github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/internal/obj/link.go (about) 1 // Derived from Inferno utils/6l/l.h and related files. 2 // https://bitbucket.org/inferno-os/inferno-os/src/default/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/sys" 36 "fmt" 37 ) 38 39 // An Addr is an argument to an instruction. 40 // The general forms and their encodings are: 41 // 42 // sym±offset(symkind)(reg)(index*scale) 43 // Memory reference at address &sym(symkind) + offset + reg + index*scale. 44 // Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted. 45 // If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg). 46 // To force a parsing as index*scale, write (index*1). 47 // Encoding: 48 // type = TYPE_MEM 49 // name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE) 50 // sym = sym 51 // offset = ±offset 52 // reg = reg (REG_*) 53 // index = index (REG_*) 54 // scale = scale (1, 2, 4, 8) 55 // 56 // $<mem> 57 // Effective address of memory reference <mem>, defined above. 58 // Encoding: same as memory reference, but type = TYPE_ADDR. 59 // 60 // $<±integer value> 61 // This is a special case of $<mem>, in which only ±offset is present. 62 // It has a separate type for easy recognition. 63 // Encoding: 64 // type = TYPE_CONST 65 // offset = ±integer value 66 // 67 // *<mem> 68 // Indirect reference through memory reference <mem>, defined above. 69 // Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function 70 // pointer stored in the data word sym(SB), not a function named sym(SB). 71 // Encoding: same as above, but type = TYPE_INDIR. 72 // 73 // $*$<mem> 74 // No longer used. 75 // On machines with actual SB registers, $*$<mem> forced the 76 // instruction encoding to use a full 32-bit constant, never a 77 // reference relative to SB. 78 // 79 // $<floating point literal> 80 // Floating point constant value. 81 // Encoding: 82 // type = TYPE_FCONST 83 // val = floating point value 84 // 85 // $<string literal, up to 8 chars> 86 // String literal value (raw bytes used for DATA instruction). 87 // Encoding: 88 // type = TYPE_SCONST 89 // val = string 90 // 91 // <register name> 92 // Any register: integer, floating point, control, segment, and so on. 93 // If looking for specific register kind, must check type and reg value range. 94 // Encoding: 95 // type = TYPE_REG 96 // reg = reg (REG_*) 97 // 98 // x(PC) 99 // Encoding: 100 // type = TYPE_BRANCH 101 // val = Prog* reference OR ELSE offset = target pc (branch takes priority) 102 // 103 // $±x-±y 104 // Final argument to TEXT, specifying local frame size x and argument size y. 105 // In this form, x and y are integer literals only, not arbitrary expressions. 106 // This avoids parsing ambiguities due to the use of - as a separator. 107 // The ± are optional. 108 // If the final argument to TEXT omits the -±y, the encoding should still 109 // use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown. 110 // Encoding: 111 // type = TYPE_TEXTSIZE 112 // offset = x 113 // val = int32(y) 114 // 115 // reg<<shift, reg>>shift, reg->shift, reg@>shift 116 // Shifted register value, for ARM and ARM64. 117 // In this form, reg must be a register and shift can be a register or an integer constant. 118 // Encoding: 119 // type = TYPE_SHIFT 120 // On ARM: 121 // offset = (reg&15) | shifttype<<5 | count 122 // shifttype = 0, 1, 2, 3 for <<, >>, ->, @> 123 // count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant. 124 // On ARM64: 125 // offset = (reg&31)<<16 | shifttype<<22 | (count&63)<<10 126 // shifttype = 0, 1, 2 for <<, >>, -> 127 // 128 // (reg, reg) 129 // A destination register pair. When used as the last argument of an instruction, 130 // this form makes clear that both registers are destinations. 131 // Encoding: 132 // type = TYPE_REGREG 133 // reg = first register 134 // offset = second register 135 // 136 // [reg, reg, reg-reg] 137 // Register list for ARM. 138 // Encoding: 139 // type = TYPE_REGLIST 140 // offset = bit mask of registers in list; R0 is low bit. 141 // 142 // reg, reg 143 // Register pair for ARM. 144 // TYPE_REGREG2 145 // 146 // (reg+reg) 147 // Register pair for PPC64. 148 // Encoding: 149 // type = TYPE_MEM 150 // reg = first register 151 // index = second register 152 // scale = 1 153 // 154 type Addr struct { 155 Reg int16 156 Index int16 157 Scale int16 // Sometimes holds a register. 158 Type AddrType 159 Name int8 160 Class int8 161 Etype uint8 162 Offset int64 163 Width int64 164 Sym *LSym 165 Gotype *LSym 166 167 // argument value: 168 // for TYPE_SCONST, a string 169 // for TYPE_FCONST, a float64 170 // for TYPE_BRANCH, a *Prog (optional) 171 // for TYPE_TEXTSIZE, an int32 (optional) 172 Val interface{} 173 174 Node interface{} // for use by compiler 175 } 176 177 type AddrType uint8 178 179 const ( 180 NAME_NONE = 0 + iota 181 NAME_EXTERN 182 NAME_STATIC 183 NAME_AUTO 184 NAME_PARAM 185 // A reference to name@GOT(SB) is a reference to the entry in the global offset 186 // table for 'name'. 187 NAME_GOTREF 188 ) 189 190 const ( 191 TYPE_NONE AddrType = 0 192 193 TYPE_BRANCH AddrType = 5 + iota 194 TYPE_TEXTSIZE 195 TYPE_MEM 196 TYPE_CONST 197 TYPE_FCONST 198 TYPE_SCONST 199 TYPE_REG 200 TYPE_ADDR 201 TYPE_SHIFT 202 TYPE_REGREG 203 TYPE_REGREG2 204 TYPE_INDIR 205 TYPE_REGLIST 206 ) 207 208 // TODO(rsc): Describe prog. 209 // TODO(rsc): Describe TEXT/GLOBL flag in from3 210 type Prog struct { 211 Ctxt *Link 212 Link *Prog 213 From Addr 214 From3 *Addr // optional 215 To Addr 216 Opt interface{} 217 Forwd *Prog 218 Pcond *Prog 219 Rel *Prog // Source of forward jumps on x86; pcrel on arm 220 Pc int64 221 Lineno int32 222 Spadj int32 223 As As // Assembler opcode. 224 Reg int16 225 RegTo2 int16 // 2nd register output operand 226 Mark uint16 // bitmask of arch-specific items 227 Optab uint16 228 Scond uint8 229 Back uint8 230 Ft uint8 231 Tt uint8 232 Isize uint8 // size of the instruction in bytes (x86 only) 233 Mode int8 234 235 Info ProgInfo 236 } 237 238 // From3Type returns From3.Type, or TYPE_NONE when From3 is nil. 239 func (p *Prog) From3Type() AddrType { 240 if p.From3 == nil { 241 return TYPE_NONE 242 } 243 return p.From3.Type 244 } 245 246 // From3Offset returns From3.Offset, or 0 when From3 is nil. 247 func (p *Prog) From3Offset() int64 { 248 if p.From3 == nil { 249 return 0 250 } 251 return p.From3.Offset 252 } 253 254 // ProgInfo holds information about the instruction for use 255 // by clients such as the compiler. The exact meaning of this 256 // data is up to the client and is not interpreted by the cmd/internal/obj/... packages. 257 type ProgInfo struct { 258 _ struct{} // to prevent unkeyed literals. Trailing zero-sized field will take space. 259 Flags uint32 // flag bits 260 Reguse uint64 // registers implicitly used by this instruction 261 Regset uint64 // registers implicitly set by this instruction 262 Regindex uint64 // registers used by addressing mode 263 } 264 265 // An As denotes an assembler opcode. 266 // There are some portable opcodes, declared here in package obj, 267 // that are common to all architectures. 268 // However, the majority of opcodes are arch-specific 269 // and are declared in their respective architecture's subpackage. 270 type As int16 271 272 // These are the portable opcodes. 273 const ( 274 AXXX As = iota 275 ACALL 276 ACHECKNIL 277 ADUFFCOPY 278 ADUFFZERO 279 AEND 280 AFUNCDATA 281 AGLOBL 282 AJMP 283 ANOP 284 APCDATA 285 ARET 286 ATEXT 287 ATYPE 288 AUNDEF 289 AUSEFIELD 290 AVARDEF 291 AVARKILL 292 AVARLIVE 293 A_ARCHSPECIFIC 294 ) 295 296 // Each architecture is allotted a distinct subspace of opcode values 297 // for declaring its arch-specific opcodes. 298 // Within this subspace, the first arch-specific opcode should be 299 // at offset A_ARCHSPECIFIC. 300 // 301 // Subspaces are aligned to a power of two so opcodes can be masked 302 // with AMask and used as compact array indices. 303 const ( 304 ABase386 = (1 + iota) << 10 305 ABaseARM 306 ABaseAMD64 307 ABasePPC64 308 ABaseARM64 309 ABaseMIPS64 310 ABaseS390X 311 312 AllowedOpCodes = 1 << 10 // The number of opcodes available for any given architecture. 313 AMask = AllowedOpCodes - 1 // AND with this to use the opcode as an array index. 314 ) 315 316 // An LSym is the sort of symbol that is written to an object file. 317 type LSym struct { 318 Name string 319 Type int16 320 Version int16 321 Dupok bool 322 Cfunc bool 323 Nosplit bool 324 Leaf bool 325 Seenglobl bool 326 Onlist bool 327 328 // ReflectMethod means the function may call reflect.Type.Method or 329 // reflect.Type.MethodByName. Matching is imprecise (as reflect.Type 330 // can be used through a custom interface), so ReflectMethod may be 331 // set in some cases when the reflect package is not called. 332 // 333 // Used by the linker to determine what methods can be pruned. 334 ReflectMethod bool 335 336 // Local means make the symbol local even when compiling Go code to reference Go 337 // symbols in other shared libraries, as in this mode symbols are global by 338 // default. "local" here means in the sense of the dynamic linker, i.e. not 339 // visible outside of the module (shared library or executable) that contains its 340 // definition. (When not compiling to support Go shared libraries, all symbols are 341 // local in this sense unless there is a cgo_export_* directive). 342 Local bool 343 344 RefIdx int // Index of this symbol in the symbol reference list. 345 Args int32 346 Locals int32 347 Size int64 348 Gotype *LSym 349 Autom *Auto 350 Text *Prog 351 Pcln *Pcln 352 P []byte 353 R []Reloc 354 } 355 356 // The compiler needs LSym to satisfy fmt.Stringer, because it stores 357 // an LSym in ssa.ExternSymbol. 358 func (s *LSym) String() string { 359 return s.Name 360 } 361 362 type Pcln struct { 363 Pcsp Pcdata 364 Pcfile Pcdata 365 Pcline Pcdata 366 Pcdata []Pcdata 367 Funcdata []*LSym 368 Funcdataoff []int64 369 File []*LSym 370 Lastfile *LSym 371 Lastindex int 372 } 373 374 // LSym.type 375 const ( 376 Sxxx = iota 377 STEXT 378 SELFRXSECT 379 380 STYPE 381 SSTRING 382 SGOSTRING 383 SGOSTRINGHDR 384 SGOFUNC 385 SGCBITS 386 SRODATA 387 SFUNCTAB 388 389 // Types STYPE-SFUNCTAB above are written to the .rodata section by default. 390 // When linking a shared object, some conceptually "read only" types need to 391 // be written to by relocations and putting them in a section called 392 // ".rodata" interacts poorly with the system linkers. The GNU linkers 393 // support this situation by arranging for sections of the name 394 // ".data.rel.ro.XXX" to be mprotected read only by the dynamic linker after 395 // relocations have applied, so when the Go linker is creating a shared 396 // object it checks all objects of the above types and bumps any object that 397 // has a relocation to it to the corresponding type below, which are then 398 // written to sections with appropriate magic names. 399 STYPERELRO 400 SSTRINGRELRO 401 SGOSTRINGRELRO 402 SGOSTRINGHDRRELRO 403 SGOFUNCRELRO 404 SGCBITSRELRO 405 SRODATARELRO 406 SFUNCTABRELRO 407 408 STYPELINK 409 SITABLINK 410 SSYMTAB 411 SPCLNTAB 412 SELFROSECT 413 SMACHOPLT 414 SELFSECT 415 SMACHO 416 SMACHOGOT 417 SWINDOWS 418 SELFGOT 419 SNOPTRDATA 420 SINITARR 421 SDATA 422 SBSS 423 SNOPTRBSS 424 STLSBSS 425 SXREF 426 SMACHOSYMSTR 427 SMACHOSYMTAB 428 SMACHOINDIRECTPLT 429 SMACHOINDIRECTGOT 430 SFILE 431 SFILEPATH 432 SCONST 433 SDYNIMPORT 434 SHOSTOBJ 435 SDWARFSECT 436 SDWARFINFO 437 SSUB = 1 << 8 438 SMASK = SSUB - 1 439 SHIDDEN = 1 << 9 440 SCONTAINER = 1 << 10 // has a sub-symbol 441 ) 442 443 type Reloc struct { 444 Off int32 445 Siz uint8 446 Type RelocType 447 Add int64 448 Sym *LSym 449 } 450 451 type RelocType int32 452 453 //go:generate stringer -type=RelocType 454 const ( 455 R_ADDR RelocType = 1 + iota 456 // R_ADDRPOWER relocates a pair of "D-form" instructions (instructions with 16-bit 457 // immediates in the low half of the instruction word), usually addis followed by 458 // another add or a load, inserting the "high adjusted" 16 bits of the address of 459 // the referenced symbol into the immediate field of the first instruction and the 460 // low 16 bits into that of the second instruction. 461 R_ADDRPOWER 462 // R_ADDRARM64 relocates an adrp, add pair to compute the address of the 463 // referenced symbol. 464 R_ADDRARM64 465 // R_ADDRMIPS (only used on mips64) resolves to the low 16 bits of an external 466 // address, by encoding it into the instruction. 467 R_ADDRMIPS 468 // R_ADDROFF resolves to a 32-bit offset from the beginning of the section 469 // holding the data being relocated to the referenced symbol. 470 R_ADDROFF 471 R_SIZE 472 R_CALL 473 R_CALLARM 474 R_CALLARM64 475 R_CALLIND 476 R_CALLPOWER 477 // R_CALLMIPS (only used on mips64) resolves to non-PC-relative target address 478 // of a CALL (JAL) instruction, by encoding the address into the instruction. 479 R_CALLMIPS 480 R_CONST 481 R_PCREL 482 // R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the 483 // thread-local symbol from the thread local base and is used to implement the 484 // "local exec" model for tls access (r.Sym is not set on intel platforms but is 485 // set to a TLS symbol -- runtime.tlsg -- in the linker when externally linking). 486 R_TLS_LE 487 // R_TLS_IE, used 386, amd64, and ARM resolves to the PC-relative offset to a GOT 488 // slot containing the offset from the thread-local symbol from the thread local 489 // base and is used to implemented the "initial exec" model for tls access (r.Sym 490 // is not set on intel platforms but is set to a TLS symbol -- runtime.tlsg -- in 491 // the linker when externally linking). 492 R_TLS_IE 493 R_GOTOFF 494 R_PLT0 495 R_PLT1 496 R_PLT2 497 R_USEFIELD 498 // R_USETYPE resolves to an *rtype, but no relocation is created. The 499 // linker uses this as a signal that the pointed-to type information 500 // should be linked into the final binary, even if there are no other 501 // direct references. (This is used for types reachable by reflection.) 502 R_USETYPE 503 // R_METHODOFF resolves to a 32-bit offset from the beginning of the section 504 // holding the data being relocated to the referenced symbol. 505 // It is a variant of R_ADDROFF used when linking from the uncommonType of a 506 // *rtype, and may be set to zero by the linker if it determines the method 507 // text is unreachable by the linked program. 508 R_METHODOFF 509 R_POWER_TOC 510 R_GOTPCREL 511 // R_JMPMIPS (only used on mips64) resolves to non-PC-relative target address 512 // of a JMP instruction, by encoding the address into the instruction. 513 // The stack nosplit check ignores this since it is not a function call. 514 R_JMPMIPS 515 // R_DWARFREF resolves to the offset of the symbol from its section. 516 R_DWARFREF 517 518 // Platform dependent relocations. Architectures with fixed width instructions 519 // have the inherent issue that a 32-bit (or 64-bit!) displacement cannot be 520 // stuffed into a 32-bit instruction, so an address needs to be spread across 521 // several instructions, and in turn this requires a sequence of relocations, each 522 // updating a part of an instruction. This leads to relocation codes that are 523 // inherently processor specific. 524 525 // Arm64. 526 527 // Set a MOV[NZ] immediate field to bits [15:0] of the offset from the thread 528 // local base to the thread local variable defined by the referenced (thread 529 // local) symbol. Error if the offset does not fit into 16 bits. 530 R_ARM64_TLS_LE 531 532 // Relocates an ADRP; LD64 instruction sequence to load the offset between 533 // the thread local base and the thread local variable defined by the 534 // referenced (thread local) symbol from the GOT. 535 R_ARM64_TLS_IE 536 537 // R_ARM64_GOTPCREL relocates an adrp, ld64 pair to compute the address of the GOT 538 // slot of the referenced symbol. 539 R_ARM64_GOTPCREL 540 541 // PPC64. 542 543 // R_POWER_TLS_LE is used to implement the "local exec" model for tls 544 // access. It resolves to the offset of the thread-local symbol from the 545 // thread pointer (R13) and inserts this value into the low 16 bits of an 546 // instruction word. 547 R_POWER_TLS_LE 548 549 // R_POWER_TLS_IE is used to implement the "initial exec" model for tls access. It 550 // relocates a D-form, DS-form instruction sequence like R_ADDRPOWER_DS. It 551 // inserts to the offset of GOT slot for the thread-local symbol from the TOC (the 552 // GOT slot is filled by the dynamic linker with the offset of the thread-local 553 // symbol from the thread pointer (R13)). 554 R_POWER_TLS_IE 555 556 // R_POWER_TLS marks an X-form instruction such as "MOVD 0(R13)(R31*1), g" as 557 // accessing a particular thread-local symbol. It does not affect code generation 558 // but is used by the system linker when relaxing "initial exec" model code to 559 // "local exec" model code. 560 R_POWER_TLS 561 562 // R_ADDRPOWER_DS is similar to R_ADDRPOWER above, but assumes the second 563 // instruction is a "DS-form" instruction, which has an immediate field occupying 564 // bits [15:2] of the instruction word. Bits [15:2] of the address of the 565 // relocated symbol are inserted into this field; it is an error if the last two 566 // bits of the address are not 0. 567 R_ADDRPOWER_DS 568 569 // R_ADDRPOWER_PCREL relocates a D-form, DS-form instruction sequence like 570 // R_ADDRPOWER_DS but inserts the offset of the GOT slot for the referenced symbol 571 // from the TOC rather than the symbol's address. 572 R_ADDRPOWER_GOT 573 574 // R_ADDRPOWER_PCREL relocates two D-form instructions like R_ADDRPOWER, but 575 // inserts the displacement from the place being relocated to the address of the 576 // the relocated symbol instead of just its address. 577 R_ADDRPOWER_PCREL 578 579 // R_ADDRPOWER_TOCREL relocates two D-form instructions like R_ADDRPOWER, but 580 // inserts the offset from the TOC to the address of the the relocated symbol 581 // rather than the symbol's address. 582 R_ADDRPOWER_TOCREL 583 584 // R_ADDRPOWER_TOCREL relocates a D-form, DS-form instruction sequence like 585 // R_ADDRPOWER_DS but inserts the offset from the TOC to the address of the the 586 // relocated symbol rather than the symbol's address. 587 R_ADDRPOWER_TOCREL_DS 588 589 // R_PCRELDBL relocates s390x 2-byte aligned PC-relative addresses. 590 // TODO(mundaym): remove once variants can be serialized - see issue 14218. 591 R_PCRELDBL 592 593 // R_ADDRMIPSU (only used on mips64) resolves to the sign-adjusted "upper" 16 594 // bits (bit 16-31) of an external address, by encoding it into the instruction. 595 R_ADDRMIPSU 596 // R_ADDRMIPSTLS (only used on mips64) resolves to the low 16 bits of a TLS 597 // address (offset from thread pointer), by encoding it into the instruction. 598 R_ADDRMIPSTLS 599 ) 600 601 type Auto struct { 602 Asym *LSym 603 Link *Auto 604 Aoffset int32 605 Name int16 606 Gotype *LSym 607 } 608 609 // Auto.name 610 const ( 611 A_AUTO = 1 + iota 612 A_PARAM 613 ) 614 615 type Pcdata struct { 616 P []byte 617 } 618 619 // symbol version, incremented each time a file is loaded. 620 // version==1 is reserved for savehist. 621 const ( 622 HistVersion = 1 623 ) 624 625 // Link holds the context for writing object code from a compiler 626 // to be linker input or for reading that input into the linker. 627 type Link struct { 628 Goarm int32 629 Headtype int 630 Arch *LinkArch 631 Debugasm int32 632 Debugvlog int32 633 Debugdivmod int32 634 Debugpcln int32 635 Flag_shared bool 636 Flag_dynlink bool 637 Flag_optimize bool 638 Bso *bufio.Writer 639 Pathname string 640 Goroot string 641 Goroot_final string 642 Hash map[SymVer]*LSym 643 LineHist LineHist 644 Imports []string 645 Plist *Plist 646 Plast *Plist 647 Sym_div *LSym 648 Sym_divu *LSym 649 Sym_mod *LSym 650 Sym_modu *LSym 651 Plan9privates *LSym 652 Curp *Prog 653 Printp *Prog 654 Blitrl *Prog 655 Elitrl *Prog 656 Rexflag int 657 Vexflag int 658 Rep int 659 Repn int 660 Lock int 661 Asmode int 662 AsmBuf AsmBuf // instruction buffer for x86 663 Instoffset int64 664 Autosize int32 665 Armsize int32 666 Pc int64 667 DiagFunc func(string, ...interface{}) 668 Mode int 669 Cursym *LSym 670 Version int 671 Textp *LSym 672 Etextp *LSym 673 Errors int 674 675 Framepointer_enabled bool 676 677 // state for writing objects 678 Text []*LSym 679 Data []*LSym 680 681 // Cache of Progs 682 allocIdx int 683 progs [10000]Prog 684 } 685 686 func (ctxt *Link) Diag(format string, args ...interface{}) { 687 ctxt.Errors++ 688 ctxt.DiagFunc(format, args...) 689 } 690 691 func (ctxt *Link) Logf(format string, args ...interface{}) { 692 fmt.Fprintf(ctxt.Bso, format, args...) 693 ctxt.Bso.Flush() 694 } 695 696 // The smallest possible offset from the hardware stack pointer to a local 697 // variable on the stack. Architectures that use a link register save its value 698 // on the stack in the function prologue and so always have a pointer between 699 // the hardware stack pointer and the local variable area. 700 func (ctxt *Link) FixedFrameSize() int64 { 701 switch ctxt.Arch.Family { 702 case sys.AMD64, sys.I386: 703 return 0 704 case sys.PPC64: 705 // PIC code on ppc64le requires 32 bytes of stack, and it's easier to 706 // just use that much stack always on ppc64x. 707 return int64(4 * ctxt.Arch.PtrSize) 708 default: 709 return int64(ctxt.Arch.PtrSize) 710 } 711 } 712 713 type SymVer struct { 714 Name string 715 Version int // TODO: make int16 to match LSym.Version? 716 } 717 718 // LinkArch is the definition of a single architecture. 719 type LinkArch struct { 720 *sys.Arch 721 Preprocess func(*Link, *LSym) 722 Assemble func(*Link, *LSym) 723 Follow func(*Link, *LSym) 724 Progedit func(*Link, *Prog) 725 UnaryDst map[As]bool // Instruction takes one operand, a destination. 726 } 727 728 /* executable header types */ 729 const ( 730 Hunknown = 0 + iota 731 Hdarwin 732 Hdragonfly 733 Hfreebsd 734 Hlinux 735 Hnacl 736 Hnetbsd 737 Hopenbsd 738 Hplan9 739 Hsolaris 740 Hwindows 741 ) 742 743 // AsmBuf is a simple buffer to assemble variable-length x86 instructions into. 744 type AsmBuf struct { 745 buf [100]byte 746 off int 747 } 748 749 // Put1 appends one byte to the end of the buffer. 750 func (a *AsmBuf) Put1(x byte) { 751 a.buf[a.off] = x 752 a.off++ 753 } 754 755 // Put2 appends two bytes to the end of the buffer. 756 func (a *AsmBuf) Put2(x, y byte) { 757 a.buf[a.off+0] = x 758 a.buf[a.off+1] = y 759 a.off += 2 760 } 761 762 // Put3 appends three bytes to the end of the buffer. 763 func (a *AsmBuf) Put3(x, y, z byte) { 764 a.buf[a.off+0] = x 765 a.buf[a.off+1] = y 766 a.buf[a.off+2] = z 767 a.off += 3 768 } 769 770 // Put4 appends four bytes to the end of the buffer. 771 func (a *AsmBuf) Put4(x, y, z, w byte) { 772 a.buf[a.off+0] = x 773 a.buf[a.off+1] = y 774 a.buf[a.off+2] = z 775 a.buf[a.off+3] = w 776 a.off += 4 777 } 778 779 // PutInt16 writes v into the buffer using little-endian encoding. 780 func (a *AsmBuf) PutInt16(v int16) { 781 a.buf[a.off+0] = byte(v) 782 a.buf[a.off+1] = byte(v >> 8) 783 a.off += 2 784 } 785 786 // PutInt32 writes v into the buffer using little-endian encoding. 787 func (a *AsmBuf) PutInt32(v int32) { 788 a.buf[a.off+0] = byte(v) 789 a.buf[a.off+1] = byte(v >> 8) 790 a.buf[a.off+2] = byte(v >> 16) 791 a.buf[a.off+3] = byte(v >> 24) 792 a.off += 4 793 } 794 795 // PutInt64 writes v into the buffer using little-endian encoding. 796 func (a *AsmBuf) PutInt64(v int64) { 797 a.buf[a.off+0] = byte(v) 798 a.buf[a.off+1] = byte(v >> 8) 799 a.buf[a.off+2] = byte(v >> 16) 800 a.buf[a.off+3] = byte(v >> 24) 801 a.buf[a.off+4] = byte(v >> 32) 802 a.buf[a.off+5] = byte(v >> 40) 803 a.buf[a.off+6] = byte(v >> 48) 804 a.buf[a.off+7] = byte(v >> 56) 805 a.off += 8 806 } 807 808 // Put copies b into the buffer. 809 func (a *AsmBuf) Put(b []byte) { 810 copy(a.buf[a.off:], b) 811 a.off += len(b) 812 } 813 814 // Insert inserts b at offset i. 815 func (a *AsmBuf) Insert(i int, b byte) { 816 a.off++ 817 copy(a.buf[i+1:a.off], a.buf[i:a.off-1]) 818 a.buf[i] = b 819 } 820 821 // Last returns the byte at the end of the buffer. 822 func (a *AsmBuf) Last() byte { return a.buf[a.off-1] } 823 824 // Len returns the length of the buffer. 825 func (a *AsmBuf) Len() int { return a.off } 826 827 // Bytes returns the contents of the buffer. 828 func (a *AsmBuf) Bytes() []byte { return a.buf[:a.off] } 829 830 // Reset empties the buffer. 831 func (a *AsmBuf) Reset() { a.off = 0 } 832 833 // Peek returns the byte at offset i. 834 func (a *AsmBuf) Peek(i int) byte { return a.buf[i] }