target_attr_10.c: Add -mcpu=generic.
[gcc.git] / libbacktrace / xcoff.c
1 /* xcoff.c -- Get debug data from an XCOFF file for backtraces.
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
3 Adapted from elf.c.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16
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.
20
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. */
32
33 #include "config.h"
34
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39
40 #ifdef HAVE_LOADQUERY
41 #include <sys/ldr.h>
42 #endif
43
44 #include "backtrace.h"
45 #include "internal.h"
46
47 /* The configure script must tell us whether we are 32-bit or 64-bit
48 XCOFF. We could make this code test and support either possibility,
49 but there is no point. This code only works for the currently
50 running executable, which means that we know the XCOFF mode at
51 configure time. */
52
53 #if BACKTRACE_XCOFF_SIZE != 32 && BACKTRACE_XCOFF_SIZE != 64
54 #error "Unknown BACKTRACE_XCOFF_SIZE"
55 #endif
56
57 /* XCOFF file header. */
58
59 #if BACKTRACE_XCOFF_SIZE == 32
60
61 typedef struct {
62 uint16_t f_magic;
63 uint16_t f_nscns;
64 uint32_t f_timdat;
65 uint32_t f_symptr;
66 uint32_t f_nsyms;
67 uint16_t f_opthdr;
68 uint16_t f_flags;
69 } b_xcoff_filhdr;
70
71 #define XCOFF_MAGIC 0737
72
73 #else /* BACKTRACE_XCOFF_SIZE != 32 */
74
75 typedef struct {
76 uint16_t f_magic;
77 uint16_t f_nscns;
78 uint32_t f_timdat;
79 uint64_t f_symptr;
80 uint16_t f_opthdr;
81 uint16_t f_flags;
82 uint32_t f_nsyms;
83 } b_xcoff_filhdr;
84
85 #define XCOFF_MAGIC 0767
86
87 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
88
89 #define F_SHROBJ 0x2000 /* File is a shared object. */
90
91 /* XCOFF section header. */
92
93 #if BACKTRACE_XCOFF_SIZE == 32
94
95 typedef struct {
96 char s_name[8];
97 uint32_t s_paddr;
98 uint32_t s_vaddr;
99 uint32_t s_size;
100 uint32_t s_scnptr;
101 uint32_t s_relptr;
102 uint32_t s_lnnoptr;
103 uint16_t s_nreloc;
104 uint16_t s_nlnno;
105 uint32_t s_flags;
106 } b_xcoff_scnhdr;
107
108 #define _OVERFLOW_MARKER 65535
109
110 #else /* BACKTRACE_XCOFF_SIZE != 32 */
111
112 typedef struct {
113 char name[8];
114 uint64_t s_paddr;
115 uint64_t s_vaddr;
116 uint64_t s_size;
117 uint64_t s_scnptr;
118 uint64_t s_relptr;
119 uint64_t s_lnnoptr;
120 uint32_t s_nreloc;
121 uint32_t s_nlnno;
122 uint32_t s_flags;
123 } b_xcoff_scnhdr;
124
125 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
126
127 #define STYP_TEXT 0x20 /* Executable text (code) section. */
128 #define STYP_OVRFLO 0x8000 /* Line-number field overflow section. */
129
130 /* XCOFF symbol. */
131
132 #define SYMNMLEN 8
133
134 #if BACKTRACE_XCOFF_SIZE == 32
135
136 typedef struct {
137 union {
138 char _name[SYMNMLEN];
139 struct {
140 uint32_t _zeroes;
141 uint32_t _offset;
142 } _s;
143 } _u;
144 #define n_name _u._name
145 #define n_zeroes _u._s._zeroes
146 #define n_offset_ _u._s._offset
147
148 uint32_t n_value;
149 int16_t n_scnum;
150 uint16_t n_type;
151 uint8_t n_sclass;
152 uint8_t n_numaux;
153 } __attribute__ ((packed)) b_xcoff_syment;
154
155 #else /* BACKTRACE_XCOFF_SIZE != 32 */
156
157 typedef struct {
158 uint64_t n_value;
159 uint32_t n_offset_;
160 int16_t n_scnum;
161 uint16_t n_type;
162 uint8_t n_sclass;
163 uint8_t n_numaux;
164 } __attribute__ ((packed)) b_xcoff_syment;
165
166 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
167
168 #define SYMESZ 18
169
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. */
177
178 #define ISFCN(x) ((x) & 0x0020)
179
180 /* XCOFF AUX entry. */
181
182 #define AUXESZ 18
183 #define FILNMLEN 14
184
185 typedef union {
186 #if BACKTRACE_XCOFF_SIZE == 32
187 struct {
188 uint16_t pad;
189 uint16_t x_lnnohi;
190 uint16_t x_lnno;
191 } x_block;
192 #else
193 struct {
194 uint32_t x_lnno;
195 } x_block;
196 #endif
197 union {
198 char x_fname[FILNMLEN];
199 struct {
200 uint32_t x_zeroes;
201 uint32_t x_offset;
202 char pad[FILNMLEN-8];
203 uint8_t x_ftype;
204 } _x;
205 } x_file;
206 #if BACKTRACE_XCOFF_SIZE == 32
207 struct {
208 uint32_t x_exptr;
209 uint32_t x_fsize;
210 uint32_t x_lnnoptr;
211 uint32_t x_endndx;
212 } x_fcn;
213 #else
214 struct {
215 uint64_t x_lnnoptr;
216 uint32_t x_fsize;
217 uint32_t x_endndx;
218 } x_fcn;
219 #endif
220 struct {
221 uint8_t pad[AUXESZ-1];
222 uint8_t x_auxtype;
223 } x_auxtype;
224 } __attribute__ ((packed)) b_xcoff_auxent;
225
226 /* XCOFF line number entry. */
227
228 #if BACKTRACE_XCOFF_SIZE == 32
229
230 typedef struct {
231 union {
232 uint32_t l_symndx;
233 uint32_t l_paddr;
234 } l_addr;
235 uint16_t l_lnno;
236 } b_xcoff_lineno;
237
238 #define LINESZ 6
239
240 #else /* BACKTRACE_XCOFF_SIZE != 32 */
241
242 typedef struct {
243 union {
244 uint32_t l_symndx;
245 uint64_t l_paddr;
246 } l_addr;
247 uint32_t l_lnno;
248 } b_xcoff_lineno;
249
250 #define LINESZ 12
251
252 #endif /* BACKTRACE_XCOFF_SIZE != 32 */
253
254 #if BACKTRACE_XCOFF_SIZE == 32
255 #define XCOFF_AIX_TEXTBASE 0x10000000u
256 #else
257 #define XCOFF_AIX_TEXTBASE 0x100000000ul
258 #endif
259
260 /* AIX big archive fixed-length header. */
261
262 #define AIAMAGBIG "<bigaf>\n"
263
264 typedef struct {
265 char fl_magic[8]; /* Archive magic string. */
266 char fl_memoff[20]; /* Offset to member table. */
267 char fl_gstoff[20]; /* Offset to global symbol table. */
268 char fl_gst64off[20]; /* Offset to global symbol table for 64-bit objects. */
269 char fl_fstmoff[20]; /* Offset to first archive member. */
270 char fl_freeoff[20]; /* Offset to first member on free list. */
271 } b_ar_fl_hdr;
272
273 /* AIX big archive file member header. */
274
275 typedef struct {
276 char ar_size[20]; /* File member size - decimal. */
277 char ar_nxtmem[20]; /* Next member offset - decimal. */
278 char ar_prvmem[20]; /* Previous member offset - decimal. */
279 char ar_date[12]; /* File member date - decimal. */
280 char ar_uid[12]; /* File member userid - decimal. */
281 char ar_gid[12]; /* File member group id - decimal. */
282 char ar_mode[12]; /* File member mode - octal. */
283 char ar_namlen[4]; /* File member name length - decimal. */
284 char ar_name[2]; /* Start of member name. */
285 } b_ar_hdr;
286
287
288 /* Information we keep for an XCOFF symbol. */
289
290 struct xcoff_symbol
291 {
292 /* The name of the symbol. */
293 const char *name;
294 /* The address of the symbol. */
295 uintptr_t address;
296 /* The size of the symbol. */
297 size_t size;
298 };
299
300 /* Information to pass to xcoff_syminfo. */
301
302 struct xcoff_syminfo_data
303 {
304 /* Symbols for the next module. */
305 struct xcoff_syminfo_data *next;
306 /* The XCOFF symbols, sorted by address. */
307 struct xcoff_symbol *symbols;
308 /* The number of symbols. */
309 size_t count;
310 };
311
312 /* Information about an include file. */
313
314 struct xcoff_incl
315 {
316 /* File name. */
317 const char *filename;
318 /* Offset to first line number from the include file. */
319 uintptr_t begin;
320 /* Offset to last line number from the include file. */
321 uintptr_t end;
322 };
323
324 /* A growable vector of include files information. */
325
326 struct xcoff_incl_vector
327 {
328 /* Memory. This is an array of struct xcoff_incl. */
329 struct backtrace_vector vec;
330 /* Number of include files. */
331 size_t count;
332 };
333
334 /* Map a single PC value to a file/function/line. */
335
336 struct xcoff_line
337 {
338 /* PC. */
339 uintptr_t pc;
340 /* File name. Many entries in the array are expected to point to
341 the same file name. */
342 const char *filename;
343 /* Function name. */
344 const char *function;
345 /* Line number. */
346 int lineno;
347 };
348
349 /* A growable vector of line number information. This is used while
350 reading the line numbers. */
351
352 struct xcoff_line_vector
353 {
354 /* Memory. This is an array of struct xcoff_line. */
355 struct backtrace_vector vec;
356 /* Number of valid mappings. */
357 size_t count;
358 };
359
360 /* The information we need to map a PC to a file and line. */
361
362 struct xcoff_fileline_data
363 {
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;
368 };
369
370
371 /* A dummy callback function used when we can't find any debug info. */
372
373 static int
374 xcoff_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
375 uintptr_t pc ATTRIBUTE_UNUSED,
376 backtrace_full_callback callback ATTRIBUTE_UNUSED,
377 backtrace_error_callback error_callback, void *data)
378 {
379 error_callback (data, "no debug info in XCOFF executable", -1);
380 return 0;
381 }
382
383 /* A dummy callback function used when we can't find a symbol
384 table. */
385
386 static void
387 xcoff_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
388 uintptr_t addr ATTRIBUTE_UNUSED,
389 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
390 backtrace_error_callback error_callback, void *data)
391 {
392 error_callback (data, "no symbol table in XCOFF executable", -1);
393 }
394
395 /* Compare struct xcoff_symbol for qsort. */
396
397 static int
398 xcoff_symbol_compare (const void *v1, const void *v2)
399 {
400 const struct xcoff_symbol *e1 = (const struct xcoff_symbol *) v1;
401 const struct xcoff_symbol *e2 = (const struct xcoff_symbol *) v2;
402
403 if (e1->address < e2->address)
404 return -1;
405 else if (e1->address > e2->address)
406 return 1;
407 else
408 return 0;
409 }
410
411 /* Compare an ADDR against an xcoff_symbol for bsearch. */
412
413 static int
414 xcoff_symbol_search (const void *vkey, const void *ventry)
415 {
416 const uintptr_t *key = (const uintptr_t *) vkey;
417 const struct xcoff_symbol *entry = (const struct xcoff_symbol *) ventry;
418 uintptr_t addr;
419
420 addr = *key;
421 if (addr < entry->address)
422 return -1;
423 else if ((entry->size == 0 && addr > entry->address)
424 || (entry->size > 0 && addr >= entry->address + entry->size))
425 return 1;
426 else
427 return 0;
428 }
429
430 /* Add XDATA to the list in STATE. */
431
432 static void
433 xcoff_add_syminfo_data (struct backtrace_state *state,
434 struct xcoff_syminfo_data *xdata)
435 {
436 if (!state->threaded)
437 {
438 struct xcoff_syminfo_data **pp;
439
440 for (pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
441 *pp != NULL;
442 pp = &(*pp)->next)
443 ;
444 *pp = xdata;
445 }
446 else
447 {
448 while (1)
449 {
450 struct xcoff_syminfo_data **pp;
451
452 pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
453
454 while (1)
455 {
456 struct xcoff_syminfo_data *p;
457
458 p = backtrace_atomic_load_pointer (pp);
459
460 if (p == NULL)
461 break;
462
463 pp = &p->next;
464 }
465
466 if (__sync_bool_compare_and_swap (pp, NULL, xdata))
467 break;
468 }
469 }
470 }
471
472 /* Return the symbol name and value for an ADDR. */
473
474 static void
475 xcoff_syminfo (struct backtrace_state *state ATTRIBUTE_UNUSED, uintptr_t addr,
476 backtrace_syminfo_callback callback,
477 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
478 void *data)
479 {
480 struct xcoff_syminfo_data *edata;
481 struct xcoff_symbol *sym = NULL;
482
483 if (!state->threaded)
484 {
485 for (edata = (struct xcoff_syminfo_data *) state->syminfo_data;
486 edata != NULL;
487 edata = edata->next)
488 {
489 sym = ((struct xcoff_symbol *)
490 bsearch (&addr, edata->symbols, edata->count,
491 sizeof (struct xcoff_symbol), xcoff_symbol_search));
492 if (sym != NULL)
493 break;
494 }
495 }
496 else
497 {
498 struct xcoff_syminfo_data **pp;
499
500 pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
501 while (1)
502 {
503 edata = backtrace_atomic_load_pointer (pp);
504 if (edata == NULL)
505 break;
506
507 sym = ((struct xcoff_symbol *)
508 bsearch (&addr, edata->symbols, edata->count,
509 sizeof (struct xcoff_symbol), xcoff_symbol_search));
510 if (sym != NULL)
511 break;
512
513 pp = &edata->next;
514 }
515 }
516
517 if (sym == NULL)
518 callback (data, addr, NULL, 0, 0);
519 else
520 callback (data, addr, sym->name, sym->address, sym->size);
521 }
522
523 /* Return the name of an XCOFF symbol. */
524
525 static const char *
526 xcoff_symname (const b_xcoff_syment *asym,
527 const unsigned char *strtab, size_t strtab_size)
528 {
529 #if BACKTRACE_XCOFF_SIZE == 32
530 if (asym->n_zeroes != 0)
531 {
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);
537 }
538 #endif
539 if (asym->n_sclass & 0x80)
540 return NULL; /* .debug */
541 if (asym->n_offset_ >= strtab_size)
542 return NULL;
543 return (const char *) strtab + asym->n_offset_;
544 }
545
546 /* Initialize the symbol table info for xcoff_syminfo. */
547
548 static int
549 xcoff_initialize_syminfo (struct backtrace_state *state,
550 uintptr_t base_address,
551 const b_xcoff_scnhdr *sects,
552 const b_xcoff_syment *syms, size_t nsyms,
553 const unsigned char *strtab, size_t strtab_size,
554 backtrace_error_callback error_callback, void *data,
555 struct xcoff_syminfo_data *sdata)
556 {
557 size_t xcoff_symbol_count;
558 size_t xcoff_symbol_size;
559 struct xcoff_symbol *xcoff_symbols;
560 size_t i;
561 unsigned int j;
562
563 /* We only care about function symbols. Count them. */
564 xcoff_symbol_count = 0;
565 for (i = 0; i < nsyms; ++i)
566 {
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;
572
573 i += asym->n_numaux;
574 }
575
576 xcoff_symbol_size = xcoff_symbol_count * sizeof (struct xcoff_symbol);
577 xcoff_symbols = ((struct xcoff_symbol *)
578 backtrace_alloc (state, xcoff_symbol_size, error_callback,
579 data));
580 if (xcoff_symbols == NULL)
581 return 0;
582
583 j = 0;
584 for (i = 0; i < nsyms; ++i)
585 {
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)
590 {
591 const b_xcoff_auxent *aux = (const b_xcoff_auxent *) (asym + 1);
592 xcoff_symbols[j].name = xcoff_symname (asym, strtab, strtab_size);
593 xcoff_symbols[j].address = base_address + asym->n_value
594 - sects[asym->n_scnum - 1].s_paddr;
595 /* x_fsize will be 0 if there is no debug information. */
596 xcoff_symbols[j].size = aux->x_fcn.x_fsize;
597 ++j;
598 }
599
600 i += asym->n_numaux;
601 }
602
603 backtrace_qsort (xcoff_symbols, xcoff_symbol_count,
604 sizeof (struct xcoff_symbol), xcoff_symbol_compare);
605
606 sdata->next = NULL;
607 sdata->symbols = xcoff_symbols;
608 sdata->count = xcoff_symbol_count;
609
610 return 1;
611 }
612
613 /* Compare struct xcoff_line for qsort. */
614
615 static int
616 xcoff_line_compare (const void *v1, const void *v2)
617 {
618 const struct xcoff_line *ln1 = (const struct xcoff_line *) v1;
619 const struct xcoff_line *ln2 = (const struct xcoff_line *) v2;
620
621 if (ln1->pc < ln2->pc)
622 return -1;
623 else if (ln1->pc > ln2->pc)
624 return 1;
625 else
626 return 0;
627 }
628
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. */
632
633 static int
634 xcoff_line_search (const void *vkey, const void *ventry)
635 {
636 const uintptr_t *key = (const uintptr_t *) vkey;
637 const struct xcoff_line *entry = (const struct xcoff_line *) ventry;
638 uintptr_t pc;
639
640 pc = *key;
641 if (pc < entry->pc)
642 return -1;
643 else if ((entry + 1)->pc == (uintptr_t) -1 || pc >= (entry + 1)->pc)
644 return 1;
645 else
646 return 0;
647 }
648
649 /* Look for a PC in the line vector for one module. On success,
650 call CALLBACK and return whatever it returns. On error, call
651 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
652 0 if not. */
653
654 static int
655 xcoff_lookup_pc (struct backtrace_state *state ATTRIBUTE_UNUSED,
656 struct xcoff_fileline_data *fdata, uintptr_t pc,
657 backtrace_full_callback callback,
658 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
659 void *data, int *found)
660 {
661 const struct xcoff_line *ln;
662 const char *function;
663
664 *found = 1;
665
666 ln = (struct xcoff_line *) bsearch (&pc, fdata->vec.vec.base,
667 fdata->vec.count,
668 sizeof (struct xcoff_line),
669 xcoff_line_search);
670 if (ln == NULL)
671 {
672 *found = 0;
673 return 0;
674 }
675
676 function = ln->function;
677 /* AIX prepends a '.' to function entry points, remove it. */
678 if (*function == '.')
679 ++function;
680 return callback (data, pc, ln->filename, ln->lineno, function);
681 }
682
683 /* Return the file/line information for a PC using the XCOFF lineno
684 mapping we built earlier. */
685
686 static int
687 xcoff_fileline (struct backtrace_state *state, uintptr_t pc,
688 backtrace_full_callback callback,
689 backtrace_error_callback error_callback, void *data)
690
691 {
692 struct xcoff_fileline_data *fdata;
693 int found;
694 int ret;
695
696 if (!state->threaded)
697 {
698 for (fdata = (struct xcoff_fileline_data *) state->fileline_data;
699 fdata != NULL;
700 fdata = fdata->next)
701 {
702 ret = xcoff_lookup_pc (state, fdata, pc, callback, error_callback,
703 data, &found);
704 if (ret != 0 || found)
705 return ret;
706 }
707 }
708 else
709 {
710 struct xcoff_fileline_data **pp;
711
712 pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
713 while (1)
714 {
715 fdata = backtrace_atomic_load_pointer (pp);
716 if (fdata == NULL)
717 break;
718
719 ret = xcoff_lookup_pc (state, fdata, pc, callback, error_callback,
720 data, &found);
721 if (ret != 0 || found)
722 return ret;
723
724 pp = &fdata->next;
725 }
726 }
727
728 /* FIXME: See if any libraries have been dlopen'ed. */
729
730 return callback (data, pc, NULL, 0, NULL);
731 }
732
733 /* Add a new mapping to the vector of line mappings that we are
734 building. Returns 1 on success, 0 on failure. */
735
736 static int
737 xcoff_add_line (struct backtrace_state *state, uintptr_t pc,
738 const char *filename, const char *function, uint32_t lnno,
739 backtrace_error_callback error_callback, void *data,
740 struct xcoff_line_vector *vec)
741 {
742 struct xcoff_line *ln;
743
744 ln = ((struct xcoff_line *)
745 backtrace_vector_grow (state, sizeof (struct xcoff_line),
746 error_callback, data, &vec->vec));
747 if (ln == NULL)
748 return 0;
749
750 ln->pc = pc;
751 ln->filename = filename;
752 ln->function = function;
753 ln->lineno = lnno;
754
755 ++vec->count;
756
757 return 1;
758 }
759
760 /* Add the line number entries for a function to the line vector. */
761
762 static int
763 xcoff_process_linenos (struct backtrace_state *state, uintptr_t base_address,
764 const b_xcoff_syment *fsym, const char *filename,
765 const b_xcoff_scnhdr *sects,
766 const unsigned char *strtab, size_t strtab_size,
767 uint32_t fcn_lnno, struct xcoff_incl_vector *vec,
768 struct xcoff_line_vector *lvec,
769 const unsigned char *linenos, size_t linenos_size,
770 uintptr_t lnnoptr0,
771 backtrace_error_callback error_callback, void *data)
772 {
773 const b_xcoff_auxent *aux;
774 const b_xcoff_lineno *lineno;
775 const unsigned char *lineptr;
776 const char *function;
777 struct xcoff_incl *incl = NULL;
778 uintptr_t lnnoptr;
779 uintptr_t pc;
780 uint32_t lnno;
781 int begincl;
782 size_t i;
783
784 aux = (const b_xcoff_auxent *) (fsym + 1);
785 lnnoptr = aux->x_fcn.x_lnnoptr;
786
787 if (lnnoptr < lnnoptr0 || lnnoptr + LINESZ > lnnoptr0 + linenos_size)
788 return 0;
789
790 function = xcoff_symname (fsym, strtab, strtab_size);
791 if (function == NULL)
792 return 0;
793
794 /* Skip first entry that points to symtab. */
795
796 lnnoptr += LINESZ;
797
798 lineptr = linenos + (lnnoptr - lnnoptr0);
799
800 begincl = -1;
801 while (lineptr + LINESZ <= linenos + linenos_size)
802 {
803 lineno = (const b_xcoff_lineno *) lineptr;
804
805 lnno = lineno->l_lnno;
806 if (lnno == 0)
807 break;
808
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)
813 {
814 incl = (struct xcoff_incl *) vec->vec.base + i;
815 if (incl->begin <= lnnoptr && lnnoptr <= incl->end)
816 break;
817 }
818 if (begincl == -1)
819 begincl = (i < vec->count);
820 if (i < vec->count)
821 {
822 filename = incl->filename;
823 if (begincl == 1)
824 lnno += fcn_lnno - 1;
825 }
826 else
827 lnno += fcn_lnno - 1;
828
829 pc = base_address + lineno->l_addr.l_paddr
830 - sects[fsym->n_scnum - 1].s_paddr;
831 xcoff_add_line (state, pc, filename, function, lnno, error_callback,
832 data, lvec);
833
834 lnnoptr += LINESZ;
835 lineptr += LINESZ;
836 }
837
838 return 1;
839 }
840
841 /* Initialize the line vector info for xcoff_fileline. */
842
843 static int
844 xcoff_initialize_fileline (struct backtrace_state *state,
845 uintptr_t base_address,
846 const b_xcoff_scnhdr *sects,
847 const b_xcoff_syment *syms, size_t nsyms,
848 const unsigned char *strtab, size_t strtab_size,
849 const unsigned char *linenos, size_t linenos_size,
850 uint64_t lnnoptr0,
851 backtrace_error_callback error_callback, void *data)
852 {
853 struct xcoff_fileline_data *fdata;
854 struct xcoff_incl_vector vec;
855 struct xcoff_line *ln;
856 const b_xcoff_syment *fsym;
857 const b_xcoff_auxent *aux;
858 const char *filename;
859 const char *name;
860 struct xcoff_incl *incl;
861 uintptr_t begin, end;
862 uintptr_t lnno;
863 size_t i;
864
865 fdata = ((struct xcoff_fileline_data *)
866 backtrace_alloc (state, sizeof (struct xcoff_fileline_data),
867 error_callback, data));
868 if (fdata == NULL)
869 return 0;
870
871 memset (fdata, 0, sizeof *fdata);
872 memset (&vec, 0, sizeof vec);
873
874 /* Process include files first. */
875
876 begin = 0;
877 for (i = 0; i < nsyms; ++i)
878 {
879 const b_xcoff_syment *asym = &syms[i];
880
881 switch (asym->n_sclass)
882 {
883 case C_BINCL:
884 begin = asym->n_value;
885 break;
886
887 case C_EINCL:
888 if (begin == 0)
889 break;
890 end = asym->n_value;
891 incl = ((struct xcoff_incl *)
892 backtrace_vector_grow (state, sizeof (struct xcoff_incl),
893 error_callback, data, &vec.vec));
894 if (incl != NULL)
895 {
896 incl->filename = xcoff_symname (asym, strtab, strtab_size);
897 incl->begin = begin;
898 incl->end = end;
899 ++vec.count;
900 }
901 begin = 0;
902 break;
903 }
904
905 i += asym->n_numaux;
906 }
907
908 filename = NULL;
909 fsym = NULL;
910 for (i = 0; i < nsyms; ++i)
911 {
912 const b_xcoff_syment *asym = &syms[i];
913
914 switch (asym->n_sclass)
915 {
916 case C_FILE:
917 filename = xcoff_symname (asym, strtab, strtab_size);
918 if (filename == NULL)
919 break;
920
921 /* If the file auxiliary entry is not used, the symbol name is
922 the name of the source file. If the file auxiliary entry is
923 used, then the symbol name should be .file, and the first
924 file auxiliary entry (by convention) contains the source
925 file name. */
926
927 if (asym->n_numaux > 0 && !strcmp (filename, ".file"))
928 {
929 aux = (const b_xcoff_auxent *) (asym + 1);
930 if (aux->x_file._x.x_zeroes != 0)
931 {
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);
937 }
938 else if (aux->x_file._x.x_offset < strtab_size)
939 filename = (const char *) strtab + aux->x_file._x.x_offset;
940 else
941 filename = NULL;
942 }
943 break;
944
945 case C_EXT:
946 case C_HIDEXT:
947 case C_WEAKEXT:
948 fsym = NULL;
949 if (!ISFCN (asym->n_type) || asym->n_numaux == 0)
950 break;
951 if (filename == NULL)
952 break;
953 fsym = asym;
954 break;
955
956 case C_FCN:
957 if (asym->n_numaux == 0)
958 break;
959 if (fsym == NULL)
960 break;
961 name = xcoff_symname (asym, strtab, strtab_size);
962 if (name == NULL)
963 break;
964 aux = (const b_xcoff_auxent *) (asym + 1);
965 #if BACKTRACE_XCOFF_SIZE == 32
966 lnno = (uint32_t) aux->x_block.x_lnnohi << 16
967 | aux->x_block.x_lnno;
968 #else
969 lnno = aux->x_block.x_lnno;
970 #endif
971 if (!strcmp (name, ".bf"))
972 {
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);
977 }
978 else if (!strcmp (name, ".ef"))
979 {
980 fsym = NULL;
981 }
982 break;
983 }
984
985 i += asym->n_numaux;
986 }
987
988 /* Allocate one extra entry at the end. */
989 ln = ((struct xcoff_line *)
990 backtrace_vector_grow (state, sizeof (struct xcoff_line),
991 error_callback, data, &fdata->vec.vec));
992 if (ln == NULL)
993 goto fail;
994 ln->pc = (uintptr_t) -1;
995 ln->filename = NULL;
996 ln->function = NULL;
997 ln->lineno = 0;
998
999 if (!backtrace_vector_release (state, &fdata->vec.vec, error_callback, data))
1000 goto fail;
1001
1002 backtrace_qsort (fdata->vec.vec.base, fdata->vec.count,
1003 sizeof (struct xcoff_line), xcoff_line_compare);
1004
1005 if (!state->threaded)
1006 {
1007 struct xcoff_fileline_data **pp;
1008
1009 for (pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
1010 *pp != NULL;
1011 pp = &(*pp)->next)
1012 ;
1013 *pp = fdata;
1014 }
1015 else
1016 {
1017 while (1)
1018 {
1019 struct xcoff_fileline_data **pp;
1020
1021 pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
1022
1023 while (1)
1024 {
1025 struct xcoff_fileline_data *p;
1026
1027 p = backtrace_atomic_load_pointer (pp);
1028
1029 if (p == NULL)
1030 break;
1031
1032 pp = &p->next;
1033 }
1034
1035 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
1036 break;
1037 }
1038 }
1039
1040 return 1;
1041
1042 fail:
1043 return 0;
1044 }
1045
1046 /* Add the backtrace data for one XCOFF file. Returns 1 on success,
1047 0 on failure (in both cases descriptor is closed). */
1048
1049 static int
1050 xcoff_add (struct backtrace_state *state, int descriptor, off_t offset,
1051 uintptr_t base_address, backtrace_error_callback error_callback,
1052 void *data, fileline *fileline_fn, int *found_sym, int exe)
1053 {
1054 struct backtrace_view fhdr_view;
1055 struct backtrace_view sects_view;
1056 struct backtrace_view linenos_view;
1057 struct backtrace_view syms_view;
1058 struct backtrace_view str_view;
1059 b_xcoff_filhdr fhdr;
1060 const b_xcoff_scnhdr *sects;
1061 const b_xcoff_scnhdr *stext;
1062 uint64_t lnnoptr;
1063 uint32_t nlnno;
1064 off_t str_off;
1065 size_t sects_size;
1066 size_t syms_size;
1067 int32_t str_size;
1068 int sects_view_valid;
1069 int linenos_view_valid;
1070 int syms_view_valid;
1071 int str_view_valid;
1072 int magic_ok;
1073 int i;
1074
1075 *found_sym = 0;
1076
1077 sects_view_valid = 0;
1078 linenos_view_valid = 0;
1079 syms_view_valid = 0;
1080 str_view_valid = 0;
1081
1082 /* Map the XCOFF file header. */
1083 if (!backtrace_get_view (state, descriptor, offset, sizeof (b_xcoff_filhdr),
1084 error_callback, data, &fhdr_view))
1085 goto fail;
1086
1087 memcpy (&fhdr, fhdr_view.data, sizeof fhdr);
1088 magic_ok = (fhdr.f_magic == XCOFF_MAGIC);
1089
1090 backtrace_release_view (state, &fhdr_view, error_callback, data);
1091
1092 if (!magic_ok)
1093 {
1094 if (exe)
1095 error_callback (data, "executable file is not XCOFF", 0);
1096 goto fail;
1097 }
1098
1099 /* Verify object is of expected type. */
1100 if ((exe && (fhdr.f_flags & F_SHROBJ))
1101 || (!exe && !(fhdr.f_flags & F_SHROBJ)))
1102 goto fail;
1103
1104 /* Read the section headers. */
1105
1106 sects_size = fhdr.f_nscns * sizeof (b_xcoff_scnhdr);
1107
1108 if (!backtrace_get_view (state, descriptor,
1109 offset + sizeof (fhdr) + fhdr.f_opthdr,
1110 sects_size, error_callback, data, &sects_view))
1111 goto fail;
1112 sects_view_valid = 1;
1113 sects = (const b_xcoff_scnhdr *) sects_view.data;
1114
1115 /* FIXME: assumes only one .text section. */
1116 for (i = 0; i < fhdr.f_nscns; ++i)
1117 if ((sects[i].s_flags & 0xffff) == STYP_TEXT)
1118 break;
1119 if (i == fhdr.f_nscns)
1120 goto fail;
1121
1122 stext = &sects[i];
1123
1124 /* AIX ldinfo_textorg includes the XCOFF headers. */
1125 base_address = (exe ? XCOFF_AIX_TEXTBASE : base_address) + stext->s_scnptr;
1126
1127 lnnoptr = stext->s_lnnoptr;
1128 nlnno = stext->s_nlnno;
1129
1130 #if BACKTRACE_XCOFF_SIZE == 32
1131 if (nlnno == _OVERFLOW_MARKER)
1132 {
1133 int sntext = i + 1;
1134 /* Find the matching .ovrflo section. */
1135 for (i = 0; i < fhdr.f_nscns; ++i)
1136 {
1137 if (((sects[i].s_flags & 0xffff) == STYP_OVRFLO)
1138 && sects[i].s_nlnno == sntext)
1139 {
1140 nlnno = sects[i].s_vaddr;
1141 break;
1142 }
1143 }
1144 }
1145 #endif
1146
1147 /* Read the symbol table and the string table. */
1148
1149 if (fhdr.f_symptr != 0)
1150 {
1151 struct xcoff_syminfo_data *sdata;
1152
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);
1157
1158 if (!backtrace_get_view (state, descriptor, offset + fhdr.f_symptr,
1159 syms_size + 4, error_callback, data,
1160 &syms_view))
1161 goto fail;
1162 syms_view_valid = 1;
1163
1164 memcpy (&str_size, syms_view.data + syms_size, 4);
1165
1166 str_off = fhdr.f_symptr + syms_size;
1167
1168 if (str_size > 4)
1169 {
1170 /* Map string table (including the length word). */
1171
1172 if (!backtrace_get_view (state, descriptor, offset + str_off,
1173 str_size, error_callback, data, &str_view))
1174 goto fail;
1175 str_view_valid = 1;
1176 }
1177
1178 sdata = ((struct xcoff_syminfo_data *)
1179 backtrace_alloc (state, sizeof *sdata, error_callback, data));
1180 if (sdata == NULL)
1181 goto fail;
1182
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))
1187 {
1188 backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
1189 goto fail;
1190 }
1191
1192 *found_sym = 1;
1193
1194 xcoff_add_syminfo_data (state, sdata);
1195 }
1196
1197 /* Read the line number entries. */
1198
1199 if (fhdr.f_symptr != 0 && lnnoptr != 0)
1200 {
1201 size_t linenos_size = (size_t) nlnno * LINESZ;
1202
1203 if (!backtrace_get_view (state, descriptor, offset + lnnoptr,
1204 linenos_size,
1205 error_callback, data, &linenos_view))
1206 goto fail;
1207 linenos_view_valid = 1;
1208
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;
1215
1216 backtrace_release_view (state, &linenos_view, error_callback, data);
1217 linenos_view_valid = 0;
1218 }
1219
1220 backtrace_release_view (state, &sects_view, error_callback, data);
1221 sects_view_valid = 0;
1222 if (syms_view_valid)
1223 backtrace_release_view (state, &syms_view, error_callback, data);
1224 syms_view_valid = 0;
1225
1226 /* We've read all we need from the executable. */
1227 if (!backtrace_close (descriptor, error_callback, data))
1228 goto fail;
1229 descriptor = -1;
1230
1231 return 1;
1232
1233 fail:
1234 if (sects_view_valid)
1235 backtrace_release_view (state, &sects_view, error_callback, data);
1236 if (str_view_valid)
1237 backtrace_release_view (state, &str_view, error_callback, data);
1238 if (syms_view_valid)
1239 backtrace_release_view (state, &syms_view, error_callback, data);
1240 if (linenos_view_valid)
1241 backtrace_release_view (state, &linenos_view, error_callback, data);
1242 if (descriptor != -1 && offset == 0)
1243 backtrace_close (descriptor, error_callback, data);
1244 return 0;
1245 }
1246
1247 #ifdef HAVE_LOADQUERY
1248
1249 /* Read an integer value in human-readable format from an AIX
1250 big archive fixed-length or member header. */
1251
1252 static int
1253 xcoff_parse_decimal (const char *buf, size_t size, off_t *off)
1254 {
1255 char str[32];
1256 char *end;
1257
1258 if (size >= sizeof str)
1259 return 0;
1260 memcpy (str, buf, size);
1261 str[size] = '\0';
1262 *off = strtol (str, &end, 10);
1263 if (*end != '\0' && *end != ' ')
1264 return 0;
1265
1266 return 1;
1267 }
1268
1269 /* Add the backtrace data for a member of an AIX big archive.
1270 Returns 1 on success, 0 on failure. */
1271
1272 static int
1273 xcoff_armem_add (struct backtrace_state *state, int descriptor,
1274 uintptr_t base_address, const char *member,
1275 backtrace_error_callback error_callback, void *data,
1276 fileline *fileline_fn, int *found_sym)
1277 {
1278 struct backtrace_view view;
1279 b_ar_fl_hdr fl_hdr;
1280 const b_ar_hdr *ar_hdr;
1281 off_t off;
1282 off_t len;
1283 int memlen;
1284
1285 *found_sym = 0;
1286
1287 /* Map archive fixed-length header. */
1288
1289 if (!backtrace_get_view (state, descriptor, 0, sizeof (b_ar_fl_hdr),
1290 error_callback, data, &view))
1291 goto fail;
1292
1293 memcpy (&fl_hdr, view.data, sizeof (b_ar_fl_hdr));
1294
1295 backtrace_release_view (state, &view, error_callback, data);
1296
1297 if (memcmp (fl_hdr.fl_magic, AIAMAGBIG, 8) != 0)
1298 goto fail;
1299
1300 memlen = strlen (member);
1301
1302 /* Read offset of first archive member. */
1303 if (!xcoff_parse_decimal (fl_hdr.fl_fstmoff, sizeof fl_hdr.fl_fstmoff, &off))
1304 goto fail;
1305 while (off != 0)
1306 {
1307 /* Map archive member header and member name. */
1308
1309 if (!backtrace_get_view (state, descriptor, off,
1310 sizeof (b_ar_hdr) + memlen,
1311 error_callback, data, &view))
1312 break;
1313
1314 ar_hdr = (const b_ar_hdr *) view.data;
1315
1316 /* Read archive member name length. */
1317 if (!xcoff_parse_decimal (ar_hdr->ar_namlen, sizeof ar_hdr->ar_namlen,
1318 &len))
1319 {
1320 backtrace_release_view (state, &view, error_callback, data);
1321 break;
1322 }
1323 if (len == memlen && !memcmp (ar_hdr->ar_name, member, memlen))
1324 {
1325 off = (off + sizeof (b_ar_hdr) + memlen + 1) & ~1;
1326
1327 /* The archive can contain several members with the same name
1328 (e.g. 32-bit and 64-bit), so continue if not ok. */
1329
1330 if (xcoff_add (state, descriptor, off, base_address, error_callback,
1331 data, fileline_fn, found_sym, 0))
1332 {
1333 backtrace_release_view (state, &view, error_callback, data);
1334 return 1;
1335 }
1336 }
1337
1338 /* Read offset of next archive member. */
1339 if (!xcoff_parse_decimal (ar_hdr->ar_nxtmem, sizeof ar_hdr->ar_nxtmem,
1340 &off))
1341 {
1342 backtrace_release_view (state, &view, error_callback, data);
1343 break;
1344 }
1345 backtrace_release_view (state, &view, error_callback, data);
1346 }
1347
1348 fail:
1349 /* No matching member found. */
1350 backtrace_close (descriptor, error_callback, data);
1351 return 0;
1352 }
1353
1354 /* Add the backtrace data for dynamically loaded libraries. */
1355
1356 static void
1357 xcoff_add_shared_libs (struct backtrace_state *state,
1358 backtrace_error_callback error_callback,
1359 void *data, fileline *fileline_fn, int *found_sym)
1360 {
1361 const struct ld_info *ldinfo;
1362 void *buf;
1363 unsigned int buflen;
1364 const char *member;
1365 int descriptor;
1366 int does_not_exist;
1367 int lib_found_sym;
1368 int ret;
1369
1370 /* Retrieve the list of loaded libraries. */
1371
1372 buf = NULL;
1373 buflen = 512;
1374 do
1375 {
1376 buf = realloc (buf, buflen);
1377 if (buf == NULL)
1378 {
1379 ret = -1;
1380 break;
1381 }
1382 ret = loadquery (L_GETINFO, buf, buflen);
1383 if (ret == 0)
1384 break;
1385 buflen *= 2;
1386 }
1387 while (ret == -1 && errno == ENOMEM);
1388 if (ret != 0)
1389 {
1390 free (buf);
1391 return;
1392 }
1393
1394 ldinfo = (const struct ld_info *) buf;
1395 while ((const char *) ldinfo < (const char *) buf + buflen)
1396 {
1397 if (*ldinfo->ldinfo_filename != '/')
1398 goto next;
1399
1400 descriptor = backtrace_open (ldinfo->ldinfo_filename, error_callback,
1401 data, &does_not_exist);
1402 if (descriptor < 0)
1403 goto next;
1404
1405 /* Check if it is an archive (member name not empty). */
1406
1407 member = ldinfo->ldinfo_filename + strlen (ldinfo->ldinfo_filename) + 1;
1408 if (*member)
1409 {
1410 xcoff_armem_add (state, descriptor,
1411 (uintptr_t) ldinfo->ldinfo_textorg, member,
1412 error_callback, data, fileline_fn, &lib_found_sym);
1413 }
1414 else
1415 {
1416 xcoff_add (state, descriptor, 0, (uintptr_t) ldinfo->ldinfo_textorg,
1417 error_callback, data, fileline_fn, &lib_found_sym, 0);
1418 }
1419 if (lib_found_sym)
1420 *found_sym = 1;
1421
1422 next:
1423 if (ldinfo->ldinfo_next == 0)
1424 break;
1425 ldinfo = (const struct ld_info *) ((const char *) ldinfo
1426 + ldinfo->ldinfo_next);
1427 }
1428
1429 free (buf);
1430 }
1431 #endif /* HAVE_LOADQUERY */
1432
1433 /* Initialize the backtrace data we need from an XCOFF executable.
1434 Returns 1 on success, 0 on failure. */
1435
1436 int
1437 backtrace_initialize (struct backtrace_state *state, int descriptor,
1438 backtrace_error_callback error_callback,
1439 void *data, fileline *fileline_fn)
1440 {
1441 int ret;
1442 int found_sym;
1443 fileline xcoff_fileline_fn = xcoff_nodebug;
1444
1445 ret = xcoff_add (state, descriptor, 0, 0, error_callback, data,
1446 &xcoff_fileline_fn, &found_sym, 1);
1447 if (!ret)
1448 return 0;
1449
1450 #ifdef HAVE_LOADQUERY
1451 xcoff_add_shared_libs (state, error_callback, data, &xcoff_fileline_fn,
1452 &found_sym);
1453 #endif
1454
1455 if (!state->threaded)
1456 {
1457 if (found_sym)
1458 state->syminfo_fn = xcoff_syminfo;
1459 else if (state->syminfo_fn == NULL)
1460 state->syminfo_fn = xcoff_nosyms;
1461 }
1462 else
1463 {
1464 if (found_sym)
1465 backtrace_atomic_store_pointer (&state->syminfo_fn, xcoff_syminfo);
1466 else
1467 __sync_bool_compare_and_swap (&state->syminfo_fn, NULL, xcoff_nosyms);
1468 }
1469
1470 if (!state->threaded)
1471 {
1472 if (state->fileline_fn == NULL || state->fileline_fn == xcoff_nodebug)
1473 *fileline_fn = xcoff_fileline_fn;
1474 }
1475 else
1476 {
1477 fileline current_fn;
1478
1479 current_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
1480 if (current_fn == NULL || current_fn == xcoff_nodebug)
1481 *fileline_fn = xcoff_fileline_fn;
1482 }
1483
1484 return 1;
1485 }