Skip to content

Commit c74751d

Browse files
author
Georgii Rymar
committed
[obj2yaml] - Fix the crash in getUniquedSectionName().
`getUniquedSectionName(const Elf_Shdr *Sec)` assumes that `Sec` is not `nullptr`. I've found one place in `getUniquedSymbolName` where it is not true (because of that we crash when trying to dump unnamed null section symbols). Patch fixes the crash and changes the signature of the `getUniquedSectionName` section to accept a reference. Differential revision: https://reviews.llvm.org/D93754
1 parent d02de13 commit c74751d

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

llvm/test/tools/obj2yaml/ELF/symbol.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,44 @@ Symbols:
2525
- Name: bar
2626
Size: 0x1
2727
Value: 0x1
28+
29+
## Check how we dump unnamed section symbols.
30+
## Check we are able to handle the section symbol for the null section.
31+
## Document we name them with a section name they describe.
32+
33+
# RUN: yaml2obj --docnum=2 %s -o %t2
34+
# RUN: obj2yaml %t2 | FileCheck %s --check-prefix=SECTION-SYM
35+
36+
# SECTION-SYM: --- !ELF
37+
# SECTION-SYM-NEXT: FileHeader:
38+
# SECTION-SYM-NEXT: Class: ELFCLASS64
39+
# SECTION-SYM-NEXT: Data: ELFDATA2LSB
40+
# SECTION-SYM-NEXT: Type: ET_REL
41+
# SECTION-SYM-NEXT: Sections:
42+
# SECTION-SYM-NEXT: - Name: .section
43+
# SECTION-SYM-NEXT: Type: SHT_PROGBITS
44+
# SECTION-SYM-NEXT: Symbols:
45+
# SECTION-SYM-NEXT: - Type: STT_SECTION
46+
# SECTION-SYM-NEXT: - Name: .section
47+
# SECTION-SYM-NEXT: Type: STT_SECTION
48+
# SECTION-SYM-NEXT: Section: .section
49+
# SECTION-SYM-NEXT: - Name: .section
50+
# SECTION-SYM-NEXT: Type: STT_SECTION
51+
# SECTION-SYM-NEXT: Section: .section
52+
# SECTION-SYM-NEXT: ...
53+
54+
--- !ELF
55+
FileHeader:
56+
Class: ELFCLASS64
57+
Data: ELFDATA2LSB
58+
Type: ET_REL
59+
Sections:
60+
- Name: .section
61+
Type: SHT_PROGBITS
62+
Symbols:
63+
- Type: STT_SECTION
64+
Index: 0
65+
- Type: STT_SECTION
66+
Index: 1
67+
- Type: STT_SECTION
68+
Index: 1

