aboutsummaryrefslogtreecommitdiff
path: root/bfd/xsym.c
blob: 6a4a7509d88fbef6e33999cacbfad8cfd849250f (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
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
/* xSYM symbol-file support for BFD.
   Copyright (C) 1999-2017 Free Software Foundation, Inc.

   This file is part of BFD, the Binary File Descriptor library.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */

/* xSYM is the debugging format used by CodeWarrior on Mac OS classic.  */

#include "sysdep.h"
#include "alloca-conf.h"
#include "xsym.h"
#include "bfd.h"
#include "libbfd.h"

#define bfd_sym_close_and_cleanup                   _bfd_generic_close_and_cleanup
#define bfd_sym_bfd_free_cached_info                _bfd_generic_bfd_free_cached_info
#define bfd_sym_new_section_hook                    _bfd_generic_new_section_hook
#define bfd_sym_bfd_is_local_label_name             bfd_generic_is_local_label_name
#define bfd_sym_bfd_is_target_special_symbol       ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
#define bfd_sym_get_lineno                          _bfd_nosymbols_get_lineno
#define bfd_sym_find_nearest_line                   _bfd_nosymbols_find_nearest_line
#define bfd_sym_find_line                           _bfd_nosymbols_find_line
#define bfd_sym_find_inliner_info                   _bfd_nosymbols_find_inliner_info
#define bfd_sym_get_symbol_version_string	    _bfd_nosymbols_get_symbol_version_string
#define bfd_sym_bfd_make_debug_symbol               _bfd_nosymbols_bfd_make_debug_symbol
#define bfd_sym_read_minisymbols                    _bfd_generic_read_minisymbols
#define bfd_sym_minisymbol_to_symbol                _bfd_generic_minisymbol_to_symbol
#define bfd_sym_set_arch_mach                       _bfd_generic_set_arch_mach
#define bfd_sym_get_section_contents                _bfd_generic_get_section_contents
#define bfd_sym_set_section_contents                _bfd_generic_set_section_contents
#define bfd_sym_bfd_get_relocated_section_contents  bfd_generic_get_relocated_section_contents
#define bfd_sym_bfd_relax_section                   bfd_generic_relax_section
#define bfd_sym_bfd_gc_sections                     bfd_generic_gc_sections
#define bfd_sym_bfd_lookup_section_flags            bfd_generic_lookup_section_flags
#define bfd_sym_bfd_merge_sections                  bfd_generic_merge_sections
#define bfd_sym_bfd_is_group_section                bfd_generic_is_group_section
#define bfd_sym_bfd_discard_group                   bfd_generic_discard_group
#define bfd_sym_section_already_linked              _bfd_generic_section_already_linked
#define bfd_sym_bfd_define_common_symbol            bfd_generic_define_common_symbol
#define bfd_sym_bfd_link_hash_table_create          _bfd_generic_link_hash_table_create
#define bfd_sym_bfd_link_add_symbols                _bfd_generic_link_add_symbols
#define bfd_sym_bfd_link_just_syms                  _bfd_generic_link_just_syms
#define bfd_sym_bfd_copy_link_hash_symbol_type \
  _bfd_generic_copy_link_hash_symbol_type
#define bfd_sym_bfd_final_link                      _bfd_generic_final_link
#define bfd_sym_bfd_link_split_section              _bfd_generic_link_split_section
#define bfd_sym_get_section_contents_in_window      _bfd_generic_get_section_contents_in_window
#define bfd_sym_bfd_link_check_relocs               _bfd_generic_link_check_relocs

extern const bfd_target sym_vec;

static int
pstrcmp (const char *as, const char *bs)
{
  const unsigned char *a = (const unsigned char *) as;
  const unsigned char *b = (const unsigned char *) bs;
  unsigned char clen;
  int ret;

  clen = (a[0] > b[0]) ? b[0] : a[0];
  ret = memcmp (a + 1, b + 1, clen);
  if (ret != 0)
    return ret;

  if (a[0] == b[0])
    return 0;
  else if (a[0] < b[0])
    return -1;
  else
    return 1;
}

static unsigned long
compute_offset (unsigned long first_page,
		unsigned long page_size,
		unsigned long entry_size,
		unsigned long sym_index)
{
  unsigned long entries_per_page = page_size / entry_size;
  unsigned long page_number = first_page + (sym_index / entries_per_page);
  unsigned long page_offset = (sym_index % entries_per_page) * entry_size;

  return (page_number * page_size) + page_offset;
}

bfd_boolean
bfd_sym_mkobject (bfd *abfd ATTRIBUTE_UNUSED)
{
  return 1;
}

void
bfd_sym_print_symbol (bfd *abfd ATTRIBUTE_UNUSED,
		      void * afile ATTRIBUTE_UNUSED,
		      asymbol *symbol ATTRIBUTE_UNUSED,
		      bfd_print_symbol_type how ATTRIBUTE_UNUSED)
{
  return;
}

bfd_boolean
bfd_sym_valid (bfd *abfd)
{
  if (abfd == NULL || abfd->xvec == NULL)
    return 0;

  return abfd->xvec == &sym_vec;
}

unsigned char *
bfd_sym_read_name_table (bfd *abfd, bfd_sym_header_block *dshb)
{
  unsigned char *rstr;
  long ret;
  size_t table_size = dshb->dshb_nte.dti_page_count * dshb->dshb_page_size;
  size_t table_offset = dshb->dshb_nte.dti_first_page * dshb->dshb_page_size;

  rstr = bfd_alloc (abfd, table_size);
  if (rstr == NULL)
    return rstr;

  bfd_seek (abfd, table_offset, SEEK_SET);
  ret = bfd_bread (rstr, table_size, abfd);
  if (ret < 0 || (unsigned long) ret != table_size)
    {
      bfd_release (abfd, rstr);
      return NULL;
    }

  return rstr;
}

void
bfd_sym_parse_file_reference_v32 (unsigned char *buf,
				  size_t len,
				  bfd_sym_file_reference *entry)
{
  BFD_ASSERT (len == 6);

  entry->fref_frte_index = bfd_getb16 (buf);
  entry->fref_offset = bfd_getb32 (buf + 2);
}

void
bfd_sym_parse_disk_table_v32 (unsigned char *buf,
			      size_t len,
			      bfd_sym_table_info *table)
{
  BFD_ASSERT (len == 8);

  table->dti_first_page = bfd_getb16 (buf);
  table->dti_page_count = bfd_getb16 (buf + 2);
  table->dti_object_count = bfd_getb32 (buf + 4);
}

void
bfd_sym_parse_header_v32 (unsigned char *buf,
			  size_t len,
			  bfd_sym_header_block *header)
{
  BFD_ASSERT (len == 154);

  memcpy (header->dshb_id, buf, 32);
  header->dshb_page_size = bfd_getb16 (buf + 32);
  header->dshb_hash_page = bfd_getb16 (buf + 34);
  header->dshb_root_mte = bfd_getb16 (buf + 36);
  header->dshb_mod_date = bfd_getb32 (buf + 38);

  bfd_sym_parse_disk_table_v32 (buf + 42, 8, &header->dshb_frte);
  bfd_sym_parse_disk_table_v32 (buf + 50, 8, &header->dshb_rte);
  bfd_sym_parse_disk_table_v32 (buf + 58, 8, &header->dshb_mte);
  bfd_sym_parse_disk_table_v32 (buf + 66, 8, &header->dshb_cmte);
  bfd_sym_parse_disk_table_v32 (buf + 74, 8, &header->dshb_cvte);
  bfd_sym_parse_disk_table_v32 (buf + 82, 8, &header->dshb_csnte);
  bfd_sym_parse_disk_table_v32 (buf + 90, 8, &header->dshb_clte);
  bfd_sym_parse_disk_table_v32 (buf + 98, 8, &header->dshb_ctte);
  bfd_sym_parse_disk_table_v32 (buf + 106, 8, &header->dshb_tte);
  bfd_sym_parse_disk_table_v32 (buf + 114, 8, &header->dshb_nte);
  bfd_sym_parse_disk_table_v32 (buf + 122, 8, &header->dshb_tinfo);
  bfd_sym_parse_disk_table_v32 (buf + 130, 8, &header->dshb_fite);
  bfd_sym_parse_disk_table_v32 (buf + 138, 8, &header->dshb_const);

  memcpy (&header->dshb_file_creator, buf + 146, 4);
  memcpy (&header->dshb_file_type, buf + 150, 4);
}

int
bfd_sym_read_header_v32 (bfd *abfd, bfd_sym_header_block *header)
{
  unsigned char buf[154];
  long ret;

  ret = bfd_bread (buf, 154, abfd);
  if (ret != 154)
    return -1;

  bfd_sym_parse_header_v32 (buf, 154, header);

  return 0;
}

int
bfd_sym_read_header_v34 (bfd *abfd ATTRIBUTE_UNUSED,
			 bfd_sym_header_block *header ATTRIBUTE_UNUSED)
{
  abort ();
}

int
bfd_sym_read_header (bfd *abfd,
		     bfd_sym_header_block *header,
		     bfd_sym_version version)
{
  switch (version)
    {
    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
      return bfd_sym_read_header_v34 (abfd, header);
    case BFD_SYM_VERSION_3_3:
    case BFD_SYM_VERSION_3_2:
      return bfd_sym_read_header_v32 (abfd, header);
    case BFD_SYM_VERSION_3_1:
    default:
      return 0;
    }
}

int
bfd_sym_read_version (bfd *abfd, bfd_sym_version *version)
{
  char version_string[32];
  long ret;

  ret = bfd_bread (version_string, sizeof (version_string), abfd);
  if (ret != sizeof (version_string))
    return -1;

  if (pstrcmp (version_string, BFD_SYM_VERSION_STR_3_1) == 0)
    *version = BFD_SYM_VERSION_3_1;
  else if (pstrcmp (version_string, BFD_SYM_VERSION_STR_3_2) == 0)
    *version = BFD_SYM_VERSION_3_2;
  else if (pstrcmp (version_string, BFD_SYM_VERSION_STR_3_3) == 0)
    *version = BFD_SYM_VERSION_3_3;
  else if (pstrcmp (version_string, BFD_SYM_VERSION_STR_3_4) == 0)
    *version = BFD_SYM_VERSION_3_4;
  else if (pstrcmp (version_string, BFD_SYM_VERSION_STR_3_5) == 0)
    *version = BFD_SYM_VERSION_3_5;
  else
    return -1;

  return 0;
}

void
bfd_sym_display_table_summary (FILE *f,
			       bfd_sym_table_info *dti,
			       const char *name)
{
  fprintf (f, "%-6s %13ld %13ld %13ld\n",
	   name,
	   dti->dti_first_page,
	   dti->dti_page_count,
	   dti->dti_object_count);
}

void
bfd_sym_display_header (FILE *f, bfd_sym_header_block *dshb)
{
  fprintf (f, "            Version: %.*s\n", dshb->dshb_id[0], dshb->dshb_id + 1);
  fprintf (f, "          Page Size: 0x%x\n", dshb->dshb_page_size);
  fprintf (f, "          Hash Page: %lu\n", dshb->dshb_hash_page);
  fprintf (f, "           Root MTE: %lu\n", dshb->dshb_root_mte);
  fprintf (f, "  Modification Date: ");
  fprintf (f, "[unimplemented]");
  fprintf (f, " (0x%lx)\n", dshb->dshb_mod_date);

  fprintf (f, "       File Creator:  %.4s  Type: %.4s\n\n",
	   dshb->dshb_file_creator, dshb->dshb_file_type);

  fprintf (f, "Table Name   First Page    Page Count   Object Count\n");
  fprintf (f, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

  bfd_sym_display_table_summary (f, &dshb->dshb_nte, "NTE");
  bfd_sym_display_table_summary (f, &dshb->dshb_rte, "RTE");
  bfd_sym_display_table_summary (f, &dshb->dshb_mte, "MTE");
  bfd_sym_display_table_summary (f, &dshb->dshb_frte, "FRTE");
  bfd_sym_display_table_summary (f, &dshb->dshb_cmte, "CMTE");
  bfd_sym_display_table_summary (f, &dshb->dshb_cvte, "CVTE");
  bfd_sym_display_table_summary (f, &dshb->dshb_csnte, "CSNTE");
  bfd_sym_display_table_summary (f, &dshb->dshb_clte, "CLTE");
  bfd_sym_display_table_summary (f, &dshb->dshb_ctte, "CTTE");
  bfd_sym_display_table_summary (f, &dshb->dshb_tte, "TTE");
  bfd_sym_display_table_summary (f, &dshb->dshb_tinfo, "TINFO");
  bfd_sym_display_table_summary (f, &dshb->dshb_fite, "FITE");
  bfd_sym_display_table_summary (f, &dshb->dshb_const, "CONST");

  fprintf (f, "\n");
}

void
bfd_sym_parse_resources_table_entry_v32 (unsigned char *buf,
					 size_t len,
					 bfd_sym_resources_table_entry *entry)
{
  BFD_ASSERT (len == 18);

  memcpy (&entry->rte_res_type, buf, 4);
  entry->rte_res_number = bfd_getb16 (buf + 4);
  entry->rte_nte_index = bfd_getb32 (buf + 6);
  entry->rte_mte_first = bfd_getb16 (buf + 10);
  entry->rte_mte_last = bfd_getb16 (buf + 12);
  entry->rte_res_size = bfd_getb32 (buf + 14);
}

void
bfd_sym_parse_modules_table_entry_v33 (unsigned char *buf,
				       size_t len,
				       bfd_sym_modules_table_entry *entry)
{
  BFD_ASSERT (len == 46);

  entry->mte_rte_index = bfd_getb16 (buf);
  entry->mte_res_offset = bfd_getb32 (buf + 2);
  entry->mte_size = bfd_getb32 (buf + 6);
  entry->mte_kind = buf[10];
  entry->mte_scope = buf[11];
  entry->mte_parent = bfd_getb16 (buf + 12);
  bfd_sym_parse_file_reference_v32 (buf + 14, 6, &entry->mte_imp_fref);
  entry->mte_imp_end = bfd_getb32 (buf + 20);
  entry->mte_nte_index = bfd_getb32 (buf + 24);
  entry->mte_cmte_index = bfd_getb16 (buf + 28);
  entry->mte_cvte_index = bfd_getb32 (buf + 30);
  entry->mte_clte_index = bfd_getb16 (buf + 34);
  entry->mte_ctte_index = bfd_getb16 (buf + 36);
  entry->mte_csnte_idx_1 = bfd_getb32 (buf + 38);
  entry->mte_csnte_idx_2 = bfd_getb32 (buf + 42);
}

void
bfd_sym_parse_file_references_table_entry_v32 (unsigned char *buf,
					       size_t len,
					       bfd_sym_file_references_table_entry *entry)
{
  unsigned int type;

  BFD_ASSERT (len == 10);

  memset (entry, 0, sizeof (bfd_sym_file_references_table_entry));
  type = bfd_getb16 (buf);

  switch (type)
    {
    case BFD_SYM_END_OF_LIST_3_2:
      entry->generic.type = BFD_SYM_END_OF_LIST;
      break;

    case BFD_SYM_FILE_NAME_INDEX_3_2:
      entry->filename.type = BFD_SYM_FILE_NAME_INDEX;
      entry->filename.nte_index = bfd_getb32 (buf + 2);
      entry->filename.mod_date = bfd_getb32 (buf + 6);
      break;

    default:
      entry->entry.mte_index = type;
      entry->entry.file_offset = bfd_getb32 (buf + 2);
    }
}

void
bfd_sym_parse_contained_modules_table_entry_v32 (unsigned char *buf,
						 size_t len,
						 bfd_sym_contained_modules_table_entry *entry)
{
  unsigned int type;

  BFD_ASSERT (len == 6);

  memset (entry, 0, sizeof (bfd_sym_contained_modules_table_entry));
  type = bfd_getb16 (buf);

  switch (type)
    {
    case BFD_SYM_END_OF_LIST_3_2:
      entry->generic.type = BFD_SYM_END_OF_LIST;
      break;

    default:
      entry->entry.mte_index = type;
      entry->entry.nte_index = bfd_getb32 (buf + 2);
      break;
    }
}

void
bfd_sym_parse_contained_variables_table_entry_v32 (unsigned char *buf,
						   size_t len,
						   bfd_sym_contained_variables_table_entry *entry)
{
  unsigned int type;

  BFD_ASSERT (len == 26);

  memset (entry, 0, sizeof (bfd_sym_contained_variables_table_entry));
  type = bfd_getb16 (buf);

  switch (type)
    {
    case BFD_SYM_END_OF_LIST_3_2:
      entry->generic.type = BFD_SYM_END_OF_LIST;
      break;

    case BFD_SYM_SOURCE_FILE_CHANGE_3_2:
      entry->file.type = BFD_SYM_SOURCE_FILE_CHANGE;
      bfd_sym_parse_file_reference_v32 (buf + 2, 6, &entry->file.fref);
      break;

    default:
      entry->entry.tte_index = type;
      entry->entry.nte_index = bfd_getb32 (buf + 2);
      entry->entry.file_delta = bfd_getb16 (buf + 6);
      entry->entry.scope = buf[8];
      entry->entry.la_size = buf[9];

      if (entry->entry.la_size == BFD_SYM_CVTE_SCA)
	{
	  entry->entry.address.scstruct.sca_kind = buf[10];
	  entry->entry.address.scstruct.sca_class = buf[11];
	  entry->entry.address.scstruct.sca_offset = bfd_getb32 (buf + 12);
	}
      else if (entry->entry.la_size <= BFD_SYM_CVTE_SCA)
	{
#if BFD_SYM_CVTE_SCA > 0
	  memcpy (&entry->entry.address.lastruct.la, buf + 10,
		  BFD_SYM_CVTE_SCA);
#endif
	  entry->entry.address.lastruct.la_kind = buf[23];
	}
      else if (entry->entry.la_size == BFD_SYM_CVTE_BIG_LA)
	{
	  entry->entry.address.biglastruct.big_la = bfd_getb32 (buf + 10);
	  entry->entry.address.biglastruct.big_la_kind = buf[12];
	}
    }
}

void
bfd_sym_parse_contained_statements_table_entry_v32 (unsigned char *buf,
						    size_t len,
						    bfd_sym_contained_statements_table_entry *entry)
{
  unsigned int type;

  BFD_ASSERT (len == 8);

  memset (entry, 0, sizeof (bfd_sym_contained_statements_table_entry));
  type = bfd_getb16 (buf);

  switch (type)
    {
    case BFD_SYM_END_OF_LIST_3_2:
      entry->generic.type = BFD_SYM_END_OF_LIST;
      break;

    case BFD_SYM_SOURCE_FILE_CHANGE_3_2:
      entry->file.type = BFD_SYM_SOURCE_FILE_CHANGE;
      bfd_sym_parse_file_reference_v32 (buf + 2, 6, &entry->file.fref);
      break;

    default:
      entry->entry.mte_index = type;
      entry->entry.mte_offset = bfd_getb16 (buf + 2);
      entry->entry.file_delta = bfd_getb32 (buf + 4);
      break;
    }
}

void
bfd_sym_parse_contained_labels_table_entry_v32 (unsigned char *buf,
						size_t len,
						bfd_sym_contained_labels_table_entry *entry)
{
  unsigned int type;

  BFD_ASSERT (len == 12);

  memset (entry, 0, sizeof (bfd_sym_contained_labels_table_entry));
  type = bfd_getb16 (buf);

  switch (type)
    {
    case BFD_SYM_END_OF_LIST_3_2:
      entry->generic.type = BFD_SYM_END_OF_LIST;
      break;

    case BFD_SYM_SOURCE_FILE_CHANGE_3_2:
      entry->file.type = BFD_SYM_SOURCE_FILE_CHANGE;
      bfd_sym_parse_file_reference_v32 (buf + 2, 6, &entry->file.fref);
      break;

    default:
      entry->entry.mte_index = type;
      entry->entry.mte_offset = bfd_getb16 (buf + 2);
      entry->entry.nte_index = bfd_getb32 (buf + 4);
      entry->entry.file_delta = bfd_getb16 (buf + 8);
      entry->entry.scope = bfd_getb16 (buf + 10);
      break;
    }
}

void
bfd_sym_parse_type_table_entry_v32 (unsigned char *buf,
				    size_t len,
				    bfd_sym_type_table_entry *entry)
{
  BFD_ASSERT (len == 4);

  *entry = bfd_getb32 (buf);
}

int
bfd_sym_fetch_resources_table_entry (bfd *abfd,
				     bfd_sym_resources_table_entry *entry,
				     unsigned long sym_index)
{
  void (*parser) (unsigned char *, size_t, bfd_sym_resources_table_entry *);
  unsigned long offset;
  unsigned long entry_size;
  unsigned char buf[18];
  bfd_sym_data_struct *sdata = NULL;

  parser = NULL;
  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sym_index == 0)
    return -1;

  switch (sdata->version)
    {
    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
      return -1;

    case BFD_SYM_VERSION_3_3:
    case BFD_SYM_VERSION_3_2:
      entry_size = 18;
      parser = bfd_sym_parse_resources_table_entry_v32;
      break;

    case BFD_SYM_VERSION_3_1:
    default:
      return -1;
    }
  if (parser == NULL)
    return -1;

  offset = compute_offset (sdata->header.dshb_rte.dti_first_page,
			   sdata->header.dshb_page_size,
			   entry_size, sym_index);

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;
  if (bfd_bread (buf, entry_size, abfd) != entry_size)
    return -1;

  (*parser) (buf, entry_size, entry);

  return 0;
}

int
bfd_sym_fetch_modules_table_entry (bfd *abfd,
				   bfd_sym_modules_table_entry *entry,
				   unsigned long sym_index)
{
  void (*parser) (unsigned char *, size_t, bfd_sym_modules_table_entry *);
  unsigned long offset;
  unsigned long entry_size;
  unsigned char buf[46];
  bfd_sym_data_struct *sdata = NULL;

  parser = NULL;
  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sym_index == 0)
    return -1;

  switch (sdata->version)
    {
    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
      return -1;

    case BFD_SYM_VERSION_3_3:
      entry_size = 46;
      parser = bfd_sym_parse_modules_table_entry_v33;
      break;

    case BFD_SYM_VERSION_3_2:
    case BFD_SYM_VERSION_3_1:
    default:
      return -1;
    }
  if (parser == NULL)
    return -1;

  offset = compute_offset (sdata->header.dshb_mte.dti_first_page,
			   sdata->header.dshb_page_size,
			   entry_size, sym_index);

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;
  if (bfd_bread (buf, entry_size, abfd) != entry_size)
    return -1;

  (*parser) (buf, entry_size, entry);

  return 0;
}

