[PATCH] D97209: [lld-macho] Check for arch compatibility when loading ObjFiles and TBDs

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 22 11:13:26 PST 2021


int3 created this revision.
int3 added a reviewer: lld-macho.
Herald added a project: lld-macho.
int3 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The silent failures had confused me a few times.

I haven't added a similar check for platform yet as we don't yet have logic to
infer the platform automatically, and so adding that check would require
updating dozens of test files.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97209

Files:
  lld/MachO/InputFiles.cpp
  lld/test/MachO/Inputs/MacOSX.sdk/usr/lib/libc++.tbd
  lld/test/MachO/invalid/Inputs/libincompatible.tbd
  lld/test/MachO/invalid/incompatible-arch-tapi.s
  lld/test/MachO/invalid/incompatible-arch.s


Index: lld/test/MachO/invalid/incompatible-arch.s
===================================================================
--- /dev/null
+++ lld/test/MachO/invalid/incompatible-arch.s
@@ -0,0 +1,7 @@
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t.o
+# RUN: not %lld -arch x86_64 -lSystem %t.o -o /dev/null 2>&1 | FileCheck %s -DFILE=%t.o
+# CHECK: error: {{.*}}[[FILE]] is incompatible with x86_64
+
+.globl _main
+_main:
+  ret
Index: lld/test/MachO/invalid/incompatible-arch-tapi.s
===================================================================
--- /dev/null
+++ lld/test/MachO/invalid/incompatible-arch-tapi.s
@@ -0,0 +1,9 @@
+# RUN: rm -rf %t; split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macosx %t/main.s -o %t/main.o
+# RUN: not %lld -arch x86_64 -lSystem %S/Inputs/libincompatible.tbd %t/main.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ARCH
+# ARCH: error: {{.*}}libincompatible.tbd is incompatible with x86_64
+
+#--- main.s
+.globl _main
+_main:
+  ret
Index: lld/test/MachO/invalid/Inputs/libincompatible.tbd
===================================================================
--- /dev/null
+++ lld/test/MachO/invalid/Inputs/libincompatible.tbd
@@ -0,0 +1,9 @@
+--- !tapi-tbd-v3
+archs:            [ arm64 ]
+uuids:            [ 'arm64: 00000000-0000-0000-0000-000000000000' ]
+platform:         macosx
+install-name:     '/usr/lib/libincompatible.dylib'
+current-version:  0001.001.1
+exports:
+  - archs:        [ 'x86_64', 'arm64' ]
+    symbols:      [ ]
Index: lld/test/MachO/Inputs/MacOSX.sdk/usr/lib/libc++.tbd
===================================================================
--- lld/test/MachO/Inputs/MacOSX.sdk/usr/lib/libc++.tbd
+++ lld/test/MachO/Inputs/MacOSX.sdk/usr/lib/libc++.tbd
@@ -1,5 +1,5 @@
 --- !tapi-tbd-v3
-archs:            [ i386, x86_64 ]
+archs:            [ i386, x86_64, arm64 ]
 uuids:            [ 'i386: 00000000-0000-0000-0000-000000000000', 'x86_64: 00000000-0000-0000-0000-000000000001', 'arm64: 00000000-0000-0000-0000-000000000002' ]
 platform:         macosx
 install-name:     '/usr/lib/libc++.dylib'
Index: lld/MachO/InputFiles.cpp
===================================================================
--- lld/MachO/InputFiles.cpp
+++ lld/MachO/InputFiles.cpp
@@ -481,6 +481,13 @@
   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
   auto *hdr = reinterpret_cast<const mach_header_64 *>(mb.getBufferStart());
 
+  if (hdr->cputype != target->cpuType ||
+      hdr->cpusubtype != target->cpuSubtype) {
+    error(toString(this) + " is incompatible with " +
+          getArchitectureName(config->arch));
+    return;
+  }
+
   if (const load_command *cmd = findCommand(hdr, LC_LINKER_OPTION)) {
     auto *c = reinterpret_cast<const linker_option_command *>(cmd);
     StringRef data{reinterpret_cast<const char *>(c + 1),
@@ -669,6 +676,12 @@
   if (umbrella == nullptr)
     umbrella = this;
 
+  if (!interface.getArchitectures().has(config->arch)) {
+    error(toString(this) + " is incompatible with " +
+          getArchitectureName(config->arch));
+    return;
+  }
+
   dylibName = saver.save(interface.getInstallName());
   compatibilityVersion = interface.getCompatibilityVersion().rawValue();
   currentVersion = interface.getCurrentVersion().rawValue();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97209.325501.patch
Type: text/x-patch
Size: 3287 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210222/2967877a/attachment.bin>


More information about the llvm-commits mailing list