Skip to content

[WebAssembly] Set IS_64 flag correctly on __indirect_function_table in object files #94487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions llvm/include/llvm/MC/MCSymbolWasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ class MCSymbolWasm : public MCSymbol {
return isTable() && hasTableType() &&
getTableType().ElemType == wasm::ValType::FUNCREF;
}
void setFunctionTable() {
void setFunctionTable(bool is64) {
setType(wasm::WASM_SYMBOL_TYPE_TABLE);
setTableType(wasm::ValType::FUNCREF);
uint8_t flags =
is64 ? wasm::WASM_LIMITS_FLAG_IS_64 : wasm::WASM_LIMITS_FLAG_NONE;
setTableType(wasm::ValType::FUNCREF, flags);
}

void setUsedInGOT() const { IsUsedInGOT = true; }
Expand All @@ -140,10 +142,11 @@ class MCSymbolWasm : public MCSymbol {
return *TableType;
}
void setTableType(wasm::WasmTableType TT) { TableType = TT; }
void setTableType(wasm::ValType VT) {
void setTableType(wasm::ValType VT,
uint8_t flags = wasm::WASM_LIMITS_FLAG_NONE) {
// Declare a table with element type VT and no limits (min size 0, no max
// size).
wasm::WasmLimits Limits = {wasm::WASM_LIMITS_FLAG_NONE, 0, 0};
wasm::WasmLimits Limits = {flags, 0, 0};
setTableType({VT, Limits});
}
};
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/MC/WasmObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
break;
case wasm::WASM_EXTERNAL_TABLE:
W->OS << char(Import.Table.ElemType);
encodeULEB128(0, W->OS); // flags
encodeULEB128(Import.Table.Limits.Flags, W->OS);
encodeULEB128(NumElements, W->OS); // initial
break;
case wasm::WASM_EXTERNAL_TAG:
Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,15 @@ static wasm::WasmLimits DefaultLimits() {
}

static MCSymbolWasm *GetOrCreateFunctionTableSymbol(MCContext &Ctx,
const StringRef &Name) {
const StringRef &Name,
bool is64) {
MCSymbolWasm *Sym = cast_or_null<MCSymbolWasm>(Ctx.lookupSymbol(Name));
if (Sym) {
if (!Sym->isFunctionTable())
Ctx.reportError(SMLoc(), "symbol is not a wasm funcref table");
} else {
Sym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(Name));
Sym->setFunctionTable();
Sym->setFunctionTable(is64);
// The default function table is synthesized by the linker.
Sym->setUndefined();
}
Expand Down Expand Up @@ -258,7 +259,7 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
MCAsmParserExtension::Initialize(Parser);

DefaultFunctionTable = GetOrCreateFunctionTableSymbol(
getContext(), "__indirect_function_table");
getContext(), "__indirect_function_table", is64);
if (!STI->checkFeatures("+reference-types"))
DefaultFunctionTable->setOmitFromLinkingSection();
}
Expand Down Expand Up @@ -508,7 +509,7 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
auto &Tok = Lexer.getTok();
if (Tok.is(AsmToken::Identifier)) {
auto *Sym =
GetOrCreateFunctionTableSymbol(getContext(), Tok.getString());
GetOrCreateFunctionTableSymbol(getContext(), Tok.getString(), is64);
const auto *Val = MCSymbolRefExpr::create(Sym, getContext());
*Op = std::make_unique<WebAssemblyOperand>(
WebAssemblyOperand::Symbol, Tok.getLoc(), Tok.getEndLoc(),
Expand Down Expand Up @@ -836,6 +837,9 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
// symbol
auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TABLE);
if (is64) {
Limits.Flags |= wasm::WASM_LIMITS_FLAG_IS_64;
}
wasm::WasmTableType Type = {*ElemType, Limits};
WasmSym->setTableType(Type);
TOut.emitTableType(WasmSym);
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/WebAssembly/WebAssemblyUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ MCSymbolWasm *WebAssembly::getOrCreateFunctionTableSymbol(
if (!Sym->isFunctionTable())
Ctx.reportError(SMLoc(), "symbol is not a wasm funcref table");
} else {
bool is64 = Subtarget && Subtarget->getTargetTriple().isArch64Bit();
Sym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(Name));
Sym->setFunctionTable();
Sym->setFunctionTable(is64);
// The default function table is synthesized by the linker.
Sym->setUndefined();
}
Expand Down
1 change: 1 addition & 0 deletions llvm/test/MC/WebAssembly/reloc-pic64.s
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ hidden_func:
# CHECK-NEXT: Index: 0
# CHECK-NEXT: ElemType: FUNCREF
# CHECK-NEXT: Limits:
# CHECK-NEXT: Flags: [ IS_64 ]
# CHECK-NEXT: Minimum: 0x1
# CHECK-NEXT: - Module: GOT.mem
# CHECK-NEXT: Field: default_data
Expand Down
Loading