github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/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/dwarf" 36 "cmd/internal/objabi" 37 "cmd/internal/src" 38 "cmd/internal/sys" 39 "fmt" 40 "sync" 41 ) 42 43 // An Addr is an argument to an instruction. 44 // The general forms and their encodings are: 45 // 46 // sym±offset(symkind)(reg)(index*scale) 47 // Memory reference at address &sym(symkind) + offset + reg + index*scale. 48 // Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted. 49 // If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg). 50 // To force a parsing as index*scale, write (index*1). 51 // Encoding: 52 // type = TYPE_MEM 53 // name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE) 54 // sym = sym 55 // offset = ±offset 56 // reg = reg (REG_*) 57 // index = index (REG_*) 58 // scale = scale (1, 2, 4, 8) 59 // 60 // $<mem> 61 // Effective address of memory reference <mem>, defined above. 62 // Encoding: same as memory reference, but type = TYPE_ADDR. 63 // 64 // $<±integer value> 65 // This is a special case of $<mem>, in which only ±offset is present. 66 // It has a separate type for easy recognition. 67 // Encoding: 68 // type = TYPE_CONST 69 // offset = ±integer value 70 // 71 // *<mem> 72 // Indirect reference through memory reference <mem>, defined above. 73 // Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function 74 // pointer stored in the data word sym(SB), not a function named sym(SB). 75 // Encoding: same as above, but type = TYPE_INDIR. 76 // 77 // $*$<mem> 78 // No longer used. 79 // On machines with actual SB registers, $*$<mem> forced the 80 // instruction encoding to use a full 32-bit constant, never a 81 // reference relative to SB. 82 // 83 // $<floating point literal> 84 // Floating point constant value. 85 // Encoding: 86 // type = TYPE_FCONST 87 // val = floating point value 88 // 89 // $<string literal, up to 8 chars> 90 // String literal value (raw bytes used for DATA instruction). 91 // Encoding: 92 // type = TYPE_SCONST 93 // val = string 94 // 95 // <register name> 96 // Any register: integer, floating point, control, segment, and so on. 97 // If looking for specific register kind, must check type and reg value range. 98 // Encoding: 99 // type = TYPE_REG 100 // reg = reg (REG_*) 101 // 102 // x(PC) 103 // Encoding: 104 // type = TYPE_BRANCH 105 // val = Prog* reference OR ELSE offset = target pc (branch takes priority) 106 // 107 // $±x-±y 108 // Final argument to TEXT, specifying local frame size x and argument size y. 109 // In this form, x and y are integer literals only, not arbitrary expressions. 110 // This avoids parsing ambiguities due to the use of - as a separator. 111 // The ± are optional. 112 // If the final argument to TEXT omits the -±y, the encoding should still 113 // use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown. 114 // Encoding: 115 // type = TYPE_TEXTSIZE 116 // offset = x 117 // val = int32(y) 118 // 119 // reg<<shift, reg>>shift, reg->shift, reg@>shift 120 // Shifted register value, for ARM and ARM64. 121 // In this form, reg must be a register and shift can be a register or an integer constant. 122 // Encoding: 123 // type = TYPE_SHIFT 124 // On ARM: 125 // offset = (reg&15) | shifttype<<5 | count 126 // shifttype = 0, 1, 2, 3 for <<, >>, ->, @> 127 // count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant. 128 // On ARM64: 129 // offset = (reg&31)<<16 | shifttype<<22 | (count&63)<<10 130 // shifttype = 0, 1, 2 for <<, >>, -> 131 // 132 // (reg, reg) 133 // A destination register pair. When used as the last argument of an instruction, 134 // this form makes clear that both registers are destinations. 135 // Encoding: 136 // type = TYPE_REGREG 137 // reg = first register 138 // offset = second register 139 // 140 // [reg, reg, reg-reg] 141 // Register list for ARM, ARM64, 386/AMD64. 142 // Encoding: 143 // type = TYPE_REGLIST 144 // On ARM: 145 // offset = bit mask of registers in list; R0 is low bit. 146 // On ARM64: 147 // offset = register count (Q:size) | arrangement (opcode) | first register 148 // On 386/AMD64: 149 // reg = range low register 150 // offset = 2 packed registers + kind tag (see x86.EncodeRegisterRange) 151 // 152 // reg, reg 153 // Register pair for ARM. 154 // TYPE_REGREG2 155 // 156 // (reg+reg) 157 // Register pair for PPC64. 158 // Encoding: 159 // type = TYPE_MEM 160 // reg = first register 161 // index = second register 162 // scale = 1 163 // 164 // reg.[US]XT[BHWX] 165 // Register extension for ARM64 166 // Encoding: 167 // type = TYPE_REG 168 // reg = REG_[US]XT[BHWX] + register + shift amount 169 // offset = ((reg&31) << 16) | (exttype << 13) | (amount<<10) 170 // 171 // reg.<T> 172 // Register arrangement for ARM64 SIMD register 173 // e.g.: V1.S4, V2.S2, V7.D2, V2.H4, V6.B16 174 // Encoding: 175 // type = TYPE_REG 176 // reg = REG_ARNG + register + arrangement 177 // 178 // reg.<T>[index] 179 // Register element for ARM64 180 // Encoding: 181 // type = TYPE_REG 182 // reg = REG_ELEM + register + arrangement 183 // index = element index 184 185 type Addr struct { 186 Reg int16 187 Index int16 188 Scale int16 // Sometimes holds a register. 189 Type AddrType 190 Name AddrName 191 Class int8 192 Offset int64 193 Sym *LSym 194 195 // argument value: 196 // for TYPE_SCONST, a string 197 // for TYPE_FCONST, a float64 198 // for TYPE_BRANCH, a *Prog (optional) 199 // for TYPE_TEXTSIZE, an int32 (optional) 200 Val interface{} 201 } 202 203 type AddrName int8 204 205 const ( 206 NAME_NONE AddrName = iota 207 NAME_EXTERN 208 NAME_STATIC 209 NAME_AUTO 210 NAME_PARAM 211 // A reference to name@GOT(SB) is a reference to the entry in the global offset 212 // table for 'name'. 213 NAME_GOTREF 214 // Indicates auto that was optimized away, but whose type 215 // we want to preserve in the DWARF debug info. 216 NAME_DELETED_AUTO 217 ) 218 219 //go:generate stringer -type AddrType 220 221 type AddrType uint8 222 223 const ( 224 TYPE_NONE AddrType = iota 225 TYPE_BRANCH 226 TYPE_TEXTSIZE 227 TYPE_MEM 228 TYPE_CONST 229 TYPE_FCONST 230 TYPE_SCONST 231 TYPE_REG 232 TYPE_ADDR 233 TYPE_SHIFT 234 TYPE_REGREG 235 TYPE_REGREG2 236 TYPE_INDIR 237 TYPE_REGLIST 238 ) 239 240 // Prog describes a single machine instruction. 241 // 242 // The general instruction form is: 243 // 244 // (1) As.Scond From [, ...RestArgs], To 245 // (2) As.Scond From, Reg [, ...RestArgs], To, RegTo2 246 // 247 // where As is an opcode and the others are arguments: 248 // From, Reg are sources, and To, RegTo2 are destinations. 249 // RestArgs can hold additional sources and destinations. 250 // Usually, not all arguments are present. 251 // For example, MOVL R1, R2 encodes using only As=MOVL, From=R1, To=R2. 252 // The Scond field holds additional condition bits for systems (like arm) 253 // that have generalized conditional execution. 254 // (2) form is present for compatibility with older code, 255 // to avoid too much changes in a single swing. 256 // (1) scheme is enough to express any kind of operand combination. 257 // 258 // Jump instructions use the Pcond field to point to the target instruction, 259 // which must be in the same linked list as the jump instruction. 260 // 261 // The Progs for a given function are arranged in a list linked through the Link field. 262 // 263 // Each Prog is charged to a specific source line in the debug information, 264 // specified by Pos.Line(). 265 // Every Prog has a Ctxt field that defines its context. 266 // For performance reasons, Progs usually are usually bulk allocated, cached, and reused; 267 // those bulk allocators should always be used, rather than new(Prog). 268 // 269 // The other fields not yet mentioned are for use by the back ends and should 270 // be left zeroed by creators of Prog lists. 271 type Prog struct { 272 Ctxt *Link // linker context 273 Link *Prog // next Prog in linked list 274 From Addr // first source operand 275 RestArgs []Addr // can pack any operands that not fit into {Prog.From, Prog.To} 276 To Addr // destination operand (second is RegTo2 below) 277 Pcond *Prog // target of conditional jump 278 Forwd *Prog // for x86 back end 279 Rel *Prog // for x86, arm back ends 280 Pc int64 // for back ends or assembler: virtual or actual program counter, depending on phase 281 Pos src.XPos // source position of this instruction 282 Spadj int32 // effect of instruction on stack pointer (increment or decrement amount) 283 As As // assembler opcode 284 Reg int16 // 2nd source operand 285 RegTo2 int16 // 2nd destination operand 286 Mark uint16 // bitmask of arch-specific items 287 Optab uint16 // arch-specific opcode index 288 Scond uint8 // bits that describe instruction suffixes (e.g. ARM conditions) 289 Back uint8 // for x86 back end: backwards branch state 290 Ft uint8 // for x86 back end: type index of Prog.From 291 Tt uint8 // for x86 back end: type index of Prog.To 292 Isize uint8 // for x86 back end: size of the instruction in bytes 293 } 294 295 // From3Type returns p.GetFrom3().Type, or TYPE_NONE when 296 // p.GetFrom3() returns nil. 297 // 298 // Deprecated: for the same reasons as Prog.GetFrom3. 299 func (p *Prog) From3Type() AddrType { 300 if p.RestArgs == nil { 301 return TYPE_NONE 302 } 303 return p.RestArgs[0].Type 304 } 305 306 // GetFrom3 returns second source operand (the first is Prog.From). 307 // In combination with Prog.From and Prog.To it makes common 3 operand 308 // case easier to use. 309 // 310 // Should be used only when RestArgs is set with SetFrom3. 311 // 312 // Deprecated: better use RestArgs directly or define backend-specific getters. 313 // Introduced to simplify transition to []Addr. 314 // Usage of this is discouraged due to fragility and lack of guarantees. 315 func (p *Prog) GetFrom3() *Addr { 316 if p.RestArgs == nil { 317 return nil 318 } 319 return &p.RestArgs[0] 320 } 321 322 // SetFrom3 assigns []Addr{a} to p.RestArgs. 323 // In pair with Prog.GetFrom3 it can help in emulation of Prog.From3. 324 // 325 // Deprecated: for the same reasons as Prog.GetFrom3. 326 func (p *Prog) SetFrom3(a Addr) { 327 p.RestArgs = []Addr{a} 328 } 329 330 // An As denotes an assembler opcode. 331 // There are some portable opcodes, declared here in package obj, 332 // that are common to all architectures. 333 // However, the majority of opcodes are arch-specific 334 // and are declared in their respective architecture's subpackage. 335 type As int16 336 337 // These are the portable opcodes. 338 const ( 339 AXXX As = iota 340 ACALL 341 ADUFFCOPY 342 ADUFFZERO 343 AEND 344 AFUNCDATA 345 AJMP 346 ANOP 347 APCALIGN 348 APCDATA 349 ARET 350 AGETCALLERPC 351 ATEXT 352 AUNDEF 353 A_ARCHSPECIFIC 354 ) 355 356 // Each architecture is allotted a distinct subspace of opcode values 357 // for declaring its arch-specific opcodes. 358 // Within this subspace, the first arch-specific opcode should be 359 // at offset A_ARCHSPECIFIC. 360 // 361 // Subspaces are aligned to a power of two so opcodes can be masked 362 // with AMask and used as compact array indices. 363 const ( 364 ABase386 = (1 + iota) << 11 365 ABaseARM 366 ABaseAMD64 367 ABasePPC64 368 ABaseARM64 369 ABaseMIPS 370 ABaseS390X 371 ABaseWasm 372 373 AllowedOpCodes = 1 << 11 // The number of opcodes available for any given architecture. 374 AMask = AllowedOpCodes - 1 // AND with this to use the opcode as an array index. 375 ) 376 377 // An LSym is the sort of symbol that is written to an object file. 378 type LSym struct { 379 Name string 380 Type objabi.SymKind 381 Attribute 382 383 RefIdx int // Index of this symbol in the symbol reference list. 384 Size int64 385 Gotype *LSym 386 P []byte 387 R []Reloc 388 389 Func *FuncInfo 390 } 391 392 // A FuncInfo contains extra fields for STEXT symbols. 393 type FuncInfo struct { 394 Args int32 395 Locals int32 396 Text *Prog 397 Autom []*Auto 398 Pcln Pcln 399 400 dwarfInfoSym *LSym 401 dwarfLocSym *LSym 402 dwarfRangesSym *LSym 403 dwarfAbsFnSym *LSym 404 dwarfIsStmtSym *LSym 405 406 GCArgs LSym 407 GCLocals LSym 408 GCRegs LSym 409 StackObjects *LSym 410 } 411 412 // Attribute is a set of symbol attributes. 413 type Attribute int16 414 415 const ( 416 AttrDuplicateOK Attribute = 1 << iota 417 AttrCFunc 418 AttrNoSplit 419 AttrLeaf 420 AttrWrapper 421 AttrNeedCtxt 422 AttrNoFrame 423 AttrSeenGlobl 424 AttrOnList 425 AttrStatic 426 427 // MakeTypelink means that the type should have an entry in the typelink table. 428 AttrMakeTypelink 429 430 // ReflectMethod means the function may call reflect.Type.Method or 431 // reflect.Type.MethodByName. Matching is imprecise (as reflect.Type 432 // can be used through a custom interface), so ReflectMethod may be 433 // set in some cases when the reflect package is not called. 434 // 435 // Used by the linker to determine what methods can be pruned. 436 AttrReflectMethod 437 438 // Local means make the symbol local even when compiling Go code to reference Go 439 // symbols in other shared libraries, as in this mode symbols are global by 440 // default. "local" here means in the sense of the dynamic linker, i.e. not 441 // visible outside of the module (shared library or executable) that contains its 442 // definition. (When not compiling to support Go shared libraries, all symbols are 443 // local in this sense unless there is a cgo_export_* directive). 444 AttrLocal 445 446 // For function symbols; indicates that the specified function was the 447 // target of an inline during compilation 448 AttrWasInlined 449 ) 450 451 func (a Attribute) DuplicateOK() bool { return a&AttrDuplicateOK != 0 } 452 func (a Attribute) MakeTypelink() bool { return a&AttrMakeTypelink != 0 } 453 func (a Attribute) CFunc() bool { return a&AttrCFunc != 0 } 454 func (a Attribute) NoSplit() bool { return a&AttrNoSplit != 0 } 455 func (a Attribute) Leaf() bool { return a&AttrLeaf != 0 } 456 func (a Attribute) SeenGlobl() bool { return a&AttrSeenGlobl != 0 } 457 func (a Attribute) OnList() bool { return a&AttrOnList != 0 } 458 func (a Attribute) ReflectMethod() bool { return a&AttrReflectMethod != 0 } 459 func (a Attribute) Local() bool { return a&AttrLocal != 0 } 460 func (a Attribute) Wrapper() bool { return a&AttrWrapper != 0 } 461 func (a Attribute) NeedCtxt() bool { return a&AttrNeedCtxt != 0 } 462 func (a Attribute) NoFrame() bool { return a&AttrNoFrame != 0 } 463 func (a Attribute) Static() bool { return a&AttrStatic != 0 } 464 func (a Attribute) WasInlined() bool { return a&AttrWasInlined != 0 } 465 466 func (a *Attribute) Set(flag Attribute, value bool) { 467 if value { 468 *a |= flag 469 } else { 470 *a &^= flag 471 } 472 } 473 474 var textAttrStrings = [...]struct { 475 bit Attribute 476 s string 477 }{ 478 {bit: AttrDuplicateOK, s: "DUPOK"}, 479 {bit: AttrMakeTypelink, s: ""}, 480 {bit: AttrCFunc, s: "CFUNC"}, 481 {bit: AttrNoSplit, s: "NOSPLIT"}, 482 {bit: AttrLeaf, s: "LEAF"}, 483 {bit: AttrSeenGlobl, s: ""}, 484 {bit: AttrOnList, s: ""}, 485 {bit: AttrReflectMethod, s: "REFLECTMETHOD"}, 486 {bit: AttrLocal, s: "LOCAL"}, 487 {bit: AttrWrapper, s: "WRAPPER"}, 488 {bit: AttrNeedCtxt, s: "NEEDCTXT"}, 489 {bit: AttrNoFrame, s: "NOFRAME"}, 490 {bit: AttrStatic, s: "STATIC"}, 491 {bit: AttrWasInlined, s: ""}, 492 } 493 494 // TextAttrString formats a for printing in as part of a TEXT prog. 495 func (a Attribute) TextAttrString() string { 496 var s string 497 for _, x := range textAttrStrings { 498 if a&x.bit != 0 { 499 if x.s != "" { 500 s += x.s + "|" 501 } 502 a &^= x.bit 503 } 504 } 505 if a != 0 { 506 s += fmt.Sprintf("UnknownAttribute(%d)|", a) 507 } 508 // Chop off trailing |, if present. 509 if len(s) > 0 { 510 s = s[:len(s)-1] 511 } 512 return s 513 } 514 515 // The compiler needs LSym to satisfy fmt.Stringer, because it stores 516 // an LSym in ssa.ExternSymbol. 517 func (s *LSym) String() string { 518 return s.Name 519 } 520 521 type Pcln struct { 522 Pcsp Pcdata 523 Pcfile Pcdata 524 Pcline Pcdata 525 Pcinline Pcdata 526 Pcdata []Pcdata 527 Funcdata []*LSym 528 Funcdataoff []int64 529 File []string 530 Lastfile string 531 Lastindex int 532 InlTree InlTree // per-function inlining tree extracted from the global tree 533 } 534 535 type Reloc struct { 536 Off int32 537 Siz uint8 538 Type objabi.RelocType 539 Add int64 540 Sym *LSym 541 } 542 543 type Auto struct { 544 Asym *LSym 545 Aoffset int32 546 Name AddrName 547 Gotype *LSym 548 } 549 550 type Pcdata struct { 551 P []byte 552 } 553 554 // Link holds the context for writing object code from a compiler 555 // to be linker input or for reading that input into the linker. 556 type Link struct { 557 Headtype objabi.HeadType 558 Arch *LinkArch 559 Debugasm bool 560 Debugvlog bool 561 Debugpcln string 562 Flag_shared bool 563 Flag_dynlink bool 564 Flag_optimize bool 565 Flag_locationlists bool 566 Bso *bufio.Writer 567 Pathname string 568 hashmu sync.Mutex // protects hash 569 hash map[string]*LSym // name -> sym mapping 570 statichash map[string]*LSym // name -> sym mapping for static syms 571 PosTable src.PosTable 572 InlTree InlTree // global inlining tree used by gc/inl.go 573 DwFixups *DwarfFixupTable 574 Imports []string 575 DiagFunc func(string, ...interface{}) 576 DiagFlush func() 577 DebugInfo func(fn *LSym, curfn interface{}) ([]dwarf.Scope, dwarf.InlCalls) // if non-nil, curfn is a *gc.Node 578 GenAbstractFunc func(fn *LSym) 579 Errors int 580 581 InParallel bool // parallel backend phase in effect 582 Framepointer_enabled bool 583 584 // state for writing objects 585 Text []*LSym 586 Data []*LSym 587 } 588 589 func (ctxt *Link) Diag(format string, args ...interface{}) { 590 ctxt.Errors++ 591 ctxt.DiagFunc(format, args...) 592 } 593 594 func (ctxt *Link) Logf(format string, args ...interface{}) { 595 fmt.Fprintf(ctxt.Bso, format, args...) 596 ctxt.Bso.Flush() 597 } 598 599 // The smallest possible offset from the hardware stack pointer to a local 600 // variable on the stack. Architectures that use a link register save its value 601 // on the stack in the function prologue and so always have a pointer between 602 // the hardware stack pointer and the local variable area. 603 func (ctxt *Link) FixedFrameSize() int64 { 604 switch ctxt.Arch.Family { 605 case sys.AMD64, sys.I386, sys.Wasm: 606 return 0 607 case sys.PPC64: 608 // PIC code on ppc64le requires 32 bytes of stack, and it's easier to 609 // just use that much stack always on ppc64x. 610 return int64(4 * ctxt.Arch.PtrSize) 611 default: 612 return int64(ctxt.Arch.PtrSize) 613 } 614 } 615 616 // LinkArch is the definition of a single architecture. 617 type LinkArch struct { 618 *sys.Arch 619 Init func(*Link) 620 Preprocess func(*Link, *LSym, ProgAlloc) 621 Assemble func(*Link, *LSym, ProgAlloc) 622 Progedit func(*Link, *Prog, ProgAlloc) 623 UnaryDst map[As]bool // Instruction takes one operand, a destination. 624 DWARFRegisters map[int16]int16 625 }