aboutsummaryrefslogtreecommitdiff
path: root/libbacktrace/xcoff.c
blob: e22f15dd5cecaa328c53f25efdb3ad39d6b18dd1 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
/* 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.  */
  uintptr_t 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,
			  uintptr_t 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 = base_address + asym->n_value;
	  /* 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 <= fdata->base_address + lineno->l_addr.l_paddr)
	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,
			   uintptr_t 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));
	    if (fn == NULL)
	      break;
	    fn->name = xcoff_symname (fsym, strtab, strtab_size);
	    fn->filename = filename;
	    fn->sect_base = sects[fsym->n_scnum - 1].s_paddr;
	    fn->pc = base_address + fsym->n_value;
	    fn->size = fsize;
	    fn->lnno = lnno;
	    fn->lnnoptr = lnnoptr;
	    ++fdata->func_vec.count;
	    break;
	}

      i += asym->n_numaux;
    }

  if (!backtrace_vector_release (state, &fdata->func_vec.vec, error_callback,
				 data))
    goto fail;
  backtrace_qsort (fdata->func_vec.vec.base, fdata->func_vec.count,
		   sizeof (struct xcoff_func), xcoff_func_compare);

  if (!backtrace_vector_release (state, &fdata->incl_vec.vec, error_callback,
				 data))
    goto fail;
  backtrace_qsort (fdata->incl_vec.vec.base, fdata->incl_vec.count,
		   sizeof (struct xcoff_incl), xcoff_incl_compare);

  if (!state->threaded)
    {
      struct xcoff_fileline_data **pp;

      for (pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;
	   *pp != NULL;
	   pp = &(*pp)->next)
	;
      *pp = fdata;
    }
  else
    {
      while (1)
	{
	  struct xcoff_fileline_data **pp;

	  pp = (struct xcoff_fileline_data **) (void *) &state->fileline_data;

	  while (1)
	    {
	      struct xcoff_fileline_data *p;

	      p = backtrace_atomic_load_pointer (pp);

	      if (p == NULL)
		break;

	      pp = &p->next;
	    }

	  if (__sync_bool_compare_and_swap (pp, NULL, fdata))
	    break;
	}
    }

  return 1;

fail:
  return 0;
}

/* Add the backtrace data for one XCOFF file.  Returns 1 on success,
   0 on failure (in both cases descriptor is closed).  */

