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