|
root / base / usr / src / cmd / sgs / dump / common
common Plain Text 2360 lines 58.3 KB
   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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 *	Copyright (c) 1988 AT&T
 *	  All Rights Reserved
 *
 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2018, Joyent, Inc.
 */

/* Get definitions for the relocation types supported. */
#define	ELF_TARGET_ALL

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <libelf.h>
#include <sys/link.h>
#include <sys/elf.h>
#include <sys/machelf.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include "sgs.h"
#include "conv.h"
#include "dump.h"


#define	OPTSTR	"agcd:fhn:oprstvCLT:V?"		/* option string for getopt() */

const char *UNKNOWN = "<unknown>";

static SCNTAB *p_symtab, *p_head_scns, *p_dynsym;

static int
	x_flag = 0,	/* option requires section header table */
	z_flag = 0,	/* process files within an archive */
	rn_flag = 0;	/* dump named relocation information */

static int
	/* flags: ?_flag corresponds to ? option */
	a_flag = 0,	/* dump archive header of each member of archive */
	g_flag = 0,	/* dump archive symbol table */
	c_flag = 0,	/* dump the string table */
	d_flag = 0,	/* dump range of sections */
	f_flag = 0,	/* dump each file header */
	h_flag = 0,	/* dump section headers */
	n_flag = 0,	/* dump named section */
	o_flag = 0,	/* dump each program execution header */
	r_flag = 0,	/* dump relocation information */
	s_flag = 0,	/* dump section contents */
	t_flag = 0,	/* dump symbol table entries */
	C_flag = 0,	/* dump decoded C++ symbol names */
	L_flag = 0,	/* dump dynamic linking information */
	T_flag = 0,	/* dump symbol table range */
	V_flag = 0;	/* dump version information */

int	p_flag = 0,	/* suppress printing of headings */
	v_flag = 0;	/* print information in verbose form */

static int
	d_low = 0,	/* range for use with -d */
	d_hi = 0,
	d_num = 0;

static int
	T_low = 0,	/* range for use with -T */
	T_hi = 0,
	T_num = 0;

static char *name = NULL; /* for use with -n option */
char *prog_name;
static int errflag = 0;

static struct stab_list_s {
	struct stab_list_s *next;
	char *strings;
	size_t size;
} *StringTableList = (void *)0;

extern void ar_sym_read();
extern void dump_exec_header();


/*
 * Get the section descriptor and set the size of the
 * data returned.  Data is byte-order converted.
 */
void *
get_scndata(Elf_Scn *fd_scn, size_t *size)
{
	Elf_Data *p_data;

	p_data = 0;
	if ((p_data = elf_getdata(fd_scn, p_data)) == 0 ||
	    p_data->d_size == 0) {
		return (NULL);
	}
	*size = p_data->d_size;
	return (p_data->d_buf);
}

/*
 * Get the section descriptor and set the size of the
 * data returned.  Data is raw (i.e., not byte-order converted).
 */
static void *
get_rawscn(Elf_Scn *fd_scn, size_t *size)
{
	Elf_Data *p_data;

	p_data = 0;
	if ((p_data = elf_rawdata(fd_scn, p_data)) == 0 ||
	    p_data->d_size == 0) {
		return (NULL);
	}

	*size = p_data->d_size;
	return (p_data->d_buf);
}

/*
 * Print out a usage message in short form when program is invoked
 * with insufficient or no arguments, and in long form when given
 * either a ? or an invalid option.
 */
static void
usage()
{
	(void) fprintf(stderr,
	"Usage: %s [-%s] file(s) ...\n", prog_name, OPTSTR);
	if (errflag) {
		(void) fprintf(stderr,
		"\t\t[-a dump archive header of each member of archive]\n\
		[-g dump archive global symbol table]\n\
		[-c dump the string table]\n\
		[-d dump range of sections]\n\
		[-f dump each file header]\n\
		[-h dump section headers]\n\
		[-n dump named section]\n\
		[-o dump each program execution header]\n\
		[-p suppress printing of headings]\n\
		[-r dump relocation information]\n\
		[-s dump section contents]\n\
		[-t dump symbol table entries]\n\
		[-v print information in verbose form]\n\
		[-C dump decoded C++ symbol names]\n\
		[-L dump the .dynamic structure]\n\
		[-T dump symbol table range]\n\
		[-V dump version information]\n");
	}
}

/*
 * Set a range.  Input is a character string, a lower
 * bound and an upper bound.  This function converts
 * a character string into its correct integer values,
 * setting the first value as the lower bound, and
 * the second value as the upper bound.  If more values
 * are given they are ignored with a warning.
 */
static void
set_range(char *s, int  *low, int  *high)
{
	char *w;
	char *lasts;

	while ((w = strtok_r(s, ",", &lasts)) != NULL) {
		if (!(*low))
			/* LINTED */
			*low = (int)atol(w);
		else
			if (!(*high))
				/* LINTED */
				*high = (int)atol(w);
			else {
				(void) fprintf(stderr,
				    "%s: too many arguments - %s ignored\n",
				    prog_name, w);
				return;
			}
		s = NULL;
	} /* end while */
}


/*
 * Print static shared library information.
 */
static void
print_static(SCNTAB *l_scns, char *filename)
{
	size_t section_size;
	unsigned char *strtab;
	unsigned char *path, buf[1024];
	unsigned long *temp;
	unsigned long total, topath;

	(void) printf("\n  **** STATIC SHARED LIBRARY INFORMATION ****\n");
	(void) printf("\n%s:\n", filename);
	(void) printf("\t");
	section_size  = 0;
	if ((strtab = (unsigned char *)
	    get_scndata(l_scns->p_sd, &section_size)) == NULL) {
		return;
	}

	while (section_size != 0) {
		/* LINTED */
		temp = (unsigned long *)strtab;
		total = temp[0];
		topath = temp[1];
		path = strtab + (topath*sizeof (long));
		(void) strncpy((char *)buf, (char *)path,
		    (total - topath)*sizeof (long));
		(void) fprintf(stdout, "%s\n", buf);
		strtab += total*sizeof (long);
		section_size -= (total*sizeof (long));
	}
}

/*
 * Print raw data in hexidecimal.  Input is the section data to
 * be printed out and the size of the data.  Output is relative
 * to a table lookup in dumpmap.h.
 */
static void
print_rawdata(unsigned char *p_sec, size_t size)
{
	size_t   j;
	size_t   count;

	count = 1;

	(void) printf("\t");
	for (j = size/sizeof (short); j != 0; --j, ++count) {
		(void) printf("%.2x %.2x ", p_sec[0], p_sec[1]);
		p_sec += 2;
		if (count == 12) {
			(void) printf("\n\t");
			count = 0;
		}
	}

	/*
	 * take care of last byte if odd byte section
	 */
	if ((size & 0x1L) == 1L)
		(void) printf("%.2x", *p_sec);
	(void) printf("\n");
}



/*
 * Print relocation data of type SHT_RELA
 * If d_flag, print data corresponding only to
 * the section or range of sections specified.
 * If n_flag, print data corresponding only to
 * the named section.
 */
static void
print_rela(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
    GElf_Ehdr * p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
    SCNTAB *reloc_symtab)
{
	GElf_Rela rela;
	GElf_Sym sym;
	size_t no_entries;
	size_t rel_entsize;
	size_t no_syms;
	int type, symid;
	static int n_title = 0;
	int ndx = 0;
	char *sym_name;
	int adj = 0;

	if (gelf_getclass(elf_file) == ELFCLASS64)
		adj = 8;

	rel_entsize = p_scns->p_shdr.sh_entsize;
	if ((rel_entsize == 0) ||
	    (rel_entsize > p_scns->p_shdr.sh_size)) {
		rel_entsize = gelf_fsize(elf_file, ELF_T_RELA, 1,
		    EV_CURRENT);
	}
	no_entries = reloc_size / rel_entsize;

	no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
	while (no_entries--) {
		(void) gelf_getrela(rdata, ndx, &rela);
		/* LINTED */
		type = (int)GELF_R_TYPE(rela.r_info);
		/* LINTED */
		symid = (int)GELF_R_SYM(rela.r_info);
		/* LINTED */
		if ((symid > (no_syms - 1)) || (symid < 0)) {
			(void) fprintf(stderr, "%s: %s: invalid symbol table "
			    "offset - %d - in %s\n", prog_name, filename,
			    symid, p_scns->scn_name);
			ndx++;
			continue;
		}
		(void) gelf_getsym(sym_data, symid, &sym);
		sym_name = (char *)elf_strptr(elf_file,
		    reloc_symtab->p_shdr.sh_link, sym.st_name);
		if (sym_name == NULL)
			sym_name = (char *)UNKNOWN;
		if (r_flag && rn_flag) {
			if (strcmp(name, p_scns->scn_name) != 0) {
				ndx++;
				continue;
			}
			if (!n_title) {
				(void) printf("\n%s:\n", p_scns->scn_name);
				(void) printf("%-*s%-*s%-*s%s\n\n",
				    12 + adj, "Offset", 22, "Symndx",
				    16, "Type", "Addend");
				n_title = 1;
			}
		}
		if (d_flag) {
			if (!d_hi)
				d_hi = d_low;
			if ((symid < d_low) || (symid > d_hi)) {
				ndx++;
				continue;
			}
		}

		(void) printf("%-#*llx", 12 + adj, EC_XWORD(rela.r_offset));
		if (!v_flag) {
			(void) printf("%-22d%-18d", symid, type);
		} else {
			Conv_inv_buf_t	inv_buf;

			if (strlen(sym_name)) {
				size_t len = strlen(sym_name) + 1;
				char tmpstr[10];
				if (len > 22) {
					(void) sprintf(tmpstr, "%%-%ds",
					    /* LINTED */
					    (int)len);
					/*LINTED: E_SEC_PRINTF_VAR_FMT*/
					(void) printf(tmpstr, sym_name);
				} else
					(void) printf("%-22s", sym_name);
			} else {
				(void) printf("%-22d", symid);
			}
			(void) printf("%-20s",
			    conv_reloc_type(p_ehdr->e_machine,
			    type, DUMP_CONVFMT, &inv_buf));
		}
		(void) printf("%lld\n", EC_SXWORD(rela.r_addend));
		ndx++;
	}
}

