Skip to content

Commit

Permalink
[llvm-install-name-tool] Error on non-Mach-O binaries (#90351)
Browse files Browse the repository at this point in the history
Previously if you passed an ELF binary it would be silently copied with no changes.
  • Loading branch information
keith committed May 1, 2024
1 parent 00821fe commit fa53545
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions llvm/test/tools/llvm-objcopy/MachO/install-name-tool.test
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -26,6 +27,7 @@

using namespace llvm;
using namespace llvm::objcopy;
using namespace llvm::object;
using namespace llvm::opt;

namespace {
Expand Down Expand Up @@ -1242,6 +1244,16 @@ objcopy::parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr) {
Config.InputFilename = Positional[0];
Config.OutputFilename = Positional[0];

Expected<OwningBinary<Binary>> 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);
}
Expand Down

0 comments on commit fa53545

Please sign in to comment.