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" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 19 | #include "llvm/ExecutionEngine/RuntimeDyldChecker.h" |
| 20 | #include "llvm/MC/MCAsmInfo.h" |
| 21 | #include "llvm/MC/MCContext.h" |
| 22 | #include "llvm/MC/MCDisassembler.h" |
| 23 | #include "llvm/MC/MCInstrInfo.h" |
| 24 | #include "llvm/MC/MCInstPrinter.h" |
| 25 | #include "llvm/MC/MCRegisterInfo.h" |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 26 | #include "llvm/Object/MachO.h" |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 28 | #include "llvm/Support/DynamicLibrary.h" |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ManagedStatic.h" |
| 30 | #include "llvm/Support/Memory.h" |
| 31 | #include "llvm/Support/MemoryBuffer.h" |
Alp Toker | 9f67932 | 2013-11-05 09:33:43 +0000 | [diff] [blame] | 32 | #include "llvm/Support/PrettyStackTrace.h" |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Signals.h" |
| 35 | #include "llvm/Support/TargetRegistry.h" |
| 36 | #include "llvm/Support/TargetSelect.h" |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 37 | #include <system_error> |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 38 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | using namespace llvm::object; |
| 41 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 42 | static cl::list<std::string> |
| 43 | InputFileList(cl::Positional, cl::ZeroOrMore, |
| 44 | cl::desc("<input file>")); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 45 | |
| 46 | enum ActionType { |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 47 | AC_Execute, |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 48 | AC_PrintLineInfo, |
| 49 | AC_Verify |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static cl::opt<ActionType> |
| 53 | Action(cl::desc("Action to perform:"), |
| 54 | cl::init(AC_Execute), |
| 55 | cl::values(clEnumValN(AC_Execute, "execute", |
| 56 | "Load, link, and execute the inputs."), |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 57 | clEnumValN(AC_PrintLineInfo, "printline", |
| 58 | "Load, link, and print line information for each function."), |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 59 | clEnumValN(AC_Verify, "verify", |
| 60 | "Load, link and verify the resulting memory image."), |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 61 | clEnumValEnd)); |
| 62 | |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 63 | static cl::opt<std::string> |
| 64 | EntryPoint("entry", |
| 65 | cl::desc("Function to call as entry point."), |
| 66 | cl::init("_main")); |
| 67 | |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 68 | static cl::list<std::string> |
| 69 | Dylibs("dylib", |
| 70 | cl::desc("Add library."), |
| 71 | cl::ZeroOrMore); |
| 72 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 73 | static cl::opt<std::string> |
| 74 | TripleName("triple", cl::desc("Target triple for disassembler")); |
| 75 | |
| 76 | static cl::list<std::string> |
| 77 | CheckFiles("check", |
| 78 | cl::desc("File containing RuntimeDyld verifier checks."), |
| 79 | cl::ZeroOrMore); |
| 80 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 81 | /* *** */ |
| 82 | |
Jim Grosbach | 2dcef050 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 83 | // A trivial memory manager that doesn't do anything fancy, just uses the |
| 84 | // support library allocation routines directly. |
| 85 | class TrivialMemoryManager : public RTDyldMemoryManager { |
| 86 | public: |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 87 | SmallVector<sys::MemoryBlock, 16> FunctionMemory; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 88 | SmallVector<sys::MemoryBlock, 16> DataMemory; |
| 89 | |
| 90 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 91 | unsigned SectionID, |
| 92 | StringRef SectionName) override; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 93 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 94 | unsigned SectionID, StringRef SectionName, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 95 | bool IsReadOnly) override; |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 96 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 97 | void *getPointerToNamedFunction(const std::string &Name, |
| 98 | bool AbortOnFailure = true) override { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 99 | return nullptr; |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 102 | bool finalizeMemory(std::string *ErrMsg) override { return false; } |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 103 | |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 104 | // Invalidate instruction cache for sections with execute permissions. |
| 105 | // Some platforms with separate data cache and instruction cache require |
| 106 | // explicit cache flush, otherwise JIT code manipulations (like resolved |
| 107 | // relocations) will get to the data cache but not to the instruction cache. |
| 108 | virtual void invalidateInstructionCache(); |
Jim Grosbach | 2dcef050 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 111 | uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size, |
| 112 | unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 113 | unsigned SectionID, |
| 114 | StringRef SectionName) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 115 | sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 116 | FunctionMemory.push_back(MB); |
| 117 | return (uint8_t*)MB.base(); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size, |
| 121 | unsigned Alignment, |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 122 | unsigned SectionID, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 123 | StringRef SectionName, |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 124 | bool IsReadOnly) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 125 | sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 126 | DataMemory.push_back(MB); |
| 127 | return (uint8_t*)MB.base(); |
| 128 | } |
| 129 | |
| 130 | void TrivialMemoryManager::invalidateInstructionCache() { |
| 131 | for (int i = 0, e = FunctionMemory.size(); i != e; ++i) |
| 132 | sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(), |
| 133 | FunctionMemory[i].size()); |
| 134 | |
| 135 | for (int i = 0, e = DataMemory.size(); i != e; ++i) |
| 136 | sys::Memory::InvalidateInstructionCache(DataMemory[i].base(), |
| 137 | DataMemory[i].size()); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 140 | static const char *ProgramName; |
| 141 | |
| 142 | static void Message(const char *Type, const Twine &Msg) { |
| 143 | errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; |
| 144 | } |
| 145 | |
| 146 | static int Error(const Twine &Msg) { |
| 147 | Message("error", Msg); |
| 148 | return 1; |
| 149 | } |
| 150 | |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 151 | static void loadDylibs() { |
| 152 | for (const std::string &Dylib : Dylibs) { |
| 153 | if (sys::fs::is_regular_file(Dylib)) { |
| 154 | std::string ErrMsg; |
| 155 | if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg)) |
| 156 | llvm::errs() << "Error loading '" << Dylib << "': " |
| 157 | << ErrMsg << "\n"; |
| 158 | } else |
| 159 | llvm::errs() << "Dylib not found: '" << Dylib << "'.\n"; |
| 160 | } |
| 161 | } |
| 162 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 163 | /* *** */ |
| 164 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 165 | static int printLineInfoForInput() { |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 166 | // Load any dylibs requested on the command line. |
| 167 | loadDylibs(); |
| 168 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 169 | // If we don't have any input files, read from stdin. |
| 170 | if (!InputFileList.size()) |
| 171 | InputFileList.push_back("-"); |
| 172 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 173 | // Instantiate a dynamic linker. |
Benjamin Kramer | 9ce7708 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 174 | TrivialMemoryManager MemMgr; |
| 175 | RuntimeDyld Dyld(&MemMgr); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 176 | |
| 177 | // Load the input memory buffer. |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 178 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame^] | 179 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
| 180 | MemoryBuffer::getFileOrSTDIN(InputFileList[i]); |
| 181 | if (std::error_code EC = InputBuffer.getError()) |
| 182 | return Error("unable to read input: '" + EC.message() + "'"); |
| 183 | |
| 184 | std::unique_ptr<ObjectImage> LoadedObject; |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 185 | // Load the object file |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame^] | 186 | LoadedObject.reset( |
| 187 | Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release()))); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 188 | if (!LoadedObject) { |
| 189 | return Error(Dyld.getErrorString()); |
| 190 | } |
| 191 | |
| 192 | // Resolve all the relocations we can. |
| 193 | Dyld.resolveRelocations(); |
| 194 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 195 | std::unique_ptr<DIContext> Context( |
| 196 | DIContext::getDWARFContext(LoadedObject->getObjectFile())); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 197 | |
| 198 | // Use symbol info to iterate functions in the object. |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 199 | for (object::symbol_iterator I = LoadedObject->begin_symbols(), |
| 200 | E = LoadedObject->end_symbols(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 201 | I != E; ++I) { |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 202 | object::SymbolRef::Type SymType; |
| 203 | if (I->getType(SymType)) continue; |
| 204 | if (SymType == object::SymbolRef::ST_Function) { |
| 205 | StringRef Name; |
| 206 | uint64_t Addr; |
| 207 | uint64_t Size; |
| 208 | if (I->getName(Name)) continue; |
| 209 | if (I->getAddress(Addr)) continue; |
| 210 | if (I->getSize(Size)) continue; |
| 211 | |
| 212 | outs() << "Function: " << Name << ", Size = " << Size << "\n"; |
| 213 | |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 214 | DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size); |
| 215 | DILineInfoTable::iterator Begin = Lines.begin(); |
| 216 | DILineInfoTable::iterator End = Lines.end(); |
| 217 | for (DILineInfoTable::iterator It = Begin; It != End; ++It) { |
| 218 | outs() << " Line info @ " << It->first - Addr << ": " |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 219 | << It->second.FileName << ", line:" << It->second.Line << "\n"; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 220 | } |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Jim Grosbach | 4d5284b | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 228 | static int executeInput() { |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 229 | // Load any dylibs requested on the command line. |
| 230 | loadDylibs(); |
| 231 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 232 | // Instantiate a dynamic linker. |
Benjamin Kramer | 9ce7708 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 233 | TrivialMemoryManager MemMgr; |
| 234 | RuntimeDyld Dyld(&MemMgr); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 235 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 236 | // If we don't have any input files, read from stdin. |
| 237 | if (!InputFileList.size()) |
| 238 | InputFileList.push_back("-"); |
| 239 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 240 | // Load the input memory buffer. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame^] | 241 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
| 242 | MemoryBuffer::getFileOrSTDIN(InputFileList[i]); |
| 243 | if (std::error_code EC = InputBuffer.getError()) |
| 244 | return Error("unable to read input: '" + EC.message() + "'"); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 245 | std::unique_ptr<ObjectImage> LoadedObject; |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 246 | // Load the object file |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame^] | 247 | LoadedObject.reset( |
| 248 | Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release()))); |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 249 | if (!LoadedObject) { |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 250 | return Error(Dyld.getErrorString()); |
| 251 | } |
Jim Grosbach | 40411cc | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 252 | } |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 253 | |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 254 | // Resolve all the relocations we can. |
| 255 | Dyld.resolveRelocations(); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 256 | // Clear instruction cache before code will be executed. |
Benjamin Kramer | 5d62ad2 | 2013-08-03 22:18:45 +0000 | [diff] [blame] | 257 | MemMgr.invalidateInstructionCache(); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 258 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 259 | // FIXME: Error out if there are unresolved relocations. |
| 260 | |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 261 | // Get the address of the entry point (_main by default). |
| 262 | void *MainAddress = Dyld.getSymbolAddress(EntryPoint); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 263 | if (!MainAddress) |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 264 | return Error("no definition for '" + EntryPoint + "'"); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 265 | |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 266 | // Invalidate the instruction cache for each loaded function. |
Benjamin Kramer | 5d62ad2 | 2013-08-03 22:18:45 +0000 | [diff] [blame] | 267 | for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) { |
| 268 | sys::MemoryBlock &Data = MemMgr.FunctionMemory[i]; |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 269 | // Make sure the memory is executable. |
| 270 | std::string ErrorStr; |
| 271 | sys::Memory::InvalidateInstructionCache(Data.base(), Data.size()); |
| 272 | if (!sys::Memory::setExecutable(Data, &ErrorStr)) |
| 273 | return Error("unable to mark function executable: '" + ErrorStr + "'"); |
| 274 | } |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 275 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 276 | // Dispatch to _main(). |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 277 | errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n"; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 278 | |
| 279 | int (*Main)(int, const char**) = |
| 280 | (int(*)(int,const char**)) uintptr_t(MainAddress); |
| 281 | const char **Argv = new const char*[2]; |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 282 | // Use the name of the first input object module as argv[0] for the target. |
| 283 | Argv[0] = InputFileList[0].c_str(); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 284 | Argv[1] = nullptr; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 285 | return Main(1, Argv); |
| 286 | } |
| 287 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 288 | static int checkAllExpressions(RuntimeDyldChecker &Checker) { |
| 289 | for (const auto& CheckerFileName : CheckFiles) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame^] | 290 | ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf = |
| 291 | MemoryBuffer::getFileOrSTDIN(CheckerFileName); |
| 292 | if (std::error_code EC = CheckerFileBuf.getError()) |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 293 | return Error("unable to read input '" + CheckerFileName + "': " + |
| 294 | EC.message()); |
| 295 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame^] | 296 | if (!Checker.checkAllRulesInBuffer("# rtdyld-check:", |
| 297 | CheckerFileBuf.get().get())) |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 298 | return Error("some checks in '" + CheckerFileName + "' failed"); |
| 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int linkAndVerify() { |
| 304 | |
| 305 | // Check for missing triple. |
| 306 | if (TripleName == "") { |
| 307 | llvm::errs() << "Error: -triple required when running in -verify mode.\n"; |
| 308 | return 1; |
| 309 | } |
| 310 | |
| 311 | // Look up the target and build the disassembler. |
| 312 | Triple TheTriple(Triple::normalize(TripleName)); |
| 313 | std::string ErrorStr; |
| 314 | const Target *TheTarget = |
| 315 | TargetRegistry::lookupTarget("", TheTriple, ErrorStr); |
| 316 | if (!TheTarget) { |
| 317 | llvm::errs() << "Error accessing target '" << TripleName << "': " |
| 318 | << ErrorStr << "\n"; |
| 319 | return 1; |
| 320 | } |
| 321 | TripleName = TheTriple.getTriple(); |
| 322 | |
| 323 | std::unique_ptr<MCSubtargetInfo> STI( |
| 324 | TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 325 | assert(STI && "Unable to create subtarget info!"); |
| 326 | |
| 327 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 328 | assert(MRI && "Unable to create target register info!"); |
| 329 | |
| 330 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 331 | assert(MAI && "Unable to create target asm info!"); |
| 332 | |
| 333 | MCContext Ctx(MAI.get(), MRI.get(), nullptr); |
| 334 | |
| 335 | std::unique_ptr<MCDisassembler> Disassembler( |
| 336 | TheTarget->createMCDisassembler(*STI, Ctx)); |
| 337 | assert(Disassembler && "Unable to create disassembler!"); |
| 338 | |
| 339 | std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
| 340 | |
| 341 | std::unique_ptr<MCInstPrinter> InstPrinter( |
| 342 | TheTarget->createMCInstPrinter(0, *MAI, *MII, *MRI, *STI)); |
| 343 | |
| 344 | // Load any dylibs requested on the command line. |
| 345 | loadDylibs(); |
| 346 | |
| 347 | // Instantiate a dynamic linker. |
| 348 | TrivialMemoryManager MemMgr; |
| 349 | RuntimeDyld Dyld(&MemMgr); |
| 350 | |
| 351 | // If we don't have any input files, read from stdin. |
| 352 | if (!InputFileList.size()) |
| 353 | InputFileList.push_back("-"); |
| 354 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 355 | // Load the input memory buffer. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame^] | 356 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
| 357 | MemoryBuffer::getFileOrSTDIN(InputFileList[i]); |
| 358 | if (std::error_code EC = InputBuffer.getError()) |
| 359 | return Error("unable to read input: '" + EC.message() + "'"); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 360 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame^] | 361 | std::unique_ptr<ObjectImage> LoadedObject; |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 362 | // Load the object file |
| 363 | LoadedObject.reset( |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame^] | 364 | Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release()))); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 365 | if (!LoadedObject) { |
| 366 | return Error(Dyld.getErrorString()); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Resolve all the relocations we can. |
| 371 | Dyld.resolveRelocations(); |
| 372 | |
| 373 | RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(), |
| 374 | llvm::dbgs()); |
| 375 | return checkAllExpressions(Checker); |
| 376 | } |
| 377 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 378 | int main(int argc, char **argv) { |
Alp Toker | 9f67932 | 2013-11-05 09:33:43 +0000 | [diff] [blame] | 379 | sys::PrintStackTraceOnErrorSignal(); |
| 380 | PrettyStackTraceProgram X(argc, argv); |
| 381 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 382 | ProgramName = argv[0]; |
| 383 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 384 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 385 | llvm::InitializeAllTargetInfos(); |
| 386 | llvm::InitializeAllTargetMCs(); |
| 387 | llvm::InitializeAllDisassemblers(); |
| 388 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 389 | cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); |
| 390 | |
| 391 | switch (Action) { |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 392 | case AC_Execute: |
Jim Grosbach | 4d5284b | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 393 | return executeInput(); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 394 | case AC_PrintLineInfo: |
| 395 | return printLineInfoForInput(); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 396 | case AC_Verify: |
| 397 | return linkAndVerify(); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 398 | } |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 399 | } |