/*
 * Print relocation data of type SHT_REL.
 * If d_flag, print data corresponding only to
 * the section or range of sections specified.
 * If n_flag, print data corresponding only to
 * the named section.
 */
static void
print_rel(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
    GElf_Ehdr *p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
    SCNTAB *reloc_symtab)
{
	GElf_Rel rel;
	GElf_Sym sym;
	size_t no_entries;
	size_t rel_entsize;
	int type, symid;
	size_t no_syms;
	static int n_title = 0;
	int ndx = 0;
	char *sym_name;
	int adj = 0;

	if (gelf_getclass(elf_file) == ELFCLASS64)
		adj = 8;

	rel_entsize = p_scns->p_shdr.sh_entsize;
	if ((rel_entsize == 0) ||
	    (rel_entsize > p_scns->p_shdr.sh_size)) {
		rel_entsize = gelf_fsize(elf_file, ELF_T_REL, 1,
		    EV_CURRENT);
	}
	no_entries = reloc_size / rel_entsize;

	no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
	while (no_entries--) {
		(void) gelf_getrel(rdata, ndx, &rel);
		/* LINTED */
		type = (int)GELF_R_TYPE(rel.r_info);
		/* LINTED */
		symid = (int)GELF_R_SYM(rel.r_info);
		/* LINTED */
		if ((symid > (no_syms - 1)) || (symid < 0)) {
			(void) fprintf(stderr, "%s: %s: invalid symbol table "
			    "offset - %d - in %s\n", prog_name, filename,
			    symid, p_scns->scn_name);
			ndx++;
			continue;
		}
		(void) gelf_getsym(sym_data, symid, &sym);
		sym_name = (char *)elf_strptr(elf_file,
		    reloc_symtab->p_shdr.sh_link, sym.st_name);
		if (sym_name == NULL)
			sym_name = (char *)UNKNOWN;
		if (r_flag && rn_flag) {
			if (strcmp(name, p_scns->scn_name) != 0) {
				ndx++;
				continue;
			}
			if (!n_title) {
				(void) printf("\n%s:\n", p_scns->scn_name);
				(void) printf("%-*s%-*s%s\n\n",
				    12 + adj, "Offset", 20, "Symndx", "Type");
				n_title = 1;
			}
		}
		if (d_flag) {
			if (!d_hi)
				d_hi = d_low;
			if ((symid < d_low) || (symid > d_hi)) {
				ndx++;
				continue;
			}
		}

		(void) printf("%-#*llx", 12 + adj, EC_ADDR(rel.r_offset));
		if (!v_flag) {
			(void) printf("%-20d%-18d", symid, type);
		} else {
			Conv_inv_buf_t	inv_buf;

			if (strlen(sym_name))
				(void) printf("%-20s", sym_name);
			else {
				(void) printf("%-20d", sym.st_name);
			}
			(void) printf("%-20s",
			    conv_reloc_type(p_ehdr->e_machine,
			    type, DUMP_CONVFMT, &inv_buf));
		}
		(void) printf("\n");
		ndx++;
	}
}

/* demangle C++ names */
static char *
demangled_name(char *s)
{
	static char	*buf = NULL;
	size_t		buflen = 0;
	char		*dn;
	size_t		len;

	dn = (char *)conv_demangle_name(s);

	/*
	 * If not demangled, just return the symbol name
	 */
	if (dn == s)
		return (s);

	len = strlen(dn) + strlen(s) + 4;

	if (buflen < len) {
		free(buf);
		if ((buf = malloc(len)) == NULL)
			return (s);
		buflen = len;
	}

	/*
	 * Demangled. Format it
	 */
	(void) snprintf(buf, buflen, "%s\t[%s]", dn, s);
	free(dn);
	return (buf);
}

/*
 * Print the symbol table.  Input is an ELF file descriptor, a
 * pointer to the symbol table SCNTAB structure,
 * the number of symbols, a range of symbols to print,
 * an index which is the number of the
 * section in the file, and the filename.  The number of sections,
 * the range, and the index are set in
 * dump_symbol_table, depending on whether -n or -T were set.
 */
static void
print_symtab(Elf *elf_file, SCNTAB *p_symtab, Elf_Data *sym_data,
    long range, int index)
{
	GElf_Sym sym;
	int adj = 0;		/* field adjustment for elf64 */
	Elf32_Word	*symshndx = 0;
	unsigned int	nosymshndx = 0;
	Conv_inv_buf_t	inv_buf;


	if (gelf_getclass(elf_file) == ELFCLASS64)
		adj = 8;

	while (range > 0) {
		char		*sym_name = NULL;
		int		type, bind;
		int		specsec;
		unsigned int	shndx;

		(void) gelf_getsym(sym_data, index, &sym);
		type = (int)GELF_ST_TYPE(sym.st_info);
		bind = (int)GELF_ST_BIND(sym.st_info);

		if ((sym.st_shndx == SHN_XINDEX) &&
		    (symshndx == 0) && (nosymshndx == 0)) {
			Elf_Scn		*_scn;
			GElf_Shdr	_shdr;
			size_t		symscnndx;

			symscnndx = elf_ndxscn(p_symtab->p_sd);
			_scn = 0;
			while ((_scn = elf_nextscn(elf_file, _scn)) != 0) {
				if (gelf_getshdr(_scn, &_shdr) == 0)
					break;
				if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) &&
				    /* LINTED */
				    (_shdr.sh_link == (GElf_Word)symscnndx)) {
					Elf_Data	*_data;

					if ((_data = elf_getdata(_scn, 0)) == 0)
						continue;

					symshndx = (Elf32_Word *)_data->d_buf;
					nosymshndx = 0;
					break;
				}
			}
			nosymshndx = 1;
		}

		if ((symshndx) && (sym.st_shndx == SHN_XINDEX)) {
			shndx = symshndx[index];
			specsec = 0;
		} else {
			shndx = sym.st_shndx;
			if ((sym.st_shndx == SHN_UNDEF) ||
			    (sym.st_shndx >= SHN_LORESERVE))
				specsec = 1;
			else
				specsec = 0;
		}


		(void) printf("[%d]\t ", index++);

		if (v_flag && (type == STT_SPARC_REGISTER)) {
			/*
			 *  The strings "REG_G1" through "REG_G7" are intended
			 *  to be consistent with output from elfdump(1).
			 */
			(void) printf("%-*s", 12 + adj,
			    conv_sym_SPARC_value(sym.st_value,
			    DUMP_CONVFMT, &inv_buf));
		} else {
			(void) printf("0x%-*llx", 10 + adj,
			    EC_ADDR(sym.st_value));
		}

		(void) printf("%-*lld", 9 + adj, EC_XWORD(sym.st_size));

		if (!v_flag) {
			(void) printf("%d\t\t%d\t%d\t%#x\t",
			    type, bind, (int)sym.st_other, (int)shndx);
		} else {
			GElf_Ehdr p_ehdr;
			(void) gelf_getehdr(elf_file, &p_ehdr);
			(void) printf("%s\t",
			    conv_sym_info_type(p_ehdr.e_machine, type,
			    DUMP_CONVFMT, &inv_buf));
			(void) printf("%s",
			    conv_sym_info_bind(bind, DUMP_CONVFMT, &inv_buf));
			(void) printf("\t  %d\t", EC_WORD(sym.st_other));

			if (specsec)
				(void) printf("%s",
				    conv_sym_shndx(p_ehdr.e_ident[EI_OSABI],
				    p_ehdr.e_machine, shndx,
				    CONV_FMT_DECIMAL, &inv_buf));
			else
				(void) printf("%d", EC_WORD(shndx));
			(void) printf("\t");
		}

		/* support machines where NULL-deref causes core dump */
		if (sym.st_name == 0)
			sym_name = (char *)UNKNOWN;
		else
			if (C_flag)
				sym_name = demangled_name(
				    (char *)elf_strptr(elf_file,
				    p_symtab->p_shdr.sh_link,
				    sym.st_name));
		else
			sym_name = (char *)elf_strptr(elf_file,
			    p_symtab->p_shdr.sh_link, sym.st_name);
		if (sym_name == NULL)
			sym_name = (char *)UNKNOWN;
		(void) printf("%s\n", sym_name);

		range--;
	}	/* end while */
}

