Index: include/llvm/MC/MCSectionWasm.h =================================================================== --- include/llvm/MC/MCSectionWasm.h +++ include/llvm/MC/MCSectionWasm.h @@ -26,7 +26,15 @@ /// This represents a section on wasm. class MCSectionWasm final : public MCSection { +public: + enum SectionType { + Code, + Data, + Debug, + }; + private: + /// This is the name of the section. The referenced memory is owned by /// TargetLoweringObjectFileWasm's WasmUniqueMap. StringRef SectionName; @@ -52,7 +60,6 @@ const MCSymbolWasm *group, unsigned UniqueID, MCSymbol *Begin) : MCSection(SV_Wasm, K, Begin), SectionName(Section), Type(type), UniqueID(UniqueID), Group(group), SectionOffset(0) { - assert(type == wasm::WASM_SEC_CODE || type == wasm::WASM_SEC_DATA); } void setSectionName(StringRef Name) { SectionName = Name; } Index: lib/CodeGen/TargetLoweringObjectFileImpl.cpp =================================================================== --- lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1249,7 +1249,7 @@ MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { StringRef Name = GO->getSection(); - return getContext().getWasmSection(Name, wasm::WASM_SEC_DATA); + return getContext().getWasmSection(Name, MCSectionWasm::Data); } static MCSectionWasm *selectWasmSectionForGlobal( @@ -1262,12 +1262,12 @@ bool UniqueSectionNames = TM.getUniqueSectionNames(); SmallString<128> Name = getSectionPrefixForGlobal(Kind); - uint32_t Type = wasm::WASM_SEC_DATA; + uint32_t Type = MCSectionWasm::Data; if (const auto *F = dyn_cast(GO)) { const auto &OptionalPrefix = F->getSectionPrefix(); if (OptionalPrefix) Name += *OptionalPrefix; - Type = wasm::WASM_SEC_CODE; + Type = MCSectionWasm::Code; } if (EmitUniqueSection && UniqueSectionNames) { @@ -1330,7 +1330,7 @@ void TargetLoweringObjectFileWasm::InitializeWasm() { StaticCtorSection = - getContext().getWasmSection(".init_array", wasm::WASM_SEC_DATA); + getContext().getWasmSection(".init_array", MCSectionWasm::Data); StaticDtorSection = - getContext().getWasmSection(".fini_array", wasm::WASM_SEC_DATA); + getContext().getWasmSection(".fini_array", MCSectionWasm::Data); } Index: lib/MC/MCObjectFileInfo.cpp =================================================================== --- lib/MC/MCObjectFileInfo.cpp +++ lib/MC/MCObjectFileInfo.cpp @@ -824,24 +824,24 @@ void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) { // TODO: Set the section types and flags. - TextSection = Ctx->getWasmSection(".text", wasm::WASM_SEC_CODE); - DataSection = Ctx->getWasmSection(".data", wasm::WASM_SEC_DATA); + TextSection = Ctx->getWasmSection(".text", MCSectionWasm::Code); + DataSection = Ctx->getWasmSection(".data", MCSectionWasm::Data); // TODO: Set the section types and flags. - DwarfLineSection = Ctx->getWasmSection(".debug_line", wasm::WASM_SEC_DATA); - DwarfStrSection = Ctx->getWasmSection(".debug_str", wasm::WASM_SEC_DATA); - DwarfLocSection = Ctx->getWasmSection(".debug_loc", wasm::WASM_SEC_DATA); - DwarfAbbrevSection = Ctx->getWasmSection(".debug_abbrev", wasm::WASM_SEC_DATA, "section_abbrev"); - DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", wasm::WASM_SEC_DATA); - DwarfRangesSection = Ctx->getWasmSection(".debug_ranges", wasm::WASM_SEC_DATA, "debug_range"); - DwarfMacinfoSection = Ctx->getWasmSection(".debug_macinfo", wasm::WASM_SEC_DATA, "debug_macinfo"); - DwarfAddrSection = Ctx->getWasmSection(".debug_addr", wasm::WASM_SEC_DATA); - DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", wasm::WASM_SEC_DATA); - DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", wasm::WASM_SEC_DATA); - DwarfInfoSection = Ctx->getWasmSection(".debug_info", wasm::WASM_SEC_DATA, "section_info"); - DwarfFrameSection = Ctx->getWasmSection(".debug_frame", wasm::WASM_SEC_DATA); - DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", wasm::WASM_SEC_DATA); - DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", wasm::WASM_SEC_DATA); + DwarfLineSection = Ctx->getWasmSection(".debug_line", MCSectionWasm::Debug); + DwarfStrSection = Ctx->getWasmSection(".debug_str", MCSectionWasm::Debug); + DwarfLocSection = Ctx->getWasmSection(".debug_loc", MCSectionWasm::Debug); + DwarfAbbrevSection = Ctx->getWasmSection(".debug_abbrev", MCSectionWasm::Debug, "section_abbrev"); + DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", MCSectionWasm::Debug); + DwarfRangesSection = Ctx->getWasmSection(".debug_ranges", MCSectionWasm::Debug, "debug_range"); + DwarfMacinfoSection = Ctx->getWasmSection(".debug_macinfo", MCSectionWasm::Debug, "debug_macinfo"); + DwarfAddrSection = Ctx->getWasmSection(".debug_addr", MCSectionWasm::Debug); + DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", MCSectionWasm::Debug); + DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", MCSectionWasm::Debug); + DwarfInfoSection = Ctx->getWasmSection(".debug_info", MCSectionWasm::Debug, "section_info"); + DwarfFrameSection = Ctx->getWasmSection(".debug_frame", MCSectionWasm::Debug); + DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", MCSectionWasm::Debug); + DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", MCSectionWasm::Debug); // TODO: Define more sections. } Index: lib/MC/MCWasmStreamer.cpp =================================================================== --- lib/MC/MCWasmStreamer.cpp +++ lib/MC/MCWasmStreamer.cpp @@ -156,7 +156,7 @@ void MCWasmStreamer::EmitIdent(StringRef IdentString) { MCSection *Comment = getAssembler().getContext().getWasmSection( - ".comment", wasm::WASM_SEC_DATA); + ".comment", MCSectionWasm::Data); PushSection(); SwitchSection(Comment); if (!SeenIdent) { Index: lib/MC/WasmObjectWriter.cpp =================================================================== --- lib/MC/WasmObjectWriter.cpp +++ lib/MC/WasmObjectWriter.cpp @@ -437,10 +437,13 @@ WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection); DEBUG(dbgs() << "WasmReloc: " << Rec << "\n"); - if (FixupSection.hasInstructions()) - CodeRelocations.push_back(Rec); - else + if (FixupSection.getType() == MCSectionWasm::Data) DataRelocations.push_back(Rec); + else if (FixupSection.getType() == MCSectionWasm::Code) + CodeRelocations.push_back(Rec); + else if (FixupSection.getType() != MCSectionWasm::Debug) + // TODO(sbc): Add support for debug sections. + llvm_unreachable("unexpected section type"); } // Write X as an (unsigned) LEB value at offset Offset in Stream, padded @@ -1060,7 +1063,7 @@ // In the special .global_variables section, we've encoded global // variables used by the function. Translate them into the Globals // list. - MCSectionWasm *GlobalVars = Ctx.getWasmSection(".global_variables", wasm::WASM_SEC_DATA); + MCSectionWasm *GlobalVars = Ctx.getWasmSection(".global_variables", MCSectionWasm::Data); if (!GlobalVars->getFragmentList().empty()) { if (GlobalVars->getFragmentList().size() != 1) report_fatal_error("only one .global_variables fragment supported"); @@ -1116,7 +1119,7 @@ // In the special .stack_pointer section, we've encoded the stack pointer // index. - MCSectionWasm *StackPtr = Ctx.getWasmSection(".stack_pointer", wasm::WASM_SEC_DATA); + MCSectionWasm *StackPtr = Ctx.getWasmSection(".stack_pointer", MCSectionWasm::Data); if (!StackPtr->getFragmentList().empty()) { if (StackPtr->getFragmentList().size() != 1) report_fatal_error("only one .stack_pointer fragment supported"); @@ -1135,7 +1138,7 @@ for (MCSection &Sec : Asm) { auto &Section = static_cast(Sec); - if (Section.getType() != wasm::WASM_SEC_DATA) + if (Section.getType() != MCSectionWasm::Data) continue; DataSize = alignTo(DataSize, Section.getAlignment()); Index: test/MC/WebAssembly/debug-info.ll =================================================================== --- /dev/null +++ test/MC/WebAssembly/debug-info.ll @@ -0,0 +1,43 @@ +; RUN: llc -mtriple wasm32-unknown-unknown-wasm -filetype=obj %s -o - | llvm-readobj -r -s -expand-relocs | FileCheck %s + +; Debug information is currently not supported. This test simply verifies that +; a valid object generated. +source_filename = "test.c" + +@myextern = external global i32, align 4 +@foo = hidden global i32* @myextern, align 4, !dbg !0 +@ptr2 = hidden global void ()* @f2, align 4, !dbg !6 + +; Function Attrs: noinline nounwind optnone +define hidden void @f2() #0 !dbg !17 { +entry: + ret void, !dbg !18 +} + +attributes #0 = { noinline nounwind optnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" } + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!13, !14, !15} +!llvm.ident = !{!16} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "foo", scope: !2, file: !3, line: 4, type: !11, isLocal: false, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 6.0.0 (trunk 315924) (llvm/trunk 315960)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5) +!3 = !DIFile(filename: "test.c", directory: "/usr/local/google/home/sbc/dev/wasm/simple") +!4 = !{} +!5 = !{!0, !6} +!6 = !DIGlobalVariableExpression(var: !7, expr: !DIExpression()) +!7 = distinct !DIGlobalVariable(name: "ptr2", scope: !2, file: !3, line: 5, type: !8, isLocal: false, isDefinition: true) +!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 32) +!9 = !DISubroutineType(types: !10) +!10 = !{null} +!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 32) +!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!13 = !{i32 2, !"Dwarf Version", i32 4} +!14 = !{i32 2, !"Debug Info Version", i32 3} +!15 = !{i32 1, !"wchar_size", i32 4} +!16 = !{!"clang version 6.0.0 (trunk 315924) (llvm/trunk 315960)"} +!17 = distinct !DISubprogram(name: "f2", scope: !3, file: !3, line: 2, type: !9, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: false, unit: !2, variables: !4) +!18 = !DILocation(line: 2, column: 16, scope: !17) + +; CHECK: Format: WASM