Debian Codegraph @

src/lib/libdwarf/dwarf_util.c

raw
1 /*
2 Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2007-2022 David Anderson. All Rights Reserved.
4 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
5 Portions Copyright 2020 Google All rights reserved.
6
7 This program is free software; you can redistribute it
8 and/or modify it under the terms of version 2.1 of the
9 GNU Lesser General Public License as published by the Free
10 Software Foundation.
11
12 This program is distributed in the hope that it would be
13 useful, but WITHOUT ANY WARRANTY; without even the implied
14 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE.
16
17 Further, this software is distributed without any warranty
18 that it is free of the rightful claim of any third person
19 regarding infringement or the like. Any license provided
20 herein, whether implied or otherwise, applies only to this
21 software file. Patent licenses, if any, provided herein
22 do not apply to combinations of this program with other
23 software, or any other product whatsoever.
24
25 You should have received a copy of the GNU Lesser General
26 Public License along with this program; if not, write the
27 Free Software Foundation, Inc., 51 Franklin Street - Fifth
28 Floor, Boston MA 02110-1301, USA.
29
30 */
31
32 #include <config.h>
33
34 #include <stddef.h> /* NULL size_t */
35 #include <stdlib.h> /* free() */
36 #include <string.h> /* memset() strlen() */
37 #include <stdio.h> /* for debugging */
38
39 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
40 #include "stdafx.h"
41 #endif /* HAVE_STDAFX_H */
42
43 #include "dwarf.h"
44 #include "libdwarf.h"
45 #include "libdwarf_private.h"
46 #include "dwarf_base_types.h"
47 #include "dwarf_opaque.h"
48 #include "dwarf_alloc.h"
49 #include "dwarf_error.h"
50 #include "dwarf_util.h"
51 #include "dwarf_abbrev.h"
52 #include "dwarf_memcpy_swap.h"
53 #include "dwarf_die_deliv.h"
54 #include "dwarf_string.h"
55
56 #define MINBUFLEN 1000
57
58 #define MORE_BYTES 0x80
59 #define DATA_MASK 0x7f
60 #define DIGIT_WIDTH 7
61 #define SIGN_BIT 0x40
62
63 /* The function returned allows dwarfdump and other callers to
64 do an endian-sensitive copy-word with a chosen
65 source-length. */
66 typedef void (*endian_funcp_type)(void *, const void *,unsigned long);
67
68 const char *
69 dwarf_package_version(void)
70 {
71 return PACKAGE_VERSION;
72 }
73 #ifdef DEBUG_PRIMARY_DBG
74 /* These functions are helpers in printing data while
75 debugging problems.
76 In normal use these are not compiled or used.
77 Created November 2024. */
78
79 const char *
80 _dwarf_basename(const char *full)
81 {
82 const char *cp = full;
83 unsigned slashat = 0;
84 unsigned charnum = 0;
85
86 if (!cp) {
87 return "null-filepath";
88 }
89 for ( ; *cp; ++cp,++charnum) {
90 if (*cp == '/') {
91 slashat=charnum;
92 }
93 }
94 if (slashat) {
95 ++slashat; /* skip showing /(slash) */
96 }
97 return (full+slashat);
98 }
99 void
100 _dwarf_print_is_primary(const char *msg,
101 Dwarf_Debug p,
102 int line,
103 const char *filepath)
104 {
105 const char *basen = 0;
106 basen = _dwarf_basename(filepath);
107 if (DBG_IS_SECONDARY(p)) {
108 printf("%s SECONDARY dbg 0x%lx line %d %s\n",msg,
109 (unsigned long)p,
110 line,
111 basen);
112 fflush(stdout);
113 return;
114 }
115 if (DBG_IS_PRIMARY(p)) {
116 printf("%s PRIMARY dbg 0x%lx line %d %s\n",msg,
117 (unsigned long)p,
118 line,
119 basen);
120 fflush(stdout);
121 return;
122 }
123 printf("%s Error in primary/secondary. %s. "
124 "Unknown dbg 0x%lx line %d %s\n",
125 msg,p?"":"null dbg passed in",
126 (unsigned long)p,line,basen);
127 fflush(stdout);
128 }
129 void
130 _dwarf_dump_prim_sec(const char *msg,Dwarf_Debug p, int line,
131 const char *filepath)
132 {
133 const char *basen = 0;
134 basen = _dwarf_basename(filepath);
135 printf("%s Print Primary/Secondary data from line %d %s",
136 msg,line,basen);
137 _dwarf_print_is_primary(msg,p,line,filepath);
138 printf(" dbg.............: 0x%lx\n",
139 (unsigned long)p);
140 printf(" de_dbg..........: 0x%lx\n",
141 (unsigned long)p->de_dbg);
142 printf(" de_primary_dbg..: 0x%lx\n",
143 (unsigned long)p->de_primary_dbg);
144 printf(" de_secondary_dbg: 0x%lx\n",
145 (unsigned long)p->de_secondary_dbg);
146 printf(" de_errors_dbg ..: 0x%lx\n",
147 (unsigned long)p->de_errors_dbg);
148 printf(" td_tied_object..: 0x%lx\n",
149 (unsigned long)p->de_tied_data.td_tied_object);
150 fflush(stdout);
151 }
152 void
153 _dwarf_dump_optional_fields(const char *msg,
154 Dwarf_CU_Context context,
155 int line,
156 const char *filepath)
157 {
158 const char *basen = 0;
159 Dwarf_Debug dbg = 0;
160
161 basen = _dwarf_basename(filepath);
162 printf("%s Optional Fields line %d %s\n",
163 msg,line,basen);
164 if (!context) {
165 printf(" ERROR: context not passed in \n");
166 fflush(stdout);
167 return;
168 }
169 dbg = context->cc_dbg;
170 _dwarf_print_is_primary(" For Inheritance and more",
171 dbg,line,filepath);
172 printf(" cc_signature_present.......: %d \n",
173 context->cc_signature_present);
174 _dwarf_dumpsig(" signature", &context->cc_signature,line);
175 printf(" cc_low_pc_present..........: %d 0x%lx\n",
176 context->cc_low_pc_present,
177 (unsigned long)context->cc_low_pc);
178 printf(" cc_base_address_present....: %d 0x%lx\n",
179 context->cc_base_address_present,
180 (unsigned long)context->cc_base_address);
181 printf(" cc_dwo_name_present........: %d %s\n",
182 context->cc_dwo_name_present,
183 context->cc_dwo_name?context->cc_dwo_name:
184 "no-dwo-name-");
185 /* useful? */
186 printf(" cc_at_strx_present.........: %d\n",
187 context->cc_at_strx_present);
188 /* useful? */
189 printf(" cc_cu_die_offset_present...: %d \n",
190 context->cc_cu_die_offset_present);
191 printf(" cc_at_ranges_offset_present: %d 0x%lx\n",
192 context->cc_at_ranges_offset_present,
193 (unsigned long)context->cc_at_ranges_offset);
194 printf(" cc_addr_base_offset_present: %d 0x%lx\n",
195 context->cc_addr_base_offset_present,
196 (unsigned long)context->cc_addr_base_offset);
197 printf(" cc_line_base_present.......: %d 0x%lx\n",
198 context->cc_line_base_present,
199 (unsigned long)context->cc_line_base);
200 printf(" cc_loclists_base_present...: %d 0x%lx\n",
201 context->cc_loclists_base_present,
202 (unsigned long)context->cc_loclists_base);
203 /* useful? */
204 printf(" cc_loclists_header_length_present: %d\n",
205 context->cc_loclists_header_length_present);
206 printf(" cc_str_offsets_array_offset_present: %d 0x%lx\n",
207 context->cc_str_offsets_array_offset_present,
208 (unsigned long)context->cc_str_offsets_array_offset);
209 /* useful? */
210 printf(" cc_str_offsets_tab_present.: %d \n",
211 context->cc_str_offsets_tab_present);
212 printf(" cc_macro_base_present......: %d 0x%lx\n",
213 context->cc_macro_base_present,
214 (unsigned long)context->cc_macro_base_present);
215 /* useful? */
216 printf(" cc_macro_header_length_present: %d\n",
217 context->cc_macro_header_length_present);
218 printf(" cc_ranges_base_present.....: %d 0x%lx\n",
219 context->cc_ranges_base_present,
220 (unsigned long)context->cc_ranges_base);
221 printf(" cc_rnglists_base_present...: %d 0x%lx\n",
222 context->cc_rnglists_base_present,
223 (unsigned long)context->cc_rnglists_base);
224 /* useful? */
225 printf(" cc_rnglists_header_length_present: %d\n",
226 context->cc_rnglists_header_length_present);
227 fflush(stdout);
228 }
229
230 #endif /* DEBUG_PRIMARY_DBG */
231
232 #if 0 /* dump_bytes */
233 static void
234 dump_bytes(char * msg,Dwarf_Small * start, long len)
235 {
236 Dwarf_Small *end = start + len;
237 Dwarf_Small *cur = start;
238
239 printf("%s ",msg);
240 for (; cur < end; cur++) {
241 printf("%02x ", *cur);
242 }
243 printf("\n");
244 }
245 #endif /*0*/
246 #if 0 /* dump_ab_list */
247 static void
248 dump_ab_list(const char *prefix,const char *msg,
249 unsigned long hash_num,
250 Dwarf_Abbrev_List entry_base, int line)
251 {
252 Dwarf_Abbrev_List listent = 0;
253 Dwarf_Abbrev_List nextlistent = 0;
254 printf("%s abb dump %s hashnum %lu line %d\n",prefix,msg,
255 hash_num,line);
256
257 listent = entry_base;
258 for ( ; listent; listent = nextlistent) {
259 printf("%s ptr %p code %lu ",
260 prefix,
261 (void *)listent,
262 (unsigned long)listent->abl_code);
263 printf(" tag %u has child %u \n",
264 listent->abl_tag,listent->abl_has_child);
265 printf("%s abbrev count %lu impl-ct %lu \n",
266 prefix,
267 (unsigned long)listent->abl_abbrev_count,
268 (unsigned long)listent->abl_implicit_const_count);
269 nextlistent = listent->abl_next;
270 printf("%s next %p \n",prefix,(void *)nextlistent);
271 }
272 }
273 static void dump_hash_table(char *msg,
274 Dwarf_Hash_Table tab, int line)
275 {
276 static char buf[200];
277 unsigned long i = 0;
278 printf("debugging: hash tab: %s ptr %p line %d\n",
279 msg,(void *)tab,line);
280 printf(" entryct: %lu abbrevct: %lu highestused: %lu\n",
281 tab->tb_table_entry_count,
282 tab->tb_total_abbrev_count,
283 tab->tb_highest_used_entry);
284
285 for (i = 0; i < tab->tb_table_entry_count; ++i) {
286 sprintf(buf,sizeof(buf),
287 "Tab entry %lu:",i); /* Only for debug */
288 dump_ab_list(" ",buf,i,tab->tb_entries[i],__LINE__);
289 }
290 printf(" ---end hash tab---\n");
291 }
292 #endif
293
294 endian_funcp_type
295 dwarf_get_endian_copy_function(Dwarf_Debug dbg)
296 {
297 if (dbg) {
298 return dbg->de_copy_word;
299 }
300 return 0;
301 }
302
303 Dwarf_Bool
304 _dwarf_file_has_debug_fission_cu_index(Dwarf_Debug dbg)
305 {
306 if (IS_INVALID_DBG(dbg)) {
307 return FALSE;
308 }
309 if (dbg->de_cu_hashindex_data) {
310 return TRUE;
311 }
312 return FALSE;
313 }
314 Dwarf_Bool
315 _dwarf_file_has_debug_fission_tu_index(Dwarf_Debug dbg)
316 {
317 if (IS_INVALID_DBG(dbg)) {
318 return FALSE;
319 }
320 if (dbg->de_tu_hashindex_data ) {
321 return TRUE;
322 }
323 return FALSE;
324 }
325
326 Dwarf_Bool
327 _dwarf_file_has_debug_fission_index(Dwarf_Debug dbg)
328 {
329 if (IS_INVALID_DBG(dbg)) {
330 return FALSE;
331 }
332 if (dbg->de_cu_hashindex_data ||
333 dbg->de_tu_hashindex_data) {
334 return 1;
335 }
336 return FALSE;
337 }
338
339 void
340 _dwarf_create_area_len_error(Dwarf_Debug dbg, Dwarf_Error *error,
341 Dwarf_Unsigned targ,
342 Dwarf_Unsigned sectionlen)
343 {
344 dwarfstring m;
345
346 dwarfstring_constructor(&m);
347 dwarfstring_append_printf_u(&m,
348 "DW_DLE_HEADER_LEN_BIGGER_THAN_SECSIZE: "
349 " The header length of 0x%x is larger",
350 targ);
351 dwarfstring_append_printf_u(&m," than the "
352 "section length of 0x%x.",sectionlen);
353 _dwarf_error_string(dbg,
354 error,DW_DLE_HEADER_LEN_BIGGER_THAN_SECSIZE,
355 dwarfstring_string(&m));
356 dwarfstring_destructor(&m);
357 }
358
359 int
360 _dwarf_internal_get_die_comp_dir(Dwarf_Die die,
361 const char **compdir_out,
362 const char **compname_out,
363 Dwarf_Error *error)
364 {
365 Dwarf_Attribute comp_dir_attr = 0;
366 Dwarf_Attribute comp_name_attr = 0;
367 int resattr = 0;
368 Dwarf_Debug dbg = 0;
369
370 dbg = die->di_cu_context->cc_dbg;
371 resattr = dwarf_attr(die, DW_AT_name, &comp_name_attr, error);
372 if (resattr == DW_DLV_ERROR) {
373 return resattr;
374 }
375 if (resattr == DW_DLV_OK) {
376 int cres = DW_DLV_ERROR;
377 char *name = 0;
378
379 cres = dwarf_formstring(comp_name_attr, &name, error);
380 if (cres == DW_DLV_ERROR) {
381 dwarf_dealloc(dbg, comp_name_attr, DW_DLA_ATTR);
382 return cres;
383 } else if (cres == DW_DLV_OK) {
384 *compname_out = (const char *)name;
385 } else {
386 /* FALL thru */
387 }
388 }
389 if (resattr == DW_DLV_OK) {
390 dwarf_dealloc(dbg, comp_name_attr, DW_DLA_ATTR);
391 }
392 resattr = dwarf_attr(die, DW_AT_comp_dir, &comp_dir_attr, error);
393 if (resattr == DW_DLV_ERROR) {
394 return resattr;
395 }
396 if (resattr == DW_DLV_OK) {
397 int cres = DW_DLV_ERROR;
398 char *cdir = 0;
399
400 cres = dwarf_formstring(comp_dir_attr, &cdir, error);
401 if (cres == DW_DLV_ERROR) {
402 dwarf_dealloc(dbg, comp_dir_attr, DW_DLA_ATTR);
403 return cres;
404 } else if (cres == DW_DLV_OK) {
405 *compdir_out = (const char *) cdir;
406 } else {
407 /* FALL thru */
408 }
409 }
410 if (resattr == DW_DLV_OK) {
411 dwarf_dealloc(dbg, comp_dir_attr, DW_DLA_ATTR);
412 }
413 return resattr;
414 }
415
416 /* Given a form, and a pointer to the bytes encoding
417 a value of that form, val_ptr, this function returns
418 the length, in bytes, of a value of that form.
419 When using this function, check for a return of 0
420 a recursive DW_FORM_INDIRECT value. */
421 int
422 _dwarf_get_size_of_val(Dwarf_Debug dbg,
423 Dwarf_Unsigned form,
424 Dwarf_Half cu_version,
425 Dwarf_Half address_size,
426 Dwarf_Small * val_ptr,
427 int v_length_size,
428 Dwarf_Unsigned *size_out,
429 Dwarf_Small *section_end_ptr,
430 Dwarf_Error*error)
431 {
432 Dwarf_Unsigned length = 0;
433 Dwarf_Unsigned leb128_length = 0;
434 Dwarf_Unsigned form_indirect = 0;
435 Dwarf_Unsigned ret_value = 0;
436
437 switch (form) {
438 case 0: return DW_DLV_OK;
439
440 case DW_FORM_GNU_ref_alt:
441 case DW_FORM_GNU_strp_alt:
442 case DW_FORM_strp_sup:
443 *size_out = v_length_size;
444 return DW_DLV_OK;
445
446 case DW_FORM_addr:
447 if (address_size) {
448 *size_out = address_size;
449 } else {
450 /* This should never happen,
451 address_size should be set. */
452 *size_out = dbg->de_pointer_size;
453 }
454 return DW_DLV_OK;
455 case DW_FORM_ref_sig8:
456 *size_out = 8;
457 /* sizeof Dwarf_Sig8 */
458 return DW_DLV_OK;
459
460 /* DWARF2 was wrong on the size of the attribute for
461 DW_FORM_ref_addr. We assume compilers are using the
462 corrected DWARF3 text (for 32bit pointer target
463 objects pointer and
464 offsets are the same size anyway).
465 It is clear (as of 2014) that for 64bit folks used
466 the V2 spec in the way V2 was
467 written, so the ref_addr has to account for that.*/
468 case DW_FORM_ref_addr:
469 if (cu_version == DW_CU_VERSION2) {
470 *size_out = address_size;
471 } else {
472 *size_out = v_length_size;
473 }
474 return DW_DLV_OK;
475
476 case DW_FORM_block1: {
477 Dwarf_Unsigned space_left = 0;
478
479 if (val_ptr >= section_end_ptr) {
480 _dwarf_error_string(dbg,error,
481 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
482 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: DW_FORM_block1"
483 " itself is off the end of the section."
484 " Corrupt Dwarf.");
485 return DW_DLV_ERROR;
486 }
487 ret_value = *(Dwarf_Small *) val_ptr;
488 /* ptrdiff_t is generated but not named */
489 space_left = (section_end_ptr >= val_ptr)?
490 (section_end_ptr - val_ptr):0;
491 if (ret_value > space_left) {
492 _dwarf_error_string(dbg,error,
493 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
494 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: DW_FORM_block1"
495 " runs off the end of the section."
496 " Corrupt Dwarf.");
497 return DW_DLV_ERROR;
498 }
499 *size_out = ret_value +1;
500 }
501 return DW_DLV_OK;
502
503 case DW_FORM_block2: {
504 Dwarf_Unsigned space_left = 0;
505
506 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
507 val_ptr, DWARF_HALF_SIZE,error,section_end_ptr);
508 /* ptrdiff_t is generated but not named */
509 space_left = (section_end_ptr >= val_ptr)?
510 (section_end_ptr - val_ptr):0;
511 if (ret_value > space_left) {
512 _dwarf_error_string(dbg,error,
513 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
514 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: DW_FORM_block2"
515 " runs off the end of the section."
516 " Corrupt Dwarf.");
517 return DW_DLV_ERROR;
518 }
519 *size_out = ret_value + DWARF_HALF_SIZE;
520 }
521 return DW_DLV_OK;
522
523 case DW_FORM_block4: {
524 Dwarf_Unsigned space_left = 0;
525
526 READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
527 val_ptr, DWARF_32BIT_SIZE,
528 error,section_end_ptr);
529 /* ptrdiff_t is generated but not named */
530 space_left = (section_end_ptr >= val_ptr)?
531 (section_end_ptr - val_ptr):0;
532 if (ret_value > space_left) {
533 _dwarf_error_string(dbg,error,
534 DW_DLE_FORM_BLOCK_LENGTH_ERROR,
535 "DW_DLE_FORM_BLOCK_LENGTH_ERROR: DW_FORM_block4"
536 " runs off the end of the section."
537 " Corrupt Dwarf.");
538 return DW_DLV_ERROR;
539 }
540 *size_out = ret_value + DWARF_32BIT_SIZE;
541 }
542 return DW_DLV_OK;
543
544 case DW_FORM_data1:
545 *size_out = 1;
546 return DW_DLV_OK;
547
548 case DW_FORM_data2:
549 *size_out = 2;
550 return DW_DLV_OK;
551
552 case DW_FORM_data4:
553 *size_out = 4;
554 return DW_DLV_OK;
555
556 case DW_FORM_data8:
557 *size_out = 8;
558 return DW_DLV_OK;
559
560 case DW_FORM_data16:
561 *size_out = 16;
562 return DW_DLV_OK;
563
564 case DW_FORM_string: {
565 int res = 0;
566 res = _dwarf_check_string_valid(dbg,val_ptr,
567 val_ptr,
568 section_end_ptr,
569 DW_DLE_FORM_STRING_BAD_STRING,
570 error);
571 if ( res != DW_DLV_OK) {
572 return res;
573 }
574 }
575 *size_out = strlen((char *) val_ptr) + 1;
576 return DW_DLV_OK;
577
578 case DW_FORM_block:
579 case DW_FORM_exprloc: {
580 DECODE_LEB128_UWORD_LEN_CK(val_ptr,length,leb128_length,
581 dbg,error,section_end_ptr);
582 *size_out = length + leb128_length;
583 return DW_DLV_OK;;
584 }
585
586 case DW_FORM_flag_present:
587 *size_out = 0;
588 return DW_DLV_OK;
589
590 case DW_FORM_flag:
591 *size_out = 1;
592 return DW_DLV_OK;
593
594 case DW_FORM_sec_offset:
595 /* If 32bit dwarf, is 4. Else is 64bit dwarf and is 8. */
596 *size_out = v_length_size;
597 return DW_DLV_OK;
598
599 case DW_FORM_ref_udata: {
600 /* Discard the decoded value, we just want the length
601 of the value. */
602 SKIP_LEB128_LEN_CK(val_ptr,leb128_length,
603 dbg,error,section_end_ptr);
604 *size_out = leb128_length;
605 return DW_DLV_OK;;
606 }
607
608 case DW_FORM_indirect:
609 {
610 Dwarf_Unsigned indir_len = 0;
611 int res = 0;
612 Dwarf_Unsigned info_data_len = 0;
613
614 DECODE_LEB128_UWORD_LEN_CK(val_ptr,form_indirect,
615 indir_len,
616 dbg,error,section_end_ptr);
617 if (form_indirect == DW_FORM_indirect) {
618 /* We are in big trouble: The true form
619 of DW_FORM_indirect is
620 DW_FORM_indirect? Nonsense. Should
621 never happen. */
622 _dwarf_error(dbg,error,
623 DW_DLE_NESTED_FORM_INDIRECT_ERROR);
624 return DW_DLV_ERROR;
625 }
626 /* If form_indirect is DW_FORM_implicit_const then
627 the following call will set info_data_len 0 */
628 res = _dwarf_get_size_of_val(dbg,
629 form_indirect,
630 cu_version,
631 address_size,
632 val_ptr + indir_len,
633 v_length_size,
634 &info_data_len,
635 section_end_ptr,
636 error);
637 if (res != DW_DLV_OK) {
638 return res;
639 }
640 *size_out = indir_len + info_data_len;
641 return DW_DLV_OK;
642 }
643
644 case DW_FORM_ref1:
645 *size_out = 1;
646 return DW_DLV_OK;
647
648 case DW_FORM_ref2:
649 *size_out = 2;
650 return DW_DLV_OK;
651
652 case DW_FORM_ref4:
653 *size_out = 4;
654 return DW_DLV_OK;
655
656 case DW_FORM_ref8:
657 *size_out = 8;
658 return DW_DLV_OK;
659
660 /* DW_FORM_implicit_const is a value in the
661 abbreviations, not in the DIEs and this
662 functions measures DIE size. */
663 case DW_FORM_implicit_const:
664 *size_out = 0;
665 return DW_DLV_OK;
666
667 case DW_FORM_sdata: {
668 /* Discard the decoded value, we just want the length
669 of the value. */
670 SKIP_LEB128_LEN_CK(val_ptr,leb128_length,
671 dbg,error,section_end_ptr);
672 *size_out = leb128_length;
673 return DW_DLV_OK;
674 }
675 case DW_FORM_ref_sup4:
676 *size_out = 4;
677 return DW_DLV_OK;
678 case DW_FORM_ref_sup8:
679 *size_out = 8;
680 return DW_DLV_OK;
681 case DW_FORM_addrx1:
682 *size_out = 1;
683 return DW_DLV_OK;
684 case DW_FORM_addrx2:
685 *size_out = 2;
686 return DW_DLV_OK;
687 case DW_FORM_addrx3:
688 *size_out = 3;
689 return DW_DLV_OK;
690 case DW_FORM_addrx4:
691 *size_out = 4;
692 return DW_DLV_OK;
693 case DW_FORM_strx1:
694 *size_out = 1;
695 return DW_DLV_OK;
696 case DW_FORM_strx2:
697 *size_out = 2;
698 return DW_DLV_OK;
699 case DW_FORM_strx3:
700 *size_out = 3;
701 return DW_DLV_OK;
702 case DW_FORM_strx4:
703 *size_out = 4;
704 return DW_DLV_OK;
705
706 case DW_FORM_loclistx:
707 case DW_FORM_rnglistx:
708 case DW_FORM_addrx:
709 case DW_FORM_GNU_addr_index:
710 case DW_FORM_strx:
711 case DW_FORM_GNU_str_index: {
712 SKIP_LEB128_LEN_CK(val_ptr,leb128_length,
713 dbg,error,section_end_ptr);
714 *size_out = leb128_length;
715 return DW_DLV_OK;
716 }
717
718 case DW_FORM_line_strp:
719 case DW_FORM_strp:
720 *size_out = v_length_size;
721 return DW_DLV_OK;
722
723 case DW_FORM_LLVM_addrx_offset:
724 SKIP_LEB128_LEN_CK(val_ptr,leb128_length,
725 dbg,error,section_end_ptr);
726 *size_out = leb128_length + SIZEOFT32;
727 return DW_DLV_OK;
728 case DW_FORM_udata: {
729 /* Discard the decoded value, we just want the length
730 of the value. */
731 SKIP_LEB128_LEN_CK(val_ptr,leb128_length,
732 dbg,error,section_end_ptr);
733 *size_out = leb128_length;
734 return DW_DLV_OK;
735 }
736 default: break;
737 }
738 /* When we encounter a FORM here that
739 we know about but forgot to enter here,
740 we had better not just continue.
741 Usually means we forgot to update this function
742 when implementing form handling of a new FORM.
743 Disaster results from using a bogus value,
744 so generate error. */
745 {
746 dwarfstring m;
747 dwarfstring_constructor(&m);
748
749 dwarfstring_append_printf_u(&m,
750 "DW_DLE_DEBUG_FORM_HANDLING_INCOMPLETE: "
751 "DW_FORM 0x%x"
752 " is not being handled!",
753 form);
754
755 _dwarf_error_string(dbg,error,
756 DW_DLE_DEBUG_FORM_HANDLING_INCOMPLETE,
757 dwarfstring_string(&m));
758 dwarfstring_destructor(&m);
759 }
760 return DW_DLV_ERROR;
761 }
762
763 /* Table size is always a power of two so
764 we can use "val2 = val1 & (size-1)"
765 instead of a slower 'hash' function. */
766 #define HT_DEFAULT_TABLE_SIZE 128
767 #define HT_MULTIPLE 2
768 #define HT_MOD_OP &
769
770 /* Copy the old entries, updating each to be in
771 a new list. Don't delete anything. Leave the
772 htin with stale data. */
773 static void
774 copy_abbrev_table_to_new_table(Dwarf_Hash_Table htin,
775 Dwarf_Hash_Table htout)
776 {
777 Dwarf_Abbrev_List *entry_in = htin->tb_entries;
778 unsigned long entry_in_count = htin->tb_table_entry_count;
779 Dwarf_Abbrev_List *entry_out = htout->tb_entries;
780 unsigned long entry_out_count = htout->tb_table_entry_count;
781 unsigned long k = 0;
782
783 for (k = 0; k < entry_in_count; ++k) {
784 Dwarf_Abbrev_List listent = entry_in[k];
785 Dwarf_Abbrev_List nextlistent = 0;
786 for (; listent ; listent = nextlistent) {
787 Dwarf_Unsigned newtmp = listent->abl_code;
788 Dwarf_Unsigned newhash = newtmp HT_MOD_OP
789 (entry_out_count -1);
790
791 nextlistent = listent->abl_next;
792 /* Move_entry_to_new_hash. This reverses the
793 order of the entries, effectively, but
794 that does not seem significant. */
795 if (newhash > htout->tb_highest_used_entry) {
796 htout->tb_highest_used_entry =
797 (unsigned long)newhash;
798 }
799 listent->abl_next = entry_out[newhash];
800 entry_out[newhash] = listent;
801 htout->tb_total_abbrev_count++;
802 }
803 }
804 }
805
806 /* We allow zero form here, end of list. */
807 int
808 _dwarf_valid_form_we_know(Dwarf_Unsigned at_form,
809 Dwarf_Unsigned at_name)
810 {
811 if (at_form == 0 && at_name == 0) {
812 return TRUE;
813 }
814 if (at_name == 0) {
815 return FALSE;
816 }
817 if (at_form <= DW_FORM_addrx4 ) {
818 return TRUE;
819 }
820 if (at_form == DW_FORM_GNU_addr_index ||
821 at_form == DW_FORM_GNU_str_index ||
822 at_form == DW_FORM_GNU_ref_alt ||
823 at_form == DW_FORM_GNU_strp_alt) {
824 return TRUE;
825 }
826 return FALSE;
827 }
828
829 int
830 _dwarf_format_TAG_err_msg(Dwarf_Debug dbg,
831 Dwarf_Unsigned tag,
832 const char *m,
833 Dwarf_Error *errp)
834 {
835 dwarfstring v;
836
837 dwarfstring_constructor(&v);
838 dwarfstring_append(&v,(char *)m);
839 dwarfstring_append(&v," The value ");
840 dwarfstring_append_printf_u(&v,"0x%" DW_PR_DUx
841 " is outside the valid TAG range.",tag);
842 dwarfstring_append(&v," Corrupt DWARF.");
843 _dwarf_error_string(dbg, errp,DW_DLE_TAG_CORRUPT,
844 dwarfstring_string(&v));
845 dwarfstring_destructor(&v);
846 return DW_DLV_ERROR;
847 }
848
849 /* This function returns a pointer to a Dwarf_Abbrev_List_s
850 struct for the abbrev with the given code. It puts the
851 struct on the appropriate hash table. It also adds all
852 the abbrev between the last abbrev added and this one to
853 the hash table. In other words, the .debug_abbrev section
854 is scanned sequentially from the top for an abbrev with
855 the given code. All intervening abbrevs are also put
856 into the hash table.
857
858 This function hashes the given code, and checks the chain
859 at that hash table entry to see if a Dwarf_Abbrev_List_s
860 with the given code exists. If yes, it returns a pointer
861 to that struct. Otherwise, it scans the .debug_abbrev
862 section from the last byte scanned for that CU till either
863 an abbrev with the given code is found, or an abbrev code
864 of 0 is read. It puts Dwarf_Abbrev_List_s entries for all
865 abbrev's read till that point into the hash table. The
866 hash table contains both a head pointer and a tail pointer
867 for each entry.
868
869 While the lists can move and entries can be moved between
870 lists on reallocation, any given Dwarf_Abbrev_list entry
871 never moves once allocated, so the pointer is safe to return.
872
873 See also dwarf_get_abbrev() in dwarf_abbrev.c.
874
875 On returning DW_DLV_NO_ENTRY (as well
876 as DW_DLV_OK) it sets
877 *highest_known_code as a side effect
878 for better error messages by callers.
879
880 Returns DW_DLV_ERROR on error. */
881 int
882 _dwarf_get_abbrev_for_code(Dwarf_CU_Context context,
883 Dwarf_Unsigned code,
884 Dwarf_Abbrev_List *list_out,
885 Dwarf_Unsigned *highest_known_code,
886 Dwarf_Error *error)
887 {
888 Dwarf_Debug dbg = context->cc_dbg;
889 Dwarf_Hash_Table hash_table_base =
890 context->cc_abbrev_hash_table;
891 Dwarf_Abbrev_List *entry_base = 0;
892 Dwarf_Abbrev_List entry_cur = 0;
893 Dwarf_Unsigned hash_num = 0;
894 Dwarf_Unsigned abbrev_code = 0;
895 Dwarf_Unsigned abbrev_tag = 0;
896 Dwarf_Abbrev_List hash_abbrev_entry = 0;
897 Dwarf_Abbrev_List inner_list_entry = 0;
898 Dwarf_Byte_Ptr abbrev_ptr = 0;
899 Dwarf_Byte_Ptr end_abbrev_ptr = 0;
900 Dwarf_Small *abbrev_section_start =
901 dbg->de_debug_abbrev.dss_data;
902 Dwarf_Unsigned hashable_val = 0;
903
904 if (!hash_table_base->tb_entries) {
905 hash_table_base->tb_table_entry_count =
906 HT_DEFAULT_TABLE_SIZE;
907 hash_table_base->tb_total_abbrev_count= 0;
908 #ifdef TESTINGHASHTAB
909 printf("debugging: initial size %u\n",HT_DEFAULT_TABLE_SIZE);
910 #endif
911 hash_table_base->tb_entries =
912 (Dwarf_Abbrev_List *)
913 calloc(hash_table_base->tb_table_entry_count,
914 sizeof(Dwarf_Abbrev_List));
915 if (!hash_table_base->tb_entries) {
916 *highest_known_code =
917 context->cc_highest_known_code;
918 return DW_DLV_NO_ENTRY;
919 }
920 } else if (hash_table_base->tb_total_abbrev_count >
921 (hash_table_base->tb_table_entry_count * HT_MULTIPLE)) {
922 struct Dwarf_Hash_Table_s * newht = 0;
923
924 newht = (Dwarf_Hash_Table) calloc(1,
925 sizeof(struct Dwarf_Hash_Table_s));
926 if (!newht) {
927 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
928 "DW_DLE_ALLOC_FAIL: allocating a "
929 "struct Dwarf_Hash_Table_s");
930 return DW_DLV_ERROR;
931 }
932
933 /* This grows the hash table, likely too much.
934 Since abbrev codes are usually assigned
935 from 1 and increasing by one the hash usually
936 results in no hash collisions whatever,
937 so searching the list of collisions
938 is normally very quick. */
939 newht->tb_table_entry_count =
940 hash_table_base->tb_table_entry_count * HT_MULTIPLE;
941 #ifdef TESTINGHASHTAB
942 printf("debugging: Resize size to %lu\n",
943 (unsigned long)newht->tb_table_entry_count);
944 #endif
945 newht->tb_total_abbrev_count = 0;
946 newht->tb_entries =
947 (Dwarf_Abbrev_List *)
948 calloc(newht->tb_table_entry_count,
949 sizeof(Dwarf_Abbrev_List));
950 if (!newht->tb_entries) {
951 free(newht);
952 *highest_known_code =
953 context->cc_highest_known_code;
954 return DW_DLV_NO_ENTRY;
955 }
956 /* Copy the existing entries to the new table,
957 rehashing each. */
958 copy_abbrev_table_to_new_table(hash_table_base, newht);
959 _dwarf_free_abbrev_hash_table_contents(hash_table_base,
960 TRUE /* keep abbrev content */);
961 /* Now overwrite the existing table pointer
962 the new, newly valid, pointer. */
963 free(context->cc_abbrev_hash_table);
964 context->cc_abbrev_hash_table = newht;
965 hash_table_base = context->cc_abbrev_hash_table;
966 } /* Else is ok as is */
967 /* Now add entry. */
968 if (code > context->cc_highest_known_code) {
969 context->cc_highest_known_code = code;
970 }
971 hashable_val = code;
972 hash_num = hashable_val HT_MOD_OP
973 (hash_table_base->tb_table_entry_count-1);
974 if (hash_num > hash_table_base->tb_highest_used_entry) {
975 hash_table_base->tb_highest_used_entry =
976 (unsigned long)hash_num;
977 }
978 entry_base = hash_table_base->tb_entries;
979 entry_cur = entry_base[hash_num];
980
981 /* Determine if the 'code' is the list of synonyms already. */
982 hash_abbrev_entry = entry_cur;
983 for ( ; hash_abbrev_entry && hash_abbrev_entry->abl_code != code;
984 hash_abbrev_entry = hash_abbrev_entry->abl_next) {}
985 if (hash_abbrev_entry) {
986 /* This returns a pointer to an abbrev
987 list entry, not the list itself. */
988 *highest_known_code =
989 context->cc_highest_known_code;
990 hash_abbrev_entry->abl_reference_count++;
991 *list_out = hash_abbrev_entry;
992 return DW_DLV_OK;
993 }
994
995 if (context->cc_last_abbrev_ptr) {
996 abbrev_ptr = context->cc_last_abbrev_ptr;
997 end_abbrev_ptr = context->cc_last_abbrev_endptr;
998 } else {
999 /* This is ok because cc_abbrev_offset includes DWP
1000 offset if appropriate.
1001 */
1002 end_abbrev_ptr = dbg->de_debug_abbrev.dss_data
1003 + dbg->de_debug_abbrev.dss_size;
1004 abbrev_ptr = dbg->de_debug_abbrev.dss_data
1005 + context->cc_abbrev_offset;
1006
1007 if (context->cc_dwp_offsets.pcu_type) {
1008 /* In a DWP the abbrevs
1009 for this context are known quite precisely. */
1010 Dwarf_Unsigned size = 0;
1011
1012 /* Ignore the offset returned.
1013 Already in cc_abbrev_offset. */
1014 _dwarf_get_dwp_extra_offset(
1015 &context->cc_dwp_offsets,
1016 DW_SECT_ABBREV,&size);
1017 /* ASSERT: size != 0 */
1018 end_abbrev_ptr = abbrev_ptr + size;
1019 }
1020 }
1021
1022 /* End of abbrev's as we are past the end entirely.
1023 This can happen,though it seems wrong.
1024 Or we are at the end of the data block,
1025 which we also take as
1026 meaning done with abbrevs for this CU.
1027 An abbreviations table
1028 is supposed to end with a zero byte.
1029 Not ended by end of data block.
1030 But we are allowing what is possibly a bit
1031 more flexible end policy here. */
1032 if (abbrev_ptr >= end_abbrev_ptr) {
1033 return DW_DLV_NO_ENTRY;
1034 }
1035 /* End of abbrev's for this cu, since abbrev code
1036 is 0. */
1037 if (*abbrev_ptr == 0) {
1038 *highest_known_code =
1039 context->cc_highest_known_code;
1040 return DW_DLV_NO_ENTRY;
1041 }
1042 do {
1043 Dwarf_Unsigned new_hashable_val = 0;
1044 Dwarf_Off abb_goff = 0;
1045 Dwarf_Unsigned atcount = 0;
1046 Dwarf_Unsigned impl_const_count = 0;
1047 Dwarf_Byte_Ptr abbrev_ptr2 = 0;
1048 int res = 0;
1049
1050 abb_goff = abbrev_ptr - abbrev_section_start;
1051 DECODE_LEB128_UWORD_CK(abbrev_ptr, abbrev_code,
1052 dbg,error,end_abbrev_ptr);
1053 DECODE_LEB128_UWORD_CK(abbrev_ptr, abbrev_tag,
1054 dbg,error,end_abbrev_ptr);
1055 if (abbrev_tag > DW_TAG_hi_user) {
1056 return _dwarf_format_TAG_err_msg(dbg,
1057 abbrev_tag,"DW_DLE_TAG_CORRUPT",
1058 error);
1059 }
1060 if (abbrev_ptr >= end_abbrev_ptr) {
1061 _dwarf_error(dbg, error, DW_DLE_ABBREV_OFF_END);
1062 return DW_DLV_ERROR;
1063 }
1064 inner_list_entry = (Dwarf_Abbrev_List)
1065 calloc(1,sizeof(struct Dwarf_Abbrev_List_s));
1066 if (!inner_list_entry) {
1067 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
1068 "DW_DLE_ALLOC_FAIL: Allocating an "
1069 "abbrev list entry");
1070 return DW_DLV_ERROR;
1071 }
1072 new_hashable_val = abbrev_code;
1073 if (abbrev_code > context->cc_highest_known_code) {
1074 context->cc_highest_known_code = abbrev_code;
1075 }
1076 hash_num = new_hashable_val HT_MOD_OP
1077 (hash_table_base->tb_table_entry_count-1);
1078 if (hash_num > hash_table_base->tb_highest_used_entry) {
1079 hash_table_base->tb_highest_used_entry =
1080 (unsigned long)hash_num;
1081 }
1082
1083 hash_table_base->tb_total_abbrev_count++;
1084 inner_list_entry->abl_code = abbrev_code;
1085 inner_list_entry->abl_tag = (Dwarf_Half)abbrev_tag;
1086 inner_list_entry->abl_has_child = *(abbrev_ptr++);
1087 inner_list_entry->abl_abbrev_ptr = abbrev_ptr;
1088 inner_list_entry->abl_goffset = abb_goff;
1089
1090 /* Move_entry_to_new_hash list recording
1091 in cu_context. */
1092 inner_list_entry->abl_next = entry_base[hash_num];
1093 entry_base[hash_num] = inner_list_entry;
1094 /* Cycle thru the abbrev content,
1095 ignoring the content except
1096 to find the end of the content. */
1097 res = _dwarf_count_abbrev_entries(dbg,abbrev_ptr,
1098 end_abbrev_ptr,&atcount,&impl_const_count,
1099 &abbrev_ptr2,error);
1100 if (res != DW_DLV_OK) {
1101 *highest_known_code =
1102 context->cc_highest_known_code;
1103 return res;
1104 }
1105 inner_list_entry->abl_implicit_const_count =
1106 impl_const_count;
1107 abbrev_ptr = abbrev_ptr2;
1108 inner_list_entry->abl_abbrev_count = atcount;
1109 } while ((abbrev_ptr < end_abbrev_ptr) &&
1110 *abbrev_ptr != 0 && abbrev_code != code);
1111
1112 *highest_known_code = context->cc_highest_known_code;
1113 context->cc_last_abbrev_ptr = abbrev_ptr;
1114 context->cc_last_abbrev_endptr = end_abbrev_ptr;
1115 if (abbrev_code == code) {
1116 *list_out = inner_list_entry;
1117 inner_list_entry->abl_reference_count++;
1118 return DW_DLV_OK;
1119 }
1120 /* We cannot find an abbrev_code matching code.
1121 ERROR will be declared eventually.
1122 Might be better to declare
1123 specific errors here? */
1124 return DW_DLV_NO_ENTRY;
1125 }
1126
1127 /*
1128 We check that:
1129 areaptr <= strptr.
1130 a NUL byte (*p) exists at p < end.
1131 and return DW_DLV_ERROR if a check fails.
1132
1133 de_assume_string_in_bounds
1134 */
1135 int
1136 _dwarf_check_string_valid(Dwarf_Debug dbg,void *areaptr,
1137 void *strptr, void *areaendptr,
1138 int suggested_error,
1139 Dwarf_Error*error)
1140 {
1141 Dwarf_Small *start = areaptr;
1142 Dwarf_Small *p = strptr;
1143 Dwarf_Small *end = areaendptr;
1144
1145 if (p < start) {
1146 _dwarf_error(dbg,error,suggested_error);
1147 return DW_DLV_ERROR;
1148 }
1149 if (p >= end) {
1150 _dwarf_error(dbg,error,suggested_error);
1151 return DW_DLV_ERROR;
1152 }
1153 if (dbg->de_assume_string_in_bounds) {
1154 /* This NOT the default. But folks can choose
1155 to live dangerously and just assume strings ok. */
1156 return DW_DLV_OK;
1157 }
1158 while (p < end) {
1159 if (*p == 0) {
1160 return DW_DLV_OK;
1161 }
1162 ++p;
1163 }
1164 _dwarf_error(dbg,error,DW_DLE_STRING_NOT_TERMINATED);
1165 return DW_DLV_ERROR;
1166 }
1167
1168 /* Return non-zero if the start/end are not valid for the
1169 die's section.
1170 If pastend matches the dss_data+dss_size then
1171 pastend is a pointer that cannot be dereferenced.
1172 But we allow it as valid here, it is normal for
1173 a pointer to point one-past-end in
1174 various circumstances (one must
1175 avoid dereferencing it, of course).
1176 Return 0 if valid. Return 1 if invalid. */
1177 int
1178 _dwarf_reference_outside_section(Dwarf_Die die,
1179 Dwarf_Small * startaddr,
1180 Dwarf_Small * pastend)
1181 {
1182 Dwarf_Debug dbg = 0;
1183 Dwarf_CU_Context contxt = 0;
1184 struct Dwarf_Section_s *sec = 0;
1185
1186 contxt = die->di_cu_context;
1187 dbg = contxt->cc_dbg;
1188 if (die->di_is_info) {
1189 sec = &dbg->de_debug_info;
1190 } else {
1191 sec = &dbg->de_debug_types;
1192 }
1193 if (startaddr < sec->dss_data) {
1194 return 1;
1195 }
1196 if (pastend > (sec->dss_data + sec->dss_size)) {
1197 return 1;
1198 }
1199 return 0;
1200 }
1201
1202 /* This calculation used to be sprinkled all over.
1203 Now brought to one place.
1204
1205 We try to accurately compute the size of a cu header
1206 given a known cu header location ( an offset in .debug_info
1207 or debug_types). */
1208 /* ARGSUSED */
1209
1210 int
1211 _dwarf_length_of_cu_header(Dwarf_Debug dbg,
1212 Dwarf_Unsigned offset,
1213 Dwarf_Bool is_info,
1214 Dwarf_Unsigned *area_length_out,
1215 Dwarf_Error *error)
1216 {
1217 int local_length_size = 0;
1218 int local_extension_size = 0;
1219 Dwarf_Half version = 0;
1220 Dwarf_Unsigned length = 0;
1221 Dwarf_Unsigned final_size = 0;
1222 Dwarf_Small *section_start =
1223 is_info? dbg->de_debug_info.dss_data:
1224 dbg->de_debug_types.dss_data;
1225 Dwarf_Small *cuptr = section_start + offset;
1226 Dwarf_Unsigned section_length =
1227 is_info? dbg->de_debug_info.dss_size:
1228 dbg->de_debug_types.dss_size;
1229 Dwarf_Small * section_end_ptr =
1230 section_start + section_length;
1231
1232 READ_AREA_LENGTH_CK(dbg, length, Dwarf_Unsigned,
1233 cuptr, local_length_size, local_extension_size,
1234 error,section_length,section_end_ptr);
1235 if (length > section_length ||
1236 (length+local_length_size + local_extension_size) >
1237 section_length) {
1238 _dwarf_create_area_len_error(dbg, error,
1239 length+local_length_size + local_extension_size,
1240 section_length);
1241 return DW_DLV_ERROR;
1242 }
1243
1244 READ_UNALIGNED_CK(dbg, version, Dwarf_Half,
1245 cuptr, DWARF_HALF_SIZE,error,section_end_ptr);
1246 cuptr += DWARF_HALF_SIZE;
1247 if (version == 5) {
1248 Dwarf_Ubyte unit_type = 0;
1249
1250 READ_UNALIGNED_CK(dbg, unit_type, Dwarf_Ubyte,
1251 cuptr, sizeof(Dwarf_Ubyte),error,section_end_ptr);
1252 switch (unit_type) {
1253 case DW_UT_compile:
1254 case DW_UT_partial:
1255 final_size = local_extension_size +
1256 local_length_size + /* Size of cu length field. */
1257 DWARF_HALF_SIZE + /* Size of version stamp field. */
1258 sizeof(Dwarf_Small)+ /* Size of unit type field. */
1259 sizeof(Dwarf_Small)+ /* Size of address size field. */
1260 local_length_size ; /* Size of abbrev offset field. */
1261 break;
1262 case DW_UT_type:
1263 case DW_UT_split_type:
1264 final_size = local_extension_size +
1265 local_length_size +/*Size of type unit length field.*/
1266 DWARF_HALF_SIZE + /* Size of version stamp field. */
1267 sizeof(Dwarf_Small)+ /*Size of unit type field. */
1268 sizeof(Dwarf_Small)+ /*Size of address size field. */
1269 local_length_size + /*Size of abbrev offset field. */
1270 sizeof(Dwarf_Sig8) + /*Size of type signature field.*/
1271 local_length_size; /* Size of type offset field. */
1272 break;
1273 case DW_UT_skeleton:
1274 case DW_UT_split_compile:
1275 final_size = local_extension_size +
1276 local_length_size + /* Size of unit length field. */
1277 DWARF_HALF_SIZE + /* Size of version stamp field. */
1278 sizeof(Dwarf_Small) + /* Size of unit type field. */
1279 sizeof(Dwarf_Small)+ /* Size of address size field. */
1280 local_length_size + /* Size of abbrev offset field. */
1281 sizeof(Dwarf_Sig8); /* Size of dwo id field. */
1282 break;
1283 default:
1284 _dwarf_error(dbg,error,DW_DLE_UNIT_TYPE_NOT_HANDLED);
1285 return DW_DLV_ERROR;
1286 }
1287 } else if (version == 4) {
1288 final_size = local_extension_size +
1289 local_length_size + /* Size of cu length field. */
1290 DWARF_HALF_SIZE + /* Size of version stamp field. */
1291 local_length_size + /* Size of abbrev offset field. */
1292 sizeof(Dwarf_Small); /* Size of address size field. */
1293 if (!is_info) {
1294 final_size +=
1295 /* type signature size */
1296 sizeof (Dwarf_Sig8) +
1297 /* type offset size */
1298 local_length_size;
1299 }
1300 } else if (version < 4) {
1301 final_size = local_extension_size +
1302 local_length_size +
1303 DWARF_HALF_SIZE +
1304 local_length_size +
1305 sizeof(Dwarf_Small); /* Size of address size field. */
1306 }
1307
1308 *area_length_out = final_size;
1309 return DW_DLV_OK;
1310 }
1311
1312 /* Pretend we know nothing about the CU
1313 and just roughly compute the result. */
1314 Dwarf_Unsigned
1315 _dwarf_length_of_cu_header_simple(Dwarf_Debug dbg,
1316 Dwarf_Bool dinfo)
1317 {
1318 Dwarf_Unsigned finalsize = 0;
1319 finalsize = dbg->de_length_size + /* Size of cu length field. */
1320 DWARF_HALF_SIZE + /* Size of version stamp field. */
1321 dbg->de_length_size + /* Size of abbrev offset field. */
1322 sizeof(Dwarf_Small); /* Size of address size field. */
1323 if (!dinfo) {
1324 finalsize +=
1325 /* type signature size */
1326 sizeof (Dwarf_Sig8) +
1327 /* type offset size */
1328 dbg->de_length_size;
1329 }
1330 return finalsize;
1331 }
1332
1333 /* Now that we delay loading .debug_info, we need to do the
1334 load in more places. So putting the load
1335 code in one place now instead of replicating it in multiple
1336 places. */
1337 int
1338 _dwarf_load_debug_info(Dwarf_Debug dbg, Dwarf_Error * error)
1339 {
1340 int res = DW_DLV_ERROR;
1341 if (dbg->de_debug_info.dss_data) {
1342 return DW_DLV_OK;
1343 }
1344 res = _dwarf_load_section(dbg, &dbg->de_debug_abbrev,error);
1345 if (res != DW_DLV_OK) {
1346 return res;
1347 }
1348 res = _dwarf_load_section(dbg, &dbg->de_debug_info, error);
1349 if (res != DW_DLV_OK) {
1350 return res;
1351 }
1352 /* debug_info won't be meaningful without
1353 .debug_rnglists and .debug_loclists if there
1354 is one or both such sections. */
1355 res = dwarf_load_rnglists(dbg,0,error);
1356 if (res == DW_DLV_ERROR) {
1357 return res;
1358 }
1359 res = dwarf_load_loclists(dbg,0,error);
1360 if (res == DW_DLV_ERROR) {
1361 return res;
1362 }
1363 return DW_DLV_OK;
1364 }
1365 int
1366 _dwarf_load_debug_types(Dwarf_Debug dbg, Dwarf_Error * error)
1367 {
1368 int res = DW_DLV_ERROR;
1369 if (dbg->de_debug_types.dss_data) {
1370 return DW_DLV_OK;
1371 }
1372 res = _dwarf_load_section(dbg, &dbg->de_debug_abbrev,error);
1373 if (res != DW_DLV_OK) {
1374 return res;
1375 }
1376 res = _dwarf_load_section(dbg, &dbg->de_debug_types, error);
1377 return res;
1378 }
1379 void
1380 _dwarf_free_abbrev_hash_table_contents(Dwarf_Hash_Table hash_table,
1381 Dwarf_Bool keep_abbrev_list)
1382 {
1383 #ifdef TESTINGHASHTAB
1384 Dwarf_Unsigned max_refs = 0;
1385 #endif
1386 /* A Hash Table is an array with tb_table_entry_count
1387 struct Dwarf_Hash_Table_Entry_s entries in the array. */
1388 unsigned long hashnum = 0;
1389
1390 if (!hash_table) {
1391 /* Not fully set up yet. There is nothing to do. */
1392 return;
1393 }
1394 if (!hash_table->tb_entries) {
1395 /* Not fully set up yet. There is nothing to do. */
1396 return;
1397 }
1398
1399 if (!keep_abbrev_list) {
1400 for (hashnum=0; hashnum <= hash_table->tb_highest_used_entry;
1401 ++hashnum) {
1402 Dwarf_Abbrev_List nextabbrev = 0;
1403 Dwarf_Abbrev_List abbrev =
1404 hash_table->tb_entries[hashnum];
1405 #ifdef TESTINGHASHTAB
1406 unsigned listcount = 0;
1407 #endif
1408
1409 if (!abbrev) {
1410 continue;
1411 }
1412 for (; abbrev; abbrev = nextabbrev) {
1413 #ifdef TESTINGHASHTAB
1414 if (abbrev->abl_reference_count > max_refs) {
1415 max_refs = abbrev->abl_reference_count;
1416 }
1417 #endif
1418 free(abbrev->abl_attr);
1419 abbrev->abl_attr = 0;
1420 free(abbrev->abl_form);
1421 abbrev->abl_form = 0;
1422 free(abbrev->abl_implicit_const);
1423 abbrev->abl_implicit_const = 0;
1424 nextabbrev = abbrev->abl_next;
1425 abbrev->abl_next = 0;
1426 /* dealloc single list entry */
1427 free(abbrev);
1428 abbrev = 0;
1429 #ifdef TESTINGHASHTAB
1430 ++listcount;
1431 #endif
1432 }
1433 #ifdef TESTINGHASHTAB
1434 printf("debugging: hashnum %lu listcount %u\n",hashnum,listcount);
1435 #endif
1436 }
1437 }
1438 #ifdef TESTINGHASHTAB
1439 printf("debugging: max ref count of any abbrev %lu, \n",
1440 (unsigned long)max_refs);
1441 #endif
1442 /* Frees all the pointers at once: an array. */
1443 free(hash_table->tb_entries);
1444 hash_table->tb_entries = 0;
1445 }
1446
1447 /*
1448 If no die provided the size value returned might be wrong.
1449 If different compilation units have different address sizes
1450 this may not give the correct value in all contexts if the die
1451 pointer is NULL.
1452 If the Elf offset size != address_size
1453 (for example if address_size = 4 but recorded in elf64 object)
1454 this may not give the correct value in all contexts if the die
1455 pointer is NULL.
1456 If the die pointer is non-NULL (in which case it must point to
1457 a valid DIE) this will return the correct size.
1458 */
1459 int
1460 _dwarf_get_address_size(Dwarf_Debug dbg, Dwarf_Die die)
1461 {
1462 Dwarf_CU_Context context = 0;
1463 Dwarf_Half addrsize = 0;
1464 if (!die) {
1465 return dbg->de_pointer_size;
1466 }
1467 context = die->di_cu_context;
1468 addrsize = context->cc_address_size;
1469 return addrsize;
1470 }
1471
1472 struct Dwarf_Printf_Callback_Info_s
1473 dwarf_register_printf_callback( Dwarf_Debug dbg,
1474 struct Dwarf_Printf_Callback_Info_s * newvalues)
1475 {
1476 struct Dwarf_Printf_Callback_Info_s oldval =
1477 dbg->de_printf_callback;
1478
1479 if (!newvalues) {
1480 return oldval;
1481 }
1482 if ( newvalues->dp_buffer_user_provided) {
1483 if ( oldval.dp_buffer_user_provided) {
1484 /* User continues to control the buffer. */
1485 dbg->de_printf_callback = *newvalues;
1486 }else {
1487 /* Switch from our control of buffer to user
1488 control. */
1489 free(oldval.dp_buffer);
1490 oldval.dp_buffer = 0;
1491 dbg->de_printf_callback = *newvalues;
1492 }
1493 } else if (oldval.dp_buffer_user_provided){
1494 /* Switch from user control to our control */
1495 dbg->de_printf_callback = *newvalues;
1496 dbg->de_printf_callback.dp_buffer_len = 0;
1497 dbg->de_printf_callback.dp_buffer= 0;
1498 } else {
1499 /* User does not control the buffer. */
1500 dbg->de_printf_callback = *newvalues;
1501 dbg->de_printf_callback.dp_buffer_len =
1502 oldval.dp_buffer_len;
1503 dbg->de_printf_callback.dp_buffer =
1504 oldval.dp_buffer;
1505 }
1506 return oldval;
1507 }
1508
1509 /* No varargs required */
1510 void
1511 _dwarf_printf(Dwarf_Debug dbg,
1512 const char * data)
1513 {
1514 struct Dwarf_Printf_Callback_Info_s *bufdata =
1515 &dbg->de_printf_callback;
1516
1517 dwarf_printf_callback_function_type func = bufdata->dp_fptr;
1518 if (!func) {
1519 return;
1520 }
1521 func(bufdata->dp_user_pointer,data);
1522 return;
1523 }
1524
1525 static int
1526 inthissection(struct Dwarf_Section_s *sec,Dwarf_Small *ptr)
1527 {
1528 if (!sec->dss_data) {
1529 return FALSE;
1530 }
1531 if (ptr < sec->dss_data ) {
1532 return FALSE;
1533 }
1534 if (ptr >= (sec->dss_data + sec->dss_size) ) {
1535 return FALSE;
1536 }
1537 return TRUE;
1538 }
1539
1540 #define FINDSEC(m_s,m_p,n,st,l,e) \
1541 do { \
1542 if (inthissection((m_s),(m_p))) { \
1543 *(n) = (m_s)->dss_name; \
1544 *(st)= (m_s)->dss_data; \
1545 *(l) = (m_s)->dss_size; \
1546 *(e) = (m_s)->dss_data + (m_s)->dss_size; \
1547 return DW_DLV_OK; \
1548 } \
1549 } while (0)
1550
1551 /* So we can know a section end even when we do not
1552 have the section info apriori It's only
1553 needed for a subset of sections. */
1554 int
1555 _dwarf_what_section_are_we(Dwarf_Debug dbg,
1556 Dwarf_Small * our_pointer,
1557 const char ** section_name_out,
1558 Dwarf_Small ** sec_start_ptr_out,
1559 Dwarf_Unsigned * sec_len_out,
1560 Dwarf_Small ** sec_end_ptr_out)
1561 {
1562 FINDSEC(&dbg->de_debug_info,
1563 our_pointer, section_name_out,
1564 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1565 FINDSEC(&dbg->de_debug_loc,
1566 our_pointer, section_name_out,
1567 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1568 FINDSEC(&dbg->de_debug_loclists,
1569 our_pointer, section_name_out,
1570 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1571 FINDSEC(&dbg->de_debug_rnglists,
1572 our_pointer, section_name_out,
1573 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1574 FINDSEC(&dbg->de_debug_addr,
1575 our_pointer, section_name_out,
1576 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1577 FINDSEC(&dbg->de_debug_line,
1578 our_pointer, section_name_out,
1579 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1580 FINDSEC(&dbg->de_debug_aranges,
1581 our_pointer, section_name_out,
1582 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1583 FINDSEC(&dbg->de_debug_macro,
1584 our_pointer, section_name_out,
1585 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1586 FINDSEC(&dbg->de_debug_ranges,
1587 our_pointer, section_name_out,
1588 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1589 FINDSEC(&dbg->de_debug_str_offsets,
1590 our_pointer, section_name_out,
1591 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1592 FINDSEC(&dbg->de_debug_addr,
1593 our_pointer, section_name_out,
1594 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1595 FINDSEC(&dbg->de_debug_pubtypes,
1596 our_pointer, section_name_out,
1597 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1598 FINDSEC(&dbg->de_debug_gdbindex,
1599 our_pointer, section_name_out,
1600 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1601 FINDSEC(&dbg->de_debug_abbrev,
1602 our_pointer, section_name_out,
1603 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1604 FINDSEC(&dbg->de_debug_cu_index,
1605 our_pointer, section_name_out,
1606 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1607 FINDSEC(&dbg->de_debug_tu_index,
1608 our_pointer, section_name_out,
1609 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1610 FINDSEC(&dbg->de_debug_line_str,
1611 our_pointer, section_name_out,
1612 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1613 FINDSEC(&dbg->de_debug_types,
1614 our_pointer, section_name_out,
1615 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1616 FINDSEC(&dbg->de_debug_sup,
1617 our_pointer, section_name_out,
1618 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1619 FINDSEC(&dbg->de_debug_frame,
1620 our_pointer, section_name_out,
1621 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1622 FINDSEC(&dbg->de_debug_frame_eh_gnu,
1623 our_pointer, section_name_out,
1624 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1625 FINDSEC(&dbg->de_gnu_debuglink,
1626 our_pointer, section_name_out,
1627 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1628 FINDSEC(&dbg->de_note_gnu_buildid,
1629 our_pointer, section_name_out,
1630 sec_start_ptr_out, sec_len_out, sec_end_ptr_out);
1631 return DW_DLV_NO_ENTRY;
1632 }
1633
1634 /* New late April 2020.
1635 All the crucial macros will surely
1636 need to use wrapper code to ensure we do not leak
1637 memory at certain points. */
1638 int
1639 _dwarf_read_unaligned_ck_wrapper(Dwarf_Debug dbg,
1640 Dwarf_Unsigned *out_value,
1641 Dwarf_Small *readfrom,
1642 int readlength,
1643 Dwarf_Small *end_arange,
1644 Dwarf_Error *error)
1645 {
1646 Dwarf_Unsigned val = 0;
1647
1648 READ_UNALIGNED_CK(dbg,val,Dwarf_Unsigned,
1649 readfrom,readlength,error,end_arange);
1650 *out_value = val;
1651 return DW_DLV_OK;
1652 }
1653
1654 int
1655 _dwarf_read_area_length_ck_wrapper(Dwarf_Debug dbg,
1656 Dwarf_Unsigned *out_value,
1657 Dwarf_Small **readfrom,
1658 int *length_size_out,
1659 int *exten_size_out,
1660 Dwarf_Unsigned sectionlength,
1661 Dwarf_Small *endsection,
1662 Dwarf_Error *error)
1663 {
1664 Dwarf_Small *ptr = *readfrom;
1665 Dwarf_Unsigned length = 0;
1666 int length_size = 0;
1667 int exten_size = 0;
1668
1669 /* This verifies the lenght itself can be read,
1670 callers must verify the length is appropriate. */
1671 READ_AREA_LENGTH_CK(dbg,length,Dwarf_Unsigned,
1672 ptr,length_size,exten_size,
1673 error,
1674 sectionlength,endsection);
1675 /* It is up to callers to check the length etc. */
1676 *readfrom = ptr;
1677 *out_value = length;
1678 *length_size_out = length_size;
1679 *exten_size_out = exten_size;
1680 return DW_DLV_OK;
1681 }
1682 /* New March 2020 */
1683 /* We need to increment startptr for the caller
1684 in these wrappers so the caller passes in
1685 wrappers return either DW_DLV_OK or DW_DLV_ERROR.
1686 Never DW_DLV_NO_ENTRY. */
1687 int
1688 _dwarf_leb128_uword_wrapper(Dwarf_Debug dbg,
1689 Dwarf_Small ** startptr,
1690 Dwarf_Small * endptr,
1691 Dwarf_Unsigned *out_value,
1692 Dwarf_Error * error)
1693 {
1694 Dwarf_Unsigned utmp2 = 0;
1695 Dwarf_Small * start = *startptr;
1696 DECODE_LEB128_UWORD_CK(start, utmp2,
1697 dbg,error,endptr);
1698 *out_value = utmp2;
1699 *startptr = start;
1700 return DW_DLV_OK;
1701 }
1702 int
1703 _dwarf_leb128_sword_wrapper(Dwarf_Debug dbg,
1704 Dwarf_Small ** startptr,
1705 Dwarf_Small * endptr,
1706 Dwarf_Signed *out_value,
1707 Dwarf_Error * error)
1708 {
1709 Dwarf_Small * start = *startptr;
1710 Dwarf_Signed stmp2 = 0;
1711 DECODE_LEB128_SWORD_CK(start, stmp2,
1712 dbg,error,endptr);
1713 *out_value = stmp2;
1714 *startptr = start;
1715 return DW_DLV_OK;
1716 }