]> git.cworth.org Git - vogl/blob - src/libbacktrace/elf.c
Initial vogl checkin
[vogl] / src / libbacktrace / elf.c
1 /* elf.c -- Get debug data from an ELF file for backtraces.
2    Copyright (C) 2012-2014 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor, Google.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9     (1) Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer. 
11
12     (2) Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in
14     the documentation and/or other materials provided with the
15     distribution.  
16     
17     (3) The name of the author may not be used to
18     endorse or promote products derived from this software without
19     specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.  */
32
33 #include "config.h"
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <limits.h>
40 #include <pwd.h>
41
42 #ifdef HAVE_DL_ITERATE_PHDR
43 #include <link.h>
44 #endif
45
46 #include "backtrace.h"
47 #include "internal.h"
48
49 #ifndef HAVE_DL_ITERATE_PHDR
50
51 /* Dummy version of dl_iterate_phdr for systems that don't have it.  */
52
53 #define dl_phdr_info x_dl_phdr_info
54 #define dl_iterate_phdr x_dl_iterate_phdr
55
56 struct dl_phdr_info
57 {
58   uintptr_t dlpi_addr;
59   const char *dlpi_name;
60 };
61
62 static int
63 dl_iterate_phdr (int (*callback) (struct dl_phdr_info *,
64                                   size_t, void *) ATTRIBUTE_UNUSED,
65                  void *data ATTRIBUTE_UNUSED)
66 {
67   return 0;
68 }
69
70 #endif /* ! defined (HAVE_DL_ITERATE_PHDR) */
71
72 /* The configure script must tell us whether we are 32-bit or 64-bit
73    ELF.  We could make this code test and support either possibility,
74    but there is no point.  This code only works for the currently
75    running executable, which means that we know the ELF mode at
76    configure mode.  */
77
78 #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64
79 #error "Unknown BACKTRACE_ELF_SIZE"
80 #endif
81
82 /* <link.h> might #include <elf.h> which might define our constants
83    with slightly different values.  Undefine them to be safe.  */
84
85 #undef EI_NIDENT
86 #undef EI_MAG0
87 #undef EI_MAG1
88 #undef EI_MAG2
89 #undef EI_MAG3
90 #undef EI_CLASS
91 #undef EI_DATA
92 #undef EI_VERSION
93 #undef ELF_MAG0
94 #undef ELF_MAG1
95 #undef ELF_MAG2
96 #undef ELF_MAG3
97 #undef ELFCLASS32
98 #undef ELFCLASS64
99 #undef ELFDATA2LSB
100 #undef ELFDATA2MSB
101 #undef EV_CURRENT
102 #undef ET_DYN
103 #undef SHN_LORESERVE
104 #undef SHN_XINDEX
105 #undef SHN_UNDEF
106 #undef SHT_SYMTAB
107 #undef SHT_STRTAB
108 #undef SHT_DYNSYM
109 #undef STT_OBJECT
110 #undef STT_FUNC
111
112 /* Basic types.  */
113
114 typedef uint16_t b_elf_half;    /* Elf_Half.  */
115 typedef uint32_t b_elf_word;    /* Elf_Word.  */
116 typedef int32_t  b_elf_sword;   /* Elf_Sword.  */
117
118 #if BACKTRACE_ELF_SIZE == 32
119
120 typedef uint32_t b_elf_addr;    /* Elf_Addr.  */
121 typedef uint32_t b_elf_off;     /* Elf_Off.  */
122
123 typedef uint32_t b_elf_wxword;  /* 32-bit Elf_Word, 64-bit ELF_Xword.  */
124
125 #else
126
127 typedef uint64_t b_elf_addr;    /* Elf_Addr.  */
128 typedef uint64_t b_elf_off;     /* Elf_Off.  */
129 typedef uint64_t b_elf_xword;   /* Elf_Xword.  */
130
131 typedef uint64_t b_elf_wxword;  /* 32-bit Elf_Word, 64-bit ELF_Xword.  */
132
133 #endif
134
135 /* Data structures and associated constants.  */
136
137 #define EI_NIDENT 16
138
139 typedef struct {
140   unsigned char e_ident[EI_NIDENT];     /* ELF "magic number" */
141   b_elf_half    e_type;                 /* Identifies object file type */
142   b_elf_half    e_machine;              /* Specifies required architecture */
143   b_elf_word    e_version;              /* Identifies object file version */
144   b_elf_addr    e_entry;                /* Entry point virtual address */
145   b_elf_off     e_phoff;                /* Program header table file offset */
146   b_elf_off     e_shoff;                /* Section header table file offset */
147   b_elf_word    e_flags;                /* Processor-specific flags */
148   b_elf_half    e_ehsize;               /* ELF header size in bytes */
149   b_elf_half    e_phentsize;            /* Program header table entry size */
150   b_elf_half    e_phnum;                /* Program header table entry count */
151   b_elf_half    e_shentsize;            /* Section header table entry size */
152   b_elf_half    e_shnum;                /* Section header table entry count */
153   b_elf_half    e_shstrndx;             /* Section header string table index */
154 } b_elf_ehdr;  /* Elf_Ehdr.  */
155
156 #define EI_MAG0 0
157 #define EI_MAG1 1
158 #define EI_MAG2 2
159 #define EI_MAG3 3
160 #define EI_CLASS 4
161 #define EI_DATA 5
162 #define EI_VERSION 6
163
164 #define ELFMAG0 0x7f
165 #define ELFMAG1 'E'
166 #define ELFMAG2 'L'
167 #define ELFMAG3 'F'
168
169 #define ELFCLASS32 1
170 #define ELFCLASS64 2
171
172 #define ELFDATA2LSB 1
173 #define ELFDATA2MSB 2
174
175 #define EV_CURRENT 1
176
177 #define ET_DYN 3
178
179 typedef struct {
180   b_elf_word    sh_name;                /* Section name, index in string tbl */
181   b_elf_word    sh_type;                /* Type of section */
182   b_elf_wxword  sh_flags;               /* Miscellaneous section attributes */
183   b_elf_addr    sh_addr;                /* Section virtual addr at execution */
184   b_elf_off     sh_offset;              /* Section file offset */
185   b_elf_wxword  sh_size;                /* Size of section in bytes */
186   b_elf_word    sh_link;                /* Index of another section */
187   b_elf_word    sh_info;                /* Additional section information */
188   b_elf_wxword  sh_addralign;           /* Section alignment */
189   b_elf_wxword  sh_entsize;             /* Entry size if section holds table */
190 } b_elf_shdr;  /* Elf_Shdr.  */
191
192 #define SHN_UNDEF       0x0000          /* Undefined section */
193 #define SHN_LORESERVE   0xFF00          /* Begin range of reserved indices */
194 #define SHN_XINDEX      0xFFFF          /* Section index is held elsewhere */
195
196 #define SHT_SYMTAB 2
197 #define SHT_STRTAB 3
198 #define SHT_DYNSYM 11
199
200 #if BACKTRACE_ELF_SIZE == 32
201
202 typedef struct
203 {
204   b_elf_word    st_name;                /* Symbol name, index in string tbl */
205   b_elf_addr    st_value;               /* Symbol value */
206   b_elf_word    st_size;                /* Symbol size */
207   unsigned char st_info;                /* Symbol binding and type */
208   unsigned char st_other;               /* Visibility and other data */
209   b_elf_half    st_shndx;               /* Symbol section index */
210 } b_elf_sym;  /* Elf_Sym.  */
211
212 #else /* BACKTRACE_ELF_SIZE != 32 */
213
214 typedef struct
215 {
216   b_elf_word    st_name;                /* Symbol name, index in string tbl */
217   unsigned char st_info;                /* Symbol binding and type */
218   unsigned char st_other;               /* Visibility and other data */
219   b_elf_half    st_shndx;               /* Symbol section index */
220   b_elf_addr    st_value;               /* Symbol value */
221   b_elf_xword   st_size;                /* Symbol size */
222 } b_elf_sym;  /* Elf_Sym.  */
223
224 #endif /* BACKTRACE_ELF_SIZE != 32 */
225
226 #define STT_OBJECT 1
227 #define STT_FUNC 2
228
229 /* An index of ELF sections we care about.  */
230
231 enum debug_section
232 {
233   DEBUG_INFO,
234   DEBUG_LINE,
235   DEBUG_ABBREV,
236   DEBUG_RANGES,
237   DEBUG_STR,
238   GNU_DEBUGLINK,
239   DEBUG_MAX
240 };
241
242 /* Names of sections, indexed by enum elf_section.  */
243
244 static const char * const debug_section_names[DEBUG_MAX] =
245 {
246   ".debug_info",
247   ".debug_line",
248   ".debug_abbrev",
249   ".debug_ranges",
250   ".debug_str",
251   ".gnu_debuglink"
252 };
253
254 /* Information we gather for the sections we care about.  */
255
256 struct debug_section_info
257 {
258   /* Section file offset.  */
259   off_t offset;
260   /* Section size.  */
261   size_t size;
262   /* Section contents, after read from file.  */
263   const unsigned char *data;
264 };
265
266 /* Information we keep for an ELF symbol.  */
267
268 struct elf_symbol
269 {
270   /* The name of the symbol.  */
271   const char *name;
272   /* The address of the symbol.  */
273   uintptr_t address;
274   /* The size of the symbol.  */
275   size_t size;
276 };
277
278 /* Information to pass to elf_syminfo.  */
279
280 struct elf_syminfo_data
281 {
282   /* Symbols for the next module.  */
283   struct elf_syminfo_data *next;
284   /* The ELF symbols, sorted by address.  */
285   struct elf_symbol *symbols;
286   /* The number of symbols.  */
287   size_t count;
288   /* The base address for this module. */
289   uintptr_t base_address;
290   /* Address symbols size. */
291   uintptr_t symbol_size;
292 };
293
294 /* A dummy callback function used when we can't find any debug info.  */
295
296 static int
297 elf_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
298              uintptr_t pc ATTRIBUTE_UNUSED,
299              backtrace_full_callback callback ATTRIBUTE_UNUSED,
300              backtrace_error_callback error_callback, void *data)
301 {
302   error_callback (data, "no debug info in ELF executable", -1);
303   return 0;
304 }
305
306 /* A dummy callback function used when we can't find a symbol
307    table.  */
308
309 static void
310 elf_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
311             uintptr_t addr ATTRIBUTE_UNUSED,
312             backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
313             backtrace_error_callback error_callback, void *data)
314 {
315   error_callback (data, "no symbol table in ELF executable", -1);
316 }
317
318 /* Compare struct elf_symbol for qsort.  */
319
320 static int
321 elf_symbol_compare (const void *v1, const void *v2)
322 {
323   const struct elf_symbol *e1 = (const struct elf_symbol *) v1;
324   const struct elf_symbol *e2 = (const struct elf_symbol *) v2;
325
326   if (e1->address < e2->address)
327     return -1;
328   else if (e1->address > e2->address)
329     return 1;
330   else
331     return 0;
332 }
333
334 /* Compare an ADDR against an elf_symbol for bsearch.  We allocate one
335    extra entry in the array so that this can look safely at the next
336    entry.  */
337
338 static int
339 elf_symbol_search (const void *vkey, const void *ventry)
340 {
341   const uintptr_t *key = (const uintptr_t *) vkey;
342   const struct elf_symbol *entry = (const struct elf_symbol *) ventry;
343   uintptr_t addr;
344
345   addr = *key;
346   if (addr < entry->address)
347     return -1;
348   else if (addr >= entry->address + entry->size)
349     return 1;
350   else
351     return 0;
352 }
353
354 /* Initialize the symbol table info for elf_syminfo.  */
355
356 static int
357 elf_initialize_syminfo (struct backtrace_state *state,
358                         uintptr_t base_address,
359                         const unsigned char *symtab_data, size_t symtab_size,
360                         const unsigned char *strtab, size_t strtab_size,
361                         backtrace_error_callback error_callback,
362                         void *data, struct elf_syminfo_data *sdata)
363 {
364   size_t sym_count;
365   const b_elf_sym *sym;
366   size_t elf_symbol_count;
367   size_t elf_symbol_size;
368   struct elf_symbol *elf_symbols;
369   size_t symbol_size = 0;
370   size_t i;
371   unsigned int j;
372
373   sym_count = symtab_size / sizeof (b_elf_sym);
374
375   /* We only care about function symbols.  Count them.  */
376   sym = (const b_elf_sym *) symtab_data;
377   elf_symbol_count = 0;
378   for (i = 0; i < sym_count; ++i, ++sym)
379     {
380       int info;
381
382       info = sym->st_info & 0xf;
383       if ((info == STT_FUNC || info == STT_OBJECT)
384           && sym->st_shndx != SHN_UNDEF)
385         ++elf_symbol_count;
386     }
387
388   elf_symbol_size = elf_symbol_count * sizeof (struct elf_symbol);
389   elf_symbols = ((struct elf_symbol *)
390                  backtrace_alloc (state, elf_symbol_size, error_callback,
391                                   data));
392   if (elf_symbols == NULL)
393     return 0;
394
395   sym = (const b_elf_sym *) symtab_data;
396   j = 0;
397   for (i = 0; i < sym_count; ++i, ++sym)
398     {
399       int info;
400
401       info = sym->st_info & 0xf;
402       if (info != STT_FUNC && info != STT_OBJECT)
403         continue;
404       if (sym->st_shndx == SHN_UNDEF)
405         continue;
406       if (sym->st_name >= strtab_size)
407         {
408           error_callback (data, "symbol string index out of range", 0);
409           backtrace_free (state, elf_symbols, elf_symbol_size, error_callback,
410                           data);
411           return 0;
412         }
413       elf_symbols[j].name = (const char *) strtab + sym->st_name;
414       elf_symbols[j].address = sym->st_value + base_address;
415       elf_symbols[j].size = sym->st_size;
416
417       if (symbol_size < sym->st_value + sym->st_size)
418         symbol_size = sym->st_value + sym->st_size;
419       ++j;
420     }
421
422   qsort (elf_symbols, elf_symbol_count, sizeof (struct elf_symbol),
423          elf_symbol_compare);
424
425   sdata->next = NULL;
426   sdata->symbols = elf_symbols;
427   sdata->count = elf_symbol_count;
428   sdata->base_address = base_address;
429   sdata->symbol_size = symbol_size;
430
431   return 1;
432 }
433
434 /* Add EDATA to the list in STATE.  */
435
436 static void
437 elf_add_syminfo_data (struct backtrace_state *state,
438                       struct elf_syminfo_data *edata)
439 {
440   if (!state->threaded)
441     {
442       struct elf_syminfo_data **pp;
443
444       for (pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
445            *pp != NULL;
446            pp = &(*pp)->next)
447         ;
448       *pp = edata;
449     }
450   else
451     {
452       while (1)
453         {
454           struct elf_syminfo_data **pp;
455
456           pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
457
458           while (1)
459             {
460               struct elf_syminfo_data *p;
461
462               p = backtrace_atomic_load_pointer (pp);
463
464               if (p == NULL)
465                 break;
466
467               pp = &p->next;
468             }
469
470           if (__sync_bool_compare_and_swap (pp, NULL, edata))
471             break;
472         }
473     }
474 }
475
476 /* Return the symbol name and value for an ADDR.  */
477
478 static void
479 elf_syminfo (struct backtrace_state *state, uintptr_t addr,
480              backtrace_syminfo_callback callback,
481              backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
482              void *data)
483 {
484   struct elf_syminfo_data *edata;
485   struct elf_symbol *sym = NULL;
486
487   if (!state->threaded)
488     {
489       for (edata = (struct elf_syminfo_data *) state->syminfo_data;
490            edata != NULL;
491            edata = edata->next)
492         {
493           if ((addr >= edata->base_address) && (addr < edata->base_address + edata->symbol_size))
494             {
495               sym = ((struct elf_symbol *)
496                      bsearch (&addr, edata->symbols, edata->count,
497                               sizeof (struct elf_symbol), elf_symbol_search));
498               if (sym != NULL)
499                 break;
500             }
501         }
502     }
503   else
504     {
505       struct elf_syminfo_data **pp;
506
507       pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
508       while (1)
509         {
510           edata = backtrace_atomic_load_pointer (pp);
511           if (edata == NULL)
512             break;
513
514           if ((addr >= edata->base_address) && (addr < edata->base_address + edata->symbol_size))
515             {
516               sym = ((struct elf_symbol *)
517                      bsearch (&addr, edata->symbols, edata->count,
518                               sizeof (struct elf_symbol), elf_symbol_search));
519               if (sym != NULL)
520                 break;
521             }
522
523           pp = &edata->next;
524         }
525     }
526
527   if (sym == NULL)
528     callback (data, addr, NULL, 0, 0);
529   else
530     callback (data, addr, sym->name, sym->address, sym->size);
531 }
532
533 /* Search this section for the .gnu_debuglink build id uuid. */
534
535 static uint32_t 
536 elf_parse_gnu_buildid(const uint8_t *data, size_t len, uint8_t uuid[20])
537 {
538     typedef struct
539     {
540         uint32_t name_len;  // Length of note name
541         uint32_t desc_len;  // Length of note descriptor
542         uint32_t type;      // Type of note (1 is ABI_TAG, 3 is BUILD_ID)
543         char name[4];       // "GNU\0"
544         uint8_t uuid[16];   // This can be 16 or 20 bytes
545     } elf_notehdr;
546
547     /* Try to parse the note section (ie .note.gnu.build-id|.notes|.note|...) and get the build id.
548        BuildID documentation: https://fedoraproject.org/wiki/Releases/FeatureBuildId */
549     static const uint32_t s_gnu_build_id = 3; // NT_GNU_BUILD_ID from elf.h
550
551     while (len >= sizeof(elf_notehdr))
552     {
553         const elf_notehdr *notehdr = (const elf_notehdr *)data;
554         uint32_t name_len = (notehdr->name_len + 3) & ~3;
555         uint32_t desc_len = (notehdr->desc_len + 3) & ~3;
556         size_t offset_next_note = name_len + desc_len;
557
558         /* 16 bytes is UUID|MD5, 20 bytes is SHA1 */
559         if ((notehdr->type == s_gnu_build_id) &&
560             (desc_len == 16 || desc_len == 20) &&
561             (name_len == 4) && !strcmp(notehdr->name, "GNU"))
562         {
563             const uint8_t *uuidbuf = data + offsetof(elf_notehdr, uuid);
564
565             memcpy(uuid, uuidbuf, desc_len);
566             return desc_len;
567         }
568
569         if (offset_next_note >= len)
570             break;
571         data += offset_next_note;
572         len -= offset_next_note;
573     }
574
575     return 0;
576 }
577
578 static void
579 uuid_to_str(char *uuid_str, const uint8_t *uuid, int len) 
580 {
581     int i;
582     static const char hex[] = "0123456789abcdef";
583
584     for (i = 0; i < len; i++)
585       {
586         uint8_t c = *uuid++;
587
588         *uuid_str++ = hex[c >> 4];
589         *uuid_str++ = hex[c & 0xf];
590       }    
591     *uuid_str = 0;
592 }
593
594 static int
595 elf_add (struct backtrace_state *state, const char *filename, uintptr_t base_address,
596          backtrace_error_callback error_callback, void *data,
597          fileline *fileline_fn, int *found_sym, int *found_dwarf, int exe,
598          const uint8_t *uuid_to_match, uint32_t uuid_to_match_len);
599
600 /* Search and add the backtrace for a gnu_debuglink file. */
601
602 static int
603 elf_add_gnu_debuglink (struct backtrace_state *state, const char *modulename, const char *gnu_debuglink,
604                        uintptr_t base_address, backtrace_error_callback error_callback, void *data,
605                        fileline *fileline_fn, int *found_sym, int *found_dwarf,
606                        const uint8_t *uuid_to_match, uint32_t uuid_to_match_len)
607 {
608   int pass;
609   char file[PATH_MAX];
610   char uuid_str[20 * 2 + 1];
611   const char *homedir = NULL;
612   const char *modulefile = strrchr(modulename, '/');
613   int moduledirlen = modulefile ? (modulefile - modulename) : 0;
614
615   uuid_to_str(uuid_str, uuid_to_match, uuid_to_match_len); 
616
617   for (pass = 0;; ++pass)
618     {
619       file[0] = 0;
620
621       switch (pass)
622       {
623       case 0:
624         /* Try the gnu_debuglink filename we were passed in. */
625         strncpy(file, gnu_debuglink, sizeof(file));
626         break;
627
628       case 1:
629         if (moduledirlen > 0)
630         {
631           /* Try current module directory. */
632           snprintf(file, sizeof(file), "%.*s/%s", moduledirlen, modulename, gnu_debuglink);
633         }
634         break;
635
636       case 2:
637         {
638           /* current exe directory. */
639           char buf[PATH_MAX];
640           int len = readlink("/proc/self/exe", buf, sizeof(buf));
641           if (len > 0)
642             {
643               snprintf(file, sizeof(file), "%.*s/%s", len, buf, gnu_debuglink);
644             }
645         }
646         break;
647
648       case 3:
649         if (moduledirlen > 0)
650         {
651           /* Try /lib/x86_64-linux-gnu/libc-2.15.so --> /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.15.so */
652           snprintf(file, sizeof(file), "/usr/lib/debug%.*s/%s", moduledirlen, modulename, gnu_debuglink);
653         }
654         break;
655
656       case 4:
657         {
658           struct passwd *pw = getpwuid(getuid());
659           homedir = pw ? pw->pw_dir : NULL;
660           if (homedir)
661             {
662               /* try ~/.debug/.build-id/af/1da84a6c83719a4c92cc6e7144623c5a1d1f6d */
663               snprintf(file, sizeof(file), "%s/.debug/.build-id/%.2s/%s", homedir, uuid_str, uuid_str + 2);
664             }
665         }
666         break;
667
668       case 5:
669         if (homedir)
670         {
671             /* try /home/mikesart/.debug/.build-id/af/1da84a6c83719a4c92cc6e7144623c5a1d1f6d.debug */
672             snprintf(file, sizeof(file), "%s/.debug/.build-id/%.2s/%s.debug", homedir, uuid_str, uuid_str + 2);
673         }
674         break;
675
676       case 6:
677           snprintf(file, sizeof(file), "%s/.build-id/%.2s/%s.debug", "/mnt/symstoresymbols/Debug", uuid_str, uuid_str + 2);
678           //$ TODO: mikesart
679           // Check debug-file-directory from .gdbinit maybe?
680           //   /mnt/symstoresymbols/Debug/.build-id/f5/a99d37b2b105c50bccc3fd87e4cd27492f3032.debug
681           //   set debug-file-directory /usr/lib/debug:/mnt/symstoresymbols/debug
682           break;
683
684       default:
685         return 0;
686       }
687
688       file[sizeof(file) - 1] = 0;
689
690       /* If we've got a filename and the file exists, try loading it. */
691
692       if (file[0] && (access(file, F_OK) == 0))
693         {
694           if (elf_add(state, file, base_address,
695                       error_callback, data,
696                       fileline_fn, found_sym, found_dwarf, 0,
697                       uuid_to_match, uuid_to_match_len))
698           {
699             /* Success. Woot! Return our fresh new symbols. */
700             return 1;
701           }
702         }
703     }
704
705   return 0;
706 }
707
708
709 /* Add the backtrace data for one ELF file.  */
710
711 static int
712 elf_add (struct backtrace_state *state, const char *filename, uintptr_t base_address,
713          backtrace_error_callback error_callback, void *data,
714          fileline *fileline_fn, int *found_sym, int *found_dwarf, int exe,
715          const uint8_t *uuid_to_match, uint32_t uuid_to_match_len)
716 {
717   struct backtrace_view ehdr_view;
718   b_elf_ehdr ehdr;
719   off_t shoff;
720   unsigned int shnum;
721   unsigned int shstrndx;
722   struct backtrace_view shdrs_view;
723   int shdrs_view_valid;
724   const b_elf_shdr *shdrs;
725   const b_elf_shdr *shstrhdr;
726   size_t shstr_size;
727   off_t shstr_off;
728   struct backtrace_view names_view;
729   int names_view_valid;
730   const char *names;
731   unsigned int symtab_shndx;
732   unsigned int dynsym_shndx;
733   unsigned int i;
734   struct debug_section_info sections[DEBUG_MAX];
735   struct backtrace_view symtab_view;
736   int symtab_view_valid;
737   struct backtrace_view strtab_view;
738   int strtab_view_valid;
739   off_t min_offset;
740   off_t max_offset;
741   struct backtrace_view debug_view;
742   int debug_view_valid;
743   int retval = 0;
744   int uuid_len = 0;
745   uint8_t uuid[20];
746
747   *found_sym = 0;
748   *found_dwarf = 0;
749
750   shdrs_view_valid = 0;
751   names_view_valid = 0;
752   symtab_view_valid = 0;
753   strtab_view_valid = 0;
754   debug_view_valid = 0;
755
756   int descriptor = backtrace_open (filename, error_callback, data, NULL);
757   if (descriptor < 0)
758     return 0;
759
760   if (!backtrace_get_view (state, descriptor, 0, sizeof ehdr, error_callback,
761                            data, &ehdr_view))
762     goto fail;
763
764   memcpy (&ehdr, ehdr_view.data, sizeof ehdr);
765
766   backtrace_release_view (state, &ehdr_view, error_callback, data);
767
768   if (ehdr.e_ident[EI_MAG0] != ELFMAG0
769       || ehdr.e_ident[EI_MAG1] != ELFMAG1
770       || ehdr.e_ident[EI_MAG2] != ELFMAG2
771       || ehdr.e_ident[EI_MAG3] != ELFMAG3)
772     {
773       error_callback (data, "executable file is not ELF", 0);
774       goto fail;
775     }
776   if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
777     {
778       error_callback (data, "executable file is unrecognized ELF version", 0);
779       goto fail;
780     }
781
782 #if BACKTRACE_ELF_SIZE == 32
783 #define BACKTRACE_ELFCLASS ELFCLASS32
784 #else
785 #define BACKTRACE_ELFCLASS ELFCLASS64
786 #endif
787
788   if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS)
789     {
790       error_callback (data, "executable file is unexpected ELF class", 0);
791       goto fail;
792     }
793
794   if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB
795       && ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
796     {
797       error_callback (data, "executable file has unknown endianness", 0);
798       goto fail;
799     }
800
801   /* If the executable is ET_DYN, it is either a PIE, or we are running
802      directly a shared library with .interp.  We need to wait for
803      dl_iterate_phdr in that case to determine the actual base_address.  */
804   if (exe)
805   {
806     /* If we were given a base address and this isn't PIE, set addr to 0. */
807     if (base_address && (ehdr.e_type != ET_DYN))
808       base_address = 0;
809     else if (!base_address && (ehdr.e_type == ET_DYN))
810       goto succeeded;
811   }
812
813   shoff = ehdr.e_shoff;
814   shnum = ehdr.e_shnum;
815   shstrndx = ehdr.e_shstrndx;
816
817   if ((shnum == 0 || shstrndx == SHN_XINDEX)
818       && shoff != 0)
819     {
820       struct backtrace_view shdr_view;
821       const b_elf_shdr *shdr;
822
823       if (!backtrace_get_view (state, descriptor, shoff, sizeof shdr,
824                                error_callback, data, &shdr_view))
825         goto fail;
826
827       shdr = (const b_elf_shdr *) shdr_view.data;
828
829       if (shnum == 0)
830         shnum = shdr->sh_size;
831
832       if (shstrndx == SHN_XINDEX)
833         {
834           shstrndx = shdr->sh_link;
835
836           /* Versions of the GNU binutils between 2.12 and 2.18 did
837              not handle objects with more than SHN_LORESERVE sections
838              correctly.  All large section indexes were offset by
839              0x100.  There is more information at
840              http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
841              Fortunately these object files are easy to detect, as the
842              GNU binutils always put the section header string table
843              near the end of the list of sections.  Thus if the
844              section header string table index is larger than the
845              number of sections, then we know we have to subtract
846              0x100 to get the real section index.  */
847           if (shstrndx >= shnum && shstrndx >= SHN_LORESERVE + 0x100)
848             shstrndx -= 0x100;
849         }
850
851       backtrace_release_view (state, &shdr_view, error_callback, data);
852     }
853
854   /* To translate PC to file/line when using DWARF, we need to find
855      the .debug_info and .debug_line sections.  */
856
857   /* Read the section headers, skipping the first one.  */
858
859   if (!backtrace_get_view (state, descriptor, shoff + sizeof (b_elf_shdr),
860                            (shnum - 1) * sizeof (b_elf_shdr),
861                            error_callback, data, &shdrs_view))
862     goto fail;
863   shdrs_view_valid = 1;
864   shdrs = (const b_elf_shdr *) shdrs_view.data;
865
866   /* Read the section names.  */
867
868   shstrhdr = &shdrs[shstrndx - 1];
869   shstr_size = shstrhdr->sh_size;
870   shstr_off = shstrhdr->sh_offset;
871
872   if (!backtrace_get_view (state, descriptor, shstr_off, shstr_size,
873                            error_callback, data, &names_view))
874     goto fail;
875   names_view_valid = 1;
876   names = (const char *) names_view.data;
877
878   symtab_shndx = 0;
879   dynsym_shndx = 0;
880
881   memset (sections, 0, sizeof sections);
882   memset (uuid, 0, sizeof(uuid));
883
884   /* Look for the symbol table.  */
885   for (i = 1; i < shnum; ++i)
886     {
887       const b_elf_shdr *shdr;
888       unsigned int sh_name;
889       const char *name;
890       int j;
891
892       shdr = &shdrs[i - 1];
893
894       if (shdr->sh_type == SHT_SYMTAB)
895         symtab_shndx = i;
896       else if (shdr->sh_type == SHT_DYNSYM)
897         dynsym_shndx = i;
898       else if ((shdr->sh_type == SHT_NOTE) && (uuid_len == 0))
899       {
900         struct backtrace_view note_view;
901
902         if (backtrace_get_view (state, descriptor, shdr->sh_offset,
903                                  shdr->sh_size, error_callback, data,
904                                  &note_view))
905         {
906           uuid_len = elf_parse_gnu_buildid(note_view.data, shdr->sh_size, uuid);
907           backtrace_release_view (state, &note_view, error_callback, data);
908         }
909       }
910
911       sh_name = shdr->sh_name;
912       if (sh_name >= shstr_size)
913         {
914           error_callback (data, "ELF section name out of range", 0);
915           goto fail;
916         }
917
918       name = names + sh_name;
919
920       for (j = 0; j < (int) DEBUG_MAX; ++j)
921         {
922           if (strcmp (name, debug_section_names[j]) == 0)
923             {
924               sections[j].offset = shdr->sh_offset;
925               sections[j].size = shdr->sh_size;
926               break;
927             }
928         }
929     }
930
931   if (uuid_to_match_len)
932     {
933       /* We're supposed to be looking for a gnu_debuglink file... */
934
935       /* Found debug file but uuids don't match... */
936       if ((uuid_to_match_len != uuid_len) || memcmp(uuid_to_match, uuid, uuid_len))
937         goto fail;
938
939       /* Found debug file, but no .debuginfo in it? */
940       if (!sections[DEBUG_INFO].size)
941         goto fail;
942     }
943   else if (uuid_len && !sections[DEBUG_INFO].size && sections[GNU_DEBUGLINK].size)
944     {
945       /* We're not searching for a gnu_debuglink file, we have no .debug_info section, we
946          have a uuid, and we have a gnu_debuglink filename - try looking for this debug file. */
947       struct backtrace_view gnu_debuglink_view;
948
949       if (backtrace_get_view (state, descriptor, sections[GNU_DEBUGLINK].offset,
950                               sections[GNU_DEBUGLINK].size, error_callback, data,
951                               &gnu_debuglink_view))
952         {
953           int added = 0;
954           int debug_found_sym = 0;
955           int debug_found_dwarf = 0;
956           const char *gnu_debuglink_file = gnu_debuglink_view.data;
957
958           if (gnu_debuglink_file && gnu_debuglink_file[0])
959             {
960               added = elf_add_gnu_debuglink (state, filename, gnu_debuglink_file, base_address, error_callback,
961                                              data, fileline_fn, &debug_found_sym, &debug_found_dwarf,
962                                              uuid, uuid_len);
963             }
964
965           backtrace_release_view (state, &gnu_debuglink_view, error_callback, data);
966           gnu_debuglink_file = NULL;
967
968           /* If we found symbols of any kind, head on out... */
969           if (added && (debug_found_sym || debug_found_dwarf))
970             {
971               *found_sym = debug_found_sym;
972               *found_dwarf = debug_found_dwarf;
973               goto succeeded;
974             }
975         }
976     }
977
978   if (symtab_shndx == 0)
979     symtab_shndx = dynsym_shndx;
980   if (symtab_shndx != 0)
981     {
982       const b_elf_shdr *symtab_shdr;
983       unsigned int strtab_shndx;
984       const b_elf_shdr *strtab_shdr;
985       struct elf_syminfo_data *sdata;
986
987       symtab_shdr = &shdrs[symtab_shndx - 1];
988       strtab_shndx = symtab_shdr->sh_link;
989       if (strtab_shndx >= shnum)
990         {
991           error_callback (data,
992                           "ELF symbol table strtab link out of range", 0);
993           goto fail;
994         }
995       strtab_shdr = &shdrs[strtab_shndx - 1];
996
997       if (!backtrace_get_view (state, descriptor, symtab_shdr->sh_offset,
998                                symtab_shdr->sh_size, error_callback, data,
999                                &symtab_view))
1000         goto fail;
1001       symtab_view_valid = 1;
1002
1003       if (!backtrace_get_view (state, descriptor, strtab_shdr->sh_offset,
1004                                strtab_shdr->sh_size, error_callback, data,
1005                                &strtab_view))
1006         goto fail;
1007       strtab_view_valid = 1;
1008
1009       sdata = ((struct elf_syminfo_data *)
1010                backtrace_alloc (state, sizeof *sdata, error_callback, data));
1011       if (sdata == NULL)
1012         goto fail;
1013
1014       if (!elf_initialize_syminfo (state, base_address,
1015                                    symtab_view.data, symtab_shdr->sh_size,
1016                                    strtab_view.data, strtab_shdr->sh_size,
1017                                    error_callback, data, sdata))
1018         {
1019           backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
1020           goto fail;
1021         }
1022
1023       /* We no longer need the symbol table, but we hold on to the
1024          string table permanently.  */
1025       backtrace_release_view (state, &symtab_view, error_callback, data);
1026
1027       *found_sym = 1;
1028
1029       elf_add_syminfo_data (state, sdata);
1030     }
1031
1032   /* FIXME: Need to handle compressed debug sections.  */
1033
1034   backtrace_release_view (state, &shdrs_view, error_callback, data);
1035   shdrs_view_valid = 0;
1036   backtrace_release_view (state, &names_view, error_callback, data);
1037   names_view_valid = 0;
1038
1039   /* Read all the debug sections in a single view, since they are
1040      probably adjacent in the file.  We never release this view.  */
1041
1042   min_offset = 0;
1043   max_offset = 0;
1044   for (i = 0; i < (int) DEBUG_MAX; ++i)
1045     {
1046       off_t end;
1047
1048       if (sections[i].size == 0)
1049         continue;
1050       if (min_offset == 0 || sections[i].offset < min_offset)
1051         min_offset = sections[i].offset;
1052       end = sections[i].offset + sections[i].size;
1053       if (end > max_offset)
1054         max_offset = end;
1055     }
1056   if (min_offset == 0 || max_offset == 0)
1057     {
1058       if (!backtrace_close (descriptor, error_callback, data))
1059       {
1060         descriptor = -1;
1061         goto fail;
1062       }
1063       *fileline_fn = elf_nodebug;
1064       return 1;
1065     }
1066
1067   if (!backtrace_get_view (state, descriptor, min_offset,
1068                            max_offset - min_offset,
1069                            error_callback, data, &debug_view))
1070     goto fail;
1071   debug_view_valid = 1;
1072
1073   /* We've read all we need from the executable.  */
1074   if (!backtrace_close (descriptor, error_callback, data))
1075   {
1076     descriptor = -1;
1077     goto fail;
1078   }
1079   descriptor = -1;
1080
1081   for (i = 0; i < (int) DEBUG_MAX; ++i)
1082     {
1083       if (sections[i].size == 0)
1084         sections[i].data = NULL;
1085       else
1086         sections[i].data = ((const unsigned char *) debug_view.data
1087                             + (sections[i].offset - min_offset));
1088     }
1089
1090   if (!backtrace_dwarf_add (state, base_address,
1091                             sections[DEBUG_INFO].data,
1092                             sections[DEBUG_INFO].size,
1093                             sections[DEBUG_LINE].data,
1094                             sections[DEBUG_LINE].size,
1095                             sections[DEBUG_ABBREV].data,
1096                             sections[DEBUG_ABBREV].size,
1097                             sections[DEBUG_RANGES].data,
1098                             sections[DEBUG_RANGES].size,
1099                             sections[DEBUG_STR].data,
1100                             sections[DEBUG_STR].size,
1101                             ehdr.e_ident[EI_DATA] == ELFDATA2MSB,
1102                             error_callback, data, fileline_fn))
1103     goto fail;
1104
1105   state->debug_filename = backtrace_strdup(state, filename, error_callback, data);
1106   *found_dwarf = 1;
1107   return 1;
1108
1109 succeeded:
1110   retval = 1;
1111
1112 fail:
1113   if (shdrs_view_valid)
1114     backtrace_release_view (state, &shdrs_view, error_callback, data);
1115   if (names_view_valid)
1116     backtrace_release_view (state, &names_view, error_callback, data);
1117   if (symtab_view_valid)
1118     backtrace_release_view (state, &symtab_view, error_callback, data);
1119   if (strtab_view_valid)
1120     backtrace_release_view (state, &strtab_view, error_callback, data);
1121   if (debug_view_valid)
1122     backtrace_release_view (state, &debug_view, error_callback, data);
1123   if (descriptor != -1)
1124     backtrace_close (descriptor, error_callback, data);
1125   return retval;
1126 }
1127
1128 /* Data passed to phdr_callback.  */
1129
1130 struct phdr_data
1131 {
1132   struct backtrace_state *state;
1133   backtrace_error_callback error_callback;
1134   void *data;
1135   fileline *fileline_fn;
1136   int *found_sym;
1137   int *found_dwarf;
1138   const char * exe_filename;
1139 };
1140
1141 /* Callback passed to dl_iterate_phdr.  Load debug info from shared
1142    libraries.  */
1143
1144 static int
1145 phdr_callback (struct dl_phdr_info *info, size_t size ATTRIBUTE_UNUSED,
1146                void *pdata)
1147 {
1148   struct phdr_data *pd = (struct phdr_data *) pdata;
1149   fileline elf_fileline_fn;
1150   int found_dwarf;
1151   const char * filename = info->dlpi_name;
1152
1153   /* There is not much we can do if we don't have the module name,
1154      unless executable is ET_DYN, where we expect the very first
1155      phdr_callback to be for the PIE.  */
1156   if (filename == NULL || filename[0] == '\0')
1157     {
1158       if (!pd->exe_filename)
1159         return 0;
1160       filename = pd->exe_filename;
1161       pd->exe_filename = NULL;
1162     }
1163
1164   if (elf_add (pd->state, filename, info->dlpi_addr, pd->error_callback,
1165                pd->data, &elf_fileline_fn, pd->found_sym, &found_dwarf, 0,
1166                NULL, 0))
1167     {
1168       if (found_dwarf)
1169         {
1170           *pd->found_dwarf = 1;
1171           *pd->fileline_fn = elf_fileline_fn;
1172         }
1173     }
1174
1175   return 0;
1176 }
1177
1178 /* Initialize the backtrace data we need from an ELF executable.  At
1179    the ELF level, all we need to do is find the debug info
1180    sections.  */
1181
1182 int
1183 backtrace_initialize (struct backtrace_state *state, const char *filename,
1184                       uintptr_t base_address, int is_exe,
1185                       backtrace_error_callback error_callback,
1186                       void *data, fileline *fileline_fn)
1187 {
1188   int ret;
1189   int found_sym;
1190   int found_dwarf;
1191   fileline elf_fileline_fn;
1192
1193   ret = elf_add (state, filename, base_address, error_callback, data, &elf_fileline_fn,
1194                 &found_sym, &found_dwarf, is_exe, NULL, 0);
1195   if (!ret)
1196     return 0;
1197
1198   if (!base_address)
1199     {
1200       struct phdr_data pd;
1201
1202       pd.state = state;
1203       pd.error_callback = error_callback;
1204       pd.data = data;
1205       pd.fileline_fn = &elf_fileline_fn;
1206       pd.found_sym = &found_sym;
1207       pd.found_dwarf = &found_dwarf;
1208       pd.exe_filename = filename;
1209     
1210       dl_iterate_phdr (phdr_callback, (void *) &pd);
1211     }
1212
1213   if (!state->threaded)
1214     {
1215       if (found_sym)
1216         state->syminfo_fn = elf_syminfo;
1217       else if (state->syminfo_fn == NULL)
1218         state->syminfo_fn = elf_nosyms;
1219     }
1220   else
1221     {
1222       if (found_sym)
1223         backtrace_atomic_store_pointer (&state->syminfo_fn, elf_syminfo);
1224       else
1225         __sync_bool_compare_and_swap (&state->syminfo_fn, NULL, elf_nosyms);
1226     }
1227
1228   if (!state->threaded)
1229     {
1230       if (state->fileline_fn == NULL || state->fileline_fn == elf_nodebug)
1231         *fileline_fn = elf_fileline_fn;
1232     }
1233   else
1234     {
1235       fileline current_fn;
1236
1237       current_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
1238       if (current_fn == NULL || current_fn == elf_nodebug)
1239         *fileline_fn = elf_fileline_fn;
1240     }
1241
1242   return 1;
1243 }
1244 static void
1245 elf_get_uuid_error_callback(void *data, const char *msg, int errnum)
1246 {
1247     // Do nothing here. elf_get_uuid() will return the error.
1248 }
1249
1250 int
1251 elf_get_uuid (struct backtrace_state *state,
1252               const char *filename, uint8_t uuid[20], int *uuid_len)
1253 {
1254   struct backtrace_view ehdr_view;
1255   b_elf_ehdr ehdr;
1256   off_t shoff;
1257   unsigned int shnum;
1258   struct backtrace_view shdrs_view;
1259   int shdrs_view_valid = 0;
1260   const b_elf_shdr *shdrs;
1261   unsigned int i;
1262   void *data = NULL;
1263   int retval = 0;
1264   struct backtrace_state *state_alloced = NULL;
1265
1266   if (!state)
1267   {
1268       state_alloced = backtrace_create_state(filename, 0, elf_get_uuid_error_callback, NULL);
1269       state = state_alloced;
1270   }
1271
1272   int descriptor = backtrace_open (filename, elf_get_uuid_error_callback, data, NULL);
1273   if (descriptor < 0)
1274     return 0;
1275
1276   if (!backtrace_get_view (state, descriptor, 0, sizeof ehdr, elf_get_uuid_error_callback,
1277                            data, &ehdr_view))
1278     goto fail;
1279
1280   memcpy (&ehdr, ehdr_view.data, sizeof ehdr);
1281
1282   backtrace_release_view (state, &ehdr_view, elf_get_uuid_error_callback, data);
1283
1284   if (ehdr.e_ident[EI_MAG0] != ELFMAG0
1285       || ehdr.e_ident[EI_MAG1] != ELFMAG1
1286       || ehdr.e_ident[EI_MAG2] != ELFMAG2
1287       || ehdr.e_ident[EI_MAG3] != ELFMAG3)
1288     {
1289       // elf_get_uuid_error_callback (data, "executable file is not ELF", 0);
1290       goto fail;
1291     }
1292   if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
1293     {
1294       // elf_get_uuid_error_callback (data, "executable file is unrecognized ELF version", 0);
1295       goto fail;
1296     }
1297
1298 #if BACKTRACE_ELF_SIZE == 32
1299 #define BACKTRACE_ELFCLASS ELFCLASS32
1300 #else
1301 #define BACKTRACE_ELFCLASS ELFCLASS64
1302 #endif
1303
1304   if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS)
1305     {
1306       // elf_get_uuid_error_callback (data, "executable file is unexpected ELF class", 0);
1307       goto fail;
1308     }
1309
1310   if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB
1311       && ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
1312     {
1313       // elf_get_uuid_error_callback (data, "executable file has unknown endianness", 0);
1314       goto fail;
1315     }
1316
1317   shoff = ehdr.e_shoff;
1318   shnum = ehdr.e_shnum;
1319
1320   if ((shnum == 0) && (shoff != 0))
1321     {
1322       struct backtrace_view shdr_view;
1323       const b_elf_shdr *shdr;
1324
1325       if (!backtrace_get_view (state, descriptor, shoff, sizeof shdr,
1326                                elf_get_uuid_error_callback, data, &shdr_view))
1327         goto fail;
1328
1329       shdr = (const b_elf_shdr *) shdr_view.data;
1330
1331       if (shnum == 0)
1332         shnum = shdr->sh_size;
1333
1334       backtrace_release_view (state, &shdr_view, elf_get_uuid_error_callback, data);
1335     }
1336
1337   if (!backtrace_get_view (state, descriptor, shoff + sizeof (b_elf_shdr),
1338                            (shnum - 1) * sizeof (b_elf_shdr),
1339                            elf_get_uuid_error_callback, data, &shdrs_view))
1340     goto fail;
1341   shdrs_view_valid = 1;
1342   shdrs = (const b_elf_shdr *) shdrs_view.data;
1343
1344   for (i = 1; i < shnum; ++i)
1345     {
1346       const b_elf_shdr *shdr = &shdrs[i - 1];
1347
1348       if (shdr->sh_type == SHT_NOTE)
1349       {
1350         struct backtrace_view note_view;
1351
1352         if (backtrace_get_view (state, descriptor, shdr->sh_offset,
1353                                  shdr->sh_size, elf_get_uuid_error_callback, data,
1354                                  &note_view))
1355         {
1356           *uuid_len = elf_parse_gnu_buildid(note_view.data, shdr->sh_size, uuid);
1357           backtrace_release_view (state, &note_view, elf_get_uuid_error_callback, data);
1358           if (*uuid_len)
1359           {
1360              retval = 1;
1361              break;
1362           }
1363         }
1364       }
1365     }
1366
1367 fail:
1368   if (shdrs_view_valid)
1369     backtrace_release_view (state, &shdrs_view, elf_get_uuid_error_callback, data);
1370   if (descriptor != -1)
1371     backtrace_close (descriptor, elf_get_uuid_error_callback, data);
1372   if (state_alloced)
1373     backtrace_free(state_alloced, state_alloced, sizeof(*state_alloced), elf_get_uuid_error_callback, NULL);
1374   return retval;
1375 }