2011-03-18 18:11:39 +01:00
|
|
|
//===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is a testing tool for use with the MC-JIT LLVM components.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2013-01-25 23:50:58 +01:00
|
|
|
#include "llvm/DebugInfo/DIContext.h"
|
2012-10-02 23:18:39 +02:00
|
|
|
#include "llvm/ExecutionEngine/ObjectBuffer.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/ExecutionEngine/ObjectImage.h"
|
|
|
|
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
2013-04-26 22:07:33 +02:00
|
|
|
#include "llvm/Object/MachO.h"
|
2011-03-18 18:11:39 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/Memory.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/system_error.h"
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
2011-04-13 17:49:40 +02:00
|
|
|
static cl::list<std::string>
|
|
|
|
InputFileList(cl::Positional, cl::ZeroOrMore,
|
|
|
|
cl::desc("<input file>"));
|
2011-03-18 18:11:39 +01:00
|
|
|
|
|
|
|
enum ActionType {
|
2013-01-25 23:50:58 +01:00
|
|
|
AC_Execute,
|
|
|
|
AC_PrintLineInfo
|
2011-03-18 18:11:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static cl::opt<ActionType>
|
|
|
|
Action(cl::desc("Action to perform:"),
|
|
|
|
cl::init(AC_Execute),
|
|
|
|
cl::values(clEnumValN(AC_Execute, "execute",
|
|
|
|
"Load, link, and execute the inputs."),
|
2013-01-25 23:50:58 +01:00
|
|
|
clEnumValN(AC_PrintLineInfo, "printline",
|
|
|
|
"Load, link, and print line information for each function."),
|
2011-03-18 18:11:39 +01:00
|
|
|
clEnumValEnd));
|
|
|
|
|
2011-04-13 17:38:30 +02:00
|
|
|
static cl::opt<std::string>
|
|
|
|
EntryPoint("entry",
|
|
|
|
cl::desc("Function to call as entry point."),
|
|
|
|
cl::init("_main"));
|
|
|
|
|
2011-03-18 18:11:39 +01:00
|
|
|
/* *** */
|
|
|
|
|
2011-04-05 01:04:39 +02:00
|
|
|
// A trivial memory manager that doesn't do anything fancy, just uses the
|
|
|
|
// support library allocation routines directly.
|
|
|
|
class TrivialMemoryManager : public RTDyldMemoryManager {
|
|
|
|
public:
|
2011-04-12 02:23:32 +02:00
|
|
|
SmallVector<sys::MemoryBlock, 16> FunctionMemory;
|
2012-01-16 23:26:39 +01:00
|
|
|
SmallVector<sys::MemoryBlock, 16> DataMemory;
|
|
|
|
|
|
|
|
uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
|
|
|
|
unsigned SectionID);
|
|
|
|
uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
|
2012-11-16 00:50:01 +01:00
|
|
|
unsigned SectionID, bool IsReadOnly);
|
2011-04-12 02:23:32 +02:00
|
|
|
|
2012-03-28 23:46:36 +02:00
|
|
|
virtual void *getPointerToNamedFunction(const std::string &Name,
|
|
|
|
bool AbortOnFailure = true) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-17 12:01:46 +02:00
|
|
|
bool finalizeMemory(std::string *ErrMsg) { return false; }
|
2012-11-16 00:50:01 +01:00
|
|
|
|
2012-05-16 20:50:11 +02:00
|
|
|
// Invalidate instruction cache for sections with execute permissions.
|
|
|
|
// Some platforms with separate data cache and instruction cache require
|
|
|
|
// explicit cache flush, otherwise JIT code manipulations (like resolved
|
|
|
|
// relocations) will get to the data cache but not to the instruction cache.
|
|
|
|
virtual void invalidateInstructionCache();
|
2011-04-05 01:04:39 +02:00
|
|
|
};
|
|
|
|
|
2012-01-16 23:26:39 +01:00
|
|
|
uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
|
|
|
|
unsigned Alignment,
|
|
|
|
unsigned SectionID) {
|
2012-05-16 20:50:11 +02:00
|
|
|
sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
|
|
|
|
FunctionMemory.push_back(MB);
|
|
|
|
return (uint8_t*)MB.base();
|
2012-01-16 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
|
|
|
|
unsigned Alignment,
|
2012-11-16 00:50:01 +01:00
|
|
|
unsigned SectionID,
|
|
|
|
bool IsReadOnly) {
|
2012-05-16 20:50:11 +02:00
|
|
|
sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0);
|
|
|
|
DataMemory.push_back(MB);
|
|
|
|
return (uint8_t*)MB.base();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrivialMemoryManager::invalidateInstructionCache() {
|
|
|
|
for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
|
|
|
|
sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
|
|
|
|
FunctionMemory[i].size());
|
|
|
|
|
|
|
|
for (int i = 0, e = DataMemory.size(); i != e; ++i)
|
|
|
|
sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
|
|
|
|
DataMemory[i].size());
|
2012-01-16 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
2011-03-18 18:11:39 +01:00
|
|
|
static const char *ProgramName;
|
|
|
|
|
|
|
|
static void Message(const char *Type, const Twine &Msg) {
|
|
|
|
errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static int Error(const Twine &Msg) {
|
|
|
|
Message("error", Msg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* *** */
|
2011-03-18 19:54:32 +01:00
|
|
|
|
2013-01-25 23:50:58 +01:00
|
|
|
static int printLineInfoForInput() {
|
|
|
|
// If we don't have any input files, read from stdin.
|
|
|
|
if (!InputFileList.size())
|
|
|
|
InputFileList.push_back("-");
|
|
|
|
for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
|
|
|
|
// Instantiate a dynamic linker.
|
|
|
|
TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
|
|
|
|
RuntimeDyld Dyld(MemMgr);
|
|
|
|
|
|
|
|
// Load the input memory buffer.
|
|
|
|
OwningPtr<MemoryBuffer> InputBuffer;
|
|
|
|
OwningPtr<ObjectImage> LoadedObject;
|
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
|
|
|
|
InputBuffer))
|
|
|
|
return Error("unable to read input: '" + ec.message() + "'");
|
|
|
|
|
|
|
|
// Load the object file
|
|
|
|
LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.take())));
|
|
|
|
if (!LoadedObject) {
|
|
|
|
return Error(Dyld.getErrorString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve all the relocations we can.
|
|
|
|
Dyld.resolveRelocations();
|
|
|
|
|
|
|
|
OwningPtr<DIContext> Context(DIContext::getDWARFContext(LoadedObject->getObjectFile()));
|
|
|
|
|
|
|
|
// Use symbol info to iterate functions in the object.
|
|
|
|
error_code ec;
|
|
|
|
for (object::symbol_iterator I = LoadedObject->begin_symbols(),
|
|
|
|
E = LoadedObject->end_symbols();
|
|
|
|
I != E && !ec;
|
|
|
|
I.increment(ec)) {
|
|
|
|
object::SymbolRef::Type SymType;
|
|
|
|
if (I->getType(SymType)) continue;
|
|
|
|
if (SymType == object::SymbolRef::ST_Function) {
|
|
|
|
StringRef Name;
|
|
|
|
uint64_t Addr;
|
|
|
|
uint64_t Size;
|
|
|
|
if (I->getName(Name)) continue;
|
|
|
|
if (I->getAddress(Addr)) continue;
|
|
|
|
if (I->getSize(Size)) continue;
|
|
|
|
|
|
|
|
outs() << "Function: " << Name << ", Size = " << Size << "\n";
|
|
|
|
|
2013-01-26 01:28:05 +01:00
|
|
|
DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
|
|
|
|
DILineInfoTable::iterator Begin = Lines.begin();
|
|
|
|
DILineInfoTable::iterator End = Lines.end();
|
|
|
|
for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
|
|
|
|
outs() << " Line info @ " << It->first - Addr << ": "
|
|
|
|
<< It->second.getFileName()
|
|
|
|
<< ", line:" << It->second.getLine() << "\n";
|
|
|
|
}
|
2013-01-25 23:50:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-18 19:54:32 +01:00
|
|
|
static int executeInput() {
|
2011-03-21 23:15:52 +01:00
|
|
|
// Instantiate a dynamic linker.
|
2011-04-12 02:23:32 +02:00
|
|
|
TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
|
|
|
|
RuntimeDyld Dyld(MemMgr);
|
2011-03-18 19:54:32 +01:00
|
|
|
|
2011-04-13 17:49:40 +02:00
|
|
|
// If we don't have any input files, read from stdin.
|
|
|
|
if (!InputFileList.size())
|
|
|
|
InputFileList.push_back("-");
|
|
|
|
for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
|
|
|
|
// Load the input memory buffer.
|
|
|
|
OwningPtr<MemoryBuffer> InputBuffer;
|
2012-10-02 23:18:39 +02:00
|
|
|
OwningPtr<ObjectImage> LoadedObject;
|
2011-04-13 17:49:40 +02:00
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
|
|
|
|
InputBuffer))
|
|
|
|
return Error("unable to read input: '" + ec.message() + "'");
|
|
|
|
|
2012-10-02 23:18:39 +02:00
|
|
|
// Load the object file
|
|
|
|
LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.take())));
|
|
|
|
if (!LoadedObject) {
|
2011-04-13 17:49:40 +02:00
|
|
|
return Error(Dyld.getErrorString());
|
|
|
|
}
|
2011-03-22 19:19:42 +01:00
|
|
|
}
|
2011-04-13 17:49:40 +02:00
|
|
|
|
MCJIT lazy relocation resolution and symbol address re-assignment.
Add handling for tracking the relocations on symbols and resolving them.
Keep track of the relocations even after they are resolved so that if
the RuntimeDyld client moves the object, it can update the address and any
relocations to that object will be updated.
For our trival object file load/run test harness (llvm-rtdyld), this enables
relocations between functions located in the same object module. It should
be trivially extendable to load multiple objects with mutual references.
As a simple example, the following now works (running on x86_64 Darwin 10.6):
$ cat t.c
int bar() {
return 65;
}
int main() {
return bar();
}
$ clang t.c -fno-asynchronous-unwind-tables -o t.o -c
$ otool -vt t.o
t.o:
(__TEXT,__text) section
_bar:
0000000000000000 pushq %rbp
0000000000000001 movq %rsp,%rbp
0000000000000004 movl $0x00000041,%eax
0000000000000009 popq %rbp
000000000000000a ret
000000000000000b nopl 0x00(%rax,%rax)
_main:
0000000000000010 pushq %rbp
0000000000000011 movq %rsp,%rbp
0000000000000014 subq $0x10,%rsp
0000000000000018 movl $0x00000000,0xfc(%rbp)
000000000000001f callq 0x00000024
0000000000000024 addq $0x10,%rsp
0000000000000028 popq %rbp
0000000000000029 ret
$ llvm-rtdyld t.o -debug-only=dyld ; echo $?
Function sym: '_bar' @ 0
Function sym: '_main' @ 16
Extracting function: _bar from [0, 15]
allocated to 0x100153000
Extracting function: _main from [16, 41]
allocated to 0x100154000
Relocation at '_main' + 16 from '_bar(Word1: 0x2d000000)
Resolving relocation at '_main' + 16 (0x100154010) from '_bar (0x100153000)(pcrel, type: 2, Size: 4).
loaded '_main' at: 0x100154000
65
$
llvm-svn: 129388
2011-04-12 23:20:41 +02:00
|
|
|
// Resolve all the relocations we can.
|
|
|
|
Dyld.resolveRelocations();
|
2012-05-16 20:50:11 +02:00
|
|
|
// Clear instruction cache before code will be executed.
|
|
|
|
MemMgr->invalidateInstructionCache();
|
2011-03-18 19:54:32 +01:00
|
|
|
|
2011-04-13 17:49:40 +02:00
|
|
|
// FIXME: Error out if there are unresolved relocations.
|
|
|
|
|
2011-04-13 17:38:30 +02:00
|
|
|
// Get the address of the entry point (_main by default).
|
|
|
|
void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
|
2011-03-21 23:15:52 +01:00
|
|
|
if (MainAddress == 0)
|
2011-04-13 17:38:30 +02:00
|
|
|
return Error("no definition for '" + EntryPoint + "'");
|
2011-03-18 18:11:39 +01:00
|
|
|
|
2011-04-12 02:23:32 +02:00
|
|
|
// Invalidate the instruction cache for each loaded function.
|
|
|
|
for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) {
|
|
|
|
sys::MemoryBlock &Data = MemMgr->FunctionMemory[i];
|
|
|
|
// Make sure the memory is executable.
|
|
|
|
std::string ErrorStr;
|
|
|
|
sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
|
|
|
|
if (!sys::Memory::setExecutable(Data, &ErrorStr))
|
|
|
|
return Error("unable to mark function executable: '" + ErrorStr + "'");
|
|
|
|
}
|
2011-03-18 18:11:39 +01:00
|
|
|
|
|
|
|
// Dispatch to _main().
|
2011-04-13 17:49:40 +02:00
|
|
|
errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
|
2011-03-18 18:11:39 +01:00
|
|
|
|
|
|
|
int (*Main)(int, const char**) =
|
|
|
|
(int(*)(int,const char**)) uintptr_t(MainAddress);
|
|
|
|
const char **Argv = new const char*[2];
|
2011-04-13 17:49:40 +02:00
|
|
|
// Use the name of the first input object module as argv[0] for the target.
|
|
|
|
Argv[0] = InputFileList[0].c_str();
|
2011-03-18 18:11:39 +01:00
|
|
|
Argv[1] = 0;
|
|
|
|
return Main(1, Argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
ProgramName = argv[0];
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
|
|
|
|
|
|
|
|
switch (Action) {
|
|
|
|
case AC_Execute:
|
2011-03-18 18:24:21 +01:00
|
|
|
return executeInput();
|
2013-01-25 23:50:58 +01:00
|
|
|
case AC_PrintLineInfo:
|
|
|
|
return printLineInfoForInput();
|
2011-03-18 18:11:39 +01:00
|
|
|
}
|
|
|
|
}
|