1 //===---- RuntimeDyldMachOI386.h ---- MachO/I386 specific code. ---*- 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 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOI386_H
11 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOI386_H
13 #include "../RuntimeDyldMachO.h"
15 #define DEBUG_TYPE "dyld"
19 class RuntimeDyldMachOI386
20 : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOI386> {
23 typedef uint32_t TargetPtrT;
25 RuntimeDyldMachOI386(RTDyldMemoryManager *MM)
26 : RuntimeDyldMachOCRTPBase(MM) {}
28 unsigned getMaxStubSize() override { return 0; }
30 unsigned getStubAlignment() override { return 1; }
33 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
34 ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
35 const SymbolTableMap &Symbols, StubMap &Stubs) override {
36 const MachOObjectFile &Obj =
37 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
38 MachO::any_relocation_info RelInfo =
39 Obj.getRelocation(RelI->getRawDataRefImpl());
40 uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
42 if (Obj.isRelocationScattered(RelInfo)) {
43 if (RelType == MachO::GENERIC_RELOC_SECTDIFF ||
44 RelType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)
45 return processSECTDIFFRelocation(SectionID, RelI, ObjImg,
47 else if (RelType == MachO::GENERIC_RELOC_VANILLA)
48 return processI386ScatteredVANILLA(SectionID, RelI, ObjImg,
50 llvm_unreachable("Unhandled scattered relocation.");
53 RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI));
54 RE.Addend = memcpyAddend(RE);
55 RelocationValueRef Value(
56 getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
58 // Addends for external, PC-rel relocations on i386 point back to the zero
59 // offset. Calculate the final offset from the relocation target instead.
60 // This allows us to use the same logic for both external and internal
61 // relocations in resolveI386RelocationRef.
62 // bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
63 // if (IsExtern && RE.IsPCRel) {
64 // uint64_t RelocAddr = 0;
65 // RelI->getAddress(RelocAddr);
66 // Value.Addend += RelocAddr + 4;
69 makeValueAddendPCRel(Value, ObjImg, RelI, 1 << RE.Size);
71 RE.Addend = Value.Offset;
74 addRelocationForSymbol(RE, Value.SymbolName);
76 addRelocationForSection(RE, Value.SectionID);
81 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
82 DEBUG(dumpRelocationToResolve(RE, Value));
84 const SectionEntry &Section = Sections[RE.SectionID];
85 uint8_t *LocalAddress = Section.Address + RE.Offset;
88 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
89 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
94 llvm_unreachable("Invalid relocation type!");
95 case MachO::GENERIC_RELOC_VANILLA:
96 writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
98 case MachO::GENERIC_RELOC_SECTDIFF:
99 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
100 uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress;
101 uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress;
102 assert((Value == SectionABase || Value == SectionBBase) &&
103 "Unexpected SECTDIFF relocation value.");
104 Value = SectionABase - SectionBBase + RE.Addend;
105 writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
108 case MachO::GENERIC_RELOC_PB_LA_PTR:
109 Error("Relocation type not implemented yet!");
113 void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
114 const SectionRef &Section) {
116 Section.getName(Name);
118 if (Name == "__jump_table")
119 populateJumpTable(cast<MachOObjectFile>(*ObjImg.getObjectFile()), Section,
121 else if (Name == "__pointers")
122 populateIndirectSymbolPointersSection(
123 cast<MachOObjectFile>(*ObjImg.getObjectFile()),
129 processSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
131 ObjSectionToIDMap &ObjSectionToID) {
132 const MachOObjectFile *MachO =
133 static_cast<const MachOObjectFile *>(Obj.getObjectFile());
134 MachO::any_relocation_info RE =
135 MachO->getRelocation(RelI->getRawDataRefImpl());
137 SectionEntry &Section = Sections[SectionID];
138 uint32_t RelocType = MachO->getAnyRelocationType(RE);
139 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
140 unsigned Size = MachO->getAnyRelocationLength(RE);
142 RelI->getOffset(Offset);
143 uint8_t *LocalAddress = Section.Address + Offset;
144 unsigned NumBytes = 1 << Size;
146 memcpy(&Addend, LocalAddress, NumBytes);
149 MachO::any_relocation_info RE2 =
150 MachO->getRelocation(RelI->getRawDataRefImpl());
152 uint32_t AddrA = MachO->getScatteredRelocationValue(RE);
153 section_iterator SAI = getSectionByAddress(*MachO, AddrA);
154 assert(SAI != MachO->section_end() && "Can't find section for address A");
155 uint64_t SectionABase;
156 SAI->getAddress(SectionABase);
157 uint64_t SectionAOffset = AddrA - SectionABase;
158 SectionRef SectionA = *SAI;
160 SectionA.isText(IsCode);
161 uint32_t SectionAID =
162 findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID);
164 uint32_t AddrB = MachO->getScatteredRelocationValue(RE2);
165 section_iterator SBI = getSectionByAddress(*MachO, AddrB);
166 assert(SBI != MachO->section_end() && "Can't find section for address B");
167 uint64_t SectionBBase;
168 SBI->getAddress(SectionBBase);
169 uint64_t SectionBOffset = AddrB - SectionBBase;
170 SectionRef SectionB = *SBI;
171 uint32_t SectionBID =
172 findOrEmitSection(Obj, SectionB, IsCode, ObjSectionToID);
174 if (Addend != AddrA - AddrB)
175 Error("Unexpected SECTDIFF relocation addend.");
177 DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
178 << ", Addend: " << Addend << ", SectionA ID: " << SectionAID
179 << ", SectionAOffset: " << SectionAOffset
180 << ", SectionB ID: " << SectionBID
181 << ", SectionBOffset: " << SectionBOffset << "\n");
182 RelocationEntry R(SectionID, Offset, RelocType, 0, SectionAID,
183 SectionAOffset, SectionBID, SectionBOffset, IsPCRel,
186 addRelocationForSection(R, SectionAID);
187 addRelocationForSection(R, SectionBID);
192 relocation_iterator processI386ScatteredVANILLA(
193 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
194 RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID) {
195 const MachOObjectFile *MachO =
196 static_cast<const MachOObjectFile *>(Obj.getObjectFile());
197 MachO::any_relocation_info RE =
198 MachO->getRelocation(RelI->getRawDataRefImpl());
200 SectionEntry &Section = Sections[SectionID];
201 uint32_t RelocType = MachO->getAnyRelocationType(RE);
202 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
203 unsigned Size = MachO->getAnyRelocationLength(RE);
205 RelI->getOffset(Offset);
206 uint8_t *LocalAddress = Section.Address + Offset;
207 unsigned NumBytes = 1 << Size;
209 memcpy(&Addend, LocalAddress, NumBytes);
211 unsigned SymbolBaseAddr = MachO->getScatteredRelocationValue(RE);
212 section_iterator TargetSI = getSectionByAddress(*MachO, SymbolBaseAddr);
213 assert(TargetSI != MachO->section_end() && "Can't find section for symbol");
214 uint64_t SectionBaseAddr;
215 TargetSI->getAddress(SectionBaseAddr);
216 SectionRef TargetSection = *TargetSI;
218 TargetSection.isText(IsCode);
219 uint32_t TargetSectionID =
220 findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID);
222 Addend -= SectionBaseAddr;
223 RelocationEntry R(SectionID, Offset, RelocType, Addend, IsPCRel, Size);
225 addRelocationForSection(R, TargetSectionID);
230 // Populate stubs in __jump_table section.
231 void populateJumpTable(MachOObjectFile &Obj, const SectionRef &JTSection,
232 unsigned JTSectionID) {
233 assert(!Obj.is64Bit() &&
234 "__jump_table section not supported in 64-bit MachO.");
236 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
237 MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
238 uint32_t JTSectionSize = Sec32.size;
239 unsigned FirstIndirectSymbol = Sec32.reserved1;
240 unsigned JTEntrySize = Sec32.reserved2;
241 unsigned NumJTEntries = JTSectionSize / JTEntrySize;
242 uint8_t *JTSectionAddr = getSectionAddress(JTSectionID);
243 unsigned JTEntryOffset = 0;
245 assert((JTSectionSize % JTEntrySize) == 0 &&
246 "Jump-table section does not contain a whole number of stubs?");
248 for (unsigned i = 0; i < NumJTEntries; ++i) {
249 unsigned SymbolIndex =
250 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
251 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
252 StringRef IndirectSymbolName;
253 SI->getName(IndirectSymbolName);
254 uint8_t *JTEntryAddr = JTSectionAddr + JTEntryOffset;
255 createStubFunction(JTEntryAddr);
256 RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
257 MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
258 addRelocationForSymbol(RE, IndirectSymbolName);
259 JTEntryOffset += JTEntrySize;