int
bfd_sym_fetch_file_references_table_entry (bfd *abfd,
					   bfd_sym_file_references_table_entry *entry,
					   unsigned long sym_index)
{
  void (*parser) (unsigned char *, size_t, bfd_sym_file_references_table_entry *);
  unsigned long offset;
  unsigned long entry_size = 0;
  unsigned char buf[8];
  bfd_sym_data_struct *sdata = NULL;

  parser = NULL;
  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sym_index == 0)
    return -1;

  switch (sdata->version)
    {
    case BFD_SYM_VERSION_3_3:
    case BFD_SYM_VERSION_3_2:
      entry_size = 10;
      parser = bfd_sym_parse_file_references_table_entry_v32;
      break;

    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
    case BFD_SYM_VERSION_3_1:
    default:
      break;
    }

  if (parser == NULL)
    return -1;

  offset = compute_offset (sdata->header.dshb_frte.dti_first_page,
			   sdata->header.dshb_page_size,
			   entry_size, sym_index);

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;
  if (bfd_bread (buf, entry_size, abfd) != entry_size)
    return -1;

  (*parser) (buf, entry_size, entry);

  return 0;
}

int
bfd_sym_fetch_contained_modules_table_entry (bfd *abfd,
					     bfd_sym_contained_modules_table_entry *entry,
					     unsigned long sym_index)
{
  void (*parser) (unsigned char *, size_t, bfd_sym_contained_modules_table_entry *);
  unsigned long offset;
  unsigned long entry_size = 0;
  unsigned char buf[6];
  bfd_sym_data_struct *sdata = NULL;

  parser = NULL;
  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sym_index == 0)
    return -1;

  switch (sdata->version)
    {
    case BFD_SYM_VERSION_3_3:
    case BFD_SYM_VERSION_3_2:
      entry_size = 6;
      parser = bfd_sym_parse_contained_modules_table_entry_v32;
      break;

    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
    case BFD_SYM_VERSION_3_1:
    default:
      break;
    }

  if (parser == NULL)
    return -1;

  offset = compute_offset (sdata->header.dshb_cmte.dti_first_page,
			   sdata->header.dshb_page_size,
			   entry_size, sym_index);

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;
  if (bfd_bread (buf, entry_size, abfd) != entry_size)
    return -1;

  (*parser) (buf, entry_size, entry);

  return 0;
}