/*
 * Print the section header table.  Input is the SCNTAB structure,
 * the number of sections, an index which is the number of the
 * section in the file, and the filename.  The values of the SCNTAB
 * structure, the number of sections, and the index are set in
 * dump_shdr depending on whether the -n or -d modifiers were set.
 */
static void
print_shdr(Elf *elf_file, SCNTAB *s, int num_scns, int index)
{
	SCNTAB *p;
	int num;
	int field;
	GElf_Ehdr p_ehdr;

	if (gelf_getclass(elf_file) == ELFCLASS64)
		field = 21;
	else
		field = 13;

	p = s;
	(void) gelf_getehdr(elf_file, &p_ehdr);

	for (num = 0; num < num_scns; num++, p++) {
		(void) printf("[%d]\t", index++);
		if (!v_flag) {
			(void) printf("%u\t%llu\t",
			    EC_WORD(p->p_shdr.sh_type),
			    EC_XWORD(p->p_shdr.sh_flags));
		} else {
			Conv_inv_buf_t inv_buf;

			/*LINTED: E_SEC_PRINTF_VAR_FMT*/
			(void) printf(conv_sec_type(
			    p_ehdr.e_ident[EI_OSABI], p_ehdr.e_machine,
			    p->p_shdr.sh_type, DUMP_CONVFMT, &inv_buf));
			(void) printf("    ");

			if (p->p_shdr.sh_flags & SHF_WRITE)
				(void) printf("W");
			else
				(void) printf("-");
			if (p->p_shdr.sh_flags & SHF_ALLOC)
				(void) printf("A");
			else
				(void) printf("-");
			if (p->p_shdr.sh_flags & SHF_EXECINSTR)
				(void) printf("I");
			else
				(void) printf("-");

			if (p->p_shdr.sh_flags & SHF_ORDERED)
				(void) printf("O");
			if (p->p_shdr.sh_flags & SHF_EXCLUDE)
				(void) printf("E");

			(void) printf("\t");

		}
		(void) printf("%-#*llx%-#*llx%-#*llx%s%s\n",
		    field, EC_ADDR(p->p_shdr.sh_addr),
		    field, EC_OFF(p->p_shdr.sh_offset),
		    field, EC_XWORD(p->p_shdr.sh_size),
		    /* compatibility:  tab for elf32 */
		    (field == 13) ? "\t" : " ", p->scn_name);

		(void) printf("\t%u\t%u\t%-#*llx%-#*llx\n\n",
		    EC_WORD(p->p_shdr.sh_link),
		    EC_WORD(p->p_shdr.sh_info),
		    field, EC_XWORD(p->p_shdr.sh_addralign),
		    field, EC_XWORD(p->p_shdr.sh_entsize));
	}
}

/*
 * Check that a range of numbers is valid.  Input is
 * a lower bound, an upper bound, a boundary condition,
 * and the filename.  Negative numbers and numbers greater
 * than the bound are invalid.  low must be smaller than hi.
 * The returned integer is the number of items in the
 * range if it is valid and -1 otherwise.
 */
static int
check_range(int low, int hi, size_t bound, char *filename)
{
	if (((size_t)low > bound) || (low <= 0)) {
		(void) fprintf(stderr,
		    "%s: %s: number out of range, %d\n",
		    prog_name, filename, low);
		return (-1);
	}
	if (((size_t)hi > bound) || (hi < 0)) {
		(void) fprintf(stderr,
		    "%s: %s: number out of range, %d\n",
		    prog_name, filename, hi);
		return (-1);
	}

	if (hi && (low > hi)) {
		(void) fprintf(stderr,
		    "%s: %s: invalid range, %d,%d\n",
		    prog_name, filename, low, hi);
		return (-1);
	}
	if (hi)
		return (hi - low + 1);
	else
		return (1);
}

/*
 * Print relocation information.  Since this information is
 * machine dependent, new sections must be added for each machine
 * that is supported.  Input is an ELF file descriptor, the ELF header,
 * the SCNTAB structure, the number of sections, and a filename.
 * Set up necessary information to print relocation information
 * and call the appropriate print function depending on the
 * type of relocation information.  If the symbol table is
 * absent, no relocation data is processed.  Input is an
 * ELF file descriptor, the ELF header, the SCNTAB structure,
 * and the filename.  Set range of d_flag and name if n_flag.
 */
static void
dump_reloc_table(Elf *elf_file, GElf_Ehdr *p_ehdr,
    SCNTAB *p_scns, int num_scns, char *filename)
{
	Elf_Data *rel_data;
	Elf_Data *sym_data;
	size_t    sym_size;
	size_t    reloc_size;
	SCNTAB *reloc_symtab;
	SCNTAB *head_scns;
	int r_title = 0;
	int adj = 0;
	size_t shnum;

	if (gelf_getclass(elf_file) == ELFCLASS64)
		adj = 8;

	if ((!p_flag) && (!r_title)) {
		(void) printf("\n    **** RELOCATION INFORMATION ****\n");
		r_title = 1;
	}

	while (num_scns-- > 0) {
		if ((p_scns->p_shdr.sh_type != SHT_RELA) &&
		    (p_scns->p_shdr.sh_type != SHT_REL)) {
			p_scns++;
			continue;
		}

	head_scns = p_head_scns;

	if (elf_getshdrnum(elf_file, &shnum) == -1) {
		(void) fprintf(stderr,
		    "%s: %s: elf_getshdrnum failed: %s\n",
		    prog_name, filename, elf_errmsg(-1));
		return;
	}

	if ((p_scns->p_shdr.sh_link == 0) ||
	    /* LINTED */
	    (p_scns->p_shdr.sh_link >= (GElf_Word)shnum)) {
		(void) fprintf(stderr, "%s: %s: invalid sh_link field: "
		    "section #: %d sh_link: %d\n",
		    /* LINTED */
		    prog_name, filename, (int)elf_ndxscn(p_scns->p_sd),
		    (int)p_scns->p_shdr.sh_link);
		return;
	}
	head_scns += (p_scns->p_shdr.sh_link -1);

	if (head_scns->p_shdr.sh_type == SHT_SYMTAB) {
		reloc_symtab = p_symtab;
	} else if (head_scns->p_shdr.sh_type  == SHT_DYNSYM) {
		reloc_symtab = p_dynsym;
	} else {
		(void) fprintf(stderr,
"%s: %s: could not get symbol table\n", prog_name, filename);
		return;
	}

	sym_data = NULL;
	sym_size = 0;
	reloc_size = 0;

	if ((sym_data = elf_getdata(reloc_symtab->p_sd, NULL)) == NULL) {
		(void) fprintf(stderr,
		"%s: %s: no symbol table data\n", prog_name, filename);
		return;
	}
	sym_size = sym_data->d_size;

	if (p_scns->p_shdr.sh_type == SHT_RELA) {
		if (!n_flag && r_flag)
			(void) printf("\n%s:\n", p_scns->scn_name);
		if (!p_flag && (!n_flag && r_flag))
			(void) printf("%-*s%-*s%-*s%s\n\n",
			    12 + adj, "Offset", 22, "Symndx",
			    18, "Type", "Addend");
		if ((rel_data = elf_getdata(p_scns->p_sd, NULL)) == NULL) {
			(void) fprintf(stderr,
"%s: %s: no relocation information\n", prog_name, filename);
			return;
		}
		reloc_size = rel_data->d_size;

		if (n_flag) {
			rn_flag = 1;
			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
			    reloc_size, sym_size, filename, reloc_symtab);
		}
		if (d_flag) {
			rn_flag = 0;
			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
			    reloc_size, sym_size, filename, reloc_symtab);
		}
		if (!n_flag && !d_flag)
			print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
			    reloc_size, sym_size, filename, reloc_symtab);
	} else {
		if (p_scns->p_shdr.sh_type == SHT_REL) {
			if (!n_flag && r_flag)
				(void) printf("\n%s:\n", p_scns->scn_name);
			if (!p_flag && (!n_flag && r_flag)) {
				(void) printf("%-*s%-*s%s\n\n",
				    12 + adj, "Offset", 20, "Symndx", "Type");
			}
			if ((rel_data = elf_getdata(p_scns->p_sd, NULL))
			    == NULL) {
				(void) fprintf(stderr,
"%s: %s: no relocation information\n", prog_name, filename);
				return;
			}
			reloc_size = rel_data->d_size;
			if (n_flag) {
				rn_flag = 1;
				print_rel(elf_file, p_scns, rel_data, sym_data,
				    p_ehdr, reloc_size, sym_size,
				    filename, reloc_symtab);
			}
			if (d_flag) {
				rn_flag = 0;
				print_rel(elf_file, p_scns, rel_data, sym_data,
				    p_ehdr, reloc_size, sym_size,
				    filename, reloc_symtab);
			}
			if (!n_flag && !d_flag)
				print_rel(elf_file, p_scns, rel_data, sym_data,
				    p_ehdr, reloc_size, sym_size,
				    filename, reloc_symtab);
		}
	}
	p_scns++;
	}
}

