diff --git a/llvm/docs/CommandGuide/llvm-objcopy.rst b/llvm/docs/CommandGuide/llvm-objcopy.rst --- a/llvm/docs/CommandGuide/llvm-objcopy.rst +++ b/llvm/docs/CommandGuide/llvm-objcopy.rst @@ -84,6 +84,19 @@ For MachO objects, ``
`` must be formatted as ``,
``. +.. option:: --redefine-sym = + + Rename symbols called ```` to ```` in the output. Can be specified + multiple times to rename multiple symbols. + +.. option:: --redefine-syms + + Rename symbols in the output as described in the file ````. In the + file, each line represents a single symbol to rename, with the old name and new + name separated by an equals sign. Leading and trailing whitespace is ignored, + as is anything following a '#'. Can be specified multiple times to read names + from multiple files. + .. option:: --regex If specified, symbol and section names specified by other switches are treated @@ -378,19 +391,6 @@ Preserve access and modification timestamps in the output. -.. option:: --redefine-sym = - - Rename symbols called ```` to ```` in the output. Can be specified - multiple times to rename multiple symbols. - -.. option:: --redefine-syms - - Rename symbols in the output as described in the file ````. In the - file, each line represents a single symbol to rename, with the old name and new - name separated by an equals sign. Leading and trailing whitespace is ignored, - as is anything following a '#'. Can be specified multiple times to read names - from multiple files. - .. option:: --rename-section =[,,...] Rename sections called ```` to ```` in the output, and apply any diff --git a/llvm/test/tools/llvm-objcopy/COFF/redefine-symbol.test b/llvm/test/tools/llvm-objcopy/COFF/redefine-symbol.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/COFF/redefine-symbol.test @@ -0,0 +1,67 @@ +# RUN: yaml2obj %s -o %t + +# RUN: llvm-objcopy --redefine-sym func=cnuf --redefine-sym foo=bar %t %t2 +# RUN: llvm-readobj --symbols %t2 | FileCheck %s + +# RUN: echo 'func cnuf #rename func' > %t.rename.txt +# RUN: echo 'foo bar' >> %t.rename.txt +# RUN: llvm-objcopy --redefine-syms %t.rename.txt %t %t3 +# RUN: cmp %t2 %t3 + +# CHECK: Symbol { +# CHECK: Symbol { +# CHECK: Symbol { +# CHECK-NEXT: Name: cnuf +# CHECK-NEXT: Value: 0 +# CHECK-NEXT: Section: .text (1) +# CHECK-NEXT: BaseType: Null (0x0) +# CHECK-NEXT: ComplexType: Function (0x2) +# CHECK-NEXT: StorageClass: External (0x2) +# CHECK-NEXT: AuxSymbolCount: 0 +# CHECK-NEXT: } +# CHECK-NEXT: Symbol { +# CHECK-NEXT: Name: bar +# CHECK-NEXT: Value: 0 +# CHECK-NEXT: Section: .rdata (2) +# CHECK-NEXT: BaseType: Null (0x0) +# CHECK-NEXT: ComplexType: Null (0x0) +# CHECK-NEXT: StorageClass: External (0x2) +# CHECK-NEXT: AuxSymbolCount: 0 +# CHECK-NEXT: } + +--- !COFF +header: + Machine: IMAGE_FILE_MACHINE_AMD64 + Characteristics: [ ] +sections: + - Name: .text + Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ] + - Name: .rdata + Characteristics: [ ] + Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ] +symbols: + - Name: .text + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC + - Name: .rdata + Value: 0 + SectionNumber: 2 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC + - Name: func + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_FUNCTION + StorageClass: IMAGE_SYM_CLASS_EXTERNAL + - Name: foo + Value: 0 + SectionNumber: 2 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL +... diff --git a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp --- a/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp +++ b/llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp @@ -131,6 +131,12 @@ if (Error E = Obj.markSymbols()) return E; + for (Symbol &Sym : Obj.getMutableSymbols()) { + auto I = Config.SymbolsToRename.find(Sym.Name); + if (I != Config.SymbolsToRename.end()) + Sym.Name = I->getValue(); + } + // Actually do removals of symbols. Obj.removeSymbols([&](const Symbol &Sym) { // For StripAll, all relocations have been stripped and we remove all @@ -200,10 +206,9 @@ !Config.SymbolsToLocalize.empty() || !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() || !Config.SectionsToRename.empty() || !Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() || - !Config.SymbolsToRename.empty() || Config.ExtractDWO || - Config.KeepFileSymbols || Config.LocalizeHidden || Config.PreserveDates || - Config.StripDWO || Config.StripNonAlloc || Config.StripSections || - Config.Weaken || Config.DecompressDebugSections || + Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden || + Config.PreserveDates || Config.StripDWO || Config.StripNonAlloc || + Config.StripSections || Config.Weaken || Config.DecompressDebugSections || Config.DiscardMode == DiscardType::Locals || !Config.SymbolsToAdd.empty() || Config.EntryExpr) { return createStringError(llvm::errc::invalid_argument,