diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h --- a/lld/MachO/InputFiles.h +++ b/lld/MachO/InputFiles.h @@ -175,8 +175,20 @@ llvm::Optional readFile(StringRef path); -const llvm::MachO::load_command * -findCommand(const llvm::MachO::mach_header_64 *, uint32_t type); +template +const CommandType *findCommand(const llvm::MachO::mach_header_64 *hdr, + uint32_t type) { + const uint8_t *p = reinterpret_cast(hdr) + + sizeof(llvm::MachO::mach_header_64); + + for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) { + auto *cmd = reinterpret_cast(p); + if (cmd->cmd == type) + return cmd; + p += cmd->cmdsize; + } + return nullptr; +} } // namespace macho diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -149,20 +149,6 @@ return None; } -const load_command *macho::findCommand(const mach_header_64 *hdr, - uint32_t type) { - const uint8_t *p = - reinterpret_cast(hdr) + sizeof(mach_header_64); - - for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) { - auto *cmd = reinterpret_cast(p); - if (cmd->cmd == type) - return cmd; - p += cmd->cmdsize; - } - return nullptr; -} - void ObjFile::parseSections(ArrayRef sections) { subsections.reserve(sections.size()); auto *buf = reinterpret_cast(mb.getBufferStart()); @@ -352,6 +338,47 @@ /*isExternal=*/false, /*isPrivateExtern=*/false); } +// Checks if the version specified in `cmd` is compatible with target +// version in `config`. IOW, check if cmd's version >= config's version. +// +// prefixFn: a function that returns a prefix string describing which object cmd +// belongs to. +template +static bool hasCompatVersion(PrefixFn prefixFn, + const build_version_command *cmd, + const Configuration *config) { + + if (static_cast(config->target.Platform) != cmd->platform) { + error(prefixFn() + " has platform " + + getPlatformName(static_cast(cmd->platform)) + + Twine(", which is different from target platform ") + + getPlatformName(config->target.Platform)); + return false; + } + + unsigned major = cmd->minos >> 16; + unsigned minor = (cmd->minos >> 8) & 0xffu; + unsigned subMinor = cmd->minos & 0xffu; + + const VersionTuple &minVersion = config->platformInfo.minimum; + if (major > minVersion.getMajor() || + (major == minVersion.getMajor() && + (minor > minVersion.getMinor() || + (minor == minVersion.getMinor() && + subMinor >= minVersion.getSubminor())))) + return true; + + // Prepare the human-readable string for error message. + std::string rep; + llvm::raw_string_ostream out(rep); + out << major << "." << minor << "." << subMinor; + + error(prefixFn() + " has version " + rep + + ", which is incompatible with target version of " + + config->platformInfo.minimum.getAsString()); + return false; +} + // Absolute symbols are defined symbols that do not have an associated // InputSection. They cannot be weak. static macho::Symbol *createAbsolute(const structs::nlist_64 &sym, @@ -492,7 +519,13 @@ getArchitectureName(config->target.Arch)); return; } - // TODO: check platform too + + if (const build_version_command *cmd = + findCommand(hdr, LC_BUILD_VERSION)) { + if (!hasCompatVersion([&]() -> std::string { return toString(this); }, cmd, + config)) + return; + } if (const load_command *cmd = findCommand(hdr, LC_LINKER_OPTION)) { auto *c = reinterpret_cast(cmd); @@ -648,6 +681,14 @@ return; } + if (const build_version_command *cmd = + findCommand(hdr, LC_BUILD_VERSION)) { + if (!hasCompatVersion( + [&]() -> std::string { return "dylib " + toString(this); }, cmd, + config)) + return; + } + // Initialize symbols. DylibFile *exportingFile = isImplicitlyLinked(dylibName) ? this : umbrella; if (const load_command *cmd = findCommand(hdr, LC_DYLD_INFO_ONLY)) { diff --git a/lld/test/MachO/incompat-version.s b/lld/test/MachO/incompat-version.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/incompat-version.s @@ -0,0 +1,17 @@ +# REQUIRES: x86 + +# RUN: rm -rf %t && mkdir -p %t +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o +# RUN: %lld -dylib -arch x86_64 -platform_version macOS 9.0 11.0 -o %t/out.dylib %t/test.o -lSystem + +# RUN: not %lld -dylib -arch x86_64 -platform_version macOS 14.0 15.0 -o %t/new.dylib %t/out.dylib -lSystem \ +# RUN: -o /dev/null 2>&1 | FileCheck %s --check-prefix=VERSION +# VERSION: dylib {{.*}}out.dylib has version 9.0.0, which is incompatible with target version of 14.0 + +# RUN: not %lld -dylib -arch x86_64 -platform_version iOS 9.0 11.0 -o %t/new.dylib %t/out.dylib -lSystem \ +# RUN: -o /dev/null 2>&1 | FileCheck %s --check-prefix=PLAT +# PLAT: dylib {{.*}}out.dylib has platform macOS, which is different from target platform iOS +.text +.global _main +_main: + ret