[llvm] a931328 - [llvm-objcopy][ELF] Fix removing SHT_GROUP sections.
Igor Kudrin via llvm-commits
llvm-commits at lists.llvm.org
Fri May 29 06:28:41 PDT 2020
Author: Igor Kudrin
Date: 2020-05-29T20:24:53+07:00
New Revision: a9313282cd5413ed498dceb763ebba12ea5bdecd
URL: https://github.com/llvm/llvm-project/commit/a9313282cd5413ed498dceb763ebba12ea5bdecd
DIFF: https://github.com/llvm/llvm-project/commit/a9313282cd5413ed498dceb763ebba12ea5bdecd.diff
LOG: [llvm-objcopy][ELF] Fix removing SHT_GROUP sections.
When a SHT_GROUP section is removed, but other sections of the group are
kept, the SHF_GROUP flag of these sections should be dropped, otherwise
the resulting ELF file will be malformed.
Differential Revision: https://reviews.llvm.org/D80511
Added:
llvm/test/tools/llvm-objcopy/ELF/remove-section-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-group.test b/llvm/test/tools/llvm-objcopy/ELF/remove-section-group.test
new file mode 100644
index 000000000000..166fc3965f80
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/remove-section-group.test
@@ -0,0 +1,33 @@
+## This checks that when the header section of a group is removed, the tool
+## drops the flag SHF_GROUP for preserved members of that group.
+
+# RUN: yaml2obj %s -o - \
+# RUN: | llvm-objcopy -R .group - - \
+# RUN: | llvm-readobj --sections - \
+# RUN: | FileCheck %s
+
+# CHECK: Name: .foo
+# CHECK-NEXT: Type: SHT_PROGBITS
+# CHECK-NEXT: Flags [
+# CHECK-NEXT: SHF_ALLOC
+# CHECK-NEXT: ]
+
+--- !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: .foo
+ - Name: .foo
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_GROUP ]
+Symbols:
+ - Name: foo_grp
+ Section: .group
diff --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp
index 8c3ae2596722..2ceb47923649 100644
--- a/llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -65,6 +65,7 @@ void SectionBase::finalize() {}
void SectionBase::markSymbols() {}
void SectionBase::replaceSectionReferences(
const DenseMap<SectionBase *, SectionBase *> &) {}
+void SectionBase::onRemove() {}
template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
uint8_t *B = Buf.getBufferStart() + Sec.HeaderOffset;
@@ -988,6 +989,13 @@ void GroupSection::replaceSectionReferences(
Sec = To;
}
+void GroupSection::onRemove() {
+ // As the header section of the group is removed, drop the Group flag in its
+ // former members.
+ for (SectionBase *Sec : GroupMembers)
+ Sec->Flags &= ~SHF_GROUP;
+}
+
void Section::initialize(SectionTableRef SecTable) {
if (Link == ELF::SHN_UNDEF)
return;
@@ -1838,6 +1846,7 @@ Error Object::removeSections(bool AllowBrokenLinks,
for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
for (auto &Segment : Segments)
Segment->removeSection(RemoveSec.get());
+ RemoveSec->onRemove();
RemoveSections.insert(RemoveSec.get());
}
diff --git a/llvm/tools/llvm-objcopy/ELF/Object.h b/llvm/tools/llvm-objcopy/ELF/Object.h
index 97702a66bc47..3ef1ec75352b 100644
--- a/llvm/tools/llvm-objcopy/ELF/Object.h
+++ b/llvm/tools/llvm-objcopy/ELF/Object.h
@@ -424,6 +424,8 @@ class SectionBase {
virtual void markSymbols();
virtual void
replaceSectionReferences(const DenseMap<SectionBase *, SectionBase *> &);
+ // Notify the section that it is subject to removal.
+ virtual void onRemove();
};
class Segment {
@@ -803,6 +805,7 @@ class GroupSection : public SectionBase {
void markSymbols() override;
void replaceSectionReferences(
const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
+ void onRemove() override;
static bool classof(const SectionBase *S) {
return S->OriginalType == ELF::SHT_GROUP;
More information about the llvm-commits
mailing list