-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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-objcopy] Remove empty SHT_GROUP sections #97141
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be 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 If you have received no comments on your PR for a week, you can request a review 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. |
@llvm/pr-subscribers-llvm-binary-utilities Author: Dmitriy Chestnykh (chestnykh) ChangesCurrently Full diff: https://github.com/llvm/llvm-project/pull/97141.diff 3 Files Affected:
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..26bbde0c603bb
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/strip-debug-empty-group.test
@@ -0,0 +1,25 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-strip --strip-debug %t -o %t1
+# RUN: llvm-readelf --section-groups %t1 | 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.
|
0e46333
to
ce96728
Compare
CC @MaskRay |
Thanks for noticing this. This applies to other section removal operations like --remove-section, so another test will be useful |
ce96728
to
8bc990f
Compare
Added some new runs of llvm-strip + llvm-readelf |
8bc990f
to
a6e39db
Compare
a6e39db
to
eed7c5e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can reopen this PR...
# RUN: yaml2obj %s -o %t | ||
# RUN: llvm-strip --strip-debug %t -o %t1 | ||
# RUN: llvm-readelf --section-groups %t1 | FileCheck %s | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding another test using --remove-section=.debug_macro
The test file can probably be merged into remove-section-in-group.test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved test to remove-section-in-group.test
. Added --remove-section=.debug_macro
test (line 44).
✅ With the latest revision this PR passed the C/C++ code formatter. |
af93768
to
648809a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be worth a test case where you explicitly remove an empty .group section too, to show that the code handles that successfully (i.e. it doesn't try to remove the same .group section twice and blow up).
648809a
to
f0108b1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
llvm/lib/ObjCopy/ELF/ELFObject.cpp
Outdated
for (auto Iter = Sections.begin(); Iter != Sections.end(); ++Iter) | ||
if (auto GroupSec = dyn_cast<GroupSection>(Iter->get())) | ||
if (GroupSec->getMembersCount() == 0) | ||
Sections.erase(Iter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is UB and has a quadratic time complexity if the erased elements are many. https://eel.is/c++draft/vector#modifiers-3
"Effects: Invalidates iterators and references at or after the point of the erase."
You could utilize the return address of erase, but to address the time complexity issue you'd need erase_if
anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thanks!
f0108b1
to
49172a9
Compare
llvm/lib/ObjCopy/ELF/ELFObject.h
Outdated
@@ -963,6 +963,8 @@ class GroupSection : public SectionBase { | |||
const DenseMap<SectionBase *, SectionBase *> &FromTo) override; | |||
void onRemove() override; | |||
|
|||
size_t getMembersCount() { return GroupMembers.size(); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
() const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
49172a9
to
fbd81eb
Compare
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM again.
@MaskRay can you merge this PR? |
@chestnykh Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
This reverts commit 359c64f. This caused heap-use-after-free. See llvm#98106.
Currently
llvm-objcopy/llvm-strip
in--strip-debug
mode doesn't remove such sections. This behavior can lead to incompatibilities with GNU binutils (for examples ld.bfd before https://sourceware.org/PR20520 cannot process the object file contains empty .group section).The ELF object that contains group section with
.debug_*
sections inside can be obtained bygcc -g3
.Fix #97139