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"
|
2016-12-13 19:39:18 +08:00
|
|
|
#include "Targets/RuntimeDyldELFMips.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;
|
2016-12-27 21:33:32 +08:00
|
|
|
using namespace llvm::support::endian;
|
2012-01-16 16:56:09 +08:00
|
|
|
|
2014-04-22 11:04:17 +08:00
|
|
|
#define DEBUG_TYPE "dyld"
|
|
|
|
|
2016-12-27 21:33:32 +08:00
|
|
|
static void or32le(void *P, int32_t V) { write32le(P, read32le(P) | V); }
|
|
|
|
|
|
|
|
static void or32AArch64Imm(void *L, uint64_t Imm) {
|
|
|
|
or32le(L, (Imm & 0xFFF) << 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T> static void write(bool isBE, void *P, T V) {
|
|
|
|
isBE ? write<T, support::big>(P, V) : write<T, support::little>(P, V);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write32AArch64Addr(void *L, uint64_t Imm) {
|
|
|
|
uint32_t ImmLo = (Imm & 0x3) << 29;
|
|
|
|
uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
|
|
|
|
uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
|
|
|
|
write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the bits [Start, End] from Val shifted Start bits.
|
|
|
|
// For instance, getBits(0xF0, 4, 8) returns 0xF.
|
|
|
|
static uint64_t getBits(uint64_t Val, int Start, int End) {
|
|
|
|
uint64_t Mask = ((uint64_t)1 << (End + 1 - Start)) - 1;
|
|
|
|
return (Val >> Start) & Mask;
|
|
|
|
}
|
|
|
|
|
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-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-08-06 04:20:29 +08:00
|
|
|
class LoadedELFObjectInfo final
|
2015-05-22 18:11:07 +08:00
|
|
|
: public RuntimeDyld::LoadedObjectInfoHelper<LoadedELFObjectInfo> {
|
2014-11-27 00:54:40 +08:00
|
|
|
public:
|
2015-07-29 01:52:11 +08:00
|
|
|
LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
|
|
|
|
: LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
|
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,
|
2015-07-29 01:52:11 +08:00
|
|
|
const ObjectFile &SourceObject,
|
2014-11-27 00:54:40 +08:00
|
|
|
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.
|
2015-07-29 01:52:11 +08:00
|
|
|
auto SI = SourceObject.section_begin();
|
2014-11-27 00:54:40 +08:00
|
|
|
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));
|
|
|
|
|
2015-07-29 01:52:11 +08:00
|
|
|
if (uint64_t SecLoadAddr = L.getSectionLoadAddress(*SI)) {
|
2014-11-27 00:54:40 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 01:52:11 +08:00
|
|
|
++SI;
|
2014-11-27 00:54:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2015-07-29 01:52:11 +08:00
|
|
|
DebugObj = createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj, L,
|
|
|
|
ec);
|
2014-11-27 00:54:40 +08:00
|
|
|
} else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian()) {
|
2015-06-02 20:05:27 +08:00
|
|
|
typedef ELFType<support::big, false> ELF32BE;
|
2015-07-29 01:52:11 +08:00
|
|
|
DebugObj = createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj, L,
|
|
|
|
ec);
|
2014-11-27 00:54:40 +08:00
|
|
|
} else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian()) {
|
2015-06-02 20:05:27 +08:00
|
|
|
typedef ELFType<support::big, true> ELF64BE;
|
2015-07-29 01:52:11 +08:00
|
|
|
DebugObj = createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj, L,
|
|
|
|
ec);
|
2014-11-27 00:54:40 +08:00
|
|
|
} else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian()) {
|
2015-06-02 20:05:27 +08:00
|
|
|
typedef ELFType<support::little, true> ELF64LE;
|
2015-07-29 01:52:11 +08:00
|
|
|
DebugObj = createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj, L,
|
|
|
|
ec);
|
2014-11-27 00:54:40 +08:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
|
2015-10-07 07:24:35 +08:00
|
|
|
} // anonymous namespace
|
2012-04-17 06:12:58 +08:00
|
|
|
|
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,
|
2016-08-02 04:49:11 +08:00
|
|
|
JITSymbolResolver &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];
|
2015-11-24 05:47:41 +08:00
|
|
|
uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
|
|
|
|
uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
|
|
|
|
size_t EHFrameSize = Sections[EHFrameSID].getSize();
|
[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];
|
2015-11-24 05:47:41 +08:00
|
|
|
uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
|
|
|
|
uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
|
|
|
|
size_t EHFrameSize = Sections[EHFrameSID].getSize();
|
[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();
|
|
|
|
}
|
|
|
|
|
2016-12-13 19:39:18 +08:00
|
|
|
std::unique_ptr<RuntimeDyldELF>
|
|
|
|
llvm::RuntimeDyldELF::create(Triple::ArchType Arch,
|
|
|
|
RuntimeDyld::MemoryManager &MemMgr,
|
|
|
|
JITSymbolResolver &Resolver) {
|
|
|
|
switch (Arch) {
|
|
|
|
default:
|
|
|
|
return make_unique<RuntimeDyldELF>(MemMgr, Resolver);
|
|
|
|
case Triple::mips:
|
|
|
|
case Triple::mipsel:
|
|
|
|
case Triple::mips64:
|
|
|
|
case Triple::mips64el:
|
|
|
|
return make_unique<RuntimeDyldELFMips>(MemMgr, Resolver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
|
|
|
|
RuntimeDyldELF::loadObject(const object::ObjectFile &O) {
|
2016-04-28 04:24:48 +08:00
|
|
|
if (auto ObjSectionToIDOrErr = loadObjectImpl(O))
|
|
|
|
return llvm::make_unique<LoadedELFObjectInfo>(*this, *ObjSectionToIDOrErr);
|
|
|
|
else {
|
|
|
|
HasError = true;
|
|
|
|
raw_string_ostream ErrStream(ErrorStr);
|
|
|
|
logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream, "");
|
|
|
|
return nullptr;
|
|
|
|
}
|
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: {
|
2015-11-24 05:47:41 +08:00
|
|
|
support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
|
|
|
|
Value + Addend;
|
2014-03-22 04:28:42 +08:00
|
|
|
DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
|
2015-11-24 05:47:41 +08:00
|
|
|
<< format("%p\n", Section.getAddressWithOffset(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);
|
2015-11-24 05:47:41 +08:00
|
|
|
support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
|
|
|
|
TruncatedAddr;
|
2014-03-22 04:28:42 +08:00
|
|
|
DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
|
2015-11-24 05:47:41 +08:00
|
|
|
<< format("%p\n", Section.getAddressWithOffset(Offset)));
|
2012-01-16 16:56:09 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-11-09 03:34:17 +08:00
|
|
|
case ELF::R_X86_64_PC8: {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
|
2015-11-09 03:34:17 +08:00
|
|
|
int64_t RealOffset = Value + Addend - FinalAddress;
|
|
|
|
assert(isInt<8>(RealOffset));
|
|
|
|
int8_t TruncOffset = (RealOffset & 0xFF);
|
2015-11-24 05:47:41 +08:00
|
|
|
Section.getAddress()[Offset] = TruncOffset;
|
2015-11-09 03:34:17 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-01-16 16:56:09 +08:00
|
|
|
case ELF::R_X86_64_PC32: {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(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);
|
2015-11-24 05:47:41 +08:00
|
|
|
support::ulittle32_t::ref(Section.getAddressWithOffset(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: {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
|
2015-04-14 10:10:35 +08:00
|
|
|
int64_t RealOffset = Value + Addend - FinalAddress;
|
2015-11-24 05:47:41 +08:00
|
|
|
support::ulittle64_t::ref(Section.getAddressWithOffset(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-11-24 05:47:41 +08:00
|
|
|
support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
|
|
|
|
Value + Addend;
|
2012-01-16 16:56:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_386_PC32: {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint32_t FinalAddress =
|
|
|
|
Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
|
2015-05-02 04:21:45 +08:00
|
|
|
uint32_t RealOffset = Value + Addend - FinalAddress;
|
2015-11-24 05:47:41 +08:00
|
|
|
support::ulittle32_t::ref(Section.getAddressWithOffset(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) {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint32_t *TargetPtr =
|
|
|
|
reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
|
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
|
2016-12-16 06:36:53 +08:00
|
|
|
// Data should use target endian. Code should always use little endian.
|
|
|
|
bool isBE = Arch == Triple::aarch64_be;
|
2013-05-05 04:13:59 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
|
2015-11-24 05:47:41 +08:00
|
|
|
<< format("%llx", Section.getAddressWithOffset(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;
|
2016-12-27 21:33:32 +08:00
|
|
|
case ELF::R_AARCH64_ABS64:
|
|
|
|
write(isBE, TargetPtr, Value + Addend);
|
2013-05-05 04:14:14 +08:00
|
|
|
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);
|
2016-12-27 21:33:32 +08:00
|
|
|
write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
|
2013-05-05 04:13:59 +08:00
|
|
|
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));
|
2016-12-27 21:33:32 +08:00
|
|
|
or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
|
2013-05-05 04:14:09 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-12-27 21:33:32 +08:00
|
|
|
case ELF::R_AARCH64_MOVW_UABS_G3:
|
|
|
|
or32le(TargetPtr, ((Value + Addend) & 0xFFFF000000000000) >> 43);
|
2013-05-05 04:14:04 +08:00
|
|
|
break;
|
2016-12-27 21:33:32 +08:00
|
|
|
case ELF::R_AARCH64_MOVW_UABS_G2_NC:
|
|
|
|
or32le(TargetPtr, ((Value + Addend) & 0xFFFF00000000) >> 27);
|
2013-05-05 04:14:04 +08:00
|
|
|
break;
|
2016-12-27 21:33:32 +08:00
|
|
|
case ELF::R_AARCH64_MOVW_UABS_G1_NC:
|
|
|
|
or32le(TargetPtr, ((Value + Addend) & 0xFFFF0000) >> 11);
|
2013-05-05 04:14:04 +08:00
|
|
|
break;
|
2016-12-27 21:33:32 +08:00
|
|
|
case ELF::R_AARCH64_MOVW_UABS_G0_NC:
|
|
|
|
or32le(TargetPtr, ((Value + Addend) & 0xFFFF) << 5);
|
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
|
|
|
|
|
|
|
// Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
|
|
|
|
// from bits 32:12 of X.
|
2016-12-27 21:33:32 +08:00
|
|
|
write32AArch64Addr(TargetPtr, Result >> 12);
|
2014-02-11 20:59:09 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-12-27 21:33:32 +08:00
|
|
|
case ELF::R_AARCH64_ADD_ABS_LO12_NC:
|
2016-12-27 17:51:38 +08:00
|
|
|
// Operation: S + A
|
|
|
|
// Immediate goes in bits 21:10 of LD/ST instruction, taken
|
|
|
|
// from bits 11:0 of X
|
2016-12-27 21:33:32 +08:00
|
|
|
or32AArch64Imm(TargetPtr, Value + Addend);
|
2016-12-27 17:51:38 +08:00
|
|
|
break;
|
2016-12-27 21:33:32 +08:00
|
|
|
case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
|
2014-02-11 20:59:09 +08:00
|
|
|
// Operation: S + A
|
|
|
|
// Immediate goes in bits 21:10 of LD/ST instruction, taken
|
|
|
|
// from bits 11:2 of X
|
2016-12-27 21:33:32 +08:00
|
|
|
or32AArch64Imm(TargetPtr, getBits(Value + Addend, 2, 11));
|
2014-02-11 20:59:09 +08:00
|
|
|
break;
|
2016-12-27 21:33:32 +08:00
|
|
|
case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
|
2014-02-11 20:59:09 +08:00
|
|
|
// Operation: S + A
|
|
|
|
// Immediate goes in bits 21:10 of LD/ST instruction, taken
|
|
|
|
// from bits 11:3 of X
|
2016-12-27 21:33:32 +08:00
|
|
|
or32AArch64Imm(TargetPtr, getBits(Value + Addend, 3, 11));
|
2014-02-11 20:59:09 +08:00
|
|
|
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.
|
2015-11-24 05:47:41 +08:00
|
|
|
uint32_t *TargetPtr =
|
|
|
|
reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
|
|
|
|
uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
|
2012-03-31 00:45:19 +08:00
|
|
|
Value += Addend;
|
|
|
|
|
2012-11-03 03:45:23 +08:00
|
|
|
DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
|
2015-11-24 05:47:41 +08:00
|
|
|
<< Section.getAddressWithOffset(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;
|
2016-10-21 05:15:29 +08:00
|
|
|
// Write a 31bit signed offset
|
2014-01-29 19:50:56 +08:00
|
|
|
case ELF::R_ARM_PREL31:
|
2016-10-21 06:15:56 +08:00
|
|
|
support::ulittle32_t::ref{TargetPtr} =
|
|
|
|
(support::ulittle32_t::ref{TargetPtr} & 0x80000000) |
|
|
|
|
((Value - FinalAddress) & ~0x80000000);
|
2016-10-21 05:15:29 +08:00
|
|
|
break;
|
2013-05-29 03:48:19 +08:00
|
|
|
case ELF::R_ARM_TARGET1:
|
|
|
|
case ELF::R_ARM_ABS32:
|
2016-10-21 06:15:56 +08:00
|
|
|
support::ulittle32_t::ref{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;
|
2016-10-21 06:15:56 +08:00
|
|
|
support::ulittle32_t::ref{TargetPtr} =
|
|
|
|
(support::ulittle32_t::ref{TargetPtr} & ~0x000F0FFF) | (Value & 0xFFF) |
|
|
|
|
(((Value >> 12) & 0xF) << 16);
|
2012-03-31 00:45:19 +08:00
|
|
|
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;
|
2016-10-21 06:15:56 +08:00
|
|
|
assert((support::ulittle32_t::ref{TargetPtr} & 0xFFFFFF) == 0xFFFFFE);
|
|
|
|
support::ulittle32_t::ref{TargetPtr} =
|
|
|
|
(support::ulittle32_t::ref{TargetPtr} & 0xFF000000) | RelValue;
|
2012-03-31 00:45:19 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-01-16 16:56:09 +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;
|
2016-10-20 21:02:23 +08:00
|
|
|
IsMipsN32ABI = false;
|
2015-05-28 21:48:41 +08:00
|
|
|
IsMipsN64ABI = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned AbiVariant;
|
|
|
|
Obj.getPlatformFlags(AbiVariant);
|
|
|
|
IsMipsO32ABI = AbiVariant & ELF::EF_MIPS_ABI_O32;
|
2016-10-20 21:02:23 +08:00
|
|
|
IsMipsN32ABI = AbiVariant & ELF::EF_MIPS_ABI2;
|
2015-05-28 21:48:41 +08:00
|
|
|
IsMipsN64ABI = Obj.getFileFormatName().equals("ELF64-mips");
|
|
|
|
}
|
|
|
|
|
[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.
|
2016-04-28 04:24:48 +08:00
|
|
|
Error RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj,
|
|
|
|
ObjSectionToIDMap &LocalSections,
|
|
|
|
RelocationValueRef &Rel) {
|
[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
|
|
|
// 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.
|
2015-10-07 07:24:35 +08:00
|
|
|
Rel.SymbolName = nullptr;
|
[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.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;
|
2016-04-28 04:24:48 +08:00
|
|
|
if (auto EC = Section.getName(SectionName))
|
|
|
|
return errorCodeToError(EC);
|
[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") {
|
2016-04-28 04:24:48 +08:00
|
|
|
if (auto SectionIDOrErr =
|
|
|
|
findOrEmitSection(Obj, Section, false, LocalSections))
|
|
|
|
Rel.SectionID = *SectionIDOrErr;
|
|
|
|
else
|
|
|
|
return SectionIDOrErr.takeError();
|
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;
|
2016-04-28 04:24:48 +08:00
|
|
|
|
|
|
|
return Error::success();
|
2012-10-25 21:13:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the sections and offset associated with the ODP entry referenced
|
|
|
|
// by Symbol.
|
2016-04-28 04:24:48 +08:00
|
|
|
Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj,
|
|
|
|
ObjSectionToIDMap &LocalSections,
|
|
|
|
RelocationValueRef &Rel) {
|
2012-10-25 21:13:48 +08:00
|
|
|
// 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;
|
2016-04-28 04:24:48 +08:00
|
|
|
if (auto EC = RelSecI->getName(RelSectionName))
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
|
2013-06-04 03:37:34 +08:00
|
|
|
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();
|
2016-04-28 04:24:48 +08:00
|
|
|
int64_t Addend;
|
|
|
|
if (auto AddendOrErr = i->getAddend())
|
|
|
|
Addend = *AddendOrErr;
|
|
|
|
else
|
|
|
|
return errorCodeToError(AddendOrErr.getError());
|
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;
|
|
|
|
|
2016-04-28 04:24:48 +08:00
|
|
|
section_iterator TSI = Obj.section_end();
|
|
|
|
if (auto TSIOrErr = TargetSymbol->getSection())
|
|
|
|
TSI = *TSIOrErr;
|
|
|
|
else
|
2016-05-03 04:28:12 +08:00
|
|
|
return TSIOrErr.takeError();
|
2016-04-28 04:24:48 +08:00
|
|
|
assert(TSI != Obj.section_end() && "TSI should refer to a valid section");
|
|
|
|
|
|
|
|
bool IsCode = TSI->isText();
|
|
|
|
if (auto SectionIDOrErr = findOrEmitSection(Obj, *TSI, IsCode,
|
|
|
|
LocalSections))
|
|
|
|
Rel.SectionID = *SectionIDOrErr;
|
|
|
|
else
|
|
|
|
return SectionIDOrErr.takeError();
|
2013-05-09 11:39:05 +08:00
|
|
|
Rel.Addend = (intptr_t)Addend;
|
2016-04-28 04:24:48 +08:00
|
|
|
return Error::success();
|
2012-10-25 21:13:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-04 23:29:00 +08:00
|
|
|
void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section,
|
|
|
|
uint64_t Offset, uint64_t Value,
|
|
|
|
uint32_t Type, int64_t Addend) {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
|
2015-08-04 23:29:00 +08:00
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Relocation type not implemented yet!");
|
|
|
|
break;
|
|
|
|
case ELF::R_PPC_ADDR16_LO:
|
|
|
|
writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
|
|
|
|
break;
|
|
|
|
case ELF::R_PPC_ADDR16_HI:
|
|
|
|
writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
|
|
|
|
break;
|
|
|
|
case ELF::R_PPC_ADDR16_HA:
|
|
|
|
writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint8_t *LocalAddress = Section.getAddressWithOffset(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: {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
|
2014-06-21 01:51:47 +08:00
|
|
|
uint64_t Delta = Value - FinalAddress + Addend;
|
|
|
|
writeInt16BE(LocalAddress, applyPPClo(Delta));
|
|
|
|
} break;
|
|
|
|
case ELF::R_PPC64_REL16_HI: {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
|
2014-06-21 01:51:47 +08:00
|
|
|
uint64_t Delta = Value - FinalAddress + Addend;
|
|
|
|
writeInt16BE(LocalAddress, applyPPChi(Delta));
|
|
|
|
} break;
|
|
|
|
case ELF::R_PPC64_REL16_HA: {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
|
2014-06-21 01:51:47 +08:00
|
|
|
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: {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
|
2012-10-25 21:13:48 +08:00
|
|
|
int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
|
2015-11-18 04:08:31 +08:00
|
|
|
if (SignExtend32<26>(delta) != delta)
|
2012-10-25 21:13:48 +08:00
|
|
|
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: {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
|
2013-01-10 01:08:15 +08:00
|
|
|
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: {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
|
2013-05-07 01:21:23 +08:00
|
|
|
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) {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
|
2013-05-03 22:15:35 +08:00
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Relocation type not implemented yet!");
|
|
|
|
break;
|
|
|
|
case ELF::R_390_PC16DBL:
|
|
|
|
case ELF::R_390_PLT16DBL: {
|
2015-11-24 05:47:41 +08:00
|
|
|
int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
|
2013-05-03 22:15:35 +08:00
|
|
|
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: {
|
2015-11-24 05:47:41 +08:00
|
|
|
int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
|
2013-05-03 22:15:35 +08:00
|
|
|
assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
|
|
|
|
writeInt32BE(LocalAddress, Delta / 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_390_PC32: {
|
2015-11-24 05:47:41 +08:00
|
|
|
int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
|
2013-05-03 22:15:35 +08:00
|
|
|
assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
|
|
|
|
writeInt32BE(LocalAddress, Delta);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::R_390_64:
|
|
|
|
writeInt64BE(LocalAddress, Value + Addend);
|
|
|
|
break;
|
2016-05-14 01:23:48 +08:00
|
|
|
case ELF::R_390_PC64: {
|
|
|
|
int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
|
|
|
|
writeInt64BE(LocalAddress, Delta);
|
|
|
|
break;
|
|
|
|
}
|
2013-05-03 22:15:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2015-08-04 23:29:00 +08:00
|
|
|
case Triple::ppc:
|
|
|
|
resolvePPC32Relocation(Section, Offset, Value, Type, Addend);
|
|
|
|
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 {
|
2015-11-24 05:47:41 +08:00
|
|
|
return (void *)(Sections[SectionID].getObjAddress() + Offset);
|
2015-05-02 04:21:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-08-13 23:12:49 +08:00
|
|
|
uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType,
|
|
|
|
bool IsLocal) const {
|
|
|
|
switch (RelType) {
|
|
|
|
case ELF::R_MICROMIPS_GOT16:
|
|
|
|
if (IsLocal)
|
|
|
|
return ELF::R_MICROMIPS_LO16;
|
|
|
|
break;
|
|
|
|
case ELF::R_MICROMIPS_HI16:
|
|
|
|
return ELF::R_MICROMIPS_LO16;
|
|
|
|
case ELF::R_MIPS_GOT16:
|
|
|
|
if (IsLocal)
|
|
|
|
return ELF::R_MIPS_LO16;
|
|
|
|
break;
|
|
|
|
case ELF::R_MIPS_HI16:
|
|
|
|
return ELF::R_MIPS_LO16;
|
|
|
|
case ELF::R_MIPS_PCHI16:
|
|
|
|
return ELF::R_MIPS_PCLO16;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ELF::R_MIPS_NONE;
|
|
|
|
}
|
|
|
|
|
2017-01-09 17:56:31 +08:00
|
|
|
// Sometimes we don't need to create thunk for a branch.
|
|
|
|
// This typically happens when branch target is located
|
|
|
|
// in the same object file. In such case target is either
|
|
|
|
// a weak symbol or symbol in a different executable section.
|
|
|
|
// This function checks if branch target is located in the
|
|
|
|
// same object file and if distance between source and target
|
|
|
|
// fits R_AARCH64_CALL26 relocation. If both conditions are
|
|
|
|
// met, it emits direct jump to the target and returns true.
|
|
|
|
// Otherwise false is returned and thunk is created.
|
|
|
|
bool RuntimeDyldELF::resolveAArch64ShortBranch(
|
|
|
|
unsigned SectionID, relocation_iterator RelI,
|
|
|
|
const RelocationValueRef &Value) {
|
|
|
|
uint64_t Address;
|
|
|
|
if (Value.SymbolName) {
|
|
|
|
auto Loc = GlobalSymbolTable.find(Value.SymbolName);
|
|
|
|
|
|
|
|
// Don't create direct branch for external symbols.
|
|
|
|
if (Loc == GlobalSymbolTable.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto &SymInfo = Loc->second;
|
|
|
|
Address = reinterpret_cast<uint64_t>(
|
|
|
|
Sections[SymInfo.getSectionID()].getLoadAddressWithOffset(
|
|
|
|
SymInfo.getOffset()));
|
|
|
|
} else {
|
|
|
|
Address =
|
|
|
|
reinterpret_cast<uint64_t>(Sections[Value.SectionID].getLoadAddress());
|
|
|
|
}
|
|
|
|
uint64_t Offset = RelI->getOffset();
|
|
|
|
uint64_t SourceAddress = Sections[SectionID].getLoadAddressWithOffset(Offset);
|
|
|
|
|
|
|
|
// R_AARCH64_CALL26 requires immediate to be in range -2^27 <= imm < 2^27
|
|
|
|
// If distance between source and target is out of range then we should
|
|
|
|
// create thunk.
|
|
|
|
if (!isInt<28>(Address + Value.Addend - SourceAddress))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
resolveRelocation(Sections[SectionID], Offset, Address, RelI->getType(),
|
|
|
|
Value.Addend);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-28 04:24:48 +08:00
|
|
|
Expected<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;
|
2015-07-03 04:55:21 +08:00
|
|
|
if (Symbol != Obj.symbol_end()) {
|
2016-04-28 04:24:48 +08:00
|
|
|
if (auto TargetNameOrErr = Symbol->getName())
|
|
|
|
TargetName = *TargetNameOrErr;
|
|
|
|
else
|
|
|
|
return TargetNameOrErr.takeError();
|
2015-07-03 04:55:21 +08:00
|
|
|
}
|
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());
|
2016-05-03 04:28:12 +08:00
|
|
|
Expected<SymbolRef::Type> SymTypeOrErr = Symbol->getType();
|
|
|
|
if (!SymTypeOrErr) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
|
|
|
logAllUnhandledErrors(SymTypeOrErr.takeError(), OS, "");
|
|
|
|
OS.flush();
|
|
|
|
report_fatal_error(Buf);
|
|
|
|
}
|
2016-03-24 04:27:00 +08:00
|
|
|
SymType = *SymTypeOrErr;
|
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.
|
2016-05-03 04:28:12 +08:00
|
|
|
auto SectionOrErr = Symbol->getSection();
|
|
|
|
if (!SectionOrErr) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
|
|
|
logAllUnhandledErrors(SectionOrErr.takeError(), OS, "");
|
|
|
|
OS.flush();
|
|
|
|
report_fatal_error(Buf);
|
|
|
|
}
|
|
|
|
section_iterator si = *SectionOrErr;
|
2014-11-27 13:40:13 +08:00
|
|
|
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();
|
2016-04-28 04:24:48 +08:00
|
|
|
if (auto SectionIDOrErr = findOrEmitSection(Obj, (*si), isCode,
|
|
|
|
ObjSectionToID))
|
|
|
|
Value.SectionID = *SectionIDOrErr;
|
|
|
|
else
|
|
|
|
return SectionIDOrErr.takeError();
|
2014-11-27 13:40:13 +08:00
|
|
|
Value.Addend = Addend;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolRef::ST_Data:
|
2016-08-10 03:27:17 +08:00
|
|
|
case SymbolRef::ST_Function:
|
2014-11-27 13:40:13 +08:00
|
|
|
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()) {
|
2015-11-24 05:47:41 +08:00
|
|
|
resolveRelocation(Section, Offset,
|
|
|
|
(uint64_t)Section.getAddressWithOffset(i->second),
|
2014-03-22 04:28:42 +08:00
|
|
|
RelType, 0);
|
2013-05-05 04:14:09 +08:00
|
|
|
DEBUG(dbgs() << " Stub function found\n");
|
2017-01-09 17:56:31 +08:00
|
|
|
} else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) {
|
2013-05-05 04:14:09 +08:00
|
|
|
// Create a new stub function.
|
|
|
|
DEBUG(dbgs() << " Create a new stub function\n");
|
2015-11-24 05:47:41 +08:00
|
|
|
Stubs[Value] = Section.getStubOffset();
|
|
|
|
uint8_t *StubTargetAddr = createStubFunction(
|
|
|
|
Section.getAddressWithOffset(Section.getStubOffset()));
|
2013-05-05 04:14:09 +08:00
|
|
|
|
2015-11-24 05:47:41 +08:00
|
|
|
RelocationEntry REmovz_g3(SectionID,
|
|
|
|
StubTargetAddr - Section.getAddress(),
|
2014-09-07 12:13:13 +08:00
|
|
|
ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
|
2015-11-24 05:47:41 +08:00
|
|
|
RelocationEntry REmovk_g2(SectionID, StubTargetAddr -
|
2015-11-25 04:37:01 +08:00
|
|
|
Section.getAddress() + 4,
|
2014-09-07 12:13:13 +08:00
|
|
|
ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
|
2015-11-24 05:47:41 +08:00
|
|
|
RelocationEntry REmovk_g1(SectionID, StubTargetAddr -
|
2015-11-25 04:37:01 +08:00
|
|
|
Section.getAddress() + 8,
|
2014-09-07 12:13:13 +08:00
|
|
|
ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
|
2015-11-24 05:47:41 +08:00
|
|
|
RelocationEntry REmovk_g0(SectionID, StubTargetAddr -
|
2015-11-25 04:37:01 +08:00
|
|
|
Section.getAddress() + 12,
|
2013-05-05 04:14:09 +08:00
|
|
|
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,
|
2015-11-24 05:47:41 +08:00
|
|
|
reinterpret_cast<uint64_t>(Section.getAddressWithOffset(
|
|
|
|
Section.getStubOffset())),
|
|
|
|
RelType, 0);
|
|
|
|
Section.advanceStubOffset(getMaxStubSize());
|
2013-05-05 04:14:09 +08:00
|
|
|
}
|
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.
|
2016-03-06 04:00:41 +08:00
|
|
|
DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.\n");
|
2015-05-02 04:21:45 +08:00
|
|
|
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()) {
|
2015-11-24 05:47:41 +08:00
|
|
|
resolveRelocation(
|
|
|
|
Section, Offset,
|
|
|
|
reinterpret_cast<uint64_t>(Section.getAddressWithOffset(i->second)),
|
|
|
|
RelType, 0);
|
2015-05-02 04:21:45 +08:00
|
|
|
DEBUG(dbgs() << " Stub function found\n");
|
|
|
|
} else {
|
|
|
|
// Create a new stub function.
|
|
|
|
DEBUG(dbgs() << " Create a new stub function\n");
|
2015-11-24 05:47:41 +08:00
|
|
|
Stubs[Value] = Section.getStubOffset();
|
|
|
|
uint8_t *StubTargetAddr = createStubFunction(
|
|
|
|
Section.getAddressWithOffset(Section.getStubOffset()));
|
|
|
|
RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
|
|
|
|
ELF::R_ARM_ABS32, Value.Addend);
|
2015-05-02 04:21:45 +08:00
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
|
|
|
|
2015-11-24 05:47:41 +08:00
|
|
|
resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
|
|
|
|
Section.getAddressWithOffset(
|
|
|
|
Section.getStubOffset())),
|
|
|
|
RelType, 0);
|
|
|
|
Section.advanceStubOffset(getMaxStubSize());
|
2015-05-02 04:21:45 +08:00
|
|
|
}
|
|
|
|
} 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");
|
2015-11-24 05:47:41 +08:00
|
|
|
Stubs[Value] = Section.getStubOffset();
|
2016-07-15 20:56:37 +08:00
|
|
|
|
|
|
|
unsigned AbiVariant;
|
|
|
|
O.getPlatformFlags(AbiVariant);
|
|
|
|
|
2015-11-24 05:47:41 +08:00
|
|
|
uint8_t *StubTargetAddr = createStubFunction(
|
2016-07-15 20:56:37 +08:00
|
|
|
Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
|
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.
|
2015-11-24 05:47:41 +08:00
|
|
|
RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
|
|
|
|
ELF::R_MIPS_HI16, Value.Addend);
|
|
|
|
RelocationEntry RELo(SectionID,
|
2015-11-25 04:37:01 +08:00
|
|
|
StubTargetAddr - Section.getAddress() + 4,
|
2015-11-24 05:47:41 +08:00
|
|
|
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-11-24 05:47:41 +08:00
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
|
2015-05-02 04:21:45 +08:00
|
|
|
addRelocationForSection(RE, SectionID);
|
2015-11-24 05:47:41 +08:00
|
|
|
Section.advanceStubOffset(getMaxStubSize());
|
2015-05-02 04:21:45 +08:00
|
|
|
}
|
2015-08-13 23:12:49 +08:00
|
|
|
} else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
|
|
|
|
int64_t Addend = (Opcode & 0x0000ffff) << 16;
|
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, Addend);
|
|
|
|
PendingRelocs.push_back(std::make_pair(Value, RE));
|
|
|
|
} else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) {
|
|
|
|
int64_t Addend = Value.Addend + SignExtend32<16>(Opcode & 0x0000ffff);
|
|
|
|
for (auto I = PendingRelocs.begin(); I != PendingRelocs.end();) {
|
|
|
|
const RelocationValueRef &MatchingValue = I->first;
|
|
|
|
RelocationEntry &Reloc = I->second;
|
|
|
|
if (MatchingValue == Value &&
|
|
|
|
RelType == getMatchingLoRelocation(Reloc.RelType) &&
|
|
|
|
SectionID == Reloc.SectionID) {
|
|
|
|
Reloc.Addend += Addend;
|
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(Reloc, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(Reloc, Value.SectionID);
|
|
|
|
I = PendingRelocs.erase(I);
|
|
|
|
} else
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, Addend);
|
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
2015-05-02 04:21:45 +08:00
|
|
|
} else {
|
2015-08-13 23:12:49 +08:00
|
|
|
if (RelType == ELF::R_MIPS_32)
|
2015-06-03 18:27:28 +08:00
|
|
|
Value.Addend += Opcode;
|
2015-07-06 20:50:55 +08:00
|
|
|
else if (RelType == ELF::R_MIPS_PC16)
|
|
|
|
Value.Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2);
|
|
|
|
else if (RelType == ELF::R_MIPS_PC19_S2)
|
|
|
|
Value.Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2);
|
|
|
|
else if (RelType == ELF::R_MIPS_PC21_S2)
|
|
|
|
Value.Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2);
|
|
|
|
else if (RelType == ELF::R_MIPS_PC26_S2)
|
|
|
|
Value.Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2);
|
2015-05-02 04:21:45 +08:00
|
|
|
processSimpleRelocation(SectionID, Offset, RelType, Value);
|
2012-08-18 05:28:04 +08:00
|
|
|
}
|
2016-10-20 21:02:23 +08:00
|
|
|
} else if (IsMipsN32ABI || IsMipsN64ABI) {
|
2015-05-28 21:48:41 +08:00
|
|
|
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];
|
2015-11-24 05:47:41 +08:00
|
|
|
uint8_t *Target = Section.getAddressWithOffset(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.
|
2016-04-28 06:54:03 +08:00
|
|
|
if (auto Err = findOPDEntrySection(Obj, ObjSectionToID, Value))
|
|
|
|
return std::move(Err);
|
2014-07-21 07:53:14 +08:00
|
|
|
} 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);
|
|
|
|
}
|
2015-11-24 05:47:41 +08:00
|
|
|
uint8_t *RelocTarget =
|
|
|
|
Sections[Value.SectionID].getAddressWithOffset(Value.Addend);
|
2012-10-25 21:13:48 +08:00
|
|
|
int32_t delta = static_cast<int32_t>(Target - RelocTarget);
|
2015-11-18 04:08:31 +08:00
|
|
|
// If it is within 26-bits branch range, just set the branch target
|
|
|
|
if (SignExtend32<26>(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,
|
2015-11-24 05:47:41 +08:00
|
|
|
reinterpret_cast<uint64_t>(
|
|
|
|
Section.getAddressWithOffset(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");
|
2015-11-24 05:47:41 +08:00
|
|
|
Stubs[Value] = Section.getStubOffset();
|
|
|
|
uint8_t *StubTargetAddr = createStubFunction(
|
|
|
|
Section.getAddressWithOffset(Section.getStubOffset()),
|
|
|
|
AbiVariant);
|
|
|
|
RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
|
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.
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress();
|
2014-06-21 02:17:56 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-11-24 05:47:41 +08:00
|
|
|
resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
|
|
|
|
Section.getAddressWithOffset(
|
|
|
|
Section.getStubOffset())),
|
2012-11-03 03:45:23 +08:00
|
|
|
RelType, 0);
|
2015-11-24 05:47:41 +08:00
|
|
|
Section.advanceStubOffset(getMaxStubSize());
|
2012-10-25 21:13:48 +08:00
|
|
|
}
|
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
|
2015-08-09 02:27:36 +08:00
|
|
|
// the relocation value (the section of the symbol referred to by the
|
[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
|
|
|
// relocation, and the TOC section associated with the current module).
|
|
|
|
//
|
|
|
|
// Fortunately, these relocations are currently only ever generated
|
2015-08-09 02:27:36 +08:00
|
|
|
// referring to symbols that themselves reside in the TOC, which means
|
[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
|
|
|
// 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;
|
2016-04-28 04:51:58 +08:00
|
|
|
if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue))
|
|
|
|
return std::move(Err);
|
[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 (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;
|
2016-04-28 04:51:58 +08:00
|
|
|
if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
|
|
|
|
return std::move(Err);
|
[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 (TargetName == ".TOC.") {
|
2016-04-28 04:51:58 +08:00
|
|
|
if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
|
|
|
|
return std::move(Err);
|
[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
|
|
|
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()) {
|
2015-11-24 05:47:41 +08:00
|
|
|
StubAddress = uintptr_t(Section.getAddressWithOffset(i->second));
|
2013-05-03 22:15:35 +08:00
|
|
|
DEBUG(dbgs() << " Stub function found\n");
|
|
|
|
} else {
|
|
|
|
// Create a new stub function.
|
|
|
|
DEBUG(dbgs() << " Create a new stub function\n");
|
|
|
|
|
2015-11-24 05:47:41 +08:00
|
|
|
uintptr_t BaseAddress = uintptr_t(Section.getAddress());
|
2013-05-03 22:15:35 +08:00
|
|
|
uintptr_t StubAlignment = getStubAlignment();
|
2015-11-24 05:47:41 +08:00
|
|
|
StubAddress =
|
|
|
|
(BaseAddress + Section.getStubOffset() + 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);
|
2015-11-24 05:47:41 +08:00
|
|
|
Section.advanceStubOffset(getMaxStubSize());
|
2013-05-03 22:15:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
2015-11-24 05:47:41 +08:00
|
|
|
StubAddress = uintptr_t(Section.getAddress()) + i->second;
|
2015-11-14 08:16:15 +08:00
|
|
|
DEBUG(dbgs() << " Stub function found\n");
|
2015-05-02 04:21:45 +08:00
|
|
|
} else {
|
2015-11-14 08:16:15 +08:00
|
|
|
// Create a new stub function (equivalent to a PLT entry).
|
|
|
|
DEBUG(dbgs() << " Create a new stub function\n");
|
2013-08-20 07:27:43 +08:00
|
|
|
|
2015-11-24 05:47:41 +08:00
|
|
|
uintptr_t BaseAddress = uintptr_t(Section.getAddress());
|
2015-11-14 08:16:15 +08:00
|
|
|
uintptr_t StubAlignment = getStubAlignment();
|
2015-11-24 05:47:41 +08:00
|
|
|
StubAddress =
|
|
|
|
(BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
|
|
|
|
-StubAlignment;
|
2015-11-14 08:16:15 +08:00
|
|
|
unsigned StubOffset = StubAddress - BaseAddress;
|
|
|
|
Stubs[Value] = StubOffset;
|
|
|
|
createStubFunction((uint8_t *)StubAddress);
|
2013-08-20 07:27:43 +08:00
|
|
|
|
2015-11-14 08:16:15 +08:00
|
|
|
// Bump our stub offset counter
|
2015-11-24 05:47:41 +08:00
|
|
|
Section.advanceStubOffset(getMaxStubSize());
|
2015-04-14 10:10:35 +08:00
|
|
|
|
2015-11-14 08:16:15 +08:00
|
|
|
// Allocate a GOT Entry
|
|
|
|
uint64_t GOTOffset = allocateGOTEntries(SectionID, 1);
|
2015-04-14 10:10:35 +08:00
|
|
|
|
2015-11-14 08:16:15 +08:00
|
|
|
// The load of the GOT address has an addend of -4
|
|
|
|
resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4);
|
2015-04-14 10:10:35 +08:00
|
|
|
|
2015-11-14 08:16:15 +08:00
|
|
|
// Fill in the value of the symbol we're targeting into the GOT
|
|
|
|
addRelocationForSymbol(
|
|
|
|
computeGOTOffsetRE(SectionID, GOTOffset, 0, ELF::R_X86_64_64),
|
|
|
|
Value.SymbolName);
|
2015-05-02 04:21:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the target call a call into the stub table.
|
|
|
|
resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
|
2015-11-14 08:16:15 +08:00
|
|
|
Addend);
|
2015-05-02 04:21:45 +08:00
|
|
|
} 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
|
|
|
}
|
2016-04-24 09:36:37 +08:00
|
|
|
} else if (RelType == ELF::R_X86_64_GOTPCREL ||
|
|
|
|
RelType == ELF::R_X86_64_GOTPCRELX ||
|
|
|
|
RelType == ELF::R_X86_64_REX_GOTPCRELX) {
|
2015-05-02 04:21:45 +08:00
|
|
|
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:
|
2016-10-20 21:02:23 +08:00
|
|
|
if (IsMipsO32ABI || IsMipsN32ABI)
|
2015-05-28 21:48:41 +08:00
|
|
|
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
|
2015-11-24 05:47:46 +08:00
|
|
|
Sections.push_back(SectionEntry(".got", nullptr, 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
|
|
|
}
|
|
|
|
|
2016-04-28 04:24:48 +08:00
|
|
|
Error RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj,
|
2014-05-13 05:39:59 +08:00
|
|
|
ObjSectionToIDMap &SectionMap) {
|
2015-08-13 23:12:49 +08:00
|
|
|
if (IsMipsO32ABI)
|
|
|
|
if (!PendingRelocs.empty())
|
2016-04-28 04:24:48 +08:00
|
|
|
return make_error<RuntimeDyldError>("Can't find matching LO16 reloc");
|
2015-08-13 23:12:49 +08:00
|
|
|
|
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)
|
2016-04-28 04:24:48 +08:00
|
|
|
return make_error<RuntimeDyldError>("Unable to allocate memory for GOT!");
|
[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
|
|
|
|
2015-11-24 05:47:46 +08:00
|
|
|
Sections[GOTSectionID] =
|
|
|
|
SectionEntry(".got", Addr, TotalSize, TotalSize, 0);
|
2015-04-14 10:10:35 +08:00
|
|
|
|
|
|
|
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);
|
2016-10-20 21:02:23 +08:00
|
|
|
if (IsMipsN32ABI || IsMipsN64ABI) {
|
2015-05-28 21:48:41 +08:00
|
|
|
// 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;
|
2016-04-28 04:24:48 +08:00
|
|
|
|
|
|
|
return Error::success();
|
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
|
|
|
}
|
|
|
|
|
2015-11-24 05:47:51 +08:00
|
|
|
bool RuntimeDyldELF::relocationNeedsStub(const RelocationRef &R) const {
|
|
|
|
if (Arch != Triple::x86_64)
|
|
|
|
return true; // Conservative answer
|
|
|
|
|
|
|
|
switch (R.getType()) {
|
|
|
|
default:
|
|
|
|
return true; // Conservative answer
|
|
|
|
|
|
|
|
|
|
|
|
case ELF::R_X86_64_GOTPCREL:
|
2016-04-24 09:36:37 +08:00
|
|
|
case ELF::R_X86_64_GOTPCRELX:
|
|
|
|
case ELF::R_X86_64_REX_GOTPCRELX:
|
2015-11-24 05:47:51 +08:00
|
|
|
case ELF::R_X86_64_PC32:
|
|
|
|
case ELF::R_X86_64_PC64:
|
|
|
|
case ELF::R_X86_64_64:
|
|
|
|
// We know that these reloation types won't need a stub function. This list
|
|
|
|
// can be extended as needed.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-16 16:56:09 +08:00
|
|
|
} // namespace llvm
|