Remove bogus std::error_code returns form SectionRef.
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / Targets / RuntimeDyldMachOI386.h
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
25   RuntimeDyldMachOI386(RTDyldMemoryManager *MM)
26       : RuntimeDyldMachOCRTPBase(MM) {}
27
28   unsigned getMaxStubSize() override { return 0; }
29
30   unsigned getStubAlignment() override { return 1; }
31
32   relocation_iterator
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);
41
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,
46                                          ObjSectionToID);
47       else if (RelType == MachO::GENERIC_RELOC_VANILLA)
48         return processI386ScatteredVANILLA(SectionID, RelI, ObjImg,
49                                            ObjSectionToID);
50       llvm_unreachable("Unhandled scattered relocation.");
51     }
52
53     RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI));
54     RE.Addend = memcpyAddend(RE);
55     RelocationValueRef Value(
56         getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
57
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;
67     // }
68     if (RE.IsPCRel)
69       makeValueAddendPCRel(Value, ObjImg, RelI, 1 << RE.Size);
70
71     RE.Addend = Value.Offset;
72
73     if (Value.SymbolName)
74       addRelocationForSymbol(RE, Value.SymbolName);
75     else
76       addRelocationForSection(RE, Value.SectionID);
77
78     return ++RelI;
79   }
80
81   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
82     DEBUG(dumpRelocationToResolve(RE, Value));
83
84     const SectionEntry &Section = Sections[RE.SectionID];
85     uint8_t *LocalAddress = Section.Address + RE.Offset;
86
87     if (RE.IsPCRel) {
88       uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
89       Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
90     }
91
92     switch (RE.RelType) {
93     default:
94       llvm_unreachable("Invalid relocation type!");
95     case MachO::GENERIC_RELOC_VANILLA:
96       writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
97       break;
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);
106       break;
107     }
108     case MachO::GENERIC_RELOC_PB_LA_PTR:
109       Error("Relocation type not implemented yet!");
110     }
111   }
112
113   void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
114                        const SectionRef &Section) {
115     StringRef Name;
116     Section.getName(Name);
117
118     if (Name == "__jump_table")
119       populateJumpTable(cast<MachOObjectFile>(*ObjImg.getObjectFile()), Section,
120                         SectionID);
121     else if (Name == "__pointers")
122       populateIndirectSymbolPointersSection(
123                                  cast<MachOObjectFile>(*ObjImg.getObjectFile()),
124                                  Section, SectionID);
125   }
126
127 private:
128   relocation_iterator
129   processSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
130                             ObjectImage &Obj,
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());
136
137     SectionEntry &Section = Sections[SectionID];
138     uint32_t RelocType = MachO->getAnyRelocationType(RE);
139     bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
140     unsigned Size = MachO->getAnyRelocationLength(RE);
141     uint64_t Offset;
142     RelI->getOffset(Offset);
143     uint8_t *LocalAddress = Section.Address + Offset;
144     unsigned NumBytes = 1 << Size;
145     int64_t Addend = 0;
146     memcpy(&Addend, LocalAddress, NumBytes);
147
148     ++RelI;
149     MachO::any_relocation_info RE2 =
150         MachO->getRelocation(RelI->getRawDataRefImpl());
151
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 = SAI->getAddress();
156     uint64_t SectionAOffset = AddrA - SectionABase;
157     SectionRef SectionA = *SAI;
158     bool IsCode = SectionA.isText();
159     uint32_t SectionAID =
160         findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID);
161
162     uint32_t AddrB = MachO->getScatteredRelocationValue(RE2);
163     section_iterator SBI = getSectionByAddress(*MachO, AddrB);
164     assert(SBI != MachO->section_end() && "Can't find section for address B");
165     uint64_t SectionBBase = SBI->getAddress();
166     uint64_t SectionBOffset = AddrB - SectionBBase;
167     SectionRef SectionB = *SBI;
168     uint32_t SectionBID =
169         findOrEmitSection(Obj, SectionB, IsCode, ObjSectionToID);
170
171     if (Addend != AddrA - AddrB)
172       Error("Unexpected SECTDIFF relocation addend.");
173
174     DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
175                  << ", Addend: " << Addend << ", SectionA ID: " << SectionAID
176                  << ", SectionAOffset: " << SectionAOffset
177                  << ", SectionB ID: " << SectionBID
178                  << ", SectionBOffset: " << SectionBOffset << "\n");
179     RelocationEntry R(SectionID, Offset, RelocType, 0, SectionAID,
180                       SectionAOffset, SectionBID, SectionBOffset, IsPCRel,
181                       Size);
182
183     addRelocationForSection(R, SectionAID);
184     addRelocationForSection(R, SectionBID);
185
186     return ++RelI;
187   }
188
189   relocation_iterator processI386ScatteredVANILLA(
190       unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
191       RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID) {
192     const MachOObjectFile *MachO =
193         static_cast<const MachOObjectFile *>(Obj.getObjectFile());
194     MachO::any_relocation_info RE =
195         MachO->getRelocation(RelI->getRawDataRefImpl());
196
197     SectionEntry &Section = Sections[SectionID];
198     uint32_t RelocType = MachO->getAnyRelocationType(RE);
199     bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
200     unsigned Size = MachO->getAnyRelocationLength(RE);
201     uint64_t Offset;
202     RelI->getOffset(Offset);
203     uint8_t *LocalAddress = Section.Address + Offset;
204     unsigned NumBytes = 1 << Size;
205     int64_t Addend = 0;
206     memcpy(&Addend, LocalAddress, NumBytes);
207
208     unsigned SymbolBaseAddr = MachO->getScatteredRelocationValue(RE);
209     section_iterator TargetSI = getSectionByAddress(*MachO, SymbolBaseAddr);
210     assert(TargetSI != MachO->section_end() && "Can't find section for symbol");
211     uint64_t SectionBaseAddr = TargetSI->getAddress();
212     SectionRef TargetSection = *TargetSI;
213     bool IsCode = TargetSection.isText();
214     uint32_t TargetSectionID =
215         findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID);
216
217     Addend -= SectionBaseAddr;
218     RelocationEntry R(SectionID, Offset, RelocType, Addend, IsPCRel, Size);
219
220     addRelocationForSection(R, TargetSectionID);
221
222     return ++RelI;
223   }
224
225   // Populate stubs in __jump_table section.
226   void populateJumpTable(MachOObjectFile &Obj, const SectionRef &JTSection,
227                          unsigned JTSectionID) {
228     assert(!Obj.is64Bit() &&
229            "__jump_table section not supported in 64-bit MachO.");
230
231     MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
232     MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
233     uint32_t JTSectionSize = Sec32.size;
234     unsigned FirstIndirectSymbol = Sec32.reserved1;
235     unsigned JTEntrySize = Sec32.reserved2;
236     unsigned NumJTEntries = JTSectionSize / JTEntrySize;
237     uint8_t *JTSectionAddr = getSectionAddress(JTSectionID);
238     unsigned JTEntryOffset = 0;
239
240     assert((JTSectionSize % JTEntrySize) == 0 &&
241            "Jump-table section does not contain a whole number of stubs?");
242
243     for (unsigned i = 0; i < NumJTEntries; ++i) {
244       unsigned SymbolIndex =
245           Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
246       symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
247       StringRef IndirectSymbolName;
248       SI->getName(IndirectSymbolName);
249       uint8_t *JTEntryAddr = JTSectionAddr + JTEntryOffset;
250       createStubFunction(JTEntryAddr);
251       RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
252                          MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
253       addRelocationForSymbol(RE, IndirectSymbolName);
254       JTEntryOffset += JTEntrySize;
255     }
256   }
257
258 };
259 }
260
261 #undef DEBUG_TYPE
262
263 #endif