[PATCH] D110352: [llvm-objcopy] Rename relocation sections together with their targets.

Igor Kudrin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 23 21:37:48 PDT 2021


ikudrin updated this revision to Diff 374725.
ikudrin marked 2 inline comments as done.
ikudrin added a comment.

Thanks for the review!

- Combined tests
- Updated comments


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

https://reviews.llvm.org/D110352

Files:
  llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test
  llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp


Index: llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
===================================================================
--- llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
+++ llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
@@ -588,15 +588,33 @@
     return E;
 
   if (!Config.SectionsToRename.empty()) {
+    std::vector<RelocationSectionBase *> RelocSections;
+    DenseSet<SectionBase *> RenamedSections;
     for (SectionBase &Sec : Obj.sections()) {
+      // Postpone processing relocation sections until after their target
+      // sections are renamed.
+      // Dynamic relocation sections (i.e. ones with SHF_ALLOC) should be
+      // renamed as normal sections to match the behavior of GNU objcopy.
+      auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec);
+      if (RelocSec && !(Sec.Flags & SHF_ALLOC)) {
+        RelocSections.push_back(RelocSec);
+        continue;
+      }
       const auto Iter = Config.SectionsToRename.find(Sec.Name);
       if (Iter != Config.SectionsToRename.end()) {
         const SectionRename &SR = Iter->second;
         Sec.Name = std::string(SR.NewName);
         if (SR.NewFlags.hasValue())
           setSectionFlagsAndType(Sec, SR.NewFlags.getValue());
+        RenamedSections.insert(&Sec);
       }
     }
+    // Rename relocation sections according to their targets.
+    for (RelocationSectionBase *RelocSec : RelocSections) {
+      auto Iter = RenamedSections.find(RelocSec->getSection());
+      if (Iter != RenamedSections.end())
+        RelocSec->Name = (RelocSec->getPrefix() + (*Iter)->Name).str();
+    }
   }
 
   // Add a prefix to allocated sections and their relocation sections. This
Index: llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test
@@ -0,0 +1,43 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy %t %t2 --rename-section=.text=.text2 \
+# RUN:   --rename-section=.data=.data2 --rename-section=.rela.plt=.rela.plt2
+# RUN: llvm-readobj --sections %t2 | FileCheck %s
+
+## Check that when renaming a section then the corresponding relocation section
+## is renamed similarly.
+## A dynamic relocation section can be renamed with an explicit
+## --rename-section. That resembles the behavior of GNU objcopy.
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC ]
+  - Name:            .rel.text
+    Type:            SHT_REL
+    Info:            .text
+  - Name:            .rela.data
+    Type:            SHT_RELA
+    Info:            .data
+  - Name:            .data
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC ]
+  - Name:            .rela.plt
+    Type:            SHT_RELA
+    Flags:           [ SHF_ALLOC ]
+    Link:            0
+  - Name:            .plt
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+
+# CHECK: Name: .text2
+# CHECK: Name: .rel.text2
+# CHECK: Name: .rela.data2
+# CHECK: Name: .data2
+# CHECK: Name: .rela.plt2
+# CHECK: Name: .plt


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110352.374725.patch
Type: text/x-patch
Size: 3257 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210924/7e8acc1e/attachment.bin>


More information about the llvm-commits mailing list