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