/*
 * Print out the string tables.  Input is an opened ELF file,
 * the SCNTAB structure, the number of sections, and the filename.
 * Since there can be more than one string table, all sections are
 * examined and any with the correct type are printed out.
 */
static void
dump_string_table(SCNTAB *s, int num_scns)
{
	size_t section_size;
	unsigned char *strtab;
	int beg_of_string;
	int counter = 0;
	int str_off;
	int i;

	if (!p_flag) {
		(void) printf("\n     **** STRING TABLE INFORMATION ****\n");
	}

	for (i = 0; i < num_scns; i++, s++) {
		if (s->p_shdr.sh_type != SHT_STRTAB)
			continue;

		str_off = 0;

		if (!p_flag) {
			(void) printf("\n%s:\n", s->scn_name);
			(void) printf("   <offset>  \tName\n");
		}
		section_size = 0;
		if ((strtab = (unsigned char *)
		    get_scndata(s->p_sd, &section_size)) == NULL) {
			continue;
		}

		if (section_size != 0) {
			(void) printf("   <%d>  \t", str_off);
			beg_of_string = 0;
			while (section_size--) {
				unsigned char c = *strtab++;

				if (beg_of_string) {
					(void) printf("   <%d>  \t", str_off);
					counter++;
					beg_of_string = 0;
				}
				str_off++;
				switch (c) {
				case '\0':
					(void) printf("\n");
					beg_of_string = 1;
					break;
				default:
					(void) putchar(c);
				}
			}
		}
	}
	(void) printf("\n");
}

/*
 * Print the symbol table.  This function does not print the contents
 * of the symbol table but sets up the parameters and then calls
 * print_symtab to print the symbols.  Calling another function to print
 * the symbols allows both -T and -n to work correctly
 * simultaneously.  Input is an opened ELF file, a pointer to the
 * symbol table SCNTAB structure, and the filename.
 * Set the range of symbols to print if T_flag, and set
 * name of symbol to print if n_flag.
 */
static void
dump_symbol_table(Elf *elf_file, SCNTAB *p_symtab, char *filename)
{
	Elf_Data  *sym_data;
	GElf_Sym  T_range, n_range;	/* for use with -T and -n */
	size_t count = 0;
	size_t sym_size;
	int index = 1;
	int found_it = 0;
	int i;
	int adj = 0;			/*  field adjustment for elf64 */

	if (gelf_getclass(elf_file) == ELFCLASS64)
		adj = 8;

	if (p_symtab == NULL) {
		(void) fprintf(stderr,
		"%s: %s: could not get symbol table\n", prog_name, filename);
		return;
	}

	/* get symbol table data */
	sym_data = NULL;
	sym_size = 0;
	if ((sym_data =
	    elf_getdata(p_symtab->p_sd, NULL)) == NULL) {
		(void) printf("\n%s:\n", p_symtab->scn_name);
		(void) printf("No symbol table data\n");
		return;
	}
	sym_size = sym_data->d_size;

	count = sym_size / p_symtab->p_shdr.sh_entsize;

	if (n_flag && t_flag && !T_flag) {
		/* LINTED */
		for (i = 1; i < count; i++) {
			(void) gelf_getsym(sym_data, i, &n_range);
			if (strcmp(name, (char *)
			    elf_strptr(elf_file,
			    p_symtab->p_shdr.sh_link,
			    n_range.st_name)) != 0) {
				continue;
			} else {
				found_it = 1;
				if (!p_flag) {
					(void) printf(
"\n              ***** SYMBOL TABLE INFORMATION *****\n");
					(void) printf(
"[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
					    12 + adj, "Value", 9 + adj, "Size");
				}
				(void) printf("\n%s:\n", p_symtab->scn_name);
				print_symtab(elf_file, p_symtab, sym_data,
				    1, i);
			}
		}   /* end for */
		if (!found_it) {
			(void) fprintf(stderr, "%s: %s: %s not found\n",
			    prog_name, filename, name);
		}
	} else if (T_flag) {
		T_num = check_range(T_low, T_hi, count, filename);
		if (T_num < 0)
			return;

		(void) gelf_getsym(sym_data, T_low-1, &T_range);
		index = T_low;

		if (!p_flag) {
			(void) printf(
"\n              ***** SYMBOL TABLE INFORMATION *****\n");
			(void) printf(
"[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
			    12 + adj, "Value", 9 + adj, "Size");
		}
		(void) printf("\n%s:\n", p_symtab->scn_name);
		print_symtab(elf_file, p_symtab, sym_data, T_num, index);
	} else {
		if (!p_flag) {
			(void) printf(
"\n              ***** SYMBOL TABLE INFORMATION *****\n");
			(void) printf(
"[Index]  %-*s%-*sType\tBind\tOther\tShndx\tName",
			    12 + adj, "Value", 9 + adj, "Size");
		}
		(void) printf("\n%s:\n", p_symtab->scn_name);
		print_symtab(elf_file, p_symtab, sym_data, count-1, 1);
	}
}


/*
 * Print dynamic linking information.  Input is an ELF
 * file descriptor, the SCNTAB structure, the number of
 * sections, and the filename.
 */
