Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h
35267 views
//===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-=//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// MachO support for MC-JIT runtime dynamic linker.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H13#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H1415#include "RuntimeDyldImpl.h"16#include "llvm/Object/MachO.h"17#include "llvm/Support/Format.h"1819#define DEBUG_TYPE "dyld"2021using namespace llvm;22using namespace llvm::object;2324namespace llvm {25class RuntimeDyldMachO : public RuntimeDyldImpl {26protected:27struct SectionOffsetPair {28unsigned SectionID;29uint64_t Offset;30};3132struct EHFrameRelatedSections {33EHFrameRelatedSections()34: EHFrameSID(RTDYLD_INVALID_SECTION_ID),35TextSID(RTDYLD_INVALID_SECTION_ID),36ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}3738EHFrameRelatedSections(SID EH, SID T, SID Ex)39: EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}40SID EHFrameSID;41SID TextSID;42SID ExceptTabSID;43};4445// When a module is loaded we save the SectionID of the EH frame section46// in a table until we receive a request to register all unregistered47// EH frame sections with the memory manager.48SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;4950RuntimeDyldMachO(RuntimeDyld::MemoryManager &MemMgr,51JITSymbolResolver &Resolver)52: RuntimeDyldImpl(MemMgr, Resolver) {}5354/// This convenience method uses memcpy to extract a contiguous addend (the55/// addend size and offset are taken from the corresponding fields of the RE).56int64_t memcpyAddend(const RelocationEntry &RE) const;5758/// Given a relocation_iterator for a non-scattered relocation, construct a59/// RelocationEntry and fill in the common fields. The 'Addend' field is *not*60/// filled in, since immediate encodings are highly target/opcode specific.61/// For targets/opcodes with simple, contiguous immediates (e.g. X86) the62/// memcpyAddend method can be used to read the immediate.63RelocationEntry getRelocationEntry(unsigned SectionID,64const ObjectFile &BaseTObj,65const relocation_iterator &RI) const {66const MachOObjectFile &Obj =67static_cast<const MachOObjectFile &>(BaseTObj);68MachO::any_relocation_info RelInfo =69Obj.getRelocation(RI->getRawDataRefImpl());7071bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);72unsigned Size = Obj.getAnyRelocationLength(RelInfo);73uint64_t Offset = RI->getOffset();74MachO::RelocationInfoType RelType =75static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RelInfo));7677return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size);78}7980/// Process a scattered vanilla relocation.81Expected<relocation_iterator>82processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI,83const ObjectFile &BaseObjT,84RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID,85bool TargetIsLocalThumbFunc = false);8687/// Construct a RelocationValueRef representing the relocation target.88/// For Symbols in known sections, this will return a RelocationValueRef89/// representing a (SectionID, Offset) pair.90/// For Symbols whose section is not known, this will return a91/// (SymbolName, Offset) pair, where the Offset is taken from the instruction92/// immediate (held in RE.Addend).93/// In both cases the Addend field is *NOT* fixed up to be PC-relative. That94/// should be done by the caller where appropriate by calling makePCRel on95/// the RelocationValueRef.96Expected<RelocationValueRef>97getRelocationValueRef(const ObjectFile &BaseTObj,98const relocation_iterator &RI,99const RelocationEntry &RE,100ObjSectionToIDMap &ObjSectionToID);101102/// Make the RelocationValueRef addend PC-relative.103void makeValueAddendPCRel(RelocationValueRef &Value,104const relocation_iterator &RI,105unsigned OffsetToNextPC);106107/// Dump information about the relocation entry (RE) and resolved value.108void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;109110// Return a section iterator for the section containing the given address.111static section_iterator getSectionByAddress(const MachOObjectFile &Obj,112uint64_t Addr);113114115// Populate __pointers section.116Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj,117const SectionRef &PTSection,118unsigned PTSectionID);119120public:121122/// Create a RuntimeDyldMachO instance for the given target architecture.123static std::unique_ptr<RuntimeDyldMachO>124create(Triple::ArchType Arch,125RuntimeDyld::MemoryManager &MemMgr,126JITSymbolResolver &Resolver);127128std::unique_ptr<RuntimeDyld::LoadedObjectInfo>129loadObject(const object::ObjectFile &O) override;130131SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }132133bool isCompatibleFile(const object::ObjectFile &Obj) const override;134};135136/// RuntimeDyldMachOTarget - Templated base class for generic MachO linker137/// algorithms and data structures.138///139/// Concrete, target specific sub-classes can be accessed via the impl()140/// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously141/// Recurring Template Idiom). Concrete subclasses for each target142/// can be found in ./Targets.143template <typename Impl>144class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO {145private:146Impl &impl() { return static_cast<Impl &>(*this); }147const Impl &impl() const { return static_cast<const Impl &>(*this); }148149unsigned char *processFDE(uint8_t *P, int64_t DeltaForText,150int64_t DeltaForEH);151152public:153RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr,154JITSymbolResolver &Resolver)155: RuntimeDyldMachO(MemMgr, Resolver) {}156157Error finalizeLoad(const ObjectFile &Obj,158ObjSectionToIDMap &SectionMap) override;159void registerEHFrames() override;160};161162} // end namespace llvm163164#undef DEBUG_TYPE165166#endif167168169