[lld] 5bfdbde - [lld-macho] Fix cpuSubtype for non-x86_64 archs

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 22 09:13:24 PST 2021


Author: Jez Ng
Date: 2021-02-22T12:13:17-05:00
New Revision: 5bfdbdeb408a966fd882f20c6fa79f6b5eb12990

URL: https://github.com/llvm/llvm-project/commit/5bfdbdeb408a966fd882f20c6fa79f6b5eb12990
DIFF: https://github.com/llvm/llvm-project/commit/5bfdbdeb408a966fd882f20c6fa79f6b5eb12990.diff

LOG: [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

Added: 
    lld/test/MachO/header.s

Modified: 
    lld/MachO/SyntheticSections.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index ca7f08239d04..3281f116833c 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -53,11 +53,23 @@ uint64_t MachHeaderSection::getSize() const {
   return sizeof(MachO::mach_header_64) + sizeOfCmds + config->headerPad;
 }
 
+static uint32_t cpuSubtype() {
+  uint32_t subtype = target->cpuSubtype;
+
+  if (config->outputType == MachO::MH_EXECUTE && !config->staticLink &&
+      target->cpuSubtype == MachO::CPU_SUBTYPE_X86_64_ALL &&
+      config->platform.kind == MachO::PlatformKind::macOS &&
+      config->platform.minimum >= VersionTuple(10, 5))
+    subtype |= MachO::CPU_SUBTYPE_LIB64;
+
+  return subtype;
+}
+
 void MachHeaderSection::writeTo(uint8_t *buf) const {
   auto *hdr = reinterpret_cast<MachO::mach_header_64 *>(buf);
   hdr->magic = MachO::MH_MAGIC_64;
   hdr->cputype = target->cpuType;
-  hdr->cpusubtype = target->cpuSubtype | MachO::CPU_SUBTYPE_LIB64;
+  hdr->cpusubtype = cpuSubtype();
   hdr->filetype = config->outputType;
   hdr->ncmds = loadCommands.size();
   hdr->sizeofcmds = sizeOfCmds;

diff  --git a/lld/test/MachO/header.s b/lld/test/MachO/header.s
new file mode 100644
index 000000000000..58565bccc437
--- /dev/null
+++ b/lld/test/MachO/header.s
@@ -0,0 +1,18 @@
+# RUN: rm -rf %t && mkdir -p %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
+# RUN: %lld -arch x86_64 -platform_version macos 10.5.0 11.0 -o %t/x86-64-executable %t/test.o
+# RUN: %lld -arch arm64 -o %t/arm64-executable %t/test.o
+# RUN: %lld -arch x86_64 -dylib -o %t/x86-64-dylib %t/test.o
+# RUN: %lld -arch arm64  -dylib -o %t/arm64-dylib %t/test.o
+
+# RUN: llvm-objdump --macho --all-headers %t/x86-64-executable | FileCheck %s -DCAPS=LIB64
+# RUN: llvm-objdump --macho --all-headers %t/arm64-executable | FileCheck %s -DCAPS=0x00
+# RUN: llvm-objdump --macho --all-headers %t/x86-64-dylib | FileCheck %s -DCAPS=0x00
+# RUN: llvm-objdump --macho --all-headers %t/arm64-dylib | FileCheck %s -DCAPS=0x00
+
+# CHECK:      magic        cputype cpusubtype  caps    filetype
+# CHECK-NEXT: MH_MAGIC_64  {{.*}}         ALL  [[CAPS]]   {{.*}}
+
+.globl _main
+_main:
+  ret


        


More information about the llvm-commits mailing list