Debian Codegraph @

src/bin/dwarfdump/print_debug_names.c

raw
1 /*
2 Copyright 2017-2018 David Anderson. All rights reserved.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of version 2 of the GNU General
6 Public License as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it would be
9 useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE.
12
13 Further, this software is distributed without any warranty
14 that it is free of the rightful claim of any third person
15 regarding infringement or the like. Any license provided
16 herein, whether implied or otherwise, applies only to this
17 software file. Patent licenses, if any, provided herein
18 do not apply to combinations of this program with other
19 software, or any other product whatsoever.
20
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write the Free
23 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 Boston MA 02110-1301, USA.
25
26 */
27
28 #include <config.h>
29 #include <stdio.h> /* FILE decl for dd_esb.h, printf etc */
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include "dwarf.h"
34 #include "libdwarf.h"
35 #include "libdwarf_private.h"
36 #include "dd_defined_types.h"
37 #include "dd_checkutil.h"
38 #include "dd_glflags.h"
39 #include "dd_globals.h"
40 #include "dd_naming.h"
41 #include "dd_sanitized.h"
42 #include "dd_esb.h"
43 #include "dd_esb_using_functions.h"
44
45 #define ATTR_ARRAY_SIZE 10
46
47 struct Dnames_Abb_Check_s {
48 Dwarf_Unsigned ac_code;
49 Dwarf_Unsigned ac_codeoffset;
50 Dwarf_Unsigned ac_codelength;
51 Dwarf_Unsigned ac_pairscount;
52 Dwarf_Unsigned ac_nametablerefs;
53 Dwarf_Half ac_tag;
54 };
55 static Dwarf_Unsigned abblist_nexttouse = 0;
56 static Dwarf_Unsigned abblist_count = 0;
57 static Dwarf_Unsigned abblist_ab_table_len = 0;
58 static struct Dnames_Abb_Check_s *abblist = 0;
59
60 static void
61 reset_abblist(void)
62 {
63 if (abblist) {
64 free(abblist);
65 abblist = 0;
66 }
67 abblist_count = 0;
68 abblist_ab_table_len = 0;
69 abblist_nexttouse = 0;
70 }
71
72 #if 0
73 static void
74 dump_table(const char *msg,unsigned long count,
75 Dwarf_Half *idx, Dwarf_Half *form)
76 {
77 unsigned long i = 0;
78 for ( ; i < count; ++i) {
79 printf("[%lu] %x %x\n",i,idx[i],form[i]);
80 }
81 }
82 #endif
83
84 static void
85 printindent(unsigned int l)
86 {
87 unsigned int i = 0;
88 for ( ; i < l; ++i) {
89 printf(" ");
90 }
91 }
92
93 static const Dwarf_Sig8 zerosig;
94 /* The table entries here are indexed starting at 0
95 Whereas names table area indexed starting at 1. */
96 static int
97 print_cu_table(unsigned int indent,Dwarf_Dnames_Head dn,
98 const char *type,
99 Dwarf_Unsigned offset_count,
100 Dwarf_Unsigned sig_count,
101 Dwarf_Bool *has_single_cu_offset,
102 Dwarf_Unsigned *single_cu_offset,
103 Dwarf_Error *error)
104 {
105 Dwarf_Unsigned i = 0;
106 Dwarf_Unsigned totalcount = offset_count+sig_count;
107 int res = 0;
108 Dwarf_Bool formtu = FALSE;
109
110 if (type[0] == 't' && type[1] == 'u' &&
111 type[2] == 0) {
112 formtu = TRUE;
113 } else if (type[0] == 'c' && type[1] == 'u' &&
114 type[2] == 0) {
115 formtu = FALSE;
116 } else {
117 printf("\nERROR: Calling print_cu_table with type"
118 "%s is invalid. Must be tu or cu ."
119 "Not printing this cu table set\n",
120 type);
121 glflags.gf_count_major_errors++;
122 return DW_DLV_NO_ENTRY;
123 }
124 if (formtu) {
125 printf("\n");
126 printindent(indent);
127 printf("%s List. Entry count: %" DW_PR_DUu
128 " (local tu count %" DW_PR_DUu
129 ",foreign tu count %" DW_PR_DUu
130 ")\n",type,totalcount,offset_count,sig_count);
131 } else {
132 printindent(indent);
133 printf("%s List. Entry count: %" DW_PR_DUu "\n",
134 type,totalcount);
135 }
136 for (i = 0 ; i < totalcount; ++i) {
137 Dwarf_Unsigned offset = 0;
138 Dwarf_Sig8 signature;
139
140 signature = zerosig;
141 res = dwarf_dnames_cu_table(dn,type,i,
142 &offset, &signature,error);
143 if (res == DW_DLV_NO_ENTRY) {
144 break;
145 }
146 if (res == DW_DLV_ERROR) {
147 return res;
148 }
149 if (i == 0 && !formtu) {
150 *single_cu_offset = offset;
151 *has_single_cu_offset = TRUE;
152 }
153 /* Could equally test for non-zero offset here. */
154 if (i < offset_count) {
155
156 printindent(indent);
157 printf("[%4" DW_PR_DUu "] ",i);
158 printf("CU-offset: 0x%"
159 DW_PR_XZEROS DW_PR_DUx "\n",
160 offset);
161 continue;
162 }
163
164 {
165 static char sigarea[32];
166 struct esb_s sig8str;
167
168 esb_constructor_fixed(&sig8str,sigarea,sizeof(sigarea));
169 format_sig8_string(&signature,&sig8str);
170 printindent(indent);
171 printf("[%4" DW_PR_DUu "] " ,i);
172 printf("Signature: %s\n",esb_get_string(&sig8str));
173 esb_destructor(&sig8str);
174 }
175 }
176 return DW_DLV_OK;
177 }
178
179 static int
180 print_buckets(unsigned int indent,Dwarf_Dnames_Head dn,
181 Dwarf_Unsigned bucket_count,
182 Dwarf_Error *error)
183 {
184 int res = 0;
185 Dwarf_Unsigned bn = 0;
186
187 if (!bucket_count) {
188 return DW_DLV_NO_ENTRY;
189 }
190
191 printf("\n");
192 printindent(indent);
193 printf("Bucket (hash) table entry count: "
194 "%" DW_PR_DUu "\n",bucket_count);
195 printindent(indent);
196 printf("[ ] nameindex collisioncount\n");
197 for ( ; bn < bucket_count; ++bn) {
198 Dwarf_Unsigned name_index = 0;
199 Dwarf_Unsigned collision_count = 0;
200
201 res = dwarf_dnames_bucket(dn,bn,&name_index,
202 &collision_count,error);
203 if (res == DW_DLV_ERROR) {
204 return res;
205 }
206 if (res == DW_DLV_NO_ENTRY) {
207 break;
208 }
209 printindent(indent);
210 printf("[ %3" DW_PR_DUu "] ",bn);
211 printf("%6" DW_PR_DUu " %6" DW_PR_DUu ,
212 name_index,
213 collision_count);
214 printf("\n");
215 }
216 printf("\n");
217 return DW_DLV_OK;
218 }
219
220 static void
221 P_Entry(const char * leader,Dwarf_Unsigned value)
222 {
223 printf(" %13s 0x%" DW_PR_XZEROS DW_PR_DUx
224 " (%8" DW_PR_DUu
225 ")\n",
226 leader,value,value);
227 }
228 static int
229 print_dnames_offsets(unsigned int indent,Dwarf_Dnames_Head dn,
230 Dwarf_Error *error)
231 {
232 Dwarf_Unsigned header_offset = 0;
233 Dwarf_Unsigned cu_table_offset = 0;
234 Dwarf_Unsigned tu_local_offset = 0;
235 Dwarf_Unsigned foreign_tu_offset = 0;
236 Dwarf_Unsigned buckets_offset = 0;
237 Dwarf_Unsigned hashes_offset = 0;
238 Dwarf_Unsigned stringoffsets_offset = 0;
239 Dwarf_Unsigned entryoffsets_offset = 0;
240 Dwarf_Unsigned abbrev_table_offset = 0;
241 Dwarf_Unsigned entry_pool_offset = 0;
242 int res = 0;
243
244 printf("\n");
245 printindent(indent);
246 printf("Table Offsets \n");
247 res = dwarf_dnames_offsets(dn,&header_offset,
248 &cu_table_offset, &tu_local_offset, &foreign_tu_offset,
249 &buckets_offset, &hashes_offset,
250 &stringoffsets_offset,&entryoffsets_offset,
251 &abbrev_table_offset,&entry_pool_offset,error);
252 if (res == DW_DLV_ERROR) {
253 return res;
254 }
255 P_Entry("Header :",header_offset);
256 P_Entry("CU table :",cu_table_offset);
257 P_Entry("TU local :",tu_local_offset);
258 P_Entry("Foreign TU :",foreign_tu_offset);
259 P_Entry("Buckets :",buckets_offset);
260 P_Entry("Hashes :",hashes_offset);
261 P_Entry("String Offsets:",stringoffsets_offset);
262 P_Entry("Entry Offsets :",entryoffsets_offset);
263 P_Entry("Abbrev Table :",abbrev_table_offset);
264 P_Entry("Entry Pool: :",entry_pool_offset);
265 return DW_DLV_OK;
266 }
267
268 static void
269 update_abblist_count(Dwarf_Unsigned ab_code,Dwarf_Half tag)
270 {
271 Dwarf_Unsigned i = 0;
272 Dwarf_Unsigned curcount = abblist_nexttouse;
273 struct Dnames_Abb_Check_s *cur = abblist;
274
275 if (!abblist) {
276 /* Not counting. */
277 return;
278 }
279 for ( ; i < curcount; ++i,++cur) {
280 if (cur->ac_code == ab_code) {
281 if (cur->ac_tag != tag) {
282 printf("ERROR: Abbrev code %" DW_PR_DUu
283 "has tag 0x%x but internal array shows"
284 "tag 0x%x\n",ab_code,
285 tag,cur->ac_tag);
286 glflags.gf_count_major_errors++;
287 }
288 break;
289 }
290 }
291 if (i >= abblist_nexttouse) {
292 printf("ERROR: Abbrev code %" DW_PR_DUu
293 " does not appear in the abbrev table. Corrupt data\n",
294 ab_code);
295 glflags.gf_count_major_errors++;
296 return;
297 }
298 cur->ac_nametablerefs += 1 ;
299 }
300
301 static void
302 insert_ab_in_abblist(
303 Dwarf_Unsigned abbrev_code,
304 Dwarf_Half abbrev_tag,
305 Dwarf_Unsigned abbrev_offset,
306 Dwarf_Unsigned actual_attr_count)
307 {
308 Dwarf_Unsigned i = 0;
309 Dwarf_Unsigned curcount = abblist_nexttouse;
310 struct Dnames_Abb_Check_s *cur = abblist;
311
312 for ( ; i < curcount; ++i,++cur) {
313 if (cur->ac_code == abbrev_code) {
314 break;
315 }
316 }
317 if (i >= curcount) {
318 if (i >= abblist_count) {
319 static int printed = FALSE;
320
321 if (!printed) {
322 printf("ERROR: Impossible, out of room for "
323 "abbrev list entry checking, count"
324 " is %" DW_PR_DUu "\n",abblist_count);
325 glflags.gf_count_major_errors++;
326 printed = TRUE;
327 }
328 return;
329 }
330 cur->ac_code = abbrev_code;
331 cur->ac_tag = abbrev_tag;
332 cur->ac_codeoffset = abbrev_offset;
333 cur->ac_codelength = 0; /* unknown yet */
334 cur->ac_pairscount = actual_attr_count;
335 cur->ac_nametablerefs = 0;
336 abblist_nexttouse++;
337 } else {
338 printf("ERROR: Impossible duplicate abbrev code "
339 "at abbrev entry %" DW_PR_DUu "\n",i);
340 glflags.gf_count_major_errors++;
341 }
342 }
343
344 static void
345 print_abblist_use(unsigned int indent)
346 {
347 Dwarf_Unsigned i = 0;
348 Dwarf_Unsigned table_count = abblist_count;
349 Dwarf_Unsigned table_byte_size = abblist_ab_table_len;
350 struct Dnames_Abb_Check_s *cur = 0;
351 Dwarf_Off last_offset = 0;
352
353 if (!abblist_count || !abblist) {
354 return;
355 }
356
357 cur = abblist;
358 for (i=0; i < table_count ; ++i,++cur) {
359 Dwarf_Unsigned prevlen = 0;
360 if (!i) {
361 if (cur->ac_codeoffset) {
362 printf("ERROR: Seemingly initial abbrev"
363 "is at non-zero offset 0x%" DW_PR_DUx "\n",
364 cur->ac_codeoffset);
365 glflags.gf_count_major_errors++;
366 }
367 last_offset = cur->ac_codeoffset;
368 continue;
369 }
370 if ( cur->ac_codeoffset <= last_offset) {
371 printf("ERROR: abbrev code offsets out of order"
372 " 0x%" DW_PR_DUx
373 " followed by 0x%" DW_PR_DUx "\n",
374 last_offset,
375 cur->ac_codeoffset);
376 glflags.gf_count_major_errors++;
377 }
378 prevlen = cur->ac_codeoffset - last_offset;
379 (cur-1)->ac_codelength = prevlen;
380 last_offset = cur->ac_codeoffset;
381 }
382 /* Pull back to last */
383 --cur;
384 /* Adjust codelength for table_byte_size, by 1 */
385 cur->ac_codelength =
386 table_byte_size - cur->ac_codeoffset -1;
387 cur = abblist;
388 printindent(indent);
389 printf("Abbreviation List: %" DW_PR_DUu " entries.\n",
390 table_count);
391 printf(" U is Number of name entries using this abbrev\n");
392 printf(" P is Number of IDX/FORM pairs on this abbrev\n");
393 printf(" not counting end of list 0\n");
394 printindent(indent);
395
396 printf("[ ] code tag "
397 " aboffs bytes U P\n");
398 for (i=0; i < table_count ; ++i,++cur) {
399 const char *tagname= "<none>";
400 printindent(indent);
401 printf("[%3" DW_PR_DUu "] ",i);
402 printf(" %4" DW_PR_DUu ,cur->ac_code);
403 printf(" (0x%04" DW_PR_DUx ")" ,cur->ac_code);
404 dwarf_get_TAG_name((unsigned int)cur->ac_tag,&tagname);
405 printf(" %-25s",tagname);
406
407 printf(" 0x%04" DW_PR_DUx ,cur->ac_codeoffset);
408 printf(" %2" DW_PR_DUu ,cur->ac_codelength);
409 printf(" %2" DW_PR_DUu ,cur->ac_nametablerefs);
410 printf(" %2" DW_PR_DUu "\n",cur->ac_pairscount-1);
411 }
412 }
413
414 /* The abbrev_table_length here is in bytes, not entries.
415 The name_table_count is in entries, indexed as
416 1 through name_table_count. */
417 static int
418 print_dnames_abbrevtable(unsigned int indent,Dwarf_Dnames_Head dn,
419 Dwarf_Unsigned abbrev_table_length /* bytes */)
420 {
421 int res = DW_DLV_OK;
422 Dwarf_Unsigned abbrev_offset = 0;
423 Dwarf_Unsigned abbrev_code = 0;
424 Dwarf_Unsigned abbrev_tag = 0;
425 Dwarf_Unsigned array_size = ATTR_ARRAY_SIZE;
426 static Dwarf_Half idxattr_array[ATTR_ARRAY_SIZE];
427 static Dwarf_Half form_array[ATTR_ARRAY_SIZE];
428 Dwarf_Unsigned actual_attr_count = 0;
429 Dwarf_Unsigned i = 0;
430
431 for ( ;res == DW_DLV_OK; ++i) {
432 res = dwarf_dnames_abbrevtable(dn,i,
433 &abbrev_offset,
434 &abbrev_code, &abbrev_tag,
435 array_size,
436 idxattr_array,form_array,
437 &actual_attr_count);
438 if (res != DW_DLV_OK) {
439 break;
440 }
441 }
442 res = DW_DLV_OK;
443 abblist_count = i;
444 if (abblist_count) {
445 abblist = calloc(abblist_count,
446 sizeof(struct Dnames_Abb_Check_s));
447 if (!abblist) {
448 printf("ERROR: Unable to allocate %" DW_PR_DUu
449 "entries of a struct to check "
450 "for wasted abbrev space\n",
451 abblist_count);
452 glflags.gf_count_major_errors++;
453 }
454 }
455 printf("\n");
456 printindent(indent);
457 printf("Debug Names abbreviation table entries per Name: length %"
458 DW_PR_DUu " (0x%04" DW_PR_DUx ") bytes.\n",
459 abbrev_table_length,
460 abbrev_table_length);
461 printindent(indent);
462 printf("[NameIndex] abbrev_offset abbrev_code"
463 " count idxattr\n");
464 res = DW_DLV_OK;
465 for (i = 0 ; res == DW_DLV_OK; ++i) {
466 Dwarf_Unsigned limit = 0;
467 Dwarf_Unsigned k = 0;
468 const char *tagname = "<TAGunknown>";
469
470 /* Never returns DW_DLV_ERROR */
471 res = dwarf_dnames_abbrevtable(dn,i,
472 &abbrev_offset,
473 &abbrev_code, &abbrev_tag,
474 array_size,
475 idxattr_array,form_array,
476 &actual_attr_count);
477 if (res == DW_DLV_NO_ENTRY) {
478 break;
479 }
480 if (abblist) {
481 insert_ab_in_abblist(abbrev_code,(Dwarf_Half)abbrev_tag,
482 abbrev_offset,actual_attr_count);
483 }
484 dwarf_get_TAG_name((unsigned int)abbrev_tag,&tagname);
485 printindent(indent+2);
486 printf("[%4" DW_PR_DUu "] ",i);
487 printf(" 0x%" DW_PR_XZEROS DW_PR_DUx " ",abbrev_offset);
488 printf(" 0x%05" DW_PR_DUx ,abbrev_code);
489 printf(" %3" DW_PR_DUu " ",actual_attr_count);
490 printf(" 0x%04" DW_PR_DUx " %s",abbrev_tag,tagname);
491 printf("\n");
492 limit = actual_attr_count;
493 if (limit > ATTR_ARRAY_SIZE) {
494 printf(" \nERROR: allowed %" DW_PR_DUu " pairs,"
495 " But we have %" DW_PR_DUu "pairs!\n",
496 array_size, actual_attr_count);
497 glflags.gf_count_major_errors++;
498 }
499 printindent(indent+4);
500 printf("[abbrindex] idxattr form \n");
501 for (k = 0; k < limit; ++k) {
502 const char *idname = "<unknownidx>";
503 const char *formname = "<unknownform>";
504 Dwarf_Half a = idxattr_array[k];
505 Dwarf_Half f = form_array[k];
506
507 printindent(indent+4);
508 printf("[%3" DW_PR_DUu "] ",k);
509 printf("0x%04x ",a);
510 printf("0x%04x ",f);
511 if (a || f) {
512 dwarf_get_IDX_name(a,&idname);
513 printf("%-19s",idname);
514 dwarf_get_FORM_name(f,&formname);
515 printf("%15s",formname);
516 if (!(a && f)){
517 printf("\nERROR: improper idx/form pair!\n");
518 glflags.gf_count_major_errors++;
519 }
520 } else {
521 printf(" (end of list)");
522 }
523 printf("\n");
524 }
525 }
526 return DW_DLV_OK;
527 }
528
529 static int
530 print_attr_array(unsigned int indent,
531 Dwarf_Unsigned attr_count,
532 Dwarf_Unsigned array_size,
533 Dwarf_Half * idxattr_array,
534 Dwarf_Half * form_array)
535 {
536 Dwarf_Unsigned k = 0;
537 Dwarf_Unsigned count = attr_count;
538 if (!attr_count) {
539 printindent(indent);
540 printf("No idxattr/form content available\n");
541 return DW_DLV_NO_ENTRY;
542 }
543 if (array_size < attr_count) {
544 printindent(indent);
545 printf("Array size %" DW_PR_DUu
546 " but count is %" DW_PR_DUu
547 " so some entries not available\n",
548 array_size,attr_count);
549 count = array_size;
550 }
551 printindent(indent);
552 printf("[] idxnum formnum idxname formname\n");
553 for ( ; k < count; ++k) {
554 const char *idname = 0;
555 const char *formname = 0;
556 Dwarf_Half a = idxattr_array[k];
557 Dwarf_Half f = form_array[k];
558
559 printindent(indent);
560 printf("[%3" DW_PR_DUu "]", k);
561 printf(" 0x%04u 0x%04u", a,f);
562 if (k == (count-1)) {
563 if (a || f) {
564 printf("\nERROR: last entry should be 0,0"
565 "not 0x%x a 0x%xf \n",a,f);
566 glflags.gf_count_major_errors++;
567 break;
568 } else {
569 printf(" (end of list)\n");
570 continue;
571 }
572 }
573 dwarf_get_IDX_name(a,&idname);
574 printf(" %-19s",idname);
575 dwarf_get_FORM_name(f,&formname);
576 printf("%15s",formname);
577 printf("\n");
578 }
579 return DW_DLV_OK;
580 }
581
582 /* Check die offset for correctness.
583 Only dealing with local cu/tu (not foreighn tu)*/
584 static void
585 check_die_pub_offset( Dwarf_Debug dbg,
586 Dwarf_Unsigned cu_hd_offset,
587 Dwarf_Unsigned local_die_offset)
588 {
589 Dwarf_Error error = 0;
590 int sres = 0;
591 Dwarf_Bool is_info = TRUE;
592 Dwarf_Die itemdie = 0;
593 Dwarf_Unsigned global_offset = local_die_offset+
594 cu_hd_offset;
595 Dwarf_Unsigned cuhdr_offset = 0;
596
597 sres = dwarf_offdie_b(dbg,global_offset,
598 is_info,&itemdie,&error);
599 if (sres != DW_DLV_OK) {
600 printf("ERROR No global DIE at cuhdroff="
601 "0x%" DW_PR_DUx
602 " + culocaldieoffset="
603 "0x%" DW_PR_DUx
604 " = "
605 "0x%" DW_PR_DUx "\n\n",
606 cuhdr_offset,local_die_offset,
607 global_offset);
608 if (sres == DW_DLV_ERROR) {
609 dwarf_dealloc_error(dbg,error);
610 }
611 glflags.gf_count_major_errors++;
612 error = 0;
613 } else {
614 dwarf_dealloc_die(itemdie);
615 }
616 }
617
618 /* Uses local error, issues error when appropriate.
619 Has no other side effect.
620 Checking DIE offset . Messier than it should be. */
621 static void
622 _dwarf_check_dn_die_offset(Dwarf_Debug dbg,Dwarf_Dnames_Head dn,
623 Dwarf_Bool single_cu_case,
624 Dwarf_Unsigned local_type_unit_count,
625 Dwarf_Unsigned single_cu_offset,
626 Dwarf_Unsigned local_die_offset,
627 Dwarf_Bool has_cu_table_index,
628 Dwarf_Bool has_tu_table_index,
629 Dwarf_Unsigned cu_table_index,
630 Dwarf_Unsigned tu_table_index)
631 {
632 int cres = 0;
633 Dwarf_Error chkerror = 0;
634 Dwarf_Unsigned cu_hd_offset = 0;
635 Dwarf_Sig8 sig8;
636
637 if (!single_cu_case) {
638 /* tu or multiple CUs */
639 if (has_cu_table_index) {
640 cres = dwarf_dnames_cu_table(dn,"cu",
641 cu_table_index,&cu_hd_offset,&sig8,&chkerror);
642 if (cres != DW_DLV_OK) {
643 printf("\nERROR: Cannot get "
644 "dwarf_dnames_cu_table on cu \n");
645 glflags.gf_count_major_errors++;
646 if (cres == DW_DLV_ERROR) {
647 dwarf_dealloc_error(dbg,chkerror);
648 }
649 return;
650 }
651 } else {
652 if (has_tu_table_index) {
653 if (tu_table_index < local_type_unit_count) {
654 cres = dwarf_dnames_cu_table(dn,"tu",
655 tu_table_index,&cu_hd_offset,
656 &sig8,&chkerror);
657 if (cres != DW_DLV_OK) {
658 printf("\nERROR: Cannot get "
659 "dwarf_dnames_cu_table on tu \n");
660 glflags.gf_count_major_errors++;
661 if (cres == DW_DLV_ERROR) {
662 dwarf_dealloc_error(dbg,chkerror);
663 }
664 return;
665 }
666 } else {
667 printf("\nERROR: debug_names tu_table_index >= "
668 "local_type_unit_count \n");
669 glflags.gf_count_major_errors++;
670 return;
671 }
672 }
673 }
674 } else {
675 /* Single unit case means CU */
676 cu_hd_offset = single_cu_offset;
677 }
678 check_die_pub_offset(dbg,
679 cu_hd_offset, local_die_offset);
680 return;
681 }
682
683 #define MAXPAIRS 8 /* The standard defines 5.*/
684 static Dwarf_Half idx_array[MAXPAIRS];
685 static Dwarf_Half form_array[MAXPAIRS];
686 static Dwarf_Unsigned offsets_array[MAXPAIRS];
687 static Dwarf_Sig8 signatures_array[MAXPAIRS];
688
689 static int
690 print_name_values(unsigned int indent,
691 Dwarf_Debug dbg,
692 Dwarf_Dnames_Head dn,
693 Dwarf_Unsigned name_index,
694 Dwarf_Unsigned offset_in_entrypool,
695 Dwarf_Unsigned local_type_unit_count,
696 Dwarf_Bool single_cu_case,
697 Dwarf_Unsigned single_cu_offset,
698 Dwarf_Error * error)
699 {
700 int res = 0;
701 Dwarf_Unsigned abbrev_code = 0;
702 Dwarf_Half tag = 0;
703 Dwarf_Unsigned value_count = 0;
704 Dwarf_Unsigned index_of_abbrev = 0;
705 Dwarf_Unsigned offset_of_initial_value = 0;
706 Dwarf_Unsigned offset_next_entry_pool = 0;
707 const char *idname = "<DW_IDX-unknown>";
708 Dwarf_Unsigned i = 0;
709 const char *tagname = "<TAGunknown";
710 Dwarf_Unsigned cu_table_index = 0;
711 Dwarf_Unsigned tu_table_index = 0;
712 Dwarf_Unsigned local_die_offset = 0;
713 Dwarf_Bool has_cu_table_index = FALSE;
714 Dwarf_Bool has_tu_table_index = FALSE;
715
716 res = dwarf_dnames_entrypool(dn,
717 offset_in_entrypool,
718 &abbrev_code,&tag,&value_count,&index_of_abbrev,
719 &offset_of_initial_value,
720 error);
721 if (res != DW_DLV_OK) {
722 return res;
723 }
724 printindent(indent);
725 dwarf_get_TAG_name((unsigned int)tag,&tagname);
726 printf(
727 "Nameindex=%" DW_PR_DUu
728 " abbrevcode=%" DW_PR_DUu
729 " (0x%04" DW_PR_DUx ")"
730 " abbrevindex=%" DW_PR_DUu
731 "\n",
732 name_index,
733 abbrev_code,
734 abbrev_code,
735 index_of_abbrev);
736 printindent(indent);
737 printf(
738 "Tag 0x%04x %-16s\n",
739 tag,tagname);
740 printindent(indent);
741 printf(
742 "Valuecount %5" DW_PR_DUu
743 " valuesoffset 0x%04" DW_PR_DUx
744 "\n",
745 value_count, offset_of_initial_value);
746 if (value_count > MAXPAIRS) {
747 printf("\nERROR: The number of values in an entrypool"
748 " entry is %" DW_PR_DUu
749 " but the max allowed is %d\n",value_count,MAXPAIRS);
750 glflags.gf_count_major_errors++;
751 return DW_DLV_OK;
752 }
753 update_abblist_count(abbrev_code,tag);
754 res = dwarf_dnames_entrypool_values(dn,
755 index_of_abbrev,
756 offset_of_initial_value,
757 value_count,
758 idx_array,
759 form_array,
760 offsets_array,
761 signatures_array,
762 &single_cu_case,&single_cu_offset,
763 &offset_next_entry_pool,
764 error);
765 if (res != DW_DLV_OK) {
766 return res;
767 }
768
769 indent += 2;
770 printindent(indent);
771 printf("Entrypool Values. Entry count:%" DW_PR_DUu ".\n",
772 value_count);
773 if (single_cu_case) {
774 printindent(indent);
775 printf("Single CU case. CUoffset defaults to: 0x%"
776 DW_PR_XZEROS DW_PR_DUx "\n",
777 single_cu_offset);
778 }
779 printindent(indent);
780 printf("[ ] idxattr idxname value\n");
781 for (i = 0; i < value_count; ++i) {
782 Dwarf_Half idx = idx_array[i];
783
784 printindent(indent);
785 printf("[%2" DW_PR_DUu "] ",i);
786
787 if (!idx) {
788 if (i == (value_count-1)) {
789 printf(" 0 (end of list)\n");
790 continue;
791 }
792 }
793 dwarf_get_IDX_name(idx,&idname);
794 printf(" %2u %-19s ",idx,idname);
795 switch(idx) {
796 case DW_IDX_compile_unit:
797 /* compile units special-case a single CU, and
798 this instance eliminates that special case here. */
799 printf(" CUindex= %" DW_PR_DUu ,offsets_array[i]);
800 cu_table_index = offsets_array[i];
801 has_cu_table_index = TRUE;
802 single_cu_case = FALSE;
803 break;
804 case DW_IDX_type_unit: {
805 /* type units do not special-case a single CU */
806 printf(" typeunitindex= %" DW_PR_DUu
807 " (0x%" DW_PR_XZEROS DW_PR_DUx ")",
808 offsets_array[i],
809 offsets_array[i]);
810 has_tu_table_index = TRUE;
811 tu_table_index = offsets_array[i];
812 }
813 break;
814 case DW_IDX_die_offset: {
815 printf(" DIEcuLocalOff=%" DW_PR_DUu
816 " (0x%" DW_PR_XZEROS DW_PR_DUx ")",
817 offsets_array[i],
818 offsets_array[i]);
819 local_die_offset = offsets_array[i];
820 }
821 break;
822 case DW_IDX_parent: {
823 Dwarf_Half f = form_array[i];
824 if (f == DW_FORM_flag || f == DW_FORM_flag_present) {
825 if (offsets_array[i] ) {
826 printf(" FORM-flag-present "
827 "(Parent generated but not indexed)");
828 } else {
829 printf(" FORM_flag (false)");
830 }
831 } else {
832 printf(" pooloffsetofparent=%" DW_PR_DUu
833 " (0x%" DW_PR_XZEROS DW_PR_DUx ")",
834 offsets_array[i],
835 offsets_array[i]);
836 }
837 }
838 break;
839 case DW_IDX_type_hash: {
840 struct esb_s m;
841
842 esb_constructor(&m);
843 format_sig8_string((Dwarf_Sig8*)&signatures_array[i],
844 &m);
845 printf(" typehash= %s",esb_get_string(&m));
846 esb_destructor(&m);
847 break;
848 }
849 default: {
850 printf("\nERROR: idxattr %u is unknown!\n",idx);
851 glflags.gf_count_major_errors++;
852 }
853 }
854 printf("\n");
855 }
856 _dwarf_check_dn_die_offset(dbg,dn,
857 single_cu_case,
858 local_type_unit_count,
859 single_cu_offset,
860 local_die_offset,
861 has_cu_table_index,
862 has_tu_table_index,
863 cu_table_index,
864 tu_table_index);
865 return DW_DLV_OK;
866 }
867
868 static int
869 print_names_table(unsigned int indent,
870 Dwarf_Debug dbg,
871 Dwarf_Dnames_Head dn,
872 Dwarf_Unsigned name_count,
873 Dwarf_Unsigned bucket_count,
874 Dwarf_Unsigned local_type_unit_count,
875 Dwarf_Bool has_single_cu_offset,
876 Dwarf_Unsigned single_cu_offset,
877 Dwarf_Error * error)
878 {
879 Dwarf_Unsigned i = 1;
880 int res = 0;
881 Dwarf_Unsigned bucketnum = 0;
882 Dwarf_Unsigned hashval = 0;
883 Dwarf_Unsigned offset_to_debug_str = 0;
884 char * namestr = 0;
885 Dwarf_Unsigned offset_in_entrypool = 0;
886 Dwarf_Unsigned abbrev_code = 0;
887 Dwarf_Half abbrev_tag = 0;
888 Dwarf_Unsigned array_size = ATTR_ARRAY_SIZE;
889 static Dwarf_Half nt_idxattr_array[ATTR_ARRAY_SIZE];
890 static Dwarf_Half nt_form_array[ATTR_ARRAY_SIZE];
891 Dwarf_Unsigned attr_count = 0;
892
893 memset(nt_idxattr_array,0,sizeof(Dwarf_Half) * ATTR_ARRAY_SIZE);
894 memset(nt_form_array,0,sizeof(Dwarf_Half) * ATTR_ARRAY_SIZE);
895 printf("\n");
896 printindent(indent);
897 printf("Names Table, entry count %" DW_PR_DUu "\n",name_count);
898 printindent(indent);
899 #if 0
900 printf("[] ");
901 if (bucket_count) {
902
903 printf(" Bucket Hash");
904 } else {
905 }
906 #endif
907 printf("(Any DW_IDX_die_offset not in .debug_info"
908 " will be reported as an error.)\n");
909 for ( ; i <= name_count;++i) {
910 const char *tagname = "<TAGunknown>";
911 printindent(indent);
912 res = dwarf_dnames_name(dn,i,
913 &bucketnum, &hashval,
914 &offset_to_debug_str,&namestr,
915 &offset_in_entrypool, &abbrev_code,
916 &abbrev_tag,
917 array_size, nt_idxattr_array,
918 nt_form_array,
919 &attr_count,error);
920 if (res == DW_DLV_ERROR) {
921 return res;
922 }
923 if (res == DW_DLV_NO_ENTRY) {
924 printf("[%4" DW_PR_DUu "] ",i);
925 printf("\nERROR: NO ENTRY on name index "
926 "%" DW_PR_DUu " is impossible \n",i);
927 glflags.gf_count_major_errors++;
928 printf("\n");
929 continue;
930 }
931 printf("[Entry %4" DW_PR_DUu "] ",i);
932 if (bucket_count) {
933 printf("Bucket# %" DW_PR_DUu " ",bucketnum);
934 printf("hash 0x%" DW_PR_XZEROS DW_PR_DUx " ",hashval);
935 }
936 printf("nameoffset=0x%" DW_PR_DUx , offset_to_debug_str);
937 printf(" \"%s\"",namestr?sanitized(namestr):"<null>");
938 printf("\n");
939 printindent(indent+4);
940 dwarf_get_TAG_name((unsigned int)abbrev_tag,&tagname);
941 printf("EntrypoolOffset=0x%06" DW_PR_DUx ,
942 offset_in_entrypool);
943 printf(" abbrevcode=%4" DW_PR_DUu,
944 abbrev_code);
945 printf(" (0x%04" DW_PR_DUx ")",
946 abbrev_code);
947 printf(" attrcount=%" DW_PR_DUu,
948 attr_count);
949 printf(" arraysz=%" DW_PR_DUu "\n",
950 array_size);
951 printindent(indent+4);
952 printf("Tag=0x%04x %-16s",
953 abbrev_tag,tagname);
954 printf("\n");
955 if (glflags.verbose) {
956 print_attr_array(indent+2,
957 attr_count,array_size,
958 nt_idxattr_array, nt_form_array);
959 }
960 res = print_name_values(indent+6,
961 dbg,
962 dn,i,offset_in_entrypool,
963 local_type_unit_count,
964 has_single_cu_offset,
965 single_cu_offset,
966 error);
967 if (res != DW_DLV_OK) {
968 return res;
969 }
970 }
971 return DW_DLV_OK;
972 }
973
974 static int
975 print_dname_record(Dwarf_Debug dbg,Dwarf_Dnames_Head dn,
976 Dwarf_Unsigned offset,
977 Dwarf_Unsigned new_offset,
978 Dwarf_Error *error)
979 {
980 int res = 0;
981 Dwarf_Unsigned comp_unit_count = 0;
982 Dwarf_Unsigned local_type_unit_count = 0;
983 Dwarf_Unsigned foreign_type_unit_count = 0;
984 Dwarf_Unsigned bucket_count = 0;
985 Dwarf_Unsigned name_count = 0;
986 Dwarf_Unsigned abbrev_table_size = 0;
987 Dwarf_Unsigned entry_pool_size = 0;
988 Dwarf_Unsigned augmentation_string_size = 0;
989 Dwarf_Unsigned section_size = 0;
990 Dwarf_Half table_version = 0;
991 Dwarf_Half offset_size = 0;
992 char * augstring = 0;
993 unsigned int indent = 0;
994 Dwarf_Bool has_single_cu_offset;
995 Dwarf_Bool has_single_tu_offset;
996 Dwarf_Unsigned single_cu_offset = 0;
997 Dwarf_Unsigned single_tu_offset = 0;
998
999 res = dwarf_dnames_sizes(dn,&comp_unit_count,
1000 &local_type_unit_count,
1001 &foreign_type_unit_count,
1002 &bucket_count,
1003 &name_count,&abbrev_table_size,
1004 &entry_pool_size,&augmentation_string_size,
1005 &augstring,
1006 &section_size,&table_version,
1007 &offset_size,
1008 error);
1009 if (res != DW_DLV_OK) {
1010 return res;
1011 }
1012 abblist_ab_table_len = abbrev_table_size;
1013 printf("\n");
1014 printf("Name table offset : 0x%"
1015 DW_PR_XZEROS DW_PR_DUx " (offset of header length field)\n",
1016 offset);
1017 printf("Next name table offset : 0x%"
1018 DW_PR_XZEROS DW_PR_DUx "\n",
1019 new_offset);
1020 printf("Section size : 0x%"
1021 DW_PR_XZEROS DW_PR_DUx "\n",
1022 section_size);
1023 printf("Table version : %4u\n",
1024 table_version);
1025 printf("Comp unit count : %4" DW_PR_DUu "\n",
1026 comp_unit_count);
1027 printf("Type unit count : %4" DW_PR_DUu "\n",
1028 local_type_unit_count);
1029 printf("Foreign Type unit count : %4" DW_PR_DUu "\n",
1030 foreign_type_unit_count);
1031 printf("Bucket count : %4" DW_PR_DUu "\n",
1032 bucket_count);
1033 printf("Name count : %4" DW_PR_DUu "\n",
1034 name_count);
1035 printf("Abbrev table length : %4" DW_PR_DUu
1036 " (0x%04" DW_PR_DUx ")\n",
1037 abbrev_table_size,
1038 abbrev_table_size);
1039 printf("Entry pool size : %4" DW_PR_DUu
1040 " (0x%04" DW_PR_DUx ")\n",
1041 entry_pool_size,
1042 entry_pool_size);
1043 printf("Augmentation string size: %4" DW_PR_DUu
1044 " (0x%04" DW_PR_DUx ")\n",
1045 augmentation_string_size,
1046 augmentation_string_size);
1047 if (augmentation_string_size > 0) {
1048 printf("Augmentation string : %s\n",
1049 sanitized(augstring));
1050 }
1051 if (glflags.verbose) {
1052 res = print_dnames_offsets(indent+2,dn,error);
1053 if (res == DW_DLV_ERROR) {
1054 return res;
1055 }
1056 }
1057 if (glflags.verbose) {
1058 print_dnames_abbrevtable(indent+2,dn,
1059 abbrev_table_size);
1060 }
1061 res = print_cu_table(indent+2,dn,"cu",comp_unit_count,
1062 0,&has_single_cu_offset,&single_cu_offset,error);
1063 if (res == DW_DLV_ERROR) {
1064 return res;
1065 }
1066 /* There is no single offset default for TU indexes,
1067 only CU tables that have such a default. */
1068 res = print_cu_table(indent+2,dn,"tu",local_type_unit_count,
1069 foreign_type_unit_count,
1070 &has_single_tu_offset,&single_tu_offset,error);
1071 if (res == DW_DLV_ERROR) {
1072 return res;
1073 }
1074 res = print_buckets(indent+2,dn,bucket_count,error);
1075 if (res == DW_DLV_ERROR) {
1076 return res;
1077 }
1078 res = print_names_table(indent+2,dbg,dn,name_count,
1079 bucket_count,
1080 local_type_unit_count,
1081 has_single_cu_offset,
1082 single_cu_offset,error);
1083 if (res == DW_DLV_ERROR) {
1084 return res;
1085 }
1086 print_abblist_use(indent+2);
1087 return DW_DLV_OK;
1088 }
1089
1090 int
1091 print_debug_names(Dwarf_Debug dbg,Dwarf_Error *error)
1092 {
1093 Dwarf_Dnames_Head dnhead = 0;
1094 Dwarf_Unsigned offset = 0;
1095 Dwarf_Unsigned new_offset = 0;
1096 int res = 0;
1097 const char * section_name = ".debug_names";
1098 struct esb_s truename;
1099 char buf[DWARF_SECNAME_BUFFER_SIZE];
1100
1101 if (!dbg) {
1102 printf("\nERROR: Cannot print .debug_names, "
1103 "no Dwarf_Debug passed in\n");
1104 return DW_DLV_NO_ENTRY;
1105 }
1106 glflags.current_section_id = DEBUG_NAMES;
1107 /* Do nothing if not printing. */
1108 if (!glflags.gf_do_print_dwarf) {
1109 return DW_DLV_OK;
1110 }
1111
1112 /* Only print anything if we know it has debug names
1113 present. */
1114 reset_abblist();
1115 res = dwarf_dnames_header(dbg,offset,&dnhead,&new_offset,error);
1116 if (res == DW_DLV_NO_ENTRY) {
1117 reset_abblist();
1118 return res;
1119 }
1120 esb_constructor_fixed(&truename,buf,sizeof(buf));
1121 get_true_section_name(dbg,section_name,
1122 &truename,TRUE);
1123 printf("\n%s\n",sanitized(esb_get_string(&truename)));
1124 esb_destructor(&truename);
1125
1126 while (res == DW_DLV_OK) {
1127 res = print_dname_record(dbg,dnhead,
1128 offset,new_offset,error);
1129 if (res != DW_DLV_OK) {
1130 dwarf_dealloc_dnames(dnhead);
1131 dnhead = 0;
1132 reset_abblist();
1133 return res;
1134 }
1135 offset = new_offset;
1136 dwarf_dealloc_dnames(dnhead);
1137 dnhead = 0;
1138 reset_abblist();
1139 res = dwarf_dnames_header(dbg,offset,&dnhead,
1140 &new_offset,error);
1141 }
1142 reset_abblist();
1143 return res;
1144 }