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