blob: 975638ed82d1dc0dacc330378adfd2ebf7d1e845 [file] [log] [blame]
Jim Grosbach1cb19a42011-03-18 17:11:39 +00001//===-- 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 Carruthf010c462012-12-04 10:44:52 +000014#include "llvm/ADT/StringMap.h"
Zachary Turner45e7e932015-04-23 17:37:47 +000015#include "llvm/DebugInfo/DIContext.h"
16#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Lang Hamesda621552015-03-30 03:37:06 +000017#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000018#include "llvm/ExecutionEngine/RuntimeDyld.h"
Lang Hamesc3747f22014-06-27 20:20:57 +000019#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
20#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCContext.h"
Benjamin Kramerb6242a82016-01-26 16:44:37 +000022#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Lang Hamesc3747f22014-06-27 20:20:57 +000023#include "llvm/MC/MCInstPrinter.h"
Chandler Carruth1b279142015-01-14 11:23:27 +000024#include "llvm/MC/MCInstrInfo.h"
Lang Hamesc3747f22014-06-27 20:20:57 +000025#include "llvm/MC/MCRegisterInfo.h"
Pete Cooper39aa8932015-05-15 21:58:42 +000026#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola344dd652015-06-23 02:08:48 +000027#include "llvm/Object/SymbolSize.h"
Jim Grosbach1cb19a42011-03-18 17:11:39 +000028#include "llvm/Support/CommandLine.h"
Lang Hamesf0576252014-05-13 22:37:41 +000029#include "llvm/Support/DynamicLibrary.h"
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000030#include "llvm/Support/InitLLVM.h"
Jim Grosbach1cb19a42011-03-18 17:11:39 +000031#include "llvm/Support/Memory.h"
32#include "llvm/Support/MemoryBuffer.h"
Lang Hamesc3747f22014-06-27 20:20:57 +000033#include "llvm/Support/TargetRegistry.h"
34#include "llvm/Support/TargetSelect.h"
Chandler Carruth1b279142015-01-14 11:23:27 +000035#include "llvm/Support/raw_ostream.h"
Lang Hames5ade5842014-09-04 04:19:54 +000036#include <list>
Lang Hamesc3747f22014-06-27 20:20:57 +000037
Jim Grosbach1cb19a42011-03-18 17:11:39 +000038using namespace llvm;
39using namespace llvm::object;
40
Jim Grosbach4f9f41f2011-04-13 15:49:40 +000041static cl::list<std::string>
42InputFileList(cl::Positional, cl::ZeroOrMore,
Lang Hames6906b1f2018-03-31 16:01:01 +000043 cl::desc("<input files>"));
Jim Grosbach1cb19a42011-03-18 17:11:39 +000044
45enum ActionType {
Andrew Kayloree7c0d22013-01-25 22:50:58 +000046 AC_Execute,
Keno Fischerdbdf6672015-05-30 19:44:53 +000047 AC_PrintObjectLineInfo,
Lang Hamesc3747f22014-06-27 20:20:57 +000048 AC_PrintLineInfo,
Keno Fischerb6976af2015-05-21 21:24:32 +000049 AC_PrintDebugLineInfo,
Lang Hamesc3747f22014-06-27 20:20:57 +000050 AC_Verify
Jim Grosbach1cb19a42011-03-18 17:11:39 +000051};
52
53static cl::opt<ActionType>
54Action(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 Kayloree7c0d22013-01-25 22:50:58 +000058 clEnumValN(AC_PrintLineInfo, "printline",
59 "Load, link, and print line information for each function."),
Keno Fischerb6976af2015-05-21 21:24:32 +000060 clEnumValN(AC_PrintDebugLineInfo, "printdebugline",
61 "Load, link, and print line information for each function using the debug object"),
Keno Fischerdbdf6672015-05-30 19:44:53 +000062 clEnumValN(AC_PrintObjectLineInfo, "printobjline",
63 "Like -printlineinfo but does not load the object first"),
Lang Hamesc3747f22014-06-27 20:20:57 +000064 clEnumValN(AC_Verify, "verify",
Mehdi Amini3ffe1132016-10-08 19:41:06 +000065 "Load, link and verify the resulting memory image.")));
Jim Grosbach1cb19a42011-03-18 17:11:39 +000066
Jim Grosbach6b32e7e2011-04-13 15:38:30 +000067static cl::opt<std::string>
68EntryPoint("entry",
69 cl::desc("Function to call as entry point."),
70 cl::init("_main"));
71
Lang Hamesf0576252014-05-13 22:37:41 +000072static cl::list<std::string>
73Dylibs("dylib",
74 cl::desc("Add library."),
75 cl::ZeroOrMore);
76
Lang Hamesc3747f22014-06-27 20:20:57 +000077static cl::opt<std::string>
78TripleName("triple", cl::desc("Target triple for disassembler"));
79
Petar Jovanovic8dad59b2015-06-23 22:52:19 +000080static cl::opt<std::string>
81MCPU("mcpu",
82 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
83 cl::value_desc("cpu-name"),
84 cl::init(""));
85
Lang Hamesc3747f22014-06-27 20:20:57 +000086static cl::list<std::string>
87CheckFiles("check",
88 cl::desc("File containing RuntimeDyld verifier checks."),
89 cl::ZeroOrMore);
90
Lang Hamesc407a172018-10-23 01:36:33 +000091// 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.
96static cl::opt<unsigned long long>
Davide Italiano8c651fb2015-10-21 22:12:03 +000097PreallocMemory("preallocate",
98 cl::desc("Allocate memory upfront rather than on-demand"),
99 cl::init(0));
100
Lang Hamesc407a172018-10-23 01:36:33 +0000101static cl::opt<unsigned long long>
Lang Hames41364b72014-07-29 23:43:13 +0000102TargetAddrStart("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 Hamesc407a172018-10-23 01:36:33 +0000108static cl::opt<unsigned long long>
Lang Hames41364b72014-07-29 23:43:13 +0000109TargetAddrEnd("target-addr-end",
110 cl::desc("For -verify only: end of phony target address range."),
111 cl::init(~0ULL),
112 cl::Hidden);
113
Lang Hamesc407a172018-10-23 01:36:33 +0000114static cl::opt<unsigned long long>
Lang Hames41364b72014-07-29 23:43:13 +0000115TargetSectionSep("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 Hames5ade5842014-09-04 04:19:54 +0000121static cl::list<std::string>
122SpecificSectionMappings("map-section",
Lang Hamesb21c7642015-07-04 01:35:26 +0000123 cl::desc("For -verify only: Map a section to a "
124 "specific address."),
125 cl::ZeroOrMore,
126 cl::Hidden);
127
128static cl::list<std::string>
129DummySymbolMappings("dummy-extern",
130 cl::desc("For -verify only: Inject a symbol into the extern "
131 "symbol table."),
132 cl::ZeroOrMore,
133 cl::Hidden);
Lang Hames5ade5842014-09-04 04:19:54 +0000134
Sanjoy Das96ddcce2015-11-23 21:47:51 +0000135static cl::opt<bool>
136PrintAllocationRequests("print-alloc-requests",
137 cl::desc("Print allocation requests made to the memory "
138 "manager by RuntimeDyld"),
139 cl::Hidden);
140
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000141/* *** */
142
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000143// A trivial memory manager that doesn't do anything fancy, just uses the
144// support library allocation routines directly.
145class TrivialMemoryManager : public RTDyldMemoryManager {
146public:
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000147 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
Jim Grosbach61425c02012-01-16 22:26:39 +0000148 SmallVector<sys::MemoryBlock, 16> DataMemory;
149
150 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
Craig Topperc83e68f2014-03-08 08:27:28 +0000151 unsigned SectionID,
152 StringRef SectionName) override;
Jim Grosbach61425c02012-01-16 22:26:39 +0000153 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
Filip Pizlo6eb43d22013-10-02 00:59:25 +0000154 unsigned SectionID, StringRef SectionName,
Craig Topperc83e68f2014-03-08 08:27:28 +0000155 bool IsReadOnly) override;
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000156
Craig Topperc83e68f2014-03-08 08:27:28 +0000157 void *getPointerToNamedFunction(const std::string &Name,
158 bool AbortOnFailure = true) override {
Craig Topper573faec2014-04-25 04:24:47 +0000159 return nullptr;
Danil Malyshev30b9e322012-03-28 21:46:36 +0000160 }
161
Craig Topperc83e68f2014-03-08 08:27:28 +0000162 bool finalizeMemory(std::string *ErrMsg) override { return false; }
Andrew Kaylor53608a32012-11-15 23:50:01 +0000163
Lang Hamesb21c7642015-07-04 01:35:26 +0000164 void addDummySymbol(const std::string &Name, uint64_t Addr) {
165 DummyExterns[Name] = Addr;
166 }
167
Lang Hames075c1e22016-08-01 20:49:11 +0000168 JITSymbol findSymbol(const std::string &Name) override {
Lang Hamesb21c7642015-07-04 01:35:26 +0000169 auto I = DummyExterns.find(Name);
170
171 if (I != DummyExterns.end())
Lang Hames075c1e22016-08-01 20:49:11 +0000172 return JITSymbol(I->second, JITSymbolFlags::Exported);
Lang Hamesb21c7642015-07-04 01:35:26 +0000173
174 return RTDyldMemoryManager::findSymbol(Name);
175 }
176
Tim Northover6da0e022015-06-03 18:26:52 +0000177 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
178 size_t Size) override {}
Lang Hamesab3dba82017-05-09 21:32:18 +0000179 void deregisterEHFrames() override {}
Davide Italiano8c651fb2015-10-21 22:12:03 +0000180
181 void preallocateSlab(uint64_t Size) {
Lang Hames6e1e5aa2017-11-16 23:04:44 +0000182 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 Italiano8c651fb2015-10-21 22:12:03 +0000188 if (!MB.base())
Lang Hames6e1e5aa2017-11-16 23:04:44 +0000189 report_fatal_error("Can't allocate enough memory: " + EC.message());
Davide Italiano8c651fb2015-10-21 22:12:03 +0000190
191 PreallocSlab = MB;
192 UsePreallocation = true;
193 SlabSize = Size;
194 }
195
196 uint8_t *allocateFromSlab(uintptr_t Size, unsigned Alignment, bool isCode) {
Rui Ueyama3edb0ec2016-01-14 21:06:47 +0000197 Size = alignTo(Size, Alignment);
Davide Italiano8c651fb2015-10-21 22:12:03 +0000198 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 Hamesb21c7642015-07-04 01:35:26 +0000211private:
212 std::map<std::string, uint64_t> DummyExterns;
Davide Italiano8c651fb2015-10-21 22:12:03 +0000213 sys::MemoryBlock PreallocSlab;
214 bool UsePreallocation = false;
215 uintptr_t SlabSize = 0;
216 uintptr_t CurrentSlabOffset = 0;
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000217};
218
Jim Grosbach61425c02012-01-16 22:26:39 +0000219uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
220 unsigned Alignment,
Filip Pizlo6eb43d22013-10-02 00:59:25 +0000221 unsigned SectionID,
222 StringRef SectionName) {
Sanjoy Das96ddcce2015-11-23 21:47:51 +0000223 if (PrintAllocationRequests)
224 outs() << "allocateCodeSection(Size = " << Size << ", Alignment = "
225 << Alignment << ", SectionName = " << SectionName << ")\n";
226
Davide Italiano8c651fb2015-10-21 22:12:03 +0000227 if (UsePreallocation)
228 return allocateFromSlab(Size, Alignment, true /* isCode */);
229
Lang Hames6e1e5aa2017-11-16 23:04:44 +0000230 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 Italianod88454b2015-10-15 00:05:32 +0000236 if (!MB.base())
Lang Hames6e1e5aa2017-11-16 23:04:44 +0000237 report_fatal_error("MemoryManager allocation failed: " + EC.message());
Danil Malyshev068c65b2012-05-16 18:50:11 +0000238 FunctionMemory.push_back(MB);
239 return (uint8_t*)MB.base();
Jim Grosbach61425c02012-01-16 22:26:39 +0000240}
241
242uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
243 unsigned Alignment,
Andrew Kaylor53608a32012-11-15 23:50:01 +0000244 unsigned SectionID,
Filip Pizlo6eb43d22013-10-02 00:59:25 +0000245 StringRef SectionName,
Andrew Kaylor53608a32012-11-15 23:50:01 +0000246 bool IsReadOnly) {
Sanjoy Das96ddcce2015-11-23 21:47:51 +0000247 if (PrintAllocationRequests)
248 outs() << "allocateDataSection(Size = " << Size << ", Alignment = "
249 << Alignment << ", SectionName = " << SectionName << ")\n";
250
Davide Italiano8c651fb2015-10-21 22:12:03 +0000251 if (UsePreallocation)
252 return allocateFromSlab(Size, Alignment, false /* isCode */);
253
Lang Hames6e1e5aa2017-11-16 23:04:44 +0000254 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 Italianod88454b2015-10-15 00:05:32 +0000260 if (!MB.base())
Lang Hames6e1e5aa2017-11-16 23:04:44 +0000261 report_fatal_error("MemoryManager allocation failed: " + EC.message());
Danil Malyshev068c65b2012-05-16 18:50:11 +0000262 DataMemory.push_back(MB);
263 return (uint8_t*)MB.base();
264}
265
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000266static const char *ProgramName;
267
Lang Hamescb6a5652016-04-05 20:11:24 +0000268static void ErrorAndExit(const Twine &Msg) {
Davide Italianoc25aec12015-11-20 23:12:15 +0000269 errs() << ProgramName << ": error: " << Msg << "\n";
Lang Hames3e222972016-03-25 17:25:34 +0000270 exit(1);
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000271}
272
Lang Hamesf0576252014-05-13 22:37:41 +0000273static void loadDylibs() {
274 for (const std::string &Dylib : Dylibs) {
Davide Italianoe5dedf92015-11-22 01:58:33 +0000275 if (!sys::fs::is_regular_file(Dylib))
Davide Italiano82d93662015-11-21 05:58:19 +0000276 report_fatal_error("Dylib not found: '" + Dylib + "'.");
Davide Italianoe5dedf92015-11-22 01:58:33 +0000277 std::string ErrMsg;
278 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
279 report_fatal_error("Error loading '" + Dylib + "': " + ErrMsg);
Lang Hamesf0576252014-05-13 22:37:41 +0000280 }
281}
282
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000283/* *** */
284
Keno Fischerb6976af2015-05-21 21:24:32 +0000285static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
286 assert(LoadObjects || !UseDebugObj);
287
Lang Hamesf0576252014-05-13 22:37:41 +0000288 // Load any dylibs requested on the command line.
289 loadDylibs();
290
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000291 // If we don't have any input files, read from stdin.
292 if (!InputFileList.size())
293 InputFileList.push_back("-");
Davide Italianodc7bc462015-10-12 00:57:29 +0000294 for (auto &File : InputFileList) {
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000295 // Instantiate a dynamic linker.
Benjamin Kramera757e932013-08-03 22:16:31 +0000296 TrivialMemoryManager MemMgr;
Lang Hamesda621552015-03-30 03:37:06 +0000297 RuntimeDyld Dyld(MemMgr, MemMgr);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000298
299 // Load the input memory buffer.
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000300
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000301 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
Davide Italianodc7bc462015-10-12 00:57:29 +0000302 MemoryBuffer::getFileOrSTDIN(File);
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000303 if (std::error_code EC = InputBuffer.getError())
Lang Hames3e222972016-03-25 17:25:34 +0000304 ErrorAndExit("unable to read input: '" + EC.message() + "'");
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000305
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +0000306 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
Lang Hames216e5322014-11-26 16:54:40 +0000307 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
308
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +0000309 if (!MaybeObj) {
310 std::string Buf;
311 raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +0000312 logAllUnhandledErrors(MaybeObj.takeError(), OS);
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +0000313 OS.flush();
314 ErrorAndExit("unable to create object file: '" + Buf + "'");
315 }
Lang Hames216e5322014-11-26 16:54:40 +0000316
317 ObjectFile &Obj = **MaybeObj;
318
Keno Fischerb6976af2015-05-21 21:24:32 +0000319 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 Hames216e5322014-11-26 16:54:40 +0000326
Keno Fischerb6976af2015-05-21 21:24:32 +0000327 if (Dyld.hasError())
Lang Hames3e222972016-03-25 17:25:34 +0000328 ErrorAndExit(Dyld.getErrorString());
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000329
Keno Fischerb6976af2015-05-21 21:24:32 +0000330 // Resolve all the relocations we can.
331 Dyld.resolveRelocations();
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000332
Keno Fischerb6976af2015-05-21 21:24:32 +0000333 if (UseDebugObj) {
334 DebugObj = LoadedObjInfo->getObjectForDebug(Obj);
335 SymbolObj = DebugObj.getBinary();
Lang Hames86eea132015-07-28 20:51:53 +0000336 LoadedObjInfo.reset();
Keno Fischerb6976af2015-05-21 21:24:32 +0000337 }
338 }
Lang Hames216e5322014-11-26 16:54:40 +0000339
Rafael Espindolaee809ac2017-07-19 22:27:28 +0000340 std::unique_ptr<DIContext> Context =
341 DWARFContext::create(*SymbolObj, LoadedObjInfo.get());
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000342
Rafael Espindolaa3af3472015-06-24 19:57:32 +0000343 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
Rafael Espindola344dd652015-06-23 02:08:48 +0000344 object::computeSymbolSizes(*SymbolObj);
Rafael Espindolaf8f94762015-05-31 23:15:35 +0000345
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000346 // Use symbol info to iterate functions in the object.
Rafael Espindolaa3af3472015-06-24 19:57:32 +0000347 for (const auto &P : SymAddr) {
Rafael Espindola344dd652015-06-23 02:08:48 +0000348 object::SymbolRef Sym = P.first;
Kevin Enderbya486dca2016-05-02 20:28:12 +0000349 Expected<SymbolRef::Type> TypeOrErr = Sym.getType();
350 if (!TypeOrErr) {
351 // TODO: Actually report errors helpfully.
352 consumeError(TypeOrErr.takeError());
Kevin Enderby46e35ed2016-03-23 20:27:00 +0000353 continue;
Kevin Enderbya486dca2016-05-02 20:28:12 +0000354 }
Kevin Enderby46e35ed2016-03-23 20:27:00 +0000355 SymbolRef::Type Type = *TypeOrErr;
356 if (Type == object::SymbolRef::ST_Function) {
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000357 Expected<StringRef> Name = Sym.getName();
358 if (!Name) {
359 // TODO: Actually report errors helpfully.
360 consumeError(Name.takeError());
Rafael Espindola67635ab2015-05-31 22:13:51 +0000361 continue;
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000362 }
Kevin Enderby317de7c2016-06-24 18:24:42 +0000363 Expected<uint64_t> AddrOrErr = Sym.getAddress();
364 if (!AddrOrErr) {
365 // TODO: Actually report errors helpfully.
366 consumeError(AddrOrErr.takeError());
Rafael Espindola67635ab2015-05-31 22:13:51 +0000367 continue;
Kevin Enderby317de7c2016-06-24 18:24:42 +0000368 }
Rafael Espindola5954faa2015-07-03 18:19:00 +0000369 uint64_t Addr = *AddrOrErr;
Rafael Espindolaf8f94762015-05-31 23:15:35 +0000370
Rafael Espindola344dd652015-06-23 02:08:48 +0000371 uint64_t Size = P.second;
Keno Fischerb6976af2015-05-21 21:24:32 +0000372 // 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 Enderbya486dca2016-05-02 20:28:12 +0000376 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 Fischerb6976af2015-05-21 21:24:32 +0000383 StringRef SecName;
384 Sec->getName(SecName);
385 uint64_t SectionLoadAddress =
Lang Hamesce8287d2015-07-28 17:52:11 +0000386 LoadedObjInfo->getSectionLoadAddress(*Sec);
Keno Fischerb6976af2015-05-21 21:24:32 +0000387 if (SectionLoadAddress != 0)
388 Addr += SectionLoadAddress - Sec->getAddress();
389 }
390
Rafael Espindola8a806412015-07-02 20:55:21 +0000391 outs() << "Function: " << *Name << ", Size = " << Size
392 << ", Addr = " << Addr << "\n";
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000393
Andrew Kaylore27a7872013-01-26 00:28:05 +0000394 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
Davide Italianodc7bc462015-10-12 00:57:29 +0000395 for (auto &D : Lines) {
396 outs() << " Line info @ " << D.first - Addr << ": "
397 << D.second.FileName << ", line:" << D.second.Line << "\n";
Andrew Kaylore27a7872013-01-26 00:28:05 +0000398 }
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000399 }
400 }
401 }
402
403 return 0;
404}
405
Davide Italiano8c651fb2015-10-21 22:12:03 +0000406static 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 Grosbach82c25b42011-03-18 17:24:21 +0000417static int executeInput() {
Lang Hamesf0576252014-05-13 22:37:41 +0000418 // Load any dylibs requested on the command line.
419 loadDylibs();
420
Jim Grosbach6e563312011-03-21 22:15:52 +0000421 // Instantiate a dynamic linker.
Benjamin Kramera757e932013-08-03 22:16:31 +0000422 TrivialMemoryManager MemMgr;
Davide Italiano8c651fb2015-10-21 22:12:03 +0000423 doPreallocation(MemMgr);
Lang Hamesda621552015-03-30 03:37:06 +0000424 RuntimeDyld Dyld(MemMgr, MemMgr);
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000425
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000426 // If we don't have any input files, read from stdin.
427 if (!InputFileList.size())
428 InputFileList.push_back("-");
Davide Italianodc7bc462015-10-12 00:57:29 +0000429 for (auto &File : InputFileList) {
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000430 // Load the input memory buffer.
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000431 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
Davide Italianodc7bc462015-10-12 00:57:29 +0000432 MemoryBuffer::getFileOrSTDIN(File);
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000433 if (std::error_code EC = InputBuffer.getError())
Lang Hames3e222972016-03-25 17:25:34 +0000434 ErrorAndExit("unable to read input: '" + EC.message() + "'");
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +0000435 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
Lang Hames216e5322014-11-26 16:54:40 +0000436 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
437
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +0000438 if (!MaybeObj) {
439 std::string Buf;
440 raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +0000441 logAllUnhandledErrors(MaybeObj.takeError(), OS);
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +0000442 OS.flush();
443 ErrorAndExit("unable to create object file: '" + Buf + "'");
444 }
Lang Hames216e5322014-11-26 16:54:40 +0000445
446 ObjectFile &Obj = **MaybeObj;
447
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000448 // Load the object file
Lang Hames216e5322014-11-26 16:54:40 +0000449 Dyld.loadObject(Obj);
450 if (Dyld.hasError()) {
Lang Hames3e222972016-03-25 17:25:34 +0000451 ErrorAndExit(Dyld.getErrorString());
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000452 }
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000453 }
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000454
Davide Italianoa17892d2015-11-17 16:37:52 +0000455 // Resove all the relocations we can.
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000456 // FIXME: Error out if there are unresolved relocations.
Davide Italianoa17892d2015-11-17 16:37:52 +0000457 Dyld.resolveRelocations();
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000458
Jim Grosbach6b32e7e2011-04-13 15:38:30 +0000459 // Get the address of the entry point (_main by default).
Lang Hames94c37b02015-03-11 00:43:26 +0000460 void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint);
Craig Topper573faec2014-04-25 04:24:47 +0000461 if (!MainAddress)
Lang Hames3e222972016-03-25 17:25:34 +0000462 ErrorAndExit("no definition for '" + EntryPoint + "'");
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000463
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000464 // Invalidate the instruction cache for each loaded function.
Davide Italianodc7bc462015-10-12 00:57:29 +0000465 for (auto &FM : MemMgr.FunctionMemory) {
Davide Italianoa17892d2015-11-17 16:37:52 +0000466
Jim Grosbach7cbf92d2011-04-12 00:23:32 +0000467 // Make sure the memory is executable.
Davide Italianoa17892d2015-11-17 16:37:52 +0000468 // setExecutable will call InvalidateInstructionCache.
Lang Hames6e1e5aa2017-11-16 23:04:44 +0000469 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 Grosbach7cbf92d2011-04-12 00:23:32 +0000474 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000475
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000476 // Dispatch to _main().
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000477 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000478
479 int (*Main)(int, const char**) =
480 (int(*)(int,const char**)) uintptr_t(MainAddress);
481 const char **Argv = new const char*[2];
Jim Grosbach4f9f41f2011-04-13 15:49:40 +0000482 // Use the name of the first input object module as argv[0] for the target.
483 Argv[0] = InputFileList[0].c_str();
Craig Topper573faec2014-04-25 04:24:47 +0000484 Argv[1] = nullptr;
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000485 return Main(1, Argv);
486}
487
Lang Hamesc3747f22014-06-27 20:20:57 +0000488static int checkAllExpressions(RuntimeDyldChecker &Checker) {
489 for (const auto& CheckerFileName : CheckFiles) {
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000490 ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
491 MemoryBuffer::getFileOrSTDIN(CheckerFileName);
492 if (std::error_code EC = CheckerFileBuf.getError())
Lang Hames3e222972016-03-25 17:25:34 +0000493 ErrorAndExit("unable to read input '" + CheckerFileName + "': " +
Lang Hamesc3747f22014-06-27 20:20:57 +0000494 EC.message());
495
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000496 if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
497 CheckerFileBuf.get().get()))
Lang Hames3e222972016-03-25 17:25:34 +0000498 ErrorAndExit("some checks in '" + CheckerFileName + "' failed");
Lang Hamesc3747f22014-06-27 20:20:57 +0000499 }
500 return 0;
501}
502
Lang Hamese3f37142017-05-07 17:19:53 +0000503void applySpecificSectionMappings(RuntimeDyldChecker &Checker) {
Lang Hames5ade5842014-09-04 04:19:54 +0000504
505 for (StringRef Mapping : SpecificSectionMappings) {
506
507 size_t EqualsIdx = Mapping.find_first_of("=");
Lang Hamesb21c7642015-07-04 01:35:26 +0000508 std::string SectionIDStr = Mapping.substr(0, EqualsIdx);
Lang Hames5ade5842014-09-04 04:19:54 +0000509 size_t ComaIdx = Mapping.find_first_of(",");
510
Davide Italianoea83f222015-11-21 02:15:51 +0000511 if (ComaIdx == StringRef::npos)
512 report_fatal_error("Invalid section specification '" + Mapping +
513 "'. Should be '<file name>,<section name>=<addr>'");
Lang Hames5ade5842014-09-04 04:19:54 +0000514
Lang Hamesb21c7642015-07-04 01:35:26 +0000515 std::string FileName = SectionIDStr.substr(0, ComaIdx);
516 std::string SectionName = SectionIDStr.substr(ComaIdx + 1);
Lang Hames5ade5842014-09-04 04:19:54 +0000517
518 uint64_t OldAddrInt;
519 std::string ErrorMsg;
520 std::tie(OldAddrInt, ErrorMsg) =
521 Checker.getSectionAddr(FileName, SectionName, true);
522
Davide Italianoea83f222015-11-21 02:15:51 +0000523 if (ErrorMsg != "")
524 report_fatal_error(ErrorMsg);
Lang Hames5ade5842014-09-04 04:19:54 +0000525
526 void* OldAddr = reinterpret_cast<void*>(static_cast<uintptr_t>(OldAddrInt));
527
Lang Hamesb21c7642015-07-04 01:35:26 +0000528 std::string NewAddrStr = Mapping.substr(EqualsIdx + 1);
Lang Hames5ade5842014-09-04 04:19:54 +0000529 uint64_t NewAddr;
530
Davide Italianoea83f222015-11-21 02:15:51 +0000531 if (StringRef(NewAddrStr).getAsInteger(0, NewAddr))
532 report_fatal_error("Invalid section address in mapping '" + Mapping +
533 "'.");
Lang Hames5ade5842014-09-04 04:19:54 +0000534
535 Checker.getRTDyld().mapSectionAddress(OldAddr, NewAddr);
Lang Hames5ade5842014-09-04 04:19:54 +0000536 }
Lang Hames5ade5842014-09-04 04:19:54 +0000537}
538
Lang Hames41364b72014-07-29 23:43:13 +0000539// 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 Pilgrim84354d92016-11-20 13:31:13 +0000543// -target-addr-start <s> -- Specify where the phony target address range starts.
Lang Hames41364b72014-07-29 23:43:13 +0000544// -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 Hamesb21c7642015-07-04 01:35:26 +0000550static void remapSectionsAndSymbols(const llvm::Triple &TargetTriple,
551 TrivialMemoryManager &MemMgr,
552 RuntimeDyldChecker &Checker) {
Lang Hames5ade5842014-09-04 04:19:54 +0000553
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 Hamese3f37142017-05-07 17:19:53 +0000565 applySpecificSectionMappings(Checker);
Lang Hames5ade5842014-09-04 04:19:54 +0000566
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 Hamese3f37142017-05-07 17:19:53 +0000573 // 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 Hames5ade5842014-09-04 04:19:54 +0000576 for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end();
577 I != E;) {
578 WorklistT::iterator Tmp = I;
579 ++I;
Lang Hamese3f37142017-05-07 17:19:53 +0000580 auto LoadAddr = Checker.getSectionLoadAddress(Tmp->first);
Lang Hames5ade5842014-09-04 04:19:54 +0000581
Lang Hamese3f37142017-05-07 17:19:53 +0000582 if (LoadAddr &&
583 *LoadAddr != static_cast<uint64_t>(
584 reinterpret_cast<uintptr_t>(Tmp->first))) {
Lang Hamesc407a172018-10-23 01:36:33 +0000585 // 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 Hames5ade5842014-09-04 04:19:54 +0000590 Worklist.erase(Tmp);
591 }
592 }
Lang Hames41364b72014-07-29 23:43:13 +0000593
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 Hames5ade5842014-09-04 04:19:54 +0000605 // 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 Hames41364b72014-07-29 23:43:13 +0000609
Lang Hames5ade5842014-09-04 04:19:54 +0000610 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 Hames41364b72014-07-29 23:43:13 +0000620 }
621
Lang Hamesb21c7642015-07-04 01:35:26 +0000622 // Add dummy symbols to the memory manager.
623 for (const auto &Mapping : DummySymbolMappings) {
Benjamin Kramerbf60ce02016-11-30 10:01:11 +0000624 size_t EqualsIdx = Mapping.find_first_of('=');
Lang Hamesb21c7642015-07-04 01:35:26 +0000625
Davide Italianoea83f222015-11-21 02:15:51 +0000626 if (EqualsIdx == StringRef::npos)
627 report_fatal_error("Invalid dummy symbol specification '" + Mapping +
628 "'. Should be '<symbol name>=<addr>'");
Lang Hamesb21c7642015-07-04 01:35:26 +0000629
630 std::string Symbol = Mapping.substr(0, EqualsIdx);
631 std::string AddrStr = Mapping.substr(EqualsIdx + 1);
632
633 uint64_t Addr;
Davide Italianoea83f222015-11-21 02:15:51 +0000634 if (StringRef(AddrStr).getAsInteger(0, Addr))
635 report_fatal_error("Invalid symbol mapping '" + Mapping + "'.");
Lang Hamesb21c7642015-07-04 01:35:26 +0000636
637 MemMgr.addDummySymbol(Symbol, Addr);
638 }
Lang Hames41364b72014-07-29 23:43:13 +0000639}
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 Hamesc3747f22014-06-27 20:20:57 +0000644static int linkAndVerify() {
645
646 // Check for missing triple.
Davide Italiano87daa912015-11-21 05:44:41 +0000647 if (TripleName == "")
Lang Hames3e222972016-03-25 17:25:34 +0000648 ErrorAndExit("-triple required when running in -verify mode.");
Lang Hamesc3747f22014-06-27 20:20:57 +0000649
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 Italiano87daa912015-11-21 05:44:41 +0000655 if (!TheTarget)
Lang Hames3e222972016-03-25 17:25:34 +0000656 ErrorAndExit("Error accessing target '" + TripleName + "': " + ErrorStr);
Davide Italiano87daa912015-11-21 05:44:41 +0000657
Lang Hamesc3747f22014-06-27 20:20:57 +0000658 TripleName = TheTriple.getTriple();
659
660 std::unique_ptr<MCSubtargetInfo> STI(
Petar Jovanovic8dad59b2015-06-23 22:52:19 +0000661 TheTarget->createMCSubtargetInfo(TripleName, MCPU, ""));
Davide Italiano9fed20c2015-11-21 05:49:07 +0000662 if (!STI)
Lang Hames3e222972016-03-25 17:25:34 +0000663 ErrorAndExit("Unable to create subtarget info!");
Lang Hamesc3747f22014-06-27 20:20:57 +0000664
665 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
Davide Italiano9fed20c2015-11-21 05:49:07 +0000666 if (!MRI)
Lang Hames3e222972016-03-25 17:25:34 +0000667 ErrorAndExit("Unable to create target register info!");
Lang Hamesc3747f22014-06-27 20:20:57 +0000668
669 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
Davide Italiano9fed20c2015-11-21 05:49:07 +0000670 if (!MAI)
Lang Hames3e222972016-03-25 17:25:34 +0000671 ErrorAndExit("Unable to create target asm info!");
Lang Hamesc3747f22014-06-27 20:20:57 +0000672
673 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
674
675 std::unique_ptr<MCDisassembler> Disassembler(
676 TheTarget->createMCDisassembler(*STI, Ctx));
Davide Italiano9fed20c2015-11-21 05:49:07 +0000677 if (!Disassembler)
Lang Hames3e222972016-03-25 17:25:34 +0000678 ErrorAndExit("Unable to create disassembler!");
Lang Hamesc3747f22014-06-27 20:20:57 +0000679
680 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
681
Daniel Sanders47b167d2015-09-15 16:17:27 +0000682 std::unique_ptr<MCInstPrinter> InstPrinter(
683 TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
Lang Hamesc3747f22014-06-27 20:20:57 +0000684
685 // Load any dylibs requested on the command line.
686 loadDylibs();
687
688 // Instantiate a dynamic linker.
689 TrivialMemoryManager MemMgr;
Davide Italiano8c651fb2015-10-21 22:12:03 +0000690 doPreallocation(MemMgr);
Lang Hamesda621552015-03-30 03:37:06 +0000691 RuntimeDyld Dyld(MemMgr, MemMgr);
Lang Hamesef279f92014-09-03 05:42:52 +0000692 Dyld.setProcessAllSections(true);
Lang Hamesdaf061c2014-07-22 22:47:39 +0000693 RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),
694 llvm::dbgs());
Lang Hamesc3747f22014-06-27 20:20:57 +0000695
696 // If we don't have any input files, read from stdin.
697 if (!InputFileList.size())
698 InputFileList.push_back("-");
Davide Italianof2e1e0e2015-10-10 00:45:24 +0000699 for (auto &Filename : InputFileList) {
Lang Hamesc3747f22014-06-27 20:20:57 +0000700 // Load the input memory buffer.
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000701 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
Davide Italianof2e1e0e2015-10-10 00:45:24 +0000702 MemoryBuffer::getFileOrSTDIN(Filename);
Lang Hames216e5322014-11-26 16:54:40 +0000703
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000704 if (std::error_code EC = InputBuffer.getError())
Lang Hames3e222972016-03-25 17:25:34 +0000705 ErrorAndExit("unable to read input: '" + EC.message() + "'");
Lang Hamesc3747f22014-06-27 20:20:57 +0000706
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +0000707 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
Lang Hames216e5322014-11-26 16:54:40 +0000708 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
709
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +0000710 if (!MaybeObj) {
711 std::string Buf;
712 raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +0000713 logAllUnhandledErrors(MaybeObj.takeError(), OS);
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +0000714 OS.flush();
715 ErrorAndExit("unable to create object file: '" + Buf + "'");
716 }
Lang Hames216e5322014-11-26 16:54:40 +0000717
718 ObjectFile &Obj = **MaybeObj;
719
Lang Hamesc3747f22014-06-27 20:20:57 +0000720 // Load the object file
Lang Hames216e5322014-11-26 16:54:40 +0000721 Dyld.loadObject(Obj);
722 if (Dyld.hasError()) {
Lang Hames3e222972016-03-25 17:25:34 +0000723 ErrorAndExit(Dyld.getErrorString());
Lang Hamesc3747f22014-06-27 20:20:57 +0000724 }
725 }
726
Lang Hamesb21c7642015-07-04 01:35:26 +0000727 // Re-map the section addresses into the phony target address space and add
728 // dummy symbols.
729 remapSectionsAndSymbols(TheTriple, MemMgr, Checker);
Lang Hamese1681c92014-07-30 03:12:41 +0000730
Lang Hamesc3747f22014-06-27 20:20:57 +0000731 // Resolve all the relocations we can.
732 Dyld.resolveRelocations();
733
Lang Hamesef279f92014-09-03 05:42:52 +0000734 // Register EH frames.
735 Dyld.registerEHFrames();
736
Lang Hames416ea4b2014-08-05 20:51:46 +0000737 int ErrorCode = checkAllExpressions(Checker);
Davide Italiano87daa912015-11-21 05:44:41 +0000738 if (Dyld.hasError())
Lang Hames3e222972016-03-25 17:25:34 +0000739 ErrorAndExit("RTDyld reported an error applying relocations:\n " +
Davide Italiano87daa912015-11-21 05:44:41 +0000740 Dyld.getErrorString());
Lang Hames416ea4b2014-08-05 20:51:46 +0000741
742 return ErrorCode;
Lang Hamesc3747f22014-06-27 20:20:57 +0000743}
744
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000745int main(int argc, char **argv) {
Rui Ueyama0b9d56a2018-04-13 18:26:06 +0000746 InitLLVM X(argc, argv);
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000747 ProgramName = argv[0];
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000748
Lang Hamesc3747f22014-06-27 20:20:57 +0000749 llvm::InitializeAllTargetInfos();
750 llvm::InitializeAllTargetMCs();
751 llvm::InitializeAllDisassemblers();
752
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000753 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
754
755 switch (Action) {
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000756 case AC_Execute:
Jim Grosbach82c25b42011-03-18 17:24:21 +0000757 return executeInput();
Keno Fischerb6976af2015-05-21 21:24:32 +0000758 case AC_PrintDebugLineInfo:
Keno Fischerdbdf6672015-05-30 19:44:53 +0000759 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */ true);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000760 case AC_PrintLineInfo:
Keno Fischerdbdf6672015-05-30 19:44:53 +0000761 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */false);
762 case AC_PrintObjectLineInfo:
763 return printLineInfoForInput(/* LoadObjects */false,/* UseDebugObj */false);
Lang Hamesc3747f22014-06-27 20:20:57 +0000764 case AC_Verify:
765 return linkAndVerify();
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000766 }
Jim Grosbach1cb19a42011-03-18 17:11:39 +0000767}