[llvm] [ELFObject] Added conditions to print removed symbols and removed sections (PR #124692)

Kshitij Paranjape via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 11 00:01:39 PDT 2025


https://github.com/kshitijvp updated https://github.com/llvm/llvm-project/pull/124692

>From 718edfee71e995ab4fbee442af7b92a0c15cee65 Mon Sep 17 00:00:00 2001
From: Kshitij Paranjape <kshitijvparanjape at gmail.com>
Date: Mon, 27 Jan 2025 21:19:33 +0530
Subject: [PATCH 1/3] [ELFObject] Added dbgs() statement in removeSections()

This patch adds dbgs() statement inside removeSections()
and removeSymbol() function to print the removed sections
and symbols.

Fixes: #123041
---
 llvm/lib/ObjCopy/ELF/ELFObject.cpp    | 26 ++++++++++++++++++++------
 llvm/tools/llvm-objcopy/CommonOpts.td |  2 ++
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index 45c7ea49b5d93..94c73b62de12e 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -766,8 +766,14 @@ Error SymbolTableSection::removeSymbols(
     function_ref<bool(const Symbol &)> ToRemove) {
   Symbols.erase(
       std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
-                     [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
-      std::end(Symbols));
+               [ToRemove](const SymPtr &Sym) {
+                   if (ToRemove(*Sym)) {
+                       dbgs()<<"Symbols Removed:"<<Sym->Name<< "\n";
+                       return true;
+                   }
+                   return false;
+               }));
+
   auto PrevSize = Size;
   Size = Symbols.size() * EntrySize;
   if (Size < PrevSize)
@@ -2249,10 +2255,18 @@ Error Object::removeSections(
 
   // Transfer removed sections into the Object RemovedSections container for use
   // later.
-  std::move(Iter, Sections.end(), std::back_inserter(RemovedSections));
-  // Now finally get rid of them all together.
-  Sections.erase(Iter, std::end(Sections));
-  return Error::success();
+  for(auto &KeepSec : make_range(std::begin(Sections) , Iter))
+  {
+    
+    if (Error E = KeepSec->removeSectionReferences(
+            AllowBrokenLinks, [&RemoveSections](const SectionBase *Sec) {
+              return RemoveSections.find(Sec) != RemoveSections.end();
+            }))
+      std::move(Iter, Sections.end(), std::back_inserter(RemovedSections));
+      dbgs()<<"Sections Removed:"<<KeepSec->Name<<'\n';
+      Sections.erase(Iter, std::end(Sections));
+      return Error::success();
+  }
 }
 
 Error Object::replaceSections(
diff --git a/llvm/tools/llvm-objcopy/CommonOpts.td b/llvm/tools/llvm-objcopy/CommonOpts.td
index c247c93f6e0f2..5b15191f54605 100644
--- a/llvm/tools/llvm-objcopy/CommonOpts.td
+++ b/llvm/tools/llvm-objcopy/CommonOpts.td
@@ -117,6 +117,8 @@ def regex
 
 def version : Flag<["--"], "version">,
               HelpText<"Print the version and exit.">;
+def verbose : Flag<["--"], "verbose">,
+              HelpText<"Prints the removed symbols and sections">;
 def V : Flag<["-"], "V">,
         Alias<version>,
         HelpText<"Alias for --version">;

>From 02fb5c0b003a99cdbd36c47c2ffd1ca95ab5db85 Mon Sep 17 00:00:00 2001
From: Kshitij Paranjape <kshitijvparanjape at gmail.com>
Date: Sat, 8 Feb 2025 14:59:42 +0530
Subject: [PATCH 2/3] Made Changes

---
 llvm/include/llvm/ObjCopy/CommonConfig.h   |  1 +
 llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp        |  1 +
 llvm/lib/ObjCopy/ELF/ELFObject.cpp         | 27 +++++++++++-----------
 llvm/lib/ObjCopy/ELF/ELFObject.h           |  1 +
 llvm/tools/llvm-objcopy/ObjcopyOptions.cpp |  2 ++
 5 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/llvm/include/llvm/ObjCopy/CommonConfig.h b/llvm/include/llvm/ObjCopy/CommonConfig.h
index aea9cd6f9a9c7..83ad4590d9c72 100644
--- a/llvm/include/llvm/ObjCopy/CommonConfig.h
+++ b/llvm/include/llvm/ObjCopy/CommonConfig.h
@@ -274,6 +274,7 @@ struct CommonConfig {
   bool StripNonAlloc = false;
   bool StripSections = false;
   bool StripUnneeded = false;
+  bool Verbose = false;
   bool Weaken = false;
   bool DecompressDebugSections = false;
 
diff --git a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
index 5aa0079f3fbc7..ad3a2135d23a7 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
@@ -782,6 +782,7 @@ static Error verifyNoteSection(StringRef Name, endianness Endianness,
 // system. The only priority is that keeps/copies overrule removes.
 static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
                         ElfType OutputElfType, Object &Obj) {
+  Obj.isVerboseEnabled = Config.Verbose;
   if (Config.OutputArch) {
     Obj.Machine = Config.OutputArch->EMachine;
     Obj.OSABI = Config.OutputArch->OSABI;
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index 94c73b62de12e..c9a090cdea517 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -768,7 +768,7 @@ Error SymbolTableSection::removeSymbols(
       std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
                [ToRemove](const SymPtr &Sym) {
                    if (ToRemove(*Sym)) {
-                       dbgs()<<"Symbols Removed:"<<Sym->Name<< "\n";
+                       llvm::outs() << "Symbols Removed:" << Sym->Name<< "\n";
                        return true;
                    }
                    return false;
@@ -2236,6 +2236,9 @@ Error Object::removeSections(
   for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
     for (auto &Segment : Segments)
       Segment->removeSection(RemoveSec.get());
+    if (isVerboseEnabled) {
+      llvm::outs() << "Removed Section: " << (RemoveSec.get()->Name);
+    }
     RemoveSec->onRemove();
     RemoveSections.insert(RemoveSec.get());
   }
@@ -2255,18 +2258,10 @@ Error Object::removeSections(
 
   // Transfer removed sections into the Object RemovedSections container for use
   // later.
-  for(auto &KeepSec : make_range(std::begin(Sections) , Iter))
-  {
-    
-    if (Error E = KeepSec->removeSectionReferences(
-            AllowBrokenLinks, [&RemoveSections](const SectionBase *Sec) {
-              return RemoveSections.find(Sec) != RemoveSections.end();
-            }))
-      std::move(Iter, Sections.end(), std::back_inserter(RemovedSections));
-      dbgs()<<"Sections Removed:"<<KeepSec->Name<<'\n';
-      Sections.erase(Iter, std::end(Sections));
-      return Error::success();
-  }
+  std::move(Iter, Sections.end(), std::back_inserter(RemovedSections));
+  // Now finally get rid of them all together.
+  Sections.erase(Iter, std::end(Sections));
+  return Error::success();
 }
 
 Error Object::replaceSections(
@@ -2296,8 +2291,12 @@ Error Object::replaceSections(
 Error Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
   if (SymbolTable)
     for (const SecPtr &Sec : Sections)
-      if (Error E = Sec->removeSymbols(ToRemove))
+      if (Error E = Sec->removeSymbols(ToRemove)){
+        if (isVerboseEnabled){
+          llvm::outs() << "Removed Symbols:" << Sec->Name;
+        }
         return E;
+      }
   return Error::success();
 }
 
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.h b/llvm/lib/ObjCopy/ELF/ELFObject.h
index d8f79a4b1a3cc..4ec1f7188ef88 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.h
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.h
@@ -1195,6 +1195,7 @@ class Object {
   uint32_t Flags;
 
   bool HadShdrs = true;
+  bool isVerboseEnabled = true;
   bool MustBeRelocatable = false;
   StringTableSection *SectionNames = nullptr;
   SymbolTableSection *SymbolTable = nullptr;
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index 0d209590655ef..7a0d719756081 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -1103,6 +1103,7 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> ArgsArr,
       OBJCOPY_verify_note_sections, OBJCOPY_no_verify_note_sections, true);
 
   Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug);
+  Config.Verbose = InputArgs.hasArg(OBJCOPY_verbose);
   ELFConfig.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
   MachOConfig.KeepUndefined = InputArgs.hasArg(OBJCOPY_keep_undefined);
   Config.DecompressDebugSections =
@@ -1586,6 +1587,7 @@ objcopy::parseStripOptions(ArrayRef<const char *> RawArgsArr,
   Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu);
   MachOConfig.StripSwiftSymbols = InputArgs.hasArg(STRIP_strip_swift_symbols);
   Config.OnlyKeepDebug = InputArgs.hasArg(STRIP_only_keep_debug);
+  Config.Verbose = InputArgs.hasArg(STRIP_verbose);
   ELFConfig.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols);
   MachOConfig.KeepUndefined = InputArgs.hasArg(STRIP_keep_undefined);
 

>From 76ccb5731498b389f6c3511dfd0ec09eae36505b Mon Sep 17 00:00:00 2001
From: Kshitij Paranjape <kshitijvparanjape at gmail.com>
Date: Tue, 11 Mar 2025 12:29:29 +0530
Subject: [PATCH 3/3] Made Changes

---
 llvm/docs/CommandGuide/llvm-objcopy.rst |  4 +++
 llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp     |  6 ++---
 llvm/lib/ObjCopy/ELF/ELFObject.cpp      | 33 ++++++++++++-------------
 llvm/lib/ObjCopy/ELF/ELFObject.h        |  7 ++++--
 4 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/llvm/docs/CommandGuide/llvm-objcopy.rst b/llvm/docs/CommandGuide/llvm-objcopy.rst
index 8dc1357635e1b..b90a5a4ddb0b5 100644
--- a/llvm/docs/CommandGuide/llvm-objcopy.rst
+++ b/llvm/docs/CommandGuide/llvm-objcopy.rst
@@ -178,6 +178,10 @@ multiple file formats.
  specified ``<flag>`` values. Can be specified multiple times to update multiple
  sections.
 
+.. option:: --verbose
+
+  List all object files modified.
+
  Supported flag names are `alloc`, `load`, `noload`, `readonly`, `exclude`,
  `debug`, `code`, `data`, `rom`, `share`, `contents`, `merge`, `strings`, and
  `large`. Not all flags are meaningful for all object file formats or target
diff --git a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
index ad3a2135d23a7..6cf42ea0e0303 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
@@ -546,7 +546,7 @@ static Error replaceAndRemoveSections(const CommonConfig &Config,
     };
   }
 
-  if (Error E = Obj.removeSections(ELFConfig.AllowBrokenLinks, RemovePred))
+  if (Error E = Obj.removeSections(ELFConfig.AllowBrokenLinks, RemovePred, Config.Verbose))
     return E;
 
   if (Error E = Obj.compressOrDecompressSections(Config))
@@ -782,7 +782,7 @@ static Error verifyNoteSection(StringRef Name, endianness Endianness,
 // system. The only priority is that keeps/copies overrule removes.
 static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
                         ElfType OutputElfType, Object &Obj) {
-  Obj.isVerboseEnabled = Config.Verbose;
+  Obj.VerboseOutput = Config.Verbose;
   if (Config.OutputArch) {
     Obj.Machine = Config.OutputArch->EMachine;
     Obj.OSABI = Config.OutputArch->OSABI;
@@ -791,7 +791,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
   if (!Config.SplitDWO.empty() && Config.ExtractDWO) {
     return Obj.removeSections(
         ELFConfig.AllowBrokenLinks,
-        [&Obj](const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); });
+        [&Obj](const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); }, Config.Verbose);
   }
 
   // Dump sections before add/remove for compatibility with GNU objcopy.
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index c9a090cdea517..8976612d5e67e 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -766,10 +766,11 @@ Error SymbolTableSection::removeSymbols(
     function_ref<bool(const Symbol &)> ToRemove) {
   Symbols.erase(
       std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
-               [ToRemove](const SymPtr &Sym) {
+               [&](const SymPtr &Sym) {
                    if (ToRemove(*Sym)) {
-                       llvm::outs() << "Symbols Removed:" << Sym->Name<< "\n";
-                       return true;
+                    if(VerboseOutput)
+                      outs() << "Symbols Removed:" << Sym->Name<< "\n";
+                    return true;
                    }
                    return false;
                }));
@@ -779,7 +780,6 @@ Error SymbolTableSection::removeSymbols(
   if (Size < PrevSize)
     IndicesChanged = true;
   assignIndices();
-  return Error::success();
 }
 
 void SymbolTableSection::replaceSectionReferences(
@@ -2201,7 +2201,7 @@ Error Object::updateSectionData(SectionBase &S, ArrayRef<uint8_t> Data) {
 }
 
 Error Object::removeSections(
-    bool AllowBrokenLinks, std::function<bool(const SectionBase &)> ToRemove) {
+    bool AllowBrokenLinks, std::function<bool(const SectionBase &)> ToRemove, bool VerboseOutput) {
 
   auto Iter = std::stable_partition(
       std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
@@ -2236,8 +2236,8 @@ Error Object::removeSections(
   for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
     for (auto &Segment : Segments)
       Segment->removeSection(RemoveSec.get());
-    if (isVerboseEnabled) {
-      llvm::outs() << "Removed Section: " << (RemoveSec.get()->Name);
+    if (VerboseOutput) {
+      outs() << "removed section: " << (RemoveSec.get()->Name);
     }
     RemoveSec->onRemove();
     RemoveSections.insert(RemoveSec.get());
@@ -2282,21 +2282,20 @@ Error Object::replaceSections(
 
   if (Error E = removeSections(
           /*AllowBrokenLinks=*/false,
-          [=](const SectionBase &Sec) { return FromTo.count(&Sec) > 0; }))
+          [=](const SectionBase &Sec) { return FromTo.count(&Sec) > 0; }, false))
     return E;
   llvm::sort(Sections, SectionIndexLess);
   return Error::success();
 }
 
 Error Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
-  if (SymbolTable)
-    for (const SecPtr &Sec : Sections)
-      if (Error E = Sec->removeSymbols(ToRemove)){
-        if (isVerboseEnabled){
-          llvm::outs() << "Removed Symbols:" << Sec->Name;
-        }
+  if (SymbolTable){
+    for (const SecPtr &Sec : Sections){
+      if (Error E = Sec->removeSymbols(ToRemove))
         return E;
-      }
+      outs() << "removed symbols:" << Sec->Name;
+    }
+  }
   return Error::success();
 }
 
@@ -2583,7 +2582,7 @@ static Error removeUnneededSections(Object &Obj) {
                      : Obj.SymbolTable->getStrTab();
   return Obj.removeSections(false, [&](const SectionBase &Sec) {
     return &Sec == Obj.SymbolTable || &Sec == StrTab;
-  });
+  }, false);
 }
 
 template <class ELFT> Error ELFWriter<ELFT>::finalize() {
@@ -2637,7 +2636,7 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
       if (Error E = Obj.removeSections(false /*AllowBrokenLinks*/,
                                        [this](const SectionBase &Sec) {
                                          return &Sec == Obj.SectionIndexTable;
-                                       }))
+                                       }, false))
         return E;
     }
   }
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.h b/llvm/lib/ObjCopy/ELF/ELFObject.h
index 4ec1f7188ef88..bcda100343813 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.h
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.h
@@ -814,6 +814,8 @@ class SymbolTableSection : public SectionBase {
   void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
   void assignIndices();
 
+private:
+  bool VerboseOutput;
 protected:
   std::vector<std::unique_ptr<Symbol>> Symbols;
   StringTableSection *SymbolNames = nullptr;
@@ -856,6 +858,7 @@ class SymbolTableSection : public SectionBase {
   static bool classof(const SectionBase *S) {
     return S->OriginalType == ELF::SHT_SYMTAB;
   }
+  bool getVerboseOutput() { return VerboseOutput; }
 };
 
 struct Relocation {
@@ -1195,7 +1198,7 @@ class Object {
   uint32_t Flags;
 
   bool HadShdrs = true;
-  bool isVerboseEnabled = true;
+  bool VerboseOutput;
   bool MustBeRelocatable = false;
   StringTableSection *SectionNames = nullptr;
   SymbolTableSection *SymbolTable = nullptr;
@@ -1225,7 +1228,7 @@ class Object {
   ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
 
   Error removeSections(bool AllowBrokenLinks,
-                       std::function<bool(const SectionBase &)> ToRemove);
+                       std::function<bool(const SectionBase &)> ToRemove, bool VerboseOutput);
   Error compressOrDecompressSections(const CommonConfig &Config);
   Error replaceSections(const DenseMap<SectionBase *, SectionBase *> &FromTo);
   Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);



More information about the llvm-commits mailing list