llvm/tools/obj2yaml/elf2yaml.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ELFDumper {
3737

3838
BumpPtrAllocator StringAllocator;
3939

40-
Expected<StringRef> getUniquedSectionName(const Elf_Shdr *Sec);
40+
Expected<StringRef> getUniquedSectionName(const Elf_Shdr &Sec);
4141
Expected<StringRef> getUniquedSymbolName(const Elf_Sym *Sym,
4242
StringRef StrTable,
4343
const Elf_Shdr *SymTab);
@@ -115,13 +115,12 @@ ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O,
115115

116116
template <class ELFT>
117117
Expected<StringRef>
118-
ELFDumper<ELFT>::getUniquedSectionName(const Elf_Shdr *Sec) {
119-
unsigned SecIndex = Sec - &Sections[0];
120-
assert(&Sections[SecIndex] == Sec);
118+
ELFDumper<ELFT>::getUniquedSectionName(const Elf_Shdr &Sec) {
119+
unsigned SecIndex = &Sec - &Sections[0];
121120
if (!SectionNames[SecIndex].empty())
122121
return SectionNames[SecIndex];
123122

124-
auto NameOrErr = Obj.getSectionName(*Sec);
123+
auto NameOrErr = Obj.getSectionName(Sec);
125124
if (!NameOrErr)
126125
return NameOrErr;
127126
StringRef Name = *NameOrErr;
@@ -150,10 +149,12 @@ ELFDumper<ELFT>::getUniquedSymbolName(const Elf_Sym *Sym, StringRef StrTable,
150149
return SymbolNameOrErr;
151150
StringRef Name = *SymbolNameOrErr;
152151
if (Name.empty() && Sym->getType() == ELF::STT_SECTION) {
153-
auto ShdrOrErr = Obj.getSection(*Sym, SymTab, ShndxTables.lookup(SymTab));
152+
Expected<const Elf_Shdr *> ShdrOrErr =
153+
Obj.getSection(*Sym, SymTab, ShndxTables.lookup(SymTab));
154154
if (!ShdrOrErr)
155155
return ShdrOrErr.takeError();
156-
return getUniquedSectionName(*ShdrOrErr);
156+
// The null section has no name.
157+
return (*ShdrOrErr == nullptr) ? "" : getUniquedSectionName(**ShdrOrErr);
157158
}
158159

159160
// Symbols in .symtab can have duplicate names. For example, it is a common
@@ -678,7 +679,7 @@ Error ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
678679
if (!Shdr)
679680
return Error::success();
680681

681-
auto NameOrErr = getUniquedSectionName(Shdr);
682+
auto NameOrErr = getUniquedSectionName(*Shdr);
682683
if (!NameOrErr)
683684
return NameOrErr.takeError();
684685
S.Section = NameOrErr.get();
@@ -755,7 +756,7 @@ Error ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
755756

756757
S.OriginalSecNdx = Shdr - &Sections[0];
757758

758-
auto NameOrErr = getUniquedSectionName(Shdr);
759+
Expected<StringRef> NameOrErr = getUniquedSectionName(*Shdr);
759760
if (!NameOrErr)
760761
return NameOrErr.takeError();
761762
S.Name = NameOrErr.get();
@@ -764,14 +765,14 @@ Error ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
764765
S.EntSize = static_cast<llvm::yaml::Hex64>(Shdr->sh_entsize);
765766

766767
if (Shdr->sh_link != ELF::SHN_UNDEF) {
767-
auto LinkSection = Obj.getSection(Shdr->sh_link);
768+
Expected<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
768769
if (!LinkSection)
769770
return make_error<StringError>(
770771
"unable to resolve sh_link reference in section '" + S.Name +
771772
"': " + toString(LinkSection.takeError()),
772773
inconvertibleErrorCode());
773774

774-
NameOrErr = getUniquedSectionName(*LinkSection);
775+
NameOrErr = getUniquedSectionName(**LinkSection);
775776
if (!NameOrErr)
776777
return NameOrErr.takeError();
777778
S.Link = NameOrErr.get();
@@ -795,7 +796,7 @@ Error ELFDumper<ELFT>::dumpCommonRelocationSection(
795796
if (!InfoSection)
796797
return InfoSection.takeError();
797798

798-
auto NameOrErr = getUniquedSectionName(*InfoSection);
799+
Expected<StringRef> NameOrErr = getUniquedSectionName(**InfoSection);
799800
if (!NameOrErr)
800801
return NameOrErr.takeError();
801802
S.RelocatableSec = NameOrErr.get();
@@ -1462,10 +1463,10 @@ ELFDumper<ELFT>::dumpGroupSection(const Elf_Shdr *Shdr) {
14621463
continue;
14631464
}
14641465

1465-
auto SHdrOrErr = Obj.getSection(Member);
1466+
Expected<const Elf_Shdr *> SHdrOrErr = Obj.getSection(Member);
14661467
if (!SHdrOrErr)
14671468
return SHdrOrErr.takeError();
1468-
auto NameOrErr = getUniquedSectionName(*SHdrOrErr);
1469+
Expected<StringRef> NameOrErr = getUniquedSectionName(**SHdrOrErr);
14691470
if (!NameOrErr)
14701471
return NameOrErr.takeError();
14711472
S->Members->push_back({*NameOrErr});

0 commit comments

Comments
 (0)