int
bfd_sym_fetch_contained_variables_table_entry (bfd *abfd,
					       bfd_sym_contained_variables_table_entry *entry,
					       unsigned long sym_index)
{
  void (*parser) (unsigned char *, size_t, bfd_sym_contained_variables_table_entry *);
  unsigned long offset;
  unsigned long entry_size = 0;
  unsigned char buf[26];
  bfd_sym_data_struct *sdata = NULL;

  parser = NULL;
  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sym_index == 0)
    return -1;

  switch (sdata->version)
    {
    case BFD_SYM_VERSION_3_3:
    case BFD_SYM_VERSION_3_2:
      entry_size = 26;
      parser = bfd_sym_parse_contained_variables_table_entry_v32;
      break;

    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
    case BFD_SYM_VERSION_3_1:
    default:
      break;
    }

  if (parser == NULL)
    return -1;

  offset = compute_offset (sdata->header.dshb_cvte.dti_first_page,
			   sdata->header.dshb_page_size,
			   entry_size, sym_index);

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;
  if (bfd_bread (buf, entry_size, abfd) != entry_size)
    return -1;

  (*parser) (buf, entry_size, entry);

  return 0;
}

int
bfd_sym_fetch_contained_statements_table_entry (bfd *abfd,
						bfd_sym_contained_statements_table_entry *entry,
						unsigned long sym_index)
{
  void (*parser) (unsigned char *, size_t, bfd_sym_contained_statements_table_entry *);
  unsigned long offset;
  unsigned long entry_size = 0;
  unsigned char buf[8];
  bfd_sym_data_struct *sdata = NULL;

  parser = NULL;
  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sym_index == 0)
    return -1;

  switch (sdata->version)
    {
    case BFD_SYM_VERSION_3_3:
    case BFD_SYM_VERSION_3_2:
      entry_size = 8;
      parser = bfd_sym_parse_contained_statements_table_entry_v32;
      break;

    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
    case BFD_SYM_VERSION_3_1:
    default:
      break;
    }

  if (parser == NULL)
    return -1;

  offset = compute_offset (sdata->header.dshb_csnte.dti_first_page,
			   sdata->header.dshb_page_size,
			   entry_size, sym_index);

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;
  if (bfd_bread (buf, entry_size, abfd) != entry_size)
    return -1;

  (*parser) (buf, entry_size, entry);

  return 0;
}

