1 //===-- RuntimeDyldELF.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // ELF support for MC-JIT runtime dynamic linker.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
14 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
16 #include "RuntimeDyldImpl.h"
17 #include "llvm/ADT/DenseMap.h"
23 class ELFObjectFileBase
;
26 class RuntimeDyldELF
: public RuntimeDyldImpl
{
28 void resolveRelocation(const SectionEntry
&Section
, uint64_t Offset
,
29 uint64_t Value
, uint32_t Type
, int64_t Addend
,
30 uint64_t SymOffset
= 0, SID SectionID
= 0);
32 void resolveX86_64Relocation(const SectionEntry
&Section
, uint64_t Offset
,
33 uint64_t Value
, uint32_t Type
, int64_t Addend
,
36 void resolveX86Relocation(const SectionEntry
&Section
, uint64_t Offset
,
37 uint32_t Value
, uint32_t Type
, int32_t Addend
);
39 void resolveAArch64Relocation(const SectionEntry
&Section
, uint64_t Offset
,
40 uint64_t Value
, uint32_t Type
, int64_t Addend
);
42 bool resolveAArch64ShortBranch(unsigned SectionID
, relocation_iterator RelI
,
43 const RelocationValueRef
&Value
);
45 void resolveAArch64Branch(unsigned SectionID
, const RelocationValueRef
&Value
,
46 relocation_iterator RelI
, StubMap
&Stubs
);
48 void resolveARMRelocation(const SectionEntry
&Section
, uint64_t Offset
,
49 uint32_t Value
, uint32_t Type
, int32_t Addend
);
51 void resolvePPC32Relocation(const SectionEntry
&Section
, uint64_t Offset
,
52 uint64_t Value
, uint32_t Type
, int64_t Addend
);
54 void resolvePPC64Relocation(const SectionEntry
&Section
, uint64_t Offset
,
55 uint64_t Value
, uint32_t Type
, int64_t Addend
);
57 void resolveSystemZRelocation(const SectionEntry
&Section
, uint64_t Offset
,
58 uint64_t Value
, uint32_t Type
, int64_t Addend
);
60 void resolveBPFRelocation(const SectionEntry
&Section
, uint64_t Offset
,
61 uint64_t Value
, uint32_t Type
, int64_t Addend
);
63 unsigned getMaxStubSize() const override
{
64 if (Arch
== Triple::aarch64
|| Arch
== Triple::aarch64_be
)
65 return 20; // movz; movk; movk; movk; br
66 if (Arch
== Triple::arm
|| Arch
== Triple::thumb
)
67 return 8; // 32-bit instruction and 32-bit address
68 else if (IsMipsO32ABI
|| IsMipsN32ABI
)
70 else if (IsMipsN64ABI
)
72 else if (Arch
== Triple::ppc64
|| Arch
== Triple::ppc64le
)
74 else if (Arch
== Triple::x86_64
)
75 return 6; // 2-byte jmp instruction + 32-bit relative address
76 else if (Arch
== Triple::systemz
)
82 unsigned getStubAlignment() override
{
83 if (Arch
== Triple::systemz
)
89 void setMipsABI(const ObjectFile
&Obj
) override
;
91 Error
findPPC64TOCSection(const ELFObjectFileBase
&Obj
,
92 ObjSectionToIDMap
&LocalSections
,
93 RelocationValueRef
&Rel
);
94 Error
findOPDEntrySection(const ELFObjectFileBase
&Obj
,
95 ObjSectionToIDMap
&LocalSections
,
96 RelocationValueRef
&Rel
);
98 size_t getGOTEntrySize() override
;
101 SectionEntry
&getSection(unsigned SectionID
) { return Sections
[SectionID
]; }
103 // Allocate no GOT entries for use in the given section.
104 uint64_t allocateGOTEntries(unsigned no
);
106 // Find GOT entry corresponding to relocation or create new one.
107 uint64_t findOrAllocGOTEntry(const RelocationValueRef
&Value
,
108 unsigned GOTRelType
);
110 // Resolve the relvative address of GOTOffset in Section ID and place
111 // it at the given Offset
112 void resolveGOTOffsetRelocation(unsigned SectionID
, uint64_t Offset
,
113 uint64_t GOTOffset
, uint32_t Type
);
115 // For a GOT entry referenced from SectionID, compute a relocation entry
116 // that will place the final resolved value in the GOT slot
117 RelocationEntry
computeGOTOffsetRE(uint64_t GOTOffset
, uint64_t SymbolOffset
,
120 // Compute the address in memory where we can find the placeholder
121 void *computePlaceholderAddress(unsigned SectionID
, uint64_t Offset
) const;
123 // Split out common case for createing the RelocationEntry for when the relocation requires
124 // no particular advanced processing.
125 void processSimpleRelocation(unsigned SectionID
, uint64_t Offset
, unsigned RelType
, RelocationValueRef Value
);
127 // Return matching *LO16 relocation (Mips specific)
128 uint32_t getMatchingLoRelocation(uint32_t RelType
,
129 bool IsLocal
= false) const;
131 // The tentative ID for the GOT section
132 unsigned GOTSectionID
;
134 // Records the current number of allocated slots in the GOT
135 // (This would be equivalent to GOTEntries.size() were it not for relocations
136 // that consume more than one slot)
137 unsigned CurrentGOTIndex
;
140 // A map from section to a GOT section that has entries for section's GOT
141 // relocations. (Mips64 specific)
142 DenseMap
<SID
, SID
> SectionToGOTMap
;
145 // A map to avoid duplicate got entries (Mips64 specific)
146 StringMap
<uint64_t> GOTSymbolOffsets
;
148 // *HI16 relocations will be added for resolving when we find matching
149 // *LO16 part. (Mips specific)
150 SmallVector
<std::pair
<RelocationValueRef
, RelocationEntry
>, 8> PendingRelocs
;
152 // When a module is loaded we save the SectionID of the EH frame section
153 // in a table until we receive a request to register all unregistered
154 // EH frame sections with the memory manager.
155 SmallVector
<SID
, 2> UnregisteredEHFrameSections
;
157 // Map between GOT relocation value and corresponding GOT offset
158 std::map
<RelocationValueRef
, uint64_t> GOTOffsetMap
;
160 bool relocationNeedsGot(const RelocationRef
&R
) const override
;
161 bool relocationNeedsStub(const RelocationRef
&R
) const override
;
164 RuntimeDyldELF(RuntimeDyld::MemoryManager
&MemMgr
,
165 JITSymbolResolver
&Resolver
);
166 ~RuntimeDyldELF() override
;
168 static std::unique_ptr
<RuntimeDyldELF
>
169 create(Triple::ArchType Arch
, RuntimeDyld::MemoryManager
&MemMgr
,
170 JITSymbolResolver
&Resolver
);
172 std::unique_ptr
<RuntimeDyld::LoadedObjectInfo
>
173 loadObject(const object::ObjectFile
&O
) override
;
175 void resolveRelocation(const RelocationEntry
&RE
, uint64_t Value
) override
;
176 Expected
<relocation_iterator
>
177 processRelocationRef(unsigned SectionID
, relocation_iterator RelI
,
178 const ObjectFile
&Obj
,
179 ObjSectionToIDMap
&ObjSectionToID
,
180 StubMap
&Stubs
) override
;
181 bool isCompatibleFile(const object::ObjectFile
&Obj
) const override
;
182 void registerEHFrames() override
;
183 Error
finalizeLoad(const ObjectFile
&Obj
,
184 ObjSectionToIDMap
&SectionMap
) override
;
187 } // end namespace llvm