Skip to content

Commit ff2338d

Browse files
author
git apple-llvm automerger
committed
Merge commit '2e2cb04b98ff' from apple/master into swift/master-next
2 parents 0b9583c + 2e2cb04 commit ff2338d

File tree

6 files changed

+249
-39
lines changed

6 files changed

+249
-39
lines changed

llvm/test/Object/invalid.test

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,14 @@ Symbols: []
235235

236236
# INVALID-PH-ENTSIZE: error: '[[FILE]]': invalid e_phentsize: 12336
237237

238-
## Check that llvm-readobj reports an error when we have no SHT_SYMTAB_SHNDX section,
238+
## Check that llvm-readobj reports a warning when we have no SHT_SYMTAB_SHNDX section,
239239
## but have a symbol referencing it.
240240

241-
# RUN: not llvm-readobj --symbols %p/Inputs/invalid-ext-symtab-index.elf-x86-64 2>&1 | \
241+
# RUN: llvm-readobj --symbols %p/Inputs/invalid-ext-symtab-index.elf-x86-64 2>&1 | \
242242
# RUN: FileCheck -DFILE=%p/Inputs/invalid-ext-symtab-index.elf-x86-64 --check-prefix=INVALID-EXT-SYMTAB-INDEX %s
243243

244-
# INVALID-EXT-SYMTAB-INDEX: error: '[[FILE]]': extended symbol index (0) is past the end of the SHT_SYMTAB_SHNDX section of size 0
244+
# INVALID-EXT-SYMTAB-INDEX: warning: '[[FILE]]': extended symbol index (0) is past the end of the SHT_SYMTAB_SHNDX section of size 0
245+
# INVALID-EXT-SYMTAB-INDEX: Section: Reserved (0xFFFF)
245246

246247
## Check that llvm-readobj reports an error if a relocation section
247248
## has a broken sh_offset (past the end of the file).
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
## ELF section symbols use the section names when printing. This test verifies
2+
## this and also that appropriate things are printed if the section is somehow
3+
## invalid.
4+
5+
# RUN: yaml2obj %s -o %t1
6+
# RUN: llvm-readobj %t1 --symbols 2> %t.llvm.err1 | FileCheck %s --check-prefix=LLVM1
7+
# RUN: FileCheck %s --input-file %t.llvm.err1 --check-prefix=WARN1 --implicit-check-not=warning
8+
# RUN: llvm-readelf %t1 --symbols 2> %t.gnu.err1 | FileCheck %s --check-prefix=GNU1
9+
# RUN: FileCheck %s --input-file %t.gnu.err1 --check-prefix=WARN1 --implicit-check-not=warning
10+
11+
# LLVM1: Name: (0)
12+
# LLVM1: Name: .foo (0)
13+
# LLVM1: Name: <section 67> (0)
14+
# LLVM1: Name: .bar (0)
15+
# LLVM1: Name: <section 66> (0)
16+
17+
# GNU1: Symbol table '.symtab' contains 5 entries:
18+
# GNU1-NEXT: Num: {{.*}} Type {{.*}} Ndx Name
19+
# GNU1-NEXT: 0: {{.*}} NOTYPE {{.*}} UND {{$}}
20+
# GNU1-NEXT: 1: {{.*}} SECTION {{.*}} 1 .foo
21+
# GNU1-NEXT: 2: {{.*}} SECTION {{.*}} 67 <section 67>
22+
# GNU1-NEXT: 3: {{.*}} SECTION {{.*}} 2 .bar
23+
# GNU1-NEXT: 4: {{.*}} SECTION {{.*}} 66 <section 66>
24+
25+
# WARN1: warning: '{{.*}}.tmp1': invalid section index: 67
26+
# WARN1: warning: '{{.*}}.tmp1': invalid section index: 66
27+
28+
--- !ELF
29+
FileHeader:
30+
Class: ELFCLASS32
31+
Data: ELFDATA2LSB
32+
Type: ET_REL
33+
Machine: EM_386
34+
Sections:
35+
- Name: .foo
36+
Type: SHT_PROGBITS
37+
- Name: .bar
38+
Type: SHT_PROGBITS
39+
- Name: .symtab_shndx
40+
Type: SHT_SYMTAB_SHNDX
41+
Link: .symtab
42+
Entries: [ 0, 0, 0, 2, 0x42 ]
43+
Symbols:
44+
- Name: ""
45+
Section: .foo
46+
Type: STT_SECTION
47+
- Name: ""
48+
Index: 0x43
49+
Type: STT_SECTION
50+
# Section symbol via SHT_SYMTAB_SHNDX.
51+
- Name: ""
52+
Index: SHN_XINDEX
53+
Type: STT_SECTION
54+
# Section symbol via SHT_SYMTAB_SHNDX with invalid index.
55+
- Name: ""
56+
Index: SHN_XINDEX
57+
Type: STT_SECTION
58+
59+
# RUN: yaml2obj %s --docnum=2 -o %t2
60+
# RUN: llvm-readobj %t2 --symbols 2> %t.llvm.err2 | FileCheck %s --check-prefix=LLVM2
61+
# RUN: FileCheck %s --input-file %t.llvm.err2 --check-prefix=WARN2 --implicit-check-not=warning
62+
# RUN: llvm-readelf %t2 --symbols 2> %t.gnu.err2 | FileCheck %s --check-prefix=GNU2
63+
# RUN: FileCheck %s --input-file %t.gnu.err2 --check-prefix=WARN2 --implicit-check-not=warning
64+
65+
# LLVM2: Name: (0)
66+
# LLVM2: Name: <?> (0)
67+
68+
# GNU2: Symbol table '.symtab' contains 2 entries:
69+
# GNU2-NEXT: Num: {{.*}} Type {{.*}} Ndx Name
70+
# GNU2-NEXT: 0: {{.*}} NOTYPE {{.*}} UND {{$}}
71+
# GNU2-NEXT: 1: {{.*}} SECTION {{.*}} RSV[0xffff] <?>
72+
73+
# WARN2: warning: '{{.*}}.tmp2': extended symbol index (1) is past the end of the SHT_SYMTAB_SHNDX section of size 0
74+
75+
--- !ELF
76+
FileHeader:
77+
Class: ELFCLASS32
78+
Data: ELFDATA2LSB
79+
Type: ET_REL
80+
Machine: EM_386
81+
Symbols:
82+
# Section symbol via SHT_SYMTAB_SHNDX when SHT_SYMTAB_SHNDX is missing.
83+
- Name: ""
84+
Index: SHN_XINDEX
85+
Type: STT_SECTION

