S/390: Fix vmslg instruction and builtin.
[official-gcc.git] / libbacktrace / xcoff.c
blobf75239164ba4a4d56ebdb203c17fcf976914ad0b
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.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
33 #include "config.h"
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
40 #ifdef HAVE_LOADQUERY
41 #include <sys/ldr.h>
42 #endif
44 #include "backtrace.h"
45 #include "internal.h"
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. */
53 #if BACKTRACE_XCOFF_SIZE != 32 && BACKTRACE_XCOFF_SIZE != 64
54 #error "Unknown BACKTRACE_XCOFF_SIZE"
55 #endif
57 /* XCOFF file header. */
59 #if BACKTRACE_XCOFF_SIZE == 32
61 typedef 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;
71 #define XCOFF_MAGIC 0737
73 #else /* BACKTRACE_XCOFF_SIZE != 32 */
75 typedef 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;
85 #define XCOFF_MAGIC 0767
87 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
89 #define F_SHROBJ 0x2000 /* File is a shared object. */
91 /* XCOFF section header. */
93 #if BACKTRACE_XCOFF_SIZE == 32
95 typedef 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;
108 #define _OVERFLOW_MARKER 65535
110 #else /* BACKTRACE_XCOFF_SIZE != 32 */
112 typedef 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;
125 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
127 #define STYP_TEXT 0x20 /* Executable text (code) section. */
128 #define STYP_OVRFLO 0x8000 /* Line-number field overflow section. */
130 /* XCOFF symbol. */
132 #define SYMNMLEN 8
134 #if BACKTRACE_XCOFF_SIZE == 32
136 typedef struct {
137 union {
138 char _name[SYMNMLEN];
139 struct {
140 uint32_t _zeroes;
141 uint32_t _offset;
142 } _s;
143 } _u;
144 #define n_name _u._name
145 #define n_zeroes _u._s._zeroes
146 #define n_offset_ _u._s._offset
148 uint32_t n_value;
149 int16_t n_scnum;
150 uint16_t n_type;
151 uint8_t n_sclass;
152 uint8_t n_numaux;
153 } __attribute__ ((packed)) b_xcoff_syment;
155 #else /* BACKTRACE_XCOFF_SIZE != 32 */
157 typedef struct {
158 uint64_t n_value;
159 uint32_t n_offset_;
160 int16_t n_scnum;
161 uint16_t n_type;
162 uint8_t n_sclass;
163 uint8_t n_numaux;
164 } __attribute__ ((packed)) b_xcoff_syment;
166 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
168 #define SYMESZ 18
170 #define C_EXT 2 /* External symbol. */
171 #define C_FCN 101 /* Beginning or end of function. */
172 #define C_FILE 103 /* Source file name. */
173 #define C_HIDEXT 107 /* Unnamed external symbol. */
174 #define C_BINCL 108 /* Beginning of include file. */
175 #define C_EINCL 109 /* End of include file. */
176 #define C_WEAKEXT 111 /* Weak external symbol. */
178 #define ISFCN(x) ((x) & 0x0020)
180 /* XCOFF AUX entry. */
182 #define AUXESZ 18
183 #define FILNMLEN 14
185 typedef union {
186 #if BACKTRACE_XCOFF_SIZE == 32
187 struct {
188 uint16_t pad;
189 uint16_t x_lnnohi;
190 uint16_t x_lnno;
191 } x_block;
192 #else
193 struct {
194 uint32_t x_lnno;
195 } x_block;
196 #endif
197 union {
198 char x_fname[FILNMLEN];
199 struct {
200 uint32_t x_zeroes;
201 uint32_t x_offset;
202 char pad[FILNMLEN-8];
203 uint8_t x_ftype;
204 } _x;
205 } x_file;
206 #if BACKTRACE_XCOFF_SIZE == 32
207 struct {
208 uint32_t x_exptr;
209 uint32_t x_fsize;
210 uint32_t x_lnnoptr;
211 uint32_t x_endndx;
212 } x_fcn;
213 #else
214 struct {
215 uint64_t x_lnnoptr;
216 uint32_t x_fsize;
217 uint32_t x_endndx;
218 } x_fcn;
219 #endif
220 struct {
221 uint8_t pad[AUXESZ-1];
222 uint8_t x_auxtype;
223 } x_auxtype;
224 } __attribute__ ((packed)) b_xcoff_auxent;
226 /* XCOFF line number entry. */
228 #if BACKTRACE_XCOFF_SIZE == 32
230 typedef struct {
231 union {
232 uint32_t l_symndx;
233 uint32_t l_paddr;
234 } l_addr;
235 uint16_t l_lnno;
236 } b_xcoff_lineno;
238 #define LINESZ 6
240 #else /* BACKTRACE_XCOFF_SIZE != 32 */
242 typedef struct {
243 union {
244 uint32_t l_symndx;
245 uint64_t l_paddr;
246 } l_addr;
247 uint32_t l_lnno;
248 } b_xcoff_lineno;
250 #define LINESZ 12
252 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
254 #if BACKTRACE_XCOFF_SIZE == 32
255 #define XCOFF_AIX_TEXTBASE 0x10000000u
256 #else
257 #define XCOFF_AIX_TEXTBASE 0x100000000ul
258 #endif
260 /* AIX big archive fixed-length header. */
262 #define AIAMAGBIG "<bigaf>\n"
264 typedef struct {
265 char fl_magic[8]; /* Archive magic string. */
266 char fl_memoff[20]; /* Offset to member table. */
267 char fl_gstoff[20]; /* Offset to global symbol table. */
268 char fl_gst64off[20]; /* Offset to global symbol table for 64-bit objects. */
269 char fl_fstmoff[20]; /* Offset to first archive member. */
270 char fl_freeoff[20]; /* Offset to first member on free list. */
271 } b_ar_fl_hdr;
273 /* AIX big archive file member header. */
275 typedef struct {
276 char ar_size[20]; /* File member size - decimal. */
277 char ar_nxtmem[20]; /* Next member offset - decimal. */
278 char ar_prvmem[20]; /* Previous member offset - decimal. */
279 char ar_date[12]; /* File member date - decimal. */
280 char ar_uid[12]; /* File member userid - decimal. */
281 char ar_gid[12]; /* File member group id - decimal. */
282 char ar_mode[12]; /* File member mode - octal. */
283 char ar_namlen[4]; /* File member name length - decimal. */
284 char ar_name[2]; /* Start of member name. */
285 } b_ar_hdr;
288 /* Information we keep for an XCOFF symbol. */
290 struct xcoff_symbol
292 /* The name of the symbol. */
293 const char *name;
294 /* The address of the symbol. */
295 uintptr_t address;
296 /* The size of the symbol. */
297 size_t size;
300 /* Information to pass to xcoff_syminfo. */
302 struct xcoff_syminfo_data
304 /* Symbols for the next module. */
305 struct xcoff_syminfo_data *next;
306 /* The XCOFF symbols, sorted by address. */
307 struct xcoff_symbol *symbols;
308 /* The number of symbols. */
309 size_t count;
312 /* Information about an include file. */
314 struct xcoff_incl
316 /* File name. */
317 const char *filename;
318 /* Offset to first line number from the include file. */
319 uintptr_t begin;
320 /* Offset to last line number from the include file. */
321 uintptr_t end;
324 /* A growable vector of include files information. */
326 struct xcoff_incl_vector
328 /* Memory. This is an array of struct xcoff_incl. */
329 struct backtrace_vector vec;
330 /* Number of include files. */
331 size_t count;
334 /* Map a single PC value to a file/function/line. */
336 struct xcoff_line
338 /* PC. */
339 uintptr_t pc;
340 /* File name. Many entries in the array are expected to point to
341 the same file name. */
342 const char *filename;
343 /* Function name. */
344 const char *function;
345 /* Line number. */
346 int lineno;
349 /* A growable vector of line number information. This is used while
350 reading the line numbers. */
352 struct xcoff_line_vector
354 /* Memory. This is an array of struct xcoff_line. */
355 struct backtrace_vector vec;
356 /* Number of valid mappings. */
357 size_t count;
360 /* The information we need to map a PC to a file and line. */
362 struct xcoff_fileline_data
364 /* The data for the next file we know about. */
365 struct xcoff_fileline_data *next;
366 /* Line number information. */
367 struct xcoff_line_vector vec;
371 /* A dummy callback function used when we can't find any debug info. */
373 static int
374 xcoff_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
375 uintptr_t pc ATTRIBUTE_UNUSED,
376 backtrace_full_callback callback ATTRIBUTE_UNUSED,
377 backtrace_error_callback error_callback, void *data)
379 error_callback (data, "no debug info in XCOFF executable", -1);
380 return 0;
383 /* A dummy callback function used when we can't find a symbol
384 table. */
386 static void
387 xcoff_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
388 uintptr_t addr ATTRIBUTE_UNUSED,
389 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
390 backtrace_error_callback error_callback, void *data)
392 error_callback (data, "no symbol table in XCOFF executable", -1);
395 /* Compare struct xcoff_symbol for qsort. */
397 static int
398 xcoff_symbol_compare (const void *v1, const void *v2)
400 const struct xcoff_symbol *e1 = (const struct xcoff_symbol *) v1;
401 const struct xcoff_symbol *e2 = (const struct xcoff_symbol *) v2;
403 if (e1->address < e2->address)
404 return -1;
405 else if (e1->address > e2->address)
406 return 1;
407 else
408 return 0;
411 /* Compare an ADDR against an xcoff_symbol for bsearch. */
413 static int
414 xcoff_symbol_search (const void *vkey, const void *ventry)
416 const uintptr_t *key = (const uintptr_t *) vkey;
417 const struct xcoff_symbol *entry = (const struct xcoff_symbol *) ventry;
418 uintptr_t addr;
420 addr = *key;
421 if (addr < entry->address)
422 return -1;
423 else if ((entry->size == 0 && addr > entry->address)
424 || (entry->size > 0 && addr >= entry->address + entry->size))
425 return 1;
426 else
427 return 0;
430 /* Add XDATA to the list in STATE. */
432 static void
433 xcoff_add_syminfo_data (struct backtrace_state *state,
434 struct xcoff_syminfo_data *xdata)
436 if (!state->threaded)
438 struct xcoff_syminfo_data **pp;
440 for (pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
441 *pp != NULL;
442 pp = &(*pp)->next)
444 *pp = xdata;
446 else
448 while (1)
450 struct xcoff_syminfo_data **pp;
452 pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
454 while (1)
456 struct xcoff_syminfo_data *p;
458 p = backtrace_atomic_load_pointer (pp);
460 if (p == NULL)
461 break;
463 pp = &p->next;
466 if (__sync_bool_compare_and_swap (pp, NULL, xdata))
467 break;
472 /* Return the symbol name and value for an ADDR. */
474 static void
475 xcoff_syminfo (struct backtrace_state *state ATTRIBUTE_UNUSED, uintptr_t addr,
476 backtrace_syminfo_callback callback,
477 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
478 void *data)
480 struct xcoff_syminfo_data *edata;
481 struct xcoff_symbol *sym = NULL;
483 if (!state->threaded)
485 for (edata = (struct xcoff_syminfo_data *) state->syminfo_data;
486 edata != NULL;
487 edata = edata->next)
489 sym = ((struct xcoff_symbol *)
490 bsearch (&addr, edata->symbols, edata->count,
491 sizeof (struct xcoff_symbol), xcoff_symbol_search));
492 if (sym != NULL)
493 break;
496 else
498 struct xcoff_syminfo_data **pp;
500 pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
501 while (1)
503 edata = backtrace_atomic_load_pointer (pp);
504 if (edata == NULL)
505 break;
507 sym = ((struct xcoff_symbol *)
508 bsearch (&addr, edata->symbols, edata->count,
509 sizeof (struct xcoff_symbol), xcoff_symbol_search));
510 if (sym != NULL)
511 break;
513 pp = &edata->next;
517 if (sym == NULL)
518 callback (data, addr, NULL, 0, 0);
519 else
520 callback (data, addr, sym->name, sym->address, sym->size);
523 /* Return the name of an XCOFF symbol. */
525 static const char *
526 xcoff_symname (const b_xcoff_syment *asym,
527 const unsigned char *strtab, size_t strtab_size)
529 #if BACKTRACE_XCOFF_SIZE == 32
530 if (asym->n_zeroes != 0)
532 /* Make a copy as we will release the symtab view. */
533 char name[SYMNMLEN+1];
534 strncpy (name, asym->n_name, SYMNMLEN);
535 name[SYMNMLEN] = '\0';
536 return strdup (name);
538 #endif
539 if (asym->n_sclass & 0x80)
540 return NULL; /* .debug */
541 if (asym->n_offset_ >= strtab_size)
542 return NULL;
543 return (const char *) strtab + asym->n_offset_;
546 /* Initialize the symbol table info for xcoff_syminfo. */
548 static int
549 xcoff_initialize_syminfo (struct backtrace_state *state,
550 uintptr_t base_address,
551 const b_xcoff_scnhdr *sects,
552 const b_xcoff_syment *syms, size_t nsyms,
553 const unsigned char *strtab, size_t strtab_size,
554 backtrace_error_callback error_callback, void *data,
555 struct xcoff_syminfo_data *sdata)
557 size_t xcoff_symbol_count;
558 size_t xcoff_symbol_size;
559 struct xcoff_symbol *xcoff_symbols;
560 size_t i;
561 unsigned int j;
563 /* We only care about function symbols. Count them. */
564 xcoff_symbol_count = 0;
565 for (i = 0; i < nsyms; ++i)
567 const b_xcoff_syment *asym = &syms[i];
568 if ((asym->n_sclass == C_EXT || asym->n_sclass == C_HIDEXT
569 || asym->n_sclass == C_WEAKEXT)
570 && ISFCN (asym->n_type) && asym->n_numaux > 0 && asym->n_scnum > 0)
571 ++xcoff_symbol_count;
573 i += asym->n_numaux;
576 xcoff_symbol_size = xcoff_symbol_count * sizeof (struct xcoff_symbol);
577 xcoff_symbols = ((struct xcoff_symbol *)
578 backtrace_alloc (state, xcoff_symbol_size, error_callback,
579 data));
580 if (xcoff_symbols == NULL)
581 return 0;
583 j = 0;
584 for (i = 0; i < nsyms; ++i)
586 const b_xcoff_syment *asym = &syms[i];
587 if ((asym->n_sclass == C_EXT || asym->n_sclass == C_HIDEXT
588 || asym->n_sclass == C_WEAKEXT)
589 && ISFCN (asym->n_type) && asym->n_numaux > 0 && asym->n_scnum > 0)
591 const b_xcoff_auxent *aux = (const b_xcoff_auxent *) (asym + 1);
592 xcoff_symbols[j].name = xcoff_symname (asym, strtab, strtab_size);
593 xcoff_symbols[j].address = base_address + asym->n_value
594 - sects[asym->n_scnum - 1].s_paddr;
595 /* x_fsize will be 0 if there is no debug information. */
596 xcoff_symbols[j].size = aux->x_fcn.x_fsize;
597 ++j;
600 i += asym->n_numaux;
603 backtrace_qsort (xcoff_symbols, xcoff_symbol_count,
604 sizeof (struct xcoff_symbol), xcoff_symbol_compare);
606 sdata->next = NULL;
607 sdata->symbols = xcoff_symbols;
608 sdata->count = xcoff_symbol_count;
610 return 1;
613 /* Compare struct xcoff_line for qsort. */
615 static int
616 xcoff_line_compare (const void *v1, const void *v2)
618 const struct xcoff_line *ln1 = (const struct xcoff_line *) v1;
619 const struct xcoff_line *ln2 = (const struct xcoff_line *) v2;
621 if (ln1->pc < ln2->pc)
622 return -1;
623 else if (ln1->pc > ln2->pc)
624 return 1;
625 else
626 return 0;
629 /* Find a PC in a line vector. We always allocate an extra entry at
630 the end of the lines vector, so that this routine can safely look
631 at the next entry. */
633 static int
634 xcoff_line_search (const void *vkey, const void *ventry)
636 const uintptr_t *key = (const uintptr_t *) vkey;
637 const struct xcoff_line *entry = (const struct xcoff_line *) ventry;
638 uintptr_t pc;
640 pc = *key;
641 if (pc < entry->pc)
642 return -1;
643 else if ((entry + 1)->pc == (uintptr_t) -1 || pc >= (entry + 1)->pc)
644 return 1;
645 else
646 return 0;
649 /* Look for a PC in the line vector for one module. On success,
650 call CALLBACK and return whatever it returns. On error, call
651 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
652 0 if not. */
654 static int
655 xcoff_lookup_pc (struct backtrace_state *state ATTRIBUTE_UNUSED,
656 struct xcoff_fileline_data *fdata, uintptr_t pc,
657 backtrace_full_callback callback,
658 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
659 void *data, int *found)
661 const struct xcoff_line *ln;
662 const char *function;
664 *found = 1;
666 ln = (struct xcoff_line *) bsearch (&pc, fdata->vec.vec.base,
667 fdata->vec.count,
668 sizeof (struct xcoff_line),
669 xcoff_line_search);
670 if (ln == NULL)
672 *found = 0;
673 return 0;
676 function = ln->function;
677 /* AIX prepends a '.' to function entry points, remove it. */
678 if (*function == '.')
679 ++function;
680 return callback (data, pc, ln->filename, ln->lineno, function);
683 /* Return the file/line information for a PC using the XCOFF lineno
684 mapping we built earlier. */
686 static int
687 xcoff_fileline (struct backtrace_state *state, uintptr_t pc,
688 backtrace_full_callback callback,
689 backtrace_error_callback error_callback, void *data)
692 struct xcoff_fileline_data *fdata;
693 int found;
694 int ret;
696 if (!state->threaded)
698 for (fdata = (struct xcoff_fileline_data *) state->fileline_data;
699 fdata != NULL;
700 fdata = fdata->next)
702 ret = xcoff_lookup_pc (state, fdata, pc, callback, error_callback,
703 data, &found);
704 if (ret != 0 || found)
705 return ret;
708 else
710 struct xcoff_fileline_data **pp;
712 pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
713 while (1)
715 fdata = backtrace_atomic_load_pointer (pp);
716 if (fdata == NULL)
717 break;
719 ret = xcoff_lookup_pc (state, fdata, pc, callback, error_callback,
720 data, &found);
721 if (ret != 0 || found)
722 return ret;
724 pp = &fdata->next;
728 /* FIXME: See if any libraries have been dlopen'ed. */
730 return callback (data, pc, NULL, 0, NULL);
733 /* Add a new mapping to the vector of line mappings that we are
734 building. Returns 1 on success, 0 on failure. */
736 static int
737 xcoff_add_line (struct backtrace_state *state, uintptr_t pc,
738 const char *filename, const char *function, uint32_t lnno,
739 backtrace_error_callback error_callback, void *data,
740 struct xcoff_line_vector *vec)
742 struct xcoff_line *ln;
744 ln = ((struct xcoff_line *)
745 backtrace_vector_grow (state, sizeof (struct xcoff_line),
746 error_callback, data, &vec->vec));
747 if (ln == NULL)
748 return 0;
750 ln->pc = pc;
751 ln->filename = filename;
752 ln->function = function;
753 ln->lineno = lnno;
755 ++vec->count;
757 return 1;
760 /* Add the line number entries for a function to the line vector. */
762 static int
763 xcoff_process_linenos (struct backtrace_state *state, uintptr_t base_address,
764 const b_xcoff_syment *fsym, const char *filename,
765 const b_xcoff_scnhdr *sects,
766 const unsigned char *strtab, size_t strtab_size,
767 uint32_t fcn_lnno, struct xcoff_incl_vector *vec,
768 struct xcoff_line_vector *lvec,
769 const unsigned char *linenos, size_t linenos_size,
770 uintptr_t lnnoptr0,
771 backtrace_error_callback error_callback, void *data)
773 const b_xcoff_auxent *aux;
774 const b_xcoff_lineno *lineno;
775 const unsigned char *lineptr;
776 const char *function;
777 struct xcoff_incl *incl = NULL;
778 uintptr_t lnnoptr;
779 uintptr_t pc;
780 uint32_t lnno;
781 int begincl;
782 size_t i;
784 aux = (const b_xcoff_auxent *) (fsym + 1);
785 lnnoptr = aux->x_fcn.x_lnnoptr;
787 if (lnnoptr < lnnoptr0 || lnnoptr + LINESZ > lnnoptr0 + linenos_size)
788 return 0;
790 function = xcoff_symname (fsym, strtab, strtab_size);
791 if (function == NULL)
792 return 0;
794 /* Skip first entry that points to symtab. */
796 lnnoptr += LINESZ;
798 lineptr = linenos + (lnnoptr - lnnoptr0);
800 begincl = -1;
801 while (lineptr + LINESZ <= linenos + linenos_size)
803 lineno = (const b_xcoff_lineno *) lineptr;
805 lnno = lineno->l_lnno;
806 if (lnno == 0)
807 break;
809 /* If part of a function other than the beginning comes from an
810 include file, the line numbers are absolute, rather than
811 relative to the beginning of the function. */
812 for (i = 0; i < vec->count; ++i)
814 incl = (struct xcoff_incl *) vec->vec.base + i;
815 if (incl->begin <= lnnoptr && lnnoptr <= incl->end)
816 break;
818 if (begincl == -1)
819 begincl = (i < vec->count);
820 if (i < vec->count)
822 filename = incl->filename;
823 if (begincl == 1)
824 lnno += fcn_lnno - 1;
826 else
827 lnno += fcn_lnno - 1;
829 pc = base_address + lineno->l_addr.l_paddr
830 - sects[fsym->n_scnum - 1].s_paddr;
831 xcoff_add_line (state, pc, filename, function, lnno, error_callback,
832 data, lvec);
834 lnnoptr += LINESZ;
835 lineptr += LINESZ;
838 return 1;
841 /* Initialize the line vector info for xcoff_fileline. */
843 static int
844 xcoff_initialize_fileline (struct backtrace_state *state,
845 uintptr_t base_address,
846 const b_xcoff_scnhdr *sects,
847 const b_xcoff_syment *syms, size_t nsyms,
848 const unsigned char *strtab, size_t strtab_size,
849 const unsigned char *linenos, size_t linenos_size,
850 uint64_t lnnoptr0,
851 backtrace_error_callback error_callback, void *data)
853 struct xcoff_fileline_data *fdata;
854 struct xcoff_incl_vector vec;
855 struct xcoff_line *ln;
856 const b_xcoff_syment *fsym;
857 const b_xcoff_auxent *aux;
858 const char *filename;
859 const char *name;
860 struct xcoff_incl *incl;
861 uintptr_t begin, end;
862 uintptr_t lnno;
863 size_t i;
865 fdata = ((struct xcoff_fileline_data *)
866 backtrace_alloc (state, sizeof (struct xcoff_fileline_data),
867 error_callback, data));
868 if (fdata == NULL)
869 return 0;
871 memset (fdata, 0, sizeof *fdata);
872 memset (&vec, 0, sizeof vec);
874 /* Process include files first. */
876 begin = 0;
877 for (i = 0; i < nsyms; ++i)
879 const b_xcoff_syment *asym = &syms[i];
881 switch (asym->n_sclass)
883 case C_BINCL:
884 begin = asym->n_value;
885 break;
887 case C_EINCL:
888 if (begin == 0)
889 break;
890 end = asym->n_value;
891 incl = ((struct xcoff_incl *)
892 backtrace_vector_grow (state, sizeof (struct xcoff_incl),
893 error_callback, data, &vec.vec));
894 if (incl != NULL)
896 incl->filename = xcoff_symname (asym, strtab, strtab_size);
897 incl->begin = begin;
898 incl->end = end;
899 ++vec.count;
901 begin = 0;
902 break;
905 i += asym->n_numaux;
908 filename = NULL;
909 fsym = NULL;
910 for (i = 0; i < nsyms; ++i)
912 const b_xcoff_syment *asym = &syms[i];
914 switch (asym->n_sclass)
916 case C_FILE:
917 filename = xcoff_symname (asym, strtab, strtab_size);
918 if (filename == NULL)
919 break;
921 /* If the file auxiliary entry is not used, the symbol name is
922 the name of the source file. If the file auxiliary entry is
923 used, then the symbol name should be .file, and the first
924 file auxiliary entry (by convention) contains the source
925 file name. */
927 if (asym->n_numaux > 0 && !strcmp (filename, ".file"))
929 aux = (const b_xcoff_auxent *) (asym + 1);
930 if (aux->x_file._x.x_zeroes != 0)
932 /* Make a copy as we will release the symtab view. */
933 char name[FILNMLEN+1];
934 strncpy (name, aux->x_file.x_fname, FILNMLEN);
935 name[FILNMLEN] = '\0';
936 filename = strdup (name);
938 else if (aux->x_file._x.x_offset < strtab_size)
939 filename = (const char *) strtab + aux->x_file._x.x_offset;
940 else
941 filename = NULL;
943 break;
945 case C_EXT:
946 case C_HIDEXT:
947 case C_WEAKEXT:
948 fsym = NULL;
949 if (!ISFCN (asym->n_type) || asym->n_numaux == 0)
950 break;
951 if (filename == NULL)
952 break;
953 fsym = asym;
954 break;
956 case C_FCN:
957 if (asym->n_numaux == 0)
958 break;
959 if (fsym == NULL)
960 break;
961 name = xcoff_symname (asym, strtab, strtab_size);
962 if (name == NULL)
963 break;
964 aux = (const b_xcoff_auxent *) (asym + 1);
965 #if BACKTRACE_XCOFF_SIZE == 32
966 lnno = (uint32_t) aux->x_block.x_lnnohi << 16
967 | aux->x_block.x_lnno;
968 #else
969 lnno = aux->x_block.x_lnno;
970 #endif
971 if (!strcmp (name, ".bf"))
973 xcoff_process_linenos (state, base_address, fsym, filename,
974 sects, strtab, strtab_size, lnno, &vec,
975 &fdata->vec, linenos, linenos_size,
976 lnnoptr0, error_callback, data);
978 else if (!strcmp (name, ".ef"))
980 fsym = NULL;
982 break;
985 i += asym->n_numaux;
988 /* Allocate one extra entry at the end. */
989 ln = ((struct xcoff_line *)
990 backtrace_vector_grow (state, sizeof (struct xcoff_line),
991 error_callback, data, &fdata->vec.vec));
992 if (ln == NULL)
993 goto fail;
994 ln->pc = (uintptr_t) -1;
995 ln->filename = NULL;
996 ln->function = NULL;
997 ln->lineno = 0;
999 if (!backtrace_vector_release (state, &fdata->vec.vec, error_callback, data))
1000 goto fail;
1002 backtrace_qsort (fdata->vec.vec.base, fdata->vec.count,
1003 sizeof (struct xcoff_line), xcoff_line_compare);
1005 if (!state->threaded)
1007 struct xcoff_fileline_data **pp;
1009 for (pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
1010 *pp != NULL;
1011 pp = &(*pp)->next)
1013 *pp = fdata;
1015 else
1017 while (1)
1019 struct xcoff_fileline_data **pp;
1021 pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
1023 while (1)
1025 struct xcoff_fileline_data *p;
1027 p = backtrace_atomic_load_pointer (pp);
1029 if (p == NULL)
1030 break;
1032 pp = &p->next;
1035 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
1036 break;
1040 return 1;
1042 fail:
1043 return 0;
1046 /* Add the backtrace data for one XCOFF file. Returns 1 on success,
1047 0 on failure (in both cases descriptor is closed). */
1049 static int
1050 xcoff_add (struct backtrace_state *state, int descriptor, off_t offset,
1051 uintptr_t base_address, backtrace_error_callback error_callback,
1052 void *data, fileline *fileline_fn, int *found_sym, int exe)
1054 struct backtrace_view fhdr_view;
1055 struct backtrace_view sects_view;
1056 struct backtrace_view linenos_view;
1057 struct backtrace_view syms_view;
1058 struct backtrace_view str_view;
1059 b_xcoff_filhdr fhdr;
1060 const b_xcoff_scnhdr *sects;
1061 const b_xcoff_scnhdr *stext;
1062 uint64_t lnnoptr;
1063 uint32_t nlnno;
1064 off_t str_off;
1065 size_t sects_size;
1066 size_t syms_size;
1067 int32_t str_size;
1068 int sects_view_valid;
1069 int linenos_view_valid;
1070 int syms_view_valid;
1071 int str_view_valid;
1072 int magic_ok;
1073 int i;
1075 *found_sym = 0;
1077 sects_view_valid = 0;
1078 linenos_view_valid = 0;
1079 syms_view_valid = 0;
1080 str_view_valid = 0;
1082 /* Map the XCOFF file header. */
1083 if (!backtrace_get_view (state, descriptor, offset, sizeof (b_xcoff_filhdr),
1084 error_callback, data, &fhdr_view))
1085 goto fail;
1087 memcpy (&fhdr, fhdr_view.data, sizeof fhdr);
1088 magic_ok = (fhdr.f_magic == XCOFF_MAGIC);
1090 backtrace_release_view (state, &fhdr_view, error_callback, data);
1092 if (!magic_ok)
1094 if (exe)
1095 error_callback (data, "executable file is not XCOFF", 0);
1096 goto fail;
1099 /* Verify object is of expected type. */
1100 if ((exe && (fhdr.f_flags & F_SHROBJ))
1101 || (!exe && !(fhdr.f_flags & F_SHROBJ)))
1102 goto fail;
1104 /* Read the section headers. */
1106 sects_size = fhdr.f_nscns * sizeof (b_xcoff_scnhdr);
1108 if (!backtrace_get_view (state, descriptor,
1109 offset + sizeof (fhdr) + fhdr.f_opthdr,
1110 sects_size, error_callback, data, &sects_view))
1111 goto fail;
1112 sects_view_valid = 1;
1113 sects = (const b_xcoff_scnhdr *) sects_view.data;
1115 /* FIXME: assumes only one .text section. */
1116 for (i = 0; i < fhdr.f_nscns; ++i)
1117 if ((sects[i].s_flags & 0xffff) == STYP_TEXT)
1118 break;
1119 if (i == fhdr.f_nscns)
1120 goto fail;
1122 stext = &sects[i];
1124 /* AIX ldinfo_textorg includes the XCOFF headers. */
1125 base_address = (exe ? XCOFF_AIX_TEXTBASE : base_address) + stext->s_scnptr;
1127 lnnoptr = stext->s_lnnoptr;
1128 nlnno = stext->s_nlnno;
1130 #if BACKTRACE_XCOFF_SIZE == 32
1131 if (nlnno == _OVERFLOW_MARKER)
1133 int sntext = i + 1;
1134 /* Find the matching .ovrflo section. */
1135 for (i = 0; i < fhdr.f_nscns; ++i)
1137 if (((sects[i].s_flags & 0xffff) == STYP_OVRFLO)
1138 && sects[i].s_nlnno == sntext)
1140 nlnno = sects[i].s_vaddr;
1141 break;
1145 #endif
1147 /* Read the symbol table and the string table. */
1149 if (fhdr.f_symptr != 0)
1151 struct xcoff_syminfo_data *sdata;
1153 /* Symbol table is followed by the string table. The string table
1154 starts with its length (on 4 bytes).
1155 Map the symbol table and the length of the string table. */
1156 syms_size = fhdr.f_nsyms * sizeof (b_xcoff_syment);
1158 if (!backtrace_get_view (state, descriptor, offset + fhdr.f_symptr,
1159 syms_size + 4, error_callback, data,
1160 &syms_view))
1161 goto fail;
1162 syms_view_valid = 1;
1164 memcpy (&str_size, syms_view.data + syms_size, 4);
1166 str_off = fhdr.f_symptr + syms_size;
1168 if (str_size > 4)
1170 /* Map string table (including the length word). */
1172 if (!backtrace_get_view (state, descriptor, offset + str_off,
1173 str_size, error_callback, data, &str_view))
1174 goto fail;
1175 str_view_valid = 1;
1178 sdata = ((struct xcoff_syminfo_data *)
1179 backtrace_alloc (state, sizeof *sdata, error_callback, data));
1180 if (sdata == NULL)
1181 goto fail;
1183 if (!xcoff_initialize_syminfo (state, base_address, sects,
1184 syms_view.data, fhdr.f_nsyms,
1185 str_view.data, str_size,
1186 error_callback, data, sdata))
1188 backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
1189 goto fail;
1192 *found_sym = 1;
1194 xcoff_add_syminfo_data (state, sdata);
1197 /* Read the line number entries. */
1199 if (fhdr.f_symptr != 0 && lnnoptr != 0)
1201 size_t linenos_size = (size_t) nlnno * LINESZ;
1203 if (!backtrace_get_view (state, descriptor, offset + lnnoptr,
1204 linenos_size,
1205 error_callback, data, &linenos_view))
1206 goto fail;
1207 linenos_view_valid = 1;
1209 if (xcoff_initialize_fileline (state, base_address, sects,
1210 syms_view.data, fhdr.f_nsyms,
1211 str_view.data, str_size,
1212 linenos_view.data, linenos_size,
1213 lnnoptr, error_callback, data))
1214 *fileline_fn = xcoff_fileline;
1216 backtrace_release_view (state, &linenos_view, error_callback, data);
1217 linenos_view_valid = 0;
1220 backtrace_release_view (state, &sects_view, error_callback, data);
1221 sects_view_valid = 0;
1222 if (syms_view_valid)
1223 backtrace_release_view (state, &syms_view, error_callback, data);
1224 syms_view_valid = 0;
1226 /* We've read all we need from the executable. */
1227 if (!backtrace_close (descriptor, error_callback, data))
1228 goto fail;
1229 descriptor = -1;
1231 return 1;
1233 fail:
1234 if (sects_view_valid)
1235 backtrace_release_view (state, &sects_view, error_callback, data);
1236 if (str_view_valid)
1237 backtrace_release_view (state, &str_view, error_callback, data);
1238 if (syms_view_valid)
1239 backtrace_release_view (state, &syms_view, error_callback, data);
1240 if (linenos_view_valid)
1241 backtrace_release_view (state, &linenos_view, error_callback, data);
1242 if (descriptor != -1 && offset == 0)
1243 backtrace_close (descriptor, error_callback, data);
1244 return 0;
1247 #ifdef HAVE_LOADQUERY
1249 /* Read an integer value in human-readable format from an AIX
1250 big archive fixed-length or member header. */
1252 static int
1253 xcoff_parse_decimal (const char *buf, size_t size, off_t *off)
1255 char str[32];
1256 char *end;
1258 if (size >= sizeof str)
1259 return 0;
1260 memcpy (str, buf, size);
1261 str[size] = '\0';
1262 *off = strtol (str, &end, 10);
1263 if (*end != '\0' && *end != ' ')
1264 return 0;
1266 return 1;
1269 /* Add the backtrace data for a member of an AIX big archive.
1270 Returns 1 on success, 0 on failure. */
1272 static int
1273 xcoff_armem_add (struct backtrace_state *state, int descriptor,
1274 uintptr_t base_address, const char *member,
1275 backtrace_error_callback error_callback, void *data,
1276 fileline *fileline_fn, int *found_sym)
1278 struct backtrace_view view;
1279 b_ar_fl_hdr fl_hdr;
1280 const b_ar_hdr *ar_hdr;
1281 off_t off;
1282 off_t len;
1283 int memlen;
1285 *found_sym = 0;
1287 /* Map archive fixed-length header. */
1289 if (!backtrace_get_view (state, descriptor, 0, sizeof (b_ar_fl_hdr),
1290 error_callback, data, &view))
1291 goto fail;
1293 memcpy (&fl_hdr, view.data, sizeof (b_ar_fl_hdr));
1295 backtrace_release_view (state, &view, error_callback, data);
1297 if (memcmp (fl_hdr.fl_magic, AIAMAGBIG, 8) != 0)
1298 goto fail;
1300 memlen = strlen (member);
1302 /* Read offset of first archive member. */
1303 if (!xcoff_parse_decimal (fl_hdr.fl_fstmoff, sizeof fl_hdr.fl_fstmoff, &off))
1304 goto fail;
1305 while (off != 0)
1307 /* Map archive member header and member name. */
1309 if (!backtrace_get_view (state, descriptor, off,
1310 sizeof (b_ar_hdr) + memlen,
1311 error_callback, data, &view))
1312 break;
1314 ar_hdr = (const b_ar_hdr *) view.data;
1316 /* Read archive member name length. */
1317 if (!xcoff_parse_decimal (ar_hdr->ar_namlen, sizeof ar_hdr->ar_namlen,
1318 &len))
1320 backtrace_release_view (state, &view, error_callback, data);
1321 break;
1323 if (len == memlen && !memcmp (ar_hdr->ar_name, member, memlen))
1325 off = (off + sizeof (b_ar_hdr) + memlen + 1) & ~1;
1327 /* The archive can contain several members with the same name
1328 (e.g. 32-bit and 64-bit), so continue if not ok. */
1330 if (xcoff_add (state, descriptor, off, base_address, error_callback,
1331 data, fileline_fn, found_sym, 0))
1333 backtrace_release_view (state, &view, error_callback, data);
1334 return 1;
1338 /* Read offset of next archive member. */
1339 if (!xcoff_parse_decimal (ar_hdr->ar_nxtmem, sizeof ar_hdr->ar_nxtmem,
1340 &off))
1342 backtrace_release_view (state, &view, error_callback, data);
1343 break;
1345 backtrace_release_view (state, &view, error_callback, data);
1348 fail:
1349 /* No matching member found. */
1350 backtrace_close (descriptor, error_callback, data);
1351 return 0;
1354 /* Add the backtrace data for dynamically loaded libraries. */
1356 static void
1357 xcoff_add_shared_libs (struct backtrace_state *state,
1358 backtrace_error_callback error_callback,
1359 void *data, fileline *fileline_fn, int *found_sym)
1361 const struct ld_info *ldinfo;
1362 void *buf;
1363 unsigned int buflen;
1364 const char *member;
1365 int descriptor;
1366 int does_not_exist;
1367 int lib_found_sym;
1368 int ret;
1370 /* Retrieve the list of loaded libraries. */
1372 buf = NULL;
1373 buflen = 512;
1376 buf = realloc (buf, buflen);
1377 if (buf == NULL)
1379 ret = -1;
1380 break;
1382 ret = loadquery (L_GETINFO, buf, buflen);
1383 if (ret == 0)
1384 break;
1385 buflen *= 2;
1387 while (ret == -1 && errno == ENOMEM);
1388 if (ret != 0)
1390 free (buf);
1391 return;
1394 ldinfo = (const struct ld_info *) buf;
1395 while ((const char *) ldinfo < (const char *) buf + buflen)
1397 if (*ldinfo->ldinfo_filename != '/')
1398 goto next;
1400 descriptor = backtrace_open (ldinfo->ldinfo_filename, error_callback,
1401 data, &does_not_exist);
1402 if (descriptor < 0)
1403 goto next;
1405 /* Check if it is an archive (member name not empty). */
1407 member = ldinfo->ldinfo_filename + strlen (ldinfo->ldinfo_filename) + 1;
1408 if (*member)
1410 xcoff_armem_add (state, descriptor,
1411 (uintptr_t) ldinfo->ldinfo_textorg, member,
1412 error_callback, data, fileline_fn, &lib_found_sym);
1414 else
1416 xcoff_add (state, descriptor, 0, (uintptr_t) ldinfo->ldinfo_textorg,
1417 error_callback, data, fileline_fn, &lib_found_sym, 0);
1419 if (lib_found_sym)
1420 *found_sym = 1;
1422 next:
1423 if (ldinfo->ldinfo_next == 0)
1424 break;
1425 ldinfo = (const struct ld_info *) ((const char *) ldinfo
1426 + ldinfo->ldinfo_next);
1429 free (buf);
1431 #endif /* HAVE_LOADQUERY */
1433 /* Initialize the backtrace data we need from an XCOFF executable.
1434 Returns 1 on success, 0 on failure. */
1437 backtrace_initialize (struct backtrace_state *state,
1438 const char *filename ATTRIBUTE_UNUSED, int descriptor,
1439 backtrace_error_callback error_callback,
1440 void *data, fileline *fileline_fn)
1442 int ret;
1443 int found_sym;
1444 fileline xcoff_fileline_fn = xcoff_nodebug;
1446 ret = xcoff_add (state, descriptor, 0, 0, error_callback, data,
1447 &xcoff_fileline_fn, &found_sym, 1);
1448 if (!ret)
1449 return 0;
1451 #ifdef HAVE_LOADQUERY
1452 xcoff_add_shared_libs (state, error_callback, data, &xcoff_fileline_fn,
1453 &found_sym);
1454 #endif
1456 if (!state->threaded)
1458 if (found_sym)
1459 state->syminfo_fn = xcoff_syminfo;
1460 else if (state->syminfo_fn == NULL)
1461 state->syminfo_fn = xcoff_nosyms;
1463 else
1465 if (found_sym)
1466 backtrace_atomic_store_pointer (&state->syminfo_fn, xcoff_syminfo);
1467 else
1468 __sync_bool_compare_and_swap (&state->syminfo_fn, NULL, xcoff_nosyms);
1471 if (!state->threaded)
1473 if (state->fileline_fn == NULL || state->fileline_fn == xcoff_nodebug)
1474 *fileline_fn = xcoff_fileline_fn;
1476 else
1478 fileline current_fn;
1480 current_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
1481 if (current_fn == NULL || current_fn == xcoff_nodebug)
1482 *fileline_fn = xcoff_fileline_fn;
1485 return 1;