github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/link/internal/amd64/asm.go (about) 1 // Inferno utils/6l/asm.c 2 // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/asm.c 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 amd64 32 33 import ( 34 "cmd/internal/objabi" 35 "cmd/internal/sys" 36 "cmd/link/internal/ld" 37 "cmd/link/internal/sym" 38 "debug/elf" 39 "log" 40 ) 41 42 func PADDR(x uint32) uint32 { 43 return x &^ 0x80000000 44 } 45 46 func Addcall(ctxt *ld.Link, s *sym.Symbol, t *sym.Symbol) int64 { 47 s.Attr |= sym.AttrReachable 48 i := s.Size 49 s.Size += 4 50 s.Grow(s.Size) 51 r := s.AddRel() 52 r.Sym = t 53 r.Off = int32(i) 54 r.Type = objabi.R_CALL 55 r.Siz = 4 56 return i + int64(r.Siz) 57 } 58 59 func gentext(ctxt *ld.Link) { 60 if !ctxt.DynlinkingGo() { 61 return 62 } 63 addmoduledata := ctxt.Syms.Lookup("runtime.addmoduledata", 0) 64 if addmoduledata.Type == sym.STEXT && ctxt.BuildMode != ld.BuildModePlugin { 65 // we're linking a module containing the runtime -> no need for 66 // an init function 67 return 68 } 69 addmoduledata.Attr |= sym.AttrReachable 70 initfunc := ctxt.Syms.Lookup("go.link.addmoduledata", 0) 71 initfunc.Type = sym.STEXT 72 initfunc.Attr |= sym.AttrLocal 73 initfunc.Attr |= sym.AttrReachable 74 o := func(op ...uint8) { 75 for _, op1 := range op { 76 initfunc.AddUint8(op1) 77 } 78 } 79 // 0000000000000000 <local.dso_init>: 80 // 0: 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi # 7 <local.dso_init+0x7> 81 // 3: R_X86_64_PC32 runtime.firstmoduledata-0x4 82 o(0x48, 0x8d, 0x3d) 83 initfunc.AddPCRelPlus(ctxt.Arch, ctxt.Moduledata, 0) 84 // 7: e8 00 00 00 00 callq c <local.dso_init+0xc> 85 // 8: R_X86_64_PLT32 runtime.addmoduledata-0x4 86 o(0xe8) 87 Addcall(ctxt, initfunc, addmoduledata) 88 // c: c3 retq 89 o(0xc3) 90 if ctxt.BuildMode == ld.BuildModePlugin { 91 ctxt.Textp = append(ctxt.Textp, addmoduledata) 92 } 93 ctxt.Textp = append(ctxt.Textp, initfunc) 94 initarray_entry := ctxt.Syms.Lookup("go.link.addmoduledatainit", 0) 95 initarray_entry.Attr |= sym.AttrReachable 96 initarray_entry.Attr |= sym.AttrLocal 97 initarray_entry.Type = sym.SINITARR 98 initarray_entry.AddAddr(ctxt.Arch, initfunc) 99 } 100 101 func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool { 102 targ := r.Sym 103 104 switch r.Type { 105 default: 106 if r.Type >= 256 { 107 ld.Errorf(s, "unexpected relocation type %d (%s)", r.Type, sym.RelocName(ctxt.Arch, r.Type)) 108 return false 109 } 110 111 // Handle relocations found in ELF object files. 112 case 256 + objabi.RelocType(elf.R_X86_64_PC32): 113 if targ.Type == sym.SDYNIMPORT { 114 ld.Errorf(s, "unexpected R_X86_64_PC32 relocation for dynamic symbol %s", targ.Name) 115 } 116 // TODO(mwhudson): the test of VisibilityHidden here probably doesn't make 117 // sense and should be removed when someone has thought about it properly. 118 if (targ.Type == 0 || targ.Type == sym.SXREF) && !targ.Attr.VisibilityHidden() { 119 ld.Errorf(s, "unknown symbol %s in pcrel", targ.Name) 120 } 121 r.Type = objabi.R_PCREL 122 r.Add += 4 123 return true 124 125 case 256 + objabi.RelocType(elf.R_X86_64_PC64): 126 if targ.Type == sym.SDYNIMPORT { 127 ld.Errorf(s, "unexpected R_X86_64_PC64 relocation for dynamic symbol %s", targ.Name) 128 } 129 if targ.Type == 0 || targ.Type == sym.SXREF { 130 ld.Errorf(s, "unknown symbol %s in pcrel", targ.Name) 131 } 132 r.Type = objabi.R_PCREL 133 r.Add += 8 134 return true 135 136 case 256 + objabi.RelocType(elf.R_X86_64_PLT32): 137 r.Type = objabi.R_PCREL 138 r.Add += 4 139 if targ.Type == sym.SDYNIMPORT { 140 addpltsym(ctxt, targ) 141 r.Sym = ctxt.Syms.Lookup(".plt", 0) 142 r.Add += int64(targ.Plt()) 143 } 144 145 return true 146 147 case 256 + objabi.RelocType(elf.R_X86_64_GOTPCREL), 256 + objabi.RelocType(elf.R_X86_64_GOTPCRELX), 256 + objabi.RelocType(elf.R_X86_64_REX_GOTPCRELX): 148 if targ.Type != sym.SDYNIMPORT { 149 // have symbol 150 if r.Off >= 2 && s.P[r.Off-2] == 0x8b { 151 // turn MOVQ of GOT entry into LEAQ of symbol itself 152 s.P[r.Off-2] = 0x8d 153 154 r.Type = objabi.R_PCREL 155 r.Add += 4 156 return true 157 } 158 } 159 160 // fall back to using GOT and hope for the best (CMOV*) 161 // TODO: just needs relocation, no need to put in .dynsym 162 addgotsym(ctxt, targ) 163 164 r.Type = objabi.R_PCREL 165 r.Sym = ctxt.Syms.Lookup(".got", 0) 166 r.Add += 4 167 r.Add += int64(targ.Got()) 168 return true 169 170 case 256 + objabi.RelocType(elf.R_X86_64_64): 171 if targ.Type == sym.SDYNIMPORT { 172 ld.Errorf(s, "unexpected R_X86_64_64 relocation for dynamic symbol %s", targ.Name) 173 } 174 r.Type = objabi.R_ADDR 175 return true 176 177 // Handle relocations found in Mach-O object files. 178 case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 0, 179 512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 0, 180 512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 0: 181 // TODO: What is the difference between all these? 182 r.Type = objabi.R_ADDR 183 184 if targ.Type == sym.SDYNIMPORT { 185 ld.Errorf(s, "unexpected reloc for dynamic symbol %s", targ.Name) 186 } 187 return true 188 189 case 512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 1: 190 if targ.Type == sym.SDYNIMPORT { 191 addpltsym(ctxt, targ) 192 r.Sym = ctxt.Syms.Lookup(".plt", 0) 193 r.Add = int64(targ.Plt()) 194 r.Type = objabi.R_PCREL 195 return true 196 } 197 fallthrough 198 199 case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 1, 200 512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 1, 201 512 + ld.MACHO_X86_64_RELOC_SIGNED_1*2 + 1, 202 512 + ld.MACHO_X86_64_RELOC_SIGNED_2*2 + 1, 203 512 + ld.MACHO_X86_64_RELOC_SIGNED_4*2 + 1: 204 r.Type = objabi.R_PCREL 205 206 if targ.Type == sym.SDYNIMPORT { 207 ld.Errorf(s, "unexpected pc-relative reloc for dynamic symbol %s", targ.Name) 208 } 209 return true 210 211 case 512 + ld.MACHO_X86_64_RELOC_GOT_LOAD*2 + 1: 212 if targ.Type != sym.SDYNIMPORT { 213 // have symbol 214 // turn MOVQ of GOT entry into LEAQ of symbol itself 215 if r.Off < 2 || s.P[r.Off-2] != 0x8b { 216 ld.Errorf(s, "unexpected GOT_LOAD reloc for non-dynamic symbol %s", targ.Name) 217 return false 218 } 219 220 s.P[r.Off-2] = 0x8d 221 r.Type = objabi.R_PCREL 222 return true 223 } 224 fallthrough 225 226 case 512 + ld.MACHO_X86_64_RELOC_GOT*2 + 1: 227 if targ.Type != sym.SDYNIMPORT { 228 ld.Errorf(s, "unexpected GOT reloc for non-dynamic symbol %s", targ.Name) 229 } 230 addgotsym(ctxt, targ) 231 r.Type = objabi.R_PCREL 232 r.Sym = ctxt.Syms.Lookup(".got", 0) 233 r.Add += int64(targ.Got()) 234 return true 235 } 236 237 switch r.Type { 238 case objabi.R_CALL, 239 objabi.R_PCREL: 240 if targ.Type != sym.SDYNIMPORT { 241 // nothing to do, the relocation will be laid out in reloc 242 return true 243 } 244 if ctxt.LinkMode == ld.LinkExternal { 245 // External linker will do this relocation. 246 return true 247 } 248 // Internal linking, for both ELF and Mach-O. 249 // Build a PLT entry and change the relocation target to that entry. 250 addpltsym(ctxt, targ) 251 r.Sym = ctxt.Syms.Lookup(".plt", 0) 252 r.Add = int64(targ.Plt()) 253 return true 254 255 case objabi.R_ADDR: 256 if s.Type == sym.STEXT && ctxt.IsELF { 257 if ctxt.HeadType == objabi.Hsolaris { 258 addpltsym(ctxt, targ) 259 r.Sym = ctxt.Syms.Lookup(".plt", 0) 260 r.Add += int64(targ.Plt()) 261 return true 262 } 263 // The code is asking for the address of an external 264 // function. We provide it with the address of the 265 // correspondent GOT symbol. 266 addgotsym(ctxt, targ) 267 268 r.Sym = ctxt.Syms.Lookup(".got", 0) 269 r.Add += int64(targ.Got()) 270 return true 271 } 272 273 // Process dynamic relocations for the data sections. 274 if ctxt.BuildMode == ld.BuildModePIE && ctxt.LinkMode == ld.LinkInternal { 275 // When internally linking, generate dynamic relocations 276 // for all typical R_ADDR relocations. The exception 277 // are those R_ADDR that are created as part of generating 278 // the dynamic relocations and must be resolved statically. 279 // 280 // There are three phases relevant to understanding this: 281 // 282 // dodata() // we are here 283 // address() // symbol address assignment 284 // reloc() // resolution of static R_ADDR relocs 285 // 286 // At this point symbol addresses have not been 287 // assigned yet (as the final size of the .rela section 288 // will affect the addresses), and so we cannot write 289 // the Elf64_Rela.r_offset now. Instead we delay it 290 // until after the 'address' phase of the linker is 291 // complete. We do this via Addaddrplus, which creates 292 // a new R_ADDR relocation which will be resolved in 293 // the 'reloc' phase. 294 // 295 // These synthetic static R_ADDR relocs must be skipped 296 // now, or else we will be caught in an infinite loop 297 // of generating synthetic relocs for our synthetic 298 // relocs. 299 // 300 // Furthermore, the rela sections contain dynamic 301 // relocations with R_ADDR relocations on 302 // Elf64_Rela.r_offset. This field should contain the 303 // symbol offset as determined by reloc(), not the 304 // final dynamically linked address as a dynamic 305 // relocation would provide. 306 switch s.Name { 307 case ".dynsym", ".rela", ".rela.plt", ".got.plt", ".dynamic": 308 return false 309 } 310 } else { 311 // Either internally linking a static executable, 312 // in which case we can resolve these relocations 313 // statically in the 'reloc' phase, or externally 314 // linking, in which case the relocation will be 315 // prepared in the 'reloc' phase and passed to the 316 // external linker in the 'asmb' phase. 317 if s.Type != sym.SDATA && s.Type != sym.SRODATA { 318 break 319 } 320 } 321 322 if ctxt.IsELF { 323 // TODO: We generate a R_X86_64_64 relocation for every R_ADDR, even 324 // though it would be more efficient (for the dynamic linker) if we 325 // generated R_X86_RELATIVE instead. 326 ld.Adddynsym(ctxt, targ) 327 rela := ctxt.Syms.Lookup(".rela", 0) 328 rela.AddAddrPlus(ctxt.Arch, s, int64(r.Off)) 329 if r.Siz == 8 { 330 rela.AddUint64(ctxt.Arch, ld.ELF64_R_INFO(uint32(targ.Dynid), uint32(elf.R_X86_64_64))) 331 } else { 332 // TODO: never happens, remove. 333 rela.AddUint64(ctxt.Arch, ld.ELF64_R_INFO(uint32(targ.Dynid), uint32(elf.R_X86_64_32))) 334 } 335 rela.AddUint64(ctxt.Arch, uint64(r.Add)) 336 r.Type = 256 // ignore during relocsym 337 return true 338 } 339 340 if ctxt.HeadType == objabi.Hdarwin && s.Size == int64(ctxt.Arch.PtrSize) && r.Off == 0 { 341 // Mach-O relocations are a royal pain to lay out. 342 // They use a compact stateful bytecode representation 343 // that is too much bother to deal with. 344 // Instead, interpret the C declaration 345 // void *_Cvar_stderr = &stderr; 346 // as making _Cvar_stderr the name of a GOT entry 347 // for stderr. This is separate from the usual GOT entry, 348 // just in case the C code assigns to the variable, 349 // and of course it only works for single pointers, 350 // but we only need to support cgo and that's all it needs. 351 ld.Adddynsym(ctxt, targ) 352 353 got := ctxt.Syms.Lookup(".got", 0) 354 s.Type = got.Type 355 s.Attr |= sym.AttrSubSymbol 356 s.Outer = got 357 s.Sub = got.Sub 358 got.Sub = s 359 s.Value = got.Size 360 got.AddUint64(ctxt.Arch, 0) 361 ctxt.Syms.Lookup(".linkedit.got", 0).AddUint32(ctxt.Arch, uint32(targ.Dynid)) 362 r.Type = 256 // ignore during relocsym 363 return true 364 } 365 } 366 367 return false 368 } 369 370 func elfreloc1(ctxt *ld.Link, r *sym.Reloc, sectoff int64) bool { 371 ctxt.Out.Write64(uint64(sectoff)) 372 373 elfsym := r.Xsym.ElfsymForReloc() 374 switch r.Type { 375 default: 376 return false 377 case objabi.R_ADDR: 378 if r.Siz == 4 { 379 ctxt.Out.Write64(uint64(elf.R_X86_64_32) | uint64(elfsym)<<32) 380 } else if r.Siz == 8 { 381 ctxt.Out.Write64(uint64(elf.R_X86_64_64) | uint64(elfsym)<<32) 382 } else { 383 return false 384 } 385 case objabi.R_TLS_LE: 386 if r.Siz == 4 { 387 ctxt.Out.Write64(uint64(elf.R_X86_64_TPOFF32) | uint64(elfsym)<<32) 388 } else { 389 return false 390 } 391 case objabi.R_TLS_IE: 392 if r.Siz == 4 { 393 ctxt.Out.Write64(uint64(elf.R_X86_64_GOTTPOFF) | uint64(elfsym)<<32) 394 } else { 395 return false 396 } 397 case objabi.R_CALL: 398 if r.Siz == 4 { 399 if r.Xsym.Type == sym.SDYNIMPORT { 400 if ctxt.DynlinkingGo() { 401 ctxt.Out.Write64(uint64(elf.R_X86_64_PLT32) | uint64(elfsym)<<32) 402 } else { 403 ctxt.Out.Write64(uint64(elf.R_X86_64_GOTPCREL) | uint64(elfsym)<<32) 404 } 405 } else { 406 ctxt.Out.Write64(uint64(elf.R_X86_64_PC32) | uint64(elfsym)<<32) 407 } 408 } else { 409 return false 410 } 411 case objabi.R_PCREL: 412 if r.Siz == 4 { 413 if r.Xsym.Type == sym.SDYNIMPORT && r.Xsym.ElfType() == elf.STT_FUNC { 414 ctxt.Out.Write64(uint64(elf.R_X86_64_PLT32) | uint64(elfsym)<<32) 415 } else { 416 ctxt.Out.Write64(uint64(elf.R_X86_64_PC32) | uint64(elfsym)<<32) 417 } 418 } else { 419 return false 420 } 421 case objabi.R_GOTPCREL: 422 if r.Siz == 4 { 423 ctxt.Out.Write64(uint64(elf.R_X86_64_GOTPCREL) | uint64(elfsym)<<32) 424 } else { 425 return false 426 } 427 } 428 429 ctxt.Out.Write64(uint64(r.Xadd)) 430 return true 431 } 432 433 func machoreloc1(arch *sys.Arch, out *ld.OutBuf, s *sym.Symbol, r *sym.Reloc, sectoff int64) bool { 434 var v uint32 435 436 rs := r.Xsym 437 438 if rs.Type == sym.SHOSTOBJ || r.Type == objabi.R_PCREL || r.Type == objabi.R_GOTPCREL || r.Type == objabi.R_CALL { 439 if rs.Dynid < 0 { 440 ld.Errorf(s, "reloc %d (%s) to non-macho symbol %s type=%d (%s)", r.Type, sym.RelocName(arch, r.Type), rs.Name, rs.Type, rs.Type) 441 return false 442 } 443 444 v = uint32(rs.Dynid) 445 v |= 1 << 27 // external relocation 446 } else { 447 v = uint32(rs.Sect.Extnum) 448 if v == 0 { 449 ld.Errorf(s, "reloc %d (%s) to symbol %s in non-macho section %s type=%d (%s)", r.Type, sym.RelocName(arch, r.Type), rs.Name, rs.Sect.Name, rs.Type, rs.Type) 450 return false 451 } 452 } 453 454 switch r.Type { 455 default: 456 return false 457 458 case objabi.R_ADDR: 459 v |= ld.MACHO_X86_64_RELOC_UNSIGNED << 28 460 461 case objabi.R_CALL: 462 v |= 1 << 24 // pc-relative bit 463 v |= ld.MACHO_X86_64_RELOC_BRANCH << 28 464 465 // NOTE: Only works with 'external' relocation. Forced above. 466 case objabi.R_PCREL: 467 v |= 1 << 24 // pc-relative bit 468 v |= ld.MACHO_X86_64_RELOC_SIGNED << 28 469 case objabi.R_GOTPCREL: 470 v |= 1 << 24 // pc-relative bit 471 v |= ld.MACHO_X86_64_RELOC_GOT_LOAD << 28 472 } 473 474 switch r.Siz { 475 default: 476 return false 477 478 case 1: 479 v |= 0 << 25 480 481 case 2: 482 v |= 1 << 25 483 484 case 4: 485 v |= 2 << 25 486 487 case 8: 488 v |= 3 << 25 489 } 490 491 out.Write32(uint32(sectoff)) 492 out.Write32(v) 493 return true 494 } 495 496 func pereloc1(arch *sys.Arch, out *ld.OutBuf, s *sym.Symbol, r *sym.Reloc, sectoff int64) bool { 497 var v uint32 498 499 rs := r.Xsym 500 501 if rs.Dynid < 0 { 502 ld.Errorf(s, "reloc %d (%s) to non-coff symbol %s type=%d (%s)", r.Type, sym.RelocName(arch, r.Type), rs.Name, rs.Type, rs.Type) 503 return false 504 } 505 506 out.Write32(uint32(sectoff)) 507 out.Write32(uint32(rs.Dynid)) 508 509 switch r.Type { 510 default: 511 return false 512 513 case objabi.R_DWARFSECREF: 514 v = ld.IMAGE_REL_AMD64_SECREL 515 516 case objabi.R_ADDR: 517 if r.Siz == 8 { 518 v = ld.IMAGE_REL_AMD64_ADDR64 519 } else { 520 v = ld.IMAGE_REL_AMD64_ADDR32 521 } 522 523 case objabi.R_CALL, 524 objabi.R_PCREL: 525 v = ld.IMAGE_REL_AMD64_REL32 526 } 527 528 out.Write16(uint16(v)) 529 530 return true 531 } 532 533 func archreloc(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, val int64) (int64, bool) { 534 return val, false 535 } 536 537 func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64 { 538 log.Fatalf("unexpected relocation variant") 539 return t 540 } 541 542 func elfsetupplt(ctxt *ld.Link) { 543 plt := ctxt.Syms.Lookup(".plt", 0) 544 got := ctxt.Syms.Lookup(".got.plt", 0) 545 if plt.Size == 0 { 546 // pushq got+8(IP) 547 plt.AddUint8(0xff) 548 549 plt.AddUint8(0x35) 550 plt.AddPCRelPlus(ctxt.Arch, got, 8) 551 552 // jmpq got+16(IP) 553 plt.AddUint8(0xff) 554 555 plt.AddUint8(0x25) 556 plt.AddPCRelPlus(ctxt.Arch, got, 16) 557 558 // nopl 0(AX) 559 plt.AddUint32(ctxt.Arch, 0x00401f0f) 560 561 // assume got->size == 0 too 562 got.AddAddrPlus(ctxt.Arch, ctxt.Syms.Lookup(".dynamic", 0), 0) 563 564 got.AddUint64(ctxt.Arch, 0) 565 got.AddUint64(ctxt.Arch, 0) 566 } 567 } 568 569 func addpltsym(ctxt *ld.Link, s *sym.Symbol) { 570 if s.Plt() >= 0 { 571 return 572 } 573 574 ld.Adddynsym(ctxt, s) 575 576 if ctxt.IsELF { 577 plt := ctxt.Syms.Lookup(".plt", 0) 578 got := ctxt.Syms.Lookup(".got.plt", 0) 579 rela := ctxt.Syms.Lookup(".rela.plt", 0) 580 if plt.Size == 0 { 581 elfsetupplt(ctxt) 582 } 583 584 // jmpq *got+size(IP) 585 plt.AddUint8(0xff) 586 587 plt.AddUint8(0x25) 588 plt.AddPCRelPlus(ctxt.Arch, got, got.Size) 589 590 // add to got: pointer to current pos in plt 591 got.AddAddrPlus(ctxt.Arch, plt, plt.Size) 592 593 // pushq $x 594 plt.AddUint8(0x68) 595 596 plt.AddUint32(ctxt.Arch, uint32((got.Size-24-8)/8)) 597 598 // jmpq .plt 599 plt.AddUint8(0xe9) 600 601 plt.AddUint32(ctxt.Arch, uint32(-(plt.Size + 4))) 602 603 // rela 604 rela.AddAddrPlus(ctxt.Arch, got, got.Size-8) 605 606 rela.AddUint64(ctxt.Arch, ld.ELF64_R_INFO(uint32(s.Dynid), uint32(elf.R_X86_64_JMP_SLOT))) 607 rela.AddUint64(ctxt.Arch, 0) 608 609 s.SetPlt(int32(plt.Size - 16)) 610 } else if ctxt.HeadType == objabi.Hdarwin { 611 // To do lazy symbol lookup right, we're supposed 612 // to tell the dynamic loader which library each 613 // symbol comes from and format the link info 614 // section just so. I'm too lazy (ha!) to do that 615 // so for now we'll just use non-lazy pointers, 616 // which don't need to be told which library to use. 617 // 618 // https://networkpx.blogspot.com/2009/09/about-lcdyldinfoonly-command.html 619 // has details about what we're avoiding. 620 621 addgotsym(ctxt, s) 622 plt := ctxt.Syms.Lookup(".plt", 0) 623 624 ctxt.Syms.Lookup(".linkedit.plt", 0).AddUint32(ctxt.Arch, uint32(s.Dynid)) 625 626 // jmpq *got+size(IP) 627 s.SetPlt(int32(plt.Size)) 628 629 plt.AddUint8(0xff) 630 plt.AddUint8(0x25) 631 plt.AddPCRelPlus(ctxt.Arch, ctxt.Syms.Lookup(".got", 0), int64(s.Got())) 632 } else { 633 ld.Errorf(s, "addpltsym: unsupported binary format") 634 } 635 } 636 637 func addgotsym(ctxt *ld.Link, s *sym.Symbol) { 638 if s.Got() >= 0 { 639 return 640 } 641 642 ld.Adddynsym(ctxt, s) 643 got := ctxt.Syms.Lookup(".got", 0) 644 s.SetGot(int32(got.Size)) 645 got.AddUint64(ctxt.Arch, 0) 646 647 if ctxt.IsELF { 648 rela := ctxt.Syms.Lookup(".rela", 0) 649 rela.AddAddrPlus(ctxt.Arch, got, int64(s.Got())) 650 rela.AddUint64(ctxt.Arch, ld.ELF64_R_INFO(uint32(s.Dynid), uint32(elf.R_X86_64_GLOB_DAT))) 651 rela.AddUint64(ctxt.Arch, 0) 652 } else if ctxt.HeadType == objabi.Hdarwin { 653 ctxt.Syms.Lookup(".linkedit.got", 0).AddUint32(ctxt.Arch, uint32(s.Dynid)) 654 } else { 655 ld.Errorf(s, "addgotsym: unsupported binary format") 656 } 657 } 658 659 func asmb(ctxt *ld.Link) { 660 if ctxt.Debugvlog != 0 { 661 ctxt.Logf("%5.2f asmb\n", ld.Cputime()) 662 } 663 664 if ctxt.Debugvlog != 0 { 665 ctxt.Logf("%5.2f codeblk\n", ld.Cputime()) 666 } 667 668 if ctxt.IsELF { 669 ld.Asmbelfsetup() 670 } 671 672 sect := ld.Segtext.Sections[0] 673 ctxt.Out.SeekSet(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff)) 674 // 0xCC is INT $3 - breakpoint instruction 675 ld.CodeblkPad(ctxt, int64(sect.Vaddr), int64(sect.Length), []byte{0xCC}) 676 for _, sect = range ld.Segtext.Sections[1:] { 677 ctxt.Out.SeekSet(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff)) 678 ld.Datblk(ctxt, int64(sect.Vaddr), int64(sect.Length)) 679 } 680 681 if ld.Segrodata.Filelen > 0 { 682 if ctxt.Debugvlog != 0 { 683 ctxt.Logf("%5.2f rodatblk\n", ld.Cputime()) 684 } 685 ctxt.Out.SeekSet(int64(ld.Segrodata.Fileoff)) 686 ld.Datblk(ctxt, int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen)) 687 } 688 if ld.Segrelrodata.Filelen > 0 { 689 if ctxt.Debugvlog != 0 { 690 ctxt.Logf("%5.2f relrodatblk\n", ld.Cputime()) 691 } 692 ctxt.Out.SeekSet(int64(ld.Segrelrodata.Fileoff)) 693 ld.Datblk(ctxt, int64(ld.Segrelrodata.Vaddr), int64(ld.Segrelrodata.Filelen)) 694 } 695 696 if ctxt.Debugvlog != 0 { 697 ctxt.Logf("%5.2f datblk\n", ld.Cputime()) 698 } 699 700 ctxt.Out.SeekSet(int64(ld.Segdata.Fileoff)) 701 ld.Datblk(ctxt, int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen)) 702 703 ctxt.Out.SeekSet(int64(ld.Segdwarf.Fileoff)) 704 ld.Dwarfblk(ctxt, int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen)) 705 706 machlink := int64(0) 707 if ctxt.HeadType == objabi.Hdarwin { 708 machlink = ld.Domacholink(ctxt) 709 } 710 711 switch ctxt.HeadType { 712 default: 713 ld.Errorf(nil, "unknown header type %v", ctxt.HeadType) 714 fallthrough 715 716 case objabi.Hplan9: 717 break 718 719 case objabi.Hdarwin: 720 ld.Flag8 = true /* 64-bit addresses */ 721 722 case objabi.Hlinux, 723 objabi.Hfreebsd, 724 objabi.Hnetbsd, 725 objabi.Hopenbsd, 726 objabi.Hdragonfly, 727 objabi.Hsolaris: 728 ld.Flag8 = true /* 64-bit addresses */ 729 730 case objabi.Hnacl, 731 objabi.Hwindows: 732 break 733 } 734 735 ld.Symsize = 0 736 ld.Spsize = 0 737 ld.Lcsize = 0 738 symo := int64(0) 739 if !*ld.FlagS { 740 if ctxt.Debugvlog != 0 { 741 ctxt.Logf("%5.2f sym\n", ld.Cputime()) 742 } 743 switch ctxt.HeadType { 744 default: 745 case objabi.Hplan9: 746 *ld.FlagS = true 747 symo = int64(ld.Segdata.Fileoff + ld.Segdata.Filelen) 748 749 case objabi.Hdarwin: 750 symo = int64(ld.Segdwarf.Fileoff + uint64(ld.Rnd(int64(ld.Segdwarf.Filelen), int64(*ld.FlagRound))) + uint64(machlink)) 751 752 case objabi.Hlinux, 753 objabi.Hfreebsd, 754 objabi.Hnetbsd, 755 objabi.Hopenbsd, 756 objabi.Hdragonfly, 757 objabi.Hsolaris, 758 objabi.Hnacl: 759 symo = int64(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen) 760 symo = ld.Rnd(symo, int64(*ld.FlagRound)) 761 762 case objabi.Hwindows: 763 symo = int64(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen) 764 symo = ld.Rnd(symo, ld.PEFILEALIGN) 765 } 766 767 ctxt.Out.SeekSet(symo) 768 switch ctxt.HeadType { 769 default: 770 if ctxt.IsELF { 771 ctxt.Out.SeekSet(symo) 772 ld.Asmelfsym(ctxt) 773 ctxt.Out.Flush() 774 ctxt.Out.Write(ld.Elfstrdat) 775 776 if ctxt.Debugvlog != 0 { 777 ctxt.Logf("%5.2f dwarf\n", ld.Cputime()) 778 } 779 780 if ctxt.LinkMode == ld.LinkExternal { 781 ld.Elfemitreloc(ctxt) 782 } 783 } 784 785 case objabi.Hplan9: 786 ld.Asmplan9sym(ctxt) 787 ctxt.Out.Flush() 788 789 sym := ctxt.Syms.Lookup("pclntab", 0) 790 if sym != nil { 791 ld.Lcsize = int32(len(sym.P)) 792 ctxt.Out.Write(sym.P) 793 ctxt.Out.Flush() 794 } 795 796 case objabi.Hwindows: 797 if ctxt.Debugvlog != 0 { 798 ctxt.Logf("%5.2f dwarf\n", ld.Cputime()) 799 } 800 801 case objabi.Hdarwin: 802 if ctxt.LinkMode == ld.LinkExternal { 803 ld.Machoemitreloc(ctxt) 804 } 805 } 806 } 807 808 if ctxt.Debugvlog != 0 { 809 ctxt.Logf("%5.2f headr\n", ld.Cputime()) 810 } 811 ctxt.Out.SeekSet(0) 812 switch ctxt.HeadType { 813 default: 814 case objabi.Hplan9: /* plan9 */ 815 magic := int32(4*26*26 + 7) 816 817 magic |= 0x00008000 /* fat header */ 818 ctxt.Out.Write32b(uint32(magic)) /* magic */ 819 ctxt.Out.Write32b(uint32(ld.Segtext.Filelen)) /* sizes */ 820 ctxt.Out.Write32b(uint32(ld.Segdata.Filelen)) 821 ctxt.Out.Write32b(uint32(ld.Segdata.Length - ld.Segdata.Filelen)) 822 ctxt.Out.Write32b(uint32(ld.Symsize)) /* nsyms */ 823 vl := ld.Entryvalue(ctxt) 824 ctxt.Out.Write32b(PADDR(uint32(vl))) /* va of entry */ 825 ctxt.Out.Write32b(uint32(ld.Spsize)) /* sp offsets */ 826 ctxt.Out.Write32b(uint32(ld.Lcsize)) /* line offsets */ 827 ctxt.Out.Write64b(uint64(vl)) /* va of entry */ 828 829 case objabi.Hdarwin: 830 ld.Asmbmacho(ctxt) 831 832 case objabi.Hlinux, 833 objabi.Hfreebsd, 834 objabi.Hnetbsd, 835 objabi.Hopenbsd, 836 objabi.Hdragonfly, 837 objabi.Hsolaris, 838 objabi.Hnacl: 839 ld.Asmbelf(ctxt, symo) 840 841 case objabi.Hwindows: 842 ld.Asmbpe(ctxt) 843 } 844 845 ctxt.Out.Flush() 846 } 847 848 func tlsIEtoLE(s *sym.Symbol, off, size int) { 849 // Transform the PC-relative instruction into a constant load. 850 // That is, 851 // 852 // MOVQ X(IP), REG -> MOVQ $Y, REG 853 // 854 // To determine the instruction and register, we study the op codes. 855 // Consult an AMD64 instruction encoding guide to decipher this. 856 if off < 3 { 857 log.Fatal("R_X86_64_GOTTPOFF reloc not preceded by MOVQ or ADDQ instruction") 858 } 859 op := s.P[off-3 : off] 860 reg := op[2] >> 3 861 862 if op[1] == 0x8b || reg == 4 { 863 // MOVQ 864 if op[0] == 0x4c { 865 op[0] = 0x49 866 } else if size == 4 && op[0] == 0x44 { 867 op[0] = 0x41 868 } 869 if op[1] == 0x8b { 870 op[1] = 0xc7 871 } else { 872 op[1] = 0x81 // special case for SP 873 } 874 op[2] = 0xc0 | reg 875 } else { 876 // An alternate op is ADDQ. This is handled by GNU gold, 877 // but right now is not generated by the Go compiler: 878 // ADDQ X(IP), REG -> ADDQ $Y, REG 879 // Consider adding support for it here. 880 log.Fatalf("expected TLS IE op to be MOVQ, got %v", op) 881 } 882 }