link.go raw

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