Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 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" |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 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" |
| 24 | using namespace llvm; |
| 25 | using namespace llvm::object; |
| 26 | |
| 27 | static cl::opt<std::string> |
| 28 | InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 29 | |
| 30 | enum ActionType { |
| 31 | AC_Execute |
| 32 | }; |
| 33 | |
| 34 | static cl::opt<ActionType> |
| 35 | Action(cl::desc("Action to perform:"), |
| 36 | cl::init(AC_Execute), |
| 37 | cl::values(clEnumValN(AC_Execute, "execute", |
| 38 | "Load, link, and execute the inputs."), |
| 39 | clEnumValEnd)); |
| 40 | |
| 41 | /* *** */ |
| 42 | |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 43 | // A trivial memory manager that doesn't do anything fancy, just uses the |
| 44 | // support library allocation routines directly. |
| 45 | class TrivialMemoryManager : public RTDyldMemoryManager { |
| 46 | public: |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 47 | SmallVector<sys::MemoryBlock, 16> FunctionMemory; |
| 48 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 49 | uint8_t *startFunctionBody(const char *Name, uintptr_t &Size); |
| 50 | void endFunctionBody(const char *Name, uint8_t *FunctionStart, |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 51 | uint8_t *FunctionEnd); |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 54 | uint8_t *TrivialMemoryManager::startFunctionBody(const char *Name, |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 55 | uintptr_t &Size) { |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 56 | return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base(); |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 59 | void TrivialMemoryManager::endFunctionBody(const char *Name, |
| 60 | uint8_t *FunctionStart, |
| 61 | uint8_t *FunctionEnd) { |
| 62 | uintptr_t Size = FunctionEnd - FunctionStart + 1; |
| 63 | FunctionMemory.push_back(sys::MemoryBlock(FunctionStart, Size)); |
| 64 | } |
| 65 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 66 | static const char *ProgramName; |
| 67 | |
| 68 | static void Message(const char *Type, const Twine &Msg) { |
| 69 | errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; |
| 70 | } |
| 71 | |
| 72 | static int Error(const Twine &Msg) { |
| 73 | Message("error", Msg); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | /* *** */ |
| 78 | |
Jim Grosbach | 82c25b4 | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 79 | static int executeInput() { |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 80 | // Load the input memory buffer. |
| 81 | OwningPtr<MemoryBuffer> InputBuffer; |
| 82 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer)) |
| 83 | return Error("unable to read input: '" + ec.message() + "'"); |
| 84 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 85 | // Instantiate a dynamic linker. |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 86 | TrivialMemoryManager *MemMgr = new TrivialMemoryManager; |
| 87 | RuntimeDyld Dyld(MemMgr); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 88 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 89 | // Load the object file into it. |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 90 | if (Dyld.loadObject(InputBuffer.take())) { |
| 91 | return Error(Dyld.getErrorString()); |
| 92 | } |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame^] | 93 | // Resolve all the relocations we can. |
| 94 | Dyld.resolveRelocations(); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 95 | |
| 96 | // Get the address of "_main". |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 97 | void *MainAddress = Dyld.getSymbolAddress("_main"); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 98 | if (MainAddress == 0) |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 99 | return Error("no definition for '_main'"); |
| 100 | |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 101 | // Invalidate the instruction cache for each loaded function. |
| 102 | for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) { |
| 103 | sys::MemoryBlock &Data = MemMgr->FunctionMemory[i]; |
| 104 | // Make sure the memory is executable. |
| 105 | std::string ErrorStr; |
| 106 | sys::Memory::InvalidateInstructionCache(Data.base(), Data.size()); |
| 107 | if (!sys::Memory::setExecutable(Data, &ErrorStr)) |
| 108 | return Error("unable to mark function executable: '" + ErrorStr + "'"); |
| 109 | } |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 110 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 111 | |
| 112 | // Dispatch to _main(). |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 113 | errs() << "loaded '_main' at: " << (void*)MainAddress << "\n"; |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 114 | |
| 115 | int (*Main)(int, const char**) = |
| 116 | (int(*)(int,const char**)) uintptr_t(MainAddress); |
| 117 | const char **Argv = new const char*[2]; |
| 118 | Argv[0] = InputFile.c_str(); |
| 119 | Argv[1] = 0; |
| 120 | return Main(1, Argv); |
| 121 | } |
| 122 | |
| 123 | int main(int argc, char **argv) { |
| 124 | ProgramName = argv[0]; |
| 125 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 126 | |
| 127 | cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); |
| 128 | |
| 129 | switch (Action) { |
| 130 | default: |
| 131 | case AC_Execute: |
Jim Grosbach | 82c25b4 | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 132 | return executeInput(); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |