github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/cmd/internal/obj/link.go (about) 1 // Derived from Inferno utils/6l/l.h and related files. 2 // http://code.google.com/p/inferno-os/source/browse/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 "encoding/binary" 34 35 // An Addr is an argument to an instruction. 36 // The general forms and their encodings are: 37 // 38 // sym±offset(symkind)(reg)(index*scale) 39 // Memory reference at address &sym(symkind) + offset + reg + index*scale. 40 // Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted. 41 // If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg). 42 // To force a parsing as index*scale, write (index*1). 43 // Encoding: 44 // type = TYPE_MEM 45 // name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE) 46 // sym = sym 47 // offset = ±offset 48 // reg = reg (REG_*) 49 // index = index (REG_*) 50 // scale = scale (1, 2, 4, 8) 51 // 52 // $<mem> 53 // Effective address of memory reference <mem>, defined above. 54 // Encoding: same as memory reference, but type = TYPE_ADDR. 55 // 56 // $<±integer value> 57 // This is a special case of $<mem>, in which only ±offset is present. 58 // It has a separate type for easy recognition. 59 // Encoding: 60 // type = TYPE_CONST 61 // offset = ±integer value 62 // 63 // *<mem> 64 // Indirect reference through memory reference <mem>, defined above. 65 // Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function 66 // pointer stored in the data word sym(SB), not a function named sym(SB). 67 // Encoding: same as above, but type = TYPE_INDIR. 68 // 69 // $*$<mem> 70 // No longer used. 71 // On machines with actual SB registers, $*$<mem> forced the 72 // instruction encoding to use a full 32-bit constant, never a 73 // reference relative to SB. 74 // 75 // $<floating point literal> 76 // Floating point constant value. 77 // Encoding: 78 // type = TYPE_FCONST 79 // val = floating point value 80 // 81 // $<string literal, up to 8 chars> 82 // String literal value (raw bytes used for DATA instruction). 83 // Encoding: 84 // type = TYPE_SCONST 85 // val = string 86 // 87 // <register name> 88 // Any register: integer, floating point, control, segment, and so on. 89 // If looking for specific register kind, must check type and reg value range. 90 // Encoding: 91 // type = TYPE_REG 92 // reg = reg (REG_*) 93 // 94 // x(PC) 95 // Encoding: 96 // type = TYPE_BRANCH 97 // val = Prog* reference OR ELSE offset = target pc (branch takes priority) 98 // 99 // $±x-±y 100 // Final argument to TEXT, specifying local frame size x and argument size y. 101 // In this form, x and y are integer literals only, not arbitrary expressions. 102 // This avoids parsing ambiguities due to the use of - as a separator. 103 // The ± are optional. 104 // If the final argument to TEXT omits the -±y, the encoding should still 105 // use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown. 106 // Encoding: 107 // type = TYPE_TEXTSIZE 108 // offset = x 109 // val = int32(y) 110 // 111 // reg<<shift, reg>>shift, reg->shift, reg@>shift 112 // Shifted register value, for ARM. 113 // In this form, reg must be a register and shift can be a register or an integer constant. 114 // Encoding: 115 // type = TYPE_SHIFT 116 // offset = (reg&15) | shifttype<<5 | count 117 // shifttype = 0, 1, 2, 3 for <<, >>, ->, @> 118 // count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant. 119 // 120 // (reg, reg) 121 // A destination register pair. When used as the last argument of an instruction, 122 // this form makes clear that both registers are destinations. 123 // Encoding: 124 // type = TYPE_REGREG 125 // reg = first register 126 // offset = second register 127 // 128 // [reg, reg, reg-reg] 129 // Register list for ARM. 130 // Encoding: 131 // type = TYPE_REGLIST 132 // offset = bit mask of registers in list; R0 is low bit. 133 // 134 // reg, reg 135 // Register pair for ARM. 136 // TYPE_REGREG2 137 // 138 // (reg+reg) 139 // Register pair for PPC64. 140 // Encoding: 141 // type = TYPE_MEM 142 // reg = first register 143 // index = second register 144 // scale = 1 145 // 146 type Addr struct { 147 Type int16 148 Reg int16 149 Index int16 150 Scale int16 // Sometimes holds a register. 151 Name int8 152 Class int8 153 Etype uint8 154 Offset int64 155 Width int64 156 Sym *LSym 157 Gotype *LSym 158 159 // argument value: 160 // for TYPE_SCONST, a string 161 // for TYPE_FCONST, a float64 162 // for TYPE_BRANCH, a *Prog (optional) 163 // for TYPE_TEXTSIZE, an int32 (optional) 164 Val interface{} 165 166 Node interface{} // for use by compiler 167 } 168 169 const ( 170 NAME_NONE = 0 + iota 171 NAME_EXTERN 172 NAME_STATIC 173 NAME_AUTO 174 NAME_PARAM 175 // A reference to name@GOT(SB) is a reference to the entry in the global offset 176 // table for 'name'. 177 NAME_GOTREF 178 ) 179 180 const ( 181 TYPE_NONE = 0 182 ) 183 184 const ( 185 TYPE_BRANCH = 5 + iota 186 TYPE_TEXTSIZE 187 TYPE_MEM 188 TYPE_CONST 189 TYPE_FCONST 190 TYPE_SCONST 191 TYPE_REG 192 TYPE_ADDR 193 TYPE_SHIFT 194 TYPE_REGREG 195 TYPE_REGREG2 196 TYPE_INDIR 197 TYPE_REGLIST 198 ) 199 200 // TODO(rsc): Describe prog. 201 // TODO(rsc): Describe TEXT/GLOBL flag in from3, DATA width in from3. 202 type Prog struct { 203 Ctxt *Link 204 Link *Prog 205 From Addr 206 From3 *Addr // optional 207 To Addr 208 Opt interface{} 209 Forwd *Prog 210 Pcond *Prog 211 Rel *Prog // Source of forward jumps on x86; pcrel on arm 212 Pc int64 213 Lineno int32 214 Spadj int32 215 As int16 216 Reg int16 217 RegTo2 int16 // 2nd register output operand 218 Mark uint16 219 Optab uint16 220 Scond uint8 221 Back uint8 222 Ft uint8 223 Tt uint8 224 Isize uint8 225 Mode int8 226 227 Info ProgInfo 228 } 229 230 // From3Type returns From3.Type, or TYPE_NONE when From3 is nil. 231 func (p *Prog) From3Type() int16 { 232 if p.From3 == nil { 233 return TYPE_NONE 234 } 235 return p.From3.Type 236 } 237 238 // From3Offset returns From3.Offset, or 0 when From3 is nil. 239 func (p *Prog) From3Offset() int64 { 240 if p.From3 == nil { 241 return 0 242 } 243 return p.From3.Offset 244 } 245 246 // ProgInfo holds information about the instruction for use 247 // by clients such as the compiler. The exact meaning of this 248 // data is up to the client and is not interpreted by the cmd/internal/obj/... packages. 249 type ProgInfo struct { 250 _ struct{} // to prevent unkeyed literals. Trailing zero-sized field will take space. 251 Flags uint32 // flag bits 252 Reguse uint64 // registers implicitly used by this instruction 253 Regset uint64 // registers implicitly set by this instruction 254 Regindex uint64 // registers used by addressing mode 255 } 256 257 // Prog.as opcodes. 258 // These are the portable opcodes, common to all architectures. 259 // Each architecture defines many more arch-specific opcodes, 260 // with values starting at A_ARCHSPECIFIC. 261 // Each architecture adds an offset to this so each machine has 262 // distinct space for its instructions. The offset is a power of 263 // two so it can be masked to return to origin zero. 264 // See the definitions of ABase386 etc. 265 const ( 266 AXXX = 0 + iota 267 ACALL 268 ACHECKNIL 269 ADATA 270 ADUFFCOPY 271 ADUFFZERO 272 AEND 273 AFUNCDATA 274 AGLOBL 275 AJMP 276 ANOP 277 APCDATA 278 ARET 279 ATEXT 280 ATYPE 281 AUNDEF 282 AUSEFIELD 283 AVARDEF 284 AVARKILL 285 A_ARCHSPECIFIC 286 ) 287 288 // An LSym is the sort of symbol that is written to an object file. 289 type LSym struct { 290 Name string 291 Type int16 292 Version int16 293 Dupok uint8 294 Cfunc uint8 295 Nosplit uint8 296 Leaf uint8 297 Seenglobl uint8 298 Onlist uint8 299 // Local means make the symbol local even when compiling Go code to reference Go 300 // symbols in other shared libraries, as in this mode symbols are global by 301 // default. "local" here means in the sense of the dynamic linker, i.e. not 302 // visible outside of the module (shared library or executable) that contains its 303 // definition. (When not compiling to support Go shared libraries, all symbols are 304 // local in this sense unless there is a cgo_export_* directive). 305 Local bool 306 Args int32 307 Locals int32 308 Value int64 309 Size int64 310 Next *LSym 311 Gotype *LSym 312 Autom *Auto 313 Text *Prog 314 Etext *Prog 315 Pcln *Pcln 316 P []byte 317 R []Reloc 318 } 319 320 type Pcln struct { 321 Pcsp Pcdata 322 Pcfile Pcdata 323 Pcline Pcdata 324 Pcdata []Pcdata 325 Funcdata []*LSym 326 Funcdataoff []int64 327 File []*LSym 328 Lastfile *LSym 329 Lastindex int 330 } 331 332 // LSym.type 333 const ( 334 Sxxx = iota 335 STEXT 336 SELFRXSECT 337 338 STYPE 339 SSTRING 340 SGOSTRING 341 SGOFUNC 342 SGCBITS 343 SRODATA 344 SFUNCTAB 345 346 // Types STYPE-SFUNCTAB above are written to the .rodata section by default. 347 // When linking a shared object, some conceptually "read only" types need to 348 // be written to by relocations and putting them in a section called 349 // ".rodata" interacts poorly with the system linkers. The GNU linkers 350 // support this situation by arranging for sections of the name 351 // ".data.rel.ro.XXX" to be mprotected read only by the dynamic linker after 352 // relocations have applied, so when the Go linker is creating a shared 353 // object it checks all objects of the above types and bumps any object that 354 // has a relocation to it to the corresponding type below, which are then 355 // written to sections with appropriate magic names. 356 STYPERELRO 357 SSTRINGRELRO 358 SGOSTRINGRELRO 359 SGOFUNCRELRO 360 SGCBITSRELRO 361 SRODATARELRO 362 SFUNCTABRELRO 363 364 STYPELINK 365 SSYMTAB 366 SPCLNTAB 367 SELFROSECT 368 SMACHOPLT 369 SELFSECT 370 SMACHO 371 SMACHOGOT 372 SWINDOWS 373 SELFGOT 374 SNOPTRDATA 375 SINITARR 376 SDATA 377 SBSS 378 SNOPTRBSS 379 STLSBSS 380 SXREF 381 SMACHOSYMSTR 382 SMACHOSYMTAB 383 SMACHOINDIRECTPLT 384 SMACHOINDIRECTGOT 385 SFILE 386 SFILEPATH 387 SCONST 388 SDYNIMPORT 389 SHOSTOBJ 390 SSUB = 1 << 8 391 SMASK = SSUB - 1 392 SHIDDEN = 1 << 9 393 SCONTAINER = 1 << 10 // has a sub-symbol 394 ) 395 396 type Reloc struct { 397 Off int32 398 Siz uint8 399 Type int32 400 Add int64 401 Sym *LSym 402 } 403 404 // Reloc.type 405 const ( 406 R_ADDR = 1 + iota 407 // R_ADDRPOWER relocates a pair of "D-form" instructions (instructions with 16-bit 408 // immediates in the low half of the instruction word), usually addis followed by 409 // another add or a load, inserting the "high adjusted" 16 bits of the address of 410 // the referenced symbol into the immediate field of the first instruction and the 411 // low 16 bits into that of the second instruction. 412 R_ADDRPOWER 413 // R_ADDRARM64 relocates an adrp, add pair to compute the address of the 414 // referenced symbol. 415 R_ADDRARM64 416 // R_ADDRMIPS (only used on mips64) resolves to a 32-bit external address, 417 // by loading the address into a register with two instructions (lui, ori). 418 R_ADDRMIPS 419 R_SIZE 420 R_CALL 421 R_CALLARM 422 R_CALLARM64 423 R_CALLIND 424 R_CALLPOWER 425 // R_CALLMIPS (only used on mips64) resolves to non-PC-relative target address 426 // of a CALL (JAL) instruction, by encoding the address into the instruction. 427 R_CALLMIPS 428 R_CONST 429 R_PCREL 430 // R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the 431 // thread-local symbol from the thread local base and is used to implement the 432 // "local exec" model for tls access (r.Sym is not set on intel platforms but is 433 // set to a TLS symbol -- runtime.tlsg -- in the linker when externally linking). 434 R_TLS_LE 435 // R_TLS_IE, used 386, amd64, and ARM resolves to the PC-relative offset to a GOT 436 // slot containing the offset from the thread-local symbol from the thread local 437 // base and is used to implemented the "initial exec" model for tls access (r.Sym 438 // is not set on intel platforms but is set to a TLS symbol -- runtime.tlsg -- in 439 // the linker when externally linking). 440 R_TLS_IE 441 R_GOTOFF 442 R_PLT0 443 R_PLT1 444 R_PLT2 445 R_USEFIELD 446 R_POWER_TOC 447 R_GOTPCREL 448 // R_JMPMIPS (only used on mips64) resolves to non-PC-relative target address 449 // of a JMP instruction, by encoding the address into the instruction. 450 // The stack nosplit check ignores this since it is not a function call. 451 R_JMPMIPS 452 453 // Platform dependent relocations. Architectures with fixed width instructions 454 // have the inherent issue that a 32-bit (or 64-bit!) displacement cannot be 455 // stuffed into a 32-bit instruction, so an address needs to be spread across 456 // several instructions, and in turn this requires a sequence of relocations, each 457 // updating a part of an instruction. This leads to relocation codes that are 458 // inherently processor specific. 459 460 // Arm64. 461 462 // Set a MOV[NZ] immediate field to bits [15:0] of the offset from the thread 463 // local base to the thread local variable defined by the referenced (thread 464 // local) symbol. Error if the offset does not fit into 16 bits. 465 R_ARM64_TLS_LE 466 467 // Relocates an ADRP; LD64 instruction sequence to load the offset between 468 // the thread local base and the thread local variable defined by the 469 // referenced (thread local) symbol from the GOT. 470 R_ARM64_TLS_IE 471 472 // R_ARM64_GOTPCREL relocates an adrp, ld64 pair to compute the address of the GOT 473 // slot of the referenced symbol. 474 R_ARM64_GOTPCREL 475 476 // PPC64. 477 478 // R_POWER_TLS_LE is used to implement the "local exec" model for tls 479 // access. It resolves to the offset of the thread-local symbol from the 480 // thread pointer (R13) and inserts this value into the low 16 bits of an 481 // instruction word. 482 R_POWER_TLS_LE 483 484 // R_POWER_TLS_IE is used to implement the "initial exec" model for tls access. It 485 // relocates a D-form, DS-form instruction sequence like R_ADDRPOWER_DS. It 486 // inserts to the offset of GOT slot for the thread-local symbol from the TOC (the 487 // GOT slot is filled by the dynamic linker with the offset of the thread-local 488 // symbol from the thread pointer (R13)). 489 R_POWER_TLS_IE 490 491 // R_POWER_TLS marks an X-form instruction such as "MOVD 0(R13)(R31*1), g" as 492 // accessing a particular thread-local symbol. It does not affect code generation 493 // but is used by the system linker when relaxing "initial exec" model code to 494 // "local exec" model code. 495 R_POWER_TLS 496 497 // R_ADDRPOWER_DS is similar to R_ADDRPOWER above, but assumes the second 498 // instruction is a "DS-form" instruction, which has an immediate field occupying 499 // bits [15:2] of the instruction word. Bits [15:2] of the address of the 500 // relocated symbol are inserted into this field; it is an error if the last two 501 // bits of the address are not 0. 502 R_ADDRPOWER_DS 503 504 // R_ADDRPOWER_PCREL relocates a D-form, DS-form instruction sequence like 505 // R_ADDRPOWER_DS but inserts the offset of the GOT slot for the referenced symbol 506 // from the TOC rather than the symbol's address. 507 R_ADDRPOWER_GOT 508 509 // R_ADDRPOWER_PCREL relocates two D-form instructions like R_ADDRPOWER, but 510 // inserts the displacement from the place being relocated to the address of the 511 // the relocated symbol instead of just its address. 512 R_ADDRPOWER_PCREL 513 514 // R_ADDRPOWER_TOCREL relocates two D-form instructions like R_ADDRPOWER, but 515 // inserts the offset from the TOC to the address of the the relocated symbol 516 // rather than the symbol's address. 517 R_ADDRPOWER_TOCREL 518 519 // R_ADDRPOWER_TOCREL relocates a D-form, DS-form instruction sequence like 520 // R_ADDRPOWER_DS but inserts the offset from the TOC to the address of the the 521 // relocated symbol rather than the symbol's address. 522 R_ADDRPOWER_TOCREL_DS 523 ) 524 525 type Auto struct { 526 Asym *LSym 527 Link *Auto 528 Aoffset int32 529 Name int16 530 Gotype *LSym 531 } 532 533 // Auto.name 534 const ( 535 A_AUTO = 1 + iota 536 A_PARAM 537 ) 538 539 type Pcdata struct { 540 P []byte 541 } 542 543 // Pcdata iterator. 544 // for(pciterinit(ctxt, &it, &pcd); !it.done; pciternext(&it)) { it.value holds in [it.pc, it.nextpc) } 545 type Pciter struct { 546 d Pcdata 547 p []byte 548 pc uint32 549 nextpc uint32 550 pcscale uint32 551 value int32 552 start int 553 done int 554 } 555 556 // symbol version, incremented each time a file is loaded. 557 // version==1 is reserved for savehist. 558 const ( 559 HistVersion = 1 560 ) 561 562 // Link holds the context for writing object code from a compiler 563 // to be linker input or for reading that input into the linker. 564 type Link struct { 565 Goarm int32 566 Headtype int 567 Arch *LinkArch 568 Debugasm int32 569 Debugvlog int32 570 Debugdivmod int32 571 Debugpcln int32 572 Flag_shared int32 573 Flag_dynlink bool 574 Bso *Biobuf 575 Pathname string 576 Windows int32 577 Goroot string 578 Goroot_final string 579 Enforce_data_order int32 580 Hash map[SymVer]*LSym 581 LineHist LineHist 582 Imports []string 583 Plist *Plist 584 Plast *Plist 585 Sym_div *LSym 586 Sym_divu *LSym 587 Sym_mod *LSym 588 Sym_modu *LSym 589 Plan9privates *LSym 590 Curp *Prog 591 Printp *Prog 592 Blitrl *Prog 593 Elitrl *Prog 594 Rexflag int 595 Vexflag int 596 Rep int 597 Repn int 598 Lock int 599 Asmode int 600 Andptr []byte 601 And [100]uint8 602 Instoffset int64 603 Autosize int32 604 Armsize int32 605 Pc int64 606 Diag func(string, ...interface{}) 607 Mode int 608 Cursym *LSym 609 Version int 610 Textp *LSym 611 Etextp *LSym 612 } 613 614 // The smallest possible offset from the hardware stack pointer to a local 615 // variable on the stack. Architectures that use a link register save its value 616 // on the stack in the function prologue and so always have a pointer between 617 // the hardware stack pointer and the local variable area. 618 func (ctxt *Link) FixedFrameSize() int64 { 619 switch ctxt.Arch.Thechar { 620 case '6', '8': 621 return 0 622 case '9': 623 // PIC code on ppc64le requires 32 bytes of stack, and it's easier to 624 // just use that much stack always on ppc64x. 625 return int64(4 * ctxt.Arch.Ptrsize) 626 default: 627 return int64(ctxt.Arch.Ptrsize) 628 } 629 } 630 631 type SymVer struct { 632 Name string 633 Version int // TODO: make int16 to match LSym.Version? 634 } 635 636 // LinkArch is the definition of a single architecture. 637 type LinkArch struct { 638 ByteOrder binary.ByteOrder 639 Name string 640 Thechar int 641 Preprocess func(*Link, *LSym) 642 Assemble func(*Link, *LSym) 643 Follow func(*Link, *LSym) 644 Progedit func(*Link, *Prog) 645 UnaryDst map[int]bool // Instruction takes one operand, a destination. 646 Minlc int 647 Ptrsize int 648 Regsize int 649 } 650 651 /* executable header types */ 652 const ( 653 Hunknown = 0 + iota 654 Hdarwin 655 Hdragonfly 656 Helf 657 Hfreebsd 658 Hlinux 659 Hnacl 660 Hnetbsd 661 Hopenbsd 662 Hplan9 663 Hsolaris 664 Hwindows 665 ) 666 667 type Plist struct { 668 Name *LSym 669 Firstpc *Prog 670 Recur int 671 Link *Plist 672 } 673 674 /* 675 * start a new Prog list. 676 */ 677 func Linknewplist(ctxt *Link) *Plist { 678 pl := new(Plist) 679 if ctxt.Plist == nil { 680 ctxt.Plist = pl 681 } else { 682 ctxt.Plast.Link = pl 683 } 684 ctxt.Plast = pl 685 return pl 686 }