[lld] 4752cdc - [lld-macho] Check for arch compatibility when loading ObjFiles and TBDs
Jez Ng via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 23 19:02:53 PST 2021
Author: Jez Ng
Date: 2021-02-23T22:02:38-05:00
New Revision: 4752cdc9a20abb5fbb2a255011417b8a77e4c31e
URL: https://github.com/llvm/llvm-project/commit/4752cdc9a20abb5fbb2a255011417b8a77e4c31e
DIFF: https://github.com/llvm/llvm-project/commit/4752cdc9a20abb5fbb2a255011417b8a77e4c31e.diff
LOG: [lld-macho] Check for arch compatibility when loading ObjFiles and TBDs
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.
Reviewed By: #lld-macho, thakis, alexshap
Differential Revision: https://reviews.llvm.org/D97209
Added:
lld/test/MachO/invalid/Inputs/libincompatible.tbd
lld/test/MachO/invalid/incompatible-arch-tapi.s
lld/test/MachO/invalid/incompatible-arch.s
Modified:
lld/MachO/InputFiles.cpp
lld/test/MachO/Inputs/MacOSX.sdk/usr/lib/libc++.tbd
lld/test/MachO/header.s
Removed:
################################################################################
diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index c2026a8787e5..238d9a4a8496 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -66,6 +66,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TarWriter.h"
+#include "llvm/TextAPI/MachO/Architecture.h"
using namespace llvm;
using namespace llvm::MachO;
@@ -474,6 +475,15 @@ ObjFile::ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName)
auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
auto *hdr = reinterpret_cast<const mach_header_64 *>(mb.getBufferStart());
+ MachO::Architecture arch =
+ MachO::getArchitectureFromCpuType(hdr->cputype, hdr->cpusubtype);
+ if (arch != config->arch) {
+ error(toString(this) + " has architecture " + getArchitectureName(arch) +
+ " which is incompatible with target architecture " +
+ 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),
@@ -672,6 +682,12 @@ DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella,
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();
diff --git a/lld/test/MachO/Inputs/MacOSX.sdk/usr/lib/libc++.tbd b/lld/test/MachO/Inputs/MacOSX.sdk/usr/lib/libc++.tbd
index c4f63be6c964..0e2d21877ee3 100644
--- a/lld/test/MachO/Inputs/MacOSX.sdk/usr/lib/libc++.tbd
+++ b/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'
diff --git a/lld/test/MachO/header.s b/lld/test/MachO/header.s
index b215dc4a1bea..c96682fefba9 100644
--- a/lld/test/MachO/header.s
+++ b/lld/test/MachO/header.s
@@ -1,10 +1,11 @@
# REQUIRES: x86, arm
# 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-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/x86_64-test.o
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t/arm64-test.o
+# RUN: %lld -arch x86_64 -platform_version macos 10.5.0 11.0 -o %t/x86-64-executable %t/x86_64-test.o
+# RUN: %lld -arch arm64 -o %t/arm64-executable %t/arm64-test.o
+# RUN: %lld -arch x86_64 -dylib -o %t/x86-64-dylib %t/x86_64-test.o
+# RUN: %lld -arch arm64 -dylib -o %t/arm64-dylib %t/arm64-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
diff --git a/lld/test/MachO/invalid/Inputs/libincompatible.tbd b/lld/test/MachO/invalid/Inputs/libincompatible.tbd
new file mode 100644
index 000000000000..a07e81b0407d
--- /dev/null
+++ b/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: [ ]
diff --git a/lld/test/MachO/invalid/incompatible-arch-tapi.s b/lld/test/MachO/invalid/incompatible-arch-tapi.s
new file mode 100644
index 000000000000..0222ae473ab9
--- /dev/null
+++ b/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
diff --git a/lld/test/MachO/invalid/incompatible-arch.s b/lld/test/MachO/invalid/incompatible-arch.s
new file mode 100644
index 000000000000..3f25713b4c10
--- /dev/null
+++ b/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]] has architecture arm64 which is incompatible with target architecture x86_64
+
+.globl _main
+_main:
+ ret
More information about the llvm-commits
mailing list