[llvm] 5b875bf - [llvm-objcopy][ELF] Fix removing a group member.
Igor Kudrin via llvm-commits
llvm-commits at lists.llvm.org
Fri May 29 06:28:43 PDT 2020
Author: Igor Kudrin
Date: 2020-05-29T20:24:53+07:00
New Revision: 5b875bf59b068f3db91c750b24030a7b2bd164aa
URL: https://github.com/llvm/llvm-project/commit/5b875bf59b068f3db91c750b24030a7b2bd164aa
DIFF: https://github.com/llvm/llvm-project/commit/5b875bf59b068f3db91c750b24030a7b2bd164aa.diff
LOG: [llvm-objcopy][ELF] Fix removing a group member.
When a group member is removed, the corresponding record in the
SHT_GROUP section has to be deleted.
This fixes PR46064.
Differential Revision: https://reviews.llvm.org/D80568
Added:
llvm/test/tools/llvm-objcopy/ELF/remove-section-in-group.test
Modified:
llvm/tools/llvm-objcopy/ELF/Object.cpp
llvm/tools/llvm-objcopy/ELF/Object.h
Removed:
################################################################################
diff --git a/llvm/test/tools/llvm-objcopy/ELF/remove-section-in-group.test b/llvm/test/tools/llvm-objcopy/ELF/remove-section-in-group.test
new file mode 100644
index 000000000000..6b2e43d68033
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/remove-section-in-group.test
@@ -0,0 +1,36 @@
+## This checks that the group section is shrunk when its member is removed.
+
+# RUN: yaml2obj %s -o - \
+# RUN: | llvm-objcopy -R .foo - - \
+# RUN: | obj2yaml - \
+# RUN: | FileCheck %s
+
+# CHECK: - Name: .group
+# CHECK: Members:
+# CHECK-NEXT: - SectionOrType: GRP_COMDAT
+# CHECK-NEXT: - SectionOrType: .bar
+# CHECK-NOT: - SectionOrType:
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .group
+ Type: SHT_GROUP
+ Info: foo_bar_grp
+ Members:
+ - SectionOrType: GRP_COMDAT
+ - SectionOrType: .foo
+ - SectionOrType: .bar
+ - Name: .foo
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_GROUP ]
+ - Name: .bar
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_GROUP ]
+Symbols:
+ - Name: foo_bar_grp
+ Section: .group
diff --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp
index 2ceb47923649..8bb4bbb40f94 100644
--- a/llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -112,7 +112,9 @@ void ELFSectionSizer<ELFT>::visit(RelocationSection &Sec) {
template <class ELFT>
void ELFSectionSizer<ELFT>::visit(GnuDebugLinkSection &Sec) {}
-template <class ELFT> void ELFSectionSizer<ELFT>::visit(GroupSection &Sec) {}
+template <class ELFT> void ELFSectionSizer<ELFT>::visit(GroupSection &Sec) {
+ Sec.Size = sizeof(Elf_Word) + Sec.GroupMembers.size() * sizeof(Elf_Word);
+}
template <class ELFT>
void ELFSectionSizer<ELFT>::visit(SectionIndexSection &Sec) {}
@@ -968,6 +970,12 @@ void GroupSection::finalize() {
this->Link = SymTab->Index;
}
+Error GroupSection::removeSectionReferences(
+ bool AllowBrokenLinks, function_ref<bool(const SectionBase *)> ToRemove) {
+ llvm::erase_if(GroupMembers, ToRemove);
+ return Error::success();
+}
+
Error GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
if (ToRemove(*Sym))
return createStringError(llvm::errc::invalid_argument,
diff --git a/llvm/tools/llvm-objcopy/ELF/Object.h b/llvm/tools/llvm-objcopy/ELF/Object.h
index 3ef1ec75352b..c7db57708fc3 100644
--- a/llvm/tools/llvm-objcopy/ELF/Object.h
+++ b/llvm/tools/llvm-objcopy/ELF/Object.h
@@ -801,6 +801,9 @@ class GroupSection : public SectionBase {
void accept(SectionVisitor &) const override;
void accept(MutableSectionVisitor &Visitor) override;
void finalize() override;
+ Error removeSectionReferences(
+ bool AllowBrokenLinks,
+ function_ref<bool(const SectionBase *)> ToRemove) override;
Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
void markSymbols() override;
void replaceSectionReferences(
More information about the llvm-commits
mailing list