Skip to content

Commit 5bfdbde

Browse files
committed
[lld-macho] Fix cpuSubtype for non-x86_64 archs
dyld on iOS will complain if the LIB64 bit is set. Reviewed By: #lld-macho, thakis Differential Revision: https://reviews.llvm.org/D96565
1 parent 784c70d commit 5bfdbde

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lld/MachO/SyntheticSections.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,23 @@ uint64_t MachHeaderSection::getSize() const {
5353
return sizeof(MachO::mach_header_64) + sizeOfCmds + config->headerPad;
5454
}
5555

56+
static uint32_t cpuSubtype() {
57+
uint32_t subtype = target->cpuSubtype;
58+
59+
if (config->outputType == MachO::MH_EXECUTE && !config->staticLink &&
60+
target->cpuSubtype == MachO::CPU_SUBTYPE_X86_64_ALL &&
61+
config->platform.kind == MachO::PlatformKind::macOS &&
62+
config->platform.minimum >= VersionTuple(10, 5))
63+
subtype |= MachO::CPU_SUBTYPE_LIB64;
64+
65+
return subtype;
66+
}
67+
5668
void MachHeaderSection::writeTo(uint8_t *buf) const {
5769
auto *hdr = reinterpret_cast<MachO::mach_header_64 *>(buf);
5870
hdr->magic = MachO::MH_MAGIC_64;
5971
hdr->cputype = target->cpuType;
60-
hdr->cpusubtype = target->cpuSubtype | MachO::CPU_SUBTYPE_LIB64;
72+
hdr->cpusubtype = cpuSubtype();
6173
hdr->filetype = config->outputType;
6274
hdr->ncmds = loadCommands.size();
6375
hdr->sizeofcmds = sizeOfCmds;

lld/test/MachO/header.s

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# RUN: rm -rf %t && mkdir -p %t
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
3+
# RUN: %lld -arch x86_64 -platform_version macos 10.5.0 11.0 -o %t/x86-64-executable %t/test.o
4+
# RUN: %lld -arch arm64 -o %t/arm64-executable %t/test.o
5+
# RUN: %lld -arch x86_64 -dylib -o %t/x86-64-dylib %t/test.o
6+
# RUN: %lld -arch arm64 -dylib -o %t/arm64-dylib %t/test.o
7+
8+
# RUN: llvm-objdump --macho --all-headers %t/x86-64-executable | FileCheck %s -DCAPS=LIB64
9+
# RUN: llvm-objdump --macho --all-headers %t/arm64-executable | FileCheck %s -DCAPS=0x00
10+
# RUN: llvm-objdump --macho --all-headers %t/x86-64-dylib | FileCheck %s -DCAPS=0x00
11+
# RUN: llvm-objdump --macho --all-headers %t/arm64-dylib | FileCheck %s -DCAPS=0x00
12+
13+
# CHECK: magic cputype cpusubtype caps filetype
14+
# CHECK-NEXT: MH_MAGIC_64 {{.*}} ALL [[CAPS]] {{.*}}
15+
16+
.globl _main
17+
_main:
18+
ret

0 commit comments

Comments
 (0)