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