llvm/test/tools/llvm-readobj/elf-symbol-shndx.test

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
# Show that llvm-readobj prints symbol shndxes correctly, for valid indexes,
2-
# invalid indexes (i.e. section indexes that don't correspond to a real
3-
# section), reserved values and processor/os-specific index values, for both GNU
4-
# and LLVM styles.
1+
## Show that llvm-readobj prints symbol shndxes correctly, for valid indexes,
2+
## invalid indexes (i.e. section indexes that don't correspond to a real
3+
## section), reserved values and processor/os-specific index values, for both GNU
4+
## and LLVM styles.
55

66
# RUN: yaml2obj --docnum=1 %s > %t1
77
# RUN: llvm-readobj --symbols %t1 | FileCheck %s --check-prefix=LLVM1
88
# RUN: llvm-readelf --symbols %t1 | FileCheck %s --check-prefix=GNU1
99

10-
# llvm-readobj doesn't accept shndx values that are not valid section indexes
11-
# for LLVM style, so only test GNU output in this case.
12-
# RUN: yaml2obj --docnum=2 %s > %t2
13-
# RUN: llvm-readelf --symbols %t2 | FileCheck %s --check-prefix=GNU2
14-
1510
# LLVM1: Name: undef
1611
# LLVM1: Section:
1712
# LLVM1-SAME: Undefined (0x0)
@@ -49,11 +44,6 @@
4944
# GNU1-NEXT: 7: {{.*}} RSV[0xfffe] reserved
5045
# GNU1-NEXT: 8: {{.*}} 1 xindex
5146

52-
# GNU2: Symbol table '.symtab' contains 2 entries:
53-
# GNU2-NEXT: Num: {{.*}} Ndx Name
54-
# GNU2-NEXT: 0: {{.*}} UND
55-
# GNU2-NEXT: 1: {{.*}} 66 bad
56-
5747
--- !ELF
5848
FileHeader:
5949
Class: ELFCLASS32
@@ -92,13 +82,106 @@ Symbols:
9282
Index: SHN_XINDEX
9383
Binding: STB_GLOBAL
9484

