| 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-2011 SN Systems Ltd. All rights reserved. |
| 5 |
Portions Copyright 2008-2020 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 Floor, |
| 32 |
Boston, MA 02110-1301, USA. |
| 33 |
SGI has moved from the Crittenden Lane address. |
| 34 |
*/ |
| 35 |
|
| 36 |
#include <config.h> |
| 37 |
|
| 38 |
#include <stdlib.h> /* calloc() free() realloc() */ |
| 39 |
#include <string.h> /* memset() */ |
| 40 |
#include <stdio.h> /* FILE decl for dd_esb.h */ |
| 41 |
|
| 42 |
#include "dwarf.h" |
| 43 |
#include "libdwarf.h" |
| 44 |
#include "libdwarf_private.h" |
| 45 |
#include "dd_defined_types.h" |
| 46 |
#include "dd_checkutil.h" |
| 47 |
#include "dd_glflags.h" |
| 48 |
#include "dd_globals.h" |
| 49 |
#include "dd_naming.h" |
| 50 |
#include "dd_sanitized.h" |
| 51 |
#include "dd_esb.h" |
| 52 |
#include "dd_esb_using_functions.h" |
| 53 |
|
| 54 |
#include "print_sections.h" |
| 55 |
|
| 56 |
/* The following relevent for one specific Linker. */ |
| 57 |
#define SNLINKER_MAX_ATTRIB_COUNT 16 |
| 58 |
/* a warning limit which is arbitrary but |
| 59 |
leaves a bit more flexibility. */ |
| 60 |
#define GENERAL_MAX_ATTRIB_COUNT 32 |
| 61 |
|
| 62 |
/* Print data in .debug_abbrev |
| 63 |
This is inherently unsafe as it assumes there |
| 64 |
are no byte sequences in .debug_abbrev other than |
| 65 |
legal abbrev sequences. But the Dwarf spec |
| 66 |
does not promise that. The spec only promises |
| 67 |
that any bytes at an offset referred to from |
| 68 |
.debug_info are legal sequences. |
| 69 |
*/ |
| 70 |
|
| 71 |
struct abbrev_entry_s { |
| 72 |
Dwarf_Unsigned ae_number; |
| 73 |
Dwarf_Unsigned ae_offset; |
| 74 |
Dwarf_Unsigned ae_attr; |
| 75 |
Dwarf_Unsigned ae_form; |
| 76 |
Dwarf_Unsigned ae_impl_const; |
| 77 |
unsigned ae_dupcount; |
| 78 |
}; |
| 79 |
|
| 80 |
static int |
| 81 |
ab_compare(const void *lin, const void *rin) |
| 82 |
{ |
| 83 |
const struct abbrev_entry_s *l = |
| 84 |
(const struct abbrev_entry_s *)lin; |
| 85 |
const struct abbrev_entry_s *r = |
| 86 |
(const struct abbrev_entry_s *)rin; |
| 87 |
if (l->ae_attr < r->ae_attr) { |
| 88 |
return -1; |
| 89 |
} |
| 90 |
if (l->ae_attr > r->ae_attr) { |
| 91 |
return 1; |
| 92 |
} |
| 93 |
if (l->ae_form < r->ae_form) { |
| 94 |
return -1; |
| 95 |
} |
| 96 |
if (l->ae_form > r->ae_form) { |
| 97 |
return 1; |
| 98 |
} |
| 99 |
if (l->ae_number < r->ae_number) { |
| 100 |
return -1; |
| 101 |
} |
| 102 |
if (l->ae_number > r->ae_number) { |
| 103 |
return 1; |
| 104 |
} |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
static int |
| 109 |
attr_unknown(Dwarf_Unsigned attr) |
| 110 |
{ |
| 111 |
const char *n = 0; |
| 112 |
int res = 0; |
| 113 |
|
| 114 |
if (!attr) { |
| 115 |
return TRUE; |
| 116 |
} |
| 117 |
if (attr <= DW_AT_loclists_base) { |
| 118 |
return FALSE; |
| 119 |
} |
| 120 |
if (attr > DW_AT_hi_user) { |
| 121 |
return TRUE; |
| 122 |
} |
| 123 |
res = dwarf_get_AT_name((unsigned int)attr,&n); |
| 124 |
if (res == DW_DLV_NO_ENTRY) { |
| 125 |
return TRUE; |
| 126 |
} |
| 127 |
return FALSE; |
| 128 |
} |
| 129 |
static int |
| 130 |
is_valid_form_we_know(Dwarf_Unsigned form) |
| 131 |
{ |
| 132 |
int res = 0; |
| 133 |
const char *n = 0; |
| 134 |
|
| 135 |
res = dwarf_get_FORM_name((unsigned int)form,&n); |
| 136 |
if (res == DW_DLV_NO_ENTRY) { |
| 137 |
return FALSE; |
| 138 |
} |
| 139 |
return TRUE; |
| 140 |
} |
| 141 |
|
| 142 |
static void |
| 143 |
printdupab(struct abbrev_entry_s * lastaep) |
| 144 |
{ |
| 145 |
struct esb_s msg; |
| 146 |
|
| 147 |
esb_constructor(&msg); |
| 148 |
esb_append_printf_u(&msg, |
| 149 |
"Attribute " |
| 150 |
"0x%" DW_PR_XZEROS DW_PR_DUx , |
| 151 |
lastaep->ae_attr); |
| 152 |
esb_append_printf_s(&msg, |
| 153 |
" (%s)", get_AT_name((unsigned int)lastaep->ae_attr, |
| 154 |
dwarf_names_print_on_error)); |
| 155 |
esb_append_printf_u(&msg, |
| 156 |
" %u times", lastaep->ae_dupcount); |
| 157 |
esb_append_printf_u(&msg, |
| 158 |
" near offset " |
| 159 |
"0x%" DW_PR_XZEROS DW_PR_DUx ".", |
| 160 |
lastaep->ae_offset); |
| 161 |
|
| 162 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 163 |
esb_get_string(&msg), |
| 164 |
"Duplicated attribute in abbrevs "); |
| 165 |
esb_destructor(&msg); |
| 166 |
} |
| 167 |
static int |
| 168 |
print_one_abbrev_for_cu(Dwarf_Debug dbg, |
| 169 |
Dwarf_Unsigned offset, |
| 170 |
Dwarf_Unsigned abbrev_num_in, |
| 171 |
Dwarf_Unsigned *length_out, |
| 172 |
Dwarf_Unsigned *abbrev_num_out, |
| 173 |
Dwarf_Error * error) |
| 174 |
{ |
| 175 |
const char *tagname = ""; |
| 176 |
struct abbrev_entry_s *entryarray =0; |
| 177 |
Dwarf_Unsigned entryarray_size = 0; |
| 178 |
Dwarf_Unsigned abbrev_entry_count = 0; |
| 179 |
Dwarf_Unsigned abbrev_code = 0; |
| 180 |
Dwarf_Half tag = 0; |
| 181 |
Dwarf_Unsigned length = 0; |
| 182 |
int acres = 0; |
| 183 |
Dwarf_Abbrev ab = 0; |
| 184 |
int tres = 0; |
| 185 |
Dwarf_Unsigned abbrev_num = abbrev_num_in; |
| 186 |
Dwarf_Signed child_flag = 0; |
| 187 |
int abres = 0; |
| 188 |
Dwarf_Unsigned i = 0; |
| 189 |
|
| 190 |
abres = dwarf_get_abbrev(dbg, offset, &ab, |
| 191 |
&length, &abbrev_entry_count, error); |
| 192 |
if (abres == DW_DLV_ERROR) { |
| 193 |
return abres; |
| 194 |
} |
| 195 |
if (abres == DW_DLV_NO_ENTRY) { |
| 196 |
return abres; |
| 197 |
} |
| 198 |
/* Here offset is the global offset in .debug_abbrev. |
| 199 |
The abbrev_num is a relatively worthless counter |
| 200 |
of all abbreviations. */ |
| 201 |
tres = dwarf_get_abbrev_tag(ab, &tag, error); |
| 202 |
if (tres == DW_DLV_ERROR) { |
| 203 |
dwarf_dealloc(dbg, ab, DW_DLA_ABBREV); |
| 204 |
print_error_and_continue( |
| 205 |
"Error reading abbreviation Tag", tres, *error); |
| 206 |
return tres; |
| 207 |
} |
| 208 |
tres = dwarf_get_abbrev_code(ab, &abbrev_code, error); |
| 209 |
if (tres != DW_DLV_OK) { |
| 210 |
dwarf_dealloc(dbg, ab, DW_DLA_ABBREV); |
| 211 |
print_error_and_continue( |
| 212 |
"Error reading abbreviation code", |
| 213 |
tres, *error); |
| 214 |
return tres; |
| 215 |
} |
| 216 |
if (!tag) { |
| 217 |
/* This means we are done with this abbrev set, |
| 218 |
abbrev for entire CU. */ |
| 219 |
tagname = "Abbrev 0: null abbrev entry"; |
| 220 |
} else { |
| 221 |
tagname = get_TAG_name((unsigned)tag, |
| 222 |
dwarf_names_print_on_error); |
| 223 |
} |
| 224 |
if ( glflags.gf_do_print_dwarf) { |
| 225 |
if (glflags.dense) { |
| 226 |
printf("<%" DW_PR_DUu "><0x%" DW_PR_XZEROS DW_PR_DUx |
| 227 |
"><code: %" DW_PR_DUu ">", |
| 228 |
abbrev_num, offset,abbrev_code); |
| 229 |
if (glflags.verbose) { |
| 230 |
printf("<length: 0x%" DW_PR_XZEROS DW_PR_DUx ">", |
| 231 |
length); |
| 232 |
} |
| 233 |
printf(" %s", tagname); |
| 234 |
} |
| 235 |
else { |
| 236 |
printf("<%5" DW_PR_DUu "><0x%" DW_PR_XZEROS DW_PR_DUx |
| 237 |
"><code: %3" DW_PR_DUu ">", |
| 238 |
abbrev_num, offset, abbrev_code); |
| 239 |
if (glflags.verbose) { |
| 240 |
printf("<length: 0x%" DW_PR_XZEROS DW_PR_DUx ">", |
| 241 |
length); |
| 242 |
} |
| 243 |
printf(" %-27s", tagname); |
| 244 |
} |
| 245 |
} |
| 246 |
/* Process specific TAGs specially. */ |
| 247 |
tag_specific_globals_setup(dbg,tag,0); |
| 248 |
++abbrev_num; |
| 249 |
acres = dwarf_get_abbrev_children_flag(ab, &child_flag, |
| 250 |
error); |
| 251 |
if (acres == DW_DLV_ERROR) { |
| 252 |
dwarf_dealloc(dbg, ab, DW_DLA_ABBREV); |
| 253 |
print_error_and_continue( |
| 254 |
"Error reading abbreviation children flag", |
| 255 |
acres, *error); |
| 256 |
return acres; |
| 257 |
} |
| 258 |
if (acres == DW_DLV_NO_ENTRY) { |
| 259 |
child_flag = 0; |
| 260 |
} |
| 261 |
/* If tag is zero, it is a null byte, not a real abbreviation, |
| 262 |
so there is no 'children' flag to print. */ |
| 263 |
if (tag && glflags.gf_do_print_dwarf) { |
| 264 |
const char * child_name = 0; |
| 265 |
|
| 266 |
child_name = get_children_name((int)(unsigned int)child_flag, |
| 267 |
dwarf_names_print_on_error); |
| 268 |
printf(" %s", child_name); |
| 269 |
} |
| 270 |
if (!glflags.dense) { |
| 271 |
if ( glflags.gf_do_print_dwarf) { |
| 272 |
printf("\n"); |
| 273 |
} |
| 274 |
} |
| 275 |
if (abbrev_entry_count < 1) { |
| 276 |
if (tag && glflags.gf_do_print_dwarf) { |
| 277 |
printf(" This abbreviation code has no entries\n"); |
| 278 |
} |
| 279 |
if (length == 0 || length == 1 ) { |
| 280 |
if ( glflags.gf_do_print_dwarf && glflags.dense ) { |
| 281 |
printf("\n"); |
| 282 |
} |
| 283 |
*length_out = length; |
| 284 |
*abbrev_num_out = abbrev_num; |
| 285 |
/* printed null abrev name above */ |
| 286 |
dwarf_dealloc(dbg, ab, DW_DLA_ABBREV); |
| 287 |
return DW_DLV_OK; |
| 288 |
} |
| 289 |
} |
| 290 |
/* Abbrev contains the format of a die, |
| 291 |
which debug_info then points to with the |
| 292 |
real data. So here we just print the |
| 293 |
given format. */ |
| 294 |
entryarray_size = abbrev_entry_count; |
| 295 |
entryarray = calloc(entryarray_size, |
| 296 |
sizeof(struct abbrev_entry_s)); |
| 297 |
if (!entryarray) { |
| 298 |
printf( "\n%s ERROR: Malloc of %" DW_PR_DUu " abbrev_entry_s" |
| 299 |
" structs failed. Near section global offset 0x%" |
| 300 |
DW_PR_DUx " Trying to continue. .\n", |
| 301 |
glflags.program_name,entryarray_size,offset); |
| 302 |
glflags.gf_count_major_errors++; |
| 303 |
dwarf_dealloc(dbg, ab, DW_DLA_ABBREV); |
| 304 |
entryarray_size = 0; |
| 305 |
return DW_DLV_OK; |
| 306 |
} |
| 307 |
for (i = 0; i < abbrev_entry_count ; i++) { |
| 308 |
int aeres = 0; |
| 309 |
Dwarf_Bool dofilter = FALSE; |
| 310 |
Dwarf_Unsigned form = 0; |
| 311 |
struct abbrev_entry_s *aep = entryarray+i; |
| 312 |
Dwarf_Unsigned attr = 0; |
| 313 |
Dwarf_Signed impl_const = 0; |
| 314 |
Dwarf_Off off = 0; |
| 315 |
|
| 316 |
aeres = dwarf_get_abbrev_entry_b(ab, i, |
| 317 |
dofilter,&attr, &form,&impl_const, &off, |
| 318 |
error); |
| 319 |
if (aeres == DW_DLV_ERROR) { |
| 320 |
dwarf_dealloc(dbg, ab, DW_DLA_ABBREV); |
| 321 |
free(entryarray); |
| 322 |
print_error_and_continue( |
| 323 |
"Error reading abbreviation entry", |
| 324 |
aeres, *error); |
| 325 |
return aeres; |
| 326 |
} |
| 327 |
aep->ae_number = i; |
| 328 |
aep->ae_attr = attr; |
| 329 |
aep->ae_form = form; |
| 330 |
aep->ae_offset = off; |
| 331 |
aep->ae_impl_const = impl_const; |
| 332 |
if (glflags.gf_do_print_dwarf) { |
| 333 |
char buf [80]; |
| 334 |
struct esb_s m; |
| 335 |
|
| 336 |
buf[0] = 0; |
| 337 |
esb_constructor_fixed(&m,buf,sizeof(buf)); |
| 338 |
if (form == DW_FORM_implicit_const) { |
| 339 |
esb_append_printf_i(&m, |
| 340 |
" <%d",impl_const); |
| 341 |
esb_append_printf_u(&m, |
| 342 |
" (0x%x)>", |
| 343 |
impl_const); |
| 344 |
} |
| 345 |
if (glflags.dense) { |
| 346 |
printf(" <%ld>%s<%s>%s", (unsigned long) off, |
| 347 |
get_AT_name((unsigned int)attr, |
| 348 |
dwarf_names_print_on_error), |
| 349 |
get_FORM_name((Dwarf_Half) form, |
| 350 |
dwarf_names_print_on_error), |
| 351 |
esb_get_string(&m)); |
| 352 |
} else if (!esb_string_len(&m)) { |
| 353 |
printf(" <0x%08lx> %-28s%s\n", |
| 354 |
(unsigned long) off, |
| 355 |
get_AT_name((unsigned int)attr, |
| 356 |
dwarf_names_print_on_error), |
| 357 |
get_FORM_name((Dwarf_Half) form, |
| 358 |
dwarf_names_print_on_error)); |
| 359 |
} else { |
| 360 |
printf(" <0x%08lx>" |
| 361 |
" %-28s%-20s%s\n", |
| 362 |
(unsigned long) off, |
| 363 |
get_AT_name((unsigned int)attr, |
| 364 |
dwarf_names_print_on_error), |
| 365 |
get_FORM_name((Dwarf_Half) form, |
| 366 |
dwarf_names_print_on_error), |
| 367 |
esb_get_string(&m)); |
| 368 |
} |
| 369 |
esb_destructor(&m); |
| 370 |
} |
| 371 |
} |
| 372 |
if (glflags.gf_check_abbreviations && |
| 373 |
entryarray_size > 0) { |
| 374 |
unsigned l = 0; |
| 375 |
struct abbrev_entry_s *lastaep = 0; |
| 376 |
|
| 377 |
DWARF_CHECK_COUNT(abbreviations_result,1); |
| 378 |
qsort((void *)entryarray,entryarray_size, |
| 379 |
sizeof(struct abbrev_entry_s),ab_compare); |
| 380 |
|
| 381 |
for (l = 0; l < entryarray_size ; ++l) { |
| 382 |
struct abbrev_entry_s *aep = entryarray+l; |
| 383 |
|
| 384 |
if (attr_unknown(aep->ae_attr) ) { |
| 385 |
struct esb_s msg; |
| 386 |
|
| 387 |
esb_constructor(&msg); |
| 388 |
esb_append_printf_u(&msg, |
| 389 |
"Attribute " |
| 390 |
"0x%" DW_PR_XZEROS DW_PR_DUx , |
| 391 |
aep->ae_attr); |
| 392 |
esb_append_printf_u(&msg, |
| 393 |
" near offset " |
| 394 |
"0x%" DW_PR_XZEROS DW_PR_DUx "." , |
| 395 |
aep->ae_offset); |
| 396 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 397 |
"Attr number unknown", |
| 398 |
esb_get_string(&msg)); |
| 399 |
esb_destructor(&msg); |
| 400 |
} |
| 401 |
if (!is_valid_form_we_know(aep->ae_form)){ |
| 402 |
struct esb_s msg; |
| 403 |
|
| 404 |
esb_constructor(&msg); |
| 405 |
esb_append_printf_u(&msg, |
| 406 |
"Form " |
| 407 |
"0x%" DW_PR_XZEROS DW_PR_DUx, |
| 408 |
aep->ae_form); |
| 409 |
esb_append_printf_u(&msg, |
| 410 |
" near offset " |
| 411 |
"0x%" DW_PR_XZEROS DW_PR_DUx ".", |
| 412 |
aep->ae_offset); |
| 413 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 414 |
"Form number unknown", |
| 415 |
esb_get_string(&msg)); |
| 416 |
esb_destructor(&msg); |
| 417 |
} |
| 418 |
if (l == 0) { |
| 419 |
lastaep = aep; |
| 420 |
} else if (lastaep->ae_attr == aep->ae_attr) { |
| 421 |
lastaep->ae_dupcount++; |
| 422 |
} else { |
| 423 |
if (lastaep->ae_dupcount) { |
| 424 |
printdupab(lastaep); |
| 425 |
} |
| 426 |
lastaep = aep; |
| 427 |
} |
| 428 |
} |
| 429 |
if (lastaep->ae_dupcount) { |
| 430 |
printdupab(lastaep); |
| 431 |
} |
| 432 |
} |
| 433 |
dwarf_dealloc(dbg, ab, DW_DLA_ABBREV); |
| 434 |
free(entryarray); |
| 435 |
entryarray = 0; |
| 436 |
entryarray_size = 0; |
| 437 |
*length_out = length; |
| 438 |
*abbrev_num_out = abbrev_num; |
| 439 |
if (glflags.gf_do_print_dwarf && glflags.dense) { |
| 440 |
printf("\n"); |
| 441 |
} |
| 442 |
return DW_DLV_OK; |
| 443 |
} |
| 444 |
|
| 445 |
int |
| 446 |
print_all_abbrevs_for_cu(Dwarf_Debug dbg, |
| 447 |
Dwarf_Unsigned offset, |
| 448 |
Dwarf_Unsigned abbrev_num_in, |
| 449 |
Dwarf_Unsigned *length_out, |
| 450 |
Dwarf_Unsigned *abbrev_num_out, |
| 451 |
Dwarf_Error *error) |
| 452 |
{ |
| 453 |
Dwarf_Unsigned total_len = 0; |
| 454 |
int pres = 0; |
| 455 |
Dwarf_Unsigned loopcount = 0; |
| 456 |
/* We have always printed the abbrev_num starting with 1. |
| 457 |
Unclear why. */ |
| 458 |
Dwarf_Unsigned abbrev_num = abbrev_num_in; |
| 459 |
Dwarf_Unsigned abbrev_num_ret = abbrev_num_in; |
| 460 |
|
| 461 |
for ( ; ;++loopcount ) { |
| 462 |
Dwarf_Unsigned local_len = 0; |
| 463 |
|
| 464 |
abbrev_num = abbrev_num_ret; |
| 465 |
pres = print_one_abbrev_for_cu(dbg,offset, |
| 466 |
abbrev_num, |
| 467 |
&local_len,&abbrev_num_ret,error); |
| 468 |
if (pres == DW_DLV_ERROR) { |
| 469 |
return pres; |
| 470 |
} |
| 471 |
if (pres == DW_DLV_NO_ENTRY) { |
| 472 |
if (loopcount) { |
| 473 |
/* This could be an incomplete final entry, |
| 474 |
the trailing NUL byte for a CU |
| 475 |
abbrev set is missing |
| 476 |
as of end of section. */ |
| 477 |
printf("ERROR: The final .debug_abbrev " |
| 478 |
"abbreviation ends without its required " |
| 479 |
"final NUL byte. A harmless error at " |
| 480 |
"section offset 0x%" |
| 481 |
DW_PR_XZEROS DW_PR_DUx |
| 482 |
".",offset); |
| 483 |
glflags.gf_count_major_errors++; |
| 484 |
return pres; |
| 485 |
} |
| 486 |
return pres; |
| 487 |
} |
| 488 |
total_len += local_len; |
| 489 |
if (local_len == 1) { |
| 490 |
/* Last of a CU data printed, ending loop */ |
| 491 |
break; |
| 492 |
} |
| 493 |
offset += local_len; |
| 494 |
} |
| 495 |
*abbrev_num_out = abbrev_num_ret; |
| 496 |
*length_out = total_len; |
| 497 |
return DW_DLV_OK; |
| 498 |
} |
| 499 |
|
| 500 |
int |
| 501 |
print_abbrevs(Dwarf_Debug dbg,Dwarf_Error* paerr) |
| 502 |
{ |
| 503 |
Dwarf_Abbrev ab = 0; |
| 504 |
Dwarf_Unsigned offset = 0; |
| 505 |
int abres = 0; |
| 506 |
int tres = 0; |
| 507 |
unsigned loopct = 0; |
| 508 |
Dwarf_Unsigned length = 0; |
| 509 |
Dwarf_Unsigned unused_entry_count = 0; |
| 510 |
Dwarf_Unsigned abbrev_num = 1; |
| 511 |
Dwarf_Unsigned abbrev_num_ret = 1; |
| 512 |
|
| 513 |
glflags.current_section_id = DEBUG_ABBREV; |
| 514 |
/* Doing this just to print the section name */ |
| 515 |
abres = dwarf_get_abbrev(dbg, offset, &ab, |
| 516 |
&length, &unused_entry_count, paerr); |
| 517 |
{ |
| 518 |
/* Do this after a dwarf_get_abbrev() |
| 519 |
so the section is loaded and uncompressed |
| 520 |
if necessary. We get information printed |
| 521 |
about the compression (if any) this way. */ |
| 522 |
print_secname(dbg,".debug_abbrev"); |
| 523 |
} |
| 524 |
if (abres == DW_DLV_OK) { |
| 525 |
/* discard what we got. */ |
| 526 |
dwarf_dealloc(dbg,ab, DW_DLA_ABBREV); |
| 527 |
ab = 0; |
| 528 |
} else if (abres == DW_DLV_ERROR) { |
| 529 |
dwarf_dealloc_error(dbg,*paerr); |
| 530 |
*paerr = 0; |
| 531 |
} else { |
| 532 |
/* DW_DLV_NO_ENTRY, fall through, nothing to discard */ |
| 533 |
} |
| 534 |
|
| 535 |
for (loopct = 0; ; ++loopct) { |
| 536 |
tres = print_all_abbrevs_for_cu(dbg,offset, |
| 537 |
abbrev_num,&length,&abbrev_num_ret,paerr); |
| 538 |
if (tres == DW_DLV_NO_ENTRY) { |
| 539 |
if (loopct > 0) { |
| 540 |
return DW_DLV_OK; |
| 541 |
} |
| 542 |
return DW_DLV_NO_ENTRY; |
| 543 |
} |
| 544 |
if (tres == DW_DLV_ERROR) { |
| 545 |
return tres; |
| 546 |
} |
| 547 |
offset = offset+length; |
| 548 |
abbrev_num = abbrev_num_ret; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
/* Abbreviations array info for checking abbrev tags. |
| 553 |
The [zero] entry is not used. |
| 554 |
We never shrink the array, but it never grows beyond |
| 555 |
the largest abbreviation count of all the CUs. |
| 556 |
It is set up when we start a new CU and |
| 557 |
used to validate abbreviations on each DIE in the CU. |
| 558 |
See print_die.c |
| 559 |
*/ |
| 560 |
|
| 561 |
static Dwarf_Unsigned *abbrev_array = NULL; |
| 562 |
/* Size of the array, the same as the abbrev tag |
| 563 |
count of the CU with the most of them. |
| 564 |
Be careful as abbrev_array[abbrev_array_size] |
| 565 |
is outside the high bound. */ |
| 566 |
static Dwarf_Unsigned abbrev_array_size = 0; |
| 567 |
|
| 568 |
#define ABBREV_ARRAY_INITIAL_SIZE 64 |
| 569 |
|
| 570 |
void |
| 571 |
destruct_abbrev_array(void) |
| 572 |
{ |
| 573 |
free(abbrev_array); |
| 574 |
abbrev_array = 0; |
| 575 |
abbrev_array_size = 0; |
| 576 |
} |
| 577 |
|
| 578 |
/* Normally abbreviation numbers are allocated in sequence from 1 |
| 579 |
and increase by 1 |
| 580 |
but in case of a compiler bug or a damaged object file one can |
| 581 |
see strange things. This looks for surprises and reports them. |
| 582 |
Returns the abbrev_code unless the value looks very wrong, |
| 583 |
and then it returns zero as we do not want a gigantic |
| 584 |
abbrev code to cause trouble. |
| 585 |
*/ |
| 586 |
static Dwarf_Unsigned |
| 587 |
check_abbrev_num_sequence(Dwarf_Unsigned abbrev_code, |
| 588 |
Dwarf_Unsigned last_abbrev_code) |
| 589 |
{ |
| 590 |
char buf[128]; |
| 591 |
|
| 592 |
DWARF_CHECK_COUNT(abbreviations_result,1); |
| 593 |
if (abbrev_code > last_abbrev_code) { |
| 594 |
if ((abbrev_code-last_abbrev_code) > 100 ) { |
| 595 |
struct esb_s ar; |
| 596 |
esb_constructor_fixed(&ar,buf,sizeof(buf)); |
| 597 |
esb_append_printf_u(&ar, |
| 598 |
"Abbrev code %" DW_PR_DUu,abbrev_code); |
| 599 |
esb_append_printf_u(&ar, |
| 600 |
" skips up by %" DW_PR_DUu, |
| 601 |
(abbrev_code-last_abbrev_code)); |
| 602 |
esb_append_printf_u(&ar, |
| 603 |
" from last abbrev code of %" DW_PR_DUu , |
| 604 |
last_abbrev_code); |
| 605 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 606 |
esb_get_string(&ar), |
| 607 |
"Questionable abbreviation code! " |
| 608 |
"Not checking reuse."); |
| 609 |
esb_destructor(&ar); |
| 610 |
return 0; |
| 611 |
} else if ((abbrev_code-last_abbrev_code) > 1 ) { |
| 612 |
struct esb_s ar; |
| 613 |
esb_constructor_fixed(&ar,buf,sizeof(buf)); |
| 614 |
esb_append_printf_u(&ar, |
| 615 |
"Abbrev code %" DW_PR_DUu, |
| 616 |
abbrev_code); |
| 617 |
esb_append_printf_u(&ar, |
| 618 |
" skips up by %" DW_PR_DUu, |
| 619 |
(abbrev_code-last_abbrev_code)); |
| 620 |
esb_append_printf_u(&ar, |
| 621 |
" from last abbrev code of %" DW_PR_DUu , |
| 622 |
last_abbrev_code); |
| 623 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 624 |
esb_get_string(&ar), |
| 625 |
"Questionable abbreviation code."); |
| 626 |
esb_destructor(&ar); |
| 627 |
} |
| 628 |
} else if (abbrev_code < last_abbrev_code) { |
| 629 |
struct esb_s ar; |
| 630 |
esb_constructor_fixed(&ar,buf,sizeof(buf)); |
| 631 |
esb_append_printf_u(&ar, |
| 632 |
"Abbrev code %" DW_PR_DUu,abbrev_code); |
| 633 |
esb_append_printf_u(&ar, |
| 634 |
" skips down by %" DW_PR_DUu, |
| 635 |
(last_abbrev_code - abbrev_code)); |
| 636 |
esb_append_printf_u(&ar, |
| 637 |
" from last abbrev code of %" DW_PR_DUu , |
| 638 |
last_abbrev_code); |
| 639 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 640 |
esb_get_string(&ar), |
| 641 |
"Questionable abbreviation code."); |
| 642 |
esb_destructor(&ar); |
| 643 |
} else { |
| 644 |
struct esb_s ar; |
| 645 |
|
| 646 |
esb_constructor_fixed(&ar,buf,sizeof(buf)); |
| 647 |
esb_append_printf_u(&ar, |
| 648 |
"Abbrev code %" DW_PR_DUu |
| 649 |
" unchanged from last abbrev code!.", |
| 650 |
abbrev_code); |
| 651 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 652 |
esb_get_string(&ar), |
| 653 |
"Questionable abbreviation code."); |
| 654 |
esb_destructor(&ar); |
| 655 |
} |
| 656 |
return abbrev_code; |
| 657 |
} |
| 658 |
|
| 659 |
static void |
| 660 |
check_reused_code(Dwarf_Unsigned abbrev_code, |
| 661 |
Dwarf_Unsigned abbrev_entry_count) |
| 662 |
{ |
| 663 |
char buf[128]; |
| 664 |
|
| 665 |
if (abbrev_code >= abbrev_array_size) { |
| 666 |
struct esb_s ar; |
| 667 |
|
| 668 |
esb_constructor_fixed(&ar,buf,sizeof(buf)); |
| 669 |
esb_append_printf_u(&ar, |
| 670 |
"Abbrev code %" DW_PR_DUu, |
| 671 |
abbrev_code); |
| 672 |
esb_append_printf_u(&ar, |
| 673 |
" entry_count unchecked: %" DW_PR_DUu " ", |
| 674 |
abbrev_entry_count); |
| 675 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 676 |
esb_get_string(&ar), |
| 677 |
"Questionable abbreviation code."); |
| 678 |
esb_destructor(&ar); |
| 679 |
return; |
| 680 |
} |
| 681 |
if (abbrev_array[abbrev_code]) { |
| 682 |
DWARF_CHECK_COUNT(abbreviations_result,1); |
| 683 |
/* This abbrev code slot was used before. */ |
| 684 |
if (abbrev_array[abbrev_code] == abbrev_entry_count) { |
| 685 |
struct esb_s ar; |
| 686 |
|
| 687 |
esb_constructor_fixed(&ar,buf,sizeof(buf)); |
| 688 |
esb_append_printf_u(&ar, |
| 689 |
"Abbrev code %" DW_PR_DUu, |
| 690 |
abbrev_code); |
| 691 |
esb_append_printf_u(&ar, |
| 692 |
" reused for same entry_count: %" DW_PR_DUu " ", |
| 693 |
abbrev_entry_count); |
| 694 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 695 |
esb_get_string(&ar), |
| 696 |
"Questionable abbreviation code."); |
| 697 |
esb_destructor(&ar); |
| 698 |
} else { |
| 699 |
struct esb_s ar; |
| 700 |
|
| 701 |
esb_constructor_fixed(&ar,buf,sizeof(buf)); |
| 702 |
esb_append_printf_u(&ar, |
| 703 |
"Abbrev code %" DW_PR_DUu, |
| 704 |
abbrev_code); |
| 705 |
esb_append_printf_u(&ar, |
| 706 |
" reused for different entry_count. " |
| 707 |
" %" DW_PR_DUu , abbrev_array[abbrev_code]); |
| 708 |
esb_append_printf_u(&ar, |
| 709 |
" now %" DW_PR_DUu |
| 710 |
" ", |
| 711 |
abbrev_entry_count); |
| 712 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 713 |
esb_get_string(&ar), |
| 714 |
"Invalid abbreviation code."); |
| 715 |
esb_destructor(&ar); |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
/* Calculate the number of abbreviations for the |
| 721 |
current CU and set up basic abbreviations array info, |
| 722 |
storing the number of attributes per abbreviation |
| 723 |
*/ |
| 724 |
void |
| 725 |
get_abbrev_array_info(Dwarf_Debug dbg, Dwarf_Unsigned offset_in) |
| 726 |
{ |
| 727 |
Dwarf_Unsigned offset = offset_in; |
| 728 |
if (glflags.gf_check_abbreviations) { |
| 729 |
Dwarf_Unsigned length = 0; |
| 730 |
Dwarf_Unsigned last_abbrev_code = 0; |
| 731 |
Dwarf_Bool bMore = TRUE; |
| 732 |
|
| 733 |
if (!abbrev_array ) { |
| 734 |
/* Allocate initial abbreviation array info */ |
| 735 |
abbrev_array_size = ABBREV_ARRAY_INITIAL_SIZE; |
| 736 |
abbrev_array = (Dwarf_Unsigned *) |
| 737 |
calloc(abbrev_array_size,sizeof(Dwarf_Unsigned)); |
| 738 |
if (!abbrev_array) { |
| 739 |
printf("\nERROR: Unable to malloc " |
| 740 |
"abbrev_array to print abbrev data. " |
| 741 |
"Attempting to continue\n"); |
| 742 |
glflags.gf_count_major_errors++; |
| 743 |
return; |
| 744 |
} |
| 745 |
} else { |
| 746 |
/* Clear out values from previous CU */ |
| 747 |
memset((void *)abbrev_array,0, |
| 748 |
(abbrev_array_size) * sizeof(Dwarf_Unsigned)); |
| 749 |
} |
| 750 |
|
| 751 |
while (bMore) { |
| 752 |
Dwarf_Abbrev ab = 0; |
| 753 |
int abres = DW_DLV_OK; |
| 754 |
Dwarf_Unsigned abbrev_entry_count = 0; |
| 755 |
Dwarf_Unsigned abbrev_code = 0; |
| 756 |
Dwarf_Error aberr = 0; |
| 757 |
|
| 758 |
abres = dwarf_get_abbrev(dbg, offset, &ab, |
| 759 |
&length, &abbrev_entry_count, &aberr); |
| 760 |
if (abres == DW_DLV_ERROR) { |
| 761 |
destruct_abbrev_array(); |
| 762 |
print_error_and_continue( |
| 763 |
"Error reading abbreviations", abres, aberr); |
| 764 |
dwarf_dealloc(dbg,aberr,DW_DLA_ERROR); |
| 765 |
bMore = FALSE; |
| 766 |
break; |
| 767 |
} |
| 768 |
if (abres == DW_DLV_NO_ENTRY) { |
| 769 |
destruct_abbrev_array(); |
| 770 |
bMore = FALSE; |
| 771 |
break; |
| 772 |
} |
| 773 |
/* Will not error unless ab is NULL! */ |
| 774 |
dwarf_get_abbrev_code(ab,&abbrev_code,&aberr); |
| 775 |
if (abbrev_code == 0) { |
| 776 |
/* End of abbreviation table for this CU */ |
| 777 |
++offset; /* Skip abbreviation code */ |
| 778 |
bMore = FALSE; |
| 779 |
} else { |
| 780 |
/* Valid abbreviation code. We hope. */ |
| 781 |
Dwarf_Unsigned abhigh = check_abbrev_num_sequence( |
| 782 |
abbrev_code, |
| 783 |
last_abbrev_code); |
| 784 |
if (abhigh >= abbrev_array_size) { |
| 785 |
/* It is a new high, but is not outrageous. */ |
| 786 |
while (abbrev_code >= abbrev_array_size) { |
| 787 |
Dwarf_Unsigned old_size = abbrev_array_size; |
| 788 |
size_t addl_size_bytes = old_size * |
| 789 |
sizeof(Dwarf_Unsigned); |
| 790 |
Dwarf_Unsigned absize = abbrev_array_size*2; |
| 791 |
Dwarf_Unsigned * newab = 0; |
| 792 |
|
| 793 |
/* Resize abbreviation array. |
| 794 |
Only a bogus abbreviation number |
| 795 |
will iterate |
| 796 |
more than once. The abhigh check. |
| 797 |
prevents a runaway. */ |
| 798 |
newab = (Dwarf_Unsigned *) |
| 799 |
realloc(abbrev_array, |
| 800 |
absize * sizeof(Dwarf_Unsigned)); |
| 801 |
if (!newab) { |
| 802 |
static int msgcount = 0; |
| 803 |
if (!msgcount) { |
| 804 |
/* just print this once. */ |
| 805 |
printf("\nERROR: Unable to " |
| 806 |
"realloc " |
| 807 |
"abbrev_array to " |
| 808 |
"print abbrev data. " |
| 809 |
"Attempting to continue\n"); |
| 810 |
glflags.gf_count_major_errors++; |
| 811 |
} |
| 812 |
msgcount++; |
| 813 |
destruct_abbrev_array(); |
| 814 |
dwarf_dealloc(dbg, ab, DW_DLA_ABBREV); |
| 815 |
return; |
| 816 |
} |
| 817 |
abbrev_array = newab; |
| 818 |
abbrev_array_size = absize; |
| 819 |
/* Zero out the new bytes. */ |
| 820 |
memset(abbrev_array + old_size,0, |
| 821 |
addl_size_bytes); |
| 822 |
} |
| 823 |
last_abbrev_code = abbrev_code; |
| 824 |
check_reused_code(abbrev_code, |
| 825 |
abbrev_entry_count); |
| 826 |
abbrev_array[abbrev_code] = abbrev_entry_count; |
| 827 |
} else { |
| 828 |
/* Zero is the case of 'too high' abbrev_code. */ |
| 829 |
if (abhigh > 0) { |
| 830 |
/* More or less normal abbrev_code. */ |
| 831 |
last_abbrev_code = abbrev_code; |
| 832 |
check_reused_code(abbrev_code, |
| 833 |
abbrev_entry_count); |
| 834 |
abbrev_array[abbrev_code] = |
| 835 |
abbrev_entry_count; |
| 836 |
} |
| 837 |
} |
| 838 |
offset += length; |
| 839 |
} |
| 840 |
dwarf_dealloc(dbg, ab, DW_DLA_ABBREV); |
| 841 |
ab = 0; |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
/* Validate an abbreviation for the current CU. |
| 847 |
In case of bogus abbrev input the CU_abbrev_count |
| 848 |
might not be as large as abbrev_array_size says |
| 849 |
the array is. This should catch that case. |
| 850 |
This just checks and reports errors. |
| 851 |
*/ |
| 852 |
void |
| 853 |
validate_abbrev_code(Dwarf_Unsigned abbrev_code) |
| 854 |
{ |
| 855 |
char buf[128]; |
| 856 |
|
| 857 |
buf[0] = 0; |
| 858 |
DWARF_CHECK_COUNT(abbreviations_result,1); |
| 859 |
if (abbrev_code && abbrev_code >= abbrev_array_size) { |
| 860 |
struct esb_s ar; |
| 861 |
esb_constructor_fixed(&ar,buf,sizeof(buf)); |
| 862 |
if (!abbrev_array_size) { |
| 863 |
esb_append_printf_u(&ar, |
| 864 |
"Abbrev code %" DW_PR_DUu, abbrev_code); |
| 865 |
esb_append(&ar, |
| 866 |
" is invalid given the abbrev-code array size" |
| 867 |
" for the CU is zero"); |
| 868 |
} else { |
| 869 |
esb_append_printf_u(&ar, |
| 870 |
"Abbrev code %" DW_PR_DUu, abbrev_code); |
| 871 |
esb_append_printf_u(&ar, |
| 872 |
" outside valid range of [1-%" DW_PR_DUu ")" |
| 873 |
" for a CU", |
| 874 |
abbrev_array_size); |
| 875 |
} |
| 876 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 877 |
esb_get_string(&ar), |
| 878 |
"Invalid abbreviation code."); |
| 879 |
esb_destructor(&ar); |
| 880 |
} else { |
| 881 |
Dwarf_Unsigned abbrev_entry_count = |
| 882 |
abbrev_array[abbrev_code]; |
| 883 |
if (abbrev_entry_count > SNLINKER_MAX_ATTRIB_COUNT) { |
| 884 |
if (abbrev_entry_count > GENERAL_MAX_ATTRIB_COUNT) { |
| 885 |
struct esb_s ar; |
| 886 |
esb_constructor_fixed(&ar,buf,sizeof(buf)); |
| 887 |
esb_append_printf_u(&ar, |
| 888 |
"Abbrev code %" DW_PR_DUu,abbrev_code); |
| 889 |
esb_append_printf_u(&ar, |
| 890 |
", with %" DW_PR_DUu " attributes: ", |
| 891 |
abbrev_entry_count); |
| 892 |
esb_append_printf_i(&ar, |
| 893 |
"outside a sanity-check maximum of %d.", |
| 894 |
GENERAL_MAX_ATTRIB_COUNT); |
| 895 |
|
| 896 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 897 |
esb_get_string(&ar), |
| 898 |
"Number of attributes exceeds sanity check"); |
| 899 |
esb_destructor(&ar); |
| 900 |
} else { |
| 901 |
/* These apply only to one compilation environment, |
| 902 |
and are not generally applicable. */ |
| 903 |
struct esb_s ar; |
| 904 |
esb_constructor_fixed(&ar,buf,sizeof(buf)); |
| 905 |
esb_append_printf_u(&ar, |
| 906 |
"Abbrev code %" DW_PR_DUu,abbrev_code); |
| 907 |
esb_append_printf_u(&ar, |
| 908 |
", with %" DW_PR_DUu " attributes: ", |
| 909 |
abbrev_entry_count); |
| 910 |
esb_append_printf_i(&ar, |
| 911 |
"outside an SN-LINKER expected-maximum of %d.", |
| 912 |
SNLINKER_MAX_ATTRIB_COUNT); |
| 913 |
DWARF_CHECK_ERROR2(abbreviations_result, |
| 914 |
esb_get_string(&ar), |
| 915 |
"Number of attributes exceeds " |
| 916 |
"SN-LINKER-specific sanity check."); |
| 917 |
esb_destructor(&ar); |
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
} |