summaryrefslogtreecommitdiffstats
path: root/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h')
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h143
1 files changed, 87 insertions, 56 deletions
diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
index 2dea13f..c38ca69 100644
--- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
+++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
@@ -14,60 +14,83 @@
#ifndef LLVM_RUNTIME_DYLD_IMPL_H
#define LLVM_RUNTIME_DYLD_IMPL_H
+#include "ObjectImage.h"
#include "llvm/ExecutionEngine/RuntimeDyld.h"
-#include "llvm/Object/ObjectFile.h"
#include "llvm/ADT/DenseMap.h"
-#include "llvm/ADT/StringMap.h"
-#include "llvm/ADT/Twine.h"
#include "llvm/ADT/SmallVector.h"
-#include "llvm/Support/Memory.h"
-#include "llvm/Support/MemoryBuffer.h"
-#include "llvm/Support/system_error.h"
-#include "llvm/Support/raw_ostream.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
-#include "llvm/ADT/Triple.h"
-#include <map>
#include "llvm/Support/Format.h"
-#include "ObjectImage.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/system_error.h"
+#include <map>
using namespace llvm;
using namespace llvm::object;
namespace llvm {
+class MemoryBuffer;
+class Twine;
+
+
+/// SectionEntry - represents a section emitted into memory by the dynamic
+/// linker.
class SectionEntry {
public:
- uint8_t* Address;
+ /// Address - address in the linker's memory where the section resides.
+ uint8_t *Address;
+
+ /// Size - section size.
size_t Size;
- uint64_t LoadAddress; // 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.
- uintptr_t StubOffset; // It's used for architecturies with stub
- // functions for far relocations like ARM.
- uintptr_t ObjAddress; // Section address in object file. It's use for
- // calculate MachO relocation addend
- SectionEntry(uint8_t* address, size_t size, uintptr_t stubOffset,
+
+ /// LoadAddress - the address of the section in the target process's memory.
+ /// Used for situations in which JIT-ed code is being executed in the address
+ /// space of a separate process. If the code executes in the same address
+ /// space where it was JIT-ed, this just equals Address.
+ uint64_t LoadAddress;
+
+ /// StubOffset - used for architectures with stub functions for far
+ /// relocations (like ARM).
+ uintptr_t StubOffset;
+
+ /// ObjAddress - address of the section in the in-memory object file. Used
+ /// for calculating relocations in some object formats (like MachO).
+ uintptr_t ObjAddress;
+
+ SectionEntry(uint8_t *address, size_t size, uintptr_t stubOffset,
uintptr_t objAddress)
: Address(address), Size(size), LoadAddress((uintptr_t)address),
StubOffset(stubOffset), ObjAddress(objAddress) {}
};
+/// RelocationEntry - used to represent relocations internally in the dynamic
+/// linker.
class RelocationEntry {
public:
- unsigned SectionID; // Section the relocation is contained in.
- uintptr_t Offset; // Offset into the section for the relocation.
- uint32_t Data; // Relocatino data. Including type of relocation
- // and another flags and parameners from
- intptr_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) {}
+ /// SectionID - the section this relocation points to.
+ unsigned SectionID;
+
+ /// Offset - offset into the section.
+ uintptr_t Offset;
+
+ /// RelType - relocation type.
+ uint32_t RelType;
+
+ /// Addend - the relocation addend encoded in the instruction itself. Also
+ /// used to make a relocation section relative instead of symbol relative.
+ intptr_t Addend;
+
+ RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend)
+ : SectionID(id), Offset(offset), RelType(type), Addend(addend) {}
};
-// Raw relocation data from object file
+/// ObjRelocationInfo - relocation information as read from the object file.
+/// Used to pass around data taken from object::RelocationRef, together with
+/// the section to which the relocation points (represented by a SectionID).
class ObjRelocationInfo {
public:
unsigned SectionID;
@@ -97,7 +120,8 @@ protected:
// The MemoryManager to load objects into.
RTDyldMemoryManager *MemMgr;
- // A list of emmitted sections.
+ // A list of all sections emitted by the dynamic linker. These sections are
+ // referenced in the code by means of their index in this list - SectionID.
typedef SmallVector<SectionEntry, 64> SectionList;
SectionList Sections;
@@ -105,11 +129,11 @@ protected:
// references it.
typedef std::map<SectionRef, unsigned> ObjSectionToIDMap;
- // Master symbol table. As modules are loaded and external symbols are
- // resolved, their addresses are stored here as a SectionID/Offset pair.
+ // A global symbol table for symbols from all loaded modules. Maps the
+ // symbol name to a (SectionID, offset in section) pair.
typedef std::pair<unsigned, uintptr_t> SymbolLoc;
- StringMap<SymbolLoc> SymbolTable;
- typedef DenseMap<const char*, SymbolLoc> LocalSymbolMap;
+ typedef StringMap<SymbolLoc> SymbolTableMap;
+ SymbolTableMap GlobalSymbolTable;
// Keep a map of common symbols to their sizes
typedef std::map<SymbolRef, unsigned> CommonSymbolMap;
@@ -121,12 +145,14 @@ protected:
// in the relocation list where it's stored.
typedef SmallVector<RelocationEntry, 64> 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
+ // source of the address. The target where the address will be written is
// SectionID/Offset in the relocation itself.
DenseMap<unsigned, RelocationList> Relocations;
- // Relocations to external symbols that are not yet resolved.
- // Indexed by symbol name.
- StringMap<RelocationList> SymbolRelocations;
+
+ // Relocations to external symbols that are not yet resolved. Symbols are
+ // external when they aren't found in the global symbol table of all loaded
+ // modules. This map is indexed by symbol name.
+ StringMap<RelocationList> ExternalSymbolRelocations;
typedef std::map<RelocationValueRef, uintptr_t> StubMap;
@@ -153,16 +179,17 @@ protected:
return (uint8_t*)Sections[SectionID].Address;
}
- /// \brief Emits a section containing common symbols.
- /// \return SectionID.
- unsigned emitCommonSymbols(ObjectImage &Obj,
- const CommonSymbolMap &Map,
- uint64_t TotalSize,
- LocalSymbolMap &Symbols);
+ /// \brief Given the common symbols discovered in the object file, emit a
+ /// new section for them and update the symbol mappings in the object and
+ /// symbol table.
+ void emitCommonSymbols(ObjectImage &Obj,
+ const CommonSymbolMap &CommonSymbols,
+ uint64_t TotalSize,
+ SymbolTableMap &SymbolTable);
/// \brief Emits section data from the object file to the MemoryManager.
/// \param IsCode if it's true then allocateCodeSection() will be
- /// used for emmits, else allocateDataSection() will be used.
+ /// used for emits, else allocateDataSection() will be used.
/// \return SectionID.
unsigned emitSection(ObjectImage &Obj,
const SectionRef &Section,
@@ -178,10 +205,12 @@ protected:
bool IsCode,
ObjSectionToIDMap &LocalSections);
- /// \brief If Value.SymbolName is NULL then store relocation to the
- /// Relocations, else store it in the SymbolRelocations.
- void AddRelocation(const RelocationValueRef &Value, unsigned SectionID,
- uintptr_t Offset, uint32_t RelType);
+ // \brief Add a relocation entry that uses the given section.
+ void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID);
+
+ // \brief Add a relocation entry that uses the given symbol. This symbol may
+ // be found in the global symbol table, or it may be external.
+ void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName);
/// \brief Emits long jump instruction to Addr.
/// \return Pointer to the memory area for emitting target address.
@@ -203,14 +232,16 @@ protected:
uint32_t Type,
int64_t Addend) = 0;
- /// \brief Parses the object file relocation and store it to Relocations
- /// or SymbolRelocations. Its depend from object file type.
+ /// \brief Parses the object file relocation and stores it to Relocations
+ /// or SymbolRelocations (this depends on the object file type).
virtual void processRelocationRef(const ObjRelocationInfo &Rel,
ObjectImage &Obj,
ObjSectionToIDMap &ObjSectionToID,
- LocalSymbolMap &Symbols, StubMap &Stubs) = 0;
+ const SymbolTableMap &Symbols,
+ StubMap &Stubs) = 0;
- void resolveSymbols();
+ /// \brief Resolve relocations to external symbols.
+ void resolveExternalSymbols();
virtual ObjectImage *createObjectImage(const MemoryBuffer *InputBuffer);
virtual void handleObjectLoaded(ObjectImage *Obj)
{
@@ -228,9 +259,9 @@ public:
void *getSymbolAddress(StringRef Name) {
// FIXME: Just look up as a function for now. Overly simple of course.
// Work in progress.
- if (SymbolTable.find(Name) == SymbolTable.end())
+ if (GlobalSymbolTable.find(Name) == GlobalSymbolTable.end())
return 0;
- SymbolLoc Loc = SymbolTable.lookup(Name);
+ SymbolLoc Loc = GlobalSymbolTable.lookup(Name);
return getSectionAddress(Loc.first) + Loc.second;
}
OpenPOWER on IntegriCloud