| 1 |
/* |
| 2 |
Copyright (C) 2000-2006 Silicon Graphics, Inc. All Rights Reserved. |
| 3 |
Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved. |
| 4 |
Portions Copyright 2009-2018 SN Systems Ltd. All rights reserved. |
| 5 |
Portions Copyright 2007-2021 David Anderson. All rights reserved. |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or |
| 8 |
modify it under the terms of version 2 of the GNU General |
| 9 |
Public License as published by the Free Software Foundation. |
| 10 |
|
| 11 |
This program is distributed in the hope that it would be |
| 12 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 13 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
PURPOSE. |
| 15 |
|
| 16 |
Further, this software is distributed without any warranty |
| 17 |
that it is free of the rightful claim of any third person |
| 18 |
regarding infringement or the like. Any license provided |
| 19 |
herein, whether implied or otherwise, applies only to this |
| 20 |
software file. Patent licenses, if any, provided herein |
| 21 |
do not apply to combinations of this program with other |
| 22 |
software, or any other product whatsoever. |
| 23 |
|
| 24 |
You should have received a copy of the GNU General Public |
| 25 |
License along with this program; if not, write the Free |
| 26 |
Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
| 27 |
Boston MA 02110-1301, USA. |
| 28 |
*/ |
| 29 |
|
| 30 |
/* The address of the Free Software Foundation is |
| 31 |
Free Software Foundation, Inc., 51 Franklin St, Fifth |
| 32 |
Floor, Boston, MA 02110-1301, USA. SGI has moved from |
| 33 |
the Crittenden Lane address. */ |
| 34 |
|
| 35 |
/* |
| 36 |
Handles the following from print_die.c print_attribute() |
| 37 |
case DW_AT_specification: |
| 38 |
case DW_AT_abstract_origin: |
| 39 |
case DW_AT_type: |
| 40 |
*/ |
| 41 |
#include <config.h> |
| 42 |
|
| 43 |
#include <stdlib.h> /* calloc() free() */ |
| 44 |
#include <string.h> /* memcmp() memset() strchr() strcmp() |
| 45 |
strlen() strncmp() */ |
| 46 |
#include <stdio.h> /* FILE decl for dd_esb.h, printf etc */ |
| 47 |
|
| 48 |
#ifdef HAVE_STDINT_H |
| 49 |
#include <stdint.h> /* uintptr_t */ |
| 50 |
#endif /* HAVE_STDINT_H */ |
| 51 |
|
| 52 |
#include "dwarf.h" |
| 53 |
#include "libdwarf.h" |
| 54 |
#include "libdwarf_private.h" |
| 55 |
#include "dd_defined_types.h" |
| 56 |
#include "dd_checkutil.h" |
| 57 |
#include "dd_glflags.h" |
| 58 |
#include "dd_globals.h" |
| 59 |
#include "dd_naming.h" |
| 60 |
#include "dd_esb.h" /* For flexible string buffer. */ |
| 61 |
#include "dd_esb_using_functions.h" |
| 62 |
#include "dd_sanitized.h" |
| 63 |
#include "print_frames.h" /* for print_expression_operations() . */ |
| 64 |
#include "dd_macrocheck.h" |
| 65 |
#include "dd_helpertree.h" |
| 66 |
#include "dd_opscounttab.h" |
| 67 |
#include "dd_tag_common.h" |
| 68 |
#include "dd_attr_form.h" |
| 69 |
#include "dd_regex.h" |
| 70 |
#include "dd_safe_strcpy.h" |
| 71 |
|
| 72 |
int |
| 73 |
dd_trace_abstract_origin_etc( |
| 74 |
Dwarf_Debug dbg, |
| 75 |
Dwarf_Half tag, |
| 76 |
Dwarf_Die die, |
| 77 |
Dwarf_Off dieprint_cu_goffset, |
| 78 |
Dwarf_Half theform, |
| 79 |
Dwarf_Half attrnum, |
| 80 |
Dwarf_Attribute attrib, |
| 81 |
char **srcfiles, |
| 82 |
Dwarf_Signed srcfiles_count, |
| 83 |
struct esb_s *valname, |
| 84 |
struct esb_s *esb_extra, |
| 85 |
int die_indent_level, |
| 86 |
int pd_dwarf_names_print_on_error, |
| 87 |
Dwarf_Error *err) |
| 88 |
{ |
| 89 |
char typebuf[ESB_FIXED_ALLOC_SIZE]; |
| 90 |
struct esb_s lesb; |
| 91 |
|
| 92 |
/* A local flag to make it easy to tell |
| 93 |
if we should append a target die name */ |
| 94 |
Dwarf_Bool standard_messages = TRUE; |
| 95 |
|
| 96 |
/* To support finding target DIE, use these helper locals. */ |
| 97 |
Dwarf_Unsigned target_goff = 0; |
| 98 |
#if 0 |
| 99 |
Dwarf_Bool target_goff_known = FALSE; |
| 100 |
Dwarf_Unsigned target_gofferror = 0; |
| 101 |
Dwarf_Bool target_refsig8 = FALSE; |
| 102 |
#endif |
| 103 |
Dwarf_Bool target_is_info = FALSE; |
| 104 |
Dwarf_Bool is_info = dwarf_get_die_infotypes_flag(die); |
| 105 |
int tres = 0; |
| 106 |
|
| 107 |
esb_constructor_fixed(&lesb,typebuf, |
| 108 |
sizeof(typebuf)); |
| 109 |
tres = get_attr_value(dbg, tag, die, |
| 110 |
dieprint_cu_goffset,attrib, srcfiles, |
| 111 |
srcfiles_count, &lesb, |
| 112 |
glflags.show_form_used,glflags.verbose,err); |
| 113 |
if (tres == DW_DLV_ERROR) { |
| 114 |
struct esb_s m; |
| 115 |
const char *n = |
| 116 |
get_AT_name(attrnum,pd_dwarf_names_print_on_error); |
| 117 |
esb_constructor(&m); |
| 118 |
esb_append(&m, |
| 119 |
"Cannot get get value for a "); |
| 120 |
esb_append(&m,n); |
| 121 |
print_error_and_continue( |
| 122 |
esb_get_string(&m), |
| 123 |
tres,*err); |
| 124 |
esb_destructor(&m); |
| 125 |
esb_destructor(valname); |
| 126 |
esb_destructor(esb_extra); |
| 127 |
return tres; |
| 128 |
} |
| 129 |
if (theform == DW_FORM_ref_sig8) { |
| 130 |
/* SIG8 CHECK */ |
| 131 |
int res = 0; |
| 132 |
|
| 133 |
#if 0 |
| 134 |
target_refsig8 = TRUE; |
| 135 |
#endif |
| 136 |
standard_messages = FALSE; |
| 137 |
res = dd_print_sig8_target(dbg,attrib, |
| 138 |
die_indent_level, |
| 139 |
srcfiles, srcfiles_count, &lesb,err); |
| 140 |
if (res == DW_DLV_ERROR) { |
| 141 |
esb_destructor(&lesb); |
| 142 |
return res; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
esb_empty_string(valname); |
| 147 |
esb_append(valname, esb_get_string(&lesb)); |
| 148 |
esb_destructor(&lesb); |
| 149 |
|
| 150 |
if (glflags.gf_check_forward_decl || |
| 151 |
glflags.gf_check_self_references || |
| 152 |
glflags.gf_search_is_on) { |
| 153 |
Dwarf_Off die_goff = 0; |
| 154 |
Dwarf_Off ref_goff = 0; |
| 155 |
int frres = 0; |
| 156 |
int suppress_check = 0; |
| 157 |
Dwarf_Bool is_info2 = TRUE; |
| 158 |
|
| 159 |
/* SPECIFIC CHECKS A */ |
| 160 |
standard_messages = FALSE; |
| 161 |
frres = dwarf_global_formref_b(attrib, &ref_goff, |
| 162 |
&is_info2,err); |
| 163 |
if (frres == DW_DLV_ERROR) { |
| 164 |
/* myerr will be way less than 1000 */ |
| 165 |
int myerr = (int)dwarf_errno(*err); |
| 166 |
if (myerr == DW_DLE_REF_SIG8_NOT_HANDLED) { |
| 167 |
/* DW_DLE_REF_SIG8_NOT_HANDLED */ |
| 168 |
/* No offset available, it makes |
| 169 |
little sense |
| 170 |
to delve into this sort of reference |
| 171 |
unless |
| 172 |
we think a graph of self-refs *across* |
| 173 |
type-units is possible. Hmm. FIXME? */ |
| 174 |
suppress_check = 1 ; |
| 175 |
DWARF_CHECK_COUNT(self_references_result,1); |
| 176 |
DWARF_CHECK_ERROR(self_references_result, |
| 177 |
"DW_AT_ref_sig8 not handled so " |
| 178 |
"self references not fully checked"); |
| 179 |
DROP_ERROR_INSTANCE(dbg,frres,*err); |
| 180 |
} else { |
| 181 |
const char *n = |
| 182 |
get_AT_name(attrnum, |
| 183 |
pd_dwarf_names_print_on_error); |
| 184 |
struct esb_s m; |
| 185 |
esb_constructor(&m); |
| 186 |
esb_append(&m, |
| 187 |
"Cannot get formref global offset " |
| 188 |
"for a "); |
| 189 |
esb_append(&m,n); |
| 190 |
print_error_and_continue( |
| 191 |
esb_get_string(&m), |
| 192 |
frres,*err); |
| 193 |
esb_destructor(&m); |
| 194 |
esb_destructor(valname); |
| 195 |
esb_destructor(esb_extra); |
| 196 |
return frres; |
| 197 |
} |
| 198 |
} else if (frres == DW_DLV_NO_ENTRY) { |
| 199 |
const char *n = |
| 200 |
get_AT_name(attrnum, |
| 201 |
pd_dwarf_names_print_on_error); |
| 202 |
struct esb_s m; |
| 203 |
|
| 204 |
esb_constructor(&m); |
| 205 |
esb_append(&m, |
| 206 |
"Cannot get formref global offset for a "); |
| 207 |
esb_append(&m,n); |
| 208 |
print_error_and_continue( |
| 209 |
esb_get_string(&m), |
| 210 |
frres,*err); |
| 211 |
esb_destructor(&m); |
| 212 |
esb_destructor(valname); |
| 213 |
esb_destructor(esb_extra); |
| 214 |
return frres; |
| 215 |
} |
| 216 |
frres = dwarf_dieoffset(die, &die_goff, err); |
| 217 |
if (frres != DW_DLV_OK) { |
| 218 |
const char *n = get_AT_name(attrnum, |
| 219 |
pd_dwarf_names_print_on_error); |
| 220 |
struct esb_s m; |
| 221 |
esb_constructor(&m); |
| 222 |
esb_append(&m, |
| 223 |
"Cannot get formref dieoffset offset for a "); |
| 224 |
esb_append(&m,n); |
| 225 |
print_error_and_continue( |
| 226 |
esb_get_string(&m), |
| 227 |
frres,*err); |
| 228 |
esb_destructor(&m); |
| 229 |
esb_destructor(valname); |
| 230 |
esb_destructor(esb_extra); |
| 231 |
return frres; |
| 232 |
} |
| 233 |
|
| 234 |
if (!suppress_check && |
| 235 |
glflags.gf_check_self_references && |
| 236 |
dd_form_refers_local_info(theform) ) { |
| 237 |
Dwarf_Die ref_die = 0; |
| 238 |
int ifres = 0; |
| 239 |
|
| 240 |
/* SPECIFIC CHECKS B */ |
| 241 |
standard_messages = FALSE; |
| 242 |
ResetBucketGroup(glflags.pVisitedInfo); |
| 243 |
AddEntryIntoBucketGroup(glflags.pVisitedInfo, |
| 244 |
die_goff,0,0,0, |
| 245 |
NULL,FALSE); |
| 246 |
|
| 247 |
/* Follow reference chain, looking for |
| 248 |
self references */ |
| 249 |
frres = dwarf_offdie_b(dbg,ref_goff,is_info2, |
| 250 |
&ref_die,err); |
| 251 |
if (frres == DW_DLV_OK) { |
| 252 |
Dwarf_Off ref_die_cu_goff = 0; |
| 253 |
Dwarf_Off die_loff = 0; /* CU-relative. */ |
| 254 |
int fresb = 0; |
| 255 |
|
| 256 |
if (dump_visited_info) { |
| 257 |
const char *atname = get_AT_name(attrnum, |
| 258 |
pd_dwarf_names_print_on_error); |
| 259 |
fresb = dwarf_die_CU_offset(die, |
| 260 |
&die_loff, err); |
| 261 |
if (fresb == DW_DLV_OK) { |
| 262 |
dd_do_dump_visited_info(die_indent_level, |
| 263 |
die_loff,die_goff, |
| 264 |
dieprint_cu_goffset, |
| 265 |
atname,esb_get_string(valname)); |
| 266 |
} else { |
| 267 |
esb_destructor(valname); |
| 268 |
esb_destructor(esb_extra); |
| 269 |
dwarf_dealloc_die(ref_die); |
| 270 |
return fresb; |
| 271 |
} |
| 272 |
} |
| 273 |
++die_indent_level; |
| 274 |
fresb = dwarf_CU_dieoffset_given_die(ref_die, |
| 275 |
&ref_die_cu_goff, err); |
| 276 |
/* Check above call return |
| 277 |
status? FIXME */ |
| 278 |
if (fresb != DW_DLV_OK) { |
| 279 |
const char *n = |
| 280 |
get_AT_name(attrnum, |
| 281 |
pd_dwarf_names_print_on_error); |
| 282 |
struct esb_s m; |
| 283 |
esb_constructor(&m); |
| 284 |
esb_append(&m, |
| 285 |
"Cannot get CU dieoffset " |
| 286 |
"given die for a "); |
| 287 |
esb_append(&m,n); |
| 288 |
print_error_and_continue( |
| 289 |
esb_get_string(&m), |
| 290 |
frres,*err); |
| 291 |
dwarf_dealloc_die(ref_die); |
| 292 |
esb_destructor(&m); |
| 293 |
esb_destructor(valname); |
| 294 |
esb_destructor(esb_extra); |
| 295 |
return frres; |
| 296 |
} |
| 297 |
|
| 298 |
ifres = dd_traverse_one_die(dbg,attrib,ref_die, |
| 299 |
ref_die_cu_goff, |
| 300 |
is_info,srcfiles,srcfiles_count, |
| 301 |
die_indent_level, err); |
| 302 |
dwarf_dealloc_die(ref_die); |
| 303 |
ref_die = 0; |
| 304 |
--die_indent_level; |
| 305 |
if (ifres != DW_DLV_OK) { |
| 306 |
esb_destructor(valname); |
| 307 |
esb_destructor(esb_extra); |
| 308 |
return ifres; |
| 309 |
} |
| 310 |
} |
| 311 |
DeleteKeyInBucketGroup(glflags.pVisitedInfo, |
| 312 |
die_goff); |
| 313 |
if (frres == DW_DLV_ERROR) { |
| 314 |
esb_destructor(valname); |
| 315 |
esb_destructor(esb_extra); |
| 316 |
return frres; |
| 317 |
} |
| 318 |
} |
| 319 |
if (!suppress_check && |
| 320 |
glflags.gf_check_forward_decl) { |
| 321 |
/* Check the DW_AT_specification |
| 322 |
forward references to DIEs. |
| 323 |
DWARF4 specifications, section 2.13.2. |
| 324 |
They are legal, this just reports how many |
| 325 |
are forward decls as 'error' in |
| 326 |
the final checks output. */ |
| 327 |
/* SPECIFIC CHECKS C */ |
| 328 |
standard_messages = FALSE; |
| 329 |
if (attrnum == DW_AT_specification) { |
| 330 |
/* Counting DW_AT_specification */ |
| 331 |
DWARF_CHECK_COUNT(forward_decl_result,1); |
| 332 |
if (ref_goff > die_goff) { |
| 333 |
/* Not an error. Just counting |
| 334 |
the number that are forward |
| 335 |
as if it might be a problem. */ |
| 336 |
DWARF_ERROR_COUNT(forward_decl_result,1); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
/* When doing search, if the attribute is |
| 341 |
DW_AT_specification or |
| 342 |
DW_AT_abstract_origin, get any name |
| 343 |
associated with the DIE |
| 344 |
referenced in the offset. |
| 345 |
The 2 more typical cases are: |
| 346 |
Member functions, where 2 DIES are generated: |
| 347 |
DIE for the declaration and DIE for |
| 348 |
the definition |
| 349 |
and connected via the DW_AT_specification. |
| 350 |
Inlined functions, where 2 DIES are generated: |
| 351 |
DIE for the concrete instance and |
| 352 |
DIE for the abstract |
| 353 |
instance and connected via the |
| 354 |
DW_AT_abstract_origin. |
| 355 |
*/ |
| 356 |
if ( glflags.gf_search_is_on && |
| 357 |
(attrnum == DW_AT_specification || |
| 358 |
attrnum == DW_AT_abstract_origin)) { |
| 359 |
Dwarf_Die ref_die = 0; |
| 360 |
int srcres = 0; |
| 361 |
|
| 362 |
/* SPECIFIC CHECKS D */ |
| 363 |
standard_messages = FALSE; |
| 364 |
/* Follow reference chain, looking for |
| 365 |
the DIE name */ |
| 366 |
srcres = dwarf_offdie_b(dbg,ref_goff,is_info2, |
| 367 |
&ref_die,err); |
| 368 |
if (srcres == DW_DLV_OK) { |
| 369 |
/* Get the DIE name */ |
| 370 |
char *name = 0; |
| 371 |
srcres = dwarf_diename(ref_die,&name,err); |
| 372 |
if (srcres == DW_DLV_OK) { |
| 373 |
esb_empty_string(valname); |
| 374 |
esb_append(valname,name); |
| 375 |
} |
| 376 |
if (srcres == DW_DLV_ERROR) { |
| 377 |
glflags.gf_count_major_errors++; |
| 378 |
esb_empty_string(valname); |
| 379 |
esb_append(valname, |
| 380 |
"<ERROR: no name for reference"); |
| 381 |
DROP_ERROR_INSTANCE(dbg,srcres,*err); |
| 382 |
} |
| 383 |
/* Release the allocated DIE */ |
| 384 |
dwarf_dealloc_die(ref_die); |
| 385 |
} else if (srcres == DW_DLV_ERROR) { |
| 386 |
glflags.gf_count_major_errors++; |
| 387 |
esb_empty_string(valname); |
| 388 |
esb_append(valname, |
| 389 |
"<ERROR: no referred-to die found "); |
| 390 |
DROP_ERROR_INSTANCE(dbg,srcres,*err); |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
/* If we are in checking mode and we do not |
| 395 |
have a PU name */ |
| 396 |
if (( glflags.gf_check_locations || |
| 397 |
glflags.gf_check_ranges) && |
| 398 |
glflags.seen_PU && !glflags.PU_name[0]) { |
| 399 |
/* SPECIFIC CHECKS E */ |
| 400 |
standard_messages = FALSE; |
| 401 |
if (tag == DW_TAG_subprogram) { |
| 402 |
/* This gets the DW_AT_name if this |
| 403 |
DIE has one. */ |
| 404 |
Dwarf_Addr low_pc = 0; |
| 405 |
struct esb_s pn; |
| 406 |
int found = 0; |
| 407 |
/* The cu_die_for_print_frames will not |
| 408 |
be changed |
| 409 |
by get_proc_name_by_die(). |
| 410 |
Used when printing frames */ |
| 411 |
Dwarf_Die cu_die_for_print_frames = 0; |
| 412 |
|
| 413 |
esb_constructor(&pn); |
| 414 |
/* Only looks in this one DIE's attributes */ |
| 415 |
found = get_proc_name_by_die(dbg,die, |
| 416 |
low_pc,&pn, |
| 417 |
&cu_die_for_print_frames, |
| 418 |
/*pcMap=*/0, |
| 419 |
err); |
| 420 |
if (found == DW_DLV_ERROR) { |
| 421 |
struct esb_s m; |
| 422 |
const char *n = |
| 423 |
get_AT_name(attrnum, |
| 424 |
pd_dwarf_names_print_on_error); |
| 425 |
esb_constructor(&m); |
| 426 |
esb_append(&m, |
| 427 |
"Cannot get get value for a "); |
| 428 |
esb_append(&m,n); |
| 429 |
print_error_and_continue( |
| 430 |
esb_get_string(&m), |
| 431 |
found,*err); |
| 432 |
esb_destructor(&m); |
| 433 |
return found; |
| 434 |
} |
| 435 |
if (found == DW_DLV_OK) { |
| 436 |
dd_safe_strcpy(glflags.PU_name, |
| 437 |
sizeof(glflags.PU_name), |
| 438 |
esb_get_string(&pn), |
| 439 |
esb_string_len(&pn)); |
| 440 |
} |
| 441 |
esb_destructor(&pn); |
| 442 |
} |
| 443 |
} |
| 444 |
if (standard_messages) { |
| 445 |
int smres = 0; |
| 446 |
Dwarf_Die target_die = 0; |
| 447 |
|
| 448 |
smres = dwarf_global_formref_b(attrib, &target_goff, |
| 449 |
&target_is_info,err); |
| 450 |
if (smres != DW_DLV_OK) { |
| 451 |
if (smres == DW_DLV_ERROR) { |
| 452 |
esb_append(valname, |
| 453 |
" ERROR reading target offset: "); |
| 454 |
dwarf_dealloc_error(dbg,*err); |
| 455 |
*err = 0; |
| 456 |
} |
| 457 |
} else { |
| 458 |
int tares = 0; |
| 459 |
tares = dwarf_offdie_b(dbg,target_goff,target_is_info, |
| 460 |
&target_die,err); |
| 461 |
if (tares == DW_DLV_OK) { |
| 462 |
/* Get the DIE name */ |
| 463 |
char *name = 0; |
| 464 |
tares = dwarf_diename(target_die,&name,err); |
| 465 |
if (tares == DW_DLV_OK) { |
| 466 |
esb_append(valname," Refers to: "); |
| 467 |
esb_append(valname,name); |
| 468 |
} else if (tares == DW_DLV_ERROR) { |
| 469 |
dwarf_dealloc_error(dbg,*err); |
| 470 |
*err = 0; |
| 471 |
} else { |
| 472 |
#if 0 |
| 473 |
Be quiet here. Useless message, one thinks. |
| 474 |
esb_append(valname," No target name available. "); |
| 475 |
#endif |
| 476 |
} |
| 477 |
} else { |
| 478 |
if (tares == DW_DLV_ERROR) { |
| 479 |
dwarf_dealloc_error(dbg,*err); |
| 480 |
*err = 0; |
| 481 |
if (attrnum != DW_AT_GNU_locviews) { |
| 482 |
/* Not yet sure I understand the value |
| 483 |
yet (of this attribute) . */ |
| 484 |
esb_append(valname," Reference fails"); |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
if (target_die) { |
| 489 |
dwarf_dealloc_die(target_die); |
| 490 |
target_die = 0; |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
return DW_DLV_OK; |
| 495 |
} |