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.wasmMove": {Params: []byte{I32, I32, I32}}, // dst, src, len
64 "runtime.wasmZero": {Params: []byte{I32, I32}}, // ptr, len
65 "runtime.wasmDiv": {Params: []byte{I64, I64}, Results: []byte{I64}}, // x, y -> x/y
66 "runtime.wasmTruncS": {Params: []byte{F64}, Results: []byte{I64}}, // x -> int(x)
67 "runtime.wasmTruncU": {Params: []byte{F64}, Results: []byte{I64}}, // x -> uint(x)
68 "runtime.gcWriteBarrier": {Params: []byte{I64, I64}}, // ptr, val
69 "cmpbody": {Params: []byte{I64, I64, I64, I64}, Results: []byte{I64}}, // a, alen, b, blen -> -1/0/1
70 "memeqbody": {Params: []byte{I64, I64, I64}, Results: []byte{I64}}, // a, b, len -> 0/1
71 "memcmp": {Params: []byte{I32, I32, I32}, Results: []byte{I32}}, // a, b, len -> <0/0/>0
72 "memchr": {Params: []byte{I32, I32, I32}, Results: []byte{I32}}, // s, c, len -> index
75 func assignAddress(ldr *loader.Loader, sect *sym.Section, n int, s loader.Sym, va uint64, isTramp bool) (*sym.Section, int, uint64) {
76 // WebAssembly functions do not live in the same address space as the linear memory.
77 // Instead, WebAssembly automatically assigns indices. Imported functions (section "import")
78 // have indices 0 to n. They are followed by native functions (sections "function" and "code")
79 // with indices n+1 and following.
81 // The following rules describe how wasm handles function indices and addresses:
82 // PC_F = funcValueOffset + WebAssembly function index (not including the imports)
83 // s.Value = PC = PC_F<<16 + PC_B
85 // The funcValueOffset is necessary to avoid conflicts with expectations
86 // that the Go runtime has about function addresses.
87 // The field "s.Value" corresponds to the concept of PC at runtime.
88 // However, there is no PC register, only PC_F and PC_B. PC_F denotes the function,
89 // PC_B the resume point inside of that function. The entry of the function has PC_B = 0.
90 ldr.SetSymSect(s, sect)
91 ldr.SetSymValue(s, int64(funcValueOffset+va/ld.MINFUNC)<<16) // va starts at zero
92 va += uint64(ld.MINFUNC)
96 type wasmDataSect struct {
101 var dataSects []wasmDataSect
103 func asmb(ctxt *ld.Link, ldr *loader.Loader) {
104 sections := []*sym.Section{
105 ldr.SymSect(ldr.Lookup("runtime.rodata", 0)),
106 ldr.SymSect(ldr.Lookup("runtime.typelink", 0)),
107 ldr.SymSect(ldr.Lookup("runtime.itablink", 0)),
108 ldr.SymSect(ldr.Lookup("runtime.symtab", 0)),
109 ldr.SymSect(ldr.Lookup("runtime.pclntab", 0)),
110 ldr.SymSect(ldr.Lookup("runtime.noptrdata", 0)),
111 ldr.SymSect(ldr.Lookup("runtime.data", 0)),
114 dataSects = make([]wasmDataSect, len(sections))
115 for i, sect := range sections {
116 data := ld.DatblkBytes(ctxt, int64(sect.Vaddr), int64(sect.Length))
117 dataSects[i] = wasmDataSect{sect, data}
121 // asmb writes the final WebAssembly module binary.
122 // Spec: https://webassembly.github.io/spec/core/binary/modules.html
123 func asmb2(ctxt *ld.Link, ldr *loader.Loader) {
124 types := []*wasmFuncType{
125 // For normal Go functions, the single parameter is PC_B,
126 // the return value is
127 // 0 if the function returned normally or
128 // 1 if the stack needs to be unwound.
129 {Params: []byte{I32}, Results: []byte{I32}},
132 // collect host imports (functions that get imported from the WebAssembly host, usually JavaScript)
133 hostImports := []*wasmFunc{
136 Type: lookupType(&wasmFuncType{Params: []byte{I32}}, &types),
139 hostImportMap := make(map[loader.Sym]int64)
140 for _, fn := range ctxt.Textp {
141 relocs := ldr.Relocs(fn)
142 for ri := 0; ri < relocs.Count(); ri++ {
144 if r.Type() == objabi.R_WASMIMPORT {
145 hostImportMap[r.Sym()] = int64(len(hostImports))
146 hostImports = append(hostImports, &wasmFunc{
147 Name: ldr.SymName(r.Sym()),
148 Type: lookupType(&wasmFuncType{Params: []byte{I32}}, &types),
154 // collect functions with WebAssembly body
156 fns := make([]*wasmFunc, len(ctxt.Textp))
157 for i, fn := range ctxt.Textp {
158 wfn := new(bytes.Buffer)
159 if ldr.SymName(fn) == "go.buildid" {
160 writeUleb128(wfn, 0) // number of sets of locals
161 writeI32Const(wfn, 0)
162 wfn.WriteByte(0x0b) // end
163 buildid = ldr.Data(fn)
165 // Relocations have variable length, handle them here.
166 relocs := ldr.Relocs(fn)
169 for ri := 0; ri < relocs.Count(); ri++ {
172 continue // skip marker relocations
174 wfn.Write(P[off:r.Off()])
179 writeSleb128(wfn, ldr.SymValue(rs)+r.Add())
181 writeSleb128(wfn, int64(len(hostImports))+ldr.SymValue(rs)>>16-funcValueOffset)
182 case objabi.R_WASMIMPORT:
183 writeSleb128(wfn, hostImportMap[rs])
185 ldr.Errorf(fn, "bad reloc type %d (%s)", r.Type(), sym.RelocName(ctxt.Arch, r.Type()))
193 if sig, ok := wasmFuncTypes[ldr.SymName(fn)]; ok {
194 typ = lookupType(sig, &types)
197 name := nameRegexp.ReplaceAllString(ldr.SymName(fn), "_")
198 fns[i] = &wasmFunc{Name: name, Type: typ, Code: wfn.Bytes()}
201 ctxt.Out.Write([]byte{0x00, 0x61, 0x73, 0x6d}) // magic
202 ctxt.Out.Write([]byte{0x01, 0x00, 0x00, 0x00}) // version
204 // Add any buildid early in the binary:
205 if len(buildid) != 0 {
206 writeBuildID(ctxt, buildid)
209 writeTypeSec(ctxt, types)
210 writeImportSec(ctxt, hostImports)
211 writeFunctionSec(ctxt, fns)
212 writeTableSec(ctxt, fns)
213 writeMemorySec(ctxt, ldr)
215 writeExportSec(ctxt, ldr, len(hostImports))
216 writeElementSec(ctxt, uint64(len(hostImports)), uint64(len(fns)))
217 writeCodeSec(ctxt, fns)
219 writeProducerSec(ctxt)
221 writeNameSec(ctxt, len(hostImports), fns)
225 func lookupType(sig *wasmFuncType, types *[]*wasmFuncType) uint32 {
226 for i, t := range *types {
227 if bytes.Equal(sig.Params, t.Params) && bytes.Equal(sig.Results, t.Results) {
231 *types = append(*types, sig)
232 return uint32(len(*types) - 1)
235 func writeSecHeader(ctxt *ld.Link, id uint8) int64 {
236 ctxt.Out.WriteByte(id)
237 sizeOffset := ctxt.Out.Offset()
238 ctxt.Out.Write(make([]byte, 5)) // placeholder for length
242 func writeSecSize(ctxt *ld.Link, sizeOffset int64) {
243 endOffset := ctxt.Out.Offset()
244 ctxt.Out.SeekSet(sizeOffset)
245 writeUleb128FixedLength(ctxt.Out, uint64(endOffset-sizeOffset-5), 5)
246 ctxt.Out.SeekSet(endOffset)
249 func writeBuildID(ctxt *ld.Link, buildid []byte) {
250 sizeOffset := writeSecHeader(ctxt, sectionCustom)
251 writeName(ctxt.Out, "go.buildid")
252 ctxt.Out.Write(buildid)
253 writeSecSize(ctxt, sizeOffset)
256 // writeTypeSec writes the section that declares all function types
257 // so they can be referenced by index.
258 func writeTypeSec(ctxt *ld.Link, types []*wasmFuncType) {
259 sizeOffset := writeSecHeader(ctxt, sectionType)
261 writeUleb128(ctxt.Out, uint64(len(types)))
263 for _, t := range types {
264 ctxt.Out.WriteByte(0x60) // functype
265 writeUleb128(ctxt.Out, uint64(len(t.Params)))
266 for _, v := range t.Params {
267 ctxt.Out.WriteByte(byte(v))
269 writeUleb128(ctxt.Out, uint64(len(t.Results)))
270 for _, v := range t.Results {
271 ctxt.Out.WriteByte(byte(v))
275 writeSecSize(ctxt, sizeOffset)
278 // writeImportSec writes the section that lists the functions that get
279 // imported from the WebAssembly host, usually JavaScript.
280 func writeImportSec(ctxt *ld.Link, hostImports []*wasmFunc) {
281 sizeOffset := writeSecHeader(ctxt, sectionImport)
283 writeUleb128(ctxt.Out, uint64(len(hostImports))) // number of imports
284 for _, fn := range hostImports {
285 writeName(ctxt.Out, "go") // provided by the import object in wasm_exec.js
286 writeName(ctxt.Out, fn.Name)
287 ctxt.Out.WriteByte(0x00) // func import
288 writeUleb128(ctxt.Out, uint64(fn.Type))
291 writeSecSize(ctxt, sizeOffset)
294 // writeFunctionSec writes the section that declares the types of functions.
295 // The bodies of these functions will later be provided in the "code" section.
296 func writeFunctionSec(ctxt *ld.Link, fns []*wasmFunc) {
297 sizeOffset := writeSecHeader(ctxt, sectionFunction)
299 writeUleb128(ctxt.Out, uint64(len(fns)))
300 for _, fn := range fns {
301 writeUleb128(ctxt.Out, uint64(fn.Type))
304 writeSecSize(ctxt, sizeOffset)
307 // writeTableSec writes the section that declares tables. Currently there is only a single table
308 // that is used by the CallIndirect operation to dynamically call any function.
309 // The contents of the table get initialized by the "element" section.
310 func writeTableSec(ctxt *ld.Link, fns []*wasmFunc) {
311 sizeOffset := writeSecHeader(ctxt, sectionTable)
313 numElements := uint64(funcValueOffset + len(fns))
314 writeUleb128(ctxt.Out, 1) // number of tables
315 ctxt.Out.WriteByte(0x70) // type: anyfunc
316 ctxt.Out.WriteByte(0x00) // no max
317 writeUleb128(ctxt.Out, numElements) // min
319 writeSecSize(ctxt, sizeOffset)
322 // writeMemorySec writes the section that declares linear memories. Currently one linear memory is being used.
323 // Linear memory always starts at address zero. More memory can be requested with the GrowMemory instruction.
324 func writeMemorySec(ctxt *ld.Link, ldr *loader.Loader) {
325 sizeOffset := writeSecHeader(ctxt, sectionMemory)
327 dataSection := ldr.SymSect(ldr.Lookup("runtime.data", 0))
328 dataEnd := dataSection.Vaddr + dataSection.Length
329 var initialSize = dataEnd + 16<<20 // 16MB, enough for runtime init without growing
331 const wasmPageSize = 64 << 10 // 64KB
333 writeUleb128(ctxt.Out, 1) // number of memories
334 ctxt.Out.WriteByte(0x00) // no maximum memory size
335 writeUleb128(ctxt.Out, initialSize/wasmPageSize) // minimum (initial) memory size
337 writeSecSize(ctxt, sizeOffset)
340 // writeGlobalSec writes the section that declares global variables.
341 func writeGlobalSec(ctxt *ld.Link) {
342 sizeOffset := writeSecHeader(ctxt, sectionGlobal)
344 globalRegs := []byte{
355 writeUleb128(ctxt.Out, uint64(len(globalRegs))) // number of globals
357 for _, typ := range globalRegs {
358 ctxt.Out.WriteByte(typ)
359 ctxt.Out.WriteByte(0x01) // var
362 writeI32Const(ctxt.Out, 0)
364 writeI64Const(ctxt.Out, 0)
366 ctxt.Out.WriteByte(0x0b) // end
369 writeSecSize(ctxt, sizeOffset)
372 // writeExportSec writes the section that declares exports.
373 // Exports can be accessed by the WebAssembly host, usually JavaScript.
374 // The wasm_export_* functions and the linear memory get exported.
375 func writeExportSec(ctxt *ld.Link, ldr *loader.Loader, lenHostImports int) {
376 sizeOffset := writeSecHeader(ctxt, sectionExport)
378 writeUleb128(ctxt.Out, 4) // number of exports
380 for _, name := range []string{"run", "resume", "getsp"} {
381 s := ldr.Lookup("wasm_export_"+name, 0)
382 idx := uint32(lenHostImports) + uint32(ldr.SymValue(s)>>16) - funcValueOffset
383 writeName(ctxt.Out, name) // inst.exports.run/resume/getsp in wasm_exec.js
384 ctxt.Out.WriteByte(0x00) // func export
385 writeUleb128(ctxt.Out, uint64(idx)) // funcidx
388 writeName(ctxt.Out, "mem") // inst.exports.mem in wasm_exec.js
389 ctxt.Out.WriteByte(0x02) // mem export
390 writeUleb128(ctxt.Out, 0) // memidx
392 writeSecSize(ctxt, sizeOffset)
395 // writeElementSec writes the section that initializes the tables declared by the "table" section.
396 // The table for CallIndirect gets initialized in a very simple way so that each table index (PC_F value)
397 // maps linearly to the function index (numImports + PC_F).
398 func writeElementSec(ctxt *ld.Link, numImports, numFns uint64) {
399 sizeOffset := writeSecHeader(ctxt, sectionElement)
401 writeUleb128(ctxt.Out, 1) // number of element segments
403 writeUleb128(ctxt.Out, 0) // tableidx
404 writeI32Const(ctxt.Out, funcValueOffset)
405 ctxt.Out.WriteByte(0x0b) // end
407 writeUleb128(ctxt.Out, numFns) // number of entries
408 for i := uint64(0); i < numFns; i++ {
409 writeUleb128(ctxt.Out, numImports+i)
412 writeSecSize(ctxt, sizeOffset)
415 // writeElementSec writes the section that provides the function bodies for the functions
416 // declared by the "func" section.
417 func writeCodeSec(ctxt *ld.Link, fns []*wasmFunc) {
418 sizeOffset := writeSecHeader(ctxt, sectionCode)
420 writeUleb128(ctxt.Out, uint64(len(fns))) // number of code entries
421 for _, fn := range fns {
422 writeUleb128(ctxt.Out, uint64(len(fn.Code)))
423 ctxt.Out.Write(fn.Code)
426 writeSecSize(ctxt, sizeOffset)
429 // writeDataSec writes the section that provides data that will be used to initialize the linear memory.
430 func writeDataSec(ctxt *ld.Link) {
431 sizeOffset := writeSecHeader(ctxt, sectionData)
433 type dataSegment struct {
438 // Omit blocks of zeroes and instead emit data segments with offsets skipping the zeroes.
439 // This reduces the size of the WebAssembly binary. We use 8 bytes as an estimate for the
440 // overhead of adding a new segment (same as wasm-opt's memory-packing optimization uses).
441 const segmentOverhead = 8
443 // Generate at most this many segments. A higher number of segments gets rejected by some WebAssembly runtimes.
444 const maxNumSegments = 100000
446 var segments []*dataSegment
447 for secIndex, ds := range dataSects {
449 offset := int32(ds.sect.Vaddr)
451 // skip leading zeroes
452 for len(data) > 0 && data[0] == 0 {
458 dataLen := int32(len(data))
459 var segmentEnd, zeroEnd int32
460 if len(segments)+(len(dataSects)-secIndex) == maxNumSegments {
465 // look for beginning of zeroes
466 for segmentEnd < dataLen && data[segmentEnd] != 0 {
469 // look for end of zeroes
471 for zeroEnd < dataLen && data[zeroEnd] == 0 {
474 // emit segment if omitting zeroes reduces the output size
475 if zeroEnd-segmentEnd >= segmentOverhead || zeroEnd == dataLen {
482 segments = append(segments, &dataSegment{
484 data: data[:segmentEnd],
486 data = data[zeroEnd:]
491 writeUleb128(ctxt.Out, uint64(len(segments))) // number of data entries
492 for _, seg := range segments {
493 writeUleb128(ctxt.Out, 0) // memidx
494 writeI32Const(ctxt.Out, seg.offset)
495 ctxt.Out.WriteByte(0x0b) // end
496 writeUleb128(ctxt.Out, uint64(len(seg.data)))
497 ctxt.Out.Write(seg.data)
500 writeSecSize(ctxt, sizeOffset)
503 // writeProducerSec writes an optional section that reports the source language and compiler version.
504 func writeProducerSec(ctxt *ld.Link) {
505 sizeOffset := writeSecHeader(ctxt, sectionCustom)
506 writeName(ctxt.Out, "producers")
508 writeUleb128(ctxt.Out, 2) // number of fields
510 writeName(ctxt.Out, "language") // field name
511 writeUleb128(ctxt.Out, 1) // number of values
512 writeName(ctxt.Out, "Go") // value: name
513 writeName(ctxt.Out, buildcfg.Version) // value: version
515 writeName(ctxt.Out, "processed-by") // field name
516 writeUleb128(ctxt.Out, 1) // number of values
517 writeName(ctxt.Out, "Go cmd/compile") // value: name
518 writeName(ctxt.Out, buildcfg.Version) // value: version
520 writeSecSize(ctxt, sizeOffset)
523 var nameRegexp = regexp.MustCompile(`[^\w\.]`)
525 // writeNameSec writes an optional section that assigns names to the functions declared by the "func" section.
526 // The names are only used by WebAssembly stack traces, debuggers and decompilers.
527 // TODO(neelance): add symbol table of DATA symbols
528 func writeNameSec(ctxt *ld.Link, firstFnIndex int, fns []*wasmFunc) {
529 sizeOffset := writeSecHeader(ctxt, sectionCustom)
530 writeName(ctxt.Out, "name")
532 sizeOffset2 := writeSecHeader(ctxt, 0x01) // function names
533 writeUleb128(ctxt.Out, uint64(len(fns)))
534 for i, fn := range fns {
535 writeUleb128(ctxt.Out, uint64(firstFnIndex+i))
536 writeName(ctxt.Out, fn.Name)
538 writeSecSize(ctxt, sizeOffset2)
540 writeSecSize(ctxt, sizeOffset)
543 type nameWriter interface {
548 func writeI32Const(w io.ByteWriter, v int32) {
549 w.WriteByte(0x41) // i32.const
550 writeSleb128(w, int64(v))
553 func writeI64Const(w io.ByteWriter, v int64) {
554 w.WriteByte(0x42) // i64.const
558 func writeName(w nameWriter, name string) {
559 writeUleb128(w, uint64(len(name)))
560 w.Write([]byte(name))
563 func writeUleb128(w io.ByteWriter, v uint64) {
565 w.WriteByte(uint8(v))
580 func writeUleb128FixedLength(w io.ByteWriter, v uint64, length int) {
581 for i := 0; i < length; i++ {
590 panic("writeUleb128FixedLength: length too small")
594 func writeSleb128(w io.ByteWriter, v int64) {
600 more = !((v == 0 && s == 0) || (v == -1 && s != 0))