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 processI386ScatteredVANILLA(SectionID, RelI, Obj, 51 ObjSectionToID); 52 llvm_unreachable("Unhandled scattered relocation."); 53 } 54 55 RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI)); 56 RE.Addend = memcpyAddend(RE); 57 RelocationValueRef Value( 58 getRelocationValueRef(Obj, RelI, RE, ObjSectionToID)); 59 60 // Addends for external, PC-rel relocations on i386 point back to the zero 61 // offset. Calculate the final offset from the relocation target instead. 62 // This allows us to use the same logic for both external and internal 63 // relocations in resolveI386RelocationRef. 64 // bool IsExtern = Obj.getPlainRelocationExternal(RelInfo); 65 // if (IsExtern && RE.IsPCRel) { 66 // uint64_t RelocAddr = 0; 67 // RelI->getAddress(RelocAddr); 68 // Value.Addend += RelocAddr + 4; 69 // } 70 if (RE.IsPCRel) 71 makeValueAddendPCRel(Value, Obj, RelI, 1 << RE.Size); 72 73 RE.Addend = Value.Offset; 74 75 if (Value.SymbolName) 76 addRelocationForSymbol(RE, Value.SymbolName); 77 else 78 addRelocationForSection(RE, Value.SectionID); 79 80 return ++RelI; 81 } 82 resolveRelocation(const RelocationEntry & RE,uint64_t Value)83 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { 84 DEBUG(dumpRelocationToResolve(RE, Value)); 85 86 const SectionEntry &Section = Sections[RE.SectionID]; 87 uint8_t *LocalAddress = Section.Address + RE.Offset; 88 89 if (RE.IsPCRel) { 90 uint64_t FinalAddress = Section.LoadAddress + RE.Offset; 91 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation. 92 } 93 94 switch (RE.RelType) { 95 default: 96 llvm_unreachable("Invalid relocation type!"); 97 case MachO::GENERIC_RELOC_VANILLA: 98 writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size); 99 break; 100 case MachO::GENERIC_RELOC_SECTDIFF: 101 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: { 102 uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress; 103 uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress; 104 assert((Value == SectionABase || Value == SectionBBase) && 105 "Unexpected SECTDIFF relocation value."); 106 Value = SectionABase - SectionBBase + RE.Addend; 107 writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size); 108 break; 109 } 110 case MachO::GENERIC_RELOC_PB_LA_PTR: 111 Error("Relocation type not implemented yet!"); 112 } 113 } 114 finalizeSection(const ObjectFile & Obj,unsigned SectionID,const SectionRef & Section)115 void finalizeSection(const ObjectFile &Obj, unsigned SectionID, 116 const SectionRef &Section) { 117 StringRef Name; 118 Section.getName(Name); 119 120 if (Name == "__jump_table") 121 populateJumpTable(cast<MachOObjectFile>(Obj), Section, SectionID); 122 else if (Name == "__pointers") 123 populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj), 124 Section, SectionID); 125 } 126 127 private: 128 relocation_iterator processSECTDIFFRelocation(unsigned SectionID,relocation_iterator RelI,const ObjectFile & BaseObjT,ObjSectionToIDMap & ObjSectionToID)129 processSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI, 130 const ObjectFile &BaseObjT, 131 ObjSectionToIDMap &ObjSectionToID) { 132 const MachOObjectFile &Obj = 133 static_cast<const MachOObjectFile&>(BaseObjT); 134 MachO::any_relocation_info RE = 135 Obj.getRelocation(RelI->getRawDataRefImpl()); 136 137 SectionEntry &Section = Sections[SectionID]; 138 uint32_t RelocType = Obj.getAnyRelocationType(RE); 139 bool IsPCRel = Obj.getAnyRelocationPCRel(RE); 140 unsigned Size = Obj.getAnyRelocationLength(RE); 141 uint64_t Offset; 142 RelI->getOffset(Offset); 143 uint8_t *LocalAddress = Section.Address + Offset; 144 unsigned NumBytes = 1 << Size; 145 uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes); 146 147 ++RelI; 148 MachO::any_relocation_info RE2 = 149 Obj.getRelocation(RelI->getRawDataRefImpl()); 150 151 uint32_t AddrA = Obj.getScatteredRelocationValue(RE); 152 section_iterator SAI = getSectionByAddress(Obj, AddrA); 153 assert(SAI != Obj.section_end() && "Can't find section for address A"); 154 uint64_t SectionABase = SAI->getAddress(); 155 uint64_t SectionAOffset = AddrA - SectionABase; 156 SectionRef SectionA = *SAI; 157 bool IsCode = SectionA.isText(); 158 uint32_t SectionAID = 159 findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID); 160 161 uint32_t AddrB = Obj.getScatteredRelocationValue(RE2); 162 section_iterator SBI = getSectionByAddress(Obj, AddrB); 163 assert(SBI != Obj.section_end() && "Can't find section for address B"); 164 uint64_t SectionBBase = SBI->getAddress(); 165 uint64_t SectionBOffset = AddrB - SectionBBase; 166 SectionRef SectionB = *SBI; 167 uint32_t SectionBID = 168 findOrEmitSection(Obj, SectionB, IsCode, ObjSectionToID); 169 170 if (Addend != AddrA - AddrB) 171 Error("Unexpected SECTDIFF relocation addend."); 172 173 DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB 174 << ", Addend: " << Addend << ", SectionA ID: " << SectionAID 175 << ", SectionAOffset: " << SectionAOffset 176 << ", SectionB ID: " << SectionBID 177 << ", SectionBOffset: " << SectionBOffset << "\n"); 178 RelocationEntry R(SectionID, Offset, RelocType, 0, SectionAID, 179 SectionAOffset, SectionBID, SectionBOffset, IsPCRel, 180 Size); 181 182 addRelocationForSection(R, SectionAID); 183 addRelocationForSection(R, SectionBID); 184 185 return ++RelI; 186 } 187 processI386ScatteredVANILLA(unsigned SectionID,relocation_iterator RelI,const ObjectFile & BaseObjT,RuntimeDyldMachO::ObjSectionToIDMap & ObjSectionToID)188 relocation_iterator processI386ScatteredVANILLA( 189 unsigned SectionID, relocation_iterator RelI, 190 const ObjectFile &BaseObjT, 191 RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID) { 192 const MachOObjectFile &Obj = 193 static_cast<const MachOObjectFile&>(BaseObjT); 194 MachO::any_relocation_info RE = 195 Obj.getRelocation(RelI->getRawDataRefImpl()); 196 197 SectionEntry &Section = Sections[SectionID]; 198 uint32_t RelocType = Obj.getAnyRelocationType(RE); 199 bool IsPCRel = Obj.getAnyRelocationPCRel(RE); 200 unsigned Size = Obj.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 = readBytesUnaligned(LocalAddress, NumBytes); 206 207 unsigned SymbolBaseAddr = Obj.getScatteredRelocationValue(RE); 208 section_iterator TargetSI = getSectionByAddress(Obj, SymbolBaseAddr); 209 assert(TargetSI != Obj.section_end() && "Can't find section for symbol"); 210 uint64_t SectionBaseAddr = TargetSI->getAddress(); 211 SectionRef TargetSection = *TargetSI; 212 bool IsCode = TargetSection.isText(); 213 uint32_t TargetSectionID = 214 findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID); 215 216 Addend -= SectionBaseAddr; 217 RelocationEntry R(SectionID, Offset, RelocType, Addend, IsPCRel, Size); 218 219 addRelocationForSection(R, TargetSectionID); 220 221 return ++RelI; 222 } 223 224 // Populate stubs in __jump_table section. populateJumpTable(const MachOObjectFile & Obj,const SectionRef & JTSection,unsigned JTSectionID)225 void populateJumpTable(const MachOObjectFile &Obj, const SectionRef &JTSection, 226 unsigned JTSectionID) { 227 assert(!Obj.is64Bit() && 228 "__jump_table section not supported in 64-bit MachO."); 229 230 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand(); 231 MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl()); 232 uint32_t JTSectionSize = Sec32.size; 233 unsigned FirstIndirectSymbol = Sec32.reserved1; 234 unsigned JTEntrySize = Sec32.reserved2; 235 unsigned NumJTEntries = JTSectionSize / JTEntrySize; 236 uint8_t *JTSectionAddr = getSectionAddress(JTSectionID); 237 unsigned JTEntryOffset = 0; 238 239 assert((JTSectionSize % JTEntrySize) == 0 && 240 "Jump-table section does not contain a whole number of stubs?"); 241 242 for (unsigned i = 0; i < NumJTEntries; ++i) { 243 unsigned SymbolIndex = 244 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i); 245 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex); 246 StringRef IndirectSymbolName; 247 SI->getName(IndirectSymbolName); 248 uint8_t *JTEntryAddr = JTSectionAddr + JTEntryOffset; 249 createStubFunction(JTEntryAddr); 250 RelocationEntry RE(JTSectionID, JTEntryOffset + 1, 251 MachO::GENERIC_RELOC_VANILLA, 0, true, 2); 252 addRelocationForSymbol(RE, IndirectSymbolName); 253 JTEntryOffset += JTEntrySize; 254 } 255 } 256 257 }; 258 } 259 260 #undef DEBUG_TYPE 261 262 #endif 263