Skip to content

Commit

Permalink
Reland [lld-macho] Fix bug in reading cpuSubType field.
Browse files Browse the repository at this point in the history
This reverts commit 66692c8.

New changes:
  - update test to require aarch64
  - update test to not hard-code cpu[sub] type values (since they could change)
  - update test to temporarily skip windows (because llvm-mc on windows doesn't seem to work with triple arm64-apple-macos)

Differential Revision: https://reviews.llvm.org/D139572
  • Loading branch information
oontvoo committed Dec 9, 2022
1 parent 981a28f commit f472da1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
10 changes: 8 additions & 2 deletions lld/MachO/InputFiles.cpp
Expand Up @@ -231,8 +231,14 @@ std::optional<MemoryBufferRef> macho::readFile(StringRef path) {
return std::nullopt;
}

if (read32be(&arch[i].cputype) != static_cast<uint32_t>(target->cpuType) ||
read32be(&arch[i].cpusubtype) != target->cpuSubtype)
uint32_t cpuType = read32be(&arch[i].cputype);
uint32_t cpuSubtype =
read32be(&arch[i].cpusubtype) & ~MachO::CPU_SUBTYPE_MASK;

// FIXME: LD64 has a more complex fallback logic here.
// Consider implementing that as well?
if (cpuType != static_cast<uint32_t>(target->cpuType) ||
cpuSubtype != target->cpuSubtype)
continue;

uint32_t offset = read32be(&arch[i].offset);
Expand Down
22 changes: 19 additions & 3 deletions lld/test/MachO/fat-arch.s
@@ -1,16 +1,32 @@
# REQUIRES: x86
# REQUIRES: x86,aarch64
## FIXME: The tests doesn't run on windows right now because of llvm-mc (can't produce triple=arm64-apple-macos11.0)
# UNSUPPORTED: system-windows

# RUN: llvm-mc -filetype=obj -triple=i386-apple-darwin %s -o %t.i386.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.x86_64.o
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-macos11.0 %s -o %t.arm64.o

# RUN: llvm-lipo %t.i386.o %t.x86_64.o -create -o %t.fat.o
# RUN: %lld -o /dev/null %t.fat.o

# RUN: llvm-lipo %t.i386.o -create -o %t.noarch.o
# RUN: not %lld -o /dev/null %t.noarch.o 2>&1 | \
# RUN: FileCheck %s -DFILE=%t.noarch.o
# CHECK: error: unable to find matching architecture in [[FILE]]

## Validates that we read the cpu-subtype correctly from a fat exec.
# RUN: %lld -o %t.x86_64.out %t.x86_64.o
# RUN: %lld -arch arm64 -o %t.arm64.out %t.arm64.o
# RUN: llvm-lipo %t.x86_64.out %t.arm64.out -create -o %t.fat.exec.out
# RUN: %lld %t.x86_64.o -bundle_loader %t.fat.exec.out -bundle -o %t.fat.bundle

# RUN: llvm-otool -h %t.fat.exec.out %t.fat.bundle| FileCheck %s --check-prefix=CPU-SUB
# CPU-SUB: magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
# CPU-SUB-NEXT: 0xfeedfacf [[#CPU:]] [[#CPU_SUB:]] 0x{{.+}} {{.+}} {{.+}} {{.+}} {{.+}}
# CPU-SUB-NEXT: Mach header
# CPU-SUB-NEXT: magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
# CPU-SUB-NEXT: 0xfeedfacf [[#CPU]] [[#CPU_SUB]] 0x{{.+}} {{.+}} {{.+}} {{.+}} {{.+}}

.text
.global _main
_main:
mov $0, %eax
ret

0 comments on commit f472da1

Please sign in to comment.