static int
xcoff_add (struct backtrace_state *state, int descriptor, off_t offset,
	   uintptr_t base_address, backtrace_error_callback error_callback,
	   void *data, fileline *fileline_fn, int *found_sym, int exe)
{
  struct backtrace_view fhdr_view;
  struct backtrace_view sects_view;
  struct backtrace_view linenos_view;
  struct backtrace_view syms_view;
  struct backtrace_view str_view;
  struct backtrace_view dwarf_view;
  b_xcoff_filhdr fhdr;
  const b_xcoff_scnhdr *sects;
  const b_xcoff_scnhdr *stext;
  uint64_t lnnoptr;
  uint32_t nlnno;
  off_t str_off;
  off_t min_offset;
  off_t max_offset;
  struct dwsect_info dwsect[DEBUG_MAX];
  size_t sects_size;
  size_t syms_size;
  int32_t str_size;
  int sects_view_valid;
  int linenos_view_valid;
  int syms_view_valid;
  int str_view_valid;
  int dwarf_view_valid;
  int magic_ok;
  int i;
  struct dwarf_sections dwarf_sections;

  *found_sym = 0;

  sects_view_valid = 0;
  linenos_view_valid = 0;
  syms_view_valid = 0;
  str_view_valid = 0;
  dwarf_view_valid = 0;

  str_size = 0;

  /* Map the XCOFF file header.  */
  if (!backtrace_get_view (state, descriptor, offset, sizeof (b_xcoff_filhdr),
			   error_callback, data, &fhdr_view))
    goto fail;

  memcpy (&fhdr, fhdr_view.data, sizeof fhdr);
  magic_ok = (fhdr.f_magic == XCOFF_MAGIC);

  backtrace_release_view (state, &fhdr_view, error_callback, data);

  if (!magic_ok)
    {
      if (exe)
	error_callback (data, "executable file is not XCOFF", 0);
      goto fail;
    }

  /* Verify object is of expected type.  */
  if ((exe && (fhdr.f_flags & F_SHROBJ))
      || (!exe && !(fhdr.f_flags & F_SHROBJ)))
    goto fail;

  /* Read the section headers.  */

  sects_size = fhdr.f_nscns * sizeof (b_xcoff_scnhdr);

  if (!backtrace_get_view (state, descriptor,
			   offset + sizeof (fhdr) + fhdr.f_opthdr,
			   sects_size, error_callback, data, &sects_view))
    goto fail;
  sects_view_valid = 1;
  sects = (const b_xcoff_scnhdr *) sects_view.data;

  /* FIXME: assumes only one .text section.  */
  for (i = 0; i < fhdr.f_nscns; ++i)
    if ((sects[i].s_flags & 0xffff) == STYP_TEXT)
      break;
  if (i == fhdr.f_nscns)
    goto fail;

  stext = &sects[i];

  /* base_address represents the difference between the
     virtual memory address of the shared object or a loaded
     executable and the offset of that object in the file
     from which it was loaded.
     On AIX, virtual address is either fixed for executable
     or given by ldinfo.  This address will include the XCOFF
     headers.  */
  base_address = ((exe ? XCOFF_AIX_TEXTBASE : base_address)
		  + stext->s_scnptr
		  - stext->s_paddr);

  lnnoptr = stext->s_lnnoptr;
  nlnno = stext->s_nlnno;

#if BACKTRACE_XCOFF_SIZE == 32
  if (nlnno == _OVERFLOW_MARKER)
    {
      int sntext = i + 1;
      /* Find the matching .ovrflo section.  */
      for (i = 0; i < fhdr.f_nscns; ++i)
	{
	  if (((sects[i].s_flags & 0xffff) == STYP_OVRFLO)
	      && sects[i].s_nlnno == sntext)
	    {
	      nlnno = sects[i].s_vaddr;
	      break;
	    }
	}
    }
#endif

  /* Read the symbol table and the string table.  */

  if (fhdr.f_symptr != 0)
    {
      struct xcoff_syminfo_data *sdata;

      /* Symbol table is followed by the string table.  The string table
	 starts with its length (on 4 bytes).
	 Map the symbol table and the length of the string table.  */
      syms_size = fhdr.f_nsyms * sizeof (b_xcoff_syment);

      if (!backtrace_get_view (state, descriptor, offset + fhdr.f_symptr,
			       syms_size + 4, error_callback, data,
			       &syms_view))
	goto fail;
      syms_view_valid = 1;

      memcpy (&str_size, syms_view.data + syms_size, 4);

      str_off = fhdr.f_symptr + syms_size;

      if (str_size > 4)
	{
	  /* Map string table (including the length word).  */

	  if (!backtrace_get_view (state, descriptor, offset + str_off,
				   str_size, error_callback, data, &str_view))
	    goto fail;
	  str_view_valid = 1;
	}

      sdata = ((struct xcoff_syminfo_data *)
	       backtrace_alloc (state, sizeof *sdata, error_callback, data));
      if (sdata == NULL)
	goto fail;

      if (!xcoff_initialize_syminfo (state, base_address,
				     syms_view.data, fhdr.f_nsyms,
				     str_view.data, str_size,
				     error_callback, data, sdata))
	{
	  backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
	  goto fail;
	}

      *found_sym = 1;

      xcoff_add_syminfo_data (state, sdata);
    }

  /* Read all the DWARF sections in a single view, since they are
     probably adjacent in the file.  We never release this view.  */

  min_offset = 0;
  max_offset = 0;
  memset (dwsect, 0, sizeof dwsect);
  for (i = 0; i < fhdr.f_nscns; ++i)
    {
      off_t end;
      int idx;

      if ((sects[i].s_flags & 0xffff) != STYP_DWARF
	  || sects[i].s_size == 0)
	continue;
      /* Map DWARF section to array index.  */
      switch (sects[i].s_flags & 0xffff0000)
	{
	  case SSUBTYP_DWINFO:
	    idx = DEBUG_INFO;
	    break;
	  case SSUBTYP_DWLINE:
	    idx = DEBUG_LINE;
	    break;
	  case SSUBTYP_DWABREV:
	    idx = DEBUG_ABBREV;
	    break;
	  case SSUBTYP_DWRNGES:
	    idx = DEBUG_RANGES;
	    break;
	  case SSUBTYP_DWSTR:
	    idx = DEBUG_STR;
	    break;
	  default:
	    continue;
	}
      if (min_offset == 0 || (off_t) sects[i].s_scnptr < min_offset)
	min_offset = sects[i].s_scnptr;
      end = sects[i].s_scnptr + sects[i].s_size;
      if (end > max_offset)
	max_offset = end;
      dwsect[idx].offset = sects[i].s_scnptr;
      dwsect[idx].size = sects[i].s_size;
    }
  if (min_offset != 0 && max_offset != 0)
    {
      if (!backtrace_get_view (state, descriptor, offset + min_offset,
			       max_offset - min_offset,
			       error_callback, data, &dwarf_view))
	goto fail;
      dwarf_view_valid = 1;

      for (i = 0; i < (int) DEBUG_MAX; ++i)
	{
	  if (dwsect[i].offset == 0)
	    dwsect[i].data = NULL;
	  else
	    dwsect[i].data = ((const unsigned char *) dwarf_view.data
			      + (dwsect[i].offset - min_offset));
	}

      memset (&dwarf_sections, 0, sizeof dwarf_sections);

      dwarf_sections.data[DEBUG_INFO] = dwsect[DEBUG_INFO].data;
      dwarf_sections.size[DEBUG_INFO] = dwsect[DEBUG_INFO].size;
      dwarf_sections.data[DEBUG_LINE] = dwsect[DEBUG_LINE].data;
      dwarf_sections.size[DEBUG_LINE] = dwsect[DEBUG_LINE].size;
      dwarf_sections.data[DEBUG_ABBREV] = dwsect[DEBUG_ABBREV].data;
      dwarf_sections.size[DEBUG_ABBREV] = dwsect[DEBUG_ABBREV].size;
      dwarf_sections.data[DEBUG_RANGES] = dwsect[DEBUG_RANGES].data;
      dwarf_sections.size[DEBUG_RANGES] = dwsect[DEBUG_RANGES].size;
      dwarf_sections.data[DEBUG_STR] = dwsect[DEBUG_STR].data;
      dwarf_sections.size[DEBUG_STR] = dwsect[DEBUG_STR].size;

      if (!backtrace_dwarf_add (state, base_address, &dwarf_sections,
				1, /* big endian */
				NULL, /* altlink */
				error_callback, data, fileline_fn,
				NULL /* returned fileline_entry */))
	goto fail;
    }

  /* Read the XCOFF line number entries if DWARF sections not found.  */

  if (!dwarf_view_valid && fhdr.f_symptr != 0 && lnnoptr != 0)
    {
      size_t linenos_size = (size_t) nlnno * LINESZ;

      /* We never release this view.  */
      if (!backtrace_get_view (state, descriptor, offset + lnnoptr,
			       linenos_size,
			       error_callback, data, &linenos_view))
	goto fail;
      linenos_view_valid = 1;

      if (xcoff_initialize_fileline (state, base_address, sects,
				     syms_view.data, fhdr.f_nsyms,
				     str_view.data, str_size,
				     linenos_view.data, linenos_size,
				     lnnoptr, error_callback, data))
	*fileline_fn = xcoff_fileline;
    }

  backtrace_release_view (state, &sects_view, error_callback, data);
  sects_view_valid = 0;
  if (syms_view_valid)
    backtrace_release_view (state, &syms_view, error_callback, data);
  syms_view_valid = 0;

  /* We've read all we need from the executable.  */
  if (!backtrace_close (descriptor, error_callback, data))
    goto fail;
  descriptor = -1;

  return 1;

 fail:
  if (sects_view_valid)
    backtrace_release_view (state, &sects_view, error_callback, data);
  if (str_view_valid)
    backtrace_release_view (state, &str_view, error_callback, data);
  if (syms_view_valid)
    backtrace_release_view (state, &syms_view, error_callback, data);
  if (linenos_view_valid)
    backtrace_release_view (state, &linenos_view, error_callback, data);
  if (dwarf_view_valid)
    backtrace_release_view (state, &dwarf_view, error_callback, data);
  if (descriptor != -1 && offset == 0)
    backtrace_close (descriptor, error_callback, data);
  return 0;
}