int
bfd_sym_fetch_contained_labels_table_entry (bfd *abfd,
					    bfd_sym_contained_labels_table_entry *entry,
					    unsigned long sym_index)
{
  void (*parser) (unsigned char *, size_t, bfd_sym_contained_labels_table_entry *);
  unsigned long offset;
  unsigned long entry_size = 0;
  unsigned char buf[12];
  bfd_sym_data_struct *sdata = NULL;

  parser = NULL;
  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sym_index == 0)
    return -1;

  switch (sdata->version)
    {
    case BFD_SYM_VERSION_3_3:
    case BFD_SYM_VERSION_3_2:
      entry_size = 12;
      parser = bfd_sym_parse_contained_labels_table_entry_v32;
      break;

    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
    case BFD_SYM_VERSION_3_1:
    default:
      break;
    }

  if (parser == NULL)
    return -1;

  offset = compute_offset (sdata->header.dshb_clte.dti_first_page,
			   sdata->header.dshb_page_size,
			   entry_size, sym_index);

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;
  if (bfd_bread (buf, entry_size, abfd) != entry_size)
    return -1;

  (*parser) (buf, entry_size, entry);

  return 0;
}

int
bfd_sym_fetch_contained_types_table_entry (bfd *abfd,
					   bfd_sym_contained_types_table_entry *entry,
					   unsigned long sym_index)
{
  void (*parser) (unsigned char *, size_t, bfd_sym_contained_types_table_entry *);
  unsigned long offset;
  unsigned long entry_size = 0;
  unsigned char buf[0];
  bfd_sym_data_struct *sdata = NULL;

  parser = NULL;
  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sym_index == 0)
    return -1;

  switch (sdata->version)
    {
    case BFD_SYM_VERSION_3_3:
    case BFD_SYM_VERSION_3_2:
      entry_size = 0;
      parser = NULL;
      break;

    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
    case BFD_SYM_VERSION_3_1:
    default:
      break;
    }

  if (parser == NULL)
    return -1;

  offset = compute_offset (sdata->header.dshb_ctte.dti_first_page,
			   sdata->header.dshb_page_size,
			   entry_size, sym_index);

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;
  if (bfd_bread (buf, entry_size, abfd) != entry_size)
    return -1;

  (*parser) (buf, entry_size, entry);

  return 0;
}

int
bfd_sym_fetch_file_references_index_table_entry (bfd *abfd,
						 bfd_sym_file_references_index_table_entry *entry,
						 unsigned long sym_index)
{
  void (*parser) (unsigned char *, size_t, bfd_sym_file_references_index_table_entry *);
  unsigned long offset;
  unsigned long entry_size = 0;
  unsigned char buf[0];
  bfd_sym_data_struct *sdata = NULL;

  parser = NULL;
  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sym_index == 0)
    return -1;

  switch (sdata->version)
    {
    case BFD_SYM_VERSION_3_3:
    case BFD_SYM_VERSION_3_2:
      entry_size = 0;
      parser = NULL;
      break;

    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
    case BFD_SYM_VERSION_3_1:
    default:
      break;
    }

  if (parser == NULL)
    return -1;

  offset = compute_offset (sdata->header.dshb_fite.dti_first_page,
			   sdata->header.dshb_page_size,
			   entry_size, sym_index);

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;
  if (bfd_bread (buf, entry_size, abfd) != entry_size)
    return -1;

  (*parser) (buf, entry_size, entry);

  return 0;
}

int
bfd_sym_fetch_constant_pool_entry (bfd *abfd,
				   bfd_sym_constant_pool_entry *entry,
				   unsigned long sym_index)
{
  void (*parser) (unsigned char *, size_t, bfd_sym_constant_pool_entry *);
  unsigned long offset;
  unsigned long entry_size = 0;
  unsigned char buf[0];
  bfd_sym_data_struct *sdata = NULL;

  parser = NULL;
  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sym_index == 0)
    return -1;

  switch (sdata->version)
    {
    case BFD_SYM_VERSION_3_3:
    case BFD_SYM_VERSION_3_2:
      entry_size = 0;
      parser = NULL;
      break;

    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
    case BFD_SYM_VERSION_3_1:
    default:
      break;
    }

  if (parser == NULL)
    return -1;

  offset = compute_offset (sdata->header.dshb_fite.dti_first_page,
			   sdata->header.dshb_page_size,
			   entry_size, sym_index);

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;
  if (bfd_bread (buf, entry_size, abfd) != entry_size)
    return -1;

  (*parser) (buf, entry_size, entry);

  return 0;
}

int
bfd_sym_fetch_type_table_entry (bfd *abfd,
				bfd_sym_type_table_entry *entry,
				unsigned long sym_index)
{
  void (*parser) (unsigned char *, size_t, bfd_sym_type_table_entry *);
  unsigned long offset;
  unsigned long entry_size = 0;
  unsigned char buf[4];
  bfd_sym_data_struct *sdata = NULL;

  parser = NULL;
  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  switch (sdata->version)
    {
    case BFD_SYM_VERSION_3_3:
    case BFD_SYM_VERSION_3_2:
      entry_size = 4;
      parser = bfd_sym_parse_type_table_entry_v32;
      break;

    case BFD_SYM_VERSION_3_5:
    case BFD_SYM_VERSION_3_4:
    case BFD_SYM_VERSION_3_1:
    default:
      break;
    }

  if (parser == NULL)
    return -1;

  offset = compute_offset (sdata->header.dshb_tte.dti_first_page,
			   sdata->header.dshb_page_size,
			   entry_size, sym_index);

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;
  if (bfd_bread (buf, entry_size, abfd) != entry_size)
    return -1;

  (*parser) (buf, entry_size, entry);

  return 0;
}

int
bfd_sym_fetch_type_information_table_entry (bfd *abfd,
					    bfd_sym_type_information_table_entry *entry,
					    unsigned long offset)
{
  unsigned char buf[4];

  BFD_ASSERT (bfd_sym_valid (abfd));

  if (offset == 0)
    return -1;

  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
    return -1;

  if (bfd_bread (buf, 4, abfd) != 4)
    return -1;
  entry->nte_index = bfd_getb32 (buf);

  if (bfd_bread (buf, 2, abfd) != 2)
    return -1;
  entry->physical_size = bfd_getb16 (buf);

  if (entry->physical_size & 0x8000)
    {
      if (bfd_bread (buf, 4, abfd) != 4)
	return -1;
      entry->physical_size &= 0x7fff;
      entry->logical_size = bfd_getb32 (buf);
      entry->offset = offset + 10;
    }
  else
    {
      if (bfd_bread (buf, 2, abfd) != 2)
	return -1;
      entry->physical_size &= 0x7fff;
      entry->logical_size = bfd_getb16 (buf);
      entry->offset = offset + 8;
    }

  return 0;
}

int
bfd_sym_fetch_type_table_information (bfd *abfd,
				      bfd_sym_type_information_table_entry *entry,
				      unsigned long sym_index)
{
  bfd_sym_type_table_entry tindex;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sdata->header.dshb_tte.dti_object_count <= 99)
    return -1;
  if (sym_index < 100)
    return -1;

  if (bfd_sym_fetch_type_table_entry (abfd, &tindex, sym_index - 100) < 0)
    return -1;
  if (bfd_sym_fetch_type_information_table_entry (abfd, entry, tindex) < 0)
    return -1;

  return 0;
}

const unsigned char *
bfd_sym_symbol_name (bfd *abfd, unsigned long sym_index)
{
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sym_index == 0)
    return (const unsigned char *) "";

  sym_index *= 2;
  if ((sym_index / sdata->header.dshb_page_size)
      > sdata->header.dshb_nte.dti_page_count)
    return (const unsigned char *) "\09[INVALID]";

  return (const unsigned char *) sdata->name_table + sym_index;
}

const unsigned char *
bfd_sym_module_name (bfd *abfd, unsigned long sym_index)
{
  bfd_sym_modules_table_entry entry;

  if (bfd_sym_fetch_modules_table_entry (abfd, &entry, sym_index) < 0)
    return (const unsigned char *) "\09[INVALID]";

  return bfd_sym_symbol_name (abfd, entry.mte_nte_index);
}

const char *
bfd_sym_unparse_storage_kind (enum bfd_sym_storage_kind kind)
{
  switch (kind)
    {
    case BFD_SYM_STORAGE_KIND_LOCAL: return "LOCAL";
    case BFD_SYM_STORAGE_KIND_VALUE: return "VALUE";
    case BFD_SYM_STORAGE_KIND_REFERENCE: return "REFERENCE";
    case BFD_SYM_STORAGE_KIND_WITH: return "WITH";
    default: return "[UNKNOWN]";
    }
}

