llvm-rtdyld.cpp revision 30b9e322e159df8eaabb5b194cec6e11ba99c261
1//===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This is a testing tool for use with the MC-JIT LLVM components. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/ADT/StringMap.h" 15#include "llvm/ADT/OwningPtr.h" 16#include "llvm/ExecutionEngine/RuntimeDyld.h" 17#include "llvm/Object/MachOObject.h" 18#include "llvm/Support/CommandLine.h" 19#include "llvm/Support/ManagedStatic.h" 20#include "llvm/Support/Memory.h" 21#include "llvm/Support/MemoryBuffer.h" 22#include "llvm/Support/raw_ostream.h" 23#include "llvm/Support/system_error.h" 24using namespace llvm; 25using namespace llvm::object; 26 27static cl::list<std::string> 28InputFileList(cl::Positional, cl::ZeroOrMore, 29 cl::desc("<input file>")); 30 31enum ActionType { 32 AC_Execute 33}; 34 35static cl::opt<ActionType> 36Action(cl::desc("Action to perform:"), 37 cl::init(AC_Execute), 38 cl::values(clEnumValN(AC_Execute, "execute", 39 "Load, link, and execute the inputs."), 40 clEnumValEnd)); 41 42static cl::opt<std::string> 43EntryPoint("entry", 44 cl::desc("Function to call as entry point."), 45 cl::init("_main")); 46 47/* *** */ 48 49// A trivial memory manager that doesn't do anything fancy, just uses the 50// support library allocation routines directly. 51class TrivialMemoryManager : public RTDyldMemoryManager { 52public: 53 SmallVector<sys::MemoryBlock, 16> FunctionMemory; 54 SmallVector<sys::MemoryBlock, 16> DataMemory; 55 56 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, 57 unsigned SectionID); 58 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, 59 unsigned SectionID); 60 61 uint8_t *startFunctionBody(const char *Name, uintptr_t &Size); 62 void endFunctionBody(const char *Name, uint8_t *FunctionStart, 63 uint8_t *FunctionEnd); 64 65 virtual void *getPointerToNamedFunction(const std::string &Name, 66 bool AbortOnFailure = true) { 67 return 0; 68 } 69 70}; 71 72uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size, 73 unsigned Alignment, 74 unsigned SectionID) { 75 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base(); 76} 77 78uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size, 79 unsigned Alignment, 80 unsigned SectionID) { 81 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base(); 82} 83 84uint8_t *TrivialMemoryManager::startFunctionBody(const char *Name, 85 uintptr_t &Size) { 86 return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base(); 87} 88 89void TrivialMemoryManager::endFunctionBody(const char *Name, 90 uint8_t *FunctionStart, 91 uint8_t *FunctionEnd) { 92 uintptr_t Size = FunctionEnd - FunctionStart + 1; 93 FunctionMemory.push_back(sys::MemoryBlock(FunctionStart, Size)); 94} 95 96static const char *ProgramName; 97 98static void Message(const char *Type, const Twine &Msg) { 99 errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; 100} 101 102static int Error(const Twine &Msg) { 103 Message("error", Msg); 104 return 1; 105} 106 107/* *** */ 108 109static int executeInput() { 110 // Instantiate a dynamic linker. 111 TrivialMemoryManager *MemMgr = new TrivialMemoryManager; 112 RuntimeDyld Dyld(MemMgr); 113 114 // If we don't have any input files, read from stdin. 115 if (!InputFileList.size()) 116 InputFileList.push_back("-"); 117 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { 118 // Load the input memory buffer. 119 OwningPtr<MemoryBuffer> InputBuffer; 120 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i], 121 InputBuffer)) 122 return Error("unable to read input: '" + ec.message() + "'"); 123 124 // Load the object file into it. 125 if (Dyld.loadObject(InputBuffer.take())) { 126 return Error(Dyld.getErrorString()); 127 } 128 } 129 130 // Resolve all the relocations we can. 131 Dyld.resolveRelocations(); 132 133 // FIXME: Error out if there are unresolved relocations. 134 135 // Get the address of the entry point (_main by default). 136 void *MainAddress = Dyld.getSymbolAddress(EntryPoint); 137 if (MainAddress == 0) 138 return Error("no definition for '" + EntryPoint + "'"); 139 140 // Invalidate the instruction cache for each loaded function. 141 for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) { 142 sys::MemoryBlock &Data = MemMgr->FunctionMemory[i]; 143 // Make sure the memory is executable. 144 std::string ErrorStr; 145 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size()); 146 if (!sys::Memory::setExecutable(Data, &ErrorStr)) 147 return Error("unable to mark function executable: '" + ErrorStr + "'"); 148 } 149 150 // Dispatch to _main(). 151 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n"; 152 153 int (*Main)(int, const char**) = 154 (int(*)(int,const char**)) uintptr_t(MainAddress); 155 const char **Argv = new const char*[2]; 156 // Use the name of the first input object module as argv[0] for the target. 157 Argv[0] = InputFileList[0].c_str(); 158 Argv[1] = 0; 159 return Main(1, Argv); 160} 161 162int main(int argc, char **argv) { 163 ProgramName = argv[0]; 164 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 165 166 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); 167 168 switch (Action) { 169 case AC_Execute: 170 return executeInput(); 171 } 172} 173