func elfsetupplt() { plt := ld.Linklookup(ld.Ctxt, ".plt", 0) got := ld.Linklookup(ld.Ctxt, ".got.plt", 0) if plt.Size == 0 { // pushl got+4 ld.Adduint8(ld.Ctxt, plt, 0xff) ld.Adduint8(ld.Ctxt, plt, 0x35) ld.Addaddrplus(ld.Ctxt, plt, got, 4) // jmp *got+8 ld.Adduint8(ld.Ctxt, plt, 0xff) ld.Adduint8(ld.Ctxt, plt, 0x25) ld.Addaddrplus(ld.Ctxt, plt, got, 8) // zero pad ld.Adduint32(ld.Ctxt, plt, 0) // assume got->size == 0 too ld.Addaddrplus(ld.Ctxt, got, ld.Linklookup(ld.Ctxt, ".dynamic", 0), 0) ld.Adduint32(ld.Ctxt, got, 0) ld.Adduint32(ld.Ctxt, got, 0) } }
func addpltsym(ctxt *ld.Link, s *ld.LSym) { if s.Plt >= 0 { return } ld.Adddynsym(ctxt, s) if ld.Iself { plt := ld.Linklookup(ctxt, ".plt", 0) got := ld.Linklookup(ctxt, ".got.plt", 0) rel := ld.Linklookup(ctxt, ".rel.plt", 0) if plt.Size == 0 { elfsetupplt() } // jmpq *got+size ld.Adduint8(ctxt, plt, 0xff) ld.Adduint8(ctxt, plt, 0x25) ld.Addaddrplus(ctxt, plt, got, got.Size) // add to got: pointer to current pos in plt ld.Addaddrplus(ctxt, got, plt, plt.Size) // pushl $x ld.Adduint8(ctxt, plt, 0x68) ld.Adduint32(ctxt, plt, uint32(rel.Size)) // jmp .plt ld.Adduint8(ctxt, plt, 0xe9) ld.Adduint32(ctxt, plt, uint32(-(plt.Size + 4))) // rel ld.Addaddrplus(ctxt, rel, got, got.Size-4) ld.Adduint32(ctxt, rel, ld.ELF32_R_INFO(uint32(s.Dynid), ld.R_386_JMP_SLOT)) s.Plt = int32(plt.Size - 16) } else if ld.HEADTYPE == obj.Hdarwin { // Same laziness as in 6l. plt := ld.Linklookup(ctxt, ".plt", 0) addgotsym(ctxt, s) ld.Adduint32(ctxt, ld.Linklookup(ctxt, ".linkedit.plt", 0), uint32(s.Dynid)) // jmpq *got+size(IP) s.Plt = int32(plt.Size) ld.Adduint8(ctxt, plt, 0xff) ld.Adduint8(ctxt, plt, 0x25) ld.Addaddrplus(ctxt, plt, ld.Linklookup(ctxt, ".got", 0), int64(s.Got)) } else { ld.Diag("addpltsym: unsupported binary format") } }
// gentext generates assembly to append the local moduledata to the global // moduledata linked list at initialization time. This is only done if the runtime // is in a different module. // // <go.link.addmoduledata>: // larl %r2, <local.moduledata> // jg <runtime.addmoduledata@plt> // undef // // The job of appending the moduledata is delegated to runtime.addmoduledata. func gentext() { if !ld.DynlinkingGo() { return } addmoduledata := ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0) if addmoduledata.Type == obj.STEXT { // we're linking a module containing the runtime -> no need for // an init function return } addmoduledata.Attr |= ld.AttrReachable initfunc := ld.Linklookup(ld.Ctxt, "go.link.addmoduledata", 0) initfunc.Type = obj.STEXT initfunc.Attr |= ld.AttrLocal initfunc.Attr |= ld.AttrReachable // larl %r2, <local.moduledata> ld.Adduint8(ld.Ctxt, initfunc, 0xc0) ld.Adduint8(ld.Ctxt, initfunc, 0x20) lmd := ld.Addrel(initfunc) lmd.Off = int32(initfunc.Size) lmd.Siz = 4 lmd.Sym = ld.Ctxt.Moduledata lmd.Type = obj.R_PCREL lmd.Variant = ld.RV_390_DBL lmd.Add = 2 + int64(lmd.Siz) ld.Adduint32(ld.Ctxt, initfunc, 0) // jg <runtime.addmoduledata[@plt]> ld.Adduint8(ld.Ctxt, initfunc, 0xc0) ld.Adduint8(ld.Ctxt, initfunc, 0xf4) rel := ld.Addrel(initfunc) rel.Off = int32(initfunc.Size) rel.Siz = 4 rel.Sym = ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0) rel.Type = obj.R_CALL rel.Variant = ld.RV_390_DBL rel.Add = 2 + int64(rel.Siz) ld.Adduint32(ld.Ctxt, initfunc, 0) // undef (for debugging) ld.Adduint32(ld.Ctxt, initfunc, 0) ld.Ctxt.Textp = append(ld.Ctxt.Textp, initfunc) initarray_entry := ld.Linklookup(ld.Ctxt, "go.link.addmoduledatainit", 0) initarray_entry.Attr |= ld.AttrLocal initarray_entry.Attr |= ld.AttrReachable initarray_entry.Type = obj.SINITARR ld.Addaddr(ld.Ctxt, initarray_entry, initfunc) }
func elfsetupplt() { plt := ld.Linklookup(ld.Ctxt, ".plt", 0) got := ld.Linklookup(ld.Ctxt, ".got.plt", 0) if plt.Size == 0 { // pushq got+8(IP) ld.Adduint8(ld.Ctxt, plt, 0xff) ld.Adduint8(ld.Ctxt, plt, 0x35) ld.Addpcrelplus(ld.Ctxt, plt, got, 8) // jmpq got+16(IP) ld.Adduint8(ld.Ctxt, plt, 0xff) ld.Adduint8(ld.Ctxt, plt, 0x25) ld.Addpcrelplus(ld.Ctxt, plt, got, 16) // nopl 0(AX) ld.Adduint32(ld.Ctxt, plt, 0x00401f0f) // assume got->size == 0 too ld.Addaddrplus(ld.Ctxt, got, ld.Linklookup(ld.Ctxt, ".dynamic", 0), 0) ld.Adduint64(ld.Ctxt, got, 0) ld.Adduint64(ld.Ctxt, got, 0) } }
func genaddmoduledata() { addmoduledata := ld.Linkrlookup(ld.Ctxt, "runtime.addmoduledata", 0) if addmoduledata.Type == obj.STEXT { return } addmoduledata.Attr |= ld.AttrReachable initfunc := ld.Linklookup(ld.Ctxt, "go.link.addmoduledata", 0) initfunc.Type = obj.STEXT initfunc.Attr |= ld.AttrLocal initfunc.Attr |= ld.AttrReachable o := func(op uint32) { ld.Adduint32(ld.Ctxt, initfunc, op) } // addis r2, r12, .TOC.-func@ha rel := ld.Addrel(initfunc) rel.Off = int32(initfunc.Size) rel.Siz = 8 rel.Sym = ld.Linklookup(ld.Ctxt, ".TOC.", 0) rel.Type = obj.R_ADDRPOWER_PCREL o(0x3c4c0000) // addi r2, r2, .TOC.-func@l o(0x38420000) // mflr r31 o(0x7c0802a6) // stdu r31, -32(r1) o(0xf801ffe1) // addis r3, r2, local.moduledata@got@ha rel = ld.Addrel(initfunc) rel.Off = int32(initfunc.Size) rel.Siz = 8 rel.Sym = ld.Linklookup(ld.Ctxt, "local.moduledata", 0) rel.Type = obj.R_ADDRPOWER_GOT o(0x3c620000) // ld r3, local.moduledata@got@l(r3) o(0xe8630000) // bl runtime.addmoduledata rel = ld.Addrel(initfunc) rel.Off = int32(initfunc.Size) rel.Siz = 4 rel.Sym = addmoduledata rel.Type = obj.R_CALLPOWER o(0x48000001) // nop o(0x60000000) // ld r31, 0(r1) o(0xe8010000) // mtlr r31 o(0x7c0803a6) // addi r1,r1,32 o(0x38210020) // blr o(0x4e800020) ld.Ctxt.Textp = append(ld.Ctxt.Textp, initfunc) initarray_entry := ld.Linklookup(ld.Ctxt, "go.link.addmoduledatainit", 0) initarray_entry.Attr |= ld.AttrReachable initarray_entry.Attr |= ld.AttrLocal initarray_entry.Type = obj.SINITARR ld.Addaddr(ld.Ctxt, initarray_entry, initfunc) }
func elfsetupplt() { plt := ld.Linklookup(ld.Ctxt, ".plt", 0) got := ld.Linklookup(ld.Ctxt, ".got.plt", 0) if plt.Size == 0 { // str lr, [sp, #-4]! ld.Adduint32(ld.Ctxt, plt, 0xe52de004) // ldr lr, [pc, #4] ld.Adduint32(ld.Ctxt, plt, 0xe59fe004) // add lr, pc, lr ld.Adduint32(ld.Ctxt, plt, 0xe08fe00e) // ldr pc, [lr, #8]! ld.Adduint32(ld.Ctxt, plt, 0xe5bef008) // .word &GLOBAL_OFFSET_TABLE[0] - . ld.Addpcrelplus(ld.Ctxt, plt, got, 4) // the first .plt entry requires 3 .plt.got entries ld.Adduint32(ld.Ctxt, got, 0) ld.Adduint32(ld.Ctxt, got, 0) ld.Adduint32(ld.Ctxt, got, 0) } }
func addgotsym(ctxt *ld.Link, s *ld.LSym) { if s.Got >= 0 { return } ld.Adddynsym(ctxt, s) got := ld.Linklookup(ctxt, ".got", 0) s.Got = int32(got.Size) ld.Adduint32(ctxt, got, 0) if ld.Iself { rel := ld.Linklookup(ctxt, ".rel", 0) ld.Addaddrplus(ctxt, rel, got, int64(s.Got)) ld.Adduint32(ctxt, rel, ld.ELF32_R_INFO(uint32(s.Dynid), ld.R_ARM_GLOB_DAT)) } else { ld.Diag("addgotsym: unsupported binary format") } }
// 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 = obj.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 = obj.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 = obj.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 gentext() { if !ld.DynlinkingGo() { return } addmoduledata := ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0) if addmoduledata.Type == obj.STEXT { // we're linking a module containing the runtime -> no need for // an init function return } addmoduledata.Attr |= ld.AttrReachable initfunc := ld.Linklookup(ld.Ctxt, "go.link.addmoduledata", 0) initfunc.Type = obj.STEXT initfunc.Attr |= ld.AttrLocal initfunc.Attr |= ld.AttrReachable o := func(op uint32) { ld.Adduint32(ld.Ctxt, initfunc, op) } // 0000000000000000 <local.dso_init>: // 0: 90000000 adrp x0, 0 <runtime.firstmoduledata> // 0: R_AARCH64_ADR_PREL_PG_HI21 local.moduledata // 4: 91000000 add x0, x0, #0x0 // 4: R_AARCH64_ADD_ABS_LO12_NC local.moduledata o(0x90000000) o(0x91000000) rel := ld.Addrel(initfunc) rel.Off = 0 rel.Siz = 8 rel.Sym = ld.Ctxt.Moduledata rel.Type = obj.R_ADDRARM64 // 8: 14000000 bl 0 <runtime.addmoduledata> // 8: R_AARCH64_CALL26 runtime.addmoduledata o(0x14000000) rel = ld.Addrel(initfunc) rel.Off = 8 rel.Siz = 4 rel.Sym = ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0) rel.Type = obj.R_CALLARM64 // Really should be R_AARCH64_JUMP26 but doesn't seem to make any difference ld.Ctxt.Textp = append(ld.Ctxt.Textp, initfunc) initarray_entry := ld.Linklookup(ld.Ctxt, "go.link.addmoduledatainit", 0) initarray_entry.Attr |= ld.AttrReachable initarray_entry.Attr |= ld.AttrLocal initarray_entry.Type = obj.SINITARR ld.Addaddr(ld.Ctxt, initarray_entry, initfunc) }
func gentext() { if !ld.DynlinkingGo() { return } addmoduledata := ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0) if addmoduledata.Type == obj.STEXT { // we're linking a module containing the runtime -> no need for // an init function return } addmoduledata.Attr |= ld.AttrReachable initfunc := ld.Linklookup(ld.Ctxt, "go.link.addmoduledata", 0) initfunc.Type = obj.STEXT initfunc.Attr |= ld.AttrLocal initfunc.Attr |= ld.AttrReachable o := func(op uint32) { ld.Adduint32(ld.Ctxt, initfunc, op) } o(0xe59f0004) o(0xe08f0000) o(0xeafffffe) rel := ld.Addrel(initfunc) rel.Off = 8 rel.Siz = 4 rel.Sym = ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0) rel.Type = obj.R_CALLARM rel.Add = 0xeafffffe // vomit o(0x00000000) rel = ld.Addrel(initfunc) rel.Off = 12 rel.Siz = 4 rel.Sym = ld.Ctxt.Moduledata rel.Type = obj.R_PCREL rel.Add = 4 ld.Ctxt.Textp = append(ld.Ctxt.Textp, initfunc) initarray_entry := ld.Linklookup(ld.Ctxt, "go.link.addmoduledatainit", 0) initarray_entry.Attr |= ld.AttrReachable initarray_entry.Attr |= ld.AttrLocal initarray_entry.Type = obj.SINITARR ld.Addaddr(ld.Ctxt, initarray_entry, initfunc) }
func addpltsym(ctxt *ld.Link, s *ld.LSym) { if s.Plt >= 0 { return } ld.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 = obj.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") } }
func addgotsym(s *ld.LSym) { if s.Got >= 0 { return } ld.Adddynsym(ld.Ctxt, s) got := ld.Linklookup(ld.Ctxt, ".got", 0) s.Got = int32(got.Size) ld.Adduint64(ld.Ctxt, got, 0) if ld.Iself { rela := ld.Linklookup(ld.Ctxt, ".rela", 0) ld.Addaddrplus(ld.Ctxt, rela, got, int64(s.Got)) ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_X86_64_GLOB_DAT)) ld.Adduint64(ld.Ctxt, rela, 0) } else if ld.HEADTYPE == obj.Hdarwin { ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.got", 0), uint32(s.Dynid)) } else { ld.Diag("addgotsym: unsupported binary format") } }
func addpltsym(ctxt *ld.Link, s *ld.LSym) { if s.Plt >= 0 { return } ld.Adddynsym(ctxt, s) if ld.Iself { plt := ld.Linklookup(ctxt, ".plt", 0) got := ld.Linklookup(ctxt, ".got.plt", 0) rel := ld.Linklookup(ctxt, ".rel.plt", 0) if plt.Size == 0 { elfsetupplt() } // .got entry s.Got = int32(got.Size) // In theory, all GOT should point to the first PLT entry, // Linux/ARM's dynamic linker will do that for us, but FreeBSD/ARM's // dynamic linker won't, so we'd better do it ourselves. ld.Addaddrplus(ctxt, got, plt, 0) // .plt entry, this depends on the .got entry s.Plt = int32(plt.Size) addpltreloc(ctxt, plt, got, s, obj.R_PLT0) // add lr, pc, #0xXX00000 addpltreloc(ctxt, plt, got, s, obj.R_PLT1) // add lr, lr, #0xYY000 addpltreloc(ctxt, plt, got, s, obj.R_PLT2) // ldr pc, [lr, #0xZZZ]! // rel ld.Addaddrplus(ctxt, rel, got, int64(s.Got)) ld.Adduint32(ctxt, rel, ld.ELF32_R_INFO(uint32(s.Dynid), ld.R_ARM_JUMP_SLOT)) } else { ld.Diag("addpltsym: unsupported binary format") } }
func adddynrel(s *ld.LSym, r *ld.Reloc) { targ := r.Sym ld.Ctxt.Cursym = s switch r.Type { default: if r.Type >= 256 { ld.Diag("unexpected relocation type %d", r.Type) return } // Handle relocations found in ELF object files. case 256 + ld.R_X86_64_PC32: if targ.Type == obj.SDYNIMPORT { ld.Diag("unexpected R_X86_64_PC32 relocation for dynamic symbol %s", targ.Name) } if targ.Type == 0 || targ.Type == obj.SXREF { ld.Diag("unknown symbol %s in pcrel", targ.Name) } r.Type = obj.R_PCREL r.Add += 4 return case 256 + ld.R_X86_64_PLT32: r.Type = obj.R_PCREL r.Add += 4 if targ.Type == obj.SDYNIMPORT { addpltsym(targ) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Add += int64(targ.Plt) } return case 256 + ld.R_X86_64_GOTPCREL, 256 + ld.R_X86_64_GOTPCRELX, 256 + ld.R_X86_64_REX_GOTPCRELX: if targ.Type != obj.SDYNIMPORT { // have symbol if r.Off >= 2 && s.P[r.Off-2] == 0x8b { // turn MOVQ of GOT entry into LEAQ of symbol itself s.P[r.Off-2] = 0x8d r.Type = obj.R_PCREL r.Add += 4 return } } // fall back to using GOT and hope for the best (CMOV*) // TODO: just needs relocation, no need to put in .dynsym addgotsym(targ) r.Type = obj.R_PCREL r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) r.Add += 4 r.Add += int64(targ.Got) return case 256 + ld.R_X86_64_64: if targ.Type == obj.SDYNIMPORT { ld.Diag("unexpected R_X86_64_64 relocation for dynamic symbol %s", targ.Name) } r.Type = obj.R_ADDR return // Handle relocations found in Mach-O object files. case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 0, 512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 0, 512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 0: // TODO: What is the difference between all these? r.Type = obj.R_ADDR if targ.Type == obj.SDYNIMPORT { ld.Diag("unexpected reloc for dynamic symbol %s", targ.Name) } return case 512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 1: if targ.Type == obj.SDYNIMPORT { addpltsym(targ) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Add = int64(targ.Plt) r.Type = obj.R_PCREL return } fallthrough // fall through case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 1, 512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 1, 512 + ld.MACHO_X86_64_RELOC_SIGNED_1*2 + 1, 512 + ld.MACHO_X86_64_RELOC_SIGNED_2*2 + 1, 512 + ld.MACHO_X86_64_RELOC_SIGNED_4*2 + 1: r.Type = obj.R_PCREL if targ.Type == obj.SDYNIMPORT { ld.Diag("unexpected pc-relative reloc for dynamic symbol %s", targ.Name) } return case 512 + ld.MACHO_X86_64_RELOC_GOT_LOAD*2 + 1: if targ.Type != obj.SDYNIMPORT { // have symbol // turn MOVQ of GOT entry into LEAQ of symbol itself if r.Off < 2 || s.P[r.Off-2] != 0x8b { ld.Diag("unexpected GOT_LOAD reloc for non-dynamic symbol %s", targ.Name) return } s.P[r.Off-2] = 0x8d r.Type = obj.R_PCREL return } fallthrough // fall through case 512 + ld.MACHO_X86_64_RELOC_GOT*2 + 1: if targ.Type != obj.SDYNIMPORT { ld.Diag("unexpected GOT reloc for non-dynamic symbol %s", targ.Name) } addgotsym(targ) r.Type = obj.R_PCREL r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) r.Add += int64(targ.Got) return } // Handle references to ELF symbols from our own object files. if targ.Type != obj.SDYNIMPORT { return } switch r.Type { case obj.R_CALL, obj.R_PCREL: if ld.HEADTYPE == obj.Hwindows { // nothing to do, the relocation will be laid out in pereloc1 return } else { // for both ELF and Mach-O addpltsym(targ) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Add = int64(targ.Plt) return } case obj.R_ADDR: if s.Type == obj.STEXT && ld.Iself { if ld.HEADTYPE == obj.Hsolaris { addpltsym(targ) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Add += int64(targ.Plt) return } // The code is asking for the address of an external // function. We provide it with the address of the // correspondent GOT symbol. addgotsym(targ) r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) r.Add += int64(targ.Got) return } if s.Type != obj.SDATA { break } if ld.Iself { ld.Adddynsym(ld.Ctxt, targ) rela := ld.Linklookup(ld.Ctxt, ".rela", 0) ld.Addaddrplus(ld.Ctxt, rela, s, int64(r.Off)) if r.Siz == 8 { ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_X86_64_64)) } else { ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_X86_64_32)) } ld.Adduint64(ld.Ctxt, rela, uint64(r.Add)) r.Type = 256 // ignore during relocsym return } if ld.HEADTYPE == obj.Hdarwin && s.Size == int64(ld.SysArch.PtrSize) && r.Off == 0 { // Mach-O relocations are a royal pain to lay out. // They use a compact stateful bytecode representation // that is too much bother to deal with. // Instead, interpret the C declaration // void *_Cvar_stderr = &stderr; // as making _Cvar_stderr the name of a GOT entry // for stderr. This is separate from the usual GOT entry, // just in case the C code assigns to the variable, // and of course it only works for single pointers, // but we only need to support cgo and that's all it needs. ld.Adddynsym(ld.Ctxt, targ) got := ld.Linklookup(ld.Ctxt, ".got", 0) s.Type = got.Type | obj.SSUB s.Outer = got s.Sub = got.Sub got.Sub = s s.Value = got.Size ld.Adduint64(ld.Ctxt, got, 0) ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.got", 0), uint32(targ.Dynid)) r.Type = 256 // ignore during relocsym return } if ld.HEADTYPE == obj.Hwindows { // nothing to do, the relocation will be laid out in pereloc1 return } } ld.Ctxt.Cursym = s ld.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type) }
func addpltsym(s *ld.LSym) { if s.Plt >= 0 { return } ld.Adddynsym(ld.Ctxt, s) if ld.Iself { plt := ld.Linklookup(ld.Ctxt, ".plt", 0) got := ld.Linklookup(ld.Ctxt, ".got.plt", 0) rela := ld.Linklookup(ld.Ctxt, ".rela.plt", 0) if plt.Size == 0 { elfsetupplt() } // jmpq *got+size(IP) ld.Adduint8(ld.Ctxt, plt, 0xff) ld.Adduint8(ld.Ctxt, plt, 0x25) ld.Addpcrelplus(ld.Ctxt, plt, got, got.Size) // add to got: pointer to current pos in plt ld.Addaddrplus(ld.Ctxt, got, plt, plt.Size) // pushq $x ld.Adduint8(ld.Ctxt, plt, 0x68) ld.Adduint32(ld.Ctxt, plt, uint32((got.Size-24-8)/8)) // jmpq .plt ld.Adduint8(ld.Ctxt, plt, 0xe9) ld.Adduint32(ld.Ctxt, plt, uint32(-(plt.Size + 4))) // rela ld.Addaddrplus(ld.Ctxt, rela, got, got.Size-8) ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_X86_64_JMP_SLOT)) ld.Adduint64(ld.Ctxt, rela, 0) s.Plt = int32(plt.Size - 16) } else if ld.HEADTYPE == obj.Hdarwin { // To do lazy symbol lookup right, we're supposed // to tell the dynamic loader which library each // symbol comes from and format the link info // section just so. I'm too lazy (ha!) to do that // so for now we'll just use non-lazy pointers, // which don't need to be told which library to use. // // http://networkpx.blogspot.com/2009/09/about-lcdyldinfoonly-command.html // has details about what we're avoiding. addgotsym(s) plt := ld.Linklookup(ld.Ctxt, ".plt", 0) ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.plt", 0), uint32(s.Dynid)) // jmpq *got+size(IP) s.Plt = int32(plt.Size) ld.Adduint8(ld.Ctxt, plt, 0xff) ld.Adduint8(ld.Ctxt, plt, 0x25) ld.Addpcrelplus(ld.Ctxt, plt, ld.Linklookup(ld.Ctxt, ".got", 0), int64(s.Got)) } else { ld.Diag("addpltsym: unsupported binary format") } }
func addpltsym(ctxt *ld.Link, s *ld.LSym) { if s.Plt >= 0 { return } ld.Adddynsym(ctxt, s) if ld.Iself { plt := ld.Linklookup(ctxt, ".plt", 0) got := ld.Linklookup(ctxt, ".got", 0) rela := ld.Linklookup(ctxt, ".rela.plt", 0) if plt.Size == 0 { elfsetupplt() } // larl %r1,_GLOBAL_OFFSET_TABLE_+index ld.Adduint8(ctxt, plt, 0xc0) ld.Adduint8(ctxt, plt, 0x10) ld.Addpcrelplus(ctxt, plt, got, got.Size+6) // need variant? // add to got: pointer to current pos in plt ld.Addaddrplus(ctxt, got, plt, plt.Size+8) // weird but correct // lg %r1,0(%r1) ld.Adduint8(ctxt, plt, 0xe3) ld.Adduint8(ctxt, plt, 0x10) ld.Adduint8(ctxt, plt, 0x10) ld.Adduint8(ctxt, plt, 0x00) ld.Adduint8(ctxt, plt, 0x00) ld.Adduint8(ctxt, plt, 0x04) // br %r1 ld.Adduint8(ctxt, plt, 0x07) ld.Adduint8(ctxt, plt, 0xf1) // basr %r1,%r0 ld.Adduint8(ctxt, plt, 0x0d) ld.Adduint8(ctxt, plt, 0x10) // lgf %r1,12(%r1) ld.Adduint8(ctxt, plt, 0xe3) ld.Adduint8(ctxt, plt, 0x10) ld.Adduint8(ctxt, plt, 0x10) ld.Adduint8(ctxt, plt, 0x0c) ld.Adduint8(ctxt, plt, 0x00) ld.Adduint8(ctxt, plt, 0x14) // jg .plt ld.Adduint8(ctxt, plt, 0xc0) ld.Adduint8(ctxt, plt, 0xf4) ld.Adduint32(ctxt, plt, uint32(-((plt.Size - 2) >> 1))) // roll-your-own relocation //.plt index ld.Adduint32(ctxt, plt, uint32(rela.Size)) // rela size before current entry // rela ld.Addaddrplus(ctxt, rela, got, got.Size-8) ld.Adduint64(ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_390_JMP_SLOT)) ld.Adduint64(ctxt, rela, 0) s.Plt = int32(plt.Size - 32) } else { ld.Diag("addpltsym: unsupported binary format") } }
func adddynrel(s *ld.LSym, r *ld.Reloc) { targ := r.Sym ld.Ctxt.Cursym = s switch r.Type { default: if r.Type >= 256 { ld.Diag("unexpected relocation type %d", r.Type) return } // Handle relocations found in ELF object files. case 256 + ld.R_386_PC32: if targ.Type == obj.SDYNIMPORT { ld.Diag("unexpected R_386_PC32 relocation for dynamic symbol %s", targ.Name) } if targ.Type == 0 || targ.Type == obj.SXREF { ld.Diag("unknown symbol %s in pcrel", targ.Name) } r.Type = obj.R_PCREL r.Add += 4 return case 256 + ld.R_386_PLT32: r.Type = obj.R_PCREL r.Add += 4 if targ.Type == obj.SDYNIMPORT { addpltsym(ld.Ctxt, targ) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Add += int64(targ.Plt) } return case 256 + ld.R_386_GOT32, 256 + ld.R_386_GOT32X: if targ.Type != obj.SDYNIMPORT { // have symbol if r.Off >= 2 && s.P[r.Off-2] == 0x8b { // turn MOVL of GOT entry into LEAL of symbol address, relative to GOT. s.P[r.Off-2] = 0x8d r.Type = obj.R_GOTOFF return } if r.Off >= 2 && s.P[r.Off-2] == 0xff && s.P[r.Off-1] == 0xb3 { // turn PUSHL of GOT entry into PUSHL of symbol itself. // use unnecessary SS prefix to keep instruction same length. s.P[r.Off-2] = 0x36 s.P[r.Off-1] = 0x68 r.Type = obj.R_ADDR return } ld.Diag("unexpected GOT reloc for non-dynamic symbol %s", targ.Name) return } addgotsym(ld.Ctxt, targ) r.Type = obj.R_CONST // write r->add during relocsym r.Sym = nil r.Add += int64(targ.Got) return case 256 + ld.R_386_GOTOFF: r.Type = obj.R_GOTOFF return case 256 + ld.R_386_GOTPC: r.Type = obj.R_PCREL r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) r.Add += 4 return case 256 + ld.R_386_32: if targ.Type == obj.SDYNIMPORT { ld.Diag("unexpected R_386_32 relocation for dynamic symbol %s", targ.Name) } r.Type = obj.R_ADDR return case 512 + ld.MACHO_GENERIC_RELOC_VANILLA*2 + 0: r.Type = obj.R_ADDR if targ.Type == obj.SDYNIMPORT { ld.Diag("unexpected reloc for dynamic symbol %s", targ.Name) } return case 512 + ld.MACHO_GENERIC_RELOC_VANILLA*2 + 1: if targ.Type == obj.SDYNIMPORT { addpltsym(ld.Ctxt, targ) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Add = int64(targ.Plt) r.Type = obj.R_PCREL return } r.Type = obj.R_PCREL return case 512 + ld.MACHO_FAKE_GOTPCREL: if targ.Type != obj.SDYNIMPORT { // have symbol // turn MOVL of GOT entry into LEAL of symbol itself if r.Off < 2 || s.P[r.Off-2] != 0x8b { ld.Diag("unexpected GOT reloc for non-dynamic symbol %s", targ.Name) return } s.P[r.Off-2] = 0x8d r.Type = obj.R_PCREL return } addgotsym(ld.Ctxt, targ) r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) r.Add += int64(targ.Got) r.Type = obj.R_PCREL return } // Handle references to ELF symbols from our own object files. if targ.Type != obj.SDYNIMPORT { return } switch r.Type { case obj.R_CALL, obj.R_PCREL: addpltsym(ld.Ctxt, targ) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Add = int64(targ.Plt) return case obj.R_ADDR: if s.Type != obj.SDATA { break } if ld.Iself { ld.Adddynsym(ld.Ctxt, targ) rel := ld.Linklookup(ld.Ctxt, ".rel", 0) ld.Addaddrplus(ld.Ctxt, rel, s, int64(r.Off)) ld.Adduint32(ld.Ctxt, rel, ld.ELF32_R_INFO(uint32(targ.Dynid), ld.R_386_32)) r.Type = obj.R_CONST // write r->add during relocsym r.Sym = nil return } if ld.HEADTYPE == obj.Hdarwin && s.Size == int64(ld.SysArch.PtrSize) && r.Off == 0 { // Mach-O relocations are a royal pain to lay out. // They use a compact stateful bytecode representation // that is too much bother to deal with. // Instead, interpret the C declaration // void *_Cvar_stderr = &stderr; // as making _Cvar_stderr the name of a GOT entry // for stderr. This is separate from the usual GOT entry, // just in case the C code assigns to the variable, // and of course it only works for single pointers, // but we only need to support cgo and that's all it needs. ld.Adddynsym(ld.Ctxt, targ) got := ld.Linklookup(ld.Ctxt, ".got", 0) s.Type = got.Type | obj.SSUB s.Outer = got s.Sub = got.Sub got.Sub = s s.Value = got.Size ld.Adduint32(ld.Ctxt, got, 0) ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.got", 0), uint32(targ.Dynid)) r.Type = 256 // ignore during relocsym return } if ld.HEADTYPE == obj.Hwindows && s.Size == int64(ld.SysArch.PtrSize) { // nothing to do, the relocation will be laid out in pereloc1 return } } ld.Ctxt.Cursym = s ld.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type) }
// 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 = obj.R_ADDRPOWER ld.Adduint32(ld.Ctxt, glink, 0x3d600000) // addis r11,0,.plt@ha ld.Adduint32(ld.Ctxt, glink, 0x396b0000) // addi r11,r11,.plt@l // 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 }
func adddynrel(s *ld.LSym, r *ld.Reloc) { targ := r.Sym ld.Ctxt.Cursym = s switch r.Type { default: if r.Type >= 256 { ld.Diag("unexpected relocation type %d", r.Type) return } // Handle relocations found in ELF object files. case 256 + ld.R_ARM_PLT32: r.Type = obj.R_CALLARM if targ.Type == obj.SDYNIMPORT { addpltsym(ld.Ctxt, targ) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Add = int64(braddoff(int32(r.Add), targ.Plt/4)) } return case 256 + ld.R_ARM_THM_PC22: // R_ARM_THM_CALL ld.Exitf("R_ARM_THM_CALL, are you using -marm?") return case 256 + ld.R_ARM_GOT32: // R_ARM_GOT_BREL if targ.Type != obj.SDYNIMPORT { addgotsyminternal(ld.Ctxt, targ) } else { addgotsym(ld.Ctxt, targ) } r.Type = obj.R_CONST // write r->add during relocsym r.Sym = nil r.Add += int64(targ.Got) return case 256 + ld.R_ARM_GOT_PREL: // GOT(nil) + A - nil if targ.Type != obj.SDYNIMPORT { addgotsyminternal(ld.Ctxt, targ) } else { addgotsym(ld.Ctxt, targ) } r.Type = obj.R_PCREL r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) r.Add += int64(targ.Got) + 4 return case 256 + ld.R_ARM_GOTOFF: // R_ARM_GOTOFF32 r.Type = obj.R_GOTOFF return case 256 + ld.R_ARM_GOTPC: // R_ARM_BASE_PREL r.Type = obj.R_PCREL r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) r.Add += 4 return case 256 + ld.R_ARM_CALL: r.Type = obj.R_CALLARM if targ.Type == obj.SDYNIMPORT { addpltsym(ld.Ctxt, targ) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Add = int64(braddoff(int32(r.Add), targ.Plt/4)) } return case 256 + ld.R_ARM_REL32: // R_ARM_REL32 r.Type = obj.R_PCREL r.Add += 4 return case 256 + ld.R_ARM_ABS32: if targ.Type == obj.SDYNIMPORT { ld.Diag("unexpected R_ARM_ABS32 relocation for dynamic symbol %s", targ.Name) } r.Type = obj.R_ADDR return // we can just ignore this, because we are targeting ARM V5+ anyway case 256 + ld.R_ARM_V4BX: if r.Sym != nil { // R_ARM_V4BX is ABS relocation, so this symbol is a dummy symbol, ignore it r.Sym.Type = 0 } r.Sym = nil return case 256 + ld.R_ARM_PC24, 256 + ld.R_ARM_JUMP24: r.Type = obj.R_CALLARM if targ.Type == obj.SDYNIMPORT { addpltsym(ld.Ctxt, targ) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Add = int64(braddoff(int32(r.Add), targ.Plt/4)) } return } // Handle references to ELF symbols from our own object files. if targ.Type != obj.SDYNIMPORT { return } switch r.Type { case obj.R_CALLARM: addpltsym(ld.Ctxt, targ) r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) r.Add = int64(targ.Plt) return case obj.R_ADDR: if s.Type != obj.SDATA { break } if ld.Iself { ld.Adddynsym(ld.Ctxt, targ) rel := ld.Linklookup(ld.Ctxt, ".rel", 0) ld.Addaddrplus(ld.Ctxt, rel, s, int64(r.Off)) ld.Adduint32(ld.Ctxt, rel, ld.ELF32_R_INFO(uint32(targ.Dynid), ld.R_ARM_GLOB_DAT)) // we need a nil + A dynamic reloc r.Type = obj.R_CONST // write r->add during relocsym r.Sym = nil return } } ld.Ctxt.Cursym = s ld.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type) }