Debian Codegraph @

src/lib/libdwarf/dwarf_object_detector.c

raw
1 /*
2 Copyright (c) 2018-2020, David Anderson All rights reserved.
3
4 Redistribution and use in source and binary forms, with
5 or without modification, are permitted provided that the
6 following conditions are met:
7
8 Redistributions of source code must retain the above
9 copyright notice, this list of conditions and the following
10 disclaimer.
11
12 Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials
15 provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <config.h>
33
34 #include <stdlib.h> /* free() */
35 #include <stdio.h> /* SEEK_END SEEK_SET */
36 #include <string.h> /* memset() strlen() */
37
38 #include "dwarf.h"
39 #include "libdwarf.h"
40 #include "libdwarf_private.h"
41 #include "dwarf_base_types.h"
42 #include "dwarf_opaque.h"
43 #include "dwarf_memcpy_swap.h"
44 #include "dwarf_object_read_common.h"
45 #include "dwarf_object_detector.h"
46 #include "dwarf_macho_loader.h"
47 #include "dwarf_string.h"
48
49 /* TYP, SIZEOFT32 and ASNAR
50 mean we can use correctly-sized arrays of char for the
51 struct members instead of determining a proper integer
52 that size.
53
54 We are dealing with carefully constructed structs
55 that do not have any alignment-forced (hidden)
56 unused bytes so reading lengths from the real structs
57 works for each variable. */
58
59 #define TYP(n,l) char (n)[(l)]
60 #define SIZEOFT32 4
61
62 #define DW_DLV_NO_ENTRY -1
63 #define DW_DLV_OK 0
64 #define DW_DLV_ERROR 1
65
66 #ifndef EI_NIDENT
67 #define EI_NIDENT 16
68 #define EI_CLASS 4
69 #define EI_DATA 5
70 #define EI_VERSION 6
71 #define ELFCLASS32 1
72 #define ELFCLASS64 2
73 #define ELFDATA2LSB 1
74 #define ELFDATA2MSB 2
75 #endif /* EI_NIDENT */
76
77 #define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
78 #define PATHSIZE 2000
79
80 #ifndef MH_MAGIC
81 /* mach-o 32bit */
82 #define MH_MAGIC 0xfeedface
83 #define MH_CIGAM 0xcefaedfe
84 #endif /* MH_MAGIC */
85 #ifndef MH_MAGIC_64
86 /* mach-o 64bit */
87 #define MH_MAGIC_64 0xfeedfacf
88 #define MH_CIGAM_64 0xcffaedfe
89 #endif /* MH_MAGIC_64 */
90
91 /* A flag not public to users. */
92 static int _dwarf_global_debuglink_crc_suppress;
93
94 #if 0 /* debugging only */
95 /* debugging only */
96 static void
97 dump_bytes(char * msg,Dwarf_Small * start, long len)
98 {
99 Dwarf_Small *end = start + len;
100 Dwarf_Small *cur = start;
101
102 printf("%s ",msg);
103 for (; cur < end; cur++) {
104 printf("%02x ", *cur);
105 }
106 printf("\n");
107 }
108 #endif
109
110 int
111 dwarf_suppress_debuglink_crc(int dw_suppress)
112 {
113 int old = _dwarf_global_debuglink_crc_suppress;
114 _dwarf_global_debuglink_crc_suppress = dw_suppress;
115 return old;
116 }
117
118 int _dwarf_get_suppress_debuglink_crc(void)
119 {
120 return _dwarf_global_debuglink_crc_suppress;
121 }
122
123 static unsigned long
124 magic_copy(unsigned char *d, unsigned len)
125 {
126 unsigned i = 0;
127 unsigned long v = 0;
128
129 v = d[0];
130 for (i = 1 ; i < len; ++i) {
131 v <<= 8;
132 v |= d[i];
133 }
134 return v;
135 }
136
137 #define EI_NIDENT 16
138 /* An incomplete elf header, good for 32 and 64bit elf */
139 struct elf_header {
140 unsigned char e_ident[EI_NIDENT];
141 TYP(e_type,2);
142 TYP(e_machine,2);
143 TYP(e_version,4);
144 };
145
146 /* Windows. Certain PE objects.
147 The following references may be of interest.
148 https://msdn.microsoft.com/library/windows/\
149 desktop/ms680547(v=vs.85).aspx
150 #PE format overview and various machine magic numbers
151
152 https://msdn.microsoft.com/en-us/library/\
153 ms809762.aspx
154 # describes some details of PE headers, basically an overview
155
156 https://msdn.microsoft.com/en-us/library/\
157 windows/desktop/aa383751(v=vs.85).aspx
158 #defines sizes of various types
159
160 https://msdn.microsoft.com/fr-fr/library/\
161 windows/desktop/ms680313(v=vs.85).aspx
162 #defines IMAGE_FILE_HEADER and Machine fields (32/64)
163
164 https://msdn.microsoft.com/fr-fr/library/\
165 windows/desktop/ms680305(v=vs.85).aspx
166 #defines IMAGE_DATA_DIRECTORY
167
168 https://msdn.microsoft.com/en-us/library/\
169 windows/desktop/ms680339(v=vs.85).aspx
170 #Defines IMAGE_OPTIONAL_HEADER and some magic numbers
171
172 https://msdn.microsoft.com/fr-fr/library/\
173 windows/desktop/ms680336(v=vs.85).aspx
174 # defines _IMAGE_NT_HEADERS 32 64
175
176 https://msdn.microsoft.com/en-us/library/\
177 windows/desktop/ms680341(v=vs.85).aspx
178 # defines _IMAGE_SECTION_HEADER
179
180 */
181
182 /* ===== START pe structures */
183
184 struct dos_header {
185 TYP(dh_mz,2);
186 TYP(dh_dos_data,58);
187 TYP(dh_image_offset,4);
188 };
189
190 #define IMAGE_DOS_SIGNATURE_dw 0x5A4D
191 #define IMAGE_DOS_REVSIGNATURE_dw 0x4D5A
192 #define IMAGE_NT_SIGNATURE_dw 0x00004550
193 #define IMAGE_FILE_MACHINE_I386_dw 0x14c
194 #define IMAGE_FILE_MACHINE_IA64_dw 0x200
195 #define IMAGE_FILE_MACHINE_AMD64_dw 0x8664
196
197 struct pe_image_file_header {
198 TYP(im_machine,2);
199 TYP(im_sectioncount,2);
200 TYP(im_ignoring,(3*4));
201 TYP(im_opt_header_size,2);
202 TYP(im_ignoringb,2);
203 };
204
205 /* ===== END pe structures */
206
207 /* For following MacOS file naming convention */
208 static const char *
209 getseparator (const char *f)
210 {
211 const char *p = 0;
212 const char *q = 0;
213 char c = 0;;
214
215 p = NULL;
216 q = f;
217 do {
218 c = *q++;
219 if (c == '\\' || c == '/' || c == ':') {
220 p = q;
221 }
222 } while (c);
223 return p;
224 }
225
226 static const char *
227 getbasename (const char *f)
228 {
229 const char *pseparator = getseparator (f);
230 if (!pseparator) {
231 return f;
232 }
233 return pseparator;
234 }
235
236 /* Not a standard function. */
237 static int
238 dw_stpcpy(char *dest,const char *src,char **destend, char *endpoint)
239 {
240 const char *cp = src;
241 char *dp = dest;
242
243 for ( ; *cp; ++cp,++dp) {
244 if (dp >= endpoint) {
245 return DW_DLV_ERROR;
246 }
247 *dp = *cp;
248 }
249 if (dp >= endpoint) {
250 return DW_DLV_ERROR;
251 }
252 *dp = 0;
253 *destend = dp;
254 return DW_DLV_OK;
255 }
256
257 /* This started like Elf, so check initial fields. */
258 static int
259 fill_in_elf_fields(struct elf_header *h,
260 unsigned *endian,
261 /* Size of the object file offsets, not DWARF offset
262 size. */
263 unsigned *objoffsetsize,
264 int *errcode)
265 {
266 unsigned locendian = 0;
267 unsigned locoffsetsize = 0;
268
269 switch(h->e_ident[EI_CLASS]) {
270 case ELFCLASS32:
271 locoffsetsize = 32;
272 break;
273 case ELFCLASS64:
274 locoffsetsize = 64;
275 break;
276 default:
277 *errcode = DW_DLE_ELF_CLASS_BAD;
278 return DW_DLV_ERROR;
279 }
280 switch(h->e_ident[EI_DATA]) {
281 case ELFDATA2LSB:
282 locendian = DW_END_little;
283 break;
284 case ELFDATA2MSB:
285 locendian = DW_END_big;
286 break;
287 default:
288 *errcode = DW_DLE_ELF_ENDIAN_BAD;
289 return DW_DLV_ERROR;
290 }
291 if (h->e_ident[EI_VERSION] != 1 /* EV_CURRENT */) {
292 *errcode = DW_DLE_ELF_VERSION_BAD;
293 return DW_DLV_ERROR;
294 }
295 *endian = locendian;
296 *objoffsetsize = locoffsetsize;
297 return DW_DLV_OK;
298 }
299 static char archive_magic[8] = {
300 '!','<','a','r','c','h','>',0x0a
301 };
302 static int
303 is_archive_magic(struct elf_header *h) {
304 int i = 0;
305 int len = sizeof(archive_magic);
306 const char *cp = (const char *)h;
307 for ( ; i < len; ++i) {
308 if (cp[i] != archive_magic[i]) {
309 return FALSE;
310 }
311 }
312 return TRUE;
313 }
314
315 /* A bit unusual in that it always sets *is_pe_flag
316 Return of DW_DLV_OK it is a PE file we recognize. */
317 static int
318 is_pe_object(int fd,
319 Dwarf_Unsigned filesize,
320 unsigned *endian,
321 unsigned *offsetsize,
322 int *errcode)
323 {
324 unsigned dos_sig = 0;
325 unsigned locendian = 0;
326 void (*word_swap) (void *, const void *, unsigned long);
327 unsigned long nt_address = 0;
328 struct dos_header dhinmem;
329 char nt_sig_array[4];
330 unsigned long nt_sig = 0;
331 struct pe_image_file_header ifh;
332 int res = 0;
333
334 if (filesize < (sizeof (struct dos_header) +
335 SIZEOFT32 + sizeof(struct pe_image_file_header))) {
336 *errcode = DW_DLE_FILE_TOO_SMALL;
337 return DW_DLV_ERROR;
338 }
339 res = _dwarf_object_read_random(fd,(char *)&dhinmem,
340 0,sizeof(dhinmem),filesize,errcode);
341 if (res != DW_DLV_OK) {
342 return res;
343 }
344 /* No swap here, want it as in the file */
345 dos_sig = magic_copy((unsigned char *)dhinmem.dh_mz,
346 sizeof(dhinmem.dh_mz));
347 if (dos_sig == IMAGE_DOS_SIGNATURE_dw) {
348 /* IMAGE_DOS_SIGNATURE_dw assumes bytes
349 reversed by little-endian
350 load, so we intrepet a match the other way. */
351 /* BIG ENDIAN. From looking at hex characters in object */
352 #ifdef WORDS_BIGENDIAN
353 word_swap = _dwarf_memcpy_noswap_bytes;
354 #else /* LITTLE ENDIAN */
355 word_swap = _dwarf_memcpy_swap_bytes;
356 #endif /* LITTLE- BIG-ENDIAN */
357 locendian = DW_END_big;
358 } else if (dos_sig == IMAGE_DOS_REVSIGNATURE_dw) {
359 /* raw load, so intrepet a match the other way. */
360 /* LITTLE ENDIAN */
361 #ifdef WORDS_BIGENDIAN
362 word_swap = _dwarf_memcpy_swap_bytes;
363 #else /* LITTLE ENDIAN */
364 word_swap = _dwarf_memcpy_noswap_bytes;
365 #endif /* LITTLE- BIG-ENDIAN */
366 locendian = DW_END_little;
367 } else {
368 /* Not dos header not a PE file we recognize */
369 *errcode = DW_DLE_FILE_WRONG_TYPE;
370 return DW_DLV_ERROR;
371 }
372 ASNAR(word_swap,nt_address, dhinmem.dh_image_offset);
373 if (filesize < nt_address) {
374 /* Not dos header not a PE file we recognize */
375 *errcode = DW_DLE_FILE_TOO_SMALL;
376 return DW_DLV_ERROR;
377 }
378 if (filesize < (nt_address + SIZEOFT32 +
379 sizeof(struct pe_image_file_header))) {
380 *errcode = DW_DLE_FILE_TOO_SMALL;
381 /* Not dos header not a PE file we recognize */
382 return DW_DLV_ERROR;
383 }
384 res = _dwarf_object_read_random(fd,(char *)&nt_sig_array[0],
385 nt_address, sizeof(nt_sig_array),filesize,errcode);
386 if (res != DW_DLV_OK) {
387 return res;
388 }
389 { unsigned long lsig = 0;
390
391 ASNAR(word_swap,lsig,nt_sig_array);
392 nt_sig = lsig;
393 }
394 if (nt_sig != IMAGE_NT_SIGNATURE_dw) {
395 *errcode = DW_DLE_FILE_WRONG_TYPE;
396 return DW_DLV_ERROR;
397 }
398 res = _dwarf_object_read_random(fd,(char *)&ifh,
399 nt_address + SIZEOFT32,
400 sizeof(struct pe_image_file_header),
401 filesize,
402 errcode);
403 if (res != DW_DLV_OK) {
404 return res;
405 }
406 {
407 unsigned long machine = 0;
408
409 ASNAR(word_swap,machine,ifh.im_machine);
410 switch(machine) {
411 case IMAGE_FILE_MACHINE_I386_dw:
412 *offsetsize = 32;
413 *endian = locendian;
414 return DW_DLV_OK;
415 case IMAGE_FILE_MACHINE_IA64_dw:
416 case IMAGE_FILE_MACHINE_AMD64_dw:
417 *offsetsize = 64;
418 *endian = locendian;
419 return DW_DLV_OK;
420 default: break;
421 }
422 }
423 *errcode = DW_DLE_IMAGE_FILE_UNKNOWN_TYPE;
424 return DW_DLV_ERROR;
425 }
426
427 static int
428 is_mach_o_universal(struct elf_header *h,
429 unsigned *endian,
430 unsigned *offsetsize)
431 {
432 unsigned long magicval = 0;
433 unsigned locendian = 0;
434 unsigned locoffsetsize = 0;
435
436 /* No swapping here. Need to match size of
437 the universal-object magic field. */
438 magicval = magic_copy(h->e_ident,4);
439 if (magicval == FAT_MAGIC) {
440 locendian = DW_END_big;
441 locoffsetsize = 32;
442 } else if (magicval == FAT_CIGAM) {
443 locendian = DW_END_little;
444 locoffsetsize = 32;
445 }else if (magicval == FAT_MAGIC_64) {
446 locendian = DW_END_big;
447 locoffsetsize = 64;
448 } else if (magicval == FAT_CIGAM_64) {
449 locendian = DW_END_little;
450 locoffsetsize = 64;
451 } else {
452 return FALSE;
453 }
454 *endian = locendian;
455 *offsetsize = locoffsetsize;
456 return TRUE;
457 }
458
459 static int
460 is_mach_o_magic(struct elf_header *h,
461 unsigned *endian,
462 unsigned *offsetsize)
463 {
464 unsigned long magicval = 0;
465 unsigned locendian = 0;
466 unsigned locoffsetsize = 0;
467
468 /* No swapping here. Need to match size of
469 Mach-o magic field. */
470 magicval = magic_copy(h->e_ident,4);
471 if (magicval == MH_MAGIC) {
472 locendian = DW_END_big;
473 locoffsetsize = 32;
474 } else if (magicval == MH_CIGAM) {
475 locendian = DW_END_little;
476 locoffsetsize = 32;
477 }else if (magicval == MH_MAGIC_64) {
478 locendian = DW_END_big;
479 locoffsetsize = 64;
480 } else if (magicval == MH_CIGAM_64) {
481 locendian = DW_END_little;
482 locoffsetsize = 64;
483 } else {
484 return FALSE;
485 }
486 *endian = locendian;
487 *offsetsize = locoffsetsize;
488 return TRUE;
489 }
490
491 int
492 dwarf_object_detector_fd(int fd,
493 unsigned *ftype,
494 unsigned *endian,
495 unsigned *offsetsize,
496 Dwarf_Unsigned *filesize,
497 int *errcode)
498 {
499 Dwarf_Unsigned fileoffsetbase = 0;
500 int res = 0;
501
502 res = _dwarf_object_detector_fd_a(fd,
503 ftype,endian,offsetsize,
504 fileoffsetbase,filesize,
505 errcode);
506 return res;
507 }
508
509 int
510 _dwarf_object_detector_fd_a(int fd,
511 unsigned *ftype,
512 unsigned *endian,
513 unsigned *offsetsize,
514 Dwarf_Unsigned fileoffsetbase,
515 Dwarf_Unsigned *filesize,
516 int *errcode)
517 {
518 struct elf_header h;
519 Dwarf_Unsigned readlen = sizeof(h);
520 Dwarf_Unsigned fsize = 0;
521 Dwarf_Unsigned remaininglen = 0;
522 int res = 0;
523
524 res = _dwarf_seekr(fd,0,SEEK_END,&fsize);
525 if (res != DW_DLV_OK) {
526 *errcode = DW_DLE_SEEK_ERROR;
527 return DW_DLV_ERROR;
528 }
529 if (fsize <= readlen) {
530 /* Not a real object file */
531 *errcode = DW_DLE_FILE_TOO_SMALL;
532 return DW_DLV_ERROR;
533 }
534 if (fsize <= fileoffsetbase) {
535 *errcode = DW_DLE_SEEK_ERROR;
536 return DW_DLV_ERROR;
537 }
538 remaininglen = fsize - fileoffsetbase;
539 if (remaininglen <= readlen) {
540 /* Not a real object file */
541 *errcode = DW_DLE_FILE_TOO_SMALL;
542 return DW_DLV_ERROR;
543 }
544 /* fileoffsetbase is non zero iff we have
545 an Apple Universal Binary. */
546 res = _dwarf_seekr(fd,fileoffsetbase,SEEK_SET,0);
547 if (res != DW_DLV_OK) {
548 *errcode = DW_DLE_SEEK_ERROR;
549 return DW_DLV_ERROR;
550 }
551 res = _dwarf_readr(fd,(char *)&h,readlen,0);
552 if (res != DW_DLV_OK) {
553 *errcode = DW_DLE_READ_ERROR;
554 return DW_DLV_ERROR;
555 }
556 if (h.e_ident[0] == 0x7f &&
557 h.e_ident[1] == 'E' &&
558 h.e_ident[2] == 'L' &&
559 h.e_ident[3] == 'F') {
560 /* is ELF */
561
562 res = fill_in_elf_fields(&h,endian,offsetsize,errcode);
563 if (res != DW_DLV_OK) {
564 return res;
565 }
566 *ftype = DW_FTYPE_ELF;
567 *filesize = (Dwarf_Unsigned)fsize;
568 return DW_DLV_OK;
569 }
570 if (is_mach_o_universal(&h,endian,offsetsize)) {
571 *ftype = DW_FTYPE_APPLEUNIVERSAL;
572 *filesize = (Dwarf_Unsigned)fsize;
573 return DW_DLV_OK;
574 }
575 if (is_mach_o_magic(&h,endian,offsetsize)) {
576 *ftype = DW_FTYPE_MACH_O;
577 *filesize = (Dwarf_Unsigned)fsize;
578 return DW_DLV_OK;
579 }
580 if (is_archive_magic(&h)) {
581 *ftype = DW_FTYPE_ARCHIVE;
582 *filesize = (Dwarf_Unsigned)fsize;
583 return DW_DLV_OK;
584 }
585 res = is_pe_object(fd,fsize,endian,offsetsize,errcode);
586 if (res == DW_DLV_OK ) {
587 *ftype = DW_FTYPE_PE;
588 *filesize = (Dwarf_Unsigned)fsize;
589 return DW_DLV_OK;
590 }
591 /* Unknown object format. */
592 return DW_DLV_NO_ENTRY;
593 }
594
595 int
596 dwarf_object_detector_path_dSYM(
597 const char *path,
598 char *outpath, unsigned long outpath_len,
599 char ** gl_pathnames,
600 unsigned gl_pathcount,
601 unsigned *ftype,
602 unsigned *endian,
603 unsigned *offsetsize,
604 Dwarf_Unsigned *filesize,
605 unsigned char *pathsource,
606 int *errcode)
607 {
608 char *cp = 0;
609 size_t plen = strlen(path);
610 size_t dsprefixlen = sizeof(DSYM_SUFFIX);
611 int fd = -1;
612 int res = 0;
613 int have_outpath = outpath && outpath_len;
614
615 (void)gl_pathnames;
616 (void)gl_pathcount;
617 if (have_outpath) {
618 /* Looking for MacOS dSYM */
619 if ((2*plen + dsprefixlen +2) >= (size_t)outpath_len) {
620 *errcode = DW_DLE_PATH_SIZE_TOO_SMALL;
621 return DW_DLV_ERROR;
622 }
623 res = dw_stpcpy(outpath,path,&cp,outpath+outpath_len);
624 if (res == DW_DLV_ERROR) {
625 *errcode = DW_DLE_PATH_SIZE_TOO_SMALL;
626 return DW_DLV_ERROR;
627 }
628 res = dw_stpcpy(cp,DSYM_SUFFIX,&cp,outpath+outpath_len);
629 if (res == DW_DLV_ERROR) {
630 *errcode = DW_DLE_PATH_SIZE_TOO_SMALL;
631 return DW_DLV_ERROR;
632 }
633 res= dw_stpcpy(cp,getbasename(path),&cp,outpath+outpath_len);
634 if (res == DW_DLV_ERROR) {
635 *errcode = DW_DLE_PATH_SIZE_TOO_SMALL;
636 return DW_DLV_ERROR;
637 }
638 fd = _dwarf_openr(outpath);
639 if (fd < 0) {
640 outpath[0] = 0;
641 return DW_DLV_NO_ENTRY;
642 }
643 *pathsource = DW_PATHSOURCE_dsym;
644 res = dwarf_object_detector_fd(fd,
645 ftype,endian,offsetsize,filesize,errcode);
646 if (res != DW_DLV_OK) {
647 _dwarf_closer(fd);
648 return res;
649 }
650 _dwarf_closer(fd);
651 return DW_DLV_OK;
652 }
653 return DW_DLV_NO_ENTRY;
654 }
655
656 static int
657 blockmatch(unsigned char *l,
658 unsigned char* r,
659 unsigned length)
660 {
661 unsigned int i = 0;
662 for ( ; i < length; ++i) {
663 if (l[i] != r[i]) {
664 return FALSE;
665 }
666 }
667 return TRUE;
668 }
669
670 /* The debug version we expect not to have debuglink,
671 checking here if buildid matches.
672 Returns TRUE or FALSE */
673 static Dwarf_Bool
674 match_buildid(
675 unsigned char * crc_base,
676 unsigned buildid_length_base,
677 unsigned char *buildid_base,
678 /* *_base is executable info while
679 *_debug is the debug object. */
680 unsigned char *crc_debug,
681 unsigned buildid_length_debug,
682 unsigned char *buildid_debug)
683 {
684 if (!_dwarf_get_suppress_debuglink_crc() && \
685 crc_debug && crc_base) {
686 /* crc available for both */
687 if (!blockmatch(crc_debug,crc_base,4)) {
688 return FALSE;
689 }
690 return TRUE;
691 }
692 if (!blockmatch(buildid_base,buildid_debug,
693 buildid_length_base)) {
694 return FALSE;
695 }
696 if (buildid_length_base != buildid_length_debug) {
697 return FALSE;
698 }
699 return TRUE;
700 }
701
702 /* we need the crc byte order to match that
703 of the object file so a comparison works.
704 Here we fix up when there is a mismatch */
705 static int
706 _dwarf_debuglink_finder_newpath(
707 char * path_in,
708 unsigned char *crc_in,
709 unsigned buildid_len_in,
710 unsigned char *buildid_in,
711 dwarfstring *m,
712 int * fd_out)
713 {
714 unsigned char lcrc[4] = {0,0,0,0};
715 unsigned char newcrc[4] = {0,0,0,0};
716 char *debuglinkpath = 0; /* must be freed */
717 unsigned char *crc = 0;
718 char *debuglinkfullpath = 0;
719 unsigned debuglinkfullpath_strlen = 0;
720 unsigned buildid_type = 0;
721 char * buildidownername = 0;
722 unsigned char *buildid = 0;
723 unsigned buildid_length = 0;
724 char ** paths = 0; /* must be freed */
725 unsigned paths_count = 0;
726 Dwarf_Debug dbg = 0;
727 Dwarf_Error error = 0;
728 char *path = path_in;
729 Dwarf_Bool didmatch = FALSE;
730 int res = 0;
731
732 res = dwarf_init_path(path,
733 0,0,
734 DW_GROUPNUMBER_ANY,
735 0,0, &dbg,&error);
736 if (res == DW_DLV_ERROR) {
737 /* ASSERT: dbg is NULL as init failed */
738 dwarf_dealloc_error(dbg,error);
739 error = 0;
740 return DW_DLV_NO_ENTRY;
741 }
742 if (res == DW_DLV_NO_ENTRY) {
743 /* should never happen */
744 return DW_DLV_NO_ENTRY;
745 }
746 res = dwarf_gnu_debuglink(dbg,
747 &debuglinkpath,
748 &crc, &debuglinkfullpath, &debuglinkfullpath_strlen,
749 &buildid_type, &buildidownername,
750 &buildid, &buildid_length,
751 &paths, &paths_count, &error);
752 if (res == DW_DLV_ERROR) {
753 dwarf_dealloc_error(dbg,error);
754 dwarf_finish(dbg);
755 error = 0;
756 dbg = 0;
757 return DW_DLV_NO_ENTRY;
758 }
759 if (res == DW_DLV_NO_ENTRY) {
760 /* There is no debuglink section */
761 dwarf_finish(dbg);
762 dbg = 0;
763 return DW_DLV_NO_ENTRY;
764 }
765 free(paths);
766 paths = 0;
767
768 if (!_dwarf_get_suppress_debuglink_crc() &&crc_in && !crc) {
769 int res1 = 0;
770
771 res1 = dwarf_crc32(dbg,lcrc,&error);
772 if (res1 == DW_DLV_ERROR) {
773 paths = 0;
774 free(debuglinkfullpath);
775 dwarf_dealloc_error(dbg,error);
776 dwarf_finish(dbg);
777 error = 0;
778 dbg = 0;
779 /* Cannot match the crc_in, give up. */
780 return DW_DLV_NO_ENTRY;
781 }
782 if (res1 == DW_DLV_OK) {
783 dbg->de_copy_word((void *)newcrc,(void *)lcrc,4);
784 crc=&newcrc[0];
785 }
786 }
787 free(debuglinkfullpath);
788 didmatch = match_buildid(
789 /* This is about the executable */
790 crc_in,buildid_len_in,buildid_in,
791 /* pass in local so we can calculate the missing crc */
792 /* following is the target, ie, debug */
793 crc,buildid_length,buildid);
794 if (error) {
795 /* This should never happen. It would mean
796 error was set without DW_DLV_ERROR */
797 dwarf_dealloc_error(dbg,error);
798 error = 0;
799 }
800 if (didmatch) {
801 dwarfstring_append(m,path);
802 *fd_out = dbg->de_fd;
803 dbg->de_owns_fd = FALSE;
804 dwarf_finish(dbg);
805 dbg = 0;
806 return DW_DLV_OK;
807 }
808 dwarf_finish(dbg);
809 return DW_DLV_NO_ENTRY;
810 }
811
812 static int
813 _dwarf_debuglink_finder_internal(
814 char **gl_pathnames,
815 unsigned int gl_pathcount,
816 char * path_in,
817 dwarfstring *m,
818 int * fd_out,
819 int * errcode)
820 {
821 int res = 0;
822 /* This local dbg is opened and then dwarf_finish()
823 here. No dbg in the arguments! */
824 Dwarf_Debug dbg = 0;
825 char * path = 0;
826 Dwarf_Error error = 0;
827 unsigned int p = 0;
828 char *debuglinkpath = 0;
829 unsigned char *crc = 0;
830 char *debuglinkfullpath = 0; /* must be freed*/
831 unsigned debuglinkfullpath_strlen = 0;
832 unsigned buildid_type = 0;
833 char * buildidownername = 0;
834 unsigned char *buildid = 0;
835 unsigned buildid_length = 0;
836 char ** paths = 0; /* must be freed */
837 unsigned paths_count = 0;
838 unsigned i = 0;
839
840 path = path_in;
841 /* This path will work.
842 Already know the file is there. */
843 res = dwarf_init_path(path,
844 0,0,
845 DW_GROUPNUMBER_ANY,
846 0,0, &dbg, &error);
847 if (res == DW_DLV_ERROR) {
848 /* error codes all >=0 && < 2000 */
849 *errcode = (int)dwarf_errno(error);
850 dwarf_dealloc_error(dbg,error);
851 error = 0;
852 return res;
853 }
854 if (res == DW_DLV_NO_ENTRY) {
855 return res;
856 }
857 for (p = 0; p < gl_pathcount; ++p) {
858 const char *lpath = 0;
859
860 lpath = (const char *)gl_pathnames[p];
861 res = dwarf_add_debuglink_global_path(dbg,
862 lpath, &error);
863 if (res != DW_DLV_OK){
864 if (res == DW_DLV_ERROR) {
865 *errcode = (int)dwarf_errno(error);
866 dwarf_dealloc_error(dbg,error);
867 error = 0;
868 }
869 dwarf_finish(dbg);
870 return res;
871 }
872 }
873 res = dwarf_gnu_debuglink(dbg,
874 &debuglinkpath,
875 &crc, &debuglinkfullpath, &debuglinkfullpath_strlen,
876 &buildid_type, &buildidownername,
877 &buildid, &buildid_length,
878 &paths, &paths_count, &error);
879 if (res == DW_DLV_ERROR) {
880 *errcode = (int)dwarf_errno(error);
881 dwarf_dealloc_error(dbg,error);
882 dwarf_finish(dbg);
883 return DW_DLV_NO_ENTRY;
884 }
885 if (res == DW_DLV_NO_ENTRY) {
886 /* There is no debuglink buildid section? */
887 dwarf_finish(dbg);
888 return DW_DLV_NO_ENTRY;
889 }
890 for (i =0; i < paths_count; ++i) {
891 char *pa = paths[i];
892 int pfd = 0;
893
894 /* First, open the file to determine if it exists.
895 If not, loop again */
896
897 pfd = _dwarf_openr(pa);
898 if (pfd < 0) {
899 /* This is the usual path. */
900 continue;
901 }
902 _dwarf_closer(pfd);
903 /* ASSERT: never returns DW_DLV_ERROR */
904 res = _dwarf_debuglink_finder_newpath(
905 pa,crc,buildid_length, buildid,
906 m,fd_out);
907 if (res == DW_DLV_OK) {
908 free(debuglinkfullpath);
909 free(paths);
910 paths = 0;
911 dwarf_finish(dbg);
912 return DW_DLV_OK;
913 }
914 *errcode = 0;
915 continue;
916 }
917 free(debuglinkfullpath);
918 free(paths);
919 paths = 0;
920 dwarf_finish(dbg);
921 return DW_DLV_NO_ENTRY;
922 }
923
924 int
925 dwarf_object_detector_path_b(
926 const char * path,
927 char * outpath,
928 unsigned long outpath_len,
929 char ** gl_pathnames,
930 unsigned gl_pathcount,
931 unsigned * ftype,
932 unsigned * endian,
933 unsigned * offsetsize,
934 Dwarf_Unsigned * filesize,
935 unsigned char * pathsource,
936 int *errcode)
937 {
938 int fd = -1;
939 int res = 0;
940 int have_outpath = outpath && outpath_len;
941 unsigned char lpathsource = DW_PATHSOURCE_basic;
942
943 if (pathsource) {
944 lpathsource = *pathsource;
945 }
946 if (lpathsource == DW_PATHSOURCE_basic && have_outpath) {
947 /* On return from the following call we could well
948 close the fd above and open a new one. */
949 int debuglink_fd = -1;
950 size_t dllenszt = 0;
951 char *cp = 0;
952 dwarfstring m;
953
954 dwarfstring_constructor(&m);
955 res = _dwarf_debuglink_finder_internal(
956 gl_pathnames,gl_pathcount,
957 (char *)path, &m,&debuglink_fd, errcode);
958 if (res == DW_DLV_ERROR) {
959 dwarfstring_destructor(&m);
960 if (debuglink_fd != -1) {
961 _dwarf_closer(debuglink_fd);
962 }
963 return res;
964 }
965 if (res == DW_DLV_NO_ENTRY) {
966 /* We did not find an alternative path */
967 res = dw_stpcpy(outpath,path,&cp,outpath+outpath_len);
968 if (res != DW_DLV_OK) {
969 *errcode = DW_DLE_PATH_SIZE_TOO_SMALL;
970 return DW_DLV_ERROR;
971 }
972 lpathsource = DW_PATHSOURCE_basic;
973 } else {
974 if (debuglink_fd != -1) {
975 _dwarf_closer(debuglink_fd);
976 debuglink_fd = -1;
977 }
978 dllenszt = dwarfstring_strlen(&m)+1;
979 if (dllenszt >= (size_t)outpath_len) {
980 *errcode = DW_DLE_DEBUGLINK_PATH_SHORT;
981 return DW_DLV_ERROR;
982 }
983 res = dw_stpcpy(outpath,dwarfstring_string(&m),
984 &cp,outpath+outpath_len);
985 if (res != DW_DLV_OK) {
986 *errcode = DW_DLE_DEBUGLINK_PATH_SHORT;
987 return DW_DLV_ERROR;
988 }
989 lpathsource = DW_PATHSOURCE_debuglink;
990 }
991 dwarfstring_destructor(&m);
992 fd = _dwarf_openr(outpath);
993 /* fall through to get fsize etc */
994 } else {
995 lpathsource = DW_PATHSOURCE_basic;
996 fd = _dwarf_openr(path);
997 }
998 if (fd < 0) {
999 if (pathsource) {
1000 *pathsource = DW_PATHSOURCE_unspecified;
1001 }
1002 return DW_DLV_NO_ENTRY;
1003 }
1004 res = dwarf_object_detector_fd(fd,
1005 ftype,endian,offsetsize,filesize,errcode);
1006 if (res != DW_DLV_OK) {
1007 lpathsource = DW_PATHSOURCE_unspecified;
1008 }
1009 if (pathsource) {
1010 *pathsource = lpathsource;
1011 }
1012 _dwarf_closer(fd);
1013 return res;
1014 }