diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp --- a/llvm/lib/MC/MCExpr.cpp +++ b/llvm/lib/MC/MCExpr.cpp @@ -690,9 +690,15 @@ const MCSymbolRefExpr *SRE = cast(this); const MCSymbol &Sym = SRE->getSymbol(); + //dbgs() << "MCExpr::evaluateAsRelocatableImpl SymbolRef\n"; + //dbgs() << Sym.isVariable() << "\n"; + //dbgs() << (SRE->getKind() == MCSymbolRefExpr::VK_None) << "\n"; + //dbgs() << canExpand(Sym, InSet) << "\n"; // Evaluate recursively if this is a variable. - if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None && - canExpand(Sym, InSet)) { + if (Sym.isVariable() + && SRE->getKind() == MCSymbolRefExpr::VK_None + && canExpand(Sym, InSet)) { + dbgs() << "recurse\n"; bool IsMachO = SRE->hasSubsectionsViaSymbols(); if (Sym.getVariableValue()->evaluateAsRelocatableImpl( Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) { diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -557,12 +557,13 @@ } static const MCSymbolWasm *resolveSymbol(const MCSymbolWasm &Symbol) { - if (Symbol.isVariable()) { - const MCExpr *Expr = Symbol.getVariableValue(); + const MCSymbolWasm* Ret = &Symbol; + while (Ret->isVariable()) { + const MCExpr *Expr = Ret->getVariableValue(); auto *Inner = cast(Expr); - return cast(&Inner->getSymbol()); + Ret = cast(&Inner->getSymbol()); } - return &Symbol; + return Ret; } // Compute a value to write into the code at the location covered @@ -1421,12 +1422,12 @@ LLVM_DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym << "'\n"); - if (WS.isFunction()) { + if (ResolvedSym->isFunction()) { assert(WasmIndices.count(ResolvedSym) > 0); uint32_t WasmIndex = WasmIndices.find(ResolvedSym)->second; WasmIndices[&WS] = WasmIndex; LLVM_DEBUG(dbgs() << " -> index:" << WasmIndex << "\n"); - } else if (WS.isData()) { + } else if (ResolvedSym->isData()) { assert(DataLocations.count(ResolvedSym) > 0); const wasm::WasmDataReference &Ref = DataLocations.find(ResolvedSym)->second; @@ -1555,15 +1556,16 @@ assert(Fixup.getKind() == MCFixup::getKindForSize(is64Bit() ? 8 : 4, false)); const MCExpr *Expr = Fixup.getValue(); - auto *Sym = dyn_cast(Expr); - if (!Sym) + auto *SymRef = dyn_cast(Expr); + const auto &TargetSym = cast(SymRef->getSymbol()); + if (!SymRef) report_fatal_error("fixups in .init_array should be symbol references"); - if (Sym->getKind() != MCSymbolRefExpr::VK_WebAssembly_FUNCTION) - report_fatal_error("symbols in .init_array should be for functions"); - if (Sym->getSymbol().getIndex() == InvalidIndex) + if (TargetSym.getIndex() == InvalidIndex) report_fatal_error("symbols in .init_array should exist in symbtab"); + if (!TargetSym.isFunction()) + report_fatal_error("symbols in .init_array should be for functions"); InitFuncs.push_back( - std::make_pair(Priority, Sym->getSymbol().getIndex())); + std::make_pair(Priority, TargetSym.getIndex())); } } diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h --- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h +++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h @@ -63,7 +63,6 @@ void EmitConstantPool() override; void EmitFunctionBodyStart() override; void EmitInstruction(const MachineInstr *MI) override; - const MCExpr *lowerConstant(const Constant *CV) override; bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant, const char *ExtraCode, raw_ostream &OS) override; diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp @@ -331,15 +331,6 @@ } } -const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) { - if (const auto *GV = dyn_cast(CV)) - if (GV->getValueType()->isFunctionTy()) { - return MCSymbolRefExpr::create( - getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext); - } - return AsmPrinter::lowerConstant(CV); -} - bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant, const char *ExtraCode, diff --git a/llvm/test/MC/WebAssembly/function-alias.ll b/llvm/test/MC/WebAssembly/function-alias.ll --- a/llvm/test/MC/WebAssembly/function-alias.ll +++ b/llvm/test/MC/WebAssembly/function-alias.ll @@ -5,8 +5,10 @@ @foo = alias i8, bitcast (i8* ()* @func to i8*) @bar = alias i8* (), i8* ()* @func +@bar2 = alias i8* (), i8* ()* @bar define i8* @func() { + call i8* @bar2(); ret i8* @foo; } @@ -19,6 +21,13 @@ ; CHECK-NEXT: ElementIndex: 0x0 ; CHECK-NEXT: } ; CHECK-NEXT: Symbol { +; CHECK-NEXT: Name: bar2 +; CHECK-NEXT: Type: FUNCTION (0x0) +; CHECK-NEXT: Flags [ (0x0) +; CHECK-NEXT: ] +; CHECK-NEXT: ElementIndex: 0x0 +; CHECK-NEXT: } +; CHECK-NEXT: Symbol { ; CHECK-NEXT: Name: foo ; CHECK-NEXT: Type: FUNCTION (0x0) ; CHECK-NEXT: Flags [ (0x0)