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