-
Notifications
You must be signed in to change notification settings - Fork 229
/
xcoff.c
1617 lines (1362 loc) · 41 KB
/
xcoff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* xcoff.c -- Get debug data from an XCOFF file for backtraces.
Copyright (C) 2012-2024 Free Software Foundation, Inc.
Adapted from elf.c.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
(1) Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
(2) Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
(3) The name of the author may not be used to
endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. */
#include "config.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_LOADQUERY
#include <sys/ldr.h>
#endif
#include "backtrace.h"
#include "internal.h"
/* The configure script must tell us whether we are 32-bit or 64-bit
XCOFF. We could make this code test and support either possibility,
but there is no point. This code only works for the currently
running executable, which means that we know the XCOFF mode at
configure time. */
#if BACKTRACE_XCOFF_SIZE != 32 && BACKTRACE_XCOFF_SIZE != 64
#error "Unknown BACKTRACE_XCOFF_SIZE"
#endif
/* XCOFF file header. */
#if BACKTRACE_XCOFF_SIZE == 32
typedef struct {
uint16_t f_magic;
uint16_t f_nscns;
uint32_t f_timdat;
uint32_t f_symptr;
uint32_t f_nsyms;
uint16_t f_opthdr;
uint16_t f_flags;
} b_xcoff_filhdr;
#define XCOFF_MAGIC 0737
#else /* BACKTRACE_XCOFF_SIZE != 32 */
typedef struct {
uint16_t f_magic;
uint16_t f_nscns;
uint32_t f_timdat;
uint64_t f_symptr;
uint16_t f_opthdr;
uint16_t f_flags;
uint32_t f_nsyms;
} b_xcoff_filhdr;
#define XCOFF_MAGIC 0767
#endif /* BACKTRACE_XCOFF_SIZE != 32 */
#define F_SHROBJ 0x2000 /* File is a shared object. */
/* XCOFF section header. */
#if BACKTRACE_XCOFF_SIZE == 32
typedef struct {
char s_name[8];
uint32_t s_paddr;
uint32_t s_vaddr;
uint32_t s_size;
uint32_t s_scnptr;
uint32_t s_relptr;
uint32_t s_lnnoptr;
uint16_t s_nreloc;
uint16_t s_nlnno;
uint32_t s_flags;
} b_xcoff_scnhdr;
#define _OVERFLOW_MARKER 65535
#else /* BACKTRACE_XCOFF_SIZE != 32 */
typedef struct {
char name[8];
uint64_t s_paddr;
uint64_t s_vaddr;
uint64_t s_size;
uint64_t s_scnptr;
uint64_t s_relptr;
uint64_t s_lnnoptr;
uint32_t s_nreloc;
uint32_t s_nlnno;
uint32_t s_flags;
} b_xcoff_scnhdr;
#endif /* BACKTRACE_XCOFF_SIZE != 32 */
#define STYP_DWARF 0x10 /* DWARF debugging section. */
#define STYP_TEXT 0x20 /* Executable text (code) section. */
#define STYP_OVRFLO 0x8000 /* Line-number field overflow section. */
#define SSUBTYP_DWINFO 0x10000 /* DWARF info section. */
#define SSUBTYP_DWLINE 0x20000 /* DWARF line-number section. */
#define SSUBTYP_DWARNGE 0x50000 /* DWARF aranges section. */
#define SSUBTYP_DWABREV 0x60000 /* DWARF abbreviation section. */
#define SSUBTYP_DWSTR 0x70000 /* DWARF strings section. */
#define SSUBTYP_DWRNGES 0x80000 /* DWARF ranges section. */
/* XCOFF symbol. */
#define SYMNMLEN 8
#if BACKTRACE_XCOFF_SIZE == 32
typedef struct {
union {
char _name[SYMNMLEN];
struct {
uint32_t _zeroes;
uint32_t _offset;
} _s;
} _u;
#define n_name _u._name
#define n_zeroes _u._s._zeroes
#define n_offset_ _u._s._offset
uint32_t n_value;
int16_t n_scnum;
uint16_t n_type;
uint8_t n_sclass;
uint8_t n_numaux;
} __attribute__ ((packed)) b_xcoff_syment;
#else /* BACKTRACE_XCOFF_SIZE != 32 */
typedef struct {
uint64_t n_value;
uint32_t n_offset_;
int16_t n_scnum;
uint16_t n_type;
uint8_t n_sclass;
uint8_t n_numaux;
} __attribute__ ((packed)) b_xcoff_syment;
#endif /* BACKTRACE_XCOFF_SIZE != 32 */
#define SYMESZ 18
#define C_EXT 2 /* External symbol. */
#define C_FCN 101 /* Beginning or end of function. */
#define C_FILE 103 /* Source file name. */
#define C_HIDEXT 107 /* Unnamed external symbol. */
#define C_BINCL 108 /* Beginning of include file. */
#define C_EINCL 109 /* End of include file. */
#define C_WEAKEXT 111 /* Weak external symbol. */
#define ISFCN(x) ((x) & 0x0020)
/* XCOFF AUX entry. */
#define AUXESZ 18
#define FILNMLEN 14
typedef union {
#if BACKTRACE_XCOFF_SIZE == 32
struct {
uint16_t pad;
uint16_t x_lnnohi;
uint16_t x_lnno;
} x_block;
#else
struct {
uint32_t x_lnno;
} x_block;
#endif
union {
char x_fname[FILNMLEN];
struct {
uint32_t x_zeroes;
uint32_t x_offset;
char pad[FILNMLEN-8];
uint8_t x_ftype;
} _x;
} x_file;
#if BACKTRACE_XCOFF_SIZE == 32
struct {
uint32_t x_exptr;
uint32_t x_fsize;
uint32_t x_lnnoptr;
uint32_t x_endndx;
} x_fcn;
#else
struct {
uint64_t x_lnnoptr;
uint32_t x_fsize;
uint32_t x_endndx;
} x_fcn;
#endif
struct {
uint8_t pad[AUXESZ-1];
uint8_t x_auxtype;
} x_auxtype;
} __attribute__ ((packed)) b_xcoff_auxent;
/* XCOFF line number entry. */
#if BACKTRACE_XCOFF_SIZE == 32
typedef struct {
union {
uint32_t l_symndx;
uint32_t l_paddr;
} l_addr;
uint16_t l_lnno;
} b_xcoff_lineno;
#define LINESZ 6
#else /* BACKTRACE_XCOFF_SIZE != 32 */
typedef struct {
union {
uint32_t l_symndx;
uint64_t l_paddr;
} l_addr;
uint32_t l_lnno;
} b_xcoff_lineno;
#define LINESZ 12
#endif /* BACKTRACE_XCOFF_SIZE != 32 */
#if BACKTRACE_XCOFF_SIZE == 32
#define XCOFF_AIX_TEXTBASE 0x10000000u
#else
#define XCOFF_AIX_TEXTBASE 0x100000000ul
#endif
/* AIX big archive fixed-length header. */
#define AIAMAGBIG "<bigaf>\n"
typedef struct {
char fl_magic[8]; /* Archive magic string. */
char fl_memoff[20]; /* Offset to member table. */
char fl_gstoff[20]; /* Offset to global symbol table. */
char fl_gst64off[20]; /* Offset to global symbol table for 64-bit objects. */
char fl_fstmoff[20]; /* Offset to first archive member. */
char fl_freeoff[20]; /* Offset to first member on free list. */
} b_ar_fl_hdr;
/* AIX big archive file member header. */
typedef struct {
char ar_size[20]; /* File member size - decimal. */
char ar_nxtmem[20]; /* Next member offset - decimal. */
char ar_prvmem[20]; /* Previous member offset - decimal. */
char ar_date[12]; /* File member date - decimal. */
char ar_uid[12]; /* File member userid - decimal. */
char ar_gid[12]; /* File member group id - decimal. */
char ar_mode[12]; /* File member mode - octal. */
char ar_namlen[4]; /* File member name length - decimal. */
char ar_name[2]; /* Start of member name. */
} b_ar_hdr;
/* Information we keep for an XCOFF symbol. */
struct xcoff_symbol
{
/* The name of the symbol. */
const char *name;
/* The address of the symbol. */
uintptr_t address;
/* The size of the symbol. */
size_t size;
};
/* Information to pass to xcoff_syminfo. */
struct xcoff_syminfo_data
{
/* Symbols for the next module. */
struct xcoff_syminfo_data *next;
/* The XCOFF symbols, sorted by address. */
struct xcoff_symbol *symbols;
/* The number of symbols. */
size_t count;
};
/* Information about an include file. */
struct xcoff_incl
{
/* File name. */
const char *filename;
/* Offset to first line number from the include file. */
uintptr_t begin;
/* Offset to last line number from the include file. */
uintptr_t end;
};
/* A growable vector of include files information. */
struct xcoff_incl_vector
{
/* Memory. This is an array of struct xcoff_incl. */
struct backtrace_vector vec;
/* Number of include files. */
size_t count;
};
/* A growable vector of functions information. */
struct xcoff_func
{
/* PC. */
uintptr_t pc;
/* The size of the function. */
size_t size;
/* Function name. */
const char *name;
/* File name. */
const char *filename;
/* Pointer to first lnno entry. */
uintptr_t lnnoptr;
/* Base address of containing section. */
uintptr_t sect_base;
/* Starting source line number. */
int lnno;
};
/* A growable vector of function information. This is used while
reading the function symbols. */
struct xcoff_func_vector
{
/* Memory. This is an array of struct xcoff_func. */
struct backtrace_vector vec;
/* Number of valid mappings. */
size_t count;
};
/* The information we need to map a PC to a file and line. */
struct xcoff_fileline_data
{
/* The data for the next file we know about. */
struct xcoff_fileline_data *next;
/* Functions information. */
struct xcoff_func_vector func_vec;
/* Include files information. */
struct xcoff_incl_vector incl_vec;
/* Line numbers information. */
const unsigned char *linenos;
size_t linenos_size;
uint64_t lnnoptr0;
/* Loader address. */
struct libbacktrace_base_address base_address;
};
/* Information we gather for the DWARF sections we care about. */
struct dwsect_info
{
/* Section file offset. */
off_t offset;
/* Section size. */
size_t size;
/* Section contents, after read from file. */
const unsigned char *data;
};
/* A dummy callback function used when we can't find any debug info. */
static int
xcoff_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED,
uintptr_t pc ATTRIBUTE_UNUSED,
backtrace_full_callback callback ATTRIBUTE_UNUSED,
backtrace_error_callback error_callback, void *data)
{
error_callback (data, "no debug info in XCOFF executable", -1);
return 0;
}
/* A dummy callback function used when we can't find a symbol
table. */
static void
xcoff_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED,
uintptr_t addr ATTRIBUTE_UNUSED,
backtrace_syminfo_callback callback ATTRIBUTE_UNUSED,
backtrace_error_callback error_callback, void *data)
{
error_callback (data, "no symbol table in XCOFF executable", -1);
}
/* Compare struct xcoff_symbol for qsort. */
static int
xcoff_symbol_compare (const void *v1, const void *v2)
{
const struct xcoff_symbol *e1 = (const struct xcoff_symbol *) v1;
const struct xcoff_symbol *e2 = (const struct xcoff_symbol *) v2;
if (e1->address < e2->address)
return -1;
else if (e1->address > e2->address)
return 1;
else
return 0;
}
/* Compare an ADDR against an xcoff_symbol for bsearch. */
static int
xcoff_symbol_search (const void *vkey, const void *ventry)
{
const uintptr_t *key = (const uintptr_t *) vkey;
const struct xcoff_symbol *entry = (const struct xcoff_symbol *) ventry;
uintptr_t addr;
addr = *key;
if (addr < entry->address)
return -1;
else if ((entry->size == 0 && addr > entry->address)
|| (entry->size > 0 && addr >= entry->address + entry->size))
return 1;
else
return 0;
}
/* Add XDATA to the list in STATE. */
static void
xcoff_add_syminfo_data (struct backtrace_state *state,
struct xcoff_syminfo_data *xdata)
{
if (!state->threaded)
{
struct xcoff_syminfo_data **pp;
for (pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
*pp != NULL;
pp = &(*pp)->next)
;
*pp = xdata;
}
else
{
while (1)
{
struct xcoff_syminfo_data **pp;
pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
while (1)
{
struct xcoff_syminfo_data *p;
p = backtrace_atomic_load_pointer (pp);
if (p == NULL)
break;
pp = &p->next;
}
if (__sync_bool_compare_and_swap (pp, NULL, xdata))
break;
}
}
}
/* Return the symbol name and value for an ADDR. */
static void
xcoff_syminfo (struct backtrace_state *state ATTRIBUTE_UNUSED, uintptr_t addr,
backtrace_syminfo_callback callback,
backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
void *data)
{
struct xcoff_syminfo_data *edata;
struct xcoff_symbol *sym = NULL;
const char *name;
if (!state->threaded)
{
for (edata = (struct xcoff_syminfo_data *) state->syminfo_data;
edata != NULL;
edata = edata->next)
{
sym = ((struct xcoff_symbol *)
bsearch (&addr, edata->symbols, edata->count,
sizeof (struct xcoff_symbol), xcoff_symbol_search));
if (sym != NULL)
break;
}
}
else
{
struct xcoff_syminfo_data **pp;
pp = (struct xcoff_syminfo_data **) (void *) &state->syminfo_data;
while (1)
{
edata = backtrace_atomic_load_pointer (pp);
if (edata == NULL)
break;
sym = ((struct xcoff_symbol *)
bsearch (&addr, edata->symbols, edata->count,
sizeof (struct xcoff_symbol), xcoff_symbol_search));
if (sym != NULL)
break;
pp = &edata->next;
}
}
if (sym == NULL)
callback (data, addr, NULL, 0, 0);
else
{
name = sym->name;
/* AIX prepends a '.' to function entry points, remove it. */
if (name && *name == '.')
++name;
callback (data, addr, name, sym->address, sym->size);
}
}
/* Return the name of an XCOFF symbol. */
static const char *
xcoff_symname (const b_xcoff_syment *asym,
const unsigned char *strtab, size_t strtab_size)
{
#if BACKTRACE_XCOFF_SIZE == 32
if (asym->n_zeroes != 0)
{
/* Make a copy as we will release the symtab view. */
char name[SYMNMLEN+1];
strncpy (name, asym->n_name, SYMNMLEN);
name[SYMNMLEN] = '\0';
return strdup (name);
}
#endif
if (asym->n_sclass & 0x80)
return NULL; /* .debug */
if (asym->n_offset_ >= strtab_size)
return NULL;
return (const char *) strtab + asym->n_offset_;
}
/* Initialize the symbol table info for xcoff_syminfo. */
static int
xcoff_initialize_syminfo (struct backtrace_state *state,
struct libbacktrace_base_address base_address,
const b_xcoff_syment *syms, size_t nsyms,
const unsigned char *strtab, size_t strtab_size,
backtrace_error_callback error_callback, void *data,
struct xcoff_syminfo_data *sdata)
{
size_t xcoff_symbol_count;
size_t xcoff_symbol_size;
struct xcoff_symbol *xcoff_symbols;
size_t i;
unsigned int j;
/* We only care about function symbols. Count them. */
xcoff_symbol_count = 0;
for (i = 0; i < nsyms; ++i)
{
const b_xcoff_syment *asym = &syms[i];
if ((asym->n_sclass == C_EXT || asym->n_sclass == C_HIDEXT
|| asym->n_sclass == C_WEAKEXT)
&& ISFCN (asym->n_type) && asym->n_numaux > 0 && asym->n_scnum > 0)
++xcoff_symbol_count;
i += asym->n_numaux;
}
xcoff_symbol_size = xcoff_symbol_count * sizeof (struct xcoff_symbol);
xcoff_symbols = ((struct xcoff_symbol *)
backtrace_alloc (state, xcoff_symbol_size, error_callback,
data));
if (xcoff_symbols == NULL)
return 0;
j = 0;
for (i = 0; i < nsyms; ++i)
{
const b_xcoff_syment *asym = &syms[i];
if ((asym->n_sclass == C_EXT || asym->n_sclass == C_HIDEXT
|| asym->n_sclass == C_WEAKEXT)
&& ISFCN (asym->n_type) && asym->n_numaux > 0 && asym->n_scnum > 0)
{
const b_xcoff_auxent *aux = (const b_xcoff_auxent *) (asym + 1);
xcoff_symbols[j].name = xcoff_symname (asym, strtab, strtab_size);
xcoff_symbols[j].address =
libbacktrace_add_base (asym->n_value, base_address);
/* x_fsize will be 0 if there is no debug information. */
xcoff_symbols[j].size = aux->x_fcn.x_fsize;
++j;
}
i += asym->n_numaux;
}
backtrace_qsort (xcoff_symbols, xcoff_symbol_count,
sizeof (struct xcoff_symbol), xcoff_symbol_compare);
sdata->next = NULL;
sdata->symbols = xcoff_symbols;
sdata->count = xcoff_symbol_count;
return 1;
}
/* Compare struct xcoff_func for qsort. */
static int
xcoff_func_compare (const void *v1, const void *v2)
{
const struct xcoff_func *fn1 = (const struct xcoff_func *) v1;
const struct xcoff_func *fn2 = (const struct xcoff_func *) v2;
if (fn1->pc < fn2->pc)
return -1;
else if (fn1->pc > fn2->pc)
return 1;
else
return 0;
}
/* Compare a PC against an xcoff_func for bsearch. */
static int
xcoff_func_search (const void *vkey, const void *ventry)
{
const uintptr_t *key = (const uintptr_t *) vkey;
const struct xcoff_func *entry = (const struct xcoff_func *) ventry;
uintptr_t pc;
pc = *key;
if (pc < entry->pc)
return -1;
else if ((entry->size == 0 && pc > entry->pc)
|| (entry->size > 0 && pc >= entry->pc + entry->size))
return 1;
else
return 0;
}
/* Compare struct xcoff_incl for qsort. */
static int
xcoff_incl_compare (const void *v1, const void *v2)
{
const struct xcoff_incl *in1 = (const struct xcoff_incl *) v1;
const struct xcoff_incl *in2 = (const struct xcoff_incl *) v2;
if (in1->begin < in2->begin)
return -1;
else if (in1->begin > in2->begin)
return 1;
else
return 0;
}
/* Find a lnnoptr in an include file. */
static int
xcoff_incl_search (const void *vkey, const void *ventry)
{
const uintptr_t *key = (const uintptr_t *) vkey;
const struct xcoff_incl *entry = (const struct xcoff_incl *) ventry;
uintptr_t lnno;
lnno = *key;
if (lnno < entry->begin)
return -1;
else if (lnno > entry->end)
return 1;
else
return 0;
}
/* Look for a PC in the function vector for one module. On success,
call CALLBACK and return whatever it returns. On error, call
ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
0 if not. */
static int
xcoff_lookup_pc (struct backtrace_state *state ATTRIBUTE_UNUSED,
struct xcoff_fileline_data *fdata, uintptr_t pc,
backtrace_full_callback callback,
backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
void *data, int *found)
{
const struct xcoff_incl *incl, *bincl;
const struct xcoff_func *fn;
const b_xcoff_lineno *lineno;
const unsigned char *lineptr;
const char *function;
const char *filename;
uintptr_t lnnoptr, match;
uint32_t lnno = 0;
*found = 1;
if ((pc & 3) != 0)
++pc;
/* Find the function first. */
fn = ((struct xcoff_func *)
bsearch (&pc, fdata->func_vec.vec.base, fdata->func_vec.count,
sizeof (struct xcoff_func), xcoff_func_search));
if (fn == NULL)
{
*found = 0;
return 0;
}
filename = fn->filename;
/* Find the line number next. */
/* Skip first entry that points to symtab. */
lnnoptr = fn->lnnoptr + LINESZ;
match = lnnoptr;
lineptr = fdata->linenos + (lnnoptr - fdata->lnnoptr0);
while (lineptr + LINESZ <= fdata->linenos + fdata->linenos_size)
{
lineno = (const b_xcoff_lineno *) lineptr;
if (lineno->l_lnno == 0)
break;
if (pc <= libbacktrace_add_base (lineno->l_addr.l_paddr,
fdata->base_address))
break;
match = lnnoptr;
lnno = lineno->l_lnno;
lnnoptr += LINESZ;
lineptr += LINESZ;
}
/* If part of a function other than the beginning comes from an
include file, the line numbers are absolute, rather than
relative to the beginning of the function. */
incl = ((struct xcoff_incl *)
bsearch (&match, fdata->incl_vec.vec.base,
fdata->incl_vec.count, sizeof (struct xcoff_incl),
xcoff_incl_search));
if (incl != NULL)
{
bincl = ((struct xcoff_incl *)
bsearch (&fn->lnnoptr, fdata->incl_vec.vec.base,
fdata->incl_vec.count, sizeof (struct xcoff_incl),
xcoff_incl_search));
if (bincl != NULL && strcmp (incl->filename, bincl->filename) == 0)
{
lnno += fn->lnno - 1;
}
filename = incl->filename;
}
else
{
lnno += fn->lnno - 1;
}
function = fn->name;
/* AIX prepends a '.' to function entry points, remove it. */
if (function != NULL && *function == '.')
++function;
return callback (data, pc, filename, lnno, function);
}
/* Return the file/line information for a PC using the XCOFF lineno
mapping we built earlier. */
static int
xcoff_fileline (struct backtrace_state *state, uintptr_t pc,
backtrace_full_callback callback,
backtrace_error_callback error_callback, void *data)
{
struct xcoff_fileline_data *fdata;
int found;
int ret;
if (!state->threaded)
{
for (fdata = (struct xcoff_fileline_data *) state->fileline_data;
fdata != NULL;
fdata = fdata->next)
{
ret = xcoff_lookup_pc (state, fdata, pc, callback, error_callback,
data, &found);
if (ret != 0 || found)
return ret;
}
}
else
{
struct xcoff_fileline_data **pp;
pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
while (1)
{
fdata = backtrace_atomic_load_pointer (pp);
if (fdata == NULL)
break;
ret = xcoff_lookup_pc (state, fdata, pc, callback, error_callback,
data, &found);
if (ret != 0 || found)
return ret;
pp = &fdata->next;
}
}
/* FIXME: See if any libraries have been dlopen'ed. */
return callback (data, pc, NULL, 0, NULL);
}
/* Initialize the function vector info for xcoff_fileline. */
static int
xcoff_initialize_fileline (struct backtrace_state *state,
struct libbacktrace_base_address base_address,
const b_xcoff_scnhdr *sects,
const b_xcoff_syment *syms, size_t nsyms,
const unsigned char *strtab, size_t strtab_size,
const unsigned char *linenos, size_t linenos_size,
uint64_t lnnoptr0,
backtrace_error_callback error_callback, void *data)
{
struct xcoff_fileline_data *fdata;
struct xcoff_func *fn;
const b_xcoff_syment *fsym;
const b_xcoff_auxent *aux;
const char *filename;
const char *name;
struct xcoff_incl *incl;
uintptr_t begin, end;
uintptr_t lnno, lnnoptr;
uint32_t fsize;
size_t i;
fdata = ((struct xcoff_fileline_data *)
backtrace_alloc (state, sizeof (struct xcoff_fileline_data),
error_callback, data));
if (fdata == NULL)
return 0;
memset (fdata, 0, sizeof *fdata);
fdata->base_address = base_address;
fdata->linenos = linenos;
fdata->linenos_size = linenos_size;
fdata->lnnoptr0 = lnnoptr0;
begin = 0;
filename = NULL;
fsym = NULL;
lnnoptr = 0;
fsize = 0;
for (i = 0; i < nsyms; ++i)
{
const b_xcoff_syment *asym = &syms[i];
switch (asym->n_sclass)
{
case C_BINCL:
begin = asym->n_value;
break;
case C_EINCL:
if (begin == 0)
break;
end = asym->n_value;
incl = ((struct xcoff_incl *)
backtrace_vector_grow (state, sizeof (struct xcoff_incl),
error_callback, data,
&fdata->incl_vec.vec));
if (incl != NULL)
{
incl->filename = xcoff_symname (asym, strtab, strtab_size);
incl->begin = begin;
incl->end = end;
++fdata->incl_vec.count;
}
begin = 0;
break;
case C_FILE:
filename = xcoff_symname (asym, strtab, strtab_size);
if (filename == NULL)
break;
/* If the file auxiliary entry is not used, the symbol name is
the name of the source file. If the file auxiliary entry is
used, then the symbol name should be .file, and the first
file auxiliary entry (by convention) contains the source
file name. */
if (asym->n_numaux > 0 && strcmp (filename, ".file") == 0)
{
aux = (const b_xcoff_auxent *) (asym + 1);
if (aux->x_file._x.x_zeroes != 0)
{
/* Make a copy as we will release the symtab view. */
char name[FILNMLEN+1];
strncpy (name, aux->x_file.x_fname, FILNMLEN);
name[FILNMLEN] = '\0';
filename = strdup (name);
}
else if (aux->x_file._x.x_offset < strtab_size)
filename = (const char *) strtab + aux->x_file._x.x_offset;
else
filename = NULL;
}
break;
case C_EXT:
case C_HIDEXT:
case C_WEAKEXT:
fsym = NULL;
lnnoptr = 0;
fsize = 0;
if (!ISFCN (asym->n_type) || asym->n_numaux == 0
|| asym->n_scnum <= 0)
break;
if (filename == NULL)
break;
aux = (const b_xcoff_auxent *) (asym + 1);
lnnoptr = aux->x_fcn.x_lnnoptr;
if (lnnoptr < lnnoptr0
|| lnnoptr + LINESZ > lnnoptr0 + linenos_size)
break;
/* x_fsize will be 0 if there is no debug information. */
fsize = aux->x_fcn.x_fsize;
fsym = asym;
break;
case C_FCN:
if (asym->n_numaux == 0)
break;
if (fsym == NULL)
break;
name = xcoff_symname (asym, strtab, strtab_size);
if (name == NULL || strcmp (name, ".bf") != 0)
{
fsym = NULL;
break;
}
aux = (const b_xcoff_auxent *) (asym + 1);
#if BACKTRACE_XCOFF_SIZE == 32
lnno = (uint32_t) aux->x_block.x_lnnohi << 16
| aux->x_block.x_lnno;
#else
lnno = aux->x_block.x_lnno;
#endif
fn = ((struct xcoff_func *)
backtrace_vector_grow (state, sizeof (struct xcoff_func),
error_callback, data,
&fdata->func_vec.vec));