[ELF] Change some global pointers to unique_ptr

Currently the singleton `config` is assigned by `config = make<Configuration>()`
and (if `canExitEarly` is false) destroyed by `lld::freeArena`.

`make<Configuration>` allocates a stab with `malloc(4096)`. This both wastes
memory and bloats the executable (every type instantiates `BumpPtrAllocator`
which costs more than 1KiB code on x86-64).

(No need to worry about `clang::no_destroy`. Regular invocations (`canExitEarly`
is true) call `_Exit` via llvm::sys::Process::ExitNoCleanup.)

Reviewed By: lichray

Differential Revision: https://reviews.llvm.org/D116143
This commit is contained in:
Fangrui Song 2021-12-22 14:36:14 -08:00
parent 3cc2161c89
commit 5fc4323eda
7 changed files with 13 additions and 12 deletions

View File

@ -22,6 +22,7 @@
#include "llvm/Support/GlobPattern.h"
#include "llvm/Support/PrettyStackTrace.h"
#include <atomic>
#include <memory>
#include <vector>
namespace lld {
@ -343,7 +344,7 @@ struct Configuration {
};
// The only instance of Configuration struct.
extern Configuration *config;
extern std::unique_ptr<Configuration> config;
// The first two elements of versionDefinitions represent VER_NDX_LOCAL and
// VER_NDX_GLOBAL. This helper returns other elements.

View File

@ -71,8 +71,8 @@ using namespace llvm::support;
using namespace lld;
using namespace lld::elf;
Configuration *elf::config;
LinkerDriver *elf::driver;
std::unique_ptr<Configuration> elf::config;
std::unique_ptr<LinkerDriver> elf::driver;
static void setConfigs(opt::InputArgList &args);
static void readConfigs(opt::InputArgList &args);
@ -111,10 +111,10 @@ bool elf::link(ArrayRef<const char *> args, bool canExitEarly,
errorHandler().exitEarly = canExitEarly;
stderrOS.enable_colors(stderrOS.has_colors());
config = make<Configuration>();
driver = make<LinkerDriver>();
script = make<LinkerScript>();
symtab = make<SymbolTable>();
config = std::make_unique<Configuration>();
driver = std::make_unique<LinkerDriver>();
script = std::make_unique<LinkerScript>();
symtab = std::make_unique<SymbolTable>();
partitions = {Partition()};

View File

@ -22,7 +22,7 @@
namespace lld {
namespace elf {
extern class LinkerDriver *driver;
extern std::unique_ptr<class LinkerDriver> driver;
class LinkerDriver {
public:

View File

@ -47,7 +47,7 @@ using namespace llvm::support::endian;
using namespace lld;
using namespace lld::elf;
LinkerScript *elf::script;
std::unique_ptr<LinkerScript> elf::script;
static bool isSectionPrefix(StringRef prefix, StringRef name) {
return name.consume_front(prefix) && (name.empty() || name[0] == '.');

View File

@ -366,7 +366,7 @@ public:
std::vector<const InputSectionBase *> orphanSections;
};
extern LinkerScript *script;
extern std::unique_ptr<LinkerScript> script;
} // end namespace elf
} // end namespace lld

View File

@ -29,7 +29,7 @@ using namespace llvm::ELF;
using namespace lld;
using namespace lld::elf;
SymbolTable *elf::symtab;
std::unique_ptr<SymbolTable> elf::symtab;
void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {
// Redirect __real_foo to the original foo and foo to the original __wrap_foo.

View File

@ -91,7 +91,7 @@ private:
llvm::Optional<llvm::StringMap<std::vector<Symbol *>>> demangledSyms;
};
extern SymbolTable *symtab;
extern std::unique_ptr<SymbolTable> symtab;
} // namespace elf
} // namespace lld