Index: llvm/test/tools/llvm-objcopy/ELF/prefix-alloc-sections.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objcopy/ELF/prefix-alloc-sections.test @@ -0,0 +1,32 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --prefix-alloc-sections=.alloc_prefix %t %t2 +# RUN: llvm-readobj --sections %t2 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_PROGBITS + Flags: [ ] + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + - Name: .data + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_WRITE ] + - Name: .rel.text + Type: SHT_REL + Info: .text + - Name: .rela.data + Type: SHT_RELA + Info: .data + +# CHECK: Name: .foo +# CHECK: Name: .alloc_prefix.text +# CHECK: Name: .alloc_prefix.data +# CHECK: Name: .rel.alloc_prefix.text +# CHECK: Name: .rela.alloc_prefix.data Index: llvm/test/tools/llvm-objcopy/ELF/rename-section-and-prefix-alloc-sections.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objcopy/ELF/rename-section-and-prefix-alloc-sections.test @@ -0,0 +1,28 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --rename-section=.text=.text2 --prefix-alloc-sections=.alloc_prefix %t %t2 +# RUN: llvm-readobj --sections %t2 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_PROGBITS + Flags: [ ] + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + - Name: .rel.text + Type: SHT_REL + Info: .text + - Name: .bar + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + +# CHECK: Name: .foo +# CHECK: Name: .alloc_prefix.text2 +# CHECK: Name: .rel.alloc_prefix.text2 +# CHECK: Name: .alloc_prefix.bar Index: llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp =================================================================== --- llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp +++ llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp @@ -176,7 +176,8 @@ if (!Config.BuildIdLinkDir.empty() || Config.BuildIdLinkInput || Config.BuildIdLinkOutput || !Config.SplitDWO.empty() || - !Config.SymbolsPrefix.empty() || !Config.AddSection.empty() || + !Config.SymbolsPrefix.empty() || !Config.AllocSectionsPrefix.empty() || + !Config.AddSection.empty() || !Config.DumpSection.empty() || !Config.KeepSection.empty() || !Config.SymbolsToGlobalize.empty() || !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() || !Config.SymbolsToWeaken.empty() || Index: llvm/tools/llvm-objcopy/CopyConfig.h =================================================================== --- llvm/tools/llvm-objcopy/CopyConfig.h +++ llvm/tools/llvm-objcopy/CopyConfig.h @@ -113,6 +113,7 @@ Optional BuildIdLinkOutput; StringRef SplitDWO; StringRef SymbolsPrefix; + StringRef AllocSectionsPrefix; DiscardType DiscardMode = DiscardType::None; // Repeated options Index: llvm/tools/llvm-objcopy/CopyConfig.cpp =================================================================== --- llvm/tools/llvm-objcopy/CopyConfig.cpp +++ llvm/tools/llvm-objcopy/CopyConfig.cpp @@ -473,6 +473,8 @@ InputArgs.getLastArgValue(OBJCOPY_build_id_link_output); Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols); + Config.AllocSectionsPrefix = + InputArgs.getLastArgValue(OBJCOPY_prefix_alloc_sections); for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) { if (!StringRef(Arg->getValue()).contains('=')) Index: llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp =================================================================== --- llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -609,6 +609,40 @@ NewSection.Type = SHT_NOTE; } + if (!Config.AllocSectionsPrefix.empty()) { + auto SectionTable = Obj.sections(); + + // Rename relocation sections associated to the allocated sections. For + // example, if we rename .text to .prefix.text, we also rename .rel.text + // to .rel.prefix.text. + for (auto &Sec : SectionTable) { + if (Sec.Info == SHN_UNDEF) + continue; + + StringRef prefix; + switch (Sec.Type) { + case SHT_REL: + prefix = ".rel"; + break; + case SHT_RELA: + prefix = ".rela"; + break; + default: + continue; + } + + auto *TargetSec = SectionTable.getSection( + Sec.Info, "Link field value " + Twine(Sec.Info) + " in section " + + Sec.Name + " is invalid"); + + Sec.Name = (prefix + Config.AllocSectionsPrefix + TargetSec->Name).str(); + } + + for (auto &Sec : SectionTable) + if ((Sec.Flags & SHF_ALLOC) != 0) + Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str(); + } + for (const auto &Flag : Config.DumpSection) { std::pair SecPair = Flag.split("="); StringRef SecName = SecPair.first; Index: llvm/tools/llvm-objcopy/ObjcopyOpts.td =================================================================== --- llvm/tools/llvm-objcopy/ObjcopyOpts.td +++ llvm/tools/llvm-objcopy/ObjcopyOpts.td @@ -222,6 +222,10 @@ : Eq<"prefix-symbols", "Add to the start of every symbol name">, MetaVarName<"prefix">; +defm prefix_alloc_sections + : Eq<"prefix-alloc-sections", "Add to the start of every allocated section names">, + MetaVarName<"prefix">; + def version : Flag<["-", "--"], "version">, HelpText<"Print the version and exit.">; def V : Flag<["-"], "V">, Alias;