| 1 |
/* |
| 2 |
Copyright (C) 2010-2018 David Anderson. All rights reserved. |
| 3 |
|
| 4 |
Redistribution and use in source and binary forms, with or without |
| 5 |
modification, are permitted provided that the following |
| 6 |
conditions are met: |
| 7 |
* Redistributions of source code must retain the above copyright |
| 8 |
notice, this list of conditions and the following disclaimer. |
| 9 |
* Redistributions in binary form must reproduce the above copyright |
| 10 |
notice, this list of conditions and the following |
| 11 |
disclaimer in the |
| 12 |
documentation and/or other materials provided with the |
| 13 |
distribution. |
| 14 |
* Neither the name of the example nor the |
| 15 |
names of its contributors may be used to endorse or |
| 16 |
promote products |
| 17 |
derived from this software without specific prior |
| 18 |
written permission. |
| 19 |
|
| 20 |
THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS'' |
| 21 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
| 22 |
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 |
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
ARE DISCLAIMED. IN NO EVENT SHALL David Anderson BE |
| 25 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 30 |
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 32 |
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 33 |
OF SUCH DAMAGE. |
| 34 |
|
| 35 |
*/ |
| 36 |
|
| 37 |
// createirepfrombinary.cc |
| 38 |
|
| 39 |
// Reads an object and inserts its dwarf data into |
| 40 |
// an object intended to hold all the dwarf data. |
| 41 |
|
| 42 |
#include "config.h" |
| 43 |
/* Windows specific header files */ |
| 44 |
#if defined(_WIN32) && defined(HAVE_STDAFX_H) |
| 45 |
#include "stdafx.h" |
| 46 |
#endif /* HAVE_STDAFX_H */ |
| 47 |
|
| 48 |
#ifdef _WIN32 |
| 49 |
#include <io.h> |
| 50 |
#elif defined HAVE_UNISTD_H |
| 51 |
#include <unistd.h> |
| 52 |
#endif /* _WIN32 */ |
| 53 |
#include <stdlib.h> /* for exit() */ |
| 54 |
#include <iostream> |
| 55 |
#include <string> |
| 56 |
#include <list> |
| 57 |
#include <map> |
| 58 |
#include <vector> |
| 59 |
#include <string.h> // For memset etc |
| 60 |
#include <sys/stat.h> //open |
| 61 |
#include <fcntl.h> //open |
| 62 |
#include "strtabdata.h" |
| 63 |
#include "dwarf.h" |
| 64 |
#include "libdwarf.h" |
| 65 |
#include "libdwarfp.h" |
| 66 |
#include "libdwarf_private.h" |
| 67 |
#include "irepresentation.h" |
| 68 |
#include "createirepfrombinary.h" |
| 69 |
#include "general.h" // For IToHex() |
| 70 |
|
| 71 |
using std::string; |
| 72 |
using std::cout; |
| 73 |
using std::cerr; |
| 74 |
using std::endl; |
| 75 |
using std::vector; |
| 76 |
using std::list; |
| 77 |
using std::map; |
| 78 |
|
| 79 |
static void readFrameDataFromBinary(Dwarf_Debug dbg, |
| 80 |
IRepresentation & irep); |
| 81 |
static void readMacroDataFromBinary(Dwarf_Debug dbg, |
| 82 |
IRepresentation & irep); |
| 83 |
static void readCUDataFromBinary(Dwarf_Debug dbg, |
| 84 |
IRepresentation & irep); |
| 85 |
static void readGlobals(Dwarf_Debug dbg, IRepresentation & irep); |
| 86 |
|
| 87 |
/* Use a generic call to open the file, due to issues with Windows */ |
| 88 |
extern int open_a_file(const char * name); |
| 89 |
extern int create_a_file(const char * name); |
| 90 |
extern void close_a_file(int f); |
| 91 |
|
| 92 |
static void errprint(Dwarf_Error err) |
| 93 |
{ |
| 94 |
cerr << "Error num: " << dwarf_errno(err) << |
| 95 |
" " << dwarf_errmsg(err) << endl; |
| 96 |
} |
| 97 |
|
| 98 |
class DbgInAutoCloser { |
| 99 |
public: |
| 100 |
DbgInAutoCloser(Dwarf_Debug dbg,int fd): dbg_(dbg),fd_(fd) {}; |
| 101 |
~DbgInAutoCloser() { |
| 102 |
dwarf_finish(dbg_); |
| 103 |
close(fd_); |
| 104 |
}; |
| 105 |
private: |
| 106 |
Dwarf_Debug dbg_; |
| 107 |
int fd_; |
| 108 |
}; |
| 109 |
|
| 110 |
void |
| 111 |
createIrepFromBinary(const std::string &infile, |
| 112 |
IRepresentation & irep) |
| 113 |
{ |
| 114 |
Dwarf_Debug dbg = 0; |
| 115 |
Dwarf_Error err; |
| 116 |
int fd = open_a_file(infile.c_str()); |
| 117 |
if (fd < 0 ) { |
| 118 |
cerr << "Unable to open " << infile << |
| 119 |
" for reading." << endl; |
| 120 |
exit(1); |
| 121 |
} |
| 122 |
// All reader error handling is via the err argument. |
| 123 |
int res = dwarf_init_b(fd, |
| 124 |
DW_GROUPNUMBER_ANY, |
| 125 |
0, |
| 126 |
0, |
| 127 |
&dbg, |
| 128 |
&err); |
| 129 |
if (res == DW_DLV_NO_ENTRY) { |
| 130 |
close_a_file(fd); |
| 131 |
cerr << "Error init-ing " << infile << |
| 132 |
" for reading. dwarf_init_b(). Not object file." << endl; |
| 133 |
exit(1); |
| 134 |
} else if (res == DW_DLV_ERROR) { |
| 135 |
close_a_file(fd); |
| 136 |
cerr << "Error init-ing " << infile << |
| 137 |
" for reading. dwarf_init_b() failed. " |
| 138 |
<< dwarf_errmsg(err) |
| 139 |
<< endl; |
| 140 |
exit(1); |
| 141 |
} |
| 142 |
|
| 143 |
DbgInAutoCloser closer(dbg,fd); |
| 144 |
|
| 145 |
readFrameDataFromBinary(dbg,irep); |
| 146 |
readCUDataFromBinary(dbg,irep); |
| 147 |
readMacroDataFromBinary(dbg,irep); |
| 148 |
readGlobals(dbg,irep); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
static void |
| 153 |
readFrameDataFromBinary(Dwarf_Debug dbg, IRepresentation & irep) |
| 154 |
{ |
| 155 |
Dwarf_Error err = 0; |
| 156 |
Dwarf_Cie * cie_data = 0; |
| 157 |
Dwarf_Signed cie_count = 0; |
| 158 |
Dwarf_Fde * fde_data = 0; |
| 159 |
Dwarf_Signed fde_count = 0; |
| 160 |
#if 0 |
| 161 |
bool have_standard_frame = true; |
| 162 |
#endif |
| 163 |
int res = dwarf_get_fde_list(dbg, |
| 164 |
&cie_data, |
| 165 |
&cie_count, |
| 166 |
&fde_data, |
| 167 |
&fde_count, |
| 168 |
&err); |
| 169 |
if (res == DW_DLV_NO_ENTRY) { |
| 170 |
// No frame data we are dealing with. |
| 171 |
#if 0 |
| 172 |
res = dwarf_get_fde_list_eh(dbg, |
| 173 |
&cie_data, |
| 174 |
&cie_count, |
| 175 |
&fde_data, |
| 176 |
&fde_count, |
| 177 |
&err); |
| 178 |
if (res == DW_DLV_NO_ENTRY) { |
| 179 |
// No frame data. |
| 180 |
return; |
| 181 |
} |
| 182 |
have_standard_frame = false; |
| 183 |
#endif /* 0 */ |
| 184 |
return; |
| 185 |
} |
| 186 |
if (res == DW_DLV_ERROR) { |
| 187 |
cerr << "Error reading frame data " << endl; |
| 188 |
exit(1); |
| 189 |
} |
| 190 |
|
| 191 |
for (Dwarf_Signed i =0; i < cie_count; ++i) { |
| 192 |
Dwarf_Unsigned bytes_in_cie = 0; |
| 193 |
Dwarf_Small version = 0; |
| 194 |
char * augmentation = 0; |
| 195 |
Dwarf_Unsigned code_alignment_factor = 0; |
| 196 |
Dwarf_Signed data_alignment_factor = 0; |
| 197 |
Dwarf_Half return_address_register_rule = 0; |
| 198 |
Dwarf_Small *initial_instructions = 0; |
| 199 |
Dwarf_Unsigned initial_instructions_length = 0; |
| 200 |
Dwarf_Half offset_size = 0; |
| 201 |
res = dwarf_get_cie_info_b(cie_data[i], &bytes_in_cie, |
| 202 |
&version,&augmentation, &code_alignment_factor, |
| 203 |
&data_alignment_factor,&return_address_register_rule, |
| 204 |
&initial_instructions,&initial_instructions_length, |
| 205 |
&offset_size, |
| 206 |
&err); |
| 207 |
if (res != DW_DLV_OK) { |
| 208 |
errprint(err); |
| 209 |
cerr << "Error reading frame data cie index " |
| 210 |
<< i << endl; |
| 211 |
exit(1); |
| 212 |
} |
| 213 |
// Index in cie_data must match index in ciedata_, here |
| 214 |
// correct by construction. |
| 215 |
IRCie cie(bytes_in_cie,version,augmentation, |
| 216 |
code_alignment_factor, |
| 217 |
data_alignment_factor,return_address_register_rule, |
| 218 |
initial_instructions,initial_instructions_length); |
| 219 |
irep.framedata().insert_cie(cie); |
| 220 |
#if 0 |
| 221 |
if (have_standard_frame) { |
| 222 |
irep.framedata().insert_cie(cie); |
| 223 |
} |
| 224 |
else { |
| 225 |
irep.ehframedata().insert_cie(cie); |
| 226 |
} |
| 227 |
#endif |
| 228 |
} |
| 229 |
for (Dwarf_Signed i =0; i < fde_count; ++i) { |
| 230 |
Dwarf_Addr low_pc = 0; |
| 231 |
Dwarf_Unsigned func_length = 0; |
| 232 |
Dwarf_Small *fde_bytes = 0; |
| 233 |
Dwarf_Unsigned fde_byte_length = 0; |
| 234 |
Dwarf_Off cie_offset = 0; |
| 235 |
Dwarf_Signed cie_index = 0; |
| 236 |
Dwarf_Off fde_offset = 0; |
| 237 |
res = dwarf_get_fde_range(fde_data[i], &low_pc, |
| 238 |
&func_length,&fde_bytes, &fde_byte_length, |
| 239 |
&cie_offset,&cie_index, |
| 240 |
&fde_offset, |
| 241 |
&err); |
| 242 |
if (res != DW_DLV_OK) { |
| 243 |
cerr << "Error reading frame data fde index " |
| 244 |
<< i << endl; |
| 245 |
exit(1); |
| 246 |
} |
| 247 |
IRFde fde(low_pc,func_length,fde_bytes,fde_byte_length, |
| 248 |
cie_offset, cie_index,fde_offset); |
| 249 |
Dwarf_Small *instr_in = 0; |
| 250 |
Dwarf_Unsigned instr_len = 0; |
| 251 |
res = dwarf_get_fde_instr_bytes(fde_data[i], |
| 252 |
&instr_in, &instr_len, &err); |
| 253 |
if (res != DW_DLV_OK) { |
| 254 |
cerr << "Error reading frame data fde instructions " |
| 255 |
<< i << endl; |
| 256 |
exit(1); |
| 257 |
} |
| 258 |
fde.get_fde_instrs_into_ir(instr_in,instr_len); |
| 259 |
irep.framedata().insert_fde(fde); |
| 260 |
#if 0 |
| 261 |
if (have_standard_frame) { |
| 262 |
irep.framedata().insert_fde(fde); |
| 263 |
} else { |
| 264 |
irep.ehframedata().insert_fde(fde); |
| 265 |
} |
| 266 |
#endif |
| 267 |
} |
| 268 |
dwarf_dealloc_fde_cie_list(dbg,cie_data,cie_count, |
| 269 |
fde_data,fde_count); |
| 270 |
} |
| 271 |
|
| 272 |
static void |
| 273 |
readCUMacroDataFromBinary(Dwarf_Debug dbg, IRepresentation & irep, |
| 274 |
Dwarf_Unsigned macrodataoffset,IRCUdata &cu) |
| 275 |
{ |
| 276 |
// Arbitrary, but way too high to be real! |
| 277 |
// Probably should be lower. |
| 278 |
Dwarf_Unsigned maxcount = 1000000000; |
| 279 |
Dwarf_Error error; |
| 280 |
Dwarf_Signed mcount=0; |
| 281 |
Dwarf_Macro_Details *md = 0; |
| 282 |
int res = dwarf_get_macro_details(dbg,macrodataoffset, |
| 283 |
maxcount, &mcount, &md, &error); |
| 284 |
if (res == DW_DLV_OK) { |
| 285 |
IRMacro ¯odata = irep.macrodata(); |
| 286 |
vector<IRMacroRecord> mvec = macrodata.getMacroVec(); |
| 287 |
mvec.reserve(mcount); |
| 288 |
Dwarf_Unsigned dieoffset = cu.baseDie().getGlobalOffset(); |
| 289 |
Dwarf_Macro_Details *mdp = md; |
| 290 |
for (int i = 0; i < mcount; ++i, mdp++ ) { |
| 291 |
IRMacroRecord ir(dieoffset,mdp->dmd_offset, |
| 292 |
mdp->dmd_type, mdp->dmd_lineno, |
| 293 |
mdp->dmd_fileindex, mdp->dmd_macro?mdp->dmd_macro:""); |
| 294 |
mvec.push_back(ir); |
| 295 |
} |
| 296 |
} |
| 297 |
if (res == DW_DLV_ERROR) { |
| 298 |
dwarf_dealloc_error(dbg,error); |
| 299 |
error = 0; |
| 300 |
} |
| 301 |
dwarf_dealloc(dbg, md, DW_DLA_STRING); |
| 302 |
} |
| 303 |
|
| 304 |
static void |
| 305 |
get_basic_attr_data_one_attr(Dwarf_Debug dbg, |
| 306 |
Dwarf_Attribute attr,IRCUdata &cudata,IRAttr & irattr) |
| 307 |
{ |
| 308 |
Dwarf_Error error; |
| 309 |
Dwarf_Half attrnum = 0; |
| 310 |
Dwarf_Half finalform = 0; |
| 311 |
Dwarf_Half initialform = 0; |
| 312 |
int res = dwarf_whatattr(attr,&attrnum,&error); |
| 313 |
if (res != DW_DLV_OK) { |
| 314 |
errprint(error); |
| 315 |
cerr << "Unable to get attr number " << endl; |
| 316 |
exit(1); |
| 317 |
} |
| 318 |
res = dwarf_whatform(attr,&finalform,&error); |
| 319 |
if (res != DW_DLV_OK) { |
| 320 |
errprint(error); |
| 321 |
cerr << "Unable to get attr form " << endl; |
| 322 |
exit(1); |
| 323 |
} |
| 324 |
res = dwarf_whatform_direct(attr,&initialform,&error); |
| 325 |
if (res != DW_DLV_OK) { |
| 326 |
errprint(error); |
| 327 |
cerr << "Unable to get attr direct form " << endl; |
| 328 |
exit(1); |
| 329 |
} |
| 330 |
irattr.setBaseData(attrnum,finalform,initialform); |
| 331 |
enum Dwarf_Form_Class cl = dwarf_get_form_class( |
| 332 |
cudata.getVersionStamp(), attrnum, |
| 333 |
cudata.getOffsetSize(), finalform); |
| 334 |
irattr.setFormClass(cl); |
| 335 |
if (cl == DW_FORM_CLASS_UNKNOWN) { |
| 336 |
cerr << "Unable to figure out form class. ver " << |
| 337 |
cudata.getVersionStamp() << |
| 338 |
" attrnum " << attrnum << |
| 339 |
" offsetsize " << cudata.getOffsetSize() << |
| 340 |
" formnum " << finalform << endl; |
| 341 |
return; |
| 342 |
} |
| 343 |
irattr.setFormData(formFactory(dbg,attr,cudata,irattr)); |
| 344 |
} |
| 345 |
static void |
| 346 |
get_basic_die_data(Dwarf_Debug dbg, |
| 347 |
Dwarf_Die indie,IRDie &irdie) |
| 348 |
{ |
| 349 |
Dwarf_Half tagval = 0; |
| 350 |
Dwarf_Error error = 0; |
| 351 |
|
| 352 |
(void)dbg; |
| 353 |
int res = dwarf_tag(indie,&tagval,&error); |
| 354 |
if (res != DW_DLV_OK) { |
| 355 |
errprint(error); |
| 356 |
cerr << "Unable to get die tag "<< endl; |
| 357 |
exit(1); |
| 358 |
} |
| 359 |
Dwarf_Off goff = 0; |
| 360 |
res = dwarf_dieoffset(indie,&goff,&error); |
| 361 |
if (res != DW_DLV_OK) { |
| 362 |
errprint(error); |
| 363 |
cerr << "Unable to get die offset "<< endl; |
| 364 |
exit(1); |
| 365 |
} |
| 366 |
Dwarf_Off localoff = 0; |
| 367 |
res = dwarf_die_CU_offset(indie,&localoff,&error); |
| 368 |
if (res != DW_DLV_OK) { |
| 369 |
errprint(error); |
| 370 |
cerr << "Unable to get cu die offset "<< endl; |
| 371 |
exit(1); |
| 372 |
} |
| 373 |
irdie.setBaseData(tagval,goff,localoff); |
| 374 |
} |
| 375 |
|
| 376 |
static void |
| 377 |
get_attrs_of_die(Dwarf_Die in_die,IRDie &irdie, |
| 378 |
IRCUdata &cudata, |
| 379 |
IRepresentation &irep, |
| 380 |
Dwarf_Debug dbg) |
| 381 |
{ |
| 382 |
Dwarf_Error error = 0; |
| 383 |
Dwarf_Attribute *atlist = 0; |
| 384 |
Dwarf_Signed atcnt = 0; |
| 385 |
std::list<IRAttr> &attrlist = irdie.getAttributes(); |
| 386 |
int res = dwarf_attrlist(in_die, &atlist,&atcnt,&error); |
| 387 |
std::map<unsigned,unsigned> attrmap; |
| 388 |
|
| 389 |
(void)irep; |
| 390 |
if (res == DW_DLV_NO_ENTRY) { |
| 391 |
return; |
| 392 |
} |
| 393 |
if (res == DW_DLV_ERROR) { |
| 394 |
errprint(error); |
| 395 |
cerr << "dwarf_attrlist failed " << endl; |
| 396 |
exit(1); |
| 397 |
} |
| 398 |
for (Dwarf_Signed i = 0; i < atcnt; ++i) { |
| 399 |
Dwarf_Attribute attr = atlist[i]; |
| 400 |
Dwarf_Half attrnum = 0; |
| 401 |
|
| 402 |
res = dwarf_whatattr(attr,&attrnum,&error); |
| 403 |
if (res != DW_DLV_OK) { |
| 404 |
dwarf_dealloc(dbg,atlist, DW_DLA_LIST); |
| 405 |
cout << "ERROR FAIL: unable to get attrnum from attr!" |
| 406 |
<<endl; |
| 407 |
return; |
| 408 |
} |
| 409 |
std::map<unsigned,unsigned>::iterator m = |
| 410 |
attrmap.find(attrnum); |
| 411 |
|
| 412 |
if (m != attrmap.end()) { |
| 413 |
// A duplicate! ignore. Compiler bug |
| 414 |
// in some gcc versions. |
| 415 |
continue; |
| 416 |
} |
| 417 |
attrmap[attrnum] = i; |
| 418 |
// Use an empty attr to get a placeholder on |
| 419 |
// the attr list for this IRDie. |
| 420 |
IRAttr irattr; |
| 421 |
attrlist.push_back(irattr); |
| 422 |
// We want a pointer to the final attr to be |
| 423 |
// recorded for references, not a local temp IRAttr. |
| 424 |
IRAttr & lastirattr = attrlist.back(); |
| 425 |
get_basic_attr_data_one_attr(dbg,attr,cudata,lastirattr); |
| 426 |
} |
| 427 |
dwarf_dealloc(dbg,atlist, DW_DLA_LIST); |
| 428 |
} |
| 429 |
|
| 430 |
// Invariant: IRDie and IRCUdata are in the irep tree, |
| 431 |
// not local record references to local scopes. |
| 432 |
static void |
| 433 |
get_children_of_die(Dwarf_Die in_die,IRDie&irdie, |
| 434 |
IRCUdata &ircudata, |
| 435 |
IRepresentation &irep, |
| 436 |
Dwarf_Debug dbg) |
| 437 |
{ |
| 438 |
Dwarf_Die curchilddie = 0; |
| 439 |
Dwarf_Error error = 0; |
| 440 |
int res = dwarf_child(in_die,&curchilddie,&error); |
| 441 |
if (res == DW_DLV_NO_ENTRY) { |
| 442 |
return; |
| 443 |
} |
| 444 |
if (res == DW_DLV_ERROR) { |
| 445 |
errprint(error); |
| 446 |
cerr << "dwarf_child failed " << endl; |
| 447 |
exit(1); |
| 448 |
} |
| 449 |
//std::list<IRDie> & childlist = irdie.getChildren(); |
| 450 |
for (;;) { |
| 451 |
IRDie child; |
| 452 |
get_basic_die_data(dbg,curchilddie,child); |
| 453 |
irdie.addChild(child); |
| 454 |
IRDie &lastchild = irdie.lastChild(); |
| 455 |
get_attrs_of_die(curchilddie,lastchild,ircudata,irep,dbg); |
| 456 |
|
| 457 |
ircudata.insertLocalDieOffset(lastchild.getCURelativeOffset(), |
| 458 |
&lastchild); |
| 459 |
get_children_of_die(curchilddie,lastchild,ircudata,irep,dbg); |
| 460 |
|
| 461 |
Dwarf_Die tchild = 0; |
| 462 |
res = dwarf_siblingof_b(dbg,curchilddie, |
| 463 |
dwarf_get_die_infotypes_flag(curchilddie), |
| 464 |
&tchild,&error); |
| 465 |
if (res == DW_DLV_NO_ENTRY) { |
| 466 |
break; |
| 467 |
} |
| 468 |
if (res == DW_DLV_ERROR) { |
| 469 |
errprint(error); |
| 470 |
cerr << "dwarf_siblingof_b failed " << endl; |
| 471 |
exit(1); |
| 472 |
} |
| 473 |
dwarf_dealloc(dbg,curchilddie,DW_DLA_DIE); |
| 474 |
curchilddie = tchild; |
| 475 |
} |
| 476 |
dwarf_dealloc(dbg,curchilddie,DW_DLA_DIE); |
| 477 |
} |
| 478 |
|
| 479 |
static void |
| 480 |
get_linedata_of_cu_die(Dwarf_Die in_die,IRDie&irdie, |
| 481 |
IRCUdata &ircudata, |
| 482 |
IRepresentation &irep, |
| 483 |
Dwarf_Debug dbg) |
| 484 |
{ |
| 485 |
Dwarf_Error error = 0; |
| 486 |
char **srcfiles = 0; |
| 487 |
Dwarf_Signed srccount = 0; |
| 488 |
|
| 489 |
(void)irdie; |
| 490 |
(void)irep; |
| 491 |
IRCULineData&culinesdata = ircudata.getCULines(); |
| 492 |
int res = dwarf_srcfiles(in_die,&srcfiles, &srccount,&error); |
| 493 |
if (res == DW_DLV_ERROR) { |
| 494 |
errprint(error); |
| 495 |
cerr << "dwarf_srcfiles failed " << endl; |
| 496 |
exit(1); |
| 497 |
} else if (res == DW_DLV_NO_ENTRY) { |
| 498 |
// No data. |
| 499 |
return; |
| 500 |
} |
| 501 |
|
| 502 |
std::vector<IRCUSrcfile> &srcs = culinesdata.get_cu_srcfiles(); |
| 503 |
for (Dwarf_Signed i = 0; i < srccount; ++i) { |
| 504 |
IRCUSrcfile S(srcfiles[i]); |
| 505 |
srcs.push_back(S); |
| 506 |
/* use srcfiles[i] */ |
| 507 |
dwarf_dealloc(dbg, srcfiles[i], DW_DLA_STRING); |
| 508 |
} |
| 509 |
dwarf_dealloc(dbg, srcfiles, DW_DLA_LIST); |
| 510 |
|
| 511 |
Dwarf_Line * linebuf = 0; |
| 512 |
Dwarf_Signed linecnt = 0; |
| 513 |
Dwarf_Line_Context linecontext= 0; |
| 514 |
Dwarf_Unsigned version = 0; |
| 515 |
Dwarf_Small table_count = 0; |
| 516 |
int res2 = dwarf_srclines_b(in_die,&version, |
| 517 |
&table_count,&linecontext,&error); |
| 518 |
if (res2 == DW_DLV_ERROR) { |
| 519 |
errprint(error); |
| 520 |
cerr << "dwarf_srclines failed " << endl; |
| 521 |
exit(1); |
| 522 |
} else if (res2 == DW_DLV_NO_ENTRY) { |
| 523 |
// No data. |
| 524 |
cerr << "dwarf_srclines failed NO_ENTRY, crazy " |
| 525 |
"since srcfiles worked" << endl; |
| 526 |
exit(1); |
| 527 |
} |
| 528 |
res2= dwarf_srclines_from_linecontext(linecontext, |
| 529 |
&linebuf,&linecnt,&error); |
| 530 |
if (res2 == DW_DLV_ERROR) { |
| 531 |
errprint(error); |
| 532 |
cerr << "dwarf_srclines_from_linecontext failed " << endl; |
| 533 |
exit(1); |
| 534 |
} else if (res2 == DW_DLV_NO_ENTRY) { |
| 535 |
// No data. |
| 536 |
cerr << "dwarf_srclines_from_linecontext failed NO_ENTRY" << |
| 537 |
" , crazy " |
| 538 |
"since srcfiles worked" << endl; |
| 539 |
exit(1); |
| 540 |
} |
| 541 |
|
| 542 |
std::vector<IRCULine> &lines = culinesdata.get_cu_lines(); |
| 543 |
for (Dwarf_Signed j = 0; j < linecnt ; ++j ) { |
| 544 |
Dwarf_Line li = linebuf[j]; |
| 545 |
int lres; |
| 546 |
|
| 547 |
Dwarf_Addr address = 0; |
| 548 |
lres = dwarf_lineaddr(li,&address,&error); |
| 549 |
if (lres != DW_DLV_OK) { |
| 550 |
cerr << "dwarf_lineaddr failed. " << endl; |
| 551 |
exit(1); |
| 552 |
} |
| 553 |
Dwarf_Bool is_addr_set = 0; |
| 554 |
lres = dwarf_line_is_addr_set(li,&is_addr_set,&error); |
| 555 |
if (lres != DW_DLV_OK) { |
| 556 |
cerr << "dwarf_line_is_addr_set failed. " << endl; |
| 557 |
exit(1); |
| 558 |
} |
| 559 |
Dwarf_Unsigned fileno = 0; |
| 560 |
lres = dwarf_line_srcfileno(li,&fileno,&error); |
| 561 |
if (lres != DW_DLV_OK) { |
| 562 |
cerr << "dwarf_srcfileno failed. " << endl; |
| 563 |
exit(1); |
| 564 |
} |
| 565 |
Dwarf_Unsigned lineno = 0; |
| 566 |
lres = dwarf_lineno(li,&lineno,&error); |
| 567 |
if (lres != DW_DLV_OK) { |
| 568 |
cerr << "dwarf_lineno failed. " << endl; |
| 569 |
exit(1); |
| 570 |
} |
| 571 |
Dwarf_Unsigned lineoff = 0; |
| 572 |
lres = dwarf_lineoff_b(li,&lineoff,&error); |
| 573 |
if (lres != DW_DLV_OK) { |
| 574 |
cerr << "dwarf_lineoff failed. " << endl; |
| 575 |
exit(1); |
| 576 |
} |
| 577 |
char *linesrctmp = 0; |
| 578 |
lres = dwarf_linesrc(li,&linesrctmp,&error); |
| 579 |
if (lres != DW_DLV_OK) { |
| 580 |
cerr << "dwarf_linesrc failed. " << endl; |
| 581 |
exit(1); |
| 582 |
} |
| 583 |
// libdwarf is trying to generate a full path, |
| 584 |
// the string here is that generated data, not |
| 585 |
// simply the 'file' path represented by the |
| 586 |
// file number (fileno). |
| 587 |
std::string linesrc(linesrctmp); |
| 588 |
|
| 589 |
Dwarf_Bool is_stmt = 0; |
| 590 |
lres = dwarf_linebeginstatement(li,&is_stmt,&error); |
| 591 |
if (lres != DW_DLV_OK) { |
| 592 |
cerr << "dwarf_linebeginstatement failed. " << endl; |
| 593 |
exit(1); |
| 594 |
} |
| 595 |
|
| 596 |
Dwarf_Bool basic_block = 0; |
| 597 |
lres = dwarf_lineblock(li,&basic_block,&error); |
| 598 |
if (lres != DW_DLV_OK) { |
| 599 |
dwarf_dealloc(dbg,linesrctmp,DW_DLA_STRING); |
| 600 |
cerr << "dwarf_lineblock failed. " << endl; |
| 601 |
exit(1); |
| 602 |
} |
| 603 |
|
| 604 |
Dwarf_Bool end_sequence = 0; |
| 605 |
lres = dwarf_lineendsequence(li,&end_sequence,&error); |
| 606 |
if (lres != DW_DLV_OK) { |
| 607 |
dwarf_dealloc(dbg,linesrctmp,DW_DLA_STRING); |
| 608 |
cerr << "dwarf_lineendsequence failed. " << endl; |
| 609 |
exit(1); |
| 610 |
} |
| 611 |
|
| 612 |
Dwarf_Bool prologue_end = 0; |
| 613 |
Dwarf_Bool epilogue_begin = 0; |
| 614 |
Dwarf_Unsigned isa = 0; |
| 615 |
Dwarf_Unsigned discriminator = 0; |
| 616 |
lres = dwarf_prologue_end_etc(li,&prologue_end, |
| 617 |
&epilogue_begin,&isa,&discriminator,&error); |
| 618 |
if (lres != DW_DLV_OK) { |
| 619 |
cerr << "dwarf_prologue_end_etc failed. " << endl; |
| 620 |
exit(1); |
| 621 |
} |
| 622 |
|
| 623 |
IRCULine L(address, |
| 624 |
is_addr_set, |
| 625 |
fileno, |
| 626 |
lineno, |
| 627 |
lineoff, |
| 628 |
linesrc, |
| 629 |
is_stmt, |
| 630 |
basic_block, |
| 631 |
end_sequence, |
| 632 |
prologue_end,epilogue_begin, |
| 633 |
isa,discriminator); |
| 634 |
lines.push_back(L); |
| 635 |
dwarf_dealloc(dbg,linesrctmp,DW_DLA_STRING); |
| 636 |
} |
| 637 |
dwarf_srclines_dealloc_b(linecontext); |
| 638 |
} |
| 639 |
|
| 640 |
static bool |
| 641 |
getToplevelOffsetAttr(Dwarf_Die cu_die,Dwarf_Half attrnumber, |
| 642 |
Dwarf_Unsigned &offset_out) |
| 643 |
{ |
| 644 |
Dwarf_Error error = 0; |
| 645 |
Dwarf_Off offset = 0; |
| 646 |
Dwarf_Attribute attr = 0; |
| 647 |
int res = dwarf_attr(cu_die,attrnumber,&attr, &error); |
| 648 |
bool foundit = false; |
| 649 |
|
| 650 |
if (res == DW_DLV_OK) { |
| 651 |
res = dwarf_global_formref(attr,&offset,&error); |
| 652 |
if (res == DW_DLV_OK) { |
| 653 |
foundit = true; |
| 654 |
offset_out = offset; |
| 655 |
} |
| 656 |
} |
| 657 |
dwarf_dealloc_attribute(attr); |
| 658 |
return foundit; |
| 659 |
} |
| 660 |
|
| 661 |
// We record the .debug_info info for each CU found |
| 662 |
// To start with we restrict attention to very few DIEs and |
| 663 |
// attributes, but intend to get all eventually. |
| 664 |
static void |
| 665 |
readCUDataFromBinary(Dwarf_Debug dbg, IRepresentation & irep) |
| 666 |
{ |
| 667 |
Dwarf_Error error; |
| 668 |
std::list<IRCUdata> &culist = irep.infodata().getCUData(); |
| 669 |
|
| 670 |
for (;;) { |
| 671 |
Dwarf_Unsigned cu_header_length = 0; |
| 672 |
Dwarf_Half version_stamp = 0; |
| 673 |
Dwarf_Unsigned abbrev_offset = 0; |
| 674 |
Dwarf_Half address_size = 0; |
| 675 |
Dwarf_Half offset_size = 0; |
| 676 |
Dwarf_Half extension_size = 0; |
| 677 |
Dwarf_Sig8 type_signature; |
| 678 |
Dwarf_Unsigned type_offset = 0; |
| 679 |
Dwarf_Unsigned next_cu_header = 0; |
| 680 |
Dwarf_Half header_cu_type= 0; |
| 681 |
Dwarf_Die no_die = 0; |
| 682 |
Dwarf_Die cu_die = 0; |
| 683 |
int res = DW_DLV_ERROR; |
| 684 |
Dwarf_Bool is_info = TRUE; /* This restricts |
| 685 |
dwarfgen a bit. For now. */ |
| 686 |
|
| 687 |
res = dwarf_next_cu_header_d(dbg,is_info,&cu_header_length, |
| 688 |
&version_stamp, &abbrev_offset, &address_size, |
| 689 |
&offset_size, &extension_size, |
| 690 |
&type_signature,&type_offset, |
| 691 |
&next_cu_header, &header_cu_type, &error); |
| 692 |
if (res == DW_DLV_ERROR) { |
| 693 |
errprint(error); |
| 694 |
cerr <<"Error in dwarf_next_cu_header"<< endl; |
| 695 |
exit(1); |
| 696 |
} |
| 697 |
if (res == DW_DLV_NO_ENTRY) { |
| 698 |
/* Done. */ |
| 699 |
return; |
| 700 |
} |
| 701 |
IRCUdata cudata(cu_header_length,version_stamp, |
| 702 |
abbrev_offset,address_size, offset_size, |
| 703 |
extension_size,next_cu_header); |
| 704 |
|
| 705 |
// The CU will have a single sibling (well, it is |
| 706 |
// not exactly a sibling, but close enough), a cu_die. |
| 707 |
res = dwarf_siblingof_b(dbg,no_die, |
| 708 |
is_info,&cu_die,&error); |
| 709 |
if (res == DW_DLV_ERROR) { |
| 710 |
errprint(error); |
| 711 |
cerr <<"Error in dwarf_siblingof_b on CU die "<< endl; |
| 712 |
exit(1); |
| 713 |
} |
| 714 |
if (res == DW_DLV_NO_ENTRY) { |
| 715 |
/* Impossible case. */ |
| 716 |
cerr <<"no Entry! in dwarf_siblingof_b on CU die "<< endl; |
| 717 |
exit(1); |
| 718 |
} |
| 719 |
Dwarf_Off macrooffset = 0; |
| 720 |
bool foundmsect = getToplevelOffsetAttr(cu_die, |
| 721 |
DW_AT_macro_info, |
| 722 |
macrooffset); |
| 723 |
Dwarf_Off linesoffset = 0; |
| 724 |
bool foundlines = getToplevelOffsetAttr(cu_die, |
| 725 |
DW_AT_stmt_list, |
| 726 |
linesoffset); |
| 727 |
Dwarf_Off dieoff = 0; |
| 728 |
res = dwarf_dieoffset(cu_die,&dieoff,&error); |
| 729 |
if (res != DW_DLV_OK) { |
| 730 |
cerr << "Unable to get cu die offset for macro " |
| 731 |
"infomation "<< endl; |
| 732 |
exit(1); |
| 733 |
} |
| 734 |
if (foundmsect) { |
| 735 |
cudata.setMacroData(macrooffset,dieoff); |
| 736 |
} |
| 737 |
if (foundlines) { |
| 738 |
cudata.setLineData(linesoffset,dieoff); |
| 739 |
} |
| 740 |
culist.push_back(cudata); |
| 741 |
IRCUdata & treecu = irep.infodata().lastCU(); |
| 742 |
IRDie &cuirdie = treecu.baseDie(); |
| 743 |
get_basic_die_data(dbg,cu_die,cuirdie); |
| 744 |
treecu.insertLocalDieOffset(cuirdie.getCURelativeOffset(), |
| 745 |
&cuirdie); |
| 746 |
get_attrs_of_die(cu_die,cuirdie,treecu,irep,dbg); |
| 747 |
get_children_of_die(cu_die,cuirdie,treecu,irep,dbg); |
| 748 |
get_linedata_of_cu_die(cu_die,cuirdie,treecu,irep,dbg); |
| 749 |
|
| 750 |
// Now we have all local DIEs in the CU so we |
| 751 |
// can identify all targets of local CLASS_REFERENCE |
| 752 |
// and insert the IRDie * into the IRFormReference |
| 753 |
treecu.updateReferenceAttrDieTargets(); |
| 754 |
|
| 755 |
dwarf_dealloc(dbg,cu_die,DW_DLA_DIE); |
| 756 |
} |
| 757 |
// If we want pointers from child to parent now is the time |
| 758 |
// we can construct them. |
| 759 |
} |
| 760 |
|
| 761 |
// We read thru the CU headers and the CU die to find |
| 762 |
// the macro info for each CU (if any). |
| 763 |
// We record the CU macro info for each CU found (using |
| 764 |
// the value of the DW_AT_macro_info attribute, if any). |
| 765 |
static void |
| 766 |
readMacroDataFromBinary(Dwarf_Debug dbg, IRepresentation & irep) |
| 767 |
{ |
| 768 |
|
| 769 |
list<IRCUdata> &cudata = irep.infodata().getCUData(); |
| 770 |
list<IRCUdata>::iterator it = cudata.begin(); |
| 771 |
int ct = 0; |
| 772 |
for ( ; it != cudata.end(); ++it,++ct) { |
| 773 |
Dwarf_Unsigned macrooffset = 0; |
| 774 |
Dwarf_Unsigned cudieoffset = 0; |
| 775 |
bool foundmsect = it->hasMacroData(¯ooffset,&cudieoffset); |
| 776 |
if (foundmsect) { |
| 777 |
readCUMacroDataFromBinary(dbg, irep, macrooffset,*it); |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
// Offsets refer to .debug_info, nothing else. |
| 783 |
static void |
| 784 |
readGlobals(Dwarf_Debug dbg, IRepresentation & irep) |
| 785 |
{ |
| 786 |
Dwarf_Error error; |
| 787 |
Dwarf_Global *globs = 0; |
| 788 |
Dwarf_Global *types = 0; |
| 789 |
Dwarf_Signed gcnt = 0; |
| 790 |
Dwarf_Signed tcnt = 0; |
| 791 |
int res = 0; |
| 792 |
IRPubsData &pubdata = irep.pubnamedata(); |
| 793 |
|
| 794 |
res = dwarf_globals_by_type(dbg, |
| 795 |
DW_GL_GLOBALS,&globs,&gcnt, &error); |
| 796 |
if (res == DW_DLV_OK) { |
| 797 |
std::list<IRPub> &pubnames = pubdata.getPubnames(); |
| 798 |
for (Dwarf_Signed i = 0; i < gcnt; ++i) { |
| 799 |
Dwarf_Global g = globs[i]; |
| 800 |
char *name = 0; |
| 801 |
// dieoff and cuoff may be in .debug_info |
| 802 |
// or .debug_types |
| 803 |
Dwarf_Off dieoff = 0; |
| 804 |
Dwarf_Off cuoff = 0; |
| 805 |
res = dwarf_global_name_offsets(g,&name, |
| 806 |
&dieoff,&cuoff,&error); |
| 807 |
if ( res == DW_DLV_OK) { |
| 808 |
IRPub p(name,dieoff,cuoff); |
| 809 |
dwarf_dealloc(dbg,name,DW_DLA_STRING); |
| 810 |
pubnames.push_back(p); |
| 811 |
} else if (res == DW_DLV_ERROR) { |
| 812 |
cerr << "dwarf_global_name_offsets failed" << endl; |
| 813 |
dwarf_dealloc_error(dbg,error); |
| 814 |
dwarf_globals_dealloc(dbg, globs, gcnt); |
| 815 |
exit(1); |
| 816 |
} /* Ignoring DW_DLV_NO_ENTRY */ |
| 817 |
} |
| 818 |
dwarf_globals_dealloc(dbg, globs, gcnt); |
| 819 |
globs = 0; |
| 820 |
} else if (res == DW_DLV_ERROR) { |
| 821 |
dwarf_dealloc_error(dbg,error); |
| 822 |
cerr << "dwarf_get_globals failed" << endl; |
| 823 |
exit(1); |
| 824 |
} |
| 825 |
res = dwarf_globals_by_type(dbg,DW_GL_PUBTYPES, |
| 826 |
&types,&tcnt, &error); |
| 827 |
if (res == DW_DLV_OK) { |
| 828 |
std::list<IRPub> &pubtypes = pubdata.getPubtypes(); |
| 829 |
for (Dwarf_Signed i = 0; i < tcnt; ++i) { |
| 830 |
Dwarf_Global g = types[i]; |
| 831 |
char *name = 0; |
| 832 |
// dieoff and cuoff may be in .debug_info |
| 833 |
// or .debug_types |
| 834 |
Dwarf_Off dieoff = 0; |
| 835 |
Dwarf_Off cuoff = 0; |
| 836 |
res = dwarf_global_name_offsets(g,&name, |
| 837 |
&dieoff,&cuoff,&error); |
| 838 |
if ( res == DW_DLV_OK) { |
| 839 |
IRPub p(name,dieoff,cuoff); |
| 840 |
dwarf_dealloc(dbg,name,DW_DLA_STRING); |
| 841 |
pubtypes.push_back(p); |
| 842 |
} else if (res == DW_DLV_ERROR) { |
| 843 |
dwarf_dealloc_error(dbg,error); |
| 844 |
cerr << "dwarf_get_pubtypes failed" << endl; |
| 845 |
exit(1); |
| 846 |
} /* Ignoring DW_DLV_NO_ENTRY */ |
| 847 |
} |
| 848 |
dwarf_globals_dealloc(dbg, types, tcnt); |
| 849 |
} else if (res == DW_DLV_ERROR) { |
| 850 |
dwarf_dealloc_error(dbg,error); |
| 851 |
cerr << "dwarf_get_globals failed" << endl; |
| 852 |
exit(1); |
| 853 |
} |
| 854 |
} |