Skip to content

Commit 94fba14

Browse files
rmazdrodriguez
authored andcommitted
[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
1 parent e1895a4 commit 94fba14

File tree

6 files changed

+110
-8
lines changed

6 files changed

+110
-8
lines changed

llvm/test/tools/llvm-objcopy/MachO/bitcode-strip.test renamed to llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-basic.test

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
## Test output flag is required.
1+
## Test output flag and action flag are required.
22
# RUN: yaml2obj %s -o %t
3-
# RUN: not llvm-bitcode-strip %t 2>&1 | FileCheck --check-prefix=MISSING-ARG %s
4-
# RUN: llvm-bitcode-strip %t -o %t2
5-
# RUN: cmp %t %t2
3+
# RUN: not llvm-bitcode-strip %t 2>&1 | FileCheck --check-prefix=MISSING-OUTPUT %s
4+
# RUN: not llvm-bitcode-strip %t -o %t2 2>&1 | FileCheck --check-prefix=MISSING-ACTION %s
5+
# RUN: llvm-bitcode-strip -r %t -o %t2
66

7-
# MISSING-ARG: error: -o is a required argument
7+
# MISSING-OUTPUT: error: -o is a required argument
8+
# MISSING-ACTION: error: no action specified
89

910
--- !mach-o
1011
FileHeader:
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
## Test bitcode section removal.
2+
# RUN: yaml2obj %s -o %t
3+
# RUN: llvm-bitcode-strip -r %t -o %t2
4+
# RUN: llvm-readobj --sections %t2 | FileCheck --implicit-check-not=Name: %s
5+
6+
# CHECK: Name: __text
7+
# CHECK-NEXT: Segment: __TEXT
8+
# CHECK: Name: __bundle
9+
# CHECK-NEXT: Segment: __DATA
10+
# CHECK: Name: __notbundle
11+
# CHECK-NEXT: Segment: __LLVM
12+
13+
--- !mach-o
14+
FileHeader:
15+
magic: 0xFEEDFACF
16+
cputype: 0x01000007
17+
cpusubtype: 0x00000003
18+
filetype: 0x00000001
19+
ncmds: 1
20+
sizeofcmds: 392
21+
flags: 0x00002000
22+
reserved: 0x00000000
23+
LoadCommands:
24+
- cmd: LC_SEGMENT_64
25+
cmdsize: 392
26+
segname: ''
27+
vmaddr: 0
28+
vmsize: 16
29+
fileoff: 424
30+
filesize: 16
31+
maxprot: 7
32+
initprot: 7
33+
nsects: 4
34+
flags: 0
35+
Sections:
36+
- sectname: __text
37+
segname: __TEXT
38+
addr: 0x0000000000000000
39+
content: 'AABBCCDD'
40+
size: 4
41+
offset: 424
42+
align: 0
43+
reloff: 0x00000000
44+
nreloc: 0
45+
flags: 0x80000400
46+
reserved1: 0x00000000
47+
reserved2: 0x00000000
48+
reserved3: 0x00000000
49+
- sectname: __bundle
50+
segname: __DATA
51+
addr: 0x0000000000000004
52+
content: 'DDAADDAA'
53+
size: 4
54+
offset: 428
55+
align: 0
56+
reloff: 0x00000000
57+
nreloc: 0
58+
flags: 0x00000000
59+
reserved1: 0x00000000
60+
reserved2: 0x00000000
61+
reserved3: 0x00000000
62+
- sectname: __bundle
63+
segname: __LLVM
64+
addr: 0x0000000000000008
65+
content: 'EEFFEEFF'
66+
size: 4
67+
offset: 432
68+
align: 0
69+
reloff: 0x00000000
70+
nreloc: 0
71+
flags: 0x00000000
72+
reserved1: 0x00000000
73+
reserved2: 0x00000000
74+
reserved3: 0x00000000
75+
- sectname: __notbundle
76+
segname: __LLVM
77+
addr: 0x0000000000000008
78+
content: 'EEFFEEFF'
79+
size: 4
80+
offset: 436
81+
align: 0
82+
reloff: 0x00000000
83+
nreloc: 0
84+
flags: 0x00000000
85+
reserved1: 0x00000000
86+
reserved2: 0x00000000
87+
reserved3: 0x00000000

llvm/tools/llvm-objcopy/BitcodeStripOpts.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ def V : Flag<["-"], "V">,
2323
Alias<version>,
2424
HelpText<"Alias for --version">;
2525

26+
def remove : Flag<["-"], "r">,
27+
HelpText<"Remove the __LLVM bitcode segment entirely">;
28+
2629
def output : JoinedOrSeparate<["-"], "o">, HelpText<"Write output to <file>">,
2730
MetaVarName<"<file>">;

llvm/tools/llvm-objcopy/ObjcopyOptions.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/ADT/StringRef.h"
1313
#include "llvm/ADT/StringSet.h"
1414
#include "llvm/BinaryFormat/COFF.h"
15+
#include "llvm/ObjCopy/CommonConfig.h"
1516
#include "llvm/ObjCopy/ConfigManager.h"
1617
#include "llvm/Option/Arg.h"
1718
#include "llvm/Option/ArgList.h"
@@ -1183,7 +1184,8 @@ objcopy::parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr) {
11831184
}
11841185

11851186
Expected<DriverConfig>
1186-
objcopy::parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr) {
1187+
objcopy::parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr,
1188+
function_ref<Error(Error)> ErrorCallback) {
11871189
DriverConfig DC;
11881190
ConfigManager ConfigMgr;
11891191
CommonConfig &Config = ConfigMgr.Common;
@@ -1228,6 +1230,13 @@ objcopy::parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr) {
12281230
}
12291231
Config.OutputFilename = InputArgs.getLastArgValue(BITCODE_STRIP_output);
12301232

1233+
if (!InputArgs.hasArg(BITCODE_STRIP_remove))
1234+
return createStringError(errc::invalid_argument, "no action specified");
1235+
1236+
// We only support -r for now, which removes all bitcode sections.
1237+
cantFail(Config.ToRemove.addMatcher(NameOrPattern::create(
1238+
"__LLVM,__bundle", MatchStyle::Literal, ErrorCallback)));
1239+
12311240
DC.CopyConfigs.push_back(std::move(ConfigMgr));
12321241
return std::move(DC);
12331242
}

llvm/tools/llvm-objcopy/ObjcopyOptions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ parseInstallNameToolOptions(ArrayRef<const char *> ArgsArr);
4141
// ParseBitcodeStripOptions returns the config and sets the input arguments.
4242
// If a help flag is set then ParseBitcodeStripOptions will print the help
4343
// messege and exit.
44-
Expected<DriverConfig> parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr);
44+
Expected<DriverConfig>
45+
parseBitcodeStripOptions(ArrayRef<const char *> ArgsArr,
46+
llvm::function_ref<Error(Error)> ErrorCallback);
4547

4648
// ParseStripOptions returns the config and sets the input arguments. If a
4749
// help flag is set then ParseStripOptions will print the help messege and

llvm/tools/llvm-objcopy/llvm-objcopy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static Expected<DriverConfig> getDriverConfig(ArrayRef<const char *> Args) {
8686
};
8787

8888
if (Is("bitcode-strip") || Is("bitcode_strip"))
89-
return parseBitcodeStripOptions(Args);
89+
return parseBitcodeStripOptions(Args, reportWarning);
9090
else if (Is("strip"))
9191
return parseStripOptions(Args, reportWarning);
9292
else if (Is("install-name-tool") || Is("install_name_tool"))

0 commit comments

Comments
 (0)