85+
## In this case, the index does not correspond to a real section. Check that GNU
86+
## style just prints the section index as normal and LLVM style prints a warning
87+
## (but only once for each warning).
88+
# RUN: yaml2obj --docnum=2 %s > %t2
89+
# RUN: llvm-readobj --symbols %t2 2> %t2.llvm.err | FileCheck %s --check-prefix=LLVM2
90+
# RUN: FileCheck %s --input-file=%t2.llvm.err --check-prefix=BAD-SHNDX --implicit-check-not=warning
91+
# RUN: llvm-readelf --symbols %t2 2> %t2.gnu.err | FileCheck %s --check-prefix=GNU2
92+
# RUN: FileCheck %s --input-file=%t2.gnu.err --allow-empty --implicit-check-not={{.}}
93+
94+
# LLVM2: Name: bad
95+
# LLVM2: Section:
96+
# LLVM2-SAME: <?> (0x42)
97+
# LLVM2: Name: bad2
98+
# LLVM2: Section:
99+
# LLVM2-SAME: <?> (0x42)
100+
# LLVM2: Name: bad3
101+
# LLVM2: Section:
102+
# LLVM2-SAME: <?> (0x43)
103+
# LLVM2: Name: invalid_shndx
104+
# LLVM2: Section:
105+
# LLVM2-SAME: <?> (0x9)
106+
# LLVM2: Name: invalid_shndx2
107+
# LLVM2: Section:
108+
# LLVM2-SAME: <?> (0x9)
109+
# LLVM2: Name: invalid_shndx3
110+
# LLVM2: Section:
111+
# LLVM2-SAME: <?> (0xA)
112+
113+
# GNU2: Symbol table '.symtab' contains 7 entries:
114+
# GNU2-NEXT: Num: {{.*}} Ndx Name
115+
# GNU2-NEXT: 0: {{.*}} UND
116+
# GNU2-NEXT: 1: {{.*}} 66 bad
117+
# GNU2-NEXT: 2: {{.*}} 66 bad2
118+
# GNU2-NEXT: 3: {{.*}} 67 bad3
119+
# GNU2-NEXT: 4: {{.*}} 9 invalid_shndx
120+
# GNU2-NEXT: 5: {{.*}} 9 invalid_shndx2
121+
# GNU2-NEXT: 6: {{.*}} 10 invalid_shndx3
122+
123+
# BAD-SHNDX: warning: '{{.*}}tmp2': invalid section index: 66
124+
# BAD-SHNDX: warning: '{{.*}}tmp2': invalid section index: 67
125+
# BAD-SHNDX: warning: '{{.*}}tmp2': invalid section index: 9
126+
# BAD-SHNDX: warning: '{{.*}}tmp2': invalid section index: 10
127+
95128
--- !ELF
96129
FileHeader:
97130
Class: ELFCLASS32
98131
Data: ELFDATA2LSB
99132
Type: ET_REL
100133
Machine: EM_386
134+
Sections:
135+
- Name: .symtab_shndx
136+
Type: SHT_SYMTAB_SHNDX
137+
Link: .symtab
138+
Entries: [ 0, 0, 0, 0, 9, 9, 10 ]
101139
Symbols:
102140
- Name: bad
103141
Index: 0x42
104-
Binding: STB_GLOBAL
142+
- Name: bad2
143+
Index: 0x42
144+
- Name: bad3
145+
Index: 0x43
146+
- Name: invalid_shndx
147+
Index: SHN_XINDEX
148+
- Name: invalid_shndx2
149+
Index: SHN_XINDEX
150+
- Name: invalid_shndx3
151+
Index: SHN_XINDEX
152+
153+
## In this case, the symtab shndx section is missing, so symbols with section
154+
## indexes of SHN_XINDEX print as Reserved symbols.
155+
# RUN: yaml2obj --docnum=3 %s > %t3
156+
# RUN: llvm-readobj --symbols %t3 2> %t3.llvm.err | FileCheck %s --check-prefix=LLVM3
157+
# RUN: FileCheck %s --input-file=%t3.llvm.err --check-prefix=NO-SYMTAB-SHNDX --implicit-check-not=warning
158+
# RUN: llvm-readelf --symbols %t3 2> %t3.gnu.err | FileCheck %s --check-prefix=GNU3
159+
# RUN: FileCheck %s --input-file=%t3.gnu.err --check-prefix=NO-SYMTAB-SHNDX --implicit-check-not=warning
160+
161+
# LLVM3: Name: no_shndx
162+
# LLVM3: Section:
163+
# LLVM3-SAME: Reserved (0xFFFF)
164+
# LLVM3: Name: no_shndx2
165+
# LLVM3: Section:
166+
# LLVM3-SAME: Reserved (0xFFFF)
167+
168+
# GNU3: Symbol table '.symtab' contains 3 entries:
169+
# GNU3-NEXT: Num: {{.*}} Ndx Name
170+
# GNU3-NEXT: 0: {{.*}} UND
171+
# GNU3-NEXT: 1: {{.*}} RSV[0xffff] no_shndx
172+
# GNU3-NEXT: 2: {{.*}} RSV[0xffff] no_shndx2
173+
174+
# NO-SYMTAB-SHNDX: warning: '{{.*}}tmp3': extended symbol index (1) is past the end of the SHT_SYMTAB_SHNDX section of size 0
175+
# NO-SYMTAB-SHNDX: warning: '{{.*}}tmp3': extended symbol index (2) is past the end of the SHT_SYMTAB_SHNDX section of size 0
176+
177+
--- !ELF
178+
FileHeader:
179+
Class: ELFCLASS32
180+
Data: ELFDATA2LSB
181+
Type: ET_REL
182+
Machine: EM_386
183+
Symbols:
184+
- Name: no_shndx
185+
Index: SHN_XINDEX
186+
- Name: no_shndx2
187+
Index: SHN_XINDEX