#ifdef HAVE_LOADQUERY

/* Read an integer value in human-readable format from an AIX
   big archive fixed-length or member header.  */

static int
xcoff_parse_decimal (const char *buf, size_t size, off_t *off)
{
  char str[32];
  char *end;

  if (size >= sizeof str)
    return 0;
  memcpy (str, buf, size);
  str[size] = '\0';
  *off = strtol (str, &end, 10);
  if (*end != '\0' && *end != ' ')
    return 0;

  return 1;
}

/* Add the backtrace data for a member of an AIX big archive.
   Returns 1 on success, 0 on failure.  */

static int
xcoff_armem_add (struct backtrace_state *state, int descriptor,
		 uintptr_t base_address, const char *member,
		 backtrace_error_callback error_callback, void *data,
		 fileline *fileline_fn, int *found_sym)
{
  struct backtrace_view view;
  b_ar_fl_hdr fl_hdr;
  const b_ar_hdr *ar_hdr;
  off_t off;
  off_t len;
  int memlen;

  *found_sym = 0;

  /* Map archive fixed-length header.  */

  if (!backtrace_get_view (state, descriptor, 0, sizeof (b_ar_fl_hdr),
			   error_callback, data, &view))
    goto fail;

  memcpy (&fl_hdr, view.data, sizeof (b_ar_fl_hdr));

  backtrace_release_view (state, &view, error_callback, data);

  if (memcmp (fl_hdr.fl_magic, AIAMAGBIG, 8) != 0)
    goto fail;

  memlen = strlen (member);

  /* Read offset of first archive member.  */
  if (!xcoff_parse_decimal (fl_hdr.fl_fstmoff, sizeof fl_hdr.fl_fstmoff, &off))
    goto fail;
  while (off != 0)
    {
      /* Map archive member header and member name.  */

      if (!backtrace_get_view (state, descriptor, off,
			       sizeof (b_ar_hdr) + memlen,
			       error_callback, data, &view))
	break;

      ar_hdr = (const b_ar_hdr *) view.data;

      /* Read archive member name length.  */
      if (!xcoff_parse_decimal (ar_hdr->ar_namlen, sizeof ar_hdr->ar_namlen,
				&len))
	{
	  backtrace_release_view (state, &view, error_callback, data);
	  break;
	}
      if (len == memlen && !memcmp (ar_hdr->ar_name, member, memlen))
	{
	  off = (off + sizeof (b_ar_hdr) + memlen + 1) & ~1;

	  /* The archive can contain several members with the same name
	     (e.g. 32-bit and 64-bit), so continue if not ok.  */

	  if (xcoff_add (state, descriptor, off, base_address, error_callback,
			 data, fileline_fn, found_sym, 0))
	    {
	      backtrace_release_view (state, &view, error_callback, data);
	      return 1;
	    }
	}

      /* Read offset of next archive member.  */
      if (!xcoff_parse_decimal (ar_hdr->ar_nxtmem, sizeof ar_hdr->ar_nxtmem,
				&off))
	{
	  backtrace_release_view (state, &view, error_callback, data);
	  break;
	}
      backtrace_release_view (state, &view, error_callback, data);
    }

 fail:
  /* No matching member found.  */
  backtrace_close (descriptor, error_callback, data);
  return 0;
}

