1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:04:42 -07:00

[dev.link] cmd/internal/obj: change writer to use new-style accessors

Introduce field setters and use them on the writer side. Now we
are able to eliminate the old-style types.

Change-Id: I650d837328dc02f9be839d16a31812be86721b91
Reviewed-on: https://go-review.googlesource.com/c/go/+/227640
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
This commit is contained in:
Cherry Zhang 2020-04-08 13:24:27 -04:00
parent d9c51620b9
commit 88d6d37b77
2 changed files with 56 additions and 52 deletions

View File

@ -245,6 +245,19 @@ func (s *Sym2) ReflectMethod() bool { return s.Flag()&SymFlagReflectMethod != 0
func (s *Sym2) IsGoType() bool { return s.Flag()&SymFlagGoType != 0 }
func (s *Sym2) TopFrame() bool { return s.Flag()&SymFlagTopFrame != 0 }
func (s *Sym2) SetName(x string, w *Writer) {
binary.LittleEndian.PutUint32(s[:], uint32(len(x)))
binary.LittleEndian.PutUint32(s[4:], w.stringOff(x))
}
func (s *Sym2) SetABI(x uint16) { binary.LittleEndian.PutUint16(s[8:], x) }
func (s *Sym2) SetType(x uint8) { s[10] = x }
func (s *Sym2) SetFlag(x uint8) { s[11] = x }
func (s *Sym2) SetSiz(x uint32) { binary.LittleEndian.PutUint32(s[12:], x) }
func (s *Sym2) SetAlign(x uint32) { binary.LittleEndian.PutUint32(s[16:], x) }
func (s *Sym2) Write(w *Writer) { w.Bytes(s[:]) }
// Symbol reference.
type SymRef struct {
PkgIdx uint32
@ -302,6 +315,8 @@ func (r *Reloc2) Set(off int32, size uint8, typ uint8, add int64, sym SymRef) {
r.SetSym(sym)
}
func (r *Reloc2) Write(w *Writer) { w.Bytes(r[:]) }
// Aux symbol info.
type Aux struct {
Type uint8
@ -335,6 +350,14 @@ func (a *Aux2) Sym() SymRef {
return SymRef{binary.LittleEndian.Uint32(a[1:]), binary.LittleEndian.Uint32(a[5:])}
}
func (a *Aux2) SetType(x uint8) { a[0] = x }
func (a *Aux2) SetSym(x SymRef) {
binary.LittleEndian.PutUint32(a[1:], x.PkgIdx)
binary.LittleEndian.PutUint32(a[5:], x.SymIdx)
}
func (a *Aux2) Write(w *Writer) { w.Bytes(a[:]) }
type Writer struct {
wr *bio.Writer
stringMap map[string]uint32
@ -353,13 +376,17 @@ func (w *Writer) AddString(s string) {
w.RawString(s)
}
func (w *Writer) StringRef(s string) {
func (w *Writer) stringOff(s string) uint32 {
off, ok := w.stringMap[s]
if !ok {
panic(fmt.Sprintf("writeStringRef: string not added: %q", s))
}
return off
}
func (w *Writer) StringRef(s string) {
w.Uint32(uint32(len(s)))
w.Uint32(off)
w.Uint32(w.stringOff(s))
}
func (w *Writer) RawString(s string) {

View File

@ -248,14 +248,13 @@ func (w *writer) Sym(s *LSym) {
if s.Func != nil {
align = uint32(s.Func.Align)
}
o := goobj2.Sym{
Name: name,
ABI: abi,
Type: uint8(s.Type),
Flag: flag,
Siz: uint32(s.Size),
Align: align,
}
var o goobj2.Sym2
o.SetName(name, w.Writer)
o.SetABI(abi)
o.SetType(uint8(s.Type))
o.SetFlag(flag)
o.SetSiz(uint32(s.Size))
o.SetAlign(align)
o.Write(w.Writer)
}
@ -271,66 +270,44 @@ func makeSymRef(s *LSym) goobj2.SymRef {
}
func (w *writer) Reloc(r *Reloc) {
o := goobj2.Reloc{
Off: r.Off,
Siz: r.Siz,
Type: uint8(r.Type),
Add: r.Add,
Sym: makeSymRef(r.Sym),
var o goobj2.Reloc2
o.SetOff(r.Off)
o.SetSiz(r.Siz)
o.SetType(uint8(r.Type))
o.SetAdd(r.Add)
o.SetSym(makeSymRef(r.Sym))
o.Write(w.Writer)
}
func (w *writer) aux1(typ uint8, rs *LSym) {
var o goobj2.Aux2
o.SetType(typ)
o.SetSym(makeSymRef(rs))
o.Write(w.Writer)
}
func (w *writer) Aux(s *LSym) {
if s.Gotype != nil {
o := goobj2.Aux{
Type: goobj2.AuxGotype,
Sym: makeSymRef(s.Gotype),
}
o.Write(w.Writer)
w.aux1(goobj2.AuxGotype, s.Gotype)
}
if s.Func != nil {
o := goobj2.Aux{
Type: goobj2.AuxFuncInfo,
Sym: makeSymRef(s.Func.FuncInfoSym),
}
o.Write(w.Writer)
w.aux1(goobj2.AuxFuncInfo, s.Func.FuncInfoSym)
for _, d := range s.Func.Pcln.Funcdata {
o := goobj2.Aux{
Type: goobj2.AuxFuncdata,
Sym: makeSymRef(d),
}
o.Write(w.Writer)
w.aux1(goobj2.AuxFuncdata, d)
}
if s.Func.dwarfInfoSym != nil && s.Func.dwarfInfoSym.Size != 0 {
o := goobj2.Aux{
Type: goobj2.AuxDwarfInfo,
Sym: makeSymRef(s.Func.dwarfInfoSym),
}
o.Write(w.Writer)
w.aux1(goobj2.AuxDwarfInfo, s.Func.dwarfInfoSym)
}
if s.Func.dwarfLocSym != nil && s.Func.dwarfLocSym.Size != 0 {
o := goobj2.Aux{
Type: goobj2.AuxDwarfLoc,
Sym: makeSymRef(s.Func.dwarfLocSym),
}
o.Write(w.Writer)
w.aux1(goobj2.AuxDwarfLoc, s.Func.dwarfLocSym)
}
if s.Func.dwarfRangesSym != nil && s.Func.dwarfRangesSym.Size != 0 {
o := goobj2.Aux{
Type: goobj2.AuxDwarfRanges,
Sym: makeSymRef(s.Func.dwarfRangesSym),
}
o.Write(w.Writer)
w.aux1(goobj2.AuxDwarfRanges, s.Func.dwarfRangesSym)
}
if s.Func.dwarfDebugLinesSym != nil && s.Func.dwarfDebugLinesSym.Size != 0 {
o := goobj2.Aux{
Type: goobj2.AuxDwarfLines,
Sym: makeSymRef(s.Func.dwarfDebugLinesSym),
}
o.Write(w.Writer)
w.aux1(goobj2.AuxDwarfLines, s.Func.dwarfDebugLinesSym)
}
}
}