github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/src/cmd/ld/dwarf.c (about) 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. 4 5 // TODO/NICETOHAVE: 6 // - eliminate DW_CLS_ if not used 7 // - package info in compilation units 8 // - assign global variables and types to their packages 9 // - gdb uses c syntax, meaning clumsy quoting is needed for go identifiers. eg 10 // ptype struct '[]uint8' and qualifiers need to be quoted away 11 // - lexical scoping is lost, so gdb gets confused as to which 'main.i' you mean. 12 // - file:line info for variables 13 // - make strings a typedef so prettyprinters can see the underlying string type 14 // 15 #include "l.h" 16 #include "lib.h" 17 #include "../ld/dwarf.h" 18 #include "../ld/dwarf_defs.h" 19 #include "../ld/elf.h" 20 #include "../ld/macho.h" 21 #include "../ld/pe.h" 22 #include "../../runtime/typekind.h" 23 24 /* 25 * Offsets and sizes of the debug_* sections in the cout file. 26 */ 27 28 static vlong abbrevo; 29 static vlong abbrevsize; 30 static LSym* abbrevsym; 31 static vlong abbrevsympos; 32 static vlong lineo; 33 static vlong linesize; 34 static LSym* linesym; 35 static vlong linesympos; 36 static vlong infoo; // also the base for DWDie->offs and reference attributes. 37 static vlong infosize; 38 static LSym* infosym; 39 static vlong infosympos; 40 static vlong frameo; 41 static vlong framesize; 42 static LSym* framesym; 43 static vlong framesympos; 44 static vlong pubnameso; 45 static vlong pubnamessize; 46 static vlong pubtypeso; 47 static vlong pubtypessize; 48 static vlong arangeso; 49 static vlong arangessize; 50 static vlong gdbscripto; 51 static vlong gdbscriptsize; 52 53 static LSym *infosec; 54 static vlong inforeloco; 55 static vlong inforelocsize; 56 57 static LSym *arangessec; 58 static vlong arangesreloco; 59 static vlong arangesrelocsize; 60 61 static LSym *linesec; 62 static vlong linereloco; 63 static vlong linerelocsize; 64 65 static LSym *framesec; 66 static vlong framereloco; 67 static vlong framerelocsize; 68 69 static char gdbscript[1024]; 70 71 /* 72 * Basic I/O 73 */ 74 75 static void 76 addrput(vlong addr) 77 { 78 switch(PtrSize) { 79 case 4: 80 LPUT(addr); 81 break; 82 case 8: 83 VPUT(addr); 84 break; 85 } 86 } 87 88 static int 89 uleb128enc(uvlong v, char* dst) 90 { 91 uint8 c, len; 92 93 len = 0; 94 do { 95 c = v & 0x7f; 96 v >>= 7; 97 if (v) 98 c |= 0x80; 99 if (dst) 100 *dst++ = c; 101 len++; 102 } while (c & 0x80); 103 return len; 104 }; 105 106 static int 107 sleb128enc(vlong v, char *dst) 108 { 109 uint8 c, s, len; 110 111 len = 0; 112 do { 113 c = v & 0x7f; 114 s = v & 0x40; 115 v >>= 7; 116 if ((v != -1 || !s) && (v != 0 || s)) 117 c |= 0x80; 118 if (dst) 119 *dst++ = c; 120 len++; 121 } while(c & 0x80); 122 return len; 123 } 124 125 static void 126 uleb128put(vlong v) 127 { 128 char buf[10]; 129 strnput(buf, uleb128enc(v, buf)); 130 } 131 132 static void 133 sleb128put(vlong v) 134 { 135 char buf[10]; 136 strnput(buf, sleb128enc(v, buf)); 137 } 138 139 /* 140 * Defining Abbrevs. This is hardcoded, and there will be 141 * only a handful of them. The DWARF spec places no restriction on 142 * the ordering of attributes in the Abbrevs and DIEs, and we will 143 * always write them out in the order of declaration in the abbrev. 144 */ 145 typedef struct DWAttrForm DWAttrForm; 146 struct DWAttrForm { 147 uint16 attr; 148 uint8 form; 149 }; 150 151 // Go-specific type attributes. 152 enum { 153 DW_AT_go_kind = 0x2900, 154 DW_AT_go_key = 0x2901, 155 DW_AT_go_elem = 0x2902, 156 157 DW_AT_internal_location = 253, // params and locals; not emitted 158 }; 159 160 // Index into the abbrevs table below. 161 // Keep in sync with ispubname() and ispubtype() below. 162 // ispubtype considers >= NULLTYPE public 163 enum 164 { 165 DW_ABRV_NULL, 166 DW_ABRV_COMPUNIT, 167 DW_ABRV_FUNCTION, 168 DW_ABRV_VARIABLE, 169 DW_ABRV_AUTO, 170 DW_ABRV_PARAM, 171 DW_ABRV_STRUCTFIELD, 172 DW_ABRV_FUNCTYPEPARAM, 173 DW_ABRV_DOTDOTDOT, 174 DW_ABRV_ARRAYRANGE, 175 DW_ABRV_NULLTYPE, 176 DW_ABRV_BASETYPE, 177 DW_ABRV_ARRAYTYPE, 178 DW_ABRV_CHANTYPE, 179 DW_ABRV_FUNCTYPE, 180 DW_ABRV_IFACETYPE, 181 DW_ABRV_MAPTYPE, 182 DW_ABRV_PTRTYPE, 183 DW_ABRV_BARE_PTRTYPE, // only for void*, no DW_AT_type attr to please gdb 6. 184 DW_ABRV_SLICETYPE, 185 DW_ABRV_STRINGTYPE, 186 DW_ABRV_STRUCTTYPE, 187 DW_ABRV_TYPEDECL, 188 DW_NABRV 189 }; 190 191 typedef struct DWAbbrev DWAbbrev; 192 static struct DWAbbrev { 193 uint8 tag; 194 uint8 children; 195 DWAttrForm attr[30]; 196 } abbrevs[DW_NABRV] = { 197 /* The mandatory DW_ABRV_NULL entry. */ 198 { 0 }, 199 /* COMPUNIT */ 200 { 201 DW_TAG_compile_unit, DW_CHILDREN_yes, 202 DW_AT_name, DW_FORM_string, 203 DW_AT_language, DW_FORM_data1, 204 DW_AT_low_pc, DW_FORM_addr, 205 DW_AT_high_pc, DW_FORM_addr, 206 DW_AT_stmt_list, DW_FORM_data4, 207 0, 0 208 }, 209 /* FUNCTION */ 210 { 211 DW_TAG_subprogram, DW_CHILDREN_yes, 212 DW_AT_name, DW_FORM_string, 213 DW_AT_low_pc, DW_FORM_addr, 214 DW_AT_high_pc, DW_FORM_addr, 215 DW_AT_external, DW_FORM_flag, 216 0, 0 217 }, 218 /* VARIABLE */ 219 { 220 DW_TAG_variable, DW_CHILDREN_no, 221 DW_AT_name, DW_FORM_string, 222 DW_AT_location, DW_FORM_block1, 223 DW_AT_type, DW_FORM_ref_addr, 224 DW_AT_external, DW_FORM_flag, 225 0, 0 226 }, 227 /* AUTO */ 228 { 229 DW_TAG_variable, DW_CHILDREN_no, 230 DW_AT_name, DW_FORM_string, 231 DW_AT_location, DW_FORM_block1, 232 DW_AT_type, DW_FORM_ref_addr, 233 0, 0 234 }, 235 /* PARAM */ 236 { 237 DW_TAG_formal_parameter, DW_CHILDREN_no, 238 DW_AT_name, DW_FORM_string, 239 DW_AT_location, DW_FORM_block1, 240 DW_AT_type, DW_FORM_ref_addr, 241 0, 0 242 }, 243 /* STRUCTFIELD */ 244 { 245 DW_TAG_member, DW_CHILDREN_no, 246 DW_AT_name, DW_FORM_string, 247 DW_AT_data_member_location, DW_FORM_block1, 248 DW_AT_type, DW_FORM_ref_addr, 249 0, 0 250 }, 251 /* FUNCTYPEPARAM */ 252 { 253 DW_TAG_formal_parameter, DW_CHILDREN_no, 254 // No name! 255 DW_AT_type, DW_FORM_ref_addr, 256 0, 0 257 }, 258 259 /* DOTDOTDOT */ 260 { 261 DW_TAG_unspecified_parameters, DW_CHILDREN_no, 262 0, 0 263 }, 264 /* ARRAYRANGE */ 265 { 266 DW_TAG_subrange_type, DW_CHILDREN_no, 267 // No name! 268 DW_AT_type, DW_FORM_ref_addr, 269 DW_AT_count, DW_FORM_udata, 270 0, 0 271 }, 272 273 // Below here are the types considered public by ispubtype 274 /* NULLTYPE */ 275 { 276 DW_TAG_unspecified_type, DW_CHILDREN_no, 277 DW_AT_name, DW_FORM_string, 278 0, 0 279 }, 280 /* BASETYPE */ 281 { 282 DW_TAG_base_type, DW_CHILDREN_no, 283 DW_AT_name, DW_FORM_string, 284 DW_AT_encoding, DW_FORM_data1, 285 DW_AT_byte_size, DW_FORM_data1, 286 DW_AT_go_kind, DW_FORM_data1, 287 0, 0 288 }, 289 /* ARRAYTYPE */ 290 // child is subrange with upper bound 291 { 292 DW_TAG_array_type, DW_CHILDREN_yes, 293 DW_AT_name, DW_FORM_string, 294 DW_AT_type, DW_FORM_ref_addr, 295 DW_AT_byte_size, DW_FORM_udata, 296 DW_AT_go_kind, DW_FORM_data1, 297 0, 0 298 }, 299 300 /* CHANTYPE */ 301 { 302 DW_TAG_typedef, DW_CHILDREN_no, 303 DW_AT_name, DW_FORM_string, 304 DW_AT_type, DW_FORM_ref_addr, 305 DW_AT_go_kind, DW_FORM_data1, 306 DW_AT_go_elem, DW_FORM_ref_addr, 307 0, 0 308 }, 309 310 /* FUNCTYPE */ 311 { 312 DW_TAG_subroutine_type, DW_CHILDREN_yes, 313 DW_AT_name, DW_FORM_string, 314 // DW_AT_type, DW_FORM_ref_addr, 315 DW_AT_go_kind, DW_FORM_data1, 316 0, 0 317 }, 318 319 /* IFACETYPE */ 320 { 321 DW_TAG_typedef, DW_CHILDREN_yes, 322 DW_AT_name, DW_FORM_string, 323 DW_AT_type, DW_FORM_ref_addr, 324 DW_AT_go_kind, DW_FORM_data1, 325 0, 0 326 }, 327 328 /* MAPTYPE */ 329 { 330 DW_TAG_typedef, DW_CHILDREN_no, 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_key, DW_FORM_ref_addr, 335 DW_AT_go_elem, DW_FORM_ref_addr, 336 0, 0 337 }, 338 339 /* PTRTYPE */ 340 { 341 DW_TAG_pointer_type, DW_CHILDREN_no, 342 DW_AT_name, DW_FORM_string, 343 DW_AT_type, DW_FORM_ref_addr, 344 DW_AT_go_kind, DW_FORM_data1, 345 0, 0 346 }, 347 /* BARE_PTRTYPE */ 348 { 349 DW_TAG_pointer_type, DW_CHILDREN_no, 350 DW_AT_name, DW_FORM_string, 351 0, 0 352 }, 353 354 /* SLICETYPE */ 355 { 356 DW_TAG_structure_type, DW_CHILDREN_yes, 357 DW_AT_name, DW_FORM_string, 358 DW_AT_byte_size, DW_FORM_udata, 359 DW_AT_go_kind, DW_FORM_data1, 360 DW_AT_go_elem, DW_FORM_ref_addr, 361 0, 0 362 }, 363 364 /* STRINGTYPE */ 365 { 366 DW_TAG_structure_type, DW_CHILDREN_yes, 367 DW_AT_name, DW_FORM_string, 368 DW_AT_byte_size, DW_FORM_udata, 369 DW_AT_go_kind, DW_FORM_data1, 370 0, 0 371 }, 372 373 /* STRUCTTYPE */ 374 { 375 DW_TAG_structure_type, DW_CHILDREN_yes, 376 DW_AT_name, DW_FORM_string, 377 DW_AT_byte_size, DW_FORM_udata, 378 DW_AT_go_kind, DW_FORM_data1, 379 0, 0 380 }, 381 382 /* TYPEDECL */ 383 { 384 DW_TAG_typedef, DW_CHILDREN_no, 385 DW_AT_name, DW_FORM_string, 386 DW_AT_type, DW_FORM_ref_addr, 387 0, 0 388 }, 389 }; 390 391 static void 392 writeabbrev(void) 393 { 394 int i, j; 395 DWAttrForm *f; 396 397 abbrevo = cpos(); 398 for (i = 1; i < DW_NABRV; i++) { 399 // See section 7.5.3 400 uleb128put(i); 401 uleb128put(abbrevs[i].tag); 402 cput(abbrevs[i].children); 403 for(j=0; j<nelem(abbrevs[i].attr); j++) { 404 f = &abbrevs[i].attr[j]; 405 uleb128put(f->attr); 406 uleb128put(f->form); 407 if(f->attr == 0) 408 break; 409 } 410 } 411 cput(0); 412 abbrevsize = cpos() - abbrevo; 413 } 414 415 /* 416 * Debugging Information Entries and their attributes. 417 */ 418 419 enum 420 { 421 HASHSIZE = 107 422 }; 423 424 static uint32 425 hashstr(char* s) 426 { 427 uint32 h; 428 429 h = 0; 430 while (*s) 431 h = h+h+h + *s++; 432 return h % HASHSIZE; 433 } 434 435 // For DW_CLS_string and _block, value should contain the length, and 436 // data the data, for _reference, value is 0 and data is a DWDie* to 437 // the referenced instance, for all others, value is the whole thing 438 // and data is null. 439 440 typedef struct DWAttr DWAttr; 441 struct DWAttr { 442 DWAttr *link; 443 uint16 atr; // DW_AT_ 444 uint8 cls; // DW_CLS_ 445 vlong value; 446 char *data; 447 }; 448 449 typedef struct DWDie DWDie; 450 struct DWDie { 451 int abbrev; 452 DWDie *link; 453 DWDie *child; 454 DWAttr *attr; 455 // offset into .debug_info section, i.e relative to 456 // infoo. only valid after call to putdie() 457 vlong offs; 458 DWDie **hash; // optional index of children by name, enabled by mkindex() 459 DWDie *hlink; // bucket chain in parent's index 460 }; 461 462 /* 463 * Root DIEs for compilation units, types and global variables. 464 */ 465 466 static DWDie dwroot; 467 static DWDie dwtypes; 468 static DWDie dwglobals; 469 470 static DWAttr* 471 newattr(DWDie *die, uint16 attr, int cls, vlong value, char *data) 472 { 473 DWAttr *a; 474 475 a = mal(sizeof *a); 476 a->link = die->attr; 477 die->attr = a; 478 a->atr = attr; 479 a->cls = cls; 480 a->value = value; 481 a->data = data; 482 return a; 483 } 484 485 // Each DIE (except the root ones) has at least 1 attribute: its 486 // name. getattr moves the desired one to the front so 487 // frequently searched ones are found faster. 488 static DWAttr* 489 getattr(DWDie *die, uint16 attr) 490 { 491 DWAttr *a, *b; 492 493 if (die->attr->atr == attr) 494 return die->attr; 495 496 a = die->attr; 497 b = a->link; 498 while (b != nil) { 499 if (b->atr == attr) { 500 a->link = b->link; 501 b->link = die->attr; 502 die->attr = b; 503 return b; 504 } 505 a = b; 506 b = b->link; 507 } 508 return nil; 509 } 510 511 // Every DIE has at least a DW_AT_name attribute (but it will only be 512 // written out if it is listed in the abbrev). If its parent is 513 // keeping an index, the new DIE will be inserted there. 514 static DWDie* 515 newdie(DWDie *parent, int abbrev, char *name) 516 { 517 DWDie *die; 518 int h; 519 520 die = mal(sizeof *die); 521 die->abbrev = abbrev; 522 die->link = parent->child; 523 parent->child = die; 524 525 newattr(die, DW_AT_name, DW_CLS_STRING, strlen(name), name); 526 527 if (parent->hash) { 528 h = hashstr(name); 529 die->hlink = parent->hash[h]; 530 parent->hash[h] = die; 531 } 532 533 return die; 534 } 535 536 static void 537 mkindex(DWDie *die) 538 { 539 die->hash = mal(HASHSIZE * sizeof(DWDie*)); 540 } 541 542 static DWDie* 543 walktypedef(DWDie *die) 544 { 545 DWAttr *attr; 546 547 // Resolve typedef if present. 548 if (die->abbrev == DW_ABRV_TYPEDECL) { 549 for (attr = die->attr; attr; attr = attr->link) { 550 if (attr->atr == DW_AT_type && attr->cls == DW_CLS_REFERENCE && attr->data != nil) { 551 return (DWDie*)attr->data; 552 } 553 } 554 } 555 return die; 556 } 557 558 // Find child by AT_name using hashtable if available or linear scan 559 // if not. 560 static DWDie* 561 find(DWDie *die, char* name) 562 { 563 DWDie *a, *b, *die2; 564 int h; 565 566 top: 567 if (die->hash == nil) { 568 for (a = die->child; a != nil; a = a->link) 569 if (strcmp(name, getattr(a, DW_AT_name)->data) == 0) 570 return a; 571 goto notfound; 572 } 573 574 h = hashstr(name); 575 a = die->hash[h]; 576 577 if (a == nil) 578 goto notfound; 579 580 581 if (strcmp(name, getattr(a, DW_AT_name)->data) == 0) 582 return a; 583 584 // Move found ones to head of the list. 585 b = a->hlink; 586 while (b != nil) { 587 if (strcmp(name, getattr(b, DW_AT_name)->data) == 0) { 588 a->hlink = b->hlink; 589 b->hlink = die->hash[h]; 590 die->hash[h] = b; 591 return b; 592 } 593 a = b; 594 b = b->hlink; 595 } 596 597 notfound: 598 die2 = walktypedef(die); 599 if(die2 != die) { 600 die = die2; 601 goto top; 602 } 603 604 return nil; 605 } 606 607 static DWDie* 608 find_or_diag(DWDie *die, char* name) 609 { 610 DWDie *r; 611 r = find(die, name); 612 if (r == nil) { 613 diag("dwarf find: %s %p has no %s", getattr(die, DW_AT_name)->data, die, name); 614 errorexit(); 615 } 616 return r; 617 } 618 619 static void 620 adddwarfrel(LSym* sec, LSym* sym, vlong offsetbase, int siz, vlong addend) 621 { 622 Reloc *r; 623 624 r = addrel(sec); 625 r->sym = sym; 626 r->xsym = sym; 627 r->off = cpos() - offsetbase; 628 r->siz = siz; 629 r->type = R_ADDR; 630 r->add = addend; 631 r->xadd = addend; 632 if(iself && thechar == '6') 633 addend = 0; 634 switch(siz) { 635 case 4: 636 LPUT(addend); 637 break; 638 case 8: 639 VPUT(addend); 640 break; 641 default: 642 diag("bad size in adddwarfrel"); 643 break; 644 } 645 } 646 647 static DWAttr* 648 newrefattr(DWDie *die, uint16 attr, DWDie* ref) 649 { 650 if (ref == nil) 651 return nil; 652 return newattr(die, attr, DW_CLS_REFERENCE, 0, (char*)ref); 653 } 654 655 static int fwdcount; 656 657 static void 658 putattr(int abbrev, int form, int cls, vlong value, char *data) 659 { 660 vlong off; 661 662 switch(form) { 663 case DW_FORM_addr: // address 664 if(linkmode == LinkExternal) { 665 value -= ((LSym*)data)->value; 666 adddwarfrel(infosec, (LSym*)data, infoo, PtrSize, value); 667 break; 668 } 669 addrput(value); 670 break; 671 672 case DW_FORM_block1: // block 673 if(cls == DW_CLS_ADDRESS) { 674 cput(1+PtrSize); 675 cput(DW_OP_addr); 676 if(linkmode == LinkExternal) { 677 value -= ((LSym*)data)->value; 678 adddwarfrel(infosec, (LSym*)data, infoo, PtrSize, value); 679 break; 680 } 681 addrput(value); 682 break; 683 } 684 value &= 0xff; 685 cput(value); 686 while(value--) 687 cput(*data++); 688 break; 689 690 case DW_FORM_block2: // block 691 value &= 0xffff; 692 WPUT(value); 693 while(value--) 694 cput(*data++); 695 break; 696 697 case DW_FORM_block4: // block 698 value &= 0xffffffff; 699 LPUT(value); 700 while(value--) 701 cput(*data++); 702 break; 703 704 case DW_FORM_block: // block 705 uleb128put(value); 706 while(value--) 707 cput(*data++); 708 break; 709 710 case DW_FORM_data1: // constant 711 cput(value); 712 break; 713 714 case DW_FORM_data2: // constant 715 WPUT(value); 716 break; 717 718 case DW_FORM_data4: // constant, {line,loclist,mac,rangelist}ptr 719 if(linkmode == LinkExternal && cls == DW_CLS_PTR) { 720 adddwarfrel(infosec, linesym, infoo, 4, value); 721 break; 722 } 723 LPUT(value); 724 break; 725 726 case DW_FORM_data8: // constant, {line,loclist,mac,rangelist}ptr 727 VPUT(value); 728 break; 729 730 case DW_FORM_sdata: // constant 731 sleb128put(value); 732 break; 733 734 case DW_FORM_udata: // constant 735 uleb128put(value); 736 break; 737 738 case DW_FORM_string: // string 739 strnput(data, value+1); 740 break; 741 742 case DW_FORM_flag: // flag 743 cput(value?1:0); 744 break; 745 746 case DW_FORM_ref_addr: // reference to a DIE in the .info section 747 // In DWARF 2 (which is what we claim to generate), 748 // the ref_addr is the same size as a normal address. 749 // In DWARF 3 it is always 32 bits, unless emitting a large 750 // (> 4 GB of debug info aka "64-bit") unit, which we don't implement. 751 if (data == nil) { 752 diag("dwarf: null reference in %d", abbrev); 753 if(PtrSize == 8) 754 VPUT(0); // invalid dwarf, gdb will complain. 755 else 756 LPUT(0); // invalid dwarf, gdb will complain. 757 } else { 758 off = ((DWDie*)data)->offs; 759 if (off == 0) 760 fwdcount++; 761 if(linkmode == LinkExternal) { 762 adddwarfrel(infosec, infosym, infoo, PtrSize, off); 763 break; 764 } 765 addrput(off); 766 } 767 break; 768 769 case DW_FORM_ref1: // reference within the compilation unit 770 case DW_FORM_ref2: // reference 771 case DW_FORM_ref4: // reference 772 case DW_FORM_ref8: // reference 773 case DW_FORM_ref_udata: // reference 774 775 case DW_FORM_strp: // string 776 case DW_FORM_indirect: // (see Section 7.5.3) 777 default: 778 diag("dwarf: unsupported attribute form %d / class %d", form, cls); 779 errorexit(); 780 } 781 } 782 783 // Note that we can (and do) add arbitrary attributes to a DIE, but 784 // only the ones actually listed in the Abbrev will be written out. 785 static void 786 putattrs(int abbrev, DWAttr* attr) 787 { 788 DWAttrForm* af; 789 DWAttr *ap; 790 791 for(af = abbrevs[abbrev].attr; af->attr; af++) { 792 for(ap=attr; ap; ap=ap->link) { 793 if(ap->atr == af->attr) { 794 putattr(abbrev, af->form, 795 ap->cls, 796 ap->value, 797 ap->data); 798 goto done; 799 } 800 } 801 putattr(abbrev, af->form, 0, 0, nil); 802 done:; 803 } 804 } 805 806 static void putdie(DWDie* die); 807 808 static void 809 putdies(DWDie* die) 810 { 811 for(; die; die = die->link) 812 putdie(die); 813 } 814 815 static void 816 putdie(DWDie* die) 817 { 818 die->offs = cpos() - infoo; 819 uleb128put(die->abbrev); 820 putattrs(die->abbrev, die->attr); 821 if (abbrevs[die->abbrev].children) { 822 putdies(die->child); 823 cput(0); 824 } 825 } 826 827 static void 828 reverselist(DWDie** list) 829 { 830 DWDie *curr, *prev; 831 832 curr = *list; 833 prev = nil; 834 while(curr != nil) { 835 DWDie* next = curr->link; 836 curr->link = prev; 837 prev = curr; 838 curr = next; 839 } 840 *list = prev; 841 } 842 843 static void 844 reversetree(DWDie** list) 845 { 846 DWDie *die; 847 848 reverselist(list); 849 for (die = *list; die != nil; die = die->link) 850 if (abbrevs[die->abbrev].children) 851 reversetree(&die->child); 852 } 853 854 static void 855 newmemberoffsetattr(DWDie *die, int32 offs) 856 { 857 char block[10]; 858 int i; 859 860 i = 0; 861 block[i++] = DW_OP_plus_uconst; 862 i += uleb128enc(offs, block+i); 863 newattr(die, DW_AT_data_member_location, DW_CLS_BLOCK, i, mal(i)); 864 memmove(die->attr->data, block, i); 865 } 866 867 // GDB doesn't like DW_FORM_addr for DW_AT_location, so emit a 868 // location expression that evals to a const. 869 static void 870 newabslocexprattr(DWDie *die, vlong addr, LSym *sym) 871 { 872 newattr(die, DW_AT_location, DW_CLS_ADDRESS, addr, (char*)sym); 873 } 874 875 static DWDie* defptrto(DWDie *dwtype); // below 876 877 // Lookup predefined types 878 static LSym* 879 lookup_or_diag(char *n) 880 { 881 LSym *s; 882 883 s = linkrlookup(ctxt, n, 0); 884 if (s == nil || s->size == 0) { 885 diag("dwarf: missing type: %s", n); 886 errorexit(); 887 } 888 return s; 889 } 890 891 static void 892 dotypedef(DWDie *parent, char *name, DWDie *def) 893 { 894 DWDie *die; 895 896 // Only emit typedefs for real names. 897 if(strncmp(name, "map[", 4) == 0) 898 return; 899 if(strncmp(name, "struct {", 8) == 0) 900 return; 901 if(strncmp(name, "chan ", 5) == 0) 902 return; 903 if(*name == '[' || *name == '*') 904 return; 905 if(def == nil) 906 diag("dwarf: bad def in dotypedef"); 907 908 // The typedef entry must be created after the def, 909 // so that future lookups will find the typedef instead 910 // of the real definition. This hooks the typedef into any 911 // circular definition loops, so that gdb can understand them. 912 die = newdie(parent, DW_ABRV_TYPEDECL, name); 913 newrefattr(die, DW_AT_type, def); 914 } 915 916 // Define gotype, for composite ones recurse into constituents. 917 static DWDie* 918 defgotype(LSym *gotype) 919 { 920 DWDie *die, *fld; 921 LSym *s; 922 char *name, *f; 923 uint8 kind; 924 vlong bytesize; 925 int i, nfields; 926 927 if (gotype == nil) 928 return find_or_diag(&dwtypes, "<unspecified>"); 929 930 if (strncmp("type.", gotype->name, 5) != 0) { 931 diag("dwarf: type name doesn't start with \".type\": %s", gotype->name); 932 return find_or_diag(&dwtypes, "<unspecified>"); 933 } 934 name = gotype->name + 5; // could also decode from Type.string 935 936 die = find(&dwtypes, name); 937 if (die != nil) 938 return die; 939 940 if (0 && debug['v'] > 2) 941 print("new type: %Y\n", gotype); 942 943 kind = decodetype_kind(gotype); 944 bytesize = decodetype_size(gotype); 945 946 switch (kind) { 947 case KindBool: 948 die = newdie(&dwtypes, DW_ABRV_BASETYPE, name); 949 newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_boolean, 0); 950 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0); 951 break; 952 953 case KindInt: 954 case KindInt8: 955 case KindInt16: 956 case KindInt32: 957 case KindInt64: 958 die = newdie(&dwtypes, DW_ABRV_BASETYPE, name); 959 newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_signed, 0); 960 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0); 961 break; 962 963 case KindUint: 964 case KindUint8: 965 case KindUint16: 966 case KindUint32: 967 case KindUint64: 968 case KindUintptr: 969 die = newdie(&dwtypes, DW_ABRV_BASETYPE, name); 970 newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_unsigned, 0); 971 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0); 972 break; 973 974 case KindFloat32: 975 case KindFloat64: 976 die = newdie(&dwtypes, DW_ABRV_BASETYPE, name); 977 newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_float, 0); 978 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0); 979 break; 980 981 case KindComplex64: 982 case KindComplex128: 983 die = newdie(&dwtypes, DW_ABRV_BASETYPE, name); 984 newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_complex_float, 0); 985 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0); 986 break; 987 988 case KindArray: 989 die = newdie(&dwtypes, DW_ABRV_ARRAYTYPE, name); 990 dotypedef(&dwtypes, name, die); 991 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0); 992 s = decodetype_arrayelem(gotype); 993 newrefattr(die, DW_AT_type, defgotype(s)); 994 fld = newdie(die, DW_ABRV_ARRAYRANGE, "range"); 995 // use actual length not upper bound; correct for 0-length arrays. 996 newattr(fld, DW_AT_count, DW_CLS_CONSTANT, decodetype_arraylen(gotype), 0); 997 newrefattr(fld, DW_AT_type, find_or_diag(&dwtypes, "uintptr")); 998 break; 999 1000 case KindChan: 1001 die = newdie(&dwtypes, DW_ABRV_CHANTYPE, name); 1002 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0); 1003 s = decodetype_chanelem(gotype); 1004 newrefattr(die, DW_AT_go_elem, defgotype(s)); 1005 break; 1006 1007 case KindFunc: 1008 die = newdie(&dwtypes, DW_ABRV_FUNCTYPE, name); 1009 dotypedef(&dwtypes, name, die); 1010 newrefattr(die, DW_AT_type, find_or_diag(&dwtypes, "void")); 1011 nfields = decodetype_funcincount(gotype); 1012 for (i = 0; i < nfields; i++) { 1013 s = decodetype_funcintype(gotype, i); 1014 fld = newdie(die, DW_ABRV_FUNCTYPEPARAM, s->name+5); 1015 newrefattr(fld, DW_AT_type, defgotype(s)); 1016 } 1017 if (decodetype_funcdotdotdot(gotype)) 1018 newdie(die, DW_ABRV_DOTDOTDOT, "..."); 1019 nfields = decodetype_funcoutcount(gotype); 1020 for (i = 0; i < nfields; i++) { 1021 s = decodetype_funcouttype(gotype, i); 1022 fld = newdie(die, DW_ABRV_FUNCTYPEPARAM, s->name+5); 1023 newrefattr(fld, DW_AT_type, defptrto(defgotype(s))); 1024 } 1025 break; 1026 1027 case KindInterface: 1028 die = newdie(&dwtypes, DW_ABRV_IFACETYPE, name); 1029 dotypedef(&dwtypes, name, die); 1030 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0); 1031 nfields = decodetype_ifacemethodcount(gotype); 1032 if (nfields == 0) 1033 s = lookup_or_diag("type.runtime.eface"); 1034 else 1035 s = lookup_or_diag("type.runtime.iface"); 1036 newrefattr(die, DW_AT_type, defgotype(s)); 1037 break; 1038 1039 case KindMap: 1040 die = newdie(&dwtypes, DW_ABRV_MAPTYPE, name); 1041 s = decodetype_mapkey(gotype); 1042 newrefattr(die, DW_AT_go_key, defgotype(s)); 1043 s = decodetype_mapvalue(gotype); 1044 newrefattr(die, DW_AT_go_elem, defgotype(s)); 1045 break; 1046 1047 case KindPtr: 1048 die = newdie(&dwtypes, DW_ABRV_PTRTYPE, name); 1049 dotypedef(&dwtypes, name, die); 1050 s = decodetype_ptrelem(gotype); 1051 newrefattr(die, DW_AT_type, defgotype(s)); 1052 break; 1053 1054 case KindSlice: 1055 die = newdie(&dwtypes, DW_ABRV_SLICETYPE, name); 1056 dotypedef(&dwtypes, name, die); 1057 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0); 1058 s = decodetype_arrayelem(gotype); 1059 newrefattr(die, DW_AT_go_elem, defgotype(s)); 1060 break; 1061 1062 case KindString: 1063 die = newdie(&dwtypes, DW_ABRV_STRINGTYPE, name); 1064 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0); 1065 break; 1066 1067 case KindStruct: 1068 die = newdie(&dwtypes, DW_ABRV_STRUCTTYPE, name); 1069 dotypedef(&dwtypes, name, die); 1070 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0); 1071 nfields = decodetype_structfieldcount(gotype); 1072 for (i = 0; i < nfields; i++) { 1073 f = decodetype_structfieldname(gotype, i); 1074 s = decodetype_structfieldtype(gotype, i); 1075 if (f == nil) 1076 f = s->name + 5; // skip "type." 1077 fld = newdie(die, DW_ABRV_STRUCTFIELD, f); 1078 newrefattr(fld, DW_AT_type, defgotype(s)); 1079 newmemberoffsetattr(fld, decodetype_structfieldoffs(gotype, i)); 1080 } 1081 break; 1082 1083 case KindUnsafePointer: 1084 die = newdie(&dwtypes, DW_ABRV_BARE_PTRTYPE, name); 1085 break; 1086 1087 default: 1088 diag("dwarf: definition of unknown kind %d: %s", kind, gotype->name); 1089 die = newdie(&dwtypes, DW_ABRV_TYPEDECL, name); 1090 newrefattr(die, DW_AT_type, find_or_diag(&dwtypes, "<unspecified>")); 1091 } 1092 1093 newattr(die, DW_AT_go_kind, DW_CLS_CONSTANT, kind, 0); 1094 1095 return die; 1096 } 1097 1098 // Find or construct *T given T. 1099 static DWDie* 1100 defptrto(DWDie *dwtype) 1101 { 1102 char ptrname[1024]; 1103 DWDie *die; 1104 1105 snprint(ptrname, sizeof ptrname, "*%s", getattr(dwtype, DW_AT_name)->data); 1106 die = find(&dwtypes, ptrname); 1107 if (die == nil) { 1108 die = newdie(&dwtypes, DW_ABRV_PTRTYPE, 1109 strcpy(mal(strlen(ptrname)+1), ptrname)); 1110 newrefattr(die, DW_AT_type, dwtype); 1111 } 1112 return die; 1113 } 1114 1115 // Copies src's children into dst. Copies attributes by value. 1116 // DWAttr.data is copied as pointer only. If except is one of 1117 // the top-level children, it will not be copied. 1118 static void 1119 copychildrenexcept(DWDie *dst, DWDie *src, DWDie *except) 1120 { 1121 DWDie *c; 1122 DWAttr *a; 1123 1124 for (src = src->child; src != nil; src = src->link) { 1125 if(src == except) 1126 continue; 1127 c = newdie(dst, src->abbrev, getattr(src, DW_AT_name)->data); 1128 for (a = src->attr; a != nil; a = a->link) 1129 newattr(c, a->atr, a->cls, a->value, a->data); 1130 copychildrenexcept(c, src, nil); 1131 } 1132 reverselist(&dst->child); 1133 } 1134 static void 1135 copychildren(DWDie *dst, DWDie *src) 1136 { 1137 copychildrenexcept(dst, src, nil); 1138 } 1139 1140 // Search children (assumed to have DW_TAG_member) for the one named 1141 // field and set its DW_AT_type to dwtype 1142 static void 1143 substitutetype(DWDie *structdie, char *field, DWDie* dwtype) 1144 { 1145 DWDie *child; 1146 DWAttr *a; 1147 1148 child = find_or_diag(structdie, field); 1149 if (child == nil) 1150 return; 1151 1152 a = getattr(child, DW_AT_type); 1153 if (a != nil) 1154 a->data = (char*) dwtype; 1155 else 1156 newrefattr(child, DW_AT_type, dwtype); 1157 } 1158 1159 static void 1160 synthesizestringtypes(DWDie* die) 1161 { 1162 DWDie *prototype; 1163 1164 prototype = walktypedef(defgotype(lookup_or_diag("type.runtime._string"))); 1165 if (prototype == nil) 1166 return; 1167 1168 for (; die != nil; die = die->link) { 1169 if (die->abbrev != DW_ABRV_STRINGTYPE) 1170 continue; 1171 copychildren(die, prototype); 1172 } 1173 } 1174 1175 static void 1176 synthesizeslicetypes(DWDie *die) 1177 { 1178 DWDie *prototype, *elem; 1179 1180 prototype = walktypedef(defgotype(lookup_or_diag("type.runtime.slice"))); 1181 if (prototype == nil) 1182 return; 1183 1184 for (; die != nil; die = die->link) { 1185 if (die->abbrev != DW_ABRV_SLICETYPE) 1186 continue; 1187 copychildren(die, prototype); 1188 elem = (DWDie*) getattr(die, DW_AT_go_elem)->data; 1189 substitutetype(die, "array", defptrto(elem)); 1190 } 1191 } 1192 1193 static char* 1194 mkinternaltypename(char *base, char *arg1, char *arg2) 1195 { 1196 char buf[1024]; 1197 char *n; 1198 1199 if (arg2 == nil) 1200 snprint(buf, sizeof buf, "%s<%s>", base, arg1); 1201 else 1202 snprint(buf, sizeof buf, "%s<%s,%s>", base, arg1, arg2); 1203 n = mal(strlen(buf) + 1); 1204 memmove(n, buf, strlen(buf)); 1205 return n; 1206 } 1207 1208 // synthesizemaptypes is way too closely married to runtime/hashmap.c 1209 enum { 1210 MaxKeySize = 128, 1211 MaxValSize = 128, 1212 BucketSize = 8, 1213 }; 1214 1215 static void 1216 synthesizemaptypes(DWDie *die) 1217 { 1218 1219 DWDie *hash, *bucket, *dwh, *dwhk, *dwhv, *dwhb, *keytype, *valtype, *fld; 1220 int indirect_key, indirect_val; 1221 int keysize, valsize; 1222 DWAttr *a; 1223 1224 hash = walktypedef(defgotype(lookup_or_diag("type.runtime.hmap"))); 1225 bucket = walktypedef(defgotype(lookup_or_diag("type.runtime.bmap"))); 1226 1227 if (hash == nil) 1228 return; 1229 1230 for (; die != nil; die = die->link) { 1231 if (die->abbrev != DW_ABRV_MAPTYPE) 1232 continue; 1233 1234 keytype = walktypedef((DWDie*) getattr(die, DW_AT_go_key)->data); 1235 valtype = walktypedef((DWDie*) getattr(die, DW_AT_go_elem)->data); 1236 1237 // compute size info like hashmap.c does. 1238 a = getattr(keytype, DW_AT_byte_size); 1239 keysize = a ? a->value : PtrSize; // We don't store size with Pointers 1240 a = getattr(valtype, DW_AT_byte_size); 1241 valsize = a ? a->value : PtrSize; 1242 indirect_key = 0; 1243 indirect_val = 0; 1244 if(keysize > MaxKeySize) { 1245 keysize = PtrSize; 1246 indirect_key = 1; 1247 } 1248 if(valsize > MaxValSize) { 1249 valsize = PtrSize; 1250 indirect_val = 1; 1251 } 1252 1253 // Construct type to represent an array of BucketSize keys 1254 dwhk = newdie(&dwtypes, DW_ABRV_ARRAYTYPE, 1255 mkinternaltypename("[]key", 1256 getattr(keytype, DW_AT_name)->data, nil)); 1257 newattr(dwhk, DW_AT_byte_size, DW_CLS_CONSTANT, BucketSize * keysize, 0); 1258 newrefattr(dwhk, DW_AT_type, indirect_key ? defptrto(keytype) : keytype); 1259 fld = newdie(dwhk, DW_ABRV_ARRAYRANGE, "size"); 1260 newattr(fld, DW_AT_count, DW_CLS_CONSTANT, BucketSize, 0); 1261 newrefattr(fld, DW_AT_type, find_or_diag(&dwtypes, "uintptr")); 1262 1263 // Construct type to represent an array of BucketSize values 1264 dwhv = newdie(&dwtypes, DW_ABRV_ARRAYTYPE, 1265 mkinternaltypename("[]val", 1266 getattr(valtype, DW_AT_name)->data, nil)); 1267 newattr(dwhv, DW_AT_byte_size, DW_CLS_CONSTANT, BucketSize * valsize, 0); 1268 newrefattr(dwhv, DW_AT_type, indirect_val ? defptrto(valtype) : valtype); 1269 fld = newdie(dwhv, DW_ABRV_ARRAYRANGE, "size"); 1270 newattr(fld, DW_AT_count, DW_CLS_CONSTANT, BucketSize, 0); 1271 newrefattr(fld, DW_AT_type, find_or_diag(&dwtypes, "uintptr")); 1272 1273 // Construct bucket<K,V> 1274 dwhb = newdie(&dwtypes, DW_ABRV_STRUCTTYPE, 1275 mkinternaltypename("bucket", 1276 getattr(keytype, DW_AT_name)->data, 1277 getattr(valtype, DW_AT_name)->data)); 1278 // Copy over all fields except the field "data" from the generic bucket. 1279 // "data" will be replaced with keys/values below. 1280 copychildrenexcept(dwhb, bucket, find(bucket, "data")); 1281 1282 fld = newdie(dwhb, DW_ABRV_STRUCTFIELD, "keys"); 1283 newrefattr(fld, DW_AT_type, dwhk); 1284 newmemberoffsetattr(fld, BucketSize + PtrSize); 1285 fld = newdie(dwhb, DW_ABRV_STRUCTFIELD, "values"); 1286 newrefattr(fld, DW_AT_type, dwhv); 1287 newmemberoffsetattr(fld, BucketSize + PtrSize + BucketSize * keysize); 1288 newattr(dwhb, DW_AT_byte_size, DW_CLS_CONSTANT, BucketSize + PtrSize + BucketSize * keysize + BucketSize * valsize, 0); 1289 substitutetype(dwhb, "overflow", defptrto(dwhb)); 1290 1291 // Construct hash<K,V> 1292 dwh = newdie(&dwtypes, DW_ABRV_STRUCTTYPE, 1293 mkinternaltypename("hash", 1294 getattr(keytype, DW_AT_name)->data, 1295 getattr(valtype, DW_AT_name)->data)); 1296 copychildren(dwh, hash); 1297 substitutetype(dwh, "buckets", defptrto(dwhb)); 1298 substitutetype(dwh, "oldbuckets", defptrto(dwhb)); 1299 newattr(dwh, DW_AT_byte_size, DW_CLS_CONSTANT, 1300 getattr(hash, DW_AT_byte_size)->value, nil); 1301 1302 // make map type a pointer to hash<K,V> 1303 newrefattr(die, DW_AT_type, defptrto(dwh)); 1304 } 1305 } 1306 1307 static void 1308 synthesizechantypes(DWDie *die) 1309 { 1310 DWDie *sudog, *waitq, *hchan, 1311 *dws, *dww, *dwh, *elemtype; 1312 DWAttr *a; 1313 int elemsize, sudogsize; 1314 1315 sudog = walktypedef(defgotype(lookup_or_diag("type.runtime.sudog"))); 1316 waitq = walktypedef(defgotype(lookup_or_diag("type.runtime.waitq"))); 1317 hchan = walktypedef(defgotype(lookup_or_diag("type.runtime.hchan"))); 1318 if (sudog == nil || waitq == nil || hchan == nil) 1319 return; 1320 1321 sudogsize = getattr(sudog, DW_AT_byte_size)->value; 1322 1323 for (; die != nil; die = die->link) { 1324 if (die->abbrev != DW_ABRV_CHANTYPE) 1325 continue; 1326 elemtype = (DWDie*) getattr(die, DW_AT_go_elem)->data; 1327 a = getattr(elemtype, DW_AT_byte_size); 1328 elemsize = a ? a->value : PtrSize; 1329 1330 // sudog<T> 1331 dws = newdie(&dwtypes, DW_ABRV_STRUCTTYPE, 1332 mkinternaltypename("sudog", 1333 getattr(elemtype, DW_AT_name)->data, nil)); 1334 copychildren(dws, sudog); 1335 substitutetype(dws, "elem", elemtype); 1336 newattr(dws, DW_AT_byte_size, DW_CLS_CONSTANT, 1337 sudogsize + (elemsize > 8 ? elemsize - 8 : 0), nil); 1338 1339 // waitq<T> 1340 dww = newdie(&dwtypes, DW_ABRV_STRUCTTYPE, 1341 mkinternaltypename("waitq", getattr(elemtype, DW_AT_name)->data, nil)); 1342 copychildren(dww, waitq); 1343 substitutetype(dww, "first", defptrto(dws)); 1344 substitutetype(dww, "last", defptrto(dws)); 1345 newattr(dww, DW_AT_byte_size, DW_CLS_CONSTANT, 1346 getattr(waitq, DW_AT_byte_size)->value, nil); 1347 1348 // hchan<T> 1349 dwh = newdie(&dwtypes, DW_ABRV_STRUCTTYPE, 1350 mkinternaltypename("hchan", getattr(elemtype, DW_AT_name)->data, nil)); 1351 copychildren(dwh, hchan); 1352 substitutetype(dwh, "recvq", dww); 1353 substitutetype(dwh, "sendq", dww); 1354 newattr(dwh, DW_AT_byte_size, DW_CLS_CONSTANT, 1355 getattr(hchan, DW_AT_byte_size)->value, nil); 1356 1357 newrefattr(die, DW_AT_type, defptrto(dwh)); 1358 } 1359 } 1360 1361 // For use with pass.c::genasmsym 1362 static void 1363 defdwsymb(LSym* sym, char *s, int t, vlong v, vlong size, int ver, LSym *gotype) 1364 { 1365 DWDie *dv, *dt; 1366 1367 USED(size); 1368 if (strncmp(s, "go.string.", 10) == 0) 1369 return; 1370 1371 if (strncmp(s, "type.", 5) == 0 && strcmp(s, "type.*") != 0 && strncmp(s, "type..", 6) != 0) { 1372 defgotype(sym); 1373 return; 1374 } 1375 1376 dv = nil; 1377 1378 switch (t) { 1379 default: 1380 return; 1381 case 'd': 1382 case 'b': 1383 case 'D': 1384 case 'B': 1385 dv = newdie(&dwglobals, DW_ABRV_VARIABLE, s); 1386 newabslocexprattr(dv, v, sym); 1387 if (ver == 0) 1388 newattr(dv, DW_AT_external, DW_CLS_FLAG, 1, 0); 1389 // fallthrough 1390 case 'a': 1391 case 'p': 1392 dt = defgotype(gotype); 1393 } 1394 1395 if (dv != nil) 1396 newrefattr(dv, DW_AT_type, dt); 1397 } 1398 1399 static void 1400 movetomodule(DWDie *parent) 1401 { 1402 DWDie *die; 1403 1404 die = dwroot.child->child; 1405 while(die->link != nil) 1406 die = die->link; 1407 die->link = parent->child; 1408 } 1409 1410 // If the pcln table contains runtime/string.goc, use that to set gdbscript path. 1411 static void 1412 finddebugruntimepath(LSym *s) 1413 { 1414 int i; 1415 char *p; 1416 LSym *f; 1417 1418 if(gdbscript[0] != '\0') 1419 return; 1420 1421 for(i=0; i<s->pcln->nfile; i++) { 1422 f = s->pcln->file[i]; 1423 if((p = strstr(f->name, "runtime/string.goc")) != nil) { 1424 *p = '\0'; 1425 snprint(gdbscript, sizeof gdbscript, "%sruntime/runtime-gdb.py", f->name); 1426 *p = 'r'; 1427 break; 1428 } 1429 } 1430 } 1431 1432 /* 1433 * Generate short opcodes when possible, long ones when necessary. 1434 * See section 6.2.5 1435 */ 1436 1437 enum { 1438 LINE_BASE = -1, 1439 LINE_RANGE = 4, 1440 OPCODE_BASE = 10 1441 }; 1442 1443 static void 1444 putpclcdelta(vlong delta_pc, vlong delta_lc) 1445 { 1446 if (LINE_BASE <= delta_lc && delta_lc < LINE_BASE+LINE_RANGE) { 1447 vlong opcode = OPCODE_BASE + (delta_lc - LINE_BASE) + (LINE_RANGE * delta_pc); 1448 if (OPCODE_BASE <= opcode && opcode < 256) { 1449 cput(opcode); 1450 return; 1451 } 1452 } 1453 1454 if (delta_pc) { 1455 cput(DW_LNS_advance_pc); 1456 sleb128put(delta_pc); 1457 } 1458 1459 cput(DW_LNS_advance_line); 1460 sleb128put(delta_lc); 1461 cput(DW_LNS_copy); 1462 } 1463 1464 static void 1465 newcfaoffsetattr(DWDie *die, int32 offs) 1466 { 1467 char block[10]; 1468 int i; 1469 1470 i = 0; 1471 1472 block[i++] = DW_OP_call_frame_cfa; 1473 if (offs != 0) { 1474 block[i++] = DW_OP_consts; 1475 i += sleb128enc(offs, block+i); 1476 block[i++] = DW_OP_plus; 1477 } 1478 newattr(die, DW_AT_location, DW_CLS_BLOCK, i, mal(i)); 1479 memmove(die->attr->data, block, i); 1480 } 1481 1482 static char* 1483 mkvarname(char* name, int da) 1484 { 1485 char buf[1024]; 1486 char *n; 1487 1488 snprint(buf, sizeof buf, "%s#%d", name, da); 1489 n = mal(strlen(buf) + 1); 1490 memmove(n, buf, strlen(buf)); 1491 return n; 1492 } 1493 1494 /* 1495 * Walk prog table, emit line program and build DIE tree. 1496 */ 1497 1498 // flush previous compilation unit. 1499 static void 1500 flushunit(DWDie *dwinfo, vlong pc, LSym *pcsym, vlong unitstart, int32 header_length) 1501 { 1502 vlong here; 1503 1504 if (dwinfo != nil && pc != 0) { 1505 newattr(dwinfo, DW_AT_high_pc, DW_CLS_ADDRESS, pc+1, (char*)pcsym); 1506 } 1507 1508 if (unitstart >= 0) { 1509 cput(0); // start extended opcode 1510 uleb128put(1); 1511 cput(DW_LNE_end_sequence); 1512 1513 here = cpos(); 1514 cseek(unitstart); 1515 LPUT(here - unitstart - sizeof(int32)); // unit_length 1516 WPUT(2); // dwarf version 1517 LPUT(header_length); // header length starting here 1518 cseek(here); 1519 } 1520 } 1521 1522 static void 1523 writelines(void) 1524 { 1525 LSym *s, *epcs; 1526 Auto *a; 1527 vlong unitstart, headerend, offs; 1528 vlong pc, epc; 1529 int i, lang, da, dt, line, file; 1530 DWDie *dwinfo, *dwfunc, *dwvar, **dws; 1531 DWDie *varhash[HASHSIZE]; 1532 char *n, *nn; 1533 Pciter pcfile, pcline; 1534 LSym **files, *f; 1535 1536 if(linesec == S) 1537 linesec = linklookup(ctxt, ".dwarfline", 0); 1538 linesec->nr = 0; 1539 1540 unitstart = -1; 1541 headerend = -1; 1542 epc = 0; 1543 epcs = S; 1544 lineo = cpos(); 1545 dwinfo = nil; 1546 1547 flushunit(dwinfo, epc, epcs, unitstart, headerend - unitstart - 10); 1548 unitstart = cpos(); 1549 1550 lang = DW_LANG_Go; 1551 1552 s = ctxt->textp; 1553 1554 dwinfo = newdie(&dwroot, DW_ABRV_COMPUNIT, estrdup("go")); 1555 newattr(dwinfo, DW_AT_language, DW_CLS_CONSTANT,lang, 0); 1556 newattr(dwinfo, DW_AT_stmt_list, DW_CLS_PTR, unitstart - lineo, 0); 1557 newattr(dwinfo, DW_AT_low_pc, DW_CLS_ADDRESS, s->value, (char*)s); 1558 1559 // Write .debug_line Line Number Program Header (sec 6.2.4) 1560 // Fields marked with (*) must be changed for 64-bit dwarf 1561 LPUT(0); // unit_length (*), will be filled in by flushunit. 1562 WPUT(2); // dwarf version (appendix F) 1563 LPUT(0); // header_length (*), filled in by flushunit. 1564 // cpos == unitstart + 4 + 2 + 4 1565 cput(1); // minimum_instruction_length 1566 cput(1); // default_is_stmt 1567 cput(LINE_BASE); // line_base 1568 cput(LINE_RANGE); // line_range 1569 cput(OPCODE_BASE); // opcode_base 1570 cput(0); // standard_opcode_lengths[1] 1571 cput(1); // standard_opcode_lengths[2] 1572 cput(1); // standard_opcode_lengths[3] 1573 cput(1); // standard_opcode_lengths[4] 1574 cput(1); // standard_opcode_lengths[5] 1575 cput(0); // standard_opcode_lengths[6] 1576 cput(0); // standard_opcode_lengths[7] 1577 cput(0); // standard_opcode_lengths[8] 1578 cput(1); // standard_opcode_lengths[9] 1579 cput(0); // include_directories (empty) 1580 1581 files = emallocz(ctxt->nhistfile*sizeof files[0]); 1582 for(f = ctxt->filesyms; f != nil; f = f->next) 1583 files[f->value-1] = f; 1584 1585 for(i=0; i<ctxt->nhistfile; i++) { 1586 strnput(files[i]->name, strlen(files[i]->name) + 4); 1587 // 4 zeros: the string termination + 3 fields. 1588 } 1589 1590 cput(0); // terminate file_names. 1591 headerend = cpos(); 1592 1593 cput(0); // start extended opcode 1594 uleb128put(1 + PtrSize); 1595 cput(DW_LNE_set_address); 1596 1597 pc = s->value; 1598 line = 1; 1599 file = 1; 1600 if(linkmode == LinkExternal) 1601 adddwarfrel(linesec, s, lineo, PtrSize, 0); 1602 else 1603 addrput(pc); 1604 1605 for(ctxt->cursym = ctxt->textp; ctxt->cursym != nil; ctxt->cursym = ctxt->cursym->next) { 1606 s = ctxt->cursym; 1607 1608 dwfunc = newdie(dwinfo, DW_ABRV_FUNCTION, s->name); 1609 newattr(dwfunc, DW_AT_low_pc, DW_CLS_ADDRESS, s->value, (char*)s); 1610 epc = s->value + s->size; 1611 epcs = s; 1612 newattr(dwfunc, DW_AT_high_pc, DW_CLS_ADDRESS, epc, (char*)s); 1613 if (s->version == 0) 1614 newattr(dwfunc, DW_AT_external, DW_CLS_FLAG, 1, 0); 1615 1616 if(s->pcln == nil) 1617 continue; 1618 1619 finddebugruntimepath(s); 1620 1621 pciterinit(ctxt, &pcfile, &s->pcln->pcfile); 1622 pciterinit(ctxt, &pcline, &s->pcln->pcline); 1623 epc = pc; 1624 while(!pcfile.done && !pcline.done) { 1625 if(epc - s->value >= pcfile.nextpc) { 1626 pciternext(&pcfile); 1627 continue; 1628 } 1629 if(epc - s->value >= pcline.nextpc) { 1630 pciternext(&pcline); 1631 continue; 1632 } 1633 1634 if(file != pcfile.value) { 1635 cput(DW_LNS_set_file); 1636 uleb128put(pcfile.value); 1637 file = pcfile.value; 1638 } 1639 putpclcdelta(s->value + pcline.pc - pc, pcline.value - line); 1640 1641 pc = s->value + pcline.pc; 1642 line = pcline.value; 1643 if(pcfile.nextpc < pcline.nextpc) 1644 epc = pcfile.nextpc; 1645 else 1646 epc = pcline.nextpc; 1647 epc += s->value; 1648 } 1649 1650 da = 0; 1651 dwfunc->hash = varhash; // enable indexing of children by name 1652 memset(varhash, 0, sizeof varhash); 1653 for(a = s->autom; a; a = a->link) { 1654 switch (a->type) { 1655 case A_AUTO: 1656 dt = DW_ABRV_AUTO; 1657 offs = a->aoffset - PtrSize; 1658 break; 1659 case A_PARAM: 1660 dt = DW_ABRV_PARAM; 1661 offs = a->aoffset; 1662 break; 1663 default: 1664 continue; 1665 } 1666 if (strstr(a->asym->name, ".autotmp_")) 1667 continue; 1668 if (find(dwfunc, a->asym->name) != nil) 1669 n = mkvarname(a->asym->name, da); 1670 else 1671 n = a->asym->name; 1672 // Drop the package prefix from locals and arguments. 1673 nn = strrchr(n, '.'); 1674 if (nn) 1675 n = nn + 1; 1676 1677 dwvar = newdie(dwfunc, dt, n); 1678 newcfaoffsetattr(dwvar, offs); 1679 newrefattr(dwvar, DW_AT_type, defgotype(a->gotype)); 1680 1681 // push dwvar down dwfunc->child to preserve order 1682 newattr(dwvar, DW_AT_internal_location, DW_CLS_CONSTANT, offs, nil); 1683 dwfunc->child = dwvar->link; // take dwvar out from the top of the list 1684 for (dws = &dwfunc->child; *dws != nil; dws = &(*dws)->link) 1685 if (offs > getattr(*dws, DW_AT_internal_location)->value) 1686 break; 1687 dwvar->link = *dws; 1688 *dws = dwvar; 1689 1690 da++; 1691 } 1692 1693 dwfunc->hash = nil; 1694 } 1695 1696 flushunit(dwinfo, epc, epcs, unitstart, headerend - unitstart - 10); 1697 linesize = cpos() - lineo; 1698 } 1699 1700 /* 1701 * Emit .debug_frame 1702 */ 1703 enum 1704 { 1705 CIERESERVE = 16, 1706 DATAALIGNMENTFACTOR = -4, // TODO -PtrSize? 1707 FAKERETURNCOLUMN = 16 // TODO gdb6 doesn't like > 15? 1708 }; 1709 1710 static void 1711 putpccfadelta(vlong deltapc, vlong cfa) 1712 { 1713 cput(DW_CFA_def_cfa_offset_sf); 1714 sleb128put(cfa / DATAALIGNMENTFACTOR); 1715 1716 if (deltapc < 0x40) { 1717 cput(DW_CFA_advance_loc + deltapc); 1718 } else if (deltapc < 0x100) { 1719 cput(DW_CFA_advance_loc1); 1720 cput(deltapc); 1721 } else if (deltapc < 0x10000) { 1722 cput(DW_CFA_advance_loc2); 1723 WPUT(deltapc); 1724 } else { 1725 cput(DW_CFA_advance_loc4); 1726 LPUT(deltapc); 1727 } 1728 } 1729 1730 static void 1731 writeframes(void) 1732 { 1733 LSym *s; 1734 vlong fdeo, fdesize, pad; 1735 Pciter pcsp; 1736 uint32 nextpc; 1737 1738 if(framesec == S) 1739 framesec = linklookup(ctxt, ".dwarfframe", 0); 1740 framesec->nr = 0; 1741 frameo = cpos(); 1742 1743 // Emit the CIE, Section 6.4.1 1744 LPUT(CIERESERVE); // initial length, must be multiple of PtrSize 1745 LPUT(0xffffffff); // cid. 1746 cput(3); // dwarf version (appendix F) 1747 cput(0); // augmentation "" 1748 uleb128put(1); // code_alignment_factor 1749 sleb128put(DATAALIGNMENTFACTOR); // guess 1750 uleb128put(FAKERETURNCOLUMN); // return_address_register 1751 1752 cput(DW_CFA_def_cfa); 1753 uleb128put(DWARFREGSP); // register SP (**ABI-dependent, defined in l.h) 1754 uleb128put(PtrSize); // offset 1755 1756 cput(DW_CFA_offset + FAKERETURNCOLUMN); // return address 1757 uleb128put(-PtrSize / DATAALIGNMENTFACTOR); // at cfa - x*4 1758 1759 // 4 is to exclude the length field. 1760 pad = CIERESERVE + frameo + 4 - cpos(); 1761 if (pad < 0) { 1762 diag("dwarf: CIERESERVE too small by %lld bytes.", -pad); 1763 errorexit(); 1764 } 1765 strnput("", pad); 1766 1767 for(ctxt->cursym = ctxt->textp; ctxt->cursym != nil; ctxt->cursym = ctxt->cursym->next) { 1768 s = ctxt->cursym; 1769 if(s->pcln == nil) 1770 continue; 1771 1772 fdeo = cpos(); 1773 // Emit a FDE, Section 6.4.1, starting wit a placeholder. 1774 LPUT(0); // length, must be multiple of PtrSize 1775 LPUT(0); // Pointer to the CIE above, at offset 0 1776 addrput(0); // initial location 1777 addrput(0); // address range 1778 1779 for(pciterinit(ctxt, &pcsp, &s->pcln->pcsp); !pcsp.done; pciternext(&pcsp)) { 1780 nextpc = pcsp.nextpc; 1781 // pciterinit goes up to the end of the function, 1782 // but DWARF expects us to stop just before the end. 1783 if(nextpc == s->size) { 1784 nextpc--; 1785 if(nextpc < pcsp.pc) 1786 continue; 1787 } 1788 putpccfadelta(nextpc - pcsp.pc, PtrSize + pcsp.value); 1789 } 1790 1791 fdesize = cpos() - fdeo - 4; // exclude the length field. 1792 pad = rnd(fdesize, PtrSize) - fdesize; 1793 strnput("", pad); 1794 fdesize += pad; 1795 1796 // Emit the FDE header for real, Section 6.4.1. 1797 cseek(fdeo); 1798 LPUT(fdesize); 1799 if(linkmode == LinkExternal) { 1800 adddwarfrel(framesec, framesym, frameo, 4, 0); 1801 adddwarfrel(framesec, s, frameo, PtrSize, 0); 1802 } 1803 else { 1804 LPUT(0); 1805 addrput(s->value); 1806 } 1807 addrput(s->size); 1808 cseek(fdeo + 4 + fdesize); 1809 } 1810 1811 cflush(); 1812 framesize = cpos() - frameo; 1813 } 1814 1815 /* 1816 * Walk DWarfDebugInfoEntries, and emit .debug_info 1817 */ 1818 enum 1819 { 1820 COMPUNITHEADERSIZE = 4+2+4+1 1821 }; 1822 1823 static void 1824 writeinfo(void) 1825 { 1826 DWDie *compunit; 1827 vlong unitstart, here; 1828 1829 fwdcount = 0; 1830 if (infosec == S) 1831 infosec = linklookup(ctxt, ".dwarfinfo", 0); 1832 infosec->nr = 0; 1833 1834 if(arangessec == S) 1835 arangessec = linklookup(ctxt, ".dwarfaranges", 0); 1836 arangessec->nr = 0; 1837 1838 for (compunit = dwroot.child; compunit; compunit = compunit->link) { 1839 unitstart = cpos(); 1840 1841 // Write .debug_info Compilation Unit Header (sec 7.5.1) 1842 // Fields marked with (*) must be changed for 64-bit dwarf 1843 // This must match COMPUNITHEADERSIZE above. 1844 LPUT(0); // unit_length (*), will be filled in later. 1845 WPUT(2); // dwarf version (appendix F) 1846 1847 // debug_abbrev_offset (*) 1848 if(linkmode == LinkExternal) 1849 adddwarfrel(infosec, abbrevsym, infoo, 4, 0); 1850 else 1851 LPUT(0); 1852 1853 cput(PtrSize); // address_size 1854 1855 putdie(compunit); 1856 1857 here = cpos(); 1858 cseek(unitstart); 1859 LPUT(here - unitstart - 4); // exclude the length field. 1860 cseek(here); 1861 } 1862 cflush(); 1863 } 1864 1865 /* 1866 * Emit .debug_pubnames/_types. _info must have been written before, 1867 * because we need die->offs and infoo/infosize; 1868 */ 1869 static int 1870 ispubname(DWDie *die) 1871 { 1872 DWAttr *a; 1873 1874 switch(die->abbrev) { 1875 case DW_ABRV_FUNCTION: 1876 case DW_ABRV_VARIABLE: 1877 a = getattr(die, DW_AT_external); 1878 return a && a->value; 1879 } 1880 return 0; 1881 } 1882 1883 static int 1884 ispubtype(DWDie *die) 1885 { 1886 return die->abbrev >= DW_ABRV_NULLTYPE; 1887 } 1888 1889 static vlong 1890 writepub(int (*ispub)(DWDie*)) 1891 { 1892 DWDie *compunit, *die; 1893 DWAttr *dwa; 1894 vlong unitstart, unitend, sectionstart, here; 1895 1896 sectionstart = cpos(); 1897 1898 for (compunit = dwroot.child; compunit != nil; compunit = compunit->link) { 1899 unitstart = compunit->offs - COMPUNITHEADERSIZE; 1900 if (compunit->link != nil) 1901 unitend = compunit->link->offs - COMPUNITHEADERSIZE; 1902 else 1903 unitend = infoo + infosize; 1904 1905 // Write .debug_pubnames/types Header (sec 6.1.1) 1906 LPUT(0); // unit_length (*), will be filled in later. 1907 WPUT(2); // dwarf version (appendix F) 1908 LPUT(unitstart); // debug_info_offset (of the Comp unit Header) 1909 LPUT(unitend - unitstart); // debug_info_length 1910 1911 for (die = compunit->child; die != nil; die = die->link) { 1912 if (!ispub(die)) continue; 1913 LPUT(die->offs - unitstart); 1914 dwa = getattr(die, DW_AT_name); 1915 strnput(dwa->data, dwa->value + 1); 1916 } 1917 LPUT(0); 1918 1919 here = cpos(); 1920 cseek(sectionstart); 1921 LPUT(here - sectionstart - 4); // exclude the length field. 1922 cseek(here); 1923 1924 } 1925 1926 return sectionstart; 1927 } 1928 1929 /* 1930 * emit .debug_aranges. _info must have been written before, 1931 * because we need die->offs of dw_globals. 1932 */ 1933 static vlong 1934 writearanges(void) 1935 { 1936 DWDie *compunit; 1937 DWAttr *b, *e; 1938 int headersize; 1939 vlong sectionstart; 1940 vlong value; 1941 1942 sectionstart = cpos(); 1943 headersize = rnd(4+2+4+1+1, PtrSize); // don't count unit_length field itself 1944 1945 for (compunit = dwroot.child; compunit != nil; compunit = compunit->link) { 1946 b = getattr(compunit, DW_AT_low_pc); 1947 if (b == nil) 1948 continue; 1949 e = getattr(compunit, DW_AT_high_pc); 1950 if (e == nil) 1951 continue; 1952 1953 // Write .debug_aranges Header + entry (sec 6.1.2) 1954 LPUT(headersize + 4*PtrSize - 4); // unit_length (*) 1955 WPUT(2); // dwarf version (appendix F) 1956 1957 value = compunit->offs - COMPUNITHEADERSIZE; // debug_info_offset 1958 if(linkmode == LinkExternal) 1959 adddwarfrel(arangessec, infosym, sectionstart, 4, value); 1960 else 1961 LPUT(value); 1962 1963 cput(PtrSize); // address_size 1964 cput(0); // segment_size 1965 strnput("", headersize - (4+2+4+1+1)); // align to PtrSize 1966 1967 if(linkmode == LinkExternal) 1968 adddwarfrel(arangessec, (LSym*)b->data, sectionstart, PtrSize, b->value-((LSym*)b->data)->value); 1969 else 1970 addrput(b->value); 1971 1972 addrput(e->value - b->value); 1973 addrput(0); 1974 addrput(0); 1975 } 1976 cflush(); 1977 return sectionstart; 1978 } 1979 1980 static vlong 1981 writegdbscript(void) 1982 { 1983 vlong sectionstart; 1984 1985 sectionstart = cpos(); 1986 1987 if (gdbscript[0]) { 1988 cput(1); // magic 1 byte? 1989 strnput(gdbscript, strlen(gdbscript)+1); 1990 cflush(); 1991 } 1992 return sectionstart; 1993 } 1994 1995 static void 1996 align(vlong size) 1997 { 1998 if(HEADTYPE == Hwindows) // Only Windows PE need section align. 1999 strnput("", rnd(size, PEFILEALIGN) - size); 2000 } 2001 2002 static vlong 2003 writedwarfreloc(LSym* s) 2004 { 2005 int i; 2006 vlong start; 2007 Reloc *r; 2008 2009 start = cpos(); 2010 for(r = s->r; r < s->r+s->nr; r++) { 2011 if(iself) 2012 i = elfreloc1(r, r->off); 2013 else if(HEADTYPE == Hdarwin) 2014 i = machoreloc1(r, r->off); 2015 else 2016 i = -1; 2017 if(i < 0) 2018 diag("unsupported obj reloc %d/%d to %s", r->type, r->siz, r->sym->name); 2019 } 2020 return start; 2021 } 2022 2023 /* 2024 * This is the main entry point for generating dwarf. After emitting 2025 * the mandatory debug_abbrev section, it calls writelines() to set up 2026 * the per-compilation unit part of the DIE tree, while simultaneously 2027 * emitting the debug_line section. When the final tree contains 2028 * forward references, it will write the debug_info section in 2 2029 * passes. 2030 * 2031 */ 2032 void 2033 dwarfemitdebugsections(void) 2034 { 2035 vlong infoe; 2036 DWDie* die; 2037 2038 if(debug['w']) // disable dwarf 2039 return; 2040 2041 if(linkmode == LinkExternal && !iself) 2042 return; 2043 2044 // For diagnostic messages. 2045 newattr(&dwtypes, DW_AT_name, DW_CLS_STRING, strlen("dwtypes"), "dwtypes"); 2046 2047 mkindex(&dwroot); 2048 mkindex(&dwtypes); 2049 mkindex(&dwglobals); 2050 2051 // Some types that must exist to define other ones. 2052 newdie(&dwtypes, DW_ABRV_NULLTYPE, "<unspecified>"); 2053 newdie(&dwtypes, DW_ABRV_NULLTYPE, "void"); 2054 newdie(&dwtypes, DW_ABRV_BARE_PTRTYPE, "unsafe.Pointer"); 2055 2056 die = newdie(&dwtypes, DW_ABRV_BASETYPE, "uintptr"); // needed for array size 2057 newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_unsigned, 0); 2058 newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, PtrSize, 0); 2059 newattr(die, DW_AT_go_kind, DW_CLS_CONSTANT, KindUintptr, 0); 2060 2061 // Needed by the prettyprinter code for interface inspection. 2062 defgotype(lookup_or_diag("type.runtime._type")); 2063 defgotype(lookup_or_diag("type.runtime.interfacetype")); 2064 defgotype(lookup_or_diag("type.runtime.itab")); 2065 2066 genasmsym(defdwsymb); 2067 2068 writeabbrev(); 2069 align(abbrevsize); 2070 writelines(); 2071 align(linesize); 2072 writeframes(); 2073 align(framesize); 2074 2075 synthesizestringtypes(dwtypes.child); 2076 synthesizeslicetypes(dwtypes.child); 2077 synthesizemaptypes(dwtypes.child); 2078 synthesizechantypes(dwtypes.child); 2079 2080 reversetree(&dwroot.child); 2081 reversetree(&dwtypes.child); 2082 reversetree(&dwglobals.child); 2083 2084 movetomodule(&dwtypes); 2085 movetomodule(&dwglobals); 2086 2087 infoo = cpos(); 2088 writeinfo(); 2089 infoe = cpos(); 2090 pubnameso = infoe; 2091 pubtypeso = infoe; 2092 arangeso = infoe; 2093 gdbscripto = infoe; 2094 2095 if (fwdcount > 0) { 2096 if (debug['v']) 2097 Bprint(&bso, "%5.2f dwarf pass 2.\n", cputime()); 2098 cseek(infoo); 2099 writeinfo(); 2100 if (fwdcount > 0) { 2101 diag("dwarf: unresolved references after first dwarf info pass"); 2102 errorexit(); 2103 } 2104 if (infoe != cpos()) { 2105 diag("dwarf: inconsistent second dwarf info pass"); 2106 errorexit(); 2107 } 2108 } 2109 infosize = infoe - infoo; 2110 align(infosize); 2111 2112 pubnameso = writepub(ispubname); 2113 pubnamessize = cpos() - pubnameso; 2114 align(pubnamessize); 2115 2116 pubtypeso = writepub(ispubtype); 2117 pubtypessize = cpos() - pubtypeso; 2118 align(pubtypessize); 2119 2120 arangeso = writearanges(); 2121 arangessize = cpos() - arangeso; 2122 align(arangessize); 2123 2124 gdbscripto = writegdbscript(); 2125 gdbscriptsize = cpos() - gdbscripto; 2126 align(gdbscriptsize); 2127 2128 while(cpos()&7) 2129 cput(0); 2130 inforeloco = writedwarfreloc(infosec); 2131 inforelocsize = cpos() - inforeloco; 2132 align(inforelocsize); 2133 2134 arangesreloco = writedwarfreloc(arangessec); 2135 arangesrelocsize = cpos() - arangesreloco; 2136 align(arangesrelocsize); 2137 2138 linereloco = writedwarfreloc(linesec); 2139 linerelocsize = cpos() - linereloco; 2140 align(linerelocsize); 2141 2142 framereloco = writedwarfreloc(framesec); 2143 framerelocsize = cpos() - framereloco; 2144 align(framerelocsize); 2145 } 2146 2147 /* 2148 * Elf. 2149 */ 2150 enum 2151 { 2152 ElfStrDebugAbbrev, 2153 ElfStrDebugAranges, 2154 ElfStrDebugFrame, 2155 ElfStrDebugInfo, 2156 ElfStrDebugLine, 2157 ElfStrDebugLoc, 2158 ElfStrDebugMacinfo, 2159 ElfStrDebugPubNames, 2160 ElfStrDebugPubTypes, 2161 ElfStrDebugRanges, 2162 ElfStrDebugStr, 2163 ElfStrGDBScripts, 2164 ElfStrRelDebugInfo, 2165 ElfStrRelDebugAranges, 2166 ElfStrRelDebugLine, 2167 ElfStrRelDebugFrame, 2168 NElfStrDbg 2169 }; 2170 2171 vlong elfstrdbg[NElfStrDbg]; 2172 2173 void 2174 dwarfaddshstrings(LSym *shstrtab) 2175 { 2176 if(debug['w']) // disable dwarf 2177 return; 2178 2179 elfstrdbg[ElfStrDebugAbbrev] = addstring(shstrtab, ".debug_abbrev"); 2180 elfstrdbg[ElfStrDebugAranges] = addstring(shstrtab, ".debug_aranges"); 2181 elfstrdbg[ElfStrDebugFrame] = addstring(shstrtab, ".debug_frame"); 2182 elfstrdbg[ElfStrDebugInfo] = addstring(shstrtab, ".debug_info"); 2183 elfstrdbg[ElfStrDebugLine] = addstring(shstrtab, ".debug_line"); 2184 elfstrdbg[ElfStrDebugLoc] = addstring(shstrtab, ".debug_loc"); 2185 elfstrdbg[ElfStrDebugMacinfo] = addstring(shstrtab, ".debug_macinfo"); 2186 elfstrdbg[ElfStrDebugPubNames] = addstring(shstrtab, ".debug_pubnames"); 2187 elfstrdbg[ElfStrDebugPubTypes] = addstring(shstrtab, ".debug_pubtypes"); 2188 elfstrdbg[ElfStrDebugRanges] = addstring(shstrtab, ".debug_ranges"); 2189 elfstrdbg[ElfStrDebugStr] = addstring(shstrtab, ".debug_str"); 2190 elfstrdbg[ElfStrGDBScripts] = addstring(shstrtab, ".debug_gdb_scripts"); 2191 if(linkmode == LinkExternal) { 2192 if(thechar == '6' || thechar == '9') { 2193 elfstrdbg[ElfStrRelDebugInfo] = addstring(shstrtab, ".rela.debug_info"); 2194 elfstrdbg[ElfStrRelDebugAranges] = addstring(shstrtab, ".rela.debug_aranges"); 2195 elfstrdbg[ElfStrRelDebugLine] = addstring(shstrtab, ".rela.debug_line"); 2196 elfstrdbg[ElfStrRelDebugFrame] = addstring(shstrtab, ".rela.debug_frame"); 2197 } else { 2198 elfstrdbg[ElfStrRelDebugInfo] = addstring(shstrtab, ".rel.debug_info"); 2199 elfstrdbg[ElfStrRelDebugAranges] = addstring(shstrtab, ".rel.debug_aranges"); 2200 elfstrdbg[ElfStrRelDebugLine] = addstring(shstrtab, ".rel.debug_line"); 2201 elfstrdbg[ElfStrRelDebugFrame] = addstring(shstrtab, ".rel.debug_frame"); 2202 } 2203 2204 infosym = linklookup(ctxt, ".debug_info", 0); 2205 infosym->hide = 1; 2206 2207 abbrevsym = linklookup(ctxt, ".debug_abbrev", 0); 2208 abbrevsym->hide = 1; 2209 2210 linesym = linklookup(ctxt, ".debug_line", 0); 2211 linesym->hide = 1; 2212 2213 framesym = linklookup(ctxt, ".debug_frame", 0); 2214 framesym->hide = 1; 2215 } 2216 } 2217 2218 // Add section symbols for DWARF debug info. This is called before 2219 // dwarfaddelfheaders. 2220 void 2221 dwarfaddelfsectionsyms() 2222 { 2223 if(infosym != nil) { 2224 infosympos = cpos(); 2225 putelfsectionsym(infosym, 0); 2226 } 2227 if(abbrevsym != nil) { 2228 abbrevsympos = cpos(); 2229 putelfsectionsym(abbrevsym, 0); 2230 } 2231 if(linesym != nil) { 2232 linesympos = cpos(); 2233 putelfsectionsym(linesym, 0); 2234 } 2235 if(framesym != nil) { 2236 framesympos = cpos(); 2237 putelfsectionsym(framesym, 0); 2238 } 2239 } 2240 2241 static void 2242 dwarfaddelfrelocheader(int elfstr, ElfShdr *shdata, vlong off, vlong size) 2243 { 2244 ElfShdr *sh; 2245 2246 sh = newElfShdr(elfstrdbg[elfstr]); 2247 if(thechar == '6' || thechar == '9') { 2248 sh->type = SHT_RELA; 2249 } else { 2250 sh->type = SHT_REL; 2251 } 2252 sh->entsize = PtrSize*(2+(sh->type==SHT_RELA)); 2253 sh->link = elfshname(".symtab")->shnum; 2254 sh->info = shdata->shnum; 2255 sh->off = off; 2256 sh->size = size; 2257 sh->addralign = PtrSize; 2258 2259 } 2260 2261 void 2262 dwarfaddelfheaders(void) 2263 { 2264 ElfShdr *sh, *shinfo, *sharanges, *shline, *shframe; 2265 2266 if(debug['w']) // disable dwarf 2267 return; 2268 2269 sh = newElfShdr(elfstrdbg[ElfStrDebugAbbrev]); 2270 sh->type = SHT_PROGBITS; 2271 sh->off = abbrevo; 2272 sh->size = abbrevsize; 2273 sh->addralign = 1; 2274 if(abbrevsympos > 0) 2275 putelfsymshndx(abbrevsympos, sh->shnum); 2276 2277 sh = newElfShdr(elfstrdbg[ElfStrDebugLine]); 2278 sh->type = SHT_PROGBITS; 2279 sh->off = lineo; 2280 sh->size = linesize; 2281 sh->addralign = 1; 2282 if(linesympos > 0) 2283 putelfsymshndx(linesympos, sh->shnum); 2284 shline = sh; 2285 2286 sh = newElfShdr(elfstrdbg[ElfStrDebugFrame]); 2287 sh->type = SHT_PROGBITS; 2288 sh->off = frameo; 2289 sh->size = framesize; 2290 sh->addralign = 1; 2291 if(framesympos > 0) 2292 putelfsymshndx(framesympos, sh->shnum); 2293 shframe = sh; 2294 2295 sh = newElfShdr(elfstrdbg[ElfStrDebugInfo]); 2296 sh->type = SHT_PROGBITS; 2297 sh->off = infoo; 2298 sh->size = infosize; 2299 sh->addralign = 1; 2300 if(infosympos > 0) 2301 putelfsymshndx(infosympos, sh->shnum); 2302 shinfo = sh; 2303 2304 if (pubnamessize > 0) { 2305 sh = newElfShdr(elfstrdbg[ElfStrDebugPubNames]); 2306 sh->type = SHT_PROGBITS; 2307 sh->off = pubnameso; 2308 sh->size = pubnamessize; 2309 sh->addralign = 1; 2310 } 2311 2312 if (pubtypessize > 0) { 2313 sh = newElfShdr(elfstrdbg[ElfStrDebugPubTypes]); 2314 sh->type = SHT_PROGBITS; 2315 sh->off = pubtypeso; 2316 sh->size = pubtypessize; 2317 sh->addralign = 1; 2318 } 2319 2320 sharanges = nil; 2321 if (arangessize) { 2322 sh = newElfShdr(elfstrdbg[ElfStrDebugAranges]); 2323 sh->type = SHT_PROGBITS; 2324 sh->off = arangeso; 2325 sh->size = arangessize; 2326 sh->addralign = 1; 2327 sharanges = sh; 2328 } 2329 2330 if (gdbscriptsize) { 2331 sh = newElfShdr(elfstrdbg[ElfStrGDBScripts]); 2332 sh->type = SHT_PROGBITS; 2333 sh->off = gdbscripto; 2334 sh->size = gdbscriptsize; 2335 sh->addralign = 1; 2336 } 2337 2338 if(inforelocsize) 2339 dwarfaddelfrelocheader(ElfStrRelDebugInfo, shinfo, inforeloco, inforelocsize); 2340 2341 if(arangesrelocsize) 2342 dwarfaddelfrelocheader(ElfStrRelDebugAranges, sharanges, arangesreloco, arangesrelocsize); 2343 2344 if(linerelocsize) 2345 dwarfaddelfrelocheader(ElfStrRelDebugLine, shline, linereloco, linerelocsize); 2346 2347 if(framerelocsize) 2348 dwarfaddelfrelocheader(ElfStrRelDebugFrame, shframe, framereloco, framerelocsize); 2349 } 2350 2351 /* 2352 * Macho 2353 */ 2354 void 2355 dwarfaddmachoheaders(void) 2356 { 2357 MachoSect *msect; 2358 MachoSeg *ms; 2359 vlong fakestart; 2360 int nsect; 2361 2362 if(debug['w']) // disable dwarf 2363 return; 2364 2365 // Zero vsize segments won't be loaded in memory, even so they 2366 // have to be page aligned in the file. 2367 fakestart = abbrevo & ~0xfff; 2368 2369 nsect = 4; 2370 if (pubnamessize > 0) 2371 nsect++; 2372 if (pubtypessize > 0) 2373 nsect++; 2374 if (arangessize > 0) 2375 nsect++; 2376 if (gdbscriptsize > 0) 2377 nsect++; 2378 2379 ms = newMachoSeg("__DWARF", nsect); 2380 ms->fileoffset = fakestart; 2381 ms->filesize = abbrevo-fakestart; 2382 ms->vaddr = ms->fileoffset + segdata.vaddr - segdata.fileoff; 2383 2384 msect = newMachoSect(ms, "__debug_abbrev", "__DWARF"); 2385 msect->off = abbrevo; 2386 msect->size = abbrevsize; 2387 msect->addr = msect->off + segdata.vaddr - segdata.fileoff; 2388 ms->filesize += msect->size; 2389 2390 msect = newMachoSect(ms, "__debug_line", "__DWARF"); 2391 msect->off = lineo; 2392 msect->size = linesize; 2393 msect->addr = msect->off + segdata.vaddr - segdata.fileoff; 2394 ms->filesize += msect->size; 2395 2396 msect = newMachoSect(ms, "__debug_frame", "__DWARF"); 2397 msect->off = frameo; 2398 msect->size = framesize; 2399 msect->addr = msect->off + segdata.vaddr - segdata.fileoff; 2400 ms->filesize += msect->size; 2401 2402 msect = newMachoSect(ms, "__debug_info", "__DWARF"); 2403 msect->off = infoo; 2404 msect->size = infosize; 2405 msect->addr = msect->off + segdata.vaddr - segdata.fileoff; 2406 ms->filesize += msect->size; 2407 2408 if (pubnamessize > 0) { 2409 msect = newMachoSect(ms, "__debug_pubnames", "__DWARF"); 2410 msect->off = pubnameso; 2411 msect->size = pubnamessize; 2412 msect->addr = msect->off + segdata.vaddr - segdata.fileoff; 2413 ms->filesize += msect->size; 2414 } 2415 2416 if (pubtypessize > 0) { 2417 msect = newMachoSect(ms, "__debug_pubtypes", "__DWARF"); 2418 msect->off = pubtypeso; 2419 msect->size = pubtypessize; 2420 msect->addr = msect->off + segdata.vaddr - segdata.fileoff; 2421 ms->filesize += msect->size; 2422 } 2423 2424 if (arangessize > 0) { 2425 msect = newMachoSect(ms, "__debug_aranges", "__DWARF"); 2426 msect->off = arangeso; 2427 msect->size = arangessize; 2428 msect->addr = msect->off + segdata.vaddr - segdata.fileoff; 2429 ms->filesize += msect->size; 2430 } 2431 2432 // TODO(lvd) fix gdb/python to load MachO (16 char section name limit) 2433 if (gdbscriptsize > 0) { 2434 msect = newMachoSect(ms, "__debug_gdb_scripts", "__DWARF"); 2435 msect->off = gdbscripto; 2436 msect->size = gdbscriptsize; 2437 msect->addr = msect->off + segdata.vaddr - segdata.fileoff; 2438 ms->filesize += msect->size; 2439 } 2440 } 2441 2442 /* 2443 * Windows PE 2444 */ 2445 void 2446 dwarfaddpeheaders(void) 2447 { 2448 if(debug['w']) // disable dwarf 2449 return; 2450 2451 newPEDWARFSection(".debug_abbrev", abbrevsize); 2452 newPEDWARFSection(".debug_line", linesize); 2453 newPEDWARFSection(".debug_frame", framesize); 2454 newPEDWARFSection(".debug_info", infosize); 2455 newPEDWARFSection(".debug_pubnames", pubnamessize); 2456 newPEDWARFSection(".debug_pubtypes", pubtypessize); 2457 newPEDWARFSection(".debug_aranges", arangessize); 2458 newPEDWARFSection(".debug_gdb_scripts", gdbscriptsize); 2459 }