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,24 @@ +# 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: .test1 + Type: SHT_PROGBITS + Flags: [ ] + - Name: .test2 + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + - Name: .test3 + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + +# CHECK: Name: .test1 +# CHECK: Name: .alloc_prefix.test2 +# CHECK: Name: .alloc_prefix.test3 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,24 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --rename-section=.test2=.test4 --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: .test1 + Type: SHT_PROGBITS + Flags: [ ] + - Name: .test2 + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + - Name: .test3 + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + +# CHECK: Name: .test1 +# CHECK: Name: .alloc_prefix.test4 +# CHECK: Name: .alloc_prefix.test3 Index: llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp =================================================================== --- llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp +++ llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp @@ -177,6 +177,7 @@ if (!Config.BuildIdLinkDir.empty() || Config.BuildIdLinkInput || Config.BuildIdLinkOutput || !Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() || !Config.AddSection.empty() || + !Config.AllocSectionsPrefix.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 @@ -92,6 +92,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 @@ -502,6 +502,7 @@ 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 @@ -593,6 +593,11 @@ NewSection.Type = SHT_NOTE; } + if (!Config.AllocSectionsPrefix.empty()) + for (auto &Sec : Obj.sections()) + 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;