[WebAssembly] Write minimal types section
Don't include type signatures that are not referenced by
some relocation.
We don't include this in the -gc-sections settings since
we are always building the type section from scratch,
just like we do the table elements.
In the future we might want to unify the relocation
processing which is currently done once for gc-sections
and then again for building the sympathetic type and
table sections.
Differential Revision: https://reviews.llvm.org/D42747
llvm-svn: 323931
diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index a45b669..e0680f6 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -689,15 +689,23 @@
}
void Writer::calculateTypes() {
+ // The output type section is the union of the following sets:
+ // 1. Any signature used in the TYPE relocation
+ // 2. The signatures of all imported functions
+ // 3. The signatures of all defined functions
+
for (ObjFile *File : Symtab->ObjectFiles) {
- File->TypeMap.reserve(File->getWasmObj()->types().size());
- for (const WasmSignature &Sig : File->getWasmObj()->types())
- File->TypeMap.push_back(registerType(Sig));
+ ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
+ for (uint32_t I = 0; I < Types.size(); I++)
+ if (File->TypeIsUsed[I])
+ File->TypeMap[I] = registerType(Types[I]);
}
- for (Symbol *Sym : Symtab->getSymbols())
- if (Sym->isFunction())
- registerType(Sym->getFunctionType());
+ for (const Symbol *Sym : ImportedFunctions)
+ registerType(Sym->getFunctionType());
+
+ for (const InputFunction *F : DefinedFunctions)
+ registerType(F->Signature);
}
void Writer::assignIndexes() {
@@ -746,25 +754,30 @@
}
for (ObjFile *File : Symtab->ObjectFiles) {
- DEBUG(dbgs() << "Table Indexes: " << File->getName() << "\n");
- auto HandleTableRelocs = [&](InputChunk *Chunk) {
+ DEBUG(dbgs() << "Handle relocs: " << File->getName() << "\n");
+ auto HandleRelocs = [&](InputChunk *Chunk) {
if (Chunk->Discarded)
return;
+ ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
for (const WasmRelocation& Reloc : Chunk->getRelocations()) {
- if (Reloc.Type != R_WEBASSEMBLY_TABLE_INDEX_I32 &&
- Reloc.Type != R_WEBASSEMBLY_TABLE_INDEX_SLEB)
- continue;
- Symbol *Sym = File->getFunctionSymbol(Reloc.Index);
- if (Sym->hasTableIndex() || !Sym->hasOutputIndex())
- continue;
- Sym->setTableIndex(TableIndex++);
- IndirectFunctions.emplace_back(Sym);
+ if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32 ||
+ Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB) {
+ Symbol *Sym = File->getFunctionSymbol(Reloc.Index);
+ if (Sym->hasTableIndex() || !Sym->hasOutputIndex())
+ continue;
+ Sym->setTableIndex(TableIndex++);
+ IndirectFunctions.emplace_back(Sym);
+ } else if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB) {
+ Chunk->File->TypeMap[Reloc.Index] = registerType(Types[Reloc.Index]);
+ Chunk->File->TypeIsUsed[Reloc.Index] = true;
+ }
}
};
+
for (InputFunction* Function : File->Functions)
- HandleTableRelocs(Function);
+ HandleRelocs(Function);
for (InputSegment* Segment : File->Segments)
- HandleTableRelocs(Segment);
+ HandleRelocs(Segment);
}
}
@@ -858,8 +871,6 @@
}
void Writer::run() {
- log("-- calculateTypes");
- calculateTypes();
log("-- calculateImports");
calculateImports();
log("-- assignIndexes");
@@ -870,6 +881,8 @@
calculateInitFunctions();
if (!Config->Relocatable)
createCtorFunction();
+ log("-- calculateTypes");
+ calculateTypes();
if (errorHandler().Verbose) {
log("Defined Functions: " + Twine(DefinedFunctions.size()));