Skip to content

Commit 5745c5f

Browse files
committed
[llvm-objcopy][NFC] Use StringSaver for --keep-global-symbols
Summary: Use StringSaver/BumpPtrAlloc when parsing lines from --keep-global-symbols files. This allows us to consistently use StringRef for driver options, which avoids copying the full strings for each object copied, as well as simplifies part of D57517. Reviewers: jhenderson, evgeny777, alexshap Subscribers: jakehehrlich Tags: #llvm Differential Revision: https://reviews.llvm.org/D57617 llvm-svn: 353068
1 parent 0b3cf24 commit 5745c5f

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

llvm/tools/llvm-objcopy/CopyConfig.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "llvm/Support/CommandLine.h"
2020
#include "llvm/Support/Compression.h"
2121
#include "llvm/Support/MemoryBuffer.h"
22+
#include "llvm/Support/StringSaver.h"
2223
#include <memory>
23-
#include <string>
2424

2525
namespace llvm {
2626
namespace objcopy {
@@ -225,8 +225,10 @@ static const MachineInfo &getOutputFormatMachineInfo(StringRef Format) {
225225
return Iter->getValue();
226226
}
227227

228-
static void addGlobalSymbolsFromFile(std::vector<std::string> &Symbols,
228+
static void addGlobalSymbolsFromFile(std::vector<StringRef> &Symbols,
229+
BumpPtrAllocator &Alloc,
229230
StringRef Filename) {
231+
StringSaver Saver(Alloc);
230232
SmallVector<StringRef, 16> Lines;
231233
auto BufOrErr = MemoryBuffer::getFile(Filename);
232234
if (!BufOrErr)
@@ -238,14 +240,15 @@ static void addGlobalSymbolsFromFile(std::vector<std::string> &Symbols,
238240
// it's not empty.
239241
auto TrimmedLine = Line.split('#').first.trim();
240242
if (!TrimmedLine.empty())
241-
Symbols.push_back(TrimmedLine.str());
243+
Symbols.push_back(Saver.save(TrimmedLine));
242244
}
243245
}
244246

245247
// ParseObjcopyOptions returns the config and sets the input arguments. If a
246248
// help flag is set then ParseObjcopyOptions will print the help messege and
247249
// exit.
248250
DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
251+
DriverConfig DC;
249252
ObjcopyOptTable T;
250253
unsigned MissingArgumentIndex, MissingArgumentCount;
251254
llvm::opt::InputArgList InputArgs =
@@ -401,7 +404,8 @@ DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
401404
for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol))
402405
Config.SymbolsToKeepGlobal.push_back(Arg->getValue());
403406
for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbols))
404-
addGlobalSymbolsFromFile(Config.SymbolsToKeepGlobal, Arg->getValue());
407+
addGlobalSymbolsFromFile(Config.SymbolsToKeepGlobal, DC.Alloc,
408+
Arg->getValue());
405409
for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
406410
Config.SymbolsToGlobalize.push_back(Arg->getValue());
407411
for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
@@ -426,7 +430,6 @@ DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
426430
if (Config.DecompressDebugSections && !zlib::isAvailable())
427431
error("LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress.");
428432

429-
DriverConfig DC;
430433
DC.CopyConfigs.push_back(std::move(Config));
431434
return DC;
432435
}

llvm/tools/llvm-objcopy/CopyConfig.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#include "llvm/ADT/SmallVector.h"
1515
#include "llvm/ADT/StringMap.h"
1616
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/Support/Allocator.h"
1718
// Necessary for llvm::DebugCompressionType::None
1819
#include "llvm/Target/TargetOptions.h"
19-
#include <string>
2020
#include <vector>
2121

2222
namespace llvm {
@@ -81,7 +81,7 @@ struct CopyConfig {
8181
std::vector<StringRef> SymbolsToRemove;
8282
std::vector<StringRef> SymbolsToWeaken;
8383
std::vector<StringRef> ToRemove;
84-
std::vector<std::string> SymbolsToKeepGlobal;
84+
std::vector<StringRef> SymbolsToKeepGlobal;
8585

8686
// Map options
8787
StringMap<SectionRename> SectionsToRename;
@@ -112,6 +112,7 @@ struct CopyConfig {
112112
// will contain one or more CopyConfigs.
113113
struct DriverConfig {
114114
SmallVector<CopyConfig, 1> CopyConfigs;
115+
BumpPtrAllocator Alloc;
115116
};
116117

117118
// ParseObjcopyOptions returns the config and sets the input arguments. If a

0 commit comments

Comments
 (0)