Debian Codegraph @

src/bin/dwarfdump/print_die.c

raw
1 /*
2 Copyright (C) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
4 Portions Copyright 2009-2018 SN Systems Ltd. All rights reserved.
5 Portions Copyright 2007-2024 David Anderson. All rights reserved.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of version 2 of the GNU General
9 Public License as published by the Free Software Foundation.
10
11 This program is distributed in the hope that it would be
12 useful, but WITHOUT ANY WARRANTY; without even the implied
13 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE.
15
16 Further, this software is distributed without any warranty
17 that it is free of the rightful claim of any third person
18 regarding infringement or the like. Any license provided
19 herein, whether implied or otherwise, applies only to this
20 software file. Patent licenses, if any, provided herein
21 do not apply to combinations of this program with other
22 software, or any other product whatsoever.
23
24 You should have received a copy of the GNU General Public
25 License along with this program; if not, write the Free
26 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
27 Boston MA 02110-1301, USA.
28 */
29
30 /* The address of the Free Software Foundation is
31 Free Software Foundation, Inc., 51 Franklin St, Fifth
32 Floor, Boston, MA 02110-1301, USA. SGI has moved from
33 the Crittenden Lane address. */
34
35 #include <config.h>
36
37 #include <stdlib.h> /* calloc() free() */
38 #include <string.h> /* memcmp() memset() strchr() strcmp()
39 strlen() strncmp() */
40 #include <stdio.h> /* FILE decl for dd_esb.h, printf etc */
41
42 #ifdef HAVE_STDINT_H
43 #include <stdint.h> /* uintptr_t */
44 #endif /* HAVE_STDINT_H */
45
46 #include "dwarf.h"
47 #include "libdwarf.h"
48 #include "libdwarf_private.h"
49 #include "dd_defined_types.h"
50 #include "dd_checkutil.h"
51 #include "dd_glflags.h"
52 #include "dd_globals.h"
53 #include "dd_naming.h"
54 #include "dd_esb.h" /* For flexible string buffer. */
55 #include "dd_esb_using_functions.h"
56 #include "dd_sanitized.h"
57 #include "print_frames.h" /* for print_expression_operations() . */
58 #include "dd_macrocheck.h"
59 #include "dd_helpertree.h"
60 #include "dd_opscounttab.h"
61 #include "dd_tag_common.h"
62 #include "dd_attr_form.h"
63 #include "dd_regex.h"
64 #include "dd_safe_strcpy.h"
65
66 #define VSFBUFSZ 200
67 #define DIE_STACK_SIZE 800 /* A hard limit. */
68 /* OpBranchHead_s gives us nice type-checking
69 in calls. */
70 struct OpBranchEntry_s {
71 Dwarf_Unsigned offset;
72 Dwarf_Unsigned target_offset;
73 Dwarf_Small op;
74 };
75 struct OpBranchHead_s {
76 Dwarf_Half bh_opcount;
77 struct OpBranchEntry_s * bh_ops_array;
78 };
79
80 /* Defaults to all 0, never changes */
81 static const LoHiPc lohipc_zero;
82
83 static int traverse_attribute(Dwarf_Debug dbg,
84 Dwarf_Die die,
85 Dwarf_Off dieprint_cu_goffset,
86 Dwarf_Bool is_info,
87 Dwarf_Half attr, Dwarf_Attribute attr_in,
88 char **srcfiles, Dwarf_Signed srcfiles_cnt,
89 int die_indent_level,
90 Dwarf_Error * err);
91 static int print_die_and_children_internal(Dwarf_Debug dbg,
92 Dwarf_Die in_die_in,
93 Dwarf_Off dieprint_cu_goffset,
94 Dwarf_Bool is_info,
95 char **srcfiles, Dwarf_Signed srcfiles_cnt,
96 Dwarf_Off *sibling_off_array,
97 Dwarf_Unsigned sibling_off_count,
98 Dwarf_Error *);
99 static int print_one_die_section(Dwarf_Debug dbg,
100 Dwarf_Bool is_info,
101 Dwarf_Error *pod_err);
102 static int handle_rnglists(Dwarf_Attribute attrib,
103 Dwarf_Half theform,
104 Dwarf_Unsigned value,
105 Dwarf_Unsigned *rle_offset_out,
106 struct esb_s * esbp,
107 int local_verbose,
108 Dwarf_Error *err);
109 static int _dwarf_print_one_expr_op(Dwarf_Debug dbg,
110 Dwarf_Die die,
111 int die_indent_level,
112 Dwarf_Locdesc_c exprc,
113 Dwarf_Unsigned index,
114 Dwarf_Bool has_skip_or_branch,
115 struct OpBranchHead_s *oparray,
116 int *stackchange,
117 Dwarf_Signed *branchdistance,
118 int *zerostackdepth,
119 struct esb_s *string_out,
120 Dwarf_Error *err);
121
122 static int get_form_values(Dwarf_Attribute attrib,
123 Dwarf_Half * theform, Dwarf_Half * directform,Dwarf_Error *err);
124 static void show_form_itself(int show_form,int verbose,
125 int theform, int directform, struct esb_s * str_out);
126 static int print_attribute(Dwarf_Debug dbg, Dwarf_Die die,
127 Dwarf_Off dieprint_cu_goffset,
128 Dwarf_Half attr,
129 Dwarf_Attribute actual_addr,
130 Dwarf_Bool print_else_name_match,
131 int die_indent_level,
132 char **srcfiles, Dwarf_Signed srcfcnt,
133 LoHiPc *lohipc,
134 Dwarf_Bool *attr_matched,
135 Dwarf_Error *err);
136 static int print_location_list(Dwarf_Debug dbg,
137 Dwarf_Die die,
138 Dwarf_Attribute attr,
139 Dwarf_Bool checking,
140 int die_indent_level,
141 int no_end_newline,
142 struct esb_s *details,Dwarf_Error *);
143
144 static int formxdata_print_value(Dwarf_Debug dbg,
145 Dwarf_Die die,Dwarf_Attribute attrib,
146 Dwarf_Half theform,
147 struct esb_s *esbp, Dwarf_Error * err, Dwarf_Bool hex_format);
148 static void bracket_hex(const char *s1, Dwarf_Unsigned v,
149 const char *s2, struct esb_s * esbp);
150 static void formx_unsigned(Dwarf_Unsigned u, struct esb_s *esbp,
151 Dwarf_Bool hex_format);
152 static void formx_data16(Dwarf_Form_Data16 * u, struct esb_s *esbp,
153 Dwarf_Bool hex_format);
154
155 static void formx_signed(Dwarf_Signed s, struct esb_s *esbp);
156
157 /* pd_dwarf_names_print_on_error is always 1.
158 Used in ellipname(res,val_in,v,"TAG",printonerr);
159 See dd_naming.c */
160 static int pd_dwarf_names_print_on_error = 1;
161 static int die_stack_indent_level = 0;
162 static Dwarf_Bool local_symbols_already_begun = FALSE;
163 static const Dwarf_Sig8 zerosig;
164
165 #if 0 /* debugging only */
166 static void
167 dump_bytes(const char *msg,Dwarf_Small * start, long len)
168 {
169 Dwarf_Small *end = start + len;
170 Dwarf_Small *cur = start;
171 printf("%s (0x%lx) ",msg,(unsigned long)start);
172 for (; cur < end; cur++) {
173 printf("%02x", *cur);
174 }
175 printf("\n");
176 }
177 #endif /* 0 */
178 /* 20 characters */
179 static char * indentspace =
180 " "
181 " ";
182 static int indentspacelen = 20;
183 static void
184 appendn( struct esb_s *m, int len)
185 {
186 int remaining = len;
187 while (remaining > indentspacelen) {
188 esb_append(m,indentspace);
189 remaining -= indentspacelen;
190 }
191 esb_appendn(m,indentspace,remaining);
192 }
193 static void
194 append_indent_prefix(struct esb_s *m,
195 int prespaces,
196 int indent,
197 int postspaces)
198 {
199 if (indent < glflags.gf_max_space_indent) {
200 appendn(m,2*indent + prespaces+postspaces);
201 return;
202 }
203 appendn(m,prespaces);
204 esb_append_printf_i(m,"...%d...",indent);
205 appendn(m,postspaces);
206 }
207
208 static int
209 standard_indent(void)
210 {
211 /* Attribute indent. The global offset printed length
212 is actually 32. With GOFF the length is
213 actually 16. Usually. */
214 int nColumn = glflags.gf_show_global_offsets ? 34 : 18;
215 if (!glflags.gf_display_offsets) {
216 nColumn = 2;
217 }
218 return nColumn;
219 }
220
221 static void
222 print_indent_prefix(int prespaces,int indent,int postspaces)
223 {
224 if (indent < glflags.gf_max_space_indent) {
225 int len = prespaces+postspaces+ 2*indent;
226 printf("%*s",len," ");
227 return;
228 }
229 if (prespaces) {
230 printf("%*s",prespaces," ");
231 }
232 printf("...%d...",indent);
233 if (postspaces) {
234 printf("%*s",postspaces," ");
235 }
236 }
237
238 struct die_stack_data_s {
239 Dwarf_Die die_;
240 /* sibling_die_globaloffset_ is set while processing the DIE.
241 We do not know the sibling global offset
242 when we create the stack entry.
243 If the sibling attribute absent we never know. */
244 Dwarf_Off sibling_die_globaloffset_;
245 /* We may need is_info here too. */
246 Dwarf_Off cu_die_offset_; /* global offset. */
247 Dwarf_Bool already_printed_;
248 };
249 static struct die_stack_data_s empty_stack_entry;
250 static struct die_stack_data_s die_stack[DIE_STACK_SIZE];
251 #define SET_DIE_STACK_ENTRY(i,x,o) \
252 { die_stack[(i)].die_ = (x); \
253 die_stack[(i)].cu_die_offset_ = o; \
254 die_stack[(i)].sibling_die_globaloffset_ = 0; \
255 die_stack[(i)].already_printed_ = FALSE; }
256 #define EMPTY_DIE_STACK_ENTRY(i) \
257 { die_stack[(i)] = empty_stack_entry; }
258 #define SET_DIE_STACK_SIBLING(x) { \
259 die_stack[die_stack_indent_level]. \
260 sibling_die_globaloffset_ = (x); }
261
262 static void
263 report_die_stack_error(Dwarf_Debug dbg, Dwarf_Error *err)
264 {
265 struct esb_s m;
266
267 esb_constructor(&m);
268 esb_append_printf_i(&m,
269 "ERROR: compiled in DIE_STACK_SIZE "
270 "(the depth of the DIE tree in this CU)"
271 " of %d exceeded!"
272 " Likely a circular DIE reference."
273 ,DIE_STACK_SIZE);
274 dwarf_error_creation(dbg,err,
275 esb_get_string(&m));
276 print_error_and_continue(esb_get_string(&m),
277 DW_DLV_OK,*err);
278 esb_destructor(&m);
279 }
280
281 /* Just access the die and print selected fields into
282 the string.
283 In case of error print an error message
284 and return DW_DLV_NO_ENTRY.
285 Initially we just verify the offset is ok. */
286 #define NO_SPECIFIC_TAG 0
287 /* The following two must differ, but value unimportant.
288 We choose each to be a small prime number. */
289 #define NON_ZERO_OFFSET_REQUIRED 3
290 #define ZERO_OFFSET_GENERIC_TYPE 19
291 #define WITHIN_CU TRUE
292 static void
293 check_die_expr_op_basic_data(Dwarf_Debug dbg,Dwarf_Die die,
294 const char * op_name,
295 int indentprespaces,
296 int die_indent_level,
297 int indentpostspaces,
298 int required_tag,
299 int required_offset,
300 int within_cu,
301 Dwarf_Unsigned offset,
302 struct esb_s *string_out)
303 {
304 Dwarf_Error err = 0;
305 Dwarf_Off globaloff = 0;
306 Dwarf_Bool is_info =0;
307 Dwarf_Die other_die = 0;
308 int res = 0;
309 Dwarf_Half tag = 0;
310 const char * required_tag_name = 0;
311 const char * actual_tag_name = 0;
312
313 if (! glflags.gf_do_print_dwarf) {
314 /* We are checking so any errors detected
315 will not print, so do not check.
316 Even though this all seems self-contradictory! */
317 return;
318 }
319 if (within_cu && !offset &&
320 required_offset == ZERO_OFFSET_GENERIC_TYPE ) {
321 /* Means the offset zero represents generic type,
322 not a DIE DW_OP_convert DW_OP_reinterpret */
323 return;
324 }
325 if (!die) {
326 esb_append(string_out," <No DIE, cannot verify die offset>");
327 return;
328 }
329 is_info = dwarf_get_die_infotypes_flag(die);
330 if (within_cu) {
331 /* Our target DIE is in same CU as die argument */
332 /* DW_OP_const_type */
333 Dwarf_Unsigned length = 0;
334
335 res = dwarf_die_CU_offset_range(die,&globaloff,
336 &length,&err);
337 if (res != DW_DLV_OK) {
338 esb_append_printf_s(string_out,
339 " ERROR: %s Cannot access CU DIE global offset ",
340 (char *)op_name);
341 if (res == DW_DLV_ERROR) {
342 esb_append(string_out, dwarf_errmsg(err));
343 dwarf_dealloc_error(dbg,err);
344 err = 0;
345 } else {
346 esb_append(string_out, "DW_DLV_NO_ENTRY ");
347 }
348 glflags.gf_count_major_errors++;
349 return;
350 }
351 /* Offset passed in is off of CU header, not
352 CU DIE. */
353 globaloff += offset;
354 } else {
355 /* DW_OP_implicit_ptr */
356 globaloff = offset;
357 }
358 if ((required_offset == NON_ZERO_OFFSET_REQUIRED)
359 && !globaloff) {
360 esb_append_printf_s(string_out,
361 "ERROR: %s DIE global offset 0, but 0 not allowed ",
362 op_name);
363 glflags.gf_count_major_errors++;
364 return;
365 }
366 if (!globaloff) {
367 return;
368 }
369 res = dwarf_offdie_b(dbg,globaloff,is_info,
370 &other_die,&err);
371 if (res != DW_DLV_OK) {
372 esb_append_printf_s(string_out,
373 "ERROR: %s Cannot access DIE via global offset ",
374 op_name);
375 esb_append_printf_u(string_out,
376 "0x%x ",globaloff);
377 if (res == DW_DLV_ERROR) {
378 esb_append(string_out, dwarf_errmsg(err));
379 esb_append(string_out, " ");
380 dwarf_dealloc_error(dbg,err);
381 err = 0;
382 } else {
383 esb_append(string_out, "DW_DLV_NO_ENTRY ");
384 }
385 if (!within_cu) {
386 esb_append(string_out,"DW_OP_implicit_ptr offset "
387 "might apply to another object file ");
388 }
389 glflags.gf_count_major_errors++;
390 return;
391 }
392 res = dwarf_tag(other_die,&tag,&err);
393 if (res != DW_DLV_OK) {
394 esb_append_printf_s(string_out,
395 "ERROR: %s Cannot access DIE tag ",
396 op_name);
397 esb_append_printf_u(string_out,
398 "0x%x ",globaloff);
399 if (res == DW_DLV_ERROR) {
400 esb_append(string_out, dwarf_errmsg(err));
401 esb_append(string_out, " ");
402 dwarf_dealloc_error(dbg,err);
403 err = 0;
404 } else {
405 esb_append(string_out, "DW_DLV_NO_ENTRY ");
406 }
407 glflags.gf_count_major_errors++;
408 dwarf_dealloc_die(other_die);
409 return;
410 }
411 if (required_tag) {
412 required_tag_name = get_TAG_name(required_tag,FALSE);
413 }
414 actual_tag_name = get_TAG_name(tag,FALSE);
415 if (required_tag && tag != required_tag) {
416 esb_append_printf_s(string_out,
417 "ERROR: %s incorrect target die tag ",
418 op_name);
419 esb_append_printf_s(string_out,
420 " Tag required: %s",required_tag_name);
421 esb_append_printf_s(string_out,
422 " Tag found: %s",actual_tag_name);
423 glflags.gf_count_major_errors++;
424 }
425 if (!glflags.dense && !glflags.gf_expr_ops_joined) {
426 char *diename = 0;
427
428 /* <0x000854ff GOFF=0x00546047> */
429 esb_append(string_out,"\n");
430 append_indent_prefix(string_out,indentprespaces,
431 die_indent_level,indentpostspaces+2);
432 esb_append(string_out," Target Die: ");
433 if (within_cu) {
434 esb_append_printf_u(string_out,
435 "<0x%" DW_PR_XZEROS DW_PR_DUx ,offset);
436 if (glflags.gf_show_global_offsets) {
437 esb_append_printf_u(string_out,
438 " GOFF=0x%" DW_PR_XZEROS DW_PR_DUx,
439 globaloff);
440 }
441 esb_append(string_out,"> ");
442 } else {
443 esb_append_printf_u(string_out,
444 "<GOFF=0x%" DW_PR_XZEROS DW_PR_DUx
445 "> ",globaloff);
446 }
447 esb_append(string_out,actual_tag_name);
448 res = dwarf_diename(other_die,&diename,&err);
449 if ( res == DW_DLV_OK) {
450 esb_append_printf_s(string_out,
451 " name: %s",diename);
452 } else if (res == DW_DLV_ERROR) {
453 esb_append_printf_s(string_out,
454 "ERROR: gets error reading "
455 "DW_AT_diename: %s ",
456 dwarf_errmsg(err));
457 dwarf_dealloc_error(dbg,err);
458 err = 0;
459 } else { /* Else no entry */ }
460 }
461 dwarf_dealloc_die(other_die);
462 }
463
464 /* The first non-zero sibling offset we can find
465 is what we want to return. The lowest sibling
466 offset in the stack. Or 0 if we have none known.
467 */
468 static Dwarf_Off
469 get_die_stack_sibling(void)
470 {
471 int i = die_stack_indent_level;
472 for ( ; i >=0 ; --i)
473 {
474 Dwarf_Off v = die_stack[i].sibling_die_globaloffset_;
475 if (v) {
476 return v;
477 }
478 }
479 return 0;
480 }
481 static void
482 possibly_increase_esb_alloc(struct esb_s *esbp,
483 Dwarf_Unsigned count,
484 Dwarf_Unsigned entrysize)
485 {
486 /* for bytes of text needed per element */
487 Dwarf_Unsigned targetsize = count*entrysize;
488 Dwarf_Unsigned used = esb_string_len(esbp);
489 Dwarf_Unsigned cursize = esb_get_allocated_size(esbp);
490
491 if ((targetsize+used) > cursize) {
492 esb_force_allocation(esbp,targetsize+used);
493 }
494 }
495 static void
496 dealloc_all_srcfiles(Dwarf_Debug dbg,
497 char **srcfiles,
498 Dwarf_Signed srcfiles_cnt)
499 {
500 Dwarf_Signed i = 0;
501
502 if (!srcfiles) {
503 return;
504 }
505 for ( ; i < srcfiles_cnt; ++i) {
506 dwarf_dealloc(dbg,srcfiles[i],DW_DLA_STRING);
507 }
508 dwarf_dealloc(dbg,srcfiles, DW_DLA_LIST);
509 }
510
511 /* Higher stack level numbers must have a smaller sibling
512 offset than lower or else the sibling offsets are wrong.
513 Stack entries with sibling_die_globaloffset_ 0 must be
514 ignored in this, it just means there was no sibling
515 attribute at that level.
516 */
517 static void
518 validate_die_stack_siblings(void)
519 {
520 int i = die_stack_indent_level;
521 Dwarf_Off innersiboffset = 0;
522 for ( ; i >=0 ; --i)
523 {
524 Dwarf_Off v = die_stack[i].sibling_die_globaloffset_;
525 if (v) {
526 innersiboffset = v;
527 break;
528 }
529 }
530 if (!innersiboffset) {
531 /* no sibling values to check. */
532 return;
533 }
534 for (--i ; i >= 0 ; --i)
535 {
536 /* outersiboffset is an outer sibling offset. */
537 Dwarf_Off outersiboffset =
538 die_stack[i].sibling_die_globaloffset_;
539 if (outersiboffset ) {
540 if (outersiboffset < innersiboffset) {
541 char small_buf[ESB_FIXED_ALLOC_SIZE];
542 Dwarf_Error ouerr = 0;
543 /* safe: all values known length. */
544 struct esb_s pm;
545
546 esb_constructor_fixed(&pm,small_buf,
547 sizeof(small_buf));
548 esb_append_printf_u(&pm,
549 "ERROR: Die stack sibling error, "
550 "outer global offset "
551 "0x%" DW_PR_XZEROS DW_PR_DUx,outersiboffset);
552 esb_append_printf_u(&pm,
553 " less than inner global offset "
554 "0x%" DW_PR_XZEROS DW_PR_DUx
555 ", the DIE tree is erroneous.",
556 innersiboffset);
557 esb_append_printf_i(&pm,
558 "Die indent level; %d",i);
559 print_error_and_continue(esb_get_string(&pm),
560 DW_DLV_OK,ouerr);
561 esb_destructor(&pm);
562 return;
563 }
564 /* We only need check one level with an offset
565 at each entry. */
566 break;
567 }
568 }
569 return;
570 }
571
572 static void
573 append_local_prefix(struct esb_s *esbp)
574 {
575 esb_append(esbp,"\n ");
576 }
577 static int
578 print_as_info_or_by_cuname(void)
579 {
580 return (glflags.gf_info_flag || glflags.gf_types_flag
581 || glflags.gf_cu_name_flag);
582 }
583
584 #if 0 /* debugging only */
585 /* Only used for debugging. */
586 static void
587 dump_die_offsets(Dwarf_Debug dbg, Dwarf_Die die,
588 const char *msg)
589 {
590 Dwarf_Error dderr = 0;
591 Dwarf_Off goff = 0;
592 Dwarf_Off loff = 0;
593 Dwarf_Half tag = 0;
594 int res = 0;
595
596 res = dwarf_die_offsets(die,&goff, &loff,&dderr);
597 DROP_ERROR_INSTANCE(dbg,res,dderr);
598 res = dwarf_tag(die, &tag, &dderr);
599 DROP_ERROR_INSTANCE(dbg,res,dderr);
600 printf("debugonly: Die tag 0x%x GOFF 0x%llx Loff 0x%llx %s\n",
601 tag,goff,loff,msg);
602 }
603 #endif
604
605 /* These for sure are not to debug_info or debug_types. */
606 Dwarf_Bool
607 dd_form_refers_local_info(Dwarf_Half form)
608 {
609 switch(form) {
610 case DW_FORM_GNU_ref_alt:
611 case DW_FORM_GNU_strp_alt:
612 case DW_FORM_strp_sup:
613 case DW_FORM_line_strp:
614 /* These do not refer to the current
615 section and cannot be checked
616 as if they did. */
617 return FALSE;
618 default: break;
619 }
620 return TRUE;
621 }
622
623 /* process each compilation unit in .debug_info */
624 int
625 print_infos(Dwarf_Debug dbg,Dwarf_Bool is_info,
626 Dwarf_Error *pi_err)
627 {
628 int nres = 0;
629 nres = print_one_die_section(dbg,is_info,pi_err);
630 return nres;
631 }
632
633 static void
634 print_debug_fission_header(struct Dwarf_Debug_Fission_Per_CU_s *fsd)
635 {
636 const char * fissionsec = ".debug_cu_index";
637 unsigned i = 0;
638 struct esb_s hash_str;
639
640 if (!fsd || !fsd->pcu_type) {
641 /* No fission data. */
642 return;
643 }
644 esb_constructor(&hash_str);
645 printf("\n");
646 if (!strcmp(fsd->pcu_type,"tu")) {
647 fissionsec = ".debug_tu_index";
648 }
649 printf(" %-19s = %s\n","Fission section",fissionsec);
650 printf(" %-19s = 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
651 "Fission index ",
652 fsd->pcu_index);
653 format_sig8_string(&fsd->pcu_hash,&hash_str);
654 printf(" %-19s = %s\n","Fission hash",esb_get_string(&hash_str));
655 /* 0 is always unused. Skip it. */
656 esb_destructor(&hash_str);
657 printf(" %-19s = %s\n","Fission entries",
658 "offset size DW_SECTn");
659 for ( i = 1; i < DW_FISSION_SECT_COUNT; ++i) {
660 const char *nstring = 0;
661 Dwarf_Unsigned off = 0;
662 Dwarf_Unsigned size = fsd->pcu_size[i];
663 int res = 0;
664 if (size == 0) {
665 continue;
666 }
667 res = dwarf_get_SECT_name(i,&nstring);
668 if (res != DW_DLV_OK) {
669 nstring = "Unknown SECT";
670 }
671 off = fsd->pcu_offset[i];
672 printf(" %-19s = 0x%" DW_PR_XZEROS DW_PR_DUx " 0x%"
673 DW_PR_XZEROS DW_PR_DUx " %2d\n",
674 nstring,
675 off,size,i);
676 }
677 }
678
679 static void
680 print_cu_hdr_cudie(Dwarf_Unsigned overall_offset,
681 Dwarf_Unsigned offset )
682 {
683 struct Dwarf_Debug_Fission_Per_CU_s fission_data;
684
685 if (glflags.dense) {
686 printf("\n");
687 return;
688 }
689 memset(&fission_data,0,sizeof(fission_data));
690 printf("\nCOMPILE_UNIT<header overall offset = 0x%"
691 DW_PR_XZEROS DW_PR_DUx ">",
692 (Dwarf_Unsigned)(overall_offset - offset));
693 printf(":\n");
694 }
695
696 static void
697 print_cu_hdr_std(Dwarf_Unsigned cu_header_length,
698 Dwarf_Unsigned abbrev_offset,
699 Dwarf_Half version_stamp,
700 Dwarf_Half address_size,
701 /* offset_size is often called length_size in libdwarf. */
702 Dwarf_Half offset_size,
703 int debug_fission_res,
704 Dwarf_Half cu_type,
705 struct Dwarf_Debug_Fission_Per_CU_s *fsd)
706 {
707 int res = 0;
708 const char *utname = 0;
709
710 res = dwarf_get_UT_name(cu_type,&utname);
711 if (res != DW_DLV_OK) {
712 glflags.gf_count_major_errors++;
713 utname = "ERROR";
714 }
715 if (glflags.dense) {
716 printf("<%s>", "cu_header");
717 printf(" %s<0x%" DW_PR_XZEROS DW_PR_DUx
718 ">", "cu_header_length",
719 cu_header_length);
720 printf(" %s<0x%04x>", "version_stamp",
721 version_stamp);
722 printf(" %s<0x%" DW_PR_XZEROS DW_PR_DUx
723 ">", "abbrev_offset", abbrev_offset);
724 printf(" %s<0x%02x>", "address_size",
725 address_size);
726 printf(" %s<0x%02x>", "offset_size",
727 offset_size);
728 printf(" %s<0x%02x %s>", "cu_type",
729 cu_type,utname);
730 if (debug_fission_res == DW_DLV_OK) {
731 struct esb_s hash_str;
732 unsigned i = 0;
733
734 esb_constructor(&hash_str);
735 format_sig8_string(&fsd->pcu_hash,&hash_str);
736 printf(" %s<0x%" DW_PR_XZEROS DW_PR_DUx ">",
737 "fissionindex",
738 fsd->pcu_index);
739 printf(" %s<%s>", "fissionhash",
740 esb_get_string(&hash_str));
741 esb_destructor(&hash_str);
742 for ( i = 1; i < DW_FISSION_SECT_COUNT; ++i) {
743 const char *nstring = 0;
744 Dwarf_Unsigned off = 0;
745 Dwarf_Unsigned size = fsd->pcu_size[i];
746 int fires = 0;
747
748 if (size == 0) {
749 continue;
750 }
751 fires = dwarf_get_SECT_name(i,&nstring);
752 if (fires != DW_DLV_OK) {
753 nstring = "UnknownDW_SECT";
754 }
755 off = fsd->pcu_offset[i];
756 printf(" %s< 0x%" DW_PR_XZEROS DW_PR_DUx " 0x%"
757 DW_PR_XZEROS DW_PR_DUx ">",
758 nstring,
759 off,size);
760 }
761 }
762 } else {
763 printf("\nCU_HEADER:\n");
764 printf(" %-16s = 0x%" DW_PR_XZEROS DW_PR_DUx
765 " %" DW_PR_DUu
766 "\n", "cu_header_length",
767 cu_header_length,
768 cu_header_length);
769 printf(" %-16s = 0x%04x %u\n", "version_stamp",
770 version_stamp,version_stamp);
771 printf(" %-16s = 0x%" DW_PR_XZEROS DW_PR_DUx
772 " %" DW_PR_DUu
773 "\n", "abbrev_offset",
774 abbrev_offset,
775 abbrev_offset);
776 printf(" %-16s = 0x%02x %u\n", "address_size",
777 address_size,address_size);
778 printf(" %-16s = 0x%02x %u\n", "offset_size",
779 offset_size,offset_size);
780 printf(" %-16s = 0x%02x %s\n", "cu_type",
781 cu_type,utname);
782 if (debug_fission_res == DW_DLV_OK) {
783 print_debug_fission_header(fsd);
784 }
785 }
786 }
787 static void
788 print_cu_hdr_signature(Dwarf_Sig8 *signature,
789 Dwarf_Unsigned typeoffset)
790 {
791 if (glflags.dense) {
792 struct esb_s sig8str;
793
794 esb_constructor(&sig8str);
795 format_sig8_string(signature,&sig8str);
796 printf(" %s<%s>", "signature",esb_get_string(&sig8str));
797 printf(" %s<0x%" DW_PR_XZEROS DW_PR_DUx ">",
798 "typeoffset", typeoffset);
799 esb_destructor(&sig8str);
800 } else {
801 struct esb_s sig8str;
802
803 esb_constructor(&sig8str);
804 format_sig8_string(signature,&sig8str);
805 printf(" %-16s = %s\n", "signature",
806 esb_get_string(&sig8str));
807 printf(" %-16s = 0x%" DW_PR_XZEROS DW_PR_DUx
808 " %" DW_PR_DUu "\n",
809 "typeoffset",
810 typeoffset,typeoffset);
811 esb_destructor(&sig8str);
812 }
813 }
814
815 static int
816 get_macinfo_offset(Dwarf_Die cu_die,
817 Dwarf_Unsigned *offset,
818 Dwarf_Error *macerr)
819 {
820 Dwarf_Attribute attrib= 0;
821 int vres = 0;
822 int ares = 0;
823 Dwarf_Bool is_info = TRUE;
824
825 ares = dwarf_attr(cu_die, DW_AT_macro_info, &attrib, macerr);
826 if (ares == DW_DLV_ERROR) {
827 print_error_and_continue(
828 "ERROR: getting dwarf_attr for DW_AT_macro_info"
829 " failed",
830 ares,*macerr);
831 return ares;
832 }
833 if (ares == DW_DLV_NO_ENTRY) {
834 return ares;
835 }
836 vres = dwarf_global_formref_b(attrib,offset,&is_info,macerr);
837 if (vres == DW_DLV_ERROR) {
838 dwarf_dealloc_attribute(attrib);
839 print_error_and_continue(
840 "ERROR: dwarf_global_formref_b on DW_AT_macro_info failed",
841 vres, *macerr);
842 return vres;
843 }
844 dwarf_dealloc_attribute(attrib);
845 return vres;
846 }
847
848 static void
849 print_die_secname(Dwarf_Debug dbg,int is_info)
850 {
851 if (print_as_info_or_by_cuname() &&
852 glflags.gf_do_print_dwarf) {
853 const char * section_name = 0;
854 struct esb_s truename;
855 char buf[ESB_FIXED_ALLOC_SIZE];
856
857 if (is_info) {
858 section_name = ".debug_info";
859 } else {
860 section_name = ".debug_types";
861 }
862 esb_constructor_fixed(&truename,buf,sizeof(buf));
863 get_true_section_name(dbg,section_name,
864 &truename,TRUE);
865 printf("\n%s\n",sanitized(esb_get_string(&truename)));
866 esb_destructor(&truename);
867 }
868 }
869
870 static Dwarf_Bool
871 empty_signature(const Dwarf_Sig8 *sigp)
872 {
873 if (memcmp(sigp,&zerosig,sizeof(zerosig))) {
874 return FALSE ; /* empty */
875 }
876 return TRUE;
877 }
878
879 static int
880 print_macinfo_for_cu(
881 Dwarf_Debug dbg,
882 Dwarf_Die cu_die2,
883 Dwarf_Error *err)
884 {
885
886 int mres = 0;
887 Dwarf_Unsigned offset = 0;
888
889 mres = get_macinfo_offset(cu_die2,&offset,err);
890 if (mres == DW_DLV_NO_ENTRY) {
891 /* By far the most likely result. */
892 return mres;
893 }else if (mres == DW_DLV_ERROR) {
894 print_error_and_continue(
895 "ERROR: get_macinfo_offset for "
896 "DWARF 2,3,or 4 failed "
897 "on a CU die",
898 mres,*err);
899 return mres;
900 } else {
901 mres = print_macinfo_by_offset(dbg,
902 cu_die2,
903 offset,err);
904 if (mres==DW_DLV_ERROR) {
905 struct esb_s m;
906
907 esb_constructor(&m);
908 esb_append_printf_u(&m,
909 "\nERROR: printing macros for "
910 " a CU at macinfo offset 0x%x "
911 " failed ",offset);
912 print_error_and_continue(esb_get_string(&m),
913 mres,*err);
914 esb_destructor(&m);
915 }
916 }
917 return DW_DLV_OK;
918 }
919
920 #if 0 /* debugging only */
921 static void
922 dump_offset_list(Dwarf_Off *array,
923 Dwarf_Unsigned count,
924 int line)
925 {
926 Dwarf_Unsigned i =0;
927 printf("From line %d: count %lu\n",line,(unsigned long)count);
928 for ( ; i < count; ++i) {
929 printf("%3lu: 0x%08lx\n",(unsigned long)i,
930 (unsigned long)array[i]);
931 }
932 }
933 #endif
934
935 static void
936 dd_setup_die_check_functions(Dwarf_Debug dbg,
937 Dwarf_Die in_die_in,
938 Dwarf_Bool is_info,
939 Dwarf_Off **offset_array,
940 Dwarf_Unsigned *offset_count,
941 Dwarf_Error *err)
942 {
943 Dwarf_Off section_offset = 0;
944 Dwarf_Off local_offset = 0;
945 int res = 0;
946
947 if (!(glflags.gf_check_functions && checking_this_compiler())) {
948 return;
949 }
950 DWARF_CHECK_COUNT(check_functions_result,1);
951 res = dwarf_die_offsets(in_die_in,&section_offset,
952 &local_offset,err);
953 if (res != DW_DLV_OK) {
954 DWARF_CHECK_ERROR(check_functions_result,
955 "Unable to get die offsets");
956 if (res == DW_DLV_ERROR) {
957 DROP_ERROR_INSTANCE(dbg,res,*err);
958 }
959 } else {
960 /* This is the real intent. To check that
961 this works. */
962 res = DW_DLV_OK;
963 res = dwarf_offset_list(dbg,section_offset,is_info,
964 offset_array,offset_count,err);
965 if (res == DW_DLV_ERROR) {
966 DWARF_CHECK_ERROR(check_functions_result,
967 "Unable to get dwarf_offset_list offsets");
968 if (res == DW_DLV_ERROR) {
969 DROP_ERROR_INSTANCE(dbg,res,*err);
970 }
971 } else if (res == DW_DLV_OK) {
972 Dwarf_Unsigned i = 0;
973 Dwarf_Unsigned count = *offset_count;
974 Dwarf_Off *off_array = *offset_array;
975 Dwarf_Off lastoffset = section_offset;
976
977 for ( ; i < count; ++i) {
978 Dwarf_Off off = off_array[i];
979
980 if (off <= lastoffset) {
981 struct esb_s m;
982 char buf[100];
983
984 esb_constructor_fixed(&m,buf,sizeof(buf));
985 esb_append_printf_u(&m,
986 "dwarf_offset_list offset [%u]", i);
987 esb_append_printf_u(&m,
988 " 0x%" DW_PR_XZEROS DW_PR_DUx,
989 off);
990 esb_append_printf_u(&m,
991 "is below the previous of 0x%"
992 DW_PR_XZEROS DW_PR_DUx,
993 lastoffset);
994 DWARF_CHECK_ERROR(check_functions_result,
995 esb_get_string(&m));
996 esb_destructor(&m);
997 /* Skip further checks in this list */
998 break;
999 }
1000 }
1001 }
1002 }
1003 return;
1004 }
1005
1006 /* Called with a CU_Die as in_die_in. */
1007 static int
1008 print_die_and_children(Dwarf_Debug dbg,
1009 Dwarf_Die in_die_in,
1010 Dwarf_Off dieprint_cu_goffset,
1011 Dwarf_Bool is_info,
1012 char **srcfiles, Dwarf_Signed srcfiles_count,
1013 Dwarf_Error *err)
1014 {
1015 int res = 0;
1016 Dwarf_Off *offset_array = 0;
1017 Dwarf_Unsigned offset_count = 0;
1018
1019 /* A CU_die has a single child */
1020 local_symbols_already_begun = FALSE;
1021 res = print_die_and_children_internal(dbg,
1022 in_die_in, dieprint_cu_goffset, is_info,
1023 srcfiles,srcfiles_count,
1024 offset_array,offset_count,err);
1025 return res;
1026 }
1027
1028 static int
1029 print_cu_hdr_abbrev_data(Dwarf_Debug dbg,
1030 Dwarf_Off aboffset,
1031 Dwarf_Error *error)
1032 {
1033 Dwarf_Unsigned initial_abbnum = 1;
1034 Dwarf_Unsigned final_abbnum = 0;
1035 Dwarf_Unsigned length = 0;
1036 int ores = 0;
1037
1038 if (glflags.gf_do_print_dwarf) {
1039 printf("\nAbbreviation table this CU, offset "
1040 "0x%" DW_PR_XZEROS DW_PR_DUx ":\n",
1041 aboffset);
1042 }
1043 ores = print_all_abbrevs_for_cu(dbg,
1044 aboffset,
1045 initial_abbnum,
1046 &length,&final_abbnum,error);
1047 return ores;
1048
1049 }
1050
1051 static void
1052 fill_in_producer_name(Dwarf_Debug dbg,Dwarf_Die cu_die,
1053 Dwarf_Unsigned dieprint_cu_goffset)
1054
1055 {
1056 /* Get producer name for this CU and
1057 update compiler list */
1058 int cures = 0;
1059 struct esb_s producername;
1060 Dwarf_Error pod_err = 0;
1061
1062 esb_constructor(&producername);
1063 /* Fills in some producername no matter
1064 what status returned. */
1065 cures = get_producer_name(dbg,cu_die,
1066 dieprint_cu_goffset,&producername,&pod_err);
1067 if (cures == DW_DLV_OK) {
1068 update_compiler_target(esb_get_string(&producername));
1069 } else if (cures == DW_DLV_ERROR) {
1070 DROP_ERROR_INSTANCE(dbg,cures,pod_err);
1071 }
1072 esb_destructor(&producername);
1073 }
1074
1075 static int
1076 fill_in_compiler_target(Dwarf_Debug dbg,Dwarf_Die cu_die,
1077 Dwarf_Unsigned dieprint_cu_goffset,
1078 Dwarf_Error*pod_err)
1079 {
1080 char * cu_short_name = NULL;
1081 char * cu_long_name = NULL;
1082 int chres = 0;
1083
1084 chres = get_cu_name(dbg,cu_die,
1085 dieprint_cu_goffset,
1086 &cu_short_name,&cu_long_name,
1087 pod_err);
1088 if (chres == DW_DLV_ERROR ) {
1089 return chres;
1090 }
1091 if (chres == DW_DLV_OK) {
1092 /* Add CU name to current compiler entry */
1093 add_cu_name_compiler_target(cu_long_name);
1094 }
1095 return chres;
1096 }
1097
1098 static void
1099 suppress_irrelevant_checking(void)
1100 {
1101 /* In a .dwp file some checks get all sorts
1102 of spurious errors. */
1103 glflags.gf_suppress_checking_on_dwp = TRUE;
1104 glflags.gf_check_ranges = FALSE;
1105 glflags.gf_check_aranges = FALSE;
1106 glflags.gf_check_decl_file = FALSE;
1107 glflags.gf_check_lines = FALSE;
1108 glflags.gf_check_pubname_attr = FALSE;
1109 glflags.gf_check_fdes = FALSE;
1110 }
1111
1112 static void
1113 reset_error_reporting_globals(void)
1114 {
1115 /* We have not seen the compile unit yet, reset these
1116 error-reporting globals. */
1117 glflags.seen_CU = FALSE;
1118 glflags.need_CU_name = TRUE;
1119 glflags.need_CU_base_address = TRUE;
1120 glflags.need_CU_high_address = TRUE;
1121 /* Some prerelease gcc versions used ranges but seemingly
1122 assumed the lack of a base address in the CU was
1123 defined to be a zero base.
1124 Assuming a base address (and low and high) is sensible. */
1125 glflags.CU_base_address = 0;
1126 glflags.CU_high_address = 0;
1127 glflags.CU_low_address = 0;
1128 }
1129
1130 static void
1131 print_cu_header_data_or_signature(
1132 Dwarf_Unsigned cu_header_length,
1133 Dwarf_Unsigned abbrev_offset,
1134 Dwarf_Half version_stamp,
1135 Dwarf_Half address_size,
1136 Dwarf_Half length_size,
1137 int fission_data_result,
1138 Dwarf_Half cu_type,
1139 struct Dwarf_Debug_Fission_Per_CU_s *fission_data,
1140 Dwarf_Sig8 *signature,
1141 Dwarf_Unsigned typeoffset)
1142 {
1143 if (glflags.verbose) {
1144 print_cu_hdr_std(cu_header_length,abbrev_offset,
1145 version_stamp,address_size,length_size,
1146 fission_data_result,cu_type,fission_data);
1147 if (!empty_signature(signature)) {
1148 print_cu_hdr_signature(signature,typeoffset);
1149 }
1150 if (glflags.dense) {
1151 printf("\n");
1152 }
1153 } else {
1154 if (!empty_signature(signature)) {
1155 if (glflags.dense) {
1156 printf("<%s>", "cu_header");
1157 } else {
1158 printf("\nCU_HEADER:\n");
1159 }
1160 print_cu_hdr_signature(signature,typeoffset);
1161 if (glflags.dense) {
1162 printf("\n");
1163 }
1164 }
1165 }
1166 }
1167
1168 /* */
1169 static int
1170 print_one_die_section(Dwarf_Debug dbg,Dwarf_Bool is_info,
1171 Dwarf_Error *pod_err)
1172 {
1173 Dwarf_Unsigned cu_header_length = 0;
1174 Dwarf_Unsigned abbrev_offset = 0;
1175 Dwarf_Half version_stamp = 0;
1176 Dwarf_Half address_size = 0;
1177 Dwarf_Half extension_size = 0;
1178 Dwarf_Half length_size = 0;
1179 Dwarf_Unsigned typeoffset = 0;
1180 Dwarf_Unsigned next_cu_offset = 0;
1181 unsigned loop_count = 0;
1182 int nres = DW_DLV_OK;
1183 int cu_count = 0;
1184 int res = 0;
1185 Dwarf_Off dieprint_cu_goffset = 0;
1186
1187 glflags.current_section_id = is_info?DEBUG_INFO:
1188 DEBUG_TYPES;
1189 {
1190 const char * test_section_name = 0;
1191 res = dwarf_get_die_section_name(dbg,is_info,
1192 &test_section_name,pod_err);
1193 if (res == DW_DLV_NO_ENTRY) {
1194 if (!is_info) {
1195 /* No .debug_types. Do not print
1196 .debug_types name */
1197 return res;
1198 }
1199 }
1200 }
1201 /* Start loop on loop_count (CUs) */
1202 for (;;++loop_count) {
1203 Dwarf_Die cu_die = 0;
1204 Dwarf_Die cu_die2 = 0;
1205 struct Dwarf_Debug_Fission_Per_CU_s fission_data;
1206 int fission_data_result = 0;
1207 Dwarf_Half cu_type = 0;
1208 Dwarf_Sig8 signature;
1209 int offres = 0;
1210
1211 signature = zerosig;
1212 /* glflags.DIE_section_offset: in case
1213 dwarf_next_cu_header_d fails due
1214 to corrupt dwarf. */
1215 glflags.DIE_section_offset = dieprint_cu_goffset;
1216 memset(&fission_data,0,sizeof(fission_data));
1217 #ifdef ORIGINAL_HEADER_API
1218 /* a sample. Other places changed with no ifdef */
1219 nres = dwarf_next_cu_header_d(dbg,
1220 is_info,
1221 &cu_header_length, &version_stamp,
1222 &abbrev_offset, &address_size,
1223 &length_size,&extension_size,
1224 &signature, &typeoffset,
1225 &next_cu_offset,
1226 &cu_type, pod_err);
1227 #else
1228 nres = dwarf_next_cu_header_e(dbg,
1229 is_info,
1230 &cu_die,
1231 &cu_header_length, &version_stamp,
1232 &abbrev_offset, &address_size,
1233 &length_size,&extension_size,
1234 &signature, &typeoffset,
1235 &next_cu_offset,
1236 &cu_type, pod_err);
1237 #endif /* ORIGINAL_HEADER_API */
1238
1239 if (!loop_count) {
1240 /* So compress flags show, we waited till
1241 section loaded to do this. */
1242 print_die_secname(dbg,is_info);
1243 }
1244 if (nres == DW_DLV_NO_ENTRY) {
1245 return nres;
1246 }
1247 if (nres == DW_DLV_ERROR) {
1248 /* With corrupt DWARF due to a bad CU die
1249 we won't know much. */
1250 print_error_and_continue(
1251 "ERROR: Failure reading CU header"
1252 " or DIE, corrupt DWARF", nres, *pod_err);
1253 return nres;
1254 }
1255 if (cu_count >= glflags.break_after_n_units) {
1256 const char *m = "CUs";
1257 if (cu_count == 1) {
1258 m="CU";
1259 }
1260 printf("Break at %d %s\n",cu_count,m);
1261 dwarf_dealloc_die(cu_die);
1262 cu_die = 0;
1263 break;
1264 }
1265 /* Get the CU offset (when we can)
1266 for easy error reporting. Ignore errors. */
1267 offres = dwarf_die_offsets(cu_die,
1268 &glflags.DIE_section_offset,
1269 &glflags.DIE_offset,pod_err);
1270 DROP_ERROR_INSTANCE(dbg,offres,*pod_err);
1271 glflags.DIE_CU_overall_offset = glflags.DIE_section_offset;
1272 glflags.DIE_CU_offset = glflags.DIE_offset;
1273 dieprint_cu_goffset = glflags.DIE_section_offset;
1274
1275 if (glflags.gf_cu_name_flag) {
1276 Dwarf_Bool should_skip = FALSE;
1277
1278 /* always sets should_skip, even if error */
1279 should_skip_this_cu(dbg, &should_skip,cu_die);
1280 if (should_skip) {
1281 dwarf_dealloc_die(cu_die);
1282 cu_die = 0;
1283 ++cu_count;
1284 continue;
1285 }
1286 }
1287 fill_in_producer_name(dbg,cu_die, dieprint_cu_goffset);
1288 /* Once the compiler table has been updated, see
1289 if we need to generate the list of CU compiled
1290 by all the producers contained in the elf file */
1291 if (glflags.gf_producer_children_flag) {
1292 int chres = 0;
1293 chres = fill_in_compiler_target(dbg,cu_die,
1294 dieprint_cu_goffset,pod_err);
1295 if (chres == DW_DLV_ERROR ) {
1296 dwarf_dealloc_die(cu_die);
1297 cu_die = 0;
1298 return chres;
1299 }
1300 }
1301
1302 /* If the current compiler is not requested by the
1303 user, then move to the next CU */
1304 if (!checking_this_compiler()) {
1305 dwarf_dealloc_die(cu_die);
1306 ++cu_count;
1307 cu_die = 0;
1308 continue;
1309 }
1310 fission_data_result = dwarf_get_debugfission_for_die(
1311 cu_die,
1312 &fission_data,pod_err);
1313 if (fission_data_result == DW_DLV_ERROR) {
1314 dwarf_dealloc_die(cu_die);
1315 cu_die = 0;
1316 print_error_and_continue(
1317 "ERROR: Failure looking for Debug Fission data",
1318 fission_data_result, *pod_err);
1319 return fission_data_result;
1320 }
1321 if (fission_data_result == DW_DLV_OK) {
1322 suppress_irrelevant_checking();
1323 }
1324 reset_error_reporting_globals();
1325
1326 if ((glflags.gf_info_flag || glflags.gf_types_flag) &&
1327 glflags.gf_do_print_dwarf) {
1328 print_cu_header_data_or_signature(cu_header_length,
1329 abbrev_offset, version_stamp,address_size,
1330 length_size, fission_data_result,cu_type,
1331 &fission_data, &signature,typeoffset);
1332 }
1333 if (glflags.gf_check_abbreviations ||
1334 (glflags.verbose > 3 &&
1335 (glflags.gf_info_flag || glflags.gf_types_flag) &&
1336 glflags.gf_do_print_dwarf)) {
1337 int hares = 0;
1338 hares = print_cu_hdr_abbrev_data(dbg,
1339 abbrev_offset,
1340 pod_err);
1341 if (hares == DW_DLV_ERROR) {
1342 /* Will be reported later, no doubt. */
1343 DROP_ERROR_INSTANCE(dbg,hares,*pod_err);
1344 *pod_err = 0;
1345 }
1346 }
1347
1348 /* Get abbreviation info for this CU, given
1349 the abbrev offset */
1350 get_abbrev_array_info(dbg,abbrev_offset);
1351
1352 /* Process a single compilation unit in .debug_info or
1353 .debug_types. */
1354 cu_die2 = cu_die;
1355 cu_die = 0;
1356 {
1357 int pres = 0;
1358 Dwarf_Signed srcfiles_cnt = 0;
1359 char **srcfiles = 0;
1360 int srcf = 0;
1361 Dwarf_Error srcerr = 0;
1362
1363 srcf = dwarf_srcfiles(cu_die2,
1364 &srcfiles, &srcfiles_cnt, &srcerr);
1365 if (srcf == DW_DLV_ERROR) {
1366 print_error_and_continue(
1367 "ERROR: dwarf_srcfiles problem ",
1368 srcf,srcerr);
1369 DROP_ERROR_INSTANCE(dbg,srcf,srcerr);
1370 srcfiles = 0;
1371 srcfiles_cnt = 0;
1372 } else if (srcf == DW_DLV_NO_ENTRY) {
1373 /* DW_DLV_NO_ENTRY generally means there
1374 there is no DW_AT_stmt_list attribute.
1375 and we do not want to print anything
1376 about statements in that case */
1377 }
1378 if (print_as_info_or_by_cuname() ||
1379 glflags.gf_search_is_on) {
1380 /* Do regardless if dwarf_srcfiles
1381 was successful to print die
1382 and children as best we can
1383 even with errors . */
1384 int podres2 = 0;
1385 Dwarf_Error lperr = 0;
1386 /* Get the CU offset for easy error reporting */
1387 podres2 = dwarf_die_offsets(cu_die2,
1388 &glflags.DIE_section_offset,
1389 &glflags.DIE_offset,&lperr);
1390 DROP_ERROR_INSTANCE(dbg,podres2,lperr);
1391 glflags.DIE_CU_overall_offset =
1392 glflags.DIE_section_offset;
1393 glflags.DIE_CU_offset = glflags.DIE_offset;
1394 dieprint_cu_goffset = glflags.DIE_section_offset;
1395
1396 pres = print_die_and_children(dbg, cu_die2,
1397 dieprint_cu_goffset,is_info,
1398 srcfiles, srcfiles_cnt,pod_err);
1399 if (pres == DW_DLV_ERROR) {
1400 if (srcfiles) {
1401 dealloc_all_srcfiles(dbg,srcfiles,
1402 srcfiles_cnt);
1403 srcfiles = 0;
1404 srcfiles_cnt = 0;
1405 }
1406 dwarf_dealloc_die(cu_die2);
1407 return pres;
1408 }
1409 }
1410 /* Dump Ranges Information */
1411 if (dump_ranges_info) {
1412 PrintBucketGroup(glflags.pRangesInfo);
1413 }
1414
1415 /* Check the range array if in checl mode */
1416 if ( glflags.gf_check_ranges) {
1417 int rares = 0;
1418 Dwarf_Error raerr = 0;
1419
1420 rares = check_range_array_info(dbg,
1421 cu_die2,&raerr);
1422 if (rares == DW_DLV_ERROR) {
1423 print_error_and_continue(
1424 "ERROR: range array checks for "
1425 "the current CU failed. ",
1426 rares,raerr);
1427 DROP_ERROR_INSTANCE(dbg,rares,raerr);
1428 }
1429 }
1430
1431 /* Traverse the line section if in check mode
1432 or if line-printing requested */
1433 if (glflags.gf_line_flag ||
1434 glflags.gf_check_decl_file) {
1435 int plnres = 0;
1436
1437 int oldsection = glflags.current_section_id;
1438 plnres = print_line_numbers_this_cu(dbg,
1439 cu_die2,
1440 srcfiles,srcfiles_cnt,pod_err);
1441 if (plnres == DW_DLV_ERROR) {
1442 print_error_and_continue(
1443 "ERROR: Printing line numbers for "
1444 "the current CU failed. ",
1445 plnres,*pod_err);
1446 /* Suppress the error so we print
1447 whatever we can */
1448 DROP_ERROR_INSTANCE(dbg,plnres,*pod_err);
1449 }
1450 glflags.current_section_id = oldsection;
1451 }
1452 /* We are not currently checking the macro
1453 import trees for (infinite) loops.
1454 We do not follow the import tree directly
1455 so we will not loop forever here. */
1456 if (glflags.gf_macro_flag ||
1457 glflags.gf_check_macros) {
1458 int mres = 0;
1459 Dwarf_Bool in_import_list = FALSE;
1460 Dwarf_Unsigned import_offset = 0;
1461 int oldsection = glflags.current_section_id;
1462 /* DWARF5 .debug_macro (version 5
1463 in the macro header) or GNU extension
1464 of DWARF4 .debug_macro, with version 4
1465 in the macro header. */
1466
1467 macro_import_stack_cleanout();
1468 mres = print_macros_5style_this_cu(dbg, cu_die2,
1469 srcfiles,srcfiles_cnt,
1470 glflags.gf_do_print_dwarf,
1471 TRUE /* descend_into_imports */,
1472 in_import_list,
1473 import_offset,pod_err);
1474 if (mres == DW_DLV_ERROR) {
1475 print_error_and_continue(
1476 "ERROR: Printing DWARF5 macros "
1477 "for the current CU failed. ",
1478 mres,*pod_err);
1479 /* Suppress the error so we print
1480 whatever we can */
1481 DROP_ERROR_INSTANCE(dbg,mres,*pod_err);
1482 }
1483
1484 macro_import_stack_cleanout();
1485 in_import_list = TRUE;
1486 /* The above would have checked all reachable
1487 macro units, so no need to check here. */
1488 if (!glflags.gf_do_check_dwarf &&
1489 mres == DW_DLV_OK) {
1490 for (;;) {
1491 /* Never returns DW_DLV_ERROR */
1492 mres = get_next_unprinted_macro_offset(
1493 &macro_check_tree, &import_offset);
1494 if (mres != DW_DLV_OK) {
1495 break;
1496 }
1497 macro_import_stack_cleanout();
1498 mres = print_macros_5style_this_cu(dbg,
1499 cu_die2,
1500 srcfiles,srcfiles_cnt,
1501 glflags.gf_do_print_dwarf,
1502 FALSE /* no descend_into_imports */,
1503 in_import_list,
1504 import_offset,
1505 pod_err);
1506 if (mres == DW_DLV_ERROR) {
1507 struct esb_s m;
1508
1509 esb_constructor(&m);
1510 esb_append_printf_u(&m,
1511 "ERROR: Printing DWARF5 macros "
1512 " at offset 0x%x "
1513 "for the current macro import"
1514 " in the macros failed. ",
1515 import_offset);
1516 print_error_and_continue(
1517 esb_get_string(&m),
1518 mres,*pod_err);
1519 DROP_ERROR_INSTANCE(dbg,mres,
1520 *pod_err);
1521 esb_destructor(&m);
1522 break;
1523 }
1524 macro_import_stack_cleanout();
1525 }
1526 }
1527 glflags.current_section_id = oldsection;
1528 }
1529 if ( glflags.gf_macinfo_flag ||
1530 glflags.gf_check_macros) {
1531 int mres = 0;
1532 /* Macros have no version number before
1533 DWARF 5. */
1534
1535 mres = print_macinfo_for_cu(dbg,cu_die2,
1536 pod_err);
1537 if (mres == DW_DLV_ERROR) {
1538 if (srcfiles) {
1539 dealloc_all_srcfiles(dbg,srcfiles,
1540 srcfiles_cnt);
1541 srcfiles = 0;
1542 srcfiles_cnt = 0;
1543 }
1544 dwarf_dealloc_die(cu_die2);
1545 return mres;
1546 }
1547 }
1548 if (srcfiles) {
1549 dealloc_all_srcfiles(dbg,srcfiles,srcfiles_cnt);
1550 srcfiles = 0;
1551 srcfiles_cnt = 0;
1552 }
1553 }
1554 if (cu_die2) {
1555 dwarf_dealloc_die(cu_die2);
1556 cu_die2 = 0;
1557 }
1558 ++cu_count;
1559 } /* End loop on loop_count (CUs) */
1560 return nres;
1561 }
1562
1563 static int
1564 print_a_die_stack(Dwarf_Debug dbg,
1565 char **srcfiles,
1566 Dwarf_Signed srcfiles_cnt,
1567 int lev,
1568 Dwarf_Error *err)
1569 {
1570 Dwarf_Bool print_else_name_match = TRUE;
1571 Dwarf_Bool ignore_die_stack = FALSE;
1572 Dwarf_Bool attribute_matched = FALSE;
1573 int res = 0;
1574
1575 res = print_one_die(dbg,
1576 die_stack[lev].die_,
1577 die_stack[lev].cu_die_offset_,
1578 print_else_name_match,lev,srcfiles,srcfiles_cnt,
1579 &attribute_matched,
1580 ignore_die_stack,
1581 err);
1582 return res;
1583 }
1584
1585 static int
1586 print_die_stack(Dwarf_Debug dbg,
1587 char **srcfiles,
1588 Dwarf_Signed srcfiles_cnt,
1589 Dwarf_Error*err)
1590 {
1591 int lev = 0;
1592 /* Print_information TRUE means attribute_matched
1593 will NOT be set by attribute name match.
1594 Just print the dies in the stack.*/
1595
1596 Dwarf_Bool print_else_name_match = TRUE;
1597 Dwarf_Bool ignore_die_stack = FALSE;
1598 Dwarf_Bool attribute_matched = FALSE;
1599
1600 for (lev = 0; lev <= die_stack_indent_level; ++lev)
1601 {
1602 int res = 0;
1603 res = print_one_die(dbg,die_stack[lev].die_,
1604 die_stack[lev].cu_die_offset_,
1605 print_else_name_match,
1606 lev,srcfiles,srcfiles_cnt,
1607 &attribute_matched,
1608 ignore_die_stack,err);
1609 if (res == DW_DLV_ERROR) {
1610 return res;
1611 }
1612 }
1613 return DW_DLV_OK;
1614 }
1615
1616 static int
1617 dd_check_tag_tree(Dwarf_Debug dbg,
1618 Dwarf_Die in_die,
1619 Dwarf_Error *err)
1620 {
1621 if ( glflags.gf_check_tag_tree ||
1622 glflags.gf_print_usage_tag_attr) {
1623 DWARF_CHECK_COUNT(tag_tree_result,1);
1624 if (die_stack_indent_level == 0) {
1625 Dwarf_Half tag = 0;
1626 int dtres = 0;
1627
1628 dtres = dwarf_tag(in_die, &tag, err);
1629 if (dtres != DW_DLV_OK) {
1630 DROP_ERROR_INSTANCE(dbg,dtres,*err);
1631 DWARF_CHECK_ERROR(tag_tree_result,
1632 "Tag-tree root tag unavailable: "
1633 "is not DW_TAG_compile_unit");
1634 } else if (tag == DW_TAG_skeleton_unit) {
1635 /* OK */
1636 } else if (tag == DW_TAG_compile_unit) {
1637 /* OK */
1638 } else if (tag == DW_TAG_partial_unit) {
1639 /* OK */
1640 } else if (tag == DW_TAG_type_unit) {
1641 /* OK */
1642 } else {
1643 DWARF_CHECK_ERROR(tag_tree_result,
1644 "tag-tree root is not "
1645 "DW_TAG_compile_unit "
1646 "or DW_TAG_partial_unit or "
1647 "DW_TAG_type_unit");
1648 }
1649 return DW_DLV_OK;
1650 }
1651 {
1652 Dwarf_Half tag_parent = 0;
1653 Dwarf_Half tag_child = 0;
1654 int pres = 0;
1655 int cres = 0;
1656
1657 pres = dwarf_tag(die_stack[
1658 die_stack_indent_level - 1].die_,
1659 &tag_parent, err);
1660 if (pres != DW_DLV_OK) {
1661 return pres;
1662 }
1663 cres = dwarf_tag(in_die, &tag_child, err);
1664 if (cres != DW_DLV_OK) {
1665 return cres;
1666 }
1667
1668 /* Check for specific compiler */
1669 if (checking_this_compiler()) {
1670 unsigned char ttresult = 0;
1671 /* Process specific TAGs. */
1672 tag_specific_globals_setup(dbg,tag_child,
1673 die_stack_indent_level);
1674 ttresult = legal_tag_tree_combination(
1675 tag_parent, tag_child);
1676 switch(ttresult) {
1677 case TK_OK:
1678 break;
1679 case TK_SHOW_MESSAGE:
1680 if (glflags.gf_check_tag_tree) {
1681 DWARF_CHECK_ERROR3(tag_tree_result,
1682 get_TAG_name(tag_parent,
1683 pd_dwarf_names_print_on_error),
1684 get_TAG_name(tag_child,
1685 pd_dwarf_names_print_on_error),
1686 "tag-tree relation is "
1687 "not standard.");
1688 }
1689 break;
1690 case TK_ERROR: /* fall thru */
1691 default:
1692 {
1693 static unsigned long errcount = 0;
1694 if (!errcount) {
1695 printf("ERROR: Tag parent 0x%x "
1696 "tag child 0x%x fails for unknown"
1697 " reason, possibly out of memory"
1698 " building large search trees."
1699 " This message will not repeat\n",
1700 tag_parent,tag_child);
1701 glflags.gf_count_major_errors++;
1702 }
1703 ++errcount;
1704 }
1705 break;
1706 }
1707 }
1708 }
1709 }
1710 return DW_DLV_OK;
1711 }
1712
1713 static void
1714 check_sibling_off(Dwarf_Unsigned loop_iteration,
1715 Dwarf_Off *sibling_off_array,
1716 Dwarf_Unsigned sibling_off_count)
1717 {
1718 if (!sibling_off_count) {
1719 return;
1720 }
1721 DWARF_CHECK_COUNT(check_functions_result,1);
1722 if ((loop_iteration+1) > sibling_off_count) {
1723 DWARF_CHECK_ERROR(check_functions_result,
1724 "The offset count from dwarf_offset_list"
1725 " is smaller than the actual sibling count");
1726 } else {
1727 Dwarf_Unsigned i = 0;
1728 Dwarf_Off off = 0;
1729 struct esb_s m;
1730 Dwarf_Off check_off = glflags.DIE_section_offset;
1731
1732 /* The following could use binary search as
1733 the array is strictly assending values. */
1734 for ( ; i < sibling_off_count; ++i) {
1735 off = sibling_off_array[i];
1736 if (check_off == off) {
1737 /* Good. Found */
1738 return;
1739 }
1740 }
1741 esb_constructor(&m);
1742 esb_append_printf_u(&m,
1743 "An offset from dwarf_offset_list"
1744 " of 0x%x"
1745 " is different than any actual"
1746 " sibling offset",glflags.DIE_section_offset);
1747 DWARF_CHECK_ERROR(check_functions_result,
1748 esb_get_string(&m));
1749 esb_destructor(&m);
1750 }
1751 }
1752
1753 static void
1754 check_sibling_offset_list_count(Dwarf_Unsigned loop_iteration,
1755 Dwarf_Unsigned sibling_off_count)
1756 {
1757 if (!sibling_off_count) {
1758 return;
1759 }
1760 if ((loop_iteration+1) != sibling_off_count) {
1761 DWARF_CHECK_COUNT(check_functions_result,1);
1762 DWARF_CHECK_ERROR(check_functions_result,
1763 "The offset count from dwarf_offset_list"
1764 " does not match the actual sibling count");
1765 }
1766 }
1767
1768 /* Here do pre-descent printing of the die
1769 The die stack is a global static array
1770 of a struct. See DIE_STACK
1771 */
1772 static int
1773 dd_print_die_and_die_stack(Dwarf_Debug dbg,
1774 Dwarf_Die in_die,
1775 Dwarf_Unsigned dieprint_cu_goffset,
1776 char **srcfiles,
1777 Dwarf_Signed srcfilescount,
1778 Dwarf_Error * err)
1779 {
1780 int pdres = 0;
1781 Dwarf_Bool an_attribute_match_local = FALSE;
1782 Dwarf_Bool ignore_die_stack = FALSE;
1783
1784 pdres = print_one_die(dbg, in_die,
1785 dieprint_cu_goffset,
1786 print_as_info_or_by_cuname(),
1787 die_stack_indent_level, srcfiles, srcfilescount,
1788 &an_attribute_match_local,
1789 ignore_die_stack,
1790 err);
1791 if (pdres != DW_DLV_OK) {
1792 return pdres;
1793 }
1794 validate_die_stack_siblings();
1795 if (!print_as_info_or_by_cuname() &&
1796 an_attribute_match_local) {
1797 if (glflags.gf_display_parent_tree) {
1798 pdres = print_die_stack(dbg,srcfiles,
1799 srcfilescount, err);
1800 if (pdres == DW_DLV_ERROR) {
1801 return pdres;
1802 }
1803 } else {
1804 if (glflags.gf_display_children_tree) {
1805 pdres = print_a_die_stack(
1806 dbg,srcfiles,srcfilescount,
1807 die_stack_indent_level,err);
1808 if (pdres == DW_DLV_ERROR) {
1809 return pdres;
1810 }
1811 }
1812 }
1813 if (glflags.gf_display_children_tree) {
1814 glflags.gf_stop_indent_level = die_stack_indent_level;
1815 glflags.gf_info_flag = TRUE;
1816 glflags.gf_types_flag = TRUE;
1817 }
1818 }
1819 return DW_DLV_OK;
1820 }
1821
1822 static int
1823 dd_check_die_abbrevs(Dwarf_Debug dbg, Dwarf_Die in_die,
1824 int childres)
1825 {
1826 Dwarf_Half ab_has_child;
1827 Dwarf_Bool bError = FALSE;
1828 Dwarf_Half tag = 0;
1829 int abtres = 0;
1830
1831 /* This does not return a Dwarf_Error value! */
1832 abtres = dwarf_die_abbrev_children_flag(in_die,
1833 &ab_has_child);
1834 if (abtres == DW_DLV_OK) {
1835 Dwarf_Error tagerr = 0;
1836 int tagres = 0;
1837
1838 DWARF_CHECK_COUNT(abbreviations_result,1);
1839 tagres = dwarf_tag(in_die, &tag, &tagerr);
1840 if (tagres == DW_DLV_OK) {
1841 switch (tag) {
1842 case DW_TAG_array_type:
1843 case DW_TAG_class_type:
1844 case DW_TAG_compile_unit:
1845 case DW_TAG_type_unit:
1846 case DW_TAG_partial_unit:
1847 case DW_TAG_enumeration_type:
1848 case DW_TAG_lexical_block:
1849 case DW_TAG_namespace:
1850 case DW_TAG_structure_type:
1851 case DW_TAG_subprogram:
1852 case DW_TAG_subroutine_type:
1853 case DW_TAG_union_type:
1854 case DW_TAG_entry_point:
1855 case DW_TAG_inlined_subroutine:
1856 break;
1857 default:
1858 bError = (childres == DW_DLV_OK && !ab_has_child) ||
1859 (childres == DW_DLV_NO_ENTRY && ab_has_child);
1860 if (bError) {
1861 DWARF_CHECK_ERROR(
1862 abbreviations_result,
1863 "check 'dw_children'"
1864 " flag combination.");
1865 }
1866 break;
1867 }
1868 } else if (tagres == DW_DLV_ERROR) {
1869 print_error_and_continue(
1870 "Unable to read die tag!",
1871 tagres,tagerr);
1872 dwarf_dealloc_error(dbg,tagerr);
1873 return DW_DLV_NO_ENTRY;
1874 }
1875 } else if (abtres == DW_DLV_ERROR) {
1876 glflags.gf_count_major_errors++;
1877 printf("ERROR: Unable to read die children "
1878 "flag.\n");
1879 /* return is unrelated to *error
1880 but leaving *error NULL should be ok. */
1881 return abtres;
1882 }
1883 return DW_DLV_OK;
1884 }
1885
1886 static int
1887 dwarf_check_child_offset(Dwarf_Debug dbg,
1888 Dwarf_Die child,
1889 Dwarf_Error *err)
1890 {
1891 /* If the global offset of the (first) child is
1892 <= the parent DW_AT_sibling global-offset-value
1893 then the compiler has made a mistake, and
1894 the DIE tree is corrupt. */
1895 int childoffres = 0;
1896 Dwarf_Off child_overall_offset = 0;
1897
1898 childoffres = dwarf_dieoffset(child,
1899 &child_overall_offset, err);
1900 /* ASSERT: never returns DW_DLV_NO_ENTRY */
1901 if (childoffres == DW_DLV_ERROR) {
1902 print_error_and_continue(
1903 "Finding a DIE offset (dwarf_dieoffset())"
1904 "failed.",childoffres,*err);
1905 return DW_DLV_ERROR;
1906 }
1907 if (childoffres == DW_DLV_OK) {
1908 Dwarf_Off parent_sib_val =
1909 get_die_stack_sibling();
1910 if (parent_sib_val &&
1911 (parent_sib_val <= child_overall_offset )) {
1912 char small_buf[ESB_FIXED_ALLOC_SIZE];
1913 struct esb_s pm;
1914
1915 esb_constructor_fixed(&pm,small_buf,
1916 sizeof(small_buf));
1917 esb_append_printf_u(&pm,
1918 "ERROR: A parent DW_AT_sibling of "
1919 "0x%" DW_PR_XZEROS DW_PR_DUx,
1920 parent_sib_val);
1921 esb_append_printf_s(&pm,
1922 " points %s the first child ",
1923 (parent_sib_val == child_overall_offset)?
1924 "at":"before");
1925 esb_append_printf_u(&pm,
1926 "0x%" DW_PR_XZEROS DW_PR_DUx
1927 " so the die tree is corrupt "
1928 "(showing section, not CU, offsets). ",
1929 child_overall_offset);
1930 dwarf_error_creation(dbg,err,
1931 esb_get_string(&pm));
1932 print_error_and_continue(
1933 esb_get_string(&pm),
1934 DW_DLV_ERROR,*err);
1935 /* Original test did a print_error()
1936 here, which did exit().
1937 We would like to return ERROR all the way
1938 back, but have no way at present
1939 to generate a Dwarf_Error record.
1940 Because these sorts of errors
1941 are not really recoverable.
1942 */
1943 esb_destructor(&pm);
1944 return DW_DLV_ERROR;
1945 }
1946 }
1947 return DW_DLV_OK;
1948 }
1949
1950 static void
1951 dd_validate_die_sibling(Dwarf_Debug dbg, Dwarf_Die sibling)
1952 {
1953 Dwarf_Off glb_off = 0;
1954 DWARF_CHECK_COUNT(di_gaps_result,1);
1955 if (!sibling) {
1956 return;
1957 }
1958 if (dwarf_validate_die_sibling(sibling,&glb_off) ==
1959 DW_DLV_ERROR) {
1960 Dwarf_Off sib_off;
1961 struct esb_s msg;
1962 Dwarf_Error err = 0;
1963 int dores = 0;
1964
1965 esb_constructor(&msg);
1966 dores = dwarf_dieoffset(sibling,&sib_off,&err);
1967 if (dores == DW_DLV_OK) {
1968 esb_append_printf_u(&msg,
1969 "GSIB = 0x%" DW_PR_XZEROS DW_PR_DUx,
1970 sib_off);
1971 esb_append_printf_u(&msg,
1972 " GOFF = 0x%" DW_PR_XZEROS DW_PR_DUx,
1973 glb_off);
1974 esb_append_printf_u(&msg,
1975 " Gap = %" DW_PR_DUu " bytes",
1976 sib_off-glb_off);
1977 DWARF_CHECK_ERROR2(di_gaps_result,
1978 "Incorrect sibling chain",
1979 esb_get_string(&msg));
1980 esb_destructor(&msg);
1981 return;
1982 }
1983 glflags.gf_count_major_errors++;
1984 if (dores == DW_DLV_ERROR) {
1985 printf("ERROR: Offset of sibling is unavailable "
1986 "(traversing die tree) "
1987 "which implies corrupt DWARF %s\n",
1988 dwarf_errmsg(err));
1989 dwarf_dealloc_error(dbg,err);
1990 err = 0;
1991 return;
1992 }
1993 printf("ERROR: Offset of sibling is unavailable "
1994 "(traversing die tree) "
1995 "which implies corrupt DWARF is corrupt\n");
1996 }
1997 }
1998
1999 /* Recursively follow the die tree.
2000 For in_die and its siblings we loop here,
2001 touching eash sibling of in_die.
2002 For children of in_die we recurse on this function.
2003 We use a DIE_STACK to keep track of where
2004 we are in the current loop and in the recursion.
2005 */
2006 static int
2007 print_die_and_children_internal(Dwarf_Debug dbg,
2008 Dwarf_Die in_die_in,
2009 Dwarf_Off dieprint_cu_goffset,
2010 Dwarf_Bool is_info,
2011 char **srcfiles,
2012 Dwarf_Signed srcfilescount,
2013 Dwarf_Off *sibling_off_array,
2014 Dwarf_Unsigned sibling_off_count,
2015 Dwarf_Error *err)
2016 {
2017 Dwarf_Die sibling = 0;
2018 Dwarf_Die in_die = in_die_in;
2019 Dwarf_Unsigned loop_iteration = 0;
2020
2021 /* Loop through siblings of in_die_in */
2022 for (;;++loop_iteration) {
2023 int offres = 0;
2024 int res = 0;
2025 int childres = 0;
2026 int siblingres = 0;
2027 Dwarf_Die child = 0;
2028 int pdacires = 0;
2029
2030 /* Get the CU offset for easy error reporting */
2031 offres = dwarf_die_offsets(in_die,
2032 &glflags.DIE_section_offset,
2033 &glflags.DIE_offset,err);
2034 DROP_ERROR_INSTANCE(dbg,offres,*err);
2035 SET_DIE_STACK_ENTRY(die_stack_indent_level,in_die,
2036 dieprint_cu_goffset);
2037 check_sibling_off(loop_iteration, sibling_off_array,
2038 sibling_off_count);
2039 res = dd_check_tag_tree(dbg,in_die,err);
2040 if (res != DW_DLV_OK){
2041 if (in_die != in_die_in) {
2042 dwarf_dealloc_die(in_die);
2043 return res;
2044 }
2045 }
2046 if (glflags.gf_record_dwarf_error &&
2047 glflags.gf_check_verbose_mode) {
2048 glflags.gf_record_dwarf_error = FALSE;
2049 }
2050 res = dd_print_die_and_die_stack(dbg,in_die,
2051 dieprint_cu_goffset, srcfiles,srcfilescount,
2052 err);
2053 if (res != DW_DLV_OK) {
2054 if (in_die != in_die_in) {
2055 dwarf_dealloc_die(in_die);
2056 }
2057 return res;
2058 }
2059 childres = dwarf_child(in_die, &child, err);
2060 if (childres == DW_DLV_ERROR) {
2061 print_error_and_continue(
2062 "Call to dwarf_child failed printing die tree",
2063 childres,*err);
2064 if (in_die != in_die_in) {
2065 dwarf_dealloc_die(in_die);
2066 }
2067 return childres;
2068 }
2069 /* Check for specific compiler, gf_check_abbreviations */
2070 if (glflags.gf_check_abbreviations &&
2071 checking_this_compiler()) {
2072 int cdares = 0;
2073
2074 cdares = dd_check_die_abbrevs(dbg, in_die,childres);
2075 if (cdares != DW_DLV_OK) {
2076 dwarf_dealloc_die(child);
2077 if (in_die != in_die_in) {
2078 dwarf_dealloc_die(in_die);
2079 }
2080 return cdares;
2081 }
2082 }
2083 /* child first: we are doing depth-first walk */
2084 if (childres == DW_DLV_OK) {
2085 int chkoffres = 0;
2086
2087 chkoffres = dwarf_check_child_offset(dbg,child,
2088 err);
2089 if (chkoffres == DW_DLV_ERROR) {
2090 dwarf_dealloc_die(child);
2091 if (in_die != in_die_in) {
2092 dwarf_dealloc_die(in_die);
2093 }
2094 return chkoffres;
2095 }
2096 if ((1+die_stack_indent_level) >= DIE_STACK_SIZE ) {
2097 report_die_stack_error(dbg,err);
2098 if (in_die != in_die_in) {
2099 dwarf_dealloc_die(in_die);
2100 }
2101 dwarf_dealloc_die(child);
2102 return DW_DLV_ERROR;
2103 }
2104 /* Use DIE_STACK to process children
2105 of the current DIE. Push current DIE to stack.
2106 Recursing would take too much stack space. */
2107 die_stack_indent_level++;
2108 SET_DIE_STACK_ENTRY(die_stack_indent_level,0,
2109 dieprint_cu_goffset);
2110 { /* Recurse here to traverse child */
2111 Dwarf_Off *inner_offset_array = 0;
2112 Dwarf_Unsigned inner_offset_count = 0;
2113
2114 dd_setup_die_check_functions(dbg,in_die,is_info,
2115 &inner_offset_array, &inner_offset_count,err);
2116 pdacires = print_die_and_children_internal(dbg, child,
2117 dieprint_cu_goffset,
2118 is_info, srcfiles, srcfilescount,
2119 inner_offset_array, inner_offset_count,
2120 err);
2121 if (inner_offset_array) {
2122 dwarf_dealloc(dbg,
2123 inner_offset_array,DW_DLA_UARRAY);
2124 }
2125 }
2126
2127 EMPTY_DIE_STACK_ENTRY(die_stack_indent_level);
2128 dwarf_dealloc_die(child);
2129 child = 0;
2130 /* Unwind DIE_STACK one level to get
2131 back to our sibling list to process the
2132 next sibling at our level. */
2133 die_stack_indent_level--;
2134 if (pdacires == DW_DLV_ERROR) {
2135 if (in_die != in_die_in) {
2136 dwarf_dealloc_die(in_die);
2137 }
2138 return pdacires;
2139 }
2140 } /* End processing child */
2141 if (glflags.gf_display_children_tree &&
2142 (glflags.gf_info_flag || glflags.gf_types_flag) &&
2143 glflags.gf_stop_indent_level ==
2144 die_stack_indent_level) {
2145 glflags.gf_info_flag = FALSE;
2146 glflags.gf_types_flag = FALSE;
2147 }
2148 sibling = 0;
2149 dwarf_dealloc_die(child);
2150 child = 0;
2151 /* Find the next sibling or get DW_DLV_NO_ENTRY */
2152 siblingres = dwarf_siblingof_c(in_die, &sibling, err);
2153 if (siblingres == DW_DLV_ERROR) {
2154 print_error_and_continue(
2155 "ERROR: dwarf_siblingof fails"
2156 " tracing siblings of a DIE.",
2157 siblingres, *err);
2158 if (in_die != in_die_in) {
2159 dwarf_dealloc_die(in_die);
2160 }
2161 return siblingres;
2162 }
2163 /* If we have a sibling, verify that its offset
2164 is next to the last processed DIE;
2165 An incorrect sibling chain is a nasty bug. */
2166 if (siblingres == DW_DLV_OK &&
2167 glflags.gf_check_di_gaps &&
2168 checking_this_compiler()) {
2169 dd_validate_die_sibling(dbg,sibling);
2170 }
2171
2172 /* Here do any post-descent (ie post-dwarf_child)
2173 processing of the in_die (we prepare
2174 to loop again). */
2175 EMPTY_DIE_STACK_ENTRY(die_stack_indent_level);
2176 if (in_die != in_die_in) {
2177 /* Dealloc our in_die, but not the
2178 argument die, it belongs
2179 to our caller. Whether the siblingof
2180 call worked or not. */
2181 dwarf_dealloc_die(in_die);
2182 in_die = 0;
2183 }
2184 if (siblingres == DW_DLV_OK) {
2185 /* Set to process the sibling, loop again. */
2186 in_die = sibling;
2187 sibling = 0;
2188 } else {
2189 /* ASSERT: siblingres is DW_DLV_NO_ENTRY */
2190 sibling = 0;
2191 in_die = 0;
2192 /* We are done, no more siblings at this level. */
2193 check_sibling_offset_list_count(loop_iteration,
2194 sibling_off_count);
2195 break;
2196 }
2197 } /* end for loop on siblings */
2198 return DW_DLV_OK;
2199 }
2200
2201 static void
2202 dealloc_local_atlist(Dwarf_Debug dbg,
2203 Dwarf_Attribute *atlist,
2204 Dwarf_Signed atcnt)
2205 {
2206 Dwarf_Signed i = 0;
2207
2208 for (i = 0; i < atcnt; i++) {
2209 dwarf_dealloc_attribute(atlist[i]);
2210 atlist[i] = 0;
2211 }
2212 dwarf_dealloc(dbg, atlist, DW_DLA_LIST);
2213 }
2214
2215 /* Print one die on error and verbose or non check mode */
2216 #define PRINTING_DIES (glflags.gf_do_print_dwarf || \
2217 (glflags.gf_record_dwarf_error && \
2218 glflags.gf_check_verbose_mode))
2219
2220 static void
2221 print_srcfiles( char **srcfiles,Dwarf_Signed srcfcnt)
2222 {
2223 Dwarf_Signed i = 0;
2224 const char * cntstr = " [%" DW_PR_DSd "]";
2225
2226 printf(" dwarf_srcfiles() returned strings. Count = %"
2227 DW_PR_DSd ".\n",srcfcnt);
2228 if (srcfcnt < 0) {
2229 glflags.gf_count_major_errors++;
2230 printf("ERROR: dwarf_srcfiles count less than zero "
2231 "which should be impossible. Ignoring srcfiles.");
2232 return;
2233 }
2234 if (srcfcnt > 9) {
2235 if (srcfcnt > 99) {
2236 cntstr = " [%3" DW_PR_DSd "]";
2237 } else {
2238 cntstr = " [%2" DW_PR_DSd "]";
2239 }
2240 }
2241 for ( ; i < srcfcnt; ++i) {
2242 printf(cntstr,i);
2243 printf(" %s\n",sanitized(srcfiles[i]));
2244 }
2245 }
2246
2247 static int
2248 check_duplicated_attributes(Dwarf_Debug dbg,
2249 Dwarf_Signed i,
2250 Dwarf_Half attr,
2251 Dwarf_Attribute *atlist,
2252 Dwarf_Signed atcnt,
2253 Dwarf_Error *err)
2254 {
2255 if (glflags.gf_check_duplicated_attributes) {
2256 Dwarf_Half attr_next = 0;
2257 Dwarf_Signed j = 0;
2258
2259 DWARF_CHECK_COUNT(duplicated_attributes_result,1);
2260 for (j = i + 1; j < atcnt; ++j) {
2261 int ares = 0;
2262
2263 ares = dwarf_whatattr(atlist[j],
2264 &attr_next,err);
2265 if (ares == DW_DLV_OK) {
2266 if (attr == attr_next) {
2267 DWARF_CHECK_ERROR2(
2268 duplicated_attributes_result,
2269 "Duplicated attribute ",
2270 get_AT_name(attr,
2271 pd_dwarf_names_print_on_error));
2272 }
2273 } else {
2274 struct esb_s m;
2275 esb_constructor(&m);
2276 esb_append_printf_i(&m,
2277 "ERROR: dwarf_whatattr entry missing "
2278 " when checking for duplicated "
2279 "attributes"
2280 " reading attribute ",j);
2281 print_error_and_continue(esb_get_string(&m),
2282 ares, *err);
2283 esb_destructor(&m);
2284 dealloc_local_atlist(dbg,atlist,atcnt);
2285 return ares;
2286 }
2287 }
2288 }
2289 return DW_DLV_OK;
2290 }
2291 static void
2292 dd_check_attrlist_sensible(Dwarf_Debug dbg,
2293 Dwarf_Die die,
2294 Dwarf_Attribute *attrlist, Dwarf_Signed atcnt)
2295 {
2296 Dwarf_Signed i = 0;
2297 Dwarf_Error error = 0;
2298 static int check_limiter;
2299
2300 if (!glflags.gf_check_functions) {
2301 return;
2302 }
2303 if (check_limiter > 1000) {
2304 return;
2305 }
2306 ++check_limiter;
2307
2308 DWARF_CHECK_COUNT(check_functions_result,1);
2309 for (i=0; i < atcnt; ++i) {
2310 int res = 0;
2311 Dwarf_Off current_off = 0;
2312 Dwarf_Half form = 0;
2313 Dwarf_Bool present = FALSE;
2314
2315 res = dwarf_attr_offset(die,attrlist[i],
2316 &current_off,&error);
2317 if (res == DW_DLV_ERROR) {
2318 DWARF_CHECK_ERROR(check_functions_result,
2319 "attr offsets unavailable");
2320 DROP_ERROR_INSTANCE(dbg,res,error);
2321 return;
2322 }
2323 if (res == DW_DLV_NO_ENTRY) {
2324 DWARF_CHECK_ERROR(check_functions_result,
2325 "attr offsets no_entry (impossible)");
2326 DROP_ERROR_INSTANCE(dbg,res,error);
2327 /* impossible */
2328 continue;
2329 }
2330
2331 res = dwarf_whatform(attrlist[i], &form,&error);
2332 if (res == DW_DLV_ERROR) {
2333 DWARF_CHECK_ERROR(check_functions_result,
2334 "dwarf_whatform fails");
2335 DROP_ERROR_INSTANCE(dbg,res,error);
2336 return;
2337 } if (res == DW_DLV_NO_ENTRY) {
2338 DWARF_CHECK_ERROR(check_functions_result,
2339 "dwarf_whatform gets NO_ENTRY");
2340 DROP_ERROR_INSTANCE(dbg,res,error);
2341 /* impossible */
2342 continue;
2343 }
2344 res = dwarf_hasform(attrlist[i],form,&present,&error);
2345 if (res == DW_DLV_ERROR) {
2346 DWARF_CHECK_ERROR(check_functions_result,
2347 "dwarf_hasform fails");
2348 DROP_ERROR_INSTANCE(dbg,res,error);
2349 return;
2350 } else if (res == DW_DLV_NO_ENTRY) {
2351 DWARF_CHECK_ERROR(check_functions_result,
2352 "dwarf_hasform gets NO_ENTRY");
2353 DROP_ERROR_INSTANCE(dbg,res,error);
2354 /* impossible */
2355 continue;
2356 } else if (!present) {
2357 struct esb_s m;
2358 char buf[40];
2359
2360 esb_constructor_fixed(&m,buf,sizeof(buf));
2361 esb_append_printf_u(&m,
2362 "dwarf_hasform shows not present"
2363 " on form 0x%" DW_PR_DUx,
2364 (Dwarf_Unsigned)form);
2365 esb_append_printf_i(&m,
2366 "on attribute number %d.",
2367 i);
2368
2369 DWARF_CHECK_ERROR(check_functions_result,
2370 esb_get_string(&m));
2371 DROP_ERROR_INSTANCE(dbg,res,error);
2372 esb_destructor(&m);
2373 }
2374 if (form == DW_FORM_data16) {
2375 Dwarf_Form_Data16 data16;
2376
2377 memset(&data16,0,sizeof(data16));
2378 res = dwarf_formdata16(attrlist[i],&data16,&error);
2379 if (res == DW_DLV_ERROR) {
2380 DWARF_CHECK_ERROR(check_functions_result,
2381 "dwarf_formdata16 fails");
2382 DROP_ERROR_INSTANCE(dbg,res,error);
2383 return;
2384 } else if (res == DW_DLV_NO_ENTRY) {
2385 DWARF_CHECK_ERROR(check_functions_result,
2386 "dwarf_formdata16 gets NO_ENTRY");
2387 DROP_ERROR_INSTANCE(dbg,res,error);
2388 /* impossible */
2389 }
2390 /* Unclear how to check the value */
2391 }
2392 /* Currently unsure how to check more than this. */
2393 }
2394 }
2395
2396 static void
2397 dd_check_if_legal_offset( Dwarf_Debug dbg,
2398 Dwarf_Off offset,
2399 Dwarf_Bool is_info)
2400 {
2401 int res = 0;
2402 Dwarf_Error error = 0;
2403 Dwarf_Die dietwo = 0;
2404
2405 DWARF_CHECK_COUNT(check_functions_result,1);
2406 res = dwarf_offdie_b(dbg,offset,is_info,&dietwo,&error);
2407 if (res == DW_DLV_OK) {
2408 dwarf_dealloc_die(dietwo);
2409 return; /* ok */
2410 }
2411 if (res == DW_DLV_NO_ENTRY) {
2412 struct esb_s m;
2413 char ebuf[120];
2414
2415 ebuf[0] = 0;
2416 esb_constructor_fixed(&m,ebuf,sizeof(ebuf));
2417 esb_append_printf_u(&m,
2418 "dwarf_offdie_b ok but "
2419 "given offset 0x%"
2420 DW_PR_DUx ,offset);
2421 esb_append_printf_u(&m,
2422 " and is_info %u"
2423 " got DW_DLV_NO_ENTRY.",
2424 (Dwarf_Small)is_info);
2425 DWARF_CHECK_ERROR(check_functions_result,
2426 esb_get_string(&m));
2427 esb_destructor(&m);
2428 return;
2429 }
2430 {
2431 struct esb_s m;
2432 char ebuf[200];
2433
2434 esb_constructor_fixed(&m,ebuf,sizeof(ebuf));
2435 esb_append_printf_u(&m,
2436 "dwarf_offdie_b given offset 0x%"
2437 DW_PR_DUx, offset);
2438 esb_append_printf_u(&m,
2439 "and is_info %u"
2440 " is not a local reference.",
2441 (Dwarf_Small)is_info);
2442 esb_append_printf_s(&m," Underlying error: %s",
2443 dwarf_errmsg(error));
2444 DWARF_CHECK_ERROR(check_functions_result,
2445 esb_get_string(&m));
2446 esb_destructor(&m);
2447 DROP_ERROR_INSTANCE(dbg,res,error);
2448 }
2449 }
2450
2451 static void
2452 check_functions_simple_fail(const char *name,Dwarf_Error error)
2453 {
2454 struct esb_s m;
2455 char ebuf[60];
2456
2457 esb_constructor_fixed(&m,ebuf,sizeof(ebuf));
2458 esb_append_printf_s(&m,"fail %s ",name);
2459 esb_append(&m,
2460 dwarf_errmsg(error));
2461 DWARF_CHECK_ERROR(check_functions_result,
2462 esb_get_string(&m));
2463 esb_destructor(&m);
2464 }
2465
2466 static void
2467 dd_check_die_functions( Dwarf_Debug dbg,
2468 Dwarf_Die die)
2469 {
2470 Dwarf_Error error = 0;
2471 Dwarf_Off off = 0;
2472 Dwarf_Half attrnum = 0;
2473 Dwarf_Unsigned unsign = 0;
2474 int res = 0;
2475 Dwarf_Bool is_info = FALSE;
2476
2477 if (!glflags.gf_check_functions) {
2478 return;
2479 }
2480 DWARF_CHECK_COUNT(check_functions_result,1);
2481 res = dwarf_dietype_offset(die,&off,&is_info,&error);
2482 if (res == DW_DLV_OK) {
2483 dd_check_if_legal_offset(dbg,off,is_info);
2484 } else if (res == DW_DLV_ERROR) {
2485 /* errno > 0 , < 1000 */
2486 int err = (int)dwarf_errno(error);
2487 if (err == DW_DLE_MISSING_NEEDED_DEBUG_ADDR_SECTION) {
2488 /* OK */
2489 } else {
2490 struct esb_s m;
2491 char ebuf[60];
2492
2493 esb_constructor_fixed(&m,ebuf,sizeof(ebuf));
2494 esb_append_printf_s(&m,
2495 "dwarf_dietype_offset error %s"
2496 " Not a CU die or not a local reference",
2497 dwarf_errmsg(error));
2498 DWARF_CHECK_ERROR(check_functions_result,
2499 esb_get_string(&m));
2500 esb_destructor(&m);
2501 }
2502 DROP_ERROR_INSTANCE(dbg,res,error);
2503 } /* else DW_DLV_NO_ENTRY, we assume ok */
2504 off = 0;
2505
2506 res = dwarf_bytesize(die,&unsign,&error);
2507 if (res == DW_DLV_OK) {
2508 /* Ok */
2509 } else if (res == DW_DLV_ERROR) {
2510 check_functions_simple_fail("dwarf_bytesize",error);
2511 DROP_ERROR_INSTANCE(dbg,res,error);
2512 } /* else DW_DLV_NO_ENTRY, we assume ok */
2513 unsign = 0;
2514 res = dwarf_bitsize(die,&unsign,&error);
2515 if (res == DW_DLV_OK) {
2516 /* Ok */
2517 } else if (res == DW_DLV_ERROR) {
2518 check_functions_simple_fail("dwarf_srclang",error);
2519 DROP_ERROR_INSTANCE(dbg,res,error);
2520 } /* else DW_DLV_NO_ENTRY, we assume ok */
2521 unsign = 0;
2522
2523 res = dwarf_bitoffset(die,&attrnum,&unsign,&error);
2524 if (res == DW_DLV_OK) {
2525 /* ok */
2526 } else if (res == DW_DLV_ERROR) {
2527 check_functions_simple_fail("dwarf_bitoffset",error);
2528 DROP_ERROR_INSTANCE(dbg,res,error);
2529 } /* else DW_DLV_NO_ENTRY, we assume ok */
2530 attrnum = 0;
2531 unsign = 0;
2532
2533 res = dwarf_srclang(die,&unsign,&error);
2534 if (res == DW_DLV_OK) {
2535 if ((unsign >=1) && (unsign <= DW_LANG_Ada2012)) {
2536 /* standard */
2537 } else {
2538 switch(unsign) {
2539 case DW_LANG_Mips_Assembler:
2540 case DW_LANG_GOOGLE_RenderScript:
2541 case DW_LANG_Upc:
2542 case DW_LANG_ALTIUM_Assembler:
2543 case DW_LANG_BORLAND_Delphi:
2544 case DW_LANG_SUN_Assembler:
2545 break;
2546 default: {
2547
2548 struct esb_s m;
2549 char ebuf[60];
2550
2551 esb_constructor_fixed(&m,ebuf,sizeof(ebuf));
2552 esb_append_printf_u(&m,
2553 "dwarf_srclang fail "
2554 "as the source language 0x%x "
2555 "returned is unknown",unsign);
2556 DWARF_CHECK_ERROR(check_functions_result,
2557 esb_get_string(&m));
2558 esb_destructor(&m);
2559 }
2560 }
2561 }
2562 } else if (res == DW_DLV_ERROR) {
2563 check_functions_simple_fail("dwarf_srclang",error);
2564 DROP_ERROR_INSTANCE(dbg,res,error);
2565 } /* else DW_DLV_NO_ENTRY, we assume ok */
2566 unsign = 0;
2567
2568 res = dwarf_arrayorder(die,&unsign,&error);
2569 if (res == DW_DLV_OK) {
2570 if (unsign != DW_ORD_col_major &&
2571 unsign != DW_ORD_row_major) {
2572 DWARF_CHECK_ERROR(check_functions_result,
2573 "array ordering value not valid DW_ORD_");
2574 }
2575 } else if (res == DW_DLV_ERROR) {
2576 check_functions_simple_fail("dwarf_arrayorder",error);
2577 DROP_ERROR_INSTANCE(dbg,res,error);
2578 } /* else DW_DLV_NO_ENTRY, we assume ok */
2579 }
2580
2581 /* If print_else_name_match is FALSE,
2582 check for attribute matches with -S
2583 inr print_attribute, and if found,
2584 print the information anyway.
2585
2586 if print_else_name_match is true, do not check
2587 for attribute name matches. Just print.
2588
2589 Sets *an_attr_matched TRUE if there is
2590 attribute name or value that matches
2591 a -S option (the long form option starts with --search)
2592
2593 Returns DW_DLV_OK DW_DLV_ERROR or DW_DLV_NO_ENTRY
2594 */
2595 int
2596 print_one_die(Dwarf_Debug dbg, Dwarf_Die die,
2597 Dwarf_Off dieprint_cu_goffset,
2598 Dwarf_Bool print_else_name_match,
2599 int die_indent_level,
2600 char **srcfiles, Dwarf_Signed srcfcnt,
2601 Dwarf_Bool *an_attr_matched_io,
2602 Dwarf_Bool ignore_die_stack,
2603 Dwarf_Error *err)
2604 {
2605 Dwarf_Signed i = 0;
2606 Dwarf_Off offset = 0;
2607 Dwarf_Off overall_offset = 0;
2608 const char * tagname = 0;
2609 Dwarf_Half tag = 0;
2610 Dwarf_Signed atcnt = 0;
2611 Dwarf_Attribute *atlist = 0;
2612 int tres = 0;
2613 int ores = 0;
2614 Dwarf_Bool attribute_matchedpod = FALSE;
2615 int atres = 0;
2616 Dwarf_Unsigned abbrev_code = dwarf_die_abbrev_code(die);
2617 LoHiPc lohipc;
2618 int indentprespaces = 0;
2619
2620 lohipc = lohipc_zero;
2621 /* Print using indentation see standard_indent() above.
2622 < 1><0x000854ff GOFF=0x00546047> DW_TAG_pointer_type -> 34
2623 < 1><0x000854ff> DW_TAG_pointer_type -> 18
2624 DW_TAG_pointer_type -> 2
2625 */
2626 if (glflags.gf_check_abbreviations &&
2627 checking_this_compiler()) {
2628 validate_abbrev_code(abbrev_code);
2629 }
2630 if (!ignore_die_stack &&
2631 die_stack[die_indent_level].already_printed_) {
2632 /* FALSE seems safe . */
2633 *an_attr_matched_io = FALSE;
2634 return DW_DLV_OK;
2635 }
2636 indentprespaces = standard_indent();
2637 tres = dwarf_tag(die, &tag, err);
2638 if (tres != DW_DLV_OK) {
2639 print_error_and_continue(
2640 "ERROR: accessing tag of die!",
2641 tres, *err);
2642 return tres;
2643 }
2644 tagname = get_TAG_name(tag,pd_dwarf_names_print_on_error);
2645 if ( glflags.gf_print_usage_tag_attr) {
2646 record_tag_usage(tag);
2647 }
2648 tag_specific_globals_setup(dbg,tag,die_indent_level);
2649 ores = dwarf_dieoffset(die, &overall_offset, err);
2650 if (ores != DW_DLV_OK) {
2651 print_error_and_continue(
2652 "ERROR: failed dwarf_dieoffset call", ores, *err);
2653 return ores;
2654 }
2655 ores = dwarf_die_CU_offset(die, &offset, err);
2656 if (ores != DW_DLV_OK) {
2657 print_error_and_continue(
2658 "ERROR: dwarf_die_CU_offset failed", ores, *err);
2659 return ores;
2660 }
2661
2662 if (dump_visited_info && glflags.gf_check_self_references) {
2663 printf("<%2d><0x%" DW_PR_XZEROS DW_PR_DUx
2664 " GOFF=0x%" DW_PR_XZEROS DW_PR_DUx "> ",
2665 die_indent_level, (Dwarf_Unsigned)offset,
2666 (Dwarf_Unsigned)overall_offset);
2667 print_indent_prefix(0,die_indent_level,2);
2668 printf("%s\n",tagname);
2669 }
2670 /* Print the die */
2671 if (PRINTING_DIES && print_else_name_match) {
2672 if (!ignore_die_stack) {
2673 die_stack[die_indent_level].already_printed_ = TRUE;
2674 }
2675 if (die_indent_level == 0) {
2676 print_cu_hdr_cudie(overall_offset, offset);
2677 } else if (local_symbols_already_begun == FALSE &&
2678 die_indent_level == 1 && !glflags.dense) {
2679
2680 printf("\nLOCAL_SYMBOLS:\n");
2681 local_symbols_already_begun = TRUE;
2682 }
2683
2684 /* Print just the Tags and Attributes */
2685 if (!glflags.gf_display_offsets) {
2686 /* Print using indentation */
2687 print_indent_prefix(0,die_indent_level,2);
2688 printf("%s\n",tagname);
2689 } else {
2690 if (glflags.dense) {
2691 if (glflags.gf_show_global_offsets) {
2692 if (die_indent_level == 0) {
2693 printf("<%d><0x%" DW_PR_DUx
2694 "+0x%" DW_PR_DUx
2695 " GOFF=0x%"
2696 DW_PR_DUx ">", die_indent_level,
2697 (Dwarf_Unsigned)
2698 (overall_offset - offset),
2699 (Dwarf_Unsigned)offset,
2700 (Dwarf_Unsigned)overall_offset);
2701 } else {
2702 printf("<%d><0x%" DW_PR_DUx
2703 " GOFF=0x%" DW_PR_DUx ">",
2704 die_indent_level,
2705 (Dwarf_Unsigned)offset,
2706 (Dwarf_Unsigned)overall_offset);
2707 }
2708 } else {
2709 if (die_indent_level == 0) {
2710 printf("<%d><0x%" DW_PR_DUx "+0x%"
2711 DW_PR_DUx ">",
2712 die_indent_level,
2713 (Dwarf_Unsigned)
2714 (overall_offset - offset),
2715 (Dwarf_Unsigned)offset);
2716 } else {
2717 printf("<%d><0x%" DW_PR_DUx ">",
2718 die_indent_level,
2719 (Dwarf_Unsigned)offset);
2720 }
2721 }
2722 printf("<%s>",tagname);
2723 if (glflags.verbose) {
2724 Dwarf_Off agoff = 0;
2725 Dwarf_Unsigned acount = 0;
2726 printf(" <abbrev %" DW_PR_DUu ,abbrev_code);
2727 if (glflags.gf_show_global_offsets) {
2728 int agres = 0;
2729
2730 agres =
2731 dwarf_die_abbrev_global_offset(
2732 die, &agoff, &acount,err);
2733 if (agres == DW_DLV_ERROR) {
2734 print_error_and_continue(
2735 "dwarf_die_abbrev_global_offset"
2736 " call failed",
2737 agres, *err);
2738 return agres;
2739 } else if (agres == DW_DLV_NO_ENTRY) {
2740 print_error_and_continue(
2741 "dwarf_die_abbrev_global_offset"
2742 " no entry?",
2743 agres, *err);
2744 return agres;
2745 } else {
2746 printf(" ABGOFF = 0x%"
2747 DW_PR_XZEROS DW_PR_DUx
2748 " count = 0x%"
2749 DW_PR_XZEROS DW_PR_DUx,
2750 agoff, acount);
2751 }
2752 }
2753 printf(">");
2754 }
2755 } else {
2756 if (glflags.gf_show_global_offsets) {
2757 printf("<%2d><0x%" DW_PR_XZEROS DW_PR_DUx
2758 " GOFF=0x%" DW_PR_XZEROS DW_PR_DUx ">",
2759 die_indent_level, (Dwarf_Unsigned)offset,
2760 (Dwarf_Unsigned)overall_offset);
2761 } else {
2762 printf("<%2d><0x%" DW_PR_XZEROS DW_PR_DUx ">",
2763 die_indent_level,
2764 (Dwarf_Unsigned)offset);
2765 }
2766
2767 /* Print using indentation */
2768 print_indent_prefix(0,die_indent_level, 2);
2769 printf("%s",tagname);
2770 if (glflags.verbose) {
2771 Dwarf_Off agoff = 0;
2772 Dwarf_Unsigned acount = 0;
2773 printf(" <abbrev %" DW_PR_DUu,abbrev_code);
2774 if (glflags.gf_show_global_offsets) {
2775 int agres = 0;
2776
2777 agres = dwarf_die_abbrev_global_offset(
2778 die, &agoff, &acount,err);
2779 if (agres == DW_DLV_ERROR) {
2780 print_error_and_continue(
2781 "Call to "
2782 "dwarf_die_abbrev_global_offset"
2783 "failed ",
2784 agres, *err);
2785 return agres;
2786 } else if (agres == DW_DLV_NO_ENTRY) {
2787 print_error_and_continue(
2788 "Call to "
2789 "dwarf_die_abbrev_global_offset "
2790 " returned NO_ENTRY!",
2791 agres,*err);
2792 return agres;
2793 } else {
2794 printf(" ABGOFF = 0x%"
2795 DW_PR_XZEROS DW_PR_DUx
2796 " count = 0x%"
2797 DW_PR_XZEROS DW_PR_DUx,
2798 agoff, acount);
2799 }
2800 }
2801 printf(">");
2802 }
2803 fputs("\n",stdout);
2804 }
2805 }
2806 }
2807 if ((glflags.verbose > 2) && (die_indent_level == 0) &&
2808 srcfcnt && PRINTING_DIES) {
2809 print_srcfiles(srcfiles,srcfcnt);
2810 }
2811 atres = dwarf_attrlist(die, &atlist, &atcnt, err);
2812 if (atres == DW_DLV_ERROR) {
2813 print_error_and_continue(
2814 "ERROR: A call to dwarf_attrlist failed. "
2815 " Impossible error.", atres,*err);
2816 return atres;
2817 }
2818 if (atres == DW_DLV_NO_ENTRY) {
2819 /* indicates there are no attrs. It is not an error. */
2820 atcnt = 0;
2821 }
2822
2823 /* Get the offset for easy error reporting:
2824 This is not the CU die. */
2825 atres = dwarf_die_offsets(die,&glflags.DIE_section_offset,
2826 &glflags.DIE_offset,err);
2827 if (atres == DW_DLV_ERROR) {
2828 print_error_and_continue(
2829 "ERROR: A call to dwarf_die_offsets failed in "
2830 "printing an attribute. "
2831 , atres,*err);
2832 dealloc_local_atlist(dbg,atlist,atcnt);
2833 return atres;
2834 }
2835
2836 dd_check_die_functions(dbg,die);
2837 dd_check_attrlist_sensible(dbg,die,atlist,atcnt);
2838 for (i = 0; i < atcnt; i++) {
2839 int ares = 0;
2840 Dwarf_Half attrnum = 0;
2841
2842 ares = dwarf_whatattr(atlist[i], &attrnum, err);
2843 if (ares == DW_DLV_OK) {
2844 /* Check duplicated attributes; use brute force
2845 as the number of attributes is quite small;
2846 the problem was detected with the
2847 LLVM toolchain, generating more than 12
2848 repeated attributes */
2849 ares = check_duplicated_attributes(dbg,i,
2850 attrnum,atlist,atcnt, err);
2851 if (ares != DW_DLV_OK) {
2852 return ares;
2853 }
2854
2855 /* Print using indentation */
2856 if (!glflags.dense && PRINTING_DIES &&
2857 print_else_name_match) {
2858 print_indent_prefix(indentprespaces,
2859 die_indent_level,2);
2860 }
2861 {
2862 Dwarf_Bool attr_match_localb = FALSE;
2863 int aresb = 0;
2864
2865 aresb = print_attribute(dbg, die,
2866 dieprint_cu_goffset,
2867 attrnum,
2868 atlist[i],
2869 print_else_name_match, die_indent_level,
2870 srcfiles, srcfcnt,
2871 &lohipc,
2872 &attr_match_localb,err);
2873 if (aresb == DW_DLV_ERROR) {
2874 struct esb_s m;
2875
2876 esb_constructor(&m);
2877 esb_append_printf_i(&m,
2878 "ERROR: Failed printing attribute %d",
2879 i);
2880 esb_append_printf_i(&m,
2881 " of %d attributes.",atcnt);
2882 print_error_and_continue(
2883 esb_get_string(&m),
2884 aresb,*err);
2885 esb_destructor(&m);
2886 DROP_ERROR_INSTANCE(dbg,aresb,*err);
2887 }
2888 if (print_else_name_match == FALSE &&
2889 attr_match_localb) {
2890 attribute_matchedpod = TRUE;
2891 }
2892 }
2893
2894 if (glflags.gf_record_dwarf_error &&
2895 glflags.gf_check_verbose_mode) {
2896 glflags.gf_record_dwarf_error = FALSE;
2897 }
2898 } else {
2899 struct esb_s m;
2900
2901 esb_constructor(&m);
2902 esb_append_printf_i(&m,
2903 "ERROR: Failed getting attribute %d",
2904 i);
2905 esb_append_printf_i(&m,
2906 " of %d attributes.",
2907 atcnt);
2908 print_error_and_continue(esb_get_string(&m),ares,*err);
2909 esb_destructor(&m);
2910 dealloc_local_atlist(dbg,atlist,atcnt);
2911 return ares;
2912 }
2913 }
2914 /* atres might have been DW_DLV_NO_ENTRY, atlist NULL */
2915 if (atlist) {
2916 dealloc_local_atlist(dbg,atlist,atcnt);
2917 }
2918 if (PRINTING_DIES && glflags.dense && print_else_name_match) {
2919 printf("\n");
2920 }
2921 *an_attr_matched_io = attribute_matchedpod;
2922 return DW_DLV_OK;
2923 }
2924
2925 /* Encodings have undefined signedness. Accept either
2926 signedness. The values are integer-like (they are defined
2927 in the DWARF specification), so the
2928 form the compiler uses (as long as it is
2929 a constant value) is a non-issue.
2930
2931 The numbers need not be small (in spite of the
2932 function name), but the result should be an integer.
2933
2934 If string_out is non-NULL, construct a string output, either
2935 an error message or the name of the encoding.
2936 The function pointer passed in is to code generated
2937 by a script at dwarfdump build time. The code for
2938 the val_as_string function is generated
2939 from dwarf.h. See <build dir>/dwarf_names.c
2940
2941 The known_signed bool is set TRUE(nonzero) or FALSE (zero)
2942 and *both* uval_out and sval_out are set to the value,
2943 though of course uval_out cannot represent a signed
2944 value properly and sval_out cannot represent all unsigned
2945 values properly.
2946
2947 If string_out is non-NULL then attr_name and val_as_string
2948 must also be non-NULL. */
2949 int
2950 dd_get_integer_and_name(Dwarf_Debug dbg,
2951 Dwarf_Attribute attrib,
2952 Dwarf_Unsigned * uval_out,
2953 const char *attr_name,
2954 struct esb_s* string_out,
2955 encoding_type_func val_as_string,
2956 Dwarf_Error * err,
2957 int show_form)
2958 {
2959 Dwarf_Unsigned uval = 0;
2960 Dwarf_Bool is_info = TRUE;
2961
2962 int vres = dwarf_formudata(attrib, &uval, err);
2963 /* if it is not formudata, lets check further */
2964 DROP_ERROR_INSTANCE(dbg,vres,*err);
2965 if (vres != DW_DLV_OK) {
2966 Dwarf_Signed sval = 0;
2967 int ires = 0;
2968
2969 ires = dwarf_formsdata(attrib, &sval, err);
2970 /* It is not formudata, lets check further */
2971 DROP_ERROR_INSTANCE(dbg,ires,*err);
2972 if (ires != DW_DLV_OK) {
2973 int jres = 0;
2974
2975 jres = dwarf_global_formref_b(attrib,&uval,
2976 &is_info,err);
2977 if (jres != DW_DLV_OK) {
2978 if (string_out) {
2979 glflags.gf_count_major_errors++;
2980 esb_append_printf_s(string_out,
2981 "ERROR: %s has a bad form"
2982 " for reading a value.",
2983 attr_name);
2984 }
2985 return jres;
2986 }
2987 *uval_out = uval;
2988 } else {
2989 uval = (Dwarf_Unsigned) sval;
2990 *uval_out = uval;
2991 }
2992 } else {
2993 *uval_out = uval;
2994 }
2995 if (string_out) {
2996 Dwarf_Half theform = 0;
2997 Dwarf_Half directform = 0;
2998 char fsbuf[ESB_FIXED_ALLOC_SIZE];
2999 struct esb_s fstring;
3000 int fres = 0;
3001
3002 esb_constructor_fixed(&fstring,fsbuf,sizeof(fsbuf));
3003 fres = get_form_values(attrib,&theform,
3004 &directform,err);
3005 if (fres == DW_DLV_ERROR) {
3006 return fres;
3007 }
3008 esb_append(&fstring, val_as_string((Dwarf_Half) uval,
3009 pd_dwarf_names_print_on_error));
3010 show_form_itself(show_form,glflags.verbose,theform,
3011 directform,&fstring);
3012 esb_append(string_out,esb_get_string(&fstring));
3013 esb_destructor(&fstring);
3014 }
3015 return DW_DLV_OK;
3016 }
3017
3018 /* Called for DW_AT_SUN_func_offsets
3019 We need a 32-bit signed number here.
3020 But we're getting rid of the __[u]int[n]_t
3021 dependence so lets use plain characters.
3022
3023 This is rarely, if ever, used so lets
3024 report errors but not stop the processing.
3025 */
3026
3027 static void
3028 get_FLAG_BLOCK_string(Dwarf_Debug dbg, Dwarf_Attribute attrib,
3029 struct esb_s*esbp)
3030 {
3031 int fres = 0;
3032 Dwarf_Block *tempb = 0;
3033 Dwarf_Unsigned array_len = 0;
3034 Dwarf_Signed *array = 0;
3035 Dwarf_Unsigned next = 0;
3036 Dwarf_Error fblkerr = 0;
3037
3038 /* first get compressed block data */
3039 fres = dwarf_formblock (attrib,&tempb, &fblkerr);
3040 if (fres != DW_DLV_OK) {
3041 print_error_and_continue(
3042 "DW_FORM_blockn cannot get block\n",
3043 fres,fblkerr);
3044 DROP_ERROR_INSTANCE(dbg,fres,fblkerr);
3045 return;
3046 }
3047
3048 fres = dwarf_uncompress_integer_block_a(dbg,
3049 tempb->bl_len,
3050 (void *)tempb->bl_data,
3051 &array_len,&array,&fblkerr);
3052 /* uncompress block into 32bit signed int array.
3053 It's really a block of sleb numbers so the
3054 compression is minor unless the values
3055 are close to zero. */
3056 if (fres != DW_DLV_OK) {
3057 dwarf_dealloc(dbg,tempb,DW_DLA_BLOCK);
3058 print_error_and_continue(
3059 "DW_AT_SUN_func_offsets cannot uncompress data\n",
3060 0,fblkerr);
3061 DROP_ERROR_INSTANCE(dbg,fres,fblkerr);
3062 dwarf_dealloc_uncompressed_block(dbg, array);
3063 return;
3064 }
3065 if (array_len == 0) {
3066 dwarf_dealloc_uncompressed_block(dbg, array);
3067 print_error_and_continue(
3068 "DW_AT_SUN_func_offsets has no data (array"
3069 " length is zero), something badly"
3070 " wrong",
3071 DW_DLV_OK,fblkerr);
3072 dwarf_dealloc(dbg,tempb,DW_DLA_BLOCK);
3073 return;
3074 }
3075
3076 /* fill in string buffer */
3077 next = 0;
3078 while (next < array_len) {
3079 unsigned i = 0;
3080 /* Print a full line */
3081 esb_append(esbp,"\n ");
3082 for (i = 0 ; i < 2 && next < array_len; ++i,++next) {
3083 Dwarf_Signed vs = array[next];
3084 Dwarf_Unsigned vu = (Dwarf_Unsigned)vs;
3085
3086 if (i== 1) {
3087 esb_append(esbp," ");
3088 }
3089 esb_append_printf_i(esbp,"%6" DW_PR_DSd " ",vs);
3090 esb_append_printf_u(esbp,
3091 "(0x%" DW_PR_XZEROS DW_PR_DUx ")",vu);
3092 }
3093 }
3094 dwarf_dealloc(dbg,tempb,DW_DLA_BLOCK);
3095 /* free array buffer */
3096 dwarf_dealloc_uncompressed_block(dbg, array);
3097 }
3098
3099 static const char *
3100 get_rangelist_type_descr(Dwarf_Ranges *r)
3101 {
3102 switch (r->dwr_type) {
3103 case DW_RANGES_ENTRY: return "range entry";
3104 case DW_RANGES_ADDRESS_SELECTION: return "base address";
3105 case DW_RANGES_END: return "range end";
3106 default: break;
3107 }
3108 /* Impossible. */
3109 return "ERROR: .debug_ranges range type Unknown";
3110 }
3111
3112 /* The string produced here will need to be passed
3113 through sanitized() before actually printing.
3114 Always returns DW_DLV_OK. */
3115 int
3116 print_ranges_list_to_extra(Dwarf_Debug dbg,
3117 Dwarf_Die die /* may be NULL if unknown */,
3118 Dwarf_Unsigned originaloff,
3119 Dwarf_Unsigned finaloff,
3120 Dwarf_Ranges *rangeset,
3121 Dwarf_Signed rangecount,
3122 Dwarf_Unsigned bytecount,
3123 struct esb_s *stringbuf)
3124 {
3125 const char *sec_name = 0;
3126 int res = 0;
3127 Dwarf_Signed i = 0;
3128 struct esb_s truename;
3129 char buf[ESB_FIXED_ALLOC_SIZE];
3130 Dwarf_Unsigned base_address = 0;
3131 Dwarf_Bool have_base_addr = FALSE;
3132 Dwarf_Error grberr = 0;
3133 Dwarf_Bool have_ranges_offset = FALSE;
3134 Dwarf_Unsigned ranges_offset = 0;
3135
3136 esb_constructor_fixed(&truename,buf,sizeof(buf));
3137 /* We don't want to set the compress data into
3138 the secname here. */
3139 get_true_section_name(dbg,".debug_ranges",
3140 &truename,FALSE);
3141 sec_name = esb_get_string(&truename);
3142 if (die) {
3143 res = dwarf_get_ranges_baseaddress(dbg,die,
3144 &have_base_addr, &base_address,
3145 &have_ranges_offset, &ranges_offset,
3146 &grberr);
3147 if (res == DW_DLV_ERROR) {
3148 dwarf_dealloc_error(dbg,grberr);
3149 grberr = 0;
3150 }
3151 }
3152 if (glflags.dense) {
3153 /* for dense, ignore cooked values. */
3154 esb_append_printf_i(stringbuf,"< ranges: %"
3155 DW_PR_DSd,rangecount);
3156 esb_append_printf_s(stringbuf," ranges at %s" ,
3157 sanitized(sec_name));
3158 if (have_ranges_offset) {
3159 esb_append_printf_u(stringbuf," global offset %"
3160 DW_PR_DUu , finaloff);
3161 esb_append_printf_u(stringbuf," without base %"
3162 DW_PR_DUu , originaloff);
3163 } else {
3164 esb_append_printf_u(stringbuf,
3165 " offset base %" DW_PR_DUu ,
3166 finaloff);
3167 }
3168 esb_append_printf_u(stringbuf," (0x%"
3169 DW_PR_XZEROS DW_PR_DUx ") ",
3170 finaloff);
3171 if (have_base_addr) {
3172 esb_append_printf_u(stringbuf," (base addr 0x%"
3173 DW_PR_XZEROS DW_PR_DUx ") ",
3174 base_address);
3175 }
3176 esb_append_printf_u(stringbuf,"(%" DW_PR_DUu
3177 " bytes)>",
3178 bytecount);
3179 } else {
3180 esb_append_printf_i(stringbuf," ranges: %" DW_PR_DSd,
3181 rangecount);
3182 esb_append_printf_s(stringbuf," at %s" ,
3183 sanitized(sec_name));
3184 esb_append_printf_u(stringbuf," global offset %"
3185 DW_PR_DUu , finaloff);
3186 esb_append_printf_u(stringbuf," (0x%"
3187 DW_PR_XZEROS DW_PR_DUx ") ",
3188 finaloff);
3189 if (have_ranges_offset) {
3190 esb_append_printf_u(stringbuf," without base %"
3191 DW_PR_DUu , originaloff);
3192 esb_append_printf_u(stringbuf," (0x%"
3193 DW_PR_XZEROS DW_PR_DUx ") ",
3194 originaloff);
3195 }
3196 if (have_base_addr) {
3197 esb_append_printf_u(stringbuf," ( base addr 0x%"
3198 DW_PR_XZEROS DW_PR_DUx ") ",
3199 base_address);
3200 }
3201 esb_append_printf_u(stringbuf,"(%" DW_PR_DUu
3202 " bytes)\n",
3203 bytecount);
3204 }
3205 for (i = 0; i < rangecount; ++i) {
3206 Dwarf_Ranges * r = rangeset +i;
3207 const char *type = get_rangelist_type_descr(r);
3208 if (glflags.dense) {
3209 /* For dense, ignore cooked values */
3210 esb_append_printf_i(stringbuf,"<[%2" DW_PR_DSd,i);
3211 esb_append_printf_s(stringbuf,"] %s",type);
3212 esb_append_printf_u(stringbuf," 0x%"
3213 DW_PR_XZEROS DW_PR_DUx,r->dwr_addr1);
3214 esb_append_printf_u(stringbuf," 0x%"
3215 DW_PR_XZEROS DW_PR_DUx ">",r->dwr_addr2);
3216 } else {
3217 /* See DWARF5 page 53, line 17
3218 the addresses match so nothing is there and
3219 the address itself is irrelevant. */
3220 esb_append_printf_i(stringbuf," [%2" DW_PR_DSd,i);
3221 esb_append_printf_s(stringbuf,"] %-12s",type);
3222
3223 switch(r->dwr_type) {
3224 case DW_RANGES_ENTRY: {
3225 Dwarf_Bool emptyrange =
3226 (r->dwr_type == DW_RANGES_ENTRY
3227 && r->dwr_addr1 == r->dwr_addr2);
3228
3229 esb_append_printf_u(stringbuf,
3230 " raw: 0x%08x",r->dwr_addr1);
3231 esb_append_printf_u(stringbuf,
3232 ", 0x%08x",r->dwr_addr2);
3233 if (emptyrange) {
3234 esb_append(stringbuf," (empty range)");
3235 }
3236 if (have_base_addr) {
3237 /* See DWARF4 2.17.3 page 38 and 39 */
3238 esb_append_printf_u(stringbuf,
3239 " cooked: 0x%08x",
3240 r->dwr_addr1 +base_address);
3241 esb_append_printf_u(stringbuf,
3242 ", 0x%08x",
3243 r->dwr_addr2 +base_address);
3244 }
3245 break;
3246 }
3247 case DW_RANGES_ADDRESS_SELECTION:
3248 esb_append_printf_u(stringbuf,
3249 " 0x%08x",
3250 r->dwr_addr2);
3251 have_base_addr = TRUE;
3252 base_address = r->dwr_addr2;
3253 break;
3254 case DW_RANGES_END:
3255 esb_append(stringbuf,
3256 " 0,0");
3257 have_base_addr = FALSE;
3258 base_address = 0;
3259 break;
3260 default:
3261 esb_append_printf_u(stringbuf,
3262 "ERROR: Impossible entry from .debug_ranges, "
3263 "incorrect dwr_type is 0x%x\n",
3264 r->dwr_type);
3265 break;
3266 }
3267 esb_append(stringbuf,"\n");
3268 }
3269 }
3270 esb_destructor(&truename);
3271 return DW_DLV_OK;
3272 }
3273
3274 void
3275 dd_do_dump_visited_info(int level, Dwarf_Off loff,Dwarf_Off goff,
3276 Dwarf_Off cu_die_goff,
3277 const char *atname, const char *valname)
3278 {
3279 printf("<%2d><0x%" DW_PR_XZEROS DW_PR_DUx
3280 " GOFF=0x%" DW_PR_XZEROS DW_PR_DUx
3281 " CU-GOFF=0x%" DW_PR_XZEROS DW_PR_DUx
3282 "> ",
3283 level, loff, goff,cu_die_goff);
3284 print_indent_prefix(0,level,2);
3285 printf("%s -> %s\n",atname,valname);
3286 }
3287
3288 /* Always returns DW_DLV_OK.
3289 Expected attr is DW_AT_decl_file or DW_AT_call_file */
3290 static int
3291 turn_file_num_to_string(Dwarf_Debug dbg,
3292 Dwarf_Die die,
3293 Dwarf_Attribute attrib,
3294 Dwarf_Half theform,
3295 Dwarf_Half dwversion,
3296 Dwarf_Unsigned filenum,/* decl_file number */
3297 char **srcfiles,
3298 Dwarf_Signed srcfiles_cnt,
3299 struct esb_s * esbp,
3300 Dwarf_Error *err)
3301 {
3302 Dwarf_Half offset_size=0;
3303 int vres = 0;
3304 char declmsgbuf[ESB_FIXED_ALLOC_SIZE];
3305 struct esb_s declmsg;
3306 char *fname = 0;
3307 Dwarf_Half attrnum = 0;
3308
3309 vres = dwarf_whatattr(attrib,&attrnum,err);
3310 if (vres != DW_DLV_OK) {
3311 struct esb_s m;
3312
3313 esb_constructor(&m);
3314 esb_append(&m,
3315 "ERROR: Cannot get DIE context "
3316 "attribute number for attr ");
3317 esb_append_printf_s(&m,
3318 "form %s ",
3319 get_FORM_name(theform, FALSE));
3320 print_error_and_continue(
3321 esb_get_string(&m),vres,*err);
3322 esb_destructor(&m);
3323 if (vres == DW_DLV_ERROR) {
3324 DROP_ERROR_INSTANCE(dbg,vres,*err);
3325 }
3326 return DW_DLV_OK;
3327 }
3328 vres = dwarf_get_version_of_die(die,
3329 &dwversion,&offset_size);
3330 if (vres != DW_DLV_OK) {
3331 struct esb_s m;
3332
3333 esb_constructor(&m);
3334 esb_append_printf_s(&m,
3335 "ERROR: Cannot get DIE context "
3336 "version number for attr %s ",
3337 get_AT_name(attrnum, FALSE));
3338 esb_append_printf_s(&m,
3339 "form %s ",
3340 get_FORM_name(theform, FALSE));
3341 print_error_and_continue(
3342 esb_get_string(&m),vres,*err);
3343 esb_destructor(&m);
3344 if (vres == DW_DLV_ERROR) {
3345 DROP_ERROR_INSTANCE(dbg,vres,*err);
3346 }
3347 return DW_DLV_OK;
3348 }
3349 esb_constructor_fixed(&declmsg,declmsgbuf,
3350 sizeof(declmsgbuf));
3351 if (!srcfiles) {
3352 if (glflags.verbose > 2) {
3353 esb_append_printf_s(&declmsg,
3354 " <%s file index ",
3355 get_AT_name(attrnum,FALSE));
3356 esb_append_printf_u(&declmsg,
3357 "%" DW_PR_DUu,filenum);
3358 esb_append(&declmsg," No file list for CU>");
3359 esb_append(esbp, " ");
3360 esb_append(esbp,
3361 esb_get_string(&declmsg));
3362 }
3363 esb_destructor(&declmsg);
3364 return DW_DLV_OK;
3365 } else {
3366 Dwarf_Unsigned localud = filenum;
3367 Dwarf_Bool done = FALSE;
3368
3369 if (dwversion == DWVERSION5) {
3370 if ( localud < (Dwarf_Unsigned)srcfiles_cnt) {
3371 fname = srcfiles[localud];
3372 esb_append(&declmsg,fname);
3373 done = TRUE;
3374 }
3375 } else {
3376 if (!localud) {
3377 /* Just print the number,
3378 there is no name. */
3379 done=TRUE;
3380 } else if (localud &&
3381 localud <= (Dwarf_Unsigned)srcfiles_cnt) {
3382 fname = srcfiles[localud - 1];
3383 esb_append(&declmsg,fname);
3384 done = TRUE;
3385 }
3386 }
3387 if (!done) {
3388 esb_append_printf_s(&declmsg,
3389 " <%s file index ",
3390 get_AT_name(attrnum,FALSE));
3391 esb_append_printf_u(&declmsg,
3392 "%" DW_PR_DUu,filenum);
3393 esb_append(&declmsg," out of range>");
3394 }
3395 }
3396 if (fname) {
3397 esb_append(esbp, " ");
3398 esb_append(esbp,
3399 esb_get_string(&declmsg));
3400 }
3401 esb_destructor(&declmsg);
3402 return DW_DLV_OK;
3403 }
3404
3405 static void
3406 append_useful_die_name(Dwarf_Debug dbg,
3407 Dwarf_Die die,
3408 char **srcfiles,
3409 Dwarf_Signed srcfiles_cnt,
3410 struct esb_s *outstr,
3411 Dwarf_Error *err)
3412 {
3413 int res = 0;
3414 Dwarf_Attribute nattr = 0;
3415 Dwarf_Half nattr_form = 0;
3416 Dwarf_Attribute lattr = 0;
3417 Dwarf_Unsigned filenum = 0;
3418 Dwarf_Unsigned linenum = 0;
3419 Dwarf_Half version = 0;
3420 Dwarf_Half offset_size = 0;
3421
3422 res = dwarf_get_version_of_die(die,&version,&offset_size);
3423 if (res != DW_DLV_OK) {
3424 /* FAIL. */
3425 return;
3426 }
3427 res = dwarf_attr(die,DW_AT_decl_file, &nattr,err);
3428 if (res != DW_DLV_OK) {
3429 if (res == DW_DLV_ERROR) {
3430 DROP_ERROR_INSTANCE(dbg,res,*err);
3431 }
3432 return;
3433 }
3434 res =dwarf_whatform(nattr,&nattr_form,err);
3435 if (res == DW_DLV_ERROR) {
3436 DROP_ERROR_INSTANCE(dbg,res,*err);
3437 }
3438 if (res != DW_DLV_OK) {
3439 if (res == DW_DLV_ERROR) {
3440 DROP_ERROR_INSTANCE(dbg,res,*err);
3441 }
3442 dwarf_dealloc_attribute(nattr);
3443 return;
3444 }
3445 res = dwarf_attr(die,DW_AT_decl_line,&lattr,err);
3446 if (res != DW_DLV_OK) {
3447 if (res == DW_DLV_ERROR) {
3448 DROP_ERROR_INSTANCE(dbg,res,*err);
3449 }
3450 linenum = 0;
3451 } else {
3452 res = dwarf_formudata(lattr,&linenum,err);
3453 if (res != DW_DLV_OK) {
3454 if (res == DW_DLV_ERROR) {
3455 DROP_ERROR_INSTANCE(dbg,res,*err);
3456 }
3457 linenum = 0;
3458 }
3459 }
3460 dwarf_dealloc_attribute(lattr);
3461 lattr = 0;
3462 turn_file_num_to_string(dbg,die,
3463 nattr,
3464 nattr_form,
3465 version,filenum, srcfiles,srcfiles_cnt,
3466 outstr,
3467 err);
3468 if (linenum) {
3469 esb_append_printf_u(outstr,
3470 " line: %u",linenum);
3471 }
3472 dwarf_dealloc_attribute(nattr);
3473 return;
3474 }
3475
3476 /* So far, designed for DW_AT_type with a Dwarf_Sig8
3477 reference. Does not yet look at tied object
3478 even if one is present.
3479 Does not currently attempt to use any signature
3480 resolution fast-access DWARF sections.
3481 */
3482 int
3483 dd_print_sig8_target(Dwarf_Debug dbg,
3484 Dwarf_Attribute attrib,
3485 int die_indent_level,
3486 char **srcfiles,
3487 Dwarf_Signed srcfiles_cnt,
3488 struct esb_s * valname,
3489 Dwarf_Error *err)
3490 {
3491 Dwarf_Sig8 signature;
3492 Dwarf_Die targdie = 0;
3493 Dwarf_Bool targ_is_info = FALSE;
3494 Dwarf_Half targtag = 0;
3495 Dwarf_Unsigned targ_goff = 0;
3496 const char * targtagname = 0;
3497 char * targdiename = 0;
3498 int res = 0;
3499
3500 res = dwarf_formsig8(attrib,&signature,err);
3501 if (res == DW_DLV_ERROR) {
3502 print_error_and_continue(
3503 "dwarf_formsig8 fails in "
3504 "attribute traversal",
3505 res, *err);
3506 return res;
3507 }
3508 if (res == DW_DLV_NO_ENTRY) {
3509 return res;
3510 }
3511 res = dwarf_find_die_given_sig8(dbg,
3512 &signature, &targdie,
3513 &targ_is_info,err);
3514 if (res == DW_DLV_ERROR) {
3515 print_error_and_continue(
3516 "dwarf_find_die_given_sig8 fails "
3517 "following a signature in "
3518 "attribute traversal",
3519 res, *err);
3520 return res;
3521 }
3522 if (res == DW_DLV_NO_ENTRY) {
3523 /* We did not find the target */
3524 return res;
3525 }
3526 res = dwarf_dieoffset(targdie, &targ_goff, err);
3527 if (res == DW_DLV_ERROR) {
3528 print_error_and_continue(
3529 "dwarf_dieoffset fails following a signature in "
3530 "attribute traversal",
3531 res, *err);
3532 dwarf_dealloc_die(targdie);
3533 return res;
3534 }
3535 if (res == DW_DLV_OK) {
3536 if (!glflags.dense) {
3537 esb_append(valname,"\n");
3538 append_indent_prefix(valname,standard_indent(),
3539 die_indent_level,0);
3540 }
3541 esb_append_printf_u(valname,
3542 " <Target GOFF=0x%08x",targ_goff);
3543 }
3544 esb_append_printf_s(valname,
3545 " in section %s",
3546 targ_is_info?".debug_info":".debug_types");
3547 res = dwarf_tag(targdie,&targtag,err);
3548 if (res == DW_DLV_ERROR) {
3549 print_error_and_continue(
3550 "dwarf_tag fails following a signature in "
3551 "attribute traversal",
3552 res, *err);
3553 dwarf_dealloc_die(targdie);
3554 return res;
3555 }
3556 if (res == DW_DLV_OK) {
3557 esb_append_printf_u(valname,
3558 " TAG: 0x%02x ",targtag);
3559 }
3560 targtagname = get_TAG_name(targtag, FALSE);
3561 esb_append_printf_s(valname,
3562 "(%s) ",targtagname);
3563 res = dwarf_diename(targdie,&targdiename,err);
3564 if (res == DW_DLV_ERROR) {
3565 print_error_and_continue(
3566 "dwarf_tag fails following a signature in "
3567 "attribute traversal",
3568 res, *err);
3569 dwarf_dealloc_die(targdie);
3570 return res;
3571 }
3572 if (res == DW_DLV_NO_ENTRY) {
3573 append_useful_die_name(dbg,targdie,
3574 srcfiles,srcfiles_cnt,valname,err);
3575 }
3576 if (res == DW_DLV_OK) {
3577 esb_append_printf_s(valname,
3578 "name: %s",targdiename);
3579 }
3580 if (!glflags.dense) {
3581 esb_append(valname,">\n");
3582 }
3583 /* If we get here we extablished the target. */
3584 dwarf_dealloc_die(targdie);
3585 return DW_DLV_OK;
3586 }
3587
3588 static Dwarf_Bool
3589 is_form_block(int form)
3590 {
3591 if (form == DW_FORM_block1 ||
3592 form == DW_FORM_block2 ||
3593 form == DW_FORM_block4 ||
3594 form == DW_FORM_block) {
3595 return TRUE;
3596 }
3597 return FALSE;
3598 }
3599 /* DW_FORM_data16 should not apply here. */
3600 static Dwarf_Bool
3601 is_simple_location_expr(int form)
3602 {
3603 if (is_form_block(form)) {
3604 return TRUE;
3605 }
3606 if (form == DW_FORM_exprloc) {
3607 return TRUE;
3608 }
3609 return FALSE;
3610 }
3611 static Dwarf_Bool
3612 is_location_form(int form)
3613 {
3614 if (form == DW_FORM_data4 ||
3615 form == DW_FORM_data8 ||
3616 form == DW_FORM_sec_offset ||
3617 form == DW_FORM_loclistx ||
3618 form == DW_FORM_rnglistx ) {
3619 return TRUE;
3620 }
3621 return FALSE;
3622 }
3623
3624 static void
3625 show_attr_form_error(unsigned attr,
3626 unsigned form,
3627 struct esb_s *out)
3628 {
3629 const char *n = 0;
3630 int res = 0;
3631 Dwarf_Error formerr = 0;
3632
3633 esb_append(out,"ERROR: Attribute ");
3634 esb_append_printf_u(out,"%u",attr);
3635
3636 esb_append(out," (");
3637 res = dwarf_get_AT_name(attr,&n);
3638 if (res != DW_DLV_OK) {
3639 n = "UknownAttribute";
3640 }
3641 esb_append(out,n);
3642 esb_append(out,") ");
3643 esb_append(out," has form ");
3644 esb_append_printf_u(out,"%u",form);
3645 esb_append(out," (");
3646 esb_append(out,get_FORM_name(form,FALSE));
3647 esb_append(out,"), a form which is not appropriate");
3648 print_error_and_continue(
3649 esb_get_string(out), DW_DLV_OK,formerr);
3650 }
3651
3652 /* Traverse an attribute and following any reference
3653 in order to detect self references to DIES (loop).
3654 We do not use print_else_name_match here. Just looking
3655 for self references to report on. */
3656 static int
3657 traverse_attribute(Dwarf_Debug dbg, Dwarf_Die die,
3658 Dwarf_Off dieprint_cu_goffset,
3659 Dwarf_Bool is_info,
3660 Dwarf_Half attr,
3661 Dwarf_Attribute attr_in,
3662 char **srcfiles, Dwarf_Signed srcfcnt,
3663 int die_indent_level,
3664 Dwarf_Error *err)
3665 {
3666 Dwarf_Attribute attrib = 0;
3667 const char * atname = 0;
3668 int tres = 0;
3669 Dwarf_Half tag = 0;
3670 struct esb_s valname;
3671
3672 esb_constructor(&valname);
3673 is_info = dwarf_get_die_infotypes_flag(die);
3674 atname = get_AT_name(attr,pd_dwarf_names_print_on_error);
3675
3676 /* The following gets the real attribute,
3677 even in the face of an
3678 incorrect doubling, or worse, of attributes. */
3679 attrib = attr_in;
3680 /* Do not get attr via dwarf_attr: if there are
3681 (erroneously)
3682 multiple of an attr in a DIE, dwarf_attr will
3683 not get the
3684 second, erroneous one and dwarfdump will
3685 print the first one
3686 multiple times. Oops. */
3687
3688 tres = dwarf_tag(die, &tag, err);
3689 if (tres == DW_DLV_ERROR) {
3690 print_error_and_continue(
3691 "Cannot get DIE tag in traverse_attribute!",
3692 tres,*err);
3693 esb_destructor(&valname);
3694 return tres;
3695 } else if (tres == DW_DLV_NO_ENTRY) {
3696 tag = 0;
3697 } else {
3698 /* ok */
3699 }
3700
3701 switch (attr) {
3702 case DW_AT_specification:
3703 case DW_AT_containing_type:
3704 case DW_AT_abstract_origin:
3705 case DW_AT_object_pointer:
3706 case DW_AT_small:
3707 case DW_AT_extension:
3708 case DW_AT_priority:
3709 case DW_AT_namelist_item:
3710 case DW_AT_friend:
3711 case DW_AT_type: {
3712 int res = 0;
3713 Dwarf_Off die_goff = 0;
3714 Dwarf_Off ref_goff = 0;
3715 Dwarf_Die ref_die = 0;
3716 struct esb_s specificationstr;
3717 Dwarf_Half theform = 0;
3718 Dwarf_Half directform = 0;
3719 char buf[ESB_FIXED_ALLOC_SIZE];
3720
3721 res = get_form_values(attrib,&theform,
3722 &directform,err);
3723 if (res != DW_DLV_OK) {
3724 esb_destructor(&valname);
3725 return res;
3726 }
3727 if (!dd_form_refers_local_info(theform)) {
3728 break;
3729 }
3730 esb_constructor_fixed(&specificationstr,buf,sizeof(buf));
3731 ++die_indent_level;
3732 if (die_indent_level >= DIE_STACK_SIZE ) {
3733 esb_destructor(&valname);
3734 report_die_stack_error(dbg,err);
3735 return DW_DLV_ERROR;
3736 }
3737 res = get_attr_value(dbg, tag, die,
3738 dieprint_cu_goffset,
3739 attrib, srcfiles, srcfcnt,
3740 &specificationstr,glflags.show_form_used,
3741 glflags.verbose, err);
3742 if (res != DW_DLV_OK) {
3743 esb_destructor(&valname);
3744 return res;
3745 }
3746 esb_append(&valname, esb_get_string(&specificationstr));
3747 esb_destructor(&specificationstr);
3748
3749 /* Get the global offset for reference */
3750 if (theform == DW_FORM_ref_sig8) {
3751 res = dd_print_sig8_target(dbg,attrib,
3752 die_indent_level,
3753 srcfiles,srcfcnt,
3754 &valname,err);
3755 if (res == DW_DLV_ERROR) {
3756 esb_destructor(&valname);
3757 return res;
3758 }
3759 break;
3760 }
3761 res = dwarf_global_formref_b(attrib, &ref_goff,
3762 &is_info, err);
3763 if (res == DW_DLV_ERROR) {
3764 /* errno > 0 , < 1000 */
3765 int dwerrno = (int)dwarf_errno(*err);
3766 if (dwerrno == DW_DLE_REF_SIG8_NOT_HANDLED ) {
3767 /* No need to stop, ref_sig8 refers out of
3768 the current section. */
3769 DROP_ERROR_INSTANCE(dbg,res,*err);
3770 break;
3771 } else {
3772 print_error_and_continue(
3773 "dwarf_global_formref_b fails in "
3774 "attribute traversal",
3775 res, *err);
3776 esb_destructor(&valname);
3777 return res;
3778 }
3779 }
3780 if (res == DW_DLV_NO_ENTRY) {
3781 return res;
3782 }
3783 /* Gives die offset in section. */
3784 res = dwarf_dieoffset(die, &die_goff, err);
3785 if (res == DW_DLV_ERROR) {
3786 /* errno > 0 , < 1000 */
3787 int dwerrno = (int)dwarf_errno(*err);
3788 if (dwerrno == DW_DLE_REF_SIG8_NOT_HANDLED ) {
3789 /* No need to stop, ref_sig8 refers out of
3790 the current section. */
3791 DROP_ERROR_INSTANCE(dbg,res,*err);
3792 break;
3793 } else {
3794 print_error_and_continue(
3795 "dwarf_dieoffset fails in "
3796 " attribute traversal", res, *err);
3797 esb_destructor(&valname);
3798 return res;
3799 }
3800 }
3801
3802 /* Follow reference chain, looking for self references */
3803 res = dwarf_offdie_b(dbg,ref_goff,is_info,&ref_die,err);
3804 if (res == DW_DLV_ERROR) {
3805 print_error_and_continue(
3806 "dwarf_dieoff_b fails in "
3807 " attribute traversal", res, *err);
3808 esb_destructor(&valname);
3809 return res;
3810 }
3811 if (res == DW_DLV_OK) {
3812 Dwarf_Off target_die_cu_goff = 0;
3813
3814 if (dump_visited_info) {
3815 Dwarf_Off die_loff = 0;
3816
3817 res = dwarf_die_CU_offset(die, &die_loff, err);
3818 if (res != DW_DLV_OK) {
3819 esb_destructor(&valname);
3820 return res;
3821 }
3822 dd_do_dump_visited_info(die_indent_level,
3823 die_loff,die_goff,
3824 dieprint_cu_goffset,
3825 atname,esb_get_string(&valname));
3826 }
3827 ++die_indent_level;
3828 if (die_indent_level >= DIE_STACK_SIZE ) {
3829 report_die_stack_error(dbg,err);
3830 esb_destructor(&valname);
3831 return DW_DLV_ERROR;
3832 }
3833 res =dwarf_CU_dieoffset_given_die(ref_die,
3834 &target_die_cu_goff, err);
3835 if (res != DW_DLV_OK) {
3836 print_error_and_continue(
3837 "dwarf_dieoffset() accessing cu_goff "
3838 "die fails in traversal",
3839 res,*err);
3840 esb_destructor(&valname);
3841 return res;
3842 }
3843 res = dd_traverse_one_die(dbg,attrib,ref_die,
3844 target_die_cu_goff,
3845 is_info,
3846 srcfiles,srcfcnt,die_indent_level,
3847 err);
3848 DeleteKeyInBucketGroup(glflags.pVisitedInfo,ref_goff);
3849 dwarf_dealloc_die(ref_die);
3850 if (res == DW_DLV_ERROR) {
3851 esb_destructor(&valname);
3852 return res;
3853 }
3854 --die_indent_level;
3855 ref_die = 0;
3856 }
3857 }
3858 break;
3859 default: break;
3860 } /* End switch. */
3861 esb_destructor(&valname);
3862 return DW_DLV_OK;
3863 }
3864
3865 /* Traverse one DIE in order to detect
3866 self references to DIES.
3867
3868 This fails to deal with changing CUs via global
3869 references so srcfiles and cnt
3870 have possibly inappropriate values. FIXME.
3871 */
3872 int
3873 dd_traverse_one_die(Dwarf_Debug dbg,
3874 Dwarf_Attribute attrib,
3875 Dwarf_Die die,
3876 Dwarf_Off dieprint_cu_goffset,
3877 Dwarf_Bool is_info,
3878 char **srcfiles, Dwarf_Signed cnt,
3879 int die_indent_level,
3880 Dwarf_Error *err)
3881 {
3882 Dwarf_Half tag = 0;
3883 Dwarf_Off overall_offset = 0;
3884 Dwarf_Signed atcnt = 0;
3885 int res = 0;
3886
3887 res = dwarf_tag(die, &tag, err);
3888 if (res != DW_DLV_OK) {
3889 print_error_and_continue(
3890 "Problem accessing tag of die!"
3891 " from traverse_one_die", res, *err);
3892 return res;
3893 }
3894 res = dwarf_dieoffset(die, &overall_offset, err);
3895 if (res != DW_DLV_OK) {
3896 print_error_and_continue(
3897 "dwarf_dieoffset fails in traversing die",
3898 res, *err);
3899 return res;
3900 }
3901
3902 if (dump_visited_info) {
3903 Dwarf_Off offset = 0;
3904 const char * tagname = 0;
3905 res = dwarf_die_CU_offset(die, &offset, err);
3906 if (res != DW_DLV_OK) {
3907 print_error_and_continue(
3908 "dwarf_die_CU_offset fails in traversing die",
3909 res, *err);
3910 return res;
3911 }
3912 tagname = get_TAG_name(tag,pd_dwarf_names_print_on_error);
3913 dd_do_dump_visited_info(die_indent_level,offset,
3914 overall_offset, dieprint_cu_goffset,
3915 tagname,"");
3916 }
3917
3918 DWARF_CHECK_COUNT(self_references_result,1);
3919 if (FindKeyInBucketGroup(glflags.pVisitedInfo,
3920 overall_offset)) {
3921 char * localvaln = NULL;
3922 Dwarf_Half attr = 0;
3923 struct esb_s bucketgroupstr;
3924 const char *atname = NULL;
3925
3926 esb_constructor(&bucketgroupstr);
3927 res = get_attr_value(dbg, tag, die,
3928 dieprint_cu_goffset,
3929 attrib, srcfiles,
3930 cnt, &bucketgroupstr,
3931 glflags.show_form_used, glflags.verbose,err);
3932 if (res != DW_DLV_OK) {
3933 return res;
3934 }
3935 localvaln = esb_get_string(&bucketgroupstr);
3936
3937 res = dwarf_whatattr(attrib, &attr, err);
3938 if (res != DW_DLV_OK) {
3939 print_error_and_continue(
3940 "ERROR: dwarf_whatattr fails in traverse die",
3941 res,*err);
3942 return res;
3943 }
3944 atname = get_AT_name(attr,pd_dwarf_names_print_on_error);
3945
3946 /* We have a self reference */
3947 DWARF_CHECK_ERROR3(self_references_result,
3948 "Invalid self reference to DIE: ",atname,localvaln);
3949 esb_destructor(&bucketgroupstr);
3950 } else {
3951 Dwarf_Signed i = 0;
3952 Dwarf_Attribute *atlist = 0;
3953
3954 /* Add current DIE */
3955 AddEntryIntoBucketGroup(glflags.pVisitedInfo,
3956 overall_offset,
3957 0,0,0,NULL,FALSE);
3958
3959 res = dwarf_attrlist(die, &atlist, &atcnt, err);
3960 if (res == DW_DLV_ERROR) {
3961 print_error_and_continue(
3962 "dwarf_attrlist fails in traverse die",
3963 res, *err);
3964 return res;
3965 } else if (res == DW_DLV_NO_ENTRY) {
3966 /* indicates there are no attrs.
3967 It is not an error. */
3968 atcnt = 0;
3969 }
3970
3971 for (i = 0; i < atcnt; i++) {
3972 Dwarf_Half attr;
3973 int ares;
3974
3975 ares = dwarf_whatattr(atlist[i], &attr, err);
3976 if (ares == DW_DLV_OK) {
3977 ares = traverse_attribute(dbg, die,
3978 dieprint_cu_goffset,
3979 is_info,
3980 attr,
3981 atlist[i],
3982 srcfiles, cnt,
3983 die_indent_level,err);
3984 if (ares == DW_DLV_ERROR) {
3985 dealloc_local_atlist(dbg,atlist,atcnt);
3986 return ares;
3987 }
3988 } else {
3989 dealloc_local_atlist(dbg,atlist,atcnt);
3990 print_error_and_continue(
3991 "dwarf_whatattr entry missing in "
3992 " traverse die",
3993 ares, *err);
3994 return ares;
3995 }
3996 }
3997
3998 dealloc_local_atlist(dbg,atlist,atcnt);
3999 /* Delete current DIE */
4000 DeleteKeyInBucketGroup(glflags.pVisitedInfo,
4001 overall_offset);
4002 }
4003 return DW_DLV_OK;
4004 }
4005
4006 /* Extracted this from print_attribute()
4007 to get tolerable indents.
4008 In other words to make it readable.
4009 It uses global data fields excessively, but so does
4010 print_attribute().
4011 The majority of the code here is checking for
4012 compiler errors.
4013 Support for .debug_rnglists here is new May 2020.
4014 */
4015 static int
4016 print_range_attribute(Dwarf_Debug dbg,
4017 Dwarf_Die die,
4018 Dwarf_Half attr,
4019 Dwarf_Attribute attr_in,
4020 Dwarf_Half theform,
4021 int pra_dwarf_names_print_on_error,
4022 Dwarf_Bool print_else_name_match,
4023 int *append_extra_string,
4024 struct esb_s *esb_extrap,
4025 Dwarf_Error *raerr)
4026 {
4027 Dwarf_Unsigned original_off = 0;
4028 int fres = 0;
4029 Dwarf_Half cu_version = 2;
4030 Dwarf_Half cu_offset_size = 4;
4031 Dwarf_Bool is_info = TRUE;
4032
4033 fres = dwarf_get_version_of_die(die,&cu_version,
4034 &cu_offset_size);
4035 if (fres != DW_DLV_OK) {
4036 simple_err_return_msg_either_action(fres,
4037 "\nERROR: Unable to get version of a DIE "
4038 "to print a range attribute so something "
4039 " is badly wrong. Assuming DWARF2, offset size 4"
4040 " and continuing!");
4041 }
4042 if (theform == DW_FORM_rnglistx) {
4043 fres = dwarf_formudata(attr_in, &original_off, raerr);
4044 if (fres == DW_DLV_ERROR) {
4045 print_error_and_continue(
4046 "ERROR: In printing a range "
4047 "DW_FORM_rnglistx attribute "
4048 "dwarf_formudata failed ",fres,*raerr);
4049 return fres;
4050 }
4051 } else {
4052 fres = dwarf_global_formref_b(attr_in, &original_off,
4053 &is_info,
4054 raerr);
4055 if (fres == DW_DLV_ERROR) {
4056 print_error_and_continue(
4057 "ERROR: In printing a range attribute "
4058 "dwarf_global_formref_b failed ",fres,*raerr);
4059 return fres;
4060 }
4061 }
4062 if (fres == DW_DLV_OK && cu_version < DWVERSION5) {
4063 Dwarf_Ranges *rangeset = 0;
4064 Dwarf_Signed rangecount = 0;
4065 Dwarf_Unsigned bytecount = 0;
4066 Dwarf_Unsigned realoffset = 0;
4067 /* If this is a dwp the ranges will be
4068 missing or reported from a tied file.
4069 For now we add the ranges to dbg, not tiedbg
4070 as we do not mention tieddbg here.
4071 May need a new interface. FIXME?
4072 In the dwp case where the actual
4073 section is in tied (ie, a.out)
4074 the DW_AT_GNU_ranges_base
4075 is used, not the original_off.
4076 We really want to print the actual offset,
4077 in that case, not original_off.
4078 realoffset is that final actual section offset.
4079 */
4080 int rres = dwarf_get_ranges_b(dbg,original_off,
4081 die,
4082 &realoffset,
4083 &rangeset,
4084 &rangecount,&bytecount,raerr);
4085 if (rres == DW_DLV_OK) {
4086
4087 /* Ignore ranges inside a stripped function */
4088 if (!glflags.gf_suppress_checking_on_dwp &&
4089 glflags.gf_check_ranges &&
4090 glflags.in_valid_code &&
4091 checking_this_compiler()) {
4092 /* Record the offset, as the ranges check
4093 will be done at
4094 the end of the compilation unit;
4095 this approach solves
4096 the issue of DWARF4 generating values
4097 for the high pc
4098 as offsets relative to the low pc
4099 and the compilation
4100 unit having DW_AT_ranges attribute. */
4101 int dores = 0;
4102 Dwarf_Off die_glb_offset = 0;
4103 Dwarf_Off die_off = 0;
4104 dores = dwarf_die_offsets(die,&die_glb_offset,
4105 &die_off,raerr);
4106 if (dores == DW_DLV_ERROR) {
4107 dwarf_dealloc_ranges(dbg,rangeset,rangecount);
4108 return dores;
4109 }
4110 if (dores == DW_DLV_OK) {
4111 record_range_array_info_entry(die_glb_offset,
4112 realoffset);
4113 }
4114 }
4115 if (print_else_name_match) {
4116 *append_extra_string = 1;
4117 print_ranges_list_to_extra(dbg,die,
4118 original_off, realoffset,
4119 rangeset,rangecount,bytecount,
4120 esb_extrap);
4121 }
4122 dwarf_dealloc_ranges(dbg,rangeset,rangecount);
4123 } else if (rres == DW_DLV_ERROR) {
4124 if ( glflags.gf_suppress_checking_on_dwp) {
4125 /* Ignore checks */
4126 } else if ( glflags.gf_do_print_dwarf) {
4127 printf("\ndwarf_get_ranges_b() "
4128 "cannot find DW_AT_ranges at offset 0x%"
4129 DW_PR_XZEROS DW_PR_DUx
4130 " (0x%" DW_PR_XZEROS DW_PR_DUx ").",
4131 original_off,
4132 original_off);
4133 } else {
4134 DWARF_CHECK_COUNT(ranges_result,1);
4135 DWARF_CHECK_ERROR2(ranges_result,
4136 get_AT_name(attr,
4137 pra_dwarf_names_print_on_error),
4138 " cannot find DW_AT_ranges at offset");
4139 }
4140 return rres;
4141 } else {
4142 /* NO ENTRY */
4143 if ( glflags.gf_suppress_checking_on_dwp) {
4144 /* Ignore checks */
4145 } else if ( glflags.gf_do_print_dwarf) {
4146 printf("\ndwarf_get_ranges_b() "
4147 "finds no DW_AT_ranges at offset 0x%"
4148 DW_PR_XZEROS DW_PR_DUx
4149 " (%" DW_PR_DUu ").",
4150 original_off,
4151 original_off);
4152 } else {
4153 DWARF_CHECK_COUNT(ranges_result,1);
4154 DWARF_CHECK_ERROR2(ranges_result,
4155 get_AT_name(attr,
4156 pra_dwarf_names_print_on_error),
4157 " fails to find DW_AT_ranges at offset");
4158 }
4159 }
4160 return DW_DLV_OK;
4161 } else if (fres == DW_DLV_OK && cu_version >= DWVERSION5) {
4162 /* Here we have to access the .debug_rnglists section
4163 data with a new layout for DW5. Here we
4164 do not need to actually use rleoffset since it
4165 is identical to original_off. */
4166 int res = 0;
4167 Dwarf_Unsigned rleoffset = 0;
4168
4169 res = handle_rnglists( attr_in,
4170 theform,
4171 original_off,
4172 &rleoffset,
4173 esb_extrap,
4174 glflags.verbose,
4175 raerr);
4176 if (print_else_name_match) {
4177 *append_extra_string = 1;
4178 }
4179 return res;
4180 }
4181 /* DW_DLV_NO_ENTRY or DW_DLV_ERROR */
4182 if (glflags.gf_do_print_dwarf) {
4183 struct esb_s local;
4184 char tmp[ESB_FIXED_ALLOC_SIZE];
4185
4186 esb_constructor_fixed(&local,tmp,sizeof(tmp));
4187 esb_append(&local,
4188 " fails to find DW_AT_ranges offset");
4189 esb_append_printf_u(&local," attr 0x%x",attr);
4190 esb_append_printf_u(&local," form 0x%x",theform);
4191 printf(" %s ",esb_get_string(&local));
4192 esb_destructor(&local);
4193 } else {
4194 DWARF_CHECK_COUNT(ranges_result,1);
4195 DWARF_CHECK_ERROR2(ranges_result,
4196 get_AT_name(attr,
4197 pra_dwarf_names_print_on_error),
4198 " fails to find DW_AT_ranges offset");
4199 }
4200 return fres;
4201 }
4202
4203 /* A DW_AT_name in a CU DIE will likely have dots
4204 and be entirely sensible. So lets
4205 not call things a possible error when they are not.
4206 Some assemblers allow '.' in an identifier too.
4207
4208 This is a heuristic, not all that reliable.
4209 It is only used for a specific DWARF_CHECK_ERROR,
4210 and the 'altabi.' is from a specific
4211 (unnamed here) compiler.
4212
4213 Return 0 (FALSE) if it is a vaguely standard identifier.
4214 Else return 1 (TRUE), meaning 'it might be a file name
4215 or have '.' in it quite sensibly.'
4216
4217 If we don't do the TAG check we might report "t.c"
4218 as a questionable DW_AT_name. Which would be silly.
4219 */
4220 static Dwarf_Bool
4221 dot_ok_in_identifier(int tag,
4222 const char *val)
4223 {
4224 if (strncmp(val,"altabi.",7)) {
4225 /* Ignore the names of the form 'altabi.name',
4226 which apply to one specific compiler. */
4227 return TRUE;
4228 }
4229 if (tag == DW_TAG_compile_unit ||
4230 tag == DW_TAG_partial_unit ||
4231 tag == DW_TAG_imported_unit ||
4232 tag == DW_TAG_skeleton_unit ||
4233 tag == DW_TAG_type_unit) {
4234 return TRUE;
4235 }
4236 return FALSE;
4237 }
4238
4239 static void
4240 trim_quotes(const char *val,struct esb_s *es)
4241 {
4242 if (val[0] == '"') {
4243 size_t l = strlen(val);
4244 if (l > 2 && val[l-1] == '"') {
4245 esb_appendn(es,val+1,l-2);
4246 return;
4247 }
4248 }
4249 esb_append(es,val);
4250 }
4251
4252 static Dwarf_Bool
4253 have_a_search_match(const char *valname,const char *atname)
4254 {
4255 /* valname may have had quotes inserted,
4256 but search_match_text
4257 will not. So we need to use a new copy,
4258 not valname here. */
4259 char matchbuf[100]; /* not ESB_FIXED_ALLOC_SIZE ? */
4260 struct esb_s esb_match;
4261 char *s2;
4262 int res = 0;
4263
4264 esb_constructor_fixed(&esb_match,matchbuf,sizeof(matchbuf));
4265 trim_quotes(valname,&esb_match);
4266 s2 = esb_get_string(&esb_match);
4267 if (glflags.search_match_text ) {
4268 if (!strcmp(s2,glflags.search_match_text) ||
4269 !strcmp(atname,glflags.search_match_text)) {
4270
4271 esb_destructor(&esb_match);
4272 return TRUE;
4273 }
4274 }
4275 if (glflags.search_any_text) {
4276 if (is_strstrnocase(s2,glflags.search_any_text) ||
4277 is_strstrnocase(atname,glflags.search_any_text)) {
4278
4279 esb_destructor(&esb_match);
4280 return TRUE;
4281 }
4282 }
4283 if (glflags.search_regex_text) {
4284 res = dd_re_exec((char *)s2);
4285 if (res == DW_DLV_OK) {
4286 esb_destructor(&esb_match);
4287 return TRUE;
4288 }
4289 res = dd_re_exec((char *)atname);
4290 if (res == DW_DLV_OK) {
4291 esb_destructor(&esb_match);
4292 return TRUE;
4293 }
4294 }
4295 esb_destructor(&esb_match);
4296 return FALSE;
4297 }
4298
4299 /* Use our local die_stack to try to determine
4300 signedness of the DW_AT_discr_list
4301 LEB numbers. Returns -1 if we know
4302 it is signed. Returns 1 if we know it is
4303 unsigned. Returns 0 if we really do not know. */
4304 static int
4305 determine_discr_signedness(Dwarf_Debug dbg)
4306 {
4307 Dwarf_Die parent = 0;
4308 Dwarf_Half tag = 0;
4309 int tres = 0;
4310 Dwarf_Error descrerr = 0;
4311
4312 if (die_stack_indent_level < 1) {
4313 /* We have no idea. */
4314 return 0;
4315 }
4316 parent = die_stack[die_stack_indent_level -1].die_;
4317 if (!parent) {
4318 /* We have no idea. */
4319 return 0;
4320 }
4321 tres = dwarf_tag(parent, &tag, &descrerr);
4322 if (tres != DW_DLV_OK) {
4323 DROP_ERROR_INSTANCE(dbg,tres,descrerr);
4324 return 0;
4325 }
4326 if (tag != DW_TAG_variant_part) {
4327 return 0;
4328 }
4329 /* Expect DW_AT_discr or DW_AT_type here, and if
4330 DW_AT_discr, that might have the DW_AT_type. */
4331
4332 /* FIXME: For now lets just punt, say unsigned. */
4333 return 1;
4334 }
4335
4336 static void
4337 checksignv(
4338 struct esb_s *strout,
4339 const char *title,
4340 Dwarf_Signed sv,
4341 Dwarf_Unsigned uv)
4342 {
4343 /* The test and output are not entirely meaningful, but
4344 it can be useful for readers of dwarfdump output. */
4345 if (uv == (Dwarf_Unsigned)sv) {
4346 /* Nothing to do here. */
4347 return;
4348 }
4349 esb_append(strout," <");
4350 esb_append(strout,title);
4351 esb_append(strout," ");
4352 esb_append_printf_i(strout,"%" DW_PR_DSd ":",sv);
4353 esb_append_printf_u(strout,"%" DW_PR_DUu ">",uv);
4354 }
4355
4356 static int
4357 append_discr_array_vals(Dwarf_Dsc_Head h,
4358 Dwarf_Unsigned arraycount,
4359 int isunsigned,
4360 struct esb_s *strout,
4361 Dwarf_Error*paerr)
4362 {
4363 Dwarf_Unsigned u = 0;
4364 if (isunsigned == 0) {
4365 esb_append(strout,
4366 "<discriminant list signedness unknown>");
4367 }
4368 esb_append_printf_u(strout,
4369 "\n discr list array len: "
4370 "%" DW_PR_DUu "\n",arraycount);
4371 for (u = 0; u < arraycount; u++) {
4372 int u2res = 0;
4373 Dwarf_Half dtype = 0;
4374 Dwarf_Signed slow = 0;
4375 Dwarf_Signed shigh = 0;
4376 Dwarf_Unsigned ulow = 0;
4377 Dwarf_Unsigned uhigh = 0;
4378 const char *dsc_name = "";
4379
4380 u2res = dwarf_discr_entry_u(h,u,
4381 &dtype,&ulow,&uhigh,paerr);
4382 if (u2res == DW_DLV_ERROR) {
4383 print_error_and_continue(
4384 "DW_AT_discr_list entry access fail\n",
4385 u2res, *paerr);
4386 return u2res;
4387 }
4388 u2res = dwarf_discr_entry_s(h,u,
4389 &dtype,&slow,&shigh,paerr);
4390 if (u2res == DW_DLV_ERROR) {
4391 print_error_and_continue(
4392 "DW_AT_discr_list entry access fail\n",
4393 u2res, *paerr);
4394 }
4395 if (u2res == DW_DLV_NO_ENTRY) {
4396 glflags.gf_count_major_errors++;
4397 esb_append_printf_u(strout,
4398 "\n "
4399 "ERROR: discr index missing! %" DW_PR_DUu,u);
4400 break;
4401 }
4402 esb_append_printf_u(strout,
4403 " "
4404 "%" DW_PR_DUu ": ",u);
4405 dsc_name = get_DSC_name(dtype,
4406 pd_dwarf_names_print_on_error);
4407 esb_append(strout,sanitized(dsc_name));
4408 esb_append(strout," ");
4409 if (!dtype) {
4410 if (isunsigned < 0) {
4411 esb_append_printf_i(strout,"%" DW_PR_DSd,slow);
4412 checksignv(strout,"as signed:unsigned",slow,ulow);
4413 } else {
4414 esb_append_printf_u(strout,"%" DW_PR_DUu,ulow);
4415 checksignv(strout,"as signed:unsigned",slow,ulow);
4416 }
4417 } else {
4418 if (isunsigned < 0) {
4419 esb_append_printf_i(strout,"%" DW_PR_DSd,slow);
4420 checksignv(strout,"as signed:unsigned",slow,ulow);
4421 } else {
4422 esb_append_printf_u(strout,"%" DW_PR_DUu,ulow);
4423 checksignv(strout,"as signed:unsigned",slow,ulow);
4424 }
4425 if (isunsigned < 0) {
4426 esb_append_printf_i(strout,", %" DW_PR_DSd,shigh);
4427 checksignv(strout,"as signed:unsigned",
4428 shigh,uhigh);
4429 } else {
4430 esb_append_printf_u(strout,", %" DW_PR_DUu,uhigh);
4431 checksignv(strout,"as signed:unsigned",
4432 shigh,uhigh);
4433 }
4434 }
4435 esb_append(strout,"\n");
4436 }
4437 return DW_DLV_OK;
4438 }
4439
4440 static int
4441 print_location_description(Dwarf_Debug dbg,
4442 Dwarf_Attribute attrib,
4443 Dwarf_Die die,
4444 int checking,
4445 Dwarf_Half attr,
4446 int die_indent_level,
4447 struct esb_s *base,
4448 struct esb_s *details,
4449 Dwarf_Error *err)
4450 {
4451 /* The attribute is a location description
4452 or location list. */
4453 int res = 0;
4454 Dwarf_Half theform = 0;
4455 Dwarf_Half directform = 0;
4456 Dwarf_Half version = 0;
4457 Dwarf_Half offset_size = 0;
4458
4459 res = get_form_values(attrib,&theform,&directform,
4460 err);
4461 if (res == DW_DLV_ERROR) {
4462 return res;
4463 }
4464 res = dwarf_get_version_of_die(die,
4465 &version,&offset_size);
4466 if (is_simple_location_expr(theform)) {
4467 res = print_location_list(dbg, die, attrib,
4468 checking,
4469 die_indent_level,
4470 TRUE,base,err);
4471 if (res == DW_DLV_ERROR) {
4472 return res;
4473 }
4474 } else if (is_location_form(theform)) {
4475 res = print_location_list(dbg, die, attrib,
4476 checking,
4477 die_indent_level,
4478 FALSE,
4479 details,err);
4480 if (res == DW_DLV_ERROR) {
4481 print_error_and_continue(
4482 "ERROR: Cannot get location list"
4483 " data",
4484 res, *err);
4485 return res;
4486 }
4487 } else {
4488 show_attr_form_error(attr,theform,base);
4489 }
4490 return DW_DLV_OK;
4491 }
4492
4493 /* This was inside print_attribute() */
4494 static void
4495 check_attr_tag_combination(Dwarf_Debug dbg,
4496 Dwarf_Half tag,Dwarf_Half attr)
4497 {
4498 const char *tagname = "<tag invalid>";
4499 int res = 0;
4500
4501 DWARF_CHECK_COUNT(attr_tag_result,1);
4502 res = legal_tag_attr_combination(tag, attr);
4503 switch(res) {
4504 case TK_OK:
4505 break;
4506 case TK_SHOW_MESSAGE:
4507 /* Report errors only if tag-attr check is on */
4508 if (glflags.gf_check_tag_attr) {
4509 tagname = get_TAG_name(tag,
4510 pd_dwarf_names_print_on_error);
4511 tag_specific_globals_setup(dbg,tag,
4512 die_stack_indent_level);
4513 DWARF_CHECK_ERROR3(attr_tag_result,tagname,
4514 get_AT_name(attr,
4515 pd_dwarf_names_print_on_error),
4516 "check the tag-attr combination");
4517 }
4518 break;
4519 case TK_ERROR: /* fall thru */
4520 default:
4521 {
4522 static unsigned long errcount = 0;
4523 if (!errcount) {
4524 printf("ERROR: Tag 0x%x "
4525 " Attribute 0x%x fails for unknown"
4526 " reason, possibly out of memory"
4527 " building large search trees."
4528 " This message will not repeat\n",
4529 tag,attr);
4530 glflags.gf_count_major_errors++;
4531 }
4532 ++errcount;
4533 }
4534 break;
4535 }
4536 }
4537
4538 static void
4539 remark_wrong_string_format(Dwarf_Half attr,
4540 Dwarf_Half theform)
4541 {
4542 char buf[VSFBUFSZ+1];
4543 struct esb_s m;
4544
4545 buf[0] = 0;
4546 esb_constructor_fixed(&m,buf,VSFBUFSZ);
4547 esb_append_printf_s(&m,
4548 "ERROR: Cannot print the value of "
4549 "attribute %s ",
4550 get_AT_name(attr,FALSE));
4551 esb_append_printf_s(&m,
4552 "as it has form %s which seems wrong.",
4553 get_FORM_name(theform,FALSE));
4554 esb_append(&m," Corrupted DWARF? Continuing.");
4555 simple_err_return_msg_either_action(DW_DLV_ERROR,
4556 esb_get_string(&m));
4557 esb_destructor(&m);
4558 return;
4559 }
4560
4561 static int
4562 print_attribute(Dwarf_Debug dbg, Dwarf_Die die,
4563 Dwarf_Off dieprint_cu_goffset,
4564 Dwarf_Half attr,
4565 Dwarf_Attribute attr_in,
4566 Dwarf_Bool print_else_name_match,
4567 int die_indent_level,
4568 char **srcfiles, Dwarf_Signed srcfiles_cnt,
4569 LoHiPc * lohipc,
4570 Dwarf_Bool *attr_duplication,
4571 Dwarf_Error *err)
4572 {
4573 Dwarf_Attribute attrib = 0;
4574 Dwarf_Unsigned uval = 0;
4575 const char * atname = 0;
4576 int tres = 0;
4577 Dwarf_Half tag = 0;
4578 int append_extra_string = 0;
4579 Dwarf_Bool found_search_attr = FALSE;
4580 Dwarf_Bool bTextFound = FALSE;
4581 Dwarf_Addr max_address = 0;
4582 struct esb_s valname;
4583 struct esb_s esb_extra;
4584 char valbuf[ESB_FIXED_ALLOC_SIZE*3];
4585 char xtrabuf[ESB_FIXED_ALLOC_SIZE*3];
4586 int res = 0;
4587 Dwarf_Bool checking = glflags.gf_do_check_dwarf;
4588 Dwarf_Half theform = 0;
4589 Dwarf_Half directform = 0;
4590 Dwarf_Half version = 0;
4591 Dwarf_Half offset_size = 0;
4592 enum Dwarf_Form_Class fc = DW_FORM_CLASS_UNKNOWN;
4593 Dwarf_Half address_size_base = 0;
4594 Dwarf_Half address_size_again = 0;
4595 Dwarf_Bool attribute_handled_by_switch = TRUE;
4596 Dwarf_Bool is_class_reference = FALSE;
4597
4598 esb_constructor_fixed(&esb_extra,xtrabuf,sizeof(xtrabuf));
4599 esb_constructor_fixed(&valname,valbuf,sizeof(valbuf));
4600 atname = get_AT_name(attr,pd_dwarf_names_print_on_error);
4601 res = get_address_size_and_max(dbg,&address_size_base,
4602 &max_address,err);
4603 if (res != DW_DLV_OK) {
4604 print_error_and_continue(
4605 "Getting address maximum"
4606 " failed in printing attribute ",res,*err);
4607 return res;
4608 }
4609 /* Not passing in error, this is just a check of the
4610 function we call here. No Dwarf_Error
4611 will be allocated. */
4612 res = dwarf_get_die_address_size(die, &address_size_again,0);
4613 if (res == DW_DLV_OK) {
4614 static int showedsizeoddness;
4615 if ( !showedsizeoddness &&
4616 address_size_again != address_size_base) {
4617 printf("NOTE: DIE context address size of %u"
4618 " while object address size is %u."
4619 " This message will not be repeated.\n",
4620 address_size_again,address_size_base);
4621 showedsizeoddness++;
4622 }
4623 } else {
4624 printf("ERROR: DIE context address size "
4625 "could not be retrieved. Very odd\n");
4626 glflags.gf_count_major_errors++;
4627 }
4628 /* The following gets the real attribute, even
4629 in the face of an
4630 incorrect doubling, or worse, of attributes. */
4631 attrib = attr_in;
4632 res = get_form_values(attrib,&theform,&directform, err);
4633 if (res == DW_DLV_ERROR) {
4634 print_error_and_continue(
4635 "ERROR: Cannot get form values",
4636 res, *err);
4637 esb_destructor(&valname);
4638 esb_destructor(&esb_extra);
4639 return res;
4640 }
4641 res = dwarf_get_version_of_die(die,&version,&offset_size);
4642 if (res != DW_DLV_OK) {
4643 print_error_and_continue(
4644 "ERROR: Cannot get DIE context version number"
4645 " for DW_AT_discr_list",
4646 DW_DLV_OK,0);
4647 esb_destructor(&valname);
4648 esb_destructor(&esb_extra);
4649 /* Returning DW_DLV_ERROR would be bogus */
4650 return DW_DLV_NO_ENTRY;
4651 }
4652 fc = dwarf_get_form_class(version,attr,offset_size,theform);
4653 /* Do not get attr via dwarf_attr:
4654 if there are (erroneously)
4655 multiple of an attr in a DIE,
4656 dwarf_attr will not get the
4657 second, erroneous one and dwarfdump will
4658 print the first one
4659 multiple times. Oops. */
4660 tres = dwarf_tag(die, &tag, err);
4661 if (tres != DW_DLV_OK) {
4662 print_error_and_continue("Getting DIE tag "
4663 " failed in printing an attribute.",
4664 tres,*err);
4665 esb_destructor(&valname);
4666 esb_destructor(&esb_extra);
4667 return tres;
4668 }
4669 if ((glflags.gf_check_tag_attr ||
4670 glflags.gf_print_usage_tag_attr)&&
4671 checking_this_compiler()) {
4672 check_attr_tag_combination(dbg,tag,attr);
4673 record_attr_form_use(dbg,tag,attr,(Dwarf_Half)fc,
4674 theform,
4675 die_stack_indent_level);
4676 }
4677
4678 switch (attr) {
4679 case DW_AT_language:
4680 res = dd_get_integer_and_name(dbg, attrib,
4681 &uval,
4682 "DW_AT_language", &valname,
4683 get_LANG_name, err,
4684 glflags.show_form_used);
4685 if (res == DW_DLV_ERROR) {
4686 print_error_and_continue(
4687 "Cannot get DW_AT_language value. ",
4688 res,*err);
4689 esb_destructor(&valname);
4690 esb_destructor(&esb_extra);
4691 return res;
4692 }
4693 break;
4694 case DW_AT_accessibility:
4695 res = dd_get_integer_and_name(dbg, attrib,
4696 &uval,
4697 "DW_AT_accessibility",
4698 &valname, get_ACCESS_name,
4699 err,
4700 glflags.show_form_used);
4701 if (res == DW_DLV_ERROR) {
4702 print_error_and_continue(
4703 "Cannot get DW_AT_accessibility value",
4704 res,*err);
4705 esb_destructor(&valname);
4706 esb_destructor(&esb_extra);
4707 return res;
4708 }
4709 break;
4710 case DW_AT_visibility:
4711 res = dd_get_integer_and_name(dbg, attrib,
4712 &uval,
4713 "DW_AT_visibility",
4714 &valname, get_VIS_name,
4715 err,
4716 glflags.show_form_used);
4717 if (res == DW_DLV_ERROR) {
4718 print_error_and_continue(
4719 "Cannot get DW_AT_visibility value.",
4720 res,*err);
4721 esb_destructor(&valname);
4722 esb_destructor(&esb_extra);
4723 return res;
4724 }
4725 break;
4726 case DW_AT_virtuality:
4727 res = dd_get_integer_and_name(dbg, attrib,
4728 &uval,
4729 "DW_AT_virtuality",
4730 &valname,
4731 get_VIRTUALITY_name, err,
4732 glflags.show_form_used);
4733 if (res == DW_DLV_ERROR) {
4734 print_error_and_continue(
4735 "Cannot get DW_AT_virtuality",
4736 res,*err);
4737 esb_destructor(&valname);
4738 esb_destructor(&esb_extra);
4739 return res;
4740 }
4741 break;
4742 case DW_AT_identifier_case:
4743 res = dd_get_integer_and_name(dbg, attrib,
4744 &uval,
4745 "DW_AT_identifier",
4746 &valname, get_ID_name,
4747 err,
4748 glflags.show_form_used);
4749 if (res == DW_DLV_ERROR) {
4750 print_error_and_continue(
4751 "Cannot get DW_AT_identifier_case",
4752 res,*err);
4753 esb_destructor(&valname);
4754 esb_destructor(&esb_extra);
4755 return res;
4756 }
4757 break;
4758 case DW_AT_inline:
4759 res = dd_get_integer_and_name(dbg, attrib,
4760 &uval,
4761 "DW_AT_inline", &valname,
4762 get_INL_name, err,
4763 glflags.show_form_used);
4764 if (res == DW_DLV_ERROR) {
4765 print_error_and_continue(
4766 "Cannot get DW_AT_inline", res,*err);
4767 esb_destructor(&valname);
4768 esb_destructor(&esb_extra);
4769 return res;
4770 }
4771 break;
4772 case DW_AT_encoding:
4773 res =dd_get_integer_and_name(dbg, attrib,
4774 &uval,
4775 "DW_AT_encoding", &valname,
4776 get_ATE_name, err,
4777 glflags.show_form_used);
4778 if (res == DW_DLV_ERROR) {
4779 print_error_and_continue(
4780 "ERROR:Cannot get DW_AT_encoding",
4781 res,*err);
4782 esb_destructor(&valname);
4783 esb_destructor(&esb_extra);
4784 return res;
4785 }
4786 break;
4787 case DW_AT_ordering:
4788 res =dd_get_integer_and_name(dbg, attrib,
4789 &uval,
4790 "DW_AT_ordering", &valname,
4791 get_ORD_name, err,
4792 glflags.show_form_used);
4793 if (res == DW_DLV_ERROR) {
4794 print_error_and_continue(
4795 "ERROR:Cannot get DW_AT_ordering",
4796 res,*err);
4797 esb_destructor(&valname);
4798 esb_destructor(&esb_extra);
4799 return res;
4800 }
4801 break;
4802 case DW_AT_calling_convention:
4803 res =dd_get_integer_and_name(dbg, attrib,
4804 &uval,
4805 "DW_AT_calling_convention",
4806 &valname, get_CC_name,
4807 err,
4808 glflags.show_form_used);
4809 if (res == DW_DLV_ERROR) {
4810 print_error_and_continue(
4811 "ERROR:Cannot get DW_AT_calling_convention",
4812 res,*err);
4813 esb_destructor(&valname);
4814 esb_destructor(&esb_extra);
4815 return res;
4816 }
4817 break;
4818 case DW_AT_discr_list: { /* DWARF2 */
4819 /* This has one of the block forms.
4820 It should be in a DW_TAG_variant.
4821 Up to September 2016 it was treated as integer or name
4822 here, which was quite wrong. */
4823
4824 if (fc == DW_FORM_CLASS_BLOCK) {
4825 int fres = 0;
4826 Dwarf_Block *tempb = 0;
4827 /* the block is a series of entries each of one
4828 of these formats:
4829 DW_DSC_label caselabel
4830 DW_DSC_range lowvalue highvalue
4831
4832 The values are all LEB. Signed or unsigned
4833 depending on the DW_TAG_variant_part owning
4834 the DW_TAG_variant. The DW_TAG_variant_part
4835 will have a DW_AT_type or a DW_AT_discr
4836 and that attribute will reveal the
4837 signedness of all
4838 the leb values.
4839 As a practical matter DW_DSC_label/DW_DSC_range
4840 value (zero or one, so far)
4841 can safely be read as ULEB or SLEB
4842 and one gets a valid value whereas
4843 the caselabel, lowvalue,highvalue must be
4844 decoded with the proper sign. the high level
4845 (dwarfdump in this case) is the agent that
4846 should determine the proper signedness. */
4847
4848 fres = dwarf_formblock(attrib, &tempb,err);
4849 if (fres == DW_DLV_OK) {
4850 struct esb_s bformstr;
4851 int isunsigned = 0; /* Meaning unknown */
4852 Dwarf_Dsc_Head h = 0;
4853 Dwarf_Unsigned arraycount = 0;
4854 int sres = 0;
4855 char fbuf[ESB_FIXED_ALLOC_SIZE];
4856
4857 esb_constructor_fixed(&bformstr,fbuf,
4858 sizeof(fbuf));
4859 show_form_itself(glflags.show_form_used,
4860 glflags.verbose,
4861 theform, directform,&bformstr);
4862 isunsigned = determine_discr_signedness(dbg);
4863 esb_empty_string(&valname);
4864
4865 sres = dwarf_discr_list(dbg,
4866 (Dwarf_Small *)tempb->bl_data,
4867 tempb->bl_len,
4868 &h,&arraycount,err);
4869 if (sres == DW_DLV_NO_ENTRY) {
4870 esb_append(&bformstr,
4871 "<empty discriminant list>");
4872 break;
4873 }
4874 if (sres == DW_DLV_ERROR) {
4875 print_error_and_continue(
4876 "ERROR: DW_AT_discr_list access fail",
4877 sres, *err);
4878 esb_destructor(&valname);
4879 esb_destructor(&esb_extra);
4880 return sres;
4881 }
4882 sres = append_discr_array_vals(h,arraycount,
4883 isunsigned,&bformstr,err);
4884 if (sres == DW_DLV_ERROR) {
4885 print_error_and_continue(
4886 "ERROR: getting discriminant values "
4887 "failed",
4888 sres, *err);
4889 esb_destructor(&valname);
4890 esb_destructor(&esb_extra);
4891 return sres;
4892 }
4893
4894 if (glflags.verbose > 1) {
4895 unsigned u = 0;
4896 esb_append_printf_u(&bformstr,
4897 "\n block byte len:"
4898 "0x%" DW_PR_XZEROS DW_PR_DUx
4899 "\n ",tempb->bl_len);
4900 for (u = 0; u < tempb->bl_len; u++) {
4901 esb_append_printf_u(&bformstr, "%02x ",
4902 *(u +
4903 (unsigned char *)tempb->bl_data));
4904 }
4905 }
4906 esb_append(&valname, esb_get_string(&bformstr));
4907 dwarf_dealloc(dbg,h,DW_DLA_DSC_HEAD);
4908 dwarf_dealloc(dbg, tempb, DW_DLA_BLOCK);
4909 esb_destructor(&bformstr);
4910 tempb = 0;
4911 } else {
4912 print_error_and_continue(
4913 "ERROR: DW_AT_discr_list: cannot get list"
4914 "data", fres, *err);
4915 esb_destructor(&valname);
4916 esb_destructor(&esb_extra);
4917 return fres;
4918 }
4919 } else {
4920 print_error_and_continue(
4921 "DW_AT_discr_list is not form class BLOCK",
4922 fc, *err);
4923 esb_destructor(&valname);
4924 esb_destructor(&esb_extra);
4925 return fc;
4926 }
4927 }
4928 break;
4929 case DW_AT_allocated:
4930 case DW_AT_associated:
4931 case DW_AT_call_value:
4932 case DW_AT_call_data_value:
4933 case DW_AT_call_data_location:
4934 case DW_AT_call_origin:
4935 case DW_AT_call_target:
4936 case DW_AT_call_target_clobbered:
4937 case DW_AT_const_value:
4938 case DW_AT_count:
4939 case DW_AT_data_location:
4940 case DW_AT_data_member_location:
4941 case DW_AT_default_value:
4942 case DW_AT_frame_base:
4943 case DW_AT_GNU_call_site_value:
4944 case DW_AT_GNU_call_site_target:
4945 case DW_AT_byte_stride:
4946 case DW_AT_byte_size:
4947 case DW_AT_bit_size:
4948 case DW_AT_bit_stride:
4949 case DW_AT_location:
4950 case DW_AT_lower_bound:
4951 case DW_AT_rank:
4952 case DW_AT_return_addr:
4953 case DW_AT_segment:
4954 case DW_AT_static_link:
4955 case DW_AT_string_length:
4956 case DW_AT_upper_bound:
4957 case DW_AT_use_location:
4958 case DW_AT_vtable_elem_location:
4959 {
4960 /* Value is a constant or a location
4961 description or location list.
4962 If a constant, it could be signed or
4963 unsigned. Telling whether a constant
4964 or a reference is nontrivial
4965 since DW_FORM_data{4,8}
4966 could be either in DWARF{2,3} */
4967
4968 if (fc == DW_FORM_CLASS_CONSTANT) {
4969 struct esb_s classconstantstr;
4970 Dwarf_Bool chex = FALSE;
4971 int wres = 0;
4972
4973 esb_constructor(&classconstantstr);
4974 /* Makes no sense to look at type of our DIE
4975 to determine how to print the constant. */
4976 wres = formxdata_print_value(dbg,NULL,attrib,
4977 theform,
4978 &classconstantstr,
4979 err, chex);
4980 if (wres != DW_DLV_OK) {
4981 esb_destructor(&valname);
4982 esb_destructor(&esb_extra);
4983 return wres;
4984 }
4985 show_form_itself(glflags.show_form_used,
4986 glflags.verbose,
4987 theform, directform, &classconstantstr);
4988 esb_empty_string(&valname);
4989 esb_append(&valname,
4990 esb_get_string(&classconstantstr));
4991 esb_destructor(&classconstantstr);
4992 break;
4993 }
4994 /* FALL THRU, this is a
4995 a location description, or a reference
4996 to one, or a mistake. */
4997 }
4998 {
4999 Dwarf_Bool showform = glflags.show_form_used;
5000
5001 /* If DW_FORM_block* && show_form_used
5002 get_attr_value() results
5003 in duplicating the form name (with -M). */
5004 /* For block forms, this will show block len and bytes
5005 and if showing form, then form shown */
5006 res = get_attr_value(dbg, tag, die,
5007 dieprint_cu_goffset,
5008 attrib, srcfiles, srcfiles_cnt, &valname,
5009 showform,
5010 glflags.verbose,
5011 err);
5012 if (res == DW_DLV_ERROR) {
5013 print_error_and_continue(
5014 "Cannot get attr form value", res,*err);
5015 DROP_ERROR_INSTANCE(dbg,res,*err);
5016 break;
5017 }
5018 append_extra_string = TRUE;
5019 if (fc == DW_FORM_CLASS_EXPRLOC ||
5020 fc == DW_FORM_CLASS_LOCLIST ||
5021 fc == DW_FORM_CLASS_LOCLISTPTR) {
5022 /* Form class exprloc is set even for DWARF2&3,
5023 see libdwarf: dwarf_get_form_class(). */
5024 res = print_location_description(dbg,attrib,die,
5025 checking,
5026 attr,die_indent_level,
5027 &valname,&esb_extra,err);
5028 if (res == DW_DLV_ERROR) {
5029 print_error_and_continue(
5030 "Cannot get location data, attr "
5031 "(with -M also form) "
5032 "follow", res,*err);
5033 DROP_ERROR_INSTANCE(dbg,res,*err);
5034 break;
5035 }
5036 }
5037 }
5038 break;
5039 case DW_AT_SUN_func_offsets:
5040 {
5041 /* value is a location description or location list */
5042 char buf[100];
5043 struct esb_s funcformstr;
5044
5045 esb_constructor_fixed(&funcformstr,buf,sizeof(buf));
5046 get_FLAG_BLOCK_string(dbg, attrib,&funcformstr);
5047 show_form_itself(glflags.show_form_used,
5048 glflags.verbose, theform,
5049 directform,&funcformstr);
5050 esb_empty_string(&valname);
5051 esb_append(&valname, esb_get_string(&funcformstr));
5052 esb_destructor(&funcformstr);
5053 }
5054 break;
5055 case DW_AT_SUN_cf_kind:
5056 {
5057 Dwarf_Half kind = 0;
5058 Dwarf_Unsigned tempud = 0;
5059 int wres = 0;
5060 struct esb_s cfkindstr;
5061
5062 esb_constructor(&cfkindstr);
5063 wres = dwarf_formudata (attrib,&tempud, err);
5064 if (wres == DW_DLV_OK) {
5065 kind = (Dwarf_Half)tempud;
5066 esb_append(&cfkindstr,
5067 get_ATCF_name((unsigned int)kind,
5068 pd_dwarf_names_print_on_error));
5069 } else if (wres == DW_DLV_NO_ENTRY) {
5070 esb_append(&cfkindstr, "?");
5071 } else {
5072 print_error_and_continue(
5073 "Cannot get formudata length field for"
5074 " DW_AT_SUN_cf_kind ",
5075 wres,*err);
5076 esb_destructor(&valname);
5077 esb_destructor(&esb_extra);
5078 return res;
5079 }
5080 show_form_itself(glflags.show_form_used,
5081 glflags.verbose,theform,
5082 directform,&cfkindstr);
5083 esb_empty_string(&valname);
5084 esb_append(&valname, esb_get_string(&cfkindstr));
5085 esb_destructor(&cfkindstr);
5086 }
5087 break;
5088 case DW_AT_low_pc:
5089 case DW_AT_high_pc:
5090 {
5091 int rv = 0;
5092 rv = print_hipc_lopc_attribute(dbg, tag, die,
5093 dieprint_cu_goffset,
5094 srcfiles, srcfiles_cnt,
5095 attrib,
5096 attr,
5097 max_address,
5098 lohipc,
5099 &valname,
5100 err);
5101 if (rv != DW_DLV_OK) {
5102 esb_destructor(&valname);
5103 esb_destructor(&esb_extra);
5104 return rv;
5105 }
5106 }
5107 break;
5108 case DW_AT_ranges:
5109 {
5110 int rv;
5111
5112 rv = get_attr_value(dbg, tag,die,
5113 dieprint_cu_goffset,attrib,
5114 srcfiles, srcfiles_cnt,
5115 &valname,
5116 glflags.show_form_used,glflags.verbose,err);
5117 if (rv == DW_DLV_ERROR) {
5118 print_error_and_continue(
5119 "Cannot find Attr value"
5120 "for DW_AT_ranges",
5121 rv, *err);
5122 esb_destructor(&valname);
5123 esb_destructor(&esb_extra);
5124 return rv;
5125 }
5126 rv = print_range_attribute(dbg, die, attr,attr_in,
5127 theform,
5128 pd_dwarf_names_print_on_error,
5129 print_else_name_match,
5130 &append_extra_string,
5131 &esb_extra,err);
5132 if (rv == DW_DLV_ERROR) {
5133 print_error_and_continue(
5134 "Cannot print range attribute "
5135 "for DW_AT_ranges",
5136 rv, *err);
5137 /* We will not stop, this is an omission
5138 in libdwarf on DWARF5 rnglists */
5139 DROP_ERROR_INSTANCE(dbg,rv,*err);
5140 }
5141 }
5142 break;
5143 case DW_AT_MIPS_linkage_name:
5144 {
5145 int ml = 0;
5146 char linknamebuf[ESB_FIXED_ALLOC_SIZE];
5147 struct esb_s linkagenamestr;
5148
5149 esb_constructor_fixed(&linkagenamestr,linknamebuf,
5150 sizeof(linknamebuf));
5151 ml = get_attr_value(dbg, tag, die,
5152 dieprint_cu_goffset, attrib, srcfiles,
5153 srcfiles_cnt, &linkagenamestr, glflags.show_form_used,
5154 glflags.verbose,err);
5155 if (ml == DW_DLV_ERROR) {
5156 print_error_and_continue(
5157 "Cannot get value "
5158 "for DW_AT_MIPS_linkage_name ",
5159 ml, *err);
5160 esb_destructor(&valname);
5161 esb_destructor(&esb_extra);
5162 esb_destructor(&linkagenamestr);
5163 return ml;
5164 }
5165 if (fc != DW_FORM_CLASS_STRING) {
5166 remark_wrong_string_format(attr,theform);
5167 esb_destructor(&valname);
5168 esb_destructor(&esb_extra);
5169 esb_destructor(&linkagenamestr);
5170 return DW_DLV_NO_ENTRY;
5171 }
5172
5173 esb_empty_string(&valname);
5174 esb_append(&valname, esb_get_string(&linkagenamestr));
5175 esb_destructor(&linkagenamestr);
5176
5177 if (glflags.gf_check_locations ||
5178 glflags.gf_check_ranges) {
5179 int local_show_form = 0;
5180 int local_verbose = 0;
5181 const char *name = 0;
5182 struct esb_s lesb;
5183
5184 esb_constructor(&lesb);
5185 get_attr_value(dbg, tag, die,
5186 dieprint_cu_goffset,attrib,
5187 srcfiles, srcfiles_cnt,
5188 &lesb, local_show_form,local_verbose,err);
5189
5190 /* Look for specific name forms, attempting to
5191 notice and report 'odd' identifiers.
5192 Used in the SNC LinkOnce feature. */
5193 name = esb_get_string(&lesb);
5194 dd_safe_strcpy(glflags.PU_name,sizeof(glflags.PU_name),
5195 name,esb_string_len(&lesb));
5196 esb_destructor(&lesb);
5197 }
5198 }
5199 break;
5200 case DW_AT_name:
5201 case DW_AT_GNU_template_name:
5202 {
5203 char atnamebuf[ESB_FIXED_ALLOC_SIZE];
5204 struct esb_s templatenamestr;
5205
5206 if (fc != DW_FORM_CLASS_STRING) {
5207 remark_wrong_string_format(attr,theform);
5208 esb_destructor(&valname);
5209 esb_destructor(&esb_extra);
5210 return DW_DLV_NO_ENTRY;
5211 }
5212 esb_constructor_fixed(&templatenamestr,atnamebuf,
5213 sizeof(atnamebuf));
5214 tres = get_attr_value(dbg, tag, die,
5215 dieprint_cu_goffset,attrib, srcfiles, srcfiles_cnt,
5216 &templatenamestr, glflags.show_form_used,
5217 glflags.verbose,err);
5218 if (tres == DW_DLV_ERROR) {
5219 print_error_and_continue(
5220 "Cannot get value "
5221 "for DW_AT_name/DW_AT_GNU_template_name ",
5222 tres, *err);
5223 esb_destructor(&valname);
5224 esb_destructor(&esb_extra);
5225 return tres;
5226 }
5227 esb_empty_string(&valname);
5228 esb_append(&valname, esb_get_string(&templatenamestr));
5229 esb_destructor(&templatenamestr);
5230
5231 if ( glflags.gf_check_names && checking_this_compiler()) {
5232 int local_show_form = FALSE;
5233 int local_verbose = 0;
5234 struct esb_s lesb;
5235 const char *name = 0;
5236
5237 esb_constructor(&lesb);
5238 tres = get_attr_value(dbg, tag, die,
5239 dieprint_cu_goffset,attrib,
5240 srcfiles, srcfiles_cnt,
5241 &lesb, local_show_form,local_verbose,err);
5242 /* Look for specific name forms, attempting to
5243 notice and report 'odd' identifiers. */
5244 if (tres == DW_DLV_ERROR) {
5245 print_error_and_continue(
5246 "Cannot get check_names value "
5247 "for DW_AT_name/DW_AT_GNU_template_name ",
5248 tres, *err);
5249 esb_destructor(&lesb);
5250 esb_destructor(&valname);
5251 esb_destructor(&esb_extra);
5252 return tres;
5253 }
5254
5255 name = esb_get_string(&lesb);
5256 DWARF_CHECK_COUNT(names_result,1);
5257 if (!strcmp("\"(null)\"",name)) {
5258 DWARF_CHECK_ERROR(names_result,
5259 "string attribute is \"(null)\".");
5260 } else {
5261 if (!dot_ok_in_identifier(tag,name) &&
5262 !glflags.need_CU_name && strchr(name,'.')) {
5263 /* This is a suggestion there 'might' be
5264 a surprising name, not a guarantee of an
5265 error. */
5266 DWARF_CHECK_ERROR(names_result,
5267 "string attribute is invalid.");
5268 }
5269 }
5270 esb_destructor(&lesb);
5271 }
5272 }
5273
5274 /* If we are in checking mode and we do not
5275 have a PU name */
5276 if (( glflags.gf_check_locations ||
5277 glflags.gf_check_ranges) &&
5278 glflags.seen_PU && !glflags.PU_name[0]) {
5279 int local_show_form = FALSE;
5280 int local_verbose = 0;
5281 const char *name = 0;
5282 struct esb_s lesb;
5283 int vres = 0;
5284
5285 esb_constructor(&lesb);
5286 vres = get_attr_value(dbg, tag, die,
5287 dieprint_cu_goffset,attrib,
5288 srcfiles, srcfiles_cnt,
5289 &lesb, local_show_form,local_verbose,err);
5290 if (vres == DW_DLV_ERROR) {
5291 print_error_and_continue(
5292 "Cannot get check-locations value "
5293 "for DW_AT_name/DW_AT_GNU_template_name ",
5294 vres, *err);
5295 esb_destructor(&lesb);
5296 esb_destructor(&valname);
5297 esb_destructor(&esb_extra);
5298 return vres;
5299 }
5300 name = esb_get_string(&lesb);
5301 dd_safe_strcpy(glflags.PU_name,sizeof(glflags.PU_name),
5302 name, esb_string_len(&lesb));
5303 esb_destructor(&lesb);
5304 }
5305
5306 /* If we are processing the compile unit,
5307 record the name */
5308 if (glflags.seen_CU && glflags.need_CU_name) {
5309 /* Lets not get the form name included. */
5310 struct esb_s lesb;
5311 int local_show_form_used = FALSE;
5312 int local_verbose = 0;
5313 int sres = 0;
5314
5315 esb_constructor(&lesb);
5316 sres = get_attr_value(dbg, tag, die,
5317 dieprint_cu_goffset,attrib,
5318 srcfiles, srcfiles_cnt,
5319 &lesb, local_show_form_used,local_verbose,
5320 err);
5321 if (sres == DW_DLV_ERROR) {
5322 print_error_and_continue(
5323 "Cannot get CU name "
5324 "for DW_AT_name/DW_AT_GNU_template_name ",
5325 sres, *err);
5326 esb_destructor(&lesb);
5327 esb_destructor(&valname);
5328 esb_destructor(&esb_extra);
5329 return sres;
5330 }
5331
5332 dd_safe_strcpy(glflags.CU_name,sizeof(glflags.CU_name),
5333 esb_get_string(&lesb),esb_string_len(&lesb));
5334 glflags.need_CU_name = FALSE;
5335 esb_destructor(&lesb);
5336 }
5337 break;
5338
5339 case DW_AT_linkage_name:
5340 case DW_AT_comp_dir:
5341 case DW_AT_producer:
5342 {
5343 struct esb_s lesb;
5344 int pres = 0;
5345
5346 if (fc != DW_FORM_CLASS_STRING) {
5347 remark_wrong_string_format(attr,theform);
5348 esb_destructor(&valname);
5349 esb_destructor(&esb_extra);
5350 return DW_DLV_NO_ENTRY;
5351 }
5352 esb_constructor(&lesb);
5353 pres = get_attr_value(dbg, tag, die,
5354 dieprint_cu_goffset,attrib, srcfiles, srcfiles_cnt,
5355 &lesb, glflags.show_form_used,glflags.verbose,
5356 err);
5357 if (pres == DW_DLV_ERROR) {
5358 print_error_and_continue(
5359 "Cannot get value "
5360 "for DW_AT_producer",
5361 pres, *err);
5362 esb_destructor(&lesb);
5363 esb_destructor(&valname);
5364 esb_destructor(&esb_extra);
5365 return pres;
5366 }
5367 esb_empty_string(&valname);
5368 esb_append(&valname, esb_get_string(&lesb));
5369 esb_destructor(&lesb);
5370 /* If we are in checking mode, identify the compiler */
5371 if (attr == DW_AT_producer &&
5372 (glflags.gf_do_check_dwarf ||
5373 glflags.gf_search_is_on)) {
5374 /* Do not use show-form here! We just want
5375 the producer name, not the form name. */
5376 int show_form_local = FALSE;
5377 int local_verbose = 0;
5378 struct esb_s local_e;
5379
5380 esb_constructor(&local_e);
5381 pres = get_attr_value(dbg, tag, die,
5382 dieprint_cu_goffset,attrib,
5383 srcfiles, srcfiles_cnt,
5384 &local_e, show_form_local,local_verbose,err);
5385 if (pres == DW_DLV_ERROR) {
5386 print_error_and_continue(
5387 "Cannot get checking value "
5388 "for DW_AT_producer",
5389 pres, *err);
5390 esb_destructor(&local_e);
5391 esb_destructor(&valname);
5392 esb_destructor(&esb_extra);
5393 return pres;
5394 }
5395 /* Check if this compiler version is a target */
5396 update_compiler_target(esb_get_string(&local_e));
5397 esb_destructor(&local_e);
5398 }
5399 }
5400 break;
5401 default:
5402 attribute_handled_by_switch = FALSE;
5403 break;
5404 } /* end switch statment on attribute code */
5405 if (!attribute_handled_by_switch) {
5406 if (fc == DW_FORM_CLASS_REFERENCE) {
5407 is_class_reference = TRUE;
5408 }
5409 }
5410
5411 /* Note on checking for special symbols:
5412 When dealing with SNC linkonce symbols,
5413 the low_pc and high_pc
5414 are associated with a specific symbol;
5415 SNC always generate a name with
5416 DW_AT_MIPS_linkage_name; GCC does not;
5417 instead gcc generates
5418 DW_AT_abstract_origin or DW_AT_specification;
5419 in that case we have to
5420 traverse this attribute in order to get the
5421 name for the linkonce
5422 DW_AT_GNU_locviews are a reference too,
5423 and are actually an offset from a base address.*/
5424 if (is_class_reference) {
5425 tres = dd_trace_abstract_origin_etc(dbg,tag,die,
5426 dieprint_cu_goffset,
5427 theform,
5428 attr,
5429 attrib, srcfiles,
5430 srcfiles_cnt,&valname,&esb_extra,
5431 die_indent_level,pd_dwarf_names_print_on_error,err);
5432 if (tres != DW_DLV_OK) {
5433 return tres;
5434 }
5435 } else if (!attribute_handled_by_switch) {
5436 char ebuf[ESB_FIXED_ALLOC_SIZE];
5437 struct esb_s lesb;
5438 int dres = 0;
5439
5440 esb_constructor_fixed(&lesb,ebuf,sizeof(ebuf));
5441 dres = get_attr_value(dbg, tag,die,
5442 dieprint_cu_goffset,attrib,
5443 srcfiles, srcfiles_cnt, &lesb,
5444 glflags.show_form_used,glflags.verbose,err);
5445 if (dres == DW_DLV_ERROR) {
5446 struct esb_s m;
5447 const char *n =
5448 get_AT_name(attr,
5449 pd_dwarf_names_print_on_error);
5450 esb_constructor(&m);
5451 esb_append(&m,
5452 "Cannot get get value for a ");
5453 esb_append(&m,n);
5454 print_error_and_continue(
5455 esb_get_string(&m),
5456 dres,*err);
5457 esb_destructor(&m);
5458 esb_destructor(&valname);
5459 esb_destructor(&esb_extra);
5460 return dres;
5461 }
5462 esb_empty_string(&valname);
5463 esb_append(&valname, esb_get_string(&lesb));
5464 esb_destructor(&lesb);
5465 }
5466 res = 0; /* value above no longer relevant */
5467
5468 if (!print_else_name_match) {
5469 if (have_a_search_match(esb_get_string(&valname),atname)) {
5470 /* Count occurrence of text */
5471 ++glflags.search_occurrences;
5472 if ( glflags.gf_search_wide_format) {
5473 found_search_attr = TRUE;
5474 } else {
5475 PRINT_CU_INFO();
5476 bTextFound = TRUE;
5477 }
5478 }
5479 }
5480 #if 0 /* DEBUGGING ONLY */
5481 /* This prints all the actual fields as well as the
5482 macro results that use the fields. */
5483 printf("DEBUGONLY std print? attr name %u && "
5484 "%u && %u || %u. %u %u %u\n",
5485 (unsigned)PRINTING_UNIQUE,
5486 (unsigned)PRINTING_DIES,
5487 (unsigned)print_else_name_match,
5488 (unsigned)bTextFound,
5489 (unsigned) glflags.gf_do_print_dwarf,
5490 (unsigned) glflags.gf_check_verbose_mode,
5491 (unsigned) glflags.gf_record_dwarf_error);
5492 #endif /* DEBUGGING ONLY */
5493 /* Above we created detailed messages in
5494 the valname and esb_extra strings.
5495 If we're just printing everything
5496 we will now print those.
5497
5498 Otherwise If we're checking things we will print those
5499 that failed a check (a DWARF_CHECK message
5500 was just printed)
5501
5502 Otherwise if searching for specific attributes
5503 (the else_name_match of the following if stmt)
5504 a match means we print the strings.
5505
5506 Otherwise we will just discard the
5507 valname and esb_extra strings.
5508
5509 That's why the big IF below
5510 has so much in it.
5511 */
5512
5513 if ((PRINTING_UNIQUE && PRINTING_DIES && print_else_name_match)
5514 || bTextFound) {
5515 /* Print just the Tags and Attributes */
5516 if (!glflags.gf_display_offsets) {
5517 printf("%-28s\n",atname);
5518 } else {
5519 if (glflags.dense) {
5520 char *v = 0;
5521 v = esb_get_string(&valname);
5522 printf(" %s<%s>", atname, sanitized(v));
5523 if (append_extra_string) {
5524 v = esb_get_string(&esb_extra);
5525 printf("%s", sanitized(v));
5526 }
5527 } else {
5528 char *v = 0;
5529 printf("%-28s", atname);
5530 if (strlen(atname) >= 28) {
5531 printf(" ");
5532 }
5533 v = esb_get_string(&valname);
5534 printf("%s\n", sanitized(v));
5535 if (append_extra_string) {
5536 v = esb_get_string(&esb_extra);
5537 printf("%s", sanitized(v));
5538 }
5539 }
5540 }
5541 }
5542 esb_destructor(&valname);
5543 esb_destructor(&esb_extra);
5544 *attr_duplication = found_search_attr;
5545 return DW_DLV_OK;
5546 }
5547
5548 static void
5549 alloc_skip_branch_array(Dwarf_Unsigned no_of_ops,
5550 struct OpBranchHead_s *op_branch_checking)
5551 {
5552 op_branch_checking->bh_opcount = 0;
5553 op_branch_checking->bh_ops_array = 0;
5554 if (!no_of_ops) {
5555 return;
5556 }
5557 if (no_of_ops >= (Dwarf_Unsigned)0xffff) {
5558 /* Something wrong. Give up on the array of branch info */
5559 return;
5560 }
5561 op_branch_checking->bh_ops_array = (struct OpBranchEntry_s *)
5562 calloc(no_of_ops,sizeof(struct OpBranchEntry_s));
5563 if (!op_branch_checking->bh_ops_array) {
5564 return;
5565 }
5566 op_branch_checking->bh_opcount = (Dwarf_Half)no_of_ops;
5567 }
5568
5569 static Dwarf_Bool
5570 skip_branch_target_ok(struct OpBranchHead_s *op_branch_checking,
5571 Dwarf_Half index,
5572 struct OpBranchEntry_s*ein)
5573 {
5574 Dwarf_Half i = 0;
5575 struct OpBranchEntry_s*ec = 0;
5576
5577 if (ein->target_offset == ein->offset) {
5578 /* Infinite loop! */
5579 return FALSE;
5580 } else if (ein->target_offset < ein->offset) {
5581 ec = op_branch_checking->bh_ops_array;
5582 for ( ; i < index; ++i,++ec) {
5583 if ( ec->offset == ein->target_offset) {
5584 return TRUE;
5585 }
5586 }
5587 return FALSE;
5588 }
5589 i = index;
5590 ec = op_branch_checking->bh_ops_array+i;
5591 for ( ; i < op_branch_checking->bh_opcount; ++i,++ec) {
5592 if ( ec->offset == ein->target_offset) {
5593 return TRUE;
5594 }
5595 }
5596 return FALSE;
5597 }
5598
5599 static void
5600 check_skip_branch_offsets(struct OpBranchHead_s *op_branch_checking,
5601 struct esb_s *str)
5602 {
5603 Dwarf_Half i = 0;
5604 Dwarf_Half high = op_branch_checking->bh_opcount;
5605 struct OpBranchEntry_s*e = op_branch_checking->bh_ops_array;
5606
5607 for ( ; i < high ; ++i,++e) {
5608 char *opname = "DW_OP_skip";
5609 if (e->op != DW_OP_bra && e->op != DW_OP_skip) {
5610 continue;
5611 }
5612 if (e->op == DW_OP_bra) {
5613 opname = "DW_OP_bra";
5614 }
5615 if (skip_branch_target_ok(op_branch_checking,i,e)){
5616 continue;
5617 }
5618 /* Oops. An error. The skip or branch target
5619 is wrong. Corrupt dwarf */
5620 esb_append_printf_s(str,
5621 "\nERROR: The operation %s",opname);
5622 esb_append_printf_u(str,
5623 " at expression block offset 0x%04x ",e->offset);
5624 esb_append_printf_u(str,
5625 " has target of 0x%04x which is not a valid "
5626 "expression offset in this expression block\n",
5627 e->target_offset);
5628 glflags.gf_count_major_errors++;
5629 }
5630 }
5631
5632 static void
5633 dealloc_skip_branch_array(struct OpBranchHead_s *op_branch_checking)
5634 {
5635 if (op_branch_checking->bh_opcount) {
5636 free(op_branch_checking->bh_ops_array);
5637 op_branch_checking->bh_ops_array = 0;
5638 op_branch_checking->bh_opcount = 0;
5639 }
5640 }
5641
5642 static Dwarf_Bool
5643 op_is_skip_or_branch(Dwarf_Debug dbg,
5644 Dwarf_Locdesc_c exprc,
5645 Dwarf_Unsigned index,
5646 Dwarf_Error *err)
5647 {
5648 Dwarf_Small op = 0;
5649 Dwarf_Unsigned opd1 = 0;
5650 Dwarf_Unsigned opd2 = 0;
5651 Dwarf_Unsigned opd3 = 0;
5652 Dwarf_Unsigned offsetforbranch = 0;
5653 int res = 0;
5654
5655 res = dwarf_get_location_op_value_c(exprc,
5656 index,
5657 &op,&opd1,&opd2,&opd3,
5658 &offsetforbranch,
5659 err);
5660 if (res != DW_DLV_OK) {
5661 if (res == DW_DLV_ERROR) {
5662 DROP_ERROR_INSTANCE(dbg,res,*err);
5663 }
5664 return FALSE;
5665 }
5666 if (op == DW_OP_bra) {
5667 return TRUE;
5668 }
5669 if (op == DW_OP_skip) {
5670 return TRUE;
5671 }
5672 return FALSE;
5673 }
5674
5675 int
5676 dwarfdump_print_expression_operations(Dwarf_Debug dbg,
5677 Dwarf_Die die,
5678 int die_indent_level,
5679 Dwarf_Locdesc_c locdesc, /* for 2015 interface. */
5680 Dwarf_Unsigned entrycount,
5681 struct esb_s *string_out,
5682 Dwarf_Error *err)
5683 {
5684 Dwarf_Unsigned no_of_ops = 0;
5685 Dwarf_Unsigned i = 0;
5686 Dwarf_Bool has_skip_or_branch = FALSE;
5687 struct OpBranchHead_s op_branch_checking;
5688 int stackdepth = 0;
5689 int maxstackdepth = 0;
5690 Dwarf_Unsigned op_loop_count = 0;
5691
5692 alloc_skip_branch_array(0,&op_branch_checking);
5693 /* ASSERT: locs != NULL */
5694 no_of_ops = entrycount;
5695 possibly_increase_esb_alloc(string_out,no_of_ops,100);
5696 if (no_of_ops > 1) {
5697 for (i = 0; i < no_of_ops; i++) {
5698 if (op_is_skip_or_branch(dbg,locdesc,i,err)) {
5699 has_skip_or_branch = TRUE;
5700 break;
5701 }
5702 }
5703 }
5704 if (has_skip_or_branch) {
5705 alloc_skip_branch_array(no_of_ops,&op_branch_checking);
5706 }
5707 for (i = 0; i < no_of_ops; i++) {
5708 int res = 0;
5709 Dwarf_Signed branchdistance = 0;
5710 int zerostackdepth = FALSE;
5711 int stackchange = 0;
5712
5713 res = _dwarf_print_one_expr_op(dbg,die,
5714 die_indent_level,locdesc,i,
5715 has_skip_or_branch,
5716 &op_branch_checking,
5717 &stackchange,
5718 &branchdistance,
5719 &zerostackdepth,
5720 string_out,err);
5721 if (res == DW_DLV_ERROR) {
5722 dealloc_skip_branch_array(&op_branch_checking);
5723 return res;
5724 }
5725 if (branchdistance < 0) {
5726 op_loop_count++;
5727 }
5728 if (zerostackdepth) {
5729 /* This is a DW_OP_piece kind of operation,
5730 so stack is conceptually separate per piece. */
5731 stackdepth = 0;
5732 } else {
5733 stackdepth = stackdepth + stackchange;
5734 if (stackdepth > maxstackdepth) {
5735 maxstackdepth = stackdepth;
5736 }
5737 }
5738 }
5739 if (glflags.verbose &&
5740 !glflags.dense &&
5741 ((maxstackdepth > 1) || op_loop_count)) {
5742 int indentprespaces = standard_indent();
5743 int indentpostspaces = 6;
5744 esb_append(string_out,"\n");
5745 append_indent_prefix(string_out,indentprespaces,
5746 die_indent_level,indentpostspaces);
5747
5748 esb_append_printf_u(string_out,
5749 "DW_OPs= %u ",no_of_ops);
5750 esb_append_printf_i(string_out,
5751 "maxstackdepth= %d",maxstackdepth);
5752 esb_append_printf_i(string_out,
5753 " endingstackdepth= %d",stackdepth);
5754 if (op_loop_count) {
5755 /* When DW_OP_bra/skip has us loop back
5756 to an earlier DW_OPs. */
5757 esb_append_printf_u(string_out,
5758 " loopcount= %u",op_loop_count);
5759 }
5760 esb_append(string_out,"\n");
5761 append_indent_prefix(string_out,indentprespaces,
5762 die_indent_level,indentpostspaces);
5763 }
5764 if (has_skip_or_branch) {
5765 check_skip_branch_offsets(&op_branch_checking,string_out);
5766 }
5767 dealloc_skip_branch_array(&op_branch_checking);
5768 return DW_DLV_OK;
5769 }
5770
5771 static int
5772 op_has_no_operands(Dwarf_Small op)
5773 {
5774 return (_dwarf_opscounttab[op].oc_opcount == 0);
5775 }
5776
5777 /* Sometimes a DW_OP_implicit_const value
5778 is actually a string from gcc.
5779 A crude heuristic.
5780 We are looking for ascii here hoping
5781 it is somewhat useful..
5782 It will be sanitized before being
5783 printed.
5784 */
5785 static Dwarf_Bool
5786 looks_like_string(unsigned long length,const unsigned char *bp)
5787 {
5788 const unsigned char *end = 0;
5789 if (bp[length-1]) {
5790 return FALSE;
5791 }
5792 end = bp+length-1;
5793 for ( ; bp < end; ++bp) {
5794 /* A crude test */
5795 unsigned char c = *bp;
5796 if ( c < 0x08 || c > 0x7e) {
5797 return FALSE;
5798 }
5799 }
5800 return TRUE;
5801 }
5802
5803 static void
5804 show_contents(struct esb_s *string_out,
5805 unsigned long length,const unsigned char * bp)
5806 {
5807 unsigned int i = 0;
5808
5809 if (!length) {
5810 return;
5811 }
5812 esb_append(string_out," contents 0x");
5813 for (; i < length; ++i,++bp) {
5814 /* Do not use DW_PR_DUx here,
5815 the value *bp is a const unsigned char. */
5816 esb_append_printf_u(string_out,"%02x", *bp);
5817 }
5818 }
5819 static void
5820 emit_op_indentation(struct esb_s *string_out,
5821 int die_indent_level,
5822 Dwarf_Unsigned index)
5823 {
5824 if (!glflags.dense && !glflags.gf_expr_ops_joined) {
5825 int indentprespaces = 0;
5826 int indentpostspaces = 0;
5827
5828 indentprespaces = standard_indent();
5829 indentpostspaces = 6;
5830 esb_append(string_out,"\n");
5831 append_indent_prefix(string_out,indentprespaces,
5832 die_indent_level,indentpostspaces);
5833 } else if (index > 0) {
5834 esb_append(string_out, " ");
5835 }
5836 }
5837
5838 /* When an expression op has another expression inside
5839 this does the printing.
5840 Not limited to a single level of nesting.
5841 */
5842 static int
5843 print_expression_inner_block(Dwarf_Debug dbg,
5844 Dwarf_Die die,
5845 int die_indent_level,
5846 Dwarf_Block *block,
5847 struct esb_s *string_out,
5848 Dwarf_Error *error)
5849 {
5850 int res = 0;
5851 Dwarf_Loc_Head_c header = 0;
5852 Dwarf_Unsigned listlen = 0;
5853 Dwarf_Half dw_version = 0;
5854 Dwarf_Bool dw_is_info = 0;
5855 Dwarf_Bool dw_is_dwo = 0;
5856 Dwarf_Half dw_offset_size = 0;
5857 Dwarf_Half dw_address_size = 0;
5858 Dwarf_Half dw_extension_size = 0;
5859 Dwarf_Sig8 *dw_signature = 0;
5860 Dwarf_Off dw_offset_of_length = 0;
5861 Dwarf_Unsigned dw_total_byte_length = 0;
5862 Dwarf_Small lle_value = 0;
5863 Dwarf_Unsigned rawlowpc = 0;
5864 Dwarf_Unsigned rawhipc = 0;
5865 Dwarf_Bool debug_addr_unavailable = TRUE;
5866 Dwarf_Unsigned lowpc_cooked = 0;
5867 Dwarf_Unsigned hipc_cooked = 0;
5868 Dwarf_Unsigned locexpr_op_count=0;
5869 Dwarf_Locdesc_c locentry = 0;
5870 Dwarf_Small loclist_source = 0;
5871 Dwarf_Unsigned expression_offset = 0;
5872 Dwarf_Unsigned locdesc_offset =0;
5873
5874 res = dwarf_cu_header_basics(die, &dw_version,&dw_is_info,
5875 &dw_is_dwo,&dw_offset_size,&dw_address_size,
5876 &dw_extension_size,&dw_signature,
5877 &dw_offset_of_length,&dw_total_byte_length,
5878 error);
5879 if (res != DW_DLV_OK) {
5880 return res;
5881 }
5882 res = dwarf_loclist_from_expr_c(dbg,
5883 block->bl_data,block->bl_len,
5884 dw_address_size,dw_offset_size,
5885 dw_version,
5886 &header,&listlen,error);
5887 if (res != DW_DLV_OK) {
5888 return res;
5889 }
5890 res = dwarf_get_locdesc_entry_d(header,
5891 0, /* Data from 0th LocDesc, the only entry */
5892 &lle_value,
5893 &rawlowpc, &rawhipc,
5894 &debug_addr_unavailable,
5895 &lowpc_cooked, &hipc_cooked,
5896 &locexpr_op_count,
5897 &locentry,
5898 &loclist_source,
5899 &expression_offset,
5900 &locdesc_offset,
5901 error);
5902 if (res == DW_DLV_ERROR) {
5903 dwarf_dealloc_loc_head_c(header);
5904 glflags.gf_count_major_errors++;
5905 printf("\nERROR: calling dwarf_get_locdesc_entry_d()"
5906 " on LocDesc 0");
5907 return res;
5908 } else if (res == DW_DLV_NO_ENTRY) {
5909 dwarf_dealloc_loc_head_c(header);
5910 return res;
5911 }
5912 /* ASSERT: loclist_source == DW_LKIND_expression */
5913 /* ASSERT: lle_value == DW_LLE_start_end */
5914 res = dwarfdump_print_expression_operations(dbg,
5915 die,
5916 die_indent_level,
5917 locentry,
5918 locexpr_op_count,
5919 string_out,error);
5920 dwarf_dealloc_loc_head_c(header);
5921 return res;
5922 }
5923
5924 int
5925 _dwarf_print_one_expr_op(Dwarf_Debug dbg,
5926 Dwarf_Die die,
5927 int die_indent_level,
5928 Dwarf_Locdesc_c exprc,
5929 Dwarf_Unsigned index,
5930 Dwarf_Bool has_skip_or_branch,
5931 struct OpBranchHead_s *oparray,
5932 int *stackchange,
5933 Dwarf_Signed *branchdistance,
5934 int * zerostackdepth,
5935 struct esb_s *string_out,
5936 Dwarf_Error *err)
5937 {
5938 Dwarf_Small op = 0;
5939 Dwarf_Unsigned opd1 = 0;
5940 Dwarf_Unsigned opd2 = 0;
5941 Dwarf_Unsigned opd3 = 0;
5942 Dwarf_Unsigned offsetforbranch = 0;
5943 const char * op_name = 0;
5944 int indentprespaces = 0;
5945 int indentpostspaces = 0;
5946 Dwarf_Bool showblockoffsets = FALSE;
5947 struct OpBranchEntry_s *echecking = 0;
5948
5949 emit_op_indentation(string_out,die_indent_level,index);
5950 if (!glflags.dense && !glflags.gf_expr_ops_joined) {
5951 indentprespaces = standard_indent();
5952 indentpostspaces = 6;
5953 }
5954 {
5955 /* DWARF 2,3,4 and DWARF5 style */
5956 int res = 0;
5957 res = dwarf_get_location_op_value_c(exprc,
5958 index,
5959 &op,&opd1,&opd2,&opd3,
5960 &offsetforbranch,
5961 err);
5962 if (res != DW_DLV_OK) {
5963 print_error_and_continue(
5964 "dwarf_get_location_op_value_c "
5965 "did not get a value!",
5966 res,*err);
5967 return res;
5968 }
5969 }
5970 op_name = get_OP_name(op,pd_dwarf_names_print_on_error);
5971 if (has_skip_or_branch &&
5972 glflags.verbose) {
5973 showblockoffsets = TRUE;
5974 }
5975 if (showblockoffsets ) {
5976 /* New January 2021, showing offsets relevant to
5977 DW_OP_bra and DW_OP_skip .
5978 offsetforbranch comes from a 16 bit field, so
5979 is never large.
5980 */
5981 esb_append_printf_u(string_out,
5982 "<blkoff 0x%04" DW_PR_DUx "> ",
5983 offsetforbranch);
5984 }
5985 if (oparray && oparray->bh_opcount &&
5986 (Dwarf_Half)index < oparray->bh_opcount) {
5987 echecking = oparray->bh_ops_array+ index;
5988 echecking->op = op;
5989 echecking->offset = offsetforbranch;
5990 }
5991 esb_append(string_out, op_name);
5992 *stackchange = _dwarf_opscounttab[op].oc_stackchange;
5993 if (op_has_no_operands(op)) {
5994 /* Nothing to add. */
5995 } else if (op >= DW_OP_breg0 && op <= DW_OP_breg31) {
5996 esb_append_printf_i(string_out,
5997 "%+" DW_PR_DSd , opd1);
5998 } else {
5999 switch (op) {
6000 case DW_OP_addr:
6001 bracket_hex(" ",opd1,"",string_out);
6002 break;
6003 case DW_OP_const1s:
6004 case DW_OP_const2s:
6005 case DW_OP_const4s:
6006 case DW_OP_const8s:
6007 case DW_OP_consts:
6008 case DW_OP_fbreg:
6009 esb_append(string_out," ");
6010 formx_signed(opd1,string_out);
6011 break;
6012 case DW_OP_skip:
6013 case DW_OP_bra: {
6014 Dwarf_Signed as_signed = (Dwarf_Signed)opd1;
6015 esb_append(string_out," ");
6016 formx_signed(as_signed,string_out);
6017 *branchdistance = as_signed;
6018 if (showblockoffsets) {
6019 /* offsetforbranch is the op offset,
6020 and we need the the value 1 past
6021 the bytes in the DW_OP */
6022 Dwarf_Signed off = offsetforbranch +2+1;
6023
6024 off = off + as_signed;
6025 if (off < 0 ) {
6026 esb_append_printf_i(string_out,
6027 " <ERROR. branch/skip target erroneous %d>",
6028 off);
6029 glflags.gf_count_major_errors++;
6030 } else {
6031 esb_append_printf_u(string_out,
6032 " <target op: 0x%04" DW_PR_DUx ">",
6033 (Dwarf_Unsigned)off);
6034 }
6035 if (echecking) {
6036 echecking->target_offset = off;
6037 }
6038 }
6039 }
6040 break;
6041 case DW_OP_GNU_addr_index: /* unsigned val */
6042 case DW_OP_addrx: /* DWARF5: unsigned val */
6043 case DW_OP_GNU_const_index:
6044 case DW_OP_constx: /* DWARF5: unsigned val */
6045 case DW_OP_const1u:
6046 case DW_OP_const2u:
6047 case DW_OP_const4u:
6048 case DW_OP_const8u:
6049 case DW_OP_constu:
6050 case DW_OP_pick:
6051 case DW_OP_plus_uconst:
6052 case DW_OP_regx:
6053 case DW_OP_piece:
6054 case DW_OP_deref_size:
6055 case DW_OP_xderef_size:
6056 if (op == DW_OP_piece) {
6057 *zerostackdepth = TRUE;
6058 }
6059 esb_append_printf_u(string_out,
6060 " %" DW_PR_DUu , opd1);
6061 #if 0 /* add later? */
6062 Turn on later
6063 if (opd1 > 9) {
6064 esb_append_printf_u(string_out,
6065 " (0x%" DW_PR_XZEROS DW_PR_DUx ")",
6066 opd1);
6067 }
6068 #endif
6069 break;
6070 case DW_OP_bregx:
6071 bracket_hex(" ",opd1,"",string_out);
6072 esb_append(string_out,"+");
6073 formx_signed(opd2,string_out);
6074 break;
6075 case DW_OP_call2: {
6076 bracket_hex(" ",opd1,"",string_out);
6077 check_die_expr_op_basic_data(dbg,die,op_name,
6078 indentprespaces,die_indent_level,indentpostspaces,
6079 NO_SPECIFIC_TAG,NON_ZERO_OFFSET_REQUIRED,
6080 WITHIN_CU,opd1,string_out);
6081 }
6082 break;
6083 case DW_OP_call4: {
6084 bracket_hex(" ",opd1,"",string_out);
6085 check_die_expr_op_basic_data(dbg,die,op_name,
6086 indentprespaces,die_indent_level,indentpostspaces,
6087 NO_SPECIFIC_TAG,NON_ZERO_OFFSET_REQUIRED,
6088 WITHIN_CU,opd1,string_out);
6089 }
6090 break;
6091 case DW_OP_call_ref: {
6092 bracket_hex(" ",opd1,"",string_out);
6093 check_die_expr_op_basic_data(dbg,die,op_name,
6094 indentprespaces,die_indent_level,indentpostspaces,
6095 NO_SPECIFIC_TAG,NON_ZERO_OFFSET_REQUIRED,
6096 WITHIN_CU,opd1,string_out);
6097 }
6098 break;
6099 case DW_OP_bit_piece:
6100 *zerostackdepth = TRUE;
6101 bracket_hex(" ",opd1,"",string_out);
6102 bracket_hex(" offset ",opd2,"",string_out);
6103 break;
6104 case DW_OP_implicit_value:
6105 {
6106 unsigned long print_len = (unsigned long)opd1;
6107 Dwarf_Block inr;
6108 int inres = 0;
6109
6110 bracket_hex(" ",opd1,"",string_out);
6111 /* The other operand is a block of opd1 bytes. */
6112 {
6113 const unsigned char *bp = 0;
6114 /* This is a really ugly cast, a way
6115 to implement DW_OP_implicit value in
6116 this libdwarf context. */
6117 bp = (const unsigned char *)(uintptr_t) opd2;
6118 show_contents(string_out,print_len,bp);
6119 if (looks_like_string(print_len,bp)) {
6120 emit_op_indentation(string_out,
6121 die_indent_level,index);
6122 esb_append_printf_s(string_out,
6123 "contents='%s'",(const char *)bp);
6124 } else {
6125 inr.bl_len = opd1;
6126 inr.bl_data = (Dwarf_Ptr)(uintptr_t)opd2;
6127 inr.bl_from_loclist = DW_LKIND_expression;
6128 inr.bl_section_offset = 0;
6129 inres = print_expression_inner_block(dbg,die,
6130 die_indent_level+1,&inr,
6131 string_out,
6132 err);
6133 if (inres == DW_DLV_ERROR) {
6134 DROP_ERROR_INSTANCE(dbg,inres,*err);
6135 }
6136 }
6137 }
6138 }
6139 break;
6140
6141 /* We do not know what the operands, if any, are. */
6142 case DW_OP_HP_unknown:
6143 case DW_OP_HP_is_value:
6144 case DW_OP_HP_fltconst4:
6145 case DW_OP_HP_fltconst8:
6146 case DW_OP_HP_mod_range:
6147 case DW_OP_HP_unmod_range:
6148 case DW_OP_HP_tls:
6149 case DW_OP_INTEL_bit_piece:
6150 break;
6151 case DW_OP_stack_value: /* DWARF4 */
6152 break;
6153 case DW_OP_GNU_uninit: /* DW_OP_APPLE_uninit */
6154 /* No operands. */
6155 break;
6156 case DW_OP_GNU_encoded_addr:
6157 bracket_hex(" ",opd1,"",string_out);
6158 break;
6159 case DW_OP_GNU_variable_value: {
6160 bracket_hex(" ",opd1,"",string_out);
6161 check_die_expr_op_basic_data(dbg,die,op_name,
6162 indentprespaces,die_indent_level,indentpostspaces,
6163 NO_SPECIFIC_TAG,NON_ZERO_OFFSET_REQUIRED,
6164 WITHIN_CU,opd1,string_out);
6165 }
6166 break;
6167 case DW_OP_implicit_pointer: /* DWARF5 */
6168 case DW_OP_GNU_implicit_pointer: {
6169 /* opd1 is a section offset, not a CU offset.
6170 We don't know if DW_OP_GNU_implicit_pointer
6171 allows a zero offset meaning 'generic type'
6172 but GNU C++ 4.9.x-google 20150123 (prerelease)
6173 generates zero in DWARF4.
6174 DWARF5 does not allow zero. */
6175 bracket_hex(" ",opd1,"",string_out);
6176 esb_append(string_out, " ");
6177 formx_signed(opd2,string_out);
6178 check_die_expr_op_basic_data(dbg,die,op_name,
6179 indentprespaces,die_indent_level,indentpostspaces,
6180 NO_SPECIFIC_TAG,
6181 op= DW_OP_implicit_pointer?
6182 ZERO_OFFSET_GENERIC_TYPE:
6183 NON_ZERO_OFFSET_REQUIRED,
6184 !WITHIN_CU,opd1,string_out);
6185 }
6186 break;
6187 case DW_OP_entry_value: /* DWARF5 */
6188 case DW_OP_GNU_entry_value: {
6189 const unsigned char *bp = 0;
6190 unsigned long length = (unsigned long)opd1;
6191 Dwarf_Block inr;
6192 int inres = 0;
6193
6194 bracket_hex(" ",opd1,"",string_out);
6195 bp = (Dwarf_Small *)(uintptr_t) opd2;
6196 if (!bp) {
6197 esb_append(string_out,
6198 "ERROR: Null databyte pointer "
6199 "DW_OP_entry_value ");
6200 } else {
6201 show_contents(string_out,length,bp);
6202 if (looks_like_string(length,bp)) {
6203 emit_op_indentation(string_out,
6204 die_indent_level,index);
6205 esb_append_printf_s(string_out,
6206 "contents='%s'",(const char *)bp);
6207 } else {
6208 inr.bl_len = opd1;
6209 inr.bl_data = (Dwarf_Ptr)(uintptr_t)opd2;
6210 inr.bl_from_loclist = DW_LKIND_expression;
6211 inr.bl_section_offset = 0;
6212 inres = print_expression_inner_block(dbg,die,
6213 die_indent_level+1,&inr,
6214 string_out,
6215 err);
6216 if (inres == DW_DLV_ERROR) {
6217 DROP_ERROR_INSTANCE(dbg,inres,*err);
6218 }
6219 }
6220 }
6221 }
6222 break;
6223 case DW_OP_const_type: /* DWARF5 */
6224 case DW_OP_GNU_const_type:
6225 {
6226 const unsigned char *bp = 0;
6227 Dwarf_Unsigned length = 0;
6228 /* opd1 is cu-relative offset of type DIE.
6229 we have a die in the relevant CU in the arg
6230 list */
6231
6232 esb_append_printf_u(string_out," 0x%"
6233 DW_PR_DUx,opd1);
6234 length = opd2;
6235 esb_append(string_out," const length: ");
6236 esb_append_printf_u(string_out,
6237 "%u" , length);
6238 /* Now point to the data bytes of the const. */
6239 bp = (Dwarf_Small *)(uintptr_t)opd3;
6240 if (!bp) {
6241 esb_append(string_out,
6242 "ERROR: Null databyte pointer DW_OP_const_type ");
6243 } else {
6244 show_contents(string_out,(unsigned long)length,bp);
6245 if (looks_like_string((unsigned long)length,bp)) {
6246 emit_op_indentation(string_out,
6247 die_indent_level,index);
6248 esb_append_printf_s(string_out,
6249 "contents='%s'",(const char *)bp);
6250 }
6251 }
6252 esb_append(string_out,"\n");
6253 #if 0 /* add later? */
6254 append_indent_prefix(string_out,indentprespaces,
6255 die_indent_level,indentpostspaces+2);
6256 esb_append(string_out," Target Die: ");
6257 show_target_die_offset_name(dbg,die,opd1,
6258 string_out, err);
6259 #endif
6260
6261 check_die_expr_op_basic_data(dbg,die,op_name,
6262 indentprespaces,die_indent_level,indentpostspaces,
6263 DW_TAG_base_type, NON_ZERO_OFFSET_REQUIRED,
6264 WITHIN_CU,opd1,string_out);
6265 }
6266 break;
6267 case DW_OP_regval_type: /* DWARF5 */
6268 case DW_OP_GNU_regval_type: {
6269 esb_append_printf_u(string_out,
6270 " 0x%" DW_PR_DUx , opd1);
6271 bracket_hex(" ",opd2,"",string_out);
6272 check_die_expr_op_basic_data(dbg,die,op_name,
6273 indentprespaces,die_indent_level,indentpostspaces,
6274 DW_TAG_base_type,NON_ZERO_OFFSET_REQUIRED,
6275 WITHIN_CU,opd2,string_out);
6276 }
6277 break;
6278 case DW_OP_xderef_type: /* DWARF5 */
6279 case DW_OP_deref_type: /* DWARF5 */
6280 case DW_OP_GNU_deref_type: {
6281 esb_append_printf_u(string_out,
6282 " 0x%02" DW_PR_DUx , opd1);
6283 bracket_hex(" ",opd2,"",string_out);
6284 check_die_expr_op_basic_data(dbg,die,op_name,
6285 indentprespaces,die_indent_level,indentpostspaces,
6286 DW_TAG_base_type,NON_ZERO_OFFSET_REQUIRED,
6287 WITHIN_CU,opd2,string_out);
6288 }
6289 break;
6290 case DW_OP_convert: /* DWARF5 */
6291 case DW_OP_GNU_convert:
6292 case DW_OP_reinterpret: /* DWARF5 */
6293 case DW_OP_GNU_reinterpret:
6294 /* For following case, unsure if non-zero opd2
6295 is required or not. Assume not */
6296 case DW_OP_GNU_parameter_ref: {
6297 esb_append_printf_u(string_out,
6298 " 0x%02" DW_PR_DUx , opd1);
6299 check_die_expr_op_basic_data(dbg,die,op_name,
6300 indentprespaces,die_indent_level,indentpostspaces,
6301 DW_TAG_base_type,ZERO_OFFSET_GENERIC_TYPE,
6302 WITHIN_CU,opd2,string_out);
6303 }
6304 break;
6305 default:
6306 {
6307 esb_append_printf_u(string_out,
6308 " DWARF DW_OP_ unknown 0x%x", (unsigned)op);
6309 }
6310 break;
6311 }
6312 }
6313 return DW_DLV_OK;
6314 }
6315
6316 void
6317 loc_error_check(
6318 const char * tagname,
6319 const char * attrname,
6320 Dwarf_Addr lopcfinal,
6321 Dwarf_Addr rawlopc,
6322 Dwarf_Addr hipcfinal,
6323 Dwarf_Addr rawhipc,
6324 Dwarf_Unsigned offset,
6325 Dwarf_Addr base_address,
6326 Dwarf_Bool *bError)
6327 {
6328 DWARF_CHECK_COUNT(locations_result,1);
6329
6330 /* Check the low_pc and high_pc are within
6331 a valid range in the .text section */
6332 if (IsValidInBucketGroup(glflags.pRangesInfo,lopcfinal) &&
6333 IsValidInBucketGroup(glflags.pRangesInfo,hipcfinal)) {
6334 /* Valid values; do nothing */
6335 } else {
6336 /* At this point may be dealing with
6337 a linkonce symbol */
6338 if (IsValidInLinkonce(glflags.pLinkonceInfo,
6339 glflags.PU_name,
6340 lopcfinal,hipcfinal)) {
6341 /* Valid values; do nothing */
6342 } else {
6343 struct esb_s m;
6344
6345 esb_constructor(&m);
6346 *bError = TRUE;
6347 esb_append_printf_s(&m,
6348 ".debug_loc[lists]: Address outside a "
6349 "valid .text range: TAG %s",tagname);
6350 esb_append_printf_s(&m," with attribute %s.",
6351 attrname);
6352 DWARF_CHECK_ERROR(locations_result,
6353 esb_get_string(&m));
6354 if ( glflags.gf_check_verbose_mode && PRINTING_UNIQUE) {
6355 printf(
6356 "Offset = 0x%" DW_PR_XZEROS DW_PR_DUx
6357 ", Base = 0x%" DW_PR_XZEROS DW_PR_DUx ", "
6358 "Low = 0x%" DW_PR_XZEROS DW_PR_DUx
6359 " (rawlow = 0x%" DW_PR_XZEROS DW_PR_DUx
6360 "), High = 0x%" DW_PR_XZEROS DW_PR_DUx
6361 " (rawhigh = 0x%" DW_PR_XZEROS DW_PR_DUx ")\n",
6362 offset,base_address,
6363 lopcfinal,
6364 rawlopc,
6365 hipcfinal,
6366 rawhipc);
6367 }
6368 esb_destructor(&m);
6369 }
6370 }
6371 }
6372
6373 /* See also print_loclists.c print_offset_entry_table() */
6374 static void
6375 print_ll_offsets_table(Dwarf_Debug dbg,
6376 Dwarf_Unsigned goff,
6377 Dwarf_Unsigned contextnum,
6378 Dwarf_Unsigned offset_table_offset,
6379 Dwarf_Unsigned offset_entry_count,
6380 Dwarf_Unsigned offset_size,
6381 struct esb_s *esbp,
6382 Dwarf_Error *error)
6383 {
6384 Dwarf_Unsigned e = 0;
6385 Dwarf_Unsigned colmax = 2;
6386 Dwarf_Unsigned col = 0;
6387 Dwarf_Unsigned loff = offset_table_offset;
6388 int hasnewline = FALSE;
6389 int res = 0;
6390
6391 goff = goff + loff;
6392 for ( ; e < offset_entry_count; ++e) {
6393 Dwarf_Unsigned value = 0;
6394
6395 if (e == 0) {
6396 esb_append_printf_s(esbp,"%s","\n");
6397 esb_append_printf_u(esbp,
6398 " Location Offset Table at 0x%" DW_PR_XZEROS
6399 DW_PR_DUx "\n",loff);
6400 esb_append_printf_u(esbp,
6401 " (Added 0x%x to value for actual offsets)\n",
6402 loff);
6403 esb_append_printf_s(esbp,"%s"," [goff][loff][index]\n");
6404 }
6405 hasnewline = FALSE;
6406 res = dwarf_get_loclist_offset_index_value(dbg,
6407 contextnum,e,&value,0,error);
6408 if (res != DW_DLV_OK) {
6409 if (res == DW_DLV_ERROR) {
6410 char *msg = dwarf_errmsg(*error);
6411 esb_append_printf_s(esbp,
6412 "ERROR Offset table value not available %s\n",
6413 msg);
6414 dwarf_dealloc_error(dbg,*error);
6415 *error = 0;
6416 } else {
6417 esb_append(esbp,
6418 "Offset table value not available%s\n");
6419 }
6420 }
6421 if (col == 0) {
6422 esb_append_printf_u(esbp,
6423 " [0x%" DW_PR_XZEROS DW_PR_DUx "]",
6424 goff);
6425 esb_append_printf_u(esbp,
6426 "[0x%" DW_PR_XZEROS DW_PR_DUx "]",
6427 loff);
6428 esb_append_printf_u(esbp,"[%2" DW_PR_DUu "]",e);
6429
6430 }
6431 esb_append_printf_u(esbp,
6432 " 0x%" DW_PR_XZEROS DW_PR_DUx, value);
6433 esb_append_printf_u(esbp,
6434 "(0x%" DW_PR_XZEROS DW_PR_DUx ")",
6435 value+offset_table_offset);
6436 loff += offset_size;
6437 goff += offset_size;
6438 col++;
6439 if (col == colmax) {
6440 esb_append_printf_s(esbp,"%s","\n");
6441 hasnewline = TRUE;
6442 col = 0;
6443 }
6444 }
6445 if (!hasnewline) {
6446 esb_append_printf_s(esbp,"%s","\n");
6447 }
6448 }
6449
6450 static void
6451 print_loclists_context_head(Dwarf_Debug dbg,
6452 Dwarf_Small lkind,
6453 Dwarf_Unsigned lle_count,
6454 Dwarf_Unsigned lle_version,
6455 Dwarf_Unsigned loclists_index,
6456 Dwarf_Unsigned bytes_total_in_lle,
6457 Dwarf_Half offset_size,
6458 Dwarf_Half address_size,
6459 Dwarf_Half segment_selector_size,
6460 Dwarf_Unsigned overall_offset_of_this_context,
6461 Dwarf_Unsigned total_length_of_this_context,
6462 Dwarf_Unsigned offset_table_offset,
6463 Dwarf_Unsigned offset_table_entrycount,
6464 Dwarf_Bool loclists_base_present,
6465 Dwarf_Addr loclists_base,
6466 Dwarf_Bool loclists_base_address_present,
6467 Dwarf_Addr loclists_base_address,
6468 Dwarf_Bool loclists_debug_addr_base_present,
6469 Dwarf_Addr loclists_debug_addr_base,
6470 Dwarf_Unsigned loclists_offset_lle_set,
6471 struct esb_s *esbp,
6472 Dwarf_Error *error)
6473 {
6474 Dwarf_Unsigned goff = overall_offset_of_this_context;
6475 append_local_prefix(esbp);
6476 esb_append_printf_u(esbp,
6477 "bytes total this loclist: %3u",bytes_total_in_lle);
6478 append_local_prefix(esbp);
6479 esb_append_printf_u(esbp,
6480 "number of entries : %3u", lle_count);
6481 append_local_prefix(esbp);
6482 esb_append_printf_u(esbp,
6483 "context number : %3u",loclists_index);
6484 append_local_prefix(esbp);
6485 esb_append_printf_u(esbp,
6486 "version : %3u",lle_version);
6487 append_local_prefix(esbp);
6488 esb_append_printf_u(esbp,
6489 "address size : %3u",address_size);
6490 append_local_prefix(esbp);
6491 esb_append_printf_u(esbp,
6492 "offset size : %3u",offset_size);
6493 if (segment_selector_size) {
6494 append_local_prefix(esbp);
6495 esb_append_printf_u(esbp,
6496 "segment selector size : %3u",
6497 segment_selector_size);
6498 }
6499 if (lkind == DW_LKIND_loclists) {
6500 append_local_prefix(esbp);
6501 /* FIXME rename as
6502 context global offset
6503 once all else stabilized. */
6504 esb_append_printf_u(esbp,
6505 "offset of context : 0x%"
6506 DW_PR_XZEROS DW_PR_DUx,
6507 overall_offset_of_this_context);
6508 append_local_prefix(esbp);
6509 esb_append_printf_u(esbp,
6510 "offset table entrycount : %3u",
6511 offset_table_entrycount);
6512 append_local_prefix(esbp);
6513 esb_append_printf_u(esbp,
6514 "offset table offset : 0x%"
6515 DW_PR_XZEROS DW_PR_DUx,
6516 offset_table_offset);
6517 append_local_prefix(esbp);
6518 esb_append_printf_u(esbp,
6519 "offset of this list set : 0x%"
6520 DW_PR_XZEROS DW_PR_DUx,
6521 loclists_offset_lle_set);
6522 append_local_prefix(esbp);
6523 esb_append_printf_u(esbp,
6524 "length of context : %3u",
6525 total_length_of_this_context);
6526 append_local_prefix(esbp);
6527 esb_append_printf_u(esbp,
6528 "end of context offset : 0x%"
6529 DW_PR_XZEROS DW_PR_DUx,
6530 overall_offset_of_this_context +
6531 total_length_of_this_context);
6532 if (offset_table_entrycount) {
6533 esb_append_printf_s(esbp,"%s\n","");
6534 append_local_prefix(esbp);
6535 print_ll_offsets_table(dbg,goff,
6536 loclists_index, offset_table_offset,
6537 offset_table_entrycount,offset_size,
6538 esbp, error);
6539 }
6540 }
6541
6542 if (loclists_base_present) {
6543 append_local_prefix(esbp);
6544 esb_append_printf_u(esbp,
6545 "loclists base offset : 0x%"
6546 DW_PR_XZEROS DW_PR_DUx,
6547 loclists_base);
6548 }
6549 if (loclists_base_address_present) {
6550 append_local_prefix(esbp);
6551 esb_append_printf_u(esbp,
6552 "DW_AT_low_pc(base addr) : 0x%"
6553 DW_PR_XZEROS DW_PR_DUx,
6554 loclists_base_address);
6555 }
6556 if (loclists_debug_addr_base_present) {
6557 append_local_prefix(esbp);
6558 esb_append_printf_u(esbp,
6559 "DW_AT_addr_base : 0x%"
6560 DW_PR_XZEROS DW_PR_DUx,
6561 loclists_debug_addr_base);
6562 }
6563 esb_append(esbp,"\n");
6564 }
6565
6566 /* Fill buffer with location lists data for printing.
6567 This does the details.
6568 It's up to the caller to determine which
6569 esb to put the result in. */
6570 /*ARGSUSED*/
6571 static int
6572 print_location_list(Dwarf_Debug dbg,
6573 Dwarf_Die die,
6574 Dwarf_Attribute attr,
6575 Dwarf_Bool checking,
6576 int die_indent_level,
6577 int no_end_newline,
6578 struct esb_s *details,
6579 Dwarf_Error* llerr)
6580 {
6581 Dwarf_Unsigned no_of_elements = 0;
6582 Dwarf_Loc_Head_c loclist_head = 0; /* For DWARF2-DWARF5 */
6583 int lres = 0;
6584 unsigned int llent = 0;
6585
6586 /* Base address used to update entries in .debug_loc.
6587 CU_base_address is a global. Terrible way to
6588 pass in this value. FIXME. See also CU_low_address
6589 as base address is special for address ranges */
6590 Dwarf_Addr base_address = glflags.CU_base_address;
6591 Dwarf_Addr lopc = 0;
6592 Dwarf_Addr hipc = 0;
6593 Dwarf_Bool bError = FALSE;
6594 Dwarf_Small lle_value = 0; /* DWARF5 */
6595 Dwarf_Unsigned lle_count = 0;
6596 Dwarf_Unsigned loclists_index = 0;
6597
6598 /* This is the section offset of the expression, not
6599 the location description prefix. */
6600 Dwarf_Unsigned expr_section_offset = 0;
6601 Dwarf_Half address_size = 0;
6602 Dwarf_Half segment_selector_size = 0;
6603 Dwarf_Addr max_address = 0;
6604 Dwarf_Unsigned lle_version = 2;
6605 Dwarf_Half version = 2;
6606 Dwarf_Half offset_size = 4;
6607 Dwarf_Small lkind = DW_LKIND_unknown;
6608 Dwarf_Unsigned bytes_total_in_lle = 0;
6609 Dwarf_Unsigned overall_offset_of_this_context = 0;
6610 Dwarf_Unsigned total_length_of_this_context = 0;
6611 Dwarf_Bool loclists_base_present = FALSE;
6612 Dwarf_Unsigned loclists_base = 0;
6613 Dwarf_Bool loclists_base_address_present = FALSE;
6614 Dwarf_Unsigned loclists_base_address = 0;
6615 Dwarf_Bool loclists_debug_addr_base_present = FALSE;
6616 Dwarf_Unsigned loclists_debug_addr_base = 0;
6617 Dwarf_Unsigned loclists_offset_lle_set = 0;
6618 Dwarf_Unsigned offset_table_offset = 0;
6619 Dwarf_Unsigned offset_table_entrycount = 0;
6620 struct esb_s section_truename;
6621 Dwarf_Unsigned goff = 0;
6622 Dwarf_Unsigned loff = 0;
6623
6624 lres = dwarf_get_version_of_die(die,&version,
6625 &offset_size);
6626 if (lres != DW_DLV_OK) {
6627 /* Is DW_DLV_ERROR (see libdwarf query.c),
6628 there is no error argument. */
6629 simple_err_only_return_action(lres,
6630 "\nERROR: die or context bad calling "
6631 "dwarf_get_version_of_die in print_location_list."
6632 " Something is very wrong.");
6633 return DW_DLV_NO_ENTRY;
6634 }
6635
6636 lres = get_address_size_and_max(dbg,&address_size,
6637 &max_address,llerr);
6638 if (lres != DW_DLV_OK) {
6639 print_error_and_continue("Getting address size"
6640 " and maximum failed while getting location list",
6641 lres,*llerr);
6642 return lres;
6643 }
6644 {
6645 /* Preferred interface. Deals with DWARF2,3,4,5. */
6646 lres = dwarf_get_loclist_c(attr,&loclist_head,
6647 &no_of_elements,llerr);
6648 if (lres == DW_DLV_ERROR) {
6649 print_error_and_continue(
6650 "ERROR: dwarf_get_loclist_c fails",
6651 lres, *llerr);
6652 return lres;
6653 } else if (lres == DW_DLV_NO_ENTRY) {
6654 return lres;
6655 }
6656 lres = dwarf_get_loclist_head_basics(loclist_head,
6657 &lkind,
6658 &lle_count,&lle_version, &loclists_index,
6659 &bytes_total_in_lle,
6660 &offset_size, &address_size, &segment_selector_size,
6661 &overall_offset_of_this_context,
6662 &total_length_of_this_context,
6663 &offset_table_offset,
6664 &offset_table_entrycount,
6665 &loclists_base_present,&loclists_base,
6666 &loclists_base_address_present,&loclists_base_address,
6667 &loclists_debug_addr_base_present,
6668 &loclists_debug_addr_base,
6669 &loclists_offset_lle_set,llerr);
6670 if (lres != DW_DLV_OK) {
6671 dwarf_dealloc_loc_head_c(loclist_head);
6672 return lres;
6673 }
6674 goff = overall_offset_of_this_context;
6675 { /* This is a small consistency/function check. */
6676 unsigned int check_lkind = DW_LKIND_unknown;
6677 int lresx = 0;
6678
6679 lresx = dwarf_get_loclist_head_kind(loclist_head,
6680 &check_lkind,llerr);
6681 if (lresx == DW_DLV_OK) {
6682 if (check_lkind != lkind) {
6683 printf("ERROR: dwarf_get_loclist_head_kind "
6684 " returned a bogus value! 0x%x vs 0x%x\n",
6685 lkind,check_lkind);
6686 glflags.gf_count_major_errors++;
6687 }
6688 } else {
6689 /* The only possible return is DW_DLV_ERROR,
6690 and means a NULL loclist_head!.
6691 That will not happen here. */
6692 printf("ERROR: dwarf_get_loclist_head_kind "
6693 " returned a DW_DLV_ERROR!");
6694 glflags.gf_count_major_errors++;
6695 DROP_ERROR_INSTANCE(dbg,lresx,*llerr);
6696 }
6697 }
6698 version = (Dwarf_Half)lle_version;
6699 /* append_local_prefix(esbp); No,
6700 here the newline grates, causes blank
6701 line in the output. So. Just add 6 spaces.
6702 the output already has a newline. */
6703 if (lkind != DW_LKIND_expression) {
6704 esb_append(details," ");
6705 esb_constructor(&section_truename);
6706 if (lkind == DW_LKIND_loclists) {
6707 get_true_section_name(dbg,".debug_loclists",
6708 &section_truename,FALSE);
6709 } else {
6710 get_true_section_name(dbg,".debug_loc",
6711 &section_truename,FALSE);
6712 }
6713 esb_append_printf_s(details,"%-15s",
6714 esb_get_string(&section_truename));
6715 esb_append_printf_u(details,
6716 " offset :"
6717 " 0x%" DW_PR_XZEROS DW_PR_DUx,
6718 loclists_offset_lle_set);
6719 esb_destructor(&section_truename);
6720 if (glflags.verbose) {
6721 print_loclists_context_head(dbg,lkind,
6722 lle_count, lle_version, loclists_index,
6723 bytes_total_in_lle,
6724 offset_size,address_size, segment_selector_size,
6725 overall_offset_of_this_context,
6726 total_length_of_this_context,
6727 offset_table_offset, offset_table_entrycount,
6728 loclists_base_present,loclists_base,
6729 loclists_base_address_present,
6730 loclists_base_address,
6731 loclists_debug_addr_base_present,
6732 loclists_debug_addr_base,
6733 loclists_offset_lle_set,
6734 details,llerr);
6735 } else {
6736 /* things look better with this... no -v */
6737 esb_append(details,"\n ");
6738 }
6739 }
6740 }
6741 possibly_increase_esb_alloc(details, no_of_elements,100);
6742
6743 if (lkind == DW_LKIND_loclists) {
6744 loff = loclists_offset_lle_set;
6745 goff = overall_offset_of_this_context+loff;
6746 }
6747 for (llent = 0; llent < no_of_elements; ++llent) {
6748 Dwarf_Unsigned locdesc_offset = 0;
6749 Dwarf_Locdesc_c locentry = 0; /* 2015 */
6750 Dwarf_Unsigned rawlowpc = 0;
6751 Dwarf_Unsigned rawhipc = 0;
6752 Dwarf_Unsigned ulocentry_count = 0;
6753 Dwarf_Unsigned lle_byte_count = 0;
6754 Dwarf_Bool debug_addr_unavailable = FALSE;
6755 /* This has values DW_LKIND*, the same values
6756 that were in loclist source
6757 in 2019, but with
6758 the new value of DW_LKIND_loclists
6759 for DWARF5. See libdwarf.h */
6760 Dwarf_Small loclist_source = 0;
6761
6762 {
6763 lres = dwarf_get_locdesc_entry_e(loclist_head,
6764 llent,
6765 &lle_value,
6766 &rawlowpc,&rawhipc,
6767 &debug_addr_unavailable,
6768 &lopc, &hipc,
6769 &ulocentry_count,
6770 &lle_byte_count,
6771 &locentry,
6772 &loclist_source,
6773 &expr_section_offset,
6774 &locdesc_offset,
6775 llerr);
6776 if (lres == DW_DLV_ERROR) {
6777 print_error_and_continue(
6778 "ERROR: dwarf_get_locdesc_entry_c fails",
6779 lres, *llerr);
6780 dwarf_dealloc_loc_head_c(loclist_head);
6781 return lres;
6782 } else if (lres == DW_DLV_NO_ENTRY) {
6783 dwarf_dealloc_loc_head_c(loclist_head);
6784 return lres;
6785 }
6786 }
6787 if (loclist_source == DW_LKIND_expression &&
6788 lle_value != DW_LLE_start_end) {
6789 printf("ERROR: With DW_LKIND_expression"
6790 " lle_value should be DW_LLE_start_end (7) "
6791 "not %u\n",lle_value);
6792 glflags.gf_count_major_errors++;
6793 }
6794 /* If we have a location list refering to the .debug_loc
6795 Check for specific compiler we are validating. */
6796 if ( glflags.gf_check_locations && glflags.in_valid_code &&
6797 loclist_source && checking_this_compiler()) {
6798 checking = TRUE;
6799 }
6800 if (!glflags.dense &&
6801 loclist_source != DW_LKIND_expression) {
6802 Dwarf_Bool need_newline = TRUE;
6803
6804 if (llent == 0) {
6805 /* These messages go with the list of entries */
6806 switch(loclist_source){
6807 case DW_LKIND_loclist:
6808 esb_append_printf_u(details,
6809 " <loclist at offset 0x%"
6810 DW_PR_XZEROS DW_PR_DUx,
6811 loclists_offset_lle_set);
6812 esb_append_printf_i(details,
6813 " with %ld entries follows>",
6814 no_of_elements);
6815 break;
6816 case DW_LKIND_GNU_exp_list:
6817 esb_append_printf_u(details,
6818 " <dwo loclist at offset 0x%"
6819 DW_PR_XZEROS DW_PR_DUx,
6820 loclists_offset_lle_set);
6821 esb_append_printf_i(details,
6822 " with %ld entries follows>",
6823 no_of_elements);
6824 break;
6825 case DW_LKIND_loclists:
6826 esb_append_printf_u(details,
6827 " <debug_loclists offset 0x%"
6828 DW_PR_XZEROS DW_PR_DUx,
6829 loclists_offset_lle_set);
6830 esb_append_printf_i(details,
6831 " with %ld entries follows>",
6832 no_of_elements);
6833 goff = loclists_offset_lle_set;
6834 esb_append_printf_u(details, "\n [0x%"
6835 DW_PR_XZEROS
6836 DW_PR_DUx "]",goff);
6837 esb_append_printf_u(details, "[0x%"
6838 DW_PR_XZEROS
6839 DW_PR_DUx "]",loff);
6840 need_newline = FALSE;
6841 break;
6842 default: break;
6843 }
6844 }
6845 if (need_newline) {
6846 esb_append_printf_i(details, "\n [%2d]",llent);
6847 } else {
6848 esb_append_printf_i(details, "[%2d]",llent);
6849 }
6850 loff += lle_byte_count;
6851 goff += lle_byte_count;
6852 }
6853 /* We use DW_LLE names for DW_LKIND_loclist and
6854 DW_LKIND_loclists. We use LLEX names for
6855 DW_LKIND_GNU_exp_list */
6856 if (loclist_source) {
6857 Dwarf_Half tag = 0;
6858 Dwarf_Half attrnum = 0;
6859 const char *tagname = 0;
6860 const char *attrname = 0;
6861 int res = 0;
6862
6863 res = dwarf_tag(die,&tag,llerr);
6864 if (res != DW_DLV_OK) {
6865 dwarf_dealloc_loc_head_c(loclist_head);
6866 return res;
6867 }
6868 res = dwarf_whatattr(attr,&attrnum,llerr);
6869 if (res != DW_DLV_OK) {
6870 dwarf_dealloc_loc_head_c(loclist_head);
6871 return res;
6872 }
6873 attrname = get_AT_name(attrnum,FALSE);
6874 tagname = get_TAG_name(tag,FALSE);
6875 if (loclist_source == DW_LKIND_GNU_exp_list) {
6876 print_llex_linecodes(checking,
6877 tagname,
6878 attrname,
6879 llent,
6880 lle_value,
6881 base_address,
6882 rawlowpc, rawhipc,
6883 debug_addr_unavailable,
6884 lopc, hipc,
6885 locdesc_offset,
6886 details,
6887 &bError);
6888 } else if (loclist_source == DW_LKIND_loclist) {
6889 print_original_loclist_linecodes(checking,
6890 tagname,
6891 attrname,
6892 llent,
6893 lle_value,
6894 base_address,
6895 rawlowpc, rawhipc,
6896 debug_addr_unavailable,
6897 lopc, hipc,
6898 locdesc_offset,
6899 details,
6900 &bError);
6901 } else {
6902 /* loclist_source == DW_LKIND_loclists */
6903 print_debug_loclists_linecodes(checking,
6904 tagname,
6905 attrname,
6906 llent,
6907 lle_value,
6908 lle_byte_count,
6909 base_address,
6910 rawlowpc, rawhipc,
6911 debug_addr_unavailable,
6912 lopc, hipc,
6913 locdesc_offset,
6914 details,
6915 &bError);
6916 }
6917 }
6918 lres = dwarfdump_print_expression_operations(dbg,
6919 die,
6920 die_indent_level,
6921 /* Either llbuf or locentry non-zero.
6922 Not both. */
6923 locentry,
6924 ulocentry_count, /* How many ops in this loc desc */
6925 details,llerr);
6926 if (lres == DW_DLV_ERROR) {
6927 dwarf_dealloc_loc_head_c(loclist_head);
6928 return lres;
6929 }
6930 }
6931 if (!no_end_newline) {
6932 if (bError && glflags.gf_check_verbose_mode &&
6933 PRINTING_UNIQUE) {
6934 esb_append(details,"\n");
6935 } else if (glflags.gf_do_print_dwarf) {
6936 esb_append(details,"\n");
6937 }
6938 }
6939 dwarf_dealloc_loc_head_c(loclist_head);
6940 return DW_DLV_OK;
6941 }
6942
6943 /* New October 2017.
6944 The 'decimal' representation here is questionable.
6945 */
6946 static void
6947 formx_data16(Dwarf_Form_Data16 * u,
6948 struct esb_s *esbp, Dwarf_Bool hex_format)
6949 {
6950 unsigned i = 0;
6951
6952 for ( ; i < sizeof(Dwarf_Form_Data16); ++i){
6953 esb_append(esbp, "0x");
6954 if (hex_format) {
6955 esb_append_printf_u(esbp,
6956 "%02x ", u->fd_data[i]);
6957 } else {
6958 esb_append_printf_i(esbp, "%02d ", u->fd_data[i]);
6959 }
6960 }
6961 }
6962
6963 static void
6964 formx_unsigned(Dwarf_Unsigned u,
6965 struct esb_s *esbp,
6966 Dwarf_Bool hex_format)
6967 {
6968 if (hex_format) {
6969 esb_append_printf_u(esbp,
6970 "0x%" DW_PR_XZEROS DW_PR_DUx , u);
6971 } else {
6972 esb_append_printf_u(esbp,
6973 "%" DW_PR_DUu , u);
6974 }
6975 }
6976
6977 static void
6978 formx_signed(Dwarf_Signed s, struct esb_s *esbp)
6979 {
6980 esb_append_printf_i(esbp, "%" DW_PR_DSd ,s);
6981 }
6982 static void
6983 formx_unsigned_and_signed_if_neg(Dwarf_Unsigned tempud,
6984 Dwarf_Signed tempd,
6985 const char *leader,Dwarf_Bool hex_format,struct esb_s*esbp)
6986 {
6987 formx_unsigned(tempud,esbp,hex_format);
6988 if (tempd < 0) {
6989 esb_append(esbp,leader);
6990 formx_signed(tempd,esbp);
6991 esb_append(esbp,")");
6992 }
6993 }
6994
6995 /* If the DIE DW_AT_type exists and is directly known signed/unsigned
6996 return -1 for signed 1 for unsigned.
6997 Otherwise return 0 meaning 'no information'.
6998 So we only need to a messy lookup once per type-die offset */
6999 static int
7000 check_for_type_unsigned(Dwarf_Debug dbg,
7001 Dwarf_Die die)
7002 {
7003 Dwarf_Bool is_info = 0;
7004 struct Helpertree_Base_s * helperbase = 0;
7005 struct Helpertree_Map_Entry_s *e = 0;
7006 int res = 0;
7007 Dwarf_Attribute attr = 0;
7008 Dwarf_Attribute encodingattr = 0;
7009 Dwarf_Error error = 0;
7010 Dwarf_Unsigned diegoffset = 0;
7011 Dwarf_Unsigned typedieoffset = 0;
7012 Dwarf_Die typedie = 0;
7013 Dwarf_Unsigned tempud = 0;
7014 int show_form_here = FALSE;
7015 int retval = 0;
7016
7017 if (!die) {
7018 return 0;
7019 }
7020 is_info = dwarf_get_die_infotypes_flag(die);
7021 if (is_info) {
7022 helperbase = &helpertree_offsets_base_info;
7023 } else {
7024 helperbase = &helpertree_offsets_base_types;
7025 }
7026 res = dwarf_dieoffset(die,&diegoffset,&error);
7027 if (res == DW_DLV_ERROR) {
7028 dwarf_dealloc_error(dbg,error);
7029 return 0;
7030 } else if (res == DW_DLV_NO_ENTRY) {
7031 /* We don't know sign. */
7032 return 0;
7033 }
7034 /* This might be wrong. See the typedieoffset check below,
7035 which is correct... */
7036 e = helpertree_find(diegoffset,helperbase);
7037 if (e) {
7038 return e->hm_val;
7039 }
7040
7041 /* We look up the DW_AT_type die, if any, and
7042 use that offset to check for signedness. */
7043
7044 res = dwarf_attr(die, DW_AT_type, &attr,&error);
7045 if (res == DW_DLV_ERROR) {
7046 dwarf_dealloc_error(dbg,error);
7047 helpertree_add_entry(diegoffset, 0,helperbase);
7048 return 0;
7049 }
7050 if (res == DW_DLV_NO_ENTRY) {
7051 /* We don't know sign. */
7052 helpertree_add_entry(diegoffset, 0,helperbase);
7053 return 0;
7054 }
7055 res = dwarf_global_formref_b(attr, &typedieoffset,
7056 &is_info,&error);
7057 if (res == DW_DLV_ERROR) {
7058 dwarf_dealloc_attribute(attr);
7059 helpertree_add_entry(diegoffset, 0,helperbase);
7060 return 0;
7061 }
7062 if (res == DW_DLV_NO_ENTRY) {
7063 dwarf_dealloc_attribute(attr);
7064 helpertree_add_entry(diegoffset, 0,helperbase);
7065 return 0;
7066 }
7067 dwarf_dealloc_attribute(attr);
7068 attr = 0;
7069 e = helpertree_find(typedieoffset,helperbase);
7070 if (e) {
7071 return e->hm_val;
7072 }
7073
7074 res = dwarf_offdie_b(dbg,typedieoffset,is_info, &typedie,&error);
7075 if (res == DW_DLV_ERROR) {
7076 dwarf_dealloc_error(dbg,error);
7077 helpertree_add_entry(diegoffset, 0,helperbase);
7078 helpertree_add_entry(typedieoffset, 0,helperbase);
7079 return 0;
7080 } else if (res == DW_DLV_NO_ENTRY) {
7081 helpertree_add_entry(diegoffset, 0,helperbase);
7082 helpertree_add_entry(typedieoffset, 0,helperbase);
7083 return 0;
7084 }
7085 res = dwarf_attr(typedie, DW_AT_encoding, &encodingattr,&error);
7086 if (res == DW_DLV_ERROR) {
7087 dwarf_dealloc_error(dbg,error);
7088 dwarf_dealloc_die(typedie);
7089 helpertree_add_entry(diegoffset, 0,helperbase);
7090 helpertree_add_entry(typedieoffset, 0,helperbase);
7091 return 0;
7092 }
7093 if (res == DW_DLV_NO_ENTRY) {
7094 dwarf_dealloc_die(typedie);
7095 helpertree_add_entry(diegoffset, 0,helperbase);
7096 helpertree_add_entry(typedieoffset, 0,helperbase);
7097 return 0;
7098 }
7099 res = dd_get_integer_and_name(dbg,
7100 encodingattr,
7101 &tempud,
7102 /* attrname */ (const char *) NULL,
7103 /* err_string */ ( struct esb_s *) NULL,
7104 (encoding_type_func) 0,
7105 &error,show_form_here);
7106
7107 if (res != DW_DLV_OK) {
7108 DROP_ERROR_INSTANCE(dbg,res,error);
7109 /* dealloc attr *first* then die */
7110 dwarf_dealloc_attribute(encodingattr);
7111 dwarf_dealloc_die(typedie);
7112 helpertree_add_entry(diegoffset, 0,helperbase);
7113 helpertree_add_entry(typedieoffset, 0,helperbase);
7114 return 0;
7115 }
7116 if (tempud == DW_ATE_signed || tempud == DW_ATE_signed_char) {
7117 /*esb_append(esbp,"helper small encoding SIGNED ");*/
7118 retval = -1;
7119 } else {
7120 if (tempud == DW_ATE_unsigned ||
7121 tempud == DW_ATE_unsigned_char) {
7122 retval = 1;
7123 }
7124 }
7125 helpertree_add_entry(diegoffset,retval,helperbase);
7126 helpertree_add_entry(typedieoffset, retval,helperbase);
7127 /* dealloc attr *first* then die */
7128 dwarf_dealloc_attribute(encodingattr);
7129 dwarf_dealloc_die(typedie);
7130 return retval;
7131 }
7132
7133 /* We think this is an integer. Figure out how to print it.
7134 In case the signedness is ambiguous (such as on
7135 DW_FORM_data1 (ie, unknown signedness) print two ways.
7136
7137 If we were to look at DW_AT_type in the base DIE
7138 we could follow it and determine if the type
7139 was unsigned or signed (usually easily) and
7140 use that information.
7141 */
7142 static int
7143 formxdata_print_value(Dwarf_Debug dbg,
7144 Dwarf_Die die,
7145 Dwarf_Attribute attrib,
7146 Dwarf_Half theform,
7147 struct esb_s *esbp,
7148 Dwarf_Error * pverr, Dwarf_Bool hex_format)
7149 {
7150 Dwarf_Signed tempsd = 0;
7151 Dwarf_Unsigned tempud = 0;
7152 int sres = 0;
7153 int ures = 0;
7154
7155 if (theform == DW_FORM_data16) {
7156 Dwarf_Form_Data16 v16;
7157 ures = dwarf_formdata16(attrib,
7158 &v16,pverr);
7159 if (ures == DW_DLV_OK) {
7160 formx_data16(&v16,
7161 esbp,hex_format);
7162 return DW_DLV_OK;
7163 } else if (ures == DW_DLV_NO_ENTRY) {
7164 /* impossible */
7165 return ures;
7166 } else {
7167 return ures;
7168 }
7169 }
7170 ures = dwarf_formudata(attrib, &tempud, pverr);
7171 if (ures == DW_DLV_OK) {
7172 sres = dwarf_formsdata(attrib, &tempsd, pverr);
7173 if (sres == DW_DLV_OK) {
7174 if (tempud == (Dwarf_Unsigned)tempsd && tempsd >= 0) {
7175 /* Data is the same value and not negative,
7176 so makes no difference which
7177 we print. */
7178 formx_unsigned(tempud,esbp,hex_format);
7179 return DW_DLV_OK;
7180 } else {
7181 /* Here we don't know if signed or not and
7182 Assuming one or the other changes the
7183 interpretation of the bits. */
7184 int helpertree_unsigned = 0;
7185
7186 helpertree_unsigned =
7187 check_for_type_unsigned(dbg,die);
7188 if (!die || !helpertree_unsigned) {
7189 /* Signedness unclear. */
7190 formx_unsigned_and_signed_if_neg(tempud,tempsd,
7191 " (",hex_format,esbp);
7192 return DW_DLV_OK;
7193 } else if (helpertree_unsigned > 0) {
7194 formx_unsigned(tempud,esbp,hex_format);
7195 return DW_DLV_OK;
7196 } else {
7197 /* Value signed. */
7198 formx_signed(tempsd,esbp);
7199 return DW_DLV_OK;
7200 }
7201 }
7202 } else if (sres == DW_DLV_NO_ENTRY) {
7203 formx_unsigned(tempud,esbp,hex_format);
7204 return DW_DLV_OK;
7205 } else /* DW_DLV_ERROR */{
7206 formx_unsigned(tempud,esbp,hex_format);
7207 DROP_ERROR_INSTANCE(dbg,sres,*pverr);
7208 return DW_DLV_OK;
7209 }
7210 } else if (ures == DW_DLV_ERROR) {
7211 DROP_ERROR_INSTANCE(dbg,ures,*pverr);
7212 sres = dwarf_formsdata(attrib, &tempsd, pverr);
7213 if (sres == DW_DLV_OK) {
7214 formx_signed(tempsd,esbp);
7215 return sres;
7216 } else if (sres == DW_DLV_ERROR) {
7217 esb_append_printf_u(esbp,
7218 "<ERROR: form 0x%x ",theform);
7219 esb_append(esbp,get_FORM_name(theform,FALSE));
7220 esb_append(esbp,
7221 " not readable signed or unsigned>");
7222 simple_err_only_return_action(sres,
7223 esb_get_string(esbp));
7224 /* Neither worked. */
7225 return DW_DLV_ERROR;
7226 }
7227 }
7228 /* NO_ENTRY is crazy, impossible. */
7229 return DW_DLV_NO_ENTRY;
7230 }
7231
7232 static void
7233 bracket_hex(const char *s1,
7234 Dwarf_Unsigned v,
7235 const char *s2,
7236 struct esb_s * esbp)
7237 {
7238 Dwarf_Bool hex_format = TRUE;
7239 esb_append(esbp,s1);
7240 formx_unsigned(v,esbp,hex_format);
7241 esb_append(esbp,s2);
7242 }
7243
7244 /* Borrow the definition from pro_encode_nm.h */
7245 /* Bytes needed to encode a number.
7246 Not a tight bound, just a reasonable bound.
7247 */
7248 #ifndef ENCODE_SPACE_NEEDED
7249 #define ENCODE_SPACE_NEEDED (2*sizeof(Dwarf_Unsigned))
7250 #endif /* ENCODE_SPACE_NEEDED */
7251
7252 /* Table indexed by the attribute value;
7253 only standard attributes
7254 are included, ie. in the range [1..DW_AT_lo_user];
7255 we waste a
7256 little bit of space, but accessing the table is fast. */
7257
7258 typedef struct attr_encoding {
7259 Dwarf_Unsigned entries; /* Attribute occurrences */
7260 Dwarf_Unsigned formx; /* Space used by current encoding */
7261 Dwarf_Unsigned leb128; /* Space used with LEB128 encoding */
7262 } a_attr_encoding;
7263
7264 /* The other DW_FORM_datan are lower form values than data16,
7265 so the following is safe for the unchanging static table. */
7266 static int attributes_encoding_factor[DW_FORM_data16 + 1];
7267
7268 /* These must be reset for each object if we are processing
7269 an archive! see print_attributes_encoding(). */
7270 static a_attr_encoding *attributes_encoding_table = NULL;
7271 static Dwarf_Bool attributes_encoding_do_init = TRUE;
7272
7273 /* Check the potential amount of space wasted by
7274 attributes values that can
7275 be represented as an unsigned LEB128.
7276 Only attributes with forms:
7277 DW_FORM_data1, DW_FORM_data2, DW_FORM_data4 and
7278 DW_FORM_data are checked
7279 */
7280 static void
7281 check_attributes_encoding(Dwarf_Half attr,Dwarf_Half theform,
7282 Dwarf_Unsigned value)
7283 {
7284
7285 if (attributes_encoding_do_init) {
7286 /* Create table on first call */
7287 attributes_encoding_table = (a_attr_encoding *)calloc(
7288 DW_AT_lo_user,
7289 sizeof(a_attr_encoding));
7290 if (!attributes_encoding_table) {
7291 printf("\nERROR: Unable the check attributes "
7292 "encoding as calloc failed. Trying to continue\n");
7293 glflags.gf_count_major_errors++;
7294 return;
7295 }
7296 /* We use only 5 slots in the table, for quick access */
7297 /* index 0x0b */
7298 attributes_encoding_factor[DW_FORM_data1]=1; /* index 0x0b */
7299 attributes_encoding_factor[DW_FORM_data2]=2; /* index 0x05 */
7300 attributes_encoding_factor[DW_FORM_data4]=4; /* index 0x06 */
7301 attributes_encoding_factor[DW_FORM_data8]=8; /* index 0x07 */
7302
7303 /* index 0x1e */
7304 attributes_encoding_factor[DW_FORM_data16] = 16;
7305 attributes_encoding_do_init = FALSE;
7306 }
7307
7308 /* Regardless of the encoding form, count the checks. */
7309 DWARF_CHECK_COUNT(attr_encoding_result,1);
7310
7311 /* For 'DW_AT_stmt_list', due to the way is generated,
7312 the value can be unknown at compile time and only
7313 the assembler can decide how to represent the offset;
7314 ignore this attribute. */
7315 if (DW_AT_stmt_list == attr ||
7316 DW_AT_macros == attr ||
7317 DW_AT_GNU_macros == attr) {
7318 if (theform == DW_FORM_addr) {
7319 struct esb_s lesb;
7320 esb_constructor(&lesb);
7321 esb_append_printf_s(&lesb,
7322 "Attribute %s has form ",
7323 get_AT_name(attr,pd_dwarf_names_print_on_error));
7324 esb_append_printf_s(&lesb,
7325 " %s, An error",
7326 get_FORM_name(theform,
7327 pd_dwarf_names_print_on_error));
7328 DWARF_CHECK_ERROR(attr_encoding_result,
7329 esb_get_string(&lesb));
7330 esb_destructor(&lesb);
7331 }
7332 return;
7333 }
7334
7335 /* Only checks those attributes that have DW_FORM_dataX:
7336 DW_FORM_data1, DW_FORM_data2, DW_FORM_data4 and DW_FORM_data8
7337 DWARF5 adds DW_FORM_data16, but we ignore data16 here
7338 as it makes no sense as a uleb. */
7339 if (theform == DW_FORM_data1 || theform == DW_FORM_data2 ||
7340 theform == DW_FORM_data4 || theform == DW_FORM_data8 ) {
7341 int res = 0;
7342 /* Size of the byte stream buffer that needs to be
7343 memcpy-ed. */
7344 int leb128_size = 0;
7345 /* To encode the attribute value */
7346 char encode_buffer[ENCODE_SPACE_NEEDED];
7347
7348 res = dwarf_encode_leb128(value,&leb128_size,
7349 encode_buffer,sizeof(encode_buffer));
7350 if (res == DW_DLV_OK) {
7351 if (attributes_encoding_factor[theform] > leb128_size) {
7352 int wasted_bytes = attributes_encoding_factor[theform]
7353 - leb128_size;
7354 struct esb_s lesb;
7355 esb_constructor(&lesb);
7356
7357 esb_append_printf_i(&lesb,
7358 "%" DW_PR_DSd " wasted byte(s)",wasted_bytes);
7359 DWARF_CHECK_ERROR2(attr_encoding_result,
7360 get_AT_name(attr,pd_dwarf_names_print_on_error),
7361 esb_get_string(&lesb));
7362 esb_destructor(&lesb);
7363 /* Add the optimized size to the specific
7364 attribute, only if we are dealing with
7365 a standard attribute. */
7366 if (attr < DW_AT_lo_user) {
7367 attributes_encoding_table[attr].entries += 1;
7368 attributes_encoding_table[attr].formx +=
7369 attributes_encoding_factor[theform];
7370 attributes_encoding_table[attr].leb128 +=
7371 leb128_size;
7372 }
7373 }
7374 }
7375 /* ignoring error, it should be impossible. */
7376 }
7377 }
7378
7379 /* Print a detailed encoding usage per attribute -kE */
7380 int
7381 print_attributes_encoding(Dwarf_Debug dbg,
7382 Dwarf_Error* attr_error)
7383 {
7384 if (attributes_encoding_table) {
7385 Dwarf_Bool print_header = TRUE;
7386 Dwarf_Unsigned total_entries = 0;
7387 Dwarf_Unsigned total_bytes_formx = 0;
7388 Dwarf_Unsigned total_bytes_leb128 = 0;
7389 Dwarf_Unsigned entries = 0;
7390 Dwarf_Unsigned bytes_formx = 0;
7391 Dwarf_Unsigned bytes_leb128 = 0;
7392 int index;
7393 int count = 0;
7394 float saved_rate = 0.0;
7395
7396 for (index = 0; index < DW_AT_lo_user; ++index) {
7397 if (attributes_encoding_table[index].leb128) {
7398 if (print_header) {
7399 printf("\n*** SPACE USED BY ATTRIBUTE "
7400 "ENCODINGS ***\n");
7401 printf("Nro Attribute Name "
7402 " Entries Data_x leb128 Rate\n");
7403 print_header = FALSE;
7404 }
7405 entries = attributes_encoding_table[index].entries;
7406 bytes_formx = attributes_encoding_table[index].formx;
7407 bytes_leb128 =
7408 attributes_encoding_table[index].leb128;
7409 total_entries += entries;
7410 total_bytes_formx += bytes_formx;
7411 total_bytes_leb128 += bytes_leb128;
7412 saved_rate = (float)(bytes_leb128 * 100 /
7413 bytes_formx);
7414 printf("%3d %-25s "
7415 "%10" /*DW_PR_XZEROS*/ DW_PR_DUu /* Entries */
7416 " "
7417 "%10" /*DW_PR_XZEROS*/ DW_PR_DUu /* FORMx */
7418 " "
7419 "%10" /*DW_PR_XZEROS*/ DW_PR_DUu /* LEB128 */
7420 " "
7421 "%3.0f%%"
7422 "\n",
7423 ++count,
7424 get_AT_name(index,pd_dwarf_names_print_on_error),
7425 entries,
7426 bytes_formx,
7427 bytes_leb128,
7428 saved_rate);
7429 }
7430 }
7431 if (!print_header) {
7432 /* At least we have an entry, print summary
7433 and percentage */
7434 Dwarf_Addr lower = 0;
7435 Dwarf_Unsigned size = 0;
7436 int infoerr = 0;
7437
7438 saved_rate = (float)((total_bytes_leb128 * 100) /
7439 total_bytes_formx);
7440 printf("** Summary ** "
7441 "%10" /*DW_PR_XZEROS*/ DW_PR_DUu " " /* Entries */
7442 "%10" /*DW_PR_XZEROS*/ DW_PR_DUu " " /* FORMx */
7443 "%10" /*DW_PR_XZEROS*/ DW_PR_DUu " " /* LEB128 */
7444 "%3.0f%%"
7445 "\n",
7446 total_entries,
7447 total_bytes_formx,
7448 total_bytes_leb128,
7449 saved_rate);
7450 /* Get .debug_info size (Very unlikely to have
7451 an error here). */
7452 infoerr = dwarf_get_section_info_by_name(dbg,
7453 ".debug_info",&lower,
7454 &size,attr_error);
7455 if (infoerr == DW_DLV_ERROR) {
7456 free(attributes_encoding_table);
7457 attributes_encoding_table = 0;
7458 attributes_encoding_do_init = TRUE;
7459 return infoerr;
7460 }
7461 saved_rate = (float)((total_bytes_formx -
7462 total_bytes_leb128)
7463 * 100 / size);
7464 if (saved_rate > 0) {
7465 printf("\n** .debug_info size can be reduced "
7466 "by %.0f%% **\n",
7467 saved_rate);
7468 }
7469 }
7470 free(attributes_encoding_table);
7471 attributes_encoding_table = 0;
7472 attributes_encoding_do_init = TRUE;
7473 }
7474 return DW_DLV_OK;
7475 }
7476
7477 static void
7478 check_decl_file_only(char **srcfiles,
7479 Dwarf_Unsigned fileindex,Dwarf_Signed srcfiles_cnt,
7480 Dwarf_Half dwversion,int attr)
7481 {
7482 /* Zero is always a legal index for
7483 DWARF2,3,4 and it means no source name provided.
7484 For DWARF 5
7485 we've determined that the standard is wrong
7486 and zero meaning none just does not work.
7487 File numbers must start from zero.
7488 See the dwarfstd.org wiki */
7489
7490 DWARF_CHECK_COUNT(decl_file_result,1);
7491 if (dwversion >= DWVERSION5) {
7492 if (fileindex >= ((Dwarf_Unsigned)srcfiles_cnt)) {
7493 struct esb_s msgb;
7494
7495 esb_constructor(&msgb);
7496 if (!srcfiles) {
7497 esb_append(&msgb,
7498 "There is a file number=");
7499 esb_append_printf_u(&msgb,"%" DW_PR_DUu,fileindex);
7500 esb_append(&msgb," but no source files");
7501 /* Extra space char here to avoid pointless
7502 regression test issues with older versions. */
7503 esb_append(&msgb," are known.");
7504 } else {
7505 esb_append(&msgb,"Does not index to valid DWARF5"
7506 " file name ");
7507 esb_append(&msgb,"filenum=");
7508 esb_append_printf_u(&msgb,"%" DW_PR_DUu,fileindex);
7509 esb_append(&msgb," arraysize=");
7510 esb_append_printf_i(&msgb,"%" DW_PR_DSd,srcfiles_cnt);
7511 esb_append(&msgb,".");
7512 }
7513 DWARF_CHECK_ERROR2(decl_file_result,
7514 get_AT_name(attr, pd_dwarf_names_print_on_error),
7515 esb_get_string(&msgb));
7516 esb_destructor(&msgb);
7517 }
7518 return;
7519 }
7520
7521 /* zero means no file applicable */
7522 if (fileindex &&
7523 fileindex > ((Dwarf_Unsigned)srcfiles_cnt)) {
7524 struct esb_s msgb;
7525
7526 esb_constructor(&msgb);
7527 if (!srcfiles) {
7528 esb_append(&msgb,
7529 "There is a file number=");
7530 esb_append_printf_u(&msgb,"%" DW_PR_DUu,fileindex);
7531 esb_append(&msgb," but no source files");
7532 /* Extra space char here to avoid pointless
7533 regression test issues with older versions. */
7534 esb_append(&msgb," are known.");
7535 } else {
7536 esb_append(&msgb,"Does not index to valid file name ");
7537 esb_append(&msgb,"filenum=");
7538 esb_append_printf_u(&msgb,"%" DW_PR_DUu,fileindex);
7539 esb_append(&msgb," arraysize=");
7540 esb_append_printf_i(&msgb,"%" DW_PR_DSd,srcfiles_cnt);
7541 esb_append(&msgb,".");
7542 }
7543 DWARF_CHECK_ERROR2(decl_file_result,
7544 get_AT_name(attr, pd_dwarf_names_print_on_error),
7545 esb_get_string(&msgb));
7546 esb_destructor(&msgb);
7547 }
7548 }
7549
7550 static int
7551 expand_rnglist_entries(
7552 Dwarf_Rnglists_Head rnglhead,
7553 Dwarf_Unsigned rnglentriescount,
7554 Dwarf_Unsigned rnglglobal_offset,
7555 struct esb_s * esbp,
7556 int local_verbose,
7557 Dwarf_Error *err)
7558 {
7559 Dwarf_Unsigned count = 0;
7560 Dwarf_Unsigned i = 0;
7561 int res = 0;
7562 Dwarf_Unsigned secoffset = rnglglobal_offset;
7563 Dwarf_Bool no_debug_addr_available = FALSE;
7564
7565 count = rnglentriescount;
7566 if (local_verbose > 1) {
7567 esb_append(esbp,"\n "
7568 " "
7569 "secoff");
7570 }
7571 for ( ; i < count; ++i) {
7572 unsigned entrylen = 0;
7573 unsigned rle_code = 0;
7574 Dwarf_Unsigned raw1 = 0;
7575 Dwarf_Unsigned raw2 = 0;
7576 Dwarf_Unsigned cooked1 = 0;
7577 Dwarf_Unsigned cooked2 = 0;
7578
7579 res = dwarf_get_rnglists_entry_fields_a(rnglhead,i,
7580 &entrylen,&rle_code,
7581 &raw1,&raw2,
7582 &no_debug_addr_available,
7583 &cooked1,&cooked2,
7584 err);
7585 if (res != DW_DLV_OK) {
7586 return res;
7587 }
7588 if (local_verbose || no_debug_addr_available ) {
7589 const char *codename = "<unknown code>";
7590
7591 dwarf_get_RLE_name(rle_code,&codename);
7592 esb_append(esbp,"\n ");
7593 esb_append_printf_u(esbp,"[%2u] ",i);
7594 esb_append_printf_s(esbp,"%-20s ",codename);
7595 if (rle_code != DW_RLE_end_of_list) {
7596 esb_append_printf_u(esbp," 0x%"
7597 DW_PR_XZEROS DW_PR_DUx ,raw1);
7598 esb_append_printf_u(esbp," 0x%"
7599 DW_PR_XZEROS DW_PR_DUx ,raw2);
7600 } else {
7601 esb_append(esbp,
7602 " ");
7603 }
7604 if (local_verbose > 1) {
7605 esb_append_printf_u(esbp," 0x%"
7606 DW_PR_XZEROS DW_PR_DUx ,secoffset);
7607 }
7608 if (no_debug_addr_available) {
7609 esb_append(esbp," no .debug_addr available");
7610 }
7611 }
7612 if (!no_debug_addr_available) {
7613 const char *codename = "start,end";
7614
7615 esb_append(esbp,"\n ");
7616 esb_append_printf_u(esbp,"[%2u] ",i);
7617 if (rle_code == DW_RLE_base_addressx ||
7618 rle_code == DW_RLE_base_address) {
7619 codename = "base address";
7620 } else {
7621 if (rle_code == DW_RLE_end_of_list) {
7622 codename = "end of list";
7623 }
7624 }
7625 esb_append_printf_s(esbp,"%-20s ",codename);
7626 if (rle_code != DW_RLE_end_of_list) {
7627 esb_append_printf_u(esbp," 0x%"
7628 DW_PR_XZEROS DW_PR_DUx ,cooked1);
7629 esb_append_printf_u(esbp," 0x%"
7630 DW_PR_XZEROS DW_PR_DUx ,cooked2);
7631 } else {
7632 esb_append(esbp,
7633 " ");
7634 }
7635 if (local_verbose > 1) {
7636 esb_append_printf_u(esbp," 0x%"
7637 DW_PR_XZEROS DW_PR_DUx ,secoffset);
7638 }
7639 }
7640 secoffset += entrylen;
7641 }
7642 esb_append(esbp,"\n");
7643 return DW_DLV_OK;
7644 }
7645
7646 /* DWARF5 .debug_rnglists[.dwo] only. */
7647 static int
7648 handle_rnglists( Dwarf_Attribute attrib,
7649 Dwarf_Half theform,
7650 Dwarf_Unsigned attrval,
7651 Dwarf_Unsigned *output_rle_set_offset,
7652 struct esb_s * esbp,
7653 int local_verbose,
7654 Dwarf_Error *err)
7655 {
7656 /* This is DWARF5, by definition. Not earlier. */
7657 Dwarf_Unsigned global_offset_of_rle_set = 0;
7658 Dwarf_Unsigned count_rnglists_entries = 0;
7659 Dwarf_Rnglists_Head rnglhead = 0;
7660 int res = 0;
7661 res = dwarf_rnglists_get_rle_head(attrib,
7662 theform,
7663 attrval, /* index val or offset depending on form */
7664 &rnglhead,
7665 &count_rnglists_entries,
7666 &global_offset_of_rle_set,
7667 err);
7668 if (res != DW_DLV_OK) {
7669 return res;
7670 }
7671 *output_rle_set_offset = global_offset_of_rle_set;
7672 /* Here we put newline at start of each line
7673 of output, different from the usual practice */
7674 append_local_prefix(esbp);
7675 esb_append_printf_u(esbp,
7676 "Offset of rnglists entries: 0x%"
7677 DW_PR_XZEROS DW_PR_DUx,
7678 global_offset_of_rle_set);
7679 if (local_verbose > 1) {
7680 Dwarf_Unsigned version = 0;
7681 Dwarf_Unsigned context_index = 0;
7682 Dwarf_Unsigned rle_count = 0;
7683 Dwarf_Unsigned total_bytes_in_rle = 0;
7684 Dwarf_Half loffset_size = 0;
7685 Dwarf_Half laddress_size = 0;
7686 Dwarf_Half lsegment_selector_size = 0;
7687 Dwarf_Unsigned section_offset_of_context = 0;
7688 Dwarf_Unsigned length_of_context = 0;
7689 Dwarf_Bool rnglists_base_present = 0;
7690 Dwarf_Unsigned rnglists_base = 0;
7691 Dwarf_Bool rnglists_base_address_present = 0;
7692 Dwarf_Unsigned rnglists_base_address = 0;
7693 Dwarf_Bool debug_addr_base_present = 0;
7694 Dwarf_Unsigned debug_addr_base;
7695 Dwarf_Unsigned offsets_table_offset = 0;
7696 Dwarf_Unsigned offsets_table_entrycount = 0;
7697
7698 res = dwarf_get_rnglist_head_basics(rnglhead,
7699 &rle_count,
7700 &version,
7701 &context_index,
7702 &total_bytes_in_rle,
7703 &loffset_size,
7704 &laddress_size,
7705 &lsegment_selector_size,
7706 &section_offset_of_context,
7707 &length_of_context,
7708 &offsets_table_offset,
7709 &offsets_table_entrycount,
7710 &rnglists_base_present,&rnglists_base,
7711 &rnglists_base_address_present,&rnglists_base_address,
7712 &debug_addr_base_present,&debug_addr_base,
7713 err);
7714 if (res != DW_DLV_OK) {
7715 dwarf_dealloc_rnglists_head(rnglhead);
7716 return res;
7717 }
7718 append_local_prefix(esbp);
7719 esb_append_printf_u(esbp,
7720 "Index of rnglist head : %u",
7721 context_index);
7722
7723 append_local_prefix(esbp);
7724 esb_append_printf_u(esbp,
7725 "rnglist head version : %u",
7726 version);
7727 append_local_prefix(esbp);
7728 esb_append_printf_u(esbp,
7729 "Record count rnglist set : %u",
7730 rle_count);
7731 append_local_prefix(esbp);
7732 esb_append_printf_u(esbp,
7733 "Bytes this rnglist set : %u",
7734 total_bytes_in_rle);
7735 append_local_prefix(esbp);
7736 esb_append_printf_u(esbp,
7737 "offset size : %u",
7738 loffset_size);
7739 append_local_prefix(esbp);
7740 esb_append_printf_u(esbp,
7741 "address size : %u",
7742 laddress_size);
7743 if (offsets_table_entrycount) {
7744 append_local_prefix(esbp);
7745 esb_append_printf_u(esbp,
7746 "offsets count : %u",
7747 offsets_table_entrycount);
7748 append_local_prefix(esbp);
7749 esb_append_printf_u(esbp,
7750 "offsets table offset : 0x%x",
7751 offsets_table_offset);
7752 }
7753
7754 if (rnglists_base_present) {
7755 append_local_prefix(esbp);
7756 esb_append_printf_u(esbp,
7757 "rnglists base offset : 0x%"
7758
7759 DW_PR_XZEROS DW_PR_DUx,
7760 rnglists_base);
7761 }
7762 if (rnglists_base_address_present) {
7763 append_local_prefix(esbp);
7764 esb_append_printf_u(esbp,
7765 "CU DW_AT_low_pc (baseaddr): 0x%"
7766 DW_PR_XZEROS DW_PR_DUx,
7767 rnglists_base_address);
7768 }
7769 if (debug_addr_base_present) {
7770 append_local_prefix(esbp);
7771 esb_append_printf_u(esbp,
7772 "CU DW_AT_addr_base : 0x%"
7773 DW_PR_XZEROS DW_PR_DUx,
7774 debug_addr_base);
7775 }
7776 append_local_prefix(esbp);
7777 esb_append_printf_u(esbp,
7778 "section offset CU rnglists: 0x%"
7779 DW_PR_XZEROS DW_PR_DUx,
7780 section_offset_of_context);
7781 append_local_prefix(esbp);
7782 esb_append_printf_u(esbp,
7783 "section length CU rnglists: 0x%"
7784 DW_PR_XZEROS DW_PR_DUx,
7785 length_of_context);
7786 esb_append_printf_u(esbp, " (%u)",
7787 length_of_context);
7788 }
7789 res = expand_rnglist_entries(rnglhead,
7790 count_rnglists_entries,
7791 global_offset_of_rle_set,
7792 esbp,
7793 local_verbose,err);
7794 dwarf_dealloc_rnglists_head(rnglhead);
7795 return res;
7796 }
7797
7798 /* This detects a special case attr/form compiler
7799 mistake because we have one in the regression tests.
7800 A more general approach would be better. 5 February 2021 */
7801 static void
7802 check_sensible_addr_for_form(Dwarf_Debug dbg,
7803 Dwarf_Attribute attrib, Dwarf_Half theform)
7804 {
7805 Dwarf_Error e = 0;
7806 Dwarf_Half attrnum = 0;
7807 int res = 0;
7808
7809 res = dwarf_whatattr(attrib,&attrnum,&e);
7810 if (res == DW_DLV_OK) {
7811 switch(attrnum) {
7812 default: return;
7813 case DW_AT_stmt_list:
7814 case DW_AT_macros:
7815 case DW_AT_GNU_macros:
7816 {
7817 struct esb_s m;
7818 esb_constructor(&m);
7819 esb_append_printf_s(&m,
7820 "Attribute %s has form ",
7821 get_AT_name(attrnum,FALSE));
7822 esb_append(&m,get_FORM_name(theform,FALSE));
7823 esb_append(&m," which is improper DWARF. "
7824 "Attempting to continue.");
7825 glflags.gf_count_major_errors++;
7826 printf("ERROR / WARNING: %s\n",
7827 esb_get_string(&m));
7828 esb_destructor(&m);
7829 }
7830 }
7831 return;
7832 }
7833 /* Something crazy. Will be noted elsewhere. */
7834 if (res == DW_DLV_ERROR) {
7835 DROP_ERROR_INSTANCE(dbg,res,e);
7836 }
7837 return;
7838 }
7839
7840 /* Just report on error, do not pass any error on,
7841 it will be refound if appropriate. */
7842 static void
7843 check_indexing_function(Dwarf_Debug dbg,
7844 Dwarf_Die die,
7845 Dwarf_Unsigned index,
7846 Dwarf_Addr addr,
7847 struct esb_s *esbp)
7848 {
7849 int cres = 0;
7850 Dwarf_Addr localaddr = 0;
7851 Dwarf_Error error = 0;
7852
7853 if (!glflags.gf_check_attr_encoding) {
7854 return;
7855 }
7856 DWARF_CHECK_COUNT(check_functions_result,1);
7857 cres = dwarf_debug_addr_index_to_addr(die,
7858 index,&localaddr,&error);
7859 if (cres == DW_DLV_OK) {
7860 if (localaddr != addr) {
7861 struct esb_s m;
7862
7863 esb_constructor(&m);
7864 esb_append_printf_u(&m,
7865 "(ERROR: dwarf_debug_addr_index_to_addr "
7866 "Returns bad value 0x%x"
7867 " vs expected ",
7868 localaddr);
7869 esb_append_printf_u(&m,
7870 "value 0x%x",addr);
7871 DWARF_CHECK_ERROR(check_functions_result,
7872 esb_get_string(&m));
7873 esb_append(esbp,esb_get_string(&m));
7874 esb_destructor(&m);
7875 }
7876 return;
7877 }
7878 if (cres == DW_DLV_ERROR) {
7879 DROP_ERROR_INSTANCE(dbg,cres,error);
7880 error = 0;
7881 }
7882 return;
7883 }
7884
7885 static void
7886 check_for_mips_fde(Dwarf_Debug dbg,
7887 Dwarf_Die die,
7888 struct esb_s*esbp)
7889 {
7890 int lres = 0;
7891 Dwarf_Fde fde = 0;
7892 Dwarf_Addr low_pc = 0;
7893 Dwarf_Unsigned func_length = 0;
7894 Dwarf_Small *fde_bytes = 0;
7895 Dwarf_Unsigned fde_byte_length = 0;
7896 Dwarf_Off cie_offset = 0;
7897 Dwarf_Signed cie_index = 0;
7898 Dwarf_Off fde_offset = 0;
7899
7900 /* Not wanting a Dwarf_Error be allocated. */
7901 lres = dwarf_get_fde_for_die(dbg,die,&fde,0);
7902 if (lres != DW_DLV_OK) {
7903 esb_append(esbp,"<WARNING: Unable to find "
7904 "MIPS_fde for Die>");
7905 return;
7906 }
7907 lres = dwarf_get_fde_range(fde,&low_pc,&func_length,
7908 &fde_bytes,&fde_byte_length,
7909 &cie_offset,&cie_index,&fde_offset,0);
7910 dwarf_dealloc(dbg, fde, DW_DLA_FDE);
7911 if (lres != DW_DLV_OK) {
7912 fde = 0;
7913 esb_append(esbp,"<WARNING: Unable to "
7914 "extract fde address/range from its fde>");
7915 return;
7916 }
7917 esb_append_printf_u(esbp,"<FDE with lowpc 0x%"
7918 DW_PR_DUx, low_pc);
7919 if (glflags.verbose) {
7920 esb_append_printf_u(esbp," function length 0x%"
7921 DW_PR_DUu, func_length);
7922 esb_append_printf_u(esbp," fde offset 0x%"
7923 DW_PR_DUx, fde_offset);
7924 esb_append_printf_u(esbp," fde offset 0x%"
7925 DW_PR_DUx, fde_offset);
7926 esb_append_printf_u(esbp," cie index %"
7927 DW_PR_DUu, cie_index);
7928 esb_append_printf_u(esbp," cie offset %"
7929 DW_PR_DUx, cie_offset);
7930 }
7931 esb_append(esbp,">");
7932 }
7933
7934 /* Fill buffer with attribute value.
7935 We pass in tag so we can try to do the right thing with
7936 broken compiler DW_TAG_enumerator
7937
7938 'cnt' is signed for historical reasons (a mistake
7939 in an interface), but the value is never negative.
7940
7941 We append to esbp's buffer.
7942 */
7943 int
7944 get_attr_value(Dwarf_Debug dbg, Dwarf_Half tag,
7945 Dwarf_Die die,
7946 Dwarf_Off dieprint_cu_goffset,
7947 Dwarf_Attribute attrib,
7948 char **srcfiles, Dwarf_Signed srcfiles_cnt,
7949 struct esb_s *esbp,
7950 int show_form,
7951 int local_verbose,
7952 Dwarf_Error *err)
7953 {
7954 Dwarf_Half theform = 0;
7955 char * temps = 0;
7956 Dwarf_Block *tempb = 0;
7957 Dwarf_Signed tempsd = 0;
7958 Dwarf_Unsigned tempud = 0;
7959 Dwarf_Off off = 0;
7960 Dwarf_Die die_for_check = 0;
7961 Dwarf_Half tag_for_check = 0;
7962 Dwarf_Addr addr = 0;
7963 int fres = 0;
7964 int bres = 0;
7965 int wres = 0;
7966 int dres = 0;
7967 Dwarf_Half direct_form = 0;
7968 Dwarf_Bool is_info = 0;
7969
7970 is_info = dwarf_get_die_infotypes_flag(die);
7971 /* Dwarf_whatform gets the real form, DW_FORM_indir is
7972 never returned: instead the real form following
7973 DW_FORM_indir is returned. */
7974 fres = dwarf_whatform(attrib, &theform, err);
7975 /* Depending on the form and the attribute, process the form. */
7976 if (fres == DW_DLV_ERROR) {
7977 print_error_and_continue(
7978 "dwarf_whatform cannot Find Attr Form",
7979 fres, *err);
7980 return fres;
7981 }
7982 if (fres == DW_DLV_NO_ENTRY) {
7983 return fres;
7984 }
7985 /* dwarf_whatform_direct gets the 'direct' form, so if
7986 the form is DW_FORM_indir that is what is returned. */
7987 fres = dwarf_whatform_direct(attrib, &direct_form, err);
7988 DROP_ERROR_INSTANCE(dbg,fres,*err);
7989 /* Ignore errors in dwarf_whatform_direct() */
7990
7991 switch (theform) {
7992 case DW_FORM_GNU_addr_index:
7993 case DW_FORM_addrx:
7994 case DW_FORM_addrx1 : /* DWARF5 */
7995 case DW_FORM_addrx2 : /* DWARF5 */
7996 case DW_FORM_addrx3 : /* DWARF5 */
7997 case DW_FORM_addrx4 : /* DWARF5 */
7998 case DW_FORM_addr:
7999 check_sensible_addr_for_form(dbg,attrib,theform);
8000 bres = dwarf_formaddr(attrib, &addr, err);
8001 if (bres == DW_DLV_OK) {
8002 if (dwarf_addr_form_is_indexed(theform)) {
8003 Dwarf_Unsigned index = 0;
8004 int res = dwarf_get_debug_addr_index
8005 (attrib,&index,err);
8006 if (res != DW_DLV_OK) {
8007 struct esb_s lstr;
8008 esb_constructor(&lstr);
8009 esb_append(&lstr,"ERROR: getting debug addr"
8010 " index on form ");
8011 esb_append(&lstr,get_FORM_name(theform,FALSE));
8012 esb_append(&lstr," missing index?!");
8013 print_error_and_continue(
8014 esb_get_string(&lstr),
8015 res, *err);
8016 esb_destructor(&lstr);
8017 return res;
8018 }
8019 bracket_hex("(addr_index: ",index, ")",esbp);
8020 if (glflags.gf_do_check_dwarf) {
8021 check_indexing_function(dbg,die,
8022 index,addr,esbp);
8023 }
8024 }
8025 bracket_hex("",addr,"",esbp);
8026 } else if (bres == DW_DLV_ERROR) {
8027 if (DW_DLE_MISSING_NEEDED_DEBUG_ADDR_SECTION ==
8028 dwarf_errno(*err)) {
8029 Dwarf_Unsigned index = 0;
8030 int res = 0;
8031
8032 DROP_ERROR_INSTANCE(dbg,bres,*err);
8033 glflags.gf_debug_addr_missing = 1;
8034 res = dwarf_get_debug_addr_index(attrib,&index,
8035 err);
8036 if (res != DW_DLV_OK) {
8037 struct esb_s lstr;
8038 esb_constructor(&lstr);
8039 esb_append(&lstr,get_FORM_name(theform,FALSE));
8040 esb_append(&lstr," missing index. ?!");
8041 print_error_and_continue(
8042 esb_get_string(&lstr),
8043 res, *err);
8044 esb_destructor(&lstr);
8045 return res;
8046 }
8047 addr = 0;
8048 bracket_hex("(addr_index: ",index,
8049 ")<no .debug_addr section>",esbp);
8050 /* This is normal in a .dwo/DWP file. The .debug_addr
8051 is in a .o and in the final executable. */
8052 } else {
8053 /* Some bad error. */
8054 struct esb_s lstr;
8055
8056 esb_constructor(&lstr);
8057 esb_append(&lstr,get_FORM_name(theform,FALSE));
8058 esb_append(&lstr," form with no addr ?!");
8059 print_error_and_continue(
8060 esb_get_string(&lstr),
8061 bres, *err);
8062 esb_destructor(&lstr);
8063 if (!glflags.gf_error_code_search_by_address) {
8064 glflags.gf_error_code_search_by_address =
8065 (int)dwarf_errno(*err);
8066 }
8067 return bres;
8068 }
8069 } else { /* DW_DLV_NO_ENTRY */
8070 struct esb_s lstr;
8071
8072 esb_constructor(&lstr);
8073 esb_append(&lstr,get_FORM_name(theform,FALSE));
8074 esb_append(&lstr," is a DW_DLV_NO_ENTRY? "
8075 "something is wrong.");
8076 print_error_and_continue(
8077 esb_get_string(&lstr),
8078 bres, *err);
8079 esb_destructor(&lstr);
8080 return bres;
8081 }
8082 break;
8083 case DW_FORM_ref_addr:
8084 {
8085 Dwarf_Half attr = 0;
8086 /* DW_FORM_ref_addr is not accessed thru formref: ** it is an
8087 address (global section offset) in ** the .debug_info
8088 section.
8089 DWARF2 incorrectly specifed the value here
8090 as being the size of an address, which never made any
8091 sense: it has always been an offset of a DIE somewhere in
8092 .debug_info .
8093 The definition was changed in DWARF3 to specify the value
8094 is an offset in .debug_info, not an address. */
8095 bres = dwarf_global_formref_b(attrib, &off,&is_info,err);
8096 if (bres == DW_DLV_OK) {
8097 bracket_hex("<GOFF=",off, ">",esbp);
8098 } else {
8099 print_error_and_continue(
8100 "DW_FORM_ref_addr form with no reference?!"
8101 " Something is corrupted.",
8102 bres, *err);
8103 return bres;
8104 }
8105 wres = dwarf_whatattr(attrib, &attr, err);
8106 if (wres != DW_DLV_OK) {
8107 print_error_and_continue(
8108 "DW_FORM_ref_addr no attribute number?!"
8109 " Something is corrupted.",
8110 wres, *err);
8111 return wres;
8112 }
8113 if (attr == DW_AT_sibling) {
8114 /* The value had better be inside the current CU
8115 else there is a nasty error here, as a sibling
8116 has to be in the same CU, it seems. */
8117 /* The target offset (off) had better be
8118 following the die's global offset else
8119 we have a serious botch. this FORM
8120 defines the value as a .debug_info
8121 global offset. */
8122 Dwarf_Off cuoff = 0;
8123 Dwarf_Off culen = 0;
8124 Dwarf_Off die_overall_offset = 0;
8125 int res = 0;
8126 int ores = dwarf_dieoffset(die, &die_overall_offset,
8127 err);
8128 if (ores != DW_DLV_OK) {
8129 print_error_and_continue(
8130 "dwarf_dieoffsetnot available for"
8131 "DW_AT_sibling. Something is corrupted.",
8132 ores, *err);
8133 return ores;
8134 }
8135 if (die_stack_indent_level >= DIE_STACK_SIZE ) {
8136 report_die_stack_error(dbg,err);
8137 return DW_DLV_ERROR;
8138 }
8139 SET_DIE_STACK_SIBLING(off);
8140 if (die_overall_offset >= off) {
8141 struct esb_s msg;
8142
8143 esb_constructor(&msg);
8144 esb_append_printf_u(&msg,
8145 "ERROR: Sibling DW_FORM_ref_offset 0x%"
8146 DW_PR_XZEROS DW_PR_DUx ,
8147 off);
8148 esb_append_printf_s(&msg,
8149 " points %s die Global offset ",
8150 (die_overall_offset == off)?"at":"before");
8151 esb_append_printf_u(&msg,
8152 "0x%" DW_PR_XZEROS DW_PR_DUx,
8153 die_overall_offset);
8154 simple_err_only_return_action(DW_DLV_ERROR,
8155 esb_get_string(&msg));
8156 esb_destructor(&msg);
8157 }
8158
8159 DWARF_CHECK_COUNT(tag_tree_result,1);
8160 res = dwarf_die_CU_offset_range(die,&cuoff,
8161 &culen,err);
8162 if (res != DW_DLV_OK) {
8163 DWARF_CHECK_ERROR(tag_tree_result,
8164 "DW_AT_sibling DW_FORM_ref_addr "
8165 "offset range of the CU "
8166 "is not available");
8167 DROP_ERROR_INSTANCE(dbg,res,*err);
8168 } else {
8169 Dwarf_Off cuend = cuoff+culen;
8170 if (off < cuoff || off >= cuend) {
8171 DWARF_CHECK_ERROR(tag_tree_result,
8172 "DW_AT_sibling DW_FORM_ref_addr "
8173 "offset points "
8174 "outside of current CU");
8175 }
8176 }
8177 } else if (attr == DW_AT_abstract_origin) {
8178 int lres = 0;
8179 Dwarf_Die targ_die_a = 0;
8180 int is_info_a = dwarf_get_die_infotypes_flag(die);
8181
8182 DWARF_CHECK_COUNT(type_offset_result,1);
8183 lres = dwarf_offdie_b(dbg,off,is_info_a,
8184 &targ_die_a,err);
8185 if (lres == DW_DLV_OK) {
8186 dwarf_dealloc_die(targ_die_a);
8187 } else {
8188 DWARF_CHECK_ERROR(type_offset_result,
8189 "DW_AT_abstract_origin DW_FORM_ref_addr "
8190 "offset of the DIE "
8191 "is not valid");
8192 if (lres == DW_DLV_ERROR) {
8193 DROP_ERROR_INSTANCE(dbg,lres,*err);
8194 }
8195 }
8196 }
8197 }
8198 break;
8199 case DW_FORM_ref1:
8200 case DW_FORM_ref2:
8201 case DW_FORM_ref4:
8202 case DW_FORM_ref8:
8203 case DW_FORM_ref_udata:
8204 {
8205 int refres = 0;
8206 Dwarf_Half attr = 0;
8207 Dwarf_Off goff = 0; /* Global offset */
8208
8209 /* CU-relative offset returned. */
8210 refres = dwarf_formref(attrib, &off,
8211 &is_info, err);
8212 if (refres != DW_DLV_OK) {
8213 struct esb_s msg;
8214
8215 esb_constructor(&msg);
8216 /* Report incorrect offset */
8217 esb_append_printf_s(&msg,
8218 "reference form on attr %s has "
8219 "no valid cu_relative offset?! ",
8220 get_AT_name(attr,FALSE));
8221 esb_append_printf_u(&msg,
8222 ", for offset=<0x%" DW_PR_XZEROS DW_PR_DUx ">",
8223 off);
8224 print_error_and_continue(
8225 esb_get_string(&msg),refres,*err);
8226 esb_destructor(&msg);
8227 return refres;
8228 }
8229
8230 refres = dwarf_whatattr(attrib, &attr, err);
8231 if (refres != DW_DLV_OK) {
8232 struct esb_s lstr;
8233 esb_constructor(&lstr);
8234 esb_append(&lstr,get_AT_name(attr,FALSE));
8235 esb_append(&lstr,
8236 " is an attribute with no number? Impossible");
8237 print_error_and_continue(
8238 esb_get_string(&lstr),refres,*err);
8239 esb_destructor(&lstr);
8240 return refres;
8241 }
8242
8243 /* Convert the local offset 'off' into a global section
8244 offset 'goff'. */
8245 refres = dwarf_convert_to_global_offset(attrib,
8246 off, &goff, err);
8247 if (refres != DW_DLV_OK) {
8248 /* Report incorrect offset */
8249 struct esb_s msg;
8250
8251 esb_constructor(&msg);
8252 esb_append_printf_u(&msg,
8253 "invalid offset"
8254 ",off=<0x%" DW_PR_XZEROS DW_PR_DUx "> ",
8255 off);
8256 esb_append(&msg,"attr: ");
8257 esb_append(&msg,get_AT_name(attr,FALSE));
8258 esb_append(&msg,"local offset has no global offset! ");
8259 print_error_and_continue(
8260 esb_get_string(&msg), refres, *err);
8261 esb_destructor(&msg);
8262 return refres;
8263 }
8264 if (attr == DW_AT_sibling) {
8265 /* The value had better be inside the current CU
8266 else there is a nasty error here, as a sibling
8267 has to be in the same CU, it seems.
8268 The target offset (off) had better be
8269 following the die's global offset else
8270 we have a serious botch. this FORM
8271 defines the value as a .debug_info
8272 global offset. */
8273 Dwarf_Off die_overall_offset = 0;
8274 int ores = dwarf_dieoffset(die, &die_overall_offset,err);
8275 if (ores != DW_DLV_OK) {
8276 struct esb_s lstr;
8277 esb_constructor(&lstr);
8278 esb_append(&lstr,"DW_AT_sibling attr and DIE has");
8279 esb_append(&lstr,"no die_offset?");
8280 print_error_and_continue(
8281 esb_get_string(&lstr),
8282 ores, *err);
8283 esb_destructor(&lstr);
8284 return ores;
8285 }
8286 SET_DIE_STACK_SIBLING(goff);
8287 if (die_overall_offset >= goff) {
8288 struct esb_s lstr;
8289 esb_constructor(&lstr);
8290 esb_append(&lstr,"ERROR in DW_AT_sibling: ");
8291 esb_append(&lstr,get_FORM_name(theform,FALSE));
8292 esb_append_printf_u(&lstr,
8293 " Sibling offset 0x%" DW_PR_XZEROS DW_PR_DUx
8294 " points ",
8295 goff);
8296 esb_append(&lstr,
8297 (die_overall_offset == goff)?"at":"before");
8298 esb_append_printf_u(&lstr,
8299 " its own die GOFF="
8300 "0x%" DW_PR_XZEROS DW_PR_DUx,
8301 die_overall_offset);
8302 simple_err_only_return_action(DW_DLV_ERROR,
8303 esb_get_string(&lstr));
8304 esb_destructor(&lstr);
8305 /* At present no way to create a Dwarf_Error
8306 inside dwarfdump. */
8307 }
8308 }
8309
8310 /* Do references inside <> to distinguish them ** from
8311 constants. In dense form this results in <<>>. Ugly for
8312 dense form, but better than ambiguous. davea 9/94 */
8313 if (glflags.gf_show_global_offsets) {
8314 bracket_hex("<",off,"",esbp);
8315 bracket_hex(" GOFF=",goff,">",esbp);
8316 } else {
8317 bracket_hex("<",off,">",esbp);
8318 }
8319
8320 if (glflags.gf_check_type_offset) {
8321 if (attr == DW_AT_type &&
8322 dd_form_refers_local_info(theform)) {
8323 dres = dwarf_offdie_b(dbg, goff,
8324 is_info,
8325 &die_for_check, err);
8326 if (dres != DW_DLV_OK) {
8327 struct esb_s msgc;
8328
8329 esb_constructor(&msgc);
8330 esb_append_printf_u(&msgc,
8331 "DW_AT_type offset does not point to a DIE "
8332 "for global offset 0x%"
8333 DW_PR_XZEROS DW_PR_DUx, goff);
8334 esb_append_printf_u(&msgc,
8335 " cu off 0x%" DW_PR_XZEROS DW_PR_DUx,
8336 dieprint_cu_goffset);
8337 esb_append_printf_u(&msgc,
8338 " local offset 0x%" DW_PR_XZEROS DW_PR_DUx,
8339 off);
8340 esb_append_printf_u(&msgc,
8341 " tag 0x%x",tag);
8342
8343 DWARF_CHECK_ERROR(type_offset_result,
8344 esb_get_string(&msgc));
8345 DROP_ERROR_INSTANCE(dbg,dres,*err);
8346 esb_destructor(&msgc);
8347 } else {
8348 int tres2 =
8349 dwarf_tag(die_for_check, &tag_for_check, err);
8350 if (tres2 == DW_DLV_OK) {
8351 switch (tag_for_check) {
8352 case DW_TAG_array_type:
8353 case DW_TAG_class_type:
8354 case DW_TAG_enumeration_type:
8355 case DW_TAG_pointer_type:
8356 case DW_TAG_reference_type:
8357 case DW_TAG_rvalue_reference_type:
8358 case DW_TAG_restrict_type:
8359 case DW_TAG_string_type:
8360 case DW_TAG_structure_type:
8361 case DW_TAG_subroutine_type:
8362 case DW_TAG_typedef:
8363 case DW_TAG_union_type:
8364 case DW_TAG_ptr_to_member_type:
8365 case DW_TAG_set_type:
8366 case DW_TAG_subrange_type:
8367 case DW_TAG_base_type:
8368 case DW_TAG_const_type:
8369 case DW_TAG_file_type:
8370 case DW_TAG_packed_type:
8371 case DW_TAG_thrown_type:
8372 case DW_TAG_volatile_type:
8373 case DW_TAG_template_type_parameter:
8374 case DW_TAG_template_value_parameter:
8375 case DW_TAG_unspecified_type:
8376 /* Template alias */
8377 case DW_TAG_template_alias:
8378 /* OK */
8379 break;
8380 default:
8381 {
8382 struct esb_s msga;
8383
8384 esb_constructor(&msga);
8385 esb_append_printf_u(&msga,
8386 "DW_AT_type offset "
8387 "0x%" DW_PR_XZEROS DW_PR_DUx,
8388 goff);
8389 esb_append_printf_u(&msga,
8390 " does not point to Type"
8391 " info we got tag 0x%x ",
8392 tag_for_check);
8393 esb_append(&msga,
8394 get_TAG_name(tag_for_check,
8395 pd_dwarf_names_print_on_error));
8396 DWARF_CHECK_ERROR(type_offset_result,
8397 esb_get_string(&msga));
8398 esb_destructor(&msga);
8399 }
8400 break;
8401 }
8402 } else {
8403 DWARF_CHECK_ERROR(type_offset_result,
8404 "DW_AT_type offset does not exist");
8405 DROP_ERROR_INSTANCE(dbg,tres2,*err);
8406 }
8407 }
8408 }
8409 dwarf_dealloc_die(die_for_check);
8410 die_for_check = 0;
8411 }
8412 }
8413 break;
8414 case DW_FORM_block:
8415 case DW_FORM_block1:
8416 case DW_FORM_block2:
8417 case DW_FORM_block4:
8418 case DW_FORM_exprloc:
8419 fres = dwarf_formblock(attrib, &tempb, err);
8420 if (fres == DW_DLV_OK) {
8421 unsigned u = 0;
8422 esb_append_printf_u(esbp, "len 0x%04x: ",
8423 tempb->bl_len);
8424 if (tempb->bl_len) {
8425 esb_append(esbp,"0x");
8426 }
8427 for (u = 0; u < tempb->bl_len; u++) {
8428 esb_append_printf_u(esbp,
8429 "%02x",
8430 *(u + (unsigned char *) tempb->bl_data));
8431 }
8432 if (tempb->bl_len) {
8433 esb_append(esbp,": ");
8434 }
8435 dwarf_dealloc(dbg, tempb, DW_DLA_BLOCK);
8436 tempb = 0;
8437 } else {
8438 struct esb_s lstr;
8439 esb_constructor(&lstr);
8440 esb_append(&lstr,"Form form ");
8441 esb_append(&lstr,get_FORM_name(theform,FALSE));
8442 esb_append(&lstr," cannot get block");
8443 print_error_and_continue(
8444 esb_get_string(&lstr),
8445 fres, *err);
8446 esb_destructor(&lstr);
8447 return fres;
8448 }
8449 break;
8450 case DW_FORM_data1:
8451 case DW_FORM_data2:
8452 case DW_FORM_data4:
8453 case DW_FORM_data8:
8454 case DW_FORM_data16:
8455 {
8456 Dwarf_Half attr = 0;
8457 fres = dwarf_whatattr(attrib, &attr, err);
8458 if (fres != DW_DLV_OK) {
8459 struct esb_s lstr;
8460 esb_constructor(&lstr);
8461 esb_append(&lstr,"Form ");
8462 esb_append(&lstr,get_FORM_name(theform,FALSE));
8463 esb_append(&lstr," cannot get attribute");
8464 print_error_and_continue(
8465 esb_get_string(&lstr),
8466 fres, *err);
8467 esb_destructor(&lstr);
8468 return fres;
8469 } else {
8470 switch (attr) {
8471 case DW_AT_ordering:
8472 case DW_AT_byte_size:
8473 case DW_AT_bit_offset:
8474 case DW_AT_data_bit_offset:
8475 case DW_AT_bit_size:
8476 case DW_AT_inline:
8477 case DW_AT_language:
8478 case DW_AT_visibility:
8479 case DW_AT_virtuality:
8480 case DW_AT_accessibility:
8481 case DW_AT_address_class:
8482 case DW_AT_calling_convention:
8483 case DW_AT_discr_list: /* DWARF2 */
8484 case DW_AT_encoding:
8485 case DW_AT_identifier_case:
8486 case DW_AT_MIPS_loop_unroll_factor:
8487 case DW_AT_MIPS_software_pipeline_depth:
8488 case DW_AT_decl_column:
8489 case DW_AT_decl_file:
8490 case DW_AT_decl_line:
8491 case DW_AT_call_column:
8492 case DW_AT_call_file:
8493 case DW_AT_call_line:
8494 case DW_AT_start_scope:
8495 case DW_AT_byte_stride:
8496 case DW_AT_bit_stride:
8497 case DW_AT_count:
8498 case DW_AT_stmt_list:
8499 case DW_AT_MIPS_fde:
8500 { int show_form_here = 0;
8501 wres = dd_get_integer_and_name(dbg,
8502 attrib,
8503 &tempud,
8504 /* attrname */ (const char *) NULL,
8505 /* err_string */ ( struct esb_s *) NULL,
8506 (encoding_type_func) 0,
8507 err,show_form_here);
8508
8509 if (wres == DW_DLV_OK) {
8510 Dwarf_Bool hex_format = TRUE;
8511 Dwarf_Half dwversion = 0;
8512
8513 formx_unsigned(tempud,esbp,hex_format);
8514 /* Check attribute encoding */
8515 if (glflags.gf_check_attr_encoding) {
8516 check_attributes_encoding(attr,theform,
8517 tempud);
8518 }
8519 if (attr == DW_AT_decl_file ||
8520 attr == DW_AT_call_file) {
8521 turn_file_num_to_string(dbg,die,
8522 attrib,
8523 theform,
8524 dwversion,tempud,
8525 srcfiles, srcfiles_cnt,esbp,err);
8526 /* Validate integrity of file indices
8527 referenced in .debug_line */
8528 if (glflags.gf_check_decl_file) {
8529 check_decl_file_only(srcfiles,
8530 tempud,srcfiles_cnt,
8531 dwversion,attr);
8532 }
8533 } /* end decl_file and call_file processing */
8534 } else { /* not DW_DLV_OK on get small encoding */
8535 struct esb_s lstr;
8536 esb_constructor(&lstr);
8537 esb_append(&lstr,"For form ");
8538 esb_append(&lstr,get_FORM_name(theform,FALSE));
8539 esb_append(&lstr," and attribute ");
8540 esb_append(&lstr,get_AT_name(attr,FALSE));
8541 esb_append(&lstr,
8542 " Cannot get encoding attribute");
8543 print_error_and_continue(
8544 esb_get_string(&lstr),
8545 wres, *err);
8546 esb_destructor(&lstr);
8547 return wres;
8548 }
8549 if (attr == DW_AT_MIPS_fde) {
8550 check_for_mips_fde(dbg,die,esbp);
8551 }
8552 }
8553 break;
8554 case DW_AT_const_value: {
8555 /* Do not use hexadecimal format */
8556 Dwarf_Bool chex = FALSE;
8557 wres = formxdata_print_value(dbg,die,attrib,
8558 theform,esbp, err, /* hex format?*/chex);
8559 if (wres == DW_DLV_OK){
8560 /* String appended already. */
8561 } else if (wres == DW_DLV_NO_ENTRY) {
8562 /* nothing? */
8563 } else {
8564 struct esb_s lstr;
8565 esb_constructor(&lstr);
8566 esb_append(&lstr,"For form ");
8567 esb_append(&lstr,get_FORM_name(theform,FALSE));
8568 esb_append(&lstr," and attribute ");
8569 esb_append(&lstr,get_AT_name(attr,FALSE));
8570 esb_append(&lstr," Cannot get const value ");
8571 print_error_and_continue(
8572 esb_get_string(&lstr),
8573 wres, *err);
8574 esb_destructor(&lstr);
8575 return wres;
8576 }
8577 }
8578 break;
8579 case DW_AT_GNU_dwo_id:
8580 case DW_AT_GNU_odr_signature:
8581 case DW_AT_dwo_id:
8582 {
8583 Dwarf_Sig8 v;
8584
8585 v = zerosig;
8586 wres = dwarf_formsig8_const(attrib,&v,err);
8587 if (wres == DW_DLV_OK){
8588 struct esb_s t;
8589
8590 esb_constructor(&t);
8591 format_sig8_string(&v,&t);
8592 esb_append(esbp,esb_get_string(&t));
8593 esb_destructor(&t);
8594 } else if (wres == DW_DLV_NO_ENTRY) {
8595 /* nothing? */
8596 esb_append(esbp,"Impossible: no entry for ");
8597 esb_append(esbp,get_FORM_name(theform,FALSE));
8598 esb_append(esbp," dwo_id");
8599 } else {
8600 struct esb_s lstr;
8601 esb_constructor(&lstr);
8602 esb_append(&lstr,"For form ");
8603 esb_append(&lstr,get_FORM_name(theform,FALSE));
8604 esb_append(&lstr," and attribute ");
8605 esb_append(&lstr,get_AT_name(attr,FALSE));
8606 esb_append(&lstr,
8607 " Cannot get Dwarf_Sig8 value ");
8608 print_error_and_continue(
8609 esb_get_string(&lstr),
8610 wres, *err);
8611 esb_destructor(&lstr);
8612 return wres;
8613 }
8614 }
8615 break;
8616 case DW_AT_upper_bound:
8617 case DW_AT_lower_bound:
8618 default: {
8619 Dwarf_Bool chex = FALSE;
8620 Dwarf_Die tdie = die;
8621 if (DW_AT_ranges == attr ||
8622 DW_AT_location == attr ||
8623 DW_AT_vtable_elem_location == attr ||
8624 DW_AT_string_length == attr ||
8625 DW_AT_return_addr == attr ||
8626 DW_AT_use_location == attr ||
8627 DW_AT_static_link == attr ||
8628 DW_AT_frame_base == attr) {
8629 /* Do not look for data
8630 type for unsigned/signed.
8631 and do use HEX. */
8632 chex = TRUE;
8633 tdie = NULL;
8634 }
8635 /* Do not use hexadecimal format except for
8636 DW_AT_ranges. */
8637 wres = formxdata_print_value(dbg,
8638 tdie,attrib,
8639 theform,esbp, err, chex);
8640 if (wres == DW_DLV_OK) {
8641 /* String appended already. */
8642 } else if (wres == DW_DLV_NO_ENTRY) {
8643 /* nothing? */
8644 } else {
8645 struct esb_s lstr;
8646 esb_constructor(&lstr);
8647 esb_append(&lstr,"For form ");
8648 esb_append(&lstr,get_FORM_name(theform,FALSE));
8649 esb_append(&lstr," and attribute ");
8650 esb_append(&lstr,get_AT_name(attr,FALSE));
8651 esb_append(&lstr,
8652 " Cannot get Dwarf_Sig8 value ");
8653 print_error_and_continue(
8654 esb_get_string(&lstr),
8655 wres, *err);
8656 esb_destructor(&lstr);
8657 return wres;
8658 }
8659 }
8660 break;
8661 }
8662 }
8663 if (glflags.gf_cu_name_flag) {
8664 if (attr == DW_AT_MIPS_fde) {
8665 if (glflags.fde_offset_for_cu_low ==
8666 DW_DLV_BADOFFSET) {
8667 glflags.fde_offset_for_cu_low
8668 = glflags.fde_offset_for_cu_high = tempud;
8669 } else if (tempud < glflags.fde_offset_for_cu_low) {
8670 glflags.fde_offset_for_cu_low = tempud;
8671 } else if (tempud > glflags.fde_offset_for_cu_high) {
8672 glflags.fde_offset_for_cu_high = tempud;
8673 }
8674 }
8675 }
8676 }
8677 break;
8678 case DW_FORM_sdata:
8679 wres = dwarf_formsdata(attrib, &tempsd, err);
8680 if (wres == DW_DLV_OK) {
8681 Dwarf_Bool hxform=TRUE;
8682 tempud = tempsd;
8683 formx_unsigned_and_signed_if_neg(tempud,tempsd,
8684 " (",hxform,esbp);
8685 } else if (wres == DW_DLV_NO_ENTRY) {
8686 /* nothing? */
8687 } else {
8688 print_error_and_continue(
8689 "Cannot get DW_FORM_sdata value..",
8690 wres, *err);
8691 return wres;
8692 }
8693 break;
8694 case DW_FORM_udata:
8695 wres = dwarf_formudata(attrib, &tempud, err);
8696 if (wres == DW_DLV_OK) {
8697 Dwarf_Bool hex_format = TRUE;
8698 formx_unsigned(tempud,esbp,hex_format);
8699 } else if (wres == DW_DLV_NO_ENTRY) {
8700 /* nothing? */
8701 } else {
8702 print_error_and_continue(
8703 "Cannot get DW_FORM_udata value..",
8704 wres, *err);
8705 return wres;
8706 }
8707 break;
8708 /* various forms for strings. */
8709 case DW_FORM_string:
8710 case DW_FORM_strp:
8711 case DW_FORM_strx: /* DWARF5 */
8712 case DW_FORM_strx1: /* DWARF5 */
8713 case DW_FORM_strx2: /* DWARF5 */
8714 case DW_FORM_strx3: /* DWARF5 */
8715 case DW_FORM_strx4: /* DWARF5 */
8716 case DW_FORM_strp_sup: /* DWARF5 String in altrnt: tied file */
8717 case DW_FORM_GNU_strp_alt: /* String in altrnt: tied file */
8718 case DW_FORM_line_strp: /* DWARF5, offset to .debug_line_str */
8719 /* unsigned offset in size of an offset */
8720 case DW_FORM_GNU_str_index: {
8721 int sres = dwarf_formstring(attrib, &temps, err);
8722 if (sres == DW_DLV_OK) {
8723 if (theform == DW_FORM_strx ||
8724 theform == DW_FORM_strx1 ||
8725 theform == DW_FORM_strx2 ||
8726 theform == DW_FORM_strx3 ||
8727 theform == DW_FORM_strx4 ||
8728 theform == DW_FORM_GNU_str_index) {
8729 char saverbuf[ESB_FIXED_ALLOC_SIZE];
8730 struct esb_s saver;
8731 Dwarf_Unsigned index = 0;
8732
8733 esb_constructor_fixed(&saver,saverbuf,
8734 sizeof(saverbuf));
8735 sres = dwarf_get_debug_str_index(attrib,&index,err);
8736 esb_append(&saver,temps);
8737 if (sres == DW_DLV_OK) {
8738 bracket_hex("(indexed string: ",index,")",esbp);
8739 } else {
8740 DROP_ERROR_INSTANCE(dbg,sres,*err);
8741 esb_append(esbp,
8742 "(ERROR: indexed string:no "
8743 "string provided?)");
8744 }
8745 esb_append(esbp, esb_get_string(&saver));
8746 esb_destructor(&saver);
8747 } else {
8748 esb_append(esbp,temps);
8749 }
8750 } else if (sres == DW_DLV_NO_ENTRY) {
8751 if (theform == DW_FORM_strx ||
8752 theform == DW_FORM_GNU_str_index ||
8753 theform == DW_FORM_strx1 ||
8754 theform == DW_FORM_strx2 ||
8755 theform == DW_FORM_strx3 ||
8756 theform == DW_FORM_strx4 ) {
8757 esb_append(esbp,
8758 "(indexed string,no string provided?)");
8759 } else {
8760 esb_append(esbp, "<no string provided?>");
8761 }
8762 } else { /* DW_DLV_ERROR */
8763 if (theform == DW_FORM_strx ||
8764 theform == DW_FORM_GNU_str_index ||
8765 theform == DW_FORM_strx1 ||
8766 theform == DW_FORM_strx2 ||
8767 theform == DW_FORM_strx3 ||
8768 theform == DW_FORM_strx4 ) {
8769 struct esb_s lstr;
8770
8771 esb_constructor(&lstr);
8772 esb_append(&lstr,"Cannot get an indexed string on ");
8773 esb_append(&lstr,get_FORM_name(theform,FALSE));
8774 esb_append(&lstr,"....");
8775 print_error_and_continue(
8776 esb_get_string(&lstr),
8777 sres, *err);
8778 esb_destructor(&lstr);
8779 return sres;
8780 }
8781 {
8782 struct esb_s lstr;
8783
8784 esb_constructor(&lstr);
8785 esb_append(&lstr,"Cannot get the form on ");
8786 esb_append(&lstr,get_FORM_name(theform,FALSE));
8787 esb_append(&lstr,"....");
8788 print_error_and_continue(
8789 esb_get_string(&lstr),
8790 sres, *err);
8791 esb_destructor(&lstr);
8792 return sres;
8793 }
8794 }
8795 }
8796 break;
8797 case DW_FORM_flag: {
8798 Dwarf_Bool tempbool = 0;
8799 wres = dwarf_formflag(attrib, &tempbool, err);
8800 if (wres == DW_DLV_OK) {
8801 if (tempbool) {
8802 esb_append_printf_i(esbp,
8803 "yes(%d)", tempbool);
8804 } else {
8805 esb_append(esbp,"no");
8806 }
8807 } else if (wres == DW_DLV_NO_ENTRY) {
8808 /* nothing? */
8809 } else {
8810 print_error_and_continue(
8811 "Cannot get formflag/p....", wres, *err);
8812 return wres;
8813 }
8814 }
8815 break;
8816 case DW_FORM_indirect:
8817 /* We should not ever get here, since the true form was
8818 determined and direct_form has the DW_FORM_indirect
8819 if it is used here in this attr. */
8820 esb_append(esbp, get_FORM_name(theform,
8821 pd_dwarf_names_print_on_error));
8822 break;
8823 case DW_FORM_sec_offset: { /* DWARF4, DWARF5 */
8824 char* emptyattrname = 0;
8825 int show_form_here = 0;
8826 wres = dd_get_integer_and_name(dbg,
8827 attrib,
8828 &tempud,
8829 emptyattrname,
8830 /* err_string */ NULL,
8831 (encoding_type_func) 0,
8832 err,show_form_here);
8833 if (wres == DW_DLV_NO_ENTRY) {
8834 /* Show nothing? */
8835 } else if (wres == DW_DLV_ERROR) {
8836 print_error_and_continue(
8837 "ERROR: cannot et DW_FORM_sec_offset value content.",
8838 wres,*err);
8839 return wres;
8840 } else {
8841 bracket_hex("",tempud,"",esbp);
8842 }
8843 }
8844
8845 break;
8846 case DW_FORM_flag_present: /* DWARF4 */
8847 esb_append(esbp,"yes(1)");
8848 break;
8849 case DW_FORM_ref_sig8: { /* DWARF4 */
8850 Dwarf_Sig8 sig8data;
8851
8852 sig8data = zerosig;
8853 wres = dwarf_formsig8(attrib,&sig8data,err);
8854 if (wres != DW_DLV_OK) {
8855 /* Show nothing? */
8856 print_error_and_continue(
8857 "ERROR: cannot et DW_FORM_ref_sig8 value content.",
8858 wres,*err);
8859 return wres;
8860 } else {
8861 struct esb_s sig8str;
8862
8863 esb_constructor(&sig8str);
8864 format_sig8_string(&sig8data,&sig8str);
8865 esb_append(esbp,esb_get_string(&sig8str));
8866 esb_destructor(&sig8str);
8867 if (!show_form) {
8868 esb_append(esbp," <type signature>");
8869 }
8870 }
8871 }
8872 break;
8873 /* DWARF5, attr val is signed uleb */
8874 case DW_FORM_implicit_const: {
8875 wres = dwarf_formsdata(attrib, &tempsd, err);
8876 if (wres == DW_DLV_OK) {
8877 Dwarf_Bool hxform=TRUE;
8878 tempud = tempsd;
8879 formx_unsigned_and_signed_if_neg(tempud,tempsd,
8880 " (",hxform,esbp);
8881 } else if (wres == DW_DLV_NO_ENTRY) {
8882 /* nothing? */
8883 } else {
8884 print_error_and_continue(
8885 "ERROR: cannot get signed value of "
8886 "DW_FORM_implicit_const",
8887 wres,*err);
8888 return wres;
8889 }
8890 }
8891 break;
8892 case DW_FORM_loclistx: /* DWARF5, index into .debug_loclists */
8893 wres = dwarf_formudata(attrib, &tempud, err);
8894 if (wres == DW_DLV_OK) {
8895 /* Fall through to end to show the form details. */
8896 Dwarf_Bool hex_format = TRUE;
8897 esb_append(esbp,"<index to debug_loclists ");
8898 formx_unsigned(tempud,esbp,hex_format);
8899 esb_append(esbp,">");
8900 break;
8901 } else if (wres == DW_DLV_NO_ENTRY) {
8902 /* nothing? */
8903 } else {
8904 struct esb_s lstr;
8905 esb_constructor(&lstr);
8906 esb_append(&lstr,"Cannot get formudata on ");
8907 esb_append(&lstr,"DW_FORM_loclistx");
8908 esb_append(&lstr,"....");
8909 print_error_and_continue(
8910 esb_get_string(&lstr),
8911 wres, *err);
8912 esb_destructor(&lstr);
8913 return wres;
8914 }
8915 break;
8916 case DW_FORM_rnglistx: {
8917 /* DWARF5, index into .debug_rnglists
8918 Can appear only on DW_AT_ranges attribute.*/
8919 wres = dwarf_formudata(attrib, &tempud, err);
8920 if (wres == DW_DLV_OK) {
8921 /* Fall through to end to show the form details. */
8922 Dwarf_Bool hex_format = TRUE;
8923 esb_append(esbp,"<index to debug_rnglists ");
8924 formx_unsigned(tempud,esbp,hex_format);
8925 esb_append(esbp,">");
8926 } else if (wres == DW_DLV_NO_ENTRY) {
8927 /* nothing? */
8928 } else {
8929 struct esb_s lstr;
8930 esb_constructor(&lstr);
8931 esb_append(&lstr,"Cannot get formudata on ");
8932 esb_append(&lstr,"DW_FORM_rnglistx");
8933 esb_append(&lstr,"....");
8934 print_error_and_continue(
8935 esb_get_string(&lstr),
8936 wres, *err);
8937 esb_destructor(&lstr);
8938 return wres;
8939 }
8940 }
8941 break;
8942 case DW_FORM_ref_sup4: /* DWARF5 */
8943 case DW_FORM_ref_sup8: /* DWARF5 */
8944 case DW_FORM_GNU_ref_alt: {
8945 /* These refer to a supplemental or alternate object file,
8946 and we don't have those available to check. */
8947 bres = dwarf_global_formref_b(attrib, &off,&is_info, err);
8948 if (bres == DW_DLV_OK) {
8949 bracket_hex("",off,"",esbp);
8950 } else {
8951 struct esb_s lstr;
8952 esb_constructor(&lstr);
8953 esb_append(&lstr,get_FORM_name(theform,FALSE));
8954 esb_append(&lstr," form with no reference?!");
8955 print_error_and_continue(
8956 esb_get_string(&lstr),
8957 bres,*err);
8958 esb_destructor(&lstr);
8959 return wres;
8960 }
8961 }
8962 break;
8963 default: {
8964 struct esb_s lstr;
8965
8966 esb_constructor(&lstr);
8967 esb_append_printf_u(&lstr,
8968 "ERROR: dwarf_whatform unexpected value, "
8969 "form code 0x%04x",
8970 theform);
8971 simple_err_only_return_action(DW_DLV_ERROR,
8972 esb_get_string(&lstr));
8973 esb_destructor(&lstr);
8974 }
8975 break;
8976 } /* end switch on theform */
8977 show_form_itself(show_form,local_verbose,theform,
8978 direct_form,esbp);
8979 return DW_DLV_OK;
8980 }
8981
8982 void
8983 format_sig8_string(Dwarf_Sig8*data, struct esb_s *out)
8984 {
8985 unsigned i = 0;
8986 esb_append(out,"0x");
8987 for (; i < sizeof(data->signature); ++i) {
8988 esb_append_printf_u(out, "%02x",
8989 (unsigned char)(data->signature[i]));
8990 }
8991 }
8992
8993 static int
8994 get_form_values(Dwarf_Attribute attrib,
8995 Dwarf_Half * theform, Dwarf_Half * directform,
8996 Dwarf_Error *err)
8997 {
8998 int res = 0;
8999
9000 res = dwarf_whatform(attrib, theform, err);
9001 if (res != DW_DLV_OK) {
9002 return res;
9003 }
9004 res = dwarf_whatform_direct(attrib, directform, err);
9005 return res;
9006 }
9007
9008 static void
9009 show_form_itself(int local_show_form,
9010 int local_verbose,
9011 int theform,
9012 int directform, struct esb_s *esbp)
9013 {
9014 if (local_show_form
9015 && directform && directform == DW_FORM_indirect) {
9016 char *form_indir = " (used DW_FORM_indirect";
9017 char *form_indir2 = ") ";
9018 esb_append(esbp, form_indir);
9019 if (local_verbose) {
9020 esb_append_printf_i(esbp," %d",DW_FORM_indirect);
9021 }
9022 esb_append(esbp, form_indir2);
9023 }
9024 if (local_show_form) {
9025 esb_append(esbp," <form ");
9026 esb_append(esbp,get_FORM_name(theform,
9027 pd_dwarf_names_print_on_error));
9028 if (local_verbose) {
9029 esb_append_printf_i(esbp," %d",theform);
9030 }
9031 esb_append(esbp,">");
9032 }
9033 }