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 /* Map a single PC value to a file/function/line. */
347 /* File name. Many entries in the array are expected to point to
348 the same file name. */
349 const char *filename
;
351 const char *function
;
356 /* A growable vector of line number information. This is used while
357 reading the line numbers. */
359 struct xcoff_line_vector
361 /* Memory. This is an array of struct xcoff_line. */
362 struct backtrace_vector vec
;
363 /* Number of valid mappings. */
367 /* The information we need to map a PC to a file and line. */
369 struct xcoff_fileline_data
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
;
377 /* An index of DWARF sections we care about. */
389 /* Information we gather for the DWARF sections we care about. */
393 /* Section file offset. */
397 /* Section contents, after read from file. */
398 const unsigned char *data
;
401 /* A dummy callback function used when we can't find any debug info. */
404 xcoff_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
)
409 error_callback (data
, "no debug info in XCOFF executable", -1);
413 /* A dummy callback function used when we can't find a symbol
417 xcoff_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
)
422 error_callback (data
, "no symbol table in XCOFF executable", -1);
425 /* Compare struct xcoff_symbol for qsort. */
428 xcoff_symbol_compare (const void *v1
, const void *v2
)
430 const struct xcoff_symbol
*e1
= (const struct xcoff_symbol
*) v1
;
431 const struct xcoff_symbol
*e2
= (const struct xcoff_symbol
*) v2
;
433 if (e1
->address
< e2
->address
)
435 else if (e1
->address
> e2
->address
)
441 /* Compare an ADDR against an xcoff_symbol for bsearch. */
444 xcoff_symbol_search (const void *vkey
, const void *ventry
)
446 const uintptr_t *key
= (const uintptr_t *) vkey
;
447 const struct xcoff_symbol
*entry
= (const struct xcoff_symbol
*) ventry
;
451 if (addr
< entry
->address
)
453 else if ((entry
->size
== 0 && addr
> entry
->address
)
454 || (entry
->size
> 0 && addr
>= entry
->address
+ entry
->size
))
460 /* Add XDATA to the list in STATE. */
463 xcoff_add_syminfo_data (struct backtrace_state
*state
,
464 struct xcoff_syminfo_data
*xdata
)
466 if (!state
->threaded
)
468 struct xcoff_syminfo_data
**pp
;
470 for (pp
= (struct xcoff_syminfo_data
**) (void *) &state
->syminfo_data
;
480 struct xcoff_syminfo_data
**pp
;
482 pp
= (struct xcoff_syminfo_data
**) (void *) &state
->syminfo_data
;
486 struct xcoff_syminfo_data
*p
;
488 p
= backtrace_atomic_load_pointer (pp
);
496 if (__sync_bool_compare_and_swap (pp
, NULL
, xdata
))
502 /* Return the symbol name and value for an ADDR. */
505 xcoff_syminfo (struct backtrace_state
*state ATTRIBUTE_UNUSED
, uintptr_t addr
,
506 backtrace_syminfo_callback callback
,
507 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
510 struct xcoff_syminfo_data
*edata
;
511 struct xcoff_symbol
*sym
= NULL
;
513 if (!state
->threaded
)
515 for (edata
= (struct xcoff_syminfo_data
*) state
->syminfo_data
;
519 sym
= ((struct xcoff_symbol
*)
520 bsearch (&addr
, edata
->symbols
, edata
->count
,
521 sizeof (struct xcoff_symbol
), xcoff_symbol_search
));
528 struct xcoff_syminfo_data
**pp
;
530 pp
= (struct xcoff_syminfo_data
**) (void *) &state
->syminfo_data
;
533 edata
= backtrace_atomic_load_pointer (pp
);
537 sym
= ((struct xcoff_symbol
*)
538 bsearch (&addr
, edata
->symbols
, edata
->count
,
539 sizeof (struct xcoff_symbol
), xcoff_symbol_search
));
548 callback (data
, addr
, NULL
, 0, 0);
550 callback (data
, addr
, sym
->name
, sym
->address
, sym
->size
);
553 /* Return the name of an XCOFF symbol. */
556 xcoff_symname (const b_xcoff_syment
*asym
,
557 const unsigned char *strtab
, size_t strtab_size
)
559 #if BACKTRACE_XCOFF_SIZE == 32
560 if (asym
->n_zeroes
!= 0)
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
);
569 if (asym
->n_sclass
& 0x80)
570 return NULL
; /* .debug */
571 if (asym
->n_offset_
>= strtab_size
)
573 return (const char *) strtab
+ asym
->n_offset_
;
576 /* Initialize the symbol table info for xcoff_syminfo. */
579 xcoff_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
)
587 size_t xcoff_symbol_count
;
588 size_t xcoff_symbol_size
;
589 struct xcoff_symbol
*xcoff_symbols
;
593 /* We only care about function symbols. Count them. */
594 xcoff_symbol_count
= 0;
595 for (i
= 0; i
< nsyms
; ++i
)
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
;
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
,
610 if (xcoff_symbols
== NULL
)
614 for (i
= 0; i
< nsyms
; ++i
)
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)
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
;
633 backtrace_qsort (xcoff_symbols
, xcoff_symbol_count
,
634 sizeof (struct xcoff_symbol
), xcoff_symbol_compare
);
637 sdata
->symbols
= xcoff_symbols
;
638 sdata
->count
= xcoff_symbol_count
;
643 /* Compare struct xcoff_line for qsort. */
646 xcoff_line_compare (const void *v1
, const void *v2
)
648 const struct xcoff_line
*ln1
= (const struct xcoff_line
*) v1
;
649 const struct xcoff_line
*ln2
= (const struct xcoff_line
*) v2
;
651 if (ln1
->pc
< ln2
->pc
)
653 else if (ln1
->pc
> ln2
->pc
)
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. */
664 xcoff_line_search (const void *vkey
, const void *ventry
)
666 const uintptr_t *key
= (const uintptr_t *) vkey
;
667 const struct xcoff_line
*entry
= (const struct xcoff_line
*) ventry
;
673 else if ((entry
+ 1)->pc
== (uintptr_t) -1 || pc
>= (entry
+ 1)->pc
)
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,
685 xcoff_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
)
691 const struct xcoff_line
*ln
;
692 const char *function
;
696 ln
= (struct xcoff_line
*) bsearch (&pc
, fdata
->vec
.vec
.base
,
698 sizeof (struct xcoff_line
),
706 function
= ln
->function
;
707 /* AIX prepends a '.' to function entry points, remove it. */
708 if (*function
== '.')
710 return callback (data
, pc
, ln
->filename
, ln
->lineno
, function
);
713 /* Return the file/line information for a PC using the XCOFF lineno
714 mapping we built earlier. */
717 xcoff_fileline (struct backtrace_state
*state
, uintptr_t pc
,
718 backtrace_full_callback callback
,
719 backtrace_error_callback error_callback
, void *data
)
722 struct xcoff_fileline_data
*fdata
;
726 if (!state
->threaded
)
728 for (fdata
= (struct xcoff_fileline_data
*) state
->fileline_data
;
732 ret
= xcoff_lookup_pc (state
, fdata
, pc
, callback
, error_callback
,
734 if (ret
!= 0 || found
)
740 struct xcoff_fileline_data
**pp
;
742 pp
= (struct xcoff_fileline_data
**) (void *) &state
->fileline_data
;
745 fdata
= backtrace_atomic_load_pointer (pp
);
749 ret
= xcoff_lookup_pc (state
, fdata
, pc
, callback
, error_callback
,
751 if (ret
!= 0 || found
)
758 /* FIXME: See if any libraries have been dlopen'ed. */
760 return callback (data
, pc
, NULL
, 0, NULL
);
763 /* Compare struct xcoff_incl for qsort. */
766 xcoff_incl_compare (const void *v1
, const void *v2
)
768 const struct xcoff_incl
*in1
= (const struct xcoff_incl
*) v1
;
769 const struct xcoff_incl
*in2
= (const struct xcoff_incl
*) v2
;
771 if (in1
->begin
< in2
->begin
)
773 else if (in1
->begin
> in2
->begin
)
779 /* Find a lnnoptr in an include file. */
782 xcoff_incl_search (const void *vkey
, const void *ventry
)
784 const uintptr_t *key
= (const uintptr_t *) vkey
;
785 const struct xcoff_incl
*entry
= (const struct xcoff_incl
*) ventry
;
789 if (lnno
< entry
->begin
)
791 else if (lnno
> entry
->end
)
797 /* Add a new mapping to the vector of line mappings that we are
798 building. Returns 1 on success, 0 on failure. */
801 xcoff_add_line (struct backtrace_state
*state
, uintptr_t pc
,
802 const char *filename
, const char *function
, uint32_t lnno
,
803 backtrace_error_callback error_callback
, void *data
,
804 struct xcoff_line_vector
*vec
)
806 struct xcoff_line
*ln
;
808 ln
= ((struct xcoff_line
*)
809 backtrace_vector_grow (state
, sizeof (struct xcoff_line
),
810 error_callback
, data
, &vec
->vec
));
815 ln
->filename
= filename
;
816 ln
->function
= function
;
824 /* Add the line number entries for a function to the line vector. */
827 xcoff_process_linenos (struct backtrace_state
*state
, uintptr_t base_address
,
828 const b_xcoff_syment
*fsym
, const char *filename
,
829 const b_xcoff_scnhdr
*sects
,
830 const unsigned char *strtab
, size_t strtab_size
,
831 uint32_t fcn_lnno
, struct xcoff_incl_vector
*vec
,
832 struct xcoff_line_vector
*lvec
,
833 const unsigned char *linenos
, size_t linenos_size
,
835 backtrace_error_callback error_callback
, void *data
)
837 const b_xcoff_auxent
*aux
;
838 const b_xcoff_lineno
*lineno
;
839 const unsigned char *lineptr
;
840 const char *function
;
841 struct xcoff_incl
*incl
= NULL
;
847 aux
= (const b_xcoff_auxent
*) (fsym
+ 1);
848 lnnoptr
= aux
->x_fcn
.x_lnnoptr
;
850 if (lnnoptr
< lnnoptr0
|| lnnoptr
+ LINESZ
> lnnoptr0
+ linenos_size
)
853 function
= xcoff_symname (fsym
, strtab
, strtab_size
);
854 if (function
== NULL
)
857 /* Skip first entry that points to symtab. */
861 lineptr
= linenos
+ (lnnoptr
- lnnoptr0
);
864 while (lineptr
+ LINESZ
<= linenos
+ linenos_size
)
866 lineno
= (const b_xcoff_lineno
*) lineptr
;
868 lnno
= lineno
->l_lnno
;
872 /* If part of a function other than the beginning comes from an
873 include file, the line numbers are absolute, rather than
874 relative to the beginning of the function. */
875 incl
= (struct xcoff_incl
*) bsearch (&lnnoptr
, vec
->vec
.base
,
877 sizeof (struct xcoff_incl
),
880 begincl
= incl
!= NULL
;
883 filename
= incl
->filename
;
885 lnno
+= fcn_lnno
- 1;
888 lnno
+= fcn_lnno
- 1;
890 pc
= base_address
+ lineno
->l_addr
.l_paddr
891 - sects
[fsym
->n_scnum
- 1].s_paddr
;
892 xcoff_add_line (state
, pc
, filename
, function
, lnno
, error_callback
,
902 /* Initialize the line vector info for xcoff_fileline. */
905 xcoff_initialize_fileline (struct backtrace_state
*state
,
906 uintptr_t base_address
,
907 const b_xcoff_scnhdr
*sects
,
908 const b_xcoff_syment
*syms
, size_t nsyms
,
909 const unsigned char *strtab
, size_t strtab_size
,
910 const unsigned char *linenos
, size_t linenos_size
,
912 backtrace_error_callback error_callback
, void *data
)
914 struct xcoff_fileline_data
*fdata
;
915 struct xcoff_incl_vector vec
;
916 struct xcoff_line
*ln
;
917 const b_xcoff_syment
*fsym
;
918 const b_xcoff_auxent
*aux
;
919 const char *filename
;
921 struct xcoff_incl
*incl
;
922 uintptr_t begin
, end
;
926 fdata
= ((struct xcoff_fileline_data
*)
927 backtrace_alloc (state
, sizeof (struct xcoff_fileline_data
),
928 error_callback
, data
));
932 memset (fdata
, 0, sizeof *fdata
);
933 memset (&vec
, 0, sizeof vec
);
935 /* Process include files first. */
938 for (i
= 0; i
< nsyms
; ++i
)
940 const b_xcoff_syment
*asym
= &syms
[i
];
942 switch (asym
->n_sclass
)
945 begin
= asym
->n_value
;
952 incl
= ((struct xcoff_incl
*)
953 backtrace_vector_grow (state
, sizeof (struct xcoff_incl
),
954 error_callback
, data
, &vec
.vec
));
957 incl
->filename
= xcoff_symname (asym
, strtab
, strtab_size
);
969 backtrace_qsort (vec
.vec
.base
, vec
.count
,
970 sizeof (struct xcoff_incl
), xcoff_incl_compare
);
974 for (i
= 0; i
< nsyms
; ++i
)
976 const b_xcoff_syment
*asym
= &syms
[i
];
978 switch (asym
->n_sclass
)
981 filename
= xcoff_symname (asym
, strtab
, strtab_size
);
982 if (filename
== NULL
)
985 /* If the file auxiliary entry is not used, the symbol name is
986 the name of the source file. If the file auxiliary entry is
987 used, then the symbol name should be .file, and the first
988 file auxiliary entry (by convention) contains the source
991 if (asym
->n_numaux
> 0 && !strcmp (filename
, ".file"))
993 aux
= (const b_xcoff_auxent
*) (asym
+ 1);
994 if (aux
->x_file
._x
.x_zeroes
!= 0)
996 /* Make a copy as we will release the symtab view. */
997 char name
[FILNMLEN
+1];
998 strncpy (name
, aux
->x_file
.x_fname
, FILNMLEN
);
999 name
[FILNMLEN
] = '\0';
1000 filename
= strdup (name
);
1002 else if (aux
->x_file
._x
.x_offset
< strtab_size
)
1003 filename
= (const char *) strtab
+ aux
->x_file
._x
.x_offset
;
1013 if (!ISFCN (asym
->n_type
) || asym
->n_numaux
== 0)
1015 if (filename
== NULL
)
1021 if (asym
->n_numaux
== 0)
1025 name
= xcoff_symname (asym
, strtab
, strtab_size
);
1028 aux
= (const b_xcoff_auxent
*) (asym
+ 1);
1029 #if BACKTRACE_XCOFF_SIZE == 32
1030 lnno
= (uint32_t) aux
->x_block
.x_lnnohi
<< 16
1031 | aux
->x_block
.x_lnno
;
1033 lnno
= aux
->x_block
.x_lnno
;
1035 if (!strcmp (name
, ".bf"))
1037 xcoff_process_linenos (state
, base_address
, fsym
, filename
,
1038 sects
, strtab
, strtab_size
, lnno
, &vec
,
1039 &fdata
->vec
, linenos
, linenos_size
,
1040 lnnoptr0
, error_callback
, data
);
1042 else if (!strcmp (name
, ".ef"))
1049 i
+= asym
->n_numaux
;
1052 /* Allocate one extra entry at the end. */
1053 ln
= ((struct xcoff_line
*)
1054 backtrace_vector_grow (state
, sizeof (struct xcoff_line
),
1055 error_callback
, data
, &fdata
->vec
.vec
));
1058 ln
->pc
= (uintptr_t) -1;
1059 ln
->filename
= NULL
;
1060 ln
->function
= NULL
;
1063 if (!backtrace_vector_release (state
, &fdata
->vec
.vec
, error_callback
, data
))
1066 backtrace_qsort (fdata
->vec
.vec
.base
, fdata
->vec
.count
,
1067 sizeof (struct xcoff_line
), xcoff_line_compare
);
1069 if (!state
->threaded
)
1071 struct xcoff_fileline_data
**pp
;
1073 for (pp
= (struct xcoff_fileline_data
**) (void *) &state
->fileline_data
;
1083 struct xcoff_fileline_data
**pp
;
1085 pp
= (struct xcoff_fileline_data
**) (void *) &state
->fileline_data
;
1089 struct xcoff_fileline_data
*p
;
1091 p
= backtrace_atomic_load_pointer (pp
);
1099 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
1110 /* Add the backtrace data for one XCOFF file. Returns 1 on success,
1111 0 on failure (in both cases descriptor is closed). */
1114 xcoff_add (struct backtrace_state
*state
, int descriptor
, off_t offset
,
1115 uintptr_t base_address
, backtrace_error_callback error_callback
,
1116 void *data
, fileline
*fileline_fn
, int *found_sym
, int exe
)
1118 struct backtrace_view fhdr_view
;
1119 struct backtrace_view sects_view
;
1120 struct backtrace_view linenos_view
;
1121 struct backtrace_view syms_view
;
1122 struct backtrace_view str_view
;
1123 struct backtrace_view dwarf_view
;
1124 b_xcoff_filhdr fhdr
;
1125 const b_xcoff_scnhdr
*sects
;
1126 const b_xcoff_scnhdr
*stext
;
1132 struct dwsect_info dwsect
[DWSECT_MAX
];
1136 int sects_view_valid
;
1137 int linenos_view_valid
;
1138 int syms_view_valid
;
1140 int dwarf_view_valid
;
1146 sects_view_valid
= 0;
1147 linenos_view_valid
= 0;
1148 syms_view_valid
= 0;
1150 dwarf_view_valid
= 0;
1154 /* Map the XCOFF file header. */
1155 if (!backtrace_get_view (state
, descriptor
, offset
, sizeof (b_xcoff_filhdr
),
1156 error_callback
, data
, &fhdr_view
))
1159 memcpy (&fhdr
, fhdr_view
.data
, sizeof fhdr
);
1160 magic_ok
= (fhdr
.f_magic
== XCOFF_MAGIC
);
1162 backtrace_release_view (state
, &fhdr_view
, error_callback
, data
);
1167 error_callback (data
, "executable file is not XCOFF", 0);
1171 /* Verify object is of expected type. */
1172 if ((exe
&& (fhdr
.f_flags
& F_SHROBJ
))
1173 || (!exe
&& !(fhdr
.f_flags
& F_SHROBJ
)))
1176 /* Read the section headers. */
1178 sects_size
= fhdr
.f_nscns
* sizeof (b_xcoff_scnhdr
);
1180 if (!backtrace_get_view (state
, descriptor
,
1181 offset
+ sizeof (fhdr
) + fhdr
.f_opthdr
,
1182 sects_size
, error_callback
, data
, §s_view
))
1184 sects_view_valid
= 1;
1185 sects
= (const b_xcoff_scnhdr
*) sects_view
.data
;
1187 /* FIXME: assumes only one .text section. */
1188 for (i
= 0; i
< fhdr
.f_nscns
; ++i
)
1189 if ((sects
[i
].s_flags
& 0xffff) == STYP_TEXT
)
1191 if (i
== fhdr
.f_nscns
)
1196 /* AIX ldinfo_textorg includes the XCOFF headers. */
1197 base_address
= (exe
? XCOFF_AIX_TEXTBASE
: base_address
) + stext
->s_scnptr
;
1199 lnnoptr
= stext
->s_lnnoptr
;
1200 nlnno
= stext
->s_nlnno
;
1202 #if BACKTRACE_XCOFF_SIZE == 32
1203 if (nlnno
== _OVERFLOW_MARKER
)
1206 /* Find the matching .ovrflo section. */
1207 for (i
= 0; i
< fhdr
.f_nscns
; ++i
)
1209 if (((sects
[i
].s_flags
& 0xffff) == STYP_OVRFLO
)
1210 && sects
[i
].s_nlnno
== sntext
)
1212 nlnno
= sects
[i
].s_vaddr
;
1219 /* Read the symbol table and the string table. */
1221 if (fhdr
.f_symptr
!= 0)
1223 struct xcoff_syminfo_data
*sdata
;
1225 /* Symbol table is followed by the string table. The string table
1226 starts with its length (on 4 bytes).
1227 Map the symbol table and the length of the string table. */
1228 syms_size
= fhdr
.f_nsyms
* sizeof (b_xcoff_syment
);
1230 if (!backtrace_get_view (state
, descriptor
, offset
+ fhdr
.f_symptr
,
1231 syms_size
+ 4, error_callback
, data
,
1234 syms_view_valid
= 1;
1236 memcpy (&str_size
, syms_view
.data
+ syms_size
, 4);
1238 str_off
= fhdr
.f_symptr
+ syms_size
;
1242 /* Map string table (including the length word). */
1244 if (!backtrace_get_view (state
, descriptor
, offset
+ str_off
,
1245 str_size
, error_callback
, data
, &str_view
))
1250 sdata
= ((struct xcoff_syminfo_data
*)
1251 backtrace_alloc (state
, sizeof *sdata
, error_callback
, data
));
1255 if (!xcoff_initialize_syminfo (state
, base_address
, sects
,
1256 syms_view
.data
, fhdr
.f_nsyms
,
1257 str_view
.data
, str_size
,
1258 error_callback
, data
, sdata
))
1260 backtrace_free (state
, sdata
, sizeof *sdata
, error_callback
, data
);
1266 xcoff_add_syminfo_data (state
, sdata
);
1269 /* Read all the DWARF sections in a single view, since they are
1270 probably adjacent in the file. We never release this view. */
1274 memset (dwsect
, 0, sizeof dwsect
);
1275 for (i
= 0; i
< fhdr
.f_nscns
; ++i
)
1280 if ((sects
[i
].s_flags
& 0xffff) != STYP_DWARF
1281 || sects
[i
].s_size
== 0)
1283 /* Map DWARF section to array index. */
1284 switch (sects
[i
].s_flags
& 0xffff0000)
1286 case SSUBTYP_DWINFO
:
1289 case SSUBTYP_DWLINE
:
1292 case SSUBTYP_DWABREV
:
1293 idx
= DWSECT_ABBREV
;
1295 case SSUBTYP_DWARNGE
:
1296 idx
= DWSECT_RANGES
;
1304 if (min_offset
== 0 || (off_t
) sects
[i
].s_scnptr
< min_offset
)
1305 min_offset
= sects
[i
].s_scnptr
;
1306 end
= sects
[i
].s_scnptr
+ sects
[i
].s_size
;
1307 if (end
> max_offset
)
1309 dwsect
[idx
].offset
= sects
[i
].s_scnptr
;
1310 dwsect
[idx
].size
= sects
[i
].s_size
;
1312 if (min_offset
!= 0 && max_offset
!= 0)
1314 if (!backtrace_get_view (state
, descriptor
, offset
+ min_offset
,
1315 max_offset
- min_offset
,
1316 error_callback
, data
, &dwarf_view
))
1318 dwarf_view_valid
= 1;
1320 for (i
= 0; i
< (int) DWSECT_MAX
; ++i
)
1322 if (dwsect
[i
].offset
== 0)
1323 dwsect
[i
].data
= NULL
;
1325 dwsect
[i
].data
= ((const unsigned char *) dwarf_view
.data
1326 + (dwsect
[i
].offset
- min_offset
));
1329 if (!backtrace_dwarf_add (state
, 0,
1330 dwsect
[DWSECT_INFO
].data
,
1331 dwsect
[DWSECT_INFO
].size
,
1332 #if BACKTRACE_XCOFF_SIZE == 32
1333 /* XXX workaround for broken lineoff */
1334 dwsect
[DWSECT_LINE
].data
- 4,
1336 /* XXX workaround for broken lineoff */
1337 dwsect
[DWSECT_LINE
].data
- 12,
1339 dwsect
[DWSECT_LINE
].size
,
1340 dwsect
[DWSECT_ABBREV
].data
,
1341 dwsect
[DWSECT_ABBREV
].size
,
1342 dwsect
[DWSECT_RANGES
].data
,
1343 dwsect
[DWSECT_RANGES
].size
,
1344 dwsect
[DWSECT_STR
].data
,
1345 dwsect
[DWSECT_STR
].size
,
1347 error_callback
, data
, fileline_fn
))
1351 /* Read the XCOFF line number entries if DWARF sections not found. */
1353 if (!dwarf_view_valid
&& fhdr
.f_symptr
!= 0 && lnnoptr
!= 0)
1355 size_t linenos_size
= (size_t) nlnno
* LINESZ
;
1357 if (!backtrace_get_view (state
, descriptor
, offset
+ lnnoptr
,
1359 error_callback
, data
, &linenos_view
))
1361 linenos_view_valid
= 1;
1363 if (xcoff_initialize_fileline (state
, base_address
, sects
,
1364 syms_view
.data
, fhdr
.f_nsyms
,
1365 str_view
.data
, str_size
,
1366 linenos_view
.data
, linenos_size
,
1367 lnnoptr
, error_callback
, data
))
1368 *fileline_fn
= xcoff_fileline
;
1370 backtrace_release_view (state
, &linenos_view
, error_callback
, data
);
1371 linenos_view_valid
= 0;
1374 backtrace_release_view (state
, §s_view
, error_callback
, data
);
1375 sects_view_valid
= 0;
1376 if (syms_view_valid
)
1377 backtrace_release_view (state
, &syms_view
, error_callback
, data
);
1378 syms_view_valid
= 0;
1380 /* We've read all we need from the executable. */
1381 if (!backtrace_close (descriptor
, error_callback
, data
))
1388 if (sects_view_valid
)
1389 backtrace_release_view (state
, §s_view
, error_callback
, data
);
1391 backtrace_release_view (state
, &str_view
, error_callback
, data
);
1392 if (syms_view_valid
)
1393 backtrace_release_view (state
, &syms_view
, error_callback
, data
);
1394 if (linenos_view_valid
)
1395 backtrace_release_view (state
, &linenos_view
, error_callback
, data
);
1396 if (dwarf_view_valid
)
1397 backtrace_release_view (state
, &dwarf_view
, error_callback
, data
);
1398 if (descriptor
!= -1 && offset
== 0)
1399 backtrace_close (descriptor
, error_callback
, data
);
1403 #ifdef HAVE_LOADQUERY
1405 /* Read an integer value in human-readable format from an AIX
1406 big archive fixed-length or member header. */
1409 xcoff_parse_decimal (const char *buf
, size_t size
, off_t
*off
)
1414 if (size
>= sizeof str
)
1416 memcpy (str
, buf
, size
);
1418 *off
= strtol (str
, &end
, 10);
1419 if (*end
!= '\0' && *end
!= ' ')
1425 /* Add the backtrace data for a member of an AIX big archive.
1426 Returns 1 on success, 0 on failure. */
1429 xcoff_armem_add (struct backtrace_state
*state
, int descriptor
,
1430 uintptr_t base_address
, const char *member
,
1431 backtrace_error_callback error_callback
, void *data
,
1432 fileline
*fileline_fn
, int *found_sym
)
1434 struct backtrace_view view
;
1436 const b_ar_hdr
*ar_hdr
;
1443 /* Map archive fixed-length header. */
1445 if (!backtrace_get_view (state
, descriptor
, 0, sizeof (b_ar_fl_hdr
),
1446 error_callback
, data
, &view
))
1449 memcpy (&fl_hdr
, view
.data
, sizeof (b_ar_fl_hdr
));
1451 backtrace_release_view (state
, &view
, error_callback
, data
);
1453 if (memcmp (fl_hdr
.fl_magic
, AIAMAGBIG
, 8) != 0)
1456 memlen
= strlen (member
);
1458 /* Read offset of first archive member. */
1459 if (!xcoff_parse_decimal (fl_hdr
.fl_fstmoff
, sizeof fl_hdr
.fl_fstmoff
, &off
))
1463 /* Map archive member header and member name. */
1465 if (!backtrace_get_view (state
, descriptor
, off
,
1466 sizeof (b_ar_hdr
) + memlen
,
1467 error_callback
, data
, &view
))
1470 ar_hdr
= (const b_ar_hdr
*) view
.data
;
1472 /* Read archive member name length. */
1473 if (!xcoff_parse_decimal (ar_hdr
->ar_namlen
, sizeof ar_hdr
->ar_namlen
,
1476 backtrace_release_view (state
, &view
, error_callback
, data
);
1479 if (len
== memlen
&& !memcmp (ar_hdr
->ar_name
, member
, memlen
))
1481 off
= (off
+ sizeof (b_ar_hdr
) + memlen
+ 1) & ~1;
1483 /* The archive can contain several members with the same name
1484 (e.g. 32-bit and 64-bit), so continue if not ok. */
1486 if (xcoff_add (state
, descriptor
, off
, base_address
, error_callback
,
1487 data
, fileline_fn
, found_sym
, 0))
1489 backtrace_release_view (state
, &view
, error_callback
, data
);
1494 /* Read offset of next archive member. */
1495 if (!xcoff_parse_decimal (ar_hdr
->ar_nxtmem
, sizeof ar_hdr
->ar_nxtmem
,
1498 backtrace_release_view (state
, &view
, error_callback
, data
);
1501 backtrace_release_view (state
, &view
, error_callback
, data
);
1505 /* No matching member found. */
1506 backtrace_close (descriptor
, error_callback
, data
);
1510 /* Add the backtrace data for dynamically loaded libraries. */
1513 xcoff_add_shared_libs (struct backtrace_state
*state
,
1514 backtrace_error_callback error_callback
,
1515 void *data
, fileline
*fileline_fn
, int *found_sym
)
1517 const struct ld_info
*ldinfo
;
1519 unsigned int buflen
;
1526 /* Retrieve the list of loaded libraries. */
1532 buf
= realloc (buf
, buflen
);
1538 ret
= loadquery (L_GETINFO
, buf
, buflen
);
1543 while (ret
== -1 && errno
== ENOMEM
);
1550 ldinfo
= (const struct ld_info
*) buf
;
1551 while ((const char *) ldinfo
< (const char *) buf
+ buflen
)
1553 if (*ldinfo
->ldinfo_filename
!= '/')
1556 descriptor
= backtrace_open (ldinfo
->ldinfo_filename
, error_callback
,
1557 data
, &does_not_exist
);
1561 /* Check if it is an archive (member name not empty). */
1563 member
= ldinfo
->ldinfo_filename
+ strlen (ldinfo
->ldinfo_filename
) + 1;
1566 xcoff_armem_add (state
, descriptor
,
1567 (uintptr_t) ldinfo
->ldinfo_textorg
, member
,
1568 error_callback
, data
, fileline_fn
, &lib_found_sym
);
1572 xcoff_add (state
, descriptor
, 0, (uintptr_t) ldinfo
->ldinfo_textorg
,
1573 error_callback
, data
, fileline_fn
, &lib_found_sym
, 0);
1579 if (ldinfo
->ldinfo_next
== 0)
1581 ldinfo
= (const struct ld_info
*) ((const char *) ldinfo
1582 + ldinfo
->ldinfo_next
);
1587 #endif /* HAVE_LOADQUERY */
1589 /* Initialize the backtrace data we need from an XCOFF executable.
1590 Returns 1 on success, 0 on failure. */
1593 backtrace_initialize (struct backtrace_state
*state
,
1594 const char *filename ATTRIBUTE_UNUSED
, int descriptor
,
1595 backtrace_error_callback error_callback
,
1596 void *data
, fileline
*fileline_fn
)
1600 fileline xcoff_fileline_fn
= xcoff_nodebug
;
1602 ret
= xcoff_add (state
, descriptor
, 0, 0, error_callback
, data
,
1603 &xcoff_fileline_fn
, &found_sym
, 1);
1607 #ifdef HAVE_LOADQUERY
1608 xcoff_add_shared_libs (state
, error_callback
, data
, &xcoff_fileline_fn
,
1612 if (!state
->threaded
)
1615 state
->syminfo_fn
= xcoff_syminfo
;
1616 else if (state
->syminfo_fn
== NULL
)
1617 state
->syminfo_fn
= xcoff_nosyms
;
1622 backtrace_atomic_store_pointer (&state
->syminfo_fn
, xcoff_syminfo
);
1624 __sync_bool_compare_and_swap (&state
->syminfo_fn
, NULL
, xcoff_nosyms
);
1627 if (!state
->threaded
)
1629 if (state
->fileline_fn
== NULL
|| state
->fileline_fn
== xcoff_nodebug
)
1630 *fileline_fn
= xcoff_fileline_fn
;
1634 fileline current_fn
;
1636 current_fn
= backtrace_atomic_load_pointer (&state
->fileline_fn
);
1637 if (current_fn
== NULL
|| current_fn
== xcoff_nodebug
)
1638 *fileline_fn
= xcoff_fileline_fn
;