// Convert the direct jump relocation r to refer to a trampoline if the target is too far func trampoline(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol) { switch r.Type { case obj.R_CALLARM: // r.Add is the instruction // low 24-bit encodes the target address t := (ld.Symaddr(r.Sym) + int64(signext24(r.Add&0xffffff)*4) - (s.Value + int64(r.Off))) / 4 if t > 0x7fffff || t < -0x800000 || (*ld.FlagDebugTramp > 1 && s.File != r.Sym.File) { // direct call too far, need to insert trampoline. // look up existing trampolines first. if we found one within the range // of direct call, we can reuse it. otherwise create a new one. offset := (signext24(r.Add&0xffffff) + 2) * 4 var tramp *ld.Symbol for i := 0; ; i++ { name := r.Sym.Name + fmt.Sprintf("%+d-tramp%d", offset, i) tramp = ctxt.Syms.Lookup(name, int(r.Sym.Version)) if tramp.Type == obj.SDYNIMPORT { // don't reuse trampoline defined in other module continue } if tramp.Value == 0 { // either the trampoline does not exist -- we need to create one, // or found one the address which is not assigned -- this will be // laid down immediately after the current function. use this one. break } t = (ld.Symaddr(tramp) - 8 - (s.Value + int64(r.Off))) / 4 if t >= -0x800000 && t < 0x7fffff { // found an existing trampoline that is not too far // we can just use it break } } if tramp.Type == 0 { // trampoline does not exist, create one ctxt.AddTramp(tramp) if ctxt.DynlinkingGo() { if immrot(uint32(offset)) == 0 { ld.Errorf(s, "odd offset in dynlink direct call: %v+%d", r.Sym, offset) } gentrampdyn(tramp, r.Sym, int64(offset)) } else if ld.Buildmode == ld.BuildmodeCArchive || ld.Buildmode == ld.BuildmodeCShared || ld.Buildmode == ld.BuildmodePIE { gentramppic(tramp, r.Sym, int64(offset)) } else { gentramp(tramp, r.Sym, int64(offset)) } } // modify reloc to point to tramp, which will be resolved later r.Sym = tramp r.Add = r.Add&0xff000000 | 0xfffffe // clear the offset embedded in the instruction r.Done = 0 } default: ld.Errorf(s, "trampoline called with non-jump reloc: %v", r.Type) } }
// Convert the direct jump relocation r to refer to a trampoline if the target is too far func trampoline(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol) { switch r.Type { case obj.R_CALLARM: // r.Add is the instruction // low 24-bit encodes the target address t := (ld.Symaddr(r.Sym) + int64(signext24(r.Add&0xffffff)*4) - (s.Value + int64(r.Off))) / 4 if t > 0x7fffff || t < -0x800000 || (*ld.FlagDebugTramp > 1 && s.File != r.Sym.File) { // direct call too far, need to insert trampoline offset := (signext24(r.Add&0xffffff) + 2) * 4 var tramp *ld.Symbol for i := 0; ; i++ { name := r.Sym.Name + fmt.Sprintf("%+d-tramp%d", offset, i) tramp = ctxt.Syms.Lookup(name, int(r.Sym.Version)) if tramp.Value == 0 { // either the trampoline does not exist -- we need to create one, // or found one the address which is not assigned -- this will be // laid down immediately after the current function. use this one. break } t = (ld.Symaddr(tramp) - 8 - (s.Value + int64(r.Off))) / 4 if t >= -0x800000 && t < 0x7fffff { // found an existing trampoline that is not too far // we can just use it break } } if tramp.Type == 0 { // trampoline does not exist, create one ctxt.AddTramp(tramp) tramp.Size = 12 // 3 instructions tramp.P = make([]byte, tramp.Size) t = ld.Symaddr(r.Sym) + int64(offset) o1 := uint32(0xe5900000 | 11<<12 | 15<<16) // MOVW (R15), R11 // R15 is actual pc + 8 o2 := uint32(0xe12fff10 | 11) // JMP (R11) o3 := uint32(t) // WORD $target ld.SysArch.ByteOrder.PutUint32(tramp.P, o1) ld.SysArch.ByteOrder.PutUint32(tramp.P[4:], o2) ld.SysArch.ByteOrder.PutUint32(tramp.P[8:], o3) } // modify reloc to point to tramp, which will be resolved later r.Sym = tramp r.Add = r.Add&0xff000000 | 0xfffffe // clear the offset embedded in the instruction r.Done = 0 } default: ld.Errorf(s, "trampoline called with non-jump reloc: %v", r.Type) } }
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 == 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 == 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) }
func adddynrel(s *ld.Symbol, 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_PPC64_REL24: r.Type = obj.R_CALLPOWER // This is a local call, so the caller isn't setting // up r12 and r2 is the same for the caller and // callee. Hence, we need to go to the local entry // point. (If we don't do this, the callee will try // to use r12 to compute r2.) r.Add += int64(r.Sym.Localentry) * 4 if targ.Type == obj.SDYNIMPORT { // Should have been handled in elfsetupplt ld.Diag("unexpected R_PPC64_REL24 for dyn import") } return case 256 + ld.R_PPC_REL32: r.Type = obj.R_PCREL r.Add += 4 if targ.Type == obj.SDYNIMPORT { ld.Diag("unexpected R_PPC_REL32 for dyn import") } return case 256 + ld.R_PPC64_ADDR64: r.Type = obj.R_ADDR if targ.Type == obj.SDYNIMPORT { // These happen in .toc sections ld.Adddynsym(ld.Ctxt, targ) rela := ld.Linklookup(ld.Ctxt, ".rela", 0) ld.Addaddrplus(ld.Ctxt, rela, s, int64(r.Off)) ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_PPC64_ADDR64)) ld.Adduint64(ld.Ctxt, rela, uint64(r.Add)) r.Type = 256 // ignore during relocsym } return case 256 + ld.R_PPC64_TOC16: r.Type = obj.R_POWER_TOC r.Variant = ld.RV_POWER_LO | ld.RV_CHECK_OVERFLOW return case 256 + ld.R_PPC64_TOC16_LO: r.Type = obj.R_POWER_TOC r.Variant = ld.RV_POWER_LO return case 256 + ld.R_PPC64_TOC16_HA: r.Type = obj.R_POWER_TOC r.Variant = ld.RV_POWER_HA | ld.RV_CHECK_OVERFLOW return case 256 + ld.R_PPC64_TOC16_HI: r.Type = obj.R_POWER_TOC r.Variant = ld.RV_POWER_HI | ld.RV_CHECK_OVERFLOW return case 256 + ld.R_PPC64_TOC16_DS: r.Type = obj.R_POWER_TOC r.Variant = ld.RV_POWER_DS | ld.RV_CHECK_OVERFLOW return case 256 + ld.R_PPC64_TOC16_LO_DS: r.Type = obj.R_POWER_TOC r.Variant = ld.RV_POWER_DS return case 256 + ld.R_PPC64_REL16_LO: r.Type = obj.R_PCREL r.Variant = ld.RV_POWER_LO r.Add += 2 // Compensate for relocation size of 2 return case 256 + ld.R_PPC64_REL16_HI: r.Type = obj.R_PCREL r.Variant = ld.RV_POWER_HI | ld.RV_CHECK_OVERFLOW r.Add += 2 return case 256 + ld.R_PPC64_REL16_HA: r.Type = obj.R_PCREL r.Variant = ld.RV_POWER_HA | ld.RV_CHECK_OVERFLOW r.Add += 2 return } // Handle references to ELF symbols from our own object files. if targ.Type != obj.SDYNIMPORT { return } // TODO(austin): Translate our relocations to ELF ld.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type) }
func adddynrel(ctxt *ld.Link, s *ld.Symbol, r *ld.Reloc) { targ := r.Sym ctxt.Cursym = s switch r.Type { default: if r.Type >= 256 { ctxt.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(ctxt, targ) r.Sym = ld.Linklookup(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(ctxt, targ) } else { addgotsym(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(ctxt, targ) } else { addgotsym(ctxt, targ) } r.Type = obj.R_PCREL r.Sym = ld.Linklookup(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(ctxt, ".got", 0) r.Add += 4 return case 256 + ld.R_ARM_CALL: r.Type = obj.R_CALLARM if targ.Type == obj.SDYNIMPORT { addpltsym(ctxt, targ) r.Sym = ld.Linklookup(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 { ctxt.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(ctxt, targ) r.Sym = ld.Linklookup(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(ctxt, targ) r.Sym = ld.Linklookup(ctxt, ".plt", 0) r.Add = int64(targ.Plt) return case obj.R_ADDR: if s.Type != obj.SDATA { break } if ld.Iself { ld.Adddynsym(ctxt, targ) rel := ld.Linklookup(ctxt, ".rel", 0) ld.Addaddrplus(ctxt, rel, s, int64(r.Off)) ld.Adduint32(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 } } ctxt.Cursym = s ctxt.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type) }
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.Thearch.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 adddynrel(ctxt *ld.Link, s *ld.Symbol, r *ld.Reloc) { targ := r.Sym ctxt.Cursym = s switch r.Type { default: if r.Type >= 256 { ctxt.Diag("unexpected relocation type %d", r.Type) return } // Handle relocations found in ELF object files. case 256 + ld.R_390_12, 256 + ld.R_390_GOT12: ctxt.Diag("s390x 12-bit relocations have not been implemented (relocation type %d)", r.Type-256) return case 256 + ld.R_390_8, 256 + ld.R_390_16, 256 + ld.R_390_32, 256 + ld.R_390_64: if targ.Type == obj.SDYNIMPORT { ctxt.Diag("unexpected R_390_nn relocation for dynamic symbol %s", targ.Name) } r.Type = obj.R_ADDR return case 256 + ld.R_390_PC16, 256 + ld.R_390_PC32, 256 + ld.R_390_PC64: if targ.Type == obj.SDYNIMPORT { ctxt.Diag("unexpected R_390_PCnn relocation for dynamic symbol %s", targ.Name) } if targ.Type == 0 || targ.Type == obj.SXREF { ctxt.Diag("unknown symbol %s in pcrel", targ.Name) } r.Type = obj.R_PCREL r.Add += int64(r.Siz) return case 256 + ld.R_390_GOT16, 256 + ld.R_390_GOT32, 256 + ld.R_390_GOT64: ctxt.Diag("unimplemented S390x relocation: %v", r.Type-256) return case 256 + ld.R_390_PLT16DBL, 256 + ld.R_390_PLT32DBL: r.Type = obj.R_PCREL r.Variant = ld.RV_390_DBL r.Add += int64(r.Siz) if targ.Type == obj.SDYNIMPORT { addpltsym(ctxt, targ) r.Sym = ld.Linklookup(ctxt, ".plt", 0) r.Add += int64(targ.Plt) } return case 256 + ld.R_390_PLT32, 256 + ld.R_390_PLT64: r.Type = obj.R_PCREL r.Add += int64(r.Siz) if targ.Type == obj.SDYNIMPORT { addpltsym(ctxt, targ) r.Sym = ld.Linklookup(ctxt, ".plt", 0) r.Add += int64(targ.Plt) } return case 256 + ld.R_390_COPY: ctxt.Diag("unimplemented S390x relocation: %v", r.Type-256) case 256 + ld.R_390_GLOB_DAT: ctxt.Diag("unimplemented S390x relocation: %v", r.Type-256) case 256 + ld.R_390_JMP_SLOT: ctxt.Diag("unimplemented S390x relocation: %v", r.Type-256) case 256 + ld.R_390_RELATIVE: ctxt.Diag("unimplemented S390x relocation: %v", r.Type-256) case 256 + ld.R_390_GOTOFF: if targ.Type == obj.SDYNIMPORT { ctxt.Diag("unexpected R_390_GOTOFF relocation for dynamic symbol %s", targ.Name) } r.Type = obj.R_GOTOFF return case 256 + ld.R_390_GOTPC: r.Type = obj.R_PCREL r.Sym = ld.Linklookup(ctxt, ".got", 0) r.Add += int64(r.Siz) return case 256 + ld.R_390_PC16DBL, 256 + ld.R_390_PC32DBL: r.Type = obj.R_PCREL r.Variant = ld.RV_390_DBL r.Add += int64(r.Siz) if targ.Type == obj.SDYNIMPORT { ctxt.Diag("unexpected R_390_PCnnDBL relocation for dynamic symbol %s", targ.Name) } return case 256 + ld.R_390_GOTPCDBL: r.Type = obj.R_PCREL r.Variant = ld.RV_390_DBL r.Sym = ld.Linklookup(ctxt, ".got", 0) r.Add += int64(r.Siz) return case 256 + ld.R_390_GOTENT: addgotsym(ctxt, targ) r.Type = obj.R_PCREL r.Variant = ld.RV_390_DBL r.Sym = ld.Linklookup(ctxt, ".got", 0) r.Add += int64(targ.Got) r.Add += int64(r.Siz) return } // Handle references to ELF symbols from our own object files. if targ.Type != obj.SDYNIMPORT { return } ctxt.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type) }
func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int { if ld.Linkmode == ld.LinkExternal { switch r.Type { default: return -1 case obj.R_ADDRARM64: r.Done = 0 // set up addend for eventual relocation via outer symbol. rs := r.Sym r.Xadd = r.Add for rs.Outer != nil { r.Xadd += ld.Symaddr(rs) - ld.Symaddr(rs.Outer) rs = rs.Outer } if rs.Type != obj.SHOSTOBJ && rs.Sect == nil { ld.Diag("missing section for %s", rs.Name) } r.Xsym = rs // the first instruction is always at the lower address, this is endian neutral; // but note that o0 and o1 should still use the target endian. o0 := ld.Thelinkarch.ByteOrder.Uint32(s.P[r.Off : r.Off+4]) o1 := ld.Thelinkarch.ByteOrder.Uint32(s.P[r.Off+4 : r.Off+8]) // Note: ld64 currently has a bug that any non-zero addend for BR26 relocation // will make the linking fail because it thinks the code is not PIC even though // the BR26 relocation should be fully resolved at link time. // That is the reason why the next if block is disabled. When the bug in ld64 // is fixed, we can enable this block and also enable duff's device in cmd/7g. if false && ld.HEADTYPE == obj.Hdarwin { // Mach-O wants the addend to be encoded in the instruction // Note that although Mach-O supports ARM64_RELOC_ADDEND, it // can only encode 24-bit of signed addend, but the instructions // supports 33-bit of signed addend, so we always encode the // addend in place. o0 |= (uint32((r.Xadd>>12)&3) << 29) | (uint32((r.Xadd>>12>>2)&0x7ffff) << 5) o1 |= uint32(r.Xadd&0xfff) << 10 r.Xadd = 0 } // when laid out, the instruction order must always be o1, o2. if ld.Ctxt.Arch.ByteOrder == binary.BigEndian { *val = int64(o0)<<32 | int64(o1) } else { *val = int64(o1)<<32 | int64(o0) } return 0 case obj.R_CALLARM64: r.Done = 0 r.Xsym = r.Sym *val = int64(0xfc000000 & uint32(r.Add)) r.Xadd = int64((uint32(r.Add) &^ 0xfc000000) * 4) r.Add = 0 return 0 } } switch r.Type { case obj.R_CONST: *val = r.Add return 0 case obj.R_GOTOFF: *val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ld.Linklookup(ld.Ctxt, ".got", 0)) return 0 case obj.R_ADDRARM64: t := ld.Symaddr(r.Sym) + r.Add - ((s.Value + int64(r.Off)) &^ 0xfff) if t >= 1<<32 || t < -1<<32 { ld.Diag("program too large, address relocation distance = %d", t) } // the first instruction is always at the lower address, this is endian neutral; // but note that o0 and o1 should still use the target endian. o0 := ld.Thelinkarch.ByteOrder.Uint32(s.P[r.Off : r.Off+4]) o1 := ld.Thelinkarch.ByteOrder.Uint32(s.P[r.Off+4 : r.Off+8]) o0 |= (uint32((t>>12)&3) << 29) | (uint32((t>>12>>2)&0x7ffff) << 5) o1 |= uint32(t&0xfff) << 10 // when laid out, the instruction order must always be o1, o2. if ld.Ctxt.Arch.ByteOrder == binary.BigEndian { *val = int64(o0)<<32 | int64(o1) } else { *val = int64(o1)<<32 | int64(o0) } return 0 case obj.R_CALLARM64: *val = int64((0xfc000000 & uint32(r.Add)) | uint32((ld.Symaddr(r.Sym)+r.Add*4-(s.Value+int64(r.Off)))/4)) return 0 } return -1 }
// resolve direct jump relocation r in s, and add trampoline if necessary func trampoline(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol) { t := ld.Symaddr(r.Sym) + r.Add - (s.Value + int64(r.Off)) switch r.Type { case obj.R_CALLPOWER: // If branch offset is too far then create a trampoline. if int64(int32(t<<6)>>6) != t || (*ld.FlagDebugTramp > 1 && s.File != r.Sym.File) { var tramp *ld.Symbol for i := 0; ; i++ { // Using r.Add as part of the name is significant in functions like duffzero where the call // target is at some offset within the function. Calls to duff+8 and duff+256 must appear as // distinct trampolines. name := r.Sym.Name if r.Add == 0 { name = name + fmt.Sprintf("-tramp%d", i) } else { name = name + fmt.Sprintf("%+x-tramp%d", r.Add, i) } // Look up the trampoline in case it already exists tramp = ctxt.Syms.Lookup(name, int(r.Sym.Version)) if tramp.Value == 0 { break } t = ld.Symaddr(tramp) + r.Add - (s.Value + int64(r.Off)) // If the offset of the trampoline that has been found is within range, use it. if int64(int32(t<<6)>>6) == t { break } } if tramp.Type == 0 { ctxt.AddTramp(tramp) tramp.Size = 16 // 4 instructions tramp.P = make([]byte, tramp.Size) t = ld.Symaddr(r.Sym) + r.Add f := t & 0xffff0000 o1 := uint32(0x3fe00000 | (f >> 16)) // lis r31,trampaddr hi (r31 is temp reg) f = t & 0xffff o2 := uint32(0x63ff0000 | f) // ori r31,trampaddr lo o3 := uint32(0x7fe903a6) // mtctr o4 := uint32(0x4e800420) // bctr ld.SysArch.ByteOrder.PutUint32(tramp.P, o1) ld.SysArch.ByteOrder.PutUint32(tramp.P[4:], o2) ld.SysArch.ByteOrder.PutUint32(tramp.P[8:], o3) ld.SysArch.ByteOrder.PutUint32(tramp.P[12:], o4) } r.Sym = tramp r.Add = 0 // This was folded into the trampoline target address r.Done = 0 } default: ld.Errorf(s, "trampoline called with non-jump reloc: %v", r.Type) } }
func adddynrel(ctxt *ld.Link, s *ld.Symbol, r *ld.Reloc) bool { targ := r.Sym switch r.Type { default: if r.Type >= 256 { ld.Errorf(s, "unexpected relocation type %d", r.Type) return false } // Handle relocations found in ELF object files. case 256 + ld.R_X86_64_PC32: if targ.Type == obj.SDYNIMPORT { ld.Errorf(s, "unexpected R_X86_64_PC32 relocation for dynamic symbol %s", targ.Name) } if targ.Type == 0 || targ.Type == obj.SXREF { ld.Errorf(s, "unknown symbol %s in pcrel", targ.Name) } r.Type = obj.R_PCREL r.Add += 4 return true case 256 + ld.R_X86_64_PLT32: r.Type = obj.R_PCREL r.Add += 4 if targ.Type == obj.SDYNIMPORT { addpltsym(ctxt, targ) r.Sym = ctxt.Syms.Lookup(".plt", 0) r.Add += int64(targ.Plt) } return true 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 true } } // fall back to using GOT and hope for the best (CMOV*) // TODO: just needs relocation, no need to put in .dynsym addgotsym(ctxt, targ) r.Type = obj.R_PCREL r.Sym = ctxt.Syms.Lookup(".got", 0) r.Add += 4 r.Add += int64(targ.Got) return true case 256 + ld.R_X86_64_64: if targ.Type == obj.SDYNIMPORT { ld.Errorf(s, "unexpected R_X86_64_64 relocation for dynamic symbol %s", targ.Name) } r.Type = obj.R_ADDR return true // 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.Errorf(s, "unexpected reloc for dynamic symbol %s", targ.Name) } return true case 512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 1: if targ.Type == obj.SDYNIMPORT { addpltsym(ctxt, targ) r.Sym = ctxt.Syms.Lookup(".plt", 0) r.Add = int64(targ.Plt) r.Type = obj.R_PCREL return true } 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.Errorf(s, "unexpected pc-relative reloc for dynamic symbol %s", targ.Name) } return true 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.Errorf(s, "unexpected GOT_LOAD reloc for non-dynamic symbol %s", targ.Name) return false } s.P[r.Off-2] = 0x8d r.Type = obj.R_PCREL return true } fallthrough // fall through case 512 + ld.MACHO_X86_64_RELOC_GOT*2 + 1: if targ.Type != obj.SDYNIMPORT { ld.Errorf(s, "unexpected GOT reloc for non-dynamic symbol %s", targ.Name) } addgotsym(ctxt, targ) r.Type = obj.R_PCREL r.Sym = ctxt.Syms.Lookup(".got", 0) r.Add += int64(targ.Got) return true } switch r.Type { case obj.R_CALL, obj.R_PCREL: if targ.Type != obj.SDYNIMPORT { // nothing to do, the relocation will be laid out in reloc return true } if ld.Headtype == obj.Hwindows || ld.Headtype == obj.Hwindowsgui { // nothing to do, the relocation will be laid out in pereloc1 return true } else { // for both ELF and Mach-O addpltsym(ctxt, targ) r.Sym = ctxt.Syms.Lookup(".plt", 0) r.Add = int64(targ.Plt) return true } case obj.R_ADDR: if s.Type == obj.STEXT && ld.Iself { if ld.Headtype == obj.Hsolaris { addpltsym(ctxt, targ) r.Sym = ctxt.Syms.Lookup(".plt", 0) r.Add += int64(targ.Plt) return true } // The code is asking for the address of an external // function. We provide it with the address of the // correspondent GOT symbol. addgotsym(ctxt, targ) r.Sym = ctxt.Syms.Lookup(".got", 0) r.Add += int64(targ.Got) return true } // Process dynamic relocations for the data sections. if ld.Buildmode == ld.BuildmodePIE && ld.Linkmode == ld.LinkInternal { // When internally linking, generate dynamic relocations // for all typical R_ADDR relocations. The exception // are those R_ADDR that are created as part of generating // the dynamic relocations and must be resolved statically. // // There are three phases relevant to understanding this: // // dodata() // we are here // address() // symbol address assignment // reloc() // resolution of static R_ADDR relocs // // At this point symbol addresses have not been // assigned yet (as the final size of the .rela section // will affect the addresses), and so we cannot write // the Elf64_Rela.r_offset now. Instead we delay it // until after the 'address' phase of the linker is // complete. We do this via Addaddrplus, which creates // a new R_ADDR relocation which will be resolved in // the 'reloc' phase. // // These synthetic static R_ADDR relocs must be skipped // now, or else we will be caught in an infinite loop // of generating synthetic relocs for our synthetic // relocs. switch s.Name { case ".dynsym", ".rela", ".got.plt", ".dynamic": return false } } else { // Either internally linking a static executable, // in which case we can resolve these relocations // statically in the 'reloc' phase, or externally // linking, in which case the relocation will be // prepared in the 'reloc' phase and passed to the // external linker in the 'asmb' phase. if s.Type != obj.SDATA && s.Type != obj.SRODATA { break } } if ld.Iself { // TODO: We generate a R_X86_64_64 relocation for every R_ADDR, even // though it would be more efficient (for the dynamic linker) if we // generated R_X86_RELATIVE instead. ld.Adddynsym(ctxt, targ) rela := ctxt.Syms.Lookup(".rela", 0) ld.Addaddrplus(ctxt, rela, s, int64(r.Off)) if r.Siz == 8 { ld.Adduint64(ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_X86_64_64)) } else { // TODO: never happens, remove. ld.Adduint64(ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_X86_64_32)) } ld.Adduint64(ctxt, rela, uint64(r.Add)) r.Type = 256 // ignore during relocsym return true } 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(ctxt, targ) got := ctxt.Syms.Lookup(".got", 0) s.Type = got.Type | obj.SSUB s.Outer = got s.Sub = got.Sub got.Sub = s s.Value = got.Size ld.Adduint64(ctxt, got, 0) ld.Adduint32(ctxt, ctxt.Syms.Lookup(".linkedit.got", 0), uint32(targ.Dynid)) r.Type = 256 // ignore during relocsym return true } if ld.Headtype == obj.Hwindows || ld.Headtype == obj.Hwindowsgui { // nothing to do, the relocation will be laid out in pereloc1 return true } } return false }