2012-01-17 07:50:58 +08:00
|
|
|
//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
|
2012-01-16 16:56:09 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-10-03 05:18:39 +08:00
|
|
|
#include "RuntimeDyldELF.h"
|
2015-04-14 10:10:35 +08:00
|
|
|
#include "RuntimeDyldCheckerImpl.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/IntervalMap.h"
|
2012-01-16 16:56:09 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-01-16 16:56:09 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2014-11-27 00:54:40 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2013-08-09 06:27:13 +08:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Support/ELF.h"
|
2014-08-28 07:06:08 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2014-01-08 12:09:09 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-11-27 00:54:40 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2014-01-08 12:09:09 +08:00
|
|
|
|
2012-01-16 16:56:09 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
2014-04-22 11:04:17 +08:00
|
|
|
#define DEBUG_TYPE "dyld"
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
static inline std::error_code check(std::error_code Err) {
|
2012-10-25 21:13:48 +08:00
|
|
|
if (Err) {
|
|
|
|
report_fatal_error(Err.message());
|
|
|
|
}
|
|
|
|
return Err;
|
|
|
|
}
|
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
namespace {
|
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
|
2013-04-18 05:20:55 +08:00
|
|
|
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
|
2012-04-17 06:12:58 +08:00
|
|
|
|
2013-01-15 15:44:25 +08:00
|
|
|
typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
|
|
|
|
typedef Elf_Sym_Impl<ELFT> Elf_Sym;
|
2014-03-22 04:28:42 +08:00
|
|
|
typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
|
|
|
|
typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
|
2012-04-17 06:12:58 +08:00
|
|
|
|
2013-01-15 15:44:25 +08:00
|
|
|
typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
|
2012-04-17 06:12:58 +08:00
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
|
2012-04-17 06:12:58 +08:00
|
|
|
|
|
|
|
public:
|
2014-08-20 02:44:46 +08:00
|
|
|
DyldELFObject(MemoryBufferRef Wrapper, std::error_code &ec);
|
2012-04-17 06:12:58 +08:00
|
|
|
|
|
|
|
void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
|
2014-11-27 00:54:40 +08:00
|
|
|
|
|
|
|
void updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr);
|
2012-04-17 06:12:58 +08:00
|
|
|
|
2012-07-28 01:52:42 +08:00
|
|
|
// Methods for type inquiry through isa, cast and dyn_cast
|
2012-04-17 06:12:58 +08:00
|
|
|
static inline bool classof(const Binary *v) {
|
2014-03-22 04:28:42 +08:00
|
|
|
return (isa<ELFObjectFile<ELFT>>(v) &&
|
|
|
|
classof(cast<ELFObjectFile<ELFT>>(v)));
|
2012-04-17 06:12:58 +08:00
|
|
|
}
|
2014-03-22 04:28:42 +08:00
|
|
|
static inline bool classof(const ELFObjectFile<ELFT> *v) {
|
2012-04-17 06:12:58 +08:00
|
|
|
return v->isDyldType();
|
|
|
|
}
|
2014-11-26 23:27:39 +08:00
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
};
|
2014-11-26 23:27:39 +08:00
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
|
2012-04-17 06:12:58 +08:00
|
|
|
|
2012-10-03 05:18:39 +08:00
|
|
|
// The MemoryBuffer passed into this constructor is just a wrapper around the
|
|
|
|
// actual memory. Ultimately, the Binary parent class will take ownership of
|
|
|
|
// this MemoryBuffer object but not the underlying memory.
|
2014-03-22 04:28:42 +08:00
|
|
|
template <class ELFT>
|
2014-08-20 02:44:46 +08:00
|
|
|
DyldELFObject<ELFT>::DyldELFObject(MemoryBufferRef Wrapper, std::error_code &EC)
|
|
|
|
: ELFObjectFile<ELFT>(Wrapper, EC) {
|
2012-04-17 06:12:58 +08:00
|
|
|
this->isDyldELFObject = true;
|
|
|
|
}
|
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
template <class ELFT>
|
2013-01-15 15:44:25 +08:00
|
|
|
void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
|
|
|
|
uint64_t Addr) {
|
2012-04-17 06:12:58 +08:00
|
|
|
DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
|
2014-03-22 04:28:42 +08:00
|
|
|
Elf_Shdr *shdr =
|
|
|
|
const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
|
2012-04-17 06:12:58 +08:00
|
|
|
|
|
|
|
// This assumes the address passed in matches the target address bitness
|
|
|
|
// The template-based type cast handles everything else.
|
|
|
|
shdr->sh_addr = static_cast<addr_type>(Addr);
|
|
|
|
}
|
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
template <class ELFT>
|
2013-01-15 15:44:25 +08:00
|
|
|
void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
|
|
|
|
uint64_t Addr) {
|
2012-04-17 06:12:58 +08:00
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
Elf_Sym *sym = const_cast<Elf_Sym *>(
|
|
|
|
ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
|
2012-04-17 06:12:58 +08:00
|
|
|
|
|
|
|
// This assumes the address passed in matches the target address bitness
|
|
|
|
// The template-based type cast handles everything else.
|
|
|
|
sym->st_value = static_cast<addr_type>(Addr);
|
|
|
|
}
|
|
|
|
|
2015-05-22 18:11:07 +08:00
|
|
|
class LoadedELFObjectInfo
|
|
|
|
: public RuntimeDyld::LoadedObjectInfoHelper<LoadedELFObjectInfo> {
|
2014-11-27 00:54:40 +08:00
|
|
|
public:
|
|
|
|
LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
|
|
|
|
unsigned EndIdx)
|
2015-05-22 18:11:07 +08:00
|
|
|
: LoadedObjectInfoHelper(RTDyld, BeginIdx, EndIdx) {}
|
2014-11-27 00:54:40 +08:00
|
|
|
|
|
|
|
OwningBinary<ObjectFile>
|
|
|
|
getObjectForDebug(const ObjectFile &Obj) const override;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ELFT>
|
|
|
|
std::unique_ptr<DyldELFObject<ELFT>>
|
|
|
|
createRTDyldELFObject(MemoryBufferRef Buffer,
|
|
|
|
const LoadedELFObjectInfo &L,
|
|
|
|
std::error_code &ec) {
|
|
|
|
typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
|
|
|
|
|
|
|
|
std::unique_ptr<DyldELFObject<ELFT>> Obj =
|
|
|
|
llvm::make_unique<DyldELFObject<ELFT>>(Buffer, ec);
|
|
|
|
|
|
|
|
// Iterate over all sections in the object.
|
|
|
|
for (const auto &Sec : Obj->sections()) {
|
|
|
|
StringRef SectionName;
|
|
|
|
Sec.getName(SectionName);
|
|
|
|
if (SectionName != "") {
|
|
|
|
DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
|
|
|
|
Elf_Shdr *shdr = const_cast<Elf_Shdr *>(
|
|
|
|
reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
|
|
|
|
|
|
|
|
if (uint64_t SecLoadAddr = L.getSectionLoadAddress(SectionName)) {
|
|
|
|
// This assumes that the address passed in matches the target address
|
|
|
|
// bitness. The template-based type cast handles everything else.
|
|
|
|
shdr->sh_addr = static_cast<addr_type>(SecLoadAddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
OwningBinary<ObjectFile> createELFDebugObject(const ObjectFile &Obj,
|
|
|
|
const LoadedELFObjectInfo &L) {
|
|
|
|
assert(Obj.isELF() && "Not an ELF object file.");
|
|
|
|
|
|
|
|
std::unique_ptr<MemoryBuffer> Buffer =
|
|
|
|
MemoryBuffer::getMemBufferCopy(Obj.getData(), Obj.getFileName());
|
|
|
|
|
|
|
|
std::error_code ec;
|
|
|
|
|
|
|
|
std::unique_ptr<ObjectFile> DebugObj;
|
|
|
|
if (Obj.getBytesInAddress() == 4 && Obj.isLittleEndian()) {
|
2015-06-02 20:05:27 +08:00
|
|
|
typedef ELFType<support::little, false> ELF32LE;
|
2014-11-27 00:54:40 +08:00
|
|
|
DebugObj = createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), L, ec);
|
|
|
|
} else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian()) {
|
2015-06-02 20:05:27 +08:00
|
|
|
typedef ELFType<support::big, false> ELF32BE;
|
2014-11-27 00:54:40 +08:00
|
|
|
DebugObj = createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), L, ec);
|
|
|
|
} else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian()) {
|
2015-06-02 20:05:27 +08:00
|
|
|
typedef ELFType<support::big, true> ELF64BE;
|
2014-11-27 00:54:40 +08:00
|
|
|
DebugObj = createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), L, ec);
|
|
|
|
} else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian()) {
|
2015-06-02 20:05:27 +08:00
|
|
|
typedef ELFType<support::little, true> ELF64LE;
|
2014-11-27 00:54:40 +08:00
|
|
|
DebugObj = createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), L, ec);
|
|
|
|
} else
|
|
|
|
llvm_unreachable("Unexpected ELF format");
|
|
|
|
|
|
|
|
assert(!ec && "Could not construct copy ELF object file");
|
|
|
|
|
|
|
|
return OwningBinary<ObjectFile>(std::move(DebugObj), std::move(Buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
OwningBinary<ObjectFile>
|
|
|
|
LoadedELFObjectInfo::getObjectForDebug(const ObjectFile &Obj) const {
|
|
|
|
return createELFDebugObject(Obj, *this);
|
|
|
|
}
|
|
|
|
|
2012-04-17 06:12:58 +08:00
|
|
|
} // namespace
|
|
|
|
|
2012-01-16 16:56:09 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
|
|
|
|
RuntimeDyld::SymbolResolver &Resolver)
|
2015-04-14 10:10:35 +08:00
|
|
|
: RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {}
|
2014-11-27 00:54:40 +08:00
|
|
|
RuntimeDyldELF::~RuntimeDyldELF() {}
|
|
|
|
|
2013-10-12 05:25:48 +08:00
|
|
|
void RuntimeDyldELF::registerEHFrames() {
|
|
|
|
for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
|
|
|
|
SID EHFrameSID = UnregisteredEHFrameSections[i];
|
|
|
|
uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
|
|
|
|
uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
|
|
|
|
size_t EHFrameSize = Sections[EHFrameSID].Size;
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
|
2013-10-16 08:14:21 +08:00
|
|
|
RegisteredEHFrameSections.push_back(EHFrameSID);
|
2013-05-06 04:43:10 +08:00
|
|
|
}
|
2013-10-12 05:25:48 +08:00
|
|
|
UnregisteredEHFrameSections.clear();
|
2013-05-06 04:43:10 +08:00
|
|
|
}
|
|
|
|
|
2013-10-16 08:14:21 +08:00
|
|
|
void RuntimeDyldELF::deregisterEHFrames() {
|
|
|
|
for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
|
|
|
|
SID EHFrameSID = RegisteredEHFrameSections[i];
|
|
|
|
uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
|
|
|
|
uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
|
|
|
|
size_t EHFrameSize = Sections[EHFrameSID].Size;
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
MemMgr.deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
|
2013-10-16 08:14:21 +08:00
|
|
|
}
|
|
|
|
RegisteredEHFrameSections.clear();
|
|
|
|
}
|
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
|
|
|
|
RuntimeDyldELF::loadObject(const object::ObjectFile &O) {
|
|
|
|
unsigned SectionStartIdx, SectionEndIdx;
|
|
|
|
std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
|
|
|
|
return llvm::make_unique<LoadedELFObjectInfo>(*this, SectionStartIdx,
|
|
|
|
SectionEndIdx);
|
2014-01-08 12:09:09 +08:00
|
|
|
}
|
|
|
|
|
2012-11-03 03:45:23 +08:00
|
|
|
void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t Offset, uint64_t Value,
|
|
|
|
uint32_t Type, int64_t Addend,
|
2013-08-20 07:27:43 +08:00
|
|
|
uint64_t SymOffset) {
|
2012-03-31 00:45:19 +08:00
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Relocation type not implemented yet!");
|
2014-03-22 04:28:42 +08:00
|
|
|
break;
|
2012-01-16 16:56:09 +08:00
|
|
|
case ELF::R_X86_64_64: {
|
2014-08-28 07:06:08 +08:00
|
|
|
support::ulittle64_t::ref(Section.Address + Offset) = Value + Addend;
|
2014-03-22 04:28:42 +08:00
|
|
|
DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
|
2014-08-28 07:06:08 +08:00
|
|
|
<< format("%p\n", Section.Address + Offset));
|
2012-01-16 16:56:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_X86_64_32:
|
|
|
|
case ELF::R_X86_64_32S: {
|
2012-03-31 00:45:19 +08:00
|
|
|
Value += Addend;
|
2012-07-28 04:30:12 +08:00
|
|
|
assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
|
2013-01-05 04:36:28 +08:00
|
|
|
(Type == ELF::R_X86_64_32S &&
|
2014-03-22 04:28:42 +08:00
|
|
|
((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
|
2012-01-16 16:56:09 +08:00
|
|
|
uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
|
2014-08-28 07:06:08 +08:00
|
|
|
support::ulittle32_t::ref(Section.Address + Offset) = TruncatedAddr;
|
2014-03-22 04:28:42 +08:00
|
|
|
DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
|
2014-08-28 07:06:08 +08:00
|
|
|
<< format("%p\n", Section.Address + Offset));
|
2012-01-16 16:56:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_X86_64_PC32: {
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t FinalAddress = Section.LoadAddress + Offset;
|
2015-04-14 10:10:35 +08:00
|
|
|
int64_t RealOffset = Value + Addend - FinalAddress;
|
2015-05-16 04:32:25 +08:00
|
|
|
assert(isInt<32>(RealOffset));
|
2012-03-31 00:45:19 +08:00
|
|
|
int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
|
2014-08-28 07:06:08 +08:00
|
|
|
support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset;
|
2012-01-16 16:56:09 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-08-20 07:27:43 +08:00
|
|
|
case ELF::R_X86_64_PC64: {
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t FinalAddress = Section.LoadAddress + Offset;
|
2015-04-14 10:10:35 +08:00
|
|
|
int64_t RealOffset = Value + Addend - FinalAddress;
|
|
|
|
support::ulittle64_t::ref(Section.Address + Offset) = RealOffset;
|
2013-08-20 07:27:43 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-01-16 16:56:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-03 03:45:23 +08:00
|
|
|
void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t Offset, uint32_t Value,
|
|
|
|
uint32_t Type, int32_t Addend) {
|
2012-03-31 00:45:19 +08:00
|
|
|
switch (Type) {
|
2012-01-16 16:56:09 +08:00
|
|
|
case ELF::R_386_32: {
|
2015-05-02 04:21:45 +08:00
|
|
|
support::ulittle32_t::ref(Section.Address + Offset) = Value + Addend;
|
2012-01-16 16:56:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_386_PC32: {
|
2014-03-22 04:28:42 +08:00
|
|
|
uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
|
2015-05-02 04:21:45 +08:00
|
|
|
uint32_t RealOffset = Value + Addend - FinalAddress;
|
2014-08-28 07:06:08 +08:00
|
|
|
support::ulittle32_t::ref(Section.Address + Offset) = RealOffset;
|
2012-01-16 16:56:09 +08:00
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
}
|
|
|
|
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!");
|
|
|
|
break;
|
2012-01-16 16:56:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-05 04:13:59 +08:00
|
|
|
void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t Offset, uint64_t Value,
|
|
|
|
uint32_t Type, int64_t Addend) {
|
|
|
|
uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset);
|
2013-05-05 04:13:59 +08:00
|
|
|
uint64_t FinalAddress = Section.LoadAddress + Offset;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
|
|
|
|
<< format("%llx", Section.Address + Offset)
|
2014-03-22 04:28:42 +08:00
|
|
|
<< " FinalAddress: 0x" << format("%llx", FinalAddress)
|
|
|
|
<< " Value: 0x" << format("%llx", Value) << " Type: 0x"
|
|
|
|
<< format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
|
2013-05-05 04:13:59 +08:00
|
|
|
<< "\n");
|
|
|
|
|
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Relocation type not implemented yet!");
|
|
|
|
break;
|
2013-05-05 04:14:14 +08:00
|
|
|
case ELF::R_AARCH64_ABS64: {
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t *TargetPtr =
|
|
|
|
reinterpret_cast<uint64_t *>(Section.Address + Offset);
|
2013-05-05 04:14:14 +08:00
|
|
|
*TargetPtr = Value + Addend;
|
|
|
|
break;
|
|
|
|
}
|
2013-05-19 23:39:03 +08:00
|
|
|
case ELF::R_AARCH64_PREL32: {
|
2013-05-05 04:13:59 +08:00
|
|
|
uint64_t Result = Value + Addend - FinalAddress;
|
2013-08-09 06:27:13 +08:00
|
|
|
assert(static_cast<int64_t>(Result) >= INT32_MIN &&
|
2013-05-05 04:13:59 +08:00
|
|
|
static_cast<int64_t>(Result) <= UINT32_MAX);
|
|
|
|
*TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
|
|
|
|
break;
|
|
|
|
}
|
2013-05-05 04:14:09 +08:00
|
|
|
case ELF::R_AARCH64_CALL26: // fallthrough
|
|
|
|
case ELF::R_AARCH64_JUMP26: {
|
|
|
|
// Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
|
|
|
|
// calculation.
|
|
|
|
uint64_t BranchImm = Value + Addend - FinalAddress;
|
|
|
|
|
|
|
|
// "Check that -2^27 <= result < 2^27".
|
2015-05-16 04:32:25 +08:00
|
|
|
assert(isInt<28>(BranchImm));
|
2013-05-19 23:39:03 +08:00
|
|
|
|
|
|
|
// AArch64 code is emitted with .rela relocations. The data already in any
|
|
|
|
// bits affected by the relocation on entry is garbage.
|
|
|
|
*TargetPtr &= 0xfc000000U;
|
2013-05-05 04:14:09 +08:00
|
|
|
// Immediate goes in bits 25:0 of B and BL.
|
|
|
|
*TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
|
|
|
|
break;
|
|
|
|
}
|
2013-05-05 04:14:04 +08:00
|
|
|
case ELF::R_AARCH64_MOVW_UABS_G3: {
|
|
|
|
uint64_t Result = Value + Addend;
|
2013-05-19 23:39:03 +08:00
|
|
|
|
|
|
|
// AArch64 code is emitted with .rela relocations. The data already in any
|
|
|
|
// bits affected by the relocation on entry is garbage.
|
2013-07-25 20:42:52 +08:00
|
|
|
*TargetPtr &= 0xffe0001fU;
|
2013-05-05 04:14:04 +08:00
|
|
|
// Immediate goes in bits 20:5 of MOVZ/MOVK instruction
|
|
|
|
*TargetPtr |= Result >> (48 - 5);
|
2013-07-02 03:23:10 +08:00
|
|
|
// Shift must be "lsl #48", in bits 22:21
|
|
|
|
assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
|
2013-05-05 04:14:04 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
|
|
|
|
uint64_t Result = Value + Addend;
|
2013-05-19 23:39:03 +08:00
|
|
|
|
|
|
|
// AArch64 code is emitted with .rela relocations. The data already in any
|
|
|
|
// bits affected by the relocation on entry is garbage.
|
2013-07-25 20:42:52 +08:00
|
|
|
*TargetPtr &= 0xffe0001fU;
|
2013-05-05 04:14:04 +08:00
|
|
|
// Immediate goes in bits 20:5 of MOVZ/MOVK instruction
|
|
|
|
*TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
|
2013-07-02 03:23:10 +08:00
|
|
|
// Shift must be "lsl #32", in bits 22:21
|
|
|
|
assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
|
2013-05-05 04:14:04 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
|
|
|
|
uint64_t Result = Value + Addend;
|
2013-05-19 23:39:03 +08:00
|
|
|
|
|
|
|
// AArch64 code is emitted with .rela relocations. The data already in any
|
|
|
|
// bits affected by the relocation on entry is garbage.
|
2013-07-25 20:42:52 +08:00
|
|
|
*TargetPtr &= 0xffe0001fU;
|
2013-05-05 04:14:04 +08:00
|
|
|
// Immediate goes in bits 20:5 of MOVZ/MOVK instruction
|
|
|
|
*TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
|
2013-07-02 03:23:10 +08:00
|
|
|
// Shift must be "lsl #16", in bits 22:2
|
|
|
|
assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
|
2013-05-05 04:14:04 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
|
|
|
|
uint64_t Result = Value + Addend;
|
2013-05-19 23:39:03 +08:00
|
|
|
|
|
|
|
// AArch64 code is emitted with .rela relocations. The data already in any
|
|
|
|
// bits affected by the relocation on entry is garbage.
|
2013-07-25 20:42:52 +08:00
|
|
|
*TargetPtr &= 0xffe0001fU;
|
2013-05-05 04:14:04 +08:00
|
|
|
// Immediate goes in bits 20:5 of MOVZ/MOVK instruction
|
|
|
|
*TargetPtr |= ((Result & 0xffffU) << 5);
|
2013-07-02 03:23:10 +08:00
|
|
|
// Shift must be "lsl #0", in bits 22:21.
|
|
|
|
assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
|
2013-05-05 04:14:04 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-02-11 20:59:09 +08:00
|
|
|
case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
|
|
|
|
// Operation: Page(S+A) - Page(P)
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t Result =
|
|
|
|
((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
|
2014-02-11 20:59:09 +08:00
|
|
|
|
|
|
|
// Check that -2^32 <= X < 2^32
|
2015-05-16 04:32:25 +08:00
|
|
|
assert(isInt<33>(Result) && "overflow check failed for relocation");
|
2014-02-11 20:59:09 +08:00
|
|
|
|
|
|
|
// AArch64 code is emitted with .rela relocations. The data already in any
|
|
|
|
// bits affected by the relocation on entry is garbage.
|
|
|
|
*TargetPtr &= 0x9f00001fU;
|
|
|
|
// Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
|
|
|
|
// from bits 32:12 of X.
|
|
|
|
*TargetPtr |= ((Result & 0x3000U) << (29 - 12));
|
|
|
|
*TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
|
|
|
|
// Operation: S + A
|
|
|
|
uint64_t Result = Value + Addend;
|
|
|
|
|
|
|
|
// AArch64 code is emitted with .rela relocations. The data already in any
|
|
|
|
// bits affected by the relocation on entry is garbage.
|
|
|
|
*TargetPtr &= 0xffc003ffU;
|
|
|
|
// Immediate goes in bits 21:10 of LD/ST instruction, taken
|
|
|
|
// from bits 11:2 of X
|
|
|
|
*TargetPtr |= ((Result & 0xffc) << (10 - 2));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
|
|
|
|
// Operation: S + A
|
|
|
|
uint64_t Result = Value + Addend;
|
|
|
|
|
|
|
|
// AArch64 code is emitted with .rela relocations. The data already in any
|
|
|
|
// bits affected by the relocation on entry is garbage.
|
|
|
|
*TargetPtr &= 0xffc003ffU;
|
|
|
|
// Immediate goes in bits 21:10 of LD/ST instruction, taken
|
|
|
|
// from bits 11:3 of X
|
|
|
|
*TargetPtr |= ((Result & 0xff8) << (10 - 3));
|
|
|
|
break;
|
|
|
|
}
|
2013-05-05 04:13:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-03 03:45:23 +08:00
|
|
|
void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t Offset, uint32_t Value,
|
|
|
|
uint32_t Type, int32_t Addend) {
|
2012-03-31 00:45:19 +08:00
|
|
|
// TODO: Add Thumb relocations.
|
2014-03-22 04:28:42 +08:00
|
|
|
uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
|
2012-11-03 03:45:23 +08:00
|
|
|
uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
|
2012-03-31 00:45:19 +08:00
|
|
|
Value += Addend;
|
|
|
|
|
2012-11-03 03:45:23 +08:00
|
|
|
DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
|
|
|
|
<< Section.Address + Offset
|
2014-03-22 04:28:42 +08:00
|
|
|
<< " FinalAddress: " << format("%p", FinalAddress) << " Value: "
|
|
|
|
<< format("%x", Value) << " Type: " << format("%x", Type)
|
|
|
|
<< " Addend: " << format("%x", Addend) << "\n");
|
2012-03-31 00:45:19 +08:00
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
switch (Type) {
|
2012-03-31 00:45:19 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Not implemented relocation type!");
|
|
|
|
|
2014-01-29 19:50:56 +08:00
|
|
|
case ELF::R_ARM_NONE:
|
|
|
|
break;
|
|
|
|
case ELF::R_ARM_PREL31:
|
2013-05-29 03:48:19 +08:00
|
|
|
case ELF::R_ARM_TARGET1:
|
|
|
|
case ELF::R_ARM_ABS32:
|
2015-05-02 04:21:45 +08:00
|
|
|
*TargetPtr = Value;
|
2012-03-31 00:45:19 +08:00
|
|
|
break;
|
2015-05-02 04:21:45 +08:00
|
|
|
// Write first 16 bit of 32 bit value to the mov instruction.
|
|
|
|
// Last 4 bit should be shifted.
|
2013-05-29 03:48:19 +08:00
|
|
|
case ELF::R_ARM_MOVW_ABS_NC:
|
|
|
|
case ELF::R_ARM_MOVT_ABS:
|
2015-05-02 04:21:45 +08:00
|
|
|
if (Type == ELF::R_ARM_MOVW_ABS_NC)
|
|
|
|
Value = Value & 0xFFFF;
|
|
|
|
else if (Type == ELF::R_ARM_MOVT_ABS)
|
|
|
|
Value = (Value >> 16) & 0xFFFF;
|
|
|
|
*TargetPtr &= ~0x000F0FFF;
|
|
|
|
*TargetPtr |= Value & 0xFFF;
|
2012-03-31 00:45:19 +08:00
|
|
|
*TargetPtr |= ((Value >> 12) & 0xF) << 16;
|
|
|
|
break;
|
2015-05-02 04:21:45 +08:00
|
|
|
// Write 24 bit relative value to the branch instruction.
|
2014-03-22 04:28:42 +08:00
|
|
|
case ELF::R_ARM_PC24: // Fall through.
|
|
|
|
case ELF::R_ARM_CALL: // Fall through.
|
2015-05-02 04:21:45 +08:00
|
|
|
case ELF::R_ARM_JUMP24:
|
2012-03-31 00:45:19 +08:00
|
|
|
int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
|
|
|
|
RelValue = (RelValue & 0x03FFFFFC) >> 2;
|
2013-05-29 03:48:19 +08:00
|
|
|
assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
|
2012-03-31 00:45:19 +08:00
|
|
|
*TargetPtr &= 0xFF000000;
|
|
|
|
*TargetPtr |= RelValue;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-16 16:56:09 +08:00
|
|
|
}
|
|
|
|
|
2012-11-03 03:45:23 +08:00
|
|
|
void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t Offset, uint32_t Value,
|
|
|
|
uint32_t Type, int32_t Addend) {
|
2015-06-03 18:27:28 +08:00
|
|
|
uint8_t *TargetPtr = Section.Address + Offset;
|
2012-08-18 05:28:04 +08:00
|
|
|
Value += Addend;
|
|
|
|
|
2015-06-03 18:27:28 +08:00
|
|
|
DEBUG(dbgs() << "resolveMIPSRelocation, LocalAddress: "
|
2014-03-22 04:28:42 +08:00
|
|
|
<< Section.Address + Offset << " FinalAddress: "
|
|
|
|
<< format("%p", Section.LoadAddress + Offset) << " Value: "
|
|
|
|
<< format("%x", Value) << " Type: " << format("%x", Type)
|
|
|
|
<< " Addend: " << format("%x", Addend) << "\n");
|
2012-08-18 05:28:04 +08:00
|
|
|
|
2015-06-03 18:27:28 +08:00
|
|
|
uint32_t Insn = readBytesUnaligned(TargetPtr, 4);
|
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
switch (Type) {
|
2012-08-18 05:28:04 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Not implemented relocation type!");
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_32:
|
2015-06-03 18:27:28 +08:00
|
|
|
writeBytesUnaligned(Value, TargetPtr, 4);
|
2012-08-18 05:28:04 +08:00
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_26:
|
2015-06-03 18:27:28 +08:00
|
|
|
Insn &= 0xfc000000;
|
|
|
|
Insn |= (Value & 0x0fffffff) >> 2;
|
|
|
|
writeBytesUnaligned(Insn, TargetPtr, 4);
|
2012-08-18 05:28:04 +08:00
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_HI16:
|
|
|
|
// Get the higher 16-bits. Also add 1 if bit 15 is 1.
|
2015-06-03 18:27:28 +08:00
|
|
|
Insn &= 0xffff0000;
|
|
|
|
Insn |= ((Value + 0x8000) >> 16) & 0xffff;
|
|
|
|
writeBytesUnaligned(Insn, TargetPtr, 4);
|
2013-07-24 09:58:40 +08:00
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_LO16:
|
2015-06-03 18:27:28 +08:00
|
|
|
Insn &= 0xffff0000;
|
|
|
|
Insn |= Value & 0xffff;
|
|
|
|
writeBytesUnaligned(Insn, TargetPtr, 4);
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_PC32:
|
|
|
|
uint32_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
writeBytesUnaligned(Value + Addend - FinalAddress, (uint8_t *)TargetPtr, 4);
|
2012-08-18 05:28:04 +08:00
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
}
|
2012-08-18 05:28:04 +08:00
|
|
|
}
|
|
|
|
|
2015-05-28 21:48:41 +08:00
|
|
|
void RuntimeDyldELF::setMipsABI(const ObjectFile &Obj) {
|
2015-06-10 11:06:06 +08:00
|
|
|
if (Arch == Triple::UnknownArch ||
|
|
|
|
!StringRef(Triple::getArchTypePrefix(Arch)).equals("mips")) {
|
2015-05-28 21:48:41 +08:00
|
|
|
IsMipsO32ABI = false;
|
|
|
|
IsMipsN64ABI = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned AbiVariant;
|
|
|
|
Obj.getPlatformFlags(AbiVariant);
|
|
|
|
IsMipsO32ABI = AbiVariant & ELF::EF_MIPS_ABI_O32;
|
|
|
|
IsMipsN64ABI = Obj.getFileFormatName().equals("ELF64-mips");
|
|
|
|
if (AbiVariant & ELF::EF_MIPS_ABI2)
|
|
|
|
llvm_unreachable("Mips N32 ABI is not supported yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuntimeDyldELF::resolveMIPS64Relocation(const SectionEntry &Section,
|
|
|
|
uint64_t Offset, uint64_t Value,
|
|
|
|
uint32_t Type, int64_t Addend,
|
|
|
|
uint64_t SymOffset,
|
|
|
|
SID SectionID) {
|
|
|
|
uint32_t r_type = Type & 0xff;
|
|
|
|
uint32_t r_type2 = (Type >> 8) & 0xff;
|
|
|
|
uint32_t r_type3 = (Type >> 16) & 0xff;
|
|
|
|
|
|
|
|
// RelType is used to keep information for which relocation type we are
|
|
|
|
// applying relocation.
|
|
|
|
uint32_t RelType = r_type;
|
|
|
|
int64_t CalculatedValue = evaluateMIPS64Relocation(Section, Offset, Value,
|
|
|
|
RelType, Addend,
|
|
|
|
SymOffset, SectionID);
|
|
|
|
if (r_type2 != ELF::R_MIPS_NONE) {
|
|
|
|
RelType = r_type2;
|
|
|
|
CalculatedValue = evaluateMIPS64Relocation(Section, Offset, 0, RelType,
|
|
|
|
CalculatedValue, SymOffset,
|
|
|
|
SectionID);
|
|
|
|
}
|
|
|
|
if (r_type3 != ELF::R_MIPS_NONE) {
|
|
|
|
RelType = r_type3;
|
|
|
|
CalculatedValue = evaluateMIPS64Relocation(Section, Offset, 0, RelType,
|
|
|
|
CalculatedValue, SymOffset,
|
|
|
|
SectionID);
|
|
|
|
}
|
|
|
|
applyMIPS64Relocation(Section.Address + Offset, CalculatedValue, RelType);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
RuntimeDyldELF::evaluateMIPS64Relocation(const SectionEntry &Section,
|
|
|
|
uint64_t Offset, uint64_t Value,
|
|
|
|
uint32_t Type, int64_t Addend,
|
|
|
|
uint64_t SymOffset, SID SectionID) {
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "evaluateMIPS64Relocation, LocalAddress: 0x"
|
|
|
|
<< format("%llx", Section.Address + Offset)
|
|
|
|
<< " FinalAddress: 0x"
|
|
|
|
<< format("%llx", Section.LoadAddress + Offset)
|
|
|
|
<< " Value: 0x" << format("%llx", Value) << " Type: 0x"
|
|
|
|
<< format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
|
|
|
|
<< " SymOffset: " << format("%x", SymOffset)
|
|
|
|
<< "\n");
|
|
|
|
|
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Not implemented relocation type!");
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_JALR:
|
|
|
|
case ELF::R_MIPS_NONE:
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_32:
|
|
|
|
case ELF::R_MIPS_64:
|
|
|
|
return Value + Addend;
|
|
|
|
case ELF::R_MIPS_26:
|
|
|
|
return ((Value + Addend) >> 2) & 0x3ffffff;
|
|
|
|
case ELF::R_MIPS_GPREL16: {
|
|
|
|
uint64_t GOTAddr = getSectionLoadAddress(SectionToGOTMap[SectionID]);
|
|
|
|
return Value + Addend - (GOTAddr + 0x7ff0);
|
|
|
|
}
|
|
|
|
case ELF::R_MIPS_SUB:
|
|
|
|
return Value - Addend;
|
|
|
|
case ELF::R_MIPS_HI16:
|
|
|
|
// Get the higher 16-bits. Also add 1 if bit 15 is 1.
|
|
|
|
return ((Value + Addend + 0x8000) >> 16) & 0xffff;
|
|
|
|
case ELF::R_MIPS_LO16:
|
|
|
|
return (Value + Addend) & 0xffff;
|
|
|
|
case ELF::R_MIPS_CALL16:
|
|
|
|
case ELF::R_MIPS_GOT_DISP:
|
|
|
|
case ELF::R_MIPS_GOT_PAGE: {
|
|
|
|
uint8_t *LocalGOTAddr =
|
|
|
|
getSectionAddress(SectionToGOTMap[SectionID]) + SymOffset;
|
|
|
|
uint64_t GOTEntry = readBytesUnaligned(LocalGOTAddr, 8);
|
|
|
|
|
|
|
|
Value += Addend;
|
|
|
|
if (Type == ELF::R_MIPS_GOT_PAGE)
|
|
|
|
Value = (Value + 0x8000) & ~0xffff;
|
|
|
|
|
|
|
|
if (GOTEntry)
|
|
|
|
assert(GOTEntry == Value &&
|
|
|
|
"GOT entry has two different addresses.");
|
|
|
|
else
|
|
|
|
writeBytesUnaligned(Value, LocalGOTAddr, 8);
|
|
|
|
|
|
|
|
return (SymOffset - 0x7ff0) & 0xffff;
|
|
|
|
}
|
|
|
|
case ELF::R_MIPS_GOT_OFST: {
|
|
|
|
int64_t page = (Value + Addend + 0x8000) & ~0xffff;
|
|
|
|
return (Value + Addend - page) & 0xffff;
|
|
|
|
}
|
|
|
|
case ELF::R_MIPS_GPREL32: {
|
|
|
|
uint64_t GOTAddr = getSectionLoadAddress(SectionToGOTMap[SectionID]);
|
|
|
|
return Value + Addend - (GOTAddr + 0x7ff0);
|
|
|
|
}
|
|
|
|
case ELF::R_MIPS_PC16: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
2015-06-23 21:54:42 +08:00
|
|
|
return ((Value + Addend - FinalAddress) >> 2) & 0xffff;
|
2015-05-28 21:48:41 +08:00
|
|
|
}
|
2015-06-08 22:10:23 +08:00
|
|
|
case ELF::R_MIPS_PC32: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
return Value + Addend - FinalAddress;
|
|
|
|
}
|
2015-05-28 21:48:41 +08:00
|
|
|
case ELF::R_MIPS_PC18_S3: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
return ((Value + Addend - ((FinalAddress | 7) ^ 7)) >> 3) & 0x3ffff;
|
|
|
|
}
|
|
|
|
case ELF::R_MIPS_PC19_S2: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
return ((Value + Addend - FinalAddress) >> 2) & 0x7ffff;
|
|
|
|
}
|
|
|
|
case ELF::R_MIPS_PC21_S2: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
return ((Value + Addend - FinalAddress) >> 2) & 0x1fffff;
|
|
|
|
}
|
|
|
|
case ELF::R_MIPS_PC26_S2: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
return ((Value + Addend - FinalAddress) >> 2) & 0x3ffffff;
|
|
|
|
}
|
|
|
|
case ELF::R_MIPS_PCHI16: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
return ((Value + Addend - FinalAddress + 0x8000) >> 16) & 0xffff;
|
|
|
|
}
|
|
|
|
case ELF::R_MIPS_PCLO16: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
return (Value + Addend - FinalAddress) & 0xffff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuntimeDyldELF::applyMIPS64Relocation(uint8_t *TargetPtr,
|
|
|
|
int64_t CalculatedValue,
|
|
|
|
uint32_t Type) {
|
|
|
|
uint32_t Insn = readBytesUnaligned(TargetPtr, 4);
|
|
|
|
|
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_32:
|
|
|
|
case ELF::R_MIPS_GPREL32:
|
2015-06-08 22:10:23 +08:00
|
|
|
case ELF::R_MIPS_PC32:
|
2015-05-28 21:48:41 +08:00
|
|
|
writeBytesUnaligned(CalculatedValue & 0xffffffff, TargetPtr, 4);
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_64:
|
|
|
|
case ELF::R_MIPS_SUB:
|
|
|
|
writeBytesUnaligned(CalculatedValue, TargetPtr, 8);
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_26:
|
|
|
|
case ELF::R_MIPS_PC26_S2:
|
|
|
|
Insn = (Insn & 0xfc000000) | CalculatedValue;
|
|
|
|
writeBytesUnaligned(Insn, TargetPtr, 4);
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_GPREL16:
|
|
|
|
Insn = (Insn & 0xffff0000) | (CalculatedValue & 0xffff);
|
|
|
|
writeBytesUnaligned(Insn, TargetPtr, 4);
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_HI16:
|
|
|
|
case ELF::R_MIPS_LO16:
|
|
|
|
case ELF::R_MIPS_PCHI16:
|
|
|
|
case ELF::R_MIPS_PCLO16:
|
|
|
|
case ELF::R_MIPS_PC16:
|
|
|
|
case ELF::R_MIPS_CALL16:
|
|
|
|
case ELF::R_MIPS_GOT_DISP:
|
|
|
|
case ELF::R_MIPS_GOT_PAGE:
|
|
|
|
case ELF::R_MIPS_GOT_OFST:
|
|
|
|
Insn = (Insn & 0xffff0000) | CalculatedValue;
|
|
|
|
writeBytesUnaligned(Insn, TargetPtr, 4);
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_PC18_S3:
|
|
|
|
Insn = (Insn & 0xfffc0000) | CalculatedValue;
|
|
|
|
writeBytesUnaligned(Insn, TargetPtr, 4);
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_PC19_S2:
|
|
|
|
Insn = (Insn & 0xfff80000) | CalculatedValue;
|
|
|
|
writeBytesUnaligned(Insn, TargetPtr, 4);
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_PC21_S2:
|
|
|
|
Insn = (Insn & 0xffe00000) | CalculatedValue;
|
|
|
|
writeBytesUnaligned(Insn, TargetPtr, 4);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[RuntimeDyld, PowerPC] Fix/improve handling of TOC relocations
Current PPC64 RuntimeDyld code to handle TOC relocations has two
problems:
- With recent linkers, in addition to the relocations that implicitly
refer to the TOC base (R_PPC64_TOC*), you can now also use the .TOC.
magic symbol with any other relocation to refer to the TOC base
explicitly. This isn't currently used much in ELFv1 code (although
it could be), but it is essential in ELFv2 code.
- In a complex JIT environment with multiple modules, each module may
have its own .toc section, and TOC relocations in one module must
refer to *its own* TOC section. The current findPPC64TOC implementation
does not correctly implement this; in fact, it will always return the
address of the first TOC section it finds anywhere. (Note that at the
time findPPC64TOC is called, we don't even *know* which module the
relocation originally resided in, so it is not even possible to fix
this routine as-is.)
This commit fixes both problems by handling TOC relocations earlier, in
processRelocationRef. To do this, I've removed the findPPC64TOC routine
and replaced it by a new routine findPPC64TOCSection, which works
analogously to findOPDEntrySection in scanning the sections of the
ObjImage provided by its caller, processRelocationRef. This solves the
issue of finding the correct TOC section associated with the current
module.
This makes it straightforward to implement both R_PPC64_TOC relocations,
and relocations explicitly refering to the .TOC. symbol, directly in
processRelocationRef. There is now a new problem in implementing the
R_PPC64_TOC16* relocations, because those can now in theory involve
*three* different sections: the relocation may be applied in section A,
refer explicitly to a symbol in section B, and refer implicitly to the
TOC section C. The final processing of the relocation thus may only
happen after all three of these sections have been assigned final
addresses. There is currently no obvious means to implement this in
its general form with the common-code RuntimeDyld infrastructure.
Fortunately, ppc64 code usually makes no use of this most general form;
in fact, TOC16 relocations are only ever generated by LLVM for symbols
residing themselves in the TOC, which means "section B" == "section C"
in the above terminology. This special case can easily be handled with
the current infrastructure, and that is what this patch does.
[ Unhandled cases result in an explicit error, unlike the current code
which silently returns the wrong TOC base address ... ]
This patch makes the JIT work on both BE and LE (ELFv2 requires
additional patches, of course), and allowed me to successfully run
complex JIT scenarios (via mesa/llvmpipe).
Reviewed by Hal Finkel.
llvm-svn: 211885
2014-06-27 18:32:14 +08:00
|
|
|
// Return the .TOC. section and offset.
|
2015-06-20 04:58:43 +08:00
|
|
|
void RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj,
|
[RuntimeDyld, PowerPC] Fix/improve handling of TOC relocations
Current PPC64 RuntimeDyld code to handle TOC relocations has two
problems:
- With recent linkers, in addition to the relocations that implicitly
refer to the TOC base (R_PPC64_TOC*), you can now also use the .TOC.
magic symbol with any other relocation to refer to the TOC base
explicitly. This isn't currently used much in ELFv1 code (although
it could be), but it is essential in ELFv2 code.
- In a complex JIT environment with multiple modules, each module may
have its own .toc section, and TOC relocations in one module must
refer to *its own* TOC section. The current findPPC64TOC implementation
does not correctly implement this; in fact, it will always return the
address of the first TOC section it finds anywhere. (Note that at the
time findPPC64TOC is called, we don't even *know* which module the
relocation originally resided in, so it is not even possible to fix
this routine as-is.)
This commit fixes both problems by handling TOC relocations earlier, in
processRelocationRef. To do this, I've removed the findPPC64TOC routine
and replaced it by a new routine findPPC64TOCSection, which works
analogously to findOPDEntrySection in scanning the sections of the
ObjImage provided by its caller, processRelocationRef. This solves the
issue of finding the correct TOC section associated with the current
module.
This makes it straightforward to implement both R_PPC64_TOC relocations,
and relocations explicitly refering to the .TOC. symbol, directly in
processRelocationRef. There is now a new problem in implementing the
R_PPC64_TOC16* relocations, because those can now in theory involve
*three* different sections: the relocation may be applied in section A,
refer explicitly to a symbol in section B, and refer implicitly to the
TOC section C. The final processing of the relocation thus may only
happen after all three of these sections have been assigned final
addresses. There is currently no obvious means to implement this in
its general form with the common-code RuntimeDyld infrastructure.
Fortunately, ppc64 code usually makes no use of this most general form;
in fact, TOC16 relocations are only ever generated by LLVM for symbols
residing themselves in the TOC, which means "section B" == "section C"
in the above terminology. This special case can easily be handled with
the current infrastructure, and that is what this patch does.
[ Unhandled cases result in an explicit error, unlike the current code
which silently returns the wrong TOC base address ... ]
This patch makes the JIT work on both BE and LE (ELFv2 requires
additional patches, of course), and allowed me to successfully run
complex JIT scenarios (via mesa/llvmpipe).
Reviewed by Hal Finkel.
llvm-svn: 211885
2014-06-27 18:32:14 +08:00
|
|
|
ObjSectionToIDMap &LocalSections,
|
|
|
|
RelocationValueRef &Rel) {
|
|
|
|
// Set a default SectionID in case we do not find a TOC section below.
|
|
|
|
// This may happen for references to TOC base base (sym@toc, .odp
|
|
|
|
// relocation) without a .toc directive. In this case just use the
|
|
|
|
// first section (which is usually the .odp) since the code won't
|
|
|
|
// reference the .toc base directly.
|
|
|
|
Rel.SymbolName = NULL;
|
|
|
|
Rel.SectionID = 0;
|
|
|
|
|
2012-10-25 21:13:48 +08:00
|
|
|
// The TOC consists of sections .got, .toc, .tocbss, .plt in that
|
|
|
|
// order. The TOC starts where the first of these sections starts.
|
2015-06-02 09:52:28 +08:00
|
|
|
for (auto &Section: Obj.sections()) {
|
[RuntimeDyld, PowerPC] Fix/improve handling of TOC relocations
Current PPC64 RuntimeDyld code to handle TOC relocations has two
problems:
- With recent linkers, in addition to the relocations that implicitly
refer to the TOC base (R_PPC64_TOC*), you can now also use the .TOC.
magic symbol with any other relocation to refer to the TOC base
explicitly. This isn't currently used much in ELFv1 code (although
it could be), but it is essential in ELFv2 code.
- In a complex JIT environment with multiple modules, each module may
have its own .toc section, and TOC relocations in one module must
refer to *its own* TOC section. The current findPPC64TOC implementation
does not correctly implement this; in fact, it will always return the
address of the first TOC section it finds anywhere. (Note that at the
time findPPC64TOC is called, we don't even *know* which module the
relocation originally resided in, so it is not even possible to fix
this routine as-is.)
This commit fixes both problems by handling TOC relocations earlier, in
processRelocationRef. To do this, I've removed the findPPC64TOC routine
and replaced it by a new routine findPPC64TOCSection, which works
analogously to findOPDEntrySection in scanning the sections of the
ObjImage provided by its caller, processRelocationRef. This solves the
issue of finding the correct TOC section associated with the current
module.
This makes it straightforward to implement both R_PPC64_TOC relocations,
and relocations explicitly refering to the .TOC. symbol, directly in
processRelocationRef. There is now a new problem in implementing the
R_PPC64_TOC16* relocations, because those can now in theory involve
*three* different sections: the relocation may be applied in section A,
refer explicitly to a symbol in section B, and refer implicitly to the
TOC section C. The final processing of the relocation thus may only
happen after all three of these sections have been assigned final
addresses. There is currently no obvious means to implement this in
its general form with the common-code RuntimeDyld infrastructure.
Fortunately, ppc64 code usually makes no use of this most general form;
in fact, TOC16 relocations are only ever generated by LLVM for symbols
residing themselves in the TOC, which means "section B" == "section C"
in the above terminology. This special case can easily be handled with
the current infrastructure, and that is what this patch does.
[ Unhandled cases result in an explicit error, unlike the current code
which silently returns the wrong TOC base address ... ]
This patch makes the JIT work on both BE and LE (ELFv2 requires
additional patches, of course), and allowed me to successfully run
complex JIT scenarios (via mesa/llvmpipe).
Reviewed by Hal Finkel.
llvm-svn: 211885
2014-06-27 18:32:14 +08:00
|
|
|
StringRef SectionName;
|
2015-06-02 09:52:28 +08:00
|
|
|
check(Section.getName(SectionName));
|
[RuntimeDyld, PowerPC] Fix/improve handling of TOC relocations
Current PPC64 RuntimeDyld code to handle TOC relocations has two
problems:
- With recent linkers, in addition to the relocations that implicitly
refer to the TOC base (R_PPC64_TOC*), you can now also use the .TOC.
magic symbol with any other relocation to refer to the TOC base
explicitly. This isn't currently used much in ELFv1 code (although
it could be), but it is essential in ELFv2 code.
- In a complex JIT environment with multiple modules, each module may
have its own .toc section, and TOC relocations in one module must
refer to *its own* TOC section. The current findPPC64TOC implementation
does not correctly implement this; in fact, it will always return the
address of the first TOC section it finds anywhere. (Note that at the
time findPPC64TOC is called, we don't even *know* which module the
relocation originally resided in, so it is not even possible to fix
this routine as-is.)
This commit fixes both problems by handling TOC relocations earlier, in
processRelocationRef. To do this, I've removed the findPPC64TOC routine
and replaced it by a new routine findPPC64TOCSection, which works
analogously to findOPDEntrySection in scanning the sections of the
ObjImage provided by its caller, processRelocationRef. This solves the
issue of finding the correct TOC section associated with the current
module.
This makes it straightforward to implement both R_PPC64_TOC relocations,
and relocations explicitly refering to the .TOC. symbol, directly in
processRelocationRef. There is now a new problem in implementing the
R_PPC64_TOC16* relocations, because those can now in theory involve
*three* different sections: the relocation may be applied in section A,
refer explicitly to a symbol in section B, and refer implicitly to the
TOC section C. The final processing of the relocation thus may only
happen after all three of these sections have been assigned final
addresses. There is currently no obvious means to implement this in
its general form with the common-code RuntimeDyld infrastructure.
Fortunately, ppc64 code usually makes no use of this most general form;
in fact, TOC16 relocations are only ever generated by LLVM for symbols
residing themselves in the TOC, which means "section B" == "section C"
in the above terminology. This special case can easily be handled with
the current infrastructure, and that is what this patch does.
[ Unhandled cases result in an explicit error, unlike the current code
which silently returns the wrong TOC base address ... ]
This patch makes the JIT work on both BE and LE (ELFv2 requires
additional patches, of course), and allowed me to successfully run
complex JIT scenarios (via mesa/llvmpipe).
Reviewed by Hal Finkel.
llvm-svn: 211885
2014-06-27 18:32:14 +08:00
|
|
|
|
|
|
|
if (SectionName == ".got"
|
|
|
|
|| SectionName == ".toc"
|
|
|
|
|| SectionName == ".tocbss"
|
|
|
|
|| SectionName == ".plt") {
|
2015-06-02 09:52:28 +08:00
|
|
|
Rel.SectionID = findOrEmitSection(Obj, Section, false, LocalSections);
|
2012-10-25 21:13:48 +08:00
|
|
|
break;
|
[RuntimeDyld, PowerPC] Fix/improve handling of TOC relocations
Current PPC64 RuntimeDyld code to handle TOC relocations has two
problems:
- With recent linkers, in addition to the relocations that implicitly
refer to the TOC base (R_PPC64_TOC*), you can now also use the .TOC.
magic symbol with any other relocation to refer to the TOC base
explicitly. This isn't currently used much in ELFv1 code (although
it could be), but it is essential in ELFv2 code.
- In a complex JIT environment with multiple modules, each module may
have its own .toc section, and TOC relocations in one module must
refer to *its own* TOC section. The current findPPC64TOC implementation
does not correctly implement this; in fact, it will always return the
address of the first TOC section it finds anywhere. (Note that at the
time findPPC64TOC is called, we don't even *know* which module the
relocation originally resided in, so it is not even possible to fix
this routine as-is.)
This commit fixes both problems by handling TOC relocations earlier, in
processRelocationRef. To do this, I've removed the findPPC64TOC routine
and replaced it by a new routine findPPC64TOCSection, which works
analogously to findOPDEntrySection in scanning the sections of the
ObjImage provided by its caller, processRelocationRef. This solves the
issue of finding the correct TOC section associated with the current
module.
This makes it straightforward to implement both R_PPC64_TOC relocations,
and relocations explicitly refering to the .TOC. symbol, directly in
processRelocationRef. There is now a new problem in implementing the
R_PPC64_TOC16* relocations, because those can now in theory involve
*three* different sections: the relocation may be applied in section A,
refer explicitly to a symbol in section B, and refer implicitly to the
TOC section C. The final processing of the relocation thus may only
happen after all three of these sections have been assigned final
addresses. There is currently no obvious means to implement this in
its general form with the common-code RuntimeDyld infrastructure.
Fortunately, ppc64 code usually makes no use of this most general form;
in fact, TOC16 relocations are only ever generated by LLVM for symbols
residing themselves in the TOC, which means "section B" == "section C"
in the above terminology. This special case can easily be handled with
the current infrastructure, and that is what this patch does.
[ Unhandled cases result in an explicit error, unlike the current code
which silently returns the wrong TOC base address ... ]
This patch makes the JIT work on both BE and LE (ELFv2 requires
additional patches, of course), and allowed me to successfully run
complex JIT scenarios (via mesa/llvmpipe).
Reviewed by Hal Finkel.
llvm-svn: 211885
2014-06-27 18:32:14 +08:00
|
|
|
}
|
2012-10-25 21:13:48 +08:00
|
|
|
}
|
[RuntimeDyld, PowerPC] Fix/improve handling of TOC relocations
Current PPC64 RuntimeDyld code to handle TOC relocations has two
problems:
- With recent linkers, in addition to the relocations that implicitly
refer to the TOC base (R_PPC64_TOC*), you can now also use the .TOC.
magic symbol with any other relocation to refer to the TOC base
explicitly. This isn't currently used much in ELFv1 code (although
it could be), but it is essential in ELFv2 code.
- In a complex JIT environment with multiple modules, each module may
have its own .toc section, and TOC relocations in one module must
refer to *its own* TOC section. The current findPPC64TOC implementation
does not correctly implement this; in fact, it will always return the
address of the first TOC section it finds anywhere. (Note that at the
time findPPC64TOC is called, we don't even *know* which module the
relocation originally resided in, so it is not even possible to fix
this routine as-is.)
This commit fixes both problems by handling TOC relocations earlier, in
processRelocationRef. To do this, I've removed the findPPC64TOC routine
and replaced it by a new routine findPPC64TOCSection, which works
analogously to findOPDEntrySection in scanning the sections of the
ObjImage provided by its caller, processRelocationRef. This solves the
issue of finding the correct TOC section associated with the current
module.
This makes it straightforward to implement both R_PPC64_TOC relocations,
and relocations explicitly refering to the .TOC. symbol, directly in
processRelocationRef. There is now a new problem in implementing the
R_PPC64_TOC16* relocations, because those can now in theory involve
*three* different sections: the relocation may be applied in section A,
refer explicitly to a symbol in section B, and refer implicitly to the
TOC section C. The final processing of the relocation thus may only
happen after all three of these sections have been assigned final
addresses. There is currently no obvious means to implement this in
its general form with the common-code RuntimeDyld infrastructure.
Fortunately, ppc64 code usually makes no use of this most general form;
in fact, TOC16 relocations are only ever generated by LLVM for symbols
residing themselves in the TOC, which means "section B" == "section C"
in the above terminology. This special case can easily be handled with
the current infrastructure, and that is what this patch does.
[ Unhandled cases result in an explicit error, unlike the current code
which silently returns the wrong TOC base address ... ]
This patch makes the JIT work on both BE and LE (ELFv2 requires
additional patches, of course), and allowed me to successfully run
complex JIT scenarios (via mesa/llvmpipe).
Reviewed by Hal Finkel.
llvm-svn: 211885
2014-06-27 18:32:14 +08:00
|
|
|
|
2012-10-25 21:13:48 +08:00
|
|
|
// Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
|
|
|
|
// thus permitting a full 64 Kbytes segment.
|
[RuntimeDyld, PowerPC] Fix/improve handling of TOC relocations
Current PPC64 RuntimeDyld code to handle TOC relocations has two
problems:
- With recent linkers, in addition to the relocations that implicitly
refer to the TOC base (R_PPC64_TOC*), you can now also use the .TOC.
magic symbol with any other relocation to refer to the TOC base
explicitly. This isn't currently used much in ELFv1 code (although
it could be), but it is essential in ELFv2 code.
- In a complex JIT environment with multiple modules, each module may
have its own .toc section, and TOC relocations in one module must
refer to *its own* TOC section. The current findPPC64TOC implementation
does not correctly implement this; in fact, it will always return the
address of the first TOC section it finds anywhere. (Note that at the
time findPPC64TOC is called, we don't even *know* which module the
relocation originally resided in, so it is not even possible to fix
this routine as-is.)
This commit fixes both problems by handling TOC relocations earlier, in
processRelocationRef. To do this, I've removed the findPPC64TOC routine
and replaced it by a new routine findPPC64TOCSection, which works
analogously to findOPDEntrySection in scanning the sections of the
ObjImage provided by its caller, processRelocationRef. This solves the
issue of finding the correct TOC section associated with the current
module.
This makes it straightforward to implement both R_PPC64_TOC relocations,
and relocations explicitly refering to the .TOC. symbol, directly in
processRelocationRef. There is now a new problem in implementing the
R_PPC64_TOC16* relocations, because those can now in theory involve
*three* different sections: the relocation may be applied in section A,
refer explicitly to a symbol in section B, and refer implicitly to the
TOC section C. The final processing of the relocation thus may only
happen after all three of these sections have been assigned final
addresses. There is currently no obvious means to implement this in
its general form with the common-code RuntimeDyld infrastructure.
Fortunately, ppc64 code usually makes no use of this most general form;
in fact, TOC16 relocations are only ever generated by LLVM for symbols
residing themselves in the TOC, which means "section B" == "section C"
in the above terminology. This special case can easily be handled with
the current infrastructure, and that is what this patch does.
[ Unhandled cases result in an explicit error, unlike the current code
which silently returns the wrong TOC base address ... ]
This patch makes the JIT work on both BE and LE (ELFv2 requires
additional patches, of course), and allowed me to successfully run
complex JIT scenarios (via mesa/llvmpipe).
Reviewed by Hal Finkel.
llvm-svn: 211885
2014-06-27 18:32:14 +08:00
|
|
|
Rel.Addend = 0x8000;
|
2012-10-25 21:13:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the sections and offset associated with the ODP entry referenced
|
|
|
|
// by Symbol.
|
2015-06-20 04:58:43 +08:00
|
|
|
void RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj,
|
2012-10-25 21:13:48 +08:00
|
|
|
ObjSectionToIDMap &LocalSections,
|
|
|
|
RelocationValueRef &Rel) {
|
|
|
|
// Get the ELF symbol value (st_value) to compare with Relocation offset in
|
|
|
|
// .opd entries
|
2014-11-27 00:54:40 +08:00
|
|
|
for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
|
2014-01-30 10:49:50 +08:00
|
|
|
si != se; ++si) {
|
2013-06-04 03:37:34 +08:00
|
|
|
section_iterator RelSecI = si->getRelocatedSection();
|
2014-11-27 00:54:40 +08:00
|
|
|
if (RelSecI == Obj.section_end())
|
2013-06-04 03:37:34 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
StringRef RelSectionName;
|
|
|
|
check(RelSecI->getName(RelSectionName));
|
|
|
|
if (RelSectionName != ".opd")
|
2012-10-25 21:13:48 +08:00
|
|
|
continue;
|
|
|
|
|
2015-06-30 08:33:59 +08:00
|
|
|
for (elf_relocation_iterator i = si->relocation_begin(),
|
|
|
|
e = si->relocation_end();
|
2014-03-22 04:28:42 +08:00
|
|
|
i != e;) {
|
2012-10-25 21:13:48 +08:00
|
|
|
// The R_PPC64_ADDR64 relocation indicates the first field
|
|
|
|
// of a .opd entry
|
2015-06-30 09:53:01 +08:00
|
|
|
uint64_t TypeFunc = i->getType();
|
2012-10-25 21:13:48 +08:00
|
|
|
if (TypeFunc != ELF::R_PPC64_ADDR64) {
|
2014-01-30 10:49:50 +08:00
|
|
|
++i;
|
2012-10-25 21:13:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-30 07:29:12 +08:00
|
|
|
uint64_t TargetSymbolOffset = i->getOffset();
|
2013-06-05 09:33:53 +08:00
|
|
|
symbol_iterator TargetSymbol = i->getSymbol();
|
2015-06-30 08:33:59 +08:00
|
|
|
ErrorOr<int64_t> AddendOrErr = i->getAddend();
|
2015-06-20 04:58:43 +08:00
|
|
|
Check(AddendOrErr.getError());
|
|
|
|
int64_t Addend = *AddendOrErr;
|
2012-10-25 21:13:48 +08:00
|
|
|
|
2014-01-30 10:49:50 +08:00
|
|
|
++i;
|
2012-10-25 21:13:48 +08:00
|
|
|
if (i == e)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Just check if following relocation is a R_PPC64_TOC
|
2015-06-30 09:53:01 +08:00
|
|
|
uint64_t TypeTOC = i->getType();
|
2012-10-25 21:13:48 +08:00
|
|
|
if (TypeTOC != ELF::R_PPC64_TOC)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Finally compares the Symbol value and the target symbol offset
|
|
|
|
// to check if this .opd entry refers to the symbol the relocation
|
|
|
|
// points to.
|
2013-08-20 07:27:43 +08:00
|
|
|
if (Rel.Addend != (int64_t)TargetSymbolOffset)
|
2012-10-25 21:13:48 +08:00
|
|
|
continue;
|
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
section_iterator tsi(Obj.section_end());
|
2013-06-05 09:33:53 +08:00
|
|
|
check(TargetSymbol->getSection(tsi));
|
2014-10-08 23:28:58 +08:00
|
|
|
bool IsCode = tsi->isText();
|
2014-02-19 05:46:39 +08:00
|
|
|
Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
|
2013-05-09 11:39:05 +08:00
|
|
|
Rel.Addend = (intptr_t)Addend;
|
2012-10-25 21:13:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("Attempting to get address of ODP entry!");
|
|
|
|
}
|
|
|
|
|
2014-06-21 01:51:47 +08:00
|
|
|
// Relocation masks following the #lo(value), #hi(value), #ha(value),
|
|
|
|
// #higher(value), #highera(value), #highest(value), and #highesta(value)
|
|
|
|
// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
|
|
|
|
// document.
|
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
|
2012-10-25 21:13:48 +08:00
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
static inline uint16_t applyPPChi(uint64_t value) {
|
2012-10-25 21:13:48 +08:00
|
|
|
return (value >> 16) & 0xffff;
|
|
|
|
}
|
|
|
|
|
2014-06-21 01:51:47 +08:00
|
|
|
static inline uint16_t applyPPCha (uint64_t value) {
|
|
|
|
return ((value + 0x8000) >> 16) & 0xffff;
|
|
|
|
}
|
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
static inline uint16_t applyPPChigher(uint64_t value) {
|
2012-10-25 21:13:48 +08:00
|
|
|
return (value >> 32) & 0xffff;
|
|
|
|
}
|
|
|
|
|
2014-06-21 01:51:47 +08:00
|
|
|
static inline uint16_t applyPPChighera (uint64_t value) {
|
|
|
|
return ((value + 0x8000) >> 32) & 0xffff;
|
|
|
|
}
|
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
static inline uint16_t applyPPChighest(uint64_t value) {
|
2012-10-25 21:13:48 +08:00
|
|
|
return (value >> 48) & 0xffff;
|
|
|
|
}
|
|
|
|
|
2014-06-21 01:51:47 +08:00
|
|
|
static inline uint16_t applyPPChighesta (uint64_t value) {
|
|
|
|
return ((value + 0x8000) >> 48) & 0xffff;
|
|
|
|
}
|
|
|
|
|
2012-11-03 03:45:23 +08:00
|
|
|
void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t Offset, uint64_t Value,
|
|
|
|
uint32_t Type, int64_t Addend) {
|
|
|
|
uint8_t *LocalAddress = Section.Address + Offset;
|
2012-10-25 21:13:48 +08:00
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Relocation type not implemented yet!");
|
|
|
|
break;
|
2014-06-21 01:51:47 +08:00
|
|
|
case ELF::R_PPC64_ADDR16:
|
|
|
|
writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
|
|
|
|
break;
|
|
|
|
case ELF::R_PPC64_ADDR16_DS:
|
|
|
|
writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
|
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case ELF::R_PPC64_ADDR16_LO:
|
|
|
|
writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
|
|
|
|
break;
|
2014-06-21 01:51:47 +08:00
|
|
|
case ELF::R_PPC64_ADDR16_LO_DS:
|
|
|
|
writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
|
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case ELF::R_PPC64_ADDR16_HI:
|
|
|
|
writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
|
2012-10-25 21:13:48 +08:00
|
|
|
break;
|
2014-06-21 01:51:47 +08:00
|
|
|
case ELF::R_PPC64_ADDR16_HA:
|
|
|
|
writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
|
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case ELF::R_PPC64_ADDR16_HIGHER:
|
|
|
|
writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
|
2012-10-25 21:13:48 +08:00
|
|
|
break;
|
2014-06-21 01:51:47 +08:00
|
|
|
case ELF::R_PPC64_ADDR16_HIGHERA:
|
|
|
|
writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
|
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case ELF::R_PPC64_ADDR16_HIGHEST:
|
|
|
|
writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
|
2012-10-25 21:13:48 +08:00
|
|
|
break;
|
2014-06-21 01:51:47 +08:00
|
|
|
case ELF::R_PPC64_ADDR16_HIGHESTA:
|
|
|
|
writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
|
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case ELF::R_PPC64_ADDR14: {
|
2012-10-25 21:13:48 +08:00
|
|
|
assert(((Value + Addend) & 3) == 0);
|
|
|
|
// Preserve the AA/LK bits in the branch instruction
|
2014-03-22 04:28:42 +08:00
|
|
|
uint8_t aalk = *(LocalAddress + 3);
|
2012-10-25 21:13:48 +08:00
|
|
|
writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
|
|
|
|
} break;
|
2014-06-21 01:51:47 +08:00
|
|
|
case ELF::R_PPC64_REL16_LO: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
uint64_t Delta = Value - FinalAddress + Addend;
|
|
|
|
writeInt16BE(LocalAddress, applyPPClo(Delta));
|
|
|
|
} break;
|
|
|
|
case ELF::R_PPC64_REL16_HI: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
uint64_t Delta = Value - FinalAddress + Addend;
|
|
|
|
writeInt16BE(LocalAddress, applyPPChi(Delta));
|
|
|
|
} break;
|
|
|
|
case ELF::R_PPC64_REL16_HA: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
uint64_t Delta = Value - FinalAddress + Addend;
|
|
|
|
writeInt16BE(LocalAddress, applyPPCha(Delta));
|
|
|
|
} break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case ELF::R_PPC64_ADDR32: {
|
2013-01-05 03:08:13 +08:00
|
|
|
int32_t Result = static_cast<int32_t>(Value + Addend);
|
|
|
|
if (SignExtend32<32>(Result) != Result)
|
2013-01-10 01:08:15 +08:00
|
|
|
llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
|
2013-01-05 03:08:13 +08:00
|
|
|
writeInt32BE(LocalAddress, Result);
|
|
|
|
} break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case ELF::R_PPC64_REL24: {
|
2012-11-03 03:45:23 +08:00
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
2012-10-25 21:13:48 +08:00
|
|
|
int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
|
|
|
|
if (SignExtend32<24>(delta) != delta)
|
|
|
|
llvm_unreachable("Relocation R_PPC64_REL24 overflow");
|
|
|
|
// Generates a 'bl <address>' instruction
|
|
|
|
writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
|
|
|
|
} break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case ELF::R_PPC64_REL32: {
|
2013-01-10 01:08:15 +08:00
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
|
|
|
|
if (SignExtend32<32>(delta) != delta)
|
|
|
|
llvm_unreachable("Relocation R_PPC64_REL32 overflow");
|
|
|
|
writeInt32BE(LocalAddress, delta);
|
|
|
|
} break;
|
2013-05-07 01:21:23 +08:00
|
|
|
case ELF::R_PPC64_REL64: {
|
|
|
|
uint64_t FinalAddress = (Section.LoadAddress + Offset);
|
|
|
|
uint64_t Delta = Value - FinalAddress + Addend;
|
|
|
|
writeInt64BE(LocalAddress, Delta);
|
|
|
|
} break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case ELF::R_PPC64_ADDR64:
|
2012-10-25 21:13:48 +08:00
|
|
|
writeInt64BE(LocalAddress, Value + Addend);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 22:15:35 +08:00
|
|
|
void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t Offset, uint64_t Value,
|
|
|
|
uint32_t Type, int64_t Addend) {
|
2013-05-03 22:15:35 +08:00
|
|
|
uint8_t *LocalAddress = Section.Address + Offset;
|
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Relocation type not implemented yet!");
|
|
|
|
break;
|
|
|
|
case ELF::R_390_PC16DBL:
|
|
|
|
case ELF::R_390_PLT16DBL: {
|
|
|
|
int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
|
|
|
|
assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
|
|
|
|
writeInt16BE(LocalAddress, Delta / 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_390_PC32DBL:
|
|
|
|
case ELF::R_390_PLT32DBL: {
|
|
|
|
int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
|
|
|
|
assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
|
|
|
|
writeInt32BE(LocalAddress, Delta / 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_390_PC32: {
|
|
|
|
int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
|
|
|
|
assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
|
|
|
|
writeInt32BE(LocalAddress, Delta);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_390_64:
|
|
|
|
writeInt64BE(LocalAddress, Value + Addend);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-20 03:38:06 +08:00
|
|
|
// The target location for the relocation is described by RE.SectionID and
|
|
|
|
// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
|
|
|
|
// SectionEntry has three members describing its location.
|
|
|
|
// SectionEntry::Address is the address at which the section has been loaded
|
|
|
|
// into memory in the current (host) process. SectionEntry::LoadAddress is the
|
|
|
|
// address that the section will have in the target process.
|
|
|
|
// SectionEntry::ObjAddress is the address of the bits for this section in the
|
|
|
|
// original emitted object image (also in the current address space).
|
|
|
|
//
|
|
|
|
// Relocations will be applied as if the section were loaded at
|
|
|
|
// SectionEntry::LoadAddress, but they will be applied at an address based
|
|
|
|
// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
|
|
|
|
// Target memory contents if they are required for value calculations.
|
|
|
|
//
|
|
|
|
// The Value parameter here is the load address of the symbol for the
|
|
|
|
// relocation to be applied. For relocations which refer to symbols in the
|
|
|
|
// current object Value will be the LoadAddress of the section in which
|
|
|
|
// the symbol resides (RE.Addend provides additional information about the
|
|
|
|
// symbol location). For external symbols, Value will be the address of the
|
|
|
|
// symbol in the target address space.
|
2013-04-30 01:24:34 +08:00
|
|
|
void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
|
2013-08-20 03:38:06 +08:00
|
|
|
uint64_t Value) {
|
2013-04-30 01:24:34 +08:00
|
|
|
const SectionEntry &Section = Sections[RE.SectionID];
|
2013-08-20 07:27:43 +08:00
|
|
|
return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
|
2015-05-28 21:48:41 +08:00
|
|
|
RE.SymOffset, RE.SectionID);
|
2013-04-30 01:24:34 +08:00
|
|
|
}
|
|
|
|
|
2012-11-03 03:45:23 +08:00
|
|
|
void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
|
2014-03-22 04:28:42 +08:00
|
|
|
uint64_t Offset, uint64_t Value,
|
|
|
|
uint32_t Type, int64_t Addend,
|
2015-05-28 21:48:41 +08:00
|
|
|
uint64_t SymOffset, SID SectionID) {
|
2012-01-16 16:56:09 +08:00
|
|
|
switch (Arch) {
|
|
|
|
case Triple::x86_64:
|
2013-08-20 07:27:43 +08:00
|
|
|
resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
|
2012-01-16 16:56:09 +08:00
|
|
|
break;
|
|
|
|
case Triple::x86:
|
2014-03-22 04:28:42 +08:00
|
|
|
resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
|
2012-03-31 00:45:19 +08:00
|
|
|
(uint32_t)(Addend & 0xffffffffL));
|
2012-01-16 16:56:09 +08:00
|
|
|
break;
|
2013-05-05 04:13:59 +08:00
|
|
|
case Triple::aarch64:
|
2014-03-26 22:57:32 +08:00
|
|
|
case Triple::aarch64_be:
|
2013-05-05 04:13:59 +08:00
|
|
|
resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
|
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case Triple::arm: // Fall through.
|
2014-03-28 22:35:30 +08:00
|
|
|
case Triple::armeb:
|
2012-03-31 00:45:19 +08:00
|
|
|
case Triple::thumb:
|
2014-03-28 22:35:30 +08:00
|
|
|
case Triple::thumbeb:
|
2014-03-22 04:28:42 +08:00
|
|
|
resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
|
2012-03-31 00:45:19 +08:00
|
|
|
(uint32_t)(Addend & 0xffffffffL));
|
2012-01-16 16:56:09 +08:00
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case Triple::mips: // Fall through.
|
2012-08-18 05:28:04 +08:00
|
|
|
case Triple::mipsel:
|
2015-05-28 21:48:41 +08:00
|
|
|
case Triple::mips64:
|
|
|
|
case Triple::mips64el:
|
|
|
|
if (IsMipsO32ABI)
|
|
|
|
resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL),
|
|
|
|
Type, (uint32_t)(Addend & 0xffffffffL));
|
|
|
|
else if (IsMipsN64ABI)
|
|
|
|
resolveMIPS64Relocation(Section, Offset, Value, Type, Addend, SymOffset,
|
|
|
|
SectionID);
|
|
|
|
else
|
|
|
|
llvm_unreachable("Mips ABI not handled");
|
2012-08-18 05:28:04 +08:00
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
case Triple::ppc64: // Fall through.
|
2013-07-26 09:35:43 +08:00
|
|
|
case Triple::ppc64le:
|
2012-11-03 03:45:23 +08:00
|
|
|
resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
|
2012-10-25 21:13:48 +08:00
|
|
|
break;
|
2013-05-03 22:15:35 +08:00
|
|
|
case Triple::systemz:
|
|
|
|
resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
|
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unsupported CPU type!");
|
2012-01-16 16:56:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 04:21:45 +08:00
|
|
|
void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const {
|
|
|
|
return (void*)(Sections[SectionID].ObjAddress + Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) {
|
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
|
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
|
|
|
}
|
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
relocation_iterator RuntimeDyldELF::processRelocationRef(
|
2015-06-20 04:58:43 +08:00
|
|
|
unsigned SectionID, relocation_iterator RelI, const ObjectFile &O,
|
|
|
|
ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) {
|
|
|
|
const auto &Obj = cast<ELFObjectFileBase>(O);
|
2015-06-30 09:53:01 +08:00
|
|
|
uint64_t RelType = RelI->getType();
|
2015-06-30 08:33:59 +08:00
|
|
|
ErrorOr<int64_t> AddendOrErr = ELFRelocationRef(*RelI).getAddend();
|
|
|
|
int64_t Addend = AddendOrErr ? *AddendOrErr : 0;
|
2015-06-26 19:39:57 +08:00
|
|
|
elf_symbol_iterator Symbol = RelI->getSymbol();
|
2012-05-01 18:41:12 +08:00
|
|
|
|
|
|
|
// Obtain the symbol name which is referenced in the relocation
|
|
|
|
StringRef TargetName;
|
2014-11-27 00:54:40 +08:00
|
|
|
if (Symbol != Obj.symbol_end())
|
2013-06-05 10:55:01 +08:00
|
|
|
Symbol->getName(TargetName);
|
2014-03-22 04:28:42 +08:00
|
|
|
DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
|
|
|
|
<< " TargetName: " << TargetName << "\n");
|
2012-05-01 18:41:12 +08:00
|
|
|
RelocationValueRef Value;
|
|
|
|
// First search for the symbol in the local symbol table
|
2013-06-05 10:55:01 +08:00
|
|
|
SymbolRef::Type SymType = SymbolRef::ST_Unknown;
|
2014-11-27 13:40:13 +08:00
|
|
|
|
|
|
|
// Search for the symbol in the global symbol table
|
2015-01-17 07:13:56 +08:00
|
|
|
RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end();
|
2014-11-27 00:54:40 +08:00
|
|
|
if (Symbol != Obj.symbol_end()) {
|
2014-11-27 13:40:13 +08:00
|
|
|
gsi = GlobalSymbolTable.find(TargetName.data());
|
2015-06-26 20:18:49 +08:00
|
|
|
SymType = Symbol->getType();
|
2013-06-05 10:55:01 +08:00
|
|
|
}
|
2014-11-27 13:40:13 +08:00
|
|
|
if (gsi != GlobalSymbolTable.end()) {
|
2015-01-17 07:13:56 +08:00
|
|
|
const auto &SymInfo = gsi->second;
|
|
|
|
Value.SectionID = SymInfo.getSectionID();
|
|
|
|
Value.Offset = SymInfo.getOffset();
|
|
|
|
Value.Addend = SymInfo.getOffset() + Addend;
|
2012-03-31 00:45:19 +08:00
|
|
|
} else {
|
2014-11-27 13:40:13 +08:00
|
|
|
switch (SymType) {
|
|
|
|
case SymbolRef::ST_Debug: {
|
|
|
|
// TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
|
|
|
|
// and can be changed by another developers. Maybe best way is add
|
|
|
|
// a new symbol type ST_Section to SymbolRef and use it.
|
|
|
|
section_iterator si(Obj.section_end());
|
|
|
|
Symbol->getSection(si);
|
|
|
|
if (si == Obj.section_end())
|
|
|
|
llvm_unreachable("Symbol section not found, bad object file format!");
|
|
|
|
DEBUG(dbgs() << "\t\tThis is section symbol\n");
|
|
|
|
bool isCode = si->isText();
|
|
|
|
Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID);
|
|
|
|
Value.Addend = Addend;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolRef::ST_Data:
|
|
|
|
case SymbolRef::ST_Unknown: {
|
|
|
|
Value.SymbolName = TargetName.data();
|
|
|
|
Value.Addend = Addend;
|
|
|
|
|
|
|
|
// Absolute relocations will have a zero symbol ID (STN_UNDEF), which
|
|
|
|
// will manifest here as a NULL symbol name.
|
|
|
|
// We can set this as a valid (but empty) symbol name, and rely
|
|
|
|
// on addRelocationForSymbol to handle this.
|
|
|
|
if (!Value.SymbolName)
|
|
|
|
Value.SymbolName = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unresolved symbol type!");
|
|
|
|
break;
|
2012-03-31 00:45:19 +08:00
|
|
|
}
|
2012-01-16 16:56:09 +08:00
|
|
|
}
|
2014-11-27 13:40:13 +08:00
|
|
|
|
2015-06-30 07:29:12 +08:00
|
|
|
uint64_t Offset = RelI->getOffset();
|
2013-04-29 22:44:23 +08:00
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
|
2012-03-31 00:45:19 +08:00
|
|
|
<< "\n");
|
2014-07-23 20:32:47 +08:00
|
|
|
if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be) &&
|
2014-03-22 04:28:42 +08:00
|
|
|
(RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) {
|
2013-05-05 04:14:09 +08:00
|
|
|
// This is an AArch64 branch relocation, need to use a stub function.
|
|
|
|
DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
|
|
|
|
SectionEntry &Section = Sections[SectionID];
|
|
|
|
|
|
|
|
// Look for an existing stub.
|
|
|
|
StubMap::const_iterator i = Stubs.find(Value);
|
|
|
|
if (i != Stubs.end()) {
|
2014-03-22 04:28:42 +08:00
|
|
|
resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
|
|
|
|
RelType, 0);
|
2013-05-05 04:14:09 +08:00
|
|
|
DEBUG(dbgs() << " Stub function found\n");
|
|
|
|
} else {
|
|
|
|
// Create a new stub function.
|
|
|
|
DEBUG(dbgs() << " Create a new stub function\n");
|
|
|
|
Stubs[Value] = Section.StubOffset;
|
2014-03-22 04:28:42 +08:00
|
|
|
uint8_t *StubTargetAddr =
|
|
|
|
createStubFunction(Section.Address + Section.StubOffset);
|
2013-05-05 04:14:09 +08:00
|
|
|
|
2014-03-22 04:28:42 +08:00
|
|
|
RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address,
|
2014-09-07 12:13:13 +08:00
|
|
|
ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
|
2014-03-22 04:28:42 +08:00
|
|
|
RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4,
|
2014-09-07 12:13:13 +08:00
|
|
|
ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
|
2014-03-22 04:28:42 +08:00
|
|
|
RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8,
|
2014-09-07 12:13:13 +08:00
|
|
|
ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
|
2013-05-05 04:14:09 +08:00
|
|
|
RelocationEntry REmovk_g0(SectionID,
|
|
|
|
StubTargetAddr - Section.Address + 12,
|
|
|
|
ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
|
|
|
|
|
|
|
|
if (Value.SymbolName) {
|
|
|
|
addRelocationForSymbol(REmovz_g3, Value.SymbolName);
|
|
|
|
addRelocationForSymbol(REmovk_g2, Value.SymbolName);
|
|
|
|
addRelocationForSymbol(REmovk_g1, Value.SymbolName);
|
|
|
|
addRelocationForSymbol(REmovk_g0, Value.SymbolName);
|
|
|
|
} else {
|
|
|
|
addRelocationForSection(REmovz_g3, Value.SectionID);
|
|
|
|
addRelocationForSection(REmovk_g2, Value.SectionID);
|
|
|
|
addRelocationForSection(REmovk_g1, Value.SectionID);
|
|
|
|
addRelocationForSection(REmovk_g0, Value.SectionID);
|
|
|
|
}
|
|
|
|
resolveRelocation(Section, Offset,
|
2014-03-22 04:28:42 +08:00
|
|
|
(uint64_t)Section.Address + Section.StubOffset, RelType,
|
|
|
|
0);
|
2013-05-05 04:14:09 +08:00
|
|
|
Section.StubOffset += getMaxStubSize();
|
|
|
|
}
|
2015-05-02 04:21:45 +08:00
|
|
|
} else if (Arch == Triple::arm) {
|
|
|
|
if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
|
|
|
|
RelType == ELF::R_ARM_JUMP24) {
|
|
|
|
// This is an ARM branch relocation, need to use a stub function.
|
|
|
|
DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
|
|
|
|
SectionEntry &Section = Sections[SectionID];
|
2012-03-31 00:45:19 +08:00
|
|
|
|
2015-05-02 04:21:45 +08:00
|
|
|
// Look for an existing stub.
|
|
|
|
StubMap::const_iterator i = Stubs.find(Value);
|
|
|
|
if (i != Stubs.end()) {
|
|
|
|
resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
|
|
|
|
RelType, 0);
|
|
|
|
DEBUG(dbgs() << " Stub function found\n");
|
|
|
|
} else {
|
|
|
|
// Create a new stub function.
|
|
|
|
DEBUG(dbgs() << " Create a new stub function\n");
|
|
|
|
Stubs[Value] = Section.StubOffset;
|
|
|
|
uint8_t *StubTargetAddr =
|
2015-04-16 16:58:15 +08:00
|
|
|
createStubFunction(Section.Address + Section.StubOffset);
|
2015-05-02 04:21:45 +08:00
|
|
|
RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
|
|
|
|
ELF::R_ARM_ABS32, Value.Addend);
|
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
|
|
|
|
|
|
|
resolveRelocation(Section, Offset,
|
|
|
|
(uint64_t)Section.Address + Section.StubOffset, RelType,
|
|
|
|
0);
|
|
|
|
Section.StubOffset += getMaxStubSize();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint32_t *Placeholder =
|
|
|
|
reinterpret_cast<uint32_t*>(computePlaceholderAddress(SectionID, Offset));
|
|
|
|
if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
|
|
|
|
RelType == ELF::R_ARM_ABS32) {
|
|
|
|
Value.Addend += *Placeholder;
|
|
|
|
} else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
|
|
|
|
// See ELF for ARM documentation
|
|
|
|
Value.Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
|
|
|
|
}
|
|
|
|
processSimpleRelocation(SectionID, Offset, RelType, Value);
|
2012-03-31 00:45:19 +08:00
|
|
|
}
|
2015-05-28 21:48:41 +08:00
|
|
|
} else if (IsMipsO32ABI) {
|
2015-06-03 18:27:28 +08:00
|
|
|
uint8_t *Placeholder = reinterpret_cast<uint8_t *>(
|
|
|
|
computePlaceholderAddress(SectionID, Offset));
|
|
|
|
uint32_t Opcode = readBytesUnaligned(Placeholder, 4);
|
2015-05-02 04:21:45 +08:00
|
|
|
if (RelType == ELF::R_MIPS_26) {
|
|
|
|
// This is an Mips branch relocation, need to use a stub function.
|
|
|
|
DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
|
|
|
|
SectionEntry &Section = Sections[SectionID];
|
2012-08-18 05:28:04 +08:00
|
|
|
|
2015-05-02 04:21:45 +08:00
|
|
|
// Extract the addend from the instruction.
|
|
|
|
// We shift up by two since the Value will be down shifted again
|
|
|
|
// when applying the relocation.
|
2015-06-03 18:27:28 +08:00
|
|
|
uint32_t Addend = (Opcode & 0x03ffffff) << 2;
|
2012-08-18 05:28:04 +08:00
|
|
|
|
2015-05-02 04:21:45 +08:00
|
|
|
Value.Addend += Addend;
|
2012-08-18 05:28:04 +08:00
|
|
|
|
2015-05-02 04:21:45 +08:00
|
|
|
// Look up for existing stub.
|
|
|
|
StubMap::const_iterator i = Stubs.find(Value);
|
|
|
|
if (i != Stubs.end()) {
|
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, i->second);
|
|
|
|
addRelocationForSection(RE, SectionID);
|
|
|
|
DEBUG(dbgs() << " Stub function found\n");
|
|
|
|
} else {
|
|
|
|
// Create a new stub function.
|
|
|
|
DEBUG(dbgs() << " Create a new stub function\n");
|
|
|
|
Stubs[Value] = Section.StubOffset;
|
|
|
|
uint8_t *StubTargetAddr =
|
2014-03-22 04:28:42 +08:00
|
|
|
createStubFunction(Section.Address + Section.StubOffset);
|
2012-08-18 05:28:04 +08:00
|
|
|
|
2015-05-02 04:21:45 +08:00
|
|
|
// Creating Hi and Lo relocations for the filled stub instructions.
|
|
|
|
RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address,
|
|
|
|
ELF::R_MIPS_HI16, Value.Addend);
|
|
|
|
RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4,
|
|
|
|
ELF::R_MIPS_LO16, Value.Addend);
|
2012-08-18 05:28:04 +08:00
|
|
|
|
2015-05-02 04:21:45 +08:00
|
|
|
if (Value.SymbolName) {
|
|
|
|
addRelocationForSymbol(REHi, Value.SymbolName);
|
|
|
|
addRelocationForSymbol(RELo, Value.SymbolName);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
addRelocationForSection(REHi, Value.SectionID);
|
|
|
|
addRelocationForSection(RELo, Value.SectionID);
|
|
|
|
}
|
2015-04-16 16:58:15 +08:00
|
|
|
|
2015-05-02 04:21:45 +08:00
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
|
|
|
|
addRelocationForSection(RE, SectionID);
|
|
|
|
Section.StubOffset += getMaxStubSize();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (RelType == ELF::R_MIPS_HI16)
|
2015-06-03 18:27:28 +08:00
|
|
|
Value.Addend += (Opcode & 0x0000ffff) << 16;
|
2015-05-02 04:21:45 +08:00
|
|
|
else if (RelType == ELF::R_MIPS_LO16)
|
2015-06-03 18:27:28 +08:00
|
|
|
Value.Addend += (Opcode & 0x0000ffff);
|
2015-05-02 04:21:45 +08:00
|
|
|
else if (RelType == ELF::R_MIPS_32)
|
2015-06-03 18:27:28 +08:00
|
|
|
Value.Addend += Opcode;
|
2015-05-02 04:21:45 +08:00
|
|
|
processSimpleRelocation(SectionID, Offset, RelType, Value);
|
2012-08-18 05:28:04 +08:00
|
|
|
}
|
2015-05-28 21:48:41 +08:00
|
|
|
} else if (IsMipsN64ABI) {
|
|
|
|
uint32_t r_type = RelType & 0xff;
|
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
|
|
|
|
if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
|
|
|
|
|| r_type == ELF::R_MIPS_GOT_DISP) {
|
|
|
|
StringMap<uint64_t>::iterator i = GOTSymbolOffsets.find(TargetName);
|
|
|
|
if (i != GOTSymbolOffsets.end())
|
|
|
|
RE.SymOffset = i->second;
|
|
|
|
else {
|
|
|
|
RE.SymOffset = allocateGOTEntries(SectionID, 1);
|
|
|
|
GOTSymbolOffsets[TargetName] = RE.SymOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
2013-07-26 09:35:43 +08:00
|
|
|
} else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
|
2012-10-25 21:13:48 +08:00
|
|
|
if (RelType == ELF::R_PPC64_REL24) {
|
2014-07-21 07:53:14 +08:00
|
|
|
// Determine ABI variant in use for this object.
|
|
|
|
unsigned AbiVariant;
|
2014-11-27 00:54:40 +08:00
|
|
|
Obj.getPlatformFlags(AbiVariant);
|
2014-07-21 07:53:14 +08:00
|
|
|
AbiVariant &= ELF::EF_PPC64_ABI;
|
2012-10-25 21:13:48 +08:00
|
|
|
// A PPC branch relocation will need a stub function if the target is
|
|
|
|
// an external symbol (Symbol::ST_Unknown) or if the target address
|
|
|
|
// is not within the signed 24-bits branch address.
|
2013-04-29 22:44:23 +08:00
|
|
|
SectionEntry &Section = Sections[SectionID];
|
|
|
|
uint8_t *Target = Section.Address + Offset;
|
2012-10-25 21:13:48 +08:00
|
|
|
bool RangeOverflow = false;
|
|
|
|
if (SymType != SymbolRef::ST_Unknown) {
|
2014-07-21 07:53:14 +08:00
|
|
|
if (AbiVariant != 2) {
|
|
|
|
// In the ELFv1 ABI, a function call may point to the .opd entry,
|
|
|
|
// so the final symbol value is calculated based on the relocation
|
|
|
|
// values in the .opd section.
|
|
|
|
findOPDEntrySection(Obj, ObjSectionToID, Value);
|
|
|
|
} else {
|
|
|
|
// In the ELFv2 ABI, a function symbol may provide a local entry
|
|
|
|
// point, which must be used for direct calls.
|
2015-06-26 19:39:57 +08:00
|
|
|
uint8_t SymOther = Symbol->getOther();
|
2014-07-21 07:53:14 +08:00
|
|
|
Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
|
|
|
|
}
|
2012-10-25 21:13:48 +08:00
|
|
|
uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
|
|
|
|
int32_t delta = static_cast<int32_t>(Target - RelocTarget);
|
|
|
|
// If it is within 24-bits branch range, just set the branch target
|
|
|
|
if (SignExtend32<24>(delta) == delta) {
|
2013-04-29 22:44:23 +08:00
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
|
2012-10-25 21:13:48 +08:00
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
|
|
|
} else {
|
|
|
|
RangeOverflow = true;
|
|
|
|
}
|
|
|
|
}
|
2015-03-09 09:57:13 +08:00
|
|
|
if (SymType == SymbolRef::ST_Unknown || RangeOverflow) {
|
2012-10-25 21:13:48 +08:00
|
|
|
// It is an external symbol (SymbolRef::ST_Unknown) or within a range
|
|
|
|
// larger than 24-bits.
|
|
|
|
StubMap::const_iterator i = Stubs.find(Value);
|
|
|
|
if (i != Stubs.end()) {
|
|
|
|
// Symbol function stub already created, just relocate to it
|
2013-04-29 22:44:23 +08:00
|
|
|
resolveRelocation(Section, Offset,
|
2012-11-03 03:45:23 +08:00
|
|
|
(uint64_t)Section.Address + i->second, RelType, 0);
|
2012-10-25 21:13:48 +08:00
|
|
|
DEBUG(dbgs() << " Stub function found\n");
|
|
|
|
} else {
|
|
|
|
// Create a new stub function.
|
|
|
|
DEBUG(dbgs() << " Create a new stub function\n");
|
|
|
|
Stubs[Value] = Section.StubOffset;
|
2014-03-22 04:28:42 +08:00
|
|
|
uint8_t *StubTargetAddr =
|
2014-07-21 07:53:14 +08:00
|
|
|
createStubFunction(Section.Address + Section.StubOffset,
|
|
|
|
AbiVariant);
|
2013-04-29 22:44:23 +08:00
|
|
|
RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
|
2012-10-25 21:13:48 +08:00
|
|
|
ELF::R_PPC64_ADDR64, Value.Addend);
|
|
|
|
|
|
|
|
// Generates the 64-bits address loads as exemplified in section
|
2014-06-21 02:17:56 +08:00
|
|
|
// 4.5.1 in PPC64 ELF ABI. Note that the relocations need to
|
|
|
|
// apply to the low part of the instructions, so we have to update
|
|
|
|
// the offset according to the target endianness.
|
|
|
|
uint64_t StubRelocOffset = StubTargetAddr - Section.Address;
|
|
|
|
if (!IsTargetLittleEndian)
|
|
|
|
StubRelocOffset += 2;
|
|
|
|
|
|
|
|
RelocationEntry REhst(SectionID, StubRelocOffset + 0,
|
2012-10-25 21:13:48 +08:00
|
|
|
ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
|
2014-06-21 02:17:56 +08:00
|
|
|
RelocationEntry REhr(SectionID, StubRelocOffset + 4,
|
2012-10-25 21:13:48 +08:00
|
|
|
ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
|
2014-06-21 02:17:56 +08:00
|
|
|
RelocationEntry REh(SectionID, StubRelocOffset + 12,
|
2012-10-25 21:13:48 +08:00
|
|
|
ELF::R_PPC64_ADDR16_HI, Value.Addend);
|
2014-06-21 02:17:56 +08:00
|
|
|
RelocationEntry REl(SectionID, StubRelocOffset + 16,
|
2012-10-25 21:13:48 +08:00
|
|
|
ELF::R_PPC64_ADDR16_LO, Value.Addend);
|
|
|
|
|
|
|
|
if (Value.SymbolName) {
|
|
|
|
addRelocationForSymbol(REhst, Value.SymbolName);
|
2014-03-22 04:28:42 +08:00
|
|
|
addRelocationForSymbol(REhr, Value.SymbolName);
|
|
|
|
addRelocationForSymbol(REh, Value.SymbolName);
|
|
|
|
addRelocationForSymbol(REl, Value.SymbolName);
|
2012-10-25 21:13:48 +08:00
|
|
|
} else {
|
|
|
|
addRelocationForSection(REhst, Value.SectionID);
|
2014-03-22 04:28:42 +08:00
|
|
|
addRelocationForSection(REhr, Value.SectionID);
|
|
|
|
addRelocationForSection(REh, Value.SectionID);
|
|
|
|
addRelocationForSection(REl, Value.SectionID);
|
2012-10-25 21:13:48 +08:00
|
|
|
}
|
|
|
|
|
2013-04-29 22:44:23 +08:00
|
|
|
resolveRelocation(Section, Offset,
|
2012-11-03 03:45:23 +08:00
|
|
|
(uint64_t)Section.Address + Section.StubOffset,
|
|
|
|
RelType, 0);
|
2012-10-25 21:13:48 +08:00
|
|
|
Section.StubOffset += getMaxStubSize();
|
|
|
|
}
|
2014-07-21 07:53:14 +08:00
|
|
|
if (SymType == SymbolRef::ST_Unknown) {
|
2014-03-11 23:26:27 +08:00
|
|
|
// Restore the TOC for external calls
|
2014-07-21 07:53:14 +08:00
|
|
|
if (AbiVariant == 2)
|
|
|
|
writeInt32BE(Target + 4, 0xE8410018); // ld r2,28(r1)
|
|
|
|
else
|
|
|
|
writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
|
|
|
|
}
|
2012-10-25 21:13:48 +08:00
|
|
|
}
|
[RuntimeDyld, PowerPC] Fix/improve handling of TOC relocations
Current PPC64 RuntimeDyld code to handle TOC relocations has two
problems:
- With recent linkers, in addition to the relocations that implicitly
refer to the TOC base (R_PPC64_TOC*), you can now also use the .TOC.
magic symbol with any other relocation to refer to the TOC base
explicitly. This isn't currently used much in ELFv1 code (although
it could be), but it is essential in ELFv2 code.
- In a complex JIT environment with multiple modules, each module may
have its own .toc section, and TOC relocations in one module must
refer to *its own* TOC section. The current findPPC64TOC implementation
does not correctly implement this; in fact, it will always return the
address of the first TOC section it finds anywhere. (Note that at the
time findPPC64TOC is called, we don't even *know* which module the
relocation originally resided in, so it is not even possible to fix
this routine as-is.)
This commit fixes both problems by handling TOC relocations earlier, in
processRelocationRef. To do this, I've removed the findPPC64TOC routine
and replaced it by a new routine findPPC64TOCSection, which works
analogously to findOPDEntrySection in scanning the sections of the
ObjImage provided by its caller, processRelocationRef. This solves the
issue of finding the correct TOC section associated with the current
module.
This makes it straightforward to implement both R_PPC64_TOC relocations,
and relocations explicitly refering to the .TOC. symbol, directly in
processRelocationRef. There is now a new problem in implementing the
R_PPC64_TOC16* relocations, because those can now in theory involve
*three* different sections: the relocation may be applied in section A,
refer explicitly to a symbol in section B, and refer implicitly to the
TOC section C. The final processing of the relocation thus may only
happen after all three of these sections have been assigned final
addresses. There is currently no obvious means to implement this in
its general form with the common-code RuntimeDyld infrastructure.
Fortunately, ppc64 code usually makes no use of this most general form;
in fact, TOC16 relocations are only ever generated by LLVM for symbols
residing themselves in the TOC, which means "section B" == "section C"
in the above terminology. This special case can easily be handled with
the current infrastructure, and that is what this patch does.
[ Unhandled cases result in an explicit error, unlike the current code
which silently returns the wrong TOC base address ... ]
This patch makes the JIT work on both BE and LE (ELFv2 requires
additional patches, of course), and allowed me to successfully run
complex JIT scenarios (via mesa/llvmpipe).
Reviewed by Hal Finkel.
llvm-svn: 211885
2014-06-27 18:32:14 +08:00
|
|
|
} else if (RelType == ELF::R_PPC64_TOC16 ||
|
|
|
|
RelType == ELF::R_PPC64_TOC16_DS ||
|
|
|
|
RelType == ELF::R_PPC64_TOC16_LO ||
|
|
|
|
RelType == ELF::R_PPC64_TOC16_LO_DS ||
|
|
|
|
RelType == ELF::R_PPC64_TOC16_HI ||
|
|
|
|
RelType == ELF::R_PPC64_TOC16_HA) {
|
|
|
|
// These relocations are supposed to subtract the TOC address from
|
|
|
|
// the final value. This does not fit cleanly into the RuntimeDyld
|
|
|
|
// scheme, since there may be *two* sections involved in determining
|
|
|
|
// the relocation value (the section of the symbol refered to by the
|
|
|
|
// relocation, and the TOC section associated with the current module).
|
|
|
|
//
|
|
|
|
// Fortunately, these relocations are currently only ever generated
|
|
|
|
// refering to symbols that themselves reside in the TOC, which means
|
|
|
|
// that the two sections are actually the same. Thus they cancel out
|
|
|
|
// and we can immediately resolve the relocation right now.
|
|
|
|
switch (RelType) {
|
|
|
|
case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
|
|
|
|
case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
|
|
|
|
case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
|
|
|
|
case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
|
|
|
|
case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
|
|
|
|
case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
|
|
|
|
default: llvm_unreachable("Wrong relocation type.");
|
|
|
|
}
|
|
|
|
|
|
|
|
RelocationValueRef TOCValue;
|
|
|
|
findPPC64TOCSection(Obj, ObjSectionToID, TOCValue);
|
|
|
|
if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
|
|
|
|
llvm_unreachable("Unsupported TOC relocation.");
|
|
|
|
Value.Addend -= TOCValue.Addend;
|
|
|
|
resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
|
2012-10-25 21:13:48 +08:00
|
|
|
} else {
|
[RuntimeDyld, PowerPC] Fix/improve handling of TOC relocations
Current PPC64 RuntimeDyld code to handle TOC relocations has two
problems:
- With recent linkers, in addition to the relocations that implicitly
refer to the TOC base (R_PPC64_TOC*), you can now also use the .TOC.
magic symbol with any other relocation to refer to the TOC base
explicitly. This isn't currently used much in ELFv1 code (although
it could be), but it is essential in ELFv2 code.
- In a complex JIT environment with multiple modules, each module may
have its own .toc section, and TOC relocations in one module must
refer to *its own* TOC section. The current findPPC64TOC implementation
does not correctly implement this; in fact, it will always return the
address of the first TOC section it finds anywhere. (Note that at the
time findPPC64TOC is called, we don't even *know* which module the
relocation originally resided in, so it is not even possible to fix
this routine as-is.)
This commit fixes both problems by handling TOC relocations earlier, in
processRelocationRef. To do this, I've removed the findPPC64TOC routine
and replaced it by a new routine findPPC64TOCSection, which works
analogously to findOPDEntrySection in scanning the sections of the
ObjImage provided by its caller, processRelocationRef. This solves the
issue of finding the correct TOC section associated with the current
module.
This makes it straightforward to implement both R_PPC64_TOC relocations,
and relocations explicitly refering to the .TOC. symbol, directly in
processRelocationRef. There is now a new problem in implementing the
R_PPC64_TOC16* relocations, because those can now in theory involve
*three* different sections: the relocation may be applied in section A,
refer explicitly to a symbol in section B, and refer implicitly to the
TOC section C. The final processing of the relocation thus may only
happen after all three of these sections have been assigned final
addresses. There is currently no obvious means to implement this in
its general form with the common-code RuntimeDyld infrastructure.
Fortunately, ppc64 code usually makes no use of this most general form;
in fact, TOC16 relocations are only ever generated by LLVM for symbols
residing themselves in the TOC, which means "section B" == "section C"
in the above terminology. This special case can easily be handled with
the current infrastructure, and that is what this patch does.
[ Unhandled cases result in an explicit error, unlike the current code
which silently returns the wrong TOC base address ... ]
This patch makes the JIT work on both BE and LE (ELFv2 requires
additional patches, of course), and allowed me to successfully run
complex JIT scenarios (via mesa/llvmpipe).
Reviewed by Hal Finkel.
llvm-svn: 211885
2014-06-27 18:32:14 +08:00
|
|
|
// There are two ways to refer to the TOC address directly: either
|
|
|
|
// via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
|
|
|
|
// ignored), or via any relocation that refers to the magic ".TOC."
|
|
|
|
// symbols (in which case the addend is respected).
|
|
|
|
if (RelType == ELF::R_PPC64_TOC) {
|
|
|
|
RelType = ELF::R_PPC64_ADDR64;
|
|
|
|
findPPC64TOCSection(Obj, ObjSectionToID, Value);
|
|
|
|
} else if (TargetName == ".TOC.") {
|
|
|
|
findPPC64TOCSection(Obj, ObjSectionToID, Value);
|
|
|
|
Value.Addend += Addend;
|
|
|
|
}
|
|
|
|
|
2013-04-29 22:44:23 +08:00
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
|
2013-08-17 02:54:26 +08:00
|
|
|
|
|
|
|
if (Value.SymbolName)
|
2012-10-25 21:13:48 +08:00
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
|
|
|
}
|
2013-05-03 22:15:35 +08:00
|
|
|
} else if (Arch == Triple::systemz &&
|
2014-03-22 04:28:42 +08:00
|
|
|
(RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
|
2013-05-03 22:15:35 +08:00
|
|
|
// Create function stubs for both PLT and GOT references, regardless of
|
|
|
|
// whether the GOT reference is to data or code. The stub contains the
|
|
|
|
// full address of the symbol, as needed by GOT references, and the
|
|
|
|
// executable part only adds an overhead of 8 bytes.
|
|
|
|
//
|
|
|
|
// We could try to conserve space by allocating the code and data
|
|
|
|
// parts of the stub separately. However, as things stand, we allocate
|
|
|
|
// a stub for every relocation, so using a GOT in JIT code should be
|
|
|
|
// no less space efficient than using an explicit constant pool.
|
|
|
|
DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
|
|
|
|
SectionEntry &Section = Sections[SectionID];
|
|
|
|
|
|
|
|
// Look for an existing stub.
|
|
|
|
StubMap::const_iterator i = Stubs.find(Value);
|
|
|
|
uintptr_t StubAddress;
|
|
|
|
if (i != Stubs.end()) {
|
|
|
|
StubAddress = uintptr_t(Section.Address) + i->second;
|
|
|
|
DEBUG(dbgs() << " Stub function found\n");
|
|
|
|
} else {
|
|
|
|
// Create a new stub function.
|
|
|
|
DEBUG(dbgs() << " Create a new stub function\n");
|
|
|
|
|
|
|
|
uintptr_t BaseAddress = uintptr_t(Section.Address);
|
|
|
|
uintptr_t StubAlignment = getStubAlignment();
|
2014-03-22 04:28:42 +08:00
|
|
|
StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
|
|
|
|
-StubAlignment;
|
2013-05-03 22:15:35 +08:00
|
|
|
unsigned StubOffset = StubAddress - BaseAddress;
|
|
|
|
|
|
|
|
Stubs[Value] = StubOffset;
|
|
|
|
createStubFunction((uint8_t *)StubAddress);
|
2014-03-22 04:28:42 +08:00
|
|
|
RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
|
2014-08-26 07:33:48 +08:00
|
|
|
Value.Offset);
|
2013-05-03 22:15:35 +08:00
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
|
|
|
Section.StubOffset = StubOffset + getMaxStubSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RelType == ELF::R_390_GOTENT)
|
2014-03-22 04:28:42 +08:00
|
|
|
resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
|
|
|
|
Addend);
|
2013-05-03 22:15:35 +08:00
|
|
|
else
|
|
|
|
resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
|
2015-05-02 04:21:45 +08:00
|
|
|
} else if (Arch == Triple::x86_64) {
|
|
|
|
if (RelType == ELF::R_X86_64_PLT32) {
|
|
|
|
// The way the PLT relocations normally work is that the linker allocates
|
|
|
|
// the
|
|
|
|
// PLT and this relocation makes a PC-relative call into the PLT. The PLT
|
|
|
|
// entry will then jump to an address provided by the GOT. On first call,
|
|
|
|
// the
|
|
|
|
// GOT address will point back into PLT code that resolves the symbol. After
|
|
|
|
// the first call, the GOT entry points to the actual function.
|
|
|
|
//
|
|
|
|
// For local functions we're ignoring all of that here and just replacing
|
|
|
|
// the PLT32 relocation type with PC32, which will translate the relocation
|
|
|
|
// into a PC-relative call directly to the function. For external symbols we
|
|
|
|
// can't be sure the function will be within 2^32 bytes of the call site, so
|
|
|
|
// we need to create a stub, which calls into the GOT. This case is
|
|
|
|
// equivalent to the usual PLT implementation except that we use the stub
|
|
|
|
// mechanism in RuntimeDyld (which puts stubs at the end of the section)
|
|
|
|
// rather than allocating a PLT section.
|
|
|
|
if (Value.SymbolName) {
|
|
|
|
// This is a call to an external function.
|
|
|
|
// Look for an existing stub.
|
|
|
|
SectionEntry &Section = Sections[SectionID];
|
|
|
|
StubMap::const_iterator i = Stubs.find(Value);
|
|
|
|
uintptr_t StubAddress;
|
|
|
|
if (i != Stubs.end()) {
|
2013-08-20 07:27:43 +08:00
|
|
|
StubAddress = uintptr_t(Section.Address) + i->second;
|
|
|
|
DEBUG(dbgs() << " Stub function found\n");
|
2015-05-02 04:21:45 +08:00
|
|
|
} else {
|
2013-08-20 07:27:43 +08:00
|
|
|
// Create a new stub function (equivalent to a PLT entry).
|
|
|
|
DEBUG(dbgs() << " Create a new stub function\n");
|
|
|
|
|
|
|
|
uintptr_t BaseAddress = uintptr_t(Section.Address);
|
|
|
|
uintptr_t StubAlignment = getStubAlignment();
|
2014-03-22 04:28:42 +08:00
|
|
|
StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
|
2015-05-02 04:21:45 +08:00
|
|
|
-StubAlignment;
|
2013-08-20 07:27:43 +08:00
|
|
|
unsigned StubOffset = StubAddress - BaseAddress;
|
|
|
|
Stubs[Value] = StubOffset;
|
|
|
|
createStubFunction((uint8_t *)StubAddress);
|
|
|
|
|
|
|
|
// Bump our stub offset counter
|
|
|
|
Section.StubOffset = StubOffset + getMaxStubSize();
|
2015-04-14 10:10:35 +08:00
|
|
|
|
|
|
|
// Allocate a GOT Entry
|
|
|
|
uint64_t GOTOffset = allocateGOTEntries(SectionID, 1);
|
|
|
|
|
|
|
|
// The load of the GOT address has an addend of -4
|
|
|
|
resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4);
|
|
|
|
|
|
|
|
// Fill in the value of the symbol we're targeting into the GOT
|
|
|
|
addRelocationForSymbol(computeGOTOffsetRE(SectionID,GOTOffset,0,ELF::R_X86_64_64),
|
2015-05-02 04:21:45 +08:00
|
|
|
Value.SymbolName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the target call a call into the stub table.
|
|
|
|
resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
|
|
|
|
Addend);
|
|
|
|
} else {
|
|
|
|
RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
|
|
|
|
Value.Offset);
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
2013-08-20 07:27:43 +08:00
|
|
|
}
|
2015-05-02 04:21:45 +08:00
|
|
|
} else if (RelType == ELF::R_X86_64_GOTPCREL) {
|
|
|
|
uint64_t GOTOffset = allocateGOTEntries(SectionID, 1);
|
|
|
|
resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend);
|
2013-08-20 07:27:43 +08:00
|
|
|
|
2015-05-02 04:21:45 +08:00
|
|
|
// Fill in the value of the symbol we're targeting into the GOT
|
|
|
|
RelocationEntry RE = computeGOTOffsetRE(SectionID, GOTOffset, Value.Offset, ELF::R_X86_64_64);
|
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
|
|
|
} else if (RelType == ELF::R_X86_64_PC32) {
|
|
|
|
Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
|
|
|
|
processSimpleRelocation(SectionID, Offset, RelType, Value);
|
|
|
|
} else if (RelType == ELF::R_X86_64_PC64) {
|
|
|
|
Value.Addend += support::ulittle64_t::ref(computePlaceholderAddress(SectionID, Offset));
|
|
|
|
processSimpleRelocation(SectionID, Offset, RelType, Value);
|
2013-08-20 07:27:43 +08:00
|
|
|
} else {
|
2015-05-02 04:21:45 +08:00
|
|
|
processSimpleRelocation(SectionID, Offset, RelType, Value);
|
2013-08-20 07:27:43 +08:00
|
|
|
}
|
2012-05-01 18:41:12 +08:00
|
|
|
} else {
|
2015-05-02 04:21:45 +08:00
|
|
|
if (Arch == Triple::x86) {
|
|
|
|
Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
|
|
|
|
}
|
|
|
|
processSimpleRelocation(SectionID, Offset, RelType, Value);
|
2012-05-01 18:41:12 +08:00
|
|
|
}
|
2014-03-21 15:26:41 +08:00
|
|
|
return ++RelI;
|
2012-01-17 06:26:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-20 07:27:43 +08:00
|
|
|
size_t RuntimeDyldELF::getGOTEntrySize() {
|
|
|
|
// We don't use the GOT in all of these cases, but it's essentially free
|
|
|
|
// to put them all here.
|
|
|
|
size_t Result = 0;
|
|
|
|
switch (Arch) {
|
|
|
|
case Triple::x86_64:
|
|
|
|
case Triple::aarch64:
|
2014-04-30 18:15:41 +08:00
|
|
|
case Triple::aarch64_be:
|
2013-08-20 07:27:43 +08:00
|
|
|
case Triple::ppc64:
|
|
|
|
case Triple::ppc64le:
|
|
|
|
case Triple::systemz:
|
|
|
|
Result = sizeof(uint64_t);
|
|
|
|
break;
|
|
|
|
case Triple::x86:
|
|
|
|
case Triple::arm:
|
|
|
|
case Triple::thumb:
|
2015-05-28 21:48:41 +08:00
|
|
|
Result = sizeof(uint32_t);
|
|
|
|
break;
|
2013-08-20 07:27:43 +08:00
|
|
|
case Triple::mips:
|
|
|
|
case Triple::mipsel:
|
2015-05-28 21:48:41 +08:00
|
|
|
case Triple::mips64:
|
|
|
|
case Triple::mips64el:
|
|
|
|
if (IsMipsO32ABI)
|
|
|
|
Result = sizeof(uint32_t);
|
|
|
|
else if (IsMipsN64ABI)
|
|
|
|
Result = sizeof(uint64_t);
|
|
|
|
else
|
|
|
|
llvm_unreachable("Mips ABI not handled");
|
2013-08-20 07:27:43 +08:00
|
|
|
break;
|
2014-03-22 04:28:42 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unsupported CPU type!");
|
2013-08-20 07:27:43 +08:00
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-04-14 10:10:35 +08:00
|
|
|
uint64_t RuntimeDyldELF::allocateGOTEntries(unsigned SectionID, unsigned no)
|
|
|
|
{
|
|
|
|
(void)SectionID; // The GOT Section is the same for all section in the object file
|
|
|
|
if (GOTSectionID == 0) {
|
|
|
|
GOTSectionID = Sections.size();
|
|
|
|
// Reserve a section id. We'll allocate the section later
|
|
|
|
// once we know the total size
|
|
|
|
Sections.push_back(SectionEntry(".got", 0, 0, 0));
|
2013-08-20 07:27:43 +08:00
|
|
|
}
|
2015-04-14 10:10:35 +08:00
|
|
|
uint64_t StartOffset = CurrentGOTIndex * getGOTEntrySize();
|
|
|
|
CurrentGOTIndex += no;
|
|
|
|
return StartOffset;
|
|
|
|
}
|
2013-08-20 07:27:43 +08:00
|
|
|
|
2015-04-14 10:10:35 +08:00
|
|
|
void RuntimeDyldELF::resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset, uint64_t GOTOffset)
|
|
|
|
{
|
|
|
|
// Fill in the relative address of the GOT Entry into the stub
|
|
|
|
RelocationEntry GOTRE(SectionID, Offset, ELF::R_X86_64_PC32, GOTOffset);
|
|
|
|
addRelocationForSection(GOTRE, GOTSectionID);
|
|
|
|
}
|
|
|
|
|
|
|
|
RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(unsigned SectionID, uint64_t GOTOffset, uint64_t SymbolOffset,
|
|
|
|
uint32_t Type)
|
|
|
|
{
|
|
|
|
(void)SectionID; // The GOT Section is the same for all section in the object file
|
|
|
|
return RelocationEntry(GOTSectionID, GOTOffset, Type, SymbolOffset);
|
2013-08-20 07:27:43 +08:00
|
|
|
}
|
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
void RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj,
|
2014-05-13 05:39:59 +08:00
|
|
|
ObjSectionToIDMap &SectionMap) {
|
2013-10-12 05:25:48 +08:00
|
|
|
// If necessary, allocate the global offset table
|
2015-04-14 10:10:35 +08:00
|
|
|
if (GOTSectionID != 0) {
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
// Allocate memory for the section
|
2015-04-14 10:10:35 +08:00
|
|
|
size_t TotalSize = CurrentGOTIndex * getGOTEntrySize();
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
uint8_t *Addr = MemMgr.allocateDataSection(TotalSize, getGOTEntrySize(),
|
2015-04-14 10:10:35 +08:00
|
|
|
GOTSectionID, ".got", false);
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
if (!Addr)
|
|
|
|
report_fatal_error("Unable to allocate memory for GOT!");
|
|
|
|
|
2015-04-14 10:10:35 +08:00
|
|
|
Sections[GOTSectionID] = SectionEntry(".got", Addr, TotalSize, 0);
|
|
|
|
|
|
|
|
if (Checker)
|
|
|
|
Checker->registerSection(Obj.getFileName(), GOTSectionID);
|
|
|
|
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
// For now, initialize all GOT entries to zero. We'll fill them in as
|
|
|
|
// needed when GOT-based relocations are applied.
|
|
|
|
memset(Addr, 0, TotalSize);
|
2015-05-28 21:48:41 +08:00
|
|
|
if (IsMipsN64ABI) {
|
|
|
|
// To correctly resolve Mips GOT relocations, we need a mapping from
|
|
|
|
// object's sections to GOTs.
|
|
|
|
for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
|
|
|
|
SI != SE; ++SI) {
|
|
|
|
if (SI->relocation_begin() != SI->relocation_end()) {
|
|
|
|
section_iterator RelocatedSection = SI->getRelocatedSection();
|
|
|
|
ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection);
|
|
|
|
assert (i != SectionMap.end());
|
|
|
|
SectionToGOTMap[i->second] = GOTSectionID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GOTSymbolOffsets.clear();
|
|
|
|
}
|
2013-08-20 07:27:43 +08:00
|
|
|
}
|
2013-10-12 05:25:48 +08:00
|
|
|
|
|
|
|
// Look for and record the EH frame section.
|
|
|
|
ObjSectionToIDMap::iterator i, e;
|
|
|
|
for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
|
|
|
|
const SectionRef &Section = i->first;
|
|
|
|
StringRef Name;
|
|
|
|
Section.getName(Name);
|
|
|
|
if (Name == ".eh_frame") {
|
|
|
|
UnregisteredEHFrameSections.push_back(i->second);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 10:10:35 +08:00
|
|
|
|
|
|
|
GOTSectionID = 0;
|
|
|
|
CurrentGOTIndex = 0;
|
2013-08-20 07:27:43 +08:00
|
|
|
}
|
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile &Obj) const {
|
|
|
|
return Obj.isELF();
|
2014-01-08 12:09:09 +08:00
|
|
|
}
|
|
|
|
|
2012-01-16 16:56:09 +08:00
|
|
|
} // namespace llvm
|