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