[llvm-objcopy] Rename relocation sections together with their targets.
authorIgor Kudrin <ikudrin@accesssoftek.com>
Wed, 29 Sep 2021 09:36:37 +0000 (16:36 +0700)
committerIgor Kudrin <ikudrin@accesssoftek.com>
Wed, 29 Sep 2021 09:36:37 +0000 (16:36 +0700)
As for now, llvm-objcopy renames only sections that are specified
explicitly in --rename-section, while GNU objcopy keeps names of
relocation sections in sync with their targets. For example:

> readelf -S test.o
...
  [ 1] .foo      PROGBITS
  [ 2] .rela.foo RELA

> objcopy --rename-section .foo=.bar test.o gnu.o
> readelf -S gnu.o
...
  [ 1] .bar      PROGBITS
  [ 2] .rela.bar RELA

> llvm-objcopy --rename-section .foo=.bar test.o llvm.o
> readelf -S llvm.o
...
  [ 1] .bar      PROGBITS
  [ 2] .rela.foo RELA

This patch makes llvm-objcopy to match the behavior of GNU objcopy better.

Differential Revision: https://reviews.llvm.org/D110352

llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test [new file with mode: 0644]
llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp

diff --git a/llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test b/llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test
new file mode 100644 (file)
index 0000000..d1f7ce5
--- /dev/null
@@ -0,0 +1,103 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy %t %t1 \
+# RUN:    --rename-section=.tst1.foo=.tst1.ren.foo \
+# RUN:    --rename-section=.tst1.bar=.tst1.ren.bar \
+# RUN:    --rename-section=.tst2.foo=.tst2.ren.foo \
+# RUN:    --rename-section=.tst3.foo=.tst3.ren.foo \
+# RUN:    --rename-section=.tst4.foo=.tst4.ren.foo \
+# RUN:    --rename-section=.rel.tst4.foo=.tst4.ren.foo.rel \
+# RUN:    --rename-section=.tst6.got.plt=.tst6.ren.got.plt \
+# RUN:    --rename-section=.tst7.rela.plt=.tst7.ren.rela.plt
+# RUN: llvm-readobj --sections %t1 | FileCheck %s
+
+!ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+
+## Test 1. When a section is renamed, its relocation section should be renamed
+## similarly.
+# CHECK: Name: .tst1.ren.foo{{ }}
+# CHECK: Name: .rel.tst1.ren.foo{{ }}
+# CHECK: Name: .rela.tst1.ren.bar{{ }}
+# CHECK: Name: .tst1.ren.bar{{ }}
+  - Name:  .tst1.foo
+    Type:  SHT_PROGBITS
+  - Name:  .rel.tst1.foo
+    Type:  SHT_REL
+    Info:  .tst1.foo
+  - Name:  .rela.tst1.bar
+    Type:  SHT_RELA
+    Info:  .tst1.bar
+  - Name:  .tst1.bar
+    Type:  SHT_PROGBITS
+
+## Test 2. A relocation section should be renamed together with its target
+## section even if its name does not follow the typical pattern.
+# CHECK: Name: .tst2.ren.foo{{ }}
+# CHECK: Name: .rel.tst2.ren.foo{{ }}
+  - Name:  .tst2.foo
+    Type:  SHT_PROGBITS
+  - Name:  .tst2.foo.rel
+    Type:  SHT_REL
+    Info:  .tst2.foo
+
+## Test 3. A relocation section should not be renamed if an unrelated section
+## with the same common name is renamed.
+## Note that '.rel.tst3.foo' targets '.tst3.bar', not '.tst3.foo'
+# CHECK: Name: .tst3.ren.foo{{ }}
+# CHECK: Name: .tst3.bar{{ }}
+# CHECK: Name: .rel.tst3.foo{{ }}
+  - Name:  .tst3.foo
+    Type:  SHT_PROGBITS
+  - Name:  .tst3.bar
+    Type:  SHT_PROGBITS
+  - Name:  .rel.tst3.foo
+    Type:  SHT_REL
+    Info:  .tst3.bar
+
+## Test 4. A relocation section can be renamed with an explicit --rename-section
+## command. Explicit renaming overrides implicit one.
+# CHECK: Name: .tst4.ren.foo{{ }}
+# CHECK: Name: .tst4.ren.foo.rel{{ }}
+  - Name:  .tst4.foo
+    Type:  SHT_PROGBITS
+  - Name:  .rel.tst4.foo
+    Type:  SHT_REL
+    Info:  .tst4.foo
+
+## Test 5. Should not rename a relocation section if it or its target are not
+## specified in --rename-section.
+## Note that this diverges from GNU objcopy.
+# CHECK: Name: .tst5.foo{{ }}
+# CHECK: Name: .tst5.foo.rel{{ }}
+  - Name:  .tst5.foo
+    Type:  SHT_PROGBITS
+  - Name:  .tst5.foo.rel
+    Type:  SHT_REL
+    Info:  .tst5.foo
+
+## Test 6. A dynamic relocation section should not be renamed together with its
+## target section.
+# CHECK: Name: .tst6.rela.plt{{ }}
+# CHECK: Name: .tst6.ren.got.plt{{ }}
+  - Name:  .tst6.rela.plt
+    Type:  SHT_RELA
+    Flags: [ SHF_ALLOC ]
+    Info:  .tst6.got.plt
+  - Name:  .tst6.got.plt
+    Type:  SHT_PROGBITS
+
+## Test 7. A dynamic relocation section can be renamed with an explicit
+## --rename-section command.
+# CHECK: Name: .tst7.ren.rela.plt{{ }}
+# CHECK: Name: .tst7.got.plt{{ }}
+  - Name:  .tst7.rela.plt
+    Type:  SHT_RELA
+    Flags: [ SHF_ALLOC ]
+    Info:  .tst7.got.plt
+  - Name:  .tst7.got.plt
+    Type:  SHT_PROGBITS
index ece1334..8aa6d79 100644 (file)
@@ -588,14 +588,32 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
     return E;
 
   if (!Config.SectionsToRename.empty()) {
+    std::vector<RelocationSectionBase *> RelocSections;
+    DenseSet<SectionBase *> RenamedSections;
     for (SectionBase &Sec : Obj.sections()) {
+      auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec);
       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);
+      } else if (RelocSec && !(Sec.Flags & SHF_ALLOC))
+        // Postpone processing relocation sections which are not specified in
+        // their explicit '--rename-section' commands until after their target
+        // sections are renamed.
+        // Dynamic relocation sections (i.e. ones with SHF_ALLOC) should be
+        // renamed only explicitly. Otherwise, renaming, for example, '.got.plt'
+        // would affect '.rela.plt', which is not desirable.
+        RelocSections.push_back(RelocSec);
+    }
+
+    // Rename relocation sections according to their target sections.
+    for (RelocationSectionBase *RelocSec : RelocSections) {
+      auto Iter = RenamedSections.find(RelocSec->getSection());
+      if (Iter != RenamedSections.end())
+        RelocSec->Name = (RelocSec->getNamePrefix() + (*Iter)->Name).str();
     }
   }