1 /* xcoff.c -- Get debug data from an XCOFF file for backtraces.
2 Copyright (C) 2012-2017 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_TEXT 0x20 /* Executable text (code) section. */
128 #define STYP_OVRFLO 0x8000 /* Line-number field overflow section. */
134 #if BACKTRACE_XCOFF_SIZE == 32
138 char _name
[SYMNMLEN
];
144 #define n_name _u._name
145 #define n_zeroes _u._s._zeroes
146 #define n_offset_ _u._s._offset
153 } __attribute__ ((packed
)) b_xcoff_syment
;
155 #else /* BACKTRACE_XCOFF_SIZE != 32 */
164 } __attribute__ ((packed
)) b_xcoff_syment
;
166 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
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. */
186 #if BACKTRACE_XCOFF_SIZE == 32
198 char x_fname
[FILNMLEN
];
202 char pad
[FILNMLEN
-8];
206 #if BACKTRACE_XCOFF_SIZE == 32
221 uint8_t pad
[AUXESZ
-1];
224 } __attribute__ ((packed
)) b_xcoff_auxent
;
226 /* XCOFF line number entry. */
228 #if BACKTRACE_XCOFF_SIZE == 32
240 #else /* BACKTRACE_XCOFF_SIZE != 32 */
252 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
254 #if BACKTRACE_XCOFF_SIZE == 32
255 #define XCOFF_AIX_TEXTBASE 0x10000000u
257 #define XCOFF_AIX_TEXTBASE 0x100000000ul
260 /* AIX big archive fixed-length header. */
262 #define AIAMAGBIG "<bigaf>\n"
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. */
273 /* AIX big archive file member header. */
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. */
288 /* Information we keep for an XCOFF symbol. */
292 /* The name of the symbol. */
294 /* The address of the symbol. */
296 /* The size of the symbol. */
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. */
312 /* Information about an include file. */
317 const char *filename
;
318 /* Offset to first line number from the include file. */
320 /* Offset to last line number from the include file. */
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. */
334 /* Map a single PC value to a file/function/line. */
340 /* File name. Many entries in the array are expected to point to
341 the same file name. */
342 const char *filename
;
344 const char *function
;
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. */
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. */
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);
383 /* A dummy callback function used when we can't find a symbol
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. */
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
)
405 else if (e1
->address
> e2
->address
)
411 /* Compare an ADDR against an xcoff_symbol for bsearch. */
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
;
421 if (addr
< entry
->address
)
423 else if ((entry
->size
== 0 && addr
> entry
->address
)
424 || (entry
->size
> 0 && addr
>= entry
->address
+ entry
->size
))
430 /* Add XDATA to the list in STATE. */
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
;
450 struct xcoff_syminfo_data
**pp
;
452 pp
= (struct xcoff_syminfo_data
**) (void *) &state
->syminfo_data
;
456 struct xcoff_syminfo_data
*p
;
458 p
= backtrace_atomic_load_pointer (pp
);
466 if (__sync_bool_compare_and_swap (pp
, NULL
, xdata
))
472 /* Return the symbol name and value for an ADDR. */
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
,
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
;
489 sym
= ((struct xcoff_symbol
*)
490 bsearch (&addr
, edata
->symbols
, edata
->count
,
491 sizeof (struct xcoff_symbol
), xcoff_symbol_search
));
498 struct xcoff_syminfo_data
**pp
;
500 pp
= (struct xcoff_syminfo_data
**) (void *) &state
->syminfo_data
;
503 edata
= backtrace_atomic_load_pointer (pp
);
507 sym
= ((struct xcoff_symbol
*)
508 bsearch (&addr
, edata
->symbols
, edata
->count
,
509 sizeof (struct xcoff_symbol
), xcoff_symbol_search
));
518 callback (data
, addr
, NULL
, 0, 0);
520 callback (data
, addr
, sym
->name
, sym
->address
, sym
->size
);
523 /* Return the name of an XCOFF symbol. */
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
);
539 if (asym
->n_sclass
& 0x80)
540 return NULL
; /* .debug */
541 if (asym
->n_offset_
>= strtab_size
)
543 return (const char *) strtab
+ asym
->n_offset_
;
546 /* Initialize the symbol table info for xcoff_syminfo. */
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
;
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
;
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
,
580 if (xcoff_symbols
== NULL
)
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
;
603 backtrace_qsort (xcoff_symbols
, xcoff_symbol_count
,
604 sizeof (struct xcoff_symbol
), xcoff_symbol_compare
);
607 sdata
->symbols
= xcoff_symbols
;
608 sdata
->count
= xcoff_symbol_count
;
613 /* Compare struct xcoff_line for qsort. */
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
)
623 else if (ln1
->pc
> ln2
->pc
)
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. */
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
;
643 else if ((entry
+ 1)->pc
== (uintptr_t) -1 || pc
>= (entry
+ 1)->pc
)
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,
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
;
666 ln
= (struct xcoff_line
*) bsearch (&pc
, fdata
->vec
.vec
.base
,
668 sizeof (struct xcoff_line
),
676 function
= ln
->function
;
677 /* AIX prepends a '.' to function entry points, remove it. */
678 if (*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. */
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
;
696 if (!state
->threaded
)
698 for (fdata
= (struct xcoff_fileline_data
*) state
->fileline_data
;
702 ret
= xcoff_lookup_pc (state
, fdata
, pc
, callback
, error_callback
,
704 if (ret
!= 0 || found
)
710 struct xcoff_fileline_data
**pp
;
712 pp
= (struct xcoff_fileline_data
**) (void *) &state
->fileline_data
;
715 fdata
= backtrace_atomic_load_pointer (pp
);
719 ret
= xcoff_lookup_pc (state
, fdata
, pc
, callback
, error_callback
,
721 if (ret
!= 0 || found
)
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. */
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
));
751 ln
->filename
= filename
;
752 ln
->function
= function
;
760 /* Add the line number entries for a function to the line vector. */
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
,
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
;
784 aux
= (const b_xcoff_auxent
*) (fsym
+ 1);
785 lnnoptr
= aux
->x_fcn
.x_lnnoptr
;
787 if (lnnoptr
< lnnoptr0
|| lnnoptr
+ LINESZ
> lnnoptr0
+ linenos_size
)
790 function
= xcoff_symname (fsym
, strtab
, strtab_size
);
791 if (function
== NULL
)
794 /* Skip first entry that points to symtab. */
798 lineptr
= linenos
+ (lnnoptr
- lnnoptr0
);
801 while (lineptr
+ LINESZ
<= linenos
+ linenos_size
)
803 lineno
= (const b_xcoff_lineno
*) lineptr
;
805 lnno
= lineno
->l_lnno
;
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
)
819 begincl
= (i
< vec
->count
);
822 filename
= incl
->filename
;
824 lnno
+= fcn_lnno
- 1;
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
,
841 /* Initialize the line vector info for xcoff_fileline. */
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
,
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
;
860 struct xcoff_incl
*incl
;
861 uintptr_t begin
, end
;
865 fdata
= ((struct xcoff_fileline_data
*)
866 backtrace_alloc (state
, sizeof (struct xcoff_fileline_data
),
867 error_callback
, data
));
871 memset (fdata
, 0, sizeof *fdata
);
872 memset (&vec
, 0, sizeof vec
);
874 /* Process include files first. */
877 for (i
= 0; i
< nsyms
; ++i
)
879 const b_xcoff_syment
*asym
= &syms
[i
];
881 switch (asym
->n_sclass
)
884 begin
= asym
->n_value
;
891 incl
= ((struct xcoff_incl
*)
892 backtrace_vector_grow (state
, sizeof (struct xcoff_incl
),
893 error_callback
, data
, &vec
.vec
));
896 incl
->filename
= xcoff_symname (asym
, strtab
, strtab_size
);
910 for (i
= 0; i
< nsyms
; ++i
)
912 const b_xcoff_syment
*asym
= &syms
[i
];
914 switch (asym
->n_sclass
)
917 filename
= xcoff_symname (asym
, strtab
, strtab_size
);
918 if (filename
== NULL
)
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
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
;
949 if (!ISFCN (asym
->n_type
) || asym
->n_numaux
== 0)
951 if (filename
== NULL
)
957 if (asym
->n_numaux
== 0)
961 name
= xcoff_symname (asym
, strtab
, strtab_size
);
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
;
969 lnno
= aux
->x_block
.x_lnno
;
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"))
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
));
994 ln
->pc
= (uintptr_t) -1;
999 if (!backtrace_vector_release (state
, &fdata
->vec
.vec
, error_callback
, data
))
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
;
1019 struct xcoff_fileline_data
**pp
;
1021 pp
= (struct xcoff_fileline_data
**) (void *) &state
->fileline_data
;
1025 struct xcoff_fileline_data
*p
;
1027 p
= backtrace_atomic_load_pointer (pp
);
1035 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
1046 /* Add the backtrace data for one XCOFF file. Returns 1 on success,
1047 0 on failure (in both cases descriptor is closed). */
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
;
1068 int sects_view_valid
;
1069 int linenos_view_valid
;
1070 int syms_view_valid
;
1077 sects_view_valid
= 0;
1078 linenos_view_valid
= 0;
1079 syms_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
))
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
);
1095 error_callback (data
, "executable file is not XCOFF", 0);
1099 /* Verify object is of expected type. */
1100 if ((exe
&& (fhdr
.f_flags
& F_SHROBJ
))
1101 || (!exe
&& !(fhdr
.f_flags
& F_SHROBJ
)))
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
, §s_view
))
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
)
1119 if (i
== fhdr
.f_nscns
)
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
)
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
;
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
,
1162 syms_view_valid
= 1;
1164 memcpy (&str_size
, syms_view
.data
+ syms_size
, 4);
1166 str_off
= fhdr
.f_symptr
+ syms_size
;
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
))
1178 sdata
= ((struct xcoff_syminfo_data
*)
1179 backtrace_alloc (state
, sizeof *sdata
, error_callback
, data
));
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
);
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
,
1205 error_callback
, data
, &linenos_view
))
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
, §s_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
))
1234 if (sects_view_valid
)
1235 backtrace_release_view (state
, §s_view
, error_callback
, data
);
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
);
1247 #ifdef HAVE_LOADQUERY
1249 /* Read an integer value in human-readable format from an AIX
1250 big archive fixed-length or member header. */
1253 xcoff_parse_decimal (const char *buf
, size_t size
, off_t
*off
)
1258 if (size
>= sizeof str
)
1260 memcpy (str
, buf
, size
);
1262 *off
= strtol (str
, &end
, 10);
1263 if (*end
!= '\0' && *end
!= ' ')
1269 /* Add the backtrace data for a member of an AIX big archive.
1270 Returns 1 on success, 0 on failure. */
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
;
1280 const b_ar_hdr
*ar_hdr
;
1287 /* Map archive fixed-length header. */
1289 if (!backtrace_get_view (state
, descriptor
, 0, sizeof (b_ar_fl_hdr
),
1290 error_callback
, data
, &view
))
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)
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
))
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
))
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
,
1320 backtrace_release_view (state
, &view
, error_callback
, data
);
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
);
1338 /* Read offset of next archive member. */
1339 if (!xcoff_parse_decimal (ar_hdr
->ar_nxtmem
, sizeof ar_hdr
->ar_nxtmem
,
1342 backtrace_release_view (state
, &view
, error_callback
, data
);
1345 backtrace_release_view (state
, &view
, error_callback
, data
);
1349 /* No matching member found. */
1350 backtrace_close (descriptor
, error_callback
, data
);
1354 /* Add the backtrace data for dynamically loaded libraries. */
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
;
1363 unsigned int buflen
;
1370 /* Retrieve the list of loaded libraries. */
1376 buf
= realloc (buf
, buflen
);
1382 ret
= loadquery (L_GETINFO
, buf
, buflen
);
1387 while (ret
== -1 && errno
== ENOMEM
);
1394 ldinfo
= (const struct ld_info
*) buf
;
1395 while ((const char *) ldinfo
< (const char *) buf
+ buflen
)
1397 if (*ldinfo
->ldinfo_filename
!= '/')
1400 descriptor
= backtrace_open (ldinfo
->ldinfo_filename
, error_callback
,
1401 data
, &does_not_exist
);
1405 /* Check if it is an archive (member name not empty). */
1407 member
= ldinfo
->ldinfo_filename
+ strlen (ldinfo
->ldinfo_filename
) + 1;
1410 xcoff_armem_add (state
, descriptor
,
1411 (uintptr_t) ldinfo
->ldinfo_textorg
, member
,
1412 error_callback
, data
, fileline_fn
, &lib_found_sym
);
1416 xcoff_add (state
, descriptor
, 0, (uintptr_t) ldinfo
->ldinfo_textorg
,
1417 error_callback
, data
, fileline_fn
, &lib_found_sym
, 0);
1423 if (ldinfo
->ldinfo_next
== 0)
1425 ldinfo
= (const struct ld_info
*) ((const char *) ldinfo
1426 + ldinfo
->ldinfo_next
);
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
)
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);
1451 #ifdef HAVE_LOADQUERY
1452 xcoff_add_shared_libs (state
, error_callback
, data
, &xcoff_fileline_fn
,
1456 if (!state
->threaded
)
1459 state
->syminfo_fn
= xcoff_syminfo
;
1460 else if (state
->syminfo_fn
== NULL
)
1461 state
->syminfo_fn
= xcoff_nosyms
;
1466 backtrace_atomic_store_pointer (&state
->syminfo_fn
, xcoff_syminfo
);
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
;
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
;