const char *
bfd_sym_unparse_storage_class (enum bfd_sym_storage_class kind)
{
  switch (kind)
    {
    case BFD_SYM_STORAGE_CLASS_REGISTER: return "REGISTER";
    case BFD_SYM_STORAGE_CLASS_GLOBAL: return "GLOBAL";
    case BFD_SYM_STORAGE_CLASS_FRAME_RELATIVE: return "FRAME_RELATIVE";
    case BFD_SYM_STORAGE_CLASS_STACK_RELATIVE: return "STACK_RELATIVE";
    case BFD_SYM_STORAGE_CLASS_ABSOLUTE: return "ABSOLUTE";
    case BFD_SYM_STORAGE_CLASS_CONSTANT: return "CONSTANT";
    case BFD_SYM_STORAGE_CLASS_RESOURCE: return "RESOURCE";
    case BFD_SYM_STORAGE_CLASS_BIGCONSTANT: return "BIGCONSTANT";
    default: return "[UNKNOWN]";
    }
}

const char *
bfd_sym_unparse_module_kind (enum bfd_sym_module_kind kind)
{
  switch (kind)
    {
    case BFD_SYM_MODULE_KIND_NONE: return "NONE";
    case BFD_SYM_MODULE_KIND_PROGRAM: return "PROGRAM";
    case BFD_SYM_MODULE_KIND_UNIT: return "UNIT";
    case BFD_SYM_MODULE_KIND_PROCEDURE: return "PROCEDURE";
    case BFD_SYM_MODULE_KIND_FUNCTION: return "FUNCTION";
    case BFD_SYM_MODULE_KIND_DATA: return "DATA";
    case BFD_SYM_MODULE_KIND_BLOCK: return "BLOCK";
    default: return "[UNKNOWN]";
    }
}

const char *
bfd_sym_unparse_symbol_scope (enum bfd_sym_symbol_scope scope)
{
  switch (scope)
    {
    case BFD_SYM_SYMBOL_SCOPE_LOCAL: return "LOCAL";
    case BFD_SYM_SYMBOL_SCOPE_GLOBAL: return "GLOBAL";
    default:
      return "[UNKNOWN]";
    }
}

void
bfd_sym_print_file_reference (bfd *abfd,
			      FILE *f,
			      bfd_sym_file_reference *entry)
{
  bfd_sym_file_references_table_entry frtentry;
  int ret;

  ret = bfd_sym_fetch_file_references_table_entry (abfd, &frtentry,
						   entry->fref_frte_index);
  fprintf (f, "FILE ");

  if ((ret < 0) || (frtentry.generic.type != BFD_SYM_FILE_NAME_INDEX))
    fprintf (f, "[INVALID]");
  else
    fprintf (f, "\"%.*s\"",
	     bfd_sym_symbol_name (abfd, frtentry.filename.nte_index)[0],
	     &bfd_sym_symbol_name (abfd, frtentry.filename.nte_index)[1]);

  fprintf (f, " (FRTE %lu)", entry->fref_frte_index);
}

void
bfd_sym_print_resources_table_entry (bfd *abfd,
				     FILE *f,
				     bfd_sym_resources_table_entry *entry)
{
  fprintf (f, " \"%.*s\" (NTE %lu), type \"%.4s\", num %u, size %lu, MTE %lu -- %lu",
	   bfd_sym_symbol_name (abfd, entry->rte_nte_index)[0],
	   &bfd_sym_symbol_name (abfd, entry->rte_nte_index)[1],
	   entry->rte_nte_index, entry->rte_res_type, entry->rte_res_number,
	   entry->rte_res_size, entry->rte_mte_first, entry->rte_mte_last);
}

void
bfd_sym_print_modules_table_entry (bfd *abfd,
				   FILE *f,
				   bfd_sym_modules_table_entry *entry)
{
  fprintf (f, "\"%.*s\" (NTE %lu)",
	   bfd_sym_symbol_name (abfd, entry->mte_nte_index)[0],
	   &bfd_sym_symbol_name (abfd, entry->mte_nte_index)[1],
	   entry->mte_nte_index);

  fprintf (f, "\n            ");

  bfd_sym_print_file_reference (abfd, f, &entry->mte_imp_fref);
  fprintf (f, " range %lu -- %lu",
	   entry->mte_imp_fref.fref_offset, entry->mte_imp_end);

  fprintf (f, "\n            ");

  fprintf (f, "kind %s", bfd_sym_unparse_module_kind (entry->mte_kind));
  fprintf (f, ", scope %s", bfd_sym_unparse_symbol_scope (entry->mte_scope));

  fprintf (f, ", RTE %lu, offset %lu, size %lu",
	   entry->mte_rte_index, entry->mte_res_offset, entry->mte_size);

  fprintf (f, "\n            ");

  fprintf (f, "CMTE %lu, CVTE %lu, CLTE %lu, CTTE %lu, CSNTE1 %lu, CSNTE2 %lu",
	   entry->mte_cmte_index, entry->mte_cvte_index,
	   entry->mte_clte_index, entry->mte_ctte_index,
	   entry->mte_csnte_idx_1, entry->mte_csnte_idx_2);

  if (entry->mte_parent != 0)
    fprintf (f, ", parent %lu", entry->mte_parent);
  else
    fprintf (f, ", no parent");

  if (entry->mte_cmte_index != 0)
    fprintf (f, ", child %lu", entry->mte_cmte_index);
  else
    fprintf (f, ", no child");
}

void
bfd_sym_print_file_references_table_entry (bfd *abfd,
					   FILE *f,
					   bfd_sym_file_references_table_entry *entry)
{
  switch (entry->generic.type)
    {
    case BFD_SYM_FILE_NAME_INDEX:
      fprintf (f, "FILE \"%.*s\" (NTE %lu), modtime ",
	       bfd_sym_symbol_name (abfd, entry->filename.nte_index)[0],
	       &bfd_sym_symbol_name (abfd, entry->filename.nte_index)[1],
	       entry->filename.nte_index);

      fprintf (f, "[UNIMPLEMENTED]");
      /* printModDate (entry->filename.mod_date); */
      fprintf (f, " (0x%lx)", entry->filename.mod_date);
      break;

    case BFD_SYM_END_OF_LIST:
      fprintf (f, "END");
      break;

    default:
      fprintf (f, "\"%.*s\" (MTE %lu), offset %lu",
	       bfd_sym_module_name (abfd, entry->entry.mte_index)[0],
	       &bfd_sym_module_name (abfd, entry->entry.mte_index)[1],
	       entry->entry.mte_index,
	       entry->entry.file_offset);
      break;
    }
}

void
bfd_sym_print_contained_modules_table_entry (bfd *abfd,
					     FILE *f,
					     bfd_sym_contained_modules_table_entry *entry)
{
  switch (entry->generic.type)
    {
    case BFD_SYM_END_OF_LIST:
      fprintf (f, "END");
      break;

    default:
      fprintf (f, "\"%.*s\" (MTE %lu, NTE %lu)",
	       bfd_sym_module_name (abfd, entry->entry.mte_index)[0],
	       &bfd_sym_module_name (abfd, entry->entry.mte_index)[1],
	       entry->entry.mte_index,
	       entry->entry.nte_index);
      break;
    }
}

void
bfd_sym_print_contained_variables_table_entry (bfd *abfd,
					       FILE *f,
					       bfd_sym_contained_variables_table_entry *entry)
{
  if (entry->generic.type == BFD_SYM_END_OF_LIST)
    {
      fprintf (f, "END");
      return;
    }

  if (entry->generic.type == BFD_SYM_SOURCE_FILE_CHANGE)
    {
      bfd_sym_print_file_reference (abfd, f, &entry->file.fref);
      fprintf (f, " offset %lu", entry->file.fref.fref_offset);
      return;
    }

  fprintf (f, "\"%.*s\" (NTE %lu)",
	   bfd_sym_symbol_name (abfd, entry->entry.nte_index)[0],
	   &bfd_sym_symbol_name (abfd, entry->entry.nte_index)[1],
	   entry->entry.nte_index);

  fprintf (f, ", TTE %lu", entry->entry.tte_index);
  fprintf (f, ", offset %lu", entry->entry.file_delta);
  fprintf (f, ", scope %s", bfd_sym_unparse_symbol_scope (entry->entry.scope));

  if (entry->entry.la_size == BFD_SYM_CVTE_SCA)
    fprintf (f, ", latype %s, laclass %s, laoffset %lu",
	     bfd_sym_unparse_storage_kind (entry->entry.address.scstruct.sca_kind),
	     bfd_sym_unparse_storage_class (entry->entry.address.scstruct.sca_class),
	     entry->entry.address.scstruct.sca_offset);
  else if (entry->entry.la_size <= BFD_SYM_CVTE_LA_MAX_SIZE)
    {
      unsigned long i;

      fprintf (f, ", la [");
      for (i = 0; i < entry->entry.la_size; i++)
	fprintf (f, "0x%02x ", entry->entry.address.lastruct.la[i]);
      fprintf (f, "]");
    }
  else if (entry->entry.la_size == BFD_SYM_CVTE_BIG_LA)
    fprintf (f, ", bigla %lu, biglakind %u",
	     entry->entry.address.biglastruct.big_la,
	     entry->entry.address.biglastruct.big_la_kind);

  else
    fprintf (f, ", la [INVALID]");
}