llvm/test/tools/yaml2obj/dynamic-symbols.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ DynamicSymbols:
4444
## Check we can use numeric values to refer to sections.
4545

4646
# RUN: yaml2obj --docnum=2 %s -o %t2
47-
# RUN: not llvm-readobj --dyn-symbols %t2 2>&1 | FileCheck -DFILE=%t2 %s --check-prefix=NUM
47+
# RUN: llvm-readobj --dyn-symbols %t2 2>&1 | FileCheck -DFILE=%t2 %s --check-prefix=NUM
4848

4949
# NUM: Name: foo
5050
# NUM: Section:
@@ -54,7 +54,10 @@ DynamicSymbols:
5454
# NUM: Section:
5555
# NUM-SAME: .strtab (0x2)
5656

57-
# NUM: error: '[[FILE]]': invalid section index: 255
57+
# NUM: Name: zed
58+
# NUM: warning: '[[FILE]]': invalid section index: 255
59+
# NUM: Section:
60+
# NUM-SAME: <?> (0xFF)
5861

5962
--- !ELF
6063
FileHeader:

llvm/test/tools/yaml2obj/elf-sht-symtab-shndx.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
## but no SHT_SYMTAB_SHNDX section is defined.
44

55
# RUN: yaml2obj --docnum=1 %s -o %t1
6-
# RUN: not llvm-readobj --symbols 2>&1 %t1 | FileCheck -DFILE=%t1 %s --check-prefix=CASE1
6+
# RUN: llvm-readobj --symbols 2>&1 %t1 | FileCheck -DFILE=%t1 %s --check-prefix=CASE1
77

8-
# CASE1: error: '[[FILE]]': extended symbol index (1) is past the end of the SHT_SYMTAB_SHNDX section of size 0
8+
# CASE1: warning: '[[FILE]]': extended symbol index (1) is past the end of the SHT_SYMTAB_SHNDX section of size 0
99

1010
--- !ELF
1111
FileHeader:
@@ -71,9 +71,9 @@ Symbols:
7171
## which is larger than the total number of sections in the file).
7272

7373
# RUN: yaml2obj --docnum=3 %s -o %t3
74-
# RUN: not llvm-readobj --symbols 2>&1 %t3 | FileCheck %s -DFILE=%t3 --check-prefix=CASE3
74+
# RUN: llvm-readobj --symbols 2>&1 %t3 | FileCheck %s -DFILE=%t3 --check-prefix=CASE3
7575

76-
# CASE3: error: '[[FILE]]': invalid section index: 255
76+
# CASE3: warning: '[[FILE]]': invalid section index: 255
7777

7878
--- !ELF
7979
FileHeader:

