diff --git a/llvm/test/tools/llvm-objcopy/MachO/install-name-tool.test b/llvm/test/tools/llvm-objcopy/MachO/install-name-tool.test new file mode 100644 index 0000000000000..b56543bd8cfc8 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/MachO/install-name-tool.test @@ -0,0 +1,19 @@ +## This test checks general llvm-install-name-tool behavior. + +# RUN: yaml2obj %s -o %t + +## Passing something that doesn't exist +# RUN: not llvm-install-name-tool -add_rpath foo non-existent-binary 2>&1 | FileCheck %s --check-prefix=DOES_NOT_EXIST + +# DOES_NOT_EXIST: {{.*}}non-existent-binary + +## Passing a non-Mach-O binary +# RUN: not llvm-install-name-tool -add_rpath foo %t 2>&1 | FileCheck %s --check-prefix=NON_MACH_O -DFILE=%t + +# NON_MACH_O: error: input file: [[FILE]] is not a Mach-O file + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp index 70e85460d3df0..a1897334cff2e 100644 --- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp +++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp @@ -15,6 +15,7 @@ #include "llvm/ObjCopy/CommonConfig.h" #include "llvm/ObjCopy/ConfigManager.h" #include "llvm/ObjCopy/MachO/MachOConfig.h" +#include "llvm/Object/Binary.h" #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" #include "llvm/Support/CRC.h" @@ -26,6 +27,7 @@ using namespace llvm; using namespace llvm::objcopy; +using namespace llvm::object; using namespace llvm::opt; namespace { @@ -1242,6 +1244,16 @@ objcopy::parseInstallNameToolOptions(ArrayRef ArgsArr) { Config.InputFilename = Positional[0]; Config.OutputFilename = Positional[0]; + Expected> BinaryOrErr = + createBinary(Config.InputFilename); + if (!BinaryOrErr) + return createFileError(Config.InputFilename, BinaryOrErr.takeError()); + auto *Binary = (*BinaryOrErr).getBinary(); + if (!Binary->isMachO() && !Binary->isMachOUniversalBinary()) + return createStringError(errc::invalid_argument, + "input file: %s is not a Mach-O file", + Config.InputFilename.str().c_str()); + DC.CopyConfigs.push_back(std::move(ConfigMgr)); return std::move(DC); }