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