RelocationValueRef RuntimeDyldMachO::getRelocationValueRef( const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID) { const MachOObjectFile &Obj = static_cast<const MachOObjectFile &>(BaseTObj); MachO::any_relocation_info RelInfo = Obj.getRelocation(RI->getRawDataRefImpl()); RelocationValueRef Value; bool IsExternal = Obj.getPlainRelocationExternal(RelInfo); if (IsExternal) { symbol_iterator Symbol = RI->getSymbol(); StringRef TargetName; Symbol->getName(TargetName); SymbolTableMap::const_iterator SI = GlobalSymbolTable.find(TargetName.data()); if (SI != GlobalSymbolTable.end()) { Value.SectionID = SI->second.first; Value.Offset = SI->second.second + RE.Addend; } else { Value.SymbolName = TargetName.data(); Value.Offset = RE.Addend; } } else { SectionRef Sec = Obj.getRelocationSection(RelInfo); bool IsCode = Sec.isText(); Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID); uint64_t Addr = Sec.getAddress(); Value.Offset = RE.Addend - Addr; } return Value; }
MCMachObjectSymbolizer::MCMachObjectSymbolizer( MCContext &Ctx, std::unique_ptr<MCRelocationInfo> &RelInfo, const MachOObjectFile *MOOF) : MCObjectSymbolizer(Ctx, RelInfo, MOOF), MOOF(MOOF), StubsStart(0), StubsCount(0), StubSize(0), StubsIndSymIndex(0) { for (const SectionRef &Section : MOOF->sections()) { StringRef Name; Section.getName(Name); if (Name == "__stubs") { SectionRef StubsSec = Section; if (MOOF->is64Bit()) { MachO::section_64 S = MOOF->getSection64(StubsSec.getRawDataRefImpl()); StubsIndSymIndex = S.reserved1; StubSize = S.reserved2; } else { MachO::section S = MOOF->getSection(StubsSec.getRawDataRefImpl()); StubsIndSymIndex = S.reserved1; StubSize = S.reserved2; } assert(StubSize && "Mach-O stub entry size can't be zero!"); StubsSec.getAddress(StubsStart); StubsSec.getSize(StubsCount); StubsCount /= StubSize; } } }
explicit section(const SectionRef& sec) { StringRef name; if(error_code err = sec.getName(name)) llvm_binary_fail(err); this->name_ = name.str(); if (error_code err = sec.getAddress(this->addr_)) llvm_binary_fail(err); if (error_code err = sec.getSize(this->size_)) llvm_binary_fail(err); }
/// Given a relocation from __compact_unwind, consisting of the RelocationRef /// and data being relocated, determine the best base Name and Addend to use for /// display purposes. /// /// 1. An Extern relocation will directly reference a symbol (and the data is /// then already an addend), so use that. /// 2. Otherwise the data is an offset in the object file's layout; try to find // a symbol before it in the same section, and use the offset from there. /// 3. Finally, if all that fails, fall back to an offset from the start of the /// referenced section. static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, std::map<uint64_t, SymbolRef> &Symbols, const RelocationRef &Reloc, uint64_t Addr, StringRef &Name, uint64_t &Addend) { if (Reloc.getSymbol() != Obj->symbol_end()) { Reloc.getSymbol()->getName(Name); Addend = Addr; return; } auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl()); SectionRef RelocSection = Obj->getRelocationSection(RE); uint64_t SectionAddr; RelocSection.getAddress(SectionAddr); auto Sym = Symbols.upper_bound(Addr); if (Sym == Symbols.begin()) { // The first symbol in the object is after this reference, the best we can // do is section-relative notation. RelocSection.getName(Name); Addend = Addr - SectionAddr; return; } // Go back one so that SymbolAddress <= Addr. --Sym; section_iterator SymSection = Obj->section_end(); Sym->second.getSection(SymSection); if (RelocSection == *SymSection) { // There's a valid symbol in the same section before this reference. Sym->second.getName(Name); Addend = Addr - Sym->first; return; } // There is a symbol before this reference, but it's in a different // section. Probably not helpful to mention it, so use the section name. RelocSection.getName(Name); Addend = Addr - SectionAddr; }
RelocationValueRef RuntimeDyldMachO::getRelocationValueRef( const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID) { const MachOObjectFile &Obj = static_cast<const MachOObjectFile &>(BaseTObj); MachO::any_relocation_info RelInfo = Obj.getRelocation(RI->getRawDataRefImpl()); RelocationValueRef Value; bool IsExternal = Obj.getPlainRelocationExternal(RelInfo); if (IsExternal) { symbol_iterator Symbol = RI->getSymbol(); ErrorOr<StringRef> TargetNameOrErr = Symbol->getName(); if (std::error_code EC = TargetNameOrErr.getError()) report_fatal_error(EC.message()); StringRef TargetName = *TargetNameOrErr; RTDyldSymbolTable::const_iterator SI = GlobalSymbolTable.find(TargetName.data()); if (SI != GlobalSymbolTable.end()) { const auto &SymInfo = SI->second; Value.SectionID = SymInfo.getSectionID(); Value.Offset = SymInfo.getOffset() + RE.Addend; } else { Value.SymbolName = TargetName.data(); Value.Offset = RE.Addend; } } else { SectionRef Sec = Obj.getAnyRelocationSection(RelInfo); bool IsCode = Sec.isText(); Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID); uint64_t Addr = Sec.getAddress(); Value.Offset = RE.Addend - Addr; } return Value; }
void RuntimeDyldMachO::processRelocationRef(unsigned SectionID, RelocationRef RelI, ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols, StubMap &Stubs) { const ObjectFile *OF = Obj.getObjectFile(); const MachOObjectFile *MachO = static_cast<const MachOObjectFile*>(OF); MachO::any_relocation_info RE= MachO->getRelocation(RelI.getRawDataRefImpl()); uint32_t RelType = MachO->getAnyRelocationType(RE); // FIXME: Properly handle scattered relocations. // For now, optimistically skip these: they can often be ignored, as // the static linker will already have applied the relocation, and it // only needs to be reapplied if symbols move relative to one another. // Note: This will fail horribly where the relocations *do* need to be // applied, but that was already the case. if (MachO->isRelocationScattered(RE)) return; RelocationValueRef Value; SectionEntry &Section = Sections[SectionID]; bool isExtern = MachO->getPlainRelocationExternal(RE); bool IsPCRel = MachO->getAnyRelocationPCRel(RE); unsigned Size = MachO->getAnyRelocationLength(RE); uint64_t Offset; RelI.getOffset(Offset); uint8_t *LocalAddress = Section.Address + Offset; unsigned NumBytes = 1 << Size; uint64_t Addend = 0; memcpy(&Addend, LocalAddress, NumBytes); if (isExtern) { // Obtain the symbol name which is referenced in the relocation symbol_iterator Symbol = RelI.getSymbol(); StringRef TargetName; Symbol->getName(TargetName); // First search for the symbol in the local symbol table SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data()); if (lsi != Symbols.end()) { Value.SectionID = lsi->second.first; Value.Addend = lsi->second.second + Addend; } else { // Search for the symbol in the global symbol table SymbolTableMap::const_iterator gsi = GlobalSymbolTable.find(TargetName.data()); if (gsi != GlobalSymbolTable.end()) { Value.SectionID = gsi->second.first; Value.Addend = gsi->second.second + Addend; } else { Value.SymbolName = TargetName.data(); Value.Addend = Addend; } } } else { SectionRef Sec = MachO->getRelocationSection(RE); bool IsCode = false; Sec.isText(IsCode); Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID); uint64_t Addr; Sec.getAddress(Addr); Value.Addend = Addend - Addr; if (IsPCRel) Value.Addend += Offset + NumBytes; } if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT || RelType == MachO::X86_64_RELOC_GOT_LOAD)) { assert(IsPCRel); assert(Size == 2); StubMap::const_iterator i = Stubs.find(Value); uint8_t *Addr; if (i != Stubs.end()) { Addr = Section.Address + i->second; } else { Stubs[Value] = Section.StubOffset; uint8_t *GOTEntry = Section.Address + Section.StubOffset; RelocationEntry RE(SectionID, Section.StubOffset, MachO::X86_64_RELOC_UNSIGNED, 0, false, 3); if (Value.SymbolName) addRelocationForSymbol(RE, Value.SymbolName); else addRelocationForSection(RE, Value.SectionID); Section.StubOffset += 8; Addr = GOTEntry; } resolveRelocation(Section, Offset, (uint64_t)Addr, MachO::X86_64_RELOC_UNSIGNED, Value.Addend, true, 2); } else if (Arch == Triple::arm && (RelType & 0xf) == MachO::ARM_RELOC_BR24) { // This is an ARM branch relocation, need to use a stub function. // Look up for existing stub. StubMap::const_iterator i = Stubs.find(Value); if (i != Stubs.end()) resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second, RelType, 0, IsPCRel, Size); else { // Create a new stub function. Stubs[Value] = Section.StubOffset; uint8_t *StubTargetAddr = createStubFunction(Section.Address + Section.StubOffset); RelocationEntry RE(SectionID, StubTargetAddr - Section.Address, MachO::GENERIC_RELOC_VANILLA, Value.Addend); if (Value.SymbolName) addRelocationForSymbol(RE, Value.SymbolName); else addRelocationForSection(RE, Value.SectionID); resolveRelocation(Section, Offset, (uint64_t)Section.Address + Section.StubOffset, RelType, 0, IsPCRel, Size); Section.StubOffset += getMaxStubSize(); } } else { RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, IsPCRel, Size); if (Value.SymbolName) addRelocationForSymbol(RE, Value.SymbolName); else addRelocationForSection(RE, Value.SectionID); } }
bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF, const SectionRef &Section, uint64_t FunctionAddress, uint64_t VA) { ArrayRef<uint8_t> Contents; if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents)) return false; uint64_t SectionVA = Section.getAddress(); uint64_t Offset = VA - SectionVA; const ulittle32_t *Data = reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset); const ExceptionDataRecord XData(Data); DictScope XRS(SW, "ExceptionData"); SW.printNumber("FunctionLength", XData.FunctionLength() << 1); SW.printNumber("Version", XData.Vers()); SW.printBoolean("ExceptionData", XData.X()); SW.printBoolean("EpiloguePacked", XData.E()); SW.printBoolean("Fragment", XData.F()); SW.printNumber(XData.E() ? "EpilogueOffset" : "EpilogueScopes", XData.EpilogueCount()); SW.printNumber("ByteCodeLength", static_cast<uint64_t>(XData.CodeWords() * sizeof(uint32_t))); if (XData.E()) { ArrayRef<uint8_t> UC = XData.UnwindByteCode(); if (!XData.F()) { ListScope PS(SW, "Prologue"); decodeOpcodes(UC, 0, /*Prologue=*/true); } if (XData.EpilogueCount()) { ListScope ES(SW, "Epilogue"); decodeOpcodes(UC, XData.EpilogueCount(), /*Prologue=*/false); } } else { ArrayRef<ulittle32_t> EpilogueScopes = XData.EpilogueScopes(); ListScope ESS(SW, "EpilogueScopes"); for (const EpilogueScope ES : EpilogueScopes) { DictScope ESES(SW, "EpilogueScope"); SW.printNumber("StartOffset", ES.EpilogueStartOffset()); SW.printNumber("Condition", ES.Condition()); SW.printNumber("EpilogueStartIndex", ES.EpilogueStartIndex()); ListScope Opcodes(SW, "Opcodes"); decodeOpcodes(XData.UnwindByteCode(), ES.EpilogueStartIndex(), /*Prologue=*/false); } } if (XData.X()) { const uint32_t Address = XData.ExceptionHandlerRVA(); const uint32_t Parameter = XData.ExceptionHandlerParameter(); const size_t HandlerOffset = HeaderWords(XData) + (XData.E() ? 0 : XData.EpilogueCount()) + XData.CodeWords(); ErrorOr<SymbolRef> Symbol = getRelocatedSymbol(COFF, Section, HandlerOffset * sizeof(uint32_t)); if (!Symbol) Symbol = getSymbol(COFF, Address, /*FunctionOnly=*/true); StringRef Name; if (Symbol) Symbol->getName(Name); ListScope EHS(SW, "ExceptionHandler"); SW.printString("Routine", formatSymbol(Name, Address)); SW.printHex("Parameter", Parameter); } return true; }
uint64_t getAddr(const SectionRef &sec) { return sec.getAddress(); }
static bool SectionStartsBefore(const SectionRef &S, uint64_t Addr) { uint64_t SAddr; S.getAddress(SAddr); return SAddr < Addr; }