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
|
/*
Copyright (C) 2010-2018 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.
*/
// createirepfrombinary.cc
// Reads an object and inserts its dwarf data into
// an object intended to hold all the dwarf data.
#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>
#elif defined(_WIN32) && defined(_MSC_VER)
#include <io.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 <sys/stat.h> //open
#include <fcntl.h> //open
#include "strtabdata.h"
#include "dwarf.h"
#include "libdwarf.h"
#include "irepresentation.h"
#include "createirepfrombinary.h"
#include "general.h" // For IToHex()
using std::string;
using std::cout;
using std::cerr;
using std::endl;
using std::vector;
using std::list;
using std::map;
static void readFrameDataFromBinary(Dwarf_Debug dbg, IRepresentation & irep);
static void readMacroDataFromBinary(Dwarf_Debug dbg, IRepresentation & irep);
static void readCUDataFromBinary(Dwarf_Debug dbg, IRepresentation & irep);
static void readGlobals(Dwarf_Debug dbg, IRepresentation & irep);
/* Use a generic call to open the file, due to issues with Windows */
extern int open_a_file(const char * name);
extern int create_a_file(const char * name);
extern void close_a_file(int f);
static void errprint(Dwarf_Error err)
{
cerr << "Error num: " << dwarf_errno(err) << " " << dwarf_errmsg(err) << endl;
}
class DbgInAutoCloser {
public:
DbgInAutoCloser(Dwarf_Debug dbg,int fd): dbg_(dbg),fd_(fd) {};
~DbgInAutoCloser() {
Dwarf_Error err = 0;
dwarf_finish(dbg_,&err);
close(fd_);
};
private:
Dwarf_Debug dbg_;
int fd_;
};
void
createIrepFromBinary(const std::string &infile,
IRepresentation & irep)
{
Dwarf_Debug dbg = 0;
Dwarf_Error err;
int fd = open_a_file(infile.c_str());
if(fd < 0 ) {
cerr << "Unable to open " << infile <<
" for reading." << endl;
exit(1);
}
// All reader error handling is via the err argument.
int res = dwarf_init_b(fd,
DW_GROUPNUMBER_ANY,
DW_DLC_READ,
0,
0,
&dbg,
&err);
if(res == DW_DLV_NO_ENTRY) {
close_a_file(fd);
cerr << "Error init-ing " << infile <<
" for reading. dwarf_init_b(). Not object file." << endl;
exit(1);
} else if(res == DW_DLV_ERROR) {
close_a_file(fd);
cerr << "Error init-ing " << infile <<
" for reading. dwarf_init_b() failed. "
<< dwarf_errmsg(err)
<< endl;
exit(1);
}
DbgInAutoCloser closer(dbg,fd);
readFrameDataFromBinary(dbg,irep);
readCUDataFromBinary(dbg,irep);
readMacroDataFromBinary(dbg,irep);
readGlobals(dbg,irep);
return;
}
static void
readFrameDataFromBinary(Dwarf_Debug dbg, IRepresentation & irep)
{
Dwarf_Error err = 0;
Dwarf_Cie * cie_data = 0;
Dwarf_Signed cie_count = 0;
Dwarf_Fde * fde_data = 0;
Dwarf_Signed fde_count = 0;
bool have_standard_frame = true;
int res = dwarf_get_fde_list(dbg,
&cie_data,
&cie_count,
&fde_data,
&fde_count,
&err);
if(res == DW_DLV_NO_ENTRY) {
// No frame data we are dealing with.
#if 0
res = dwarf_get_fde_list_eh(dbg,
&cie_data,
&cie_count,
&fde_data,
&fde_count,
&err);
if(res == DW_DLV_NO_ENTRY) {
// No frame data.
return;
}
have_standard_frame = false;
#endif /* 0 */
return;
}
if(res == DW_DLV_ERROR) {
cerr << "Error reading frame data " << endl;
exit(1);
}
for(Dwarf_Signed i =0; i < cie_count; ++i) {
Dwarf_Unsigned bytes_in_cie = 0;
Dwarf_Small version = 0;
char * augmentation = 0;
Dwarf_Unsigned code_alignment_factor = 0;
Dwarf_Signed data_alignment_factor = 0;
Dwarf_Half return_address_register_rule = 0;
Dwarf_Ptr initial_instructions = 0;
Dwarf_Unsigned initial_instructions_length = 0;
res = dwarf_get_cie_info(cie_data[i], &bytes_in_cie,
&version,&augmentation, &code_alignment_factor,
&data_alignment_factor,&return_address_register_rule,
&initial_instructions,&initial_instructions_length,
&err);
if(res != DW_DLV_OK) {
errprint(err);
cerr << "Error reading frame data cie index " << i << endl;
exit(1);
}
// Index in cie_data must match index in ciedata_, here
// correct by construction.
IRCie cie(bytes_in_cie,version,augmentation,
code_alignment_factor,
data_alignment_factor,return_address_register_rule,
initial_instructions,initial_instructions_length);
if (have_standard_frame) {
irep.framedata().insert_cie(cie);
} else {
irep.ehframedata().insert_cie(cie);
}
}
for(Dwarf_Signed i =0; i < fde_count; ++i) {
Dwarf_Addr low_pc = 0;
Dwarf_Unsigned func_length = 0;
Dwarf_Ptr fde_bytes = 0;
Dwarf_Unsigned fde_byte_length = 0;
Dwarf_Off cie_offset = 0;
Dwarf_Signed cie_index = 0;
Dwarf_Off fde_offset = 0;
res = dwarf_get_fde_range(fde_data[i], &low_pc,
&func_length,&fde_bytes, &fde_byte_length,
&cie_offset,&cie_index,
&fde_offset,
&err);
if(res != DW_DLV_OK) {
cerr << "Error reading frame data fde index " << i << endl;
exit(1);
}
IRFde fde(low_pc,func_length,fde_bytes,fde_byte_length,
cie_offset, cie_index,fde_offset);
Dwarf_Ptr instr_in = 0;
Dwarf_Unsigned instr_len = 0;
res = dwarf_get_fde_instr_bytes(fde_data[i],
&instr_in, &instr_len, &err);
if(res != DW_DLV_OK) {
cerr << "Error reading frame data fde instructions " << i << endl;
exit(1);
}
fde.get_fde_instrs_into_ir(instr_in,instr_len);
if (have_standard_frame) {
irep.framedata().insert_fde(fde);
} else {
irep.ehframedata().insert_fde(fde);
}
}
dwarf_fde_cie_list_dealloc(dbg,cie_data,cie_count,
fde_data,fde_count);
}
static void
readCUMacroDataFromBinary(Dwarf_Debug dbg, IRepresentation & irep,
Dwarf_Unsigned macrodataoffset,IRCUdata &cu)
{
// Arbitrary, but way too high to be real!
// Probably should be lower.
Dwarf_Unsigned maxcount = 1000000000;
Dwarf_Error error;
Dwarf_Signed mcount=0;
Dwarf_Macro_Details *md = 0;
int res = dwarf_get_macro_details(dbg,macrodataoffset,
maxcount, &mcount, &md, &error);
if(res == DW_DLV_OK) {
IRMacro ¯odata = irep.macrodata();
vector<IRMacroRecord> mvec = macrodata.getMacroVec();
mvec.reserve(mcount);
Dwarf_Unsigned dieoffset = cu.baseDie().getGlobalOffset();
Dwarf_Macro_Details *mdp = md;
for(int i = 0; i < mcount; ++i, mdp++ ) {
IRMacroRecord ir(dieoffset,mdp->dmd_offset,
mdp->dmd_type, mdp->dmd_lineno,
mdp->dmd_fileindex, mdp->dmd_macro?mdp->dmd_macro:"");
mvec.push_back(ir);
}
}
dwarf_dealloc(dbg, md, DW_DLA_STRING);
}
static void
get_basic_attr_data_one_attr(Dwarf_Debug dbg,
Dwarf_Attribute attr,IRCUdata &cudata,IRAttr & irattr)
{
Dwarf_Error error;
Dwarf_Half attrnum = 0;
Dwarf_Half finalform = 0;
Dwarf_Half initialform = 0;
int res = dwarf_whatattr(attr,&attrnum,&error);
if(res != DW_DLV_OK) {
errprint(error);
cerr << "Unable to get attr number " << endl;
exit(1);
}
res = dwarf_whatform(attr,&finalform,&error);
if(res != DW_DLV_OK) {
errprint(error);
cerr << "Unable to get attr form " << endl;
exit(1);
}
res = dwarf_whatform_direct(attr,&initialform,&error);
if(res != DW_DLV_OK) {
errprint(error);
cerr << "Unable to get attr direct form " << endl;
exit(1);
}
irattr.setBaseData(attrnum,finalform,initialform);
enum Dwarf_Form_Class cl = dwarf_get_form_class(
cudata.getVersionStamp(), attrnum,
cudata.getOffsetSize(), finalform);
irattr.setFormClass(cl);
if (cl == DW_FORM_CLASS_UNKNOWN) {
cerr << "Unable to figure out form class. ver " <<
cudata.getVersionStamp() <<
" attrnum " << attrnum <<
" offsetsize " << cudata.getOffsetSize() <<
" formnum " << finalform << endl;
return;
}
irattr.setFormData(formFactory(dbg,attr,cudata,irattr));
}
static void
get_basic_die_data(Dwarf_Debug dbg UNUSEDARG,
Dwarf_Die indie,IRDie &irdie)
{
Dwarf_Half tagval = 0;
Dwarf_Error error = 0;
int res = dwarf_tag(indie,&tagval,&error);
if(res != DW_DLV_OK) {
errprint(error);
cerr << "Unable to get die tag "<< endl;
exit(1);
}
Dwarf_Off goff = 0;
res = dwarf_dieoffset(indie,&goff,&error);
if(res != DW_DLV_OK) {
errprint(error);
cerr << "Unable to get die offset "<< endl;
exit(1);
}
Dwarf_Off localoff = 0;
res = dwarf_die_CU_offset(indie,&localoff,&error);
if(res != DW_DLV_OK) {
errprint(error);
cerr << "Unable to get cu die offset "<< endl;
exit(1);
}
irdie.setBaseData(tagval,goff,localoff);
}
static void
get_attrs_of_die(Dwarf_Die in_die,IRDie &irdie,
IRCUdata &cudata,
IRepresentation &irep UNUSEDARG,
Dwarf_Debug dbg)
{
Dwarf_Error error = 0;
Dwarf_Attribute *atlist = 0;
Dwarf_Signed atcnt = 0;
std::list<IRAttr> &attrlist = irdie.getAttributes();
int res = dwarf_attrlist(in_die, &atlist,&atcnt,&error);
std::map<unsigned,unsigned> attrmap;
if(res == DW_DLV_NO_ENTRY) {
return;
}
if(res == DW_DLV_ERROR) {
errprint(error);
cerr << "dwarf_attrlist failed " << endl;
exit(1);
}
for (Dwarf_Signed i = 0; i < atcnt; ++i) {
Dwarf_Attribute attr = atlist[i];
Dwarf_Half attrnum = 0;
res = dwarf_whatattr(attr,&attrnum,&error);
if (res != DW_DLV_OK) {
cout << "ERROR FAIL: unable to get attrnum from attr!"
<<endl;
return;
}
std::map<unsigned,unsigned>::iterator m =
attrmap.find(attrnum);
if (m != attrmap.end()) {
// A duplicate! ignore. Compiler bug
// in some gcc versions.
continue;
}
attrmap[attrnum] = i;
// Use an empty attr to get a placeholder on
// the attr list for this IRDie.
IRAttr irattr;
attrlist.push_back(irattr);
// We want a pointer to the final attr to be
// recorded for references, not a local temp IRAttr.
IRAttr & lastirattr = attrlist.back();
get_basic_attr_data_one_attr(dbg,attr,cudata,lastirattr);
}
dwarf_dealloc(dbg,atlist, DW_DLA_LIST);
}
// Invariant: IRDie and IRCUdata are in the irep tree,
// not local record references to local scopes.
static void
get_children_of_die(Dwarf_Die in_die,IRDie&irdie,
IRCUdata &ircudata,
IRepresentation &irep,
Dwarf_Debug dbg)
{
Dwarf_Die curchilddie = 0;
Dwarf_Error error = 0;
int res = dwarf_child(in_die,&curchilddie,&error);
if(res == DW_DLV_NO_ENTRY) {
return;
}
if(res == DW_DLV_ERROR) {
errprint(error);
cerr << "dwarf_child failed " << endl;
exit(1);
}
//std::list<IRDie> & childlist = irdie.getChildren();
int childcount = 0;
for(;;) {
IRDie child;
get_basic_die_data(dbg,curchilddie,child);
irdie.addChild(child);
IRDie &lastchild = irdie.lastChild();
get_attrs_of_die(curchilddie,lastchild,ircudata,irep,dbg);
ircudata.insertLocalDieOffset(lastchild.getCURelativeOffset(),
&lastchild);
get_children_of_die(curchilddie,lastchild,ircudata,irep,dbg);
++childcount;
Dwarf_Die tchild = 0;
res = dwarf_siblingof(dbg,curchilddie,&tchild,&error);
if(res == DW_DLV_NO_ENTRY) {
break;
}
if(res == DW_DLV_ERROR) {
errprint(error);
cerr << "dwarf_siblingof failed " << endl;
exit(1);
}
dwarf_dealloc(dbg,curchilddie,DW_DLA_DIE);
curchilddie = tchild;
}
dwarf_dealloc(dbg,curchilddie,DW_DLA_DIE);
}
static void
get_linedata_of_cu_die(Dwarf_Die in_die,IRDie&irdie UNUSEDARG,
IRCUdata &ircudata,
IRepresentation &irep UNUSEDARG,
Dwarf_Debug dbg)
{
Dwarf_Error error = 0;
char **srcfiles = 0;
Dwarf_Signed srccount = 0;
IRCULineData&culinesdata = ircudata.getCULines();
int res = dwarf_srcfiles(in_die,&srcfiles, &srccount,&error);
if(res == DW_DLV_ERROR) {
errprint(error);
cerr << "dwarf_srcfiles failed " << endl;
exit(1);
} else if (res == DW_DLV_NO_ENTRY) {
// No data.
return;
}
std::vector<IRCUSrcfile> &srcs = culinesdata.get_cu_srcfiles();
for (Dwarf_Signed i = 0; i < srccount; ++i) {
IRCUSrcfile S(srcfiles[i]);
srcs.push_back(S);
/* use srcfiles[i] */
dwarf_dealloc(dbg, srcfiles[i], DW_DLA_STRING);
}
dwarf_dealloc(dbg, srcfiles, DW_DLA_LIST);
Dwarf_Line * linebuf = 0;
Dwarf_Signed linecnt = 0;
int res2 = dwarf_srclines(in_die,&linebuf,&linecnt, &error);
if(res2 == DW_DLV_ERROR) {
errprint(error);
cerr << "dwarf_srclines failed " << endl;
exit(1);
} else if (res2 == DW_DLV_NO_ENTRY) {
// No data.
cerr << "dwarf_srclines failed NO_ENTRY, crazy "
"since srcfiles worked" << endl;
exit(1);
}
std::vector<IRCULine> &lines = culinesdata.get_cu_lines();
for(Dwarf_Signed j = 0; j < linecnt ; ++j ) {
Dwarf_Line li = linebuf[j];
int lres;
Dwarf_Addr address = 0;
lres = dwarf_lineaddr(li,&address,&error);
if (lres != DW_DLV_OK) {
cerr << "dwarf_lineaddr failed. " << endl;
exit(1);
}
Dwarf_Bool is_addr_set = 0;
lres = dwarf_line_is_addr_set(li,&is_addr_set,&error);
if (lres != DW_DLV_OK) {
cerr << "dwarf_line_is_addr_set failed. " << endl;
exit(1);
}
Dwarf_Unsigned fileno = 0;
lres = dwarf_line_srcfileno(li,&fileno,&error);
if (lres != DW_DLV_OK) {
cerr << "dwarf_srcfileno failed. " << endl;
exit(1);
}
Dwarf_Unsigned lineno = 0;
lres = dwarf_lineno(li,&lineno,&error);
if (lres != DW_DLV_OK) {
cerr << "dwarf_lineno failed. " << endl;
exit(1);
}
Dwarf_Unsigned lineoff = 0;
lres = dwarf_lineoff_b(li,&lineoff,&error);
if (lres != DW_DLV_OK) {
cerr << "dwarf_lineoff failed. " << endl;
exit(1);
}
char *linesrctmp = 0;
lres = dwarf_linesrc(li,&linesrctmp,&error);
if (lres != DW_DLV_OK) {
cerr << "dwarf_linesrc failed. " << endl;
exit(1);
}
// libdwarf is trying to generate a full path,
// the string here is that generated data, not
// simply the 'file' path represented by the
// file number (fileno).
std::string linesrc(linesrctmp);
Dwarf_Bool is_stmt = 0;
lres = dwarf_linebeginstatement(li,&is_stmt,&error);
if (lres != DW_DLV_OK) {
cerr << "dwarf_linebeginstatement failed. " << endl;
exit(1);
}
Dwarf_Bool basic_block = 0;
lres = dwarf_lineblock(li,&basic_block,&error);
if (lres != DW_DLV_OK) {
cerr << "dwarf_lineblock failed. " << endl;
exit(1);
}
Dwarf_Bool end_sequence = 0;
lres = dwarf_lineendsequence(li,&end_sequence,&error);
if (lres != DW_DLV_OK) {
cerr << "dwarf_lineendsequence failed. " << endl;
exit(1);
}
Dwarf_Bool prologue_end = 0;
Dwarf_Bool epilogue_begin = 0;
Dwarf_Unsigned isa = 0;
Dwarf_Unsigned discriminator = 0;
lres = dwarf_prologue_end_etc(li,&prologue_end,
&epilogue_begin,&isa,&discriminator,&error);
if (lres != DW_DLV_OK) {
cerr << "dwarf_prologue_end_etc failed. " << endl;
exit(1);
}
IRCULine L(address,
is_addr_set,
fileno,
lineno,
lineoff,
linesrc,
is_stmt,
basic_block,
end_sequence,
prologue_end,epilogue_begin,
isa,discriminator);
lines.push_back(L);
}
dwarf_srclines_dealloc(dbg, linebuf, linecnt);
}
static bool
getToplevelOffsetAttr(Dwarf_Die cu_die,Dwarf_Half attrnumber,
Dwarf_Unsigned &offset_out)
{
Dwarf_Error error = 0;
Dwarf_Off offset = 0;
Dwarf_Attribute attr = 0;
int res = dwarf_attr(cu_die,attrnumber,&attr, &error);
bool foundit = false;
if(res == DW_DLV_OK) {
res = dwarf_global_formref(attr,&offset,&error);
if(res == DW_DLV_OK) {
foundit = true;
offset_out = offset;
}
}
return foundit;
}
// We record the .debug_info info for each CU found
// To start with we restrict attention to very few DIEs and
// attributes, but intend to get all eventually.
static void
readCUDataFromBinary(Dwarf_Debug dbg, IRepresentation & irep)
{
Dwarf_Error error;
int cu_number = 0;
std::list<IRCUdata> &culist = irep.infodata().getCUData();
for(;;++cu_number) {
Dwarf_Unsigned cu_header_length = 0;
Dwarf_Half version_stamp = 0;
Dwarf_Unsigned abbrev_offset = 0;
Dwarf_Half address_size = 0;
Dwarf_Unsigned next_cu_header = 0;
Dwarf_Half offset_size = 0;
Dwarf_Half extension_size = 0;
Dwarf_Die no_die = 0;
Dwarf_Die cu_die = 0;
int res = DW_DLV_ERROR;
res = dwarf_next_cu_header_b(dbg,&cu_header_length,
&version_stamp, &abbrev_offset, &address_size,
&offset_size, &extension_size,
&next_cu_header, &error);
if(res == DW_DLV_ERROR) {
errprint(error);
cerr <<"Error in dwarf_next_cu_header"<< endl;
exit(1);
}
if(res == DW_DLV_NO_ENTRY) {
/* Done. */
return;
}
IRCUdata cudata(cu_header_length,version_stamp,
abbrev_offset,address_size, offset_size,
extension_size,next_cu_header);
// The CU will have a single sibling (well, it is
// not exactly a sibling, but close enough), a cu_die.
res = dwarf_siblingof(dbg,no_die,&cu_die,&error);
if(res == DW_DLV_ERROR) {
errprint(error);
cerr <<"Error in dwarf_siblingof on CU die "<< endl;
exit(1);
}
if(res == DW_DLV_NO_ENTRY) {
/* Impossible case. */
cerr <<"no Entry! in dwarf_siblingof on CU die "<< endl;
exit(1);
}
Dwarf_Off macrooffset = 0;
bool foundmsect = getToplevelOffsetAttr(cu_die,DW_AT_macro_info,
macrooffset);
Dwarf_Off linesoffset = 0;
bool foundlines = getToplevelOffsetAttr(cu_die,DW_AT_stmt_list,
linesoffset);
Dwarf_Off dieoff = 0;
res = dwarf_dieoffset(cu_die,&dieoff,&error);
if(res != DW_DLV_OK) {
cerr << "Unable to get cu die offset for macro infomation "<< endl;
exit(1);
}
if(foundmsect) {
cudata.setMacroData(macrooffset,dieoff);
}
if(foundlines) {
cudata.setLineData(linesoffset,dieoff);
}
culist.push_back(cudata);
IRCUdata & treecu = irep.infodata().lastCU();
IRDie &cuirdie = treecu.baseDie();
get_basic_die_data(dbg,cu_die,cuirdie);
treecu.insertLocalDieOffset(cuirdie.getCURelativeOffset(),
&cuirdie);
get_attrs_of_die(cu_die,cuirdie,treecu,irep,dbg);
get_children_of_die(cu_die,cuirdie,treecu,irep,dbg);
get_linedata_of_cu_die(cu_die,cuirdie,treecu,irep,dbg);
// Now we have all local DIEs in the CU so we
// can identify all targets of local CLASS_REFERENCE
// and insert the IRDie * into the IRFormReference
treecu.updateReferenceAttrDieTargets();
dwarf_dealloc(dbg,cu_die,DW_DLA_DIE);
}
// If we want pointers from child to parent now is the time
// we can construct them.
}
// We read thru the CU headers and the CU die to find
// the macro info for each CU (if any).
// We record the CU macro info for each CU found (using
// the value of the DW_AT_macro_info attribute, if any).
static void
readMacroDataFromBinary(Dwarf_Debug dbg, IRepresentation & irep)
{
list<IRCUdata> &cudata = irep.infodata().getCUData();
list<IRCUdata>::iterator it = cudata.begin();
int ct = 0;
for( ; it != cudata.end(); ++it,++ct) {
Dwarf_Unsigned macrooffset = 0;
Dwarf_Unsigned cudieoffset = 0;
bool foundmsect = it->hasMacroData(¯ooffset,&cudieoffset);
if(foundmsect) {
readCUMacroDataFromBinary(dbg, irep, macrooffset,*it);
}
}
}
// Offsets refer to .debug_info, nothing else.
static void
readGlobals(Dwarf_Debug dbg, IRepresentation & irep)
{
Dwarf_Error error;
Dwarf_Global *globs = 0;
Dwarf_Type *types = 0;
Dwarf_Signed cnt = 0;
int res = 0;
IRPubsData &pubdata = irep.pubnamedata();
res = dwarf_get_globals(dbg, &globs,&cnt, &error);
if(res == DW_DLV_OK) {
std::list<IRPub> &pubnames = pubdata.getPubnames();
for (Dwarf_Signed i = 0; i < cnt; ++i) {
Dwarf_Global g = globs[i];
char *name = 0;
// dieoff and cuoff may be in .debug_info
// or .debug_types
Dwarf_Off dieoff = 0;
Dwarf_Off cuoff = 0;
res = dwarf_global_name_offsets(g,&name,
&dieoff,&cuoff,&error);
if( res == DW_DLV_OK) {
IRPub p(name,dieoff,cuoff);
dwarf_dealloc(dbg,name,DW_DLA_STRING);
pubnames.push_back(p);
}
}
dwarf_globals_dealloc(dbg, globs, cnt);
} else if (res == DW_DLV_ERROR) {
cerr << "dwarf_get_globals failed" << endl;
exit(1);
}
res = dwarf_get_pubtypes(dbg, &types,&cnt, &error);
if(res == DW_DLV_OK) {
std::list<IRPub> &pubtypes = pubdata.getPubtypes();
for (Dwarf_Signed i = 0; i < cnt; ++i) {
Dwarf_Type g = types[i];
char *name = 0;
// dieoff and cuoff may be in .debug_info
// or .debug_types
Dwarf_Off dieoff = 0;
Dwarf_Off cuoff = 0;
res = dwarf_pubtype_name_offsets(g,&name,
&dieoff,&cuoff,&error);
if( res == DW_DLV_OK) {
IRPub p(name,dieoff,cuoff);
dwarf_dealloc(dbg,name,DW_DLA_STRING);
pubtypes.push_back(p);
}
}
dwarf_types_dealloc(dbg, types, cnt);
} else if (res == DW_DLV_ERROR) {
cerr << "dwarf_get_globals failed" << endl;
exit(1);
}
}
|