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 | |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringMap.h" |
Zachary Turner | 45e7e93 | 2015-04-23 17:37:47 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DIContext.h" |
| 16 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Lang Hames | da62155 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 17 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 18 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Lang Hames | c3747f2 | 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" |
Benjamin Kramer | b6242a8 | 2016-01-26 16:44:37 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInstPrinter.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInstrInfo.h" |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCRegisterInfo.h" |
Pete Cooper | 39aa893 | 2015-05-15 21:58:42 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSubtargetInfo.h" |
Rafael Espindola | 344dd65 | 2015-06-23 02:08:48 +0000 | [diff] [blame] | 27 | #include "llvm/Object/SymbolSize.h" |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
Lang Hames | f057625 | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 29 | #include "llvm/Support/DynamicLibrary.h" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/InitLLVM.h" |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Memory.h" |
| 32 | #include "llvm/Support/MemoryBuffer.h" |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 33 | #include "llvm/Support/TargetRegistry.h" |
| 34 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 36 | #include <list> |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 37 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | using namespace llvm::object; |
| 40 | |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 41 | static cl::list<std::string> |
| 42 | InputFileList(cl::Positional, cl::ZeroOrMore, |
Lang Hames | 6906b1f | 2018-03-31 16:01:01 +0000 | [diff] [blame] | 43 | cl::desc("<input files>")); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 44 | |
| 45 | enum ActionType { |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 46 | AC_Execute, |
Keno Fischer | dbdf667 | 2015-05-30 19:44:53 +0000 | [diff] [blame] | 47 | AC_PrintObjectLineInfo, |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 48 | AC_PrintLineInfo, |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 49 | AC_PrintDebugLineInfo, |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 50 | AC_Verify |
Jim Grosbach | 1cb19a4 | 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 | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 58 | clEnumValN(AC_PrintLineInfo, "printline", |
| 59 | "Load, link, and print line information for each function."), |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 60 | clEnumValN(AC_PrintDebugLineInfo, "printdebugline", |
| 61 | "Load, link, and print line information for each function using the debug object"), |
Keno Fischer | dbdf667 | 2015-05-30 19:44:53 +0000 | [diff] [blame] | 62 | clEnumValN(AC_PrintObjectLineInfo, "printobjline", |
| 63 | "Like -printlineinfo but does not load the object first"), |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 64 | clEnumValN(AC_Verify, "verify", |
Mehdi Amini | 3ffe113 | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 65 | "Load, link and verify the resulting memory image."))); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 66 | |
Jim Grosbach | 6b32e7e | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 67 | static cl::opt<std::string> |
| 68 | EntryPoint("entry", |
| 69 | cl::desc("Function to call as entry point."), |
| 70 | cl::init("_main")); |
| 71 | |
Lang Hames | f057625 | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 72 | static cl::list<std::string> |
| 73 | Dylibs("dylib", |
| 74 | cl::desc("Add library."), |
| 75 | cl::ZeroOrMore); |
| 76 | |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 77 | static cl::opt<std::string> |
| 78 | TripleName("triple", cl::desc("Target triple for disassembler")); |
| 79 | |
Petar Jovanovic | 8dad59b | 2015-06-23 22:52:19 +0000 | [diff] [blame] | 80 | static cl::opt<std::string> |
| 81 | MCPU("mcpu", |
| 82 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 83 | cl::value_desc("cpu-name"), |
| 84 | cl::init("")); |
| 85 | |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 86 | static cl::list<std::string> |
| 87 | CheckFiles("check", |
| 88 | cl::desc("File containing RuntimeDyld verifier checks."), |
| 89 | cl::ZeroOrMore); |
| 90 | |
Lang Hames | c407a17 | 2018-10-23 01:36:33 +0000 | [diff] [blame] | 91 | // Tracking BUG: 19665 |
| 92 | // http://llvm.org/bugs/show_bug.cgi?id=19665 |
| 93 | // |
| 94 | // Do not change these options to cl::opt<uint64_t> since this silently breaks |
| 95 | // argument parsing. |
| 96 | static cl::opt<unsigned long long> |
Davide Italiano | 8c651fb | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 97 | PreallocMemory("preallocate", |
| 98 | cl::desc("Allocate memory upfront rather than on-demand"), |
| 99 | cl::init(0)); |
| 100 | |
Lang Hames | c407a17 | 2018-10-23 01:36:33 +0000 | [diff] [blame] | 101 | static cl::opt<unsigned long long> |
Lang Hames | 41364b7 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 102 | TargetAddrStart("target-addr-start", |
| 103 | cl::desc("For -verify only: start of phony target address " |
| 104 | "range."), |
| 105 | cl::init(4096), // Start at "page 1" - no allocating at "null". |
| 106 | cl::Hidden); |
| 107 | |
Lang Hames | c407a17 | 2018-10-23 01:36:33 +0000 | [diff] [blame] | 108 | static cl::opt<unsigned long long> |
Lang Hames | 41364b7 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 109 | TargetAddrEnd("target-addr-end", |
| 110 | cl::desc("For -verify only: end of phony target address range."), |
| 111 | cl::init(~0ULL), |
| 112 | cl::Hidden); |
| 113 | |
Lang Hames | c407a17 | 2018-10-23 01:36:33 +0000 | [diff] [blame] | 114 | static cl::opt<unsigned long long> |
Lang Hames | 41364b7 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 115 | TargetSectionSep("target-section-sep", |
| 116 | cl::desc("For -verify only: Separation between sections in " |
| 117 | "phony target address space."), |
| 118 | cl::init(0), |
| 119 | cl::Hidden); |
| 120 | |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 121 | static cl::list<std::string> |
| 122 | SpecificSectionMappings("map-section", |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 123 | cl::desc("For -verify only: Map a section to a " |
| 124 | "specific address."), |
| 125 | cl::ZeroOrMore, |
| 126 | cl::Hidden); |
| 127 | |
| 128 | static cl::list<std::string> |
| 129 | DummySymbolMappings("dummy-extern", |
| 130 | cl::desc("For -verify only: Inject a symbol into the extern " |
| 131 | "symbol table."), |
| 132 | cl::ZeroOrMore, |
| 133 | cl::Hidden); |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 134 | |
Sanjoy Das | 96ddcce | 2015-11-23 21:47:51 +0000 | [diff] [blame] | 135 | static cl::opt<bool> |
| 136 | PrintAllocationRequests("print-alloc-requests", |
| 137 | cl::desc("Print allocation requests made to the memory " |
| 138 | "manager by RuntimeDyld"), |
| 139 | cl::Hidden); |
| 140 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 141 | /* *** */ |
| 142 | |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 143 | // A trivial memory manager that doesn't do anything fancy, just uses the |
| 144 | // support library allocation routines directly. |
| 145 | class TrivialMemoryManager : public RTDyldMemoryManager { |
| 146 | public: |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 147 | SmallVector<sys::MemoryBlock, 16> FunctionMemory; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 148 | SmallVector<sys::MemoryBlock, 16> DataMemory; |
| 149 | |
| 150 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 151 | unsigned SectionID, |
| 152 | StringRef SectionName) override; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 153 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
Filip Pizlo | 6eb43d2 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 154 | unsigned SectionID, StringRef SectionName, |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 155 | bool IsReadOnly) override; |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 156 | |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 157 | void *getPointerToNamedFunction(const std::string &Name, |
| 158 | bool AbortOnFailure = true) override { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 159 | return nullptr; |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 162 | bool finalizeMemory(std::string *ErrMsg) override { return false; } |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 163 | |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 164 | void addDummySymbol(const std::string &Name, uint64_t Addr) { |
| 165 | DummyExterns[Name] = Addr; |
| 166 | } |
| 167 | |
Lang Hames | 075c1e2 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 168 | JITSymbol findSymbol(const std::string &Name) override { |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 169 | auto I = DummyExterns.find(Name); |
| 170 | |
| 171 | if (I != DummyExterns.end()) |
Lang Hames | 075c1e2 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 172 | return JITSymbol(I->second, JITSymbolFlags::Exported); |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 173 | |
| 174 | return RTDyldMemoryManager::findSymbol(Name); |
| 175 | } |
| 176 | |
Tim Northover | 6da0e02 | 2015-06-03 18:26:52 +0000 | [diff] [blame] | 177 | void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, |
| 178 | size_t Size) override {} |
Lang Hames | ab3dba8 | 2017-05-09 21:32:18 +0000 | [diff] [blame] | 179 | void deregisterEHFrames() override {} |
Davide Italiano | 8c651fb | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 180 | |
| 181 | void preallocateSlab(uint64_t Size) { |
Lang Hames | 6e1e5aa | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 182 | std::error_code EC; |
| 183 | sys::MemoryBlock MB = |
| 184 | sys::Memory::allocateMappedMemory(Size, nullptr, |
| 185 | sys::Memory::MF_READ | |
| 186 | sys::Memory::MF_WRITE, |
| 187 | EC); |
Davide Italiano | 8c651fb | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 188 | if (!MB.base()) |
Lang Hames | 6e1e5aa | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 189 | report_fatal_error("Can't allocate enough memory: " + EC.message()); |
Davide Italiano | 8c651fb | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 190 | |
| 191 | PreallocSlab = MB; |
| 192 | UsePreallocation = true; |
| 193 | SlabSize = Size; |
| 194 | } |
| 195 | |
| 196 | uint8_t *allocateFromSlab(uintptr_t Size, unsigned Alignment, bool isCode) { |
Rui Ueyama | 3edb0ec | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 197 | Size = alignTo(Size, Alignment); |
Davide Italiano | 8c651fb | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 198 | if (CurrentSlabOffset + Size > SlabSize) |
| 199 | report_fatal_error("Can't allocate enough memory. Tune --preallocate"); |
| 200 | |
| 201 | uintptr_t OldSlabOffset = CurrentSlabOffset; |
| 202 | sys::MemoryBlock MB((void *)OldSlabOffset, Size); |
| 203 | if (isCode) |
| 204 | FunctionMemory.push_back(MB); |
| 205 | else |
| 206 | DataMemory.push_back(MB); |
| 207 | CurrentSlabOffset += Size; |
| 208 | return (uint8_t*)OldSlabOffset; |
| 209 | } |
| 210 | |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 211 | private: |
| 212 | std::map<std::string, uint64_t> DummyExterns; |
Davide Italiano | 8c651fb | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 213 | sys::MemoryBlock PreallocSlab; |
| 214 | bool UsePreallocation = false; |
| 215 | uintptr_t SlabSize = 0; |
| 216 | uintptr_t CurrentSlabOffset = 0; |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 219 | uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size, |
| 220 | unsigned Alignment, |
Filip Pizlo | 6eb43d2 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 221 | unsigned SectionID, |
| 222 | StringRef SectionName) { |
Sanjoy Das | 96ddcce | 2015-11-23 21:47:51 +0000 | [diff] [blame] | 223 | if (PrintAllocationRequests) |
| 224 | outs() << "allocateCodeSection(Size = " << Size << ", Alignment = " |
| 225 | << Alignment << ", SectionName = " << SectionName << ")\n"; |
| 226 | |
Davide Italiano | 8c651fb | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 227 | if (UsePreallocation) |
| 228 | return allocateFromSlab(Size, Alignment, true /* isCode */); |
| 229 | |
Lang Hames | 6e1e5aa | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 230 | std::error_code EC; |
| 231 | sys::MemoryBlock MB = |
| 232 | sys::Memory::allocateMappedMemory(Size, nullptr, |
| 233 | sys::Memory::MF_READ | |
| 234 | sys::Memory::MF_WRITE, |
| 235 | EC); |
Davide Italiano | d88454b | 2015-10-15 00:05:32 +0000 | [diff] [blame] | 236 | if (!MB.base()) |
Lang Hames | 6e1e5aa | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 237 | report_fatal_error("MemoryManager allocation failed: " + EC.message()); |
Danil Malyshev | 068c65b | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 238 | FunctionMemory.push_back(MB); |
| 239 | return (uint8_t*)MB.base(); |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size, |
| 243 | unsigned Alignment, |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 244 | unsigned SectionID, |
Filip Pizlo | 6eb43d2 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 245 | StringRef SectionName, |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 246 | bool IsReadOnly) { |
Sanjoy Das | 96ddcce | 2015-11-23 21:47:51 +0000 | [diff] [blame] | 247 | if (PrintAllocationRequests) |
| 248 | outs() << "allocateDataSection(Size = " << Size << ", Alignment = " |
| 249 | << Alignment << ", SectionName = " << SectionName << ")\n"; |
| 250 | |
Davide Italiano | 8c651fb | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 251 | if (UsePreallocation) |
| 252 | return allocateFromSlab(Size, Alignment, false /* isCode */); |
| 253 | |
Lang Hames | 6e1e5aa | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 254 | std::error_code EC; |
| 255 | sys::MemoryBlock MB = |
| 256 | sys::Memory::allocateMappedMemory(Size, nullptr, |
| 257 | sys::Memory::MF_READ | |
| 258 | sys::Memory::MF_WRITE, |
| 259 | EC); |
Davide Italiano | d88454b | 2015-10-15 00:05:32 +0000 | [diff] [blame] | 260 | if (!MB.base()) |
Lang Hames | 6e1e5aa | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 261 | report_fatal_error("MemoryManager allocation failed: " + EC.message()); |
Danil Malyshev | 068c65b | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 262 | DataMemory.push_back(MB); |
| 263 | return (uint8_t*)MB.base(); |
| 264 | } |
| 265 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 266 | static const char *ProgramName; |
| 267 | |
Lang Hames | cb6a565 | 2016-04-05 20:11:24 +0000 | [diff] [blame] | 268 | static void ErrorAndExit(const Twine &Msg) { |
Davide Italiano | c25aec1 | 2015-11-20 23:12:15 +0000 | [diff] [blame] | 269 | errs() << ProgramName << ": error: " << Msg << "\n"; |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 270 | exit(1); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Lang Hames | f057625 | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 273 | static void loadDylibs() { |
| 274 | for (const std::string &Dylib : Dylibs) { |
Davide Italiano | e5dedf9 | 2015-11-22 01:58:33 +0000 | [diff] [blame] | 275 | if (!sys::fs::is_regular_file(Dylib)) |
Davide Italiano | 82d9366 | 2015-11-21 05:58:19 +0000 | [diff] [blame] | 276 | report_fatal_error("Dylib not found: '" + Dylib + "'."); |
Davide Italiano | e5dedf9 | 2015-11-22 01:58:33 +0000 | [diff] [blame] | 277 | std::string ErrMsg; |
| 278 | if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg)) |
| 279 | report_fatal_error("Error loading '" + Dylib + "': " + ErrMsg); |
Lang Hames | f057625 | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 283 | /* *** */ |
| 284 | |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 285 | static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) { |
| 286 | assert(LoadObjects || !UseDebugObj); |
| 287 | |
Lang Hames | f057625 | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 288 | // Load any dylibs requested on the command line. |
| 289 | loadDylibs(); |
| 290 | |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 291 | // If we don't have any input files, read from stdin. |
| 292 | if (!InputFileList.size()) |
| 293 | InputFileList.push_back("-"); |
Davide Italiano | dc7bc46 | 2015-10-12 00:57:29 +0000 | [diff] [blame] | 294 | for (auto &File : InputFileList) { |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 295 | // Instantiate a dynamic linker. |
Benjamin Kramer | a757e93 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 296 | TrivialMemoryManager MemMgr; |
Lang Hames | da62155 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 297 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 298 | |
| 299 | // Load the input memory buffer. |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 300 | |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 301 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
Davide Italiano | dc7bc46 | 2015-10-12 00:57:29 +0000 | [diff] [blame] | 302 | MemoryBuffer::getFileOrSTDIN(File); |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 303 | if (std::error_code EC = InputBuffer.getError()) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 304 | ErrorAndExit("unable to read input: '" + EC.message() + "'"); |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 305 | |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 306 | Expected<std::unique_ptr<ObjectFile>> MaybeObj( |
Lang Hames | 216e532 | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 307 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
| 308 | |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 309 | if (!MaybeObj) { |
| 310 | std::string Buf; |
| 311 | raw_string_ostream OS(Buf); |
Jonas Devlieghere | 686dfe3 | 2018-11-11 01:46:03 +0000 | [diff] [blame] | 312 | logAllUnhandledErrors(MaybeObj.takeError(), OS); |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 313 | OS.flush(); |
| 314 | ErrorAndExit("unable to create object file: '" + Buf + "'"); |
| 315 | } |
Lang Hames | 216e532 | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 316 | |
| 317 | ObjectFile &Obj = **MaybeObj; |
| 318 | |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 319 | OwningBinary<ObjectFile> DebugObj; |
| 320 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = nullptr; |
| 321 | ObjectFile *SymbolObj = &Obj; |
| 322 | if (LoadObjects) { |
| 323 | // Load the object file |
| 324 | LoadedObjInfo = |
| 325 | Dyld.loadObject(Obj); |
Lang Hames | 216e532 | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 326 | |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 327 | if (Dyld.hasError()) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 328 | ErrorAndExit(Dyld.getErrorString()); |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 329 | |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 330 | // Resolve all the relocations we can. |
| 331 | Dyld.resolveRelocations(); |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 332 | |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 333 | if (UseDebugObj) { |
| 334 | DebugObj = LoadedObjInfo->getObjectForDebug(Obj); |
| 335 | SymbolObj = DebugObj.getBinary(); |
Lang Hames | 86eea13 | 2015-07-28 20:51:53 +0000 | [diff] [blame] | 336 | LoadedObjInfo.reset(); |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
Lang Hames | 216e532 | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 339 | |
Rafael Espindola | ee809ac | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 340 | std::unique_ptr<DIContext> Context = |
| 341 | DWARFContext::create(*SymbolObj, LoadedObjInfo.get()); |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 342 | |
Rafael Espindola | a3af347 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 343 | std::vector<std::pair<SymbolRef, uint64_t>> SymAddr = |
Rafael Espindola | 344dd65 | 2015-06-23 02:08:48 +0000 | [diff] [blame] | 344 | object::computeSymbolSizes(*SymbolObj); |
Rafael Espindola | f8f9476 | 2015-05-31 23:15:35 +0000 | [diff] [blame] | 345 | |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 346 | // Use symbol info to iterate functions in the object. |
Rafael Espindola | a3af347 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 347 | for (const auto &P : SymAddr) { |
Rafael Espindola | 344dd65 | 2015-06-23 02:08:48 +0000 | [diff] [blame] | 348 | object::SymbolRef Sym = P.first; |
Kevin Enderby | a486dca | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 349 | Expected<SymbolRef::Type> TypeOrErr = Sym.getType(); |
| 350 | if (!TypeOrErr) { |
| 351 | // TODO: Actually report errors helpfully. |
| 352 | consumeError(TypeOrErr.takeError()); |
Kevin Enderby | 46e35ed | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 353 | continue; |
Kevin Enderby | a486dca | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 354 | } |
Kevin Enderby | 46e35ed | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 355 | SymbolRef::Type Type = *TypeOrErr; |
| 356 | if (Type == object::SymbolRef::ST_Function) { |
Kevin Enderby | 813e0cf | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 357 | Expected<StringRef> Name = Sym.getName(); |
| 358 | if (!Name) { |
| 359 | // TODO: Actually report errors helpfully. |
| 360 | consumeError(Name.takeError()); |
Rafael Espindola | 67635ab | 2015-05-31 22:13:51 +0000 | [diff] [blame] | 361 | continue; |
Kevin Enderby | 813e0cf | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 362 | } |
Kevin Enderby | 317de7c | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 363 | Expected<uint64_t> AddrOrErr = Sym.getAddress(); |
| 364 | if (!AddrOrErr) { |
| 365 | // TODO: Actually report errors helpfully. |
| 366 | consumeError(AddrOrErr.takeError()); |
Rafael Espindola | 67635ab | 2015-05-31 22:13:51 +0000 | [diff] [blame] | 367 | continue; |
Kevin Enderby | 317de7c | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 368 | } |
Rafael Espindola | 5954faa | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 369 | uint64_t Addr = *AddrOrErr; |
Rafael Espindola | f8f9476 | 2015-05-31 23:15:35 +0000 | [diff] [blame] | 370 | |
Rafael Espindola | 344dd65 | 2015-06-23 02:08:48 +0000 | [diff] [blame] | 371 | uint64_t Size = P.second; |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 372 | // If we're not using the debug object, compute the address of the |
| 373 | // symbol in memory (rather than that in the unrelocated object file) |
| 374 | // and use that to query the DWARFContext. |
| 375 | if (!UseDebugObj && LoadObjects) { |
Kevin Enderby | a486dca | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 376 | auto SecOrErr = Sym.getSection(); |
| 377 | if (!SecOrErr) { |
| 378 | // TODO: Actually report errors helpfully. |
| 379 | consumeError(SecOrErr.takeError()); |
| 380 | continue; |
| 381 | } |
| 382 | object::section_iterator Sec = *SecOrErr; |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 383 | StringRef SecName; |
| 384 | Sec->getName(SecName); |
| 385 | uint64_t SectionLoadAddress = |
Lang Hames | ce8287d | 2015-07-28 17:52:11 +0000 | [diff] [blame] | 386 | LoadedObjInfo->getSectionLoadAddress(*Sec); |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 387 | if (SectionLoadAddress != 0) |
| 388 | Addr += SectionLoadAddress - Sec->getAddress(); |
| 389 | } |
| 390 | |
Rafael Espindola | 8a80641 | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 391 | outs() << "Function: " << *Name << ", Size = " << Size |
| 392 | << ", Addr = " << Addr << "\n"; |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 393 | |
Andrew Kaylor | e27a787 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 394 | DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size); |
Davide Italiano | dc7bc46 | 2015-10-12 00:57:29 +0000 | [diff] [blame] | 395 | for (auto &D : Lines) { |
| 396 | outs() << " Line info @ " << D.first - Addr << ": " |
| 397 | << D.second.FileName << ", line:" << D.second.Line << "\n"; |
Andrew Kaylor | e27a787 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 398 | } |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
Davide Italiano | 8c651fb | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 406 | static void doPreallocation(TrivialMemoryManager &MemMgr) { |
| 407 | // Allocate a slab of memory upfront, if required. This is used if |
| 408 | // we want to test small code models. |
| 409 | if (static_cast<intptr_t>(PreallocMemory) < 0) |
| 410 | report_fatal_error("Pre-allocated bytes of memory must be a positive integer."); |
| 411 | |
| 412 | // FIXME: Limit the amount of memory that can be preallocated? |
| 413 | if (PreallocMemory != 0) |
| 414 | MemMgr.preallocateSlab(PreallocMemory); |
| 415 | } |
| 416 | |
Jim Grosbach | 82c25b4 | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 417 | static int executeInput() { |
Lang Hames | f057625 | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 418 | // Load any dylibs requested on the command line. |
| 419 | loadDylibs(); |
| 420 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 421 | // Instantiate a dynamic linker. |
Benjamin Kramer | a757e93 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 422 | TrivialMemoryManager MemMgr; |
Davide Italiano | 8c651fb | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 423 | doPreallocation(MemMgr); |
Lang Hames | da62155 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 424 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 425 | |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 426 | // If we don't have any input files, read from stdin. |
| 427 | if (!InputFileList.size()) |
| 428 | InputFileList.push_back("-"); |
Davide Italiano | dc7bc46 | 2015-10-12 00:57:29 +0000 | [diff] [blame] | 429 | for (auto &File : InputFileList) { |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 430 | // Load the input memory buffer. |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 431 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
Davide Italiano | dc7bc46 | 2015-10-12 00:57:29 +0000 | [diff] [blame] | 432 | MemoryBuffer::getFileOrSTDIN(File); |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 433 | if (std::error_code EC = InputBuffer.getError()) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 434 | ErrorAndExit("unable to read input: '" + EC.message() + "'"); |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 435 | Expected<std::unique_ptr<ObjectFile>> MaybeObj( |
Lang Hames | 216e532 | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 436 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
| 437 | |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 438 | if (!MaybeObj) { |
| 439 | std::string Buf; |
| 440 | raw_string_ostream OS(Buf); |
Jonas Devlieghere | 686dfe3 | 2018-11-11 01:46:03 +0000 | [diff] [blame] | 441 | logAllUnhandledErrors(MaybeObj.takeError(), OS); |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 442 | OS.flush(); |
| 443 | ErrorAndExit("unable to create object file: '" + Buf + "'"); |
| 444 | } |
Lang Hames | 216e532 | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 445 | |
| 446 | ObjectFile &Obj = **MaybeObj; |
| 447 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 448 | // Load the object file |
Lang Hames | 216e532 | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 449 | Dyld.loadObject(Obj); |
| 450 | if (Dyld.hasError()) { |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 451 | ErrorAndExit(Dyld.getErrorString()); |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 452 | } |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 453 | } |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 454 | |
Davide Italiano | a17892d | 2015-11-17 16:37:52 +0000 | [diff] [blame] | 455 | // Resove all the relocations we can. |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 456 | // FIXME: Error out if there are unresolved relocations. |
Davide Italiano | a17892d | 2015-11-17 16:37:52 +0000 | [diff] [blame] | 457 | Dyld.resolveRelocations(); |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 458 | |
Jim Grosbach | 6b32e7e | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 459 | // Get the address of the entry point (_main by default). |
Lang Hames | 94c37b0 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 460 | void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint); |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 461 | if (!MainAddress) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 462 | ErrorAndExit("no definition for '" + EntryPoint + "'"); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 463 | |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 464 | // Invalidate the instruction cache for each loaded function. |
Davide Italiano | dc7bc46 | 2015-10-12 00:57:29 +0000 | [diff] [blame] | 465 | for (auto &FM : MemMgr.FunctionMemory) { |
Davide Italiano | a17892d | 2015-11-17 16:37:52 +0000 | [diff] [blame] | 466 | |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 467 | // Make sure the memory is executable. |
Davide Italiano | a17892d | 2015-11-17 16:37:52 +0000 | [diff] [blame] | 468 | // setExecutable will call InvalidateInstructionCache. |
Lang Hames | 6e1e5aa | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 469 | if (auto EC = sys::Memory::protectMappedMemory(FM, |
| 470 | sys::Memory::MF_READ | |
| 471 | sys::Memory::MF_EXEC)) |
| 472 | ErrorAndExit("unable to mark function executable: '" + EC.message() + |
| 473 | "'"); |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 474 | } |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 475 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 476 | // Dispatch to _main(). |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 477 | errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n"; |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 478 | |
| 479 | int (*Main)(int, const char**) = |
| 480 | (int(*)(int,const char**)) uintptr_t(MainAddress); |
| 481 | const char **Argv = new const char*[2]; |
Jim Grosbach | 4f9f41f | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 482 | // Use the name of the first input object module as argv[0] for the target. |
| 483 | Argv[0] = InputFileList[0].c_str(); |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 484 | Argv[1] = nullptr; |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 485 | return Main(1, Argv); |
| 486 | } |
| 487 | |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 488 | static int checkAllExpressions(RuntimeDyldChecker &Checker) { |
| 489 | for (const auto& CheckerFileName : CheckFiles) { |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 490 | ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf = |
| 491 | MemoryBuffer::getFileOrSTDIN(CheckerFileName); |
| 492 | if (std::error_code EC = CheckerFileBuf.getError()) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 493 | ErrorAndExit("unable to read input '" + CheckerFileName + "': " + |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 494 | EC.message()); |
| 495 | |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 496 | if (!Checker.checkAllRulesInBuffer("# rtdyld-check:", |
| 497 | CheckerFileBuf.get().get())) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 498 | ErrorAndExit("some checks in '" + CheckerFileName + "' failed"); |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 499 | } |
| 500 | return 0; |
| 501 | } |
| 502 | |
Lang Hames | e3f3714 | 2017-05-07 17:19:53 +0000 | [diff] [blame] | 503 | void applySpecificSectionMappings(RuntimeDyldChecker &Checker) { |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 504 | |
| 505 | for (StringRef Mapping : SpecificSectionMappings) { |
| 506 | |
| 507 | size_t EqualsIdx = Mapping.find_first_of("="); |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 508 | std::string SectionIDStr = Mapping.substr(0, EqualsIdx); |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 509 | size_t ComaIdx = Mapping.find_first_of(","); |
| 510 | |
Davide Italiano | ea83f22 | 2015-11-21 02:15:51 +0000 | [diff] [blame] | 511 | if (ComaIdx == StringRef::npos) |
| 512 | report_fatal_error("Invalid section specification '" + Mapping + |
| 513 | "'. Should be '<file name>,<section name>=<addr>'"); |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 514 | |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 515 | std::string FileName = SectionIDStr.substr(0, ComaIdx); |
| 516 | std::string SectionName = SectionIDStr.substr(ComaIdx + 1); |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 517 | |
| 518 | uint64_t OldAddrInt; |
| 519 | std::string ErrorMsg; |
| 520 | std::tie(OldAddrInt, ErrorMsg) = |
| 521 | Checker.getSectionAddr(FileName, SectionName, true); |
| 522 | |
Davide Italiano | ea83f22 | 2015-11-21 02:15:51 +0000 | [diff] [blame] | 523 | if (ErrorMsg != "") |
| 524 | report_fatal_error(ErrorMsg); |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 525 | |
| 526 | void* OldAddr = reinterpret_cast<void*>(static_cast<uintptr_t>(OldAddrInt)); |
| 527 | |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 528 | std::string NewAddrStr = Mapping.substr(EqualsIdx + 1); |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 529 | uint64_t NewAddr; |
| 530 | |
Davide Italiano | ea83f22 | 2015-11-21 02:15:51 +0000 | [diff] [blame] | 531 | if (StringRef(NewAddrStr).getAsInteger(0, NewAddr)) |
| 532 | report_fatal_error("Invalid section address in mapping '" + Mapping + |
| 533 | "'."); |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 534 | |
| 535 | Checker.getRTDyld().mapSectionAddress(OldAddr, NewAddr); |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 536 | } |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Lang Hames | 41364b7 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 539 | // Scatter sections in all directions! |
| 540 | // Remaps section addresses for -verify mode. The following command line options |
| 541 | // can be used to customize the layout of the memory within the phony target's |
| 542 | // address space: |
Simon Pilgrim | 84354d9 | 2016-11-20 13:31:13 +0000 | [diff] [blame] | 543 | // -target-addr-start <s> -- Specify where the phony target address range starts. |
Lang Hames | 41364b7 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 544 | // -target-addr-end <e> -- Specify where the phony target address range ends. |
| 545 | // -target-section-sep <d> -- Specify how big a gap should be left between the |
| 546 | // end of one section and the start of the next. |
| 547 | // Defaults to zero. Set to something big |
| 548 | // (e.g. 1 << 32) to stress-test stubs, GOTs, etc. |
| 549 | // |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 550 | static void remapSectionsAndSymbols(const llvm::Triple &TargetTriple, |
| 551 | TrivialMemoryManager &MemMgr, |
| 552 | RuntimeDyldChecker &Checker) { |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 553 | |
| 554 | // Set up a work list (section addr/size pairs). |
| 555 | typedef std::list<std::pair<void*, uint64_t>> WorklistT; |
| 556 | WorklistT Worklist; |
| 557 | |
| 558 | for (const auto& CodeSection : MemMgr.FunctionMemory) |
| 559 | Worklist.push_back(std::make_pair(CodeSection.base(), CodeSection.size())); |
| 560 | for (const auto& DataSection : MemMgr.DataMemory) |
| 561 | Worklist.push_back(std::make_pair(DataSection.base(), DataSection.size())); |
| 562 | |
| 563 | // Apply any section-specific mappings that were requested on the command |
| 564 | // line. |
Lang Hames | e3f3714 | 2017-05-07 17:19:53 +0000 | [diff] [blame] | 565 | applySpecificSectionMappings(Checker); |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 566 | |
| 567 | // Keep an "already allocated" mapping of section target addresses to sizes. |
| 568 | // Sections whose address mappings aren't specified on the command line will |
| 569 | // allocated around the explicitly mapped sections while maintaining the |
| 570 | // minimum separation. |
| 571 | std::map<uint64_t, uint64_t> AlreadyAllocated; |
| 572 | |
Lang Hames | e3f3714 | 2017-05-07 17:19:53 +0000 | [diff] [blame] | 573 | // Move the previously applied mappings (whether explicitly specified on the |
| 574 | // command line, or implicitly set by RuntimeDyld) into the already-allocated |
| 575 | // map. |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 576 | for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end(); |
| 577 | I != E;) { |
| 578 | WorklistT::iterator Tmp = I; |
| 579 | ++I; |
Lang Hames | e3f3714 | 2017-05-07 17:19:53 +0000 | [diff] [blame] | 580 | auto LoadAddr = Checker.getSectionLoadAddress(Tmp->first); |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 581 | |
Lang Hames | e3f3714 | 2017-05-07 17:19:53 +0000 | [diff] [blame] | 582 | if (LoadAddr && |
| 583 | *LoadAddr != static_cast<uint64_t>( |
| 584 | reinterpret_cast<uintptr_t>(Tmp->first))) { |
Lang Hames | c407a17 | 2018-10-23 01:36:33 +0000 | [diff] [blame] | 585 | // A section will have a LoadAddr of 0 if it wasn't loaded for whatever |
| 586 | // reason (e.g. zero byte COFF sections). Don't include those sections in |
| 587 | // the allocation map. |
| 588 | if (*LoadAddr != 0) |
| 589 | AlreadyAllocated[*LoadAddr] = Tmp->second; |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 590 | Worklist.erase(Tmp); |
| 591 | } |
| 592 | } |
Lang Hames | 41364b7 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 593 | |
| 594 | // If the -target-addr-end option wasn't explicitly passed, then set it to a |
| 595 | // sensible default based on the target triple. |
| 596 | if (TargetAddrEnd.getNumOccurrences() == 0) { |
| 597 | if (TargetTriple.isArch16Bit()) |
| 598 | TargetAddrEnd = (1ULL << 16) - 1; |
| 599 | else if (TargetTriple.isArch32Bit()) |
| 600 | TargetAddrEnd = (1ULL << 32) - 1; |
| 601 | // TargetAddrEnd already has a sensible default for 64-bit systems, so |
| 602 | // there's nothing to do in the 64-bit case. |
| 603 | } |
| 604 | |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 605 | // Process any elements remaining in the worklist. |
| 606 | while (!Worklist.empty()) { |
| 607 | std::pair<void*, uint64_t> CurEntry = Worklist.front(); |
| 608 | Worklist.pop_front(); |
Lang Hames | 41364b7 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 609 | |
Lang Hames | 5ade584 | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 610 | uint64_t NextSectionAddr = TargetAddrStart; |
| 611 | |
| 612 | for (const auto &Alloc : AlreadyAllocated) |
| 613 | if (NextSectionAddr + CurEntry.second + TargetSectionSep <= Alloc.first) |
| 614 | break; |
| 615 | else |
| 616 | NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep; |
| 617 | |
| 618 | AlreadyAllocated[NextSectionAddr] = CurEntry.second; |
| 619 | Checker.getRTDyld().mapSectionAddress(CurEntry.first, NextSectionAddr); |
Lang Hames | 41364b7 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 622 | // Add dummy symbols to the memory manager. |
| 623 | for (const auto &Mapping : DummySymbolMappings) { |
Benjamin Kramer | bf60ce0 | 2016-11-30 10:01:11 +0000 | [diff] [blame] | 624 | size_t EqualsIdx = Mapping.find_first_of('='); |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 625 | |
Davide Italiano | ea83f22 | 2015-11-21 02:15:51 +0000 | [diff] [blame] | 626 | if (EqualsIdx == StringRef::npos) |
| 627 | report_fatal_error("Invalid dummy symbol specification '" + Mapping + |
| 628 | "'. Should be '<symbol name>=<addr>'"); |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 629 | |
| 630 | std::string Symbol = Mapping.substr(0, EqualsIdx); |
| 631 | std::string AddrStr = Mapping.substr(EqualsIdx + 1); |
| 632 | |
| 633 | uint64_t Addr; |
Davide Italiano | ea83f22 | 2015-11-21 02:15:51 +0000 | [diff] [blame] | 634 | if (StringRef(AddrStr).getAsInteger(0, Addr)) |
| 635 | report_fatal_error("Invalid symbol mapping '" + Mapping + "'."); |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 636 | |
| 637 | MemMgr.addDummySymbol(Symbol, Addr); |
| 638 | } |
Lang Hames | 41364b7 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // Load and link the objects specified on the command line, but do not execute |
| 642 | // anything. Instead, attach a RuntimeDyldChecker instance and call it to |
| 643 | // verify the correctness of the linked memory. |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 644 | static int linkAndVerify() { |
| 645 | |
| 646 | // Check for missing triple. |
Davide Italiano | 87daa91 | 2015-11-21 05:44:41 +0000 | [diff] [blame] | 647 | if (TripleName == "") |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 648 | ErrorAndExit("-triple required when running in -verify mode."); |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 649 | |
| 650 | // Look up the target and build the disassembler. |
| 651 | Triple TheTriple(Triple::normalize(TripleName)); |
| 652 | std::string ErrorStr; |
| 653 | const Target *TheTarget = |
| 654 | TargetRegistry::lookupTarget("", TheTriple, ErrorStr); |
Davide Italiano | 87daa91 | 2015-11-21 05:44:41 +0000 | [diff] [blame] | 655 | if (!TheTarget) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 656 | ErrorAndExit("Error accessing target '" + TripleName + "': " + ErrorStr); |
Davide Italiano | 87daa91 | 2015-11-21 05:44:41 +0000 | [diff] [blame] | 657 | |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 658 | TripleName = TheTriple.getTriple(); |
| 659 | |
| 660 | std::unique_ptr<MCSubtargetInfo> STI( |
Petar Jovanovic | 8dad59b | 2015-06-23 22:52:19 +0000 | [diff] [blame] | 661 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, "")); |
Davide Italiano | 9fed20c | 2015-11-21 05:49:07 +0000 | [diff] [blame] | 662 | if (!STI) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 663 | ErrorAndExit("Unable to create subtarget info!"); |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 664 | |
| 665 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
Davide Italiano | 9fed20c | 2015-11-21 05:49:07 +0000 | [diff] [blame] | 666 | if (!MRI) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 667 | ErrorAndExit("Unable to create target register info!"); |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 668 | |
| 669 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
Davide Italiano | 9fed20c | 2015-11-21 05:49:07 +0000 | [diff] [blame] | 670 | if (!MAI) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 671 | ErrorAndExit("Unable to create target asm info!"); |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 672 | |
| 673 | MCContext Ctx(MAI.get(), MRI.get(), nullptr); |
| 674 | |
| 675 | std::unique_ptr<MCDisassembler> Disassembler( |
| 676 | TheTarget->createMCDisassembler(*STI, Ctx)); |
Davide Italiano | 9fed20c | 2015-11-21 05:49:07 +0000 | [diff] [blame] | 677 | if (!Disassembler) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 678 | ErrorAndExit("Unable to create disassembler!"); |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 679 | |
| 680 | std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
| 681 | |
Daniel Sanders | 47b167d | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 682 | std::unique_ptr<MCInstPrinter> InstPrinter( |
| 683 | TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI)); |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 684 | |
| 685 | // Load any dylibs requested on the command line. |
| 686 | loadDylibs(); |
| 687 | |
| 688 | // Instantiate a dynamic linker. |
| 689 | TrivialMemoryManager MemMgr; |
Davide Italiano | 8c651fb | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 690 | doPreallocation(MemMgr); |
Lang Hames | da62155 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 691 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Lang Hames | ef279f9 | 2014-09-03 05:42:52 +0000 | [diff] [blame] | 692 | Dyld.setProcessAllSections(true); |
Lang Hames | daf061c | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 693 | RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(), |
| 694 | llvm::dbgs()); |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 695 | |
| 696 | // If we don't have any input files, read from stdin. |
| 697 | if (!InputFileList.size()) |
| 698 | InputFileList.push_back("-"); |
Davide Italiano | f2e1e0e | 2015-10-10 00:45:24 +0000 | [diff] [blame] | 699 | for (auto &Filename : InputFileList) { |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 700 | // Load the input memory buffer. |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 701 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
Davide Italiano | f2e1e0e | 2015-10-10 00:45:24 +0000 | [diff] [blame] | 702 | MemoryBuffer::getFileOrSTDIN(Filename); |
Lang Hames | 216e532 | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 703 | |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 704 | if (std::error_code EC = InputBuffer.getError()) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 705 | ErrorAndExit("unable to read input: '" + EC.message() + "'"); |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 706 | |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 707 | Expected<std::unique_ptr<ObjectFile>> MaybeObj( |
Lang Hames | 216e532 | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 708 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
| 709 | |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 710 | if (!MaybeObj) { |
| 711 | std::string Buf; |
| 712 | raw_string_ostream OS(Buf); |
Jonas Devlieghere | 686dfe3 | 2018-11-11 01:46:03 +0000 | [diff] [blame] | 713 | logAllUnhandledErrors(MaybeObj.takeError(), OS); |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 714 | OS.flush(); |
| 715 | ErrorAndExit("unable to create object file: '" + Buf + "'"); |
| 716 | } |
Lang Hames | 216e532 | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 717 | |
| 718 | ObjectFile &Obj = **MaybeObj; |
| 719 | |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 720 | // Load the object file |
Lang Hames | 216e532 | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 721 | Dyld.loadObject(Obj); |
| 722 | if (Dyld.hasError()) { |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 723 | ErrorAndExit(Dyld.getErrorString()); |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
Lang Hames | b21c764 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 727 | // Re-map the section addresses into the phony target address space and add |
| 728 | // dummy symbols. |
| 729 | remapSectionsAndSymbols(TheTriple, MemMgr, Checker); |
Lang Hames | e1681c9 | 2014-07-30 03:12:41 +0000 | [diff] [blame] | 730 | |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 731 | // Resolve all the relocations we can. |
| 732 | Dyld.resolveRelocations(); |
| 733 | |
Lang Hames | ef279f9 | 2014-09-03 05:42:52 +0000 | [diff] [blame] | 734 | // Register EH frames. |
| 735 | Dyld.registerEHFrames(); |
| 736 | |
Lang Hames | 416ea4b | 2014-08-05 20:51:46 +0000 | [diff] [blame] | 737 | int ErrorCode = checkAllExpressions(Checker); |
Davide Italiano | 87daa91 | 2015-11-21 05:44:41 +0000 | [diff] [blame] | 738 | if (Dyld.hasError()) |
Lang Hames | 3e22297 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 739 | ErrorAndExit("RTDyld reported an error applying relocations:\n " + |
Davide Italiano | 87daa91 | 2015-11-21 05:44:41 +0000 | [diff] [blame] | 740 | Dyld.getErrorString()); |
Lang Hames | 416ea4b | 2014-08-05 20:51:46 +0000 | [diff] [blame] | 741 | |
| 742 | return ErrorCode; |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 745 | int main(int argc, char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 746 | InitLLVM X(argc, argv); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 747 | ProgramName = argv[0]; |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 748 | |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 749 | llvm::InitializeAllTargetInfos(); |
| 750 | llvm::InitializeAllTargetMCs(); |
| 751 | llvm::InitializeAllDisassemblers(); |
| 752 | |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 753 | cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); |
| 754 | |
| 755 | switch (Action) { |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 756 | case AC_Execute: |
Jim Grosbach | 82c25b4 | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 757 | return executeInput(); |
Keno Fischer | b6976af | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 758 | case AC_PrintDebugLineInfo: |
Keno Fischer | dbdf667 | 2015-05-30 19:44:53 +0000 | [diff] [blame] | 759 | return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */ true); |
Andrew Kaylor | ee7c0d2 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 760 | case AC_PrintLineInfo: |
Keno Fischer | dbdf667 | 2015-05-30 19:44:53 +0000 | [diff] [blame] | 761 | return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */false); |
| 762 | case AC_PrintObjectLineInfo: |
| 763 | return printLineInfoForInput(/* LoadObjects */false,/* UseDebugObj */false); |
Lang Hames | c3747f2 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 764 | case AC_Verify: |
| 765 | return linkAndVerify(); |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 766 | } |
Jim Grosbach | 1cb19a4 | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 767 | } |