diff --git a/llvm/test/tools/obj2yaml/ELF/symbol.yaml b/llvm/test/tools/obj2yaml/ELF/symbol.yaml --- a/llvm/test/tools/obj2yaml/ELF/symbol.yaml +++ b/llvm/test/tools/obj2yaml/ELF/symbol.yaml @@ -25,3 +25,44 @@ - Name: bar Size: 0x1 Value: 0x1 + +## Check how we dump unnamed section symbols. +## Check we are able to handle the section symbol for the null section. +## Document we name them with a section name they describe. + +# RUN: yaml2obj --docnum=2 %s -o %t2 +# RUN: obj2yaml %t2 | FileCheck %s --check-prefix=SECTION-SYM + +# SECTION-SYM: --- !ELF +# SECTION-SYM-NEXT: FileHeader: +# SECTION-SYM-NEXT: Class: ELFCLASS64 +# SECTION-SYM-NEXT: Data: ELFDATA2LSB +# SECTION-SYM-NEXT: Type: ET_REL +# SECTION-SYM-NEXT: Sections: +# SECTION-SYM-NEXT: - Name: .section +# SECTION-SYM-NEXT: Type: SHT_PROGBITS +# SECTION-SYM-NEXT: Symbols: +# SECTION-SYM-NEXT: - Type: STT_SECTION +# SECTION-SYM-NEXT: - Name: .section +# SECTION-SYM-NEXT: Type: STT_SECTION +# SECTION-SYM-NEXT: Section: .section +# SECTION-SYM-NEXT: - Name: .section +# SECTION-SYM-NEXT: Type: STT_SECTION +# SECTION-SYM-NEXT: Section: .section +# SECTION-SYM-NEXT: ... + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL +Sections: + - Name: .section + Type: SHT_PROGBITS +Symbols: + - Type: STT_SECTION + Index: 0 + - Type: STT_SECTION + Index: 1 + - Type: STT_SECTION + Index: 1 diff --git a/llvm/tools/obj2yaml/elf2yaml.cpp b/llvm/tools/obj2yaml/elf2yaml.cpp --- a/llvm/tools/obj2yaml/elf2yaml.cpp +++ b/llvm/tools/obj2yaml/elf2yaml.cpp @@ -37,7 +37,7 @@ BumpPtrAllocator StringAllocator; - Expected getUniquedSectionName(const Elf_Shdr *Sec); + Expected getUniquedSectionName(const Elf_Shdr &Sec); Expected getUniquedSymbolName(const Elf_Sym *Sym, StringRef StrTable, const Elf_Shdr *SymTab); @@ -115,13 +115,12 @@ template Expected -ELFDumper::getUniquedSectionName(const Elf_Shdr *Sec) { - unsigned SecIndex = Sec - &Sections[0]; - assert(&Sections[SecIndex] == Sec); +ELFDumper::getUniquedSectionName(const Elf_Shdr &Sec) { + unsigned SecIndex = &Sec - &Sections[0]; if (!SectionNames[SecIndex].empty()) return SectionNames[SecIndex]; - auto NameOrErr = Obj.getSectionName(*Sec); + auto NameOrErr = Obj.getSectionName(Sec); if (!NameOrErr) return NameOrErr; StringRef Name = *NameOrErr; @@ -150,10 +149,12 @@ return SymbolNameOrErr; StringRef Name = *SymbolNameOrErr; if (Name.empty() && Sym->getType() == ELF::STT_SECTION) { - auto ShdrOrErr = Obj.getSection(*Sym, SymTab, ShndxTables.lookup(SymTab)); + Expected ShdrOrErr = + Obj.getSection(*Sym, SymTab, ShndxTables.lookup(SymTab)); if (!ShdrOrErr) return ShdrOrErr.takeError(); - return getUniquedSectionName(*ShdrOrErr); + // The null section has no name. + return (*ShdrOrErr == nullptr) ? "" : getUniquedSectionName(**ShdrOrErr); } // Symbols in .symtab can have duplicate names. For example, it is a common @@ -678,7 +679,7 @@ if (!Shdr) return Error::success(); - auto NameOrErr = getUniquedSectionName(Shdr); + auto NameOrErr = getUniquedSectionName(*Shdr); if (!NameOrErr) return NameOrErr.takeError(); S.Section = NameOrErr.get(); @@ -755,7 +756,7 @@ S.OriginalSecNdx = Shdr - &Sections[0]; - auto NameOrErr = getUniquedSectionName(Shdr); + Expected NameOrErr = getUniquedSectionName(*Shdr); if (!NameOrErr) return NameOrErr.takeError(); S.Name = NameOrErr.get(); @@ -764,14 +765,14 @@ S.EntSize = static_cast(Shdr->sh_entsize); if (Shdr->sh_link != ELF::SHN_UNDEF) { - auto LinkSection = Obj.getSection(Shdr->sh_link); + Expected LinkSection = Obj.getSection(Shdr->sh_link); if (!LinkSection) return make_error( "unable to resolve sh_link reference in section '" + S.Name + "': " + toString(LinkSection.takeError()), inconvertibleErrorCode()); - NameOrErr = getUniquedSectionName(*LinkSection); + NameOrErr = getUniquedSectionName(**LinkSection); if (!NameOrErr) return NameOrErr.takeError(); S.Link = NameOrErr.get(); @@ -795,7 +796,7 @@ if (!InfoSection) return InfoSection.takeError(); - auto NameOrErr = getUniquedSectionName(*InfoSection); + Expected NameOrErr = getUniquedSectionName(**InfoSection); if (!NameOrErr) return NameOrErr.takeError(); S.RelocatableSec = NameOrErr.get(); @@ -1462,10 +1463,10 @@ continue; } - auto SHdrOrErr = Obj.getSection(Member); + Expected SHdrOrErr = Obj.getSection(Member); if (!SHdrOrErr) return SHdrOrErr.takeError(); - auto NameOrErr = getUniquedSectionName(*SHdrOrErr); + Expected NameOrErr = getUniquedSectionName(**SHdrOrErr); if (!NameOrErr) return NameOrErr.takeError(); S->Members->push_back({*NameOrErr});