Skip to content

Commit 52337d5

Browse files
authored
llvm-objdump: ensure a MachO symbol isn't STAB before looking up secion (#86667)
The section field has been repurposed for some STAB symbol types, and if we blindly look it up we'll produce an error and terminate. Logic already existed Existing stabs test had a section that was in range. Unfortunately I don't know of an easy way to produce stabs entries in LLVM (I thought they died in the 90s until this came up) so I just binary-edited it to cause a failure on existing llvm-objdump.
1 parent 7493ea2 commit 52337d5

File tree

2 files changed

+273
-10
lines changed

2 files changed

+273
-10
lines changed
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
# Check we don't error out on MachO files with stabs symbol entries that
2+
# repurpose the section field so it's out of range for normal symbols.
3+
4+
# RUN: yaml2obj %s -o %t
5+
# RUN: llvm-objdump --syms %t | FileCheck %s
6+
7+
# CHECK: 0000000000000000 d *UND*
8+
--- !mach-o
9+
FileHeader:
10+
magic: 0xFEEDFACF
11+
cputype: 0x100000C
12+
cpusubtype: 0x0
13+
filetype: 0x2
14+
ncmds: 16
15+
sizeofcmds: 744
16+
flags: 0x200085
17+
reserved: 0x0
18+
LoadCommands:
19+
- cmd: LC_SEGMENT_64
20+
cmdsize: 72
21+
segname: __PAGEZERO
22+
vmaddr: 0
23+
vmsize: 4294967296
24+
fileoff: 0
25+
filesize: 0
26+
maxprot: 0
27+
initprot: 0
28+
nsects: 0
29+
flags: 0
30+
- cmd: LC_SEGMENT_64
31+
cmdsize: 232
32+
segname: __TEXT
33+
vmaddr: 4294967296
34+
vmsize: 16384
35+
fileoff: 0
36+
filesize: 16384
37+
maxprot: 5
38+
initprot: 5
39+
nsects: 2
40+
flags: 0
41+
Sections:
42+
- sectname: __text
43+
segname: __TEXT
44+
addr: 0x100003FA0
45+
size: 8
46+
offset: 0x3FA0
47+
align: 2
48+
reloff: 0x0
49+
nreloc: 0
50+
flags: 0x80000400
51+
reserved1: 0x0
52+
reserved2: 0x0
53+
reserved3: 0x0
54+
content: 00008052C0035FD6
55+
- sectname: __unwind_info
56+
segname: __TEXT
57+
addr: 0x100003FA8
58+
size: 88
59+
offset: 0x3FA8
60+
align: 2
61+
reloff: 0x0
62+
nreloc: 0
63+
flags: 0x0
64+
reserved1: 0x0
65+
reserved2: 0x0
66+
reserved3: 0x0
67+
content: 010000001C000000000000001C000000000000001C00000002000000A03F00004000000040000000A83F00000000000040000000000000000000000000000000030000000C00010010000100000000000000000200000000
68+
- cmd: LC_SEGMENT_64
69+
cmdsize: 72
70+
segname: __LINKEDIT
71+
vmaddr: 4294983680
72+
vmsize: 16384
73+
fileoff: 16384
74+
filesize: 688
75+
maxprot: 1
76+
initprot: 1
77+
nsects: 0
78+
flags: 0
79+
- cmd: LC_DYLD_CHAINED_FIXUPS
80+
cmdsize: 16
81+
dataoff: 16384
82+
datasize: 56
83+
- cmd: LC_DYLD_EXPORTS_TRIE
84+
cmdsize: 16
85+
dataoff: 16440
86+
datasize: 48
87+
- cmd: LC_SYMTAB
88+
cmdsize: 24
89+
symoff: 16496
90+
nsyms: 11
91+
stroff: 16672
92+
strsize: 120
93+
- cmd: LC_DYSYMTAB
94+
cmdsize: 80
95+
ilocalsym: 0
96+
nlocalsym: 9
97+
iextdefsym: 9
98+
nextdefsym: 2
99+
iundefsym: 11
100+
nundefsym: 0
101+
tocoff: 0
102+
ntoc: 0
103+
modtaboff: 0
104+
nmodtab: 0
105+
extrefsymoff: 0
106+
nextrefsyms: 0
107+
indirectsymoff: 0
108+
nindirectsyms: 0
109+
extreloff: 0
110+
nextrel: 0
111+
locreloff: 0
112+
nlocrel: 0
113+
- cmd: LC_LOAD_DYLINKER
114+
cmdsize: 32
115+
name: 12
116+
Content: '/usr/lib/dyld'
117+
ZeroPadBytes: 7
118+
- cmd: LC_UUID
119+
cmdsize: 24
120+
uuid: 73BD5072-2F45-39FD-8013-8102F002C240
121+
- cmd: LC_BUILD_VERSION
122+
cmdsize: 32
123+
platform: 1
124+
minos: 917504
125+
sdk: 918528
126+
ntools: 1
127+
Tools:
128+
- tool: 3
129+
version: 72942592
130+
- cmd: LC_SOURCE_VERSION
131+
cmdsize: 16
132+
version: 0
133+
- cmd: LC_MAIN
134+
cmdsize: 24
135+
entryoff: 16288
136+
stacksize: 0
137+
- cmd: LC_LOAD_DYLIB
138+
cmdsize: 56
139+
dylib:
140+
name: 24
141+
timestamp: 2
142+
current_version: 88171522
143+
compatibility_version: 65536
144+
Content: '/usr/lib/libSystem.B.dylib'
145+
ZeroPadBytes: 6
146+
- cmd: LC_FUNCTION_STARTS
147+
cmdsize: 16
148+
dataoff: 16488
149+
datasize: 8
150+
- cmd: LC_DATA_IN_CODE
151+
cmdsize: 16
152+
dataoff: 16496
153+
datasize: 0
154+
- cmd: LC_CODE_SIGNATURE
155+
cmdsize: 16
156+
dataoff: 16800
157+
datasize: 272
158+
LinkEditData:
159+
ExportTrie:
160+
TerminalSize: 0
161+
NodeOffset: 0
162+
Name: ''
163+
Flags: 0x0
164+
Address: 0x0
165+
Other: 0x0
166+
ImportName: ''
167+
Children:
168+
- TerminalSize: 0
169+
NodeOffset: 18
170+
Name: _
171+
Flags: 0x0
172+
Address: 0x0
173+
Other: 0x0
174+
ImportName: ''
175+
Children:
176+
- TerminalSize: 2
177+
NodeOffset: 9
178+
Name: _mh_execute_header
179+
Flags: 0x0
180+
Address: 0x0
181+
Other: 0x0
182+
ImportName: ''
183+
- TerminalSize: 3
184+
NodeOffset: 13
185+
Name: main
186+
Flags: 0x0
187+
Address: 0x3FA0
188+
Other: 0x0
189+
ImportName: ''
190+
NameList:
191+
- n_strx: 1
192+
n_type: 0x64
193+
# This line has been changed from a real object file to move the section
194+
# out of valid range for normal symbols.
195+
n_sect: 42
196+
n_desc: 0
197+
n_value: 0
198+
- n_strx: 28
199+
n_type: 0x64
200+
n_sect: 0
201+
n_desc: 0
202+
n_value: 0
203+
- n_strx: 68
204+
n_type: 0x64
205+
n_sect: 0
206+
n_desc: 0
207+
n_value: 0
208+
- n_strx: 74
209+
n_type: 0x66
210+
n_sect: 0
211+
n_desc: 1
212+
n_value: 1713522175
213+
- n_strx: 1
214+
n_type: 0x2E
215+
n_sect: 1
216+
n_desc: 0
217+
n_value: 4294983584
218+
- n_strx: 22
219+
n_type: 0x24
220+
n_sect: 1
221+
n_desc: 0
222+
n_value: 4294983584
223+
- n_strx: 1
224+
n_type: 0x24
225+
n_sect: 0
226+
n_desc: 0
227+
n_value: 8
228+
- n_strx: 1
229+
n_type: 0x4E
230+
n_sect: 1
231+
n_desc: 0
232+
n_value: 4294983584
233+
- n_strx: 1
234+
n_type: 0x64
235+
n_sect: 1
236+
n_desc: 0
237+
n_value: 0
238+
- n_strx: 2
239+
n_type: 0xF
240+
n_sect: 1
241+
n_desc: 16
242+
n_value: 4294967296
243+
- n_strx: 22
244+
n_type: 0xF
245+
n_sect: 1
246+
n_desc: 0
247+
n_value: 4294983584
248+
StringTable:
249+
- ' '
250+
- __mh_execute_header
251+
- _main
252+
- '/Users/tim/llvm/llvm-project/build.rel/'
253+
- tmp.c
254+
- '/Users/tim/llvm/llvm-project/build.rel/tmp.o'
255+
- ''
256+
FunctionStarts: [ 0x3FA0 ]
257+
ChainedFixups: [ 0x0, 0x0, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x30, 0x0,
258+
0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
259+
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
260+
0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
261+
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
262+
0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
263+
...

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,16 +2885,6 @@ void Dumper::printSymbol(const SymbolRef &Symbol,
28852885
reportUniqueWarning(AddrOrErr.takeError());
28862886
return;
28872887
}
2888-
uint64_t Address = *AddrOrErr;
2889-
section_iterator SecI = unwrapOrError(Symbol.getSection(), FileName);
2890-
if (SecI != O.section_end() && shouldAdjustVA(*SecI))
2891-
Address += AdjustVMA;
2892-
if ((Address < StartAddress) || (Address > StopAddress))
2893-
return;
2894-
SymbolRef::Type Type =
2895-
unwrapOrError(Symbol.getType(), FileName, ArchiveName, ArchitectureName);
2896-
uint32_t Flags =
2897-
unwrapOrError(Symbol.getFlags(), FileName, ArchiveName, ArchitectureName);
28982888

28992889
// Don't ask a Mach-O STAB symbol for its section unless you know that
29002890
// STAB symbol's section field refers to a valid section index. Otherwise
@@ -2913,6 +2903,16 @@ void Dumper::printSymbol(const SymbolRef &Symbol,
29132903
: unwrapOrError(Symbol.getSection(), FileName,
29142904
ArchiveName, ArchitectureName);
29152905

2906+
uint64_t Address = *AddrOrErr;
2907+
if (Section != O.section_end() && shouldAdjustVA(*Section))
2908+
Address += AdjustVMA;
2909+
if ((Address < StartAddress) || (Address > StopAddress))
2910+
return;
2911+
SymbolRef::Type Type =
2912+
unwrapOrError(Symbol.getType(), FileName, ArchiveName, ArchitectureName);
2913+
uint32_t Flags =
2914+
unwrapOrError(Symbol.getFlags(), FileName, ArchiveName, ArchitectureName);
2915+
29162916
StringRef Name;
29172917
if (Type == SymbolRef::ST_Debug && Section != O.section_end()) {
29182918
if (Expected<StringRef> NameOrErr = Section->getName())

0 commit comments

Comments
 (0)