github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/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 Offset int64 162 Sym *LSym 163 164 // argument value: 165 // for TYPE_SCONST, a string 166 // for TYPE_FCONST, a float64 167 // for TYPE_BRANCH, a *Prog (optional) 168 // for TYPE_TEXTSIZE, an int32 (optional) 169 Val interface{} 170 171 Node interface{} // for use by compiler 172 } 173 174 type AddrType uint8 175 176 const ( 177 NAME_NONE = 0 + iota 178 NAME_EXTERN 179 NAME_STATIC 180 NAME_AUTO 181 NAME_PARAM 182 // A reference to name@GOT(SB) is a reference to the entry in the global offset 183 // table for 'name'. 184 NAME_GOTREF 185 ) 186 187 const ( 188 TYPE_NONE AddrType = 0 189 190 TYPE_BRANCH AddrType = 5 + iota 191 TYPE_TEXTSIZE 192 TYPE_MEM 193 TYPE_CONST 194 TYPE_FCONST 195 TYPE_SCONST 196 TYPE_REG 197 TYPE_ADDR 198 TYPE_SHIFT 199 TYPE_REGREG 200 TYPE_REGREG2 201 TYPE_INDIR 202 TYPE_REGLIST 203 ) 204 205 // Prog describes a single machine instruction. 206 // 207 // The general instruction form is: 208 // 209 // As.Scond From, Reg, From3, To, RegTo2 210 // 211 // where As is an opcode and the others are arguments: 212 // From, Reg, From3 are sources, and To, RegTo2 are destinations. 213 // Usually, not all arguments are present. 214 // For example, MOVL R1, R2 encodes using only As=MOVL, From=R1, To=R2. 215 // The Scond field holds additional condition bits for systems (like arm) 216 // that have generalized conditional execution. 217 // 218 // Jump instructions use the Pcond field to point to the target instruction, 219 // which must be in the same linked list as the jump instruction. 220 // 221 // The Progs for a given function are arranged in a list linked through the Link field. 222 // 223 // Each Prog is charged to a specific source line in the debug information, 224 // specified by Lineno, an index into the line history (see LineHist). 225 // Every Prog has a Ctxt field that defines various context, including the current LineHist. 226 // Progs should be allocated using ctxt.NewProg(), not new(Prog). 227 // 228 // The other fields not yet mentioned are for use by the back ends and should 229 // be left zeroed by creators of Prog lists. 230 type Prog struct { 231 Ctxt *Link // linker context 232 Link *Prog // next Prog in linked list 233 From Addr // first source operand 234 From3 *Addr // third source operand (second is Reg below) 235 To Addr // destination operand (second is RegTo2 below) 236 Pcond *Prog // target of conditional jump 237 Opt interface{} // available to optimization passes to hold per-Prog state 238 Forwd *Prog // for x86 back end 239 Rel *Prog // for x86, arm back ends 240 Pc int64 // for back ends or assembler: virtual or actual program counter, depending on phase 241 Lineno int32 // line number of this instruction 242 Spadj int32 // effect of instruction on stack pointer (increment or decrement amount) 243 As As // assembler opcode 244 Reg int16 // 2nd source operand 245 RegTo2 int16 // 2nd destination operand 246 Mark uint16 // bitmask of arch-specific items 247 Optab uint16 // arch-specific opcode index 248 Scond uint8 // condition bits for conditional instruction (e.g., on ARM) 249 Back uint8 // for x86 back end: backwards branch state 250 Ft uint8 // for x86 back end: type index of Prog.From 251 Tt uint8 // for x86 back end: type index of Prog.To 252 Isize uint8 // for x86 back end: size of the instruction in bytes 253 Mode int8 // for x86 back end: 32- or 64-bit mode 254 } 255 256 // From3Type returns From3.Type, or TYPE_NONE when From3 is nil. 257 func (p *Prog) From3Type() AddrType { 258 if p.From3 == nil { 259 return TYPE_NONE 260 } 261 return p.From3.Type 262 } 263 264 // From3Offset returns From3.Offset, or 0 when From3 is nil. 265 func (p *Prog) From3Offset() int64 { 266 if p.From3 == nil { 267 return 0 268 } 269 return p.From3.Offset 270 } 271 272 // An As denotes an assembler opcode. 273 // There are some portable opcodes, declared here in package obj, 274 // that are common to all architectures. 275 // However, the majority of opcodes are arch-specific 276 // and are declared in their respective architecture's subpackage. 277 type As int16 278 279 // These are the portable opcodes. 280 const ( 281 AXXX As = iota 282 ACALL 283 ADUFFCOPY 284 ADUFFZERO 285 AEND 286 AFUNCDATA 287 AJMP 288 ANOP 289 APCDATA 290 ARET 291 ATEXT 292 ATYPE 293 AUNDEF 294 AUSEFIELD 295 AVARDEF 296 AVARKILL 297 AVARLIVE 298 A_ARCHSPECIFIC 299 ) 300 301 // Each architecture is allotted a distinct subspace of opcode values 302 // for declaring its arch-specific opcodes. 303 // Within this subspace, the first arch-specific opcode should be 304 // at offset A_ARCHSPECIFIC. 305 // 306 // Subspaces are aligned to a power of two so opcodes can be masked 307 // with AMask and used as compact array indices. 308 const ( 309 ABase386 = (1 + iota) << 10 310 ABaseARM 311 ABaseAMD64 312 ABasePPC64 313 ABaseARM64 314 ABaseMIPS64 315 ABaseS390X 316 317 AllowedOpCodes = 1 << 10 // The number of opcodes available for any given architecture. 318 AMask = AllowedOpCodes - 1 // AND with this to use the opcode as an array index. 319 ) 320 321 // An LSym is the sort of symbol that is written to an object file. 322 type LSym struct { 323 Name string 324 Type SymKind 325 Version int16 326 Attribute 327 328 RefIdx int // Index of this symbol in the symbol reference list. 329 Args int32 330 Locals int32 331 Size int64 332 Gotype *LSym 333 Autom *Auto 334 Text *Prog 335 Pcln *Pcln 336 P []byte 337 R []Reloc 338 } 339 340 // Attribute is a set of symbol attributes. 341 type Attribute int16 342 343 const ( 344 AttrDuplicateOK Attribute = 1 << iota 345 AttrCFunc 346 AttrNoSplit 347 AttrLeaf 348 AttrSeenGlobl 349 AttrOnList 350 351 // MakeTypelink means that the type should have an entry in the typelink table. 352 AttrMakeTypelink 353 354 // ReflectMethod means the function may call reflect.Type.Method or 355 // reflect.Type.MethodByName. Matching is imprecise (as reflect.Type 356 // can be used through a custom interface), so ReflectMethod may be 357 // set in some cases when the reflect package is not called. 358 // 359 // Used by the linker to determine what methods can be pruned. 360 AttrReflectMethod 361 362 // Local means make the symbol local even when compiling Go code to reference Go 363 // symbols in other shared libraries, as in this mode symbols are global by 364 // default. "local" here means in the sense of the dynamic linker, i.e. not 365 // visible outside of the module (shared library or executable) that contains its 366 // definition. (When not compiling to support Go shared libraries, all symbols are 367 // local in this sense unless there is a cgo_export_* directive). 368 AttrLocal 369 ) 370 371 func (a Attribute) DuplicateOK() bool { return a&AttrDuplicateOK != 0 } 372 func (a Attribute) MakeTypelink() bool { return a&AttrMakeTypelink != 0 } 373 func (a Attribute) CFunc() bool { return a&AttrCFunc != 0 } 374 func (a Attribute) NoSplit() bool { return a&AttrNoSplit != 0 } 375 func (a Attribute) Leaf() bool { return a&AttrLeaf != 0 } 376 func (a Attribute) SeenGlobl() bool { return a&AttrSeenGlobl != 0 } 377 func (a Attribute) OnList() bool { return a&AttrOnList != 0 } 378 func (a Attribute) ReflectMethod() bool { return a&AttrReflectMethod != 0 } 379 func (a Attribute) Local() bool { return a&AttrLocal != 0 } 380 381 func (a *Attribute) Set(flag Attribute, value bool) { 382 if value { 383 *a |= flag 384 } else { 385 *a &^= flag 386 } 387 } 388 389 // The compiler needs LSym to satisfy fmt.Stringer, because it stores 390 // an LSym in ssa.ExternSymbol. 391 func (s *LSym) String() string { 392 return s.Name 393 } 394 395 type Pcln struct { 396 Pcsp Pcdata 397 Pcfile Pcdata 398 Pcline Pcdata 399 Pcdata []Pcdata 400 Funcdata []*LSym 401 Funcdataoff []int64 402 File []*LSym 403 Lastfile *LSym 404 Lastindex int 405 } 406 407 // A SymKind describes the kind of memory represented by a symbol. 408 type SymKind int16 409 410 // Defined SymKind values. 411 // 412 // TODO(rsc): Give idiomatic Go names. 413 // TODO(rsc): Reduce the number of symbol types in the object files. 414 //go:generate stringer -type=SymKind 415 const ( 416 Sxxx SymKind = iota 417 STEXT 418 SELFRXSECT 419 420 // Read-only sections. 421 STYPE 422 SSTRING 423 SGOSTRING 424 SGOFUNC 425 SGCBITS 426 SRODATA 427 SFUNCTAB 428 429 SELFROSECT 430 SMACHOPLT 431 432 // Read-only sections with relocations. 433 // 434 // Types STYPE-SFUNCTAB above are written to the .rodata section by default. 435 // When linking a shared object, some conceptually "read only" types need to 436 // be written to by relocations and putting them in a section called 437 // ".rodata" interacts poorly with the system linkers. The GNU linkers 438 // support this situation by arranging for sections of the name 439 // ".data.rel.ro.XXX" to be mprotected read only by the dynamic linker after 440 // relocations have applied, so when the Go linker is creating a shared 441 // object it checks all objects of the above types and bumps any object that 442 // has a relocation to it to the corresponding type below, which are then 443 // written to sections with appropriate magic names. 444 STYPERELRO 445 SSTRINGRELRO 446 SGOSTRINGRELRO 447 SGOFUNCRELRO 448 SGCBITSRELRO 449 SRODATARELRO 450 SFUNCTABRELRO 451 452 // Part of .data.rel.ro if it exists, otherwise part of .rodata. 453 STYPELINK 454 SITABLINK 455 SSYMTAB 456 SPCLNTAB 457 458 // Writable sections. 459 SELFSECT 460 SMACHO 461 SMACHOGOT 462 SWINDOWS 463 SELFGOT 464 SNOPTRDATA 465 SINITARR 466 SDATA 467 SBSS 468 SNOPTRBSS 469 STLSBSS 470 SXREF 471 SMACHOSYMSTR 472 SMACHOSYMTAB 473 SMACHOINDIRECTPLT 474 SMACHOINDIRECTGOT 475 SFILE 476 SFILEPATH 477 SCONST 478 SDYNIMPORT 479 SHOSTOBJ 480 SDWARFSECT 481 SDWARFINFO 482 SSUB = SymKind(1 << 8) 483 SMASK = SymKind(SSUB - 1) 484 SHIDDEN = SymKind(1 << 9) 485 SCONTAINER = SymKind(1 << 10) // has a sub-symbol 486 ) 487 488 // ReadOnly are the symbol kinds that form read-only sections. In some 489 // cases, if they will require relocations, they are transformed into 490 // rel-ro sections using RelROMap. 491 var ReadOnly = []SymKind{ 492 STYPE, 493 SSTRING, 494 SGOSTRING, 495 SGOFUNC, 496 SGCBITS, 497 SRODATA, 498 SFUNCTAB, 499 } 500 501 // RelROMap describes the transformation of read-only symbols to rel-ro 502 // symbols. 503 var RelROMap = map[SymKind]SymKind{ 504 STYPE: STYPERELRO, 505 SSTRING: SSTRINGRELRO, 506 SGOSTRING: SGOSTRINGRELRO, 507 SGOFUNC: SGOFUNCRELRO, 508 SGCBITS: SGCBITSRELRO, 509 SRODATA: SRODATARELRO, 510 SFUNCTAB: SFUNCTABRELRO, 511 } 512 513 type Reloc struct { 514 Off int32 515 Siz uint8 516 Type RelocType 517 Add int64 518 Sym *LSym 519 } 520 521 type RelocType int32 522 523 //go:generate stringer -type=RelocType 524 const ( 525 R_ADDR RelocType = 1 + iota 526 // R_ADDRPOWER relocates a pair of "D-form" instructions (instructions with 16-bit 527 // immediates in the low half of the instruction word), usually addis followed by 528 // another add or a load, inserting the "high adjusted" 16 bits of the address of 529 // the referenced symbol into the immediate field of the first instruction and the 530 // low 16 bits into that of the second instruction. 531 R_ADDRPOWER 532 // R_ADDRARM64 relocates an adrp, add pair to compute the address of the 533 // referenced symbol. 534 R_ADDRARM64 535 // R_ADDRMIPS (only used on mips64) resolves to the low 16 bits of an external 536 // address, by encoding it into the instruction. 537 R_ADDRMIPS 538 // R_ADDROFF resolves to a 32-bit offset from the beginning of the section 539 // holding the data being relocated to the referenced symbol. 540 R_ADDROFF 541 R_SIZE 542 R_CALL 543 R_CALLARM 544 R_CALLARM64 545 R_CALLIND 546 R_CALLPOWER 547 // R_CALLMIPS (only used on mips64) resolves to non-PC-relative target address 548 // of a CALL (JAL) instruction, by encoding the address into the instruction. 549 R_CALLMIPS 550 R_CONST 551 R_PCREL 552 // R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the 553 // thread-local symbol from the thread local base and is used to implement the 554 // "local exec" model for tls access (r.Sym is not set on intel platforms but is 555 // set to a TLS symbol -- runtime.tlsg -- in the linker when externally linking). 556 R_TLS_LE 557 // R_TLS_IE, used 386, amd64, and ARM resolves to the PC-relative offset to a GOT 558 // slot containing the offset from the thread-local symbol from the thread local 559 // base and is used to implemented the "initial exec" model for tls access (r.Sym 560 // is not set on intel platforms but is set to a TLS symbol -- runtime.tlsg -- in 561 // the linker when externally linking). 562 R_TLS_IE 563 R_GOTOFF 564 R_PLT0 565 R_PLT1 566 R_PLT2 567 R_USEFIELD 568 // R_USETYPE resolves to an *rtype, but no relocation is created. The 569 // linker uses this as a signal that the pointed-to type information 570 // should be linked into the final binary, even if there are no other 571 // direct references. (This is used for types reachable by reflection.) 572 R_USETYPE 573 // R_METHODOFF resolves to a 32-bit offset from the beginning of the section 574 // holding the data being relocated to the referenced symbol. 575 // It is a variant of R_ADDROFF used when linking from the uncommonType of a 576 // *rtype, and may be set to zero by the linker if it determines the method 577 // text is unreachable by the linked program. 578 R_METHODOFF 579 R_POWER_TOC 580 R_GOTPCREL 581 // R_JMPMIPS (only used on mips64) resolves to non-PC-relative target address 582 // of a JMP instruction, by encoding the address into the instruction. 583 // The stack nosplit check ignores this since it is not a function call. 584 R_JMPMIPS 585 // R_DWARFREF resolves to the offset of the symbol from its section. 586 R_DWARFREF 587 588 // Platform dependent relocations. Architectures with fixed width instructions 589 // have the inherent issue that a 32-bit (or 64-bit!) displacement cannot be 590 // stuffed into a 32-bit instruction, so an address needs to be spread across 591 // several instructions, and in turn this requires a sequence of relocations, each 592 // updating a part of an instruction. This leads to relocation codes that are 593 // inherently processor specific. 594 595 // Arm64. 596 597 // Set a MOV[NZ] immediate field to bits [15:0] of the offset from the thread 598 // local base to the thread local variable defined by the referenced (thread 599 // local) symbol. Error if the offset does not fit into 16 bits. 600 R_ARM64_TLS_LE 601 602 // Relocates an ADRP; LD64 instruction sequence to load the offset between 603 // the thread local base and the thread local variable defined by the 604 // referenced (thread local) symbol from the GOT. 605 R_ARM64_TLS_IE 606 607 // R_ARM64_GOTPCREL relocates an adrp, ld64 pair to compute the address of the GOT 608 // slot of the referenced symbol. 609 R_ARM64_GOTPCREL 610 611 // PPC64. 612 613 // R_POWER_TLS_LE is used to implement the "local exec" model for tls 614 // access. It resolves to the offset of the thread-local symbol from the 615 // thread pointer (R13) and inserts this value into the low 16 bits of an 616 // instruction word. 617 R_POWER_TLS_LE 618 619 // R_POWER_TLS_IE is used to implement the "initial exec" model for tls access. It 620 // relocates a D-form, DS-form instruction sequence like R_ADDRPOWER_DS. It 621 // inserts to the offset of GOT slot for the thread-local symbol from the TOC (the 622 // GOT slot is filled by the dynamic linker with the offset of the thread-local 623 // symbol from the thread pointer (R13)). 624 R_POWER_TLS_IE 625 626 // R_POWER_TLS marks an X-form instruction such as "MOVD 0(R13)(R31*1), g" as 627 // accessing a particular thread-local symbol. It does not affect code generation 628 // but is used by the system linker when relaxing "initial exec" model code to 629 // "local exec" model code. 630 R_POWER_TLS 631 632 // R_ADDRPOWER_DS is similar to R_ADDRPOWER above, but assumes the second 633 // instruction is a "DS-form" instruction, which has an immediate field occupying 634 // bits [15:2] of the instruction word. Bits [15:2] of the address of the 635 // relocated symbol are inserted into this field; it is an error if the last two 636 // bits of the address are not 0. 637 R_ADDRPOWER_DS 638 639 // R_ADDRPOWER_PCREL relocates a D-form, DS-form instruction sequence like 640 // R_ADDRPOWER_DS but inserts the offset of the GOT slot for the referenced symbol 641 // from the TOC rather than the symbol's address. 642 R_ADDRPOWER_GOT 643 644 // R_ADDRPOWER_PCREL relocates two D-form instructions like R_ADDRPOWER, but 645 // inserts the displacement from the place being relocated to the address of the 646 // the relocated symbol instead of just its address. 647 R_ADDRPOWER_PCREL 648 649 // R_ADDRPOWER_TOCREL relocates two D-form instructions like R_ADDRPOWER, but 650 // inserts the offset from the TOC to the address of the the relocated symbol 651 // rather than the symbol's address. 652 R_ADDRPOWER_TOCREL 653 654 // R_ADDRPOWER_TOCREL relocates a D-form, DS-form instruction sequence like 655 // R_ADDRPOWER_DS but inserts the offset from the TOC to the address of the the 656 // relocated symbol rather than the symbol's address. 657 R_ADDRPOWER_TOCREL_DS 658 659 // R_PCRELDBL relocates s390x 2-byte aligned PC-relative addresses. 660 // TODO(mundaym): remove once variants can be serialized - see issue 14218. 661 R_PCRELDBL 662 663 // R_ADDRMIPSU (only used on mips64) resolves to the sign-adjusted "upper" 16 664 // bits (bit 16-31) of an external address, by encoding it into the instruction. 665 R_ADDRMIPSU 666 // R_ADDRMIPSTLS (only used on mips64) resolves to the low 16 bits of a TLS 667 // address (offset from thread pointer), by encoding it into the instruction. 668 R_ADDRMIPSTLS 669 ) 670 671 // IsDirectJump returns whether r is a relocation for a direct jump. 672 // A direct jump is a CALL or JMP instruction that takes the target address 673 // as immediate. The address is embedded into the instruction, possibly 674 // with limited width. 675 // An indirect jump is a CALL or JMP instruction that takes the target address 676 // in register or memory. 677 func (r RelocType) IsDirectJump() bool { 678 switch r { 679 case R_CALL, R_CALLARM, R_CALLARM64, R_CALLPOWER, R_CALLMIPS, R_JMPMIPS: 680 return true 681 } 682 return false 683 } 684 685 type Auto struct { 686 Asym *LSym 687 Link *Auto 688 Aoffset int32 689 Name int16 690 Gotype *LSym 691 } 692 693 // Auto.name 694 const ( 695 A_AUTO = 1 + iota 696 A_PARAM 697 ) 698 699 type Pcdata struct { 700 P []byte 701 } 702 703 // symbol version, incremented each time a file is loaded. 704 // version==1 is reserved for savehist. 705 const ( 706 HistVersion = 1 707 ) 708 709 // Link holds the context for writing object code from a compiler 710 // to be linker input or for reading that input into the linker. 711 type Link struct { 712 Headtype HeadType 713 Arch *LinkArch 714 Debugasm int32 715 Debugvlog int32 716 Debugdivmod int32 717 Debugpcln int32 718 Flag_shared bool 719 Flag_dynlink bool 720 Flag_optimize bool 721 Bso *bufio.Writer 722 Pathname string 723 Hash map[SymVer]*LSym 724 LineHist LineHist 725 Imports []string 726 Plists []*Plist 727 Sym_div *LSym 728 Sym_divu *LSym 729 Sym_mod *LSym 730 Sym_modu *LSym 731 Plan9privates *LSym 732 Curp *Prog 733 Printp *Prog 734 Blitrl *Prog 735 Elitrl *Prog 736 Rexflag int 737 Vexflag int 738 Rep int 739 Repn int 740 Lock int 741 Asmode int 742 AsmBuf AsmBuf // instruction buffer for x86 743 Instoffset int64 744 Autosize int32 745 Armsize int32 746 Pc int64 747 DiagFunc func(string, ...interface{}) 748 Mode int 749 Cursym *LSym 750 Version int 751 Errors int 752 753 Framepointer_enabled bool 754 755 // state for writing objects 756 Text []*LSym 757 Data []*LSym 758 759 // Cache of Progs 760 allocIdx int 761 progs [10000]Prog 762 } 763 764 func (ctxt *Link) Diag(format string, args ...interface{}) { 765 ctxt.Errors++ 766 ctxt.DiagFunc(format, args...) 767 } 768 769 func (ctxt *Link) Logf(format string, args ...interface{}) { 770 fmt.Fprintf(ctxt.Bso, format, args...) 771 ctxt.Bso.Flush() 772 } 773 774 // The smallest possible offset from the hardware stack pointer to a local 775 // variable on the stack. Architectures that use a link register save its value 776 // on the stack in the function prologue and so always have a pointer between 777 // the hardware stack pointer and the local variable area. 778 func (ctxt *Link) FixedFrameSize() int64 { 779 switch ctxt.Arch.Family { 780 case sys.AMD64, sys.I386: 781 return 0 782 case sys.PPC64: 783 // PIC code on ppc64le requires 32 bytes of stack, and it's easier to 784 // just use that much stack always on ppc64x. 785 return int64(4 * ctxt.Arch.PtrSize) 786 default: 787 return int64(ctxt.Arch.PtrSize) 788 } 789 } 790 791 type SymVer struct { 792 Name string 793 Version int // TODO: make int16 to match LSym.Version? 794 } 795 796 // LinkArch is the definition of a single architecture. 797 type LinkArch struct { 798 *sys.Arch 799 Preprocess func(*Link, *LSym) 800 Assemble func(*Link, *LSym) 801 Follow func(*Link, *LSym) 802 Progedit func(*Link, *Prog) 803 UnaryDst map[As]bool // Instruction takes one operand, a destination. 804 } 805 806 // HeadType is the executable header type. 807 type HeadType uint8 808 809 const ( 810 Hunknown HeadType = iota 811 Hdarwin 812 Hdragonfly 813 Hfreebsd 814 Hlinux 815 Hnacl 816 Hnetbsd 817 Hopenbsd 818 Hplan9 819 Hsolaris 820 Hwindows 821 Hwindowsgui 822 ) 823 824 func (h *HeadType) Set(s string) error { 825 switch s { 826 case "darwin": 827 *h = Hdarwin 828 case "dragonfly": 829 *h = Hdragonfly 830 case "freebsd": 831 *h = Hfreebsd 832 case "linux", "android": 833 *h = Hlinux 834 case "nacl": 835 *h = Hnacl 836 case "netbsd": 837 *h = Hnetbsd 838 case "openbsd": 839 *h = Hopenbsd 840 case "plan9": 841 *h = Hplan9 842 case "solaris": 843 *h = Hsolaris 844 case "windows": 845 *h = Hwindows 846 case "windowsgui": 847 *h = Hwindowsgui 848 default: 849 return fmt.Errorf("invalid headtype: %q", s) 850 } 851 return nil 852 } 853 854 func (h *HeadType) String() string { 855 switch *h { 856 case Hdarwin: 857 return "darwin" 858 case Hdragonfly: 859 return "dragonfly" 860 case Hfreebsd: 861 return "freebsd" 862 case Hlinux: 863 return "linux" 864 case Hnacl: 865 return "nacl" 866 case Hnetbsd: 867 return "netbsd" 868 case Hopenbsd: 869 return "openbsd" 870 case Hplan9: 871 return "plan9" 872 case Hsolaris: 873 return "solaris" 874 case Hwindows: 875 return "windows" 876 case Hwindowsgui: 877 return "windowsgui" 878 } 879 return fmt.Sprintf("HeadType(%d)", *h) 880 } 881 882 // AsmBuf is a simple buffer to assemble variable-length x86 instructions into. 883 type AsmBuf struct { 884 buf [100]byte 885 off int 886 } 887 888 // Put1 appends one byte to the end of the buffer. 889 func (a *AsmBuf) Put1(x byte) { 890 a.buf[a.off] = x 891 a.off++ 892 } 893 894 // Put2 appends two bytes to the end of the buffer. 895 func (a *AsmBuf) Put2(x, y byte) { 896 a.buf[a.off+0] = x 897 a.buf[a.off+1] = y 898 a.off += 2 899 } 900 901 // Put3 appends three bytes to the end of the buffer. 902 func (a *AsmBuf) Put3(x, y, z byte) { 903 a.buf[a.off+0] = x 904 a.buf[a.off+1] = y 905 a.buf[a.off+2] = z 906 a.off += 3 907 } 908 909 // Put4 appends four bytes to the end of the buffer. 910 func (a *AsmBuf) Put4(x, y, z, w byte) { 911 a.buf[a.off+0] = x 912 a.buf[a.off+1] = y 913 a.buf[a.off+2] = z 914 a.buf[a.off+3] = w 915 a.off += 4 916 } 917 918 // PutInt16 writes v into the buffer using little-endian encoding. 919 func (a *AsmBuf) PutInt16(v int16) { 920 a.buf[a.off+0] = byte(v) 921 a.buf[a.off+1] = byte(v >> 8) 922 a.off += 2 923 } 924 925 // PutInt32 writes v into the buffer using little-endian encoding. 926 func (a *AsmBuf) PutInt32(v int32) { 927 a.buf[a.off+0] = byte(v) 928 a.buf[a.off+1] = byte(v >> 8) 929 a.buf[a.off+2] = byte(v >> 16) 930 a.buf[a.off+3] = byte(v >> 24) 931 a.off += 4 932 } 933 934 // PutInt64 writes v into the buffer using little-endian encoding. 935 func (a *AsmBuf) PutInt64(v int64) { 936 a.buf[a.off+0] = byte(v) 937 a.buf[a.off+1] = byte(v >> 8) 938 a.buf[a.off+2] = byte(v >> 16) 939 a.buf[a.off+3] = byte(v >> 24) 940 a.buf[a.off+4] = byte(v >> 32) 941 a.buf[a.off+5] = byte(v >> 40) 942 a.buf[a.off+6] = byte(v >> 48) 943 a.buf[a.off+7] = byte(v >> 56) 944 a.off += 8 945 } 946 947 // Put copies b into the buffer. 948 func (a *AsmBuf) Put(b []byte) { 949 copy(a.buf[a.off:], b) 950 a.off += len(b) 951 } 952 953 // Insert inserts b at offset i. 954 func (a *AsmBuf) Insert(i int, b byte) { 955 a.off++ 956 copy(a.buf[i+1:a.off], a.buf[i:a.off-1]) 957 a.buf[i] = b 958 } 959 960 // Last returns the byte at the end of the buffer. 961 func (a *AsmBuf) Last() byte { return a.buf[a.off-1] } 962 963 // Len returns the length of the buffer. 964 func (a *AsmBuf) Len() int { return a.off } 965 966 // Bytes returns the contents of the buffer. 967 func (a *AsmBuf) Bytes() []byte { return a.buf[:a.off] } 968 969 // Reset empties the buffer. 970 func (a *AsmBuf) Reset() { a.off = 0 } 971 972 // Peek returns the byte at offset i. 973 func (a *AsmBuf) Peek(i int) byte { return a.buf[i] }