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 index 0000000000000..d1f7ce556c14a --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test @@ -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 diff --git a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp index ece1334d69636..8aa6d79145b08 100644 --- a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -588,14 +588,32 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig, return E; if (!Config.SectionsToRename.empty()) { + std::vector RelocSections; + DenseSet RenamedSections; for (SectionBase &Sec : Obj.sections()) { + auto *RelocSec = dyn_cast(&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(); } }