[dev.link] cmd/link: add storage and methods for read/write of Sym value

Add loader methods SymValue() and SetSymValue() to get/set the
value of a symbol by global index.

Change-Id: Ifc71480fc34c719ad00506d0828edf36c1a57119
Reviewed-on: https://go-review.googlesource.com/c/go/+/211302
Run-TryBot: Than McIntosh <thanm@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
This commit is contained in:
Than McIntosh 2019-12-13 11:51:15 -05:00
parent b658c62e9c
commit e6b044b200
2 changed files with 36 additions and 3 deletions

View File

@ -157,6 +157,7 @@ type Loader struct {
overwrite map[Sym]Sym // overwrite[i]=j if symbol j overwrites symbol i overwrite map[Sym]Sym // overwrite[i]=j if symbol j overwrites symbol i
payloads []extSymPayload // contents of linker-materialized external syms payloads []extSymPayload // contents of linker-materialized external syms
values []int64 // symbol values, indexed by global sym index
itablink map[Sym]struct{} // itablink[j] defined if j is go.itablink.* itablink map[Sym]struct{} // itablink[j] defined if j is go.itablink.*
@ -197,11 +198,10 @@ type Loader struct {
} }
// extSymPayload holds the payload (data + relocations) for linker-synthesized // extSymPayload holds the payload (data + relocations) for linker-synthesized
// external symbols. // external symbols (note that symbol value is stored in a separate slice).
type extSymPayload struct { type extSymPayload struct {
name string // TODO: would this be better as offset into str table? name string // TODO: would this be better as offset into str table?
size int64 size int64
value int64
ver int ver int
kind sym.SymKind kind sym.SymKind
relocs []Reloc relocs []Reloc
@ -247,6 +247,7 @@ func (l *Loader) addObj(pkg string, r *oReader) Sym {
l.start[r] = i l.start[r] = i
l.objs = append(l.objs, objIdx{r, i, i + Sym(n) - 1}) l.objs = append(l.objs, objIdx{r, i, i + Sym(n) - 1})
l.max += Sym(n) l.max += Sym(n)
l.growValues(int(l.max))
return i return i
} }
@ -373,6 +374,7 @@ func (l *Loader) growSyms(i int) {
} }
l.Syms = append(l.Syms, make([]*sym.Symbol, i+1-n)...) l.Syms = append(l.Syms, make([]*sym.Symbol, i+1-n)...)
l.payloads = append(l.payloads, make([]extSymPayload, i+1-n)...) l.payloads = append(l.payloads, make([]extSymPayload, i+1-n)...)
l.growValues(int(i) + 1)
l.growAttrBitmaps(int(i) + 1) l.growAttrBitmaps(int(i) + 1)
} }
@ -832,6 +834,24 @@ func (l *Loader) IsItabLink(i Sym) bool {
return false return false
} }
// growValues grows the slice used to store symbol values.
func (l *Loader) growValues(reqLen int) {
curLen := len(l.values)
if reqLen > curLen {
l.values = append(l.values, make([]int64, reqLen+1-curLen)...)
}
}
// SymValue returns the value of the i-th symbol. i is global index.
func (l *Loader) SymValue(i Sym) int64 {
return l.values[i]
}
// SetSymValue sets the value of the i-th symbol. i is global index.
func (l *Loader) SetSymValue(i Sym, val int64) {
l.values[i] = val
}
// Returns the symbol content of the i-th symbol. i is global index. // Returns the symbol content of the i-th symbol. i is global index.
func (l *Loader) Data(i Sym) []byte { func (l *Loader) Data(i Sym) []byte {
if l.IsExternal(i) { if l.IsExternal(i) {

View File

@ -32,7 +32,7 @@ func TestAddMaterializedSymbol(t *testing.T) {
// Create some syms from a dummy object file symbol to get things going. // Create some syms from a dummy object file symbol to get things going.
addDummyObjSym(t, ldr, or, "type.uint8") addDummyObjSym(t, ldr, or, "type.uint8")
addDummyObjSym(t, ldr, or, "mumble") ts2 := addDummyObjSym(t, ldr, or, "mumble")
addDummyObjSym(t, ldr, or, "type.string") addDummyObjSym(t, ldr, or, "type.string")
// Create some external symbols. // Create some external symbols.
@ -96,4 +96,17 @@ func TestAddMaterializedSymbol(t *testing.T) {
if !ldr.AttrVisibilityHidden(es3) { if !ldr.AttrVisibilityHidden(es3) {
t.Errorf("expected hidden after update") t.Errorf("expected hidden after update")
} }
// Test get/set symbol value.
toTest := []Sym{ts2, es3}
for i, s := range toTest {
if v := ldr.SymValue(s); v != 0 {
t.Errorf("ldr.Value(%d): expected 0 got %d\n", s, v)
}
nv := int64(i + 101)
ldr.SetSymValue(s, nv)
if v := ldr.SymValue(s); v != nv {
t.Errorf("ldr.SetValue(%d,%d): expected %d got %d\n", s, nv, nv, v)
}
}
} }