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 | |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 27 | static cl::list<std::string> |
| 28 | InputFileList(cl::Positional, cl::ZeroOrMore, |
| 29 | cl::desc("<input file>")); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 30 | |
| 31 | enum ActionType { |
| 32 | AC_Execute |
| 33 | }; |
| 34 | |
| 35 | static cl::opt<ActionType> |
| 36 | Action(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 | |
Jim Grosbach | 6b32e7e | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 42 | static cl::opt<std::string> |
| 43 | EntryPoint("entry", |
| 44 | cl::desc("Function to call as entry point."), |
| 45 | cl::init("_main")); |
| 46 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 47 | /* *** */ |
| 48 | |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 49 | // A trivial memory manager that doesn't do anything fancy, just uses the |
| 50 | // support library allocation routines directly. |
| 51 | class TrivialMemoryManager : public RTDyldMemoryManager { |
| 52 | public: |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 53 | SmallVector<sys::MemoryBlock, 16> FunctionMemory; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 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); |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 60 | |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 61 | virtual void *getPointerToNamedFunction(const std::string &Name, |
| 62 | bool AbortOnFailure = true) { |
| 63 | return 0; |
| 64 | } |
| 65 | |
Danil Malyshev | 068c65b | 2012-05-16 18:50:11 +0000 | [diff] [blame^] | 66 | // Invalidate instruction cache for sections with execute permissions. |
| 67 | // Some platforms with separate data cache and instruction cache require |
| 68 | // explicit cache flush, otherwise JIT code manipulations (like resolved |
| 69 | // relocations) will get to the data cache but not to the instruction cache. |
| 70 | virtual void invalidateInstructionCache(); |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 73 | uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size, |
| 74 | unsigned Alignment, |
| 75 | unsigned SectionID) { |
Danil Malyshev | 068c65b | 2012-05-16 18:50:11 +0000 | [diff] [blame^] | 76 | sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0); |
| 77 | FunctionMemory.push_back(MB); |
| 78 | return (uint8_t*)MB.base(); |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size, |
| 82 | unsigned Alignment, |
| 83 | unsigned SectionID) { |
Danil Malyshev | 068c65b | 2012-05-16 18:50:11 +0000 | [diff] [blame^] | 84 | sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0); |
| 85 | DataMemory.push_back(MB); |
| 86 | return (uint8_t*)MB.base(); |
| 87 | } |
| 88 | |
| 89 | void TrivialMemoryManager::invalidateInstructionCache() { |
| 90 | for (int i = 0, e = FunctionMemory.size(); i != e; ++i) |
| 91 | sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(), |
| 92 | FunctionMemory[i].size()); |
| 93 | |
| 94 | for (int i = 0, e = DataMemory.size(); i != e; ++i) |
| 95 | sys::Memory::InvalidateInstructionCache(DataMemory[i].base(), |
| 96 | DataMemory[i].size()); |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 99 | static const char *ProgramName; |
| 100 | |
| 101 | static void Message(const char *Type, const Twine &Msg) { |
| 102 | errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; |
| 103 | } |
| 104 | |
| 105 | static int Error(const Twine &Msg) { |
| 106 | Message("error", Msg); |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | /* *** */ |
| 111 | |
Jim Grosbach | 82c25b4 | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 112 | static int executeInput() { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 113 | // Instantiate a dynamic linker. |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 114 | TrivialMemoryManager *MemMgr = new TrivialMemoryManager; |
| 115 | RuntimeDyld Dyld(MemMgr); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 116 | |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 117 | // If we don't have any input files, read from stdin. |
| 118 | if (!InputFileList.size()) |
| 119 | InputFileList.push_back("-"); |
| 120 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 121 | // Load the input memory buffer. |
| 122 | OwningPtr<MemoryBuffer> InputBuffer; |
| 123 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i], |
| 124 | InputBuffer)) |
| 125 | return Error("unable to read input: '" + ec.message() + "'"); |
| 126 | |
| 127 | // Load the object file into it. |
| 128 | if (Dyld.loadObject(InputBuffer.take())) { |
| 129 | return Error(Dyld.getErrorString()); |
| 130 | } |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 131 | } |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 132 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 133 | // Resolve all the relocations we can. |
| 134 | Dyld.resolveRelocations(); |
Danil Malyshev | 068c65b | 2012-05-16 18:50:11 +0000 | [diff] [blame^] | 135 | // Clear instruction cache before code will be executed. |
| 136 | MemMgr->invalidateInstructionCache(); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 137 | |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 138 | // FIXME: Error out if there are unresolved relocations. |
| 139 | |
Jim Grosbach | 6b32e7e | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 140 | // Get the address of the entry point (_main by default). |
| 141 | void *MainAddress = Dyld.getSymbolAddress(EntryPoint); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 142 | if (MainAddress == 0) |
Jim Grosbach | 6b32e7e | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 143 | return Error("no definition for '" + EntryPoint + "'"); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 144 | |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 145 | // Invalidate the instruction cache for each loaded function. |
| 146 | for (unsigned i = 0, e = MemMgr->FunctionMemory.size(); i != e; ++i) { |
| 147 | sys::MemoryBlock &Data = MemMgr->FunctionMemory[i]; |
| 148 | // Make sure the memory is executable. |
| 149 | std::string ErrorStr; |
| 150 | sys::Memory::InvalidateInstructionCache(Data.base(), Data.size()); |
| 151 | if (!sys::Memory::setExecutable(Data, &ErrorStr)) |
| 152 | return Error("unable to mark function executable: '" + ErrorStr + "'"); |
| 153 | } |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 154 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 155 | // Dispatch to _main(). |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 156 | errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n"; |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 157 | |
| 158 | int (*Main)(int, const char**) = |
| 159 | (int(*)(int,const char**)) uintptr_t(MainAddress); |
| 160 | const char **Argv = new const char*[2]; |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 161 | // Use the name of the first input object module as argv[0] for the target. |
| 162 | Argv[0] = InputFileList[0].c_str(); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 163 | Argv[1] = 0; |
| 164 | return Main(1, Argv); |
| 165 | } |
| 166 | |
| 167 | int main(int argc, char **argv) { |
| 168 | ProgramName = argv[0]; |
| 169 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 170 | |
| 171 | cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); |
| 172 | |
| 173 | switch (Action) { |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 174 | case AC_Execute: |
Jim Grosbach | 82c25b4 | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 175 | return executeInput(); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 176 | } |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 177 | } |