LLVM 19.0.0git
RuntimeDyldMachOX86_64.h
Go to the documentation of this file.
1//===-- RuntimeDyldMachOX86_64.h ---- MachO/X86_64 specific code. -*- C++ -*-=//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H
10#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H
11
12#include "../RuntimeDyldMachO.h"
13
14#define DEBUG_TYPE "dyld"
15
16namespace llvm {
17
19 : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOX86_64> {
20public:
21
23
27
28 unsigned getMaxStubSize() const override { return 8; }
29
30 Align getStubAlignment() override { return Align(8); }
31
33 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
34 const ObjectFile &BaseObjT,
35 ObjSectionToIDMap &ObjSectionToID,
36 StubMap &Stubs) override {
37 const MachOObjectFile &Obj =
38 static_cast<const MachOObjectFile &>(BaseObjT);
40 Obj.getRelocation(RelI->getRawDataRefImpl());
41 uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
42
43 if (RelType == MachO::X86_64_RELOC_SUBTRACTOR)
44 return processSubtractRelocation(SectionID, RelI, Obj, ObjSectionToID);
45
46 assert(!Obj.isRelocationScattered(RelInfo) &&
47 "Scattered relocations not supported on X86_64");
48
49 RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
50 RE.Addend = memcpyAddend(RE);
52 if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID))
53 Value = *ValueOrErr;
54 else
55 return ValueOrErr.takeError();
56
57 bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
58 if (!IsExtern && RE.IsPCRel)
59 makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
60
61 switch (RelType) {
63 default:
64 if (RelType > MachO::X86_64_RELOC_TLV)
65 return make_error<RuntimeDyldError>(("MachO X86_64 relocation type " +
66 Twine(RelType) +
67 " is out of range").str());
68 break;
69 }
70
73 processGOTRelocation(RE, Value, Stubs);
74 else {
75 RE.Addend = Value.Offset;
76 if (Value.SymbolName)
77 addRelocationForSymbol(RE, Value.SymbolName);
78 else
79 addRelocationForSection(RE, Value.SectionID);
80 }
81
82 return ++RelI;
83 }
84
85 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
87 const SectionEntry &Section = Sections[RE.SectionID];
88 uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
89
90 // If the relocation is PC-relative, the value to be encoded is the
91 // pointer difference.
92 if (RE.IsPCRel) {
93 // FIXME: It seems this value needs to be adjusted by 4 for an effective
94 // PC address. Is that expected? Only for branches, perhaps?
95 uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
96 Value -= FinalAddress + 4;
97 }
98
99 switch (RE.RelType) {
100 default:
101 llvm_unreachable("Invalid relocation type!");
108 writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
109 break;
111 uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
112 uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
113 assert((Value == SectionABase || Value == SectionBBase) &&
114 "Unexpected SUBTRACTOR relocation value.");
115 Value = SectionABase - SectionBBase + RE.Addend;
116 writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
117 break;
118 }
119 }
120 }
121
122 Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,
123 const SectionRef &Section) {
124 return Error::success();
125 }
126
127private:
128 void processGOTRelocation(const RelocationEntry &RE,
130 SectionEntry &Section = Sections[RE.SectionID];
131 assert(RE.IsPCRel);
132 assert(RE.Size == 2);
133 Value.Offset -= RE.Addend;
134 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
135 uint8_t *Addr;
136 if (i != Stubs.end()) {
137 Addr = Section.getAddressWithOffset(i->second);
138 } else {
139 Stubs[Value] = Section.getStubOffset();
140 uint8_t *GOTEntry = Section.getAddressWithOffset(Section.getStubOffset());
141 RelocationEntry GOTRE(RE.SectionID, Section.getStubOffset(),
142 MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false,
143 3);
144 if (Value.SymbolName)
145 addRelocationForSymbol(GOTRE, Value.SymbolName);
146 else
147 addRelocationForSection(GOTRE, Value.SectionID);
148 Section.advanceStubOffset(8);
149 Addr = GOTEntry;
150 }
151 RelocationEntry TargetRE(RE.SectionID, RE.Offset,
153 resolveRelocation(TargetRE, (uint64_t)Addr);
154 }
155
157 processSubtractRelocation(unsigned SectionID, relocation_iterator RelI,
158 const MachOObjectFile &BaseObj,
159 ObjSectionToIDMap &ObjSectionToID) {
160 const MachOObjectFile &Obj =
161 static_cast<const MachOObjectFile&>(BaseObj);
163 Obj.getRelocation(RelI->getRawDataRefImpl());
164
165 unsigned Size = Obj.getAnyRelocationLength(RE);
166 uint64_t Offset = RelI->getOffset();
167 uint8_t *LocalAddress = Sections[SectionID].getAddressWithOffset(Offset);
168 unsigned NumBytes = 1 << Size;
169 int64_t Addend =
170 SignExtend64(readBytesUnaligned(LocalAddress, NumBytes), NumBytes * 8);
171
172 unsigned SectionBID = ~0U;
173 uint64_t SectionBOffset = 0;
174
176 Obj.getRelocation(RelI->getRawDataRefImpl());
177
178 bool AIsExternal = BaseObj.getPlainRelocationExternal(RelInfo);
179
180 if (AIsExternal) {
181 Expected<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName();
182 if (!SubtrahendNameOrErr)
183 return SubtrahendNameOrErr.takeError();
184 auto SubtrahendI = GlobalSymbolTable.find(*SubtrahendNameOrErr);
185 SectionBID = SubtrahendI->second.getSectionID();
186 SectionBOffset = SubtrahendI->second.getOffset();
187 } else {
188 SectionRef SecB = Obj.getAnyRelocationSection(RelInfo);
189 bool IsCode = SecB.isText();
190 Expected<unsigned> SectionBIDOrErr =
191 findOrEmitSection(Obj, SecB, IsCode, ObjSectionToID);
192 if (!SectionBIDOrErr)
193 return SectionBIDOrErr.takeError();
194 SectionBID = *SectionBIDOrErr;
195 Addend += SecB.getAddress();
196 }
197
198 ++RelI;
199
200 unsigned SectionAID = ~0U;
201 uint64_t SectionAOffset = 0;
202
203 RelInfo = Obj.getRelocation(RelI->getRawDataRefImpl());
204
205 bool BIsExternal = BaseObj.getPlainRelocationExternal(RelInfo);
206 if (BIsExternal) {
207 Expected<StringRef> MinuendNameOrErr = RelI->getSymbol()->getName();
208 if (!MinuendNameOrErr)
209 return MinuendNameOrErr.takeError();
210 auto MinuendI = GlobalSymbolTable.find(*MinuendNameOrErr);
211 SectionAID = MinuendI->second.getSectionID();
212 SectionAOffset = MinuendI->second.getOffset();
213 } else {
214 SectionRef SecA = Obj.getAnyRelocationSection(RelInfo);
215 bool IsCode = SecA.isText();
216 Expected<unsigned> SectionAIDOrErr =
217 findOrEmitSection(Obj, SecA, IsCode, ObjSectionToID);
218 if (!SectionAIDOrErr)
219 return SectionAIDOrErr.takeError();
220 SectionAID = *SectionAIDOrErr;
221 Addend -= SecA.getAddress();
222 }
223
225 SectionAID, SectionAOffset, SectionBID, SectionBOffset,
226 false, Size);
227
228 addRelocationForSection(R, SectionAID);
229
230 return ++RelI;
231 }
232
233};
234}
235
236#undef DEBUG_TYPE
237
238#endif
#define LLVM_DEBUG(X)
Definition: Debug.h:101
uint64_t Addr
uint64_t Size
#define UNIMPLEMENTED_RELOC(RelType)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
Symbol resolution interface.
Definition: JITSymbol.h:371
RelocationEntry - used to represent relocations internally in the dynamic linker.
unsigned Size
The size of this relocation (MachO specific).
uint32_t RelType
RelType - relocation type.
uint64_t Offset
Offset - offset into the section.
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
unsigned SectionID
SectionID - the section this relocation points to.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2213
std::map< SectionRef, unsigned > ObjSectionToIDMap
std::map< RelocationValueRef, uintptr_t > StubMap
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
RTDyldSymbolTable GlobalSymbolTable
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
Expected< relocation_iterator > processRelocationRef(unsigned SectionID, relocation_iterator RelI, const ObjectFile &BaseObjT, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) override
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
unsigned getMaxStubSize() const override
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override
A object file specific relocation resolver.
Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section)
RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)
int64_t memcpyAddend(const RelocationEntry &RE) const
This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...
void makeValueAddendPCRel(RelocationValueRef &Value, const relocation_iterator &RI, unsigned OffsetToNextPC)
Make the RelocationValueRef addend PC-relative.
RelocationEntry getRelocationEntry(unsigned SectionID, const ObjectFile &BaseTObj, const relocation_iterator &RI) const
Given a relocation_iterator for a non-scattered relocation, construct a RelocationEntry and fill in t...
Expected< RelocationValueRef > getRelocationValueRef(const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID)
Construct a RelocationValueRef representing the relocation target.
void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const
Dump information about the relocation entry (RE) and resolved value.
SectionEntry - represents a section emitted into memory by the dynamic linker.
iterator find(StringRef Key)
Definition: StringMap.h:233
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LLVM Value Representation.
Definition: Value.h:74
SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
bool isRelocationScattered(const MachO::any_relocation_info &RE) const
This class is the base class for all object file types.
Definition: ObjectFile.h:229
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
uint64_t getAddress() const
Definition: ObjectFile.h:521
bool isText() const
Whether this section contains instructions.
Definition: ObjectFile.h:550
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ X86_64_RELOC_UNSIGNED
Definition: MachO.h:483
@ X86_64_RELOC_SIGNED
Definition: MachO.h:484
@ X86_64_RELOC_GOT
Definition: MachO.h:487
@ X86_64_RELOC_GOT_LOAD
Definition: MachO.h:486
@ X86_64_RELOC_BRANCH
Definition: MachO.h:485
@ X86_64_RELOC_SIGNED_2
Definition: MachO.h:490
@ X86_64_RELOC_TLV
Definition: MachO.h:492
@ X86_64_RELOC_SIGNED_4
Definition: MachO.h:491
@ X86_64_RELOC_SUBTRACTOR
Definition: MachO.h:488
@ X86_64_RELOC_SIGNED_1
Definition: MachO.h:489
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition: MathExtras.h:507
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39