Debian Codegraph @

src/bin/dwarfdump/print_debug_gnu.c

raw
1 /*
2 Copyright (C) 2020 David Anderson. All Rights Reserved.
3
4 Redistribution and use in source and binary forms, with
5 or without modification, are permitted provided that the
6 following conditions are met:
7
8 Redistributions of source code must retain the above
9 copyright notice, this list of conditions and the following
10 disclaimer.
11
12 Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials
15 provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /* To print .debug_gnu_pubnames, .debug_gnu_typenames */
33
34 #include <config.h>
35 #include <stdio.h> /* FILE decl for dd_esb.h */
36
37 #include "dwarf.h"
38 #include "libdwarf.h"
39 #include "libdwarf_private.h"
40 #include "dd_defined_types.h"
41 #include "dd_checkutil.h"
42 #include "dd_glflags.h"
43 #include "dd_globals.h"
44 #include "dd_naming.h"
45 #include "dd_esb.h" /* For flexible string buffer. */
46 #include "dd_esb_using_functions.h"
47 #include "dd_sanitized.h"
48 #include "print_debug_gnu.h"
49 #include "print_sections.h"
50
51 char *ikind_types[8] = {
52 "none",
53 "type",
54 "variable",
55 "function",
56 "other",
57 "unknown5",
58 "unknown6",
59 "unknown7" };
60
61 static int printed_infosize_error_a = FALSE;
62 static int printed_infosize_error_b = FALSE;
63
64 static int
65 print_block_entries(
66 Dwarf_Gnu_Index_Head head,
67 Dwarf_Unsigned blocknum,
68 Dwarf_Unsigned entrycount,
69 Dwarf_Unsigned max_offset,
70 Dwarf_Error *error)
71 {
72 Dwarf_Unsigned i = 0;
73 int res = 0;
74
75 printf(" [ ] offset Kind Name\n");
76
77 for ( ; i < entrycount; ++i) {
78 Dwarf_Unsigned offset_in_debug_info = 0;
79 const char *name = 0;
80 unsigned char flag = 0;
81 unsigned char staticorglobal = 0;
82 unsigned char typeofentry = 0;
83 /* flag is all 8 bits and staticorglobal
84 and typeofentry were extracted from the flag.
85 Present here so we can check all 8 bits
86 are correct (lowest 4 should be zero). */
87
88 res = dwarf_get_gnu_index_block_entry(head,
89 blocknum,i,&offset_in_debug_info,
90 &name,&flag,&staticorglobal,&typeofentry,
91 error);
92 if (res == DW_DLV_ERROR) {
93 return res;
94 }
95 if (res == DW_DLV_NO_ENTRY) {
96 printf(" ERROR: Block %" DW_PR_DUu
97 " entry %" DW_PR_DUu
98 " does not exist though entry count"
99 " is %" DW_PR_DUu
100 ", something is wrong\n",
101 blocknum,
102 i,entrycount);
103 glflags.gf_count_major_errors++;
104 return res;
105 }
106 printf(" [%3" DW_PR_DUu "] 0x%" DW_PR_XZEROS DW_PR_DUx,
107 i,offset_in_debug_info);
108 if (!printed_infosize_error_a &&
109 offset_in_debug_info >= max_offset) {
110 printed_infosize_error_a = TRUE;
111 printf(" ERROR: Block %" DW_PR_DUu
112 " entry %" DW_PR_DUu
113 " debug_info offset 0x%" DW_PR_DUx
114 " is greater than the debug_info section size"
115 " of 0x%" DW_PR_DUx
116 ", something is wrong. (message will not repeat)\n",
117 blocknum,
118 i,offset_in_debug_info,max_offset);
119 glflags.gf_count_major_errors++;
120 }
121 printf(" %s,%-8s",
122 staticorglobal?"s":"g",
123 ikind_types[0x7 & typeofentry]);
124 printf(" %s",sanitized(name));
125 printf("\n");
126 if (flag&0xf) {
127 printf(" ERROR: Block %" DW_PR_DUu
128 " entry %" DW_PR_DUu " flag 0x%x. "
129 "The lower bits are non-zero "
130 "so there may be a corruption problem.",
131 blocknum,i, flag);
132 glflags.gf_count_major_errors++;
133 printf("\n");
134 }
135 }
136 return DW_DLV_OK;
137 }
138
139 int attrlist[] = {
140 DW_AT_GNU_dwo_name,
141 DW_AT_dwo_name,
142 DW_AT_comp_dir,
143 DW_AT_GNU_dwo_id,
144 0 };
145
146 static void
147 error_report(int errcode,
148 const char *text,
149 Dwarf_Error *error) {
150 if (errcode == DW_DLV_ERROR) {
151 printf(" ERROR: %s"
152 ", ignoring other attributes here: %s\n",
153 text,
154 dwarf_errmsg(*error));
155 glflags.gf_count_major_errors++;
156 return;
157 } else {
158 printf(" ERROR impossible DW_DLV_NO_ENTRY: %s"
159 ", ignoringother attributes here. \n",
160 text);
161 }
162 return;
163 }
164 static void
165 print_selected_attributes(Dwarf_Debug dbg,
166 Dwarf_Die die,
167 Dwarf_Half version,
168 Dwarf_Half offset_size,
169 Dwarf_Error *error)
170 {
171 int res = 0;
172 int i = 0;
173
174 for ( ; attrlist[i]; ++i) {
175 Dwarf_Attribute attr = 0;
176 int attrid = 0;
177 const char * atname = 0;
178 Dwarf_Half form = 0;
179 enum Dwarf_Form_Class fclass = 0;
180 char *formstring = 0;
181 int print_str = FALSE;
182 struct esb_s m;
183 Dwarf_Sig8 sig;
184
185 attrid = attrlist[i];
186 res = dwarf_attr(die,attrid,&attr,error);
187 if (res == DW_DLV_ERROR) {
188 error_report(res,"dwarf_attr() returned error",error);
189 dwarf_dealloc_error(dbg,*error);
190 *error = 0;
191 return;
192 }
193 if (res == DW_DLV_NO_ENTRY) {
194 continue;
195 }
196 /* ok, this attribute is present */
197 atname = get_AT_name(attrid,FALSE);
198 res = dwarf_whatform(attr,&form,error);
199 if (res == DW_DLV_ERROR) {
200 error_report(res,"dwarf_whatform() problem: ",error);
201 dwarf_dealloc_error(dbg,*error);
202 dwarf_dealloc_attribute(attr);
203 *error = 0;
204 return;
205 }
206 if (res == DW_DLV_NO_ENTRY) {
207 /* impossible, cannot get here */
208 dwarf_dealloc_attribute(attr);
209 continue;
210 }
211 esb_constructor(&m);
212 fclass = dwarf_get_form_class(version,attrid,
213 offset_size,form);
214 if (fclass == DW_FORM_CLASS_STRING) {
215 res = dwarf_formstring(attr,&formstring,error);
216 if (res == DW_DLV_OK) {
217 print_str = TRUE;
218 esb_append(&m,formstring);
219 } else {
220 error_report(res,"dwarf_formstring() returned error",
221 error);
222 if (res == DW_DLV_ERROR) {
223 dwarf_dealloc_error(dbg,*error);
224 *error = 0;
225 }
226 dwarf_dealloc_attribute(attr);
227 esb_destructor(&m);
228 break;
229 }
230 } else if (fclass == DW_FORM_CLASS_CONSTANT) {
231 if (form == DW_FORM_data8) {
232 res = dwarf_formsig8_const(attr,&sig,error);
233 if (res == DW_DLV_OK) {
234 print_str = TRUE;
235 format_sig8_string(&sig,&m);
236 } else {
237 error_report(res,"dwarf_formsig8_const() "
238 "returned error",error);
239 if (res == DW_DLV_ERROR) {
240 dwarf_dealloc_error(dbg,*error);
241 *error = 0;
242 }
243 esb_destructor(&m);
244 dwarf_dealloc_attribute(attr);
245 break;
246 }
247 }
248 } else if (fclass == DW_FORM_CLASS_REFERENCE) {
249 /* includes DW_FORM_ref_sig8, DW_FORM_ref* */
250 if (form == DW_FORM_ref_sig8) {
251 res = dwarf_formsig8(attr,&sig,error);
252 if (res == DW_DLV_OK) {
253 print_str = TRUE;
254 esb_constructor(&m);
255 format_sig8_string(&sig,&m);
256 } else {
257 error_report(res,"dwarf_formsig8() "
258 "problem error",error);
259 if (res == DW_DLV_ERROR) {
260 dwarf_dealloc_error(dbg,*error);
261 *error = 0;
262 }
263 esb_destructor(&m);
264 dwarf_dealloc_attribute(attr);
265 break;
266 }
267 }
268 }
269 if (print_str) {
270 printf(" %-18s : %s\n",
271 atname,esb_get_string(&m));
272 }
273 dwarf_dealloc_attribute(attr);
274 esb_destructor(&m);
275 }
276 }
277
278 static int
279 print_die_basics(Dwarf_Debug dbg,
280 Dwarf_Die die,
281 Dwarf_Error *error)
282 {
283 int res = 0;
284 Dwarf_Half tag = 0;
285 Dwarf_Half version = 0;
286 Dwarf_Bool is_info = 0;
287 Dwarf_Bool is_dwo = 0;
288 Dwarf_Half offset_size = 0;
289 Dwarf_Half address_size = 0;
290 Dwarf_Half extension_size = 0;
291 Dwarf_Sig8 *signature = 0;
292 Dwarf_Off offset_of_length = 0;
293 Dwarf_Unsigned total_byte_length = 0;
294
295 res = dwarf_cu_header_basics(die, &version,
296 &is_info,&is_dwo,
297 &offset_size,&address_size,&extension_size,
298 &signature,&offset_of_length,&total_byte_length,error);
299 if (res != DW_DLV_OK) {
300 if (res == DW_DLV_ERROR) {
301 printf("ERROR: Cannot access compilation unit data: %s",
302 dwarf_errmsg(*error));
303 dwarf_dealloc_error(dbg,*error);
304 *error = 0;
305 } else {
306 printf("ERROR: Cannot access compilation unit data"
307 "No such found");
308 }
309 glflags.gf_count_major_errors++;
310 return DW_DLV_OK;
311 }
312 printf(" Compilation unit data follows\n");
313 printf(" CU version : %d\n",version);
314 if (!is_info) {
315 printf(" CU section is .debug_types");
316 }
317 printf(" CU section is dwo? : %s\n",
318 is_dwo?"yes":"no");
319 printf(" CU offset size : %u\n",
320 offset_size);
321 printf(" CU extension size : %u\n",
322 extension_size);
323 printf(" CU address size : %u\n",
324 address_size);
325 printf(" CU beginning offset : 0x%"
326 DW_PR_XZEROS DW_PR_DUx "\n",
327 offset_of_length);
328 printf(" CU total length : 0x%"
329 DW_PR_XZEROS DW_PR_DUx "\n",
330 total_byte_length);
331
332 if (signature) {
333 struct esb_s m;
334 char buf[24];
335
336 esb_constructor_fixed(&m,buf,sizeof(buf));
337 printf(" CU signature : ");
338 format_sig8_string(signature,&m);
339 printf("%s\n", esb_get_string(&m));
340 esb_destructor(&m);
341 }
342 res = dwarf_tag(die,&tag,error);
343 if (res != DW_DLV_OK) {
344 if (res == DW_DLV_ERROR) {
345 printf("ERROR: Cannot access DIE tag ERROR: %s\n",
346 dwarf_errmsg(*error));
347 dwarf_dealloc_error(dbg,*error);
348 *error = 0;
349 } else {
350 printf("ERROR: Cannot access DIE tag "
351 "No such found\n");
352 }
353 printf("\n");
354 glflags.gf_count_major_errors++;
355 } else {
356 const char *actual_tag_name = 0;
357
358 actual_tag_name = get_TAG_name(tag,FALSE);
359 printf(" CU die TAG : "
360 "%s\n", actual_tag_name);
361 }
362 print_selected_attributes(dbg,die,version,offset_size,error);
363 return DW_DLV_OK;
364 }
365
366 /* max_offset can be zero if no .debug_info
367 section present. */
368 static void
369 note_info_errors(Dwarf_Unsigned i,
370 Dwarf_Unsigned max_offset,
371 Dwarf_Unsigned offset_into_debug_info,
372 Dwarf_Unsigned size_of_debug_info_area)
373 {
374 if (printed_infosize_error_b) {
375 return;
376 }
377 if (size_of_debug_info_area > max_offset) {
378 printf(" ERROR: Block %" DW_PR_DUu
379 " required size of .debug_info"
380 " is %" DW_PR_DUu
381 " (0x%" DW_PR_DUx ")"
382 " which is greater than the size of "
383 ".debug_info of %" DW_PR_DUu
384 " bytes (message will not repeat)\n",
385 i,
386 size_of_debug_info_area,size_of_debug_info_area,
387 max_offset);
388 printed_infosize_error_b = TRUE;
389 glflags.gf_count_major_errors++;
390 return;
391 }
392 if (offset_into_debug_info >= max_offset) {
393 printf(" ERROR: Block %" DW_PR_DUu
394 " required offset into .debug_info"
395 " is %" DW_PR_DUu
396 " (0x%" DW_PR_DUx ")"
397 " which is greater than the size of "
398 ".debug_info of %" DW_PR_DUu
399 " bytes (message will not repeat)\n",
400 i,offset_into_debug_info,
401 offset_into_debug_info,
402 max_offset);
403 printed_infosize_error_b = TRUE;
404 glflags.gf_count_major_errors++;
405 return;
406 }
407 if ((size_of_debug_info_area+offset_into_debug_info)
408 > max_offset) {
409 printf(" ERROR: Block %" DW_PR_DUu
410 " required offset+size into .debug_info"
411 " is %" DW_PR_DUu
412 " which is greater than the size of "
413 ".debug_info of %" DW_PR_DUu
414 " bytes (message will not repeat)\n",
415 i,offset_into_debug_info+size_of_debug_info_area,
416 max_offset);
417 printed_infosize_error_b = TRUE;
418 glflags.gf_count_major_errors++;
419 return;
420 }
421 }
422
423 static int
424 print_all_blocks(Dwarf_Debug dbg,
425 Dwarf_Gnu_Index_Head head,
426 Dwarf_Unsigned block_count,
427 Dwarf_Error *error)
428 {
429 Dwarf_Unsigned i = 0;
430 int res = 0;
431
432 Dwarf_Unsigned max_offset = get_info_max_offset(dbg);
433
434 for ( ; i < block_count; ++i) {
435 Dwarf_Unsigned block_length = 0;
436 Dwarf_Half version = 0;
437 Dwarf_Unsigned offset_into_debug_info = 0;
438 Dwarf_Unsigned size_of_debug_info_area = 0;
439 Dwarf_Unsigned entrycount = 0;
440
441 res = dwarf_get_gnu_index_block(head,i,
442 &block_length,&version,
443 &offset_into_debug_info,
444 &size_of_debug_info_area,
445 &entrycount,error);
446 if (res == DW_DLV_NO_ENTRY) {
447 printf(" ERROR: Block %" DW_PR_DUu
448 " does not exist though block count"
449 " is %" DW_PR_DUu
450 ", something is wrong\n",
451 i,block_count);
452 glflags.gf_count_major_errors++;
453 return res;
454 }
455 if (res == DW_DLV_ERROR) {
456 return res;
457 }
458 printf(" Blocknumber : "
459 "%" DW_PR_DUu "\n",i);
460 printf(" Block length : "
461 "%" DW_PR_DUu "\n",block_length);
462 printf(" Version : "
463 "%u\n",version);
464 printf(" Offset into .debug_info section : "
465 "0x%" DW_PR_XZEROS DW_PR_DUx "\n",offset_into_debug_info);
466 printf(" Size of area in .debug_info section : "
467 "%" DW_PR_DUu "\n",size_of_debug_info_area);
468 note_info_errors(i,max_offset,offset_into_debug_info,
469 size_of_debug_info_area);
470 printf(" Number of entries in block : "
471 "%" DW_PR_DUu "\n",entrycount);
472 /* The CU offsets appear to be those in
473 the executable here. Not in
474 any dwo object. The offsets within
475 the entries in a block are a different
476 story and some of that seems odd,
477 the content names many things in libraries,
478 not just the executable or its dwo. ?
479 */
480 res = dwarf_get_cu_die_offset_given_cu_header_offset_b(
481 dbg,offset_into_debug_info,/*is_info = */ TRUE,
482 &offset_into_debug_info,error);
483 if (res != DW_DLV_OK) {
484 printf(" ERROR: Block %" DW_PR_DUu
485 " has an invalid .debug_info offset of "
486 "0x%" DW_PR_DUx
487 ", something is wrong\n",
488 i,offset_into_debug_info);
489 if (res == DW_DLV_ERROR) {
490 dwarf_dealloc_error(dbg,*error);
491 *error = 0;
492 }
493 glflags.gf_count_major_errors++;
494 } else {
495 Dwarf_Die die = 0;
496 Dwarf_Bool is_info = TRUE;
497 res = dwarf_offdie_b(dbg,offset_into_debug_info,
498 is_info, &die,error);
499 if (res != DW_DLV_OK) {
500 printf(" ERROR: Block %" DW_PR_DUu
501 " cu DIE offset 0x%" DW_PR_DUx
502 " is not a valid DIE offset in .debug_info\n",
503 i, offset_into_debug_info);
504 if (res == DW_DLV_ERROR) {
505 dwarf_dealloc_error(dbg,*error);
506 *error = 0;
507 }
508 glflags.gf_count_major_errors++;
509 } else {
510 /* Always returns DW_DLV_OK */
511 print_die_basics(dbg, die, error);
512 printf("\n");
513 dwarf_dealloc_die(die);
514 }
515 }
516 res = print_block_entries(head,i,entrycount,
517 max_offset,error);
518 if (res == DW_DLV_ERROR) {
519 return res;
520 }
521 }
522 return DW_DLV_OK;
523 }
524
525 /* November 25,2020: gdb 10.2 binutils source
526 can print these sections
527 but gdb does not, AFAICT, use this at all.
528 (binutils can print it, as can we).
529 The Block offset is part of the skeleton, and refers
530 to the skeleton CU DIEs (when
531 that is involved) but the individual item offsets
532 are referring to I-do-not-know-what.
533 Block zero refers to the single CU_DIE in
534 the .dwo file. The others....?
535 Nothing suggests how things actually connect up. */
536 int
537 print_debug_gnu(Dwarf_Debug dbg,
538 Dwarf_Error *error)
539 {
540 int res = 0;
541 Dwarf_Gnu_Index_Head head = 0;
542 Dwarf_Bool for_pubnames = TRUE;
543 Dwarf_Unsigned block_count = 0;
544 const char *stdname = 0;
545 char buf[DWARF_SECNAME_BUFFER_SIZE];
546 struct esb_s truename;
547 unsigned int i = 0;
548
549 printed_infosize_error_a = FALSE;
550 printed_infosize_error_b = FALSE;
551 for (i = 0; i < 2; i++) {
552 esb_constructor_fixed(&truename,buf,
553 DWARF_SECNAME_BUFFER_SIZE);
554 if (!i) {
555 glflags.current_section_id = DEBUG_GNU_PUBNAMES;
556 for_pubnames = TRUE;
557 stdname = ".debug_gnu_pubnames";
558 } else {
559 for_pubnames = FALSE;
560 glflags.current_section_id = DEBUG_GNU_PUBTYPES;
561 stdname = ".debug_gnu_pubtypes";
562 }
563 get_true_section_name(dbg,stdname, &truename,TRUE);
564 res = dwarf_get_gnu_index_head(dbg,for_pubnames,
565 &head, &block_count,error);
566 if (res == DW_DLV_ERROR) {
567 glflags.gf_count_major_errors++;
568 printf("ERROR: problem reading %s. %s\n",
569 sanitized(esb_get_string(&truename)),
570 dwarf_errmsg(*error));
571 dwarf_dealloc_error(dbg,*error);
572 *error = 0;
573 continue;
574 } else if (res == DW_DLV_NO_ENTRY) {
575 continue;
576 }
577 printf("\n%s with %" DW_PR_DUu
578 " blocks of names\n",
579 sanitized(esb_get_string(&truename)),
580 block_count);
581 res = print_all_blocks(dbg,head,block_count,error);
582 if (res == DW_DLV_ERROR) {
583 glflags.gf_count_major_errors++;
584 printf("ERROR: problem reading %s. %s\n",
585 sanitized(esb_get_string(&truename)),
586 dwarf_errmsg(*error));
587 dwarf_dealloc_error(dbg,*error);
588 *error = 0;
589 } else if (res == DW_DLV_NO_ENTRY) {
590 /* impossible */
591 } else {
592 /* normal */
593 }
594 dwarf_gnu_index_dealloc(head);
595 esb_destructor(&truename);
596 }
597 return DW_DLV_OK;
598 }