void
bfd_sym_print_contained_statements_table_entry (bfd *abfd,
						FILE *f,
						bfd_sym_contained_statements_table_entry *entry)
{
  if (entry->generic.type == BFD_SYM_END_OF_LIST)
    {
      fprintf (f, "END");
      return;
    }

  if (entry->generic.type == BFD_SYM_SOURCE_FILE_CHANGE)
    {
      bfd_sym_print_file_reference (abfd, f, &entry->file.fref);
      fprintf (f, " offset %lu", entry->file.fref.fref_offset);
      return;
    }

  fprintf (f, "\"%.*s\" (MTE %lu), offset %lu, delta %lu",
	   bfd_sym_module_name (abfd, entry->entry.mte_index)[0],
	   &bfd_sym_module_name (abfd, entry->entry.mte_index)[1],
	   entry->entry.mte_index,
	   entry->entry.mte_offset,
	   entry->entry.file_delta);
}

void
bfd_sym_print_contained_labels_table_entry (bfd *abfd,
					    FILE *f,
					    bfd_sym_contained_labels_table_entry *entry)
{
  if (entry->generic.type == BFD_SYM_END_OF_LIST)
    {
      fprintf (f, "END");
      return;
    }

  if (entry->generic.type == BFD_SYM_SOURCE_FILE_CHANGE)
    {
      bfd_sym_print_file_reference (abfd, f, &entry->file.fref);
      fprintf (f, " offset %lu", entry->file.fref.fref_offset);
      return;
    }

  fprintf (f, "\"%.*s\" (MTE %lu), offset %lu, delta %lu, scope %s",
	   bfd_sym_module_name (abfd, entry->entry.mte_index)[0],
	   &bfd_sym_module_name (abfd, entry->entry.mte_index)[1],
	   entry->entry.mte_index,
	   entry->entry.mte_offset,
	   entry->entry.file_delta,
	   bfd_sym_unparse_symbol_scope (entry->entry.scope));
}

void
bfd_sym_print_contained_types_table_entry (bfd *abfd ATTRIBUTE_UNUSED,
					   FILE *f,
					   bfd_sym_contained_types_table_entry *entry ATTRIBUTE_UNUSED)
{
  fprintf (f, "[UNIMPLEMENTED]");
}

const char *
bfd_sym_type_operator_name (unsigned char num)
{
  switch (num)
    {
    case 1: return "TTE";
    case 2: return "PointerTo";
    case 3: return "ScalarOf";
    case 4: return "ConstantOf";
    case 5: return "EnumerationOf";
    case 6: return "VectorOf";
    case 7: return "RecordOf";
    case 8: return "UnionOf";
    case 9: return "SubRangeOf";
    case 10: return "SetOf";
    case 11: return "NamedTypeOf";
    case 12: return "ProcOf";
    case 13: return "ValueOf";
    case 14: return "ArrayOf";
    default: return "[UNKNOWN OPERATOR]";
    }
}

const char *
bfd_sym_type_basic_name (unsigned char num)
{
  switch (num)
    {
    case 0: return "void";
    case 1: return "pascal string";
    case 2: return "unsigned long";
    case 3: return "signed long";
    case 4: return "extended (10 bytes)";
    case 5: return "pascal boolean (1 byte)";
    case 6: return "unsigned byte";
    case 7: return "signed byte";
    case 8: return "character (1 byte)";
    case 9: return "wide character (2 bytes)";
    case 10: return "unsigned short";
    case 11: return "signed short";
    case 12: return "singled";
    case 13: return "double";
    case 14: return "extended (12 bytes)";
    case 15: return "computational (8 bytes)";
    case 16: return "c string";
    case 17: return "as-is string";
    default: return "[UNKNOWN BASIC TYPE]";
    }
}

int
bfd_sym_fetch_long (unsigned char *buf,
		    unsigned long len,
		    unsigned long offset,
		    unsigned long *offsetptr,
		    long *value)
{
  int ret;

  if (offset >= len)
    {
      *value = 0;
      offset += 0;
      ret = -1;
    }
  else if (! (buf[offset] & 0x80))
    {
      *value = buf[offset];
      offset += 1;
      ret = 0;
    }
  else if (buf[offset] == 0xc0)
    {
      if ((offset + 5) > len)
	{
	  *value = 0;
	  offset = len;
	  ret = -1;
	}
      else
	{
	  *value = bfd_getb32 (buf + offset + 1);
	  offset += 5;
	  ret = 0;
	}
    }
  else if ((buf[offset] & 0xc0) == 0xc0)
    {
      *value =  -(buf[offset] & 0x3f);
      offset += 1;
      ret = 0;
    }
  else if ((buf[offset] & 0xc0) == 0x80)
    {
      if ((offset + 2) > len)
	{
	  *value = 0;
	  offset = len;
	  ret = -1;
	}
      else
	{
	  *value = bfd_getb16 (buf + offset) & 0x3fff;
	  offset += 2;
	  ret = 0;
	}
    }
  else
    abort ();

  if (offsetptr != NULL)
    *offsetptr = offset;

  return ret;
}

void
bfd_sym_print_type_information (bfd *abfd,
				FILE *f,
				unsigned char *buf,
				unsigned long len,
				unsigned long offset,
				unsigned long *offsetptr)
{
  unsigned int type;

  if (offset >= len)
    {
      fprintf (f, "[NULL]");

      if (offsetptr != NULL)
	*offsetptr = offset;
      return;
  }

  type = buf[offset];
  offset++;

  if (! (type & 0x80))
    {
      fprintf (f, "[%s] (0x%x)", bfd_sym_type_basic_name (type & 0x7f), type);

      if (offsetptr != NULL)
	*offsetptr = offset;
      return;
    }

  if (type & 0x40)
    fprintf (f, "[packed ");
  else
    fprintf (f, "[");

  switch (type & 0x3f)
    {
    case 1:
      {
	long value;
	bfd_sym_type_information_table_entry tinfo;

	bfd_sym_fetch_long (buf, len, offset, &offset, &value);
	if (value <= 0)
	  fprintf (f, "[INVALID]");
	else
	  {
	    if (bfd_sym_fetch_type_table_information (abfd, &tinfo, value) < 0)
	      fprintf (f, "[INVALID]");
	    else
	      fprintf (f, "\"%.*s\"",
		       bfd_sym_symbol_name (abfd, tinfo.nte_index)[0],
		       &bfd_sym_symbol_name (abfd, tinfo.nte_index)[1]);
	  }
	fprintf (f, " (TTE %lu)", (unsigned long) value);
	break;
      }

    case 2:
      fprintf (f, "pointer (0x%x) to ", type);
      bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
      break;

    case 3:
      {
	long value;

	fprintf (f, "scalar (0x%x) of ", type);
	bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
	bfd_sym_fetch_long (buf, len, offset, &offset, &value);
	fprintf (f, " (%lu)", (unsigned long) value);
	break;
      }

    case 5:
      {
	long lower, upper, nelem;
	int i;

	fprintf (f, "enumeration (0x%x) of ", type);
	bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
	bfd_sym_fetch_long (buf, len, offset, &offset, &lower);
	bfd_sym_fetch_long (buf, len, offset, &offset, &upper);
	bfd_sym_fetch_long (buf, len, offset, &offset, &nelem);
	fprintf (f, " from %lu to %lu with %lu elements: ",
		 (unsigned long) lower, (unsigned long) upper,
		 (unsigned long) nelem);

	for (i = 0; i < nelem; i++)
	  {
	    fprintf (f, "\n                    ");
	    bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
	  }
	break;
      }

    case 6:
      fprintf (f, "vector (0x%x)", type);
      fprintf (f, "\n                index ");
      bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
      fprintf (f, "\n                target ");
      bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
      break;

    case 7:
    case 8:
      {
	long nrec, eloff, i;

	if ((type & 0x3f) == 7)
	  fprintf (f, "record (0x%x) of ", type);
	else
	  fprintf (f, "union (0x%x) of ", type);

	bfd_sym_fetch_long (buf, len, offset, &offset, &nrec);
	fprintf (f, "%lu elements: ", (unsigned long) nrec);

	for (i = 0; i < nrec; i++)
	  {
	    bfd_sym_fetch_long (buf, len, offset, &offset, &eloff);
	    fprintf (f, "\n                ");
	    fprintf (f, "offset %lu: ", (unsigned long) eloff);
	    bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
	  }
	break;
      }

    case 9:
      fprintf (f, "subrange (0x%x) of ", type);
      bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
      fprintf (f, " lower ");
      bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
      fprintf (f, " upper ");
      bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
      break;

  case 11:
    {
      long value;

      fprintf (f, "named type (0x%x) ", type);
      bfd_sym_fetch_long (buf, len, offset, &offset, &value);
      if (value <= 0)
	fprintf (f, "[INVALID]");
      else
	fprintf (f, "\"%.*s\"",
		 bfd_sym_symbol_name (abfd, value)[0],
		 &bfd_sym_symbol_name (abfd, value)[1]);

      fprintf (f, " (NTE %lu) with type ", (unsigned long) value);
      bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
      break;
    }

  default:
    fprintf (f, "%s (0x%x)", bfd_sym_type_operator_name (type), type);
    break;
    }

  if (type == (0x40 | 0x6))
    {
      /* Vector.  */
      long n, width, m;
      long l;
      long i;

      bfd_sym_fetch_long (buf, len, offset, &offset, &n);
      bfd_sym_fetch_long (buf, len, offset, &offset, &width);
      bfd_sym_fetch_long (buf, len, offset, &offset, &m);
      /* fprintf (f, "\n                "); */
      fprintf (f, " N %ld, width %ld, M %ld, ", n, width, m);
      for (i = 0; i < m; i++)
	{
	  bfd_sym_fetch_long (buf, len, offset, &offset, &l);
	  if (i != 0)
	    fprintf (f, " ");
	  fprintf (f, "%ld", l);
	}
    }
  else  if (type & 0x40)
    {
      /* Other packed type.  */
      long msb, lsb;

      bfd_sym_fetch_long (buf, len, offset, &offset, &msb);
      bfd_sym_fetch_long (buf, len, offset, &offset, &lsb);
      /* fprintf (f, "\n                "); */
      fprintf (f, " msb %ld, lsb %ld", msb, lsb);
    }

  fprintf (f, "]");

  if (offsetptr != NULL)
    *offsetptr = offset;
}

