summaryrefslogtreecommitdiffstats
path: root/llvm/tools
diff options
context:
space:
mode:
authorEugene Leviant <eleviant@accesssoftek.com>2019-02-06 11:00:07 +0000
committerEugene Leviant <eleviant@accesssoftek.com>2019-02-06 11:00:07 +0000
commitf324f6dcfba71ede8dd213096ec3b2f1b57ded86 (patch)
tree0d9bcc4640d64e0934fc98f8f125198bb50e8418 /llvm/tools
parentb6b5b1a5921e2319cc40c6899b8fe1cdadc44ee7 (diff)
downloadbcm5719-llvm-f324f6dcfba71ede8dd213096ec3b2f1b57ded86.tar.gz
bcm5719-llvm-f324f6dcfba71ede8dd213096ec3b2f1b57ded86.zip
[llvm-objcopy] Allow regular expressions in name comparison
Differential revision: https://reviews.llvm.org/D57517 llvm-svn: 353289
Diffstat (limited to 'llvm/tools')
-rw-r--r--llvm/tools/llvm-objcopy/CopyConfig.cpp47
-rw-r--r--llvm/tools/llvm-objcopy/CopyConfig.h30
-rw-r--r--llvm/tools/llvm-objcopy/ObjcopyOpts.td4
-rw-r--r--llvm/tools/llvm-objcopy/StripOpts.td4
4 files changed, 59 insertions, 26 deletions
diff --git a/llvm/tools/llvm-objcopy/CopyConfig.cpp b/llvm/tools/llvm-objcopy/CopyConfig.cpp
index 91721962503..d013ee4f35d 100644
--- a/llvm/tools/llvm-objcopy/CopyConfig.cpp
+++ b/llvm/tools/llvm-objcopy/CopyConfig.cpp
@@ -225,9 +225,9 @@ static const MachineInfo &getOutputFormatMachineInfo(StringRef Format) {
return Iter->getValue();
}
-static void addGlobalSymbolsFromFile(std::vector<StringRef> &Symbols,
+static void addGlobalSymbolsFromFile(std::vector<NameOrRegex> &Symbols,
BumpPtrAllocator &Alloc,
- StringRef Filename) {
+ StringRef Filename, bool UseRegex) {
StringSaver Saver(Alloc);
SmallVector<StringRef, 16> Lines;
auto BufOrErr = MemoryBuffer::getFile(Filename);
@@ -240,10 +240,21 @@ static void addGlobalSymbolsFromFile(std::vector<StringRef> &Symbols,
// it's not empty.
auto TrimmedLine = Line.split('#').first.trim();
if (!TrimmedLine.empty())
- Symbols.push_back(Saver.save(TrimmedLine));
+ Symbols.emplace_back(Saver.save(TrimmedLine), UseRegex);
}
}
+NameOrRegex::NameOrRegex(StringRef Pattern, bool IsRegex) {
+ if (!IsRegex) {
+ Name = Pattern;
+ return;
+ }
+
+ SmallVector<char, 32> Data;
+ R = std::make_shared<Regex>(
+ ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data));
+}
+
// ParseObjcopyOptions returns the config and sets the input arguments. If a
// help flag is set then ParseObjcopyOptions will print the help messege and
// exit.
@@ -292,6 +303,7 @@ DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
InputArgs.hasArg(OBJCOPY_output_target)))
error("--target cannot be used with --input-target or --output-target");
+ bool UseRegex = InputArgs.hasArg(OBJCOPY_regex);
if (InputArgs.hasArg(OBJCOPY_target)) {
Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_target);
@@ -371,11 +383,11 @@ DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
}
for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
- Config.ToRemove.push_back(Arg->getValue());
+ Config.ToRemove.emplace_back(Arg->getValue(), UseRegex);
for (auto Arg : InputArgs.filtered(OBJCOPY_keep_section))
- Config.KeepSection.push_back(Arg->getValue());
+ Config.KeepSection.emplace_back(Arg->getValue(), UseRegex);
for (auto Arg : InputArgs.filtered(OBJCOPY_only_section))
- Config.OnlySection.push_back(Arg->getValue());
+ Config.OnlySection.emplace_back(Arg->getValue(), UseRegex);
for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
Config.AddSection.push_back(Arg->getValue());
for (auto Arg : InputArgs.filtered(OBJCOPY_dump_section))
@@ -400,20 +412,20 @@ DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
Config.DecompressDebugSections =
InputArgs.hasArg(OBJCOPY_decompress_debug_sections);
for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
- Config.SymbolsToLocalize.push_back(Arg->getValue());
+ Config.SymbolsToLocalize.emplace_back(Arg->getValue(), UseRegex);
for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol))
- Config.SymbolsToKeepGlobal.push_back(Arg->getValue());
+ Config.SymbolsToKeepGlobal.emplace_back(Arg->getValue(), UseRegex);
for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols))
addGlobalSymbolsFromFile(Config.SymbolsToKeepGlobal, DC.Alloc,
- Arg->getValue());
+ Arg->getValue(), UseRegex);
for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
- Config.SymbolsToGlobalize.push_back(Arg->getValue());
+ Config.SymbolsToGlobalize.emplace_back(Arg->getValue(), UseRegex);
for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
- Config.SymbolsToWeaken.push_back(Arg->getValue());
+ Config.SymbolsToWeaken.emplace_back(Arg->getValue(), UseRegex);
for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol))
- Config.SymbolsToRemove.push_back(Arg->getValue());
+ Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegex);
for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol))
- Config.SymbolsToKeep.push_back(Arg->getValue());
+ Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegex);
Config.DeterministicArchives = InputArgs.hasFlag(
OBJCOPY_enable_deterministic_archives,
@@ -472,6 +484,7 @@ DriverConfig parseStripOptions(ArrayRef<const char *> ArgsArr) {
error("Multiple input files cannot be used in combination with -o");
CopyConfig Config;
+ bool UseRegexp = InputArgs.hasArg(STRIP_regex);
Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug);
if (InputArgs.hasArg(STRIP_discard_all, STRIP_discard_locals))
@@ -485,16 +498,16 @@ DriverConfig parseStripOptions(ArrayRef<const char *> ArgsArr) {
Config.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols);
for (auto Arg : InputArgs.filtered(STRIP_keep_section))
- Config.KeepSection.push_back(Arg->getValue());
+ Config.KeepSection.emplace_back(Arg->getValue(), UseRegexp);
for (auto Arg : InputArgs.filtered(STRIP_remove_section))
- Config.ToRemove.push_back(Arg->getValue());
+ Config.ToRemove.emplace_back(Arg->getValue(), UseRegexp);
for (auto Arg : InputArgs.filtered(STRIP_strip_symbol))
- Config.SymbolsToRemove.push_back(Arg->getValue());
+ Config.SymbolsToRemove.emplace_back(Arg->getValue(), UseRegexp);
for (auto Arg : InputArgs.filtered(STRIP_keep_symbol))
- Config.SymbolsToKeep.push_back(Arg->getValue());
+ Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegexp);
if (!Config.StripDebug && !Config.StripUnneeded &&
Config.DiscardMode == DiscardType::None && !Config.StripAllGNU && Config.SymbolsToRemove.empty())
diff --git a/llvm/tools/llvm-objcopy/CopyConfig.h b/llvm/tools/llvm-objcopy/CopyConfig.h
index 9d16c7b4582..b03860465bd 100644
--- a/llvm/tools/llvm-objcopy/CopyConfig.h
+++ b/llvm/tools/llvm-objcopy/CopyConfig.h
@@ -15,6 +15,7 @@
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Allocator.h"
+#include "llvm/Support/Regex.h"
// Necessary for llvm::DebugCompressionType::None
#include "llvm/Target/TargetOptions.h"
#include <vector>
@@ -48,6 +49,17 @@ enum class DiscardType {
Locals, // --discard-locals (-X)
};
+class NameOrRegex {
+ StringRef Name;
+ // Regex is shared between multiple CopyConfig instances.
+ std::shared_ptr<Regex> R;
+
+public:
+ NameOrRegex(StringRef Pattern, bool IsRegex);
+ bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; }
+ bool operator!=(StringRef S) const { return !operator==(S); }
+};
+
// Configuration for copying/stripping a single file.
struct CopyConfig {
// Main input/output options
@@ -73,15 +85,15 @@ struct CopyConfig {
// Repeated options
std::vector<StringRef> AddSection;
std::vector<StringRef> DumpSection;
- std::vector<StringRef> KeepSection;
- std::vector<StringRef> OnlySection;
- std::vector<StringRef> SymbolsToGlobalize;
- std::vector<StringRef> SymbolsToKeep;
- std::vector<StringRef> SymbolsToLocalize;
- std::vector<StringRef> SymbolsToRemove;
- std::vector<StringRef> SymbolsToWeaken;
- std::vector<StringRef> ToRemove;
- std::vector<StringRef> SymbolsToKeepGlobal;
+ std::vector<NameOrRegex> KeepSection;
+ std::vector<NameOrRegex> OnlySection;
+ std::vector<NameOrRegex> SymbolsToGlobalize;
+ std::vector<NameOrRegex> SymbolsToKeep;
+ std::vector<NameOrRegex> SymbolsToLocalize;
+ std::vector<NameOrRegex> SymbolsToRemove;
+ std::vector<NameOrRegex> SymbolsToWeaken;
+ std::vector<NameOrRegex> ToRemove;
+ std::vector<NameOrRegex> SymbolsToKeepGlobal;
// Map options
StringMap<SectionRename> SectionsToRename;
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOpts.td b/llvm/tools/llvm-objcopy/ObjcopyOpts.td
index a25d1f3b515..bc1ac7869de 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOpts.td
+++ b/llvm/tools/llvm-objcopy/ObjcopyOpts.td
@@ -193,3 +193,7 @@ defm build_id_link_output
: Eq<"build-id-link-output", "Hard-link the output to <dir>/xx/xxx<suffix> "
"name derived from hex build ID">,
MetaVarName<"suffix">;
+
+def regex
+ : Flag<["-", "--"], "regex">,
+ HelpText<"Permit regular expressions in name comparison">;
diff --git a/llvm/tools/llvm-objcopy/StripOpts.td b/llvm/tools/llvm-objcopy/StripOpts.td
index b29f9ee6e17..c3f351992a0 100644
--- a/llvm/tools/llvm-objcopy/StripOpts.td
+++ b/llvm/tools/llvm-objcopy/StripOpts.td
@@ -74,6 +74,10 @@ def discard_all
HelpText<"Remove all local symbols except file and section symbols">;
def x : Flag<["-"], "x">, Alias<discard_all>;
+def regex
+ : Flag<["-", "--"], "regex">,
+ HelpText<"Permit regular expressions in name comparison">;
+
def version : Flag<["-", "--"], "version">,
HelpText<"Print the version and exit.">;
def V : Flag<["-"], "V">, Alias<version>;
OpenPOWER on IntegriCloud