/* Add the backtrace data for dynamically loaded libraries.  */

static void
xcoff_add_shared_libs (struct backtrace_state *state,
		       backtrace_error_callback error_callback,
		       void *data, fileline *fileline_fn, int *found_sym)
{
  const struct ld_info *ldinfo;
  void *buf;
  unsigned int buflen;
  const char *member;
  int descriptor;
  int does_not_exist;
  int lib_found_sym;
  int ret;

  /* Retrieve the list of loaded libraries.  */

  buf = NULL;
  buflen = 512;
  do
    {
      buf = realloc (buf, buflen);
      if (buf == NULL)
	{
	  ret = -1;
	  break;
	}
      ret = loadquery (L_GETINFO, buf, buflen);
      if (ret == 0)
	break;
      buflen *= 2;
    }
  while (ret == -1 && errno == ENOMEM);
  if (ret != 0)
    {
      free (buf);
      return;
    }

  ldinfo = (const struct ld_info *) buf;
  while ((const char *) ldinfo < (const char *) buf + buflen)
    {
      if (*ldinfo->ldinfo_filename != '/')
	goto next;

      descriptor = backtrace_open (ldinfo->ldinfo_filename, error_callback,
				   data, &does_not_exist);
      if (descriptor < 0)
	goto next;

      /* Check if it is an archive (member name not empty).  */

      member = ldinfo->ldinfo_filename + strlen (ldinfo->ldinfo_filename) + 1;
      if (*member)
	{
	  xcoff_armem_add (state, descriptor,
			   (uintptr_t) ldinfo->ldinfo_textorg, member,
			   error_callback, data, fileline_fn, &lib_found_sym);
	}
      else
	{
	  xcoff_add (state, descriptor, 0, (uintptr_t) ldinfo->ldinfo_textorg,
		     error_callback, data, fileline_fn, &lib_found_sym, 0);
	}
      if (lib_found_sym)
	*found_sym = 1;

 next:
      if (ldinfo->ldinfo_next == 0)
	break;
      ldinfo = (const struct ld_info *) ((const char *) ldinfo
					 + ldinfo->ldinfo_next);
    }

    free (buf);
}
#endif /* HAVE_LOADQUERY */

