| 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 |
// irepattrtodbg.cc |
| 38 |
|
| 39 |
#include "config.h" |
| 40 |
|
| 41 |
/* Windows specific header files */ |
| 42 |
#if defined(_WIN32) && defined(HAVE_STDAFX_H) |
| 43 |
#include "stdafx.h" |
| 44 |
#endif /* HAVE_STDAFX_H */ |
| 45 |
#if HAVE_UNISTD_H |
| 46 |
#include <unistd.h> |
| 47 |
#endif |
| 48 |
#include <stdlib.h> /* for exit() */ |
| 49 |
#include <iostream> |
| 50 |
#include <sstream> // For BldName |
| 51 |
#include <iomanip> // iomanp for setw etc |
| 52 |
#include <string> |
| 53 |
#include <list> |
| 54 |
#include <map> |
| 55 |
#include <vector> |
| 56 |
#include <string.h> // For memset etc |
| 57 |
#include "general.h" |
| 58 |
#include "strtabdata.h" |
| 59 |
#include "dwarf.h" |
| 60 |
#include "libdwarf.h" |
| 61 |
#include "libdwarfp.h" |
| 62 |
#include "libdwarf_private.h" |
| 63 |
#include "irepresentation.h" |
| 64 |
#include "ireptodbg.h" |
| 65 |
#include "irepattrtodbg.h" |
| 66 |
|
| 67 |
using std::string; |
| 68 |
using std::cout; |
| 69 |
using std::cerr; |
| 70 |
using std::endl; |
| 71 |
using std::vector; |
| 72 |
using std::list; |
| 73 |
using std::map; |
| 74 |
|
| 75 |
static Dwarf_Error error; |
| 76 |
static unsigned fakeaddrnum; |
| 77 |
|
| 78 |
// We are not going to 'validate' the FORM for this Attribute. |
| 79 |
// or this Die. We just assume that what we are handed is |
| 80 |
// what we are to produce. We do test the attribute |
| 81 |
// at times, partly to ensure we use as many of the dwarf_add_AT* |
| 82 |
// functions as possible. |
| 83 |
|
| 84 |
// Correctness/appropriateness must be evaluated elsewhere. |
| 85 |
|
| 86 |
void |
| 87 |
AddAttrToDie(Dwarf_P_Debug dbg, |
| 88 |
IRepresentation & Irep, |
| 89 |
IRCUdata &cu, |
| 90 |
Dwarf_P_Die outdie, |
| 91 |
IRDie & irdie, |
| 92 |
IRAttr &irattr) |
| 93 |
{ |
| 94 |
int attrnum = irattr.getAttrNum(); |
| 95 |
enum Dwarf_Form_Class formclass = irattr.getFormClass(); |
| 96 |
// IRForm is an abstract base class. |
| 97 |
IRForm *form_a = irattr.getFormData(); |
| 98 |
int res = 0; |
| 99 |
|
| 100 |
(void)irdie; |
| 101 |
switch(formclass) { |
| 102 |
case DW_FORM_CLASS_UNKNOWN: |
| 103 |
cerr << "ERROR AddAttrToDie: Impossible " |
| 104 |
"DW_FORM_CLASS_UNKNOWN, attrnum " |
| 105 |
<<attrnum << endl; |
| 106 |
break; |
| 107 |
case DW_FORM_CLASS_ADDRESS: |
| 108 |
{ |
| 109 |
IRFormAddress *f = dynamic_cast<IRFormAddress *>(form_a); |
| 110 |
if (!f) { |
| 111 |
cerr << "ERROR Impossible DW_FORM_CLASS_ADDRESS " |
| 112 |
"cast fails, attrnum " |
| 113 |
<<attrnum << endl; |
| 114 |
break; |
| 115 |
} |
| 116 |
// FIXME: do better creating a symbol: try to match original |
| 117 |
// or specified input. |
| 118 |
Dwarf_Addr addr = f->getAddress(); |
| 119 |
|
| 120 |
string symname = BldName("addrsym",fakeaddrnum++); |
| 121 |
Dwarf_Addr pcval = addr; |
| 122 |
|
| 123 |
ElfSymbols& es = Irep.getElfSymbols(); |
| 124 |
ElfSymIndex esi = es.addElfSymbol(pcval,symname); |
| 125 |
Dwarf_Unsigned sym_index = esi.getSymIndex(); |
| 126 |
|
| 127 |
// FIXME: we should allow for DW_FORM_indirect here. |
| 128 |
// Relocation later will fix value. |
| 129 |
Dwarf_P_Attribute a = 0; |
| 130 |
|
| 131 |
res = dwarf_add_AT_targ_address_c(dbg, |
| 132 |
outdie,attrnum,0,sym_index,&a,&error); |
| 133 |
if (res != DW_DLV_OK) { |
| 134 |
cerr << "ERROR dwarf_add_AT_targ_address fails, attrnum " |
| 135 |
<<attrnum << endl; |
| 136 |
|
| 137 |
} |
| 138 |
} |
| 139 |
break; |
| 140 |
case DW_FORM_CLASS_BLOCK: |
| 141 |
case DW_FORM_CLASS_EXPRLOC: |
| 142 |
{ |
| 143 |
IRFormBlock *f = dynamic_cast<IRFormBlock *>(form_a); |
| 144 |
if (!f) { |
| 145 |
cerr << "ERROR Impossible DW_FORM_CLASS_BLOCK/EXPRLOC " |
| 146 |
"cast fails, attrnum " |
| 147 |
<<attrnum << endl; |
| 148 |
break; |
| 149 |
} |
| 150 |
|
| 151 |
Dwarf_Unsigned block_len = f->getBlockLen(); |
| 152 |
Dwarf_Small * block_data = f->getBlockBytes(); |
| 153 |
Dwarf_P_Attribute attrb =0; |
| 154 |
|
| 155 |
/* Always generates uleb followed by block, |
| 156 |
sets form based on output dwarf version. */ |
| 157 |
res = dwarf_add_AT_block_a(dbg,outdie,attrnum, |
| 158 |
block_data, |
| 159 |
block_len, |
| 160 |
&attrb, |
| 161 |
&error); |
| 162 |
if (res != DW_DLV_OK) { |
| 163 |
cerr << |
| 164 |
"ERROR dwarf_add_AT_block_a: " |
| 165 |
" fails," |
| 166 |
" attrnum " << attrnum << |
| 167 |
" res "<< res << endl; |
| 168 |
} |
| 169 |
} |
| 170 |
break; |
| 171 |
case DW_FORM_CLASS_CONSTANT: |
| 172 |
{ |
| 173 |
IRFormConstant *f = dynamic_cast<IRFormConstant *>(form_a); |
| 174 |
if (!f) { |
| 175 |
cerr << "ERROR Impossible DW_FORM_CLASS_CONSTANT " |
| 176 |
"cast fails" |
| 177 |
", attrnum " |
| 178 |
<<attrnum << endl; |
| 179 |
break; |
| 180 |
} |
| 181 |
Dwarf_Half formv = f->getFinalForm(); |
| 182 |
// FIXME: Handle form indirect |
| 183 |
IRFormConstant::Signedness sn = f->getSignedness(); |
| 184 |
Dwarf_P_Attribute a = 0; |
| 185 |
|
| 186 |
if (formv == DW_FORM_data16) { |
| 187 |
Dwarf_Form_Data16 val = f->getData16Val(); |
| 188 |
|
| 189 |
res = dwarf_add_AT_data16(outdie, |
| 190 |
attrnum,&val,&a,&error); |
| 191 |
if (res != DW_DLV_OK) { |
| 192 |
cerr << |
| 193 |
"ERROR AddAttrToDie: " |
| 194 |
"dwarf_add_AT_ data16 class constant fails," |
| 195 |
" attrnum " << attrnum << |
| 196 |
" res "<<res << endl; |
| 197 |
} |
| 198 |
break; |
| 199 |
} else if (formv == DW_FORM_implicit_const) { |
| 200 |
Dwarf_Signed sval = f->getSignedVal(); |
| 201 |
|
| 202 |
res = dwarf_add_AT_implicit_const(outdie,attrnum, |
| 203 |
sval,&a,&error); |
| 204 |
if (res != DW_DLV_OK) { |
| 205 |
cerr << |
| 206 |
"ERROR AddAttrToDie: " |
| 207 |
"dwarf_add_AT_implicit_const fails," |
| 208 |
" attrnum " << attrnum << |
| 209 |
" res "<<res << endl; |
| 210 |
} |
| 211 |
break; |
| 212 |
} |
| 213 |
if (sn == IRFormConstant::SIGNED) { |
| 214 |
Dwarf_Signed sval = f->getSignedVal(); |
| 215 |
if (formv == DW_FORM_sdata) { |
| 216 |
res = dwarf_add_AT_any_value_sleb_a( |
| 217 |
outdie,attrnum, |
| 218 |
sval,&a,&error); |
| 219 |
} else { |
| 220 |
// ERROR how can we know |
| 221 |
// a non-sdata const is signed? |
| 222 |
res = dwarf_add_AT_signed_const_a(dbg, |
| 223 |
outdie,attrnum, |
| 224 |
sval,&a,&error); |
| 225 |
} |
| 226 |
} else { |
| 227 |
Dwarf_Unsigned uval_i = f->getUnsignedVal(); |
| 228 |
if (formv == DW_FORM_udata) { |
| 229 |
res = dwarf_add_AT_any_value_uleb_a( |
| 230 |
outdie,attrnum, |
| 231 |
uval_i,&a,&error); |
| 232 |
} else { |
| 233 |
res = dwarf_add_AT_unsigned_const_a(dbg, |
| 234 |
outdie,attrnum, |
| 235 |
uval_i,&a,&error); |
| 236 |
} |
| 237 |
} |
| 238 |
if (res != DW_DLV_OK) { |
| 239 |
cerr << "ERROR dwarf_add_AT_ class constant fails," << |
| 240 |
dwarf_errmsg(error) << |
| 241 |
" attrnum " << attrnum << |
| 242 |
" Continuing" << endl; |
| 243 |
} |
| 244 |
} |
| 245 |
break; |
| 246 |
case DW_FORM_CLASS_FLAG: |
| 247 |
{ |
| 248 |
IRFormFlag *f = dynamic_cast<IRFormFlag *>(form_a); |
| 249 |
if (!f) { |
| 250 |
cerr << "ERROR Impossible DW_FORM_CLASS_FLAG cast fails" |
| 251 |
", attrnum " |
| 252 |
<<attrnum << endl; |
| 253 |
break; |
| 254 |
} |
| 255 |
// FIXME: handle indirect form (libdwarf needs feature). |
| 256 |
// FIXME: handle implicit flag (libdwarf needs feature). |
| 257 |
// FIXME: rel type ok? |
| 258 |
Dwarf_P_Attribute a = 0; |
| 259 |
res = dwarf_add_AT_flag_a(dbg,outdie,attrnum, |
| 260 |
f->getFlagVal(),&a,&error); |
| 261 |
if (res != DW_DLV_OK) { |
| 262 |
cerr << "ERROR dwarf_add_AT_flag fails, attrnum " |
| 263 |
<<attrnum << endl; |
| 264 |
} |
| 265 |
} |
| 266 |
break; |
| 267 |
|
| 268 |
case DW_FORM_CLASS_LINEPTR: |
| 269 |
{ |
| 270 |
// The DW_AT_stmt_list attribute is generated |
| 271 |
// as a side effect of dwarf_transform_to_disk_form |
| 272 |
// if producer line-info-creating functions were called. |
| 273 |
// So we ignore this attribute here, it is |
| 274 |
// automatic. |
| 275 |
} |
| 276 |
break; |
| 277 |
case DW_FORM_CLASS_ADDRPTR: |
| 278 |
case DW_FORM_CLASS_LOCLIST: |
| 279 |
case DW_FORM_CLASS_LOCLISTPTR: |
| 280 |
case DW_FORM_CLASS_LOCLISTSPTR: |
| 281 |
{ |
| 282 |
//FIXME. Needs support in dwarf producer(libdwarf) |
| 283 |
} |
| 284 |
break; |
| 285 |
case DW_FORM_CLASS_MACPTR: |
| 286 |
{ |
| 287 |
// The DW_AT_macro_info attribute is generated |
| 288 |
// as a side effect of dwarf_transform_to_disk_form |
| 289 |
// if producer macro-creating functions were called. |
| 290 |
// So we ignore this attribute here, it is |
| 291 |
// automatic. |
| 292 |
} |
| 293 |
break; |
| 294 |
case DW_FORM_CLASS_STROFFSETSPTR: |
| 295 |
case DW_FORM_CLASS_RNGLISTSPTR: |
| 296 |
case DW_FORM_CLASS_MACROPTR: |
| 297 |
case DW_FORM_CLASS_RANGELISTPTR: |
| 298 |
{ |
| 299 |
//FIXME. Needs support in dwarf producer(libdwarf) |
| 300 |
} |
| 301 |
break; |
| 302 |
case DW_FORM_CLASS_REFERENCE: |
| 303 |
{ |
| 304 |
// Can be a local CU reference to a DIE, or a |
| 305 |
// global DIE reference or a |
| 306 |
// sig8 reference. |
| 307 |
//FIXME |
| 308 |
IRFormReference *r = dynamic_cast<IRFormReference *>(form_a); |
| 309 |
if (!r) { |
| 310 |
cerr << "ERROR Impossible DW_FORM_CLASS_REFERENCE " |
| 311 |
"cast fails, attrnum " |
| 312 |
<<attrnum << endl; |
| 313 |
break; |
| 314 |
} |
| 315 |
|
| 316 |
IRFormReference::RefType reftype = r->getReferenceType(); |
| 317 |
switch (reftype) { |
| 318 |
case IRFormReference::RT_NONE: |
| 319 |
cerr << "ERROR CLASS REFERENCE unknown reftype " |
| 320 |
<<attrnum << endl; |
| 321 |
break; |
| 322 |
case IRFormReference::RT_GLOBAL: |
| 323 |
// FIXME. Not handled. |
| 324 |
break; |
| 325 |
case IRFormReference::RT_CUREL: |
| 326 |
{ |
| 327 |
IRDie *targetofref = r->getTargetInDie(); |
| 328 |
Dwarf_P_Die targetoutdie = 0; |
| 329 |
if (targetofref) { |
| 330 |
targetoutdie = targetofref->getGeneratedDie(); |
| 331 |
} |
| 332 |
if (!targetoutdie) { |
| 333 |
if (!targetofref) { |
| 334 |
cerr << "ERROR CLASS REFERENCE targetdie " |
| 335 |
"of reference unknown" |
| 336 |
<<attrnum << endl; |
| 337 |
break; |
| 338 |
} |
| 339 |
// We must add the attribute when we have the |
| 340 |
// target Dwarf_P_Die, which should get set shortly. |
| 341 |
// And do the dwarf_add_AT_reference_c() then. |
| 342 |
// Before transform_to_disk_form. |
| 343 |
// NULL targetoutdie allowed here. |
| 344 |
// Arranging DIE order so there were no forward-refs |
| 345 |
// could be difficult. |
| 346 |
// Another option would be two-pass: first create |
| 347 |
// all the DIEs then all the attributes for each. |
| 348 |
Dwarf_P_Attribute a = 0; |
| 349 |
|
| 350 |
res = dwarf_add_AT_reference_c(dbg,outdie,attrnum, |
| 351 |
/*targetoutdie */NULL,&a,&error); |
| 352 |
if (res != DW_DLV_OK) { |
| 353 |
cerr << "ERROR dwarf_add_AT_reference fails, " |
| 354 |
"attrnum with not yet known targetoutdie " |
| 355 |
<< IToHex(attrnum) << |
| 356 |
" " << dwarf_errmsg(error) << |
| 357 |
endl; |
| 358 |
} else { |
| 359 |
ClassReferenceFixupData x(dbg,attrnum, |
| 360 |
outdie,targetofref); |
| 361 |
cu.insertClassReferenceFixupData(x); |
| 362 |
} |
| 363 |
break; |
| 364 |
} |
| 365 |
Dwarf_P_Attribute a = 0; |
| 366 |
|
| 367 |
res = dwarf_add_AT_reference_c(dbg,outdie,attrnum, |
| 368 |
targetoutdie,&a,&error); |
| 369 |
if (res != DW_DLV_OK) { |
| 370 |
cerr << "ERROR dwarf_add_AT_reference fails, " |
| 371 |
"attrnum with known targetoutdie " << |
| 372 |
IToHex(attrnum) << |
| 373 |
" " << dwarf_errmsg(error) << |
| 374 |
endl; |
| 375 |
} |
| 376 |
} |
| 377 |
break; |
| 378 |
case IRFormReference::RT_SIG: |
| 379 |
{ |
| 380 |
Dwarf_P_Attribute a = 0; |
| 381 |
res = dwarf_add_AT_with_ref_sig8_a(outdie,attrnum, |
| 382 |
r->getSignature(),&a,&error); |
| 383 |
if ( res != DW_DLV_OK) { |
| 384 |
cerr << "ERROR dwarf_add_AT_ref_sig8 fails, attrnum " |
| 385 |
<< IToHex(attrnum) << endl; |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
break; |
| 391 |
case DW_FORM_CLASS_STRING: |
| 392 |
{ |
| 393 |
IRFormString *f = dynamic_cast<IRFormString *>(form_a); |
| 394 |
if (!f) { |
| 395 |
cerr << "ERROR Impossible DW_FORM_CLASS_STRING " |
| 396 |
"cast fails, attrnum " |
| 397 |
<<attrnum << endl; |
| 398 |
break; |
| 399 |
} |
| 400 |
Dwarf_P_Attribute a = 0; |
| 401 |
// We know libdwarf does not change the string. |
| 402 |
// Historical mistake |
| 403 |
// not making it a const char * argument. |
| 404 |
// Ugly cast. |
| 405 |
// FIXME: handle indirect form (libdwarf needs feature). |
| 406 |
// FIXME: rel type ok? |
| 407 |
char *mystr = const_cast<char *>(f->getString().c_str()); |
| 408 |
|
| 409 |
switch(attrnum) { |
| 410 |
case DW_AT_name: |
| 411 |
res = dwarf_add_AT_name_a(outdie,mystr,&a,&error); |
| 412 |
break; |
| 413 |
case DW_AT_producer: |
| 414 |
res = dwarf_add_AT_producer_a(outdie,mystr,&a,&error); |
| 415 |
break; |
| 416 |
case DW_AT_comp_dir: |
| 417 |
res = dwarf_add_AT_comp_dir_a(outdie,mystr,&a,&error); |
| 418 |
break; |
| 419 |
default: |
| 420 |
res = dwarf_add_AT_string_a(dbg,outdie,attrnum,mystr, |
| 421 |
&a,&error); |
| 422 |
break; |
| 423 |
} |
| 424 |
if (res != DW_DLV_OK) { |
| 425 |
cerr << "ERROR dwarf_add_AT_string fails, attrnum " |
| 426 |
<<attrnum << endl; |
| 427 |
} |
| 428 |
} |
| 429 |
break; |
| 430 |
case DW_FORM_CLASS_FRAMEPTR: // SGI/MIPS/IRIX only. |
| 431 |
{ |
| 432 |
//FIXME |
| 433 |
} |
| 434 |
break; |
| 435 |
default: |
| 436 |
cerr << "ERROR Impossible DW_FORM_CLASS "<< |
| 437 |
static_cast<int>(formclass) |
| 438 |
<<attrnum << endl; |
| 439 |
//FIXME |
| 440 |
} |
| 441 |
return; |
| 442 |
} |
| 443 |
|
| 444 |
void |
| 445 |
IRCUdata::updateClassReferenceTargets() |
| 446 |
{ |
| 447 |
for (std::list<ClassReferenceFixupData>::iterator it = |
| 448 |
classReferenceFixupList_.begin(); |
| 449 |
it != classReferenceFixupList_.end(); |
| 450 |
++it) { |
| 451 |
IRDie* d = it->target_; |
| 452 |
Dwarf_P_Die sourcedie = it->sourcedie_; |
| 453 |
Dwarf_P_Die targetdie = d->getGeneratedDie(); |
| 454 |
Dwarf_Error lerror = 0; |
| 455 |
|
| 456 |
int res = dwarf_fixup_AT_reference_die(it->dbg_, |
| 457 |
it->attrnum_,sourcedie,targetdie,&lerror); |
| 458 |
if (res != DW_DLV_OK) { |
| 459 |
cerr << "Improper dwarf_fixup_AT_reference_die call" |
| 460 |
<< endl; |
| 461 |
} |
| 462 |
} |
| 463 |
} |