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 const ObjectFile &BaseObjT,
35 ObjSectionToIDMap &ObjSectionToID,
36 const SymbolTableMap &Symbols, StubMap &Stubs) override {
37 const MachOObjectFile &Obj =
38 static_cast<const MachOObjectFile &>(BaseObjT);
39 MachO::any_relocation_info RelInfo =
40 Obj.getRelocation(RelI->getRawDataRefImpl());
41 uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
43 if (Obj.isRelocationScattered(RelInfo)) {
44 if (RelType == MachO::GENERIC_RELOC_SECTDIFF ||
45 RelType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)
46 return processSECTDIFFRelocation(SectionID, RelI, Obj,
48 else if (RelType == MachO::GENERIC_RELOC_VANILLA)
49 return processI386ScatteredVANILLA(SectionID, RelI, Obj,
51 llvm_unreachable("Unhandled scattered relocation.");
54 RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
55 RE.Addend = memcpyAddend(RE);
56 RelocationValueRef Value(
57 getRelocationValueRef(Obj, RelI, RE, ObjSectionToID, Symbols));
59 // Addends for external, PC-rel relocations on i386 point back to the zero
60 // offset. Calculate the final offset from the relocation target instead.
61 // This allows us to use the same logic for both external and internal
62 // relocations in resolveI386RelocationRef.
63 // bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
64 // if (IsExtern && RE.IsPCRel) {
65 // uint64_t RelocAddr = 0;
66 // RelI->getAddress(RelocAddr);
67 // Value.Addend += RelocAddr + 4;
70 makeValueAddendPCRel(Value, Obj, RelI, 1 << RE.Size);
72 RE.Addend = Value.Offset;
75 addRelocationForSymbol(RE, Value.SymbolName);
77 addRelocationForSection(RE, Value.SectionID);
82 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
83 DEBUG(dumpRelocationToResolve(RE, Value));
85 const SectionEntry &Section = Sections[RE.SectionID];
86 uint8_t *LocalAddress = Section.Address + RE.Offset;
89 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
90 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
95 llvm_unreachable("Invalid relocation type!");
96 case MachO::GENERIC_RELOC_VANILLA:
97 writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
99 case MachO::GENERIC_RELOC_SECTDIFF:
100 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
101 uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress;
102 uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress;
103 assert((Value == SectionABase || Value == SectionBBase) &&
104 "Unexpected SECTDIFF relocation value.");
105 Value = SectionABase - SectionBBase + RE.Addend;
106 writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
109 case MachO::GENERIC_RELOC_PB_LA_PTR:
110 Error("Relocation type not implemented yet!");
114 void finalizeSection(const ObjectFile &Obj, unsigned SectionID,
115 const SectionRef &Section) {
117 Section.getName(Name);
119 if (Name == "__jump_table")
120 populateJumpTable(cast<MachOObjectFile>(Obj), Section, SectionID);
121 else if (Name == "__pointers")
122 populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
128 processSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
129 const ObjectFile &BaseObjT,
130 ObjSectionToIDMap &ObjSectionToID) {
131 const MachOObjectFile &Obj =
132 static_cast<const MachOObjectFile&>(BaseObjT);
133 MachO::any_relocation_info RE =
134 Obj.getRelocation(RelI->getRawDataRefImpl());
136 SectionEntry &Section = Sections[SectionID];
137 uint32_t RelocType = Obj.getAnyRelocationType(RE);
138 bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
139 unsigned Size = Obj.getAnyRelocationLength(RE);
141 RelI->getOffset(Offset);
142 uint8_t *LocalAddress = Section.Address + Offset;
143 unsigned NumBytes = 1 << Size;
144 uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
147 MachO::any_relocation_info RE2 =
148 Obj.getRelocation(RelI->getRawDataRefImpl());
150 uint32_t AddrA = Obj.getScatteredRelocationValue(RE);
151 section_iterator SAI = getSectionByAddress(Obj, AddrA);
152 assert(SAI != Obj.section_end() && "Can't find section for address A");
153 uint64_t SectionABase = SAI->getAddress();
154 uint64_t SectionAOffset = AddrA - SectionABase;
155 SectionRef SectionA = *SAI;
156 bool IsCode = SectionA.isText();
157 uint32_t SectionAID =
158 findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID);
160 uint32_t AddrB = Obj.getScatteredRelocationValue(RE2);
161 section_iterator SBI = getSectionByAddress(Obj, AddrB);
162 assert(SBI != Obj.section_end() && "Can't find section for address B");
163 uint64_t SectionBBase = SBI->getAddress();
164 uint64_t SectionBOffset = AddrB - SectionBBase;
165 SectionRef SectionB = *SBI;
166 uint32_t SectionBID =
167 findOrEmitSection(Obj, SectionB, IsCode, ObjSectionToID);
169 if (Addend != AddrA - AddrB)
170 Error("Unexpected SECTDIFF relocation addend.");
172 DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
173 << ", Addend: " << Addend << ", SectionA ID: " << SectionAID
174 << ", SectionAOffset: " << SectionAOffset
175 << ", SectionB ID: " << SectionBID
176 << ", SectionBOffset: " << SectionBOffset << "\n");
177 RelocationEntry R(SectionID, Offset, RelocType, 0, SectionAID,
178 SectionAOffset, SectionBID, SectionBOffset, IsPCRel,
181 addRelocationForSection(R, SectionAID);
182 addRelocationForSection(R, SectionBID);
187 relocation_iterator processI386ScatteredVANILLA(
188 unsigned SectionID, relocation_iterator RelI,
189 const ObjectFile &BaseObjT,
190 RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID) {
191 const MachOObjectFile &Obj =
192 static_cast<const MachOObjectFile&>(BaseObjT);
193 MachO::any_relocation_info RE =
194 Obj.getRelocation(RelI->getRawDataRefImpl());
196 SectionEntry &Section = Sections[SectionID];
197 uint32_t RelocType = Obj.getAnyRelocationType(RE);
198 bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
199 unsigned Size = Obj.getAnyRelocationLength(RE);
201 RelI->getOffset(Offset);
202 uint8_t *LocalAddress = Section.Address + Offset;
203 unsigned NumBytes = 1 << Size;
204 int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
206 unsigned SymbolBaseAddr = Obj.getScatteredRelocationValue(RE);
207 section_iterator TargetSI = getSectionByAddress(Obj, SymbolBaseAddr);
208 assert(TargetSI != Obj.section_end() && "Can't find section for symbol");
209 uint64_t SectionBaseAddr = TargetSI->getAddress();
210 SectionRef TargetSection = *TargetSI;
211 bool IsCode = TargetSection.isText();
212 uint32_t TargetSectionID =
213 findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID);
215 Addend -= SectionBaseAddr;
216 RelocationEntry R(SectionID, Offset, RelocType, Addend, IsPCRel, Size);
218 addRelocationForSection(R, TargetSectionID);
223 // Populate stubs in __jump_table section.
224 void populateJumpTable(const MachOObjectFile &Obj, const SectionRef &JTSection,
225 unsigned JTSectionID) {
226 assert(!Obj.is64Bit() &&
227 "__jump_table section not supported in 64-bit MachO.");
229 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
230 MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
231 uint32_t JTSectionSize = Sec32.size;
232 unsigned FirstIndirectSymbol = Sec32.reserved1;
233 unsigned JTEntrySize = Sec32.reserved2;
234 unsigned NumJTEntries = JTSectionSize / JTEntrySize;
235 uint8_t *JTSectionAddr = getSectionAddress(JTSectionID);
236 unsigned JTEntryOffset = 0;
238 assert((JTSectionSize % JTEntrySize) == 0 &&
239 "Jump-table section does not contain a whole number of stubs?");
241 for (unsigned i = 0; i < NumJTEntries; ++i) {
242 unsigned SymbolIndex =
243 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
244 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
245 StringRef IndirectSymbolName;
246 SI->getName(IndirectSymbolName);
247 uint8_t *JTEntryAddr = JTSectionAddr + JTEntryOffset;
248 createStubFunction(JTEntryAddr);
249 RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
250 MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
251 addRelocationForSymbol(RE, IndirectSymbolName);
252 JTEntryOffset += JTEntrySize;