diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp --- 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 @@ auto *buf = reinterpret_cast(mb.getBufferStart()); auto *hdr = reinterpret_cast(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(cmd); StringRef data{reinterpret_cast(c + 1), @@ -672,6 +682,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(); diff --git a/lld/test/MachO/Inputs/MacOSX.sdk/usr/lib/libc++.tbd b/lld/test/MachO/Inputs/MacOSX.sdk/usr/lib/libc++.tbd --- 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 --- 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 --- /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 --- /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 --- /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