[dev.link] cmd/link: add SymbolBuilder helper

Add SymbolBuilder helper type -- this type provides a set of methods
intended to make it easy to manipulate the content of a symbol (type,
relocations, data, etc).

Change-Id: I579bf8d04650e66d33a9780a6c2347a576c94c6f
Reviewed-on: https://go-review.googlesource.com/c/go/+/210178
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
This commit is contained in:
Than McIntosh 2019-12-11 14:36:17 -05:00
parent c6fea80b95
commit b720014743
4 changed files with 491 additions and 6 deletions

View file

@ -211,8 +211,12 @@ type Loader struct {
flags uint32
strictDupMsgs int // number of strict-dup warning/errors, when FlagStrictDups is enabled
elfsetstring elfsetstringFunc
}
type elfsetstringFunc func(s *sym.Symbol, str string, off int)
// extSymPayload holds the payload (data + relocations) for linker-synthesized
// external symbols (note that symbol value is stored in a separate slice).
type extSymPayload struct {
@ -229,7 +233,7 @@ const (
FlagStrictDups = 1 << iota
)
func NewLoader(flags uint32) *Loader {
func NewLoader(flags uint32, elfsetstring elfsetstringFunc) *Loader {
nbuiltin := goobj2.NBuiltin()
return &Loader{
start: make(map[*oReader]Sym),
@ -252,6 +256,7 @@ func NewLoader(flags uint32) *Loader {
extStaticSyms: make(map[nameVer]Sym),
builtinSyms: make([]Sym, nbuiltin),
flags: flags,
elfsetstring: elfsetstring,
}
}
@ -392,6 +397,21 @@ func (l *Loader) getPayload(i Sym) *extSymPayload {
return &l.payloads[pi]
}
func (ms *extSymPayload) Grow(siz int64) {
if int64(int(siz)) != siz {
log.Fatalf("symgrow size %d too long", siz)
}
if int64(len(ms.data)) >= siz {
return
}
if cap(ms.data) < int(siz) {
cl := len(ms.data)
ms.data = append(ms.data, make([]byte, int(siz)+1-cl)...)
ms.data = ms.data[0:cl]
}
ms.data = ms.data[:siz]
}
// Ensure Syms slice has enough space, as well as growing the
// 'payloads' slice.
func (l *Loader) growSyms(i int) {