// Append 4 bytes to s and create a R_CALL relocation targeting t to fill them in. func addcall(ctxt *ld.Link, s *ld.LSym, t *ld.LSym) { 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 = obj.R_CALL r.Siz = 4 }
// Append 4 bytes to s and create a R_CALL relocation targeting t to fill them in. func addcall(ctxt *ld.Link, s *ld.Symbol, t *ld.Symbol) { s.Attr |= ld.AttrReachable i := s.Size s.Size += 4 ld.Symgrow(s, s.Size) r := ld.Addrel(s) r.Sym = t r.Off = int32(i) r.Type = obj.R_CALL r.Siz = 4 }
func Addcall(ctxt *ld.Link, s *ld.LSym, t *ld.LSym) int64 { s.Attr |= ld.AttrReachable 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 = obj.R_CALL r.Siz = 4 return i + int64(r.Siz) }
func addpltreloc(ctxt *ld.Link, plt *ld.Symbol, got *ld.Symbol, sym *ld.Symbol, 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.Attr |= ld.AttrReachable plt.Size += 4 ld.Symgrow(ctxt, plt, plt.Size) return r }
func gentext() { if !ld.DynlinkingGo() && ld.Buildmode != ld.BuildmodePIE && ld.Buildmode != ld.BuildmodeCShared { return } thunkfunc := ld.Linklookup(ld.Ctxt, "__x86.get_pc_thunk.cx", 0) thunkfunc.Type = obj.STEXT thunkfunc.Local = true thunkfunc.Reachable = true o := func(op ...uint8) { for _, op1 := range op { ld.Adduint8(ld.Ctxt, thunkfunc, op1) } } // 8b 0c 24 mov (%esp),%ecx o(0x8b, 0x0c, 0x24) // c3 ret o(0xc3) if ld.Ctxt.Etextp != nil { ld.Ctxt.Etextp.Next = thunkfunc } else { ld.Ctxt.Textp = thunkfunc } ld.Ctxt.Etextp = thunkfunc 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.Reachable = true initfunc := ld.Linklookup(ld.Ctxt, "go.link.addmoduledata", 0) initfunc.Type = obj.STEXT initfunc.Local = true initfunc.Reachable = true o = func(op ...uint8) { for _, op1 := range op { ld.Adduint8(ld.Ctxt, initfunc, op1) } } // go.link.addmoduledata: // 53 push %ebx // e8 00 00 00 00 call __x86.get_pc_thunk.cx + R_CALL __x86.get_pc_thunk.cx // 8d 81 00 00 00 00 lea 0x0(%ecx), %eax + R_PCREL ld.Ctxt.Moduledata // 8d 99 00 00 00 00 lea 0x0(%ecx), %ebx + R_GOTPC _GLOBAL_OFFSET_TABLE_ // e8 00 00 00 00 call runtime.addmoduledata@plt + R_CALL runtime.addmoduledata // 5b pop %ebx // c3 ret o(0x53) o(0xe8) addcall(ld.Ctxt, initfunc, ld.Linklookup(ld.Ctxt, "__x86.get_pc_thunk.cx", 0)) o(0x8d, 0x81) ld.Addpcrelplus(ld.Ctxt, initfunc, ld.Ctxt.Moduledata, 6) o(0x8d, 0x99) i := initfunc.Size initfunc.Size += 4 ld.Symgrow(ld.Ctxt, initfunc, initfunc.Size) r := ld.Addrel(initfunc) r.Sym = ld.Linklookup(ld.Ctxt, "_GLOBAL_OFFSET_TABLE_", 0) r.Off = int32(i) r.Type = obj.R_PCREL r.Add = 12 r.Siz = 4 o(0xe8) addcall(ld.Ctxt, initfunc, addmoduledata) o(0x5b) o(0xc3) ld.Ctxt.Etextp.Next = initfunc ld.Ctxt.Etextp = initfunc initarray_entry := ld.Linklookup(ld.Ctxt, "go.link.addmoduledatainit", 0) initarray_entry.Reachable = true initarray_entry.Local = true initarray_entry.Type = obj.SINITARR ld.Addaddr(ld.Ctxt, initarray_entry, initfunc) }
func gentext(ctxt *ld.Link) { if !ld.DynlinkingGo() && ld.Buildmode != ld.BuildmodePIE && ld.Buildmode != ld.BuildmodeCShared { return } // Generate little thunks that load the PC of the next instruction into a register. for _, r := range [...]struct { name string num uint8 }{ {"ax", 0}, {"cx", 1}, {"dx", 2}, {"bx", 3}, // sp {"bp", 5}, {"si", 6}, {"di", 7}, } { thunkfunc := ld.Linklookup(ctxt, "__x86.get_pc_thunk."+r.name, 0) thunkfunc.Type = obj.STEXT thunkfunc.Attr |= ld.AttrLocal thunkfunc.Attr |= ld.AttrReachable //TODO: remove? o := func(op ...uint8) { for _, op1 := range op { ld.Adduint8(ctxt, thunkfunc, op1) } } // 8b 04 24 mov (%esp),%eax // Destination register is in bits 3-5 of the middle byte, so add that in. o(0x8b, 0x04+r.num<<3, 0x24) // c3 ret o(0xc3) ctxt.Textp = append(ctxt.Textp, thunkfunc) } addmoduledata := ld.Linklookup(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(ctxt, "go.link.addmoduledata", 0) initfunc.Type = obj.STEXT initfunc.Attr |= ld.AttrLocal initfunc.Attr |= ld.AttrReachable o := func(op ...uint8) { for _, op1 := range op { ld.Adduint8(ctxt, initfunc, op1) } } // go.link.addmoduledata: // 53 push %ebx // e8 00 00 00 00 call __x86.get_pc_thunk.cx + R_CALL __x86.get_pc_thunk.cx // 8d 81 00 00 00 00 lea 0x0(%ecx), %eax + R_PCREL ctxt.Moduledata // 8d 99 00 00 00 00 lea 0x0(%ecx), %ebx + R_GOTPC _GLOBAL_OFFSET_TABLE_ // e8 00 00 00 00 call runtime.addmoduledata@plt + R_CALL runtime.addmoduledata // 5b pop %ebx // c3 ret o(0x53) o(0xe8) addcall(ctxt, initfunc, ld.Linklookup(ctxt, "__x86.get_pc_thunk.cx", 0)) o(0x8d, 0x81) ld.Addpcrelplus(ctxt, initfunc, ctxt.Moduledata, 6) o(0x8d, 0x99) i := initfunc.Size initfunc.Size += 4 ld.Symgrow(ctxt, initfunc, initfunc.Size) r := ld.Addrel(initfunc) r.Sym = ld.Linklookup(ctxt, "_GLOBAL_OFFSET_TABLE_", 0) r.Off = int32(i) r.Type = obj.R_PCREL r.Add = 12 r.Siz = 4 o(0xe8) addcall(ctxt, initfunc, addmoduledata) o(0x5b) o(0xc3) ctxt.Textp = append(ctxt.Textp, initfunc) initarray_entry := ld.Linklookup(ctxt, "go.link.addmoduledatainit", 0) initarray_entry.Attr |= ld.AttrReachable initarray_entry.Attr |= ld.AttrLocal initarray_entry.Type = obj.SINITARR ld.Addaddr(ctxt, initarray_entry, initfunc) }