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.
10 "cmd/link/internal/ld"
11 "cmd/link/internal/loader"
12 "cmd/link/internal/sym"
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
43 func gentext(ctxt *ld.Link, ldr *loader.Loader) {
46 type wasmFunc struct {
52 type wasmFuncType struct {
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
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.
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
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)
102 type wasmDataSect struct {
107 var dataSects []wasmDataSect
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)),
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}
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}},
138 // collect host imports (functions that get imported from the WebAssembly host, usually JavaScript)
139 hostImports := []*wasmFunc{
142 Type: lookupType(&wasmFuncType{Params: []byte{I32}}, &types),
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++ {
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),
160 // collect functions with WebAssembly body
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)
171 // Relocations have variable length, handle them here.
172 relocs := ldr.Relocs(fn)
175 for ri := 0; ri < relocs.Count(); ri++ {
178 continue // skip marker relocations
180 wfn.Write(P[off:r.Off()])
185 writeSleb128(wfn, ldr.SymValue(rs)+r.Add())
187 writeSleb128(wfn, int64(len(hostImports))+ldr.SymValue(rs)>>16-funcValueOffset)
188 case objabi.R_WASMIMPORT:
189 writeSleb128(wfn, hostImportMap[rs])
191 ldr.Errorf(fn, "bad reloc type %d (%s)", r.Type(), sym.RelocName(ctxt.Arch, r.Type()))
199 if sig, ok := wasmFuncTypes[ldr.SymName(fn)]; ok {
200 typ = lookupType(sig, &types)
203 name := nameRegexp.ReplaceAllString(ldr.SymName(fn), "_")
204 fns[i] = &wasmFunc{Name: name, Type: typ, Code: wfn.Bytes()}
207 ctxt.Out.Write([]byte{0x00, 0x61, 0x73, 0x6d}) // magic
208 ctxt.Out.Write([]byte{0x01, 0x00, 0x00, 0x00}) // version
210 // Add any buildid early in the binary:
211 if len(buildid) != 0 {
212 writeBuildID(ctxt, buildid)
215 writeTypeSec(ctxt, types)
216 writeImportSec(ctxt, hostImports)
217 writeFunctionSec(ctxt, fns)
218 writeTableSec(ctxt, fns)
219 writeMemorySec(ctxt, ldr)
221 writeExportSec(ctxt, ldr, len(hostImports))
222 writeElementSec(ctxt, uint64(len(hostImports)), uint64(len(fns)))
223 writeCodeSec(ctxt, fns)
225 writeProducerSec(ctxt)
227 writeNameSec(ctxt, len(hostImports), fns)
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) {
237 *types = append(*types, sig)
238 return uint32(len(*types) - 1)
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
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)
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)
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)
267 writeUleb128(ctxt.Out, uint64(len(types)))
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))
275 writeUleb128(ctxt.Out, uint64(len(t.Results)))
276 for _, v := range t.Results {
277 ctxt.Out.WriteByte(byte(v))
281 writeSecSize(ctxt, sizeOffset)
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)
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))
297 writeSecSize(ctxt, sizeOffset)
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)
305 writeUleb128(ctxt.Out, uint64(len(fns)))
306 for _, fn := range fns {
307 writeUleb128(ctxt.Out, uint64(fn.Type))
310 writeSecSize(ctxt, sizeOffset)
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)
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
325 writeSecSize(ctxt, sizeOffset)
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)
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
337 const wasmPageSize = 64 << 10 // 64KB
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
343 writeSecSize(ctxt, sizeOffset)
346 // writeGlobalSec writes the section that declares global variables.
347 func writeGlobalSec(ctxt *ld.Link) {
348 sizeOffset := writeSecHeader(ctxt, sectionGlobal)
350 globalRegs := []byte{
361 writeUleb128(ctxt.Out, uint64(len(globalRegs))) // number of globals
363 for _, typ := range globalRegs {
364 ctxt.Out.WriteByte(typ)
365 ctxt.Out.WriteByte(0x01) // var
368 writeI32Const(ctxt.Out, 0)
370 writeI64Const(ctxt.Out, 0)
372 ctxt.Out.WriteByte(0x0b) // end
375 writeSecSize(ctxt, sizeOffset)
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)
384 writeUleb128(ctxt.Out, 4) // number of exports
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
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
398 writeSecSize(ctxt, sizeOffset)
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)
407 writeUleb128(ctxt.Out, 1) // number of element segments
409 writeUleb128(ctxt.Out, 0) // tableidx
410 writeI32Const(ctxt.Out, funcValueOffset)
411 ctxt.Out.WriteByte(0x0b) // end
413 writeUleb128(ctxt.Out, numFns) // number of entries
414 for i := uint64(0); i < numFns; i++ {
415 writeUleb128(ctxt.Out, numImports+i)
418 writeSecSize(ctxt, sizeOffset)
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)
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)
432 writeSecSize(ctxt, sizeOffset)
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)
439 type dataSegment struct {
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
449 // Generate at most this many segments. A higher number of segments gets rejected by some WebAssembly runtimes.
450 const maxNumSegments = 100000
452 var segments []*dataSegment
453 for secIndex, ds := range dataSects {
455 offset := int32(ds.sect.Vaddr)
457 // skip leading zeroes
458 for len(data) > 0 && data[0] == 0 {
464 dataLen := int32(len(data))
465 var segmentEnd, zeroEnd int32
466 if len(segments)+(len(dataSects)-secIndex) == maxNumSegments {
471 // look for beginning of zeroes
472 for segmentEnd < dataLen && data[segmentEnd] != 0 {
475 // look for end of zeroes
477 for zeroEnd < dataLen && data[zeroEnd] == 0 {
480 // emit segment if omitting zeroes reduces the output size
481 if zeroEnd-segmentEnd >= segmentOverhead || zeroEnd == dataLen {
488 segments = append(segments, &dataSegment{
490 data: data[:segmentEnd],
492 data = data[zeroEnd:]
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)
506 writeSecSize(ctxt, sizeOffset)
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")
514 writeUleb128(ctxt.Out, 2) // number of fields
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
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
526 writeSecSize(ctxt, sizeOffset)
529 var nameRegexp = regexp.MustCompile(`[^\w.]`)
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")
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)
544 writeSecSize(ctxt, sizeOffset2)
546 writeSecSize(ctxt, sizeOffset)
549 type nameWriter interface {
554 func writeI32Const(w io.ByteWriter, v int32) {
555 w.WriteByte(0x41) // i32.const
556 writeSleb128(w, int64(v))
559 func writeI64Const(w io.ByteWriter, v int64) {
560 w.WriteByte(0x42) // i64.const
564 func writeName(w nameWriter, name string) {
565 writeUleb128(w, uint64(len(name)))
566 w.Write([]byte(name))
569 func writeUleb128(w io.ByteWriter, v uint64) {
571 w.WriteByte(uint8(v))
586 func writeUleb128FixedLength(w io.ByteWriter, v uint64, length int) {
587 for i := 0; i < length; i++ {
596 panic("writeUleb128FixedLength: length too small")
600 func writeSleb128(w io.ByteWriter, v int64) {
606 more = !((v == 0 && s == 0) || (v == -1 && s != 0))