[lld] 7d4f70f - Reland 3nd attempt: [lld-macho] Fix bug in reading cpuSubType field.

Vy Nguyen via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 17 20:36:01 PST 2022


Author: Vy Nguyen
Date: 2022-12-17T23:35:50-05:00
New Revision: 7d4f70f8e598a41a4b673294de84ef3ee1cb333e

URL: https://github.com/llvm/llvm-project/commit/7d4f70f8e598a41a4b673294de84ef3ee1cb333e
DIFF: https://github.com/llvm/llvm-project/commit/7d4f70f8e598a41a4b673294de84ef3ee1cb333e.diff

LOG: Reland 3nd attempt:  [lld-macho] Fix bug in reading cpuSubType field.

This reverts commit 09c5aab7f88178c1af92de0b00417416da9db6c1.

New changes:
Temporarily skip checking the output bundle's cpu/cpu-subtype
Suspected there's a bug in selecting targets which caused the produced bundle
to be arm64 bundle (even though it was specifically linked with -arch x86_64).

The current test that link succeeded should be sufficient, because
it would have failed with "unable to find matching tagets" prior to this patch.

Differential Revision: https://reviews.llvm.org/D139572

Added: 
    

Modified: 
    lld/MachO/InputFiles.cpp
    lld/test/MachO/fat-arch.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 54a02f264b851..174ad959c5bcd 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -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);

diff  --git a/lld/test/MachO/fat-arch.s b/lld/test/MachO/fat-arch.s
index 33928b1049be1..5609ee56e2d01 100644
--- a/lld/test/MachO/fat-arch.s
+++ b/lld/test/MachO/fat-arch.s
@@ -1,16 +1,40 @@
-# 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 -arch x86_64 %t.x86_64.o -bundle_loader %t.fat.exec.out -bundle -o %t.fat.bundle
+
+## FIXME: Re-enable this test, which is failing on arm64, once we figured out why
+## %t.fat.bundle is produced as an arm64.
+# RUN llvm-otool -h %t.fat.bundle -f %t.fat.exec.out | FileCheck %s --check-prefix=CPU-SUB
+# CPU-SUB: Fat headers
+# CPU-SUB: nfat_arch 2
+# CPU-SUB: architecture 0
+# CPU-SUB-NEXT:    cputype 16777223
+# CPU-SUB-NEXT:    cpusubtype 3
+# CPU-SUB: architecture 1
+# CPU-SUB-NEXT:    cputype 16777228
+# CPU-SUB-NEXT:    cpusubtype 0
+
+# CPU-SUB:            magic     cputype      cpusubtype   caps      filetype   ncmds sizeofcmds      flags
+# CPU-SUB-NEXT:  0xfeedfacf     16777223              3  0x{{.+}}    {{.+}}  {{.+}}    {{.+}}      {{.+}}
+
 .text
 .global _main
 _main:
-  mov $0, %eax
   ret


        


More information about the llvm-commits mailing list