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 Type objabi.SymKind 460 Attribute 461 462 Size int64 463 Gotype *LSym 464 P []byte 465 R []Reloc 466 467 Extra *any // *FuncInfo, *VarInfo, *FileInfo, *TypeInfo, or *ItabInfo, if present 468 469 Pkg string 470 PkgIdx int32 471 SymIdx int32 472 } 473 474 // A FuncInfo contains extra fields for STEXT symbols. 475 type FuncInfo struct { 476 Args int32 477 Locals int32 478 Align 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 803 // bool is not really a wasm type, but we allow it on wasmimport/wasmexport 804 // function parameters/results. 32-bit on Wasm side, 8-bit on Go side. 805 WasmBool 806 ) 807 808 type InlMark struct { 809 // When unwinding from an instruction in an inlined body, mark 810 // where we should unwind to. 811 // id records the global inlining id of the inlined body. 812 // p records the location of an instruction in the parent (inliner) frame. 813 p *Prog 814 id int32 815 } 816 817 // Mark p as the instruction to set as the pc when 818 // "unwinding" the inlining global frame id. Usually it should be 819 // instruction with a file:line at the callsite, and occur 820 // just before the body of the inlined function. 821 func (fi *FuncInfo) AddInlMark(p *Prog, id int32) { 822 fi.InlMarks = append(fi.InlMarks, InlMark{p: p, id: id}) 823 } 824 825 // AddSpill appends a spill record to the list for FuncInfo fi 826 func (fi *FuncInfo) AddSpill(s RegSpill) { 827 fi.spills = append(fi.spills, s) 828 } 829 830 // Record the type symbol for an auto variable so that the linker 831 // an emit DWARF type information for the type. 832 func (fi *FuncInfo) RecordAutoType(gotype *LSym) { 833 if fi.Autot == nil { 834 fi.Autot = make(map[*LSym]struct{}) 835 } 836 fi.Autot[gotype] = struct{}{} 837 } 838 839 //go:generate stringer -type ABI 840 841 // ABI is the calling convention of a text symbol. 842 type ABI uint8 843 844 const ( 845 // ABI0 is the stable stack-based ABI. It's important that the 846 // value of this is "0": we can't distinguish between 847 // references to data and ABI0 text symbols in assembly code, 848 // and hence this doesn't distinguish between symbols without 849 // an ABI and text symbols with ABI0. 850 ABI0 ABI = iota 851 852 // ABIInternal is the internal ABI that may change between Go 853 // versions. All Go functions use the internal ABI and the 854 // compiler generates wrappers for calls to and from other 855 // ABIs. 856 ABIInternal 857 858 ABICount 859 ) 860 861 // ParseABI converts from a string representation in 'abistr' to the 862 // corresponding ABI value. Second return value is TRUE if the 863 // abi string is recognized, FALSE otherwise. 864 func ParseABI(abistr string) (ABI, bool) { 865 switch abistr { 866 default: 867 return ABI0, false 868 case "ABI0": 869 return ABI0, true 870 case "ABIInternal": 871 return ABIInternal, true 872 } 873 } 874 875 // ABISet is a bit set of ABI values. 876 type ABISet uint8 877 878 const ( 879 // ABISetCallable is the set of all ABIs any function could 880 // potentially be called using. 881 ABISetCallable ABISet = (1 << ABI0) | (1 << ABIInternal) 882 ) 883 884 // Ensure ABISet is big enough to hold all ABIs. 885 var _ ABISet = 1 << (ABICount - 1) 886 887 func ABISetOf(abi ABI) ABISet { 888 return 1 << abi 889 } 890 891 func (a *ABISet) Set(abi ABI, value bool) { 892 if value { 893 *a |= 1 << abi 894 } else { 895 *a &^= 1 << abi 896 } 897 } 898 899 func (a *ABISet) Get(abi ABI) bool { 900 return (*a>>abi)&1 != 0 901 } 902 903 func (a ABISet) String() string { 904 s := "{" 905 for i := ABI(0); a != 0; i++ { 906 if a&(1<<i) != 0 { 907 if s != "{" { 908 s += "," 909 } 910 s += i.String() 911 a &^= 1 << i 912 } 913 } 914 return s + "}" 915 } 916 917 // Attribute is a set of symbol attributes. 918 type Attribute uint32 919 920 const ( 921 AttrDuplicateOK Attribute = 1 << iota 922 AttrCFunc 923 AttrNoSplit 924 AttrLeaf 925 AttrWrapper 926 AttrNeedCtxt 927 AttrNoFrame 928 AttrOnList 929 AttrStatic 930 931 // MakeTypelink means that the type should have an entry in the typelink table. 932 AttrMakeTypelink 933 934 // ReflectMethod means the function may call reflect.Type.Method or 935 // reflect.Type.MethodByName. Matching is imprecise (as reflect.Type 936 // can be used through a custom interface), so ReflectMethod may be 937 // set in some cases when the reflect package is not called. 938 // 939 // Used by the linker to determine what methods can be pruned. 940 AttrReflectMethod 941 942 // Local means make the symbol local even when compiling Go code to reference Go 943 // symbols in other shared libraries, as in this mode symbols are global by 944 // default. "local" here means in the sense of the dynamic linker, i.e. not 945 // visible outside of the module (shared library or executable) that contains its 946 // definition. (When not compiling to support Go shared libraries, all symbols are 947 // local in this sense unless there is a cgo_export_* directive). 948 AttrLocal 949 950 // For function symbols; indicates that the specified function was the 951 // target of an inline during compilation 952 AttrWasInlined 953 954 // Indexed indicates this symbol has been assigned with an index (when using the 955 // new object file format). 956 AttrIndexed 957 958 // Only applied on type descriptor symbols, UsedInIface indicates this type is 959 // converted to an interface. 960 // 961 // Used by the linker to determine what methods can be pruned. 962 AttrUsedInIface 963 964 // ContentAddressable indicates this is a content-addressable symbol. 965 AttrContentAddressable 966 967 // ABI wrapper is set for compiler-generated text symbols that 968 // convert between ABI0 and ABIInternal calling conventions. 969 AttrABIWrapper 970 971 // IsPcdata indicates this is a pcdata symbol. 972 AttrPcdata 973 974 // PkgInit indicates this is a compiler-generated package init func. 975 AttrPkgInit 976 977 // Linkname indicates this is a go:linkname'd symbol. 978 AttrLinkname 979 980 // attrABIBase is the value at which the ABI is encoded in 981 // Attribute. This must be last; all bits after this are 982 // assumed to be an ABI value. 983 // 984 // MUST BE LAST since all bits above this comprise the ABI. 985 attrABIBase 986 ) 987 988 func (a *Attribute) load() Attribute { return Attribute(atomic.LoadUint32((*uint32)(a))) } 989 990 func (a *Attribute) DuplicateOK() bool { return a.load()&AttrDuplicateOK != 0 } 991 func (a *Attribute) MakeTypelink() bool { return a.load()&AttrMakeTypelink != 0 } 992 func (a *Attribute) CFunc() bool { return a.load()&AttrCFunc != 0 } 993 func (a *Attribute) NoSplit() bool { return a.load()&AttrNoSplit != 0 } 994 func (a *Attribute) Leaf() bool { return a.load()&AttrLeaf != 0 } 995 func (a *Attribute) OnList() bool { return a.load()&AttrOnList != 0 } 996 func (a *Attribute) ReflectMethod() bool { return a.load()&AttrReflectMethod != 0 } 997 func (a *Attribute) Local() bool { return a.load()&AttrLocal != 0 } 998 func (a *Attribute) Wrapper() bool { return a.load()&AttrWrapper != 0 } 999 func (a *Attribute) NeedCtxt() bool { return a.load()&AttrNeedCtxt != 0 } 1000 func (a *Attribute) NoFrame() bool { return a.load()&AttrNoFrame != 0 } 1001 func (a *Attribute) Static() bool { return a.load()&AttrStatic != 0 } 1002 func (a *Attribute) WasInlined() bool { return a.load()&AttrWasInlined != 0 } 1003 func (a *Attribute) Indexed() bool { return a.load()&AttrIndexed != 0 } 1004 func (a *Attribute) UsedInIface() bool { return a.load()&AttrUsedInIface != 0 } 1005 func (a *Attribute) ContentAddressable() bool { return a.load()&AttrContentAddressable != 0 } 1006 func (a *Attribute) ABIWrapper() bool { return a.load()&AttrABIWrapper != 0 } 1007 func (a *Attribute) IsPcdata() bool { return a.load()&AttrPcdata != 0 } 1008 func (a *Attribute) IsPkgInit() bool { return a.load()&AttrPkgInit != 0 } 1009 func (a *Attribute) IsLinkname() bool { return a.load()&AttrLinkname != 0 } 1010 1011 func (a *Attribute) Set(flag Attribute, value bool) { 1012 for { 1013 v0 := a.load() 1014 v := v0 1015 if value { 1016 v |= flag 1017 } else { 1018 v &^= flag 1019 } 1020 if atomic.CompareAndSwapUint32((*uint32)(a), uint32(v0), uint32(v)) { 1021 break 1022 } 1023 } 1024 } 1025 1026 func (a *Attribute) ABI() ABI { return ABI(a.load() / attrABIBase) } 1027 func (a *Attribute) SetABI(abi ABI) { 1028 const mask = 1 // Only one ABI bit for now. 1029 for { 1030 v0 := a.load() 1031 v := (v0 &^ (mask * attrABIBase)) | Attribute(abi)*attrABIBase 1032 if atomic.CompareAndSwapUint32((*uint32)(a), uint32(v0), uint32(v)) { 1033 break 1034 } 1035 } 1036 } 1037 1038 var textAttrStrings = [...]struct { 1039 bit Attribute 1040 s string 1041 }{ 1042 {bit: AttrDuplicateOK, s: "DUPOK"}, 1043 {bit: AttrMakeTypelink, s: ""}, 1044 {bit: AttrCFunc, s: "CFUNC"}, 1045 {bit: AttrNoSplit, s: "NOSPLIT"}, 1046 {bit: AttrLeaf, s: "LEAF"}, 1047 {bit: AttrOnList, s: ""}, 1048 {bit: AttrReflectMethod, s: "REFLECTMETHOD"}, 1049 {bit: AttrLocal, s: "LOCAL"}, 1050 {bit: AttrWrapper, s: "WRAPPER"}, 1051 {bit: AttrNeedCtxt, s: "NEEDCTXT"}, 1052 {bit: AttrNoFrame, s: "NOFRAME"}, 1053 {bit: AttrStatic, s: "STATIC"}, 1054 {bit: AttrWasInlined, s: ""}, 1055 {bit: AttrIndexed, s: ""}, 1056 {bit: AttrContentAddressable, s: ""}, 1057 {bit: AttrABIWrapper, s: "ABIWRAPPER"}, 1058 {bit: AttrPkgInit, s: "PKGINIT"}, 1059 {bit: AttrLinkname, s: "LINKNAME"}, 1060 } 1061 1062 // String formats a for printing in as part of a TEXT prog. 1063 func (a Attribute) String() string { 1064 var s string 1065 for _, x := range textAttrStrings { 1066 if a&x.bit != 0 { 1067 if x.s != "" { 1068 s += x.s + "|" 1069 } 1070 a &^= x.bit 1071 } 1072 } 1073 switch a.ABI() { 1074 case ABI0: 1075 case ABIInternal: 1076 s += "ABIInternal|" 1077 a.SetABI(0) // Clear ABI so we don't print below. 1078 } 1079 if a != 0 { 1080 s += fmt.Sprintf("UnknownAttribute(%d)|", a) 1081 } 1082 // Chop off trailing |, if present. 1083 if len(s) > 0 { 1084 s = s[:len(s)-1] 1085 } 1086 return s 1087 } 1088 1089 // TextAttrString formats the symbol attributes for printing in as part of a TEXT prog. 1090 func (s *LSym) TextAttrString() string { 1091 attr := s.Attribute.String() 1092 if s.Func().FuncFlag&abi.FuncFlagTopFrame != 0 { 1093 if attr != "" { 1094 attr += "|" 1095 } 1096 attr += "TOPFRAME" 1097 } 1098 return attr 1099 } 1100 1101 func (s *LSym) String() string { 1102 return s.Name 1103 } 1104 1105 // The compiler needs *LSym to be assignable to cmd/compile/internal/ssa.Sym. 1106 func (*LSym) CanBeAnSSASym() {} 1107 func (*LSym) CanBeAnSSAAux() {} 1108 1109 type Pcln struct { 1110 // Aux symbols for pcln 1111 Pcsp *LSym 1112 Pcfile *LSym 1113 Pcline *LSym 1114 Pcinline *LSym 1115 Pcdata []*LSym 1116 Funcdata []*LSym 1117 UsedFiles map[goobj.CUFileIndex]struct{} // file indices used while generating pcfile 1118 InlTree InlTree // per-function inlining tree extracted from the global tree 1119 } 1120 1121 type Reloc struct { 1122 Off int32 1123 Siz uint8 1124 Type objabi.RelocType 1125 Add int64 1126 Sym *LSym 1127 } 1128 1129 type Auto struct { 1130 Asym *LSym 1131 Aoffset int32 1132 Name AddrName 1133 Gotype *LSym 1134 } 1135 1136 // RegSpill provides spill/fill information for a register-resident argument 1137 // to a function. These need spilling/filling in the safepoint/stackgrowth case. 1138 // At the time of fill/spill, the offset must be adjusted by the architecture-dependent 1139 // adjustment to hardware SP that occurs in a call instruction. E.g., for AMD64, 1140 // at Offset+8 because the return address was pushed. 1141 type RegSpill struct { 1142 Addr Addr 1143 Reg int16 1144 Reg2 int16 // If not 0, a second register to spill at Addr+regSize. Only for some archs. 1145 Spill, Unspill As 1146 } 1147 1148 // A Func represents a Go function. If non-nil, it must be a *ir.Func. 1149 type Func interface { 1150 Pos() src.XPos 1151 } 1152 1153 // Link holds the context for writing object code from a compiler 1154 // to be linker input or for reading that input into the linker. 1155 type Link struct { 1156 Headtype objabi.HeadType 1157 Arch *LinkArch 1158 CompressInstructions bool // use compressed instructions where possible (if supported by architecture) 1159 Debugasm int 1160 Debugvlog bool 1161 Debugpcln string 1162 Flag_shared bool 1163 Flag_dynlink bool 1164 Flag_linkshared bool 1165 Flag_optimize bool 1166 Flag_locationlists bool 1167 Flag_noRefName bool // do not include referenced symbol names in object file 1168 Retpoline bool // emit use of retpoline stubs for indirect jmp/call 1169 Flag_maymorestack string // If not "", call this function before stack checks 1170 Bso *bufio.Writer 1171 Pathname string 1172 Pkgpath string // the current package's import path 1173 hashmu sync.Mutex // protects hash, funchash 1174 hash map[string]*LSym // name -> sym mapping 1175 funchash map[string]*LSym // name -> sym mapping for ABIInternal syms 1176 statichash map[string]*LSym // name -> sym mapping for static syms 1177 PosTable src.PosTable 1178 InlTree InlTree // global inlining tree used by gc/inl.go 1179 DwFixups *DwarfFixupTable 1180 DwTextCount int 1181 Imports []goobj.ImportedPkg 1182 DiagFunc func(string, ...any) 1183 DiagFlush func() 1184 DebugInfo func(ctxt *Link, fn *LSym, info *LSym, curfn Func) ([]dwarf.Scope, dwarf.InlCalls) 1185 GenAbstractFunc func(fn *LSym) 1186 Errors int 1187 1188 InParallel bool // parallel backend phase in effect 1189 UseBASEntries bool // use Base Address Selection Entries in location lists and PC ranges 1190 IsAsm bool // is the source assembly language, which may contain surprising idioms (e.g., call tables) 1191 Std bool // is standard library package 1192 1193 // state for writing objects 1194 Text []*LSym 1195 Data []*LSym 1196 1197 // Constant symbols (e.g. $i64.*) are data symbols created late 1198 // in the concurrent phase. To ensure a deterministic order, we 1199 // add them to a separate list, sort at the end, and append it 1200 // to Data. 1201 constSyms []*LSym 1202 1203 // Windows SEH symbols are also data symbols that can be created 1204 // concurrently. 1205 SEHSyms []*LSym 1206 1207 // pkgIdx maps package path to index. The index is used for 1208 // symbol reference in the object file. 1209 pkgIdx map[string]int32 1210 1211 defs []*LSym // list of defined symbols in the current package 1212 hashed64defs []*LSym // list of defined short (64-bit or less) hashed (content-addressable) symbols 1213 hasheddefs []*LSym // list of defined hashed (content-addressable) symbols 1214 nonpkgdefs []*LSym // list of defined non-package symbols 1215 nonpkgrefs []*LSym // list of referenced non-package symbols 1216 1217 Fingerprint goobj.FingerprintType // fingerprint of symbol indices, to catch index mismatch 1218 } 1219 1220 // Assert to vet's printf checker that Link.DiagFunc is a printf-like. 1221 func _(ctxt *Link) { 1222 ctxt.DiagFunc = func(format string, args ...any) { 1223 _ = fmt.Sprintf(format, args...) 1224 } 1225 } 1226 1227 func (ctxt *Link) Diag(format string, args ...any) { 1228 ctxt.Errors++ 1229 ctxt.DiagFunc(format, args...) 1230 } 1231 1232 func (ctxt *Link) Logf(format string, args ...any) { 1233 fmt.Fprintf(ctxt.Bso, format, args...) 1234 ctxt.Bso.Flush() 1235 } 1236 1237 // SpillRegisterArgs emits the code to spill register args into whatever 1238 // locations the spill records specify. 1239 func (fi *FuncInfo) SpillRegisterArgs(last *Prog, pa ProgAlloc) *Prog { 1240 // Spill register args. 1241 for _, ra := range fi.spills { 1242 spill := Appendp(last, pa) 1243 spill.As = ra.Spill 1244 spill.From.Type = TYPE_REG 1245 spill.From.Reg = ra.Reg 1246 if ra.Reg2 != 0 { 1247 spill.From.Type = TYPE_REGREG 1248 spill.From.Offset = int64(ra.Reg2) 1249 } 1250 spill.To = ra.Addr 1251 last = spill 1252 } 1253 return last 1254 } 1255 1256 // UnspillRegisterArgs emits the code to restore register args from whatever 1257 // locations the spill records specify. 1258 func (fi *FuncInfo) UnspillRegisterArgs(last *Prog, pa ProgAlloc) *Prog { 1259 // Unspill any spilled register args 1260 for _, ra := range fi.spills { 1261 unspill := Appendp(last, pa) 1262 unspill.As = ra.Unspill 1263 unspill.From = ra.Addr 1264 unspill.To.Type = TYPE_REG 1265 unspill.To.Reg = ra.Reg 1266 if ra.Reg2 != 0 { 1267 unspill.To.Type = TYPE_REGREG 1268 unspill.To.Offset = int64(ra.Reg2) 1269 } 1270 last = unspill 1271 } 1272 return last 1273 } 1274 1275 // LinkArch is the definition of a single architecture. 1276 type LinkArch struct { 1277 *sys.Arch 1278 Init func(*Link) 1279 ErrorCheck func(*Link, *LSym) 1280 Preprocess func(*Link, *LSym, ProgAlloc) 1281 Assemble func(*Link, *LSym, ProgAlloc) 1282 Progedit func(*Link, *Prog, ProgAlloc) 1283 SEH func(*Link, *LSym) *LSym 1284 UnaryDst map[As]bool // Instruction takes one operand, a destination. 1285 DWARFRegisters map[int16]int16 1286 } 1287