Skip to content

Commit 73bb022

Browse files
committed
[ELF] Remove global ctx
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).
1 parent 7379a19 commit 73bb022

File tree

3 files changed

+1
-93
lines changed

3 files changed

+1
-93
lines changed

lld/ELF/Config.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,6 @@ struct InStruct {
537537
std::unique_ptr<StringTableSection> strTab;
538538
std::unique_ptr<SymbolTableBaseSection> symTab;
539539
std::unique_ptr<SymtabShndxSection> symTabShndx;
540-
541-
void reset();
542540
};
543541

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

664662
Ctx();
665-
void reset();
666663

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

669666
ArrayRef<uint8_t> aarch64PauthAbiCoreInfo;
670667
};
671668

672-
LLVM_LIBRARY_VISIBILITY extern Ctx ctx;
673-
674669
// The first two elements of versionDefinitions represent VER_NDX_LOCAL and
675670
// VER_NDX_GLOBAL. This helper returns other elements.
676671
static inline ArrayRef<VersionDefinition> namedVersionDefs(Ctx &ctx) {

lld/ELF/Driver.cpp

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ using namespace llvm::support;
7979
using namespace lld;
8080
using namespace lld::elf;
8181

82-
Ctx elf::ctx;
83-
8482
static void setConfigs(Ctx &ctx, opt::InputArgList &args);
8583
static void readConfigs(Ctx &ctx, opt::InputArgList &args);
8684

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

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

105-
void Ctx::reset() {
106-
arg.~Config();
107-
new (&arg) Config();
108-
driver.~LinkerDriver();
109-
new (&driver) LinkerDriver(*this);
110-
script = nullptr;
111-
target.reset();
112-
113-
commonCtx = nullptr;
114-
errHandler = nullptr;
115-
116-
bufferStart = nullptr;
117-
mainPart = nullptr;
118-
tlsPhdr = nullptr;
119-
out = OutSections{};
120-
outputSections.clear();
121-
partitions.clear();
122-
123-
in.reset();
124-
sym = ElfSym{};
125-
symtab = std::make_unique<SymbolTable>(*this);
126-
127-
memoryBuffers.clear();
128-
objectFiles.clear();
129-
sharedFiles.clear();
130-
binaryFiles.clear();
131-
bitcodeFiles.clear();
132-
lazyBitcodeFiles.clear();
133-
inputSections.clear();
134-
ehInputSections.clear();
135-
136-
symAux.clear();
137-
duplicates.clear();
138-
nonPrevailingSyms.clear();
139-
whyExtractRecords.clear();
140-
backwardReferences.clear();
141-
auxiliaryFiles.clear();
142-
tar.reset();
143-
internalFile = nullptr;
144-
hasSympart.store(false, std::memory_order_relaxed);
145-
hasTlsIe.store(false, std::memory_order_relaxed);
146-
needsTlsLd.store(false, std::memory_order_relaxed);
147-
vernauxNum = 0;
148-
scriptSymOrderCounter = 1;
149-
scriptSymOrder.clear();
150-
ppc64noTocRelax.clear();
151-
ltoAllVtablesHaveTypeInfos = false;
152-
}
153-
154103
llvm::raw_fd_ostream Ctx::openAuxiliaryFile(llvm::StringRef filename,
155104
std::error_code &ec) {
156105
using namespace llvm::sys::fs;
@@ -163,21 +112,16 @@ namespace lld {
163112
namespace elf {
164113
bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
165114
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
115+
Ctx ctx;
166116
// This driver-specific context will be freed later by unsafeLldMain().
167117
auto *context = new CommonLinkerContext;
168118

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

180-
Ctx &ctx = elf::ctx;
181125
LinkerScript script(ctx);
182126
ctx.script = &script;
183127
ctx.commonCtx = context;

lld/ELF/SyntheticSections.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4504,37 +4504,6 @@ void PartitionIndexSection::writeTo(uint8_t *buf) {
45044504
}
45054505
}
45064506

4507-
void InStruct::reset() {
4508-
attributes.reset();
4509-
riscvAttributes.reset();
4510-
bss.reset();
4511-
bssRelRo.reset();
4512-
got.reset();
4513-
gotPlt.reset();
4514-
igotPlt.reset();
4515-
relroPadding.reset();
4516-
armCmseSGSection.reset();
4517-
ppc64LongBranchTarget.reset();
4518-
mipsAbiFlags.reset();
4519-
mipsGot.reset();
4520-
mipsOptions.reset();
4521-
mipsReginfo.reset();
4522-
mipsRldMap.reset();
4523-
partEnd.reset();
4524-
partIndex.reset();
4525-
plt.reset();
4526-
iplt.reset();
4527-
ppc32Got2.reset();
4528-
ibtPlt.reset();
4529-
relaPlt.reset();
4530-
debugNames.reset();
4531-
gdbIndex.reset();
4532-
shStrTab.reset();
4533-
strTab.reset();
4534-
symTab.reset();
4535-
symTabShndx.reset();
4536-
}
4537-
45384507
static bool needsInterpSection(Ctx &ctx) {
45394508
return !ctx.arg.relocatable && !ctx.arg.shared &&
45404509
!ctx.arg.dynamicLinker.empty() && ctx.script->needsInterpSection();

0 commit comments

Comments
 (0)