Skip to content

Commit e925e52

Browse files
authored
[lld][WebAssembly] Fix check for implicitly exported mutable globals (#160966)
This check is designed to avoid exporting mutable globals in the case when mutable globals are not available. However, it was being applied in all cases even when mutable globals was enabled. This error is particularly bad since mutable-globals have been enabled in default CPU for a while now, meaning that this condition should not be firing in the wild very often.
1 parent 597f93d commit e925e52

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lld/test/wasm/mutable-global-exports.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ _start:
7373
# CHECK-ALL-NEXT: - Name: __wasm_call_ctors
7474
# CHECK-ALL-NEXT: Kind: FUNCTION
7575
# CHECK-ALL-NEXT: Index: 0
76+
# CHECK-ALL-NEXT: - Name: __stack_pointer
77+
# CHECK-ALL-NEXT: Kind: GLOBAL
78+
# CHECK-ALL-NEXT: Index: 0
7679
# CHECK-ALL-NEXT: - Name: _start
7780
# CHECK-ALL-NEXT: Kind: FUNCTION
7881
# CHECK-ALL-NEXT: Index: 1

lld/wasm/Writer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,9 @@ void Writer::calculateExports() {
784784
unsigned globalIndex =
785785
out.importSec->getNumImportedGlobals() + out.globalSec->numGlobals();
786786

787+
bool hasMutableGlobals =
788+
out.targetFeaturesSec->features.count("mutable-globals") > 0;
789+
787790
for (Symbol *sym : symtab->symbols()) {
788791
if (!sym->isExported())
789792
continue;
@@ -801,7 +804,8 @@ void Writer::calculateExports() {
801804
}
802805
export_ = {name, WASM_EXTERNAL_FUNCTION, f->getExportedFunctionIndex()};
803806
} else if (auto *g = dyn_cast<DefinedGlobal>(sym)) {
804-
if (g->getGlobalType()->Mutable && !g->getFile() && !g->forceExport) {
807+
if (!hasMutableGlobals && g->getGlobalType()->Mutable && !g->getFile() &&
808+
!g->isExportedExplicit()) {
805809
// Avoid exporting mutable globals are linker synthesized (e.g.
806810
// __stack_pointer or __tls_base) unless they are explicitly exported
807811
// from the command line.

0 commit comments

Comments
 (0)