LLVM 20.0.0git
RuntimeDyldMachOI386.h
Go to the documentation of this file.
1//===---- RuntimeDyldMachOI386.h ---- MachO/I386 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_RUNTIMEDYLDMACHOI386_H
10#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOI386_H
11
12#include "../RuntimeDyldMachO.h"
13
14#define DEBUG_TYPE "dyld"
15
16namespace llvm {
17
19 : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOI386> {
20public:
21
23
27
28 unsigned getMaxStubSize() const override { return 0; }
29
30 Align getStubAlignment() override { return Align(1); }
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 (Obj.isRelocationScattered(RelInfo)) {
44 if (RelType == MachO::GENERIC_RELOC_SECTDIFF ||
46 return processSECTDIFFRelocation(SectionID, RelI, Obj,
47 ObjSectionToID);
48 else if (RelType == MachO::GENERIC_RELOC_VANILLA)
49 return processScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID);
50 return make_error<RuntimeDyldError>(("Unhandled I386 scattered relocation "
51 "type: " + Twine(RelType)).str());
52 }
53
54 switch (RelType) {
58 default:
59 if (RelType > MachO::GENERIC_RELOC_TLV)
60 return make_error<RuntimeDyldError>(("MachO I386 relocation type " +
61 Twine(RelType) +
62 " is out of range").str());
63 break;
64 }
65
66 RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
67 RE.Addend = memcpyAddend(RE);
69 if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID))
70 Value = *ValueOrErr;
71 else
72 return ValueOrErr.takeError();
73
74 // Addends for external, PC-rel relocations on i386 point back to the zero
75 // offset. Calculate the final offset from the relocation target instead.
76 // This allows us to use the same logic for both external and internal
77 // relocations in resolveI386RelocationRef.
78 // bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
79 // if (IsExtern && RE.IsPCRel) {
80 // uint64_t RelocAddr = 0;
81 // RelI->getAddress(RelocAddr);
82 // Value.Addend += RelocAddr + 4;
83 // }
84 if (RE.IsPCRel)
85 makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
86
87 RE.Addend = Value.Offset;
88
89 if (Value.SymbolName)
90 addRelocationForSymbol(RE, Value.SymbolName);
91 else
92 addRelocationForSection(RE, Value.SectionID);
93
94 return ++RelI;
95 }
96
97 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
99
100 const SectionEntry &Section = Sections[RE.SectionID];
101 uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
102
103 if (RE.IsPCRel) {
104 uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
105 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
106 }
107
108 switch (RE.RelType) {
110 writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
111 break;
114 uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
115 uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
116 assert((Value == SectionABase || Value == SectionBBase) &&
117 "Unexpected SECTDIFF relocation value.");
118 Value = SectionABase - SectionBBase + RE.Addend;
119 writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
120 break;
121 }
122 default:
123 llvm_unreachable("Invalid relocation type!");
124 }
125 }
126
127 Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,
128 const SectionRef &Section) {
130 if (Expected<StringRef> NameOrErr = Section.getName())
131 Name = *NameOrErr;
132 else
133 consumeError(NameOrErr.takeError());
134
135 if (Name == "__jump_table")
136 return populateJumpTable(cast<MachOObjectFile>(Obj), Section, SectionID);
137 else if (Name == "__pointers")
138 return populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
139 Section, SectionID);
140 return Error::success();
141 }
142
143private:
145 processSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
146 const ObjectFile &BaseObjT,
147 ObjSectionToIDMap &ObjSectionToID) {
148 const MachOObjectFile &Obj =
149 static_cast<const MachOObjectFile&>(BaseObjT);
151 Obj.getRelocation(RelI->getRawDataRefImpl());
152
153 SectionEntry &Section = Sections[SectionID];
154 uint32_t RelocType = Obj.getAnyRelocationType(RE);
155 bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
156 unsigned Size = Obj.getAnyRelocationLength(RE);
157 uint64_t Offset = RelI->getOffset();
158 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
159 unsigned NumBytes = 1 << Size;
160 uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
161
162 ++RelI;
164 Obj.getRelocation(RelI->getRawDataRefImpl());
165
167 section_iterator SAI = getSectionByAddress(Obj, AddrA);
168 assert(SAI != Obj.section_end() && "Can't find section for address A");
169 uint64_t SectionABase = SAI->getAddress();
170 uint64_t SectionAOffset = AddrA - SectionABase;
171 SectionRef SectionA = *SAI;
172 bool IsCode = SectionA.isText();
173 uint32_t SectionAID = ~0U;
174 if (auto SectionAIDOrErr =
175 findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID))
176 SectionAID = *SectionAIDOrErr;
177 else
178 return SectionAIDOrErr.takeError();
179
180 uint32_t AddrB = Obj.getScatteredRelocationValue(RE2);
181 section_iterator SBI = getSectionByAddress(Obj, AddrB);
182 assert(SBI != Obj.section_end() && "Can't find section for address B");
183 uint64_t SectionBBase = SBI->getAddress();
184 uint64_t SectionBOffset = AddrB - SectionBBase;
185 SectionRef SectionB = *SBI;
186 uint32_t SectionBID = ~0U;
187 if (auto SectionBIDOrErr =
188 findOrEmitSection(Obj, SectionB, IsCode, ObjSectionToID))
189 SectionBID = *SectionBIDOrErr;
190 else
191 return SectionBIDOrErr.takeError();
192
193 // Compute the addend 'C' from the original expression 'A - B + C'.
194 Addend -= AddrA - AddrB;
195
196 LLVM_DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA
197 << ", AddrB: " << AddrB << ", Addend: " << Addend
198 << ", SectionA ID: " << SectionAID << ", SectionAOffset: "
199 << SectionAOffset << ", SectionB ID: " << SectionBID
200 << ", SectionBOffset: " << SectionBOffset << "\n");
201 RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
202 SectionAOffset, SectionBID, SectionBOffset,
203 IsPCRel, Size);
204
205 addRelocationForSection(R, SectionAID);
206
207 return ++RelI;
208 }
209
210 // Populate stubs in __jump_table section.
211 Error populateJumpTable(const MachOObjectFile &Obj,
212 const SectionRef &JTSection,
213 unsigned JTSectionID) {
215 MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
216 uint32_t JTSectionSize = Sec32.size;
217 unsigned FirstIndirectSymbol = Sec32.reserved1;
218 unsigned JTEntrySize = Sec32.reserved2;
219 unsigned NumJTEntries = JTSectionSize / JTEntrySize;
220 uint8_t *JTSectionAddr = getSectionAddress(JTSectionID);
221 unsigned JTEntryOffset = 0;
222
223 if (JTSectionSize % JTEntrySize != 0)
224 return make_error<RuntimeDyldError>("Jump-table section does not contain "
225 "a whole number of stubs?");
226
227 for (unsigned i = 0; i < NumJTEntries; ++i) {
228 unsigned SymbolIndex =
229 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
230 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
231 Expected<StringRef> IndirectSymbolName = SI->getName();
232 if (!IndirectSymbolName)
233 return IndirectSymbolName.takeError();
234 uint8_t *JTEntryAddr = JTSectionAddr + JTEntryOffset;
235 createStubFunction(JTEntryAddr);
236 RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
237 MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
238 addRelocationForSymbol(RE, *IndirectSymbolName);
239 JTEntryOffset += JTEntrySize;
240 }
241
242 return Error::success();
243 }
244
245};
246}
247
248#undef DEBUG_TYPE
249
250#endif
#define LLVM_DEBUG(...)
Definition: Debug.h:106
std::string Name
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:2203
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.
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
uint8_t * getSectionAddress(unsigned SectionID) const
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
unsigned getMaxStubSize() const override
Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section)
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 ...
RuntimeDyldMachOI386(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override
A object file specific relocation resolver.
static section_iterator getSectionByAddress(const MachOObjectFile &Obj, uint64_t Addr)
int64_t memcpyAddend(const RelocationEntry &RE) const
This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...
Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj, const SectionRef &PTSection, unsigned PTSectionID)
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.
Expected< relocation_iterator > processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI, const ObjectFile &BaseObjT, RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID, bool TargetIsLocalThumbFunc=false)
Process a scattered vanilla relocation.
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.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LLVM Value Representation.
Definition: Value.h:74
Expected< SectionRef > getSection(unsigned SectionIndex) const
section_iterator section_end() const override
uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const
MachO::dysymtab_command getDysymtabLoadCommand() const
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
symbol_iterator getSymbolByIndex(unsigned Index) const
unsigned getAnyRelocationPCRel(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
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) 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
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:598
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.
@ GENERIC_RELOC_LOCAL_SECTDIFF
Definition: MachO.h:414
@ GENERIC_RELOC_PB_LA_PTR
Definition: MachO.h:413
@ GENERIC_RELOC_PAIR
Definition: MachO.h:411
@ GENERIC_RELOC_VANILLA
Definition: MachO.h:410
@ GENERIC_RELOC_TLV
Definition: MachO.h:415
@ GENERIC_RELOC_SECTDIFF
Definition: MachO.h:412
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint32_t reserved2
Definition: MachO.h:577
uint32_t size
Definition: MachO.h:570
uint32_t reserved1
Definition: MachO.h:576