Index: llvm/trunk/include/llvm/BinaryFormat/Wasm.h =================================================================== --- llvm/trunk/include/llvm/BinaryFormat/Wasm.h +++ llvm/trunk/include/llvm/BinaryFormat/Wasm.h @@ -24,6 +24,8 @@ const char WasmMagic[] = {'\0', 'a', 's', 'm'}; // Wasm binary format version const uint32_t WasmVersion = 0x1; +// Wasm linking metadata version +const uint32_t WasmMetadataVersion = 0x1; // Wasm uses a 64k page size const uint32_t WasmPageSize = 65536; @@ -161,6 +163,7 @@ }; struct WasmLinkingData { + uint32_t Version; std::vector InitFunctions; std::vector Comdats; std::vector SymbolTable; Index: llvm/trunk/include/llvm/ObjectYAML/WasmYAML.h =================================================================== --- llvm/trunk/include/llvm/ObjectYAML/WasmYAML.h +++ llvm/trunk/include/llvm/ObjectYAML/WasmYAML.h @@ -195,6 +195,7 @@ return C && C->Name == "linking"; } + uint32_t Version; std::vector SymbolTable; std::vector SegmentInfos; std::vector InitFunctions; Index: llvm/trunk/lib/MC/WasmObjectWriter.cpp =================================================================== --- llvm/trunk/lib/MC/WasmObjectWriter.cpp +++ llvm/trunk/lib/MC/WasmObjectWriter.cpp @@ -857,8 +857,9 @@ const std::map> &Comdats) { SectionBookkeeping Section; startCustomSection(Section, "linking"); - SectionBookkeeping SubSection; + encodeULEB128(wasm::WasmMetadataVersion, getStream()); + SectionBookkeeping SubSection; if (SymbolInfos.size() != 0) { startSection(SubSection, wasm::WASM_SYMBOL_TABLE); encodeULEB128(SymbolInfos.size(), getStream()); Index: llvm/trunk/lib/Object/WasmObjectFile.cpp =================================================================== --- llvm/trunk/lib/Object/WasmObjectFile.cpp +++ llvm/trunk/lib/Object/WasmObjectFile.cpp @@ -313,6 +313,14 @@ "Linking data must come after code section", object_error::parse_failed); } + LinkingData.Version = readVaruint32(Ptr); + if (LinkingData.Version != wasm::WasmMetadataVersion) { + return make_error( + "Unexpected metadata version: " + Twine(LinkingData.Version) + + " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")", + object_error::parse_failed); + } + while (Ptr < End) { uint8_t Type = readUint8(Ptr); uint32_t Size = readVaruint32(Ptr); Index: llvm/trunk/lib/ObjectYAML/WasmYAML.cpp =================================================================== --- llvm/trunk/lib/ObjectYAML/WasmYAML.cpp +++ llvm/trunk/lib/ObjectYAML/WasmYAML.cpp @@ -57,6 +57,7 @@ static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) { commonSectionMapping(IO, Section); IO.mapRequired("Name", Section.Name); + IO.mapRequired("Version", Section.Version); IO.mapOptional("SymbolTable", Section.SymbolTable); IO.mapOptional("SegmentInfo", Section.SegmentInfos); IO.mapOptional("InitFunctions", Section.InitFunctions); Index: llvm/trunk/test/MC/WebAssembly/array-fill.ll =================================================================== --- llvm/trunk/test/MC/WebAssembly/array-fill.ll +++ llvm/trunk/test/MC/WebAssembly/array-fill.ll @@ -12,9 +12,7 @@ ; CHECK: - Type: DATA ; CHECK: Content: '0102' -; CHECK: - Type: CUSTOM -; CHECK-NEXT: Name: linking -; CHECK-NEXT: SymbolTable: +; CHECK: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: DATA ; CHECK-NEXT: Name: gBd Index: llvm/trunk/test/MC/WebAssembly/bss.ll =================================================================== --- llvm/trunk/test/MC/WebAssembly/bss.ll +++ llvm/trunk/test/MC/WebAssembly/bss.ll @@ -35,9 +35,8 @@ ; CHECK-NEXT: Opcode: I32_CONST ; CHECK-NEXT: Value: 8 ; CHECK-NEXT: Content: '' -; CHECK-NEXT: - Type: CUSTOM -; CHECK-NEXT: Name: linking -; CHECK-NEXT: SymbolTable: + +; CHECK: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: DATA ; CHECK-NEXT: Name: g0 Index: llvm/trunk/test/MC/WebAssembly/comdat.ll =================================================================== --- llvm/trunk/test/MC/WebAssembly/comdat.ll +++ llvm/trunk/test/MC/WebAssembly/comdat.ll @@ -75,6 +75,7 @@ ; CHECK-NEXT: Content: '616263' ; CHECK-NEXT: - Type: CUSTOM ; CHECK-NEXT: Name: linking +; CHECK-NEXT: Version: 1 ; CHECK-NEXT: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: FUNCTION Index: llvm/trunk/test/MC/WebAssembly/explicit-sections.ll =================================================================== --- llvm/trunk/test/MC/WebAssembly/explicit-sections.ll +++ llvm/trunk/test/MC/WebAssembly/explicit-sections.ll @@ -30,9 +30,8 @@ ; CHECK-NEXT: Opcode: I32_CONST ; CHECK-NEXT: Value: 24 ; CHECK-NEXT: Content: '08000000' -; CHECK-NEXT: - Type: CUSTOM -; CHECK-NEXT: Name: linking -; CHECK-NEXT: SymbolTable: + +; CHECK: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: DATA ; CHECK-NEXT: Name: global0 Index: llvm/trunk/test/MC/WebAssembly/global-ctor-dtor.ll =================================================================== --- llvm/trunk/test/MC/WebAssembly/global-ctor-dtor.ll +++ llvm/trunk/test/MC/WebAssembly/global-ctor-dtor.ll @@ -110,6 +110,7 @@ ; CHECK-NEXT: Content: '01040000' ; CHECK-NEXT: - Type: CUSTOM ; CHECK-NEXT: Name: linking +; CHECK-NEXT: Version: 1 ; CHECK-NEXT: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: FUNCTION Index: llvm/trunk/test/MC/WebAssembly/unnamed-data.ll =================================================================== --- llvm/trunk/test/MC/WebAssembly/unnamed-data.ll +++ llvm/trunk/test/MC/WebAssembly/unnamed-data.ll @@ -44,6 +44,7 @@ ; CHECK-NEXT: Content: '06000000' ; CHECK-NEXT: - Type: CUSTOM ; CHECK-NEXT: Name: linking +; CHECK-NEXT: Version: 1 ; CHECK-NEXT: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: DATA Index: llvm/trunk/test/MC/WebAssembly/visibility.ll =================================================================== --- llvm/trunk/test/MC/WebAssembly/visibility.ll +++ llvm/trunk/test/MC/WebAssembly/visibility.ll @@ -14,9 +14,7 @@ ret void } -; CHECK: - Type: CUSTOM -; CHECK-NEXT: Name: linking -; CHECK-NEXT: SymbolTable: +; CHECK: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: FUNCTION ; CHECK-NEXT: Name: defaultVis Index: llvm/trunk/test/MC/WebAssembly/weak-alias.ll =================================================================== --- llvm/trunk/test/MC/WebAssembly/weak-alias.ll +++ llvm/trunk/test/MC/WebAssembly/weak-alias.ll @@ -138,6 +138,7 @@ ; CHECK-NEXT: Content: '01000000' ; CHECK-NEXT: - Type: CUSTOM ; CHECK-NEXT: Name: linking +; CHECK-NEXT: Version: 1 ; CHECK-NEXT: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: FUNCTION Index: llvm/trunk/test/MC/WebAssembly/weak.ll =================================================================== --- llvm/trunk/test/MC/WebAssembly/weak.ll +++ llvm/trunk/test/MC/WebAssembly/weak.ll @@ -20,9 +20,7 @@ ; CHECK-NEXT: Field: __indirect_function_table -; CHECK: - Type: CUSTOM -; CHECK-NEXT: Name: linking -; CHECK-NEXT: SymbolTable: +; CHECK: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: FUNCTION ; CHECK-NEXT: Name: weak_function Index: llvm/trunk/test/Object/obj2yaml.test =================================================================== --- llvm/trunk/test/Object/obj2yaml.test +++ llvm/trunk/test/Object/obj2yaml.test @@ -651,6 +651,7 @@ WASM-NEXT: Version: 0x00000001 WASM: - Type: CUSTOM WASM-NEXT: Name: linking +WASM-NEXT: Version: 1 WASM-NEXT: SymbolTable: WASM-NEXT: - Index: 0 WASM-NEXT: Kind: FUNCTION Index: llvm/trunk/test/Object/wasm-bad-metadata-version.yaml =================================================================== --- llvm/trunk/test/Object/wasm-bad-metadata-version.yaml +++ llvm/trunk/test/Object/wasm-bad-metadata-version.yaml @@ -0,0 +1,11 @@ +# RUN: yaml2obj %s | not llvm-objdump -h - 2>&1 | FileCheck %s + +!WASM +FileHeader: + Version: 0x00000001 +Sections: + - Type: CUSTOM + Name: linking + Version: 0 + +# CHECK: {{.*}}: Unexpected metadata version: 0 (Expected: 1) Index: llvm/trunk/test/ObjectYAML/wasm/code_section.yaml =================================================================== --- llvm/trunk/test/ObjectYAML/wasm/code_section.yaml +++ llvm/trunk/test/ObjectYAML/wasm/code_section.yaml @@ -39,6 +39,7 @@ Body: 108180808000210020000F0B - Type: CUSTOM Name: linking + Version: 1 SymbolTable: - Index: 0 Kind: FUNCTION Index: llvm/trunk/test/ObjectYAML/wasm/data_section.yaml =================================================================== --- llvm/trunk/test/ObjectYAML/wasm/data_section.yaml +++ llvm/trunk/test/ObjectYAML/wasm/data_section.yaml @@ -24,6 +24,7 @@ Addend: -6 - Type: CUSTOM Name: linking + Version: 1 SymbolTable: - Index: 0 Kind: DATA Index: llvm/trunk/test/ObjectYAML/wasm/invalid_global_weak.yaml =================================================================== --- llvm/trunk/test/ObjectYAML/wasm/invalid_global_weak.yaml +++ llvm/trunk/test/ObjectYAML/wasm/invalid_global_weak.yaml @@ -13,6 +13,7 @@ GlobalMutable: false - Type: CUSTOM Name: linking + Version: 1 SymbolTable: - Index: 0 Kind: GLOBAL Index: llvm/trunk/test/ObjectYAML/wasm/linking_section.yaml =================================================================== --- llvm/trunk/test/ObjectYAML/wasm/linking_section.yaml +++ llvm/trunk/test/ObjectYAML/wasm/linking_section.yaml @@ -29,6 +29,7 @@ Content: '11110000' - Type: CUSTOM Name: linking + Version: 1 SymbolTable: - Index: 0 Kind: FUNCTION @@ -48,9 +49,7 @@ - Priority: 1 Symbol: 0 ... -# CHECK: - Type: CUSTOM -# CHECK-NEXT: Name: linking -# CHECK-NEXT: SymbolTable: +# CHECK: SymbolTable: # CHECK-NEXT: - Index: 0 # CHECK-NEXT: Kind: FUNCTION # CHECK-NEXT: Name: bar Index: llvm/trunk/test/ObjectYAML/wasm/weak_symbols.yaml =================================================================== --- llvm/trunk/test/ObjectYAML/wasm/weak_symbols.yaml +++ llvm/trunk/test/ObjectYAML/wasm/weak_symbols.yaml @@ -36,6 +36,7 @@ Body: 00 - Type: CUSTOM Name: linking + Version: 1 SymbolTable: - Index: 0 Kind: FUNCTION Index: llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml =================================================================== --- llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml +++ llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml @@ -37,6 +37,7 @@ Content: '616263' - Type: CUSTOM Name: linking + Version: 1 SymbolTable: - Index: 0 Kind: FUNCTION Index: llvm/trunk/test/tools/llvm-nm/wasm/imports.yaml =================================================================== --- llvm/trunk/test/tools/llvm-nm/wasm/imports.yaml +++ llvm/trunk/test/tools/llvm-nm/wasm/imports.yaml @@ -25,6 +25,7 @@ GlobalMutable: false - Type: CUSTOM Name: linking + Version: 1 SymbolTable: - Index: 0 Kind: FUNCTION Index: llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml =================================================================== --- llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml +++ llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml @@ -43,6 +43,7 @@ Content: '616263' - Type: CUSTOM Name: linking + Version: 1 SymbolTable: - Index: 0 Kind: DATA Index: llvm/trunk/test/tools/llvm-objdump/wasm.txt =================================================================== --- llvm/trunk/test/tools/llvm-objdump/wasm.txt +++ llvm/trunk/test/tools/llvm-objdump/wasm.txt @@ -7,7 +7,7 @@ # CHECK-NEXT: 2 FUNCTION 00000002 0000000000000000 # CHECK-NEXT: 3 CODE 00000019 0000000000000000 TEXT # CHECK-NEXT: 4 DATA 0000001c 0000000000000000 DATA -# CHECK-NEXT: 5 linking 0000004d 0000000000000000 +# CHECK-NEXT: 5 linking 00000051 0000000000000000 # CHECK-NEXT: 6 reloc.CODE 0000000c 0000000000000000 # RUN: llvm-objdump -p %p/Inputs/trivial.obj.wasm | FileCheck %s -check-prefix CHECK-HEADER Index: llvm/trunk/test/tools/llvm-readobj/sections.test =================================================================== --- llvm/trunk/test/tools/llvm-readobj/sections.test +++ llvm/trunk/test/tools/llvm-readobj/sections.test @@ -528,14 +528,14 @@ WASM-NEXT: } WASM-NEXT: Section { WASM-NEXT: Type: CUSTOM (0x0) -WASM-NEXT: Size: 57 +WASM-NEXT: Size: 61 WASM-NEXT: Offset: 191 WASM-NEXT: Name: linking WASM-NEXT: } WASM-NEXT: Section { WASM-NEXT: Type: CUSTOM (0x0) WASM-NEXT: Size: 12 -WASM-NEXT: Offset: 262 +WASM-NEXT: Offset: 266 WASM-NEXT: Name: reloc.CODE WASM-NEXT: } WASM-NEXT: ] Index: llvm/trunk/tools/obj2yaml/wasm2yaml.cpp =================================================================== --- llvm/trunk/tools/obj2yaml/wasm2yaml.cpp +++ llvm/trunk/tools/obj2yaml/wasm2yaml.cpp @@ -62,6 +62,8 @@ CustomSec = std::move(NameSec); } else if (WasmSec.Name == "linking") { std::unique_ptr LinkingSec = make_unique(); + LinkingSec->Version = Obj.linkingData().Version; + ArrayRef Comdats = Obj.linkingData().Comdats; for (StringRef ComdatName : Comdats) LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}}); @@ -71,6 +73,7 @@ WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index}); } } + uint32_t SegmentIndex = 0; for (const object::WasmSegment &Segment : Obj.dataSegments()) { if (!Segment.Data.Name.empty()) { @@ -87,6 +90,7 @@ } SegmentIndex++; } + uint32_t SymbolIndex = 0; for (const wasm::WasmSymbolInfo &Symbol : Obj.linkingData().SymbolTable) { WasmYAML::SymbolInfo Info; @@ -105,10 +109,12 @@ } LinkingSec->SymbolTable.emplace_back(Info); } + for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) { WasmYAML::InitFunction F{Func.Priority, Func.Symbol}; LinkingSec->InitFunctions.emplace_back(F); } + CustomSec = std::move(LinkingSec); } else { CustomSec = make_unique(WasmSec.Name); Index: llvm/trunk/tools/yaml2obj/yaml2wasm.cpp =================================================================== --- llvm/trunk/tools/yaml2obj/yaml2wasm.cpp +++ llvm/trunk/tools/yaml2obj/yaml2wasm.cpp @@ -134,6 +134,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section) { writeStringRef(Section.Name, OS); + encodeULEB128(Section.Version, OS); SubSectionWriter SubSection(OS);