Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 1 | //===- yaml2wasm - Convert YAML to a Wasm object file --------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// The Wasm component of yaml2obj. |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 14 | |
Heejin Ahn | feef720 | 2018-12-15 00:58:12 +0000 | [diff] [blame] | 15 | #include "llvm/Object/Wasm.h" |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 16 | #include "llvm/ObjectYAML/ObjectYAML.h" |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 17 | #include "llvm/ObjectYAML/yaml2obj.h" |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Endian.h" |
| 19 | #include "llvm/Support/LEB128.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 23 | namespace { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 24 | /// This parses a yaml stream that represents a Wasm object file. |
| 25 | /// See docs/yaml2obj for the yaml scheema. |
| 26 | class WasmWriter { |
| 27 | public: |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 28 | WasmWriter(WasmYAML::Object &Obj, yaml::ErrorHandler EH) |
| 29 | : Obj(Obj), ErrHandler(EH) {} |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 30 | bool writeWasm(raw_ostream &OS); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 31 | |
| 32 | private: |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 33 | void writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec, |
| 34 | uint32_t SectionIndex); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 35 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 36 | void writeInitExpr(raw_ostream &OS, const wasm::WasmInitExpr &InitExpr); |
| 37 | |
| 38 | void writeSectionContent(raw_ostream &OS, WasmYAML::CustomSection &Section); |
| 39 | void writeSectionContent(raw_ostream &OS, WasmYAML::TypeSection &Section); |
| 40 | void writeSectionContent(raw_ostream &OS, WasmYAML::ImportSection &Section); |
| 41 | void writeSectionContent(raw_ostream &OS, WasmYAML::FunctionSection &Section); |
| 42 | void writeSectionContent(raw_ostream &OS, WasmYAML::TableSection &Section); |
| 43 | void writeSectionContent(raw_ostream &OS, WasmYAML::MemorySection &Section); |
| 44 | void writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section); |
| 45 | void writeSectionContent(raw_ostream &OS, WasmYAML::EventSection &Section); |
| 46 | void writeSectionContent(raw_ostream &OS, WasmYAML::ExportSection &Section); |
| 47 | void writeSectionContent(raw_ostream &OS, WasmYAML::StartSection &Section); |
| 48 | void writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section); |
| 49 | void writeSectionContent(raw_ostream &OS, WasmYAML::CodeSection &Section); |
| 50 | void writeSectionContent(raw_ostream &OS, WasmYAML::DataSection &Section); |
| 51 | void writeSectionContent(raw_ostream &OS, WasmYAML::DataCountSection &Section); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 52 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 53 | // Custom section types |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 54 | void writeSectionContent(raw_ostream &OS, WasmYAML::DylinkSection &Section); |
| 55 | void writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section); |
| 56 | void writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section); |
| 57 | void writeSectionContent(raw_ostream &OS, WasmYAML::ProducersSection &Section); |
| 58 | void writeSectionContent(raw_ostream &OS, |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 59 | WasmYAML::TargetFeaturesSection &Section); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 60 | WasmYAML::Object &Obj; |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 61 | uint32_t NumImportedFunctions = 0; |
| 62 | uint32_t NumImportedGlobals = 0; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 63 | uint32_t NumImportedEvents = 0; |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 64 | |
| 65 | bool HasError = false; |
| 66 | yaml::ErrorHandler ErrHandler; |
| 67 | void reportError(const Twine &Msg); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 70 | class SubSectionWriter { |
| 71 | raw_ostream &OS; |
| 72 | std::string OutString; |
| 73 | raw_string_ostream StringStream; |
| 74 | |
| 75 | public: |
| 76 | SubSectionWriter(raw_ostream &OS) : OS(OS), StringStream(OutString) {} |
| 77 | |
| 78 | void done() { |
| 79 | StringStream.flush(); |
| 80 | encodeULEB128(OutString.size(), OS); |
| 81 | OS << OutString; |
| 82 | OutString.clear(); |
| 83 | } |
| 84 | |
| 85 | raw_ostream &getStream() { return StringStream; } |
| 86 | }; |
| 87 | |
| 88 | } // end anonymous namespace |
| 89 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 90 | static int writeUint64(raw_ostream &OS, uint64_t Value) { |
| 91 | char Data[sizeof(Value)]; |
| 92 | support::endian::write64le(Data, Value); |
| 93 | OS.write(Data, sizeof(Data)); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int writeUint32(raw_ostream &OS, uint32_t Value) { |
| 98 | char Data[sizeof(Value)]; |
| 99 | support::endian::write32le(Data, Value); |
| 100 | OS.write(Data, sizeof(Data)); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int writeUint8(raw_ostream &OS, uint8_t Value) { |
| 105 | char Data[sizeof(Value)]; |
| 106 | memcpy(Data, &Value, sizeof(Data)); |
| 107 | OS.write(Data, sizeof(Data)); |
| 108 | return 0; |
| 109 | } |
| 110 | |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 111 | static int writeStringRef(const StringRef &Str, raw_ostream &OS) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 112 | encodeULEB128(Str.size(), OS); |
| 113 | OS << Str; |
| 114 | return 0; |
| 115 | } |
| 116 | |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 117 | static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 118 | writeUint8(OS, Lim.Flags); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 119 | encodeULEB128(Lim.Initial, OS); |
| 120 | if (Lim.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) |
| 121 | encodeULEB128(Lim.Maximum, OS); |
| 122 | return 0; |
| 123 | } |
| 124 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 125 | void WasmWriter::reportError(const Twine &Msg) { |
| 126 | ErrHandler(Msg); |
| 127 | HasError = true; |
| 128 | } |
| 129 | |
| 130 | void WasmWriter::writeInitExpr(raw_ostream &OS, |
| 131 | const wasm::WasmInitExpr &InitExpr) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 132 | writeUint8(OS, InitExpr.Opcode); |
| 133 | switch (InitExpr.Opcode) { |
| 134 | case wasm::WASM_OPCODE_I32_CONST: |
| 135 | encodeSLEB128(InitExpr.Value.Int32, OS); |
| 136 | break; |
| 137 | case wasm::WASM_OPCODE_I64_CONST: |
| 138 | encodeSLEB128(InitExpr.Value.Int64, OS); |
| 139 | break; |
| 140 | case wasm::WASM_OPCODE_F32_CONST: |
| 141 | writeUint32(OS, InitExpr.Value.Float32); |
| 142 | break; |
| 143 | case wasm::WASM_OPCODE_F64_CONST: |
| 144 | writeUint64(OS, InitExpr.Value.Float64); |
| 145 | break; |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 146 | case wasm::WASM_OPCODE_GLOBAL_GET: |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 147 | encodeULEB128(InitExpr.Value.Global, OS); |
| 148 | break; |
| 149 | default: |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 150 | reportError("unknown opcode in init_expr: " + Twine(InitExpr.Opcode)); |
| 151 | return; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 152 | } |
| 153 | writeUint8(OS, wasm::WASM_OPCODE_END); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 154 | } |
| 155 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 156 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 157 | WasmYAML::DylinkSection &Section) { |
Sam Clegg | e4afbc6 | 2018-11-14 18:36:24 +0000 | [diff] [blame] | 158 | writeStringRef(Section.Name, OS); |
| 159 | encodeULEB128(Section.MemorySize, OS); |
| 160 | encodeULEB128(Section.MemoryAlignment, OS); |
| 161 | encodeULEB128(Section.TableSize, OS); |
| 162 | encodeULEB128(Section.TableAlignment, OS); |
Sam Clegg | 0380125 | 2018-12-12 23:40:58 +0000 | [diff] [blame] | 163 | encodeULEB128(Section.Needed.size(), OS); |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 164 | for (StringRef Needed : Section.Needed) |
Sam Clegg | 0380125 | 2018-12-12 23:40:58 +0000 | [diff] [blame] | 165 | writeStringRef(Needed, OS); |
Sam Clegg | e4afbc6 | 2018-11-14 18:36:24 +0000 | [diff] [blame] | 166 | } |
| 167 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 168 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 169 | WasmYAML::LinkingSection &Section) { |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 170 | writeStringRef(Section.Name, OS); |
Sam Clegg | 6bb5a41 | 2018-04-26 18:15:32 +0000 | [diff] [blame] | 171 | encodeULEB128(Section.Version, OS); |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 172 | |
| 173 | SubSectionWriter SubSection(OS); |
| 174 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 175 | // SYMBOL_TABLE subsection |
| 176 | if (Section.SymbolTable.size()) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 177 | writeUint8(OS, wasm::WASM_SYMBOL_TABLE); |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 178 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 179 | encodeULEB128(Section.SymbolTable.size(), SubSection.getStream()); |
Benjamin Kramer | 8d71fdc | 2018-02-23 12:20:18 +0000 | [diff] [blame] | 180 | #ifndef NDEBUG |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 181 | uint32_t SymbolIndex = 0; |
| 182 | #endif |
| 183 | for (const WasmYAML::SymbolInfo &Info : Section.SymbolTable) { |
| 184 | assert(Info.Index == SymbolIndex++); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 185 | writeUint8(SubSection.getStream(), Info.Kind); |
| 186 | encodeULEB128(Info.Flags, SubSection.getStream()); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 187 | switch (Info.Kind) { |
| 188 | case wasm::WASM_SYMBOL_TYPE_FUNCTION: |
| 189 | case wasm::WASM_SYMBOL_TYPE_GLOBAL: |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 190 | case wasm::WASM_SYMBOL_TYPE_EVENT: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 191 | encodeULEB128(Info.ElementIndex, SubSection.getStream()); |
Dan Gohman | 29874ce | 2019-02-07 22:03:32 +0000 | [diff] [blame] | 192 | if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 || |
| 193 | (Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 194 | writeStringRef(Info.Name, SubSection.getStream()); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 195 | break; |
| 196 | case wasm::WASM_SYMBOL_TYPE_DATA: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 197 | writeStringRef(Info.Name, SubSection.getStream()); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 198 | if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 199 | encodeULEB128(Info.DataRef.Segment, SubSection.getStream()); |
| 200 | encodeULEB128(Info.DataRef.Offset, SubSection.getStream()); |
| 201 | encodeULEB128(Info.DataRef.Size, SubSection.getStream()); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 202 | } |
| 203 | break; |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 204 | case wasm::WASM_SYMBOL_TYPE_SECTION: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 205 | encodeULEB128(Info.ElementIndex, SubSection.getStream()); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 206 | break; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 207 | default: |
| 208 | llvm_unreachable("unexpected kind"); |
| 209 | } |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 212 | SubSection.done(); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 213 | } |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 214 | |
| 215 | // SEGMENT_NAMES subsection |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 216 | if (Section.SegmentInfos.size()) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 217 | writeUint8(OS, wasm::WASM_SEGMENT_INFO); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 218 | encodeULEB128(Section.SegmentInfos.size(), SubSection.getStream()); |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 219 | for (const WasmYAML::SegmentInfo &SegmentInfo : Section.SegmentInfos) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 220 | writeStringRef(SegmentInfo.Name, SubSection.getStream()); |
| 221 | encodeULEB128(SegmentInfo.Alignment, SubSection.getStream()); |
| 222 | encodeULEB128(SegmentInfo.Flags, SubSection.getStream()); |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 223 | } |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 224 | SubSection.done(); |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 225 | } |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 226 | |
| 227 | // INIT_FUNCS subsection |
| 228 | if (Section.InitFunctions.size()) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 229 | writeUint8(OS, wasm::WASM_INIT_FUNCS); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 230 | encodeULEB128(Section.InitFunctions.size(), SubSection.getStream()); |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 231 | for (const WasmYAML::InitFunction &Func : Section.InitFunctions) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 232 | encodeULEB128(Func.Priority, SubSection.getStream()); |
| 233 | encodeULEB128(Func.Symbol, SubSection.getStream()); |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 234 | } |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 235 | SubSection.done(); |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 236 | } |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 237 | |
| 238 | // COMDAT_INFO subsection |
| 239 | if (Section.Comdats.size()) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 240 | writeUint8(OS, wasm::WASM_COMDAT_INFO); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 241 | encodeULEB128(Section.Comdats.size(), SubSection.getStream()); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 242 | for (const auto &C : Section.Comdats) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 243 | writeStringRef(C.Name, SubSection.getStream()); |
| 244 | encodeULEB128(0, SubSection.getStream()); // flags for future use |
| 245 | encodeULEB128(C.Entries.size(), SubSection.getStream()); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 246 | for (const WasmYAML::ComdatEntry &Entry : C.Entries) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 247 | writeUint8(SubSection.getStream(), Entry.Kind); |
| 248 | encodeULEB128(Entry.Index, SubSection.getStream()); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 251 | SubSection.done(); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 252 | } |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 253 | } |
| 254 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 255 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 256 | WasmYAML::NameSection &Section) { |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 257 | writeStringRef(Section.Name, OS); |
| 258 | if (Section.FunctionNames.size()) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 259 | writeUint8(OS, wasm::WASM_NAMES_FUNCTION); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 260 | |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 261 | SubSectionWriter SubSection(OS); |
| 262 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 263 | encodeULEB128(Section.FunctionNames.size(), SubSection.getStream()); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 264 | for (const WasmYAML::NameEntry &NameEntry : Section.FunctionNames) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 265 | encodeULEB128(NameEntry.Index, SubSection.getStream()); |
| 266 | writeStringRef(NameEntry.Name, SubSection.getStream()); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 269 | SubSection.done(); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 270 | } |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 271 | } |
| 272 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 273 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 274 | WasmYAML::ProducersSection &Section) { |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 275 | writeStringRef(Section.Name, OS); |
| 276 | int Fields = int(!Section.Languages.empty()) + int(!Section.Tools.empty()) + |
| 277 | int(!Section.SDKs.empty()); |
| 278 | if (Fields == 0) |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 279 | return; |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 280 | encodeULEB128(Fields, OS); |
| 281 | for (auto &Field : {std::make_pair(StringRef("language"), &Section.Languages), |
| 282 | std::make_pair(StringRef("processed-by"), &Section.Tools), |
| 283 | std::make_pair(StringRef("sdk"), &Section.SDKs)}) { |
| 284 | if (Field.second->empty()) |
| 285 | continue; |
| 286 | writeStringRef(Field.first, OS); |
| 287 | encodeULEB128(Field.second->size(), OS); |
| 288 | for (auto &Entry : *Field.second) { |
| 289 | writeStringRef(Entry.Name, OS); |
| 290 | writeStringRef(Entry.Version, OS); |
| 291 | } |
| 292 | } |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 293 | } |
| 294 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 295 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 296 | WasmYAML::TargetFeaturesSection &Section) { |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 297 | writeStringRef(Section.Name, OS); |
| 298 | encodeULEB128(Section.Features.size(), OS); |
| 299 | for (auto &E : Section.Features) { |
| 300 | writeUint8(OS, E.Prefix); |
| 301 | writeStringRef(E.Name, OS); |
| 302 | } |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 303 | } |
| 304 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 305 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 306 | WasmYAML::CustomSection &Section) { |
Sam Clegg | e4afbc6 | 2018-11-14 18:36:24 +0000 | [diff] [blame] | 307 | if (auto S = dyn_cast<WasmYAML::DylinkSection>(&Section)) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 308 | writeSectionContent(OS, *S); |
Sam Clegg | e4afbc6 | 2018-11-14 18:36:24 +0000 | [diff] [blame] | 309 | } else if (auto S = dyn_cast<WasmYAML::NameSection>(&Section)) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 310 | writeSectionContent(OS, *S); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 311 | } else if (auto S = dyn_cast<WasmYAML::LinkingSection>(&Section)) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 312 | writeSectionContent(OS, *S); |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 313 | } else if (auto S = dyn_cast<WasmYAML::ProducersSection>(&Section)) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 314 | writeSectionContent(OS, *S); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 315 | } else if (auto S = dyn_cast<WasmYAML::TargetFeaturesSection>(&Section)) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 316 | writeSectionContent(OS, *S); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 317 | } else { |
Sam Clegg | 9745afa | 2018-04-12 20:31:12 +0000 | [diff] [blame] | 318 | writeStringRef(Section.Name, OS); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 319 | Section.Payload.writeAsBinary(OS); |
| 320 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 321 | } |
| 322 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 323 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 324 | WasmYAML::TypeSection &Section) { |
| 325 | encodeULEB128(Section.Signatures.size(), OS); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 326 | uint32_t ExpectedIndex = 0; |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 327 | for (const WasmYAML::Signature &Sig : Section.Signatures) { |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 328 | if (Sig.Index != ExpectedIndex) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 329 | reportError("unexpected type index: " + Twine(Sig.Index)); |
| 330 | return; |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 331 | } |
| 332 | ++ExpectedIndex; |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 333 | writeUint8(OS, Sig.Form); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 334 | encodeULEB128(Sig.ParamTypes.size(), OS); |
| 335 | for (auto ParamType : Sig.ParamTypes) |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 336 | writeUint8(OS, ParamType); |
Thomas Lively | 393d0f7 | 2019-10-18 20:27:30 +0000 | [diff] [blame] | 337 | encodeULEB128(Sig.ReturnTypes.size(), OS); |
| 338 | for (auto ReturnType : Sig.ReturnTypes) |
| 339 | writeUint8(OS, ReturnType); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 340 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 341 | } |
| 342 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 343 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 344 | WasmYAML::ImportSection &Section) { |
| 345 | encodeULEB128(Section.Imports.size(), OS); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 346 | for (const WasmYAML::Import &Import : Section.Imports) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 347 | writeStringRef(Import.Module, OS); |
| 348 | writeStringRef(Import.Field, OS); |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 349 | writeUint8(OS, Import.Kind); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 350 | switch (Import.Kind) { |
| 351 | case wasm::WASM_EXTERNAL_FUNCTION: |
| 352 | encodeULEB128(Import.SigIndex, OS); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 353 | NumImportedFunctions++; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 354 | break; |
| 355 | case wasm::WASM_EXTERNAL_GLOBAL: |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 356 | writeUint8(OS, Import.GlobalImport.Type); |
Sam Clegg | 41db519 | 2017-05-10 00:14:04 +0000 | [diff] [blame] | 357 | writeUint8(OS, Import.GlobalImport.Mutable); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 358 | NumImportedGlobals++; |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 359 | break; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 360 | case wasm::WASM_EXTERNAL_EVENT: |
| 361 | writeUint32(OS, Import.EventImport.Attribute); |
| 362 | writeUint32(OS, Import.EventImport.SigIndex); |
| 363 | NumImportedGlobals++; |
| 364 | break; |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 365 | case wasm::WASM_EXTERNAL_MEMORY: |
| 366 | writeLimits(Import.Memory, OS); |
| 367 | break; |
| 368 | case wasm::WASM_EXTERNAL_TABLE: |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 369 | writeUint8(OS, Import.TableImport.ElemType); |
Sam Clegg | 41db519 | 2017-05-10 00:14:04 +0000 | [diff] [blame] | 370 | writeLimits(Import.TableImport.TableLimits, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 371 | break; |
| 372 | default: |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 373 | reportError("unknown import type: " +Twine(Import.Kind)); |
| 374 | return; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 377 | } |
| 378 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 379 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 380 | WasmYAML::FunctionSection &Section) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 381 | encodeULEB128(Section.FunctionTypes.size(), OS); |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 382 | for (uint32_t FuncType : Section.FunctionTypes) |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 383 | encodeULEB128(FuncType, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 384 | } |
| 385 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 386 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 387 | WasmYAML::ExportSection &Section) { |
| 388 | encodeULEB128(Section.Exports.size(), OS); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 389 | for (const WasmYAML::Export &Export : Section.Exports) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 390 | writeStringRef(Export.Name, OS); |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 391 | writeUint8(OS, Export.Kind); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 392 | encodeULEB128(Export.Index, OS); |
| 393 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 394 | } |
| 395 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 396 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 397 | WasmYAML::StartSection &Section) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 398 | encodeULEB128(Section.StartFunction, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 399 | } |
| 400 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 401 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 402 | WasmYAML::TableSection &Section) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 403 | encodeULEB128(Section.Tables.size(), OS); |
| 404 | for (auto &Table : Section.Tables) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 405 | writeUint8(OS, Table.ElemType); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 406 | writeLimits(Table.TableLimits, OS); |
| 407 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 408 | } |
| 409 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 410 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 411 | WasmYAML::MemorySection &Section) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 412 | encodeULEB128(Section.Memories.size(), OS); |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 413 | for (const WasmYAML::Limits &Mem : Section.Memories) |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 414 | writeLimits(Mem, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 415 | } |
| 416 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 417 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 418 | WasmYAML::GlobalSection &Section) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 419 | encodeULEB128(Section.Globals.size(), OS); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 420 | uint32_t ExpectedIndex = NumImportedGlobals; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 421 | for (auto &Global : Section.Globals) { |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 422 | if (Global.Index != ExpectedIndex) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 423 | reportError("unexpected global index: " + Twine(Global.Index)); |
| 424 | return; |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 425 | } |
| 426 | ++ExpectedIndex; |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 427 | writeUint8(OS, Global.Type); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 428 | writeUint8(OS, Global.Mutable); |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 429 | writeInitExpr(OS, Global.InitExpr); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 430 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 431 | } |
| 432 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 433 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 434 | WasmYAML::EventSection &Section) { |
| 435 | encodeULEB128(Section.Events.size(), OS); |
| 436 | uint32_t ExpectedIndex = NumImportedEvents; |
| 437 | for (auto &Event : Section.Events) { |
| 438 | if (Event.Index != ExpectedIndex) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 439 | reportError("unexpected event index: " + Twine(Event.Index)); |
| 440 | return; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 441 | } |
| 442 | ++ExpectedIndex; |
| 443 | encodeULEB128(Event.Attribute, OS); |
| 444 | encodeULEB128(Event.SigIndex, OS); |
| 445 | } |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 446 | } |
| 447 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 448 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 449 | WasmYAML::ElemSection &Section) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 450 | encodeULEB128(Section.Segments.size(), OS); |
| 451 | for (auto &Segment : Section.Segments) { |
| 452 | encodeULEB128(Segment.TableIndex, OS); |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 453 | writeInitExpr(OS, Segment.Offset); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 454 | |
| 455 | encodeULEB128(Segment.Functions.size(), OS); |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 456 | for (auto &Function : Segment.Functions) |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 457 | encodeULEB128(Function, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 458 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 459 | } |
| 460 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 461 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 462 | WasmYAML::CodeSection &Section) { |
| 463 | encodeULEB128(Section.Functions.size(), OS); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 464 | uint32_t ExpectedIndex = NumImportedFunctions; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 465 | for (auto &Func : Section.Functions) { |
| 466 | std::string OutString; |
| 467 | raw_string_ostream StringStream(OutString); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 468 | if (Func.Index != ExpectedIndex) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 469 | reportError("unexpected function index: " + Twine(Func.Index)); |
| 470 | return; |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 471 | } |
| 472 | ++ExpectedIndex; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 473 | |
| 474 | encodeULEB128(Func.Locals.size(), StringStream); |
| 475 | for (auto &LocalDecl : Func.Locals) { |
| 476 | encodeULEB128(LocalDecl.Count, StringStream); |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 477 | writeUint8(StringStream, LocalDecl.Type); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | Func.Body.writeAsBinary(StringStream); |
| 481 | |
| 482 | // Write the section size followed by the content |
| 483 | StringStream.flush(); |
| 484 | encodeULEB128(OutString.size(), OS); |
| 485 | OS << OutString; |
| 486 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 487 | } |
| 488 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 489 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 490 | WasmYAML::DataSection &Section) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 491 | encodeULEB128(Section.Segments.size(), OS); |
| 492 | for (auto &Segment : Section.Segments) { |
Thomas Lively | 2e15040 | 2019-02-19 22:56:19 +0000 | [diff] [blame] | 493 | encodeULEB128(Segment.InitFlags, OS); |
| 494 | if (Segment.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) |
| 495 | encodeULEB128(Segment.MemoryIndex, OS); |
| 496 | if ((Segment.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 497 | writeInitExpr(OS, Segment.Offset); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 498 | encodeULEB128(Segment.Content.binary_size(), OS); |
| 499 | Segment.Content.writeAsBinary(OS); |
| 500 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 501 | } |
| 502 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 503 | void WasmWriter::writeSectionContent(raw_ostream &OS, |
| 504 | WasmYAML::DataCountSection &Section) { |
Thomas Lively | fef8de6 | 2019-04-12 22:27:48 +0000 | [diff] [blame] | 505 | encodeULEB128(Section.Count, OS); |
Thomas Lively | fef8de6 | 2019-04-12 22:27:48 +0000 | [diff] [blame] | 506 | } |
| 507 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 508 | void WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec, |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 509 | uint32_t SectionIndex) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 510 | switch (Sec.Type) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 511 | case wasm::WASM_SEC_CODE: |
| 512 | writeStringRef("reloc.CODE", OS); |
| 513 | break; |
| 514 | case wasm::WASM_SEC_DATA: |
| 515 | writeStringRef("reloc.DATA", OS); |
| 516 | break; |
| 517 | case wasm::WASM_SEC_CUSTOM: { |
Simon Pilgrim | 6e1a9b0 | 2019-09-17 19:14:11 +0000 | [diff] [blame] | 518 | auto *CustomSection = cast<WasmYAML::CustomSection>(&Sec); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 519 | writeStringRef(("reloc." + CustomSection->Name).str(), OS); |
| 520 | break; |
| 521 | } |
| 522 | default: |
| 523 | llvm_unreachable("not yet implemented"); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 526 | encodeULEB128(SectionIndex, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 527 | encodeULEB128(Sec.Relocations.size(), OS); |
| 528 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 529 | for (auto Reloc : Sec.Relocations) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 530 | writeUint8(OS, Reloc.Type); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 531 | encodeULEB128(Reloc.Offset, OS); |
| 532 | encodeULEB128(Reloc.Index, OS); |
| 533 | switch (Reloc.Type) { |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 534 | case wasm::R_WASM_MEMORY_ADDR_LEB: |
| 535 | case wasm::R_WASM_MEMORY_ADDR_SLEB: |
| 536 | case wasm::R_WASM_MEMORY_ADDR_I32: |
| 537 | case wasm::R_WASM_FUNCTION_OFFSET_I32: |
| 538 | case wasm::R_WASM_SECTION_OFFSET_I32: |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 539 | encodeULEB128(Reloc.Addend, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 542 | } |
| 543 | |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 544 | bool WasmWriter::writeWasm(raw_ostream &OS) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 545 | // Write headers |
| 546 | OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic)); |
| 547 | writeUint32(OS, Obj.Header.Version); |
| 548 | |
| 549 | // Write each section |
Heejin Ahn | feef720 | 2018-12-15 00:58:12 +0000 | [diff] [blame] | 550 | llvm::object::WasmSectionOrderChecker Checker; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 551 | for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) { |
Heejin Ahn | feef720 | 2018-12-15 00:58:12 +0000 | [diff] [blame] | 552 | StringRef SecName = ""; |
| 553 | if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get())) |
| 554 | SecName = S->Name; |
| 555 | if (!Checker.isValidSectionOrder(Sec->Type, SecName)) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 556 | reportError("out of order section type: " + Twine(Sec->Type)); |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 557 | return false; |
Heejin Ahn | feef720 | 2018-12-15 00:58:12 +0000 | [diff] [blame] | 558 | } |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 559 | encodeULEB128(Sec->Type, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 560 | std::string OutString; |
| 561 | raw_string_ostream StringStream(OutString); |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 562 | if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get())) |
| 563 | writeSectionContent(StringStream, *S); |
| 564 | else if (auto S = dyn_cast<WasmYAML::TypeSection>(Sec.get())) |
| 565 | writeSectionContent(StringStream, *S); |
| 566 | else if (auto S = dyn_cast<WasmYAML::ImportSection>(Sec.get())) |
| 567 | writeSectionContent(StringStream, *S); |
| 568 | else if (auto S = dyn_cast<WasmYAML::FunctionSection>(Sec.get())) |
| 569 | writeSectionContent(StringStream, *S); |
| 570 | else if (auto S = dyn_cast<WasmYAML::TableSection>(Sec.get())) |
| 571 | writeSectionContent(StringStream, *S); |
| 572 | else if (auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get())) |
| 573 | writeSectionContent(StringStream, *S); |
| 574 | else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get())) |
| 575 | writeSectionContent(StringStream, *S); |
| 576 | else if (auto S = dyn_cast<WasmYAML::EventSection>(Sec.get())) |
| 577 | writeSectionContent(StringStream, *S); |
| 578 | else if (auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get())) |
| 579 | writeSectionContent(StringStream, *S); |
| 580 | else if (auto S = dyn_cast<WasmYAML::StartSection>(Sec.get())) |
| 581 | writeSectionContent(StringStream, *S); |
| 582 | else if (auto S = dyn_cast<WasmYAML::ElemSection>(Sec.get())) |
| 583 | writeSectionContent(StringStream, *S); |
| 584 | else if (auto S = dyn_cast<WasmYAML::CodeSection>(Sec.get())) |
| 585 | writeSectionContent(StringStream, *S); |
| 586 | else if (auto S = dyn_cast<WasmYAML::DataSection>(Sec.get())) |
| 587 | writeSectionContent(StringStream, *S); |
| 588 | else if (auto S = dyn_cast<WasmYAML::DataCountSection>(Sec.get())) |
| 589 | writeSectionContent(StringStream, *S); |
| 590 | else |
| 591 | reportError("unknown section type: " + Twine(Sec->Type)); |
| 592 | |
| 593 | if (HasError) |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 594 | return false; |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 595 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 596 | StringStream.flush(); |
| 597 | |
| 598 | // Write the section size followed by the content |
| 599 | encodeULEB128(OutString.size(), OS); |
| 600 | OS << OutString; |
| 601 | } |
| 602 | |
| 603 | // write reloc sections for any section that have relocations |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 604 | uint32_t SectionIndex = 0; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 605 | for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) { |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 606 | if (Sec->Relocations.empty()) { |
| 607 | SectionIndex++; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 608 | continue; |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 609 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 610 | |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 611 | writeUint8(OS, wasm::WASM_SEC_CUSTOM); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 612 | std::string OutString; |
| 613 | raw_string_ostream StringStream(OutString); |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 614 | writeRelocSection(StringStream, *Sec, SectionIndex++); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 615 | StringStream.flush(); |
| 616 | |
| 617 | encodeULEB128(OutString.size(), OS); |
| 618 | OS << OutString; |
| 619 | } |
| 620 | |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 621 | return true; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 624 | namespace llvm { |
| 625 | namespace yaml { |
| 626 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 627 | bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH) { |
| 628 | WasmWriter Writer(Doc, EH); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 629 | return Writer.writeWasm(Out); |
| 630 | } |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 631 | |
| 632 | } // namespace yaml |
| 633 | } // namespace llvm |