/* Initialize the backtrace data we need from an XCOFF executable.
   Returns 1 on success, 0 on failure.  */

int
backtrace_initialize (struct backtrace_state *state,
		      const char *filename ATTRIBUTE_UNUSED, int descriptor,
		      backtrace_error_callback error_callback,
		      void *data, fileline *fileline_fn)
{
  int ret;
  int found_sym;
  fileline xcoff_fileline_fn = xcoff_nodebug;

  ret = xcoff_add (state, descriptor, 0, 0, error_callback, data,
		   &xcoff_fileline_fn, &found_sym, 1);
  if (!ret)
    return 0;

#ifdef HAVE_LOADQUERY
  xcoff_add_shared_libs (state, error_callback, data, &xcoff_fileline_fn,
			 &found_sym);
#endif

  if (!state->threaded)
    {
      if (found_sym)
	state->syminfo_fn = xcoff_syminfo;
      else if (state->syminfo_fn == NULL)
	state->syminfo_fn = xcoff_nosyms;
    }
  else
    {
      if (found_sym)
	backtrace_atomic_store_pointer (&state->syminfo_fn, xcoff_syminfo);
      else
	(void) __sync_bool_compare_and_swap (&state->syminfo_fn, NULL,
					     xcoff_nosyms);
    }

  if (!state->threaded)
    {
      if (state->fileline_fn == NULL || state->fileline_fn == xcoff_nodebug)
	*fileline_fn = xcoff_fileline_fn;
    }
  else
    {
      fileline current_fn;

      current_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
      if (current_fn == NULL || current_fn == xcoff_nodebug)
	*fileline_fn = xcoff_fileline_fn;
    }

  return 1;
}