Skip to content

Commit 184c22d

Browse files
authored
[lld][WebAssembly] Move linker global state in to context object. NFC (#78629)
See lld/ELF/Config.h
1 parent f268495 commit 184c22d

12 files changed

+64
-65
lines changed

lld/wasm/Config.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ struct Configuration {
7171
uint64_t initialHeap;
7272
uint64_t initialMemory;
7373
uint64_t maxMemory;
74+
// The table offset at which to place function addresses. We reserve zero
75+
// for the null function pointer. This gets set to 1 for executables and 0
76+
// for shared libraries (since they always added to a dynamic offset at
77+
// runtime).
78+
uint64_t tableBase;
7479
uint64_t zStackSize;
7580
unsigned ltoPartitions;
7681
unsigned ltoo;
@@ -96,24 +101,20 @@ struct Configuration {
96101
std::optional<std::vector<std::string>> features;
97102
std::optional<std::vector<std::string>> extraFeatures;
98103
llvm::SmallVector<uint8_t, 0> buildIdVector;
104+
};
99105

100-
// The following config options do not directly correspond to any
101-
// particular command line options, and should probably be moved to separate
102-
// Ctx struct as in ELF/Config.h
106+
// The only instance of Configuration struct.
107+
extern Configuration *config;
103108

109+
// The Ctx object hold all other (non-configuration) global state.
110+
struct Ctx {
104111
// True if we are creating position-independent code.
105112
bool isPic;
106113

107114
// True if we have an MVP input that uses __indirect_function_table and which
108115
// requires it to be allocated to table number 0.
109116
bool legacyFunctionTable = false;
110117

111-
// The table offset at which to place function addresses. We reserve zero
112-
// for the null function pointer. This gets set to 1 for executables and 0
113-
// for shared libraries (since they always added to a dynamic offset at
114-
// runtime).
115-
uint32_t tableBase = 0;
116-
117118
// Will be set to true if bss data segments should be emitted. In most cases
118119
// this is not necessary.
119120
bool emitBssSegments = false;
@@ -124,8 +125,7 @@ struct Configuration {
124125
whyExtractRecords;
125126
};
126127

127-
// The only instance of Configuration struct.
128-
extern Configuration *config;
128+
extern Ctx ctx;
129129

130130
} // namespace lld::wasm
131131

lld/wasm/Driver.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ using namespace llvm::wasm;
4545

4646
namespace lld::wasm {
4747
Configuration *config;
48+
Ctx ctx;
4849

4950
namespace {
5051

@@ -308,7 +309,7 @@ static std::optional<std::string> searchLibraryBaseName(StringRef name) {
308309
for (StringRef dir : config->searchPaths) {
309310
// Currently we don't enable dynamic linking at all unless -shared or -pie
310311
// are used, so don't even look for .so files in that case..
311-
if (config->isPic && !config->isStatic)
312+
if (ctx.isPic && !config->isStatic)
312313
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".so"))
313314
return s;
314315
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".a"))
@@ -571,9 +572,9 @@ static void readConfigs(opt::InputArgList &args) {
571572
// This function initialize such members. See Config.h for the details
572573
// of these values.
573574
static void setConfigs() {
574-
config->isPic = config->pie || config->shared;
575+
ctx.isPic = config->pie || config->shared;
575576

576-
if (config->isPic) {
577+
if (ctx.isPic) {
577578
if (config->exportTable)
578579
error("-shared/-pie is incompatible with --export-table");
579580
config->importTable = true;
@@ -680,7 +681,7 @@ static void checkOptions(opt::InputArgList &args) {
680681
warn("-Bsymbolic is only meaningful when combined with -shared");
681682
}
682683

683-
if (config->isPic) {
684+
if (ctx.isPic) {
684685
if (config->globalBase)
685686
error("--global-base may not be used with -shared/-pie");
686687
if (config->tableBase)
@@ -707,7 +708,7 @@ static Symbol *handleUndefined(StringRef name, const char *option) {
707708
if (auto *lazySym = dyn_cast<LazySymbol>(sym)) {
708709
lazySym->extract();
709710
if (!config->whyExtract.empty())
710-
config->whyExtractRecords.emplace_back(option, sym->getFile(), *sym);
711+
ctx.whyExtractRecords.emplace_back(option, sym->getFile(), *sym);
711712
}
712713

713714
return sym;
@@ -722,8 +723,7 @@ static void handleLibcall(StringRef name) {
722723
MemoryBufferRef mb = lazySym->getMemberBuffer();
723724
if (isBitcode(mb)) {
724725
if (!config->whyExtract.empty())
725-
config->whyExtractRecords.emplace_back("<libcall>", sym->getFile(),
726-
*sym);
726+
ctx.whyExtractRecords.emplace_back("<libcall>", sym->getFile(), *sym);
727727
lazySym->extract();
728728
}
729729
}
@@ -742,7 +742,7 @@ static void writeWhyExtract() {
742742
}
743743

744744
os << "reference\textracted\tsymbol\n";
745-
for (auto &entry : config->whyExtractRecords) {
745+
for (auto &entry : ctx.whyExtractRecords) {
746746
os << std::get<0>(entry) << '\t' << toString(std::get<1>(entry)) << '\t'
747747
<< toString(std::get<2>(entry)) << '\n';
748748
}
@@ -811,7 +811,7 @@ static void createSyntheticSymbols() {
811811

812812
bool is64 = config->is64.value_or(false);
813813

814-
if (config->isPic) {
814+
if (ctx.isPic) {
815815
WasmSym::stackPointer =
816816
createUndefinedGlobal("__stack_pointer", config->is64.value_or(false)
817817
? &mutableGlobalTypeI64
@@ -850,7 +850,7 @@ static void createSyntheticSymbols() {
850850
"__wasm_init_tls"));
851851
}
852852

853-
if (config->isPic ||
853+
if (ctx.isPic ||
854854
config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic) {
855855
// For PIC code, or when dynamically importing addresses, we create
856856
// synthetic functions that apply relocations. These get called from
@@ -871,7 +871,7 @@ static void createOptionalSymbols() {
871871
if (!config->shared)
872872
WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
873873

874-
if (!config->isPic) {
874+
if (!ctx.isPic) {
875875
WasmSym::stackLow = symtab->addOptionalDataSymbol("__stack_low");
876876
WasmSym::stackHigh = symtab->addOptionalDataSymbol("__stack_high");
877877
WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
@@ -967,8 +967,8 @@ static void processStubLibraries() {
967967
depsAdded = true;
968968
lazy->extract();
969969
if (!config->whyExtract.empty())
970-
config->whyExtractRecords.emplace_back(stub_file->getName(),
971-
sym->getFile(), *sym);
970+
ctx.whyExtractRecords.emplace_back(stub_file->getName(),
971+
sym->getFile(), *sym);
972972
}
973973
}
974974
}
@@ -1305,7 +1305,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
13051305
sym->forceExport = true;
13061306
}
13071307

1308-
if (!config->relocatable && !config->isPic) {
1308+
if (!config->relocatable && !ctx.isPic) {
13091309
// Add synthetic dummies for weak undefined functions. Must happen
13101310
// after LTO otherwise functions may not yet have signatures.
13111311
symtab->handleWeakUndefines();

lld/wasm/InputChunks.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ void InputChunk::generateRelocationCode(raw_ostream &os) const {
378378
uint64_t offset = getVA(rel.Offset) - getInputSectionOffset();
379379

380380
Symbol *sym = file->getSymbol(rel);
381-
if (!config->isPic && sym->isDefined())
381+
if (!ctx.isPic && sym->isDefined())
382382
continue;
383383

384384
LLVM_DEBUG(dbgs() << "gen reloc: type=" << relocTypeToString(rel.Type)
@@ -390,7 +390,7 @@ void InputChunk::generateRelocationCode(raw_ostream &os) const {
390390
writeSleb128(os, offset, "offset");
391391

392392
// In PIC mode we need to add the __memory_base
393-
if (config->isPic) {
393+
if (ctx.isPic) {
394394
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
395395
if (isTLS())
396396
writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "tls_base");
@@ -417,7 +417,7 @@ void InputChunk::generateRelocationCode(raw_ostream &os) const {
417417
writeU8(os, opcode_reloc_add, "ADD");
418418
}
419419
} else {
420-
assert(config->isPic);
420+
assert(ctx.isPic);
421421
const GlobalSymbol* baseSymbol = WasmSym::memoryBase;
422422
if (rel.Type == R_WASM_TABLE_INDEX_I32 ||
423423
rel.Type == R_WASM_TABLE_INDEX_I64)

lld/wasm/InputFiles.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ void ObjFile::addLegacyIndirectFunctionTableIfNeeded(
351351

352352
// We assume that this compilation unit has unrelocatable references to
353353
// this table.
354-
config->legacyFunctionTable = true;
354+
ctx.legacyFunctionTable = true;
355355
}
356356

357357
static bool shouldMerge(const WasmSection &sec) {

lld/wasm/LTO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static std::unique_ptr<lto::LTO> createLTO() {
5555

5656
if (config->relocatable)
5757
c.RelocModel = std::nullopt;
58-
else if (config->isPic)
58+
else if (ctx.isPic)
5959
c.RelocModel = Reloc::PIC_;
6060
else
6161
c.RelocModel = Reloc::Static;

lld/wasm/MarkLive.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ bool MarkLive::isCallCtorsLive() {
187187

188188
// In Emscripten-style PIC, we call `__wasm_call_ctors` which calls
189189
// `__wasm_apply_data_relocs`.
190-
if (config->isPic)
190+
if (ctx.isPic)
191191
return true;
192192

193193
// If there are any init functions, mark `__wasm_call_ctors` live so that

lld/wasm/OutputSections.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void DataSection::finalizeContents() {
107107
});
108108
#endif
109109

110-
assert((config->sharedMemory || !config->isPic || config->extendedConst ||
110+
assert((config->sharedMemory || !ctx.isPic || config->extendedConst ||
111111
activeCount <= 1) &&
112112
"output segments should have been combined by now");
113113

@@ -124,7 +124,7 @@ void DataSection::finalizeContents() {
124124
if (segment->initFlags & WASM_DATA_SEGMENT_HAS_MEMINDEX)
125125
writeUleb128(os, 0, "memory index");
126126
if ((segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
127-
if (config->isPic && config->extendedConst) {
127+
if (ctx.isPic && config->extendedConst) {
128128
writeU8(os, WASM_OPCODE_GLOBAL_GET, "global get");
129129
writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(),
130130
"literal (global index)");
@@ -136,7 +136,7 @@ void DataSection::finalizeContents() {
136136
} else {
137137
WasmInitExpr initExpr;
138138
initExpr.Extended = false;
139-
if (config->isPic) {
139+
if (ctx.isPic) {
140140
assert(segment->startVA == 0);
141141
initExpr.Inst.Opcode = WASM_OPCODE_GLOBAL_GET;
142142
initExpr.Inst.Value.Global = WasmSym::memoryBase->getGlobalIndex();

lld/wasm/OutputSegment.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class OutputSegment {
2727
// to the output binary. However if the memory is imported, and
2828
// we can't use memory.fill during startup (due to lack of bulk
2929
// memory feature) then we include BSS segments verbatim.
30-
bool requiredInBinary() const { return !isBss || config->emitBssSegments; }
30+
bool requiredInBinary() const { return !isBss || ctx.emitBssSegments; }
3131

3232
bool isTLS() const { return name == ".tdata"; }
3333

lld/wasm/Relocations.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using namespace llvm::wasm;
1919
namespace lld::wasm {
2020

2121
static bool requiresGOTAccess(const Symbol *sym) {
22-
if (!config->isPic &&
22+
if (!ctx.isPic &&
2323
config->unresolvedSymbols != UnresolvedPolicy::ImportDynamic)
2424
return false;
2525
if (sym->isHidden() || sym->isLocal())
@@ -141,7 +141,7 @@ void scanRelocations(InputChunk *chunk) {
141141
break;
142142
}
143143

144-
if (config->isPic ||
144+
if (ctx.isPic ||
145145
(sym->isUndefined() &&
146146
config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic)) {
147147
switch (reloc.Type) {

lld/wasm/SymbolTable.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,7 @@ Symbol *SymbolTable::addUndefinedFunction(StringRef name,
535535
} else {
536536
lazy->extract();
537537
if (!config->whyExtract.empty())
538-
config->whyExtractRecords.emplace_back(toString(file), s->getFile(),
539-
*s);
538+
ctx.whyExtractRecords.emplace_back(toString(file), s->getFile(), *s);
540539
}
541540
} else {
542541
auto existingFunction = dyn_cast<FunctionSymbol>(s);
@@ -774,7 +773,7 @@ void SymbolTable::addLazy(ArchiveFile *file, const Archive::Symbol *sym) {
774773
const InputFile *oldFile = s->getFile();
775774
file->addMember(sym);
776775
if (!config->whyExtract.empty())
777-
config->whyExtractRecords.emplace_back(toString(oldFile), s->getFile(), *s);
776+
ctx.whyExtractRecords.emplace_back(toString(oldFile), s->getFile(), *s);
778777
}
779778

780779
bool SymbolTable::addComdat(StringRef name) {

lld/wasm/SyntheticSections.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class SubSection {
5555
} // namespace
5656

5757
bool DylinkSection::isNeeded() const {
58-
return config->isPic ||
58+
return ctx.isPic ||
5959
config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic ||
6060
!symtab->sharedFiles.empty();
6161
}
@@ -174,7 +174,7 @@ void ImportSection::addGOTEntry(Symbol *sym) {
174174
return;
175175
LLVM_DEBUG(dbgs() << "addGOTEntry: " << toString(*sym) << "\n");
176176
sym->setGOTIndex(numImportedGlobals++);
177-
if (config->isPic) {
177+
if (ctx.isPic) {
178178
// Any symbol that is assigned an normal GOT entry must be exported
179179
// otherwise the dynamic linker won't be able create the entry that contains
180180
// it.
@@ -319,7 +319,7 @@ void TableSection::addTable(InputTable *table) {
319319
return;
320320
// Some inputs require that the indirect function table be assigned to table
321321
// number 0.
322-
if (config->legacyFunctionTable &&
322+
if (ctx.legacyFunctionTable &&
323323
isa<DefinedTable>(WasmSym::indirectFunctionTable) &&
324324
cast<DefinedTable>(WasmSym::indirectFunctionTable)->table == table) {
325325
if (out.importSec->getNumImportedTables()) {
@@ -474,7 +474,7 @@ void GlobalSection::writeBody() {
474474
// In the case of dynamic linking, unless we have 'extended-const'
475475
// available, these global must to be mutable since they get updated to
476476
// the correct runtime value during `__wasm_apply_global_relocs`.
477-
if (!config->extendedConst && config->isPic && !sym->isTLS())
477+
if (!config->extendedConst && ctx.isPic && !sym->isTLS())
478478
mutable_ = true;
479479
// With multi-theadeding any TLS globals must be mutable since they get
480480
// set during `__wasm_apply_global_tls_relocs`
@@ -487,7 +487,7 @@ void GlobalSection::writeBody() {
487487
bool useExtendedConst = false;
488488
uint32_t globalIdx;
489489
int64_t offset;
490-
if (config->extendedConst && config->isPic) {
490+
if (config->extendedConst && ctx.isPic) {
491491
if (auto *d = dyn_cast<DefinedData>(sym)) {
492492
if (!sym->isTLS()) {
493493
globalIdx = WasmSym::memoryBase->getGlobalIndex();
@@ -582,7 +582,7 @@ void ElemSection::writeBody() {
582582

583583
WasmInitExpr initExpr;
584584
initExpr.Extended = false;
585-
if (config->isPic) {
585+
if (ctx.isPic) {
586586
initExpr.Inst.Opcode = WASM_OPCODE_GLOBAL_GET;
587587
initExpr.Inst.Value.Global =
588588
(config->is64.value_or(false) ? WasmSym::tableBase32

0 commit comments

Comments
 (0)