Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* xcoff.c -- Get debug data from an XCOFF file for backtraces.
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
3 Adapted from elf.c.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
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
21THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31POSSIBILITY 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
40#ifdef HAVE_LOADQUERY
41#include <sys/ldr.h>
42#endif
43
44#include "backtrace.h"
45#include "internal.h"
46
47/* The configure script must tell us whether we are 32-bit or 64-bit
48 XCOFF. We could make this code test and support either possibility,
49 but there is no point. This code only works for the currently
50 running executable, which means that we know the XCOFF mode at
51 configure time. */
52
53#if BACKTRACE_XCOFF_SIZE != 32 && BACKTRACE_XCOFF_SIZE != 64
54#error "Unknown BACKTRACE_XCOFF_SIZE"
55#endif
56
57/* XCOFF file header. */
58
59#if BACKTRACE_XCOFF_SIZE == 32
60
61typedef struct {
62 uint16_t f_magic;
63 uint16_t f_nscns;
64 uint32_t f_timdat;
65 uint32_t f_symptr;
66 uint32_t f_nsyms;
67 uint16_t f_opthdr;
68 uint16_t f_flags;
69} b_xcoff_filhdr;
70
71#define XCOFF_MAGIC 0737
72
73#else /* BACKTRACE_XCOFF_SIZE != 32 */
74
75typedef struct {
76 uint16_t f_magic;
77 uint16_t f_nscns;
78 uint32_t f_timdat;
79 uint64_t f_symptr;
80 uint16_t f_opthdr;
81 uint16_t f_flags;
82 uint32_t f_nsyms;
83} b_xcoff_filhdr;
84
85#define XCOFF_MAGIC 0767
86
87#endif /* BACKTRACE_XCOFF_SIZE != 32 */
88
89#define F_SHROBJ 0x2000 /* File is a shared object. */
90
91/* XCOFF section header. */
92
93#if BACKTRACE_XCOFF_SIZE == 32
94
95typedef struct {
96 char s_name[8];
97 uint32_t s_paddr;
98 uint32_t s_vaddr;
99 uint32_t s_size;
100 uint32_t s_scnptr;
101 uint32_t s_relptr;
102 uint32_t s_lnnoptr;
103 uint16_t s_nreloc;
104 uint16_t s_nlnno;
105 uint32_t s_flags;
106} b_xcoff_scnhdr;
107
108#define _OVERFLOW_MARKER 65535
109
110#else /* BACKTRACE_XCOFF_SIZE != 32 */
111
112typedef struct {
113 char name[8];
114 uint64_t s_paddr;
115 uint64_t s_vaddr;
116 uint64_t s_size;
117 uint64_t s_scnptr;
118 uint64_t s_relptr;
119 uint64_t s_lnnoptr;
120 uint32_t s_nreloc;
121 uint32_t s_nlnno;
122 uint32_t s_flags;
123} b_xcoff_scnhdr;
124
125#endif /* BACKTRACE_XCOFF_SIZE != 32 */
126
127#define STYP_DWARF 0x10 /* DWARF debugging section. */
128#define STYP_TEXT 0x20 /* Executable text (code) section. */
129#define STYP_OVRFLO 0x8000 /* Line-number field overflow section. */
130
131#define SSUBTYP_DWINFO 0x10000 /* DWARF info section. */
132#define SSUBTYP_DWLINE 0x20000 /* DWARF line-number section. */
133#define SSUBTYP_DWARNGE 0x50000 /* DWARF aranges section. */
134#define SSUBTYP_DWABREV 0x60000 /* DWARF abbreviation section. */
135#define SSUBTYP_DWSTR 0x70000 /* DWARF strings section. */
136
137/* XCOFF symbol. */
138
139#define SYMNMLEN 8
140
141#if BACKTRACE_XCOFF_SIZE == 32
142
143typedef struct {
144 union {
145 char _name[SYMNMLEN];
146 struct {
147 uint32_t _zeroes;
148 uint32_t _offset;
149 } _s;
150 } _u;
151#define n_name _u._name
152#define n_zeroes _u._s._zeroes
153#define n_offset_ _u._s._offset
154
155 uint32_t n_value;
156 int16_t n_scnum;
157 uint16_t n_type;
158 uint8_t n_sclass;
159 uint8_t n_numaux;
160} __attribute__ ((packed)) b_xcoff_syment;
161
162#else /* BACKTRACE_XCOFF_SIZE != 32 */
163
164typedef struct {
165 uint64_t n_value;
166 uint32_t n_offset_;
167 int16_t n_scnum;
168 uint16_t n_type;
169 uint8_t n_sclass;
170 uint8_t n_numaux;
171} __attribute__ ((packed)) b_xcoff_syment;
172
173#endif /* BACKTRACE_XCOFF_SIZE != 32 */
174
175#define SYMESZ 18
176
177#define C_EXT 2 /* External symbol. */
178#define C_FCN 101 /* Beginning or end of function. */
179#define C_FILE 103 /* Source file name. */
180#define C_HIDEXT 107 /* Unnamed external symbol. */
181#define C_BINCL 108 /* Beginning of include file. */
182#define C_EINCL 109 /* End of include file. */
183#define C_WEAKEXT 111 /* Weak external symbol. */
184
185#define ISFCN(x) ((x) & 0x0020)
186
187/* XCOFF AUX entry. */
188
189#define AUXESZ 18
190#define FILNMLEN 14
191
192typedef union {
193#if BACKTRACE_XCOFF_SIZE == 32
194 struct {
195 uint16_t pad;
196 uint16_t x_lnnohi;
197 uint16_t x_lnno;
198 } x_block;
199#else
200 struct {
201 uint32_t x_lnno;
202 } x_block;
203#endif
204 union {
205 char x_fname[FILNMLEN];
206 struct {
207 uint32_t x_zeroes;
208 uint32_t x_offset;
209 char pad[FILNMLEN-8];
210 uint8_t x_ftype;
211 } _x;
212 } x_file;
213#if BACKTRACE_XCOFF_SIZE == 32
214 struct {
215 uint32_t x_exptr;
216 uint32_t x_fsize;
217 uint32_t x_lnnoptr;
218 uint32_t x_endndx;
219 } x_fcn;
220#else
221 struct {
222 uint64_t x_lnnoptr;
223 uint32_t x_fsize;
224 uint32_t x_endndx;
225 } x_fcn;
226#endif
227 struct {
228 uint8_t pad[AUXESZ-1];
229 uint8_t x_auxtype;
230 } x_auxtype;
231} __attribute__ ((packed)) b_xcoff_auxent;
232
233/* XCOFF line number entry. */
234
235#if BACKTRACE_XCOFF_SIZE == 32
236
237typedef struct {
238 union {
239 uint32_t l_symndx;
240 uint32_t l_paddr;
241 } l_addr;
242 uint16_t l_lnno;
243} b_xcoff_lineno;
244
245#define LINESZ 6
246
247#else /* BACKTRACE_XCOFF_SIZE != 32 */
248
249typedef struct {
250 union {
251 uint32_t l_symndx;
252 uint64_t l_paddr;
253 } l_addr;
254 uint32_t l_lnno;
255} b_xcoff_lineno;
256
257#define LINESZ 12
258
259#endif /* BACKTRACE_XCOFF_SIZE != 32 */
260
261#if BACKTRACE_XCOFF_SIZE == 32
262#define XCOFF_AIX_TEXTBASE 0x10000000u
263#else
264#define XCOFF_AIX_TEXTBASE 0x100000000ul
265#endif
266
267/* AIX big archive fixed-length header. */
268
269#define AIAMAGBIG "<bigaf>\n"
270
271typedef struct {
272 char fl_magic[8]; /* Archive magic string. */
273 char fl_memoff[20]; /* Offset to member table. */
274 char fl_gstoff[20]; /* Offset to global symbol table. */
275 char fl_gst64off[20]; /* Offset to global symbol table for 64-bit objects. */
276 char fl_fstmoff[20]; /* Offset to first archive member. */
277 char fl_freeoff[20]; /* Offset to first member on free list. */
278} b_ar_fl_hdr;
279
280/* AIX big archive file member header. */
281
282typedef struct {
283 char ar_size[20]; /* File member size - decimal. */
284 char ar_nxtmem[20]; /* Next member offset - decimal. */
285 char ar_prvmem[20]; /* Previous member offset - decimal. */
286 char ar_date[12]; /* File member date - decimal. */
287 char ar_uid[12]; /* File member userid - decimal. */
288 char ar_gid[12]; /* File member group id - decimal. */
289 char ar_mode[12]; /* File member mode - octal. */
290 char ar_namlen[4]; /* File member name length - decimal. */
291 char ar_name[2]; /* Start of member name. */
292} b_ar_hdr;
293
294
295/* Information we keep for an XCOFF symbol. */
296
297struct xcoff_symbol
298{
299 /* The name of the symbol. */
300 const char *name;
301 /* The address of the symbol. */
302 uintptr_t address;
303 /* The size of the symbol. */
304 size_t size;
305};
306
307/* Information to pass to xcoff_syminfo. */
308
309struct xcoff_syminfo_data
310{
311 /* Symbols for the next module. */
312 struct xcoff_syminfo_data *next;
313 /* The XCOFF symbols, sorted by address. */
314 struct xcoff_symbol *symbols;
315 /* The number of symbols. */
316 size_t count;
317};
318
319/* Information about an include file. */
320
321struct xcoff_incl
322{
323 /* File name. */
324 const char *filename;
325 /* Offset to first line number from the include file. */
326 uintptr_t begin;
327 /* Offset to last line number from the include file. */
328 uintptr_t end;
329};
330
331/* A growable vector of include files information. */
332
333struct xcoff_incl_vector
334{
335 /* Memory. This is an array of struct xcoff_incl. */
336 struct backtrace_vector vec;
337 /* Number of include files. */
338 size_t count;
339};
340
341/* Map a single PC value to a file/function/line. */
342
343struct xcoff_line
344{
345 /* PC. */
346 uintptr_t pc;
347 /* File name. Many entries in the array are expected to point to
348 the same file name. */
349 const char *filename;
350 /* Function name. */
351 const char *function;
352 /* Line number. */
353 int lineno;
354};
355
356/* A growable vector of line number information. This is used while
357 reading the line numbers. */
358
359struct xcoff_line_vector
360{
361 /* Memory. This is an array of struct xcoff_line. */
362 struct backtrace_vector vec;
363 /* Number of valid mappings. */
364 size_t count;
365};
366
367/* The information we need to map a PC to a file and line. */
368
369struct xcoff_fileline_data
370{
371 /* The data for the next file we know about. */
372 struct xcoff_fileline_data *next;
373 /* Line number information. */
374 struct xcoff_line_vector vec;
375};
376
377/* An index of DWARF sections we care about. */
378
379enum dwarf_section
380{
381 DWSECT_INFO,
382 DWSECT_LINE,
383 DWSECT_ABBREV,
384 DWSECT_RANGES,
385 DWSECT_STR,
386 DWSECT_MAX
387};
388
389/* Information we gather for the DWARF sections we care about. */
390
391struct dwsect_info
392{
393 /* Section file offset. */
394 off_t offset;
395 /* Section size. */
396 size_t size;
397 /* Section contents, after read from file. */
398 const unsigned char *data;
399};
400
401/* A dummy callback function used when we can't find any debug info. */
402
403static int
404xcoff_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
405 uintptr_t pc ATTRIBUTE_UNUSED,
406 backtrace_full_callback callback ATTRIBUTE_UNUSED,
407 backtrace_error_callback error_callback, void *data)
408{
409 error_callback (data, "no debug info in XCOFF executable", -1);
410 return 0;
411}
412
413/* A dummy callback function used when we can't find a symbol
414 table. */
415
416static void
417xcoff_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
418 uintptr_t addr ATTRIBUTE_UNUSED,
419 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
420 backtrace_error_callback error_callback, void *data)
421{
422 error_callback (data, "no symbol table in XCOFF executable", -1);
423}
424
425/* Compare struct xcoff_symbol for qsort. */
426
427static int
428xcoff_symbol_compare (const void *v1, const void *v2)
429{
430 const struct xcoff_symbol *e1 = (const struct xcoff_symbol *) v1;
431 const struct xcoff_symbol *e2 = (const struct xcoff_symbol *) v2;
432
433 if (e1->address < e2->address)
434 return -1;
435 else if (e1->address > e2->address)
436 return 1;
437 else
438 return 0;
439}
440
441/* Compare an ADDR against an xcoff_symbol for bsearch. */
442
443static int
444xcoff_symbol_search (const void *vkey, const void *ventry)
445{
446 const uintptr_t *key = (const uintptr_t *) vkey;
447 const struct xcoff_symbol *entry = (const struct xcoff_symbol *) ventry;
448 uintptr_t addr;
449
450 addr = *key;
451 if (addr < entry->address)
452 return -1;
453 else if ((entry->size == 0 && addr > entry->address)
454 || (entry->size > 0 && addr >= entry->address + entry->size))
455 return 1;
456 else
457 return 0;
458}
459
460/* Add XDATA to the list in STATE. */
461
462static void
463xcoff_add_syminfo_data (struct backtrace_state *state,
464 struct xcoff_syminfo_data *xdata)
465{
466 if (!state->threaded)
467 {
468 struct xcoff_syminfo_data **pp;
469
470 for (pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
471 *pp != NULL;
472 pp = &(*pp)->next)
473 ;
474 *pp = xdata;
475 }
476 else
477 {
478 while (1)
479 {
480 struct xcoff_syminfo_data **pp;
481
482 pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
483
484 while (1)
485 {
486 struct xcoff_syminfo_data *p;
487
488 p = backtrace_atomic_load_pointer (pp);
489
490 if (p == NULL)
491 break;
492
493 pp = &p->next;
494 }
495
496 if (__sync_bool_compare_and_swap (pp, NULL, xdata))
497 break;
498 }
499 }
500}
501
502/* Return the symbol name and value for an ADDR. */
503
504static void
505xcoff_syminfo (struct backtrace_state *state ATTRIBUTE_UNUSED, uintptr_t addr,
506 backtrace_syminfo_callback callback,
507 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
508 void *data)
509{
510 struct xcoff_syminfo_data *edata;
511 struct xcoff_symbol *sym = NULL;
512
513 if (!state->threaded)
514 {
515 for (edata = (struct xcoff_syminfo_data *) state->syminfo_data;
516 edata != NULL;
517 edata = edata->next)
518 {
519 sym = ((struct xcoff_symbol *)
520 bsearch (&addr, edata->symbols, edata->count,
521 sizeof (struct xcoff_symbol), xcoff_symbol_search));
522 if (sym != NULL)
523 break;
524 }
525 }
526 else
527 {
528 struct xcoff_syminfo_data **pp;
529
530 pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
531 while (1)
532 {
533 edata = backtrace_atomic_load_pointer (pp);
534 if (edata == NULL)
535 break;
536
537 sym = ((struct xcoff_symbol *)
538 bsearch (&addr, edata->symbols, edata->count,
539 sizeof (struct xcoff_symbol), xcoff_symbol_search));
540 if (sym != NULL)
541 break;
542
543 pp = &edata->next;
544 }
545 }
546
547 if (sym == NULL)
548 callback (data, addr, NULL, 0, 0);
549 else
550 callback (data, addr, sym->name, sym->address, sym->size);
551}
552
553/* Return the name of an XCOFF symbol. */
554
555static const char *
556xcoff_symname (const b_xcoff_syment *asym,
557 const unsigned char *strtab, size_t strtab_size)
558{
559#if BACKTRACE_XCOFF_SIZE == 32
560 if (asym->n_zeroes != 0)
561 {
562 /* Make a copy as we will release the symtab view. */
563 char name[SYMNMLEN+1];
564 strncpy (name, asym->n_name, SYMNMLEN);
565 name[SYMNMLEN] = '\0';
566 return strdup (name);
567 }
568#endif
569 if (asym->n_sclass & 0x80)
570 return NULL; /* .debug */
571 if (asym->n_offset_ >= strtab_size)
572 return NULL;
573 return (const char *) strtab + asym->n_offset_;
574}
575
576/* Initialize the symbol table info for xcoff_syminfo. */
577
578static int
579xcoff_initialize_syminfo (struct backtrace_state *state,
580 uintptr_t base_address,
581 const b_xcoff_scnhdr *sects,
582 const b_xcoff_syment *syms, size_t nsyms,
583 const unsigned char *strtab, size_t strtab_size,
584 backtrace_error_callback error_callback, void *data,
585 struct xcoff_syminfo_data *sdata)
586{
587 size_t xcoff_symbol_count;
588 size_t xcoff_symbol_size;
589 struct xcoff_symbol *xcoff_symbols;
590 size_t i;
591 unsigned int j;
592
593 /* We only care about function symbols. Count them. */
594 xcoff_symbol_count = 0;
595 for (i = 0; i < nsyms; ++i)
596 {
597 const b_xcoff_syment *asym = &syms[i];
598 if ((asym->n_sclass == C_EXT || asym->n_sclass == C_HIDEXT
599 || asym->n_sclass == C_WEAKEXT)
600 && ISFCN (asym->n_type) && asym->n_numaux > 0 && asym->n_scnum > 0)
601 ++xcoff_symbol_count;
602
603 i += asym->n_numaux;
604 }
605
606 xcoff_symbol_size = xcoff_symbol_count * sizeof (struct xcoff_symbol);
607 xcoff_symbols = ((struct xcoff_symbol *)
608 backtrace_alloc (state, xcoff_symbol_size, error_callback,
609 data));
610 if (xcoff_symbols == NULL)
611 return 0;
612
613 j = 0;
614 for (i = 0; i < nsyms; ++i)
615 {
616 const b_xcoff_syment *asym = &syms[i];
617 if ((asym->n_sclass == C_EXT || asym->n_sclass == C_HIDEXT
618 || asym->n_sclass == C_WEAKEXT)
619 && ISFCN (asym->n_type) && asym->n_numaux > 0 && asym->n_scnum > 0)
620 {
621 const b_xcoff_auxent *aux = (const b_xcoff_auxent *) (asym + 1);
622 xcoff_symbols[j].name = xcoff_symname (asym, strtab, strtab_size);
623 xcoff_symbols[j].address = base_address + asym->n_value
624 - sects[asym->n_scnum - 1].s_paddr;
625 /* x_fsize will be 0 if there is no debug information. */
626 xcoff_symbols[j].size = aux->x_fcn.x_fsize;
627 ++j;
628 }
629
630 i += asym->n_numaux;
631 }
632
633 backtrace_qsort (xcoff_symbols, xcoff_symbol_count,
634 sizeof (struct xcoff_symbol), xcoff_symbol_compare);
635
636 sdata->next = NULL;
637 sdata->symbols = xcoff_symbols;
638 sdata->count = xcoff_symbol_count;
639
640 return 1;
641}
642
643/* Compare struct xcoff_line for qsort. */
644
645static int
646xcoff_line_compare (const void *v1, const void *v2)
647{
648 const struct xcoff_line *ln1 = (const struct xcoff_line *) v1;
649 const struct xcoff_line *ln2 = (const struct xcoff_line *) v2;
650
651 if (ln1->pc < ln2->pc)
652 return -1;
653 else if (ln1->pc > ln2->pc)
654 return 1;
655 else
656 return 0;
657}
658
659/* Find a PC in a line vector. We always allocate an extra entry at
660 the end of the lines vector, so that this routine can safely look
661 at the next entry. */
662
663static int
664xcoff_line_search (const void *vkey, const void *ventry)
665{
666 const uintptr_t *key = (const uintptr_t *) vkey;
667 const struct xcoff_line *entry = (const struct xcoff_line *) ventry;
668 uintptr_t pc;
669
670 pc = *key;
671 if (pc < entry->pc)
672 return -1;
673 else if ((entry + 1)->pc == (uintptr_t) -1 || pc >= (entry + 1)->pc)
674 return 1;
675 else
676 return 0;
677}
678
679/* Look for a PC in the line vector for one module. On success,
680 call CALLBACK and return whatever it returns. On error, call
681 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
682 0 if not. */
683
684static int
685xcoff_lookup_pc (struct backtrace_state *state ATTRIBUTE_UNUSED,
686 struct xcoff_fileline_data *fdata, uintptr_t pc,
687 backtrace_full_callback callback,
688 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
689 void *data, int *found)
690{
691 const struct xcoff_line *ln;
692 const char *function;
693
694 *found = 1;
695
696 ln = (struct xcoff_line *) bsearch (&pc, fdata->vec.vec.base,
697 fdata->vec.count,
698 sizeof (struct xcoff_line),
699 xcoff_line_search);
700 if (ln == NULL)
701 {
702 *found = 0;
703 return 0;
704 }
705
706 function = ln->function;
707 /* AIX prepends a '.' to function entry points, remove it. */
708 if (*function == '.')
709 ++function;
710 return callback (data, pc, ln->filename, ln->lineno, function);
711}
712
713/* Return the file/line information for a PC using the XCOFF lineno
714 mapping we built earlier. */
715
716static int
717xcoff_fileline (struct backtrace_state *state, uintptr_t pc,
718 backtrace_full_callback callback,
719 backtrace_error_callback error_callback, void *data)
720
721{
722 struct xcoff_fileline_data *fdata;
723 int found;
724 int ret;
725
726 if (!state->threaded)
727 {
728 for (fdata = (struct xcoff_fileline_data *) state->fileline_data;
729 fdata != NULL;
730 fdata = fdata->next)
731 {
732 ret = xcoff_lookup_pc (state, fdata, pc, callback, error_callback,
733 data, &found);
734 if (ret != 0 || found)
735 return ret;
736 }
737 }
738 else
739 {
740 struct xcoff_fileline_data **pp;
741
742 pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
743 while (1)
744 {
745 fdata = backtrace_atomic_load_pointer (pp);
746 if (fdata == NULL)
747 break;
748
749 ret = xcoff_lookup_pc (state, fdata, pc, callback, error_callback,
750 data, &found);
751 if (ret != 0 || found)
752 return ret;
753
754 pp = &fdata->next;
755 }
756 }
757
758 /* FIXME: See if any libraries have been dlopen'ed. */
759
760 return callback (data, pc, NULL, 0, NULL);
761}
762
763/* Add a new mapping to the vector of line mappings that we are
764 building. Returns 1 on success, 0 on failure. */
765
766static int
767xcoff_add_line (struct backtrace_state *state, uintptr_t pc,
768 const char *filename, const char *function, uint32_t lnno,
769 backtrace_error_callback error_callback, void *data,
770 struct xcoff_line_vector *vec)
771{
772 struct xcoff_line *ln;
773
774 ln = ((struct xcoff_line *)
775 backtrace_vector_grow (state, sizeof (struct xcoff_line),
776 error_callback, data, &vec->vec));
777 if (ln == NULL)
778 return 0;
779
780 ln->pc = pc;
781 ln->filename = filename;
782 ln->function = function;
783 ln->lineno = lnno;
784
785 ++vec->count;
786
787 return 1;
788}
789
790/* Add the line number entries for a function to the line vector. */
791
792static int
793xcoff_process_linenos (struct backtrace_state *state, uintptr_t base_address,
794 const b_xcoff_syment *fsym, const char *filename,
795 const b_xcoff_scnhdr *sects,
796 const unsigned char *strtab, size_t strtab_size,
797 uint32_t fcn_lnno, struct xcoff_incl_vector *vec,
798 struct xcoff_line_vector *lvec,
799 const unsigned char *linenos, size_t linenos_size,
800 uintptr_t lnnoptr0,
801 backtrace_error_callback error_callback, void *data)
802{
803 const b_xcoff_auxent *aux;
804 const b_xcoff_lineno *lineno;
805 const unsigned char *lineptr;
806 const char *function;
807 struct xcoff_incl *incl = NULL;
808 uintptr_t lnnoptr;
809 uintptr_t pc;
810 uint32_t lnno;
811 int begincl;
812 size_t i;
813
814 aux = (const b_xcoff_auxent *) (fsym + 1);
815 lnnoptr = aux->x_fcn.x_lnnoptr;
816
817 if (lnnoptr < lnnoptr0 || lnnoptr + LINESZ > lnnoptr0 + linenos_size)
818 return 0;
819
820 function = xcoff_symname (fsym, strtab, strtab_size);
821 if (function == NULL)
822 return 0;
823
824 /* Skip first entry that points to symtab. */
825
826 lnnoptr += LINESZ;
827
828 lineptr = linenos + (lnnoptr - lnnoptr0);
829
830 begincl = -1;
831 while (lineptr + LINESZ <= linenos + linenos_size)
832 {
833 lineno = (const b_xcoff_lineno *) lineptr;
834
835 lnno = lineno->l_lnno;
836 if (lnno == 0)
837 break;
838
839 /* If part of a function other than the beginning comes from an
840 include file, the line numbers are absolute, rather than
841 relative to the beginning of the function. */
842 for (i = 0; i < vec->count; ++i)
843 {
844 incl = (struct xcoff_incl *) vec->vec.base + i;
845 if (incl->begin <= lnnoptr && lnnoptr <= incl->end)
846 break;
847 }
848 if (begincl == -1)
849 begincl = (i < vec->count);
850 if (i < vec->count)
851 {
852 filename = incl->filename;
853 if (begincl == 1)
854 lnno += fcn_lnno - 1;
855 }
856 else
857 lnno += fcn_lnno - 1;
858
859 pc = base_address + lineno->l_addr.l_paddr
860 - sects[fsym->n_scnum - 1].s_paddr;
861 xcoff_add_line (state, pc, filename, function, lnno, error_callback,
862 data, lvec);
863
864 lnnoptr += LINESZ;
865 lineptr += LINESZ;
866 }
867
868 return 1;
869}
870
871/* Initialize the line vector info for xcoff_fileline. */
872
873static int
874xcoff_initialize_fileline (struct backtrace_state *state,
875 uintptr_t base_address,
876 const b_xcoff_scnhdr *sects,
877 const b_xcoff_syment *syms, size_t nsyms,
878 const unsigned char *strtab, size_t strtab_size,
879 const unsigned char *linenos, size_t linenos_size,
880 uint64_t lnnoptr0,
881 backtrace_error_callback error_callback, void *data)
882{
883 struct xcoff_fileline_data *fdata;
884 struct xcoff_incl_vector vec;
885 struct xcoff_line *ln;
886 const b_xcoff_syment *fsym;
887 const b_xcoff_auxent *aux;
888 const char *filename;
889 const char *name;
890 struct xcoff_incl *incl;
891 uintptr_t begin, end;
892 uintptr_t lnno;
893 size_t i;
894
895 fdata = ((struct xcoff_fileline_data *)
896 backtrace_alloc (state, sizeof (struct xcoff_fileline_data),
897 error_callback, data));
898 if (fdata == NULL)
899 return 0;
900
901 memset (fdata, 0, sizeof *fdata);
902 memset (&vec, 0, sizeof vec);
903
904 /* Process include files first. */
905
906 begin = 0;
907 for (i = 0; i < nsyms; ++i)
908 {
909 const b_xcoff_syment *asym = &syms[i];
910
911 switch (asym->n_sclass)
912 {
913 case C_BINCL:
914 begin = asym->n_value;
915 break;
916
917 case C_EINCL:
918 if (begin == 0)
919 break;
920 end = asym->n_value;
921 incl = ((struct xcoff_incl *)
922 backtrace_vector_grow (state, sizeof (struct xcoff_incl),
923 error_callback, data, &vec.vec));
924 if (incl != NULL)
925 {
926 incl->filename = xcoff_symname (asym, strtab, strtab_size);
927 incl->begin = begin;
928 incl->end = end;
929 ++vec.count;
930 }
931 begin = 0;
932 break;
933 }
934
935 i += asym->n_numaux;
936 }
937
938 filename = NULL;
939 fsym = NULL;
940 for (i = 0; i < nsyms; ++i)
941 {
942 const b_xcoff_syment *asym = &syms[i];
943
944 switch (asym->n_sclass)
945 {
946 case C_FILE:
947 filename = xcoff_symname (asym, strtab, strtab_size);
948 if (filename == NULL)
949 break;
950
951 /* If the file auxiliary entry is not used, the symbol name is
952 the name of the source file. If the file auxiliary entry is
953 used, then the symbol name should be .file, and the first
954 file auxiliary entry (by convention) contains the source
955 file name. */
956
957 if (asym->n_numaux > 0 && !strcmp (filename, ".file"))
958 {
959 aux = (const b_xcoff_auxent *) (asym + 1);
960 if (aux->x_file._x.x_zeroes != 0)
961 {
962 /* Make a copy as we will release the symtab view. */
963 char name[FILNMLEN+1];
964 strncpy (name, aux->x_file.x_fname, FILNMLEN);
965 name[FILNMLEN] = '\0';
966 filename = strdup (name);
967 }
968 else if (aux->x_file._x.x_offset < strtab_size)
969 filename = (const char *) strtab + aux->x_file._x.x_offset;
970 else
971 filename = NULL;
972 }
973 break;
974
975 case C_EXT:
976 case C_HIDEXT:
977 case C_WEAKEXT:
978 fsym = NULL;
979 if (!ISFCN (asym->n_type) || asym->n_numaux == 0)
980 break;
981 if (filename == NULL)
982 break;
983 fsym = asym;
984 break;
985
986 case C_FCN:
987 if (asym->n_numaux == 0)
988 break;
989 if (fsym == NULL)
990 break;
991 name = xcoff_symname (asym, strtab, strtab_size);
992 if (name == NULL)
993 break;
994 aux = (const b_xcoff_auxent *) (asym + 1);
995#if BACKTRACE_XCOFF_SIZE == 32
996 lnno = (uint32_t) aux->x_block.x_lnnohi << 16
997 | aux->x_block.x_lnno;
998#else
999 lnno = aux->x_block.x_lnno;
1000#endif
1001 if (!strcmp (name, ".bf"))
1002 {
1003 xcoff_process_linenos (state, base_address, fsym, filename,
1004 sects, strtab, strtab_size, lnno, &vec,
1005 &fdata->vec, linenos, linenos_size,
1006 lnnoptr0, error_callback, data);
1007 }
1008 else if (!strcmp (name, ".ef"))
1009 {
1010 fsym = NULL;
1011 }
1012 break;
1013 }
1014
1015 i += asym->n_numaux;
1016 }
1017
1018 /* Allocate one extra entry at the end. */
1019 ln = ((struct xcoff_line *)
1020 backtrace_vector_grow (state, sizeof (struct xcoff_line),
1021 error_callback, data, &fdata->vec.vec));
1022 if (ln == NULL)
1023 goto fail;
1024 ln->pc = (uintptr_t) -1;
1025 ln->filename = NULL;
1026 ln->function = NULL;
1027 ln->lineno = 0;
1028
1029 if (!backtrace_vector_release (state, &fdata->vec.vec, error_callback, data))
1030 goto fail;
1031
1032 backtrace_qsort (fdata->vec.vec.base, fdata->vec.count,
1033 sizeof (struct xcoff_line), xcoff_line_compare);
1034
1035 if (!state->threaded)
1036 {
1037 struct xcoff_fileline_data **pp;
1038
1039 for (pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
1040 *pp != NULL;
1041 pp = &(*pp)->next)
1042 ;
1043 *pp = fdata;
1044 }
1045 else
1046 {
1047 while (1)
1048 {
1049 struct xcoff_fileline_data **pp;
1050
1051 pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
1052
1053 while (1)
1054 {
1055 struct xcoff_fileline_data *p;
1056
1057 p = backtrace_atomic_load_pointer (pp);
1058
1059 if (p == NULL)
1060 break;
1061
1062 pp = &p->next;
1063 }
1064
1065 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
1066 break;
1067 }
1068 }
1069
1070 return 1;
1071
1072fail:
1073 return 0;
1074}
1075
1076/* Add the backtrace data for one XCOFF file. Returns 1 on success,
1077 0 on failure (in both cases descriptor is closed). */
1078
1079static int
1080xcoff_add (struct backtrace_state *state, int descriptor, off_t offset,
1081 uintptr_t base_address, backtrace_error_callback error_callback,
1082 void *data, fileline *fileline_fn, int *found_sym, int exe)
1083{
1084 struct backtrace_view fhdr_view;
1085 struct backtrace_view sects_view;
1086 struct backtrace_view linenos_view;
1087 struct backtrace_view syms_view;
1088 struct backtrace_view str_view;
1089 struct backtrace_view dwarf_view;
1090 b_xcoff_filhdr fhdr;
1091 const b_xcoff_scnhdr *sects;
1092 const b_xcoff_scnhdr *stext;
1093 uint64_t lnnoptr;
1094 uint32_t nlnno;
1095 off_t str_off;
1096 off_t min_offset;
1097 off_t max_offset;
1098 struct dwsect_info dwsect[DWSECT_MAX];
1099 size_t sects_size;
1100 size_t syms_size;
1101 int32_t str_size;
1102 int sects_view_valid;
1103 int linenos_view_valid;
1104 int syms_view_valid;
1105 int str_view_valid;
1106 int dwarf_view_valid;
1107 int magic_ok;
1108 int i;
1109
1110 *found_sym = 0;
1111
1112 sects_view_valid = 0;
1113 linenos_view_valid = 0;
1114 syms_view_valid = 0;
1115 str_view_valid = 0;
1116 dwarf_view_valid = 0;
1117
1118 str_size = 0;
1119
1120 /* Map the XCOFF file header. */
1121 if (!backtrace_get_view (state, descriptor, offset, sizeof (b_xcoff_filhdr),
1122 error_callback, data, &fhdr_view))
1123 goto fail;
1124
1125 memcpy (&fhdr, fhdr_view.data, sizeof fhdr);
1126 magic_ok = (fhdr.f_magic == XCOFF_MAGIC);
1127
1128 backtrace_release_view (state, &fhdr_view, error_callback, data);
1129
1130 if (!magic_ok)
1131 {
1132 if (exe)
1133 error_callback (data, "executable file is not XCOFF", 0);
1134 goto fail;
1135 }
1136
1137 /* Verify object is of expected type. */
1138 if ((exe && (fhdr.f_flags & F_SHROBJ))
1139 || (!exe && !(fhdr.f_flags & F_SHROBJ)))
1140 goto fail;
1141
1142 /* Read the section headers. */
1143
1144 sects_size = fhdr.f_nscns * sizeof (b_xcoff_scnhdr);
1145
1146 if (!backtrace_get_view (state, descriptor,
1147 offset + sizeof (fhdr) + fhdr.f_opthdr,
1148 sects_size, error_callback, data, &sects_view))
1149 goto fail;
1150 sects_view_valid = 1;
1151 sects = (const b_xcoff_scnhdr *) sects_view.data;
1152
1153 /* FIXME: assumes only one .text section. */
1154 for (i = 0; i < fhdr.f_nscns; ++i)
1155 if ((sects[i].s_flags & 0xffff) == STYP_TEXT)
1156 break;
1157 if (i == fhdr.f_nscns)
1158 goto fail;
1159
1160 stext = &sects[i];
1161
1162 /* AIX ldinfo_textorg includes the XCOFF headers. */
1163 base_address = (exe ? XCOFF_AIX_TEXTBASE : base_address) + stext->s_scnptr;
1164
1165 lnnoptr = stext->s_lnnoptr;
1166 nlnno = stext->s_nlnno;
1167
1168#if BACKTRACE_XCOFF_SIZE == 32
1169 if (nlnno == _OVERFLOW_MARKER)
1170 {
1171 int sntext = i + 1;
1172 /* Find the matching .ovrflo section. */
1173 for (i = 0; i < fhdr.f_nscns; ++i)
1174 {
1175 if (((sects[i].s_flags & 0xffff) == STYP_OVRFLO)
1176 && sects[i].s_nlnno == sntext)
1177 {
1178 nlnno = sects[i].s_vaddr;
1179 break;
1180 }
1181 }
1182 }
1183#endif
1184
1185 /* Read the symbol table and the string table. */
1186
1187 if (fhdr.f_symptr != 0)
1188 {
1189 struct xcoff_syminfo_data *sdata;
1190
1191 /* Symbol table is followed by the string table. The string table
1192 starts with its length (on 4 bytes).
1193 Map the symbol table and the length of the string table. */
1194 syms_size = fhdr.f_nsyms * sizeof (b_xcoff_syment);
1195
1196 if (!backtrace_get_view (state, descriptor, offset + fhdr.f_symptr,
1197 syms_size + 4, error_callback, data,
1198 &syms_view))
1199 goto fail;
1200 syms_view_valid = 1;
1201
1202 memcpy (&str_size, syms_view.data + syms_size, 4);
1203
1204 str_off = fhdr.f_symptr + syms_size;
1205
1206 if (str_size > 4)
1207 {
1208 /* Map string table (including the length word). */
1209
1210 if (!backtrace_get_view (state, descriptor, offset + str_off,
1211 str_size, error_callback, data, &str_view))
1212 goto fail;
1213 str_view_valid = 1;
1214 }
1215
1216 sdata = ((struct xcoff_syminfo_data *)
1217 backtrace_alloc (state, sizeof *sdata, error_callback, data));
1218 if (sdata == NULL)
1219 goto fail;
1220
1221 if (!xcoff_initialize_syminfo (state, base_address, sects,
1222 syms_view.data, fhdr.f_nsyms,
1223 str_view.data, str_size,
1224 error_callback, data, sdata))
1225 {
1226 backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
1227 goto fail;
1228 }
1229
1230 *found_sym = 1;
1231
1232 xcoff_add_syminfo_data (state, sdata);
1233 }
1234
1235 /* Read all the DWARF sections in a single view, since they are
1236 probably adjacent in the file. We never release this view. */
1237
1238 min_offset = 0;
1239 max_offset = 0;
1240 memset (dwsect, 0, sizeof dwsect);
1241 for (i = 0; i < fhdr.f_nscns; ++i)
1242 {
1243 off_t end;
1244 int idx;
1245
1246 if ((sects[i].s_flags & 0xffff) != STYP_DWARF
1247 || sects[i].s_size == 0)
1248 continue;
1249 /* Map DWARF section to array index. */
1250 switch (sects[i].s_flags & 0xffff0000)
1251 {
1252 case SSUBTYP_DWINFO:
1253 idx = DWSECT_INFO;
1254 break;
1255 case SSUBTYP_DWLINE:
1256 idx = DWSECT_LINE;
1257 break;
1258 case SSUBTYP_DWABREV:
1259 idx = DWSECT_ABBREV;
1260 break;
1261 case SSUBTYP_DWARNGE:
1262 idx = DWSECT_RANGES;
1263 break;
1264 case SSUBTYP_DWSTR:
1265 idx = DWSECT_STR;
1266 break;
1267 default:
1268 continue;
1269 }
1270 if (min_offset == 0 || (off_t) sects[i].s_scnptr < min_offset)
1271 min_offset = sects[i].s_scnptr;
1272 end = sects[i].s_scnptr + sects[i].s_size;
1273 if (end > max_offset)
1274 max_offset = end;
1275 dwsect[idx].offset = sects[i].s_scnptr;
1276 dwsect[idx].size = sects[i].s_size;
1277 }
1278 if (min_offset != 0 && max_offset != 0)
1279 {
1280 if (!backtrace_get_view (state, descriptor, offset + min_offset,
1281 max_offset - min_offset,
1282 error_callback, data, &dwarf_view))
1283 goto fail;
1284 dwarf_view_valid = 1;
1285
1286 for (i = 0; i < (int) DWSECT_MAX; ++i)
1287 {
1288 if (dwsect[i].offset == 0)
1289 dwsect[i].data = NULL;
1290 else
1291 dwsect[i].data = ((const unsigned char *) dwarf_view.data
1292 + (dwsect[i].offset - min_offset));
1293 }
1294
1295 if (!backtrace_dwarf_add (state, 0,
1296 dwsect[DWSECT_INFO].data,
1297 dwsect[DWSECT_INFO].size,
1298#if BACKTRACE_XCOFF_SIZE == 32
1299 /* XXX workaround for broken lineoff */
1300 dwsect[DWSECT_LINE].data - 4,
1301#else
1302 /* XXX workaround for broken lineoff */
1303 dwsect[DWSECT_LINE].data - 12,
1304#endif
1305 dwsect[DWSECT_LINE].size,
1306 dwsect[DWSECT_ABBREV].data,
1307 dwsect[DWSECT_ABBREV].size,
1308 dwsect[DWSECT_RANGES].data,
1309 dwsect[DWSECT_RANGES].size,
1310 dwsect[DWSECT_STR].data,
1311 dwsect[DWSECT_STR].size,
1312 1, /* big endian */
1313 error_callback, data, fileline_fn))
1314 goto fail;
1315 }
1316
1317 /* Read the XCOFF line number entries if DWARF sections not found. */
1318
1319 if (!dwarf_view_valid && fhdr.f_symptr != 0 && lnnoptr != 0)
1320 {
1321 size_t linenos_size = (size_t) nlnno * LINESZ;
1322
1323 if (!backtrace_get_view (state, descriptor, offset + lnnoptr,
1324 linenos_size,
1325 error_callback, data, &linenos_view))
1326 goto fail;
1327 linenos_view_valid = 1;
1328
1329 if (xcoff_initialize_fileline (state, base_address, sects,
1330 syms_view.data, fhdr.f_nsyms,
1331 str_view.data, str_size,
1332 linenos_view.data, linenos_size,
1333 lnnoptr, error_callback, data))
1334 *fileline_fn = xcoff_fileline;
1335
1336 backtrace_release_view (state, &linenos_view, error_callback, data);
1337 linenos_view_valid = 0;
1338 }
1339
1340 backtrace_release_view (state, &sects_view, error_callback, data);
1341 sects_view_valid = 0;
1342 if (syms_view_valid)
1343 backtrace_release_view (state, &syms_view, error_callback, data);
1344 syms_view_valid = 0;
1345
1346 /* We've read all we need from the executable. */
1347 if (!backtrace_close (descriptor, error_callback, data))
1348 goto fail;
1349 descriptor = -1;
1350
1351 return 1;
1352
1353 fail:
1354 if (sects_view_valid)
1355 backtrace_release_view (state, &sects_view, error_callback, data);
1356 if (str_view_valid)
1357 backtrace_release_view (state, &str_view, error_callback, data);
1358 if (syms_view_valid)
1359 backtrace_release_view (state, &syms_view, error_callback, data);
1360 if (linenos_view_valid)
1361 backtrace_release_view (state, &linenos_view, error_callback, data);
1362 if (dwarf_view_valid)
1363 backtrace_release_view (state, &dwarf_view, error_callback, data);
1364 if (descriptor != -1 && offset == 0)
1365 backtrace_close (descriptor, error_callback, data);
1366 return 0;
1367}
1368
1369#ifdef HAVE_LOADQUERY
1370
1371/* Read an integer value in human-readable format from an AIX
1372 big archive fixed-length or member header. */
1373
1374static int
1375xcoff_parse_decimal (const char *buf, size_t size, off_t *off)
1376{
1377 char str[32];
1378 char *end;
1379
1380 if (size >= sizeof str)
1381 return 0;
1382 memcpy (str, buf, size);
1383 str[size] = '\0';
1384 *off = strtol (str, &end, 10);
1385 if (*end != '\0' && *end != ' ')
1386 return 0;
1387
1388 return 1;
1389}
1390
1391/* Add the backtrace data for a member of an AIX big archive.
1392 Returns 1 on success, 0 on failure. */
1393
1394static int
1395xcoff_armem_add (struct backtrace_state *state, int descriptor,
1396 uintptr_t base_address, const char *member,
1397 backtrace_error_callback error_callback, void *data,
1398 fileline *fileline_fn, int *found_sym)
1399{
1400 struct backtrace_view view;
1401 b_ar_fl_hdr fl_hdr;
1402 const b_ar_hdr *ar_hdr;
1403 off_t off;
1404 off_t len;
1405 int memlen;
1406
1407 *found_sym = 0;
1408
1409 /* Map archive fixed-length header. */
1410
1411 if (!backtrace_get_view (state, descriptor, 0, sizeof (b_ar_fl_hdr),
1412 error_callback, data, &view))
1413 goto fail;
1414
1415 memcpy (&fl_hdr, view.data, sizeof (b_ar_fl_hdr));
1416
1417 backtrace_release_view (state, &view, error_callback, data);
1418
1419 if (memcmp (fl_hdr.fl_magic, AIAMAGBIG, 8) != 0)
1420 goto fail;
1421
1422 memlen = strlen (member);
1423
1424 /* Read offset of first archive member. */
1425 if (!xcoff_parse_decimal (fl_hdr.fl_fstmoff, sizeof fl_hdr.fl_fstmoff, &off))
1426 goto fail;
1427 while (off != 0)
1428 {
1429 /* Map archive member header and member name. */
1430
1431 if (!backtrace_get_view (state, descriptor, off,
1432 sizeof (b_ar_hdr) + memlen,
1433 error_callback, data, &view))
1434 break;
1435
1436 ar_hdr = (const b_ar_hdr *) view.data;
1437
1438 /* Read archive member name length. */
1439 if (!xcoff_parse_decimal (ar_hdr->ar_namlen, sizeof ar_hdr->ar_namlen,
1440 &len))
1441 {
1442 backtrace_release_view (state, &view, error_callback, data);
1443 break;
1444 }
1445 if (len == memlen && !memcmp (ar_hdr->ar_name, member, memlen))
1446 {
1447 off = (off + sizeof (b_ar_hdr) + memlen + 1) & ~1;
1448
1449 /* The archive can contain several members with the same name
1450 (e.g. 32-bit and 64-bit), so continue if not ok. */
1451
1452 if (xcoff_add (state, descriptor, off, base_address, error_callback,
1453 data, fileline_fn, found_sym, 0))
1454 {
1455 backtrace_release_view (state, &view, error_callback, data);
1456 return 1;
1457 }
1458 }
1459
1460 /* Read offset of next archive member. */
1461 if (!xcoff_parse_decimal (ar_hdr->ar_nxtmem, sizeof ar_hdr->ar_nxtmem,
1462 &off))
1463 {
1464 backtrace_release_view (state, &view, error_callback, data);
1465 break;
1466 }
1467 backtrace_release_view (state, &view, error_callback, data);
1468 }
1469
1470 fail:
1471 /* No matching member found. */
1472 backtrace_close (descriptor, error_callback, data);
1473 return 0;
1474}
1475
1476/* Add the backtrace data for dynamically loaded libraries. */
1477
1478static void
1479xcoff_add_shared_libs (struct backtrace_state *state,
1480 backtrace_error_callback error_callback,
1481 void *data, fileline *fileline_fn, int *found_sym)
1482{
1483 const struct ld_info *ldinfo;
1484 void *buf;
1485 unsigned int buflen;
1486 const char *member;
1487 int descriptor;
1488 int does_not_exist;
1489 int lib_found_sym;
1490 int ret;
1491
1492 /* Retrieve the list of loaded libraries. */
1493
1494 buf = NULL;
1495 buflen = 512;
1496 do
1497 {
1498 buf = realloc (buf, buflen);
1499 if (buf == NULL)
1500 {
1501 ret = -1;
1502 break;
1503 }
1504 ret = loadquery (L_GETINFO, buf, buflen);
1505 if (ret == 0)
1506 break;
1507 buflen *= 2;
1508 }
1509 while (ret == -1 && errno == ENOMEM);
1510 if (ret != 0)
1511 {
1512 free (buf);
1513 return;
1514 }
1515
1516 ldinfo = (const struct ld_info *) buf;
1517 while ((const char *) ldinfo < (const char *) buf + buflen)
1518 {
1519 if (*ldinfo->ldinfo_filename != '/')
1520 goto next;
1521
1522 descriptor = backtrace_open (ldinfo->ldinfo_filename, error_callback,
1523 data, &does_not_exist);
1524 if (descriptor < 0)
1525 goto next;
1526
1527 /* Check if it is an archive (member name not empty). */
1528
1529 member = ldinfo->ldinfo_filename + strlen (ldinfo->ldinfo_filename) + 1;
1530 if (*member)
1531 {
1532 xcoff_armem_add (state, descriptor,
1533 (uintptr_t) ldinfo->ldinfo_textorg, member,
1534 error_callback, data, fileline_fn, &lib_found_sym);
1535 }
1536 else
1537 {
1538 xcoff_add (state, descriptor, 0, (uintptr_t) ldinfo->ldinfo_textorg,
1539 error_callback, data, fileline_fn, &lib_found_sym, 0);
1540 }
1541 if (lib_found_sym)
1542 *found_sym = 1;
1543
1544 next:
1545 if (ldinfo->ldinfo_next == 0)
1546 break;
1547 ldinfo = (const struct ld_info *) ((const char *) ldinfo
1548 + ldinfo->ldinfo_next);
1549 }
1550
1551 free (buf);
1552}
1553#endif /* HAVE_LOADQUERY */
1554
1555/* Initialize the backtrace data we need from an XCOFF executable.
1556 Returns 1 on success, 0 on failure. */
1557
1558int
1559backtrace_initialize (struct backtrace_state *state,
1560 const char *filename ATTRIBUTE_UNUSED, int descriptor,
1561 backtrace_error_callback error_callback,
1562 void *data, fileline *fileline_fn)
1563{
1564 int ret;
1565 int found_sym;
1566 fileline xcoff_fileline_fn = xcoff_nodebug;
1567
1568 ret = xcoff_add (state, descriptor, 0, 0, error_callback, data,
1569 &xcoff_fileline_fn, &found_sym, 1);
1570 if (!ret)
1571 return 0;
1572
1573#ifdef HAVE_LOADQUERY
1574 xcoff_add_shared_libs (state, error_callback, data, &xcoff_fileline_fn,
1575 &found_sym);
1576#endif
1577
1578 if (!state->threaded)
1579 {
1580 if (found_sym)
1581 state->syminfo_fn = xcoff_syminfo;
1582 else if (state->syminfo_fn == NULL)
1583 state->syminfo_fn = xcoff_nosyms;
1584 }
1585 else
1586 {
1587 if (found_sym)
1588 backtrace_atomic_store_pointer (&state->syminfo_fn, xcoff_syminfo);
1589 else
1590 __sync_bool_compare_and_swap (&state->syminfo_fn, NULL, xcoff_nosyms);
1591 }
1592
1593 if (!state->threaded)
1594 {
1595 if (state->fileline_fn == NULL || state->fileline_fn == xcoff_nodebug)
1596 *fileline_fn = xcoff_fileline_fn;
1597 }
1598 else
1599 {
1600 fileline current_fn;
1601
1602 current_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
1603 if (current_fn == NULL || current_fn == xcoff_nodebug)
1604 *fileline_fn = xcoff_fileline_fn;
1605 }
1606
1607 return 1;
1608}
1609

Warning: That file was not part of the compilation database. It may have many parsing errors.