Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 1 | //===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This is a testing tool for use with the MC-JIT LLVM components. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringMap.h" |
Zachary Turner | 6489d7b | 2015-04-23 17:37:47 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/DIContext.h" |
| 15 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 17 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 18 | #include "llvm/ExecutionEngine/RuntimeDyldChecker.h" |
| 19 | #include "llvm/MC/MCAsmInfo.h" |
| 20 | #include "llvm/MC/MCContext.h" |
Benjamin Kramer | f57c197 | 2016-01-26 16:44:37 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCInstPrinter.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInstrInfo.h" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCRegisterInfo.h" |
Pete Cooper | 3de83e4 | 2015-05-15 21:58:42 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSubtargetInfo.h" |
Mirko Brkusanin | 4b63ca1 | 2019-10-23 12:24:35 +0200 | [diff] [blame] | 26 | #include "llvm/MC/MCTargetOptions.h" |
Rafael Espindola | b109c03 | 2015-06-23 02:08:48 +0000 | [diff] [blame] | 27 | #include "llvm/Object/SymbolSize.h" |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 29 | #include "llvm/Support/DynamicLibrary.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/InitLLVM.h" |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MSVCErrorWorkarounds.h" |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Memory.h" |
| 33 | #include "llvm/Support/MemoryBuffer.h" |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Path.h" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 35 | #include "llvm/Support/TargetRegistry.h" |
| 36 | #include "llvm/Support/TargetSelect.h" |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Timer.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 39 | |
| 40 | #include <future> |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 41 | #include <list> |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 42 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | using namespace llvm::object; |
| 45 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 46 | static cl::list<std::string> |
| 47 | InputFileList(cl::Positional, cl::ZeroOrMore, |
Lang Hames | 9c75545 | 2018-03-31 16:01:01 +0000 | [diff] [blame] | 48 | cl::desc("<input files>")); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 49 | |
| 50 | enum ActionType { |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 51 | AC_Execute, |
Keno Fischer | 281b694 | 2015-05-30 19:44:53 +0000 | [diff] [blame] | 52 | AC_PrintObjectLineInfo, |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 53 | AC_PrintLineInfo, |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 54 | AC_PrintDebugLineInfo, |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 55 | AC_Verify |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | static cl::opt<ActionType> |
| 59 | Action(cl::desc("Action to perform:"), |
| 60 | cl::init(AC_Execute), |
| 61 | cl::values(clEnumValN(AC_Execute, "execute", |
| 62 | "Load, link, and execute the inputs."), |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 63 | clEnumValN(AC_PrintLineInfo, "printline", |
| 64 | "Load, link, and print line information for each function."), |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 65 | clEnumValN(AC_PrintDebugLineInfo, "printdebugline", |
| 66 | "Load, link, and print line information for each function using the debug object"), |
Keno Fischer | 281b694 | 2015-05-30 19:44:53 +0000 | [diff] [blame] | 67 | clEnumValN(AC_PrintObjectLineInfo, "printobjline", |
| 68 | "Like -printlineinfo but does not load the object first"), |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 69 | clEnumValN(AC_Verify, "verify", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 70 | "Load, link and verify the resulting memory image."))); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 71 | |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 72 | static cl::opt<std::string> |
| 73 | EntryPoint("entry", |
| 74 | cl::desc("Function to call as entry point."), |
| 75 | cl::init("_main")); |
| 76 | |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 77 | static cl::list<std::string> |
| 78 | Dylibs("dylib", |
| 79 | cl::desc("Add library."), |
| 80 | cl::ZeroOrMore); |
| 81 | |
Lang Hames | cf49aa3 | 2019-04-25 05:02:10 +0000 | [diff] [blame] | 82 | static cl::list<std::string> InputArgv("args", cl::Positional, |
| 83 | cl::desc("<program arguments>..."), |
| 84 | cl::ZeroOrMore, cl::PositionalEatsArgs); |
| 85 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 86 | static cl::opt<std::string> |
| 87 | TripleName("triple", cl::desc("Target triple for disassembler")); |
| 88 | |
Petar Jovanovic | 280e562 | 2015-06-23 22:52:19 +0000 | [diff] [blame] | 89 | static cl::opt<std::string> |
| 90 | MCPU("mcpu", |
| 91 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 92 | cl::value_desc("cpu-name"), |
| 93 | cl::init("")); |
| 94 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 95 | static cl::list<std::string> |
| 96 | CheckFiles("check", |
| 97 | cl::desc("File containing RuntimeDyld verifier checks."), |
| 98 | cl::ZeroOrMore); |
| 99 | |
Fangrui Song | b5f3984 | 2019-04-24 02:40:20 +0000 | [diff] [blame] | 100 | static cl::opt<uint64_t> |
| 101 | PreallocMemory("preallocate", |
| 102 | cl::desc("Allocate memory upfront rather than on-demand"), |
| 103 | cl::init(0)); |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 104 | |
Fangrui Song | b5f3984 | 2019-04-24 02:40:20 +0000 | [diff] [blame] | 105 | static cl::opt<uint64_t> TargetAddrStart( |
| 106 | "target-addr-start", |
| 107 | cl::desc("For -verify only: start of phony target address " |
| 108 | "range."), |
| 109 | cl::init(4096), // Start at "page 1" - no allocating at "null". |
| 110 | cl::Hidden); |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 111 | |
Fangrui Song | b5f3984 | 2019-04-24 02:40:20 +0000 | [diff] [blame] | 112 | static cl::opt<uint64_t> TargetAddrEnd( |
| 113 | "target-addr-end", |
| 114 | cl::desc("For -verify only: end of phony target address range."), |
| 115 | cl::init(~0ULL), cl::Hidden); |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 116 | |
Fangrui Song | b5f3984 | 2019-04-24 02:40:20 +0000 | [diff] [blame] | 117 | static cl::opt<uint64_t> TargetSectionSep( |
| 118 | "target-section-sep", |
| 119 | cl::desc("For -verify only: Separation between sections in " |
| 120 | "phony target address space."), |
| 121 | cl::init(0), cl::Hidden); |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 122 | |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 123 | static cl::list<std::string> |
| 124 | SpecificSectionMappings("map-section", |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 125 | cl::desc("For -verify only: Map a section to a " |
| 126 | "specific address."), |
| 127 | cl::ZeroOrMore, |
| 128 | cl::Hidden); |
| 129 | |
| 130 | static cl::list<std::string> |
| 131 | DummySymbolMappings("dummy-extern", |
| 132 | cl::desc("For -verify only: Inject a symbol into the extern " |
| 133 | "symbol table."), |
| 134 | cl::ZeroOrMore, |
| 135 | cl::Hidden); |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 136 | |
Sanjoy Das | d5658b0 | 2015-11-23 21:47:51 +0000 | [diff] [blame] | 137 | static cl::opt<bool> |
| 138 | PrintAllocationRequests("print-alloc-requests", |
| 139 | cl::desc("Print allocation requests made to the memory " |
| 140 | "manager by RuntimeDyld"), |
| 141 | cl::Hidden); |
| 142 | |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 143 | static cl::opt<bool> ShowTimes("show-times", |
| 144 | cl::desc("Show times for llvm-rtdyld phases"), |
| 145 | cl::init(false)); |
| 146 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 147 | ExitOnError ExitOnErr; |
| 148 | |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 149 | struct RTDyldTimers { |
Lang Hames | 41adc37 | 2019-09-04 20:26:26 +0000 | [diff] [blame] | 150 | TimerGroup RTDyldTG{"llvm-rtdyld timers", "timers for llvm-rtdyld phases"}; |
| 151 | Timer LoadObjectsTimer{"load", "time to load/add object files", RTDyldTG}; |
| 152 | Timer LinkTimer{"link", "time to link object files", RTDyldTG}; |
| 153 | Timer RunTimer{"run", "time to execute jitlink'd code", RTDyldTG}; |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | std::unique_ptr<RTDyldTimers> Timers; |
| 157 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 158 | /* *** */ |
| 159 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 160 | using SectionIDMap = StringMap<unsigned>; |
| 161 | using FileToSectionIDMap = StringMap<SectionIDMap>; |
| 162 | |
| 163 | void dumpFileToSectionIDMap(const FileToSectionIDMap &FileToSecIDMap) { |
| 164 | for (const auto &KV : FileToSecIDMap) { |
| 165 | llvm::dbgs() << "In " << KV.first() << "\n"; |
| 166 | for (auto &KV2 : KV.second) |
| 167 | llvm::dbgs() << " \"" << KV2.first() << "\" -> " << KV2.second << "\n"; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | Expected<unsigned> getSectionId(const FileToSectionIDMap &FileToSecIDMap, |
| 172 | StringRef FileName, StringRef SectionName) { |
| 173 | auto I = FileToSecIDMap.find(FileName); |
| 174 | if (I == FileToSecIDMap.end()) |
| 175 | return make_error<StringError>("No file named " + FileName, |
| 176 | inconvertibleErrorCode()); |
| 177 | auto &SectionIDs = I->second; |
| 178 | auto J = SectionIDs.find(SectionName); |
| 179 | if (J == SectionIDs.end()) |
| 180 | return make_error<StringError>("No section named \"" + SectionName + |
| 181 | "\" in file " + FileName, |
| 182 | inconvertibleErrorCode()); |
| 183 | return J->second; |
| 184 | } |
| 185 | |
Jim Grosbach | 2dcef050 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 186 | // A trivial memory manager that doesn't do anything fancy, just uses the |
| 187 | // support library allocation routines directly. |
| 188 | class TrivialMemoryManager : public RTDyldMemoryManager { |
| 189 | public: |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 190 | struct SectionInfo { |
| 191 | SectionInfo(StringRef Name, sys::MemoryBlock MB, unsigned SectionID) |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 192 | : Name(std::string(Name)), MB(std::move(MB)), SectionID(SectionID) {} |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 193 | std::string Name; |
| 194 | sys::MemoryBlock MB; |
| 195 | unsigned SectionID = ~0U; |
| 196 | }; |
| 197 | |
| 198 | SmallVector<SectionInfo, 16> FunctionMemory; |
| 199 | SmallVector<SectionInfo, 16> DataMemory; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 200 | |
| 201 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 202 | unsigned SectionID, |
| 203 | StringRef SectionName) override; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 204 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 205 | unsigned SectionID, StringRef SectionName, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 206 | bool IsReadOnly) override; |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 207 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 208 | /// If non null, records subsequent Name -> SectionID mappings. |
| 209 | void setSectionIDsMap(SectionIDMap *SecIDMap) { |
| 210 | this->SecIDMap = SecIDMap; |
| 211 | } |
| 212 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 213 | void *getPointerToNamedFunction(const std::string &Name, |
| 214 | bool AbortOnFailure = true) override { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 215 | return nullptr; |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 218 | bool finalizeMemory(std::string *ErrMsg) override { return false; } |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 219 | |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 220 | void addDummySymbol(const std::string &Name, uint64_t Addr) { |
| 221 | DummyExterns[Name] = Addr; |
| 222 | } |
| 223 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 224 | JITSymbol findSymbol(const std::string &Name) override { |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 225 | auto I = DummyExterns.find(Name); |
| 226 | |
| 227 | if (I != DummyExterns.end()) |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 228 | return JITSymbol(I->second, JITSymbolFlags::Exported); |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 229 | |
Lang Hames | cf49aa3 | 2019-04-25 05:02:10 +0000 | [diff] [blame] | 230 | if (auto Sym = RTDyldMemoryManager::findSymbol(Name)) |
| 231 | return Sym; |
| 232 | else if (auto Err = Sym.takeError()) |
| 233 | ExitOnErr(std::move(Err)); |
| 234 | else |
| 235 | ExitOnErr(make_error<StringError>("Could not find definition for \"" + |
| 236 | Name + "\"", |
| 237 | inconvertibleErrorCode())); |
| 238 | llvm_unreachable("Should have returned or exited by now"); |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Tim Northover | 5af339a | 2015-06-03 18:26:52 +0000 | [diff] [blame] | 241 | void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, |
| 242 | size_t Size) override {} |
Lang Hames | c936ac7 | 2017-05-09 21:32:18 +0000 | [diff] [blame] | 243 | void deregisterEHFrames() override {} |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 244 | |
| 245 | void preallocateSlab(uint64_t Size) { |
Lang Hames | afcb70d | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 246 | std::error_code EC; |
| 247 | sys::MemoryBlock MB = |
| 248 | sys::Memory::allocateMappedMemory(Size, nullptr, |
| 249 | sys::Memory::MF_READ | |
| 250 | sys::Memory::MF_WRITE, |
| 251 | EC); |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 252 | if (!MB.base()) |
Lang Hames | afcb70d | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 253 | report_fatal_error("Can't allocate enough memory: " + EC.message()); |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 254 | |
| 255 | PreallocSlab = MB; |
| 256 | UsePreallocation = true; |
| 257 | SlabSize = Size; |
| 258 | } |
| 259 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 260 | uint8_t *allocateFromSlab(uintptr_t Size, unsigned Alignment, bool isCode, |
| 261 | StringRef SectionName, unsigned SectionID) { |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 262 | Size = alignTo(Size, Alignment); |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 263 | if (CurrentSlabOffset + Size > SlabSize) |
| 264 | report_fatal_error("Can't allocate enough memory. Tune --preallocate"); |
| 265 | |
| 266 | uintptr_t OldSlabOffset = CurrentSlabOffset; |
| 267 | sys::MemoryBlock MB((void *)OldSlabOffset, Size); |
| 268 | if (isCode) |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 269 | FunctionMemory.push_back(SectionInfo(SectionName, MB, SectionID)); |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 270 | else |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 271 | DataMemory.push_back(SectionInfo(SectionName, MB, SectionID)); |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 272 | CurrentSlabOffset += Size; |
| 273 | return (uint8_t*)OldSlabOffset; |
| 274 | } |
| 275 | |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 276 | private: |
| 277 | std::map<std::string, uint64_t> DummyExterns; |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 278 | sys::MemoryBlock PreallocSlab; |
| 279 | bool UsePreallocation = false; |
| 280 | uintptr_t SlabSize = 0; |
| 281 | uintptr_t CurrentSlabOffset = 0; |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 282 | SectionIDMap *SecIDMap = nullptr; |
Jim Grosbach | 2dcef050 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 285 | uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size, |
| 286 | unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 287 | unsigned SectionID, |
| 288 | StringRef SectionName) { |
Sanjoy Das | d5658b0 | 2015-11-23 21:47:51 +0000 | [diff] [blame] | 289 | if (PrintAllocationRequests) |
| 290 | outs() << "allocateCodeSection(Size = " << Size << ", Alignment = " |
| 291 | << Alignment << ", SectionName = " << SectionName << ")\n"; |
| 292 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 293 | if (SecIDMap) |
| 294 | (*SecIDMap)[SectionName] = SectionID; |
| 295 | |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 296 | if (UsePreallocation) |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 297 | return allocateFromSlab(Size, Alignment, true /* isCode */, |
| 298 | SectionName, SectionID); |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 299 | |
Lang Hames | afcb70d | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 300 | std::error_code EC; |
| 301 | sys::MemoryBlock MB = |
| 302 | sys::Memory::allocateMappedMemory(Size, nullptr, |
| 303 | sys::Memory::MF_READ | |
| 304 | sys::Memory::MF_WRITE, |
| 305 | EC); |
Davide Italiano | 89151a0 | 2015-10-15 00:05:32 +0000 | [diff] [blame] | 306 | if (!MB.base()) |
Lang Hames | afcb70d | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 307 | report_fatal_error("MemoryManager allocation failed: " + EC.message()); |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 308 | FunctionMemory.push_back(SectionInfo(SectionName, MB, SectionID)); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 309 | return (uint8_t*)MB.base(); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size, |
| 313 | unsigned Alignment, |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 314 | unsigned SectionID, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 315 | StringRef SectionName, |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 316 | bool IsReadOnly) { |
Sanjoy Das | d5658b0 | 2015-11-23 21:47:51 +0000 | [diff] [blame] | 317 | if (PrintAllocationRequests) |
| 318 | outs() << "allocateDataSection(Size = " << Size << ", Alignment = " |
| 319 | << Alignment << ", SectionName = " << SectionName << ")\n"; |
| 320 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 321 | if (SecIDMap) |
| 322 | (*SecIDMap)[SectionName] = SectionID; |
| 323 | |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 324 | if (UsePreallocation) |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 325 | return allocateFromSlab(Size, Alignment, false /* isCode */, SectionName, |
| 326 | SectionID); |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 327 | |
Lang Hames | afcb70d | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 328 | std::error_code EC; |
| 329 | sys::MemoryBlock MB = |
| 330 | sys::Memory::allocateMappedMemory(Size, nullptr, |
| 331 | sys::Memory::MF_READ | |
| 332 | sys::Memory::MF_WRITE, |
| 333 | EC); |
Davide Italiano | 89151a0 | 2015-10-15 00:05:32 +0000 | [diff] [blame] | 334 | if (!MB.base()) |
Lang Hames | afcb70d | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 335 | report_fatal_error("MemoryManager allocation failed: " + EC.message()); |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 336 | DataMemory.push_back(SectionInfo(SectionName, MB, SectionID)); |
Danil Malyshev | 8c17fbd | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 337 | return (uint8_t*)MB.base(); |
| 338 | } |
| 339 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 340 | static const char *ProgramName; |
| 341 | |
Lang Hames | ee5417d | 2016-04-05 20:11:24 +0000 | [diff] [blame] | 342 | static void ErrorAndExit(const Twine &Msg) { |
Davide Italiano | c2e910d | 2015-11-20 23:12:15 +0000 | [diff] [blame] | 343 | errs() << ProgramName << ": error: " << Msg << "\n"; |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 344 | exit(1); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 347 | static void loadDylibs() { |
| 348 | for (const std::string &Dylib : Dylibs) { |
Davide Italiano | 5cdf936 | 2015-11-22 01:58:33 +0000 | [diff] [blame] | 349 | if (!sys::fs::is_regular_file(Dylib)) |
Davide Italiano | 41d0fa7 | 2015-11-21 05:58:19 +0000 | [diff] [blame] | 350 | report_fatal_error("Dylib not found: '" + Dylib + "'."); |
Davide Italiano | 5cdf936 | 2015-11-22 01:58:33 +0000 | [diff] [blame] | 351 | std::string ErrMsg; |
| 352 | if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg)) |
| 353 | report_fatal_error("Error loading '" + Dylib + "': " + ErrMsg); |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 357 | /* *** */ |
| 358 | |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 359 | static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) { |
| 360 | assert(LoadObjects || !UseDebugObj); |
| 361 | |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 362 | // Load any dylibs requested on the command line. |
| 363 | loadDylibs(); |
| 364 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 365 | // If we don't have any input files, read from stdin. |
| 366 | if (!InputFileList.size()) |
| 367 | InputFileList.push_back("-"); |
Davide Italiano | 5d7e8fd | 2015-10-12 00:57:29 +0000 | [diff] [blame] | 368 | for (auto &File : InputFileList) { |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 369 | // Instantiate a dynamic linker. |
Benjamin Kramer | 9ce7708 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 370 | TrivialMemoryManager MemMgr; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 371 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 372 | |
| 373 | // Load the input memory buffer. |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 374 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 375 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
Davide Italiano | 5d7e8fd | 2015-10-12 00:57:29 +0000 | [diff] [blame] | 376 | MemoryBuffer::getFileOrSTDIN(File); |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 377 | if (std::error_code EC = InputBuffer.getError()) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 378 | ErrorAndExit("unable to read input: '" + EC.message() + "'"); |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 379 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 380 | Expected<std::unique_ptr<ObjectFile>> MaybeObj( |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 381 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
| 382 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 383 | if (!MaybeObj) { |
| 384 | std::string Buf; |
| 385 | raw_string_ostream OS(Buf); |
Jonas Devlieghere | 45eb84f | 2018-11-11 01:46:03 +0000 | [diff] [blame] | 386 | logAllUnhandledErrors(MaybeObj.takeError(), OS); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 387 | OS.flush(); |
| 388 | ErrorAndExit("unable to create object file: '" + Buf + "'"); |
| 389 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 390 | |
| 391 | ObjectFile &Obj = **MaybeObj; |
| 392 | |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 393 | OwningBinary<ObjectFile> DebugObj; |
| 394 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = nullptr; |
| 395 | ObjectFile *SymbolObj = &Obj; |
| 396 | if (LoadObjects) { |
| 397 | // Load the object file |
| 398 | LoadedObjInfo = |
| 399 | Dyld.loadObject(Obj); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 400 | |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 401 | if (Dyld.hasError()) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 402 | ErrorAndExit(Dyld.getErrorString()); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 403 | |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 404 | // Resolve all the relocations we can. |
| 405 | Dyld.resolveRelocations(); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 406 | |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 407 | if (UseDebugObj) { |
| 408 | DebugObj = LoadedObjInfo->getObjectForDebug(Obj); |
| 409 | SymbolObj = DebugObj.getBinary(); |
Lang Hames | 5c96933 | 2015-07-28 20:51:53 +0000 | [diff] [blame] | 410 | LoadedObjInfo.reset(); |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 413 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 414 | std::unique_ptr<DIContext> Context = |
| 415 | DWARFContext::create(*SymbolObj, LoadedObjInfo.get()); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 416 | |
Rafael Espindola | 6bf3221 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 417 | std::vector<std::pair<SymbolRef, uint64_t>> SymAddr = |
Rafael Espindola | b109c03 | 2015-06-23 02:08:48 +0000 | [diff] [blame] | 418 | object::computeSymbolSizes(*SymbolObj); |
Rafael Espindola | a82ce1d | 2015-05-31 23:15:35 +0000 | [diff] [blame] | 419 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 420 | // Use symbol info to iterate functions in the object. |
Rafael Espindola | 6bf3221 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 421 | for (const auto &P : SymAddr) { |
Rafael Espindola | b109c03 | 2015-06-23 02:08:48 +0000 | [diff] [blame] | 422 | object::SymbolRef Sym = P.first; |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 423 | Expected<SymbolRef::Type> TypeOrErr = Sym.getType(); |
| 424 | if (!TypeOrErr) { |
| 425 | // TODO: Actually report errors helpfully. |
| 426 | consumeError(TypeOrErr.takeError()); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 427 | continue; |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 428 | } |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 429 | SymbolRef::Type Type = *TypeOrErr; |
| 430 | if (Type == object::SymbolRef::ST_Function) { |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 431 | Expected<StringRef> Name = Sym.getName(); |
| 432 | if (!Name) { |
| 433 | // TODO: Actually report errors helpfully. |
| 434 | consumeError(Name.takeError()); |
Rafael Espindola | da17627 | 2015-05-31 22:13:51 +0000 | [diff] [blame] | 435 | continue; |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 436 | } |
Kevin Enderby | 931cb65 | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 437 | Expected<uint64_t> AddrOrErr = Sym.getAddress(); |
| 438 | if (!AddrOrErr) { |
| 439 | // TODO: Actually report errors helpfully. |
| 440 | consumeError(AddrOrErr.takeError()); |
Rafael Espindola | da17627 | 2015-05-31 22:13:51 +0000 | [diff] [blame] | 441 | continue; |
Kevin Enderby | 931cb65 | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 442 | } |
Rafael Espindola | ed067c4 | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 443 | uint64_t Addr = *AddrOrErr; |
Rafael Espindola | a82ce1d | 2015-05-31 23:15:35 +0000 | [diff] [blame] | 444 | |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 445 | object::SectionedAddress Address; |
| 446 | |
Rafael Espindola | b109c03 | 2015-06-23 02:08:48 +0000 | [diff] [blame] | 447 | uint64_t Size = P.second; |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 448 | // If we're not using the debug object, compute the address of the |
| 449 | // symbol in memory (rather than that in the unrelocated object file) |
| 450 | // and use that to query the DWARFContext. |
| 451 | if (!UseDebugObj && LoadObjects) { |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 452 | auto SecOrErr = Sym.getSection(); |
| 453 | if (!SecOrErr) { |
| 454 | // TODO: Actually report errors helpfully. |
| 455 | consumeError(SecOrErr.takeError()); |
| 456 | continue; |
| 457 | } |
| 458 | object::section_iterator Sec = *SecOrErr; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 459 | Address.SectionIndex = Sec->getIndex(); |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 460 | uint64_t SectionLoadAddress = |
Lang Hames | 2e88f4f | 2015-07-28 17:52:11 +0000 | [diff] [blame] | 461 | LoadedObjInfo->getSectionLoadAddress(*Sec); |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 462 | if (SectionLoadAddress != 0) |
| 463 | Addr += SectionLoadAddress - Sec->getAddress(); |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 464 | } else if (auto SecOrErr = Sym.getSection()) |
| 465 | Address.SectionIndex = SecOrErr.get()->getIndex(); |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 466 | |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 467 | outs() << "Function: " << *Name << ", Size = " << Size |
| 468 | << ", Addr = " << Addr << "\n"; |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 469 | |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 470 | Address.Address = Addr; |
| 471 | DILineInfoTable Lines = |
| 472 | Context->getLineInfoForAddressRange(Address, Size); |
Davide Italiano | 5d7e8fd | 2015-10-12 00:57:29 +0000 | [diff] [blame] | 473 | for (auto &D : Lines) { |
| 474 | outs() << " Line info @ " << D.first - Addr << ": " |
| 475 | << D.second.FileName << ", line:" << D.second.Line << "\n"; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 476 | } |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 484 | static void doPreallocation(TrivialMemoryManager &MemMgr) { |
| 485 | // Allocate a slab of memory upfront, if required. This is used if |
| 486 | // we want to test small code models. |
| 487 | if (static_cast<intptr_t>(PreallocMemory) < 0) |
| 488 | report_fatal_error("Pre-allocated bytes of memory must be a positive integer."); |
| 489 | |
| 490 | // FIXME: Limit the amount of memory that can be preallocated? |
| 491 | if (PreallocMemory != 0) |
| 492 | MemMgr.preallocateSlab(PreallocMemory); |
| 493 | } |
| 494 | |
Jim Grosbach | 4d5284b | 2011-03-18 17:24:21 +0000 | [diff] [blame] | 495 | static int executeInput() { |
Lang Hames | d311c0e | 2014-05-13 22:37:41 +0000 | [diff] [blame] | 496 | // Load any dylibs requested on the command line. |
| 497 | loadDylibs(); |
| 498 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 499 | // Instantiate a dynamic linker. |
Benjamin Kramer | 9ce7708 | 2013-08-03 22:16:31 +0000 | [diff] [blame] | 500 | TrivialMemoryManager MemMgr; |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 501 | doPreallocation(MemMgr); |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 502 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 503 | |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 504 | // If we don't have any input files, read from stdin. |
| 505 | if (!InputFileList.size()) |
| 506 | InputFileList.push_back("-"); |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 507 | { |
| 508 | TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr); |
| 509 | for (auto &File : InputFileList) { |
| 510 | // Load the input memory buffer. |
| 511 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
| 512 | MemoryBuffer::getFileOrSTDIN(File); |
| 513 | if (std::error_code EC = InputBuffer.getError()) |
| 514 | ErrorAndExit("unable to read input: '" + EC.message() + "'"); |
| 515 | Expected<std::unique_ptr<ObjectFile>> MaybeObj( |
| 516 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 517 | |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 518 | if (!MaybeObj) { |
| 519 | std::string Buf; |
| 520 | raw_string_ostream OS(Buf); |
| 521 | logAllUnhandledErrors(MaybeObj.takeError(), OS); |
| 522 | OS.flush(); |
| 523 | ErrorAndExit("unable to create object file: '" + Buf + "'"); |
| 524 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 525 | |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 526 | ObjectFile &Obj = **MaybeObj; |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 527 | |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 528 | // Load the object file |
| 529 | Dyld.loadObject(Obj); |
| 530 | if (Dyld.hasError()) { |
| 531 | ErrorAndExit(Dyld.getErrorString()); |
| 532 | } |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 533 | } |
Jim Grosbach | 40411cc | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 534 | } |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 535 | |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 536 | { |
| 537 | TimeRegion TR(Timers ? &Timers->LinkTimer : nullptr); |
| 538 | // Resove all the relocations we can. |
| 539 | // FIXME: Error out if there are unresolved relocations. |
| 540 | Dyld.resolveRelocations(); |
| 541 | } |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 542 | |
Jim Grosbach | d35159a | 2011-04-13 15:38:30 +0000 | [diff] [blame] | 543 | // Get the address of the entry point (_main by default). |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 544 | void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 545 | if (!MainAddress) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 546 | ErrorAndExit("no definition for '" + EntryPoint + "'"); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 547 | |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 548 | // Invalidate the instruction cache for each loaded function. |
Davide Italiano | 5d7e8fd | 2015-10-12 00:57:29 +0000 | [diff] [blame] | 549 | for (auto &FM : MemMgr.FunctionMemory) { |
Davide Italiano | af08e1b | 2015-11-17 16:37:52 +0000 | [diff] [blame] | 550 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 551 | auto &FM_MB = FM.MB; |
| 552 | |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 553 | // Make sure the memory is executable. |
Davide Italiano | af08e1b | 2015-11-17 16:37:52 +0000 | [diff] [blame] | 554 | // setExecutable will call InvalidateInstructionCache. |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 555 | if (auto EC = sys::Memory::protectMappedMemory(FM_MB, |
Lang Hames | afcb70d | 2017-11-16 23:04:44 +0000 | [diff] [blame] | 556 | sys::Memory::MF_READ | |
| 557 | sys::Memory::MF_EXEC)) |
| 558 | ErrorAndExit("unable to mark function executable: '" + EC.message() + |
| 559 | "'"); |
Jim Grosbach | 3ed03f1 | 2011-04-12 00:23:32 +0000 | [diff] [blame] | 560 | } |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 561 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 562 | // Dispatch to _main(). |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 563 | errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n"; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 564 | |
| 565 | int (*Main)(int, const char**) = |
| 566 | (int(*)(int,const char**)) uintptr_t(MainAddress); |
Lang Hames | cf49aa3 | 2019-04-25 05:02:10 +0000 | [diff] [blame] | 567 | std::vector<const char *> Argv; |
Jim Grosbach | 7cb41d7 | 2011-04-13 15:49:40 +0000 | [diff] [blame] | 568 | // Use the name of the first input object module as argv[0] for the target. |
Lang Hames | cf49aa3 | 2019-04-25 05:02:10 +0000 | [diff] [blame] | 569 | Argv.push_back(InputFileList[0].data()); |
| 570 | for (auto &Arg : InputArgv) |
| 571 | Argv.push_back(Arg.data()); |
| 572 | Argv.push_back(nullptr); |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 573 | int Result = 0; |
| 574 | { |
| 575 | TimeRegion TR(Timers ? &Timers->RunTimer : nullptr); |
| 576 | Result = Main(Argv.size() - 1, Argv.data()); |
| 577 | } |
| 578 | |
| 579 | return Result; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 582 | static int checkAllExpressions(RuntimeDyldChecker &Checker) { |
| 583 | for (const auto& CheckerFileName : CheckFiles) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 584 | ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf = |
| 585 | MemoryBuffer::getFileOrSTDIN(CheckerFileName); |
| 586 | if (std::error_code EC = CheckerFileBuf.getError()) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 587 | ErrorAndExit("unable to read input '" + CheckerFileName + "': " + |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 588 | EC.message()); |
| 589 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 590 | if (!Checker.checkAllRulesInBuffer("# rtdyld-check:", |
| 591 | CheckerFileBuf.get().get())) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 592 | ErrorAndExit("some checks in '" + CheckerFileName + "' failed"); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 593 | } |
| 594 | return 0; |
| 595 | } |
| 596 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 597 | void applySpecificSectionMappings(RuntimeDyld &Dyld, |
| 598 | const FileToSectionIDMap &FileToSecIDMap) { |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 599 | |
| 600 | for (StringRef Mapping : SpecificSectionMappings) { |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 601 | size_t EqualsIdx = Mapping.find_first_of("="); |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 602 | std::string SectionIDStr = std::string(Mapping.substr(0, EqualsIdx)); |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 603 | size_t ComaIdx = Mapping.find_first_of(","); |
| 604 | |
Davide Italiano | 07557fc | 2015-11-21 02:15:51 +0000 | [diff] [blame] | 605 | if (ComaIdx == StringRef::npos) |
| 606 | report_fatal_error("Invalid section specification '" + Mapping + |
| 607 | "'. Should be '<file name>,<section name>=<addr>'"); |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 608 | |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 609 | std::string FileName = SectionIDStr.substr(0, ComaIdx); |
| 610 | std::string SectionName = SectionIDStr.substr(ComaIdx + 1); |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 611 | unsigned SectionID = |
| 612 | ExitOnErr(getSectionId(FileToSecIDMap, FileName, SectionName)); |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 613 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 614 | auto* OldAddr = Dyld.getSectionContent(SectionID).data(); |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 615 | std::string NewAddrStr = std::string(Mapping.substr(EqualsIdx + 1)); |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 616 | uint64_t NewAddr; |
| 617 | |
Davide Italiano | 07557fc | 2015-11-21 02:15:51 +0000 | [diff] [blame] | 618 | if (StringRef(NewAddrStr).getAsInteger(0, NewAddr)) |
| 619 | report_fatal_error("Invalid section address in mapping '" + Mapping + |
| 620 | "'."); |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 621 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 622 | Dyld.mapSectionAddress(OldAddr, NewAddr); |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 623 | } |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 626 | // Scatter sections in all directions! |
| 627 | // Remaps section addresses for -verify mode. The following command line options |
| 628 | // can be used to customize the layout of the memory within the phony target's |
| 629 | // address space: |
Simon Pilgrim | dae11f7 | 2016-11-20 13:31:13 +0000 | [diff] [blame] | 630 | // -target-addr-start <s> -- Specify where the phony target address range starts. |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 631 | // -target-addr-end <e> -- Specify where the phony target address range ends. |
| 632 | // -target-section-sep <d> -- Specify how big a gap should be left between the |
| 633 | // end of one section and the start of the next. |
| 634 | // Defaults to zero. Set to something big |
| 635 | // (e.g. 1 << 32) to stress-test stubs, GOTs, etc. |
| 636 | // |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 637 | static void remapSectionsAndSymbols(const llvm::Triple &TargetTriple, |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 638 | RuntimeDyld &Dyld, |
| 639 | TrivialMemoryManager &MemMgr) { |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 640 | |
| 641 | // Set up a work list (section addr/size pairs). |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 642 | typedef std::list<const TrivialMemoryManager::SectionInfo*> WorklistT; |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 643 | WorklistT Worklist; |
| 644 | |
| 645 | for (const auto& CodeSection : MemMgr.FunctionMemory) |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 646 | Worklist.push_back(&CodeSection); |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 647 | for (const auto& DataSection : MemMgr.DataMemory) |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 648 | Worklist.push_back(&DataSection); |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 649 | |
| 650 | // Keep an "already allocated" mapping of section target addresses to sizes. |
| 651 | // Sections whose address mappings aren't specified on the command line will |
| 652 | // allocated around the explicitly mapped sections while maintaining the |
| 653 | // minimum separation. |
| 654 | std::map<uint64_t, uint64_t> AlreadyAllocated; |
| 655 | |
Lang Hames | ff41150 | 2017-05-07 17:19:53 +0000 | [diff] [blame] | 656 | // Move the previously applied mappings (whether explicitly specified on the |
| 657 | // command line, or implicitly set by RuntimeDyld) into the already-allocated |
| 658 | // map. |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 659 | for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end(); |
| 660 | I != E;) { |
| 661 | WorklistT::iterator Tmp = I; |
| 662 | ++I; |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 663 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 664 | auto LoadAddr = Dyld.getSectionLoadAddress((*Tmp)->SectionID); |
| 665 | |
| 666 | if (LoadAddr != static_cast<uint64_t>( |
| 667 | reinterpret_cast<uintptr_t>((*Tmp)->MB.base()))) { |
Lang Hames | 776f1d5 | 2018-10-23 01:36:33 +0000 | [diff] [blame] | 668 | // A section will have a LoadAddr of 0 if it wasn't loaded for whatever |
| 669 | // reason (e.g. zero byte COFF sections). Don't include those sections in |
| 670 | // the allocation map. |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 671 | if (LoadAddr != 0) |
Lang Hames | 93d2bdd | 2019-05-20 20:53:05 +0000 | [diff] [blame] | 672 | AlreadyAllocated[LoadAddr] = (*Tmp)->MB.allocatedSize(); |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 673 | Worklist.erase(Tmp); |
| 674 | } |
| 675 | } |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 676 | |
| 677 | // If the -target-addr-end option wasn't explicitly passed, then set it to a |
| 678 | // sensible default based on the target triple. |
| 679 | if (TargetAddrEnd.getNumOccurrences() == 0) { |
| 680 | if (TargetTriple.isArch16Bit()) |
| 681 | TargetAddrEnd = (1ULL << 16) - 1; |
| 682 | else if (TargetTriple.isArch32Bit()) |
| 683 | TargetAddrEnd = (1ULL << 32) - 1; |
| 684 | // TargetAddrEnd already has a sensible default for 64-bit systems, so |
| 685 | // there's nothing to do in the 64-bit case. |
| 686 | } |
| 687 | |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 688 | // Process any elements remaining in the worklist. |
| 689 | while (!Worklist.empty()) { |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 690 | auto *CurEntry = Worklist.front(); |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 691 | Worklist.pop_front(); |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 692 | |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 693 | uint64_t NextSectionAddr = TargetAddrStart; |
| 694 | |
| 695 | for (const auto &Alloc : AlreadyAllocated) |
Lang Hames | 93d2bdd | 2019-05-20 20:53:05 +0000 | [diff] [blame] | 696 | if (NextSectionAddr + CurEntry->MB.allocatedSize() + TargetSectionSep <= |
| 697 | Alloc.first) |
Lang Hames | 778ef5b | 2014-09-04 04:19:54 +0000 | [diff] [blame] | 698 | break; |
| 699 | else |
| 700 | NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep; |
| 701 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 702 | Dyld.mapSectionAddress(CurEntry->MB.base(), NextSectionAddr); |
Lang Hames | 93d2bdd | 2019-05-20 20:53:05 +0000 | [diff] [blame] | 703 | AlreadyAllocated[NextSectionAddr] = CurEntry->MB.allocatedSize(); |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 706 | // Add dummy symbols to the memory manager. |
| 707 | for (const auto &Mapping : DummySymbolMappings) { |
Benjamin Kramer | e6ba5ef | 2016-11-30 10:01:11 +0000 | [diff] [blame] | 708 | size_t EqualsIdx = Mapping.find_first_of('='); |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 709 | |
Davide Italiano | 07557fc | 2015-11-21 02:15:51 +0000 | [diff] [blame] | 710 | if (EqualsIdx == StringRef::npos) |
| 711 | report_fatal_error("Invalid dummy symbol specification '" + Mapping + |
| 712 | "'. Should be '<symbol name>=<addr>'"); |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 713 | |
| 714 | std::string Symbol = Mapping.substr(0, EqualsIdx); |
| 715 | std::string AddrStr = Mapping.substr(EqualsIdx + 1); |
| 716 | |
| 717 | uint64_t Addr; |
Davide Italiano | 07557fc | 2015-11-21 02:15:51 +0000 | [diff] [blame] | 718 | if (StringRef(AddrStr).getAsInteger(0, Addr)) |
| 719 | report_fatal_error("Invalid symbol mapping '" + Mapping + "'."); |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 720 | |
| 721 | MemMgr.addDummySymbol(Symbol, Addr); |
| 722 | } |
Lang Hames | 9cb7353 | 2014-07-29 23:43:13 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // Load and link the objects specified on the command line, but do not execute |
| 726 | // anything. Instead, attach a RuntimeDyldChecker instance and call it to |
| 727 | // verify the correctness of the linked memory. |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 728 | static int linkAndVerify() { |
| 729 | |
| 730 | // Check for missing triple. |
Davide Italiano | 78da759 | 2015-11-21 05:44:41 +0000 | [diff] [blame] | 731 | if (TripleName == "") |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 732 | ErrorAndExit("-triple required when running in -verify mode."); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 733 | |
| 734 | // Look up the target and build the disassembler. |
| 735 | Triple TheTriple(Triple::normalize(TripleName)); |
| 736 | std::string ErrorStr; |
| 737 | const Target *TheTarget = |
| 738 | TargetRegistry::lookupTarget("", TheTriple, ErrorStr); |
Davide Italiano | 78da759 | 2015-11-21 05:44:41 +0000 | [diff] [blame] | 739 | if (!TheTarget) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 740 | ErrorAndExit("Error accessing target '" + TripleName + "': " + ErrorStr); |
Davide Italiano | 78da759 | 2015-11-21 05:44:41 +0000 | [diff] [blame] | 741 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 742 | TripleName = TheTriple.getTriple(); |
| 743 | |
| 744 | std::unique_ptr<MCSubtargetInfo> STI( |
Petar Jovanovic | 280e562 | 2015-06-23 22:52:19 +0000 | [diff] [blame] | 745 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, "")); |
Davide Italiano | ebb27af | 2015-11-21 05:49:07 +0000 | [diff] [blame] | 746 | if (!STI) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 747 | ErrorAndExit("Unable to create subtarget info!"); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 748 | |
| 749 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
Davide Italiano | ebb27af | 2015-11-21 05:49:07 +0000 | [diff] [blame] | 750 | if (!MRI) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 751 | ErrorAndExit("Unable to create target register info!"); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 752 | |
Mirko Brkusanin | 4b63ca1 | 2019-10-23 12:24:35 +0200 | [diff] [blame] | 753 | MCTargetOptions MCOptions; |
| 754 | std::unique_ptr<MCAsmInfo> MAI( |
| 755 | TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); |
Davide Italiano | ebb27af | 2015-11-21 05:49:07 +0000 | [diff] [blame] | 756 | if (!MAI) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 757 | ErrorAndExit("Unable to create target asm info!"); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 758 | |
| 759 | MCContext Ctx(MAI.get(), MRI.get(), nullptr); |
| 760 | |
| 761 | std::unique_ptr<MCDisassembler> Disassembler( |
| 762 | TheTarget->createMCDisassembler(*STI, Ctx)); |
Davide Italiano | ebb27af | 2015-11-21 05:49:07 +0000 | [diff] [blame] | 763 | if (!Disassembler) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 764 | ErrorAndExit("Unable to create disassembler!"); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 765 | |
| 766 | std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
| 767 | |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 768 | std::unique_ptr<MCInstPrinter> InstPrinter( |
| 769 | TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI)); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 770 | |
| 771 | // Load any dylibs requested on the command line. |
| 772 | loadDylibs(); |
| 773 | |
| 774 | // Instantiate a dynamic linker. |
| 775 | TrivialMemoryManager MemMgr; |
Davide Italiano | b59ea90 | 2015-10-21 22:12:03 +0000 | [diff] [blame] | 776 | doPreallocation(MemMgr); |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 777 | |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 778 | struct StubID { |
| 779 | unsigned SectionID; |
| 780 | uint32_t Offset; |
| 781 | }; |
| 782 | using StubInfos = StringMap<StubID>; |
| 783 | using StubContainers = StringMap<StubInfos>; |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 784 | |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 785 | StubContainers StubMap; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 786 | RuntimeDyld Dyld(MemMgr, MemMgr); |
Lang Hames | 925e51b | 2014-09-03 05:42:52 +0000 | [diff] [blame] | 787 | Dyld.setProcessAllSections(true); |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 788 | |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 789 | Dyld.setNotifyStubEmitted([&StubMap](StringRef FilePath, |
| 790 | StringRef SectionName, |
| 791 | StringRef SymbolName, unsigned SectionID, |
| 792 | uint32_t StubOffset) { |
| 793 | std::string ContainerName = |
| 794 | (sys::path::filename(FilePath) + "/" + SectionName).str(); |
| 795 | StubMap[ContainerName][SymbolName] = {SectionID, StubOffset}; |
| 796 | }); |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 797 | |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 798 | auto GetSymbolInfo = |
| 799 | [&Dyld, &MemMgr]( |
| 800 | StringRef Symbol) -> Expected<RuntimeDyldChecker::MemoryRegionInfo> { |
| 801 | RuntimeDyldChecker::MemoryRegionInfo SymInfo; |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 802 | |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 803 | // First get the target address. |
| 804 | if (auto InternalSymbol = Dyld.getSymbol(Symbol)) |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 805 | SymInfo.setTargetAddress(InternalSymbol.getAddress()); |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 806 | else { |
| 807 | // Symbol not found in RuntimeDyld. Fall back to external lookup. |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 808 | #ifdef _MSC_VER |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 809 | using ExpectedLookupResult = |
| 810 | MSVCPExpected<JITSymbolResolver::LookupResult>; |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 811 | #else |
| 812 | using ExpectedLookupResult = Expected<JITSymbolResolver::LookupResult>; |
| 813 | #endif |
| 814 | |
| 815 | auto ResultP = std::make_shared<std::promise<ExpectedLookupResult>>(); |
| 816 | auto ResultF = ResultP->get_future(); |
| 817 | |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 818 | MemMgr.lookup(JITSymbolResolver::LookupSet({Symbol}), |
| 819 | [=](Expected<JITSymbolResolver::LookupResult> Result) { |
| 820 | ResultP->set_value(std::move(Result)); |
| 821 | }); |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 822 | |
| 823 | auto Result = ResultF.get(); |
| 824 | if (!Result) |
| 825 | return Result.takeError(); |
| 826 | |
| 827 | auto I = Result->find(Symbol); |
| 828 | assert(I != Result->end() && |
| 829 | "Expected symbol address if no error occurred"); |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 830 | SymInfo.setTargetAddress(I->second.getAddress()); |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 831 | } |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 832 | |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 833 | // Now find the symbol content if possible (otherwise leave content as a |
| 834 | // default-constructed StringRef). |
| 835 | if (auto *SymAddr = Dyld.getSymbolLocalAddress(Symbol)) { |
| 836 | unsigned SectionID = Dyld.getSymbolSectionID(Symbol); |
| 837 | if (SectionID != ~0U) { |
| 838 | char *CSymAddr = static_cast<char *>(SymAddr); |
| 839 | StringRef SecContent = Dyld.getSectionContent(SectionID); |
| 840 | uint64_t SymSize = SecContent.size() - (CSymAddr - SecContent.data()); |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 841 | SymInfo.setContent(StringRef(CSymAddr, SymSize)); |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 842 | } |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 843 | } |
| 844 | return SymInfo; |
| 845 | }; |
| 846 | |
| 847 | auto IsSymbolValid = [&Dyld, GetSymbolInfo](StringRef Symbol) { |
| 848 | if (Dyld.getSymbol(Symbol)) |
| 849 | return true; |
| 850 | auto SymInfo = GetSymbolInfo(Symbol); |
| 851 | if (!SymInfo) { |
| 852 | logAllUnhandledErrors(SymInfo.takeError(), errs(), "RTDyldChecker: "); |
| 853 | return false; |
| 854 | } |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 855 | return SymInfo->getTargetAddress() != 0; |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 856 | }; |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 857 | |
| 858 | FileToSectionIDMap FileToSecIDMap; |
| 859 | |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 860 | auto GetSectionInfo = [&Dyld, &FileToSecIDMap](StringRef FileName, |
| 861 | StringRef SectionName) |
| 862 | -> Expected<RuntimeDyldChecker::MemoryRegionInfo> { |
| 863 | auto SectionID = getSectionId(FileToSecIDMap, FileName, SectionName); |
| 864 | if (!SectionID) |
| 865 | return SectionID.takeError(); |
| 866 | RuntimeDyldChecker::MemoryRegionInfo SecInfo; |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 867 | SecInfo.setTargetAddress(Dyld.getSectionLoadAddress(*SectionID)); |
| 868 | SecInfo.setContent(Dyld.getSectionContent(*SectionID)); |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 869 | return SecInfo; |
| 870 | }; |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 871 | |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 872 | auto GetStubInfo = [&Dyld, &StubMap](StringRef StubContainer, |
| 873 | StringRef SymbolName) |
| 874 | -> Expected<RuntimeDyldChecker::MemoryRegionInfo> { |
| 875 | if (!StubMap.count(StubContainer)) |
| 876 | return make_error<StringError>("Stub container not found: " + |
| 877 | StubContainer, |
| 878 | inconvertibleErrorCode()); |
| 879 | if (!StubMap[StubContainer].count(SymbolName)) |
| 880 | return make_error<StringError>("Symbol name " + SymbolName + |
| 881 | " in stub container " + StubContainer, |
| 882 | inconvertibleErrorCode()); |
| 883 | auto &SI = StubMap[StubContainer][SymbolName]; |
| 884 | RuntimeDyldChecker::MemoryRegionInfo StubMemInfo; |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 885 | StubMemInfo.setTargetAddress(Dyld.getSectionLoadAddress(SI.SectionID) + |
| 886 | SI.Offset); |
| 887 | StubMemInfo.setContent( |
| 888 | Dyld.getSectionContent(SI.SectionID).substr(SI.Offset)); |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 889 | return StubMemInfo; |
| 890 | }; |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 891 | |
| 892 | // We will initialize this below once we have the first object file and can |
| 893 | // know the endianness. |
| 894 | std::unique_ptr<RuntimeDyldChecker> Checker; |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 895 | |
| 896 | // If we don't have any input files, read from stdin. |
| 897 | if (!InputFileList.size()) |
| 898 | InputFileList.push_back("-"); |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 899 | for (auto &InputFile : InputFileList) { |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 900 | // Load the input memory buffer. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 901 | ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer = |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 902 | MemoryBuffer::getFileOrSTDIN(InputFile); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 903 | |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 904 | if (std::error_code EC = InputBuffer.getError()) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 905 | ErrorAndExit("unable to read input: '" + EC.message() + "'"); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 906 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 907 | Expected<std::unique_ptr<ObjectFile>> MaybeObj( |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 908 | ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef())); |
| 909 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 910 | if (!MaybeObj) { |
| 911 | std::string Buf; |
| 912 | raw_string_ostream OS(Buf); |
Jonas Devlieghere | 45eb84f | 2018-11-11 01:46:03 +0000 | [diff] [blame] | 913 | logAllUnhandledErrors(MaybeObj.takeError(), OS); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 914 | OS.flush(); |
| 915 | ErrorAndExit("unable to create object file: '" + Buf + "'"); |
| 916 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 917 | |
| 918 | ObjectFile &Obj = **MaybeObj; |
| 919 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 920 | if (!Checker) |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 921 | Checker = std::make_unique<RuntimeDyldChecker>( |
Lang Hames | c7c1f21 | 2019-04-12 18:07:28 +0000 | [diff] [blame] | 922 | IsSymbolValid, GetSymbolInfo, GetSectionInfo, GetStubInfo, |
| 923 | GetStubInfo, Obj.isLittleEndian() ? support::little : support::big, |
| 924 | Disassembler.get(), InstPrinter.get(), dbgs()); |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 925 | |
| 926 | auto FileName = sys::path::filename(InputFile); |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 927 | MemMgr.setSectionIDsMap(&FileToSecIDMap[FileName]); |
| 928 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 929 | // Load the object file |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 930 | Dyld.loadObject(Obj); |
| 931 | if (Dyld.hasError()) { |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 932 | ErrorAndExit(Dyld.getErrorString()); |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 936 | // Re-map the section addresses into the phony target address space and add |
| 937 | // dummy symbols. |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 938 | applySpecificSectionMappings(Dyld, FileToSecIDMap); |
| 939 | remapSectionsAndSymbols(TheTriple, Dyld, MemMgr); |
Lang Hames | 375385f | 2014-07-30 03:12:41 +0000 | [diff] [blame] | 940 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 941 | // Resolve all the relocations we can. |
| 942 | Dyld.resolveRelocations(); |
| 943 | |
Lang Hames | 925e51b | 2014-09-03 05:42:52 +0000 | [diff] [blame] | 944 | // Register EH frames. |
| 945 | Dyld.registerEHFrames(); |
| 946 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 947 | int ErrorCode = checkAllExpressions(*Checker); |
Davide Italiano | 78da759 | 2015-11-21 05:44:41 +0000 | [diff] [blame] | 948 | if (Dyld.hasError()) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 949 | ErrorAndExit("RTDyld reported an error applying relocations:\n " + |
Davide Italiano | 78da759 | 2015-11-21 05:44:41 +0000 | [diff] [blame] | 950 | Dyld.getErrorString()); |
Lang Hames | ae17268 | 2014-08-05 20:51:46 +0000 | [diff] [blame] | 951 | |
| 952 | return ErrorCode; |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 955 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 956 | InitLLVM X(argc, argv); |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 957 | ProgramName = argv[0]; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 958 | |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 959 | llvm::InitializeAllTargetInfos(); |
| 960 | llvm::InitializeAllTargetMCs(); |
| 961 | llvm::InitializeAllDisassemblers(); |
| 962 | |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 963 | cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); |
| 964 | |
Lang Hames | 941f247 | 2019-04-08 21:50:48 +0000 | [diff] [blame] | 965 | ExitOnErr.setBanner(std::string(argv[0]) + ": "); |
| 966 | |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 967 | Timers = ShowTimes ? std::make_unique<RTDyldTimers>() : nullptr; |
| 968 | |
| 969 | int Result; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 970 | switch (Action) { |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 971 | case AC_Execute: |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 972 | Result = executeInput(); |
| 973 | break; |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 974 | case AC_PrintDebugLineInfo: |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 975 | Result = |
| 976 | printLineInfoForInput(/* LoadObjects */ true, /* UseDebugObj */ true); |
| 977 | break; |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 978 | case AC_PrintLineInfo: |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 979 | Result = |
| 980 | printLineInfoForInput(/* LoadObjects */ true, /* UseDebugObj */ false); |
| 981 | break; |
Keno Fischer | 281b694 | 2015-05-30 19:44:53 +0000 | [diff] [blame] | 982 | case AC_PrintObjectLineInfo: |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 983 | Result = |
| 984 | printLineInfoForInput(/* LoadObjects */ false, /* UseDebugObj */ false); |
| 985 | break; |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 986 | case AC_Verify: |
Lang Hames | 7966953 | 2019-09-04 20:26:25 +0000 | [diff] [blame] | 987 | Result = linkAndVerify(); |
| 988 | break; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 989 | } |
Sjoerd Meijer | 5f349d5 | 2019-09-16 10:30:37 +0000 | [diff] [blame] | 990 | return Result; |
Jim Grosbach | 0072cdb | 2011-03-18 17:11:39 +0000 | [diff] [blame] | 991 | } |