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" |
Zachary Turner | 6489d7b | 2015-04-23 17:37:47 +0000 | [diff] [blame^] | 15 | #include "llvm/DebugInfo/DIContext.h" |
| 16 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 17 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 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" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInstPrinter.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInstrInfo.h" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 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" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Signals.h" |
| 34 | #include "llvm/Support/TargetRegistry.h" |
| 35 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 37 | #include <list> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 38 | #include <system_error> |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 39 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | using namespace llvm::object; |
| 42 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 43 | static cl::list<std::string> |
| 44 | InputFileList(cl::Positional, cl::ZeroOrMore, |
| 45 | cl::desc("<input file>")); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 46 | |
| 47 | enum ActionType { |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 48 | AC_Execute, |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 49 | AC_PrintLineInfo, |
| 50 | AC_Verify |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static cl::opt<ActionType> |
| 54 | Action(cl::desc("Action to perform:"), |
| 55 | cl::init(AC_Execute), |
| 56 | cl::values(clEnumValN(AC_Execute, "execute", |
| 57 | "Load, link, and execute the inputs."), |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 58 | clEnumValN(AC_PrintLineInfo, "printline", |
| 59 | "Load, link, and print line information for each function."), |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 60 | clEnumValN(AC_Verify, "verify", |
| 61 | "Load, link and verify the resulting memory image."), |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 62 | clEnumValEnd)); |
| 63 | |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 64 | static cl::opt<std::string> |
| 65 | EntryPoint("entry", |
| 66 | cl::desc("Function to call as entry point."), |
| 67 | cl::init("_main")); |
| 68 | |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 69 | static cl::list<std::string> |
| 70 | Dylibs("dylib", |
| 71 | cl::desc("Add library."), |
| 72 | cl::ZeroOrMore); |
| 73 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 74 | static cl::opt<std::string> |
| 75 | TripleName("triple", cl::desc("Target triple for disassembler")); |
| 76 | |
| 77 | static cl::list<std::string> |
| 78 | CheckFiles("check", |
| 79 | cl::desc("File containing RuntimeDyld verifier checks."), |
| 80 | cl::ZeroOrMore); |
| 81 | |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 82 | static cl::opt<uint64_t> |
| 83 | TargetAddrStart("target-addr-start", |
| 84 | cl::desc("For -verify only: start of phony target address " |
| 85 | "range."), |
| 86 | cl::init(4096), // Start at "page 1" - no allocating at "null". |
| 87 | cl::Hidden); |
| 88 | |
| 89 | static cl::opt<uint64_t> |
| 90 | TargetAddrEnd("target-addr-end", |
| 91 | cl::desc("For -verify only: end of phony target address range."), |
| 92 | cl::init(~0ULL), |
| 93 | cl::Hidden); |
| 94 | |
| 95 | static cl::opt<uint64_t> |
| 96 | TargetSectionSep("target-section-sep", |
| 97 | cl::desc("For -verify only: Separation between sections in " |
| 98 | "phony target address space."), |
| 99 | cl::init(0), |
| 100 | cl::Hidden); |
| 101 | |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 102 | static cl::list<std::string> |
| 103 | SpecificSectionMappings("map-section", |
| 104 | cl::desc("Map a section to a specific address."), |
| 105 | cl::ZeroOrMore); |
| 106 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 107 | /* *** */ |
| 108 | |
Jim Grosbach | 2dcef050 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 109 | // A trivial memory manager that doesn't do anything fancy, just uses the |
| 110 | // support library allocation routines directly. |
| 111 | class TrivialMemoryManager : public RTDyldMemoryManager { |
| 112 | public: |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 113 | SmallVector<sys::MemoryBlock, 16> FunctionMemory; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 114 | SmallVector<sys::MemoryBlock, 16> DataMemory; |
| 115 | |
| 116 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 117 | unsigned SectionID, |
| 118 | StringRef SectionName) override; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 119 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 120 | unsigned SectionID, StringRef SectionName, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 121 | bool IsReadOnly) override; |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 122 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 123 | void *getPointerToNamedFunction(const std::string &Name, |
| 124 | bool AbortOnFailure = true) override { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 125 | return nullptr; |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 128 | bool finalizeMemory(std::string *ErrMsg) override { return false; } |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 129 | |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 130 | // Invalidate instruction cache for sections with execute permissions. |
| 131 | // Some platforms with separate data cache and instruction cache require |
| 132 | // explicit cache flush, otherwise JIT code manipulations (like resolved |
| 133 | // relocations) will get to the data cache but not to the instruction cache. |
| 134 | virtual void invalidateInstructionCache(); |
Jim Grosbach | 2dcef050 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 137 | uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size, |
| 138 | unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 139 | unsigned SectionID, |
| 140 | StringRef SectionName) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 141 | sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 142 | FunctionMemory.push_back(MB); |
| 143 | return (uint8_t*)MB.base(); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size, |
| 147 | unsigned Alignment, |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 148 | unsigned SectionID, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 149 | StringRef SectionName, |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 150 | bool IsReadOnly) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 151 | sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 152 | DataMemory.push_back(MB); |
| 153 | return (uint8_t*)MB.base(); |
| 154 | } |
| 155 | |
| 156 | void TrivialMemoryManager::invalidateInstructionCache() { |
| 157 | for (int i = 0, e = FunctionMemory.size(); i != e; ++i) |
| 158 | sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(), |
| 159 | FunctionMemory[i].size()); |
| 160 | |
| 161 | for (int i = 0, e = DataMemory.size(); i != e; ++i) |
| 162 | sys::Memory::InvalidateInstructionCache(DataMemory[i].base(), |
| 163 | DataMemory[i].size()); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 166 | static const char *ProgramName; |
| 167 | |
| 168 | static void Message(const char *Type, const Twine &Msg) { |
| 169 | errs() << ProgramName << ": " << Type << ": " << Msg << "\n"; |
| 170 | } |
| 171 | |
| 172 | static int Error(const Twine &Msg) { |
| 173 | Message("error", Msg); |
| 174 | return 1; |
| 175 | } |
| 176 | |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 177 | static void loadDylibs() { |
| 178 | for (const std::string &Dylib : Dylibs) { |
| 179 | if (sys::fs::is_regular_file(Dylib)) { |
| 180 | std::string ErrMsg; |
| 181 | if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg)) |
| 182 | llvm::errs() << "Error loading '" << Dylib << "': " |
| 183 | << ErrMsg << "\n"; |
| 184 | } else |
| 185 | llvm::errs() << "Dylib not found: '" << Dylib << "'.\n"; |
| 186 | } |
| 187 | } |
| 188 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 189 | /* *** */ |
| 190 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 191 | static int printLineInfoForInput() { |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 192 | // Load any dylibs requested on the command line. |
| 193 | loadDylibs(); |
| 194 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 195 | // If we don't have any input files, read from stdin. |
| 196 | if (!InputFileList.size()) |
| 197 | InputFileList.push_back("-"); |
| 198 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 199 | // Instantiate a dynamic linker. |
Benjamin Kramer | 9ce7708 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 200 | TrivialMemoryManager MemMgr; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 201 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 202 | |
| 203 | // Load the input memory buffer. |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 204 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 205 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
| 206 | MemoryBuffer::getFileOrSTDIN(InputFileList[i]); |
| 207 | if (std::error_code EC = InputBuffer.getError()) |
| 208 | return Error("unable to read input: '" + EC.message() + "'"); |
| 209 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 210 | ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj( |
| 211 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
| 212 | |
| 213 | if (std::error_code EC = MaybeObj.getError()) |
| 214 | return Error("unable to create object file: '" + EC.message() + "'"); |
| 215 | |
| 216 | ObjectFile &Obj = **MaybeObj; |
| 217 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 218 | // Load the object file |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 219 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = |
| 220 | Dyld.loadObject(Obj); |
| 221 | |
| 222 | if (Dyld.hasError()) |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 223 | return Error(Dyld.getErrorString()); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 224 | |
| 225 | // Resolve all the relocations we can. |
| 226 | Dyld.resolveRelocations(); |
| 227 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 228 | OwningBinary<ObjectFile> DebugObj = LoadedObjInfo->getObjectForDebug(Obj); |
| 229 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 230 | std::unique_ptr<DIContext> Context( |
Zachary Turner | 6489d7b | 2015-04-23 17:37:47 +0000 | [diff] [blame^] | 231 | new DWARFContextInMemory(*DebugObj.getBinary())); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 232 | |
| 233 | // Use symbol info to iterate functions in the object. |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 234 | for (object::symbol_iterator I = DebugObj.getBinary()->symbol_begin(), |
| 235 | E = DebugObj.getBinary()->symbol_end(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 236 | I != E; ++I) { |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 237 | object::SymbolRef::Type SymType; |
| 238 | if (I->getType(SymType)) continue; |
| 239 | if (SymType == object::SymbolRef::ST_Function) { |
| 240 | StringRef Name; |
| 241 | uint64_t Addr; |
| 242 | uint64_t Size; |
| 243 | if (I->getName(Name)) continue; |
| 244 | if (I->getAddress(Addr)) continue; |
| 245 | if (I->getSize(Size)) continue; |
| 246 | |
| 247 | outs() << "Function: " << Name << ", Size = " << Size << "\n"; |
| 248 | |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 249 | DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size); |
| 250 | DILineInfoTable::iterator Begin = Lines.begin(); |
| 251 | DILineInfoTable::iterator End = Lines.end(); |
| 252 | for (DILineInfoTable::iterator It = Begin; It != End; ++It) { |
| 253 | outs() << " Line info @ " << It->first - Addr << ": " |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 254 | << It->second.FileName << ", line:" << It->second.Line << "\n"; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 255 | } |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Jim Grosbach | 4d5284b | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 263 | static int executeInput() { |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 264 | // Load any dylibs requested on the command line. |
| 265 | loadDylibs(); |
| 266 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 267 | // Instantiate a dynamic linker. |
Benjamin Kramer | 9ce7708 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 268 | TrivialMemoryManager MemMgr; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 269 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 270 | |
Lang Hames | c3246ad | 2015-04-15 21:18:41 +0000 | [diff] [blame] | 271 | // FIXME: Preserve buffers until resolveRelocations time to work around a bug |
| 272 | // in RuntimeDyldELF. |
| 273 | // This fixme should be fixed ASAP. This is a very brittle workaround. |
| 274 | std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers; |
| 275 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 276 | // If we don't have any input files, read from stdin. |
| 277 | if (!InputFileList.size()) |
| 278 | InputFileList.push_back("-"); |
| 279 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 280 | // Load the input memory buffer. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 281 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
| 282 | MemoryBuffer::getFileOrSTDIN(InputFileList[i]); |
| 283 | if (std::error_code EC = InputBuffer.getError()) |
| 284 | return Error("unable to read input: '" + EC.message() + "'"); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 285 | ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj( |
| 286 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
| 287 | |
| 288 | if (std::error_code EC = MaybeObj.getError()) |
| 289 | return Error("unable to create object file: '" + EC.message() + "'"); |
| 290 | |
| 291 | ObjectFile &Obj = **MaybeObj; |
Lang Hames | c3246ad | 2015-04-15 21:18:41 +0000 | [diff] [blame] | 292 | InputBuffers.push_back(std::move(*InputBuffer)); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 293 | |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 294 | // Load the object file |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 295 | Dyld.loadObject(Obj); |
| 296 | if (Dyld.hasError()) { |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 297 | return Error(Dyld.getErrorString()); |
| 298 | } |
Jim Grosbach | 40411cc | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 299 | } |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 300 | |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 301 | // Resolve all the relocations we can. |
| 302 | Dyld.resolveRelocations(); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 303 | // Clear instruction cache before code will be executed. |
Benjamin Kramer | 5d62ad2 | 2013-08-03 22:18:45 +0000 | [diff] [blame] | 304 | MemMgr.invalidateInstructionCache(); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 305 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 306 | // FIXME: Error out if there are unresolved relocations. |
| 307 | |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 308 | // Get the address of the entry point (_main by default). |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 309 | void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 310 | if (!MainAddress) |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 311 | return Error("no definition for '" + EntryPoint + "'"); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 312 | |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 313 | // Invalidate the instruction cache for each loaded function. |
Benjamin Kramer | 5d62ad2 | 2013-08-03 22:18:45 +0000 | [diff] [blame] | 314 | for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) { |
| 315 | sys::MemoryBlock &Data = MemMgr.FunctionMemory[i]; |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 316 | // Make sure the memory is executable. |
| 317 | std::string ErrorStr; |
| 318 | sys::Memory::InvalidateInstructionCache(Data.base(), Data.size()); |
| 319 | if (!sys::Memory::setExecutable(Data, &ErrorStr)) |
| 320 | return Error("unable to mark function executable: '" + ErrorStr + "'"); |
| 321 | } |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 322 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 323 | // Dispatch to _main(). |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 324 | errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n"; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 325 | |
| 326 | int (*Main)(int, const char**) = |
| 327 | (int(*)(int,const char**)) uintptr_t(MainAddress); |
| 328 | const char **Argv = new const char*[2]; |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 329 | // Use the name of the first input object module as argv[0] for the target. |
| 330 | Argv[0] = InputFileList[0].c_str(); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 331 | Argv[1] = nullptr; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 332 | return Main(1, Argv); |
| 333 | } |
| 334 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 335 | static int checkAllExpressions(RuntimeDyldChecker &Checker) { |
| 336 | for (const auto& CheckerFileName : CheckFiles) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 337 | ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf = |
| 338 | MemoryBuffer::getFileOrSTDIN(CheckerFileName); |
| 339 | if (std::error_code EC = CheckerFileBuf.getError()) |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 340 | return Error("unable to read input '" + CheckerFileName + "': " + |
| 341 | EC.message()); |
| 342 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 343 | if (!Checker.checkAllRulesInBuffer("# rtdyld-check:", |
| 344 | CheckerFileBuf.get().get())) |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 345 | return Error("some checks in '" + CheckerFileName + "' failed"); |
| 346 | } |
| 347 | return 0; |
| 348 | } |
| 349 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 350 | static std::map<void *, uint64_t> |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 351 | applySpecificSectionMappings(RuntimeDyldChecker &Checker) { |
| 352 | |
| 353 | std::map<void*, uint64_t> SpecificMappings; |
| 354 | |
| 355 | for (StringRef Mapping : SpecificSectionMappings) { |
| 356 | |
| 357 | size_t EqualsIdx = Mapping.find_first_of("="); |
| 358 | StringRef SectionIDStr = Mapping.substr(0, EqualsIdx); |
| 359 | size_t ComaIdx = Mapping.find_first_of(","); |
| 360 | |
| 361 | if (ComaIdx == StringRef::npos) { |
| 362 | errs() << "Invalid section specification '" << Mapping |
| 363 | << "'. Should be '<file name>,<section name>=<addr>'\n"; |
| 364 | exit(1); |
| 365 | } |
| 366 | |
| 367 | StringRef FileName = SectionIDStr.substr(0, ComaIdx); |
| 368 | StringRef SectionName = SectionIDStr.substr(ComaIdx + 1); |
| 369 | |
| 370 | uint64_t OldAddrInt; |
| 371 | std::string ErrorMsg; |
| 372 | std::tie(OldAddrInt, ErrorMsg) = |
| 373 | Checker.getSectionAddr(FileName, SectionName, true); |
| 374 | |
| 375 | if (ErrorMsg != "") { |
| 376 | errs() << ErrorMsg; |
| 377 | exit(1); |
| 378 | } |
| 379 | |
| 380 | void* OldAddr = reinterpret_cast<void*>(static_cast<uintptr_t>(OldAddrInt)); |
| 381 | |
| 382 | StringRef NewAddrStr = Mapping.substr(EqualsIdx + 1); |
| 383 | uint64_t NewAddr; |
| 384 | |
| 385 | if (NewAddrStr.getAsInteger(0, NewAddr)) { |
| 386 | errs() << "Invalid section address in mapping: " << Mapping << "\n"; |
| 387 | exit(1); |
| 388 | } |
| 389 | |
| 390 | Checker.getRTDyld().mapSectionAddress(OldAddr, NewAddr); |
| 391 | SpecificMappings[OldAddr] = NewAddr; |
| 392 | } |
| 393 | |
| 394 | return SpecificMappings; |
| 395 | } |
| 396 | |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 397 | // Scatter sections in all directions! |
| 398 | // Remaps section addresses for -verify mode. The following command line options |
| 399 | // can be used to customize the layout of the memory within the phony target's |
| 400 | // address space: |
| 401 | // -target-addr-start <s> -- Specify where the phony target addres range starts. |
| 402 | // -target-addr-end <e> -- Specify where the phony target address range ends. |
| 403 | // -target-section-sep <d> -- Specify how big a gap should be left between the |
| 404 | // end of one section and the start of the next. |
| 405 | // Defaults to zero. Set to something big |
| 406 | // (e.g. 1 << 32) to stress-test stubs, GOTs, etc. |
| 407 | // |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 408 | static void remapSections(const llvm::Triple &TargetTriple, |
| 409 | const TrivialMemoryManager &MemMgr, |
| 410 | RuntimeDyldChecker &Checker) { |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 411 | |
| 412 | // Set up a work list (section addr/size pairs). |
| 413 | typedef std::list<std::pair<void*, uint64_t>> WorklistT; |
| 414 | WorklistT Worklist; |
| 415 | |
| 416 | for (const auto& CodeSection : MemMgr.FunctionMemory) |
| 417 | Worklist.push_back(std::make_pair(CodeSection.base(), CodeSection.size())); |
| 418 | for (const auto& DataSection : MemMgr.DataMemory) |
| 419 | Worklist.push_back(std::make_pair(DataSection.base(), DataSection.size())); |
| 420 | |
| 421 | // Apply any section-specific mappings that were requested on the command |
| 422 | // line. |
| 423 | typedef std::map<void*, uint64_t> AppliedMappingsT; |
| 424 | AppliedMappingsT AppliedMappings = applySpecificSectionMappings(Checker); |
| 425 | |
| 426 | // Keep an "already allocated" mapping of section target addresses to sizes. |
| 427 | // Sections whose address mappings aren't specified on the command line will |
| 428 | // allocated around the explicitly mapped sections while maintaining the |
| 429 | // minimum separation. |
| 430 | std::map<uint64_t, uint64_t> AlreadyAllocated; |
| 431 | |
| 432 | // Move the previously applied mappings into the already-allocated map. |
| 433 | for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end(); |
| 434 | I != E;) { |
| 435 | WorklistT::iterator Tmp = I; |
| 436 | ++I; |
| 437 | AppliedMappingsT::iterator AI = AppliedMappings.find(Tmp->first); |
| 438 | |
| 439 | if (AI != AppliedMappings.end()) { |
| 440 | AlreadyAllocated[AI->second] = Tmp->second; |
| 441 | Worklist.erase(Tmp); |
| 442 | } |
| 443 | } |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 444 | |
| 445 | // If the -target-addr-end option wasn't explicitly passed, then set it to a |
| 446 | // sensible default based on the target triple. |
| 447 | if (TargetAddrEnd.getNumOccurrences() == 0) { |
| 448 | if (TargetTriple.isArch16Bit()) |
| 449 | TargetAddrEnd = (1ULL << 16) - 1; |
| 450 | else if (TargetTriple.isArch32Bit()) |
| 451 | TargetAddrEnd = (1ULL << 32) - 1; |
| 452 | // TargetAddrEnd already has a sensible default for 64-bit systems, so |
| 453 | // there's nothing to do in the 64-bit case. |
| 454 | } |
| 455 | |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 456 | // Process any elements remaining in the worklist. |
| 457 | while (!Worklist.empty()) { |
| 458 | std::pair<void*, uint64_t> CurEntry = Worklist.front(); |
| 459 | Worklist.pop_front(); |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 460 | |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 461 | uint64_t NextSectionAddr = TargetAddrStart; |
| 462 | |
| 463 | for (const auto &Alloc : AlreadyAllocated) |
| 464 | if (NextSectionAddr + CurEntry.second + TargetSectionSep <= Alloc.first) |
| 465 | break; |
| 466 | else |
| 467 | NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep; |
| 468 | |
| 469 | AlreadyAllocated[NextSectionAddr] = CurEntry.second; |
| 470 | Checker.getRTDyld().mapSectionAddress(CurEntry.first, NextSectionAddr); |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // Load and link the objects specified on the command line, but do not execute |
| 476 | // anything. Instead, attach a RuntimeDyldChecker instance and call it to |
| 477 | // verify the correctness of the linked memory. |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 478 | static int linkAndVerify() { |
| 479 | |
| 480 | // Check for missing triple. |
| 481 | if (TripleName == "") { |
| 482 | llvm::errs() << "Error: -triple required when running in -verify mode.\n"; |
| 483 | return 1; |
| 484 | } |
| 485 | |
| 486 | // Look up the target and build the disassembler. |
| 487 | Triple TheTriple(Triple::normalize(TripleName)); |
| 488 | std::string ErrorStr; |
| 489 | const Target *TheTarget = |
| 490 | TargetRegistry::lookupTarget("", TheTriple, ErrorStr); |
| 491 | if (!TheTarget) { |
| 492 | llvm::errs() << "Error accessing target '" << TripleName << "': " |
| 493 | << ErrorStr << "\n"; |
| 494 | return 1; |
| 495 | } |
| 496 | TripleName = TheTriple.getTriple(); |
| 497 | |
| 498 | std::unique_ptr<MCSubtargetInfo> STI( |
| 499 | TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 500 | assert(STI && "Unable to create subtarget info!"); |
| 501 | |
| 502 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 503 | assert(MRI && "Unable to create target register info!"); |
| 504 | |
| 505 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 506 | assert(MAI && "Unable to create target asm info!"); |
| 507 | |
| 508 | MCContext Ctx(MAI.get(), MRI.get(), nullptr); |
| 509 | |
| 510 | std::unique_ptr<MCDisassembler> Disassembler( |
| 511 | TheTarget->createMCDisassembler(*STI, Ctx)); |
| 512 | assert(Disassembler && "Unable to create disassembler!"); |
| 513 | |
| 514 | std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
| 515 | |
| 516 | std::unique_ptr<MCInstPrinter> InstPrinter( |
Eric Christopher | f801940 | 2015-03-31 00:10:04 +0000 | [diff] [blame] | 517 | TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI)); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 518 | |
| 519 | // Load any dylibs requested on the command line. |
| 520 | loadDylibs(); |
| 521 | |
| 522 | // Instantiate a dynamic linker. |
| 523 | TrivialMemoryManager MemMgr; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 524 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Lang Hames | 925e51b | 2014-09-03 05:42:52 +0000 | [diff] [blame] | 525 | Dyld.setProcessAllSections(true); |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 526 | RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(), |
| 527 | llvm::dbgs()); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 528 | |
Lang Hames | c3246ad | 2015-04-15 21:18:41 +0000 | [diff] [blame] | 529 | // FIXME: Preserve buffers until resolveRelocations time to work around a bug |
| 530 | // in RuntimeDyldELF. |
| 531 | // This fixme should be fixed ASAP. This is a very brittle workaround. |
| 532 | std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers; |
| 533 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 534 | // If we don't have any input files, read from stdin. |
| 535 | if (!InputFileList.size()) |
| 536 | InputFileList.push_back("-"); |
| 537 | for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { |
| 538 | // Load the input memory buffer. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 539 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
| 540 | MemoryBuffer::getFileOrSTDIN(InputFileList[i]); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 541 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 542 | if (std::error_code EC = InputBuffer.getError()) |
| 543 | return Error("unable to read input: '" + EC.message() + "'"); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 544 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 545 | ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj( |
| 546 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
| 547 | |
| 548 | if (std::error_code EC = MaybeObj.getError()) |
| 549 | return Error("unable to create object file: '" + EC.message() + "'"); |
| 550 | |
| 551 | ObjectFile &Obj = **MaybeObj; |
Lang Hames | c3246ad | 2015-04-15 21:18:41 +0000 | [diff] [blame] | 552 | InputBuffers.push_back(std::move(*InputBuffer)); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 553 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 554 | // Load the object file |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 555 | Dyld.loadObject(Obj); |
| 556 | if (Dyld.hasError()) { |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 557 | return Error(Dyld.getErrorString()); |
| 558 | } |
| 559 | } |
| 560 | |
Lang Hames | 375385f | 2014-07-30 03:12:41 +0000 | [diff] [blame] | 561 | // Re-map the section addresses into the phony target address space. |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 562 | remapSections(TheTriple, MemMgr, Checker); |
Lang Hames | 375385f | 2014-07-30 03:12:41 +0000 | [diff] [blame] | 563 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 564 | // Resolve all the relocations we can. |
| 565 | Dyld.resolveRelocations(); |
| 566 | |
Lang Hames | 925e51b | 2014-09-03 05:42:52 +0000 | [diff] [blame] | 567 | // Register EH frames. |
| 568 | Dyld.registerEHFrames(); |
| 569 | |
Lang Hames | ae17268 | 2014-08-05 20:51:46 +0000 | [diff] [blame] | 570 | int ErrorCode = checkAllExpressions(Checker); |
| 571 | if (Dyld.hasError()) { |
| 572 | errs() << "RTDyld reported an error applying relocations:\n " |
| 573 | << Dyld.getErrorString() << "\n"; |
| 574 | ErrorCode = 1; |
| 575 | } |
| 576 | |
| 577 | return ErrorCode; |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 580 | int main(int argc, char **argv) { |
Alp Toker | 9f67932 | 2013-11-05 09:33:43 +0000 | [diff] [blame] | 581 | sys::PrintStackTraceOnErrorSignal(); |
| 582 | PrettyStackTraceProgram X(argc, argv); |
| 583 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 584 | ProgramName = argv[0]; |
| 585 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 586 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 587 | llvm::InitializeAllTargetInfos(); |
| 588 | llvm::InitializeAllTargetMCs(); |
| 589 | llvm::InitializeAllDisassemblers(); |
| 590 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 591 | cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); |
| 592 | |
| 593 | switch (Action) { |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 594 | case AC_Execute: |
Jim Grosbach | 4d5284b | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 595 | return executeInput(); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 596 | case AC_PrintLineInfo: |
| 597 | return printLineInfoForInput(); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 598 | case AC_Verify: |
| 599 | return linkAndVerify(); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 600 | } |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 601 | } |