| 1 |
/* |
| 2 |
Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved. |
| 3 |
Portions Copyright (C) 2009-2020 David Anderson. All Rights Reserved. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it |
| 6 |
and/or modify it under the terms of version 2.1 of the |
| 7 |
GNU Lesser General Public License as published by the Free |
| 8 |
Software Foundation. |
| 9 |
|
| 10 |
This program is distributed in the hope that it would be |
| 11 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 12 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 13 |
PURPOSE. |
| 14 |
|
| 15 |
Further, this software is distributed without any warranty |
| 16 |
that it is free of the rightful claim of any third person |
| 17 |
regarding infringement or the like. Any license provided |
| 18 |
herein, whether implied or otherwise, applies only to this |
| 19 |
software file. Patent licenses, if any, provided herein |
| 20 |
do not apply to combinations of this program with other |
| 21 |
software, or any other product whatsoever. |
| 22 |
|
| 23 |
You should have received a copy of the GNU Lesser General |
| 24 |
Public License along with this program; if not, write the |
| 25 |
Free Software Foundation, Inc., 51 Franklin Street - Fifth |
| 26 |
Floor, Boston MA 02110-1301, USA. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <config.h> |
| 30 |
|
| 31 |
#include <stddef.h> /* NULL size_t */ |
| 32 |
|
| 33 |
#if defined(_WIN32) && defined(HAVE_STDAFX_H) |
| 34 |
#include "stdafx.h" |
| 35 |
#endif /* HAVE_STDAFX_H */ |
| 36 |
|
| 37 |
#include "dwarf.h" |
| 38 |
#include "libdwarf.h" |
| 39 |
#include "libdwarf_private.h" |
| 40 |
#include "dwarf_base_types.h" |
| 41 |
#include "dwarf_opaque.h" |
| 42 |
#include "libdwarf_private.h" |
| 43 |
#include "dwarf_util.h" |
| 44 |
#include "dwarf_abbrev.h" |
| 45 |
#include "dwarf_alloc.h" |
| 46 |
#include "dwarf_error.h" |
| 47 |
#include "dwarf_util.h" |
| 48 |
#include "dwarf_string.h" |
| 49 |
|
| 50 |
/* For abbrevs we first count the entries. |
| 51 |
Actually recording the attr/form/implicit const |
| 52 |
values happens later. */ |
| 53 |
int |
| 54 |
_dwarf_count_abbrev_entries(Dwarf_Debug dbg, |
| 55 |
Dwarf_Byte_Ptr abbrev_ptr, |
| 56 |
Dwarf_Byte_Ptr abbrev_section_end, |
| 57 |
Dwarf_Unsigned *abbrev_count_out, |
| 58 |
Dwarf_Unsigned *abbrev_implicit_const_count_out, |
| 59 |
Dwarf_Byte_Ptr *abbrev_ptr_out, |
| 60 |
Dwarf_Error *error) |
| 61 |
{ |
| 62 |
Dwarf_Unsigned abbrev_count = 0; |
| 63 |
Dwarf_Unsigned abbrev_implicit_const_count = 0; |
| 64 |
Dwarf_Unsigned attr_name = 0; |
| 65 |
Dwarf_Unsigned attr_form = 0; |
| 66 |
|
| 67 |
/* The abbreviations table ends with an entry with a single |
| 68 |
byte of zero for the abbreviation code. |
| 69 |
Padding bytes following that zero are allowed, but |
| 70 |
here we simply stop looking past that zero abbrev. |
| 71 |
|
| 72 |
We also stop looking if the block/section ends, |
| 73 |
though the DWARF2 and later standards do not specifically |
| 74 |
allow section/block end to terminate an abbreviations |
| 75 |
list. */ |
| 76 |
|
| 77 |
do { |
| 78 |
DECODE_LEB128_UWORD_CK(abbrev_ptr, attr_name, |
| 79 |
dbg,error,abbrev_section_end); |
| 80 |
if (attr_name > DW_AT_hi_user) { |
| 81 |
_dwarf_error(dbg, error,DW_DLE_ATTR_CORRUPT); |
| 82 |
return DW_DLV_ERROR; |
| 83 |
} |
| 84 |
DECODE_LEB128_UWORD_CK(abbrev_ptr, attr_form, |
| 85 |
dbg,error,abbrev_section_end); |
| 86 |
/* If we have attr, form as 0,0, fall through to end */ |
| 87 |
if (!_dwarf_valid_form_we_know(attr_form,attr_name)) { |
| 88 |
dwarfstring m; |
| 89 |
|
| 90 |
dwarfstring_constructor(&m); |
| 91 |
dwarfstring_append_printf_u(&m, |
| 92 |
"DW_DLE_UNKNOWN_FORM: Abbrev form 0x%" |
| 93 |
DW_PR_DUx, |
| 94 |
attr_form); |
| 95 |
if (attr_name) { |
| 96 |
dwarfstring_append_printf_u(&m, |
| 97 |
" DW_DLE_UNKNOWN_FORM: Abbrev form 0x%" |
| 98 |
DW_PR_DUx, |
| 99 |
attr_form); |
| 100 |
dwarfstring_append_printf_u(&m, |
| 101 |
" with attribute 0x%" DW_PR_DUx, |
| 102 |
attr_name); |
| 103 |
} else { |
| 104 |
dwarfstring_append_printf_u(&m, |
| 105 |
" DW_DLE_UNKNOWN_FORM(really unknown attr)" |
| 106 |
": Abbrev form 0x%" |
| 107 |
DW_PR_DUx, |
| 108 |
attr_form); |
| 109 |
dwarfstring_append_printf_u(&m, |
| 110 |
" with attribute 0x%" DW_PR_DUx, |
| 111 |
attr_name); |
| 112 |
} |
| 113 |
dwarfstring_append(&m," so abbreviations unusable. "); |
| 114 |
_dwarf_error_string(dbg, error, DW_DLE_UNKNOWN_FORM, |
| 115 |
dwarfstring_string(&m)); |
| 116 |
dwarfstring_destructor(&m); |
| 117 |
return DW_DLV_ERROR; |
| 118 |
} |
| 119 |
if (attr_form == DW_FORM_implicit_const) { |
| 120 |
/* The value is here, not in a DIE. We do |
| 121 |
nothing with it, but must read past it. */ |
| 122 |
abbrev_implicit_const_count++; |
| 123 |
SKIP_LEB128_CK(abbrev_ptr, |
| 124 |
dbg,error,abbrev_section_end); |
| 125 |
} |
| 126 |
abbrev_count++; |
| 127 |
} while ((abbrev_ptr < abbrev_section_end) && |
| 128 |
(attr_name != 0 || attr_form != 0)); |
| 129 |
/* We counted one too high,we included the 0,0 */ |
| 130 |
*abbrev_count_out = abbrev_count-1; |
| 131 |
*abbrev_implicit_const_count_out = abbrev_implicit_const_count; |
| 132 |
*abbrev_ptr_out = abbrev_ptr; |
| 133 |
return DW_DLV_OK; |
| 134 |
} |
| 135 |
|
| 136 |
/* dwarf_get_abbrev() is used to print |
| 137 |
a .debug_abbrev section without |
| 138 |
knowing about the DIEs that use the abbrevs. |
| 139 |
|
| 140 |
When we have a simple .o |
| 141 |
there is at least a hope of iterating through |
| 142 |
the abbrevs meaningfully without knowing |
| 143 |
a CU context. |
| 144 |
|
| 145 |
This often fails or gets incorrect info |
| 146 |
because there is no guarantee the .debug_abbrev |
| 147 |
section is free of garbage bytes. |
| 148 |
|
| 149 |
In an object with multiple CU/TUs the |
| 150 |
output is difficult/impossible to usefully interpret. |
| 151 |
|
| 152 |
In a dwp (Package File) it is really impossible |
| 153 |
to associate abbrevs with a CU. |
| 154 |
|
| 155 |
*/ |
| 156 |
|
| 157 |
int |
| 158 |
dwarf_get_abbrev(Dwarf_Debug dbg, |
| 159 |
Dwarf_Unsigned offset, |
| 160 |
Dwarf_Abbrev * returned_abbrev, |
| 161 |
Dwarf_Unsigned * length, |
| 162 |
Dwarf_Unsigned * abbr_count, Dwarf_Error * error) |
| 163 |
{ |
| 164 |
Dwarf_Byte_Ptr abbrev_ptr = 0; |
| 165 |
Dwarf_Byte_Ptr abbrev_ptr_out = 0; |
| 166 |
Dwarf_Byte_Ptr abbrev_section_end = 0; |
| 167 |
Dwarf_Abbrev ret_abbrev = 0; |
| 168 |
Dwarf_Unsigned labbr_count = 0; |
| 169 |
Dwarf_Unsigned utmp = 0; |
| 170 |
Dwarf_Unsigned abbrev_implicit_const_count_out = 0; |
| 171 |
int res = 0; |
| 172 |
|
| 173 |
CHECK_DBG(dbg,error,"dwarf_get_abbrev()"); |
| 174 |
if (dbg->de_debug_abbrev.dss_data == 0) { |
| 175 |
/* Loads abbrev section (and .debug_info as we do those |
| 176 |
together). */ |
| 177 |
res = _dwarf_load_debug_info(dbg, error); |
| 178 |
if (res != DW_DLV_OK) { |
| 179 |
return res; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
if (offset >= dbg->de_debug_abbrev.dss_size) { |
| 184 |
return DW_DLV_NO_ENTRY; |
| 185 |
} |
| 186 |
ret_abbrev = (Dwarf_Abbrev) _dwarf_get_alloc(dbg, |
| 187 |
DW_DLA_ABBREV, 1); |
| 188 |
if (ret_abbrev == NULL) { |
| 189 |
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL); |
| 190 |
return DW_DLV_ERROR; |
| 191 |
} |
| 192 |
ret_abbrev->dab_dbg = dbg; |
| 193 |
if (returned_abbrev == 0 || abbr_count == 0) { |
| 194 |
dwarf_dealloc(dbg, ret_abbrev, DW_DLA_ABBREV); |
| 195 |
_dwarf_error(dbg, error, DW_DLE_DWARF_ABBREV_NULL); |
| 196 |
return DW_DLV_ERROR; |
| 197 |
} |
| 198 |
|
| 199 |
*abbr_count = 0; |
| 200 |
if (length) { |
| 201 |
*length = 1; |
| 202 |
} |
| 203 |
|
| 204 |
abbrev_ptr = dbg->de_debug_abbrev.dss_data + offset; |
| 205 |
abbrev_section_end = |
| 206 |
dbg->de_debug_abbrev.dss_data + dbg->de_debug_abbrev.dss_size; |
| 207 |
res = _dwarf_leb128_uword_wrapper(dbg,&abbrev_ptr, |
| 208 |
abbrev_section_end,&utmp,error); |
| 209 |
if (res == DW_DLV_ERROR) { |
| 210 |
dwarf_dealloc(dbg, ret_abbrev, DW_DLA_ABBREV); |
| 211 |
return res; |
| 212 |
} |
| 213 |
ret_abbrev->dab_code = utmp; |
| 214 |
if (ret_abbrev->dab_code == 0) { |
| 215 |
*returned_abbrev = ret_abbrev; |
| 216 |
*abbr_count = 0; |
| 217 |
if (length) { |
| 218 |
*length = 1; |
| 219 |
} |
| 220 |
return DW_DLV_OK; |
| 221 |
} |
| 222 |
|
| 223 |
res = _dwarf_leb128_uword_wrapper(dbg,&abbrev_ptr, |
| 224 |
abbrev_section_end,&utmp,error); |
| 225 |
if (res == DW_DLV_ERROR) { |
| 226 |
dwarf_dealloc(dbg, ret_abbrev, DW_DLA_ABBREV); |
| 227 |
return res; |
| 228 |
} |
| 229 |
if (utmp > DW_TAG_hi_user) { |
| 230 |
dwarf_dealloc(dbg, ret_abbrev, DW_DLA_ABBREV); |
| 231 |
return _dwarf_format_TAG_err_msg(dbg, |
| 232 |
utmp,"DW_DLE_TAG_CORRUPT", |
| 233 |
error); |
| 234 |
} |
| 235 |
ret_abbrev->dab_tag = utmp; |
| 236 |
if (abbrev_ptr >= abbrev_section_end) { |
| 237 |
dwarfstring m; |
| 238 |
|
| 239 |
dwarf_dealloc(dbg, ret_abbrev, DW_DLA_ABBREV); |
| 240 |
dwarfstring_constructor(&m); |
| 241 |
dwarfstring_append_printf_u(&m, |
| 242 |
"DW_DLE_ABBREV_DECODE_ERROR: Ran off the end " |
| 243 |
"of the abbrev section reading tag, starting at" |
| 244 |
" abbrev section offset 0x%x",offset); |
| 245 |
_dwarf_error_string(dbg, error, |
| 246 |
DW_DLE_ABBREV_DECODE_ERROR, |
| 247 |
dwarfstring_string(&m)); |
| 248 |
dwarfstring_destructor(&m); |
| 249 |
return DW_DLV_ERROR; |
| 250 |
} |
| 251 |
ret_abbrev->dab_has_child = *(abbrev_ptr++); |
| 252 |
ret_abbrev->dab_abbrev_ptr = abbrev_ptr; |
| 253 |
ret_abbrev->dab_next_ptr = abbrev_ptr; |
| 254 |
ret_abbrev->dab_next_index = 0; |
| 255 |
res = _dwarf_count_abbrev_entries(dbg,abbrev_ptr, |
| 256 |
abbrev_section_end,&labbr_count, |
| 257 |
&abbrev_implicit_const_count_out, |
| 258 |
&abbrev_ptr_out,error); |
| 259 |
if (res == DW_DLV_ERROR) { |
| 260 |
dwarf_dealloc(dbg, ret_abbrev, DW_DLA_ABBREV); |
| 261 |
return res; |
| 262 |
} |
| 263 |
abbrev_ptr = abbrev_ptr_out; |
| 264 |
|
| 265 |
/* Global section offset. */ |
| 266 |
ret_abbrev->dab_goffset = offset; |
| 267 |
ret_abbrev->dab_count = labbr_count; |
| 268 |
ret_abbrev->dab_implicit_count = abbrev_implicit_const_count_out; |
| 269 |
if (abbrev_ptr > abbrev_section_end) { |
| 270 |
dwarf_dealloc(dbg, ret_abbrev, DW_DLA_ABBREV); |
| 271 |
_dwarf_error_string(dbg, error, |
| 272 |
DW_DLE_ABBREV_DECODE_ERROR, |
| 273 |
"DW_DLE_ABBREV_DECODE_ERROR: Ran off the end " |
| 274 |
"of the abbrev section reading abbrev_entries."); |
| 275 |
_dwarf_error(dbg, error, DW_DLE_ABBREV_DECODE_ERROR); |
| 276 |
return DW_DLV_ERROR; |
| 277 |
} |
| 278 |
if (length) { |
| 279 |
*length = abbrev_ptr - dbg->de_debug_abbrev.dss_data - offset; |
| 280 |
} |
| 281 |
*returned_abbrev = ret_abbrev; |
| 282 |
*abbr_count = labbr_count; |
| 283 |
return DW_DLV_OK; |
| 284 |
} |
| 285 |
|
| 286 |
int |
| 287 |
dwarf_get_abbrev_code(Dwarf_Abbrev abbrev, |
| 288 |
Dwarf_Unsigned * returned_code, |
| 289 |
Dwarf_Error * error) |
| 290 |
{ |
| 291 |
if (abbrev == NULL) { |
| 292 |
_dwarf_error(NULL, error, DW_DLE_DWARF_ABBREV_NULL); |
| 293 |
return DW_DLV_ERROR; |
| 294 |
} |
| 295 |
*returned_code = abbrev->dab_code; |
| 296 |
return DW_DLV_OK; |
| 297 |
} |
| 298 |
|
| 299 |
/* DWARF defines DW_TAG_hi_user as 0xffff so no tag should be |
| 300 |
over 16 bits. */ |
| 301 |
int |
| 302 |
dwarf_get_abbrev_tag(Dwarf_Abbrev abbrev, |
| 303 |
Dwarf_Half * returned_tag, Dwarf_Error * error) |
| 304 |
{ |
| 305 |
if (abbrev == NULL) { |
| 306 |
_dwarf_error(NULL, error, DW_DLE_DWARF_ABBREV_NULL); |
| 307 |
return DW_DLV_ERROR; |
| 308 |
} |
| 309 |
|
| 310 |
*returned_tag = (Dwarf_Half)abbrev->dab_tag; |
| 311 |
return DW_DLV_OK; |
| 312 |
} |
| 313 |
|
| 314 |
int |
| 315 |
dwarf_get_abbrev_children_flag(Dwarf_Abbrev abbrev, |
| 316 |
Dwarf_Signed * returned_flag, |
| 317 |
Dwarf_Error * error) |
| 318 |
{ |
| 319 |
if (abbrev == NULL) { |
| 320 |
_dwarf_error(NULL, error, DW_DLE_DWARF_ABBREV_NULL); |
| 321 |
return DW_DLV_ERROR; |
| 322 |
} |
| 323 |
|
| 324 |
*returned_flag = abbrev->dab_has_child; |
| 325 |
return DW_DLV_OK; |
| 326 |
} |
| 327 |
|
| 328 |
/* If filter_outliers is non-zero then |
| 329 |
the routine will return DW_DLV_ERROR |
| 330 |
if the leb reading generates a number that |
| 331 |
is so large it cannot be correct. |
| 332 |
|
| 333 |
If filter_outliers is 0 the uleb/sleb |
| 334 |
values read are returned, even if |
| 335 |
the values are unreasonable. This is |
| 336 |
a useful option if one wishes to |
| 337 |
have callers examine the return values |
| 338 |
in greater detail than the checking here |
| 339 |
provides. |
| 340 |
*/ |
| 341 |
int |
| 342 |
dwarf_get_abbrev_entry_b(Dwarf_Abbrev abbrev, |
| 343 |
Dwarf_Unsigned indx, |
| 344 |
Dwarf_Bool filter_outliers, |
| 345 |
Dwarf_Unsigned * returned_attr_num, |
| 346 |
Dwarf_Unsigned * returned_form, |
| 347 |
Dwarf_Signed * returned_implicitconst, |
| 348 |
Dwarf_Off * offset, |
| 349 |
Dwarf_Error * error) |
| 350 |
{ |
| 351 |
Dwarf_Byte_Ptr abbrev_ptr = 0; |
| 352 |
Dwarf_Byte_Ptr abbrev_end = 0; |
| 353 |
Dwarf_Byte_Ptr mark_abbrev_ptr = 0; |
| 354 |
Dwarf_Unsigned attr = 0; |
| 355 |
Dwarf_Unsigned form = 0; |
| 356 |
Dwarf_Unsigned implicitconst = 0; |
| 357 |
Dwarf_Debug dbg = 0; |
| 358 |
Dwarf_Signed local_indx = (Dwarf_Signed)indx; |
| 359 |
|
| 360 |
if (abbrev == NULL) { |
| 361 |
_dwarf_error(NULL, error, DW_DLE_DWARF_ABBREV_NULL); |
| 362 |
return DW_DLV_ERROR; |
| 363 |
} |
| 364 |
if (abbrev->dab_code == 0) { |
| 365 |
return DW_DLV_NO_ENTRY; |
| 366 |
} |
| 367 |
|
| 368 |
dbg = abbrev->dab_dbg; |
| 369 |
if (IS_INVALID_DBG(dbg)) { |
| 370 |
_dwarf_error_string(NULL, error, DW_DLE_DBG_NULL, |
| 371 |
"DW_DLE_DBG_NULL: " |
| 372 |
"calling dwarf_get_abbrev_entry_b() " |
| 373 |
"either null or it contains" |
| 374 |
"a stale Dwarf_Debug pointer"); |
| 375 |
return DW_DLV_ERROR; |
| 376 |
} |
| 377 |
|
| 378 |
abbrev_ptr = abbrev->dab_abbrev_ptr; |
| 379 |
abbrev_end = dbg->de_debug_abbrev.dss_data + |
| 380 |
dbg->de_debug_abbrev.dss_size; |
| 381 |
if ((Dwarf_Unsigned)local_indx >= abbrev->dab_next_index) { |
| 382 |
/* We want a part not yet scanned , |
| 383 |
so we can start closer to the desired value. */ |
| 384 |
abbrev_ptr = abbrev->dab_next_ptr; |
| 385 |
local_indx -= abbrev->dab_next_index; |
| 386 |
} |
| 387 |
|
| 388 |
for (attr = 1, form = 1; |
| 389 |
local_indx >= 0 && abbrev_ptr < abbrev_end && |
| 390 |
(attr != 0 || form != 0); |
| 391 |
local_indx--) { |
| 392 |
|
| 393 |
mark_abbrev_ptr = abbrev_ptr; |
| 394 |
DECODE_LEB128_UWORD_CK(abbrev_ptr, attr,dbg, |
| 395 |
error,abbrev_end); |
| 396 |
if (filter_outliers && attr > DW_AT_hi_user) { |
| 397 |
_dwarf_error(dbg, error,DW_DLE_ATTR_CORRUPT); |
| 398 |
return DW_DLV_ERROR; |
| 399 |
} |
| 400 |
DECODE_LEB128_UWORD_CK(abbrev_ptr, form,dbg, |
| 401 |
error,abbrev_end); |
| 402 |
if (filter_outliers && |
| 403 |
!_dwarf_valid_form_we_know(form,attr)) { |
| 404 |
_dwarf_error(dbg, error, DW_DLE_UNKNOWN_FORM); |
| 405 |
return DW_DLV_ERROR; |
| 406 |
} |
| 407 |
if (form == DW_FORM_implicit_const) { |
| 408 |
/* The value is here, not in a DIE. */ |
| 409 |
DECODE_LEB128_SWORD_CK( abbrev_ptr, implicitconst, |
| 410 |
dbg,error,abbrev_end); |
| 411 |
} else { |
| 412 |
implicitconst = 0; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
if (abbrev_ptr >= abbrev_end) { |
| 417 |
_dwarf_error_string(dbg, error, |
| 418 |
DW_DLE_ABBREV_DECODE_ERROR, |
| 419 |
"DW_DLE_ABBREV_DECODE_ERROR: Ran off the end " |
| 420 |
"of the abbrev section reading abbrev entries.."); |
| 421 |
return DW_DLV_ERROR; |
| 422 |
} |
| 423 |
|
| 424 |
if (local_indx >= 0) { |
| 425 |
return DW_DLV_NO_ENTRY; |
| 426 |
} |
| 427 |
|
| 428 |
if (returned_form != NULL) { |
| 429 |
*returned_form = form; |
| 430 |
} |
| 431 |
if (offset != NULL) { |
| 432 |
*offset = mark_abbrev_ptr - dbg->de_debug_abbrev.dss_data; |
| 433 |
} |
| 434 |
if (returned_attr_num) { |
| 435 |
*returned_attr_num = attr; |
| 436 |
} |
| 437 |
if (returned_implicitconst) { |
| 438 |
/* Callers should only examine implicit const value |
| 439 |
if the form is DW_FORM_implicit_const. */ |
| 440 |
*returned_implicitconst = implicitconst; |
| 441 |
} |
| 442 |
abbrev->dab_next_ptr = abbrev_ptr; |
| 443 |
abbrev->dab_next_index = (Dwarf_Unsigned)local_indx ; |
| 444 |
return DW_DLV_OK; |
| 445 |
} |