Skip to content

Commit 71f9b12

Browse files
mstorsjotru
authored andcommitted
[llvm-objcopy] [COFF] Ignore associative sections in executables (#151143)
COFF associative sections is a feature where relocatable object files can have section snippets marked as related to another section snippet, so they are kept or discarded in relation to that other section snippet. When llvm-objcopy removes sections, it also removes sections that are marked as associative to the removed section (as the associative sections otherwise would end up orphaned). In a linked executable module (EXE or DLL), section associativity is meaningless - thus, we should ignore those fields from the input. After linking, GNU ld keeps the SectionDefinition auxillary part of symbols intact as it was in the source object file, which means that it references section numbers in the source object files. This fixes #53433. (cherry picked from commit fcbbcff)
1 parent 773b718 commit 71f9b12

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed

llvm/lib/ObjCopy/COFF/COFFReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Error COFFReader::readSymbols(Object &Obj, bool IsBigObj) const {
135135
// it is, find the target section unique id.
136136
const coff_aux_section_definition *SD = SymRef.getSectionDefinition();
137137
const coff_aux_weak_external *WE = SymRef.getWeakExternal();
138-
if (SD && SD->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
138+
if (SD && SD->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE && !Obj.IsPE) {
139139
int32_t Index = SD->getNumber(IsBigObj);
140140
if (Index <= 0 || static_cast<uint32_t>(Index - 1) >= Sections.size())
141141
return createStringError(object_error::parse_failed,
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
## Test that bogus associative section symbols in executables are ignored.
2+
##
3+
## The executable contains two (bogus) associative section symbols, both for
4+
## (parts of) the .rdata section; one pointing at the .debug_info section
5+
## (which will be stripped out) and one pointing at a nonexistent section.
6+
##
7+
## Check that stripping does succeed, and that it doesn't end up removing
8+
## the .rdata section.
9+
10+
# RUN: yaml2obj %s -o %t.in.exe
11+
12+
# RUN: llvm-strip --strip-debug %t.in.exe -o %t.out.exe
13+
# RUN: llvm-readobj --sections %t.out.exe | FileCheck %s
14+
15+
# CHECK: Name: .rdata
16+
17+
--- !COFF
18+
OptionalHeader:
19+
AddressOfEntryPoint: 4096
20+
ImageBase: 5368709120
21+
SectionAlignment: 4096
22+
FileAlignment: 512
23+
MajorOperatingSystemVersion: 4
24+
MinorOperatingSystemVersion: 0
25+
MajorImageVersion: 0
26+
MinorImageVersion: 0
27+
MajorSubsystemVersion: 5
28+
MinorSubsystemVersion: 2
29+
Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI
30+
DLLCharacteristics: [ ]
31+
SizeOfStackReserve: 2097152
32+
SizeOfStackCommit: 4096
33+
SizeOfHeapReserve: 1048576
34+
SizeOfHeapCommit: 4096
35+
header:
36+
Machine: IMAGE_FILE_MACHINE_AMD64
37+
Characteristics: [ ]
38+
sections:
39+
- Name: .text
40+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
41+
VirtualAddress: 4096
42+
VirtualSize: 48
43+
SectionData: E806000000E802000000C3C3C30F1F00FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000
44+
SizeOfRawData: 512
45+
- Name: .rdata
46+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
47+
VirtualAddress: 8192
48+
VirtualSize: 4
49+
SectionData: '00000000'
50+
SizeOfRawData: 512
51+
- Name: .debug_info
52+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
53+
VirtualAddress: 16384
54+
VirtualSize: 4
55+
SectionData: '00000000'
56+
SizeOfRawData: 512
57+
symbols:
58+
- Name: .text
59+
Value: 0
60+
SectionNumber: 1
61+
SimpleType: IMAGE_SYM_TYPE_NULL
62+
ComplexType: IMAGE_SYM_DTYPE_NULL
63+
StorageClass: IMAGE_SYM_CLASS_STATIC
64+
SectionDefinition:
65+
Length: 11
66+
NumberOfRelocations: 2
67+
NumberOfLinenumbers: 0
68+
CheckSum: 1703692295
69+
Number: 1
70+
- Name: '.text$func1'
71+
Value: 11
72+
SectionNumber: 1
73+
SimpleType: IMAGE_SYM_TYPE_NULL
74+
ComplexType: IMAGE_SYM_DTYPE_NULL
75+
StorageClass: IMAGE_SYM_CLASS_STATIC
76+
SectionDefinition:
77+
Length: 1
78+
NumberOfRelocations: 0
79+
NumberOfLinenumbers: 0
80+
CheckSum: 40735498
81+
Number: 3
82+
Selection: IMAGE_COMDAT_SELECT_ANY
83+
- Name: .rdata
84+
Value: 0
85+
SectionNumber: 2
86+
SimpleType: IMAGE_SYM_TYPE_NULL
87+
ComplexType: IMAGE_SYM_DTYPE_NULL
88+
StorageClass: IMAGE_SYM_CLASS_STATIC
89+
SectionDefinition:
90+
Length: 1
91+
NumberOfRelocations: 0
92+
NumberOfLinenumbers: 0
93+
CheckSum: 0
94+
Number: 3
95+
Selection: IMAGE_COMDAT_SELECT_ASSOCIATIVE
96+
- Name: '.text$func2'
97+
Value: 12
98+
SectionNumber: 1
99+
SimpleType: IMAGE_SYM_TYPE_NULL
100+
ComplexType: IMAGE_SYM_DTYPE_NULL
101+
StorageClass: IMAGE_SYM_CLASS_STATIC
102+
SectionDefinition:
103+
Length: 1
104+
NumberOfRelocations: 0
105+
NumberOfLinenumbers: 0
106+
CheckSum: 40735498
107+
Number: 4
108+
Selection: IMAGE_COMDAT_SELECT_ANY
109+
- Name: .rdata
110+
Value: 1
111+
SectionNumber: 2
112+
SimpleType: IMAGE_SYM_TYPE_NULL
113+
ComplexType: IMAGE_SYM_DTYPE_NULL
114+
StorageClass: IMAGE_SYM_CLASS_STATIC
115+
SectionDefinition:
116+
Length: 1
117+
NumberOfRelocations: 0
118+
NumberOfLinenumbers: 0
119+
CheckSum: 0
120+
Number: 4
121+
Selection: IMAGE_COMDAT_SELECT_ASSOCIATIVE
122+
- Name: .debug_info
123+
Value: 0
124+
SectionNumber: 3
125+
SimpleType: IMAGE_SYM_TYPE_NULL
126+
ComplexType: IMAGE_SYM_DTYPE_NULL
127+
StorageClass: IMAGE_SYM_CLASS_STATIC
128+
SectionDefinition:
129+
Length: 4
130+
NumberOfRelocations: 0
131+
NumberOfLinenumbers: 0
132+
CheckSum: 0
133+
Number: 0
134+
...

0 commit comments

Comments
 (0)