[llvm] 94fba14 - [llvm] add -r functionality to llvm-bitcode-strip
Daniel RodrÃguez Troitiño via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 4 13:28:36 PST 2022
Author: Richard Howell
Date: 2022-03-04T13:28:28-08:00
New Revision: 94fba14f97233f77f30ded2fc3c2bfca9f2ce6fe
URL: https://github.com/llvm/llvm-project/commit/94fba14f97233f77f30ded2fc3c2bfca9f2ce6fe
DIFF: https://github.com/llvm/llvm-project/commit/94fba14f97233f77f30ded2fc3c2bfca9f2ce6fe.diff
LOG: [llvm] add -r functionality to llvm-bitcode-strip
This diff adds functionality to the llvm-bitcode-strip tool for
stripping of LLVM bitcode sections.
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D120669
Added:
llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-basic.test
llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-remove.test
Modified:
llvm/tools/llvm-objcopy/BitcodeStripOpts.td
llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
llvm/tools/llvm-objcopy/ObjcopyOptions.h
llvm/tools/llvm-objcopy/llvm-objcopy.cpp
Removed:
llvm/test/tools/llvm-objcopy/MachO/bitcode-strip.test
################################################################################
diff --git a/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip.test b/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-basic.test
similarity index 78%
rename from llvm/test/tools/llvm-objcopy/MachO/bitcode-strip.test
rename to llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-basic.test
index 4ea7c9db5ccd1..75ed3a7d3afed 100644
--- a/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip.test
+++ b/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-basic.test
@@ -1,10 +1,11 @@
-## Test output flag is required.
+## Test output flag and action flag are required.
# RUN: yaml2obj %s -o %t
-# RUN: not llvm-bitcode-strip %t 2>&1 | FileCheck --check-prefix=MISSING-ARG %s
-# RUN: llvm-bitcode-strip %t -o %t2
-# RUN: cmp %t %t2
+# RUN: not llvm-bitcode-strip %t 2>&1 | FileCheck --check-prefix=MISSING-OUTPUT %s
+# RUN: not llvm-bitcode-strip %t -o %t2 2>&1 | FileCheck --check-prefix=MISSING-ACTION %s
+# RUN: llvm-bitcode-strip -r %t -o %t2
-# MISSING-ARG: error: -o is a required argument
+# MISSING-OUTPUT: error: -o is a required argument
+# MISSING-ACTION: error: no action specified
--- !mach-o
FileHeader:
diff --git a/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-remove.test b/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-remove.test
new file mode 100644
index 0000000000000..a7acd17004549
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-remove.test
@@ -0,0 +1,87 @@
+## Test bitcode section removal.
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-bitcode-strip -r %t -o %t2
+# RUN: llvm-readobj --sections %t2 | FileCheck --implicit-check-not=Name: %s
+
+# CHECK: Name: __text
+# CHECK-NEXT: Segment: __TEXT
+# CHECK: Name: __bundle
+# CHECK-NEXT: Segment: __DATA
+# CHECK: Name: __notbundle
+# CHECK-NEXT: Segment: __LLVM
+
+--- !mach-o
+FileHeader:
+ magic: 0xFEEDFACF
+ cputype: 0x01000007
+ cpusubtype: 0x00000003
+ filetype: 0x00000001
+ ncmds: 1
+ sizeofcmds: 392
+ flags: 0x00002000
+ reserved: 0x00000000
+LoadCommands:
+ - cmd: LC_SEGMENT_64
+ cmdsize: 392
+ segname: ''
+ vmaddr: 0
+ vmsize: 16
+ fileoff: 424
+ filesize: 16
+ maxprot: 7
+ initprot: 7
+ nsects: 4
+ flags: 0
+ Sections:
+ - sectname: __text
+ segname: __TEXT
+ addr: 0x0000000000000000
+ content: 'AABBCCDD'
+ size: 4
+ offset: 424
+ align: 0
+ reloff: 0x00000000
+ nreloc: 0
+ flags: 0x80000400
+ reserved1: 0x00000000
+ reserved2: 0x00000000
+ reserved3: 0x00000000
+ - sectname: __bundle
+ segname: __DATA
+ addr: 0x0000000000000004
+ content: 'DDAADDAA'
+ size: 4
+ offset: 428
+ align: 0
+ reloff: 0x00000000
+ nreloc: 0
+ flags: 0x00000000
+ reserved1: 0x00000000
+ reserved2: 0x00000000
+ reserved3: 0x00000000
+ - sectname: __bundle
+ segname: __LLVM
+ addr: 0x0000000000000008
+ content: 'EEFFEEFF'
+ size: 4
+ offset: 432
+ align: 0
+ reloff: 0x00000000
+ nreloc: 0
+ flags: 0x00000000
+ reserved1: 0x00000000
+ reserved2: 0x00000000
+ reserved3: 0x00000000
+ - sectname: __notbundle
+ segname: __LLVM
+ addr: 0x0000000000000008
+ content: 'EEFFEEFF'
+ size: 4
+ offset: 436
+ align: 0
+ reloff: 0x00000000
+ nreloc: 0
+ flags: 0x00000000
+ reserved1: 0x00000000
+ reserved2: 0x00000000
+ reserved3: 0x00000000
diff --git a/llvm/tools/llvm-objcopy/BitcodeStripOpts.td b/llvm/tools/llvm-objcopy/BitcodeStripOpts.td
index 8f945899e8077..21db854b1e6fe 100644
--- a/llvm/tools/llvm-objcopy/BitcodeStripOpts.td
+++ b/llvm/tools/llvm-objcopy/BitcodeStripOpts.td
@@ -23,5 +23,8 @@ def V : Flag<["-"], "V">,
Alias<version>,
HelpText<"Alias for --version">;
+def remove : Flag<["-"], "r">,
+ HelpText<"Remove the __LLVM bitcode segment entirely">;
+
def output : JoinedOrSeparate<["-"], "o">, HelpText<"Write output to <file>">,
MetaVarName<"<file>">;
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index c627095205b65..6457be21fa4d4 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -12,6 +12,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/BinaryFormat/COFF.h"
+#include "llvm/ObjCopy/CommonConfig.h"
#include "llvm/ObjCopy/ConfigManager.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
@@ -1183,7 +1184,8 @@ objcopy::parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr) {
}
Expected<DriverConfig>
-objcopy::parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr) {
+objcopy::parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr,
+ function_ref<Error(Error)> ErrorCallback) {
DriverConfig DC;
ConfigManager ConfigMgr;
CommonConfig &Config = ConfigMgr.Common;
@@ -1228,6 +1230,13 @@ objcopy::parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr) {
}
Config.OutputFilename = InputArgs.getLastArgValue(BITCODE_STRIP_output);
+ if (!InputArgs.hasArg(BITCODE_STRIP_remove))
+ return createStringError(errc::invalid_argument, "no action specified");
+
+ // We only support -r for now, which removes all bitcode sections.
+ cantFail(Config.ToRemove.addMatcher(NameOrPattern::create(
+ "__LLVM,__bundle", MatchStyle::Literal, ErrorCallback)));
+
DC.CopyConfigs.push_back(std::move(ConfigMgr));
return std::move(DC);
}
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.h b/llvm/tools/llvm-objcopy/ObjcopyOptions.h
index d5c1fad10b7ab..f7fa2af304d77 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.h
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.h
@@ -41,7 +41,9 @@ parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr);
// ParseBitcodeStripOptions returns the config and sets the input arguments.
// If a help flag is set then ParseBitcodeStripOptions will print the help
// messege and exit.
-Expected<DriverConfig> parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr);
+Expected<DriverConfig>
+parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr,
+ llvm::function_ref<Error(Error)> ErrorCallback);
// ParseStripOptions returns the config and sets the input arguments. If a
// help flag is set then ParseStripOptions will print the help messege and
diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
index 26484d5d8e5b1..00712664eab27 100644
--- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
+++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -86,7 +86,7 @@ static Expected<DriverConfig> getDriverConfig(ArrayRef<const char *> Args) {
};
if (Is("bitcode-strip") || Is("bitcode_strip"))
- return parseBitcodeStripOptions(Args);
+ return parseBitcodeStripOptions(Args, reportWarning);
else if (Is("strip"))
return parseStripOptions(Args, reportWarning);
else if (Is("install-name-tool") || Is("install_name_tool"))
More information about the llvm-commits
mailing list