[llvm] [llvm-install-name-tool] Error on non-Mach-O binaries (PR #90351)
Keith Smiley via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 29 09:49:33 PDT 2024
https://github.com/keith updated https://github.com/llvm/llvm-project/pull/90351
>From 576a13cd6038e8b100323b34436ea5656151870a Mon Sep 17 00:00:00 2001
From: Keith Smiley <keithbsmiley at gmail.com>
Date: Sat, 27 Apr 2024 13:27:22 -0400
Subject: [PATCH 1/2] [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.
---
.../llvm-objcopy/MachO/install-name-tool.test | 26 +++++++++++++++++++
llvm/tools/llvm-objcopy/ObjcopyOptions.cpp | 11 ++++++++
2 files changed, 37 insertions(+)
create mode 100644 llvm/test/tools/llvm-objcopy/MachO/install-name-tool.test
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 00000000000000..fb4cd4ae9f5e28
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/MachO/install-name-tool.test
@@ -0,0 +1,26 @@
+## This test checks general llvm-install-name-tool behavior
+
+# RUN: yaml2obj %s -o %t
+
+## 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
+
+# NON_MACH_O: llvm-install-name-tool: error: input file: {{.*}} is not a Mach-O file
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_X86_64
+Sections:
+ - Name: .bss
+ Type: SHT_NOBITS
+ Flags: [ SHF_ALLOC ]
+ AddressAlign: 0x0000000000000010
+ Size: 64
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ AddressAlign: 0x0000000000000010
+ Content: "00000000"
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index 70e85460d3df0d..929ccbeace64f4 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"
@@ -1242,6 +1243,16 @@ objcopy::parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr) {
Config.InputFilename = Positional[0];
Config.OutputFilename = Positional[0];
+ Expected<llvm::object::OwningBinary<llvm::object::Binary>> BinaryOrErr =
+ llvm::object::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);
}
>From 11bf83672063f8812b7fd539ea339d871a990d46 Mon Sep 17 00:00:00 2001
From: Keith Smiley <keithbsmiley at gmail.com>
Date: Mon, 29 Apr 2024 09:48:20 -0700
Subject: [PATCH 2/2] feedback
---
.../llvm-objcopy/MachO/install-name-tool.test | 23 +++++++------------
llvm/tools/llvm-objcopy/ObjcopyOptions.cpp | 5 ++--
2 files changed, 11 insertions(+), 17 deletions(-)
diff --git a/llvm/test/tools/llvm-objcopy/MachO/install-name-tool.test b/llvm/test/tools/llvm-objcopy/MachO/install-name-tool.test
index fb4cd4ae9f5e28..8666fa73aeba2c 100644
--- a/llvm/test/tools/llvm-objcopy/MachO/install-name-tool.test
+++ b/llvm/test/tools/llvm-objcopy/MachO/install-name-tool.test
@@ -1,26 +1,19 @@
-## This test checks general llvm-install-name-tool behavior
+## 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
+# RUN: not llvm-install-name-tool -add_rpath foo %t 2>&1 | FileCheck %s --check-prefix=NON_MACH_O -DFILE=%t
-# NON_MACH_O: llvm-install-name-tool: error: input file: {{.*}} is not a Mach-O file
+# NON_MACH_O: llvm-install-name-tool: error: input file: [[FILE]] is not a Mach-O file
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
- Machine: EM_X86_64
-Sections:
- - Name: .bss
- Type: SHT_NOBITS
- Flags: [ SHF_ALLOC ]
- AddressAlign: 0x0000000000000010
- Size: 64
- - Name: .text
- Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- AddressAlign: 0x0000000000000010
- Content: "00000000"
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index 929ccbeace64f4..a1897334cff2ed 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -27,6 +27,7 @@
using namespace llvm;
using namespace llvm::objcopy;
+using namespace llvm::object;
using namespace llvm::opt;
namespace {
@@ -1243,8 +1244,8 @@ objcopy::parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr) {
Config.InputFilename = Positional[0];
Config.OutputFilename = Positional[0];
- Expected<llvm::object::OwningBinary<llvm::object::Binary>> BinaryOrErr =
- llvm::object::createBinary(Config.InputFilename);
+ Expected<OwningBinary<Binary>> BinaryOrErr =
+ createBinary(Config.InputFilename);
if (!BinaryOrErr)
return createFileError(Config.InputFilename, BinaryOrErr.takeError());
auto *Binary = (*BinaryOrErr).getBinary();
More information about the llvm-commits
mailing list