[PATCH] D80511: [llvm-objcopy][ELF] Fix removing ".group" sections.

Igor Kudrin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 25 23:00:24 PDT 2020


ikudrin updated this revision to Diff 266104.
ikudrin marked 3 inline comments as done.
ikudrin added a comment.

- Updated the test according to @MaskRay's comments. Thanks!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80511/new/

https://reviews.llvm.org/D80511

Files:
  llvm/test/tools/llvm-objcopy/ELF/remove-section-group.test
  llvm/tools/llvm-objcopy/ELF/Object.cpp
  llvm/tools/llvm-objcopy/ELF/Object.h


Index: llvm/tools/llvm-objcopy/ELF/Object.h
===================================================================
--- llvm/tools/llvm-objcopy/ELF/Object.h
+++ llvm/tools/llvm-objcopy/ELF/Object.h
@@ -424,6 +424,8 @@
   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 @@
   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;
Index: llvm/tools/llvm-objcopy/ELF/Object.cpp
===================================================================
--- llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -65,6 +65,7 @@
 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 @@
       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 @@
   for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
     for (auto &Segment : Segments)
       Segment->removeSection(RemoveSec.get());
+    RemoveSec->onRemove();
     RemoveSections.insert(RemoveSec.get());
   }
 
Index: llvm/test/tools/llvm-objcopy/ELF/remove-section-group.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-objcopy/ELF/remove-section-group.test
@@ -0,0 +1,34 @@
+# RUN: yaml2obj %s -o - \
+# RUN:   | llvm-objcopy -R .group - - \
+# RUN:   | llvm-readobj --sections - \
+# RUN:   | FileCheck %s
+
+# 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.
+
+--- !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
+
+# CHECK:        Section {
+# CHECK:          Name: .foo
+# CHECK-NEXT:     Type: SHT_PROGBITS
+# CHECK-NEXT:     Flags [
+# CHECK-NEXT:       SHF_ALLOC
+# CHECK-NEXT:     ]


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80511.266104.patch
Type: text/x-patch
Size: 3047 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200526/98fc98d9/attachment-0001.bin>


More information about the llvm-commits mailing list