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