[WebAssembly] Seal imports section before counting imports
Summary:
Before we can assign entries in the function of global index space
we need to know the total number of function and global imports
respectively.
To avoid programmer error this change seals that imports section before
assigned function and global index space. Any attempt to add an import
after the section is sealed will assert.
The lack this such as check caused https://reviews.llvm.org/D61876
to be reverted. I'm also trying to craft a test case the this
failure.
Subscribers: dschuff, jgravelle-google, aheejin, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62240
llvm-svn: 361470
diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index eb567ec..01dbd82 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -439,7 +439,7 @@
WasmExport{FunctionTableName, WASM_EXTERNAL_TABLE, 0});
unsigned FakeGlobalIndex =
- Out.ImportSec->NumImportedGlobals + Out.GlobalSec->InputGlobals.size();
+ Out.ImportSec->numImportedGlobals() + Out.GlobalSec->InputGlobals.size();
for (Symbol *Sym : Symtab->getSymbols()) {
if (!Sym->isExported())
@@ -532,7 +532,9 @@
}
void Writer::assignIndexes() {
- assert(Out.FunctionSec->InputFunctions.empty());
+ // Seal the import section, since other index spaces such as function and
+ // global are effected by the number of imports.
+ Out.ImportSec->seal();
for (InputFunction *Func : Symtab->SyntheticFunctions)
Out.FunctionSec->addFunction(Func);
@@ -543,8 +545,6 @@
Out.FunctionSec->addFunction(Func);
}
- scanRelocations();
-
for (InputGlobal *Global : Symtab->SyntheticGlobals)
Out.GlobalSec->addGlobal(Global);
@@ -724,6 +724,8 @@
populateTargetFeatures();
log("-- calculateImports");
calculateImports();
+ log("-- scanRelocations");
+ scanRelocations();
log("-- assignIndexes");
assignIndexes();
log("-- calculateInitFunctions");
@@ -750,9 +752,9 @@
log("Defined Functions: " + Twine(Out.FunctionSec->InputFunctions.size()));
log("Defined Globals : " + Twine(Out.GlobalSec->InputGlobals.size()));
log("Defined Events : " + Twine(Out.EventSec->InputEvents.size()));
- log("Function Imports : " + Twine(Out.ImportSec->NumImportedFunctions));
- log("Global Imports : " + Twine(Out.ImportSec->NumImportedGlobals));
- log("Event Imports : " + Twine(Out.ImportSec->NumImportedEvents));
+ log("Function Imports : " + Twine(Out.ImportSec->numImportedFunctions()));
+ log("Global Imports : " + Twine(Out.ImportSec->numImportedGlobals()));
+ log("Event Imports : " + Twine(Out.ImportSec->numImportedEvents()));
for (ObjFile *File : Symtab->ObjectFiles)
File->dumpInfo();
}