• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===---- RuntimeDyldMachOI386.h ---- MachO/I386 specific code. ---*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOI386_H
11 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOI386_H
12 
13 #include "../RuntimeDyldMachO.h"
14 
15 #define DEBUG_TYPE "dyld"
16 
17 namespace llvm {
18 
19 class RuntimeDyldMachOI386
20     : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOI386> {
21 public:
22 
23   typedef uint32_t TargetPtrT;
24 
RuntimeDyldMachOI386(RuntimeDyld::MemoryManager & MM,RuntimeDyld::SymbolResolver & Resolver)25   RuntimeDyldMachOI386(RuntimeDyld::MemoryManager &MM,
26                        RuntimeDyld::SymbolResolver &Resolver)
27       : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
28 
getMaxStubSize()29   unsigned getMaxStubSize() override { return 0; }
30 
getStubAlignment()31   unsigned getStubAlignment() override { return 1; }
32 
33   relocation_iterator
processRelocationRef(unsigned SectionID,relocation_iterator RelI,const ObjectFile & BaseObjT,ObjSectionToIDMap & ObjSectionToID,StubMap & Stubs)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);
43 
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,
48                                          ObjSectionToID);
49       else if (RelType == MachO::GENERIC_RELOC_VANILLA)
50         return processScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID);
51       llvm_unreachable("Unhandled scattered relocation.");
52     }
53 
54     RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
55     RE.Addend = memcpyAddend(RE);
56     RelocationValueRef Value(
57         getRelocationValueRef(Obj, RelI, RE, ObjSectionToID));
58 
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;
68     // }
69     if (RE.IsPCRel)
70       makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
71 
72     RE.Addend = Value.Offset;
73 
74     if (Value.SymbolName)
75       addRelocationForSymbol(RE, Value.SymbolName);
76     else
77       addRelocationForSection(RE, Value.SectionID);
78 
79     return ++RelI;
80   }
81 
resolveRelocation(const RelocationEntry & RE,uint64_t Value)82   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
83     DEBUG(dumpRelocationToResolve(RE, Value));
84 
85     const SectionEntry &Section = Sections[RE.SectionID];
86     uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
87 
88     if (RE.IsPCRel) {
89       uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
90       Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
91     }
92 
93     switch (RE.RelType) {
94     default:
95       llvm_unreachable("Invalid relocation type!");
96     case MachO::GENERIC_RELOC_VANILLA:
97       writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
98       break;
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);
107       break;
108     }
109     case MachO::GENERIC_RELOC_PB_LA_PTR:
110       Error("Relocation type not implemented yet!");
111     }
112   }
113 
finalizeSection(const ObjectFile & Obj,unsigned SectionID,const SectionRef & Section)114   void finalizeSection(const ObjectFile &Obj, unsigned SectionID,
115                        const SectionRef &Section) {
116     StringRef Name;
117     Section.getName(Name);
118 
119     if (Name == "__jump_table")
120       populateJumpTable(cast<MachOObjectFile>(Obj), Section, SectionID);
121     else if (Name == "__pointers")
122       populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
123                                             Section, SectionID);
124   }
125 
126 private:
127   relocation_iterator
processSECTDIFFRelocation(unsigned SectionID,relocation_iterator RelI,const ObjectFile & BaseObjT,ObjSectionToIDMap & ObjSectionToID)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());
135 
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);
144 
145     ++RelI;
146     MachO::any_relocation_info RE2 =
147         Obj.getRelocation(RelI->getRawDataRefImpl());
148 
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);
158 
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);
167 
168     // Compute the addend 'C' from the original expression 'A - B + C'.
169     Addend -= AddrA - AddrB;
170 
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,
178                       IsPCRel, Size);
179 
180     addRelocationForSection(R, SectionAID);
181 
182     return ++RelI;
183   }
184 
185   // Populate stubs in __jump_table section.
populateJumpTable(const MachOObjectFile & Obj,const SectionRef & JTSection,unsigned JTSectionID)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.");
190 
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;
199 
200     assert((JTSectionSize % JTEntrySize) == 0 &&
201            "Jump-table section does not contain a whole number of stubs?");
202 
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;
216     }
217   }
218 
219 };
220 }
221 
222 #undef DEBUG_TYPE
223 
224 #endif
225