| 1 |
/* |
| 2 |
Copyright (C) 2018-2020 David Anderson. All Rights Reserved. |
| 3 |
|
| 4 |
This program is free software; you can redistribute it |
| 5 |
and/or modify it under the terms of version 2.1 of the |
| 6 |
GNU Lesser General Public License as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is distributed in the hope that it would |
| 10 |
be useful, but WITHOUT ANY WARRANTY; without even the |
| 11 |
implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 |
PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
Further, this software is distributed without any warranty |
| 15 |
that it is free of the rightful claim of any third person |
| 16 |
regarding infringement or the like. Any license provided |
| 17 |
herein, whether implied or otherwise, applies only to |
| 18 |
this software file. Patent licenses, if any, provided |
| 19 |
herein do not apply to combinations of this program with |
| 20 |
other software, or any other product whatsoever. |
| 21 |
|
| 22 |
You should have received a copy of the GNU Lesser General |
| 23 |
Public License along with this program; if not, write |
| 24 |
the Free Software Foundation, Inc., 51 Franklin Street - |
| 25 |
Fifth Floor, Boston MA 02110-1301, USA. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <config.h> |
| 29 |
|
| 30 |
#include <stddef.h> /* NULL size_t */ |
| 31 |
#ifdef HAVE_STDINT_H |
| 32 |
#include <stdint.h> /* uintptr_t */ |
| 33 |
#endif /* HAVE_STDINT_H */ |
| 34 |
|
| 35 |
#if defined(_WIN32) && defined(HAVE_STDAFX_H) |
| 36 |
#include "stdafx.h" |
| 37 |
#endif /* HAVE_STDAFX_H */ |
| 38 |
#include <stdio.h> /* printf */ |
| 39 |
|
| 40 |
#include "dwarf.h" |
| 41 |
#include "libdwarf.h" |
| 42 |
#include "libdwarf_private.h" |
| 43 |
#include "dwarf_base_types.h" |
| 44 |
#include "dwarf_opaque.h" |
| 45 |
#include "dwarf_alloc.h" |
| 46 |
#include "dwarf_error.h" |
| 47 |
#include "dwarf_util.h" |
| 48 |
#include "dwarf_string.h" |
| 49 |
#include "dwarf_str_offsets.h" |
| 50 |
|
| 51 |
#define STR_OFFSETS_MAGIC 0x2feed2 |
| 52 |
|
| 53 |
#define VALIDATE_SOT(xsot) \ |
| 54 |
if (!(xsot)) { \ |
| 55 |
_dwarf_error(NULL,error,DW_DLE_STR_OFFSETS_NULLARGUMENT);\ |
| 56 |
return DW_DLV_ERROR; \ |
| 57 |
} \ |
| 58 |
if (!(xsot)->so_dbg) { \ |
| 59 |
_dwarf_error(NULL,error,DW_DLE_STR_OFFSETS_NULL_DBG);\ |
| 60 |
return DW_DLV_ERROR; \ |
| 61 |
} \ |
| 62 |
if ((xsot)->so_magic_value != STR_OFFSETS_MAGIC) { \ |
| 63 |
_dwarf_error((xsot)->so_dbg,error, \ |
| 64 |
DW_DLE_STR_OFFSETS_NO_MAGIC); \ |
| 65 |
return DW_DLV_ERROR; \ |
| 66 |
} |
| 67 |
|
| 68 |
#if 0 /* dump_bytes */ |
| 69 |
static void |
| 70 |
dump_bytes(char * msg,Dwarf_Small * start, long len) |
| 71 |
{ |
| 72 |
Dwarf_Small *end = start + len; |
| 73 |
Dwarf_Small *cur = start; |
| 74 |
|
| 75 |
printf("%s starting at %p \n",msg,(void*)start); |
| 76 |
for (; cur < end; cur++) { |
| 77 |
printf("%02x ", *cur); |
| 78 |
} |
| 79 |
printf("\n"); |
| 80 |
} |
| 81 |
#endif /*0*/ |
| 82 |
|
| 83 |
int |
| 84 |
dwarf_open_str_offsets_table_access(Dwarf_Debug dbg, |
| 85 |
Dwarf_Str_Offsets_Table * table_data, |
| 86 |
Dwarf_Error * error) |
| 87 |
{ |
| 88 |
int res = 0; |
| 89 |
Dwarf_Str_Offsets_Table local_table_data = 0; |
| 90 |
Dwarf_Small *offsets_start_ptr = 0; |
| 91 |
Dwarf_Unsigned sec_size = 0; |
| 92 |
|
| 93 |
CHECK_DBG(dbg,error,"dwarf_open_str_offsets_table_access()"); |
| 94 |
if (!table_data) { |
| 95 |
_dwarf_error(dbg,error,DW_DLE_STR_OFFSETS_NULLARGUMENT); |
| 96 |
return DW_DLV_ERROR; |
| 97 |
} |
| 98 |
/* Considered testing for *table_data being NULL, but |
| 99 |
not doing such a test. */ |
| 100 |
|
| 101 |
res = _dwarf_load_section(dbg, &dbg->de_debug_str_offsets,error); |
| 102 |
if (res != DW_DLV_OK) { |
| 103 |
return res; |
| 104 |
} |
| 105 |
offsets_start_ptr = dbg->de_debug_str_offsets.dss_data; |
| 106 |
if (!offsets_start_ptr) { |
| 107 |
return DW_DLV_NO_ENTRY; |
| 108 |
} |
| 109 |
sec_size = dbg->de_debug_str_offsets.dss_size; |
| 110 |
local_table_data = (Dwarf_Str_Offsets_Table)_dwarf_get_alloc(dbg, |
| 111 |
DW_DLA_STR_OFFSETS,1); |
| 112 |
if (!local_table_data) { |
| 113 |
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL); |
| 114 |
return DW_DLV_ERROR; |
| 115 |
} |
| 116 |
local_table_data->so_dbg = dbg; |
| 117 |
local_table_data->so_magic_value = STR_OFFSETS_MAGIC; |
| 118 |
local_table_data->so_section_start_ptr = offsets_start_ptr; |
| 119 |
local_table_data->so_section_end_ptr = offsets_start_ptr + |
| 120 |
sec_size; |
| 121 |
local_table_data->so_section_size = sec_size; |
| 122 |
local_table_data->so_next_table_offset = 0; |
| 123 |
local_table_data->so_wasted_section_bytes = 0; |
| 124 |
/* get_alloc zeroed all the bits, no need to repeat that here. */ |
| 125 |
*table_data = local_table_data; |
| 126 |
return DW_DLV_OK; |
| 127 |
} |
| 128 |
|
| 129 |
int |
| 130 |
dwarf_close_str_offsets_table_access( |
| 131 |
Dwarf_Str_Offsets_Table table_data, |
| 132 |
Dwarf_Error * error) |
| 133 |
{ |
| 134 |
Dwarf_Debug dbg = 0; |
| 135 |
|
| 136 |
VALIDATE_SOT(table_data) |
| 137 |
dbg = table_data->so_dbg; |
| 138 |
table_data->so_magic_value = 0xdead; |
| 139 |
dwarf_dealloc(dbg,table_data, DW_DLA_STR_OFFSETS); |
| 140 |
return DW_DLV_OK; |
| 141 |
} |
| 142 |
|
| 143 |
int |
| 144 |
dwarf_str_offsets_value_by_index(Dwarf_Str_Offsets_Table sot, |
| 145 |
Dwarf_Unsigned index, |
| 146 |
Dwarf_Unsigned *stroffset, |
| 147 |
Dwarf_Error *error) |
| 148 |
{ |
| 149 |
Dwarf_Small *entryptr = 0; |
| 150 |
Dwarf_Unsigned val = 0; |
| 151 |
Dwarf_Unsigned entryoffset = 0; |
| 152 |
Dwarf_Unsigned secsize = 0; |
| 153 |
Dwarf_Debug dbg = 0; |
| 154 |
Dwarf_Small *end_ptr = 0; |
| 155 |
/* so_section_size */ |
| 156 |
|
| 157 |
VALIDATE_SOT(sot) |
| 158 |
dbg = sot->so_dbg; |
| 159 |
secsize = dbg->de_debug_str_offsets.dss_size; |
| 160 |
if (index >= sot->so_array_entry_count) { |
| 161 |
return DW_DLV_NO_ENTRY; |
| 162 |
} |
| 163 |
entryoffset = sot->so_table_start_offset + |
| 164 |
sot->so_lcl_offset_to_array; |
| 165 |
entryoffset += index*sot->so_array_entry_size; |
| 166 |
entryptr = sot->so_section_start_ptr + entryoffset; |
| 167 |
if (entryoffset > secsize || |
| 168 |
(entryoffset+sot->so_array_entry_size) > secsize) { |
| 169 |
_dwarf_error_string(dbg,error, |
| 170 |
DW_DLE_STR_OFFSETS_ARRAY_INDEX_WRONG, |
| 171 |
"DW_DLE_STR_OFFSETS_ARRAY_INDEX_WRONG: " |
| 172 |
"A libdwarf internal bug. Report to the maintainers"); |
| 173 |
return DW_DLV_ERROR; |
| 174 |
} |
| 175 |
end_ptr = entryptr+sot->so_array_entry_size; |
| 176 |
READ_UNALIGNED_CK(dbg, val, Dwarf_Unsigned, |
| 177 |
entryptr, sot->so_array_entry_size,error,end_ptr); |
| 178 |
*stroffset = val; |
| 179 |
return DW_DLV_OK; |
| 180 |
} |
| 181 |
|
| 182 |
/* The minimum possible area .debug_str_offsets header . */ |
| 183 |
#define MIN_HEADER_LENGTH 8 |
| 184 |
|
| 185 |
/* New April 2018. |
| 186 |
Beginning at starting_offset zero, |
| 187 |
returns data about the first table found. |
| 188 |
The value *next_table_offset is the value |
| 189 |
of the next table (if any), one byte past |
| 190 |
the end of the table whose data is returned.. |
| 191 |
Returns DW_DLV_NO_ENTRY if the starting offset |
| 192 |
is past the end of valid data. |
| 193 |
|
| 194 |
There is no guarantee that there are no non-0 nonsense |
| 195 |
bytes in the section outside of useful tables, |
| 196 |
so this can fail and return nonsense or |
| 197 |
DW_DLV_ERROR if such garbage exists. |
| 198 |
*/ |
| 199 |
|
| 200 |
static int |
| 201 |
is_all_zeroes(Dwarf_Small*start, |
| 202 |
Dwarf_Small*end) |
| 203 |
{ |
| 204 |
if (start >= end) { |
| 205 |
/* We should not get here, this is just |
| 206 |
a defensive test. */ |
| 207 |
return TRUE; |
| 208 |
} |
| 209 |
for ( ; start < end; ++start) { |
| 210 |
if (!*start) { |
| 211 |
/* There is some garbage here. */ |
| 212 |
return FALSE; |
| 213 |
} |
| 214 |
} |
| 215 |
/* All just zero bytes. */ |
| 216 |
return TRUE; |
| 217 |
} |
| 218 |
|
| 219 |
static void |
| 220 |
emit_invalid_dw5tab(Dwarf_Debug dbg, |
| 221 |
Dwarf_Unsigned headerlen, |
| 222 |
Dwarf_Error *error) |
| 223 |
{ |
| 224 |
dwarfstring m; |
| 225 |
|
| 226 |
dwarfstring_constructor(&m); |
| 227 |
dwarfstring_append_printf_u(&m, |
| 228 |
"DW_DLE_SECTION_SIZE_ERROR: " |
| 229 |
"header length 0x%x is too small " |
| 230 |
"to be a real .debug_str_offsets " |
| 231 |
"DWARF5 section", |
| 232 |
headerlen); |
| 233 |
_dwarf_error_string(dbg,error, |
| 234 |
DW_DLE_SECTION_SIZE_ERROR, |
| 235 |
dwarfstring_string(&m)); |
| 236 |
dwarfstring_destructor(&m); |
| 237 |
} |
| 238 |
static void |
| 239 |
emit_offsets_array_msg(Dwarf_Debug dbg, |
| 240 |
Dwarf_Unsigned tab_length, |
| 241 |
Dwarf_Unsigned secsize, |
| 242 |
Dwarf_Error *error) |
| 243 |
{ |
| 244 |
dwarfstring m; |
| 245 |
|
| 246 |
dwarfstring_constructor(&m); |
| 247 |
dwarfstring_append_printf_u(&m, |
| 248 |
"DW_DLE_STR_OFFSETS_ARRAY_SIZE: " |
| 249 |
" total length 0x%x with table offset larger than ", |
| 250 |
tab_length); |
| 251 |
dwarfstring_append_printf_u(&m, |
| 252 |
".debug_str_offsets section size of 0x%x." |
| 253 |
" Perhaps the section is a GNU DWARF4" |
| 254 |
" extension with a different format.", |
| 255 |
secsize); |
| 256 |
_dwarf_error_string(dbg,error, |
| 257 |
DW_DLE_STR_OFFSETS_ARRAY_SIZE, |
| 258 |
dwarfstring_string(&m)); |
| 259 |
dwarfstring_destructor(&m); |
| 260 |
} |
| 261 |
|
| 262 |
static void |
| 263 |
emit_wrong_version(Dwarf_Debug dbg, |
| 264 |
Dwarf_Half version, |
| 265 |
Dwarf_Error *error) |
| 266 |
{ |
| 267 |
dwarfstring m; |
| 268 |
|
| 269 |
dwarfstring_constructor(&m); |
| 270 |
dwarfstring_append_printf_u(&m, |
| 271 |
"DW_DLE_STR_OFFSETS_VERSION_WRONG: " |
| 272 |
"%u. Only version 5 is supported " |
| 273 |
"when reading .debug_str_offsets." |
| 274 |
" Perhaps the section is a GNU DWARF4" |
| 275 |
" extension with a different format.", |
| 276 |
version); |
| 277 |
_dwarf_error_string(dbg,error, |
| 278 |
DW_DLE_STR_OFFSETS_VERSION_WRONG, |
| 279 |
dwarfstring_string(&m)); |
| 280 |
dwarfstring_destructor(&m); |
| 281 |
} |
| 282 |
|
| 283 |
int |
| 284 |
_dwarf_trial_read_dwarf_five_hdr(Dwarf_Debug dbg, |
| 285 |
Dwarf_Unsigned new_table_offset, |
| 286 |
Dwarf_Unsigned secsize, |
| 287 |
Dwarf_Unsigned *table_local_offset_of_array, |
| 288 |
Dwarf_Unsigned *total_table_length, |
| 289 |
Dwarf_Unsigned *length_out, |
| 290 |
Dwarf_Half *local_offset_size_out, |
| 291 |
Dwarf_Half *local_extension_size_out, |
| 292 |
Dwarf_Half *version_out, |
| 293 |
Dwarf_Half *padding_out, |
| 294 |
Dwarf_Error *error) |
| 295 |
{ |
| 296 |
Dwarf_Unsigned length = 0; /* length following the |
| 297 |
local_offset_size + local_extension_size */ |
| 298 |
Dwarf_Unsigned local_offset_size = 0; |
| 299 |
Dwarf_Unsigned local_extension_size = 0; |
| 300 |
Dwarf_Unsigned tab_length = 0; |
| 301 |
Dwarf_Unsigned array_local_offset = 0; |
| 302 |
Dwarf_Half version = 0; |
| 303 |
Dwarf_Half padding = 0; |
| 304 |
Dwarf_Unsigned globloff = 0; |
| 305 |
Dwarf_Unsigned net_end_offset = 0; |
| 306 |
Dwarf_Small *secstart = dbg->de_debug_str_offsets.dss_data; |
| 307 |
Dwarf_Small *table_start_ptr = secstart + new_table_offset; |
| 308 |
Dwarf_Small *secendptr = secstart+secsize; |
| 309 |
|
| 310 |
READ_AREA_LENGTH_CK(dbg,length,Dwarf_Unsigned, |
| 311 |
table_start_ptr,local_offset_size, |
| 312 |
local_extension_size,error, |
| 313 |
secsize,secendptr); |
| 314 |
/* The 'length' part of any header is |
| 315 |
local_extension_size + local_offset_size. |
| 316 |
The length of an offset in the section is just |
| 317 |
local_offset_size. |
| 318 |
Standard DWARF2 sums to 4. |
| 319 |
Standard DWARF3,4,5 sums to 4 or 12. |
| 320 |
Nonstandard SGI IRIX 64bit dwarf sums to 8 (SGI IRIX |
| 321 |
was all DWARF2 and could not have a .debug_str_offsets |
| 322 |
section). |
| 323 |
The header includes 2 bytes of version and two bytes |
| 324 |
of padding. */ |
| 325 |
if (length < 4) { |
| 326 |
/* Usually DW4-style .debug_str_offsets |
| 327 |
starts off with a zero value to ref the |
| 328 |
base string in .debug_str. Should not apply here. |
| 329 |
Any tiny value is guaranteed not to be a legal |
| 330 |
DWARF5 .debug_str_offsets section. */ |
| 331 |
emit_invalid_dw5tab(dbg,length,error); |
| 332 |
return DW_DLV_ERROR; |
| 333 |
} |
| 334 |
globloff = new_table_offset; |
| 335 |
array_local_offset = local_extension_size +local_offset_size |
| 336 |
+4; /* 4 for the two Dwarf_Half in the table header */ |
| 337 |
tab_length = local_extension_size +local_offset_size + |
| 338 |
length; |
| 339 |
|
| 340 |
net_end_offset = tab_length+globloff; |
| 341 |
|
| 342 |
if (length > secsize || |
| 343 |
array_local_offset > secsize || |
| 344 |
tab_length > secsize || |
| 345 |
net_end_offset > secsize) { |
| 346 |
emit_offsets_array_msg(dbg,tab_length, |
| 347 |
secsize,error); |
| 348 |
return DW_DLV_ERROR; |
| 349 |
} |
| 350 |
/* table_start_ptr is incremented past |
| 351 |
the length data by read-area-length-ck. */ |
| 352 |
READ_UNALIGNED_CK(dbg, version, Dwarf_Half, |
| 353 |
table_start_ptr, DWARF_HALF_SIZE, |
| 354 |
error,secendptr); |
| 355 |
table_start_ptr += DWARF_HALF_SIZE; |
| 356 |
if (version != DW_STR_OFFSETS_VERSION5) { |
| 357 |
emit_wrong_version(dbg,version,error); |
| 358 |
return DW_DLV_ERROR; |
| 359 |
} |
| 360 |
READ_UNALIGNED_CK(dbg, padding, Dwarf_Half, |
| 361 |
table_start_ptr, DWARF_HALF_SIZE, |
| 362 |
error,secendptr); |
| 363 |
|
| 364 |
/* padding should be zero, but we are |
| 365 |
not checking it here at present. */ |
| 366 |
*table_local_offset_of_array = array_local_offset; |
| 367 |
*total_table_length = tab_length; |
| 368 |
*length_out = length; |
| 369 |
*local_offset_size_out = (Dwarf_Half)local_offset_size; |
| 370 |
*local_extension_size_out = (Dwarf_Half)local_extension_size; |
| 371 |
*version_out = version; |
| 372 |
*padding_out = padding; |
| 373 |
return DW_DLV_OK; |
| 374 |
} |
| 375 |
|
| 376 |
/* Used by code reading attributes/forms and |
| 377 |
also by code reading the raw .debug_str_offsets |
| 378 |
section, hence the code allows for |
| 379 |
output arguments to be zero. |
| 380 |
If cucontext is null it means the call part |
| 381 |
of trying to print the section without |
| 382 |
accessing any context. dwarfdump option |
| 383 |
--print-str-offsets. |
| 384 |
New 30 August 2020. */ |
| 385 |
int |
| 386 |
_dwarf_read_str_offsets_header(Dwarf_Str_Offsets_Table sot, |
| 387 |
Dwarf_CU_Context cucontext, |
| 388 |
/* Followed by return values/error */ |
| 389 |
Dwarf_Unsigned *length_out, |
| 390 |
Dwarf_Half *offset_size_out, |
| 391 |
Dwarf_Half *extension_size_out, |
| 392 |
Dwarf_Half *version_out, |
| 393 |
Dwarf_Half *padding_out, |
| 394 |
Dwarf_Unsigned *local_offset_to_array_out, |
| 395 |
Dwarf_Unsigned *total_table_length_out, |
| 396 |
Dwarf_Error *error) |
| 397 |
{ |
| 398 |
Dwarf_Unsigned length = 0; |
| 399 |
Dwarf_Half local_offset_size = 0; |
| 400 |
Dwarf_Half local_extension_size = 0; |
| 401 |
Dwarf_Half version = 0; |
| 402 |
Dwarf_Half padding = 0; |
| 403 |
int res = 0; |
| 404 |
Dwarf_Bool is_dwarf_five = TRUE; |
| 405 |
Dwarf_Debug dbg = sot->so_dbg; |
| 406 |
Dwarf_Unsigned secsize = sot->so_section_size; |
| 407 |
Dwarf_Unsigned table_local_offset_of_array = 0; |
| 408 |
Dwarf_Unsigned total_table_length = 0; |
| 409 |
Dwarf_Unsigned globaltaboff = 0; |
| 410 |
|
| 411 |
globaltaboff = sot->so_next_table_offset; |
| 412 |
if (cucontext) { |
| 413 |
if (cucontext->cc_str_offsets_tab_present) { |
| 414 |
/* cu_context has what it needs already and we do |
| 415 |
not need the rest of what the interface |
| 416 |
provides */ |
| 417 |
return DW_DLV_OK; |
| 418 |
} |
| 419 |
} |
| 420 |
res = _dwarf_trial_read_dwarf_five_hdr(dbg, |
| 421 |
globaltaboff, |
| 422 |
secsize, |
| 423 |
&table_local_offset_of_array, |
| 424 |
&total_table_length, |
| 425 |
/* Length is the length field from the table involved */ |
| 426 |
&length, |
| 427 |
&local_offset_size, |
| 428 |
&local_extension_size, |
| 429 |
&version, |
| 430 |
&padding, |
| 431 |
error); |
| 432 |
if (res != DW_DLV_OK) { |
| 433 |
if (res == DW_DLV_ERROR && error) { |
| 434 |
dwarf_dealloc_error(dbg,*error); |
| 435 |
*error = 0; |
| 436 |
} |
| 437 |
/* If it's really DWARF5 but with a serious |
| 438 |
problem this will think...NOT 5! */ |
| 439 |
is_dwarf_five = FALSE; |
| 440 |
} |
| 441 |
if ( !is_dwarf_five) { |
| 442 |
length = secsize; |
| 443 |
/* This is possibly |
| 444 |
GNU Dwarf4 extension .debug_str_offsets. |
| 445 |
It could also just be corrupted data. |
| 446 |
offset size is not going to be 8. |
| 447 |
de_length_size is a guess and not set at this point . |
| 448 |
We assume the data starts with the array |
| 449 |
of string offsets, |
| 450 |
and all in one simple array from start to end |
| 451 |
of section. */ |
| 452 |
table_local_offset_of_array = 0; |
| 453 |
total_table_length = secsize; |
| 454 |
length = secsize; |
| 455 |
local_offset_size = 4; |
| 456 |
local_extension_size = 0; |
| 457 |
version = DW_STR_OFFSETS_VERSION4; |
| 458 |
padding = 0; |
| 459 |
} |
| 460 |
|
| 461 |
if (length_out) { |
| 462 |
*length_out = length; |
| 463 |
} |
| 464 |
if (offset_size_out) { |
| 465 |
*offset_size_out = local_offset_size; |
| 466 |
} |
| 467 |
if (extension_size_out) { |
| 468 |
*extension_size_out = local_extension_size; |
| 469 |
} |
| 470 |
if (version_out) { |
| 471 |
*version_out = version; |
| 472 |
} |
| 473 |
if (padding_out) { |
| 474 |
*padding_out = padding; |
| 475 |
} |
| 476 |
if (cucontext) { |
| 477 |
cucontext->cc_str_offsets_header_offset = globaltaboff; |
| 478 |
cucontext->cc_str_offsets_tab_present = TRUE; |
| 479 |
cucontext->cc_str_offsets_tab_to_array = |
| 480 |
table_local_offset_of_array; |
| 481 |
cucontext->cc_str_offsets_version = version; |
| 482 |
cucontext->cc_str_offsets_offset_size = local_offset_size; |
| 483 |
cucontext->cc_str_offsets_table_size = total_table_length; |
| 484 |
} |
| 485 |
if (local_offset_to_array_out) { |
| 486 |
*local_offset_to_array_out = table_local_offset_of_array; |
| 487 |
} |
| 488 |
if (total_table_length_out) { |
| 489 |
*total_table_length_out = total_table_length; |
| 490 |
} |
| 491 |
return DW_DLV_OK; |
| 492 |
} |
| 493 |
|
| 494 |
int |
| 495 |
dwarf_next_str_offsets_table(Dwarf_Str_Offsets_Table sot, |
| 496 |
Dwarf_Unsigned *unit_length_out, |
| 497 |
Dwarf_Unsigned *unit_length_offset_out, |
| 498 |
Dwarf_Unsigned *table_start_offset_out, |
| 499 |
Dwarf_Half *entry_size_out, |
| 500 |
Dwarf_Half *version_out, |
| 501 |
Dwarf_Half *padding_out, |
| 502 |
Dwarf_Unsigned *table_value_count_out, |
| 503 |
Dwarf_Error * error) |
| 504 |
{ |
| 505 |
|
| 506 |
Dwarf_Small *table_header_ptr = 0; |
| 507 |
Dwarf_Unsigned table_header_offset = 0; |
| 508 |
Dwarf_Unsigned table_end_offset = 0; |
| 509 |
Dwarf_Unsigned array_start_offset = 0; |
| 510 |
Dwarf_Unsigned length = 0; |
| 511 |
Dwarf_Half local_length_size = 0; |
| 512 |
Dwarf_Half local_extension_size = 0; |
| 513 |
Dwarf_Half version = 0; |
| 514 |
Dwarf_Half padding = 0; |
| 515 |
Dwarf_Unsigned local_offset_to_array= 0; |
| 516 |
Dwarf_Unsigned total_table_length = 0; |
| 517 |
Dwarf_Debug dbg = sot->so_dbg; |
| 518 |
int res = 0; |
| 519 |
|
| 520 |
VALIDATE_SOT(sot) |
| 521 |
|
| 522 |
table_header_offset = sot->so_next_table_offset; |
| 523 |
if (table_header_offset >= sot->so_section_size) { |
| 524 |
return DW_DLV_NO_ENTRY; |
| 525 |
} |
| 526 |
table_header_ptr = sot->so_section_start_ptr + |
| 527 |
table_header_offset; |
| 528 |
if (table_header_offset >= sot->so_section_size) { |
| 529 |
if (table_header_ptr == sot->so_section_end_ptr) { |
| 530 |
/* At end of section. Done. */ |
| 531 |
return DW_DLV_NO_ENTRY; |
| 532 |
} else { |
| 533 |
/* bogus table offset. */ |
| 534 |
Dwarf_Unsigned len = 0; |
| 535 |
dwarfstring m; |
| 536 |
|
| 537 |
len = (sot->so_section_end_ptr >= table_header_ptr)? |
| 538 |
(Dwarf_Unsigned)(uintptr_t)sot->so_section_end_ptr - |
| 539 |
(Dwarf_Unsigned)(uintptr_t)table_header_ptr: |
| 540 |
(Dwarf_Unsigned)0xffffffff; |
| 541 |
dwarfstring_constructor(&m); |
| 542 |
dwarfstring_append_printf_i(&m, |
| 543 |
"DW_DLE_STR_OFFSETS_EXTRA_BYTES: " |
| 544 |
"Table Offset is %" DW_PR_DSd |
| 545 |
" bytes past end of section",len); |
| 546 |
_dwarf_error_string(dbg,error, |
| 547 |
DW_DLE_STR_OFFSETS_EXTRA_BYTES, |
| 548 |
dwarfstring_string(&m)); |
| 549 |
dwarfstring_destructor(&m); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
if ((table_header_ptr + MIN_HEADER_LENGTH) > |
| 554 |
sot->so_section_end_ptr) { |
| 555 |
|
| 556 |
/* We have a too-short section it appears. |
| 557 |
Should we generate error? Or ignore? |
| 558 |
As of March 10 2020 we check for garbage |
| 559 |
bytes in-section. */ |
| 560 |
dwarfstring m; |
| 561 |
Dwarf_Small *hend = 0; |
| 562 |
Dwarf_Unsigned len = 0; |
| 563 |
|
| 564 |
if (is_all_zeroes(table_header_ptr,sot->so_section_end_ptr)){ |
| 565 |
return DW_DLV_NO_ENTRY; |
| 566 |
} |
| 567 |
hend = table_header_ptr + MIN_HEADER_LENGTH; |
| 568 |
len = (hend >= sot->so_section_end_ptr)? |
| 569 |
(Dwarf_Unsigned)(uintptr_t)sot->so_section_end_ptr - |
| 570 |
(Dwarf_Unsigned)(uintptr_t)table_header_ptr: |
| 571 |
(Dwarf_Unsigned)0xffffffff; |
| 572 |
dwarfstring_constructor(&m); |
| 573 |
dwarfstring_append_printf_i(&m, |
| 574 |
"DW_DLE_STR_OFFSETS_EXTRA_BYTES: " |
| 575 |
"Table Offset plus a minimal header is %" |
| 576 |
DW_PR_DSd |
| 577 |
" bytes past end of section" |
| 578 |
" and some bytes in-section are non-zero",len); |
| 579 |
_dwarf_error_string(dbg,error, |
| 580 |
DW_DLE_STR_OFFSETS_EXTRA_BYTES, |
| 581 |
dwarfstring_string(&m)); |
| 582 |
dwarfstring_destructor(&m); |
| 583 |
return DW_DLV_ERROR; |
| 584 |
} |
| 585 |
|
| 586 |
res = _dwarf_read_str_offsets_header(sot, |
| 587 |
0 /* cu_context */, |
| 588 |
&length, |
| 589 |
&local_length_size, |
| 590 |
&local_extension_size, |
| 591 |
&version, |
| 592 |
&padding, |
| 593 |
&local_offset_to_array, |
| 594 |
&total_table_length, error); |
| 595 |
if (res != DW_DLV_OK) { |
| 596 |
return res; |
| 597 |
} |
| 598 |
if (version == DW_STR_OFFSETS_VERSION5) { |
| 599 |
/* header_length includes length field plus 4 for |
| 600 |
the rest of the header. Don't double count! |
| 601 |
length includes space for 4 bytes of the header! */ |
| 602 |
array_start_offset = table_header_offset + |
| 603 |
local_offset_to_array; |
| 604 |
table_end_offset = array_start_offset + total_table_length; |
| 605 |
} else { |
| 606 |
Dwarf_Unsigned space_left = 0; |
| 607 |
|
| 608 |
/* leave table header offset as-is */ |
| 609 |
space_left = sot->so_section_size - table_header_offset; |
| 610 |
array_start_offset = table_header_offset; |
| 611 |
table_end_offset = table_header_offset + space_left; |
| 612 |
} |
| 613 |
{ |
| 614 |
Dwarf_Unsigned entrycount = 0; |
| 615 |
Dwarf_Unsigned entrybytes = 0; |
| 616 |
|
| 617 |
entrybytes = table_end_offset - array_start_offset; |
| 618 |
if (entrybytes % local_length_size) { |
| 619 |
_dwarf_error_string(dbg,error, |
| 620 |
DW_DLE_STR_OFFSETS_ARRAY_SIZE, |
| 621 |
"DW_DLE_STR_OFFSETS_ARRAY_SIZE " |
| 622 |
" array size not a multiple of the size of " |
| 623 |
"a single entry"); |
| 624 |
return DW_DLV_ERROR; |
| 625 |
} |
| 626 |
entrycount = entrybytes/local_length_size; |
| 627 |
|
| 628 |
/* On the next table loop this will be the new table offset */ |
| 629 |
sot->so_next_table_offset = table_end_offset; |
| 630 |
|
| 631 |
sot->so_table_end_offset = table_end_offset; |
| 632 |
sot->so_table_start_offset = table_header_offset; |
| 633 |
sot->so_table_end_offset = table_header_offset+ |
| 634 |
total_table_length; |
| 635 |
sot->so_array_entry_count = entrycount; |
| 636 |
sot->so_array_entry_size = local_length_size; |
| 637 |
sot->so_table_count += 1; |
| 638 |
|
| 639 |
/* The data length in bytes following the unit_length field |
| 640 |
of the header. includes any other header bytes |
| 641 |
and the table space */ |
| 642 |
*unit_length_out = length; |
| 643 |
|
| 644 |
/* Where the unit_length field starts in the section. */ |
| 645 |
*unit_length_offset_out = sot->so_table_start_offset; |
| 646 |
|
| 647 |
/* Where the table of offsets starts in the section. */ |
| 648 |
*table_start_offset_out = sot->so_table_start_offset; |
| 649 |
|
| 650 |
/* Entrysize: 4 or 8 */ |
| 651 |
*entry_size_out = local_length_size; |
| 652 |
|
| 653 |
/* Version is 5. */ |
| 654 |
*version_out = version; |
| 655 |
|
| 656 |
/* This is supposed to be zero. */ |
| 657 |
*padding_out = padding; |
| 658 |
|
| 659 |
/* How many entry_size entries are in the array. */ |
| 660 |
*table_value_count_out = entrycount; |
| 661 |
return DW_DLV_OK; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
/* Meant to be called after all tables accessed using |
| 666 |
dwarf_next_str_offsets_table(). */ |
| 667 |
int |
| 668 |
dwarf_str_offsets_statistics(Dwarf_Str_Offsets_Table table_data, |
| 669 |
Dwarf_Unsigned * wasted_byte_count, |
| 670 |
Dwarf_Unsigned * table_count, |
| 671 |
Dwarf_Error * error) |
| 672 |
{ |
| 673 |
VALIDATE_SOT(table_data) |
| 674 |
if (wasted_byte_count) { |
| 675 |
*wasted_byte_count = table_data->so_wasted_section_bytes; |
| 676 |
} |
| 677 |
if (table_count) { |
| 678 |
*table_count = table_data->so_table_count; |
| 679 |
} |
| 680 |
return DW_DLV_OK; |
| 681 |
} |