Skip to content

Commit 874c9cc

Browse files
committed
Handle illegal symidx
1 parent 2264108 commit 874c9cc

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

llvm/lib/ObjCopy/COFF/COFFWriter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ Error COFFWriter::finalizeCFGuardContents() {
144144
RawIds.size() / 4);
145145
std::vector<support::ulittle32_t> NewIds;
146146
for (auto Id : Ids) {
147+
if (!SymIdMap.contains(Id))
148+
return createStringError(object_error::invalid_symbol_index,
149+
"section '%s' contains a .symidx (%d) that is "
150+
"incorrect or was stripped",
151+
Sec.Name.str().c_str(), Id.value());
147152
NewIds.push_back(support::ulittle32_t(SymIdMap[Id]));
148153
}
149154
ArrayRef<uint8_t> NewRawIds(reinterpret_cast<uint8_t *>(NewIds.data()),
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# RUN: yaml2obj %s -o %t.in.o
2+
# RUN: llvm-readobj -r -s -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.in.o | FileCheck %s --check-prefix=ORIG
3+
# RUN: not llvm-objcopy --strip-debug %t.in.o %t.out.o 2>&1 | FileCheck %s --check-prefix=ERROR
4+
5+
# ORIG: Relocations [
6+
# ORIG-NEXT: Section (1) .text {
7+
# ORIG-NEXT: 0x3 IMAGE_REL_AMD64_REL32 foo (10)
8+
# ORIG-NEXT: }
9+
# ORIG-NEXT: ]
10+
# ORIG: Symbols [
11+
# ORIG: Name: .gfids$y
12+
# ORIG: Section: .gfids$y
13+
# ORIG: AuxSymbolCount: 1
14+
# ORIG: AuxSectionDef {
15+
# ORIG: Checksum: 0x459345AD
16+
# ORIG: }
17+
# ORIG: Name: .giats$y
18+
# ORIG: Section: .giats$y
19+
# ORIG: AuxSymbolCount: 1
20+
# ORIG: AuxSectionDef {
21+
# ORIG: Checksum: 0x0
22+
# ORIG: }
23+
# ORIG: Name: .gljmp$y
24+
# ORIG: Section: .gljmp$y
25+
# ORIG: AuxSymbolCount: 1
26+
# ORIG: AuxSectionDef {
27+
# ORIG: Checksum: 0x0
28+
# ORIG: }
29+
# ORIG: ]
30+
# ORIG: Hex dump of section '.gfids$y':
31+
# ORIG-NEXT: 0x00000000 0a000000 10000000 ........
32+
# ORIG: Hex dump of section '.giats$y':
33+
# ORIG-EMPTY-NEXT:
34+
# ORIG: Hex dump of section '.gljmp$y':
35+
# ORIG-EMPTY-NEXT:
36+
37+
# ERROR: section '.gfids$y' contains a .symidx (16) that is incorrect or was stripped
38+
39+
--- !COFF
40+
header:
41+
Machine: IMAGE_FILE_MACHINE_AMD64
42+
Characteristics: [ ]
43+
sections:
44+
- Name: .text
45+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
46+
Alignment: 4
47+
SectionData: 488D0500000000488D0D00000000488D0500000000488D0500000000
48+
SizeOfRawData: 28
49+
Relocations:
50+
- VirtualAddress: 3
51+
SymbolName: foo
52+
Type: IMAGE_REL_AMD64_REL32
53+
- Name: '.debug$S'
54+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
55+
Alignment: 4
56+
SectionData: 04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
57+
SizeOfRawData: 37
58+
- Name: '.gfids$y'
59+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
60+
Alignment: 4
61+
SectionData: '0A00000010000000'
62+
SizeOfRawData: 8
63+
- Name: '.giats$y'
64+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
65+
Alignment: 4
66+
SectionData: ''
67+
SizeOfRawData: 0
68+
- Name: '.gljmp$y'
69+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
70+
Alignment: 4
71+
SectionData: ''
72+
SizeOfRawData: 0
73+
symbols:
74+
- Name: .text
75+
Value: 0
76+
SectionNumber: 1
77+
SimpleType: IMAGE_SYM_TYPE_NULL
78+
ComplexType: IMAGE_SYM_DTYPE_NULL
79+
StorageClass: IMAGE_SYM_CLASS_STATIC
80+
SectionDefinition:
81+
Length: 28
82+
NumberOfRelocations: 4
83+
NumberOfLinenumbers: 0
84+
CheckSum: 3583480811
85+
Number: 1
86+
- Name: '.debug$S'
87+
Value: 0
88+
SectionNumber: 2
89+
SimpleType: IMAGE_SYM_TYPE_NULL
90+
ComplexType: IMAGE_SYM_DTYPE_NULL
91+
StorageClass: IMAGE_SYM_CLASS_STATIC
92+
SectionDefinition:
93+
Length: 37
94+
NumberOfRelocations: 0
95+
NumberOfLinenumbers: 0
96+
CheckSum: 2941632545
97+
Number: 4
98+
- Name: '.gfids$y'
99+
Value: 0
100+
SectionNumber: 3
101+
SimpleType: IMAGE_SYM_TYPE_NULL
102+
ComplexType: IMAGE_SYM_DTYPE_NULL
103+
StorageClass: IMAGE_SYM_CLASS_STATIC
104+
SectionDefinition:
105+
Length: 8
106+
NumberOfRelocations: 0
107+
NumberOfLinenumbers: 0
108+
CheckSum: 1167279533
109+
Number: 5
110+
- Name: '.giats$y'
111+
Value: 0
112+
SectionNumber: 4
113+
SimpleType: IMAGE_SYM_TYPE_NULL
114+
ComplexType: IMAGE_SYM_DTYPE_NULL
115+
StorageClass: IMAGE_SYM_CLASS_STATIC
116+
SectionDefinition:
117+
Length: 0
118+
NumberOfRelocations: 0
119+
NumberOfLinenumbers: 0
120+
CheckSum: 0
121+
Number: 6
122+
- Name: '.gljmp$y'
123+
Value: 0
124+
SectionNumber: 5
125+
SimpleType: IMAGE_SYM_TYPE_NULL
126+
ComplexType: IMAGE_SYM_DTYPE_NULL
127+
StorageClass: IMAGE_SYM_CLASS_STATIC
128+
SectionDefinition:
129+
Length: 0
130+
NumberOfRelocations: 0
131+
NumberOfLinenumbers: 0
132+
CheckSum: 0
133+
Number: 7
134+
- Name: foo
135+
Value: 0
136+
SectionNumber: 0
137+
SimpleType: IMAGE_SYM_TYPE_NULL
138+
ComplexType: IMAGE_SYM_DTYPE_NULL
139+
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
140+
...

0 commit comments

Comments
 (0)