]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/link/internal/loader/symbolbuilder.go
cmd/link/internal/loader: remove some dead code
[gostls13.git] / src / cmd / link / internal / loader / symbolbuilder.go
index 7c8115f0b9fdf5e425f512ae2c98a957f7c15caa..b9eaca7fb6fd61c9e41b6d5d1b90220dbb1831ea 100644 (file)
@@ -5,7 +5,7 @@
 package loader
 
 import (
-       "cmd/internal/goobj2"
+       "cmd/internal/goobj"
        "cmd/internal/objabi"
        "cmd/internal/sys"
        "cmd/link/internal/sym"
@@ -118,29 +118,19 @@ func (sb *SymbolBuilder) Relocs() Relocs {
        return sb.l.Relocs(sb.symIdx)
 }
 
-func (sb *SymbolBuilder) SetRelocs(rslice []Reloc) {
-       n := len(rslice)
-       if cap(sb.relocs) < n {
-               sb.relocs = make([]goobj2.Reloc, n)
-               sb.reltypes = make([]objabi.RelocType, n)
-       } else {
-               sb.relocs = sb.relocs[:n]
-               sb.reltypes = sb.reltypes[:n]
-       }
-       for i := range rslice {
-               sb.SetReloc(i, rslice[i])
-       }
+// ResetRelocs removes all relocations on this symbol.
+func (sb *SymbolBuilder) ResetRelocs() {
+       sb.relocs = sb.relocs[:0]
 }
 
 // SetRelocType sets the type of the 'i'-th relocation on this sym to 't'
 func (sb *SymbolBuilder) SetRelocType(i int, t objabi.RelocType) {
-       sb.relocs[i].SetType(0)
-       sb.reltypes[i] = t
+       sb.relocs[i].SetType(uint16(t))
 }
 
 // SetRelocSym sets the target sym of the 'i'-th relocation on this sym to 's'
 func (sb *SymbolBuilder) SetRelocSym(i int, tgt Sym) {
-       sb.relocs[i].SetSym(goobj2.SymRef{PkgIdx: 0, SymIdx: uint32(tgt)})
+       sb.relocs[i].SetSym(goobj.SymRef{PkgIdx: 0, SymIdx: uint32(tgt)})
 }
 
 // SetRelocAdd sets the addend of the 'i'-th relocation on this sym to 'a'
@@ -150,19 +140,18 @@ func (sb *SymbolBuilder) SetRelocAdd(i int, a int64) {
 
 // Add n relocations, return a handle to the relocations.
 func (sb *SymbolBuilder) AddRelocs(n int) Relocs {
-       sb.relocs = append(sb.relocs, make([]goobj2.Reloc, n)...)
-       sb.reltypes = append(sb.reltypes, make([]objabi.RelocType, n)...)
+       sb.relocs = append(sb.relocs, make([]goobj.Reloc, n)...)
        return sb.l.Relocs(sb.symIdx)
 }
 
 // Add a relocation with given type, return its handle and index
 // (to set other fields).
-func (sb *SymbolBuilder) AddRel(typ objabi.RelocType) (Reloc2, int) {
+func (sb *SymbolBuilder) AddRel(typ objabi.RelocType) (Reloc, int) {
        j := len(sb.relocs)
-       sb.relocs = append(sb.relocs, goobj2.Reloc{})
-       sb.reltypes = append(sb.reltypes, typ)
+       sb.relocs = append(sb.relocs, goobj.Reloc{})
+       sb.relocs[j].SetType(uint16(typ))
        relocs := sb.Relocs()
-       return relocs.At2(j), j
+       return relocs.At(j), j
 }
 
 // Sort relocations by offset.
@@ -177,27 +166,6 @@ func (p *relocsByOff) Len() int           { return len(p.relocs) }
 func (p *relocsByOff) Less(i, j int) bool { return p.relocs[i].Off() < p.relocs[j].Off() }
 func (p *relocsByOff) Swap(i, j int) {
        p.relocs[i], p.relocs[j] = p.relocs[j], p.relocs[i]
-       p.reltypes[i], p.reltypes[j] = p.reltypes[j], p.reltypes[i]
-}
-
-// AddReloc appends the specified reloc to the symbols list of
-// relocations. Return value is the index of the newly created
-// reloc.
-func (sb *SymbolBuilder) AddReloc(r Reloc) uint32 {
-       // Populate a goobj2.Reloc from external reloc record.
-       rval := uint32(len(sb.relocs))
-       var b goobj2.Reloc
-       b.Set(r.Off, r.Size, 0, r.Add, goobj2.SymRef{PkgIdx: 0, SymIdx: uint32(r.Sym)})
-       sb.relocs = append(sb.relocs, b)
-       sb.reltypes = append(sb.reltypes, r.Type)
-       return rval
-}
-
-// Update the j-th relocation in place.
-func (sb *SymbolBuilder) SetReloc(j int, r Reloc) {
-       // Populate a goobj2.Reloc from external reloc record.
-       sb.relocs[j].Set(r.Off, r.Size, 0, r.Add, goobj2.SymRef{PkgIdx: 0, SymIdx: uint32(r.Sym)})
-       sb.reltypes[j] = r.Type
 }
 
 func (sb *SymbolBuilder) Reachable() bool {
@@ -208,10 +176,6 @@ func (sb *SymbolBuilder) SetReachable(v bool) {
        sb.l.SetAttrReachable(sb.symIdx, v)
 }
 
-func (sb *SymbolBuilder) setReachable() {
-       sb.SetReachable(true)
-}
-
 func (sb *SymbolBuilder) ReadOnly() bool {
        return sb.l.AttrReadOnly(sb.symIdx)
 }
@@ -315,6 +279,10 @@ func (sb *SymbolBuilder) SetUint(arch *sys.Arch, r int64, v uint64) int64 {
        return sb.setUintXX(arch, r, v, int64(arch.PtrSize))
 }
 
+func (sb *SymbolBuilder) SetUintptr(arch *sys.Arch, r int64, v uintptr) int64 {
+       return sb.setUintXX(arch, r, uint64(v), int64(arch.PtrSize))
+}
+
 func (sb *SymbolBuilder) SetAddrPlus(arch *sys.Arch, off int64, tgt Sym, add int64) int64 {
        if sb.Type() == 0 {
                sb.SetType(sym.SDATA)
@@ -323,35 +291,58 @@ func (sb *SymbolBuilder) SetAddrPlus(arch *sys.Arch, off int64, tgt Sym, add int
                sb.size = off + int64(arch.PtrSize)
                sb.Grow(sb.size)
        }
-       var r Reloc
-       r.Sym = tgt
-       r.Off = int32(off)
-       r.Size = uint8(arch.PtrSize)
-       r.Type = objabi.R_ADDR
-       r.Add = add
-       sb.AddReloc(r)
-       return off + int64(r.Size)
+       r, _ := sb.AddRel(objabi.R_ADDR)
+       r.SetSym(tgt)
+       r.SetOff(int32(off))
+       r.SetSiz(uint8(arch.PtrSize))
+       r.SetAdd(add)
+       return off + int64(r.Siz())
 }
 
 func (sb *SymbolBuilder) SetAddr(arch *sys.Arch, off int64, tgt Sym) int64 {
        return sb.SetAddrPlus(arch, off, tgt, 0)
 }
 
+func (sb *SymbolBuilder) AddStringAt(off int64, str string) int64 {
+       strLen := int64(len(str))
+       if off+strLen > int64(len(sb.data)) {
+               panic("attempt to write past end of buffer")
+       }
+       copy(sb.data[off:off+strLen], str)
+       return off + strLen
+}
+
+// AddCStringAt adds str plus a null terminating byte.
+func (sb *SymbolBuilder) AddCStringAt(off int64, str string) int64 {
+       strLen := int64(len(str))
+       if off+strLen+1 > int64(len(sb.data)) {
+               panic("attempt to write past end of buffer")
+       }
+       copy(sb.data[off:off+strLen], str)
+       sb.data[off+strLen] = 0
+       return off + strLen + 1
+}
+
 func (sb *SymbolBuilder) Addstring(str string) int64 {
        if sb.kind == 0 {
                sb.kind = sym.SNOPTRDATA
        }
        r := sb.size
-       if sb.name == ".shstrtab" {
-               // FIXME: find a better mechanism for this
-               sb.l.elfsetstring(str, int(r))
-       }
        sb.data = append(sb.data, str...)
        sb.data = append(sb.data, 0)
        sb.size = int64(len(sb.data))
        return r
 }
 
+func (sb *SymbolBuilder) SetBytesAt(off int64, b []byte) int64 {
+       datLen := int64(len(b))
+       if off+datLen > int64(len(sb.data)) {
+               panic("attempt to write past end of buffer")
+       }
+       copy(sb.data[off:off+datLen], b)
+       return off + datLen
+}
+
 func (sb *SymbolBuilder) addSymRef(tgt Sym, add int64, typ objabi.RelocType, rsize int) int64 {
        if sb.kind == 0 {
                sb.kind = sym.SDATA
@@ -361,15 +352,13 @@ func (sb *SymbolBuilder) addSymRef(tgt Sym, add int64, typ objabi.RelocType, rsi
        sb.size += int64(rsize)
        sb.Grow(sb.size)
 
-       var r Reloc
-       r.Sym = tgt
-       r.Off = int32(i)
-       r.Size = uint8(rsize)
-       r.Type = typ
-       r.Add = add
-       sb.AddReloc(r)
+       r, _ := sb.AddRel(typ)
+       r.SetSym(tgt)
+       r.SetOff(int32(i))
+       r.SetSiz(uint8(rsize))
+       r.SetAdd(add)
 
-       return i + int64(r.Size)
+       return i + int64(rsize)
 }
 
 // Add a symbol reference (relocation) with given type, addend, and size
@@ -390,6 +379,10 @@ func (sb *SymbolBuilder) AddAddr(arch *sys.Arch, tgt Sym) int64 {
        return sb.AddAddrPlus(arch, tgt, 0)
 }
 
+func (sb *SymbolBuilder) AddPEImageRelativeAddrPlus(arch *sys.Arch, tgt Sym, add int64) int64 {
+       return sb.addSymRef(tgt, add, objabi.R_PEIMAGEOFF, 4)
+}
+
 func (sb *SymbolBuilder) AddPCRelPlus(arch *sys.Arch, tgt Sym, add int64) int64 {
        return sb.addSymRef(tgt, add, objabi.R_PCREL, 4)
 }
@@ -429,3 +422,21 @@ func (sb *SymbolBuilder) MakeWritable() {
                sb.l.SetAttrReadOnly(sb.symIdx, false)
        }
 }
+
+func (sb *SymbolBuilder) AddUleb(v uint64) {
+       if v < 128 { // common case: 1 byte
+               sb.AddUint8(uint8(v))
+               return
+       }
+       for {
+               c := uint8(v & 0x7f)
+               v >>= 7
+               if v != 0 {
+                       c |= 0x80
+               }
+               sb.AddUint8(c)
+               if c&0x80 == 0 {
+                       break
+               }
+       }
+}