Debian Codegraph @

src/bin/dwarfdump/dwarfdump.c

raw
1 /*
2 Copyright (C) 2000,2002,2004,2005 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2007-2021 David Anderson. All Rights Reserved.
4 Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
5 Portions Copyright 2012 SN Systems Ltd. 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
31 /* The address of the Free Software Foundation is
32 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
33 Boston, MA 02110-1301, USA.
34 SGI has moved from the Crittenden Lane address.
35 */
36
37 #include <config.h>
38
39 #include <stddef.h> /* NULL size_t */
40 #include <stdio.h> /* FILE stdout stderr fprintf() printf() */
41 #include <stdlib.h> /* exit() free() malloc() qsort() realloc()
42 getenv() */
43 #include <string.h> /* memset() strcmp() stricmp()
44 strlen() strrchr() strstr() */
45
46 /* Windows specific header files */
47 #ifdef _WIN32
48 #ifdef HAVE_STDAFX_H
49 #include "stdafx.h"
50 #endif /* HAVE_STDAFX_H */
51 #include <io.h> /* close() dup2() */
52 #elif defined HAVE_UNISTD_H
53 #include <unistd.h> /* close() dup2() */
54 #endif /* _WIN32 */
55
56 #ifdef HAVE_FCNTL_H
57 #include <fcntl.h> /* O_RDONLY open() */
58 #endif /* HAVE_FCNTL_H */
59 #ifdef HAVE_UTF8
60 /* locale.h is guaranteed in C90 and later,
61 but langinfo.h might not be present. */
62 #include "locale.h"
63 #include "langinfo.h"
64 #endif /* HAVE_UTF8 */
65
66 #include "dwarf.h"
67 #include "libdwarf.h"
68 #include "libdwarf_private.h"
69 #include "dd_defined_types.h"
70 #include "dd_checkutil.h"
71 #include "dd_glflags.h"
72 #include "dd_globals.h"
73 #include "dd_makename.h"
74 #include "dd_macrocheck.h"
75 #include "dd_dwconf.h"
76 #include "dd_dwconf_using_functions.h"
77 #include "dd_common.h"
78 #include "dd_helpertree.h"
79 #include "dd_esb.h" /* For flexible string buffer. */
80 #include "dd_esb_using_functions.h"
81 #include "dd_sanitized.h"
82 #include "dd_tag_common.h"
83 #include "dd_addrmap.h"
84 #include "dd_attr_form.h"
85 #include "print_debug_gnu.h"
86 #include "dd_naming.h" /* for get_FORM_name() */
87 #include "dd_command_options.h"
88 #include "dd_compiler_info.h"
89 #include "dd_safe_strcpy.h"
90 #include "dd_minimal.h"
91 #include "dd_mac_cputype.h"
92 #include "dd_elf_cputype.h"
93 #include "dd_pe_cputype.h"
94
95 #ifndef O_RDONLY
96 /* This is for a Windows environment */
97 # define O_RDONLY _O_RDONLY
98 #endif
99
100 #ifdef _O_BINARY
101 /* This is for a Windows environment */
102 #define O_BINARY _O_BINARY
103 #else
104 # ifndef O_BINARY
105 # define O_BINARY 0 /* So it does nothing in Linux/Unix */
106 # endif
107 #endif /* O_BINARY */
108
109 #define BYTES_PER_INSTRUCTION 4
110
111 #define LOCAL_PTR_ARY_COUNT 50
112
113 /* Build section information */
114 void build_linkonce_info(Dwarf_Debug dbg);
115
116 struct glflags_s glflags;
117
118 /* Functions used to manage the unique errors table */
119 static void allocate_unique_errors_table(void);
120 static void release_unique_errors_table(void);
121 #ifdef TESTING
122 static void dump_unique_errors_table(void);
123 #endif
124 static Dwarf_Bool add_to_unique_errors_table(char * error_text);
125
126 static struct esb_s esb_short_cu_name;
127 static struct esb_s esb_long_cu_name;
128 static struct esb_s dwarf_error_line;
129 static int global_basefd = -1;
130 static int global_tiedfd = -1;
131 static struct esb_s global_file_name;
132 static struct esb_s global_tied_file_name;
133
134 static void print_machine_arch(Dwarf_Debug dbg);
135
136 static void homeify(char *s, struct esb_s* out);
137
138 static int process_one_file(
139 const char * file_name,
140 const char * tied_file_name,
141 char * tempbuf,
142 size_t tempbuflen,
143 struct dwconf_s *conf);
144
145 static int print_gnu_debuglink(Dwarf_Debug dbg,Dwarf_Error *err);
146
147 static int
148 open_a_file(const char * name)
149 {
150 /* Set to a file number that cannot be legal. */
151 int fd = -1;
152 fd = open(name, O_RDONLY | O_BINARY);
153 return fd;
154 }
155
156 static void
157 close_a_file(int f)
158 {
159 if (f != -1) {
160 close(f);
161 }
162 }
163
164 void
165 global_destructors(void)
166 {
167 makename_destructor();
168 uri_data_destructor();
169 esb_destructor(&esb_long_cu_name);
170 esb_destructor(&esb_short_cu_name);
171 esb_destructor(&dwarf_error_line);
172 /* Global flags initialization and esb-buffers destruction. */
173 reset_global_flags();
174 esb_destructor(&global_file_name);
175 esb_destructor(&global_tied_file_name);
176 free_all_dwconf(glflags.config_file_data);
177 sanitized_string_destructor();
178 ranges_esb_string_destructor();
179 close_a_file(global_basefd);
180 close_a_file(global_tiedfd);
181 #ifdef _WIN32
182 /* Close the null device used during formatting printing */
183 esb_close_null_device();
184 #endif /* _WIN32 */
185 if (glflags.gf_global_debuglink_paths) {
186 unsigned int i = 0;
187
188 for ( ; i < glflags.gf_global_debuglink_count ; ++i ) {
189 free(glflags.gf_global_debuglink_paths[i]);
190 glflags.gf_global_debuglink_paths[i] = 0;
191 }
192 free(glflags.gf_global_debuglink_paths);
193 glflags.gf_global_debuglink_paths = 0;
194 }
195 glflags.gf_global_debuglink_count = 0;
196 }
197
198 static void
199 check_for_notes(void)
200 {
201 long int ect = glflags.gf_count_macronotes;
202 const char *w = "was";
203 const char *e = "MACRONOTE";
204 if (!ect) {
205 return;
206 }
207 if (ect > 1) {
208 w = "were";
209 e = "MACRONOTEs";
210 }
211 printf("There %s %ld DWARF %s reported: "
212 "see MACRONOTE above.\n",
213 w, ect,e);
214 }
215 static void
216 check_for_major_errors(void)
217 {
218 long int ect = glflags.gf_count_major_errors;
219 const char *w = "was";
220 const char *e = "error";
221 if (!ect) {
222 return;
223 }
224 if (ect > 1) {
225 w = "were";
226 e = "errors";
227 }
228 printf("There %s %ld DWARF %s reported: "
229 "see ERROR above.\n",
230 w, ect,e);
231 }
232
233 static void
234 flag_data_pre_allocation(void)
235 {
236 memset(glflags.section_high_offsets_global,0,
237 sizeof(*glflags.section_high_offsets_global));
238 /* If we are checking .debug_line, .debug_ranges, .debug_aranges,
239 or .debug_loc build the tables containing
240 the pairs LowPC and HighPC. It is safer (and not
241 expensive) to build all
242 of these at once so mistakes in options do not lead
243 to coredumps (like -ka -p did once). */
244 if (glflags.gf_check_decl_file || glflags.gf_check_ranges ||
245 glflags.gf_check_locations ||
246 glflags.gf_do_check_dwarf ||
247 glflags.gf_check_self_references) {
248 glflags.pRangesInfo = AllocateBucketGroup(KIND_RANGES_INFO);
249 glflags.pLinkonceInfo =
250 AllocateBucketGroup(KIND_LINKONCE_INFO);
251 glflags.pVisitedInfo = AllocateBucketGroup(KIND_VISITED_INFO);
252 }
253 /* Create the unique error table */
254 if (glflags.gf_print_unique_errors) {
255 allocate_unique_errors_table();
256 }
257 /* Allocate range array to be used by all CUs */
258 if (glflags.gf_check_ranges) {
259 allocate_range_array_info();
260 }
261 }
262
263 static void
264 flag_data_post_cleanup(void)
265 {
266 if (glflags.pRangesInfo) {
267 ReleaseBucketGroup(glflags.pRangesInfo);
268 glflags.pRangesInfo = 0;
269 }
270 if (glflags.pLinkonceInfo) {
271 ReleaseBucketGroup(glflags.pLinkonceInfo);
272 glflags.pLinkonceInfo = 0;
273 }
274 if (glflags.pVisitedInfo) {
275 ReleaseBucketGroup(glflags.pVisitedInfo);
276 glflags.pVisitedInfo = 0;
277 }
278 /* Release range array to be used by all CUs */
279 if (glflags.gf_check_ranges) {
280 release_range_array_info();
281 }
282 /* Delete the unique error set */
283 if (glflags.gf_print_unique_errors) {
284 release_unique_errors_table();
285 }
286 clean_up_compilers_detected();
287 destruct_abbrev_array();
288 }
289
290 void _dwarf_alloc_tree_counts(Dwarf_Unsigned *allocount,
291 Dwarf_Unsigned *allosum,
292 Dwarf_Unsigned *treecount,
293 Dwarf_Unsigned *treesum,
294 Dwarf_Unsigned *earlydealloccount,
295 Dwarf_Unsigned *earlydeallocsize,
296 Dwarf_Unsigned *unused1,
297 Dwarf_Unsigned *unused2,
298 Dwarf_Unsigned *unused3);
299
300 /*
301 Iterate through dwarf and print all info.
302 */
303 int
304 main(int argc, char *argv[])
305 {
306 const char *file_name = 0;
307 unsigned ftype = 0;
308 unsigned endian = 0;
309 unsigned offsetsize = 0;
310 Dwarf_Unsigned filesize = 0;
311 int errcode = 0;
312 char *temp_path_buf = 0;
313 size_t temp_path_buf_len = 0;
314 int res = 0;
315 /* path_source will be DW_PATHSOURCE_basic */
316 unsigned char path_source = DW_PATHSOURCE_unspecified;
317
318 #ifdef _WIN32
319 /* Open the null device used during formatting printing */
320 if (!esb_open_null_device()) {
321 printf("ERROR dwarfdump: Unable to open null device.\n");
322 exit(EXIT_FAILURE);
323 }
324 #endif /* _WIN32 */
325
326 /* Global flags initialization and esb-buffers construction. */
327 init_global_flags();
328
329 set_checks_off();
330 uri_data_constructor();
331 esb_constructor(&esb_short_cu_name);
332 esb_constructor(&esb_long_cu_name);
333 esb_constructor(&dwarf_error_line);
334 #ifdef _WIN32
335 /* Often we redirect the output to a file, but we have found
336 issues due to the buffering associated with stdout.
337 Some issues were fixed just by the use of 'fflush',
338 but the main issued remained.
339 The stdout stream is buffered, so will only display
340 what's in the buffer after it reaches a newline
341 (or when it's told to).
342 We have a few options to print immediately:
343 - Print to stderr instead using fprintf.
344 - Print to stdout and flush stdout whenever
345 we need it to using fflush.
346 - We can also disable buffering on stdout by using setbuf:
347 setbuf(stdout,NULL);
348 Make stdout unbuffered; this seems to work for all cases.
349 The problem is no longer present. Now, for practical
350 purposes, there is no stderr output, all is stdout.
351 September 2018. */
352
353 /* Calling setbuf() with NULL argument, it turns off
354 all buffering for the specified stream.
355 Then writing to and/or reading from the stream
356 will be exactly as directed by the program.
357 But if dwarfdump is used over a network drive,
358 it shows a dramatic
359 slowdown when sending the output to a file.
360 An operation that takes
361 couple of seconds, it was taking few hours. */
362 /* setbuf(stdout,NULL); */
363 /* Redirect stderr to stdout. */
364 /* No more redirect needed. We only use stdout */
365 #endif /* _WIN32 */
366
367 #ifdef HAVE_UTF8
368 {
369 char *langinf = 0;
370
371 setlocale(LC_CTYPE, "");
372 setlocale(LC_NUMERIC, "");
373 langinf = nl_langinfo(CODESET);
374 if (strcmp(langinf,"UTF-8") && strcmp(langinf,"UTF8")) {
375 glflags.gf_print_utf8_flag = FALSE;
376 }else {
377 glflags.gf_print_utf8_flag = TRUE;
378 }
379 }
380 #endif /* HAVE_UTF8 */
381 file_name = process_args(argc, argv);
382 /* print_version_details already done in
383 dd_command_options.c */
384 print_args(argc,argv);
385
386 /* Redirect stdout to an specific file */
387 if (glflags.output_file) {
388 if (NULL == freopen(glflags.output_file,"w",stdout)) {
389 printf("ERROR dwarfdump: Unable to redirect "
390 "output to '%s'\n",
391 glflags.output_file);
392 global_destructors();
393 exit(EXIT_FAILURE);
394 }
395 /* Record version and arguments in the output file */
396 print_version_details(argv[0]);
397 print_args(argc,argv);
398 }
399
400 /* Allow the user to hide some warnings by using
401 command line options */
402 {
403 Dwarf_Cmdline_Options wcmd;
404 /* The struct has just one field!.
405 If glflags.gf_check_verbose_mode is non-zero
406 this tells libdwarf to emit a detailed
407 message (which flows to the caller via
408 _dwarf_printf()) about the header problem.
409 Defaults to zero for print options,
410 Set to 1 for check options like -ka
411 dwarfdump "-ks --check-silent" sets it zero. */
412
413 wcmd.check_verbose_mode = glflags.gf_check_verbose_mode;
414 dwarf_record_cmdline_options(wcmd);
415 }
416 if (glflags.gf_check_functions) {
417 static const Dwarf_Signed stab[] =
418 {0,1,-1,100,-100,-10000000,10000000};
419 int i = 0;
420 int len = sizeof(stab)/sizeof(stab[0]);
421 char vbuf[100];
422
423 vbuf[0] = 0;
424 DWARF_CHECK_COUNT(check_functions_result,1);
425 for (i = 0; i < len; ++i) {
426 Dwarf_Signed basevalue = 0;
427 Dwarf_Signed decodedvalue = 0;
428 Dwarf_Unsigned silen = 0;
429 int leblen = 0;
430
431 basevalue = stab[i];
432 memset(vbuf,0,sizeof(vbuf));
433 res = dwarf_encode_signed_leb128(basevalue,
434 &leblen,
435 vbuf,(int)sizeof(vbuf));
436 if (res == DW_DLV_ERROR) {
437 DWARF_CHECK_ERROR(check_functions_result,
438 "Got error encoding Encoding Dwarf_Signed");
439 break;
440 }
441 res = dwarf_decode_signed_leb128(
442 vbuf,&silen, &decodedvalue,
443 vbuf + sizeof(vbuf));
444 if (res == DW_DLV_ERROR) {
445 DWARF_CHECK_ERROR(check_functions_result,
446 "Got error encoding Decoding Dwarf_Signed");
447 break;
448 }
449 if ( decodedvalue != basevalue) {
450 DWARF_CHECK_ERROR(check_functions_result,
451 "Decode Dwarf_signed does not match"
452 "starting value");
453 break;
454 }
455 }
456 }
457 /* ======= BEGIN FINDING NAMES AND OPENING FDs ===== */
458 /* The 200+2 etc is more than suffices for the expansion that a
459 MacOS dsym or a GNU debuglink might need, we hope. */
460 temp_path_buf_len = strlen(file_name)*3 + 200 + 2;
461 temp_path_buf = malloc(temp_path_buf_len);
462 if (!temp_path_buf) {
463 printf("%s ERROR: Unable to malloc %lu bytes "
464 "for possible path string %s.\n",
465 glflags.program_name,(unsigned long)temp_path_buf_len,
466 file_name);
467 global_destructors();
468 exit(EXIT_FAILURE);
469 }
470 temp_path_buf[0] = 0;
471 /* This data scan is to find Elf objects and
472 unknown objects early. If the user
473 asks for certain options
474 that will rule out handling GNU_debuglink
475 on that object. This does not concern itself
476 with dSYM or debuglink at all. */
477 res = dwarf_object_detector_path_b(file_name,
478 0,0,
479 0,0,
480 &ftype,&endian,&offsetsize,&filesize,
481 &path_source,&errcode);
482 if (res != DW_DLV_OK) {
483 if (res == DW_DLV_ERROR) {
484 printf("%s ERROR: Can't open/use %s. Error %s\n",
485 glflags.program_name, sanitized(file_name),
486 dwarf_errmsg_by_number(errcode));
487 } else {
488 printf("%s ERROR: Can't open %s. "
489 "There is no valid object file with that name)\n",
490 glflags.program_name, sanitized(file_name));
491 }
492 global_destructors();
493 free(temp_path_buf);
494 exit(EXIT_FAILURE);
495 }
496 esb_append(&global_file_name,file_name);
497 temp_path_buf[0] = 0;
498 global_basefd = open_a_file(esb_get_string(
499 &global_file_name));
500 if (global_basefd == -1) {
501 printf("%s ERROR: can't open.. %s\n",
502 glflags.program_name,
503 esb_get_string(&global_file_name));
504 global_destructors();
505 free(temp_path_buf);
506 exit(EXIT_FAILURE);
507 }
508
509 if (esb_string_len(glflags.config_file_tiedpath) > 0) {
510 unsigned tftype = 0;
511 unsigned tendian = 0;
512 unsigned toffsetsize = 0;
513 Dwarf_Unsigned tfilesize = 0;
514 const char * tied_file_name = 0;
515 /* path_source will be DW_PATHSOURCE_basic */
516 unsigned char tpath_source = 0;
517
518 temp_path_buf[0] = 0;
519 tied_file_name = esb_get_string(glflags.config_file_tiedpath);
520 /* A genuine tiedpath cannot be dsym or debuglink. */
521 res = dwarf_object_detector_path_b (tied_file_name,
522 0,0,
523 0,0,
524 &tftype,&tendian,&toffsetsize,&tfilesize,
525 &tpath_source,&errcode);
526 if (res != DW_DLV_OK) {
527 if (res == DW_DLV_ERROR) {
528 char *errmsg = 0;
529 struct esb_s m;
530
531 esb_constructor(&m);
532 errmsg = dwarf_errmsg_by_number(errcode);
533 homeify((char *)sanitized(tied_file_name),&m);
534 printf("%s ERROR: can't open tied file"
535 ".. %s: %s\n",
536 glflags.program_name,
537 esb_get_string(&m),
538 errmsg);
539 esb_destructor(&m);
540 } else {
541 struct esb_s m;
542 esb_constructor(&m);
543 homeify((char *)sanitized(tied_file_name),&m);
544 printf(
545 "%s ERROR: tied file not an object file '%s'.\n",
546 glflags.program_name, esb_get_string(&m));
547 esb_destructor(&m);
548 }
549 glflags.gf_count_major_errors++;
550 global_destructors();
551 free(temp_path_buf);
552 exit(EXIT_FAILURE);
553 }
554 if (ftype != tftype || endian != tendian ||
555 offsetsize != toffsetsize) {
556 struct esb_s m;
557 struct esb_s mgf;
558
559 esb_constructor(&m);
560 esb_constructor(&mgf);
561 homeify((char *)sanitized(tied_file_name),&m);
562 homeify((char *)esb_get_string(&global_file_name),&mgf);
563 printf("%s ERROR: tied file \'%s\' and "
564 "main file \'%s\' not "
565 "the same kind of object!\n",
566 glflags.program_name,
567 esb_get_string(&m),
568 esb_get_string(&mgf));
569 esb_destructor(&m);
570 esb_destructor(&mgf);
571 free(temp_path_buf);
572 global_destructors();
573 glflags.gf_count_major_errors++;
574 exit(EXIT_FAILURE);
575 }
576 esb_append(&global_tied_file_name,tied_file_name);
577 global_tiedfd = open_a_file(esb_get_string(
578 &global_tied_file_name));
579 if (global_tiedfd == -1) {
580 struct esb_s m;
581
582 esb_constructor(&m);
583 homeify((char *)sanitized(esb_get_string(
584 &global_tied_file_name)), &m);
585 printf("%s ERROR: can't open tied file"
586 "... %s\n",
587 glflags.program_name,
588 esb_get_string(&m));
589 esb_destructor(&m);
590 global_destructors();
591 glflags.gf_count_major_errors++;
592 free(temp_path_buf);
593 exit(EXIT_FAILURE);
594 }
595 }
596 /* ======= end FINDING NAMES AND OPENING FDs ===== */
597 temp_path_buf[0] = 0;
598 /* ======= BEGIN PROCESSING OBJECT FILES BY TYPE ===== */
599 if ((ftype == DW_FTYPE_ELF && (glflags.gf_reloc_flag ||
600 glflags.gf_header_flag)) ||
601 ftype == DW_FTYPE_ARCHIVE) {
602 printf("ERROR Can't process %s: archives and "
603 "printing elf headers not supported in this dwarfdump "
604 "--disable-libelf build.\n",
605 file_name);
606 glflags.gf_count_major_errors++;
607 } else if (ftype == DW_FTYPE_ELF ||
608 ftype == DW_FTYPE_MACH_O ||
609 ftype == DW_FTYPE_APPLEUNIVERSAL ||
610 ftype == DW_FTYPE_PE ) {
611 flag_data_pre_allocation();
612 close_a_file(global_basefd);
613 global_basefd = -1;
614 close_a_file(global_tiedfd);
615 global_tiedfd = -1;
616 process_one_file(
617 esb_get_string(&global_file_name),
618 esb_get_string(&global_tied_file_name),
619 temp_path_buf, (unsigned int)temp_path_buf_len,
620 glflags.config_file_data);
621 flag_data_post_cleanup();
622 } else {
623 printf("ERROR Can't process %s: unhandled format\n",
624 file_name);
625 glflags.gf_count_major_errors++;
626 }
627 free(temp_path_buf);
628 temp_path_buf = 0;
629 temp_path_buf_len = 0;
630 /* ======= END PROCESSING OBJECT FILES BY TYPE ===== */
631
632 /* These cleanups only necessary once all
633 objects processed. */
634 /* In case of a serious DWARF error
635 we try to get here, we try not
636 to exit(1) by using print_error() */
637 check_for_major_errors();
638 check_for_notes();
639 flag_data_post_cleanup();
640 global_destructors();
641 free(temp_path_buf);
642 /* As the tool have reached this point, it means there are
643 no internal errors and we should return an OKAY condition,
644 regardless if the file being processed has
645 minor errors. */
646 exit(0);
647 }
648
649 void
650 print_any_harmless_errors(Dwarf_Debug dbg)
651 {
652 /* We do not need to initialize the local array,
653 libdwarf does it. */
654 const char *buf[LOCAL_PTR_ARY_COUNT];
655 unsigned totalcount = 0;
656 unsigned i = 0;
657 unsigned printcount = 0;
658 int res = dwarf_get_harmless_error_list(dbg,
659 LOCAL_PTR_ARY_COUNT,buf,
660 &totalcount);
661 if (res == DW_DLV_NO_ENTRY) {
662 return;
663 }
664 if (totalcount > 0) {
665 printf("\n*** HARMLESS ERROR COUNT: %u ***\n",totalcount);
666 }
667 for (i = 0 ; buf[i]; ++i) {
668 ++printcount;
669 DWARF_CHECK_COUNT(harmless_result,1);
670 DWARF_CHECK_ERROR(harmless_result,buf[i]);
671 }
672 if (totalcount > printcount) {
673 /*harmless_result.checks += (totalcount - printcount); */
674 DWARF_CHECK_COUNT(harmless_result,(totalcount - printcount));
675 /*harmless_result.errors += (totalcount - printcount); */
676 DWARF_ERROR_COUNT(harmless_result,(totalcount - printcount));
677 }
678 }
679
680 /* Print a summary of search results */
681 static void
682 print_search_results(void)
683 {
684 const char *search_type = 0;
685 const char *search_text = 0;
686 if (glflags.search_any_text) {
687 search_type = "any";
688 search_text = glflags.search_any_text;
689 } else {
690 if (glflags.search_match_text) {
691 search_type = "match";
692 search_text = glflags.search_match_text;
693 } else {
694 search_type = "regex";
695 search_text = glflags.search_regex_text;
696 }
697 }
698 fflush(stdout);
699 printf("\nSearch type : '%s'\n",search_type);
700 printf("Pattern searched : '%s'\n",search_text);
701 printf("Occurrences Found: %d\n",glflags.search_occurrences);
702 fflush(stdout);
703 }
704
705 /* This is for dwarf_print_lines(), we are not using
706 the userdata because dwarfdump does not need it. */
707 static void
708 printf_callback_for_libdwarf(void *userdata,
709 const char *data)
710 {
711 (void)userdata;
712 printf("%s",sanitized(data));
713 }
714
715 int
716 get_address_size_and_max(Dwarf_Debug dbg,
717 Dwarf_Half * size,
718 Dwarf_Addr * max,
719 Dwarf_Error *aerr)
720 {
721 int dres = 0;
722 Dwarf_Half lsize = 4;
723 /* Get address size and largest representable address */
724 dres = dwarf_get_address_size(dbg,&lsize,aerr);
725 if (dres != DW_DLV_OK) {
726 return dres;
727 }
728 if (max) {
729 *max = (lsize == 8 ) ? 0xffffffffffffffffULL : 0xffffffff;
730 }
731 if (size) {
732 *size = lsize;
733 }
734 return DW_DLV_OK;
735 }
736
737 /* dbg is often null when dbgtied was passed in. */
738 static void
739 dbgsetup(Dwarf_Debug dbg,struct dwconf_s *setup_config_file_data)
740 {
741 if (!dbg) {
742 return;
743 }
744 dwarf_set_frame_rule_initial_value(dbg,
745 (Dwarf_Half)setup_config_file_data->cf_initial_rule_value);
746 dwarf_set_frame_rule_table_size(dbg,
747 (Dwarf_Half)setup_config_file_data->cf_table_entry_count);
748 dwarf_set_frame_cfa_value(dbg,
749 (Dwarf_Half)setup_config_file_data->cf_cfa_reg);
750 dwarf_set_frame_same_value(dbg,
751 (Dwarf_Half)setup_config_file_data->cf_same_val);
752 dwarf_set_frame_undefined_value(dbg,
753 (Dwarf_Half)setup_config_file_data->cf_undefined_val);
754 if (setup_config_file_data->cf_address_size) {
755 /* In libdwarf we mostly use Dwarf_Half,
756 but Dwarf_Small works ok here. */
757 dwarf_set_default_address_size(dbg,
758 (Dwarf_Small)setup_config_file_data->cf_address_size);
759 }
760 dwarf_set_harmless_error_list_size(dbg,50);
761 }
762
763 /* Callable at any time, Sets section sizes with the sizes
764 known as of the call.
765 Repeat whenever about to reference a size that might not
766 have been set as of the last call. */
767 static void
768 set_global_section_sizes(Dwarf_Debug dbg)
769 {
770 dwarf_get_section_max_offsets_d(dbg,
771 &glflags.section_high_offsets_global->debug_info_size,
772 &glflags.section_high_offsets_global->debug_abbrev_size,
773 &glflags.section_high_offsets_global->debug_line_size,
774 &glflags.section_high_offsets_global->debug_loc_size,
775 &glflags.section_high_offsets_global->debug_aranges_size,
776 &glflags.section_high_offsets_global->debug_macinfo_size,
777 &glflags.section_high_offsets_global->debug_pubnames_size,
778 &glflags.section_high_offsets_global->debug_str_size,
779 &glflags.section_high_offsets_global->debug_frame_size,
780 &glflags.section_high_offsets_global->debug_ranges_size,
781 &glflags.section_high_offsets_global->debug_pubtypes_size,
782 &glflags.section_high_offsets_global->debug_types_size,
783 &glflags.section_high_offsets_global->debug_macro_size,
784 &glflags.section_high_offsets_global->debug_str_offsets_size,
785 &glflags.section_high_offsets_global->debug_sup_size,
786 &glflags.section_high_offsets_global->debug_cu_index_size,
787 &glflags.section_high_offsets_global->debug_tu_index_size,
788 &glflags.section_high_offsets_global->debug_names_size,
789 &glflags.section_high_offsets_global->debug_loclists_size,
790 &glflags.section_high_offsets_global->debug_rnglists_size);
791
792 }
793
794 /* Set limits for Ranges Information.
795 The linker may
796 put parts of the text(code) in additional sections
797 such as .init .fini __libc_freeres_fn
798 .rodata __libc_subfreeres __libc_atexit too. */
799 #define LIKELYNAMESMAX 3
800 static const char *likely_ns[LIKELYNAMESMAX] = {
801 /* .text is first as it is often the only thing.See below. */
802 ".init",
803 ".text",
804 ".fini"
805 };
806 #define ORIGLKLYTEXTINDEX 1
807 struct likely_names_s {
808 const char * name;
809 int origindex;
810 Dwarf_Unsigned low;
811 Dwarf_Unsigned size;
812 Dwarf_Unsigned end;
813 };
814 static struct likely_names_s likely_names[LIKELYNAMESMAX];
815 #if 0 /* FOR DEBUG ONLY */
816 static void
817 printlnrec(const char *msg,struct likely_names_s * ln,
818 int line,char * fn)
819 {
820 printf("%s: name %s origindx %d "
821 "low 0x%lx "
822 "size 0x%lx "
823 "end 0x%lx "
824 " line %d %s\n",msg,
825 ln->name,ln->origindex,
826 (unsigned long)ln->low,
827 (unsigned long)ln->size,
828 (unsigned long)ln->end,line,fn);
829 }
830 #endif /* 0 */
831
832 static int
833 likelycmp(const void *l_in, const void *r_in)
834 {
835 struct likely_names_s *l = (struct likely_names_s *)l_in;
836 struct likely_names_s *r = (struct likely_names_s *)r_in;
837
838 if (l->low < r->low) {
839 return -1;
840 }
841 if (l->low > r->low ) {
842 return 1;
843 }
844 if (l->end < r->end) {
845 return -1;
846 }
847 if (l->end > r->end) {
848 return 1;
849 }
850 return 0;
851 }
852
853 /* This is a bit slow, but happens only once for a dbg.
854 It is not as much help as I expected in avoiding
855 line table content CHECK warnings because, so far,
856 those come from .init csu code and the DWARF has
857 no subprogram information nor any high/low pc
858 information at all. */
859 static int
860 calculate_likely_limits_of_code(Dwarf_Debug dbg,
861 Dwarf_Unsigned *lower,
862 Dwarf_Unsigned *size)
863 {
864 struct likely_names_s * ln = 0;
865 int ct = 0;
866 Dwarf_Unsigned baselow = 0;
867 Dwarf_Unsigned basesize = 0;
868 Dwarf_Unsigned baseend = 0;
869 int lnindex = 0;
870 int lncount = 0;
871
872 memset(likely_names,0,sizeof(likely_names));
873 for (ct = 0 ; ct < LIKELYNAMESMAX; ct++) {
874 Dwarf_Unsigned clow = 0;
875 Dwarf_Unsigned csize = 0;
876 int res = 0;
877 Dwarf_Error err = 0;
878 /* Just looks for .text and .init and .fini for ranges. */
879 const char *name = likely_ns[ct];
880
881 ln = likely_names + lnindex;
882 res = dwarf_get_section_info_by_name_a(dbg,name,
883 &clow,&csize,0,0,&err);
884 if (res == DW_DLV_ERROR) {
885 dwarf_dealloc_error(dbg,err);
886 return res;
887 }
888 if (res == DW_DLV_NO_ENTRY) {
889 continue;
890 }
891 ln->name = name;
892 ln->low = clow;
893 ln->size = csize;
894 ln->end = csize +clow;
895 ln->origindex = ct;
896 if (ct == ORIGLKLYTEXTINDEX) {
897 basesize = csize;
898 baselow = clow;
899 }
900 ++lnindex;
901 }
902 if (!lnindex) {
903 return DW_DLV_NO_ENTRY;
904 }
905 if (lnindex == 1) {
906 *lower = baselow;
907 *size = basesize;
908 return DW_DLV_OK;
909 }
910 lncount = lnindex;
911 qsort(likely_names,lncount,sizeof(struct likely_names_s),
912 likelycmp);
913 ln = likely_names;
914 baselow =ln->low;
915 basesize =ln->size;
916 baseend = ln->end;
917 for (lnindex = 1; lnindex<lncount; ++lnindex) {
918 ln = likely_names+lnindex;
919 if (ln->end > baseend) {
920 baseend = ln->end;
921 basesize = (baseend - baselow);
922 }
923 }
924 *lower = baselow;
925 *size = basesize;
926 return DW_DLV_OK;
927 }
928
929 /* So that regression testing can work better
930 we substitute '$HOME" where the string s
931 began with the value of that environment
932 variable. Otherwise, we just fill in
933 the esb with the name as it came in.
934
935 ASSERT: s non-null and points to a valid C string.
936 */
937 static void
938 homeify(char *s, struct esb_s* out)
939 {
940 char *home = getenv("HOME");
941 size_t homelen = 0;
942
943 #ifdef _WIN32
944 /* Windows In msys2
945 $HOME might be C:\msys64\home\admin
946 which messes up regression testing.
947 For msys2 with a simple setup this
948 helps regressiontesting.
949 */
950 char *winprefix = "C:/msys64/home/";
951 char *domain = getenv("USERDOMAIN");
952 char *user = getenv("USER");
953 size_t winlen = 15;
954
955 if (domain && !strcmp(domain,"MSYS")) {
956
957 if (strncmp(s,winprefix,winlen)) {
958 /* giving up, not msys2 */
959 esb_append(out,s);
960 return;
961 }
962 if (user) {
963 /* \\home\\admin
964 Change to $HOME
965 This is a crude way to get some
966 regressiontests to pass.
967 */
968 size_t userlen = strlen(user);
969 esb_append(out,"$HOME");
970 esb_append(out,s+winlen+userlen);
971 return;
972 } else {
973 /* giving up */
974 esb_append(out,s);
975 return;
976 }
977 }
978 #endif /* _WIN32 */
979 if (!home) {
980 /* giving up */
981 esb_append(out,s);
982 return;
983 }
984 homelen = strlen(home);
985 if (strlen(s) <= homelen) {
986 /* Giving up, s is shorter than $HOME alone. */
987 esb_append(out,s);
988 return;
989 }
990 /* Checking one-past the hoped-for home prefix */
991 if (homelen && s[homelen] != '/') {
992 /* giving up */
993 esb_append(out,s);
994 return;
995 }
996 if (strncmp(s,(const char *)home,homelen)) {
997 /* Giving up, the initial characters do not
998 match $HOME */
999 esb_append(out,s);
1000 return;
1001 }
1002 esb_append(out,"$HOME");
1003 /* Append, starting at the / in x */
1004 esb_append(out,s+homelen);
1005 return;
1006 }
1007
1008 /* Given a file which is an object type
1009 we think we can read, process the dwarf data. */
1010 static int
1011 process_one_file(
1012 const char * file_name,
1013 const char * tied_file_name,
1014 char * temp_path_buf,
1015 size_t temp_path_buf_len,
1016 struct dwconf_s *l_config_file_data)
1017 {
1018 Dwarf_Debug dbg = 0;
1019 Dwarf_Debug dbgtied = 0;
1020 int dres = 0;
1021 struct Dwarf_Printf_Callback_Info_s printfcallbackdata;
1022 Dwarf_Half elf_address_size = 0; /* Target pointer size */
1023 Dwarf_Error onef_err = 0;
1024 const char *title = 0;
1025 unsigned char path_source = 0;
1026 int localerrno = 0;
1027
1028 /* If using a tied file group number should be
1029 2 DW_GROUPNUMBER_DWO
1030 but in a dwp or separate-split-dwarf object then
1031 0 DW_GROUPNUMBER_ANY will find the .dwo data
1032 automatically. */
1033 {
1034 /* This will go for the real main file, whether
1035 an underlying dSYM or via debuglink or
1036 if those find nothing then the original. */
1037 char *tb = temp_path_buf;
1038 size_t tblen = temp_path_buf_len;
1039 title = "dwarf_init_path_dl fails.";
1040 if (glflags.gf_no_follow_debuglink) {
1041 tb = 0;
1042 tblen = 0;
1043 }
1044 dres = dwarf_init_path_dl_a(file_name,
1045 tb,(unsigned int)tblen,
1046 (unsigned int)glflags.group_number,
1047 (unsigned int)glflags.gf_universalnumber,
1048 NULL, NULL, &dbg,
1049 glflags.gf_global_debuglink_paths,
1050 (unsigned int)glflags.gf_global_debuglink_count,
1051 &path_source,
1052 &onef_err);
1053 }
1054 if (dres == DW_DLV_NO_ENTRY) {
1055 if (glflags.group_number > 0) {
1056 printf("No DWARF information present in %s "
1057 "for section group %d \n",
1058 file_name,glflags.group_number);
1059 } else {
1060 struct esb_s m;
1061
1062 esb_constructor(&m);
1063 homeify((char *)file_name,&m);
1064 printf("No DWARF information present in %s\n",
1065 esb_get_string(&m));
1066 esb_destructor(&m);
1067 }
1068 return dres;
1069 }
1070 if (dres == DW_DLV_ERROR) {
1071 /* Prints error, cleans up Dwarf_Error data. */
1072 print_error_and_continue(
1073 title,dres,onef_err);
1074 DROP_ERROR_INSTANCE(dbg,dres,onef_err);
1075 return DW_DLV_NO_ENTRY;
1076 }
1077 if (path_source == DW_PATHSOURCE_dsym) {
1078 struct esb_s homifiedname;
1079
1080 esb_constructor(&homifiedname);
1081 homeify(temp_path_buf,&homifiedname);
1082 printf("Filename by dSYM is %s\n",
1083 sanitized(esb_get_string(&homifiedname)));
1084 esb_destructor(&homifiedname);
1085 } else if (path_source == DW_PATHSOURCE_debuglink) {
1086 struct esb_s homifiedname;
1087
1088 esb_constructor(&homifiedname);
1089 homeify(temp_path_buf,&homifiedname);
1090 printf("Filename by debuglink is %s\n",
1091 sanitized(esb_get_string(&homifiedname)));
1092 esb_destructor(&homifiedname);
1093 glflags.gf_gnu_debuglink_flag = TRUE;
1094 } else { /* Nothing to print yet. */ }
1095 {
1096 Dwarf_Unsigned index = 0;
1097 Dwarf_Unsigned count = 0;
1098 dres = dwarf_get_universalbinary_count(dbg,&index,&count);
1099 if (dres == DW_DLV_OK) {
1100 const char * name = "object";
1101 if (count != 1) {
1102 name = "objects";
1103 }
1104 printf("This is a Mach-O Universal Binary with %"
1105 DW_PR_DUu " %s. This is object %"
1106 DW_PR_DUu "\n",
1107 count,name,index);
1108 }
1109 if (glflags.gf_machine_arch_flag) {
1110 print_machine_arch(dbg);
1111 }
1112 }
1113 if (tied_file_name && strlen(tied_file_name)) {
1114 {
1115 /* The tied file we define as group 1, BASE.
1116 Cannot follow debuglink or dSYM,
1117 is a tied file */
1118 dres = dwarf_init_path_a(tied_file_name,
1119 0,0, /* ignore dSYM & debuglink */
1120 DW_GROUPNUMBER_BASE,
1121 glflags.gf_universalnumber,
1122 0,0,
1123 &dbgtied,
1124 &onef_err);
1125 /* path_source = DW_PATHSOURCE_basic; */
1126 }
1127 if (dres == DW_DLV_NO_ENTRY) {
1128 printf("No DWARF information present in tied file: %s\n",
1129 tied_file_name);
1130 return dres;
1131 }
1132 if (dres == DW_DLV_ERROR) {
1133 /* Prints error, cleans up Dwarf_Error data.
1134 Never returns*/
1135 print_error(dbg,
1136 "dwarf_init_path on tied_file",
1137 dres, onef_err);
1138 }
1139 {
1140 Dwarf_Unsigned index = 0;
1141 Dwarf_Unsigned count = 0;
1142 dres = dwarf_get_universalbinary_count(dbg,&index,&count);
1143 if (dres == DW_DLV_OK) {
1144 const char * name = "object";
1145 if (count != 1) {
1146 name = "objects";
1147 }
1148 printf("The tied-file is a Mach-O Universal Binary "
1149 "with %"
1150 DW_PR_DUu " %s. This is object %"
1151 DW_PR_DUu "\n",
1152 count,name,index);
1153 }
1154 }
1155 }
1156
1157 memset(&printfcallbackdata,0,sizeof(printfcallbackdata));
1158 printfcallbackdata.dp_fptr = printf_callback_for_libdwarf;
1159 dwarf_register_printf_callback(dbg,&printfcallbackdata);
1160 if (dbgtied) {
1161 dwarf_register_printf_callback(dbgtied,&printfcallbackdata);
1162 }
1163 memset(&printfcallbackdata,0,sizeof(printfcallbackdata));
1164
1165 dbgsetup(dbg,l_config_file_data);
1166 dbgsetup(dbgtied,l_config_file_data);
1167 dres = get_address_size_and_max(dbg,&elf_address_size,0,
1168 &onef_err);
1169 if (dres != DW_DLV_OK) {
1170 print_error(dbg,"Unable to read address"
1171 " size so unable to continue",
1172 dres,onef_err);
1173 }
1174 if (glflags.gf_check_tag_attr ||
1175 glflags.gf_print_usage_tag_attr ||
1176 glflags.gf_check_tag_tree) {
1177 dres = dd_build_tag_attr_form_base_trees(&localerrno);
1178 if (dres != DW_DLV_OK) {
1179 simple_err_return_msg_either_action(dres,
1180 "ERROR: Failed to initialize tag/attribute/form"
1181 " tables properly");
1182 }
1183 }
1184
1185 /* Ok for dbgtied to be NULL. */
1186 dres = dwarf_set_tied_dbg(dbg,dbgtied,&onef_err);
1187 if (dres != DW_DLV_OK) {
1188 print_error(dbg, "dwarf_set_tied_dbg() failed",
1189 dres, onef_err);
1190 }
1191
1192 /* Get .text and .debug_ranges info if in check mode.
1193 Depending on the section count and layout
1194 it is possible this
1195 will not get all the sections it wants to:
1196 See calculate_likely_limits_of_code(). */
1197 if (glflags.gf_do_check_dwarf) {
1198 Dwarf_Addr lower = 0;
1199 Dwarf_Addr upper = 0;
1200 Dwarf_Unsigned size = 0;
1201 Dwarf_Debug dbg_with_code = dbg;
1202 int res = 0;
1203
1204 if (dbgtied) {
1205 /* Assuming tied is exectuable main is dwo/dwp */
1206 dbg_with_code = dbgtied;
1207 }
1208 res = calculate_likely_limits_of_code(dbg_with_code,
1209 &lower,&size);
1210 upper = lower + size;
1211 /* Set limits for Ranges Information.
1212 Some objects have CUs for startup code
1213 and the expanded range here turns out
1214 not to actually help. */
1215 if (res == DW_DLV_OK && glflags.pRangesInfo) {
1216 /* Recording high/low for .test .init .fini */
1217 SetLimitsBucketGroup(glflags.pRangesInfo,lower,upper);
1218 AddEntryIntoBucketGroup(glflags.pRangesInfo,
1219 1,
1220 lower,lower,
1221 upper,
1222 ".text",
1223 TRUE);
1224 }
1225 /* Build section information for linkonce.
1226 linkonce is related to, for example,
1227 gcc .gnu.linkonce.?? sections we have no reason to think
1228 this really works, we have no test cases as of 2022. */
1229 build_linkonce_info(dbg);
1230 }
1231 if (glflags.gf_section_groups_flag) {
1232 int res = 0;
1233 Dwarf_Error err = 0;
1234
1235 res = print_section_groups_data(dbg,&err);
1236 if (res == DW_DLV_ERROR) {
1237 print_error_and_continue(
1238 "printing section groups had a problem.",
1239 res,err);
1240 DROP_ERROR_INSTANCE(dbg,res,err);
1241 }
1242 /* If groupnum > 2 this turns off some
1243 of the gf_flags here so we don't print
1244 section names of things we do not
1245 want to print. */
1246 update_section_flags_per_groups();
1247 }
1248 reset_overall_CU_error_data();
1249 if (glflags.gf_info_flag || glflags.gf_line_flag ||
1250 glflags.gf_types_flag ||
1251 glflags.gf_check_macros || glflags.gf_macinfo_flag ||
1252 glflags.gf_macro_flag ||
1253 glflags.gf_cu_name_flag || glflags.gf_search_is_on ||
1254 glflags.gf_producer_children_flag) {
1255 Dwarf_Error err = 0;
1256 int res = 0;
1257
1258 reset_overall_CU_error_data();
1259 res = print_infos(dbg,TRUE,&err);
1260 if (res == DW_DLV_ERROR) {
1261 print_error_and_continue(
1262 "printing .debug_info had a problem.",
1263 res,err);
1264 DROP_ERROR_INSTANCE(dbg,res,err);
1265 }
1266 reset_overall_CU_error_data();
1267 res = print_infos(dbg,FALSE,&err);
1268 if (res == DW_DLV_ERROR) {
1269 print_error_and_continue(
1270 "printing .debug_types had a problem.",
1271 res,err);
1272 DROP_ERROR_INSTANCE(dbg,res,err);
1273 }
1274 {
1275 set_global_section_sizes(dbg);
1276 /* The statistics are for ALL of the
1277 DWARF5 (and DWARF4 with .debug_macro)
1278 across all CUs. */
1279 if (macro_check_tree) {
1280 /* debug_macro_size is to check the section end */
1281 print_macrocheck_statistics("DWARF5 .debug_macro",
1282 &macro_check_tree,
1283 /* DWARF5 */ TRUE,
1284 glflags.section_high_offsets_global->
1285 debug_macro_size);
1286 }
1287 }
1288 if (glflags.gf_check_macros) {
1289 if (macinfo_check_tree) {
1290 /* debug_macinfo_size is to check the section end */
1291 print_macrocheck_statistics("DWARF2 .debug_macinfo",
1292 &macinfo_check_tree,
1293 /* DWARF5 */ FALSE,
1294 glflags.section_high_offsets_global->
1295 debug_macinfo_size);
1296 }
1297 }
1298 clear_macrocheck_statistics(&macro_check_tree);
1299 clear_macrocheck_statistics(&macinfo_check_tree);
1300 }
1301 if (glflags.gf_gdbindex_flag) {
1302 int res = 0;
1303 Dwarf_Error err = 0;
1304
1305 reset_overall_CU_error_data();
1306 /* By definition if gdb_index is present
1307 then "cu" and "tu" will not be. And vice versa. */
1308 res = print_gdb_index(dbg,&err);
1309 if (res == DW_DLV_ERROR) {
1310 print_error_and_continue(
1311 "printing the gdb index section had a problem "
1312 ,res,err);
1313 }
1314 res = print_debugfission_index(dbg,"cu",&err);
1315 if (res == DW_DLV_ERROR) {
1316 print_error_and_continue(
1317 "printing the debugfission cu section "
1318 "had a problem "
1319 ,res,err);
1320 }
1321 res = print_debugfission_index(dbg,"tu",&err);
1322 if (res == DW_DLV_ERROR) {
1323 print_error_and_continue(
1324 "printing the debugfission tu section "
1325 "had a problem "
1326 ,res,err);
1327 }
1328 }
1329 if (glflags.gf_pubnames_flag) {
1330 int res = 0;
1331 Dwarf_Error err = 0;
1332
1333 reset_overall_CU_error_data();
1334 res = print_pubnames_style(dbg,DW_GL_GLOBALS,&err);
1335 if (res == DW_DLV_ERROR) {
1336 print_error_and_continue(
1337 "printing pubnames data had a problem ",res,err);
1338 DROP_ERROR_INSTANCE(dbg,res,err);
1339 }
1340 }
1341 if (glflags.gf_debug_addr_flag) {
1342 Dwarf_Error err = 0;
1343 int res = 0;
1344
1345 reset_overall_CU_error_data();
1346 res = print_debug_addr(dbg,&err);
1347 if (res == DW_DLV_ERROR) {
1348 print_error_and_continue(
1349 "printing the .debug_addr section"
1350 " had a problem.",res,err);
1351 DROP_ERROR_INSTANCE(dbg,res,err);
1352 }
1353 }
1354 if (glflags.gf_abbrev_flag) {
1355 Dwarf_Error err = 0;
1356 int res = 0;
1357
1358 reset_overall_CU_error_data();
1359 res = print_abbrevs(dbg,&err);
1360 if (res == DW_DLV_ERROR) {
1361 print_error_and_continue(
1362 "printing the .debug_abbrev section"
1363 " had a problem.",res,err);
1364 DROP_ERROR_INSTANCE(dbg,res,err);
1365 }
1366 }
1367 if (glflags.gf_string_flag) {
1368 Dwarf_Error err = 0;
1369 int res = 0;
1370
1371 reset_overall_CU_error_data();
1372 res = print_strings(dbg,&err);
1373 if (res == DW_DLV_ERROR) {
1374 print_error_and_continue(
1375 "printing the .debug_str section"
1376 " had a problem.",res,err);
1377 DROP_ERROR_INSTANCE(dbg,res,err);
1378 }
1379 }
1380 if (glflags.gf_aranges_flag) {
1381 Dwarf_Error err = 0;
1382 int res = 0;
1383
1384 reset_overall_CU_error_data();
1385 res = print_aranges(dbg,&err);
1386 if (res == DW_DLV_ERROR) {
1387 print_error_and_continue(
1388 "printing the aranges section"
1389 " had a problem.",res,err);
1390 DROP_ERROR_INSTANCE(dbg,res,err);
1391 }
1392 }
1393 if (glflags.gf_ranges_flag) {
1394 reset_overall_CU_error_data();
1395 print_ranges(dbg);
1396 /* Never returns DW_DLV_ERROR */
1397 }
1398 if (glflags.gf_print_raw_loclists) {
1399 int res = 0;
1400 Dwarf_Error err = 0;
1401 /* This for DWARF5 loclists.
1402 No raw printing of .debug_loc available. */
1403
1404 reset_overall_CU_error_data();
1405 res = print_raw_all_loclists(dbg,&err);
1406 if (res == DW_DLV_ERROR) {
1407 print_error_and_continue(
1408 "printing the raw .debug_loclists section"
1409 " had a problem.",res,err);
1410 DROP_ERROR_INSTANCE(dbg,res,err);
1411 }
1412 }
1413 if (glflags.gf_print_raw_rnglists &&
1414 glflags.gf_do_print_dwarf) {
1415
1416 int res = 0;
1417 Dwarf_Error err = 0;
1418
1419 reset_overall_CU_error_data();
1420 res = print_raw_all_rnglists(dbg,&err);
1421 if (res == DW_DLV_ERROR) {
1422 print_error_and_continue(
1423 "printing the raw .debug_rnglists section"
1424 " had a problem.",res,err);
1425 DROP_ERROR_INSTANCE(dbg,res,err);
1426 }
1427 }
1428 if (glflags.gf_frame_flag || glflags.gf_eh_frame_flag) {
1429 int sres = 0;
1430 Dwarf_Error err = 0;
1431 int want_eh = 0;
1432 /* These three shared .eh_frame and .debug_frame
1433 as they are about the DIEs, not about frames. */
1434 Dwarf_Die cu_die_for_print_frames = 0;
1435 void *map_lowpc_to_name = 0;
1436 void *lowpcSet = 0;
1437
1438 reset_overall_CU_error_data();
1439 if (glflags.gf_frame_flag) {
1440 want_eh = 0;
1441 sres = print_frames(dbg,want_eh,
1442 l_config_file_data,
1443 &cu_die_for_print_frames,
1444 &map_lowpc_to_name,
1445 &lowpcSet,
1446 &err);
1447 if (sres == DW_DLV_ERROR) {
1448 print_error_and_continue(
1449 "printing standard frame data had a problem.",
1450 sres,err);
1451 DROP_ERROR_INSTANCE(dbg,sres,err);
1452 }
1453 }
1454 if (glflags.gf_eh_frame_flag) {
1455 want_eh = 1;
1456 sres = print_frames(dbg, want_eh,
1457 l_config_file_data,
1458 &cu_die_for_print_frames,
1459 &map_lowpc_to_name,
1460 &lowpcSet,
1461 &err);
1462 if (sres == DW_DLV_ERROR) {
1463 print_error_and_continue(
1464 "printing eh frame data had a problem.",sres,
1465 err);
1466 DROP_ERROR_INSTANCE(dbg,sres,err);
1467 }
1468 }
1469 addr_map_destroy(lowpcSet);
1470 addr_map_destroy(map_lowpc_to_name);
1471 if (cu_die_for_print_frames) {
1472 dwarf_dealloc_die(cu_die_for_print_frames);
1473 }
1474 }
1475 if (glflags.gf_static_func_flag) {
1476 int sres = 0;
1477 Dwarf_Error err = 0;
1478
1479 reset_overall_CU_error_data();
1480 sres = print_pubnames_style(dbg,DW_GL_FUNCS,&err);
1481 if (sres == DW_DLV_ERROR) {
1482 print_error_and_continue(
1483 "printing SGI static funcs had a problem.",sres,err);
1484 DROP_ERROR_INSTANCE(dbg,sres,err);
1485 }
1486 }
1487 if (glflags.gf_static_var_flag) {
1488 int sres = 0;
1489 Dwarf_Error err = 0;
1490
1491 reset_overall_CU_error_data();
1492 sres = print_pubnames_style(dbg,DW_GL_VARS,&err);
1493 if (sres == DW_DLV_ERROR) {
1494 print_error_and_continue(
1495 "printing SGI static vars had a problem.",sres,err);
1496 DROP_ERROR_INSTANCE(dbg,sres,err);
1497 }
1498 }
1499 /* DWARF_PUBTYPES is the standard typenames dwarf section.
1500 SGI_TYPENAME is the same concept but is SGI specific ( it was
1501 defined 10 years before dwarf pubtypes). */
1502
1503 if (glflags.gf_pubtypes_flag) {
1504 Dwarf_Error err = 0;
1505 int tres = 0;
1506
1507 reset_overall_CU_error_data();
1508 tres = print_pubnames_style(dbg,DW_GL_PUBTYPES,&err);
1509 if (tres == DW_DLV_ERROR) {
1510 print_error_and_continue(
1511 "printing pubtypes had a problem.",tres,err);
1512 DROP_ERROR_INSTANCE(dbg,tres,err);
1513 }
1514 reset_overall_CU_error_data();
1515 tres = print_pubnames_style(dbg,DW_GL_TYPES,&err);
1516 if (tres == DW_DLV_ERROR) {
1517 print_error_and_continue(
1518 "printing SGI typenames had a problem.",tres,err);
1519 DROP_ERROR_INSTANCE(dbg,tres,err);
1520 }
1521 }
1522 if (glflags.gf_weakname_flag) {
1523 Dwarf_Error err = 0;
1524 int res3 = 0;
1525
1526 reset_overall_CU_error_data();
1527 res3 = print_pubnames_style(dbg,DW_GL_WEAKS,&err);
1528 if (res3 == DW_DLV_ERROR) {
1529 print_error_and_continue(
1530 "printing weaknames had a problem.",res3,err);
1531 DROP_ERROR_INSTANCE(dbg,res3,err);
1532 }
1533 }
1534 if (glflags.gf_reloc_flag) {
1535 /* Harmless, though likely not needed here. */
1536 reset_overall_CU_error_data();
1537 }
1538 if (glflags.gf_debug_names_flag) {
1539 int nres = 0;
1540 Dwarf_Error err = 0;
1541 reset_overall_CU_error_data();
1542 nres = print_debug_names(dbg,&err);
1543 if (nres == DW_DLV_ERROR) {
1544 print_error_and_continue(
1545 "print .debug_names section failed", nres, err);
1546 DROP_ERROR_INSTANCE(dbg,nres,err);
1547 }
1548 }
1549
1550 /* Print search results */
1551 if (glflags.gf_search_print_results && glflags.gf_search_is_on) {
1552 /* No dwarf errors possible in this function. */
1553 print_search_results();
1554 }
1555
1556 /* The right time to do this is unclear. But we need to do it. */
1557 if (glflags.gf_check_harmless) {
1558 /* No dwarf errors possible in this function. */
1559 print_any_harmless_errors(dbg);
1560 }
1561
1562 /* Print error report only if errors have been detected
1563 Print error report if the -kd option.
1564 No errors possible in this function. */
1565 print_checks_results();
1566
1567 /* Print the detailed attribute usage space
1568 and free the attributes_encoding data allocated.
1569 Option -kE
1570 Also prints the attr/formclass/form reports
1571 from attr_form.c See build_attr_form_base()
1572 call above and record_attr_form_use() in print_die.c */
1573 if (glflags.gf_check_attr_encoding ) {
1574 int ares = 0;
1575 Dwarf_Error aerr = 0;
1576
1577 ares = print_attributes_encoding(dbg,&aerr);
1578 if (ares == DW_DLV_ERROR) {
1579 print_error_and_continue(
1580 "print attributes encoding failed", ares, aerr);
1581 DROP_ERROR_INSTANCE(dbg,ares,aerr);
1582 }
1583 }
1584
1585 /* Print the tags and attribute usage -ku or -kuf */
1586 if (glflags.gf_print_usage_tag_attr) {
1587 int tres = 0;
1588 Dwarf_Error err = 0;
1589
1590 tres = print_tag_attributes_usage();
1591 if (tres == DW_DLV_ERROR) {
1592 print_error_and_continue(
1593 "print tag attributes usage failed", tres, err);
1594 DROP_ERROR_INSTANCE(dbg,tres,err);
1595 }
1596 }
1597
1598 if (glflags.gf_print_str_offsets) {
1599 /* print the .debug_str_offsets section, if any. */
1600 int lres = 0;
1601 Dwarf_Error err = 0;
1602
1603 lres = print_str_offsets_section(dbg,&err);
1604 if (lres == DW_DLV_ERROR) {
1605 print_error_and_continue(
1606 "print .debug_str_offsets failed", lres, err);
1607 DROP_ERROR_INSTANCE(dbg,lres,err);
1608 }
1609 }
1610
1611 /* prints nothing unless section .gnu_debuglink is present.
1612 Lets print for a few critical sections. */
1613 if (glflags.gf_gnu_debuglink_flag) {
1614 int lresdl = 0;
1615 Dwarf_Error err = 0;
1616
1617 lresdl = print_gnu_debuglink(dbg,&err);
1618 if (lresdl == DW_DLV_ERROR) {
1619 print_error_and_continue(
1620 "print gnu_debuglink data failed", lresdl, err);
1621 DROP_ERROR_INSTANCE(dbg,lresdl,err);
1622 err = 0;
1623 }
1624 }
1625 if (glflags.gf_debug_gnu_flag) {
1626 int lres = 0;
1627 Dwarf_Error err = 0;
1628
1629 lres = print_debug_gnu(dbg,&err);
1630 if (lres == DW_DLV_ERROR) {
1631 print_error_and_continue(
1632 "print .debug_gnu* section failed", lres, err);
1633 DROP_ERROR_INSTANCE(dbg,lres,err);
1634 err = 0;
1635 }
1636 }
1637 if (glflags.gf_debug_sup_flag) {
1638 int lres = 0;
1639 Dwarf_Error err = 0;
1640
1641 lres = print_debug_sup(dbg,&err);
1642 if (lres == DW_DLV_ERROR) {
1643 print_error_and_continue(
1644 "print .debug_sup* section failed", lres, err);
1645 DROP_ERROR_INSTANCE(dbg,lres,err);
1646 }
1647 }
1648 if (glflags.gf_debug_addr_missing) {
1649 printf("\nERROR: At some point "
1650 "the .debug_addr section was needed but missing, "
1651 "meaning some frame information was missing "
1652 "relevant function names. See the dwarfdump "
1653 " option --file-tied=</path/to/executable>.");
1654 glflags.gf_count_major_errors++;
1655 }
1656 if (glflags.gf_error_code_search_by_address) {
1657 printf("\nERROR: At some point "
1658 "There was some data corruption in frame data "
1659 "so at least the following error occurred: "
1660 "%s .\n",
1661 dwarf_errmsg_by_number(
1662 glflags.gf_error_code_search_by_address));
1663 glflags.gf_count_major_errors++;
1664 }
1665
1666 /* Could finish dbg first. Either order ok. */
1667 if (dbgtied) {
1668 dres = dwarf_finish(dbgtied);
1669 if (dres != DW_DLV_OK) {
1670 print_error_and_continue(
1671 "dwarf_finish failed on tied dbg", dres, onef_err);
1672 DROP_ERROR_INSTANCE(dbg,dres,onef_err);
1673 }
1674 dbgtied = 0;
1675 }
1676 groups_restore_subsidiary_flags();
1677 dres = dwarf_finish(dbg);
1678 if (dres != DW_DLV_OK) {
1679 print_error_and_continue(
1680 "dwarf_finish failed", dres, onef_err);
1681 DROP_ERROR_INSTANCE(dbg,dres,onef_err);
1682 dbg = 0;
1683 }
1684 printf("\n");
1685 dd_destroy_tag_attr_form_trees();
1686 destruct_abbrev_array();
1687 esb_close_null_device();
1688 release_range_array_info();
1689 helpertree_clear_statistics(&helpertree_offsets_base_info);
1690 helpertree_clear_statistics(&helpertree_offsets_base_types);
1691 return 0;
1692 }
1693
1694 /* ==============START of dwarfdump error print functions. */
1695 int
1696 simple_err_return_msg_either_action(int res,const char *msg)
1697 {
1698 const char *etype = "No-entry";
1699 if (res == DW_DLV_ERROR) {
1700 etype="Major error";
1701 }
1702 glflags.gf_count_major_errors++;
1703 printf("%s fails. %s\n",msg,etype);
1704 return res;
1705 }
1706 int
1707 simple_err_return_action(int res,const char *msg)
1708 {
1709 if (res == DW_DLV_ERROR) {
1710 const char *etype = "Major error";
1711 glflags.gf_count_major_errors++;
1712 printf("%s %s\n",msg, etype);
1713 }
1714 return res;
1715 }
1716
1717 int
1718 simple_err_only_return_action(int res,const char *msg)
1719 {
1720 const char *etype="Major error";
1721 /*const char *msg = "\nERROR: dwarf_get_address_size() fails."; */
1722
1723 glflags.gf_count_major_errors++;
1724 printf("%s %s\n",msg,etype);
1725 return res;
1726 }
1727
1728 /* Does not increment glflags.gf_count_major_errors
1729 unless a return code is not one of the three
1730 standard values.
1731 */
1732 static void
1733 print_error_maybe_continue( const char * msg,
1734 int dwarf_ret_val,
1735 Dwarf_Error lerr,
1736 Dwarf_Bool do_continue)
1737 {
1738 unsigned long realmajorerr = glflags.gf_count_major_errors;
1739 printf("\n");
1740 if (dwarf_ret_val == DW_DLV_ERROR) {
1741 /* We do not dwarf_dealloc the error here. */
1742 char * errmsg = dwarf_errmsg(lerr);
1743
1744 /* We now (April 2016) guarantee the
1745 error number is in
1746 the error string so we do not need to print
1747 the dwarf_errno() value to show the number. */
1748 if (do_continue) {
1749 printf(
1750 "%s ERROR: %s: %s. "
1751 "Attempting to continue.\n",
1752 glflags.program_name, msg, errmsg);
1753 } else {
1754 printf( "%s ERROR: %s: %s\n",
1755 glflags.program_name, msg, errmsg);
1756 }
1757 } else if (dwarf_ret_val == DW_DLV_NO_ENTRY) {
1758 printf("%s NO ENTRY: %s: \n",
1759 glflags.program_name, msg);
1760 } else if (dwarf_ret_val == DW_DLV_OK) {
1761 printf("%s: %s \n", glflags.program_name, msg);
1762 } else {
1763 printf("%s ERROR InternalError: %s: code %d\n",
1764 glflags.program_name, msg, dwarf_ret_val);
1765 ++realmajorerr;
1766 }
1767 /* Display compile unit name */
1768 PRINT_CU_INFO();
1769 glflags.gf_count_major_errors = realmajorerr;
1770 }
1771
1772 void
1773 print_error(Dwarf_Debug dbg,
1774 const char * msg,
1775 int dwarf_ret_val,
1776 Dwarf_Error lerr)
1777 {
1778 print_error_maybe_continue(msg,dwarf_ret_val,lerr,FALSE);
1779 glflags.gf_count_major_errors++;
1780 if (dwarf_ret_val == DW_DLV_ERROR) {
1781 /* If dbg was never initialized
1782 this still cleans up the Error data. */
1783 DROP_ERROR_INSTANCE(dbg,dwarf_ret_val,lerr);
1784 dwarf_finish(dbg);
1785 check_for_major_errors();
1786 check_for_notes();
1787 }
1788 global_destructors();
1789 flag_data_post_cleanup();
1790 dd_destroy_tag_attr_form_trees();
1791 exit(EXIT_FAILURE);
1792 }
1793 /* ARGSUSED */
1794 void
1795 print_error_and_continue(const char * msg,
1796 int dwarf_ret_val,
1797 Dwarf_Error lerr)
1798 {
1799 glflags.gf_count_major_errors++;
1800 print_error_maybe_continue(msg,dwarf_ret_val,lerr,TRUE);
1801 }
1802 /* ==============END of dwarfdump error print functions. */
1803
1804 static Dwarf_Bool
1805 is_a_string_form(int sf)
1806 {
1807 switch(sf){
1808 case DW_FORM_string:
1809 case DW_FORM_GNU_strp_alt:
1810 case DW_FORM_strp_sup:
1811 case DW_FORM_GNU_str_index:
1812 case DW_FORM_strx:
1813 case DW_FORM_strx1:
1814 case DW_FORM_strx2:
1815 case DW_FORM_strx3:
1816 case DW_FORM_strx4:
1817 case DW_FORM_strp:
1818 case DW_FORM_line_strp:
1819 /* There is some hope we can actually get
1820 the string itself, depending on
1821 other factors */
1822 return TRUE;
1823 default: break;
1824 }
1825 /* Nope. No string is possible */
1826 return FALSE;
1827 }
1828 /* Always sets the return argument *should_skip,
1829 whether it returns DW_DLV_NO_ENTRY or
1830 DW_DLV_ERROR or DW_DLV_OK.
1831 determines if the CU should be
1832 skipped as the DW_AT_name of the CU
1833 does not match the command-line-supplied
1834 cu name. The two callers ignore the
1835 return value.
1836 This suppresses any errors it finds, no
1837 Dwarf_Error is lost and none is returned. */
1838 int
1839 should_skip_this_cu(Dwarf_Debug dbg, Dwarf_Bool*should_skip,
1840 Dwarf_Die cu_die)
1841 {
1842 Dwarf_Half tag = 0;
1843 Dwarf_Attribute attrib = 0;
1844 Dwarf_Half theform = 0;
1845 Dwarf_Error skperr;
1846 int dares = 0;
1847 int tres = 0;
1848 int fres = 0;
1849
1850 tres = dwarf_tag(cu_die, &tag, &skperr);
1851 if (tres != DW_DLV_OK) {
1852 print_error_and_continue("ERROR: "
1853 "Cannot get the TAG of the cu_die to check "
1854 " if we should skip this CU or not.",
1855 tres, skperr);
1856 *should_skip = FALSE;
1857 DROP_ERROR_INSTANCE(dbg,tres,skperr);
1858 return tres;
1859 }
1860 dares = dwarf_attr(cu_die, DW_AT_name, &attrib, &skperr);
1861 if (dares != DW_DLV_OK) {
1862 print_error_and_continue("should skip this cu? "
1863 " cu die has no DW_AT_name attribute!",
1864 dares, skperr);
1865 *should_skip = FALSE;
1866 DROP_ERROR_INSTANCE(dbg,dares,skperr);
1867 return dares;
1868 }
1869 fres = dwarf_whatform(attrib, &theform, &skperr);
1870 if (fres == DW_DLV_OK) {
1871 if (is_a_string_form(theform)) {
1872 char * temps = 0;
1873 int sres = dwarf_formstring(attrib, &temps,
1874 &skperr);
1875 if (sres == DW_DLV_OK) {
1876 char *lcun = esb_get_string(glflags.cu_name);
1877 char *p = temps;
1878 if (lcun[0] != '/') {
1879 p = strrchr(temps, '/');
1880 if (p == NULL) {
1881 p = temps;
1882 } else {
1883 p++;
1884 }
1885 }
1886 /* Ignore case if Windows */
1887 #if _WIN32
1888 if (stricmp(lcun, p)) {
1889 /* skip this cu. */
1890 dwarf_dealloc_attribute(attrib);
1891 attrib = 0;
1892 *should_skip = TRUE;
1893 return DW_DLV_OK;
1894 }
1895 #else
1896 if (strcmp(lcun, p)) {
1897 /* skip this cu. */
1898 dwarf_dealloc_attribute(attrib);
1899 attrib = 0;
1900 *should_skip = TRUE;
1901 return DW_DLV_OK;
1902 }
1903 #endif /* _WIN32 */
1904
1905 } else if (sres == DW_DLV_ERROR) {
1906 struct esb_s m;
1907 int dwarf_names_print_on_error = 1;
1908
1909 dwarf_dealloc_attribute(attrib);
1910 attrib = 0;
1911 esb_constructor(&m);
1912 esb_append(&m,"In determining if we should "
1913 "skip this CU dwarf_formstring "
1914 "gets an error on form ");
1915 esb_append(&m,get_FORM_name(theform,
1916 dwarf_names_print_on_error));
1917 esb_append(&m,".");
1918
1919 print_error_and_continue(
1920 esb_get_string(&m),
1921 sres, skperr);
1922 *should_skip = FALSE;
1923 esb_destructor(&m);
1924 return sres;
1925 } else {
1926 /* DW_DLV_NO_ENTRY on the string itself */
1927 dwarf_dealloc_attribute(attrib);
1928 attrib = 0;
1929 *should_skip = FALSE;
1930 return sres;
1931 }
1932 }
1933 } else if (fres == DW_DLV_ERROR) {
1934 /* DW_DLV_ERROR */
1935 print_error_and_continue(
1936 "dwarf_whatform failed on a CU_die when"
1937 " attempting to determine if this CU should"
1938 " be skipped.",
1939 fres, skperr);
1940 DROP_ERROR_INSTANCE(dbg,fres,skperr);
1941 fres = DW_DLV_NO_ENTRY;
1942 } else {
1943 /* DW_DLV_NO_ENTRY, nothing to print */
1944 }
1945 dwarf_dealloc_attribute(attrib);
1946 attrib = 0;
1947 *should_skip = FALSE;
1948 return fres;
1949 }
1950
1951 /* Returns the cu of the CUn the name fields when it can,
1952 else a no-entry
1953 else DW_DLV_ERROR. */
1954 int
1955 get_cu_name(Dwarf_Debug dbg, Dwarf_Die cu_die,
1956 Dwarf_Off dieprint_cu_offset,
1957 char * *short_name, char * *long_name,
1958 Dwarf_Error *lerr)
1959 {
1960 Dwarf_Attribute name_attr = 0;
1961 int ares = 0;
1962
1963 ares = dwarf_attr(cu_die, DW_AT_name, &name_attr, lerr);
1964 if (ares == DW_DLV_ERROR) {
1965 print_error_and_continue(
1966 "dwarf_attr fails on DW_AT_name on the CU die",
1967 ares, *lerr);
1968 return ares;
1969 } else if (ares == DW_DLV_NO_ENTRY) {
1970 *short_name = "<unknown name>";
1971 *long_name = "<unknown name>";
1972 } else {
1973 /* DW_DLV_OK */
1974 /* The string return is valid until the next call to this
1975 function; so if the caller needs to keep the returned
1976 string, the string must be copied (makename()). */
1977 char *filename = 0;
1978
1979 esb_empty_string(&esb_long_cu_name);
1980 ares = get_attr_value(dbg, DW_TAG_compile_unit,
1981 cu_die, dieprint_cu_offset,
1982 name_attr, NULL, 0, &esb_long_cu_name,
1983 0 /*show_form_used*/,0 /* verbose */,lerr);
1984 if (ares != DW_DLV_OK) {
1985 *short_name = "<unknown name>";
1986 *long_name = "<unknown name>";
1987 dwarf_dealloc_attribute(name_attr);
1988 return ares;
1989 }
1990 *long_name = esb_get_string(&esb_long_cu_name);
1991 /* Generate the short name (filename) */
1992 filename = strrchr(*long_name,'/');
1993 if (!filename) {
1994 filename = strrchr(*long_name,'\\');
1995 }
1996 if (filename) {
1997 ++filename;
1998 } else {
1999 filename = *long_name;
2000 }
2001 esb_empty_string(&esb_short_cu_name);
2002 esb_append(&esb_short_cu_name,filename);
2003 *short_name = esb_get_string(&esb_short_cu_name);
2004 dwarf_dealloc_attribute(name_attr);
2005 }
2006 return ares;
2007 }
2008
2009 /* Returns the producer of the CU
2010 Caller must ensure producernameout is
2011 a valid, constructed, empty esb_s instance before calling.
2012 */
2013 int
2014 get_producer_name(Dwarf_Debug dbg, Dwarf_Die cu_die,
2015 Dwarf_Off dieprint_cu_offset,
2016 struct esb_s *producernameout,
2017 Dwarf_Error *err)
2018 {
2019 Dwarf_Attribute producer_attr = 0;
2020 int ares = 0;
2021 /* See also glflags.c for "<unknown>" as default producer
2022 string */
2023
2024 if (!cu_die) {
2025 glflags.gf_count_major_errors++;
2026 esb_append(producernameout,
2027 "\"<ERROR: CU-missing-DW_AT_producer (null cu_die)>\"");
2028 return DW_DLV_NO_ENTRY;
2029 }
2030 ares = dwarf_attr(cu_die, DW_AT_producer,
2031 &producer_attr, err);
2032 if (ares == DW_DLV_ERROR) {
2033 glflags.gf_count_major_errors++;
2034 esb_append(producernameout,
2035 "\"<ERROR: CU-DW_AT_producer-error>\"");
2036 return ares;
2037 }
2038 if (ares == DW_DLV_NO_ENTRY) {
2039 /* We add extra quotes so it looks more like
2040 the names for real producers that get_attr_value
2041 produces. */
2042 /* Same string is in glflags.c */
2043 esb_append(producernameout,
2044 "\"<ERROR: CU-missing-DW_AT_producer>\"");
2045 dwarf_dealloc_attribute(producer_attr);
2046 return ares;
2047 }
2048 /* DW_DLV_OK */
2049 ares = get_attr_value(dbg, DW_TAG_compile_unit,
2050 cu_die, dieprint_cu_offset,
2051 producer_attr, NULL, 0, producernameout,
2052 0 /*show_form_used*/,0 /* verbose */,err);
2053 dwarf_dealloc_attribute(producer_attr);
2054 return ares;
2055 }
2056
2057 void
2058 print_secname(Dwarf_Debug dbg,const char *secname)
2059 {
2060 if (glflags.gf_do_print_dwarf) {
2061 struct esb_s truename;
2062 char buf[DWARF_SECNAME_BUFFER_SIZE];
2063
2064 esb_constructor_fixed(&truename,buf,sizeof(buf));
2065 get_true_section_name(dbg,secname,
2066 &truename,TRUE);
2067 printf("\n%s\n",sanitized(esb_get_string(&truename)));
2068 esb_destructor(&truename);
2069 }
2070 }
2071
2072 /* We'll check for errors when checking.
2073 print only if printing (as opposed to checking). */
2074 static int
2075 print_gnu_debuglink(Dwarf_Debug dbg, Dwarf_Error *error)
2076 {
2077 int res = 0;
2078 char * name = 0;
2079 unsigned char *crcbytes = 0;
2080 char * link_path = 0;
2081 unsigned link_path_len = 0;
2082 unsigned buildidtype = 0;
2083 char *buildidowner = 0;
2084 unsigned char *buildidbyteptr = 0;
2085 unsigned buildidlength = 0;
2086 char **paths_array = 0;
2087 unsigned paths_array_length = 0;
2088
2089 res = dwarf_gnu_debuglink(dbg,
2090 &name,
2091 &crcbytes,
2092 &link_path, /* free this */
2093 &link_path_len,
2094 &buildidtype,
2095 &buildidowner,
2096 &buildidbyteptr, &buildidlength,
2097 &paths_array, /* free this */
2098 &paths_array_length,
2099 error);
2100 if (res == DW_DLV_NO_ENTRY) {
2101 return res;
2102 }
2103 if (res == DW_DLV_ERROR) {
2104 print_secname(dbg,".gnu_debuglink");
2105 return res;
2106 }
2107 if (crcbytes) {
2108 print_secname(dbg,".gnu_debuglink");
2109 /* Done with error checking, so print if we are printing. */
2110 if (glflags.gf_do_print_dwarf) {
2111 printf(" Debuglink name : %s",sanitized(name));
2112 {
2113 unsigned char *crc = 0;
2114 unsigned char *end = 0;
2115
2116 crc = crcbytes;
2117 end = crcbytes +4;
2118 printf(" crc 0X: ");
2119 for (; crc < end; crc++) {
2120 printf("%02x ", *crc);
2121 }
2122 }
2123 printf("\n");
2124 if (link_path_len) {
2125 printf(" Debuglink target: %s\n",
2126 sanitized(link_path));
2127 }
2128 }
2129 }
2130 if (buildidlength) {
2131 print_secname(dbg,".note.gnu.build-id");
2132 if (glflags.gf_do_print_dwarf) {
2133 printf(" Build-id type : %u\n", buildidtype);
2134 printf(" Build-id ownername: %s\n",
2135 sanitized(buildidowner));
2136 printf(" Build-id length : %u\n",buildidlength);
2137 printf(" Build-id : ");
2138 {
2139 const unsigned char *cur = 0;
2140 const unsigned char *end = 0;
2141
2142 cur = buildidbyteptr;
2143 end = cur + buildidlength;
2144 for (; cur < end; cur++) {
2145 printf("%02x", (unsigned char)*cur);
2146 }
2147 }
2148 printf("\n");
2149 }
2150 }
2151 if (paths_array_length) {
2152 unsigned i = 0;
2153
2154 printf(" Possible "
2155 ".gnu_debuglink/.note.gnu.build-id pathnames for\n");
2156 printf(" an alternate object file with more detailed "
2157 "DWARF\n");
2158 for ( ; i < paths_array_length; ++i) {
2159 char *path = paths_array[i];
2160 char outpath[2000];
2161 unsigned long outpathlen = sizeof(outpath);
2162 unsigned int ftype = 0;
2163 unsigned int endian = 0;
2164 unsigned int offsetsize = 0;
2165 Dwarf_Unsigned filesize = 0;
2166 /* See DW_PATHSOURCE_dsym for what
2167 pathsource might be set to in the
2168 call below. */
2169 unsigned char pathsource = 0;
2170 int errcode = 0;
2171
2172 printf(" [%u] %s\n",i,sanitized(path));
2173 res = dwarf_object_detector_path_b(path,
2174 outpath,outpathlen,
2175 0,0,
2176 &ftype,&endian,&offsetsize,
2177 &filesize, &pathsource, &errcode);
2178 if (res == DW_DLV_NO_ENTRY) {
2179 if (glflags.verbose) {
2180 printf(" file above does not exist\n");
2181 }
2182 continue;
2183 }
2184 if (res == DW_DLV_ERROR) {
2185 printf(" access attempt of the above leads"
2186 " to error %s\n",
2187 dwarf_errmsg_by_number(errcode));
2188 continue;
2189 }
2190 switch(ftype) {
2191 case DW_FTYPE_ELF:
2192 printf(" file above is an Elf object\n");
2193 break;
2194 case DW_FTYPE_MACH_O:
2195 printf(" file above is a Mach-O object\n");
2196 break;
2197 case DW_FTYPE_PE:
2198 printf(" file above is a PE object");
2199 break;
2200 case DW_FTYPE_ARCHIVE:
2201 if (glflags.verbose) {
2202 printf(" file above is an archive "
2203 "so ignore it.\n");
2204 }
2205 continue;
2206 default:
2207 if (glflags.verbose) {
2208 printf(" file above is not"
2209 " any object type we recognize\n");
2210 }
2211 continue;
2212 }
2213 }
2214 printf("\n");
2215 }
2216 free(link_path);
2217 free(paths_array);
2218 return DW_DLV_OK;
2219 }
2220
2221 /* GCC linkonce names */
2222 char *lo_text = ".text."; /*".gnu.linkonce.t.";*/
2223 char *lo_debug_abbr = ".gnu.linkonce.wa.";
2224 char *lo_debug_aranges = ".gnu.linkonce.wr.";
2225 char *lo_debug_frame_1 = ".gnu.linkonce.wf.";
2226 char *lo_debug_frame_2 = ".gnu.linkonce.wF.";
2227 char *lo_debug_info = ".gnu.linkonce.wi.";
2228 char *lo_debug_line = ".gnu.linkonce.wl.";
2229 char *lo_debug_macinfo = ".gnu.linkonce.wm.";
2230 char *lo_debug_loc = ".gnu.linkonce.wo.";
2231 char *lo_debug_pubnames = ".gnu.linkonce.wp.";
2232 char *lo_debug_ranges = ".gnu.linkonce.wR.";
2233 char *lo_debug_str = ".gnu.linkonce.ws.";
2234
2235 /* SNC compiler/linker linkonce names */
2236 char *nlo_text = ".text.";
2237 char *nlo_debug_abbr = ".debug.wa.";
2238 char *nlo_debug_aranges = ".debug.wr.";
2239 char *nlo_debug_frame_1 = ".debug.wf.";
2240 char *nlo_debug_frame_2 = ".debug.wF.";
2241 char *nlo_debug_info = ".debug.wi.";
2242 char *nlo_debug_line = ".debug.wl.";
2243 char *nlo_debug_macinfo = ".debug.wm.";
2244 char *nlo_debug_loc = ".debug.wo.";
2245 char *nlo_debug_pubnames = ".debug.wp.";
2246 char *nlo_debug_ranges = ".debug.wR.";
2247 char *nlo_debug_str = ".debug.ws.";
2248
2249 /* Build linkonce section information */
2250 void
2251 build_linkonce_info(Dwarf_Debug dbg)
2252 {
2253 Dwarf_Unsigned nCount = 0;
2254 Dwarf_Unsigned section_index = 0;
2255 int res = 0;
2256
2257 static char **linkonce_names[] = {
2258 &lo_text, /* .text */
2259 &nlo_text, /* .text */
2260 &lo_debug_abbr, /* .debug_abbr */
2261 &nlo_debug_abbr, /* .debug_abbr */
2262 &lo_debug_aranges, /* .debug_aranges */
2263 &nlo_debug_aranges, /* .debug_aranges */
2264 &lo_debug_frame_1, /* .debug_frame */
2265 &nlo_debug_frame_1, /* .debug_frame */
2266 &lo_debug_frame_2, /* .debug_frame */
2267 &nlo_debug_frame_2, /* .debug_frame */
2268 &lo_debug_info, /* .debug_info */
2269 &nlo_debug_info, /* .debug_info */
2270 &lo_debug_line, /* .debug_line */
2271 &nlo_debug_line, /* .debug_line */
2272 &lo_debug_macinfo, /* .debug_macinfo */
2273 &nlo_debug_macinfo, /* .debug_macinfo */
2274 &lo_debug_loc, /* .debug_loc */
2275 &nlo_debug_loc, /* .debug_loc */
2276 &lo_debug_pubnames, /* .debug_pubnames */
2277 &nlo_debug_pubnames, /* .debug_pubnames */
2278 &lo_debug_ranges, /* .debug_ranges */
2279 &nlo_debug_ranges, /* .debug_ranges */
2280 &lo_debug_str, /* .debug_str */
2281 &nlo_debug_str, /* .debug_str */
2282 NULL
2283 };
2284
2285 const char *section_name = NULL;
2286 Dwarf_Addr section_addr = 0;
2287 Dwarf_Unsigned section_size = 0;
2288 Dwarf_Error error = 0;
2289 int nIndex = 0;
2290
2291 nCount = dwarf_get_section_count(dbg);
2292
2293 /* FIXME: dwarf_get_section_info_by_index_a() only
2294 works for section indicies
2295 as int. It works acceptably, but will fail with
2296 more than 32000 sections
2297 (a very large number) with 32bit Windows. */
2298 /* Ignore section with index=0 */
2299 for (section_index = 1;
2300 section_index < nCount;
2301 ++section_index) {
2302 res = dwarf_get_section_info_by_index_a(dbg,
2303 (int)(unsigned int)section_index,
2304 &section_name,
2305 &section_addr,
2306 &section_size,0,0,
2307 &error);
2308 if (res == DW_DLV_OK) {
2309 for (nIndex = 0; linkonce_names[nIndex]; ++nIndex) {
2310 if (section_name == strstr(section_name,
2311 *linkonce_names[nIndex])) {
2312
2313 /* Insert only linkonce sections */
2314 AddEntryIntoBucketGroup(glflags.pLinkonceInfo,
2315 section_index,
2316 section_addr,section_addr,
2317 section_addr + section_size,
2318 section_name,
2319 TRUE);
2320 break;
2321 }
2322 }
2323 } else if (res == DW_DLV_ERROR) {
2324 dwarf_dealloc_error(dbg,error);
2325 error = 0;
2326 }
2327 }
2328 if (dump_linkonce_info) {
2329 /* Unlikely this is ever useful...at present. */
2330 PrintBucketGroup(glflags.pLinkonceInfo);
2331 }
2332 }
2333
2334 /* Check for specific TAGs and initialize some
2335 information used by '-k' options */
2336 void
2337 tag_specific_globals_setup(Dwarf_Debug dbg,
2338 Dwarf_Half val,int die_indent_level)
2339 {
2340 switch (val) {
2341 /* DW_TAG_type unit will not have addresses */
2342 /* DW_TAG_skeleton unit will have addresses, but
2343 likely no children. But they are useful
2344 as marking glflags.seen_CU TRUE is useful */
2345 case DW_TAG_partial_unit:
2346 case DW_TAG_compile_unit:
2347 case DW_TAG_type_unit:
2348 case DW_TAG_skeleton_unit:
2349 /* To help getting the compile unit name */
2350 glflags.seen_CU = TRUE;
2351 /* If we are checking line information, build
2352 the table containing the pairs LowPC and HighPC */
2353 if (glflags.gf_check_decl_file ||
2354 glflags.gf_check_ranges ||
2355 glflags.gf_check_locations) {
2356 Dwarf_Debug td = 0;
2357
2358 if (!dbg) {
2359 ResetBucketGroup(glflags.pRangesInfo);
2360 } else {
2361 /* Only returns DW_DLV_OK */
2362 dwarf_get_tied_dbg(dbg,&td,0);
2363 /* With a tied-dbg we do not have
2364 detailed address ranges, so
2365 do not reset the single .text-size
2366 bucket group */
2367 if (!td) {
2368 ResetBucketGroup(glflags.pRangesInfo);
2369 }
2370 }
2371 }
2372 /* The following flag indicate that only
2373 low_pc and high_pc
2374 values found in DW_TAG_subprograms
2375 are going to be considered when
2376 building the address table used to check
2377 ranges, lines, etc */
2378 glflags.need_PU_valid_code = TRUE;
2379 break;
2380
2381 case DW_TAG_subprogram:
2382 /* Keep track of a PU */
2383 if (die_indent_level == 1) {
2384 /* A DW_TAG_subprogram can be nested,
2385 when is used to
2386 declare a member function for a
2387 local class; process the DIE
2388 only if we are at level zero in the DIEs tree */
2389 glflags.seen_PU = TRUE;
2390 glflags.seen_PU_base_address = FALSE;
2391 glflags.seen_PU_high_address = FALSE;
2392 glflags.PU_name[0] = 0;
2393 glflags.need_PU_valid_code = TRUE;
2394 }
2395 break;
2396 default: break;
2397 }
2398 }
2399
2400 /* Print CU basic information but
2401 use the local DIE for the offsets. */
2402 void
2403 PRINT_CU_INFO(void)
2404 {
2405 Dwarf_Unsigned loff = glflags.DIE_offset;
2406 Dwarf_Unsigned goff = glflags.DIE_section_offset;
2407 char hbufarea[50];
2408 char lbufarea[50];
2409 struct esb_s hbuf;
2410 struct esb_s lbuf;
2411
2412 if (glflags.current_section_id == DEBUG_LINE ||
2413 glflags.current_section_id == DEBUG_FRAME ||
2414 glflags.current_section_id == DEBUG_FRAME_EH_GNU ||
2415 glflags.current_section_id == DEBUG_ARANGES ||
2416 glflags.current_section_id == DEBUG_MACRO ||
2417 glflags.current_section_id == DEBUG_PUBNAMES ||
2418 glflags.current_section_id == DEBUG_MACINFO ) {
2419 /* These sections involve the CU die, so
2420 use the CU offsets.
2421 The DEBUG_MAC* cases are logical but
2422 not yet useful (Dec 2015).
2423 In other cases the local DIE offset makes
2424 more sense. */
2425 loff = glflags.DIE_CU_offset;
2426 goff = glflags.DIE_CU_overall_offset;
2427 }
2428 if (!cu_data_is_set()) {
2429
2430 return;
2431 }
2432 esb_constructor_fixed(&hbuf,hbufarea,sizeof(hbufarea));
2433 esb_constructor_fixed(&lbuf,lbufarea,sizeof(lbufarea));
2434 printf("\n");
2435 printf("CU Name = %s\n",sanitized(glflags.CU_name));
2436 printf("CU Producer = %s\n",sanitized(glflags.CU_producer));
2437 printf("DIE OFF = 0x%" DW_PR_XZEROS DW_PR_DUx
2438 " GOFF = 0x%" DW_PR_XZEROS DW_PR_DUx,
2439 loff,goff);
2440 /* We used to print leftover and incorrect values at times. */
2441 if (glflags.need_CU_high_address) {
2442 esb_append(&hbuf,"unknown ");
2443 } else {
2444 /* safe, hbuf is large enough. */
2445 esb_append_printf_u(&hbuf,"0x%" DW_PR_XZEROS DW_PR_DUx,
2446 glflags.CU_high_address);
2447 }
2448 if (glflags.need_CU_base_address) {
2449 esb_append(&lbuf,"unknown ");
2450 } else {
2451 /* safe, lbuf is large enough. */
2452 esb_append_printf_u(&lbuf,"0x%" DW_PR_XZEROS DW_PR_DUx,
2453 glflags.CU_low_address);
2454 }
2455 printf(", Low PC = %s, High PC = %s", esb_get_string(&lbuf),
2456 esb_get_string(&hbuf));
2457 printf("\n");
2458 esb_destructor(&hbuf);
2459 esb_destructor(&lbuf);
2460 }
2461
2462 void
2463 DWARF_CHECK_ERROR_PRINT_CU(void)
2464 {
2465 if (glflags.gf_check_verbose_mode) {
2466 if (glflags.gf_print_unique_errors) {
2467 if (!glflags.gf_found_error_message) {
2468 PRINT_CU_INFO();
2469 }
2470 } else {
2471 PRINT_CU_INFO();
2472 }
2473 }
2474 glflags.check_error++;
2475 glflags.gf_record_dwarf_error = TRUE;
2476 }
2477
2478 /* Sometimes is useful, just to know the kind of errors
2479 in an object file; not much interest in the number
2480 of errors; the specific case is just to have a general
2481 idea about the DWARF quality in the file */
2482
2483 char ** set_unique_errors = NULL;
2484 unsigned int set_unique_errors_entries = 0;
2485 unsigned int set_unique_errors_size = 0;
2486 #define SET_UNIQUE_ERRORS_DELTA 64
2487
2488 /* Create the space to store the unique error messages */
2489 void
2490 allocate_unique_errors_table(void)
2491 {
2492 if (!set_unique_errors) {
2493 set_unique_errors = (char **)
2494 malloc(SET_UNIQUE_ERRORS_DELTA * sizeof(char*));
2495 set_unique_errors_size = SET_UNIQUE_ERRORS_DELTA;
2496 set_unique_errors_entries = 0;
2497 }
2498 }
2499
2500 #ifdef TESTING
2501 /* Just for debugging purposes, dump the unique errors table */
2502 void
2503 dump_unique_errors_table(void)
2504 {
2505 unsigned int index;
2506 printf("*** Unique Errors Table ***\n");
2507 printf("Delta : %d\n",SET_UNIQUE_ERRORS_DELTA);
2508 printf("Size : %d\n",set_unique_errors_size);
2509 printf("Entries: %d\n",set_unique_errors_entries);
2510 for (index = 0; index < set_unique_errors_entries; ++index) {
2511 printf("%3d: '%s'\n",index,set_unique_errors[index]);
2512 }
2513 }
2514 #endif
2515
2516 /* Release the space used to store the unique error messages */
2517 void
2518 release_unique_errors_table(void)
2519 {
2520 #if 0
2521 The pointed-to entries are all saved in makename,
2522 so let its destructor do the work.
2523 unsigned int index;
2524 for (index = 0; index < set_unique_errors_entries; ++index) {
2525 free(set_unique_errors[index]);
2526 }
2527 #endif
2528 free(set_unique_errors);
2529 set_unique_errors = 0;
2530 set_unique_errors_entries = 0;
2531 set_unique_errors_size = 0;
2532 }
2533
2534 /* Returns TRUE if the text is already in the set; otherwise FALSE */
2535 Dwarf_Bool
2536 add_to_unique_errors_table(char * error_text)
2537 {
2538 unsigned int index;
2539 size_t len;
2540 char * stored_text;
2541 char * filtered_text;
2542 char * start = NULL;
2543 char * end = NULL;
2544 char * pattern = "0x";
2545 char * white = " ";
2546 char * question = "?";
2547
2548 /* Create a copy of the incoming text */
2549 filtered_text = makename(error_text);
2550 if (!filtered_text) {
2551 /* Lets not do anything. */
2552 return TRUE;
2553 }
2554 len = strlen(filtered_text);
2555
2556 /* Remove from the error_text, any hexadecimal
2557 numbers (start with 0x),
2558 because for some errors, an additional
2559 information is given in the
2560 form of addresses; we are interested just in
2561 the general error. */
2562 start = strstr(filtered_text,pattern);
2563 while (start) {
2564 /* We have found the start of the pattern; look for a space */
2565 end = strstr(start,white);
2566 if (!end) {
2567 /* Preserve any line terminator */
2568 end = filtered_text + len -1;
2569 }
2570 memset(start,*question,end - start);
2571 start = strstr(filtered_text,pattern);
2572 }
2573
2574 /* Check if the error text is already in the table */
2575 for (index = 0; index < set_unique_errors_entries; ++index) {
2576 stored_text = set_unique_errors[index];
2577 if (!strcmp(stored_text,filtered_text)) {
2578 return TRUE;
2579 }
2580 }
2581
2582 /* Store the new text; check if we have space
2583 to store the error text */
2584 if (set_unique_errors_entries + 1 == set_unique_errors_size) {
2585 char ** newsun = 0;
2586 unsigned int newsun_size = set_unique_errors_size +
2587 SET_UNIQUE_ERRORS_DELTA;
2588
2589 newsun = (char **)realloc(set_unique_errors,
2590 newsun_size* sizeof(char*));
2591 if (!newsun) {
2592 static int ercount = 0;
2593 if (!ercount) {
2594 printf("\nERROR: Out of space recording unique"
2595 " errors. Attempting to continue. "
2596 " Message will not repeat.\n");
2597 glflags.gf_count_major_errors++;
2598 }
2599 ++ercount;
2600 return FALSE;
2601 }
2602 set_unique_errors = newsun;
2603 set_unique_errors_size = newsun_size;
2604 }
2605 set_unique_errors[set_unique_errors_entries] = filtered_text;
2606 ++set_unique_errors_entries;
2607 return FALSE;
2608 }
2609
2610 /*
2611 Print a DWARF error message and if in "reduced" output
2612 only print one error of each kind; this feature is useful,
2613 when we are interested only in the kind of errors and
2614 not on the number of errors.
2615
2616 PRECONDITION: if s3 non-null so are s1,s2.
2617 If s2 is non-null so is s1.
2618 s1 is always non-null. */
2619
2620 static void
2621 print_dwarf_check_error(const char *s1,
2622 const char *s2,
2623 const char *s3)
2624 {
2625 static Dwarf_Bool do_init = TRUE;
2626 Dwarf_Bool found = FALSE;
2627 char * error_text = NULL;
2628 static char *leader = "\n*** DWARF CHECK: ";
2629 static char *trailer = " ***\n";
2630
2631 if (do_init) {
2632 esb_constructor(&dwarf_error_line);
2633 do_init = FALSE;
2634 }
2635 esb_empty_string(&dwarf_error_line);
2636 esb_append(&dwarf_error_line,leader);
2637 if (s3) {
2638 esb_append(&dwarf_error_line,s1);
2639 esb_append(&dwarf_error_line," -> ");
2640 esb_append(&dwarf_error_line,s2);
2641 esb_append(&dwarf_error_line,": ");
2642 esb_append(&dwarf_error_line,s3);
2643 } else if (s2) {
2644 esb_append(&dwarf_error_line,s1);
2645 esb_append(&dwarf_error_line,": ");
2646 esb_append(&dwarf_error_line,s2);
2647 } else {
2648 esb_append(&dwarf_error_line,s1);
2649 }
2650 esb_append(&dwarf_error_line,trailer);
2651
2652 error_text = esb_get_string(&dwarf_error_line);
2653 if (glflags.gf_print_unique_errors) {
2654 found = add_to_unique_errors_table(error_text);
2655 if (!found) {
2656 printf("%s",error_text);
2657 }
2658 } else {
2659 printf("%s",error_text);
2660 }
2661
2662 /* To indicate if the current error message has
2663 been found or not */
2664 glflags.gf_found_error_message = found;
2665 }
2666
2667 void
2668 DWARF_CHECK_ERROR3(Dwarf_Check_Categories category,
2669 const char *str1, const char *str2, const char *strexpl)
2670 {
2671 if (checking_this_compiler()) {
2672 DWARF_ERROR_COUNT(category,1);
2673 if (glflags.gf_check_verbose_mode) {
2674 print_dwarf_check_error(str1, str2,strexpl);
2675 }
2676 /* does glflags.check_error++; */
2677 /* sets glflags.gf_record_dwarf_error = TRUE; */
2678 DWARF_CHECK_ERROR_PRINT_CU();
2679 }
2680 }
2681
2682 /* This is too much to put in the DROP_ERROR_INSTANCE macro,
2683 so we put it here rather arbitrarily. */
2684 void
2685 report_caller_error_drop_error(int dwdlv,
2686 int line, char *fname)
2687 {
2688 printf("\nERROR in dwarfdump:"
2689 " The value passed to the macro DROP_ERROR_INSTANCE "
2690 "is not one of the three allowed values, but is "
2691 "%d. dwarfdump has a bug. "
2692 " See line %d file %s\n",dwdlv,line,fname);
2693 glflags.gf_count_major_errors++;
2694
2695 }
2696 void
2697 dd_minimal_count_global_error(void)
2698 {
2699 glflags.gf_count_major_errors++;
2700 }
2701
2702 static const char *ft[] = {
2703 "DW_FTYPE_UNKNOWN",
2704 "DW_FTYPE_ELF",
2705 "DW_FTYPE_MACH_O",
2706 "DW_FTYPE_PE",
2707 "DW_FTYPE_ARCHIVE",
2708 "DW_FTYPE_APPLEUNIVERSAL"
2709 };
2710
2711 static
2712 const char * get_ftype_name(Dwarf_Small ftype)
2713 {
2714 if (ftype >DW_FTYPE_APPLEUNIVERSAL) {
2715 dd_minimal_count_global_error();
2716 return "ERROR: Impossible ftype value";
2717 }
2718 return ft[ftype];
2719 }
2720
2721 static const char *psn[] = {
2722 "DW_PATHSOURCE_unspecified",
2723 "DW_PATHSOURCE_basic, standard",
2724 "DW_PATHSOURCE_dsym, MacOS",
2725 "DW_PATHSOURCE_debuglink, GNU debuglink"
2726 };
2727
2728 static
2729 const char * get_pathsource_name(Dwarf_Small ps)
2730 {
2731 if (ps >DW_PATHSOURCE_debuglink) {
2732 dd_minimal_count_global_error();
2733 return "ERROR: Impossible pathsource value";
2734 }
2735 return psn[ps];
2736 }
2737
2738 static const char *
2739 get_machine_name(Dwarf_Unsigned machine,
2740 Dwarf_Small ftype)
2741 {
2742 switch(ftype) {
2743 case DW_FTYPE_ELF:
2744 return dd_elf_arch_name(machine);
2745 case DW_FTYPE_PE:
2746 return dd_pe_arch_name(machine);
2747 case DW_FTYPE_APPLEUNIVERSAL:
2748 case DW_FTYPE_MACH_O:
2749 return dd_mach_arch_name(machine);
2750 default:
2751 return "Unexpected DW_FTYPE!";
2752 }
2753 }
2754
2755 /* 'machine' number meaning the cpu architecture */
2756 static void
2757 print_machine_arch(Dwarf_Debug dbg)
2758 {
2759 int res = 0;
2760 Dwarf_Small dw_ftype = 0;
2761 Dwarf_Small dw_obj_pointersize = 0;
2762 Dwarf_Bool dw_obj_is_big_endian = 0;
2763 Dwarf_Unsigned dw_obj_machine = 0;
2764 Dwarf_Unsigned dw_obj_flags = 0;
2765 Dwarf_Small dw_path_source = 0;
2766 Dwarf_Unsigned dw_ub_offset = 0;
2767 Dwarf_Unsigned dw_ub_count = 0;
2768 Dwarf_Unsigned dw_ub_index = 0;
2769 Dwarf_Unsigned dw_comdat_groupnumber = 0;
2770 res = dwarf_machine_architecture(dbg,
2771 &dw_ftype,
2772 &dw_obj_pointersize,
2773 &dw_obj_is_big_endian,
2774 &dw_obj_machine,
2775 &dw_obj_flags,
2776 &dw_path_source,
2777 &dw_ub_offset,
2778 &dw_ub_count,
2779 &dw_ub_index,
2780 &dw_comdat_groupnumber);
2781 if (res != DW_DLV_OK) {
2782 printf("ERROR: Impossible error calling "
2783 "dwarf_machine_architecture!\n");
2784 dd_minimal_count_global_error();
2785 return;
2786 }
2787 printf("Machine Architecture, Object Information fields\n");
2788 printf(" Filetype : %d (%s)\n",dw_ftype,
2789 get_ftype_name(dw_ftype));
2790 printf(" Pointersize : %u\n",dw_obj_pointersize);
2791 printf(" endian : %s\n", dw_obj_is_big_endian?
2792 "big endian":"little endian");
2793 printf(" machine/architecture : %" DW_PR_DUu " (0x%" DW_PR_DUx
2794 ") <%s>\n",dw_obj_machine,dw_obj_machine,
2795 get_machine_name(dw_obj_machine, dw_ftype));
2796 printf(" machine flags : 0x%" DW_PR_DUx "\n",
2797 dw_obj_flags);
2798 printf(" path source : %u (%s)\n",dw_path_source,
2799 get_pathsource_name(dw_path_source));
2800 if (glflags.verbose || dw_ftype == DW_FTYPE_APPLEUNIVERSAL) {
2801 printf(" MacOS Universal Binary \n");
2802 printf(" Object Offset in UB : Ox%" DW_PR_DUx "\n",
2803 dw_ub_offset);
2804 printf(" UB object count : %" DW_PR_DUu "\n",
2805 dw_ub_count);
2806 printf(" UB index : %" DW_PR_DUu "\n",
2807 dw_ub_index);
2808 }
2809 if (glflags.verbose ||dw_ftype == DW_FTYPE_ELF) {
2810 printf(" Comdat group number : %" DW_PR_DUu
2811 " (%s)\n",
2812 dw_comdat_groupnumber,
2813 dw_comdat_groupnumber==1?"DW_GROUPNUMBER_BASE, standard":
2814 dw_comdat_groupnumber==2?
2815 "DW_GROUPNUMBER_DWO, .debug.dwo etc":
2816 "(a requested non-default group number)");
2817 }
2818 if (glflags.verbose) {
2819 Dwarf_Error error = 0;
2820 int j = 0;
2821 int fres = DW_DLV_OK;
2822
2823 printf(" [] offset size flags addr\n");
2824 for ( ; fres == DW_DLV_OK; ++j) {
2825 const char *name = 0;
2826 Dwarf_Addr addr = 0;
2827 Dwarf_Unsigned size = 0;
2828 Dwarf_Unsigned flags = 0;
2829 Dwarf_Unsigned offset = 0;
2830
2831 fres = dwarf_get_section_info_by_index_a(dbg,j,
2832 &name,&addr,&size,&flags,&offset,&error);
2833 if (fres != DW_DLV_OK) {
2834 break;
2835 }
2836 printf(" [%3d]"
2837 " 0x%" DW_PR_XZEROS DW_PR_DUx
2838 " 0x%" DW_PR_XZEROS DW_PR_DUx
2839 " 0x%" DW_PR_XZEROS DW_PR_DUx
2840 " 0x%" DW_PR_XZEROS DW_PR_DUx
2841 " %s\n",j,offset,size,flags,addr,name);
2842 }
2843 if (fres == DW_DLV_ERROR) {
2844 char *errm = dwarf_errmsg(error);
2845 printf("ERROR: dwarf_get_secion_info_by_index_a "
2846 "failed %s\n",
2847 errm);
2848 dwarf_dealloc_error(dbg,error);
2849 glflags.gf_count_major_errors++;
2850 error = 0;
2851 }
2852 }
2853 }