void BinMapOutput::OutputSymbols(const Section* sect) { for (Object::const_symbol_iterator sym = m_object.symbols_begin(), end = m_object.symbols_end(); sym != end; ++sym) { const std::string& name = sym->getName(); const Expr* equ; Location loc; if (sect == 0 && (equ = sym->getEqu())) { std::auto_ptr<Expr> realequ(equ->clone()); realequ->Simplify(m_diags); BinSimplify(*realequ); realequ->Simplify(m_diags); OutputIntNum(realequ->getIntNum()); m_os << " " << name << '\n'; } else if (sym->getLabel(&loc) && loc.bc->getContainer() == sect) { // Real address OutputIntNum(sect->getLMA() + loc.getOffset()); m_os << " "; // Virtual address OutputIntNum(sect->getVMA() + loc.getOffset()); // Name m_os << " " << name << '\n'; } } }
static unsigned long CountSymbols(const Object& object, const Section* sect) { unsigned long count = 0; for (Object::const_symbol_iterator sym = object.symbols_begin(), end = object.symbols_end(); sym != end; ++sym) { Location loc; // TODO: autodetect wider size if ((sect == 0 && sym->getEqu()) || (sym->getLabel(&loc) && loc.bc->getContainer() == sect)) count++; } return count; }
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; } }