Skip to content

Commit 8e22720

Browse files
committed
[llvm-intsall-name-tool] Error on non-Mach-O binaries
Previously if you passed an ELF binary it would be silently copied with no changes.
1 parent b7248d5 commit 8e22720

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## This test checks general llvm-install-name-tool behavior
2+
3+
# RUN: yaml2obj %s -o %t
4+
5+
## Passing a non-Mach-O binary
6+
# RUN: not llvm-install-name-tool -add_rpath foo %t 2>&1 | FileCheck %s --check-prefix=NON_MACH_O
7+
8+
# NON_MACH_O: llvm-install-name-tool: error: input file: {{.*}} is not a Mach-O file
9+
10+
--- !ELF
11+
FileHeader:
12+
Class: ELFCLASS64
13+
Data: ELFDATA2LSB
14+
Type: ET_EXEC
15+
Machine: EM_X86_64
16+
Sections:
17+
- Name: .bss
18+
Type: SHT_NOBITS
19+
Flags: [ SHF_ALLOC ]
20+
AddressAlign: 0x0000000000000010
21+
Size: 64
22+
- Name: .text
23+
Type: SHT_PROGBITS
24+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
25+
AddressAlign: 0x0000000000000010
26+
Content: "00000000"

llvm/tools/llvm-objcopy/ObjcopyOptions.cpp

Lines changed: 11 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"
@@ -1242,6 +1243,16 @@ objcopy::parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr) {
12421243
Config.InputFilename = Positional[0];
12431244
Config.OutputFilename = Positional[0];
12441245

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

0 commit comments

Comments
 (0)