[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
This commit is contained in:
Richard Howell 2022-03-04 13:24:37 -08:00 committed by Daniel Rodríguez Troitiño
parent e1895a46dc
commit 94fba14f97
6 changed files with 110 additions and 8 deletions

View File

@ -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:

View File

@ -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

View File

@ -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>">;

View File

@ -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);
}

View File

@ -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

View File

@ -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"))