Skip to content

Commit fa53545

Browse files
authored
[llvm-install-name-tool] Error on non-Mach-O binaries (#90351)
Previously if you passed an ELF binary it would be silently copied with no changes.
1 parent 00821fe commit fa53545

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## This test checks general llvm-install-name-tool behavior.
2+
3+
# RUN: yaml2obj %s -o %t
4+
5+
## Passing something that doesn't exist
6+
# RUN: not llvm-install-name-tool -add_rpath foo non-existent-binary 2>&1 | FileCheck %s --check-prefix=DOES_NOT_EXIST
7+
8+
# DOES_NOT_EXIST: {{.*}}non-existent-binary
9+
10+
## Passing a non-Mach-O binary
11+
# RUN: not llvm-install-name-tool -add_rpath foo %t 2>&1 | FileCheck %s --check-prefix=NON_MACH_O -DFILE=%t
12+
13+
# NON_MACH_O: error: input file: [[FILE]] is not a Mach-O file
14+
15+
--- !ELF
16+
FileHeader:
17+
Class: ELFCLASS64
18+
Data: ELFDATA2LSB
19+
Type: ET_EXEC

llvm/tools/llvm-objcopy/ObjcopyOptions.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ObjCopy/CommonConfig.h"
1616
#include "llvm/ObjCopy/ConfigManager.h"
1717
#include "llvm/ObjCopy/MachO/MachOConfig.h"
18+
#include "llvm/Object/Binary.h"
1819
#include "llvm/Option/Arg.h"
1920
#include "llvm/Option/ArgList.h"
2021
#include "llvm/Support/CRC.h"
@@ -26,6 +27,7 @@
2627

2728
using namespace llvm;
2829
using namespace llvm::objcopy;
30+
using namespace llvm::object;
2931
using namespace llvm::opt;
3032

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

1247+
Expected<OwningBinary<Binary>> BinaryOrErr =
1248+
createBinary(Config.InputFilename);
1249+
if (!BinaryOrErr)
1250+
return createFileError(Config.InputFilename, BinaryOrErr.takeError());
1251+
auto *Binary = (*BinaryOrErr).getBinary();
1252+
if (!Binary->isMachO() && !Binary->isMachOUniversalBinary())
1253+
return createStringError(errc::invalid_argument,
1254+
"input file: %s is not a Mach-O file",
1255+
Config.InputFilename.str().c_str());
1256+
12451257
DC.CopyConfigs.push_back(std::move(ConfigMgr));
12461258
return std::move(DC);
12471259
}

0 commit comments

Comments
 (0)