Debian Codegraph @

src/bin/dwarfdump/dd_macrocheck.c

raw
1 /*
2 Copyright 2015-2016 David Anderson. All rights reserved.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of version 2 of the GNU General
6 Public License as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it would be
9 useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE.
12
13 Further, this software is distributed without any warranty
14 that it is free of the rightful claim of any third person
15 regarding infringement or the like. Any license provided
16 herein, whether implied or otherwise, applies only to this
17 software file. Patent licenses, if any, provided herein
18 do not apply to combinations of this program with other
19 software, or any other product whatsoever.
20
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write the Free
23 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 Boston MA 02110-1301, USA.
25
26 */
27
28 /* macrocheck.c, .h create a map of macro import
29 operators to establish that they are resolved and
30 that their offsets refer to actual macro groups.
31 This is across all CUs in the object file,
32 not per-cu..
33
34 DEFECTS: There are some things done here that are
35 not correct and need fixing at some point.
36 See macfile_stack, macro_import_stack,
37 macro_check_tree and macinfo_check_tree here
38 and in print_macro.c and in print_die.c
39 print_macro.c, print_die.c, and macrocheck.c
40 work together in checking DWARF macro data.
41
42 A) macro_import_stack and macfile_stack are global
43 to dwarfdump and therefore these can only be safely
44 used with DWARF5-style .debug_macro data. Not
45 with .debug_macinfo.
46 B) To support .debug_macro_sup in a separate object
47 will likely require moving all the macro data
48 out of global and into something per-open-dbg.
49 */
50
51 #include <config.h>
52
53 #include <stdlib.h> /* calloc() free() */
54 #include <stdio.h> /* FILE decl for dd_esb.h */
55
56 #include "dwarf.h"
57 #include "libdwarf.h"
58 #include "libdwarf_private.h"
59 #include "dd_defined_types.h"
60 #include "dd_checkutil.h"
61 #include "dd_glflags.h"
62 #include "dd_globals.h"
63 #include "dd_tsearchbal.h"
64 #include "dd_macrocheck.h"
65 #include "dd_esb.h"
66
67 /* WARNING: the tree walk functions will, if presented **tree
68 when *tree is wanted, simply find nothing. No error,
69 just bad results. So when a walk produces nothing
70 suspect a code mistake here.
71 The basic problem is void* is a terrible way to
72 pass in a pointer. But it's how tsearch was defined
73 long ago.
74 */
75
76 Dwarf_Unsigned macro_import_stack[MACRO_IMPORT_STACK_DEPTH +1];
77 unsigned macro_import_stack_next_to_use;
78 unsigned macro_import_stack_max_seen;
79
80 unsigned macfile_stack_next_to_use = 0;
81 unsigned macfile_stack[MACFILE_STACK_DEPTH_MAX+1];
82 unsigned macfile_stack_max_seen;
83
84 void * macro_check_tree; /* DWARF5 macros */
85 void * macinfo_check_tree; /* DWARF 2,3,4 macros */
86 void * macdefundeftree; /* DWARF5 macros */
87
88 static struct Macrocheck_Map_Entry_s * macrocheck_map_insert
89 (
90 Dwarf_Unsigned off,
91 Dwarf_Unsigned prim,
92 Dwarf_Unsigned sec,
93 Dwarf_Unsigned linenum,
94 unsigned src_file_num,
95 void **map);
96 static void macrocheck_map_destroy(void *map);
97 static Dwarf_Unsigned macro_count_recs(void **base);
98
99 #ifdef TESTING
100 int failcount = 0;
101 struct glflags_s glflags;
102 #endif /* TESTING */
103
104 static struct Macrocheck_Map_Entry_s *
105 macrocheck_map_create_entry(Dwarf_Unsigned offset,
106 Dwarf_Unsigned add_primary, Dwarf_Unsigned add_secondary)
107 {
108 struct Macrocheck_Map_Entry_s *mp =
109 (struct Macrocheck_Map_Entry_s *)
110 calloc(1,sizeof(struct Macrocheck_Map_Entry_s));
111 if (!mp) {
112 return 0;
113 }
114 mp->mp_key = offset;
115 mp->mp_len = 0;
116 mp->mp_printed = 0;
117 mp->mp_refcount_primary = add_primary;
118 mp->mp_refcount_secondary = add_secondary;
119 return mp;
120 }
121 static void
122 macrocheck_map_free_func(void *mx)
123 {
124 struct Macrocheck_Map_Entry_s *m = mx;
125 free(m);
126 }
127
128 static int
129 macrocheck_map_compare_func(const void *l, const void *r)
130 {
131 const struct Macrocheck_Map_Entry_s *ml = l;
132 const struct Macrocheck_Map_Entry_s *mr = r;
133 if (ml->mp_key < mr->mp_key) {
134 return -1;
135 }
136 if (ml->mp_key > mr->mp_key) {
137 return 1;
138 }
139 return 0;
140 }
141
142 static struct Macrocheck_Map_Entry_s *
143 macrocheck_map_insert(Dwarf_Unsigned offset,
144 Dwarf_Unsigned add_prim,Dwarf_Unsigned add_sec,
145 Dwarf_Unsigned line_num,
146 unsigned src_file_num,
147 void **tree1)
148 {
149 void *retval = 0;
150 struct Macrocheck_Map_Entry_s *re = 0;
151 struct Macrocheck_Map_Entry_s *e = 0;
152
153 e = macrocheck_map_create_entry(offset,add_prim,add_sec);
154 /* tsearch records e's contents unless e
155 is already present . We must not free it till
156 destroy time if it got added to tree1. */
157 retval = dwarf_tsearch(e,tree1, macrocheck_map_compare_func);
158 if (retval) {
159 re = *(struct Macrocheck_Map_Entry_s **)retval;
160 if (re != e) {
161 /* We returned an existing record, e not needed.
162 Increment refcounts. Lets update line, filenum
163 to latest. So later reports show latest...? */
164 re->mp_import_linenum = line_num;
165 re->mp_import_from_filenum = src_file_num;
166 re->mp_refcount_primary += add_prim;
167 re->mp_refcount_secondary += add_sec;
168 macrocheck_map_free_func(e);
169 } else {
170 /* New record */
171 e->mp_import_linenum = line_num;
172 e->mp_import_from_filenum = src_file_num;
173 /* Record e got added to tree1, do not free record e. */
174 }
175 }
176 return NULL;
177 }
178
179 struct Macrocheck_Map_Entry_s *
180 macrocheck_map_find(Dwarf_Unsigned offset,void **tree1)
181 {
182 void *retval = 0;
183 struct Macrocheck_Map_Entry_s *re = 0;
184 struct Macrocheck_Map_Entry_s *e = 0;
185
186 e = macrocheck_map_create_entry(offset,0,0);
187 retval = dwarf_tfind(e,tree1, macrocheck_map_compare_func);
188 if (retval) {
189 re = *(struct Macrocheck_Map_Entry_s **)retval;
190 }
191 /* The one we created here must be deleted, it is dead.
192 We look at the returned one instead. */
193 macrocheck_map_free_func(e);
194 return re;
195 }
196
197 static void
198 macrocheck_map_destroy(void *map)
199 {
200 /* tdestroy is not part of Posix. */
201 dwarf_tdestroy(map,macrocheck_map_free_func);
202 }
203
204 void
205 add_macro_import(void **base,
206 Dwarf_Bool is_primary,
207 Dwarf_Unsigned offset,
208 Dwarf_Unsigned line_num,
209 unsigned src_filenum)
210 {
211 Dwarf_Unsigned prim_count = 0;
212 Dwarf_Unsigned sec_count = 0;
213
214 if (is_primary) {
215 prim_count = 1;
216 } else {
217 sec_count = 1;
218 }
219 macrocheck_map_insert(offset,prim_count,sec_count,
220 line_num,src_filenum,base);
221 }
222
223 void
224 add_macro_area_len(void **base, Dwarf_Unsigned offset,
225 Dwarf_Unsigned len)
226 {
227 struct Macrocheck_Map_Entry_s *re = 0;
228
229 re = macrocheck_map_find(offset,base);
230 if (re) {
231 re->mp_len = len;
232 }
233 }
234
235 static Dwarf_Unsigned reccount = 0;
236
237 static void
238 macro_walk_count_recs(const void *nodep,
239 const DW_VISIT which,
240 const int depth)
241 {
242 (void)nodep;
243 (void)depth;
244 if (which == dwarf_postorder || which == dwarf_endorder) {
245 return;
246 }
247 reccount += 1;
248 }
249 static Dwarf_Unsigned
250 macro_count_recs(void **base)
251 {
252 reccount = 0;
253 dwarf_twalk(*base,macro_walk_count_recs);
254 return reccount;
255 }
256
257 static Dwarf_Unsigned lowestoff = 0xffffff;
258 static Dwarf_Bool lowestfound = FALSE;
259 static void
260 macro_walk_find_lowest(const void *nodep,const DW_VISIT which,
261 const int depth)
262 {
263 struct Macrocheck_Map_Entry_s * re =
264 *(struct Macrocheck_Map_Entry_s**)nodep;
265
266 (void)depth;
267 if (which == dwarf_postorder || which == dwarf_endorder) {
268 return;
269 }
270 if (!re->mp_printed) {
271 if (!lowestfound) {
272 lowestoff = re->mp_key;
273 lowestfound = TRUE;
274 } else if (re->mp_key <= lowestoff) {
275 lowestoff = re->mp_key;
276 }
277 }
278 }
279
280 /* Never returns DW_DLV_ERROR */
281 int
282 get_next_unprinted_macro_offset(void **tree, Dwarf_Unsigned * off)
283 {
284 lowestfound = FALSE;
285 lowestoff = 0xffffffff;
286
287 /* This walks the tree to find one entry.
288 Which could get slow if the tree has lots of entries. */
289 dwarf_twalk(*tree,macro_walk_find_lowest);
290 if (!lowestfound) {
291 return DW_DLV_NO_ENTRY;
292 }
293 *off = lowestoff;
294 return DW_DLV_OK;
295 }
296
297 void
298 mark_macro_offset_printed(void **base, Dwarf_Unsigned offset)
299 {
300 struct Macrocheck_Map_Entry_s *re = 0;
301
302 re = macrocheck_map_find(offset,base);
303 if (re) {
304 re->mp_printed = TRUE;
305 }
306 }
307
308 static struct Macrocheck_Map_Entry_s **mac_as_array = 0;
309 static unsigned mac_as_array_next = 0;
310 static void
311 macro_walk_to_array(const void *nodep,const DW_VISIT which,
312 const int depth)
313 {
314 struct Macrocheck_Map_Entry_s * re =
315 *(struct Macrocheck_Map_Entry_s**)nodep;
316
317 (void)depth;
318 if (which == dwarf_postorder || which == dwarf_endorder) {
319 return;
320 }
321 mac_as_array[mac_as_array_next] = re;
322 mac_as_array_next++;
323 }
324
325 static int
326 qsort_compare(const void *lin, const void *rin)
327 {
328 const struct Macrocheck_Map_Entry_s *l =
329 *(const struct Macrocheck_Map_Entry_s **)lin;
330 const struct Macrocheck_Map_Entry_s *r =
331 *(const struct Macrocheck_Map_Entry_s **)rin;
332 if (l->mp_key < r->mp_key) {
333 return -1;
334 }
335 if (l->mp_key > r->mp_key) {
336 return 1;
337 }
338 if (l->mp_len < r->mp_len) {
339 return -1;
340 }
341 if (l->mp_len > r->mp_len) {
342 return 1;
343 }
344 return 0;
345 }
346
347 static void
348 warnprimeandsecond(struct Macrocheck_Map_Entry_s *r)
349 {
350 #ifdef TESTING
351 ++failcount;
352 #endif
353 glflags.gf_count_major_errors++;
354 printf("\nERROR: For offset "
355 "0x%" DW_PR_XZEROS DW_PR_DUx
356 " %" DW_PR_DUu
357 " there is a nonzero primary count of "
358 "0x%" DW_PR_XZEROS DW_PR_DUx
359 " %" DW_PR_DUu
360 " with a secondary count of "
361 "0x%" DW_PR_XZEROS DW_PR_DUx
362 " %" DW_PR_DUu
363 "\n",
364 r->mp_key,
365 r->mp_key,
366 r->mp_refcount_primary,
367 r->mp_refcount_primary,
368 r->mp_refcount_secondary,
369 r->mp_refcount_secondary);
370 }
371
372 int
373 print_macrocheck_statistics(const char *name,void **tsbase,
374 int isdwarf5,
375 Dwarf_Unsigned section_size)
376 {
377 Dwarf_Unsigned count = 0;
378 Dwarf_Unsigned lowest = (Dwarf_Unsigned)-1ll;
379 Dwarf_Unsigned highest = 0;
380 Dwarf_Unsigned lastend = 0;
381 Dwarf_Unsigned laststart = 0;
382 Dwarf_Unsigned internalgap = 0;
383 Dwarf_Unsigned wholegap = 0;
384 Dwarf_Unsigned i = 0;
385 Dwarf_Unsigned end = 0;
386
387 if (! *tsbase) {
388 return DW_DLV_NO_ENTRY;
389 }
390 count = macro_count_recs(tsbase);
391 if (count < 1) {
392 return DW_DLV_NO_ENTRY;
393 }
394 free(mac_as_array);
395 mac_as_array = 0;
396 mac_as_array_next = 0;
397 mac_as_array = (struct Macrocheck_Map_Entry_s **)calloc(count,
398 sizeof(struct Macrocheck_Map_Entry_s *));
399 if (!mac_as_array) {
400 #ifdef TESTING
401 ++failcount;
402 #endif
403 glflags.gf_count_major_errors++;
404 printf("\nERROR: Macro checking %s: "
405 "unable to allocate %" DW_PR_DUu "pointers\n",
406 name,
407 count);
408 /* Return OK so dwarfdump.c won't look for Dwarf_Error */
409 return DW_DLV_OK;
410 }
411 dwarf_twalk(*tsbase,macro_walk_to_array);
412 printf(" Macro unit count %s: %" DW_PR_DUu "\n",name,count);
413 qsort(mac_as_array,
414 count,sizeof(struct Macrocheck_Map_Entry_s *),
415 qsort_compare);
416 for (i = 0; i < count ; ++i) {
417 struct Macrocheck_Map_Entry_s *r = mac_as_array[i];
418 #if 0
419 printf("debugging: i %u off 0x%x len 0x%x printed? %u "
420 " ref prim: %u sec: %u\n",
421 (unsigned)i,
422 (unsigned)r->mp_key,
423 (unsigned)r->mp_len,
424 (unsigned)r->mp_printed,
425 (unsigned)r->mp_refcount_primary,
426 (unsigned)r->mp_refcount_secondary);
427 #endif
428 if (r->mp_key < lowest) {
429 lowest = r->mp_key;
430 }
431 end = r->mp_key + r->mp_len;
432 if (end > highest) {
433 highest = end;
434 }
435 if (r->mp_refcount_primary > 1) {
436 #ifdef TESTING
437 ++failcount;
438 #endif
439 glflags.gf_count_major_errors++;
440 printf("\nERROR: For offset 0x%" DW_PR_XZEROS DW_PR_DUx
441 " %" DW_PR_DUu
442 " there is a primary reference count of "
443 "0x%" DW_PR_XZEROS DW_PR_DUx
444 " %" DW_PR_DUu "\n",
445 r->mp_key,
446 r->mp_key,
447 r->mp_refcount_primary,
448 r->mp_refcount_primary);
449 }
450 /* For DWARF5 style macros (in .debug_macro)
451 having both counts
452 is normal. Not so for DWARF2 .debug_macinfo. */
453 if (!isdwarf5 && r->mp_refcount_primary &&
454 r->mp_refcount_secondary) {
455 warnprimeandsecond(r);
456 }
457 }
458 lastend =
459 mac_as_array[0]->mp_key +
460 mac_as_array[0]->mp_len;
461 laststart = mac_as_array[0]->mp_key;
462 printf(" Macro Offsets start at 0x%" DW_PR_XZEROS DW_PR_DUx
463 " and end at 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
464 lowest, highest);
465 for (i = 1; i < count ; ++i) {
466 struct Macrocheck_Map_Entry_s *r = mac_as_array[i];
467 #if 0
468 printf("debugging i %u off 0x%x len 0x%x\n",
469 (unsigned)i,
470 (unsigned)r->mp_key,
471 (unsigned)r->mp_len);
472 #endif
473 if (r->mp_key > lastend) {
474 internalgap += (r->mp_key - lastend);
475 } else if (r->mp_key < lastend) {
476 /* crazy overlap */
477 #ifdef TESTING
478 ++failcount;
479 #endif
480 glflags.gf_count_major_errors++;
481 printf(" ERROR: For offset 0x%" DW_PR_XZEROS DW_PR_DUx
482 " %" DW_PR_DUu
483 " there is a crazy overlap with the previous "
484 "end offset of "
485 "0x%" DW_PR_XZEROS DW_PR_DUx
486 " %" DW_PR_DUu
487 " (previous start offset "
488 "of 0x%" DW_PR_XZEROS DW_PR_DUx ")"
489 " %" DW_PR_DUu
490 "\n",
491 r->mp_key,
492 r->mp_key,
493 lastend,
494 lastend,
495 laststart,
496 laststart);
497 }
498 laststart = r->mp_key;
499 lastend = laststart + r->mp_len;
500 }
501 /* wholegap is a) starting offset > 0 and b)
502 space after used area before end of section. */
503 wholegap = mac_as_array[0]->mp_key + internalgap;
504 if (lastend > section_size) {
505 /* Something seriously wrong */
506 #ifdef TESTING
507 ++failcount;
508 #endif
509 printf(" ERROR: For offset 0x%" DW_PR_XZEROS DW_PR_DUx
510 " %" DW_PR_DUu
511 " there is an overlap with the end of section "
512 "0x%" DW_PR_XZEROS DW_PR_DUx
513 " %" DW_PR_DUu
514 "\n",laststart,laststart,
515 lastend, lastend);
516 } else {
517 wholegap += (section_size - lastend);
518 }
519 if (wholegap) {
520 printf(" Macro Offsets internal unused space: "
521 "0x%" DW_PR_XZEROS DW_PR_DUx
522 "\n",
523 internalgap);
524 printf(" Macro Offsets total unused space: "
525 "0x%" DW_PR_XZEROS DW_PR_DUx
526 "\n",
527 wholegap);
528 }
529 if (macfile_stack_max_seen) {
530 printf("Maximum nest depth of DW_MACRO_start_file: %u\n",
531 macfile_stack_max_seen-1);
532 }
533 if (macro_import_stack_max_seen) {
534 printf("Maximum nest depth of DW_MACRO_import : %u\n",
535 macro_import_stack_max_seen-1);
536 }
537 free (mac_as_array);
538 mac_as_array = 0;
539 mac_as_array_next = 0;
540 return DW_DLV_OK;
541 }
542
543 void
544 clear_macrocheck_statistics(void **tsbase)
545 {
546 if (!*tsbase) {
547 return;
548 }
549 macrocheck_map_destroy(*tsbase);
550 *tsbase = 0;
551 }
552
553 void
554 print_macro_import_stack(void)
555 {
556 unsigned i = 0;
557
558 printf("Macro Stack Depth: %u\n",
559 macro_import_stack_next_to_use);
560 for ( ; i < macro_import_stack_next_to_use; ++i) {
561 printf("Macro Stack[%u] "
562 "MOFF=0x%" DW_PR_XZEROS DW_PR_DUx "\n",
563 i,macro_import_stack[i]);
564 }
565 }
566
567 /* Returns DW_DLV_ERROR if the push could not done,
568 which would be because full.
569 Else returns DW_DLV_OK. */
570 int
571 macro_import_stack_push(Dwarf_Unsigned offset)
572 {
573 if (macro_import_stack_next_to_use >=
574 MACRO_IMPORT_STACK_DEPTH) {
575 printf("ERROR: The macro_import_stack has exceeded "
576 "its maximum of %d\n",MACRO_IMPORT_STACK_DEPTH);
577 print_macro_import_stack();
578 glflags.gf_count_major_errors++;
579 return DW_DLV_ERROR;
580 }
581 macro_import_stack[macro_import_stack_next_to_use] = offset;
582 ++macro_import_stack_next_to_use;
583 if (macro_import_stack_max_seen <
584 macro_import_stack_next_to_use) {
585 macro_import_stack_max_seen = macro_import_stack_next_to_use;
586 }
587 return DW_DLV_OK;
588 }
589
590 /* Returns DW_DLV_ERROR if the pop could not done,
591 else returns DW_DLV_OK. */
592 int
593 macro_import_stack_pop(void)
594 {
595 if (!macro_import_stack_next_to_use) {
596 printf("ERROR: The macro_import_stack is"
597 " empty and the attempted pop() is "
598 "impossible. A dwarfdump bug.\n");
599 glflags.gf_count_major_errors++;
600 return DW_DLV_ERROR;
601 }
602 --macro_import_stack_next_to_use;
603 return DW_DLV_OK;
604 }
605
606 /* Returns DW_DLV_OK if offset present
607 else DW_DLV_NO_ENTRY. */
608 int
609 macro_import_stack_present(Dwarf_Unsigned offset)
610 {
611 unsigned i = 0;
612
613 for ( ; i < macro_import_stack_next_to_use;++i) {
614 if (macro_import_stack[i] == offset) {
615 return DW_DLV_OK;
616 }
617 }
618 return DW_DLV_NO_ENTRY;
619 }
620
621 void
622 macro_import_stack_cleanout(void)
623 {
624 macro_import_stack_next_to_use = 0;
625 }