2012-01-22 07:05:02 +00:00
|
|
|
//===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// MachO support for MC-JIT runtime dynamic linker.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
|
|
|
|
#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
|
2012-01-22 07:05:02 +00:00
|
|
|
|
2014-03-08 18:45:12 +00:00
|
|
|
#include "ObjectImageCommon.h"
|
2012-12-04 07:12:27 +00:00
|
|
|
#include "RuntimeDyldImpl.h"
|
2013-04-26 20:07:33 +00:00
|
|
|
#include "llvm/Object/MachO.h"
|
2012-01-22 07:05:02 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
|
2014-07-17 18:54:50 +00:00
|
|
|
#define DEBUG_TYPE "dyld"
|
|
|
|
|
2012-01-22 07:05:02 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class RuntimeDyldMachO : public RuntimeDyldImpl {
|
2014-07-17 18:54:50 +00:00
|
|
|
protected:
|
|
|
|
struct SectionOffsetPair {
|
|
|
|
unsigned SectionID;
|
|
|
|
uint64_t Offset;
|
|
|
|
};
|
2014-05-22 22:30:13 +00:00
|
|
|
|
2013-10-11 21:25:48 +00:00
|
|
|
struct EHFrameRelatedSections {
|
2014-03-21 20:28:42 +00:00
|
|
|
EHFrameRelatedSections()
|
|
|
|
: EHFrameSID(RTDYLD_INVALID_SECTION_ID),
|
|
|
|
TextSID(RTDYLD_INVALID_SECTION_ID),
|
|
|
|
ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
|
[MCJIT] Refactor and add stub inspection to the RuntimeDyldChecker framework.
This patch introduces a 'stub_addr' builtin that can be used to find the address
of the stub for a given (<file>, <section>, <symbol>) tuple. This address can be
used both to verify the contents of stubs (by loading from the returned address)
and to verify references to stubs (by comparing against the returned address).
Example (1) - Verifying stub contents:
Load 8 bytes (assuming a 64-bit target) from the stub for 'x' in the __text
section of f.o, and compare that value against the addres of 'x'.
# rtdyld-check: *{8}(stub_addr(f.o, __text, x) = x
Example (2) - Verifying references to stubs:
Decode the immediate of the instruction at label 'l', and verify that it's
equal to the offset from the next instruction's PC to the stub for 'y' in the
__text section of f.o (i.e. it's the correct PC-rel difference).
# rtdyld-check: decode_operand(l, 4) = stub_addr(f.o, __text, y) - next_pc(l)
l:
movq y@GOTPCREL(%rip), %rax
Since stub inspection requires cooperation with RuntimeDyldImpl this patch
pimpl-ifies RuntimeDyldChecker. Its implementation is moved in to a new class,
RuntimeDyldCheckerImpl, that has access to the definition of RuntimeDyldImpl.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213698 91177308-0d34-0410-b5e6-96231b3b80d8
2014-07-22 22:47:39 +00:00
|
|
|
|
2013-12-07 11:21:42 +00:00
|
|
|
EHFrameRelatedSections(SID EH, SID T, SID Ex)
|
2014-03-21 20:28:42 +00:00
|
|
|
: EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
|
2013-10-11 21:25:48 +00:00
|
|
|
SID EHFrameSID;
|
|
|
|
SID TextSID;
|
|
|
|
SID ExceptTabSID;
|
|
|
|
};
|
|
|
|
|
|
|
|
// When a module is loaded we save the SectionID of the EH frame section
|
|
|
|
// in a table until we receive a request to register all unregistered
|
|
|
|
// EH frame sections with the memory manager.
|
|
|
|
SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;
|
2014-03-21 20:28:42 +00:00
|
|
|
|
2012-01-22 07:05:02 +00:00
|
|
|
RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
|
|
|
|
|
2014-08-08 23:12:22 +00:00
|
|
|
/// This convenience method uses memcpy to extract a contiguous addend (the
|
|
|
|
/// addend size and offset are taken from the corresponding fields of the RE).
|
|
|
|
int64_t memcpyAddend(const RelocationEntry &RE) const;
|
|
|
|
|
|
|
|
/// Given a relocation_iterator for a non-scattered relocation, construct a
|
|
|
|
/// RelocationEntry and fill in the common fields. The 'Addend' field is *not*
|
|
|
|
/// filled in, since immediate encodings are highly target/opcode specific.
|
|
|
|
/// For targets/opcodes with simple, contiguous immediates (e.g. X86) the
|
|
|
|
/// memcpyAddend method can be used to read the immediate.
|
|
|
|
RelocationEntry getRelocationEntry(unsigned SectionID, ObjectImage &ObjImg,
|
|
|
|
const relocation_iterator &RI) const {
|
|
|
|
const MachOObjectFile &Obj =
|
|
|
|
static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
|
|
|
|
MachO::any_relocation_info RelInfo =
|
|
|
|
Obj.getRelocation(RI->getRawDataRefImpl());
|
|
|
|
|
|
|
|
bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
|
|
|
|
unsigned Size = Obj.getAnyRelocationLength(RelInfo);
|
|
|
|
uint64_t Offset;
|
|
|
|
RI->getOffset(Offset);
|
|
|
|
MachO::RelocationInfoType RelType =
|
|
|
|
static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RelInfo));
|
|
|
|
|
|
|
|
return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size);
|
|
|
|
}
|
2014-07-17 18:54:50 +00:00
|
|
|
|
|
|
|
/// Construct a RelocationValueRef representing the relocation target.
|
|
|
|
/// For Symbols in known sections, this will return a RelocationValueRef
|
|
|
|
/// representing a (SectionID, Offset) pair.
|
|
|
|
/// For Symbols whose section is not known, this will return a
|
|
|
|
/// (SymbolName, Offset) pair, where the Offset is taken from the instruction
|
|
|
|
/// immediate (held in RE.Addend).
|
|
|
|
/// In both cases the Addend field is *NOT* fixed up to be PC-relative. That
|
|
|
|
/// should be done by the caller where appropriate by calling makePCRel on
|
|
|
|
/// the RelocationValueRef.
|
|
|
|
RelocationValueRef getRelocationValueRef(ObjectImage &ObjImg,
|
|
|
|
const relocation_iterator &RI,
|
|
|
|
const RelocationEntry &RE,
|
|
|
|
ObjSectionToIDMap &ObjSectionToID,
|
|
|
|
const SymbolTableMap &Symbols);
|
|
|
|
|
|
|
|
/// Make the RelocationValueRef addend PC-relative.
|
|
|
|
void makeValueAddendPCRel(RelocationValueRef &Value, ObjectImage &ObjImg,
|
2014-07-30 03:35:05 +00:00
|
|
|
const relocation_iterator &RI,
|
|
|
|
unsigned OffsetToNextPC);
|
2014-07-17 18:54:50 +00:00
|
|
|
|
|
|
|
/// Dump information about the relocation entry (RE) and resolved value.
|
|
|
|
void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;
|
2014-03-08 18:45:12 +00:00
|
|
|
|
2014-09-11 19:21:14 +00:00
|
|
|
// Return a section iterator for the section containing the given address.
|
|
|
|
static section_iterator getSectionByAddress(const MachOObjectFile &Obj,
|
|
|
|
uint64_t Addr);
|
|
|
|
|
|
|
|
|
|
|
|
// Populate __pointers section.
|
|
|
|
void populateIndirectSymbolPointersSection(MachOObjectFile &Obj,
|
|
|
|
const SectionRef &PTSection,
|
|
|
|
unsigned PTSectionID);
|
|
|
|
|
2014-07-17 18:54:50 +00:00
|
|
|
public:
|
|
|
|
/// Create an ObjectImage from the given ObjectBuffer.
|
2014-09-03 19:48:09 +00:00
|
|
|
static std::unique_ptr<ObjectImage>
|
|
|
|
createObjectImage(std::unique_ptr<ObjectBuffer> InputBuffer) {
|
|
|
|
return llvm::make_unique<ObjectImageCommon>(std::move(InputBuffer));
|
2014-07-15 19:35:22 +00:00
|
|
|
}
|
|
|
|
|
2014-07-17 18:54:50 +00:00
|
|
|
/// Create an ObjectImage from the given ObjectFile.
|
2014-03-21 20:28:42 +00:00
|
|
|
static ObjectImage *
|
2014-07-15 19:35:22 +00:00
|
|
|
createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) {
|
|
|
|
return new ObjectImageCommon(std::move(InputObject));
|
|
|
|
}
|
2014-07-17 18:54:50 +00:00
|
|
|
|
|
|
|
/// Create a RuntimeDyldMachO instance for the given target architecture.
|
|
|
|
static std::unique_ptr<RuntimeDyldMachO> create(Triple::ArchType Arch,
|
|
|
|
RTDyldMemoryManager *mm);
|
|
|
|
|
|
|
|
SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
|
|
|
|
|
|
|
|
bool isCompatibleFormat(const ObjectBuffer *Buffer) const override;
|
|
|
|
bool isCompatibleFile(const object::ObjectFile *Obj) const override;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// RuntimeDyldMachOTarget - Templated base class for generic MachO linker
|
|
|
|
/// algorithms and data structures.
|
|
|
|
///
|
|
|
|
/// Concrete, target specific sub-classes can be accessed via the impl()
|
|
|
|
/// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously
|
|
|
|
/// Recurring Template Idiom). Concrete subclasses for each target
|
|
|
|
/// can be found in ./Targets.
|
|
|
|
template <typename Impl>
|
|
|
|
class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO {
|
|
|
|
private:
|
|
|
|
Impl &impl() { return static_cast<Impl &>(*this); }
|
2014-07-19 00:19:17 +00:00
|
|
|
const Impl &impl() const { return static_cast<const Impl &>(*this); }
|
|
|
|
|
2014-09-04 04:53:03 +00:00
|
|
|
unsigned char *processFDE(unsigned char *P, int64_t DeltaForText,
|
|
|
|
int64_t DeltaForEH);
|
|
|
|
|
2014-07-17 18:54:50 +00:00
|
|
|
public:
|
|
|
|
RuntimeDyldMachOCRTPBase(RTDyldMemoryManager *mm) : RuntimeDyldMachO(mm) {}
|
|
|
|
|
2014-09-03 11:41:21 +00:00
|
|
|
void finalizeLoad(ObjectImage &ObjImg,
|
2014-09-04 04:53:03 +00:00
|
|
|
ObjSectionToIDMap &SectionMap) override;
|
|
|
|
void registerEHFrames() override;
|
2012-01-22 07:05:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2014-07-17 18:54:50 +00:00
|
|
|
#undef DEBUG_TYPE
|
|
|
|
|
2012-01-22 07:05:02 +00:00
|
|
|
#endif
|