llvm/tools/llvm-readobj/ELFDumper.cpp

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ template <typename ELFT> class DumpStyle {
433433
virtual void printMipsABIFlags(const ELFObjectFile<ELFT> *Obj) = 0;
434434
const ELFDumper<ELFT> *dumper() const { return Dumper; }
435435

436+
void reportUniqueWarning(Error Err) const;
437+
436438
protected:
437439
std::function<Error(const Twine &Msg)> WarningHandler;
438440
StringRef FileName;
@@ -556,6 +558,14 @@ template <typename ELFT> class GNUStyle : public DumpStyle<ELFT> {
556558
void printSectionMapping(const ELFO *Obj);
557559
};
558560

561+
template <class ELFT>
562+
void DumpStyle<ELFT>::reportUniqueWarning(Error Err) const {
563+
handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
564+
cantFail(WarningHandler(EI.message()),
565+
"WarningHandler should always return ErrorSuccess");
566+
});
567+
}
568+
559569
template <typename ELFT> class LLVMStyle : public DumpStyle<ELFT> {
560570
public:
561571
TYPEDEF_ELF_TYPES(ELFT)
@@ -824,10 +834,18 @@ std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym *Symbol,
824834
if (SymbolName.empty() && Symbol->getType() == ELF::STT_SECTION) {
825835
Elf_Sym_Range Syms = unwrapOrError(
826836
ObjF->getFileName(), ObjF->getELFFile()->symbols(DotSymtabSec));
827-
unsigned SectionIndex = unwrapOrError(
828-
ObjF->getFileName(), getSymbolSectionIndex(Symbol, Syms.begin()));
829-
return unwrapOrError(ObjF->getFileName(),
830-
getSymbolSectionName(Symbol, SectionIndex));
837+
Expected<unsigned> SectionIndex =
838+
getSymbolSectionIndex(Symbol, Syms.begin());
839+
if (!SectionIndex) {
840+
ELFDumperStyle->reportUniqueWarning(SectionIndex.takeError());
841+
return "<?>";
842+
}
843+
Expected<StringRef> NameOrErr = getSymbolSectionName(Symbol, *SectionIndex);
844+
if (!NameOrErr) {
845+
ELFDumperStyle->reportUniqueWarning(NameOrErr.takeError());
846+
return ("<section " + Twine(*SectionIndex) + ">").str();
847+
}
848+
return *NameOrErr;
831849
}
832850

833851
if (!IsDynamic)
@@ -3298,12 +3316,18 @@ std::string GNUStyle<ELFT>::getSymbolSectionNdx(const ELFO *Obj,
32983316
return "ABS";
32993317
case ELF::SHN_COMMON:
33003318
return "COM";
3301-
case ELF::SHN_XINDEX:
3302-
return to_string(format_decimal(
3303-
unwrapOrError(this->FileName,
3304-
object::getExtendedSymbolTableIndex<ELFT>(
3305-
Symbol, FirstSym, this->dumper()->getShndxTable())),
3306-
3));
3319+
case ELF::SHN_XINDEX: {
3320+
Expected<uint32_t> IndexOrErr = object::getExtendedSymbolTableIndex<ELFT>(
3321+
Symbol, FirstSym, this->dumper()->getShndxTable());
3322+
if (!IndexOrErr) {
3323+
assert(Symbol->st_shndx == SHN_XINDEX &&
3324+
"getSymbolSectionIndex should only fail due to an invalid "
3325+
"SHT_SYMTAB_SHNDX table/reference");
3326+
this->reportUniqueWarning(IndexOrErr.takeError());
3327+
return "RSV[0xffff]";
3328+
}
3329+
return to_string(format_decimal(*IndexOrErr, 3));
3330+
}
33073331
default:
33083332
// Find if:
33093333
// Processor specific
@@ -5454,11 +5478,25 @@ void LLVMStyle<ELFT>::printSectionHeaders(const ELFO *Obj) {
54545478
template <class ELFT>
54555479
void LLVMStyle<ELFT>::printSymbolSection(const Elf_Sym *Symbol,
54565480
const Elf_Sym *First) {
5457-
unsigned SectionIndex = unwrapOrError(
5458-
this->FileName, this->dumper()->getSymbolSectionIndex(Symbol, First));
5459-
StringRef SectionName = unwrapOrError(
5460-
this->FileName, this->dumper()->getSymbolSectionName(Symbol, SectionIndex));
5461-
W.printHex("Section", SectionName, SectionIndex);
5481+
Expected<unsigned> SectionIndex =
5482+
this->dumper()->getSymbolSectionIndex(Symbol, First);
5483+
if (!SectionIndex) {
5484+
assert(Symbol->st_shndx == SHN_XINDEX &&
5485+
"getSymbolSectionIndex should only fail due to an invalid "
5486+
"SHT_SYMTAB_SHNDX table/reference");
5487+
this->reportUniqueWarning(SectionIndex.takeError());
5488+
W.printHex("Section", "Reserved", SHN_XINDEX);
5489+
return;
5490+
}
5491+
5492+
Expected<StringRef> SectionName =
5493+
this->dumper()->getSymbolSectionName(Symbol, *SectionIndex);
5494+
if (!SectionName) {
5495+
this->reportUniqueWarning(SectionName.takeError());
5496+
W.printHex("Section", "<?>", *SectionIndex);
5497+
} else {
5498+
W.printHex("Section", *SectionName, *SectionIndex);
5499+
}
54625500
}
54635501

54645502
template <class ELFT>

0 commit comments

Comments
 (0)