Debian Codegraph @

src/lib/libdwarf/dwarf_debuglink.c

raw
1 /*
2 Copyright (c) 2019-2021, David Anderson
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with
6 or without modification, are permitted provided that the
7 following conditions are met:
8
9 Redistributions of source code must retain the above
10 copyright notice, this list of conditions and the
11 following disclaimer.
12
13 Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the
15 following disclaimer in the documentation and/or
16 other materials
17 provided with the distribution.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
20 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32
33 #include <config.h>
34
35 #include <stdio.h> /* printf() */
36 #include <stdlib.h> /* free() malloc() */
37 #include <string.h> /* memcpy() memset() strcmp() strdup() strlen() */
38 #ifdef HAVE_STDINT_H
39 #include <stdint.h> /* uintptr_t */
40 #endif /* HAVE_STDINT_H */
41
42 #if defined _WIN32
43 #ifdef HAVE_STDAFX_H
44 #include "stdafx.h"
45 #endif /* HAVE_STDAFX_H */
46 #include <direct.h> /* getcwd */
47 #elif defined HAVE_UNISTD_H
48 #include <unistd.h> /* getcwd */
49 #endif /* _WIN32 */
50
51 #include "dwarf.h"
52 #include "libdwarf.h"
53 #include "libdwarf_private.h"
54 #include "dwarf_base_types.h"
55 #include "dwarf_safe_strcpy.h"
56 #include "dwarf_opaque.h"
57 #include "dwarf_global.h"
58 #include "dwarf_alloc.h"
59 #include "dwarf_error.h"
60 #include "dwarf_util.h"
61 #include "dwarf_string.h"
62 #include "dwarf_debuglink.h"
63
64 #define MINBUFLEN 1000
65
66 static char joinchar = '/';
67 static char* joinstr = "/";
68 static char joincharw = '\\';
69 /* Large enough it is unlikely dwarfstring will ever
70 do a malloc here, Windows paths usually < 256 characters. */
71 #if defined (_WIN32)
72 static char winbuf[256];
73 #endif /* _WIN32 */
74
75 static int
76 is_full_path(char *path)
77 {
78 unsigned char c = path[0];
79 unsigned char c1 = 0;
80 int okdriveletter = FALSE;
81
82 if (!c) {
83 /* empty string. */
84 return FALSE;
85 }
86 if (c == joinchar) {
87 return TRUE;
88 }
89 if (c >= 'c' && c <= 'z') {
90 okdriveletter = TRUE;
91 } else if (c >= 'C' && c <= 'Z') {
92 okdriveletter = TRUE;
93 }
94 if (!okdriveletter) {
95 return FALSE;
96 }
97
98 c1 = path[1];
99 if (!c1) {
100 /* No second character */
101 return FALSE;
102 }
103 if (c1 == ':') {
104 /* Windows full path, we assume
105 We just assume nobody would be silly enough
106 to name a linux/posix directory starting with "C:" */
107 return TRUE;
108 }
109 /* No kind of full path name */
110 return FALSE;
111 }
112
113 static int
114 _dwarf_extract_buildid(Dwarf_Debug dbg,
115 struct Dwarf_Section_s * pbuildid,
116 unsigned *type_returned,
117 char **owner_name_returned,
118 unsigned char **build_id_returned,
119 unsigned *build_id_length_returned,
120 Dwarf_Error *error);
121
122 struct joins_s {
123 dwarfstring js_dirname;
124 dwarfstring js_basenamesimple;
125 dwarfstring js_basesimpledebug;
126 dwarfstring js_binname;
127 dwarfstring js_cwd;
128 dwarfstring js_originalfullpath;
129 dwarfstring js_linkstring;
130 dwarfstring js_tmp2;
131 dwarfstring js_tmp3;
132 dwarfstring js_buildid;
133 dwarfstring js_buildid_filename;
134 };
135
136 static void
137 construct_js(struct joins_s * js)
138 {
139 memset(js,0,sizeof(struct joins_s));
140 dwarfstring_constructor(&js->js_dirname);
141 dwarfstring_constructor(&js->js_basenamesimple);
142 dwarfstring_constructor(&js->js_basesimpledebug);
143 dwarfstring_constructor(&js->js_cwd);
144 dwarfstring_constructor(&js->js_originalfullpath);
145 dwarfstring_constructor(&js->js_linkstring);
146 dwarfstring_constructor(&js->js_tmp2);
147 dwarfstring_constructor(&js->js_tmp3);
148 dwarfstring_constructor(&js->js_buildid);
149 dwarfstring_constructor(&js->js_buildid_filename);
150 }
151 static void
152 destruct_js(struct joins_s * js)
153 {
154 dwarfstring_destructor(&js->js_dirname);
155 dwarfstring_destructor(&js->js_basenamesimple);
156 dwarfstring_destructor(&js->js_basesimpledebug);
157 dwarfstring_destructor(&js->js_cwd);
158 dwarfstring_destructor(&js->js_originalfullpath);
159 dwarfstring_destructor(&js->js_linkstring);
160 dwarfstring_destructor(&js->js_tmp2);
161 dwarfstring_destructor(&js->js_tmp3);
162 dwarfstring_destructor(&js->js_buildid);
163 dwarfstring_destructor(&js->js_buildid_filename);
164 }
165
166 /* Altering Windows \ to posix /
167 which is sort of useful in mingw64 msys2.
168 Simply assuming no one would use \ in
169 a file/directory name in posix. */
170 static void
171 transform_to_posix_slash(dwarfstring * out, char *in)
172 {
173 char *cp = in;
174 int c = 0;
175
176 for (c = *cp ; c ; ++cp,c = *cp) {
177 if ( *cp == joincharw ) {
178 dwarfstring_append_length(out,joinstr,1);
179 } else {
180 dwarfstring_append_length(out,cp,1);
181 }
182 }
183 }
184
185 /* We change c: or C: to /c for example */
186 static void
187 transform_leading_windowsletter(dwarfstring *out,
188 char *in)
189 {
190 char *cp = in;
191 char c = 0;
192 int isupper = FALSE;
193 int islower = FALSE;
194
195 if (!cp) {
196 /* Nothing to do here. */
197 return;
198 }
199 c = *cp++;
200 /* Unsure if a, b are normal Windows drive letters. */
201 if (c && (c >= 'C') && (c <= 'Z')) {
202 isupper = TRUE;
203 } else if (c && (c >= 'c') && (c <= 'z')) {
204 islower = TRUE;
205 }
206 if (isupper || islower) {
207 int c2 = 0;
208
209 /* ++ here to move past the colon in the input */
210 c2 = *cp++;
211 if (c2 && (c2 == ':')) {
212 /* Example: Turn uppercase C to c */
213 char newstr[4];
214 char newletter = c;
215
216 if (isupper) {
217 char distance = 'a' - 'A';
218 newletter = c + distance;
219 }
220 newstr[0] = '/';
221 newstr[1] = newletter;
222 newstr[2] = 0;
223 dwarfstring_append(out,newstr);
224 } else {
225 cp = in;
226 }
227 } else {
228 cp = in;
229 }
230 transform_to_posix_slash(out,cp);
231 }
232
233 int
234 _dwarf_pathjoinl(dwarfstring *target,dwarfstring * input)
235 {
236 char *inputs = dwarfstring_string(input);
237 char *targ = dwarfstring_string(target);
238 size_t targlenszt = 0;
239 #if defined (_WIN32)
240 /* Assuming we are in mingw msys2 or equivalent.
241 If we are reading a Windows object file but
242 running non-Windows this won't happen
243 but debuglink won't be useful anyway. */
244 dwarfstring winput;
245
246 dwarfstring_constructor_static(&winput,winbuf,sizeof(winbuf));
247 transform_to_posix_slash(&winput,inputs);
248 targlenszt = dwarfstring_strlen(target);
249 inputs = dwarfstring_string(&winput);
250 if (!targlenszt) {
251 dwarfstring_append(target,inputs);
252 return DW_DLV_OK;
253 }
254 #else /* !_Windows */
255 targlenszt = dwarfstring_strlen(target);
256 #endif /* _WIN32 */
257
258 if (!targlenszt) {
259 dwarfstring_append(target,inputs);
260 return DW_DLV_OK;
261 }
262 targ = dwarfstring_string(target);
263 if (!is_full_path(targ+targlenszt-1)) {
264 if (!is_full_path(inputs)) {
265 dwarfstring_append(target,joinstr);
266 dwarfstring_append(target,inputs);
267 } else {
268 dwarfstring_append(target,inputs);
269 }
270 } else {
271 if (!is_full_path(inputs)) {
272 dwarfstring_append(target,inputs);
273 } else {
274 dwarfstring_append(target,inputs+1);
275 }
276 }
277 #if defined (_WIN32)
278 dwarfstring_destructor(&winput);
279 #endif /* _WIN32 */
280 return DW_DLV_OK;
281 }
282 /* Find the length of the prefix before the final part of the path.
283 ASSERT: the last character in s is not a / */
284 static size_t
285 mydirlen(char *s)
286 {
287 char *cp = 0;
288 char *lastjoinchar = 0;
289 size_t count =0;
290
291 for (cp = s ; *cp ; ++cp,++count) {
292 if (*cp == joinchar) {
293 lastjoinchar = cp;
294 }
295 }
296 if (lastjoinchar) {
297 /* ptrdiff_t is generated but not named */
298 Dwarf_Unsigned sizetoendjoin =
299 (lastjoinchar >= s)?
300 (Dwarf_Unsigned)(uintptr_t)lastjoinchar -
301 (Dwarf_Unsigned)(uintptr_t)s
302 :(Dwarf_Unsigned)0xffffffff;
303 /* count the last join as mydirlen. */
304 if (sizetoendjoin == 0xffffffff) {
305 /* impossible. */
306 return 0;
307 }
308 return sizetoendjoin;
309 }
310 return 0;
311 }
312
313 struct dwarfstring_list_s {
314 dwarfstring dl_string;
315 struct dwarfstring_list_s *dl_next;
316 };
317
318 static void
319 dwarfstring_list_constructor(struct dwarfstring_list_s *l)
320 {
321 dwarfstring_constructor(&l->dl_string);
322 l->dl_next = 0;
323 }
324
325 static int
326 dwarfstring_list_add_new(struct dwarfstring_list_s * base_entry,
327 struct dwarfstring_list_s *prev,
328 dwarfstring * input,
329 struct dwarfstring_list_s ** new_out,
330 int *errcode)
331 {
332 struct dwarfstring_list_s *next = 0;
333
334 if (prev) {
335 next = ( struct dwarfstring_list_s *)
336 malloc(sizeof(struct dwarfstring_list_s));
337 if (!next) {
338 *errcode = DW_DLE_ALLOC_FAIL;
339 return DW_DLV_ERROR;
340 }
341 dwarfstring_list_constructor(next);
342 } else {
343 next = base_entry;
344 }
345 dwarfstring_append(&next->dl_string,
346 dwarfstring_string(input));
347 if (prev) {
348 prev->dl_next = next;
349 }
350 *new_out = next;
351 return DW_DLV_OK;
352 }
353
354 /* destructs passed in entry (does not free it) and all
355 those on the dl_next list (those are freed). */
356 static void
357 dwarfstring_list_destructor(struct dwarfstring_list_s *l)
358 {
359 struct dwarfstring_list_s *curl = l;
360 struct dwarfstring_list_s *nextl = l;
361
362 nextl = curl->dl_next;
363 dwarfstring_destructor(&curl->dl_string);
364 curl->dl_next = 0;
365 curl = nextl;
366 for ( ; curl ; curl = nextl) {
367 nextl = curl->dl_next;
368 dwarfstring_destructor(&curl->dl_string);
369 curl->dl_next = 0;
370 free(curl);
371 }
372 }
373
374 static void
375 prepare_linked_name(dwarfstring *out,
376 dwarfstring *dirname,
377 dwarfstring *extradir,
378 dwarfstring *linkname)
379 {
380 dwarfstring_append(out, dwarfstring_string(dirname));
381 if (extradir) {
382 _dwarf_pathjoinl(out,extradir);
383 }
384 _dwarf_pathjoinl(out,linkname);
385 }
386
387 static void
388 build_buildid_filename(dwarfstring *target,
389 unsigned buildid_length,
390 unsigned char *buildid)
391 {
392 dwarfstring tmp;
393 unsigned bu = 0;
394 unsigned char *cp = 0;
395
396 dwarfstring_constructor(&tmp);
397 dwarfstring_append(&tmp,".build-id");
398 dwarfstring_append(&tmp,joinstr);
399 cp = buildid;
400 for (bu = 0; bu < buildid_length; ++bu ,++cp) {
401 dwarfstring_append_printf_u(&tmp, "%02x",*cp);
402 if (bu == 0) {
403 dwarfstring_append(&tmp,"/");
404 }
405 }
406 _dwarf_pathjoinl(target,&tmp);
407 dwarfstring_append(target,".debug");
408 dwarfstring_destructor(&tmp);
409 return;
410 }
411
412 #if 0 /* dump_bytes */
413 static void
414 dump_bytes(const char *msg,unsigned char * start, unsigned len)
415 {
416 Dwarf_Small *end = start + len;
417 Dwarf_Small *cur = start;
418 printf("%s (0x%lx) ",msg,(unsigned long)start);
419 for (; cur < end; cur++) {
420 printf("%02x", *cur);
421 }
422 printf("\n");
423 }
424 #endif /*0*/
425
426 /* If the compiler or the builder of the object file
427 being read has made a mistake it is possible
428 for a constructed name to match the original
429 file path, yet we do not want to add that to
430 the search path. */
431 static int
432 duplicatedpath(dwarfstring* l,
433 dwarfstring*r)
434 {
435 if (strcmp(dwarfstring_string(l),
436 dwarfstring_string(r))) {
437 return FALSE;
438 }
439 /* Oops. somebody made a mistake */
440 return TRUE;
441 }
442
443 static int
444 _dwarf_do_debuglink_setup(char *link_string_in,
445 dwarfstring * link_string_fullpath_out,
446 char ** global_prefixes_in,
447 unsigned length_global_prefixes_in,
448 struct dwarfstring_list_s **last_entry,
449 struct joins_s *joind,
450 struct dwarfstring_list_s* base_dwlist,
451 int *errcode)
452 {
453 int res = 0;
454
455 if (!link_string_in) {
456 return DW_DLV_OK;
457 }
458 {
459 /* First try dir of executable */
460 struct dwarfstring_list_s *now_last = 0;
461 unsigned global_prefix_number = 0;
462
463 dwarfstring_append(&joind->js_linkstring,
464 link_string_in);
465 dwarfstring_reset(&joind->js_tmp3);
466 dwarfstring_reset(&joind->js_tmp2);
467 prepare_linked_name(&joind->js_tmp3,
468 &joind->js_dirname,
469 0,&joind->js_linkstring);
470 dwarfstring_append(
471 link_string_fullpath_out,
472 dwarfstring_string(&joind->js_tmp3));
473 if (!duplicatedpath(&joind->js_originalfullpath,
474 &joind->js_tmp3)) {
475 res = dwarfstring_list_add_new(
476 base_dwlist,
477 *last_entry,&joind->js_tmp3,
478 &now_last,errcode);
479 if (res != DW_DLV_OK) {
480 return res;
481 }
482 *last_entry = now_last;
483 }
484 dwarfstring_reset(&joind->js_tmp2);
485 dwarfstring_reset(&joind->js_tmp3);
486 dwarfstring_append(&joind->js_tmp3,".debug");
487 prepare_linked_name(&joind->js_tmp2,
488 &joind->js_dirname,
489 &joind->js_tmp3,&joind->js_linkstring);
490 if (! duplicatedpath(&joind->js_originalfullpath,
491 &joind->js_tmp2)) {
492 res = dwarfstring_list_add_new(
493 base_dwlist,
494 *last_entry,&joind->js_tmp2,
495 &now_last,errcode);
496 if (res != DW_DLV_OK) {
497 return res;
498 }
499 *last_entry = now_last;
500 } else {
501 }
502 /* Now look in the global locations. */
503 for (global_prefix_number = 0;
504 global_prefix_number < length_global_prefixes_in;
505 ++global_prefix_number) {
506 char * prefix =
507 global_prefixes_in[global_prefix_number];
508
509 dwarfstring_reset(&joind->js_tmp2);
510 dwarfstring_reset(&joind->js_tmp3);
511 dwarfstring_append(&joind->js_tmp2, prefix);
512 prepare_linked_name(&joind->js_tmp3,
513 &joind->js_tmp2,
514 &joind->js_dirname,
515 &joind->js_linkstring);
516 if (!duplicatedpath(&joind->js_originalfullpath,
517 &joind->js_tmp3)) {
518 res = dwarfstring_list_add_new(
519 base_dwlist,
520 *last_entry,&joind->js_tmp3,
521 &now_last,errcode);
522 if (res != DW_DLV_OK) {
523 return res;
524 }
525 *last_entry = now_last;
526 } else {
527 }
528 }
529 }
530 return DW_DLV_OK;
531 }
532
533 static int
534 _dwarf_do_buildid_setup(unsigned buildid_length,
535 char ** global_prefixes_in,
536 unsigned length_global_prefixes_in,
537 struct dwarfstring_list_s **last_entry,
538 struct joins_s *joind,
539 struct dwarfstring_list_s* base_dwlist,
540 int *errcode)
541 {
542 unsigned global_prefix_number = 0;
543 int res = 0;
544
545 if (!buildid_length) {
546 return DW_DLV_OK;
547 }
548 for (global_prefix_number = 0;
549 buildid_length &&
550 (global_prefix_number < length_global_prefixes_in);
551 ++global_prefix_number) {
552 char * prefix = 0;
553 struct dwarfstring_list_s *now_last = 0;
554
555 prefix = global_prefixes_in[global_prefix_number];
556 dwarfstring_reset(&joind->js_buildid);
557 dwarfstring_append(&joind->js_buildid,prefix);
558 _dwarf_pathjoinl(&joind->js_buildid,
559 &joind->js_buildid_filename);
560 res = dwarfstring_list_add_new(
561 base_dwlist,
562 *last_entry,&joind->js_buildid,
563 &now_last,errcode);
564 if (res != DW_DLV_OK) {
565 return res;
566 }
567 *last_entry = now_last;
568 }
569 return DW_DLV_OK;
570 }
571
572 /* New September 2019. Access to the GNU section named
573 .gnu_debuglink
574 See
575 https://sourceware.org/gdb/onlinedocs/
576 gdb/Separate-Debug-Files.html
577 The default global path was set
578 to /usr/lib/debug
579 by set_global_paths_init()
580 called at libdwarf object init time.
581 So there is always the default
582 global path present.
583 From the above web page:
584 For the “debug link” method, GDB looks up the named
585 file in the directory of the executable file, then
586 in a subdirectory of that directory named .debug, and
587 finally under each one of the global debug directories,
588 in a subdirectory whose name is identical to the leading
589 directories of the executable’s absolute file name. (On
590 MS-Windows/MS-DOS, the drive letter of the executable’s
591 leading directories is converted to a one-letter
592 subdirectory, i.e. d:/usr/bin/ is converted to /d/usr/bin/,
593 because Windows filesystems disallow colons in file names.)
594
595 For the “build ID” method, GDB looks in the .build-id
596 subdirectory of each one of the global debug directories
597 for a file named nn/nnnnnnnn.debug, where nn are the
598 first 2 hex characters of the build ID bit string, and
599 nnnnnnnn are the rest of the bit string. (Real build ID
600 strings are 32 or more hex characters, not 10.) GDB can
601 automatically query debuginfod servers using build IDs
602 in order to download separate debug files that cannot be
603 found locally.
604 */
605 int
606 _dwarf_construct_linkedto_path(
607 char **global_prefixes_in,
608 unsigned length_global_prefixes_in,
609 char *pathname_in,
610 char *link_string_in, /* from debug link */
611 dwarfstring *link_string_fullpath_out,
612 unsigned char *buildid, /* from gnu buildid */
613 unsigned buildid_length, /* from gnu buildid */
614 char ***paths_out,
615 unsigned *paths_out_length,
616 int *errcode)
617 {
618 char * pathname = pathname_in;
619 int res = 0;
620 struct joins_s joind;
621 size_t dirnamelen = 0;
622 struct dwarfstring_list_s base_dwlist;
623 struct dwarfstring_list_s *last_entry = 0;
624
625 dwarfstring_list_constructor(&base_dwlist);
626 construct_js(&joind);
627 dirnamelen = mydirlen(pathname);
628 if (dirnamelen) {
629 /* Original dirname, before cwd (if needed) */
630 dwarfstring_append_length(&joind.js_tmp2,
631 pathname,dirnamelen);
632 }
633 if (!is_full_path(pathname)) {
634 /* Meaning a/b or b, not /a/b or /b ,
635 so we apply cwd */
636 char buffer[2000];
637 unsigned buflen= sizeof(buffer);
638 char *wdret = 0;
639
640 buffer[0] = 0;
641 wdret = getcwd(buffer,buflen);
642 if (!wdret) {
643 /* printf("getcwd() issue. Do nothing. "
644 " line %d %s\n",__LINE__,__FILE__); */
645 dwarfstring_list_destructor(&base_dwlist);
646 destruct_js(&joind);
647 *errcode = DW_DLE_ALLOC_FAIL;
648 return DW_DLV_ERROR;
649 }
650 dwarfstring_append(&joind.js_cwd,buffer);
651 }
652 /* Applies to the leading chars in the named file */
653 if (dwarfstring_strlen(&joind.js_tmp2) > 0) {
654 transform_leading_windowsletter(&joind.js_dirname,
655 dwarfstring_string(&joind.js_tmp2));
656 }
657 dwarfstring_reset(&joind.js_tmp2);
658
659 /* Creating a real basename. No slashes. */
660 dwarfstring_append(&joind.js_basenamesimple,
661 pathname+dirnamelen);
662 dwarfstring_append(&joind.js_basesimpledebug,
663 dwarfstring_string(&joind.js_basenamesimple));
664 dwarfstring_append(&joind.js_basesimpledebug,".debug");
665
666 /* Now js_dirname / js_basenamesimple
667 are reflecting a full path */
668
669 /* saves the full path to the original
670 executable */
671 dwarfstring_append(&joind.js_originalfullpath,
672 dwarfstring_string(&joind.js_dirname));
673 _dwarf_pathjoinl(&joind.js_originalfullpath,
674 &joind.js_basenamesimple);
675
676 build_buildid_filename(&joind.js_buildid_filename,
677 buildid_length, buildid);
678 /* The GNU buildid method of finding debug files.
679 No crc calculation required ever. */
680 res = _dwarf_do_buildid_setup(buildid_length,
681 global_prefixes_in,
682 length_global_prefixes_in,
683 &last_entry,
684 &joind,
685 &base_dwlist,errcode);
686 if (res == DW_DLV_ERROR) {
687 dwarfstring_list_destructor(&base_dwlist);
688 destruct_js(&joind);
689 return res;
690 }
691
692 /* The debug link method of finding debug files.
693 A crc calculation is required for full
694 checking. Which can be slow if an object file is large. */
695 res = _dwarf_do_debuglink_setup(link_string_in,
696 link_string_fullpath_out,
697 global_prefixes_in,
698 length_global_prefixes_in,
699 &last_entry,
700 &joind,&base_dwlist,errcode);
701 if (res == DW_DLV_ERROR) {
702 dwarfstring_list_destructor(&base_dwlist);
703 destruct_js(&joind);
704 return res;
705 }
706
707 /* Now malloc space for the pointer array
708 and the strings they point at so a simple
709 free by our caller will clean up
710 everything. Copy the data from
711 base_dwlist to the new area. */
712 {
713 struct dwarfstring_list_s *cur = 0;
714 char **resultfullstring = 0;
715 unsigned long count = 0;
716 size_t pointerarraysize = 0;
717 size_t sumstringlengths = 0;
718 size_t totalareasize = 0;
719 size_t setptrindex = 0;
720 size_t setstrindex = 0;
721
722 cur = &base_dwlist;
723 for ( ; cur ; cur = cur->dl_next) {
724 ++count;
725 pointerarraysize += sizeof(void *);
726 sumstringlengths +=
727 dwarfstring_strlen(&cur->dl_string) +1;
728 }
729 /* Make a final null pointer in the pointer array. */
730 pointerarraysize += sizeof(void *);
731 totalareasize = pointerarraysize + sumstringlengths +8;
732 resultfullstring = (char **)malloc(totalareasize);
733 setstrindex = pointerarraysize;
734 if (!resultfullstring) {
735 dwarfstring_list_destructor(&base_dwlist);
736 destruct_js(&joind);
737 *errcode = DW_DLE_ALLOC_FAIL;
738 return DW_DLV_ERROR;
739 }
740 memset(resultfullstring,0,totalareasize);
741 cur = &base_dwlist;
742
743 for ( ; cur ; cur = cur->dl_next,++setptrindex) {
744 char **iptr = (char **)((char *)resultfullstring +
745 setptrindex*sizeof(void *));
746 char *sptr = (char*)resultfullstring + setstrindex;
747 char *msg = dwarfstring_string(&cur->dl_string);
748 size_t slen = dwarfstring_strlen(&cur->dl_string);
749
750 _dwarf_safe_strcpy(sptr,totalareasize - setstrindex,
751 msg,slen);
752 setstrindex += slen +1;
753 *iptr = sptr;
754 }
755 *paths_out = resultfullstring;
756 *paths_out_length = count;
757 }
758 dwarfstring_list_destructor(&base_dwlist);
759 destruct_js(&joind);
760 return DW_DLV_OK;
761 }
762
763 static int
764 _dwarf_extract_debuglink(Dwarf_Debug dbg,
765 struct Dwarf_Section_s * pdebuglink,
766 char ** name_returned, /* static storage, do not free */
767 unsigned char ** crc_returned, /* 32bit crc , do not free */
768 Dwarf_Error *error)
769 {
770 Dwarf_Small *ptr = 0;
771 Dwarf_Small *endptr = 0;
772 size_t namelenszt = 0;
773 unsigned m = 0;
774 unsigned incr = 0;
775 Dwarf_Small *crcptr = 0;
776 int res = DW_DLV_ERROR;
777 Dwarf_Unsigned secsize = 0;
778
779 if (!pdebuglink->dss_data) {
780 return DW_DLV_NO_ENTRY;
781 }
782 secsize = pdebuglink->dss_size;
783 ptr = pdebuglink->dss_data;
784 endptr = ptr + secsize;
785
786 res = _dwarf_check_string_valid(dbg,ptr,
787 ptr, endptr, DW_DLE_FORM_STRING_BAD_STRING,
788 error);
789 if ( res != DW_DLV_OK) {
790 return res;
791 }
792 namelenszt = strlen((const char*)ptr);
793 m = (namelenszt+1) %4;
794 if (m) {
795 incr = 4 - m;
796 }
797 crcptr = (unsigned char *)ptr +namelenszt +1 +incr;
798 if ((crcptr +4) != (unsigned char*)endptr) {
799 _dwarf_error(dbg,error,DW_DLE_CORRUPT_GNU_DEBUGLINK);
800 return DW_DLV_ERROR;
801 }
802 *name_returned = (char *)ptr;
803 *crc_returned = crcptr;
804 return DW_DLV_OK;
805 }
806
807 /* The definition of .note.gnu.buildid contents (also
808 used for other GNU .note.gnu. sections too. */
809 struct buildid_s {
810 char bu_ownernamesize[4];
811 char bu_buildidsize[4];
812 char bu_type[4];
813 char bu_owner[1];
814 };
815
816 static int
817 _dwarf_extract_buildid(Dwarf_Debug dbg,
818 struct Dwarf_Section_s * pbuildid,
819 unsigned * type_returned,
820 char **owner_name_returned,
821 unsigned char **build_id_returned,
822 unsigned * build_id_length_returned,
823 Dwarf_Error *error)
824 {
825 Dwarf_Small * ptr = 0;
826 Dwarf_Small * endptr = 0;
827 int res = DW_DLV_ERROR;
828 struct buildid_s *bu = 0;
829 Dwarf_Unsigned namesize = 0;
830 Dwarf_Unsigned descrsize = 0;
831 Dwarf_Unsigned type = 0;
832 Dwarf_Unsigned finalsize;
833 Dwarf_Unsigned secsize = 0;
834
835 if (!pbuildid->dss_data) {
836 return DW_DLV_NO_ENTRY;
837 }
838 secsize = pbuildid->dss_size;
839 ptr = pbuildid->dss_data;
840 if (secsize < sizeof(struct buildid_s)) {
841 _dwarf_error(dbg,error,DW_DLE_CORRUPT_NOTE_GNU_DEBUGID);
842 return DW_DLV_ERROR;
843 }
844 endptr = ptr + secsize;
845 /* We hold gh_content till all is closed
846 as we return pointers into it
847 if all goes well. */
848 bu = (struct buildid_s *)ptr;
849 ASNAR(dbg->de_copy_word,namesize, bu->bu_ownernamesize);
850 ASNAR(dbg->de_copy_word,descrsize,bu->bu_buildidsize);
851 if (descrsize >= secsize) {
852 _dwarf_error_string(dbg,error,
853 DW_DLE_BUILD_ID_DESCRIPTION_SIZE,
854 "DW_DLE_BUILD_ID_DESCRIPTION_SIZE Size is much too "
855 "large to be correct. Corrupt Dwarf");
856 return DW_DLV_ERROR;
857 }
858 if ((descrsize+8) >= secsize) {
859 _dwarf_error_string(dbg,error,
860 DW_DLE_BUILD_ID_DESCRIPTION_SIZE,
861 "DW_DLE_BUILD_ID_DESCRIPTION_SIZE Size is too "
862 "large to be correct. Corrupt Dwarf");
863 return DW_DLV_ERROR;
864 }
865 if (descrsize >= DW_BUILDID_SANE_SIZE) {
866 _dwarf_error_string(dbg,error,
867 DW_DLE_BUILD_ID_DESCRIPTION_SIZE,
868 "DW_DLE_BUILD_ID_DESCRIPTION_SIZE Size is too "
869 "large to be sane. Corrupt Dwarf");
870 return DW_DLV_ERROR;
871 }
872
873 ASNAR(dbg->de_copy_word,type, bu->bu_type);
874 /* descrsize test is no longer appropriate, other lengths
875 than 20 may exist. --Wl,--build-id= can have
876 at least one of 'fast', 'md5', 'sha1', 'uuid' or
877 possibly others so we should not check the length. */
878 res = _dwarf_check_string_valid(dbg,
879 (Dwarf_Small *)&bu->bu_owner[0],
880 (Dwarf_Small *)&bu->bu_owner[0],
881 endptr,
882 DW_DLE_CORRUPT_GNU_DEBUGID_STRING,
883 error);
884 if ( res != DW_DLV_OK) {
885 return res;
886 }
887 if ((strlen(bu->bu_owner) +1) != namesize) {
888 _dwarf_error(dbg,error, DW_DLE_CORRUPT_GNU_DEBUGID_STRING);
889 return DW_DLV_ERROR;
890 }
891
892 finalsize = sizeof(struct buildid_s)-1 + namesize + descrsize;
893 if (finalsize > secsize) {
894 _dwarf_error(dbg,error, DW_DLE_CORRUPT_GNU_DEBUGID_SIZE);
895 return DW_DLV_ERROR;
896 }
897 *type_returned = (unsigned)type;
898 *owner_name_returned = &bu->bu_owner[0];
899 if (descrsize >= secsize) { /* In case value insanely large */
900 dwarfstring m;
901
902 dwarfstring_constructor(&m);
903 dwarfstring_append_printf_u(&m,
904 "DW_DLE_CORRUPT_NOTE_GNU_DEBUGID buildid description"
905 "length %u larger than the section size. "
906 "Corrupt object section",descrsize);
907 _dwarf_error_string(dbg,error,
908 DW_DLE_CORRUPT_GNU_DEBUGID_SIZE,
909 dwarfstring_string(&m));
910 dwarfstring_destructor(&m);
911 return DW_DLV_ERROR;
912 }
913 if ((descrsize+8) >= secsize) { /* In case a bit too large */
914 dwarfstring m;
915
916 dwarfstring_constructor(&m);
917 dwarfstring_append_printf_u(&m,
918 "DW_DLE_CORRUPT_NOTE_GNU_DEBUGID buildid description"
919 "length %u larger than is appropriate. "
920 "Corrupt object section",descrsize);
921 _dwarf_error_string(dbg,error,
922 DW_DLE_CORRUPT_GNU_DEBUGID_SIZE,
923 dwarfstring_string(&m));
924 dwarfstring_destructor(&m);
925 return DW_DLV_ERROR;
926 }
927 *build_id_length_returned = (unsigned)descrsize;
928 *build_id_returned = (unsigned char *)ptr +
929 sizeof(struct buildid_s)-1 + namesize;
930 return DW_DLV_OK;
931 }
932
933 /* Caller frees space returned by debuglink_fillpath_returned and
934 The following return pointers into the dbg itself
935 and are only valid while that dbg is open.
936 debuglink_path_returned.
937 crc_returned, buildid_owner_name_returned,
938 buildid_returned,
939
940 Caller must initialize the passed-in pointers
941 debuglink_fullpath_returned
942 and paths_returned to zero, and other arguments
943 to zero or sensible values before calling.
944
945 If the dbg was set up via dwarf_init_b(fd... )
946 then dbg->de_path will be 0, a null pointer
947 which makes some of this less than useful.
948 */
949 int
950 dwarf_gnu_debuglink(Dwarf_Debug dbg,
951 char ** debuglink_path_returned, /* do not free*/
952 unsigned char ** crc_returned,
953 char ** debuglink_fullpath_returned, /* caller frees */
954 unsigned * debuglink_fullpath_length_returned,
955
956 unsigned * buildid_type_returned ,
957 char ** buildid_owner_name_returned,
958 unsigned char ** buildid_returned,
959 unsigned * buildid_length_returned,
960 char *** paths_returned, /* caller frees */
961 unsigned * paths_count_returned,
962 Dwarf_Error* error)
963 {
964 dwarfstring debuglink_fullpath;
965 int res = DW_DLV_ERROR;
966 char * pathname = 0;
967 int errcode = 0;
968 struct Dwarf_Section_s * pdebuglink = 0;
969 struct Dwarf_Section_s * pbuildid = 0;
970
971 CHECK_DBG(dbg,error,"dwarf_gnu_debuglink()");
972 if (dbg->de_gnu_debuglink.dss_size) {
973 pdebuglink = &dbg->de_gnu_debuglink;
974 res = _dwarf_load_section(dbg, pdebuglink,error);
975 if (res == DW_DLV_ERROR) {
976 return res;
977 }
978 }
979 if (dbg->de_note_gnu_buildid.dss_size) {
980 pbuildid = &dbg->de_note_gnu_buildid;
981 res = _dwarf_load_section(dbg, pbuildid,error);
982 if (res == DW_DLV_ERROR) {
983 return res;
984 }
985 }
986 if (!pdebuglink && !pbuildid) {
987 if (dbg->de_path) {
988 *debuglink_fullpath_returned = strdup(dbg->de_path);
989 *debuglink_fullpath_length_returned =
990 (unsigned)strlen(dbg->de_path);
991 } else {
992 *debuglink_fullpath_returned = 0;
993 *debuglink_fullpath_length_returned = 0;
994 }
995 return DW_DLV_OK;
996 }
997
998 if (pbuildid) {
999 /* buildid does not involve calculating a crc at any point */
1000 int buildidres = 0;
1001 buildidres = _dwarf_extract_buildid(dbg,
1002 pbuildid,
1003 buildid_type_returned,
1004 buildid_owner_name_returned,
1005 buildid_returned,
1006 buildid_length_returned,
1007 error);
1008 if (buildidres == DW_DLV_ERROR) {
1009 return buildidres;
1010 }
1011 }
1012 if (pdebuglink) {
1013 int linkres = 0;
1014
1015 linkres = _dwarf_extract_debuglink(dbg,
1016 pdebuglink,
1017 debuglink_path_returned,
1018 crc_returned,
1019 error);
1020 if (linkres == DW_DLV_ERROR) {
1021 return linkres;
1022 }
1023 }
1024 dwarfstring_constructor(&debuglink_fullpath);
1025 pathname = dbg->de_path?(char *)dbg->de_path:
1026 "";
1027 if (pathname && paths_returned) {
1028 /* Caller wants paths created and returned. */
1029 res = _dwarf_construct_linkedto_path(
1030 (char **)dbg->de_gnu_global_paths,
1031 dbg->de_gnu_global_path_count,
1032 pathname,
1033 *debuglink_path_returned,
1034 &debuglink_fullpath,
1035 *buildid_returned,
1036 *buildid_length_returned,
1037 paths_returned,
1038 paths_count_returned,
1039 &errcode);
1040 if (res == DW_DLV_ERROR) {
1041 dwarfstring_destructor(&debuglink_fullpath);
1042 return res;
1043 }
1044 if (dwarfstring_strlen(&debuglink_fullpath)) {
1045 *debuglink_fullpath_returned =
1046 strdup(dwarfstring_string(&debuglink_fullpath));
1047 *debuglink_fullpath_length_returned =
1048 (unsigned)dwarfstring_strlen(&debuglink_fullpath);
1049 }
1050 } else if (paths_count_returned) {
1051 *paths_count_returned = 0;
1052 } else {
1053 /* nothing special to do */
1054 }
1055 dwarfstring_destructor(&debuglink_fullpath);
1056 return DW_DLV_OK;
1057 }
1058
1059 /* This should be rarely called and most likely
1060 only once (at dbg init time from dwarf_generic_init.c,
1061 see set_global_paths_init()).
1062 Maybe once or twice later.
1063 */
1064 int
1065 dwarf_add_debuglink_global_path(Dwarf_Debug dbg,
1066 const char *pathname,
1067 Dwarf_Error *error)
1068 {
1069 unsigned glpath_count_in = 0;
1070 unsigned glpath_count_out = 0;
1071 char **glpaths = 0;
1072 char *path1 = 0;
1073
1074 CHECK_DBG(dbg,error,"dwarf_add_debuglink_global_path()");
1075 glpath_count_in = dbg->de_gnu_global_path_count;
1076 glpath_count_out = glpath_count_in+1;
1077 glpaths = (char **)malloc(sizeof(char *)*
1078 glpath_count_out);
1079 if (!glpaths) {
1080 _dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
1081 return DW_DLV_ERROR;
1082 }
1083 if (glpath_count_in) {
1084 memcpy(glpaths, dbg->de_gnu_global_paths,
1085 sizeof(char *)*glpath_count_in);
1086 }
1087 path1 = strdup(pathname);
1088 if (!path1) {
1089 free(glpaths);
1090 _dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
1091 return DW_DLV_ERROR;
1092 }
1093 free(dbg->de_gnu_global_paths);
1094 glpaths[glpath_count_in] = path1;
1095 dbg->de_gnu_global_paths = (const char **)glpaths;
1096 dbg->de_gnu_global_path_count = glpath_count_out;
1097 return DW_DLV_OK;
1098 }