Index: llvm/test/tools/llvm-objcopy/ELF/rename-section-dynrelocsec.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objcopy/ELF/rename-section-dynrelocsec.test @@ -0,0 +1,24 @@ +# RUN: yaml2obj %s -o %t +# RUN: llvm-objcopy --rename-section=.rela.plt=.rela.plt2 %t %t2 +# RUN: llvm-readobj --sections %t2 | FileCheck %s + +## Check that a dynamic relocation section can be renamed using an explicit +## --rename-section. That resembles the behavior of GNU objcopy. + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .rela.plt + Type: SHT_RELA + Flags: [ SHF_ALLOC ] + Link: 0 + - Name: .plt + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + +# CHECK: Name: .rela.plt2 +# CHECK: Name: .plt 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,31 @@ +# RUN: yaml2obj %s -o %t +# RUN: llvm-objcopy --rename-section=.text=.text2 --rename-section=.data=.data2 %t %t2 +# RUN: llvm-readobj --sections %t2 | FileCheck %s + +## Check that when renaming a section then the corresponding relocation section +## is renamed similarly. + +!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 ] + +# CHECK: Name: .text2 +# CHECK: Name: .rel.text2 +# CHECK: Name: .rela.data2 +# CHECK: Name: .data2 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 RelocSections; + DenseSet RenamedSections; for (SectionBase &Sec : Obj.sections()) { + // Postpone processing relocation sections until after their target + // sections are renamed. + // Dynamic relocation sections (i.e. SHT_REL[A] with SHF_ALLOC) should be + // renamed as normal sections to match the behavior of GNU objcopy. + auto *RelocSec = dyn_cast(&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 similarly 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