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 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
|
/*
Copyright (c) 2019-2020, David Anderson
All rights reserved.
Redistribution and use in source and binary forms, with
or without modification, are permitted provided that the
following conditions are met:
Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or
other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#include "config.h"
#include <stdio.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif /* HAVE_MALLOC_H */
#ifdef HAVE_STDDEF_H
#include <stddef.h> /* ptrdiff_t */
#endif /* HAVE_STDDEF_H */
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_ELF_H
#include <elf.h>
#endif /* HAVE_ELF_H */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h> /* open(), off_t, size_t, ssize_t */
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* getcwd */
#endif /* HAVE_UNISTD_H */
#include "dwarf_incl.h"
#include "dwarf_alloc.h"
#include "dwarf_error.h"
#include "dwarf_util.h"
#include "dwarfstring.h"
#include "dwarf_debuglink.h"
#ifndef O_BINARY
#define O_BINARY 0
#endif /* O_BINARY */
#define MINBUFLEN 1000
#define TRUE 1
#define FALSE 0
#ifdef HAVE_UNUSED_ATTRIBUTE
#define UNUSEDARG __attribute__ ((unused))
#else
#define UNUSEDARG
#endif
#if _WIN32
#define NULL_DEVICE_NAME "NUL"
#else
#define NULL_DEVICE_NAME "/dev/null"
#endif /* _WIN32 */
#ifdef WORDS_BIGENDIAN
#define ASNAR(func,t,s) \
do { \
unsigned tbyte = sizeof(t) - sizeof(s); \
t = 0; \
func(((char *)&t)+tbyte ,&s[0],sizeof(s)); \
} while (0)
#else /* LITTLE ENDIAN */
#define ASNAR(func,t,s) \
do { \
t = 0; \
func(&t,&s[0],sizeof(s)); \
} while (0)
#endif /* end LITTLE- BIG-ENDIAN */
static int
extract_buildid(Dwarf_Debug dbg,
struct Dwarf_Section_s * pbuildid,
unsigned *type_returned,
char **owner_name_returned,
unsigned char **build_id_returned,
unsigned *build_id_length_returned,
Dwarf_Error *error);
struct joins_s {
dwarfstring js_dirname;
dwarfstring js_basenamesimple;
dwarfstring js_basesimpledebug;
dwarfstring js_binname;
dwarfstring js_cwd;
dwarfstring js_originalfullpath;
dwarfstring js_linkstring;
dwarfstring js_tmp2;
dwarfstring js_tmp3;
dwarfstring js_buildid;
dwarfstring js_buildid_filename;
};
static void
construct_js(struct joins_s * js)
{
memset(js,0,sizeof(struct joins_s));
dwarfstring_constructor(&js->js_dirname);
dwarfstring_constructor(&js->js_basenamesimple);
dwarfstring_constructor(&js->js_basesimpledebug);
dwarfstring_constructor(&js->js_cwd);
dwarfstring_constructor(&js->js_originalfullpath);
dwarfstring_constructor(&js->js_linkstring);
dwarfstring_constructor(&js->js_tmp2);
dwarfstring_constructor(&js->js_tmp3);
dwarfstring_constructor(&js->js_buildid);
dwarfstring_constructor(&js->js_buildid_filename);
}
static void
destruct_js(struct joins_s * js)
{
dwarfstring_destructor(&js->js_dirname);
dwarfstring_destructor(&js->js_basenamesimple);
dwarfstring_destructor(&js->js_basesimpledebug);
dwarfstring_destructor(&js->js_cwd);
dwarfstring_destructor(&js->js_originalfullpath);
dwarfstring_destructor(&js->js_linkstring);
dwarfstring_destructor(&js->js_tmp2);
dwarfstring_destructor(&js->js_tmp3);
dwarfstring_destructor(&js->js_buildid);
dwarfstring_destructor(&js->js_buildid_filename);
}
static char joinchar = '/';
static char* joinstr = "/";
#if defined (HAVE_WINDOWS_PATH)
static char joincharw = '\\';
/* Large enough it is unlikely dwarfstring will ever
do a malloc here, paths usually < 100 characters. */
static char winbuf[200];
static void
transformtoposix(dwarfstring * out, char *in)
{
char *cp = in;
for( ; *cp ; ++cp) {
if ( *cp == joincharw ) {
dwarfstring_append_length(out,joinstr,1);
} else {
dwarfstring_append_length(out,cp,1);
}
}
}
#endif /* HAVE_WINDOWS_PATH */
/* ASSERT: the target chars already have the transformtoposix()
done.
*/
int
_dwarf_pathjoinl(dwarfstring *target,dwarfstring * input)
{
char *inputs = dwarfstring_string(input);
char *targ = dwarfstring_string(target);
size_t targlen = 0;
#if defined (HAVE_WINDOWS_PATH)
dwarfstring winput;
dwarfstring_constructor_static(&winput,winbuf,sizeof(winbuf));
transformtoposix(&winput,inputs);
targlen = dwarfstring_strlen(target);
inputs = dwarfstring_string(&winput);
if (!targlen) {
dwarfstring_append(target,inputs);
return DW_DLV_OK;
}
#else /* !HAVE_WINDOWS_PATH */
targlen = dwarfstring_strlen(target);
#endif /* HAVE_WINDOWS_PATH */
if (!targlen) {
dwarfstring_append(target,inputs);
return DW_DLV_OK;
}
targ = dwarfstring_string(target);
if (targ[targlen-1] != joinchar) {
if (*inputs != joinchar) {
dwarfstring_append(target,joinstr);
dwarfstring_append(target,inputs);
} else {
dwarfstring_append(target,inputs);
}
} else {
if (*inputs != joinchar) {
dwarfstring_append(target,inputs);
} else {
dwarfstring_append(target,inputs+1);
}
}
#if defined (HAVE_WINDOWS_PATH)
dwarfstring_destructor(&winput);
#endif /* HAVE_WINDOWS_PATH */
return DW_DLV_OK;
}
/* ASSERT: the last character in s is not a / */
static size_t
mydirlen(char *s)
{
char *cp = 0;
char *lastjoinchar = 0;
size_t count =0;
for(cp = s ; *cp ; ++cp,++count) {
if (*cp == joinchar) {
lastjoinchar = cp;
}
}
if (lastjoinchar) {
/* ptrdiff_t is generated but not named */
Dwarf_Unsigned sizetoendjoin =
(lastjoinchar >= s)?(lastjoinchar-s):0xffffffff;
/* count the last join as mydirlen. */
if (sizetoendjoin == 0xffffffff) {
/* impossible. */
return 0;
}
return sizetoendjoin;
}
return 0;
}
struct dwarfstring_list_s {
dwarfstring dl_string;
struct dwarfstring_list_s *dl_next;
};
static void
dwarfstring_list_constructor(struct dwarfstring_list_s *l)
{
dwarfstring_constructor(&l->dl_string);
l->dl_next = 0;
}
static int
dwarfstring_list_add_new(struct dwarfstring_list_s * base_entry,
struct dwarfstring_list_s *prev,
dwarfstring * input,
struct dwarfstring_list_s ** new_out,
int *errcode)
{
struct dwarfstring_list_s *next = 0;
if(prev) {
next = ( struct dwarfstring_list_s *)
malloc(sizeof(struct dwarfstring_list_s));
if (!next) {
*errcode = DW_DLE_ALLOC_FAIL;
return DW_DLV_ERROR;
}
dwarfstring_list_constructor(next);
} else {
next = base_entry;
}
dwarfstring_append(&next->dl_string,
dwarfstring_string(input));
if (prev) {
prev->dl_next = next;
}
*new_out = next;
return DW_DLV_OK;
}
/* destructs passed in entry (does not free it) and all
those on the dl_next list (those are freed). */
static void
dwarfstring_list_destructor(struct dwarfstring_list_s *l)
{
struct dwarfstring_list_s *curl = l;
struct dwarfstring_list_s *nextl = l;
nextl = curl->dl_next;
dwarfstring_destructor(&curl->dl_string);
curl->dl_next = 0;
curl = nextl;
for( ; curl ; curl = nextl) {
nextl = curl->dl_next;
dwarfstring_destructor(&curl->dl_string);
curl->dl_next = 0;
free(curl);
}
}
static void
prepare_linked_name(dwarfstring *out,
dwarfstring *dirname,
dwarfstring *extradir,
dwarfstring *linkname)
{
dwarfstring_append(out, dwarfstring_string(dirname));
if (extradir) {
_dwarf_pathjoinl(out,extradir);
}
_dwarf_pathjoinl(out,linkname);
}
static void
build_buildid_filename(dwarfstring *target,
unsigned buildid_length,
unsigned char *buildid)
{
dwarfstring tmp;
unsigned bu = 0;
unsigned char *cp = 0;
dwarfstring_constructor(&tmp);
dwarfstring_append(&tmp,".build-id");
dwarfstring_append(&tmp,joinstr);
cp = buildid;
for (bu = 0; bu < buildid_length; ++bu ,++cp) {
dwarfstring_append_printf_u(&tmp, "%02x",*cp);
if (bu == 0) {
dwarfstring_append(&tmp,"/");
}
}
_dwarf_pathjoinl(target,&tmp);
dwarfstring_append(target,".debug");
dwarfstring_destructor(&tmp);
return;
}
#if 0
static void
dump_bytes(const char *msg,unsigned char * start, unsigned len)
{
Dwarf_Small *end = start + len;
Dwarf_Small *cur = start;
printf("%s (0x%lx) ",msg,(unsigned long)start);
for (; cur < end; cur++) {
printf("%02x", *cur);
}
printf("\n");
}
#endif
/* If the compiler or the builder of the object file
being read has made a mistake it is possible
for a constructed name to match the original
file path, yet we do not want to add that to
the search path. */
static int
duplicatedpath(dwarfstring* l,
dwarfstring*r)
{
if (strcmp(dwarfstring_string(l),
dwarfstring_string(r))) {
return FALSE;
}
/* Oops. somebody made a mistake */
return TRUE;
}
/* New September 2019. Access to the GNU section named
.gnu_debuglink
See
https://sourceware.org/gdb/onlinedocs/
gdb/Separate-Debug-Files.html
The default global path was set
to /usr/lib/debug
by set_global_paths_init()
called at libdwarf object init time.
So there is always the default
global path present.
*/
int
_dwarf_construct_linkedto_path(
char **global_prefixes_in,
unsigned length_global_prefixes_in,
char *pathname_in,
char *link_string_in, /* from debug link */
dwarfstring *link_string_fullpath_out,
UNUSEDARG unsigned char *crc_in, /* from debug_link, 4 bytes */
unsigned char *buildid, /* from gnu buildid */
unsigned buildid_length, /* from gnu buildid */
char ***paths_out,
unsigned *paths_out_length,
int *errcode)
{
char * depath = pathname_in;
int res = 0;
struct joins_s joind;
size_t dirnamelen = 0;
struct dwarfstring_list_s base_dwlist;
struct dwarfstring_list_s *last_entry = 0;
unsigned global_prefix_number = 0;
dwarfstring_list_constructor(&base_dwlist);
construct_js(&joind);
dirnamelen = mydirlen(depath);
if (dirnamelen) {
/* Original dirname, before cwd (if needed) */
dwarfstring_append_length(&joind.js_tmp2,
depath,dirnamelen);
}
if (depath[0] != joinchar) {
/* Meaning a/b or b, not /a/b or /b ,
so we apply cwd */
char buffer[2000];
unsigned buflen= sizeof(buffer);
char *wdret = 0;
buffer[0] = 0;
wdret = getcwd(buffer,buflen);
if (!wdret) {
printf("getcwd() issue. Do nothing. "
" line %d %s\n",__LINE__,__FILE__);
dwarfstring_list_destructor(&base_dwlist);
destruct_js(&joind);
*errcode = DW_DLE_ALLOC_FAIL;
return DW_DLV_ERROR;
}
dwarfstring_append(&joind.js_cwd,buffer);
dwarfstring_append(&joind.js_dirname,buffer);
_dwarf_pathjoinl(&joind.js_dirname,&joind.js_tmp2);
buffer[0] = 0;
} else {/* else leave js_cwd empty */
dwarfstring_append(&joind.js_dirname,
dwarfstring_string(&joind.js_tmp2));
}
/* Creating a real basename. No slashes. */
dwarfstring_append(&joind.js_basenamesimple,
depath+dirnamelen);
dwarfstring_append(&joind.js_basesimpledebug,
dwarfstring_string(&joind.js_basenamesimple));
dwarfstring_append(&joind.js_basesimpledebug,".debug");
build_buildid_filename(&joind.js_buildid_filename,
buildid_length, buildid);
dwarfstring_reset(&joind.js_tmp2);
/* Now js_dirname / js_basenamesimple
are reflecting a full path */
/* saves the full path to the original
executable */
dwarfstring_append(&joind.js_originalfullpath,
dwarfstring_string(&joind.js_dirname));
_dwarf_pathjoinl(&joind.js_originalfullpath,
&joind.js_basenamesimple);
/* js_originalfullpath is a true full path
to the object we started with as input. */
/* First, do the build-id method if it applies
at all */
for (global_prefix_number = 0;
buildid_length &&
(global_prefix_number < length_global_prefixes_in);
++global_prefix_number) {
char * prefix = 0;
struct dwarfstring_list_s *now_last = 0;
prefix = global_prefixes_in[global_prefix_number];
dwarfstring_reset(&joind.js_buildid);
dwarfstring_append(&joind.js_buildid,prefix);
_dwarf_pathjoinl(&joind.js_buildid,
&joind.js_buildid_filename);
res = dwarfstring_list_add_new(
&base_dwlist,
last_entry,&joind.js_buildid,
&now_last,errcode);
if(res != DW_DLV_OK) {
dwarfstring_list_destructor(&base_dwlist);
destruct_js(&joind);
return res;
}
last_entry = now_last;
} /* end loop on global prefix numbers */
/* end of the build-id name-finding. */
/* Now for the debug link method of finding debug
files. */
if (link_string_in) {
/* First try dir of executable */
struct dwarfstring_list_s *now_last = 0;
dwarfstring_append(&joind.js_linkstring,
link_string_in);
dwarfstring_reset(&joind.js_tmp3);
dwarfstring_reset(&joind.js_tmp2);
prepare_linked_name(&joind.js_tmp3,
&joind.js_dirname,
0,&joind.js_linkstring);
dwarfstring_append(link_string_fullpath_out,
dwarfstring_string(&joind.js_tmp3));
if (!duplicatedpath(&joind.js_originalfullpath,
&joind.js_tmp3)) {
res = dwarfstring_list_add_new(
&base_dwlist,
last_entry,&joind.js_tmp3,
&now_last,errcode);
if(res != DW_DLV_OK) {
dwarfstring_list_destructor(&base_dwlist);
destruct_js(&joind);
return res;
}
last_entry = now_last;
}
dwarfstring_reset(&joind.js_tmp2);
dwarfstring_reset(&joind.js_tmp3);
dwarfstring_append(&joind.js_tmp3,".debug");
prepare_linked_name(&joind.js_tmp2,
&joind.js_dirname,
&joind.js_tmp3,&joind.js_linkstring);
if (! duplicatedpath(&joind.js_originalfullpath,
&joind.js_tmp2)) {
res = dwarfstring_list_add_new(
&base_dwlist,
last_entry,&joind.js_tmp2,
&now_last,errcode);
if(res != DW_DLV_OK) {
dwarfstring_list_destructor(&base_dwlist);
destruct_js(&joind);
return res;
}
last_entry = now_last;
}
/* Now look in the global locations. */
for (global_prefix_number = 0;
global_prefix_number < length_global_prefixes_in;
++global_prefix_number) {
char * prefix =
global_prefixes_in[global_prefix_number];
dwarfstring_reset(&joind.js_tmp2);
dwarfstring_reset(&joind.js_tmp3);
dwarfstring_append(&joind.js_tmp2, prefix);
prepare_linked_name(&joind.js_tmp3,
&joind.js_tmp2,
&joind.js_dirname,
&joind.js_linkstring);
if (!duplicatedpath(&joind.js_originalfullpath,
&joind.js_tmp3)) {
res = dwarfstring_list_add_new(
&base_dwlist,
last_entry,&joind.js_tmp3,
&now_last,errcode);
if(res != DW_DLV_OK) {
dwarfstring_list_destructor(&base_dwlist);
destruct_js(&joind);
return res;
}
last_entry = now_last;
}
}
}
/* Now malloc space for the pointer array
and the strings they point at so a simple
free by our caller will clean up
everything. Copy the data from
base_dwlist to the new area. */
{
struct dwarfstring_list_s *cur = 0;
char **resultfullstring = 0;
unsigned long count = 0;
unsigned long pointerarraysize = 0;
unsigned long sumstringlengths = 0;
unsigned long totalareasize = 0;
unsigned long setptrindex = 0;
unsigned long setstrindex = 0;
cur = &base_dwlist;
for ( ; cur ; cur = cur->dl_next) {
++count;
pointerarraysize += sizeof(void *);
sumstringlengths +=
dwarfstring_strlen(&cur->dl_string) +1;
}
/* Make a final null pointer in the pointer array. */
pointerarraysize += sizeof(void *);
totalareasize = pointerarraysize + sumstringlengths +8;
resultfullstring = (char **)malloc(totalareasize);
setstrindex = pointerarraysize;
if(!resultfullstring) {
dwarfstring_list_destructor(&base_dwlist);
destruct_js(&joind);
*errcode = DW_DLE_ALLOC_FAIL;
return DW_DLV_ERROR;
}
memset(resultfullstring,0,totalareasize);
cur = &base_dwlist;
for ( ; cur ; cur = cur->dl_next,++setptrindex) {
char **iptr = (char **)((char *)resultfullstring +
setptrindex*sizeof(void *));
char *sptr = (char*)resultfullstring + setstrindex;
strcpy(sptr,dwarfstring_string(&cur->dl_string));
setstrindex += dwarfstring_strlen(&cur->dl_string)+1;
*iptr = sptr;
}
*paths_out = resultfullstring;
*paths_out_length = count;
}
dwarfstring_list_destructor(&base_dwlist);
destruct_js(&joind);
return DW_DLV_OK;
}
static int
extract_debuglink(Dwarf_Debug dbg,
struct Dwarf_Section_s * pdebuglink,
char ** name_returned, /* static storage, do not free */
unsigned char ** crc_returned, /* 32bit crc , do not free */
Dwarf_Error *error)
{
Dwarf_Small *ptr = 0;
Dwarf_Small *endptr = 0;
unsigned namelen = 0;
unsigned m = 0;
unsigned incr = 0;
Dwarf_Small *crcptr = 0;
int res = DW_DLV_ERROR;
Dwarf_Unsigned secsize = 0;
if (!pdebuglink->dss_data) {
res = _dwarf_load_section(dbg, pdebuglink,error);
if (res != DW_DLV_OK) {
return res;
}
}
secsize = pdebuglink->dss_size;
ptr = pdebuglink->dss_data;
endptr = ptr + secsize;
res = _dwarf_check_string_valid(dbg,ptr,
ptr, endptr, DW_DLE_FORM_STRING_BAD_STRING,
error);
if ( res != DW_DLV_OK) {
return res;
}
namelen = (unsigned)strlen((const char*)ptr);
m = (namelen+1) %4;
if (m) {
incr = 4 - m;
}
crcptr = (unsigned char *)ptr +namelen +1 +incr;
if ((crcptr +4) != (unsigned char*)endptr) {
_dwarf_error(dbg,error,DW_DLE_CORRUPT_GNU_DEBUGLINK);
return DW_DLV_ERROR;
}
*name_returned = (char *)ptr;
*crc_returned = crcptr;
return DW_DLV_OK;
}
/* The definition of .note.gnu.buildid contents (also
used for other GNU .note.gnu. sections too. */
struct buildid_s {
char bu_ownernamesize[4];
char bu_buildidsize[4];
char bu_type[4];
char bu_owner[1];
};
static int
extract_buildid(Dwarf_Debug dbg,
struct Dwarf_Section_s * pbuildid,
unsigned * type_returned,
char **owner_name_returned,
unsigned char **build_id_returned,
unsigned * build_id_length_returned,
Dwarf_Error *error)
{
Dwarf_Small * ptr = 0;
Dwarf_Small * endptr = 0;
int res = DW_DLV_ERROR;
struct buildid_s *bu = 0;
Dwarf_Unsigned namesize = 0;
Dwarf_Unsigned descrsize = 0;
Dwarf_Unsigned type = 0;
Dwarf_Unsigned finalsize;
Dwarf_Unsigned secsize = 0;
if (!pbuildid->dss_data) {
res = _dwarf_load_section(dbg, pbuildid,error);
if (res != DW_DLV_OK) {
return res;
}
}
secsize = pbuildid->dss_size;
ptr = pbuildid->dss_data;
if (secsize < sizeof(struct buildid_s)) {
_dwarf_error(dbg,error,DW_DLE_CORRUPT_NOTE_GNU_DEBUGID);
return DW_DLV_ERROR;
}
endptr = ptr + secsize;
/* We hold gh_content till all is closed
as we return pointers into it
if all goes well. */
bu = (struct buildid_s *)ptr;
ASNAR(dbg->de_copy_word,namesize, bu->bu_ownernamesize);
ASNAR(dbg->de_copy_word,descrsize,bu->bu_buildidsize);
ASNAR(dbg->de_copy_word,type, bu->bu_type);
if (descrsize != 20) {
_dwarf_error(dbg,error,DW_DLE_CORRUPT_NOTE_GNU_DEBUGID);
return DW_DLV_ERROR;
}
res = _dwarf_check_string_valid(dbg,
(Dwarf_Small *)&bu->bu_owner[0],
(Dwarf_Small *)&bu->bu_owner[0],
endptr,
DW_DLE_CORRUPT_GNU_DEBUGID_STRING,
error);
if ( res != DW_DLV_OK) {
return res;
}
if ((strlen(bu->bu_owner) +1) != namesize) {
_dwarf_error(dbg,error, DW_DLE_CORRUPT_GNU_DEBUGID_STRING);
return res;
}
finalsize = sizeof(struct buildid_s)-1 + namesize + descrsize;
if (finalsize > secsize) {
_dwarf_error(dbg,error, DW_DLE_CORRUPT_GNU_DEBUGID_SIZE);
return DW_DLV_ERROR;
}
*type_returned = type;
*owner_name_returned = &bu->bu_owner[0];
*build_id_length_returned = descrsize;
*build_id_returned = (unsigned char *)ptr +
sizeof(struct buildid_s)-1 + namesize;
return DW_DLV_OK;
}
/* Caller frees space returned by debuglink_fillpath_returned and
The following return pointers into the dbg itself
and are only valid while that dbg is open.
debuglink_path_returned.
crc_returned, buildid_owner_name_returned,
buildid_returned, */
int
dwarf_gnu_debuglink(Dwarf_Debug dbg,
char ** debuglink_path_returned, /* do not free*/
unsigned char ** crc_returned,
char ** debuglink_fullpath_returned, /* caller frees */
unsigned * debuglink_fullpath_length_returned,
unsigned * buildid_type_returned ,
char ** buildid_owner_name_returned,
unsigned char ** buildid_returned,
unsigned * buildid_length_returned,
char *** paths_returned,
unsigned * paths_count_returned,
Dwarf_Error* error)
{
dwarfstring debuglink_fullpath;
int linkres = DW_DLV_ERROR;
int res = DW_DLV_ERROR;
char * pathname = 0;
int buildidres = 0;
int errcode = 0;
struct Dwarf_Section_s * pdebuglink = 0;
struct Dwarf_Section_s * pbuildid = 0;
if(!dbg) {
_dwarf_error(dbg,error,DW_DLE_DBG_NULL);
return DW_DLV_ERROR;
}
if (dbg->de_gnu_debuglink.dss_size) {
pdebuglink = &dbg->de_gnu_debuglink;
}
if (dbg->de_note_gnu_buildid.dss_size) {
pbuildid = &dbg->de_note_gnu_buildid;
}
if (!pdebuglink && !pbuildid) {
return DW_DLV_NO_ENTRY;
}
if (pdebuglink) {
linkres = extract_debuglink(dbg,
pdebuglink,
debuglink_path_returned,
crc_returned,
error);
if (linkres == DW_DLV_ERROR) {
return linkres;
}
}
if (pbuildid) {
buildidres = extract_buildid(dbg,
pbuildid,
buildid_type_returned,
buildid_owner_name_returned,
buildid_returned,
buildid_length_returned,
error);
if (buildidres == DW_DLV_ERROR) {
return buildidres;
}
}
dwarfstring_constructor(&debuglink_fullpath);
pathname = (char *)dbg->de_path;
if (pathname && paths_returned) {
res = _dwarf_construct_linkedto_path(
(char **)dbg->de_gnu_global_paths,
dbg->de_gnu_global_path_count,
pathname,
*debuglink_path_returned,
&debuglink_fullpath,
*crc_returned,
*buildid_returned,
*buildid_length_returned,
paths_returned,
paths_count_returned,
&errcode);
if(res != DW_DLV_OK) {
dwarfstring_destructor(&debuglink_fullpath);
return res;
}
if (dwarfstring_strlen(&debuglink_fullpath)) {
*debuglink_fullpath_returned =
strdup(dwarfstring_string(&debuglink_fullpath));
*debuglink_fullpath_length_returned =
dwarfstring_strlen(&debuglink_fullpath);
}
} else if (paths_count_returned) {
*paths_count_returned = 0;
}
dwarfstring_destructor(&debuglink_fullpath);
return DW_DLV_OK;
}
/* This should be rarely called and most likely
only once (at dbg init time from dwarf_generic_init.c,
see set_global_paths_init()).
Maybe once or twice later.
*/
int
dwarf_add_debuglink_global_path(Dwarf_Debug dbg,
const char *pathname,
Dwarf_Error *error)
{
unsigned glpath_count_in = 0;
unsigned glpath_count_out = 0;
const char **glpaths = 0;
const char * path1 = 0;
glpath_count_in = dbg->de_gnu_global_path_count;
glpath_count_out = glpath_count_in+1;
glpaths = (const char **)malloc(sizeof(char *)*
glpath_count_out);
if (!glpaths) {
_dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
return DW_DLV_ERROR;
}
if (glpath_count_in) {
memcpy(glpaths, dbg->de_gnu_global_paths,
sizeof(char *)*glpath_count_in);
}
path1 = strdup(pathname);
if (!path1) {
free(glpaths);
_dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
return DW_DLV_ERROR;
}
free((char *)dbg->de_gnu_global_paths);
glpaths[glpath_count_in] = path1;
dbg->de_gnu_global_paths = (const char **)glpaths;
dbg->de_gnu_global_path_count = glpath_count_out;
return DW_DLV_OK;
}
|