void
bfd_sym_print_type_information_table_entry (bfd *abfd,
					    FILE *f,
					    bfd_sym_type_information_table_entry *entry)
{
  unsigned char *buf;
  unsigned long offset;
  unsigned int i;

  fprintf (f, "\"%.*s\" (NTE %lu), %lu bytes at %lu, logical size %lu",
	   bfd_sym_symbol_name (abfd, entry->nte_index)[0],
	   &bfd_sym_symbol_name (abfd, entry->nte_index)[1],
	   entry->nte_index,
	   entry->physical_size, entry->offset, entry->logical_size);

  fprintf (f, "\n            ");

  buf = malloc (entry->physical_size);
  if (buf == NULL)
    {
      fprintf (f, "[ERROR]\n");
      return;
    }
  if (bfd_seek (abfd, entry->offset, SEEK_SET) < 0)
    {
      fprintf (f, "[ERROR]\n");
      free (buf);
      return;
    }
  if (bfd_bread (buf, entry->physical_size, abfd) != entry->physical_size)
    {
      fprintf (f, "[ERROR]\n");
      free (buf);
      return;
    }

  fprintf (f, "[");
  for (i = 0; i < entry->physical_size; i++)
    {
      if (i == 0)
	fprintf (f, "0x%02x", buf[i]);
      else
	fprintf (f, " 0x%02x", buf[i]);
    }

  fprintf (f, "]");
  fprintf (f, "\n            ");

  bfd_sym_print_type_information (abfd, f, buf, entry->physical_size, 0, &offset);

  if (offset != entry->physical_size)
    fprintf (f, "\n            [parser used %lu bytes instead of %lu]", offset, entry->physical_size);
  free (buf);
}

void
bfd_sym_print_file_references_index_table_entry (bfd *abfd ATTRIBUTE_UNUSED,
						 FILE *f,
						 bfd_sym_file_references_index_table_entry *entry ATTRIBUTE_UNUSED)
{
  fprintf (f, "[UNIMPLEMENTED]");
}

void
bfd_sym_print_constant_pool_entry (bfd *abfd ATTRIBUTE_UNUSED,
				   FILE *f,
				   bfd_sym_constant_pool_entry *entry ATTRIBUTE_UNUSED)
{
  fprintf (f, "[UNIMPLEMENTED]");
}

unsigned char *
bfd_sym_display_name_table_entry (bfd *abfd,
				  FILE *f,
				  unsigned char *entry)
{
  unsigned long sym_index;
  unsigned long offset;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;
  sym_index = (entry - sdata->name_table) / 2;

  if (sdata->version >= BFD_SYM_VERSION_3_4 && entry[0] == 255 && entry[1] == 0)
    {
      unsigned short length = bfd_getb16 (entry + 2);
      fprintf (f, "[%8lu] \"%.*s\"\n", sym_index, length, entry + 4);
      offset = 2 + length + 1;
    }
  else
    {
      if (! (entry[0] == 0 || (entry[0] == 1 && entry[1] == '\0')))
	fprintf (f, "[%8lu] \"%.*s\"\n", sym_index, entry[0], entry + 1);

      if (sdata->version >= BFD_SYM_VERSION_3_4)
	offset = entry[0] + 2;
      else
	offset = entry[0] + 1;
    }

  return (entry + offset + (offset % 2));
}

void
bfd_sym_display_name_table (bfd *abfd, FILE *f)
{
  unsigned long name_table_len;
  unsigned char *name_table, *name_table_end, *cur;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  name_table_len = sdata->header.dshb_nte.dti_page_count * sdata->header.dshb_page_size;
  name_table = sdata->name_table;
  name_table_end = name_table + name_table_len;

  fprintf (f, "name table (NTE) contains %lu bytes:\n\n", name_table_len);

  cur = name_table;
  for (;;)
    {
      cur = bfd_sym_display_name_table_entry (abfd, f, cur);
      if (cur >= name_table_end)
	break;
    }
}

void
bfd_sym_display_resources_table (bfd *abfd, FILE *f)
{
  unsigned long i;
  bfd_sym_resources_table_entry entry;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  fprintf (f, "resource table (RTE) contains %lu objects:\n\n",
	   sdata->header.dshb_rte.dti_object_count);

  for (i = 1; i <= sdata->header.dshb_rte.dti_object_count; i++)
    {
      if (bfd_sym_fetch_resources_table_entry (abfd, &entry, i) < 0)
	fprintf (f, " [%8lu] [INVALID]\n", i);
      else
	{
	  fprintf (f, " [%8lu] ", i);
	  bfd_sym_print_resources_table_entry (abfd, f, &entry);
	  fprintf (f, "\n");
	}
    }
}

void
bfd_sym_display_modules_table (bfd *abfd, FILE *f)
{
  unsigned long i;
  bfd_sym_modules_table_entry entry;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  fprintf (f, "module table (MTE) contains %lu objects:\n\n",
	   sdata->header.dshb_mte.dti_object_count);

  for (i = 1; i <= sdata->header.dshb_mte.dti_object_count; i++)
    {
      if (bfd_sym_fetch_modules_table_entry (abfd, &entry, i) < 0)
	fprintf (f, " [%8lu] [INVALID]\n", i);
      else
	{
	  fprintf (f, " [%8lu] ", i);
	  bfd_sym_print_modules_table_entry (abfd, f, &entry);
	  fprintf (f, "\n");
	}
    }
}

void
bfd_sym_display_file_references_table (bfd *abfd, FILE *f)
{
  unsigned long i;
  bfd_sym_file_references_table_entry entry;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  fprintf (f, "file reference table (FRTE) contains %lu objects:\n\n",
	   sdata->header.dshb_frte.dti_object_count);

  for (i = 1; i <= sdata->header.dshb_frte.dti_object_count; i++)
    {
      if (bfd_sym_fetch_file_references_table_entry (abfd, &entry, i) < 0)
	fprintf (f, " [%8lu] [INVALID]\n", i);
      else
	{
	  fprintf (f, " [%8lu] ", i);
	  bfd_sym_print_file_references_table_entry (abfd, f, &entry);
	  fprintf (f, "\n");
	}
    }
}

void
bfd_sym_display_contained_modules_table (bfd *abfd, FILE *f)
{
  unsigned long i;
  bfd_sym_contained_modules_table_entry entry;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  fprintf (f, "contained modules table (CMTE) contains %lu objects:\n\n",
	   sdata->header.dshb_cmte.dti_object_count);

  for (i = 1; i <= sdata->header.dshb_cmte.dti_object_count; i++)
    {
      if (bfd_sym_fetch_contained_modules_table_entry (abfd, &entry, i) < 0)
	fprintf (f, " [%8lu] [INVALID]\n", i);
      else
	{
	  fprintf (f, " [%8lu] ", i);
	  bfd_sym_print_contained_modules_table_entry (abfd, f, &entry);
	  fprintf (f, "\n");
	}
    }
}

void
bfd_sym_display_contained_variables_table (bfd *abfd, FILE *f)
{
  unsigned long i;
  bfd_sym_contained_variables_table_entry entry;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  fprintf (f, "contained variables table (CVTE) contains %lu objects:\n\n",
	   sdata->header.dshb_cvte.dti_object_count);

  for (i = 1; i <= sdata->header.dshb_cvte.dti_object_count; i++)
    {
      if (bfd_sym_fetch_contained_variables_table_entry (abfd, &entry, i) < 0)
	fprintf (f, " [%8lu] [INVALID]\n", i);
      else
	{
	  fprintf (f, " [%8lu] ", i);
	  bfd_sym_print_contained_variables_table_entry (abfd, f, &entry);
	  fprintf (f, "\n");
	}
    }

  fprintf (f, "\n");
}

