diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h --- a/lld/MachO/InputFiles.h +++ b/lld/MachO/InputFiles.h @@ -189,13 +189,15 @@ llvm::Optional readFile(StringRef path); -template -const CommandType *findCommand(const Header *hdr, uint32_t type) { +template +const CommandType *findCommand(const Header *hdr, Types... types) { + std::initializer_list typesList{types...}; const uint8_t *p = reinterpret_cast(hdr) + sizeof(Header); for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) { auto *cmd = reinterpret_cast(p); - if (cmd->cmd == type) + if (llvm::is_contained(typesList, cmd->cmd)) return cmd; p += cmd->cmdsize; } diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -109,29 +109,31 @@ using Header = typename LP::mach_header; auto *hdr = reinterpret_cast(input->mb.getBufferStart()); + PlatformInfo platformInfo; if (const auto *cmd = findCommand(hdr, LC_BUILD_VERSION)) { platformInfo.target.Platform = static_cast(cmd->platform); platformInfo.minimum = decodeVersion(cmd->minos); return platformInfo; - } else if (const auto *cmd = - findCommand(hdr, LC_VERSION_MIN_MACOSX)) { - platformInfo.target.Platform = PlatformKind::macOS; - platformInfo.minimum = decodeVersion(cmd->version); - return platformInfo; - } else if (const auto *cmd = findCommand( - hdr, LC_VERSION_MIN_IPHONEOS)) { - platformInfo.target.Platform = PlatformKind::iOS; - platformInfo.minimum = decodeVersion(cmd->version); - return platformInfo; - } else if (const auto *cmd = - findCommand(hdr, LC_VERSION_MIN_TVOS)) { - platformInfo.target.Platform = PlatformKind::tvOS; - platformInfo.minimum = decodeVersion(cmd->version); - } else if (const auto *cmd = findCommand( - hdr, LC_VERSION_MIN_WATCHOS)) { - platformInfo.target.Platform = PlatformKind::watchOS; + } + if (const auto *cmd = findCommand( + hdr, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, + LC_VERSION_MIN_TVOS, LC_VERSION_MIN_WATCHOS)) { + switch (cmd->cmd) { + case LC_VERSION_MIN_MACOSX: + platformInfo.target.Platform = PlatformKind::macOS; + break; + case LC_VERSION_MIN_IPHONEOS: + platformInfo.target.Platform = PlatformKind::iOS; + break; + case LC_VERSION_MIN_TVOS: + platformInfo.target.Platform = PlatformKind::tvOS; + break; + case LC_VERSION_MIN_WATCHOS: + platformInfo.target.Platform = PlatformKind::watchOS; + break; + } platformInfo.minimum = decodeVersion(cmd->version); return platformInfo; } diff --git a/lld/MachO/ObjC.cpp b/lld/MachO/ObjC.cpp --- a/lld/MachO/ObjC.cpp +++ b/lld/MachO/ObjC.cpp @@ -23,8 +23,8 @@ auto *hdr = reinterpret_cast(mb.getBufferStart()); - if (const load_command *cmd = findCommand(hdr, LP::segmentLCType)) { - auto *c = reinterpret_cast(cmd); + if (const auto *c = + findCommand(hdr, LP::segmentLCType)) { auto sectionHeaders = ArrayRef
{reinterpret_cast(c + 1), c->nsects}; for (const Section &sec : sectionHeaders) {