tree.c (build_array_type_1): Add SET_CANONICAL parameter and compute TYPE_CANONICAL...
[gcc.git] / libbacktrace / elf.c
1 /* elf.c -- Get debug data from an ELF file for backtraces.
2 Copyright (C) 2012-2019 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 <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.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 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
58
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
65
66 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
67
68 /* If strnlen is not declared, provide our own version. */
69
70 static size_t
71 xstrnlen (const char *s, size_t maxlen)
72 {
73 size_t i;
74
75 for (i = 0; i < maxlen; ++i)
76 if (s[i] == '\0')
77 break;
78 return i;
79 }
80
81 #define strnlen xstrnlen
82
83 #endif
84
85 #ifndef HAVE_LSTAT
86
87 /* Dummy version of lstat for systems that don't have it. */
88
89 static int
90 xlstat (const char *path ATTRIBUTE_UNUSED, struct stat *st ATTRIBUTE_UNUSED)
91 {
92 return -1;
93 }
94
95 #define lstat xlstat
96
97 #endif
98
99 #ifndef HAVE_READLINK
100
101 /* Dummy version of readlink for systems that don't have it. */
102
103 static ssize_t
104 xreadlink (const char *path ATTRIBUTE_UNUSED, char *buf ATTRIBUTE_UNUSED,
105 size_t bufsz ATTRIBUTE_UNUSED)
106 {
107 return -1;
108 }
109
110 #define readlink xreadlink
111
112 #endif
113
114 #ifndef HAVE_DL_ITERATE_PHDR
115
116 /* Dummy version of dl_iterate_phdr for systems that don't have it. */
117
118 #define dl_phdr_info x_dl_phdr_info
119 #define dl_iterate_phdr x_dl_iterate_phdr
120
121 struct dl_phdr_info
122 {
123 uintptr_t dlpi_addr;
124 const char *dlpi_name;
125 };
126
127 static int
128 dl_iterate_phdr (int (*callback) (struct dl_phdr_info *,
129 size_t, void *) ATTRIBUTE_UNUSED,
130 void *data ATTRIBUTE_UNUSED)
131 {
132 return 0;
133 }
134
135 #endif /* ! defined (HAVE_DL_ITERATE_PHDR) */
136
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. */
142
143 #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64
144 #error "Unknown BACKTRACE_ELF_SIZE"
145 #endif
146
147 /* <link.h> might #include <elf.h> which might define our constants
148 with slightly different values. Undefine them to be safe. */
149
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_PROGBITS
174 #undef SHT_SYMTAB
175 #undef SHT_STRTAB
176 #undef SHT_DYNSYM
177 #undef SHF_COMPRESSED
178 #undef STT_OBJECT
179 #undef STT_FUNC
180 #undef NT_GNU_BUILD_ID
181 #undef ELFCOMPRESS_ZLIB
182
183 /* Basic types. */
184
185 typedef uint16_t b_elf_half; /* Elf_Half. */
186 typedef uint32_t b_elf_word; /* Elf_Word. */
187 typedef int32_t b_elf_sword; /* Elf_Sword. */
188
189 #if BACKTRACE_ELF_SIZE == 32
190
191 typedef uint32_t b_elf_addr; /* Elf_Addr. */
192 typedef uint32_t b_elf_off; /* Elf_Off. */
193
194 typedef uint32_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
195
196 #else
197
198 typedef uint64_t b_elf_addr; /* Elf_Addr. */
199 typedef uint64_t b_elf_off; /* Elf_Off. */
200 typedef uint64_t b_elf_xword; /* Elf_Xword. */
201 typedef int64_t b_elf_sxword; /* Elf_Sxword. */
202
203 typedef uint64_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */
204
205 #endif
206
207 /* Data structures and associated constants. */
208
209 #define EI_NIDENT 16
210
211 typedef struct {
212 unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
213 b_elf_half e_type; /* Identifies object file type */
214 b_elf_half e_machine; /* Specifies required architecture */
215 b_elf_word e_version; /* Identifies object file version */
216 b_elf_addr e_entry; /* Entry point virtual address */
217 b_elf_off e_phoff; /* Program header table file offset */
218 b_elf_off e_shoff; /* Section header table file offset */
219 b_elf_word e_flags; /* Processor-specific flags */
220 b_elf_half e_ehsize; /* ELF header size in bytes */
221 b_elf_half e_phentsize; /* Program header table entry size */
222 b_elf_half e_phnum; /* Program header table entry count */
223 b_elf_half e_shentsize; /* Section header table entry size */
224 b_elf_half e_shnum; /* Section header table entry count */
225 b_elf_half e_shstrndx; /* Section header string table index */
226 } b_elf_ehdr; /* Elf_Ehdr. */
227
228 #define EI_MAG0 0
229 #define EI_MAG1 1
230 #define EI_MAG2 2
231 #define EI_MAG3 3
232 #define EI_CLASS 4
233 #define EI_DATA 5
234 #define EI_VERSION 6
235
236 #define ELFMAG0 0x7f
237 #define ELFMAG1 'E'
238 #define ELFMAG2 'L'
239 #define ELFMAG3 'F'
240
241 #define ELFCLASS32 1
242 #define ELFCLASS64 2
243
244 #define ELFDATA2LSB 1
245 #define ELFDATA2MSB 2
246
247 #define EV_CURRENT 1
248
249 #define ET_DYN 3
250
251 #define EM_PPC64 21
252 #define EF_PPC64_ABI 3
253
254 typedef struct {
255 b_elf_word sh_name; /* Section name, index in string tbl */
256 b_elf_word sh_type; /* Type of section */
257 b_elf_wxword sh_flags; /* Miscellaneous section attributes */
258 b_elf_addr sh_addr; /* Section virtual addr at execution */
259 b_elf_off sh_offset; /* Section file offset */
260 b_elf_wxword sh_size; /* Size of section in bytes */
261 b_elf_word sh_link; /* Index of another section */
262 b_elf_word sh_info; /* Additional section information */
263 b_elf_wxword sh_addralign; /* Section alignment */
264 b_elf_wxword sh_entsize; /* Entry size if section holds table */
265 } b_elf_shdr; /* Elf_Shdr. */
266
267 #define SHN_UNDEF 0x0000 /* Undefined section */
268 #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
269 #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
270
271 #define SHT_PROGBITS 1
272 #define SHT_SYMTAB 2
273 #define SHT_STRTAB 3
274 #define SHT_DYNSYM 11
275
276 #define SHF_COMPRESSED 0x800
277
278 #if BACKTRACE_ELF_SIZE == 32
279
280 typedef struct
281 {
282 b_elf_word st_name; /* Symbol name, index in string tbl */
283 b_elf_addr st_value; /* Symbol value */
284 b_elf_word st_size; /* Symbol size */
285 unsigned char st_info; /* Symbol binding and type */
286 unsigned char st_other; /* Visibility and other data */
287 b_elf_half st_shndx; /* Symbol section index */
288 } b_elf_sym; /* Elf_Sym. */
289
290 #else /* BACKTRACE_ELF_SIZE != 32 */
291
292 typedef struct
293 {
294 b_elf_word st_name; /* Symbol name, index in string tbl */
295 unsigned char st_info; /* Symbol binding and type */
296 unsigned char st_other; /* Visibility and other data */
297 b_elf_half st_shndx; /* Symbol section index */
298 b_elf_addr st_value; /* Symbol value */
299 b_elf_xword st_size; /* Symbol size */
300 } b_elf_sym; /* Elf_Sym. */
301
302 #endif /* BACKTRACE_ELF_SIZE != 32 */
303
304 #define STT_OBJECT 1
305 #define STT_FUNC 2
306
307 typedef struct
308 {
309 uint32_t namesz;
310 uint32_t descsz;
311 uint32_t type;
312 char name[1];
313 } b_elf_note;
314
315 #define NT_GNU_BUILD_ID 3
316
317 #if BACKTRACE_ELF_SIZE == 32
318
319 typedef struct
320 {
321 b_elf_word ch_type; /* Compresstion algorithm */
322 b_elf_word ch_size; /* Uncompressed size */
323 b_elf_word ch_addralign; /* Alignment for uncompressed data */
324 } b_elf_chdr; /* Elf_Chdr */
325
326 #else /* BACKTRACE_ELF_SIZE != 32 */
327
328 typedef struct
329 {
330 b_elf_word ch_type; /* Compression algorithm */
331 b_elf_word ch_reserved; /* Reserved */
332 b_elf_xword ch_size; /* Uncompressed size */
333 b_elf_xword ch_addralign; /* Alignment for uncompressed data */
334 } b_elf_chdr; /* Elf_Chdr */
335
336 #endif /* BACKTRACE_ELF_SIZE != 32 */
337
338 #define ELFCOMPRESS_ZLIB 1
339
340 /* Names of sections, indexed by enum dwarf_section in internal.h. */
341
342 static const char * const dwarf_section_names[DEBUG_MAX] =
343 {
344 ".debug_info",
345 ".debug_line",
346 ".debug_abbrev",
347 ".debug_ranges",
348 ".debug_str",
349 };
350
351 /* Information we gather for the sections we care about. */
352
353 struct debug_section_info
354 {
355 /* Section file offset. */
356 off_t offset;
357 /* Section size. */
358 size_t size;
359 /* Section contents, after read from file. */
360 const unsigned char *data;
361 /* Whether the SHF_COMPRESSED flag is set for the section. */
362 int compressed;
363 };
364
365 /* Information we keep for an ELF symbol. */
366
367 struct elf_symbol
368 {
369 /* The name of the symbol. */
370 const char *name;
371 /* The address of the symbol. */
372 uintptr_t address;
373 /* The size of the symbol. */
374 size_t size;
375 };
376
377 /* Information to pass to elf_syminfo. */
378
379 struct elf_syminfo_data
380 {
381 /* Symbols for the next module. */
382 struct elf_syminfo_data *next;
383 /* The ELF symbols, sorted by address. */
384 struct elf_symbol *symbols;
385 /* The number of symbols. */
386 size_t count;
387 };
388
389 /* Information about PowerPC64 ELFv1 .opd section. */
390
391 struct elf_ppc64_opd_data
392 {
393 /* Address of the .opd section. */
394 b_elf_addr addr;
395 /* Section data. */
396 const char *data;
397 /* Size of the .opd section. */
398 size_t size;
399 /* Corresponding section view. */
400 struct backtrace_view view;
401 };
402
403 /* Compute the CRC-32 of BUF/LEN. This uses the CRC used for
404 .gnu_debuglink files. */
405
406 static uint32_t
407 elf_crc32 (uint32_t crc, const unsigned char *buf, size_t len)
408 {
409 static const uint32_t crc32_table[256] =
410 {
411 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
412 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
413 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
414 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
415 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
416 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
417 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
418 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
419 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
420 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
421 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
422 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
423 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
424 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
425 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
426 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
427 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
428 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
429 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
430 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
431 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
432 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
433 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
434 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
435 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
436 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
437 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
438 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
439 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
440 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
441 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
442 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
443 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
444 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
445 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
446 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
447 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
448 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
449 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
450 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
451 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
452 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
453 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
454 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
455 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
456 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
457 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
458 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
459 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
460 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
461 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
462 0x2d02ef8d
463 };
464 const unsigned char *end;
465
466 crc = ~crc;
467 for (end = buf + len; buf < end; ++ buf)
468 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
469 return ~crc;
470 }
471
472 /* Return the CRC-32 of the entire file open at DESCRIPTOR. */
473
474 static uint32_t
475 elf_crc32_file (struct backtrace_state *state, int descriptor,
476 backtrace_error_callback error_callback, void *data)
477 {
478 struct stat st;
479 struct backtrace_view file_view;
480 uint32_t ret;
481
482 if (fstat (descriptor, &st) < 0)
483 {
484 error_callback (data, "fstat", errno);
485 return 0;
486 }
487
488 if (!backtrace_get_view (state, descriptor, 0, st.st_size, error_callback,
489 data, &file_view))
490 return 0;
491
492 ret = elf_crc32 (0, (const unsigned char *) file_view.data, st.st_size);
493
494 backtrace_release_view (state, &file_view, error_callback, data);
495
496 return ret;
497 }
498
499 /* A dummy callback function used when we can't find any debug info. */
500
501 static int
502 elf_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
503 uintptr_t pc ATTRIBUTE_UNUSED,
504 backtrace_full_callback callback ATTRIBUTE_UNUSED,
505 backtrace_error_callback error_callback, void *data)
506 {
507 error_callback (data, "no debug info in ELF executable", -1);
508 return 0;
509 }
510
511 /* A dummy callback function used when we can't find a symbol
512 table. */
513
514 static void
515 elf_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
516 uintptr_t addr ATTRIBUTE_UNUSED,
517 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
518 backtrace_error_callback error_callback, void *data)
519 {
520 error_callback (data, "no symbol table in ELF executable", -1);
521 }
522
523 /* Compare struct elf_symbol for qsort. */
524
525 static int
526 elf_symbol_compare (const void *v1, const void *v2)
527 {
528 const struct elf_symbol *e1 = (const struct elf_symbol *) v1;
529 const struct elf_symbol *e2 = (const struct elf_symbol *) v2;
530
531 if (e1->address < e2->address)
532 return -1;
533 else if (e1->address > e2->address)
534 return 1;
535 else
536 return 0;
537 }
538
539 /* Compare an ADDR against an elf_symbol for bsearch. We allocate one
540 extra entry in the array so that this can look safely at the next
541 entry. */
542
543 static int
544 elf_symbol_search (const void *vkey, const void *ventry)
545 {
546 const uintptr_t *key = (const uintptr_t *) vkey;
547 const struct elf_symbol *entry = (const struct elf_symbol *) ventry;
548 uintptr_t addr;
549
550 addr = *key;
551 if (addr < entry->address)
552 return -1;
553 else if (addr >= entry->address + entry->size)
554 return 1;
555 else
556 return 0;
557 }
558
559 /* Initialize the symbol table info for elf_syminfo. */
560
561 static int
562 elf_initialize_syminfo (struct backtrace_state *state,
563 uintptr_t base_address,
564 const unsigned char *symtab_data, size_t symtab_size,
565 const unsigned char *strtab, size_t strtab_size,
566 backtrace_error_callback error_callback,
567 void *data, struct elf_syminfo_data *sdata,
568 struct elf_ppc64_opd_data *opd)
569 {
570 size_t sym_count;
571 const b_elf_sym *sym;
572 size_t elf_symbol_count;
573 size_t elf_symbol_size;
574 struct elf_symbol *elf_symbols;
575 size_t i;
576 unsigned int j;
577
578 sym_count = symtab_size / sizeof (b_elf_sym);
579
580 /* We only care about function symbols. Count them. */
581 sym = (const b_elf_sym *) symtab_data;
582 elf_symbol_count = 0;
583 for (i = 0; i < sym_count; ++i, ++sym)
584 {
585 int info;
586
587 info = sym->st_info & 0xf;
588 if ((info == STT_FUNC || info == STT_OBJECT)
589 && sym->st_shndx != SHN_UNDEF)
590 ++elf_symbol_count;
591 }
592
593 elf_symbol_size = elf_symbol_count * sizeof (struct elf_symbol);
594 elf_symbols = ((struct elf_symbol *)
595 backtrace_alloc (state, elf_symbol_size, error_callback,
596 data));
597 if (elf_symbols == NULL)
598 return 0;
599
600 sym = (const b_elf_sym *) symtab_data;
601 j = 0;
602 for (i = 0; i < sym_count; ++i, ++sym)
603 {
604 int info;
605
606 info = sym->st_info & 0xf;
607 if (info != STT_FUNC && info != STT_OBJECT)
608 continue;
609 if (sym->st_shndx == SHN_UNDEF)
610 continue;
611 if (sym->st_name >= strtab_size)
612 {
613 error_callback (data, "symbol string index out of range", 0);
614 backtrace_free (state, elf_symbols, elf_symbol_size, error_callback,
615 data);
616 return 0;
617 }
618 elf_symbols[j].name = (const char *) strtab + sym->st_name;
619 /* Special case PowerPC64 ELFv1 symbols in .opd section, if the symbol
620 is a function descriptor, read the actual code address from the
621 descriptor. */
622 if (opd
623 && sym->st_value >= opd->addr
624 && sym->st_value < opd->addr + opd->size)
625 elf_symbols[j].address
626 = *(const b_elf_addr *) (opd->data + (sym->st_value - opd->addr));
627 else
628 elf_symbols[j].address = sym->st_value;
629 elf_symbols[j].address += base_address;
630 elf_symbols[j].size = sym->st_size;
631 ++j;
632 }
633
634 backtrace_qsort (elf_symbols, elf_symbol_count, sizeof (struct elf_symbol),
635 elf_symbol_compare);
636
637 sdata->next = NULL;
638 sdata->symbols = elf_symbols;
639 sdata->count = elf_symbol_count;
640
641 return 1;
642 }
643
644 /* Add EDATA to the list in STATE. */
645
646 static void
647 elf_add_syminfo_data (struct backtrace_state *state,
648 struct elf_syminfo_data *edata)
649 {
650 if (!state->threaded)
651 {
652 struct elf_syminfo_data **pp;
653
654 for (pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
655 *pp != NULL;
656 pp = &(*pp)->next)
657 ;
658 *pp = edata;
659 }
660 else
661 {
662 while (1)
663 {
664 struct elf_syminfo_data **pp;
665
666 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
667
668 while (1)
669 {
670 struct elf_syminfo_data *p;
671
672 p = backtrace_atomic_load_pointer (pp);
673
674 if (p == NULL)
675 break;
676
677 pp = &p->next;
678 }
679
680 if (__sync_bool_compare_and_swap (pp, NULL, edata))
681 break;
682 }
683 }
684 }
685
686 /* Return the symbol name and value for an ADDR. */
687
688 static void
689 elf_syminfo (struct backtrace_state *state, uintptr_t addr,
690 backtrace_syminfo_callback callback,
691 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
692 void *data)
693 {
694 struct elf_syminfo_data *edata;
695 struct elf_symbol *sym = NULL;
696
697 if (!state->threaded)
698 {
699 for (edata = (struct elf_syminfo_data *) state->syminfo_data;
700 edata != NULL;
701 edata = edata->next)
702 {
703 sym = ((struct elf_symbol *)
704 bsearch (&addr, edata->symbols, edata->count,
705 sizeof (struct elf_symbol), elf_symbol_search));
706 if (sym != NULL)
707 break;
708 }
709 }
710 else
711 {
712 struct elf_syminfo_data **pp;
713
714 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data;
715 while (1)
716 {
717 edata = backtrace_atomic_load_pointer (pp);
718 if (edata == NULL)
719 break;
720
721 sym = ((struct elf_symbol *)
722 bsearch (&addr, edata->symbols, edata->count,
723 sizeof (struct elf_symbol), elf_symbol_search));
724 if (sym != NULL)
725 break;
726
727 pp = &edata->next;
728 }
729 }
730
731 if (sym == NULL)
732 callback (data, addr, NULL, 0, 0);
733 else
734 callback (data, addr, sym->name, sym->address, sym->size);
735 }
736
737 /* Return whether FILENAME is a symlink. */
738
739 static int
740 elf_is_symlink (const char *filename)
741 {
742 struct stat st;
743
744 if (lstat (filename, &st) < 0)
745 return 0;
746 return S_ISLNK (st.st_mode);
747 }
748
749 /* Return the results of reading the symlink FILENAME in a buffer
750 allocated by backtrace_alloc. Return the length of the buffer in
751 *LEN. */
752
753 static char *
754 elf_readlink (struct backtrace_state *state, const char *filename,
755 backtrace_error_callback error_callback, void *data,
756 size_t *plen)
757 {
758 size_t len;
759 char *buf;
760
761 len = 128;
762 while (1)
763 {
764 ssize_t rl;
765
766 buf = backtrace_alloc (state, len, error_callback, data);
767 if (buf == NULL)
768 return NULL;
769 rl = readlink (filename, buf, len);
770 if (rl < 0)
771 {
772 backtrace_free (state, buf, len, error_callback, data);
773 return NULL;
774 }
775 if ((size_t) rl < len - 1)
776 {
777 buf[rl] = '\0';
778 *plen = len;
779 return buf;
780 }
781 backtrace_free (state, buf, len, error_callback, data);
782 len *= 2;
783 }
784 }
785
786 #define SYSTEM_BUILD_ID_DIR "/usr/lib/debug/.build-id/"
787
788 /* Open a separate debug info file, using the build ID to find it.
789 Returns an open file descriptor, or -1.
790
791 The GDB manual says that the only place gdb looks for a debug file
792 when the build ID is known is in /usr/lib/debug/.build-id. */
793
794 static int
795 elf_open_debugfile_by_buildid (struct backtrace_state *state,
796 const char *buildid_data, size_t buildid_size,
797 backtrace_error_callback error_callback,
798 void *data)
799 {
800 const char * const prefix = SYSTEM_BUILD_ID_DIR;
801 const size_t prefix_len = strlen (prefix);
802 const char * const suffix = ".debug";
803 const size_t suffix_len = strlen (suffix);
804 size_t len;
805 char *bd_filename;
806 char *t;
807 size_t i;
808 int ret;
809 int does_not_exist;
810
811 len = prefix_len + buildid_size * 2 + suffix_len + 2;
812 bd_filename = backtrace_alloc (state, len, error_callback, data);
813 if (bd_filename == NULL)
814 return -1;
815
816 t = bd_filename;
817 memcpy (t, prefix, prefix_len);
818 t += prefix_len;
819 for (i = 0; i < buildid_size; i++)
820 {
821 unsigned char b;
822 unsigned char nib;
823
824 b = (unsigned char) buildid_data[i];
825 nib = (b & 0xf0) >> 4;
826 *t++ = nib < 10 ? '0' + nib : 'a' + nib - 10;
827 nib = b & 0x0f;
828 *t++ = nib < 10 ? '0' + nib : 'a' + nib - 10;
829 if (i == 0)
830 *t++ = '/';
831 }
832 memcpy (t, suffix, suffix_len);
833 t[suffix_len] = '\0';
834
835 ret = backtrace_open (bd_filename, error_callback, data, &does_not_exist);
836
837 backtrace_free (state, bd_filename, len, error_callback, data);
838
839 /* gdb checks that the debuginfo file has the same build ID note.
840 That seems kind of pointless to me--why would it have the right
841 name but not the right build ID?--so skipping the check. */
842
843 return ret;
844 }
845
846 /* Try to open a file whose name is PREFIX (length PREFIX_LEN)
847 concatenated with PREFIX2 (length PREFIX2_LEN) concatenated with
848 DEBUGLINK_NAME. Returns an open file descriptor, or -1. */
849
850 static int
851 elf_try_debugfile (struct backtrace_state *state, const char *prefix,
852 size_t prefix_len, const char *prefix2, size_t prefix2_len,
853 const char *debuglink_name,
854 backtrace_error_callback error_callback, void *data)
855 {
856 size_t debuglink_len;
857 size_t try_len;
858 char *try;
859 int does_not_exist;
860 int ret;
861
862 debuglink_len = strlen (debuglink_name);
863 try_len = prefix_len + prefix2_len + debuglink_len + 1;
864 try = backtrace_alloc (state, try_len, error_callback, data);
865 if (try == NULL)
866 return -1;
867
868 memcpy (try, prefix, prefix_len);
869 memcpy (try + prefix_len, prefix2, prefix2_len);
870 memcpy (try + prefix_len + prefix2_len, debuglink_name, debuglink_len);
871 try[prefix_len + prefix2_len + debuglink_len] = '\0';
872
873 ret = backtrace_open (try, error_callback, data, &does_not_exist);
874
875 backtrace_free (state, try, try_len, error_callback, data);
876
877 return ret;
878 }
879
880 /* Find a separate debug info file, using the debuglink section data
881 to find it. Returns an open file descriptor, or -1. */
882
883 static int
884 elf_find_debugfile_by_debuglink (struct backtrace_state *state,
885 const char *filename,
886 const char *debuglink_name,
887 backtrace_error_callback error_callback,
888 void *data)
889 {
890 int ret;
891 char *alc;
892 size_t alc_len;
893 const char *slash;
894 int ddescriptor;
895 const char *prefix;
896 size_t prefix_len;
897
898 /* Resolve symlinks in FILENAME. Since FILENAME is fairly likely to
899 be /proc/self/exe, symlinks are common. We don't try to resolve
900 the whole path name, just the base name. */
901 ret = -1;
902 alc = NULL;
903 alc_len = 0;
904 while (elf_is_symlink (filename))
905 {
906 char *new_buf;
907 size_t new_len;
908
909 new_buf = elf_readlink (state, filename, error_callback, data, &new_len);
910 if (new_buf == NULL)
911 break;
912
913 if (new_buf[0] == '/')
914 filename = new_buf;
915 else
916 {
917 slash = strrchr (filename, '/');
918 if (slash == NULL)
919 filename = new_buf;
920 else
921 {
922 size_t clen;
923 char *c;
924
925 slash++;
926 clen = slash - filename + strlen (new_buf) + 1;
927 c = backtrace_alloc (state, clen, error_callback, data);
928 if (c == NULL)
929 goto done;
930
931 memcpy (c, filename, slash - filename);
932 memcpy (c + (slash - filename), new_buf, strlen (new_buf));
933 c[slash - filename + strlen (new_buf)] = '\0';
934 backtrace_free (state, new_buf, new_len, error_callback, data);
935 filename = c;
936 new_buf = c;
937 new_len = clen;
938 }
939 }
940
941 if (alc != NULL)
942 backtrace_free (state, alc, alc_len, error_callback, data);
943 alc = new_buf;
944 alc_len = new_len;
945 }
946
947 /* Look for DEBUGLINK_NAME in the same directory as FILENAME. */
948
949 slash = strrchr (filename, '/');
950 if (slash == NULL)
951 {
952 prefix = "";
953 prefix_len = 0;
954 }
955 else
956 {
957 slash++;
958 prefix = filename;
959 prefix_len = slash - filename;
960 }
961
962 ddescriptor = elf_try_debugfile (state, prefix, prefix_len, "", 0,
963 debuglink_name, error_callback, data);
964 if (ddescriptor >= 0)
965 {
966 ret = ddescriptor;
967 goto done;
968 }
969
970 /* Look for DEBUGLINK_NAME in a .debug subdirectory of FILENAME. */
971
972 ddescriptor = elf_try_debugfile (state, prefix, prefix_len, ".debug/",
973 strlen (".debug/"), debuglink_name,
974 error_callback, data);
975 if (ddescriptor >= 0)
976 {
977 ret = ddescriptor;
978 goto done;
979 }
980
981 /* Look for DEBUGLINK_NAME in /usr/lib/debug. */
982
983 ddescriptor = elf_try_debugfile (state, "/usr/lib/debug/",
984 strlen ("/usr/lib/debug/"), prefix,
985 prefix_len, debuglink_name,
986 error_callback, data);
987 if (ddescriptor >= 0)
988 ret = ddescriptor;
989
990 done:
991 if (alc != NULL && alc_len > 0)
992 backtrace_free (state, alc, alc_len, error_callback, data);
993 return ret;
994 }
995
996 /* Open a separate debug info file, using the debuglink section data
997 to find it. Returns an open file descriptor, or -1. */
998
999 static int
1000 elf_open_debugfile_by_debuglink (struct backtrace_state *state,
1001 const char *filename,
1002 const char *debuglink_name,
1003 uint32_t debuglink_crc,
1004 backtrace_error_callback error_callback,
1005 void *data)
1006 {
1007 int ddescriptor;
1008
1009 ddescriptor = elf_find_debugfile_by_debuglink (state, filename,
1010 debuglink_name,
1011 error_callback, data);
1012 if (ddescriptor < 0)
1013 return -1;
1014
1015 if (debuglink_crc != 0)
1016 {
1017 uint32_t got_crc;
1018
1019 got_crc = elf_crc32_file (state, ddescriptor, error_callback, data);
1020 if (got_crc != debuglink_crc)
1021 {
1022 backtrace_close (ddescriptor, error_callback, data);
1023 return -1;
1024 }
1025 }
1026
1027 return ddescriptor;
1028 }
1029
1030 /* A function useful for setting a breakpoint for an inflation failure
1031 when this code is compiled with -g. */
1032
1033 static void
1034 elf_zlib_failed(void)
1035 {
1036 }
1037
1038 /* *PVAL is the current value being read from the stream, and *PBITS
1039 is the number of valid bits. Ensure that *PVAL holds at least 15
1040 bits by reading additional bits from *PPIN, up to PINEND, as
1041 needed. Updates *PPIN, *PVAL and *PBITS. Returns 1 on success, 0
1042 on error. */
1043
1044 static int
1045 elf_zlib_fetch (const unsigned char **ppin, const unsigned char *pinend,
1046 uint64_t *pval, unsigned int *pbits)
1047 {
1048 unsigned int bits;
1049 const unsigned char *pin;
1050 uint64_t val;
1051 uint32_t next;
1052
1053 bits = *pbits;
1054 if (bits >= 15)
1055 return 1;
1056 pin = *ppin;
1057 val = *pval;
1058
1059 if (unlikely (pinend - pin < 4))
1060 {
1061 elf_zlib_failed ();
1062 return 0;
1063 }
1064
1065 #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) \
1066 && defined(__ORDER_BIG_ENDIAN__) \
1067 && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ \
1068 || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
1069 /* We've ensured that PIN is aligned. */
1070 next = *(const uint32_t *)pin;
1071
1072 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1073 next = __builtin_bswap32 (next);
1074 #endif
1075 #else
1076 next = pin[0] | (pin[1] << 8) | (pin[2] << 16) | (pin[3] << 24);
1077 #endif
1078
1079 val |= (uint64_t)next << bits;
1080 bits += 32;
1081 pin += 4;
1082
1083 /* We will need the next four bytes soon. */
1084 __builtin_prefetch (pin, 0, 0);
1085
1086 *ppin = pin;
1087 *pval = val;
1088 *pbits = bits;
1089 return 1;
1090 }
1091
1092 /* Huffman code tables, like the rest of the zlib format, are defined
1093 by RFC 1951. We store a Huffman code table as a series of tables
1094 stored sequentially in memory. Each entry in a table is 16 bits.
1095 The first, main, table has 256 entries. It is followed by a set of
1096 secondary tables of length 2 to 128 entries. The maximum length of
1097 a code sequence in the deflate format is 15 bits, so that is all we
1098 need. Each secondary table has an index, which is the offset of
1099 the table in the overall memory storage.
1100
1101 The deflate format says that all codes of a given bit length are
1102 lexicographically consecutive. Perhaps we could have 130 values
1103 that require a 15-bit code, perhaps requiring three secondary
1104 tables of size 128. I don't know if this is actually possible, but
1105 it suggests that the maximum size required for secondary tables is
1106 3 * 128 + 3 * 64 ... == 768. The zlib enough program reports 660
1107 as the maximum. We permit 768, since in addition to the 256 for
1108 the primary table, with two bytes per entry, and with the two
1109 tables we need, that gives us a page.
1110
1111 A single table entry needs to store a value or (for the main table
1112 only) the index and size of a secondary table. Values range from 0
1113 to 285, inclusive. Secondary table indexes, per above, range from
1114 0 to 510. For a value we need to store the number of bits we need
1115 to determine that value (one value may appear multiple times in the
1116 table), which is 1 to 8. For a secondary table we need to store
1117 the number of bits used to index into the table, which is 1 to 7.
1118 And of course we need 1 bit to decide whether we have a value or a
1119 secondary table index. So each entry needs 9 bits for value/table
1120 index, 3 bits for size, 1 bit what it is. For simplicity we use 16
1121 bits per entry. */
1122
1123 /* Number of entries we allocate to for one code table. We get a page
1124 for the two code tables we need. */
1125
1126 #define HUFFMAN_TABLE_SIZE (1024)
1127
1128 /* Bit masks and shifts for the values in the table. */
1129
1130 #define HUFFMAN_VALUE_MASK 0x01ff
1131 #define HUFFMAN_BITS_SHIFT 9
1132 #define HUFFMAN_BITS_MASK 0x7
1133 #define HUFFMAN_SECONDARY_SHIFT 12
1134
1135 /* For working memory while inflating we need two code tables, we need
1136 an array of code lengths (max value 15, so we use unsigned char),
1137 and an array of unsigned shorts used while building a table. The
1138 latter two arrays must be large enough to hold the maximum number
1139 of code lengths, which RFC 1951 defines as 286 + 30. */
1140
1141 #define ZDEBUG_TABLE_SIZE \
1142 (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t) \
1143 + (286 + 30) * sizeof (uint16_t) \
1144 + (286 + 30) * sizeof (unsigned char))
1145
1146 #define ZDEBUG_TABLE_CODELEN_OFFSET \
1147 (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t) \
1148 + (286 + 30) * sizeof (uint16_t))
1149
1150 #define ZDEBUG_TABLE_WORK_OFFSET \
1151 (2 * HUFFMAN_TABLE_SIZE * sizeof (uint16_t))
1152
1153 #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
1154
1155 /* Used by the main function that generates the fixed table to learn
1156 the table size. */
1157 static size_t final_next_secondary;
1158
1159 #endif
1160
1161 /* Build a Huffman code table from an array of lengths in CODES of
1162 length CODES_LEN. The table is stored into *TABLE. ZDEBUG_TABLE
1163 is the same as for elf_zlib_inflate, used to find some work space.
1164 Returns 1 on success, 0 on error. */
1165
1166 static int
1167 elf_zlib_inflate_table (unsigned char *codes, size_t codes_len,
1168 uint16_t *zdebug_table, uint16_t *table)
1169 {
1170 uint16_t count[16];
1171 uint16_t start[16];
1172 uint16_t prev[16];
1173 uint16_t firstcode[7];
1174 uint16_t *next;
1175 size_t i;
1176 size_t j;
1177 unsigned int code;
1178 size_t next_secondary;
1179
1180 /* Count the number of code of each length. Set NEXT[val] to be the
1181 next value after VAL with the same bit length. */
1182
1183 next = (uint16_t *) (((unsigned char *) zdebug_table)
1184 + ZDEBUG_TABLE_WORK_OFFSET);
1185
1186 memset (&count[0], 0, 16 * sizeof (uint16_t));
1187 for (i = 0; i < codes_len; ++i)
1188 {
1189 if (unlikely (codes[i] >= 16))
1190 {
1191 elf_zlib_failed ();
1192 return 0;
1193 }
1194
1195 if (count[codes[i]] == 0)
1196 {
1197 start[codes[i]] = i;
1198 prev[codes[i]] = i;
1199 }
1200 else
1201 {
1202 next[prev[codes[i]]] = i;
1203 prev[codes[i]] = i;
1204 }
1205
1206 ++count[codes[i]];
1207 }
1208
1209 /* For each length, fill in the table for the codes of that
1210 length. */
1211
1212 memset (table, 0, HUFFMAN_TABLE_SIZE * sizeof (uint16_t));
1213
1214 /* Handle the values that do not require a secondary table. */
1215
1216 code = 0;
1217 for (j = 1; j <= 8; ++j)
1218 {
1219 unsigned int jcnt;
1220 unsigned int val;
1221
1222 jcnt = count[j];
1223 if (jcnt == 0)
1224 continue;
1225
1226 if (unlikely (jcnt > (1U << j)))
1227 {
1228 elf_zlib_failed ();
1229 return 0;
1230 }
1231
1232 /* There are JCNT values that have this length, the values
1233 starting from START[j] continuing through NEXT[VAL]. Those
1234 values are assigned consecutive values starting at CODE. */
1235
1236 val = start[j];
1237 for (i = 0; i < jcnt; ++i)
1238 {
1239 uint16_t tval;
1240 size_t ind;
1241 unsigned int incr;
1242
1243 /* In the compressed bit stream, the value VAL is encoded as
1244 J bits with the value C. */
1245
1246 if (unlikely ((val & ~HUFFMAN_VALUE_MASK) != 0))
1247 {
1248 elf_zlib_failed ();
1249 return 0;
1250 }
1251
1252 tval = val | ((j - 1) << HUFFMAN_BITS_SHIFT);
1253
1254 /* The table lookup uses 8 bits. If J is less than 8, we
1255 don't know what the other bits will be. We need to fill
1256 in all possibilities in the table. Since the Huffman
1257 code is unambiguous, those entries can't be used for any
1258 other code. */
1259
1260 for (ind = code; ind < 0x100; ind += 1 << j)
1261 {
1262 if (unlikely (table[ind] != 0))
1263 {
1264 elf_zlib_failed ();
1265 return 0;
1266 }
1267 table[ind] = tval;
1268 }
1269
1270 /* Advance to the next value with this length. */
1271 if (i + 1 < jcnt)
1272 val = next[val];
1273
1274 /* The Huffman codes are stored in the bitstream with the
1275 most significant bit first, as is required to make them
1276 unambiguous. The effect is that when we read them from
1277 the bitstream we see the bit sequence in reverse order:
1278 the most significant bit of the Huffman code is the least
1279 significant bit of the value we read from the bitstream.
1280 That means that to make our table lookups work, we need
1281 to reverse the bits of CODE. Since reversing bits is
1282 tedious and in general requires using a table, we instead
1283 increment CODE in reverse order. That is, if the number
1284 of bits we are currently using, here named J, is 3, we
1285 count as 000, 100, 010, 110, 001, 101, 011, 111, which is
1286 to say the numbers from 0 to 7 but with the bits
1287 reversed. Going to more bits, aka incrementing J,
1288 effectively just adds more zero bits as the beginning,
1289 and as such does not change the numeric value of CODE.
1290
1291 To increment CODE of length J in reverse order, find the
1292 most significant zero bit and set it to one while
1293 clearing all higher bits. In other words, add 1 modulo
1294 2^J, only reversed. */
1295
1296 incr = 1U << (j - 1);
1297 while ((code & incr) != 0)
1298 incr >>= 1;
1299 if (incr == 0)
1300 code = 0;
1301 else
1302 {
1303 code &= incr - 1;
1304 code += incr;
1305 }
1306 }
1307 }
1308
1309 /* Handle the values that require a secondary table. */
1310
1311 /* Set FIRSTCODE, the number at which the codes start, for each
1312 length. */
1313
1314 for (j = 9; j < 16; j++)
1315 {
1316 unsigned int jcnt;
1317 unsigned int k;
1318
1319 jcnt = count[j];
1320 if (jcnt == 0)
1321 continue;
1322
1323 /* There are JCNT values that have this length, the values
1324 starting from START[j]. Those values are assigned
1325 consecutive values starting at CODE. */
1326
1327 firstcode[j - 9] = code;
1328
1329 /* Reverse add JCNT to CODE modulo 2^J. */
1330 for (k = 0; k < j; ++k)
1331 {
1332 if ((jcnt & (1U << k)) != 0)
1333 {
1334 unsigned int m;
1335 unsigned int bit;
1336
1337 bit = 1U << (j - k - 1);
1338 for (m = 0; m < j - k; ++m, bit >>= 1)
1339 {
1340 if ((code & bit) == 0)
1341 {
1342 code += bit;
1343 break;
1344 }
1345 code &= ~bit;
1346 }
1347 jcnt &= ~(1U << k);
1348 }
1349 }
1350 if (unlikely (jcnt != 0))
1351 {
1352 elf_zlib_failed ();
1353 return 0;
1354 }
1355 }
1356
1357 /* For J from 9 to 15, inclusive, we store COUNT[J] consecutive
1358 values starting at START[J] with consecutive codes starting at
1359 FIRSTCODE[J - 9]. In the primary table we need to point to the
1360 secondary table, and the secondary table will be indexed by J - 9
1361 bits. We count down from 15 so that we install the larger
1362 secondary tables first, as the smaller ones may be embedded in
1363 the larger ones. */
1364
1365 next_secondary = 0; /* Index of next secondary table (after primary). */
1366 for (j = 15; j >= 9; j--)
1367 {
1368 unsigned int jcnt;
1369 unsigned int val;
1370 size_t primary; /* Current primary index. */
1371 size_t secondary; /* Offset to current secondary table. */
1372 size_t secondary_bits; /* Bit size of current secondary table. */
1373
1374 jcnt = count[j];
1375 if (jcnt == 0)
1376 continue;
1377
1378 val = start[j];
1379 code = firstcode[j - 9];
1380 primary = 0x100;
1381 secondary = 0;
1382 secondary_bits = 0;
1383 for (i = 0; i < jcnt; ++i)
1384 {
1385 uint16_t tval;
1386 size_t ind;
1387 unsigned int incr;
1388
1389 if ((code & 0xff) != primary)
1390 {
1391 uint16_t tprimary;
1392
1393 /* Fill in a new primary table entry. */
1394
1395 primary = code & 0xff;
1396
1397 tprimary = table[primary];
1398 if (tprimary == 0)
1399 {
1400 /* Start a new secondary table. */
1401
1402 if (unlikely ((next_secondary & HUFFMAN_VALUE_MASK)
1403 != next_secondary))
1404 {
1405 elf_zlib_failed ();
1406 return 0;
1407 }
1408
1409 secondary = next_secondary;
1410 secondary_bits = j - 8;
1411 next_secondary += 1 << secondary_bits;
1412 table[primary] = (secondary
1413 + ((j - 8) << HUFFMAN_BITS_SHIFT)
1414 + (1U << HUFFMAN_SECONDARY_SHIFT));
1415 }
1416 else
1417 {
1418 /* There is an existing entry. It had better be a
1419 secondary table with enough bits. */
1420 if (unlikely ((tprimary & (1U << HUFFMAN_SECONDARY_SHIFT))
1421 == 0))
1422 {
1423 elf_zlib_failed ();
1424 return 0;
1425 }
1426 secondary = tprimary & HUFFMAN_VALUE_MASK;
1427 secondary_bits = ((tprimary >> HUFFMAN_BITS_SHIFT)
1428 & HUFFMAN_BITS_MASK);
1429 if (unlikely (secondary_bits < j - 8))
1430 {
1431 elf_zlib_failed ();
1432 return 0;
1433 }
1434 }
1435 }
1436
1437 /* Fill in secondary table entries. */
1438
1439 tval = val | ((j - 8) << HUFFMAN_BITS_SHIFT);
1440
1441 for (ind = code >> 8;
1442 ind < (1U << secondary_bits);
1443 ind += 1U << (j - 8))
1444 {
1445 if (unlikely (table[secondary + 0x100 + ind] != 0))
1446 {
1447 elf_zlib_failed ();
1448 return 0;
1449 }
1450 table[secondary + 0x100 + ind] = tval;
1451 }
1452
1453 if (i + 1 < jcnt)
1454 val = next[val];
1455
1456 incr = 1U << (j - 1);
1457 while ((code & incr) != 0)
1458 incr >>= 1;
1459 if (incr == 0)
1460 code = 0;
1461 else
1462 {
1463 code &= incr - 1;
1464 code += incr;
1465 }
1466 }
1467 }
1468
1469 #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
1470 final_next_secondary = next_secondary;
1471 #endif
1472
1473 return 1;
1474 }
1475
1476 #ifdef BACKTRACE_GENERATE_FIXED_HUFFMAN_TABLE
1477
1478 /* Used to generate the fixed Huffman table for block type 1. */
1479
1480 #include <stdio.h>
1481
1482 static uint16_t table[ZDEBUG_TABLE_SIZE];
1483 static unsigned char codes[288];
1484
1485 int
1486 main ()
1487 {
1488 size_t i;
1489
1490 for (i = 0; i <= 143; ++i)
1491 codes[i] = 8;
1492 for (i = 144; i <= 255; ++i)
1493 codes[i] = 9;
1494 for (i = 256; i <= 279; ++i)
1495 codes[i] = 7;
1496 for (i = 280; i <= 287; ++i)
1497 codes[i] = 8;
1498 if (!elf_zlib_inflate_table (&codes[0], 288, &table[0], &table[0]))
1499 {
1500 fprintf (stderr, "elf_zlib_inflate_table failed\n");
1501 exit (EXIT_FAILURE);
1502 }
1503
1504 printf ("static const uint16_t elf_zlib_default_table[%#zx] =\n",
1505 final_next_secondary + 0x100);
1506 printf ("{\n");
1507 for (i = 0; i < final_next_secondary + 0x100; i += 8)
1508 {
1509 size_t j;
1510
1511 printf (" ");
1512 for (j = i; j < final_next_secondary + 0x100 && j < i + 8; ++j)
1513 printf (" %#x,", table[j]);
1514 printf ("\n");
1515 }
1516 printf ("};\n");
1517 printf ("\n");
1518
1519 for (i = 0; i < 32; ++i)
1520 codes[i] = 5;
1521 if (!elf_zlib_inflate_table (&codes[0], 32, &table[0], &table[0]))
1522 {
1523 fprintf (stderr, "elf_zlib_inflate_table failed\n");
1524 exit (EXIT_FAILURE);
1525 }
1526
1527 printf ("static const uint16_t elf_zlib_default_dist_table[%#zx] =\n",
1528 final_next_secondary + 0x100);
1529 printf ("{\n");
1530 for (i = 0; i < final_next_secondary + 0x100; i += 8)
1531 {
1532 size_t j;
1533
1534 printf (" ");
1535 for (j = i; j < final_next_secondary + 0x100 && j < i + 8; ++j)
1536 printf (" %#x,", table[j]);
1537 printf ("\n");
1538 }
1539 printf ("};\n");
1540
1541 return 0;
1542 }
1543
1544 #endif
1545
1546 /* The fixed tables generated by the #ifdef'ed out main function
1547 above. */
1548
1549 static const uint16_t elf_zlib_default_table[0x170] =
1550 {
1551 0xd00, 0xe50, 0xe10, 0xf18, 0xd10, 0xe70, 0xe30, 0x1230,
1552 0xd08, 0xe60, 0xe20, 0x1210, 0xe00, 0xe80, 0xe40, 0x1250,
1553 0xd04, 0xe58, 0xe18, 0x1200, 0xd14, 0xe78, 0xe38, 0x1240,
1554 0xd0c, 0xe68, 0xe28, 0x1220, 0xe08, 0xe88, 0xe48, 0x1260,
1555 0xd02, 0xe54, 0xe14, 0xf1c, 0xd12, 0xe74, 0xe34, 0x1238,
1556 0xd0a, 0xe64, 0xe24, 0x1218, 0xe04, 0xe84, 0xe44, 0x1258,
1557 0xd06, 0xe5c, 0xe1c, 0x1208, 0xd16, 0xe7c, 0xe3c, 0x1248,
1558 0xd0e, 0xe6c, 0xe2c, 0x1228, 0xe0c, 0xe8c, 0xe4c, 0x1268,
1559 0xd01, 0xe52, 0xe12, 0xf1a, 0xd11, 0xe72, 0xe32, 0x1234,
1560 0xd09, 0xe62, 0xe22, 0x1214, 0xe02, 0xe82, 0xe42, 0x1254,
1561 0xd05, 0xe5a, 0xe1a, 0x1204, 0xd15, 0xe7a, 0xe3a, 0x1244,
1562 0xd0d, 0xe6a, 0xe2a, 0x1224, 0xe0a, 0xe8a, 0xe4a, 0x1264,
1563 0xd03, 0xe56, 0xe16, 0xf1e, 0xd13, 0xe76, 0xe36, 0x123c,
1564 0xd0b, 0xe66, 0xe26, 0x121c, 0xe06, 0xe86, 0xe46, 0x125c,
1565 0xd07, 0xe5e, 0xe1e, 0x120c, 0xd17, 0xe7e, 0xe3e, 0x124c,
1566 0xd0f, 0xe6e, 0xe2e, 0x122c, 0xe0e, 0xe8e, 0xe4e, 0x126c,
1567 0xd00, 0xe51, 0xe11, 0xf19, 0xd10, 0xe71, 0xe31, 0x1232,
1568 0xd08, 0xe61, 0xe21, 0x1212, 0xe01, 0xe81, 0xe41, 0x1252,
1569 0xd04, 0xe59, 0xe19, 0x1202, 0xd14, 0xe79, 0xe39, 0x1242,
1570 0xd0c, 0xe69, 0xe29, 0x1222, 0xe09, 0xe89, 0xe49, 0x1262,
1571 0xd02, 0xe55, 0xe15, 0xf1d, 0xd12, 0xe75, 0xe35, 0x123a,
1572 0xd0a, 0xe65, 0xe25, 0x121a, 0xe05, 0xe85, 0xe45, 0x125a,
1573 0xd06, 0xe5d, 0xe1d, 0x120a, 0xd16, 0xe7d, 0xe3d, 0x124a,
1574 0xd0e, 0xe6d, 0xe2d, 0x122a, 0xe0d, 0xe8d, 0xe4d, 0x126a,
1575 0xd01, 0xe53, 0xe13, 0xf1b, 0xd11, 0xe73, 0xe33, 0x1236,
1576 0xd09, 0xe63, 0xe23, 0x1216, 0xe03, 0xe83, 0xe43, 0x1256,
1577 0xd05, 0xe5b, 0xe1b, 0x1206, 0xd15, 0xe7b, 0xe3b, 0x1246,
1578 0xd0d, 0xe6b, 0xe2b, 0x1226, 0xe0b, 0xe8b, 0xe4b, 0x1266,
1579 0xd03, 0xe57, 0xe17, 0xf1f, 0xd13, 0xe77, 0xe37, 0x123e,
1580 0xd0b, 0xe67, 0xe27, 0x121e, 0xe07, 0xe87, 0xe47, 0x125e,
1581 0xd07, 0xe5f, 0xe1f, 0x120e, 0xd17, 0xe7f, 0xe3f, 0x124e,
1582 0xd0f, 0xe6f, 0xe2f, 0x122e, 0xe0f, 0xe8f, 0xe4f, 0x126e,
1583 0x290, 0x291, 0x292, 0x293, 0x294, 0x295, 0x296, 0x297,
1584 0x298, 0x299, 0x29a, 0x29b, 0x29c, 0x29d, 0x29e, 0x29f,
1585 0x2a0, 0x2a1, 0x2a2, 0x2a3, 0x2a4, 0x2a5, 0x2a6, 0x2a7,
1586 0x2a8, 0x2a9, 0x2aa, 0x2ab, 0x2ac, 0x2ad, 0x2ae, 0x2af,
1587 0x2b0, 0x2b1, 0x2b2, 0x2b3, 0x2b4, 0x2b5, 0x2b6, 0x2b7,
1588 0x2b8, 0x2b9, 0x2ba, 0x2bb, 0x2bc, 0x2bd, 0x2be, 0x2bf,
1589 0x2c0, 0x2c1, 0x2c2, 0x2c3, 0x2c4, 0x2c5, 0x2c6, 0x2c7,
1590 0x2c8, 0x2c9, 0x2ca, 0x2cb, 0x2cc, 0x2cd, 0x2ce, 0x2cf,
1591 0x2d0, 0x2d1, 0x2d2, 0x2d3, 0x2d4, 0x2d5, 0x2d6, 0x2d7,
1592 0x2d8, 0x2d9, 0x2da, 0x2db, 0x2dc, 0x2dd, 0x2de, 0x2df,
1593 0x2e0, 0x2e1, 0x2e2, 0x2e3, 0x2e4, 0x2e5, 0x2e6, 0x2e7,
1594 0x2e8, 0x2e9, 0x2ea, 0x2eb, 0x2ec, 0x2ed, 0x2ee, 0x2ef,
1595 0x2f0, 0x2f1, 0x2f2, 0x2f3, 0x2f4, 0x2f5, 0x2f6, 0x2f7,
1596 0x2f8, 0x2f9, 0x2fa, 0x2fb, 0x2fc, 0x2fd, 0x2fe, 0x2ff,
1597 };
1598
1599 static const uint16_t elf_zlib_default_dist_table[0x100] =
1600 {
1601 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1602 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1603 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1604 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1605 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1606 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1607 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1608 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1609 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1610 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1611 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1612 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1613 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1614 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1615 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1616 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1617 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1618 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1619 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1620 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1621 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1622 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1623 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1624 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1625 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1626 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1627 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1628 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1629 0x800, 0x810, 0x808, 0x818, 0x804, 0x814, 0x80c, 0x81c,
1630 0x802, 0x812, 0x80a, 0x81a, 0x806, 0x816, 0x80e, 0x81e,
1631 0x801, 0x811, 0x809, 0x819, 0x805, 0x815, 0x80d, 0x81d,
1632 0x803, 0x813, 0x80b, 0x81b, 0x807, 0x817, 0x80f, 0x81f,
1633 };
1634
1635 /* Inflate a zlib stream from PIN/SIN to POUT/SOUT. Return 1 on
1636 success, 0 on some error parsing the stream. */
1637
1638 static int
1639 elf_zlib_inflate (const unsigned char *pin, size_t sin, uint16_t *zdebug_table,
1640 unsigned char *pout, size_t sout)
1641 {
1642 unsigned char *porigout;
1643 const unsigned char *pinend;
1644 unsigned char *poutend;
1645
1646 /* We can apparently see multiple zlib streams concatenated
1647 together, so keep going as long as there is something to read.
1648 The last 4 bytes are the checksum. */
1649 porigout = pout;
1650 pinend = pin + sin;
1651 poutend = pout + sout;
1652 while ((pinend - pin) > 4)
1653 {
1654 uint64_t val;
1655 unsigned int bits;
1656 int last;
1657
1658 /* Read the two byte zlib header. */
1659
1660 if (unlikely ((pin[0] & 0xf) != 8)) /* 8 is zlib encoding. */
1661 {
1662 /* Unknown compression method. */
1663 elf_zlib_failed ();
1664 return 0;
1665 }
1666 if (unlikely ((pin[0] >> 4) > 7))
1667 {
1668 /* Window size too large. Other than this check, we don't
1669 care about the window size. */
1670 elf_zlib_failed ();
1671 return 0;
1672 }
1673 if (unlikely ((pin[1] & 0x20) != 0))
1674 {
1675 /* Stream expects a predefined dictionary, but we have no
1676 dictionary. */
1677 elf_zlib_failed ();
1678 return 0;
1679 }
1680 val = (pin[0] << 8) | pin[1];
1681 if (unlikely (val % 31 != 0))
1682 {
1683 /* Header check failure. */
1684 elf_zlib_failed ();
1685 return 0;
1686 }
1687 pin += 2;
1688
1689 /* Align PIN to a 32-bit boundary. */
1690
1691 val = 0;
1692 bits = 0;
1693 while ((((uintptr_t) pin) & 3) != 0)
1694 {
1695 val |= (uint64_t)*pin << bits;
1696 bits += 8;
1697 ++pin;
1698 }
1699
1700 /* Read blocks until one is marked last. */
1701
1702 last = 0;
1703
1704 while (!last)
1705 {
1706 unsigned int type;
1707 const uint16_t *tlit;
1708 const uint16_t *tdist;
1709
1710 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1711 return 0;
1712
1713 last = val & 1;
1714 type = (val >> 1) & 3;
1715 val >>= 3;
1716 bits -= 3;
1717
1718 if (unlikely (type == 3))
1719 {
1720 /* Invalid block type. */
1721 elf_zlib_failed ();
1722 return 0;
1723 }
1724
1725 if (type == 0)
1726 {
1727 uint16_t len;
1728 uint16_t lenc;
1729
1730 /* An uncompressed block. */
1731
1732 /* If we've read ahead more than a byte, back up. */
1733 while (bits > 8)
1734 {
1735 --pin;
1736 bits -= 8;
1737 }
1738
1739 val = 0;
1740 bits = 0;
1741 if (unlikely ((pinend - pin) < 4))
1742 {
1743 /* Missing length. */
1744 elf_zlib_failed ();
1745 return 0;
1746 }
1747 len = pin[0] | (pin[1] << 8);
1748 lenc = pin[2] | (pin[3] << 8);
1749 pin += 4;
1750 lenc = ~lenc;
1751 if (unlikely (len != lenc))
1752 {
1753 /* Corrupt data. */
1754 elf_zlib_failed ();
1755 return 0;
1756 }
1757 if (unlikely (len > (unsigned int) (pinend - pin)
1758 || len > (unsigned int) (poutend - pout)))
1759 {
1760 /* Not enough space in buffers. */
1761 elf_zlib_failed ();
1762 return 0;
1763 }
1764 memcpy (pout, pin, len);
1765 pout += len;
1766 pin += len;
1767
1768 /* Align PIN. */
1769 while ((((uintptr_t) pin) & 3) != 0)
1770 {
1771 val |= (uint64_t)*pin << bits;
1772 bits += 8;
1773 ++pin;
1774 }
1775
1776 /* Go around to read the next block. */
1777 continue;
1778 }
1779
1780 if (type == 1)
1781 {
1782 tlit = elf_zlib_default_table;
1783 tdist = elf_zlib_default_dist_table;
1784 }
1785 else
1786 {
1787 unsigned int nlit;
1788 unsigned int ndist;
1789 unsigned int nclen;
1790 unsigned char codebits[19];
1791 unsigned char *plenbase;
1792 unsigned char *plen;
1793 unsigned char *plenend;
1794
1795 /* Read a Huffman encoding table. The various magic
1796 numbers here are from RFC 1951. */
1797
1798 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1799 return 0;
1800
1801 nlit = (val & 0x1f) + 257;
1802 val >>= 5;
1803 ndist = (val & 0x1f) + 1;
1804 val >>= 5;
1805 nclen = (val & 0xf) + 4;
1806 val >>= 4;
1807 bits -= 14;
1808 if (unlikely (nlit > 286 || ndist > 30))
1809 {
1810 /* Values out of range. */
1811 elf_zlib_failed ();
1812 return 0;
1813 }
1814
1815 /* Read and build the table used to compress the
1816 literal, length, and distance codes. */
1817
1818 memset(&codebits[0], 0, 19);
1819
1820 /* There are always at least 4 elements in the
1821 table. */
1822
1823 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1824 return 0;
1825
1826 codebits[16] = val & 7;
1827 codebits[17] = (val >> 3) & 7;
1828 codebits[18] = (val >> 6) & 7;
1829 codebits[0] = (val >> 9) & 7;
1830 val >>= 12;
1831 bits -= 12;
1832
1833 if (nclen == 4)
1834 goto codebitsdone;
1835
1836 codebits[8] = val & 7;
1837 val >>= 3;
1838 bits -= 3;
1839
1840 if (nclen == 5)
1841 goto codebitsdone;
1842
1843 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1844 return 0;
1845
1846 codebits[7] = val & 7;
1847 val >>= 3;
1848 bits -= 3;
1849
1850 if (nclen == 6)
1851 goto codebitsdone;
1852
1853 codebits[9] = val & 7;
1854 val >>= 3;
1855 bits -= 3;
1856
1857 if (nclen == 7)
1858 goto codebitsdone;
1859
1860 codebits[6] = val & 7;
1861 val >>= 3;
1862 bits -= 3;
1863
1864 if (nclen == 8)
1865 goto codebitsdone;
1866
1867 codebits[10] = val & 7;
1868 val >>= 3;
1869 bits -= 3;
1870
1871 if (nclen == 9)
1872 goto codebitsdone;
1873
1874 codebits[5] = val & 7;
1875 val >>= 3;
1876 bits -= 3;
1877
1878 if (nclen == 10)
1879 goto codebitsdone;
1880
1881 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1882 return 0;
1883
1884 codebits[11] = val & 7;
1885 val >>= 3;
1886 bits -= 3;
1887
1888 if (nclen == 11)
1889 goto codebitsdone;
1890
1891 codebits[4] = val & 7;
1892 val >>= 3;
1893 bits -= 3;
1894
1895 if (nclen == 12)
1896 goto codebitsdone;
1897
1898 codebits[12] = val & 7;
1899 val >>= 3;
1900 bits -= 3;
1901
1902 if (nclen == 13)
1903 goto codebitsdone;
1904
1905 codebits[3] = val & 7;
1906 val >>= 3;
1907 bits -= 3;
1908
1909 if (nclen == 14)
1910 goto codebitsdone;
1911
1912 codebits[13] = val & 7;
1913 val >>= 3;
1914 bits -= 3;
1915
1916 if (nclen == 15)
1917 goto codebitsdone;
1918
1919 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1920 return 0;
1921
1922 codebits[2] = val & 7;
1923 val >>= 3;
1924 bits -= 3;
1925
1926 if (nclen == 16)
1927 goto codebitsdone;
1928
1929 codebits[14] = val & 7;
1930 val >>= 3;
1931 bits -= 3;
1932
1933 if (nclen == 17)
1934 goto codebitsdone;
1935
1936 codebits[1] = val & 7;
1937 val >>= 3;
1938 bits -= 3;
1939
1940 if (nclen == 18)
1941 goto codebitsdone;
1942
1943 codebits[15] = val & 7;
1944 val >>= 3;
1945 bits -= 3;
1946
1947 codebitsdone:
1948
1949 if (!elf_zlib_inflate_table (codebits, 19, zdebug_table,
1950 zdebug_table))
1951 return 0;
1952
1953 /* Read the compressed bit lengths of the literal,
1954 length, and distance codes. We have allocated space
1955 at the end of zdebug_table to hold them. */
1956
1957 plenbase = (((unsigned char *) zdebug_table)
1958 + ZDEBUG_TABLE_CODELEN_OFFSET);
1959 plen = plenbase;
1960 plenend = plen + nlit + ndist;
1961 while (plen < plenend)
1962 {
1963 uint16_t t;
1964 unsigned int b;
1965 uint16_t v;
1966
1967 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
1968 return 0;
1969
1970 t = zdebug_table[val & 0xff];
1971
1972 /* The compression here uses bit lengths up to 7, so
1973 a secondary table is never necessary. */
1974 if (unlikely ((t & (1U << HUFFMAN_SECONDARY_SHIFT)) != 0))
1975 {
1976 elf_zlib_failed ();
1977 return 0;
1978 }
1979
1980 b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
1981 val >>= b + 1;
1982 bits -= b + 1;
1983
1984 v = t & HUFFMAN_VALUE_MASK;
1985 if (v < 16)
1986 *plen++ = v;
1987 else if (v == 16)
1988 {
1989 unsigned int c;
1990 unsigned int prev;
1991
1992 /* Copy previous entry 3 to 6 times. */
1993
1994 if (unlikely (plen == plenbase))
1995 {
1996 elf_zlib_failed ();
1997 return 0;
1998 }
1999
2000 /* We used up to 7 bits since the last
2001 elf_zlib_fetch, so we have at least 8 bits
2002 available here. */
2003
2004 c = 3 + (val & 0x3);
2005 val >>= 2;
2006 bits -= 2;
2007 if (unlikely ((unsigned int) (plenend - plen) < c))
2008 {
2009 elf_zlib_failed ();
2010 return 0;
2011 }
2012
2013 prev = plen[-1];
2014 switch (c)
2015 {
2016 case 6:
2017 *plen++ = prev;
2018 /* fallthrough */
2019 case 5:
2020 *plen++ = prev;
2021 /* fallthrough */
2022 case 4:
2023 *plen++ = prev;
2024 }
2025 *plen++ = prev;
2026 *plen++ = prev;
2027 *plen++ = prev;
2028 }
2029 else if (v == 17)
2030 {
2031 unsigned int c;
2032
2033 /* Store zero 3 to 10 times. */
2034
2035 /* We used up to 7 bits since the last
2036 elf_zlib_fetch, so we have at least 8 bits
2037 available here. */
2038
2039 c = 3 + (val & 0x7);
2040 val >>= 3;
2041 bits -= 3;
2042 if (unlikely ((unsigned int) (plenend - plen) < c))
2043 {
2044 elf_zlib_failed ();
2045 return 0;
2046 }
2047
2048 switch (c)
2049 {
2050 case 10:
2051 *plen++ = 0;
2052 /* fallthrough */
2053 case 9:
2054 *plen++ = 0;
2055 /* fallthrough */
2056 case 8:
2057 *plen++ = 0;
2058 /* fallthrough */
2059 case 7:
2060 *plen++ = 0;
2061 /* fallthrough */
2062 case 6:
2063 *plen++ = 0;
2064 /* fallthrough */
2065 case 5:
2066 *plen++ = 0;
2067 /* fallthrough */
2068 case 4:
2069 *plen++ = 0;
2070 }
2071 *plen++ = 0;
2072 *plen++ = 0;
2073 *plen++ = 0;
2074 }
2075 else if (v == 18)
2076 {
2077 unsigned int c;
2078
2079 /* Store zero 11 to 138 times. */
2080
2081 /* We used up to 7 bits since the last
2082 elf_zlib_fetch, so we have at least 8 bits
2083 available here. */
2084
2085 c = 11 + (val & 0x7f);
2086 val >>= 7;
2087 bits -= 7;
2088 if (unlikely ((unsigned int) (plenend - plen) < c))
2089 {
2090 elf_zlib_failed ();
2091 return 0;
2092 }
2093
2094 memset (plen, 0, c);
2095 plen += c;
2096 }
2097 else
2098 {
2099 elf_zlib_failed ();
2100 return 0;
2101 }
2102 }
2103
2104 /* Make sure that the stop code can appear. */
2105
2106 plen = plenbase;
2107 if (unlikely (plen[256] == 0))
2108 {
2109 elf_zlib_failed ();
2110 return 0;
2111 }
2112
2113 /* Build the decompression tables. */
2114
2115 if (!elf_zlib_inflate_table (plen, nlit, zdebug_table,
2116 zdebug_table))
2117 return 0;
2118 if (!elf_zlib_inflate_table (plen + nlit, ndist, zdebug_table,
2119 zdebug_table + HUFFMAN_TABLE_SIZE))
2120 return 0;
2121 tlit = zdebug_table;
2122 tdist = zdebug_table + HUFFMAN_TABLE_SIZE;
2123 }
2124
2125 /* Inflate values until the end of the block. This is the
2126 main loop of the inflation code. */
2127
2128 while (1)
2129 {
2130 uint16_t t;
2131 unsigned int b;
2132 uint16_t v;
2133 unsigned int lit;
2134
2135 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
2136 return 0;
2137
2138 t = tlit[val & 0xff];
2139 b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
2140 v = t & HUFFMAN_VALUE_MASK;
2141
2142 if ((t & (1U << HUFFMAN_SECONDARY_SHIFT)) == 0)
2143 {
2144 lit = v;
2145 val >>= b + 1;
2146 bits -= b + 1;
2147 }
2148 else
2149 {
2150 t = tlit[v + 0x100 + ((val >> 8) & ((1U << b) - 1))];
2151 b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
2152 lit = t & HUFFMAN_VALUE_MASK;
2153 val >>= b + 8;
2154 bits -= b + 8;
2155 }
2156
2157 if (lit < 256)
2158 {
2159 if (unlikely (pout == poutend))
2160 {
2161 elf_zlib_failed ();
2162 return 0;
2163 }
2164
2165 *pout++ = lit;
2166
2167 /* We will need to write the next byte soon. We ask
2168 for high temporal locality because we will write
2169 to the whole cache line soon. */
2170 __builtin_prefetch (pout, 1, 3);
2171 }
2172 else if (lit == 256)
2173 {
2174 /* The end of the block. */
2175 break;
2176 }
2177 else
2178 {
2179 unsigned int dist;
2180 unsigned int len;
2181
2182 /* Convert lit into a length. */
2183
2184 if (lit < 265)
2185 len = lit - 257 + 3;
2186 else if (lit == 285)
2187 len = 258;
2188 else if (unlikely (lit > 285))
2189 {
2190 elf_zlib_failed ();
2191 return 0;
2192 }
2193 else
2194 {
2195 unsigned int extra;
2196
2197 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
2198 return 0;
2199
2200 /* This is an expression for the table of length
2201 codes in RFC 1951 3.2.5. */
2202 lit -= 265;
2203 extra = (lit >> 2) + 1;
2204 len = (lit & 3) << extra;
2205 len += 11;
2206 len += ((1U << (extra - 1)) - 1) << 3;
2207 len += val & ((1U << extra) - 1);
2208 val >>= extra;
2209 bits -= extra;
2210 }
2211
2212 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
2213 return 0;
2214
2215 t = tdist[val & 0xff];
2216 b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
2217 v = t & HUFFMAN_VALUE_MASK;
2218
2219 if ((t & (1U << HUFFMAN_SECONDARY_SHIFT)) == 0)
2220 {
2221 dist = v;
2222 val >>= b + 1;
2223 bits -= b + 1;
2224 }
2225 else
2226 {
2227 t = tdist[v + 0x100 + ((val >> 8) & ((1U << b) - 1))];
2228 b = (t >> HUFFMAN_BITS_SHIFT) & HUFFMAN_BITS_MASK;
2229 dist = t & HUFFMAN_VALUE_MASK;
2230 val >>= b + 8;
2231 bits -= b + 8;
2232 }
2233
2234 /* Convert dist to a distance. */
2235
2236 if (dist == 0)
2237 {
2238 /* A distance of 1. A common case, meaning
2239 repeat the last character LEN times. */
2240
2241 if (unlikely (pout == porigout))
2242 {
2243 elf_zlib_failed ();
2244 return 0;
2245 }
2246
2247 if (unlikely ((unsigned int) (poutend - pout) < len))
2248 {
2249 elf_zlib_failed ();
2250 return 0;
2251 }
2252
2253 memset (pout, pout[-1], len);
2254 pout += len;
2255 }
2256 else if (unlikely (dist > 29))
2257 {
2258 elf_zlib_failed ();
2259 return 0;
2260 }
2261 else
2262 {
2263 if (dist < 4)
2264 dist = dist + 1;
2265 else
2266 {
2267 unsigned int extra;
2268
2269 if (!elf_zlib_fetch (&pin, pinend, &val, &bits))
2270 return 0;
2271
2272 /* This is an expression for the table of
2273 distance codes in RFC 1951 3.2.5. */
2274 dist -= 4;
2275 extra = (dist >> 1) + 1;
2276 dist = (dist & 1) << extra;
2277 dist += 5;
2278 dist += ((1U << (extra - 1)) - 1) << 2;
2279 dist += val & ((1U << extra) - 1);
2280 val >>= extra;
2281 bits -= extra;
2282 }
2283
2284 /* Go back dist bytes, and copy len bytes from
2285 there. */
2286
2287 if (unlikely ((unsigned int) (pout - porigout) < dist))
2288 {
2289 elf_zlib_failed ();
2290 return 0;
2291 }
2292
2293 if (unlikely ((unsigned int) (poutend - pout) < len))
2294 {
2295 elf_zlib_failed ();
2296 return 0;
2297 }
2298
2299 if (dist >= len)
2300 {
2301 memcpy (pout, pout - dist, len);
2302 pout += len;
2303 }
2304 else
2305 {
2306 while (len > 0)
2307 {
2308 unsigned int copy;
2309
2310 copy = len < dist ? len : dist;
2311 memcpy (pout, pout - dist, copy);
2312 len -= copy;
2313 pout += copy;
2314 }
2315 }
2316 }
2317 }
2318 }
2319 }
2320 }
2321
2322 /* We should have filled the output buffer. */
2323 if (unlikely (pout != poutend))
2324 {
2325 elf_zlib_failed ();
2326 return 0;
2327 }
2328
2329 return 1;
2330 }
2331
2332 /* Verify the zlib checksum. The checksum is in the 4 bytes at
2333 CHECKBYTES, and the uncompressed data is at UNCOMPRESSED /
2334 UNCOMPRESSED_SIZE. Returns 1 on success, 0 on failure. */
2335
2336 static int
2337 elf_zlib_verify_checksum (const unsigned char *checkbytes,
2338 const unsigned char *uncompressed,
2339 size_t uncompressed_size)
2340 {
2341 unsigned int i;
2342 unsigned int cksum;
2343 const unsigned char *p;
2344 uint32_t s1;
2345 uint32_t s2;
2346 size_t hsz;
2347
2348 cksum = 0;
2349 for (i = 0; i < 4; i++)
2350 cksum = (cksum << 8) | checkbytes[i];
2351
2352 s1 = 1;
2353 s2 = 0;
2354
2355 /* Minimize modulo operations. */
2356
2357 p = uncompressed;
2358 hsz = uncompressed_size;
2359 while (hsz >= 5552)
2360 {
2361 for (i = 0; i < 5552; i += 16)
2362 {
2363 /* Manually unroll loop 16 times. */
2364 s1 = s1 + *p++;
2365 s2 = s2 + s1;
2366 s1 = s1 + *p++;
2367 s2 = s2 + s1;
2368 s1 = s1 + *p++;
2369 s2 = s2 + s1;
2370 s1 = s1 + *p++;
2371 s2 = s2 + s1;
2372 s1 = s1 + *p++;
2373 s2 = s2 + s1;
2374 s1 = s1 + *p++;
2375 s2 = s2 + s1;
2376 s1 = s1 + *p++;
2377 s2 = s2 + s1;
2378 s1 = s1 + *p++;
2379 s2 = s2 + s1;
2380 s1 = s1 + *p++;
2381 s2 = s2 + s1;
2382 s1 = s1 + *p++;
2383 s2 = s2 + s1;
2384 s1 = s1 + *p++;
2385 s2 = s2 + s1;
2386 s1 = s1 + *p++;
2387 s2 = s2 + s1;
2388 s1 = s1 + *p++;
2389 s2 = s2 + s1;
2390 s1 = s1 + *p++;
2391 s2 = s2 + s1;
2392 s1 = s1 + *p++;
2393 s2 = s2 + s1;
2394 s1 = s1 + *p++;
2395 s2 = s2 + s1;
2396 }
2397 hsz -= 5552;
2398 s1 %= 65521;
2399 s2 %= 65521;
2400 }
2401
2402 while (hsz >= 16)
2403 {
2404 /* Manually unroll loop 16 times. */
2405 s1 = s1 + *p++;
2406 s2 = s2 + s1;
2407 s1 = s1 + *p++;
2408 s2 = s2 + s1;
2409 s1 = s1 + *p++;
2410 s2 = s2 + s1;
2411 s1 = s1 + *p++;
2412 s2 = s2 + s1;
2413 s1 = s1 + *p++;
2414 s2 = s2 + s1;
2415 s1 = s1 + *p++;
2416 s2 = s2 + s1;
2417 s1 = s1 + *p++;
2418 s2 = s2 + s1;
2419 s1 = s1 + *p++;
2420 s2 = s2 + s1;
2421 s1 = s1 + *p++;
2422 s2 = s2 + s1;
2423 s1 = s1 + *p++;
2424 s2 = s2 + s1;
2425 s1 = s1 + *p++;
2426 s2 = s2 + s1;
2427 s1 = s1 + *p++;
2428 s2 = s2 + s1;
2429 s1 = s1 + *p++;
2430 s2 = s2 + s1;
2431 s1 = s1 + *p++;
2432 s2 = s2 + s1;
2433 s1 = s1 + *p++;
2434 s2 = s2 + s1;
2435 s1 = s1 + *p++;
2436 s2 = s2 + s1;
2437
2438 hsz -= 16;
2439 }
2440
2441 for (i = 0; i < hsz; ++i)
2442 {
2443 s1 = s1 + *p++;
2444 s2 = s2 + s1;
2445 }
2446
2447 s1 %= 65521;
2448 s2 %= 65521;
2449
2450 if (unlikely ((s2 << 16) + s1 != cksum))
2451 {
2452 elf_zlib_failed ();
2453 return 0;
2454 }
2455
2456 return 1;
2457 }
2458
2459 /* Inflate a zlib stream from PIN/SIN to POUT/SOUT, and verify the
2460 checksum. Return 1 on success, 0 on error. */
2461
2462 static int
2463 elf_zlib_inflate_and_verify (const unsigned char *pin, size_t sin,
2464 uint16_t *zdebug_table, unsigned char *pout,
2465 size_t sout)
2466 {
2467 if (!elf_zlib_inflate (pin, sin, zdebug_table, pout, sout))
2468 return 0;
2469 if (!elf_zlib_verify_checksum (pin + sin - 4, pout, sout))
2470 return 0;
2471 return 1;
2472 }
2473
2474 /* Uncompress the old compressed debug format, the one emitted by
2475 --compress-debug-sections=zlib-gnu. The compressed data is in
2476 COMPRESSED / COMPRESSED_SIZE, and the function writes to
2477 *UNCOMPRESSED / *UNCOMPRESSED_SIZE. ZDEBUG_TABLE is work space to
2478 hold Huffman tables. Returns 0 on error, 1 on successful
2479 decompression or if something goes wrong. In general we try to
2480 carry on, by returning 1, even if we can't decompress. */
2481
2482 static int
2483 elf_uncompress_zdebug (struct backtrace_state *state,
2484 const unsigned char *compressed, size_t compressed_size,
2485 uint16_t *zdebug_table,
2486 backtrace_error_callback error_callback, void *data,
2487 unsigned char **uncompressed, size_t *uncompressed_size)
2488 {
2489 size_t sz;
2490 size_t i;
2491 unsigned char *po;
2492
2493 *uncompressed = NULL;
2494 *uncompressed_size = 0;
2495
2496 /* The format starts with the four bytes ZLIB, followed by the 8
2497 byte length of the uncompressed data in big-endian order,
2498 followed by a zlib stream. */
2499
2500 if (compressed_size < 12 || memcmp (compressed, "ZLIB", 4) != 0)
2501 return 1;
2502
2503 sz = 0;
2504 for (i = 0; i < 8; i++)
2505 sz = (sz << 8) | compressed[i + 4];
2506
2507 if (*uncompressed != NULL && *uncompressed_size >= sz)
2508 po = *uncompressed;
2509 else
2510 {
2511 po = (unsigned char *) backtrace_alloc (state, sz, error_callback, data);
2512 if (po == NULL)
2513 return 0;
2514 }
2515
2516 if (!elf_zlib_inflate_and_verify (compressed + 12, compressed_size - 12,
2517 zdebug_table, po, sz))
2518 return 1;
2519
2520 *uncompressed = po;
2521 *uncompressed_size = sz;
2522
2523 return 1;
2524 }
2525
2526 /* Uncompress the new compressed debug format, the official standard
2527 ELF approach emitted by --compress-debug-sections=zlib-gabi. The
2528 compressed data is in COMPRESSED / COMPRESSED_SIZE, and the
2529 function writes to *UNCOMPRESSED / *UNCOMPRESSED_SIZE.
2530 ZDEBUG_TABLE is work space as for elf_uncompress_zdebug. Returns 0
2531 on error, 1 on successful decompression or if something goes wrong.
2532 In general we try to carry on, by returning 1, even if we can't
2533 decompress. */
2534
2535 static int
2536 elf_uncompress_chdr (struct backtrace_state *state,
2537 const unsigned char *compressed, size_t compressed_size,
2538 uint16_t *zdebug_table,
2539 backtrace_error_callback error_callback, void *data,
2540 unsigned char **uncompressed, size_t *uncompressed_size)
2541 {
2542 const b_elf_chdr *chdr;
2543 unsigned char *po;
2544
2545 *uncompressed = NULL;
2546 *uncompressed_size = 0;
2547
2548 /* The format starts with an ELF compression header. */
2549 if (compressed_size < sizeof (b_elf_chdr))
2550 return 1;
2551
2552 chdr = (const b_elf_chdr *) compressed;
2553
2554 if (chdr->ch_type != ELFCOMPRESS_ZLIB)
2555 {
2556 /* Unsupported compression algorithm. */
2557 return 1;
2558 }
2559
2560 if (*uncompressed != NULL && *uncompressed_size >= chdr->ch_size)
2561 po = *uncompressed;
2562 else
2563 {
2564 po = (unsigned char *) backtrace_alloc (state, chdr->ch_size,
2565 error_callback, data);
2566 if (po == NULL)
2567 return 0;
2568 }
2569
2570 if (!elf_zlib_inflate_and_verify (compressed + sizeof (b_elf_chdr),
2571 compressed_size - sizeof (b_elf_chdr),
2572 zdebug_table, po, chdr->ch_size))
2573 return 1;
2574
2575 *uncompressed = po;
2576 *uncompressed_size = chdr->ch_size;
2577
2578 return 1;
2579 }
2580
2581 /* This function is a hook for testing the zlib support. It is only
2582 used by tests. */
2583
2584 int
2585 backtrace_uncompress_zdebug (struct backtrace_state *state,
2586 const unsigned char *compressed,
2587 size_t compressed_size,
2588 backtrace_error_callback error_callback,
2589 void *data, unsigned char **uncompressed,
2590 size_t *uncompressed_size)
2591 {
2592 uint16_t *zdebug_table;
2593 int ret;
2594
2595 zdebug_table = ((uint16_t *) backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
2596 error_callback, data));
2597 if (zdebug_table == NULL)
2598 return 0;
2599 ret = elf_uncompress_zdebug (state, compressed, compressed_size,
2600 zdebug_table, error_callback, data,
2601 uncompressed, uncompressed_size);
2602 backtrace_free (state, zdebug_table, ZDEBUG_TABLE_SIZE,
2603 error_callback, data);
2604 return ret;
2605 }
2606
2607 /* Add the backtrace data for one ELF file. Returns 1 on success,
2608 0 on failure (in both cases descriptor is closed) or -1 if exe
2609 is non-zero and the ELF file is ET_DYN, which tells the caller that
2610 elf_add will need to be called on the descriptor again after
2611 base_address is determined. */
2612
2613 static int
2614 elf_add (struct backtrace_state *state, const char *filename, int descriptor,
2615 uintptr_t base_address, backtrace_error_callback error_callback,
2616 void *data, fileline *fileline_fn, int *found_sym, int *found_dwarf,
2617 struct dwarf_data **fileline_entry, int exe, int debuginfo,
2618 const char *with_buildid_data, uint32_t with_buildid_size)
2619 {
2620 struct backtrace_view ehdr_view;
2621 b_elf_ehdr ehdr;
2622 off_t shoff;
2623 unsigned int shnum;
2624 unsigned int shstrndx;
2625 struct backtrace_view shdrs_view;
2626 int shdrs_view_valid;
2627 const b_elf_shdr *shdrs;
2628 const b_elf_shdr *shstrhdr;
2629 size_t shstr_size;
2630 off_t shstr_off;
2631 struct backtrace_view names_view;
2632 int names_view_valid;
2633 const char *names;
2634 unsigned int symtab_shndx;
2635 unsigned int dynsym_shndx;
2636 unsigned int i;
2637 struct debug_section_info sections[DEBUG_MAX];
2638 struct debug_section_info zsections[DEBUG_MAX];
2639 struct backtrace_view symtab_view;
2640 int symtab_view_valid;
2641 struct backtrace_view strtab_view;
2642 int strtab_view_valid;
2643 struct backtrace_view buildid_view;
2644 int buildid_view_valid;
2645 const char *buildid_data;
2646 uint32_t buildid_size;
2647 struct backtrace_view debuglink_view;
2648 int debuglink_view_valid;
2649 const char *debuglink_name;
2650 uint32_t debuglink_crc;
2651 struct backtrace_view debugaltlink_view;
2652 int debugaltlink_view_valid;
2653 const char *debugaltlink_name;
2654 const char *debugaltlink_buildid_data;
2655 uint32_t debugaltlink_buildid_size;
2656 off_t min_offset;
2657 off_t max_offset;
2658 struct backtrace_view debug_view;
2659 int debug_view_valid;
2660 unsigned int using_debug_view;
2661 uint16_t *zdebug_table;
2662 struct elf_ppc64_opd_data opd_data, *opd;
2663 struct dwarf_sections dwarf_sections;
2664
2665 if (!debuginfo)
2666 {
2667 *found_sym = 0;
2668 *found_dwarf = 0;
2669 }
2670
2671 shdrs_view_valid = 0;
2672 names_view_valid = 0;
2673 symtab_view_valid = 0;
2674 strtab_view_valid = 0;
2675 buildid_view_valid = 0;
2676 buildid_data = NULL;
2677 buildid_size = 0;
2678 debuglink_view_valid = 0;
2679 debuglink_name = NULL;
2680 debuglink_crc = 0;
2681 debugaltlink_view_valid = 0;
2682 debugaltlink_name = NULL;
2683 debugaltlink_buildid_data = NULL;
2684 debugaltlink_buildid_size = 0;
2685 debug_view_valid = 0;
2686 opd = NULL;
2687
2688 if (!backtrace_get_view (state, descriptor, 0, sizeof ehdr, error_callback,
2689 data, &ehdr_view))
2690 goto fail;
2691
2692 memcpy (&ehdr, ehdr_view.data, sizeof ehdr);
2693
2694 backtrace_release_view (state, &ehdr_view, error_callback, data);
2695
2696 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
2697 || ehdr.e_ident[EI_MAG1] != ELFMAG1
2698 || ehdr.e_ident[EI_MAG2] != ELFMAG2
2699 || ehdr.e_ident[EI_MAG3] != ELFMAG3)
2700 {
2701 error_callback (data, "executable file is not ELF", 0);
2702 goto fail;
2703 }
2704 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
2705 {
2706 error_callback (data, "executable file is unrecognized ELF version", 0);
2707 goto fail;
2708 }
2709
2710 #if BACKTRACE_ELF_SIZE == 32
2711 #define BACKTRACE_ELFCLASS ELFCLASS32
2712 #else
2713 #define BACKTRACE_ELFCLASS ELFCLASS64
2714 #endif
2715
2716 if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS)
2717 {
2718 error_callback (data, "executable file is unexpected ELF class", 0);
2719 goto fail;
2720 }
2721
2722 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB
2723 && ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
2724 {
2725 error_callback (data, "executable file has unknown endianness", 0);
2726 goto fail;
2727 }
2728
2729 /* If the executable is ET_DYN, it is either a PIE, or we are running
2730 directly a shared library with .interp. We need to wait for
2731 dl_iterate_phdr in that case to determine the actual base_address. */
2732 if (exe && ehdr.e_type == ET_DYN)
2733 return -1;
2734
2735 shoff = ehdr.e_shoff;
2736 shnum = ehdr.e_shnum;
2737 shstrndx = ehdr.e_shstrndx;
2738
2739 if ((shnum == 0 || shstrndx == SHN_XINDEX)
2740 && shoff != 0)
2741 {
2742 struct backtrace_view shdr_view;
2743 const b_elf_shdr *shdr;
2744
2745 if (!backtrace_get_view (state, descriptor, shoff, sizeof shdr,
2746 error_callback, data, &shdr_view))
2747 goto fail;
2748
2749 shdr = (const b_elf_shdr *) shdr_view.data;
2750
2751 if (shnum == 0)
2752 shnum = shdr->sh_size;
2753
2754 if (shstrndx == SHN_XINDEX)
2755 {
2756 shstrndx = shdr->sh_link;
2757
2758 /* Versions of the GNU binutils between 2.12 and 2.18 did
2759 not handle objects with more than SHN_LORESERVE sections
2760 correctly. All large section indexes were offset by
2761 0x100. There is more information at
2762 http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
2763 Fortunately these object files are easy to detect, as the
2764 GNU binutils always put the section header string table
2765 near the end of the list of sections. Thus if the
2766 section header string table index is larger than the
2767 number of sections, then we know we have to subtract
2768 0x100 to get the real section index. */
2769 if (shstrndx >= shnum && shstrndx >= SHN_LORESERVE + 0x100)
2770 shstrndx -= 0x100;
2771 }
2772
2773 backtrace_release_view (state, &shdr_view, error_callback, data);
2774 }
2775
2776 /* To translate PC to file/line when using DWARF, we need to find
2777 the .debug_info and .debug_line sections. */
2778
2779 /* Read the section headers, skipping the first one. */
2780
2781 if (!backtrace_get_view (state, descriptor, shoff + sizeof (b_elf_shdr),
2782 (shnum - 1) * sizeof (b_elf_shdr),
2783 error_callback, data, &shdrs_view))
2784 goto fail;
2785 shdrs_view_valid = 1;
2786 shdrs = (const b_elf_shdr *) shdrs_view.data;
2787
2788 /* Read the section names. */
2789
2790 shstrhdr = &shdrs[shstrndx - 1];
2791 shstr_size = shstrhdr->sh_size;
2792 shstr_off = shstrhdr->sh_offset;
2793
2794 if (!backtrace_get_view (state, descriptor, shstr_off, shstrhdr->sh_size,
2795 error_callback, data, &names_view))
2796 goto fail;
2797 names_view_valid = 1;
2798 names = (const char *) names_view.data;
2799
2800 symtab_shndx = 0;
2801 dynsym_shndx = 0;
2802
2803 memset (sections, 0, sizeof sections);
2804 memset (zsections, 0, sizeof zsections);
2805
2806 /* Look for the symbol table. */
2807 for (i = 1; i < shnum; ++i)
2808 {
2809 const b_elf_shdr *shdr;
2810 unsigned int sh_name;
2811 const char *name;
2812 int j;
2813
2814 shdr = &shdrs[i - 1];
2815
2816 if (shdr->sh_type == SHT_SYMTAB)
2817 symtab_shndx = i;
2818 else if (shdr->sh_type == SHT_DYNSYM)
2819 dynsym_shndx = i;
2820
2821 sh_name = shdr->sh_name;
2822 if (sh_name >= shstr_size)
2823 {
2824 error_callback (data, "ELF section name out of range", 0);
2825 goto fail;
2826 }
2827
2828 name = names + sh_name;
2829
2830 for (j = 0; j < (int) DEBUG_MAX; ++j)
2831 {
2832 if (strcmp (name, dwarf_section_names[j]) == 0)
2833 {
2834 sections[j].offset = shdr->sh_offset;
2835 sections[j].size = shdr->sh_size;
2836 sections[j].compressed = (shdr->sh_flags & SHF_COMPRESSED) != 0;
2837 break;
2838 }
2839 }
2840
2841 if (name[0] == '.' && name[1] == 'z')
2842 {
2843 for (j = 0; j < (int) DEBUG_MAX; ++j)
2844 {
2845 if (strcmp (name + 2, dwarf_section_names[j] + 1) == 0)
2846 {
2847 zsections[j].offset = shdr->sh_offset;
2848 zsections[j].size = shdr->sh_size;
2849 break;
2850 }
2851 }
2852 }
2853
2854 /* Read the build ID if present. This could check for any
2855 SHT_NOTE section with the right note name and type, but gdb
2856 looks for a specific section name. */
2857 if ((!debuginfo || with_buildid_data != NULL)
2858 && !buildid_view_valid
2859 && strcmp (name, ".note.gnu.build-id") == 0)
2860 {
2861 const b_elf_note *note;
2862
2863 if (!backtrace_get_view (state, descriptor, shdr->sh_offset,
2864 shdr->sh_size, error_callback, data,
2865 &buildid_view))
2866 goto fail;
2867
2868 buildid_view_valid = 1;
2869 note = (const b_elf_note *) buildid_view.data;
2870 if (note->type == NT_GNU_BUILD_ID
2871 && note->namesz == 4
2872 && strncmp (note->name, "GNU", 4) == 0
2873 && shdr->sh_size <= 12 + ((note->namesz + 3) & ~ 3) + note->descsz)
2874 {
2875 buildid_data = &note->name[0] + ((note->namesz + 3) & ~ 3);
2876 buildid_size = note->descsz;
2877 }
2878
2879 if (with_buildid_size != 0)
2880 {
2881 if (buildid_size != with_buildid_size)
2882 goto fail;
2883
2884 if (memcmp (buildid_data, with_buildid_data, buildid_size) != 0)
2885 goto fail;
2886 }
2887 }
2888
2889 /* Read the debuglink file if present. */
2890 if (!debuginfo
2891 && !debuglink_view_valid
2892 && strcmp (name, ".gnu_debuglink") == 0)
2893 {
2894 const char *debuglink_data;
2895 size_t crc_offset;
2896
2897 if (!backtrace_get_view (state, descriptor, shdr->sh_offset,
2898 shdr->sh_size, error_callback, data,
2899 &debuglink_view))
2900 goto fail;
2901
2902 debuglink_view_valid = 1;
2903 debuglink_data = (const char *) debuglink_view.data;
2904 crc_offset = strnlen (debuglink_data, shdr->sh_size);
2905 crc_offset = (crc_offset + 3) & ~3;
2906 if (crc_offset + 4 <= shdr->sh_size)
2907 {
2908 debuglink_name = debuglink_data;
2909 debuglink_crc = *(const uint32_t*)(debuglink_data + crc_offset);
2910 }
2911 }
2912
2913 if (!debugaltlink_view_valid
2914 && strcmp (name, ".gnu_debugaltlink") == 0)
2915 {
2916 const char *debugaltlink_data;
2917 size_t debugaltlink_name_len;
2918
2919 if (!backtrace_get_view (state, descriptor, shdr->sh_offset,
2920 shdr->sh_size, error_callback, data,
2921 &debugaltlink_view))
2922 goto fail;
2923
2924 debugaltlink_view_valid = 1;
2925 debugaltlink_data = (const char *) debugaltlink_view.data;
2926 debugaltlink_name = debugaltlink_data;
2927 debugaltlink_name_len = strnlen (debugaltlink_data, shdr->sh_size);
2928 if (debugaltlink_name_len < shdr->sh_size)
2929 {
2930 /* Include terminating zero. */
2931 debugaltlink_name_len += 1;
2932
2933 debugaltlink_buildid_data
2934 = debugaltlink_data + debugaltlink_name_len;
2935 debugaltlink_buildid_size = shdr->sh_size - debugaltlink_name_len;
2936 }
2937 }
2938
2939 /* Read the .opd section on PowerPC64 ELFv1. */
2940 if (ehdr.e_machine == EM_PPC64
2941 && (ehdr.e_flags & EF_PPC64_ABI) < 2
2942 && shdr->sh_type == SHT_PROGBITS
2943 && strcmp (name, ".opd") == 0)
2944 {
2945 if (!backtrace_get_view (state, descriptor, shdr->sh_offset,
2946 shdr->sh_size, error_callback, data,
2947 &opd_data.view))
2948 goto fail;
2949
2950 opd = &opd_data;
2951 opd->addr = shdr->sh_addr;
2952 opd->data = (const char *) opd_data.view.data;
2953 opd->size = shdr->sh_size;
2954 }
2955 }
2956
2957 if (symtab_shndx == 0)
2958 symtab_shndx = dynsym_shndx;
2959 if (symtab_shndx != 0 && !debuginfo)
2960 {
2961 const b_elf_shdr *symtab_shdr;
2962 unsigned int strtab_shndx;
2963 const b_elf_shdr *strtab_shdr;
2964 struct elf_syminfo_data *sdata;
2965
2966 symtab_shdr = &shdrs[symtab_shndx - 1];
2967 strtab_shndx = symtab_shdr->sh_link;
2968 if (strtab_shndx >= shnum)
2969 {
2970 error_callback (data,
2971 "ELF symbol table strtab link out of range", 0);
2972 goto fail;
2973 }
2974 strtab_shdr = &shdrs[strtab_shndx - 1];
2975
2976 if (!backtrace_get_view (state, descriptor, symtab_shdr->sh_offset,
2977 symtab_shdr->sh_size, error_callback, data,
2978 &symtab_view))
2979 goto fail;
2980 symtab_view_valid = 1;
2981
2982 if (!backtrace_get_view (state, descriptor, strtab_shdr->sh_offset,
2983 strtab_shdr->sh_size, error_callback, data,
2984 &strtab_view))
2985 goto fail;
2986 strtab_view_valid = 1;
2987
2988 sdata = ((struct elf_syminfo_data *)
2989 backtrace_alloc (state, sizeof *sdata, error_callback, data));
2990 if (sdata == NULL)
2991 goto fail;
2992
2993 if (!elf_initialize_syminfo (state, base_address,
2994 symtab_view.data, symtab_shdr->sh_size,
2995 strtab_view.data, strtab_shdr->sh_size,
2996 error_callback, data, sdata, opd))
2997 {
2998 backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
2999 goto fail;
3000 }
3001
3002 /* We no longer need the symbol table, but we hold on to the
3003 string table permanently. */
3004 backtrace_release_view (state, &symtab_view, error_callback, data);
3005 symtab_view_valid = 0;
3006
3007 *found_sym = 1;
3008
3009 elf_add_syminfo_data (state, sdata);
3010 }
3011
3012 backtrace_release_view (state, &shdrs_view, error_callback, data);
3013 shdrs_view_valid = 0;
3014 backtrace_release_view (state, &names_view, error_callback, data);
3015 names_view_valid = 0;
3016
3017 /* If the debug info is in a separate file, read that one instead. */
3018
3019 if (buildid_data != NULL)
3020 {
3021 int d;
3022
3023 d = elf_open_debugfile_by_buildid (state, buildid_data, buildid_size,
3024 error_callback, data);
3025 if (d >= 0)
3026 {
3027 int ret;
3028
3029 backtrace_release_view (state, &buildid_view, error_callback, data);
3030 if (debuglink_view_valid)
3031 backtrace_release_view (state, &debuglink_view, error_callback,
3032 data);
3033 if (debugaltlink_view_valid)
3034 backtrace_release_view (state, &debugaltlink_view, error_callback,
3035 data);
3036 ret = elf_add (state, "", d, base_address, error_callback, data,
3037 fileline_fn, found_sym, found_dwarf, NULL, 0, 1, NULL,
3038 0);
3039 if (ret < 0)
3040 backtrace_close (d, error_callback, data);
3041 else
3042 backtrace_close (descriptor, error_callback, data);
3043 return ret;
3044 }
3045 }
3046
3047 if (buildid_view_valid)
3048 {
3049 backtrace_release_view (state, &buildid_view, error_callback, data);
3050 buildid_view_valid = 0;
3051 }
3052
3053 if (opd)
3054 {
3055 backtrace_release_view (state, &opd->view, error_callback, data);
3056 opd = NULL;
3057 }
3058
3059 if (debuglink_name != NULL)
3060 {
3061 int d;
3062
3063 d = elf_open_debugfile_by_debuglink (state, filename, debuglink_name,
3064 debuglink_crc, error_callback,
3065 data);
3066 if (d >= 0)
3067 {
3068 int ret;
3069
3070 backtrace_release_view (state, &debuglink_view, error_callback,
3071 data);
3072 if (debugaltlink_view_valid)
3073 backtrace_release_view (state, &debugaltlink_view, error_callback,
3074 data);
3075 ret = elf_add (state, "", d, base_address, error_callback, data,
3076 fileline_fn, found_sym, found_dwarf, NULL, 0, 1, NULL,
3077 0);
3078 if (ret < 0)
3079 backtrace_close (d, error_callback, data);
3080 else
3081 backtrace_close(descriptor, error_callback, data);
3082 return ret;
3083 }
3084 }
3085
3086 if (debuglink_view_valid)
3087 {
3088 backtrace_release_view (state, &debuglink_view, error_callback, data);
3089 debuglink_view_valid = 0;
3090 }
3091
3092 struct dwarf_data *fileline_altlink = NULL;
3093 if (debugaltlink_name != NULL)
3094 {
3095 int d;
3096
3097 d = elf_open_debugfile_by_debuglink (state, filename, debugaltlink_name,
3098 0, error_callback, data);
3099 if (d >= 0)
3100 {
3101 int ret;
3102
3103 ret = elf_add (state, filename, d, base_address, error_callback, data,
3104 fileline_fn, found_sym, found_dwarf, &fileline_altlink,
3105 0, 1, debugaltlink_buildid_data,
3106 debugaltlink_buildid_size);
3107 backtrace_release_view (state, &debugaltlink_view, error_callback,
3108 data);
3109 debugaltlink_view_valid = 0;
3110 if (ret < 0)
3111 {
3112 backtrace_close (d, error_callback, data);
3113 return ret;
3114 }
3115 }
3116 }
3117
3118 if (debugaltlink_view_valid)
3119 {
3120 backtrace_release_view (state, &debugaltlink_view, error_callback, data);
3121 debugaltlink_view_valid = 0;
3122 }
3123
3124 /* Read all the debug sections in a single view, since they are
3125 probably adjacent in the file. If any of sections are
3126 uncompressed, we never release this view. */
3127
3128 min_offset = 0;
3129 max_offset = 0;
3130 for (i = 0; i < (int) DEBUG_MAX; ++i)
3131 {
3132 off_t end;
3133
3134 if (sections[i].size != 0)
3135 {
3136 if (min_offset == 0 || sections[i].offset < min_offset)
3137 min_offset = sections[i].offset;
3138 end = sections[i].offset + sections[i].size;
3139 if (end > max_offset)
3140 max_offset = end;
3141 }
3142 if (zsections[i].size != 0)
3143 {
3144 if (min_offset == 0 || zsections[i].offset < min_offset)
3145 min_offset = zsections[i].offset;
3146 end = zsections[i].offset + zsections[i].size;
3147 if (end > max_offset)
3148 max_offset = end;
3149 }
3150 }
3151 if (min_offset == 0 || max_offset == 0)
3152 {
3153 if (!backtrace_close (descriptor, error_callback, data))
3154 goto fail;
3155 return 1;
3156 }
3157
3158 if (!backtrace_get_view (state, descriptor, min_offset,
3159 max_offset - min_offset,
3160 error_callback, data, &debug_view))
3161 goto fail;
3162 debug_view_valid = 1;
3163
3164 /* We've read all we need from the executable. */
3165 if (!backtrace_close (descriptor, error_callback, data))
3166 goto fail;
3167 descriptor = -1;
3168
3169 using_debug_view = 0;
3170 for (i = 0; i < (int) DEBUG_MAX; ++i)
3171 {
3172 if (sections[i].size == 0)
3173 sections[i].data = NULL;
3174 else
3175 {
3176 sections[i].data = ((const unsigned char *) debug_view.data
3177 + (sections[i].offset - min_offset));
3178 ++using_debug_view;
3179 }
3180
3181 if (zsections[i].size == 0)
3182 zsections[i].data = NULL;
3183 else
3184 zsections[i].data = ((const unsigned char *) debug_view.data
3185 + (zsections[i].offset - min_offset));
3186 }
3187
3188 /* Uncompress the old format (--compress-debug-sections=zlib-gnu). */
3189
3190 zdebug_table = NULL;
3191 for (i = 0; i < (int) DEBUG_MAX; ++i)
3192 {
3193 if (sections[i].size == 0 && zsections[i].size > 0)
3194 {
3195 unsigned char *uncompressed_data;
3196 size_t uncompressed_size;
3197
3198 if (zdebug_table == NULL)
3199 {
3200 zdebug_table = ((uint16_t *)
3201 backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
3202 error_callback, data));
3203 if (zdebug_table == NULL)
3204 goto fail;
3205 }
3206
3207 uncompressed_data = NULL;
3208 uncompressed_size = 0;
3209 if (!elf_uncompress_zdebug (state, zsections[i].data,
3210 zsections[i].size, zdebug_table,
3211 error_callback, data,
3212 &uncompressed_data, &uncompressed_size))
3213 goto fail;
3214 sections[i].data = uncompressed_data;
3215 sections[i].size = uncompressed_size;
3216 sections[i].compressed = 0;
3217 }
3218 }
3219
3220 /* Uncompress the official ELF format
3221 (--compress-debug-sections=zlib-gabi). */
3222 for (i = 0; i < (int) DEBUG_MAX; ++i)
3223 {
3224 unsigned char *uncompressed_data;
3225 size_t uncompressed_size;
3226
3227 if (sections[i].size == 0 || !sections[i].compressed)
3228 continue;
3229
3230 if (zdebug_table == NULL)
3231 {
3232 zdebug_table = ((uint16_t *)
3233 backtrace_alloc (state, ZDEBUG_TABLE_SIZE,
3234 error_callback, data));
3235 if (zdebug_table == NULL)
3236 goto fail;
3237 }
3238
3239 uncompressed_data = NULL;
3240 uncompressed_size = 0;
3241 if (!elf_uncompress_chdr (state, sections[i].data, sections[i].size,
3242 zdebug_table, error_callback, data,
3243 &uncompressed_data, &uncompressed_size))
3244 goto fail;
3245 sections[i].data = uncompressed_data;
3246 sections[i].size = uncompressed_size;
3247 sections[i].compressed = 0;
3248
3249 --using_debug_view;
3250 }
3251
3252 if (zdebug_table != NULL)
3253 backtrace_free (state, zdebug_table, ZDEBUG_TABLE_SIZE,
3254 error_callback, data);
3255
3256 if (debug_view_valid && using_debug_view == 0)
3257 {
3258 backtrace_release_view (state, &debug_view, error_callback, data);
3259 debug_view_valid = 0;
3260 }
3261
3262 for (i = 0; i < (int) DEBUG_MAX; ++i)
3263 {
3264 dwarf_sections.data[i] = sections[i].data;
3265 dwarf_sections.size[i] = sections[i].size;
3266 }
3267
3268 if (!backtrace_dwarf_add (state, base_address, &dwarf_sections,
3269 ehdr.e_ident[EI_DATA] == ELFDATA2MSB,
3270 fileline_altlink,
3271 error_callback, data, fileline_fn,
3272 fileline_entry))
3273 goto fail;
3274
3275 *found_dwarf = 1;
3276
3277 return 1;
3278
3279 fail:
3280 if (shdrs_view_valid)
3281 backtrace_release_view (state, &shdrs_view, error_callback, data);
3282 if (names_view_valid)
3283 backtrace_release_view (state, &names_view, error_callback, data);
3284 if (symtab_view_valid)
3285 backtrace_release_view (state, &symtab_view, error_callback, data);
3286 if (strtab_view_valid)
3287 backtrace_release_view (state, &strtab_view, error_callback, data);
3288 if (debuglink_view_valid)
3289 backtrace_release_view (state, &debuglink_view, error_callback, data);
3290 if (debugaltlink_view_valid)
3291 backtrace_release_view (state, &debugaltlink_view, error_callback, data);
3292 if (buildid_view_valid)
3293 backtrace_release_view (state, &buildid_view, error_callback, data);
3294 if (debug_view_valid)
3295 backtrace_release_view (state, &debug_view, error_callback, data);
3296 if (opd)
3297 backtrace_release_view (state, &opd->view, error_callback, data);
3298 if (descriptor != -1)
3299 backtrace_close (descriptor, error_callback, data);
3300 return 0;
3301 }
3302
3303 /* Data passed to phdr_callback. */
3304
3305 struct phdr_data
3306 {
3307 struct backtrace_state *state;
3308 backtrace_error_callback error_callback;
3309 void *data;
3310 fileline *fileline_fn;
3311 int *found_sym;
3312 int *found_dwarf;
3313 const char *exe_filename;
3314 int exe_descriptor;
3315 };
3316
3317 /* Callback passed to dl_iterate_phdr. Load debug info from shared
3318 libraries. */
3319
3320 static int
3321 #ifdef __i386__
3322 __attribute__ ((__force_align_arg_pointer__))
3323 #endif
3324 phdr_callback (struct dl_phdr_info *info, size_t size ATTRIBUTE_UNUSED,
3325 void *pdata)
3326 {
3327 struct phdr_data *pd = (struct phdr_data *) pdata;
3328 const char *filename;
3329 int descriptor;
3330 int does_not_exist;
3331 fileline elf_fileline_fn;
3332 int found_dwarf;
3333
3334 /* There is not much we can do if we don't have the module name,
3335 unless executable is ET_DYN, where we expect the very first
3336 phdr_callback to be for the PIE. */
3337 if (info->dlpi_name == NULL || info->dlpi_name[0] == '\0')
3338 {
3339 if (pd->exe_descriptor == -1)
3340 return 0;
3341 filename = pd->exe_filename;
3342 descriptor = pd->exe_descriptor;
3343 pd->exe_descriptor = -1;
3344 }
3345 else
3346 {
3347 if (pd->exe_descriptor != -1)
3348 {
3349 backtrace_close (pd->exe_descriptor, pd->error_callback, pd->data);
3350 pd->exe_descriptor = -1;
3351 }
3352
3353 filename = info->dlpi_name;
3354 descriptor = backtrace_open (info->dlpi_name, pd->error_callback,
3355 pd->data, &does_not_exist);
3356 if (descriptor < 0)
3357 return 0;
3358 }
3359
3360 if (elf_add (pd->state, filename, descriptor, info->dlpi_addr,
3361 pd->error_callback, pd->data, &elf_fileline_fn, pd->found_sym,
3362 &found_dwarf, NULL, 0, 0, NULL, 0))
3363 {
3364 if (found_dwarf)
3365 {
3366 *pd->found_dwarf = 1;
3367 *pd->fileline_fn = elf_fileline_fn;
3368 }
3369 }
3370
3371 return 0;
3372 }
3373
3374 /* Initialize the backtrace data we need from an ELF executable. At
3375 the ELF level, all we need to do is find the debug info
3376 sections. */
3377
3378 int
3379 backtrace_initialize (struct backtrace_state *state, const char *filename,
3380 int descriptor, backtrace_error_callback error_callback,
3381 void *data, fileline *fileline_fn)
3382 {
3383 int ret;
3384 int found_sym;
3385 int found_dwarf;
3386 fileline elf_fileline_fn = elf_nodebug;
3387 struct phdr_data pd;
3388
3389 ret = elf_add (state, filename, descriptor, 0, error_callback, data,
3390 &elf_fileline_fn, &found_sym, &found_dwarf, NULL, 1, 0, NULL,
3391 0);
3392 if (!ret)
3393 return 0;
3394
3395 pd.state = state;
3396 pd.error_callback = error_callback;
3397 pd.data = data;
3398 pd.fileline_fn = &elf_fileline_fn;
3399 pd.found_sym = &found_sym;
3400 pd.found_dwarf = &found_dwarf;
3401 pd.exe_filename = filename;
3402 pd.exe_descriptor = ret < 0 ? descriptor : -1;
3403
3404 dl_iterate_phdr (phdr_callback, (void *) &pd);
3405
3406 if (!state->threaded)
3407 {
3408 if (found_sym)
3409 state->syminfo_fn = elf_syminfo;
3410 else if (state->syminfo_fn == NULL)
3411 state->syminfo_fn = elf_nosyms;
3412 }
3413 else
3414 {
3415 if (found_sym)
3416 backtrace_atomic_store_pointer (&state->syminfo_fn, elf_syminfo);
3417 else
3418 (void) __sync_bool_compare_and_swap (&state->syminfo_fn, NULL,
3419 elf_nosyms);
3420 }
3421
3422 if (!state->threaded)
3423 *fileline_fn = state->fileline_fn;
3424 else
3425 *fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
3426
3427 if (*fileline_fn == NULL || *fileline_fn == elf_nodebug)
3428 *fileline_fn = elf_fileline_fn;
3429
3430 return 1;
3431 }