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