llvm-rtdyld.cpp revision 276479
1151497Sru//===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===//
2151497Sru//
3151497Sru//                     The LLVM Compiler Infrastructure
4151497Sru//
5151497Sru// This file is distributed under the University of Illinois Open Source
6151497Sru// License. See LICENSE.TXT for details.
7151497Sru//
8151497Sru//===----------------------------------------------------------------------===//
9151497Sru//
10151497Sru// This is a testing tool for use with the MC-JIT LLVM components.
11151497Sru//
12151497Sru//===----------------------------------------------------------------------===//
13151497Sru
14151497Sru#include "llvm/ADT/StringMap.h"
15151497Sru#include "llvm/DebugInfo/DIContext.h"
16151497Sru#include "llvm/ExecutionEngine/ObjectBuffer.h"
17151497Sru#include "llvm/ExecutionEngine/ObjectImage.h"
18151497Sru#include "llvm/ExecutionEngine/RuntimeDyld.h"
19151497Sru#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
20151497Sru#include "llvm/MC/MCAsmInfo.h"
21151497Sru#include "llvm/MC/MCContext.h"
22151497Sru#include "llvm/MC/MCDisassembler.h"
23151497Sru#include "llvm/MC/MCInstrInfo.h"
24151497Sru#include "llvm/MC/MCInstPrinter.h"
25151497Sru#include "llvm/MC/MCRegisterInfo.h"
26151497Sru#include "llvm/Object/MachO.h"
27151497Sru#include "llvm/Support/CommandLine.h"
28151497Sru#include "llvm/Support/DynamicLibrary.h"
29151497Sru#include "llvm/Support/ManagedStatic.h"
30151497Sru#include "llvm/Support/Memory.h"
31151497Sru#include "llvm/Support/MemoryBuffer.h"
32151497Sru#include "llvm/Support/PrettyStackTrace.h"
33151497Sru#include "llvm/Support/raw_ostream.h"
34151497Sru#include "llvm/Support/Signals.h"
35151497Sru#include "llvm/Support/TargetRegistry.h"
36151497Sru#include "llvm/Support/TargetSelect.h"
37151497Sru#include <system_error>
38151497Sru
39151497Sruusing namespace llvm;
40151497Sruusing namespace llvm::object;
41151497Sru
42151497Srustatic cl::list<std::string>
43151497SruInputFileList(cl::Positional, cl::ZeroOrMore,
44151497Sru              cl::desc("<input file>"));
45151497Sru
46151497Sruenum ActionType {
47151497Sru  AC_Execute,
48151497Sru  AC_PrintLineInfo,
49151497Sru  AC_Verify
50151497Sru};
51151497Sru
52151497Srustatic cl::opt<ActionType>
53151497SruAction(cl::desc("Action to perform:"),
54151497Sru       cl::init(AC_Execute),
55151497Sru       cl::values(clEnumValN(AC_Execute, "execute",
56151497Sru                             "Load, link, and execute the inputs."),
57151497Sru                  clEnumValN(AC_PrintLineInfo, "printline",
58151497Sru                             "Load, link, and print line information for each function."),
59151497Sru                  clEnumValN(AC_Verify, "verify",
60151497Sru                             "Load, link and verify the resulting memory image."),
61151497Sru                  clEnumValEnd));
62151497Sru
63151497Srustatic cl::opt<std::string>
64151497SruEntryPoint("entry",
65151497Sru           cl::desc("Function to call as entry point."),
66151497Sru           cl::init("_main"));
67151497Sru
68151497Srustatic cl::list<std::string>
69151497SruDylibs("dylib",
70151497Sru       cl::desc("Add library."),
71151497Sru       cl::ZeroOrMore);
72151497Sru
73151497Srustatic cl::opt<std::string>
74151497SruTripleName("triple", cl::desc("Target triple for disassembler"));
75151497Sru
76151497Srustatic cl::list<std::string>
77151497SruCheckFiles("check",
78151497Sru           cl::desc("File containing RuntimeDyld verifier checks."),
79151497Sru           cl::ZeroOrMore);
80151497Sru
81151497Sru/* *** */
82151497Sru
83151497Sru// A trivial memory manager that doesn't do anything fancy, just uses the
84151497Sru// support library allocation routines directly.
85151497Sruclass TrivialMemoryManager : public RTDyldMemoryManager {
86151497Srupublic:
87151497Sru  SmallVector<sys::MemoryBlock, 16> FunctionMemory;
88151497Sru  SmallVector<sys::MemoryBlock, 16> DataMemory;
89151497Sru
90151497Sru  uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
91151497Sru                               unsigned SectionID,
92151497Sru                               StringRef SectionName) override;
93151497Sru  uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
94151497Sru                               unsigned SectionID, StringRef SectionName,
95151497Sru                               bool IsReadOnly) override;
96151497Sru
97151497Sru  void *getPointerToNamedFunction(const std::string &Name,
98151497Sru                                  bool AbortOnFailure = true) override {
99151497Sru    return nullptr;
100151497Sru  }
101151497Sru
102151497Sru  bool finalizeMemory(std::string *ErrMsg) override { return false; }
103151497Sru
104151497Sru  // Invalidate instruction cache for sections with execute permissions.
105151497Sru  // Some platforms with separate data cache and instruction cache require
106151497Sru  // explicit cache flush, otherwise JIT code manipulations (like resolved
107151497Sru  // relocations) will get to the data cache but not to the instruction cache.
108151497Sru  virtual void invalidateInstructionCache();
109151497Sru};
110151497Sru
111151497Sruuint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
112151497Sru                                                   unsigned Alignment,
113151497Sru                                                   unsigned SectionID,
114151497Sru                                                   StringRef SectionName) {
115151497Sru  sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
116151497Sru  FunctionMemory.push_back(MB);
117151497Sru  return (uint8_t*)MB.base();
118151497Sru}
119151497Sru
120151497Sruuint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
121151497Sru                                                   unsigned Alignment,
122151497Sru                                                   unsigned SectionID,
123151497Sru                                                   StringRef SectionName,
124151497Sru                                                   bool IsReadOnly) {
125151497Sru  sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
126151497Sru  DataMemory.push_back(MB);
127151497Sru  return (uint8_t*)MB.base();
128151497Sru}
129151497Sru
130151497Sruvoid TrivialMemoryManager::invalidateInstructionCache() {
131151497Sru  for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
132151497Sru    sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
133151497Sru                                            FunctionMemory[i].size());
134151497Sru
135151497Sru  for (int i = 0, e = DataMemory.size(); i != e; ++i)
136151497Sru    sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
137151497Sru                                            DataMemory[i].size());
138151497Sru}
139151497Sru
140151497Srustatic const char *ProgramName;
141151497Sru
142151497Srustatic void Message(const char *Type, const Twine &Msg) {
143151497Sru  errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
144151497Sru}
145151497Sru
146151497Srustatic int Error(const Twine &Msg) {
147151497Sru  Message("error", Msg);
148151497Sru  return 1;
149151497Sru}
150151497Sru
151151497Srustatic void loadDylibs() {
152151497Sru  for (const std::string &Dylib : Dylibs) {
153151497Sru    if (sys::fs::is_regular_file(Dylib)) {
154151497Sru      std::string ErrMsg;
155151497Sru      if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
156151497Sru        llvm::errs() << "Error loading '" << Dylib << "': "
157151497Sru                     << ErrMsg << "\n";
158151497Sru    } else
159151497Sru      llvm::errs() << "Dylib not found: '" << Dylib << "'.\n";
160151497Sru  }
161151497Sru}
162151497Sru
163151497Sru/* *** */
164151497Sru
165151497Srustatic int printLineInfoForInput() {
166151497Sru  // Load any dylibs requested on the command line.
167151497Sru  loadDylibs();
168151497Sru
169151497Sru  // If we don't have any input files, read from stdin.
170151497Sru  if (!InputFileList.size())
171151497Sru    InputFileList.push_back("-");
172151497Sru  for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
173151497Sru    // Instantiate a dynamic linker.
174151497Sru    TrivialMemoryManager MemMgr;
175151497Sru    RuntimeDyld Dyld(&MemMgr);
176151497Sru
177151497Sru    // Load the input memory buffer.
178151497Sru
179151497Sru    ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
180151497Sru        MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
181151497Sru    if (std::error_code EC = InputBuffer.getError())
182151497Sru      return Error("unable to read input: '" + EC.message() + "'");
183151497Sru
184151497Sru    std::unique_ptr<ObjectImage> LoadedObject;
185151497Sru    // Load the object file
186151497Sru    LoadedObject.reset(
187151497Sru        Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release())));
188151497Sru    if (!LoadedObject) {
189151497Sru      return Error(Dyld.getErrorString());
190151497Sru    }
191151497Sru
192151497Sru    // Resolve all the relocations we can.
193151497Sru    Dyld.resolveRelocations();
194151497Sru
195151497Sru    std::unique_ptr<DIContext> Context(
196151497Sru        DIContext::getDWARFContext(LoadedObject->getObjectFile()));
197151497Sru
198151497Sru    // Use symbol info to iterate functions in the object.
199151497Sru    for (object::symbol_iterator I = LoadedObject->begin_symbols(),
200151497Sru                                 E = LoadedObject->end_symbols();
201151497Sru         I != E; ++I) {
202151497Sru      object::SymbolRef::Type SymType;
203151497Sru      if (I->getType(SymType)) continue;
204151497Sru      if (SymType == object::SymbolRef::ST_Function) {
205151497Sru        StringRef  Name;
206151497Sru        uint64_t   Addr;
207151497Sru        uint64_t   Size;
208151497Sru        if (I->getName(Name)) continue;
209151497Sru        if (I->getAddress(Addr)) continue;
210151497Sru        if (I->getSize(Size)) continue;
211151497Sru
212151497Sru        outs() << "Function: " << Name << ", Size = " << Size << "\n";
213151497Sru
214151497Sru        DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
215151497Sru        DILineInfoTable::iterator  Begin = Lines.begin();
216151497Sru        DILineInfoTable::iterator  End = Lines.end();
217151497Sru        for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
218151497Sru          outs() << "  Line info @ " << It->first - Addr << ": "
219151497Sru                 << It->second.FileName << ", line:" << It->second.Line << "\n";
220151497Sru        }
221151497Sru      }
222151497Sru    }
223151497Sru  }
224151497Sru
225151497Sru  return 0;
226151497Sru}
227151497Sru
228151497Srustatic int executeInput() {
229151497Sru  // Load any dylibs requested on the command line.
230151497Sru  loadDylibs();
231151497Sru
232151497Sru  // Instantiate a dynamic linker.
233151497Sru  TrivialMemoryManager MemMgr;
234151497Sru  RuntimeDyld Dyld(&MemMgr);
235151497Sru
236151497Sru  // If we don't have any input files, read from stdin.
237151497Sru  if (!InputFileList.size())
238151497Sru    InputFileList.push_back("-");
239151497Sru  for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
240151497Sru    // Load the input memory buffer.
241151497Sru    ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
242151497Sru        MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
243151497Sru    if (std::error_code EC = InputBuffer.getError())
244151497Sru      return Error("unable to read input: '" + EC.message() + "'");
245151497Sru    std::unique_ptr<ObjectImage> LoadedObject;
246151497Sru    // Load the object file
247151497Sru    LoadedObject.reset(
248151497Sru        Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release())));
249151497Sru    if (!LoadedObject) {
250151497Sru      return Error(Dyld.getErrorString());
251151497Sru    }
252151497Sru  }
253151497Sru
254151497Sru  // Resolve all the relocations we can.
255151497Sru  Dyld.resolveRelocations();
256151497Sru  // Clear instruction cache before code will be executed.
257151497Sru  MemMgr.invalidateInstructionCache();
258151497Sru
259151497Sru  // FIXME: Error out if there are unresolved relocations.
260151497Sru
261151497Sru  // Get the address of the entry point (_main by default).
262151497Sru  void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
263151497Sru  if (!MainAddress)
264151497Sru    return Error("no definition for '" + EntryPoint + "'");
265151497Sru
266151497Sru  // Invalidate the instruction cache for each loaded function.
267151497Sru  for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) {
268151497Sru    sys::MemoryBlock &Data = MemMgr.FunctionMemory[i];
269151497Sru    // Make sure the memory is executable.
270151497Sru    std::string ErrorStr;
271151497Sru    sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
272151497Sru    if (!sys::Memory::setExecutable(Data, &ErrorStr))
273151497Sru      return Error("unable to mark function executable: '" + ErrorStr + "'");
274151497Sru  }
275151497Sru
276151497Sru  // Dispatch to _main().
277151497Sru  errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
278151497Sru
279151497Sru  int (*Main)(int, const char**) =
280151497Sru    (int(*)(int,const char**)) uintptr_t(MainAddress);
281151497Sru  const char **Argv = new const char*[2];
282151497Sru  // Use the name of the first input object module as argv[0] for the target.
283151497Sru  Argv[0] = InputFileList[0].c_str();
284151497Sru  Argv[1] = nullptr;
285151497Sru  return Main(1, Argv);
286151497Sru}
287151497Sru
288151497Srustatic int checkAllExpressions(RuntimeDyldChecker &Checker) {
289151497Sru  for (const auto& CheckerFileName : CheckFiles) {
290151497Sru    ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
291151497Sru        MemoryBuffer::getFileOrSTDIN(CheckerFileName);
292151497Sru    if (std::error_code EC = CheckerFileBuf.getError())
293151497Sru      return Error("unable to read input '" + CheckerFileName + "': " +
294151497Sru                   EC.message());
295151497Sru
296151497Sru    if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
297151497Sru                                       CheckerFileBuf.get().get()))
298151497Sru      return Error("some checks in '" + CheckerFileName + "' failed");
299151497Sru  }
300151497Sru  return 0;
301151497Sru}
302151497Sru
303151497Srustatic int linkAndVerify() {
304151497Sru
305151497Sru  // Check for missing triple.
306151497Sru  if (TripleName == "") {
307151497Sru    llvm::errs() << "Error: -triple required when running in -verify mode.\n";
308151497Sru    return 1;
309151497Sru  }
310151497Sru
311151497Sru  // Look up the target and build the disassembler.
312151497Sru  Triple TheTriple(Triple::normalize(TripleName));
313151497Sru  std::string ErrorStr;
314151497Sru  const Target *TheTarget =
315151497Sru    TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
316151497Sru  if (!TheTarget) {
317151497Sru    llvm::errs() << "Error accessing target '" << TripleName << "': "
318151497Sru                 << ErrorStr << "\n";
319151497Sru    return 1;
320151497Sru  }
321151497Sru  TripleName = TheTriple.getTriple();
322151497Sru
323151497Sru  std::unique_ptr<MCSubtargetInfo> STI(
324151497Sru    TheTarget->createMCSubtargetInfo(TripleName, "", ""));
325151497Sru  assert(STI && "Unable to create subtarget info!");
326151497Sru
327151497Sru  std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
328151497Sru  assert(MRI && "Unable to create target register info!");
329151497Sru
330151497Sru  std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
331151497Sru  assert(MAI && "Unable to create target asm info!");
332151497Sru
333151497Sru  MCContext Ctx(MAI.get(), MRI.get(), nullptr);
334151497Sru
335151497Sru  std::unique_ptr<MCDisassembler> Disassembler(
336151497Sru    TheTarget->createMCDisassembler(*STI, Ctx));
337151497Sru  assert(Disassembler && "Unable to create disassembler!");
338151497Sru
339151497Sru  std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
340151497Sru
341151497Sru  std::unique_ptr<MCInstPrinter> InstPrinter(
342151497Sru    TheTarget->createMCInstPrinter(0, *MAI, *MII, *MRI, *STI));
343151497Sru
344151497Sru  // Load any dylibs requested on the command line.
345151497Sru  loadDylibs();
346151497Sru
347151497Sru  // Instantiate a dynamic linker.
348151497Sru  TrivialMemoryManager MemMgr;
349151497Sru  RuntimeDyld Dyld(&MemMgr);
350151497Sru
351151497Sru  // If we don't have any input files, read from stdin.
352151497Sru  if (!InputFileList.size())
353151497Sru    InputFileList.push_back("-");
354151497Sru  for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
355151497Sru    // Load the input memory buffer.
356151497Sru    ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
357151497Sru        MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
358151497Sru    if (std::error_code EC = InputBuffer.getError())
359151497Sru      return Error("unable to read input: '" + EC.message() + "'");
360151497Sru
361151497Sru    std::unique_ptr<ObjectImage> LoadedObject;
362151497Sru    // Load the object file
363151497Sru    LoadedObject.reset(
364151497Sru        Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release())));
365151497Sru    if (!LoadedObject) {
366151497Sru      return Error(Dyld.getErrorString());
367151497Sru    }
368151497Sru  }
369151497Sru
370151497Sru  // Resolve all the relocations we can.
371151497Sru  Dyld.resolveRelocations();
372151497Sru
373151497Sru  RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),
374151497Sru                             llvm::dbgs());
375151497Sru  return checkAllExpressions(Checker);
376151497Sru}
377151497Sru
378151497Sruint main(int argc, char **argv) {
379151497Sru  sys::PrintStackTraceOnErrorSignal();
380151497Sru  PrettyStackTraceProgram X(argc, argv);
381151497Sru
382151497Sru  ProgramName = argv[0];
383151497Sru  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
384151497Sru
385151497Sru  llvm::InitializeAllTargetInfos();
386151497Sru  llvm::InitializeAllTargetMCs();
387151497Sru  llvm::InitializeAllDisassemblers();
388151497Sru
389151497Sru  cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
390151497Sru
391151497Sru  switch (Action) {
392151497Sru  case AC_Execute:
393151497Sru    return executeInput();
394151497Sru  case AC_PrintLineInfo:
395151497Sru    return printLineInfoForInput();
396151497Sru  case AC_Verify:
397151497Sru    return linkAndVerify();
398151497Sru  }
399151497Sru}
400151497Sru