Skip to content

Commit 48c79db

Browse files
committed
[dev.link] cmd/link: simplify DWARF DIE symbol payload
Get rid of of the linker's dwSym struct (which wraps a loader.Loader and a loader.Sym) in favor of just loader.Sym. This requires some minor tweaks to the cmd/internal/dwarf interfaces. Change-Id: Id3ffd7c41b2433ea04417040368700334bb0e611 Reviewed-on: https://go-review.googlesource.com/c/go/+/220982 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>
1 parent 23c52de commit 48c79db

File tree

4 files changed

+70
-73
lines changed

4 files changed

+70
-73
lines changed

src/cmd/internal/dwarf/dwarf.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var logDwarf bool
4848

4949
// Sym represents a symbol.
5050
type Sym interface {
51-
Len() int64
51+
Length(dwarfContext interface{}) int64
5252
}
5353

5454
// A Var represents a local variable or a function parameter.
@@ -1279,7 +1279,7 @@ func PutInlinedFunc(ctxt Context, s *FnState, callersym Sym, callIdx int) error
12791279
putattr(ctxt, s.Info, abbrev, DW_FORM_ref_addr, DW_CLS_REFERENCE, 0, callee)
12801280

12811281
if abbrev == DW_ABRV_INLINED_SUBROUTINE_RANGES {
1282-
putattr(ctxt, s.Info, abbrev, DW_FORM_sec_offset, DW_CLS_PTR, s.Ranges.Len(), s.Ranges)
1282+
putattr(ctxt, s.Info, abbrev, DW_FORM_sec_offset, DW_CLS_PTR, s.Ranges.Length(ctxt), s.Ranges)
12831283
s.PutRanges(ctxt, ic.Ranges)
12841284
} else {
12851285
st := ic.Ranges[0].Start
@@ -1440,7 +1440,7 @@ func putscope(ctxt Context, s *FnState, scopes []Scope, curscope int32, fnabbrev
14401440
putattr(ctxt, s.Info, DW_ABRV_LEXICAL_BLOCK_SIMPLE, DW_FORM_addr, DW_CLS_ADDRESS, scope.Ranges[0].End, s.StartPC)
14411441
} else {
14421442
Uleb128put(ctxt, s.Info, DW_ABRV_LEXICAL_BLOCK_RANGES)
1443-
putattr(ctxt, s.Info, DW_ABRV_LEXICAL_BLOCK_RANGES, DW_FORM_sec_offset, DW_CLS_PTR, s.Ranges.Len(), s.Ranges)
1443+
putattr(ctxt, s.Info, DW_ABRV_LEXICAL_BLOCK_RANGES, DW_FORM_sec_offset, DW_CLS_PTR, s.Ranges.Length(ctxt), s.Ranges)
14441444

14451445
s.PutRanges(ctxt, scope.Ranges)
14461446
}
@@ -1585,7 +1585,7 @@ func putvar(ctxt Context, s *FnState, v *Var, absfn Sym, fnabbrev, inlIndex int,
15851585
}
15861586

15871587
if abbrevUsesLoclist(abbrev) {
1588-
putattr(ctxt, s.Info, abbrev, DW_FORM_sec_offset, DW_CLS_PTR, s.Loc.Len(), s.Loc)
1588+
putattr(ctxt, s.Info, abbrev, DW_FORM_sec_offset, DW_CLS_PTR, s.Loc.Length(ctxt), s.Loc)
15891589
v.PutLocationList(s.Loc, s.StartPC)
15901590
} else {
15911591
loc := encbuf[:0]

src/cmd/internal/obj/objfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (ctxt *Link) dwarfSym(s *LSym) (dwarfInfoSym, dwarfLocSym, dwarfRangesSym,
216216
return s.Func.dwarfInfoSym, s.Func.dwarfLocSym, s.Func.dwarfRangesSym, s.Func.dwarfAbsFnSym, s.Func.dwarfDebugLinesSym
217217
}
218218

219-
func (s *LSym) Len() int64 {
219+
func (s *LSym) Length(dwarfContext interface{}) int64 {
220220
return s.Size
221221
}
222222

src/cmd/link/internal/ld/dwarf.go

Lines changed: 61 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -70,60 +70,70 @@ func newdwctxt2(linkctxt *Link, forTypeGen bool) dwctxt2 {
7070
return d
7171
}
7272

73+
// dwSym wraps a loader.Sym; this type is meant to obey the interface
74+
// rules for dwarf.Sym from the cmd/internal/dwarf package. DwDie and
75+
// DwAttr objects contain references to symbols via this type.
76+
type dwSym loader.Sym
77+
78+
func (s dwSym) Length(dwarfContext interface{}) int64 {
79+
l := dwarfContext.(dwctxt2).ldr
80+
return int64(len(l.Data(loader.Sym(s))))
81+
}
82+
7383
func (c dwctxt2) PtrSize() int {
7484
return c.arch.PtrSize
7585
}
7686

7787
func (c dwctxt2) AddInt(s dwarf.Sym, size int, i int64) {
78-
ds := s.(dwSym)
79-
dsu := ds.l.MakeSymbolUpdater(ds.s)
88+
ds := loader.Sym(s.(dwSym))
89+
dsu := c.ldr.MakeSymbolUpdater(ds)
8090
dsu.AddUintXX(c.arch, uint64(i), size)
8191
}
8292

8393
func (c dwctxt2) AddBytes(s dwarf.Sym, b []byte) {
84-
ds := s.(dwSym)
85-
dsu := ds.l.MakeSymbolUpdater(ds.s)
94+
ds := loader.Sym(s.(dwSym))
95+
dsu := c.ldr.MakeSymbolUpdater(ds)
8696
dsu.AddBytes(b)
8797
}
8898

8999
func (c dwctxt2) AddString(s dwarf.Sym, v string) {
90-
ds := s.(dwSym)
91-
dsu := ds.l.MakeSymbolUpdater(ds.s)
100+
ds := loader.Sym(s.(dwSym))
101+
dsu := c.ldr.MakeSymbolUpdater(ds)
92102
dsu.Addstring(v)
93103
}
94104

95105
func (c dwctxt2) AddAddress(s dwarf.Sym, data interface{}, value int64) {
96-
ds := s.(dwSym)
97-
dsu := ds.l.MakeSymbolUpdater(ds.s)
106+
ds := loader.Sym(s.(dwSym))
107+
dsu := c.ldr.MakeSymbolUpdater(ds)
98108
if value != 0 {
99109
value -= dsu.Value()
100110
}
101-
tgtds := data.(dwSym)
102-
dsu.AddAddrPlus(c.arch, tgtds.s, value)
111+
tgtds := loader.Sym(data.(dwSym))
112+
dsu.AddAddrPlus(c.arch, tgtds, value)
103113
}
104114

105115
func (c dwctxt2) AddCURelativeAddress(s dwarf.Sym, data interface{}, value int64) {
106-
ds := s.(dwSym)
107-
dsu := ds.l.MakeSymbolUpdater(ds.s)
116+
ds := loader.Sym(s.(dwSym))
117+
dsu := c.ldr.MakeSymbolUpdater(ds)
108118
if value != 0 {
109119
value -= dsu.Value()
110120
}
111-
tgtds := data.(dwSym)
112-
dsu.AddCURelativeAddrPlus(c.arch, tgtds.s, value)
121+
tgtds := loader.Sym(data.(dwSym))
122+
dsu.AddCURelativeAddrPlus(c.arch, tgtds, value)
113123
}
114124

115125
func (c dwctxt2) AddSectionOffset(s dwarf.Sym, size int, t interface{}, ofs int64) {
116-
ds := s.(dwSym)
117-
dsu := ds.l.MakeSymbolUpdater(ds.s)
118-
tds := t.(dwSym)
126+
ds := loader.Sym(s.(dwSym))
127+
dsu := c.ldr.MakeSymbolUpdater(ds)
128+
tds := loader.Sym(t.(dwSym))
119129
switch size {
120130
default:
121-
c.linkctxt.Errorf(ds.s, "invalid size %d in adddwarfref\n", size)
131+
c.linkctxt.Errorf(ds, "invalid size %d in adddwarfref\n", size)
122132
fallthrough
123133
case c.arch.PtrSize:
124-
dsu.AddAddrPlus(c.arch, tds.s, 0)
134+
dsu.AddAddrPlus(c.arch, tds, 0)
125135
case 4:
126-
dsu.AddAddrPlus4(c.arch, tds.s, 0)
136+
dsu.AddAddrPlus4(c.arch, tds, 0)
127137
}
128138
rsl := dsu.Relocs()
129139
r := &rsl[len(rsl)-1]
@@ -139,8 +149,8 @@ func (c dwctxt2) AddDWARFAddrSectionOffset(s dwarf.Sym, t interface{}, ofs int64
139149

140150
c.AddSectionOffset(s, size, t, ofs)
141151

142-
ds := s.(dwSym)
143-
dsu := ds.l.MakeSymbolUpdater(ds.s)
152+
ds := loader.Sym(s.(dwSym))
153+
dsu := c.ldr.MakeSymbolUpdater(ds)
144154
rsl := dsu.Relocs()
145155
r := &rsl[len(rsl)-1]
146156
r.Type = objabi.R_DWARFSECREF
@@ -168,23 +178,6 @@ func (c dwctxt2) RecordChildDieOffsets(s dwarf.Sym, vars []*dwarf.Var, offsets [
168178
panic("should be used only in the compiler")
169179
}
170180

171-
// dwSym wraps a loader.Sym; objects of this type are stored
172-
// in the 'Sym' field of dwarf.DIE objects.
173-
//
174-
// FIXME: the main reason we need the loader.Loader pointer field is
175-
// that the dwarf.Sym interface has a Len() method with no parameters.
176-
// If we changed this method to accept a dwxtxt (from which we could
177-
// access the loader) then we could get rid of this field and/or avoid
178-
// using a struct.
179-
type dwSym struct {
180-
s loader.Sym
181-
l *loader.Loader
182-
}
183-
184-
func (s dwSym) Len() int64 {
185-
return int64(len(s.l.Data(s.s)))
186-
}
187-
188181
var gdbscript string
189182

190183
var dwarfp2 []loader.Sym
@@ -264,7 +257,7 @@ func (d *dwctxt2) newdie(parent *dwarf.DWDie, abbrev int, name string, version i
264257
dsu.SetType(sym.SDWARFINFO)
265258
d.ldr.SetAttrNotInSymbolTable(ds, true)
266259
d.ldr.SetAttrReachable(ds, true)
267-
die.Sym = dwSym{s: ds, l: d.ldr}
260+
die.Sym = dwSym(ds)
268261
if abbrev >= dwarf.DW_ABRV_NULLTYPE && abbrev <= dwarf.DW_ABRV_TYPEDECL {
269262
d.tmap[name] = ds
270263
}
@@ -361,15 +354,15 @@ func (d *dwctxt2) newrefattr(die *dwarf.DWDie, attr uint16, ref loader.Sym) *dwa
361354
if ref == 0 {
362355
return nil
363356
}
364-
return newattr(die, attr, dwarf.DW_CLS_REFERENCE, 0, dwSym{s: ref, l: d.ldr})
357+
return newattr(die, attr, dwarf.DW_CLS_REFERENCE, 0, dwSym(ref))
365358
}
366359

367360
func (d *dwctxt2) dtolsym(s dwarf.Sym) loader.Sym {
368361
if s == nil {
369362
return 0
370363
}
371-
dws := s.(dwSym)
372-
return dws.s
364+
dws := loader.Sym(s.(dwSym))
365+
return dws
373366
}
374367

375368
func (d *dwctxt2) putdie(syms []loader.Sym, die *dwarf.DWDie) []loader.Sym {
@@ -405,7 +398,7 @@ func newmemberoffsetattr(die *dwarf.DWDie, offs int32) {
405398
// GDB doesn't like FORM_addr for AT_location, so emit a
406399
// location expression that evals to a const.
407400
func (d *dwctxt2) newabslocexprattr(die *dwarf.DWDie, addr int64, symIdx loader.Sym) {
408-
newattr(die, dwarf.DW_AT_location, dwarf.DW_CLS_ADDRESS, addr, dwSym{s: symIdx, l: d.ldr})
401+
newattr(die, dwarf.DW_AT_location, dwarf.DW_CLS_ADDRESS, addr, dwSym(symIdx))
409402
}
410403

411404
func (d *dwctxt2) lookupOrDiag(n string) loader.Sym {
@@ -444,7 +437,7 @@ func (d *dwctxt2) dotypedef(parent *dwarf.DWDie, gotype loader.Sym, name string,
444437
tds := d.ldr.CreateExtSym("")
445438
tdsu := d.ldr.MakeSymbolUpdater(tds)
446439
tdsu.SetType(sym.SDWARFINFO)
447-
def.Sym = dwSym{s: tds, l: d.ldr}
440+
def.Sym = dwSym(tds)
448441
d.ldr.SetAttrNotInSymbolTable(tds, true)
449442
d.ldr.SetAttrReachable(tds, true)
450443

@@ -483,8 +476,8 @@ func (d *dwctxt2) defgotype(gotype loader.Sym) loader.Sym {
483476
}
484477

485478
gtdwSym := d.newtype(gotype)
486-
d.tdmap[gotype] = gtdwSym.Sym.(dwSym).s
487-
return gtdwSym.Sym.(dwSym).s
479+
d.tdmap[gotype] = loader.Sym(gtdwSym.Sym.(dwSym))
480+
return loader.Sym(gtdwSym.Sym.(dwSym))
488481
}
489482

490483
func (d *dwctxt2) newtype(gotype loader.Sym) *dwarf.DWDie {
@@ -654,19 +647,19 @@ func (d *dwctxt2) newtype(gotype loader.Sym) *dwarf.DWDie {
654647
newattr(die, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, int64(kind), 0)
655648

656649
if d.ldr.AttrReachable(gotype) {
657-
newattr(die, dwarf.DW_AT_go_runtime_type, dwarf.DW_CLS_GO_TYPEREF, 0, dwSym{s: gotype, l: d.ldr})
650+
newattr(die, dwarf.DW_AT_go_runtime_type, dwarf.DW_CLS_GO_TYPEREF, 0, dwSym(gotype))
658651
}
659652

660653
// Sanity check.
661654
if _, ok := d.rtmap[gotype]; ok {
662655
log.Fatalf("internal error: rtmap entry already installed\n")
663656
}
664657

665-
ds := die.Sym.(dwSym)
658+
ds := loader.Sym(die.Sym.(dwSym))
666659
if typedefdie != nil {
667-
ds = typedefdie.Sym.(dwSym)
660+
ds = loader.Sym(typedefdie.Sym.(dwSym))
668661
}
669-
d.rtmap[ds.s] = gotype
662+
d.rtmap[ds] = gotype
670663

671664
if _, ok := prototypedies[sn]; ok {
672665
prototypedies[sn] = die
@@ -702,13 +695,13 @@ func (d *dwctxt2) defptrto(dwtype loader.Sym) loader.Sym {
702695
// pointers of slices. Link to the ones we can find.
703696
gts := d.ldr.Lookup("type."+ptrname, 0)
704697
if gts != 0 && d.ldr.AttrReachable(gts) {
705-
newattr(pdie, dwarf.DW_AT_go_runtime_type, dwarf.DW_CLS_GO_TYPEREF, 0, dwSym{s: gts, l: d.ldr})
698+
newattr(pdie, dwarf.DW_AT_go_runtime_type, dwarf.DW_CLS_GO_TYPEREF, 0, dwSym(gts))
706699
}
707700

708701
if gts != 0 {
709-
ds := pdie.Sym.(dwSym)
710-
d.rtmap[ds.s] = gts
711-
d.tdmap[gts] = ds.s
702+
ds := loader.Sym(pdie.Sym.(dwSym))
703+
d.rtmap[ds] = gts
704+
d.tdmap[gts] = ds
712705
}
713706

714707
return d.dtolsym(pdie.Sym)
@@ -748,7 +741,7 @@ func (d *dwctxt2) substitutetype(structdie *dwarf.DWDie, field string, dwtype lo
748741

749742
a := getattr(child, dwarf.DW_AT_type)
750743
if a != nil {
751-
a.Data = dwSym{s: dwtype, l: d.ldr}
744+
a.Data = dwSym(dwtype)
752745
} else {
753746
d.newrefattr(child, dwarf.DW_AT_type, dwtype)
754747
}
@@ -791,7 +784,7 @@ func (d *dwctxt2) synthesizeslicetypes(ctxt *Link, die *dwarf.DWDie) {
791784
continue
792785
}
793786
d.copychildren(ctxt, die, prototype)
794-
elem := getattr(die, dwarf.DW_AT_go_elem).Data.(dwSym).s
787+
elem := loader.Sym(getattr(die, dwarf.DW_AT_go_elem).Data.(dwSym))
795788
d.substitutetype(die, "array", d.defptrto(elem))
796789
}
797790
}
@@ -834,7 +827,7 @@ func (d *dwctxt2) synthesizemaptypes(ctxt *Link, die *dwarf.DWDie) {
834827
if die.Abbrev != dwarf.DW_ABRV_MAPTYPE {
835828
continue
836829
}
837-
gotype := getattr(die, dwarf.DW_AT_type).Data.(dwSym).s
830+
gotype := loader.Sym(getattr(die, dwarf.DW_AT_type).Data.(dwSym))
838831
keytype := decodetypeMapKey2(d.ldr, d.arch, gotype)
839832
valtype := decodetypeMapValue2(d.ldr, d.arch, gotype)
840833
keydata := d.ldr.Data(keytype)
@@ -932,7 +925,7 @@ func (d *dwctxt2) synthesizechantypes(ctxt *Link, die *dwarf.DWDie) {
932925
if die.Abbrev != dwarf.DW_ABRV_CHANTYPE {
933926
continue
934927
}
935-
elemgotype := getattr(die, dwarf.DW_AT_type).Data.(dwSym).s
928+
elemgotype := loader.Sym(getattr(die, dwarf.DW_AT_type).Data.(dwSym))
936929
tname := d.ldr.SymName(elemgotype)
937930
elemname := tname[5:]
938931
elemtype := d.walksymtypedef(d.defgotype(d.lookupOrDiag(tname)))
@@ -1200,11 +1193,11 @@ func (d *dwctxt2) mkBuiltinType(ctxt *Link, abrv int, tname string) *dwarf.DWDie
12001193
gotype := d.lookupOrDiag("type." + tname)
12011194

12021195
// Map from die sym to type sym
1203-
ds := die.Sym.(dwSym)
1204-
d.rtmap[ds.s] = gotype
1196+
ds := loader.Sym(die.Sym.(dwSym))
1197+
d.rtmap[ds] = gotype
12051198

12061199
// Map from type to def sym
1207-
d.tdmap[gotype] = ds.s
1200+
d.tdmap[gotype] = ds
12081201

12091202
return die
12101203
}
@@ -1241,7 +1234,7 @@ func dwarfGenerateDebugInfo2(ctxt *Link) {
12411234
newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_unsigned, 0)
12421235
newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, int64(d.arch.PtrSize), 0)
12431236
newattr(die, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, objabi.KindUintptr, 0)
1244-
newattr(die, dwarf.DW_AT_go_runtime_type, dwarf.DW_CLS_ADDRESS, 0, dwSym{s: d.lookupOrDiag("type.uintptr"), l: d.ldr})
1237+
newattr(die, dwarf.DW_AT_go_runtime_type, dwarf.DW_CLS_ADDRESS, 0, dwSym(d.lookupOrDiag("type.uintptr")))
12451238

12461239
d.uintptrInfoSym = d.mustFind("uintptr")
12471240

@@ -1515,19 +1508,19 @@ func convertSymbolsInDIE(ctxt *Link, die *dwarf.DWDie, convdies map[*dwarf.DWDie
15151508
}
15161509
convdies[die] = true
15171510
if die.Sym != nil {
1518-
symIdx, ok := die.Sym.(dwSym)
1511+
ds, ok := die.Sym.(dwSym)
15191512
if !ok {
15201513
panic("bad die sym field")
15211514
}
1522-
ls := symIdx.s
1523-
if ls == 0 {
1515+
symIdx := loader.Sym(ds)
1516+
if symIdx == 0 {
15241517
panic("zero loader sym for die")
15251518
}
1526-
die.Sym = ctxt.loader.Syms[symIdx.s]
1519+
die.Sym = ctxt.loader.Syms[symIdx]
15271520
}
15281521
for a := die.Attr; a != nil; a = a.Link {
15291522
if attrSym, ok := a.Data.(dwSym); ok {
1530-
a.Data = ctxt.loader.Syms[attrSym.s]
1523+
a.Data = ctxt.loader.Syms[loader.Sym(attrSym)]
15311524
}
15321525
}
15331526
convertSymbolsInDIE(ctxt, die.Child, convdies)

src/cmd/link/internal/sym/symbol.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ func (s *Symbol) Len() int64 {
103103
return s.Size
104104
}
105105

106+
func (s *Symbol) Length(dwarfContext interface{}) int64 {
107+
return s.Size
108+
}
109+
106110
func (s *Symbol) Grow(siz int64) {
107111
if int64(int(siz)) != siz {
108112
log.Fatalf("symgrow size %d too long", siz)

0 commit comments

Comments
 (0)