1 //===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // MachO support for MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_RUNTIME_DYLD_MACHO_H
15 #define LLVM_RUNTIME_DYLD_MACHO_H
17 #include "ObjectImageCommon.h"
18 #include "RuntimeDyldImpl.h"
19 #include "llvm/ADT/IndexedMap.h"
20 #include "llvm/Object/MachO.h"
21 #include "llvm/Support/Format.h"
24 using namespace llvm::object;
27 class RuntimeDyldMachO : public RuntimeDyldImpl {
30 /// Write the least significant 'Size' bytes in 'Value' out at the address
31 /// pointed to by Addr. Check for overflow.
32 bool applyRelocationValue(uint8_t *Addr, uint64_t Value, unsigned Size) {
33 for (unsigned i = 0; i < Size; ++i) {
34 *Addr++ = (uint8_t)Value;
38 if (Value) // Catch overflow
39 return Error("Relocation out of range.");
44 bool resolveI386Relocation(const RelocationEntry &RE, uint64_t Value);
45 bool resolveX86_64Relocation(const RelocationEntry &RE, uint64_t Value);
46 bool resolveARMRelocation(const RelocationEntry &RE, uint64_t Value);
47 bool resolveARM64Relocation(const RelocationEntry &RE, uint64_t Value);
49 // Populate stubs in __jump_table section.
50 void populateJumpTable(MachOObjectFile &Obj, const SectionRef &JTSection,
51 unsigned JTSectionID);
53 // Populate __pointers section.
54 void populatePointersSection(MachOObjectFile &Obj, const SectionRef &PTSection,
55 unsigned PTSectionID);
57 unsigned getMaxStubSize() override {
58 if (Arch == Triple::arm || Arch == Triple::thumb)
59 return 8; // 32-bit instruction and 32-bit address
60 else if (Arch == Triple::x86_64)
61 return 8; // GOT entry
66 unsigned getStubAlignment() override { return 1; }
68 relocation_iterator processSECTDIFFRelocation(
70 relocation_iterator RelI,
72 ObjSectionToIDMap &ObjSectionToID);
74 struct EHFrameRelatedSections {
75 EHFrameRelatedSections()
76 : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
77 TextSID(RTDYLD_INVALID_SECTION_ID),
78 ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
79 EHFrameRelatedSections(SID EH, SID T, SID Ex)
80 : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
86 // When a module is loaded we save the SectionID of the EH frame section
87 // in a table until we receive a request to register all unregistered
88 // EH frame sections with the memory manager.
89 SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;
92 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
94 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
96 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
97 ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID,
98 const SymbolTableMap &Symbols, StubMap &Stubs) override;
99 bool isCompatibleFormat(const ObjectBuffer *Buffer) const override;
100 bool isCompatibleFile(const object::ObjectFile *Obj) const override;
101 void registerEHFrames() override;
102 void finalizeLoad(ObjectImage &ObjImg,
103 ObjSectionToIDMap &SectionMap) override;
105 static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) {
106 return new ObjectImageCommon(InputBuffer);
110 createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) {
111 return new ObjectImageCommon(std::move(InputObject));
115 } // end namespace llvm