unique_ptrify a bunch of stuff through RuntimeDyld::loadObject
[oota-llvm.git] / tools / llvm-rtdyld / llvm-rtdyld.cpp
1 //===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is a testing tool for use with the MC-JIT LLVM components.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/DebugInfo/DIContext.h"
16 #include "llvm/ExecutionEngine/ObjectBuffer.h"
17 #include "llvm/ExecutionEngine/ObjectImage.h"
18 #include "llvm/ExecutionEngine/RuntimeDyld.h"
19 #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"
26 #include "llvm/Object/MachO.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/DynamicLibrary.h"
29 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/Support/Memory.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/PrettyStackTrace.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Support/Signals.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/TargetSelect.h"
37 #include <system_error>
38
39 using namespace llvm;
40 using namespace llvm::object;
41
42 static cl::list<std::string>
43 InputFileList(cl::Positional, cl::ZeroOrMore,
44               cl::desc("<input file>"));
45
46 enum ActionType {
47   AC_Execute,
48   AC_PrintLineInfo,
49   AC_Verify
50 };
51
52 static cl::opt<ActionType>
53 Action(cl::desc("Action to perform:"),
54        cl::init(AC_Execute),
55        cl::values(clEnumValN(AC_Execute, "execute",
56                              "Load, link, and execute the inputs."),
57                   clEnumValN(AC_PrintLineInfo, "printline",
58                              "Load, link, and print line information for each function."),
59                   clEnumValN(AC_Verify, "verify",
60                              "Load, link and verify the resulting memory image."),
61                   clEnumValEnd));
62
63 static cl::opt<std::string>
64 EntryPoint("entry",
65            cl::desc("Function to call as entry point."),
66            cl::init("_main"));
67
68 static cl::list<std::string>
69 Dylibs("dylib",
70        cl::desc("Add library."),
71        cl::ZeroOrMore);
72
73 static cl::opt<std::string>
74 TripleName("triple", cl::desc("Target triple for disassembler"));
75
76 static cl::list<std::string>
77 CheckFiles("check",
78            cl::desc("File containing RuntimeDyld verifier checks."),
79            cl::ZeroOrMore);
80
81 static cl::opt<uint64_t>
82 TargetAddrStart("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
88 static cl::opt<uint64_t>
89 TargetAddrEnd("target-addr-end",
90               cl::desc("For -verify only: end of phony target address range."),
91               cl::init(~0ULL),
92               cl::Hidden);
93
94 static cl::opt<uint64_t>
95 TargetSectionSep("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
101 /* *** */
102
103 // A trivial memory manager that doesn't do anything fancy, just uses the
104 // support library allocation routines directly.
105 class TrivialMemoryManager : public RTDyldMemoryManager {
106 public:
107   SmallVector<sys::MemoryBlock, 16> FunctionMemory;
108   SmallVector<sys::MemoryBlock, 16> DataMemory;
109
110   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
111                                unsigned SectionID,
112                                StringRef SectionName) override;
113   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
114                                unsigned SectionID, StringRef SectionName,
115                                bool IsReadOnly) override;
116
117   void *getPointerToNamedFunction(const std::string &Name,
118                                   bool AbortOnFailure = true) override {
119     return nullptr;
120   }
121
122   bool finalizeMemory(std::string *ErrMsg) override { return false; }
123
124   // 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();
129 };
130
131 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
132                                                    unsigned Alignment,
133                                                    unsigned SectionID,
134                                                    StringRef SectionName) {
135   sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
136   FunctionMemory.push_back(MB);
137   return (uint8_t*)MB.base();
138 }
139
140 uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
141                                                    unsigned Alignment,
142                                                    unsigned SectionID,
143                                                    StringRef SectionName,
144                                                    bool IsReadOnly) {
145   sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
146   DataMemory.push_back(MB);
147   return (uint8_t*)MB.base();
148 }
149
150 void 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());
158 }
159
160 static const char *ProgramName;
161
162 static void Message(const char *Type, const Twine &Msg) {
163   errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
164 }
165
166 static int Error(const Twine &Msg) {
167   Message("error", Msg);
168   return 1;
169 }
170
171 static 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
183 /* *** */
184
185 static int printLineInfoForInput() {
186   // Load any dylibs requested on the command line.
187   loadDylibs();
188
189   // 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.
194     TrivialMemoryManager MemMgr;
195     RuntimeDyld Dyld(&MemMgr);
196
197     // Load the input memory buffer.
198
199     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;
205     // Load the object file
206     LoadedObject = Dyld.loadObject(
207         llvm::make_unique<ObjectBuffer>(std::move(*InputBuffer)));
208     if (!LoadedObject) {
209       return Error(Dyld.getErrorString());
210     }
211
212     // Resolve all the relocations we can.
213     Dyld.resolveRelocations();
214
215     std::unique_ptr<DIContext> Context(
216         DIContext::getDWARFContext(*LoadedObject->getObjectFile()));
217
218     // Use symbol info to iterate functions in the object.
219     for (object::symbol_iterator I = LoadedObject->begin_symbols(),
220                                  E = LoadedObject->end_symbols();
221          I != E; ++I) {
222       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
234         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 << ": "
239                  << It->second.FileName << ", line:" << It->second.Line << "\n";
240         }
241       }
242     }
243   }
244
245   return 0;
246 }
247
248 static int executeInput() {
249   // Load any dylibs requested on the command line.
250   loadDylibs();
251
252   // Instantiate a dynamic linker.
253   TrivialMemoryManager MemMgr;
254   RuntimeDyld Dyld(&MemMgr);
255
256   // 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.
261     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() + "'");
265     std::unique_ptr<ObjectImage> LoadedObject;
266     // Load the object file
267     LoadedObject = Dyld.loadObject(
268         llvm::make_unique<ObjectBuffer>(std::move(*InputBuffer)));
269     if (!LoadedObject) {
270       return Error(Dyld.getErrorString());
271     }
272   }
273
274   // Resolve all the relocations we can.
275   Dyld.resolveRelocations();
276   // Clear instruction cache before code will be executed.
277   MemMgr.invalidateInstructionCache();
278
279   // FIXME: Error out if there are unresolved relocations.
280
281   // Get the address of the entry point (_main by default).
282   void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
283   if (!MainAddress)
284     return Error("no definition for '" + EntryPoint + "'");
285
286   // Invalidate the instruction cache for each loaded function.
287   for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) {
288     sys::MemoryBlock &Data = MemMgr.FunctionMemory[i];
289     // 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   }
295
296   // Dispatch to _main().
297   errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
298
299   int (*Main)(int, const char**) =
300     (int(*)(int,const char**)) uintptr_t(MainAddress);
301   const char **Argv = new const char*[2];
302   // Use the name of the first input object module as argv[0] for the target.
303   Argv[0] = InputFileList[0].c_str();
304   Argv[1] = nullptr;
305   return Main(1, Argv);
306 }
307
308 static int checkAllExpressions(RuntimeDyldChecker &Checker) {
309   for (const auto& CheckerFileName : CheckFiles) {
310     ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
311         MemoryBuffer::getFileOrSTDIN(CheckerFileName);
312     if (std::error_code EC = CheckerFileBuf.getError())
313       return Error("unable to read input '" + CheckerFileName + "': " +
314                    EC.message());
315
316     if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
317                                        CheckerFileBuf.get().get()))
318       return Error("some checks in '" + CheckerFileName + "' failed");
319   }
320   return 0;
321 }
322
323 // 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 //
334 void 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.
367 static 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);
414   Dyld.setProcessAllSections(true);
415   RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),
416                              llvm::dbgs());
417
418   // If we don't have any input files, read from stdin.
419   if (!InputFileList.size())
420     InputFileList.push_back("-");
421   for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
422     // Load the input memory buffer.
423     ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
424         MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
425     if (std::error_code EC = InputBuffer.getError())
426       return Error("unable to read input: '" + EC.message() + "'");
427
428     std::unique_ptr<ObjectImage> LoadedObject;
429     // Load the object file
430     LoadedObject = Dyld.loadObject(
431         llvm::make_unique<ObjectBuffer>(std::move(*InputBuffer)));
432     if (!LoadedObject) {
433       return Error(Dyld.getErrorString());
434     }
435   }
436
437   // Re-map the section addresses into the phony target address space.
438   remapSections(TheTriple, MemMgr, Dyld);
439
440   // Resolve all the relocations we can.
441   Dyld.resolveRelocations();
442
443   // Register EH frames.
444   Dyld.registerEHFrames();
445
446   int ErrorCode = checkAllExpressions(Checker);
447   if (Dyld.hasError()) {
448     errs() << "RTDyld reported an error applying relocations:\n  "
449            << Dyld.getErrorString() << "\n";
450     ErrorCode = 1;
451   }
452
453   return ErrorCode;
454 }
455
456 int main(int argc, char **argv) {
457   sys::PrintStackTraceOnErrorSignal();
458   PrettyStackTraceProgram X(argc, argv);
459
460   ProgramName = argv[0];
461   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
462
463   llvm::InitializeAllTargetInfos();
464   llvm::InitializeAllTargetMCs();
465   llvm::InitializeAllDisassemblers();
466
467   cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
468
469   switch (Action) {
470   case AC_Execute:
471     return executeInput();
472   case AC_PrintLineInfo:
473     return printLineInfoForInput();
474   case AC_Verify:
475     return linkAndVerify();
476   }
477 }