2011-03-19 01:11:39 +08:00
|
|
|
//===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is a testing tool for use with the MC-JIT LLVM components.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2015-04-24 01:37:47 +08:00
|
|
|
#include "llvm/DebugInfo/DIContext.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
2014-06-28 04:20:57 +08:00
|
|
|
#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
|
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2016-01-27 00:44:37 +08:00
|
|
|
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
|
2014-06-28 04:20:57 +08:00
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2014-06-28 04:20:57 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2015-05-16 05:58:42 +08:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2013-04-27 04:07:33 +08:00
|
|
|
#include "llvm/Object/MachO.h"
|
2015-06-23 10:08:48 +08:00
|
|
|
#include "llvm/Object/SymbolSize.h"
|
2011-03-19 01:11:39 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-05-14 06:37:41 +08:00
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
2011-03-19 01:11:39 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/Memory.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2013-11-05 17:33:43 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2014-06-28 04:20:57 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-09-04 12:19:54 +08:00
|
|
|
#include <list>
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2014-06-28 04:20:57 +08:00
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
2011-04-13 23:49:40 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
InputFileList(cl::Positional, cl::ZeroOrMore,
|
|
|
|
cl::desc("<input file>"));
|
2011-03-19 01:11:39 +08:00
|
|
|
|
|
|
|
enum ActionType {
|
2013-01-26 06:50:58 +08:00
|
|
|
AC_Execute,
|
2015-05-31 03:44:53 +08:00
|
|
|
AC_PrintObjectLineInfo,
|
2014-06-28 04:20:57 +08:00
|
|
|
AC_PrintLineInfo,
|
2015-05-22 05:24:32 +08:00
|
|
|
AC_PrintDebugLineInfo,
|
2014-06-28 04:20:57 +08:00
|
|
|
AC_Verify
|
2011-03-19 01:11:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static cl::opt<ActionType>
|
|
|
|
Action(cl::desc("Action to perform:"),
|
|
|
|
cl::init(AC_Execute),
|
|
|
|
cl::values(clEnumValN(AC_Execute, "execute",
|
|
|
|
"Load, link, and execute the inputs."),
|
2013-01-26 06:50:58 +08:00
|
|
|
clEnumValN(AC_PrintLineInfo, "printline",
|
|
|
|
"Load, link, and print line information for each function."),
|
2015-05-22 05:24:32 +08:00
|
|
|
clEnumValN(AC_PrintDebugLineInfo, "printdebugline",
|
|
|
|
"Load, link, and print line information for each function using the debug object"),
|
2015-05-31 03:44:53 +08:00
|
|
|
clEnumValN(AC_PrintObjectLineInfo, "printobjline",
|
|
|
|
"Like -printlineinfo but does not load the object first"),
|
2014-06-28 04:20:57 +08:00
|
|
|
clEnumValN(AC_Verify, "verify",
|
|
|
|
"Load, link and verify the resulting memory image."),
|
2011-03-19 01:11:39 +08:00
|
|
|
clEnumValEnd));
|
|
|
|
|
2011-04-13 23:38:30 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
EntryPoint("entry",
|
|
|
|
cl::desc("Function to call as entry point."),
|
|
|
|
cl::init("_main"));
|
|
|
|
|
2014-05-14 06:37:41 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
Dylibs("dylib",
|
|
|
|
cl::desc("Add library."),
|
|
|
|
cl::ZeroOrMore);
|
|
|
|
|
2014-06-28 04:20:57 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
TripleName("triple", cl::desc("Target triple for disassembler"));
|
|
|
|
|
2015-06-24 06:52:19 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
MCPU("mcpu",
|
|
|
|
cl::desc("Target a specific cpu type (-mcpu=help for details)"),
|
|
|
|
cl::value_desc("cpu-name"),
|
|
|
|
cl::init(""));
|
|
|
|
|
2014-06-28 04:20:57 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
CheckFiles("check",
|
|
|
|
cl::desc("File containing RuntimeDyld verifier checks."),
|
|
|
|
cl::ZeroOrMore);
|
|
|
|
|
2015-10-22 06:12:03 +08:00
|
|
|
static cl::opt<uint64_t>
|
|
|
|
PreallocMemory("preallocate",
|
|
|
|
cl::desc("Allocate memory upfront rather than on-demand"),
|
|
|
|
cl::init(0));
|
|
|
|
|
2014-07-30 07:43:13 +08:00
|
|
|
static cl::opt<uint64_t>
|
|
|
|
TargetAddrStart("target-addr-start",
|
|
|
|
cl::desc("For -verify only: start of phony target address "
|
|
|
|
"range."),
|
|
|
|
cl::init(4096), // Start at "page 1" - no allocating at "null".
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<uint64_t>
|
|
|
|
TargetAddrEnd("target-addr-end",
|
|
|
|
cl::desc("For -verify only: end of phony target address range."),
|
|
|
|
cl::init(~0ULL),
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<uint64_t>
|
|
|
|
TargetSectionSep("target-section-sep",
|
|
|
|
cl::desc("For -verify only: Separation between sections in "
|
|
|
|
"phony target address space."),
|
|
|
|
cl::init(0),
|
|
|
|
cl::Hidden);
|
|
|
|
|
2014-09-04 12:19:54 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
SpecificSectionMappings("map-section",
|
2015-07-04 09:35:26 +08:00
|
|
|
cl::desc("For -verify only: Map a section to a "
|
|
|
|
"specific address."),
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
static cl::list<std::string>
|
|
|
|
DummySymbolMappings("dummy-extern",
|
|
|
|
cl::desc("For -verify only: Inject a symbol into the extern "
|
|
|
|
"symbol table."),
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::Hidden);
|
2014-09-04 12:19:54 +08:00
|
|
|
|
2015-11-24 05:47:51 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
PrintAllocationRequests("print-alloc-requests",
|
|
|
|
cl::desc("Print allocation requests made to the memory "
|
|
|
|
"manager by RuntimeDyld"),
|
|
|
|
cl::Hidden);
|
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
/* *** */
|
|
|
|
|
2011-04-05 07:04:39 +08:00
|
|
|
// A trivial memory manager that doesn't do anything fancy, just uses the
|
|
|
|
// support library allocation routines directly.
|
|
|
|
class TrivialMemoryManager : public RTDyldMemoryManager {
|
|
|
|
public:
|
2011-04-12 08:23:32 +08:00
|
|
|
SmallVector<sys::MemoryBlock, 16> FunctionMemory;
|
2012-01-17 06:26:39 +08:00
|
|
|
SmallVector<sys::MemoryBlock, 16> DataMemory;
|
|
|
|
|
|
|
|
uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
|
2014-03-08 16:27:28 +08:00
|
|
|
unsigned SectionID,
|
|
|
|
StringRef SectionName) override;
|
2012-01-17 06:26:39 +08:00
|
|
|
uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned SectionID, StringRef SectionName,
|
2014-03-08 16:27:28 +08:00
|
|
|
bool IsReadOnly) override;
|
2011-04-12 08:23:32 +08:00
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
void *getPointerToNamedFunction(const std::string &Name,
|
|
|
|
bool AbortOnFailure = true) override {
|
2014-04-25 12:24:47 +08:00
|
|
|
return nullptr;
|
2012-03-29 05:46:36 +08:00
|
|
|
}
|
|
|
|
|
2014-03-08 16:27:28 +08:00
|
|
|
bool finalizeMemory(std::string *ErrMsg) override { return false; }
|
2012-11-16 07:50:01 +08:00
|
|
|
|
2015-07-04 09:35:26 +08:00
|
|
|
void addDummySymbol(const std::string &Name, uint64_t Addr) {
|
|
|
|
DummyExterns[Name] = Addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override {
|
|
|
|
auto I = DummyExterns.find(Name);
|
|
|
|
|
|
|
|
if (I != DummyExterns.end())
|
|
|
|
return RuntimeDyld::SymbolInfo(I->second, JITSymbolFlags::Exported);
|
|
|
|
|
|
|
|
return RTDyldMemoryManager::findSymbol(Name);
|
|
|
|
}
|
|
|
|
|
2015-06-04 02:26:52 +08:00
|
|
|
void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
|
|
|
|
size_t Size) override {}
|
|
|
|
void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
|
|
|
|
size_t Size) override {}
|
2015-10-22 06:12:03 +08:00
|
|
|
|
|
|
|
void preallocateSlab(uint64_t Size) {
|
|
|
|
std::string Err;
|
|
|
|
sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, &Err);
|
|
|
|
if (!MB.base())
|
|
|
|
report_fatal_error("Can't allocate enough memory: " + Err);
|
|
|
|
|
|
|
|
PreallocSlab = MB;
|
|
|
|
UsePreallocation = true;
|
|
|
|
SlabSize = Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *allocateFromSlab(uintptr_t Size, unsigned Alignment, bool isCode) {
|
2016-01-15 05:06:47 +08:00
|
|
|
Size = alignTo(Size, Alignment);
|
2015-10-22 06:12:03 +08:00
|
|
|
if (CurrentSlabOffset + Size > SlabSize)
|
|
|
|
report_fatal_error("Can't allocate enough memory. Tune --preallocate");
|
|
|
|
|
|
|
|
uintptr_t OldSlabOffset = CurrentSlabOffset;
|
|
|
|
sys::MemoryBlock MB((void *)OldSlabOffset, Size);
|
|
|
|
if (isCode)
|
|
|
|
FunctionMemory.push_back(MB);
|
|
|
|
else
|
|
|
|
DataMemory.push_back(MB);
|
|
|
|
CurrentSlabOffset += Size;
|
|
|
|
return (uint8_t*)OldSlabOffset;
|
|
|
|
}
|
|
|
|
|
2015-07-04 09:35:26 +08:00
|
|
|
private:
|
|
|
|
std::map<std::string, uint64_t> DummyExterns;
|
2015-10-22 06:12:03 +08:00
|
|
|
sys::MemoryBlock PreallocSlab;
|
|
|
|
bool UsePreallocation = false;
|
|
|
|
uintptr_t SlabSize = 0;
|
|
|
|
uintptr_t CurrentSlabOffset = 0;
|
2011-04-05 07:04:39 +08:00
|
|
|
};
|
|
|
|
|
2012-01-17 06:26:39 +08:00
|
|
|
uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
|
|
|
|
unsigned Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned SectionID,
|
|
|
|
StringRef SectionName) {
|
2015-11-24 05:47:51 +08:00
|
|
|
if (PrintAllocationRequests)
|
|
|
|
outs() << "allocateCodeSection(Size = " << Size << ", Alignment = "
|
|
|
|
<< Alignment << ", SectionName = " << SectionName << ")\n";
|
|
|
|
|
2015-10-22 06:12:03 +08:00
|
|
|
if (UsePreallocation)
|
|
|
|
return allocateFromSlab(Size, Alignment, true /* isCode */);
|
|
|
|
|
2015-10-15 08:05:32 +08:00
|
|
|
std::string Err;
|
|
|
|
sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, &Err);
|
|
|
|
if (!MB.base())
|
|
|
|
report_fatal_error("MemoryManager allocation failed: " + Err);
|
2012-05-17 02:50:11 +08:00
|
|
|
FunctionMemory.push_back(MB);
|
|
|
|
return (uint8_t*)MB.base();
|
2012-01-17 06:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size,
|
|
|
|
unsigned Alignment,
|
2012-11-16 07:50:01 +08:00
|
|
|
unsigned SectionID,
|
2013-10-02 08:59:25 +08:00
|
|
|
StringRef SectionName,
|
2012-11-16 07:50:01 +08:00
|
|
|
bool IsReadOnly) {
|
2015-11-24 05:47:51 +08:00
|
|
|
if (PrintAllocationRequests)
|
|
|
|
outs() << "allocateDataSection(Size = " << Size << ", Alignment = "
|
|
|
|
<< Alignment << ", SectionName = " << SectionName << ")\n";
|
|
|
|
|
2015-10-22 06:12:03 +08:00
|
|
|
if (UsePreallocation)
|
|
|
|
return allocateFromSlab(Size, Alignment, false /* isCode */);
|
|
|
|
|
2015-10-15 08:05:32 +08:00
|
|
|
std::string Err;
|
|
|
|
sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, &Err);
|
|
|
|
if (!MB.base())
|
|
|
|
report_fatal_error("MemoryManager allocation failed: " + Err);
|
2012-05-17 02:50:11 +08:00
|
|
|
DataMemory.push_back(MB);
|
|
|
|
return (uint8_t*)MB.base();
|
|
|
|
}
|
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
static const char *ProgramName;
|
|
|
|
|
2016-04-06 04:11:24 +08:00
|
|
|
static void ErrorAndExit(const Twine &Msg) {
|
2015-11-21 07:12:15 +08:00
|
|
|
errs() << ProgramName << ": error: " << Msg << "\n";
|
2016-03-26 01:25:34 +08:00
|
|
|
exit(1);
|
2011-03-19 01:11:39 +08:00
|
|
|
}
|
|
|
|
|
2014-05-14 06:37:41 +08:00
|
|
|
static void loadDylibs() {
|
|
|
|
for (const std::string &Dylib : Dylibs) {
|
2015-11-22 09:58:33 +08:00
|
|
|
if (!sys::fs::is_regular_file(Dylib))
|
2015-11-21 13:58:19 +08:00
|
|
|
report_fatal_error("Dylib not found: '" + Dylib + "'.");
|
2015-11-22 09:58:33 +08:00
|
|
|
std::string ErrMsg;
|
|
|
|
if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
|
|
|
|
report_fatal_error("Error loading '" + Dylib + "': " + ErrMsg);
|
2014-05-14 06:37:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
/* *** */
|
2011-03-19 02:54:32 +08:00
|
|
|
|
2015-05-22 05:24:32 +08:00
|
|
|
static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
|
|
|
|
assert(LoadObjects || !UseDebugObj);
|
|
|
|
|
2014-05-14 06:37:41 +08:00
|
|
|
// Load any dylibs requested on the command line.
|
|
|
|
loadDylibs();
|
|
|
|
|
2013-01-26 06:50:58 +08:00
|
|
|
// If we don't have any input files, read from stdin.
|
|
|
|
if (!InputFileList.size())
|
|
|
|
InputFileList.push_back("-");
|
2015-10-12 08:57:29 +08:00
|
|
|
for (auto &File : InputFileList) {
|
2013-01-26 06:50:58 +08:00
|
|
|
// Instantiate a dynamic linker.
|
2013-08-04 06:16:31 +08:00
|
|
|
TrivialMemoryManager MemMgr;
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
RuntimeDyld Dyld(MemMgr, MemMgr);
|
2013-01-26 06:50:58 +08:00
|
|
|
|
|
|
|
// Load the input memory buffer.
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
|
2015-10-12 08:57:29 +08:00
|
|
|
MemoryBuffer::getFileOrSTDIN(File);
|
2014-07-07 01:43:13 +08:00
|
|
|
if (std::error_code EC = InputBuffer.getError())
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("unable to read input: '" + EC.message() + "'");
|
2014-07-07 01:43:13 +08:00
|
|
|
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<std::unique_ptr<ObjectFile>> MaybeObj(
|
2014-11-27 00:54:40 +08:00
|
|
|
ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
|
|
|
|
|
2016-04-07 06:14:09 +08:00
|
|
|
if (!MaybeObj) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
|
|
|
logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
|
|
|
|
OS.flush();
|
|
|
|
ErrorAndExit("unable to create object file: '" + Buf + "'");
|
|
|
|
}
|
2014-11-27 00:54:40 +08:00
|
|
|
|
|
|
|
ObjectFile &Obj = **MaybeObj;
|
|
|
|
|
2015-05-22 05:24:32 +08:00
|
|
|
OwningBinary<ObjectFile> DebugObj;
|
|
|
|
std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo = nullptr;
|
|
|
|
ObjectFile *SymbolObj = &Obj;
|
|
|
|
if (LoadObjects) {
|
|
|
|
// Load the object file
|
|
|
|
LoadedObjInfo =
|
|
|
|
Dyld.loadObject(Obj);
|
2014-11-27 00:54:40 +08:00
|
|
|
|
2015-05-22 05:24:32 +08:00
|
|
|
if (Dyld.hasError())
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit(Dyld.getErrorString());
|
2013-01-26 06:50:58 +08:00
|
|
|
|
2015-05-22 05:24:32 +08:00
|
|
|
// Resolve all the relocations we can.
|
|
|
|
Dyld.resolveRelocations();
|
2013-01-26 06:50:58 +08:00
|
|
|
|
2015-05-22 05:24:32 +08:00
|
|
|
if (UseDebugObj) {
|
|
|
|
DebugObj = LoadedObjInfo->getObjectForDebug(Obj);
|
|
|
|
SymbolObj = DebugObj.getBinary();
|
2015-07-29 04:51:53 +08:00
|
|
|
LoadedObjInfo.reset();
|
2015-05-22 05:24:32 +08:00
|
|
|
}
|
|
|
|
}
|
2014-11-27 00:54:40 +08:00
|
|
|
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<DIContext> Context(
|
2015-05-22 05:24:32 +08:00
|
|
|
new DWARFContextInMemory(*SymbolObj,LoadedObjInfo.get()));
|
2013-01-26 06:50:58 +08:00
|
|
|
|
2015-06-25 03:57:32 +08:00
|
|
|
std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
|
2015-06-23 10:08:48 +08:00
|
|
|
object::computeSymbolSizes(*SymbolObj);
|
2015-06-01 07:15:35 +08:00
|
|
|
|
2013-01-26 06:50:58 +08:00
|
|
|
// Use symbol info to iterate functions in the object.
|
2015-06-25 03:57:32 +08:00
|
|
|
for (const auto &P : SymAddr) {
|
2015-06-23 10:08:48 +08:00
|
|
|
object::SymbolRef Sym = P.first;
|
2016-05-03 04:28:12 +08:00
|
|
|
Expected<SymbolRef::Type> TypeOrErr = Sym.getType();
|
|
|
|
if (!TypeOrErr) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(TypeOrErr.takeError());
|
2016-03-24 04:27:00 +08:00
|
|
|
continue;
|
2016-05-03 04:28:12 +08:00
|
|
|
}
|
2016-03-24 04:27:00 +08:00
|
|
|
SymbolRef::Type Type = *TypeOrErr;
|
|
|
|
if (Type == object::SymbolRef::ST_Function) {
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> Name = Sym.getName();
|
|
|
|
if (!Name) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(Name.takeError());
|
2015-06-01 06:13:51 +08:00
|
|
|
continue;
|
2016-04-21 05:24:34 +08:00
|
|
|
}
|
2016-06-25 02:24:42 +08:00
|
|
|
Expected<uint64_t> AddrOrErr = Sym.getAddress();
|
|
|
|
if (!AddrOrErr) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(AddrOrErr.takeError());
|
2015-06-01 06:13:51 +08:00
|
|
|
continue;
|
2016-06-25 02:24:42 +08:00
|
|
|
}
|
2015-07-04 02:19:00 +08:00
|
|
|
uint64_t Addr = *AddrOrErr;
|
2015-06-01 07:15:35 +08:00
|
|
|
|
2015-06-23 10:08:48 +08:00
|
|
|
uint64_t Size = P.second;
|
2015-05-22 05:24:32 +08:00
|
|
|
// If we're not using the debug object, compute the address of the
|
|
|
|
// symbol in memory (rather than that in the unrelocated object file)
|
|
|
|
// and use that to query the DWARFContext.
|
|
|
|
if (!UseDebugObj && LoadObjects) {
|
2016-05-03 04:28:12 +08:00
|
|
|
auto SecOrErr = Sym.getSection();
|
|
|
|
if (!SecOrErr) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(SecOrErr.takeError());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
object::section_iterator Sec = *SecOrErr;
|
2015-05-22 05:24:32 +08:00
|
|
|
StringRef SecName;
|
|
|
|
Sec->getName(SecName);
|
|
|
|
uint64_t SectionLoadAddress =
|
2015-07-29 01:52:11 +08:00
|
|
|
LoadedObjInfo->getSectionLoadAddress(*Sec);
|
2015-05-22 05:24:32 +08:00
|
|
|
if (SectionLoadAddress != 0)
|
|
|
|
Addr += SectionLoadAddress - Sec->getAddress();
|
|
|
|
}
|
|
|
|
|
2015-07-03 04:55:21 +08:00
|
|
|
outs() << "Function: " << *Name << ", Size = " << Size
|
|
|
|
<< ", Addr = " << Addr << "\n";
|
2013-01-26 06:50:58 +08:00
|
|
|
|
2013-01-26 08:28:05 +08:00
|
|
|
DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
|
2015-10-12 08:57:29 +08:00
|
|
|
for (auto &D : Lines) {
|
|
|
|
outs() << " Line info @ " << D.first - Addr << ": "
|
|
|
|
<< D.second.FileName << ", line:" << D.second.Line << "\n";
|
2013-01-26 08:28:05 +08:00
|
|
|
}
|
2013-01-26 06:50:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-22 06:12:03 +08:00
|
|
|
static void doPreallocation(TrivialMemoryManager &MemMgr) {
|
|
|
|
// Allocate a slab of memory upfront, if required. This is used if
|
|
|
|
// we want to test small code models.
|
|
|
|
if (static_cast<intptr_t>(PreallocMemory) < 0)
|
|
|
|
report_fatal_error("Pre-allocated bytes of memory must be a positive integer.");
|
|
|
|
|
|
|
|
// FIXME: Limit the amount of memory that can be preallocated?
|
|
|
|
if (PreallocMemory != 0)
|
|
|
|
MemMgr.preallocateSlab(PreallocMemory);
|
|
|
|
}
|
|
|
|
|
2011-03-19 02:54:32 +08:00
|
|
|
static int executeInput() {
|
2014-05-14 06:37:41 +08:00
|
|
|
// Load any dylibs requested on the command line.
|
|
|
|
loadDylibs();
|
|
|
|
|
2011-03-22 06:15:52 +08:00
|
|
|
// Instantiate a dynamic linker.
|
2013-08-04 06:16:31 +08:00
|
|
|
TrivialMemoryManager MemMgr;
|
2015-10-22 06:12:03 +08:00
|
|
|
doPreallocation(MemMgr);
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
RuntimeDyld Dyld(MemMgr, MemMgr);
|
2011-03-19 02:54:32 +08:00
|
|
|
|
2011-04-13 23:49:40 +08:00
|
|
|
// If we don't have any input files, read from stdin.
|
|
|
|
if (!InputFileList.size())
|
|
|
|
InputFileList.push_back("-");
|
2015-10-12 08:57:29 +08:00
|
|
|
for (auto &File : InputFileList) {
|
2011-04-13 23:49:40 +08:00
|
|
|
// Load the input memory buffer.
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
|
2015-10-12 08:57:29 +08:00
|
|
|
MemoryBuffer::getFileOrSTDIN(File);
|
2014-07-07 01:43:13 +08:00
|
|
|
if (std::error_code EC = InputBuffer.getError())
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("unable to read input: '" + EC.message() + "'");
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<std::unique_ptr<ObjectFile>> MaybeObj(
|
2014-11-27 00:54:40 +08:00
|
|
|
ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
|
|
|
|
|
2016-04-07 06:14:09 +08:00
|
|
|
if (!MaybeObj) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
|
|
|
logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
|
|
|
|
OS.flush();
|
|
|
|
ErrorAndExit("unable to create object file: '" + Buf + "'");
|
|
|
|
}
|
2014-11-27 00:54:40 +08:00
|
|
|
|
|
|
|
ObjectFile &Obj = **MaybeObj;
|
|
|
|
|
2012-10-03 05:18:39 +08:00
|
|
|
// Load the object file
|
2014-11-27 00:54:40 +08:00
|
|
|
Dyld.loadObject(Obj);
|
|
|
|
if (Dyld.hasError()) {
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit(Dyld.getErrorString());
|
2011-04-13 23:49:40 +08:00
|
|
|
}
|
2011-03-23 02:19:42 +08:00
|
|
|
}
|
2011-04-13 23:49:40 +08:00
|
|
|
|
2015-11-18 00:37:52 +08:00
|
|
|
// Resove all the relocations we can.
|
2011-04-13 23:49:40 +08:00
|
|
|
// FIXME: Error out if there are unresolved relocations.
|
2015-11-18 00:37:52 +08:00
|
|
|
Dyld.resolveRelocations();
|
2011-04-13 23:49:40 +08:00
|
|
|
|
2011-04-13 23:38:30 +08:00
|
|
|
// Get the address of the entry point (_main by default).
|
2015-03-11 08:43:26 +08:00
|
|
|
void *MainAddress = Dyld.getSymbolLocalAddress(EntryPoint);
|
2014-04-25 12:24:47 +08:00
|
|
|
if (!MainAddress)
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("no definition for '" + EntryPoint + "'");
|
2011-03-19 01:11:39 +08:00
|
|
|
|
2011-04-12 08:23:32 +08:00
|
|
|
// Invalidate the instruction cache for each loaded function.
|
2015-10-12 08:57:29 +08:00
|
|
|
for (auto &FM : MemMgr.FunctionMemory) {
|
2015-11-18 00:37:52 +08:00
|
|
|
|
2011-04-12 08:23:32 +08:00
|
|
|
// Make sure the memory is executable.
|
2015-11-18 00:37:52 +08:00
|
|
|
// setExecutable will call InvalidateInstructionCache.
|
2011-04-12 08:23:32 +08:00
|
|
|
std::string ErrorStr;
|
2015-10-12 08:57:29 +08:00
|
|
|
if (!sys::Memory::setExecutable(FM, &ErrorStr))
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("unable to mark function executable: '" + ErrorStr + "'");
|
2011-04-12 08:23:32 +08:00
|
|
|
}
|
2011-03-19 01:11:39 +08:00
|
|
|
|
|
|
|
// Dispatch to _main().
|
2011-04-13 23:49:40 +08:00
|
|
|
errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
|
2011-03-19 01:11:39 +08:00
|
|
|
|
|
|
|
int (*Main)(int, const char**) =
|
|
|
|
(int(*)(int,const char**)) uintptr_t(MainAddress);
|
|
|
|
const char **Argv = new const char*[2];
|
2011-04-13 23:49:40 +08:00
|
|
|
// Use the name of the first input object module as argv[0] for the target.
|
|
|
|
Argv[0] = InputFileList[0].c_str();
|
2014-04-25 12:24:47 +08:00
|
|
|
Argv[1] = nullptr;
|
2011-03-19 01:11:39 +08:00
|
|
|
return Main(1, Argv);
|
|
|
|
}
|
|
|
|
|
2014-06-28 04:20:57 +08:00
|
|
|
static int checkAllExpressions(RuntimeDyldChecker &Checker) {
|
|
|
|
for (const auto& CheckerFileName : CheckFiles) {
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> CheckerFileBuf =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(CheckerFileName);
|
|
|
|
if (std::error_code EC = CheckerFileBuf.getError())
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("unable to read input '" + CheckerFileName + "': " +
|
2014-06-28 04:20:57 +08:00
|
|
|
EC.message());
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
|
|
|
|
CheckerFileBuf.get().get()))
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("some checks in '" + CheckerFileName + "' failed");
|
2014-06-28 04:20:57 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:23:46 +08:00
|
|
|
static std::map<void *, uint64_t>
|
2014-09-04 12:19:54 +08:00
|
|
|
applySpecificSectionMappings(RuntimeDyldChecker &Checker) {
|
|
|
|
|
|
|
|
std::map<void*, uint64_t> SpecificMappings;
|
|
|
|
|
|
|
|
for (StringRef Mapping : SpecificSectionMappings) {
|
|
|
|
|
|
|
|
size_t EqualsIdx = Mapping.find_first_of("=");
|
2015-07-04 09:35:26 +08:00
|
|
|
std::string SectionIDStr = Mapping.substr(0, EqualsIdx);
|
2014-09-04 12:19:54 +08:00
|
|
|
size_t ComaIdx = Mapping.find_first_of(",");
|
|
|
|
|
2015-11-21 10:15:51 +08:00
|
|
|
if (ComaIdx == StringRef::npos)
|
|
|
|
report_fatal_error("Invalid section specification '" + Mapping +
|
|
|
|
"'. Should be '<file name>,<section name>=<addr>'");
|
2014-09-04 12:19:54 +08:00
|
|
|
|
2015-07-04 09:35:26 +08:00
|
|
|
std::string FileName = SectionIDStr.substr(0, ComaIdx);
|
|
|
|
std::string SectionName = SectionIDStr.substr(ComaIdx + 1);
|
2014-09-04 12:19:54 +08:00
|
|
|
|
|
|
|
uint64_t OldAddrInt;
|
|
|
|
std::string ErrorMsg;
|
|
|
|
std::tie(OldAddrInt, ErrorMsg) =
|
|
|
|
Checker.getSectionAddr(FileName, SectionName, true);
|
|
|
|
|
2015-11-21 10:15:51 +08:00
|
|
|
if (ErrorMsg != "")
|
|
|
|
report_fatal_error(ErrorMsg);
|
2014-09-04 12:19:54 +08:00
|
|
|
|
|
|
|
void* OldAddr = reinterpret_cast<void*>(static_cast<uintptr_t>(OldAddrInt));
|
|
|
|
|
2015-07-04 09:35:26 +08:00
|
|
|
std::string NewAddrStr = Mapping.substr(EqualsIdx + 1);
|
2014-09-04 12:19:54 +08:00
|
|
|
uint64_t NewAddr;
|
|
|
|
|
2015-11-21 10:15:51 +08:00
|
|
|
if (StringRef(NewAddrStr).getAsInteger(0, NewAddr))
|
|
|
|
report_fatal_error("Invalid section address in mapping '" + Mapping +
|
|
|
|
"'.");
|
2014-09-04 12:19:54 +08:00
|
|
|
|
|
|
|
Checker.getRTDyld().mapSectionAddress(OldAddr, NewAddr);
|
|
|
|
SpecificMappings[OldAddr] = NewAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SpecificMappings;
|
|
|
|
}
|
|
|
|
|
2014-07-30 07:43:13 +08:00
|
|
|
// Scatter sections in all directions!
|
|
|
|
// Remaps section addresses for -verify mode. The following command line options
|
|
|
|
// can be used to customize the layout of the memory within the phony target's
|
|
|
|
// address space:
|
|
|
|
// -target-addr-start <s> -- Specify where the phony target addres range starts.
|
|
|
|
// -target-addr-end <e> -- Specify where the phony target address range ends.
|
|
|
|
// -target-section-sep <d> -- Specify how big a gap should be left between the
|
|
|
|
// end of one section and the start of the next.
|
|
|
|
// Defaults to zero. Set to something big
|
|
|
|
// (e.g. 1 << 32) to stress-test stubs, GOTs, etc.
|
|
|
|
//
|
2015-07-04 09:35:26 +08:00
|
|
|
static void remapSectionsAndSymbols(const llvm::Triple &TargetTriple,
|
|
|
|
TrivialMemoryManager &MemMgr,
|
|
|
|
RuntimeDyldChecker &Checker) {
|
2014-09-04 12:19:54 +08:00
|
|
|
|
|
|
|
// Set up a work list (section addr/size pairs).
|
|
|
|
typedef std::list<std::pair<void*, uint64_t>> WorklistT;
|
|
|
|
WorklistT Worklist;
|
|
|
|
|
|
|
|
for (const auto& CodeSection : MemMgr.FunctionMemory)
|
|
|
|
Worklist.push_back(std::make_pair(CodeSection.base(), CodeSection.size()));
|
|
|
|
for (const auto& DataSection : MemMgr.DataMemory)
|
|
|
|
Worklist.push_back(std::make_pair(DataSection.base(), DataSection.size()));
|
|
|
|
|
|
|
|
// Apply any section-specific mappings that were requested on the command
|
|
|
|
// line.
|
|
|
|
typedef std::map<void*, uint64_t> AppliedMappingsT;
|
|
|
|
AppliedMappingsT AppliedMappings = applySpecificSectionMappings(Checker);
|
|
|
|
|
|
|
|
// Keep an "already allocated" mapping of section target addresses to sizes.
|
|
|
|
// Sections whose address mappings aren't specified on the command line will
|
|
|
|
// allocated around the explicitly mapped sections while maintaining the
|
|
|
|
// minimum separation.
|
|
|
|
std::map<uint64_t, uint64_t> AlreadyAllocated;
|
|
|
|
|
|
|
|
// Move the previously applied mappings into the already-allocated map.
|
|
|
|
for (WorklistT::iterator I = Worklist.begin(), E = Worklist.end();
|
|
|
|
I != E;) {
|
|
|
|
WorklistT::iterator Tmp = I;
|
|
|
|
++I;
|
|
|
|
AppliedMappingsT::iterator AI = AppliedMappings.find(Tmp->first);
|
|
|
|
|
|
|
|
if (AI != AppliedMappings.end()) {
|
|
|
|
AlreadyAllocated[AI->second] = Tmp->second;
|
|
|
|
Worklist.erase(Tmp);
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 07:43:13 +08:00
|
|
|
|
|
|
|
// If the -target-addr-end option wasn't explicitly passed, then set it to a
|
|
|
|
// sensible default based on the target triple.
|
|
|
|
if (TargetAddrEnd.getNumOccurrences() == 0) {
|
|
|
|
if (TargetTriple.isArch16Bit())
|
|
|
|
TargetAddrEnd = (1ULL << 16) - 1;
|
|
|
|
else if (TargetTriple.isArch32Bit())
|
|
|
|
TargetAddrEnd = (1ULL << 32) - 1;
|
|
|
|
// TargetAddrEnd already has a sensible default for 64-bit systems, so
|
|
|
|
// there's nothing to do in the 64-bit case.
|
|
|
|
}
|
|
|
|
|
2014-09-04 12:19:54 +08:00
|
|
|
// Process any elements remaining in the worklist.
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
std::pair<void*, uint64_t> CurEntry = Worklist.front();
|
|
|
|
Worklist.pop_front();
|
2014-07-30 07:43:13 +08:00
|
|
|
|
2014-09-04 12:19:54 +08:00
|
|
|
uint64_t NextSectionAddr = TargetAddrStart;
|
|
|
|
|
|
|
|
for (const auto &Alloc : AlreadyAllocated)
|
|
|
|
if (NextSectionAddr + CurEntry.second + TargetSectionSep <= Alloc.first)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
NextSectionAddr = Alloc.first + Alloc.second + TargetSectionSep;
|
2014-07-30 07:43:13 +08:00
|
|
|
|
2014-09-04 12:19:54 +08:00
|
|
|
AlreadyAllocated[NextSectionAddr] = CurEntry.second;
|
|
|
|
Checker.getRTDyld().mapSectionAddress(CurEntry.first, NextSectionAddr);
|
2014-07-30 07:43:13 +08:00
|
|
|
}
|
2014-09-04 12:19:54 +08:00
|
|
|
|
2015-07-04 09:35:26 +08:00
|
|
|
// Add dummy symbols to the memory manager.
|
|
|
|
for (const auto &Mapping : DummySymbolMappings) {
|
|
|
|
size_t EqualsIdx = Mapping.find_first_of("=");
|
|
|
|
|
2015-11-21 10:15:51 +08:00
|
|
|
if (EqualsIdx == StringRef::npos)
|
|
|
|
report_fatal_error("Invalid dummy symbol specification '" + Mapping +
|
|
|
|
"'. Should be '<symbol name>=<addr>'");
|
2015-07-04 09:35:26 +08:00
|
|
|
|
|
|
|
std::string Symbol = Mapping.substr(0, EqualsIdx);
|
|
|
|
std::string AddrStr = Mapping.substr(EqualsIdx + 1);
|
|
|
|
|
|
|
|
uint64_t Addr;
|
2015-11-21 10:15:51 +08:00
|
|
|
if (StringRef(AddrStr).getAsInteger(0, Addr))
|
|
|
|
report_fatal_error("Invalid symbol mapping '" + Mapping + "'.");
|
2015-07-04 09:35:26 +08:00
|
|
|
|
|
|
|
MemMgr.addDummySymbol(Symbol, Addr);
|
|
|
|
}
|
2014-07-30 07:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load and link the objects specified on the command line, but do not execute
|
|
|
|
// anything. Instead, attach a RuntimeDyldChecker instance and call it to
|
|
|
|
// verify the correctness of the linked memory.
|
2014-06-28 04:20:57 +08:00
|
|
|
static int linkAndVerify() {
|
|
|
|
|
|
|
|
// Check for missing triple.
|
2015-11-21 13:44:41 +08:00
|
|
|
if (TripleName == "")
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("-triple required when running in -verify mode.");
|
2014-06-28 04:20:57 +08:00
|
|
|
|
|
|
|
// Look up the target and build the disassembler.
|
|
|
|
Triple TheTriple(Triple::normalize(TripleName));
|
|
|
|
std::string ErrorStr;
|
|
|
|
const Target *TheTarget =
|
|
|
|
TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
|
2015-11-21 13:44:41 +08:00
|
|
|
if (!TheTarget)
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("Error accessing target '" + TripleName + "': " + ErrorStr);
|
2015-11-21 13:44:41 +08:00
|
|
|
|
2014-06-28 04:20:57 +08:00
|
|
|
TripleName = TheTriple.getTriple();
|
|
|
|
|
|
|
|
std::unique_ptr<MCSubtargetInfo> STI(
|
2015-06-24 06:52:19 +08:00
|
|
|
TheTarget->createMCSubtargetInfo(TripleName, MCPU, ""));
|
2015-11-21 13:49:07 +08:00
|
|
|
if (!STI)
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("Unable to create subtarget info!");
|
2014-06-28 04:20:57 +08:00
|
|
|
|
|
|
|
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
|
2015-11-21 13:49:07 +08:00
|
|
|
if (!MRI)
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("Unable to create target register info!");
|
2014-06-28 04:20:57 +08:00
|
|
|
|
|
|
|
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
|
2015-11-21 13:49:07 +08:00
|
|
|
if (!MAI)
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("Unable to create target asm info!");
|
2014-06-28 04:20:57 +08:00
|
|
|
|
|
|
|
MCContext Ctx(MAI.get(), MRI.get(), nullptr);
|
|
|
|
|
|
|
|
std::unique_ptr<MCDisassembler> Disassembler(
|
|
|
|
TheTarget->createMCDisassembler(*STI, Ctx));
|
2015-11-21 13:49:07 +08:00
|
|
|
if (!Disassembler)
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("Unable to create disassembler!");
|
2014-06-28 04:20:57 +08:00
|
|
|
|
|
|
|
std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
|
|
|
|
|
2015-09-16 00:17:27 +08:00
|
|
|
std::unique_ptr<MCInstPrinter> InstPrinter(
|
|
|
|
TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
|
2014-06-28 04:20:57 +08:00
|
|
|
|
|
|
|
// Load any dylibs requested on the command line.
|
|
|
|
loadDylibs();
|
|
|
|
|
|
|
|
// Instantiate a dynamic linker.
|
|
|
|
TrivialMemoryManager MemMgr;
|
2015-10-22 06:12:03 +08:00
|
|
|
doPreallocation(MemMgr);
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
RuntimeDyld Dyld(MemMgr, MemMgr);
|
2014-09-03 13:42:52 +08:00
|
|
|
Dyld.setProcessAllSections(true);
|
[MCJIT] Refactor and add stub inspection to the RuntimeDyldChecker framework.
This patch introduces a 'stub_addr' builtin that can be used to find the address
of the stub for a given (<file>, <section>, <symbol>) tuple. This address can be
used both to verify the contents of stubs (by loading from the returned address)
and to verify references to stubs (by comparing against the returned address).
Example (1) - Verifying stub contents:
Load 8 bytes (assuming a 64-bit target) from the stub for 'x' in the __text
section of f.o, and compare that value against the addres of 'x'.
# rtdyld-check: *{8}(stub_addr(f.o, __text, x) = x
Example (2) - Verifying references to stubs:
Decode the immediate of the instruction at label 'l', and verify that it's
equal to the offset from the next instruction's PC to the stub for 'y' in the
__text section of f.o (i.e. it's the correct PC-rel difference).
# rtdyld-check: decode_operand(l, 4) = stub_addr(f.o, __text, y) - next_pc(l)
l:
movq y@GOTPCREL(%rip), %rax
Since stub inspection requires cooperation with RuntimeDyldImpl this patch
pimpl-ifies RuntimeDyldChecker. Its implementation is moved in to a new class,
RuntimeDyldCheckerImpl, that has access to the definition of RuntimeDyldImpl.
llvm-svn: 213698
2014-07-23 06:47:39 +08:00
|
|
|
RuntimeDyldChecker Checker(Dyld, Disassembler.get(), InstPrinter.get(),
|
|
|
|
llvm::dbgs());
|
2014-06-28 04:20:57 +08:00
|
|
|
|
|
|
|
// If we don't have any input files, read from stdin.
|
|
|
|
if (!InputFileList.size())
|
|
|
|
InputFileList.push_back("-");
|
2015-10-10 08:45:24 +08:00
|
|
|
for (auto &Filename : InputFileList) {
|
2014-06-28 04:20:57 +08:00
|
|
|
// Load the input memory buffer.
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
|
2015-10-10 08:45:24 +08:00
|
|
|
MemoryBuffer::getFileOrSTDIN(Filename);
|
2014-11-27 00:54:40 +08:00
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
if (std::error_code EC = InputBuffer.getError())
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("unable to read input: '" + EC.message() + "'");
|
2014-06-28 04:20:57 +08:00
|
|
|
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<std::unique_ptr<ObjectFile>> MaybeObj(
|
2014-11-27 00:54:40 +08:00
|
|
|
ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
|
|
|
|
|
2016-04-07 06:14:09 +08:00
|
|
|
if (!MaybeObj) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
|
|
|
logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
|
|
|
|
OS.flush();
|
|
|
|
ErrorAndExit("unable to create object file: '" + Buf + "'");
|
|
|
|
}
|
2014-11-27 00:54:40 +08:00
|
|
|
|
|
|
|
ObjectFile &Obj = **MaybeObj;
|
|
|
|
|
2014-06-28 04:20:57 +08:00
|
|
|
// Load the object file
|
2014-11-27 00:54:40 +08:00
|
|
|
Dyld.loadObject(Obj);
|
|
|
|
if (Dyld.hasError()) {
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit(Dyld.getErrorString());
|
2014-06-28 04:20:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-04 09:35:26 +08:00
|
|
|
// Re-map the section addresses into the phony target address space and add
|
|
|
|
// dummy symbols.
|
|
|
|
remapSectionsAndSymbols(TheTriple, MemMgr, Checker);
|
2014-07-30 11:12:41 +08:00
|
|
|
|
2014-06-28 04:20:57 +08:00
|
|
|
// Resolve all the relocations we can.
|
|
|
|
Dyld.resolveRelocations();
|
|
|
|
|
2014-09-03 13:42:52 +08:00
|
|
|
// Register EH frames.
|
|
|
|
Dyld.registerEHFrames();
|
|
|
|
|
2014-08-06 04:51:46 +08:00
|
|
|
int ErrorCode = checkAllExpressions(Checker);
|
2015-11-21 13:44:41 +08:00
|
|
|
if (Dyld.hasError())
|
2016-03-26 01:25:34 +08:00
|
|
|
ErrorAndExit("RTDyld reported an error applying relocations:\n " +
|
2015-11-21 13:44:41 +08:00
|
|
|
Dyld.getErrorString());
|
2014-08-06 04:51:46 +08:00
|
|
|
|
|
|
|
return ErrorCode;
|
2014-06-28 04:20:57 +08:00
|
|
|
}
|
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
int main(int argc, char **argv) {
|
2016-06-09 08:53:21 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2013-11-05 17:33:43 +08:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
ProgramName = argv[0];
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
2014-06-28 04:20:57 +08:00
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllDisassemblers();
|
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n");
|
|
|
|
|
|
|
|
switch (Action) {
|
|
|
|
case AC_Execute:
|
2011-03-19 01:24:21 +08:00
|
|
|
return executeInput();
|
2015-05-22 05:24:32 +08:00
|
|
|
case AC_PrintDebugLineInfo:
|
2015-05-31 03:44:53 +08:00
|
|
|
return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */ true);
|
2013-01-26 06:50:58 +08:00
|
|
|
case AC_PrintLineInfo:
|
2015-05-31 03:44:53 +08:00
|
|
|
return printLineInfoForInput(/* LoadObjects */ true,/* UseDebugObj */false);
|
|
|
|
case AC_PrintObjectLineInfo:
|
|
|
|
return printLineInfoForInput(/* LoadObjects */false,/* UseDebugObj */false);
|
2014-06-28 04:20:57 +08:00
|
|
|
case AC_Verify:
|
|
|
|
return linkAndVerify();
|
2011-03-19 01:11:39 +08:00
|
|
|
}
|
|
|
|
}
|