Skip to content

Commit a6e39db

Browse files
committed
[llvm-strip] Remove empty SHT_GROUP sections.
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
1 parent 9ea9e71 commit a6e39db

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

llvm/lib/ObjCopy/ELF/ELFObject.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2206,8 +2206,16 @@ Error Object::removeSections(
22062206
// Transfer removed sections into the Object RemovedSections container for use
22072207
// later.
22082208
std::move(Iter, Sections.end(), std::back_inserter(RemovedSections));
2209-
// Now finally get rid of them all together.
2209+
// Now get rid of them all together.
22102210
Sections.erase(Iter, std::end(Sections));
2211+
2212+
// Finally iterate over all sections and erase empty SHT_GROUP sections.
2213+
for (auto Iter = Sections.begin(); Iter != Sections.end(); ++Iter) {
2214+
if (auto GroupSec = dyn_cast<GroupSection>(Iter->get())) {
2215+
if (GroupSec->getMembersCount() == 0)
2216+
Sections.erase(Iter);
2217+
}
2218+
}
22112219
return Error::success();
22122220
}
22132221

llvm/lib/ObjCopy/ELF/ELFObject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,8 @@ class GroupSection : public SectionBase {
963963
const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
964964
void onRemove() override;
965965

966+
size_t getMembersCount() { return GroupMembers.size(); }
967+
966968
static bool classof(const SectionBase *S) {
967969
return S->OriginalType == ELF::SHT_GROUP;
968970
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# RUN: yaml2obj %s -o %t
2+
# RUN: llvm-strip --strip-debug %t -o %t1
3+
# RUN: llvm-readelf --section-groups %t1 | FileCheck %s
4+
# RUN: llvm-strip --strip-unneeded %t -o %t2
5+
# RUN: llvm-readelf --section-groups %t2 | FileCheck %s
6+
# RUN: llvm-strip --remove-section=.debug_macro %t -o %t3
7+
# RUN: llvm-readelf --section-groups %t3 | FileCheck %s
8+
# RUN: llvm-strip --remove-section=.debug_* %t -o %t4
9+
# RUN: llvm-readelf --section-groups %t4 | FileCheck %s
10+
11+
--- !ELF
12+
FileHeader:
13+
Class: ELFCLASS64
14+
Data: ELFDATA2LSB
15+
Type: ET_REL
16+
Machine: EM_X86_64
17+
Sections:
18+
- Name: .group
19+
Type: SHT_GROUP
20+
Info: foo_grp
21+
Members:
22+
- SectionOrType: GRP_COMDAT
23+
- SectionOrType: .debug_macro
24+
- Name: .debug_macro
25+
Type: SHT_PROGBITS
26+
Flags: [ SHF_GROUP ]
27+
Symbols:
28+
- Name: foo_grp
29+
Section: .group
30+
31+
# CHECK: There are no section groups in this file.

0 commit comments

Comments
 (0)