Skip to content

Commit ce96728

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 94a6b9c commit ce96728

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
5+
--- !ELF
6+
FileHeader:
7+
Class: ELFCLASS64
8+
Data: ELFDATA2LSB
9+
Type: ET_REL
10+
Machine: EM_X86_64
11+
Sections:
12+
- Name: .group
13+
Type: SHT_GROUP
14+
Info: foo_grp
15+
Members:
16+
- SectionOrType: GRP_COMDAT
17+
- SectionOrType: .debug_macro
18+
- Name: .debug_macro
19+
Type: SHT_PROGBITS
20+
Flags: [ SHF_GROUP ]
21+
Symbols:
22+
- Name: foo_grp
23+
Section: .group
24+
25+
# CHECK: There are no section groups in this file.

0 commit comments

Comments
 (0)