Skip to content

Commit b08acee

Browse files
committed
Reland 2nd attempt: [lld-macho] Fix bug in reading cpuSubType field.
This reverts commit 52a118d. New changes: Fix tests to dump both slices in the fat-archive because otool isn't deterministic about which slice it prints across different archs. (It printed x86 on x86 machines but arm64 on arm64, this was why the test failed on arm64) Differential Revision: https://reviews.llvm.org/D139572
1 parent 0359c19 commit b08acee

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lld/MachO/InputFiles.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,14 @@ std::optional<MemoryBufferRef> macho::readFile(StringRef path) {
231231
return std::nullopt;
232232
}
233233

234-
if (read32be(&arch[i].cputype) != static_cast<uint32_t>(target->cpuType) ||
235-
read32be(&arch[i].cpusubtype) != target->cpuSubtype)
234+
uint32_t cpuType = read32be(&arch[i].cputype);
235+
uint32_t cpuSubtype =
236+
read32be(&arch[i].cpusubtype) & ~MachO::CPU_SUBTYPE_MASK;
237+
238+
// FIXME: LD64 has a more complex fallback logic here.
239+
// Consider implementing that as well?
240+
if (cpuType != static_cast<uint32_t>(target->cpuType) ||
241+
cpuSubtype != target->cpuSubtype)
236242
continue;
237243

238244
uint32_t offset = read32be(&arch[i].offset);

lld/test/MachO/fat-arch.s

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
1-
# REQUIRES: x86
1+
# REQUIRES: x86,aarch64
2+
## FIXME: The tests doesn't run on windows right now because of llvm-mc (can't produce triple=arm64-apple-macos11.0)
3+
# UNSUPPORTED: system-windows
4+
25
# RUN: llvm-mc -filetype=obj -triple=i386-apple-darwin %s -o %t.i386.o
36
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.x86_64.o
7+
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-macos11.0 %s -o %t.arm64.o
8+
49
# RUN: llvm-lipo %t.i386.o %t.x86_64.o -create -o %t.fat.o
510
# RUN: %lld -o /dev/null %t.fat.o
6-
711
# RUN: llvm-lipo %t.i386.o -create -o %t.noarch.o
812
# RUN: not %lld -o /dev/null %t.noarch.o 2>&1 | \
913
# RUN: FileCheck %s -DFILE=%t.noarch.o
1014
# CHECK: error: unable to find matching architecture in [[FILE]]
1115

16+
## Validates that we read the cpu-subtype correctly from a fat exec.
17+
# RUN: %lld -o %t.x86_64.out %t.x86_64.o
18+
# RUN: %lld -arch arm64 -o %t.arm64.out %t.arm64.o
19+
# RUN: llvm-lipo %t.x86_64.out %t.arm64.out -create -o %t.fat.exec.out
20+
# RUN: %lld %t.x86_64.o -bundle_loader %t.fat.exec.out -bundle -o %t.fat.bundle
21+
22+
# RUN: llvm-otool -h %t.fat.bundle -f %t.fat.exec.out | FileCheck %s --check-prefix=CPU-SUB
23+
# CPU-SUB: Fat headers
24+
# CPU-SUB: nfat_arch 2
25+
# CPU-SUB: architecture 0
26+
# CPU-SUB-NEXT: cputype 16777223
27+
# CPU-SUB-NEXT: cpusubtype 3
28+
# CPU-SUB: architecture 1
29+
# CPU-SUB-NEXT: cputype 16777228
30+
# CPU-SUB-NEXT: cpusubtype 0
31+
32+
# CPU-SUB: magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
33+
# CPU-SUB-NEXT: 0xfeedfacf 16777223 3 0x{{.+}} {{.+}} {{.+}} {{.+}} {{.+}}
34+
1235
.text
1336
.global _main
1437
_main:
15-
mov $0, %eax
1638
ret

0 commit comments

Comments
 (0)