PR other/82368
[official-gcc.git] / libbacktrace / elf.c
blob8ff3c47320e3469a08c8f4d02942c7c6952b2273
1 /* elf.c -- Get debug data from an ELF file for backtraces.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
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.
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.
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. */
33 #include "config.h"
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
42 #ifdef HAVE_DL_ITERATE_PHDR
43 #include <link.h>
44 #endif
46 #include "backtrace.h"
47 #include "internal.h"
49 #ifndef S_ISLNK
50 #ifndef S_IFLNK
51 #define S_IFLNK 0120000
52 #endif
53 #ifndef S_IFMT
54 #define S_IFMT 0170000
55 #endif
56 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
57 #endif
59 #ifndef __GNUC__
60 #define __builtin_prefetch(p, r, l)
61 #define unlikely(x) (x)
62 #else
63 #define unlikely(x) __builtin_expect(!!(x), 0)
64 #endif
66 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
68 /* If strnlen is not declared, provide our own version. */
70 static size_t
71 xstrnlen (const char *s, size_t maxlen)
73 size_t i;
75 for (i = 0; i < maxlen; ++i)
76 if (s[i] == '\0')
77 break;
78 return i;
81 #define strnlen xstrnlen
83 #endif
85 #ifndef HAVE_LSTAT
87 /* Dummy version of lstat for systems that don't have it. */
89 static int
90 xlstat (const char *path ATTRIBUTE_UNUSED, struct stat *st ATTRIBUTE_UNUSED)
92 return -1;
95 #define lstat xlstat
97 #endif
99 #ifndef HAVE_READLINK
101 /* Dummy version of readlink for systems that don't have it. */
103 static ssize_t
104 xreadlink (const char *path ATTRIBUTE_UNUSED, char *buf ATTRIBUTE_UNUSED,
105 size_t bufsz ATTRIBUTE_UNUSED)
107 return -1;
110 #define readlink xreadlink
112 #endif
114 #ifndef HAVE_DL_ITERATE_PHDR
116 /* Dummy version of dl_iterate_phdr for systems that don't have it. */
118 #define dl_phdr_info x_dl_phdr_info
119 #define dl_iterate_phdr x_dl_iterate_phdr
121 struct dl_phdr_info
123 uintptr_t dlpi_addr;
124 const char *dlpi_name;
127 static int
128 dl_iterate_phdr (int (*callback) (struct dl_phdr_info *,
129 size_t, void *) ATTRIBUTE_UNUSED,
130 void *data ATTRIBUTE_UNUSED)
132 return 0;
135 #endif /* ! defined (HAVE_DL_ITERATE_PHDR) */
137 /* The configure script must tell us whether we are 32-bit or 64-bit
138 ELF. We could make this code test and support either possibility,
139 but there is no point. This code only works for the currently
140 running executable, which means that we know the ELF mode at
141 configure time. */
143 #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64
144 #error "Unknown BACKTRACE_ELF_SIZE"
145 #endif
147 /* <link.h> might #include <elf.h> which might define our constants
148 with slightly different values. Undefine them to be safe. */
150 #undef EI_NIDENT
151 #undef EI_MAG0
152 #undef EI_MAG1
153 #undef EI_MAG2
154 #undef EI_MAG3
155 #undef EI_CLASS
156 #undef EI_DATA
157 #undef EI_VERSION
158 #undef ELF_MAG0
159 #undef ELF_MAG1
160 #undef ELF_MAG2
161 #undef ELF_MAG3
162 #undef ELFCLASS32
163 #undef ELFCLASS64
164 #undef ELFDATA2LSB
165 #undef ELFDATA2MSB
166 #undef EV_CURRENT
167 #undef ET_DYN
168 #undef EM_PPC64
169 #undef EF_PPC64_ABI
170 #undef SHN_LORESERVE
171 #undef SHN_XINDEX
172 #undef SHN_UNDEF
173 #undef SHT_SYMTAB
174 #undef SHT_STRTAB
175 #undef SHT_DYNSYM
176 #undef SHF_COMPRESSED
177 #undef STT_OBJECT
178 #undef STT_FUNC
179 #undef NT_GNU_BUILD_ID
180 #undef ELFCOMPRESS_ZLIB
182 /* Basic types. */
184 typedef uint16_t b_elf_half; /* Elf_Half. */
185 typedef uint32_t b_elf_word; /* Elf_Word. */
186 typedef int32_t b_elf_sword; /* Elf_Sword. */
188 #if BACKTRACE_ELF_SIZE == 32
190 typedef uint32_t b_elf_addr; /* Elf_Addr. */
191 typedef uint32_t b_elf_off; /* Elf_Off. */
193 typedef uint32_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
195 #else
197 typedef uint64_t b_elf_addr; /* Elf_Addr. */
198 typedef uint64_t b_elf_off; /* Elf_Off. */
199 typedef uint64_t b_elf_xword; /* Elf_Xword. */
200 typedef int64_t b_elf_sxword; /* Elf_Sxword. */
202 typedef uint64_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
204 #endif
206 /* Data structures and associated constants. */
208 #define EI_NIDENT 16
210 typedef struct {
211 unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
212 b_elf_half e_type; /* Identifies object file type */
213 b_elf_half e_machine; /* Specifies required architecture */
214 b_elf_word e_version; /* Identifies object file version */
215 b_elf_addr e_entry; /* Entry point virtual address */
216 b_elf_off e_phoff; /* Program header table file offset */
217 b_elf_off e_shoff; /* Section header table file offset */
218 b_elf_word e_flags; /* Processor-specific flags */
219 b_elf_half e_ehsize; /* ELF header size in bytes */
220 b_elf_half e_phentsize; /* Program header table entry size */
221 b_elf_half e_phnum; /* Program header table entry count */
222 b_elf_half e_shentsize; /* Section header table entry size */
223 b_elf_half e_shnum; /* Section header table entry count */
224 b_elf_half e_shstrndx; /* Section header string table index */
225 } b_elf_ehdr; /* Elf_Ehdr. */
227 #define EI_MAG0 0
228 #define EI_MAG1 1
229 #define EI_MAG2 2
230 #define EI_MAG3 3
231 #define EI_CLASS 4
232 #define EI_DATA 5
233 #define EI_VERSION 6
235 #define ELFMAG0 0x7f
236 #define ELFMAG1 'E'
237 #define ELFMAG2 'L'
238 #define ELFMAG3 'F'
240 #define ELFCLASS32 1
241 #define ELFCLASS64 2
243 #define ELFDATA2LSB 1
244 #define ELFDATA2MSB 2
246 #define EV_CURRENT 1
248 #define ET_DYN 3
250 #define EM_PPC64 21
251 #define EF_PPC64_ABI 3
253 typedef struct {
254 b_elf_word sh_name; /* Section name, index in string tbl */
255 b_elf_word sh_type; /* Type of section */
256 b_elf_wxword sh_flags; /* Miscellaneous section attributes */
257 b_elf_addr sh_addr; /* Section virtual addr at execution */
258 b_elf_off sh_offset; /* Section file offset */
259 b_elf_wxword sh_size; /* Size of section in bytes */
260 b_elf_word sh_link; /* Index of another section */
261 b_elf_word sh_info; /* Additional section information */
262 b_elf_wxword sh_addralign; /* Section alignment */
263 b_elf_wxword sh_entsize; /* Entry size if section holds table */
264 } b_elf_shdr; /* Elf_Shdr. */
266 #define SHN_UNDEF 0x0000 /* Undefined section */
267 #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
268 #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
270 #define SHT_SYMTAB 2
271 #define SHT_STRTAB 3
272 #define SHT_DYNSYM 11
274 #define SHF_COMPRESSED 0x800
276 #if BACKTRACE_ELF_SIZE == 32
278 typedef struct
280 b_elf_word st_name; /* Symbol name, index in string tbl */
281 b_elf_addr st_value; /* Symbol value */
282 b_elf_word st_size; /* Symbol size */
283 unsigned char st_info; /* Symbol binding and type */
284 unsigned char st_other; /* Visibility and other data */
285 b_elf_half st_shndx; /* Symbol section index */
286 } b_elf_sym; /* Elf_Sym. */
288 #else /* BACKTRACE_ELF_SIZE != 32 */
290 typedef struct
292 b_elf_word st_name; /* Symbol name, index in string tbl */
293 unsigned char st_info; /* Symbol binding and type */
294 unsigned char st_other; /* Visibility and other data */
295 b_elf_half st_shndx; /* Symbol section index */
296 b_elf_addr st_value; /* Symbol value */
297 b_elf_xword st_size; /* Symbol size */
298 } b_elf_sym; /* Elf_Sym. */
300 #endif /* BACKTRACE_ELF_SIZE != 32 */
302 #define STT_OBJECT 1
303 #define STT_FUNC 2
305 typedef struct
307 uint32_t namesz;
308 uint32_t descsz;
309 uint32_t type;
310 char name[1];
311 } b_elf_note;
313 #define NT_GNU_BUILD_ID 3
315 #if BACKTRACE_ELF_SIZE == 32
317 typedef struct
319 b_elf_word ch_type; /* Compresstion algorithm */
320 b_elf_word ch_size; /* Uncompressed size */
321 b_elf_word ch_addralign; /* Alignment for uncompressed data */
322 } b_elf_chdr; /* Elf_Chdr */
324 #else /* BACKTRACE_ELF_SIZE != 32 */
326 typedef struct
328 b_elf_word ch_type; /* Compression algorithm */
329 b_elf_word ch_reserved; /* Reserved */
330 b_elf_xword ch_size; /* Uncompressed size */
331 b_elf_xword ch_addralign; /* Alignment for uncompressed data */
332 } b_elf_chdr; /* Elf_Chdr */
334 #endif /* BACKTRACE_ELF_SIZE != 32 */
336 #define ELFCOMPRESS_ZLIB 1
338 /* An index of ELF sections we care about. */
340 enum debug_section
342 DEBUG_INFO,
343 DEBUG_LINE,
344 DEBUG_ABBREV,
345 DEBUG_RANGES,
346 DEBUG_STR,
348 /* The old style compressed sections. This list must correspond to
349 the list of normal debug sections. */
350 ZDEBUG_INFO,
351 ZDEBUG_LINE,
352 ZDEBUG_ABBREV,
353 ZDEBUG_RANGES,
354 ZDEBUG_STR,
356 DEBUG_MAX
359 /* Names of sections, indexed by enum elf_section. */
361 static const char * const debug_section_names[DEBUG_MAX] =
363 ".debug_info",
364 ".debug_line",
365 ".debug_abbrev",
366 ".debug_ranges",
367 ".debug_str",
368 ".zdebug_info",
369 ".zdebug_line",
370 ".zdebug_abbrev",
371 ".zdebug_ranges",
372 ".zdebug_str"
375 /* Information we gather for the sections we care about. */
377 struct debug_section_info
379 /* Section file offset. */
380 off_t offset;
381 /* Section size. */
382 size_t size;
383 /* Section contents, after read from file. */
384 const unsigned char *data;
385 /* Whether the SHF_COMPRESSED flag is set for the section. */
386 int compressed;
389 /* Information we keep for an ELF symbol. */
391 struct elf_symbol
393 /* The name of the symbol. */
394 const char *name;
395 /* The address of the symbol. */
396 uintptr_t address;
397 /* The size of the symbol. */
398 size_t size;
401 /* Information to pass to elf_syminfo. */
403 struct elf_syminfo_data
405 /* Symbols for the next module. */
406 struct elf_syminfo_data *next;
407 /* The ELF symbols, sorted by address. */
408 struct elf_symbol *symbols;
409 /* The number of symbols. */
410 size_t count;
413 /* Information about PowerPC64 ELFv1 .opd section. */
415 struct elf_ppc64_opd_data
417 /* Address of the .opd section. */
418 b_elf_addr addr;
419 /* Section data. */
420 const char *data;
421 /* Size of the .opd section. */
422 size_t size;
423 /* Corresponding section view. */
424 struct backtrace_view view;
427 /* Compute the CRC-32 of BUF/LEN. This uses the CRC used for
428 .gnu_debuglink files. */
430 static uint32_t
431 elf_crc32 (uint32_t crc, const unsigned char *buf, size_t len)
433 static const uint32_t crc32_table[256] =
435 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
436 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
437 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
438 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
439 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
440 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
441 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
442 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
443 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
444 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
445 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
446 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
447 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
448 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
449 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
450 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
451 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
452 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
453 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
454 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
455 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
456 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
457 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
458 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
459 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
460 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
461 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
462 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
463 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
464 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
465 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
466 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
467 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
468 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
469 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
470 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
471 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
472 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
473 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
474 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
475 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
476 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
477 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
478 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
479 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
480 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
481 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
482 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
483 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
484 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
485 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
486 0x2d02ef8d
488 const unsigned char *end;
490 crc = ~crc;
491 for (end = buf + len; buf < end; ++ buf)
492 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
493 return ~crc;
496 /* Return the CRC-32 of the entire file open at DESCRIPTOR. */
498 static uint32_t
499 elf_crc32_file (struct backtrace_state *state, int descriptor,
500 backtrace_error_callback error_callback, void *data)
502 struct stat st;
503 struct backtrace_view file_view;
504 uint32_t ret;
506 if (fstat (descriptor, &st) < 0)
508 error_callback (data, "fstat", errno);
509 return 0;
512 if (!backtrace_get_view (state, descriptor, 0, st.st_size, error_callback,
513 data, &file_view))
514 return 0;
516 ret = elf_crc32 (0, (const unsigned char *) file_view.data, st.st_size);
518 backtrace_release_view (state, &file_view, error_callback, data);
520 return ret;
523 /* A dummy callback function used when we can't find any debug info. */
525 static int
526 elf_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
527 uintptr_t pc ATTRIBUTE_UNUSED,
528 backtrace_full_callback callback ATTRIBUTE_UNUSED,
529 backtrace_error_callback error_callback, void *data)
531 error_callback (data, "no debug info in ELF executable", -1);
532 return 0;
535 /* A dummy callback function used when we can't find a symbol
536 table. */
538 static void
539 elf_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
540 uintptr_t addr ATTRIBUTE_UNUSED,
541 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
542 backtrace_error_callback error_callback, void *data)
544 error_callback (data, "no symbol table in ELF executable", -1);
547 /* Compare struct elf_symbol for qsort. */
549 static int
550 elf_symbol_compare (const void *v1, const void *v2)
552 const struct elf_symbol *e1 = (const struct elf_symbol *) v1;
553 const struct elf_symbol *e2 = (const struct elf_symbol *) v2;
555 if (e1->address < e2->address)
556 return -1;
557 else if (e1->address > e2->address)
558 return 1;
559 else
560 return 0;
563 /* Compare an ADDR against an elf_symbol for bsearch. We allocate one
564 extra entry in the array so that this can look safely at the next
565 entry. */
567 static int
568 elf_symbol_search (const void *vkey, const void *ventry)
570 const uintptr_t *key = (const uintptr_t *) vkey;
571 const struct elf_symbol *entry = (const struct elf_symbol *) ventry;
572 uintptr_t addr;
574 addr = *key;
575 if (addr < entry->address)
576 return -1;
577 else if (addr >= entry->address + entry->size)
578 return 1;
579 else
580 return 0;
583 /* Initialize the symbol table info for elf_syminfo. */
585 static int
586 elf_initialize_syminfo (struct backtrace_state *state,
587 uintptr_t base_address,
588 const unsigned char *symtab_data, size_t symtab_size,
589 const unsigned char *strtab, size_t strtab_size,
590 backtrace_error_callback error_callback,
591 void *data, struct elf_syminfo_data *sdata,
592 struct elf_ppc64_opd_data *opd)
594 size_t sym_count;
595 const b_elf_sym *sym;
596 size_t elf_symbol_count;
597 size_t elf_symbol_size;
598 struct elf_symbol *elf_symbols;
599 size_t i;
600 unsigned int j;
602 sym_count = symtab_size / sizeof (b_elf_sym);
604 /* We only care about function symbols. Count them. */
605 sym = (const b_elf_sym *) symtab_data;
606 elf_symbol_count = 0;
607 for (i = 0; i < sym_count; ++i, ++sym)
609 int info;
611 info = sym->st_info & 0xf;
612 if ((info == STT_FUNC || info == STT_OBJECT)
613 && sym->st_shndx != SHN_UNDEF)
614 ++elf_symbol_count;
617 elf_symbol_size = elf_symbol_count * sizeof (struct elf_symbol);
618 elf_symbols = ((struct elf_symbol *)
619 backtrace_alloc (state, elf_symbol_size, error_callback,
620 data));
621 if (elf_symbols == NULL)
622 return 0;
624 sym = (const b_elf_sym *) symtab_data;
625 j = 0;
626 for (i = 0; i < sym_count; ++i, ++sym)
628 int info;
630 info = sym->st_info & 0xf;
631 if (info != STT_FUNC && info != STT_OBJECT)
632 continue;
633 if (sym->st_shndx == SHN_UNDEF)
634 continue;
635 if (sym->st_name >= strtab_size)
637 error_callback (data, "symbol string index out of range", 0);
638 backtrace_free (state, elf_symbols, elf_symbol_size, error_callback,
639 data);
640 return 0;
642 elf_symbols[j].name = (const char *) strtab + sym->st_name;
643 /* Special case PowerPC64 ELFv1 symbols in .opd section, if the symbol
644 is a function descriptor, read the actual code address from the
645 descriptor. */
646 if (opd
647 && sym->st_value >= opd->addr
648 && sym->st_value < opd->addr + opd->size)
649 elf_symbols[j].address
650 = *(const b_elf_addr *) (opd->data + (sym->st_value - opd->addr));
651 else
652 elf_symbols[j].address = sym->st_value;
653 elf_symbols[j].address += base_address;
654 elf_symbols[j].size = sym->st_size;
655 ++j;
658 backtrace_qsort (elf_symbols, elf_symbol_count, sizeof (struct elf_symbol),
659 elf_symbol_compare);
661 sdata->next = NULL;
662 sdata->symbols = elf_symbols;
663 sdata->count = elf_symbol_count;
665 return 1;
668 /* Add EDATA to the list in STATE. */
670 static void
671 elf_add_syminfo_data (struct backtrace_state *state,
672 struct elf_syminfo_data *edata)
674 if (!state->threaded)
676 struct elf_syminfo_data **pp;
678 for (pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
679 *pp != NULL;
680 pp = &(*pp)->next)
682 *pp = edata;
684 else
686 while (1)
688 struct elf_syminfo_data **pp;
690 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
692 while (1)
694 struct elf_syminfo_data *p;
696 p = backtrace_atomic_load_pointer (pp);
698 if (p == NULL)
699 break;
701 pp = &p->next;
704 if (__sync_bool_compare_and_swap (pp, NULL, edata))
705 break;
710 /* Return the symbol name and value for an ADDR. */
712 static void
713 elf_syminfo (struct backtrace_state *state, uintptr_t addr,
714 backtrace_syminfo_callback callback,
715 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
716 void *data)
718 struct elf_syminfo_data *edata;
719 struct elf_symbol *sym = NULL;
721 if (!state->threaded)
723 for (edata = (struct elf_syminfo_data *) state->syminfo_data;
724 edata != NULL;
725 edata = edata->next)
727 sym = ((struct elf_symbol *)
728 bsearch (&addr, edata->symbols, edata->count,
729 sizeof (struct elf_symbol), elf_symbol_search));
730 if (sym != NULL)
731 break;
734 else
736 struct elf_syminfo_data **pp;
738 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
739 while (1)
741 edata = backtrace_atomic_load_pointer (pp);
742 if (edata == NULL)
743 break;
745 sym = ((struct elf_symbol *)
746 bsearch (&addr, edata->symbols, edata->count,
747 sizeof (struct elf_symbol), elf_symbol_search));
748 if (sym != NULL)
749 break;
751 pp = &edata->next;
755 if (sym == NULL)
756 callback (data, addr, NULL, 0, 0);
757 else
758 callback (data, addr, sym->name, sym->address, sym->size);
761 /* Return whether FILENAME is a symlink. */
763 static int
764 elf_is_symlink (const char *filename)
766 struct stat st;
768 if (lstat (filename, &st) < 0)
769 return 0;
770 return S_ISLNK (st.st_mode);
773 /* Return the results of reading the symlink FILENAME in a buffer
774 allocated by backtrace_alloc. Return the length of the buffer in
775 *LEN. */
777 static char *
778 elf_readlink (struct backtrace_state *state, const char *filename,
779 backtrace_error_callback error_callback, void *data,
780 size_t *plen)
782 size_t len;
783 char *buf;
785 len = 128;
786 while (1)
788 ssize_t rl;
790 buf = backtrace_alloc (state, len, error_callback, data);
791 if (buf == NULL)
792 return NULL;
793 rl = readlink (filename, buf, len);
794 if (rl < 0)
796 backtrace_free (state, buf, len, error_callback, data);
797 return NULL;
799 if ((size_t) rl < len - 1)
801 buf[rl] = '\0';
802 *plen = len;
803 return buf;
805 backtrace_free (state, buf, len, error_callback, data);
806 len *= 2;
810 /* Open a separate debug info file, using the build ID to find it.
811 Returns an open file descriptor, or -1.
813 The GDB manual says that the only place gdb looks for a debug file
814 when the build ID is known is in /usr/lib/debug/.build-id. */
816 static int
817 elf_open_debugfile_by_buildid (struct backtrace_state *state,
818 const char *buildid_data, size_t buildid_size,
819 backtrace_error_callback error_callback,
820 void *data)
822 const char * const prefix = "/usr/lib/debug/.build-id/";
823 const size_t prefix_len = strlen (prefix);
824 const char * const suffix = ".debug";
825 const size_t suffix_len = strlen (suffix);
826 size_t len;
827 char *bd_filename;
828 char *t;
829 size_t i;
830 int ret;
831 int does_not_exist;
833 len = prefix_len + buildid_size * 2 + suffix_len + 2;
834 bd_filename = backtrace_alloc (state, len, error_callback, data);
835 if (bd_filename == NULL)
836 return -1;
838 t = bd_filename;
839 memcpy (t, prefix, prefix_len);
840 t += prefix_len;
841 for (i = 0; i < buildid_size; i++)
843 unsigned char b;
844 unsigned char nib;
846 b = (unsigned char) buildid_data[i];
847 nib = (b & 0xf0) >> 4;
848 *t++ = nib < 10 ? '0' + nib : 'a' + nib - 10;
849 nib = b & 0x0f;
850 *t++ = nib < 10 ? '0' + nib : 'a' + nib - 10;
851 if (i == 0)
852 *t++ = '/';
854 memcpy (t, suffix, suffix_len);
855 t[suffix_len] = '\0';
857 ret = backtrace_open (bd_filename, error_callback, data, &does_not_exist);
859 backtrace_free (state, bd_filename, len, error_callback, data);
861 /* gdb checks that the debuginfo file has the same build ID note.
862 That seems kind of pointless to me--why would it have the right
863 name but not the right build ID?--so skipping the check. */
865 return ret;
868 /* Try to open a file whose name is PREFIX (length PREFIX_LEN)
869 concatenated with PREFIX2 (length PREFIX2_LEN) concatenated with
870 DEBUGLINK_NAME. Returns an open file descriptor, or -1. */
872 static int
873 elf_try_debugfile (struct backtrace_state *state, const char *prefix,
874 size_t prefix_len, const char *prefix2, size_t prefix2_len,
875 const char *debuglink_name,
876 backtrace_error_callback error_callback, void *data)
878 size_t debuglink_len;
879 size_t try_len;
880 char *try;
881 int does_not_exist;
882 int ret;
884 debuglink_len = strlen (debuglink_name);
885 try_len = prefix_len + prefix2_len + debuglink_len + 1;
886 try = backtrace_alloc (state, try_len, error_callback, data);
887 if (try == NULL)
888 return -1;
890 memcpy (try, prefix, prefix_len);
891 memcpy (try + prefix_len, prefix2, prefix2_len);
892 memcpy (try + prefix_len + prefix2_len, debuglink_name, debuglink_len);
893 try[prefix_len + prefix2_len + debuglink_len] = '\0';
895 ret = backtrace_open (try, error_callback, data, &does_not_exist);
897 backtrace_free (state, try, try_len, error_callback, data);
899 return ret;
902 /* Find a separate debug info file, using the debuglink section data
903 to find it. Returns an open file descriptor, or -1. */
905 static int
906 elf_find_debugfile_by_debuglink (struct backtrace_state *state,
907 const char *filename,
908 const char *debuglink_name,
909 backtrace_error_callback error_callback,
910 void *data)
912 int ret;
913 char *alc;
914 size_t alc_len;
915 const char *slash;
916 int ddescriptor;
917 const char *prefix;
918 size_t prefix_len;
920 /* Resolve symlinks in FILENAME. Since FILENAME is fairly likely to
921 be /proc/self/exe, symlinks are common. We don't try to resolve
922 the whole path name, just the base name. */
923 ret = -1;
924 alc = NULL;
925 alc_len = 0;
926 while (elf_is_symlink (filename))
928 char *new_buf;
929 size_t new_len;
931 new_buf = elf_readlink (state, filename, error_callback, data, &new_len);
932 if (new_buf == NULL)
933 break;
935 if (new_buf[0] == '/')
936 filename = new_buf;
937 else
939 slash = strrchr (filename, '/');
940 if (slash == NULL)
941 filename = new_buf;
942 else
944 size_t clen;
945 char *c;
947 slash++;
948 clen = slash - filename + strlen (new_buf) + 1;
949 c = backtrace_alloc (state, clen, error_callback, data);
950 if (c == NULL)
951 goto done;
953 memcpy (c, filename, slash - filename);
954 memcpy (c + (slash - filename), new_buf, strlen (new_buf));
955 c[slash - filename + strlen (new_buf)] = '\0';
956 backtrace_free (state, new_buf, new_len, error_callback, data);
957 filename = c;
958 new_buf = c;
959 new_len = clen;
963 if (alc != NULL)
964 backtrace_free (state, alc, alc_len, error_callback, data);
965 alc = new_buf;
966 alc_len = new_len;
969 /* Look for DEBUGLINK_NAME in the same directory as FILENAME. */
971 slash = strrchr (filename, '/');
972 if (slash == NULL)
974 prefix = "";
975 prefix_len = 0;
977 else
979 slash++;
980 prefix = filename;
981 prefix_len = slash - filename;
984 ddescriptor = elf_try_debugfile (state, prefix, prefix_len, "", 0,
985 debuglink_name, error_callback, data);
986 if (ddescriptor >= 0)
988 ret = ddescriptor;
989 goto done;
992 /* Look for DEBUGLINK_NAME in a .debug subdirectory of FILENAME. */
994 ddescriptor = elf_try_debugfile (state, prefix, prefix_len, ".debug/",
995 strlen (".debug/"), debuglink_name,
996 error_callback, data);
997 if (ddescriptor >= 0)
999 ret = ddescriptor;
1000 goto done;
1003 /* Look for DEBUGLINK_NAME in /usr/lib/debug. */
1005 ddescriptor = elf_try_debugfile (state, "/usr/lib/debug/",
1006 strlen ("/usr/lib/debug/"), prefix,
1007 prefix_len, debuglink_name,
1008 error_callback, data);
1009 if (ddescriptor >= 0)
1010 ret = ddescriptor;
1012 done:
1013 if (alc != NULL && alc_len > 0)
1014 backtrace_free (state, alc, alc_len, error_callback, data);
1015 return ret;
1018 /* Open a separate debug info file, using the debuglink section data
1019 to find it. Returns an open file descriptor, or -1. */
1021 static int
1022 elf_open_debugfile_by_debuglink (struct backtrace_state *state,
1023 const char *filename,
1024 const char *debuglink_name,
1025 uint32_t debuglink_crc,
1026 backtrace_error_callback error_callback,
1027 void *data)
1029 int ddescriptor;
1031 ddescriptor = elf_find_debugfile_by_debuglink (state, filename,
1032 debuglink_name,
1033 error_callback, data);
1034 if (ddescriptor < 0)
1035 return -1;
1037 if (debuglink_crc != 0)
1039 uint32_t got_crc;
1041 got_crc = elf_crc32_file (state, ddescriptor, error_callback, data);
1042 if (got_crc != debuglink_crc)
1044 backtrace_close (ddescriptor, error_callback, data);
1045 return -1;
1049 return ddescriptor;
1052 /* A function useful for setting a breakpoint for an inflation failure
1053 when this code is compiled with -g. */
1055 static void
1056 elf_zlib_failed(void)
1060 /* *PVAL is the current value being read from the stream, and *PBITS
1061 is the number of valid bits. Ensure that *PVAL holds at least 15
1062 bits by reading additional bits from *PPIN, up to PINEND, as
1063 needed. Updates *PPIN, *PVAL and *PBITS. Returns 1 on success, 0
1064 on error. */
1066 static int
1067 elf_zlib_fetch (const unsigned char **ppin, const unsigned char *pinend,
1068 uint64_t *pval, unsigned int *pbits)
1070 unsigned int bits;
1071 const unsigned char *pin;
1072 uint64_t val;
1073 uint32_t next;
1075 bits = *pbits;
1076 if (bits >= 15)
1077 return 1;
1078 pin = *ppin;
1079 val = *pval;
1081 if (unlikely (pinend - pin < 4))
1083 elf_zlib_failed ();
1084 return 0;
1087 /* We've ensured that PIN is aligned. */
1088 next = *(const uint32_t *)pin;
1090 #if __BYTE_ORDER == __ORDER_BIG_ENDIAN
1091 next = __builtin_bswap32 (next);
1092 #endif
1094 val |= (uint64_t)next << bits;
1095 bits += 32;
1096 pin += 4;
1098 /* We will need the next four bytes soon. */
1099 __builtin_prefetch (pin, 0, 0);
1101 *ppin = pin;
1102 *pval = val;
1103 *pbits = bits;
1104 return 1;
1107 /* Huffman code tables, like the rest of the zlib format, are defined
1108 by RFC 1951. We store a Huffman code table as a series of tables
1109 stored sequentially in memory. Each entry in a table is 16 bits.
1110 The first, main, table has 256 entries. It is followed by a set of
1111 secondary tables of length 2 to 128 entries. The maximum length of
1112 a code sequence in the deflate format is 15 bits, so that is all we
1113 need. Each secondary table has an index, which is the offset of
1114 the table in the overall memory storage.
1116 The deflate format says that all codes of a given bit length are
1117 lexicographically consecutive. Perhaps we could have 130 values
1118 that require a 15-bit code, perhaps requiring three secondary
1119 tables of size 128. I don't know if this is actually possible, but
1120 it suggests that the maximum size required for secondary tables is
1121 3 * 128 + 3 * 64 ... == 768. The zlib enough program reports 660
1122 as the maximum. We permit 768, since in addition to the 256 for
1123 the primary table, with two bytes per entry, and with the two
1124 tables we need, that gives us a page.
1126 A single table entry needs to store a value or (for the main table
1127 only) the index and size of a secondary table. Values range from 0
1128 to 285, inclusive. Secondary table indexes, per above, range from
1129 0 to 510. For a value we need to store the number of bits we need
1130 to determine that value (one value may appear multiple times in the
1131 table), which is 1 to 8. For a secondary table we need to store
1132 the number of bits used to index into the table, which is 1 to 7.
1133 And of course we need 1 bit to decide whether we have a value or a
1134 secondary table index. So each entry needs 9 bits for value/table
1135 index, 3 bits for size, 1 bit what it is. For simplicity we use 16
1136 bits per entry. */
1138 /* Number of entries we allocate to for one code table. We get a page
1139 for the two code tables we need. */
1141 #define HUFFMAN_TABLE_SIZE (1024)
1143 /* Bit masks and shifts for the values in the table. */
1145 #define HUFFMAN_VALUE_MASK 0x01ff
1146 #define HUFFMAN_BITS_SHIFT 9
1147 #define HUFFMAN_BITS_MASK 0x7
1148 #define HUFFMAN_SECONDARY_SHIFT 12
1150 /* For working memory while inflating we need two code tables, we need
1151 an array of code lengths (max value 15, so we use unsigned char),
1152 and an array of unsigned shorts used while building a table. The
1153 latter two arrays must be large enough to hold the maximum number
1154 of code lengths, which RFC 1951 defines as 286 + 30. */
1156 #define ZDEBUG_TABLE_SIZE \
1157 (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t) \
1158 + (286 + 30) * sizeof (uint16_t) \
1159 + (286 + 30) * sizeof (unsigned char))
1161 #define ZDEBUG_TABLE_CODELEN_OFFSET \
1162 (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t) \
1163 + (286 + 30) * sizeof (uint16_t))
1165 #define ZDEBUG_TABLE_WORK_OFFSET \
1166 (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t))
1168 #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
1170 /* Used by the main function that generates the fixed table to learn
1171 the table size. */
1172 static size_t final_next_secondary;
1174 #endif
1176 /* Build a Huffman code table from an array of lengths in CODES of
1177 length CODES_LEN. The table is stored into *TABLE. ZDEBUG_TABLE
1178 is the same as for elf_zlib_inflate, used to find some work space.
1179 Returns 1 on success, 0 on error. */
1181 static int
1182 elf_zlib_inflate_table (unsigned char *codes, size_t codes_len,
1183 uint16_t *zdebug_table, uint16_t *table)
1185 uint16_t count[16];
1186 uint16_t start[16];
1187 uint16_t prev[16];
1188 uint16_t firstcode[7];
1189 uint16_t *next;
1190 size_t i;
1191 size_t j;
1192 unsigned int code;
1193 size_t next_secondary;
1195 /* Count the number of code of each length. Set NEXT[val] to be the
1196 next value after VAL with the same bit length. */
1198 next = (uint16_t *) (((unsigned char *) zdebug_table)
1199 + ZDEBUG_TABLE_WORK_OFFSET);
1201 memset (&count[0], 0, 16 * sizeof (uint16_t));
1202 for (i = 0; i < codes_len; ++i)
1204 if (unlikely (codes[i] >= 16))
1206 elf_zlib_failed ();
1207 return 0;
1210 if (count[codes[i]] == 0)
1212 start[codes[i]] = i;
1213 prev[codes[i]] = i;
1215 else
1217 next[prev[codes[i]]] = i;
1218 prev[codes[i]] = i;
1221 ++count[codes[i]];
1224 /* For each length, fill in the table for the codes of that
1225 length. */
1227 memset (table, 0, HUFFMAN_TABLE_SIZE * sizeof (uint16_t));
1229 /* Handle the values that do not require a secondary table. */
1231 code = 0;
1232 for (j = 1; j <= 8; ++j)
1234 unsigned int jcnt;
1235 unsigned int val;
1237 jcnt = count[j];
1238 if (jcnt == 0)
1239 continue;
1241 if (unlikely (jcnt > (1U << j)))
1243 elf_zlib_failed ();
1244 return 0;
1247 /* There are JCNT values that have this length, the values
1248 starting from START[j] continuing through NEXT[VAL]. Those
1249 values are assigned consecutive values starting at CODE. */
1251 val = start[j];
1252 for (i = 0; i < jcnt; ++i)
1254 uint16_t tval;
1255 size_t ind;
1256 unsigned int incr;
1258 /* In the compressed bit stream, the value VAL is encoded as
1259 J bits with the value C. */
1261 if (unlikely ((val & ~HUFFMAN_VALUE_MASK) != 0))
1263 elf_zlib_failed ();
1264 return 0;
1267 tval = val | ((j - 1) << HUFFMAN_BITS_SHIFT);
1269 /* The table lookup uses 8 bits. If J is less than 8, we
1270 don't know what the other bits will be. We need to fill
1271 in all possibilities in the table. Since the Huffman
1272 code is unambiguous, those entries can't be used for any
1273 other code. */
1275 for (ind = code; ind < 0x100; ind += 1 << j)
1277 if (unlikely (table[ind] != 0))
1279 elf_zlib_failed ();
1280 return 0;
1282 table[ind] = tval;
1285 /* Advance to the next value with this length. */
1286 if (i + 1 < jcnt)
1287 val = next[val];
1289 /* The Huffman codes are stored in the bitstream with the
1290 most significant bit first, as is required to make them
1291 unambiguous. The effect is that when we read them from
1292 the bitstream we see the bit sequence in reverse order:
1293 the most significant bit of the Huffman code is the least
1294 significant bit of the value we read from the bitstream.
1295 That means that to make our table lookups work, we need
1296 to reverse the bits of CODE. Since reversing bits is
1297 tedious and in general requires using a table, we instead
1298 increment CODE in reverse order. That is, if the number
1299 of bits we are currently using, here named J, is 3, we
1300 count as 000, 100, 010, 110, 001, 101, 011, 111, which is
1301 to say the numbers from 0 to 7 but with the bits
1302 reversed. Going to more bits, aka incrementing J,
1303 effectively just adds more zero bits as the beginning,
1304 and as such does not change the numeric value of CODE.
1306 To increment CODE of length J in reverse order, find the
1307 most significant zero bit and set it to one while
1308 clearing all higher bits. In other words, add 1 modulo
1309 2^J, only reversed. */
1311 incr = 1U << (j - 1);
1312 while ((code & incr) != 0)
1313 incr >>= 1;
1314 if (incr == 0)
1315 code = 0;
1316 else
1318 code &= incr - 1;
1319 code += incr;
1324 /* Handle the values that require a secondary table. */
1326 /* Set FIRSTCODE, the number at which the codes start, for each
1327 length. */
1329 for (j = 9; j < 16; j++)
1331 unsigned int jcnt;
1332 unsigned int k;
1334 jcnt = count[j];
1335 if (jcnt == 0)
1336 continue;
1338 /* There are JCNT values that have this length, the values
1339 starting from START[j]. Those values are assigned
1340 consecutive values starting at CODE. */
1342 firstcode[j - 9] = code;
1344 /* Reverse add JCNT to CODE modulo 2^J. */
1345 for (k = 0; k < j; ++k)
1347 if ((jcnt & (1U << k)) != 0)
1349 unsigned int m;
1350 unsigned int bit;
1352 bit = 1U << (j - k - 1);
1353 for (m = 0; m < j - k; ++m, bit >>= 1)
1355 if ((code & bit) == 0)
1357 code += bit;
1358 break;
1360 code &= ~bit;
1362 jcnt &= ~(1U << k);
1365 if (unlikely (jcnt != 0))
1367 elf_zlib_failed ();
1368 return 0;
1372 /* For J from 9 to 15, inclusive, we store COUNT[J] consecutive
1373 values starting at START[J] with consecutive codes starting at
1374 FIRSTCODE[J - 9]. In the primary table we need to point to the
1375 secondary table, and the secondary table will be indexed by J - 9
1376 bits. We count down from 15 so that we install the larger
1377 secondary tables first, as the smaller ones may be embedded in
1378 the larger ones. */
1380 next_secondary = 0; /* Index of next secondary table (after primary). */
1381 for (j = 15; j >= 9; j--)
1383 unsigned int jcnt;
1384 unsigned int val;
1385 size_t primary; /* Current primary index. */
1386 size_t secondary; /* Offset to current secondary table. */
1387 size_t secondary_bits; /* Bit size of current secondary table. */
1389 jcnt = count[j];
1390 if (jcnt == 0)
1391 continue;
1393 val = start[j];
1394 code = firstcode[j - 9];
1395 primary = 0x100;
1396 secondary = 0;
1397 secondary_bits = 0;
1398 for (i = 0; i < jcnt; ++i)
1400 uint16_t tval;
1401 size_t ind;
1402 unsigned int incr;
1404 if ((code & 0xff) != primary)
1406 uint16_t tprimary;
1408 /* Fill in a new primary table entry. */
1410 primary = code & 0xff;
1412 tprimary = table[primary];
1413 if (tprimary == 0)
1415 /* Start a new secondary table. */
1417 if (unlikely ((next_secondary & HUFFMAN_VALUE_MASK)
1418 != next_secondary))
1420 elf_zlib_failed ();
1421 return 0;
1424 secondary = next_secondary;
1425 secondary_bits = j - 8;
1426 next_secondary += 1 << secondary_bits;
1427 table[primary] = (secondary
1428 + ((j - 8) << HUFFMAN_BITS_SHIFT)
1429 + (1U << HUFFMAN_SECONDARY_SHIFT));
1431 else
1433 /* There is an existing entry. It had better be a
1434 secondary table with enough bits. */
1435 if (unlikely ((tprimary & (1U << HUFFMAN_SECONDARY_SHIFT))
1436 == 0))
1438 elf_zlib_failed ();
1439 return 0;
1441 secondary = tprimary & HUFFMAN_VALUE_MASK;
1442 secondary_bits = ((tprimary >> HUFFMAN_BITS_SHIFT)
1443 & HUFFMAN_BITS_MASK);
1444 if (unlikely (secondary_bits < j - 8))
1446 elf_zlib_failed ();
1447 return 0;
1452 /* Fill in secondary table entries. */
1454 tval = val | ((j - 8) << HUFFMAN_BITS_SHIFT);
1456 for (ind = code >> 8;
1457 ind < (1U << secondary_bits);
1458 ind += 1U << (j - 8))
1460 if (unlikely (table[secondary + 0x100 + ind] != 0))
1462 elf_zlib_failed ();
1463 return 0;
1465 table[secondary + 0x100 + ind] = tval;
1468 if (i + 1 < jcnt)
1469 val = next[val];
1471 incr = 1U << (j - 1);
1472 while ((code & incr) != 0)
1473 incr >>= 1;
1474 if (incr == 0)
1475 code = 0;
1476 else
1478 code &= incr - 1;
1479 code += incr;
1484 #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
1485 final_next_secondary = next_secondary;
1486 #endif
1488 return 1;
1491 #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
1493 /* Used to generate the fixed Huffman table for block type 1. */
1495 #include <stdio.h>
1497 static uint16_t table[ZDEBUG_TABLE_SIZE];
1498 static unsigned char codes[288];
1501 main ()
1503 size_t i;
1505 for (i = 0; i <= 143; ++i)
1506 codes[i] = 8;
1507 for (i = 144; i <= 255; ++i)
1508 codes[i] = 9;
1509 for (i = 256; i <= 279; ++i)
1510 codes[i] = 7;
1511 for (i = 280; i <= 287; ++i)
1512 codes[i] = 8;
1513 if (!elf_zlib_inflate_table (&codes[0], 288, &table[0], &table[0]))
1515 fprintf (stderr, "elf_zlib_inflate_table failed\n");
1516 exit (EXIT_FAILURE);
1519 printf ("static const uint16_t elf_zlib_default_table[%#zx] =\n",
1520 final_next_secondary + 0x100);
1521 printf ("{\n");
1522 for (i = 0; i < final_next_secondary + 0x100; i += 8)
1524 size_t j;
1526 printf (" ");
1527 for (j = i; j < final_next_secondary + 0x100 && j < i + 8; ++j)
1528 printf (" %#x,", table[j]);
1529 printf ("\n");
1531 printf ("};\n");
1532 printf ("\n");
1534 for (i = 0; i < 32; ++i)
1535 codes[i] = 5;
1536 if (!elf_zlib_inflate_table (&codes[0], 32, &table[0], &table[0]))
1538 fprintf (stderr, "elf_zlib_inflate_table failed\n");
1539 exit (EXIT_FAILURE);
1542 printf ("static const uint16_t elf_zlib_default_dist_table[%#zx] =\n",
1543 final_next_secondary + 0x100);
1544 printf ("{\n");
1545 for (i = 0; i < final_next_secondary + 0x100; i += 8)
1547 size_t j;
1549 printf (" ");
1550 for (j = i; j < final_next_secondary + 0x100 && j < i + 8; ++j)
1551 printf (" %#x,", table[j]);
1552 printf ("\n");
1554 printf ("};\n");
1556 return 0;
1559 #endif
1561 /* The fixed tables generated by the #ifdef'ed out main function
1562 above. */
1564 static const uint16_t elf_zlib_default_table[0x170] =
1566 0xd00, 0xe50, 0xe10, 0xf18, 0xd10, 0xe70, 0xe30, 0x1230,
1567 0xd08, 0xe60, 0xe20, 0x1210, 0xe00, 0xe80, 0xe40, 0x1250,
1568 0xd04, 0xe58, 0xe18, 0x1200, 0xd14, 0xe78, 0xe38, 0x1240,
1569 0xd0c, 0xe68, 0xe28, 0x1220, 0xe08, 0xe88, 0xe48, 0x1260,
1570 0xd02, 0xe54, 0xe14, 0xf1c, 0xd12, 0xe74, 0xe34, 0x1238,
1571 0xd0a, 0xe64, 0xe24, 0x1218, 0xe04, 0xe84, 0xe44, 0x1258,
1572 0xd06, 0xe5c, 0xe1c, 0x1208, 0xd16, 0xe7c, 0xe3c, 0x1248,
1573 0xd0e, 0xe6c, 0xe2c, 0x1228, 0xe0c, 0xe8c, 0xe4c, 0x1268,
1574 0xd01, 0xe52, 0xe12, 0xf1a, 0xd11, 0xe72, 0xe32, 0x1234,
1575 0xd09, 0xe62, 0xe22, 0x1214, 0xe02, 0xe82, 0xe42, 0x1254,
1576 0xd05, 0xe5a, 0xe1a, 0x1204, 0xd15, 0xe7a, 0xe3a, 0x1244,
1577 0xd0d, 0xe6a, 0xe2a, 0x1224, 0xe0a, 0xe8a, 0xe4a, 0x1264,
1578 0xd03, 0xe56, 0xe16, 0xf1e, 0xd13, 0xe76, 0xe36, 0x123c,
1579 0xd0b, 0xe66, 0xe26, 0x121c, 0xe06, 0xe86, 0xe46, 0x125c,
1580 0xd07, 0xe5e, 0xe1e, 0x120c, 0xd17, 0xe7e, 0xe3e, 0x124c,
1581 0xd0f, 0xe6e, 0xe2e, 0x122c, 0xe0e, 0xe8e, 0xe4e, 0x126c,
1582 0xd00, 0xe51, 0xe11, 0xf19, 0xd10, 0xe71, 0xe31, 0x1232,
1583 0xd08, 0xe61, 0xe21, 0x1212, 0xe01, 0xe81, 0xe41, 0x1252,
1584 0xd04, 0xe59, 0xe19, 0x1202, 0xd14, 0xe79, 0xe39, 0x1242,
1585 0xd0c, 0xe69, 0xe29, 0x1222, 0xe09, 0xe89, 0xe49, 0x1262,
1586 0xd02, 0xe55, 0xe15, 0xf1d, 0xd12, 0xe75, 0xe35, 0x123a,
1587 0xd0a, 0xe65, 0xe25, 0x121a, 0xe05, 0xe85, 0xe45, 0x125a,
1588 0xd06, 0xe5d, 0xe1d, 0x120a, 0xd16, 0xe7d, 0xe3d, 0x124a,
1589 0xd0e, 0xe6d, 0xe2d, 0x122a, 0xe0d, 0xe8d, 0xe4d, 0x126a,
1590 0xd01, 0xe53, 0xe13, 0xf1b, 0xd11, 0xe73, 0xe33, 0x1236,
1591 0xd09, 0xe63, 0xe23, 0x1216, 0xe03, 0xe83, 0xe43, 0x1256,
1592 0xd05, 0xe5b, 0xe1b, 0x1206, 0xd15, 0xe7b, 0xe3b, 0x1246,
1593 0xd0d, 0xe6b, 0xe2b, 0x1226, 0xe0b, 0xe8b, 0xe4b, 0x1266,
1594 0xd03, 0xe57, 0xe17, 0xf1f, 0xd13, 0xe77, 0xe37, 0x123e,
1595 0xd0b, 0xe67, 0xe27, 0x121e, 0xe07, 0xe87, 0xe47, 0x125e,
1596 0xd07, 0xe5f, 0xe1f, 0x120e, 0xd17, 0xe7f, 0xe3f, 0x124e,
1597 0xd0f, 0xe6f, 0xe2f, 0x122e, 0xe0f, 0xe8f, 0xe4f, 0x126e,
1598 0x290, 0x291, 0x292, 0x293, 0x294, 0x295, 0x296, 0x297,
1599 0x298, 0x299, 0x29a, 0x29b, 0x29c, 0x29d, 0x29e, 0x29f,
1600 0x2a0, 0x2a1, 0x2a2, 0x2a3, 0x2a4, 0x2a5, 0x2a6, 0x2a7,
1601 0x2a8, 0x2a9, 0x2aa, 0x2ab, 0x2ac, 0x2ad, 0x2ae, 0x2af,
1602 0x2b0, 0x2b1, 0x2b2, 0x2b3, 0x2b4, 0x2b5, 0x2b6, 0x2b7,
1603 0x2b8, 0x2b9, 0x2ba, 0x2bb, 0x2bc, 0x2bd, 0x2be, 0x2bf,
1604 0x2c0, 0x2c1, 0x2c2, 0x2c3, 0x2c4, 0x2c5, 0x2c6, 0x2c7,
1605 0x2c8, 0x2c9, 0x2ca, 0x2cb, 0x2cc, 0x2cd, 0x2ce, 0x2cf,
1606 0x2d0, 0x2d1, 0x2d2, 0x2d3, 0x2d4, 0x2d5, 0x2d6, 0x2d7,
1607 0x2d8, 0x2d9, 0x2da, 0x2db, 0x2dc, 0x2dd, 0x2de, 0x2df,
1608 0x2e0, 0x2e1, 0x2e2, 0x2e3, 0x2e4, 0x2e5, 0x2e6, 0x2e7,
1609 0x2e8, 0x2e9, 0x2ea, 0x2eb, 0x2ec, 0x2ed, 0x2ee, 0x2ef,
1610 0x2f0, 0x2f1, 0x2f2, 0x2f3, 0x2f4, 0x2f5, 0x2f6, 0x2f7,
1611 0x2f8, 0x2f9, 0x2fa, 0x2fb, 0x2fc, 0x2fd, 0x2fe, 0x2ff,
1614 static const uint16_t elf_zlib_default_dist_table[0x100] =
1616 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1617 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1618 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1619 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1620 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1621 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1622 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1623 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1624 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1625 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1626 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1627 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1628 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1629 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1630 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1631 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1632 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1633 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1634 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1635 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1636 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1637 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1638 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1639 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1640 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1641 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1642 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1643 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1644 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1645 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1646 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1647 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1650 /* Inflate a zlib stream from PIN/SIN to POUT/SOUT. Return 1 on
1651 success, 0 on some error parsing the stream. */
1653 static int
1654 elf_zlib_inflate (const unsigned char *pin, size_t sin, uint16_t *zdebug_table,
1655 unsigned char *pout, size_t sout)
1657 unsigned char *porigout;
1658 const unsigned char *pinend;
1659 unsigned char *poutend;
1661 /* We can apparently see multiple zlib streams concatenated
1662 together, so keep going as long as there is something to read.
1663 The last 4 bytes are the checksum. */
1664 porigout = pout;
1665 pinend = pin + sin;
1666 poutend = pout + sout;
1667 while ((pinend - pin) > 4)
1669 uint64_t val;
1670 unsigned int bits;
1671 int last;
1673 /* Read the two byte zlib header. */
1675 if (unlikely ((pin[0] & 0xf) != 8)) /* 8 is zlib encoding. */
1677 /* Unknown compression method. */
1678 elf_zlib_failed ();
1679 return 0;
1681 if (unlikely ((pin[0] >> 4) > 7))
1683 /* Window size too large. Other than this check, we don't
1684 care about the window size. */
1685 elf_zlib_failed ();
1686 return 0;
1688 if (unlikely ((pin[1] & 0x20) != 0))
1690 /* Stream expects a predefined dictionary, but we have no
1691 dictionary. */
1692 elf_zlib_failed ();
1693 return 0;
1695 val = (pin[0] << 8) | pin[1];
1696 if (unlikely (val % 31 != 0))
1698 /* Header check failure. */
1699 elf_zlib_failed ();
1700 return 0;
1702 pin += 2;
1704 /* Align PIN to a 32-bit boundary. */
1706 val = 0;
1707 bits = 0;
1708 while ((((uintptr_t) pin) & 3) != 0)
1710 val |= (uint64_t)*pin << bits;
1711 bits += 8;
1712 ++pin;
1715 /* Read blocks until one is marked last. */
1717 last = 0;
1719 while (!last)
1721 unsigned int type;
1722 const uint16_t *tlit;
1723 const uint16_t *tdist;
1725 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1726 return 0;
1728 last = val & 1;
1729 type = (val >> 1) & 3;
1730 val >>= 3;
1731 bits -= 3;
1733 if (unlikely (type == 3))
1735 /* Invalid block type. */
1736 elf_zlib_failed ();
1737 return 0;
1740 if (type == 0)
1742 uint16_t len;
1743 uint16_t lenc;
1745 /* An uncompressed block. */
1747 /* If we've read ahead more than a byte, back up. */
1748 while (bits > 8)
1750 --pin;
1751 bits -= 8;
1754 val = 0;
1755 bits = 0;
1756 if (unlikely ((pinend - pin) < 4))
1758 /* Missing length. */
1759 elf_zlib_failed ();
1760 return 0;
1762 len = pin[0] | (pin[1] << 8);
1763 lenc = pin[2] | (pin[3] << 8);
1764 pin += 4;
1765 lenc = ~lenc;
1766 if (unlikely (len != lenc))
1768 /* Corrupt data. */
1769 elf_zlib_failed ();
1770 return 0;
1772 if (unlikely (len > (unsigned int) (pinend - pin)
1773 || len > (unsigned int) (poutend - pout)))
1775 /* Not enough space in buffers. */
1776 elf_zlib_failed ();
1777 return 0;
1779 memcpy (pout, pin, len);
1780 pout += len;
1781 pin += len;
1783 /* Align PIN. */
1784 while ((((uintptr_t) pin) & 3) != 0)
1786 val |= (uint64_t)*pin << bits;
1787 bits += 8;
1788 ++pin;
1791 /* Go around to read the next block. */
1792 continue;
1795 if (type == 1)
1797 tlit = elf_zlib_default_table;
1798 tdist = elf_zlib_default_dist_table;
1800 else
1802 unsigned int nlit;
1803 unsigned int ndist;
1804 unsigned int nclen;
1805 unsigned char codebits[19];
1806 unsigned char *plenbase;
1807 unsigned char *plen;
1808 unsigned char *plenend;
1810 /* Read a Huffman encoding table. The various magic
1811 numbers here are from RFC 1951. */
1813 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1814 return 0;
1816 nlit = (val & 0x1f) + 257;
1817 val >>= 5;
1818 ndist = (val & 0x1f) + 1;
1819 val >>= 5;
1820 nclen = (val & 0xf) + 4;
1821 val >>= 4;
1822 bits -= 14;
1823 if (unlikely (nlit > 286 || ndist > 30))
1825 /* Values out of range. */
1826 elf_zlib_failed ();
1827 return 0;
1830 /* Read and build the table used to compress the
1831 literal, length, and distance codes. */
1833 memset(&codebits[0], 0, 19);
1835 /* There are always at least 4 elements in the
1836 table. */
1838 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1839 return 0;
1841 codebits[16] = val & 7;
1842 codebits[17] = (val >> 3) & 7;
1843 codebits[18] = (val >> 6) & 7;
1844 codebits[0] = (val >> 9) & 7;
1845 val >>= 12;
1846 bits -= 12;
1848 if (nclen == 4)
1849 goto codebitsdone;
1851 codebits[8] = val & 7;
1852 val >>= 3;
1853 bits -= 3;
1855 if (nclen == 5)
1856 goto codebitsdone;
1858 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1859 return 0;
1861 codebits[7] = val & 7;
1862 val >>= 3;
1863 bits -= 3;
1865 if (nclen == 6)
1866 goto codebitsdone;
1868 codebits[9] = val & 7;
1869 val >>= 3;
1870 bits -= 3;
1872 if (nclen == 7)
1873 goto codebitsdone;
1875 codebits[6] = val & 7;
1876 val >>= 3;
1877 bits -= 3;
1879 if (nclen == 8)
1880 goto codebitsdone;
1882 codebits[10] = val & 7;
1883 val >>= 3;
1884 bits -= 3;
1886 if (nclen == 9)
1887 goto codebitsdone;
1889 codebits[5] = val & 7;
1890 val >>= 3;
1891 bits -= 3;
1893 if (nclen == 10)
1894 goto codebitsdone;
1896 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1897 return 0;
1899 codebits[11] = val & 7;
1900 val >>= 3;
1901 bits -= 3;
1903 if (nclen == 11)
1904 goto codebitsdone;
1906 codebits[4] = val & 7;
1907 val >>= 3;
1908 bits -= 3;
1910 if (nclen == 12)
1911 goto codebitsdone;
1913 codebits[12] = val & 7;
1914 val >>= 3;
1915 bits -= 3;
1917 if (nclen == 13)
1918 goto codebitsdone;
1920 codebits[3] = val & 7;
1921 val >>= 3;
1922 bits -= 3;
1924 if (nclen == 14)
1925 goto codebitsdone;
1927 codebits[13] = val & 7;
1928 val >>= 3;
1929 bits -= 3;
1931 if (nclen == 15)
1932 goto codebitsdone;
1934 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1935 return 0;
1937 codebits[2] = val & 7;
1938 val >>= 3;
1939 bits -= 3;
1941 if (nclen == 16)
1942 goto codebitsdone;
1944 codebits[14] = val & 7;
1945 val >>= 3;
1946 bits -= 3;
1948 if (nclen == 17)
1949 goto codebitsdone;
1951 codebits[1] = val & 7;
1952 val >>= 3;
1953 bits -= 3;
1955 if (nclen == 18)
1956 goto codebitsdone;
1958 codebits[15] = val & 7;
1959 val >>= 3;
1960 bits -= 3;
1962 codebitsdone:
1964 if (!elf_zlib_inflate_table (codebits, 19, zdebug_table,
1965 zdebug_table))
1966 return 0;
1968 /* Read the compressed bit lengths of the literal,
1969 length, and distance codes. We have allocated space
1970 at the end of zdebug_table to hold them. */
1972 plenbase = (((unsigned char *) zdebug_table)
1973 + ZDEBUG_TABLE_CODELEN_OFFSET);
1974 plen = plenbase;
1975 plenend = plen + nlit + ndist;
1976 while (plen < plenend)
1978 uint16_t t;
1979 unsigned int b;
1980 uint16_t v;
1982 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1983 return 0;
1985 t = zdebug_table[val & 0xff];
1987 /* The compression here uses bit lengths up to 7, so
1988 a secondary table is never necessary. */
1989 if (unlikely ((t & (1U << HUFFMAN_SECONDARY_SHIFT)) != 0))
1991 elf_zlib_failed ();
1992 return 0;
1995 b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
1996 val >>= b + 1;
1997 bits -= b + 1;
1999 v = t & HUFFMAN_VALUE_MASK;
2000 if (v < 16)
2001 *plen++ = v;
2002 else if (v == 16)
2004 unsigned int c;
2005 unsigned int prev;
2007 /* Copy previous entry 3 to 6 times. */
2009 if (unlikely (plen == plenbase))
2011 elf_zlib_failed ();
2012 return 0;
2015 /* We used up to 7 bits since the last
2016 elf_zlib_fetch, so we have at least 8 bits
2017 available here. */
2019 c = 3 + (val & 0x3);
2020 val >>= 2;
2021 bits -= 2;
2022 if (unlikely ((unsigned int) (plenend - plen) < c))
2024 elf_zlib_failed ();
2025 return 0;
2028 prev = plen[-1];
2029 switch (c)
2031 case 6:
2032 *plen++ = prev;
2033 /* fallthrough */
2034 case 5:
2035 *plen++ = prev;
2036 /* fallthrough */
2037 case 4:
2038 *plen++ = prev;
2040 *plen++ = prev;
2041 *plen++ = prev;
2042 *plen++ = prev;
2044 else if (v == 17)
2046 unsigned int c;
2048 /* Store zero 3 to 10 times. */
2050 /* We used up to 7 bits since the last
2051 elf_zlib_fetch, so we have at least 8 bits
2052 available here. */
2054 c = 3 + (val & 0x7);
2055 val >>= 3;
2056 bits -= 3;
2057 if (unlikely ((unsigned int) (plenend - plen) < c))
2059 elf_zlib_failed ();
2060 return 0;
2063 switch (c)
2065 case 10:
2066 *plen++ = 0;
2067 /* fallthrough */
2068 case 9:
2069 *plen++ = 0;
2070 /* fallthrough */
2071 case 8:
2072 *plen++ = 0;
2073 /* fallthrough */
2074 case 7:
2075 *plen++ = 0;
2076 /* fallthrough */
2077 case 6:
2078 *plen++ = 0;
2079 /* fallthrough */
2080 case 5:
2081 *plen++ = 0;
2082 /* fallthrough */
2083 case 4:
2084 *plen++ = 0;
2086 *plen++ = 0;
2087 *plen++ = 0;
2088 *plen++ = 0;
2090 else if (v == 18)
2092 unsigned int c;
2094 /* Store zero 11 to 138 times. */
2096 /* We used up to 7 bits since the last
2097 elf_zlib_fetch, so we have at least 8 bits
2098 available here. */
2100 c = 11 + (val & 0x7f);
2101 val >>= 7;
2102 bits -= 7;
2103 if (unlikely ((unsigned int) (plenend - plen) < c))
2105 elf_zlib_failed ();
2106 return 0;
2109 memset (plen, 0, c);
2110 plen += c;
2112 else
2114 elf_zlib_failed ();
2115 return 0;
2119 /* Make sure that the stop code can appear. */
2121 plen = plenbase;
2122 if (unlikely (plen[256] == 0))
2124 elf_zlib_failed ();
2125 return 0;
2128 /* Build the decompression tables. */
2130 if (!elf_zlib_inflate_table (plen, nlit, zdebug_table,
2131 zdebug_table))
2132 return 0;
2133 if (!elf_zlib_inflate_table (plen + nlit, ndist, zdebug_table,
2134 zdebug_table + HUFFMAN_TABLE_SIZE))
2135 return 0;
2136 tlit = zdebug_table;
2137 tdist = zdebug_table + HUFFMAN_TABLE_SIZE;
2140 /* Inflate values until the end of the block. This is the
2141 main loop of the inflation code. */
2143 while (1)
2145 uint16_t t;
2146 unsigned int b;
2147 uint16_t v;
2148 unsigned int lit;
2150 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
2151 return 0;
2153 t = tlit[val & 0xff];
2154 b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
2155 v = t & HUFFMAN_VALUE_MASK;
2157 if ((t & (1U << HUFFMAN_SECONDARY_SHIFT)) == 0)
2159 lit = v;
2160 val >>= b + 1;
2161 bits -= b + 1;
2163 else
2165 t = tlit[v + 0x100 + ((val >> 8) & ((1U << b) - 1))];
2166 b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
2167 lit = t & HUFFMAN_VALUE_MASK;
2168 val >>= b + 8;
2169 bits -= b + 8;
2172 if (lit < 256)
2174 if (unlikely (pout == poutend))
2176 elf_zlib_failed ();
2177 return 0;
2180 *pout++ = lit;
2182 /* We will need to write the next byte soon. We ask
2183 for high temporal locality because we will write
2184 to the whole cache line soon. */
2185 __builtin_prefetch (pout, 1, 3);
2187 else if (lit == 256)
2189 /* The end of the block. */
2190 break;
2192 else
2194 unsigned int dist;
2195 unsigned int len;
2197 /* Convert lit into a length. */
2199 if (lit < 265)
2200 len = lit - 257 + 3;
2201 else if (lit == 285)
2202 len = 258;
2203 else if (unlikely (lit > 285))
2205 elf_zlib_failed ();
2206 return 0;
2208 else
2210 unsigned int extra;
2212 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
2213 return 0;
2215 /* This is an expression for the table of length
2216 codes in RFC 1951 3.2.5. */
2217 lit -= 265;
2218 extra = (lit >> 2) + 1;
2219 len = (lit & 3) << extra;
2220 len += 11;
2221 len += ((1U << (extra - 1)) - 1) << 3;
2222 len += val & ((1U << extra) - 1);
2223 val >>= extra;
2224 bits -= extra;
2227 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
2228 return 0;
2230 t = tdist[val & 0xff];
2231 b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
2232 v = t & HUFFMAN_VALUE_MASK;
2234 if ((t & (1U << HUFFMAN_SECONDARY_SHIFT)) == 0)
2236 dist = v;
2237 val >>= b + 1;
2238 bits -= b + 1;
2240 else
2242 t = tdist[v + 0x100 + ((val >> 8) & ((1U << b) - 1))];
2243 b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
2244 dist = t & HUFFMAN_VALUE_MASK;
2245 val >>= b + 8;
2246 bits -= b + 8;
2249 /* Convert dist to a distance. */
2251 if (dist == 0)
2253 /* A distance of 1. A common case, meaning
2254 repeat the last character LEN times. */
2256 if (unlikely (pout == porigout))
2258 elf_zlib_failed ();
2259 return 0;
2262 if (unlikely ((unsigned int) (poutend - pout) < len))
2264 elf_zlib_failed ();
2265 return 0;
2268 memset (pout, pout[-1], len);
2269 pout += len;
2271 else if (unlikely (dist > 29))
2273 elf_zlib_failed ();
2274 return 0;
2276 else
2278 if (dist < 4)
2279 dist = dist + 1;
2280 else
2282 unsigned int extra;
2284 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
2285 return 0;
2287 /* This is an expression for the table of
2288 distance codes in RFC 1951 3.2.5. */
2289 dist -= 4;
2290 extra = (dist >> 1) + 1;
2291 dist = (dist & 1) << extra;
2292 dist += 5;
2293 dist += ((1U << (extra - 1)) - 1) << 2;
2294 dist += val & ((1U << extra) - 1);
2295 val >>= extra;
2296 bits -= extra;
2299 /* Go back dist bytes, and copy len bytes from
2300 there. */
2302 if (unlikely ((unsigned int) (pout - porigout) < dist))
2304 elf_zlib_failed ();
2305 return 0;
2308 if (unlikely ((unsigned int) (poutend - pout) < len))
2310 elf_zlib_failed ();
2311 return 0;
2314 if (dist >= len)
2316 memcpy (pout, pout - dist, len);
2317 pout += len;
2319 else
2321 while (len > 0)
2323 unsigned int copy;
2325 copy = len < dist ? len : dist;
2326 memcpy (pout, pout - dist, copy);
2327 len -= copy;
2328 pout += copy;
2337 /* We should have filled the output buffer. */
2338 if (unlikely (pout != poutend))
2340 elf_zlib_failed ();
2341 return 0;
2344 return 1;
2347 /* Verify the zlib checksum. The checksum is in the 4 bytes at
2348 CHECKBYTES, and the uncompressed data is at UNCOMPRESSED /
2349 UNCOMPRESSED_SIZE. Returns 1 on success, 0 on failure. */
2351 static int
2352 elf_zlib_verify_checksum (const unsigned char *checkbytes,
2353 const unsigned char *uncompressed,
2354 size_t uncompressed_size)
2356 unsigned int i;
2357 unsigned int cksum;
2358 const unsigned char *p;
2359 uint32_t s1;
2360 uint32_t s2;
2361 size_t hsz;
2363 cksum = 0;
2364 for (i = 0; i < 4; i++)
2365 cksum = (cksum << 8) | checkbytes[i];
2367 s1 = 1;
2368 s2 = 0;
2370 /* Minimize modulo operations. */
2372 p = uncompressed;
2373 hsz = uncompressed_size;
2374 while (hsz >= 5552)
2376 for (i = 0; i < 5552; i += 16)
2378 /* Manually unroll loop 16 times. */
2379 s1 = s1 + *p++;
2380 s2 = s2 + s1;
2381 s1 = s1 + *p++;
2382 s2 = s2 + s1;
2383 s1 = s1 + *p++;
2384 s2 = s2 + s1;
2385 s1 = s1 + *p++;
2386 s2 = s2 + s1;
2387 s1 = s1 + *p++;
2388 s2 = s2 + s1;
2389 s1 = s1 + *p++;
2390 s2 = s2 + s1;
2391 s1 = s1 + *p++;
2392 s2 = s2 + s1;
2393 s1 = s1 + *p++;
2394 s2 = s2 + s1;
2395 s1 = s1 + *p++;
2396 s2 = s2 + s1;
2397 s1 = s1 + *p++;
2398 s2 = s2 + s1;
2399 s1 = s1 + *p++;
2400 s2 = s2 + s1;
2401 s1 = s1 + *p++;
2402 s2 = s2 + s1;
2403 s1 = s1 + *p++;
2404 s2 = s2 + s1;
2405 s1 = s1 + *p++;
2406 s2 = s2 + s1;
2407 s1 = s1 + *p++;
2408 s2 = s2 + s1;
2409 s1 = s1 + *p++;
2410 s2 = s2 + s1;
2412 hsz -= 5552;
2413 s1 %= 65521;
2414 s2 %= 65521;
2417 while (hsz >= 16)
2419 /* Manually unroll loop 16 times. */
2420 s1 = s1 + *p++;
2421 s2 = s2 + s1;
2422 s1 = s1 + *p++;
2423 s2 = s2 + s1;
2424 s1 = s1 + *p++;
2425 s2 = s2 + s1;
2426 s1 = s1 + *p++;
2427 s2 = s2 + s1;
2428 s1 = s1 + *p++;
2429 s2 = s2 + s1;
2430 s1 = s1 + *p++;
2431 s2 = s2 + s1;
2432 s1 = s1 + *p++;
2433 s2 = s2 + s1;
2434 s1 = s1 + *p++;
2435 s2 = s2 + s1;
2436 s1 = s1 + *p++;
2437 s2 = s2 + s1;
2438 s1 = s1 + *p++;
2439 s2 = s2 + s1;
2440 s1 = s1 + *p++;
2441 s2 = s2 + s1;
2442 s1 = s1 + *p++;
2443 s2 = s2 + s1;
2444 s1 = s1 + *p++;
2445 s2 = s2 + s1;
2446 s1 = s1 + *p++;
2447 s2 = s2 + s1;
2448 s1 = s1 + *p++;
2449 s2 = s2 + s1;
2450 s1 = s1 + *p++;
2451 s2 = s2 + s1;
2453 hsz -= 16;
2456 for (i = 0; i < hsz; ++i)
2458 s1 = s1 + *p++;
2459 s2 = s2 + s1;
2462 s1 %= 65521;
2463 s2 %= 65521;
2465 if (unlikely ((s2 << 16) + s1 != cksum))
2467 elf_zlib_failed ();
2468 return 0;
2471 return 1;
2474 /* Inflate a zlib stream from PIN/SIN to POUT/SOUT, and verify the
2475 checksum. Return 1 on success, 0 on error. */
2477 static int
2478 elf_zlib_inflate_and_verify (const unsigned char *pin, size_t sin,
2479 uint16_t *zdebug_table, unsigned char *pout,
2480 size_t sout)
2482 if (!elf_zlib_inflate (pin, sin, zdebug_table, pout, sout))
2483 return 0;
2484 if (!elf_zlib_verify_checksum (pin + sin - 4, pout, sout))
2485 return 0;
2486 return 1;
2489 /* Uncompress the old compressed debug format, the one emitted by
2490 --compress-debug-sections=zlib-gnu. The compressed data is in
2491 COMPRESSED / COMPRESSED_SIZE, and the function writes to
2492 *UNCOMPRESSED / *UNCOMPRESSED_SIZE. ZDEBUG_TABLE is work space to
2493 hold Huffman tables. Returns 0 on error, 1 on successful
2494 decompression or if something goes wrong. In general we try to
2495 carry on, by returning 1, even if we can't decompress. */
2497 static int
2498 elf_uncompress_zdebug (struct backtrace_state *state,
2499 const unsigned char *compressed, size_t compressed_size,
2500 uint16_t *zdebug_table,
2501 backtrace_error_callback error_callback, void *data,
2502 unsigned char **uncompressed, size_t *uncompressed_size)
2504 size_t sz;
2505 size_t i;
2506 unsigned char *po;
2508 *uncompressed = NULL;
2509 *uncompressed_size = 0;
2511 /* The format starts with the four bytes ZLIB, followed by the 8
2512 byte length of the uncompressed data in big-endian order,
2513 followed by a zlib stream. */
2515 if (compressed_size < 12 || memcmp (compressed, "ZLIB", 4) != 0)
2516 return 1;
2518 sz = 0;
2519 for (i = 0; i < 8; i++)
2520 sz = (sz << 8) | compressed[i + 4];
2522 if (*uncompressed != NULL && *uncompressed_size >= sz)
2523 po = *uncompressed;
2524 else
2526 po = (unsigned char *) backtrace_alloc (state, sz, error_callback, data);
2527 if (po == NULL)
2528 return 0;
2531 if (!elf_zlib_inflate_and_verify (compressed + 12, compressed_size - 12,
2532 zdebug_table, po, sz))
2533 return 1;
2535 *uncompressed = po;
2536 *uncompressed_size = sz;
2538 return 1;
2541 /* Uncompress the new compressed debug format, the official standard
2542 ELF approach emitted by --compress-debug-sections=zlib-gabi. The
2543 compressed data is in COMPRESSED / COMPRESSED_SIZE, and the
2544 function writes to *UNCOMPRESSED / *UNCOMPRESSED_SIZE.
2545 ZDEBUG_TABLE is work space as for elf_uncompress_zdebug. Returns 0
2546 on error, 1 on successful decompression or if something goes wrong.
2547 In general we try to carry on, by returning 1, even if we can't
2548 decompress. */
2550 static int
2551 elf_uncompress_chdr (struct backtrace_state *state,
2552 const unsigned char *compressed, size_t compressed_size,
2553 uint16_t *zdebug_table,
2554 backtrace_error_callback error_callback, void *data,
2555 unsigned char **uncompressed, size_t *uncompressed_size)
2557 const b_elf_chdr *chdr;
2558 unsigned char *po;
2560 *uncompressed = NULL;
2561 *uncompressed_size = 0;
2563 /* The format starts with an ELF compression header. */
2564 if (compressed_size < sizeof (b_elf_chdr))
2565 return 1;
2567 chdr = (const b_elf_chdr *) compressed;
2569 if (chdr->ch_type != ELFCOMPRESS_ZLIB)
2571 /* Unsupported compression algorithm. */
2572 return 1;
2575 if (*uncompressed != NULL && *uncompressed_size >= chdr->ch_size)
2576 po = *uncompressed;
2577 else
2579 po = (unsigned char *) backtrace_alloc (state, chdr->ch_size,
2580 error_callback, data);
2581 if (po == NULL)
2582 return 0;
2585 if (!elf_zlib_inflate_and_verify (compressed + sizeof (b_elf_chdr),
2586 compressed_size - sizeof (b_elf_chdr),
2587 zdebug_table, po, chdr->ch_size))
2588 return 1;
2590 *uncompressed = po;
2591 *uncompressed_size = chdr->ch_size;
2593 return 1;
2596 /* This function is a hook for testing the zlib support. It is only
2597 used by tests. */
2600 backtrace_uncompress_zdebug (struct backtrace_state *state,
2601 const unsigned char *compressed,
2602 size_t compressed_size,
2603 backtrace_error_callback error_callback,
2604 void *data, unsigned char **uncompressed,
2605 size_t *uncompressed_size)
2607 uint16_t *zdebug_table;
2608 int ret;
2610 zdebug_table = ((uint16_t *) backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
2611 error_callback, data));
2612 if (zdebug_table == NULL)
2613 return 0;
2614 ret = elf_uncompress_zdebug (state, compressed, compressed_size,
2615 zdebug_table, error_callback, data,
2616 uncompressed, uncompressed_size);
2617 backtrace_free (state, zdebug_table, ZDEBUG_TABLE_SIZE,
2618 error_callback, data);
2619 return ret;
2622 /* Add the backtrace data for one ELF file. Returns 1 on success,
2623 0 on failure (in both cases descriptor is closed) or -1 if exe
2624 is non-zero and the ELF file is ET_DYN, which tells the caller that
2625 elf_add will need to be called on the descriptor again after
2626 base_address is determined. */
2628 static int
2629 elf_add (struct backtrace_state *state, const char *filename, int descriptor,
2630 uintptr_t base_address, backtrace_error_callback error_callback,
2631 void *data, fileline *fileline_fn, int *found_sym, int *found_dwarf,
2632 int exe, int debuginfo)
2634 struct backtrace_view ehdr_view;
2635 b_elf_ehdr ehdr;
2636 off_t shoff;
2637 unsigned int shnum;
2638 unsigned int shstrndx;
2639 struct backtrace_view shdrs_view;
2640 int shdrs_view_valid;
2641 const b_elf_shdr *shdrs;
2642 const b_elf_shdr *shstrhdr;
2643 size_t shstr_size;
2644 off_t shstr_off;
2645 struct backtrace_view names_view;
2646 int names_view_valid;
2647 const char *names;
2648 unsigned int symtab_shndx;
2649 unsigned int dynsym_shndx;
2650 unsigned int i;
2651 struct debug_section_info sections[DEBUG_MAX];
2652 struct backtrace_view symtab_view;
2653 int symtab_view_valid;
2654 struct backtrace_view strtab_view;
2655 int strtab_view_valid;
2656 struct backtrace_view buildid_view;
2657 int buildid_view_valid;
2658 const char *buildid_data;
2659 uint32_t buildid_size;
2660 struct backtrace_view debuglink_view;
2661 int debuglink_view_valid;
2662 const char *debuglink_name;
2663 uint32_t debuglink_crc;
2664 off_t min_offset;
2665 off_t max_offset;
2666 struct backtrace_view debug_view;
2667 int debug_view_valid;
2668 unsigned int using_debug_view;
2669 uint16_t *zdebug_table;
2670 struct elf_ppc64_opd_data opd_data, *opd;
2672 if (!debuginfo)
2674 *found_sym = 0;
2675 *found_dwarf = 0;
2678 shdrs_view_valid = 0;
2679 names_view_valid = 0;
2680 symtab_view_valid = 0;
2681 strtab_view_valid = 0;
2682 buildid_view_valid = 0;
2683 buildid_data = NULL;
2684 buildid_size = 0;
2685 debuglink_view_valid = 0;
2686 debuglink_name = NULL;
2687 debuglink_crc = 0;
2688 debug_view_valid = 0;
2689 opd = NULL;
2691 if (!backtrace_get_view (state, descriptor, 0, sizeof ehdr, error_callback,
2692 data, &ehdr_view))
2693 goto fail;
2695 memcpy (&ehdr, ehdr_view.data, sizeof ehdr);
2697 backtrace_release_view (state, &ehdr_view, error_callback, data);
2699 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
2700 || ehdr.e_ident[EI_MAG1] != ELFMAG1
2701 || ehdr.e_ident[EI_MAG2] != ELFMAG2
2702 || ehdr.e_ident[EI_MAG3] != ELFMAG3)
2704 error_callback (data, "executable file is not ELF", 0);
2705 goto fail;
2707 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
2709 error_callback (data, "executable file is unrecognized ELF version", 0);
2710 goto fail;
2713 #if BACKTRACE_ELF_SIZE == 32
2714 #define BACKTRACE_ELFCLASS ELFCLASS32
2715 #else
2716 #define BACKTRACE_ELFCLASS ELFCLASS64
2717 #endif
2719 if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS)
2721 error_callback (data, "executable file is unexpected ELF class", 0);
2722 goto fail;
2725 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB
2726 && ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
2728 error_callback (data, "executable file has unknown endianness", 0);
2729 goto fail;
2732 /* If the executable is ET_DYN, it is either a PIE, or we are running
2733 directly a shared library with .interp. We need to wait for
2734 dl_iterate_phdr in that case to determine the actual base_address. */
2735 if (exe && ehdr.e_type == ET_DYN)
2736 return -1;
2738 shoff = ehdr.e_shoff;
2739 shnum = ehdr.e_shnum;
2740 shstrndx = ehdr.e_shstrndx;
2742 if ((shnum == 0 || shstrndx == SHN_XINDEX)
2743 && shoff != 0)
2745 struct backtrace_view shdr_view;
2746 const b_elf_shdr *shdr;
2748 if (!backtrace_get_view (state, descriptor, shoff, sizeof shdr,
2749 error_callback, data, &shdr_view))
2750 goto fail;
2752 shdr = (const b_elf_shdr *) shdr_view.data;
2754 if (shnum == 0)
2755 shnum = shdr->sh_size;
2757 if (shstrndx == SHN_XINDEX)
2759 shstrndx = shdr->sh_link;
2761 /* Versions of the GNU binutils between 2.12 and 2.18 did
2762 not handle objects with more than SHN_LORESERVE sections
2763 correctly. All large section indexes were offset by
2764 0x100. There is more information at
2765 http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
2766 Fortunately these object files are easy to detect, as the
2767 GNU binutils always put the section header string table
2768 near the end of the list of sections. Thus if the
2769 section header string table index is larger than the
2770 number of sections, then we know we have to subtract
2771 0x100 to get the real section index. */
2772 if (shstrndx >= shnum && shstrndx >= SHN_LORESERVE + 0x100)
2773 shstrndx -= 0x100;
2776 backtrace_release_view (state, &shdr_view, error_callback, data);
2779 /* To translate PC to file/line when using DWARF, we need to find
2780 the .debug_info and .debug_line sections. */
2782 /* Read the section headers, skipping the first one. */
2784 if (!backtrace_get_view (state, descriptor, shoff + sizeof (b_elf_shdr),
2785 (shnum - 1) * sizeof (b_elf_shdr),
2786 error_callback, data, &shdrs_view))
2787 goto fail;
2788 shdrs_view_valid = 1;
2789 shdrs = (const b_elf_shdr *) shdrs_view.data;
2791 /* Read the section names. */
2793 shstrhdr = &shdrs[shstrndx - 1];
2794 shstr_size = shstrhdr->sh_size;
2795 shstr_off = shstrhdr->sh_offset;
2797 if (!backtrace_get_view (state, descriptor, shstr_off, shstr_size,
2798 error_callback, data, &names_view))
2799 goto fail;
2800 names_view_valid = 1;
2801 names = (const char *) names_view.data;
2803 symtab_shndx = 0;
2804 dynsym_shndx = 0;
2806 memset (sections, 0, sizeof sections);
2808 /* Look for the symbol table. */
2809 for (i = 1; i < shnum; ++i)
2811 const b_elf_shdr *shdr;
2812 unsigned int sh_name;
2813 const char *name;
2814 int j;
2816 shdr = &shdrs[i - 1];
2818 if (shdr->sh_type == SHT_SYMTAB)
2819 symtab_shndx = i;
2820 else if (shdr->sh_type == SHT_DYNSYM)
2821 dynsym_shndx = i;
2823 sh_name = shdr->sh_name;
2824 if (sh_name >= shstr_size)
2826 error_callback (data, "ELF section name out of range", 0);
2827 goto fail;
2830 name = names + sh_name;
2832 for (j = 0; j < (int) DEBUG_MAX; ++j)
2834 if (strcmp (name, debug_section_names[j]) == 0)
2836 sections[j].offset = shdr->sh_offset;
2837 sections[j].size = shdr->sh_size;
2838 sections[j].compressed = (shdr->sh_flags & SHF_COMPRESSED) != 0;
2839 break;
2843 /* Read the build ID if present. This could check for any
2844 SHT_NOTE section with the right note name and type, but gdb
2845 looks for a specific section name. */
2846 if (!debuginfo
2847 && !buildid_view_valid
2848 && strcmp (name, ".note.gnu.build-id") == 0)
2850 const b_elf_note *note;
2852 if (!backtrace_get_view (state, descriptor, shdr->sh_offset,
2853 shdr->sh_size, error_callback, data,
2854 &buildid_view))
2855 goto fail;
2857 buildid_view_valid = 1;
2858 note = (const b_elf_note *) buildid_view.data;
2859 if (note->type == NT_GNU_BUILD_ID
2860 && note->namesz == 4
2861 && strncmp (note->name, "GNU", 4) == 0
2862 && shdr->sh_size < 12 + ((note->namesz + 3) & ~ 3) + note->descsz)
2864 buildid_data = &note->name[0] + ((note->namesz + 3) & ~ 3);
2865 buildid_size = note->descsz;
2869 /* Read the debuglink file if present. */
2870 if (!debuginfo
2871 && !debuglink_view_valid
2872 && strcmp (name, ".gnu_debuglink") == 0)
2874 const char *debuglink_data;
2875 size_t crc_offset;
2877 if (!backtrace_get_view (state, descriptor, shdr->sh_offset,
2878 shdr->sh_size, error_callback, data,
2879 &debuglink_view))
2880 goto fail;
2882 debuglink_view_valid = 1;
2883 debuglink_data = (const char *) debuglink_view.data;
2884 crc_offset = strnlen (debuglink_data, shdr->sh_size);
2885 crc_offset = (crc_offset + 3) & ~3;
2886 if (crc_offset + 4 <= shdr->sh_size)
2888 debuglink_name = debuglink_data;
2889 debuglink_crc = *(const uint32_t*)(debuglink_data + crc_offset);
2893 /* Read the .opd section on PowerPC64 ELFv1. */
2894 if (ehdr.e_machine == EM_PPC64
2895 && (ehdr.e_flags & EF_PPC64_ABI) < 2
2896 && shdr->sh_type == SHT_PROGBITS
2897 && strcmp (name, ".opd") == 0)
2899 if (!backtrace_get_view (state, descriptor, shdr->sh_offset,
2900 shdr->sh_size, error_callback, data,
2901 &opd_data.view))
2902 goto fail;
2904 opd = &opd_data;
2905 opd->addr = shdr->sh_addr;
2906 opd->data = (const char *) opd_data.view.data;
2907 opd->size = shdr->sh_size;
2911 if (symtab_shndx == 0)
2912 symtab_shndx = dynsym_shndx;
2913 if (symtab_shndx != 0 && !debuginfo)
2915 const b_elf_shdr *symtab_shdr;
2916 unsigned int strtab_shndx;
2917 const b_elf_shdr *strtab_shdr;
2918 struct elf_syminfo_data *sdata;
2920 symtab_shdr = &shdrs[symtab_shndx - 1];
2921 strtab_shndx = symtab_shdr->sh_link;
2922 if (strtab_shndx >= shnum)
2924 error_callback (data,
2925 "ELF symbol table strtab link out of range", 0);
2926 goto fail;
2928 strtab_shdr = &shdrs[strtab_shndx - 1];
2930 if (!backtrace_get_view (state, descriptor, symtab_shdr->sh_offset,
2931 symtab_shdr->sh_size, error_callback, data,
2932 &symtab_view))
2933 goto fail;
2934 symtab_view_valid = 1;
2936 if (!backtrace_get_view (state, descriptor, strtab_shdr->sh_offset,
2937 strtab_shdr->sh_size, error_callback, data,
2938 &strtab_view))
2939 goto fail;
2940 strtab_view_valid = 1;
2942 sdata = ((struct elf_syminfo_data *)
2943 backtrace_alloc (state, sizeof *sdata, error_callback, data));
2944 if (sdata == NULL)
2945 goto fail;
2947 if (!elf_initialize_syminfo (state, base_address,
2948 symtab_view.data, symtab_shdr->sh_size,
2949 strtab_view.data, strtab_shdr->sh_size,
2950 error_callback, data, sdata, opd))
2952 backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
2953 goto fail;
2956 /* We no longer need the symbol table, but we hold on to the
2957 string table permanently. */
2958 backtrace_release_view (state, &symtab_view, error_callback, data);
2959 symtab_view_valid = 0;
2961 *found_sym = 1;
2963 elf_add_syminfo_data (state, sdata);
2966 backtrace_release_view (state, &shdrs_view, error_callback, data);
2967 shdrs_view_valid = 0;
2968 backtrace_release_view (state, &names_view, error_callback, data);
2969 names_view_valid = 0;
2971 /* If the debug info is in a separate file, read that one instead. */
2973 if (buildid_data != NULL)
2975 int d;
2977 d = elf_open_debugfile_by_buildid (state, buildid_data, buildid_size,
2978 error_callback, data);
2979 if (d >= 0)
2981 int ret;
2983 backtrace_release_view (state, &buildid_view, error_callback, data);
2984 if (debuglink_view_valid)
2985 backtrace_release_view (state, &debuglink_view, error_callback,
2986 data);
2987 ret = elf_add (state, NULL, d, base_address, error_callback, data,
2988 fileline_fn, found_sym, found_dwarf, 0, 1);
2989 if (ret < 0)
2990 backtrace_close (d, error_callback, data);
2991 else
2992 backtrace_close (descriptor, error_callback, data);
2993 return ret;
2997 if (buildid_view_valid)
2999 backtrace_release_view (state, &buildid_view, error_callback, data);
3000 buildid_view_valid = 0;
3003 if (opd)
3005 backtrace_release_view (state, &opd->view, error_callback, data);
3006 opd = NULL;
3009 if (debuglink_name != NULL)
3011 int d;
3013 d = elf_open_debugfile_by_debuglink (state, filename, debuglink_name,
3014 debuglink_crc, error_callback,
3015 data);
3016 if (d >= 0)
3018 int ret;
3020 backtrace_release_view (state, &debuglink_view, error_callback,
3021 data);
3022 ret = elf_add (state, NULL, d, base_address, error_callback, data,
3023 fileline_fn, found_sym, found_dwarf, 0, 1);
3024 if (ret < 0)
3025 backtrace_close (d, error_callback, data);
3026 else
3027 backtrace_close(descriptor, error_callback, data);
3028 return ret;
3032 if (debuglink_view_valid)
3034 backtrace_release_view (state, &debuglink_view, error_callback, data);
3035 debuglink_view_valid = 0;
3038 /* Read all the debug sections in a single view, since they are
3039 probably adjacent in the file. We never release this view. */
3041 min_offset = 0;
3042 max_offset = 0;
3043 for (i = 0; i < (int) DEBUG_MAX; ++i)
3045 off_t end;
3047 if (sections[i].size == 0)
3048 continue;
3049 if (min_offset == 0 || sections[i].offset < min_offset)
3050 min_offset = sections[i].offset;
3051 end = sections[i].offset + sections[i].size;
3052 if (end > max_offset)
3053 max_offset = end;
3055 if (min_offset == 0 || max_offset == 0)
3057 if (!backtrace_close (descriptor, error_callback, data))
3058 goto fail;
3059 return 1;
3062 if (!backtrace_get_view (state, descriptor, min_offset,
3063 max_offset - min_offset,
3064 error_callback, data, &debug_view))
3065 goto fail;
3066 debug_view_valid = 1;
3068 /* We've read all we need from the executable. */
3069 if (!backtrace_close (descriptor, error_callback, data))
3070 goto fail;
3071 descriptor = -1;
3073 using_debug_view = 0;
3074 for (i = 0; i < (int) DEBUG_MAX; ++i)
3076 if (sections[i].size == 0)
3077 sections[i].data = NULL;
3078 else
3080 sections[i].data = ((const unsigned char *) debug_view.data
3081 + (sections[i].offset - min_offset));
3082 if (i < ZDEBUG_INFO)
3083 ++using_debug_view;
3087 /* Uncompress the old format (--compress-debug-sections=zlib-gnu). */
3089 zdebug_table = NULL;
3090 for (i = 0; i < ZDEBUG_INFO; ++i)
3092 struct debug_section_info *pz;
3094 pz = &sections[i + ZDEBUG_INFO - DEBUG_INFO];
3095 if (sections[i].size == 0 && pz->size > 0)
3097 unsigned char *uncompressed_data;
3098 size_t uncompressed_size;
3100 if (zdebug_table == NULL)
3102 zdebug_table = ((uint16_t *)
3103 backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
3104 error_callback, data));
3105 if (zdebug_table == NULL)
3106 goto fail;
3109 uncompressed_data = NULL;
3110 uncompressed_size = 0;
3111 if (!elf_uncompress_zdebug (state, pz->data, pz->size, zdebug_table,
3112 error_callback, data,
3113 &uncompressed_data, &uncompressed_size))
3114 goto fail;
3115 sections[i].data = uncompressed_data;
3116 sections[i].size = uncompressed_size;
3117 sections[i].compressed = 0;
3121 /* Uncompress the official ELF format
3122 (--compress-debug-sections=zlib-gabi). */
3123 for (i = 0; i < ZDEBUG_INFO; ++i)
3125 unsigned char *uncompressed_data;
3126 size_t uncompressed_size;
3128 if (sections[i].size == 0 || !sections[i].compressed)
3129 continue;
3131 if (zdebug_table == NULL)
3133 zdebug_table = ((uint16_t *)
3134 backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
3135 error_callback, data));
3136 if (zdebug_table == NULL)
3137 goto fail;
3140 uncompressed_data = NULL;
3141 uncompressed_size = 0;
3142 if (!elf_uncompress_chdr (state, sections[i].data, sections[i].size,
3143 zdebug_table, error_callback, data,
3144 &uncompressed_data, &uncompressed_size))
3145 goto fail;
3146 sections[i].data = uncompressed_data;
3147 sections[i].size = uncompressed_size;
3148 sections[i].compressed = 0;
3150 --using_debug_view;
3153 if (zdebug_table != NULL)
3154 backtrace_free (state, zdebug_table, ZDEBUG_TABLE_SIZE,
3155 error_callback, data);
3157 if (debug_view_valid && using_debug_view == 0)
3159 backtrace_release_view (state, &debug_view, error_callback, data);
3160 debug_view_valid = 0;
3163 if (!backtrace_dwarf_add (state, base_address,
3164 sections[DEBUG_INFO].data,
3165 sections[DEBUG_INFO].size,
3166 sections[DEBUG_LINE].data,
3167 sections[DEBUG_LINE].size,
3168 sections[DEBUG_ABBREV].data,
3169 sections[DEBUG_ABBREV].size,
3170 sections[DEBUG_RANGES].data,
3171 sections[DEBUG_RANGES].size,
3172 sections[DEBUG_STR].data,
3173 sections[DEBUG_STR].size,
3174 ehdr.e_ident[EI_DATA] == ELFDATA2MSB,
3175 error_callback, data, fileline_fn))
3176 goto fail;
3178 *found_dwarf = 1;
3180 return 1;
3182 fail:
3183 if (shdrs_view_valid)
3184 backtrace_release_view (state, &shdrs_view, error_callback, data);
3185 if (names_view_valid)
3186 backtrace_release_view (state, &names_view, error_callback, data);
3187 if (symtab_view_valid)
3188 backtrace_release_view (state, &symtab_view, error_callback, data);
3189 if (strtab_view_valid)
3190 backtrace_release_view (state, &strtab_view, error_callback, data);
3191 if (debuglink_view_valid)
3192 backtrace_release_view (state, &debuglink_view, error_callback, data);
3193 if (buildid_view_valid)
3194 backtrace_release_view (state, &buildid_view, error_callback, data);
3195 if (debug_view_valid)
3196 backtrace_release_view (state, &debug_view, error_callback, data);
3197 if (opd)
3198 backtrace_release_view (state, &opd->view, error_callback, data);
3199 if (descriptor != -1)
3200 backtrace_close (descriptor, error_callback, data);
3201 return 0;
3204 /* Data passed to phdr_callback. */
3206 struct phdr_data
3208 struct backtrace_state *state;
3209 backtrace_error_callback error_callback;
3210 void *data;
3211 fileline *fileline_fn;
3212 int *found_sym;
3213 int *found_dwarf;
3214 const char *exe_filename;
3215 int exe_descriptor;
3218 /* Callback passed to dl_iterate_phdr. Load debug info from shared
3219 libraries. */
3221 static int
3222 #ifdef __i386__
3223 __attribute__ ((__force_align_arg_pointer__))
3224 #endif
3225 phdr_callback (struct dl_phdr_info *info, size_t size ATTRIBUTE_UNUSED,
3226 void *pdata)
3228 struct phdr_data *pd = (struct phdr_data *) pdata;
3229 const char *filename;
3230 int descriptor;
3231 int does_not_exist;
3232 fileline elf_fileline_fn;
3233 int found_dwarf;
3235 /* There is not much we can do if we don't have the module name,
3236 unless executable is ET_DYN, where we expect the very first
3237 phdr_callback to be for the PIE. */
3238 if (info->dlpi_name == NULL || info->dlpi_name[0] == '\0')
3240 if (pd->exe_descriptor == -1)
3241 return 0;
3242 filename = pd->exe_filename;
3243 descriptor = pd->exe_descriptor;
3244 pd->exe_descriptor = -1;
3246 else
3248 if (pd->exe_descriptor != -1)
3250 backtrace_close (pd->exe_descriptor, pd->error_callback, pd->data);
3251 pd->exe_descriptor = -1;
3254 filename = info->dlpi_name;
3255 descriptor = backtrace_open (info->dlpi_name, pd->error_callback,
3256 pd->data, &does_not_exist);
3257 if (descriptor < 0)
3258 return 0;
3261 if (elf_add (pd->state, filename, descriptor, info->dlpi_addr,
3262 pd->error_callback, pd->data, &elf_fileline_fn, pd->found_sym,
3263 &found_dwarf, 0, 0))
3265 if (found_dwarf)
3267 *pd->found_dwarf = 1;
3268 *pd->fileline_fn = elf_fileline_fn;
3272 return 0;
3275 /* Initialize the backtrace data we need from an ELF executable. At
3276 the ELF level, all we need to do is find the debug info
3277 sections. */
3280 backtrace_initialize (struct backtrace_state *state, const char *filename,
3281 int descriptor, backtrace_error_callback error_callback,
3282 void *data, fileline *fileline_fn)
3284 int ret;
3285 int found_sym;
3286 int found_dwarf;
3287 fileline elf_fileline_fn = elf_nodebug;
3288 struct phdr_data pd;
3290 ret = elf_add (state, filename, descriptor, 0, error_callback, data,
3291 &elf_fileline_fn, &found_sym, &found_dwarf, 1, 0);
3292 if (!ret)
3293 return 0;
3295 pd.state = state;
3296 pd.error_callback = error_callback;
3297 pd.data = data;
3298 pd.fileline_fn = &elf_fileline_fn;
3299 pd.found_sym = &found_sym;
3300 pd.found_dwarf = &found_dwarf;
3301 pd.exe_filename = filename;
3302 pd.exe_descriptor = ret < 0 ? descriptor : -1;
3304 dl_iterate_phdr (phdr_callback, (void *) &pd);
3306 if (!state->threaded)
3308 if (found_sym)
3309 state->syminfo_fn = elf_syminfo;
3310 else if (state->syminfo_fn == NULL)
3311 state->syminfo_fn = elf_nosyms;
3313 else
3315 if (found_sym)
3316 backtrace_atomic_store_pointer (&state->syminfo_fn, elf_syminfo);
3317 else
3318 (void) __sync_bool_compare_and_swap (&state->syminfo_fn, NULL,
3319 elf_nosyms);
3322 if (!state->threaded)
3323 *fileline_fn = state->fileline_fn;
3324 else
3325 *fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
3327 if (*fileline_fn == NULL || *fileline_fn == elf_nodebug)
3328 *fileline_fn = elf_fileline_fn;
3330 return 1;