1 /* xcoff.c -- Get debug data from an XCOFF file for backtraces.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
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
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. */
38 #include <sys/types.h>
44 #include "backtrace.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
53 #if BACKTRACE_XCOFF_SIZE != 32 && BACKTRACE_XCOFF_SIZE != 64
54 #error "Unknown BACKTRACE_XCOFF_SIZE"
57 /* XCOFF file header. */
59 #if BACKTRACE_XCOFF_SIZE == 32
71 #define XCOFF_MAGIC 0737
73 #else /* BACKTRACE_XCOFF_SIZE != 32 */
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
108 #define _OVERFLOW_MARKER 65535
110 #else /* BACKTRACE_XCOFF_SIZE != 32 */
125 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
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. */
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. */
141 #if BACKTRACE_XCOFF_SIZE == 32
145 char _name
[SYMNMLEN
];
151 #define n_name _u._name
152 #define n_zeroes _u._s._zeroes
153 #define n_offset_ _u._s._offset
160 } __attribute__ ((packed
)) b_xcoff_syment
;
162 #else /* BACKTRACE_XCOFF_SIZE != 32 */
171 } __attribute__ ((packed
)) b_xcoff_syment
;
173 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
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. */
185 #define ISFCN(x) ((x) & 0x0020)
187 /* XCOFF AUX entry. */
193 #if BACKTRACE_XCOFF_SIZE == 32
205 char x_fname
[FILNMLEN
];
209 char pad
[FILNMLEN
-8];
213 #if BACKTRACE_XCOFF_SIZE == 32
228 uint8_t pad
[AUXESZ
-1];
231 } __attribute__ ((packed
)) b_xcoff_auxent
;
233 /* XCOFF line number entry. */
235 #if BACKTRACE_XCOFF_SIZE == 32
247 #else /* BACKTRACE_XCOFF_SIZE != 32 */
259 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
261 #if BACKTRACE_XCOFF_SIZE == 32
262 #define XCOFF_AIX_TEXTBASE 0x10000000u
264 #define XCOFF_AIX_TEXTBASE 0x100000000ul
267 /* AIX big archive fixed-length header. */
269 #define AIAMAGBIG "<bigaf>\n"
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. */
280 /* AIX big archive file member header. */
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. */
295 /* Information we keep for an XCOFF symbol. */
299 /* The name of the symbol. */
301 /* The address of the symbol. */
303 /* The size of the symbol. */
307 /* Information to pass to xcoff_syminfo. */
309 struct xcoff_syminfo_data
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. */
319 /* Information about an include file. */
324 const char *filename
;
325 /* Offset to first line number from the include file. */
327 /* Offset to last line number from the include file. */
331 /* A growable vector of include files information. */
333 struct xcoff_incl_vector
335 /* Memory. This is an array of struct xcoff_incl. */
336 struct backtrace_vector vec
;
337 /* Number of include files. */
341 /* A growable vector of functions information. */
347 /* The size of the function. */
352 const char *filename
;
353 /* Pointer to first lnno entry. */
355 /* Base address of containing section. */
357 /* Starting source line number. */
361 /* A growable vector of function information. This is used while
362 reading the function symbols. */
364 struct xcoff_func_vector
366 /* Memory. This is an array of struct xcoff_func. */
367 struct backtrace_vector vec
;
368 /* Number of valid mappings. */
372 /* The information we need to map a PC to a file and line. */
374 struct xcoff_fileline_data
376 /* The data for the next file we know about. */
377 struct xcoff_fileline_data
*next
;
378 /* Functions information. */
379 struct xcoff_func_vector func_vec
;
380 /* Include files information. */
381 struct xcoff_incl_vector incl_vec
;
382 /* Line numbers information. */
383 const unsigned char *linenos
;
386 /* Loader address. */
387 uintptr_t base_address
;
390 /* An index of DWARF sections we care about. */
402 /* Information we gather for the DWARF sections we care about. */
406 /* Section file offset. */
410 /* Section contents, after read from file. */
411 const unsigned char *data
;
414 /* A dummy callback function used when we can't find any debug info. */
417 xcoff_nodebug (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
418 uintptr_t pc ATTRIBUTE_UNUSED
,
419 backtrace_full_callback callback ATTRIBUTE_UNUSED
,
420 backtrace_error_callback error_callback
, void *data
)
422 error_callback (data
, "no debug info in XCOFF executable", -1);
426 /* A dummy callback function used when we can't find a symbol
430 xcoff_nosyms (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
431 uintptr_t addr ATTRIBUTE_UNUSED
,
432 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED
,
433 backtrace_error_callback error_callback
, void *data
)
435 error_callback (data
, "no symbol table in XCOFF executable", -1);
438 /* Compare struct xcoff_symbol for qsort. */
441 xcoff_symbol_compare (const void *v1
, const void *v2
)
443 const struct xcoff_symbol
*e1
= (const struct xcoff_symbol
*) v1
;
444 const struct xcoff_symbol
*e2
= (const struct xcoff_symbol
*) v2
;
446 if (e1
->address
< e2
->address
)
448 else if (e1
->address
> e2
->address
)
454 /* Compare an ADDR against an xcoff_symbol for bsearch. */
457 xcoff_symbol_search (const void *vkey
, const void *ventry
)
459 const uintptr_t *key
= (const uintptr_t *) vkey
;
460 const struct xcoff_symbol
*entry
= (const struct xcoff_symbol
*) ventry
;
464 if (addr
< entry
->address
)
466 else if ((entry
->size
== 0 && addr
> entry
->address
)
467 || (entry
->size
> 0 && addr
>= entry
->address
+ entry
->size
))
473 /* Add XDATA to the list in STATE. */
476 xcoff_add_syminfo_data (struct backtrace_state
*state
,
477 struct xcoff_syminfo_data
*xdata
)
479 if (!state
->threaded
)
481 struct xcoff_syminfo_data
**pp
;
483 for (pp
= (struct xcoff_syminfo_data
**) (void *) &state
->syminfo_data
;
493 struct xcoff_syminfo_data
**pp
;
495 pp
= (struct xcoff_syminfo_data
**) (void *) &state
->syminfo_data
;
499 struct xcoff_syminfo_data
*p
;
501 p
= backtrace_atomic_load_pointer (pp
);
509 if (__sync_bool_compare_and_swap (pp
, NULL
, xdata
))
515 /* Return the symbol name and value for an ADDR. */
518 xcoff_syminfo (struct backtrace_state
*state ATTRIBUTE_UNUSED
, uintptr_t addr
,
519 backtrace_syminfo_callback callback
,
520 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
523 struct xcoff_syminfo_data
*edata
;
524 struct xcoff_symbol
*sym
= NULL
;
527 if (!state
->threaded
)
529 for (edata
= (struct xcoff_syminfo_data
*) state
->syminfo_data
;
533 sym
= ((struct xcoff_symbol
*)
534 bsearch (&addr
, edata
->symbols
, edata
->count
,
535 sizeof (struct xcoff_symbol
), xcoff_symbol_search
));
542 struct xcoff_syminfo_data
**pp
;
544 pp
= (struct xcoff_syminfo_data
**) (void *) &state
->syminfo_data
;
547 edata
= backtrace_atomic_load_pointer (pp
);
551 sym
= ((struct xcoff_symbol
*)
552 bsearch (&addr
, edata
->symbols
, edata
->count
,
553 sizeof (struct xcoff_symbol
), xcoff_symbol_search
));
562 callback (data
, addr
, NULL
, 0, 0);
566 /* AIX prepends a '.' to function entry points, remove it. */
567 if (name
&& *name
== '.')
569 callback (data
, addr
, name
, sym
->address
, sym
->size
);
573 /* Return the name of an XCOFF symbol. */
576 xcoff_symname (const b_xcoff_syment
*asym
,
577 const unsigned char *strtab
, size_t strtab_size
)
579 #if BACKTRACE_XCOFF_SIZE == 32
580 if (asym
->n_zeroes
!= 0)
582 /* Make a copy as we will release the symtab view. */
583 char name
[SYMNMLEN
+1];
584 strncpy (name
, asym
->n_name
, SYMNMLEN
);
585 name
[SYMNMLEN
] = '\0';
586 return strdup (name
);
589 if (asym
->n_sclass
& 0x80)
590 return NULL
; /* .debug */
591 if (asym
->n_offset_
>= strtab_size
)
593 return (const char *) strtab
+ asym
->n_offset_
;
596 /* Initialize the symbol table info for xcoff_syminfo. */
599 xcoff_initialize_syminfo (struct backtrace_state
*state
,
600 uintptr_t base_address
,
601 const b_xcoff_scnhdr
*sects
,
602 const b_xcoff_syment
*syms
, size_t nsyms
,
603 const unsigned char *strtab
, size_t strtab_size
,
604 backtrace_error_callback error_callback
, void *data
,
605 struct xcoff_syminfo_data
*sdata
)
607 size_t xcoff_symbol_count
;
608 size_t xcoff_symbol_size
;
609 struct xcoff_symbol
*xcoff_symbols
;
613 /* We only care about function symbols. Count them. */
614 xcoff_symbol_count
= 0;
615 for (i
= 0; i
< nsyms
; ++i
)
617 const b_xcoff_syment
*asym
= &syms
[i
];
618 if ((asym
->n_sclass
== C_EXT
|| asym
->n_sclass
== C_HIDEXT
619 || asym
->n_sclass
== C_WEAKEXT
)
620 && ISFCN (asym
->n_type
) && asym
->n_numaux
> 0 && asym
->n_scnum
> 0)
621 ++xcoff_symbol_count
;
626 xcoff_symbol_size
= xcoff_symbol_count
* sizeof (struct xcoff_symbol
);
627 xcoff_symbols
= ((struct xcoff_symbol
*)
628 backtrace_alloc (state
, xcoff_symbol_size
, error_callback
,
630 if (xcoff_symbols
== NULL
)
634 for (i
= 0; i
< nsyms
; ++i
)
636 const b_xcoff_syment
*asym
= &syms
[i
];
637 if ((asym
->n_sclass
== C_EXT
|| asym
->n_sclass
== C_HIDEXT
638 || asym
->n_sclass
== C_WEAKEXT
)
639 && ISFCN (asym
->n_type
) && asym
->n_numaux
> 0 && asym
->n_scnum
> 0)
641 const b_xcoff_auxent
*aux
= (const b_xcoff_auxent
*) (asym
+ 1);
642 xcoff_symbols
[j
].name
= xcoff_symname (asym
, strtab
, strtab_size
);
643 xcoff_symbols
[j
].address
= base_address
+ asym
->n_value
644 - sects
[asym
->n_scnum
- 1].s_paddr
;
645 /* x_fsize will be 0 if there is no debug information. */
646 xcoff_symbols
[j
].size
= aux
->x_fcn
.x_fsize
;
653 backtrace_qsort (xcoff_symbols
, xcoff_symbol_count
,
654 sizeof (struct xcoff_symbol
), xcoff_symbol_compare
);
657 sdata
->symbols
= xcoff_symbols
;
658 sdata
->count
= xcoff_symbol_count
;
663 /* Compare struct xcoff_func for qsort. */
666 xcoff_func_compare (const void *v1
, const void *v2
)
668 const struct xcoff_func
*fn1
= (const struct xcoff_func
*) v1
;
669 const struct xcoff_func
*fn2
= (const struct xcoff_func
*) v2
;
671 if (fn1
->pc
< fn2
->pc
)
673 else if (fn1
->pc
> fn2
->pc
)
679 /* Compare a PC against an xcoff_func for bsearch. */
682 xcoff_func_search (const void *vkey
, const void *ventry
)
684 const uintptr_t *key
= (const uintptr_t *) vkey
;
685 const struct xcoff_func
*entry
= (const struct xcoff_func
*) ventry
;
691 else if ((entry
->size
== 0 && pc
> entry
->pc
)
692 || (entry
->size
> 0 && pc
>= entry
->pc
+ entry
->size
))
698 /* Compare struct xcoff_incl for qsort. */
701 xcoff_incl_compare (const void *v1
, const void *v2
)
703 const struct xcoff_incl
*in1
= (const struct xcoff_incl
*) v1
;
704 const struct xcoff_incl
*in2
= (const struct xcoff_incl
*) v2
;
706 if (in1
->begin
< in2
->begin
)
708 else if (in1
->begin
> in2
->begin
)
714 /* Find a lnnoptr in an include file. */
717 xcoff_incl_search (const void *vkey
, const void *ventry
)
719 const uintptr_t *key
= (const uintptr_t *) vkey
;
720 const struct xcoff_incl
*entry
= (const struct xcoff_incl
*) ventry
;
724 if (lnno
< entry
->begin
)
726 else if (lnno
> entry
->end
)
732 /* Look for a PC in the function vector for one module. On success,
733 call CALLBACK and return whatever it returns. On error, call
734 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
738 xcoff_lookup_pc (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
739 struct xcoff_fileline_data
*fdata
, uintptr_t pc
,
740 backtrace_full_callback callback
,
741 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
742 void *data
, int *found
)
744 const struct xcoff_incl
*incl
, *bincl
;
745 const struct xcoff_func
*fn
;
746 const b_xcoff_lineno
*lineno
;
747 const unsigned char *lineptr
;
748 const char *function
;
749 const char *filename
;
750 uintptr_t lnnoptr
, match
;
758 /* Find the function first. */
759 fn
= ((struct xcoff_func
*)
760 bsearch (&pc
, fdata
->func_vec
.vec
.base
, fdata
->func_vec
.count
,
761 sizeof (struct xcoff_func
), xcoff_func_search
));
768 filename
= fn
->filename
;
770 /* Find the line number next. */
772 /* Skip first entry that points to symtab. */
773 lnnoptr
= fn
->lnnoptr
+ LINESZ
;
776 lineptr
= fdata
->linenos
+ (lnnoptr
- fdata
->lnnoptr0
);
777 while (lineptr
+ LINESZ
<= fdata
->linenos
+ fdata
->linenos_size
)
779 lineno
= (const b_xcoff_lineno
*) lineptr
;
780 if (lineno
->l_lnno
== 0)
782 if (pc
<= fdata
->base_address
+ lineno
->l_addr
.l_paddr
- fn
->sect_base
)
785 lnno
= lineno
->l_lnno
;
791 /* If part of a function other than the beginning comes from an
792 include file, the line numbers are absolute, rather than
793 relative to the beginning of the function. */
794 incl
= ((struct xcoff_incl
*)
795 bsearch (&match
, fdata
->incl_vec
.vec
.base
,
796 fdata
->incl_vec
.count
, sizeof (struct xcoff_incl
),
800 bincl
= ((struct xcoff_incl
*)
801 bsearch (&fn
->lnnoptr
, fdata
->incl_vec
.vec
.base
,
802 fdata
->incl_vec
.count
, sizeof (struct xcoff_incl
),
804 if (bincl
!= NULL
&& strcmp (incl
->filename
, bincl
->filename
) == 0)
806 lnno
+= fn
->lnno
- 1;
808 filename
= incl
->filename
;
812 lnno
+= fn
->lnno
- 1;
816 /* AIX prepends a '.' to function entry points, remove it. */
817 if (function
!= NULL
&& *function
== '.')
819 return callback (data
, pc
, filename
, lnno
, function
);
822 /* Return the file/line information for a PC using the XCOFF lineno
823 mapping we built earlier. */
826 xcoff_fileline (struct backtrace_state
*state
, uintptr_t pc
,
827 backtrace_full_callback callback
,
828 backtrace_error_callback error_callback
, void *data
)
831 struct xcoff_fileline_data
*fdata
;
835 if (!state
->threaded
)
837 for (fdata
= (struct xcoff_fileline_data
*) state
->fileline_data
;
841 ret
= xcoff_lookup_pc (state
, fdata
, pc
, callback
, error_callback
,
843 if (ret
!= 0 || found
)
849 struct xcoff_fileline_data
**pp
;
851 pp
= (struct xcoff_fileline_data
**) (void *) &state
->fileline_data
;
854 fdata
= backtrace_atomic_load_pointer (pp
);
858 ret
= xcoff_lookup_pc (state
, fdata
, pc
, callback
, error_callback
,
860 if (ret
!= 0 || found
)
867 /* FIXME: See if any libraries have been dlopen'ed. */
869 return callback (data
, pc
, NULL
, 0, NULL
);
872 /* Initialize the function vector info for xcoff_fileline. */
875 xcoff_initialize_fileline (struct backtrace_state
*state
,
876 uintptr_t base_address
,
877 const b_xcoff_scnhdr
*sects
,
878 const b_xcoff_syment
*syms
, size_t nsyms
,
879 const unsigned char *strtab
, size_t strtab_size
,
880 const unsigned char *linenos
, size_t linenos_size
,
882 backtrace_error_callback error_callback
, void *data
)
884 struct xcoff_fileline_data
*fdata
;
885 struct xcoff_func
*fn
;
886 const b_xcoff_syment
*fsym
;
887 const b_xcoff_auxent
*aux
;
888 const char *filename
;
890 struct xcoff_incl
*incl
;
891 uintptr_t begin
, end
;
892 uintptr_t lnno
, lnnoptr
;
896 fdata
= ((struct xcoff_fileline_data
*)
897 backtrace_alloc (state
, sizeof (struct xcoff_fileline_data
),
898 error_callback
, data
));
901 memset (fdata
, 0, sizeof *fdata
);
902 fdata
->base_address
= base_address
;
903 fdata
->linenos
= linenos
;
904 fdata
->linenos_size
= linenos_size
;
905 fdata
->lnnoptr0
= lnnoptr0
;
912 for (i
= 0; i
< nsyms
; ++i
)
914 const b_xcoff_syment
*asym
= &syms
[i
];
916 switch (asym
->n_sclass
)
919 begin
= asym
->n_value
;
926 incl
= ((struct xcoff_incl
*)
927 backtrace_vector_grow (state
, sizeof (struct xcoff_incl
),
928 error_callback
, data
,
929 &fdata
->incl_vec
.vec
));
932 incl
->filename
= xcoff_symname (asym
, strtab
, strtab_size
);
935 ++fdata
->incl_vec
.count
;
941 filename
= xcoff_symname (asym
, strtab
, strtab_size
);
942 if (filename
== NULL
)
945 /* If the file auxiliary entry is not used, the symbol name is
946 the name of the source file. If the file auxiliary entry is
947 used, then the symbol name should be .file, and the first
948 file auxiliary entry (by convention) contains the source
951 if (asym
->n_numaux
> 0 && strcmp (filename
, ".file") == 0)
953 aux
= (const b_xcoff_auxent
*) (asym
+ 1);
954 if (aux
->x_file
._x
.x_zeroes
!= 0)
956 /* Make a copy as we will release the symtab view. */
957 char name
[FILNMLEN
+1];
958 strncpy (name
, aux
->x_file
.x_fname
, FILNMLEN
);
959 name
[FILNMLEN
] = '\0';
960 filename
= strdup (name
);
962 else if (aux
->x_file
._x
.x_offset
< strtab_size
)
963 filename
= (const char *) strtab
+ aux
->x_file
._x
.x_offset
;
975 if (!ISFCN (asym
->n_type
) || asym
->n_numaux
== 0
976 || asym
->n_scnum
<= 0)
978 if (filename
== NULL
)
980 aux
= (const b_xcoff_auxent
*) (asym
+ 1);
981 lnnoptr
= aux
->x_fcn
.x_lnnoptr
;
982 if (lnnoptr
< lnnoptr0
983 || lnnoptr
+ LINESZ
> lnnoptr0
+ linenos_size
)
985 /* x_fsize will be 0 if there is no debug information. */
986 fsize
= aux
->x_fcn
.x_fsize
;
991 if (asym
->n_numaux
== 0)
995 name
= xcoff_symname (asym
, strtab
, strtab_size
);
996 if (name
== NULL
|| strcmp (name
, ".bf") != 0)
1001 aux
= (const b_xcoff_auxent
*) (asym
+ 1);
1002 #if BACKTRACE_XCOFF_SIZE == 32
1003 lnno
= (uint32_t) aux
->x_block
.x_lnnohi
<< 16
1004 | aux
->x_block
.x_lnno
;
1006 lnno
= aux
->x_block
.x_lnno
;
1008 fn
= ((struct xcoff_func
*)
1009 backtrace_vector_grow (state
, sizeof (struct xcoff_func
),
1010 error_callback
, data
,
1011 &fdata
->func_vec
.vec
));
1014 fn
->name
= xcoff_symname (fsym
, strtab
, strtab_size
);
1015 fn
->filename
= filename
;
1016 fn
->sect_base
= sects
[fsym
->n_scnum
- 1].s_paddr
;
1017 fn
->pc
= base_address
+ fsym
->n_value
- fn
->sect_base
;
1020 fn
->lnnoptr
= lnnoptr
;
1021 ++fdata
->func_vec
.count
;
1025 i
+= asym
->n_numaux
;
1028 if (!backtrace_vector_release (state
, &fdata
->func_vec
.vec
, error_callback
,
1031 backtrace_qsort (fdata
->func_vec
.vec
.base
, fdata
->func_vec
.count
,
1032 sizeof (struct xcoff_func
), xcoff_func_compare
);
1034 if (!backtrace_vector_release (state
, &fdata
->incl_vec
.vec
, error_callback
,
1037 backtrace_qsort (fdata
->incl_vec
.vec
.base
, fdata
->incl_vec
.count
,
1038 sizeof (struct xcoff_incl
), xcoff_incl_compare
);
1040 if (!state
->threaded
)
1042 struct xcoff_fileline_data
**pp
;
1044 for (pp
= (struct xcoff_fileline_data
**) (void *) &state
->fileline_data
;
1054 struct xcoff_fileline_data
**pp
;
1056 pp
= (struct xcoff_fileline_data
**) (void *) &state
->fileline_data
;
1060 struct xcoff_fileline_data
*p
;
1062 p
= backtrace_atomic_load_pointer (pp
);
1070 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
1081 /* Add the backtrace data for one XCOFF file. Returns 1 on success,
1082 0 on failure (in both cases descriptor is closed). */
1085 xcoff_add (struct backtrace_state
*state
, int descriptor
, off_t offset
,
1086 uintptr_t base_address
, backtrace_error_callback error_callback
,
1087 void *data
, fileline
*fileline_fn
, int *found_sym
, int exe
)
1089 struct backtrace_view fhdr_view
;
1090 struct backtrace_view sects_view
;
1091 struct backtrace_view linenos_view
;
1092 struct backtrace_view syms_view
;
1093 struct backtrace_view str_view
;
1094 struct backtrace_view dwarf_view
;
1095 b_xcoff_filhdr fhdr
;
1096 const b_xcoff_scnhdr
*sects
;
1097 const b_xcoff_scnhdr
*stext
;
1103 struct dwsect_info dwsect
[DWSECT_MAX
];
1107 int sects_view_valid
;
1108 int linenos_view_valid
;
1109 int syms_view_valid
;
1111 int dwarf_view_valid
;
1117 sects_view_valid
= 0;
1118 linenos_view_valid
= 0;
1119 syms_view_valid
= 0;
1121 dwarf_view_valid
= 0;
1125 /* Map the XCOFF file header. */
1126 if (!backtrace_get_view (state
, descriptor
, offset
, sizeof (b_xcoff_filhdr
),
1127 error_callback
, data
, &fhdr_view
))
1130 memcpy (&fhdr
, fhdr_view
.data
, sizeof fhdr
);
1131 magic_ok
= (fhdr
.f_magic
== XCOFF_MAGIC
);
1133 backtrace_release_view (state
, &fhdr_view
, error_callback
, data
);
1138 error_callback (data
, "executable file is not XCOFF", 0);
1142 /* Verify object is of expected type. */
1143 if ((exe
&& (fhdr
.f_flags
& F_SHROBJ
))
1144 || (!exe
&& !(fhdr
.f_flags
& F_SHROBJ
)))
1147 /* Read the section headers. */
1149 sects_size
= fhdr
.f_nscns
* sizeof (b_xcoff_scnhdr
);
1151 if (!backtrace_get_view (state
, descriptor
,
1152 offset
+ sizeof (fhdr
) + fhdr
.f_opthdr
,
1153 sects_size
, error_callback
, data
, §s_view
))
1155 sects_view_valid
= 1;
1156 sects
= (const b_xcoff_scnhdr
*) sects_view
.data
;
1158 /* FIXME: assumes only one .text section. */
1159 for (i
= 0; i
< fhdr
.f_nscns
; ++i
)
1160 if ((sects
[i
].s_flags
& 0xffff) == STYP_TEXT
)
1162 if (i
== fhdr
.f_nscns
)
1167 /* AIX ldinfo_textorg includes the XCOFF headers. */
1168 base_address
= (exe
? XCOFF_AIX_TEXTBASE
: base_address
) + stext
->s_scnptr
;
1170 lnnoptr
= stext
->s_lnnoptr
;
1171 nlnno
= stext
->s_nlnno
;
1173 #if BACKTRACE_XCOFF_SIZE == 32
1174 if (nlnno
== _OVERFLOW_MARKER
)
1177 /* Find the matching .ovrflo section. */
1178 for (i
= 0; i
< fhdr
.f_nscns
; ++i
)
1180 if (((sects
[i
].s_flags
& 0xffff) == STYP_OVRFLO
)
1181 && sects
[i
].s_nlnno
== sntext
)
1183 nlnno
= sects
[i
].s_vaddr
;
1190 /* Read the symbol table and the string table. */
1192 if (fhdr
.f_symptr
!= 0)
1194 struct xcoff_syminfo_data
*sdata
;
1196 /* Symbol table is followed by the string table. The string table
1197 starts with its length (on 4 bytes).
1198 Map the symbol table and the length of the string table. */
1199 syms_size
= fhdr
.f_nsyms
* sizeof (b_xcoff_syment
);
1201 if (!backtrace_get_view (state
, descriptor
, offset
+ fhdr
.f_symptr
,
1202 syms_size
+ 4, error_callback
, data
,
1205 syms_view_valid
= 1;
1207 memcpy (&str_size
, syms_view
.data
+ syms_size
, 4);
1209 str_off
= fhdr
.f_symptr
+ syms_size
;
1213 /* Map string table (including the length word). */
1215 if (!backtrace_get_view (state
, descriptor
, offset
+ str_off
,
1216 str_size
, error_callback
, data
, &str_view
))
1221 sdata
= ((struct xcoff_syminfo_data
*)
1222 backtrace_alloc (state
, sizeof *sdata
, error_callback
, data
));
1226 if (!xcoff_initialize_syminfo (state
, base_address
, sects
,
1227 syms_view
.data
, fhdr
.f_nsyms
,
1228 str_view
.data
, str_size
,
1229 error_callback
, data
, sdata
))
1231 backtrace_free (state
, sdata
, sizeof *sdata
, error_callback
, data
);
1237 xcoff_add_syminfo_data (state
, sdata
);
1240 /* Read all the DWARF sections in a single view, since they are
1241 probably adjacent in the file. We never release this view. */
1245 memset (dwsect
, 0, sizeof dwsect
);
1246 for (i
= 0; i
< fhdr
.f_nscns
; ++i
)
1251 if ((sects
[i
].s_flags
& 0xffff) != STYP_DWARF
1252 || sects
[i
].s_size
== 0)
1254 /* Map DWARF section to array index. */
1255 switch (sects
[i
].s_flags
& 0xffff0000)
1257 case SSUBTYP_DWINFO
:
1260 case SSUBTYP_DWLINE
:
1263 case SSUBTYP_DWABREV
:
1264 idx
= DWSECT_ABBREV
;
1266 case SSUBTYP_DWARNGE
:
1267 idx
= DWSECT_RANGES
;
1275 if (min_offset
== 0 || (off_t
) sects
[i
].s_scnptr
< min_offset
)
1276 min_offset
= sects
[i
].s_scnptr
;
1277 end
= sects
[i
].s_scnptr
+ sects
[i
].s_size
;
1278 if (end
> max_offset
)
1280 dwsect
[idx
].offset
= sects
[i
].s_scnptr
;
1281 dwsect
[idx
].size
= sects
[i
].s_size
;
1283 if (min_offset
!= 0 && max_offset
!= 0)
1285 if (!backtrace_get_view (state
, descriptor
, offset
+ min_offset
,
1286 max_offset
- min_offset
,
1287 error_callback
, data
, &dwarf_view
))
1289 dwarf_view_valid
= 1;
1291 for (i
= 0; i
< (int) DWSECT_MAX
; ++i
)
1293 if (dwsect
[i
].offset
== 0)
1294 dwsect
[i
].data
= NULL
;
1296 dwsect
[i
].data
= ((const unsigned char *) dwarf_view
.data
1297 + (dwsect
[i
].offset
- min_offset
));
1300 if (!backtrace_dwarf_add (state
, 0,
1301 dwsect
[DWSECT_INFO
].data
,
1302 dwsect
[DWSECT_INFO
].size
,
1303 #if BACKTRACE_XCOFF_SIZE == 32
1304 /* XXX workaround for broken lineoff */
1305 dwsect
[DWSECT_LINE
].data
- 4,
1307 /* XXX workaround for broken lineoff */
1308 dwsect
[DWSECT_LINE
].data
- 12,
1310 dwsect
[DWSECT_LINE
].size
,
1311 dwsect
[DWSECT_ABBREV
].data
,
1312 dwsect
[DWSECT_ABBREV
].size
,
1313 dwsect
[DWSECT_RANGES
].data
,
1314 dwsect
[DWSECT_RANGES
].size
,
1315 dwsect
[DWSECT_STR
].data
,
1316 dwsect
[DWSECT_STR
].size
,
1318 error_callback
, data
, fileline_fn
))
1322 /* Read the XCOFF line number entries if DWARF sections not found. */
1324 if (!dwarf_view_valid
&& fhdr
.f_symptr
!= 0 && lnnoptr
!= 0)
1326 size_t linenos_size
= (size_t) nlnno
* LINESZ
;
1328 /* We never release this view. */
1329 if (!backtrace_get_view (state
, descriptor
, offset
+ lnnoptr
,
1331 error_callback
, data
, &linenos_view
))
1333 linenos_view_valid
= 1;
1335 if (xcoff_initialize_fileline (state
, base_address
, sects
,
1336 syms_view
.data
, fhdr
.f_nsyms
,
1337 str_view
.data
, str_size
,
1338 linenos_view
.data
, linenos_size
,
1339 lnnoptr
, error_callback
, data
))
1340 *fileline_fn
= xcoff_fileline
;
1343 backtrace_release_view (state
, §s_view
, error_callback
, data
);
1344 sects_view_valid
= 0;
1345 if (syms_view_valid
)
1346 backtrace_release_view (state
, &syms_view
, error_callback
, data
);
1347 syms_view_valid
= 0;
1349 /* We've read all we need from the executable. */
1350 if (!backtrace_close (descriptor
, error_callback
, data
))
1357 if (sects_view_valid
)
1358 backtrace_release_view (state
, §s_view
, error_callback
, data
);
1360 backtrace_release_view (state
, &str_view
, error_callback
, data
);
1361 if (syms_view_valid
)
1362 backtrace_release_view (state
, &syms_view
, error_callback
, data
);
1363 if (linenos_view_valid
)
1364 backtrace_release_view (state
, &linenos_view
, error_callback
, data
);
1365 if (dwarf_view_valid
)
1366 backtrace_release_view (state
, &dwarf_view
, error_callback
, data
);
1367 if (descriptor
!= -1 && offset
== 0)
1368 backtrace_close (descriptor
, error_callback
, data
);
1372 #ifdef HAVE_LOADQUERY
1374 /* Read an integer value in human-readable format from an AIX
1375 big archive fixed-length or member header. */
1378 xcoff_parse_decimal (const char *buf
, size_t size
, off_t
*off
)
1383 if (size
>= sizeof str
)
1385 memcpy (str
, buf
, size
);
1387 *off
= strtol (str
, &end
, 10);
1388 if (*end
!= '\0' && *end
!= ' ')
1394 /* Add the backtrace data for a member of an AIX big archive.
1395 Returns 1 on success, 0 on failure. */
1398 xcoff_armem_add (struct backtrace_state
*state
, int descriptor
,
1399 uintptr_t base_address
, const char *member
,
1400 backtrace_error_callback error_callback
, void *data
,
1401 fileline
*fileline_fn
, int *found_sym
)
1403 struct backtrace_view view
;
1405 const b_ar_hdr
*ar_hdr
;
1412 /* Map archive fixed-length header. */
1414 if (!backtrace_get_view (state
, descriptor
, 0, sizeof (b_ar_fl_hdr
),
1415 error_callback
, data
, &view
))
1418 memcpy (&fl_hdr
, view
.data
, sizeof (b_ar_fl_hdr
));
1420 backtrace_release_view (state
, &view
, error_callback
, data
);
1422 if (memcmp (fl_hdr
.fl_magic
, AIAMAGBIG
, 8) != 0)
1425 memlen
= strlen (member
);
1427 /* Read offset of first archive member. */
1428 if (!xcoff_parse_decimal (fl_hdr
.fl_fstmoff
, sizeof fl_hdr
.fl_fstmoff
, &off
))
1432 /* Map archive member header and member name. */
1434 if (!backtrace_get_view (state
, descriptor
, off
,
1435 sizeof (b_ar_hdr
) + memlen
,
1436 error_callback
, data
, &view
))
1439 ar_hdr
= (const b_ar_hdr
*) view
.data
;
1441 /* Read archive member name length. */
1442 if (!xcoff_parse_decimal (ar_hdr
->ar_namlen
, sizeof ar_hdr
->ar_namlen
,
1445 backtrace_release_view (state
, &view
, error_callback
, data
);
1448 if (len
== memlen
&& !memcmp (ar_hdr
->ar_name
, member
, memlen
))
1450 off
= (off
+ sizeof (b_ar_hdr
) + memlen
+ 1) & ~1;
1452 /* The archive can contain several members with the same name
1453 (e.g. 32-bit and 64-bit), so continue if not ok. */
1455 if (xcoff_add (state
, descriptor
, off
, base_address
, error_callback
,
1456 data
, fileline_fn
, found_sym
, 0))
1458 backtrace_release_view (state
, &view
, error_callback
, data
);
1463 /* Read offset of next archive member. */
1464 if (!xcoff_parse_decimal (ar_hdr
->ar_nxtmem
, sizeof ar_hdr
->ar_nxtmem
,
1467 backtrace_release_view (state
, &view
, error_callback
, data
);
1470 backtrace_release_view (state
, &view
, error_callback
, data
);
1474 /* No matching member found. */
1475 backtrace_close (descriptor
, error_callback
, data
);
1479 /* Add the backtrace data for dynamically loaded libraries. */
1482 xcoff_add_shared_libs (struct backtrace_state
*state
,
1483 backtrace_error_callback error_callback
,
1484 void *data
, fileline
*fileline_fn
, int *found_sym
)
1486 const struct ld_info
*ldinfo
;
1488 unsigned int buflen
;
1495 /* Retrieve the list of loaded libraries. */
1501 buf
= realloc (buf
, buflen
);
1507 ret
= loadquery (L_GETINFO
, buf
, buflen
);
1512 while (ret
== -1 && errno
== ENOMEM
);
1519 ldinfo
= (const struct ld_info
*) buf
;
1520 while ((const char *) ldinfo
< (const char *) buf
+ buflen
)
1522 if (*ldinfo
->ldinfo_filename
!= '/')
1525 descriptor
= backtrace_open (ldinfo
->ldinfo_filename
, error_callback
,
1526 data
, &does_not_exist
);
1530 /* Check if it is an archive (member name not empty). */
1532 member
= ldinfo
->ldinfo_filename
+ strlen (ldinfo
->ldinfo_filename
) + 1;
1535 xcoff_armem_add (state
, descriptor
,
1536 (uintptr_t) ldinfo
->ldinfo_textorg
, member
,
1537 error_callback
, data
, fileline_fn
, &lib_found_sym
);
1541 xcoff_add (state
, descriptor
, 0, (uintptr_t) ldinfo
->ldinfo_textorg
,
1542 error_callback
, data
, fileline_fn
, &lib_found_sym
, 0);
1548 if (ldinfo
->ldinfo_next
== 0)
1550 ldinfo
= (const struct ld_info
*) ((const char *) ldinfo
1551 + ldinfo
->ldinfo_next
);
1556 #endif /* HAVE_LOADQUERY */
1558 /* Initialize the backtrace data we need from an XCOFF executable.
1559 Returns 1 on success, 0 on failure. */
1562 backtrace_initialize (struct backtrace_state
*state
,
1563 const char *filename ATTRIBUTE_UNUSED
, int descriptor
,
1564 backtrace_error_callback error_callback
,
1565 void *data
, fileline
*fileline_fn
)
1569 fileline xcoff_fileline_fn
= xcoff_nodebug
;
1571 ret
= xcoff_add (state
, descriptor
, 0, 0, error_callback
, data
,
1572 &xcoff_fileline_fn
, &found_sym
, 1);
1576 #ifdef HAVE_LOADQUERY
1577 xcoff_add_shared_libs (state
, error_callback
, data
, &xcoff_fileline_fn
,
1581 if (!state
->threaded
)
1584 state
->syminfo_fn
= xcoff_syminfo
;
1585 else if (state
->syminfo_fn
== NULL
)
1586 state
->syminfo_fn
= xcoff_nosyms
;
1591 backtrace_atomic_store_pointer (&state
->syminfo_fn
, xcoff_syminfo
);
1593 __sync_bool_compare_and_swap (&state
->syminfo_fn
, NULL
, xcoff_nosyms
);
1596 if (!state
->threaded
)
1598 if (state
->fileline_fn
== NULL
|| state
->fileline_fn
== xcoff_nodebug
)
1599 *fileline_fn
= xcoff_fileline_fn
;
1603 fileline current_fn
;
1605 current_fn
= backtrace_atomic_load_pointer (&state
->fileline_fn
);
1606 if (current_fn
== NULL
|| current_fn
== xcoff_nodebug
)
1607 *fileline_fn
= xcoff_fileline_fn
;