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 @@ -218,11 +218,11 @@ // Relocations for fixing up references in the code section. std::vector CodeRelocations; - uint32_t CodeSectionIndex; + Optional CodeSectionIndex; // Relocations for fixing up references in the data section. std::vector DataRelocations; - uint32_t DataSectionIndex; + Optional DataSectionIndex; // Index values to use for fixing up call_indirect type indices. // Maps function symbols to the index of the type of the function @@ -329,9 +329,9 @@ void writeExportSection(ArrayRef Exports); void writeElemSection(ArrayRef TableElems); void writeDataCountSection(); - void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout, - ArrayRef Functions); - void writeDataSection(); + int writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout, + ArrayRef Functions); + int writeDataSection(); void writeEventSection(ArrayRef Events); void writeGlobalSection(ArrayRef Globals); void writeRelocSection(uint32_t SectionIndex, StringRef Name, @@ -876,11 +876,11 @@ endSection(Section); } -void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm, - const MCAsmLayout &Layout, - ArrayRef Functions) { +int WasmObjectWriter::writeCodeSection(const MCAssembler &Asm, + const MCAsmLayout &Layout, + ArrayRef Functions) { if (Functions.empty()) - return; + return -1; SectionBookkeeping Section; startSection(Section, wasm::WASM_SEC_CODE); @@ -904,11 +904,12 @@ applyRelocations(CodeRelocations, Section.ContentsOffset); endSection(Section); + return 0; } -void WasmObjectWriter::writeDataSection() { +int WasmObjectWriter::writeDataSection() { if (DataSegments.empty()) - return; + return -1; SectionBookkeeping Section; startSection(Section, wasm::WASM_SEC_DATA); @@ -934,6 +935,7 @@ applyRelocations(DataRelocations, Section.ContentsOffset); endSection(Section); + return 0; } void WasmObjectWriter::writeRelocSection( @@ -1661,13 +1663,15 @@ writeExportSection(Exports); writeElemSection(TableElems); writeDataCountSection(); - writeCodeSection(Asm, Layout, Functions); - writeDataSection(); + int code_ret = writeCodeSection(Asm, Layout, Functions); + int data_ret = writeDataSection(); for (auto &CustomSection : CustomSections) writeCustomSection(CustomSection, Asm, Layout); writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats); - writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations); - writeRelocSection(DataSectionIndex, "DATA", DataRelocations); + if (!code_ret) + writeRelocSection(*CodeSectionIndex, "CODE", CodeRelocations); + if (!data_ret) + writeRelocSection(*DataSectionIndex, "DATA", DataRelocations); writeCustomRelocSections(); if (ProducersSection) writeCustomSection(*ProducersSection, Asm, Layout);