// Generate the glink resolver stub if necessary and return the .glink section func ensureglinkresolver() *ld.LSym { glink := ld.Linklookup(ld.Ctxt, ".glink", 0) if glink.Size != 0 { return glink } // This is essentially the resolver from the ppc64 ELF ABI. // At entry, r12 holds the address of the symbol resolver stub // for the target routine and the argument registers hold the // arguments for the target routine. // // This stub is PIC, so first get the PC of label 1 into r11. // Other things will be relative to this. ld.Adduint32(ld.Ctxt, glink, 0x7c0802a6) // mflr r0 ld.Adduint32(ld.Ctxt, glink, 0x429f0005) // bcl 20,31,1f ld.Adduint32(ld.Ctxt, glink, 0x7d6802a6) // 1: mflr r11 ld.Adduint32(ld.Ctxt, glink, 0x7c0803a6) // mtlf r0 // Compute the .plt array index from the entry point address. // Because this is PIC, everything is relative to label 1b (in // r11): // r0 = ((r12 - r11) - (res_0 - r11)) / 4 = (r12 - res_0) / 4 ld.Adduint32(ld.Ctxt, glink, 0x3800ffd0) // li r0,-(res_0-1b)=-48 ld.Adduint32(ld.Ctxt, glink, 0x7c006214) // add r0,r0,r12 ld.Adduint32(ld.Ctxt, glink, 0x7c0b0050) // sub r0,r0,r11 ld.Adduint32(ld.Ctxt, glink, 0x7800f082) // srdi r0,r0,2 // r11 = address of the first byte of the PLT r := ld.Addrel(glink) r.Off = int32(glink.Size) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Siz = 8 r.Type = ld.R_ADDRPOWER // addis r11,0,.plt@ha; addi r11,r11,.plt@l r.Add = 0x3d600000<<32 | 0x396b0000 glink.Size += 8 // Load r12 = dynamic resolver address and r11 = DSO // identifier from the first two doublewords of the PLT. ld.Adduint32(ld.Ctxt, glink, 0xe98b0000) // ld r12,0(r11) ld.Adduint32(ld.Ctxt, glink, 0xe96b0008) // ld r11,8(r11) // Jump to the dynamic resolver ld.Adduint32(ld.Ctxt, glink, 0x7d8903a6) // mtctr r12 ld.Adduint32(ld.Ctxt, glink, 0x4e800420) // bctr // The symbol resolvers must immediately follow. // res_0: // Add DT_PPC64_GLINK .dynamic entry, which points to 32 bytes // before the first symbol resolver stub. s := ld.Linklookup(ld.Ctxt, ".dynamic", 0) ld.Elfwritedynentsymplus(s, ld.DT_PPC64_GLINK, glink, glink.Size-32) return glink }
// Construct a call stub in stub that calls symbol targ via its PLT // entry. func gencallstub(abicase int, stub *ld.LSym, targ *ld.LSym) { if abicase != 1 { // If we see R_PPC64_TOCSAVE or R_PPC64_REL24_NOTOC // relocations, we'll need to implement cases 2 and 3. log.Fatalf("gencallstub only implements case 1 calls") } plt := ld.Linklookup(ld.Ctxt, ".plt", 0) stub.Type = ld.STEXT // Save TOC pointer in TOC save slot ld.Adduint32(ld.Ctxt, stub, 0xf8410018) // std r2,24(r1) // Load the function pointer from the PLT. r := ld.Addrel(stub) r.Off = int32(stub.Size) r.Sym = plt r.Add = int64(targ.Plt) r.Siz = 2 if ld.Ctxt.Arch.ByteOrder == binary.BigEndian { r.Off += int32(r.Siz) } r.Type = ld.R_POWER_TOC r.Variant = ld.RV_POWER_HA ld.Adduint32(ld.Ctxt, stub, 0x3d820000) // addis r12,r2,targ@plt@toc@ha r = ld.Addrel(stub) r.Off = int32(stub.Size) r.Sym = plt r.Add = int64(targ.Plt) r.Siz = 2 if ld.Ctxt.Arch.ByteOrder == binary.BigEndian { r.Off += int32(r.Siz) } r.Type = ld.R_POWER_TOC r.Variant = ld.RV_POWER_LO ld.Adduint32(ld.Ctxt, stub, 0xe98c0000) // ld r12,targ@plt@toc@l(r12) // Jump to the loaded pointer ld.Adduint32(ld.Ctxt, stub, 0x7d8903a6) // mtctr r12 ld.Adduint32(ld.Ctxt, stub, 0x4e800420) // bctr }
func Addcall(ctxt *ld.Link, s *ld.LSym, t *ld.LSym) int64 { s.Reachable = true i := s.Size s.Size += 4 ld.Symgrow(ctxt, s, s.Size) r := ld.Addrel(s) r.Sym = t r.Off = int32(i) r.Type = ld.R_CALL r.Siz = 4 return i + int64(r.Siz) }
func addpltreloc(ctxt *ld.Link, plt *ld.LSym, got *ld.LSym, sym *ld.LSym, typ int) *ld.Reloc { r := ld.Addrel(plt) r.Sym = got r.Off = int32(plt.Size) r.Siz = 4 r.Type = int32(typ) r.Add = int64(sym.Got) - 8 plt.Reachable = true plt.Size += 4 ld.Symgrow(ctxt, plt, plt.Size) return r }
func addpltsym(ctxt *ld.Link, s *ld.LSym) { if s.Plt >= 0 { return } adddynsym(ctxt, s) if ld.Iself { plt := ld.Linklookup(ctxt, ".plt", 0) rela := ld.Linklookup(ctxt, ".rela.plt", 0) if plt.Size == 0 { elfsetupplt() } // Create the glink resolver if necessary glink := ensureglinkresolver() // Write symbol resolver stub (just a branch to the // glink resolver stub) r := ld.Addrel(glink) r.Sym = glink r.Off = int32(glink.Size) r.Siz = 4 r.Type = ld.R_CALLPOWER ld.Adduint32(ctxt, glink, 0x48000000) // b .glink // In the ppc64 ABI, the dynamic linker is responsible // for writing the entire PLT. We just need to // reserve 8 bytes for each PLT entry and generate a // JMP_SLOT dynamic relocation for it. // // TODO(austin): ABI v1 is different s.Plt = int32(plt.Size) plt.Size += 8 ld.Addaddrplus(ctxt, rela, plt, int64(s.Plt)) ld.Adduint64(ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_PPC64_JMP_SLOT)) ld.Adduint64(ctxt, rela, 0) } else { ld.Diag("addpltsym: unsupported binary format") } }