2011-03-19 01:11:39 +08: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2013-01-26 06:50:58 +08:00
|
|
|
#include "llvm/DebugInfo/DIContext.h"
|
2012-10-03 05:18:39 +08:00
|
|
|
#include "llvm/ExecutionEngine/ObjectBuffer.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/ExecutionEngine/ObjectImage.h"
|
|
|
|
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
2013-04-27 04:07:33 +08:00
|
|
|
#include "llvm/Object/MachO.h"
|
2011-03-19 01:11:39 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-05-14 06:37:41 +08:00
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
2011-03-19 01:11:39 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/Memory.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2013-11-05 17:33:43 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
2011-03-19 01:11:39 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2011-03-19 01:11:39 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
2011-04-13 23:49:40 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
InputFileList(cl::Positional, cl::ZeroOrMore,
|
|
|
|
cl::desc("<input file>"));
|
2011-03-19 01:11:39 +08:00
|
|
|
|
|
|
|
enum ActionType {
|
2013-01-26 06:50:58 +08:00
|
|
|
AC_Execute,
|
|
|
|
AC_PrintLineInfo
|
2011-03-19 01:11:39 +08: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-26 06:50:58 +08:00
|
|
|
clEnumValN(AC_PrintLineInfo, "printline",
|
|
|
|
"Load, link, and print line information for each function."),
|
2011-03-19 01:11:39 +08:00
|
|
|
clEnumValEnd));
|
|
|
|
|
2011-04-13 23:38:30 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
EntryPoint("entry",
|
|
|
|
cl::desc("Function to call as entry point."),
|
|
|
|
cl::init("_main"));
|
|
|
|
|
2014-05-14 06:37:41 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
Dylibs("dylib",
|
|
|
|
cl::desc("Add library."),
|
|
|
|
cl::ZeroOrMore);
|
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
/* *** */
|
|
|
|
|
2011-04-05 07:04:39 +08: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 08:23:32 +08:00
|
|
|
SmallVector<sys::MemoryBlock, 16> FunctionMemory;
|
2012-01-17 06:26:39 +08:00
|
|
|
SmallVector<sys::MemoryBlock, 16> DataMemory;
|
|
|
|
|
|
|
|
uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
|
2014-03-08 16:27:28 +08:00
|
|
|
unsigned SectionID,
|
|
|
|
StringRef SectionName) override;
|
2012-01-17 06:26:39 +08:00
|
|
|
uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned SectionID, StringRef SectionName,
|
2014-03-08 16:27:28 +08:00
|
|
|
bool IsReadOnly) override;
|
2011-04-12 08:23:32 +08:00
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
void *getPointerToNamedFunction(const std::string &Name,
|
|
|
|
bool AbortOnFailure = true) override {
|
2014-04-25 12:24:47 +08:00
|
|
|
return nullptr;
|
2012-03-29 05:46:36 +08:00
|
|
|
}
|
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
bool finalizeMemory(std::string *ErrMsg) override { return false; }
|
2012-11-16 07:50:01 +08:00
|
|
|
|
2012-05-17 02:50:11 +08: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 07:04:39 +08:00
|
|
|
};
|
|
|
|
|
2012-01-17 06:26:39 +08:00
|
|
|
uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
|
|
|
|
unsigned Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned SectionID,
|
|
|
|
StringRef SectionName) {
|
2014-04-25 12:24:47 +08:00
|
|
|
sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
|
2012-05-17 02:50:11 +08:00
|
|
|
FunctionMemory.push_back(MB);
|
|
|
|
return (uint8_t*)MB.base();
|
2012-01-17 06:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
|
|
|
|
unsigned Alignment,
|
2012-11-16 07:50:01 +08:00
|
|
|
unsigned SectionID,
|
2013-10-02 08:59:25 +08:00
|
|
|
StringRef SectionName,
|
2012-11-16 07:50:01 +08:00
|
|
|
bool IsReadOnly) {
|
2014-04-25 12:24:47 +08:00
|
|
|
sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
|
2012-05-17 02:50:11 +08:00
|
|
|
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-17 06:26:39 +08:00
|
|
|
}
|
|
|
|
|
2011-03-19 01:11:39 +08: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;
|
|
|
|
}
|
|
|
|
|
2014-05-14 06:37:41 +08:00
|
|
|
static void loadDylibs() {
|
|
|
|
for (const std::string &Dylib : Dylibs) {
|
|
|
|
if (sys::fs::is_regular_file(Dylib)) {
|
|
|
|
std::string ErrMsg;
|
|
|
|
if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
|
|
|
|
llvm::errs() << "Error loading '" << Dylib << "': "
|
|
|
|
<< ErrMsg << "\n";
|
|
|
|
} else
|
|
|
|
llvm::errs() << "Dylib not found: '" << Dylib << "'.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
/* *** */
|
2011-03-19 02:54:32 +08:00
|
|
|
|
2013-01-26 06:50:58 +08:00
|
|
|
static int printLineInfoForInput() {
|
2014-05-14 06:37:41 +08:00
|
|
|
// Load any dylibs requested on the command line.
|
|
|
|
loadDylibs();
|
|
|
|
|
2013-01-26 06:50:58 +08: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) {
|
|
|
|
// Instantiate a dynamic linker.
|
2013-08-04 06:16:31 +08:00
|
|
|
TrivialMemoryManager MemMgr;
|
|
|
|
RuntimeDyld Dyld(&MemMgr);
|
2013-01-26 06:50:58 +08:00
|
|
|
|
|
|
|
// Load the input memory buffer.
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> InputBuffer;
|
|
|
|
std::unique_ptr<ObjectImage> LoadedObject;
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code ec =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(InputFileList[i], InputBuffer))
|
2013-01-26 06:50:58 +08:00
|
|
|
return Error("unable to read input: '" + ec.message() + "'");
|
|
|
|
|
|
|
|
// Load the object file
|
2014-03-05 18:19:29 +08:00
|
|
|
LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.release())));
|
2013-01-26 06:50:58 +08:00
|
|
|
if (!LoadedObject) {
|
|
|
|
return Error(Dyld.getErrorString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve all the relocations we can.
|
|
|
|
Dyld.resolveRelocations();
|
|
|
|
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<DIContext> Context(
|
|
|
|
DIContext::getDWARFContext(LoadedObject->getObjectFile()));
|
2013-01-26 06:50:58 +08:00
|
|
|
|
|
|
|
// Use symbol info to iterate functions in the object.
|
|
|
|
for (object::symbol_iterator I = LoadedObject->begin_symbols(),
|
|
|
|
E = LoadedObject->end_symbols();
|
2014-01-30 10:49:50 +08:00
|
|
|
I != E; ++I) {
|
2013-01-26 06:50:58 +08:00
|
|
|
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 08:28:05 +08: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 << ": "
|
2014-04-19 05:36:39 +08:00
|
|
|
<< It->second.FileName << ", line:" << It->second.Line << "\n";
|
2013-01-26 08:28:05 +08:00
|
|
|
}
|
2013-01-26 06:50:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-19 02:54:32 +08:00
|
|
|
static int executeInput() {
|
2014-05-14 06:37:41 +08:00
|
|
|
// Load any dylibs requested on the command line.
|
|
|
|
loadDylibs();
|
|
|
|
|
2011-03-22 06:15:52 +08:00
|
|
|
// Instantiate a dynamic linker.
|
2013-08-04 06:16:31 +08:00
|
|
|
TrivialMemoryManager MemMgr;
|
|
|
|
RuntimeDyld Dyld(&MemMgr);
|
2011-03-19 02:54:32 +08:00
|
|
|
|
2011-04-13 23:49:40 +08: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.
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> InputBuffer;
|
|
|
|
std::unique_ptr<ObjectImage> LoadedObject;
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code ec =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(InputFileList[i], InputBuffer))
|
2011-04-13 23:49:40 +08:00
|
|
|
return Error("unable to read input: '" + ec.message() + "'");
|
|
|
|
|
2012-10-03 05:18:39 +08:00
|
|
|
// Load the object file
|
2014-03-05 18:19:29 +08:00
|
|
|
LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.release())));
|
2012-10-03 05:18:39 +08:00
|
|
|
if (!LoadedObject) {
|
2011-04-13 23:49:40 +08:00
|
|
|
return Error(Dyld.getErrorString());
|
|
|
|
}
|
2011-03-23 02:19:42 +08:00
|
|
|
}
|
2011-04-13 23:49:40 +08: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-13 05:20:41 +08:00
|
|
|
// Resolve all the relocations we can.
|
|
|
|
Dyld.resolveRelocations();
|
2012-05-17 02:50:11 +08:00
|
|
|
// Clear instruction cache before code will be executed.
|
2013-08-04 06:18:45 +08:00
|
|
|
MemMgr.invalidateInstructionCache();
|
2011-03-19 02:54:32 +08:00
|
|
|
|
2011-04-13 23:49:40 +08:00
|
|
|
// FIXME: Error out if there are unresolved relocations.
|
|
|
|
|
2011-04-13 23:38:30 +08:00
|
|
|
// Get the address of the entry point (_main by default).
|
|
|
|
void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
|
2014-04-25 12:24:47 +08:00
|
|
|
if (!MainAddress)
|
2011-04-13 23:38:30 +08:00
|
|
|
return Error("no definition for '" + EntryPoint + "'");
|
2011-03-19 01:11:39 +08:00
|
|
|
|
2011-04-12 08:23:32 +08:00
|
|
|
// Invalidate the instruction cache for each loaded function.
|
2013-08-04 06:18:45 +08:00
|
|
|
for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) {
|
|
|
|
sys::MemoryBlock &Data = MemMgr.FunctionMemory[i];
|
2011-04-12 08:23:32 +08:00
|
|
|
// 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-19 01:11:39 +08:00
|
|
|
|
|
|
|
// Dispatch to _main().
|
2011-04-13 23:49:40 +08:00
|
|
|
errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
|
2011-03-19 01:11:39 +08:00
|
|
|
|
|
|
|
int (*Main)(int, const char**) =
|
|
|
|
(int(*)(int,const char**)) uintptr_t(MainAddress);
|
|
|
|
const char **Argv = new const char*[2];
|
2011-04-13 23:49:40 +08:00
|
|
|
// Use the name of the first input object module as argv[0] for the target.
|
|
|
|
Argv[0] = InputFileList[0].c_str();
|
2014-04-25 12:24:47 +08:00
|
|
|
Argv[1] = nullptr;
|
2011-03-19 01:11:39 +08:00
|
|
|
return Main(1, Argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2013-11-05 17:33:43 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
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-19 01:24:21 +08:00
|
|
|
return executeInput();
|
2013-01-26 06:50:58 +08:00
|
|
|
case AC_PrintLineInfo:
|
|
|
|
return printLineInfoForInput();
|
2011-03-19 01:11:39 +08:00
|
|
|
}
|
|
|
|
}
|