Debian Codegraph @

src/cmd/link/internal/wasm/asm.go

raw
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package wasm
6
7 import (
8 "bytes"
9 "cmd/internal/obj"
10 "cmd/internal/obj/wasm"
11 "cmd/internal/objabi"
12 "cmd/link/internal/ld"
13 "cmd/link/internal/loader"
14 "cmd/link/internal/sym"
15 "fmt"
16 "internal/abi"
17 "internal/buildcfg"
18 "io"
19 "regexp"
20 )
21
22 const (
23 I32 = 0x7F
24 I64 = 0x7E
25 F32 = 0x7D
26 F64 = 0x7C
27 )
28
29 const (
30 sectionCustom = 0
31 sectionType = 1
32 sectionImport = 2
33 sectionFunction = 3
34 sectionTable = 4
35 sectionMemory = 5
36 sectionGlobal = 6
37 sectionExport = 7
38 sectionStart = 8
39 sectionElement = 9
40 sectionCode = 10
41 sectionData = 11
42 )
43
44 // funcValueOffset is the offset between the PC_F value of a function and the index of the function in WebAssembly
45 const funcValueOffset = 0x1000 // TODO(neelance): make function addresses play nice with heap addresses
46
47 func gentext(ctxt *ld.Link, ldr *loader.Loader) {
48 }
49
50 type wasmFunc struct {
51 Module string
52 Name string
53 Type uint32
54 Code []byte
55 }
56
57 type wasmFuncType struct {
58 Params []byte
59 Results []byte
60 }
61
62 func readWasmImport(ldr *loader.Loader, s loader.Sym) obj.WasmImport {
63 var wi obj.WasmImport
64 wi.Read(ldr.Data(s))
65 return wi
66 }
67
68 var wasmFuncTypes = map[string]*wasmFuncType{
69 "_rt0_wasm_js": {Params: []byte{}}, //
70 "_rt0_wasm_wasip1": {Params: []byte{}}, //
71 "_rt0_wasm_wasip1_lib": {Params: []byte{}}, //
72 "wasm_export__start": {}, //
73 "wasm_export_run": {Params: []byte{I32, I32}}, // argc, argv
74 "wasm_export_resume": {Params: []byte{}}, //
75 "wasm_export_getsp": {Results: []byte{I32}}, // sp
76 "wasm_pc_f_loop": {Params: []byte{}}, //
77 "wasm_pc_f_loop_export": {Params: []byte{I32}}, // pc_f
78 "runtime.wasmDiv": {Params: []byte{I64, I64}, Results: []byte{I64}}, // x, y -> x/y
79 "runtime.wasmTruncS": {Params: []byte{F64}, Results: []byte{I64}}, // x -> int(x)
80 "runtime.wasmTruncU": {Params: []byte{F64}, Results: []byte{I64}}, // x -> uint(x)
81 "gcWriteBarrier": {Params: []byte{I64}, Results: []byte{I64}}, // #bytes -> bufptr
82 "runtime.gcWriteBarrier1": {Results: []byte{I64}}, // -> bufptr
83 "runtime.gcWriteBarrier2": {Results: []byte{I64}}, // -> bufptr
84 "runtime.gcWriteBarrier3": {Results: []byte{I64}}, // -> bufptr
85 "runtime.gcWriteBarrier4": {Results: []byte{I64}}, // -> bufptr
86 "runtime.gcWriteBarrier5": {Results: []byte{I64}}, // -> bufptr
87 "runtime.gcWriteBarrier6": {Results: []byte{I64}}, // -> bufptr
88 "runtime.gcWriteBarrier7": {Results: []byte{I64}}, // -> bufptr
89 "runtime.gcWriteBarrier8": {Results: []byte{I64}}, // -> bufptr
90 "runtime.notInitialized": {}, //
91 "cmpbody": {Params: []byte{I64, I64, I64, I64}, Results: []byte{I64}}, // a, alen, b, blen -> -1/0/1
92 "memeqbody": {Params: []byte{I64, I64, I64}, Results: []byte{I64}}, // a, b, len -> 0/1
93 "memcmp": {Params: []byte{I32, I32, I32}, Results: []byte{I32}}, // a, b, len -> <0/0/>0
94 "memchr": {Params: []byte{I32, I32, I32}, Results: []byte{I32}}, // s, c, len -> index
95 }
96
97 func assignAddress(ldr *loader.Loader, sect *sym.Section, n int, s loader.Sym, va uint64, isTramp bool) (*sym.Section, int, uint64) {
98 // WebAssembly functions do not live in the same address space as the linear memory.
99 // Instead, WebAssembly automatically assigns indices. Imported functions (section "import")
100 // have indices 0 to n. They are followed by native functions (sections "function" and "code")
101 // with indices n+1 and following.
102 //
103 // The following rules describe how wasm handles function indices and addresses:
104 // PC_F = funcValueOffset + WebAssembly function index (not including the imports)
105 // s.Value = PC = PC_F<<16 + PC_B
106 //
107 // The funcValueOffset is necessary to avoid conflicts with expectations
108 // that the Go runtime has about function addresses.
109 // The field "s.Value" corresponds to the concept of PC at runtime.
110 // However, there is no PC register, only PC_F and PC_B. PC_F denotes the function,
111 // PC_B the resume point inside of that function. The entry of the function has PC_B = 0.
112 ldr.SetSymSect(s, sect)
113 ldr.SetSymValue(s, int64(funcValueOffset+va/abi.MINFUNC)<<16) // va starts at zero
114 va += uint64(abi.MINFUNC)
115 return sect, n, va
116 }
117
118 type wasmDataSect struct {
119 sect *sym.Section
120 data []byte
121 }
122
123 var dataSects []wasmDataSect
124
125 func asmb(ctxt *ld.Link, ldr *loader.Loader) {
126 sections := []*sym.Section{
127 ldr.SymSect(ldr.Lookup("runtime.rodata", 0)),
128 ldr.SymSect(ldr.Lookup("runtime.typelink", 0)),
129 ldr.SymSect(ldr.Lookup("runtime.itablink", 0)),
130 ldr.SymSect(ldr.Lookup("runtime.firstmoduledata", 0)),
131 ldr.SymSect(ldr.Lookup("runtime.pclntab", 0)),
132 ldr.SymSect(ldr.Lookup("runtime.noptrdata", 0)),
133 ldr.SymSect(ldr.Lookup("runtime.data", 0)),
134 }
135
136 dataSects = make([]wasmDataSect, len(sections))
137 for i, sect := range sections {
138 data := ld.DatblkBytes(ctxt, int64(sect.Vaddr), int64(sect.Length))
139 dataSects[i] = wasmDataSect{sect, data}
140 }
141 }
142
143 // asmb writes the final WebAssembly module binary.
144 // Spec: https://webassembly.github.io/spec/core/binary/modules.html
145 func asmb2(ctxt *ld.Link, ldr *loader.Loader) {
146 types := []*wasmFuncType{
147 // For normal Go functions, the single parameter is PC_B,
148 // the return value is
149 // 0 if the function returned normally or
150 // 1 if the stack needs to be unwound.
151 {Params: []byte{I32}, Results: []byte{I32}},
152 }
153
154 // collect host imports (functions that get imported from the WebAssembly host, usually JavaScript)
155 // we store the import index of each imported function, so the R_WASMIMPORT relocation
156 // can write the correct index after a "call" instruction
157 // these are added as import statements to the top of the WebAssembly binary
158 var hostImports []*wasmFunc
159 hostImportMap := make(map[loader.Sym]int64)
160 for _, fn := range ctxt.Textp {
161 relocs := ldr.Relocs(fn)
162 for ri := 0; ri < relocs.Count(); ri++ {
163 r := relocs.At(ri)
164 if r.Type() == objabi.R_WASMIMPORT {
165 if wsym := ldr.WasmImportSym(fn); wsym != 0 {
166 wi := readWasmImport(ldr, wsym)
167 hostImportMap[fn] = int64(len(hostImports))
168 hostImports = append(hostImports, &wasmFunc{
169 Module: wi.Module,
170 Name: wi.Name,
171 Type: lookupType(&wasmFuncType{
172 Params: fieldsToTypes(wi.Params),
173 Results: fieldsToTypes(wi.Results),
174 }, &types),
175 })
176 } else {
177 panic(fmt.Sprintf("missing wasm symbol for %s", ldr.SymName(r.Sym())))
178 }
179 }
180 }
181 }
182
183 // collect functions with WebAssembly body
184 var buildid []byte
185 fns := make([]*wasmFunc, len(ctxt.Textp))
186 for i, fn := range ctxt.Textp {
187 wfn := new(bytes.Buffer)
188 if ldr.SymName(fn) == "go:buildid" {
189 writeUleb128(wfn, 0) // number of sets of locals
190 writeI32Const(wfn, 0)
191 wfn.WriteByte(0x0b) // end
192 buildid = ldr.Data(fn)
193 } else {
194 // Relocations have variable length, handle them here.
195 relocs := ldr.Relocs(fn)
196 P := ldr.Data(fn)
197 off := int32(0)
198 for ri := 0; ri < relocs.Count(); ri++ {
199 r := relocs.At(ri)
200 if r.Siz() == 0 {
201 continue // skip marker relocations
202 }
203 wfn.Write(P[off:r.Off()])
204 off = r.Off()
205 rs := r.Sym()
206 switch r.Type() {
207 case objabi.R_ADDR:
208 writeSleb128(wfn, ldr.SymValue(rs)+r.Add())
209 case objabi.R_CALL:
210 writeSleb128(wfn, int64(len(hostImports))+ldr.SymValue(rs)>>16-funcValueOffset)
211 case objabi.R_WASMIMPORT:
212 writeSleb128(wfn, hostImportMap[rs])
213 default:
214 ldr.Errorf(fn, "bad reloc type %d (%s)", r.Type(), sym.RelocName(ctxt.Arch, r.Type()))
215 continue
216 }
217 }
218 wfn.Write(P[off:])
219 }
220
221 typ := uint32(0)
222 if sig, ok := wasmFuncTypes[ldr.SymName(fn)]; ok {
223 typ = lookupType(sig, &types)
224 }
225 if s := ldr.WasmTypeSym(fn); s != 0 {
226 var o obj.WasmFuncType
227 o.Read(ldr.Data(s))
228 t := &wasmFuncType{
229 Params: fieldsToTypes(o.Params),
230 Results: fieldsToTypes(o.Results),
231 }
232 typ = lookupType(t, &types)
233 }
234
235 name := nameRegexp.ReplaceAllString(ldr.SymName(fn), "_")
236 fns[i] = &wasmFunc{Name: name, Type: typ, Code: wfn.Bytes()}
237 }
238
239 ctxt.Out.Write([]byte{0x00, 0x61, 0x73, 0x6d}) // magic
240 ctxt.Out.Write([]byte{0x01, 0x00, 0x00, 0x00}) // version
241
242 // Add any buildid early in the binary:
243 if len(buildid) != 0 {
244 writeBuildID(ctxt, buildid)
245 }
246
247 writeTypeSec(ctxt, types)
248 writeImportSec(ctxt, hostImports)
249 writeFunctionSec(ctxt, fns)
250 writeTableSec(ctxt, fns)
251 writeMemorySec(ctxt, ldr)
252 writeGlobalSec(ctxt)
253 writeExportSec(ctxt, ldr, len(hostImports))
254 writeElementSec(ctxt, uint64(len(hostImports)), uint64(len(fns)))
255 writeCodeSec(ctxt, fns)
256 writeDataSec(ctxt)
257 writeProducerSec(ctxt)
258 if !*ld.FlagS {
259 writeNameSec(ctxt, len(hostImports), fns)
260 }
261 }
262
263 func lookupType(sig *wasmFuncType, types *[]*wasmFuncType) uint32 {
264 for i, t := range *types {
265 if bytes.Equal(sig.Params, t.Params) && bytes.Equal(sig.Results, t.Results) {
266 return uint32(i)
267 }
268 }
269 *types = append(*types, sig)
270 return uint32(len(*types) - 1)
271 }
272
273 func writeSecHeader(ctxt *ld.Link, id uint8) int64 {
274 ctxt.Out.WriteByte(id)
275 sizeOffset := ctxt.Out.Offset()
276 ctxt.Out.Write(make([]byte, 5)) // placeholder for length
277 return sizeOffset
278 }
279
280 func writeSecSize(ctxt *ld.Link, sizeOffset int64) {
281 endOffset := ctxt.Out.Offset()
282 ctxt.Out.SeekSet(sizeOffset)
283 writeUleb128FixedLength(ctxt.Out, uint64(endOffset-sizeOffset-5), 5)
284 ctxt.Out.SeekSet(endOffset)
285 }
286
287 func writeBuildID(ctxt *ld.Link, buildid []byte) {
288 sizeOffset := writeSecHeader(ctxt, sectionCustom)
289 writeName(ctxt.Out, "go:buildid")
290 ctxt.Out.Write(buildid)
291 writeSecSize(ctxt, sizeOffset)
292 }
293
294 // writeTypeSec writes the section that declares all function types
295 // so they can be referenced by index.
296 func writeTypeSec(ctxt *ld.Link, types []*wasmFuncType) {
297 sizeOffset := writeSecHeader(ctxt, sectionType)
298
299 writeUleb128(ctxt.Out, uint64(len(types)))
300
301 for _, t := range types {
302 ctxt.Out.WriteByte(0x60) // functype
303 writeUleb128(ctxt.Out, uint64(len(t.Params)))
304 for _, v := range t.Params {
305 ctxt.Out.WriteByte(v)
306 }
307 writeUleb128(ctxt.Out, uint64(len(t.Results)))
308 for _, v := range t.Results {
309 ctxt.Out.WriteByte(v)
310 }
311 }
312
313 writeSecSize(ctxt, sizeOffset)
314 }
315
316 // writeImportSec writes the section that lists the functions that get
317 // imported from the WebAssembly host, usually JavaScript.
318 func writeImportSec(ctxt *ld.Link, hostImports []*wasmFunc) {
319 sizeOffset := writeSecHeader(ctxt, sectionImport)
320
321 writeUleb128(ctxt.Out, uint64(len(hostImports))) // number of imports
322 for _, fn := range hostImports {
323 if fn.Module != "" {
324 writeName(ctxt.Out, fn.Module)
325 } else {
326 writeName(ctxt.Out, wasm.GojsModule) // provided by the import object in wasm_exec.js
327 }
328 writeName(ctxt.Out, fn.Name)
329 ctxt.Out.WriteByte(0x00) // func import
330 writeUleb128(ctxt.Out, uint64(fn.Type))
331 }
332
333 writeSecSize(ctxt, sizeOffset)
334 }
335
336 // writeFunctionSec writes the section that declares the types of functions.
337 // The bodies of these functions will later be provided in the "code" section.
338 func writeFunctionSec(ctxt *ld.Link, fns []*wasmFunc) {
339 sizeOffset := writeSecHeader(ctxt, sectionFunction)
340
341 writeUleb128(ctxt.Out, uint64(len(fns)))
342 for _, fn := range fns {
343 writeUleb128(ctxt.Out, uint64(fn.Type))
344 }
345
346 writeSecSize(ctxt, sizeOffset)
347 }
348
349 // writeTableSec writes the section that declares tables. Currently there is only a single table
350 // that is used by the CallIndirect operation to dynamically call any function.
351 // The contents of the table get initialized by the "element" section.
352 func writeTableSec(ctxt *ld.Link, fns []*wasmFunc) {
353 sizeOffset := writeSecHeader(ctxt, sectionTable)
354
355 numElements := uint64(funcValueOffset + len(fns))
356 writeUleb128(ctxt.Out, 1) // number of tables
357 ctxt.Out.WriteByte(0x70) // type: anyfunc
358 ctxt.Out.WriteByte(0x00) // no max
359 writeUleb128(ctxt.Out, numElements) // min
360
361 writeSecSize(ctxt, sizeOffset)
362 }
363
364 // writeMemorySec writes the section that declares linear memories. Currently one linear memory is being used.
365 // Linear memory always starts at address zero. More memory can be requested with the GrowMemory instruction.
366 func writeMemorySec(ctxt *ld.Link, ldr *loader.Loader) {
367 sizeOffset := writeSecHeader(ctxt, sectionMemory)
368
369 dataEnd := uint64(ldr.SymValue(ldr.Lookup("runtime.end", 0)))
370 var initialSize = dataEnd + 1<<20 // 1 MB, for runtime init allocating a few pages
371
372 const wasmPageSize = 64 << 10 // 64KB
373
374 writeUleb128(ctxt.Out, 1) // number of memories
375 ctxt.Out.WriteByte(0x00) // no maximum memory size
376 writeUleb128(ctxt.Out, initialSize/wasmPageSize) // minimum (initial) memory size
377
378 writeSecSize(ctxt, sizeOffset)
379 }
380
381 // writeGlobalSec writes the section that declares global variables.
382 func writeGlobalSec(ctxt *ld.Link) {
383 sizeOffset := writeSecHeader(ctxt, sectionGlobal)
384
385 globalRegs := []byte{
386 I32, // 0: SP
387 I64, // 1: CTXT
388 I64, // 2: g
389 I64, // 3: RET0
390 I64, // 4: RET1
391 I64, // 5: RET2
392 I64, // 6: RET3
393 I32, // 7: PAUSE
394 }
395
396 writeUleb128(ctxt.Out, uint64(len(globalRegs))) // number of globals
397
398 for _, typ := range globalRegs {
399 ctxt.Out.WriteByte(typ)
400 ctxt.Out.WriteByte(0x01) // var
401 switch typ {
402 case I32:
403 writeI32Const(ctxt.Out, 0)
404 case I64:
405 writeI64Const(ctxt.Out, 0)
406 }
407 ctxt.Out.WriteByte(0x0b) // end
408 }
409
410 writeSecSize(ctxt, sizeOffset)
411 }
412
413 // writeExportSec writes the section that declares exports.
414 // Exports can be accessed by the WebAssembly host, usually JavaScript.
415 // The wasm_export_* functions and the linear memory get exported.
416 func writeExportSec(ctxt *ld.Link, ldr *loader.Loader, lenHostImports int) {
417 sizeOffset := writeSecHeader(ctxt, sectionExport)
418
419 switch buildcfg.GOOS {
420 case "wasip1":
421 writeUleb128(ctxt.Out, uint64(2+len(ldr.WasmExports))) // number of exports
422 var entry, entryExpName string
423 switch ctxt.BuildMode {
424 case ld.BuildModeExe:
425 entry = "_rt0_wasm_wasip1"
426 entryExpName = "_start"
427 case ld.BuildModeCShared:
428 entry = "_rt0_wasm_wasip1_lib"
429 entryExpName = "_initialize"
430 }
431 s := ldr.Lookup(entry, 0)
432 if s == 0 {
433 ld.Errorf("export symbol %s not defined", entry)
434 }
435 idx := uint32(lenHostImports) + uint32(ldr.SymValue(s)>>16) - funcValueOffset
436 writeName(ctxt.Out, entryExpName) // the wasi entrypoint
437 ctxt.Out.WriteByte(0x00) // func export
438 writeUleb128(ctxt.Out, uint64(idx)) // funcidx
439 for _, s := range ldr.WasmExports {
440 idx := uint32(lenHostImports) + uint32(ldr.SymValue(s)>>16) - funcValueOffset
441 writeName(ctxt.Out, ldr.SymName(s))
442 ctxt.Out.WriteByte(0x00) // func export
443 writeUleb128(ctxt.Out, uint64(idx)) // funcidx
444 }
445 writeName(ctxt.Out, "memory") // memory in wasi
446 ctxt.Out.WriteByte(0x02) // mem export
447 writeUleb128(ctxt.Out, 0) // memidx
448 case "js":
449 writeUleb128(ctxt.Out, uint64(4+len(ldr.WasmExports))) // number of exports
450 for _, name := range []string{"run", "resume", "getsp"} {
451 s := ldr.Lookup("wasm_export_"+name, 0)
452 if s == 0 {
453 ld.Errorf("export symbol %s not defined", "wasm_export_"+name)
454 }
455 idx := uint32(lenHostImports) + uint32(ldr.SymValue(s)>>16) - funcValueOffset
456 writeName(ctxt.Out, name) // inst.exports.run/resume/getsp in wasm_exec.js
457 ctxt.Out.WriteByte(0x00) // func export
458 writeUleb128(ctxt.Out, uint64(idx)) // funcidx
459 }
460 for _, s := range ldr.WasmExports {
461 idx := uint32(lenHostImports) + uint32(ldr.SymValue(s)>>16) - funcValueOffset
462 writeName(ctxt.Out, ldr.SymName(s))
463 ctxt.Out.WriteByte(0x00) // func export
464 writeUleb128(ctxt.Out, uint64(idx)) // funcidx
465 }
466 writeName(ctxt.Out, "mem") // inst.exports.mem in wasm_exec.js
467 ctxt.Out.WriteByte(0x02) // mem export
468 writeUleb128(ctxt.Out, 0) // memidx
469 default:
470 ld.Exitf("internal error: writeExportSec: unrecognized GOOS %s", buildcfg.GOOS)
471 }
472
473 writeSecSize(ctxt, sizeOffset)
474 }
475
476 // writeElementSec writes the section that initializes the tables declared by the "table" section.
477 // The table for CallIndirect gets initialized in a very simple way so that each table index (PC_F value)
478 // maps linearly to the function index (numImports + PC_F).
479 func writeElementSec(ctxt *ld.Link, numImports, numFns uint64) {
480 sizeOffset := writeSecHeader(ctxt, sectionElement)
481
482 writeUleb128(ctxt.Out, 1) // number of element segments
483
484 writeUleb128(ctxt.Out, 0) // tableidx
485 writeI32Const(ctxt.Out, funcValueOffset)
486 ctxt.Out.WriteByte(0x0b) // end
487
488 writeUleb128(ctxt.Out, numFns) // number of entries
489 for i := uint64(0); i < numFns; i++ {
490 writeUleb128(ctxt.Out, numImports+i)
491 }
492
493 writeSecSize(ctxt, sizeOffset)
494 }
495
496 // writeCodeSec writes the section that provides the function bodies for the functions
497 // declared by the "func" section.
498 func writeCodeSec(ctxt *ld.Link, fns []*wasmFunc) {
499 sizeOffset := writeSecHeader(ctxt, sectionCode)
500
501 writeUleb128(ctxt.Out, uint64(len(fns))) // number of code entries
502 for _, fn := range fns {
503 writeUleb128(ctxt.Out, uint64(len(fn.Code)))
504 ctxt.Out.Write(fn.Code)
505 }
506
507 writeSecSize(ctxt, sizeOffset)
508 }
509
510 // writeDataSec writes the section that provides data that will be used to initialize the linear memory.
511 func writeDataSec(ctxt *ld.Link) {
512 sizeOffset := writeSecHeader(ctxt, sectionData)
513
514 type dataSegment struct {
515 offset int32
516 data []byte
517 }
518
519 // Omit blocks of zeroes and instead emit data segments with offsets skipping the zeroes.
520 // This reduces the size of the WebAssembly binary. We use 8 bytes as an estimate for the
521 // overhead of adding a new segment (same as wasm-opt's memory-packing optimization uses).
522 const segmentOverhead = 8
523
524 // Generate at most this many segments. A higher number of segments gets rejected by some WebAssembly runtimes.
525 const maxNumSegments = 100000
526
527 var segments []*dataSegment
528 for secIndex, ds := range dataSects {
529 data := ds.data
530 offset := int32(ds.sect.Vaddr)
531
532 // skip leading zeroes
533 for len(data) > 0 && data[0] == 0 {
534 data = data[1:]
535 offset++
536 }
537
538 for len(data) > 0 {
539 dataLen := int32(len(data))
540 var segmentEnd, zeroEnd int32
541 if len(segments)+(len(dataSects)-secIndex) == maxNumSegments {
542 segmentEnd = dataLen
543 zeroEnd = dataLen
544 } else {
545 for {
546 // look for beginning of zeroes
547 for segmentEnd < dataLen && data[segmentEnd] != 0 {
548 segmentEnd++
549 }
550 // look for end of zeroes
551 zeroEnd = segmentEnd
552 for zeroEnd < dataLen && data[zeroEnd] == 0 {
553 zeroEnd++
554 }
555 // emit segment if omitting zeroes reduces the output size
556 if zeroEnd-segmentEnd >= segmentOverhead || zeroEnd == dataLen {
557 break
558 }
559 segmentEnd = zeroEnd
560 }
561 }
562
563 segments = append(segments, &dataSegment{
564 offset: offset,
565 data: data[:segmentEnd],
566 })
567 data = data[zeroEnd:]
568 offset += zeroEnd
569 }
570 }
571
572 writeUleb128(ctxt.Out, uint64(len(segments))) // number of data entries
573 for _, seg := range segments {
574 writeUleb128(ctxt.Out, 0) // memidx
575 writeI32Const(ctxt.Out, seg.offset)
576 ctxt.Out.WriteByte(0x0b) // end
577 writeUleb128(ctxt.Out, uint64(len(seg.data)))
578 ctxt.Out.Write(seg.data)
579 }
580
581 writeSecSize(ctxt, sizeOffset)
582 }
583
584 // writeProducerSec writes an optional section that reports the source language and compiler version.
585 func writeProducerSec(ctxt *ld.Link) {
586 sizeOffset := writeSecHeader(ctxt, sectionCustom)
587 writeName(ctxt.Out, "producers")
588
589 writeUleb128(ctxt.Out, 2) // number of fields
590
591 writeName(ctxt.Out, "language") // field name
592 writeUleb128(ctxt.Out, 1) // number of values
593 writeName(ctxt.Out, "Go") // value: name
594 writeName(ctxt.Out, buildcfg.Version) // value: version
595
596 writeName(ctxt.Out, "processed-by") // field name
597 writeUleb128(ctxt.Out, 1) // number of values
598 writeName(ctxt.Out, "Go cmd/compile") // value: name
599 writeName(ctxt.Out, buildcfg.Version) // value: version
600
601 writeSecSize(ctxt, sizeOffset)
602 }
603
604 var nameRegexp = regexp.MustCompile(`[^\w.]`)
605
606 // writeNameSec writes an optional section that assigns names to the functions declared by the "func" section.
607 // The names are only used by WebAssembly stack traces, debuggers and decompilers.
608 // TODO(neelance): add symbol table of DATA symbols
609 func writeNameSec(ctxt *ld.Link, firstFnIndex int, fns []*wasmFunc) {
610 sizeOffset := writeSecHeader(ctxt, sectionCustom)
611 writeName(ctxt.Out, "name")
612
613 sizeOffset2 := writeSecHeader(ctxt, 0x01) // function names
614 writeUleb128(ctxt.Out, uint64(len(fns)))
615 for i, fn := range fns {
616 writeUleb128(ctxt.Out, uint64(firstFnIndex+i))
617 writeName(ctxt.Out, fn.Name)
618 }
619 writeSecSize(ctxt, sizeOffset2)
620
621 writeSecSize(ctxt, sizeOffset)
622 }
623
624 type nameWriter interface {
625 io.ByteWriter
626 io.Writer
627 }
628
629 func writeI32Const(w io.ByteWriter, v int32) {
630 w.WriteByte(0x41) // i32.const
631 writeSleb128(w, int64(v))
632 }
633
634 func writeI64Const(w io.ByteWriter, v int64) {
635 w.WriteByte(0x42) // i64.const
636 writeSleb128(w, v)
637 }
638
639 func writeName(w nameWriter, name string) {
640 writeUleb128(w, uint64(len(name)))
641 w.Write([]byte(name))
642 }
643
644 func writeUleb128(w io.ByteWriter, v uint64) {
645 if v < 128 {
646 w.WriteByte(uint8(v))
647 return
648 }
649 more := true
650 for more {
651 c := uint8(v & 0x7f)
652 v >>= 7
653 more = v != 0
654 if more {
655 c |= 0x80
656 }
657 w.WriteByte(c)
658 }
659 }
660
661 func writeUleb128FixedLength(w io.ByteWriter, v uint64, length int) {
662 for i := 0; i < length; i++ {
663 c := uint8(v & 0x7f)
664 v >>= 7
665 if i < length-1 {
666 c |= 0x80
667 }
668 w.WriteByte(c)
669 }
670 if v != 0 {
671 panic("writeUleb128FixedLength: length too small")
672 }
673 }
674
675 func writeSleb128(w io.ByteWriter, v int64) {
676 more := true
677 for more {
678 c := uint8(v & 0x7f)
679 s := uint8(v & 0x40)
680 v >>= 7
681 more = !((v == 0 && s == 0) || (v == -1 && s != 0))
682 if more {
683 c |= 0x80
684 }
685 w.WriteByte(c)
686 }
687 }
688
689 func fieldsToTypes(fields []obj.WasmField) []byte {
690 b := make([]byte, len(fields))
691 for i, f := range fields {
692 switch f.Type {
693 case obj.WasmI32, obj.WasmPtr, obj.WasmBool:
694 b[i] = I32
695 case obj.WasmI64:
696 b[i] = I64
697 case obj.WasmF32:
698 b[i] = F32
699 case obj.WasmF64:
700 b[i] = F64
701 default:
702 panic(fmt.Sprintf("fieldsToTypes: unknown field type: %d", f.Type))
703 }
704 }
705 return b
706 }