]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.link] cmd/link: add storage and methods for read/write of Sym value
authorThan McIntosh <thanm@google.com>
Fri, 13 Dec 2019 16:51:15 +0000 (11:51 -0500)
committerThan McIntosh <thanm@google.com>
Wed, 18 Dec 2019 14:24:45 +0000 (14:24 +0000)
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>
src/cmd/link/internal/loader/loader.go
src/cmd/link/internal/loader/loader_test.go

index 0a6887ca8a43481c0ad962615f20509d4757dcae..f238df7f5a17dd11244508e5e648deedb7d5a4e0 100644 (file)
@@ -157,6 +157,7 @@ type Loader struct {
        overwrite     map[Sym]Sym       // overwrite[i]=j if symbol j overwrites symbol i
 
        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.*
 
@@ -197,11 +198,10 @@ type Loader struct {
 }
 
 // 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 {
        name   string // TODO: would this be better as offset into str table?
        size   int64
-       value  int64
        ver    int
        kind   sym.SymKind
        relocs []Reloc
@@ -247,6 +247,7 @@ func (l *Loader) addObj(pkg string, r *oReader) Sym {
        l.start[r] = i
        l.objs = append(l.objs, objIdx{r, i, i + Sym(n) - 1})
        l.max += Sym(n)
+       l.growValues(int(l.max))
        return i
 }
 
@@ -373,6 +374,7 @@ func (l *Loader) growSyms(i int) {
        }
        l.Syms = append(l.Syms, make([]*sym.Symbol, i+1-n)...)
        l.payloads = append(l.payloads, make([]extSymPayload, i+1-n)...)
+       l.growValues(int(i) + 1)
        l.growAttrBitmaps(int(i) + 1)
 }
 
@@ -832,6 +834,24 @@ func (l *Loader) IsItabLink(i Sym) bool {
        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.
 func (l *Loader) Data(i Sym) []byte {
        if l.IsExternal(i) {
index 4dde9e04e8cdc0b5257cd4171ef44e07a5135f3c..ec3090bd5a31d6d7acfa7308037ed41d5c6cfc6a 100644 (file)
@@ -32,7 +32,7 @@ func TestAddMaterializedSymbol(t *testing.T) {
 
        // Create some syms from a dummy object file symbol to get things going.
        addDummyObjSym(t, ldr, or, "type.uint8")
-       addDummyObjSym(t, ldr, or, "mumble")
+       ts2 := addDummyObjSym(t, ldr, or, "mumble")
        addDummyObjSym(t, ldr, or, "type.string")
 
        // Create some external symbols.
@@ -96,4 +96,17 @@ func TestAddMaterializedSymbol(t *testing.T) {
        if !ldr.AttrVisibilityHidden(es3) {
                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)
+               }
+       }
 }