Index: lib/MC/MCContext.cpp =================================================================== --- lib/MC/MCContext.cpp +++ lib/MC/MCContext.cpp @@ -515,15 +515,18 @@ StringRef CachedName = Entry.first.SectionName; - MCSymbol *Begin = nullptr; - if (BeginSymName) { - Begin = createSymbol(BeginSymName, false, false); - cast(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION); - } + MCSymbol *Begin = createSymbol(CachedName, false, false); + cast(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION); MCSectionWasm *Result = new (WasmAllocator.Allocate()) MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin); Entry.second = Result; + + auto *F = new MCDataFragment(); + Result->getFragmentList().insert(Result->begin(), F); + F->setParent(Result); + Begin->setFragment(F); + return Result; } Index: lib/MC/MCObjectFileInfo.cpp =================================================================== --- lib/MC/MCObjectFileInfo.cpp +++ lib/MC/MCObjectFileInfo.cpp @@ -861,27 +861,22 @@ } void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) { - // TODO: Set the section types and flags. TextSection = Ctx->getWasmSection(".text", SectionKind::getText()); DataSection = Ctx->getWasmSection(".data", SectionKind::getData()); - // TODO: Set the section types and flags. - DwarfLineSection = Ctx->getWasmSection(".debug_line", SectionKind::getMetadata(), ".debug_line"); + DwarfLineSection = Ctx->getWasmSection(".debug_line", SectionKind::getMetadata()); DwarfLineStrSection = Ctx->getWasmSection(".debug_line_str", SectionKind::getMetadata()); - DwarfStrSection = Ctx->getWasmSection(".debug_str", SectionKind::getMetadata(), ".debug_str"); - DwarfLocSection = Ctx->getWasmSection( - ".debug_loc", SectionKind::getMetadata(), ".debug_loc"); - DwarfAbbrevSection = Ctx->getWasmSection( - ".debug_abbrev", SectionKind::getMetadata(), ".debug_abbrev"); + DwarfStrSection = Ctx->getWasmSection(".debug_str", SectionKind::getMetadata()); + DwarfLocSection = Ctx->getWasmSection(".debug_loc", SectionKind::getMetadata()); + DwarfAbbrevSection = Ctx->getWasmSection(".debug_abbrev", SectionKind::getMetadata()); DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", SectionKind::getMetadata()); - DwarfRangesSection = Ctx->getWasmSection( - ".debug_ranges", SectionKind::getMetadata(), ".debug_ranges"); - DwarfMacinfoSection = Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata(), ".debug_macinfo"); + DwarfRangesSection = Ctx->getWasmSection(".debug_ranges", SectionKind::getMetadata()); + DwarfMacinfoSection = Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata()); 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(), ".debug_info"); + DwarfInfoSection = Ctx->getWasmSection(".debug_info", SectionKind::getMetadata()); DwarfFrameSection = Ctx->getWasmSection(".debug_frame", SectionKind::getMetadata()); DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", SectionKind::getMetadata()); DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", SectionKind::getMetadata()); Index: lib/MC/MCWasmStreamer.cpp =================================================================== --- lib/MC/MCWasmStreamer.cpp +++ lib/MC/MCWasmStreamer.cpp @@ -66,6 +66,7 @@ Asm.registerSymbol(*Grp); this->MCObjectStreamer::ChangeSection(Section, Subsection); + Asm.registerSymbol(*Section->getBeginSymbol()); } void MCWasmStreamer::EmitWeakReference(MCSymbol *Alias, Index: lib/MC/WasmObjectWriter.cpp =================================================================== --- lib/MC/WasmObjectWriter.cpp +++ lib/MC/WasmObjectWriter.cpp @@ -1204,9 +1204,9 @@ MCSymbol* Begin = Sec.getBeginSymbol(); if (Begin) { WasmIndices[cast(Begin)] = CustomSections.size(); - if (Name != Begin->getName()) + if (SectionName != Begin->getName()) report_fatal_error("section name and begin symbol should match: " + - Twine(Name)); + Twine(SectionName)); } CustomSections.emplace_back(Name, &Section); } @@ -1341,6 +1341,7 @@ const auto &WS = static_cast(S); if (!isInSymtab(WS)) { WS.setIndex(INVALID_INDEX); + DEBUG(dbgs() << "skipping symtab: " << WS << "\n"); continue; } DEBUG(dbgs() << "adding to symtab: " << WS << "\n"); @@ -1405,16 +1406,27 @@ continue; if (WS.getFragmentList().empty()) continue; - if (WS.getFragmentList().size() != 2) + + // init_array is expected to contain a single non-empty data fragment + if (WS.getFragmentList().size() != 3) report_fatal_error("only one .init_array section fragment supported"); - const MCFragment &AlignFrag = *WS.begin(); + + auto IT = WS.begin(); + const MCFragment &EmptyFrag = *IT; + if (EmptyFrag.getKind() != MCFragment::FT_Data) + report_fatal_error(".init_array section should be aligned"); + + IT = std::next(IT); + const MCFragment &AlignFrag = *IT; if (AlignFrag.getKind() != MCFragment::FT_Align) report_fatal_error(".init_array section should be aligned"); if (cast(AlignFrag).getAlignment() != (is64Bit() ? 8 : 4)) report_fatal_error(".init_array section should be aligned for pointers"); - const MCFragment &Frag = *std::next(WS.begin()); + + const MCFragment &Frag = *std::next(IT); if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data) report_fatal_error("only data supported in .init_array section"); + uint16_t Priority = UINT16_MAX; unsigned PrefixLength = strlen(".init_array"); if (WS.getSectionName().size() > PrefixLength) {