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(RuntimeDyld::MemoryManager &MM,
26 RuntimeDyld::SymbolResolver &Resolver)
27 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
29 unsigned getMaxStubSize() override { return 0; }
31 unsigned getStubAlignment() override { return 1; }
34 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
35 const ObjectFile &BaseObjT,
36 ObjSectionToIDMap &ObjSectionToID,
37 StubMap &Stubs) override {
38 const MachOObjectFile &Obj =
39 static_cast<const MachOObjectFile &>(BaseObjT);
40 MachO::any_relocation_info RelInfo =
41 Obj.getRelocation(RelI->getRawDataRefImpl());
42 uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
44 if (Obj.isRelocationScattered(RelInfo)) {
45 if (RelType == MachO::GENERIC_RELOC_SECTDIFF ||
46 RelType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)
47 return processSECTDIFFRelocation(SectionID, RelI, Obj,
49 else if (RelType == MachO::GENERIC_RELOC_VANILLA)
50 return processScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID);
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));
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, 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.getAddressWithOffset(RE.Offset);
89 uint64_t FinalAddress = Section.getLoadAddressWithOffset(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].getLoadAddress();
102 uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
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);
140 uint64_t Offset = RelI->getOffset();
141 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
142 unsigned NumBytes = 1 << Size;
143 uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
146 MachO::any_relocation_info RE2 =
147 Obj.getRelocation(RelI->getRawDataRefImpl());
149 uint32_t AddrA = Obj.getScatteredRelocationValue(RE);
150 section_iterator SAI = getSectionByAddress(Obj, AddrA);
151 assert(SAI != Obj.section_end() && "Can't find section for address A");
152 uint64_t SectionABase = SAI->getAddress();
153 uint64_t SectionAOffset = AddrA - SectionABase;
154 SectionRef SectionA = *SAI;
155 bool IsCode = SectionA.isText();
156 uint32_t SectionAID =
157 findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID);
159 uint32_t AddrB = Obj.getScatteredRelocationValue(RE2);
160 section_iterator SBI = getSectionByAddress(Obj, AddrB);
161 assert(SBI != Obj.section_end() && "Can't find section for address B");
162 uint64_t SectionBBase = SBI->getAddress();
163 uint64_t SectionBOffset = AddrB - SectionBBase;
164 SectionRef SectionB = *SBI;
165 uint32_t SectionBID =
166 findOrEmitSection(Obj, SectionB, IsCode, ObjSectionToID);
168 // Compute the addend 'C' from the original expression 'A - B + C'.
169 Addend -= AddrA - AddrB;
171 DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
172 << ", Addend: " << Addend << ", SectionA ID: " << SectionAID
173 << ", SectionAOffset: " << SectionAOffset
174 << ", SectionB ID: " << SectionBID
175 << ", SectionBOffset: " << SectionBOffset << "\n");
176 RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
177 SectionAOffset, SectionBID, SectionBOffset,
180 addRelocationForSection(R, SectionAID);
185 // Populate stubs in __jump_table section.
186 void populateJumpTable(const MachOObjectFile &Obj, const SectionRef &JTSection,
187 unsigned JTSectionID) {
188 assert(!Obj.is64Bit() &&
189 "__jump_table section not supported in 64-bit MachO.");
191 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
192 MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
193 uint32_t JTSectionSize = Sec32.size;
194 unsigned FirstIndirectSymbol = Sec32.reserved1;
195 unsigned JTEntrySize = Sec32.reserved2;
196 unsigned NumJTEntries = JTSectionSize / JTEntrySize;
197 uint8_t *JTSectionAddr = getSectionAddress(JTSectionID);
198 unsigned JTEntryOffset = 0;
200 assert((JTSectionSize % JTEntrySize) == 0 &&
201 "Jump-table section does not contain a whole number of stubs?");
203 for (unsigned i = 0; i < NumJTEntries; ++i) {
204 unsigned SymbolIndex =
205 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
206 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
207 ErrorOr<StringRef> IndirectSymbolName = SI->getName();
208 if (std::error_code EC = IndirectSymbolName.getError())
209 report_fatal_error(EC.message());
210 uint8_t *JTEntryAddr = JTSectionAddr + JTEntryOffset;
211 createStubFunction(JTEntryAddr);
212 RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
213 MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
214 addRelocationForSymbol(RE, *IndirectSymbolName);
215 JTEntryOffset += JTEntrySize;