blob: 166093b527a9e9d2108a2392c2c1819d988b4d3b [file] [log] [blame]
Jim Grosbach0072cdb2011-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 Carruth4d88a1c2012-12-04 10:44:52 +000014#include "llvm/ADT/StringMap.h"
Zachary Turner6489d7b2015-04-23 17:37:47 +000015#include "llvm/DebugInfo/DIContext.h"
16#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Lang Hames633fe142015-03-30 03:37:06 +000017#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000018#include "llvm/ExecutionEngine/RuntimeDyld.h"
Lang Hamese1c11382014-06-27 20:20:57 +000019#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
20#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCContext.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000022#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Lang Hamese1c11382014-06-27 20:20:57 +000023#include "llvm/MC/MCInstPrinter.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000024#include "llvm/MC/MCInstrInfo.h"
Lang Hamese1c11382014-06-27 20:20:57 +000025#include "llvm/MC/MCRegisterInfo.h"
Pete Cooper3de83e42015-05-15 21:58:42 +000026#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindolab109c032015-06-23 02:08:48 +000027#include "llvm/Object/SymbolSize.h"
Jim Grosbach0072cdb2011-03-18 17:11:39 +000028#include "llvm/Support/CommandLine.h"
Lang Hamesd311c0e2014-05-13 22:37:41 +000029#include "llvm/Support/DynamicLibrary.h"
Jim Grosbach0072cdb2011-03-18 17:11:39 +000030#include "llvm/Support/ManagedStatic.h"
31#include "llvm/Support/Memory.h"
32#include "llvm/Support/MemoryBuffer.h"
Alp Toker9f679322013-11-05 09:33:43 +000033#include "llvm/Support/PrettyStackTrace.h"
Lang Hamese1c11382014-06-27 20:20:57 +000034#include "llvm/Support/Signals.h"
35#include "llvm/Support/TargetRegistry.h"
36#include "llvm/Support/TargetSelect.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000037#include "llvm/Support/raw_ostream.h"
Lang Hames778ef5b2014-09-04 04:19:54 +000038#include <list>
Lang Hamese1c11382014-06-27 20:20:57 +000039
Jim Grosbach0072cdb2011-03-18 17:11:39 +000040using namespace llvm;
41using namespace llvm::object;
42
Jim Grosbach7cb41d72011-04-13 15:49:40 +000043static cl::list<std::string>
44InputFileList(cl::Positional, cl::ZeroOrMore,
Lang Hames9c755452018-03-31 16:01:01 +000045 cl::desc("<input files>"));
Jim Grosbach0072cdb2011-03-18 17:11:39 +000046
47enum ActionType {
Andrew Kaylord55d7012013-01-25 22:50:58 +000048 AC_Execute,
Keno Fischer281b6942015-05-30 19:44:53 +000049 AC_PrintObjectLineInfo,
Lang Hamese1c11382014-06-27 20:20:57 +000050 AC_PrintLineInfo,
Keno Fischerc780e8e2015-05-21 21:24:32 +000051 AC_PrintDebugLineInfo,
Lang Hamese1c11382014-06-27 20:20:57 +000052 AC_Verify
Jim Grosbach0072cdb2011-03-18 17:11:39 +000053};
54
55static cl::opt<ActionType>
56Action(cl::desc("Action to perform:"),
57 cl::init(AC_Execute),
58 cl::values(clEnumValN(AC_Execute, "execute",
59 "Load, link, and execute the inputs."),
Andrew Kaylord55d7012013-01-25 22:50:58 +000060 clEnumValN(AC_PrintLineInfo, "printline",
61 "Load, link, and print line information for each function."),
Keno Fischerc780e8e2015-05-21 21:24:32 +000062 clEnumValN(AC_PrintDebugLineInfo, "printdebugline",
63 "Load, link, and print line information for each function using the debug object"),
Keno Fischer281b6942015-05-30 19:44:53 +000064 clEnumValN(AC_PrintObjectLineInfo, "printobjline",
65 "Like -printlineinfo but does not load the object first"),
Lang Hamese1c11382014-06-27 20:20:57 +000066 clEnumValN(AC_Verify, "verify",
Mehdi Amini732afdd2016-10-08 19:41:06 +000067 "Load, link and verify the resulting memory image.")));
Jim Grosbach0072cdb2011-03-18 17:11:39 +000068
Jim Grosbachd35159a2011-04-13 15:38:30 +000069static cl::opt<std::string>
70EntryPoint("entry",
71 cl::desc("Function to call as entry point."),
72 cl::init("_main"));
73
Lang Hamesd311c0e2014-05-13 22:37:41 +000074static cl::list<std::string>
75Dylibs("dylib",
76 cl::desc("Add library."),
77 cl::ZeroOrMore);
78
Lang Hamese1c11382014-06-27 20:20:57 +000079static cl::opt<std::string>
80TripleName("triple", cl::desc("Target triple for disassembler"));
81
Petar Jovanovic280e5622015-06-23 22:52:19 +000082static cl::opt<std::string>
83MCPU("mcpu",
84 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
85 cl::value_desc("cpu-name"),
86 cl::init(""));
87
Lang Hamese1c11382014-06-27 20:20:57 +000088static cl::list<std::string>
89CheckFiles("check",
90 cl::desc("File containing RuntimeDyld verifier checks."),
91 cl::ZeroOrMore);
92
Lang Hames9cb73532014-07-29 23:43:13 +000093static cl::opt<uint64_t>
Davide Italianob59ea902015-10-21 22:12:03 +000094PreallocMemory("preallocate",
95 cl::desc("Allocate memory upfront rather than on-demand"),
96 cl::init(0));
97
98static cl::opt<uint64_t>
Lang Hames9cb73532014-07-29 23:43:13 +000099TargetAddrStart("target-addr-start",
100 cl::desc("For -verify only: start of phony target address "
101 "range."),
102 cl::init(4096), // Start at "page 1" - no allocating at "null".
103 cl::Hidden);
104
105static cl::opt<uint64_t>
106TargetAddrEnd("target-addr-end",
107 cl::desc("For -verify only: end of phony target address range."),
108 cl::init(~0ULL),
109 cl::Hidden);
110
111static cl::opt<uint64_t>
112TargetSectionSep("target-section-sep",
113 cl::desc("For -verify only: Separation between sections in "
114 "phony target address space."),
115 cl::init(0),
116 cl::Hidden);
117
Lang Hames778ef5b2014-09-04 04:19:54 +0000118static cl::list<std::string>
119SpecificSectionMappings("map-section",
Lang Hames78937c22015-07-04 01:35:26 +0000120 cl::desc("For -verify only: Map a section to a "
121 "specific address."),
122 cl::ZeroOrMore,
123 cl::Hidden);
124
125static cl::list<std::string>
126DummySymbolMappings("dummy-extern",
127 cl::desc("For -verify only: Inject a symbol into the extern "
128 "symbol table."),
129 cl::ZeroOrMore,
130 cl::Hidden);
Lang Hames778ef5b2014-09-04 04:19:54 +0000131
Sanjoy Dasd5658b02015-11-23 21:47:51 +0000132static cl::opt<bool>
133PrintAllocationRequests("print-alloc-requests",
134 cl::desc("Print allocation requests made to the memory "
135 "manager by RuntimeDyld"),
136 cl::Hidden);
137
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000138/* *** */
139
Jim Grosbach2dcef0502011-04-04 23:04:39 +0000140// A trivial memory manager that doesn't do anything fancy, just uses the
141// support library allocation routines directly.
142class TrivialMemoryManager : public RTDyldMemoryManager {
143public:
Jim Grosbach3ed03f12011-04-12 00:23:32 +0000144 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
Jim Grosbacheff0a402012-01-16 22:26:39 +0000145 SmallVector<sys::MemoryBlock, 16> DataMemory;
146
147 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
Craig Toppere56917c2014-03-08 08:27:28 +0000148 unsigned SectionID,
149 StringRef SectionName) override;
Jim Grosbacheff0a402012-01-16 22:26:39 +0000150 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000151 unsigned SectionID, StringRef SectionName,
Craig Toppere56917c2014-03-08 08:27:28 +0000152 bool IsReadOnly) override;
Jim Grosbach3ed03f12011-04-12 00:23:32 +0000153
Craig Toppere56917c2014-03-08 08:27:28 +0000154 void *getPointerToNamedFunction(const std::string &Name,
155 bool AbortOnFailure = true) override {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000156 return nullptr;
Danil Malyshevbfee5422012-03-28 21:46:36 +0000157 }
158
Craig Toppere56917c2014-03-08 08:27:28 +0000159 bool finalizeMemory(std::string *ErrMsg) override { return false; }
Andrew Kaylora342cb92012-11-15 23:50:01 +0000160
Lang Hames78937c22015-07-04 01:35:26 +0000161 void addDummySymbol(const std::string &Name, uint64_t Addr) {
162 DummyExterns[Name] = Addr;
163 }
164
Lang Hamesad4a9112016-08-01 20:49:11 +0000165 JITSymbol findSymbol(const std::string &Name) override {
Lang Hames78937c22015-07-04 01:35:26 +0000166 auto I = DummyExterns.find(Name);
167
168 if (I != DummyExterns.end())
Lang Hamesad4a9112016-08-01 20:49:11 +0000169 return JITSymbol(I->second, JITSymbolFlags::Exported);
Lang Hames78937c22015-07-04 01:35:26 +0000170
171 return RTDyldMemoryManager::findSymbol(Name);
172 }
173
Tim Northover5af339a2015-06-03 18:26:52 +0000174 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
175 size_t Size) override {}
Lang Hamesc936ac72017-05-09 21:32:18 +0000176 void deregisterEHFrames() override {}
Davide Italianob59ea902015-10-21 22:12:03 +0000177
178 void preallocateSlab(uint64_t Size) {
Lang Hamesafcb70d2017-11-16 23:04:44 +0000179 std::error_code EC;
180 sys::MemoryBlock MB =
181 sys::Memory::allocateMappedMemory(Size, nullptr,
182 sys::Memory::MF_READ |
183 sys::Memory::MF_WRITE,
184 EC);
Davide Italianob59ea902015-10-21 22:12:03 +0000185 if (!MB.base())
Lang Hamesafcb70d2017-11-16 23:04:44 +0000186 report_fatal_error("Can't allocate enough memory: " + EC.message());
Davide Italianob59ea902015-10-21 22:12:03 +0000187
188 PreallocSlab = MB;
189 UsePreallocation = true;
190 SlabSize = Size;
191 }
192
193 uint8_t *allocateFromSlab(uintptr_t Size, unsigned Alignment, bool isCode) {
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000194 Size = alignTo(Size, Alignment);
Davide Italianob59ea902015-10-21 22:12:03 +0000195 if (CurrentSlabOffset + Size > SlabSize)
196 report_fatal_error("Can't allocate enough memory. Tune --preallocate");
197
198 uintptr_t OldSlabOffset = CurrentSlabOffset;
199 sys::MemoryBlock MB((void *)OldSlabOffset, Size);
200 if (isCode)
201 FunctionMemory.push_back(MB);
202 else
203 DataMemory.push_back(MB);
204 CurrentSlabOffset += Size;
205 return (uint8_t*)OldSlabOffset;
206 }
207
Lang Hames78937c22015-07-04 01:35:26 +0000208private:
209 std::map<std::string, uint64_t> DummyExterns;
Davide Italianob59ea902015-10-21 22:12:03 +0000210 sys::MemoryBlock PreallocSlab;
211 bool UsePreallocation = false;
212 uintptr_t SlabSize = 0;
213 uintptr_t CurrentSlabOffset = 0;
Jim Grosbach2dcef0502011-04-04 23:04:39 +0000214};
215
Jim Grosbacheff0a402012-01-16 22:26:39 +0000216uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
217 unsigned Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000218 unsigned SectionID,
219 StringRef SectionName) {
Sanjoy Dasd5658b02015-11-23 21:47:51 +0000220 if (PrintAllocationRequests)
221 outs() << "allocateCodeSection(Size = " << Size << ", Alignment = "
222 << Alignment << ", SectionName = " << SectionName << ")\n";
223
Davide Italianob59ea902015-10-21 22:12:03 +0000224 if (UsePreallocation)
225 return allocateFromSlab(Size, Alignment, true /* isCode */);
226
Lang Hamesafcb70d2017-11-16 23:04:44 +0000227 std::error_code EC;
228 sys::MemoryBlock MB =
229 sys::Memory::allocateMappedMemory(Size, nullptr,
230 sys::Memory::MF_READ |
231 sys::Memory::MF_WRITE,
232 EC);
Davide Italiano89151a02015-10-15 00:05:32 +0000233 if (!MB.base())
Lang Hamesafcb70d2017-11-16 23:04:44 +0000234 report_fatal_error("MemoryManager allocation failed: " + EC.message());
Danil Malyshev8c17fbd2012-05-16 18:50:11 +0000235 FunctionMemory.push_back(MB);
236 return (uint8_t*)MB.base();
Jim Grosbacheff0a402012-01-16 22:26:39 +0000237}
238
239uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
240 unsigned Alignment,
Andrew Kaylora342cb92012-11-15 23:50:01 +0000241 unsigned SectionID,
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000242 StringRef SectionName,
Andrew Kaylora342cb92012-11-15 23:50:01 +0000243 bool IsReadOnly) {
Sanjoy Dasd5658b02015-11-23 21:47:51 +0000244 if (PrintAllocationRequests)
245 outs() << "allocateDataSection(Size = " << Size << ", Alignment = "
246 << Alignment << ", SectionName = " << SectionName << ")\n";
247
Davide Italianob59ea902015-10-21 22:12:03 +0000248 if (UsePreallocation)
249 return allocateFromSlab(Size, Alignment, false /* isCode */);
250
Lang Hamesafcb70d2017-11-16 23:04:44 +0000251 std::error_code EC;
252 sys::MemoryBlock MB =
253 sys::Memory::allocateMappedMemory(Size, nullptr,
254 sys::Memory::MF_READ |
255 sys::Memory::MF_WRITE,
256 EC);
Davide Italiano89151a02015-10-15 00:05:32 +0000257 if (!MB.base())
Lang Hamesafcb70d2017-11-16 23:04:44 +0000258 report_fatal_error("MemoryManager allocation failed: " + EC.message());
Danil Malyshev8c17fbd2012-05-16 18:50:11 +0000259 DataMemory.push_back(MB);
260 return (uint8_t*)MB.base();
261}
262
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000263static const char *ProgramName;
264
Lang Hamesee5417d2016-04-05 20:11:24 +0000265static void ErrorAndExit(const Twine &Msg) {
Davide Italianoc2e910d2015-11-20 23:12:15 +0000266 errs() << ProgramName << ": error: " << Msg << "\n";
Lang Hames9e964f32016-03-25 17:25:34 +0000267 exit(1);
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000268}
269
Lang Hamesd311c0e2014-05-13 22:37:41 +0000270static void loadDylibs() {
271 for (const std::string &Dylib : Dylibs) {
Davide Italiano5cdf9362015-11-22 01:58:33 +0000272 if (!sys::fs::is_regular_file(Dylib))
Davide Italiano41d0fa72015-11-21 05:58:19 +0000273 report_fatal_error("Dylib not found: '" + Dylib + "'.");
Davide Italiano5cdf9362015-11-22 01:58:33 +0000274 std::string ErrMsg;
275 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
276 report_fatal_error("Error loading '" + Dylib + "': " + ErrMsg);
Lang Hamesd311c0e2014-05-13 22:37:41 +0000277 }
278}
279
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000280/* *** */
281
Keno Fischerc780e8e2015-05-21 21:24:32 +0000282static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
283 assert(LoadObjects || !UseDebugObj);
284
Lang Hamesd311c0e2014-05-13 22:37:41 +0000285 // Load any dylibs requested on the command line.
286 loadDylibs();
287
Andrew Kaylord55d7012013-01-25 22:50:58 +0000288 // If we don't have any input files, read from stdin.
289 if (!InputFileList.size())
290 InputFileList.push_back("-");
Davide Italiano5d7e8fd2015-10-12 00:57:29 +0000291 for (auto &File : InputFileList) {
Andrew Kaylord55d7012013-01-25 22:50:58 +0000292 // Instantiate a dynamic linker.
Benjamin Kramer9ce77082013-08-03 22:16:31 +0000293 TrivialMemoryManager MemMgr;
Lang Hames633fe142015-03-30 03:37:06 +0000294 RuntimeDyld Dyld(MemMgr, MemMgr);
Andrew Kaylord55d7012013-01-25 22:50:58 +0000295
296 // Load the input memory buffer.
Andrew Kaylord55d7012013-01-25 22:50:58 +0000297
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000298 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
Davide Italiano5d7e8fd2015-10-12 00:57:29 +0000299 MemoryBuffer::getFileOrSTDIN(File);
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000300 if (std::error_code EC = InputBuffer.getError())
Lang Hames9e964f32016-03-25 17:25:34 +0000301 ErrorAndExit("unable to read input: '" + EC.message() + "'");
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000302
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000303 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000304 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
305
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000306 if (!MaybeObj) {
307 std::string Buf;
308 raw_string_ostream OS(Buf);
309 logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
310 OS.flush();
311 ErrorAndExit("unable to create object file: '" + Buf + "'");
312 }
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000313
314 ObjectFile &Obj = **MaybeObj;
315
Keno Fischerc780e8e2015-05-21 21:24:32 +0000316 OwningBinary<ObjectFile> DebugObj;
317 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = nullptr;
318 ObjectFile *SymbolObj = &Obj;
319 if (LoadObjects) {
320 // Load the object file
321 LoadedObjInfo =
322 Dyld.loadObject(Obj);
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000323
Keno Fischerc780e8e2015-05-21 21:24:32 +0000324 if (Dyld.hasError())
Lang Hames9e964f32016-03-25 17:25:34 +0000325 ErrorAndExit(Dyld.getErrorString());
Andrew Kaylord55d7012013-01-25 22:50:58 +0000326
Keno Fischerc780e8e2015-05-21 21:24:32 +0000327 // Resolve all the relocations we can.
328 Dyld.resolveRelocations();
Andrew Kaylord55d7012013-01-25 22:50:58 +0000329
Keno Fischerc780e8e2015-05-21 21:24:32 +0000330 if (UseDebugObj) {
331 DebugObj = LoadedObjInfo->getObjectForDebug(Obj);
332 SymbolObj = DebugObj.getBinary();
Lang Hames5c969332015-07-28 20:51:53 +0000333 LoadedObjInfo.reset();
Keno Fischerc780e8e2015-05-21 21:24:32 +0000334 }
335 }
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000336
Rafael Espindolac398e672017-07-19 22:27:28 +0000337 std::unique_ptr<DIContext> Context =
338 DWARFContext::create(*SymbolObj, LoadedObjInfo.get());
Andrew Kaylord55d7012013-01-25 22:50:58 +0000339
Rafael Espindola6bf32212015-06-24 19:57:32 +0000340 std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
Rafael Espindolab109c032015-06-23 02:08:48 +0000341 object::computeSymbolSizes(*SymbolObj);
Rafael Espindolaa82ce1d2015-05-31 23:15:35 +0000342
Andrew Kaylord55d7012013-01-25 22:50:58 +0000343 // Use symbol info to iterate functions in the object.
Rafael Espindola6bf32212015-06-24 19:57:32 +0000344 for (const auto &P : SymAddr) {
Rafael Espindolab109c032015-06-23 02:08:48 +0000345 object::SymbolRef Sym = P.first;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000346 Expected<SymbolRef::Type> TypeOrErr = Sym.getType();
347 if (!TypeOrErr) {
348 // TODO: Actually report errors helpfully.
349 consumeError(TypeOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000350 continue;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000351 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000352 SymbolRef::Type Type = *TypeOrErr;
353 if (Type == object::SymbolRef::ST_Function) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000354 Expected<StringRef> Name = Sym.getName();
355 if (!Name) {
356 // TODO: Actually report errors helpfully.
357 consumeError(Name.takeError());
Rafael Espindolada176272015-05-31 22:13:51 +0000358 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000359 }
Kevin Enderby931cb652016-06-24 18:24:42 +0000360 Expected<uint64_t> AddrOrErr = Sym.getAddress();
361 if (!AddrOrErr) {
362 // TODO: Actually report errors helpfully.
363 consumeError(AddrOrErr.takeError());
Rafael Espindolada176272015-05-31 22:13:51 +0000364 continue;
Kevin Enderby931cb652016-06-24 18:24:42 +0000365 }
Rafael Espindolaed067c42015-07-03 18:19:00 +0000366 uint64_t Addr = *AddrOrErr;
Rafael Espindolaa82ce1d2015-05-31 23:15:35 +0000367
Rafael Espindolab109c032015-06-23 02:08:48 +0000368 uint64_t Size = P.second;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000369 // If we're not using the debug object, compute the address of the
370 // symbol in memory (rather than that in the unrelocated object file)
371 // and use that to query the DWARFContext.
372 if (!UseDebugObj && LoadObjects) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000373 auto SecOrErr = Sym.getSection();
374 if (!SecOrErr) {
375 // TODO: Actually report errors helpfully.
376 consumeError(SecOrErr.takeError());
377 continue;
378 }
379 object::section_iterator Sec = *SecOrErr;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000380 StringRef SecName;
381 Sec->getName(SecName);
382 uint64_t SectionLoadAddress =
Lang Hames2e88f4f2015-07-28 17:52:11 +0000383 LoadedObjInfo->getSectionLoadAddress(*Sec);
Keno Fischerc780e8e2015-05-21 21:24:32 +0000384 if (SectionLoadAddress != 0)
385 Addr += SectionLoadAddress - Sec->getAddress();
386 }
387
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000388 outs() << "Function: " << *Name << ", Size = " << Size
389 << ", Addr = " << Addr << "\n";
Andrew Kaylord55d7012013-01-25 22:50:58 +0000390
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000391 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
Davide Italiano5d7e8fd2015-10-12 00:57:29 +0000392 for (auto &D : Lines) {
393 outs() << " Line info @ " << D.first - Addr << ": "
394 << D.second.FileName << ", line:" << D.second.Line << "\n";
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000395 }
Andrew Kaylord55d7012013-01-25 22:50:58 +0000396 }
397 }
398 }
399
400 return 0;
401}
402
Davide Italianob59ea902015-10-21 22:12:03 +0000403static void doPreallocation(TrivialMemoryManager &MemMgr) {
404 // Allocate a slab of memory upfront, if required. This is used if
405 // we want to test small code models.
406 if (static_cast<intptr_t>(PreallocMemory) < 0)
407 report_fatal_error("Pre-allocated bytes of memory must be a positive integer.");
408
409 // FIXME: Limit the amount of memory that can be preallocated?
410 if (PreallocMemory != 0)
411 MemMgr.preallocateSlab(PreallocMemory);
412}
413
Jim Grosbach4d5284b2011-03-18 17:24:21 +0000414static int executeInput() {
Lang Hamesd311c0e2014-05-13 22:37:41 +0000415 // Load any dylibs requested on the command line.
416 loadDylibs();
417
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000418 // Instantiate a dynamic linker.
Benjamin Kramer9ce77082013-08-03 22:16:31 +0000419 TrivialMemoryManager MemMgr;
Davide Italianob59ea902015-10-21 22:12:03 +0000420 doPreallocation(MemMgr);
Lang Hames633fe142015-03-30 03:37:06 +0000421 RuntimeDyld Dyld(MemMgr, MemMgr);
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000422
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000423 // If we don't have any input files, read from stdin.
424 if (!InputFileList.size())
425 InputFileList.push_back("-");
Davide Italiano5d7e8fd2015-10-12 00:57:29 +0000426 for (auto &File : InputFileList) {
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000427 // Load the input memory buffer.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000428 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
Davide Italiano5d7e8fd2015-10-12 00:57:29 +0000429 MemoryBuffer::getFileOrSTDIN(File);
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000430 if (std::error_code EC = InputBuffer.getError())
Lang Hames9e964f32016-03-25 17:25:34 +0000431 ErrorAndExit("unable to read input: '" + EC.message() + "'");
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000432 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000433 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
434
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000435 if (!MaybeObj) {
436 std::string Buf;
437 raw_string_ostream OS(Buf);
438 logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
439 OS.flush();
440 ErrorAndExit("unable to create object file: '" + Buf + "'");
441 }
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000442
443 ObjectFile &Obj = **MaybeObj;
444
Andrew Kayloradc70562012-10-02 21:18:39 +0000445 // Load the object file
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000446 Dyld.loadObject(Obj);
447 if (Dyld.hasError()) {
Lang Hames9e964f32016-03-25 17:25:34 +0000448 ErrorAndExit(Dyld.getErrorString());
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000449 }
Jim Grosbach40411cc2011-03-22 18:19:42 +0000450 }
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000451
Davide Italianoaf08e1b2015-11-17 16:37:52 +0000452 // Resove all the relocations we can.
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000453 // FIXME: Error out if there are unresolved relocations.
Davide Italianoaf08e1b2015-11-17 16:37:52 +0000454 Dyld.resolveRelocations();
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000455
Jim Grosbachd35159a2011-04-13 15:38:30 +0000456 // Get the address of the entry point (_main by default).
Lang Hamesb1186032015-03-11 00:43:26 +0000457 void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000458 if (!MainAddress)
Lang Hames9e964f32016-03-25 17:25:34 +0000459 ErrorAndExit("no definition for '" + EntryPoint + "'");
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000460
Jim Grosbach3ed03f12011-04-12 00:23:32 +0000461 // Invalidate the instruction cache for each loaded function.
Davide Italiano5d7e8fd2015-10-12 00:57:29 +0000462 for (auto &FM : MemMgr.FunctionMemory) {
Davide Italianoaf08e1b2015-11-17 16:37:52 +0000463
Jim Grosbach3ed03f12011-04-12 00:23:32 +0000464 // Make sure the memory is executable.
Davide Italianoaf08e1b2015-11-17 16:37:52 +0000465 // setExecutable will call InvalidateInstructionCache.
Lang Hamesafcb70d2017-11-16 23:04:44 +0000466 if (auto EC = sys::Memory::protectMappedMemory(FM,
467 sys::Memory::MF_READ |
468 sys::Memory::MF_EXEC))
469 ErrorAndExit("unable to mark function executable: '" + EC.message() +
470 "'");
Jim Grosbach3ed03f12011-04-12 00:23:32 +0000471 }
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000472
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000473 // Dispatch to _main().
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000474 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000475
476 int (*Main)(int, const char**) =
477 (int(*)(int,const char**)) uintptr_t(MainAddress);
478 const char **Argv = new const char*[2];
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000479 // Use the name of the first input object module as argv[0] for the target.
480 Argv[0] = InputFileList[0].c_str();
Craig Toppere6cb63e2014-04-25 04:24:47 +0000481 Argv[1] = nullptr;
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000482 return Main(1, Argv);
483}
484
Lang Hamese1c11382014-06-27 20:20:57 +0000485static int checkAllExpressions(RuntimeDyldChecker &Checker) {
486 for (const auto& CheckerFileName : CheckFiles) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000487 ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
488 MemoryBuffer::getFileOrSTDIN(CheckerFileName);
489 if (std::error_code EC = CheckerFileBuf.getError())
Lang Hames9e964f32016-03-25 17:25:34 +0000490 ErrorAndExit("unable to read input '" + CheckerFileName + "': " +
Lang Hamese1c11382014-06-27 20:20:57 +0000491 EC.message());
492
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000493 if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
494 CheckerFileBuf.get().get()))
Lang Hames9e964f32016-03-25 17:25:34 +0000495 ErrorAndExit("some checks in '" + CheckerFileName + "' failed");
Lang Hamese1c11382014-06-27 20:20:57 +0000496 }
497 return 0;
498}
499
Lang Hamesff411502017-05-07 17:19:53 +0000500void applySpecificSectionMappings(RuntimeDyldChecker &Checker) {
Lang Hames778ef5b2014-09-04 04:19:54 +0000501
502 for (StringRef Mapping : SpecificSectionMappings) {
503
504 size_t EqualsIdx = Mapping.find_first_of("=");
Lang Hames78937c22015-07-04 01:35:26 +0000505 std::string SectionIDStr = Mapping.substr(0, EqualsIdx);
Lang Hames778ef5b2014-09-04 04:19:54 +0000506 size_t ComaIdx = Mapping.find_first_of(",");
507
Davide Italiano07557fc2015-11-21 02:15:51 +0000508 if (ComaIdx == StringRef::npos)
509 report_fatal_error("Invalid section specification '" + Mapping +
510 "'. Should be '<file name>,<section name>=<addr>'");
Lang Hames778ef5b2014-09-04 04:19:54 +0000511
Lang Hames78937c22015-07-04 01:35:26 +0000512 std::string FileName = SectionIDStr.substr(0, ComaIdx);
513 std::string SectionName = SectionIDStr.substr(ComaIdx + 1);
Lang Hames778ef5b2014-09-04 04:19:54 +0000514
515 uint64_t OldAddrInt;
516 std::string ErrorMsg;
517 std::tie(OldAddrInt, ErrorMsg) =
518 Checker.getSectionAddr(FileName, SectionName, true);
519
Davide Italiano07557fc2015-11-21 02:15:51 +0000520 if (ErrorMsg != "")
521 report_fatal_error(ErrorMsg);
Lang Hames778ef5b2014-09-04 04:19:54 +0000522
523 void* OldAddr = reinterpret_cast<void*>(static_cast<uintptr_t>(OldAddrInt));
524
Lang Hames78937c22015-07-04 01:35:26 +0000525 std::string NewAddrStr = Mapping.substr(EqualsIdx + 1);
Lang Hames778ef5b2014-09-04 04:19:54 +0000526 uint64_t NewAddr;
527
Davide Italiano07557fc2015-11-21 02:15:51 +0000528 if (StringRef(NewAddrStr).getAsInteger(0, NewAddr))
529 report_fatal_error("Invalid section address in mapping '" + Mapping +
530 "'.");
Lang Hames778ef5b2014-09-04 04:19:54 +0000531
532 Checker.getRTDyld().mapSectionAddress(OldAddr, NewAddr);
Lang Hames778ef5b2014-09-04 04:19:54 +0000533 }
Lang Hames778ef5b2014-09-04 04:19:54 +0000534}
535
Lang Hames9cb73532014-07-29 23:43:13 +0000536// Scatter sections in all directions!
537// Remaps section addresses for -verify mode. The following command line options
538// can be used to customize the layout of the memory within the phony target's
539// address space:
Simon Pilgrimdae11f72016-11-20 13:31:13 +0000540// -target-addr-start <s> -- Specify where the phony target address range starts.
Lang Hames9cb73532014-07-29 23:43:13 +0000541// -target-addr-end <e> -- Specify where the phony target address range ends.
542// -target-section-sep <d> -- Specify how big a gap should be left between the
543// end of one section and the start of the next.
544// Defaults to zero. Set to something big
545// (e.g. 1 << 32) to stress-test stubs, GOTs, etc.
546//
Lang Hames78937c22015-07-04 01:35:26 +0000547static void remapSectionsAndSymbols(const llvm::Triple &TargetTriple,
548 TrivialMemoryManager &MemMgr,
549 RuntimeDyldChecker &Checker) {
Lang Hames778ef5b2014-09-04 04:19:54 +0000550
551 // Set up a work list (section addr/size pairs).
552 typedef std::list<std::pair<void*, uint64_t>> WorklistT;
553 WorklistT Worklist;
554
555 for (const auto& CodeSection : MemMgr.FunctionMemory)
556 Worklist.push_back(std::make_pair(CodeSection.base(), CodeSection.size()));
557 for (const auto& DataSection : MemMgr.DataMemory)
558 Worklist.push_back(std::make_pair(DataSection.base(), DataSection.size()));
559
560 // Apply any section-specific mappings that were requested on the command
561 // line.
Lang Hamesff411502017-05-07 17:19:53 +0000562 applySpecificSectionMappings(Checker);
Lang Hames778ef5b2014-09-04 04:19:54 +0000563
564 // Keep an "already allocated" mapping of section target addresses to sizes.
565 // Sections whose address mappings aren't specified on the command line will
566 // allocated around the explicitly mapped sections while maintaining the
567 // minimum separation.
568 std::map<uint64_t, uint64_t> AlreadyAllocated;
569
Lang Hamesff411502017-05-07 17:19:53 +0000570 // Move the previously applied mappings (whether explicitly specified on the
571 // command line, or implicitly set by RuntimeDyld) into the already-allocated
572 // map.
Lang Hames778ef5b2014-09-04 04:19:54 +0000573 for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end();
574 I != E;) {
575 WorklistT::iterator Tmp = I;
576 ++I;
Lang Hamesff411502017-05-07 17:19:53 +0000577 auto LoadAddr = Checker.getSectionLoadAddress(Tmp->first);
Lang Hames778ef5b2014-09-04 04:19:54 +0000578
Lang Hamesff411502017-05-07 17:19:53 +0000579 if (LoadAddr &&
580 *LoadAddr != static_cast<uint64_t>(
581 reinterpret_cast<uintptr_t>(Tmp->first))) {
582 AlreadyAllocated[*LoadAddr] = Tmp->second;
Lang Hames778ef5b2014-09-04 04:19:54 +0000583 Worklist.erase(Tmp);
584 }
585 }
Lang Hames9cb73532014-07-29 23:43:13 +0000586
587 // If the -target-addr-end option wasn't explicitly passed, then set it to a
588 // sensible default based on the target triple.
589 if (TargetAddrEnd.getNumOccurrences() == 0) {
590 if (TargetTriple.isArch16Bit())
591 TargetAddrEnd = (1ULL << 16) - 1;
592 else if (TargetTriple.isArch32Bit())
593 TargetAddrEnd = (1ULL << 32) - 1;
594 // TargetAddrEnd already has a sensible default for 64-bit systems, so
595 // there's nothing to do in the 64-bit case.
596 }
597
Lang Hames778ef5b2014-09-04 04:19:54 +0000598 // Process any elements remaining in the worklist.
599 while (!Worklist.empty()) {
600 std::pair<void*, uint64_t> CurEntry = Worklist.front();
601 Worklist.pop_front();
Lang Hames9cb73532014-07-29 23:43:13 +0000602
Lang Hames778ef5b2014-09-04 04:19:54 +0000603 uint64_t NextSectionAddr = TargetAddrStart;
604
605 for (const auto &Alloc : AlreadyAllocated)
606 if (NextSectionAddr + CurEntry.second + TargetSectionSep <= Alloc.first)
607 break;
608 else
609 NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep;
610
611 AlreadyAllocated[NextSectionAddr] = CurEntry.second;
612 Checker.getRTDyld().mapSectionAddress(CurEntry.first, NextSectionAddr);
Lang Hames9cb73532014-07-29 23:43:13 +0000613 }
614
Lang Hames78937c22015-07-04 01:35:26 +0000615 // Add dummy symbols to the memory manager.
616 for (const auto &Mapping : DummySymbolMappings) {
Benjamin Kramere6ba5ef2016-11-30 10:01:11 +0000617 size_t EqualsIdx = Mapping.find_first_of('=');
Lang Hames78937c22015-07-04 01:35:26 +0000618
Davide Italiano07557fc2015-11-21 02:15:51 +0000619 if (EqualsIdx == StringRef::npos)
620 report_fatal_error("Invalid dummy symbol specification '" + Mapping +
621 "'. Should be '<symbol name>=<addr>'");
Lang Hames78937c22015-07-04 01:35:26 +0000622
623 std::string Symbol = Mapping.substr(0, EqualsIdx);
624 std::string AddrStr = Mapping.substr(EqualsIdx + 1);
625
626 uint64_t Addr;
Davide Italiano07557fc2015-11-21 02:15:51 +0000627 if (StringRef(AddrStr).getAsInteger(0, Addr))
628 report_fatal_error("Invalid symbol mapping '" + Mapping + "'.");
Lang Hames78937c22015-07-04 01:35:26 +0000629
630 MemMgr.addDummySymbol(Symbol, Addr);
631 }
Lang Hames9cb73532014-07-29 23:43:13 +0000632}
633
634// Load and link the objects specified on the command line, but do not execute
635// anything. Instead, attach a RuntimeDyldChecker instance and call it to
636// verify the correctness of the linked memory.
Lang Hamese1c11382014-06-27 20:20:57 +0000637static int linkAndVerify() {
638
639 // Check for missing triple.
Davide Italiano78da7592015-11-21 05:44:41 +0000640 if (TripleName == "")
Lang Hames9e964f32016-03-25 17:25:34 +0000641 ErrorAndExit("-triple required when running in -verify mode.");
Lang Hamese1c11382014-06-27 20:20:57 +0000642
643 // Look up the target and build the disassembler.
644 Triple TheTriple(Triple::normalize(TripleName));
645 std::string ErrorStr;
646 const Target *TheTarget =
647 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
Davide Italiano78da7592015-11-21 05:44:41 +0000648 if (!TheTarget)
Lang Hames9e964f32016-03-25 17:25:34 +0000649 ErrorAndExit("Error accessing target '" + TripleName + "': " + ErrorStr);
Davide Italiano78da7592015-11-21 05:44:41 +0000650
Lang Hamese1c11382014-06-27 20:20:57 +0000651 TripleName = TheTriple.getTriple();
652
653 std::unique_ptr<MCSubtargetInfo> STI(
Petar Jovanovic280e5622015-06-23 22:52:19 +0000654 TheTarget->createMCSubtargetInfo(TripleName, MCPU, ""));
Davide Italianoebb27af2015-11-21 05:49:07 +0000655 if (!STI)
Lang Hames9e964f32016-03-25 17:25:34 +0000656 ErrorAndExit("Unable to create subtarget info!");
Lang Hamese1c11382014-06-27 20:20:57 +0000657
658 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
Davide Italianoebb27af2015-11-21 05:49:07 +0000659 if (!MRI)
Lang Hames9e964f32016-03-25 17:25:34 +0000660 ErrorAndExit("Unable to create target register info!");
Lang Hamese1c11382014-06-27 20:20:57 +0000661
662 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
Davide Italianoebb27af2015-11-21 05:49:07 +0000663 if (!MAI)
Lang Hames9e964f32016-03-25 17:25:34 +0000664 ErrorAndExit("Unable to create target asm info!");
Lang Hamese1c11382014-06-27 20:20:57 +0000665
666 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
667
668 std::unique_ptr<MCDisassembler> Disassembler(
669 TheTarget->createMCDisassembler(*STI, Ctx));
Davide Italianoebb27af2015-11-21 05:49:07 +0000670 if (!Disassembler)
Lang Hames9e964f32016-03-25 17:25:34 +0000671 ErrorAndExit("Unable to create disassembler!");
Lang Hamese1c11382014-06-27 20:20:57 +0000672
673 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
674
Daniel Sanders50f17232015-09-15 16:17:27 +0000675 std::unique_ptr<MCInstPrinter> InstPrinter(
676 TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
Lang Hamese1c11382014-06-27 20:20:57 +0000677
678 // Load any dylibs requested on the command line.
679 loadDylibs();
680
681 // Instantiate a dynamic linker.
682 TrivialMemoryManager MemMgr;
Davide Italianob59ea902015-10-21 22:12:03 +0000683 doPreallocation(MemMgr);
Lang Hames633fe142015-03-30 03:37:06 +0000684 RuntimeDyld Dyld(MemMgr, MemMgr);
Lang Hames925e51b2014-09-03 05:42:52 +0000685 Dyld.setProcessAllSections(true);
Lang Hamesf7acddd2014-07-22 22:47:39 +0000686 RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),
687 llvm::dbgs());
Lang Hamese1c11382014-06-27 20:20:57 +0000688
689 // If we don't have any input files, read from stdin.
690 if (!InputFileList.size())
691 InputFileList.push_back("-");
Davide Italianod91bf8e2015-10-10 00:45:24 +0000692 for (auto &Filename : InputFileList) {
Lang Hamese1c11382014-06-27 20:20:57 +0000693 // Load the input memory buffer.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000694 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
Davide Italianod91bf8e2015-10-10 00:45:24 +0000695 MemoryBuffer::getFileOrSTDIN(Filename);
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000696
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000697 if (std::error_code EC = InputBuffer.getError())
Lang Hames9e964f32016-03-25 17:25:34 +0000698 ErrorAndExit("unable to read input: '" + EC.message() + "'");
Lang Hamese1c11382014-06-27 20:20:57 +0000699
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000700 Expected<std::unique_ptr<ObjectFile>> MaybeObj(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000701 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
702
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000703 if (!MaybeObj) {
704 std::string Buf;
705 raw_string_ostream OS(Buf);
706 logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
707 OS.flush();
708 ErrorAndExit("unable to create object file: '" + Buf + "'");
709 }
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000710
711 ObjectFile &Obj = **MaybeObj;
712
Lang Hamese1c11382014-06-27 20:20:57 +0000713 // Load the object file
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000714 Dyld.loadObject(Obj);
715 if (Dyld.hasError()) {
Lang Hames9e964f32016-03-25 17:25:34 +0000716 ErrorAndExit(Dyld.getErrorString());
Lang Hamese1c11382014-06-27 20:20:57 +0000717 }
718 }
719
Lang Hames78937c22015-07-04 01:35:26 +0000720 // Re-map the section addresses into the phony target address space and add
721 // dummy symbols.
722 remapSectionsAndSymbols(TheTriple, MemMgr, Checker);
Lang Hames375385f2014-07-30 03:12:41 +0000723
Lang Hamese1c11382014-06-27 20:20:57 +0000724 // Resolve all the relocations we can.
725 Dyld.resolveRelocations();
726
Lang Hames925e51b2014-09-03 05:42:52 +0000727 // Register EH frames.
728 Dyld.registerEHFrames();
729
Lang Hamesae172682014-08-05 20:51:46 +0000730 int ErrorCode = checkAllExpressions(Checker);
Davide Italiano78da7592015-11-21 05:44:41 +0000731 if (Dyld.hasError())
Lang Hames9e964f32016-03-25 17:25:34 +0000732 ErrorAndExit("RTDyld reported an error applying relocations:\n " +
Davide Italiano78da7592015-11-21 05:44:41 +0000733 Dyld.getErrorString());
Lang Hamesae172682014-08-05 20:51:46 +0000734
735 return ErrorCode;
Lang Hamese1c11382014-06-27 20:20:57 +0000736}
737
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000738int main(int argc, char **argv) {
Richard Smith2ad6d482016-06-09 00:53:21 +0000739 sys::PrintStackTraceOnErrorSignal(argv[0]);
Alp Toker9f679322013-11-05 09:33:43 +0000740 PrettyStackTraceProgram X(argc, argv);
741
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000742 ProgramName = argv[0];
743 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
744
Lang Hamese1c11382014-06-27 20:20:57 +0000745 llvm::InitializeAllTargetInfos();
746 llvm::InitializeAllTargetMCs();
747 llvm::InitializeAllDisassemblers();
748
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000749 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
750
751 switch (Action) {
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000752 case AC_Execute:
Jim Grosbach4d5284b2011-03-18 17:24:21 +0000753 return executeInput();
Keno Fischerc780e8e2015-05-21 21:24:32 +0000754 case AC_PrintDebugLineInfo:
Keno Fischer281b6942015-05-30 19:44:53 +0000755 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */ true);
Andrew Kaylord55d7012013-01-25 22:50:58 +0000756 case AC_PrintLineInfo:
Keno Fischer281b6942015-05-30 19:44:53 +0000757 return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */false);
758 case AC_PrintObjectLineInfo:
759 return printLineInfoForInput(/* LoadObjects */false,/* UseDebugObj */false);
Lang Hamese1c11382014-06-27 20:20:57 +0000760 case AC_Verify:
761 return linkAndVerify();
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000762 }
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000763}