static void
dump_dynamic(Elf *elf_file, SCNTAB *p_scns, int num_scns, char *filename)
{
#define	pdyn_Fmtptr	"%#llx"

	Elf_Data	*dyn_data;
	GElf_Dyn	p_dyn;
	GElf_Phdr	p_phdr;
	GElf_Ehdr	p_ehdr;
	int		index = 1;
	int		lib_scns = num_scns;
	SCNTAB		*l_scns = p_scns;
	int		header_num = 0;
	const char	*str;

	(void) gelf_getehdr(elf_file, &p_ehdr);

	if (!p_flag)
		(void) printf("\n  **** DYNAMIC SECTION INFORMATION ****\n");

	for (; num_scns > 0; num_scns--, p_scns++) {
		GElf_Word	link;
		int		ii;


		if (p_scns->p_shdr.sh_type != SHT_DYNAMIC)
			continue;

		if (!p_flag) {
			(void) printf("%s:\n", p_scns->scn_name);
			(void) printf("[INDEX]\tTag         Value\n");
		}

		if ((dyn_data = elf_getdata(p_scns->p_sd, NULL)) == 0) {
			(void) fprintf(stderr, "%s: %s: no data in "
			    "%s section\n", prog_name, filename,
			    p_scns->scn_name);
			return;
		}

		link = p_scns->p_shdr.sh_link;
		ii = 0;

		(void) gelf_getdyn(dyn_data, ii++, &p_dyn);
		while (p_dyn.d_tag != DT_NULL) {
			union {
				Conv_inv_buf_t		inv;
				Conv_dyn_flag_buf_t	dyn_flag;
				Conv_dyn_flag1_buf_t	dyn_flag1;
				Conv_dyn_feature1_buf_t	dyn_feature1;
				Conv_dyn_posflag1_buf_t	dyn_posflag1;
			} conv_buf;

			(void) printf("[%d]\t%-15.15s ", index++,
			    conv_dyn_tag(p_dyn.d_tag,
			    p_ehdr.e_ident[EI_OSABI], p_ehdr.e_machine,
			    DUMP_CONVFMT, &conv_buf.inv));

			/*
			 * It would be nice to use a table driven loop
			 * here, but the address space is too sparse
			 * and irregular. A switch is simple and robust.
			 */
			switch (p_dyn.d_tag) {
			/*
			 * Items with an address value
			 */
			case DT_PLTGOT:
			case DT_HASH:
			case DT_STRTAB:
			case DT_RELA:
			case DT_SYMTAB:
			case DT_INIT:
			case DT_FINI:
			case DT_REL:
			case DT_DEBUG:
			case DT_TEXTREL:
			case DT_JMPREL:
			case DT_INIT_ARRAY:
			case DT_FINI_ARRAY:
			case DT_INIT_ARRAYSZ:
			case DT_FINI_ARRAYSZ:
			case DT_PREINIT_ARRAY:
			case DT_PREINIT_ARRAYSZ:
			case DT_SUNW_RTLDINF:
			case DT_SUNW_CAP:
			case DT_SUNW_CAPINFO:
			case DT_SUNW_CAPCHAIN:
			case DT_SUNW_SYMTAB:
			case DT_SUNW_SYMSORT:
			case DT_SUNW_TLSSORT:
			case DT_PLTPAD:
			case DT_MOVETAB:
			case DT_SYMINFO:
			case DT_RELACOUNT:
			case DT_RELCOUNT:
			case DT_VERSYM:
			case DT_VERDEF:
			case DT_VERDEFNUM:
			case DT_VERNEED:
				(void) printf(pdyn_Fmtptr,
				    EC_ADDR(p_dyn.d_un.d_ptr));
				break;

			/*
			 * Items with a string value
			 */
			case DT_NEEDED:
			case DT_SONAME:
			case DT_RPATH:
			case DT_RUNPATH:
			case DT_SUNW_AUXILIARY:
			case DT_SUNW_FILTER:
			case DT_CONFIG:
			case DT_DEPAUDIT:
			case DT_AUDIT:
			case DT_AUXILIARY:
			case DT_USED:
			case DT_FILTER:
				if (v_flag) {	/* Look up the string */
					str = (char *)elf_strptr(elf_file, link,
					    p_dyn.d_un.d_ptr);
					if (!(str && *str))
						str = (char *)UNKNOWN;
					(void) printf("%s", str);
				} else {	/* Show the address */
					(void) printf(pdyn_Fmtptr,
					    EC_ADDR(p_dyn.d_un.d_ptr));
				}
				break;

			/*
			 * Items with a literal value
			 */
			case DT_PLTRELSZ:
			case DT_RELASZ:
			case DT_RELAENT:
			case DT_STRSZ:
			case DT_SYMENT:
			case DT_RELSZ:
			case DT_RELENT:
			case DT_PLTREL:
			case DT_BIND_NOW:
			case DT_CHECKSUM:
			case DT_PLTPADSZ:
			case DT_MOVEENT:
			case DT_MOVESZ:
			case DT_SYMINSZ:
			case DT_SYMINENT:
			case DT_VERNEEDNUM:
			case DT_SPARC_REGISTER:
			case DT_SUNW_SYMSZ:
			case DT_SUNW_SORTENT:
			case DT_SUNW_SYMSORTSZ:
			case DT_SUNW_TLSSORTSZ:
			case DT_SUNW_STRPAD:
			case DT_SUNW_CAPCHAINENT:
			case DT_SUNW_CAPCHAINSZ:
			case DT_SUNW_ASLR:
			case DT_SUNW_KMOD:
				(void) printf(pdyn_Fmtptr,
				    EC_XWORD(p_dyn.d_un.d_val));
				break;

			/*
			 * Integer items that are bitmasks, or which
			 * can be otherwise formatted in symbolic form.
			 */
			case DT_FLAGS:
			case DT_FEATURE_1:
			case DT_POSFLAG_1:
			case DT_FLAGS_1:
			case DT_SUNW_LDMACH:
				str = NULL;
				if (v_flag) {
					switch (p_dyn.d_tag) {
					case DT_FLAGS:
						str = conv_dyn_flag(
						    p_dyn.d_un.d_val,
						    DUMP_CONVFMT,
						    &conv_buf.dyn_flag);
						break;
					case DT_FEATURE_1:
						str = conv_dyn_feature1(
						    p_dyn.d_un.d_val,
						    DUMP_CONVFMT,
						    &conv_buf.dyn_feature1);
						break;
					case DT_POSFLAG_1:
						str = conv_dyn_posflag1(
						    p_dyn.d_un.d_val,
						    DUMP_CONVFMT,
						    &conv_buf.dyn_posflag1);
						break;
					case DT_FLAGS_1:
						str = conv_dyn_flag1(
						    p_dyn.d_un.d_val, 0,
						    &conv_buf.dyn_flag1);
						break;
					case DT_SUNW_LDMACH:
						str = conv_ehdr_mach(
						    p_dyn.d_un.d_val, 0,
						    &conv_buf.inv);
						break;
					}
				}
				if (str) {	/* Show as string */
					(void) printf("%s", str);
				} else {	/* Numeric form */
					(void) printf(pdyn_Fmtptr,
					    EC_ADDR(p_dyn.d_un.d_ptr));
				}
				break;

			/*
			 * Depreciated items with a literal value
			 */
			case DT_DEPRECATED_SPARC_REGISTER:
				(void) printf(pdyn_Fmtptr
				    "  (deprecated value)",
				    EC_XWORD(p_dyn.d_un.d_val));
				break;

			/* Ignored items */
			case DT_SYMBOLIC:
				(void) printf("(ignored)");
				break;
			}
			(void) printf("\n");
			(void) gelf_getdyn(dyn_data, ii++, &p_dyn);
		}
	}

	/*
	 * Check for existence of static shared library information.
	 */
	while (header_num < p_ehdr.e_phnum) {
		(void) gelf_getphdr(elf_file, header_num, &p_phdr);
		if (p_phdr.p_type == PT_SHLIB) {
			while (--lib_scns > 0) {
				if (strcmp(l_scns->scn_name, ".lib") == 0) {
					print_static(l_scns, filename);
				}
				l_scns++;
			}
		}
		header_num++;
	}
#undef	pdyn_Fmtptr
}

/*
 * Print the ELF header.  Input is an ELF file descriptor
 * and the filename.  If f_flag is set, the ELF header is
 * printed to stdout, otherwise the function returns after
 * setting the pointer to the ELF header.  Any values which
 * are not known are printed in decimal.  Fields must be updated
 * as new values are added.
 */
static GElf_Ehdr *
dump_elf_header(Elf *elf_file, char *filename, GElf_Ehdr * elf_head_p)
{
	int class;
	int field;

	if (gelf_getehdr(elf_file, elf_head_p) == NULL) {
		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
		    elf_errmsg(-1));
		return (NULL);
	}

	class = (int)elf_head_p->e_ident[4];

	if (class == ELFCLASS64)
		field = 21;
	else
		field = 13;

	if (!f_flag)
		return (elf_head_p);

	if (!p_flag) {
		(void) printf("\n                    **** ELF HEADER ****\n");
		(void) printf("%-*s%-11s%-*sMachine     Version\n",
		    field, "Class", "Data", field, "Type");
		(void) printf("%-*s%-11s%-*sFlags       Ehsize\n",
		    field, "Entry", "Phoff", field, "Shoff");
		(void) printf("%-*s%-11s%-*sShnum       Shstrndx\n\n",
		    field, "Phentsize", "Phnum", field, "Shentsz");
	}

	if (!v_flag) {
		(void) printf("%-*d%-11d%-*d%-12d%d\n",
		    field, elf_head_p->e_ident[4], elf_head_p->e_ident[5],
		    field, (int)elf_head_p->e_type, (int)elf_head_p->e_machine,
		    elf_head_p->e_version);
	} else {
		Conv_inv_buf_t	inv_buf;

		(void) printf("%-*s", field,
		    conv_ehdr_class(class, DUMP_CONVFMT, &inv_buf));
		(void) printf("%-11s",
		    conv_ehdr_data(elf_head_p->e_ident[5], DUMP_CONVFMT,
		    &inv_buf));
		(void) printf("%-*s", field,
		    conv_ehdr_type(elf_head_p->e_ident[EI_OSABI],
		    elf_head_p->e_type, DUMP_CONVFMT, &inv_buf));
		(void) printf("%-12s",
		    conv_ehdr_mach(elf_head_p->e_machine, DUMP_CONVFMT,
		    &inv_buf));
		(void) printf("%s\n",
		    conv_ehdr_vers(elf_head_p->e_version, DUMP_CONVFMT,
		    &inv_buf));
	}
	(void) printf("%-#*llx%-#11llx%-#*llx%-#12x%#x\n",
	    field, EC_ADDR(elf_head_p->e_entry), EC_OFF(elf_head_p->e_phoff),
	    field, EC_OFF(elf_head_p->e_shoff), EC_WORD(elf_head_p->e_flags),
	    EC_WORD(elf_head_p->e_ehsize));
	if (!v_flag || (elf_head_p->e_shstrndx != SHN_XINDEX)) {
		(void) printf("%-#*x%-11u%-#*x%-12u%u\n",
		    field, EC_WORD(elf_head_p->e_phentsize),
		    EC_WORD(elf_head_p->e_phnum),
		    field, EC_WORD(elf_head_p->e_shentsize),
		    EC_WORD(elf_head_p->e_shnum),
		    EC_WORD(elf_head_p->e_shstrndx));
	} else {
		(void) printf("%-#*x%-11u%-#*x%-12uXINDEX\n",
		    field, EC_WORD(elf_head_p->e_phentsize),
		    EC_WORD(elf_head_p->e_phnum),
		    field, EC_WORD(elf_head_p->e_shentsize),
		    EC_WORD(elf_head_p->e_shnum));
	}
	if ((elf_head_p->e_shnum == 0) && (elf_head_p->e_shoff > 0)) {
		Elf_Scn		*scn;
		GElf_Shdr	shdr0;
		int		field;

		if (gelf_getclass(elf_file) == ELFCLASS64)
			field = 21;
		else
			field = 13;
		if (!p_flag) {
			(void) printf("\n	   **** SECTION HEADER[0] "
			    "{Elf Extensions} ****\n");
			(void) printf(
			    "[No]\tType\tFlags\t%-*s %-*s%-*s%sName\n",
			    field, "Addr", field, "Offset", field,
			    "Size(shnum)",
			    /* compatibility:  tab for elf32 */
			    (field == 13) ? "\t" : "  ");
			(void) printf("\tLn(strndx) Info\t%-*s Entsize\n",
			    field, "Adralgn");
		}
		if ((scn = elf_getscn(elf_file, 0)) == NULL) {
			(void) fprintf(stderr,
			    "%s: %s: elf_getscn failed: %s\n",
			    prog_name, filename, elf_errmsg(-1));
			return (NULL);
		}
		if (gelf_getshdr(scn, &shdr0) == 0) {
			(void) fprintf(stderr,
			    "%s: %s: gelf_getshdr: %s\n",
			    prog_name, filename, elf_errmsg(-1));
			return (NULL);
		}
		(void) printf("[0]\t%u\t%llu\t", EC_WORD(shdr0.sh_type),
		    EC_XWORD(shdr0.sh_flags));

		(void) printf("%-#*llx %-#*llx%-*llu%s%-*u\n",
		    field, EC_ADDR(shdr0.sh_addr),
		    field, EC_OFF(shdr0.sh_offset),
		    field, EC_XWORD(shdr0.sh_size),
		    /* compatibility:  tab for elf32 */
		    ((field == 13) ? "\t" : "  "),
		    field, EC_WORD(shdr0.sh_name));

		(void) printf("\t%u\t%u\t%-#*llx %-#*llx\n",
		    EC_WORD(shdr0.sh_link),
		    EC_WORD(shdr0.sh_info),
		    field, EC_XWORD(shdr0.sh_addralign),
		    field, EC_XWORD(shdr0.sh_entsize));
	}
	(void) printf("\n");

	return (elf_head_p);
}

