[llvm] [BOLT][DRAFT] Enable binary stripping (PR #120972)

Paschalis Mpeis via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 23 07:15:28 PST 2024


https://github.com/paschalis-mpeis created https://github.com/llvm/llvm-project/pull/120972

The patch toggles on `-remove-symtab`, checks compatibility with other flags, and omits sections that are removed by tools like `llvm-strip`.


>From 4173be950fddb271ab0d0701af900df57f058794 Mon Sep 17 00:00:00 2001
From: Paschalis Mpeis <Paschalis.Mpeis at arm.com>
Date: Mon, 23 Dec 2024 10:25:27 +0000
Subject: [PATCH] [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`.
---
 bolt/include/bolt/Utils/CommandLineOpts.h |  1 +
 bolt/lib/Rewrite/RewriteInstance.cpp      | 34 +++++++++++++++++++++--
 bolt/lib/Utils/CommandLineOpts.cpp        |  3 ++
 bolt/test/AArch64/strip-binary.test       | 26 +++++++++++++++++
 4 files changed, 62 insertions(+), 2 deletions(-)
 create mode 100644 bolt/test/AArch64/strip-binary.test

diff --git a/bolt/include/bolt/Utils/CommandLineOpts.h b/bolt/include/bolt/Utils/CommandLineOpts.h
index 111eb650c37465..e19347379696cf 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 4329235d470497..53337eca22e84c 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 17f090aa61ee9e..3baa2c98d55ba8 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 00000000000000..b0275a8239360a
--- /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



More information about the llvm-commits mailing list