github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/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 143 DW_AT_internal_location = 253 // params and locals; not emitted 144 ) 145 146 // Index into the abbrevs table below. 147 // Keep in sync with ispubname() and ispubtype() below. 148 // ispubtype considers >= NULLTYPE public 149 const ( 150 DW_ABRV_NULL = iota 151 DW_ABRV_COMPUNIT 152 DW_ABRV_FUNCTION 153 DW_ABRV_VARIABLE 154 DW_ABRV_AUTO 155 DW_ABRV_PARAM 156 DW_ABRV_STRUCTFIELD 157 DW_ABRV_FUNCTYPEPARAM 158 DW_ABRV_DOTDOTDOT 159 DW_ABRV_ARRAYRANGE 160 DW_ABRV_NULLTYPE 161 DW_ABRV_BASETYPE 162 DW_ABRV_ARRAYTYPE 163 DW_ABRV_CHANTYPE 164 DW_ABRV_FUNCTYPE 165 DW_ABRV_IFACETYPE 166 DW_ABRV_MAPTYPE 167 DW_ABRV_PTRTYPE 168 DW_ABRV_BARE_PTRTYPE // only for void*, no DW_AT_type attr to please gdb 6. 169 DW_ABRV_SLICETYPE 170 DW_ABRV_STRINGTYPE 171 DW_ABRV_STRUCTTYPE 172 DW_ABRV_TYPEDECL 173 DW_NABRV 174 ) 175 176 type dwAbbrev struct { 177 tag uint8 178 children uint8 179 attr []dwAttrForm 180 } 181 182 var abbrevs = [DW_NABRV]dwAbbrev{ 183 /* The mandatory DW_ABRV_NULL entry. */ 184 {0, 0, []dwAttrForm{}}, 185 186 /* COMPUNIT */ 187 { 188 DW_TAG_compile_unit, 189 DW_CHILDREN_yes, 190 []dwAttrForm{ 191 {DW_AT_name, DW_FORM_string}, 192 {DW_AT_language, DW_FORM_data1}, 193 {DW_AT_low_pc, DW_FORM_addr}, 194 {DW_AT_high_pc, DW_FORM_addr}, 195 {DW_AT_stmt_list, DW_FORM_data4}, 196 {DW_AT_comp_dir, DW_FORM_string}, 197 }, 198 }, 199 200 /* FUNCTION */ 201 { 202 DW_TAG_subprogram, 203 DW_CHILDREN_yes, 204 []dwAttrForm{ 205 {DW_AT_name, DW_FORM_string}, 206 {DW_AT_low_pc, DW_FORM_addr}, 207 {DW_AT_high_pc, DW_FORM_addr}, 208 {DW_AT_external, DW_FORM_flag}, 209 }, 210 }, 211 212 /* VARIABLE */ 213 { 214 DW_TAG_variable, 215 DW_CHILDREN_no, 216 []dwAttrForm{ 217 {DW_AT_name, DW_FORM_string}, 218 {DW_AT_location, DW_FORM_block1}, 219 {DW_AT_type, DW_FORM_ref_addr}, 220 {DW_AT_external, DW_FORM_flag}, 221 }, 222 }, 223 224 /* AUTO */ 225 { 226 DW_TAG_variable, 227 DW_CHILDREN_no, 228 []dwAttrForm{ 229 {DW_AT_name, DW_FORM_string}, 230 {DW_AT_location, DW_FORM_block1}, 231 {DW_AT_type, DW_FORM_ref_addr}, 232 }, 233 }, 234 235 /* PARAM */ 236 { 237 DW_TAG_formal_parameter, 238 DW_CHILDREN_no, 239 []dwAttrForm{ 240 {DW_AT_name, DW_FORM_string}, 241 {DW_AT_location, DW_FORM_block1}, 242 {DW_AT_type, DW_FORM_ref_addr}, 243 }, 244 }, 245 246 /* STRUCTFIELD */ 247 { 248 DW_TAG_member, 249 DW_CHILDREN_no, 250 []dwAttrForm{ 251 {DW_AT_name, DW_FORM_string}, 252 {DW_AT_data_member_location, DW_FORM_block1}, 253 {DW_AT_type, DW_FORM_ref_addr}, 254 }, 255 }, 256 257 /* FUNCTYPEPARAM */ 258 { 259 DW_TAG_formal_parameter, 260 DW_CHILDREN_no, 261 262 // No name! 263 []dwAttrForm{ 264 {DW_AT_type, DW_FORM_ref_addr}, 265 }, 266 }, 267 268 /* DOTDOTDOT */ 269 { 270 DW_TAG_unspecified_parameters, 271 DW_CHILDREN_no, 272 []dwAttrForm{}, 273 }, 274 275 /* ARRAYRANGE */ 276 { 277 DW_TAG_subrange_type, 278 DW_CHILDREN_no, 279 280 // No name! 281 []dwAttrForm{ 282 {DW_AT_type, DW_FORM_ref_addr}, 283 {DW_AT_count, DW_FORM_udata}, 284 }, 285 }, 286 287 // Below here are the types considered public by ispubtype 288 /* NULLTYPE */ 289 { 290 DW_TAG_unspecified_type, 291 DW_CHILDREN_no, 292 []dwAttrForm{ 293 {DW_AT_name, DW_FORM_string}, 294 }, 295 }, 296 297 /* BASETYPE */ 298 { 299 DW_TAG_base_type, 300 DW_CHILDREN_no, 301 []dwAttrForm{ 302 {DW_AT_name, DW_FORM_string}, 303 {DW_AT_encoding, DW_FORM_data1}, 304 {DW_AT_byte_size, DW_FORM_data1}, 305 {DW_AT_go_kind, DW_FORM_data1}, 306 }, 307 }, 308 309 /* ARRAYTYPE */ 310 // child is subrange with upper bound 311 { 312 DW_TAG_array_type, 313 DW_CHILDREN_yes, 314 []dwAttrForm{ 315 {DW_AT_name, DW_FORM_string}, 316 {DW_AT_type, DW_FORM_ref_addr}, 317 {DW_AT_byte_size, DW_FORM_udata}, 318 {DW_AT_go_kind, DW_FORM_data1}, 319 }, 320 }, 321 322 /* CHANTYPE */ 323 { 324 DW_TAG_typedef, 325 DW_CHILDREN_no, 326 []dwAttrForm{ 327 {DW_AT_name, DW_FORM_string}, 328 {DW_AT_type, DW_FORM_ref_addr}, 329 {DW_AT_go_kind, DW_FORM_data1}, 330 {DW_AT_go_elem, DW_FORM_ref_addr}, 331 }, 332 }, 333 334 /* FUNCTYPE */ 335 { 336 DW_TAG_subroutine_type, 337 DW_CHILDREN_yes, 338 []dwAttrForm{ 339 {DW_AT_name, DW_FORM_string}, 340 // {DW_AT_type, DW_FORM_ref_addr}, 341 {DW_AT_go_kind, DW_FORM_data1}, 342 }, 343 }, 344 345 /* IFACETYPE */ 346 { 347 DW_TAG_typedef, 348 DW_CHILDREN_yes, 349 []dwAttrForm{ 350 {DW_AT_name, DW_FORM_string}, 351 {DW_AT_type, DW_FORM_ref_addr}, 352 {DW_AT_go_kind, DW_FORM_data1}, 353 }, 354 }, 355 356 /* MAPTYPE */ 357 { 358 DW_TAG_typedef, 359 DW_CHILDREN_no, 360 []dwAttrForm{ 361 {DW_AT_name, DW_FORM_string}, 362 {DW_AT_type, DW_FORM_ref_addr}, 363 {DW_AT_go_kind, DW_FORM_data1}, 364 {DW_AT_go_key, DW_FORM_ref_addr}, 365 {DW_AT_go_elem, DW_FORM_ref_addr}, 366 }, 367 }, 368 369 /* PTRTYPE */ 370 { 371 DW_TAG_pointer_type, 372 DW_CHILDREN_no, 373 []dwAttrForm{ 374 {DW_AT_name, DW_FORM_string}, 375 {DW_AT_type, DW_FORM_ref_addr}, 376 {DW_AT_go_kind, DW_FORM_data1}, 377 }, 378 }, 379 380 /* BARE_PTRTYPE */ 381 { 382 DW_TAG_pointer_type, 383 DW_CHILDREN_no, 384 []dwAttrForm{ 385 {DW_AT_name, DW_FORM_string}, 386 }, 387 }, 388 389 /* SLICETYPE */ 390 { 391 DW_TAG_structure_type, 392 DW_CHILDREN_yes, 393 []dwAttrForm{ 394 {DW_AT_name, DW_FORM_string}, 395 {DW_AT_byte_size, DW_FORM_udata}, 396 {DW_AT_go_kind, DW_FORM_data1}, 397 {DW_AT_go_elem, DW_FORM_ref_addr}, 398 }, 399 }, 400 401 /* STRINGTYPE */ 402 { 403 DW_TAG_structure_type, 404 DW_CHILDREN_yes, 405 []dwAttrForm{ 406 {DW_AT_name, DW_FORM_string}, 407 {DW_AT_byte_size, DW_FORM_udata}, 408 {DW_AT_go_kind, DW_FORM_data1}, 409 }, 410 }, 411 412 /* STRUCTTYPE */ 413 { 414 DW_TAG_structure_type, 415 DW_CHILDREN_yes, 416 []dwAttrForm{ 417 {DW_AT_name, DW_FORM_string}, 418 {DW_AT_byte_size, DW_FORM_udata}, 419 {DW_AT_go_kind, DW_FORM_data1}, 420 }, 421 }, 422 423 /* TYPEDECL */ 424 { 425 DW_TAG_typedef, 426 DW_CHILDREN_no, 427 []dwAttrForm{ 428 {DW_AT_name, DW_FORM_string}, 429 {DW_AT_type, DW_FORM_ref_addr}, 430 }, 431 }, 432 } 433 434 // GetAbbrev returns the contents of the .debug_abbrev section. 435 func GetAbbrev() []byte { 436 var buf []byte 437 for i := 1; i < DW_NABRV; i++ { 438 // See section 7.5.3 439 buf = AppendUleb128(buf, uint64(i)) 440 buf = AppendUleb128(buf, uint64(abbrevs[i].tag)) 441 buf = append(buf, byte(abbrevs[i].children)) 442 for _, f := range abbrevs[i].attr { 443 buf = AppendUleb128(buf, uint64(f.attr)) 444 buf = AppendUleb128(buf, uint64(f.form)) 445 } 446 buf = append(buf, 0, 0) 447 } 448 return append(buf, 0) 449 } 450 451 /* 452 * Debugging Information Entries and their attributes. 453 */ 454 455 // DWAttr represents an attribute of a DWDie. 456 // 457 // For DW_CLS_string and _block, value should contain the length, and 458 // data the data, for _reference, value is 0 and data is a DWDie* to 459 // the referenced instance, for all others, value is the whole thing 460 // and data is null. 461 type DWAttr struct { 462 Link *DWAttr 463 Atr uint16 // DW_AT_ 464 Cls uint8 // DW_CLS_ 465 Value int64 466 Data interface{} 467 } 468 469 // DWDie represents a DWARF debug info entry. 470 type DWDie struct { 471 Abbrev int 472 Link *DWDie 473 Child *DWDie 474 Attr *DWAttr 475 Sym Sym 476 } 477 478 func putattr(ctxt Context, s Sym, abbrev int, form int, cls int, value int64, data interface{}) error { 479 switch form { 480 case DW_FORM_addr: // address 481 ctxt.AddAddress(s, data, value) 482 483 case DW_FORM_block1: // block 484 if cls == DW_CLS_ADDRESS { 485 ctxt.AddInt(s, 1, int64(1+ctxt.PtrSize())) 486 ctxt.AddInt(s, 1, DW_OP_addr) 487 ctxt.AddAddress(s, data, 0) 488 break 489 } 490 491 value &= 0xff 492 ctxt.AddInt(s, 1, value) 493 p := data.([]byte)[:value] 494 ctxt.AddBytes(s, p) 495 496 case DW_FORM_block2: // block 497 value &= 0xffff 498 499 ctxt.AddInt(s, 2, value) 500 p := data.([]byte)[:value] 501 ctxt.AddBytes(s, p) 502 503 case DW_FORM_block4: // block 504 value &= 0xffffffff 505 506 ctxt.AddInt(s, 4, value) 507 p := data.([]byte)[:value] 508 ctxt.AddBytes(s, p) 509 510 case DW_FORM_block: // block 511 Uleb128put(ctxt, s, value) 512 513 p := data.([]byte)[:value] 514 ctxt.AddBytes(s, p) 515 516 case DW_FORM_data1: // constant 517 ctxt.AddInt(s, 1, value) 518 519 case DW_FORM_data2: // constant 520 ctxt.AddInt(s, 2, value) 521 522 case DW_FORM_data4: // constant, {line,loclist,mac,rangelist}ptr 523 if cls == DW_CLS_PTR { // DW_AT_stmt_list 524 ctxt.AddSectionOffset(s, 4, data, 0) 525 break 526 } 527 ctxt.AddInt(s, 4, value) 528 529 case DW_FORM_data8: // constant, {line,loclist,mac,rangelist}ptr 530 ctxt.AddInt(s, 8, value) 531 532 case DW_FORM_sdata: // constant 533 Sleb128put(ctxt, s, value) 534 535 case DW_FORM_udata: // constant 536 Uleb128put(ctxt, s, value) 537 538 case DW_FORM_string: // string 539 str := data.(string) 540 ctxt.AddString(s, str) 541 // TODO(ribrdb): verify padded strings are never used and remove this 542 for i := int64(len(str)); i < value; i++ { 543 ctxt.AddInt(s, 1, 0) 544 } 545 546 case DW_FORM_flag: // flag 547 if value != 0 { 548 ctxt.AddInt(s, 1, 1) 549 } else { 550 ctxt.AddInt(s, 1, 0) 551 } 552 553 // In DWARF 2 (which is what we claim to generate), 554 // the ref_addr is the same size as a normal address. 555 // In DWARF 3 it is always 32 bits, unless emitting a large 556 // (> 4 GB of debug info aka "64-bit") unit, which we don't implement. 557 case DW_FORM_ref_addr: // reference to a DIE in the .info section 558 if data == nil { 559 return fmt.Errorf("dwarf: null reference in %d", abbrev) 560 } else { 561 ctxt.AddSectionOffset(s, ctxt.PtrSize(), data, 0) 562 } 563 564 case DW_FORM_ref1, // reference within the compilation unit 565 DW_FORM_ref2, // reference 566 DW_FORM_ref4, // reference 567 DW_FORM_ref8, // reference 568 DW_FORM_ref_udata, // reference 569 570 DW_FORM_strp, // string 571 DW_FORM_indirect: // (see Section 7.5.3) 572 fallthrough 573 default: 574 return fmt.Errorf("dwarf: unsupported attribute form %d / class %d", form, cls) 575 } 576 return nil 577 } 578 579 // PutAttrs writes the attributes for a DIE to symbol 's'. 580 // 581 // Note that we can (and do) add arbitrary attributes to a DIE, but 582 // only the ones actually listed in the Abbrev will be written out. 583 func PutAttrs(ctxt Context, s Sym, abbrev int, attr *DWAttr) { 584 Outer: 585 for _, f := range abbrevs[abbrev].attr { 586 for ap := attr; ap != nil; ap = ap.Link { 587 if ap.Atr == f.attr { 588 putattr(ctxt, s, abbrev, int(f.form), int(ap.Cls), ap.Value, ap.Data) 589 continue Outer 590 } 591 } 592 593 putattr(ctxt, s, abbrev, int(f.form), 0, 0, nil) 594 } 595 } 596 597 // HasChildren returns true if 'die' uses an abbrev that supports children. 598 func HasChildren(die *DWDie) bool { 599 return abbrevs[die.Abbrev].children != 0 600 } 601 602 // PutFunc writes a DIE for a function to s. 603 // It also writes child DIEs for each variable in vars. 604 func PutFunc(ctxt Context, s Sym, name string, external bool, startPC Sym, size int64, vars []*Var) { 605 Uleb128put(ctxt, s, DW_ABRV_FUNCTION) 606 putattr(ctxt, s, DW_ABRV_FUNCTION, DW_FORM_string, DW_CLS_STRING, int64(len(name)), name) 607 putattr(ctxt, s, DW_ABRV_FUNCTION, DW_FORM_addr, DW_CLS_ADDRESS, 0, startPC) 608 putattr(ctxt, s, DW_ABRV_FUNCTION, DW_FORM_addr, DW_CLS_ADDRESS, size+ctxt.SymValue(startPC), startPC) 609 var ev int64 610 if external { 611 ev = 1 612 } 613 putattr(ctxt, s, DW_ABRV_FUNCTION, DW_FORM_flag, DW_CLS_FLAG, ev, 0) 614 names := make(map[string]bool) 615 var encbuf [20]byte 616 for _, v := range vars { 617 var n string 618 if names[v.Name] { 619 n = fmt.Sprintf("%s#%d", v.Name, len(names)) 620 } else { 621 n = v.Name 622 } 623 names[n] = true 624 625 Uleb128put(ctxt, s, int64(v.Abbrev)) 626 putattr(ctxt, s, v.Abbrev, DW_FORM_string, DW_CLS_STRING, int64(len(n)), n) 627 loc := append(encbuf[:0], DW_OP_call_frame_cfa) 628 if v.Offset != 0 { 629 loc = append(loc, DW_OP_consts) 630 loc = AppendSleb128(loc, int64(v.Offset)) 631 loc = append(loc, DW_OP_plus) 632 } 633 putattr(ctxt, s, v.Abbrev, DW_FORM_block1, DW_CLS_BLOCK, int64(len(loc)), loc) 634 putattr(ctxt, s, v.Abbrev, DW_FORM_ref_addr, DW_CLS_REFERENCE, 0, v.Type) 635 } 636 Uleb128put(ctxt, s, 0) 637 } 638 639 // VarsByOffset attaches the methods of sort.Interface to []*Var, 640 // sorting in increasing Offset. 641 type VarsByOffset []*Var 642 643 func (s VarsByOffset) Len() int { return len(s) } 644 func (s VarsByOffset) Less(i, j int) bool { return s[i].Offset < s[j].Offset } 645 func (s VarsByOffset) Swap(i, j int) { s[i], s[j] = s[j], s[i] }