/*
 * Print section contents.  Input is an ELF file descriptor,
 * the ELF header, the SCNTAB structure,
 * the number of symbols, and the filename.
 * The number of sections,
 * and the offset into the SCNTAB structure will be
 * set in dump_section if d_flag or n_flag are set.
 * If v_flag is set, sections which can be interpreted will
 * be interpreted, otherwise raw data will be output in hexidecimal.
 */
static void
print_section(Elf *elf_file,
    GElf_Ehdr *p_ehdr, SCNTAB *p, int num_scns, char *filename)
{
	unsigned char    *p_sec;
	int	i;
	size_t	size;

	for (i = 0; i < num_scns; i++, p++) {
		GElf_Shdr shdr;

		size = 0;
		if (s_flag && !v_flag)
			p_sec = (unsigned char *)get_rawscn(p->p_sd, &size);
		else
			p_sec = (unsigned char *)get_scndata(p->p_sd, &size);

		if ((gelf_getshdr(p->p_sd, &shdr) != NULL) &&
		    (shdr.sh_type == SHT_NOBITS)) {
			continue;
		}
		if (s_flag && !v_flag) {
			(void) printf("\n%s:\n", p->scn_name);
			print_rawdata(p_sec, size);
			continue;
		}
		if (shdr.sh_type == SHT_SYMTAB) {
			dump_symbol_table(elf_file, p, filename);
			continue;
		}
		if (shdr.sh_type == SHT_DYNSYM) {
			dump_symbol_table(elf_file, p, filename);
			continue;
		}
		if (shdr.sh_type == SHT_STRTAB) {
			dump_string_table(p, 1);
			continue;
		}
		if (shdr.sh_type == SHT_RELA) {
			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
			continue;
		}
		if (shdr.sh_type == SHT_REL) {
			dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
			continue;
		}
		if (shdr.sh_type == SHT_DYNAMIC) {
			dump_dynamic(elf_file, p, 1, filename);
			continue;
		}

		(void) printf("\n%s:\n", p->scn_name);
		print_rawdata(p_sec, size);
	}
	(void) printf("\n");
}

/*
 * Print section contents. This function does not print the contents
 * of the sections but sets up the parameters and then calls
 * print_section to print the contents.  Calling another function to print
 * the contents allows both -d and -n to work correctly
 * simultaneously. Input is an ELF file descriptor, the ELF header,
 * the SCNTAB structure, the number of sections, and the filename.
 * Set the range of sections if d_flag, and set section name if
 * n_flag.
 */
static void
dump_section(Elf *elf_file,
    GElf_Ehdr *p_ehdr, SCNTAB *s, int num_scns, char *filename)
{
	SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */
	int i;
	int found_it = 0;  /* for use with -n section_name */

	if (n_flag) {
		n_range = s;

		for (i = 0; i < num_scns; i++, n_range++) {
			if ((strcmp(name, n_range->scn_name)) != 0)
				continue;
			else {
				found_it = 1;
				print_section(elf_file, p_ehdr,
				    n_range, 1, filename);
			}
		}

		if (!found_it) {
			(void) fprintf(stderr, "%s: %s: %s not found\n",
			    prog_name, filename, name);
		}
	} /* end n_flag */

	if (d_flag) {
		d_range = s;
		d_num = check_range(d_low, d_hi, num_scns, filename);
		if (d_num < 0)
			return;
		d_range += d_low - 1;

		print_section(elf_file, p_ehdr, d_range, d_num, filename);
	}	/* end d_flag */

	if (!n_flag && !d_flag)
		print_section(elf_file, p_ehdr, s, num_scns, filename);
}

/*
 * Print the section header table. This function does not print the contents
 * of the section headers but sets up the parameters and then calls
 * print_shdr to print the contents.  Calling another function to print
 * the contents allows both -d and -n to work correctly
 * simultaneously.  Input is the SCNTAB structure,
 * the number of sections from the ELF header, and the filename.
 * Set the range of section headers to print if d_flag, and set
 * name of section header to print if n_flag.
 */
static void
dump_shdr(Elf *elf_file, SCNTAB *s, int num_scns, char *filename)
{

	SCNTAB *n_range, *d_range;	/* for use with -n and -d modifiers */
	int field;
	int i;
	int found_it = 0;  /* for use with -n section_name */

	if (gelf_getclass(elf_file) == ELFCLASS64)
		field = 21;
	else
		field = 13;

	if (!p_flag) {
		(void) printf("\n	   **** SECTION HEADER TABLE ****\n");
		(void) printf("[No]\tType\tFlags\t%-*s %-*s %-*s%sName\n",
		    field, "Addr", field, "Offset", field, "Size",
		    /* compatibility:  tab for elf32 */
		    (field == 13) ? "\t" : "  ");
		(void) printf("\tLink\tInfo\t%-*s Entsize\n\n",
		    field, "Adralgn");
	}

	if (n_flag) {
		n_range = s;

		for (i = 1; i <= num_scns; i++, n_range++) {
			if ((strcmp(name, n_range->scn_name)) != 0)
				continue;
			else {
				found_it = 1;
				print_shdr(elf_file, n_range, 1, i);
			}
		}

		if (!found_it) {
			(void) fprintf(stderr, "%s: %s: %s not found\n",
			    prog_name, filename, name);
		}
	} /* end n_flag */

	if (d_flag) {
		d_range = s;
		d_num = check_range(d_low, d_hi, num_scns, filename);
		if (d_num < 0)
			return;
		d_range += d_low - 1;

		print_shdr(elf_file, d_range, d_num, d_low);
	}	/* end d_flag */

	if (!n_flag && !d_flag)
		print_shdr(elf_file, s, num_scns, 1);
}

/*
 * Process all of the command line options (except
 * for -a, -g, -f, and -o).  All of the options processed
 * by this function require the presence of the section
 * header table and will not be processed if it is not present.
 * Set up a buffer containing section name, section header,
 * and section descriptor for each section in the file.  This
 * structure is used to avoid duplicate calls to libelf functions.
 * Structure members for the symbol table, the debugging information,
 * and the line number information are global.  All of the
 * rest are local.
 */
static void
dump_section_table(Elf *elf_file, GElf_Ehdr *elf_head_p, char *filename)
{

	static SCNTAB	*buffer, *p_scns;
	Elf_Scn		*scn = 0;
	char		*s_name = NULL;
	int		found = 0;
	unsigned int	num_scns;
	size_t		shstrndx;
	size_t		shnum;


	if (elf_getshdrnum(elf_file, &shnum) == -1) {
		(void) fprintf(stderr,
		    "%s: %s: elf_getshdrnum failed: %s\n",
		    prog_name, filename, elf_errmsg(-1));
		return;
	}
	if (elf_getshdrstrndx(elf_file, &shstrndx) == -1) {
		(void) fprintf(stderr,
		    "%s: %s: elf_getshdrstrndx failed: %s\n",
		    prog_name, filename, elf_errmsg(-1));
		return;
	}

	if ((buffer = calloc(shnum, sizeof (SCNTAB))) == NULL) {
		(void) fprintf(stderr, "%s: %s: cannot calloc space\n",
		    prog_name, filename);
		return;
	}
	/* LINTED */
	num_scns = (int)shnum - 1;

	p_symtab = (SCNTAB *)0;
	p_dynsym = (SCNTAB *)0;
	p_scns = buffer;
	p_head_scns = buffer;

	while ((scn = elf_nextscn(elf_file, scn)) != 0) {
		if ((gelf_getshdr(scn, &buffer->p_shdr)) == 0) {
			(void) fprintf(stderr,
			    "%s: %s: %s\n", prog_name, filename,
			    elf_errmsg(-1));
			return;
		}
		s_name = (char *)
		    elf_strptr(elf_file, shstrndx, buffer->p_shdr.sh_name);
		buffer->scn_name = s_name ? s_name : (char *)UNKNOWN;
		buffer->p_sd   =  scn;

		if (buffer->p_shdr.sh_type == SHT_SYMTAB) {
			found += 1;
			p_symtab = buffer;
		}
		if (buffer->p_shdr.sh_type == SHT_DYNSYM)
			p_dynsym = buffer;
		buffer++;
	}

	/*
	 * These functions depend upon the presence of the section header table
	 * and will not be invoked in its absence
	 */
	if (h_flag) {
		dump_shdr(elf_file, p_scns, num_scns, filename);
	}
	if (p_symtab && (t_flag || T_flag)) {
		dump_symbol_table(elf_file, p_symtab, filename);
	}
	if (c_flag) {
		dump_string_table(p_scns, num_scns);
	}
	if (r_flag) {
		dump_reloc_table(elf_file, elf_head_p,
		    p_scns, num_scns, filename);
	}
	if (L_flag) {
		dump_dynamic(elf_file, p_scns, num_scns, filename);
	}
	if (s_flag) {
		dump_section(elf_file, elf_head_p, p_scns,
		    num_scns, filename);
	}
}

