[llvm] [llvm-intsall-name-tool] Error on non-Mach-O binaries (PR #90351)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 27 10:28:44 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-binary-utilities
Author: Keith Smiley (keith)
<details>
<summary>Changes</summary>
Previously if you passed an ELF binary it would be silently copied with no changes.
---
Full diff: https://github.com/llvm/llvm-project/pull/90351.diff
2 Files Affected:
- (added) llvm/test/tools/llvm-objcopy/MachO/install-name-tool.test (+26)
- (modified) llvm/tools/llvm-objcopy/ObjcopyOptions.cpp (+11)
``````````diff
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);
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/90351
More information about the llvm-commits
mailing list