Skip to content

Commit 4173be9

Browse files
[BOLT][DRAFT] Enable binary stripping
The patch toggles on `-remove-symtab`, checks compatibility with other flags, and omits sections that are removed by tools like `llvm-strip`.
1 parent 0745add commit 4173be9

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

bolt/include/bolt/Utils/CommandLineOpts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ extern llvm::cl::opt<bool> SplitEH;
6262
extern llvm::cl::opt<bool> StrictMode;
6363
extern llvm::cl::opt<bool> TimeOpts;
6464
extern llvm::cl::opt<bool> UseOldText;
65+
extern llvm::cl::opt<bool> StripBinary;
6566
extern llvm::cl::opt<bool> UpdateDebugSections;
6667

6768
// The default verbosity level (0) is pretty terse, level 1 is fairly

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,29 @@ void RewriteInstance::adjustCommandLineOptions() {
21082108
opts::UseOldText = false;
21092109
}
21102110

2111+
if (opts::StripBinary) {
2112+
auto exitStripFlagError = [&](StringRef IncompatibleFlag) {
2113+
BC->errs() << "BOLT-ERROR: -strip-binary and " << IncompatibleFlag
2114+
<< " cannot be used at the same time\n";
2115+
exit(1);
2116+
};
2117+
2118+
if (opts::UpdateDebugSections)
2119+
exitStripFlagError("-update-debug-sections");
2120+
if (opts::EnableBAT)
2121+
exitStripFlagError("-enable-bat");
2122+
2123+
// Incompatible with any other flags that introduce extra symbols/sections?
2124+
if (opts::Instrument)
2125+
exitStripFlagError("-instrument");
2126+
if (opts::Hugify)
2127+
exitStripFlagError("-hugify");
2128+
2129+
// Toggle symbol table removal on
2130+
if (!opts::RemoveSymtab)
2131+
opts::RemoveSymtab = true;
2132+
}
2133+
21112134
if (opts::Lite && opts::StrictMode) {
21122135
BC->errs()
21132136
<< "BOLT-ERROR: -strict and -lite cannot be used at the same time\n";
@@ -3640,7 +3663,7 @@ void RewriteInstance::updateMetadata() {
36403663
DebugInfoRewriter->updateDebugInfo();
36413664
}
36423665

3643-
if (opts::WriteBoltInfoSection)
3666+
if (opts::WriteBoltInfoSection && !opts::StripBinary)
36443667
addBoltInfoSection();
36453668
}
36463669

@@ -4367,6 +4390,13 @@ bool RewriteInstance::shouldStrip(const ELFShdrTy &Section,
43674390
if (opts::RemoveSymtab && Section.sh_type == ELF::SHT_SYMTAB)
43684391
return true;
43694392

4393+
if (opts::StripBinary) {
4394+
if (Section.sh_type == ELF::SHT_STRTAB)
4395+
return true;
4396+
if (Section.sh_type == ELF::SHT_PROGBITS && SectionName == ".comment")
4397+
return true;
4398+
// TODO: ignore any .note* sections here?
4399+
}
43704400
return false;
43714401
}
43724402

@@ -4788,7 +4818,7 @@ void RewriteInstance::updateELFSymbolTable(
47884818

47894819
for (const ELFSymTy &Symbol : cantFail(Obj.symbols(&SymTabSection))) {
47904820
// For regular (non-dynamic) symbol table strip unneeded symbols.
4791-
if (!IsDynSym && shouldStrip(Symbol))
4821+
if (!IsDynSym && shouldStrip(Symbol)) // TODO: -strip-all support ?
47924822
continue;
47934823

47944824
const BinaryFunction *Function =

bolt/lib/Utils/CommandLineOpts.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ cl::opt<bool> UpdateDebugSections(
208208
cl::desc("update DWARF debug sections of the executable"),
209209
cl::cat(BoltCategory));
210210

211+
cl::opt<bool> StripBinary("strip-binary", cl::desc("perform binary stripping"),
212+
cl::cat(BoltCategory));
213+
211214
cl::opt<unsigned>
212215
Verbosity("v", cl::desc("set verbosity level for diagnostic output"),
213216
cl::init(0), cl::ZeroOrMore, cl::cat(BoltCategory),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Verify that bolt correctly strips the binary of symbols and sections.
2+
3+
REQUIRES: system-linux
4+
5+
RUN: %clang %cflags -g %p/../Inputs/asm_foo.s %p/../Inputs/asm_main.c -o %t.exe -Wl,-q
6+
RUN: llvm-bolt %t.exe -o %t.stripped -strip-binary
7+
8+
RUN: nm %t.stripped 2>&1 | FileCheck %s --check-prefix=CHECK-NM
9+
RUN: llvm-readelf --sections %t.stripped 2>&1 | FileCheck %s --check-prefix=CHECK-READELF
10+
11+
CHECK-NM: nm: {{.*}}: no symbols
12+
CHECK-READELF-NOT: .debug
13+
CHECK-READELF-NOT: .comment
14+
CHECK-READELF-NOT: .note{{.*}}
15+
16+
RUN: not llvm-bolt %t.exe -o %t -strip-binary -update-debug-sections 2>&1 | FileCheck %s --check-prefix CHECK-UPDATE-DEBUG
17+
CHECK-UPDATE-DEBUG: BOLT-ERROR: -strip-binary and -update-debug-sections cannot be used at the same time
18+
19+
RUN: not llvm-bolt %t.exe -o %t -strip-binary -enable-bat 2>&1 | FileCheck %s --check-prefix CHECK-BAT
20+
CHECK-BAT: BOLT-ERROR: -strip-binary and -enable-bat cannot be used at the same time
21+
22+
RUN: not llvm-bolt %t.exe -o %t -strip-binary -instrument 2>&1 | FileCheck %s --check-prefix CHECK-INSTRUMENT
23+
CHECK-INSTRUMENT: BOLT-ERROR: -strip-binary and -instrument cannot be used at the same time
24+
25+
RUN: not llvm-bolt %t.exe -o %t -strip-binary -hugify 2>&1 | FileCheck %s --check-prefix CHECK-HUGIFY
26+
CHECK-HUGIFY: BOLT-ERROR: -strip-binary and -hugify cannot be used at the same time

0 commit comments

Comments
 (0)