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