diff --git a/llvm/include/llvm/MC/MCWasmObjectWriter.h b/llvm/include/llvm/MC/MCWasmObjectWriter.h --- a/llvm/include/llvm/MC/MCWasmObjectWriter.h +++ b/llvm/include/llvm/MC/MCWasmObjectWriter.h @@ -15,6 +15,7 @@ namespace llvm { class MCFixup; +class MCSectionWasm; class MCValue; class raw_pwrite_stream; @@ -34,6 +35,7 @@ } virtual unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup, + const MCSectionWasm &FixupSection, bool IsLocRel) const = 0; /// \name Accessors 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 @@ -497,20 +497,31 @@ // LLVM expects wrapping, in contrast to wasm's immediates which can't // be negative and don't wrap. FixedValue = 0; - - unsigned Type = TargetObjectWriter->getRelocType(Target, Fixup, IsLocRel); + errs() << "FixupSection " << FixupSection.getName() << "\n"; + errs() << " sym " << *SymA << " "; + errs() << (SymA->isData() ? "data\n" + : SymA->isSection() ? "section\n" + : SymA->isGlobal() ? "global\n" + : SymA->isFunction() ? "function\n" + : SymA->isTable() ? "table\n" + : "???\n"); + //<< SymA->getType().getValue() << "\n"; + unsigned Type = + TargetObjectWriter->getRelocType(Target, Fixup, FixupSection, IsLocRel); // Absolute offset within a section or a function. // Currently only supported for for metadata sections. // See: test/MC/WebAssembly/blockaddress.ll - if (Type == wasm::R_WASM_FUNCTION_OFFSET_I32 || - Type == wasm::R_WASM_FUNCTION_OFFSET_I64 || - Type == wasm::R_WASM_SECTION_OFFSET_I32) { + if ((Type == wasm::R_WASM_FUNCTION_OFFSET_I32 || + Type == wasm::R_WASM_FUNCTION_OFFSET_I64 || + Type == wasm::R_WASM_SECTION_OFFSET_I32) && + !SymA->isFunction()) { if (!FixupSection.getKind().isMetadata()) report_fatal_error("relocations for function or section offsets are " "only supported in metadata sections"); const MCSymbol *SectionSymbol = nullptr; + // Oh noes, fixup against an undefined symbol, with no section! const MCSection &SecA = SymA->getSection(); if (SecA.getKind().isText()) { auto SecSymIt = SectionFunctions.find(&SecA); @@ -620,6 +631,8 @@ case wasm::R_WASM_FUNCTION_OFFSET_I32: case wasm::R_WASM_FUNCTION_OFFSET_I64: case wasm::R_WASM_SECTION_OFFSET_I32: { + if (!RelEntry.Symbol->isInSection()) + return 0; const auto &Section = static_cast(RelEntry.Symbol->getSection()); return Section.getSectionOffset() + RelEntry.Addend; diff --git a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp --- a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp +++ b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp @@ -35,6 +35,7 @@ private: unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup, + const MCSectionWasm &FixupSection, bool IsLocRel) const override; }; } // end anonymous namespace @@ -43,7 +44,7 @@ bool IsEmscripten) : MCWasmObjectTargetWriter(Is64Bit, IsEmscripten) {} -static const MCSection *getFixupSection(const MCExpr *Expr) { +static const MCSection *getTargetSection(const MCExpr *Expr) { if (auto SyExp = dyn_cast(Expr)) { if (SyExp->getSymbol().isInSection()) return &SyExp->getSymbol().getSection(); @@ -51,20 +52,20 @@ } if (auto BinOp = dyn_cast(Expr)) { - auto SectionLHS = getFixupSection(BinOp->getLHS()); - auto SectionRHS = getFixupSection(BinOp->getRHS()); + auto SectionLHS = getTargetSection(BinOp->getLHS()); + auto SectionRHS = getTargetSection(BinOp->getRHS()); return SectionLHS == SectionRHS ? nullptr : SectionLHS; } if (auto UnOp = dyn_cast(Expr)) - return getFixupSection(UnOp->getSubExpr()); + return getTargetSection(UnOp->getSubExpr()); return nullptr; } -unsigned WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target, - const MCFixup &Fixup, - bool IsLocRel) const { +unsigned WebAssemblyWasmObjectWriter::getRelocType( + const MCValue &Target, const MCFixup &Fixup, + const MCSectionWasm &FixupSection, bool IsLocRel) const { const MCSymbolRefExpr *RefA = Target.getSymA(); assert(RefA); auto& SymA = cast(RefA->getSymbol()); @@ -89,7 +90,10 @@ default: break; } - + auto *fs = getTargetSection(Fixup.getValue()); + errs() << "inner TargetSection " << fs << "\n"; + if (fs) + errs() << " name " << fs->getName() << "\n"; switch (unsigned(Fixup.getKind())) { case WebAssembly::fixup_sleb128_i32: if (SymA.isFunction()) @@ -113,12 +117,16 @@ assert(SymA.isData()); return wasm::R_WASM_MEMORY_ADDR_LEB64; case FK_Data_4: - if (SymA.isFunction()) + if (SymA.isFunction()) { + if (FixupSection.getKind().isMetadata()) + return wasm::R_WASM_FUNCTION_OFFSET_I32; + assert(FixupSection.isWasmData()); return wasm::R_WASM_TABLE_INDEX_I32; + } if (SymA.isGlobal()) return wasm::R_WASM_GLOBAL_INDEX_I32; if (auto Section = static_cast( - getFixupSection(Fixup.getValue()))) { + getTargetSection(Fixup.getValue()))) { if (Section->getKind().isText()) return wasm::R_WASM_FUNCTION_OFFSET_I32; else if (!Section->isWasmData()) @@ -127,12 +135,15 @@ return IsLocRel ? wasm::R_WASM_MEMORY_ADDR_LOCREL_I32 : wasm::R_WASM_MEMORY_ADDR_I32; case FK_Data_8: - if (SymA.isFunction()) + if (SymA.isFunction()) { + if (FixupSection.getKind().isMetadata()) + return wasm::R_WASM_FUNCTION_OFFSET_I64; return wasm::R_WASM_TABLE_INDEX_I64; + } if (SymA.isGlobal()) llvm_unreachable("unimplemented R_WASM_GLOBAL_INDEX_I64"); if (auto Section = static_cast( - getFixupSection(Fixup.getValue()))) { + getTargetSection(Fixup.getValue()))) { if (Section->getKind().isText()) return wasm::R_WASM_FUNCTION_OFFSET_I64; else if (!Section->isWasmData()) diff --git a/llvm/test/MC/WebAssembly/debug-template-param.ll b/llvm/test/MC/WebAssembly/debug-template-param.ll new file mode 100644 --- /dev/null +++ b/llvm/test/MC/WebAssembly/debug-template-param.ll @@ -0,0 +1,67 @@ +; RUN: llc -filetype=obj %s -o - | llvm-readobj -r - | FileCheck %s + +; Test for PR50408. Compiled from: +; char a(); +; template +; void f() { b(); } +; void g() { f(); } + +; CHECK: Section (10) .debug_addr +; CHECK-NEXT: 0x8 R_WASM_FUNCTION_OFFSET_I32 _Z1gv 0 +; CHECK-NEXT: 0xC R_WASM_FUNCTION_OFFSET_I32 _Z1fIcXadL_Z1avEEEvv 0 +; ensure that the reloc type is correct for _Z1av which is undefined +; CHECK-NEXT: 0x10 R_WASM_FUNCTION_OFFSET_I32 _Z1av 0 +; CHECK-NEXT: } + +; ModuleID = 'PR50408.cc' +source_filename = "PR50408.cc" +target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128-ni:1" +target triple = "wasm32-unknown-emscripten" + +$_Z1fIcXadL_Z1avEEEvv = comdat any + +; Function Attrs: noinline optnone mustprogress +define hidden void @_Z1gv() #0 !dbg !7 { +entry: + call void @_Z1fIcXadL_Z1avEEEvv(), !dbg !10 + ret void, !dbg !11 +} + +; Function Attrs: noinline optnone mustprogress +define linkonce_odr hidden void @_Z1fIcXadL_Z1avEEEvv() #0 comdat !dbg !12 { +entry: + %call = call signext i8 @_Z1av(), !dbg !20 + ret void, !dbg !21 +} + +declare signext i8 @_Z1av() #1 + +attributes #0 = { noinline optnone mustprogress "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" } +attributes #1 = { "frame-pointer"="none" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 13.0.0 (https://github.com/llvm/llvm-project.git 5027637fa1d409e3ca78dab60dc2e2db6c62c175)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "PR50408.cc", directory: "/s/emr/emscripten-releases/localtests", checksumkind: CSK_MD5, checksum: "285a5682ae46dbbe90ccfb84cdef66c7") +!2 = !{} +!3 = !{i32 7, !"Dwarf Version", i32 5} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 13.0.0 (https://github.com/llvm/llvm-project.git 5027637fa1d409e3ca78dab60dc2e2db6c62c175)"} +!7 = distinct !DISubprogram(name: "g", linkageName: "_Z1gv", scope: !1, file: !1, line: 5, type: !8, scopeLine: 5, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!8 = !DISubroutineType(types: !9) +!9 = !{null} +!10 = !DILocation(line: 5, column: 12, scope: !7) +!11 = !DILocation(line: 5, column: 26, scope: !7) +!12 = distinct !DISubprogram(name: "f", linkageName: "_Z1fIcXadL_Z1avEEEvv", scope: !1, file: !1, line: 4, type: !8, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, templateParams: !13, retainedNodes: !2) +!13 = !{!14, !16} +!14 = !DITemplateTypeParameter(type: !15) +!15 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char) +!16 = !DITemplateValueParameter(name: "b", type: !17, value: i8 ()* @_Z1av) +!17 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !18, size: 32) +!18 = !DISubroutineType(types: !19) +!19 = !{!15} +!20 = !DILocation(line: 4, column: 12, scope: !12) +!21 = !DILocation(line: 4, column: 17, scope: !12)