Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[llvm-strip] Remove empty SHT_GROUP sections. #97204

Closed
wants to merge 1 commit into from

Conversation

chestnykh
Copy link
Contributor

Currently llvm-strip in --strip-debug mode doesn't remove such sections. This behavior can lead to incompatibilities with GNU binutils (for examples ld.bfd cannot process the object file contains empty .group section). The ELF object that contains group section
with .debug_* sections inside can be obtained by gcc -g3. Fix #97139

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@chestnykh
Copy link
Contributor Author

chestnykh commented Jun 30, 2024

@MaskRay sorry, i've accidentally closed #97141. This PR contains the same changes.

CC: @jh7370

@llvmbot
Copy link
Collaborator

llvmbot commented Jun 30, 2024

@llvm/pr-subscribers-llvm-binary-utilities

Author: Dmitriy Chestnykh (chestnykh)

Changes

Currently llvm-strip in --strip-debug mode doesn't remove such sections. This behavior can lead to incompatibilities with GNU binutils (for examples ld.bfd cannot process the object file contains empty .group section). The ELF object that contains group section
with .debug_* sections inside can be obtained by gcc -g3. Fix #97139


Full diff: https://github.com/llvm/llvm-project/pull/97204.diff

3 Files Affected:

  • (modified) llvm/lib/ObjCopy/ELF/ELFObject.cpp (+9-1)
  • (modified) llvm/lib/ObjCopy/ELF/ELFObject.h (+2)
  • (added) llvm/test/tools/llvm-objcopy/ELF/strip-debug-empty-group.test (+31)
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index 02591e6f987c2..44181ccd328bf 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -2206,8 +2206,16 @@ 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.
+  // Now get rid of them all together.
   Sections.erase(Iter, std::end(Sections));
+
+  // Finally iterate over all sections and erase empty SHT_GROUP sections.
+  for (auto Iter = Sections.begin(); Iter != Sections.end(); ++Iter) {
+    if (auto GroupSec = dyn_cast<GroupSection>(Iter->get())) {
+      if (GroupSec->getMembersCount() == 0)
+        Sections.erase(Iter);
+    }
+  }
   return Error::success();
 }
 
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.h b/llvm/lib/ObjCopy/ELF/ELFObject.h
index 2b1895a30b41e..9bbef268017ea 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.h
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.h
@@ -963,6 +963,8 @@ class GroupSection : public SectionBase {
       const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
   void onRemove() override;
 
+  size_t getMembersCount() {return GroupMembers.size(); }
+
   static bool classof(const SectionBase *S) {
     return S->OriginalType == ELF::SHT_GROUP;
   }
diff --git a/llvm/test/tools/llvm-objcopy/ELF/strip-debug-empty-group.test b/llvm/test/tools/llvm-objcopy/ELF/strip-debug-empty-group.test
new file mode 100644
index 0000000000000..3e09674e2288d
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/strip-debug-empty-group.test
@@ -0,0 +1,31 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-strip --strip-debug %t -o %t1
+# RUN: llvm-readelf --section-groups %t1 | FileCheck %s
+# RUN: llvm-strip --strip-unneeded %t -o %t2
+# RUN: llvm-readelf --section-groups %t2 | FileCheck %s
+# RUN: llvm-strip --remove-section=.debug_macro %t -o %t3
+# RUN: llvm-readelf --section-groups %t3 | FileCheck %s
+# RUN: llvm-strip --remove-section=.debug_* %t -o %t4
+# RUN: llvm-readelf --section-groups %t4 | FileCheck %s
+
+--- !ELF
+FileHeader:
+  Class:      ELFCLASS64
+  Data:       ELFDATA2LSB
+  Type:       ET_REL
+  Machine:    EM_X86_64
+Sections:
+  - Name:     .group
+    Type:     SHT_GROUP
+    Info:     foo_grp
+    Members:
+      - SectionOrType:  GRP_COMDAT
+      - SectionOrType:  .debug_macro
+  - Name:     .debug_macro
+    Type:     SHT_PROGBITS
+    Flags:    [ SHF_GROUP ]
+Symbols:
+  - Name:     foo_grp
+    Section:  .group
+
+# CHECK: There are no section groups in this file.

@tschuett tschuett requested review from jh7370 and MaskRay June 30, 2024 14:03
Sections.erase(Iter, std::end(Sections));

// Finally iterate over all sections and erase empty SHT_GROUP sections.
for (auto Iter = Sections.begin(); Iter != Sections.end(); ++Iter) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least for the for loop, I am not convinced that braces are needed. Furthermore:
https://llvm.org/docs/CodingStandards.html#don-t-evaluate-end-every-time-through-a-loop

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or:

llvm::remove_if(Sections, Lambda_Predicate);

Copy link
Contributor Author

@chestnykh chestnykh Jun 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the braces. The loop cannot be rewritten to auto I = Sections.begin(), E = Sections.end(); I != E; ++I because Sections may change size every time

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

llvm::remove_if as i can see isn't very widely used so i think we can leave for here

@chestnykh chestnykh force-pushed the empty-group-strip branch 2 times, most recently from c01b2a4 to 2ff0399 Compare June 30, 2024 18:18
Currently llvm-strip in --strip-debug mode doesn't remove such sections.
This behavior can lead to incompatibilities with GNU binutils
(for examples ld.bfd cannot process the object file contains empty .group section).
The ELF object that contains group section
with .debug_* sections inside can be obtained by gcc -g3.
Fix llvm#97139
@chestnykh chestnykh closed this Jun 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[llvm-strip] llvm-strip doesn't remove empty group sections
3 participants