1
// Copyright 2010 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
6
// - eliminate DW_CLS_ if not used
7
// - package info in compilation units
8
// - assign global variables and types to their packages
9
// - (upstream) type info for C parts of runtime
10
// - gdb uses c syntax, meaning clumsy quoting is needed for go identifiers. eg
11
// ptype struct '[]uint8' and qualifiers need to be quoted away
12
// - lexical scoping is lost, so gdb gets confused as to which 'main.i' you mean.
13
// - file:line info for variables
14
// - make strings a typedef so prettyprinters can see the underlying string type
18
#include "../ld/dwarf.h"
19
#include "../ld/dwarf_defs.h"
20
#include "../ld/elf.h"
21
#include "../ld/macho.h"
25
* Offsets and sizes of the debug_* sections in the cout file.
29
static vlong abbrevsize;
31
static vlong linesize;
32
static vlong infoo; // also the base for DWDie->offs and reference attributes.
33
static vlong infosize;
35
static vlong framesize;
36
static vlong pubnameso;
37
static vlong pubnamessize;
38
static vlong pubtypeso;
39
static vlong pubtypessize;
40
static vlong arangeso;
41
static vlong arangessize;
42
static vlong gdbscripto;
43
static vlong gdbscriptsize;
45
static char gdbscript[1024];
65
uleb128enc(uvlong v, char* dst)
83
sleb128enc(vlong v, char *dst)
92
if ((v != -1 || !s) && (v != 0 || s))
105
strnput(buf, uleb128enc(v, buf));
112
strnput(buf, sleb128enc(v, buf));
116
* Defining Abbrevs. This is hardcoded, and there will be
117
* only a handful of them. The DWARF spec places no restriction on
118
* the ordering of atributes in the Abbrevs and DIEs, and we will
119
* always write them out in the order of declaration in the abbrev.
120
* This implementation relies on tag, attr < 127, so they serialize as
121
* a char. Higher numbered user-defined tags or attributes can be used
122
* for storing internal data but won't be serialized.
124
typedef struct DWAttrForm DWAttrForm;
130
// Index into the abbrevs table below.
131
// Keep in sync with ispubname() and ispubtype() below.
132
// ispubtype considers >= NULLTYPE public
142
DW_ABRV_FUNCTYPEPARAM,
160
typedef struct DWAbbrev DWAbbrev;
161
static struct DWAbbrev {
165
} abbrevs[DW_NABRV] = {
166
/* The mandatory DW_ABRV_NULL entry. */
170
DW_TAG_compile_unit, DW_CHILDREN_yes,
171
DW_AT_name, DW_FORM_string,
172
DW_AT_language, DW_FORM_data1,
173
DW_AT_low_pc, DW_FORM_addr,
174
DW_AT_high_pc, DW_FORM_addr,
175
DW_AT_stmt_list, DW_FORM_data4,
180
DW_TAG_subprogram, DW_CHILDREN_yes,
181
DW_AT_name, DW_FORM_string,
182
DW_AT_low_pc, DW_FORM_addr,
183
DW_AT_high_pc, DW_FORM_addr,
184
DW_AT_external, DW_FORM_flag,
189
DW_TAG_variable, DW_CHILDREN_no,
190
DW_AT_name, DW_FORM_string,
191
DW_AT_location, DW_FORM_block1,
192
DW_AT_type, DW_FORM_ref_addr,
193
DW_AT_external, DW_FORM_flag,
198
DW_TAG_variable, DW_CHILDREN_no,
199
DW_AT_name, DW_FORM_string,
200
DW_AT_location, DW_FORM_block1,
201
DW_AT_type, DW_FORM_ref_addr,
206
DW_TAG_formal_parameter, DW_CHILDREN_no,
207
DW_AT_name, DW_FORM_string,
208
DW_AT_location, DW_FORM_block1,
209
DW_AT_type, DW_FORM_ref_addr,
214
DW_TAG_member, DW_CHILDREN_no,
215
DW_AT_name, DW_FORM_string,
216
DW_AT_data_member_location, DW_FORM_block1,
217
DW_AT_type, DW_FORM_ref_addr,
222
DW_TAG_formal_parameter, DW_CHILDREN_no,
224
DW_AT_type, DW_FORM_ref_addr,
230
DW_TAG_unspecified_parameters, DW_CHILDREN_no,
235
DW_TAG_subrange_type, DW_CHILDREN_no,
237
DW_AT_type, DW_FORM_ref_addr,
238
DW_AT_upper_bound, DW_FORM_data1,
242
// Below here are the types considered public by ispubtype
245
DW_TAG_unspecified_type, DW_CHILDREN_no,
246
DW_AT_name, DW_FORM_string,
251
DW_TAG_base_type, DW_CHILDREN_no,
252
DW_AT_name, DW_FORM_string,
253
DW_AT_encoding, DW_FORM_data1,
254
DW_AT_byte_size, DW_FORM_data1,
258
// child is subrange with upper bound
260
DW_TAG_array_type, DW_CHILDREN_yes,
261
DW_AT_name, DW_FORM_string,
262
DW_AT_type, DW_FORM_ref_addr,
263
DW_AT_byte_size, DW_FORM_udata,
269
DW_TAG_typedef, DW_CHILDREN_no,
270
DW_AT_name, DW_FORM_string,
271
DW_AT_type, DW_FORM_ref_addr,
277
DW_TAG_subroutine_type, DW_CHILDREN_yes,
278
DW_AT_name, DW_FORM_string,
279
// DW_AT_type, DW_FORM_ref_addr,
285
DW_TAG_typedef, DW_CHILDREN_yes,
286
DW_AT_name, DW_FORM_string,
287
DW_AT_type, DW_FORM_ref_addr,
293
DW_TAG_typedef, DW_CHILDREN_no,
294
DW_AT_name, DW_FORM_string,
295
DW_AT_type, DW_FORM_ref_addr,
301
DW_TAG_pointer_type, DW_CHILDREN_no,
302
DW_AT_name, DW_FORM_string,
303
DW_AT_type, DW_FORM_ref_addr,
309
DW_TAG_structure_type, DW_CHILDREN_yes,
310
DW_AT_name, DW_FORM_string,
311
DW_AT_byte_size, DW_FORM_udata,
317
DW_TAG_structure_type, DW_CHILDREN_yes,
318
DW_AT_name, DW_FORM_string,
319
DW_AT_byte_size, DW_FORM_udata,
325
DW_TAG_structure_type, DW_CHILDREN_yes,
326
DW_AT_name, DW_FORM_string,
327
DW_AT_byte_size, DW_FORM_udata,
333
DW_TAG_typedef, DW_CHILDREN_no,
334
DW_AT_name, DW_FORM_string,
335
DW_AT_type, DW_FORM_ref_addr,
346
for (i = 1; i < DW_NABRV; i++) {
349
uleb128put(abbrevs[i].tag);
350
cput(abbrevs[i].children);
351
// 0 is not a valid attr or form, and DWAbbrev.attr is
352
// 0-terminated, so we can treat it as a string
353
n = strlen((char*)abbrevs[i].attr) / 2;
354
strnput((char*)abbrevs[i].attr,
355
(n+1) * sizeof(DWAttrForm));
358
abbrevsize = cpos() - abbrevo;
362
* Debugging Information Entries and their attributes.
381
// For DW_CLS_string and _block, value should contain the length, and
382
// data the data, for _reference, value is 0 and data is a DWDie* to
383
// the referenced instance, for all others, value is the whole thing
386
typedef struct DWAttr DWAttr;
390
uint8 cls; // DW_CLS_
395
typedef struct DWDie DWDie;
401
// offset into .debug_info section, i.e relative to
402
// infoo. only valid after call to putdie()
404
DWDie **hash; // optional index of children by name, enabled by mkindex()
405
DWDie *hlink; // bucket chain in parent's index
409
* Root DIEs for compilation units, types and global variables.
413
static DWDie dwtypes;
414
static DWDie dwglobals;
417
newattr(DWDie *die, uint8 attr, int cls, vlong value, char *data)
431
// Each DIE (except the root ones) has at least 1 attribute: its
432
// name. getattr moves the desired one to the front so
433
// frequently searched ones are found faster.
435
getattr(DWDie *die, uint8 attr)
439
if (die->attr->atr == attr)
445
if (b->atr == attr) {
457
// Every DIE has at least a DW_AT_name attribute (but it will only be
458
// written out if it is listed in the abbrev). If its parent is
459
// keeping an index, the new DIE will be inserted there.
461
newdie(DWDie *parent, int abbrev, char *name)
466
die = mal(sizeof *die);
467
die->abbrev = abbrev;
468
die->link = parent->child;
471
newattr(die, DW_AT_name, DW_CLS_STRING, strlen(name), name);
475
die->hlink = parent->hash[h];
476
parent->hash[h] = die;
485
die->hash = mal(HASHSIZE * sizeof(DWDie*));
488
// Find child by AT_name using hashtable if available or linear scan
491
find(DWDie *die, char* name)
496
if (die->hash == nil) {
497
for (a = die->child; a != nil; a = a->link)
498
if (strcmp(name, getattr(a, DW_AT_name)->data) == 0)
510
if (strcmp(name, getattr(a, DW_AT_name)->data) == 0)
513
// Move found ones to head of the list.
516
if (strcmp(name, getattr(b, DW_AT_name)->data) == 0) {
518
b->hlink = die->hash[h];
529
find_or_diag(DWDie *die, char* name)
534
diag("dwarf find: %s has no %s", getattr(die, DW_AT_name)->data, name);
539
newrefattr(DWDie *die, uint8 attr, DWDie* ref)
543
return newattr(die, attr, DW_CLS_REFERENCE, 0, (char*)ref);
549
putattr(int form, int cls, vlong value, char *data)
552
case DW_FORM_addr: // address
556
case DW_FORM_block1: // block
563
case DW_FORM_block2: // block
570
case DW_FORM_block4: // block
577
case DW_FORM_block: // block
583
case DW_FORM_data1: // constant
587
case DW_FORM_data2: // constant
591
case DW_FORM_data4: // constant, {line,loclist,mac,rangelist}ptr
595
case DW_FORM_data8: // constant, {line,loclist,mac,rangelist}ptr
599
case DW_FORM_sdata: // constant
603
case DW_FORM_udata: // constant
607
case DW_FORM_string: // string
608
strnput(data, value+1);
611
case DW_FORM_flag: // flag
615
case DW_FORM_ref_addr: // reference to a DIE in the .info section
617
diag("null dwarf reference");
618
LPUT(0); // invalid dwarf, gdb will complain.
620
if (((DWDie*)data)->offs == 0)
622
LPUT(((DWDie*)data)->offs);
626
case DW_FORM_ref1: // reference within the compilation unit
627
case DW_FORM_ref2: // reference
628
case DW_FORM_ref4: // reference
629
case DW_FORM_ref8: // reference
630
case DW_FORM_ref_udata: // reference
632
case DW_FORM_strp: // string
633
case DW_FORM_indirect: // (see Section 7.5.3)
635
diag("Unsupported atribute form %d / class %d", form, cls);
640
// Note that we can (and do) add arbitrary attributes to a DIE, but
641
// only the ones actually listed in the Abbrev will be written out.
643
putattrs(int abbrev, DWAttr* attr)
645
DWAttr *attrs[DW_AT_recursive + 1];
648
memset(attrs, 0, sizeof attrs);
649
for( ; attr; attr = attr->link)
650
if (attr->atr < nelem(attrs))
651
attrs[attr->atr] = attr;
653
for(af = abbrevs[abbrev].attr; af->attr; af++)
656
attrs[af->attr]->cls,
657
attrs[af->attr]->value,
658
attrs[af->attr]->data);
660
putattr(af->form, 0, 0, 0);
663
static void putdie(DWDie* die);
668
for(; die; die = die->link)
675
die->offs = cpos() - infoo;
676
uleb128put(die->abbrev);
677
putattrs(die->abbrev, die->attr);
678
if (abbrevs[die->abbrev].children) {
685
reverselist(DWDie** list)
692
DWDie* next = curr->link;
701
reversetree(DWDie** list)
706
for (die = *list; die != nil; die = die->link)
707
if (abbrevs[die->abbrev].children)
708
reversetree(&die->child);
712
newmemberoffsetattr(DWDie *die, int32 offs)
719
block[i++] = DW_OP_consts;
720
i += sleb128enc(offs, block+i);
721
block[i++] = DW_OP_plus;
723
newattr(die, DW_AT_data_member_location, DW_CLS_BLOCK, i, mal(i));
724
memmove(die->attr->data, block, i);
727
// GDB doesn't like DW_FORM_addr for DW_AT_location, so emit a
728
// location expression that evals to a const.
730
newabslocexprattr(DWDie *die, vlong addr)
736
block[i++] = DW_OP_constu;
737
i += uleb128enc(addr, block+i);
738
newattr(die, DW_AT_location, DW_CLS_BLOCK, i, mal(i));
739
memmove(die->attr->data, block, i);
742
// Decoding the type.* symbols. This has to be in sync with
743
// ../../pkg/runtime/type.go, or more specificaly, with what
744
// ../gc/reflect.c stuffs in these.
774
KindNoPointers = 1<<7,
776
// size of Type interface header + CommonType structure.
777
CommonSize = 2*PtrSize+ 4*PtrSize + 8,
781
decode_reloc(Sym *s, int32 off)
785
for (i = 0; i < s->nr; i++)
786
if (s->r[i].off == off)
792
decode_reloc_sym(Sym *s, int32 off)
796
r = decode_reloc(s,off);
803
decode_inuxi(uchar* p, int sz)
827
diag("decode inuxi %d", sz);
830
for (i = 0; i < sz; i++)
831
cast[inuxi[i]] = p[i];
837
// Type.commonType.kind
839
decodetype_kind(Sym *s)
841
return s->p[3*PtrSize + 7] & ~KindNoPointers; // 0x13 / 0x1f
844
// Type.commonType.size
846
decodetype_size(Sym *s)
848
return decode_inuxi(s->p + 2*PtrSize, PtrSize); // 0x8 / 0x10
851
// Type.ArrayType.elem and Type.SliceType.Elem
853
decodetype_arrayelem(Sym *s)
855
return decode_reloc_sym(s, CommonSize); // 0x1c / 0x30
859
decodetype_arraylen(Sym *s)
861
return decode_inuxi(s->p + CommonSize+PtrSize, PtrSize);
866
decodetype_ptrelem(Sym *s)
868
return decode_reloc_sym(s, CommonSize); // 0x1c / 0x30
871
// Type.MapType.key, elem
873
decodetype_mapkey(Sym *s)
875
return decode_reloc_sym(s, CommonSize); // 0x1c / 0x30
878
decodetype_mapvalue(Sym *s)
880
return decode_reloc_sym(s, CommonSize+PtrSize); // 0x20 / 0x38
883
// Type.ChanType.elem
885
decodetype_chanelem(Sym *s)
887
return decode_reloc_sym(s, CommonSize); // 0x1c / 0x30
890
// Type.FuncType.dotdotdot
892
decodetype_funcdotdotdot(Sym *s)
894
return s->p[CommonSize];
897
// Type.FuncType.in.len
899
decodetype_funcincount(Sym *s)
901
return decode_inuxi(s->p + CommonSize+2*PtrSize, 4);
905
decodetype_funcoutcount(Sym *s)
907
return decode_inuxi(s->p + CommonSize+3*PtrSize + 2*4, 4);
911
decodetype_funcintype(Sym *s, int i)
915
r = decode_reloc(s, CommonSize + PtrSize);
918
return decode_reloc_sym(r->sym, r->add + i * PtrSize);
922
decodetype_funcouttype(Sym *s, int i)
926
r = decode_reloc(s, CommonSize + 2*PtrSize + 2*4);
929
return decode_reloc_sym(r->sym, r->add + i * PtrSize);
932
// Type.StructType.fields.Slice::len
934
decodetype_structfieldcount(Sym *s)
936
return decode_inuxi(s->p + CommonSize + PtrSize, 4);
940
StructFieldSize = 5*PtrSize
942
// Type.StructType.fields[]-> name, typ and offset.
944
decodetype_structfieldname(Sym *s, int i)
946
// go.string."foo" 0x28 / 0x40
947
s = decode_reloc_sym(s, CommonSize + PtrSize + 2*4 + i*StructFieldSize);
948
if (s == nil) // embedded structs have a nil name.
950
s = decode_reloc_sym(s, 0); // string."foo"
951
if (s == nil) // shouldn't happen.
953
return (char*)s->p; // the c-string
957
decodetype_structfieldtype(Sym *s, int i)
959
return decode_reloc_sym(s, CommonSize + PtrSize + 2*4 + i*StructFieldSize + 2*PtrSize);
963
decodetype_structfieldoffs(Sym *s, int i)
965
return decode_inuxi(s->p + CommonSize + PtrSize + 2*4 + i*StructFieldSize + 4*PtrSize, 4);
968
// InterfaceTYpe.methods.len
970
decodetype_ifacemethodcount(Sym *s)
972
return decode_inuxi(s->p + CommonSize + PtrSize, 4);
976
// Fake attributes for slices, maps and channel
978
DW_AT_internal_elem_type = 250, // channels and slices
979
DW_AT_internal_key_type = 251, // maps
980
DW_AT_internal_val_type = 252, // maps
981
DW_AT_internal_location = 253, // params and locals
984
static DWDie* defptrto(DWDie *dwtype); // below
986
// Lookup predefined types
988
lookup_or_diag(char *n)
994
diag("dwarf: missing type: %s", n);
1000
// Define gotype, for composite ones recurse into constituents.
1002
defgotype(Sym *gotype)
1012
return find_or_diag(&dwtypes, "<unspecified>");
1014
if (strncmp("type.", gotype->name, 5) != 0) {
1015
diag("Type name doesn't start with \".type\": %s", gotype->name);
1016
return find_or_diag(&dwtypes, "<unspecified>");
1018
name = gotype->name + 5; // could also decode from Type.string
1020
die = find(&dwtypes, name);
1024
if (0 && debug['v'] > 2) {
1025
print("new type: %s @0x%08x [%d]", gotype->name, gotype->value, gotype->size);
1026
for (i = 0; i < gotype->size; i++) {
1027
if (!(i%8)) print("\n\t%04x ", i);
1028
print("%02x ", gotype->p[i]);
1031
for (i = 0; i < gotype->nr; i++) {
1032
print("\t0x%02x[%x] %d %s[%llx]\n",
1036
gotype->r[i].sym->name,
1037
(vlong)gotype->r[i].add);
1041
kind = decodetype_kind(gotype);
1042
bytesize = decodetype_size(gotype);
1046
die = newdie(&dwtypes, DW_ABRV_BASETYPE, name);
1047
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_boolean, 0);
1048
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0);
1056
die = newdie(&dwtypes, DW_ABRV_BASETYPE, name);
1057
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_signed, 0);
1058
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0);
1067
die = newdie(&dwtypes, DW_ABRV_BASETYPE, name);
1068
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_unsigned, 0);
1069
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0);
1074
die = newdie(&dwtypes, DW_ABRV_BASETYPE, name);
1075
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_float, 0);
1076
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0);
1080
case KindComplex128:
1081
die = newdie(&dwtypes, DW_ABRV_BASETYPE, name);
1082
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_complex_float, 0);
1083
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0);
1087
die = newdie(&dwtypes, DW_ABRV_ARRAYTYPE, name);
1088
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0);
1089
s = decodetype_arrayelem(gotype);
1090
newrefattr(die, DW_AT_type, defgotype(s));
1091
fld = newdie(die, DW_ABRV_ARRAYRANGE, "range");
1092
newattr(fld, DW_AT_upper_bound, DW_CLS_CONSTANT, decodetype_arraylen(gotype), 0);
1093
newrefattr(fld, DW_AT_type, find_or_diag(&dwtypes, "uintptr"));
1097
die = newdie(&dwtypes, DW_ABRV_CHANTYPE, name);
1098
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0);
1099
s = decodetype_chanelem(gotype);
1100
newrefattr(die, DW_AT_internal_elem_type, defgotype(s));
1104
die = newdie(&dwtypes, DW_ABRV_FUNCTYPE, name);
1105
newrefattr(die, DW_AT_type, find_or_diag(&dwtypes, "void"));
1106
nfields = decodetype_funcincount(gotype);
1107
for (i = 0; i < nfields; i++) {
1108
s = decodetype_funcintype(gotype, i);
1109
fld = newdie(die, DW_ABRV_FUNCTYPEPARAM, s->name+5);
1110
newrefattr(fld, DW_AT_type, defgotype(s));
1112
if (decodetype_funcdotdotdot(gotype))
1113
newdie(die, DW_ABRV_DOTDOTDOT, "...");
1114
nfields = decodetype_funcoutcount(gotype);
1115
for (i = 0; i < nfields; i++) {
1116
s = decodetype_funcouttype(gotype, i);
1117
fld = newdie(die, DW_ABRV_FUNCTYPEPARAM, s->name+5);
1118
newrefattr(fld, DW_AT_type, defptrto(defgotype(s)));
1120
die = defptrto(die);
1124
die = newdie(&dwtypes, DW_ABRV_IFACETYPE, name);
1125
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0);
1126
nfields = decodetype_ifacemethodcount(gotype);
1128
s = lookup_or_diag("type.runtime.eface");
1130
s = lookup_or_diag("type.runtime.iface");
1131
newrefattr(die, DW_AT_type, defgotype(s));
1135
die = newdie(&dwtypes, DW_ABRV_MAPTYPE, name);
1136
s = decodetype_mapkey(gotype);
1137
newrefattr(die, DW_AT_internal_key_type, defgotype(s));
1138
s = decodetype_mapvalue(gotype);
1139
newrefattr(die, DW_AT_internal_val_type, defgotype(s));
1143
die = newdie(&dwtypes, DW_ABRV_PTRTYPE, name);
1144
s = decodetype_ptrelem(gotype);
1145
newrefattr(die, DW_AT_type, defgotype(s));
1149
die = newdie(&dwtypes, DW_ABRV_SLICETYPE, name);
1150
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0);
1151
s = decodetype_arrayelem(gotype);
1152
newrefattr(die, DW_AT_internal_elem_type, defgotype(s));
1156
die = newdie(&dwtypes, DW_ABRV_STRINGTYPE, name);
1157
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0);
1161
die = newdie(&dwtypes, DW_ABRV_STRUCTTYPE, name);
1162
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0);
1163
nfields = decodetype_structfieldcount(gotype);
1164
for (i = 0; i < nfields; i++) {
1165
f = decodetype_structfieldname(gotype, i);
1166
s = decodetype_structfieldtype(gotype, i);
1168
f = s->name + 5; // skip "type."
1169
fld = newdie(die, DW_ABRV_STRUCTFIELD, f);
1170
newrefattr(fld, DW_AT_type, defgotype(s));
1171
newmemberoffsetattr(fld, decodetype_structfieldoffs(gotype, i));
1175
case KindUnsafePointer:
1176
die = newdie(&dwtypes, DW_ABRV_PTRTYPE, name);
1177
newrefattr(die, DW_AT_type, find(&dwtypes, "void"));
1181
diag("definition of unknown kind %d: %s", kind, gotype->name);
1182
die = newdie(&dwtypes, DW_ABRV_TYPEDECL, name);
1183
newrefattr(die, DW_AT_type, find_or_diag(&dwtypes, "<unspecified>"));
1189
// Find or construct *T given T.
1191
defptrto(DWDie *dwtype)
1196
snprint(ptrname, sizeof ptrname, "*%s", getattr(dwtype, DW_AT_name)->data);
1197
die = find(&dwtypes, ptrname);
1199
die = newdie(&dwtypes, DW_ABRV_PTRTYPE,
1200
strcpy(mal(strlen(ptrname)+1), ptrname));
1201
newrefattr(die, DW_AT_type, dwtype);
1206
// Copies src's children into dst. Copies attributes by value.
1207
// DWAttr.data is copied as pointer only.
1209
copychildren(DWDie *dst, DWDie *src)
1214
for (src = src->child; src != nil; src = src->link) {
1215
c = newdie(dst, src->abbrev, getattr(src, DW_AT_name)->data);
1216
for (a = src->attr; a != nil; a = a->link)
1217
newattr(c, a->atr, a->cls, a->value, a->data);
1218
copychildren(c, src);
1220
reverselist(&dst->child);
1223
// Search children (assumed to have DW_TAG_member) for the one named
1224
// field and set it's DW_AT_type to dwtype
1226
substitutetype(DWDie *structdie, char *field, DWDie* dwtype)
1231
child = find_or_diag(structdie, field);
1235
a = getattr(child, DW_AT_type);
1237
a->data = (char*) dwtype;
1239
newrefattr(child, DW_AT_type, dwtype);
1243
synthesizestringtypes(DWDie* die)
1247
prototype = defgotype(lookup_or_diag("type.runtime._string"));
1248
if (prototype == nil)
1251
for (; die != nil; die = die->link) {
1252
if (die->abbrev != DW_ABRV_STRINGTYPE)
1254
copychildren(die, prototype);
1259
synthesizeslicetypes(DWDie *die)
1261
DWDie *prototype, *elem;
1263
prototype = defgotype(lookup_or_diag("type.runtime.slice"));
1264
if (prototype == nil)
1267
for (; die != nil; die = die->link) {
1268
if (die->abbrev != DW_ABRV_SLICETYPE)
1270
copychildren(die, prototype);
1271
elem = (DWDie*) getattr(die, DW_AT_internal_elem_type)->data;
1272
substitutetype(die, "array", defptrto(elem));
1277
mkinternaltypename(char *base, char *arg1, char *arg2)
1283
snprint(buf, sizeof buf, "%s<%s>", base, arg1);
1285
snprint(buf, sizeof buf, "%s<%s,%s>", base, arg1, arg2);
1286
n = mal(strlen(buf) + 1);
1287
memmove(n, buf, strlen(buf));
1292
// synthesizemaptypes is way too closely married to runtime/hashmap.c
1294
MaxValsize = 256 - 64
1298
synthesizemaptypes(DWDie *die)
1301
DWDie *hash, *hash_subtable, *hash_entry,
1302
*dwh, *dwhs, *dwhe, *dwhash, *keytype, *valtype, *fld;
1303
int hashsize, keysize, valsize, datsize, valsize_in_hash, datavo;
1306
hash = defgotype(lookup_or_diag("type.runtime.hmap"));
1307
hash_subtable = defgotype(lookup_or_diag("type.runtime.hash_subtable"));
1308
hash_entry = defgotype(lookup_or_diag("type.runtime.hash_entry"));
1310
if (hash == nil || hash_subtable == nil || hash_entry == nil)
1313
dwhash = (DWDie*)getattr(find_or_diag(hash_entry, "hash"), DW_AT_type)->data;
1317
hashsize = getattr(dwhash, DW_AT_byte_size)->value;
1319
for (; die != nil; die = die->link) {
1320
if (die->abbrev != DW_ABRV_MAPTYPE)
1323
keytype = (DWDie*) getattr(die, DW_AT_internal_key_type)->data;
1324
valtype = (DWDie*) getattr(die, DW_AT_internal_val_type)->data;
1326
a = getattr(keytype, DW_AT_byte_size);
1327
keysize = a ? a->value : PtrSize; // We don't store size with Pointers
1329
a = getattr(valtype, DW_AT_byte_size);
1330
valsize = a ? a->value : PtrSize;
1332
// This is what happens in hash_init and makemap_c
1333
valsize_in_hash = valsize;
1334
if (valsize > MaxValsize)
1335
valsize_in_hash = PtrSize;
1337
if (valsize_in_hash >= PtrSize)
1338
datavo = rnd(keysize, PtrSize);
1339
datsize = datavo + valsize_in_hash;
1340
if (datsize < PtrSize)
1342
datsize = rnd(datsize, PtrSize);
1344
// Construct struct hash_entry<K,V>
1345
dwhe = newdie(&dwtypes, DW_ABRV_STRUCTTYPE,
1346
mkinternaltypename("hash_entry",
1347
getattr(keytype, DW_AT_name)->data,
1348
getattr(valtype, DW_AT_name)->data));
1350
fld = newdie(dwhe, DW_ABRV_STRUCTFIELD, "hash");
1351
newrefattr(fld, DW_AT_type, dwhash);
1352
newmemberoffsetattr(fld, 0);
1354
fld = newdie(dwhe, DW_ABRV_STRUCTFIELD, "key");
1355
newrefattr(fld, DW_AT_type, keytype);
1356
newmemberoffsetattr(fld, hashsize);
1358
fld = newdie(dwhe, DW_ABRV_STRUCTFIELD, "val");
1359
if (valsize > MaxValsize)
1360
valtype = defptrto(valtype);
1361
newrefattr(fld, DW_AT_type, valtype);
1362
newmemberoffsetattr(fld, hashsize + datavo);
1363
newattr(dwhe, DW_AT_byte_size, DW_CLS_CONSTANT, hashsize + datsize, NULL);
1365
// Construct hash_subtable<hash_entry<K,V>>
1366
dwhs = newdie(&dwtypes, DW_ABRV_STRUCTTYPE,
1367
mkinternaltypename("hash_subtable",
1368
getattr(keytype, DW_AT_name)->data,
1369
getattr(valtype, DW_AT_name)->data));
1370
copychildren(dwhs, hash_subtable);
1371
substitutetype(dwhs, "end", defptrto(dwhe));
1372
substitutetype(dwhs, "entry", dwhe); // todo: []hash_entry with dynamic size
1373
newattr(dwhs, DW_AT_byte_size, DW_CLS_CONSTANT,
1374
getattr(hash_subtable, DW_AT_byte_size)->value, NULL);
1376
// Construct hash<K,V>
1377
dwh = newdie(&dwtypes, DW_ABRV_STRUCTTYPE,
1378
mkinternaltypename("hash",
1379
getattr(keytype, DW_AT_name)->data,
1380
getattr(valtype, DW_AT_name)->data));
1381
copychildren(dwh, hash);
1382
substitutetype(dwh, "st", defptrto(dwhs));
1383
newattr(dwh, DW_AT_byte_size, DW_CLS_CONSTANT,
1384
getattr(hash, DW_AT_byte_size)->value, NULL);
1386
newrefattr(die, DW_AT_type, defptrto(dwh));
1391
synthesizechantypes(DWDie *die)
1393
DWDie *sudog, *waitq, *link, *hchan,
1394
*dws, *dww, *dwl, *dwh, *elemtype;
1396
int elemsize, linksize, sudogsize;
1398
sudog = defgotype(lookup_or_diag("type.runtime.sudog"));
1399
waitq = defgotype(lookup_or_diag("type.runtime.waitq"));
1400
link = defgotype(lookup_or_diag("type.runtime.link"));
1401
hchan = defgotype(lookup_or_diag("type.runtime.hchan"));
1402
if (sudog == nil || waitq == nil || link == nil || hchan == nil)
1405
sudogsize = getattr(sudog, DW_AT_byte_size)->value;
1406
linksize = getattr(link, DW_AT_byte_size)->value;
1408
for (; die != nil; die = die->link) {
1409
if (die->abbrev != DW_ABRV_CHANTYPE)
1411
elemtype = (DWDie*) getattr(die, DW_AT_internal_elem_type)->data;
1412
a = getattr(elemtype, DW_AT_byte_size);
1413
elemsize = a ? a->value : PtrSize;
1416
dws = newdie(&dwtypes, DW_ABRV_STRUCTTYPE,
1417
mkinternaltypename("sudog",
1418
getattr(elemtype, DW_AT_name)->data, NULL));
1419
copychildren(dws, sudog);
1420
substitutetype(dws, "elem", elemtype);
1421
newattr(dws, DW_AT_byte_size, DW_CLS_CONSTANT,
1422
sudogsize + (elemsize > 8 ? elemsize - 8 : 0), NULL);
1425
dww = newdie(&dwtypes, DW_ABRV_STRUCTTYPE,
1426
mkinternaltypename("waitq", getattr(elemtype, DW_AT_name)->data, NULL));
1427
copychildren(dww, waitq);
1428
substitutetype(dww, "first", defptrto(dws));
1429
substitutetype(dww, "last", defptrto(dws));
1430
newattr(dww, DW_AT_byte_size, DW_CLS_CONSTANT,
1431
getattr(waitq, DW_AT_byte_size)->value, NULL);
1434
dwl = newdie(&dwtypes, DW_ABRV_STRUCTTYPE,
1435
mkinternaltypename("link", getattr(elemtype, DW_AT_name)->data, NULL));
1436
copychildren(dwl, link);
1437
substitutetype(dwl, "link", defptrto(dwl));
1438
substitutetype(dwl, "elem", elemtype);
1439
newattr(dwl, DW_AT_byte_size, DW_CLS_CONSTANT,
1440
linksize + (elemsize > 8 ? elemsize - 8 : 0), NULL);
1443
dwh = newdie(&dwtypes, DW_ABRV_STRUCTTYPE,
1444
mkinternaltypename("hchan", getattr(elemtype, DW_AT_name)->data, NULL));
1445
copychildren(dwh, hchan);
1446
substitutetype(dwh, "senddataq", defptrto(dwl));
1447
substitutetype(dwh, "recvdataq", defptrto(dwl));
1448
substitutetype(dwh, "recvq", dww);
1449
substitutetype(dwh, "sendq", dww);
1450
substitutetype(dwh, "free", dws);
1451
newattr(dwh, DW_AT_byte_size, DW_CLS_CONSTANT,
1452
getattr(hchan, DW_AT_byte_size)->value, NULL);
1454
newrefattr(die, DW_AT_type, defptrto(dwh));
1458
// For use with pass.c::genasmsym
1460
defdwsymb(Sym* sym, char *s, int t, vlong v, vlong size, int ver, Sym *gotype)
1464
if (strncmp(s, "go.string.", 10) == 0)
1466
if (strncmp(s, "string.", 7) == 0)
1468
if (strncmp(s, "type._.", 7) == 0)
1471
if (strncmp(s, "type.", 5) == 0) {
1485
dv = newdie(&dwglobals, DW_ABRV_VARIABLE, s);
1486
newabslocexprattr(dv, v);
1488
newattr(dv, DW_AT_external, DW_CLS_FLAG, 1, 0);
1492
dt = defgotype(gotype);
1496
newrefattr(dv, DW_AT_type, dt);
1499
// TODO(lvd) For now, just append them all to the first compilation
1500
// unit (that should be main), in the future distribute them to the
1501
// appropriate compilation units.
1503
movetomodule(DWDie *parent)
1507
for (die = dwroot.child->child; die->link != nil; die = die->link) /* nix */;
1508
die->link = parent->child;
1512
* Filename fragments for the line history stack.
1516
static int ftabsize;
1519
dwarfaddfrag(int n, char *frag)
1523
if (n >= ftabsize) {
1525
ftabsize = 1 + n + (n >> 2);
1526
ftab = realloc(ftab, ftabsize * sizeof(ftab[0]));
1527
memset(ftab + s, 0, (ftabsize - s) * sizeof(ftab[0]));
1535
// Returns a malloc'ed string, piecewise copied from the ftab.
1544
ss = s + 1; // first is 0
1545
while((o = ((uint8)ss[0] << 8) | (uint8)ss[1]) != 0) {
1546
if (o < 0 || o >= ftabsize) {
1547
diag("corrupt z entry");
1552
diag("corrupt z entry");
1555
len += strlen(f) + 1; // for the '/'
1562
r = malloc(len + 1);
1567
while((o = ((uint8)s[0] << 8) | (uint8)s[1]) != 0) {
1569
if (rb == r || rb[-1] == '/')
1570
rb = seprint(rb, re, "%s", f);
1572
rb = seprint(rb, re, "/%s", f);
1579
* The line history itself
1582
static char **histfile; // [0] holds "<eof>", DW_LNS_set_file arguments must be > 0.
1583
static int histfilesize;
1584
static int histfilecap;
1591
// [0] holds "<eof>"
1592
for (i = 1; i < histfilesize; i++)
1598
addhistfile(char *zentry)
1602
if (histfilesize == histfilecap) {
1603
histfilecap = 2 * histfilecap + 2;
1604
histfile = realloc(histfile, histfilecap * sizeof(char*));
1606
if (histfilesize == 0)
1607
histfile[histfilesize++] = "<eof>";
1609
fname = decodez(zentry);
1612
// Don't fill with duplicates (check only top one).
1613
if (strcmp(fname, histfile[histfilesize-1]) == 0) {
1615
return histfilesize - 1;
1617
histfile[histfilesize++] = fname;
1618
return histfilesize - 1;
1621
// if the histfile stack contains ..../runtime/runtime_defs.go
1622
// use that to set gdbscript
1624
finddebugruntimepath()
1629
for (i = 1; i < histfilesize; i++) {
1630
if ((c = strstr(histfile[i], "runtime/runtime_defs.go")) != nil) {
1631
l = c - histfile[i];
1632
memmove(gdbscript, histfile[i], l);
1633
memmove(gdbscript + l, "runtime/runtime-gdb.py", strlen("runtime/runtime-gdb.py") + 1);
1639
// Go's runtime C sources are sane, and Go sources nest only 1 level,
1640
// so 16 should be plenty.
1645
static int includetop;
1646
static vlong absline;
1648
typedef struct Linehist Linehist;
1656
static Linehist *linehist;
1663
if (includetop < 0) {
1664
diag("corrupt z stack");
1667
if (includetop >= nelem(includestack)) {
1668
diag("nesting too deep");
1669
for (i = 0; i < nelem(includestack); i++)
1670
diag("\t%s", histfile[includestack[i].file]);
1676
* Return false if the a->link chain contains no history, otherwise
1677
* returns true and finds z and Z entries in the Auto list (of a
1678
* Prog), and resets the history stack
1685
for (; a; a = a->link)
1686
if (a->type == D_FILE)
1691
// We have a new history. They are guaranteed to come completely
1692
// at the beginning of the compilation unit.
1693
if (a->aoffset != 1) {
1694
diag("stray 'z' with offset %d", a->aoffset);
1698
// Clear the history.
1701
includestack[includetop].file = 0;
1702
includestack[includetop].line = -1;
1704
while (linehist != nil) {
1705
lh = linehist->link;
1710
// Construct the new one.
1711
for (; a; a = a->link) {
1712
if (a->type == D_FILE) { // 'z'
1713
int f = addhistfile(a->asym->name);
1714
if (f < 0) { // pop file
1717
} else if(f != includestack[includetop].file) { // pushed a new file
1718
includestack[includetop].line += a->aoffset - absline;
1721
includestack[includetop].file = f;
1722
includestack[includetop].line = 1;
1724
absline = a->aoffset;
1725
} else if (a->type == D_FILE1) { // 'Z'
1726
// We could just fixup the current
1727
// linehist->line, but there doesn't appear to
1728
// be a guarantee that every 'Z' is preceded
1729
// by it's own 'z', so do the safe thing and
1730
// update the stack and push a new Linehist
1732
includestack[includetop].line = a->aoffset;
1735
if (linehist == 0 || linehist->absline != absline) {
1736
Linehist* lh = malloc(sizeof *lh);
1737
lh->link = linehist;
1738
lh->absline = absline;
1741
linehist->file = includestack[includetop].file;
1742
linehist->line = includestack[includetop].line;
1748
searchhist(vlong absline)
1752
for (lh = linehist; lh; lh = lh->link)
1753
if (lh->absline <= absline)
1761
if(strlen(s) >= 3 && strcmp(s+strlen(s)-3, ".go") == 0)
1768
* Generate short opcodes when possible, long ones when neccesary.
1779
putpclcdelta(vlong delta_pc, vlong delta_lc)
1781
if (LINE_BASE <= delta_lc && delta_lc < LINE_BASE+LINE_RANGE) {
1782
vlong opcode = OPCODE_BASE + (delta_lc - LINE_BASE) + (LINE_RANGE * delta_pc);
1783
if (OPCODE_BASE <= opcode && opcode < 256) {
1790
cput(DW_LNS_advance_pc);
1791
sleb128put(delta_pc);
1794
cput(DW_LNS_advance_line);
1795
sleb128put(delta_lc);
1800
newcfaoffsetattr(DWDie *die, int32 offs)
1807
block[i++] = DW_OP_call_frame_cfa;
1809
block[i++] = DW_OP_consts;
1810
i += sleb128enc(offs, block+i);
1811
block[i++] = DW_OP_plus;
1813
newattr(die, DW_AT_location, DW_CLS_BLOCK, i, mal(i));
1814
memmove(die->attr->data, block, i);
1818
mkvarname(char* name, int da)
1823
snprint(buf, sizeof buf, "%s#%d", name, da);
1824
n = mal(strlen(buf) + 1);
1825
memmove(n, buf, strlen(buf));
1830
* Walk prog table, emit line program and build DIE tree.
1833
// flush previous compilation unit.
1835
flushunit(DWDie *dwinfo, vlong pc, vlong unitstart)
1839
if (dwinfo != nil && pc != 0) {
1840
newattr(dwinfo, DW_AT_high_pc, DW_CLS_ADDRESS, pc+1, 0);
1843
if (unitstart >= 0) {
1844
cput(0); // start extended opcode
1846
cput(DW_LNE_end_sequence);
1850
seek(cout, unitstart, 0);
1851
LPUT(here - unitstart - sizeof(int32));
1853
seek(cout, here, 0);
1863
vlong unitstart, offs;
1864
vlong pc, epc, lc, llc, lline;
1866
int i, lang, da, dt;
1868
DWDie *dwinfo, *dwfunc, *dwvar, **dws;
1869
DWDie *varhash[HASHSIZE];
1880
for(cursym = textp; cursym != nil; cursym = cursym->next) {
1885
// Look for history stack. If we find one,
1886
// we're entering a new compilation unit
1888
if (inithist(s->autom)) {
1889
flushunit(dwinfo, epc, unitstart);
1892
if(debug['v'] > 1) {
1893
print("dwarf writelines found %s\n", histfile[1]);
1895
for (lh = linehist; lh; lh = lh->link)
1896
print("\t%8lld: [%4lld]%s\n",
1897
lh->absline, lh->line, histfile[lh->file]);
1900
lang = guesslang(histfile[1]);
1901
finddebugruntimepath();
1903
dwinfo = newdie(&dwroot, DW_ABRV_COMPUNIT, strdup(histfile[1]));
1904
newattr(dwinfo, DW_AT_language, DW_CLS_CONSTANT,lang, 0);
1905
newattr(dwinfo, DW_AT_stmt_list, DW_CLS_PTR, unitstart - lineo, 0);
1906
newattr(dwinfo, DW_AT_low_pc, DW_CLS_ADDRESS, s->text->pc, 0);
1908
// Write .debug_line Line Number Program Header (sec 6.2.4)
1909
// Fields marked with (*) must be changed for 64-bit dwarf
1910
LPUT(0); // unit_length (*), will be filled in later.
1911
WPUT(3); // dwarf version (appendix F)
1912
LPUT(11); // header_length (*), starting here.
1914
cput(1); // minimum_instruction_length
1915
cput(1); // default_is_stmt
1916
cput(LINE_BASE); // line_base
1917
cput(LINE_RANGE); // line_range
1918
cput(OPCODE_BASE); // opcode_base (we only use 1..4)
1919
cput(0); // standard_opcode_lengths[1]
1920
cput(1); // standard_opcode_lengths[2]
1921
cput(1); // standard_opcode_lengths[3]
1922
cput(1); // standard_opcode_lengths[4]
1923
cput(0); // include_directories (empty)
1924
cput(0); // file_names (empty) (emitted by DW_LNE's below)
1925
// header_length ends here.
1927
for (i=1; i < histfilesize; i++) {
1928
cput(0); // start extended opcode
1929
uleb128put(1 + strlen(histfile[i]) + 4);
1930
cput(DW_LNE_define_file);
1931
strnput(histfile[i], strlen(histfile[i]) + 4);
1932
// 4 zeros: the string termination + 3 fields.
1935
epc = pc = s->text->pc;
1940
cput(0); // start extended opcode
1941
uleb128put(1 + PtrSize);
1942
cput(DW_LNE_set_address);
1948
if (unitstart < 0) {
1949
diag("reachable code before seeing any history: %P", s->text);
1953
dwfunc = newdie(dwinfo, DW_ABRV_FUNCTION, s->name);
1954
newattr(dwfunc, DW_AT_low_pc, DW_CLS_ADDRESS, s->value, 0);
1955
epc = s->value + s->size;
1956
newattr(dwfunc, DW_AT_high_pc, DW_CLS_ADDRESS, epc, 0);
1957
if (s->version == 0)
1958
newattr(dwfunc, DW_AT_external, DW_CLS_FLAG, 1, 0);
1960
if(s->text->link == nil)
1963
for(q = s->text; q != P; q = q->link) {
1964
lh = searchhist(q->line);
1966
diag("corrupt history or bad absolute line: %P", q);
1970
if (lh->file < 1) { // 0 is the past-EOF entry.
1971
// diag("instruction with line number past EOF in %s: %P", histfile[1], q);
1975
lline = lh->line + q->line - lh->absline;
1977
print("%6llux %s[%lld] %P\n", (vlong)q->pc, histfile[lh->file], lline, q);
1981
if (currfile != lh->file) {
1982
currfile = lh->file;
1983
cput(DW_LNS_set_file);
1984
uleb128put(currfile);
1986
putpclcdelta(q->pc - pc, lline - llc);
1993
dwfunc->hash = varhash; // enable indexing of children by name
1994
memset(varhash, 0, sizeof varhash);
1995
for(a = s->autom; a; a = a->link) {
1999
offs = a->aoffset - PtrSize;
2008
if (strstr(a->asym->name, ".autotmp_"))
2010
if (find(dwfunc, a->asym->name) != nil)
2011
n = mkvarname(a->asym->name, da);
2014
// Drop the package prefix from locals and arguments.
2015
nn = strrchr(n, '.');
2019
dwvar = newdie(dwfunc, dt, n);
2020
newcfaoffsetattr(dwvar, offs);
2021
newrefattr(dwvar, DW_AT_type, defgotype(a->gotype));
2023
// push dwvar down dwfunc->child to preserve order
2024
newattr(dwvar, DW_AT_internal_location, DW_CLS_CONSTANT, offs, NULL);
2025
dwfunc->child = dwvar->link; // take dwvar out from the top of the list
2026
for (dws = &dwfunc->child; *dws != nil; dws = &(*dws)->link)
2027
if (offs > getattr(*dws, DW_AT_internal_location)->value)
2038
flushunit(dwinfo, epc, unitstart);
2039
linesize = cpos() - lineo;
2048
DATAALIGNMENTFACTOR = -4, // TODO -PtrSize?
2049
FAKERETURNCOLUMN = 16 // TODO gdb6 doesnt like > 15?
2053
putpccfadelta(vlong deltapc, vlong cfa)
2055
if (deltapc < 0x40) {
2056
cput(DW_CFA_advance_loc + deltapc);
2057
} else if (deltapc < 0x100) {
2058
cput(DW_CFA_advance_loc1);
2060
} else if (deltapc < 0x10000) {
2061
cput(DW_CFA_advance_loc2);
2064
cput(DW_CFA_advance_loc4);
2068
cput(DW_CFA_def_cfa_offset_sf);
2069
sleb128put(cfa / DATAALIGNMENTFACTOR);
2077
vlong fdeo, fdesize, pad, cfa, pc;
2081
// Emit the CIE, Section 6.4.1
2082
LPUT(CIERESERVE); // initial length, must be multiple of PtrSize
2083
LPUT(0xffffffff); // cid.
2084
cput(3); // dwarf version (appendix F)
2085
cput(0); // augmentation ""
2086
uleb128put(1); // code_alignment_factor
2087
sleb128put(DATAALIGNMENTFACTOR); // guess
2088
uleb128put(FAKERETURNCOLUMN); // return_address_register
2090
cput(DW_CFA_def_cfa);
2091
uleb128put(DWARFREGSP); // register SP (**ABI-dependent, defined in l.h)
2092
uleb128put(PtrSize); // offset
2094
cput(DW_CFA_offset + FAKERETURNCOLUMN); // return address
2095
uleb128put(-PtrSize / DATAALIGNMENTFACTOR); // at cfa - x*4
2097
// 4 is to exclude the length field.
2098
pad = CIERESERVE + frameo + 4 - cpos();
2100
diag("CIERESERVE too small by %lld bytes.", -pad);
2105
for(cursym = textp; cursym != nil; cursym = cursym->next) {
2111
// Emit a FDE, Section 6.4.1, starting wit a placeholder.
2112
LPUT(0); // length, must be multiple of PtrSize
2113
LPUT(0); // Pointer to the CIE above, at offset 0
2114
addrput(0); // initial location
2115
addrput(0); // address range
2117
cfa = PtrSize; // CFA starts at sp+PtrSize
2121
for(q = p; q->link != P; q = q->link) {
2125
putpccfadelta(q->link->pc - pc, cfa);
2129
fdesize = cpos() - fdeo - 4; // exclude the length field.
2130
pad = rnd(fdesize, PtrSize) - fdesize;
2135
// Emit the FDE header for real, Section 6.4.1.
2136
seek(cout, fdeo, 0);
2143
seek(cout, fdeo + 4 + fdesize, 0);
2147
framesize = cpos() - frameo;
2151
* Walk DWarfDebugInfoEntries, and emit .debug_info
2155
COMPUNITHEADERSIZE = 4+2+4+1
2162
vlong unitstart, here;
2166
for (compunit = dwroot.child; compunit; compunit = compunit->link) {
2169
// Write .debug_info Compilation Unit Header (sec 7.5.1)
2170
// Fields marked with (*) must be changed for 64-bit dwarf
2171
// This must match COMPUNITHEADERSIZE above.
2172
LPUT(0); // unit_length (*), will be filled in later.
2173
WPUT(3); // dwarf version (appendix F)
2174
LPUT(0); // debug_abbrev_offset (*)
2175
cput(PtrSize); // address_size
2181
seek(cout, unitstart, 0);
2182
LPUT(here - unitstart - 4); // exclude the length field.
2184
seek(cout, here, 0);
2190
* Emit .debug_pubnames/_types. _info must have been written before,
2191
* because we need die->offs and infoo/infosize;
2194
ispubname(DWDie *die) {
2197
switch(die->abbrev) {
2198
case DW_ABRV_FUNCTION:
2199
case DW_ABRV_VARIABLE:
2200
a = getattr(die, DW_AT_external);
2201
return a && a->value;
2207
ispubtype(DWDie *die) {
2208
return die->abbrev >= DW_ABRV_NULLTYPE;
2212
writepub(int (*ispub)(DWDie*))
2214
DWDie *compunit, *die;
2216
vlong unitstart, unitend, sectionstart, here;
2218
sectionstart = cpos();
2220
for (compunit = dwroot.child; compunit != nil; compunit = compunit->link) {
2221
unitstart = compunit->offs - COMPUNITHEADERSIZE;
2222
if (compunit->link != nil)
2223
unitend = compunit->link->offs - COMPUNITHEADERSIZE;
2225
unitend = infoo + infosize;
2227
// Write .debug_pubnames/types Header (sec 6.1.1)
2228
LPUT(0); // unit_length (*), will be filled in later.
2229
WPUT(2); // dwarf version (appendix F)
2230
LPUT(unitstart); // debug_info_offset (of the Comp unit Header)
2231
LPUT(unitend - unitstart); // debug_info_length
2233
for (die = compunit->child; die != nil; die = die->link) {
2234
if (!ispub(die)) continue;
2235
LPUT(die->offs - unitstart);
2236
dwa = getattr(die, DW_AT_name);
2237
strnput(dwa->data, dwa->value + 1);
2243
seek(cout, sectionstart, 0);
2244
LPUT(here - sectionstart - 4); // exclude the length field.
2246
seek(cout, here, 0);
2250
return sectionstart;
2254
* emit .debug_aranges. _info must have been written before,
2255
* because we need die->offs of dw_globals.
2265
sectionstart = cpos();
2266
headersize = rnd(4+2+4+1+1, PtrSize); // don't count unit_length field itself
2268
for (compunit = dwroot.child; compunit != nil; compunit = compunit->link) {
2269
b = getattr(compunit, DW_AT_low_pc);
2272
e = getattr(compunit, DW_AT_high_pc);
2276
// Write .debug_aranges Header + entry (sec 6.1.2)
2277
LPUT(headersize + 4*PtrSize - 4); // unit_length (*)
2278
WPUT(2); // dwarf version (appendix F)
2279
LPUT(compunit->offs - COMPUNITHEADERSIZE); // debug_info_offset
2280
cput(PtrSize); // address_size
2281
cput(0); // segment_size
2282
strnput("", headersize - (4+2+4+1+1)); // align to PtrSize
2285
addrput(e->value - b->value);
2290
return sectionstart;
2294
writegdbscript(void)
2298
sectionstart = cpos();
2301
cput(1); // magic 1 byte?
2302
strnput(gdbscript, strlen(gdbscript)+1);
2305
return sectionstart;
2311
if(HEADTYPE == Hwindows) // Only Windows PE need section align.
2312
strnput("", rnd(size, PEFILEALIGN) - size);
2316
* This is the main entry point for generating dwarf. After emitting
2317
* the mandatory debug_abbrev section, it calls writelines() to set up
2318
* the per-compilation unit part of the DIE tree, while simultaneously
2319
* emitting the debug_line section. When the final tree contains
2320
* forward references, it will write the debug_info section in 2
2325
dwarfemitdebugsections(void)
2330
// For diagnostic messages.
2331
newattr(&dwtypes, DW_AT_name, DW_CLS_STRING, strlen("dwtypes"), "dwtypes");
2335
mkindex(&dwglobals);
2337
// Some types that must exist to define other ones.
2338
newdie(&dwtypes, DW_ABRV_NULLTYPE, "<unspecified>");
2339
newdie(&dwtypes, DW_ABRV_NULLTYPE, "void");
2340
newrefattr(newdie(&dwtypes, DW_ABRV_PTRTYPE, "unsafe.Pointer"),
2341
DW_AT_type, find(&dwtypes, "void"));
2342
die = newdie(&dwtypes, DW_ABRV_BASETYPE, "uintptr"); // needed for array size
2343
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_unsigned, 0);
2344
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, PtrSize, 0);
2346
// Needed by the prettyprinter code for interface inspection.
2347
defgotype(lookup_or_diag("type.runtime.commonType"));
2348
defgotype(lookup_or_diag("type.runtime.InterfaceType"));
2349
defgotype(lookup_or_diag("type.runtime.itab"));
2351
genasmsym(defdwsymb);
2360
synthesizestringtypes(dwtypes.child);
2361
synthesizeslicetypes(dwtypes.child);
2362
synthesizemaptypes(dwtypes.child);
2363
synthesizechantypes(dwtypes.child);
2365
reversetree(&dwroot.child);
2366
reversetree(&dwtypes.child);
2367
reversetree(&dwglobals.child);
2369
movetomodule(&dwtypes);
2370
movetomodule(&dwglobals);
2374
gdbscripto = arangeso = pubtypeso = pubnameso = infoe = cpos();
2378
Bprint(&bso, "%5.2f dwarf pass 2.\n", cputime());
2379
seek(cout, infoo, 0);
2382
diag("unresolved references after first dwarf info pass");
2385
if (infoe != cpos()) {
2386
diag("inconsistent second dwarf info pass");
2390
infosize = infoe - infoo;
2393
pubnameso = writepub(ispubname);
2394
pubnamessize = cpos() - pubnameso;
2395
align(pubnamessize);
2397
pubtypeso = writepub(ispubtype);
2398
pubtypessize = cpos() - pubtypeso;
2399
align(pubtypessize);
2401
arangeso = writearanges();
2402
arangessize = cpos() - arangeso;
2405
gdbscripto = writegdbscript();
2406
gdbscriptsize = cpos() - gdbscripto;
2407
align(gdbscriptsize);
2422
ElfStrDebugPubNames,
2423
ElfStrDebugPubTypes,
2430
vlong elfstrdbg[NElfStrDbg];
2433
dwarfaddshstrings(Sym *shstrtab)
2435
elfstrdbg[ElfStrDebugAbbrev] = addstring(shstrtab, ".debug_abbrev");
2436
elfstrdbg[ElfStrDebugAranges] = addstring(shstrtab, ".debug_aranges");
2437
elfstrdbg[ElfStrDebugFrame] = addstring(shstrtab, ".debug_frame");
2438
elfstrdbg[ElfStrDebugInfo] = addstring(shstrtab, ".debug_info");
2439
elfstrdbg[ElfStrDebugLine] = addstring(shstrtab, ".debug_line");
2440
elfstrdbg[ElfStrDebugLoc] = addstring(shstrtab, ".debug_loc");
2441
elfstrdbg[ElfStrDebugMacinfo] = addstring(shstrtab, ".debug_macinfo");
2442
elfstrdbg[ElfStrDebugPubNames] = addstring(shstrtab, ".debug_pubnames");
2443
elfstrdbg[ElfStrDebugPubTypes] = addstring(shstrtab, ".debug_pubtypes");
2444
elfstrdbg[ElfStrDebugRanges] = addstring(shstrtab, ".debug_ranges");
2445
elfstrdbg[ElfStrDebugStr] = addstring(shstrtab, ".debug_str");
2446
elfstrdbg[ElfStrGDBScripts] = addstring(shstrtab, ".debug_gdb_scripts");
2450
dwarfaddelfheaders(void)
2454
sh = newElfShdr(elfstrdbg[ElfStrDebugAbbrev]);
2455
sh->type = SHT_PROGBITS;
2457
sh->size = abbrevsize;
2460
sh = newElfShdr(elfstrdbg[ElfStrDebugLine]);
2461
sh->type = SHT_PROGBITS;
2463
sh->size = linesize;
2466
sh = newElfShdr(elfstrdbg[ElfStrDebugFrame]);
2467
sh->type = SHT_PROGBITS;
2469
sh->size = framesize;
2472
sh = newElfShdr(elfstrdbg[ElfStrDebugInfo]);
2473
sh->type = SHT_PROGBITS;
2475
sh->size = infosize;
2478
if (pubnamessize > 0) {
2479
sh = newElfShdr(elfstrdbg[ElfStrDebugPubNames]);
2480
sh->type = SHT_PROGBITS;
2481
sh->off = pubnameso;
2482
sh->size = pubnamessize;
2486
if (pubtypessize > 0) {
2487
sh = newElfShdr(elfstrdbg[ElfStrDebugPubTypes]);
2488
sh->type = SHT_PROGBITS;
2489
sh->off = pubtypeso;
2490
sh->size = pubtypessize;
2495
sh = newElfShdr(elfstrdbg[ElfStrDebugAranges]);
2496
sh->type = SHT_PROGBITS;
2498
sh->size = arangessize;
2502
if (gdbscriptsize) {
2503
sh = newElfShdr(elfstrdbg[ElfStrGDBScripts]);
2504
sh->type = SHT_PROGBITS;
2505
sh->off = gdbscripto;
2506
sh->size = gdbscriptsize;
2515
dwarfaddmachoheaders(void)
2522
// Zero vsize segments won't be loaded in memory, even so they
2523
// have to be page aligned in the file.
2524
fakestart = abbrevo & ~0xfff;
2527
if (pubnamessize > 0)
2529
if (pubtypessize > 0)
2531
if (arangessize > 0)
2533
if (gdbscriptsize > 0)
2536
ms = newMachoSeg("__DWARF", nsect);
2537
ms->fileoffset = fakestart;
2538
ms->filesize = abbrevo-fakestart;
2540
msect = newMachoSect(ms, "__debug_abbrev");
2541
msect->off = abbrevo;
2542
msect->size = abbrevsize;
2543
ms->filesize += msect->size;
2545
msect = newMachoSect(ms, "__debug_line");
2547
msect->size = linesize;
2548
ms->filesize += msect->size;
2550
msect = newMachoSect(ms, "__debug_frame");
2551
msect->off = frameo;
2552
msect->size = framesize;
2553
ms->filesize += msect->size;
2555
msect = newMachoSect(ms, "__debug_info");
2557
msect->size = infosize;
2558
ms->filesize += msect->size;
2560
if (pubnamessize > 0) {
2561
msect = newMachoSect(ms, "__debug_pubnames");
2562
msect->off = pubnameso;
2563
msect->size = pubnamessize;
2564
ms->filesize += msect->size;
2567
if (pubtypessize > 0) {
2568
msect = newMachoSect(ms, "__debug_pubtypes");
2569
msect->off = pubtypeso;
2570
msect->size = pubtypessize;
2571
ms->filesize += msect->size;
2574
if (arangessize > 0) {
2575
msect = newMachoSect(ms, "__debug_aranges");
2576
msect->off = arangeso;
2577
msect->size = arangessize;
2578
ms->filesize += msect->size;
2581
// TODO(lvd) fix gdb/python to load MachO (16 char section name limit)
2582
if (gdbscriptsize > 0) {
2583
msect = newMachoSect(ms, "__debug_gdb_scripts");
2584
msect->off = gdbscripto;
2585
msect->size = gdbscriptsize;
2586
ms->filesize += msect->size;
2594
dwarfaddpeheaders(void)
2596
dwarfemitdebugsections();
2597
newPEDWARFSection(".debug_abbrev", abbrevsize);
2598
newPEDWARFSection(".debug_line", linesize);
2599
newPEDWARFSection(".debug_frame", framesize);
2600
newPEDWARFSection(".debug_info", infosize);
2601
if (pubnamessize > 0)
2602
newPEDWARFSection(".debug_pubnames", pubnamessize);
2603
if (pubtypessize > 0)
2604
newPEDWARFSection(".debug_pubtypes", pubtypessize);
2605
if (arangessize > 0)
2606
newPEDWARFSection(".debug_aranges", arangessize);
2607
if (gdbscriptsize > 0)
2608
newPEDWARFSection(".debug_gdb_scripts", gdbscriptsize);