Jim Grosbach | 0072cdb | 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 | |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringMap.h" |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DIContext.h" |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/ObjectBuffer.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 17 | #include "llvm/ExecutionEngine/ObjectImage.h" |
| 18 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 19 | #include "llvm/Object/MachO.h" |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
| 21 | #include "llvm/Support/ManagedStatic.h" |
| 22 | #include "llvm/Support/Memory.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
Alp Toker | 9f67932 | 2013-11-05 09:33:43 +0000 | [diff] [blame] | 24 | #include "llvm/Support/PrettyStackTrace.h" |
| 25 | #include "llvm/Support/Signals.h" |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/Support/system_error.h" |
| 28 | using namespace llvm; |
| 29 | using namespace llvm::object; |
| 30 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 31 | static cl::list<std::string> |
| 32 | InputFileList(cl::Positional, cl::ZeroOrMore, |
| 33 | cl::desc("<input file>")); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 34 | |
| 35 | enum ActionType { |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 36 | AC_Execute, |
| 37 | AC_PrintLineInfo |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static cl::opt<ActionType> |
| 41 | Action(cl::desc("Action to perform:"), |
| 42 | cl::init(AC_Execute), |
| 43 | cl::values(clEnumValN(AC_Execute, "execute", |
| 44 | "Load, link, and execute the inputs."), |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 45 | clEnumValN(AC_PrintLineInfo, "printline", |
| 46 | "Load, link, and print line information for each function."), |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 47 | clEnumValEnd)); |
| 48 | |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 49 | static cl::opt<std::string> |
| 50 | EntryPoint("entry", |
| 51 | cl::desc("Function to call as entry point."), |
| 52 | cl::init("_main")); |
| 53 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 54 | /* *** */ |
| 55 | |
Jim Grosbach | 2dcef050 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 56 | // A trivial memory manager that doesn't do anything fancy, just uses the |
| 57 | // support library allocation routines directly. |
| 58 | class TrivialMemoryManager : public RTDyldMemoryManager { |
| 59 | public: |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 60 | SmallVector<sys::MemoryBlock, 16> FunctionMemory; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 61 | SmallVector<sys::MemoryBlock, 16> DataMemory; |
| 62 | |
| 63 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame^] | 64 | unsigned SectionID, |
| 65 | StringRef SectionName) override; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 66 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 67 | unsigned SectionID, StringRef SectionName, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame^] | 68 | bool IsReadOnly) override; |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 69 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame^] | 70 | void *getPointerToNamedFunction(const std::string &Name, |
| 71 | bool AbortOnFailure = true) override { |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame^] | 75 | bool finalizeMemory(std::string *ErrMsg) override { return false; } |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 76 | |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 77 | // Invalidate instruction cache for sections with execute permissions. |
| 78 | // Some platforms with separate data cache and instruction cache require |
| 79 | // explicit cache flush, otherwise JIT code manipulations (like resolved |
| 80 | // relocations) will get to the data cache but not to the instruction cache. |
| 81 | virtual void invalidateInstructionCache(); |
Jim Grosbach | 2dcef050 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 84 | uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size, |
| 85 | unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 86 | unsigned SectionID, |
| 87 | StringRef SectionName) { |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 88 | sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0); |
| 89 | FunctionMemory.push_back(MB); |
| 90 | return (uint8_t*)MB.base(); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size, |
| 94 | unsigned Alignment, |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 95 | unsigned SectionID, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 96 | StringRef SectionName, |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 97 | bool IsReadOnly) { |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 98 | sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, 0, 0); |
| 99 | DataMemory.push_back(MB); |
| 100 | return (uint8_t*)MB.base(); |
| 101 | } |
| 102 | |
| 103 | void TrivialMemoryManager::invalidateInstructionCache() { |
| 104 | for (int i = 0, e = FunctionMemory.size(); i != e; ++i) |
| 105 | sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(), |
| 106 | FunctionMemory[i].size()); |
| 107 | |
| 108 | for (int i = 0, e = DataMemory.size(); i != e; ++i) |
| 109 | sys::Memory::InvalidateInstructionCache(DataMemory[i].base(), |
| 110 | DataMemory[i].size()); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 113 | static const char *ProgramName; |
| 114 | |
| 115 | static void Message(const char *Type, const Twine &Msg) { |
| 116 | errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; |
| 117 | } |
| 118 | |
| 119 | static int Error(const Twine &Msg) { |
| 120 | Message("error", Msg); |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | /* *** */ |
| 125 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 126 | static int printLineInfoForInput() { |
| 127 | // If we don't have any input files, read from stdin. |
| 128 | if (!InputFileList.size()) |
| 129 | InputFileList.push_back("-"); |
| 130 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 131 | // Instantiate a dynamic linker. |
Benjamin Kramer | 9ce7708 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 132 | TrivialMemoryManager MemMgr; |
| 133 | RuntimeDyld Dyld(&MemMgr); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 134 | |
| 135 | // Load the input memory buffer. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 136 | std::unique_ptr<MemoryBuffer> InputBuffer; |
| 137 | std::unique_ptr<ObjectImage> LoadedObject; |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 138 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i], |
| 139 | InputBuffer)) |
| 140 | return Error("unable to read input: '" + ec.message() + "'"); |
| 141 | |
| 142 | // Load the object file |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 143 | LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.release()))); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 144 | if (!LoadedObject) { |
| 145 | return Error(Dyld.getErrorString()); |
| 146 | } |
| 147 | |
| 148 | // Resolve all the relocations we can. |
| 149 | Dyld.resolveRelocations(); |
| 150 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 151 | std::unique_ptr<DIContext> Context( |
| 152 | DIContext::getDWARFContext(LoadedObject->getObjectFile())); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 153 | |
| 154 | // Use symbol info to iterate functions in the object. |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 155 | for (object::symbol_iterator I = LoadedObject->begin_symbols(), |
| 156 | E = LoadedObject->end_symbols(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 157 | I != E; ++I) { |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 158 | object::SymbolRef::Type SymType; |
| 159 | if (I->getType(SymType)) continue; |
| 160 | if (SymType == object::SymbolRef::ST_Function) { |
| 161 | StringRef Name; |
| 162 | uint64_t Addr; |
| 163 | uint64_t Size; |
| 164 | if (I->getName(Name)) continue; |
| 165 | if (I->getAddress(Addr)) continue; |
| 166 | if (I->getSize(Size)) continue; |
| 167 | |
| 168 | outs() << "Function: " << Name << ", Size = " << Size << "\n"; |
| 169 | |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 170 | DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size); |
| 171 | DILineInfoTable::iterator Begin = Lines.begin(); |
| 172 | DILineInfoTable::iterator End = Lines.end(); |
| 173 | for (DILineInfoTable::iterator It = Begin; It != End; ++It) { |
| 174 | outs() << " Line info @ " << It->first - Addr << ": " |
| 175 | << It->second.getFileName() |
| 176 | << ", line:" << It->second.getLine() << "\n"; |
| 177 | } |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Jim Grosbach | 4d5284b | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 185 | static int executeInput() { |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 186 | // Instantiate a dynamic linker. |
Benjamin Kramer | 9ce7708 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 187 | TrivialMemoryManager MemMgr; |
| 188 | RuntimeDyld Dyld(&MemMgr); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 189 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 190 | // If we don't have any input files, read from stdin. |
| 191 | if (!InputFileList.size()) |
| 192 | InputFileList.push_back("-"); |
| 193 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 194 | // Load the input memory buffer. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 195 | std::unique_ptr<MemoryBuffer> InputBuffer; |
| 196 | std::unique_ptr<ObjectImage> LoadedObject; |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 197 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i], |
| 198 | InputBuffer)) |
| 199 | return Error("unable to read input: '" + ec.message() + "'"); |
| 200 | |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 201 | // Load the object file |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 202 | LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.release()))); |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 203 | if (!LoadedObject) { |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 204 | return Error(Dyld.getErrorString()); |
| 205 | } |
Jim Grosbach | 40411cc | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 206 | } |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 207 | |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 208 | // Resolve all the relocations we can. |
| 209 | Dyld.resolveRelocations(); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 210 | // Clear instruction cache before code will be executed. |
Benjamin Kramer | 5d62ad2 | 2013-08-03 22:18:45 +0000 | [diff] [blame] | 211 | MemMgr.invalidateInstructionCache(); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 212 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 213 | // FIXME: Error out if there are unresolved relocations. |
| 214 | |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 215 | // Get the address of the entry point (_main by default). |
| 216 | void *MainAddress = Dyld.getSymbolAddress(EntryPoint); |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 217 | if (MainAddress == 0) |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 218 | return Error("no definition for '" + EntryPoint + "'"); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 219 | |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 220 | // Invalidate the instruction cache for each loaded function. |
Benjamin Kramer | 5d62ad2 | 2013-08-03 22:18:45 +0000 | [diff] [blame] | 221 | for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) { |
| 222 | sys::MemoryBlock &Data = MemMgr.FunctionMemory[i]; |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 223 | // Make sure the memory is executable. |
| 224 | std::string ErrorStr; |
| 225 | sys::Memory::InvalidateInstructionCache(Data.base(), Data.size()); |
| 226 | if (!sys::Memory::setExecutable(Data, &ErrorStr)) |
| 227 | return Error("unable to mark function executable: '" + ErrorStr + "'"); |
| 228 | } |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 229 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 230 | // Dispatch to _main(). |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 231 | errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n"; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 232 | |
| 233 | int (*Main)(int, const char**) = |
| 234 | (int(*)(int,const char**)) uintptr_t(MainAddress); |
| 235 | const char **Argv = new const char*[2]; |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 236 | // Use the name of the first input object module as argv[0] for the target. |
| 237 | Argv[0] = InputFileList[0].c_str(); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 238 | Argv[1] = 0; |
| 239 | return Main(1, Argv); |
| 240 | } |
| 241 | |
| 242 | int main(int argc, char **argv) { |
Alp Toker | 9f67932 | 2013-11-05 09:33:43 +0000 | [diff] [blame] | 243 | sys::PrintStackTraceOnErrorSignal(); |
| 244 | PrettyStackTraceProgram X(argc, argv); |
| 245 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 246 | ProgramName = argv[0]; |
| 247 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 248 | |
| 249 | cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); |
| 250 | |
| 251 | switch (Action) { |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 252 | case AC_Execute: |
Jim Grosbach | 4d5284b | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 253 | return executeInput(); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 254 | case AC_PrintLineInfo: |
| 255 | return printLineInfoForInput(); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 256 | } |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 257 | } |