Index: include/llvm/Object/ELFObjectFile.h =================================================================== --- include/llvm/Object/ELFObjectFile.h +++ include/llvm/Object/ELFObjectFile.h @@ -55,6 +55,7 @@ virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0; virtual ErrorOr getRelocationAddend(DataRefImpl Rel) const = 0; + std::string NameFromPRCNumber(uint32_t Index) const; public: typedef iterator_range elf_symbol_iterator_range; @@ -205,6 +206,7 @@ section_iterator getSymbolSection(const Elf_Sym *Symb) const; std::error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const override; + ErrorOr getSymbolSectionName(DataRefImpl Symb) const override; void moveSectionNext(DataRefImpl &Sec) const override; std::error_code getSectionName(DataRefImpl Sec, @@ -526,6 +528,34 @@ } template +ErrorOr +ELFObjectFile::getSymbolSectionName(DataRefImpl Symb) const { + auto Symbol = getSymbol(Symb); + uint32_t Index = Symbol->st_shndx; + if(Index < ELF::SHN_LOPROC || Index > ELF::SHN_HIPROC) + return ObjectFile::getSymbolSectionName(Symb); + switch (EF.getHeader()->e_machine) { + case ELF::EM_HEXAGON: + switch(Index) { + case ELF::SHN_HEXAGON_SCOMMON: + return std::string(".scommon"); + case ELF::SHN_HEXAGON_SCOMMON_1: + return std::string(".scommon.1"); + case ELF::SHN_HEXAGON_SCOMMON_2: + return std::string(".scommon.2"); + case ELF::SHN_HEXAGON_SCOMMON_4: + return std::string(".scommon.4"); + case ELF::SHN_HEXAGON_SCOMMON_8: + return std::string(".scommon.8"); + default: + return NameFromPRCNumber(Index); + } + default: + return NameFromPRCNumber(Index); + } +} + +template void ELFObjectFile::moveSectionNext(DataRefImpl &Sec) const { const Elf_Shdr *ESec = toELFShdrIter(Sec); Sec = toDRI(++ESec); Index: include/llvm/Object/MachO.h =================================================================== --- include/llvm/Object/MachO.h +++ include/llvm/Object/MachO.h @@ -212,6 +212,8 @@ uint32_t getSymbolFlags(DataRefImpl Symb) const override; std::error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const override; + ErrorOr getSymbolSectionName(DataRefImpl Symb) const override; + unsigned getSymbolSectionID(SymbolRef Symb) const; unsigned getSectionID(SectionRef Sec) const; Index: include/llvm/Object/ObjectFile.h =================================================================== --- include/llvm/Object/ObjectFile.h +++ include/llvm/Object/ObjectFile.h @@ -148,6 +148,9 @@ /// @brief Get section this symbol is defined in reference to. Result is /// end_sections() if it is undefined or is an absolute symbol. std::error_code getSection(section_iterator &Result) const; + /// Return the name of the section this symbol belongs to + /// Allows for named processor specific sections with no file representation + ErrorOr getSectionName() const; const ObjectFile *getObject() const; }; @@ -204,6 +207,7 @@ virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0; virtual std::error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const = 0; + virtual ErrorOr getSymbolSectionName(DataRefImpl Symb) const; // Same as above for SectionRef. friend class SectionRef; @@ -325,6 +329,10 @@ inline std::error_code SymbolRef::getSection(section_iterator &Result) const { return getObject()->getSymbolSection(getRawDataRefImpl(), Result); +} + +inline ErrorOr SymbolRef::getSectionName() const { + return getObject()->getSymbolSectionName(getRawDataRefImpl()); } inline SymbolRef::Type SymbolRef::getType() const { Index: lib/Object/ELFObjectFile.cpp =================================================================== --- lib/Object/ELFObjectFile.cpp +++ lib/Object/ELFObjectFile.cpp @@ -13,6 +13,7 @@ #include "llvm/Object/ELFObjectFile.h" #include "llvm/Support/MathExtras.h" +#include "llvm/Support/raw_ostream.h" namespace llvm { using namespace object; @@ -55,4 +56,15 @@ return std::move(R); } +std::string ELFObjectFileBase::NameFromPRCNumber(uint32_t Index) const { + std::string Name; + { + llvm::raw_string_ostream Stream(Name); + Stream << "PRC["; + Stream.write_hex(Index); + Stream << "]"; + } + return Name; +} + } // end namespace llvm Index: lib/Object/MachOObjectFile.cpp =================================================================== --- lib/Object/MachOObjectFile.cpp +++ lib/Object/MachOObjectFile.cpp @@ -467,6 +467,20 @@ MachO::nlist_base Entry = getSymbolTableEntryBase(this, Sym.getRawDataRefImpl()); return Entry.n_sect - 1; +} + +ErrorOr MachOObjectFile::getSymbolSectionName(DataRefImpl Symb) const { + auto Result = ObjectFile::getSymbolSectionName(Symb); + if(!Result) + return Result; + section_iterator Section = section_end(); + auto Error = getSymbolSection(Symb, Section); + if(Error || Section == section_end()) + // Section doesn't existing in file e.g. proc-specific + return Result; + DataRefImpl DR = Section->getRawDataRefImpl(); + StringRef SegmentName = getSectionFinalSegmentName(DR); + return SegmentName.str() + "," + *Result; } void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const { Index: lib/Object/ObjectFile.cpp =================================================================== --- lib/Object/ObjectFile.cpp +++ lib/Object/ObjectFile.cpp @@ -53,7 +53,30 @@ return std::error_code(); } -uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; } +uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; } + +ErrorOr ObjectFile::getSymbolSectionName(DataRefImpl Symb) const { + section_iterator Section = section_end(); + std::error_code Error = getSymbolSection(Symb, Section); + uint32_t Flags = getSymbolFlags(Symb); + bool Absolute = Flags & SymbolRef::SF_Absolute; + bool Common = Flags & SymbolRef::SF_Common; + if(Error) + return Error; + else if (Absolute) + return std::string("*ABS*"); + else if (Common) + return std::string("*COM*"); + else if(Section != section_end()) { + StringRef Name; + auto Error = Section->getName(Name); + if(Error) + return Error; + return Name.str(); + } + else + return std::string("*UND*"); +} section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const { return section_iterator(SectionRef(Sec, this)); Index: tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- tools/llvm-objdump/llvm-objdump.cpp +++ tools/llvm-objdump/llvm-objdump.cpp @@ -1235,10 +1235,11 @@ error(NameOrErr.getError()); Name = *NameOrErr; } + ErrorOr SectionName = Symbol.getSectionName(); + error(SectionName.getError()); bool Global = Flags & SymbolRef::SF_Global; bool Weak = Flags & SymbolRef::SF_Weak; - bool Absolute = Flags & SymbolRef::SF_Absolute; bool Common = Flags & SymbolRef::SF_Common; bool Hidden = Flags & SymbolRef::SF_Hidden; @@ -1265,24 +1266,7 @@ << Debug // Debugging (d) or dynamic (D) symbol. << FileFunc // Name of function (F), file (f) or object (O). << ' '; - if (Absolute) { - outs() << "*ABS*"; - } else if (Common) { - outs() << "*COM*"; - } else if (Section == o->section_end()) { - outs() << "*UND*"; - } else { - if (const MachOObjectFile *MachO = - dyn_cast(o)) { - DataRefImpl DR = Section->getRawDataRefImpl(); - StringRef SegmentName = MachO->getSectionFinalSegmentName(DR); - outs() << SegmentName << ","; - } - StringRef SectionName; - error(Section->getName(SectionName)); - outs() << SectionName; - } - + outs() << *SectionName; outs() << '\t'; if (Common || isa(o)) { uint64_t Val =