diff options
author | Paschalis Mpeis <Paschalis.Mpeis@arm.com> | 2024-12-23 10:25:27 +0000 |
---|---|---|
committer | Paschalis Mpeis <Paschalis.Mpeis@arm.com> | 2024-12-23 15:04:45 +0000 |
commit | 4173be950fddb271ab0d0701af900df57f058794 (patch) | |
tree | a0b8e67cba86fede0e03a66d93a8b5828b30948d | |
parent | 0745add7f458e0a65e048f8c74933bdb48ae97d9 (diff) | |
download | llvm-users/paschalis-mpeis/bolt-binary-strip.zip llvm-users/paschalis-mpeis/bolt-binary-strip.tar.gz llvm-users/paschalis-mpeis/bolt-binary-strip.tar.bz2 |
[BOLT][DRAFT] Enable binary strippingusers/paschalis-mpeis/bolt-binary-strip
The patch toggles on `-remove-symtab`, checks compatibility with other
flags, and omits sections that are removed by tools like `llvm-strip`.
-rw-r--r-- | bolt/include/bolt/Utils/CommandLineOpts.h | 1 | ||||
-rw-r--r-- | bolt/lib/Rewrite/RewriteInstance.cpp | 34 | ||||
-rw-r--r-- | bolt/lib/Utils/CommandLineOpts.cpp | 3 | ||||
-rw-r--r-- | bolt/test/AArch64/strip-binary.test | 26 |
4 files changed, 62 insertions, 2 deletions
diff --git a/bolt/include/bolt/Utils/CommandLineOpts.h b/bolt/include/bolt/Utils/CommandLineOpts.h index 111eb65..e193473 100644 --- a/bolt/include/bolt/Utils/CommandLineOpts.h +++ b/bolt/include/bolt/Utils/CommandLineOpts.h @@ -62,6 +62,7 @@ extern llvm::cl::opt<bool> SplitEH; extern llvm::cl::opt<bool> StrictMode; extern llvm::cl::opt<bool> TimeOpts; extern llvm::cl::opt<bool> UseOldText; +extern llvm::cl::opt<bool> StripBinary; extern llvm::cl::opt<bool> UpdateDebugSections; // The default verbosity level (0) is pretty terse, level 1 is fairly diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp index 4329235..53337ec 100644 --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -2108,6 +2108,29 @@ void RewriteInstance::adjustCommandLineOptions() { opts::UseOldText = false; } + if (opts::StripBinary) { + auto exitStripFlagError = [&](StringRef IncompatibleFlag) { + BC->errs() << "BOLT-ERROR: -strip-binary and " << IncompatibleFlag + << " cannot be used at the same time\n"; + exit(1); + }; + + if (opts::UpdateDebugSections) + exitStripFlagError("-update-debug-sections"); + if (opts::EnableBAT) + exitStripFlagError("-enable-bat"); + + // Incompatible with any other flags that introduce extra symbols/sections? + if (opts::Instrument) + exitStripFlagError("-instrument"); + if (opts::Hugify) + exitStripFlagError("-hugify"); + + // Toggle symbol table removal on + if (!opts::RemoveSymtab) + opts::RemoveSymtab = true; + } + if (opts::Lite && opts::StrictMode) { BC->errs() << "BOLT-ERROR: -strict and -lite cannot be used at the same time\n"; @@ -3640,7 +3663,7 @@ void RewriteInstance::updateMetadata() { DebugInfoRewriter->updateDebugInfo(); } - if (opts::WriteBoltInfoSection) + if (opts::WriteBoltInfoSection && !opts::StripBinary) addBoltInfoSection(); } @@ -4367,6 +4390,13 @@ bool RewriteInstance::shouldStrip(const ELFShdrTy &Section, if (opts::RemoveSymtab && Section.sh_type == ELF::SHT_SYMTAB) return true; + if (opts::StripBinary) { + if (Section.sh_type == ELF::SHT_STRTAB) + return true; + if (Section.sh_type == ELF::SHT_PROGBITS && SectionName == ".comment") + return true; + // TODO: ignore any .note* sections here? + } return false; } @@ -4788,7 +4818,7 @@ void RewriteInstance::updateELFSymbolTable( for (const ELFSymTy &Symbol : cantFail(Obj.symbols(&SymTabSection))) { // For regular (non-dynamic) symbol table strip unneeded symbols. - if (!IsDynSym && shouldStrip(Symbol)) + if (!IsDynSym && shouldStrip(Symbol)) // TODO: -strip-all support ? continue; const BinaryFunction *Function = diff --git a/bolt/lib/Utils/CommandLineOpts.cpp b/bolt/lib/Utils/CommandLineOpts.cpp index 17f090a..3baa2c9 100644 --- a/bolt/lib/Utils/CommandLineOpts.cpp +++ b/bolt/lib/Utils/CommandLineOpts.cpp @@ -208,6 +208,9 @@ cl::opt<bool> UpdateDebugSections( cl::desc("update DWARF debug sections of the executable"), cl::cat(BoltCategory)); +cl::opt<bool> StripBinary("strip-binary", cl::desc("perform binary stripping"), + cl::cat(BoltCategory)); + cl::opt<unsigned> Verbosity("v", cl::desc("set verbosity level for diagnostic output"), cl::init(0), cl::ZeroOrMore, cl::cat(BoltCategory), diff --git a/bolt/test/AArch64/strip-binary.test b/bolt/test/AArch64/strip-binary.test new file mode 100644 index 0000000..b0275a8 --- /dev/null +++ b/bolt/test/AArch64/strip-binary.test @@ -0,0 +1,26 @@ +## Verify that bolt correctly strips the binary of symbols and sections. + +REQUIRES: system-linux + +RUN: %clang %cflags -g %p/../Inputs/asm_foo.s %p/../Inputs/asm_main.c -o %t.exe -Wl,-q +RUN: llvm-bolt %t.exe -o %t.stripped -strip-binary + +RUN: nm %t.stripped 2>&1 | FileCheck %s --check-prefix=CHECK-NM +RUN: llvm-readelf --sections %t.stripped 2>&1 | FileCheck %s --check-prefix=CHECK-READELF + +CHECK-NM: nm: {{.*}}: no symbols +CHECK-READELF-NOT: .debug +CHECK-READELF-NOT: .comment +CHECK-READELF-NOT: .note{{.*}} + +RUN: not llvm-bolt %t.exe -o %t -strip-binary -update-debug-sections 2>&1 | FileCheck %s --check-prefix CHECK-UPDATE-DEBUG +CHECK-UPDATE-DEBUG: BOLT-ERROR: -strip-binary and -update-debug-sections cannot be used at the same time + +RUN: not llvm-bolt %t.exe -o %t -strip-binary -enable-bat 2>&1 | FileCheck %s --check-prefix CHECK-BAT +CHECK-BAT: BOLT-ERROR: -strip-binary and -enable-bat cannot be used at the same time + +RUN: not llvm-bolt %t.exe -o %t -strip-binary -instrument 2>&1 | FileCheck %s --check-prefix CHECK-INSTRUMENT +CHECK-INSTRUMENT: BOLT-ERROR: -strip-binary and -instrument cannot be used at the same time + +RUN: not llvm-bolt %t.exe -o %t -strip-binary -hugify 2>&1 | FileCheck %s --check-prefix CHECK-HUGIFY +CHECK-HUGIFY: BOLT-ERROR: -strip-binary and -hugify cannot be used at the same time |