Index: include/llvm/Object/ELFObjectFile.h =================================================================== --- include/llvm/Object/ELFObjectFile.h +++ include/llvm/Object/ELFObjectFile.h @@ -31,6 +31,7 @@ #include #include #include +#include namespace llvm { namespace object { @@ -205,6 +206,8 @@ section_iterator getSymbolSection(const Elf_Sym *Symb) const; std::error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const override; + std::error_code getSymbolSectionName(DataRefImpl Symb, + std::string &Result) const override; void moveSectionNext(DataRefImpl &Sec) const override; std::error_code getSectionName(DataRefImpl Sec, @@ -525,7 +528,53 @@ return std::error_code(); } +namespace { + std::string NameFromPRCNumber(uint32_t Index) { + std::stringstream Name; + Name << "PRC[" << std::hex << Index << "]"; + return Name.str(); + } +} + template +std::error_code +ELFObjectFile::getSymbolSectionName(DataRefImpl Symb, + std::string &Result) const { + auto Symbol = getSymbol(Symb); + uint32_t Index = Symbol->st_shndx; + if(Index < ELF::SHN_LOPROC || Index > ELF::SHN_HIPROC) + return ObjectFile::getSymbolSectionName(Symb, Result); + switch (EF.getHeader()->e_machine) { + case ELF::EM_HEXAGON: + switch(Index) { + case ELF::SHN_HEXAGON_SCOMMON: + Result = ".scommon"; + break; + case ELF::SHN_HEXAGON_SCOMMON_1: + Result = ".scommon.1"; + break; + case ELF::SHN_HEXAGON_SCOMMON_2: + Result = ".scommon.2"; + break; + case ELF::SHN_HEXAGON_SCOMMON_4: + Result = ".scommon.4"; + break; + case ELF::SHN_HEXAGON_SCOMMON_8: + Result = ".scommon.8"; + break; + default: + Result = NameFromPRCNumber(Index); + break; + } + break; + default: + Result = NameFromPRCNumber(Index); + break; + } + return std::error_code(); +} + +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,9 @@ uint32_t getSymbolFlags(DataRefImpl Symb) const override; std::error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const override; + std::error_code getSymbolSectionName(DataRefImpl Symb, + std::string &Res) 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 + std::error_code getSectionName(std::string &Result) const; const ObjectFile *getObject() const; }; @@ -204,6 +207,8 @@ virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0; virtual std::error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const = 0; + virtual std::error_code getSymbolSectionName(DataRefImpl Symb, + std::string &Res) const; // Same as above for SectionRef. friend class SectionRef; @@ -325,6 +330,10 @@ inline std::error_code SymbolRef::getSection(section_iterator &Result) const { return getObject()->getSymbolSection(getRawDataRefImpl(), Result); +} + +inline std::error_code SymbolRef::getSectionName(std::string &Result) const { + return getObject()->getSymbolSectionName(getRawDataRefImpl(), Result); } inline SymbolRef::Type SymbolRef::getType() const { Index: lib/Object/MachOObjectFile.cpp =================================================================== --- lib/Object/MachOObjectFile.cpp +++ lib/Object/MachOObjectFile.cpp @@ -467,6 +467,22 @@ MachO::nlist_base Entry = getSymbolTableEntryBase(this, Sym.getRawDataRefImpl()); return Entry.n_sect - 1; +} + +std::error_code MachOObjectFile::getSymbolSectionName(DataRefImpl Symb, + std::string &Res) const { + std::error_code Result = ObjectFile::getSymbolSectionName(Symb, Res); + if(Result) + return Result; + section_iterator Section = section_end(); + Result = getSymbolSection(Symb, Section); + if(Result || Section == section_end()) + // Section doesn't existing in file e.g. proc-specific + return std::error_code(); + DataRefImpl DR = Section->getRawDataRefImpl(); + StringRef SegmentName = getSectionFinalSegmentName(DR); + Res = SegmentName.str() + "," + Res; + return std::error_code(); } void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const { Index: lib/Object/ObjectFile.cpp =================================================================== --- lib/Object/ObjectFile.cpp +++ lib/Object/ObjectFile.cpp @@ -53,7 +53,31 @@ return std::error_code(); } -uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; } +uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; } + +std::error_code ObjectFile::getSymbolSectionName(DataRefImpl Symb, + std::string &Res) 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) + Res = "*ABS*"; + else if (Common) + Res = "*COM*"; + else if(Section != section_end()) { + StringRef Name; + auto Error = Section->getName(Name); + Res = Name; + return Error; + } + else + Res = "*UND*"; + return std::error_code(); +} 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; } + std::string SectionName; + error(Symbol.getSectionName(SectionName)); 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 =