Source file src/cmd/internal/obj/link.go

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

View as plain text