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_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 /* Add a new mapping to the vector of line mappings that we are
764 building. Returns 1 on success, 0 on failure. */
767 xcoff_add_line (struct backtrace_state
*state
, uintptr_t pc
,
768 const char *filename
, const char *function
, uint32_t lnno
,
769 backtrace_error_callback error_callback
, void *data
,
770 struct xcoff_line_vector
*vec
)
772 struct xcoff_line
*ln
;
774 ln
= ((struct xcoff_line
*)
775 backtrace_vector_grow (state
, sizeof (struct xcoff_line
),
776 error_callback
, data
, &vec
->vec
));
781 ln
->filename
= filename
;
782 ln
->function
= function
;
790 /* Add the line number entries for a function to the line vector. */
793 xcoff_process_linenos (struct backtrace_state
*state
, uintptr_t base_address
,
794 const b_xcoff_syment
*fsym
, const char *filename
,
795 const b_xcoff_scnhdr
*sects
,
796 const unsigned char *strtab
, size_t strtab_size
,
797 uint32_t fcn_lnno
, struct xcoff_incl_vector
*vec
,
798 struct xcoff_line_vector
*lvec
,
799 const unsigned char *linenos
, size_t linenos_size
,
801 backtrace_error_callback error_callback
, void *data
)
803 const b_xcoff_auxent
*aux
;
804 const b_xcoff_lineno
*lineno
;
805 const unsigned char *lineptr
;
806 const char *function
;
807 struct xcoff_incl
*incl
= NULL
;
814 aux
= (const b_xcoff_auxent
*) (fsym
+ 1);
815 lnnoptr
= aux
->x_fcn
.x_lnnoptr
;
817 if (lnnoptr
< lnnoptr0
|| lnnoptr
+ LINESZ
> lnnoptr0
+ linenos_size
)
820 function
= xcoff_symname (fsym
, strtab
, strtab_size
);
821 if (function
== NULL
)
824 /* Skip first entry that points to symtab. */
828 lineptr
= linenos
+ (lnnoptr
- lnnoptr0
);
831 while (lineptr
+ LINESZ
<= linenos
+ linenos_size
)
833 lineno
= (const b_xcoff_lineno
*) lineptr
;
835 lnno
= lineno
->l_lnno
;
839 /* If part of a function other than the beginning comes from an
840 include file, the line numbers are absolute, rather than
841 relative to the beginning of the function. */
842 for (i
= 0; i
< vec
->count
; ++i
)
844 incl
= (struct xcoff_incl
*) vec
->vec
.base
+ i
;
845 if (incl
->begin
<= lnnoptr
&& lnnoptr
<= incl
->end
)
849 begincl
= (i
< vec
->count
);
852 filename
= incl
->filename
;
854 lnno
+= fcn_lnno
- 1;
857 lnno
+= fcn_lnno
- 1;
859 pc
= base_address
+ lineno
->l_addr
.l_paddr
860 - sects
[fsym
->n_scnum
- 1].s_paddr
;
861 xcoff_add_line (state
, pc
, filename
, function
, lnno
, error_callback
,
871 /* Initialize the line vector info for xcoff_fileline. */
874 xcoff_initialize_fileline (struct backtrace_state
*state
,
875 uintptr_t base_address
,
876 const b_xcoff_scnhdr
*sects
,
877 const b_xcoff_syment
*syms
, size_t nsyms
,
878 const unsigned char *strtab
, size_t strtab_size
,
879 const unsigned char *linenos
, size_t linenos_size
,
881 backtrace_error_callback error_callback
, void *data
)
883 struct xcoff_fileline_data
*fdata
;
884 struct xcoff_incl_vector vec
;
885 struct xcoff_line
*ln
;
886 const b_xcoff_syment
*fsym
;
887 const b_xcoff_auxent
*aux
;
888 const char *filename
;
890 struct xcoff_incl
*incl
;
891 uintptr_t begin
, end
;
895 fdata
= ((struct xcoff_fileline_data
*)
896 backtrace_alloc (state
, sizeof (struct xcoff_fileline_data
),
897 error_callback
, data
));
901 memset (fdata
, 0, sizeof *fdata
);
902 memset (&vec
, 0, sizeof vec
);
904 /* Process include files first. */
907 for (i
= 0; i
< nsyms
; ++i
)
909 const b_xcoff_syment
*asym
= &syms
[i
];
911 switch (asym
->n_sclass
)
914 begin
= asym
->n_value
;
921 incl
= ((struct xcoff_incl
*)
922 backtrace_vector_grow (state
, sizeof (struct xcoff_incl
),
923 error_callback
, data
, &vec
.vec
));
926 incl
->filename
= xcoff_symname (asym
, strtab
, strtab_size
);
940 for (i
= 0; i
< nsyms
; ++i
)
942 const b_xcoff_syment
*asym
= &syms
[i
];
944 switch (asym
->n_sclass
)
947 filename
= xcoff_symname (asym
, strtab
, strtab_size
);
948 if (filename
== NULL
)
951 /* If the file auxiliary entry is not used, the symbol name is
952 the name of the source file. If the file auxiliary entry is
953 used, then the symbol name should be .file, and the first
954 file auxiliary entry (by convention) contains the source
957 if (asym
->n_numaux
> 0 && !strcmp (filename
, ".file"))
959 aux
= (const b_xcoff_auxent
*) (asym
+ 1);
960 if (aux
->x_file
._x
.x_zeroes
!= 0)
962 /* Make a copy as we will release the symtab view. */
963 char name
[FILNMLEN
+1];
964 strncpy (name
, aux
->x_file
.x_fname
, FILNMLEN
);
965 name
[FILNMLEN
] = '\0';
966 filename
= strdup (name
);
968 else if (aux
->x_file
._x
.x_offset
< strtab_size
)
969 filename
= (const char *) strtab
+ aux
->x_file
._x
.x_offset
;
979 if (!ISFCN (asym
->n_type
) || asym
->n_numaux
== 0)
981 if (filename
== NULL
)
987 if (asym
->n_numaux
== 0)
991 name
= xcoff_symname (asym
, strtab
, strtab_size
);
994 aux
= (const b_xcoff_auxent
*) (asym
+ 1);
995 #if BACKTRACE_XCOFF_SIZE == 32
996 lnno
= (uint32_t) aux
->x_block
.x_lnnohi
<< 16
997 | aux
->x_block
.x_lnno
;
999 lnno
= aux
->x_block
.x_lnno
;
1001 if (!strcmp (name
, ".bf"))
1003 xcoff_process_linenos (state
, base_address
, fsym
, filename
,
1004 sects
, strtab
, strtab_size
, lnno
, &vec
,
1005 &fdata
->vec
, linenos
, linenos_size
,
1006 lnnoptr0
, error_callback
, data
);
1008 else if (!strcmp (name
, ".ef"))
1015 i
+= asym
->n_numaux
;
1018 /* Allocate one extra entry at the end. */
1019 ln
= ((struct xcoff_line
*)
1020 backtrace_vector_grow (state
, sizeof (struct xcoff_line
),
1021 error_callback
, data
, &fdata
->vec
.vec
));
1024 ln
->pc
= (uintptr_t) -1;
1025 ln
->filename
= NULL
;
1026 ln
->function
= NULL
;
1029 if (!backtrace_vector_release (state
, &fdata
->vec
.vec
, error_callback
, data
))
1032 backtrace_qsort (fdata
->vec
.vec
.base
, fdata
->vec
.count
,
1033 sizeof (struct xcoff_line
), xcoff_line_compare
);
1035 if (!state
->threaded
)
1037 struct xcoff_fileline_data
**pp
;
1039 for (pp
= (struct xcoff_fileline_data
**) (void *) &state
->fileline_data
;
1049 struct xcoff_fileline_data
**pp
;
1051 pp
= (struct xcoff_fileline_data
**) (void *) &state
->fileline_data
;
1055 struct xcoff_fileline_data
*p
;
1057 p
= backtrace_atomic_load_pointer (pp
);
1065 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
1076 /* Add the backtrace data for one XCOFF file. Returns 1 on success,
1077 0 on failure (in both cases descriptor is closed). */
1080 xcoff_add (struct backtrace_state
*state
, int descriptor
, off_t offset
,
1081 uintptr_t base_address
, backtrace_error_callback error_callback
,
1082 void *data
, fileline
*fileline_fn
, int *found_sym
, int exe
)
1084 struct backtrace_view fhdr_view
;
1085 struct backtrace_view sects_view
;
1086 struct backtrace_view linenos_view
;
1087 struct backtrace_view syms_view
;
1088 struct backtrace_view str_view
;
1089 struct backtrace_view dwarf_view
;
1090 b_xcoff_filhdr fhdr
;
1091 const b_xcoff_scnhdr
*sects
;
1092 const b_xcoff_scnhdr
*stext
;
1098 struct dwsect_info dwsect
[DWSECT_MAX
];
1102 int sects_view_valid
;
1103 int linenos_view_valid
;
1104 int syms_view_valid
;
1106 int dwarf_view_valid
;
1112 sects_view_valid
= 0;
1113 linenos_view_valid
= 0;
1114 syms_view_valid
= 0;
1116 dwarf_view_valid
= 0;
1120 /* Map the XCOFF file header. */
1121 if (!backtrace_get_view (state
, descriptor
, offset
, sizeof (b_xcoff_filhdr
),
1122 error_callback
, data
, &fhdr_view
))
1125 memcpy (&fhdr
, fhdr_view
.data
, sizeof fhdr
);
1126 magic_ok
= (fhdr
.f_magic
== XCOFF_MAGIC
);
1128 backtrace_release_view (state
, &fhdr_view
, error_callback
, data
);
1133 error_callback (data
, "executable file is not XCOFF", 0);
1137 /* Verify object is of expected type. */
1138 if ((exe
&& (fhdr
.f_flags
& F_SHROBJ
))
1139 || (!exe
&& !(fhdr
.f_flags
& F_SHROBJ
)))
1142 /* Read the section headers. */
1144 sects_size
= fhdr
.f_nscns
* sizeof (b_xcoff_scnhdr
);
1146 if (!backtrace_get_view (state
, descriptor
,
1147 offset
+ sizeof (fhdr
) + fhdr
.f_opthdr
,
1148 sects_size
, error_callback
, data
, §s_view
))
1150 sects_view_valid
= 1;
1151 sects
= (const b_xcoff_scnhdr
*) sects_view
.data
;
1153 /* FIXME: assumes only one .text section. */
1154 for (i
= 0; i
< fhdr
.f_nscns
; ++i
)
1155 if ((sects
[i
].s_flags
& 0xffff) == STYP_TEXT
)
1157 if (i
== fhdr
.f_nscns
)
1162 /* AIX ldinfo_textorg includes the XCOFF headers. */
1163 base_address
= (exe
? XCOFF_AIX_TEXTBASE
: base_address
) + stext
->s_scnptr
;
1165 lnnoptr
= stext
->s_lnnoptr
;
1166 nlnno
= stext
->s_nlnno
;
1168 #if BACKTRACE_XCOFF_SIZE == 32
1169 if (nlnno
== _OVERFLOW_MARKER
)
1172 /* Find the matching .ovrflo section. */
1173 for (i
= 0; i
< fhdr
.f_nscns
; ++i
)
1175 if (((sects
[i
].s_flags
& 0xffff) == STYP_OVRFLO
)
1176 && sects
[i
].s_nlnno
== sntext
)
1178 nlnno
= sects
[i
].s_vaddr
;
1185 /* Read the symbol table and the string table. */
1187 if (fhdr
.f_symptr
!= 0)
1189 struct xcoff_syminfo_data
*sdata
;
1191 /* Symbol table is followed by the string table. The string table
1192 starts with its length (on 4 bytes).
1193 Map the symbol table and the length of the string table. */
1194 syms_size
= fhdr
.f_nsyms
* sizeof (b_xcoff_syment
);
1196 if (!backtrace_get_view (state
, descriptor
, offset
+ fhdr
.f_symptr
,
1197 syms_size
+ 4, error_callback
, data
,
1200 syms_view_valid
= 1;
1202 memcpy (&str_size
, syms_view
.data
+ syms_size
, 4);
1204 str_off
= fhdr
.f_symptr
+ syms_size
;
1208 /* Map string table (including the length word). */
1210 if (!backtrace_get_view (state
, descriptor
, offset
+ str_off
,
1211 str_size
, error_callback
, data
, &str_view
))
1216 sdata
= ((struct xcoff_syminfo_data
*)
1217 backtrace_alloc (state
, sizeof *sdata
, error_callback
, data
));
1221 if (!xcoff_initialize_syminfo (state
, base_address
, sects
,
1222 syms_view
.data
, fhdr
.f_nsyms
,
1223 str_view
.data
, str_size
,
1224 error_callback
, data
, sdata
))
1226 backtrace_free (state
, sdata
, sizeof *sdata
, error_callback
, data
);
1232 xcoff_add_syminfo_data (state
, sdata
);
1235 /* Read all the DWARF sections in a single view, since they are
1236 probably adjacent in the file. We never release this view. */
1240 memset (dwsect
, 0, sizeof dwsect
);
1241 for (i
= 0; i
< fhdr
.f_nscns
; ++i
)
1246 if ((sects
[i
].s_flags
& 0xffff) != STYP_DWARF
1247 || sects
[i
].s_size
== 0)
1249 /* Map DWARF section to array index. */
1250 switch (sects
[i
].s_flags
& 0xffff0000)
1252 case SSUBTYP_DWINFO
:
1255 case SSUBTYP_DWLINE
:
1258 case SSUBTYP_DWABREV
:
1259 idx
= DWSECT_ABBREV
;
1261 case SSUBTYP_DWARNGE
:
1262 idx
= DWSECT_RANGES
;
1270 if (min_offset
== 0 || (off_t
) sects
[i
].s_scnptr
< min_offset
)
1271 min_offset
= sects
[i
].s_scnptr
;
1272 end
= sects
[i
].s_scnptr
+ sects
[i
].s_size
;
1273 if (end
> max_offset
)
1275 dwsect
[idx
].offset
= sects
[i
].s_scnptr
;
1276 dwsect
[idx
].size
= sects
[i
].s_size
;
1278 if (min_offset
!= 0 && max_offset
!= 0)
1280 if (!backtrace_get_view (state
, descriptor
, offset
+ min_offset
,
1281 max_offset
- min_offset
,
1282 error_callback
, data
, &dwarf_view
))
1284 dwarf_view_valid
= 1;
1286 for (i
= 0; i
< (int) DWSECT_MAX
; ++i
)
1288 if (dwsect
[i
].offset
== 0)
1289 dwsect
[i
].data
= NULL
;
1291 dwsect
[i
].data
= ((const unsigned char *) dwarf_view
.data
1292 + (dwsect
[i
].offset
- min_offset
));
1295 if (!backtrace_dwarf_add (state
, 0,
1296 dwsect
[DWSECT_INFO
].data
,
1297 dwsect
[DWSECT_INFO
].size
,
1298 #if BACKTRACE_XCOFF_SIZE == 32
1299 /* XXX workaround for broken lineoff */
1300 dwsect
[DWSECT_LINE
].data
- 4,
1302 /* XXX workaround for broken lineoff */
1303 dwsect
[DWSECT_LINE
].data
- 12,
1305 dwsect
[DWSECT_LINE
].size
,
1306 dwsect
[DWSECT_ABBREV
].data
,
1307 dwsect
[DWSECT_ABBREV
].size
,
1308 dwsect
[DWSECT_RANGES
].data
,
1309 dwsect
[DWSECT_RANGES
].size
,
1310 dwsect
[DWSECT_STR
].data
,
1311 dwsect
[DWSECT_STR
].size
,
1313 error_callback
, data
, fileline_fn
))
1317 /* Read the XCOFF line number entries if DWARF sections not found. */
1319 if (!dwarf_view_valid
&& fhdr
.f_symptr
!= 0 && lnnoptr
!= 0)
1321 size_t linenos_size
= (size_t) nlnno
* LINESZ
;
1323 if (!backtrace_get_view (state
, descriptor
, offset
+ lnnoptr
,
1325 error_callback
, data
, &linenos_view
))
1327 linenos_view_valid
= 1;
1329 if (xcoff_initialize_fileline (state
, base_address
, sects
,
1330 syms_view
.data
, fhdr
.f_nsyms
,
1331 str_view
.data
, str_size
,
1332 linenos_view
.data
, linenos_size
,
1333 lnnoptr
, error_callback
, data
))
1334 *fileline_fn
= xcoff_fileline
;
1336 backtrace_release_view (state
, &linenos_view
, error_callback
, data
);
1337 linenos_view_valid
= 0;
1340 backtrace_release_view (state
, §s_view
, error_callback
, data
);
1341 sects_view_valid
= 0;
1342 if (syms_view_valid
)
1343 backtrace_release_view (state
, &syms_view
, error_callback
, data
);
1344 syms_view_valid
= 0;
1346 /* We've read all we need from the executable. */
1347 if (!backtrace_close (descriptor
, error_callback
, data
))
1354 if (sects_view_valid
)
1355 backtrace_release_view (state
, §s_view
, error_callback
, data
);
1357 backtrace_release_view (state
, &str_view
, error_callback
, data
);
1358 if (syms_view_valid
)
1359 backtrace_release_view (state
, &syms_view
, error_callback
, data
);
1360 if (linenos_view_valid
)
1361 backtrace_release_view (state
, &linenos_view
, error_callback
, data
);
1362 if (dwarf_view_valid
)
1363 backtrace_release_view (state
, &dwarf_view
, error_callback
, data
);
1364 if (descriptor
!= -1 && offset
== 0)
1365 backtrace_close (descriptor
, error_callback
, data
);
1369 #ifdef HAVE_LOADQUERY
1371 /* Read an integer value in human-readable format from an AIX
1372 big archive fixed-length or member header. */
1375 xcoff_parse_decimal (const char *buf
, size_t size
, off_t
*off
)
1380 if (size
>= sizeof str
)
1382 memcpy (str
, buf
, size
);
1384 *off
= strtol (str
, &end
, 10);
1385 if (*end
!= '\0' && *end
!= ' ')
1391 /* Add the backtrace data for a member of an AIX big archive.
1392 Returns 1 on success, 0 on failure. */
1395 xcoff_armem_add (struct backtrace_state
*state
, int descriptor
,
1396 uintptr_t base_address
, const char *member
,
1397 backtrace_error_callback error_callback
, void *data
,
1398 fileline
*fileline_fn
, int *found_sym
)
1400 struct backtrace_view view
;
1402 const b_ar_hdr
*ar_hdr
;
1409 /* Map archive fixed-length header. */
1411 if (!backtrace_get_view (state
, descriptor
, 0, sizeof (b_ar_fl_hdr
),
1412 error_callback
, data
, &view
))
1415 memcpy (&fl_hdr
, view
.data
, sizeof (b_ar_fl_hdr
));
1417 backtrace_release_view (state
, &view
, error_callback
, data
);
1419 if (memcmp (fl_hdr
.fl_magic
, AIAMAGBIG
, 8) != 0)
1422 memlen
= strlen (member
);
1424 /* Read offset of first archive member. */
1425 if (!xcoff_parse_decimal (fl_hdr
.fl_fstmoff
, sizeof fl_hdr
.fl_fstmoff
, &off
))
1429 /* Map archive member header and member name. */
1431 if (!backtrace_get_view (state
, descriptor
, off
,
1432 sizeof (b_ar_hdr
) + memlen
,
1433 error_callback
, data
, &view
))
1436 ar_hdr
= (const b_ar_hdr
*) view
.data
;
1438 /* Read archive member name length. */
1439 if (!xcoff_parse_decimal (ar_hdr
->ar_namlen
, sizeof ar_hdr
->ar_namlen
,
1442 backtrace_release_view (state
, &view
, error_callback
, data
);
1445 if (len
== memlen
&& !memcmp (ar_hdr
->ar_name
, member
, memlen
))
1447 off
= (off
+ sizeof (b_ar_hdr
) + memlen
+ 1) & ~1;
1449 /* The archive can contain several members with the same name
1450 (e.g. 32-bit and 64-bit), so continue if not ok. */
1452 if (xcoff_add (state
, descriptor
, off
, base_address
, error_callback
,
1453 data
, fileline_fn
, found_sym
, 0))
1455 backtrace_release_view (state
, &view
, error_callback
, data
);
1460 /* Read offset of next archive member. */
1461 if (!xcoff_parse_decimal (ar_hdr
->ar_nxtmem
, sizeof ar_hdr
->ar_nxtmem
,
1464 backtrace_release_view (state
, &view
, error_callback
, data
);
1467 backtrace_release_view (state
, &view
, error_callback
, data
);
1471 /* No matching member found. */
1472 backtrace_close (descriptor
, error_callback
, data
);
1476 /* Add the backtrace data for dynamically loaded libraries. */
1479 xcoff_add_shared_libs (struct backtrace_state
*state
,
1480 backtrace_error_callback error_callback
,
1481 void *data
, fileline
*fileline_fn
, int *found_sym
)
1483 const struct ld_info
*ldinfo
;
1485 unsigned int buflen
;
1492 /* Retrieve the list of loaded libraries. */
1498 buf
= realloc (buf
, buflen
);
1504 ret
= loadquery (L_GETINFO
, buf
, buflen
);
1509 while (ret
== -1 && errno
== ENOMEM
);
1516 ldinfo
= (const struct ld_info
*) buf
;
1517 while ((const char *) ldinfo
< (const char *) buf
+ buflen
)
1519 if (*ldinfo
->ldinfo_filename
!= '/')
1522 descriptor
= backtrace_open (ldinfo
->ldinfo_filename
, error_callback
,
1523 data
, &does_not_exist
);
1527 /* Check if it is an archive (member name not empty). */
1529 member
= ldinfo
->ldinfo_filename
+ strlen (ldinfo
->ldinfo_filename
) + 1;
1532 xcoff_armem_add (state
, descriptor
,
1533 (uintptr_t) ldinfo
->ldinfo_textorg
, member
,
1534 error_callback
, data
, fileline_fn
, &lib_found_sym
);
1538 xcoff_add (state
, descriptor
, 0, (uintptr_t) ldinfo
->ldinfo_textorg
,
1539 error_callback
, data
, fileline_fn
, &lib_found_sym
, 0);
1545 if (ldinfo
->ldinfo_next
== 0)
1547 ldinfo
= (const struct ld_info
*) ((const char *) ldinfo
1548 + ldinfo
->ldinfo_next
);
1553 #endif /* HAVE_LOADQUERY */
1555 /* Initialize the backtrace data we need from an XCOFF executable.
1556 Returns 1 on success, 0 on failure. */
1559 backtrace_initialize (struct backtrace_state
*state
,
1560 const char *filename ATTRIBUTE_UNUSED
, int descriptor
,
1561 backtrace_error_callback error_callback
,
1562 void *data
, fileline
*fileline_fn
)
1566 fileline xcoff_fileline_fn
= xcoff_nodebug
;
1568 ret
= xcoff_add (state
, descriptor
, 0, 0, error_callback
, data
,
1569 &xcoff_fileline_fn
, &found_sym
, 1);
1573 #ifdef HAVE_LOADQUERY
1574 xcoff_add_shared_libs (state
, error_callback
, data
, &xcoff_fileline_fn
,
1578 if (!state
->threaded
)
1581 state
->syminfo_fn
= xcoff_syminfo
;
1582 else if (state
->syminfo_fn
== NULL
)
1583 state
->syminfo_fn
= xcoff_nosyms
;
1588 backtrace_atomic_store_pointer (&state
->syminfo_fn
, xcoff_syminfo
);
1590 __sync_bool_compare_and_swap (&state
->syminfo_fn
, NULL
, xcoff_nosyms
);
1593 if (!state
->threaded
)
1595 if (state
->fileline_fn
== NULL
|| state
->fileline_fn
== xcoff_nodebug
)
1596 *fileline_fn
= xcoff_fileline_fn
;
1600 fileline current_fn
;
1602 current_fn
= backtrace_atomic_load_pointer (&state
->fileline_fn
);
1603 if (current_fn
== NULL
|| current_fn
== xcoff_nodebug
)
1604 *fileline_fn
= xcoff_fileline_fn
;