Index: test/tools/llvm-objcopy/ELF/redefine-symbol.test =================================================================== --- test/tools/llvm-objcopy/ELF/redefine-symbol.test +++ test/tools/llvm-objcopy/ELF/redefine-symbol.test @@ -3,6 +3,13 @@ # RUN: llvm-readobj --symbols %t2 | FileCheck %s # RUN: not llvm-objcopy --redefine-sym barbar %t %t2 2>&1 | FileCheck %s --check-prefix=BAD-FORMAT # RUN: not llvm-objcopy --redefine-sym foo=f1 --redefine-sym foo=f2 %t %t2 2>&1 | FileCheck %s --check-prefix=MULTIPLE-REDEFINITION +# RUN: echo " foo oof #rename foo " > %t.rename.txt +# RUN: echo "empty" >> %t.rename.txt +# RUN: llvm-objcopy --redefine-syms %t.rename.txt %t %t3 2>&1 | FileCheck %s --check-prefix=WARNING +# RUN: cmp %t2 %t3 +# RUN: echo " bar rab #rename bar " > %t.rename2.txt +# RUN: llvm-objcopy --redefine-syms %t.rename.txt --redefine-syms %t.rename2.txt %t %t4 +# RUN: llvm-readobj --symbols %t4 | FileCheck %s --check-prefix=MULTIPLE-FILES !ELF FileHeader: @@ -79,3 +86,9 @@ #BAD-FORMAT: Bad format for --redefine-sym #MULTIPLE-REDEFINITION: Multiple redefinition of symbol foo +#WARNING: rename.txt:2: missing new symbol name + +#MULTIPLE-FILES: Name: oof +#MULTIPLE-FILES-NEXT: Value: 0x1004 +#MULTIPLE-FILES: Name: rab +#MULTIPLE-FILES-NEXT: Value: 0x2000 \ No newline at end of file Index: tools/llvm-objcopy/CopyConfig.cpp =================================================================== --- tools/llvm-objcopy/CopyConfig.cpp +++ tools/llvm-objcopy/CopyConfig.cpp @@ -255,6 +255,29 @@ ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)); } +static void addSymbolsToRenameFromFile(StringMap &SymbolsToRename, + BumpPtrAllocator &Alloc, + StringRef Filename) { + StringSaver Saver(Alloc); + SmallVector Lines; + auto BufOrErr = MemoryBuffer::getFile(Filename); + if (!BufOrErr) + reportError(Filename, BufOrErr.getError()); + + BufOrErr.get()->getBuffer().split(Lines, '\n'); + for (unsigned LineNo = 0; LineNo < Lines.size(); ++LineNo) { + auto TrimmedLine = Lines[LineNo].split('#').first.trim(); + if (TrimmedLine.empty()) + continue; + + auto Pair = Saver.save(TrimmedLine).split(' '); + StringRef NewName = Pair.second.trim(); + if (NewName.empty()) + warning(Filename + ":" + std::to_string(LineNo + 1) + + ": missing new symbol name"); + SymbolsToRename.insert({Pair.first, NewName}); + } +} // ParseObjcopyOptions returns the config and sets the input arguments. If a // help flag is set then ParseObjcopyOptions will print the help messege and // exit. @@ -358,6 +381,10 @@ error("Multiple redefinition of symbol " + Old2New.first); } + for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbols)) + addSymbolsToRenameFromFile(Config.SymbolsToRename, DC.Alloc, + Arg->getValue()); + for (auto Arg : InputArgs.filtered(OBJCOPY_rename_section)) { SectionRename SR = parseRenameSectionValue(StringRef(Arg->getValue())); if (!Config.SectionsToRename.try_emplace(SR.OriginalName, SR).second) Index: tools/llvm-objcopy/ObjcopyOpts.td =================================================================== --- tools/llvm-objcopy/ObjcopyOpts.td +++ tools/llvm-objcopy/ObjcopyOpts.td @@ -76,6 +76,16 @@ defm redefine_symbol : Eq<"redefine-sym", "Change the name of a symbol old to new">, MetaVarName<"old=new">; +defm redefine_symbols + : Eq<"redefine-syms", + "Reads a list of symbol pairs from and runs as if " + "--redefine-sym== is set for each one. " + "contains two symbol per line separated with whitespace and may " + "contain comments beginning with '#'. Leading and trailing " + "whitespace is stripped from each line. May be repeated to read " + "symbols from many files.">, + MetaVarName<"filename">; + defm keep_section : Eq<"keep-section", "Keep
">, MetaVarName<"section">; defm only_section : Eq<"only-section", "Remove all but
">, Index: tools/llvm-objcopy/llvm-objcopy.h =================================================================== --- tools/llvm-objcopy/llvm-objcopy.h +++ tools/llvm-objcopy/llvm-objcopy.h @@ -18,6 +18,7 @@ namespace llvm { namespace objcopy { +extern void warning(Twine Message); LLVM_ATTRIBUTE_NORETURN extern void error(Twine Message); LLVM_ATTRIBUTE_NORETURN extern void error(Error E); LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File, Error E); Index: tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -52,6 +52,11 @@ // The name this program was invoked as. StringRef ToolName; +void warning(Twine Message) { + WithColor::warning(errs(), ToolName) << Message << ".\n"; + errs().flush(); +} + LLVM_ATTRIBUTE_NORETURN void error(Twine Message) { WithColor::error(errs(), ToolName) << Message << ".\n"; errs().flush();