github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/cmd/internal/dwarf/dwarf.go (about) 1 // Copyright 2016 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. 4 5 // Package dwarf generates DWARF debugging information. 6 // DWARF generation is split between the compiler and the linker, 7 // this package contains the shared code. 8 package dwarf 9 10 import ( 11 "fmt" 12 ) 13 14 // InfoPrefix is the prefix for all the symbols containing DWARF info entries. 15 const InfoPrefix = "go.info." 16 17 // Sym represents a symbol. 18 type Sym interface { 19 } 20 21 // A Var represents a local variable or a function parameter. 22 type Var struct { 23 Name string 24 Abbrev int // Either DW_ABRV_AUTO or DW_ABRV_PARAM 25 Offset int32 26 Type Sym 27 } 28 29 // A Context specifies how to add data to a Sym. 30 type Context interface { 31 PtrSize() int 32 AddInt(s Sym, size int, i int64) 33 AddBytes(s Sym, b []byte) 34 AddAddress(s Sym, t interface{}, ofs int64) 35 AddSectionOffset(s Sym, size int, t interface{}, ofs int64) 36 AddString(s Sym, v string) 37 SymValue(s Sym) int64 38 } 39 40 // AppendUleb128 appends v to b using DWARF's unsigned LEB128 encoding. 41 func AppendUleb128(b []byte, v uint64) []byte { 42 for { 43 c := uint8(v & 0x7f) 44 v >>= 7 45 if v != 0 { 46 c |= 0x80 47 } 48 b = append(b, c) 49 if c&0x80 == 0 { 50 break 51 } 52 } 53 return b 54 } 55 56 // AppendSleb128 appends v to b using DWARF's signed LEB128 encoding. 57 func AppendSleb128(b []byte, v int64) []byte { 58 for { 59 c := uint8(v & 0x7f) 60 s := uint8(v & 0x40) 61 v >>= 7 62 if (v != -1 || s == 0) && (v != 0 || s != 0) { 63 c |= 0x80 64 } 65 b = append(b, c) 66 if c&0x80 == 0 { 67 break 68 } 69 } 70 return b 71 } 72 73 // sevenbits contains all unsigned seven bit numbers, indexed by their value. 74 var sevenbits = [...]byte{ 75 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 76 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 77 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 78 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 79 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 80 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 81 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 82 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 83 } 84 85 // sevenBitU returns the unsigned LEB128 encoding of v if v is seven bits and nil otherwise. 86 // The contents of the returned slice must not be modified. 87 func sevenBitU(v int64) []byte { 88 if uint64(v) < uint64(len(sevenbits)) { 89 return sevenbits[v : v+1] 90 } 91 return nil 92 } 93 94 // sevenBitS returns the signed LEB128 encoding of v if v is seven bits and nil otherwise. 95 // The contents of the returned slice must not be modified. 96 func sevenBitS(v int64) []byte { 97 if uint64(v) <= 63 { 98 return sevenbits[v : v+1] 99 } 100 if uint64(-v) <= 64 { 101 return sevenbits[128+v : 128+v+1] 102 } 103 return nil 104 } 105 106 // Uleb128put appends v to s using DWARF's unsigned LEB128 encoding. 107 func Uleb128put(ctxt Context, s Sym, v int64) { 108 b := sevenBitU(v) 109 if b == nil { 110 var encbuf [20]byte 111 b = AppendUleb128(encbuf[:0], uint64(v)) 112 } 113 ctxt.AddBytes(s, b) 114 } 115 116 // Sleb128put appends v to s using DWARF's signed LEB128 encoding. 117 func Sleb128put(ctxt Context, s Sym, v int64) { 118 b := sevenBitS(v) 119 if b == nil { 120 var encbuf [20]byte 121 b = AppendSleb128(encbuf[:0], v) 122 } 123 ctxt.AddBytes(s, b) 124 } 125 126 /* 127 * Defining Abbrevs. This is hardcoded, and there will be 128 * only a handful of them. The DWARF spec places no restriction on 129 * the ordering of attributes in the Abbrevs and DIEs, and we will 130 * always write them out in the order of declaration in the abbrev. 131 */ 132 type dwAttrForm struct { 133 attr uint16 134 form uint8 135 } 136 137 // Go-specific type attributes. 138 const ( 139 DW_AT_go_kind = 0x2900 140 DW_AT_go_key = 0x2901 141 DW_AT_go_elem = 0x2902 142 // Attribute for DW_TAG_member of a struct type. 143 // Nonzero value indicates the struct field is an embedded field. 144 DW_AT_go_embedded_field = 0x2903 145 146 DW_AT_internal_location = 253 // params and locals; not emitted 147 ) 148 149 // Index into the abbrevs table below. 150 // Keep in sync with ispubname() and ispubtype() below. 151 // ispubtype considers >= NULLTYPE public 152 const ( 153 DW_ABRV_NULL = iota 154 DW_ABRV_COMPUNIT 155 DW_ABRV_FUNCTION 156 DW_ABRV_VARIABLE 157 DW_ABRV_AUTO 158 DW_ABRV_PARAM 159 DW_ABRV_STRUCTFIELD 160 DW_ABRV_FUNCTYPEPARAM 161 DW_ABRV_DOTDOTDOT 162 DW_ABRV_ARRAYRANGE 163 DW_ABRV_NULLTYPE 164 DW_ABRV_BASETYPE 165 DW_ABRV_ARRAYTYPE 166 DW_ABRV_CHANTYPE 167 DW_ABRV_FUNCTYPE 168 DW_ABRV_IFACETYPE 169 DW_ABRV_MAPTYPE 170 DW_ABRV_PTRTYPE 171 DW_ABRV_BARE_PTRTYPE // only for void*, no DW_AT_type attr to please gdb 6. 172 DW_ABRV_SLICETYPE 173 DW_ABRV_STRINGTYPE 174 DW_ABRV_STRUCTTYPE 175 DW_ABRV_TYPEDECL 176 DW_NABRV 177 ) 178 179 type dwAbbrev struct { 180 tag uint8 181 children uint8 182 attr []dwAttrForm 183 } 184 185 var abbrevs = [DW_NABRV]dwAbbrev{ 186 /* The mandatory DW_ABRV_NULL entry. */ 187 {0, 0, []dwAttrForm{}}, 188 189 /* COMPUNIT */ 190 { 191 DW_TAG_compile_unit, 192 DW_CHILDREN_yes, 193 []dwAttrForm{ 194 {DW_AT_name, DW_FORM_string}, 195 {DW_AT_language, DW_FORM_data1}, 196 {DW_AT_low_pc, DW_FORM_addr}, 197 {DW_AT_high_pc, DW_FORM_addr}, 198 {DW_AT_stmt_list, DW_FORM_data4}, 199 {DW_AT_comp_dir, DW_FORM_string}, 200 }, 201 }, 202 203 /* FUNCTION */ 204 { 205 DW_TAG_subprogram, 206 DW_CHILDREN_yes, 207 []dwAttrForm{ 208 {DW_AT_name, DW_FORM_string}, 209 {DW_AT_low_pc, DW_FORM_addr}, 210 {DW_AT_high_pc, DW_FORM_addr}, 211 {DW_AT_external, DW_FORM_flag}, 212 }, 213 }, 214 215 /* VARIABLE */ 216 { 217 DW_TAG_variable, 218 DW_CHILDREN_no, 219 []dwAttrForm{ 220 {DW_AT_name, DW_FORM_string}, 221 {DW_AT_location, DW_FORM_block1}, 222 {DW_AT_type, DW_FORM_ref_addr}, 223 {DW_AT_external, DW_FORM_flag}, 224 }, 225 }, 226 227 /* AUTO */ 228 { 229 DW_TAG_variable, 230 DW_CHILDREN_no, 231 []dwAttrForm{ 232 {DW_AT_name, DW_FORM_string}, 233 {DW_AT_location, DW_FORM_block1}, 234 {DW_AT_type, DW_FORM_ref_addr}, 235 }, 236 }, 237 238 /* PARAM */ 239 { 240 DW_TAG_formal_parameter, 241 DW_CHILDREN_no, 242 []dwAttrForm{ 243 {DW_AT_name, DW_FORM_string}, 244 {DW_AT_location, DW_FORM_block1}, 245 {DW_AT_type, DW_FORM_ref_addr}, 246 }, 247 }, 248 249 /* STRUCTFIELD */ 250 { 251 DW_TAG_member, 252 DW_CHILDREN_no, 253 []dwAttrForm{ 254 {DW_AT_name, DW_FORM_string}, 255 {DW_AT_data_member_location, DW_FORM_block1}, 256 {DW_AT_type, DW_FORM_ref_addr}, 257 {DW_AT_go_embedded_field, DW_FORM_flag}, 258 }, 259 }, 260 261 /* FUNCTYPEPARAM */ 262 { 263 DW_TAG_formal_parameter, 264 DW_CHILDREN_no, 265 266 // No name! 267 []dwAttrForm{ 268 {DW_AT_type, DW_FORM_ref_addr}, 269 }, 270 }, 271 272 /* DOTDOTDOT */ 273 { 274 DW_TAG_unspecified_parameters, 275 DW_CHILDREN_no, 276 []dwAttrForm{}, 277 }, 278 279 /* ARRAYRANGE */ 280 { 281 DW_TAG_subrange_type, 282 DW_CHILDREN_no, 283 284 // No name! 285 []dwAttrForm{ 286 {DW_AT_type, DW_FORM_ref_addr}, 287 {DW_AT_count, DW_FORM_udata}, 288 }, 289 }, 290 291 // Below here are the types considered public by ispubtype 292 /* NULLTYPE */ 293 { 294 DW_TAG_unspecified_type, 295 DW_CHILDREN_no, 296 []dwAttrForm{ 297 {DW_AT_name, DW_FORM_string}, 298 }, 299 }, 300 301 /* BASETYPE */ 302 { 303 DW_TAG_base_type, 304 DW_CHILDREN_no, 305 []dwAttrForm{ 306 {DW_AT_name, DW_FORM_string}, 307 {DW_AT_encoding, DW_FORM_data1}, 308 {DW_AT_byte_size, DW_FORM_data1}, 309 {DW_AT_go_kind, DW_FORM_data1}, 310 }, 311 }, 312 313 /* ARRAYTYPE */ 314 // child is subrange with upper bound 315 { 316 DW_TAG_array_type, 317 DW_CHILDREN_yes, 318 []dwAttrForm{ 319 {DW_AT_name, DW_FORM_string}, 320 {DW_AT_type, DW_FORM_ref_addr}, 321 {DW_AT_byte_size, DW_FORM_udata}, 322 {DW_AT_go_kind, DW_FORM_data1}, 323 }, 324 }, 325 326 /* CHANTYPE */ 327 { 328 DW_TAG_typedef, 329 DW_CHILDREN_no, 330 []dwAttrForm{ 331 {DW_AT_name, DW_FORM_string}, 332 {DW_AT_type, DW_FORM_ref_addr}, 333 {DW_AT_go_kind, DW_FORM_data1}, 334 {DW_AT_go_elem, DW_FORM_ref_addr}, 335 }, 336 }, 337 338 /* FUNCTYPE */ 339 { 340 DW_TAG_subroutine_type, 341 DW_CHILDREN_yes, 342 []dwAttrForm{ 343 {DW_AT_name, DW_FORM_string}, 344 // {DW_AT_type, DW_FORM_ref_addr}, 345 {DW_AT_go_kind, DW_FORM_data1}, 346 }, 347 }, 348 349 /* IFACETYPE */ 350 { 351 DW_TAG_typedef, 352 DW_CHILDREN_yes, 353 []dwAttrForm{ 354 {DW_AT_name, DW_FORM_string}, 355 {DW_AT_type, DW_FORM_ref_addr}, 356 {DW_AT_go_kind, DW_FORM_data1}, 357 }, 358 }, 359 360 /* MAPTYPE */ 361 { 362 DW_TAG_typedef, 363 DW_CHILDREN_no, 364 []dwAttrForm{ 365 {DW_AT_name, DW_FORM_string}, 366 {DW_AT_type, DW_FORM_ref_addr}, 367 {DW_AT_go_kind, DW_FORM_data1}, 368 {DW_AT_go_key, DW_FORM_ref_addr}, 369 {DW_AT_go_elem, DW_FORM_ref_addr}, 370 }, 371 }, 372 373 /* PTRTYPE */ 374 { 375 DW_TAG_pointer_type, 376 DW_CHILDREN_no, 377 []dwAttrForm{ 378 {DW_AT_name, DW_FORM_string}, 379 {DW_AT_type, DW_FORM_ref_addr}, 380 {DW_AT_go_kind, DW_FORM_data1}, 381 }, 382 }, 383 384 /* BARE_PTRTYPE */ 385 { 386 DW_TAG_pointer_type, 387 DW_CHILDREN_no, 388 []dwAttrForm{ 389 {DW_AT_name, DW_FORM_string}, 390 }, 391 }, 392 393 /* SLICETYPE */ 394 { 395 DW_TAG_structure_type, 396 DW_CHILDREN_yes, 397 []dwAttrForm{ 398 {DW_AT_name, DW_FORM_string}, 399 {DW_AT_byte_size, DW_FORM_udata}, 400 {DW_AT_go_kind, DW_FORM_data1}, 401 {DW_AT_go_elem, DW_FORM_ref_addr}, 402 }, 403 }, 404 405 /* STRINGTYPE */ 406 { 407 DW_TAG_structure_type, 408 DW_CHILDREN_yes, 409 []dwAttrForm{ 410 {DW_AT_name, DW_FORM_string}, 411 {DW_AT_byte_size, DW_FORM_udata}, 412 {DW_AT_go_kind, DW_FORM_data1}, 413 }, 414 }, 415 416 /* STRUCTTYPE */ 417 { 418 DW_TAG_structure_type, 419 DW_CHILDREN_yes, 420 []dwAttrForm{ 421 {DW_AT_name, DW_FORM_string}, 422 {DW_AT_byte_size, DW_FORM_udata}, 423 {DW_AT_go_kind, DW_FORM_data1}, 424 }, 425 }, 426 427 /* TYPEDECL */ 428 { 429 DW_TAG_typedef, 430 DW_CHILDREN_no, 431 []dwAttrForm{ 432 {DW_AT_name, DW_FORM_string}, 433 {DW_AT_type, DW_FORM_ref_addr}, 434 }, 435 }, 436 } 437 438 // GetAbbrev returns the contents of the .debug_abbrev section. 439 func GetAbbrev() []byte { 440 var buf []byte 441 for i := 1; i < DW_NABRV; i++ { 442 // See section 7.5.3 443 buf = AppendUleb128(buf, uint64(i)) 444 buf = AppendUleb128(buf, uint64(abbrevs[i].tag)) 445 buf = append(buf, byte(abbrevs[i].children)) 446 for _, f := range abbrevs[i].attr { 447 buf = AppendUleb128(buf, uint64(f.attr)) 448 buf = AppendUleb128(buf, uint64(f.form)) 449 } 450 buf = append(buf, 0, 0) 451 } 452 return append(buf, 0) 453 } 454 455 /* 456 * Debugging Information Entries and their attributes. 457 */ 458 459 // DWAttr represents an attribute of a DWDie. 460 // 461 // For DW_CLS_string and _block, value should contain the length, and 462 // data the data, for _reference, value is 0 and data is a DWDie* to 463 // the referenced instance, for all others, value is the whole thing 464 // and data is null. 465 type DWAttr struct { 466 Link *DWAttr 467 Atr uint16 // DW_AT_ 468 Cls uint8 // DW_CLS_ 469 Value int64 470 Data interface{} 471 } 472 473 // DWDie represents a DWARF debug info entry. 474 type DWDie struct { 475 Abbrev int 476 Link *DWDie 477 Child *DWDie 478 Attr *DWAttr 479 Sym Sym 480 } 481 482 func putattr(ctxt Context, s Sym, abbrev int, form int, cls int, value int64, data interface{}) error { 483 switch form { 484 case DW_FORM_addr: // address 485 ctxt.AddAddress(s, data, value) 486 487 case DW_FORM_block1: // block 488 if cls == DW_CLS_ADDRESS { 489 ctxt.AddInt(s, 1, int64(1+ctxt.PtrSize())) 490 ctxt.AddInt(s, 1, DW_OP_addr) 491 ctxt.AddAddress(s, data, 0) 492 break 493 } 494 495 value &= 0xff 496 ctxt.AddInt(s, 1, value) 497 p := data.([]byte)[:value] 498 ctxt.AddBytes(s, p) 499 500 case DW_FORM_block2: // block 501 value &= 0xffff 502 503 ctxt.AddInt(s, 2, value) 504 p := data.([]byte)[:value] 505 ctxt.AddBytes(s, p) 506 507 case DW_FORM_block4: // block 508 value &= 0xffffffff 509 510 ctxt.AddInt(s, 4, value) 511 p := data.([]byte)[:value] 512 ctxt.AddBytes(s, p) 513 514 case DW_FORM_block: // block 515 Uleb128put(ctxt, s, value) 516 517 p := data.([]byte)[:value] 518 ctxt.AddBytes(s, p) 519 520 case DW_FORM_data1: // constant 521 ctxt.AddInt(s, 1, value) 522 523 case DW_FORM_data2: // constant 524 ctxt.AddInt(s, 2, value) 525 526 case DW_FORM_data4: // constant, {line,loclist,mac,rangelist}ptr 527 if cls == DW_CLS_PTR { // DW_AT_stmt_list 528 ctxt.AddSectionOffset(s, 4, data, 0) 529 break 530 } 531 ctxt.AddInt(s, 4, value) 532 533 case DW_FORM_data8: // constant, {line,loclist,mac,rangelist}ptr 534 ctxt.AddInt(s, 8, value) 535 536 case DW_FORM_sdata: // constant 537 Sleb128put(ctxt, s, value) 538 539 case DW_FORM_udata: // constant 540 Uleb128put(ctxt, s, value) 541 542 case DW_FORM_string: // string 543 str := data.(string) 544 ctxt.AddString(s, str) 545 // TODO(ribrdb): verify padded strings are never used and remove this 546 for i := int64(len(str)); i < value; i++ { 547 ctxt.AddInt(s, 1, 0) 548 } 549 550 case DW_FORM_flag: // flag 551 if value != 0 { 552 ctxt.AddInt(s, 1, 1) 553 } else { 554 ctxt.AddInt(s, 1, 0) 555 } 556 557 // In DWARF 2 (which is what we claim to generate), 558 // the ref_addr is the same size as a normal address. 559 // In DWARF 3 it is always 32 bits, unless emitting a large 560 // (> 4 GB of debug info aka "64-bit") unit, which we don't implement. 561 case DW_FORM_ref_addr: // reference to a DIE in the .info section 562 if data == nil { 563 return fmt.Errorf("dwarf: null reference in %d", abbrev) 564 } else { 565 ctxt.AddSectionOffset(s, ctxt.PtrSize(), data, 0) 566 } 567 568 case DW_FORM_ref1, // reference within the compilation unit 569 DW_FORM_ref2, // reference 570 DW_FORM_ref4, // reference 571 DW_FORM_ref8, // reference 572 DW_FORM_ref_udata, // reference 573 574 DW_FORM_strp, // string 575 DW_FORM_indirect: // (see Section 7.5.3) 576 fallthrough 577 default: 578 return fmt.Errorf("dwarf: unsupported attribute form %d / class %d", form, cls) 579 } 580 return nil 581 } 582 583 // PutAttrs writes the attributes for a DIE to symbol 's'. 584 // 585 // Note that we can (and do) add arbitrary attributes to a DIE, but 586 // only the ones actually listed in the Abbrev will be written out. 587 func PutAttrs(ctxt Context, s Sym, abbrev int, attr *DWAttr) { 588 Outer: 589 for _, f := range abbrevs[abbrev].attr { 590 for ap := attr; ap != nil; ap = ap.Link { 591 if ap.Atr == f.attr { 592 putattr(ctxt, s, abbrev, int(f.form), int(ap.Cls), ap.Value, ap.Data) 593 continue Outer 594 } 595 } 596 597 putattr(ctxt, s, abbrev, int(f.form), 0, 0, nil) 598 } 599 } 600 601 // HasChildren returns true if 'die' uses an abbrev that supports children. 602 func HasChildren(die *DWDie) bool { 603 return abbrevs[die.Abbrev].children != 0 604 } 605 606 // PutFunc writes a DIE for a function to s. 607 // It also writes child DIEs for each variable in vars. 608 func PutFunc(ctxt Context, s Sym, name string, external bool, startPC Sym, size int64, vars []*Var) { 609 Uleb128put(ctxt, s, DW_ABRV_FUNCTION) 610 putattr(ctxt, s, DW_ABRV_FUNCTION, DW_FORM_string, DW_CLS_STRING, int64(len(name)), name) 611 putattr(ctxt, s, DW_ABRV_FUNCTION, DW_FORM_addr, DW_CLS_ADDRESS, 0, startPC) 612 putattr(ctxt, s, DW_ABRV_FUNCTION, DW_FORM_addr, DW_CLS_ADDRESS, size+ctxt.SymValue(startPC), startPC) 613 var ev int64 614 if external { 615 ev = 1 616 } 617 putattr(ctxt, s, DW_ABRV_FUNCTION, DW_FORM_flag, DW_CLS_FLAG, ev, 0) 618 names := make(map[string]bool) 619 var encbuf [20]byte 620 for _, v := range vars { 621 var n string 622 if names[v.Name] { 623 n = fmt.Sprintf("%s#%d", v.Name, len(names)) 624 } else { 625 n = v.Name 626 } 627 names[n] = true 628 629 Uleb128put(ctxt, s, int64(v.Abbrev)) 630 putattr(ctxt, s, v.Abbrev, DW_FORM_string, DW_CLS_STRING, int64(len(n)), n) 631 loc := append(encbuf[:0], DW_OP_call_frame_cfa) 632 if v.Offset != 0 { 633 loc = append(loc, DW_OP_consts) 634 loc = AppendSleb128(loc, int64(v.Offset)) 635 loc = append(loc, DW_OP_plus) 636 } 637 putattr(ctxt, s, v.Abbrev, DW_FORM_block1, DW_CLS_BLOCK, int64(len(loc)), loc) 638 putattr(ctxt, s, v.Abbrev, DW_FORM_ref_addr, DW_CLS_REFERENCE, 0, v.Type) 639 } 640 Uleb128put(ctxt, s, 0) 641 } 642 643 // VarsByOffset attaches the methods of sort.Interface to []*Var, 644 // sorting in increasing Offset. 645 type VarsByOffset []*Var 646 647 func (s VarsByOffset) Len() int { return len(s) } 648 func (s VarsByOffset) Less(i, j int) bool { return s[i].Offset < s[j].Offset } 649 func (s VarsByOffset) Swap(i, j int) { s[i], s[j] = s[j], s[i] }