Debian Codegraph @

src/bin/dwarfdump/dd_checkutil.c

raw
1 /*
2 Copyright (C) 2011-2012 SN Systems Ltd. All Rights Reserved.
3 Portions Copyright (C) 2011-2019 David Anderson. All Rights Reserved.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of version 2 of the GNU General
7 Public License as published by the Free Software Foundation.
8
9 This program is distributed in the hope that it would be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE.
13
14 Further, this software is distributed without any warranty
15 that it is free of the rightful claim of any third person
16 regarding infringement or the like. Any license provided
17 herein, whether implied or otherwise, applies only to this
18 software file. Patent licenses, if any, provided herein
19 do not apply to combinations of this program with other
20 software, or any other product whatsoever.
21
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write the Free
24 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 Boston MA 02110-1301, USA.
26
27 */
28 /*
29 These simple list-processing functions are in support
30 of checking DWARF for compiler-errors of various sorts.
31
32 These functions apply to
33 pRangesInfo
34 enabling checking for sanity of code addresses.
35 Using .test .init .fini as one source of valid
36 code ranges. And adding ranges from CUs etc.
37
38 pVisitedInfo
39 Checks that (within a CU) there are no loops
40 of function references in the DWARF itself.
41 Checking that we are not in an infinite loop
42 following DIE references.
43
44 pLinkOnce.
45 We have no test cases for pLinkOnce as of 2022,
46 gcc has linkonce sections such as
47 ".gnu.linkonce.wa." but we do not have any
48 to test with.
49 2022.
50
51 */
52
53 #include <config.h>
54
55 #include <stdio.h> /* printf() */
56 #include <stdlib.h> /* calloc() free() */
57 #include <string.h> /* strcmp() */
58
59 /* Windows specific header files */
60 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
61 #include "stdafx.h"
62 #endif /* HAVE_STDAFX_H */
63 #include <stdio.h> /* FILE decl for dd_esb.h */
64
65 #ifdef HAVE_STDINT_H
66 #include <stdint.h> /* uintptr_t */
67 #endif /* HAVE_STDINT_H */
68
69 #include "dwarf.h"
70 #include "libdwarf.h"
71 #include "libdwarf_private.h"
72 #include "dd_defined_types.h"
73 #include "dd_checkutil.h"
74 #include "dd_glflags.h"
75 #include "dd_globals.h"
76 #include "dd_glflags.h"
77 #include "dd_esb.h"
78
79 /* Guessing a sensible length for max section name. */
80 #define SECTION_NAME_LEN 2048
81
82 /* Private function */
83 static void DumpFullBucketGroup(Bucket_Group *pBucketGroup);
84 static unsigned long bucketgroupnext = 0;
85 static unsigned long bucketnext = 0;
86
87 static const char *
88 kindstring(int kind)
89 {
90 const char *name = 0;
91
92 switch (kind) {
93 case KIND_RANGES_INFO:
94 name = "KIND_RANGES_INFO";
95 break;
96 case KIND_LINKONCE_INFO:
97 name = "KIND_LINKONCE_INFO";
98 break;
99 case KIND_VISITED_INFO:
100 name = "KIND_VISITED_INFO";
101 break;
102 default:
103 name = 0;
104 printf("ERROR BucketGroup unknown kind of %d. Ignored\n",
105 kind);
106 glflags.gf_count_major_errors++;
107 }
108 return name;
109 }
110
111 Bucket_Group *
112 AllocateBucketGroup(int kind)
113 {
114 Bucket_Group *pBucketGroup = (Bucket_Group *)calloc(1,
115 sizeof(Bucket_Group));
116 if (!pBucketGroup) {
117 return NULL;
118 }
119 pBucketGroup->bg_number = bucketgroupnext++;
120 pBucketGroup->kind = kind;
121 return pBucketGroup;
122 }
123
124 void
125 ReleaseBucketGroup(Bucket_Group *pBucketGroup)
126 {
127 Bucket *pBucket = 0;
128 Bucket *pNext = 0;
129
130 if (!pBucketGroup) {
131 printf("ERROR ReleaseBucketGroup passed NULL. Ignored\n");
132 glflags.gf_count_major_errors++;
133 return;
134 }
135 for (pBucket = pBucketGroup->pHead; pBucket; pBucket = pNext ) {
136 pNext = pBucket->pNext;
137 free(pBucket);
138 }
139 pBucketGroup->pHead = NULL;
140 pBucketGroup->pTail = NULL;
141 free(pBucketGroup);
142 }
143
144 void
145 ResetBucketGroup(Bucket_Group *pBucketGroup)
146 {
147 Bucket *pBucket = 0;
148
149 if (!pBucketGroup) {
150 printf("ERROR ResetBucketGroup passed NULL. Ignored\n");
151 glflags.gf_count_major_errors++;
152 return;
153 }
154 for (pBucket = pBucketGroup->pHead; pBucket;
155 pBucket = pBucket->pNext) {
156 pBucket->nEntries = 0;
157 }
158 ResetSentinelBucketGroup(pBucketGroup);
159 }
160
161 /* Reset sentinels in a Bucket Group. */
162 void
163 ResetSentinelBucketGroup(Bucket_Group *pBucketGroup)
164 {
165 /* Sanity checks */
166 if (!pBucketGroup) {
167 printf("ERROR ResetSentinelBucketGroup passed NULL."
168 " Ignored\n");
169 glflags.gf_count_major_errors++;
170 return;
171 }
172 pBucketGroup->pFirst = NULL;
173 pBucketGroup->pLast = NULL;
174 }
175
176 void
177 PrintBucketGroup(Bucket_Group *pBucketGroup)
178 {
179 if (pBucketGroup) {
180 DumpFullBucketGroup(pBucketGroup);
181 }
182 }
183
184 static void
185 DumpFullBucketGroup(Bucket_Group *pBucketGroup)
186 {
187 int nBucketNo = 1;
188 int nIndex = 0;
189 int nCount = 0;
190 Bucket *pBucket = 0;
191 Bucket_Data *pBucketData = 0;
192 const char *kindstr = 0;
193
194 if (!pBucketGroup) {
195 printf("ERROR BucketGroup passed in NULL. Ignored\n");
196 glflags.gf_count_major_errors++;
197 return;
198 }
199 kindstr = kindstring(pBucketGroup->kind);
200 if (!kindstr) {
201 return;
202 }
203 printf("\nBucket Group %s index %lu"
204 " [lower 0x%" DW_PR_DUx " upper 0x%" DW_PR_DUx "]\n",
205 kindstr,
206 pBucketGroup->bg_number,
207 (Dwarf_Unsigned)pBucketGroup->lower,
208 (Dwarf_Unsigned)pBucketGroup->upper);
209 for (pBucket = pBucketGroup->pHead; pBucket && pBucket->nEntries;
210 pBucket = pBucket->pNext) {
211
212 printf("LowPC & HighPC records for bucket %d, at index %lu"
213 "\n",
214 nBucketNo++,
215 pBucket->b_number);
216 for (nIndex = 0; nIndex < pBucket->nEntries; ++nIndex) {
217 pBucketData = &pBucket->Entries[nIndex];
218 printf("[%06d] Key = 0x%08" DW_PR_DUx
219 ", Base = 0x%08" DW_PR_DUx
220 ", Low = 0x%08" DW_PR_DUx ", High = 0x%08" DW_PR_DUx
221 ", Flag = %d, Name = '%s'\n",
222 ++nCount,
223 pBucketData->key,
224 pBucketData->base,
225 pBucketData->low,
226 pBucketData->high,
227 pBucketData->bFlag,
228 pBucketData->name);
229 }
230 }
231 }
232
233 /* Insert entry into Bucket Group.
234 We make no check for duplicate information. */
235 void
236 AddEntryIntoBucketGroup(Bucket_Group *pBucketGroup,
237 Dwarf_Addr key,Dwarf_Addr base,
238 Dwarf_Addr low,Dwarf_Addr high,
239 const char *name,
240 Dwarf_Bool bFlag)
241 {
242 Bucket *pBucket = 0;
243 Bucket_Data data;
244
245 data.bFlag = bFlag;
246 data.name = name;
247 data.key = key;
248 data.base = base;
249 data.low = low;
250 data.high = high;
251
252 if (!pBucketGroup) {
253 printf("ERROR AddEntryIntoBucketGroup passed NULL."
254 " Ignored\n");
255 glflags.gf_count_major_errors++;
256 return;
257 }
258 if (!pBucketGroup->pHead) {
259 /* Allocate first bucket */
260 pBucket = (Bucket *)calloc(1,sizeof(Bucket));
261 if (!pBucket) {
262 return;
263 }
264 pBucket->b_number = bucketnext++;
265 pBucketGroup->pHead = pBucket;
266 pBucketGroup->pTail = pBucket;
267 pBucket->nEntries = 1;
268 pBucket->Entries[0] = data;
269 return;
270 }
271 pBucket = pBucketGroup->pTail;
272
273 /* Check if we have a previous allocated set of
274 buckets (that have been cleared) */
275 if (pBucket->nEntries) {
276 if (pBucket->nEntries < BUCKET_SIZE) {
277 pBucket->Entries[pBucket->nEntries++] = data;
278 } else {
279 /* Allocate new bucket */
280 pBucket = (Bucket *)calloc(1,sizeof(Bucket));
281 if (!pBucket) {
282 return;
283 }
284 pBucketGroup->pTail->pNext = pBucket;
285 pBucketGroup->pTail = pBucket;
286 pBucket->b_number = bucketnext++;
287 pBucket->nEntries = 1;
288 pBucket->Entries[0] = data;
289 }
290 } else {
291 /* We have an allocated bucket with zero entries;
292 search for the
293 first available bucket to be used as the current
294 insertion point */
295 for (pBucket = pBucketGroup->pHead; pBucket;
296 pBucket = pBucket->pNext) {
297
298 if (pBucket->nEntries < BUCKET_SIZE) {
299 pBucket->Entries[pBucket->nEntries++] = data;
300 break;
301 }
302 }
303 }
304 }
305
306 /* For Groups where entries are individually deleted, this does
307 that work. */
308 Dwarf_Bool
309 DeleteKeyInBucketGroup(Bucket_Group *pBucketGroup,Dwarf_Addr key)
310 {
311 int nIndex = 0;
312 Bucket *pBucket = 0;
313 Bucket_Data *pBucketData = 0;
314
315 if (!pBucketGroup) {
316 printf("ERROR DeleteKeyInBucketGroup passed NULL. "
317 "Ignored\n");
318 glflags.gf_count_major_errors++;
319 return FALSE;
320 }
321
322 /* For now do a linear search */
323 for (pBucket = pBucketGroup->pHead; pBucket && pBucket->nEntries;
324 pBucket = pBucket->pNext) {
325
326 for (nIndex = 0; nIndex < pBucket->nEntries; ++nIndex) {
327 pBucketData = &pBucket->Entries[nIndex];
328 if (pBucketData->key == key) {
329 Bucket_Data data = {FALSE,NULL,0,0,0,0};
330 int nStart;
331 for (nStart = nIndex + 1; nStart < pBucket->nEntries;
332 ++nStart) {
333
334 pBucket->Entries[nIndex] =
335 pBucket->Entries[nStart];
336 ++nIndex;
337 }
338 pBucket->Entries[nIndex] = data;
339 --pBucket->nEntries;
340 return TRUE;
341 }
342 }
343 }
344 return FALSE;
345 }
346
347 /* Search to see if the address is in the range between
348 low and high addresses in some Bucket Data record.
349 Checks if 'address' is inside some bucket entry.
350 This matches == if high is exact match
351 (which usually means one-past-true-high). */
352 Dwarf_Bool
353 FindAddressInBucketGroup(Bucket_Group *pBucketGroup,
354 Dwarf_Addr address)
355 {
356 int nIndex = 0;
357 Bucket *pBucket = 0;
358 Bucket_Data *pBucketData = 0;
359
360 if (!pBucketGroup) {
361 printf("ERROR FindAdressinBucketGroup passed NULL. "
362 "Ignored\n");
363 glflags.gf_count_major_errors++;
364 return FALSE;
365 }
366 /* For now do a linear search */
367 for (pBucket = pBucketGroup->pHead; pBucket && pBucket->nEntries;
368 pBucket = pBucket->pNext) {
369
370 for (nIndex = 0; nIndex < pBucket->nEntries; ++nIndex) {
371 pBucketData = &pBucket->Entries[nIndex];
372 if (address >= pBucketData->low &&
373 address <= pBucketData->high) {
374 return TRUE;
375 }
376 }
377 }
378 return FALSE;
379 }
380
381 /* Search an entry (Bucket Data) in the Bucket Set */
382 Bucket_Data *FindDataInBucketGroup(Bucket_Group *pBucketGroup,
383 Dwarf_Addr key)
384 {
385 int mid = 0;
386 int low = 0;
387 int high = 0;
388 Bucket *pBucket = 0;
389 Bucket_Data *pBucketData = 0;
390
391 if (!pBucketGroup) {
392 printf("ERROR FindDataInBucketGroup passed NULL. Ignored\n");
393 glflags.gf_count_major_errors++;
394 return 0;
395 }
396
397 for (pBucket = pBucketGroup->pHead; pBucket;
398 pBucket = pBucket->pNext) {
399 /* Get lower and upper references */
400 if (pBucket->nEntries) {
401 low = 0;
402 high = pBucket->nEntries;
403 while (low < high) {
404 mid = low + (high - low) / 2;
405 if (pBucket->Entries[mid].key < key) {
406 low = mid + 1;
407 } else {
408 high = mid;
409 }
410 }
411 if ((low < pBucket->nEntries) &&
412 (pBucket->Entries[low].key == key)) {
413
414 pBucketData = &pBucket->Entries[low];
415 /* Update sentinels to allow traversing the table */
416 if (!pBucketGroup->pFirst) {
417 pBucketGroup->pFirst = pBucketData;
418 }
419 pBucketGroup->pLast = pBucketData;
420 return pBucketData;
421 }
422 }
423 }
424 return (Bucket_Data *)NULL;
425 }
426
427 /* Search an entry (Bucket Data) in the Bucket Group.
428 The key is an offset, a DIE offset
429 within Visited info. */
430 Bucket_Data *FindKeyInBucketGroup(Bucket_Group *pBucketGroup,
431 Dwarf_Addr key)
432 {
433 int nIndex = 0;
434 Bucket *pBucket = 0;
435 Bucket_Data *pBucketData = 0;
436
437 /* Sanity checks */
438 if (!pBucketGroup) {
439 printf("ERROR FindKeyInBucketGroup passed NULL. Ignored\n");
440 glflags.gf_count_major_errors++;
441 return 0;
442 }
443
444 /* For now do a linear search */
445 for (pBucket = pBucketGroup->pHead; pBucket && pBucket->nEntries;
446 pBucket = pBucket->pNext) {
447 for (nIndex = 0; nIndex < pBucket->nEntries; ++nIndex) {
448 pBucketData = &pBucket->Entries[nIndex];
449 if (pBucketData->key == key) {
450 return pBucketData;
451 }
452 }
453 }
454 return (Bucket_Data *)NULL;
455 }
456
457 /* Search an entry (Bucket Data) in the Bucket Set by name.
458 Used to find link-once section names,
459 so the bucket group passed in is pLinkOnce. */
460 static Bucket_Data *
461 FindNameInBucketGroup(Bucket_Group *pBucketGroup,char *name)
462 {
463 int nIndex = 0;
464 Bucket *pBucket = 0;
465 Bucket_Data *pBucketData = 0;
466
467 if (!pBucketGroup) {
468 printf("ERROR FindnameInBucketGroup passed NULL. Ignored\n");
469 glflags.gf_count_major_errors++;
470 return 0;
471 }
472 /* For now do a linear search. */
473 for (pBucket = pBucketGroup->pHead; pBucket && pBucket->nEntries;
474 pBucket = pBucket->pNext) {
475 for (nIndex = 0; nIndex < pBucket->nEntries; ++nIndex) {
476 pBucketData = &pBucket->Entries[nIndex];
477 if (!strcmp(pBucketData->name,name)) {
478 return pBucketData;
479 }
480 }
481 }
482 return (Bucket_Data *)NULL;
483 }
484
485 /* Check if an address valid or not. That is,
486 check if it is in the lower -> upper range of a bucket.
487 It checks <= and >= so the lower end
488 and one-past on the upper end matches.
489
490 Used with pRangesInfo
491 */
492 Dwarf_Bool
493 IsValidInBucketGroup(Bucket_Group *pBucketGroup,Dwarf_Addr address)
494 {
495 Bucket *pBucket = 0;
496 Bucket_Data *pBucketData = 0;
497 int nIndex = 0;
498
499 if (!pBucketGroup) {
500 printf("ERROR IsValidInBucketGroup passed NULL. Ignored\n");
501 glflags.gf_count_major_errors++;
502 return FALSE;
503 }
504 /* Check the address is within the allowed limits */
505 if (address >= pBucketGroup->lower &&
506 address <= pBucketGroup->upper) {
507 pBucket = pBucketGroup->pHead;
508 for ( ;
509 pBucket && pBucket->nEntries;
510 pBucket = pBucket->pNext) {
511 for (nIndex = 0; nIndex < pBucket->nEntries; ++nIndex) {
512 pBucketData = &pBucket->Entries[nIndex];
513 if (address >= pBucketData->low &&
514 address <= pBucketData->high) {
515 return TRUE;
516 }
517 }
518 }
519 }
520 return FALSE;
521 }
522
523 /* Reset limits for values in the Bucket Set */
524 void
525 ResetLimitsBucketSet(Bucket_Group *pBucketGroup)
526 {
527 if (!pBucketGroup) {
528 printf("ERROR ResetLimitsBucketSet passed NULL. Ignored\n");
529 glflags.gf_count_major_errors++;
530 return;
531 }
532 pBucketGroup->lower = 0;
533 pBucketGroup->upper = 0;
534 }
535
536 /* Limits are set only for ranges, so only in pRangesInfo.
537 But is used for ranges and location lists.
538 The default is set from object data (virt addr,
539 size in object file) but that does not work
540 sensibly in PE object files. */
541 void
542 SetLimitsBucketGroup(Bucket_Group *pBucketGroup,
543 Dwarf_Addr lower,Dwarf_Addr upper)
544 {
545 if (!pBucketGroup) {
546 printf("ERROR SetLimitsBucketGroup passed NULL. Ignored\n");
547 glflags.gf_count_major_errors++;
548 return;
549 }
550 if (lower < upper) {
551 pBucketGroup->lower = lower;
552 pBucketGroup->upper = upper;
553 }
554 }
555
556 /* Check if a given (lopc,hipc) are valid for a linkonce.
557 We pass in the linkonce (instead of
558 referencing the global pLinkonceInfo) as that means
559 searches for pLinkonceInfo find all the uses,
560 making understanding of the code a tiny bit easier.
561 The section name created is supposed to be the appropriate
562 linkonce section name.
563 */
564 Dwarf_Bool IsValidInLinkonce(Bucket_Group *pLo,
565 const char *name,Dwarf_Addr lopc,Dwarf_Addr hipc)
566 {
567 static char section_name[SECTION_NAME_LEN];
568 Bucket_Data *pBucketData = 0;
569 /* Since text is quite uniformly just this name,
570 no need to get it
571 from elsewhere, though it will not work for non-elf. */
572 const char *lo_text = ".text.";
573
574 /* Build the name that represents the linkonce section (.text).
575 This is not defined in DWARF so not correct for all
576 compilers. */
577 struct esb_s sn;
578
579 esb_constructor_fixed(&sn,section_name,sizeof(section_name));
580 esb_append(&sn,lo_text);
581 esb_append(&sn,name);
582 pBucketData = FindNameInBucketGroup(pLo,esb_get_string(&sn));
583 esb_destructor(&sn);
584 if (pBucketData) {
585 if (lopc >= pBucketData->low && lopc <= pBucketData->high) {
586 if (hipc >= pBucketData->low &&
587 hipc <= pBucketData->high) {
588 return TRUE;
589 }
590 }
591 }
592 return FALSE;
593 }