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