Debian Codegraph @

src/bin/dwarfdump/print_macro.c

raw
1 /*
2 Copyright 2015-2020 David Anderson. All rights reserved.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of version 2 of the GNU General
6 Public License as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it would be
9 useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE.
12
13 Further, this software is distributed without any warranty
14 that it is free of the rightful claim of any third person
15 regarding infringement or the like. Any license provided
16 herein, whether implied or otherwise, applies only to this
17 software file. Patent licenses, if any, provided herein
18 do not apply to combinations of this program with other
19 software, or any other product whatsoever.
20
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write the Free
23 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 Boston MA 02110-1301, USA.
25 */
26
27 #include <config.h>
28
29 #include <stdlib.h> /* calloc() free() */
30 #include <string.h> /* memcpy() strcmp() strdup() strlen() */
31 #include <stddef.h> /* size_t */
32 #include <stdio.h> /* FILE decl for dd_esb.h, printf etc */
33
34 #include "dwarf.h"
35 #include "libdwarf.h"
36 #include "libdwarf_private.h"
37 #include "dd_defined_types.h"
38 #include "dd_checkutil.h"
39 #include "dd_glflags.h"
40 #include "dd_globals.h"
41 #include "dd_naming.h"
42 #include "dd_esb.h"
43 #include "dd_esb_using_functions.h"
44 #include "dd_uri.h"
45 #include "dd_makename.h"
46 #include "dd_tsearchbal.h"
47 #include "print_sections.h"
48 #include "dd_macrocheck.h"
49 #include "dd_sanitized.h"
50 #include "dd_safe_strcpy.h"
51
52 /* See the comments at the beginning of macrocheck.c */
53
54 static int
55 strcmp_for_macdef_tdel( const void *l,const void *r)
56 {
57 const char *ls = (const char *)l;
58 const char *rs = (const char *)r;
59 return strcmp(ls,rs);
60 }
61
62 /* Extensible array, of pointers to file records . */
63 #define MACFILE_ARRAY_START_LEN 100
64 unsigned macfile_array_len;
65 unsigned macfile_array_next_to_use;
66 /* Array of pointers to extended macfile_entry records...
67 records with the file name string appended, so
68 not all the same length. */
69 macfile_entry ** macfile_array;
70
71 static int macdef_tree_compare_func(const void *l, const void *r);
72 static void macdef_tree_insert(char *key,
73 unsigned opnum,
74
75 unsigned operator,
76 Dwarf_Unsigned line,
77 Dwarf_Unsigned offset,
78 const char * string,
79 Dwarf_Unsigned macro_unit_offset,
80 void **map);
81 static macdef_entry * macdef_tree_find(char *key,
82 void **map);
83 static macdef_entry * macdef_tree_create_entry(char *key,
84 unsigned opnum,
85 unsigned operator,
86 Dwarf_Unsigned line,
87 Dwarf_Unsigned offset,
88 Dwarf_Unsigned macro_unit_offset,
89 const char * string);
90 static void macfile_array_destroy(void);
91 void macdef_tree_run_checks(void);
92
93 static void macdef_free_func(void *mx)
94 {
95 struct madef_entry *m = mx;
96 /* frees both strings and the struct */
97 free(m);
98 }
99
100 macfile_entry *
101 macfile_from_array_index(
102 unsigned index)
103 {
104 macfile_entry *m = 0;
105 m = macfile_array[index];
106 return m;
107 }
108
109 static void
110 macdef_tree_destroy_inner( void *tree)
111 {
112 dwarf_tdestroy(tree,macdef_free_func);
113 }
114 static void
115 destroy_macdef_tree(void)
116 {
117 macdef_tree_destroy_inner(macdefundeftree);
118 macdefundeftree = 0;
119 }
120 static void
121 destroy_macro_globals(void)
122 {
123 macfile_array_destroy();
124 destroy_macdef_tree();
125 /* The stack is static, not malloc,
126 and only has unsigned vals, nothing
127 to free. */
128 macfile_stack_next_to_use = 0;
129 }
130
131 static int
132 print_macros_5style_this_cu_inner(Dwarf_Debug dbg, Dwarf_Die cu_die,
133 char **dwarf_srcfiles,
134 Dwarf_Signed srcfiles_count,
135 int do_print_dwarf /* not relying on gf_do_print_dwarf here */,
136 int descend_into_inport/* TRUE means follow imports */,
137 int by_offset /* if TRUE is an imported macro unit
138 so the offset is relevant.
139 If false is the set for the CU itself. */,
140 Dwarf_Unsigned offset,
141 Dwarf_Unsigned lineno,
142 unsigned fileno,
143 int level,
144 Dwarf_Error *err);
145
146 static const char *nonameavail = "<no-name-available>";
147 static const char *nofileseenyet =
148 "<Before-First-DW_MACRO_start_file>";
149
150 static void
151 print_stack_crash(void)
152 {
153 unsigned i = 0;
154 printf("MACRONOTE: The start file operation just above"
155 " exceeds the max allowed of %d. "
156 "Possibly corrupt dwarf\n",MACFILE_STACK_DEPTH_MAX);
157 glflags.gf_count_macronotes++;
158 printf(" [] op# line filenum filename\n");
159 for (i = 0; i < macfile_stack_next_to_use; ++i) {
160 macfile_entry * m = macfile_array[macfile_stack[i]];
161 printf(" [%u] %3u %4" DW_PR_DUu
162 " %2" DW_PR_DUu
163 " MOFF=0x%" DW_PR_DUx
164 " %s\n",
165 i, m->ms_operatornum,m->ms_line,m->ms_filenum,
166 m->ms_macro_unit_offset,
167 sanitized(m->ms_filename));
168 }
169 }
170
171 /* Return 0 if format is bogus */
172 static unsigned
173 find_set_keyend(char *str)
174 {
175 char c = 0;
176 char *cp = str;
177 unsigned int len = 0;
178
179 for ( ; *cp ; ++cp) {
180 c=*cp;
181 /* The following two returns are
182 the norm for #define */
183 if (c == ' ') {
184 *cp = 0;
185 return len;
186 } else if (c == '(') {
187 *cp = 0;
188 return len;
189 }
190 ++len;
191 continue;
192 }
193 /* This is the normal return for #undef.
194 If an incoming #define string is empty or has
195 no spaces we get here too.*/
196 return len;
197 }
198
199 static int
200 is_define_op(unsigned int operator)
201 {
202 switch(operator) {
203 case DW_MACRO_define:
204 case DW_MACRO_define_strx:
205 case DW_MACRO_define_sup:
206 case DW_MACRO_define_strp:
207 return TRUE;
208 }
209 return FALSE;
210 }
211
212 static void
213 add_def_undef(unsigned opnum,
214 Dwarf_Unsigned offset,
215 unsigned int operator,
216 Dwarf_Unsigned line_number,
217 const char * macro_string,
218 Dwarf_Off macro_unit_offset,
219 struct esb_s *mtext,
220 Dwarf_Bool didprintdwarf)
221 {
222 unsigned int key_length = 0;
223 macfile_entry *m = 0;
224 macdef_entry *meb = 0;
225 char * keystr = 0;
226 int isdef = FALSE;
227 int allocerror = FALSE;
228
229 if (!strcmp(esb_get_string(mtext),nonameavail)) {
230 /* we have no string, just the fake we provide.
231 hard to check much in this case. */
232 return;
233 }
234 /* Have to strdup here as find_set_keyend
235 modifies what it is passed. */
236 keystr = strdup((const char *)macro_string);
237 if (!keystr) {
238 if (!allocerror) {
239 glflags.gf_count_major_errors++;
240 printf("ERROR: Macro define/undef "
241 "macro op string strdup() fails, "
242 "Out of memory. Some dwarfdump"
243 "reporting will be incorrect.\n");
244 allocerror = TRUE;
245 }
246 return;
247 }
248 key_length = find_set_keyend(keystr);
249 if (!key_length) {
250 if (!didprintdwarf) {
251 printf("%s",sanitized(esb_get_string(mtext)));
252 }
253 glflags.gf_count_major_errors++;
254 printf("ERROR: the above define/undef "
255 "macro op is missing its "
256 "macro name. Corrupt DWARF.\n");
257 free(keystr);
258 return;
259 }
260 isdef = is_define_op(operator);
261 meb = macdef_tree_find(keystr,&macdefundeftree);
262 if (!meb) {
263 /* Unknown key. */
264 macdef_tree_insert(keystr,opnum,operator,line_number,
265 offset,macro_string,
266 macro_unit_offset,
267 &macdefundeftree);
268 meb = macdef_tree_find(keystr,&macdefundeftree);
269 if (!meb) {
270 printf("ERROR: Unable to find key \"%s\" "
271 "in macdef tree though just created.\n",
272 sanitized(keystr));
273 free(keystr);
274 return;
275 }
276 if (isdef) {
277 meb->md_defined = isdef;
278 meb->md_defcount = 1;
279 } else {
280 meb->md_undefined = !isdef;
281 meb->md_undefcount = 1;
282 }
283 free(keystr);
284 return;
285 }
286 /* A key we have seen */
287 if (isdef) {
288 if (meb->md_defined) {
289 if (!strcmp(macro_string,meb->md_string)) {
290 /* duplicate def. Legal C */
291 } else {
292 /* Not duplicate def. Bogus. */
293 if (!didprintdwarf) {
294 printf("%s",sanitized(esb_get_string(mtext)));
295 }
296 glflags.gf_count_macronotes++;
297 printf("MACRONOTE: Duplicating the macro "
298 "name \"%s\" but with a different spelling "
299 "seems to be an error\n",
300 sanitized(keystr));
301 printf(" Earlier spelling in operator %u is\n",
302 meb->md_operatornum);
303 m = macfile_from_array_index(
304 meb->md_file_array_entry);
305 printf(" MOFF=0x%" DW_PR_XZEROS DW_PR_DUx
306 " from line %" DW_PR_DUu " file %s",
307 meb->md_macro_unit_offset,
308 meb->md_line,
309 sanitized(m->ms_filename));
310 printf(" %s\n",sanitized(meb->md_string));
311 printf(" new spelling with operator %u is\n",
312 opnum);
313 m = macfile_from_array_index(
314 macfile_array_next_to_use -1);
315 printf(" MOFF=0x%" DW_PR_XZEROS DW_PR_DUx
316 " from line %" DW_PR_DUu " file %s",
317 macro_unit_offset,
318 line_number,
319 sanitized(m->ms_filename));
320 printf(" %s\n",sanitized(macro_string));
321 meb->md_defcount++;
322 }
323 } else {
324 /* Now defining something new with this key. */
325 /* Should we print something? Warn or not? */
326 unsigned defcount = meb->md_defcount+1;
327 unsigned undefcount = meb->md_undefcount;
328 macdef_entry *mee = 0;
329 dwarf_tdelete(keystr,&macdefundeftree,
330 strcmp_for_macdef_tdel);
331 macdef_tree_insert(keystr,opnum,
332 operator,line_number,offset,
333 macro_string,
334 macro_unit_offset,
335 &macdefundeftree);
336 mee = macdef_tree_find(keystr,&macdefundeftree);
337 if (!mee) {
338 printf("ERROR: Unable to find key \"%s\" "
339 "in macdef tree though just created..\n",
340 sanitized(keystr));
341 return;
342 }
343 mee->md_defined = TRUE;
344 mee->md_defcount = defcount;
345 mee->md_undefcount = undefcount+1;
346 }
347 free(keystr);
348 /* free(me); */
349 return;
350 }
351 /* We have seen it and we have an undef for it now */
352 if (meb->md_defined) {
353 /* Was def of something with this key,
354 now undefining with this key. */
355 unsigned defcount = meb->md_defcount;
356 unsigned undefcount = meb->md_undefcount+1;
357 macdef_entry *mec = 0;
358 dwarf_tdelete(keystr,&macdefundeftree,
359 strcmp_for_macdef_tdel);
360 macdef_tree_insert(keystr,opnum,
361 operator,line_number,offset,macro_string,
362 macro_unit_offset,
363 &macdefundeftree);
364 mec = macdef_tree_find(keystr,&macdefundeftree);
365 if (!mec) {
366 printf("ERROR: Unable to find key \"%s\" "
367 "in macdef tree though just created..\n",
368 sanitized(keystr));
369 return;
370 }
371 mec->md_defined = FALSE;
372 mec->md_undefined = TRUE;
373 mec->md_defcount = defcount;
374 mec->md_undefcount = undefcount+1;
375 /* free(me); */
376 free(keystr);
377 return;
378 }
379 if (!meb->md_undefined) {
380 /* Simply a normal first undef of a defined thing. */
381 unsigned defcount = meb->md_defcount;
382 unsigned undefcount = meb->md_undefcount+1;
383 macdef_entry *med = 0;
384 dwarf_tdelete(keystr,&macdefundeftree,
385 strcmp_for_macdef_tdel);
386 macdef_tree_insert(keystr,opnum,
387 operator,line_number,offset,macro_string,
388 macro_unit_offset,
389 &macdefundeftree);
390 med = macdef_tree_find(keystr,&macdefundeftree);
391 if (!med) {
392 printf("ERROR: Unable to find key \"%s\" "
393 "in macdef tree though just created.3.\n",
394 sanitized(keystr));
395 return;
396 }
397 med->md_defined = FALSE;
398 med->md_undefined = TRUE;
399 med->md_defcount = defcount;
400 med->md_undefcount = undefcount+1;
401 /* free(me); */
402 free(keystr);
403 return;
404 }
405 /* ASSERT: meb->md_undefined TRUE */
406 /* In tree is marked undef. So undef again */
407 if (!didprintdwarf) {
408 printf("%s",sanitized(esb_get_string(mtext)));
409 }
410 if (!glflags.gf_do_check_dwarf) {
411 free(keystr);
412 return;
413 }
414 glflags.gf_count_macronotes++;
415 printf("MACRONOTE: Duplicating the undefine of macro "
416 "name \"%s\" "
417 "could possibly be an error.\n",
418 sanitized(keystr));
419 printf(" Earlier in operator %u is\n", meb->md_operatornum);
420 m = macfile_from_array_index(meb->md_file_array_entry);
421 printf(" MOFF=0x%" DW_PR_XZEROS DW_PR_DUx
422 " from line %" DW_PR_DUu " file %s",
423 meb->md_macro_unit_offset,
424 meb->md_line,
425 sanitized(m->ms_filename));
426 printf(" %s\n",sanitized(meb->md_key));
427 printf(" new in operator %u is\n", opnum);
428 m = macfile_from_array_index(macfile_array_next_to_use -1);
429 printf(" MOFF=0x%" DW_PR_XZEROS DW_PR_DUx
430 " from line %" DW_PR_DUu " file %s",
431 macro_unit_offset,
432 line_number,
433 sanitized(m->ms_filename));
434 printf(" %s\n",sanitized(keystr));
435 free(keystr);
436 /* free(me); */
437 return;
438 }
439 static void
440 expand_array_file_if_required(void)
441 {
442 if (macfile_array_next_to_use >= macfile_array_len) {
443 /* ASSERT: useme == macfile_array_len */
444 unsigned oldlen = macfile_array_len;
445 unsigned newlen = 2*oldlen;
446 macfile_entry ** newar = 0;
447
448 if (!newlen) {
449 /* We have seen nothing, make a fresh start. */
450 newlen = MACFILE_ARRAY_START_LEN;
451 free(macfile_array);
452 macfile_array = 0;
453 macfile_array_next_to_use = 0;
454 macfile_array_len = 0;
455 }
456 newar = (macfile_entry **)calloc(newlen,
457 sizeof(macfile_entry *));
458 if (!newar) {
459 /* Out of memory. */
460 printf("\nERROR: out of memory attempting "
461 "allocation of %u "
462 "entries on macfile_array. Skipping entry.\n",newlen);
463 glflags.gf_count_major_errors++;
464 return;
465 }
466 if (oldlen && macfile_array) {
467 memcpy(newar,macfile_array,
468 oldlen*sizeof(macfile_entry *));
469 free(macfile_array);
470 macfile_array = 0;
471 }
472 macfile_array_next_to_use = oldlen;
473 macfile_array_len = newlen;
474 macfile_array = newar;
475 }
476 }
477
478 static void
479 add_array_file_entry(unsigned k,
480 Dwarf_Unsigned offset,
481 unsigned int operator,
482 Dwarf_Unsigned line_number,
483 Dwarf_Unsigned index,
484 Dwarf_Off macro_unit_offset,
485 const char * macro_string)
486 {
487 size_t namelen = strlen(macro_string) +1;
488 unsigned stroff = sizeof(macfile_entry);
489 macfile_entry *m = 0;
490 /* Room for the string, its NUL, and the entry struct
491 fields. */
492 unsigned long alloclen = (unsigned long)
493 (sizeof(macfile_entry) + namelen);
494
495 expand_array_file_if_required();
496 m = (macfile_entry*) calloc(1,alloclen);
497 if (!m) {
498 return;
499 }
500 m->ms_operatornum = k;
501 m->ms_operator = operator;
502 m->ms_line = line_number;
503 m->ms_filenum = index;
504 m->ms_offset = offset;
505 m->ms_macro_unit_offset = macro_unit_offset;
506 m->ms_array_number = macfile_array_next_to_use;
507 m->ms_filename = (char *)m + stroff;
508 dd_safe_strcpy(m->ms_filename,namelen,macro_string,namelen-1);
509 macfile_array[macfile_array_next_to_use] = m;
510 macfile_stack[macfile_stack_next_to_use] =
511 macfile_array_next_to_use;
512 macfile_array_next_to_use++;
513 macfile_stack_next_to_use++;
514 if (macfile_stack_next_to_use > macfile_stack_max_seen) {
515 macfile_stack_max_seen = macfile_stack_next_to_use;
516 }
517 }
518
519 static void
520 add_to_file_stack(unsigned k,
521 Dwarf_Unsigned offset,
522 unsigned int operator,
523 Dwarf_Unsigned line_number,
524 Dwarf_Unsigned index,
525 Dwarf_Off macro_unit_offset,
526 const char * macro_string,
527 struct esb_s * mtext,
528 Dwarf_Bool didprintdwarf)
529 {
530 if (operator == DW_MACRO_end_file) {
531 unsigned stack_useme = 0;
532
533 /* DW_MACRO_end_file */
534 if (!didprintdwarf && !glflags.gf_do_check_dwarf) {
535 printf("%s",sanitized(esb_get_string(mtext)));
536 }
537 if (!glflags.gf_do_check_dwarf) {
538 macfile_entry *m = 0;
539 m = macfile_from_array_index(
540 macfile_array_next_to_use -1);
541 if (macfile_stack_next_to_use < 1) {
542 printf("MACRONOTE: End file operation just above"
543 " MOFF=0x%" DW_PR_XZEROS DW_PR_DUx
544 " file %s"
545 " has no applicable start file!"
546 " Possibly corrupt dwarf.\n",
547 macro_unit_offset,
548 sanitized(m->ms_filename));
549 glflags.gf_count_macronotes++;
550 return;
551 }
552 }
553 /* Leave the file array untouched. */
554 stack_useme = macfile_stack_next_to_use -1;
555 macfile_stack[stack_useme] = 0;
556 macfile_stack_next_to_use = stack_useme;
557 return;
558 }
559 if (macfile_stack_next_to_use >= MACFILE_STACK_DEPTH_MAX) {
560 if (!didprintdwarf) {
561 printf("%s",sanitized(esb_get_string(mtext)));
562 }
563 print_stack_crash();
564 return;
565 }
566 add_array_file_entry(k,offset,operator,
567 line_number,index,
568 macro_unit_offset,macro_string);
569 return;
570 }
571
572 static void
573 print_source_intro(Dwarf_Debug dbg,Dwarf_Die cu_die)
574 {
575 Dwarf_Off off = 0;
576 int ores = 0;
577 Dwarf_Error err = 0;
578
579 ores = dwarf_dieoffset(cu_die, &off, &err);
580 if (ores == DW_DLV_OK) {
581 int lres = 0;
582 const char *sec_name = 0;
583
584 lres = dwarf_get_die_section_name_b(cu_die,
585 &sec_name,&err);
586 if (lres != DW_DLV_OK || !sec_name || !strlen(sec_name)) {
587 sec_name = ".debug_info";
588 }
589 if (lres == DW_DLV_ERROR) {
590 dwarf_dealloc_error(dbg,err);
591 err = 0;
592 }
593 printf("Macro data from CU-DIE at %s offset 0x%"
594 DW_PR_XZEROS DW_PR_DUx ":\n",
595 sanitized(sec_name),
596 (Dwarf_Unsigned) off);
597 } else {
598 printf("Macro data (for the CU-DIE at unknown location):\n");
599 }
600 if (ores == DW_DLV_ERROR) {
601 dwarf_dealloc_error(dbg,err);
602 }
603 }
604
605 static void
606 derive_error_message(unsigned k,
607 Dwarf_Half macro_operator,
608 Dwarf_Unsigned number_of_ops,
609 int res,Dwarf_Error *err,
610 const char *operator_string)
611 {
612 const char *name = "";
613 struct esb_s m;
614
615 dwarf_get_MACRO_name(macro_operator,&name);
616 esb_constructor(&m);
617 if (res == DW_DLV_ERROR) {
618 esb_append(&m,
619 "ERROR from ");
620 } else {
621 esb_append(&m,
622 "ERROR. NO_ENTRY from ");
623 }
624 esb_append(&m,operator_string);
625 esb_append_printf_s(&m,
626 " for operand %s ",sanitized(name));
627 esb_append_printf_u(&m,
628 " operand %u ",k);
629 esb_append_printf_u(&m,
630 " of %u operands",number_of_ops);
631 print_error_and_continue(esb_get_string(&m),
632 res,*err);
633 esb_destructor(&m);
634 }
635
636 /* For all macro/macinfo versions. */
637 void
638 print_split_macro_value(const char *m_in)
639 {
640 char typbuf[400];
641 char *local = (char *)m_in;
642 char *valstart = 0;
643 char *cur = local;
644 char lastchar = 0;
645 struct esb_s m;
646
647 if (!glflags.verbose) {
648 return;
649 }
650 typbuf[0] = 0;
651 valstart = dwarf_find_macro_value_start(local);
652 if (!*valstart) {
653 /* No value, just a name */
654 printf(" Name Only: %s\n",local);
655 return;
656 }
657 esb_constructor_fixed(&m,typbuf,sizeof(typbuf));
658 /* Has name<space>value
659 or name(operands)<space>value */
660 lastchar = 0;
661 for ( ;*cur && cur != valstart; ++cur) {
662 esb_appendn(&m,cur,1);
663 lastchar = *cur;
664 }
665 if (lastchar != ' ') {
666 printf("ERROR: Macro missing space to separate"
667 "name from value!");
668 glflags.gf_count_major_errors++;
669 return;
670 }
671 printf(" Name : %s\n",sanitized(esb_get_string(&m)));
672 printf(" Value: %s\n",sanitized(valstart));
673 esb_destructor(&m);
674 }
675
676 static int
677 print_macro_ops(Dwarf_Debug dbg,
678 Dwarf_Die cu_die,
679 char ** dwarf_srcfiles,
680 Dwarf_Signed srcfiles_count,
681 Dwarf_Macro_Context mcontext,
682 Dwarf_Unsigned number_of_ops,
683 int do_print_dwarf /* not relying on gf_do_print_dwarf here */,
684 int descend_into_import /* TRUE means follow imports */,
685 int by_offset /* if TRUE is an imported macro unit */,
686 Dwarf_Unsigned macro_unit_offset /* of this set*/,
687 Dwarf_Unsigned *macro_unit_length /* return val */,
688 int level,
689 Dwarf_Error *err)
690 {
691 unsigned k = 0;
692
693 for (k = 0; k < number_of_ops; ++k) {
694 Dwarf_Unsigned section_offset = 0;
695 Dwarf_Half macro_operator = 0;
696 Dwarf_Half forms_count = 0;
697 const Dwarf_Small *formcode_array = 0;
698 Dwarf_Unsigned line_number = 0;
699 Dwarf_Unsigned index = 0;
700 Dwarf_Unsigned offset =0;
701 const char * macro_string =0;
702 int lres = 0;
703 static char mbuf[100];
704 struct esb_s mtext;
705
706 esb_constructor_fixed(&mtext,mbuf,sizeof(mbuf));
707 lres = dwarf_get_macro_op(mcontext,
708 k, &section_offset,&macro_operator,
709 &forms_count, &formcode_array,err);
710 if (lres != DW_DLV_OK) {
711 struct esb_s m;
712
713 esb_constructor(&m);
714 if (lres == DW_DLV_ERROR) {
715 esb_append(&m,
716 "ERROR from dwarf_get_macro_op()");
717 } else {
718 esb_append(&m,
719 "ERROR. NO_ENTRY from dwarf_get_macro_op()");
720 }
721 esb_append_printf_u(&m,
722 " for operand %u ",k);
723 esb_append_printf_u(&m,
724 " of %u operands",number_of_ops);
725 print_error_and_continue(esb_get_string(&m),
726 lres,*err);
727 esb_destructor(&m);
728 esb_destructor(&mtext);
729 return lres;
730 }
731 esb_append_printf_i(&mtext," [%3d] ",k);
732 if (by_offset && descend_into_import) {
733 esb_append_printf_u(&mtext," <MOFF=0x%"
734 DW_PR_XZEROS DW_PR_DUx ">",
735 macro_unit_offset);
736 }
737 esb_append_printf_u(&mtext,"0x%02x",macro_operator);
738 esb_append_printf_s(&mtext," %-20s",
739 (macro_operator?
740 get_MACRO_name(macro_operator,
741 dwarf_names_print_on_error):
742 "end-of-macros"));
743 if (glflags.gf_show_global_offsets) {
744 esb_append_printf_u(&mtext," <GOFF=0x%"
745 DW_PR_XZEROS DW_PR_DUx ">",
746 section_offset);
747 }
748 if (glflags.show_form_used && forms_count > 0) {
749 unsigned l = 0;
750
751 esb_append_printf_u(&mtext,"\n Forms count %2u:",
752 forms_count);
753 for (; l < forms_count;++l) {
754 Dwarf_Small form = formcode_array[l];
755 esb_append_printf_u(&mtext," 0x%02x",
756 form);
757 esb_append_printf_s(&mtext," %-18s ",
758 get_FORM_name(form,dwarf_names_print_on_error));
759
760 }
761 esb_append(&mtext,"\n ");
762 }
763 switch(macro_operator) {
764 case 0: {
765 /* End of these DWARF_MACRO ops */
766 Dwarf_Unsigned macro_unit_len = section_offset +1 -
767 macro_unit_offset;
768 esb_append_printf_u(&mtext,
769 " op offset 0x%" DW_PR_XZEROS DW_PR_DUx,
770 section_offset);
771 esb_append_printf_u(&mtext,
772 " macro unit length %" DW_PR_DUu,
773 macro_unit_len);
774 esb_append_printf_u(&mtext,
775 " next byte offset 0x%" DW_PR_XZEROS DW_PR_DUx,
776 section_offset+1);
777 *macro_unit_length = macro_unit_len;
778 esb_append(&mtext,"\n");
779 if (do_print_dwarf) {
780 printf("%s",sanitized(esb_get_string(&mtext)));
781 }
782 }
783 break;
784 case DW_MACRO_end_file:
785 if (do_print_dwarf) {
786 esb_append(&mtext,"\n");
787 }
788 if (do_print_dwarf) {
789 printf("%s",sanitized(esb_get_string(&mtext)));
790 }
791 add_to_file_stack(k,offset,macro_operator,
792 line_number,offset,
793 macro_unit_offset,"",
794 &mtext,do_print_dwarf);
795 break;
796 case DW_MACRO_define:
797 case DW_MACRO_undef: {
798 lres = dwarf_get_macro_defundef(mcontext,
799 k,
800 &line_number,
801 &index,
802 &offset,
803 &forms_count,
804 &macro_string,
805 err);
806 if (lres != DW_DLV_OK) {
807 derive_error_message(k,macro_operator,
808 number_of_ops,
809 lres,err,"dwarf_get_macro_defundef");
810 esb_destructor(&mtext);
811 return lres;
812 }
813 esb_append_printf_u(&mtext," line %u",line_number);
814 esb_append_printf_s(&mtext," %s\n",
815 macro_string?
816 sanitized(macro_string):nonameavail);
817 if (do_print_dwarf) {
818 printf("%s",sanitized(esb_get_string(&mtext)));
819 if (macro_string) {
820 print_split_macro_value(macro_string);
821 }
822 }
823 add_def_undef(k,offset,macro_operator,
824 line_number,macro_string,
825 macro_unit_offset,
826 &mtext,do_print_dwarf);
827 break;
828 }
829
830 case DW_MACRO_define_strp:
831 case DW_MACRO_undef_strp: {
832 lres = dwarf_get_macro_defundef(mcontext,
833 k,
834 &line_number,
835 &index,
836 &offset,
837 &forms_count,
838 &macro_string,
839 err);
840 if (lres != DW_DLV_OK) {
841 derive_error_message(k,macro_operator,
842 number_of_ops,
843 lres,err,"dwarf_get_macro_defundef");
844 esb_destructor(&mtext);
845 return lres;
846 }
847 esb_append_printf_u(&mtext,
848 " line %" DW_PR_DUu,line_number);
849 esb_append_printf_u(&mtext,
850 " str offset 0x%" DW_PR_XZEROS DW_PR_DUx,
851 offset);
852 esb_append_printf_s(&mtext,
853 " %s\n",macro_string?
854 sanitized(macro_string):nonameavail);
855 if (do_print_dwarf) {
856 printf("%s",esb_get_string(&mtext));
857 if (macro_string) {
858 print_split_macro_value(macro_string);
859 }
860 }
861 add_def_undef(k,offset,macro_operator,
862 line_number,macro_string,
863 macro_unit_offset,
864 &mtext,do_print_dwarf);
865 }
866 break;
867 case DW_MACRO_define_strx:
868 case DW_MACRO_undef_strx: {
869 lres = dwarf_get_macro_defundef(mcontext,
870 k,
871 &line_number,
872 &index,
873 &offset,
874 &forms_count,
875 &macro_string,
876 err);
877 if (lres != DW_DLV_OK) {
878 derive_error_message(k,macro_operator,
879 number_of_ops,
880 lres,err,"dwarf_get_macro_defundef");
881 esb_destructor(&mtext);
882 return lres;
883 }
884 esb_append_printf_u(&mtext,
885 " line %" DW_PR_DUu,line_number);
886 esb_append_printf_u(&mtext,
887 " str offset 0x%" DW_PR_XZEROS DW_PR_DUx,
888 offset);
889 esb_append_printf_s(&mtext,
890 " %s\n",macro_string?
891 sanitized(macro_string):nonameavail);
892 if (do_print_dwarf) {
893 printf("%s",sanitized(esb_get_string(&mtext)));
894 if (macro_string) {
895 print_split_macro_value(macro_string);
896 }
897 }
898 add_def_undef(k,offset,macro_operator,
899 line_number,macro_string,
900 macro_unit_offset,
901 &mtext,do_print_dwarf);
902 break;
903 }
904 case DW_MACRO_define_sup:
905 case DW_MACRO_undef_sup: {
906 /* The strings here are from a supplementary
907 object file, not this object file.
908 Until we have a way to find
909 the supplementary object file
910 those will show name
911 "<no-name-available>"
912 */
913 /* We do not add these to the MacroCheck
914 treer */
915 lres = dwarf_get_macro_defundef(mcontext,
916 k,
917 &line_number,
918 &index,
919 &offset,
920 &forms_count,
921 &macro_string,
922 err);
923 if (lres != DW_DLV_OK) {
924 derive_error_message(k,macro_operator,
925 number_of_ops,
926 lres,err,"dwarf_get_macro_defundef");
927 esb_destructor(&mtext);
928 return lres;
929 }
930 esb_append_printf_u(&mtext,
931 " line %" DW_PR_DUu,line_number);
932 esb_append_printf_u(&mtext,
933 " str offset 0x%" DW_PR_XZEROS DW_PR_DUx,
934 offset);
935 esb_append_printf_s(&mtext,
936 " %s\n",macro_string?
937 sanitized(macro_string):nonameavail);
938 if (do_print_dwarf) {
939 printf("%s",sanitized(esb_get_string(&mtext)));
940 if (macro_string) {
941 print_split_macro_value(macro_string);
942 }
943 }
944 break;
945 }
946 case DW_MACRO_start_file: {
947 lres = dwarf_get_macro_startend_file(mcontext,
948 k,&line_number,
949 &index,
950 &macro_string,err);
951 /* The above call knows how to reference
952 its one srcfiles data and has the
953 .debug_macro version. So we do not
954 need to worry about getting the file name
955 here. */
956 if (lres != DW_DLV_OK) {
957 derive_error_message(k,macro_operator,
958 number_of_ops,
959 lres,err,"dwarf_get_macro_startend_file");
960 esb_destructor(&mtext);
961 return lres;
962 }
963 esb_append_printf_u(&mtext," line %" DW_PR_DUu,
964 line_number);
965 esb_append_printf_u(&mtext," file number %"
966 DW_PR_DUu " ",
967 index);
968 esb_append(&mtext,macro_string?
969 macro_string: "<no-name-available>");
970 esb_append(&mtext,"\n");
971 if (do_print_dwarf) {
972 printf("%s",sanitized(esb_get_string(&mtext)));
973 }
974 add_to_file_stack(k,offset,macro_operator,
975 line_number,index,
976 macro_unit_offset,macro_string,
977 &mtext,do_print_dwarf);
978 break;
979 }
980 case DW_MACRO_import: {
981 int mres = 0;
982 lres = dwarf_get_macro_import(mcontext,
983 k,&offset,err);
984 if (lres != DW_DLV_OK) {
985 derive_error_message(k,macro_operator,
986 number_of_ops,
987 lres,err,"dwarf_get_macro_import");
988 esb_destructor(&mtext);
989 return lres;
990 }
991 if (do_print_dwarf) {
992 esb_append_printf_u(&mtext,
993 " offset 0x%" DW_PR_XZEROS DW_PR_DUx,
994 offset);
995 }
996 esb_append(&mtext,"\n");
997 if (do_print_dwarf) {
998 printf("%s",sanitized(esb_get_string(&mtext)));
999 }
1000 if (descend_into_import) {
1001 /* not do_print_dwarf */
1002 macfile_entry *mac_e = 0;
1003 mac_e = macfile_from_array_index(
1004 macfile_array_next_to_use-1);
1005 mres = macro_import_stack_present(offset);
1006 if (mres == DW_DLV_OK) {
1007 printf("ERROR: While Printing DWARF5 macros "
1008 "we find a recursive nest of imports "
1009 " noted with offset 0x%"
1010 DW_PR_XZEROS DW_PR_DUx " so we stop now. \n",
1011 offset);
1012 print_macro_import_stack();
1013 glflags.gf_count_major_errors++;
1014 return DW_DLV_NO_ENTRY;
1015 }
1016 mres = print_macros_5style_this_cu_inner(dbg,
1017 cu_die,
1018 dwarf_srcfiles,srcfiles_count,
1019 FALSE /* turns off do_print_dwarf */,
1020 descend_into_import,
1021 TRUE /* by offset */,
1022 offset,
1023 mac_e->ms_line,
1024 macfile_array_next_to_use-1,
1025 level+1,
1026 err);
1027 if (mres == DW_DLV_ERROR) {
1028 struct esb_s m;
1029
1030 esb_constructor(&m);
1031 esb_append_printf_u(&m,
1032 "ERROR: Printing DWARF5 macros "
1033 " at offset 0x%x "
1034 "for the import CU failed. ",
1035 offset);
1036 print_error_and_continue(esb_get_string(&m),
1037 mres,*err);
1038 DROP_ERROR_INSTANCE(dbg,mres,*err);
1039 esb_destructor(&m);
1040 }
1041 }
1042 break;
1043 }
1044 case DW_MACRO_import_sup: {
1045 lres = dwarf_get_macro_import(mcontext,
1046 k,&offset,err);
1047 if (lres != DW_DLV_OK) {
1048 derive_error_message(k,macro_operator,
1049 number_of_ops,
1050 lres,err,"dwarf_get_macro_import");
1051 esb_destructor(&mtext);
1052 return lres;
1053 }
1054 #if 0
1055 add_macro_import_sup();
1056 /* The supplementary object file is not available,
1057 So we cannot check the import references
1058 or know the size. As of December 2020 */
1059 #endif
1060 if (do_print_dwarf) {
1061 printf(" sup_offset 0x%" DW_PR_XZEROS DW_PR_DUx "\n"
1062 ,offset);
1063 }
1064 break;
1065 }
1066 } /* End switch(macro_operator) */
1067 esb_destructor(&mtext);
1068 }
1069 return DW_DLV_OK;
1070 }
1071
1072 /* We follow imports if building_primary_tree
1073 and in that case following imports we
1074 turn do_print_dwarf FALSE.
1075 */
1076 static int
1077 print_macros_5style_this_cu_inner(Dwarf_Debug dbg, Dwarf_Die cu_die,
1078 char **dwarf_srcfiles,
1079 Dwarf_Signed srcfiles_count,
1080 int do_print_dwarf /* not relying on gf_do_print_dwarf here */,
1081 int descend_into_import /* TRUE means follow imports */,
1082 int by_offset /* if TRUE is an imported macro unit
1083 so the offset is relevant.
1084 If false is the set for the CU itself. */,
1085 Dwarf_Unsigned offset,
1086 Dwarf_Unsigned lineno,
1087 unsigned filenum,
1088 int level,
1089 Dwarf_Error *err)
1090 {
1091 int lres = 0;
1092 Dwarf_Unsigned version = 0;
1093 Dwarf_Macro_Context macro_context = 0;
1094 Dwarf_Unsigned macro_unit_offset = 0;
1095 Dwarf_Unsigned number_of_ops = 0;
1096 Dwarf_Unsigned ops_total_byte_len = 0;
1097 Dwarf_Unsigned context_total_byte_len = 0;
1098 Dwarf_Off dieprint_cu_goffset = 0;
1099 Dwarf_Off cudie_local_offset = 0;
1100 int atres = 0;
1101
1102 glflags.current_section_id = DEBUG_MACRO;
1103 if (!by_offset) {
1104 lres = dwarf_get_macro_context(cu_die,
1105 &version,&macro_context,
1106 &macro_unit_offset,
1107 &number_of_ops,
1108 &ops_total_byte_len,
1109 err);
1110 offset = macro_unit_offset;
1111 } else {
1112 lres = dwarf_get_macro_context_by_offset(cu_die,
1113 offset,
1114 &version,&macro_context,
1115 &number_of_ops,
1116 &ops_total_byte_len,
1117 err);
1118 macro_unit_offset = offset;
1119 }
1120 if (lres == DW_DLV_NO_ENTRY) {
1121 return lres;
1122 }
1123 if (lres == DW_DLV_ERROR) {
1124 print_error_and_continue("Unable to dwarf_get_macro_context()"
1125 " for the DWARF 5 style macro",
1126 lres,*err);
1127 return lres;
1128 }
1129 /* If we fail to get the offsets we won't worry about it. */
1130 atres = dwarf_die_offsets(cu_die,&dieprint_cu_goffset,
1131 &cudie_local_offset,err);
1132 DROP_ERROR_INSTANCE(dbg,atres,*err);
1133 lres = dwarf_macro_context_total_length(macro_context,
1134 &context_total_byte_len,err);
1135 if (lres != DW_DLV_OK) {
1136 dwarf_dealloc_macro_context(macro_context);
1137 return lres;
1138 }
1139 add_macro_import(&macro_check_tree,
1140 (!level)? 1:0,
1141 offset,lineno,filenum);
1142 add_macro_area_len(&macro_check_tree,offset,
1143 context_total_byte_len);
1144 lres = macro_import_stack_push(offset);
1145 if (lres == DW_DLV_ERROR) {
1146 dwarf_dealloc_macro_context(macro_context);
1147 /* message printed. Give up. */
1148 return DW_DLV_NO_ENTRY;
1149 }
1150
1151 if (do_print_dwarf) {
1152 struct esb_s truename;
1153 char buf[DWARF_SECNAME_BUFFER_SIZE];
1154
1155 esb_constructor_fixed(&truename,buf,sizeof(buf));
1156 get_true_section_name(dbg,".debug_macro",
1157 &truename,TRUE);
1158 /* This does not return */
1159
1160 if (!by_offset) {
1161 printf("\n%s: Macro info for a single cu at macro Offset"
1162 " 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
1163 sanitized(esb_get_string(&truename)),
1164 macro_unit_offset);
1165 print_source_intro(dbg,cu_die);
1166 } else {
1167 printf("\n%s: Macro info for imported macro unit "
1168 "at macro Offset "
1169 "0x%" DW_PR_XZEROS DW_PR_DUx
1170 "\n",
1171 sanitized(esb_get_string(&truename)),
1172 offset);
1173 }
1174 esb_destructor(&truename);
1175 } else {
1176 /* We are checking, not printing. */
1177 Dwarf_Half tag = 0;
1178 int tres = dwarf_tag(cu_die, &tag, err);
1179 if (tres != DW_DLV_OK) {
1180 /* Something broken here. */
1181 dwarf_dealloc_macro_context(macro_context);
1182 print_error_and_continue("Unable to get CU DIE tag "
1183 "though we could see it earlier. "
1184 "Something broken.",
1185 tres,*err);
1186 return tres;
1187 } else if (tag == DW_TAG_type_unit) {
1188 dwarf_dealloc_macro_context(macro_context);
1189 /* Not checking since type units missing
1190 address or range in CU header. */
1191 return DW_DLV_OK;
1192 }
1193 }
1194 if (do_print_dwarf && glflags.verbose > 1) {
1195 Dwarf_Bool attr_dup = FALSE;
1196 int pdres = 0;
1197 pdres = print_one_die(dbg, cu_die,
1198 dieprint_cu_goffset,
1199 /* print_information= */ 1,
1200 /* indent level */0,
1201 dwarf_srcfiles,srcfiles_count,
1202 &attr_dup,
1203 /* ignore_die_stack= */TRUE,err);
1204 if (pdres == DW_DLV_ERROR) {
1205 dwarf_dealloc_macro_context(macro_context);
1206 return pdres;
1207 }
1208 }
1209 {
1210 Dwarf_Half lversion =0;
1211 Dwarf_Unsigned mac_offset =0;
1212 Dwarf_Unsigned mac_len =0;
1213 Dwarf_Unsigned mac_header_len =0;
1214 Dwarf_Unsigned line_offset =0;
1215 unsigned mflags = 0;
1216 Dwarf_Bool has_line_offset = FALSE;
1217 Dwarf_Bool has_offset_size_64 = FALSE;
1218 Dwarf_Bool has_operands_table = FALSE;
1219 Dwarf_Half opcode_count = 0;
1220 Dwarf_Half offset_size = 4;
1221 Dwarf_Unsigned macro_unit_length = 0;
1222 const char *prefix = "";
1223
1224 if (by_offset) {
1225 prefix = " ";
1226 }
1227 lres = dwarf_macro_context_head(macro_context,
1228 &lversion, &mac_offset,&mac_len,
1229 &mac_header_len,&mflags,&has_line_offset,
1230 &line_offset,
1231 &has_offset_size_64,&has_operands_table,
1232 &opcode_count,err);
1233 if (lres == DW_DLV_NO_ENTRY) {
1234 dwarf_dealloc_macro_context(macro_context);
1235 /* Impossible */
1236 return lres;
1237 }
1238 if (lres == DW_DLV_ERROR) {
1239 dwarf_dealloc_macro_context(macro_context);
1240 print_error_and_continue(
1241 "ERROR: dwarf_macro_context_head failed",
1242 lres,*err);
1243 return lres;
1244 }
1245 if (has_offset_size_64) {
1246 offset_size = 8;
1247 }
1248 /* If pure checking we won't print this header info */
1249 if (!glflags.gf_do_check_dwarf) {
1250 /* To understand imports we really need the basic
1251 data shown on all targeted macro offsets.
1252 This is a start, allowing us to track
1253 the imported tables. Add verbose to see
1254 the rest printed just below */
1255 printf("%s Nested import level: %d\n",prefix,level);
1256 printf("%s Macro version : %d\n",prefix,lversion);
1257 printf("%s macro section offset 0x%"
1258 DW_PR_XZEROS DW_PR_DUx "\n",prefix,
1259 mac_offset);
1260 }
1261 if (glflags.verbose && !glflags.gf_do_check_dwarf) {
1262 printf("%s flags: 0x%x, "
1263 "offsetsize64? %s, "
1264 "lineoffset? %s, "
1265 "operands_table? %s\n",
1266 prefix,
1267 mflags,
1268 has_offset_size_64?"yes":" no",
1269 has_line_offset ?"yes":" no",
1270 has_operands_table?"yes":" no");
1271 printf("%s offset size 0x%x\n",prefix,offset_size);
1272 printf("%s header length: 0x%" DW_PR_XZEROS DW_PR_DUx
1273 " total length: 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
1274 prefix,
1275 mac_header_len,mac_len);
1276 if (has_line_offset) {
1277 printf(" debug_line_offset: 0x%"
1278 DW_PR_XZEROS DW_PR_DUx "\n",
1279 line_offset);
1280 }
1281 if (has_operands_table) {
1282 Dwarf_Half i = 0;
1283
1284 for (i = 0; i < opcode_count; ++i) {
1285 Dwarf_Half opcode_num = 0;
1286 Dwarf_Half operand_count = 0;
1287 const Dwarf_Small *operand_array = 0;
1288 Dwarf_Half j = 0;
1289
1290 lres = dwarf_macro_operands_table(macro_context,
1291 i, &opcode_num, &operand_count,
1292 &operand_array,err);
1293 if (lres == DW_DLV_NO_ENTRY) {
1294 struct esb_s m;
1295
1296 dwarf_dealloc_macro_context(macro_context);
1297 esb_constructor(&m);
1298
1299 esb_append_printf_u(&m,
1300 "ERROR: dwarf_macro_operands_table()"
1301 " returns NO_ENTRY for index %u ",
1302 i);
1303 esb_append_printf_u(&m,
1304 " of %u indexes. ",
1305 opcode_count);
1306 print_error_and_continue(esb_get_string(&m),
1307 lres,*err);
1308 esb_destructor(&m);
1309 return lres;
1310 }
1311 if (lres == DW_DLV_ERROR) {
1312 struct esb_s m;
1313
1314 dwarf_dealloc_macro_context(macro_context);
1315 esb_constructor(&m);
1316 esb_append_printf_u(&m,
1317 "ERROR: dwarf_macro_operands_table()"
1318 " returns ERROR for index %u ",
1319 i);
1320 esb_append_printf_u(&m,
1321 " of %u indexes. ",
1322 opcode_count);
1323 print_error_and_continue(esb_get_string(&m),
1324 lres,*err);
1325 esb_destructor(&m);
1326 return lres;
1327 }
1328 if (opcode_num == 0) {
1329 printf("%s [%3u] end of macro operands.",
1330 prefix,i);
1331 /* Continue just in case something is wrong
1332 and there are more operands! */
1333 continue;
1334 }
1335 printf("%s [%3u] op: 0x%04x %20s "
1336 "operandcount: %u\n",
1337 prefix,
1338 i,opcode_num,
1339 get_MACRO_name(opcode_num,
1340 dwarf_names_print_on_error),
1341 operand_count);
1342 for (j = 0; j < operand_count; ++j) {
1343 Dwarf_Small opnd = operand_array[j];
1344 printf("%s [%3u] 0x%04x %20s\n",
1345 prefix,j,opnd,
1346 get_FORM_name(opnd,
1347 dwarf_names_print_on_error));
1348 }
1349 }
1350 }
1351 }
1352 if (do_print_dwarf) {
1353 printf(" MacroInformationEntries count: %" DW_PR_DUu
1354 ", bytes length: %" DW_PR_DUu "\n",
1355 number_of_ops,ops_total_byte_len);
1356 }
1357 lres = print_macro_ops(dbg,
1358 cu_die,
1359 dwarf_srcfiles,
1360 srcfiles_count,
1361 macro_context,number_of_ops,
1362 do_print_dwarf /*not gf_do_print_dwarf here*/,
1363 descend_into_import /* TRUE means follow imports */,
1364 by_offset /* if TRUE is an imported macro set */,
1365 macro_unit_offset,
1366 &macro_unit_length,
1367 level,
1368 err);
1369 if (lres != DW_DLV_OK) {
1370 struct esb_s m;
1371
1372 dwarf_dealloc_macro_context(macro_context);
1373 esb_constructor(&m);
1374 if (lres == DW_DLV_ERROR){
1375 esb_append(&m,
1376 "ERROR: print_macro_ops() failed"
1377 " returns ERROR ");
1378 } else {
1379 esb_append(&m,
1380 "ERROR: print_macro_ops() failed"
1381 " returns NO_ENTRY ");
1382 }
1383 print_error_and_continue(esb_get_string(&m),
1384 lres,*err);
1385 esb_destructor(&m);
1386 return lres;
1387 }
1388 /* macro_unit_offset for macro_unit_length bytes
1389 is a real macro unit. */
1390 }
1391 #if 0
1392 if (check_lines && checking_this_compiler()) {
1393 DWARF_CHECK_COUNT(lines_result,1);
1394 dwarf_check_lineheader(cu_die,&line_errs);
1395 if (line_errs > 0) {
1396 /* does glflags.check_error++; */
1397 /* sets glflags.gf_record_dwarf_error = TRUE; */
1398 DWARF_CHECK_ERROR_PRINT_CU();
1399 DWARF_ERROR_COUNT(lines_result,line_errs);
1400 DWARF_CHECK_COUNT(lines_result,(line_errs-1));
1401 }
1402 }
1403 #endif
1404 if (do_print_dwarf) {
1405 mark_macro_offset_printed(&macro_check_tree,offset);
1406 }
1407 lres = macro_import_stack_pop();
1408 dwarf_dealloc_macro_context(macro_context);
1409 macro_context = 0;
1410 if (lres != DW_DLV_OK) {
1411 return DW_DLV_NO_ENTRY;
1412 }
1413 return DW_DLV_OK;
1414 }
1415
1416 /* descend_into_import is TRUE if we are supposed to
1417 run through the import trees when imported versus
1418 running through imported later by stacking import offsets.
1419 It is passed as the value of do_check_dwarf(), so
1420 we could have left off the argument and used
1421 do_check_dwarf() here.
1422 */
1423 int
1424 print_macros_5style_this_cu(Dwarf_Debug dbg, Dwarf_Die cu_die,
1425 char **dwarf_srcfiles,
1426 Dwarf_Signed srcfiles_count,
1427 int do_print_dwarf /* not relying on gf_do_print_dwarf here */,
1428 int descend_into_import /* TRUE means follow imports */,
1429
1430 /* if TRUE is an imported macro unit
1431 so the offset is relevant.
1432 If false is for the CU itself, offset not relevant. */
1433 int by_offset ,
1434
1435 Dwarf_Unsigned offset,
1436 Dwarf_Error *err)
1437 {
1438 int res = 0;
1439
1440 if (macfile_array_next_to_use || macfile_stack_next_to_use ||
1441 macdefundeftree || macfile_array) {
1442 printf("ERROR: dwarfdump internal files not properly "
1443 "initialized, internal dwarfdump bug. "
1444 "No macro access done. "
1445 "Pretending no macro section present\n");
1446 glflags.gf_count_major_errors++;
1447 return DW_DLV_NO_ENTRY;
1448 }
1449 add_array_file_entry(0,0,DW_MACRO_start_file,
1450 0,0,0,nofileseenyet);
1451 res =print_macros_5style_this_cu_inner(dbg,cu_die,
1452 dwarf_srcfiles,
1453 srcfiles_count,
1454 do_print_dwarf,
1455 descend_into_import,
1456 by_offset,
1457 offset,
1458 0,0,
1459 0,
1460 err);
1461 macdef_tree_run_checks();
1462 destroy_macro_globals();
1463
1464 /* Do NOT clear macrocheck statistics here,
1465 wait till all CUs processed before clearing. */
1466 return res;
1467 }
1468
1469 static int
1470 macdef_tree_compare_func(const void *l, const void *r)
1471 {
1472 const macdef_entry *ml = l;
1473 const macdef_entry *mr = r;
1474 int res = 0;
1475
1476 res = strcmp(ml->md_key,mr->md_key);
1477 return res;
1478 }
1479
1480 static void
1481 macdef_tree_insert(char *key,
1482 unsigned opnum,
1483 unsigned operator,
1484 Dwarf_Unsigned line,
1485 Dwarf_Unsigned offset,
1486 const char * string,
1487 Dwarf_Unsigned macro_unit_offset,
1488 void **map)
1489 {
1490 void *retval = 0;
1491 macdef_entry *re = 0;
1492 macdef_entry *e = 0;
1493
1494 e = macdef_tree_create_entry(key,
1495 opnum,operator,line,offset,macro_unit_offset,string);
1496 e->md_defcount = 0;
1497 e->md_undefcount = 0;
1498 e->md_undefined = FALSE;
1499 e->md_defined = FALSE;
1500 /* tsearch records e's contents unless e
1501 is already present . We must not free it till
1502 destroy time if it got added to tree1. */
1503 retval = dwarf_tsearch(e,map, macdef_tree_compare_func);
1504 if (retval) {
1505 re = *(macdef_entry **)retval;
1506 if (re != e) {
1507 /* We returned an existing record, e not needed.
1508 Increment refcounts. */
1509 macdef_free_func(e);
1510 } else {
1511 /* Record e got added to tree1, do not free record e. */
1512 }
1513 }
1514 }
1515 static macdef_entry *
1516 macdef_tree_create_entry(char *key,
1517 unsigned opnum,
1518 unsigned operator,
1519 Dwarf_Unsigned line,
1520 Dwarf_Unsigned offset,
1521 Dwarf_Unsigned macro_unit_offset,
1522 const char * string)
1523 {
1524 char *keyspace = 0;
1525 size_t klen = strlen(key) +1;
1526 size_t slen = strlen(string) +1;
1527 size_t finallen = sizeof(macdef_entry) + klen + slen;
1528 macdef_entry *me =
1529 (macdef_entry*)calloc(1,finallen);
1530 if (!me) {
1531 return 0;
1532 }
1533 keyspace = sizeof(macdef_entry) + (char *)me;
1534 me->md_key = keyspace;
1535 dd_safe_strcpy(me->md_key,klen,key,klen-1);
1536 me->md_operatornum = opnum;
1537 /* We will set md_define, md_undefined,
1538 and the md_defcount and md_undefcount
1539 elsewhere. */
1540 me->md_defined = FALSE;
1541 me->md_undefined = FALSE;
1542 me->md_operator = operator;
1543 me->md_line = line;
1544 me->md_offset = offset;
1545 me->md_macro_unit_offset = macro_unit_offset;
1546 me->md_string = keyspace + klen;
1547 me->md_file_array_entry = macfile_array_next_to_use-1;
1548 dd_safe_strcpy(me->md_string,slen,string,slen-1);
1549 return me;
1550 }
1551
1552 static macdef_entry *
1553 macdef_tree_find(char *key, void**tree)
1554 {
1555 void *retval = 0;
1556 macdef_entry *re = 0;
1557 macdef_entry *e = 0;
1558
1559 e = macdef_tree_create_entry(key, 0,0,0,0,0,"<fake>");
1560 retval = dwarf_tfind(e,tree, macdef_tree_compare_func);
1561 if (retval) {
1562 re = *(macdef_entry **)retval;
1563 }
1564 /* The one we created here must be deleted, it is dead.
1565 We look at the returned one instead. */
1566 macdef_free_func(e);
1567 return re;
1568
1569 }
1570
1571 static void
1572 macfile_array_destroy(void)
1573 {
1574 unsigned i = 0;
1575 for ( ; i < macfile_array_next_to_use; ++i ) {
1576 macfile_entry *m = macfile_array[i];
1577
1578 /* Frees the macfile_entry and the filename string
1579 attached to the end of the struct. */
1580 free(m);
1581 macfile_array[i] = 0;
1582 }
1583 free(macfile_array);
1584 macfile_array_len = 0;
1585 macfile_array_next_to_use = 0;
1586 macfile_array = 0;
1587 }
1588
1589 static Dwarf_Unsigned walk_reccount = 0;
1590 static void
1591 macro_walk_count_recs(const void *nodep,
1592 const DW_VISIT which,
1593 const int depth)
1594 {
1595 (void)nodep;
1596 (void)depth;
1597 if (which == dwarf_postorder || which == dwarf_endorder) {
1598 return;
1599 }
1600 walk_reccount += 1;
1601 }
1602 static Dwarf_Unsigned
1603 macro_count_recs(void **base)
1604 {
1605 walk_reccount = 0;
1606 dwarf_twalk(*base,macro_walk_count_recs);
1607 return walk_reccount;
1608 }
1609
1610 /* These are file-static, not local, as we need to
1611 access in a tree-walk. */
1612 static macdef_entry **mac_as_array = 0;
1613 static unsigned mac_as_array_next = 0;
1614 static void
1615 macro_walk_to_array(const void *nodep,const DW_VISIT which,
1616 const int depth)
1617 {
1618 macdef_entry * re = *(macdef_entry**)nodep;
1619
1620 (void)depth;
1621 if (which == dwarf_postorder || which == dwarf_endorder) {
1622 return;
1623 }
1624 mac_as_array[mac_as_array_next] = re;
1625 mac_as_array_next++;
1626 }
1627
1628 static int
1629 macdef_qsort_compare(const void *lin, const void *rin)
1630 {
1631 const macdef_entry *l =
1632 *(const macdef_entry **)lin;
1633 const macdef_entry *r =
1634 *(const macdef_entry **)rin;
1635 int res = 0;
1636
1637 res = strcmp(l->md_key,r->md_key);
1638 if (res) {
1639 return res;
1640 }
1641 if (l->md_operatornum < r->md_operatornum) {
1642 return -1;
1643 }
1644 if (l->md_operatornum > r->md_operatornum) {
1645 return 1;
1646 }
1647 /* No two can have the same md_operatornum,
1648 so this is impossible. */
1649 return 0;
1650 }
1651
1652 static void
1653 print_macdef_warn(unsigned i, macdef_entry *m,unsigned warncount)
1654 {
1655 if (!warncount) {
1656 printf(" macro "
1657 " defs undefs at-end\n");
1658 }
1659 printf("[%2d] %-24s",i,m->md_key);
1660 printf(" %2u",m->md_defcount);
1661 printf(" %2u",m->md_undefcount);
1662 printf(" %s",m->md_defined?"defined":"undefined");
1663 printf("\n");
1664 }
1665
1666 /* Check the macdefundef tree for the unusual
1667 Check the macfile_stack for leftovers.
1668 The tree starts with 0 and that entry
1669 is a fake for macro ops before a DW_MACRO_start_file
1670 encountered. */
1671 void
1672 macdef_tree_run_checks(void)
1673 {
1674 unsigned i = 0;
1675 unsigned warncount = 0;
1676 Dwarf_Unsigned me_array_count = 0;
1677
1678 free(mac_as_array);
1679 mac_as_array = 0;
1680 mac_as_array_next = 0;
1681 if (macfile_stack_next_to_use > 1) {
1682 printf("MACRONOTE: The DWARF5 macro start-file stack has"
1683 " %u entries left on the stack. Missing "
1684 " some end-file entries?\n",
1685 macfile_stack_next_to_use);
1686 glflags.gf_count_macronotes++;
1687 printf(" [] op# line filenum filename\n");
1688 for (i = 0; i < macfile_stack_next_to_use; ++i) {
1689 macfile_entry * m = macfile_array[macfile_stack[i]];
1690 printf(" [%u] %3u %4" DW_PR_DUu
1691 " %2" DW_PR_DUu " %s\n",
1692 i, m->ms_operatornum,m->ms_line,m->ms_filenum,
1693 sanitized(m->ms_filename));
1694 }
1695 }
1696 /* Now check the def/undef tree left */
1697 if (!glflags.gf_do_check_dwarf) {
1698 return;
1699 }
1700 if (!macdefundeftree) {
1701 return;
1702 }
1703 me_array_count = macro_count_recs(&macdefundeftree);
1704 if (me_array_count) {
1705 mac_as_array = (macdef_entry**) calloc(me_array_count,
1706 sizeof(macdef_entry*));
1707 }
1708 if (!mac_as_array) {
1709 /* done */
1710 return;
1711 }
1712 warncount = 0;
1713 mac_as_array_next = 0;
1714 dwarf_twalk(macdefundeftree, macro_walk_to_array);
1715 qsort(mac_as_array,
1716 me_array_count,sizeof(macdef_entry*),
1717 macdef_qsort_compare);
1718
1719 for (i = 0; i < me_array_count; ++i) {
1720 macdef_entry *m = mac_as_array[i];
1721
1722 if (!m->md_defined &&
1723 m->md_defcount == m->md_undefcount) {
1724 /* totally normal.*/
1725 continue;
1726 }
1727 if (m->md_defined &&
1728 m->md_defcount ==1 && m->md_undefcount == 0) {
1729 /* totally normal.*/
1730 continue;
1731 }
1732 print_macdef_warn(i,m,warncount);
1733 ++warncount;
1734 }
1735 free(mac_as_array);
1736 mac_as_array = 0;
1737 mac_as_array_next = 0;
1738 }