Index: llvm/trunk/include/llvm/MC/MCContext.h =================================================================== --- llvm/trunk/include/llvm/MC/MCContext.h +++ llvm/trunk/include/llvm/MC/MCContext.h @@ -441,25 +441,25 @@ getAssociativeCOFFSection(MCSectionCOFF *Sec, const MCSymbol *KeySym, unsigned UniqueID = GenericSectionID); - MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type) { - return getWasmSection(Section, Type, nullptr); + MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K) { + return getWasmSection(Section, K, nullptr); } - MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type, + MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K, const char *BeginSymName) { - return getWasmSection(Section, Type, "", ~0, BeginSymName); + return getWasmSection(Section, K, "", ~0, BeginSymName); } - MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type, + MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K, const Twine &Group, unsigned UniqueID) { - return getWasmSection(Section, Type, Group, UniqueID, nullptr); + return getWasmSection(Section, K, Group, UniqueID, nullptr); } - MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type, + MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K, const Twine &Group, unsigned UniqueID, const char *BeginSymName); - MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type, + MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K, const MCSymbolWasm *Group, unsigned UniqueID, const char *BeginSymName); Index: llvm/trunk/include/llvm/MC/MCSectionWasm.h =================================================================== --- llvm/trunk/include/llvm/MC/MCSectionWasm.h +++ llvm/trunk/include/llvm/MC/MCSectionWasm.h @@ -27,13 +27,11 @@ /// This represents a section on wasm. class MCSectionWasm final : public MCSection { private: + /// This is the name of the section. The referenced memory is owned by /// TargetLoweringObjectFileWasm's WasmUniqueMap. StringRef SectionName; - /// This is the type of the section, from the enums in BinaryFormat/Wasm.h - unsigned Type; - unsigned UniqueID; const MCSymbolWasm *Group; @@ -48,12 +46,10 @@ uint64_t MemoryOffset; friend class MCContext; - MCSectionWasm(StringRef Section, unsigned type, SectionKind K, - 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); - } + MCSectionWasm(StringRef Section, SectionKind K, const MCSymbolWasm *group, + unsigned UniqueID, MCSymbol *Begin) + : MCSection(SV_Wasm, K, Begin), SectionName(Section), UniqueID(UniqueID), + Group(group), SectionOffset(0) {} void setSectionName(StringRef Name) { SectionName = Name; } @@ -65,7 +61,6 @@ bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const; StringRef getSectionName() const { return SectionName; } - unsigned getType() const { return Type; } const MCSymbolWasm *getGroup() const { return Group; } void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, @@ -74,6 +69,10 @@ bool UseCodeAlign() const override; bool isVirtualSection() const override; + bool isWasmData() const { + return Kind.isGlobalWriteableData() || Kind.isReadOnly(); + } + bool isUnique() const { return UniqueID != ~0U; } unsigned getUniqueID() const { return UniqueID; } Index: llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp =================================================================== --- llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -168,8 +168,7 @@ MMI, Streamer); } -static SectionKind -getELFKindForNamedSection(StringRef Name, SectionKind K) { +static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) { // N.B.: The defaults used in here are no the same ones used in MC. // We follow gcc, MC follows gas. For example, given ".section .eh_frame", // both gas and MC will produce a section with no flags. Given @@ -1249,7 +1248,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, SectionKind::getData()); } static MCSectionWasm *selectWasmSectionForGlobal( @@ -1262,12 +1261,10 @@ bool UniqueSectionNames = TM.getUniqueSectionNames(); SmallString<128> Name = getSectionPrefixForGlobal(Kind); - uint32_t Type = wasm::WASM_SEC_DATA; if (const auto *F = dyn_cast(GO)) { const auto &OptionalPrefix = F->getSectionPrefix(); if (OptionalPrefix) Name += *OptionalPrefix; - Type = wasm::WASM_SEC_CODE; } if (EmitUniqueSection && UniqueSectionNames) { @@ -1279,7 +1276,7 @@ UniqueID = *NextUniqueID; (*NextUniqueID)++; } - return Ctx.getWasmSection(Name, Type, Group, UniqueID); + return Ctx.getWasmSection(Name, Kind, Group, UniqueID); } MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal( @@ -1330,7 +1327,7 @@ void TargetLoweringObjectFileWasm::InitializeWasm() { StaticCtorSection = - getContext().getWasmSection(".init_array", wasm::WASM_SEC_DATA); + getContext().getWasmSection(".init_array", SectionKind::getData()); StaticDtorSection = - getContext().getWasmSection(".fini_array", wasm::WASM_SEC_DATA); + getContext().getWasmSection(".fini_array", SectionKind::getData()); } Index: llvm/trunk/lib/MC/MCContext.cpp =================================================================== --- llvm/trunk/lib/MC/MCContext.cpp +++ llvm/trunk/lib/MC/MCContext.cpp @@ -486,17 +486,17 @@ "", 0, UniqueID); } -MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type, +MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K, const Twine &Group, unsigned UniqueID, const char *BeginSymName) { MCSymbolWasm *GroupSym = nullptr; if (!Group.isTriviallyEmpty() && !Group.str().empty()) GroupSym = cast(getOrCreateSymbol(Group)); - return getWasmSection(Section, Type, GroupSym, UniqueID, BeginSymName); + return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName); } -MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type, +MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind, const MCSymbolWasm *GroupSym, unsigned UniqueID, const char *BeginSymName) { @@ -512,14 +512,12 @@ StringRef CachedName = Entry.first.SectionName; - SectionKind Kind = SectionKind::getText(); - MCSymbol *Begin = nullptr; if (BeginSymName) Begin = createTempSymbol(BeginSymName, false); MCSectionWasm *Result = new (WasmAllocator.Allocate()) - MCSectionWasm(CachedName, Type, Kind, GroupSym, UniqueID, Begin); + MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin); Entry.second = Result; return Result; } Index: llvm/trunk/lib/MC/MCObjectFileInfo.cpp =================================================================== --- llvm/trunk/lib/MC/MCObjectFileInfo.cpp +++ llvm/trunk/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", SectionKind::getText()); + DataSection = Ctx->getWasmSection(".data", SectionKind::getData()); // 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", SectionKind::getMetadata()); + DwarfStrSection = Ctx->getWasmSection(".debug_str", SectionKind::getMetadata()); + DwarfLocSection = Ctx->getWasmSection(".debug_loc", SectionKind::getMetadata()); + DwarfAbbrevSection = Ctx->getWasmSection(".debug_abbrev", SectionKind::getMetadata(), "section_abbrev"); + DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", SectionKind::getMetadata()); + DwarfRangesSection = Ctx->getWasmSection(".debug_ranges", SectionKind::getMetadata(), "debug_range"); + DwarfMacinfoSection = Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata(), "debug_macinfo"); + DwarfAddrSection = Ctx->getWasmSection(".debug_addr", SectionKind::getMetadata()); + DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata()); + DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata()); + DwarfInfoSection = Ctx->getWasmSection(".debug_info", SectionKind::getMetadata(), "section_info"); + DwarfFrameSection = Ctx->getWasmSection(".debug_frame", SectionKind::getMetadata()); + DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", SectionKind::getMetadata()); + DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", SectionKind::getMetadata()); // TODO: Define more sections. } Index: llvm/trunk/lib/MC/MCWasmStreamer.cpp =================================================================== --- llvm/trunk/lib/MC/MCWasmStreamer.cpp +++ llvm/trunk/lib/MC/MCWasmStreamer.cpp @@ -157,7 +157,7 @@ void MCWasmStreamer::EmitIdent(StringRef IdentString) { MCSection *Comment = getAssembler().getContext().getWasmSection( - ".comment", wasm::WASM_SEC_DATA); + ".comment", SectionKind::getMetadata()); PushSection(); SwitchSection(Comment); if (!SeenIdent) { Index: llvm/trunk/lib/MC/WasmObjectWriter.cpp =================================================================== --- llvm/trunk/lib/MC/WasmObjectWriter.cpp +++ llvm/trunk/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.isWasmData()) DataRelocations.push_back(Rec); + else if (FixupSection.getKind().isText()) + CodeRelocations.push_back(Rec); + else if (!FixupSection.getKind().isMetadata()) + // 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,8 @@ // 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", SectionKind::getMetadata()); if (!GlobalVars->getFragmentList().empty()) { if (GlobalVars->getFragmentList().size() != 1) report_fatal_error("only one .global_variables fragment supported"); @@ -1116,7 +1120,8 @@ // 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", SectionKind::getMetadata()); if (!StackPtr->getFragmentList().empty()) { if (StackPtr->getFragmentList().size() != 1) report_fatal_error("only one .stack_pointer fragment supported"); @@ -1135,7 +1140,7 @@ for (MCSection &Sec : Asm) { auto &Section = static_cast(Sec); - if (Section.getType() != wasm::WASM_SEC_DATA) + if (!Section.isWasmData()) continue; DataSize = alignTo(DataSize, Section.getAlignment()); Index: llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp =================================================================== --- llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp +++ llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp @@ -219,8 +219,8 @@ // section. This will later be decoded and turned into contents for the // Globals Section. Streamer.PushSection(); - Streamer.SwitchSection(Streamer.getContext() - .getWasmSection(".global_variables", 0, 0)); + Streamer.SwitchSection(Streamer.getContext().getWasmSection( + ".global_variables", SectionKind::getMetadata())); for (const wasm::Global &G : Globals) { Streamer.EmitIntValue(int32_t(G.Type), 1); Streamer.EmitIntValue(G.Mutable, 1); @@ -240,8 +240,8 @@ void WebAssemblyTargetWasmStreamer::emitStackPointer(uint32_t Index) { Streamer.PushSection(); - Streamer.SwitchSection(Streamer.getContext() - .getWasmSection(".stack_pointer", 0, 0)); + Streamer.SwitchSection(Streamer.getContext().getWasmSection( + ".stack_pointer", SectionKind::getMetadata())); Streamer.EmitIntValue(Index, 4); Streamer.PopSection(); } Index: llvm/trunk/test/MC/WebAssembly/debug-info.ll =================================================================== --- llvm/trunk/test/MC/WebAssembly/debug-info.ll +++ llvm/trunk/test/MC/WebAssembly/debug-info.ll @@ -0,0 +1,41 @@ +; RUN: llc -mtriple wasm32-unknown-unknown-wasm -filetype=obj %s -o - | llvm-readobj -r -s -expand-relocs + +; 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)