Skip to content

Commit d6a486c

Browse files
authored
[ORC] Apply MachO::CPU_SUBTYPE_MASK to comparison in getDylibInterfac… (#145154)
…eFromDylib. When comparing CPU subtypes from slices in a MachO universal binary we need to apply the MachO::CPU_SUBTYPE_MASK to mask out any flags in the high bits, otherwise we might fail to correctly match a slice and return a spurious "does not contain slice" error. rdar://153913779
1 parent 2f5d965 commit d6a486c

File tree

3 files changed

+371
-1
lines changed

3 files changed

+371
-1
lines changed

llvm/lib/ExecutionEngine/Orc/GetDylibInterface.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ Expected<SymbolNameSet> getDylibInterfaceFromDylib(ExecutionSession &ES,
4141
else if (auto *MachOUni =
4242
dyn_cast<object::MachOUniversalBinary>(BinFile->get())) {
4343
for (auto &O : MachOUni->objects()) {
44-
if (O.getCPUType() == *CPUType && O.getCPUSubType() == *CPUSubType) {
44+
if (O.getCPUType() == *CPUType &&
45+
(O.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) == *CPUSubType) {
4546
if (auto Obj = O.getAsObjectFile())
4647
MachOFile = std::move(*Obj);
4748
else
Lines changed: 350 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,350 @@
1+
--- !fat-mach-o
2+
3+
# Contains two slices (arm64e and x86-64) for libfoo.dylib, each containing the
4+
# following:
5+
#
6+
# int foo(void) { return 0; }
7+
8+
FatHeader:
9+
magic: 0xCAFEBABE
10+
nfat_arch: 2
11+
FatArchs:
12+
- cputype: 0x1000007
13+
cpusubtype: 0x3
14+
offset: 0x1000
15+
size: 4200
16+
align: 12
17+
- cputype: 0x100000C
18+
cpusubtype: 0x80000002
19+
offset: 0x4000
20+
size: 16784
21+
align: 14
22+
Slices:
23+
- !mach-o
24+
FileHeader:
25+
magic: 0xFEEDFACF
26+
cputype: 0x1000007
27+
cpusubtype: 0x3
28+
filetype: 0x6
29+
ncmds: 13
30+
sizeofcmds: 568
31+
flags: 0x100085
32+
reserved: 0x0
33+
LoadCommands:
34+
- cmd: LC_SEGMENT_64
35+
cmdsize: 152
36+
segname: __TEXT
37+
vmaddr: 0
38+
vmsize: 4096
39+
fileoff: 0
40+
filesize: 4096
41+
maxprot: 5
42+
initprot: 5
43+
nsects: 1
44+
flags: 0
45+
Sections:
46+
- sectname: __text
47+
segname: __TEXT
48+
addr: 0x2A0
49+
size: 3
50+
offset: 0x2A0
51+
align: 4
52+
reloff: 0x0
53+
nreloc: 0
54+
flags: 0x80000400
55+
reserved1: 0x0
56+
reserved2: 0x0
57+
reserved3: 0x0
58+
content: 31C0C3
59+
- cmd: LC_SEGMENT_64
60+
cmdsize: 72
61+
segname: __LINKEDIT
62+
vmaddr: 4096
63+
vmsize: 4096
64+
fileoff: 4096
65+
filesize: 104
66+
maxprot: 1
67+
initprot: 1
68+
nsects: 0
69+
flags: 0
70+
- cmd: LC_ID_DYLIB
71+
cmdsize: 48
72+
dylib:
73+
name: 24
74+
timestamp: 1
75+
current_version: 0
76+
compatibility_version: 0
77+
Content: libfoo.x86-64.dylib
78+
ZeroPadBytes: 5
79+
- cmd: LC_DYLD_CHAINED_FIXUPS
80+
cmdsize: 16
81+
dataoff: 4096
82+
datasize: 48
83+
- cmd: LC_DYLD_EXPORTS_TRIE
84+
cmdsize: 16
85+
dataoff: 4144
86+
datasize: 24
87+
- cmd: LC_SYMTAB
88+
cmdsize: 24
89+
symoff: 4176
90+
nsyms: 1
91+
stroff: 4192
92+
strsize: 8
93+
- cmd: LC_DYSYMTAB
94+
cmdsize: 80
95+
ilocalsym: 0
96+
nlocalsym: 0
97+
iextdefsym: 0
98+
nextdefsym: 1
99+
iundefsym: 1
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_UUID
114+
cmdsize: 24
115+
uuid: A29E87C8-EF8B-3721-B0D3-9749DEBEEBBB
116+
- cmd: LC_BUILD_VERSION
117+
cmdsize: 32
118+
platform: 1
119+
minos: 983040
120+
sdk: 984320
121+
ntools: 1
122+
Tools:
123+
- tool: 3
124+
version: 76481792
125+
- cmd: LC_SOURCE_VERSION
126+
cmdsize: 16
127+
version: 0
128+
- cmd: LC_LOAD_DYLIB
129+
cmdsize: 56
130+
dylib:
131+
name: 24
132+
timestamp: 2
133+
current_version: 88539136
134+
compatibility_version: 65536
135+
Content: '/usr/lib/libSystem.B.dylib'
136+
ZeroPadBytes: 6
137+
- cmd: LC_FUNCTION_STARTS
138+
cmdsize: 16
139+
dataoff: 4168
140+
datasize: 8
141+
- cmd: LC_DATA_IN_CODE
142+
cmdsize: 16
143+
dataoff: 4176
144+
datasize: 0
145+
LinkEditData:
146+
ExportTrie:
147+
TerminalSize: 0
148+
NodeOffset: 0
149+
Name: ''
150+
Flags: 0x0
151+
Address: 0x0
152+
Other: 0x0
153+
ImportName: ''
154+
Children:
155+
- TerminalSize: 3
156+
NodeOffset: 12
157+
Name: _foo
158+
Flags: 0x0
159+
Address: 0x2A0
160+
Other: 0x0
161+
ImportName: ''
162+
NameList:
163+
- n_strx: 2
164+
n_type: 0xF
165+
n_sect: 1
166+
n_desc: 0
167+
n_value: 672
168+
StringTable:
169+
- ' '
170+
- _foo
171+
- ''
172+
FunctionStarts: [ 0x2A0 ]
173+
ChainedFixups: [ 0x0, 0x0, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x2C,
174+
0x0, 0x0, 0x0, 0x2C, 0x0, 0x0, 0x0, 0x0, 0x0,
175+
0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
176+
0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0,
177+
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
178+
- !mach-o
179+
FileHeader:
180+
magic: 0xFEEDFACF
181+
cputype: 0x100000C
182+
cpusubtype: 0x80000002
183+
filetype: 0x6
184+
ncmds: 14
185+
sizeofcmds: 664
186+
flags: 0x100085
187+
reserved: 0x0
188+
LoadCommands:
189+
- cmd: LC_SEGMENT_64
190+
cmdsize: 232
191+
segname: __TEXT
192+
vmaddr: 0
193+
vmsize: 16384
194+
fileoff: 0
195+
filesize: 16384
196+
maxprot: 5
197+
initprot: 5
198+
nsects: 2
199+
flags: 0
200+
Sections:
201+
- sectname: __text
202+
segname: __TEXT
203+
addr: 0x2D8
204+
size: 8
205+
offset: 0x2D8
206+
align: 2
207+
reloff: 0x0
208+
nreloc: 0
209+
flags: 0x80000400
210+
reserved1: 0x0
211+
reserved2: 0x0
212+
reserved3: 0x0
213+
content: 00008052C0035FD6
214+
- sectname: __unwind_info
215+
segname: __TEXT
216+
addr: 0x2E0
217+
size: 88
218+
offset: 0x2E0
219+
align: 2
220+
reloff: 0x0
221+
nreloc: 0
222+
flags: 0x0
223+
reserved1: 0x0
224+
reserved2: 0x0
225+
reserved3: 0x0
226+
content: 010000001C000000000000001C000000000000001C00000002000000D80200004000000040000000E00200000000000040000000000000000000000000000000030000000C00010010000100000000000000000200000000
227+
- cmd: LC_SEGMENT_64
228+
cmdsize: 72
229+
segname: __LINKEDIT
230+
vmaddr: 16384
231+
vmsize: 16384
232+
fileoff: 16384
233+
filesize: 400
234+
maxprot: 1
235+
initprot: 1
236+
nsects: 0
237+
flags: 0
238+
- cmd: LC_ID_DYLIB
239+
cmdsize: 48
240+
dylib:
241+
name: 24
242+
timestamp: 1
243+
current_version: 0
244+
compatibility_version: 0
245+
Content: libfoo.arm64e.dylib
246+
ZeroPadBytes: 5
247+
- cmd: LC_DYLD_CHAINED_FIXUPS
248+
cmdsize: 16
249+
dataoff: 16384
250+
datasize: 48
251+
- cmd: LC_DYLD_EXPORTS_TRIE
252+
cmdsize: 16
253+
dataoff: 16432
254+
datasize: 24
255+
- cmd: LC_SYMTAB
256+
cmdsize: 24
257+
symoff: 16464
258+
nsyms: 1
259+
stroff: 16480
260+
strsize: 8
261+
- cmd: LC_DYSYMTAB
262+
cmdsize: 80
263+
ilocalsym: 0
264+
nlocalsym: 0
265+
iextdefsym: 0
266+
nextdefsym: 1
267+
iundefsym: 1
268+
nundefsym: 0
269+
tocoff: 0
270+
ntoc: 0
271+
modtaboff: 0
272+
nmodtab: 0
273+
extrefsymoff: 0
274+
nextrefsyms: 0
275+
indirectsymoff: 0
276+
nindirectsyms: 0
277+
extreloff: 0
278+
nextrel: 0
279+
locreloff: 0
280+
nlocrel: 0
281+
- cmd: LC_UUID
282+
cmdsize: 24
283+
uuid: 0220C592-2D73-3642-B893-B7DA8FCD396C
284+
- cmd: LC_BUILD_VERSION
285+
cmdsize: 32
286+
platform: 1
287+
minos: 983040
288+
sdk: 984320
289+
ntools: 1
290+
Tools:
291+
- tool: 3
292+
version: 76481792
293+
- cmd: LC_SOURCE_VERSION
294+
cmdsize: 16
295+
version: 0
296+
- cmd: LC_LOAD_DYLIB
297+
cmdsize: 56
298+
dylib:
299+
name: 24
300+
timestamp: 2
301+
current_version: 88539136
302+
compatibility_version: 65536
303+
Content: '/usr/lib/libSystem.B.dylib'
304+
ZeroPadBytes: 6
305+
- cmd: LC_FUNCTION_STARTS
306+
cmdsize: 16
307+
dataoff: 16456
308+
datasize: 8
309+
- cmd: LC_DATA_IN_CODE
310+
cmdsize: 16
311+
dataoff: 16464
312+
datasize: 0
313+
- cmd: LC_CODE_SIGNATURE
314+
cmdsize: 16
315+
dataoff: 16496
316+
datasize: 288
317+
LinkEditData:
318+
ExportTrie:
319+
TerminalSize: 0
320+
NodeOffset: 0
321+
Name: ''
322+
Flags: 0x0
323+
Address: 0x0
324+
Other: 0x0
325+
ImportName: ''
326+
Children:
327+
- TerminalSize: 3
328+
NodeOffset: 12
329+
Name: _foo
330+
Flags: 0x0
331+
Address: 0x2D8
332+
Other: 0x0
333+
ImportName: ''
334+
NameList:
335+
- n_strx: 2
336+
n_type: 0xF
337+
n_sect: 1
338+
n_desc: 0
339+
n_value: 728
340+
StringTable:
341+
- ' '
342+
- _foo
343+
- ''
344+
FunctionStarts: [ 0x2D8 ]
345+
ChainedFixups: [ 0x0, 0x0, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x2C,
346+
0x0, 0x0, 0x0, 0x2C, 0x0, 0x0, 0x0, 0x0, 0x0,
347+
0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
348+
0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0,
349+
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
350+
...
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: rm -rf %t && mkdir %t
2+
; RUN: yaml2obj -o %t/libFoo.dylib %S/Inputs/MachO_Universal_libFoo_dylib.yaml
3+
; RUN: llc -filetype=obj -mtriple arm64e-apple-macosx -o %t/main.o %s
4+
; RUN: llvm-jitlink -noexec -triple arm64e-apple-macosx %t/main.o -weak_library \
5+
; RUN: %t/libFoo.dylib
6+
;
7+
; REQUIRES: x86-registered-target && aarch64-registered-target
8+
;
9+
; Check MachO universal binary handling in the orc::getDylibInterfaceFromDylib
10+
; function, including that the cpusubtype field is masked correctly (for arm64e
11+
; slices this field will have the MachO::CPU_SUBTYPE_LIB64 flag set in the high
12+
; bits -- the subtype will fail to match unless it's masked out).
13+
14+
declare i32 @foo()
15+
16+
define i32 @main(i32 %argc, ptr %argv) {
17+
entry:
18+
ret i32 ptrtoint (ptr @foo to i32)
19+
}

0 commit comments

Comments
 (0)