[llvm] [llvm-install-name-tool] Error on non-Mach-O binaries (PR #90351)

Keith Smiley via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 27 15:18:42 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] [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);
 }



More information about the llvm-commits mailing list