-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwarf.go
2710 lines (2402 loc) · 87.4 KB
/
dwarf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// TODO/NICETOHAVE:
// - eliminate DW_CLS_ if not used
// - package info in compilation units
// - assign types to their packages
// - gdb uses c syntax, meaning clumsy quoting is needed for go identifiers. eg
// ptype struct '[]uint8' and qualifiers need to be quoted away
// - file:line info for variables
// - make strings a typedef so prettyprinters can see the underlying string type
package ld
import (
"cmd/internal/dwarf"
"cmd/internal/obj"
"cmd/internal/objabi"
"cmd/internal/src"
"cmd/internal/sys"
"cmd/link/internal/loader"
"cmd/link/internal/sym"
"cmp"
"fmt"
"internal/abi"
"internal/buildcfg"
"log"
"path"
"runtime"
"slices"
"strings"
"sync"
)
// dwctxt is a wrapper intended to satisfy the method set of
// dwarf.Context, so that functions like dwarf.PutAttrs will work with
// DIEs that use loader.Sym as opposed to *sym.Symbol. It is also
// being used as a place to store tables/maps that are useful as part
// of type conversion (this is just a convenience; it would be easy to
// split these things out into another type if need be).
type dwctxt struct {
linkctxt *Link
ldr *loader.Loader
arch *sys.Arch
// This maps type name string (e.g. "uintptr") to loader symbol for
// the DWARF DIE for that type (e.g. "go:info.type.uintptr")
tmap map[string]loader.Sym
// This maps loader symbol for the DWARF DIE symbol generated for
// a type (e.g. "go:info.uintptr") to the type symbol itself
// ("type:uintptr").
// FIXME: try converting this map (and the next one) to a single
// array indexed by loader.Sym -- this may perform better.
rtmap map[loader.Sym]loader.Sym
// This maps Go type symbol (e.g. "type:XXX") to loader symbol for
// the typedef DIE for that type (e.g. "go:info.XXX..def")
tdmap map[loader.Sym]loader.Sym
// Cache these type symbols, so as to avoid repeatedly looking them up
typeRuntimeEface loader.Sym
typeRuntimeIface loader.Sym
uintptrInfoSym loader.Sym
// Used at various points in that parallel portion of DWARF gen to
// protect against conflicting updates to globals (such as "gdbscript")
dwmu *sync.Mutex
}
// dwSym wraps a loader.Sym; this type is meant to obey the interface
// rules for dwarf.Sym from the cmd/internal/dwarf package. DwDie and
// DwAttr objects contain references to symbols via this type.
type dwSym loader.Sym
func (c dwctxt) PtrSize() int {
return c.arch.PtrSize
}
func (c dwctxt) Size(s dwarf.Sym) int64 {
return int64(len(c.ldr.Data(loader.Sym(s.(dwSym)))))
}
func (c dwctxt) AddInt(s dwarf.Sym, size int, i int64) {
ds := loader.Sym(s.(dwSym))
dsu := c.ldr.MakeSymbolUpdater(ds)
dsu.AddUintXX(c.arch, uint64(i), size)
}
func (c dwctxt) AddBytes(s dwarf.Sym, b []byte) {
ds := loader.Sym(s.(dwSym))
dsu := c.ldr.MakeSymbolUpdater(ds)
dsu.AddBytes(b)
}
func (c dwctxt) AddString(s dwarf.Sym, v string) {
ds := loader.Sym(s.(dwSym))
dsu := c.ldr.MakeSymbolUpdater(ds)
dsu.Addstring(v)
}
func (c dwctxt) AddAddress(s dwarf.Sym, data interface{}, value int64) {
ds := loader.Sym(s.(dwSym))
dsu := c.ldr.MakeSymbolUpdater(ds)
if value != 0 {
value -= dsu.Value()
}
tgtds := loader.Sym(data.(dwSym))
dsu.AddAddrPlus(c.arch, tgtds, value)
}
func (c dwctxt) AddCURelativeAddress(s dwarf.Sym, data interface{}, value int64) {
ds := loader.Sym(s.(dwSym))
dsu := c.ldr.MakeSymbolUpdater(ds)
if value != 0 {
value -= dsu.Value()
}
tgtds := loader.Sym(data.(dwSym))
dsu.AddCURelativeAddrPlus(c.arch, tgtds, value)
}
func (c dwctxt) AddSectionOffset(s dwarf.Sym, size int, t interface{}, ofs int64) {
ds := loader.Sym(s.(dwSym))
dsu := c.ldr.MakeSymbolUpdater(ds)
tds := loader.Sym(t.(dwSym))
switch size {
default:
c.linkctxt.Errorf(ds, "invalid size %d in adddwarfref\n", size)
case c.arch.PtrSize, 4:
}
dsu.AddSymRef(c.arch, tds, ofs, objabi.R_ADDROFF, size)
}
func (c dwctxt) AddDWARFAddrSectionOffset(s dwarf.Sym, t interface{}, ofs int64) {
size := 4
if isDwarf64(c.linkctxt) {
size = 8
}
ds := loader.Sym(s.(dwSym))
dsu := c.ldr.MakeSymbolUpdater(ds)
tds := loader.Sym(t.(dwSym))
switch size {
default:
c.linkctxt.Errorf(ds, "invalid size %d in adddwarfref\n", size)
case c.arch.PtrSize, 4:
}
dsu.AddSymRef(c.arch, tds, ofs, objabi.R_DWARFSECREF, size)
}
func (c dwctxt) AddIndirectTextRef(s dwarf.Sym, t interface{}) {
ds := loader.Sym(s.(dwSym))
dsu := c.ldr.MakeSymbolUpdater(ds)
tds := loader.Sym(t.(dwSym))
dsu.AddSymRef(c.arch, tds, 0, objabi.R_DWTXTADDR_U4, 4)
}
func (c dwctxt) Logf(format string, args ...interface{}) {
c.linkctxt.Logf(format, args...)
}
// At the moment these interfaces are only used in the compiler.
func (c dwctxt) CurrentOffset(s dwarf.Sym) int64 {
panic("should be used only in the compiler")
}
func (c dwctxt) RecordDclReference(s dwarf.Sym, t dwarf.Sym, dclIdx int, inlIndex int) {
panic("should be used only in the compiler")
}
func (c dwctxt) RecordChildDieOffsets(s dwarf.Sym, vars []*dwarf.Var, offsets []int32) {
panic("should be used only in the compiler")
}
func isDwarf64(ctxt *Link) bool {
return ctxt.HeadType == objabi.Haix
}
// https://sourceware.org/gdb/onlinedocs/gdb/dotdebug_005fgdb_005fscripts-section.html
// Each entry inside .debug_gdb_scripts section begins with a non-null prefix
// byte that specifies the kind of entry. The following entries are supported:
const (
GdbScriptPythonFileId = 1
GdbScriptSchemeFileId = 3
GdbScriptPythonTextId = 4
GdbScriptSchemeTextId = 6
)
var gdbscript string
// dwarfSecInfo holds information about a DWARF output section,
// specifically a section symbol and a list of symbols contained in
// that section. On the syms list, the first symbol will always be the
// section symbol, then any remaining symbols (if any) will be
// sub-symbols in that section. Note that for some sections (eg:
// .debug_abbrev), the section symbol is all there is (all content is
// contained in it). For other sections (eg: .debug_info), the section
// symbol is empty and all the content is in the sub-symbols. Finally
// there are some sections (eg: .debug_ranges) where it is a mix (both
// the section symbol and the sub-symbols have content)
type dwarfSecInfo struct {
syms []loader.Sym
}
// secSym returns the section symbol for the section.
func (dsi *dwarfSecInfo) secSym() loader.Sym {
if len(dsi.syms) == 0 {
return 0
}
return dsi.syms[0]
}
// subSyms returns a list of sub-symbols for the section.
func (dsi *dwarfSecInfo) subSyms() []loader.Sym {
if len(dsi.syms) == 0 {
return []loader.Sym{}
}
return dsi.syms[1:]
}
// dwarfp stores the collected DWARF symbols created during
// dwarf generation.
var dwarfp []dwarfSecInfo
func (d *dwctxt) writeabbrev() dwarfSecInfo {
abrvs := d.ldr.CreateSymForUpdate(".debug_abbrev", 0)
abrvs.SetType(sym.SDWARFSECT)
abrvs.AddBytes(dwarf.GetAbbrev())
return dwarfSecInfo{syms: []loader.Sym{abrvs.Sym()}}
}
var dwtypes dwarf.DWDie
// newattr attaches a new attribute to the specified DIE.
//
// FIXME: at the moment attributes are stored in a linked list in a
// fairly space-inefficient way -- it might be better to instead look
// up all attrs in a single large table, then store indices into the
// table in the DIE. This would allow us to common up storage for
// attributes that are shared by many DIEs (ex: byte size of N).
func newattr(die *dwarf.DWDie, attr uint16, cls int, value int64, data interface{}) {
a := new(dwarf.DWAttr)
a.Link = die.Attr
die.Attr = a
a.Atr = attr
a.Cls = uint8(cls)
a.Value = value
a.Data = data
}
// Each DIE (except the root ones) has at least 1 attribute: its
// name. getattr moves the desired one to the front so
// frequently searched ones are found faster.
func getattr(die *dwarf.DWDie, attr uint16) *dwarf.DWAttr {
if die.Attr.Atr == attr {
return die.Attr
}
a := die.Attr
b := a.Link
for b != nil {
if b.Atr == attr {
a.Link = b.Link
b.Link = die.Attr
die.Attr = b
return b
}
a = b
b = b.Link
}
return nil
}
// Every DIE manufactured by the linker has at least an AT_name
// attribute (but it will only be written out if it is listed in the abbrev).
// The compiler does create nameless DWARF DIEs (ex: concrete subprogram
// instance).
// FIXME: it would be more efficient to bulk-allocate DIEs.
func (d *dwctxt) newdie(parent *dwarf.DWDie, abbrev int, name string) *dwarf.DWDie {
die := new(dwarf.DWDie)
die.Abbrev = abbrev
die.Link = parent.Child
parent.Child = die
newattr(die, dwarf.DW_AT_name, dwarf.DW_CLS_STRING, int64(len(name)), name)
// Sanity check: all DIEs created in the linker should be named.
if name == "" {
panic("nameless DWARF DIE")
}
var st sym.SymKind
switch abbrev {
case dwarf.DW_ABRV_FUNCTYPEPARAM, dwarf.DW_ABRV_FUNCTYPEOUTPARAM, dwarf.DW_ABRV_DOTDOTDOT, dwarf.DW_ABRV_STRUCTFIELD, dwarf.DW_ABRV_ARRAYRANGE:
// There are no relocations against these dies, and their names
// are not unique, so don't create a symbol.
return die
case dwarf.DW_ABRV_COMPUNIT, dwarf.DW_ABRV_COMPUNIT_TEXTLESS:
// Avoid collisions with "real" symbol names.
name = fmt.Sprintf(".pkg.%s.%d", name, len(d.linkctxt.compUnits))
st = sym.SDWARFCUINFO
case dwarf.DW_ABRV_VARIABLE:
st = sym.SDWARFVAR
default:
// Everything else is assigned a type of SDWARFTYPE. that
// this also includes loose ends such as STRUCT_FIELD.
st = sym.SDWARFTYPE
}
ds := d.ldr.LookupOrCreateSym(dwarf.InfoPrefix+name, 0)
dsu := d.ldr.MakeSymbolUpdater(ds)
dsu.SetType(st)
d.ldr.SetAttrNotInSymbolTable(ds, true)
d.ldr.SetAttrReachable(ds, true)
die.Sym = dwSym(ds)
if abbrev >= dwarf.DW_ABRV_NULLTYPE && abbrev <= dwarf.DW_ABRV_TYPEDECL {
d.tmap[name] = ds
}
return die
}
func walktypedef(die *dwarf.DWDie) *dwarf.DWDie {
if die == nil {
return nil
}
// Resolve typedef if present.
if die.Abbrev == dwarf.DW_ABRV_TYPEDECL {
for attr := die.Attr; attr != nil; attr = attr.Link {
if attr.Atr == dwarf.DW_AT_type && attr.Cls == dwarf.DW_CLS_REFERENCE && attr.Data != nil {
return attr.Data.(*dwarf.DWDie)
}
}
}
return die
}
func (d *dwctxt) walksymtypedef(symIdx loader.Sym) loader.Sym {
// We're being given the loader symbol for the type DIE, e.g.
// "go:info.type.uintptr". Map that first to the type symbol (e.g.
// "type:uintptr") and then to the typedef DIE for the type.
// FIXME: this seems clunky, maybe there is a better way to do this.
if ts, ok := d.rtmap[symIdx]; ok {
if def, ok := d.tdmap[ts]; ok {
return def
}
d.linkctxt.Errorf(ts, "internal error: no entry for sym %d in tdmap\n", ts)
return 0
}
d.linkctxt.Errorf(symIdx, "internal error: no entry for sym %d in rtmap\n", symIdx)
return 0
}
// Find child by AT_name using hashtable if available or linear scan
// if not.
func findchild(die *dwarf.DWDie, name string) *dwarf.DWDie {
var prev *dwarf.DWDie
for ; die != prev; prev, die = die, walktypedef(die) {
for a := die.Child; a != nil; a = a.Link {
if name == getattr(a, dwarf.DW_AT_name).Data {
return a
}
}
continue
}
return nil
}
// find looks up the loader symbol for the DWARF DIE generated for the
// type with the specified name.
func (d *dwctxt) find(name string) loader.Sym {
return d.tmap[name]
}
func (d *dwctxt) mustFind(name string) loader.Sym {
r := d.find(name)
if r == 0 {
Exitf("dwarf find: cannot find %s", name)
}
return r
}
func (d *dwctxt) adddwarfref(sb *loader.SymbolBuilder, t loader.Sym, size int) {
switch size {
default:
d.linkctxt.Errorf(sb.Sym(), "invalid size %d in adddwarfref\n", size)
case d.arch.PtrSize, 4:
}
sb.AddSymRef(d.arch, t, 0, objabi.R_DWARFSECREF, size)
}
func (d *dwctxt) newrefattr(die *dwarf.DWDie, attr uint16, ref loader.Sym) {
if ref == 0 {
return
}
newattr(die, attr, dwarf.DW_CLS_REFERENCE, 0, dwSym(ref))
}
func (d *dwctxt) dtolsym(s dwarf.Sym) loader.Sym {
if s == nil {
return 0
}
dws := loader.Sym(s.(dwSym))
return dws
}
func (d *dwctxt) putdie(syms []loader.Sym, die *dwarf.DWDie) []loader.Sym {
s := d.dtolsym(die.Sym)
if s == 0 {
s = syms[len(syms)-1]
} else {
syms = append(syms, s)
}
sDwsym := dwSym(s)
dwarf.Uleb128put(d, sDwsym, int64(die.Abbrev))
dwarf.PutAttrs(d, sDwsym, die.Abbrev, die.Attr)
if dwarf.HasChildren(die) {
for die := die.Child; die != nil; die = die.Link {
syms = d.putdie(syms, die)
}
dsu := d.ldr.MakeSymbolUpdater(syms[len(syms)-1])
dsu.AddUint8(0)
}
return syms
}
func reverselist(list **dwarf.DWDie) {
curr := *list
var prev *dwarf.DWDie
for curr != nil {
next := curr.Link
curr.Link = prev
prev = curr
curr = next
}
*list = prev
}
func reversetree(list **dwarf.DWDie) {
reverselist(list)
for die := *list; die != nil; die = die.Link {
if dwarf.HasChildren(die) {
reversetree(&die.Child)
}
}
}
func newmemberoffsetattr(die *dwarf.DWDie, offs int32) {
newattr(die, dwarf.DW_AT_data_member_location, dwarf.DW_CLS_CONSTANT, int64(offs), nil)
}
func (d *dwctxt) lookupOrDiag(n string) loader.Sym {
symIdx := d.ldr.Lookup(n, 0)
if symIdx == 0 {
Exitf("dwarf: missing type: %s", n)
}
if len(d.ldr.Data(symIdx)) == 0 {
Exitf("dwarf: missing type (no data): %s", n)
}
return symIdx
}
func (d *dwctxt) dotypedef(parent *dwarf.DWDie, name string, def *dwarf.DWDie) *dwarf.DWDie {
// Only emit typedefs for real names.
if strings.HasPrefix(name, "map[") {
return nil
}
if strings.HasPrefix(name, "struct {") {
return nil
}
// cmd/compile uses "noalg.struct {...}" as type name when hash and eq algorithm generation of
// this struct type is suppressed.
if strings.HasPrefix(name, "noalg.struct {") {
return nil
}
if strings.HasPrefix(name, "chan ") {
return nil
}
if name[0] == '[' || name[0] == '*' {
return nil
}
if def == nil {
Errorf("dwarf: bad def in dotypedef")
}
// Create a new loader symbol for the typedef. We no longer
// do lookups of typedef symbols by name, so this is going
// to be an anonymous symbol (we want this for perf reasons).
tds := d.ldr.CreateExtSym("", 0)
tdsu := d.ldr.MakeSymbolUpdater(tds)
tdsu.SetType(sym.SDWARFTYPE)
def.Sym = dwSym(tds)
d.ldr.SetAttrNotInSymbolTable(tds, true)
d.ldr.SetAttrReachable(tds, true)
// The typedef entry must be created after the def,
// so that future lookups will find the typedef instead
// of the real definition. This hooks the typedef into any
// circular definition loops, so that gdb can understand them.
die := d.newdie(parent, dwarf.DW_ABRV_TYPEDECL, name)
d.newrefattr(die, dwarf.DW_AT_type, tds)
return die
}
// Define gotype, for composite ones recurse into constituents.
func (d *dwctxt) defgotype(gotype loader.Sym) loader.Sym {
if gotype == 0 {
return d.mustFind("<unspecified>")
}
// If we already have a tdmap entry for the gotype, return it.
if ds, ok := d.tdmap[gotype]; ok {
return ds
}
sn := d.ldr.SymName(gotype)
if !strings.HasPrefix(sn, "type:") {
d.linkctxt.Errorf(gotype, "dwarf: type name doesn't start with \"type:\"")
return d.mustFind("<unspecified>")
}
name := sn[len("type:"):] // could also decode from Type.string
sdie := d.find(name)
if sdie != 0 {
return sdie
}
gtdwSym := d.newtype(gotype)
d.tdmap[gotype] = loader.Sym(gtdwSym.Sym.(dwSym))
return loader.Sym(gtdwSym.Sym.(dwSym))
}
func (d *dwctxt) newtype(gotype loader.Sym) *dwarf.DWDie {
sn := d.ldr.SymName(gotype)
name := sn[len("type:"):] // could also decode from Type.string
tdata := d.ldr.Data(gotype)
if len(tdata) == 0 {
d.linkctxt.Errorf(gotype, "missing type")
}
kind := decodetypeKind(d.arch, tdata)
bytesize := decodetypeSize(d.arch, tdata)
var die, typedefdie *dwarf.DWDie
switch kind {
case abi.Bool:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_BASETYPE, name)
newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_boolean, 0)
newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
case abi.Int,
abi.Int8,
abi.Int16,
abi.Int32,
abi.Int64:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_BASETYPE, name)
newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_signed, 0)
newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
case abi.Uint,
abi.Uint8,
abi.Uint16,
abi.Uint32,
abi.Uint64,
abi.Uintptr:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_BASETYPE, name)
newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_unsigned, 0)
newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
case abi.Float32,
abi.Float64:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_BASETYPE, name)
newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_float, 0)
newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
case abi.Complex64,
abi.Complex128:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_BASETYPE, name)
newattr(die, dwarf.DW_AT_encoding, dwarf.DW_CLS_CONSTANT, dwarf.DW_ATE_complex_float, 0)
newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
case abi.Array:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_ARRAYTYPE, name)
typedefdie = d.dotypedef(&dwtypes, name, die)
newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
s := decodetypeArrayElem(d.linkctxt, d.arch, gotype)
d.newrefattr(die, dwarf.DW_AT_type, d.defgotype(s))
fld := d.newdie(die, dwarf.DW_ABRV_ARRAYRANGE, "range")
// use actual length not upper bound; correct for 0-length arrays.
newattr(fld, dwarf.DW_AT_count, dwarf.DW_CLS_CONSTANT, decodetypeArrayLen(d.ldr, d.arch, gotype), 0)
d.newrefattr(fld, dwarf.DW_AT_type, d.uintptrInfoSym)
case abi.Chan:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_CHANTYPE, name)
s := decodetypeChanElem(d.ldr, d.arch, gotype)
d.newrefattr(die, dwarf.DW_AT_go_elem, d.defgotype(s))
// Save elem type for synthesizechantypes. We could synthesize here
// but that would change the order of DIEs we output.
d.newrefattr(die, dwarf.DW_AT_type, s)
case abi.Func:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_FUNCTYPE, name)
newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
typedefdie = d.dotypedef(&dwtypes, name, die)
data := d.ldr.Data(gotype)
// FIXME: add caching or reuse reloc slice.
relocs := d.ldr.Relocs(gotype)
nfields := decodetypeFuncInCount(d.arch, data)
for i := 0; i < nfields; i++ {
s := decodetypeFuncInType(d.ldr, d.arch, gotype, &relocs, i)
sn := d.ldr.SymName(s)
fld := d.newdie(die, dwarf.DW_ABRV_FUNCTYPEPARAM, sn[5:])
d.newrefattr(fld, dwarf.DW_AT_type, d.defgotype(s))
}
if decodetypeFuncDotdotdot(d.arch, data) {
d.newdie(die, dwarf.DW_ABRV_DOTDOTDOT, "...")
}
nfields = decodetypeFuncOutCount(d.arch, data)
for i := 0; i < nfields; i++ {
s := decodetypeFuncOutType(d.ldr, d.arch, gotype, &relocs, i)
sn := d.ldr.SymName(s)
fld := d.newdie(die, dwarf.DW_ABRV_FUNCTYPEOUTPARAM, sn[5:])
newattr(fld, dwarf.DW_AT_variable_parameter, dwarf.DW_CLS_FLAG, 1, 0)
d.newrefattr(fld, dwarf.DW_AT_type, d.defgotype(s))
}
case abi.Interface:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_IFACETYPE, name)
typedefdie = d.dotypedef(&dwtypes, name, die)
data := d.ldr.Data(gotype)
nfields := int(decodetypeIfaceMethodCount(d.arch, data))
var s loader.Sym
if nfields == 0 {
s = d.typeRuntimeEface
} else {
s = d.typeRuntimeIface
}
d.newrefattr(die, dwarf.DW_AT_type, d.defgotype(s))
case abi.Map:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_MAPTYPE, name)
s := decodetypeMapKey(d.ldr, d.arch, gotype)
d.newrefattr(die, dwarf.DW_AT_go_key, d.defgotype(s))
s = decodetypeMapValue(d.ldr, d.arch, gotype)
d.newrefattr(die, dwarf.DW_AT_go_elem, d.defgotype(s))
// Save gotype for use in synthesizemaptypes. We could synthesize here,
// but that would change the order of the DIEs.
d.newrefattr(die, dwarf.DW_AT_type, gotype)
case abi.Pointer:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_PTRTYPE, name)
typedefdie = d.dotypedef(&dwtypes, name, die)
s := decodetypePtrElem(d.ldr, d.arch, gotype)
d.newrefattr(die, dwarf.DW_AT_type, d.defgotype(s))
case abi.Slice:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_SLICETYPE, name)
typedefdie = d.dotypedef(&dwtypes, name, die)
newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
s := decodetypeArrayElem(d.linkctxt, d.arch, gotype)
elem := d.defgotype(s)
d.newrefattr(die, dwarf.DW_AT_go_elem, elem)
case abi.String:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_STRINGTYPE, name)
newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
case abi.Struct:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_STRUCTTYPE, name)
typedefdie = d.dotypedef(&dwtypes, name, die)
newattr(die, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, bytesize, 0)
nfields := decodetypeStructFieldCount(d.ldr, d.arch, gotype)
for i := 0; i < nfields; i++ {
f := decodetypeStructFieldName(d.ldr, d.arch, gotype, i)
s := decodetypeStructFieldType(d.linkctxt, d.arch, gotype, i)
if f == "" {
sn := d.ldr.SymName(s)
f = sn[5:] // skip "type:"
}
fld := d.newdie(die, dwarf.DW_ABRV_STRUCTFIELD, f)
d.newrefattr(fld, dwarf.DW_AT_type, d.defgotype(s))
offset := decodetypeStructFieldOffset(d.ldr, d.arch, gotype, i)
newmemberoffsetattr(fld, int32(offset))
if decodetypeStructFieldEmbedded(d.ldr, d.arch, gotype, i) {
newattr(fld, dwarf.DW_AT_go_embedded_field, dwarf.DW_CLS_FLAG, 1, 0)
}
}
case abi.UnsafePointer:
die = d.newdie(&dwtypes, dwarf.DW_ABRV_BARE_PTRTYPE, name)
default:
d.linkctxt.Errorf(gotype, "dwarf: definition of unknown kind %d", kind)
die = d.newdie(&dwtypes, dwarf.DW_ABRV_TYPEDECL, name)
d.newrefattr(die, dwarf.DW_AT_type, d.mustFind("<unspecified>"))
}
newattr(die, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, int64(kind), 0)
if d.ldr.AttrReachable(gotype) {
newattr(die, dwarf.DW_AT_go_runtime_type, dwarf.DW_CLS_GO_TYPEREF, 0, dwSym(gotype))
}
// Sanity check.
if _, ok := d.rtmap[gotype]; ok {
log.Fatalf("internal error: rtmap entry already installed\n")
}
ds := loader.Sym(die.Sym.(dwSym))
if typedefdie != nil {
ds = loader.Sym(typedefdie.Sym.(dwSym))
}
d.rtmap[ds] = gotype
if _, ok := prototypedies[sn]; ok {
prototypedies[sn] = die
}
if typedefdie != nil {
return typedefdie
}
return die
}
func (d *dwctxt) nameFromDIESym(dwtypeDIESym loader.Sym) string {
sn := d.ldr.SymName(dwtypeDIESym)
return sn[len(dwarf.InfoPrefix):]
}
func (d *dwctxt) defptrto(dwtype loader.Sym) loader.Sym {
// FIXME: it would be nice if the compiler attached an aux symbol
// ref from the element type to the pointer type -- it would be
// more efficient to do it this way as opposed to via name lookups.
ptrname := "*" + d.nameFromDIESym(dwtype)
if die := d.find(ptrname); die != 0 {
return die
}
pdie := d.newdie(&dwtypes, dwarf.DW_ABRV_PTRTYPE, ptrname)
d.newrefattr(pdie, dwarf.DW_AT_type, dwtype)
// The DWARF info synthesizes pointer types that don't exist at the
// language level, like *hash<...> and *bucket<...>, and the data
// pointers of slices. Link to the ones we can find.
gts := d.ldr.Lookup("type:"+ptrname, 0)
if gts != 0 && d.ldr.AttrReachable(gts) {
newattr(pdie, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, int64(abi.Pointer), 0)
newattr(pdie, dwarf.DW_AT_go_runtime_type, dwarf.DW_CLS_GO_TYPEREF, 0, dwSym(gts))
}
if gts != 0 {
ds := loader.Sym(pdie.Sym.(dwSym))
d.rtmap[ds] = gts
d.tdmap[gts] = ds
}
return d.dtolsym(pdie.Sym)
}
// Copies src's children into dst. Copies attributes by value.
// DWAttr.data is copied as pointer only. If except is one of
// the top-level children, it will not be copied.
func (d *dwctxt) copychildrenexcept(ctxt *Link, dst *dwarf.DWDie, src *dwarf.DWDie, except *dwarf.DWDie) {
for src = src.Child; src != nil; src = src.Link {
if src == except {
continue
}
c := d.newdie(dst, src.Abbrev, getattr(src, dwarf.DW_AT_name).Data.(string))
for a := src.Attr; a != nil; a = a.Link {
newattr(c, a.Atr, int(a.Cls), a.Value, a.Data)
}
d.copychildrenexcept(ctxt, c, src, nil)
}
reverselist(&dst.Child)
}
func (d *dwctxt) copychildren(ctxt *Link, dst *dwarf.DWDie, src *dwarf.DWDie) {
d.copychildrenexcept(ctxt, dst, src, nil)
}
// Search children (assumed to have TAG_member) for the one named
// field and set its AT_type to dwtype
func (d *dwctxt) substitutetype(structdie *dwarf.DWDie, field string, dwtype loader.Sym) {
child := findchild(structdie, field)
if child == nil {
Exitf("dwarf substitutetype: %s does not have member %s",
getattr(structdie, dwarf.DW_AT_name).Data, field)
return
}
a := getattr(child, dwarf.DW_AT_type)
if a != nil {
a.Data = dwSym(dwtype)
} else {
d.newrefattr(child, dwarf.DW_AT_type, dwtype)
}
}
func (d *dwctxt) findprotodie(ctxt *Link, name string) *dwarf.DWDie {
die, ok := prototypedies[name]
if ok && die == nil {
d.defgotype(d.lookupOrDiag(name))
die = prototypedies[name]
}
if die == nil {
log.Fatalf("internal error: DIE generation failed for %s\n", name)
}
return die
}
func (d *dwctxt) synthesizestringtypes(ctxt *Link, die *dwarf.DWDie) {
prototype := walktypedef(d.findprotodie(ctxt, "type:runtime.stringStructDWARF"))
if prototype == nil {
return
}
for ; die != nil; die = die.Link {
if die.Abbrev != dwarf.DW_ABRV_STRINGTYPE {
continue
}
d.copychildren(ctxt, die, prototype)
}
}
func (d *dwctxt) synthesizeslicetypes(ctxt *Link, die *dwarf.DWDie) {
prototype := walktypedef(d.findprotodie(ctxt, "type:runtime.slice"))
if prototype == nil {
return
}
for ; die != nil; die = die.Link {
if die.Abbrev != dwarf.DW_ABRV_SLICETYPE {
continue
}
d.copychildren(ctxt, die, prototype)
elem := loader.Sym(getattr(die, dwarf.DW_AT_go_elem).Data.(dwSym))
d.substitutetype(die, "array", d.defptrto(elem))
}
}
func mkinternaltypename(base string, arg1 string, arg2 string) string {
if arg2 == "" {
return fmt.Sprintf("%s<%s>", base, arg1)
}
return fmt.Sprintf("%s<%s,%s>", base, arg1, arg2)
}
func (d *dwctxt) mkinternaltype(ctxt *Link, abbrev int, typename, keyname, valname string, f func(*dwarf.DWDie)) loader.Sym {
name := mkinternaltypename(typename, keyname, valname)
symname := dwarf.InfoPrefix + name
s := d.ldr.Lookup(symname, 0)
if s != 0 && d.ldr.SymType(s) == sym.SDWARFTYPE {
return s
}
die := d.newdie(&dwtypes, abbrev, name)
f(die)
return d.dtolsym(die.Sym)
}
func (d *dwctxt) synthesizemaptypes(ctxt *Link, die *dwarf.DWDie) {
if buildcfg.Experiment.SwissMap {
d.synthesizemaptypesSwiss(ctxt, die)
} else {
d.synthesizemaptypesOld(ctxt, die)
}
}
func (d *dwctxt) synthesizemaptypesSwiss(ctxt *Link, die *dwarf.DWDie) {
mapType := walktypedef(d.findprotodie(ctxt, "type:internal/runtime/maps.Map"))
tableType := walktypedef(d.findprotodie(ctxt, "type:internal/runtime/maps.table"))
groupsReferenceType := walktypedef(d.findprotodie(ctxt, "type:internal/runtime/maps.groupsReference"))
for ; die != nil; die = die.Link {
if die.Abbrev != dwarf.DW_ABRV_MAPTYPE {
continue
}
gotype := loader.Sym(getattr(die, dwarf.DW_AT_type).Data.(dwSym))
keyType := decodetypeMapKey(d.ldr, d.arch, gotype)
valType := decodetypeMapValue(d.ldr, d.arch, gotype)
groupType := decodetypeMapSwissGroup(d.ldr, d.arch, gotype)
keyType = d.walksymtypedef(d.defgotype(keyType))
valType = d.walksymtypedef(d.defgotype(valType))
groupType = d.walksymtypedef(d.defgotype(groupType))
keyName := d.nameFromDIESym(keyType)
valName := d.nameFromDIESym(valType)
// Construct groupsReference[K,V]
dwGroupsReference := d.mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "groupReference", keyName, valName, func(dwh *dwarf.DWDie) {
d.copychildren(ctxt, dwh, groupsReferenceType)
// data *group[K,V]
//
// This is actually a pointer to an array
// *[lengthMask+1]group[K,V], but the length is
// variable, so we can't statically record the length.
d.substitutetype(dwh, "data", d.defptrto(groupType))
newattr(dwh, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, getattr(groupsReferenceType, dwarf.DW_AT_byte_size).Value, nil)
newattr(dwh, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, int64(abi.Struct), 0)
})
// Construct table[K,V]
dwTable := d.mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "table", keyName, valName, func(dwh *dwarf.DWDie) {
d.copychildren(ctxt, dwh, tableType)
d.substitutetype(dwh, "groups", dwGroupsReference)
newattr(dwh, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, getattr(tableType, dwarf.DW_AT_byte_size).Value, nil)
newattr(dwh, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, int64(abi.Struct), 0)
})
// Construct map[K,V]
dwMap := d.mkinternaltype(ctxt, dwarf.DW_ABRV_STRUCTTYPE, "map", keyName, valName, func(dwh *dwarf.DWDie) {
d.copychildren(ctxt, dwh, mapType)
// dirPtr is a pointer to a variable-length array of
// *table[K,V], of length dirLen.
//
// Since we can't directly define a variable-length
// array, store this as **table[K,V]. i.e., pointer to
// the first entry in the array.
d.substitutetype(dwh, "dirPtr", d.defptrto(d.defptrto(dwTable)))
newattr(dwh, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, getattr(mapType, dwarf.DW_AT_byte_size).Value, nil)
newattr(dwh, dwarf.DW_AT_go_kind, dwarf.DW_CLS_CONSTANT, int64(abi.Struct), 0)
})
// make map type a pointer to map[K,V]
d.newrefattr(die, dwarf.DW_AT_type, d.defptrto(dwMap))
}
}
func (d *dwctxt) synthesizemaptypesOld(ctxt *Link, die *dwarf.DWDie) {
hash := walktypedef(d.findprotodie(ctxt, "type:runtime.hmap"))
bucket := walktypedef(d.findprotodie(ctxt, "type:runtime.bmap"))
if hash == nil {
return
}
for ; die != nil; die = die.Link {
if die.Abbrev != dwarf.DW_ABRV_MAPTYPE {
continue
}
gotype := loader.Sym(getattr(die, dwarf.DW_AT_type).Data.(dwSym))
keytype := decodetypeMapKey(d.ldr, d.arch, gotype)
valtype := decodetypeMapValue(d.ldr, d.arch, gotype)
keydata := d.ldr.Data(keytype)
valdata := d.ldr.Data(valtype)
keysize, valsize := decodetypeSize(d.arch, keydata), decodetypeSize(d.arch, valdata)
keytype, valtype = d.walksymtypedef(d.defgotype(keytype)), d.walksymtypedef(d.defgotype(valtype))
// compute size info like hashmap.c does.
indirectKey, indirectVal := false, false
if keysize > abi.OldMapMaxKeyBytes {
keysize = int64(d.arch.PtrSize)
indirectKey = true
}
if valsize > abi.OldMapMaxElemBytes {
valsize = int64(d.arch.PtrSize)
indirectVal = true
}
// Construct type to represent an array of BucketSize keys
keyname := d.nameFromDIESym(keytype)
dwhks := d.mkinternaltype(ctxt, dwarf.DW_ABRV_ARRAYTYPE, "[]key", keyname, "", func(dwhk *dwarf.DWDie) {
newattr(dwhk, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, abi.OldMapBucketCount*keysize, 0)
t := keytype
if indirectKey {
t = d.defptrto(keytype)
}
d.newrefattr(dwhk, dwarf.DW_AT_type, t)
fld := d.newdie(dwhk, dwarf.DW_ABRV_ARRAYRANGE, "size")
newattr(fld, dwarf.DW_AT_count, dwarf.DW_CLS_CONSTANT, abi.OldMapBucketCount, 0)
d.newrefattr(fld, dwarf.DW_AT_type, d.uintptrInfoSym)
})
// Construct type to represent an array of BucketSize values
valname := d.nameFromDIESym(valtype)
dwhvs := d.mkinternaltype(ctxt, dwarf.DW_ABRV_ARRAYTYPE, "[]val", valname, "", func(dwhv *dwarf.DWDie) {
newattr(dwhv, dwarf.DW_AT_byte_size, dwarf.DW_CLS_CONSTANT, abi.OldMapBucketCount*valsize, 0)
t := valtype
if indirectVal {
t = d.defptrto(valtype)
}
d.newrefattr(dwhv, dwarf.DW_AT_type, t)
fld := d.newdie(dwhv, dwarf.DW_ABRV_ARRAYRANGE, "size")
newattr(fld, dwarf.DW_AT_count, dwarf.DW_CLS_CONSTANT, abi.OldMapBucketCount, 0)
d.newrefattr(fld, dwarf.DW_AT_type, d.uintptrInfoSym)