aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
diff options
context:
space:
mode:
authorJordan Rupprecht <rupprecht@google.com>2018-08-17 22:34:48 +0000
committerJordan Rupprecht <rupprecht@google.com>2018-08-17 22:34:48 +0000
commitbe8ebccaed22135669fd3716ed4cd235085ef5fd (patch)
tree3e49c58a2d41d305e9c27d1b54d9049ad6881131 /llvm/tools/llvm-objcopy/llvm-objcopy.cpp
parent04df67e268eaf4c63b7fd0712b0c54a6db89c1cf (diff)
downloadllvm-be8ebccaed22135669fd3716ed4cd235085ef5fd.zip
llvm-be8ebccaed22135669fd3716ed4cd235085ef5fd.tar.gz
llvm-be8ebccaed22135669fd3716ed4cd235085ef5fd.tar.bz2
[llvm-objcopy] Implement -G/--keep-global-symbol(s).
Summary: Port GNU Objcopy -G/--keep-global-symbol(s). This is slightly different than the already-implemented --globalize-symbol, which marks a symbol as global when copying. When --keep-global-symbol (alias -G) is used, *only* those symbols marked will stay global, and all other globals are demoted to local. (Also note that it doesn't *promote* a symbol to global). Additionally, there is a pluralized version of the flag --keep-global-symbols, which effectively applies --keep-global-symbol for every non-comment in a file. Reviewers: jakehehrlich, jhenderson, alexshap Reviewed By: jhenderson Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D50589 llvm-svn: 340105
Diffstat (limited to 'llvm/tools/llvm-objcopy/llvm-objcopy.cpp')
-rw-r--r--llvm/tools/llvm-objcopy/llvm-objcopy.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
index 7713aa6f..699dca6 100644
--- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
+++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -154,6 +154,7 @@ struct CopyConfig {
std::vector<StringRef> SymbolsToRemove;
std::vector<StringRef> SymbolsToWeaken;
std::vector<StringRef> ToRemove;
+ std::vector<std::string> SymbolsToKeepGlobal;
// Map options
StringMap<SectionRename> SectionsToRename;
@@ -428,6 +429,20 @@ static void handleArgs(const CopyConfig &Config, Object &Obj,
is_contained(Config.SymbolsToLocalize, Sym.Name)))
Sym.Binding = STB_LOCAL;
+ // Note: these two globalize flags have very similar names but different
+ // meanings:
+ //
+ // --globalize-symbol: promote a symbol to global
+ // --keep-global-symbol: all symbols except for these should be made local
+ //
+ // If --globalize-symbol is specified for a given symbol, it will be
+ // global in the output file even if it is not included via
+ // --keep-global-symbol. Because of that, make sure to check
+ // --globalize-symbol second.
+ if (!Config.SymbolsToKeepGlobal.empty() &&
+ !is_contained(Config.SymbolsToKeepGlobal, Sym.Name))
+ Sym.Binding = STB_LOCAL;
+
if (!Config.SymbolsToGlobalize.empty() &&
is_contained(Config.SymbolsToGlobalize, Sym.Name))
Sym.Binding = STB_GLOBAL;
@@ -782,6 +797,23 @@ static void executeElfObjcopy(const CopyConfig &Config) {
}
}
+static void addGlobalSymbolsFromFile(std::vector<std::string> &Symbols,
+ 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) {
+ // Ignore everything after '#', trim whitespace, and only add the symbol if
+ // it's not empty.
+ auto TrimmedLine = Line.split('#').first.trim();
+ if (!TrimmedLine.empty())
+ Symbols.push_back(TrimmedLine.str());
+ }
+}
+
// ParseObjcopyOptions returns the config and sets the input arguments. If a
// help flag is set then ParseObjcopyOptions will print the help messege and
// exit.
@@ -870,6 +902,10 @@ static CopyConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
Config.SymbolsToLocalize.push_back(Arg->getValue());
+ for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol))
+ Config.SymbolsToKeepGlobal.push_back(Arg->getValue());
+ for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols))
+ addGlobalSymbolsFromFile(Config.SymbolsToKeepGlobal, Arg->getValue());
for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
Config.SymbolsToGlobalize.push_back(Arg->getValue());
for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))