1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
|
/*
Copyright (C) 2010-2019 David Anderson. All rights reserved.
Redistribution and use in source and binary forms, with
or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the example nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
Anderson BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// ireptodbg.cc
#include "config.h"
#ifdef HAVE_UNUSED_ATTRIBUTE
#define UNUSEDARG __attribute__ ((unused))
#else
#define UNUSEDARG
#endif
/* Windows specific header files */
#if defined(_WIN32) && defined(HAVE_STDAFX_H)
#include "stdafx.h"
#endif /* HAVE_STDAFX_H */
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h> /* for exit() */
#endif /* HAVE_STDLIB_H */
#include <iostream>
#include <string>
#include <list>
#include <map>
#include <vector>
#include <string.h> // For memset etc
#include "strtabdata.h"
#include "dwarf.h"
#include "libdwarf.h"
#include "irepresentation.h"
#include "ireptodbg.h"
#include "irepattrtodbg.h"
#include "general.h"
using std::string;
using std::cout;
using std::cerr;
using std::endl;
using std::vector;
using std::map;
using std::list;
using std::map;
static Dwarf_Error error;
typedef std::map<std::string,unsigned> pathToUnsignedType;
// The first special transformation is converting DW_AT_high_pc
// from FORM_addr to an offset and we choose FORM_uleb
// The attrs ref passed in is (sometimes) used to generate
// a new attrs list for the caller.
static void
specialAttrTransformations(Dwarf_P_Debug dbg UNUSEDARG,
IRepresentation & Irep UNUSEDARG,
Dwarf_P_Die ourdie UNUSEDARG,
IRDie &inDie,
list<IRAttr>& attrs,
unsigned level UNUSEDARG)
{
if(!cmdoptions.transformHighpcToConst) {
// No transformation of this sort requested.
return;
}
Dwarf_Half dietag = inDie.getTag();
if(dietag != DW_TAG_subprogram) {
return;
}
bool foundhipc= false;
bool foundlopc= false;
Dwarf_Addr lopcval = 0;
Dwarf_Addr hipcval = 0;
for (list<IRAttr>::iterator it = attrs.begin();
it != attrs.end();
it++) {
IRAttr & attr = *it;
Dwarf_Half attrnum = attr.getAttrNum();
Dwarf_Half attrform = attr.getFinalForm();
Dwarf_Form_Class formclass = attr.getFormClass();
if(attrnum == DW_AT_high_pc) {
if (attrform == DW_FORM_udata) {
// Already the right form for the test.
// Nothing to do.
return;
}
if (formclass != DW_FORM_CLASS_ADDRESS) {
return;
}
IRForm*f = attr.getFormData();
IRFormAddress *f2 = dynamic_cast<IRFormAddress *>(f);
hipcval = f2->getAddress();
foundhipc = true;
continue;
}
if(attrnum == DW_AT_low_pc) {
if (formclass != DW_FORM_CLASS_ADDRESS) {
return;
}
IRForm*f = attr.getFormData();
IRFormAddress *f2 = dynamic_cast<IRFormAddress *>(f);
lopcval = f2->getAddress();
foundlopc = true;
continue;
}
continue;
}
if(!foundlopc || !foundhipc) {
return;
}
Dwarf_Addr hipcoffset = hipcval - lopcval;
// Now we create a revised attribute.
list<IRAttr> revisedattrs;
for (list<IRAttr>::iterator it = attrs.begin();
it != attrs.end();
it++) {
IRAttr & attr = *it;
Dwarf_Half attrnum = attr.getAttrNum();
if(attrnum == DW_AT_high_pc) {
// Here we want to create a constant form
// to test that a const high_pc works.
// This is new in DWARF5.
IRAttr attr2(attrnum,
DW_FORM_udata,
DW_FORM_udata);
attr2.setFormClass(DW_FORM_CLASS_CONSTANT);
IRFormConstant *f = new IRFormConstant(
DW_FORM_udata,
DW_FORM_udata,
DW_FORM_CLASS_CONSTANT,
IRFormConstant::UNSIGNED,
hipcoffset,
0);
attr2.setFormData(f);
revisedattrs.push_back(attr2);
// Avoid memoryleak
attr.dropFormData();
continue;
}
revisedattrs.push_back(attr);
continue;
}
attrs = revisedattrs;
}
/* Create a data16 data item out of nothing... */
static void
addData16DataItem(Dwarf_P_Debug dbg UNUSEDARG,
IRepresentation & Irep UNUSEDARG,
Dwarf_P_Die ourdie UNUSEDARG,
IRDie &inDie,
IRDie &inParent,
list<IRAttr>& attrs,
unsigned level)
{
static bool alreadydone = false;
if (alreadydone) {
return;
}
if(!cmdoptions.adddata16) {
// No transformation of this sort requested.
return;
}
if (level < 2) {
return;
}
Dwarf_Half dietag = inDie.getTag();
Dwarf_Half parenttag = inParent.getTag();
if(dietag != DW_TAG_variable || parenttag != DW_TAG_subprogram) {
return;
}
list<IRAttr> revisedattrs;
for (list<IRAttr>::iterator it = attrs.begin();
it != attrs.end();
it++) {
IRAttr & attr = *it;
Dwarf_Half attrnum = attr.getAttrNum();
if(attrnum == DW_AT_name){
// Avoid memoryleak
attr.dropFormData();
continue;
}
if(attrnum == DW_AT_const_value){
// Avoid memoryleak
attr.dropFormData();
continue;
}
revisedattrs.push_back(attr);
}
// add two new attrs.
Dwarf_Half attrnum = DW_AT_name;
const char *attrname("vardata16");
IRAttr attr2(attrnum,
DW_FORM_string,
DW_FORM_string);
attr2.setFormClass(DW_FORM_CLASS_STRING);
IRFormString *f = new IRFormString();
f->setInitialForm(DW_FORM_string);
f->setFinalForm(DW_FORM_string);
f->setString(attrname);
attr2.setFormData(f);
revisedattrs.push_back(attr2);
Dwarf_Form_Data16 data16 = {
0x01,0x08,
0x02,0x07,
0x03,0x06,
0x04,0x05,
0x05,0x04,
0x06,0x03,
0x07,0x02,
0x08,0x01
};
IRAttr attrc(DW_AT_const_value, DW_FORM_data16,DW_FORM_data16);
attrc.setFormClass(DW_FORM_CLASS_CONSTANT);
IRFormConstant *fc = new IRFormConstant(
DW_FORM_data16,
DW_FORM_data16,
DW_FORM_CLASS_CONSTANT,
data16);
attrc.setFormData(fc);
revisedattrs.push_back(attrc);
attrs = revisedattrs;
alreadydone = true;
}
int testvals[3] = {
-2018,
-2019,
-2018
};
const char *testnames[3] = {
"myimplicitconst1",
"myimplicitconst2newabbrev",
"myimplicitconst3share1abbrev"
};
static void
addSUNfuncoffsets(Dwarf_P_Debug dbg ,
IRepresentation & Irep UNUSEDARG,
UNUSEDARG Dwarf_P_Die ourdie,
IRDie &inDie,
UNUSEDARG IRDie &inParent,
list<IRAttr>& attrs,
UNUSEDARG unsigned level)
{
if(!cmdoptions.addSUNfuncoffsets) {
// No transformation of this sort requested.
return;
}
Dwarf_Half dietag = inDie.getTag();
if (dietag == DW_TAG_compile_unit) {
}
if (dietag != DW_TAG_compile_unit) {
return;
}
// add new attr.
Dwarf_Half attrnum = DW_AT_SUN_func_offsets;
IRFormBlock *f = new IRFormBlock();
Dwarf_Signed signar[5] = {-1,2000,-2500000000ll,60000000000ll,-2};
Dwarf_Unsigned block_len = 0;
void *block_ptr;
int res = dwarf_compress_integer_block_a(dbg,
5,signar, &block_len,&block_ptr,&error);
if (res == DW_DLV_ERROR) {
cerr << " FAIL: Unable to generate via " <<
"dwarf_compress_integer_block_a: err " <<
dwarf_errmsg(error) << endl;
return;
} else if (res == DW_DLV_NO_ENTRY) {
cerr << " FAIL: NO_ENTRY impossible but got it" <<
" from dwarf_compress_integer_block_a: err " << endl;
return;
}
f->setInitialForm(DW_FORM_block);
f->setFinalForm(DW_FORM_block);
Dwarf_Block bl;
bl.bl_len = block_len;
bl.bl_data = block_ptr;
bl.bl_from_loclist = false;
bl.bl_section_offset = 0; // FAKE
f->insertBlock(&bl);
IRAttr attr1(attrnum,
DW_FORM_block,
DW_FORM_block);
attr1.setFormClass(DW_FORM_CLASS_BLOCK);
attr1.setFormData(f);
attrs.push_back(attr1);
}
static void
addImplicitConstItem(Dwarf_P_Debug dbg UNUSEDARG,
IRepresentation & Irep UNUSEDARG,
Dwarf_P_Die ourdie UNUSEDARG,
IRDie &inDie,
IRDie &inParent,
list<IRAttr>& attrs,
unsigned level)
{
static int alreadydone = 0;
if (alreadydone > 2) {
// The limit here MUST be below the size of
// testvals[] and testnames[] above.
// Some abbrevs should match
// if the test case is right. Others not.
return;
}
if(!cmdoptions.addimplicitconst) {
// No transformation of this sort requested.
return;
}
if (level < 2) {
return;
}
Dwarf_Half dietag = inDie.getTag();
Dwarf_Half parenttag = inParent.getTag();
if(dietag != DW_TAG_variable || parenttag != DW_TAG_subprogram) {
return;
}
list<IRAttr> revisedattrs;
for (list<IRAttr>::iterator it = attrs.begin();
it != attrs.end();
it++) {
IRAttr & attr = *it;
Dwarf_Half attrnum = attr.getAttrNum();
if(attrnum == DW_AT_name){
// Avoid memory leak.
attr.dropFormData();
continue;
}
if(attrnum == DW_AT_const_value){
// Avoid memory leak.
attr.dropFormData();
continue;
}
revisedattrs.push_back(attr);
}
// add two new attrs.
Dwarf_Half attrnum = DW_AT_name;
const char *attrname(testnames[alreadydone]);
IRAttr attr2(attrnum,
DW_FORM_string,
DW_FORM_string);
attr2.setFormClass(DW_FORM_CLASS_STRING);
IRFormString *f = new IRFormString();
f->setInitialForm(DW_FORM_implicit_const);
f->setFinalForm(DW_FORM_implicit_const);
f->setString(attrname);
attr2.setFormData(f);
revisedattrs.push_back(attr2);
Dwarf_Signed myconstval = testvals[alreadydone];
IRAttr attrc(DW_AT_const_value,
DW_FORM_implicit_const,DW_FORM_implicit_const);
attrc.setFormClass(DW_FORM_CLASS_CONSTANT);
IRFormConstant *fc = new IRFormConstant(
DW_FORM_implicit_const,
DW_FORM_implicit_const,
DW_FORM_CLASS_CONSTANT,
IRFormConstant::SIGNED,
0,myconstval);
attrc.setFormData(fc);
revisedattrs.push_back(attrc);
attrs = revisedattrs;
++alreadydone;
}
// Here we emit all the DIEs for a single Die and
// its children. When level == 0 the inDie is
// the CU die.
static Dwarf_P_Die
HandleOneDieAndChildren(Dwarf_P_Debug dbg,
IRepresentation &Irep,
IRCUdata &cu,
IRDie &inDie,
IRDie &inParent,
unsigned level)
{
list<IRDie>& children = inDie.getChildren();
// We create our target DIE first so we can link
// children to it, but add no content yet.
Dwarf_P_Die gendie = 0;
int res =dwarf_new_die_a(dbg,inDie.getTag(),NULL,NULL,
NULL,NULL,&gendie,&error);
if (res != DW_DLV_OK) {
cerr << "Die creation failure. "<< endl;
exit(1);
}
inDie.setGeneratedDie(gendie);
Dwarf_P_Die lastch = 0;
for ( list<IRDie>::iterator it = children.begin();
it != children.end();
it++) {
IRDie & ch = *it;
Dwarf_P_Die chp = HandleOneDieAndChildren(dbg,Irep,
cu,ch,inDie,level+1);
int res2 = 0;
if(lastch) {
// Link to right of earlier sibling.
res2 = dwarf_die_link_a(chp,NULL,NULL,lastch,NULL,&error);
} else {
// Link as first child.
res2 = dwarf_die_link_a(chp,gendie,NULL,NULL, NULL,&error);
}
if (res2 != DW_DLV_OK) {
cerr << "Die link failure. "<< endl;
exit(1);
}
lastch = chp;
}
{
list<IRAttr>& attrs = inDie.getAttributes();
// Now any special transformations to the attrs list.
specialAttrTransformations(dbg,Irep,gendie,inDie,attrs,level);
addData16DataItem(dbg,Irep,gendie,inDie,inParent,attrs,level);
addImplicitConstItem(dbg,Irep,gendie,inDie,inParent,attrs,level);
addSUNfuncoffsets(dbg,Irep,gendie,inDie,inParent,attrs,level);
// Now we add attributes (content), if any, to the
// output die 'gendie'.
for (list<IRAttr>::iterator it = attrs.begin();
it != attrs.end();
it++) {
IRAttr & attr = *it;
AddAttrToDie(dbg,Irep,cu,gendie,inDie,attr);
}
}
return gendie;
}
static void
HandleLineData(Dwarf_P_Debug dbg,
IRepresentation & Irep UNUSEDARG,
IRCUdata&cu)
{
Dwarf_Error lerror = 0;
// We refer to files by fileno, this builds an index.
pathToUnsignedType pathmap;
IRCULineData& ld = cu.getCULines();
std::vector<IRCULine> & cu_lines = ld.get_cu_lines();
//std::vector<IRCUSrcfile> &cu_srcfiles = ld.get_cu_srcfiles();
if(cu_lines.empty()) {
// No lines data to emit, do nothing.
return;
}
// To start with, we are doing a trivial generation here.
// To be refined 'soon'. FIXME
// Initially we don't worry about dwarf_add_directory_decl().
bool firstline = true;
bool addrsetincu = false;
for(unsigned k = 0; k < cu_lines.size(); ++k) {
IRCULine &li = cu_lines[k];
const std::string&path = li.getpath();
unsigned pathindex = 0;
pathToUnsignedType::const_iterator it = pathmap.find(path);
if(it == pathmap.end()) {
Dwarf_Error l2error = 0;
Dwarf_Unsigned idx = 0;
int res = dwarf_add_file_decl_a(
dbg,const_cast<char *>(path.c_str()),
0,0,0,&idx,&l2error);
if(res != DW_DLV_OK) {
cerr << "Error from dwarf_add_file_decl() on " <<
path << endl;
exit(1);
}
pathindex = idx;
pathmap[path] = pathindex;
} else {
pathindex = it->second;
}
Dwarf_Addr a = li.getaddr();
bool addrsetinline = li.getaddrset();
bool endsequence = li.getendsequence();
if(firstline || !addrsetincu) {
// We fake an elf sym index here.
Dwarf_Unsigned elfsymidx = 0;
if(firstline && !addrsetinline) {
cerr << "Error building line, first entry not addr set" <<
endl;
exit(1);
}
int res = dwarf_lne_set_address_a(dbg,
a,elfsymidx,&lerror);
if(res != DW_DLV_OK) {
cerr << "Error building line, dwarf_lne_set_address" <<
endl;
exit(1);
}
addrsetincu = true;
firstline = false;
} else if( endsequence) {
int res = dwarf_lne_end_sequence_a(dbg,
a,&lerror);
if(res != DW_DLV_OK) {
cerr << "Error building line, dwarf_lne_end_sequence" <<
endl;
exit(1);
}
addrsetincu = false;
continue;
}
Dwarf_Signed linecol = li.getlinecol();
// It's really the code address or (when in a proper compiler)
// a section or function offset.
// libdwarf subtracts the code_offset from the address passed
// this way or from dwarf_lne_set_address() and writes a small
// offset in a DW_LNS_advance_pc instruction.
Dwarf_Addr code_offset = a;
Dwarf_Unsigned lineno = li.getlineno();
Dwarf_Bool isstmt = li.getisstmt()?1:0;
Dwarf_Bool isblock = li.getisblock()?1:0;
Dwarf_Bool isepiloguebegin = li.getepiloguebegin()?1:0;
Dwarf_Bool isprologueend = li.getprologueend()?1:0;
Dwarf_Unsigned isa = li.getisa();
Dwarf_Unsigned discriminator = li.getdiscriminator();
int lires = dwarf_add_line_entry_c(dbg,
pathindex,
code_offset,
lineno,
linecol,
isstmt,
isblock,
isepiloguebegin,
isprologueend,
isa,
discriminator,
&lerror);
if(lires != DW_DLV_OK) {
cerr << "Error building line, dwarf_add_line_entry" <<
endl;
exit(1);
}
}
if(addrsetincu) {
cerr << "CU Lines did not end in an end_sequence!" << endl;
}
}
// This emits the DIEs for a single CU and possibly line data
// associated with the CU.
// The DIEs form a graph (which can be created and linked together
// in any order) and which is emitted in tree preorder as
// defined by the DWARF spec.
//
static void
emitOneCU( Dwarf_P_Debug dbg,IRepresentation & Irep,
IRCUdata&cu,
int cu_of_input_we_output UNUSEDARG)
{
// We descend the the tree, creating DIEs and linking
// them in as we return back up the tree of recursing
// on IRDie children.
Dwarf_Error lerror;
IRDie & basedie = cu.baseDie();
Dwarf_P_Die cudie = HandleOneDieAndChildren(dbg,Irep,
cu,basedie,basedie,0);
// Add base die to debug, this is the CU die.
// This is not a good design as DWARF3/4 have
// requirements of multiple CUs in a single creation,
// which cannot be handled yet.
Dwarf_Unsigned res = dwarf_add_die_to_debug(dbg,cudie,&lerror);
if(res != DW_DLV_OK) {
cerr << "Unable to add_die_to_debug " << endl;
exit(1);
}
// Does fixup of IRFormReference targets.
cu.updateClassReferenceTargets();
HandleLineData(dbg,Irep,cu);
}
// .debug_info creation.
// Also creates .debug_line
static void
transform_debug_info(Dwarf_P_Debug dbg,
IRepresentation & irep,int cu_of_input_we_output)
{
int cu_number = 0;
std::list<IRCUdata> &culist = irep.infodata().getCUData();
// For now, just one CU we write (as spoken by Yoda).
for ( list<IRCUdata>::iterator it = culist.begin();
it != culist.end();
it++,cu_number++) {
if(cu_number == cu_of_input_we_output) {
IRCUdata & primecu = *it;
emitOneCU(dbg,irep,primecu,cu_of_input_we_output);
break;
}
}
}
static void
transform_cie_fde(Dwarf_P_Debug dbg,
IRepresentation & Irep,
int cu_of_input_we_output UNUSEDARG)
{
Dwarf_Error err = 0;
std::vector<IRCie> &cie_vec =
Irep.framedata().get_cie_vec();
std::vector<IRFde> &fde_vec =
Irep.framedata().get_fde_vec();
Dwarf_Unsigned cievecsize = cie_vec.size();
if (!cievecsize) {
// If debug_frame missing try for eh_frame.
// Just do one section, not both, for now.
cie_vec = Irep.ehframedata().get_cie_vec();
fde_vec = Irep.ehframedata().get_fde_vec();
cievecsize = cie_vec.size();
}
for(Dwarf_Unsigned i = 0; i < cievecsize ; ++i) {
IRCie &ciein = cie_vec[i];
Dwarf_Unsigned version = 0;
string aug;
Dwarf_Unsigned code_align = 0;
Dwarf_Signed data_align = 0;
Dwarf_Half ret_addr_reg = -1;
void * bytes = 0;
Dwarf_Unsigned bytes_len = 0;
Dwarf_Unsigned out_cie_index = 0;
ciein.get_basic_cie_data(&version, &aug,
&code_align, &data_align, &ret_addr_reg);
ciein.get_init_instructions(&bytes_len,&bytes);
// version implied: FIXME, need to let user code set output
// frame version.
char *str = const_cast<char *>(aug.c_str());
int res = dwarf_add_frame_cie_a(dbg, str,
code_align, data_align, ret_addr_reg,
bytes,bytes_len,
&out_cie_index,
&err);
if(res != DW_DLV_OK) {
cerr << "Error creating cie from input cie " << i << endl;
exit(1);
}
vector<int> fdeindex;
// This inner loop is C*F so a bit slow.
for(size_t j = 0; j < fde_vec.size(); ++j) {
IRFde &fdein = fde_vec[j];
Dwarf_Unsigned code_len = 0;
Dwarf_Addr code_virt_addr = 0;
Dwarf_Unsigned cie_input_index = 0;
fdein.get_fde_base_data(&code_virt_addr,
&code_len, &cie_input_index);
if(cie_input_index != i) {
// Wrong cie, ignore this fde right now.
continue;
}
Dwarf_P_Fde fdeout = 0;
res = dwarf_new_fde_a(dbg,&fdeout,&err);
if(res != DW_DLV_OK) {
cerr << "Error creating new fde " << j << endl;
exit(1);
}
Dwarf_Unsigned ilen = 0;
void *instrs = 0;
fdein.get_fde_instructions(&ilen, &instrs);
res = dwarf_insert_fde_inst_bytes(dbg,
fdeout, ilen, instrs,&err);
if(res != DW_DLV_OK) {
cerr << "Error inserting frame instr block " << j << endl;
exit(1);
}
Dwarf_P_Die irix_die = 0;
Dwarf_Signed irix_table_offset = 0;
Dwarf_Unsigned irix_excep_sym = 0;
Dwarf_Unsigned code_virt_addr_symidx =
Irep.getBaseTextSymbol();
Dwarf_Unsigned fde_index = 0;
Dwarf_Unsigned end_symbol_index = 0;
Dwarf_Unsigned offset_from_end_symbol = 0;
res = dwarf_add_frame_info_c(
dbg, fdeout,irix_die,
out_cie_index, code_virt_addr,
code_len,
code_virt_addr_symidx,
end_symbol_index,
offset_from_end_symbol,
irix_table_offset,irix_excep_sym,
&fde_index,
&err);
if(res != DW_DLV_OK) {
cerr << "Error creating new fde " << j << endl;
exit(1);
}
}
}
if (cmdoptions.addframeadvanceloc) {
// Add a whole new fde, cie, and some instructions
Dwarf_Unsigned code_align = 1;
Dwarf_Signed data_align = 1;
Dwarf_Half ret_addr_reg = 2; // fake, of course.
void * bytes = 0;
Dwarf_Unsigned bytes_len = 0;
Dwarf_Unsigned out_cie_index = 0;
const char *augstr = "";
int res = dwarf_add_frame_cie_a(dbg,
(char *)augstr,
code_align,
data_align,
ret_addr_reg,
bytes,bytes_len,
&out_cie_index,
&err);
if(res != DW_DLV_OK) {
cerr << "Error creating made-up addframeadvanceloc cie "
<< endl;
exit(1);
}
Dwarf_P_Fde fdeout = 0;
res = dwarf_new_fde_a(dbg,&fdeout,&err);
if(res != DW_DLV_OK) {
cerr << "Error creating addframeadvance fde " << endl;
exit(1);
}
// These lead to a set of adv_loc ops values in the output
// for the fde which looks odd in -f
// output (-vvv -f makes more sense), might be
// better to have some additional frame instrs in there
// so plain -f looks more sensible.
std::list<Dwarf_Unsigned> adval;
adval.push_back(48);
adval.push_back(64);
adval.push_back(17219);
adval.push_back(4408131);
adval.push_back(18308350787ull);
// 0x30 0x40 0x4343 0x434343 0x434343434
unsigned i = 3;
for( list<Dwarf_Unsigned>::iterator it =
adval.begin();
it != adval.end();it++ , ++i ) {
Dwarf_Unsigned v = *it;
res = dwarf_add_fde_inst_a(fdeout,
DW_CFA_advance_loc,v,0,&err);
if (res != DW_DLV_OK) {
cerr << "Error adding advance_loc" << v << endl;
exit(1);
}
res = dwarf_add_fde_inst_a(fdeout,
DW_CFA_same_value,i,0,&err);
if (res != DW_DLV_OK) {
cerr << "Error adding dummy same_value op" << v << endl;
exit(1);
}
}
// We increase code len to account for the
// big advance_loc values inserted above.
Dwarf_P_Die irix_die = 0;
Dwarf_Signed irix_table_offset = 0;
Dwarf_Unsigned irix_excep_sym = 0;
Dwarf_Unsigned code_virt_addr_symidx =
Irep.getBaseTextSymbol();
Dwarf_Unsigned fde_index = 0;
Dwarf_Unsigned end_symbol_index = 0;
Dwarf_Unsigned offset_from_end_symbol = 0;
Dwarf_Addr code_virt_addr = 0;
Dwarf_Addr code_len = 0x444343434;
res = dwarf_add_frame_info_c(
dbg, fdeout,irix_die,
out_cie_index, code_virt_addr,
code_len,
code_virt_addr_symidx,
end_symbol_index,
offset_from_end_symbol,
irix_table_offset,irix_excep_sym,
&fde_index,
&err);
if(res != DW_DLV_OK) {
cerr << "Error creating advance_loc fde " << endl;
exit(1);
}
}
}
static void
transform_macro_info(Dwarf_P_Debug dbg,
IRepresentation & Irep,
int cu_of_input_we_output UNUSEDARG)
{
IRMacro ¯odata = Irep.macrodata();
std::vector<IRMacroRecord> ¯ov = macrodata.getMacroVec();
for(size_t m = 0; m < macrov.size() ; m++ ) {
// FIXME: we need to coordinate with generated
// CUs .
cout << "FIXME: macros not really output yet " <<
m << " " <<
macrov.size() << endl;
}
Dwarf_Unsigned reloc_count = 0;
int drd_version = 0;
int res = dwarf_get_relocation_info_count(dbg,&reloc_count,
&drd_version,&error);
if( res != DW_DLV_OK) {
cerr << "Error getting relocation info count." << endl;
exit(1);
}
for( Dwarf_Unsigned ct = 0; ct < reloc_count ; ++ct) {
}
}
// Starting at a Die, look through its children
// in its input to find which one we have by
// comparing the input-die global offset.
static
Dwarf_P_Die findTargetDieByOffset(IRDie& indie,
Dwarf_Unsigned targetglobaloff)
{
Dwarf_Unsigned globoff = indie.getGlobalOffset();
if(globoff == targetglobaloff) {
return indie.getGeneratedDie();
}
std::list<IRDie> dielist = indie.getChildren();
for ( list<IRDie>::iterator it = dielist.begin();
it != dielist.end();
it++) {
IRDie &ldie = *it;
Dwarf_P_Die foundDie = findTargetDieByOffset(ldie,
targetglobaloff);
if(foundDie) {
return foundDie;
}
}
return NULL;
}
// If the pubnames/pubtypes entry is in the
// cu we are emitting, find the generated output
// Dwarf_P_Die in that CU
// and attach the pubname/type entry to it.
static void
transform_debug_pubnames_types_inner(Dwarf_P_Debug dbg,
IRepresentation & Irep,
int cu_of_input_we_output UNUSEDARG,
IRCUdata&cu)
{
// First, get the target CU. */
Dwarf_Unsigned targetcuoff= cu.getCUdieOffset();
IRDie &basedie = cu.baseDie();
IRPubsData& pubs = Irep.pubnamedata();
std::list<IRPub> &nameslist = pubs.getPubnames();
if(!nameslist.empty()) {
for ( list<IRPub>::iterator it = nameslist.begin();
it != nameslist.end();
it++) {
IRPub &pub = *it;
Dwarf_Unsigned pubcuoff= pub.getCUdieOffset();
Dwarf_Unsigned ourdieoff= pub.getDieOffset();
if (pubcuoff != targetcuoff) {
continue;
}
Dwarf_P_Die targdie = findTargetDieByOffset(basedie,
ourdieoff);
if(targdie) {
// Ugly. Old mistake in libdwarf declaration.
char *mystr = const_cast<char *>(pub.getName().c_str());
Dwarf_Unsigned res = dwarf_add_pubname(
dbg,targdie,
mystr,
&error);
if(!res) {
cerr << "Failed to add pubname entry for offset"
<< ourdieoff
<< "in CU at offset " << pubcuoff << endl;
exit(1);
}
} else {
cerr << "Did not find target pubname P_Die for offset "
<< ourdieoff
<< "in CU at offset " << pubcuoff << endl;
}
}
}
std::list<IRPub> &typeslist = pubs.getPubtypes();
if(!typeslist.empty()) {
for ( list<IRPub>::iterator it = typeslist.begin();
it != typeslist.end();
it++) {
IRPub &pub = *it;
Dwarf_Unsigned pubcuoff= pub.getCUdieOffset();
Dwarf_Unsigned ourdieoff= pub.getDieOffset();
if (pubcuoff != targetcuoff) {
continue;
}
Dwarf_P_Die targdie = findTargetDieByOffset(basedie,
ourdieoff);
if(targdie) {
// Ugly. Old mistake in libdwarf declaration.
char *mystr = const_cast<char *>(pub.getName().c_str());
Dwarf_Unsigned res = dwarf_add_pubtype(
dbg,targdie,
mystr,
&error);
if(!res) {
cerr << "Failed to add pubtype entry for offset"
<< ourdieoff
<< "in CU at offset " << pubcuoff << endl;
exit(1);
}
} else {
cerr << "Did not find target pubtype P_Die for offset "
<< ourdieoff
<< "in CU at offset " << pubcuoff << endl;
}
}
}
}
// This looks for pubnames/pubtypes
// to generate based on an object file input.
static void
transform_debug_pubnames_types(Dwarf_P_Debug dbg,
IRepresentation & Irep,int cu_of_input_we_output)
{
int cu_number = 0;
std::list<IRCUdata> &culist = Irep.infodata().getCUData();
// For now, just one CU we write (as spoken by Yoda).
for ( list<IRCUdata>::iterator it = culist.begin();
it != culist.end();
it++,cu_number++) {
if(cu_number == cu_of_input_we_output) {
IRCUdata & primecu = *it;
transform_debug_pubnames_types_inner(
dbg,Irep,cu_of_input_we_output,primecu);
break;
}
}
}
void
transform_irep_to_dbg(Dwarf_P_Debug dbg,
IRepresentation & Irep,int cu_of_input_we_output)
{
transform_debug_info(dbg,Irep,cu_of_input_we_output);
transform_cie_fde(dbg,Irep,cu_of_input_we_output);
transform_macro_info(dbg,Irep,cu_of_input_we_output);
transform_debug_pubnames_types(dbg,Irep,cu_of_input_we_output);
}
|