[PATCH] D57738: [llvm-objcopy] Add --redefine-syms

Eugene Leviant via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 5 01:53:02 PST 2019


evgeny777 created this revision.
evgeny777 added reviewers: rupprecht, jhenderson.
Herald added subscribers: jakehehrlich, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a reviewer: alexshap.

https://reviews.llvm.org/D57738

Files:
  test/tools/llvm-objcopy/ELF/redefine-symbol.test
  tools/llvm-objcopy/CopyConfig.cpp
  tools/llvm-objcopy/ObjcopyOpts.td


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 <filename> and runs as if "
+         "--redefine-sym=<old>=<new> is set for each one. <filename> "
+         "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 <section>">,
                     MetaVarName<"section">;
 defm only_section : Eq<"only-section", "Remove all but <section>">,
Index: tools/llvm-objcopy/CopyConfig.cpp
===================================================================
--- tools/llvm-objcopy/CopyConfig.cpp
+++ tools/llvm-objcopy/CopyConfig.cpp
@@ -244,6 +244,22 @@
   }
 }
 
+static void addSymbolsToRenameFromFile(StringMap<StringRef> &SymbolsToRename,
+                                       StringRef Filename) {
+  SmallVector<StringRef, 16> Lines;
+  auto BufOrErr = MemoryBuffer::getFile(Filename);
+  if (!BufOrErr)
+    reportError(Filename, BufOrErr.getError());
+
+  BufOrErr.get()->getBuffer().split(Lines, '\n');
+  for (StringRef Line : Lines) {
+    auto TrimmedLine = Line.split('#').first.trim();
+    if (!TrimmedLine.empty()) {
+      auto Pair = TrimmedLine.split(' ');
+      SymbolsToRename.insert({Pair.first, Pair.second.trim()});
+    }
+  }
+}
 // ParseObjcopyOptions returns the config and sets the input arguments. If a
 // help flag is set then ParseObjcopyOptions will print the help messege and
 // exit.
@@ -346,6 +362,9 @@
       error("Multiple redefinition of symbol " + Old2New.first);
   }
 
+  for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbols))
+    addSymbolsToRenameFromFile(Config.SymbolsToRename, 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: 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,10 @@
 # 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
+# RUN: cmp %t2 %t3
 
 !ELF
 FileHeader:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57738.185256.patch
Type: text/x-patch
Size: 3150 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190205/36f341a7/attachment.bin>


More information about the llvm-commits mailing list