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

Igor Kudrin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 25 02:38:46 PDT 2020


ikudrin created this revision.
ikudrin added reviewers: MaskRay, jhenderson, dblaikie, rupprecht.
ikudrin added a project: LLVM.
Herald added subscribers: abrachet, emaste.
Herald added a reviewer: espindola.
Herald added a reviewer: alexshap.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80511

Files:
  llvm/test/tools/llvm-objcopy/ELF/remove-group-section.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-group-section.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-objcopy/ELF/remove-group-section.test
@@ -0,0 +1,35 @@
+# 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: Sections [
+# 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.265983.patch
Type: text/x-patch
Size: 3058 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200525/e9ff64c4/attachment-0001.bin>


More information about the llvm-commits mailing list