LLVM 22.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);
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
156 Expected<relocation_iterator>
157 processSubtractRelocation(unsigned SectionID, relocation_iterator RelI,
158 const MachOObjectFile &BaseObj,
159 ObjSectionToIDMap &ObjSectionToID) {
160 const MachOObjectFile &Obj = BaseObj;
161 MachO::any_relocation_info RE =
162 Obj.getRelocation(RelI->getRawDataRefImpl());
163
164 unsigned Size = Obj.getAnyRelocationLength(RE);
165 uint64_t Offset = RelI->getOffset();
166 uint8_t *LocalAddress = Sections[SectionID].getAddressWithOffset(Offset);
167 unsigned NumBytes = 1 << Size;
168 int64_t Addend =
169 SignExtend64(readBytesUnaligned(LocalAddress, NumBytes), NumBytes * 8);
170
171 unsigned SectionBID = ~0U;
172 uint64_t SectionBOffset = 0;
173
174 MachO::any_relocation_info RelInfo =
175 Obj.getRelocation(RelI->getRawDataRefImpl());
176
177 bool AIsExternal = BaseObj.getPlainRelocationExternal(RelInfo);
178
179 if (AIsExternal) {
180 Expected<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName();
181 if (!SubtrahendNameOrErr)
182 return SubtrahendNameOrErr.takeError();
183 auto SubtrahendI = GlobalSymbolTable.find(*SubtrahendNameOrErr);
184 SectionBID = SubtrahendI->second.getSectionID();
185 SectionBOffset = SubtrahendI->second.getOffset();
186 } else {
187 SectionRef SecB = Obj.getAnyRelocationSection(RelInfo);
188 bool IsCode = SecB.isText();
189 Expected<unsigned> SectionBIDOrErr =
190 findOrEmitSection(Obj, SecB, IsCode, ObjSectionToID);
191 if (!SectionBIDOrErr)
192 return SectionBIDOrErr.takeError();
193 SectionBID = *SectionBIDOrErr;
194 Addend += SecB.getAddress();
195 }
196
197 ++RelI;
198
199 unsigned SectionAID = ~0U;
200 uint64_t SectionAOffset = 0;
201
202 RelInfo = Obj.getRelocation(RelI->getRawDataRefImpl());
203
204 bool BIsExternal = BaseObj.getPlainRelocationExternal(RelInfo);
205 if (BIsExternal) {
206 Expected<StringRef> MinuendNameOrErr = RelI->getSymbol()->getName();
207 if (!MinuendNameOrErr)
208 return MinuendNameOrErr.takeError();
209 auto MinuendI = GlobalSymbolTable.find(*MinuendNameOrErr);
210 SectionAID = MinuendI->second.getSectionID();
211 SectionAOffset = MinuendI->second.getOffset();
212 } else {
213 SectionRef SecA = Obj.getAnyRelocationSection(RelInfo);
214 bool IsCode = SecA.isText();
215 Expected<unsigned> SectionAIDOrErr =
216 findOrEmitSection(Obj, SecA, IsCode, ObjSectionToID);
217 if (!SectionAIDOrErr)
218 return SectionAIDOrErr.takeError();
219 SectionAID = *SectionAIDOrErr;
220 Addend -= SecA.getAddress();
221 }
222
223 RelocationEntry R(SectionID, Offset, MachO::X86_64_RELOC_SUBTRACTOR, (uint64_t)Addend,
224 SectionAID, SectionAOffset, SectionBID, SectionBOffset,
225 false, Size);
226
227 addRelocationForSection(R, SectionAID);
228
229 return ++RelI;
230 }
231
232};
233}
234
235#undef DEBUG_TYPE
236
237#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define UNIMPLEMENTED_RELOC(RelType)
#define LLVM_DEBUG(...)
Definition Debug.h:114
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
Symbol resolution interface.
Definition JITSymbol.h:373
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:2199
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
RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
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.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
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:231
uint64_t getOffset() const
Definition ObjectFile.h:625
symbol_iterator getSymbol() const
Definition ObjectFile.h:629
DataRefImpl getRawDataRefImpl() const
Definition ObjectFile.h:641
This is a value type class that represents a single section in the list of sections in the object fil...
Definition ObjectFile.h:83
uint64_t getAddress() const
Definition ObjectFile.h:526
bool isText() const
Whether this section contains instructions.
Definition ObjectFile.h:555
Expected< StringRef > getName() const
Definition ObjectFile.h:465
#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
content_iterator< RelocationRef > relocation_iterator
Definition ObjectFile.h:79
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
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:583
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39