/*
 * Load the archive string table(s) (for extended-length strings)
 * into an in-core table/list
 */
static struct stab_list_s *
load_arstring_table(struct stab_list_s *STabList,
    int fd, Elf *elf_file, Elf_Arhdr *p_ar, char *filename)
{
	off_t here;
	struct stab_list_s *STL_entry, *STL_next;

	if (p_ar) {
		STL_entry = malloc(sizeof (struct stab_list_s));
		STL_entry->next    = 0;
		STL_entry->strings = 0;
		STL_entry->size    = 0;

		if (!STabList)
			STabList = STL_entry;
		else {
			STL_next = STabList;
			while (STL_next->next != (void *)0)
				STL_next = STL_next->next;
			STL_next->next = STL_entry;
		}

		STL_entry->size    = p_ar->ar_size;
		STL_entry->strings = malloc(p_ar->ar_size);
		here = elf_getbase(elf_file);
		if ((lseek(fd, here, 0)) != here) {
			(void) fprintf(stderr,
			    "%s: %s: could not lseek\n", prog_name, filename);
		}

		if ((read(fd, STL_entry->strings, p_ar->ar_size)) == -1) {
			(void) fprintf(stderr,
			    "%s: %s: could not read\n", prog_name, filename);
		}
	}
	return (STabList);
}

/*
 * Print the archive header for each member of an archive.
 * Also call ar_sym_read to print the symbols in the
 * archive symbol table if g_flag.  Input is a file descriptor,
 * an ELF file descriptor, and the filename.  Putting the call
 * to dump the archive symbol table in this function is more
 * efficient since it is necessary to examine the archive member
 * name in the archive header to determine which member is the
 * symbol table.
 */
static void
dump_ar_hdr(int fd, Elf *elf_file, char *filename)
{
	extern int v_flag, g_flag, a_flag, p_flag;
	Elf_Arhdr  *p_ar;
	Elf *arf;
	Elf_Cmd cmd;
	int title = 0;
	int err = 0;

	char buf[DATESIZE];

	cmd = ELF_C_READ;
	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
		p_ar = elf_getarhdr(arf);
		if (p_ar == NULL) {
			(void) fprintf(stderr,
			    "%s: %s: %s\n", prog_name, filename,
			    elf_errmsg(-1));
			continue;
		}
		if ((strcmp(p_ar->ar_name, "/") == 0) ||
		    (strcmp(p_ar->ar_name, "/SYM64/") == 0)) {
			if (g_flag)
				ar_sym_read(elf_file, filename);
		} else if (strcmp(p_ar->ar_name, "//") == 0) {
			StringTableList = load_arstring_table(
			    StringTableList, fd, arf, p_ar, filename);
			cmd = elf_next(arf);
			(void) elf_end(arf);
			continue;
		} else {
			if (a_flag) {
				(void) printf("%s[%s]:\n", filename,
				    p_ar->ar_name);
				if (!p_flag && title == 0) {
					if (!v_flag)
						(void) printf(
"\n\n\t\t\t***ARCHIVE HEADER***"
"\n	Date          Uid     Gid    Mode      Size	 Member Name\n\n");
					else
						(void) printf(
"\n\n\t\t\t***ARCHIVE HEADER***"
"\n	Date                   Uid    Gid   Mode     Size     Member Name\n\n");
					title = 1;
				}
				if (!v_flag) {
					(void) printf(
"\t0x%.8lx  %6d  %6d  0%.6ho  0x%.8lx  %-s\n\n",
					    p_ar->ar_date, (int)p_ar->ar_uid,
					    (int)p_ar->ar_gid,
					    (int)p_ar->ar_mode,
					    p_ar->ar_size, p_ar->ar_name);
				} else {
					if ((strftime(buf, DATESIZE,
					    "%b %d %H:%M:%S %Y",
					    localtime(
					    &(p_ar->ar_date)))) == 0) {
						(void) fprintf(stderr,
"%s: %s: don't have enough space to store the date\n", prog_name, filename);
						exit(1);
					}
					(void) printf(
"\t%s %6d %6d 0%.6ho 0x%.8lx %-s\n\n",
					    buf, (int)p_ar->ar_uid,
					    (int)p_ar->ar_gid,
					    (int)p_ar->ar_mode,
					    p_ar->ar_size, p_ar->ar_name);
				}
			}
		}
		cmd = elf_next(arf);
		(void) elf_end(arf);
	} /* end while */

	err = elf_errno();
	if (err != 0) {
		(void) fprintf(stderr,
		    "%s: %s: %s\n", prog_name, filename, elf_errmsg(err));
	}
}

/*
 * Process member files of an archive.  This function provides
 * a loop through an archive equivalent the processing of
 * each_file for individual object files.
 */
static void
dump_ar_files(int fd, Elf *elf_file, char *filename)
{
	Elf_Arhdr  *p_ar;
	Elf *arf;
	Elf_Cmd cmd;
	Elf_Kind file_type;
	GElf_Ehdr elf_head;
	char *fullname;

	cmd = ELF_C_READ;
	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
		size_t	len;

		p_ar = elf_getarhdr(arf);
		if (p_ar == NULL) {
			(void) fprintf(stderr, "%s: %s: %s\n",
			    prog_name, filename, elf_errmsg(-1));
			return;
		}
		if (p_ar->ar_name[0] == '/') {
			cmd = elf_next(arf);
			(void) elf_end(arf);
			continue;
		}

		len = strlen(filename) + strlen(p_ar->ar_name) + 3;
		if ((fullname = malloc(len)) == NULL)
			return;
		(void) snprintf(fullname, len, "%s[%s]", filename,
		    p_ar->ar_name);
		(void) printf("\n%s:\n", fullname);
		file_type = elf_kind(arf);
		if (file_type == ELF_K_ELF) {
			if (dump_elf_header(arf, fullname, &elf_head) == NULL)
				return;
			if (o_flag)
				dump_exec_header(arf,
				    (unsigned)elf_head.e_phnum, fullname);
			if (x_flag)
				dump_section_table(arf, &elf_head, fullname);
		} else {
			(void) fprintf(stderr, "%s: %s: invalid file type\n",
			    prog_name, fullname);
			cmd = elf_next(arf);
			(void) elf_end(arf);
			continue;
		}

		cmd = elf_next(arf);
		(void) elf_end(arf);
	} /* end while */
}

/*
 * Takes a filename as input.  Test first for a valid version
 * of libelf.a and exit on error.  Process each valid file
 * or archive given as input on the command line.  Check
 * for file type.  If it is an archive, process the archive-
 * specific options first, then files within the archive.
 * If it is an ELF object file, process it; otherwise
 * warn that it is an invalid file type.
 * All options except the archive-specific and program
 * execution header are processed in the function, dump_section_table.
 */
static void
each_file(char *filename)
{
	Elf *elf_file;
	GElf_Ehdr elf_head;
	int fd;
	Elf_Kind   file_type;

	struct stat buf;

	Elf_Cmd cmd;
	errno = 0;

	if (stat(filename, &buf) == -1) {
		int	err = errno;
		(void) fprintf(stderr, "%s: %s: %s", prog_name, filename,
		    strerror(err));
		return;
	}

	if ((fd = open((filename), O_RDONLY)) == -1) {
		(void) fprintf(stderr, "%s: %s: cannot read\n", prog_name,
		    filename);
		return;
	}
	cmd = ELF_C_READ;
	if ((elf_file = elf_begin(fd, cmd, (Elf *)0)) == NULL) {
		(void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
		    elf_errmsg(-1));
		return;
	}

	file_type = elf_kind(elf_file);
	if (file_type == ELF_K_AR) {
		if (a_flag || g_flag) {
			dump_ar_hdr(fd, elf_file, filename);
			elf_file = elf_begin(fd, cmd, (Elf *)0);
		}
		if (z_flag)
			dump_ar_files(fd, elf_file, filename);
	} else {
		if (file_type == ELF_K_ELF) {
			(void) printf("\n%s:\n", filename);
			if (dump_elf_header(elf_file, filename, &elf_head)) {
				if (o_flag)
					dump_exec_header(elf_file,
					    (unsigned)elf_head.e_phnum,
					    filename);
				if (x_flag)
					dump_section_table(elf_file,
					    &elf_head, filename);
			}
		} else {
			(void) fprintf(stderr, "%s: %s: invalid file type\n",
			    prog_name, filename);
		}
	}
	(void) elf_end(elf_file);
	(void) close(fd);
}

