github.com/bir3/gocompiler@v0.9.2202/src/cmd/internal/obj/link.go (about)

     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  
    31  package obj
    32  
    33  import (
    34  	"bufio"
    35  	"github.com/bir3/gocompiler/src/cmd/internal/dwarf"
    36  	"github.com/bir3/gocompiler/src/cmd/internal/goobj"
    37  	"github.com/bir3/gocompiler/src/cmd/internal/objabi"
    38  	"github.com/bir3/gocompiler/src/cmd/internal/src"
    39  	"github.com/bir3/gocompiler/src/cmd/internal/sys"
    40  	"encoding/binary"
    41  	"fmt"
    42  	"github.com/bir3/gocompiler/src/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  
   195  type 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  
   213  type AddrName int8
   214  
   215  const (
   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  
   230  type AddrType uint8
   231  
   232  const (
   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  
   250  func (a *Addr) Target() *Prog {
   251  	if a.Type == TYPE_BRANCH && a.Val != nil {
   252  		return a.Val.(*Prog)
   253  	}
   254  	return nil
   255  }
   256  func (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  
   263  func (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.
   300  type 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)
   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.
   325  type AddrPos struct {
   326  	Addr
   327  	Pos	OperandPos
   328  }
   329  
   330  type OperandPos int8
   331  
   332  const (
   333  	Source	OperandPos	= iota
   334  	Destination
   335  )
   336  
   337  // From3Type returns p.GetFrom3().Type, or TYPE_NONE when
   338  // p.GetFrom3() returns nil.
   339  func (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.
   352  func (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.
   362  func (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.
   367  func (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.
   372  func (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.
   378  func (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[]
   385  func (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.
   395  func (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.
   406  type As int16
   407  
   408  // These are the portable opcodes.
   409  const (
   410  	AXXX	As	= iota
   411  	ACALL
   412  	ADUFFCOPY
   413  	ADUFFZERO
   414  	AEND
   415  	AFUNCDATA
   416  	AJMP
   417  	ANOP
   418  	APCALIGN
   419  	APCDATA
   420  	ARET
   421  	AGETCALLERPC
   422  	ATEXT
   423  	AUNDEF
   424  	A_ARCHSPECIFIC
   425  )
   426  
   427  // Each architecture is allotted a distinct subspace of opcode values
   428  // for declaring its arch-specific opcodes.
   429  // Within this subspace, the first arch-specific opcode should be
   430  // at offset A_ARCHSPECIFIC.
   431  //
   432  // Subspaces are aligned to a power of two so opcodes can be masked
   433  // with AMask and used as compact array indices.
   434  const (
   435  	ABase386	= (1 + iota) << 11
   436  	ABaseARM
   437  	ABaseAMD64
   438  	ABasePPC64
   439  	ABaseARM64
   440  	ABaseMIPS
   441  	ABaseLoong64
   442  	ABaseRISCV
   443  	ABaseS390X
   444  	ABaseWasm
   445  
   446  	AllowedOpCodes	= 1 << 11		// The number of opcodes available for any given architecture.
   447  	AMask		= AllowedOpCodes - 1	// AND with this to use the opcode as an array index.
   448  )
   449  
   450  // An LSym is the sort of symbol that is written to an object file.
   451  // It represents Go symbols in a flat pkg+"."+name namespace.
   452  type LSym struct {
   453  	Name	string
   454  	Type	objabi.SymKind
   455  	Attribute
   456  
   457  	Size	int64
   458  	Gotype	*LSym
   459  	P	[]byte
   460  	R	[]Reloc
   461  
   462  	Extra	*interface{}	// *FuncInfo, *VarInfo, *FileInfo, or *TypeInfo, if present
   463  
   464  	Pkg	string
   465  	PkgIdx	int32
   466  	SymIdx	int32
   467  }
   468  
   469  // A FuncInfo contains extra fields for STEXT symbols.
   470  type FuncInfo struct {
   471  	Args		int32
   472  	Locals		int32
   473  	Align		int32
   474  	FuncID		abi.FuncID
   475  	FuncFlag	abi.FuncFlag
   476  	StartLine	int32
   477  	Text		*Prog
   478  	Autot		map[*LSym]struct{}
   479  	Pcln		Pcln
   480  	InlMarks	[]InlMark
   481  	spills		[]RegSpill
   482  
   483  	dwarfInfoSym		*LSym
   484  	dwarfLocSym		*LSym
   485  	dwarfRangesSym		*LSym
   486  	dwarfAbsFnSym		*LSym
   487  	dwarfDebugLinesSym	*LSym
   488  
   489  	GCArgs			*LSym
   490  	GCLocals		*LSym
   491  	StackObjects		*LSym
   492  	OpenCodedDeferInfo	*LSym
   493  	ArgInfo			*LSym	// argument info for traceback
   494  	ArgLiveInfo		*LSym	// argument liveness info for traceback
   495  	WrapInfo		*LSym	// for wrapper, info of wrapped function
   496  	JumpTables		[]JumpTable
   497  
   498  	FuncInfoSym	*LSym
   499  	WasmImportSym	*LSym
   500  	WasmImport	*WasmImport
   501  
   502  	sehUnwindInfoSym	*LSym
   503  }
   504  
   505  // JumpTable represents a table used for implementing multi-way
   506  // computed branching, used typically for implementing switches.
   507  // Sym is the table itself, and Targets is a list of target
   508  // instructions to go to for the computed branch index.
   509  type JumpTable struct {
   510  	Sym	*LSym
   511  	Targets	[]*Prog
   512  }
   513  
   514  // NewFuncInfo allocates and returns a FuncInfo for LSym.
   515  func (s *LSym) NewFuncInfo() *FuncInfo {
   516  	if s.Extra != nil {
   517  		panic(fmt.Sprintf("invalid use of LSym - NewFuncInfo with Extra of type %T", *s.Extra))
   518  	}
   519  	f := new(FuncInfo)
   520  	s.Extra = new(interface{})
   521  	*s.Extra = f
   522  	return f
   523  }
   524  
   525  // Func returns the *FuncInfo associated with s, or else nil.
   526  func (s *LSym) Func() *FuncInfo {
   527  	if s.Extra == nil {
   528  		return nil
   529  	}
   530  	f, _ := (*s.Extra).(*FuncInfo)
   531  	return f
   532  }
   533  
   534  type VarInfo struct {
   535  	dwarfInfoSym *LSym
   536  }
   537  
   538  // NewVarInfo allocates and returns a VarInfo for LSym.
   539  func (s *LSym) NewVarInfo() *VarInfo {
   540  	if s.Extra != nil {
   541  		panic(fmt.Sprintf("invalid use of LSym - NewVarInfo with Extra of type %T", *s.Extra))
   542  	}
   543  	f := new(VarInfo)
   544  	s.Extra = new(interface{})
   545  	*s.Extra = f
   546  	return f
   547  }
   548  
   549  // VarInfo returns the *VarInfo associated with s, or else nil.
   550  func (s *LSym) VarInfo() *VarInfo {
   551  	if s.Extra == nil {
   552  		return nil
   553  	}
   554  	f, _ := (*s.Extra).(*VarInfo)
   555  	return f
   556  }
   557  
   558  // A FileInfo contains extra fields for SDATA symbols backed by files.
   559  // (If LSym.Extra is a *FileInfo, LSym.P == nil.)
   560  type FileInfo struct {
   561  	Name	string	// name of file to read into object file
   562  	Size	int64	// length of file
   563  }
   564  
   565  // NewFileInfo allocates and returns a FileInfo for LSym.
   566  func (s *LSym) NewFileInfo() *FileInfo {
   567  	if s.Extra != nil {
   568  		panic(fmt.Sprintf("invalid use of LSym - NewFileInfo with Extra of type %T", *s.Extra))
   569  	}
   570  	f := new(FileInfo)
   571  	s.Extra = new(interface{})
   572  	*s.Extra = f
   573  	return f
   574  }
   575  
   576  // File returns the *FileInfo associated with s, or else nil.
   577  func (s *LSym) File() *FileInfo {
   578  	if s.Extra == nil {
   579  		return nil
   580  	}
   581  	f, _ := (*s.Extra).(*FileInfo)
   582  	return f
   583  }
   584  
   585  // A TypeInfo contains information for a symbol
   586  // that contains a runtime._type.
   587  type TypeInfo struct {
   588  	Type interface{}	// a *cmd/compile/internal/types.Type
   589  }
   590  
   591  func (s *LSym) NewTypeInfo() *TypeInfo {
   592  	if s.Extra != nil {
   593  		panic(fmt.Sprintf("invalid use of LSym - NewTypeInfo with Extra of type %T", *s.Extra))
   594  	}
   595  	t := new(TypeInfo)
   596  	s.Extra = new(interface{})
   597  	*s.Extra = t
   598  	return t
   599  }
   600  
   601  // WasmImport represents a WebAssembly (WASM) imported function with
   602  // parameters and results translated into WASM types based on the Go function
   603  // declaration.
   604  type WasmImport struct {
   605  	// Module holds the WASM module name specified by the //go:wasmimport
   606  	// directive.
   607  	Module	string
   608  	// Name holds the WASM imported function name specified by the
   609  	// //go:wasmimport directive.
   610  	Name	string
   611  	// Params holds the imported function parameter fields.
   612  	Params	[]WasmField
   613  	// Results holds the imported function result fields.
   614  	Results	[]WasmField
   615  }
   616  
   617  func (wi *WasmImport) CreateSym(ctxt *Link) *LSym {
   618  	var sym LSym
   619  
   620  	var b [8]byte
   621  	writeByte := func(x byte) {
   622  		sym.WriteBytes(ctxt, sym.Size, []byte{x})
   623  	}
   624  	writeUint32 := func(x uint32) {
   625  		binary.LittleEndian.PutUint32(b[:], x)
   626  		sym.WriteBytes(ctxt, sym.Size, b[:4])
   627  	}
   628  	writeInt64 := func(x int64) {
   629  		binary.LittleEndian.PutUint64(b[:], uint64(x))
   630  		sym.WriteBytes(ctxt, sym.Size, b[:])
   631  	}
   632  	writeString := func(s string) {
   633  		writeUint32(uint32(len(s)))
   634  		sym.WriteString(ctxt, sym.Size, len(s), s)
   635  	}
   636  	writeString(wi.Module)
   637  	writeString(wi.Name)
   638  	writeUint32(uint32(len(wi.Params)))
   639  	for _, f := range wi.Params {
   640  		writeByte(byte(f.Type))
   641  		writeInt64(f.Offset)
   642  	}
   643  	writeUint32(uint32(len(wi.Results)))
   644  	for _, f := range wi.Results {
   645  		writeByte(byte(f.Type))
   646  		writeInt64(f.Offset)
   647  	}
   648  
   649  	return &sym
   650  }
   651  
   652  type WasmField struct {
   653  	Type	WasmFieldType
   654  	// Offset holds the frame-pointer-relative locations for Go's stack-based
   655  	// ABI. This is used by the src/cmd/internal/wasm package to map WASM
   656  	// import parameters to the Go stack in a wrapper function.
   657  	Offset	int64
   658  }
   659  
   660  type WasmFieldType byte
   661  
   662  const (
   663  	WasmI32	WasmFieldType	= iota
   664  	WasmI64
   665  	WasmF32
   666  	WasmF64
   667  	WasmPtr
   668  )
   669  
   670  type InlMark struct {
   671  	// When unwinding from an instruction in an inlined body, mark
   672  	// where we should unwind to.
   673  	// id records the global inlining id of the inlined body.
   674  	// p records the location of an instruction in the parent (inliner) frame.
   675  	p	*Prog
   676  	id	int32
   677  }
   678  
   679  // Mark p as the instruction to set as the pc when
   680  // "unwinding" the inlining global frame id. Usually it should be
   681  // instruction with a file:line at the callsite, and occur
   682  // just before the body of the inlined function.
   683  func (fi *FuncInfo) AddInlMark(p *Prog, id int32) {
   684  	fi.InlMarks = append(fi.InlMarks, InlMark{p: p, id: id})
   685  }
   686  
   687  // AddSpill appends a spill record to the list for FuncInfo fi
   688  func (fi *FuncInfo) AddSpill(s RegSpill) {
   689  	fi.spills = append(fi.spills, s)
   690  }
   691  
   692  // Record the type symbol for an auto variable so that the linker
   693  // an emit DWARF type information for the type.
   694  func (fi *FuncInfo) RecordAutoType(gotype *LSym) {
   695  	if fi.Autot == nil {
   696  		fi.Autot = make(map[*LSym]struct{})
   697  	}
   698  	fi.Autot[gotype] = struct{}{}
   699  }
   700  
   701  //go:generate stringer -type ABI
   702  
   703  // ABI is the calling convention of a text symbol.
   704  type ABI uint8
   705  
   706  const (
   707  	// ABI0 is the stable stack-based ABI. It's important that the
   708  	// value of this is "0": we can't distinguish between
   709  	// references to data and ABI0 text symbols in assembly code,
   710  	// and hence this doesn't distinguish between symbols without
   711  	// an ABI and text symbols with ABI0.
   712  	ABI0	ABI	= iota
   713  
   714  	// ABIInternal is the internal ABI that may change between Go
   715  	// versions. All Go functions use the internal ABI and the
   716  	// compiler generates wrappers for calls to and from other
   717  	// ABIs.
   718  	ABIInternal
   719  
   720  	ABICount
   721  )
   722  
   723  // ParseABI converts from a string representation in 'abistr' to the
   724  // corresponding ABI value. Second return value is TRUE if the
   725  // abi string is recognized, FALSE otherwise.
   726  func ParseABI(abistr string) (ABI, bool) {
   727  	switch abistr {
   728  	default:
   729  		return ABI0, false
   730  	case "ABI0":
   731  		return ABI0, true
   732  	case "ABIInternal":
   733  		return ABIInternal, true
   734  	}
   735  }
   736  
   737  // ABISet is a bit set of ABI values.
   738  type ABISet uint8
   739  
   740  const (
   741  	// ABISetCallable is the set of all ABIs any function could
   742  	// potentially be called using.
   743  	ABISetCallable ABISet = (1 << ABI0) | (1 << ABIInternal)
   744  )
   745  
   746  // Ensure ABISet is big enough to hold all ABIs.
   747  var _ ABISet = 1 << (ABICount - 1)
   748  
   749  func ABISetOf(abi ABI) ABISet {
   750  	return 1 << abi
   751  }
   752  
   753  func (a *ABISet) Set(abi ABI, value bool) {
   754  	if value {
   755  		*a |= 1 << abi
   756  	} else {
   757  		*a &^= 1 << abi
   758  	}
   759  }
   760  
   761  func (a *ABISet) Get(abi ABI) bool {
   762  	return (*a>>abi)&1 != 0
   763  }
   764  
   765  func (a ABISet) String() string {
   766  	s := "{"
   767  	for i := ABI(0); a != 0; i++ {
   768  		if a&(1<<i) != 0 {
   769  			if s != "{" {
   770  				s += ","
   771  			}
   772  			s += i.String()
   773  			a &^= 1 << i
   774  		}
   775  	}
   776  	return s + "}"
   777  }
   778  
   779  // Attribute is a set of symbol attributes.
   780  type Attribute uint32
   781  
   782  const (
   783  	AttrDuplicateOK	Attribute	= 1 << iota
   784  	AttrCFunc
   785  	AttrNoSplit
   786  	AttrLeaf
   787  	AttrWrapper
   788  	AttrNeedCtxt
   789  	AttrNoFrame
   790  	AttrOnList
   791  	AttrStatic
   792  
   793  	// MakeTypelink means that the type should have an entry in the typelink table.
   794  	AttrMakeTypelink
   795  
   796  	// ReflectMethod means the function may call reflect.Type.Method or
   797  	// reflect.Type.MethodByName. Matching is imprecise (as reflect.Type
   798  	// can be used through a custom interface), so ReflectMethod may be
   799  	// set in some cases when the reflect package is not called.
   800  	//
   801  	// Used by the linker to determine what methods can be pruned.
   802  	AttrReflectMethod
   803  
   804  	// Local means make the symbol local even when compiling Go code to reference Go
   805  	// symbols in other shared libraries, as in this mode symbols are global by
   806  	// default. "local" here means in the sense of the dynamic linker, i.e. not
   807  	// visible outside of the module (shared library or executable) that contains its
   808  	// definition. (When not compiling to support Go shared libraries, all symbols are
   809  	// local in this sense unless there is a cgo_export_* directive).
   810  	AttrLocal
   811  
   812  	// For function symbols; indicates that the specified function was the
   813  	// target of an inline during compilation
   814  	AttrWasInlined
   815  
   816  	// Indexed indicates this symbol has been assigned with an index (when using the
   817  	// new object file format).
   818  	AttrIndexed
   819  
   820  	// Only applied on type descriptor symbols, UsedInIface indicates this type is
   821  	// converted to an interface.
   822  	//
   823  	// Used by the linker to determine what methods can be pruned.
   824  	AttrUsedInIface
   825  
   826  	// ContentAddressable indicates this is a content-addressable symbol.
   827  	AttrContentAddressable
   828  
   829  	// ABI wrapper is set for compiler-generated text symbols that
   830  	// convert between ABI0 and ABIInternal calling conventions.
   831  	AttrABIWrapper
   832  
   833  	// IsPcdata indicates this is a pcdata symbol.
   834  	AttrPcdata
   835  
   836  	// PkgInit indicates this is a compiler-generated package init func.
   837  	AttrPkgInit
   838  
   839  	// attrABIBase is the value at which the ABI is encoded in
   840  	// Attribute. This must be last; all bits after this are
   841  	// assumed to be an ABI value.
   842  	//
   843  	// MUST BE LAST since all bits above this comprise the ABI.
   844  	attrABIBase
   845  )
   846  
   847  func (a *Attribute) load() Attribute	{ return Attribute(atomic.LoadUint32((*uint32)(a))) }
   848  
   849  func (a *Attribute) DuplicateOK() bool		{ return a.load()&AttrDuplicateOK != 0 }
   850  func (a *Attribute) MakeTypelink() bool		{ return a.load()&AttrMakeTypelink != 0 }
   851  func (a *Attribute) CFunc() bool		{ return a.load()&AttrCFunc != 0 }
   852  func (a *Attribute) NoSplit() bool		{ return a.load()&AttrNoSplit != 0 }
   853  func (a *Attribute) Leaf() bool			{ return a.load()&AttrLeaf != 0 }
   854  func (a *Attribute) OnList() bool		{ return a.load()&AttrOnList != 0 }
   855  func (a *Attribute) ReflectMethod() bool	{ return a.load()&AttrReflectMethod != 0 }
   856  func (a *Attribute) Local() bool		{ return a.load()&AttrLocal != 0 }
   857  func (a *Attribute) Wrapper() bool		{ return a.load()&AttrWrapper != 0 }
   858  func (a *Attribute) NeedCtxt() bool		{ return a.load()&AttrNeedCtxt != 0 }
   859  func (a *Attribute) NoFrame() bool		{ return a.load()&AttrNoFrame != 0 }
   860  func (a *Attribute) Static() bool		{ return a.load()&AttrStatic != 0 }
   861  func (a *Attribute) WasInlined() bool		{ return a.load()&AttrWasInlined != 0 }
   862  func (a *Attribute) Indexed() bool		{ return a.load()&AttrIndexed != 0 }
   863  func (a *Attribute) UsedInIface() bool		{ return a.load()&AttrUsedInIface != 0 }
   864  func (a *Attribute) ContentAddressable() bool	{ return a.load()&AttrContentAddressable != 0 }
   865  func (a *Attribute) ABIWrapper() bool		{ return a.load()&AttrABIWrapper != 0 }
   866  func (a *Attribute) IsPcdata() bool		{ return a.load()&AttrPcdata != 0 }
   867  func (a *Attribute) IsPkgInit() bool		{ return a.load()&AttrPkgInit != 0 }
   868  
   869  func (a *Attribute) Set(flag Attribute, value bool) {
   870  	for {
   871  		v0 := a.load()
   872  		v := v0
   873  		if value {
   874  			v |= flag
   875  		} else {
   876  			v &^= flag
   877  		}
   878  		if atomic.CompareAndSwapUint32((*uint32)(a), uint32(v0), uint32(v)) {
   879  			break
   880  		}
   881  	}
   882  }
   883  
   884  func (a *Attribute) ABI() ABI	{ return ABI(a.load() / attrABIBase) }
   885  func (a *Attribute) SetABI(abi ABI) {
   886  	const mask = 1	// Only one ABI bit for now.
   887  	for {
   888  		v0 := a.load()
   889  		v := (v0 &^ (mask * attrABIBase)) | Attribute(abi)*attrABIBase
   890  		if atomic.CompareAndSwapUint32((*uint32)(a), uint32(v0), uint32(v)) {
   891  			break
   892  		}
   893  	}
   894  }
   895  
   896  var textAttrStrings = [...]struct {
   897  	bit	Attribute
   898  	s	string
   899  }{
   900  	{bit: AttrDuplicateOK, s: "DUPOK"},
   901  	{bit: AttrMakeTypelink, s: ""},
   902  	{bit: AttrCFunc, s: "CFUNC"},
   903  	{bit: AttrNoSplit, s: "NOSPLIT"},
   904  	{bit: AttrLeaf, s: "LEAF"},
   905  	{bit: AttrOnList, s: ""},
   906  	{bit: AttrReflectMethod, s: "REFLECTMETHOD"},
   907  	{bit: AttrLocal, s: "LOCAL"},
   908  	{bit: AttrWrapper, s: "WRAPPER"},
   909  	{bit: AttrNeedCtxt, s: "NEEDCTXT"},
   910  	{bit: AttrNoFrame, s: "NOFRAME"},
   911  	{bit: AttrStatic, s: "STATIC"},
   912  	{bit: AttrWasInlined, s: ""},
   913  	{bit: AttrIndexed, s: ""},
   914  	{bit: AttrContentAddressable, s: ""},
   915  	{bit: AttrABIWrapper, s: "ABIWRAPPER"},
   916  	{bit: AttrPkgInit, s: "PKGINIT"},
   917  }
   918  
   919  // String formats a for printing in as part of a TEXT prog.
   920  func (a Attribute) String() string {
   921  	var s string
   922  	for _, x := range textAttrStrings {
   923  		if a&x.bit != 0 {
   924  			if x.s != "" {
   925  				s += x.s + "|"
   926  			}
   927  			a &^= x.bit
   928  		}
   929  	}
   930  	switch a.ABI() {
   931  	case ABI0:
   932  	case ABIInternal:
   933  		s += "ABIInternal|"
   934  		a.SetABI(0)	// Clear ABI so we don't print below.
   935  	}
   936  	if a != 0 {
   937  		s += fmt.Sprintf("UnknownAttribute(%d)|", a)
   938  	}
   939  	// Chop off trailing |, if present.
   940  	if len(s) > 0 {
   941  		s = s[:len(s)-1]
   942  	}
   943  	return s
   944  }
   945  
   946  // TextAttrString formats the symbol attributes for printing in as part of a TEXT prog.
   947  func (s *LSym) TextAttrString() string {
   948  	attr := s.Attribute.String()
   949  	if s.Func().FuncFlag&abi.FuncFlagTopFrame != 0 {
   950  		if attr != "" {
   951  			attr += "|"
   952  		}
   953  		attr += "TOPFRAME"
   954  	}
   955  	return attr
   956  }
   957  
   958  func (s *LSym) String() string {
   959  	return s.Name
   960  }
   961  
   962  // The compiler needs *LSym to be assignable to cmd/compile/internal/ssa.Sym.
   963  func (*LSym) CanBeAnSSASym()	{}
   964  func (*LSym) CanBeAnSSAAux()	{}
   965  
   966  type Pcln struct {
   967  	// Aux symbols for pcln
   968  	Pcsp		*LSym
   969  	Pcfile		*LSym
   970  	Pcline		*LSym
   971  	Pcinline	*LSym
   972  	Pcdata		[]*LSym
   973  	Funcdata	[]*LSym
   974  	UsedFiles	map[goobj.CUFileIndex]struct{}	// file indices used while generating pcfile
   975  	InlTree		InlTree				// per-function inlining tree extracted from the global tree
   976  }
   977  
   978  type Reloc struct {
   979  	Off	int32
   980  	Siz	uint8
   981  	Type	objabi.RelocType
   982  	Add	int64
   983  	Sym	*LSym
   984  }
   985  
   986  type Auto struct {
   987  	Asym	*LSym
   988  	Aoffset	int32
   989  	Name	AddrName
   990  	Gotype	*LSym
   991  }
   992  
   993  // RegSpill provides spill/fill information for a register-resident argument
   994  // to a function.  These need spilling/filling in the safepoint/stackgrowth case.
   995  // At the time of fill/spill, the offset must be adjusted by the architecture-dependent
   996  // adjustment to hardware SP that occurs in a call instruction.  E.g., for AMD64,
   997  // at Offset+8 because the return address was pushed.
   998  type RegSpill struct {
   999  	Addr		Addr
  1000  	Reg		int16
  1001  	Spill, Unspill	As
  1002  }
  1003  
  1004  // A Func represents a Go function. If non-nil, it must be a *ir.Func.
  1005  type Func interface {
  1006  	Pos() src.XPos
  1007  }
  1008  
  1009  // Link holds the context for writing object code from a compiler
  1010  // to be linker input or for reading that input into the linker.
  1011  type Link struct {
  1012  	Headtype		objabi.HeadType
  1013  	Arch			*LinkArch
  1014  	Debugasm		int
  1015  	Debugvlog		bool
  1016  	Debugpcln		string
  1017  	Flag_shared		bool
  1018  	Flag_dynlink		bool
  1019  	Flag_linkshared		bool
  1020  	Flag_optimize		bool
  1021  	Flag_locationlists	bool
  1022  	Flag_noRefName		bool	// do not include referenced symbol names in object file
  1023  	Retpoline		bool	// emit use of retpoline stubs for indirect jmp/call
  1024  	Flag_maymorestack	string	// If not "", call this function before stack checks
  1025  	Bso			*bufio.Writer
  1026  	Pathname		string
  1027  	Pkgpath			string			// the current package's import path
  1028  	hashmu			sync.Mutex		// protects hash, funchash
  1029  	hash			map[string]*LSym	// name -> sym mapping
  1030  	funchash		map[string]*LSym	// name -> sym mapping for ABIInternal syms
  1031  	statichash		map[string]*LSym	// name -> sym mapping for static syms
  1032  	PosTable		src.PosTable
  1033  	InlTree			InlTree	// global inlining tree used by gc/inl.go
  1034  	DwFixups		*DwarfFixupTable
  1035  	Imports			[]goobj.ImportedPkg
  1036  	DiagFunc		func(string, ...interface{})
  1037  	DiagFlush		func()
  1038  	DebugInfo		func(fn *LSym, info *LSym, curfn Func) ([]dwarf.Scope, dwarf.InlCalls)
  1039  	GenAbstractFunc		func(fn *LSym)
  1040  	Errors			int
  1041  
  1042  	InParallel	bool	// parallel backend phase in effect
  1043  	UseBASEntries	bool	// use Base Address Selection Entries in location lists and PC ranges
  1044  	IsAsm		bool	// is the source assembly language, which may contain surprising idioms (e.g., call tables)
  1045  
  1046  	// state for writing objects
  1047  	Text	[]*LSym
  1048  	Data	[]*LSym
  1049  
  1050  	// Constant symbols (e.g. $i64.*) are data symbols created late
  1051  	// in the concurrent phase. To ensure a deterministic order, we
  1052  	// add them to a separate list, sort at the end, and append it
  1053  	// to Data.
  1054  	constSyms	[]*LSym
  1055  
  1056  	// pkgIdx maps package path to index. The index is used for
  1057  	// symbol reference in the object file.
  1058  	pkgIdx	map[string]int32
  1059  
  1060  	defs		[]*LSym	// list of defined symbols in the current package
  1061  	hashed64defs	[]*LSym	// list of defined short (64-bit or less) hashed (content-addressable) symbols
  1062  	hasheddefs	[]*LSym	// list of defined hashed (content-addressable) symbols
  1063  	nonpkgdefs	[]*LSym	// list of defined non-package symbols
  1064  	nonpkgrefs	[]*LSym	// list of referenced non-package symbols
  1065  
  1066  	Fingerprint	goobj.FingerprintType	// fingerprint of symbol indices, to catch index mismatch
  1067  }
  1068  
  1069  func (ctxt *Link) Diag(format string, args ...interface{}) {
  1070  	ctxt.Errors++
  1071  	ctxt.DiagFunc(format, args...)
  1072  }
  1073  
  1074  func (ctxt *Link) Logf(format string, args ...interface{}) {
  1075  	fmt.Fprintf(ctxt.Bso, format, args...)
  1076  	ctxt.Bso.Flush()
  1077  }
  1078  
  1079  // SpillRegisterArgs emits the code to spill register args into whatever
  1080  // locations the spill records specify.
  1081  func (fi *FuncInfo) SpillRegisterArgs(last *Prog, pa ProgAlloc) *Prog {
  1082  	// Spill register args.
  1083  	for _, ra := range fi.spills {
  1084  		spill := Appendp(last, pa)
  1085  		spill.As = ra.Spill
  1086  		spill.From.Type = TYPE_REG
  1087  		spill.From.Reg = ra.Reg
  1088  		spill.To = ra.Addr
  1089  		last = spill
  1090  	}
  1091  	return last
  1092  }
  1093  
  1094  // UnspillRegisterArgs emits the code to restore register args from whatever
  1095  // locations the spill records specify.
  1096  func (fi *FuncInfo) UnspillRegisterArgs(last *Prog, pa ProgAlloc) *Prog {
  1097  	// Unspill any spilled register args
  1098  	for _, ra := range fi.spills {
  1099  		unspill := Appendp(last, pa)
  1100  		unspill.As = ra.Unspill
  1101  		unspill.From = ra.Addr
  1102  		unspill.To.Type = TYPE_REG
  1103  		unspill.To.Reg = ra.Reg
  1104  		last = unspill
  1105  	}
  1106  	return last
  1107  }
  1108  
  1109  // LinkArch is the definition of a single architecture.
  1110  type LinkArch struct {
  1111  	*sys.Arch
  1112  	Init		func(*Link)
  1113  	ErrorCheck	func(*Link, *LSym)
  1114  	Preprocess	func(*Link, *LSym, ProgAlloc)
  1115  	Assemble	func(*Link, *LSym, ProgAlloc)
  1116  	Progedit	func(*Link, *Prog, ProgAlloc)
  1117  	SEH		func(*Link, *LSym) *LSym
  1118  	UnaryDst	map[As]bool	// Instruction takes one operand, a destination.
  1119  	DWARFRegisters	map[int16]int16
  1120  }