llvm.org GIT mirror llvm / 14598cb
[WebAssembly] Add version to object file metadata Summary: See https://github.com/WebAssembly/tool-conventions/issues/54 Subscribers: jfb, dschuff, jgravelle-google, aheejin, sunfish, llvm-commits Differential Revision: https://reviews.llvm.org/D46069 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330969 91177308-0d34-0410-b5e6-96231b3b80d8 Sam Clegg 1 year, 3 months ago
31 changed file(s) with 57 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
2323 const char WasmMagic[] = {'\0', 'a', 's', 'm'};
2424 // Wasm binary format version
2525 const uint32_t WasmVersion = 0x1;
26 // Wasm linking metadata version
27 const uint32_t WasmMetadataVersion = 0x1;
2628 // Wasm uses a 64k page size
2729 const uint32_t WasmPageSize = 65536;
2830
160162 };
161163
162164 struct WasmLinkingData {
165 uint32_t Version;
163166 std::vector InitFunctions;
164167 std::vector Comdats;
165168 std::vector SymbolTable;
194194 return C && C->Name == "linking";
195195 }
196196
197 uint32_t Version;
197198 std::vector SymbolTable;
198199 std::vector SegmentInfos;
199200 std::vector InitFunctions;
856856 const std::map> &Comdats) {
857857 SectionBookkeeping Section;
858858 startCustomSection(Section, "linking");
859 encodeULEB128(wasm::WasmMetadataVersion, getStream());
860
859861 SectionBookkeeping SubSection;
860
861862 if (SymbolInfos.size() != 0) {
862863 startSection(SubSection, wasm::WASM_SYMBOL_TABLE);
863864 encodeULEB128(SymbolInfos.size(), getStream());
312312 "Linking data must come after code section", object_error::parse_failed);
313313 }
314314
315 LinkingData.Version = readVaruint32(Ptr);
316 if (LinkingData.Version != wasm::WasmMetadataVersion) {
317 return make_error(
318 "Unexpected metadata version: " + Twine(LinkingData.Version) +
319 " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")",
320 object_error::parse_failed);
321 }
322
315323 while (Ptr < End) {
316324 uint8_t Type = readUint8(Ptr);
317325 uint32_t Size = readVaruint32(Ptr);
5656 static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) {
5757 commonSectionMapping(IO, Section);
5858 IO.mapRequired("Name", Section.Name);
59 IO.mapRequired("Version", Section.Version);
5960 IO.mapOptional("SymbolTable", Section.SymbolTable);
6061 IO.mapOptional("SegmentInfo", Section.SegmentInfos);
6162 IO.mapOptional("InitFunctions", Section.InitFunctions);
1111 ; CHECK: - Type: DATA
1212 ; CHECK: Content: '0102'
1313
14 ; CHECK: - Type: CUSTOM
15 ; CHECK-NEXT: Name: linking
16 ; CHECK-NEXT: SymbolTable:
14 ; CHECK: SymbolTable:
1715 ; CHECK-NEXT: - Index: 0
1816 ; CHECK-NEXT: Kind: DATA
1917 ; CHECK-NEXT: Name: gBd
3434 ; CHECK-NEXT: Opcode: I32_CONST
3535 ; CHECK-NEXT: Value: 8
3636 ; CHECK-NEXT: Content: ''
37 ; CHECK-NEXT: - Type: CUSTOM
38 ; CHECK-NEXT: Name: linking
39 ; CHECK-NEXT: SymbolTable:
37
38 ; CHECK: SymbolTable:
4039 ; CHECK-NEXT: - Index: 0
4140 ; CHECK-NEXT: Kind: DATA
4241 ; CHECK-NEXT: Name: g0
7474 ; CHECK-NEXT: Content: '616263'
7575 ; CHECK-NEXT: - Type: CUSTOM
7676 ; CHECK-NEXT: Name: linking
77 ; CHECK-NEXT: Version: 1
7778 ; CHECK-NEXT: SymbolTable:
7879 ; CHECK-NEXT: - Index: 0
7980 ; CHECK-NEXT: Kind: FUNCTION
2929 ; CHECK-NEXT: Opcode: I32_CONST
3030 ; CHECK-NEXT: Value: 24
3131 ; CHECK-NEXT: Content: '08000000'
32 ; CHECK-NEXT: - Type: CUSTOM
33 ; CHECK-NEXT: Name: linking
34 ; CHECK-NEXT: SymbolTable:
32
33 ; CHECK: SymbolTable:
3534 ; CHECK-NEXT: - Index: 0
3635 ; CHECK-NEXT: Kind: DATA
3736 ; CHECK-NEXT: Name: global0
109109 ; CHECK-NEXT: Content: '01040000'
110110 ; CHECK-NEXT: - Type: CUSTOM
111111 ; CHECK-NEXT: Name: linking
112 ; CHECK-NEXT: Version: 1
112113 ; CHECK-NEXT: SymbolTable:
113114 ; CHECK-NEXT: - Index: 0
114115 ; CHECK-NEXT: Kind: FUNCTION
4343 ; CHECK-NEXT: Content: '06000000'
4444 ; CHECK-NEXT: - Type: CUSTOM
4545 ; CHECK-NEXT: Name: linking
46 ; CHECK-NEXT: Version: 1
4647 ; CHECK-NEXT: SymbolTable:
4748 ; CHECK-NEXT: - Index: 0
4849 ; CHECK-NEXT: Kind: DATA
1313 ret void
1414 }
1515
16 ; CHECK: - Type: CUSTOM
17 ; CHECK-NEXT: Name: linking
18 ; CHECK-NEXT: SymbolTable:
16 ; CHECK: SymbolTable:
1917 ; CHECK-NEXT: - Index: 0
2018 ; CHECK-NEXT: Kind: FUNCTION
2119 ; CHECK-NEXT: Name: defaultVis
137137 ; CHECK-NEXT: Content: '01000000'
138138 ; CHECK-NEXT: - Type: CUSTOM
139139 ; CHECK-NEXT: Name: linking
140 ; CHECK-NEXT: Version: 1
140141 ; CHECK-NEXT: SymbolTable:
141142 ; CHECK-NEXT: - Index: 0
142143 ; CHECK-NEXT: Kind: FUNCTION
1919 ; CHECK-NEXT: Field: __indirect_function_table
2020
2121
22 ; CHECK: - Type: CUSTOM
23 ; CHECK-NEXT: Name: linking
24 ; CHECK-NEXT: SymbolTable:
22 ; CHECK: SymbolTable:
2523 ; CHECK-NEXT: - Index: 0
2624 ; CHECK-NEXT: Kind: FUNCTION
2725 ; CHECK-NEXT: Name: weak_function
650650 WASM-NEXT: Version: 0x00000001
651651 WASM: - Type: CUSTOM
652652 WASM-NEXT: Name: linking
653 WASM-NEXT: Version: 1
653654 WASM-NEXT: SymbolTable:
654655 WASM-NEXT: - Index: 0
655656 WASM-NEXT: Kind: FUNCTION
0 # RUN: yaml2obj %s | not llvm-objdump -h - 2>&1 | FileCheck %s
1
2 !WASM
3 FileHeader:
4 Version: 0x00000001
5 Sections:
6 - Type: CUSTOM
7 Name: linking
8 Version: 0
9
10 # CHECK: {{.*}}: Unexpected metadata version: 0 (Expected: 1)
3838 Body: 108180808000210020000F0B
3939 - Type: CUSTOM
4040 Name: linking
41 Version: 1
4142 SymbolTable:
4243 - Index: 0
4344 Kind: FUNCTION
2323 Addend: -6
2424 - Type: CUSTOM
2525 Name: linking
26 Version: 1
2627 SymbolTable:
2728 - Index: 0
2829 Kind: DATA
1212 GlobalMutable: false
1313 - Type: CUSTOM
1414 Name: linking
15 Version: 1
1516 SymbolTable:
1617 - Index: 0
1718 Kind: GLOBAL
2828 Content: '11110000'
2929 - Type: CUSTOM
3030 Name: linking
31 Version: 1
3132 SymbolTable:
3233 - Index: 0
3334 Kind: FUNCTION
4748 - Priority: 1
4849 Symbol: 0
4950 ...
50 # CHECK: - Type: CUSTOM
51 # CHECK-NEXT: Name: linking
52 # CHECK-NEXT: SymbolTable:
51 # CHECK: SymbolTable:
5352 # CHECK-NEXT: - Index: 0
5453 # CHECK-NEXT: Kind: FUNCTION
5554 # CHECK-NEXT: Name: bar
3535 Body: 00
3636 - Type: CUSTOM
3737 Name: linking
38 Version: 1
3839 SymbolTable:
3940 - Index: 0
4041 Kind: FUNCTION
3636 Content: '616263'
3737 - Type: CUSTOM
3838 Name: linking
39 Version: 1
3940 SymbolTable:
4041 - Index: 0
4142 Kind: FUNCTION
2424 GlobalMutable: false
2525 - Type: CUSTOM
2626 Name: linking
27 Version: 1
2728 SymbolTable:
2829 - Index: 0
2930 Kind: FUNCTION
4242 Content: '616263'
4343 - Type: CUSTOM
4444 Name: linking
45 Version: 1
4546 SymbolTable:
4647 - Index: 0
4748 Kind: DATA
66 # CHECK-NEXT: 2 FUNCTION 00000002 0000000000000000
77 # CHECK-NEXT: 3 CODE 00000019 0000000000000000 TEXT
88 # CHECK-NEXT: 4 DATA 0000001c 0000000000000000 DATA
9 # CHECK-NEXT: 5 linking 0000004d 0000000000000000
9 # CHECK-NEXT: 5 linking 00000051 0000000000000000
1010 # CHECK-NEXT: 6 reloc.CODE 0000000c 0000000000000000
1111
1212 # RUN: llvm-objdump -p %p/Inputs/trivial.obj.wasm | FileCheck %s -check-prefix CHECK-HEADER
527527 WASM-NEXT: }
528528 WASM-NEXT: Section {
529529 WASM-NEXT: Type: CUSTOM (0x0)
530 WASM-NEXT: Size: 57
530 WASM-NEXT: Size: 61
531531 WASM-NEXT: Offset: 191
532532 WASM-NEXT: Name: linking
533533 WASM-NEXT: }
534534 WASM-NEXT: Section {
535535 WASM-NEXT: Type: CUSTOM (0x0)
536536 WASM-NEXT: Size: 12
537 WASM-NEXT: Offset: 262
537 WASM-NEXT: Offset: 266
538538 WASM-NEXT: Name: reloc.CODE
539539 WASM-NEXT: }
540540 WASM-NEXT: ]
6161 CustomSec = std::move(NameSec);
6262 } else if (WasmSec.Name == "linking") {
6363 std::unique_ptr LinkingSec = make_unique();
64 LinkingSec->Version = Obj.linkingData().Version;
65
6466 ArrayRef Comdats = Obj.linkingData().Comdats;
6567 for (StringRef ComdatName : Comdats)
6668 LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
7072 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
7173 }
7274 }
75
7376 uint32_t SegmentIndex = 0;
7477 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
7578 if (!Segment.Data.Name.empty()) {
8689 }
8790 SegmentIndex++;
8891 }
92
8993 uint32_t SymbolIndex = 0;
9094 for (const wasm::WasmSymbolInfo &Symbol : Obj.linkingData().SymbolTable) {
9195 WasmYAML::SymbolInfo Info;
104108 }
105109 LinkingSec->SymbolTable.emplace_back(Info);
106110 }
111
107112 for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
108113 WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
109114 LinkingSec->InitFunctions.emplace_back(F);
110115 }
116
111117 CustomSec = std::move(LinkingSec);
112118 } else {
113119 CustomSec = make_unique(WasmSec.Name);
133133
134134 int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section) {
135135 writeStringRef(Section.Name, OS);
136 encodeULEB128(Section.Version, OS);
136137
137138 SubSectionWriter SubSection(OS);
138139