Skip to content

Commit 4629aa1

Browse files
committed
[ELF] Move script into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such singletons. We now use default-initialization for `LinkerScript` and should pay attention to non-class types (e.g. `dot` is initialized by commit 503907d).
1 parent 503907d commit 4629aa1

11 files changed

+128
-127
lines changed

lld/ELF/Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Defined;
4646
class Symbol;
4747
class BitcodeCompiler;
4848
class OutputSection;
49+
class LinkerScript;
4950
struct Partition;
5051
struct PhdrEntry;
5152

@@ -483,6 +484,7 @@ struct DuplicateSymbol {
483484

484485
struct Ctx {
485486
LinkerDriver driver;
487+
LinkerScript *script;
486488

487489
// These variables are initialized by Writer and should not be used before
488490
// Writer is initialized.

lld/ELF/Driver.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void elf::errorOrWarn(const Twine &msg) {
9393

9494
void Ctx::reset() {
9595
driver = LinkerDriver();
96+
script = nullptr;
9697

9798
bufferStart = nullptr;
9899
mainPart = nullptr;
@@ -160,8 +161,9 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
160161
"--error-limit=0 to see all errors)";
161162

162163
config = ConfigWrapper();
163-
script = ScriptWrapper();
164164

165+
LinkerScript script;
166+
elf::ctx.script = &script;
165167
elf::ctx.symAux.emplace_back();
166168

167169
partitions.clear();
@@ -463,7 +465,7 @@ static void checkOptions() {
463465
if (config->emachine != EM_AARCH64)
464466
error("--execute-only is only supported on AArch64 targets");
465467

466-
if (config->singleRoRx && !script->hasSectionsCommand)
468+
if (config->singleRoRx && !ctx.script->hasSectionsCommand)
467469
error("--execute-only and --no-rosegment cannot be used together");
468470
}
469471

@@ -2456,10 +2458,10 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
24562458
// Forbid partitions from being used on incompatible targets, and forbid them
24572459
// from being used together with various linker features that assume a single
24582460
// set of output sections.
2459-
if (script->hasSectionsCommand)
2461+
if (ctx.script->hasSectionsCommand)
24602462
error(toString(s->file) +
24612463
": partitions cannot be used with the SECTIONS command");
2462-
if (script->hasPhdrsCommands())
2464+
if (ctx.script->hasPhdrsCommands())
24632465
error(toString(s->file) +
24642466
": partitions cannot be used with the PHDRS command");
24652467
if (!config->sectionStartMap.empty())
@@ -2873,7 +2875,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
28732875
// After potential archive member extraction involving ENTRY and
28742876
// -u/--undefined-glob, check whether PROVIDE symbols should be defined (the
28752877
// RHS may refer to definitions in just extracted object files).
2876-
script->addScriptReferencedSymbolsToSymTable();
2878+
ctx.script->addScriptReferencedSymbolsToSymTable();
28772879

28782880
// Prevent LTO from removing any definition referenced by -u.
28792881
for (StringRef name : config->undefined)
@@ -2939,7 +2941,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
29392941
// We want to declare linker script's symbols early,
29402942
// so that we can version them.
29412943
// They also might be exported if referenced by DSOs.
2942-
script->declareSymbols();
2944+
ctx.script->declareSymbols();
29432945

29442946
// Handle --exclude-libs. This is before scanVersionScript() due to a
29452947
// workaround for Android ndk: for a defined versioned symbol in an archive
@@ -3158,13 +3160,13 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
31583160
llvm::TimeTraceScope timeScope("Assign sections");
31593161

31603162
// Create output sections described by SECTIONS commands.
3161-
script->processSectionCommands();
3163+
ctx.script->processSectionCommands();
31623164

31633165
// Linker scripts control how input sections are assigned to output
31643166
// sections. Input sections that were not handled by scripts are called
31653167
// "orphans", and they are assigned to output sections by the default rule.
31663168
// Process that.
3167-
script->addOrphanSections();
3169+
ctx.script->addOrphanSections();
31683170
}
31693171

31703172
{
@@ -3174,9 +3176,9 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
31743176
// merging MergeInputSections into a single MergeSyntheticSection. From this
31753177
// point onwards InputSectionDescription::sections should be used instead of
31763178
// sectionBases.
3177-
for (SectionCommand *cmd : script->sectionCommands)
3179+
for (SectionCommand *cmd : ctx.script->sectionCommands)
31783180
if (auto *osd = dyn_cast<OutputDesc>(cmd))
3179-
osd->osec.finalizeInputSections(&script.s);
3181+
osd->osec.finalizeInputSections(ctx.script);
31803182
}
31813183

31823184
// Two input sections with different output sections should not be folded.

lld/ELF/ICF.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ template <class ELFT> void ICF<ELFT>::run() {
577577

578578
// InputSectionDescription::sections is populated by processSectionCommands().
579579
// ICF may fold some input sections assigned to output sections. Remove them.
580-
for (SectionCommand *cmd : script->sectionCommands)
580+
for (SectionCommand *cmd : ctx.script->sectionCommands)
581581
if (auto *osd = dyn_cast<OutputDesc>(cmd))
582582
for (SectionCommand *subCmd : osd->osec.commands)
583583
if (auto *isd = dyn_cast<InputSectionDescription>(subCmd))

lld/ELF/LinkerScript.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ using namespace llvm::support::endian;
4444
using namespace lld;
4545
using namespace lld::elf;
4646

47-
ScriptWrapper elf::script;
48-
4947
static bool isSectionPrefix(StringRef prefix, StringRef name) {
5048
return name.consume_front(prefix) && (name.empty() || name[0] == '.');
5149
}
@@ -862,7 +860,7 @@ static OutputSection *findByName(ArrayRef<SectionCommand *> vec,
862860
}
863861

864862
static OutputDesc *createSection(InputSectionBase *isec, StringRef outsecName) {
865-
OutputDesc *osd = script->createOutputSection(outsecName, "<internal>");
863+
OutputDesc *osd = ctx.script->createOutputSection(outsecName, "<internal>");
866864
osd->osec.recordSection(isec);
867865
return osd;
868866
}
@@ -1470,7 +1468,7 @@ void LinkerScript::allocateHeaders(SmallVector<PhdrEntry *, 0> &phdrs) {
14701468
}
14711469

14721470
LinkerScript::AddressState::AddressState() {
1473-
for (auto &mri : script->memoryRegions) {
1471+
for (auto &mri : ctx.script->memoryRegions) {
14741472
MemoryRegion *mr = mri.second;
14751473
mr->curPos = (mr->origin)().getValue();
14761474
}

lld/ELF/LinkerScript.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,6 @@ class LinkerScript final {
446446
llvm::DenseMap<llvm::CachedHashStringRef, SectionClassDesc *> sectionClasses;
447447
};
448448

449-
struct ScriptWrapper {
450-
LinkerScript s;
451-
LinkerScript *operator->() { return &s; }
452-
};
453-
454-
LLVM_LIBRARY_VISIBILITY extern ScriptWrapper script;
455-
456449
} // end namespace lld::elf
457450

458451
#endif // LLD_ELF_LINKER_SCRIPT_H

lld/ELF/MapFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static void writeMapFile(raw_fd_ostream &os) {
158158
<< " Size Align Out In Symbol\n";
159159

160160
OutputSection *osec = nullptr;
161-
for (SectionCommand *cmd : script->sectionCommands) {
161+
for (SectionCommand *cmd : ctx.script->sectionCommands) {
162162
if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
163163
if (assign->provide && !assign->sym)
164164
continue;

lld/ELF/MarkLive.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ template <class ELFT> void MarkLive<ELFT>::run() {
234234
markSymbol(symtab.find(config->fini));
235235
for (StringRef s : config->undefined)
236236
markSymbol(symtab.find(s));
237-
for (StringRef s : script->referencedSymbols)
237+
for (StringRef s : ctx.script->referencedSymbols)
238238
markSymbol(symtab.find(s));
239239
for (auto [symName, _] : symtab.cmseSymMap) {
240240
markSymbol(symtab.cmseSymMap[symName].sym);
@@ -293,7 +293,7 @@ template <class ELFT> void MarkLive<ELFT>::run() {
293293

294294
// Preserve special sections and those which are specified in linker
295295
// script KEEP command.
296-
if (isReserved(sec) || script->shouldKeep(sec)) {
296+
if (isReserved(sec) || ctx.script->shouldKeep(sec)) {
297297
enqueue(sec, 0);
298298
} else if ((!config->zStartStopGC || sec->name.starts_with("__libc_")) &&
299299
isValidCIdentifier(sec->name)) {

lld/ELF/Relocations.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ using namespace lld;
6666
using namespace lld::elf;
6767

6868
static std::optional<std::string> getLinkerScriptLocation(const Symbol &sym) {
69-
for (SectionCommand *cmd : script->sectionCommands)
69+
for (SectionCommand *cmd : ctx.script->sectionCommands)
7070
if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
7171
if (assign->sym == &sym)
7272
return assign->location;
@@ -2420,7 +2420,7 @@ static void scanCrossRefs(const NoCrossRefCommand &cmd, OutputSection *osec,
24202420
// scan relocations from its input sections for prohibited cross references.
24212421
template <class ELFT> void elf::checkNoCrossRefs() {
24222422
for (OutputSection *osec : ctx.outputSections) {
2423-
for (const NoCrossRefCommand &noxref : script->noCrossRefs) {
2423+
for (const NoCrossRefCommand &noxref : ctx.script->noCrossRefs) {
24242424
if (!llvm::is_contained(noxref.outputSections, osec->name) ||
24252425
(noxref.toFirst && noxref.outputSections[0] == osec->name))
24262426
continue;

0 commit comments

Comments
 (0)