Skip to content

[llvm-objcopy] Remove references for empty section groups #98106

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

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/lib/ObjCopy/ELF/ELFObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,11 @@ Error Object::removeSections(
if (auto ToRelSec = RelSec->getSection())
return !ToRemove(*ToRelSec);
}
// Remove empty group sections.
if (Sec->Type == ELF::SHT_GROUP) {
auto GroupSec = cast<GroupSection>(Sec.get());
return !llvm::all_of(GroupSec->members(), ToRemove);
}
return true;
});
if (SymbolTable != nullptr && ToRemove(*SymbolTable))
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/ObjCopy/ELF/ELFObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,9 @@ class GroupSection : public SectionBase {
SmallVector<SectionBase *, 3> GroupMembers;

public:
template <class T>
using ConstRange = iterator_range<
pointee_iterator<typename llvm::SmallVector<T *, 3>::const_iterator>>;
// TODO: Contents is present in several classes of the hierarchy.
// This needs to be refactored to avoid duplication.
ArrayRef<uint8_t> Contents;
Expand All @@ -964,6 +967,10 @@ class GroupSection : public SectionBase {
const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
void onRemove() override;

ConstRange<SectionBase> members() const {
return make_pointee_range(GroupMembers);
}

static bool classof(const SectionBase *S) {
return S->OriginalType == ELF::SHT_GROUP;
}
Expand Down
50 changes: 49 additions & 1 deletion llvm/test/tools/llvm-objcopy/ELF/remove-section-in-group.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## This checks that the group section is shrunk when its member is removed.

# RUN: yaml2obj %s -o - \
# RUN: yaml2obj --docnum=1 %s -o - \
# RUN: | llvm-objcopy -R .foo - - \
# RUN: | obj2yaml - \
# RUN: | FileCheck %s
Expand Down Expand Up @@ -35,3 +35,51 @@ Symbols:
- Name: foo_bar_grp
Section: .group
Binding: STB_GLOBAL

# RUN: yaml2obj --docnum=2 %s -o %t
# RUN: llvm-objcopy --remove-section=.debug_macro %t
# RUN: llvm-readelf --section-groups %t | FileCheck %s --check-prefix=GROUP-REMOVED

--- !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

# GROUP-REMOVED: There are no section groups in this file.

# RUN: yaml2obj --docnum=3 %s -o %t
# RUN: llvm-objcopy --remove-section=.group %t
# RUN: llvm-readelf --section-groups %t | FileCheck %s --check-prefix=EMPTY-GROUP-REMOVED

--- !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
Symbols:
- Name: foo_grp
Section: .group

# EMPTY-GROUP-REMOVED: There are no section groups in this file.
Loading