| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | package ld |
| |
|
| | import ( |
| | "bytes" |
| | "cmd/internal/gcprog" |
| | "cmd/internal/objabi" |
| | "cmd/internal/sys" |
| | "cmd/link/internal/loader" |
| | "cmd/link/internal/loadpe" |
| | "cmd/link/internal/sym" |
| | "compress/zlib" |
| | "debug/elf" |
| | "encoding/binary" |
| | "fmt" |
| | "internal/abi" |
| | "log" |
| | "math/rand" |
| | "os" |
| | "sort" |
| | "strconv" |
| | "strings" |
| | "sync" |
| | "sync/atomic" |
| | ) |
| |
|
| | |
| | func isRuntimeDepPkg(pkg string) bool { |
| | return objabi.LookupPkgSpecial(pkg).Runtime |
| | } |
| |
|
| | |
| | |
| | |
| | func maxSizeTrampolines(ctxt *Link, ldr *loader.Loader, s loader.Sym, isTramp bool) uint64 { |
| | |
| | |
| | if thearch.Trampoline == nil || isTramp { |
| | return 0 |
| | } |
| |
|
| | n := uint64(0) |
| | relocs := ldr.Relocs(s) |
| | for ri := 0; ri < relocs.Count(); ri++ { |
| | r := relocs.At(ri) |
| | if r.Type().IsDirectCallOrJump() { |
| | n++ |
| | } |
| | } |
| |
|
| | switch { |
| | case ctxt.IsARM(): |
| | return n * 20 |
| | case ctxt.IsARM64(): |
| | return n * 12 |
| | case ctxt.IsLOONG64(): |
| | return n * 12 |
| | case ctxt.IsPPC64(): |
| | return n * 16 |
| | case ctxt.IsRISCV64(): |
| | return n * 8 |
| | } |
| | panic("unreachable") |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | func trampoline(ctxt *Link, s loader.Sym) { |
| | if thearch.Trampoline == nil { |
| | return |
| | } |
| |
|
| | ldr := ctxt.loader |
| | relocs := ldr.Relocs(s) |
| | for ri := 0; ri < relocs.Count(); ri++ { |
| | r := relocs.At(ri) |
| | rt := r.Type() |
| | if !rt.IsDirectCallOrJump() && !isPLTCall(ctxt.Arch, rt) { |
| | continue |
| | } |
| | rs := r.Sym() |
| | if !ldr.AttrReachable(rs) || ldr.SymType(rs) == sym.Sxxx { |
| | continue |
| | } |
| |
|
| | if ldr.SymValue(rs) == 0 && ldr.SymType(rs) != sym.SDYNIMPORT && ldr.SymType(rs) != sym.SUNDEFEXT { |
| | |
| | |
| | |
| | |
| | if ldr.SymPkg(s) != "" && ldr.SymPkg(rs) == ldr.SymPkg(s) && ldr.SymType(rs) == ldr.SymType(s) && *flagRandLayout == 0 { |
| | |
| | |
| | |
| | if !ctxt.Target.IsRISCV64() { |
| | continue |
| | } |
| | } |
| | |
| | if isRuntimeDepPkg(ldr.SymPkg(s)) && isRuntimeDepPkg(ldr.SymPkg(rs)) && *flagRandLayout == 0 { |
| | continue |
| | } |
| | } |
| | thearch.Trampoline(ctxt, ldr, ri, rs, s) |
| | } |
| | } |
| |
|
| | |
| | |
| | func isPLTCall(arch *sys.Arch, rt objabi.RelocType) bool { |
| | const pcrel = 1 |
| | switch uint32(arch.Family) | uint32(rt)<<8 { |
| | |
| | case uint32(sys.ARM64) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_AARCH64_CALL26))<<8, |
| | uint32(sys.ARM64) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_AARCH64_JUMP26))<<8, |
| | uint32(sys.ARM64) | uint32(objabi.MachoRelocOffset+MACHO_ARM64_RELOC_BRANCH26*2+pcrel)<<8: |
| | return true |
| |
|
| | |
| | case uint32(sys.ARM) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_ARM_CALL))<<8, |
| | uint32(sys.ARM) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_ARM_PC24))<<8, |
| | uint32(sys.ARM) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_ARM_JUMP24))<<8: |
| | return true |
| |
|
| | |
| | case uint32(sys.Loong64) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_LARCH_B26))<<8: |
| | return true |
| | } |
| | |
| | return false |
| | } |
| |
|
| | |
| | |
| | |
| | func FoldSubSymbolOffset(ldr *loader.Loader, s loader.Sym) (loader.Sym, int64) { |
| | outer := ldr.OuterSym(s) |
| | off := int64(0) |
| | if outer != 0 { |
| | off += ldr.SymValue(s) - ldr.SymValue(outer) |
| | s = outer |
| | } |
| | return s, off |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (st *relocSymState) relocsym(s loader.Sym, P []byte) { |
| | ldr := st.ldr |
| | relocs := ldr.Relocs(s) |
| | if relocs.Count() == 0 { |
| | return |
| | } |
| | target := st.target |
| | syms := st.syms |
| | nExtReloc := 0 |
| | for ri := 0; ri < relocs.Count(); ri++ { |
| | r := relocs.At(ri) |
| | off := r.Off() |
| | siz := int32(r.Siz()) |
| | rs := r.Sym() |
| | rt := r.Type() |
| | weak := r.Weak() |
| | if off < 0 || off+siz > int32(len(P)) { |
| | rname := "" |
| | if rs != 0 { |
| | rname = ldr.SymName(rs) |
| | } |
| | st.err.Errorf(s, "invalid relocation %s: %d+%d not in [%d,%d)", rname, off, siz, 0, len(P)) |
| | continue |
| | } |
| | if siz == 0 { |
| | continue |
| | } |
| |
|
| | var rst sym.SymKind |
| | if rs != 0 { |
| | rst = ldr.SymType(rs) |
| | } |
| |
|
| | if rs != 0 && (rst == sym.Sxxx || rst == sym.SXREF) { |
| | |
| | |
| | if target.IsShared() || target.IsPlugin() { |
| | if ldr.SymName(rs) == "main.main" || (!target.IsPlugin() && ldr.SymName(rs) == "main..inittask") { |
| | sb := ldr.MakeSymbolUpdater(rs) |
| | sb.SetType(sym.SDYNIMPORT) |
| | } else if strings.HasPrefix(ldr.SymName(rs), "go:info.") { |
| | |
| | |
| | continue |
| | } |
| | } else if target.IsPPC64() && ldr.SymName(rs) == ".TOC." { |
| | |
| | |
| | |
| | } else { |
| | st.err.errorUnresolved(ldr, s, rs) |
| | continue |
| | } |
| | } |
| |
|
| | if rt >= objabi.ElfRelocOffset { |
| | continue |
| | } |
| |
|
| | |
| | |
| | if !target.IsAIX() && !target.IsDarwin() && !target.IsSolaris() && !target.IsOpenbsd() && rs != 0 && rst == sym.SDYNIMPORT && !target.IsDynlinkingGo() && !ldr.AttrSubSymbol(rs) { |
| | if !(target.IsPPC64() && target.IsExternal() && ldr.SymName(rs) == ".TOC.") { |
| | st.err.Errorf(s, "unhandled relocation for %s (type %d (%s) rtype %d (%s))", ldr.SymName(rs), rst, rst, rt, sym.RelocName(target.Arch, rt)) |
| | } |
| | } |
| | if rs != 0 && rst != sym.STLSBSS && !weak && rt != objabi.R_METHODOFF && !ldr.AttrReachable(rs) { |
| | st.err.Errorf(s, "unreachable sym in relocation: %s", ldr.SymName(rs)) |
| | } |
| |
|
| | var rv sym.RelocVariant |
| | if target.IsPPC64() || target.IsS390X() { |
| | rv = ldr.RelocVariant(s, ri) |
| | } |
| |
|
| | |
| | if target.IsS390X() { |
| | switch rt { |
| | case objabi.R_PCRELDBL: |
| | rt = objabi.R_PCREL |
| | rv = sym.RV_390_DBL |
| | case objabi.R_CALL: |
| | rv = sym.RV_390_DBL |
| | } |
| | } |
| |
|
| | var o int64 |
| | switch rt { |
| | default: |
| | switch siz { |
| | default: |
| | st.err.Errorf(s, "bad reloc size %#x for %s", uint32(siz), ldr.SymName(rs)) |
| | case 1: |
| | o = int64(P[off]) |
| | case 2: |
| | o = int64(target.Arch.ByteOrder.Uint16(P[off:])) |
| | case 4: |
| | o = int64(target.Arch.ByteOrder.Uint32(P[off:])) |
| | case 8: |
| | o = int64(target.Arch.ByteOrder.Uint64(P[off:])) |
| | } |
| | out, n, ok := thearch.Archreloc(target, ldr, syms, r, s, o) |
| | if target.IsExternal() { |
| | nExtReloc += n |
| | } |
| | if ok { |
| | o = out |
| | } else { |
| | st.err.Errorf(s, "unknown reloc to %v: %d (%s)", ldr.SymName(rs), rt, sym.RelocName(target.Arch, rt)) |
| | } |
| | case objabi.R_TLS_LE: |
| | if target.IsExternal() && target.IsElf() { |
| | nExtReloc++ |
| | o = 0 |
| | if !target.IsAMD64() { |
| | o = r.Add() |
| | } |
| | break |
| | } |
| |
|
| | if target.IsElf() && target.IsARM() { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | o = 8 + ldr.SymValue(rs) |
| | } else if target.IsElf() || target.IsPlan9() || target.IsDarwin() { |
| | o = int64(syms.Tlsoffset) + r.Add() |
| | } else if target.IsWindows() { |
| | o = r.Add() |
| | } else { |
| | log.Fatalf("unexpected R_TLS_LE relocation for %v", target.HeadType) |
| | } |
| | case objabi.R_TLS_IE: |
| | if target.IsExternal() && target.IsElf() { |
| | nExtReloc++ |
| | o = 0 |
| | if !target.IsAMD64() { |
| | o = r.Add() |
| | } |
| | if target.Is386() { |
| | nExtReloc++ |
| | } |
| | break |
| | } |
| | if target.IsPIE() && target.IsElf() { |
| | |
| | |
| | if thearch.TLSIEtoLE == nil { |
| | log.Fatalf("internal linking of TLS IE not supported on %v", target.Arch.Family) |
| | } |
| | thearch.TLSIEtoLE(P, int(off), int(siz)) |
| | o = int64(syms.Tlsoffset) |
| | } else { |
| | log.Fatalf("cannot handle R_TLS_IE (sym %s) when linking internally", ldr.SymName(s)) |
| | } |
| | case objabi.R_ADDR, objabi.R_PEIMAGEOFF: |
| | if weak && !ldr.AttrReachable(rs) { |
| | |
| | rs = syms.unreachableMethod |
| | } |
| | if target.IsExternal() { |
| | nExtReloc++ |
| |
|
| | |
| | rs := rs |
| | rs, off := FoldSubSymbolOffset(ldr, rs) |
| | xadd := r.Add() + off |
| | rst := ldr.SymType(rs) |
| | if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && rst != sym.SUNDEFEXT && ldr.SymSect(rs) == nil { |
| | st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs)) |
| | } |
| |
|
| | o = xadd |
| | if target.IsElf() { |
| | if target.IsAMD64() { |
| | o = 0 |
| | } |
| | } else if target.IsDarwin() { |
| | if ldr.SymType(s).IsDWARF() { |
| | |
| | |
| | |
| | |
| | o += ldr.SymValue(rs) |
| | } |
| | } else if target.IsWindows() { |
| | |
| | } else if target.IsAIX() { |
| | o = ldr.SymValue(rs) + xadd |
| | } else { |
| | st.err.Errorf(s, "unhandled pcrel relocation to %s on %v", ldr.SymName(rs), target.HeadType) |
| | } |
| |
|
| | break |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | if target.IsAIX() && rst != sym.SDYNIMPORT { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | if ldr.SymSect(s).Seg == &Segdata { |
| | Xcoffadddynrel(target, ldr, syms, s, r, ri) |
| | } |
| | } |
| |
|
| | o = ldr.SymValue(rs) + r.Add() |
| | if rt == objabi.R_PEIMAGEOFF { |
| | |
| | |
| | o -= PEBASE |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | if int32(o) < 0 && target.Arch.PtrSize > 4 && siz == 4 { |
| | st.err.Errorf(s, "non-pc-relative relocation address for %s is too big: %#x (%#x + %#x)", ldr.SymName(rs), uint64(o), ldr.SymValue(rs), r.Add()) |
| | errorexit() |
| | } |
| | case objabi.R_DWTXTADDR_U1, objabi.R_DWTXTADDR_U2, objabi.R_DWTXTADDR_U3, objabi.R_DWTXTADDR_U4: |
| | unit := ldr.SymUnit(rs) |
| | if idx, ok := unit.Addrs[rs]; ok { |
| | o = int64(idx) |
| | } else { |
| | st.err.Errorf(s, "missing .debug_addr index relocation target %s", ldr.SymName(rs)) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | _, leb128len := rt.DwTxtAddrRelocParams() |
| | if err := writeUleb128FixedLength(P[off:], uint64(o), leb128len); err != nil { |
| | st.err.Errorf(s, "internal error: %v applying %s to DWARF sym with reloc target %s", err, rt.String(), ldr.SymName(rs)) |
| | } |
| | continue |
| |
|
| | case objabi.R_DWARFSECREF: |
| | if ldr.SymSect(rs) == nil { |
| | st.err.Errorf(s, "missing DWARF section for relocation target %s", ldr.SymName(rs)) |
| | } |
| |
|
| | if target.IsExternal() { |
| | |
| | |
| | |
| | |
| | |
| | if !target.IsDarwin() { |
| | nExtReloc++ |
| | } |
| |
|
| | xadd := r.Add() + ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr) |
| |
|
| | o = xadd |
| | if target.IsElf() && target.IsAMD64() { |
| | o = 0 |
| | } |
| | break |
| | } |
| | o = ldr.SymValue(rs) + r.Add() - int64(ldr.SymSect(rs).Vaddr) |
| | case objabi.R_METHODOFF: |
| | if !ldr.AttrReachable(rs) { |
| | |
| | |
| | o = -1 |
| | break |
| | } |
| | fallthrough |
| | case objabi.R_ADDROFF: |
| | if weak && !ldr.AttrReachable(rs) { |
| | continue |
| | } |
| | sect := ldr.SymSect(rs) |
| | if sect == nil { |
| | if rst == sym.SDYNIMPORT { |
| | st.err.Errorf(s, "cannot target DYNIMPORT sym in section-relative reloc: %s", ldr.SymName(rs)) |
| | } else if rst == sym.SUNDEFEXT { |
| | st.err.Errorf(s, "undefined symbol in relocation: %s", ldr.SymName(rs)) |
| | } else { |
| | st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs)) |
| | } |
| | continue |
| | } |
| |
|
| | |
| | |
| | if sect.Name == ".text" { |
| | o = ldr.SymValue(rs) - int64(Segtext.Sections[0].Vaddr) + r.Add() |
| | if target.IsWasm() { |
| | |
| | |
| | |
| | if o&(1<<16-1) != 0 { |
| | st.err.Errorf(s, "textoff relocation %s does not target function entry: %s %#x", rt, ldr.SymName(rs), o) |
| | } |
| | o >>= 16 |
| | } |
| | } else { |
| | o = ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr) + r.Add() |
| | } |
| |
|
| | case objabi.R_ADDRCUOFF: |
| | |
| | |
| | o = ldr.SymValue(rs) + r.Add() - ldr.SymValue(ldr.SymUnit(rs).Textp[0]) |
| |
|
| | |
| | case objabi.R_GOTPCREL: |
| | if target.IsDynlinkingGo() && target.IsDarwin() && rs != 0 { |
| | nExtReloc++ |
| | o = r.Add() |
| | break |
| | } |
| | if target.Is386() && target.IsExternal() && target.IsELF { |
| | nExtReloc++ |
| | } |
| | fallthrough |
| | case objabi.R_CALL, objabi.R_PCREL: |
| | if target.IsExternal() && rs != 0 && rst == sym.SUNDEFEXT { |
| | |
| | nExtReloc++ |
| | o = 0 |
| | break |
| | } |
| | if target.IsExternal() && rs != 0 && (ldr.SymSect(rs) != ldr.SymSect(s) || rt == objabi.R_GOTPCREL) { |
| | nExtReloc++ |
| |
|
| | |
| | rs := rs |
| | rs, off := FoldSubSymbolOffset(ldr, rs) |
| | xadd := r.Add() + off - int64(siz) |
| | rst := ldr.SymType(rs) |
| | if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && ldr.SymSect(rs) == nil { |
| | st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs)) |
| | } |
| |
|
| | o = xadd |
| | if target.IsElf() { |
| | if target.IsAMD64() { |
| | o = 0 |
| | } |
| | } else if target.IsDarwin() { |
| | if rt == objabi.R_CALL { |
| | if target.IsExternal() && rst == sym.SDYNIMPORT { |
| | if target.IsAMD64() { |
| | |
| | o += int64(siz) |
| | } |
| | } else { |
| | if rst != sym.SHOSTOBJ { |
| | o += int64(uint64(ldr.SymValue(rs)) - ldr.SymSect(rs).Vaddr) |
| | } |
| | o -= off |
| | } |
| | } else { |
| | o += int64(siz) |
| | } |
| | } else if target.IsWindows() && target.IsAMD64() { |
| | |
| | |
| | o += int64(siz) |
| | } else { |
| | st.err.Errorf(s, "unhandled pcrel relocation to %s on %v", ldr.SymName(rs), target.HeadType) |
| | } |
| |
|
| | break |
| | } |
| |
|
| | o = 0 |
| | if rs != 0 { |
| | o = ldr.SymValue(rs) |
| | } |
| |
|
| | o += r.Add() - (ldr.SymValue(s) + int64(off) + int64(siz)) |
| | case objabi.R_SIZE: |
| | o = ldr.SymSize(rs) + r.Add() |
| |
|
| | case objabi.R_XCOFFREF: |
| | if !target.IsAIX() { |
| | st.err.Errorf(s, "find XCOFF R_REF on non-XCOFF files") |
| | } |
| | if !target.IsExternal() { |
| | st.err.Errorf(s, "find XCOFF R_REF with internal linking") |
| | } |
| | nExtReloc++ |
| | continue |
| |
|
| | case objabi.R_CONST: |
| | o = r.Add() |
| |
|
| | case objabi.R_GOTOFF: |
| | o = ldr.SymValue(rs) + r.Add() - ldr.SymValue(syms.GOT) |
| | } |
| |
|
| | if target.IsPPC64() || target.IsS390X() { |
| | if rv != sym.RV_NONE { |
| | o = thearch.Archrelocvariant(target, ldr, r, rv, s, o, P) |
| | } |
| | } |
| |
|
| | switch siz { |
| | default: |
| | st.err.Errorf(s, "bad reloc size %#x for %s", uint32(siz), ldr.SymName(rs)) |
| | case 1: |
| | P[off] = byte(int8(o)) |
| | case 2: |
| | if (rt == objabi.R_PCREL || rt == objabi.R_CALL) && o != int64(int16(o)) { |
| | st.err.Errorf(s, "pc-relative relocation %s address for %s is too big: %#x", rt, ldr.SymName(rs), o) |
| | } else if o != int64(int16(o)) && o != int64(uint16(o)) { |
| | st.err.Errorf(s, "non-pc-relative relocation %s address for %s is too big: %#x", rt, ldr.SymName(rs), uint64(o)) |
| | } |
| | target.Arch.ByteOrder.PutUint16(P[off:], uint16(o)) |
| | case 4: |
| | if (rt == objabi.R_PCREL || rt == objabi.R_CALL) && o != int64(int32(o)) { |
| | st.err.Errorf(s, "pc-relative relocation %s address for %s is too big: %#x", rt, ldr.SymName(rs), o) |
| | } else if o != int64(int32(o)) && o != int64(uint32(o)) { |
| | st.err.Errorf(s, "non-pc-relative relocation %s address for %s is too big: %#x", rt, ldr.SymName(rs), uint64(o)) |
| | } |
| | target.Arch.ByteOrder.PutUint32(P[off:], uint32(o)) |
| | case 8: |
| | target.Arch.ByteOrder.PutUint64(P[off:], uint64(o)) |
| | } |
| | } |
| | if target.IsExternal() { |
| | |
| | |
| | atomic.AddUint32(&ldr.SymSect(s).Relcount, uint32(nExtReloc)) |
| | } |
| | } |
| |
|
| | |
| | func extreloc(ctxt *Link, ldr *loader.Loader, s loader.Sym, r loader.Reloc) (loader.ExtReloc, bool) { |
| | var rr loader.ExtReloc |
| | target := &ctxt.Target |
| | siz := int32(r.Siz()) |
| | if siz == 0 { |
| | return rr, false |
| | } |
| |
|
| | rt := r.Type() |
| | if rt >= objabi.ElfRelocOffset { |
| | return rr, false |
| | } |
| | rr.Type = rt |
| | rr.Size = uint8(siz) |
| |
|
| | |
| | if target.IsS390X() { |
| | switch rt { |
| | case objabi.R_PCRELDBL: |
| | rt = objabi.R_PCREL |
| | } |
| | } |
| |
|
| | switch rt { |
| | default: |
| | return thearch.Extreloc(target, ldr, r, s) |
| |
|
| | case objabi.R_TLS_LE, objabi.R_TLS_IE: |
| | if target.IsElf() { |
| | rs := r.Sym() |
| | rr.Xsym = rs |
| | if rr.Xsym == 0 { |
| | rr.Xsym = ctxt.Tlsg |
| | } |
| | rr.Xadd = r.Add() |
| | break |
| | } |
| | return rr, false |
| |
|
| | case objabi.R_ADDR, objabi.R_PEIMAGEOFF: |
| | |
| | rs := r.Sym() |
| | if r.Weak() && !ldr.AttrReachable(rs) { |
| | rs = ctxt.ArchSyms.unreachableMethod |
| | } |
| | rs, off := FoldSubSymbolOffset(ldr, rs) |
| | rr.Xadd = r.Add() + off |
| | rr.Xsym = rs |
| |
|
| | case objabi.R_DWARFSECREF: |
| | |
| | |
| | |
| | |
| | |
| | if target.IsDarwin() { |
| | return rr, false |
| | } |
| | rs := r.Sym() |
| | rr.Xsym = ldr.SymSect(rs).Sym |
| | rr.Xadd = r.Add() + ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr) |
| |
|
| | |
| | case objabi.R_GOTPCREL, objabi.R_CALL, objabi.R_PCREL: |
| | rs := r.Sym() |
| | if rt == objabi.R_GOTPCREL && target.IsDynlinkingGo() && target.IsDarwin() && rs != 0 { |
| | rr.Xadd = r.Add() |
| | rr.Xadd -= int64(siz) |
| | rr.Xsym = rs |
| | break |
| | } |
| | if rs != 0 && ldr.SymType(rs) == sym.SUNDEFEXT { |
| | |
| | rr.Xadd = 0 |
| | if target.IsElf() { |
| | rr.Xadd -= int64(siz) |
| | } |
| | rr.Xsym = rs |
| | break |
| | } |
| | if rs != 0 && (ldr.SymSect(rs) != ldr.SymSect(s) || rt == objabi.R_GOTPCREL) { |
| | |
| | rs := rs |
| | rs, off := FoldSubSymbolOffset(ldr, rs) |
| | rr.Xadd = r.Add() + off |
| | rr.Xadd -= int64(siz) |
| | rr.Xsym = rs |
| | break |
| | } |
| | return rr, false |
| |
|
| | case objabi.R_XCOFFREF: |
| | return ExtrelocSimple(ldr, r), true |
| |
|
| | |
| | case objabi.R_ADDROFF, objabi.R_METHODOFF, objabi.R_ADDRCUOFF, |
| | objabi.R_SIZE, objabi.R_CONST, objabi.R_GOTOFF, |
| | objabi.R_DWTXTADDR_U1, objabi.R_DWTXTADDR_U2, |
| | objabi.R_DWTXTADDR_U3, objabi.R_DWTXTADDR_U4: |
| | return rr, false |
| | } |
| | return rr, true |
| | } |
| |
|
| | |
| | |
| | func ExtrelocSimple(ldr *loader.Loader, r loader.Reloc) loader.ExtReloc { |
| | var rr loader.ExtReloc |
| | rs := r.Sym() |
| | rr.Xsym = rs |
| | rr.Xadd = r.Add() |
| | rr.Type = r.Type() |
| | rr.Size = r.Siz() |
| | return rr |
| | } |
| |
|
| | |
| | |
| | func ExtrelocViaOuterSym(ldr *loader.Loader, r loader.Reloc, s loader.Sym) loader.ExtReloc { |
| | |
| | var rr loader.ExtReloc |
| | rs := r.Sym() |
| | rs, off := FoldSubSymbolOffset(ldr, rs) |
| | rr.Xadd = r.Add() + off |
| | rst := ldr.SymType(rs) |
| | if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && rst != sym.SUNDEFEXT && ldr.SymSect(rs) == nil { |
| | ldr.Errorf(s, "missing section for %s", ldr.SymName(rs)) |
| | } |
| | rr.Xsym = rs |
| | rr.Type = r.Type() |
| | rr.Size = r.Siz() |
| | return rr |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | type relocSymState struct { |
| | target *Target |
| | ldr *loader.Loader |
| | err *ErrorReporter |
| | syms *ArchSyms |
| | } |
| |
|
| | |
| | |
| | |
| | func (ctxt *Link) makeRelocSymState() *relocSymState { |
| | return &relocSymState{ |
| | target: &ctxt.Target, |
| | ldr: ctxt.loader, |
| | err: &ctxt.ErrorReporter, |
| | syms: &ctxt.ArchSyms, |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func windynrelocsym(ctxt *Link, rel *loader.SymbolBuilder, s loader.Sym) error { |
| | var su *loader.SymbolBuilder |
| | relocs := ctxt.loader.Relocs(s) |
| | for ri := 0; ri < relocs.Count(); ri++ { |
| | r := relocs.At(ri) |
| | if r.IsMarker() { |
| | continue |
| | } |
| | targ := r.Sym() |
| | if targ == 0 { |
| | continue |
| | } |
| | if !ctxt.loader.AttrReachable(targ) { |
| | if r.Weak() { |
| | continue |
| | } |
| | return fmt.Errorf("dynamic relocation to unreachable symbol %s", |
| | ctxt.loader.SymName(targ)) |
| | } |
| | tgot := ctxt.loader.SymGot(targ) |
| | if tgot == loadpe.RedirectToDynImportGotToken { |
| |
|
| | |
| | sname := ctxt.loader.SymName(targ) |
| | if !strings.HasPrefix(sname, "__imp_") { |
| | return fmt.Errorf("internal error in windynrelocsym: redirect GOT token applied to non-import symbol %s", sname) |
| | } |
| |
|
| | |
| | |
| | ds, err := loadpe.LookupBaseFromImport(targ, ctxt.loader, ctxt.Arch) |
| | if err != nil { |
| | return err |
| | } |
| | dstyp := ctxt.loader.SymType(ds) |
| | if dstyp != sym.SWINDOWS { |
| | return fmt.Errorf("internal error in windynrelocsym: underlying sym for %q has wrong type %s", sname, dstyp.String()) |
| | } |
| |
|
| | |
| | r.SetSym(ds) |
| | continue |
| | } |
| |
|
| | tplt := ctxt.loader.SymPlt(targ) |
| | if tplt == loadpe.CreateImportStubPltToken { |
| |
|
| | |
| | if tgot != -1 { |
| | return fmt.Errorf("internal error in windynrelocsym: invalid GOT setting %d for reloc to %s", tgot, ctxt.loader.SymName(targ)) |
| | } |
| |
|
| | |
| | tplt := int32(rel.Size()) |
| | ctxt.loader.SetPlt(targ, tplt) |
| |
|
| | if su == nil { |
| | su = ctxt.loader.MakeSymbolUpdater(s) |
| | } |
| | r.SetSym(rel.Sym()) |
| | r.SetAdd(int64(tplt)) |
| |
|
| | |
| | switch ctxt.Arch.Family { |
| | default: |
| | return fmt.Errorf("internal error in windynrelocsym: unsupported arch %v", ctxt.Arch.Family) |
| | case sys.I386: |
| | rel.AddUint8(0xff) |
| | rel.AddUint8(0x25) |
| | rel.AddAddrPlus(ctxt.Arch, targ, 0) |
| | rel.AddUint8(0x90) |
| | rel.AddUint8(0x90) |
| | case sys.AMD64: |
| | |
| | |
| | |
| | |
| | |
| | rel.AddUint8(0xff) |
| | rel.AddUint8(0x25) |
| | rel.AddPCRelPlus(ctxt.Arch, targ, 0) |
| | rel.AddUint8(0x90) |
| | rel.AddUint8(0x90) |
| | case sys.ARM64: |
| | |
| | rel.AddUint32(ctxt.Arch, 0x90000010) |
| | r, _ := rel.AddRel(objabi.R_ARM64_PCREL) |
| | r.SetOff(int32(rel.Size() - 4)) |
| | r.SetSiz(4) |
| | r.SetSym(targ) |
| |
|
| | |
| | rel.AddUint32(ctxt.Arch, 0xf9400211) |
| | r, _ = rel.AddRel(objabi.R_ARM64_PCREL) |
| | r.SetOff(int32(rel.Size() - 4)) |
| | r.SetSiz(4) |
| | r.SetSym(targ) |
| |
|
| | |
| | rel.AddUint32(ctxt.Arch, 0xd61f0220) |
| | } |
| | } else if tplt >= 0 { |
| | if su == nil { |
| | su = ctxt.loader.MakeSymbolUpdater(s) |
| | } |
| | r.SetSym(rel.Sym()) |
| | r.SetAdd(int64(tplt)) |
| | } |
| | } |
| | return nil |
| | } |
| |
|
| | |
| | |
| | func (ctxt *Link) windynrelocsyms() { |
| | if !(ctxt.IsWindows() && iscgo && ctxt.IsInternal()) { |
| | return |
| | } |
| |
|
| | rel := ctxt.loader.CreateSymForUpdate(".rel", 0) |
| | rel.SetType(sym.STEXT) |
| |
|
| | for _, s := range ctxt.Textp { |
| | if err := windynrelocsym(ctxt, rel, s); err != nil { |
| | ctxt.Errorf(s, "%v", err) |
| | } |
| | } |
| |
|
| | ctxt.Textp = append(ctxt.Textp, rel.Sym()) |
| | } |
| |
|
| | func dynrelocsym(ctxt *Link, s loader.Sym) { |
| | target := &ctxt.Target |
| | ldr := ctxt.loader |
| | syms := &ctxt.ArchSyms |
| | relocs := ldr.Relocs(s) |
| | for ri := 0; ri < relocs.Count(); ri++ { |
| | r := relocs.At(ri) |
| | if r.IsMarker() { |
| | continue |
| | } |
| | rSym := r.Sym() |
| | if r.Weak() && !ldr.AttrReachable(rSym) { |
| | continue |
| | } |
| | if ctxt.BuildMode == BuildModePIE && ctxt.LinkMode == LinkInternal { |
| | |
| | |
| | |
| | thearch.Adddynrel(target, ldr, syms, s, r, ri) |
| | continue |
| | } |
| |
|
| | if rSym != 0 && ldr.SymType(rSym) == sym.SDYNIMPORT || r.Type() >= objabi.ElfRelocOffset { |
| | if rSym != 0 && !ldr.AttrReachable(rSym) { |
| | ctxt.Errorf(s, "dynamic relocation to unreachable symbol %s", ldr.SymName(rSym)) |
| | } |
| | if !thearch.Adddynrel(target, ldr, syms, s, r, ri) { |
| | ctxt.Errorf(s, "unsupported dynamic relocation for symbol %s (type=%d (%s) stype=%d (%s))", ldr.SymName(rSym), r.Type(), sym.RelocName(ctxt.Arch, r.Type()), ldr.SymType(rSym), ldr.SymType(rSym)) |
| | } |
| | } |
| | } |
| | } |
| |
|
| | func (state *dodataState) dynreloc(ctxt *Link) { |
| | if ctxt.HeadType == objabi.Hwindows { |
| | return |
| | } |
| | |
| | |
| | if *FlagD { |
| | return |
| | } |
| |
|
| | for _, s := range ctxt.Textp { |
| | dynrelocsym(ctxt, s) |
| | } |
| | for _, syms := range state.data { |
| | for _, s := range syms { |
| | dynrelocsym(ctxt, s) |
| | } |
| | } |
| | if ctxt.IsELF { |
| | elfdynhash(ctxt) |
| | } |
| | } |
| |
|
| | func CodeblkPad(ctxt *Link, out *OutBuf, addr int64, size int64, pad []byte) { |
| | writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, ctxt.Textp, addr, size, pad) |
| | } |
| |
|
| | const blockSize = 1 << 20 |
| |
|
| | |
| | |
| | |
| | |
| | |
| | func writeBlocks(ctxt *Link, out *OutBuf, sem chan int, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) { |
| | for i, s := range syms { |
| | if ldr.SymValue(s) >= addr && !ldr.AttrSubSymbol(s) { |
| | syms = syms[i:] |
| | break |
| | } |
| | } |
| |
|
| | var wg sync.WaitGroup |
| | max, lastAddr, written := int64(blockSize), addr+size, int64(0) |
| | for addr < lastAddr { |
| | |
| | idx := -1 |
| | for i, s := range syms { |
| | if ldr.AttrSubSymbol(s) { |
| | continue |
| | } |
| |
|
| | |
| | |
| | end := ldr.SymValue(s) + ldr.SymSize(s) |
| | if end > lastAddr { |
| | break |
| | } |
| |
|
| | |
| | idx = i |
| |
|
| | |
| | if end > addr+max { |
| | break |
| | } |
| | } |
| |
|
| | |
| | if idx < 0 { |
| | break |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | length := int64(0) |
| | if idx+1 < len(syms) { |
| | |
| | |
| | |
| | next := syms[idx+1] |
| | for ldr.AttrSubSymbol(next) { |
| | idx++ |
| | next = syms[idx+1] |
| | } |
| | length = ldr.SymValue(next) - addr |
| | } |
| | if length == 0 || length > lastAddr-addr { |
| | length = lastAddr - addr |
| | } |
| |
|
| | |
| | if ctxt.Out.isMmapped() { |
| | o := out.View(uint64(out.Offset() + written)) |
| | sem <- 1 |
| | wg.Add(1) |
| | go func(o *OutBuf, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) { |
| | writeBlock(ctxt, o, ldr, syms, addr, size, pad) |
| | wg.Done() |
| | <-sem |
| | }(o, ldr, syms, addr, length, pad) |
| | } else { |
| | writeBlock(ctxt, out, ldr, syms, addr, length, pad) |
| | } |
| |
|
| | |
| | if idx != -1 { |
| | syms = syms[idx+1:] |
| | } |
| | written += length |
| | addr += length |
| | } |
| | wg.Wait() |
| | } |
| |
|
| | func writeBlock(ctxt *Link, out *OutBuf, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) { |
| |
|
| | st := ctxt.makeRelocSymState() |
| |
|
| | |
| | |
| | |
| | |
| | eaddr := addr + size |
| | var prev loader.Sym |
| | for _, s := range syms { |
| | if ldr.AttrSubSymbol(s) { |
| | continue |
| | } |
| | val := ldr.SymValue(s) |
| | if val >= eaddr { |
| | break |
| | } |
| | if val < addr { |
| | ldr.Errorf(s, "phase error: addr=%#x but val=%#x sym=%s type=%v sect=%v sect.addr=%#x prev=%s", addr, val, ldr.SymName(s), ldr.SymType(s), ldr.SymSect(s).Name, ldr.SymSect(s).Vaddr, ldr.SymName(prev)) |
| | errorexit() |
| | } |
| | prev = s |
| | if addr < val { |
| | out.WriteStringPad("", int(val-addr), pad) |
| | addr = val |
| | } |
| | P := out.WriteSym(ldr, s) |
| | st.relocsym(s, P) |
| | if ldr.IsGeneratedSym(s) { |
| | f := ctxt.generatorSyms[s] |
| | f(ctxt, s) |
| | } |
| | addr += int64(len(P)) |
| | siz := ldr.SymSize(s) |
| | if addr < val+siz { |
| | out.WriteStringPad("", int(val+siz-addr), pad) |
| | addr = val + siz |
| | } |
| | if addr != val+siz { |
| | ldr.Errorf(s, "phase error: addr=%#x value+size=%#x", addr, val+siz) |
| | errorexit() |
| | } |
| | if val+siz >= eaddr { |
| | break |
| | } |
| | } |
| |
|
| | if addr < eaddr { |
| | out.WriteStringPad("", int(eaddr-addr), pad) |
| | } |
| | } |
| |
|
| | type writeFn func(*Link, *OutBuf, int64, int64) |
| |
|
| | |
| | func writeParallel(wg *sync.WaitGroup, fn writeFn, ctxt *Link, seek, vaddr, length uint64) { |
| | if ctxt.Out.isMmapped() { |
| | out := ctxt.Out.View(seek) |
| | wg.Add(1) |
| | go func() { |
| | defer wg.Done() |
| | fn(ctxt, out, int64(vaddr), int64(length)) |
| | }() |
| | } else { |
| | ctxt.Out.SeekSet(int64(seek)) |
| | fn(ctxt, ctxt.Out, int64(vaddr), int64(length)) |
| | } |
| | } |
| |
|
| | func datblk(ctxt *Link, out *OutBuf, addr, size int64) { |
| | writeDatblkToOutBuf(ctxt, out, addr, size) |
| | } |
| |
|
| | |
| | func DatblkBytes(ctxt *Link, addr int64, size int64) []byte { |
| | buf := make([]byte, size) |
| | out := &OutBuf{heap: buf} |
| | writeDatblkToOutBuf(ctxt, out, addr, size) |
| | return buf |
| | } |
| |
|
| | func writeDatblkToOutBuf(ctxt *Link, out *OutBuf, addr int64, size int64) { |
| | writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, ctxt.datap, addr, size, zeros[:]) |
| | } |
| |
|
| | func dwarfblk(ctxt *Link, out *OutBuf, addr int64, size int64) { |
| | |
| | |
| | |
| | |
| | |
| | |
| | n := 0 |
| | for i := range dwarfp { |
| | n += len(dwarfp[i].syms) |
| | } |
| | syms := make([]loader.Sym, 0, n) |
| | for i := range dwarfp { |
| | syms = append(syms, dwarfp[i].syms...) |
| | } |
| | writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, syms, addr, size, zeros[:]) |
| | } |
| |
|
| | func pdatablk(ctxt *Link, out *OutBuf, addr int64, size int64) { |
| | writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, sehp.pdata, addr, size, zeros[:]) |
| | } |
| |
|
| | func xdatablk(ctxt *Link, out *OutBuf, addr int64, size int64) { |
| | writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, sehp.xdata, addr, size, zeros[:]) |
| | } |
| |
|
| | var covCounterDataStartOff, covCounterDataLen uint64 |
| |
|
| | var zeros [512]byte |
| |
|
| | var ( |
| | strdata = make(map[string]string) |
| | strnames []string |
| | ) |
| |
|
| | func addstrdata1(ctxt *Link, arg string) { |
| | eq := strings.Index(arg, "=") |
| | dot := strings.LastIndex(arg[:eq+1], ".") |
| | if eq < 0 || dot < 0 { |
| | Exitf("-X flag requires argument of the form importpath.name=value") |
| | } |
| | pkg := arg[:dot] |
| | if ctxt.BuildMode == BuildModePlugin && pkg == "main" { |
| | pkg = *flagPluginPath |
| | } |
| | pkg = objabi.PathToPrefix(pkg) |
| | name := pkg + arg[dot:eq] |
| | value := arg[eq+1:] |
| | if _, ok := strdata[name]; !ok { |
| | strnames = append(strnames, name) |
| | } |
| | strdata[name] = value |
| | } |
| |
|
| | |
| | func addstrdata(arch *sys.Arch, l *loader.Loader, name, value string) { |
| | s := l.Lookup(name, 0) |
| | if s == 0 { |
| | return |
| | } |
| | if goType := l.SymGoType(s); goType == 0 { |
| | return |
| | } else if typeName := l.SymName(goType); typeName != "type:string" { |
| | Errorf("%s: cannot set with -X: not a var of type string (%s)", name, typeName) |
| | return |
| | } |
| | if !l.AttrReachable(s) { |
| | return |
| | } |
| | bld := l.MakeSymbolUpdater(s) |
| | if bld.Type() == sym.SBSS { |
| | bld.SetType(sym.SDATA) |
| | } |
| |
|
| | p := fmt.Sprintf("%s.str", name) |
| | sbld := l.CreateSymForUpdate(p, 0) |
| | sbld.Addstring(value) |
| | sbld.SetType(sym.SRODATA) |
| |
|
| | |
| | |
| | |
| | bld.SetData(make([]byte, arch.PtrSize*2)) |
| | bld.SetReadOnly(false) |
| | bld.ResetRelocs() |
| | bld.SetAddrPlus(arch, 0, sbld.Sym(), 0) |
| | bld.SetUint(arch, int64(arch.PtrSize), uint64(len(value))) |
| | } |
| |
|
| | func (ctxt *Link) dostrdata() { |
| | for _, name := range strnames { |
| | addstrdata(ctxt.Arch, ctxt.loader, name, strdata[name]) |
| | } |
| | } |
| |
|
| | |
| | |
| | func addgostring(ctxt *Link, ldr *loader.Loader, s *loader.SymbolBuilder, symname, str string) { |
| | sdata := ldr.CreateSymForUpdate(symname, 0) |
| | if sdata.Type() != sym.Sxxx { |
| | ctxt.Errorf(s.Sym(), "duplicate symname in addgostring: %s", symname) |
| | } |
| | sdata.SetLocal(true) |
| | sdata.SetType(sym.SRODATA) |
| | sdata.SetSize(int64(len(str))) |
| | sdata.SetData([]byte(str)) |
| | s.AddAddr(ctxt.Arch, sdata.Sym()) |
| | s.AddUint(ctxt.Arch, uint64(len(str))) |
| | } |
| |
|
| | func addinitarrdata(ctxt *Link, ldr *loader.Loader, s loader.Sym) { |
| | p := ldr.SymName(s) + ".ptr" |
| | sp := ldr.CreateSymForUpdate(p, 0) |
| | sp.SetType(sym.SINITARR) |
| | sp.SetSize(0) |
| | sp.SetDuplicateOK(true) |
| | sp.AddAddr(ctxt.Arch, s) |
| | } |
| |
|
| | |
| | func symalign(ldr *loader.Loader, s loader.Sym) int32 { |
| | min := int32(thearch.Minalign) |
| | align := ldr.SymAlign(s) |
| | if align >= min { |
| | return align |
| | } else if align != 0 { |
| | return min |
| | } |
| | align = int32(thearch.Maxalign) |
| | ssz := ldr.SymSize(s) |
| | for int64(align) > ssz && align > min { |
| | align >>= 1 |
| | } |
| | ldr.SetSymAlign(s, align) |
| | return align |
| | } |
| |
|
| | func aligndatsize(state *dodataState, datsize int64, s loader.Sym) int64 { |
| | return Rnd(datsize, int64(symalign(state.ctxt.loader, s))) |
| | } |
| |
|
| | const debugGCProg = false |
| |
|
| | type GCProg struct { |
| | ctxt *Link |
| | sym *loader.SymbolBuilder |
| | w gcprog.Writer |
| | } |
| |
|
| | func (p *GCProg) Init(ctxt *Link, name string) { |
| | p.ctxt = ctxt |
| | p.sym = ctxt.loader.CreateSymForUpdate(name, 0) |
| | p.w.Init(p.writeByte()) |
| | if debugGCProg { |
| | fmt.Fprintf(os.Stderr, "ld: start GCProg %s\n", name) |
| | p.w.Debug(os.Stderr) |
| | } |
| | } |
| |
|
| | func (p *GCProg) writeByte() func(x byte) { |
| | return func(x byte) { |
| | p.sym.AddUint8(x) |
| | } |
| | } |
| |
|
| | func (p *GCProg) End(size int64) { |
| | p.w.ZeroUntil(size / int64(p.ctxt.Arch.PtrSize)) |
| | p.w.End() |
| | if debugGCProg { |
| | fmt.Fprintf(os.Stderr, "ld: end GCProg\n") |
| | } |
| | } |
| |
|
| | func (p *GCProg) AddSym(s loader.Sym) { |
| | ldr := p.ctxt.loader |
| | typ := ldr.SymGoType(s) |
| |
|
| | |
| | |
| | if typ == 0 { |
| | switch ldr.SymName(s) { |
| | case "runtime.data", "runtime.edata", "runtime.bss", "runtime.ebss", "runtime.gcdata", "runtime.gcbss": |
| | |
| | |
| | |
| | return |
| | } |
| | p.ctxt.Errorf(p.sym.Sym(), "missing Go type information for global symbol %s: size %d", ldr.SymName(s), ldr.SymSize(s)) |
| | return |
| | } |
| |
|
| | if debugGCProg { |
| | fmt.Fprintf(os.Stderr, "gcprog sym: %s at %d (ptr=%d)\n", ldr.SymName(s), ldr.SymValue(s), ldr.SymValue(s)/int64(p.ctxt.Arch.PtrSize)) |
| | } |
| |
|
| | sval := ldr.SymValue(s) |
| | p.AddType(sval, typ) |
| | } |
| |
|
| | |
| | |
| | |
| | func (p *GCProg) AddType(off int64, typ loader.Sym) { |
| | ldr := p.ctxt.loader |
| | typData := ldr.Data(typ) |
| | ptrdata := decodetypePtrdata(p.ctxt.Arch, typData) |
| | if ptrdata == 0 { |
| | p.ctxt.Errorf(p.sym.Sym(), "has no pointers but in data section") |
| | |
| | |
| | |
| | } |
| | switch decodetypeKind(p.ctxt.Arch, typData) { |
| | default: |
| | if decodetypeGCMaskOnDemand(p.ctxt.Arch, typData) { |
| | p.ctxt.Errorf(p.sym.Sym(), "GC mask not available") |
| | } |
| | |
| | ptrsize := int64(p.ctxt.Arch.PtrSize) |
| | mask := decodetypeGcmask(p.ctxt, typ) |
| | for i := int64(0); i < ptrdata/ptrsize; i++ { |
| | if (mask[i/8]>>uint(i%8))&1 != 0 { |
| | p.w.Ptr(off/ptrsize + i) |
| | } |
| | } |
| | case abi.Array: |
| | elem := decodetypeArrayElem(p.ctxt, p.ctxt.Arch, typ) |
| | n := decodetypeArrayLen(ldr, p.ctxt.Arch, typ) |
| | p.AddType(off, elem) |
| | if n > 1 { |
| | |
| | elemSize := decodetypeSize(p.ctxt.Arch, ldr.Data(elem)) |
| | ptrsize := int64(p.ctxt.Arch.PtrSize) |
| | p.w.ZeroUntil((off + elemSize) / ptrsize) |
| | p.w.Repeat(elemSize/ptrsize, n-1) |
| | } |
| | case abi.Struct: |
| | nField := decodetypeStructFieldCount(ldr, p.ctxt.Arch, typ) |
| | for i := 0; i < nField; i++ { |
| | fTyp := decodetypeStructFieldType(p.ctxt, p.ctxt.Arch, typ, i) |
| | if decodetypePtrdata(p.ctxt.Arch, ldr.Data(fTyp)) == 0 { |
| | continue |
| | } |
| | fOff := decodetypeStructFieldOffset(ldr, p.ctxt.Arch, typ, i) |
| | p.AddType(off+fOff, fTyp) |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | const cutoff = 2e9 |
| |
|
| | |
| | func (state *dodataState) checkdatsize(symn sym.SymKind) { |
| | if state.datsize > cutoff { |
| | Errorf("too much data, last section %v (%d, over %v bytes)", symn, state.datsize, cutoff) |
| | } |
| | } |
| |
|
| | func checkSectSize(sect *sym.Section) { |
| | |
| | |
| | |
| | if sect.Length > cutoff { |
| | Errorf("too much data in section %s (%d, over %v bytes)", sect.Name, sect.Length, cutoff) |
| | } |
| | } |
| |
|
| | |
| | func fixZeroSizedSymbols(ctxt *Link) { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | if !(ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin) && !(ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal) { |
| | return |
| | } |
| |
|
| | ldr := ctxt.loader |
| | bss := ldr.CreateSymForUpdate("runtime.bss", 0) |
| | bss.SetSize(8) |
| | ldr.SetAttrSpecial(bss.Sym(), false) |
| |
|
| | ebss := ldr.CreateSymForUpdate("runtime.ebss", 0) |
| | ldr.SetAttrSpecial(ebss.Sym(), false) |
| |
|
| | data := ldr.CreateSymForUpdate("runtime.data", 0) |
| | data.SetSize(8) |
| | ldr.SetAttrSpecial(data.Sym(), false) |
| |
|
| | edata := ldr.CreateSymForUpdate("runtime.edata", 0) |
| | ldr.SetAttrSpecial(edata.Sym(), false) |
| |
|
| | if ctxt.HeadType == objabi.Haix { |
| | |
| | edata.SetType(sym.SXCOFFTOC) |
| | } |
| |
|
| | noptrbss := ldr.CreateSymForUpdate("runtime.noptrbss", 0) |
| | noptrbss.SetSize(8) |
| | ldr.SetAttrSpecial(noptrbss.Sym(), false) |
| |
|
| | enoptrbss := ldr.CreateSymForUpdate("runtime.enoptrbss", 0) |
| | ldr.SetAttrSpecial(enoptrbss.Sym(), false) |
| |
|
| | noptrdata := ldr.CreateSymForUpdate("runtime.noptrdata", 0) |
| | noptrdata.SetSize(8) |
| | ldr.SetAttrSpecial(noptrdata.Sym(), false) |
| |
|
| | enoptrdata := ldr.CreateSymForUpdate("runtime.enoptrdata", 0) |
| | ldr.SetAttrSpecial(enoptrdata.Sym(), false) |
| |
|
| | types := ldr.CreateSymForUpdate("runtime.types", 0) |
| | types.SetType(sym.STYPE) |
| | types.SetSize(8) |
| | ldr.SetAttrSpecial(types.Sym(), false) |
| |
|
| | etypes := ldr.CreateSymForUpdate("runtime.etypes", 0) |
| | etypes.SetType(sym.SFUNCTAB) |
| | ldr.SetAttrSpecial(etypes.Sym(), false) |
| |
|
| | if ctxt.HeadType == objabi.Haix { |
| | rodata := ldr.CreateSymForUpdate("runtime.rodata", 0) |
| | rodata.SetType(sym.SSTRING) |
| | rodata.SetSize(8) |
| | ldr.SetAttrSpecial(rodata.Sym(), false) |
| |
|
| | erodata := ldr.CreateSymForUpdate("runtime.erodata", 0) |
| | ldr.SetAttrSpecial(erodata.Sym(), false) |
| | } |
| | } |
| |
|
| | |
| | func (state *dodataState) makeRelroForSharedLib(target *Link) { |
| | if !target.UseRelro() { |
| | return |
| | } |
| |
|
| | |
| | |
| | |
| | ldr := target.loader |
| | for _, symnro := range sym.ReadOnly { |
| | symnrelro := sym.RelROMap[symnro] |
| |
|
| | ro := []loader.Sym{} |
| | relro := state.data[symnrelro] |
| |
|
| | for _, s := range state.data[symnro] { |
| | relocs := ldr.Relocs(s) |
| | isRelro := relocs.Count() > 0 |
| | switch state.symType(s) { |
| | case sym.STYPE, sym.STYPERELRO, sym.SGOFUNCRELRO: |
| | |
| | |
| | |
| | isRelro = true |
| | case sym.SFUNCTAB: |
| | if ldr.SymName(s) == "runtime.etypes" { |
| | |
| | |
| | isRelro = true |
| | } |
| | case sym.SGOFUNC, sym.SPCLNTAB: |
| | |
| | |
| | |
| | isRelro = false |
| | } |
| | if isRelro { |
| | if symnrelro == sym.Sxxx { |
| | state.ctxt.Errorf(s, "cannot contain relocations (type %v)", symnro) |
| | } |
| | state.setSymType(s, symnrelro) |
| | if outer := ldr.OuterSym(s); outer != 0 { |
| | state.setSymType(outer, symnrelro) |
| | } |
| | relro = append(relro, s) |
| | } else { |
| | ro = append(ro, s) |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | for _, s := range relro { |
| | if outer := ldr.OuterSym(s); outer != 0 { |
| | st := state.symType(s) |
| | ost := state.symType(outer) |
| | if st != ost { |
| | state.ctxt.Errorf(s, "inconsistent types for symbol and its Outer %s (%v != %v)", |
| | ldr.SymName(outer), st, ost) |
| | } |
| | } |
| | } |
| |
|
| | state.data[symnro] = ro |
| | state.data[symnrelro] = relro |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | type dodataState struct { |
| | |
| | ctxt *Link |
| | |
| | data [sym.SFirstUnallocated][]loader.Sym |
| | |
| | dataMaxAlign [sym.SFirstUnallocated]int32 |
| | |
| | symGroupType []sym.SymKind |
| | |
| | datsize int64 |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | func (state *dodataState) symType(s loader.Sym) sym.SymKind { |
| | if int(s) < len(state.symGroupType) { |
| | if override := state.symGroupType[s]; override != 0 { |
| | return override |
| | } |
| | } |
| | return state.ctxt.loader.SymType(s) |
| | } |
| |
|
| | |
| | func (state *dodataState) setSymType(s loader.Sym, kind sym.SymKind) { |
| | if s == 0 { |
| | panic("bad") |
| | } |
| | if int(s) < len(state.symGroupType) { |
| | state.symGroupType[s] = kind |
| | } else { |
| | su := state.ctxt.loader.MakeSymbolUpdater(s) |
| | su.SetType(kind) |
| | } |
| | } |
| |
|
| | func (ctxt *Link) dodata(symGroupType []sym.SymKind) { |
| |
|
| | |
| | fixZeroSizedSymbols(ctxt) |
| |
|
| | |
| | state := dodataState{ctxt: ctxt, symGroupType: symGroupType} |
| | ldr := ctxt.loader |
| | for s := loader.Sym(1); s < loader.Sym(ldr.NSym()); s++ { |
| | if !ldr.AttrReachable(s) || ldr.AttrSpecial(s) || ldr.AttrSubSymbol(s) || |
| | !ldr.TopLevelSym(s) { |
| | continue |
| | } |
| |
|
| | st := state.symType(s) |
| |
|
| | if st <= sym.STEXTEND || st >= sym.SFirstUnallocated { |
| | continue |
| | } |
| | state.data[st] = append(state.data[st], s) |
| |
|
| | |
| | if ldr.AttrOnList(s) { |
| | log.Fatalf("symbol %s listed multiple times", ldr.SymName(s)) |
| | } |
| | ldr.SetAttrOnList(s, true) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | if ctxt.HeadType == objabi.Hdarwin { |
| | machosymorder(ctxt) |
| | } |
| | state.dynreloc(ctxt) |
| |
|
| | |
| | state.makeRelroForSharedLib(ctxt) |
| |
|
| | |
| | |
| | |
| | |
| | lastSym := loader.Sym(ldr.NSym() - 1) |
| | ldr.SetSymAlign(lastSym, ldr.SymAlign(lastSym)) |
| |
|
| | |
| | var wg sync.WaitGroup |
| | for symn := range state.data { |
| | symn := sym.SymKind(symn) |
| | wg.Add(1) |
| | go func() { |
| | state.data[symn], state.dataMaxAlign[symn] = state.dodataSect(ctxt, symn, state.data[symn]) |
| | wg.Done() |
| | }() |
| | } |
| | wg.Wait() |
| |
|
| | if ctxt.IsELF { |
| | |
| | |
| | syms := state.data[sym.SELFROSECT] |
| | reli, plti := -1, -1 |
| | for i, s := range syms { |
| | switch ldr.SymName(s) { |
| | case ".rel.plt", ".rela.plt": |
| | plti = i |
| | case ".rel", ".rela": |
| | reli = i |
| | } |
| | } |
| | if reli >= 0 && plti >= 0 && plti != reli+1 { |
| | var first, second int |
| | if plti > reli { |
| | first, second = reli, plti |
| | } else { |
| | first, second = plti, reli |
| | } |
| | rel, plt := syms[reli], syms[plti] |
| | copy(syms[first+2:], syms[first+1:second]) |
| | syms[first+0] = rel |
| | syms[first+1] = plt |
| |
|
| | |
| | |
| | |
| | |
| | ldr.SetSymAlign(rel, int32(ctxt.Arch.RegSize)) |
| | ldr.SetSymAlign(plt, int32(ctxt.Arch.RegSize)) |
| | } |
| | state.data[sym.SELFROSECT] = syms |
| | } |
| |
|
| | if ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal { |
| | |
| | |
| | ldr.SetSymAlign(ldr.Lookup("runtime.data", 0), state.dataMaxAlign[sym.SDATA]) |
| | ldr.SetSymAlign(ldr.Lookup("runtime.bss", 0), state.dataMaxAlign[sym.SBSS]) |
| | } |
| |
|
| | |
| | |
| | state.allocateDataSections(ctxt) |
| |
|
| | state.allocateSEHSections(ctxt) |
| |
|
| | |
| | |
| | state.allocateDwarfSections(ctxt) |
| |
|
| | |
| | n := int16(1) |
| |
|
| | for _, sect := range Segtext.Sections { |
| | sect.Extnum = n |
| | n++ |
| | } |
| | for _, sect := range Segrodata.Sections { |
| | sect.Extnum = n |
| | n++ |
| | } |
| | for _, sect := range Segrelrodata.Sections { |
| | sect.Extnum = n |
| | n++ |
| | } |
| | for _, sect := range Segdata.Sections { |
| | sect.Extnum = n |
| | n++ |
| | } |
| | for _, sect := range Segdwarf.Sections { |
| | sect.Extnum = n |
| | n++ |
| | } |
| | for _, sect := range Segpdata.Sections { |
| | sect.Extnum = n |
| | n++ |
| | } |
| | for _, sect := range Segxdata.Sections { |
| | sect.Extnum = n |
| | n++ |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | func (state *dodataState) allocateDataSectionForSym(seg *sym.Segment, s loader.Sym, rwx int) *sym.Section { |
| | ldr := state.ctxt.loader |
| | sname := ldr.SymName(s) |
| | if strings.HasPrefix(sname, "go:") { |
| | sname = ".go." + sname[len("go:"):] |
| | } |
| | sect := addsection(ldr, state.ctxt.Arch, seg, sname, rwx) |
| | sect.Align = symalign(ldr, s) |
| | state.datsize = Rnd(state.datsize, int64(sect.Align)) |
| | sect.Vaddr = uint64(state.datsize) |
| | return sect |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | func (state *dodataState) allocateNamedDataSection(seg *sym.Segment, sName string, types []sym.SymKind, rwx int) *sym.Section { |
| | sect := addsection(state.ctxt.loader, state.ctxt.Arch, seg, sName, rwx) |
| | if len(types) == 0 { |
| | sect.Align = 1 |
| | } else if len(types) == 1 { |
| | sect.Align = state.dataMaxAlign[types[0]] |
| | } else { |
| | for _, symn := range types { |
| | align := state.dataMaxAlign[symn] |
| | if sect.Align < align { |
| | sect.Align = align |
| | } |
| | } |
| | } |
| | state.datsize = Rnd(state.datsize, int64(sect.Align)) |
| | sect.Vaddr = uint64(state.datsize) |
| | return sect |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | func (state *dodataState) assignDsymsToSection(sect *sym.Section, syms []loader.Sym, forceType sym.SymKind, aligner func(state *dodataState, datsize int64, s loader.Sym) int64) { |
| | ldr := state.ctxt.loader |
| | for _, s := range syms { |
| | state.datsize = aligner(state, state.datsize, s) |
| | ldr.SetSymSect(s, sect) |
| | if forceType != sym.Sxxx { |
| | state.setSymType(s, forceType) |
| | } |
| | ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr)) |
| | state.datsize += ldr.SymSize(s) |
| | } |
| | sect.Length = uint64(state.datsize) - sect.Vaddr |
| | } |
| |
|
| | func (state *dodataState) assignToSection(sect *sym.Section, symn sym.SymKind, forceType sym.SymKind) { |
| | state.assignDsymsToSection(sect, state.data[symn], forceType, aligndatsize) |
| | state.checkdatsize(symn) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | func (state *dodataState) allocateSingleSymSections(seg *sym.Segment, symn sym.SymKind, forceType sym.SymKind, rwx int) { |
| | ldr := state.ctxt.loader |
| | for _, s := range state.data[symn] { |
| | sect := state.allocateDataSectionForSym(seg, s, rwx) |
| | ldr.SetSymSect(s, sect) |
| | state.setSymType(s, forceType) |
| | ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr)) |
| | state.datsize += ldr.SymSize(s) |
| | sect.Length = uint64(state.datsize) - sect.Vaddr |
| | } |
| | state.checkdatsize(symn) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (state *dodataState) allocateNamedSectionAndAssignSyms(seg *sym.Segment, secName string, symn sym.SymKind, forceType sym.SymKind, rwx int) *sym.Section { |
| |
|
| | sect := state.allocateNamedDataSection(seg, secName, []sym.SymKind{symn}, rwx) |
| | state.assignDsymsToSection(sect, state.data[symn], forceType, aligndatsize) |
| | return sect |
| | } |
| |
|
| | |
| | |
| | func (state *dodataState) allocateDataSections(ctxt *Link) { |
| | |
| | |
| | |
| | |
| |
|
| | |
| | writable := []sym.SymKind{ |
| | sym.SBUILDINFO, |
| | sym.SFIPSINFO, |
| | sym.SELFSECT, |
| | sym.SMACHO, |
| | sym.SWINDOWS, |
| | } |
| | for _, symn := range writable { |
| | state.allocateSingleSymSections(&Segdata, symn, sym.SDATA, 06) |
| | } |
| | ldr := ctxt.loader |
| |
|
| | |
| | |
| | |
| | if len(state.data[sym.SMODULEDATA]) > 0 { |
| | if len(state.data[sym.SMODULEDATA]) != 1 { |
| | Errorf("internal error: more than one SMODULEDATA symbol") |
| | } |
| | s := state.data[sym.SMODULEDATA][0] |
| | sect := addsection(ldr, ctxt.Arch, &Segdata, ".go.module", 06) |
| | sect.Align = symalign(ldr, s) |
| | state.datsize = Rnd(state.datsize, int64(sect.Align)) |
| | sect.Vaddr = uint64(state.datsize) |
| | ldr.SetSymSect(s, sect) |
| | state.setSymType(s, sym.SDATA) |
| | ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr)) |
| | state.datsize += ldr.SymSize(s) |
| | sect.Length = uint64(state.datsize) - sect.Vaddr |
| | state.checkdatsize(sym.SMODULEDATA) |
| | } |
| |
|
| | |
| | if len(state.data[sym.SELFGOT]) > 0 { |
| | state.allocateNamedSectionAndAssignSyms(&Segdata, ".got", sym.SELFGOT, sym.SDATA, 06) |
| | } |
| | if len(state.data[sym.SMACHOGOT]) > 0 { |
| | state.allocateNamedSectionAndAssignSyms(&Segdata, ".got", sym.SMACHOGOT, sym.SDATA, 06) |
| | } |
| |
|
| | |
| | sect := state.allocateNamedSectionAndAssignSyms(&Segdata, ".noptrdata", sym.SNOPTRDATA, sym.SDATA, 06) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.noptrdata", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.enoptrdata", 0), sect) |
| |
|
| | state.assignToSection(sect, sym.SNOPTRDATAFIPSSTART, sym.SDATA) |
| | state.assignToSection(sect, sym.SNOPTRDATAFIPS, sym.SDATA) |
| | state.assignToSection(sect, sym.SNOPTRDATAFIPSEND, sym.SDATA) |
| | state.assignToSection(sect, sym.SNOPTRDATAEND, sym.SDATA) |
| |
|
| | hasinitarr := ctxt.linkShared |
| |
|
| | |
| | switch ctxt.BuildMode { |
| | case BuildModeCArchive, BuildModeCShared, BuildModeShared, BuildModePlugin: |
| | hasinitarr = true |
| | } |
| |
|
| | if ctxt.HeadType == objabi.Haix { |
| | if len(state.data[sym.SINITARR]) > 0 { |
| | Errorf("XCOFF format doesn't allow .init_array section") |
| | } |
| | } |
| |
|
| | if hasinitarr && len(state.data[sym.SINITARR]) > 0 { |
| | state.allocateNamedSectionAndAssignSyms(&Segdata, ".init_array", sym.SINITARR, sym.Sxxx, 06) |
| | } |
| |
|
| | |
| | sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".data", sym.SDATA, sym.SDATA, 06) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.data", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.edata", 0), sect) |
| |
|
| | state.assignToSection(sect, sym.SDATAFIPSSTART, sym.SDATA) |
| | state.assignToSection(sect, sym.SDATAFIPS, sym.SDATA) |
| | state.assignToSection(sect, sym.SDATAFIPSEND, sym.SDATA) |
| | state.assignToSection(sect, sym.SDATAEND, sym.SDATA) |
| |
|
| | dataGcEnd := state.datsize - int64(sect.Vaddr) |
| |
|
| | |
| | |
| | state.assignToSection(sect, sym.SXCOFFTOC, sym.SDATA) |
| | state.checkdatsize(sym.SDATA) |
| | sect.Length = uint64(state.datsize) - sect.Vaddr |
| |
|
| | |
| | sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".bss", sym.SBSS, sym.Sxxx, 06) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.bss", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.ebss", 0), sect) |
| | bssGcEnd := state.datsize - int64(sect.Vaddr) |
| |
|
| | |
| | gcsToEmit := []struct { |
| | symName string |
| | symKind sym.SymKind |
| | gcEnd int64 |
| | }{ |
| | {"runtime.gcdata", sym.SDATA, dataGcEnd}, |
| | {"runtime.gcbss", sym.SBSS, bssGcEnd}, |
| | } |
| | for _, g := range gcsToEmit { |
| | var gc GCProg |
| | gc.Init(ctxt, g.symName) |
| | for _, s := range state.data[g.symKind] { |
| | gc.AddSym(s) |
| | } |
| | gc.End(g.gcEnd) |
| | } |
| |
|
| | |
| | sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".noptrbss", sym.SNOPTRBSS, sym.Sxxx, 06) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.noptrbss", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.enoptrbss", 0), sect) |
| |
|
| | |
| | |
| | |
| | covCounterDataStartOff = sect.Length |
| | state.assignToSection(sect, sym.SCOVERAGE_COUNTER, sym.SNOPTRBSS) |
| | covCounterDataLen = sect.Length - covCounterDataStartOff |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.covctrs", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.ecovctrs", 0), sect) |
| |
|
| | |
| | if len(state.data[sym.SLIBFUZZER_8BIT_COUNTER]) > 0 { |
| | sect := state.allocateNamedSectionAndAssignSyms(&Segdata, ".go.fuzzcntrs", sym.SLIBFUZZER_8BIT_COUNTER, sym.Sxxx, 06) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.__start___sancov_cntrs", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.__stop___sancov_cntrs", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("internal/fuzz._counters", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("internal/fuzz._ecounters", 0), sect) |
| | } |
| |
|
| | |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.end", 0), Segdata.Sections[len(Segdata.Sections)-1]) |
| |
|
| | if len(state.data[sym.STLSBSS]) > 0 { |
| | var sect *sym.Section |
| | |
| | if (ctxt.IsELF || ctxt.HeadType == objabi.Haix) && (ctxt.LinkMode == LinkExternal || !*FlagD) { |
| | sect = addsection(ldr, ctxt.Arch, &Segdata, ".tbss", 06) |
| | sect.Align = int32(ctxt.Arch.PtrSize) |
| | |
| | sect.Vaddr = 0 |
| | } |
| | state.datsize = 0 |
| |
|
| | for _, s := range state.data[sym.STLSBSS] { |
| | state.datsize = aligndatsize(state, state.datsize, s) |
| | if sect != nil { |
| | ldr.SetSymSect(s, sect) |
| | } |
| | ldr.SetSymValue(s, state.datsize) |
| | state.datsize += ldr.SymSize(s) |
| | } |
| | state.checkdatsize(sym.STLSBSS) |
| |
|
| | if sect != nil { |
| | sect.Length = uint64(state.datsize) |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | var segro *sym.Segment |
| | if ctxt.IsELF && ctxt.LinkMode == LinkInternal { |
| | segro = &Segrodata |
| | } else if ctxt.HeadType == objabi.Hwindows { |
| | segro = &Segrodata |
| | } else { |
| | segro = &Segtext |
| | } |
| |
|
| | state.datsize = 0 |
| |
|
| | |
| | if len(state.data[sym.STEXT]) != 0 { |
| | culprit := ldr.SymName(state.data[sym.STEXT][0]) |
| | Errorf("dodata found an sym.STEXT symbol: %s", culprit) |
| | } |
| | state.allocateSingleSymSections(&Segtext, sym.SELFRXSECT, sym.SRODATA, 05) |
| | state.allocateSingleSymSections(&Segtext, sym.SMACHOPLT, sym.SRODATA, 05) |
| |
|
| | |
| | sect = state.allocateNamedDataSection(segro, ".rodata", sym.ReadOnly, 04) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.rodata", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.erodata", 0), sect) |
| | if !ctxt.UseRelro() { |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.types", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.etypes", 0), sect) |
| | } |
| | for _, symn := range sym.ReadOnly { |
| | symnStartValue := state.datsize |
| | if len(state.data[symn]) != 0 { |
| | symnStartValue = aligndatsize(state, symnStartValue, state.data[symn][0]) |
| | } |
| | state.assignToSection(sect, symn, sym.SRODATA) |
| | setCarrierSize(symn, state.datsize-symnStartValue) |
| | if ctxt.HeadType == objabi.Haix { |
| | |
| | |
| | |
| | |
| | xcoffUpdateOuterSize(ctxt, state.datsize-symnStartValue, symn) |
| | } |
| | } |
| |
|
| | |
| | sect = state.allocateNamedSectionAndAssignSyms(segro, ".gopclntab", sym.SPCLNTAB, sym.SRODATA, 04) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pclntab", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pcheader", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.funcnametab", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.cutab", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.filetab", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pctab", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.functab", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("go:func.*", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.epclntab", 0), sect) |
| | setCarrierSize(sym.SPCLNTAB, int64(sect.Length)) |
| | if ctxt.HeadType == objabi.Haix { |
| | xcoffUpdateOuterSize(ctxt, int64(sect.Length), sym.SPCLNTAB) |
| | } |
| |
|
| | |
| | state.allocateSingleSymSections(segro, sym.SELFROSECT, sym.SRODATA, 04) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | const relroPerm = 06 |
| | const fallbackPerm = 04 |
| | relroSecPerm := fallbackPerm |
| | genrelrosecname := func(suffix string) string { |
| | if suffix == "" { |
| | return ".rodata" |
| | } |
| | return suffix |
| | } |
| | seg := segro |
| |
|
| | if ctxt.UseRelro() { |
| | segrelro := &Segrelrodata |
| | if ctxt.LinkMode == LinkExternal && !ctxt.IsAIX() && !ctxt.IsDarwin() { |
| | |
| | |
| | |
| | |
| | |
| | |
| | segrelro = segro |
| | } else { |
| | |
| | state.datsize = 0 |
| | } |
| |
|
| | if !ctxt.IsDarwin() { |
| | genrelrosecname = func(suffix string) string { |
| | return ".data.rel.ro" + suffix |
| | } |
| | } |
| |
|
| | relroReadOnly := []sym.SymKind{} |
| | for _, symnro := range sym.ReadOnly { |
| | symn := sym.RelROMap[symnro] |
| | relroReadOnly = append(relroReadOnly, symn) |
| | } |
| | seg = segrelro |
| | relroSecPerm = relroPerm |
| |
|
| | |
| | sect = state.allocateNamedDataSection(segrelro, genrelrosecname(""), relroReadOnly, relroSecPerm) |
| |
|
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.types", 0), sect) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.etypes", 0), sect) |
| |
|
| | for i, symnro := range sym.ReadOnly { |
| | if i == 0 && symnro == sym.STYPE && ctxt.HeadType != objabi.Haix { |
| | |
| | |
| | |
| | |
| | state.datsize++ |
| | } |
| |
|
| | symn := sym.RelROMap[symnro] |
| | if symn == sym.Sxxx { |
| | continue |
| | } |
| | symnStartValue := state.datsize |
| | if len(state.data[symn]) != 0 { |
| | symnStartValue = aligndatsize(state, symnStartValue, state.data[symn][0]) |
| | } |
| |
|
| | for _, s := range state.data[symn] { |
| | outer := ldr.OuterSym(s) |
| | if s != 0 && ldr.SymSect(outer) != nil && ldr.SymSect(outer) != sect { |
| | ctxt.Errorf(s, "s.Outer (%s) in different section from s, %s != %s", ldr.SymName(outer), ldr.SymSect(outer).Name, sect.Name) |
| | } |
| | } |
| | state.assignToSection(sect, symn, sym.SRODATA) |
| | setCarrierSize(symn, state.datsize-symnStartValue) |
| | if ctxt.HeadType == objabi.Haix { |
| | |
| | |
| | |
| | |
| | xcoffUpdateOuterSize(ctxt, state.datsize-symnStartValue, symn) |
| | } |
| | } |
| | sect.Length = uint64(state.datsize) - sect.Vaddr |
| |
|
| | state.allocateSingleSymSections(segrelro, sym.SELFRELROSECT, sym.SRODATA, relroSecPerm) |
| | state.allocateSingleSymSections(segrelro, sym.SMACHORELROSECT, sym.SRODATA, relroSecPerm) |
| | } |
| |
|
| | |
| | sect = state.allocateNamedDataSection(seg, genrelrosecname(".typelink"), []sym.SymKind{sym.STYPELINK}, relroSecPerm) |
| |
|
| | typelink := ldr.CreateSymForUpdate("runtime.typelink", 0) |
| | ldr.SetSymSect(typelink.Sym(), sect) |
| | typelink.SetType(sym.SRODATA) |
| | state.datsize += typelink.Size() |
| | state.checkdatsize(sym.STYPELINK) |
| | sect.Length = uint64(state.datsize) - sect.Vaddr |
| |
|
| | |
| | sect = state.allocateNamedDataSection(seg, genrelrosecname(".itablink"), []sym.SymKind{sym.SITABLINK}, relroSecPerm) |
| |
|
| | itablink := ldr.CreateSymForUpdate("runtime.itablink", 0) |
| | ldr.SetSymSect(itablink.Sym(), sect) |
| | itablink.SetType(sym.SRODATA) |
| | state.datsize += itablink.Size() |
| | state.checkdatsize(sym.SITABLINK) |
| | sect.Length = uint64(state.datsize) - sect.Vaddr |
| |
|
| | |
| | if state.datsize != int64(uint32(state.datsize)) { |
| | Errorf("read-only data segment too large: %d", state.datsize) |
| | } |
| |
|
| | siz := 0 |
| | for symn := sym.SELFRXSECT; symn < sym.SFirstUnallocated; symn++ { |
| | siz += len(state.data[symn]) |
| | } |
| | ctxt.datap = make([]loader.Sym, 0, siz) |
| | for symn := sym.SELFRXSECT; symn < sym.SFirstUnallocated; symn++ { |
| | ctxt.datap = append(ctxt.datap, state.data[symn]...) |
| | } |
| | } |
| |
|
| | |
| | |
| | func (state *dodataState) allocateDwarfSections(ctxt *Link) { |
| |
|
| | alignOne := func(state *dodataState, datsize int64, s loader.Sym) int64 { return datsize } |
| |
|
| | ldr := ctxt.loader |
| | for i := 0; i < len(dwarfp); i++ { |
| | |
| | s := dwarfp[i].secSym() |
| | sect := state.allocateNamedDataSection(&Segdwarf, ldr.SymName(s), []sym.SymKind{}, 04) |
| | ldr.SetSymSect(s, sect) |
| | sect.Sym = s |
| | curType := ldr.SymType(s) |
| | state.setSymType(s, sym.SRODATA) |
| | ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr)) |
| | state.datsize += ldr.SymSize(s) |
| |
|
| | |
| | subSyms := dwarfp[i].subSyms() |
| | state.assignDsymsToSection(sect, subSyms, sym.SRODATA, alignOne) |
| |
|
| | for j := 0; j < len(subSyms); j++ { |
| | s := subSyms[j] |
| | if ctxt.HeadType == objabi.Haix && curType == sym.SDWARFLOC { |
| | |
| | |
| | addDwsectCUSize(".debug_loc", ldr.SymPkg(s), uint64(ldr.SymSize(s))) |
| | } |
| | } |
| | sect.Length = uint64(state.datsize) - sect.Vaddr |
| | checkSectSize(sect) |
| | } |
| | } |
| |
|
| | |
| | |
| | func (state *dodataState) allocateSEHSections(ctxt *Link) { |
| | if len(sehp.pdata) > 0 { |
| | sect := state.allocateNamedDataSection(&Segpdata, ".pdata", []sym.SymKind{}, 04) |
| | state.assignDsymsToSection(sect, sehp.pdata, sym.SRODATA, aligndatsize) |
| | state.checkdatsize(sym.SSEHSECT) |
| | } |
| | if len(sehp.xdata) > 0 { |
| | sect := state.allocateNamedDataSection(&Segxdata, ".xdata", []sym.SymKind{}, 04) |
| | state.assignDsymsToSection(sect, sehp.xdata, sym.SRODATA, aligndatsize) |
| | state.checkdatsize(sym.SSEHSECT) |
| | } |
| | } |
| |
|
| | type symNameSize struct { |
| | name string |
| | sz int64 |
| | val int64 |
| | sym loader.Sym |
| | } |
| |
|
| | func (state *dodataState) dodataSect(ctxt *Link, symn sym.SymKind, syms []loader.Sym) (result []loader.Sym, maxAlign int32) { |
| | var head, tail, zerobase loader.Sym |
| | ldr := ctxt.loader |
| | sl := make([]symNameSize, len(syms)) |
| |
|
| | |
| | |
| | |
| | |
| | sortBySize := symn != sym.SELFGOT |
| |
|
| | for k, s := range syms { |
| | ss := ldr.SymSize(s) |
| | sl[k] = symNameSize{sz: ss, sym: s} |
| | if !sortBySize { |
| | sl[k].name = ldr.SymName(s) |
| | } |
| | ds := int64(len(ldr.Data(s))) |
| | switch { |
| | case ss < ds: |
| | ctxt.Errorf(s, "initialize bounds (%d < %d)", ss, ds) |
| | case ss < 0: |
| | ctxt.Errorf(s, "negative size (%d bytes)", ss) |
| | case ss > cutoff: |
| | ctxt.Errorf(s, "symbol too large (%d bytes)", ss) |
| | } |
| |
|
| | |
| | |
| | |
| | if (ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin) || (ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal) { |
| | switch ldr.SymName(s) { |
| | case "runtime.text", "runtime.bss", "runtime.data", "runtime.types", "runtime.rodata", |
| | "runtime.noptrdata", "runtime.noptrbss": |
| | head = s |
| | continue |
| | case "runtime.etext", "runtime.ebss", "runtime.edata", "runtime.etypes", "runtime.erodata", |
| | "runtime.enoptrdata", "runtime.enoptrbss": |
| | tail = s |
| | continue |
| | } |
| | } |
| | } |
| | zerobase = ldr.Lookup("runtime.zerobase", 0) |
| |
|
| | |
| | if symn != sym.SPCLNTAB { |
| | sort.Slice(sl, func(i, j int) bool { |
| | si, sj := sl[i].sym, sl[j].sym |
| | isz, jsz := sl[i].sz, sl[j].sz |
| | switch { |
| | case si == head, sj == tail: |
| | return true |
| | case sj == head, si == tail: |
| | return false |
| | } |
| | if sortBySize { |
| | switch { |
| | |
| | |
| | case si == zerobase: |
| | return jsz != 0 |
| | case sj == zerobase: |
| | return isz == 0 |
| | case isz != jsz: |
| | return isz < jsz |
| | } |
| | } else { |
| | iname := sl[i].name |
| | jname := sl[j].name |
| | if iname != jname { |
| | return iname < jname |
| | } |
| | } |
| | return si < sj |
| | }) |
| | } else { |
| | |
| | } |
| |
|
| | |
| | syms = syms[:0] |
| | for k := range sl { |
| | s := sl[k].sym |
| | if s != head && s != tail { |
| | align := symalign(ldr, s) |
| | if maxAlign < align { |
| | maxAlign = align |
| | } |
| | } |
| | syms = append(syms, s) |
| | } |
| |
|
| | return syms, maxAlign |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | func (ctxt *Link) textbuildid() { |
| | if ctxt.IsELF || *flagBuildid == "" { |
| | return |
| | } |
| |
|
| | ldr := ctxt.loader |
| | s := ldr.CreateSymForUpdate("go:buildid", 0) |
| | |
| | |
| | data := "\xff Go build ID: " + strconv.Quote(*flagBuildid) + "\n \xff" |
| | s.SetType(sym.STEXT) |
| | s.SetData([]byte(data)) |
| | s.SetSize(int64(len(data))) |
| |
|
| | ctxt.Textp = append(ctxt.Textp, 0) |
| | copy(ctxt.Textp[1:], ctxt.Textp) |
| | ctxt.Textp[0] = s.Sym() |
| | } |
| |
|
| | func (ctxt *Link) buildinfo() { |
| | |
| | |
| | ldr := ctxt.loader |
| | s := ldr.CreateSymForUpdate("go:buildinfo", 0) |
| | s.SetType(sym.SBUILDINFO) |
| | s.SetAlign(16) |
| |
|
| | |
| | |
| | const prefix = "\xff Go buildinf:" |
| |
|
| | |
| | |
| | data := make([]byte, 32) |
| | copy(data, prefix) |
| | data[len(prefix)] = byte(ctxt.Arch.PtrSize) |
| | data[len(prefix)+1] = 0 |
| | if ctxt.Arch.ByteOrder == binary.BigEndian { |
| | data[len(prefix)+1] = 1 |
| | } |
| | data[len(prefix)+1] |= 2 |
| | data = appendString(data, strdata["runtime.buildVersion"]) |
| | data = appendString(data, strdata["runtime.modinfo"]) |
| | |
| | for len(data)%16 != 0 { |
| | data = append(data, 0) |
| | } |
| | s.SetData(data) |
| | s.SetSize(int64(len(data))) |
| |
|
| | |
| | |
| | |
| | sr := ldr.CreateSymForUpdate("go:buildinfo.ref", 0) |
| | sr.SetType(sym.SRODATA) |
| | sr.SetAlign(int32(ctxt.Arch.PtrSize)) |
| | sr.AddAddr(ctxt.Arch, s.Sym()) |
| | } |
| |
|
| | |
| | func appendString(data []byte, s string) []byte { |
| | var v [binary.MaxVarintLen64]byte |
| | n := binary.PutUvarint(v[:], uint64(len(s))) |
| | data = append(data, v[:n]...) |
| | data = append(data, s...) |
| | return data |
| | } |
| |
|
| | |
| | func (ctxt *Link) textaddress() { |
| | addsection(ctxt.loader, ctxt.Arch, &Segtext, ".text", 05) |
| |
|
| | |
| | |
| | |
| | sect := Segtext.Sections[0] |
| |
|
| | sect.Align = int32(Funcalign) |
| |
|
| | ldr := ctxt.loader |
| |
|
| | if *flagRandLayout != 0 { |
| | r := rand.New(rand.NewSource(*flagRandLayout)) |
| | textp := ctxt.Textp |
| | i := 0 |
| | |
| | if len(textp) > 0 && ldr.SymName(textp[0]) == "go:buildid" { |
| | i++ |
| | } |
| | |
| | |
| | |
| | for i < len(textp) && (ldr.SubSym(textp[i]) != 0 || ldr.AttrSubSymbol(textp[i])) { |
| | i++ |
| | } |
| | textp = textp[i:] |
| | r.Shuffle(len(textp), func(i, j int) { |
| | textp[i], textp[j] = textp[j], textp[i] |
| | }) |
| | } |
| |
|
| | |
| | |
| | |
| | sort.SliceStable(ctxt.Textp, func(i, j int) bool { |
| | return ldr.SymType(ctxt.Textp[i]) < ldr.SymType(ctxt.Textp[j]) |
| | }) |
| |
|
| | text := ctxt.xdefine("runtime.text", sym.STEXT, 0) |
| | etext := ctxt.xdefine("runtime.etext", sym.STEXTEND, 0) |
| | ldr.SetSymSect(text, sect) |
| | if ctxt.IsAIX() && ctxt.IsExternal() { |
| | |
| | |
| | |
| | u := ldr.MakeSymbolUpdater(text) |
| | u.SetAlign(sect.Align) |
| | u.SetSize(8) |
| | } |
| |
|
| | if (ctxt.DynlinkingGo() && ctxt.IsDarwin()) || (ctxt.IsAIX() && ctxt.IsExternal()) { |
| | ldr.SetSymSect(etext, sect) |
| | ctxt.Textp = append(ctxt.Textp, etext, 0) |
| | copy(ctxt.Textp[1:], ctxt.Textp) |
| | ctxt.Textp[0] = text |
| | } |
| |
|
| | start := uint64(Rnd(*FlagTextAddr, int64(Funcalign))) |
| | va := start |
| | n := 1 |
| | sect.Vaddr = va |
| |
|
| | limit := thearch.TrampLimit |
| | if limit == 0 { |
| | limit = 1 << 63 |
| | } |
| | if *FlagDebugTextSize != 0 { |
| | limit = uint64(*FlagDebugTextSize) |
| | } |
| | if *FlagDebugTramp > 1 { |
| | limit = 1 |
| | } |
| |
|
| | if ctxt.IsAIX() && ctxt.IsExternal() { |
| | |
| | |
| | |
| | |
| | |
| | limit = 1 |
| | } |
| |
|
| | |
| | |
| | big := false |
| | for _, s := range ctxt.Textp { |
| | sect, n, va = assignAddress(ctxt, sect, n, s, va, false, big) |
| | if va-start >= limit { |
| | big = true |
| | break |
| | } |
| | } |
| |
|
| | |
| | |
| | if big { |
| | |
| | for _, s := range ctxt.Textp { |
| | if s != text { |
| | resetAddress(ctxt, s) |
| | } |
| | } |
| | va = start |
| |
|
| | ntramps := 0 |
| | var curPkg string |
| | for i, s := range ctxt.Textp { |
| | |
| | |
| | |
| | |
| | |
| | if symPkg := ldr.SymPkg(s); symPkg != "" && curPkg != symPkg { |
| | curPkg = symPkg |
| | vaTmp := va |
| | for j := i; j < len(ctxt.Textp); j++ { |
| | curSym := ctxt.Textp[j] |
| | if symPkg := ldr.SymPkg(curSym); symPkg == "" || curPkg != symPkg { |
| | break |
| | } |
| | |
| | |
| | sect, n, vaTmp = assignAddress(ctxt, sect, n, curSym, vaTmp, false, false) |
| | vaTmp += maxSizeTrampolines(ctxt, ldr, curSym, false) |
| | } |
| | } |
| |
|
| | |
| | if s != text { |
| | resetAddress(ctxt, s) |
| | } |
| |
|
| | |
| | sect, n, va = assignAddress(ctxt, sect, n, s, va, false, big) |
| |
|
| | |
| | trampoline(ctxt, s) |
| |
|
| | |
| | for ; ntramps < len(ctxt.tramps); ntramps++ { |
| | tramp := ctxt.tramps[ntramps] |
| | if ctxt.IsAIX() && strings.HasPrefix(ldr.SymName(tramp), "runtime.text.") { |
| | |
| | continue |
| | } |
| | sect, n, va = assignAddress(ctxt, sect, n, tramp, va, true, big) |
| | } |
| | } |
| |
|
| | |
| | if ntramps != 0 { |
| | newtextp := make([]loader.Sym, 0, len(ctxt.Textp)+ntramps) |
| | i := 0 |
| | for _, s := range ctxt.Textp { |
| | for ; i < ntramps && ldr.SymValue(ctxt.tramps[i]) < ldr.SymValue(s); i++ { |
| | newtextp = append(newtextp, ctxt.tramps[i]) |
| | } |
| | newtextp = append(newtextp, s) |
| | } |
| | newtextp = append(newtextp, ctxt.tramps[i:ntramps]...) |
| |
|
| | ctxt.Textp = newtextp |
| | } |
| | } |
| |
|
| | |
| | |
| | sect.Length = va - sect.Vaddr + uint64(ctxt.Arch.MinLC) |
| | ldr.SetSymSect(etext, sect) |
| | if ldr.SymValue(etext) == 0 { |
| | |
| | |
| | ldr.SetSymValue(etext, int64(va)) |
| | ldr.SetSymValue(text, int64(Segtext.Sections[0].Vaddr)) |
| | } |
| | } |
| |
|
| | |
| | func assignAddress(ctxt *Link, sect *sym.Section, n int, s loader.Sym, va uint64, isTramp, big bool) (*sym.Section, int, uint64) { |
| | ldr := ctxt.loader |
| | if thearch.AssignAddress != nil { |
| | return thearch.AssignAddress(ldr, sect, n, s, va, isTramp) |
| | } |
| |
|
| | ldr.SetSymSect(s, sect) |
| | if ldr.AttrSubSymbol(s) { |
| | return sect, n, va |
| | } |
| |
|
| | align := ldr.SymAlign(s) |
| | align = max(align, int32(Funcalign)) |
| | va = uint64(Rnd(int64(va), int64(align))) |
| | if sect.Align < align { |
| | sect.Align = align |
| | } |
| |
|
| | funcsize := uint64(abi.MINFUNC) |
| | if ldr.SymSize(s) > abi.MINFUNC { |
| | funcsize = uint64(ldr.SymSize(s)) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | if big && splitTextSections(ctxt) && ldr.OuterSym(s) == 0 { |
| | |
| | |
| | var textSizelimit uint64 = thearch.TrampLimit |
| | if *FlagDebugTextSize != 0 { |
| | textSizelimit = uint64(*FlagDebugTextSize) |
| | } |
| |
|
| | |
| | |
| | if funcsize > textSizelimit { |
| | panic(fmt.Sprintf("error: text size limit %d less than text symbol %s size of %d", textSizelimit, ldr.SymName(s), funcsize)) |
| | } |
| |
|
| | if va-sect.Vaddr+funcsize+maxSizeTrampolines(ctxt, ldr, s, isTramp) > textSizelimit { |
| | sectAlign := int32(thearch.Funcalign) |
| | if ctxt.IsPPC64() { |
| | |
| | |
| | |
| | |
| | |
| | |
| | const ppc64maxFuncalign = 64 |
| | sectAlign = ppc64maxFuncalign |
| | va = uint64(Rnd(int64(va), ppc64maxFuncalign)) |
| | } |
| |
|
| | |
| | sect.Length = va - sect.Vaddr |
| |
|
| | |
| | sect = addsection(ctxt.loader, ctxt.Arch, &Segtext, ".text", 05) |
| |
|
| | sect.Vaddr = va |
| | sect.Align = sectAlign |
| | ldr.SetSymSect(s, sect) |
| |
|
| | |
| | ntext := ldr.CreateSymForUpdate(fmt.Sprintf("runtime.text.%d", n), 0) |
| | ntext.SetSect(sect) |
| | if ctxt.IsAIX() { |
| | |
| | |
| | |
| | ntext.SetType(sym.STEXT) |
| | ntext.SetSize(int64(abi.MINFUNC)) |
| | ntext.SetOnList(true) |
| | ntext.SetAlign(sectAlign) |
| | ctxt.tramps = append(ctxt.tramps, ntext.Sym()) |
| |
|
| | ntext.SetValue(int64(va)) |
| | va += uint64(ntext.Size()) |
| |
|
| | if align := ldr.SymAlign(s); align != 0 { |
| | va = uint64(Rnd(int64(va), int64(align))) |
| | } else { |
| | va = uint64(Rnd(int64(va), int64(Funcalign))) |
| | } |
| | } |
| | n++ |
| | } |
| | } |
| |
|
| | ldr.SetSymValue(s, 0) |
| | for sub := s; sub != 0; sub = ldr.SubSym(sub) { |
| | ldr.SetSymValue(sub, ldr.SymValue(sub)+int64(va)) |
| | if ctxt.Debugvlog > 2 { |
| | fmt.Println("assign text address:", ldr.SymName(sub), ldr.SymValue(sub)) |
| | } |
| | } |
| |
|
| | va += funcsize |
| |
|
| | return sect, n, va |
| | } |
| |
|
| | func resetAddress(ctxt *Link, s loader.Sym) { |
| | ldr := ctxt.loader |
| | if ldr.OuterSym(s) != 0 { |
| | return |
| | } |
| | oldv := ldr.SymValue(s) |
| | for sub := s; sub != 0; sub = ldr.SubSym(sub) { |
| | ldr.SetSymValue(sub, ldr.SymValue(sub)-oldv) |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func splitTextSections(ctxt *Link) bool { |
| | return (ctxt.IsARM() || ctxt.IsPPC64() || (ctxt.IsARM64() && ctxt.IsDarwin())) && ctxt.IsExternal() |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | const wasmMinDataAddr = 4096 + 8192 |
| |
|
| | |
| | |
| | func (ctxt *Link) address() []*sym.Segment { |
| | var order []*sym.Segment |
| |
|
| | va := uint64(*FlagTextAddr) |
| | order = append(order, &Segtext) |
| | Segtext.Rwx = 05 |
| | Segtext.Vaddr = va |
| | for i, s := range Segtext.Sections { |
| | va = uint64(Rnd(int64(va), int64(s.Align))) |
| | s.Vaddr = va |
| | va += s.Length |
| |
|
| | if ctxt.IsWasm() && i == 0 && va < wasmMinDataAddr { |
| | va = wasmMinDataAddr |
| | } |
| | } |
| |
|
| | Segtext.Length = va - uint64(*FlagTextAddr) |
| |
|
| | if len(Segrodata.Sections) > 0 { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | va = uint64(Rnd(int64(va), *FlagRound)) |
| |
|
| | order = append(order, &Segrodata) |
| | Segrodata.Rwx = 04 |
| | Segrodata.Vaddr = va |
| | for _, s := range Segrodata.Sections { |
| | va = uint64(Rnd(int64(va), int64(s.Align))) |
| | s.Vaddr = va |
| | va += s.Length |
| | } |
| |
|
| | Segrodata.Length = va - Segrodata.Vaddr |
| | } |
| | if len(Segrelrodata.Sections) > 0 { |
| | |
| | |
| | va = uint64(Rnd(int64(va), *FlagRound)) |
| | if ctxt.HeadType == objabi.Haix { |
| | |
| | va += uint64(XCOFFDATABASE) - uint64(XCOFFTEXTBASE) |
| | } |
| |
|
| | order = append(order, &Segrelrodata) |
| | Segrelrodata.Rwx = 06 |
| | Segrelrodata.Vaddr = va |
| | for _, s := range Segrelrodata.Sections { |
| | va = uint64(Rnd(int64(va), int64(s.Align))) |
| | s.Vaddr = va |
| | va += s.Length |
| | } |
| |
|
| | Segrelrodata.Length = va - Segrelrodata.Vaddr |
| | } |
| |
|
| | va = uint64(Rnd(int64(va), *FlagRound)) |
| | if ctxt.HeadType == objabi.Haix && len(Segrelrodata.Sections) == 0 { |
| | |
| | |
| | |
| | va += uint64(XCOFFDATABASE) - uint64(XCOFFTEXTBASE) |
| | } |
| | order = append(order, &Segdata) |
| | Segdata.Rwx = 06 |
| | if *FlagDataAddr != -1 { |
| | Segdata.Vaddr = uint64(*FlagDataAddr) |
| | va = Segdata.Vaddr |
| | } else { |
| | Segdata.Vaddr = va |
| | } |
| | var data *sym.Section |
| | var noptr *sym.Section |
| | var bss *sym.Section |
| | var noptrbss *sym.Section |
| | var fuzzCounters *sym.Section |
| | for i, s := range Segdata.Sections { |
| | if (ctxt.IsELF || ctxt.HeadType == objabi.Haix) && s.Name == ".tbss" { |
| | continue |
| | } |
| | vlen := int64(s.Length) |
| | if i+1 < len(Segdata.Sections) && !((ctxt.IsELF || ctxt.HeadType == objabi.Haix) && Segdata.Sections[i+1].Name == ".tbss") { |
| | vlen = int64(Segdata.Sections[i+1].Vaddr - s.Vaddr) |
| | } |
| | s.Vaddr = va |
| | va += uint64(vlen) |
| | Segdata.Length = va - Segdata.Vaddr |
| | switch s.Name { |
| | case ".data": |
| | data = s |
| | case ".noptrdata": |
| | noptr = s |
| | case ".bss": |
| | bss = s |
| | case ".noptrbss": |
| | noptrbss = s |
| | case ".go.fuzzcntrs": |
| | fuzzCounters = s |
| | } |
| | } |
| |
|
| | |
| | |
| | Segdata.Filelen = bss.Vaddr - Segdata.Vaddr |
| |
|
| | if len(Segpdata.Sections) > 0 { |
| | va = uint64(Rnd(int64(va), *FlagRound)) |
| | order = append(order, &Segpdata) |
| | Segpdata.Rwx = 04 |
| | Segpdata.Vaddr = va |
| | |
| | |
| | for _, s := range Segpdata.Sections { |
| | va = uint64(Rnd(int64(va), int64(s.Align))) |
| | s.Vaddr = va |
| | va += s.Length |
| | } |
| | Segpdata.Length = va - Segpdata.Vaddr |
| | } |
| |
|
| | if len(Segxdata.Sections) > 0 { |
| | va = uint64(Rnd(int64(va), *FlagRound)) |
| | order = append(order, &Segxdata) |
| | Segxdata.Rwx = 04 |
| | Segxdata.Vaddr = va |
| | |
| | |
| | for _, s := range Segxdata.Sections { |
| | va = uint64(Rnd(int64(va), int64(s.Align))) |
| | s.Vaddr = va |
| | va += s.Length |
| | } |
| | Segxdata.Length = va - Segxdata.Vaddr |
| | } |
| |
|
| | va = uint64(Rnd(int64(va), *FlagRound)) |
| | order = append(order, &Segdwarf) |
| | Segdwarf.Rwx = 06 |
| | Segdwarf.Vaddr = va |
| | for i, s := range Segdwarf.Sections { |
| | vlen := int64(s.Length) |
| | if i+1 < len(Segdwarf.Sections) { |
| | vlen = int64(Segdwarf.Sections[i+1].Vaddr - s.Vaddr) |
| | } |
| | s.Vaddr = va |
| | va += uint64(vlen) |
| | if ctxt.HeadType == objabi.Hwindows { |
| | va = uint64(Rnd(int64(va), PEFILEALIGN)) |
| | } |
| | Segdwarf.Length = va - Segdwarf.Vaddr |
| | } |
| |
|
| | ldr := ctxt.loader |
| | var ( |
| | rodata = ldr.SymSect(ldr.LookupOrCreateSym("runtime.rodata", 0)) |
| | pclntab = ldr.SymSect(ldr.LookupOrCreateSym("runtime.pclntab", 0)) |
| | types = ldr.SymSect(ldr.LookupOrCreateSym("runtime.types", 0)) |
| | ) |
| |
|
| | for _, s := range ctxt.datap { |
| | if sect := ldr.SymSect(s); sect != nil { |
| | ldr.AddToSymValue(s, int64(sect.Vaddr)) |
| | } |
| | v := ldr.SymValue(s) |
| | for sub := ldr.SubSym(s); sub != 0; sub = ldr.SubSym(sub) { |
| | ldr.AddToSymValue(sub, v) |
| | } |
| | } |
| |
|
| | for _, si := range dwarfp { |
| | for _, s := range si.syms { |
| | if sect := ldr.SymSect(s); sect != nil { |
| | ldr.AddToSymValue(s, int64(sect.Vaddr)) |
| | } |
| | sub := ldr.SubSym(s) |
| | if sub != 0 { |
| | panic(fmt.Sprintf("unexpected sub-sym for %s %s", ldr.SymName(s), ldr.SymType(s).String())) |
| | } |
| | v := ldr.SymValue(s) |
| | for ; sub != 0; sub = ldr.SubSym(sub) { |
| | ldr.AddToSymValue(s, v) |
| | } |
| | } |
| | } |
| |
|
| | for _, s := range sehp.pdata { |
| | if sect := ldr.SymSect(s); sect != nil { |
| | ldr.AddToSymValue(s, int64(sect.Vaddr)) |
| | } |
| | } |
| | for _, s := range sehp.xdata { |
| | if sect := ldr.SymSect(s); sect != nil { |
| | ldr.AddToSymValue(s, int64(sect.Vaddr)) |
| | } |
| | } |
| |
|
| | if ctxt.BuildMode == BuildModeShared { |
| | s := ldr.LookupOrCreateSym("go:link.abihashbytes", 0) |
| | sect := ldr.SymSect(ldr.LookupOrCreateSym(".note.go.abihash", 0)) |
| | ldr.SetSymSect(s, sect) |
| | ldr.SetSymValue(s, int64(sect.Vaddr+16)) |
| | } |
| |
|
| | |
| | |
| | n := 1 |
| | for _, sect := range Segtext.Sections[1:] { |
| | if sect.Name != ".text" { |
| | break |
| | } |
| | symname := fmt.Sprintf("runtime.text.%d", n) |
| | if ctxt.HeadType != objabi.Haix || ctxt.LinkMode != LinkExternal { |
| | |
| | |
| | ctxt.xdefine(symname, sym.STEXT, int64(sect.Vaddr)) |
| | } |
| | n++ |
| | } |
| |
|
| | ctxt.xdefine("runtime.rodata", sym.SRODATA, int64(rodata.Vaddr)) |
| | ctxt.xdefine("runtime.erodata", sym.SRODATA, int64(rodata.Vaddr+rodata.Length)) |
| | ctxt.xdefine("runtime.types", sym.SRODATA, int64(types.Vaddr)) |
| | ctxt.xdefine("runtime.etypes", sym.SRODATA, int64(types.Vaddr+types.Length)) |
| |
|
| | s := ldr.Lookup("runtime.gcdata", 0) |
| | ldr.SetAttrLocal(s, true) |
| | ctxt.xdefine("runtime.egcdata", sym.SRODATA, ldr.SymAddr(s)+ldr.SymSize(s)) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.egcdata", 0), ldr.SymSect(s)) |
| |
|
| | s = ldr.LookupOrCreateSym("runtime.gcbss", 0) |
| | ldr.SetAttrLocal(s, true) |
| | ctxt.xdefine("runtime.egcbss", sym.SRODATA, ldr.SymAddr(s)+ldr.SymSize(s)) |
| | ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.egcbss", 0), ldr.SymSect(s)) |
| |
|
| | ctxt.xdefine("runtime.pclntab", sym.SRODATA, int64(pclntab.Vaddr)) |
| | ctxt.defineInternal("runtime.pcheader", sym.SRODATA) |
| | ctxt.defineInternal("runtime.funcnametab", sym.SRODATA) |
| | ctxt.defineInternal("runtime.cutab", sym.SRODATA) |
| | ctxt.defineInternal("runtime.filetab", sym.SRODATA) |
| | ctxt.defineInternal("runtime.pctab", sym.SRODATA) |
| | ctxt.defineInternal("runtime.functab", sym.SRODATA) |
| | ctxt.defineInternal("go:func.*", sym.SRODATA) |
| | ctxt.xdefine("runtime.epclntab", sym.SRODATA, int64(pclntab.Vaddr+pclntab.Length)) |
| | ctxt.xdefine("runtime.noptrdata", sym.SNOPTRDATA, int64(noptr.Vaddr)) |
| | ctxt.xdefine("runtime.enoptrdata", sym.SNOPTRDATAEND, int64(noptr.Vaddr+noptr.Length)) |
| | ctxt.xdefine("runtime.bss", sym.SBSS, int64(bss.Vaddr)) |
| | ctxt.xdefine("runtime.ebss", sym.SBSS, int64(bss.Vaddr+bss.Length)) |
| | ctxt.xdefine("runtime.data", sym.SDATA, int64(data.Vaddr)) |
| | ctxt.xdefine("runtime.edata", sym.SDATAEND, int64(data.Vaddr+data.Length)) |
| | ctxt.xdefine("runtime.noptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr)) |
| | ctxt.xdefine("runtime.enoptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr+noptrbss.Length)) |
| | ctxt.xdefine("runtime.covctrs", sym.SCOVERAGE_COUNTER, int64(noptrbss.Vaddr+covCounterDataStartOff)) |
| | ctxt.xdefine("runtime.ecovctrs", sym.SCOVERAGE_COUNTER, int64(noptrbss.Vaddr+covCounterDataStartOff+covCounterDataLen)) |
| | ctxt.xdefine("runtime.end", sym.SBSS, int64(Segdata.Vaddr+Segdata.Length)) |
| |
|
| | if fuzzCounters != nil { |
| | if *flagAsan { |
| | |
| | |
| | |
| | fuzzCounters.Length = uint64(Rnd(int64(fuzzCounters.Length), 8)) |
| | } |
| | ctxt.xdefine("runtime.__start___sancov_cntrs", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr)) |
| | ctxt.xdefine("runtime.__stop___sancov_cntrs", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr+fuzzCounters.Length)) |
| | ctxt.xdefine("internal/fuzz._counters", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr)) |
| | ctxt.xdefine("internal/fuzz._ecounters", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr+fuzzCounters.Length)) |
| | } |
| |
|
| | if ctxt.IsSolaris() { |
| | |
| | |
| | |
| | etext := ldr.Lookup("runtime.etext", 0) |
| | edata := ldr.Lookup("runtime.edata", 0) |
| | end := ldr.Lookup("runtime.end", 0) |
| | ldr.SetSymExtname(etext, "runtime.etext") |
| | ldr.SetSymExtname(edata, "runtime.edata") |
| | ldr.SetSymExtname(end, "runtime.end") |
| | ctxt.xdefine("_etext", ldr.SymType(etext), ldr.SymValue(etext)) |
| | ctxt.xdefine("_edata", ldr.SymType(edata), ldr.SymValue(edata)) |
| | ctxt.xdefine("_end", ldr.SymType(end), ldr.SymValue(end)) |
| | ldr.SetSymSect(ldr.Lookup("_etext", 0), ldr.SymSect(etext)) |
| | ldr.SetSymSect(ldr.Lookup("_edata", 0), ldr.SymSect(edata)) |
| | ldr.SetSymSect(ldr.Lookup("_end", 0), ldr.SymSect(end)) |
| | } |
| |
|
| | if ctxt.IsPPC64() && ctxt.IsElf() { |
| | |
| | |
| | |
| | tocAddr := int64(Segdata.Vaddr) + 0x8000 |
| | if gotAddr := ldr.SymValue(ctxt.GOT); gotAddr != 0 { |
| | tocAddr = gotAddr + 0x8000 |
| | } |
| | for i := range ctxt.DotTOC { |
| | if i >= sym.SymVerABICount && i < sym.SymVerStatic { |
| | continue |
| | } |
| | if toc := ldr.Lookup(".TOC.", i); toc != 0 { |
| | ldr.SetSymValue(toc, tocAddr) |
| | } |
| | } |
| | } |
| |
|
| | return order |
| | } |
| |
|
| | |
| | |
| | func (ctxt *Link) layout(order []*sym.Segment) uint64 { |
| | var prev *sym.Segment |
| | for _, seg := range order { |
| | if prev == nil { |
| | seg.Fileoff = uint64(HEADR) |
| | } else { |
| | switch ctxt.HeadType { |
| | default: |
| | |
| | |
| | |
| | |
| | seg.Fileoff = uint64(Rnd(int64(prev.Fileoff+prev.Filelen), *FlagRound)) |
| | if seg.Vaddr%uint64(*FlagRound) != seg.Fileoff%uint64(*FlagRound) { |
| | Exitf("bad segment rounding (Vaddr=%#x Fileoff=%#x FlagRound=%#x)", seg.Vaddr, seg.Fileoff, *FlagRound) |
| | } |
| | case objabi.Hwindows: |
| | seg.Fileoff = prev.Fileoff + uint64(Rnd(int64(prev.Filelen), PEFILEALIGN)) |
| | case objabi.Hplan9: |
| | seg.Fileoff = prev.Fileoff + prev.Filelen |
| | } |
| | } |
| | if seg != &Segdata { |
| | |
| | |
| | seg.Filelen = seg.Length |
| | } |
| | prev = seg |
| | } |
| | return prev.Fileoff + prev.Filelen |
| | } |
| |
|
| | |
| | func (ctxt *Link) AddTramp(s *loader.SymbolBuilder, typ sym.SymKind) { |
| | s.SetType(typ) |
| | s.SetReachable(true) |
| | s.SetOnList(true) |
| | ctxt.tramps = append(ctxt.tramps, s.Sym()) |
| | if *FlagDebugTramp > 0 && ctxt.Debugvlog > 0 { |
| | ctxt.Logf("trampoline %s inserted\n", s.Name()) |
| | } |
| | } |
| |
|
| | |
| | |
| | func compressSyms(ctxt *Link, syms []loader.Sym) []byte { |
| | ldr := ctxt.loader |
| | var total int64 |
| | for _, sym := range syms { |
| | total += ldr.SymSize(sym) |
| | } |
| |
|
| | var buf bytes.Buffer |
| | if ctxt.IsELF { |
| | switch ctxt.Arch.PtrSize { |
| | case 8: |
| | binary.Write(&buf, ctxt.Arch.ByteOrder, elf.Chdr64{ |
| | Type: uint32(elf.COMPRESS_ZLIB), |
| | Size: uint64(total), |
| | Addralign: uint64(ctxt.Arch.Alignment), |
| | }) |
| | case 4: |
| | binary.Write(&buf, ctxt.Arch.ByteOrder, elf.Chdr32{ |
| | Type: uint32(elf.COMPRESS_ZLIB), |
| | Size: uint32(total), |
| | Addralign: uint32(ctxt.Arch.Alignment), |
| | }) |
| | default: |
| | log.Fatalf("can't compress header size:%d", ctxt.Arch.PtrSize) |
| | } |
| | } else { |
| | buf.Write([]byte("ZLIB")) |
| | var sizeBytes [8]byte |
| | binary.BigEndian.PutUint64(sizeBytes[:], uint64(total)) |
| | buf.Write(sizeBytes[:]) |
| | } |
| |
|
| | var relocbuf []byte |
| |
|
| | |
| | |
| | |
| | |
| | z, err := zlib.NewWriterLevel(&buf, zlib.BestSpeed) |
| | if err != nil { |
| | log.Fatalf("NewWriterLevel failed: %s", err) |
| | } |
| | st := ctxt.makeRelocSymState() |
| | for _, s := range syms { |
| | |
| | |
| | P := ldr.Data(s) |
| | relocs := ldr.Relocs(s) |
| | if relocs.Count() != 0 { |
| | relocbuf = append(relocbuf[:0], P...) |
| | P = relocbuf |
| | st.relocsym(s, P) |
| | } |
| | if _, err := z.Write(P); err != nil { |
| | log.Fatalf("compression failed: %s", err) |
| | } |
| | for i := ldr.SymSize(s) - int64(len(P)); i > 0; { |
| | b := zeros[:] |
| | if i < int64(len(b)) { |
| | b = b[:i] |
| | } |
| | n, err := z.Write(b) |
| | if err != nil { |
| | log.Fatalf("compression failed: %s", err) |
| | } |
| | i -= int64(n) |
| | } |
| | } |
| | if err := z.Close(); err != nil { |
| | log.Fatalf("compression failed: %s", err) |
| | } |
| | if int64(buf.Len()) >= total { |
| | |
| | return nil |
| | } |
| | return buf.Bytes() |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func writeUleb128FixedLength(b []byte, v uint64, length int) error { |
| | for i := 0; i < length; i++ { |
| | c := uint8(v & 0x7f) |
| | v >>= 7 |
| | if i < length-1 { |
| | c |= 0x80 |
| | } |
| | b[i] = c |
| | } |
| | if v != 0 { |
| | return fmt.Errorf("writeUleb128FixedLength: length too small") |
| | } |
| | return nil |
| | } |
| |
|