diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h --- a/lld/MachO/InputFiles.h +++ b/lld/MachO/InputFiles.h @@ -140,6 +140,9 @@ InputFile(Kind, const llvm::MachO::InterfaceFile &); + // If true, this input's arch is compatiable with target. + bool compatArch = true; + private: const Kind fileKind; const StringRef name; diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -185,6 +185,29 @@ return true; } +template +static bool compatWithTargetArch(const InputFile *file, const Header *hdr) { + uint32_t cpuType; + std::tie(cpuType, std::ignore) = getCPUTypeFromArchitecture(config->arch()); + + if (hdr->cputype != cpuType) { + Architecture arch = + getArchitectureFromCpuType(hdr->cputype, hdr->cpusubtype); + auto msg = config->errorForArchMismatch + ? static_cast(error) + : warn; + + msg(toString(file) + " has architecture " + getArchitectureName(arch) + + " which is incompatible with target architecture " + + getArchitectureName(config->arch())); + return false; + } + + if (!checkCompatibility(file)) + return false; + return true; +} + // This cache mostly exists to store system libraries (and .tbds) as they're // loaded, rather than the input archives, which are already cached at a higher // level, and other files like the filelist that are only read once. @@ -955,21 +978,10 @@ auto *buf = reinterpret_cast(mb.getBufferStart()); auto *hdr = reinterpret_cast(mb.getBufferStart()); - uint32_t cpuType; - std::tie(cpuType, std::ignore) = getCPUTypeFromArchitecture(config->arch()); - if (hdr->cputype != cpuType) { - Architecture arch = - getArchitectureFromCpuType(hdr->cputype, hdr->cpusubtype); - auto msg = config->errorForArchMismatch - ? static_cast(error) - : warn; - msg(toString(this) + " has architecture " + getArchitectureName(arch) + - " which is incompatible with target architecture " + - getArchitectureName(config->arch())); + // If we've already checked the arch, then don't need to check again. + if (!compatArch) return; - } - - if (!checkCompatibility(this)) + if (!(compatArch = compatWithTargetArch(this, hdr))) return; for (auto *cmd : findCommands(hdr, LC_LINKER_OPTION)) { @@ -1026,6 +1038,12 @@ auto *buf = reinterpret_cast(mb.getBufferStart()); auto *hdr = reinterpret_cast(mb.getBufferStart()); + + if (!compatArch) + return; + if (!(compatArch = compatWithTargetArch(this, hdr))) + return; + const load_command *cmd = findCommand(hdr, LC_SYMTAB); if (!cmd) return; @@ -2089,6 +2107,33 @@ forceHidden(forceHidden) {} void ArchiveFile::addLazySymbols() { + Error err = Error::success(); + Expected mbOrErr = + this->file->child_begin(err)->getMemoryBufferRef(); + + // Ignore the I/O error here - will be reported later. + if (!err && mbOrErr) { + if (identify_magic(mbOrErr->getBuffer()) == file_magic::macho_object) { + if (target->wordSize == 8) + compatArch = compatWithTargetArch( + this, reinterpret_cast( + mbOrErr->getBufferStart())); + else + compatArch = compatWithTargetArch( + this, reinterpret_cast( + mbOrErr->getBufferStart())); + + if (!compatArch) + return; + } + } else { + if (!mbOrErr) + llvm::handleAllErrors(mbOrErr.takeError(), + [](const llvm::ErrorInfoBase &EI) { + // do nothing + }); + } + for (const object::Archive::Symbol &sym : file->symbols()) symtab->addLazyArchive(sym.getName(), this, sym); } diff --git a/lld/MachO/Symbols.h b/lld/MachO/Symbols.h --- a/lld/MachO/Symbols.h +++ b/lld/MachO/Symbols.h @@ -58,14 +58,14 @@ virtual uint64_t getVA() const { return 0; } - virtual bool isWeakDef() const { llvm_unreachable("cannot be weak def"); } + virtual bool isWeakDef() const { return false; } // Only undefined or dylib symbols can be weak references. A weak reference // need not be satisfied at runtime, e.g. due to the symbol not being // available on a given target platform. virtual bool isWeakRef() const { return false; } - virtual bool isTlv() const { llvm_unreachable("cannot be TLV"); } + virtual bool isTlv() const { return false; } // Whether this symbol is in the GOT or TLVPointer sections. bool isInGot() const { return gotIndex != UINT32_MAX; } diff --git a/lld/test/MachO/ignore-incompat-arch.s b/lld/test/MachO/ignore-incompat-arch.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/ignore-incompat-arch.s @@ -0,0 +1,72 @@ +## Test that LLD correctly ignored archives with incompatible architecture without crashing. + +# RUN: rm -rf %t; split-file %s %t + +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-macos %t/callee.s -o %t/callee_arm64.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %t/callee.s -o %t/callee_x86_64.o + +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-macos %t/caller.s -o %t/caller_arm64.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %t/caller.s -o %t/caller_x86_64.o + +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-macos %t/main.s -o %t/main_arm64.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %t/main.s -o %t/main_x86_64.o + +# RUN: llvm-ar r %t/libcallee_arm64.a %t/callee_arm64.o +# RUN: llvm-ar r %t/libcallee_x86.a %t/callee_x86_64.o + +# RUN: llvm-ar r %t/libcaller_arm64.a %t/caller_arm64.o +# RUN: llvm-ar r %t/libcaller_x86.a %t/caller_x86_64.o + +## Symbol from the arm64 archive should be ignored even tho it appears before the x86 archive. +# RUN: %no-fatal-warnings-lld -map %t/x86_a.map -arch x86_64 %t/main_x86_64.o %t/libcallee_arm64.a %t/libcallee_x86.a %t/libcaller_x86.a -o %t/x86_a.out 2>&1 \ +# RUN: | FileCheck -check-prefix=X86-WARNING %s + +# RUN: %no-fatal-warnings-lld -map %t/x86_b.map -arch x86_64 %t/main_x86_64.o %t/libcallee_x86.a %t/libcallee_arm64.a %t/libcaller_x86.a -o %t/x86_b.out 2>&1 \ +# RUN: | FileCheck -check-prefix=X86-WARNING %s + +# RUN: %no-fatal-warnings-lld -map %t/arm64_a.map -arch arm64 %t/main_arm64.o %t/libcallee_x86.a %t/libcallee_arm64.a %t/libcaller_arm64.a -o %t/arm64_a.out 2>&1 \ +# RUN: | FileCheck -check-prefix=ARM64-WARNING %s + +# RUN: %no-fatal-warnings-lld -map %t/arm64_b.map -arch arm64 %t/main_arm64.o %t/libcallee_arm64.a %t/libcallee_x86.a %t/libcaller_arm64.a -o %t/arm64_b.out 2>&1 \ +# RUN: | FileCheck -check-prefix=ARM64-WARNING %s + +## Verify that the output doesn't take any symbol from the in-compat archive +# RUN: cat %t/x86_a.map | FileCheck --check-prefix=SYM-X86 %s +# RUN: cat %t/x86_b.map | FileCheck --check-prefix=SYM-X86 %s + +# RUN: cat %t/arm64_a.map | FileCheck --check-prefix=SYM-ARM64 %s +# RUN: cat %t/arm64_b.map | FileCheck --check-prefix=SYM-ARM64 %s + + +# X86-WARNING: libcallee_arm64.a has architecture arm64 which is incompatible with target architecture x86_64 + +# ARM64-WARNING: libcallee_x86.a has architecture x86_64 which is incompatible with target architecture arm64 + +# SYM-X86-NOT: libcallee_arm64.a +# SYM-X86: {{.+}}main_x86_64.o +# SYM-X86: {{.+}}libcallee_x86.a(callee_x86_64.o) +# SYM-X86: {{.+}}libcaller_x86.a(caller_x86_64.o) + +# SYM-ARM64-NOT: libcallee_x86.a +# SYM-ARM64: {{.+}}main_arm64.o +# SYM-ARM64: {{.+}}libcallee_arm64.a(callee_arm64.o) +# SYM-ARM64: {{.+}}libcaller_arm64.a(caller_arm64.o) + + +#--- callee.s +.globl _callee +_callee: + ret + +#--- caller.s +.globl _caller +_caller: + .quad _callee + ret + +#--- main.s +.globl _main +_main: + .quad _caller + ret + diff --git a/lld/test/MachO/objc.s b/lld/test/MachO/objc.s --- a/lld/test/MachO/objc.s +++ b/lld/test/MachO/objc.s @@ -14,15 +14,19 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o -# RUN: %lld -lSystem %t/test.o -o %t/test -L%t -lHasSomeObjC -ObjC +# RUN: %lld -lSystem %t/test.o -o %t/test -L%t -lHasSomeObjC -ObjC # RUN: llvm-objdump --section-headers --syms %t/test | FileCheck %s --check-prefix=OBJC # RUN: %lld -lSystem %t/test.o -o %t/test -L%t -lHasSomeObjC2 -ObjC # RUN: llvm-objdump --section-headers --syms %t/test | FileCheck %s --check-prefix=OBJC -# RUN: %lld -lSystem %t/test.o -o %t/test --start-lib %t/no-objc.o %t/has-objc-symbol.o %t/has-objc-category.o %t/has-swift.o %t/has-swift-proto.o %t/wrong-arch.o --end-lib -ObjC +# RUN: %no-fatal-warnings-lld -lSystem %t/test.o -o %t/test --start-lib %t/no-objc.o %t/has-objc-symbol.o %t/has-objc-category.o %t/has-swift.o %t/has-swift-proto.o %t/wrong-arch.o --end-lib -ObjC 2>&1 \ +# RUN: | FileCheck -check-prefix=WARNING %s # RUN: llvm-objdump --section-headers --syms %t/test | FileCheck %s --check-prefix=OBJC +# WARNING: {{.+}}wrong-arch.o has architecture armv7 which is incompatible with target architecture x86_64 +# WARNING-EMPTY: + # OBJC: Sections: # OBJC-NEXT: Idx Name Size VMA Type # OBJC-NEXT: 0 __text {{.*}} TEXT