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"
|
|
|
|
#include "llvm/MC/MCDisassembler.h"
|
|
|
|
#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);
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2012-05-17 02:50:11 +08:00
|
|
|
// Invalidate instruction cache for sections with execute permissions.
|
|
|
|
// Some platforms with separate data cache and instruction cache require
|
|
|
|
// explicit cache flush, otherwise JIT code manipulations (like resolved
|
|
|
|
// relocations) will get to the data cache but not to the instruction cache.
|
|
|
|
virtual void invalidateInstructionCache();
|
2015-06-04 02:26:52 +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-07-04 09:35:26 +08:00
|
|
|
private:
|
|
|
|
std::map<std::string, uint64_t> DummyExterns;
|
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) {
|
2014-04-25 12:24:47 +08:00
|
|
|
sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
|
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) {
|
2014-04-25 12:24:47 +08:00
|
|
|
sys::MemoryBlock MB = sys::Memory::AllocateRWX(Size, nullptr, nullptr);
|
2012-05-17 02:50:11 +08:00
|
|
|
DataMemory.push_back(MB);
|
|
|
|
return (uint8_t*)MB.base();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrivialMemoryManager::invalidateInstructionCache() {
|
|
|
|
for (int i = 0, e = FunctionMemory.size(); i != e; ++i)
|
|
|
|
sys::Memory::InvalidateInstructionCache(FunctionMemory[i].base(),
|
|
|
|
FunctionMemory[i].size());
|
|
|
|
|
|
|
|
for (int i = 0, e = DataMemory.size(); i != e; ++i)
|
|
|
|
sys::Memory::InvalidateInstructionCache(DataMemory[i].base(),
|
|
|
|
DataMemory[i].size());
|
2012-01-17 06:26:39 +08:00
|
|
|
}
|
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
static const char *ProgramName;
|
|
|
|
|
|
|
|
static void Message(const char *Type, const Twine &Msg) {
|
|
|
|
errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static int Error(const Twine &Msg) {
|
|
|
|
Message("error", Msg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-05-14 06:37:41 +08:00
|
|
|
static void loadDylibs() {
|
|
|
|
for (const std::string &Dylib : Dylibs) {
|
|
|
|
if (sys::fs::is_regular_file(Dylib)) {
|
|
|
|
std::string ErrMsg;
|
|
|
|
if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
|
|
|
|
llvm::errs() << "Error loading '" << Dylib << "': "
|
|
|
|
<< ErrMsg << "\n";
|
|
|
|
} else
|
|
|
|
llvm::errs() << "Dylib not found: '" << Dylib << "'.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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("-");
|
|
|
|
for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
|
|
|
|
// 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 =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
|
|
|
|
if (std::error_code EC = InputBuffer.getError())
|
|
|
|
return Error("unable to read input: '" + EC.message() + "'");
|
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
|
|
|
|
ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
|
|
|
|
|
|
|
|
if (std::error_code EC = MaybeObj.getError())
|
|
|
|
return Error("unable to create object file: '" + EC.message() + "'");
|
|
|
|
|
|
|
|
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())
|
|
|
|
return Error(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;
|
2015-06-26 20:18:49 +08:00
|
|
|
if (Sym.getType() == object::SymbolRef::ST_Function) {
|
2015-07-03 04:55:21 +08:00
|
|
|
ErrorOr<StringRef> Name = Sym.getName();
|
|
|
|
if (!Name)
|
2015-06-01 06:13:51 +08:00
|
|
|
continue;
|
2015-07-04 02:19:00 +08:00
|
|
|
ErrorOr<uint64_t> AddrOrErr = Sym.getAddress();
|
|
|
|
if (!AddrOrErr)
|
2015-06-01 06:13:51 +08:00
|
|
|
continue;
|
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) {
|
|
|
|
object::section_iterator Sec(SymbolObj->section_end());
|
2015-06-01 06:13:51 +08:00
|
|
|
Sym.getSection(Sec);
|
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);
|
|
|
|
DILineInfoTable::iterator Begin = Lines.begin();
|
|
|
|
DILineInfoTable::iterator End = Lines.end();
|
|
|
|
for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
|
|
|
|
outs() << " Line info @ " << It->first - Addr << ": "
|
2014-04-19 05:36:39 +08:00
|
|
|
<< It->second.FileName << ", line:" << It->second.Line << "\n";
|
2013-01-26 08:28:05 +08:00
|
|
|
}
|
2013-01-26 06:50:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
[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
|
|
|
|
2015-04-16 05:18:41 +08:00
|
|
|
// FIXME: Preserve buffers until resolveRelocations time to work around a bug
|
|
|
|
// in RuntimeDyldELF.
|
|
|
|
// This fixme should be fixed ASAP. This is a very brittle workaround.
|
|
|
|
std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers;
|
|
|
|
|
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("-");
|
|
|
|
for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
|
|
|
|
// Load the input memory buffer.
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
|
|
|
|
if (std::error_code EC = InputBuffer.getError())
|
|
|
|
return Error("unable to read input: '" + EC.message() + "'");
|
2014-11-27 00:54:40 +08:00
|
|
|
ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
|
|
|
|
ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
|
|
|
|
|
|
|
|
if (std::error_code EC = MaybeObj.getError())
|
|
|
|
return Error("unable to create object file: '" + EC.message() + "'");
|
|
|
|
|
|
|
|
ObjectFile &Obj = **MaybeObj;
|
2015-04-16 05:18:41 +08:00
|
|
|
InputBuffers.push_back(std::move(*InputBuffer));
|
2014-11-27 00:54:40 +08:00
|
|
|
|
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()) {
|
2011-04-13 23:49:40 +08:00
|
|
|
return Error(Dyld.getErrorString());
|
|
|
|
}
|
2011-03-23 02:19:42 +08:00
|
|
|
}
|
2011-04-13 23:49:40 +08:00
|
|
|
|
MCJIT lazy relocation resolution and symbol address re-assignment.
Add handling for tracking the relocations on symbols and resolving them.
Keep track of the relocations even after they are resolved so that if
the RuntimeDyld client moves the object, it can update the address and any
relocations to that object will be updated.
For our trival object file load/run test harness (llvm-rtdyld), this enables
relocations between functions located in the same object module. It should
be trivially extendable to load multiple objects with mutual references.
As a simple example, the following now works (running on x86_64 Darwin 10.6):
$ cat t.c
int bar() {
return 65;
}
int main() {
return bar();
}
$ clang t.c -fno-asynchronous-unwind-tables -o t.o -c
$ otool -vt t.o
t.o:
(__TEXT,__text) section
_bar:
0000000000000000 pushq %rbp
0000000000000001 movq %rsp,%rbp
0000000000000004 movl $0x00000041,%eax
0000000000000009 popq %rbp
000000000000000a ret
000000000000000b nopl 0x00(%rax,%rax)
_main:
0000000000000010 pushq %rbp
0000000000000011 movq %rsp,%rbp
0000000000000014 subq $0x10,%rsp
0000000000000018 movl $0x00000000,0xfc(%rbp)
000000000000001f callq 0x00000024
0000000000000024 addq $0x10,%rsp
0000000000000028 popq %rbp
0000000000000029 ret
$ llvm-rtdyld t.o -debug-only=dyld ; echo $?
Function sym: '_bar' @ 0
Function sym: '_main' @ 16
Extracting function: _bar from [0, 15]
allocated to 0x100153000
Extracting function: _main from [16, 41]
allocated to 0x100154000
Relocation at '_main' + 16 from '_bar(Word1: 0x2d000000)
Resolving relocation at '_main' + 16 (0x100154010) from '_bar (0x100153000)(pcrel, type: 2, Size: 4).
loaded '_main' at: 0x100154000
65
$
llvm-svn: 129388
2011-04-13 05:20:41 +08:00
|
|
|
// Resolve all the relocations we can.
|
|
|
|
Dyld.resolveRelocations();
|
2012-05-17 02:50:11 +08:00
|
|
|
// Clear instruction cache before code will be executed.
|
2013-08-04 06:18:45 +08:00
|
|
|
MemMgr.invalidateInstructionCache();
|
2011-03-19 02:54:32 +08:00
|
|
|
|
2011-04-13 23:49:40 +08:00
|
|
|
// FIXME: Error out if there are unresolved relocations.
|
|
|
|
|
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)
|
2011-04-13 23:38:30 +08:00
|
|
|
return Error("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.
|
2013-08-04 06:18:45 +08:00
|
|
|
for (unsigned i = 0, e = MemMgr.FunctionMemory.size(); i != e; ++i) {
|
|
|
|
sys::MemoryBlock &Data = MemMgr.FunctionMemory[i];
|
2011-04-12 08:23:32 +08:00
|
|
|
// Make sure the memory is executable.
|
|
|
|
std::string ErrorStr;
|
|
|
|
sys::Memory::InvalidateInstructionCache(Data.base(), Data.size());
|
|
|
|
if (!sys::Memory::setExecutable(Data, &ErrorStr))
|
|
|
|
return Error("unable to mark function executable: '" + ErrorStr + "'");
|
|
|
|
}
|
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())
|
2014-06-28 04:20:57 +08:00
|
|
|
return Error("unable to read input '" + CheckerFileName + "': " +
|
|
|
|
EC.message());
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
if (!Checker.checkAllRulesInBuffer("# rtdyld-check:",
|
|
|
|
CheckerFileBuf.get().get()))
|
2014-06-28 04:20:57 +08:00
|
|
|
return Error("some checks in '" + CheckerFileName + "' failed");
|
|
|
|
}
|
|
|
|
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(",");
|
|
|
|
|
|
|
|
if (ComaIdx == StringRef::npos) {
|
|
|
|
errs() << "Invalid section specification '" << Mapping
|
|
|
|
<< "'. Should be '<file name>,<section name>=<addr>'\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (ErrorMsg != "") {
|
|
|
|
errs() << ErrorMsg;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-04 09:35:26 +08:00
|
|
|
if (StringRef(NewAddrStr).getAsInteger(0, NewAddr)) {
|
|
|
|
errs() << "Invalid section address in mapping '" << Mapping << "'.\n";
|
2014-09-04 12:19:54 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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("=");
|
|
|
|
|
|
|
|
if (EqualsIdx == StringRef::npos) {
|
|
|
|
errs() << "Invalid dummy symbol specification '" << Mapping
|
|
|
|
<< "'. Should be '<symbol name>=<addr>'\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Symbol = Mapping.substr(0, EqualsIdx);
|
|
|
|
std::string AddrStr = Mapping.substr(EqualsIdx + 1);
|
|
|
|
|
|
|
|
uint64_t Addr;
|
|
|
|
if (StringRef(AddrStr).getAsInteger(0, Addr)) {
|
|
|
|
errs() << "Invalid symbol mapping '" << Mapping << "'.\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (TripleName == "") {
|
|
|
|
llvm::errs() << "Error: -triple required when running in -verify mode.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the target and build the disassembler.
|
|
|
|
Triple TheTriple(Triple::normalize(TripleName));
|
|
|
|
std::string ErrorStr;
|
|
|
|
const Target *TheTarget =
|
|
|
|
TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
|
|
|
|
if (!TheTarget) {
|
|
|
|
llvm::errs() << "Error accessing target '" << TripleName << "': "
|
|
|
|
<< ErrorStr << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
TripleName = TheTriple.getTriple();
|
|
|
|
|
|
|
|
std::unique_ptr<MCSubtargetInfo> STI(
|
2015-06-24 06:52:19 +08:00
|
|
|
TheTarget->createMCSubtargetInfo(TripleName, MCPU, ""));
|
2014-06-28 04:20:57 +08:00
|
|
|
assert(STI && "Unable to create subtarget info!");
|
|
|
|
|
|
|
|
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
|
|
|
|
assert(MRI && "Unable to create target register info!");
|
|
|
|
|
|
|
|
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
|
|
|
|
assert(MAI && "Unable to create target asm info!");
|
|
|
|
|
|
|
|
MCContext Ctx(MAI.get(), MRI.get(), nullptr);
|
|
|
|
|
|
|
|
std::unique_ptr<MCDisassembler> Disassembler(
|
|
|
|
TheTarget->createMCDisassembler(*STI, Ctx));
|
|
|
|
assert(Disassembler && "Unable to create disassembler!");
|
|
|
|
|
|
|
|
std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
|
|
|
|
|
|
|
|
std::unique_ptr<MCInstPrinter> InstPrinter(
|
2015-03-31 08:10:04 +08:00
|
|
|
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;
|
[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
|
|
|
|
2015-04-16 05:18:41 +08:00
|
|
|
// FIXME: Preserve buffers until resolveRelocations time to work around a bug
|
|
|
|
// in RuntimeDyldELF.
|
|
|
|
// This fixme should be fixed ASAP. This is a very brittle workaround.
|
|
|
|
std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers;
|
|
|
|
|
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("-");
|
|
|
|
for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
|
|
|
|
// Load the input memory buffer.
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
|
2014-11-27 00:54:40 +08:00
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
if (std::error_code EC = InputBuffer.getError())
|
|
|
|
return Error("unable to read input: '" + EC.message() + "'");
|
2014-06-28 04:20:57 +08:00
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
|
|
|
|
ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
|
|
|
|
|
|
|
|
if (std::error_code EC = MaybeObj.getError())
|
|
|
|
return Error("unable to create object file: '" + EC.message() + "'");
|
|
|
|
|
|
|
|
ObjectFile &Obj = **MaybeObj;
|
2015-04-16 05:18:41 +08:00
|
|
|
InputBuffers.push_back(std::move(*InputBuffer));
|
2014-11-27 00:54:40 +08:00
|
|
|
|
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()) {
|
2014-06-28 04:20:57 +08:00
|
|
|
return Error(Dyld.getErrorString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if (Dyld.hasError()) {
|
|
|
|
errs() << "RTDyld reported an error applying relocations:\n "
|
|
|
|
<< Dyld.getErrorString() << "\n";
|
|
|
|
ErrorCode = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrorCode;
|
2014-06-28 04:20:57 +08:00
|
|
|
}
|
|
|
|
|
2011-03-19 01:11:39 +08:00
|
|
|
int main(int argc, char **argv) {
|
2013-11-05 17:33:43 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|