blob: 5cea4658363fa3ab561b8d109e3a88d670c17b91 [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"
Andrew Kaylord55d7012013-01-25 22:50:58 +000015#include "llvm/DebugInfo/DIContext.h"
Andrew Kayloradc70562012-10-02 21:18:39 +000016#include "llvm/ExecutionEngine/ObjectBuffer.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000017#include "llvm/ExecutionEngine/ObjectImage.h"
18#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"
22#include "llvm/MC/MCDisassembler.h"
23#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCInstPrinter.h"
25#include "llvm/MC/MCRegisterInfo.h"
Rafael Espindola6e040c02013-04-26 20:07:33 +000026#include "llvm/Object/MachO.h"
Jim Grosbach0072cdb2011-03-18 17:11:39 +000027#include "llvm/Support/CommandLine.h"
Lang Hamesd311c0e2014-05-13 22:37:41 +000028#include "llvm/Support/DynamicLibrary.h"
Jim Grosbach0072cdb2011-03-18 17:11:39 +000029#include "llvm/Support/ManagedStatic.h"
30#include "llvm/Support/Memory.h"
31#include "llvm/Support/MemoryBuffer.h"
Alp Toker9f679322013-11-05 09:33:43 +000032#include "llvm/Support/PrettyStackTrace.h"
Jim Grosbach0072cdb2011-03-18 17:11:39 +000033#include "llvm/Support/raw_ostream.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"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000037#include <system_error>
Lang Hamese1c11382014-06-27 20:20:57 +000038
Jim Grosbach0072cdb2011-03-18 17:11:39 +000039using namespace llvm;
40using namespace llvm::object;
41
Jim Grosbach7cb41d72011-04-13 15:49:40 +000042static cl::list<std::string>
43InputFileList(cl::Positional, cl::ZeroOrMore,
44 cl::desc("<input file>"));
Jim Grosbach0072cdb2011-03-18 17:11:39 +000045
46enum ActionType {
Andrew Kaylord55d7012013-01-25 22:50:58 +000047 AC_Execute,
Lang Hamese1c11382014-06-27 20:20:57 +000048 AC_PrintLineInfo,
49 AC_Verify
Jim Grosbach0072cdb2011-03-18 17:11:39 +000050};
51
52static cl::opt<ActionType>
53Action(cl::desc("Action to perform:"),
54 cl::init(AC_Execute),
55 cl::values(clEnumValN(AC_Execute, "execute",
56 "Load, link, and execute the inputs."),
Andrew Kaylord55d7012013-01-25 22:50:58 +000057 clEnumValN(AC_PrintLineInfo, "printline",
58 "Load, link, and print line information for each function."),
Lang Hamese1c11382014-06-27 20:20:57 +000059 clEnumValN(AC_Verify, "verify",
60 "Load, link and verify the resulting memory image."),
Jim Grosbach0072cdb2011-03-18 17:11:39 +000061 clEnumValEnd));
62
Jim Grosbachd35159a2011-04-13 15:38:30 +000063static cl::opt<std::string>
64EntryPoint("entry",
65 cl::desc("Function to call as entry point."),
66 cl::init("_main"));
67
Lang Hamesd311c0e2014-05-13 22:37:41 +000068static cl::list<std::string>
69Dylibs("dylib",
70 cl::desc("Add library."),
71 cl::ZeroOrMore);
72
Lang Hamese1c11382014-06-27 20:20:57 +000073static cl::opt<std::string>
74TripleName("triple", cl::desc("Target triple for disassembler"));
75
76static cl::list<std::string>
77CheckFiles("check",
78 cl::desc("File containing RuntimeDyld verifier checks."),
79 cl::ZeroOrMore);
80
Lang Hames9cb73532014-07-29 23:43:13 +000081static cl::opt<uint64_t>
82TargetAddrStart("target-addr-start",
83 cl::desc("For -verify only: start of phony target address "
84 "range."),
85 cl::init(4096), // Start at "page 1" - no allocating at "null".
86 cl::Hidden);
87
88static cl::opt<uint64_t>
89TargetAddrEnd("target-addr-end",
90 cl::desc("For -verify only: end of phony target address range."),
91 cl::init(~0ULL),
92 cl::Hidden);
93
94static cl::opt<uint64_t>
95TargetSectionSep("target-section-sep",
96 cl::desc("For -verify only: Separation between sections in "
97 "phony target address space."),
98 cl::init(0),
99 cl::Hidden);
100
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000101/* *** */
102
Jim Grosbach2dcef0502011-04-04 23:04:39 +0000103// A trivial memory manager that doesn't do anything fancy, just uses the
104// support library allocation routines directly.
105class TrivialMemoryManager : public RTDyldMemoryManager {
106public:
Jim Grosbach3ed03f12011-04-12 00:23:32 +0000107 SmallVector<sys::MemoryBlock, 16> FunctionMemory;
Jim Grosbacheff0a402012-01-16 22:26:39 +0000108 SmallVector<sys::MemoryBlock, 16> DataMemory;
109
110 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
Craig Toppere56917c2014-03-08 08:27:28 +0000111 unsigned SectionID,
112 StringRef SectionName) override;
Jim Grosbacheff0a402012-01-16 22:26:39 +0000113 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000114 unsigned SectionID, StringRef SectionName,
Craig Toppere56917c2014-03-08 08:27:28 +0000115 bool IsReadOnly) override;
Jim Grosbach3ed03f12011-04-12 00:23:32 +0000116
Craig Toppere56917c2014-03-08 08:27:28 +0000117 void *getPointerToNamedFunction(const std::string &Name,
118 bool AbortOnFailure = true) override {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000119 return nullptr;
Danil Malyshevbfee5422012-03-28 21:46:36 +0000120 }
121
Craig Toppere56917c2014-03-08 08:27:28 +0000122 bool finalizeMemory(std::string *ErrMsg) override { return false; }
Andrew Kaylora342cb92012-11-15 23:50:01 +0000123
Danil Malyshev8c17fbd2012-05-16 18:50:11 +0000124 // Invalidate instruction cache for sections with execute permissions.
125 // Some platforms with separate data cache and instruction cache require
126 // explicit cache flush, otherwise JIT code manipulations (like resolved
127 // relocations) will get to the data cache but not to the instruction cache.
128 virtual void invalidateInstructionCache();
Jim Grosbach2dcef0502011-04-04 23:04:39 +0000129};
130
Jim Grosbacheff0a402012-01-16 22:26:39 +0000131uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
132 unsigned Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000133 unsigned SectionID,
134 StringRef SectionName) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000135 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
Danil Malyshev8c17fbd2012-05-16 18:50:11 +0000136 FunctionMemory.push_back(MB);
137 return (uint8_t*)MB.base();
Jim Grosbacheff0a402012-01-16 22:26:39 +0000138}
139
140uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
141 unsigned Alignment,
Andrew Kaylora342cb92012-11-15 23:50:01 +0000142 unsigned SectionID,
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000143 StringRef SectionName,
Andrew Kaylora342cb92012-11-15 23:50:01 +0000144 bool IsReadOnly) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000145 sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
Danil Malyshev8c17fbd2012-05-16 18:50:11 +0000146 DataMemory.push_back(MB);
147 return (uint8_t*)MB.base();
148}
149
150void TrivialMemoryManager::invalidateInstructionCache() {
151 for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
152 sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
153 FunctionMemory[i].size());
154
155 for (int i = 0, e = DataMemory.size(); i != e; ++i)
156 sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
157 DataMemory[i].size());
Jim Grosbacheff0a402012-01-16 22:26:39 +0000158}
159
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000160static const char *ProgramName;
161
162static void Message(const char *Type, const Twine &Msg) {
163 errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
164}
165
166static int Error(const Twine &Msg) {
167 Message("error", Msg);
168 return 1;
169}
170
Lang Hamesd311c0e2014-05-13 22:37:41 +0000171static void loadDylibs() {
172 for (const std::string &Dylib : Dylibs) {
173 if (sys::fs::is_regular_file(Dylib)) {
174 std::string ErrMsg;
175 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
176 llvm::errs() << "Error loading '" << Dylib << "': "
177 << ErrMsg << "\n";
178 } else
179 llvm::errs() << "Dylib not found: '" << Dylib << "'.\n";
180 }
181}
182
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000183/* *** */
184
Andrew Kaylord55d7012013-01-25 22:50:58 +0000185static int printLineInfoForInput() {
Lang Hamesd311c0e2014-05-13 22:37:41 +0000186 // Load any dylibs requested on the command line.
187 loadDylibs();
188
Andrew Kaylord55d7012013-01-25 22:50:58 +0000189 // If we don't have any input files, read from stdin.
190 if (!InputFileList.size())
191 InputFileList.push_back("-");
192 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
193 // Instantiate a dynamic linker.
Benjamin Kramer9ce77082013-08-03 22:16:31 +0000194 TrivialMemoryManager MemMgr;
195 RuntimeDyld Dyld(&MemMgr);
Andrew Kaylord55d7012013-01-25 22:50:58 +0000196
197 // Load the input memory buffer.
Andrew Kaylord55d7012013-01-25 22:50:58 +0000198
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000199 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
200 MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
201 if (std::error_code EC = InputBuffer.getError())
202 return Error("unable to read input: '" + EC.message() + "'");
203
204 std::unique_ptr<ObjectImage> LoadedObject;
Andrew Kaylord55d7012013-01-25 22:50:58 +0000205 // Load the object file
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000206 LoadedObject.reset(
207 Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release())));
Andrew Kaylord55d7012013-01-25 22:50:58 +0000208 if (!LoadedObject) {
209 return Error(Dyld.getErrorString());
210 }
211
212 // Resolve all the relocations we can.
213 Dyld.resolveRelocations();
214
Ahmed Charles56440fd2014-03-06 05:51:42 +0000215 std::unique_ptr<DIContext> Context(
216 DIContext::getDWARFContext(LoadedObject->getObjectFile()));
Andrew Kaylord55d7012013-01-25 22:50:58 +0000217
218 // Use symbol info to iterate functions in the object.
Andrew Kaylord55d7012013-01-25 22:50:58 +0000219 for (object::symbol_iterator I = LoadedObject->begin_symbols(),
220 E = LoadedObject->end_symbols();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000221 I != E; ++I) {
Andrew Kaylord55d7012013-01-25 22:50:58 +0000222 object::SymbolRef::Type SymType;
223 if (I->getType(SymType)) continue;
224 if (SymType == object::SymbolRef::ST_Function) {
225 StringRef Name;
226 uint64_t Addr;
227 uint64_t Size;
228 if (I->getName(Name)) continue;
229 if (I->getAddress(Addr)) continue;
230 if (I->getSize(Size)) continue;
231
232 outs() << "Function: " << Name << ", Size = " << Size << "\n";
233
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000234 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
235 DILineInfoTable::iterator Begin = Lines.begin();
236 DILineInfoTable::iterator End = Lines.end();
237 for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
238 outs() << " Line info @ " << It->first - Addr << ": "
Alexey Samsonovd0109992014-04-18 21:36:39 +0000239 << It->second.FileName << ", line:" << It->second.Line << "\n";
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000240 }
Andrew Kaylord55d7012013-01-25 22:50:58 +0000241 }
242 }
243 }
244
245 return 0;
246}
247
Jim Grosbach4d5284b2011-03-18 17:24:21 +0000248static int executeInput() {
Lang Hamesd311c0e2014-05-13 22:37:41 +0000249 // Load any dylibs requested on the command line.
250 loadDylibs();
251
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000252 // Instantiate a dynamic linker.
Benjamin Kramer9ce77082013-08-03 22:16:31 +0000253 TrivialMemoryManager MemMgr;
254 RuntimeDyld Dyld(&MemMgr);
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000255
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000256 // If we don't have any input files, read from stdin.
257 if (!InputFileList.size())
258 InputFileList.push_back("-");
259 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
260 // Load the input memory buffer.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000261 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
262 MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
263 if (std::error_code EC = InputBuffer.getError())
264 return Error("unable to read input: '" + EC.message() + "'");
Ahmed Charles56440fd2014-03-06 05:51:42 +0000265 std::unique_ptr<ObjectImage> LoadedObject;
Andrew Kayloradc70562012-10-02 21:18:39 +0000266 // Load the object file
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000267 LoadedObject.reset(
268 Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release())));
Andrew Kayloradc70562012-10-02 21:18:39 +0000269 if (!LoadedObject) {
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000270 return Error(Dyld.getErrorString());
271 }
Jim Grosbach40411cc2011-03-22 18:19:42 +0000272 }
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000273
Jim Grosbach733d3052011-04-12 21:20:41 +0000274 // Resolve all the relocations we can.
275 Dyld.resolveRelocations();
Danil Malyshev8c17fbd2012-05-16 18:50:11 +0000276 // Clear instruction cache before code will be executed.
Benjamin Kramer5d62ad22013-08-03 22:18:45 +0000277 MemMgr.invalidateInstructionCache();
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000278
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000279 // FIXME: Error out if there are unresolved relocations.
280
Jim Grosbachd35159a2011-04-13 15:38:30 +0000281 // Get the address of the entry point (_main by default).
282 void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000283 if (!MainAddress)
Jim Grosbachd35159a2011-04-13 15:38:30 +0000284 return Error("no definition for '" + EntryPoint + "'");
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000285
Jim Grosbach3ed03f12011-04-12 00:23:32 +0000286 // Invalidate the instruction cache for each loaded function.
Benjamin Kramer5d62ad22013-08-03 22:18:45 +0000287 for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) {
288 sys::MemoryBlock &Data = MemMgr.FunctionMemory[i];
Jim Grosbach3ed03f12011-04-12 00:23:32 +0000289 // Make sure the memory is executable.
290 std::string ErrorStr;
291 sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
292 if (!sys::Memory::setExecutable(Data, &ErrorStr))
293 return Error("unable to mark function executable: '" + ErrorStr + "'");
294 }
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000295
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000296 // Dispatch to _main().
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000297 errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000298
299 int (*Main)(int, const char**) =
300 (int(*)(int,const char**)) uintptr_t(MainAddress);
301 const char **Argv = new const char*[2];
Jim Grosbach7cb41d72011-04-13 15:49:40 +0000302 // Use the name of the first input object module as argv[0] for the target.
303 Argv[0] = InputFileList[0].c_str();
Craig Toppere6cb63e2014-04-25 04:24:47 +0000304 Argv[1] = nullptr;
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000305 return Main(1, Argv);
306}
307
Lang Hamese1c11382014-06-27 20:20:57 +0000308static int checkAllExpressions(RuntimeDyldChecker &Checker) {
309 for (const auto& CheckerFileName : CheckFiles) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000310 ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
311 MemoryBuffer::getFileOrSTDIN(CheckerFileName);
312 if (std::error_code EC = CheckerFileBuf.getError())
Lang Hamese1c11382014-06-27 20:20:57 +0000313 return Error("unable to read input '" + CheckerFileName + "': " +
314 EC.message());
315
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000316 if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
317 CheckerFileBuf.get().get()))
Lang Hamese1c11382014-06-27 20:20:57 +0000318 return Error("some checks in '" + CheckerFileName + "' failed");
319 }
320 return 0;
321}
322
Lang Hames9cb73532014-07-29 23:43:13 +0000323// Scatter sections in all directions!
324// Remaps section addresses for -verify mode. The following command line options
325// can be used to customize the layout of the memory within the phony target's
326// address space:
327// -target-addr-start <s> -- Specify where the phony target addres range starts.
328// -target-addr-end <e> -- Specify where the phony target address range ends.
329// -target-section-sep <d> -- Specify how big a gap should be left between the
330// end of one section and the start of the next.
331// Defaults to zero. Set to something big
332// (e.g. 1 << 32) to stress-test stubs, GOTs, etc.
333//
334void remapSections(const llvm::Triple &TargetTriple,
335 const TrivialMemoryManager &MemMgr,
336 RuntimeDyld &RTDyld) {
337
338 // If the -target-addr-end option wasn't explicitly passed, then set it to a
339 // sensible default based on the target triple.
340 if (TargetAddrEnd.getNumOccurrences() == 0) {
341 if (TargetTriple.isArch16Bit())
342 TargetAddrEnd = (1ULL << 16) - 1;
343 else if (TargetTriple.isArch32Bit())
344 TargetAddrEnd = (1ULL << 32) - 1;
345 // TargetAddrEnd already has a sensible default for 64-bit systems, so
346 // there's nothing to do in the 64-bit case.
347 }
348
349 uint64_t NextSectionAddress = TargetAddrStart;
350
351 // Remap code sections.
352 for (const auto& CodeSection : MemMgr.FunctionMemory) {
353 RTDyld.mapSectionAddress(CodeSection.base(), NextSectionAddress);
354 NextSectionAddress += CodeSection.size() + TargetSectionSep;
355 }
356
357 // Remap data sections.
358 for (const auto& DataSection : MemMgr.DataMemory) {
359 RTDyld.mapSectionAddress(DataSection.base(), NextSectionAddress);
360 NextSectionAddress += DataSection.size() + TargetSectionSep;
361 }
362}
363
364// Load and link the objects specified on the command line, but do not execute
365// anything. Instead, attach a RuntimeDyldChecker instance and call it to
366// verify the correctness of the linked memory.
Lang Hamese1c11382014-06-27 20:20:57 +0000367static int linkAndVerify() {
368
369 // Check for missing triple.
370 if (TripleName == "") {
371 llvm::errs() << "Error: -triple required when running in -verify mode.\n";
372 return 1;
373 }
374
375 // Look up the target and build the disassembler.
376 Triple TheTriple(Triple::normalize(TripleName));
377 std::string ErrorStr;
378 const Target *TheTarget =
379 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
380 if (!TheTarget) {
381 llvm::errs() << "Error accessing target '" << TripleName << "': "
382 << ErrorStr << "\n";
383 return 1;
384 }
385 TripleName = TheTriple.getTriple();
386
387 std::unique_ptr<MCSubtargetInfo> STI(
388 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
389 assert(STI && "Unable to create subtarget info!");
390
391 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
392 assert(MRI && "Unable to create target register info!");
393
394 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
395 assert(MAI && "Unable to create target asm info!");
396
397 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
398
399 std::unique_ptr<MCDisassembler> Disassembler(
400 TheTarget->createMCDisassembler(*STI, Ctx));
401 assert(Disassembler && "Unable to create disassembler!");
402
403 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
404
405 std::unique_ptr<MCInstPrinter> InstPrinter(
406 TheTarget->createMCInstPrinter(0, *MAI, *MII, *MRI, *STI));
407
408 // Load any dylibs requested on the command line.
409 loadDylibs();
410
411 // Instantiate a dynamic linker.
412 TrivialMemoryManager MemMgr;
413 RuntimeDyld Dyld(&MemMgr);
Lang Hamesf7acddd2014-07-22 22:47:39 +0000414 RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),
415 llvm::dbgs());
Lang Hamese1c11382014-06-27 20:20:57 +0000416
417 // If we don't have any input files, read from stdin.
418 if (!InputFileList.size())
419 InputFileList.push_back("-");
420 for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
421 // Load the input memory buffer.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000422 ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
423 MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
424 if (std::error_code EC = InputBuffer.getError())
425 return Error("unable to read input: '" + EC.message() + "'");
Lang Hamese1c11382014-06-27 20:20:57 +0000426
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000427 std::unique_ptr<ObjectImage> LoadedObject;
Lang Hamese1c11382014-06-27 20:20:57 +0000428 // Load the object file
429 LoadedObject.reset(
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000430 Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release())));
Lang Hamese1c11382014-06-27 20:20:57 +0000431 if (!LoadedObject) {
432 return Error(Dyld.getErrorString());
433 }
434 }
435
436 // Resolve all the relocations we can.
437 Dyld.resolveRelocations();
438
Lang Hamese1c11382014-06-27 20:20:57 +0000439 return checkAllExpressions(Checker);
440}
441
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000442int main(int argc, char **argv) {
Alp Toker9f679322013-11-05 09:33:43 +0000443 sys::PrintStackTraceOnErrorSignal();
444 PrettyStackTraceProgram X(argc, argv);
445
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000446 ProgramName = argv[0];
447 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
448
Lang Hamese1c11382014-06-27 20:20:57 +0000449 llvm::InitializeAllTargetInfos();
450 llvm::InitializeAllTargetMCs();
451 llvm::InitializeAllDisassemblers();
452
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000453 cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
454
455 switch (Action) {
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000456 case AC_Execute:
Jim Grosbach4d5284b2011-03-18 17:24:21 +0000457 return executeInput();
Andrew Kaylord55d7012013-01-25 22:50:58 +0000458 case AC_PrintLineInfo:
459 return printLineInfoForInput();
Lang Hamese1c11382014-06-27 20:20:57 +0000460 case AC_Verify:
461 return linkAndVerify();
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000462 }
Jim Grosbach0072cdb2011-03-18 17:11:39 +0000463}