github.com/Filosottile/go@v0.0.0-20170906193555-dbed9972d994/src/cmd/link/internal/ppc64/asm.go (about) 1 // Inferno utils/5l/asm.c 2 // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/5l/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 ppc64 32 33 import ( 34 "cmd/internal/objabi" 35 "cmd/link/internal/ld" 36 "encoding/binary" 37 "fmt" 38 "log" 39 ) 40 41 func genplt(ctxt *ld.Link) { 42 // The ppc64 ABI PLT has similar concepts to other 43 // architectures, but is laid out quite differently. When we 44 // see an R_PPC64_REL24 relocation to a dynamic symbol 45 // (indicating that the call needs to go through the PLT), we 46 // generate up to three stubs and reserve a PLT slot. 47 // 48 // 1) The call site will be bl x; nop (where the relocation 49 // applies to the bl). We rewrite this to bl x_stub; ld 50 // r2,24(r1). The ld is necessary because x_stub will save 51 // r2 (the TOC pointer) at 24(r1) (the "TOC save slot"). 52 // 53 // 2) We reserve space for a pointer in the .plt section (once 54 // per referenced dynamic function). .plt is a data 55 // section filled solely by the dynamic linker (more like 56 // .plt.got on other architectures). Initially, the 57 // dynamic linker will fill each slot with a pointer to the 58 // corresponding x@plt entry point. 59 // 60 // 3) We generate the "call stub" x_stub (once per dynamic 61 // function/object file pair). This saves the TOC in the 62 // TOC save slot, reads the function pointer from x's .plt 63 // slot and calls it like any other global entry point 64 // (including setting r12 to the function address). 65 // 66 // 4) We generate the "symbol resolver stub" x@plt (once per 67 // dynamic function). This is solely a branch to the glink 68 // resolver stub. 69 // 70 // 5) We generate the glink resolver stub (only once). This 71 // computes which symbol resolver stub we came through and 72 // invokes the dynamic resolver via a pointer provided by 73 // the dynamic linker. This will patch up the .plt slot to 74 // point directly at the function so future calls go 75 // straight from the call stub to the real function, and 76 // then call the function. 77 78 // NOTE: It's possible we could make ppc64 closer to other 79 // architectures: ppc64's .plt is like .plt.got on other 80 // platforms and ppc64's .glink is like .plt on other 81 // platforms. 82 83 // Find all R_PPC64_REL24 relocations that reference dynamic 84 // imports. Reserve PLT entries for these symbols and 85 // generate call stubs. The call stubs need to live in .text, 86 // which is why we need to do this pass this early. 87 // 88 // This assumes "case 1" from the ABI, where the caller needs 89 // us to save and restore the TOC pointer. 90 var stubs []*ld.Symbol 91 for _, s := range ctxt.Textp { 92 for i := range s.R { 93 r := &s.R[i] 94 if r.Type != 256+ld.R_PPC64_REL24 || r.Sym.Type != ld.SDYNIMPORT { 95 continue 96 } 97 98 // Reserve PLT entry and generate symbol 99 // resolver 100 addpltsym(ctxt, r.Sym) 101 102 // Generate call stub 103 n := fmt.Sprintf("%s.%s", s.Name, r.Sym.Name) 104 105 stub := ctxt.Syms.Lookup(n, 0) 106 if s.Attr.Reachable() { 107 stub.Attr |= ld.AttrReachable 108 } 109 if stub.Size == 0 { 110 // Need outer to resolve .TOC. 111 stub.Outer = s 112 stubs = append(stubs, stub) 113 gencallstub(ctxt, 1, stub, r.Sym) 114 } 115 116 // Update the relocation to use the call stub 117 r.Sym = stub 118 119 // Restore TOC after bl. The compiler put a 120 // nop here for us to overwrite. 121 const o1 = 0xe8410018 // ld r2,24(r1) 122 ctxt.Arch.ByteOrder.PutUint32(s.P[r.Off+4:], o1) 123 } 124 } 125 // Put call stubs at the beginning (instead of the end). 126 // So when resolving the relocations to calls to the stubs, 127 // the addresses are known and trampolines can be inserted 128 // when necessary. 129 ctxt.Textp = append(stubs, ctxt.Textp...) 130 } 131 132 func genaddmoduledata(ctxt *ld.Link) { 133 addmoduledata := ctxt.Syms.ROLookup("runtime.addmoduledata", 0) 134 if addmoduledata.Type == ld.STEXT { 135 return 136 } 137 addmoduledata.Attr |= ld.AttrReachable 138 initfunc := ctxt.Syms.Lookup("go.link.addmoduledata", 0) 139 initfunc.Type = ld.STEXT 140 initfunc.Attr |= ld.AttrLocal 141 initfunc.Attr |= ld.AttrReachable 142 o := func(op uint32) { 143 ld.Adduint32(ctxt, initfunc, op) 144 } 145 // addis r2, r12, .TOC.-func@ha 146 rel := ld.Addrel(initfunc) 147 rel.Off = int32(initfunc.Size) 148 rel.Siz = 8 149 rel.Sym = ctxt.Syms.Lookup(".TOC.", 0) 150 rel.Type = objabi.R_ADDRPOWER_PCREL 151 o(0x3c4c0000) 152 // addi r2, r2, .TOC.-func@l 153 o(0x38420000) 154 // mflr r31 155 o(0x7c0802a6) 156 // stdu r31, -32(r1) 157 o(0xf801ffe1) 158 // addis r3, r2, local.moduledata@got@ha 159 rel = ld.Addrel(initfunc) 160 rel.Off = int32(initfunc.Size) 161 rel.Siz = 8 162 rel.Sym = ctxt.Syms.Lookup("local.moduledata", 0) 163 rel.Type = objabi.R_ADDRPOWER_GOT 164 o(0x3c620000) 165 // ld r3, local.moduledata@got@l(r3) 166 o(0xe8630000) 167 // bl runtime.addmoduledata 168 rel = ld.Addrel(initfunc) 169 rel.Off = int32(initfunc.Size) 170 rel.Siz = 4 171 rel.Sym = addmoduledata 172 rel.Type = objabi.R_CALLPOWER 173 o(0x48000001) 174 // nop 175 o(0x60000000) 176 // ld r31, 0(r1) 177 o(0xe8010000) 178 // mtlr r31 179 o(0x7c0803a6) 180 // addi r1,r1,32 181 o(0x38210020) 182 // blr 183 o(0x4e800020) 184 185 initarray_entry := ctxt.Syms.Lookup("go.link.addmoduledatainit", 0) 186 ctxt.Textp = append(ctxt.Textp, initfunc) 187 initarray_entry.Attr |= ld.AttrReachable 188 initarray_entry.Attr |= ld.AttrLocal 189 initarray_entry.Type = ld.SINITARR 190 ld.Addaddr(ctxt, initarray_entry, initfunc) 191 } 192 193 func gentext(ctxt *ld.Link) { 194 if ctxt.DynlinkingGo() { 195 genaddmoduledata(ctxt) 196 } 197 198 if ld.Linkmode == ld.LinkInternal { 199 genplt(ctxt) 200 } 201 } 202 203 // Construct a call stub in stub that calls symbol targ via its PLT 204 // entry. 205 func gencallstub(ctxt *ld.Link, abicase int, stub *ld.Symbol, targ *ld.Symbol) { 206 if abicase != 1 { 207 // If we see R_PPC64_TOCSAVE or R_PPC64_REL24_NOTOC 208 // relocations, we'll need to implement cases 2 and 3. 209 log.Fatalf("gencallstub only implements case 1 calls") 210 } 211 212 plt := ctxt.Syms.Lookup(".plt", 0) 213 214 stub.Type = ld.STEXT 215 216 // Save TOC pointer in TOC save slot 217 ld.Adduint32(ctxt, stub, 0xf8410018) // std r2,24(r1) 218 219 // Load the function pointer from the PLT. 220 r := ld.Addrel(stub) 221 222 r.Off = int32(stub.Size) 223 r.Sym = plt 224 r.Add = int64(targ.Plt) 225 r.Siz = 2 226 if ctxt.Arch.ByteOrder == binary.BigEndian { 227 r.Off += int32(r.Siz) 228 } 229 r.Type = objabi.R_POWER_TOC 230 r.Variant = ld.RV_POWER_HA 231 ld.Adduint32(ctxt, stub, 0x3d820000) // addis r12,r2,targ@plt@toc@ha 232 r = ld.Addrel(stub) 233 r.Off = int32(stub.Size) 234 r.Sym = plt 235 r.Add = int64(targ.Plt) 236 r.Siz = 2 237 if ctxt.Arch.ByteOrder == binary.BigEndian { 238 r.Off += int32(r.Siz) 239 } 240 r.Type = objabi.R_POWER_TOC 241 r.Variant = ld.RV_POWER_LO 242 ld.Adduint32(ctxt, stub, 0xe98c0000) // ld r12,targ@plt@toc@l(r12) 243 244 // Jump to the loaded pointer 245 ld.Adduint32(ctxt, stub, 0x7d8903a6) // mtctr r12 246 ld.Adduint32(ctxt, stub, 0x4e800420) // bctr 247 } 248 249 func adddynrel(ctxt *ld.Link, s *ld.Symbol, r *ld.Reloc) bool { 250 targ := r.Sym 251 252 switch r.Type { 253 default: 254 if r.Type >= 256 { 255 ld.Errorf(s, "unexpected relocation type %d (%s)", r.Type, ld.RelocName(r.Type)) 256 return false 257 } 258 259 // Handle relocations found in ELF object files. 260 case 256 + ld.R_PPC64_REL24: 261 r.Type = objabi.R_CALLPOWER 262 263 // This is a local call, so the caller isn't setting 264 // up r12 and r2 is the same for the caller and 265 // callee. Hence, we need to go to the local entry 266 // point. (If we don't do this, the callee will try 267 // to use r12 to compute r2.) 268 r.Add += int64(r.Sym.Localentry) * 4 269 270 if targ.Type == ld.SDYNIMPORT { 271 // Should have been handled in elfsetupplt 272 ld.Errorf(s, "unexpected R_PPC64_REL24 for dyn import") 273 } 274 275 return true 276 277 case 256 + ld.R_PPC_REL32: 278 r.Type = objabi.R_PCREL 279 r.Add += 4 280 281 if targ.Type == ld.SDYNIMPORT { 282 ld.Errorf(s, "unexpected R_PPC_REL32 for dyn import") 283 } 284 285 return true 286 287 case 256 + ld.R_PPC64_ADDR64: 288 r.Type = objabi.R_ADDR 289 if targ.Type == ld.SDYNIMPORT { 290 // These happen in .toc sections 291 ld.Adddynsym(ctxt, targ) 292 293 rela := ctxt.Syms.Lookup(".rela", 0) 294 ld.Addaddrplus(ctxt, rela, s, int64(r.Off)) 295 ld.Adduint64(ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_PPC64_ADDR64)) 296 ld.Adduint64(ctxt, rela, uint64(r.Add)) 297 r.Type = 256 // ignore during relocsym 298 } 299 300 return true 301 302 case 256 + ld.R_PPC64_TOC16: 303 r.Type = objabi.R_POWER_TOC 304 r.Variant = ld.RV_POWER_LO | ld.RV_CHECK_OVERFLOW 305 return true 306 307 case 256 + ld.R_PPC64_TOC16_LO: 308 r.Type = objabi.R_POWER_TOC 309 r.Variant = ld.RV_POWER_LO 310 return true 311 312 case 256 + ld.R_PPC64_TOC16_HA: 313 r.Type = objabi.R_POWER_TOC 314 r.Variant = ld.RV_POWER_HA | ld.RV_CHECK_OVERFLOW 315 return true 316 317 case 256 + ld.R_PPC64_TOC16_HI: 318 r.Type = objabi.R_POWER_TOC 319 r.Variant = ld.RV_POWER_HI | ld.RV_CHECK_OVERFLOW 320 return true 321 322 case 256 + ld.R_PPC64_TOC16_DS: 323 r.Type = objabi.R_POWER_TOC 324 r.Variant = ld.RV_POWER_DS | ld.RV_CHECK_OVERFLOW 325 return true 326 327 case 256 + ld.R_PPC64_TOC16_LO_DS: 328 r.Type = objabi.R_POWER_TOC 329 r.Variant = ld.RV_POWER_DS 330 return true 331 332 case 256 + ld.R_PPC64_REL16_LO: 333 r.Type = objabi.R_PCREL 334 r.Variant = ld.RV_POWER_LO 335 r.Add += 2 // Compensate for relocation size of 2 336 return true 337 338 case 256 + ld.R_PPC64_REL16_HI: 339 r.Type = objabi.R_PCREL 340 r.Variant = ld.RV_POWER_HI | ld.RV_CHECK_OVERFLOW 341 r.Add += 2 342 return true 343 344 case 256 + ld.R_PPC64_REL16_HA: 345 r.Type = objabi.R_PCREL 346 r.Variant = ld.RV_POWER_HA | ld.RV_CHECK_OVERFLOW 347 r.Add += 2 348 return true 349 } 350 351 // Handle references to ELF symbols from our own object files. 352 if targ.Type != ld.SDYNIMPORT { 353 return true 354 } 355 356 // TODO(austin): Translate our relocations to ELF 357 358 return false 359 } 360 361 func elfreloc1(ctxt *ld.Link, r *ld.Reloc, sectoff int64) bool { 362 ld.Thearch.Vput(uint64(sectoff)) 363 364 elfsym := r.Xsym.ElfsymForReloc() 365 switch r.Type { 366 default: 367 return false 368 case objabi.R_ADDR: 369 switch r.Siz { 370 case 4: 371 ld.Thearch.Vput(ld.R_PPC64_ADDR32 | uint64(elfsym)<<32) 372 case 8: 373 ld.Thearch.Vput(ld.R_PPC64_ADDR64 | uint64(elfsym)<<32) 374 default: 375 return false 376 } 377 case objabi.R_POWER_TLS: 378 ld.Thearch.Vput(ld.R_PPC64_TLS | uint64(elfsym)<<32) 379 case objabi.R_POWER_TLS_LE: 380 ld.Thearch.Vput(ld.R_PPC64_TPREL16 | uint64(elfsym)<<32) 381 case objabi.R_POWER_TLS_IE: 382 ld.Thearch.Vput(ld.R_PPC64_GOT_TPREL16_HA | uint64(elfsym)<<32) 383 ld.Thearch.Vput(uint64(r.Xadd)) 384 ld.Thearch.Vput(uint64(sectoff + 4)) 385 ld.Thearch.Vput(ld.R_PPC64_GOT_TPREL16_LO_DS | uint64(elfsym)<<32) 386 case objabi.R_ADDRPOWER: 387 ld.Thearch.Vput(ld.R_PPC64_ADDR16_HA | uint64(elfsym)<<32) 388 ld.Thearch.Vput(uint64(r.Xadd)) 389 ld.Thearch.Vput(uint64(sectoff + 4)) 390 ld.Thearch.Vput(ld.R_PPC64_ADDR16_LO | uint64(elfsym)<<32) 391 case objabi.R_ADDRPOWER_DS: 392 ld.Thearch.Vput(ld.R_PPC64_ADDR16_HA | uint64(elfsym)<<32) 393 ld.Thearch.Vput(uint64(r.Xadd)) 394 ld.Thearch.Vput(uint64(sectoff + 4)) 395 ld.Thearch.Vput(ld.R_PPC64_ADDR16_LO_DS | uint64(elfsym)<<32) 396 case objabi.R_ADDRPOWER_GOT: 397 ld.Thearch.Vput(ld.R_PPC64_GOT16_HA | uint64(elfsym)<<32) 398 ld.Thearch.Vput(uint64(r.Xadd)) 399 ld.Thearch.Vput(uint64(sectoff + 4)) 400 ld.Thearch.Vput(ld.R_PPC64_GOT16_LO_DS | uint64(elfsym)<<32) 401 case objabi.R_ADDRPOWER_PCREL: 402 ld.Thearch.Vput(ld.R_PPC64_REL16_HA | uint64(elfsym)<<32) 403 ld.Thearch.Vput(uint64(r.Xadd)) 404 ld.Thearch.Vput(uint64(sectoff + 4)) 405 ld.Thearch.Vput(ld.R_PPC64_REL16_LO | uint64(elfsym)<<32) 406 r.Xadd += 4 407 case objabi.R_ADDRPOWER_TOCREL: 408 ld.Thearch.Vput(ld.R_PPC64_TOC16_HA | uint64(elfsym)<<32) 409 ld.Thearch.Vput(uint64(r.Xadd)) 410 ld.Thearch.Vput(uint64(sectoff + 4)) 411 ld.Thearch.Vput(ld.R_PPC64_TOC16_LO | uint64(elfsym)<<32) 412 case objabi.R_ADDRPOWER_TOCREL_DS: 413 ld.Thearch.Vput(ld.R_PPC64_TOC16_HA | uint64(elfsym)<<32) 414 ld.Thearch.Vput(uint64(r.Xadd)) 415 ld.Thearch.Vput(uint64(sectoff + 4)) 416 ld.Thearch.Vput(ld.R_PPC64_TOC16_LO_DS | uint64(elfsym)<<32) 417 case objabi.R_CALLPOWER: 418 if r.Siz != 4 { 419 return false 420 } 421 ld.Thearch.Vput(ld.R_PPC64_REL24 | uint64(elfsym)<<32) 422 423 } 424 ld.Thearch.Vput(uint64(r.Xadd)) 425 426 return true 427 } 428 429 func elfsetupplt(ctxt *ld.Link) { 430 plt := ctxt.Syms.Lookup(".plt", 0) 431 if plt.Size == 0 { 432 // The dynamic linker stores the address of the 433 // dynamic resolver and the DSO identifier in the two 434 // doublewords at the beginning of the .plt section 435 // before the PLT array. Reserve space for these. 436 plt.Size = 16 437 } 438 } 439 440 func machoreloc1(s *ld.Symbol, r *ld.Reloc, sectoff int64) bool { 441 return false 442 } 443 444 // Return the value of .TOC. for symbol s 445 func symtoc(ctxt *ld.Link, s *ld.Symbol) int64 { 446 var toc *ld.Symbol 447 448 if s.Outer != nil { 449 toc = ctxt.Syms.ROLookup(".TOC.", int(s.Outer.Version)) 450 } else { 451 toc = ctxt.Syms.ROLookup(".TOC.", int(s.Version)) 452 } 453 454 if toc == nil { 455 ld.Errorf(s, "TOC-relative relocation in object without .TOC.") 456 return 0 457 } 458 459 return toc.Value 460 } 461 462 func archrelocaddr(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, val *int64) bool { 463 var o1, o2 uint32 464 if ctxt.Arch.ByteOrder == binary.BigEndian { 465 o1 = uint32(*val >> 32) 466 o2 = uint32(*val) 467 } else { 468 o1 = uint32(*val) 469 o2 = uint32(*val >> 32) 470 } 471 472 // We are spreading a 31-bit address across two instructions, putting the 473 // high (adjusted) part in the low 16 bits of the first instruction and the 474 // low part in the low 16 bits of the second instruction, or, in the DS case, 475 // bits 15-2 (inclusive) of the address into bits 15-2 of the second 476 // instruction (it is an error in this case if the low 2 bits of the address 477 // are non-zero). 478 479 t := ld.Symaddr(r.Sym) + r.Add 480 if t < 0 || t >= 1<<31 { 481 ld.Errorf(s, "relocation for %s is too big (>=2G): %d", s.Name, ld.Symaddr(r.Sym)) 482 } 483 if t&0x8000 != 0 { 484 t += 0x10000 485 } 486 487 switch r.Type { 488 case objabi.R_ADDRPOWER: 489 o1 |= (uint32(t) >> 16) & 0xffff 490 o2 |= uint32(t) & 0xffff 491 case objabi.R_ADDRPOWER_DS: 492 o1 |= (uint32(t) >> 16) & 0xffff 493 if t&3 != 0 { 494 ld.Errorf(s, "bad DS reloc for %s: %d", s.Name, ld.Symaddr(r.Sym)) 495 } 496 o2 |= uint32(t) & 0xfffc 497 default: 498 return false 499 } 500 501 if ctxt.Arch.ByteOrder == binary.BigEndian { 502 *val = int64(o1)<<32 | int64(o2) 503 } else { 504 *val = int64(o2)<<32 | int64(o1) 505 } 506 return true 507 } 508 509 // resolve direct jump relocation r in s, and add trampoline if necessary 510 func trampoline(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol) { 511 512 // Trampolines are created if the branch offset is too large and the linker cannot insert a call stub to handle it. 513 // For internal linking, trampolines are always created for long calls. 514 // For external linking, the linker can insert a call stub to handle a long call, but depends on having the TOC address in 515 // r2. For those build modes with external linking where the TOC address is not maintained in r2, trampolines must be created. 516 if ld.Linkmode == ld.LinkExternal && (ctxt.DynlinkingGo() || ld.Buildmode == ld.BuildmodeCArchive || ld.Buildmode == ld.BuildmodeCShared || ld.Buildmode == ld.BuildmodePIE) { 517 // No trampolines needed since r2 contains the TOC 518 return 519 } 520 521 t := ld.Symaddr(r.Sym) + r.Add - (s.Value + int64(r.Off)) 522 switch r.Type { 523 case objabi.R_CALLPOWER: 524 525 // If branch offset is too far then create a trampoline. 526 527 if (ld.Linkmode == ld.LinkExternal && s.Sect != r.Sym.Sect) || (ld.Linkmode == ld.LinkInternal && int64(int32(t<<6)>>6) != t) || (*ld.FlagDebugTramp > 1 && s.File != r.Sym.File) { 528 var tramp *ld.Symbol 529 for i := 0; ; i++ { 530 531 // Using r.Add as part of the name is significant in functions like duffzero where the call 532 // target is at some offset within the function. Calls to duff+8 and duff+256 must appear as 533 // distinct trampolines. 534 535 name := r.Sym.Name 536 if r.Add == 0 { 537 name = name + fmt.Sprintf("-tramp%d", i) 538 } else { 539 name = name + fmt.Sprintf("%+x-tramp%d", r.Add, i) 540 } 541 542 // Look up the trampoline in case it already exists 543 544 tramp = ctxt.Syms.Lookup(name, int(r.Sym.Version)) 545 if tramp.Value == 0 { 546 break 547 } 548 549 t = ld.Symaddr(tramp) + r.Add - (s.Value + int64(r.Off)) 550 551 // With internal linking, the trampoline can be used if it is not too far. 552 // With external linking, the trampoline must be in this section for it to be reused. 553 if (ld.Linkmode == ld.LinkInternal && int64(int32(t<<6)>>6) == t) || (ld.Linkmode == ld.LinkExternal && s.Sect == tramp.Sect) { 554 break 555 } 556 } 557 if tramp.Type == 0 { 558 if ctxt.DynlinkingGo() || ld.Buildmode == ld.BuildmodeCArchive || ld.Buildmode == ld.BuildmodeCShared || ld.Buildmode == ld.BuildmodePIE { 559 // Should have returned for above cases 560 ld.Errorf(s, "unexpected trampoline for shared or dynamic linking\n") 561 } else { 562 ctxt.AddTramp(tramp) 563 gentramp(tramp, r.Sym, int64(r.Add)) 564 } 565 } 566 r.Sym = tramp 567 r.Add = 0 // This was folded into the trampoline target address 568 r.Done = false 569 } 570 default: 571 ld.Errorf(s, "trampoline called with non-jump reloc: %d (%s)", r.Type, ld.RelocName(r.Type)) 572 } 573 } 574 575 func gentramp(tramp, target *ld.Symbol, offset int64) { 576 // Used for default build mode for an executable 577 // Address of the call target is generated using 578 // relocation and doesn't depend on r2 (TOC). 579 tramp.Size = 16 // 4 instructions 580 tramp.P = make([]byte, tramp.Size) 581 t := ld.Symaddr(target) + offset 582 o1 := uint32(0x3fe00000) // lis r31,targetaddr hi 583 o2 := uint32(0x3bff0000) // addi r31,targetaddr lo 584 // With external linking, the target address must be 585 // relocated using LO and HA 586 if ld.Linkmode == ld.LinkExternal { 587 tr := ld.Addrel(tramp) 588 tr.Off = 0 589 tr.Type = objabi.R_ADDRPOWER 590 tr.Siz = 8 // generates 2 relocations: HA + LO 591 tr.Sym = target 592 tr.Add = offset 593 } else { 594 // adjustment needed if lo has sign bit set 595 // when using addi to compute address 596 val := uint32((t & 0xffff0000) >> 16) 597 if t&0x8000 != 0 { 598 val += 1 599 } 600 o1 |= val // hi part of addr 601 o2 |= uint32(t & 0xffff) // lo part of addr 602 } 603 o3 := uint32(0x7fe903a6) // mtctr r31 604 o4 := uint32(0x4e800420) // bctr 605 ld.SysArch.ByteOrder.PutUint32(tramp.P, o1) 606 ld.SysArch.ByteOrder.PutUint32(tramp.P[4:], o2) 607 ld.SysArch.ByteOrder.PutUint32(tramp.P[8:], o3) 608 ld.SysArch.ByteOrder.PutUint32(tramp.P[12:], o4) 609 } 610 611 func archreloc(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, val *int64) bool { 612 if ld.Linkmode == ld.LinkExternal { 613 switch r.Type { 614 default: 615 return false 616 case objabi.R_POWER_TLS, objabi.R_POWER_TLS_LE, objabi.R_POWER_TLS_IE: 617 r.Done = false 618 // check Outer is nil, Type is TLSBSS? 619 r.Xadd = r.Add 620 r.Xsym = r.Sym 621 return true 622 case objabi.R_ADDRPOWER, 623 objabi.R_ADDRPOWER_DS, 624 objabi.R_ADDRPOWER_TOCREL, 625 objabi.R_ADDRPOWER_TOCREL_DS, 626 objabi.R_ADDRPOWER_GOT, 627 objabi.R_ADDRPOWER_PCREL: 628 r.Done = false 629 630 // set up addend for eventual relocation via outer symbol. 631 rs := r.Sym 632 r.Xadd = r.Add 633 for rs.Outer != nil { 634 r.Xadd += ld.Symaddr(rs) - ld.Symaddr(rs.Outer) 635 rs = rs.Outer 636 } 637 638 if rs.Type != ld.SHOSTOBJ && rs.Type != ld.SDYNIMPORT && rs.Sect == nil { 639 ld.Errorf(s, "missing section for %s", rs.Name) 640 } 641 r.Xsym = rs 642 643 return true 644 case objabi.R_CALLPOWER: 645 r.Done = false 646 r.Xsym = r.Sym 647 r.Xadd = r.Add 648 return true 649 } 650 } 651 652 switch r.Type { 653 case objabi.R_CONST: 654 *val = r.Add 655 return true 656 case objabi.R_GOTOFF: 657 *val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ctxt.Syms.Lookup(".got", 0)) 658 return true 659 case objabi.R_ADDRPOWER, objabi.R_ADDRPOWER_DS: 660 return archrelocaddr(ctxt, r, s, val) 661 case objabi.R_CALLPOWER: 662 // Bits 6 through 29 = (S + A - P) >> 2 663 664 t := ld.Symaddr(r.Sym) + r.Add - (s.Value + int64(r.Off)) 665 666 if t&3 != 0 { 667 ld.Errorf(s, "relocation for %s+%d is not aligned: %d", r.Sym.Name, r.Off, t) 668 } 669 // If branch offset is too far then create a trampoline. 670 671 if int64(int32(t<<6)>>6) != t { 672 ld.Errorf(s, "direct call too far: %s %x", r.Sym.Name, t) 673 } 674 *val |= int64(uint32(t) &^ 0xfc000003) 675 return true 676 case objabi.R_POWER_TOC: // S + A - .TOC. 677 *val = ld.Symaddr(r.Sym) + r.Add - symtoc(ctxt, s) 678 679 return true 680 case objabi.R_POWER_TLS_LE: 681 // The thread pointer points 0x7000 bytes after the start of the the 682 // thread local storage area as documented in section "3.7.2 TLS 683 // Runtime Handling" of "Power Architecture 64-Bit ELF V2 ABI 684 // Specification". 685 v := r.Sym.Value - 0x7000 686 if int64(int16(v)) != v { 687 ld.Errorf(s, "TLS offset out of range %d", v) 688 } 689 *val = (*val &^ 0xffff) | (v & 0xffff) 690 return true 691 } 692 693 return false 694 } 695 696 func archrelocvariant(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, t int64) int64 { 697 switch r.Variant & ld.RV_TYPE_MASK { 698 default: 699 ld.Errorf(s, "unexpected relocation variant %d", r.Variant) 700 fallthrough 701 702 case ld.RV_NONE: 703 return t 704 705 case ld.RV_POWER_LO: 706 if r.Variant&ld.RV_CHECK_OVERFLOW != 0 { 707 // Whether to check for signed or unsigned 708 // overflow depends on the instruction 709 var o1 uint32 710 if ctxt.Arch.ByteOrder == binary.BigEndian { 711 o1 = ld.Be32(s.P[r.Off-2:]) 712 } else { 713 o1 = ld.Le32(s.P[r.Off:]) 714 } 715 switch o1 >> 26 { 716 case 24, // ori 717 26, // xori 718 28: // andi 719 if t>>16 != 0 { 720 goto overflow 721 } 722 723 default: 724 if int64(int16(t)) != t { 725 goto overflow 726 } 727 } 728 } 729 730 return int64(int16(t)) 731 732 case ld.RV_POWER_HA: 733 t += 0x8000 734 fallthrough 735 736 // Fallthrough 737 case ld.RV_POWER_HI: 738 t >>= 16 739 740 if r.Variant&ld.RV_CHECK_OVERFLOW != 0 { 741 // Whether to check for signed or unsigned 742 // overflow depends on the instruction 743 var o1 uint32 744 if ctxt.Arch.ByteOrder == binary.BigEndian { 745 o1 = ld.Be32(s.P[r.Off-2:]) 746 } else { 747 o1 = ld.Le32(s.P[r.Off:]) 748 } 749 switch o1 >> 26 { 750 case 25, // oris 751 27, // xoris 752 29: // andis 753 if t>>16 != 0 { 754 goto overflow 755 } 756 757 default: 758 if int64(int16(t)) != t { 759 goto overflow 760 } 761 } 762 } 763 764 return int64(int16(t)) 765 766 case ld.RV_POWER_DS: 767 var o1 uint32 768 if ctxt.Arch.ByteOrder == binary.BigEndian { 769 o1 = uint32(ld.Be16(s.P[r.Off:])) 770 } else { 771 o1 = uint32(ld.Le16(s.P[r.Off:])) 772 } 773 if t&3 != 0 { 774 ld.Errorf(s, "relocation for %s+%d is not aligned: %d", r.Sym.Name, r.Off, t) 775 } 776 if (r.Variant&ld.RV_CHECK_OVERFLOW != 0) && int64(int16(t)) != t { 777 goto overflow 778 } 779 return int64(o1)&0x3 | int64(int16(t)) 780 } 781 782 overflow: 783 ld.Errorf(s, "relocation for %s+%d is too big: %d", r.Sym.Name, r.Off, t) 784 return t 785 } 786 787 func addpltsym(ctxt *ld.Link, s *ld.Symbol) { 788 if s.Plt >= 0 { 789 return 790 } 791 792 ld.Adddynsym(ctxt, s) 793 794 if ld.Iself { 795 plt := ctxt.Syms.Lookup(".plt", 0) 796 rela := ctxt.Syms.Lookup(".rela.plt", 0) 797 if plt.Size == 0 { 798 elfsetupplt(ctxt) 799 } 800 801 // Create the glink resolver if necessary 802 glink := ensureglinkresolver(ctxt) 803 804 // Write symbol resolver stub (just a branch to the 805 // glink resolver stub) 806 r := ld.Addrel(glink) 807 808 r.Sym = glink 809 r.Off = int32(glink.Size) 810 r.Siz = 4 811 r.Type = objabi.R_CALLPOWER 812 ld.Adduint32(ctxt, glink, 0x48000000) // b .glink 813 814 // In the ppc64 ABI, the dynamic linker is responsible 815 // for writing the entire PLT. We just need to 816 // reserve 8 bytes for each PLT entry and generate a 817 // JMP_SLOT dynamic relocation for it. 818 // 819 // TODO(austin): ABI v1 is different 820 s.Plt = int32(plt.Size) 821 822 plt.Size += 8 823 824 ld.Addaddrplus(ctxt, rela, plt, int64(s.Plt)) 825 ld.Adduint64(ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_PPC64_JMP_SLOT)) 826 ld.Adduint64(ctxt, rela, 0) 827 } else { 828 ld.Errorf(s, "addpltsym: unsupported binary format") 829 } 830 } 831 832 // Generate the glink resolver stub if necessary and return the .glink section 833 func ensureglinkresolver(ctxt *ld.Link) *ld.Symbol { 834 glink := ctxt.Syms.Lookup(".glink", 0) 835 if glink.Size != 0 { 836 return glink 837 } 838 839 // This is essentially the resolver from the ppc64 ELF ABI. 840 // At entry, r12 holds the address of the symbol resolver stub 841 // for the target routine and the argument registers hold the 842 // arguments for the target routine. 843 // 844 // This stub is PIC, so first get the PC of label 1 into r11. 845 // Other things will be relative to this. 846 ld.Adduint32(ctxt, glink, 0x7c0802a6) // mflr r0 847 ld.Adduint32(ctxt, glink, 0x429f0005) // bcl 20,31,1f 848 ld.Adduint32(ctxt, glink, 0x7d6802a6) // 1: mflr r11 849 ld.Adduint32(ctxt, glink, 0x7c0803a6) // mtlf r0 850 851 // Compute the .plt array index from the entry point address. 852 // Because this is PIC, everything is relative to label 1b (in 853 // r11): 854 // r0 = ((r12 - r11) - (res_0 - r11)) / 4 = (r12 - res_0) / 4 855 ld.Adduint32(ctxt, glink, 0x3800ffd0) // li r0,-(res_0-1b)=-48 856 ld.Adduint32(ctxt, glink, 0x7c006214) // add r0,r0,r12 857 ld.Adduint32(ctxt, glink, 0x7c0b0050) // sub r0,r0,r11 858 ld.Adduint32(ctxt, glink, 0x7800f082) // srdi r0,r0,2 859 860 // r11 = address of the first byte of the PLT 861 r := ld.Addrel(glink) 862 863 r.Off = int32(glink.Size) 864 r.Sym = ctxt.Syms.Lookup(".plt", 0) 865 r.Siz = 8 866 r.Type = objabi.R_ADDRPOWER 867 868 ld.Adduint32(ctxt, glink, 0x3d600000) // addis r11,0,.plt@ha 869 ld.Adduint32(ctxt, glink, 0x396b0000) // addi r11,r11,.plt@l 870 871 // Load r12 = dynamic resolver address and r11 = DSO 872 // identifier from the first two doublewords of the PLT. 873 ld.Adduint32(ctxt, glink, 0xe98b0000) // ld r12,0(r11) 874 ld.Adduint32(ctxt, glink, 0xe96b0008) // ld r11,8(r11) 875 876 // Jump to the dynamic resolver 877 ld.Adduint32(ctxt, glink, 0x7d8903a6) // mtctr r12 878 ld.Adduint32(ctxt, glink, 0x4e800420) // bctr 879 880 // The symbol resolvers must immediately follow. 881 // res_0: 882 883 // Add DT_PPC64_GLINK .dynamic entry, which points to 32 bytes 884 // before the first symbol resolver stub. 885 s := ctxt.Syms.Lookup(".dynamic", 0) 886 887 ld.Elfwritedynentsymplus(ctxt, s, ld.DT_PPC64_GLINK, glink, glink.Size-32) 888 889 return glink 890 } 891 892 func asmb(ctxt *ld.Link) { 893 if ctxt.Debugvlog != 0 { 894 ctxt.Logf("%5.2f asmb\n", ld.Cputime()) 895 } 896 897 if ld.Iself { 898 ld.Asmbelfsetup() 899 } 900 901 for _, sect := range ld.Segtext.Sections { 902 ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff)) 903 // Handle additional text sections with Codeblk 904 if sect.Name == ".text" { 905 ld.Codeblk(ctxt, int64(sect.Vaddr), int64(sect.Length)) 906 } else { 907 ld.Datblk(ctxt, int64(sect.Vaddr), int64(sect.Length)) 908 } 909 } 910 911 if ld.Segrodata.Filelen > 0 { 912 if ctxt.Debugvlog != 0 { 913 ctxt.Logf("%5.2f rodatblk\n", ld.Cputime()) 914 } 915 ld.Cseek(int64(ld.Segrodata.Fileoff)) 916 ld.Datblk(ctxt, int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen)) 917 } 918 if ld.Segrelrodata.Filelen > 0 { 919 if ctxt.Debugvlog != 0 { 920 ctxt.Logf("%5.2f relrodatblk\n", ld.Cputime()) 921 } 922 ld.Cseek(int64(ld.Segrelrodata.Fileoff)) 923 ld.Datblk(ctxt, int64(ld.Segrelrodata.Vaddr), int64(ld.Segrelrodata.Filelen)) 924 } 925 926 if ctxt.Debugvlog != 0 { 927 ctxt.Logf("%5.2f datblk\n", ld.Cputime()) 928 } 929 930 ld.Cseek(int64(ld.Segdata.Fileoff)) 931 ld.Datblk(ctxt, int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen)) 932 933 ld.Cseek(int64(ld.Segdwarf.Fileoff)) 934 ld.Dwarfblk(ctxt, int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen)) 935 936 /* output symbol table */ 937 ld.Symsize = 0 938 939 ld.Lcsize = 0 940 symo := uint32(0) 941 if !*ld.FlagS { 942 // TODO: rationalize 943 if ctxt.Debugvlog != 0 { 944 ctxt.Logf("%5.2f sym\n", ld.Cputime()) 945 } 946 switch ld.Headtype { 947 default: 948 if ld.Iself { 949 symo = uint32(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen) 950 symo = uint32(ld.Rnd(int64(symo), int64(*ld.FlagRound))) 951 } 952 953 case objabi.Hplan9: 954 symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen) 955 } 956 957 ld.Cseek(int64(symo)) 958 switch ld.Headtype { 959 default: 960 if ld.Iself { 961 if ctxt.Debugvlog != 0 { 962 ctxt.Logf("%5.2f elfsym\n", ld.Cputime()) 963 } 964 ld.Asmelfsym(ctxt) 965 ld.Cflush() 966 ld.Cwrite(ld.Elfstrdat) 967 968 if ld.Linkmode == ld.LinkExternal { 969 ld.Elfemitreloc(ctxt) 970 } 971 } 972 973 case objabi.Hplan9: 974 ld.Asmplan9sym(ctxt) 975 ld.Cflush() 976 977 sym := ctxt.Syms.Lookup("pclntab", 0) 978 if sym != nil { 979 ld.Lcsize = int32(len(sym.P)) 980 for i := 0; int32(i) < ld.Lcsize; i++ { 981 ld.Cput(sym.P[i]) 982 } 983 984 ld.Cflush() 985 } 986 } 987 } 988 989 if ctxt.Debugvlog != 0 { 990 ctxt.Logf("%5.2f header\n", ld.Cputime()) 991 } 992 ld.Cseek(0) 993 switch ld.Headtype { 994 default: 995 case objabi.Hplan9: /* plan 9 */ 996 ld.Thearch.Lput(0x647) /* magic */ 997 ld.Thearch.Lput(uint32(ld.Segtext.Filelen)) /* sizes */ 998 ld.Thearch.Lput(uint32(ld.Segdata.Filelen)) 999 ld.Thearch.Lput(uint32(ld.Segdata.Length - ld.Segdata.Filelen)) 1000 ld.Thearch.Lput(uint32(ld.Symsize)) /* nsyms */ 1001 ld.Thearch.Lput(uint32(ld.Entryvalue(ctxt))) /* va of entry */ 1002 ld.Thearch.Lput(0) 1003 ld.Thearch.Lput(uint32(ld.Lcsize)) 1004 1005 case objabi.Hlinux, 1006 objabi.Hfreebsd, 1007 objabi.Hnetbsd, 1008 objabi.Hopenbsd, 1009 objabi.Hnacl: 1010 ld.Asmbelf(ctxt, int64(symo)) 1011 } 1012 1013 ld.Cflush() 1014 if *ld.FlagC { 1015 fmt.Printf("textsize=%d\n", ld.Segtext.Filelen) 1016 fmt.Printf("datsize=%d\n", ld.Segdata.Filelen) 1017 fmt.Printf("bsssize=%d\n", ld.Segdata.Length-ld.Segdata.Filelen) 1018 fmt.Printf("symsize=%d\n", ld.Symsize) 1019 fmt.Printf("lcsize=%d\n", ld.Lcsize) 1020 fmt.Printf("total=%d\n", ld.Segtext.Filelen+ld.Segdata.Length+uint64(ld.Symsize)+uint64(ld.Lcsize)) 1021 } 1022 }