void
bfd_sym_display_contained_statements_table (bfd *abfd, FILE *f)
{
  unsigned long i;
  bfd_sym_contained_statements_table_entry entry;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  fprintf (f, "contained statements table (CSNTE) contains %lu objects:\n\n",
	   sdata->header.dshb_csnte.dti_object_count);

  for (i = 1; i <= sdata->header.dshb_csnte.dti_object_count; i++)
    {
      if (bfd_sym_fetch_contained_statements_table_entry (abfd, &entry, i) < 0)
	fprintf (f, " [%8lu] [INVALID]\n", i);
      else
	{
	  fprintf (f, " [%8lu] ", i);
	  bfd_sym_print_contained_statements_table_entry (abfd, f, &entry);
	  fprintf (f, "\n");
	}
    }
}

void
bfd_sym_display_contained_labels_table (bfd *abfd, FILE *f)
{
  unsigned long i;
  bfd_sym_contained_labels_table_entry entry;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  fprintf (f, "contained labels table (CLTE) contains %lu objects:\n\n",
	   sdata->header.dshb_clte.dti_object_count);

  for (i = 1; i <= sdata->header.dshb_clte.dti_object_count; i++)
    {
      if (bfd_sym_fetch_contained_labels_table_entry (abfd, &entry, i) < 0)
	fprintf (f, " [%8lu] [INVALID]\n", i);
      else
	{
	  fprintf (f, " [%8lu] ", i);
	  bfd_sym_print_contained_labels_table_entry (abfd, f, &entry);
	  fprintf (f, "\n");
	}
    }
}

void
bfd_sym_display_contained_types_table (bfd *abfd, FILE *f)
{
  unsigned long i;
  bfd_sym_contained_types_table_entry entry;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  fprintf (f, "contained types table (CTTE) contains %lu objects:\n\n",
	   sdata->header.dshb_ctte.dti_object_count);

  for (i = 1; i <= sdata->header.dshb_ctte.dti_object_count; i++)
    {
      if (bfd_sym_fetch_contained_types_table_entry (abfd, &entry, i) < 0)
	fprintf (f, " [%8lu] [INVALID]\n", i);
      else
	{
	  fprintf (f, " [%8lu] ", i);
	  bfd_sym_print_contained_types_table_entry (abfd, f, &entry);
	  fprintf (f, "\n");
	}
    }
}

void
bfd_sym_display_file_references_index_table (bfd *abfd, FILE *f)
{
  unsigned long i;
  bfd_sym_file_references_index_table_entry entry;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  fprintf (f, "file references index table (FITE) contains %lu objects:\n\n",
	   sdata->header.dshb_fite.dti_object_count);

  for (i = 1; i <= sdata->header.dshb_fite.dti_object_count; i++)
    {
      if (bfd_sym_fetch_file_references_index_table_entry (abfd, &entry, i) < 0)
	fprintf (f, " [%8lu] [INVALID]\n", i);
      else
	{
	  fprintf (f, " [%8lu] ", i);
	  bfd_sym_print_file_references_index_table_entry (abfd, f, &entry);
	  fprintf (f, "\n");
	}
    }
}

void
bfd_sym_display_constant_pool (bfd *abfd, FILE *f)
{
  unsigned long i;
  bfd_sym_constant_pool_entry entry;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  fprintf (f, "constant pool (CONST) contains %lu objects:\n\n",
	   sdata->header.dshb_const.dti_object_count);

  for (i = 1; i <= sdata->header.dshb_const.dti_object_count; i++)
    {
      if (bfd_sym_fetch_constant_pool_entry (abfd, &entry, i) < 0)
	fprintf (f, " [%8lu] [INVALID]\n", i);
      else
	{
	  fprintf (f, " [%8lu] ", i);
	  bfd_sym_print_constant_pool_entry (abfd, f, &entry);
	  fprintf (f, "\n");
	}
    }
}

void
bfd_sym_display_type_information_table (bfd *abfd, FILE *f)
{
  unsigned long i;
  bfd_sym_type_table_entry sym_index;
  bfd_sym_type_information_table_entry entry;
  bfd_sym_data_struct *sdata = NULL;

  BFD_ASSERT (bfd_sym_valid (abfd));
  sdata = abfd->tdata.sym_data;

  if (sdata->header.dshb_tte.dti_object_count > 99)
    fprintf (f, "type table (TINFO) contains %lu objects:\n\n",
	     sdata->header.dshb_tte.dti_object_count - 99);
  else
    {
      fprintf (f, "type table (TINFO) contains [INVALID] objects:\n\n");
      return;
    }

  for (i = 100; i <= sdata->header.dshb_tte.dti_object_count; i++)
    {
      if (bfd_sym_fetch_type_table_entry (abfd, &sym_index, i - 100) < 0)
	fprintf (f, " [%8lu] [INVALID]\n", i);
      else
	{
	  fprintf (f, " [%8lu] (TINFO %lu) ", i, sym_index);

	  if (bfd_sym_fetch_type_information_table_entry (abfd, &entry, sym_index) < 0)
	    fprintf (f, "[INVALID]");
	  else
	    bfd_sym_print_type_information_table_entry (abfd, f, &entry);

	  fprintf (f, "\n");
	}
    }
}

int
bfd_sym_scan (bfd *abfd, bfd_sym_version version, bfd_sym_data_struct *mdata)
{
  asection *bfdsec;
  const char *name = "symbols";

  mdata->name_table = 0;
  mdata->sbfd = abfd;
  mdata->version = version;

  bfd_seek (abfd, 0, SEEK_SET);
  if (bfd_sym_read_header (abfd, &mdata->header, mdata->version) != 0)
    return -1;

  mdata->name_table = bfd_sym_read_name_table (abfd, &mdata->header);
  if (mdata->name_table == NULL)
    return -1;

  bfdsec = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
  if (bfdsec == NULL)
    return -1;

  bfdsec->vma = 0;
  bfdsec->lma = 0;
  bfdsec->size = 0;
  bfdsec->filepos = 0;
  bfdsec->alignment_power = 0;

  abfd->tdata.sym_data = mdata;

  return 0;
}

const bfd_target *
bfd_sym_object_p (bfd *abfd)
{
  bfd_sym_version version = -1;
  bfd_sym_data_struct *mdata;

  bfd_seek (abfd, 0, SEEK_SET);
  if (bfd_sym_read_version (abfd, &version) != 0)
    goto wrong;

  mdata = (bfd_sym_data_struct *) bfd_alloc (abfd, sizeof (*mdata));
  if (mdata == NULL)
    goto fail;

  if (bfd_sym_scan (abfd, version, mdata) != 0)
    goto wrong;

  return abfd->xvec;

 wrong:
  bfd_set_error (bfd_error_wrong_format);

 fail:
  return NULL;
}

#define bfd_sym_make_empty_symbol _bfd_generic_make_empty_symbol

void
bfd_sym_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, asymbol *symbol, symbol_info *ret)
{
  bfd_symbol_info (symbol, ret);
}

long
bfd_sym_get_symtab_upper_bound (bfd *abfd ATTRIBUTE_UNUSED)
{
  return 0;
}

long
bfd_sym_canonicalize_symtab (bfd *abfd ATTRIBUTE_UNUSED, asymbol **sym ATTRIBUTE_UNUSED)
{
  return 0;
}

int
bfd_sym_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
			struct bfd_link_info *info ATTRIBUTE_UNUSED)
{
  return 0;
}

const bfd_target sym_vec =
{
  "sym",			/* Name.  */
  bfd_target_sym_flavour,	/* Flavour.  */
  BFD_ENDIAN_BIG,		/* Byteorder.  */
  BFD_ENDIAN_BIG,		/* Header byteorder.  */
  (HAS_RELOC | EXEC_P |		/* Object flags.  */
   HAS_LINENO | HAS_DEBUG |
   HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
  (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
   | SEC_ROM | SEC_HAS_CONTENTS), /* Section_flags.  */
  0,				/* Symbol_leading_char.  */
  ' ',				/* AR_pad_char.  */
  16,				/* AR_max_namelen.  */
  0,				/* match priority.  */
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  bfd_getb16, bfd_getb_signed_16, bfd_putb16,	/* Data.  */
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  bfd_getb16, bfd_getb_signed_16, bfd_putb16,	/* Hdrs.  */
  {				/* bfd_check_format.  */
    _bfd_dummy_target,
    bfd_sym_object_p,		/* bfd_check_format.  */
    _bfd_dummy_target,
    _bfd_dummy_target,
  },
  {				/* bfd_set_format.  */
    bfd_false,
    bfd_sym_mkobject,
    bfd_false,
    bfd_false,
  },
  {				/* bfd_write_contents.  */
    bfd_false,
    bfd_true,
    bfd_false,
    bfd_false,
  },

  BFD_JUMP_TABLE_GENERIC (bfd_sym),
  BFD_JUMP_TABLE_COPY (_bfd_generic),
  BFD_JUMP_TABLE_CORE (_bfd_nocore),
  BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  BFD_JUMP_TABLE_SYMBOLS (bfd_sym),
  BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  BFD_JUMP_TABLE_WRITE (bfd_sym),
  BFD_JUMP_TABLE_LINK (bfd_sym),
  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),

  NULL,

  NULL
};