diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp --- a/lld/wasm/SyntheticSections.cpp +++ b/lld/wasm/SyntheticSections.cpp @@ -497,6 +497,17 @@ writeU8(os, is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD, "add"); } writeU8(os, WASM_OPCODE_END, "opcode:end"); + } else if (config->extendedConst && config->isPic && !sym->isStub && + isa(sym)) { + // We can use an extended init expression to add a constant + // offset of __table_base. + auto *f = cast(sym); + writeU8(os, WASM_OPCODE_GLOBAL_GET, "global get"); + writeUleb128(os, WasmSym::tableBase->getGlobalIndex(), + "literal (global index)"); + writePtrConst(os, f->getTableIndex(), is64, "offset"); + writeU8(os, is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD, "add"); + writeU8(os, WASM_OPCODE_END, "opcode:end"); } else { WasmInitExpr initExpr; if (auto *d = dyn_cast(sym))