diff options
| author | James Henderson <jh7370@my.bristol.ac.uk> | 2019-11-01 10:27:00 +0000 |
|---|---|---|
| committer | James Henderson <jh7370@my.bristol.ac.uk> | 2019-11-01 11:48:31 +0000 |
| commit | d835bc004af25d3431977047e13d1bbc5cef5b91 (patch) | |
| tree | bb36732afcb00de7228d314a294ea25e07740133 /llvm/tools/llvm-readobj/ELFDumper.cpp | |
| parent | 490f6f3211cb54d109a14be78520441b711af669 (diff) | |
| download | bcm5719-llvm-d835bc004af25d3431977047e13d1bbc5cef5b91.tar.gz bcm5719-llvm-d835bc004af25d3431977047e13d1bbc5cef5b91.zip | |
[NFC][llvm-readobj] Split getSectionIndexName function into two
getSectionIndexName was trying to fetch two things at once, which led to
a somewhat tricky to understand interface involving passing output
parameters in, and also made it hard to return Errors further up the
stack.
This change is in preparation for changing the error handling.
Additionally, update a related test now that yaml2obj supports
SHT_SYMTAB_SHNDX properly (see d3963051c490), and add missing LLVM-style
coverage for symbols with shndx SHN_XINDEX. This test (after fixing)
caught a mistake in my first attempt at this patch, hence I'm including
it as part of this patch.
Reviewed by: grimar, MaskRay
Differential Revision: https://reviews.llvm.org/D69670
Diffstat (limited to 'llvm/tools/llvm-readobj/ELFDumper.cpp')
| -rw-r--r-- | llvm/tools/llvm-readobj/ELFDumper.cpp | 84 |
1 files changed, 48 insertions, 36 deletions
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 667f09217cd..e48744b2f76 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -299,9 +299,10 @@ public: Elf_Relr_Range dyn_relrs() const; std::string getFullSymbolName(const Elf_Sym *Symbol, StringRef StrTable, bool IsDynamic) const; - void getSectionNameIndex(const Elf_Sym *Symbol, const Elf_Sym *FirstSym, - StringRef &SectionName, - unsigned &SectionIndex) const; + Expected<unsigned> getSymbolSectionIndex(const Elf_Sym *Symbol, + const Elf_Sym *FirstSym) const; + Expected<StringRef> getSymbolSectionName(const Elf_Sym *Symbol, + unsigned SectionIndex) const; Expected<std::string> getStaticSymbolName(uint32_t Index) const; std::string getDynamicString(uint64_t Value) const; StringRef getSymbolVersionByIndex(StringRef StrTab, @@ -821,12 +822,12 @@ std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym *Symbol, unwrapOrError(ObjF->getFileName(), Symbol->getName(StrTable))); if (SymbolName.empty() && Symbol->getType() == ELF::STT_SECTION) { - unsigned SectionIndex; - StringRef SectionName; Elf_Sym_Range Syms = unwrapOrError( ObjF->getFileName(), ObjF->getELFFile()->symbols(DotSymtabSec)); - getSectionNameIndex(Symbol, Syms.begin(), SectionName, SectionIndex); - return SectionName; + unsigned SectionIndex = unwrapOrError( + ObjF->getFileName(), getSymbolSectionIndex(Symbol, Syms.begin())); + return unwrapOrError(ObjF->getFileName(), + getSymbolSectionName(Symbol, SectionIndex)); } if (!IsDynamic) @@ -842,33 +843,43 @@ std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym *Symbol, } template <typename ELFT> -void ELFDumper<ELFT>::getSectionNameIndex(const Elf_Sym *Symbol, - const Elf_Sym *FirstSym, - StringRef &SectionName, - unsigned &SectionIndex) const { - SectionIndex = Symbol->st_shndx; +Expected<unsigned> +ELFDumper<ELFT>::getSymbolSectionIndex(const Elf_Sym *Symbol, + const Elf_Sym *FirstSym) const { + return Symbol->st_shndx == SHN_XINDEX + ? object::getExtendedSymbolTableIndex<ELFT>(Symbol, FirstSym, + ShndxTable) + : Symbol->st_shndx; +} + +// If the Symbol has a reserved st_shndx other than SHN_XINDEX, return a +// descriptive interpretation of the st_shndx value. Otherwise, return the name +// of the section with index SectionIndex. This function assumes that if the +// Symbol has st_shndx == SHN_XINDEX the SectionIndex will be the value derived +// from the SHT_SYMTAB_SHNDX section. +template <typename ELFT> +Expected<StringRef> +ELFDumper<ELFT>::getSymbolSectionName(const Elf_Sym *Symbol, + unsigned SectionIndex) const { if (Symbol->isUndefined()) - SectionName = "Undefined"; - else if (Symbol->isProcessorSpecific()) - SectionName = "Processor Specific"; - else if (Symbol->isOSSpecific()) - SectionName = "Operating System Specific"; - else if (Symbol->isAbsolute()) - SectionName = "Absolute"; - else if (Symbol->isCommon()) - SectionName = "Common"; - else if (Symbol->isReserved() && SectionIndex != SHN_XINDEX) - SectionName = "Reserved"; - else { - if (SectionIndex == SHN_XINDEX) - SectionIndex = unwrapOrError(ObjF->getFileName(), - object::getExtendedSymbolTableIndex<ELFT>( - Symbol, FirstSym, ShndxTable)); - const ELFFile<ELFT> *Obj = ObjF->getELFFile(); - const typename ELFT::Shdr *Sec = - unwrapOrError(ObjF->getFileName(), Obj->getSection(SectionIndex)); - SectionName = unwrapOrError(ObjF->getFileName(), Obj->getSectionName(Sec)); - } + return "Undefined"; + if (Symbol->isProcessorSpecific()) + return "Processor Specific"; + if (Symbol->isOSSpecific()) + return "Operating System Specific"; + if (Symbol->isAbsolute()) + return "Absolute"; + if (Symbol->isCommon()) + return "Common"; + if (Symbol->isReserved() && Symbol->st_shndx != SHN_XINDEX) + return "Reserved"; + + const ELFFile<ELFT> *Obj = ObjF->getELFFile(); + Expected<const Elf_Shdr *> SecOrErr = + Obj->getSection(SectionIndex); + if (!SecOrErr) + return SecOrErr.takeError(); + return Obj->getSectionName(*SecOrErr); } template <class ELFO> @@ -5443,9 +5454,10 @@ void LLVMStyle<ELFT>::printSectionHeaders(const ELFO *Obj) { template <class ELFT> void LLVMStyle<ELFT>::printSymbolSection(const Elf_Sym *Symbol, const Elf_Sym *First) { - unsigned SectionIndex = 0; - StringRef SectionName; - this->dumper()->getSectionNameIndex(Symbol, First, SectionName, SectionIndex); + unsigned SectionIndex = unwrapOrError( + this->FileName, this->dumper()->getSymbolSectionIndex(Symbol, First)); + StringRef SectionName = unwrapOrError( + this->FileName, this->dumper()->getSymbolSectionName(Symbol, SectionIndex)); W.printHex("Section", SectionName, SectionIndex); } |