/*
 * Sets up flags for command line options given and then
 * calls each_file() to process each file.
 */
int
main(int argc, char *argv[], char *envp[])
{
	char *optstr = OPTSTR; /* option string used by getopt() */
	int optchar;

	prog_name = argv[0];

	(void) setlocale(LC_ALL, "");
	while ((optchar = getopt(argc, argv, optstr)) != -1) {
		switch (optchar) {
		case 'a':
			a_flag = 1;
			x_flag = 1;
			break;
		case 'g':
			g_flag = 1;
			x_flag = 1;
			break;
		case 'v':
			v_flag = 1;
			break;
		case 'p':
			p_flag = 1;
			break;
		case 'f':
			f_flag = 1;
			z_flag = 1;
			break;
		case 'o':
			o_flag = 1;
			z_flag = 1;
			break;
		case 'h':
			h_flag = 1;
			x_flag = 1;
			z_flag = 1;
			break;
		case 's':
			s_flag = 1;
			x_flag = 1;
			z_flag = 1;
			break;
		case 'd':
			d_flag = 1;
			x_flag = 1;
			z_flag = 1;
			set_range(optarg, &d_low, &d_hi);
			break;
		case 'n':
			n_flag++;
			x_flag = 1;
			z_flag = 1;
			name = optarg;
			break;
		case 'r':
			r_flag = 1;
			x_flag = 1;
			z_flag = 1;
			break;
		case 't':
			t_flag = 1;
			x_flag = 1;
			z_flag = 1;
			break;
		case 'C':
			C_flag = 1;
			t_flag = 1;
			x_flag = 1;
			z_flag = 1;
			break;
		case 'T':
			T_flag = 1;
			x_flag = 1;
			z_flag = 1;
			set_range(optarg, &T_low, &T_hi);
			break;
		case 'c':
			c_flag = 1;
			x_flag = 1;
			z_flag = 1;
			break;
		case 'L':
			L_flag = 1;
			x_flag = 1;
			z_flag = 1;
			break;
		case 'V':
			V_flag = 1;
			(void) fprintf(stderr, "dump: %s %s\n",
			    (const char *)SGU_PKG,
			    (const char *)SGU_REL);
			break;
		case '?':
			errflag += 1;
			break;
		default:
			break;
		}
	}

	if (errflag || (optind >= argc) || (!z_flag && !x_flag)) {
		if (!(V_flag && (argc == 2))) {
			usage();
			exit(269);
		}
	}

	if (elf_version(EV_CURRENT) == EV_NONE) {
		(void) fprintf(stderr, "%s: libelf is out of date\n",
		    prog_name);
		exit(101);
	}

	while (optind < argc) {
		each_file(argv[optind]);
		optind++;
	}
	return (0);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 *	Copyright (c) 1988 AT&T
 *	  All Rights Reserved
 *
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_DUMP_H
#define	_DUMP_H

#include	<sys/elf.h>
#include	<sys/machelf.h>
#include	<gelf.h>

/*
 * DUMP_CONVFMT defines the libconv formatting options we want to use:
 *	- Unknown items to be printed as integers using decimal formatting
 *	- The "Dump Style" versions of strings.
 */
#define	DUMP_CONVFMT (CONV_FMT_DECIMAL|CONV_FMT_ALT_DUMP)

#define	DATESIZE 60

typedef struct scntab {
	char		*scn_name;
	Elf_Scn		*p_sd;
	GElf_Shdr	p_shdr;
} SCNTAB;

#endif	/* _DUMP_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved  	*/


/*
 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include	<stdio.h>
#include	<stdlib.h>
#include	<unistd.h>
#include	<string.h>
#include	<_libelf.h>
#include	<limits.h>
#include	"conv.h"
#include	"dump.h"

extern int	p_flag;
extern char	*prog_name;


/*
 * Print the symbols in the archive symbol table.
 */
void
ar_sym_read(Elf *elf, char *filename)
{
	Elf_Arsym *	arsym;
	size_t		cnt, ptr, is64;
	const char	*fmt;

	if ((arsym = elf_getarsym(elf, &ptr)) == NULL) {
		(void) fprintf(stderr, "%s: %s: no archive symbol table\n",
		    prog_name, filename);
		return;
	}

	is64 = (_elf_getarsymwordsize(elf) == 8);
	(void) printf("%s:\n", filename);

	if (!p_flag) {
		(void) printf("     **** ARCHIVE SYMBOL TABLE ****\n");
		if (is64)
			(void) printf("%-8s         %s\n\n", "Offset", "Name");
		else
			(void) printf("%-8s %s\n\n", "Offset", "Name");
	}

	if (is64)
		fmt = "%-16.16llx %s\n";
	else
		fmt = "%-8.8llx %s\n";

	for (cnt = 0; cnt < ptr; cnt++, arsym++) {
		if (arsym->as_off)
			(void) printf(fmt, EC_XWORD(arsym->as_off),
			    (arsym->as_name ? arsym->as_name : ""));
	}
}

/*
 * Print the program execution header.  Input is an opened ELF object file, the
 * number of structure instances in the header as recorded in the ELF header,
 * and the filename.
 */
void
dump_exec_header(Elf *elf_file, unsigned nseg, char *filename)
{
	GElf_Ehdr ehdr;
	GElf_Phdr p_phdr;
	int counter;
	int field;
	extern int v_flag, p_flag;
	extern char *prog_name;

	if (gelf_getclass(elf_file) == ELFCLASS64)
		field = 16;
	else
		field = 12;

	if (!p_flag) {
		(void) printf(" ***** PROGRAM EXECUTION HEADER *****\n");
		(void) printf("%-*s%-*s%-*s%s\n",
		    field, "Type", field, "Offset",
		    field, "Vaddr", "Paddr");
		(void) printf("%-*s%-*s%-*s%s\n\n",
		    field, "Filesz", field, "Memsz",
		    field, "Flags", "Align");
	}

	if ((gelf_getehdr(elf_file, &ehdr) == 0) || (ehdr.e_phnum == 0)) {
		return;
	}

	for (counter = 0; counter < nseg; counter++) {

		if (gelf_getphdr(elf_file, counter, &p_phdr) == 0) {
			(void) fprintf(stderr,
			    "%s: %s: premature EOF on program exec header\n",
			    prog_name, filename);
			return;
		}

		if (!v_flag) {
			(void) printf(
	"%-*d%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx%-*u%-#*llx\n\n",
			    field, EC_WORD(p_phdr.p_type),
			    field, EC_OFF(p_phdr.p_offset),
			    field, EC_ADDR(p_phdr.p_vaddr),
			    field, EC_ADDR(p_phdr.p_paddr),
			    field, EC_XWORD(p_phdr.p_filesz),
			    field, EC_XWORD(p_phdr.p_memsz),
			    field, EC_WORD(p_phdr.p_flags),
			    field, EC_XWORD(p_phdr.p_align));
		} else {
			Conv_inv_buf_t	inv_buf;

			(void) printf("%-*s", field,
			    conv_phdr_type(ehdr.e_ident[EI_OSABI],
			    ehdr.e_machine, p_phdr.p_type, DUMP_CONVFMT,
			    &inv_buf));
			(void) printf(
			    "%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx",
			    field, EC_OFF(p_phdr.p_offset),
			    field, EC_ADDR(p_phdr.p_vaddr),
			    field, EC_ADDR(p_phdr.p_paddr),
			    field, EC_XWORD(p_phdr.p_filesz),
			    field, EC_XWORD(p_phdr.p_memsz));

			switch (p_phdr.p_flags) {
			case 0: (void) printf("%-*s", field, "---"); break;
			case PF_X:
				(void) printf("%-*s", field, "--x");
				break;
			case PF_W:
				(void) printf("%-*s", field, "-w-");
				break;
			case PF_W+PF_X:
				(void) printf("%-*s", field, "-wx");
				break;
			case PF_R:
				(void) printf("%-*s", field, "r--");
				break;
			case PF_R+PF_X:
				(void) printf("%-*s", field, "r-x");
				break;
			case PF_R+PF_W:
				(void) printf("%-*s", field, "rw-");
				break;
			case PF_R+PF_W+PF_X:
				(void) printf("%-*s", field, "rwx");
				break;
			default:
				(void) printf("%-*d", field, p_phdr.p_flags);
				break;
			}
			(void) printf(
			    "%-#*llx\n\n", field, EC_XWORD(p_phdr.p_align));
		}
	}
}