Skip to content

Commit

Permalink
[ELF] Remove global ctx
Browse files Browse the repository at this point in the history
This commit completes the work that eliminates global variables like
config/target/inputSections/symTab from lld/ELF.

Key changes:

* Introduced `lld::elf::ctx` to encapsulate global state.
* Moved global variables into `Ctx lld::elf::ctx`
* Updated many functions to accept `Ctx &ctx`
* Made `ctx` a local variable (this commit)

If we don't count `static std::mutex`, this is the last major global
state within lld/ELF (minor ones like `SharedFile::vernauxNum`
(33ff9e4) might not all be eliminated
yet).
  • Loading branch information
MaskRay committed Nov 17, 2024
1 parent 7379a19 commit 73bb022
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 93 deletions.
5 changes: 0 additions & 5 deletions lld/ELF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,6 @@ struct InStruct {
std::unique_ptr<StringTableSection> strTab;
std::unique_ptr<SymbolTableBaseSection> symTab;
std::unique_ptr<SymtabShndxSection> symTabShndx;

void reset();
};

struct Ctx {
Expand Down Expand Up @@ -662,15 +660,12 @@ struct Ctx {
llvm::DenseSet<std::pair<const Symbol *, uint64_t>> ppc64noTocRelax;

Ctx();
void reset();

llvm::raw_fd_ostream openAuxiliaryFile(llvm::StringRef, std::error_code &);

ArrayRef<uint8_t> aarch64PauthAbiCoreInfo;
};

LLVM_LIBRARY_VISIBILITY extern Ctx ctx;

// The first two elements of versionDefinitions represent VER_NDX_LOCAL and
// VER_NDX_GLOBAL. This helper returns other elements.
static inline ArrayRef<VersionDefinition> namedVersionDefs(Ctx &ctx) {
Expand Down
58 changes: 1 addition & 57 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ using namespace llvm::support;
using namespace lld;
using namespace lld::elf;

Ctx elf::ctx;

static void setConfigs(Ctx &ctx, opt::InputArgList &args);
static void readConfigs(Ctx &ctx, opt::InputArgList &args);

Expand All @@ -102,55 +100,6 @@ ELFSyncStream elf::InternalErr(Ctx &ctx, const uint8_t *buf) {

Ctx::Ctx() : driver(*this) {}

void Ctx::reset() {
arg.~Config();
new (&arg) Config();
driver.~LinkerDriver();
new (&driver) LinkerDriver(*this);
script = nullptr;
target.reset();

commonCtx = nullptr;
errHandler = nullptr;

bufferStart = nullptr;
mainPart = nullptr;
tlsPhdr = nullptr;
out = OutSections{};
outputSections.clear();
partitions.clear();

in.reset();
sym = ElfSym{};
symtab = std::make_unique<SymbolTable>(*this);

memoryBuffers.clear();
objectFiles.clear();
sharedFiles.clear();
binaryFiles.clear();
bitcodeFiles.clear();
lazyBitcodeFiles.clear();
inputSections.clear();
ehInputSections.clear();

symAux.clear();
duplicates.clear();
nonPrevailingSyms.clear();
whyExtractRecords.clear();
backwardReferences.clear();
auxiliaryFiles.clear();
tar.reset();
internalFile = nullptr;
hasSympart.store(false, std::memory_order_relaxed);
hasTlsIe.store(false, std::memory_order_relaxed);
needsTlsLd.store(false, std::memory_order_relaxed);
vernauxNum = 0;
scriptSymOrderCounter = 1;
scriptSymOrder.clear();
ppc64noTocRelax.clear();
ltoAllVtablesHaveTypeInfos = false;
}

llvm::raw_fd_ostream Ctx::openAuxiliaryFile(llvm::StringRef filename,
std::error_code &ec) {
using namespace llvm::sys::fs;
Expand All @@ -163,21 +112,16 @@ namespace lld {
namespace elf {
bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
Ctx ctx;
// This driver-specific context will be freed later by unsafeLldMain().
auto *context = new CommonLinkerContext;

context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
context->e.cleanupCallback = []() {
Ctx &ctx = elf::ctx;
ctx.reset();
ctx.partitions.emplace_back(ctx);
};
context->e.logName = args::getFilenameWithoutExe(args[0]);
context->e.errorLimitExceededMsg =
"too many errors emitted, stopping now (use "
"--error-limit=0 to see all errors)";

Ctx &ctx = elf::ctx;
LinkerScript script(ctx);
ctx.script = &script;
ctx.commonCtx = context;
Expand Down
31 changes: 0 additions & 31 deletions lld/ELF/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4504,37 +4504,6 @@ void PartitionIndexSection::writeTo(uint8_t *buf) {
}
}

void InStruct::reset() {
attributes.reset();
riscvAttributes.reset();
bss.reset();
bssRelRo.reset();
got.reset();
gotPlt.reset();
igotPlt.reset();
relroPadding.reset();
armCmseSGSection.reset();
ppc64LongBranchTarget.reset();
mipsAbiFlags.reset();
mipsGot.reset();
mipsOptions.reset();
mipsReginfo.reset();
mipsRldMap.reset();
partEnd.reset();
partIndex.reset();
plt.reset();
iplt.reset();
ppc32Got2.reset();
ibtPlt.reset();
relaPlt.reset();
debugNames.reset();
gdbIndex.reset();
shStrTab.reset();
strTab.reset();
symTab.reset();
symTabShndx.reset();
}

static bool needsInterpSection(Ctx &ctx) {
return !ctx.arg.relocatable && !ctx.arg.shared &&
!ctx.arg.dynamicLinker.empty() && ctx.script->needsInterpSection();
Expand Down

0 comments on commit 73bb022

Please sign in to comment.