aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ExecutionEngine
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ExecutionEngine')
-rw-r--r--lib/ExecutionEngine/ExecutionEngine.cpp42
-rw-r--r--lib/ExecutionEngine/ExecutionEngineBindings.cpp2
-rw-r--r--lib/ExecutionEngine/Interpreter/Execution.cpp25
-rw-r--r--lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp24
-rw-r--r--lib/ExecutionEngine/Interpreter/Interpreter.h7
-rw-r--r--lib/ExecutionEngine/JIT/CMakeLists.txt1
-rw-r--r--lib/ExecutionEngine/JIT/JIT.cpp6
-rw-r--r--lib/ExecutionEngine/JIT/JIT.h4
-rw-r--r--lib/ExecutionEngine/JIT/JITDebugRegisterer.cpp211
-rw-r--r--lib/ExecutionEngine/JIT/JITDebugRegisterer.h116
-rw-r--r--lib/ExecutionEngine/JIT/JITEmitter.cpp37
-rw-r--r--lib/ExecutionEngine/JIT/JITMemoryManager.cpp44
-rw-r--r--lib/ExecutionEngine/MCJIT/CMakeLists.txt1
-rw-r--r--lib/ExecutionEngine/MCJIT/MCJIT.cpp6
-rw-r--r--lib/ExecutionEngine/MCJIT/MCJIT.h12
-rw-r--r--lib/ExecutionEngine/MCJIT/MCJITMemoryManager.cpp14
-rw-r--r--lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h11
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt1
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h388
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp76
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp308
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h178
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h93
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp522
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h94
25 files changed, 1491 insertions, 732 deletions
diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp
index 7829a29..2890174 100644
--- a/lib/ExecutionEngine/ExecutionEngine.cpp
+++ b/lib/ExecutionEngine/ExecutionEngine.cpp
@@ -307,13 +307,12 @@ void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
// Should be an array of '{ i32, void ()* }' structs. The first value is
// the init priority, which we ignore.
- if (isa<ConstantAggregateZero>(GV->getInitializer()))
+ ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
+ if (InitList == 0)
return;
- ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer());
for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
- if (isa<ConstantAggregateZero>(InitList->getOperand(i)))
- continue;
- ConstantStruct *CS = cast<ConstantStruct>(InitList->getOperand(i));
+ ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
+ if (CS == 0) continue;
Constant *FP = CS->getOperand(1);
if (FP->isNullValue())
@@ -954,30 +953,47 @@ void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
DEBUG(Init->dump());
- if (isa<UndefValue>(Init)) {
+ if (isa<UndefValue>(Init))
return;
- } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
+
+ if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
unsigned ElementSize =
getTargetData()->getTypeAllocSize(CP->getType()->getElementType());
for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
return;
- } else if (isa<ConstantAggregateZero>(Init)) {
+ }
+
+ if (isa<ConstantAggregateZero>(Init)) {
memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType()));
return;
- } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
+ }
+
+ if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
unsigned ElementSize =
getTargetData()->getTypeAllocSize(CPA->getType()->getElementType());
for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
return;
- } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
+ }
+
+ if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
const StructLayout *SL =
getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
return;
- } else if (Init->getType()->isFirstClassType()) {
+ }
+
+ if (const ConstantDataSequential *CDS =
+ dyn_cast<ConstantDataSequential>(Init)) {
+ // CDS is already laid out in host memory order.
+ StringRef Data = CDS->getRawDataValues();
+ memcpy(Addr, Data.data(), Data.size());
+ return;
+ }
+
+ if (Init->getType()->isFirstClassType()) {
GenericValue Val = getConstantValue(Init);
StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
return;
@@ -1133,6 +1149,6 @@ void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
const GlobalValue *,
const GlobalValue *) {
- assert(false && "The ExecutionEngine doesn't know how to handle a"
- " RAUW on a value it has a global mapping for.");
+ llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
+ " RAUW on a value it has a global mapping for.");
}
diff --git a/lib/ExecutionEngine/ExecutionEngineBindings.cpp b/lib/ExecutionEngine/ExecutionEngineBindings.cpp
index f8f1f4a..75e680a 100644
--- a/lib/ExecutionEngine/ExecutionEngineBindings.cpp
+++ b/lib/ExecutionEngine/ExecutionEngineBindings.cpp
@@ -76,9 +76,7 @@ double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
return unwrap(GenVal)->DoubleVal;
default:
llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
- break;
}
- return 0; // Not reached
}
void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
diff --git a/lib/ExecutionEngine/Interpreter/Execution.cpp b/lib/ExecutionEngine/Interpreter/Execution.cpp
index 27917da..3dce3b3 100644
--- a/lib/ExecutionEngine/Interpreter/Execution.cpp
+++ b/lib/ExecutionEngine/Interpreter/Execution.cpp
@@ -625,24 +625,6 @@ void Interpreter::visitReturnInst(ReturnInst &I) {
popStackAndReturnValueToCaller(RetTy, Result);
}
-void Interpreter::visitUnwindInst(UnwindInst &I) {
- // Unwind stack
- Instruction *Inst;
- do {
- ECStack.pop_back();
- if (ECStack.empty())
- report_fatal_error("Empty stack during unwind!");
- Inst = ECStack.back().Caller.getInstruction();
- } while (!(Inst && isa<InvokeInst>(Inst)));
-
- // Return from invoke
- ExecutionContext &InvokingSF = ECStack.back();
- InvokingSF.Caller = CallSite();
-
- // Go to exceptional destination BB of invoke instruction
- SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
-}
-
void Interpreter::visitUnreachableInst(UnreachableInst &I) {
report_fatal_error("Program executed an 'unreachable' instruction!");
}
@@ -670,10 +652,10 @@ void Interpreter::visitSwitchInst(SwitchInst &I) {
BasicBlock *Dest = 0;
unsigned NumCases = I.getNumCases();
// Skip the first item since that's the default case.
- for (unsigned i = 1; i < NumCases; ++i) {
+ for (unsigned i = 0; i < NumCases; ++i) {
GenericValue CaseVal = getOperandValue(I.getCaseValue(i), SF);
if (executeICMP_EQ(CondVal, CaseVal, ElTy).IntVal != 0) {
- Dest = cast<BasicBlock>(I.getSuccessor(i));
+ Dest = cast<BasicBlock>(I.getCaseSuccessor(i));
break;
}
}
@@ -1253,8 +1235,7 @@ GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
break;
default:
dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
- llvm_unreachable(0);
- return GenericValue();
+ llvm_unreachable("Unhandled ConstantExpr");
}
return Dest;
}
diff --git a/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp b/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp
index 1bcdbe2..7a206eb 100644
--- a/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp
+++ b/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp
@@ -297,14 +297,8 @@ GenericValue Interpreter::callExternalFunction(Function *F,
// Functions "exported" to the running application...
//
-// Visual Studio warns about returning GenericValue in extern "C" linkage
-#ifdef _MSC_VER
- #pragma warning(disable : 4190)
-#endif
-
-extern "C" { // Don't add C++ manglings to llvm mangling :)
-
// void atexit(Function*)
+static
GenericValue lle_X_atexit(FunctionType *FT,
const std::vector<GenericValue> &Args) {
assert(Args.size() == 1);
@@ -315,6 +309,7 @@ GenericValue lle_X_atexit(FunctionType *FT,
}
// void exit(int)
+static
GenericValue lle_X_exit(FunctionType *FT,
const std::vector<GenericValue> &Args) {
TheInterpreter->exitCalled(Args[0]);
@@ -322,6 +317,7 @@ GenericValue lle_X_exit(FunctionType *FT,
}
// void abort(void)
+static
GenericValue lle_X_abort(FunctionType *FT,
const std::vector<GenericValue> &Args) {
//FIXME: should we report or raise here?
@@ -332,6 +328,7 @@ GenericValue lle_X_abort(FunctionType *FT,
// int sprintf(char *, const char *, ...) - a very rough implementation to make
// output useful.
+static
GenericValue lle_X_sprintf(FunctionType *FT,
const std::vector<GenericValue> &Args) {
char *OutputBuffer = (char *)GVTOP(Args[0]);
@@ -409,11 +406,11 @@ GenericValue lle_X_sprintf(FunctionType *FT,
break;
}
}
- return GV;
}
// int printf(const char *, ...) - a very rough implementation to make output
// useful.
+static
GenericValue lle_X_printf(FunctionType *FT,
const std::vector<GenericValue> &Args) {
char Buffer[10000];
@@ -426,6 +423,7 @@ GenericValue lle_X_printf(FunctionType *FT,
}
// int sscanf(const char *format, ...);
+static
GenericValue lle_X_sscanf(FunctionType *FT,
const std::vector<GenericValue> &args) {
assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
@@ -441,6 +439,7 @@ GenericValue lle_X_sscanf(FunctionType *FT,
}
// int scanf(const char *format, ...);
+static
GenericValue lle_X_scanf(FunctionType *FT,
const std::vector<GenericValue> &args) {
assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
@@ -457,6 +456,7 @@ GenericValue lle_X_scanf(FunctionType *FT,
// int fprintf(FILE *, const char *, ...) - a very rough implementation to make
// output useful.
+static
GenericValue lle_X_fprintf(FunctionType *FT,
const std::vector<GenericValue> &Args) {
assert(Args.size() >= 2);
@@ -470,14 +470,6 @@ GenericValue lle_X_fprintf(FunctionType *FT,
return GV;
}
-} // End extern "C"
-
-// Done with externals; turn the warning back on
-#ifdef _MSC_VER
- #pragma warning(default: 4190)
-#endif
-
-
void Interpreter::initializeExternalFunctions() {
sys::ScopedLock Writer(*FunctionsLock);
FuncNames["lle_X_atexit"] = lle_X_atexit;
diff --git a/lib/ExecutionEngine/Interpreter/Interpreter.h b/lib/ExecutionEngine/Interpreter/Interpreter.h
index ee2b459..28c5775 100644
--- a/lib/ExecutionEngine/Interpreter/Interpreter.h
+++ b/lib/ExecutionEngine/Interpreter/Interpreter.h
@@ -115,6 +115,12 @@ public:
virtual GenericValue runFunction(Function *F,
const std::vector<GenericValue> &ArgValues);
+ virtual void *getPointerToNamedFunction(const std::string &Name,
+ bool AbortOnFailure = true) {
+ // FIXME: not implemented.
+ return 0;
+ }
+
/// recompileAndRelinkFunction - For the interpreter, functions are always
/// up-to-date.
///
@@ -165,7 +171,6 @@ public:
void visitCallSite(CallSite CS);
void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
- void visitUnwindInst(UnwindInst &I);
void visitUnreachableInst(UnreachableInst &I);
void visitShl(BinaryOperator &I);
diff --git a/lib/ExecutionEngine/JIT/CMakeLists.txt b/lib/ExecutionEngine/JIT/CMakeLists.txt
index cefb0ae..dcef08c 100644
--- a/lib/ExecutionEngine/JIT/CMakeLists.txt
+++ b/lib/ExecutionEngine/JIT/CMakeLists.txt
@@ -4,7 +4,6 @@ add_definitions(-DENABLE_X86_JIT)
add_llvm_library(LLVMJIT
Intercept.cpp
JIT.cpp
- JITDebugRegisterer.cpp
JITDwarfEmitter.cpp
JITEmitter.cpp
JITMemoryManager.cpp
diff --git a/lib/ExecutionEngine/JIT/JIT.cpp b/lib/ExecutionEngine/JIT/JIT.cpp
index e4f6bc4..f715f6f 100644
--- a/lib/ExecutionEngine/JIT/JIT.cpp
+++ b/lib/ExecutionEngine/JIT/JIT.cpp
@@ -475,7 +475,6 @@ GenericValue JIT::runFunction(Function *F,
case Type::FP128TyID:
case Type::PPC_FP128TyID:
llvm_unreachable("long double not supported yet");
- return rv;
case Type::PointerTyID:
return PTOGV(((void*(*)())(intptr_t)FPtr)());
}
@@ -707,9 +706,8 @@ void *JIT::getPointerToBasicBlock(BasicBlock *BB) {
if (I != getBasicBlockAddressMap(locked).end()) {
return I->second;
} else {
- assert(0 && "JIT does not have BB address for address-of-label, was"
- " it eliminated by optimizer?");
- return 0;
+ llvm_unreachable("JIT does not have BB address for address-of-label, was"
+ " it eliminated by optimizer?");
}
}
diff --git a/lib/ExecutionEngine/JIT/JIT.h b/lib/ExecutionEngine/JIT/JIT.h
index fbb9416..17d33fe 100644
--- a/lib/ExecutionEngine/JIT/JIT.h
+++ b/lib/ExecutionEngine/JIT/JIT.h
@@ -124,8 +124,8 @@ public:
/// found, this function silently returns a null pointer. Otherwise,
/// it prints a message to stderr and aborts.
///
- void *getPointerToNamedFunction(const std::string &Name,
- bool AbortOnFailure = true);
+ virtual void *getPointerToNamedFunction(const std::string &Name,
+ bool AbortOnFailure = true);
// CompilationCallback - Invoked the first time that a call site is found,
// which causes lazy compilation of the target function.
diff --git a/lib/ExecutionEngine/JIT/JITDebugRegisterer.cpp b/lib/ExecutionEngine/JIT/JITDebugRegisterer.cpp
deleted file mode 100644
index abb70fb..0000000
--- a/lib/ExecutionEngine/JIT/JITDebugRegisterer.cpp
+++ /dev/null
@@ -1,211 +0,0 @@
-//===-- JITDebugRegisterer.cpp - Register debug symbols for JIT -----------===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file defines a JITDebugRegisterer object that is used by the JIT to
-// register debug info with debuggers like GDB.
-//
-//===----------------------------------------------------------------------===//
-
-#include "JITDebugRegisterer.h"
-#include "../../CodeGen/ELF.h"
-#include "../../CodeGen/ELFWriter.h"
-#include "llvm/LLVMContext.h"
-#include "llvm/Function.h"
-#include "llvm/Module.h"
-#include "llvm/Target/TargetMachine.h"
-#include "llvm/Target/TargetOptions.h"
-#include "llvm/ADT/DenseMap.h"
-#include "llvm/ADT/OwningPtr.h"
-#include "llvm/Support/Compiler.h"
-#include "llvm/Support/MutexGuard.h"
-#include "llvm/Support/raw_ostream.h"
-#include "llvm/Support/Mutex.h"
-#include <string>
-
-namespace llvm {
-
-// This must be kept in sync with gdb/gdb/jit.h .
-extern "C" {
-
- // Debuggers puts a breakpoint in this function.
- LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() { }
-
- // We put information about the JITed function in this global, which the
- // debugger reads. Make sure to specify the version statically, because the
- // debugger checks the version before we can set it during runtime.
- struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
-
-}
-
-namespace {
-
- /// JITDebugLock - Used to serialize all code registration events, since they
- /// modify global variables.
- sys::Mutex JITDebugLock;
-
-}
-
-JITDebugRegisterer::JITDebugRegisterer(TargetMachine &tm) : TM(tm), FnMap() { }
-
-JITDebugRegisterer::~JITDebugRegisterer() {
- // Free all ELF memory.
- for (RegisteredFunctionsMap::iterator I = FnMap.begin(), E = FnMap.end();
- I != E; ++I) {
- // Call the private method that doesn't update the map so our iterator
- // doesn't break.
- UnregisterFunctionInternal(I);
- }
- FnMap.clear();
-}
-
-std::string JITDebugRegisterer::MakeELF(const Function *F, DebugInfo &I) {
- // Stack allocate an empty module with an empty LLVMContext for the ELFWriter
- // API. We don't use the real module because then the ELFWriter would write
- // out unnecessary GlobalValues during finalization.
- LLVMContext Context;
- Module M("", Context);
-
- // Make a buffer for the ELF in memory.
- std::string Buffer;
- raw_string_ostream O(Buffer);
- ELFWriter EW(O, TM);
- EW.doInitialization(M);
-
- // Copy the binary into the .text section. This isn't necessary, but it's
- // useful to be able to disassemble the ELF by hand.
- ELFSection &Text = EW.getTextSection(const_cast<Function *>(F));
- Text.Addr = (uint64_t)I.FnStart;
- // TODO: We could eliminate this copy if we somehow used a pointer/size pair
- // instead of a vector.
- Text.getData().assign(I.FnStart, I.FnEnd);
-
- // Copy the exception handling call frame information into the .eh_frame
- // section. This allows GDB to get a good stack trace, particularly on
- // linux x86_64. Mark this as a PROGBITS section that needs to be loaded
- // into memory at runtime.
- ELFSection &EH = EW.getSection(".eh_frame", ELF::SHT_PROGBITS,
- ELF::SHF_ALLOC);
- // Pointers in the DWARF EH info are all relative to the EH frame start,
- // which is stored here.
- EH.Addr = (uint64_t)I.EhStart;
- // TODO: We could eliminate this copy if we somehow used a pointer/size pair
- // instead of a vector.
- EH.getData().assign(I.EhStart, I.EhEnd);
-
- // Add this single function to the symbol table, so the debugger prints the
- // name instead of '???'. We give the symbol default global visibility.
- ELFSym *FnSym = ELFSym::getGV(F,
- ELF::STB_GLOBAL,
- ELF::STT_FUNC,
- ELF::STV_DEFAULT);
- FnSym->SectionIdx = Text.SectionIdx;
- FnSym->Size = I.FnEnd - I.FnStart;
- FnSym->Value = 0; // Offset from start of section.
- EW.SymbolList.push_back(FnSym);
-
- EW.doFinalization(M);
- O.flush();
-
- // When trying to debug why GDB isn't getting the debug info right, it's
- // awfully helpful to write the object file to disk so that it can be
- // inspected with readelf and objdump.
- if (TM.Options.JITEmitDebugInfoToDisk) {
- std::string Filename;
- raw_string_ostream O2(Filename);
- O2 << "/tmp/llvm_function_" << I.FnStart << "_" << F->getName() << ".o";
- O2.flush();
- std::string Errors;
- raw_fd_ostream O3(Filename.c_str(), Errors);
- O3 << Buffer;
- O3.close();
- }
-
- return Buffer;
-}
-
-void JITDebugRegisterer::RegisterFunction(const Function *F, DebugInfo &I) {
- // TODO: Support non-ELF platforms.
- if (!TM.getELFWriterInfo())
- return;
-
- std::string Buffer = MakeELF(F, I);
-
- jit_code_entry *JITCodeEntry = new jit_code_entry();
- JITCodeEntry->symfile_addr = Buffer.c_str();
- JITCodeEntry->symfile_size = Buffer.size();
-
- // Add a mapping from F to the entry and buffer, so we can delete this
- // info later.
- FnMap[F] = std::make_pair(Buffer, JITCodeEntry);
-
- // Acquire the lock and do the registration.
- {
- MutexGuard locked(JITDebugLock);
- __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
-
- // Insert this entry at the head of the list.
- JITCodeEntry->prev_entry = NULL;
- jit_code_entry *NextEntry = __jit_debug_descriptor.first_entry;
- JITCodeEntry->next_entry = NextEntry;
- if (NextEntry != NULL) {
- NextEntry->prev_entry = JITCodeEntry;
- }
- __jit_debug_descriptor.first_entry = JITCodeEntry;
- __jit_debug_descriptor.relevant_entry = JITCodeEntry;
- __jit_debug_register_code();
- }
-}
-
-void JITDebugRegisterer::UnregisterFunctionInternal(
- RegisteredFunctionsMap::iterator I) {
- jit_code_entry *&JITCodeEntry = I->second.second;
-
- // Acquire the lock and do the unregistration.
- {
- MutexGuard locked(JITDebugLock);
- __jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN;
-
- // Remove the jit_code_entry from the linked list.
- jit_code_entry *PrevEntry = JITCodeEntry->prev_entry;
- jit_code_entry *NextEntry = JITCodeEntry->next_entry;
- if (NextEntry) {
- NextEntry->prev_entry = PrevEntry;
- }
- if (PrevEntry) {
- PrevEntry->next_entry = NextEntry;
- } else {
- assert(__jit_debug_descriptor.first_entry == JITCodeEntry);
- __jit_debug_descriptor.first_entry = NextEntry;
- }
-
- // Tell GDB which entry we removed, and unregister the code.
- __jit_debug_descriptor.relevant_entry = JITCodeEntry;
- __jit_debug_register_code();
- }
-
- delete JITCodeEntry;
- JITCodeEntry = NULL;
-
- // Free the ELF file in memory.
- std::string &Buffer = I->second.first;
- Buffer.clear();
-}
-
-void JITDebugRegisterer::UnregisterFunction(const Function *F) {
- // TODO: Support non-ELF platforms.
- if (!TM.getELFWriterInfo())
- return;
-
- RegisteredFunctionsMap::iterator I = FnMap.find(F);
- if (I == FnMap.end()) return;
- UnregisterFunctionInternal(I);
- FnMap.erase(I);
-}
-
-} // end namespace llvm
diff --git a/lib/ExecutionEngine/JIT/JITDebugRegisterer.h b/lib/ExecutionEngine/JIT/JITDebugRegisterer.h
deleted file mode 100644
index dce506b..0000000
--- a/lib/ExecutionEngine/JIT/JITDebugRegisterer.h
+++ /dev/null
@@ -1,116 +0,0 @@
-//===-- JITDebugRegisterer.h - Register debug symbols for JIT -------------===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file defines a JITDebugRegisterer object that is used by the JIT to
-// register debug info with debuggers like GDB.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_EXECUTION_ENGINE_JIT_DEBUGREGISTERER_H
-#define LLVM_EXECUTION_ENGINE_JIT_DEBUGREGISTERER_H
-
-#include "llvm/ADT/DenseMap.h"
-#include "llvm/Support/DataTypes.h"
-#include <string>
-
-// This must be kept in sync with gdb/gdb/jit.h .
-extern "C" {
-
- typedef enum {
- JIT_NOACTION = 0,
- JIT_REGISTER_FN,
- JIT_UNREGISTER_FN
- } jit_actions_t;
-
- struct jit_code_entry {
- struct jit_code_entry *next_entry;
- struct jit_code_entry *prev_entry;
- const char *symfile_addr;
- uint64_t symfile_size;
- };
-
- struct jit_descriptor {
- uint32_t version;
- // This should be jit_actions_t, but we want to be specific about the
- // bit-width.
- uint32_t action_flag;
- struct jit_code_entry *relevant_entry;
- struct jit_code_entry *first_entry;
- };
-
-}
-
-namespace llvm {
-
-class ELFSection;
-class Function;
-class TargetMachine;
-
-
-/// This class encapsulates information we want to send to the debugger.
-///
-struct DebugInfo {
- uint8_t *FnStart;
- uint8_t *FnEnd;
- uint8_t *EhStart;
- uint8_t *EhEnd;
-
- DebugInfo() : FnStart(0), FnEnd(0), EhStart(0), EhEnd(0) {}
-};
-
-typedef DenseMap< const Function*, std::pair<std::string, jit_code_entry*> >
- RegisteredFunctionsMap;
-
-/// This class registers debug info for JITed code with an attached debugger.
-/// Without proper debug info, GDB can't do things like source level debugging
-/// or even produce a proper stack trace on linux-x86_64. To use this class,
-/// whenever a function is JITed, create a DebugInfo struct and pass it to the
-/// RegisterFunction method. The method will then do whatever is necessary to
-/// inform the debugger about the JITed function.
-class JITDebugRegisterer {
-
- TargetMachine &TM;
-
- /// FnMap - A map of functions that have been registered to the associated
- /// temporary files. Used for cleanup.
- RegisteredFunctionsMap FnMap;
-
- /// MakeELF - Builds the ELF file in memory and returns a std::string that
- /// contains the ELF.
- std::string MakeELF(const Function *F, DebugInfo &I);
-
-public:
- JITDebugRegisterer(TargetMachine &tm);
-
- /// ~JITDebugRegisterer - Unregisters all code and frees symbol files.
- ///
- ~JITDebugRegisterer();
-
- /// RegisterFunction - Register debug info for the given function with an
- /// attached debugger. Clients must call UnregisterFunction on all
- /// registered functions before deleting them to free the associated symbol
- /// file and unregister it from the debugger.
- void RegisterFunction(const Function *F, DebugInfo &I);
-
- /// UnregisterFunction - Unregister the debug info for the given function
- /// from the debugger and free associated memory.
- void UnregisterFunction(const Function *F);
-
-private:
- /// UnregisterFunctionInternal - Unregister the debug info for the given
- /// function from the debugger and delete any temporary files. The private
- /// version of this method does not remove the function from FnMap so that it
- /// can be called while iterating over FnMap.
- void UnregisterFunctionInternal(RegisteredFunctionsMap::iterator I);
-
-};
-
-} // end namespace llvm
-
-#endif // LLVM_EXECUTION_ENGINE_JIT_DEBUGREGISTERER_H
diff --git a/lib/ExecutionEngine/JIT/JITEmitter.cpp b/lib/ExecutionEngine/JIT/JITEmitter.cpp
index d9fa509..504c8bd 100644
--- a/lib/ExecutionEngine/JIT/JITEmitter.cpp
+++ b/lib/ExecutionEngine/JIT/JITEmitter.cpp
@@ -14,7 +14,6 @@
#define DEBUG_TYPE "jit"
#include "JIT.h"
-#include "JITDebugRegisterer.h"
#include "JITDwarfEmitter.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Constants.h"
@@ -77,8 +76,8 @@ namespace {
struct NoRAUWValueMapConfig : public ValueMapConfig<ValueTy> {
typedef JITResolverState *ExtraData;
static void onRAUW(JITResolverState *, Value *Old, Value *New) {
- assert(false && "The JIT doesn't know how to handle a"
- " RAUW on a value it has emitted.");
+ llvm_unreachable("The JIT doesn't know how to handle a"
+ " RAUW on a value it has emitted.");
}
};
@@ -324,9 +323,6 @@ namespace {
/// DE - The dwarf emitter for the jit.
OwningPtr<JITDwarfEmitter> DE;
- /// DR - The debug registerer for the jit.
- OwningPtr<JITDebugRegisterer> DR;
-
/// LabelLocations - This vector is a mapping from Label ID's to their
/// address.
DenseMap<MCSymbol*, uintptr_t> LabelLocations;
@@ -364,26 +360,20 @@ namespace {
bool JITExceptionHandling;
- bool JITEmitDebugInfo;
-
public:
JITEmitter(JIT &jit, JITMemoryManager *JMM, TargetMachine &TM)
: SizeEstimate(0), Resolver(jit, *this), MMI(0), CurFn(0),
EmittedFunctions(this), TheJIT(&jit),
- JITExceptionHandling(TM.Options.JITExceptionHandling),
- JITEmitDebugInfo(TM.Options.JITEmitDebugInfo) {
+ JITExceptionHandling(TM.Options.JITExceptionHandling) {
MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager();
if (jit.getJITInfo().needsGOT()) {
MemMgr->AllocateGOT();
DEBUG(dbgs() << "JIT is managing a GOT\n");
}
- if (JITExceptionHandling || JITEmitDebugInfo) {
+ if (JITExceptionHandling) {
DE.reset(new JITDwarfEmitter(jit));
}
- if (JITEmitDebugInfo) {
- DR.reset(new JITDebugRegisterer(TM));
- }
}
~JITEmitter() {
delete MemMgr;
@@ -974,7 +964,7 @@ bool JITEmitter::finishFunction(MachineFunction &F) {
}
});
- if (JITExceptionHandling || JITEmitDebugInfo) {
+ if (JITExceptionHandling) {
uintptr_t ActualSize = 0;
SavedBufferBegin = BufferBegin;
SavedBufferEnd = BufferEnd;
@@ -989,7 +979,6 @@ bool JITEmitter::finishFunction(MachineFunction &F) {
EhStart);
MemMgr->endExceptionTable(F.getFunction(), BufferBegin, CurBufferPtr,
FrameRegister);
- uint8_t *EhEnd = CurBufferPtr;
BufferBegin = SavedBufferBegin;
BufferEnd = SavedBufferEnd;
CurBufferPtr = SavedCurBufferPtr;
@@ -997,15 +986,6 @@ bool JITEmitter::finishFunction(MachineFunction &F) {
if (JITExceptionHandling) {
TheJIT->RegisterTable(F.getFunction(), FrameRegister);
}
-
- if (JITEmitDebugInfo) {
- DebugInfo I;
- I.FnStart = FnStart;
- I.FnEnd = FnEnd;
- I.EhStart = EhStart;
- I.EhEnd = EhEnd;
- DR->RegisterFunction(F.getFunction(), I);
- }
}
if (MMI)
@@ -1046,10 +1026,6 @@ void JITEmitter::deallocateMemForFunction(const Function *F) {
if (JITExceptionHandling) {
TheJIT->DeregisterTable(F);
}
-
- if (JITEmitDebugInfo) {
- DR->UnregisterFunction(F);
- }
}
@@ -1185,6 +1161,9 @@ void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) {
}
break;
}
+ case MachineJumpTableInfo::EK_GPRel64BlockAddress:
+ llvm_unreachable(
+ "JT Info emission not implemented for GPRel64BlockAddress yet.");
}
}
diff --git a/lib/ExecutionEngine/JIT/JITMemoryManager.cpp b/lib/ExecutionEngine/JIT/JITMemoryManager.cpp
index eec23ce..efd570d 100644
--- a/lib/ExecutionEngine/JIT/JITMemoryManager.cpp
+++ b/lib/ExecutionEngine/JIT/JITMemoryManager.cpp
@@ -441,6 +441,50 @@ namespace {
return (uint8_t*)DataAllocator.Allocate(Size, Alignment);
}
+ /// allocateCodeSection - Allocate memory for a code section.
+ uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+ unsigned SectionID) {
+ // FIXME: Alignement handling.
+ FreeRangeHeader* candidateBlock = FreeMemoryList;
+ FreeRangeHeader* head = FreeMemoryList;
+ FreeRangeHeader* iter = head->Next;
+
+ uintptr_t largest = candidateBlock->BlockSize;
+
+ // Search for the largest free block.
+ while (iter != head) {
+ if (iter->BlockSize > largest) {
+ largest = iter->BlockSize;
+ candidateBlock = iter;
+ }
+ iter = iter->Next;
+ }
+
+ largest = largest - sizeof(MemoryRangeHeader);
+
+ // If this block isn't big enough for the allocation desired, allocate
+ // another block of memory and add it to the free list.
+ if (largest < Size || largest <= FreeRangeHeader::getMinBlockSize()) {
+ DEBUG(dbgs() << "JIT: Allocating another slab of memory for function.");
+ candidateBlock = allocateNewCodeSlab((size_t)Size);
+ }
+
+ // Select this candidate block for allocation
+ CurBlock = candidateBlock;
+
+ // Allocate the entire memory block.
+ FreeMemoryList = candidateBlock->AllocateBlock();
+ // Release the memory at the end of this block that isn't needed.
+ FreeMemoryList = CurBlock->TrimAllocationToSize(FreeMemoryList, Size);
+ return (uint8_t *)(CurBlock + 1);
+ }
+
+ /// allocateDataSection - Allocate memory for a data section.
+ uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
+ unsigned SectionID) {
+ return (uint8_t*)DataAllocator.Allocate(Size, Alignment);
+ }
+
/// startExceptionTable - Use startFunctionBody to allocate memory for the
/// function's exception table.
uint8_t* startExceptionTable(const Function* F, uintptr_t &ActualSize) {
diff --git a/lib/ExecutionEngine/MCJIT/CMakeLists.txt b/lib/ExecutionEngine/MCJIT/CMakeLists.txt
index 38fdffa..2c0f8d6 100644
--- a/lib/ExecutionEngine/MCJIT/CMakeLists.txt
+++ b/lib/ExecutionEngine/MCJIT/CMakeLists.txt
@@ -1,4 +1,5 @@
add_llvm_library(LLVMMCJIT
MCJIT.cpp
+ MCJITMemoryManager.cpp
Intercept.cpp
)
diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/lib/ExecutionEngine/MCJIT/MCJIT.cpp
index d5aaec9..5f93a8d 100644
--- a/lib/ExecutionEngine/MCJIT/MCJIT.cpp
+++ b/lib/ExecutionEngine/MCJIT/MCJIT.cpp
@@ -85,11 +85,11 @@ MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
MCJIT::~MCJIT() {
delete MemMgr;
+ delete TM;
}
void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
report_fatal_error("not yet implemented");
- return 0;
}
void *MCJIT::getPointerToFunction(Function *F) {
@@ -208,12 +208,10 @@ GenericValue MCJIT::runFunction(Function *F,
case Type::FP128TyID:
case Type::PPC_FP128TyID:
llvm_unreachable("long double not supported yet");
- return rv;
case Type::PointerTyID:
return PTOGV(((void*(*)())(intptr_t)FPtr)());
}
}
- assert(0 && "Full-featured argument passing not supported yet!");
- return GenericValue();
+ llvm_unreachable("Full-featured argument passing not supported yet!");
}
diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.h b/lib/ExecutionEngine/MCJIT/MCJIT.h
index 2a98fc9..7f4ae77 100644
--- a/lib/ExecutionEngine/MCJIT/MCJIT.h
+++ b/lib/ExecutionEngine/MCJIT/MCJIT.h
@@ -65,8 +65,16 @@ public:
/// found, this function silently returns a null pointer. Otherwise,
/// it prints a message to stderr and aborts.
///
- void *getPointerToNamedFunction(const std::string &Name,
- bool AbortOnFailure = true);
+ virtual void *getPointerToNamedFunction(const std::string &Name,
+ bool AbortOnFailure = true);
+ /// mapSectionAddress - map a section to its target address space value.
+ /// Map the address of a JIT section as returned from the memory manager
+ /// to the address in the target process as the running code will see it.
+ /// This is the address which will be used for relocation resolution.
+ virtual void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress) {
+ Dyld.mapSectionAddress(LocalAddress, TargetAddress);
+ }
+
/// @}
/// @name (Private) Registration Interfaces
/// @{
diff --git a/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.cpp b/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.cpp
new file mode 100644
index 0000000..457fe5e
--- /dev/null
+++ b/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.cpp
@@ -0,0 +1,14 @@
+//==-- MCJITMemoryManager.cpp - Definition for the Memory Manager -*-C++ -*-==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "MCJITMemoryManager.h"
+
+using namespace llvm;
+
+void MCJITMemoryManager::anchor() { }
diff --git a/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h b/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h
index 58f9100..ac8c155 100644
--- a/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h
+++ b/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h
@@ -21,6 +21,7 @@ namespace llvm {
// and the RuntimeDyld interface that maps objects, by name, onto their
// matching LLVM IR counterparts in the module(s) being compiled.
class MCJITMemoryManager : public RTDyldMemoryManager {
+ virtual void anchor();
JITMemoryManager *JMM;
// FIXME: Multiple modules.
@@ -30,6 +31,16 @@ public:
// We own the JMM, so make sure to delete it.
~MCJITMemoryManager() { delete JMM; }
+ uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
+ unsigned SectionID) {
+ return JMM->allocateDataSection(Size, Alignment, SectionID);
+ }
+
+ uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+ unsigned SectionID) {
+ return JMM->allocateCodeSection(Size, Alignment, SectionID);
+ }
+
// Allocate ActualSize bytes, or more, for the named function. Return
// a pointer to the allocated memory and update Size to reflect how much
// memory was acutally allocated.
diff --git a/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt b/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt
index 59bdfee3..002e63c 100644
--- a/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt
+++ b/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt
@@ -1,4 +1,5 @@
add_llvm_library(LLVMRuntimeDyld
RuntimeDyld.cpp
RuntimeDyldMachO.cpp
+ RuntimeDyldELF.cpp
)
diff --git a/lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h b/lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h
new file mode 100644
index 0000000..2d777da
--- /dev/null
+++ b/lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h
@@ -0,0 +1,388 @@
+//===-- DyldELFObject.h - Dynamically loaded ELF object ----0---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Dynamically loaded ELF object class, a subclass of ELFObjectFile. Used
+// to represent a loadable ELF image.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
+#define LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
+
+#include "llvm/Object/ELF.h"
+
+
+namespace llvm {
+
+using support::endianness;
+using namespace llvm::object;
+
+template<support::endianness target_endianness, bool is64Bits>
+class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
+ LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
+
+ typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
+ typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
+ typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
+ typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
+
+ typedef typename ELFObjectFile<target_endianness, is64Bits>::
+ Elf_Ehdr Elf_Ehdr;
+ Elf_Ehdr *Header;
+
+ // Update section headers according to the current location in memory
+ virtual void rebaseObject(std::vector<uint8_t*> *MemoryMap);
+ // Record memory addresses for cleanup
+ virtual void saveAddress(std::vector<uint8_t*> *MemoryMap, uint8_t *addr);
+
+protected:
+ virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
+
+public:
+ DyldELFObject(MemoryBuffer *Object, std::vector<uint8_t*> *MemoryMap,
+ error_code &ec);
+
+ // Methods for type inquiry through isa, cast, and dyn_cast
+ static inline bool classof(const Binary *v) {
+ return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
+ && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
+ }
+ static inline bool classof(
+ const ELFObjectFile<target_endianness, is64Bits> *v) {
+ return v->isDyldType();
+ }
+ static inline bool classof(const DyldELFObject *v) {
+ return true;
+ }
+};
+
+template<support::endianness target_endianness, bool is64Bits>
+DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object,
+ std::vector<uint8_t*> *MemoryMap, error_code &ec)
+ : ELFObjectFile<target_endianness, is64Bits>(Object, ec)
+ , Header(0) {
+ this->isDyldELFObject = true;
+ Header = const_cast<Elf_Ehdr *>(
+ reinterpret_cast<const Elf_Ehdr *>(this->base()));
+ if (Header->e_shoff == 0)
+ return;
+
+ // Mark the image as a dynamic shared library
+ Header->e_type = ELF::ET_DYN;
+
+ rebaseObject(MemoryMap);
+}
+
+// Walk through the ELF headers, updating virtual addresses to reflect where
+// the object is currently loaded in memory
+template<support::endianness target_endianness, bool is64Bits>
+void DyldELFObject<target_endianness, is64Bits>::rebaseObject(
+ std::vector<uint8_t*> *MemoryMap) {
+ typedef typename ELFDataTypeTypedefHelper<
+ target_endianness, is64Bits>::value_type addr_type;
+
+ uint8_t *base_p = const_cast<uint8_t *>(this->base());
+ Elf_Shdr *sectionTable =
+ reinterpret_cast<Elf_Shdr *>(base_p + Header->e_shoff);
+ uint64_t numSections = this->getNumSections();
+
+ // Allocate memory space for NOBITS sections (such as .bss), which only exist
+ // in memory, but don't occupy space in the object file.
+ // Update the address in the section headers to reflect this allocation.
+ for (uint64_t index = 0; index < numSections; index++) {
+ Elf_Shdr *sec = reinterpret_cast<Elf_Shdr *>(
+ reinterpret_cast<char *>(sectionTable) + index * Header->e_shentsize);
+
+ // Only update sections that are meant to be present in program memory
+ if (sec->sh_flags & ELF::SHF_ALLOC) {
+ uint8_t *addr = base_p + sec->sh_offset;
+ if (sec->sh_type == ELF::SHT_NOBITS) {
+ addr = static_cast<uint8_t *>(calloc(sec->sh_size, 1));
+ saveAddress(MemoryMap, addr);
+ }
+ else {
+ // FIXME: Currently memory with RWX permissions is allocated. In the
+ // future, make sure that permissions are as necessary
+ if (sec->sh_flags & ELF::SHF_WRITE) {
+ // see FIXME above
+ }
+ if (sec->sh_flags & ELF::SHF_EXECINSTR) {
+ // see FIXME above
+ }
+ }
+ assert(sizeof(addr_type) == sizeof(intptr_t) &&
+ "Cross-architecture ELF dy-load is not supported!");
+ sec->sh_addr = static_cast<addr_type>(intptr_t(addr));
+ }
+ }
+
+ // Now allocate actual space for COMMON symbols, which also don't occupy
+ // space in the object file.
+ // We want to allocate space for all COMMON symbols at once, so the flow is:
+ // 1. Go over all symbols, find those that are in COMMON. For each such
+ // symbol, record its size and the value field in its symbol header in a
+ // special vector.
+ // 2. Allocate memory for all COMMON symbols in one fell swoop.
+ // 3. Using the recorded information from (1), update the address fields in
+ // the symbol headers of the COMMON symbols to reflect their allocated
+ // address.
+ uint64_t TotalSize = 0;
+ std::vector<std::pair<Elf_Addr *, uint64_t> > SymbAddrInfo;
+ error_code ec = object_error::success;
+ for (symbol_iterator si = this->begin_symbols(),
+ se = this->end_symbols(); si != se; si.increment(ec)) {
+ uint64_t Size = 0;
+ ec = si->getSize(Size);
+ Elf_Sym* symb = const_cast<Elf_Sym*>(
+ this->getSymbol(si->getRawDataRefImpl()));
+ if (ec == object_error::success &&
+ this->getSymbolTableIndex(symb) == ELF::SHN_COMMON && Size > 0) {
+ SymbAddrInfo.push_back(std::make_pair(&(symb->st_value), Size));
+ TotalSize += Size;
+ }
+ }
+
+ uint8_t* SectionPtr = (uint8_t *)calloc(TotalSize, 1);
+ saveAddress(MemoryMap, SectionPtr);
+
+ typedef typename std::vector<std::pair<Elf_Addr *, uint64_t> >::iterator
+ AddrInfoIterator;
+ AddrInfoIterator EndIter = SymbAddrInfo.end();
+ for (AddrInfoIterator AddrIter = SymbAddrInfo.begin();
+ AddrIter != EndIter; ++AddrIter) {
+ assert(sizeof(addr_type) == sizeof(intptr_t) &&
+ "Cross-architecture ELF dy-load is not supported!");
+ *(AddrIter->first) = static_cast<addr_type>(intptr_t(SectionPtr));
+ SectionPtr += AddrIter->second;
+ }
+}
+
+// Record memory addresses for callers
+template<support::endianness target_endianness, bool is64Bits>
+void DyldELFObject<target_endianness, is64Bits>::saveAddress(
+ std::vector<uint8_t*> *MemoryMap, uint8_t* addr) {
+ if (MemoryMap)
+ MemoryMap->push_back(addr);
+ else
+ errs() << "WARNING: Memory leak - cannot record memory for ELF dyld.";
+}
+
+template<support::endianness target_endianness, bool is64Bits>
+error_code DyldELFObject<target_endianness, is64Bits>::getSymbolAddress(
+ DataRefImpl Symb, uint64_t &Result) const {
+ this->validateSymbol(Symb);
+ const Elf_Sym *symb = this->getSymbol(Symb);
+ if (this->getSymbolTableIndex(symb) == ELF::SHN_COMMON) {
+ Result = symb->st_value;
+ return object_error::success;
+ }
+ else {
+ return ELFObjectFile<target_endianness, is64Bits>::getSymbolAddress(
+ Symb, Result);
+ }
+}
+
+}
+
+#endif
+
+//===-- DyldELFObject.h - Dynamically loaded ELF object ----0---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Dynamically loaded ELF object class, a subclass of ELFObjectFile. Used
+// to represent a loadable ELF image.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
+#define LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
+
+#include "llvm/Object/ELF.h"
+
+
+namespace llvm {
+
+using support::endianness;
+using namespace llvm::object;
+
+template<support::endianness target_endianness, bool is64Bits>
+class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
+ LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
+
+ typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
+ typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
+ typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
+ typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
+
+ typedef typename ELFObjectFile<target_endianness, is64Bits>::
+ Elf_Ehdr Elf_Ehdr;
+ Elf_Ehdr *Header;
+
+ // Update section headers according to the current location in memory
+ virtual void rebaseObject(std::vector<uint8_t*> *MemoryMap);
+ // Record memory addresses for cleanup
+ virtual void saveAddress(std::vector<uint8_t*> *MemoryMap, uint8_t *addr);
+
+protected:
+ virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
+
+public:
+ DyldELFObject(MemoryBuffer *Object, std::vector<uint8_t*> *MemoryMap,
+ error_code &ec);
+
+ // Methods for type inquiry through isa, cast, and dyn_cast
+ static inline bool classof(const Binary *v) {
+ return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
+ && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
+ }
+ static inline bool classof(
+ const ELFObjectFile<target_endianness, is64Bits> *v) {
+ return v->isDyldType();
+ }
+ static inline bool classof(const DyldELFObject *v) {
+ return true;
+ }
+};
+
+template<support::endianness target_endianness, bool is64Bits>
+DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object,
+ std::vector<uint8_t*> *MemoryMap, error_code &ec)
+ : ELFObjectFile<target_endianness, is64Bits>(Object, ec)
+ , Header(0) {
+ this->isDyldELFObject = true;
+ Header = const_cast<Elf_Ehdr *>(
+ reinterpret_cast<const Elf_Ehdr *>(this->base()));
+ if (Header->e_shoff == 0)
+ return;
+
+ // Mark the image as a dynamic shared library
+ Header->e_type = ELF::ET_DYN;
+
+ rebaseObject(MemoryMap);
+}
+
+// Walk through the ELF headers, updating virtual addresses to reflect where
+// the object is currently loaded in memory
+template<support::endianness target_endianness, bool is64Bits>
+void DyldELFObject<target_endianness, is64Bits>::rebaseObject(
+ std::vector<uint8_t*> *MemoryMap) {
+ typedef typename ELFDataTypeTypedefHelper<
+ target_endianness, is64Bits>::value_type addr_type;
+
+ uint8_t *base_p = const_cast<uint8_t *>(this->base());
+ Elf_Shdr *sectionTable =
+ reinterpret_cast<Elf_Shdr *>(base_p + Header->e_shoff);
+ uint64_t numSections = this->getNumSections();
+
+ // Allocate memory space for NOBITS sections (such as .bss), which only exist
+ // in memory, but don't occupy space in the object file.
+ // Update the address in the section headers to reflect this allocation.
+ for (uint64_t index = 0; index < numSections; index++) {
+ Elf_Shdr *sec = reinterpret_cast<Elf_Shdr *>(
+ reinterpret_cast<char *>(sectionTable) + index * Header->e_shentsize);
+
+ // Only update sections that are meant to be present in program memory
+ if (sec->sh_flags & ELF::SHF_ALLOC) {
+ uint8_t *addr = base_p + sec->sh_offset;
+ if (sec->sh_type == ELF::SHT_NOBITS) {
+ addr = static_cast<uint8_t *>(calloc(sec->sh_size, 1));
+ saveAddress(MemoryMap, addr);
+ }
+ else {
+ // FIXME: Currently memory with RWX permissions is allocated. In the
+ // future, make sure that permissions are as necessary
+ if (sec->sh_flags & ELF::SHF_WRITE) {
+ // see FIXME above
+ }
+ if (sec->sh_flags & ELF::SHF_EXECINSTR) {
+ // see FIXME above
+ }
+ }
+ assert(sizeof(addr_type) == sizeof(intptr_t) &&
+ "Cross-architecture ELF dy-load is not supported!");
+ sec->sh_addr = static_cast<addr_type>(intptr_t(addr));
+ }
+ }
+
+ // Now allocate actual space for COMMON symbols, which also don't occupy
+ // space in the object file.
+ // We want to allocate space for all COMMON symbols at once, so the flow is:
+ // 1. Go over all symbols, find those that are in COMMON. For each such
+ // symbol, record its size and the value field in its symbol header in a
+ // special vector.
+ // 2. Allocate memory for all COMMON symbols in one fell swoop.
+ // 3. Using the recorded information from (1), update the address fields in
+ // the symbol headers of the COMMON symbols to reflect their allocated
+ // address.
+ uint64_t TotalSize = 0;
+ std::vector<std::pair<Elf_Addr *, uint64_t> > SymbAddrInfo;
+ error_code ec = object_error::success;
+ for (symbol_iterator si = this->begin_symbols(),
+ se = this->end_symbols(); si != se; si.increment(ec)) {
+ uint64_t Size = 0;
+ ec = si->getSize(Size);
+ Elf_Sym* symb = const_cast<Elf_Sym*>(
+ this->getSymbol(si->getRawDataRefImpl()));
+ if (ec == object_error::success &&
+ this->getSymbolTableIndex(symb) == ELF::SHN_COMMON && Size > 0) {
+ SymbAddrInfo.push_back(std::make_pair(&(symb->st_value), Size));
+ TotalSize += Size;
+ }
+ }
+
+ uint8_t* SectionPtr = (uint8_t *)calloc(TotalSize, 1);
+ saveAddress(MemoryMap, SectionPtr);
+
+ typedef typename std::vector<std::pair<Elf_Addr *, uint64_t> >::iterator
+ AddrInfoIterator;
+ AddrInfoIterator EndIter = SymbAddrInfo.end();
+ for (AddrInfoIterator AddrIter = SymbAddrInfo.begin();
+ AddrIter != EndIter; ++AddrIter) {
+ assert(sizeof(addr_type) == sizeof(intptr_t) &&
+ "Cross-architecture ELF dy-load is not supported!");
+ *(AddrIter->first) = static_cast<addr_type>(intptr_t(SectionPtr));
+ SectionPtr += AddrIter->second;
+ }
+}
+
+// Record memory addresses for callers
+template<support::endianness target_endianness, bool is64Bits>
+void DyldELFObject<target_endianness, is64Bits>::saveAddress(
+ std::vector<uint8_t*> *MemoryMap, uint8_t* addr) {
+ if (MemoryMap)
+ MemoryMap->push_back(addr);
+ else
+ errs() << "WARNING: Memory leak - cannot record memory for ELF dyld.";
+}
+
+template<support::endianness target_endianness, bool is64Bits>
+error_code DyldELFObject<target_endianness, is64Bits>::getSymbolAddress(
+ DataRefImpl Symb, uint64_t &Result) const {
+ this->validateSymbol(Symb);
+ const Elf_Sym *symb = this->getSymbol(Symb);
+ if (this->getSymbolTableIndex(symb) == ELF::SHN_COMMON) {
+ Result = symb->st_value;
+ return object_error::success;
+ }
+ else {
+ return ELFObjectFile<target_endianness, is64Bits>::getSymbolAddress(
+ Symb, Result);
+ }
+}
+
+}
+
+#endif
+
diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
index 33dd705..2896c2d 100644
--- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
+++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
@@ -1,4 +1,4 @@
-//===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
+//===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@@ -13,6 +13,10 @@
#define DEBUG_TYPE "dyld"
#include "RuntimeDyldImpl.h"
+#include "RuntimeDyldELF.h"
+#include "RuntimeDyldMachO.h"
+#include "llvm/Support/Path.h"
+
using namespace llvm;
using namespace llvm::object;
@@ -24,6 +28,7 @@ namespace llvm {
void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress,
uint8_t *EndAddress) {
+ // FIXME: DEPRECATED in favor of by-section allocation.
// Allocate memory for the function via the memory manager.
uintptr_t Size = EndAddress - StartAddress + 1;
uintptr_t AllocSize = Size;
@@ -34,21 +39,30 @@ void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress,
memcpy(Mem, StartAddress, Size);
MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size);
// Remember where we put it.
- Functions[Name] = sys::MemoryBlock(Mem, Size);
+ unsigned SectionID = Sections.size();
+ Sections.push_back(sys::MemoryBlock(Mem, Size));
+
// Default the assigned address for this symbol to wherever this
// allocated it.
- SymbolTable[Name] = Mem;
+ SymbolTable[Name] = SymbolLoc(SectionID, 0);
DEBUG(dbgs() << " allocated to [" << Mem << ", " << Mem + Size << "]\n");
}
// Resolve the relocations for all symbols we currently know about.
void RuntimeDyldImpl::resolveRelocations() {
- // Just iterate over the symbols in our symbol table and assign their
- // addresses.
- StringMap<uint8_t*>::iterator i = SymbolTable.begin();
- StringMap<uint8_t*>::iterator e = SymbolTable.end();
- for (;i != e; ++i)
- reassignSymbolAddress(i->getKey(), i->getValue());
+ // Just iterate over the sections we have and resolve all the relocations
+ // in them. Gross overkill, but it gets the job done.
+ for (int i = 0, e = Sections.size(); i != e; ++i) {
+ reassignSectionAddress(i, SectionLoadAddress[i]);
+ }
+}
+
+void RuntimeDyldImpl::mapSectionAddress(void *LocalAddress,
+ uint64_t TargetAddress) {
+ assert(SectionLocalMemToID.count(LocalAddress) &&
+ "Attempting to remap address of unknown section!");
+ unsigned SectionID = SectionLocalMemToID[LocalAddress];
+ reassignSectionAddress(SectionID, TargetAddress);
}
//===----------------------------------------------------------------------===//
@@ -64,12 +78,36 @@ RuntimeDyld::~RuntimeDyld() {
bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
if (!Dyld) {
- if (RuntimeDyldMachO::isKnownFormat(InputBuffer))
- Dyld = new RuntimeDyldMachO(MM);
- else
- report_fatal_error("Unknown object format!");
+ sys::LLVMFileType type = sys::IdentifyFileType(
+ InputBuffer->getBufferStart(),
+ static_cast<unsigned>(InputBuffer->getBufferSize()));
+ switch (type) {
+ case sys::ELF_Relocatable_FileType:
+ case sys::ELF_Executable_FileType:
+ case sys::ELF_SharedObject_FileType:
+ case sys::ELF_Core_FileType:
+ Dyld = new RuntimeDyldELF(MM);
+ break;
+ case sys::Mach_O_Object_FileType:
+ case sys::Mach_O_Executable_FileType:
+ case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
+ case sys::Mach_O_Core_FileType:
+ case sys::Mach_O_PreloadExecutable_FileType:
+ case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
+ case sys::Mach_O_DynamicLinker_FileType:
+ case sys::Mach_O_Bundle_FileType:
+ case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
+ case sys::Mach_O_DSYMCompanion_FileType:
+ Dyld = new RuntimeDyldMachO(MM);
+ break;
+ case sys::Unknown_FileType:
+ case sys::Bitcode_FileType:
+ case sys::Archive_FileType:
+ case sys::COFF_FileType:
+ report_fatal_error("Incompatible object format!");
+ }
} else {
- if(!Dyld->isCompatibleFormat(InputBuffer))
+ if (!Dyld->isCompatibleFormat(InputBuffer))
report_fatal_error("Incompatible object format!");
}
@@ -84,8 +122,14 @@ void RuntimeDyld::resolveRelocations() {
Dyld->resolveRelocations();
}
-void RuntimeDyld::reassignSymbolAddress(StringRef Name, uint8_t *Addr) {
- Dyld->reassignSymbolAddress(Name, Addr);
+void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
+ uint64_t Addr) {
+ Dyld->reassignSectionAddress(SectionID, Addr);
+}
+
+void RuntimeDyld::mapSectionAddress(void *LocalAddress,
+ uint64_t TargetAddress) {
+ Dyld->mapSectionAddress(LocalAddress, TargetAddress);
}
StringRef RuntimeDyld::getErrorString() {
diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
new file mode 100644
index 0000000..e15b200
--- /dev/null
+++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
@@ -0,0 +1,308 @@
+//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Implementation of ELF support for the MC-JIT runtime dynamic linker.
+//
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "dyld"
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/IntervalMap.h"
+#include "RuntimeDyldELF.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Support/ELF.h"
+#include "llvm/ADT/Triple.h"
+using namespace llvm;
+using namespace llvm::object;
+
+namespace llvm {
+
+namespace {
+
+// FIXME: this function should probably not live here...
+//
+// Returns the name and address of an unrelocated symbol in an ELF section
+void getSymbolInfo(symbol_iterator Sym, uint64_t &Addr, StringRef &Name) {
+ //FIXME: error checking here required to catch corrupt ELF objects...
+ error_code Err = Sym->getName(Name);
+
+ uint64_t AddrInSection;
+ Err = Sym->getAddress(AddrInSection);
+
+ SectionRef empty_section;
+ section_iterator Section(empty_section);
+ Err = Sym->getSection(Section);
+
+ StringRef SectionContents;
+ Section->getContents(SectionContents);
+
+ Addr = reinterpret_cast<uint64_t>(SectionContents.data()) + AddrInSection;
+}
+
+}
+
+bool RuntimeDyldELF::loadObject(MemoryBuffer *InputBuffer) {
+ if (!isCompatibleFormat(InputBuffer))
+ return true;
+
+ OwningPtr<ObjectFile> Obj(ObjectFile::createELFObjectFile(InputBuffer));
+
+ Arch = Obj->getArch();
+
+ // Map address in the Object file image to function names
+ IntervalMap<uint64_t, StringRef>::Allocator A;
+ IntervalMap<uint64_t, StringRef> FuncMap(A);
+
+ // This is a bit of a hack. The ObjectFile we've just loaded reports
+ // section addresses as 0 and doesn't provide access to the section
+ // offset (from which we could calculate the address. Instead,
+ // we're storing the address when it comes up in the ST_Debug case
+ // below.
+ //
+ StringMap<uint64_t> DebugSymbolMap;
+
+ symbol_iterator SymEnd = Obj->end_symbols();
+ error_code Err;
+ for (symbol_iterator Sym = Obj->begin_symbols();
+ Sym != SymEnd; Sym.increment(Err)) {
+ SymbolRef::Type Type;
+ Sym->getType(Type);
+ if (Type == SymbolRef::ST_Function) {
+ StringRef Name;
+ uint64_t Addr;
+ getSymbolInfo(Sym, Addr, Name);
+
+ uint64_t Size;
+ Err = Sym->getSize(Size);
+
+ uint8_t *Start;
+ uint8_t *End;
+ Start = reinterpret_cast<uint8_t*>(Addr);
+ End = reinterpret_cast<uint8_t*>(Addr + Size - 1);
+
+ extractFunction(Name, Start, End);
+ FuncMap.insert(Addr, Addr + Size - 1, Name);
+ } else if (Type == SymbolRef::ST_Debug) {
+ // This case helps us find section addresses
+ StringRef Name;
+ uint64_t Addr;
+ getSymbolInfo(Sym, Addr, Name);
+ DebugSymbolMap[Name] = Addr;
+ }
+ }
+
+ // Iterate through the relocations for this object
+ section_iterator SecEnd = Obj->end_sections();
+ for (section_iterator Sec = Obj->begin_sections();
+ Sec != SecEnd; Sec.increment(Err)) {
+ StringRef SecName;
+ uint64_t SecAddr;
+ Sec->getName(SecName);
+ // Ignore sections that aren't in our map
+ if (DebugSymbolMap.find(SecName) == DebugSymbolMap.end()) {
+ continue;
+ }
+ SecAddr = DebugSymbolMap[SecName];
+ relocation_iterator RelEnd = Sec->end_relocations();
+ for (relocation_iterator Rel = Sec->begin_relocations();
+ Rel != RelEnd; Rel.increment(Err)) {
+ uint64_t RelOffset;
+ uint64_t RelType;
+ int64_t RelAddend;
+ SymbolRef RelSym;
+ StringRef SymName;
+ uint64_t SymAddr;
+ uint64_t SymOffset;
+
+ Rel->getAddress(RelOffset);
+ Rel->getType(RelType);
+ Rel->getAdditionalInfo(RelAddend);
+ Rel->getSymbol(RelSym);
+ RelSym.getName(SymName);
+ RelSym.getAddress(SymAddr);
+ RelSym.getFileOffset(SymOffset);
+
+ // If this relocation is inside a function, we want to store the
+ // function name and a function-relative offset
+ IntervalMap<uint64_t, StringRef>::iterator ContainingFunc
+ = FuncMap.find(SecAddr + RelOffset);
+ if (ContainingFunc.valid()) {
+ // Re-base the relocation to make it relative to the target function
+ RelOffset = (SecAddr + RelOffset) - ContainingFunc.start();
+ Relocations[SymName].push_back(RelocationEntry(ContainingFunc.value(),
+ RelOffset,
+ RelType,
+ RelAddend,
+ true));
+ } else {
+ Relocations[SymName].push_back(RelocationEntry(SecName,
+ RelOffset,
+ RelType,
+ RelAddend,
+ false));
+ }
+ }
+ }
+ return false;
+}
+
+void RuntimeDyldELF::resolveRelocations() {
+ // FIXME: deprecated. should be changed to use the by-section
+ // allocation and relocation scheme.
+
+ // Just iterate over the symbols in our symbol table and assign their
+ // addresses.
+ StringMap<SymbolLoc>::iterator i = SymbolTable.begin();
+ StringMap<SymbolLoc>::iterator e = SymbolTable.end();
+ for (;i != e; ++i) {
+ assert (i->getValue().second == 0 && "non-zero offset in by-function sym!");
+ reassignSymbolAddress(i->getKey(),
+ (uint8_t*)Sections[i->getValue().first].base());
+ }
+}
+
+void RuntimeDyldELF::resolveX86_64Relocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE) {
+ uint8_t *TargetAddr;
+ if (RE.IsFunctionRelative) {
+ StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(RE.Target);
+ assert(Loc != SymbolTable.end() && "Function for relocation not found");
+ TargetAddr =
+ reinterpret_cast<uint8_t*>(Sections[Loc->second.first].base()) +
+ Loc->second.second + RE.Offset;
+ } else {
+ // FIXME: Get the address of the target section and add that to RE.Offset
+ llvm_unreachable("Non-function relocation not implemented yet!");
+ }
+
+ switch (RE.Type) {
+ default: llvm_unreachable("Relocation type not implemented yet!");
+ case ELF::R_X86_64_64: {
+ uint8_t **Target = reinterpret_cast<uint8_t**>(TargetAddr);
+ *Target = Addr + RE.Addend;
+ break;
+ }
+ case ELF::R_X86_64_32:
+ case ELF::R_X86_64_32S: {
+ uint64_t Value = reinterpret_cast<uint64_t>(Addr) + RE.Addend;
+ // FIXME: Handle the possibility of this assertion failing
+ assert((RE.Type == ELF::R_X86_64_32 && !(Value & 0xFFFFFFFF00000000ULL)) ||
+ (RE.Type == ELF::R_X86_64_32S &&
+ (Value & 0xFFFFFFFF00000000ULL) == 0xFFFFFFFF00000000ULL));
+ uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
+ uint32_t *Target = reinterpret_cast<uint32_t*>(TargetAddr);
+ *Target = TruncatedAddr;
+ break;
+ }
+ case ELF::R_X86_64_PC32: {
+ uint32_t *Placeholder = reinterpret_cast<uint32_t*>(TargetAddr);
+ uint64_t RealOffset = *Placeholder +
+ reinterpret_cast<uint64_t>(Addr) +
+ RE.Addend - reinterpret_cast<uint64_t>(TargetAddr);
+ assert((RealOffset & 0xFFFFFFFF) == RealOffset);
+ uint32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
+ *Placeholder = TruncOffset;
+ break;
+ }
+ }
+}
+
+void RuntimeDyldELF::resolveX86Relocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE) {
+ uint8_t *TargetAddr;
+ if (RE.IsFunctionRelative) {
+ StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(RE.Target);
+ assert(Loc != SymbolTable.end() && "Function for relocation not found");
+ TargetAddr =
+ reinterpret_cast<uint8_t*>(Sections[Loc->second.first].base()) +
+ Loc->second.second + RE.Offset;
+ } else {
+ // FIXME: Get the address of the target section and add that to RE.Offset
+ llvm_unreachable("Non-function relocation not implemented yet!");
+ }
+
+ switch (RE.Type) {
+ case ELF::R_386_32: {
+ uint8_t **Target = reinterpret_cast<uint8_t**>(TargetAddr);
+ *Target = Addr + RE.Addend;
+ break;
+ }
+ case ELF::R_386_PC32: {
+ uint32_t *Placeholder = reinterpret_cast<uint32_t*>(TargetAddr);
+ uint32_t RealOffset = *Placeholder + reinterpret_cast<uintptr_t>(Addr) +
+ RE.Addend - reinterpret_cast<uintptr_t>(TargetAddr);
+ *Placeholder = RealOffset;
+ break;
+ }
+ default:
+ // There are other relocation types, but it appears these are the
+ // only ones currently used by the LLVM ELF object writer
+ llvm_unreachable("Relocation type not implemented yet!");
+ }
+}
+
+void RuntimeDyldELF::resolveArmRelocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE) {
+}
+
+void RuntimeDyldELF::resolveRelocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE) {
+ switch (Arch) {
+ case Triple::x86_64:
+ resolveX86_64Relocation(Name, Addr, RE);
+ break;
+ case Triple::x86:
+ resolveX86Relocation(Name, Addr, RE);
+ break;
+ case Triple::arm:
+ resolveArmRelocation(Name, Addr, RE);
+ break;
+ default: llvm_unreachable("Unsupported CPU type!");
+ }
+}
+
+void RuntimeDyldELF::reassignSymbolAddress(StringRef Name, uint8_t *Addr) {
+ // FIXME: deprecated. switch to reassignSectionAddress() instead.
+ //
+ // Actually moving the symbol address requires by-section mapping.
+ assert(Sections[SymbolTable.lookup(Name).first].base() == (void*)Addr &&
+ "Unable to relocate section in by-function JIT allocation model!");
+
+ RelocationList &Relocs = Relocations[Name];
+ for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
+ RelocationEntry &RE = Relocs[i];
+ resolveRelocation(Name, Addr, RE);
+ }
+}
+
+// Assign an address to a symbol name and resolve all the relocations
+// associated with it.
+void RuntimeDyldELF::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
+ // The address to use for relocation resolution is not
+ // the address of the local section buffer. We must be doing
+ // a remote execution environment of some sort. Re-apply any
+ // relocations referencing this section with the given address.
+ //
+ // Addr is a uint64_t because we can't assume the pointer width
+ // of the target is the same as that of the host. Just use a generic
+ // "big enough" type.
+ assert(0);
+}
+
+bool RuntimeDyldELF::isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
+ StringRef Magic = InputBuffer->getBuffer().slice(0, ELF::EI_NIDENT);
+ return (memcmp(Magic.data(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
+}
+} // namespace llvm
diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h
new file mode 100644
index 0000000..e0f7d54
--- /dev/null
+++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h
@@ -0,0 +1,178 @@
+//===-- RuntimeDyldELF.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// ELF support for MC-JIT runtime dynamic linker.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_RUNTIME_DYLD_ELF_H
+#define LLVM_RUNTIME_DYLD_ELF_H
+
+#include "RuntimeDyldImpl.h"
+
+using namespace llvm;
+
+
+namespace llvm {
+class RuntimeDyldELF : public RuntimeDyldImpl {
+ // For each symbol, keep a list of relocations based on it. Anytime
+ // its address is reassigned (the JIT re-compiled the function, e.g.),
+ // the relocations get re-resolved.
+ struct RelocationEntry {
+ // Function or section this relocation is contained in.
+ std::string Target;
+ // Offset into the target function or section for the relocation.
+ uint32_t Offset;
+ // Relocation type
+ uint32_t Type;
+ // Addend encoded in the instruction itself, if any.
+ int32_t Addend;
+ // Has the relocation been recalcuated as an offset within a function?
+ bool IsFunctionRelative;
+ // Has this relocation been resolved previously?
+ bool isResolved;
+
+ RelocationEntry(StringRef t,
+ uint32_t offset,
+ uint32_t type,
+ int32_t addend,
+ bool isFunctionRelative)
+ : Target(t)
+ , Offset(offset)
+ , Type(type)
+ , Addend(addend)
+ , IsFunctionRelative(isFunctionRelative)
+ , isResolved(false) { }
+ };
+ typedef SmallVector<RelocationEntry, 4> RelocationList;
+ StringMap<RelocationList> Relocations;
+ unsigned Arch;
+
+ void resolveRelocations();
+
+ void resolveX86_64Relocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE);
+
+ void resolveX86Relocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE);
+
+ void resolveArmRelocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE);
+
+ void resolveRelocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE);
+
+public:
+ RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
+
+ bool loadObject(MemoryBuffer *InputBuffer);
+
+ void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
+ void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
+
+ bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const;
+};
+
+} // end namespace llvm
+
+#endif
+
+//===-- RuntimeDyldELF.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// ELF support for MC-JIT runtime dynamic linker.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_RUNTIME_DYLD_ELF_H
+#define LLVM_RUNTIME_DYLD_ELF_H
+
+#include "RuntimeDyldImpl.h"
+
+using namespace llvm;
+
+
+namespace llvm {
+class RuntimeDyldELF : public RuntimeDyldImpl {
+ // For each symbol, keep a list of relocations based on it. Anytime
+ // its address is reassigned (the JIT re-compiled the function, e.g.),
+ // the relocations get re-resolved.
+ struct RelocationEntry {
+ // Function or section this relocation is contained in.
+ std::string Target;
+ // Offset into the target function or section for the relocation.
+ uint32_t Offset;
+ // Relocation type
+ uint32_t Type;
+ // Addend encoded in the instruction itself, if any.
+ int32_t Addend;
+ // Has the relocation been recalcuated as an offset within a function?
+ bool IsFunctionRelative;
+ // Has this relocation been resolved previously?
+ bool isResolved;
+
+ RelocationEntry(StringRef t,
+ uint32_t offset,
+ uint32_t type,
+ int32_t addend,
+ bool isFunctionRelative)
+ : Target(t)
+ , Offset(offset)
+ , Type(type)
+ , Addend(addend)
+ , IsFunctionRelative(isFunctionRelative)
+ , isResolved(false) { }
+ };
+ typedef SmallVector<RelocationEntry, 4> RelocationList;
+ StringMap<RelocationList> Relocations;
+ unsigned Arch;
+
+ void resolveRelocations();
+
+ void resolveX86_64Relocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE);
+
+ void resolveX86Relocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE);
+
+ void resolveArmRelocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE);
+
+ void resolveRelocation(StringRef Name,
+ uint8_t *Addr,
+ const RelocationEntry &RE);
+
+public:
+ RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
+
+ bool loadObject(MemoryBuffer *InputBuffer);
+
+ void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
+ void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
+
+ bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const;
+};
+
+} // end namespace llvm
+
+#endif
+
diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
index 7190a3c..28e99be 100644
--- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
+++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
@@ -1,4 +1,4 @@
-//===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
+//===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT --*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@@ -15,12 +15,11 @@
#define LLVM_RUNTIME_DYLD_IMPL_H
#include "llvm/ExecutionEngine/RuntimeDyld.h"
-#include "llvm/Object/MachOObject.h"
+#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
-#include "llvm/Support/Format.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/system_error.h"
@@ -29,7 +28,6 @@
#include "llvm/Support/ErrorHandling.h"
using namespace llvm;
-using namespace llvm::object;
namespace llvm {
class RuntimeDyldImpl {
@@ -40,17 +38,22 @@ protected:
// The MemoryManager to load objects into.
RTDyldMemoryManager *MemMgr;
- // FIXME: This all assumes we're dealing with external symbols for anything
- // explicitly referenced. I.e., we can index by name and things
- // will work out. In practice, this may not be the case, so we
- // should find a way to effectively generalize.
+ // For each section, we have a MemoryBlock of it's data.
+ // Indexed by SectionID.
+ SmallVector<sys::MemoryBlock, 32> Sections;
+ // For each section, the address it will be considered to live at for
+ // relocations. The same as the pointer to the above memory block for hosted
+ // JITs. Indexed by SectionID.
+ SmallVector<uint64_t, 32> SectionLoadAddress;
- // For each function, we have a MemoryBlock of it's instruction data.
- StringMap<sys::MemoryBlock> Functions;
+ // Keep a map of starting local address to the SectionID which references it.
+ // Lookup function for when we assign virtual addresses.
+ DenseMap<void *, unsigned> SectionLocalMemToID;
// Master symbol table. As modules are loaded and external symbols are
- // resolved, their addresses are stored here.
- StringMap<uint8_t*> SymbolTable;
+ // resolved, their addresses are stored here as a SectionID/Offset pair.
+ typedef std::pair<unsigned, uint64_t> SymbolLoc;
+ StringMap<SymbolLoc> SymbolTable;
bool HasError;
std::string ErrorStr;
@@ -62,6 +65,9 @@ protected:
return true;
}
+ uint8_t *getSectionAddress(unsigned SectionID) {
+ return (uint8_t*)Sections[SectionID].base();
+ }
void extractFunction(StringRef Name, uint8_t *StartAddress,
uint8_t *EndAddress);
@@ -75,12 +81,17 @@ public:
void *getSymbolAddress(StringRef Name) {
// FIXME: Just look up as a function for now. Overly simple of course.
// Work in progress.
- return SymbolTable.lookup(Name);
+ if (SymbolTable.find(Name) == SymbolTable.end())
+ return 0;
+ SymbolLoc Loc = SymbolTable.lookup(Name);
+ return getSectionAddress(Loc.first) + Loc.second;
}
- void resolveRelocations();
+ virtual void resolveRelocations();
+
+ virtual void reassignSectionAddress(unsigned SectionID, uint64_t Addr) = 0;
- virtual void reassignSymbolAddress(StringRef Name, uint8_t *Addr) = 0;
+ void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress);
// Is the linker in an error state?
bool hasError() { return HasError; }
@@ -94,58 +105,6 @@ public:
virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0;
};
-
-class RuntimeDyldMachO : public RuntimeDyldImpl {
-
- // For each symbol, keep a list of relocations based on it. Anytime
- // its address is reassigned (the JIT re-compiled the function, e.g.),
- // the relocations get re-resolved.
- struct RelocationEntry {
- std::string Target; // Object this relocation is contained in.
- uint64_t Offset; // Offset into the object for the relocation.
- uint32_t Data; // Second word of the raw macho relocation entry.
- int64_t Addend; // Addend encoded in the instruction itself, if any.
- bool isResolved; // Has this relocation been resolved previously?
-
- RelocationEntry(StringRef t, uint64_t offset, uint32_t data, int64_t addend)
- : Target(t), Offset(offset), Data(data), Addend(addend),
- isResolved(false) {}
- };
- typedef SmallVector<RelocationEntry, 4> RelocationList;
- StringMap<RelocationList> Relocations;
-
- // FIXME: Also keep a map of all the relocations contained in an object. Use
- // this to dynamically answer whether all of the relocations in it have
- // been resolved or not.
-
- bool resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel,
- unsigned Type, unsigned Size);
- bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
- unsigned Type, unsigned Size);
- bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
- unsigned Type, unsigned Size);
-
- bool loadSegment32(const MachOObject *Obj,
- const MachOObject::LoadCommandInfo *SegmentLCI,
- const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
- bool loadSegment64(const MachOObject *Obj,
- const MachOObject::LoadCommandInfo *SegmentLCI,
- const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
-
-public:
- RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
-
- bool loadObject(MemoryBuffer *InputBuffer);
-
- void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
-
- static bool isKnownFormat(const MemoryBuffer *InputBuffer);
-
- bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
- return isKnownFormat(InputBuffer);
- }
-};
-
} // end namespace llvm
diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp
index f5a68c8..c11b2c3 100644
--- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp
+++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp
@@ -1,4 +1,4 @@
-//===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
+//===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
//
// The LLVM Compiler Infrastructure
//
@@ -15,32 +15,30 @@
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/STLExtras.h"
-#include "RuntimeDyldImpl.h"
+#include "RuntimeDyldMachO.h"
using namespace llvm;
using namespace llvm::object;
namespace llvm {
bool RuntimeDyldMachO::
-resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel,
- unsigned Type, unsigned Size) {
+resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel,
+ unsigned Type, unsigned Size, int64_t Addend) {
// This just dispatches to the proper target specific routine.
switch (CPUType) {
- default: assert(0 && "Unsupported CPU type!");
+ default: llvm_unreachable("Unsupported CPU type!");
case mach::CTM_x86_64:
return resolveX86_64Relocation((uintptr_t)Address, (uintptr_t)Value,
- isPCRel, Type, Size);
+ isPCRel, Type, Size, Addend);
case mach::CTM_ARM:
return resolveARMRelocation((uintptr_t)Address, (uintptr_t)Value,
- isPCRel, Type, Size);
+ isPCRel, Type, Size, Addend);
}
- llvm_unreachable("");
}
bool RuntimeDyldMachO::
-resolveX86_64Relocation(uintptr_t Address, uintptr_t Value,
- bool isPCRel, unsigned Type,
- unsigned Size) {
+resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
+ unsigned Type, unsigned Size, int64_t Addend) {
// If the relocation is PC-relative, the value to be encoded is the
// pointer difference.
if (isPCRel)
@@ -51,8 +49,13 @@ resolveX86_64Relocation(uintptr_t Address, uintptr_t Value,
switch(Type) {
default:
llvm_unreachable("Invalid relocation type!");
+ case macho::RIT_X86_64_Signed1:
+ case macho::RIT_X86_64_Signed2:
+ case macho::RIT_X86_64_Signed4:
+ case macho::RIT_X86_64_Signed:
case macho::RIT_X86_64_Unsigned:
case macho::RIT_X86_64_Branch: {
+ Value += Addend;
// Mask in the target value a byte at a time (we don't have an alignment
// guarantee for the target address, so this is safest).
uint8_t *p = (uint8_t*)Address;
@@ -62,22 +65,17 @@ resolveX86_64Relocation(uintptr_t Address, uintptr_t Value,
}
return false;
}
- case macho::RIT_X86_64_Signed:
case macho::RIT_X86_64_GOTLoad:
case macho::RIT_X86_64_GOT:
case macho::RIT_X86_64_Subtractor:
- case macho::RIT_X86_64_Signed1:
- case macho::RIT_X86_64_Signed2:
- case macho::RIT_X86_64_Signed4:
case macho::RIT_X86_64_TLV:
return Error("Relocation type not implemented yet!");
}
- return false;
}
-bool RuntimeDyldMachO::resolveARMRelocation(uintptr_t Address, uintptr_t Value,
- bool isPCRel, unsigned Type,
- unsigned Size) {
+bool RuntimeDyldMachO::
+resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
+ unsigned Type, unsigned Size, int64_t Addend) {
// If the relocation is PC-relative, the value to be encoded is the
// pointer difference.
if (isPCRel) {
@@ -92,7 +90,6 @@ bool RuntimeDyldMachO::resolveARMRelocation(uintptr_t Address, uintptr_t Value,
default:
llvm_unreachable("Invalid relocation type!");
case macho::RIT_Vanilla: {
- llvm_unreachable("Invalid relocation type!");
// Mask in the target value a byte at a time (we don't have an alignment
// guarantee for the target address, so this is safest).
uint8_t *p = (uint8_t*)Address;
@@ -135,84 +132,64 @@ bool RuntimeDyldMachO::
loadSegment32(const MachOObject *Obj,
const MachOObject::LoadCommandInfo *SegmentLCI,
const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
+ // FIXME: This should really be combined w/ loadSegment64. Templatized
+ // function on the 32/64 datatypes maybe?
InMemoryStruct<macho::SegmentLoadCommand> SegmentLC;
Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC);
if (!SegmentLC)
return Error("unable to load segment load command");
+
+ SmallVector<unsigned, 16> SectionMap;
for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
InMemoryStruct<macho::Section> Sect;
Obj->ReadSection(*SegmentLCI, SectNum, Sect);
if (!Sect)
return Error("unable to load section: '" + Twine(SectNum) + "'");
- // FIXME: For the time being, we're only loading text segments.
- if (Sect->Flags != 0x80000400)
- continue;
+ // Allocate memory via the MM for the section.
+ uint8_t *Buffer;
+ uint32_t SectionID = Sections.size();
+ if (Sect->Flags == 0x80000400)
+ Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID);
+ else
+ Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID);
+
+ DEBUG(dbgs() << "Loading "
+ << ((Sect->Flags == 0x80000400) ? "text" : "data")
+ << " (ID #" << SectionID << ")"
+ << " '" << Sect->SegmentName << ","
+ << Sect->Name << "' of size " << Sect->Size
+ << " to address " << Buffer << ".\n");
+
+ // Copy the payload from the object file into the allocated buffer.
+ uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset,
+ SegmentLC->FileSize).data();
+ memcpy(Buffer, Base + Sect->Address, Sect->Size);
- // Address and names of symbols in the section.
- typedef std::pair<uint64_t, StringRef> SymbolEntry;
- SmallVector<SymbolEntry, 64> Symbols;
- // Index of all the names, in this section or not. Used when we're
- // dealing with relocation entries.
- SmallVector<StringRef, 64> SymbolNames;
- for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
- InMemoryStruct<macho::SymbolTableEntry> STE;
- Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
- if (!STE)
- return Error("unable to read symbol: '" + Twine(i) + "'");
- if (STE->SectionIndex > SegmentLC->NumSections)
- return Error("invalid section index for symbol: '" + Twine(i) + "'");
- // Get the symbol name.
- StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
- SymbolNames.push_back(Name);
-
- // Just skip symbols not defined in this section.
- if ((unsigned)STE->SectionIndex - 1 != SectNum)
- continue;
-
- // FIXME: Check the symbol type and flags.
- if (STE->Type != 0xF) // external, defined in this section.
- continue;
- // Flags == 0x8 marks a thumb function for ARM, which is fine as it
- // doesn't require any special handling here.
- // Flags in the upper nibble we don't care about.
- if ((STE->Flags & 0xf) != 0x0 && STE->Flags != 0x8)
- continue;
-
- // Remember the symbol.
- Symbols.push_back(SymbolEntry(STE->Value, Name));
-
- DEBUG(dbgs() << "Function sym: '" << Name << "' @ " <<
- (Sect->Address + STE->Value) << "\n");
- }
- // Sort the symbols by address, just in case they didn't come in that way.
- array_pod_sort(Symbols.begin(), Symbols.end());
+ // Remember what got allocated for this SectionID.
+ Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
+ SectionLocalMemToID[Buffer] = SectionID;
- // If there weren't any functions (odd, but just in case...)
- if (!Symbols.size())
- continue;
+ // By default, the load address of a section is its memory buffer.
+ SectionLoadAddress.push_back((uint64_t)Buffer);
- // Extract the function data.
- uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset,
- SegmentLC->FileSize).data();
- for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) {
- uint64_t StartOffset = Sect->Address + Symbols[i].first;
- uint64_t EndOffset = Symbols[i + 1].first - 1;
- DEBUG(dbgs() << "Extracting function: " << Symbols[i].second
- << " from [" << StartOffset << ", " << EndOffset << "]\n");
- extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset);
- }
- // The last symbol we do after since the end address is calculated
- // differently because there is no next symbol to reference.
- uint64_t StartOffset = Symbols[Symbols.size() - 1].first;
- uint64_t EndOffset = Sect->Size - 1;
- DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second
- << " from [" << StartOffset << ", " << EndOffset << "]\n");
- extractFunction(Symbols[Symbols.size()-1].second,
- Base + StartOffset, Base + EndOffset);
-
- // Now extract the relocation information for each function and process it.
+ // Keep a map of object file section numbers to corresponding SectionIDs
+ // while processing the file.
+ SectionMap.push_back(SectionID);
+ }
+
+ // Process the symbol table.
+ SmallVector<StringRef, 64> SymbolNames;
+ processSymbols32(Obj, SectionMap, SymbolNames, SymtabLC);
+
+ // Process the relocations for each section we're loading.
+ Relocations.grow(Relocations.size() + SegmentLC->NumSections);
+ for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
+ InMemoryStruct<macho::Section> Sect;
+ Obj->ReadSection(*SegmentLCI, SectNum, Sect);
+ if (!Sect)
+ return Error("unable to load section: '" + Twine(SectNum) + "'");
for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
InMemoryStruct<macho::RelocationEntry> RE;
Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
@@ -222,51 +199,53 @@ loadSegment32(const MachOObject *Obj,
// relocation should be applied. We need to translate that into an
// offset into a function since that's our atom.
uint32_t Offset = RE->Word0;
- // Look for the function containing the address. This is used for JIT
- // code, so the number of functions in section is almost always going
- // to be very small (usually just one), so until we have use cases
- // where that's not true, just use a trivial linear search.
- unsigned SymbolNum;
- unsigned NumSymbols = Symbols.size();
- assert(NumSymbols > 0 && Symbols[0].first <= Offset &&
- "No symbol containing relocation!");
- for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum)
- if (Symbols[SymbolNum + 1].first > Offset)
- break;
- // Adjust the offset to be relative to the symbol.
- Offset -= Symbols[SymbolNum].first;
- // Get the name of the symbol containing the relocation.
- StringRef TargetName = SymbolNames[SymbolNum];
-
bool isExtern = (RE->Word1 >> 27) & 1;
+
+ // FIXME: Get the relocation addend from the target address.
+ // FIXME: VERY imporant for internal relocations.
+
// Figure out the source symbol of the relocation. If isExtern is true,
// this relocation references the symbol table, otherwise it references
// a section in the same object, numbered from 1 through NumSections
// (SectionBases is [0, NumSections-1]).
- // FIXME: Some targets (ARM) use internal relocations even for
- // externally visible symbols, if the definition is in the same
- // file as the reference. We need to convert those back to by-name
- // references. We can resolve the address based on the section
- // offset and see if we have a symbol at that address. If we do,
- // use that; otherwise, puke.
- if (!isExtern)
- return Error("Internal relocations not supported.");
uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
- StringRef SourceName = SymbolNames[SourceNum];
-
- // FIXME: Get the relocation addend from the target address.
-
- // Now store the relocation information. Associate it with the source
- // symbol.
- Relocations[SourceName].push_back(RelocationEntry(TargetName,
- Offset,
- RE->Word1,
- 0 /*Addend*/));
- DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset
- << " from '" << SourceName << "(Word1: "
- << format("0x%x", RE->Word1) << ")\n");
+ if (!isExtern) {
+ assert(SourceNum > 0 && "Invalid relocation section number!");
+ unsigned SectionID = SectionMap[SourceNum - 1];
+ unsigned TargetID = SectionMap[SectNum];
+ DEBUG(dbgs() << "Internal relocation at Section #"
+ << TargetID << " + " << Offset
+ << " from Section #"
+ << SectionID << " (Word1: "
+ << format("0x%x", RE->Word1) << ")\n");
+
+ // Store the relocation information. It will get resolved when
+ // the section addresses are assigned.
+ Relocations[SectionID].push_back(RelocationEntry(TargetID,
+ Offset,
+ RE->Word1,
+ 0 /*Addend*/));
+ } else {
+ StringRef SourceName = SymbolNames[SourceNum];
+
+ // Now store the relocation information. Associate it with the source
+ // symbol. Just add it to the unresolved list and let the general
+ // path post-load resolve it if we know where the symbol is.
+ UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
+ Offset,
+ RE->Word1,
+ 0 /*Addend*/));
+ DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
+ << " from '" << SourceName << "(Word1: "
+ << format("0x%x", RE->Word1) << ")\n");
+ }
}
}
+
+ // Resolve the addresses of any symbols that were defined in this segment.
+ for (int i = 0, e = SymbolNames.size(); i != e; ++i)
+ resolveSymbol(SymbolNames[i]);
+
return false;
}
@@ -280,77 +259,59 @@ loadSegment64(const MachOObject *Obj,
if (!Segment64LC)
return Error("unable to load segment load command");
+
+ SmallVector<unsigned, 16> SectionMap;
for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
InMemoryStruct<macho::Section64> Sect;
Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
if (!Sect)
return Error("unable to load section: '" + Twine(SectNum) + "'");
- // FIXME: For the time being, we're only loading text segments.
- if (Sect->Flags != 0x80000400)
- continue;
+ // Allocate memory via the MM for the section.
+ uint8_t *Buffer;
+ uint32_t SectionID = Sections.size();
+ unsigned Align = 1 << Sect->Align; // .o file has log2 alignment.
+ if (Sect->Flags == 0x80000400)
+ Buffer = MemMgr->allocateCodeSection(Sect->Size, Align, SectionID);
+ else
+ Buffer = MemMgr->allocateDataSection(Sect->Size, Align, SectionID);
+
+ DEBUG(dbgs() << "Loading "
+ << ((Sect->Flags == 0x80000400) ? "text" : "data")
+ << " (ID #" << SectionID << ")"
+ << " '" << Sect->SegmentName << ","
+ << Sect->Name << "' of size " << Sect->Size
+ << " (align " << Align << ")"
+ << " to address " << Buffer << ".\n");
+
+ // Copy the payload from the object file into the allocated buffer.
+ uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
+ Segment64LC->FileSize).data();
+ memcpy(Buffer, Base + Sect->Address, Sect->Size);
- // Address and names of symbols in the section.
- typedef std::pair<uint64_t, StringRef> SymbolEntry;
- SmallVector<SymbolEntry, 64> Symbols;
- // Index of all the names, in this section or not. Used when we're
- // dealing with relocation entries.
- SmallVector<StringRef, 64> SymbolNames;
- for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
- InMemoryStruct<macho::Symbol64TableEntry> STE;
- Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
- if (!STE)
- return Error("unable to read symbol: '" + Twine(i) + "'");
- if (STE->SectionIndex > Segment64LC->NumSections)
- return Error("invalid section index for symbol: '" + Twine(i) + "'");
- // Get the symbol name.
- StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
- SymbolNames.push_back(Name);
-
- // Just skip symbols not defined in this section.
- if ((unsigned)STE->SectionIndex - 1 != SectNum)
- continue;
-
- // FIXME: Check the symbol type and flags.
- if (STE->Type != 0xF) // external, defined in this section.
- continue;
- // Flags in the upper nibble we don't care about.
- if ((STE->Flags & 0xf) != 0x0)
- continue;
-
- // Remember the symbol.
- Symbols.push_back(SymbolEntry(STE->Value, Name));
-
- DEBUG(dbgs() << "Function sym: '" << Name << "' @ " <<
- (Sect->Address + STE->Value) << "\n");
- }
- // Sort the symbols by address, just in case they didn't come in that way.
- array_pod_sort(Symbols.begin(), Symbols.end());
+ // Remember what got allocated for this SectionID.
+ Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
+ SectionLocalMemToID[Buffer] = SectionID;
- // If there weren't any functions (odd, but just in case...)
- if (!Symbols.size())
- continue;
+ // By default, the load address of a section is its memory buffer.
+ SectionLoadAddress.push_back((uint64_t)Buffer);
- // Extract the function data.
- uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
- Segment64LC->FileSize).data();
- for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) {
- uint64_t StartOffset = Sect->Address + Symbols[i].first;
- uint64_t EndOffset = Symbols[i + 1].first - 1;
- DEBUG(dbgs() << "Extracting function: " << Symbols[i].second
- << " from [" << StartOffset << ", " << EndOffset << "]\n");
- extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset);
- }
- // The last symbol we do after since the end address is calculated
- // differently because there is no next symbol to reference.
- uint64_t StartOffset = Symbols[Symbols.size() - 1].first;
- uint64_t EndOffset = Sect->Size - 1;
- DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second
- << " from [" << StartOffset << ", " << EndOffset << "]\n");
- extractFunction(Symbols[Symbols.size()-1].second,
- Base + StartOffset, Base + EndOffset);
-
- // Now extract the relocation information for each function and process it.
+ // Keep a map of object file section numbers to corresponding SectionIDs
+ // while processing the file.
+ SectionMap.push_back(SectionID);
+ }
+
+ // Process the symbol table.
+ SmallVector<StringRef, 64> SymbolNames;
+ processSymbols64(Obj, SectionMap, SymbolNames, SymtabLC);
+
+ // Process the relocations for each section we're loading.
+ Relocations.grow(Relocations.size() + Segment64LC->NumSections);
+ for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
+ InMemoryStruct<macho::Section64> Sect;
+ Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
+ if (!Sect)
+ return Error("unable to load section: '" + Twine(SectNum) + "'");
for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
InMemoryStruct<macho::RelocationEntry> RE;
Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
@@ -360,48 +321,142 @@ loadSegment64(const MachOObject *Obj,
// relocation should be applied. We need to translate that into an
// offset into a function since that's our atom.
uint32_t Offset = RE->Word0;
- // Look for the function containing the address. This is used for JIT
- // code, so the number of functions in section is almost always going
- // to be very small (usually just one), so until we have use cases
- // where that's not true, just use a trivial linear search.
- unsigned SymbolNum;
- unsigned NumSymbols = Symbols.size();
- assert(NumSymbols > 0 && Symbols[0].first <= Offset &&
- "No symbol containing relocation!");
- for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum)
- if (Symbols[SymbolNum + 1].first > Offset)
- break;
- // Adjust the offset to be relative to the symbol.
- Offset -= Symbols[SymbolNum].first;
- // Get the name of the symbol containing the relocation.
- StringRef TargetName = SymbolNames[SymbolNum];
-
bool isExtern = (RE->Word1 >> 27) & 1;
+
+ // FIXME: Get the relocation addend from the target address.
+ // FIXME: VERY imporant for internal relocations.
+
// Figure out the source symbol of the relocation. If isExtern is true,
// this relocation references the symbol table, otherwise it references
// a section in the same object, numbered from 1 through NumSections
// (SectionBases is [0, NumSections-1]).
- if (!isExtern)
- return Error("Internal relocations not supported.");
uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
- StringRef SourceName = SymbolNames[SourceNum];
+ if (!isExtern) {
+ assert(SourceNum > 0 && "Invalid relocation section number!");
+ unsigned SectionID = SectionMap[SourceNum - 1];
+ unsigned TargetID = SectionMap[SectNum];
+ DEBUG(dbgs() << "Internal relocation at Section #"
+ << TargetID << " + " << Offset
+ << " from Section #"
+ << SectionID << " (Word1: "
+ << format("0x%x", RE->Word1) << ")\n");
+
+ // Store the relocation information. It will get resolved when
+ // the section addresses are assigned.
+ Relocations[SectionID].push_back(RelocationEntry(TargetID,
+ Offset,
+ RE->Word1,
+ 0 /*Addend*/));
+ } else {
+ StringRef SourceName = SymbolNames[SourceNum];
+
+ // Now store the relocation information. Associate it with the source
+ // symbol. Just add it to the unresolved list and let the general
+ // path post-load resolve it if we know where the symbol is.
+ UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
+ Offset,
+ RE->Word1,
+ 0 /*Addend*/));
+ DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
+ << " from '" << SourceName << "(Word1: "
+ << format("0x%x", RE->Word1) << ")\n");
+ }
+ }
+ }
- // FIXME: Get the relocation addend from the target address.
+ // Resolve the addresses of any symbols that were defined in this segment.
+ for (int i = 0, e = SymbolNames.size(); i != e; ++i)
+ resolveSymbol(SymbolNames[i]);
- // Now store the relocation information. Associate it with the source
- // symbol.
- Relocations[SourceName].push_back(RelocationEntry(TargetName,
- Offset,
- RE->Word1,
- 0 /*Addend*/));
- DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset
- << " from '" << SourceName << "(Word1: "
- << format("0x%x", RE->Word1) << ")\n");
- }
+ return false;
+}
+
+bool RuntimeDyldMachO::
+processSymbols32(const MachOObject *Obj,
+ SmallVectorImpl<unsigned> &SectionMap,
+ SmallVectorImpl<StringRef> &SymbolNames,
+ const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
+ // FIXME: Combine w/ processSymbols64. Factor 64/32 datatype and such.
+ for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
+ InMemoryStruct<macho::SymbolTableEntry> STE;
+ Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
+ if (!STE)
+ return Error("unable to read symbol: '" + Twine(i) + "'");
+ // Get the symbol name.
+ StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
+ SymbolNames.push_back(Name);
+
+ // FIXME: Check the symbol type and flags.
+ if (STE->Type != 0xF) // external, defined in this segment.
+ continue;
+ // Flags in the upper nibble we don't care about.
+ if ((STE->Flags & 0xf) != 0x0)
+ continue;
+
+ // Remember the symbol.
+ uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
+ SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
+
+ DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
+ << (getSectionAddress(SectionID) + STE->Value)
+ << "\n");
}
return false;
}
+bool RuntimeDyldMachO::
+processSymbols64(const MachOObject *Obj,
+ SmallVectorImpl<unsigned> &SectionMap,
+ SmallVectorImpl<StringRef> &SymbolNames,
+ const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
+ for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
+ InMemoryStruct<macho::Symbol64TableEntry> STE;
+ Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
+ if (!STE)
+ return Error("unable to read symbol: '" + Twine(i) + "'");
+ // Get the symbol name.
+ StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
+ SymbolNames.push_back(Name);
+
+ // FIXME: Check the symbol type and flags.
+ if (STE->Type != 0xF) // external, defined in this segment.
+ continue;
+ // Flags in the upper nibble we don't care about.
+ if ((STE->Flags & 0xf) != 0x0)
+ continue;
+
+ // Remember the symbol.
+ uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
+ SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
+
+ DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
+ << (getSectionAddress(SectionID) + STE->Value)
+ << "\n");
+ }
+ return false;
+}
+
+// resolveSymbol - Resolve any relocations to the specified symbol if
+// we know where it lives.
+void RuntimeDyldMachO::resolveSymbol(StringRef Name) {
+ StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name);
+ if (Loc == SymbolTable.end())
+ return;
+
+ RelocationList &Relocs = UnresolvedRelocations[Name];
+ DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n");
+ for (int i = 0, e = Relocs.size(); i != e; ++i) {
+ // Change the relocation to be section relative rather than symbol
+ // relative and move it to the resolved relocation list.
+ RelocationEntry Entry = Relocs[i];
+ Entry.Addend += Loc->second.second;
+ Relocations[Loc->second.first].push_back(Entry);
+ }
+ // FIXME: Keep a worklist of the relocations we've added so that we can
+ // resolve more selectively later.
+ Relocs.clear();
+}
+
bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) {
// If the linker is in an error state, don't do anything.
if (hasError())
@@ -451,7 +506,7 @@ bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) {
if (!SymtabLCI)
return Error("no symbol table found in object");
if (!SegmentLCI)
- return Error("no symbol table found in object");
+ return Error("no segments found in object");
// Read and register the symbol table data.
InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
@@ -486,31 +541,46 @@ bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) {
return true;
}
+ // Assign the addresses of the sections from the object so that any
+ // relocations to them get set properly.
+ // FIXME: This is done directly from the client at the moment. We should
+ // default the values to the local storage, at least when the target arch
+ // is the same as the host arch.
+
return false;
}
// Assign an address to a symbol name and resolve all the relocations
// associated with it.
-void RuntimeDyldMachO::reassignSymbolAddress(StringRef Name, uint8_t *Addr) {
- // Assign the address in our symbol table.
- SymbolTable[Name] = Addr;
-
- RelocationList &Relocs = Relocations[Name];
+void RuntimeDyldMachO::reassignSectionAddress(unsigned SectionID,
+ uint64_t Addr) {
+ // The address to use for relocation resolution is not
+ // the address of the local section buffer. We must be doing
+ // a remote execution environment of some sort. Re-apply any
+ // relocations referencing this section with the given address.
+ //
+ // Addr is a uint64_t because we can't assume the pointer width
+ // of the target is the same as that of the host. Just use a generic
+ // "big enough" type.
+
+ SectionLoadAddress[SectionID] = Addr;
+
+ RelocationList &Relocs = Relocations[SectionID];
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
RelocationEntry &RE = Relocs[i];
- uint8_t *Target = SymbolTable[RE.Target] + RE.Offset;
+ uint8_t *Target = (uint8_t*)Sections[RE.SectionID].base() + RE.Offset;
bool isPCRel = (RE.Data >> 24) & 1;
unsigned Type = (RE.Data >> 28) & 0xf;
unsigned Size = 1 << ((RE.Data >> 25) & 3);
- DEBUG(dbgs() << "Resolving relocation at '" << RE.Target
- << "' + " << RE.Offset << " (" << format("%p", Target) << ")"
- << " from '" << Name << " (" << format("%p", Addr) << ")"
+ DEBUG(dbgs() << "Resolving relocation at Section #" << RE.SectionID
+ << " + " << RE.Offset << " (" << format("%p", Target) << ")"
+ << " from Section #" << SectionID << " (" << format("%p", Addr) << ")"
<< "(" << (isPCRel ? "pcrel" : "absolute")
- << ", type: " << Type << ", Size: " << Size << ").\n");
+ << ", type: " << Type << ", Size: " << Size << ", Addend: "
+ << RE.Addend << ").\n");
- resolveRelocation(Target, Addr, isPCRel, Type, Size);
- RE.isResolved = true;
+ resolveRelocation(Target, Addr, isPCRel, Type, Size, RE.Addend);
}
}
diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h
new file mode 100644
index 0000000..5798981
--- /dev/null
+++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h
@@ -0,0 +1,94 @@
+//===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-=//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// MachO support for MC-JIT runtime dynamic linker.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_RUNTIME_DYLD_MACHO_H
+#define LLVM_RUNTIME_DYLD_MACHO_H
+
+#include "llvm/ADT/IndexedMap.h"
+#include "llvm/Object/MachOObject.h"
+#include "llvm/Support/Format.h"
+#include "RuntimeDyldImpl.h"
+
+using namespace llvm;
+using namespace llvm::object;
+
+
+namespace llvm {
+class RuntimeDyldMachO : public RuntimeDyldImpl {
+
+ // For each symbol, keep a list of relocations based on it. Anytime
+ // its address is reassigned (the JIT re-compiled the function, e.g.),
+ // the relocations get re-resolved.
+ // The symbol (or section) the relocation is sourced from is the Key
+ // in the relocation list where it's stored.
+ struct RelocationEntry {
+ unsigned SectionID; // Section the relocation is contained in.
+ uint64_t Offset; // Offset into the section for the relocation.
+ uint32_t Data; // Second word of the raw macho relocation entry.
+ int64_t Addend; // Addend encoded in the instruction itself, if any,
+ // plus the offset into the source section for
+ // the symbol once the relocation is resolvable.
+
+ RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend)
+ : SectionID(id), Offset(offset), Data(data), Addend(addend) {}
+ };
+ typedef SmallVector<RelocationEntry, 4> RelocationList;
+ // Relocations to sections already loaded. Indexed by SectionID which is the
+ // source of the address. The target where the address will be writen is
+ // SectionID/Offset in the relocation itself.
+ IndexedMap<RelocationList> Relocations;
+ // Relocations to symbols that are not yet resolved. Must be external
+ // relocations by definition. Indexed by symbol name.
+ StringMap<RelocationList> UnresolvedRelocations;
+
+ bool resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel,
+ unsigned Type, unsigned Size, int64_t Addend);
+ bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
+ unsigned Type, unsigned Size, int64_t Addend);
+ bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
+ unsigned Type, unsigned Size, int64_t Addend);
+
+ bool loadSegment32(const MachOObject *Obj,
+ const MachOObject::LoadCommandInfo *SegmentLCI,
+ const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
+ bool loadSegment64(const MachOObject *Obj,
+ const MachOObject::LoadCommandInfo *SegmentLCI,
+ const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
+ bool processSymbols32(const MachOObject *Obj,
+ SmallVectorImpl<unsigned> &SectionMap,
+ SmallVectorImpl<StringRef> &SymbolNames,
+ const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
+ bool processSymbols64(const MachOObject *Obj,
+ SmallVectorImpl<unsigned> &SectionMap,
+ SmallVectorImpl<StringRef> &SymbolNames,
+ const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
+
+ void resolveSymbol(StringRef Name);
+
+public:
+ RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
+
+ bool loadObject(MemoryBuffer *InputBuffer);
+
+ void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
+
+ static bool isKnownFormat(const MemoryBuffer *InputBuffer);
+
+ bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
+ return isKnownFormat(InputBuffer);
+ }
+};
+
+} // end namespace llvm
+
+#endif