Debian Codegraph @

src/cmd/internal/obj/link.go

raw
1 // Derived from Inferno utils/6l/l.h and related files.
2 // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/6l/l.h
3 //
4 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5 // Portions Copyright © 1995-1997 C H Forsyth ([email protected])
6 // Portions Copyright © 1997-1999 Vita Nuova Limited
7 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
8 // Portions Copyright © 2004,2006 Bruce Ellis
9 // Portions Copyright © 2005-2007 C H Forsyth ([email protected])
10 // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
11 // Portions Copyright © 2009 The Go Authors. All rights reserved.
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a copy
14 // of this software and associated documentation files (the "Software"), to deal
15 // in the Software without restriction, including without limitation the rights
16 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 // copies of the Software, and to permit persons to whom the Software is
18 // furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included in
21 // all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 // THE SOFTWARE.
30
31 package obj
32
33 import (
34 "bufio"
35 "bytes"
36 "cmd/internal/dwarf"
37 "cmd/internal/goobj"
38 "cmd/internal/objabi"
39 "cmd/internal/src"
40 "cmd/internal/sys"
41 "encoding/binary"
42 "fmt"
43 "internal/abi"
44 "sync"
45 "sync/atomic"
46 )
47
48 // An Addr is an argument to an instruction.
49 // The general forms and their encodings are:
50 //
51 // sym±offset(symkind)(reg)(index*scale)
52 // Memory reference at address &sym(symkind) + offset + reg + index*scale.
53 // Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted.
54 // If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg).
55 // To force a parsing as index*scale, write (index*1).
56 // Encoding:
57 // type = TYPE_MEM
58 // name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE)
59 // sym = sym
60 // offset = ±offset
61 // reg = reg (REG_*)
62 // index = index (REG_*)
63 // scale = scale (1, 2, 4, 8)
64 //
65 // $<mem>
66 // Effective address of memory reference <mem>, defined above.
67 // Encoding: same as memory reference, but type = TYPE_ADDR.
68 //
69 // $<±integer value>
70 // This is a special case of $<mem>, in which only ±offset is present.
71 // It has a separate type for easy recognition.
72 // Encoding:
73 // type = TYPE_CONST
74 // offset = ±integer value
75 //
76 // *<mem>
77 // Indirect reference through memory reference <mem>, defined above.
78 // Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function
79 // pointer stored in the data word sym(SB), not a function named sym(SB).
80 // Encoding: same as above, but type = TYPE_INDIR.
81 //
82 // $*$<mem>
83 // No longer used.
84 // On machines with actual SB registers, $*$<mem> forced the
85 // instruction encoding to use a full 32-bit constant, never a
86 // reference relative to SB.
87 //
88 // $<floating point literal>
89 // Floating point constant value.
90 // Encoding:
91 // type = TYPE_FCONST
92 // val = floating point value
93 //
94 // $<string literal, up to 8 chars>
95 // String literal value (raw bytes used for DATA instruction).
96 // Encoding:
97 // type = TYPE_SCONST
98 // val = string
99 //
100 // <symbolic constant name>
101 // Special symbolic constants for ARM64 (such as conditional flags, tlbi_op and so on)
102 // and RISCV64 (such as names for vector configuration instruction arguments).
103 // Encoding:
104 // type = TYPE_SPECIAL
105 // offset = The constant value corresponding to this symbol
106 //
107 // <register name>
108 // Any register: integer, floating point, control, segment, and so on.
109 // If looking for specific register kind, must check type and reg value range.
110 // Encoding:
111 // type = TYPE_REG
112 // reg = reg (REG_*)
113 //
114 // x(PC)
115 // Encoding:
116 // type = TYPE_BRANCH
117 // val = Prog* reference OR ELSE offset = target pc (branch takes priority)
118 //
119 // $±x-±y
120 // Final argument to TEXT, specifying local frame size x and argument size y.
121 // In this form, x and y are integer literals only, not arbitrary expressions.
122 // This avoids parsing ambiguities due to the use of - as a separator.
123 // The ± are optional.
124 // If the final argument to TEXT omits the -±y, the encoding should still
125 // use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown.
126 // Encoding:
127 // type = TYPE_TEXTSIZE
128 // offset = x
129 // val = int32(y)
130 //
131 // reg<<shift, reg>>shift, reg->shift, reg@>shift
132 // Shifted register value, for ARM and ARM64.
133 // In this form, reg must be a register and shift can be a register or an integer constant.
134 // Encoding:
135 // type = TYPE_SHIFT
136 // On ARM:
137 // offset = (reg&15) | shifttype<<5 | count
138 // shifttype = 0, 1, 2, 3 for <<, >>, ->, @>
139 // count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant.
140 // On ARM64:
141 // offset = (reg&31)<<16 | shifttype<<22 | (count&63)<<10
142 // shifttype = 0, 1, 2 for <<, >>, ->
143 //
144 // (reg, reg)
145 // A destination register pair. When used as the last argument of an instruction,
146 // this form makes clear that both registers are destinations.
147 // Encoding:
148 // type = TYPE_REGREG
149 // reg = first register
150 // offset = second register
151 //
152 // [reg, reg, reg-reg]
153 // Register list for ARM, ARM64, 386/AMD64.
154 // Encoding:
155 // type = TYPE_REGLIST
156 // On ARM:
157 // offset = bit mask of registers in list; R0 is low bit.
158 // On ARM64:
159 // offset = register count (Q:size) | arrangement (opcode) | first register
160 // On 386/AMD64:
161 // reg = range low register
162 // offset = 2 packed registers + kind tag (see x86.EncodeRegisterRange)
163 //
164 // reg, reg
165 // Register pair for ARM.
166 // TYPE_REGREG2
167 //
168 // (reg+reg)
169 // Register pair for PPC64.
170 // Encoding:
171 // type = TYPE_MEM
172 // reg = first register
173 // index = second register
174 // scale = 1
175 //
176 // reg.[US]XT[BHWX]
177 // Register extension for ARM64
178 // Encoding:
179 // type = TYPE_REG
180 // reg = REG_[US]XT[BHWX] + register + shift amount
181 // offset = ((reg&31) << 16) | (exttype << 13) | (amount<<10)
182 //
183 // reg.<T>
184 // Register arrangement for ARM64 and Loong64 SIMD register
185 // e.g.:
186 // On ARM64: V1.S4, V2.S2, V7.D2, V2.H4, V6.B16
187 // On Loong64: X1.B32, X1.H16, X1.W8, X2.V4, X1.Q1, V1.B16, V1.H8, V1.W4, V1.V2
188 // Encoding:
189 // type = TYPE_REG
190 // reg = REG_ARNG + register + arrangement
191 //
192 // reg.<T>[index]
193 // Register element for ARM64 and Loong64
194 // Encoding:
195 // type = TYPE_REG
196 // reg = REG_ELEM + register + arrangement
197 // index = element index
198
199 type Addr struct {
200 Reg int16
201 Index int16
202 Scale int16 // Sometimes holds a register.
203 Type AddrType
204 Name AddrName
205 Class int8
206 Offset int64
207 Sym *LSym
208
209 // argument value:
210 // for TYPE_SCONST, a string
211 // for TYPE_FCONST, a float64
212 // for TYPE_BRANCH, a *Prog (optional)
213 // for TYPE_TEXTSIZE, an int32 (optional)
214 Val interface{}
215 }
216
217 type AddrName int8
218
219 const (
220 NAME_NONE AddrName = iota
221 NAME_EXTERN
222 NAME_STATIC
223 NAME_AUTO
224 NAME_PARAM
225 // A reference to name@GOT(SB) is a reference to the entry in the global offset
226 // table for 'name'.
227 NAME_GOTREF
228 // Indicates that this is a reference to a TOC anchor.
229 NAME_TOCREF
230 )
231
232 //go:generate stringer -type AddrType
233
234 type AddrType uint8
235
236 const (
237 TYPE_NONE AddrType = iota
238 TYPE_BRANCH
239 TYPE_TEXTSIZE
240 TYPE_MEM
241 TYPE_CONST
242 TYPE_FCONST
243 TYPE_SCONST
244 TYPE_REG
245 TYPE_ADDR
246 TYPE_SHIFT
247 TYPE_REGREG
248 TYPE_REGREG2
249 TYPE_INDIR
250 TYPE_REGLIST
251 TYPE_SPECIAL
252 )
253
254 func (a *Addr) Target() *Prog {
255 if a.Type == TYPE_BRANCH && a.Val != nil {
256 return a.Val.(*Prog)
257 }
258 return nil
259 }
260 func (a *Addr) SetTarget(t *Prog) {
261 if a.Type != TYPE_BRANCH {
262 panic("setting branch target when type is not TYPE_BRANCH")
263 }
264 a.Val = t
265 }
266
267 func (a *Addr) SetConst(v int64) {
268 a.Sym = nil
269 a.Type = TYPE_CONST
270 a.Offset = v
271 }
272
273 // Prog describes a single machine instruction.
274 //
275 // The general instruction form is:
276 //
277 // (1) As.Scond From [, ...RestArgs], To
278 // (2) As.Scond From, Reg [, ...RestArgs], To, RegTo2
279 //
280 // where As is an opcode and the others are arguments:
281 // From, Reg are sources, and To, RegTo2 are destinations.
282 // RestArgs can hold additional sources and destinations.
283 // Usually, not all arguments are present.
284 // For example, MOVL R1, R2 encodes using only As=MOVL, From=R1, To=R2.
285 // The Scond field holds additional condition bits for systems (like arm)
286 // that have generalized conditional execution.
287 // (2) form is present for compatibility with older code,
288 // to avoid too much changes in a single swing.
289 // (1) scheme is enough to express any kind of operand combination.
290 //
291 // Jump instructions use the To.Val field to point to the target *Prog,
292 // which must be in the same linked list as the jump instruction.
293 //
294 // The Progs for a given function are arranged in a list linked through the Link field.
295 //
296 // Each Prog is charged to a specific source line in the debug information,
297 // specified by Pos.Line().
298 // Every Prog has a Ctxt field that defines its context.
299 // For performance reasons, Progs are usually bulk allocated, cached, and reused;
300 // those bulk allocators should always be used, rather than new(Prog).
301 //
302 // The other fields not yet mentioned are for use by the back ends and should
303 // be left zeroed by creators of Prog lists.
304 type Prog struct {
305 Ctxt *Link // linker context
306 Link *Prog // next Prog in linked list
307 From Addr // first source operand
308 RestArgs []AddrPos // can pack any operands that not fit into {Prog.From, Prog.To}, same kinds of operands are saved in order
309 To Addr // destination operand (second is RegTo2 below)
310 Pool *Prog // constant pool entry, for arm,arm64 back ends
311 Forwd *Prog // for x86 back end
312 Rel *Prog // for x86, arm back ends
313 Pc int64 // for back ends or assembler: virtual or actual program counter, depending on phase
314 Pos src.XPos // source position of this instruction
315 Spadj int32 // effect of instruction on stack pointer (increment or decrement amount)
316 As As // assembler opcode
317 Reg int16 // 2nd source operand
318 RegTo2 int16 // 2nd destination operand
319 Mark uint16 // bitmask of arch-specific items
320 Optab uint16 // arch-specific opcode index
321 Scond uint8 // bits that describe instruction suffixes (e.g. ARM conditions, RISCV Rounding Mode)
322 Back uint8 // for x86 back end: backwards branch state
323 Ft uint8 // for x86 back end: type index of Prog.From
324 Tt uint8 // for x86 back end: type index of Prog.To
325 Isize uint8 // for x86 back end: size of the instruction in bytes
326 }
327
328 // AddrPos indicates whether the operand is the source or the destination.
329 type AddrPos struct {
330 Addr
331 Pos OperandPos
332 }
333
334 type OperandPos int8
335
336 const (
337 Source OperandPos = iota
338 Destination
339 )
340
341 // From3Type returns p.GetFrom3().Type, or TYPE_NONE when
342 // p.GetFrom3() returns nil.
343 func (p *Prog) From3Type() AddrType {
344 from3 := p.GetFrom3()
345 if from3 == nil {
346 return TYPE_NONE
347 }
348 return from3.Type
349 }
350
351 // GetFrom3 returns second source operand (the first is Prog.From).
352 // The same kinds of operands are saved in order so GetFrom3 actually
353 // return the first source operand in p.RestArgs.
354 // In combination with Prog.From and Prog.To it makes common 3 operand
355 // case easier to use.
356 func (p *Prog) GetFrom3() *Addr {
357 for i := range p.RestArgs {
358 if p.RestArgs[i].Pos == Source {
359 return &p.RestArgs[i].Addr
360 }
361 }
362 return nil
363 }
364
365 // AddRestSource assigns []Args{{a, Source}} to p.RestArgs.
366 func (p *Prog) AddRestSource(a Addr) {
367 p.RestArgs = append(p.RestArgs, AddrPos{a, Source})
368 }
369
370 // AddRestSourceReg calls p.AddRestSource with a register Addr containing reg.
371 func (p *Prog) AddRestSourceReg(reg int16) {
372 p.AddRestSource(Addr{Type: TYPE_REG, Reg: reg})
373 }
374
375 // AddRestSourceConst calls p.AddRestSource with a const Addr containing off.
376 func (p *Prog) AddRestSourceConst(off int64) {
377 p.AddRestSource(Addr{Type: TYPE_CONST, Offset: off})
378 }
379
380 // AddRestDest assigns []Args{{a, Destination}} to p.RestArgs when the second destination
381 // operand does not fit into prog.RegTo2.
382 func (p *Prog) AddRestDest(a Addr) {
383 p.RestArgs = append(p.RestArgs, AddrPos{a, Destination})
384 }
385
386 // GetTo2 returns the second destination operand.
387 // The same kinds of operands are saved in order so GetTo2 actually
388 // return the first destination operand in Prog.RestArgs[]
389 func (p *Prog) GetTo2() *Addr {
390 for i := range p.RestArgs {
391 if p.RestArgs[i].Pos == Destination {
392 return &p.RestArgs[i].Addr
393 }
394 }
395 return nil
396 }
397
398 // AddRestSourceArgs assigns more than one source operands to p.RestArgs.
399 func (p *Prog) AddRestSourceArgs(args []Addr) {
400 for i := range args {
401 p.RestArgs = append(p.RestArgs, AddrPos{args[i], Source})
402 }
403 }
404
405 // An As denotes an assembler opcode.
406 // There are some portable opcodes, declared here in package obj,
407 // that are common to all architectures.
408 // However, the majority of opcodes are arch-specific
409 // and are declared in their respective architecture's subpackage.
410 type As int16
411
412 // These are the portable opcodes.
413 const (
414 AXXX As = iota
415 ACALL
416 ADUFFCOPY
417 ADUFFZERO
418 AEND
419 AFUNCDATA
420 AJMP
421 ANOP
422 APCALIGN
423 APCALIGNMAX // currently x86, amd64 and arm64
424 APCDATA
425 ARET
426 AGETCALLERPC
427 ATEXT
428 AUNDEF
429 A_ARCHSPECIFIC
430 )
431
432 // Each architecture is allotted a distinct subspace of opcode values
433 // for declaring its arch-specific opcodes.
434 // Within this subspace, the first arch-specific opcode should be
435 // at offset A_ARCHSPECIFIC.
436 //
437 // Subspaces are aligned to a power of two so opcodes can be masked
438 // with AMask and used as compact array indices.
439 const (
440 ABase386 = (1 + iota) << 11
441 ABaseARM
442 ABaseAMD64
443 ABasePPC64
444 ABaseARM64
445 ABaseMIPS
446 ABaseLoong64
447 ABaseRISCV
448 ABaseS390X
449 ABaseWasm
450
451 AllowedOpCodes = 1 << 11 // The number of opcodes available for any given architecture.
452 AMask = AllowedOpCodes - 1 // AND with this to use the opcode as an array index.
453 )
454
455 // An LSym is the sort of symbol that is written to an object file.
456 // It represents Go symbols in a flat pkg+"."+name namespace.
457 type LSym struct {
458 Name string
459 Type objabi.SymKind
460 Attribute
461
462 Size int64
463 Gotype *LSym
464 P []byte
465 R []Reloc
466
467 Extra *interface{} // *FuncInfo, *VarInfo, *FileInfo, or *TypeInfo, if present
468
469 Pkg string
470 PkgIdx int32
471 SymIdx int32
472 }
473
474 // A FuncInfo contains extra fields for STEXT symbols.
475 type FuncInfo struct {
476 Args int32
477 Locals int32
478 Align int32
479 FuncID abi.FuncID
480 FuncFlag abi.FuncFlag
481 StartLine int32
482 Text *Prog
483 Autot map[*LSym]struct{}
484 Pcln Pcln
485 InlMarks []InlMark
486 spills []RegSpill
487
488 dwarfInfoSym *LSym
489 dwarfLocSym *LSym
490 dwarfRangesSym *LSym
491 dwarfAbsFnSym *LSym
492 dwarfDebugLinesSym *LSym
493
494 GCArgs *LSym
495 GCLocals *LSym
496 StackObjects *LSym
497 OpenCodedDeferInfo *LSym
498 ArgInfo *LSym // argument info for traceback
499 ArgLiveInfo *LSym // argument liveness info for traceback
500 WrapInfo *LSym // for wrapper, info of wrapped function
501 JumpTables []JumpTable
502
503 FuncInfoSym *LSym
504
505 WasmImport *WasmImport
506 WasmExport *WasmExport
507
508 sehUnwindInfoSym *LSym
509 }
510
511 // JumpTable represents a table used for implementing multi-way
512 // computed branching, used typically for implementing switches.
513 // Sym is the table itself, and Targets is a list of target
514 // instructions to go to for the computed branch index.
515 type JumpTable struct {
516 Sym *LSym
517 Targets []*Prog
518 }
519
520 // NewFuncInfo allocates and returns a FuncInfo for LSym.
521 func (s *LSym) NewFuncInfo() *FuncInfo {
522 if s.Extra != nil {
523 panic(fmt.Sprintf("invalid use of LSym - NewFuncInfo with Extra of type %T", *s.Extra))
524 }
525 f := new(FuncInfo)
526 s.Extra = new(interface{})
527 *s.Extra = f
528 return f
529 }
530
531 // Func returns the *FuncInfo associated with s, or else nil.
532 func (s *LSym) Func() *FuncInfo {
533 if s.Extra == nil {
534 return nil
535 }
536 f, _ := (*s.Extra).(*FuncInfo)
537 return f
538 }
539
540 type VarInfo struct {
541 dwarfInfoSym *LSym
542 }
543
544 // NewVarInfo allocates and returns a VarInfo for LSym.
545 func (s *LSym) NewVarInfo() *VarInfo {
546 if s.Extra != nil {
547 panic(fmt.Sprintf("invalid use of LSym - NewVarInfo with Extra of type %T", *s.Extra))
548 }
549 f := new(VarInfo)
550 s.Extra = new(interface{})
551 *s.Extra = f
552 return f
553 }
554
555 // VarInfo returns the *VarInfo associated with s, or else nil.
556 func (s *LSym) VarInfo() *VarInfo {
557 if s.Extra == nil {
558 return nil
559 }
560 f, _ := (*s.Extra).(*VarInfo)
561 return f
562 }
563
564 // A FileInfo contains extra fields for SDATA symbols backed by files.
565 // (If LSym.Extra is a *FileInfo, LSym.P == nil.)
566 type FileInfo struct {
567 Name string // name of file to read into object file
568 Size int64 // length of file
569 }
570
571 // NewFileInfo allocates and returns a FileInfo for LSym.
572 func (s *LSym) NewFileInfo() *FileInfo {
573 if s.Extra != nil {
574 panic(fmt.Sprintf("invalid use of LSym - NewFileInfo with Extra of type %T", *s.Extra))
575 }
576 f := new(FileInfo)
577 s.Extra = new(interface{})
578 *s.Extra = f
579 return f
580 }
581
582 // File returns the *FileInfo associated with s, or else nil.
583 func (s *LSym) File() *FileInfo {
584 if s.Extra == nil {
585 return nil
586 }
587 f, _ := (*s.Extra).(*FileInfo)
588 return f
589 }
590
591 // A TypeInfo contains information for a symbol
592 // that contains a runtime._type.
593 type TypeInfo struct {
594 Type interface{} // a *cmd/compile/internal/types.Type
595 }
596
597 func (s *LSym) NewTypeInfo() *TypeInfo {
598 if s.Extra != nil {
599 panic(fmt.Sprintf("invalid use of LSym - NewTypeInfo with Extra of type %T", *s.Extra))
600 }
601 t := new(TypeInfo)
602 s.Extra = new(interface{})
603 *s.Extra = t
604 return t
605 }
606
607 // An ItabInfo contains information for a symbol
608 // that contains a runtime.itab.
609 type ItabInfo struct {
610 Type interface{} // a *cmd/compile/internal/types.Type
611 }
612
613 func (s *LSym) NewItabInfo() *ItabInfo {
614 if s.Extra != nil {
615 panic(fmt.Sprintf("invalid use of LSym - NewItabInfo with Extra of type %T", *s.Extra))
616 }
617 t := new(ItabInfo)
618 s.Extra = new(interface{})
619 *s.Extra = t
620 return t
621 }
622
623 // WasmImport represents a WebAssembly (WASM) imported function with
624 // parameters and results translated into WASM types based on the Go function
625 // declaration.
626 type WasmImport struct {
627 // Module holds the WASM module name specified by the //go:wasmimport
628 // directive.
629 Module string
630 // Name holds the WASM imported function name specified by the
631 // //go:wasmimport directive.
632 Name string
633
634 WasmFuncType // type of the imported function
635
636 // aux symbol to pass metadata to the linker, serialization of
637 // the fields above.
638 AuxSym *LSym
639 }
640
641 func (wi *WasmImport) CreateAuxSym() {
642 var b bytes.Buffer
643 wi.Write(&b)
644 p := b.Bytes()
645 wi.AuxSym = &LSym{
646 Type: objabi.SDATA, // doesn't really matter
647 P: append([]byte(nil), p...),
648 Size: int64(len(p)),
649 }
650 }
651
652 func (wi *WasmImport) Write(w *bytes.Buffer) {
653 var b [8]byte
654 writeUint32 := func(x uint32) {
655 binary.LittleEndian.PutUint32(b[:], x)
656 w.Write(b[:4])
657 }
658 writeString := func(s string) {
659 writeUint32(uint32(len(s)))
660 w.WriteString(s)
661 }
662 writeString(wi.Module)
663 writeString(wi.Name)
664 wi.WasmFuncType.Write(w)
665 }
666
667 func (wi *WasmImport) Read(b []byte) {
668 readUint32 := func() uint32 {
669 x := binary.LittleEndian.Uint32(b)
670 b = b[4:]
671 return x
672 }
673 readString := func() string {
674 n := readUint32()
675 s := string(b[:n])
676 b = b[n:]
677 return s
678 }
679 wi.Module = readString()
680 wi.Name = readString()
681 wi.WasmFuncType.Read(b)
682 }
683
684 // WasmFuncType represents a WebAssembly (WASM) function type with
685 // parameters and results translated into WASM types based on the Go function
686 // declaration.
687 type WasmFuncType struct {
688 // Params holds the function parameter fields.
689 Params []WasmField
690 // Results holds the function result fields.
691 Results []WasmField
692 }
693
694 func (ft *WasmFuncType) Write(w *bytes.Buffer) {
695 var b [8]byte
696 writeByte := func(x byte) {
697 w.WriteByte(x)
698 }
699 writeUint32 := func(x uint32) {
700 binary.LittleEndian.PutUint32(b[:], x)
701 w.Write(b[:4])
702 }
703 writeInt64 := func(x int64) {
704 binary.LittleEndian.PutUint64(b[:], uint64(x))
705 w.Write(b[:])
706 }
707 writeUint32(uint32(len(ft.Params)))
708 for _, f := range ft.Params {
709 writeByte(byte(f.Type))
710 writeInt64(f.Offset)
711 }
712 writeUint32(uint32(len(ft.Results)))
713 for _, f := range ft.Results {
714 writeByte(byte(f.Type))
715 writeInt64(f.Offset)
716 }
717 }
718
719 func (ft *WasmFuncType) Read(b []byte) {
720 readByte := func() byte {
721 x := b[0]
722 b = b[1:]
723 return x
724 }
725 readUint32 := func() uint32 {
726 x := binary.LittleEndian.Uint32(b)
727 b = b[4:]
728 return x
729 }
730 readInt64 := func() int64 {
731 x := binary.LittleEndian.Uint64(b)
732 b = b[8:]
733 return int64(x)
734 }
735 ft.Params = make([]WasmField, readUint32())
736 for i := range ft.Params {
737 ft.Params[i].Type = WasmFieldType(readByte())
738 ft.Params[i].Offset = int64(readInt64())
739 }
740 ft.Results = make([]WasmField, readUint32())
741 for i := range ft.Results {
742 ft.Results[i].Type = WasmFieldType(readByte())
743 ft.Results[i].Offset = int64(readInt64())
744 }
745 }
746
747 // WasmExport represents a WebAssembly (WASM) exported function with
748 // parameters and results translated into WASM types based on the Go function
749 // declaration.
750 type WasmExport struct {
751 WasmFuncType
752
753 WrappedSym *LSym // the wrapped Go function
754 AuxSym *LSym // aux symbol to pass metadata to the linker
755 }
756
757 func (we *WasmExport) CreateAuxSym() {
758 var b bytes.Buffer
759 we.WasmFuncType.Write(&b)
760 p := b.Bytes()
761 we.AuxSym = &LSym{
762 Type: objabi.SDATA, // doesn't really matter
763 P: append([]byte(nil), p...),
764 Size: int64(len(p)),
765 }
766 }
767
768 type WasmField struct {
769 Type WasmFieldType
770 // Offset holds the frame-pointer-relative locations for Go's stack-based
771 // ABI. This is used by the src/cmd/internal/wasm package to map WASM
772 // import parameters to the Go stack in a wrapper function.
773 Offset int64
774 }
775
776 type WasmFieldType byte
777
778 const (
779 WasmI32 WasmFieldType = iota
780 WasmI64
781 WasmF32
782 WasmF64
783 WasmPtr
784
785 // bool is not really a wasm type, but we allow it on wasmimport/wasmexport
786 // function parameters/results. 32-bit on Wasm side, 8-bit on Go side.
787 WasmBool
788 )
789
790 type InlMark struct {
791 // When unwinding from an instruction in an inlined body, mark
792 // where we should unwind to.
793 // id records the global inlining id of the inlined body.
794 // p records the location of an instruction in the parent (inliner) frame.
795 p *Prog
796 id int32
797 }
798
799 // Mark p as the instruction to set as the pc when
800 // "unwinding" the inlining global frame id. Usually it should be
801 // instruction with a file:line at the callsite, and occur
802 // just before the body of the inlined function.
803 func (fi *FuncInfo) AddInlMark(p *Prog, id int32) {
804 fi.InlMarks = append(fi.InlMarks, InlMark{p: p, id: id})
805 }
806
807 // AddSpill appends a spill record to the list for FuncInfo fi
808 func (fi *FuncInfo) AddSpill(s RegSpill) {
809 fi.spills = append(fi.spills, s)
810 }
811
812 // Record the type symbol for an auto variable so that the linker
813 // an emit DWARF type information for the type.
814 func (fi *FuncInfo) RecordAutoType(gotype *LSym) {
815 if fi.Autot == nil {
816 fi.Autot = make(map[*LSym]struct{})
817 }
818 fi.Autot[gotype] = struct{}{}
819 }
820
821 //go:generate stringer -type ABI
822
823 // ABI is the calling convention of a text symbol.
824 type ABI uint8
825
826 const (
827 // ABI0 is the stable stack-based ABI. It's important that the
828 // value of this is "0": we can't distinguish between
829 // references to data and ABI0 text symbols in assembly code,
830 // and hence this doesn't distinguish between symbols without
831 // an ABI and text symbols with ABI0.
832 ABI0 ABI = iota
833
834 // ABIInternal is the internal ABI that may change between Go
835 // versions. All Go functions use the internal ABI and the
836 // compiler generates wrappers for calls to and from other
837 // ABIs.
838 ABIInternal
839
840 ABICount
841 )
842
843 // ParseABI converts from a string representation in 'abistr' to the
844 // corresponding ABI value. Second return value is TRUE if the
845 // abi string is recognized, FALSE otherwise.
846 func ParseABI(abistr string) (ABI, bool) {
847 switch abistr {
848 default:
849 return ABI0, false
850 case "ABI0":
851 return ABI0, true
852 case "ABIInternal":
853 return ABIInternal, true
854 }
855 }
856
857 // ABISet is a bit set of ABI values.
858 type ABISet uint8
859
860 const (
861 // ABISetCallable is the set of all ABIs any function could
862 // potentially be called using.
863 ABISetCallable ABISet = (1 << ABI0) | (1 << ABIInternal)
864 )
865
866 // Ensure ABISet is big enough to hold all ABIs.
867 var _ ABISet = 1 << (ABICount - 1)
868
869 func ABISetOf(abi ABI) ABISet {
870 return 1 << abi
871 }
872
873 func (a *ABISet) Set(abi ABI, value bool) {
874 if value {
875 *a |= 1 << abi
876 } else {
877 *a &^= 1 << abi
878 }
879 }
880
881 func (a *ABISet) Get(abi ABI) bool {
882 return (*a>>abi)&1 != 0
883 }
884
885 func (a ABISet) String() string {
886 s := "{"
887 for i := ABI(0); a != 0; i++ {
888 if a&(1<<i) != 0 {
889 if s != "{" {
890 s += ","
891 }
892 s += i.String()
893 a &^= 1 << i
894 }
895 }
896 return s + "}"
897 }
898
899 // Attribute is a set of symbol attributes.
900 type Attribute uint32
901
902 const (
903 AttrDuplicateOK Attribute = 1 << iota
904 AttrCFunc
905 AttrNoSplit
906 AttrLeaf
907 AttrWrapper
908 AttrNeedCtxt
909 AttrNoFrame
910 AttrOnList
911 AttrStatic
912
913 // MakeTypelink means that the type should have an entry in the typelink table.
914 AttrMakeTypelink
915
916 // ReflectMethod means the function may call reflect.Type.Method or
917 // reflect.Type.MethodByName. Matching is imprecise (as reflect.Type
918 // can be used through a custom interface), so ReflectMethod may be
919 // set in some cases when the reflect package is not called.
920 //
921 // Used by the linker to determine what methods can be pruned.
922 AttrReflectMethod
923
924 // Local means make the symbol local even when compiling Go code to reference Go
925 // symbols in other shared libraries, as in this mode symbols are global by
926 // default. "local" here means in the sense of the dynamic linker, i.e. not
927 // visible outside of the module (shared library or executable) that contains its
928 // definition. (When not compiling to support Go shared libraries, all symbols are
929 // local in this sense unless there is a cgo_export_* directive).
930 AttrLocal
931
932 // For function symbols; indicates that the specified function was the
933 // target of an inline during compilation
934 AttrWasInlined
935
936 // Indexed indicates this symbol has been assigned with an index (when using the
937 // new object file format).
938 AttrIndexed
939
940 // Only applied on type descriptor symbols, UsedInIface indicates this type is
941 // converted to an interface.
942 //
943 // Used by the linker to determine what methods can be pruned.
944 AttrUsedInIface
945
946 // ContentAddressable indicates this is a content-addressable symbol.
947 AttrContentAddressable
948
949 // ABI wrapper is set for compiler-generated text symbols that
950 // convert between ABI0 and ABIInternal calling conventions.
951 AttrABIWrapper
952
953 // IsPcdata indicates this is a pcdata symbol.
954 AttrPcdata
955
956 // PkgInit indicates this is a compiler-generated package init func.
957 AttrPkgInit
958
959 // Linkname indicates this is a go:linkname'd symbol.
960 AttrLinkname
961
962 // attrABIBase is the value at which the ABI is encoded in
963 // Attribute. This must be last; all bits after this are
964 // assumed to be an ABI value.
965 //
966 // MUST BE LAST since all bits above this comprise the ABI.
967 attrABIBase
968 )
969
970 func (a *Attribute) load() Attribute { return Attribute(atomic.LoadUint32((*uint32)(a))) }
971
972 func (a *Attribute) DuplicateOK() bool { return a.load()&AttrDuplicateOK != 0 }
973 func (a *Attribute) MakeTypelink() bool { return a.load()&AttrMakeTypelink != 0 }
974 func (a *Attribute) CFunc() bool { return a.load()&AttrCFunc != 0 }
975 func (a *Attribute) NoSplit() bool { return a.load()&AttrNoSplit != 0 }
976 func (a *Attribute) Leaf() bool { return a.load()&AttrLeaf != 0 }
977 func (a *Attribute) OnList() bool { return a.load()&AttrOnList != 0 }
978 func (a *Attribute) ReflectMethod() bool { return a.load()&AttrReflectMethod != 0 }
979 func (a *Attribute) Local() bool { return a.load()&AttrLocal != 0 }
980 func (a *Attribute) Wrapper() bool { return a.load()&AttrWrapper != 0 }
981 func (a *Attribute) NeedCtxt() bool { return a.load()&AttrNeedCtxt != 0 }
982 func (a *Attribute) NoFrame() bool { return a.load()&AttrNoFrame != 0 }
983 func (a *Attribute) Static() bool { return a.load()&AttrStatic != 0 }
984 func (a *Attribute) WasInlined() bool { return a.load()&AttrWasInlined != 0 }
985 func (a *Attribute) Indexed() bool { return a.load()&AttrIndexed != 0 }
986 func (a *Attribute) UsedInIface() bool { return a.load()&AttrUsedInIface != 0 }
987 func (a *Attribute) ContentAddressable() bool { return a.load()&AttrContentAddressable != 0 }
988 func (a *Attribute) ABIWrapper() bool { return a.load()&AttrABIWrapper != 0 }
989 func (a *Attribute) IsPcdata() bool { return a.load()&AttrPcdata != 0 }
990 func (a *Attribute) IsPkgInit() bool { return a.load()&AttrPkgInit != 0 }
991 func (a *Attribute) IsLinkname() bool { return a.load()&AttrLinkname != 0 }
992
993 func (a *Attribute) Set(flag Attribute, value bool) {
994 for {
995 v0 := a.load()
996 v := v0
997 if value {
998 v |= flag
999 } else {
1000 v &^= flag
1001 }
1002 if atomic.CompareAndSwapUint32((*uint32)(a), uint32(v0), uint32(v)) {
1003 break
1004 }
1005 }
1006 }
1007
1008 func (a *Attribute) ABI() ABI { return ABI(a.load() / attrABIBase) }
1009 func (a *Attribute) SetABI(abi ABI) {
1010 const mask = 1 // Only one ABI bit for now.
1011 for {
1012 v0 := a.load()
1013 v := (v0 &^ (mask * attrABIBase)) | Attribute(abi)*attrABIBase
1014 if atomic.CompareAndSwapUint32((*uint32)(a), uint32(v0), uint32(v)) {
1015 break
1016 }
1017 }
1018 }
1019
1020 var textAttrStrings = [...]struct {
1021 bit Attribute
1022 s string
1023 }{
1024 {bit: AttrDuplicateOK, s: "DUPOK"},
1025 {bit: AttrMakeTypelink, s: ""},
1026 {bit: AttrCFunc, s: "CFUNC"},
1027 {bit: AttrNoSplit, s: "NOSPLIT"},
1028 {bit: AttrLeaf, s: "LEAF"},
1029 {bit: AttrOnList, s: ""},
1030 {bit: AttrReflectMethod, s: "REFLECTMETHOD"},
1031 {bit: AttrLocal, s: "LOCAL"},
1032 {bit: AttrWrapper, s: "WRAPPER"},
1033 {bit: AttrNeedCtxt, s: "NEEDCTXT"},
1034 {bit: AttrNoFrame, s: "NOFRAME"},
1035 {bit: AttrStatic, s: "STATIC"},
1036 {bit: AttrWasInlined, s: ""},
1037 {bit: AttrIndexed, s: ""},
1038 {bit: AttrContentAddressable, s: ""},
1039 {bit: AttrABIWrapper, s: "ABIWRAPPER"},
1040 {bit: AttrPkgInit, s: "PKGINIT"},
1041 {bit: AttrLinkname, s: "LINKNAME"},
1042 }
1043
1044 // String formats a for printing in as part of a TEXT prog.
1045 func (a Attribute) String() string {
1046 var s string
1047 for _, x := range textAttrStrings {
1048 if a&x.bit != 0 {
1049 if x.s != "" {
1050 s += x.s + "|"
1051 }
1052 a &^= x.bit
1053 }
1054 }
1055 switch a.ABI() {
1056 case ABI0:
1057 case ABIInternal:
1058 s += "ABIInternal|"
1059 a.SetABI(0) // Clear ABI so we don't print below.
1060 }
1061 if a != 0 {
1062 s += fmt.Sprintf("UnknownAttribute(%d)|", a)
1063 }
1064 // Chop off trailing |, if present.
1065 if len(s) > 0 {
1066 s = s[:len(s)-1]
1067 }
1068 return s
1069 }
1070
1071 // TextAttrString formats the symbol attributes for printing in as part of a TEXT prog.
1072 func (s *LSym) TextAttrString() string {
1073 attr := s.Attribute.String()
1074 if s.Func().FuncFlag&abi.FuncFlagTopFrame != 0 {
1075 if attr != "" {
1076 attr += "|"
1077 }
1078 attr += "TOPFRAME"
1079 }
1080 return attr
1081 }
1082
1083 func (s *LSym) String() string {
1084 return s.Name
1085 }
1086
1087 // The compiler needs *LSym to be assignable to cmd/compile/internal/ssa.Sym.
1088 func (*LSym) CanBeAnSSASym() {}
1089 func (*LSym) CanBeAnSSAAux() {}
1090
1091 type Pcln struct {
1092 // Aux symbols for pcln
1093 Pcsp *LSym
1094 Pcfile *LSym
1095 Pcline *LSym
1096 Pcinline *LSym
1097 Pcdata []*LSym
1098 Funcdata []*LSym
1099 UsedFiles map[goobj.CUFileIndex]struct{} // file indices used while generating pcfile
1100 InlTree InlTree // per-function inlining tree extracted from the global tree
1101 }
1102
1103 type Reloc struct {
1104 Off int32
1105 Siz uint8
1106 Type objabi.RelocType
1107 Add int64
1108 Sym *LSym
1109 }
1110
1111 type Auto struct {
1112 Asym *LSym
1113 Aoffset int32
1114 Name AddrName
1115 Gotype *LSym
1116 }
1117
1118 // RegSpill provides spill/fill information for a register-resident argument
1119 // to a function. These need spilling/filling in the safepoint/stackgrowth case.
1120 // At the time of fill/spill, the offset must be adjusted by the architecture-dependent
1121 // adjustment to hardware SP that occurs in a call instruction. E.g., for AMD64,
1122 // at Offset+8 because the return address was pushed.
1123 type RegSpill struct {
1124 Addr Addr
1125 Reg int16
1126 Reg2 int16 // If not 0, a second register to spill at Addr+regSize. Only for some archs.
1127 Spill, Unspill As
1128 }
1129
1130 // A Func represents a Go function. If non-nil, it must be a *ir.Func.
1131 type Func interface {
1132 Pos() src.XPos
1133 }
1134
1135 // Link holds the context for writing object code from a compiler
1136 // to be linker input or for reading that input into the linker.
1137 type Link struct {
1138 Headtype objabi.HeadType
1139 Arch *LinkArch
1140 Debugasm int
1141 Debugvlog bool
1142 Debugpcln string
1143 Flag_shared bool
1144 Flag_dynlink bool
1145 Flag_linkshared bool
1146 Flag_optimize bool
1147 Flag_locationlists bool
1148 Flag_noRefName bool // do not include referenced symbol names in object file
1149 Retpoline bool // emit use of retpoline stubs for indirect jmp/call
1150 Flag_maymorestack string // If not "", call this function before stack checks
1151 Bso *bufio.Writer
1152 Pathname string
1153 Pkgpath string // the current package's import path
1154 hashmu sync.Mutex // protects hash, funchash
1155 hash map[string]*LSym // name -> sym mapping
1156 funchash map[string]*LSym // name -> sym mapping for ABIInternal syms
1157 statichash map[string]*LSym // name -> sym mapping for static syms
1158 PosTable src.PosTable
1159 InlTree InlTree // global inlining tree used by gc/inl.go
1160 DwFixups *DwarfFixupTable
1161 DwTextCount int
1162 Imports []goobj.ImportedPkg
1163 DiagFunc func(string, ...interface{})
1164 DiagFlush func()
1165 DebugInfo func(ctxt *Link, fn *LSym, info *LSym, curfn Func) ([]dwarf.Scope, dwarf.InlCalls)
1166 GenAbstractFunc func(fn *LSym)
1167 Errors int
1168
1169 InParallel bool // parallel backend phase in effect
1170 UseBASEntries bool // use Base Address Selection Entries in location lists and PC ranges
1171 IsAsm bool // is the source assembly language, which may contain surprising idioms (e.g., call tables)
1172 Std bool // is standard library package
1173
1174 // state for writing objects
1175 Text []*LSym
1176 Data []*LSym
1177
1178 // Constant symbols (e.g. $i64.*) are data symbols created late
1179 // in the concurrent phase. To ensure a deterministic order, we
1180 // add them to a separate list, sort at the end, and append it
1181 // to Data.
1182 constSyms []*LSym
1183
1184 // Windows SEH symbols are also data symbols that can be created
1185 // concurrently.
1186 SEHSyms []*LSym
1187
1188 // pkgIdx maps package path to index. The index is used for
1189 // symbol reference in the object file.
1190 pkgIdx map[string]int32
1191
1192 defs []*LSym // list of defined symbols in the current package
1193 hashed64defs []*LSym // list of defined short (64-bit or less) hashed (content-addressable) symbols
1194 hasheddefs []*LSym // list of defined hashed (content-addressable) symbols
1195 nonpkgdefs []*LSym // list of defined non-package symbols
1196 nonpkgrefs []*LSym // list of referenced non-package symbols
1197
1198 Fingerprint goobj.FingerprintType // fingerprint of symbol indices, to catch index mismatch
1199 }
1200
1201 func (ctxt *Link) Diag(format string, args ...interface{}) {
1202 ctxt.Errors++
1203 ctxt.DiagFunc(format, args...)
1204 }
1205
1206 func (ctxt *Link) Logf(format string, args ...interface{}) {
1207 fmt.Fprintf(ctxt.Bso, format, args...)
1208 ctxt.Bso.Flush()
1209 }
1210
1211 // SpillRegisterArgs emits the code to spill register args into whatever
1212 // locations the spill records specify.
1213 func (fi *FuncInfo) SpillRegisterArgs(last *Prog, pa ProgAlloc) *Prog {
1214 // Spill register args.
1215 for _, ra := range fi.spills {
1216 spill := Appendp(last, pa)
1217 spill.As = ra.Spill
1218 spill.From.Type = TYPE_REG
1219 spill.From.Reg = ra.Reg
1220 if ra.Reg2 != 0 {
1221 spill.From.Type = TYPE_REGREG
1222 spill.From.Offset = int64(ra.Reg2)
1223 }
1224 spill.To = ra.Addr
1225 last = spill
1226 }
1227 return last
1228 }
1229
1230 // UnspillRegisterArgs emits the code to restore register args from whatever
1231 // locations the spill records specify.
1232 func (fi *FuncInfo) UnspillRegisterArgs(last *Prog, pa ProgAlloc) *Prog {
1233 // Unspill any spilled register args
1234 for _, ra := range fi.spills {
1235 unspill := Appendp(last, pa)
1236 unspill.As = ra.Unspill
1237 unspill.From = ra.Addr
1238 unspill.To.Type = TYPE_REG
1239 unspill.To.Reg = ra.Reg
1240 if ra.Reg2 != 0 {
1241 unspill.To.Type = TYPE_REGREG
1242 unspill.To.Offset = int64(ra.Reg2)
1243 }
1244 last = unspill
1245 }
1246 return last
1247 }
1248
1249 // LinkArch is the definition of a single architecture.
1250 type LinkArch struct {
1251 *sys.Arch
1252 Init func(*Link)
1253 ErrorCheck func(*Link, *LSym)
1254 Preprocess func(*Link, *LSym, ProgAlloc)
1255 Assemble func(*Link, *LSym, ProgAlloc)
1256 Progedit func(*Link, *Prog, ProgAlloc)
1257 SEH func(*Link, *LSym) *LSym
1258 UnaryDst map[As]bool // Instruction takes one operand, a destination.
1259 DWARFRegisters map[int16]int16
1260 }