static bool collectRelocatedSymbols(const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress, uint64_t SymAddress, uint64_t SymSize, StringRef *I, StringRef *E) { uint64_t SymOffset = SymAddress - SecAddress; uint64_t SymEnd = SymOffset + SymSize; for (const SectionRef &SR : getRelocSections(Obj, Sec)) { for (const object::RelocationRef &Reloc : SR.relocations()) { if (I == E) break; const object::symbol_iterator RelocSymI = Reloc.getSymbol(); if (RelocSymI == Obj->symbol_end()) continue; StringRef RelocSymName; if (error(RelocSymI->getName(RelocSymName))) return true; uint64_t Offset; if (error(Reloc.getOffset(Offset))) return true; if (Offset >= SymOffset && Offset < SymEnd) { *I = RelocSymName; ++I; } } } return false; }
static bool collectRelocationOffsets( const ObjectFile *Obj, object::section_iterator SecI, StringRef SymName, std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) { for (const object::RelocationRef &Reloc : SecI->relocations()) { const object::symbol_iterator RelocSymI = Reloc.getSymbol(); if (RelocSymI == Obj->symbol_end()) continue; StringRef RelocSymName; if (error(RelocSymI->getName(RelocSymName))) return true; uint64_t Offset; if (error(Reloc.getOffset(Offset))) return true; Collection[std::make_pair(SymName, Offset)] = RelocSymName; } return false; }
static bool collectRelocatedSymbols(const ObjectFile *Obj, object::section_iterator SecI, StringRef *I, StringRef *E) { for (const object::RelocationRef &Reloc : SecI->relocations()) { if (I == E) break; const object::symbol_iterator RelocSymI = Reloc.getSymbol(); if (RelocSymI == Obj->symbol_end()) continue; StringRef RelocSymName; if (error(RelocSymI->getName(RelocSymName))) return true; *I = RelocSymName; ++I; } return false; }
static void collectRelocationOffsets( const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress, uint64_t SymAddress, uint64_t SymSize, StringRef SymName, std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) { uint64_t SymOffset = SymAddress - SecAddress; uint64_t SymEnd = SymOffset + SymSize; for (const SectionRef &SR : SectionRelocMap[Sec]) { for (const object::RelocationRef &Reloc : SR.relocations()) { const object::symbol_iterator RelocSymI = Reloc.getSymbol(); if (RelocSymI == Obj->symbol_end()) continue; ErrorOr<StringRef> RelocSymName = RelocSymI->getName(); error(RelocSymName.getError()); uint64_t Offset = Reloc.getOffset(); if (Offset >= SymOffset && Offset < SymEnd) Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName; } } }
static void collectRelocatedSymbols(const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress, uint64_t SymAddress, uint64_t SymSize, StringRef *I, StringRef *E) { uint64_t SymOffset = SymAddress - SecAddress; uint64_t SymEnd = SymOffset + SymSize; for (const SectionRef &SR : SectionRelocMap[Sec]) { for (const object::RelocationRef &Reloc : SR.relocations()) { if (I == E) break; const object::symbol_iterator RelocSymI = Reloc.getSymbol(); if (RelocSymI == Obj->symbol_end()) continue; ErrorOr<StringRef> RelocSymName = RelocSymI->getName(); error(RelocSymName.getError()); uint64_t Offset = Reloc.getOffset(); if (Offset >= SymOffset && Offset < SymEnd) { *I = *RelocSymName; ++I; } } } }
static bool collectRelocationOffsets( const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress, uint64_t SymAddress, uint64_t SymSize, StringRef SymName, std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) { uint64_t SymOffset = SymAddress - SecAddress; uint64_t SymEnd = SymOffset + SymSize; for (const SectionRef &SR : getRelocSections(Obj, Sec)) { for (const object::RelocationRef &Reloc : SR.relocations()) { const object::symbol_iterator RelocSymI = Reloc.getSymbol(); if (RelocSymI == Obj->symbol_end()) continue; StringRef RelocSymName; if (error(RelocSymI->getName(RelocSymName))) return true; uint64_t Offset; if (error(Reloc.getOffset(Offset))) return true; if (Offset >= SymOffset && Offset < SymEnd) Collection[std::make_pair(SymName, Offset - SymOffset)] = RelocSymName; } } return false; }
static void dumpVTables(const ObjectFile *Obj) { std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries; std::map<StringRef, ArrayRef<aligned_little32_t>> VBTables; for (const object::SymbolRef &Sym : Obj->symbols()) { StringRef SymName; if (error(Sym.getName(SymName))) return; // VFTables in the MS-ABI start with '??_7' and are contained within their // own COMDAT section. We then determine the contents of the VFTable by // looking at each relocation in the section. if (SymName.startswith("??_7")) { object::section_iterator SecI(Obj->section_begin()); if (error(Sym.getSection(SecI))) return; if (SecI == Obj->section_end()) continue; // Each relocation either names a virtual method or a thunk. We note the // offset into the section and the symbol used for the relocation. for (const object::RelocationRef &Reloc : SecI->relocations()) { const object::symbol_iterator RelocSymI = Reloc.getSymbol(); if (RelocSymI == Obj->symbol_end()) continue; StringRef RelocSymName; if (error(RelocSymI->getName(RelocSymName))) return; uint64_t Offset; if (error(Reloc.getOffset(Offset))) return; VFTableEntries[std::make_pair(SymName, Offset)] = RelocSymName; } } // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit // offsets of virtual bases. else if (SymName.startswith("??_8")) { object::section_iterator SecI(Obj->section_begin()); if (error(Sym.getSection(SecI))) return; if (SecI == Obj->section_end()) continue; StringRef SecContents; if (error(SecI->getContents(SecContents))) return; ArrayRef<aligned_little32_t> VBTableData( reinterpret_cast<const aligned_little32_t *>(SecContents.data()), SecContents.size() / sizeof(aligned_little32_t)); VBTables[SymName] = VBTableData; } } for ( const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VFTableEntry : VFTableEntries) { StringRef VFTableName = VFTableEntry.first.first; uint64_t Offset = VFTableEntry.first.second; StringRef SymName = VFTableEntry.second; outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n'; } for (const std::pair<StringRef, ArrayRef<aligned_little32_t>> &VBTable : VBTables) { StringRef VBTableName = VBTable.first; uint32_t Idx = 0; for (aligned_little32_t Offset : VBTable.second) { outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n'; Idx += sizeof(Offset); } } }
void XdfObject::Output(llvm::raw_fd_ostream& os, bool all_syms, DebugFormat& dbgfmt, Diagnostic& diags) { all_syms = true; // force all syms into symbol table // Get number of symbols and set symbol index in symbol data. unsigned long symtab_count = 0; for (Object::symbol_iterator sym = m_object.symbols_begin(), end = m_object.symbols_end(); sym != end; ++sym) { int vis = sym->getVisibility(); if (vis & Symbol::COMMON) { diags.Report(sym->getDeclSource(), diag::err_xdf_common_unsupported); continue; } if (all_syms || (vis != Symbol::LOCAL && !(vis & Symbol::DLOCAL))) { // Save index in symrec data sym->AddAssocData( std::auto_ptr<XdfSymbol>(new XdfSymbol(symtab_count))); ++symtab_count; } } // Number sections long scnum = 0; for (Object::section_iterator i = m_object.sections_begin(), end = m_object.sections_end(); i != end; ++i) { XdfSection* xsect = i->getAssocData<XdfSection>(); assert(xsect != 0); xsect->scnum = scnum++; } // Allocate space for headers by seeking forward os.seek(FILEHEAD_SIZE+SECTHEAD_SIZE*scnum); if (os.has_error()) { diags.Report(SourceLocation(), diag::err_file_output_seek); return; } XdfOutput out(os, m_object, diags); // Get file offset of start of string table unsigned long strtab_offset = FILEHEAD_SIZE + SECTHEAD_SIZE*scnum + SYMBOL_SIZE*symtab_count; // Output symbol table for (Object::const_symbol_iterator sym = m_object.symbols_begin(), end = m_object.symbols_end(); sym != end; ++sym) { out.OutputSymbol(*sym, all_syms, &strtab_offset); } // Output string table for (Object::const_symbol_iterator sym = m_object.symbols_begin(), end = m_object.symbols_end(); sym != end; ++sym) { if (all_syms || sym->getVisibility() != Symbol::LOCAL) os << sym->getName() << '\0'; } // Output section data/relocs for (Object::section_iterator i=m_object.sections_begin(), end=m_object.sections_end(); i != end; ++i) { out.OutputSection(*i); } // Write headers os.seek(0); if (os.has_error()) { diags.Report(SourceLocation(), diag::err_file_output_seek); return; } // Output object header Bytes& scratch = out.getScratch(); scratch.setLittleEndian(); Write32(scratch, XDF_MAGIC); // magic number Write32(scratch, scnum); // number of sects Write32(scratch, symtab_count); // number of symtabs // size of sect headers + symbol table + strings Write32(scratch, strtab_offset-FILEHEAD_SIZE); assert(scratch.size() == FILEHEAD_SIZE); os << scratch; // Output section headers for (Object::const_section_iterator i=m_object.sections_begin(), end=m_object.sections_end(); i != end; ++i) { const XdfSection* xsect = i->getAssocData<XdfSection>(); assert(xsect != 0); Bytes& scratch2 = out.getScratch(); xsect->Write(scratch2, *i); assert(scratch2.size() == SECTHEAD_SIZE); os << scratch2; } }