Source

gimli / dwarf-read.c

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
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
/*
 * Copyright (c) 2009-2012 Message Systems, Inc. All rights reserved
 * For licensing information, see:
 * https://bitbucket.org/wez/gimli/src/tip/LICENSE
 */
#include "impl.h"
#include "gimli_dwarf.h"
static gimli_addr_t calc_reloc(gimli_mapped_object_t f);

struct dw_die_arange {
  uint64_t addr;
  uint64_t len;
  uint64_t di_offset;
};

uint64_t dw_read_uleb128(const uint8_t **ptr, const uint8_t *end)
{
  uint64_t res = 0;
  int shift = 0;
  const uint8_t *cur = *ptr;
  while (cur < end) {
    uint8_t b = *(uint8_t*)cur;
    cur++;
    res |= (b & 0x7f) << shift;
    if ((b & 0x80) == 0) break;
    shift += 7;
  }
  *ptr = cur;
  return res;
}

int64_t dw_read_leb128(const uint8_t **ptr, const uint8_t *end)
{
  int64_t res = 0;
  int shift = 0;
  int sign;
  const uint8_t *cur = *ptr;
  while (cur < end) {
    uint8_t b = *(uint8_t*)cur;
    cur++;
    res |= (b & 0x7f) << shift;
    shift += 7;
    sign = (b & 0x40);
    if ((b & 0x80) == 0) break;
  }
  if ((shift < sizeof(res) * 8) && (sign)) {
    /* sign extend */
    res |= - (1 << shift);
  }
  *ptr = cur;
  return res;
}

int dw_read_encptr(gimli_proc_t proc,
  uint8_t enc, const uint8_t **ptr, const uint8_t *end,
  uint64_t pc, uint64_t *output)
{
  const uint8_t *cur = *ptr;
  uint64_t res = 0;
  uint64_t base = 0;

  if ((enc & DW_EH_PE_indirect) == DW_EH_PE_indirect) {
    /* the issue here is that we need to adjust for the load address of
     * the target. */
    printf("DW_EH_PE_indirect is not supported correctly at this time\n");
    return 0;

    if (sizeof(void*) == 8) {
      if (gimli_read_mem(proc, res, &res, sizeof(res))
          != sizeof(*output)) {
        return 0;
      }
    } else {
      uint32_t r;
      if (gimli_read_mem(proc, res, &r, sizeof(r)) != sizeof(r)) {
        return 0;
      }
      res = r;
    }
  }

  switch (enc & DW_EH_PE_APPL_MASK) {
    case DW_EH_PE_absptr:
      base = 0;
      break;
    case DW_EH_PE_pcrel:
      base = (uint64_t)pc;
      break;
    case DW_EH_PE_datarel:
    default:
      fprintf(stderr, "DWARF: unhandled pointer application value: %02x at %p\n", enc & DW_EH_PE_APPL_MASK, (void*)pc);
      return 0;
  }

  if ((enc & 0x07) == 0x00) {
    if (sizeof(void*) == 4) {
      enc |= DW_EH_PE_udata4;
    } else {
      enc |= DW_EH_PE_udata8;
    }
  }

  switch (enc & 0x0f) {
    case DW_EH_PE_uleb128:
      res = base + dw_read_uleb128(ptr, end);
      break;
    case DW_EH_PE_udata2:
      {
        uint16_t d;
        memcpy(&d, cur, sizeof(d));
        *ptr = cur + sizeof(d);
        res = base + d;
      }
      break;
    case DW_EH_PE_udata4:
      {
        uint32_t d;
        memcpy(&d, cur, sizeof(d));
        *ptr = cur + sizeof(d);
        res = base + d;
      }
      break;
    case DW_EH_PE_udata8:
      {
        uint64_t d;
        memcpy(&d, cur, sizeof(d));
        *ptr = cur + sizeof(d);
        res = base + d;
      }
      break;
    case DW_EH_PE_sleb128:
      res = base + dw_read_leb128(ptr, end);
      break;
    case DW_EH_PE_sdata2:
      {
        int16_t d;
        memcpy(&d, cur, sizeof(d));
        *ptr = cur + sizeof(d);
        res = base + d;
      }
      break;
    case DW_EH_PE_sdata4:
      {
        int32_t d;
        memcpy(&d, cur, sizeof(d));
        *ptr = cur + sizeof(d);
        res = base + d;
      }
      break;
    case DW_EH_PE_sdata8:
      {
        int64_t d;
        memcpy(&d, cur, sizeof(d));
        *ptr = cur + sizeof(d);
        res = base + d;
      }
      break;
    default:
      fprintf(stderr, "DWARF: unhandled DW_EH_PE value: 0x%02x (masked to 0x%02x) at %p\n", enc, enc & 0x0f, (void*)pc);
      return 0;
  }

  *output = res;
  return 1;
}

static int sort_by_addr(const void *A, const void *B)
{
  struct gimli_line_info *a = (struct gimli_line_info*)A;
  struct gimli_line_info *b = (struct gimli_line_info*)B;

  return a->addr - b->addr;
}

static int search_compare_line(const void *addrp, const void *L)
{
  struct gimli_line_info *line = (struct gimli_line_info*)L;
  gimli_addr_t pc = *(gimli_addr_t*)addrp;

  if (pc < line->addr) {
    return -1;
  }
  if (pc < line->end) {
    return 0;
  }
  return 1;
}

static int process_line_numbers(gimli_mapped_object_t f);

/* read dwarf info to determine the source/line information for a given
 * address */
int gimli_determine_source_line_number(gimli_proc_t proc,
  gimli_addr_t pc, char *src, int srclen,
  uint64_t *lineno)
{
  struct gimli_object_mapping *m;
  gimli_mapped_object_t f;
  struct gimli_line_info *linfo;

  m = gimli_mapping_for_addr(proc, pc);
  if (!m) {
    return 0;
  }
  f = m->objfile;

  if (!f->elf) {
    /* can happen if the original file has been removed from disk */
    return 0;
  }
  if (!f->lines) {
    process_line_numbers(f);
    if (!f->lines) {
      return 0;
    }
  }

#ifdef __MACH__
  pc -= f->base_addr;
#else
  if (!gimli_object_is_executable(f->elf)) {
    pc -= calc_reloc(f);
  }
#endif

  linfo = bsearch(&pc, f->lines, f->linecount, sizeof(*linfo),
      search_compare_line);

  if (linfo) {
    snprintf(src, srclen, "%s", linfo->filename);
    *lineno = linfo->lineno;
    return 1;
  }
  return 0;
}


static int process_line_numbers(gimli_mapped_object_t f)
{
  struct gimli_section_data *s = NULL;
  struct {
    char *address;
    uint64_t file;
    uint64_t line;
    uint64_t column;
    uint8_t is_stmt; /* recommended breakpoint location */
    uint8_t basic_block; /* is start of a basic block */
    uint8_t end_sequence;
    uint8_t prologue_end;
    uint8_t epilogue_begin;
    uint64_t isa;
  } regs;
  const uint8_t *data, *end;
  uint32_t initlen;
  uint64_t len;
  int is_64 = 0;
  uint16_t ver;
  struct {
    uint8_t min_insn_len;
    uint8_t def_is_stmt;
    int8_t line_base;
    uint8_t line_range;
    uint8_t opcode_base;
  } hdr_1;
  uint64_t *opcode_lengths = NULL;
  int i;
  const char *filenames[1024];
  uint8_t op;
  struct gimli_line_info *linfo;
  int debugline = debug && 0;

  if (f->aux_elf) {
    s = gimli_get_section_by_name(f->aux_elf, ".debug_line");
    if (s) {
      data = s->data;
    }
  }
  if (!s) {
    s = gimli_get_section_by_name(f->elf, ".debug_line");
    if (s) {
      data = s->data;
    }
  }
  if (!s) {
    return 0;
  }
  if (debugline) fprintf(stderr, "\nGot debug_line info\n");

  end = data + s->size;

  while (data < end) {
    const uint8_t *cuend;
    void *prior;

    memset(&regs, 0, sizeof(regs));
    regs.file = 1;
    regs.line = 1;
    prior = NULL;

    /* read the initial length, this tells us which dwarf version and format
     * we're dealing with */
    memcpy(&initlen, data, sizeof(initlen));
    data += sizeof(initlen);

    if (initlen == 0xffffffff) {
      /* this is a 64-bit dwarf */
      is_64 = 1;
      memcpy(&len, data, sizeof(len));
      data += sizeof(len);
    } else {
      len = initlen;
    }
    cuend = data + len;

    memcpy(&ver, data, sizeof(ver));
    data += sizeof(ver);

    if (debugline) {
      fprintf(stderr, "initlen is 0x%" PRIx64 " (%d bit) ver=%u\n",
        len, is_64 ? 64 : 32, ver);
    }

    if (is_64) {
      memcpy(&len, data, sizeof(len));
      data += sizeof(len);
    } else {
      memcpy(&initlen, data, sizeof(initlen));
      data += sizeof(initlen);
      len = initlen;
    }
    memcpy(&hdr_1, data, sizeof(hdr_1));
    data += sizeof(hdr_1);
    regs.is_stmt = hdr_1.def_is_stmt;

    if (debugline) {
      fprintf(stderr,
        "headerlen is %" PRIu64 ", min_insn_len=%u line_base=%d line_range=%u\n"
        "opcode_base=%u\n",
        len, hdr_1.min_insn_len, hdr_1.line_base, hdr_1.line_range,
        hdr_1.opcode_base);
    }
    if (opcode_lengths) free(opcode_lengths);
    opcode_lengths = calloc(hdr_1.opcode_base, sizeof(uint64_t));
    for (i = 1; i < hdr_1.opcode_base; i++) {
      opcode_lengths[i-1] = dw_read_uleb128(&data, cuend);
      if (debugline) {
        fprintf(stderr, "op len [%d] = %" PRIu64 "\n", i, opcode_lengths[i-1]);
      }
    }
    /* include_directories */
    while (*data && data < cuend) {
      if (debugline) fprintf(stderr, "inc_dir: %s\n", data);
      data += strlen((char*)data) + 1;
    }
    data++;

    /* files */
    i = 1;
    memset(filenames, 0, sizeof(filenames));
    while (*data && data < cuend) {
      if (i >= sizeof(filenames)/sizeof(filenames[0])) {
        fprintf(stderr, "DWARF: too many files for line number info reader\n");
        return 0;
      }
      if (debugline) fprintf(stderr, "file[%d] = %s\n", i, data);
      filenames[i] = (char*)data;
      data += strlen((char*)data) + 1;
      /* ignore additional data about the file */
      dw_read_uleb128(&data, cuend);
      dw_read_uleb128(&data, cuend);
      dw_read_uleb128(&data, cuend);
      i++;
    }
    data++;

    /* opcodes */
    while (data < cuend) {
      memcpy(&op, data, sizeof(op));
      data += sizeof(op);

      prior = regs.address;

      if (op == 0) {
        /* extended */
        const uint8_t *next;
        initlen = dw_read_uleb128(&data, cuend);
        memcpy(&op, data, sizeof(op));
        next = data + initlen;
        data += sizeof(op);
        switch (op) {
          case DW_LNE_set_address:
            {
              void *addr;
              memcpy(&addr, data, sizeof(addr));
              if (debugline) fprintf(stderr, "set_address %p\n", addr);
              regs.address = addr;
              break;
            }
          case DW_LNE_end_sequence:
            {
              if (debugline) fprintf(stderr, "end_sequence\n");
              memset(&regs, 0, sizeof(regs));
              regs.file = 1;
              regs.line = 1;
              break;
            }
          case DW_LNE_define_file:
            {
              const char *fname = (char*)data;
              uint64_t fno;

              data += strlen(fname)+1;
              fno = dw_read_uleb128(&data, cuend);
              filenames[fno] = fname;
              if (debugline) fprintf(stderr, "define_files[%" PRIu64 "] = %s\n", fno, fname);
              break;
            }

          default:
//            fprintf(stderr,
//              "DWARF: line nos.: unhandled extended op=%02x, len=%" PRIu32 "\n",
//              op, initlen);
            ;
        }
        data = next;
      } else if (op < hdr_1.opcode_base) {
        /* standard opcode */
        switch (op) {
          case DW_LNS_copy:
            if (debugline) fprintf(stderr, "copy\n");
            regs.basic_block = 0;
            regs.prologue_end = 0;
            regs.epilogue_begin = 0;
            break;
          case DW_LNS_advance_line:
            {
              int64_t d = dw_read_leb128(&data, cuend);
              if (debugline) {
                fprintf(stderr, "advance_line from %" PRId64 " to %" PRId64 "\n",
                  regs.line, regs.line + d);
              }
              regs.line += d;
              break;
            }

          case DW_LNS_advance_pc:
            {
              uint64_t u = dw_read_uleb128(&data, cuend);
              regs.address += u * hdr_1.min_insn_len;
              if (debugline) {
                fprintf(stderr, "advance_pc: addr=0x%" PRIx64 "\n", (uintptr_t)regs.address);
              }
              break;
            }
          case DW_LNS_set_file:
          {
            uint64_t u = dw_read_uleb128(&data, cuend);
            regs.file = u;
            if (debugline) fprintf(stderr, "set_file: %" PRIu64 "\n", regs.file);
            break;
          }
          case DW_LNS_set_column:
          {
            uint64_t u = dw_read_uleb128(&data, cuend);
            regs.column = u;
            if (debugline) fprintf(stderr, "set_column: %" PRIu64 "\n", regs.column);
            break;
          }
          case DW_LNS_negate_stmt:
            if (debugline) fprintf(stderr, "negate_stmt\n");
            regs.is_stmt = !regs.is_stmt;
            break;
          case DW_LNS_set_basic_block:
            if (debugline) fprintf(stderr, "set_basic_block\n");
            regs.basic_block = 1;
            break;
          case DW_LNS_const_add_pc:
            regs.address += ((255 - hdr_1.opcode_base) /
                            hdr_1.line_range) * hdr_1.min_insn_len;
            if (debugline) {
              fprintf(stderr, "const_add_pc: addr=0x%" PRIx64 "\n", (uintptr_t)regs.address);
            }
            break;
          case DW_LNS_fixed_advance_pc:
          {
            uint16_t u;
            memcpy(&u, data, sizeof(u));
            data += sizeof(u);
            regs.address += u;
            if (debugline) {
              fprintf(stderr, "fixed_advance_pc: 0x%" PRIx64 "\n", (uintptr_t)regs.address);
            }
            break;
          }
          case DW_LNS_set_prologue_end:
            if (debugline) {
              fprintf(stderr, "set_prologue_end\n");
            }
            regs.prologue_end = 1;
            break;
          case DW_LNS_set_epilogue_begin:
            if (debugline) {
              fprintf(stderr, "set_epilogue_begin\n");
            }
            regs.epilogue_begin = 1;
            break;
          case DW_LNS_set_isa:
            regs.isa = dw_read_uleb128(&data, cuend);
            if (debugline) {
              fprintf(stderr, "set_isa: 0x%" PRIx64 "\n", regs.isa);
            }
            break;
          default:
            fprintf(stderr, "DWARF: line nos: unhandled op: %02x\n", op);
            /* consume unknown/unhandled args */
            for (i = 0; i < opcode_lengths[i]; i++) {
              dw_read_uleb128(&data, cuend);
            }
        }
      } else {
        /* special opcode */
        op -= hdr_1.opcode_base;

        if (debugline) {
          fprintf(stderr, "special before: addr = %p, line = %" PRId64 "\n",
              regs.address, regs.line);
          fprintf(stderr, "line_base = %d, line_range = %d\n",
            hdr_1.line_base, hdr_1.line_range);
        }

        regs.address += (op / hdr_1.line_range) * hdr_1.min_insn_len;
        regs.line += hdr_1.line_base + (op % hdr_1.line_range);
        if (debugline) {
          fprintf(stderr, "special: addr = %p, line = %" PRId64 "\n",
            regs.address, regs.line);
        }
      }


      if (regs.address && filenames[regs.file]) {
        if (f->linecount + 1 >= f->linealloc) {
          f->linealloc = f->linealloc ? f->linealloc * 2 : 1024;
          f->lines = realloc(f->lines, f->linealloc * sizeof(*linfo));
        }
        linfo = &f->lines[f->linecount++];
        linfo->filename = filenames[regs.file];
        linfo->lineno = regs.line;
        linfo->addr = (gimli_addr_t)regs.address;
      }
    }
  }

  qsort(f->lines, f->linecount, sizeof(struct gimli_line_info), sort_by_addr);
//printf("sorting %d lines in %s\n", f->linecount, f->objname);

  free(opcode_lengths);

  /* make a pass to fill in the end member to make it easier to find
   * an approx match */
  if (f->linecount) {
    for (i = 0; i < f->linecount - 1; i++) {
      f->lines[i].end = f->lines[i+1].addr;
    }
  }

  return 0;
}

static int get_sect_data(gimli_mapped_object_t f, const char *name,
  const uint8_t **startptr, const uint8_t **endptr, gimli_object_file_t *elf)
{
  const uint8_t *data = NULL, *end = NULL;
  struct gimli_section_data *s;

  if (elf && *elf) {
    s = gimli_get_section_by_name(*elf, name);
  } else {
    s = gimli_get_section_by_name(f->elf, name);
    if (!s || s->size <= sizeof(void*)) {
      if (f->aux_elf) {
        s = gimli_get_section_by_name(f->aux_elf, name);
      } else {
        s = NULL;
      }
    }
  }
  if (s) {
    data = s->data;
    if (elf) *elf = s->container;
  } else {
    data = NULL;
    if (elf) *elf = NULL;
  }
  if (data == NULL) {
    return 0;
  }
  end = data + s->size;

  *startptr = data;
  *endptr = end;
  return 1;
}


/* given a location list offset, determine the location in question */
int dw_calc_location(struct gimli_unwind_cursor *cur,
  uint64_t compilation_unit_base_addr,
  struct gimli_object_mapping *m, uint64_t offset, uint64_t *res,
  gimli_object_file_t elf, int *is_stack)
{
  const uint8_t *data, *end;
  void *rstart = NULL, *rend = NULL;
  uint16_t len;
  uint64_t off = compilation_unit_base_addr;

  if (!get_sect_data(m->objfile, ".debug_loc", &data, &end, &elf)) {
    printf("Couldn't find a .debug_loc\n");
    return 0;
  }

//  printf("Using offset %d into .debug_loc\n", offset);
  data += offset;

  while (data < end) {
//    printf("populating rstart with %d bytes\n", sizeof(rstart));
    memcpy(&rstart, data, sizeof(rstart));
    data += sizeof(rstart);
    memcpy(&rend, data, sizeof(rend));
    data += sizeof(rend);
    if (rstart == 0 && rend == 0) {
      /* end of list */
//      printf("rstart = %p, so ending list\n", rstart);
      break;
    }
    if (rstart == (void*)-1) {
      /* base selection */
      off = (uint64_t)(intptr_t)rend;
      printf("got base selection: %p\n", rend);
      continue;
    }

    rstart += off;
    rend += off;

    memcpy(&len, data, sizeof(len));
    data += sizeof(len);
//    printf("This section is %d bytes in length\n", len);

//    printf("%p - %p\n", rstart, rend);
    if (cur->st.pc >= rstart && cur->st.pc < rend) {
//      printf("Found the range I was looking for, data=%p, len=%d\n", data, len);

      return dw_eval_expr(cur, (uint8_t*)data, len, 0, res, NULL, is_stack);
    }
    data += len;
  }
  return 0;
}


static const uint8_t *find_abbr(gimli_mapped_object_t file,
    uint64_t da_offset,
    uint64_t fcode)
{
  uint64_t code;
  uint64_t tag;
  uint64_t key;
  const uint8_t *abbr;
  int slow_mode = 0;

  if (!file->abbr.map) {
    if (!get_sect_data(file, ".debug_abbrev",
          &file->abbr.start, &file->abbr.end, &file->abbr.elf)) {
      printf("could not get abbrev data for %s\n", file->objname);
      return 0;
    }

    /* observed approx 11-13 per abbrev, err on the side of avoiding
     * rebuckets */
    file->abbr.map = gimli_hash_new_size(NULL, GIMLI_HASH_U64_KEYS,
        (file->abbr.end - file->abbr.start) / 10);
  }

  /* NOTE: even though DWARF allows for 64-bit offsets, we're making the assumption
   * that they are not practical or possible for the next few years.
   * Doing so allows us to cheaply record both the da_offset and code
   * into a u64 key.
   * I've observed approx 17% collisions with a max chain length of 7
   * in a collided bucket.  It's not perfect but it is effective.
   * */
  if (da_offset > UINT32_MAX || fcode > UINT32_MAX) {
    // Allow correct, albeit slow, operation when we overflow this assumption
    slow_mode = 1;
  }
  if (!slow_mode) {
    key = (da_offset << 32) | (fcode & 0xffffffff);
    if (gimli_hash_find_u64(file->abbr.map, key, (void**)&abbr)) {
      return abbr;
    }
  }

  abbr = file->abbr.start + da_offset;

  while (abbr < file->abbr.end) {
    code = dw_read_uleb128(&abbr, file->abbr.end);
    if (code == 0) continue;
//    printf("find_abbr: %lld (looking for %lld)\n", code, fcode);
    if (fcode == code) {

//printf("find_abbr: %" PRIx64 " -> %p\n", fcode, abbr);
      if (!slow_mode &&
          !gimli_hash_insert_u64(file->abbr.map, key, (void*)abbr)) {
        void *ptr = NULL;
        gimli_hash_find_u64(file->abbr.map, key, &ptr);
        if (ptr != abbr) {
          printf("find_abbr: %" PRIx64 " (key=%" PRIx64 ") collided with %p and %p\n",
              fcode, key, abbr, ptr);
        }
      }
      return abbr;
    }

    tag = dw_read_uleb128(&abbr, file->abbr.end);
    abbr += sizeof(uint8_t);


    while (abbr < file->abbr.end) {
      dw_read_uleb128(&abbr, file->abbr.end);
      code = dw_read_uleb128(&abbr, file->abbr.end);
      if (code == 0) {
        break;
      }
    }
  }
  return NULL;
}

static uint64_t get_value(uint64_t form, uint64_t addr_size, int is_64,
  const uint8_t **datap, const uint8_t *end,
  uint64_t *vptr, const uint8_t **byteptr,
  gimli_object_file_t elf)
{
  uint64_t u64;
  int64_t s64;
  uint32_t u32;
  uint16_t u16;
  uint8_t u8;
  const uint8_t *data = *datap;
  const uint8_t *strp, *send;

  *byteptr = NULL;

  switch (form) {
    case DW_FORM_addr:
    case DW_FORM_ref_addr:
      switch (addr_size) {
        case 1:
          memcpy(&u8, data, sizeof(u8));
          *vptr = u8;
          break;
        case 2:
          memcpy(&u16, data, sizeof(u16));
          *vptr = u16;
          break;
        case 4:
          memcpy(&u32, data, sizeof(u32));
          *vptr = u32;
          break;
        case 8:
          memcpy(vptr, data, sizeof(u64));
          break;
      }
      data += addr_size;
      break;
    case DW_FORM_data1:
    case DW_FORM_ref1:
      memcpy(&u8, data, sizeof(u8));
      data += sizeof(u8);
      *vptr = u8;
      break;
    case DW_FORM_data2:
    case DW_FORM_ref2:
      memcpy(&u16, data, sizeof(u16));
      data += sizeof(u16);
      *vptr = u16;
      break;
    case DW_FORM_data4:
    case DW_FORM_ref4:
      memcpy(&u32, data, sizeof(u32));
      data += sizeof(u32);
      *vptr = u32;
      break;
    case DW_FORM_data8:
    case DW_FORM_ref8:
      memcpy(&u64, data, sizeof(u64));
      data += sizeof(u64);
      *vptr = u64;
      break;
    case DW_FORM_udata:
    case DW_FORM_ref_udata:
      *vptr = dw_read_uleb128(&data, end);
      break;
    case DW_FORM_sdata:
      s64 = dw_read_leb128(&data, end);
      *vptr = (uint64_t)s64;
      break;
    case DW_FORM_flag:
      memcpy(&u8, data, sizeof(u8));
      data += sizeof(u8);
      *vptr = u8;
      break;
    /* for blocks, store length in vptr and set byteptr to start of data */
    case DW_FORM_block1:
      memcpy(&u8, data, sizeof(u8));
      *vptr = u8;
      data += sizeof(u8);
      *byteptr = data;
      data += *vptr;
      break;
    case DW_FORM_block2:
      memcpy(&u16, data, sizeof(u16));
      *vptr = u16;
      data += sizeof(u16);
      *byteptr = data;
      data += *vptr;
      break;
    case DW_FORM_block4:
      memcpy(&u32, data, sizeof(u32));
      *vptr = u32;
      data += sizeof(u32);
      *byteptr = data;
      data += *vptr;
      break;
    case DW_FORM_block:
      *vptr = dw_read_uleb128(&data, end);
      *byteptr = data;
      data += *vptr;
      break;
    case DW_FORM_strp:
      if (is_64) {
        memcpy(vptr, data, sizeof(*vptr));
        data += sizeof(*vptr);
      } else {
        memcpy(&u32, data, sizeof(u32));
        data += sizeof(u32);
        *vptr = u32;
      }
      if (get_sect_data(NULL, ".debug_str", &strp, &send, &elf)) {
        const uint8_t *str = strp + *vptr;
        *vptr = strlen((char*)str);
        form = DW_FORM_string;
        *byteptr = str;
      }
      break;
    case DW_FORM_string:
      *byteptr = data;
      *vptr = strlen((char*)data);
      data += 1 + *vptr;
      break;

    case DW_FORM_indirect:
      form = dw_read_uleb128(datap, end);
      if (form == DW_FORM_indirect) {
        printf(
          "DWARF: can't have an indirect FORM reference an indirect FORM\n");
        return 0;
      }
      return get_value(form, addr_size, is_64, datap, end, vptr, byteptr, elf);

    default:
      printf("DWARF: unhandled FORM: 0x%" PRIx64 "\n", form);
      return 0;
  }

  *datap = data;

  /* normalize the form */
  switch (form) {
    case DW_FORM_string:
    case DW_FORM_flag:
    case DW_FORM_addr:
    case DW_FORM_udata:
    case DW_FORM_sdata:
    case DW_FORM_ref_addr:
      break;
    case DW_FORM_block:
    case DW_FORM_block1:
    case DW_FORM_block2:
    case DW_FORM_block4:
      form = DW_FORM_block;
      break;
    case DW_FORM_data1:
    case DW_FORM_data2:
    case DW_FORM_data4:
    case DW_FORM_data8:
      form = DW_FORM_data8;
      break;
    case DW_FORM_ref1:
    case DW_FORM_ref2:
    case DW_FORM_ref4:
    case DW_FORM_ref8:
    case DW_FORM_ref_udata:
      form = DW_FORM_ref_udata;
      break;
  }
  if (debug && 0) {
    printf("value normalized to form 0x%" PRIx64 " val=0x%" PRIx64 " bytep=%p %s\n",
       form, *vptr, *byteptr, form == DW_FORM_string ? (char*)*byteptr : "");
  }


  return form;
}

static struct gimli_dwarf_die *process_die(
  gimli_mapped_object_t file,
  struct gimli_dwarf_cu *cu,
  const uint8_t *custart,
  const uint8_t **datap, const uint8_t *end,
  uint64_t da_offset,
  int is_64, uint8_t addr_size
)
{
  const uint8_t *data = *datap;
  uint64_t abbr_code;
  uint64_t tag;
  uint8_t has_children;
  uint64_t atype, aform;
  const uint8_t *abbr;
  struct gimli_dwarf_die *die = NULL, *kid = NULL;
  struct gimli_dwarf_attr *attr = NULL;
  uint64_t offset;

  offset = data - file->debug_info.start;

  abbr_code = dw_read_uleb128(&data, end);
  if (abbr_code == 0) {
    // Skip over NUL entry
//    printf("found a NUL entry @ 0x%" PRIx64 "\n", offset);
    *datap = data;
    return NULL;
  }
  abbr = find_abbr(file, da_offset, abbr_code);
  if (!abbr) {
    printf("Couldn't locate abbrev code %" PRId64 "\n", abbr_code);
    *datap = data;
    return NULL;
  }

  /* what kind of entry is this? */
  tag = dw_read_uleb128(&abbr, file->abbr.end);
  memcpy(&has_children, abbr, sizeof(has_children));
  abbr += sizeof(has_children);
  if (has_children != 0 && has_children != 1) {
    printf("invalid value for has_children! %d\n", has_children);
    abort();
  }

  die = gimli_slab_alloc(&file->dieslab);
  memset(die, 0, sizeof(*die));
  die->offset = offset;
  die->tag = tag;
  STAILQ_INIT(&die->kids);

  while (data < end && abbr < file->abbr.end) {
    atype = dw_read_uleb128(&abbr, file->abbr.end);
    aform = dw_read_uleb128(&abbr, file->abbr.end);

    if (atype == 0) {
      break;
    }

    attr = gimli_slab_alloc(&file->attrslab);
    memset(attr, 0, sizeof(*attr));
    attr->attr = atype;

    attr->form = get_value(aform, addr_size, is_64, &data, end,
        &attr->code, &attr->ptr, file->debug_info.elf);

    if (attr->form == 0) {
      printf("Failed to resolve value for attribute\n");
      break;
    }

    if (attr->form == DW_FORM_addr) {
      attr->code += file->debug_info.reloc;
    } else if (attr->form == DW_FORM_ref_udata) {
      /* offset from start of its respective CU */
//      printf("ref CU, code is %" PRIx64, attr->code);
      attr->code += (int64_t)(custart - file->debug_info.start);
//      printf(" fixed up to %" PRIx64 "\n", attr->code);
      attr->ptr = (const uint8_t*)cu;
      attr->form = DW_FORM_data8;
    }

    attr->next = die->attrs;
    die->attrs = attr;
    attr = NULL;
  }

  if (has_children) {
    /* go recursive and pull those in now.
     * The first child may be NULL and not indicate a terminator */
    while (1) {
      kid = process_die(file, cu, custart, &data, end, da_offset,
            is_64, addr_size);
      if (kid == NULL) {
        if (STAILQ_FIRST(&die->kids)) {
          break;
        }
        continue;
      }
      STAILQ_INSERT_TAIL(&die->kids, kid, siblings);
      kid->parent = die;
    }
  }

#if 0
  printf("process_die stopping at offset %" PRIx64 "\n",
      data - file->debug_info.start);
#endif
  *datap = data;
  return die;
}

/* Calculate the relocation slide value; it only applies
 * to shared objects (not the main executable) and must
 * be the value of the lowest load address of all the
 * mappings for that object */
static gimli_addr_t calc_reloc(gimli_mapped_object_t f)
{
  int i;
  struct gimli_object_mapping *m;
  gimli_addr_t smallest = 0;

  if (gimli_object_is_executable(f->elf) || f->debug_info.reloc) {
    return f->debug_info.reloc;
  }

  for (i = 0; i < the_proc->nmaps; i++) {
    m = the_proc->mappings[i];

    if (m->objfile == f) {
      if (smallest) {
        if (m->base < smallest) {
          smallest = m->base;
        }
      } else {
        smallest = m->base;
      }
    }
    f->debug_info.reloc = smallest;
  }
#if 0
  printf("Using reloc adjustment for %s: 0x%" PRIx64 "\n",
      m->objfile->objname, f->debug_info.reloc);
#endif
  return f->debug_info.reloc;
}

static int init_debug_info(gimli_mapped_object_t f)
{
  if (f->debug_info.start) return 1;

  if (!get_sect_data(f, ".debug_info", &f->debug_info.start,
        &f->debug_info.end, &f->debug_info.elf)) {
    printf("no debug info for %s\n", f->objname);
    return 0;
  }

  calc_reloc(f);

  return 1;
}

/* insert CU into the appropriate portion of the binary search tree
 * pointed to by root */
static void insert_cu(struct gimli_dwarf_cu **rootp, struct gimli_dwarf_cu *cu)
{
  struct gimli_dwarf_cu *root = *rootp;

  if (!root) {
    *rootp = cu;
    return;
  }
  while (root) {
    if (cu->offset >= root->offset && cu->offset < root->end) {
      printf("CU list already contains %" PRIx64 "\n", cu->offset);
      abort();
    }
    if (cu->offset < root->offset) {
      if (root->left) {
        root = root->left;
        continue;
      }
      root->left = cu;
      return;
    }
    /* must be on the right */
    if (root->right) {
      root = root->right;
      continue;
    }
    root->right = cu;
    return;
  }
}

static struct gimli_dwarf_cu *load_cu(gimli_mapped_object_t f, uint64_t offset)
{
  const uint8_t *data, *next;
  const uint8_t *cuend, *custart;
  gimli_object_file_t elf = NULL;
  uint64_t initlen;
  uint32_t len32;
  uint16_t ver;
  uint64_t da_offset;
  int is_64 = 0;
  uint8_t addr_size, seg_size;
  struct gimli_dwarf_cu *cu, *cuptr;
  struct gimli_dwarf_die *die = NULL;

  if (!init_debug_info(f)) {
    return 0;
  }
  data = f->debug_info.start + offset;

#if 0
  printf("Loading CU @ offset %" PRIx64 " from data of size %" PRIu64 "\n",
      offset,
      f->debug_info.end - f->debug_info.start);
#endif

  if (data >= f->debug_info.end) {
    printf("CU offset %" PRIx64 " it out of bounds\n", offset);
    return 0;
  }

  custart = data;
  memcpy(&len32, data, sizeof(len32));
  data += sizeof(len32);
  if (len32 == 0xffffffff) {
    is_64 = 1;
    memcpy(&initlen, data, sizeof(initlen));
    data += sizeof(initlen);
  } else {
    is_64 = 0;
    initlen = len32;
  }
  cuend = data + initlen;

  memcpy(&ver, data, sizeof(ver));
  data += sizeof(ver);
  if (ver < 2 || ver > 3) {
    printf("%s: CU @ offset 0x%" PRIx64 " with dwarf version %d; ending processing\n",
        f->objname, offset, ver);
    abort();
    return 0;
  }

  if (is_64) {
    memcpy(&da_offset, data, sizeof(da_offset));
    data += sizeof(da_offset);
  } else {
    memcpy(&len32, data, sizeof(len32));
    data += sizeof(len32);
    da_offset = len32;
  }

  memcpy(&addr_size, data, sizeof(addr_size));
  data += sizeof(addr_size);

  cu = calloc(1, sizeof(*cu));
  cu->offset = offset;
  cu->end = cuend - f->debug_info.start;
  cu->da_offset = da_offset;
  STAILQ_INIT(&cu->dies);

  /* insert into the cu tree */
  insert_cu(&f->debug_info.cus, cu);
#if 0
  printf("Recording CU %" PRIx64 " - %" PRIx64 " @ %p\n",
      cu->offset, cu->end, cu);
#endif

  /* now we have a series of Debugging Information Entries (DIE) */
  while (data < cuend) {
    die = process_die(f, cu, custart, &data, cuend,
        da_offset, is_64, addr_size);
    if (!die) {
      continue;
    }
    STAILQ_INSERT_TAIL(&cu->dies, die, siblings);
  }

#if 0
  printf("abbr.map is %d in size, data size %" PRIu64 " approx %" PRIu64 " per entry\n",
      gimli_hash_size(f->abbr.map), f->abbr.end - f->abbr.start,
      (f->abbr.end - f->abbr.start) / gimli_hash_size(f->abbr.map));
  gimli_hash_diagnose(f->abbr.map);
#endif

  return cu;
}

/* searches the CU binary search tree for the requested offset */
static struct gimli_dwarf_cu *find_cu(
  gimli_mapped_object_t f,
  uint64_t offset)
{
  struct gimli_dwarf_die *die = NULL;
  struct gimli_dwarf_cu *cu;

  /* search binary tree */
  cu = f->debug_info.cus;
  while (cu) {
    if (offset >= cu->offset && offset < cu->end) {
      return cu;
    }
    if (offset < cu->offset) {
      cu = cu->left;
    } else {
      cu = cu->right;
    }
  }
  return NULL;
}

static struct gimli_dwarf_die *find_die_r(struct gimli_dwarf_die *die, uint64_t offset)
{
  struct gimli_dwarf_die *kid, *res;

  if (die->offset == offset) {
    return die;
  }
  STAILQ_FOREACH(kid, &die->kids, siblings) {
    if (kid->offset == offset) {
      return kid;
    }
  }
  STAILQ_FOREACH(kid, &die->kids, siblings) {
    res = find_die_r(kid, offset);
    if (res) {
      return res;
    }
  }
  return NULL;
}

struct gimli_dwarf_die *gimli_dwarf_get_die(
  gimli_mapped_object_t f,
  uint64_t offset)
{
  struct gimli_dwarf_die *die = NULL, *res;
  struct gimli_dwarf_cu *cu;

  cu = find_cu(f, offset);
  if (!cu) {
    /* not found; will need to go out to disk to read it */
    cu = load_cu(f, offset);
  }

  if (cu) {
    STAILQ_FOREACH(die, &cu->dies, siblings) {
      res = find_die_r(die, offset);
      if (res) {
        return res;
      }
    }
  }

  printf("get_die: %" PRIx64 " MISSING cu=%p %" PRIx64 "-%" PRIx64 "\n",
      offset, cu, cu->offset, cu->end);
  return NULL;
}

struct gimli_dwarf_attr *gimli_dwarf_die_get_attr(
  struct gimli_dwarf_die *die, uint64_t attrcode)
{
  struct gimli_dwarf_attr *attr;

  for (attr = die->attrs; attr; attr = attr->next) {
    if (attr->attr == attrcode) {
      return attr;
    }
  }
  return NULL;
}

static int gimli_dwarf_die_get_uint64_t_attr(
  struct gimli_dwarf_die *die, uint64_t attrcode, uint64_t *val)
{
  struct gimli_dwarf_attr *attr;

  attr = gimli_dwarf_die_get_attr(die, attrcode);
  if (attr) {
    *val = attr->code;
    return 1;
  }
  return 0;
}

static int sort_compare_arange(const void *A, const void *B)
{
  struct dw_die_arange *a = (struct dw_die_arange*)A;
  struct dw_die_arange *b = (struct dw_die_arange*)B;

  return a->addr - b->addr;
}

/* Load the DIE location data from an object file */
static int load_arange(struct gimli_object_mapping *m)
{
  struct gimli_section_data *s = NULL;
  const uint8_t *data, *end, *next;
  gimli_object_file_t elf = NULL;
  uint64_t reloc = 0;
  uint32_t len32;
  int is_64 = 0;
  uint64_t initlen;
  uint16_t ver;
  uint64_t di_offset;
  uint8_t addr_size, seg_size;
  struct gimli_dwarf_die *die;

  if (!m->objfile->elf) {
    /* (deleted) */
    return 0;
  }
  if (!get_sect_data(m->objfile, ".debug_aranges", &data, &end, &elf)) {
    return 0;
  }

  reloc = calc_reloc(m->objfile);

  while (data < end) {
    uint64_t mask;

    /* read header */

    memcpy(&len32, data, sizeof(len32));
    data += sizeof(len32);
    if (len32 == 0xffffffff) {
      is_64 = 1;
      memcpy(&initlen, data, sizeof(initlen));
      data += sizeof(initlen);
    } else {
      is_64 = 0;
      initlen = len32;
    }
    next = data + initlen;

    memcpy(&ver, data, sizeof(ver));
    data += sizeof(ver);

    if (is_64) {
      memcpy(&di_offset, data, sizeof(di_offset));
      data += sizeof(di_offset);
    } else {
      memcpy(&len32, data, sizeof(len32));
      data += sizeof(len32);
      di_offset = len32;
    }

    memcpy(&addr_size, data, sizeof(addr_size));
    data += sizeof(addr_size);

    memcpy(&seg_size, data, sizeof(seg_size));
    data += sizeof(seg_size);

    if (seg_size) {
      printf("DWARF: I don't support segmented debug_aranges data\n");
      return 0;
    }

//    printf("arange: ver %d addr_size %d seg %d\n", ver, addr_size, seg_size);

    /* align to double-addr-size boundary */
    mask = (2 * addr_size) - 1;
    data += mask;
    data = (void*)(intptr_t)((intptr_t)data & ~mask);

    while (data < next) {
      /* now we have a series of tuples */
      gimli_addr_t addr;
      uint64_t l;
      struct dw_die_arange *arange;

      if (addr_size == 8) {
        memcpy(&l, data, sizeof(l));
        data += sizeof(l);
        addr = l;
      } else {
        memcpy(&len32, data, sizeof(len32));
        data += sizeof(len32);
        addr = len32;
      }

      if (data >= next) break;

      if (addr_size == 8) {
        memcpy(&l, data, sizeof(l));
        data += sizeof(l);
      } else {
        memcpy(&len32, data, sizeof(len32));
        data += sizeof(len32);
        l = len32;
      }

      if (addr == 0 && l == 0) {
        break;
      }

      addr += reloc;

      if (m->objfile->num_arange + 1 >= m->objfile->alloc_arange) {
        m->objfile->alloc_arange = m->objfile->alloc_arange ? m->objfile->alloc_arange * 2 : 1024;
        m->objfile->arange = realloc(m->objfile->arange, m->objfile->alloc_arange * sizeof(*arange));
      }
      arange = &m->objfile->arange[m->objfile->num_arange++];
      arange->addr = addr;
      arange->len = l;
      arange->di_offset = di_offset;

//      printf("arange: addr=" PTRFMT " 0x%" PRIx64 "\n", addr, l);
    }
    data = next;
  }

  /* ensure ascending order */
  qsort(m->objfile->arange, m->objfile->num_arange, sizeof(struct dw_die_arange),
      sort_compare_arange);
//printf("sorting %d arange in %s\n", m->objfile->num_arange, m->objfile->objname);

  return 1;
}

static int search_compare_arange(const void *K, const void *R)
{
  struct dw_die_arange *arange = (struct dw_die_arange*)R;
  gimli_addr_t pc = *(gimli_addr_t*)K;

  if (pc < arange->addr) {
    return -1;
  }

  if (pc < arange->addr + arange->len) {
    return 0;
  }

  return 1;
}

/* Given a CU, locate the DIE corresponding to the provided data address */
static struct gimli_dwarf_die *find_var_die_for_addr(gimli_proc_t proc,
    struct gimli_object_mapping *m,
    struct gimli_dwarf_cu *cu, gimli_addr_t addr)
{
  struct gimli_dwarf_die *die, *kid;
  struct gimli_unwind_cursor cur;
  uint64_t frame_base = 0;
  uint64_t comp_unit_base = 0;
  int is_stack = 0;
  struct gimli_dwarf_attr *frame_base_attr;

  memset(&cur, 0, sizeof(cur));
  cur.proc = proc;
#ifdef __MACH__
  addr -= m->objfile->base_addr;
#endif

  STAILQ_FOREACH(die, &cu->dies, siblings) {
    uint64_t lopc, hipc;

    if (die->tag != DW_TAG_compile_unit) {
      printf("DIE is not a compile unit!? tag=0x%" PRIx64 "\n", die->tag);
      continue;
    }

    gimli_dwarf_die_get_uint64_t_attr(die,
        DW_AT_low_pc, &comp_unit_base);

    frame_base_attr = gimli_dwarf_die_get_attr(die, DW_AT_frame_base);
    if (frame_base_attr) {
      switch (frame_base_attr->form) {
        case DW_FORM_block:
          dw_eval_expr(&cur, (uint8_t*)frame_base_attr->ptr, frame_base_attr->code,
              0, &frame_base, NULL, &is_stack);
          break;
        case DW_FORM_data8:
          dw_calc_location(&cur, comp_unit_base, m,
              frame_base_attr->code, &frame_base, NULL, &is_stack);
          break;
        default:
          printf("Unhandled frame base form 0x%" PRIx64 "\n",
              frame_base_attr->form);
          return 0;
      }
    }

    /* this is the die for the compilation unit; we need to walk
     * through it and find the data it contains */
    STAILQ_FOREACH(kid, &die->kids, siblings) {
      uint64_t res = 0;
      is_stack = 1;
      struct gimli_dwarf_attr *location, *type, *name;

      if (kid->tag != DW_TAG_variable && kid->tag != DW_TAG_constant) {
//        printf("skipping kid with tag 0x%" PRIx64 "\n", kid->tag);
        continue;
      }

      location = gimli_dwarf_die_get_attr(kid, DW_AT_location);
      if (!location) {
        continue;
      }

      switch (location->form) {
        case DW_FORM_block:
          if (!dw_eval_expr(&cur, (uint8_t*)location->ptr, location->code,
                frame_base, &res, NULL, &is_stack)) {
            res = 0;
          }
          break;
        case DW_FORM_data8:
          if (!dw_calc_location(&cur, comp_unit_base, m,
                location->code, &res, NULL, &is_stack)) {
            res = 0;
          }
          break;
        default:
          printf("Unhandled location form 0x%" PRIx64 "\n", location->form);
          res = 0;
      }

      if (res == addr) {
        return kid;
      }
    }
  }

  /* no joy */
  return NULL;
}

/* attempt to locate a DW_TAG_variable or DW_TAG_constant die
 * corresponding to the provided data address.
 * We make this attempt using the debug_aranges data, but it
 * may not be successful, as gcc doesn't appear to generate
 * it for the data segment, despite the DWARF standard indicating
 * that it can be used in that fashion.
 * In that case, we have to fall back to crawling the CU data.
 * */
static struct gimli_dwarf_die *gimli_dwarf_get_die_for_data(
    gimli_proc_t proc, gimli_addr_t pc)
{
  struct gimli_object_mapping *m;
  struct gimli_dwarf_cu *cu;
  struct dw_die_arange *arange;
  struct gimli_dwarf_die *die;
  gimli_mapped_object_t file;
  const uint8_t *cuptr;
  uint64_t off;

  m = gimli_mapping_for_addr(proc, pc);
  if (!m) {
    return NULL;
  }
  file = m->objfile;

  if (!file->elf) {
    return NULL;
  }

  if (!file->arange && !load_arange(m)) {
    return NULL;
  }
#ifdef __MACH__
  pc -= m->objfile->base_addr;
#endif
  arange = bsearch(&pc, file->arange, file->num_arange,
      sizeof(*arange), search_compare_arange);
#ifdef __MACH__
  pc += m->objfile->base_addr;
#endif

  if (arange) {
    /* arange gives us a pointer to the CU */
    cu = find_cu(file, arange->di_offset);
    if (!cu) {
      cu = load_cu(file, arange->di_offset);
    }
    if (cu) {
      return find_var_die_for_addr(proc, m, cu, pc);
    }
  }

  cuptr = file->debug_info.start;
  while (cuptr < file->debug_info.end) {
    off = cuptr - file->debug_info.start;
    cu = find_cu(file, off);
    if (!cu) {
      cu = load_cu(file, off);
    }
    if (!cu) break;
    cuptr = file->debug_info.start + cu->end;

    die = find_var_die_for_addr(proc, m, cu, pc);
    if (die) return die;
  }

  return NULL;
}

struct gimli_dwarf_die *gimli_dwarf_get_die_for_pc(gimli_proc_t proc, gimli_addr_t pc)
{
  struct gimli_object_mapping *m;
  struct gimli_dwarf_die *die, *kid;
  struct gimli_dwarf_cu *cu;
  struct dw_die_arange *arange;

  m = gimli_mapping_for_addr(proc, pc);
  if (!m) {
    return NULL;
  }

  if (!m->objfile->elf) {
    return NULL;
  }

  if (!m->objfile->arange && !load_arange(m)) {
    return NULL;
  }
#ifdef __MACH__
  pc -= m->objfile->base_addr;
#endif

  arange = bsearch(&pc, m->objfile->arange, m->objfile->num_arange,
      sizeof(*arange), search_compare_arange);
  if (!arange) {
//    printf("no arange for pc " PTRFMT "\n", pc);
    return NULL;
  }
  /* arange gives us a pointer to the CU */
  cu = find_cu(m->objfile, arange->di_offset);
  if (!cu) {
    cu = load_cu(m->objfile, arange->di_offset);
  }
  if (!cu) {
//    printf("no CU for pc " PTRFMT " arange said off %" PRIx64 "\n", pc, arange->di_offset);
    return NULL;
  }

//  printf("got CU " PTRFMT " - " PTRFMT " arange said off %" PRIx64 "\n", cu->offset, cu->end, arange->di_offset);

  STAILQ_FOREACH(die, &cu->dies, siblings) {
    uint64_t lopc, hipc;

    if (die->tag != DW_TAG_compile_unit) {
      printf("DIE is not a compile unit!? tag=0x%" PRIx64 "\n", die->tag);
      continue;
    }

    /* this is the die for the compilation unit; we need to walk
     * through it and find the subprogram that matches */
    STAILQ_FOREACH(kid, &die->kids, siblings) {
      lopc = arange->addr;
      hipc = arange->len;

      if (kid->tag != DW_TAG_subprogram) {
        continue;
      }

      if (!gimli_dwarf_die_get_uint64_t_attr(kid, DW_AT_low_pc, &lopc)) {
        continue;
      }
      if (!gimli_dwarf_die_get_uint64_t_attr(kid, DW_AT_high_pc, &hipc)) {
        continue;
      }

      if (pc >= lopc && pc <= hipc) {
        return kid;
      }
    }
  }

  /* no joy */
  return NULL;
}

int gimli_dwarf_read_value(gimli_proc_t proc,
    gimli_addr_t addr, int is_stack, void *out, uint64_t size)
{
  uint32_t u32;
  uint16_t u16;
  uint8_t u8;
  uint64_t v;

  if (is_stack) {
    return gimli_read_mem(proc, addr, out, size) == size;
  }
  /* otherwise, addr actually contains the value */
  v = addr;
  switch (size) {
    case 8:
      memcpy(out, &v, size);
      return 1;
    case 4:
      u32 = v;
      memcpy(out, &u32, size);
      return 1;
    case 2:
      u16 = v;
      memcpy(out, &u16, size);
      return 1;
    case 1:
      u8 = v;
      memcpy(out, &u8, size);
      return 1;
  }
  printf("Can't handle register stored values > 8 bytes in size\n");
  return 0;
}


static gimli_type_t load_type(
    gimli_mapped_object_t file,
    struct gimli_dwarf_attr *type);

static void populate_struct_or_union(
    gimli_type_t t,
    gimli_mapped_object_t file,
    struct gimli_dwarf_die *die)
{
  struct gimli_dwarf_attr *loc, *type, *mname;
  uint64_t root = 0;
  struct gimli_unwind_cursor cur;
  int is_stack = 1;
  uint64_t size, offset;
  gimli_type_t memt;


  memset(&cur, 0, sizeof(cur));

  STAILQ_FOREACH(die, &die->kids, siblings) {
    if (die->tag != DW_TAG_member) continue;

    loc = gimli_dwarf_die_get_attr(die, DW_AT_data_member_location);
    is_stack = 1;
    /* assume start of struct */
    root = 0;
    if (loc && loc->form == DW_FORM_block) {
      if (!dw_eval_expr(&cur, (uint8_t*)loc->ptr, loc->code, 0,
            &root, &root, &is_stack)) {
        printf("unable to evaluate member location\n");
        root = 0;
      }
    } else if (loc) {
      printf("Unhandled location form 0x%" PRIx64 " for struct member\n",
          loc->form);
    }
    type = gimli_dwarf_die_get_attr(die, DW_AT_type);
    mname = gimli_dwarf_die_get_attr(die, DW_AT_name);

    memt = load_type(file, type);
    if (!memt) {
#if 0
      printf("failed to load type info for member %s %" PRIx64 "\n", mname->ptr,
          type->code);
#endif
      continue;
    }
    offset = 0;
    if (gimli_dwarf_die_get_uint64_t_attr(die, DW_AT_bit_size, &size)) {
      uint64_t bytesize;

      if (!gimli_dwarf_die_get_uint64_t_attr(die, DW_AT_bit_offset, &offset)) {
        offset = 1;
      }
      /* convert to bit offset from start of storage */
      /* FIXME: check this for big endian systems */
      if (!gimli_dwarf_die_get_uint64_t_attr(die, DW_AT_byte_size, &bytesize)) {
        bytesize = gimli_type_size(memt)/8;
      }
//printf("bitfield: offset=%" PRIu64 " from MSB, bytesize=%" PRIu64 " bits=%" PRIu64 "\n", offset, bytesize, size);
#if !WORDS_BIGENDIAN
      offset = (bytesize * 8) - (offset + size);
//printf("    -> offset=%" PRIu64 " from lowest byte\n", offset);
#endif

    } else {
      size = gimli_type_size(memt);
    }

    gimli_type_add_member(t, mname ? (char*)mname->ptr : NULL,
        memt, size, (root * 8) + offset);
  }
}

static gimli_type_t array_dim(gimli_mapped_object_t file,
    struct gimli_dwarf_die *die, gimli_type_t eletype)
{
  struct gimli_type_arinfo info;
  uint64_t uval;
  struct gimli_dwarf_attr *type;
  gimli_type_t t, target;

  memset(&info, 0, sizeof(info));

  if (STAILQ_NEXT(die, siblings)) {
    info.contents = array_dim(file, STAILQ_NEXT(die, siblings), eletype);
  } else {
    info.contents = eletype;
  }

  if (gimli_dwarf_die_get_uint64_t_attr(die, DW_AT_upper_bound, &uval)) {
    info.nelems = uval + 1;
  } else {
    info.nelems = 0;
  }

  return gimli_type_new_array(file->types, &info);
}

static gimli_type_t populate_array(gimli_mapped_object_t file,
    struct gimli_dwarf_die *die)
{
  struct gimli_type_arinfo info;
  gimli_type_t t;
  struct gimli_dwarf_attr *type;

  if (!STAILQ_FIRST(&die->kids) || STAILQ_FIRST(&die->kids)->tag != DW_TAG_subrange_type) {
    printf("cannot determine array bounds!\n");
    return NULL;
  }

  /* find target type */
  type = gimli_dwarf_die_get_attr(die, DW_AT_type);
  t = load_type(file, type);
  if (!t) {
    return NULL;
  }

  t = array_dim(file, STAILQ_FIRST(&die->kids), t);
  return t;
}

static gimli_type_t load_void(gimli_mapped_object_t file)
{
  struct gimli_type_encoding enc;

  memset(&enc, 0, sizeof(enc));
  enc.bits = 8;
  return gimli_type_new_integer(file->types, "void", &enc);
}

static gimli_type_t populate_func(gimli_mapped_object_t file,
    const char *name,
    struct gimli_dwarf_die *die)
{
  struct gimli_dwarf_die *kid;
  struct gimli_dwarf_attr *type, *pname;
  gimli_type_t rettype = NULL, t;
  uint32_t flags = 0;

  type = gimli_dwarf_die_get_attr(die, DW_AT_type);
  if (type) {
    rettype = load_type(file, type);
  } else {
    rettype = load_void(file);
  }

  /* are we variadic? */
  STAILQ_FOREACH(kid, &die->kids, siblings) {
    if (kid->tag == DW_TAG_unspecified_parameters) {
      flags = GIMLI_FUNC_VARARG;
      break;
    }
  }

  t = gimli_type_new_function(file->types, name, flags, rettype);

  STAILQ_FOREACH(kid, &die->kids, siblings) {
    if (kid->tag == DW_TAG_unspecified_parameters) {
      continue;
    }
    if (kid->tag == DW_TAG_formal_parameter) {
      gimli_type_t ptype;
      pname = gimli_dwarf_die_get_attr(kid, DW_AT_name);

      type = gimli_dwarf_die_get_attr(kid, DW_AT_type);
      if (type) {
        ptype = load_type(file, type);
        gimli_type_function_add_parameter(t, pname ? pname->ptr : NULL, ptype);
      }
      continue;
    }
  }

  return t;
}

static int populate_enum(gimli_type_t t,
    gimli_mapped_object_t file,
    struct gimli_dwarf_die *parent)
{
  struct gimli_dwarf_die *die;
  struct gimli_dwarf_attr *name = NULL;

  STAILQ_FOREACH(die, &parent->kids, siblings) {
    struct gimli_dwarf_attr *cv;

    if (die->tag != DW_TAG_enumerator) {
      printf("unexpected tag 0x%" PRIx64 " in enumeration_type\n",
          die->tag);
      return 0;
    }
    name = gimli_dwarf_die_get_attr(die, DW_AT_name);
    if (!name) {
      printf("expected name for enumerator!\n");
      return 0;
    }

    cv = gimli_dwarf_die_get_attr(die, DW_AT_const_value);
    if (!cv) {
      printf("missing const_value for enumerator\n");
      return 0;
    }
    gimli_type_enum_add(t, (char*)name->ptr, (int)cv->code);
  }
  return 1;
}

static gimli_type_t load_type_die(
    gimli_mapped_object_t file,
    struct gimli_dwarf_die *die)
{
  gimli_type_t t = NULL, target = NULL;
  uint64_t ate, size = 0;
  struct gimli_type_encoding enc;
  const char *type_name = NULL;
  struct gimli_dwarf_attr *name = NULL;
  struct gimli_dwarf_attr *type = NULL;

  if (file->die_to_type) {
    if (gimli_hash_find_ptr(file->die_to_type, die, (void**)&t)) {
      return t;
    }
  }

  if (!file->types) {
    file->types = gimli_type_collection_new();
  }
  if (!file->die_to_type) {
    file->die_to_type = gimli_hash_new_size(NULL, GIMLI_HASH_PTR_KEYS, 0);
  }

  name = gimli_dwarf_die_get_attr(die, DW_AT_name);
  if (name) {
    type_name = (char*)name->ptr;
  } else {
    type_name = NULL;
  }

  switch (die->tag) {
    case DW_TAG_base_type:
      if (!gimli_dwarf_die_get_uint64_t_attr(die, DW_AT_encoding, &ate)) {
        ate = DW_ATE_signed;
      }
      memset(&enc, 0, sizeof(enc));

      gimli_dwarf_die_get_uint64_t_attr(die, DW_AT_byte_size, &size);
      enc.bits = size * 8;

      switch (ate) {
        case DW_ATE_unsigned_char:
          enc.format = GIMLI_INT_CHAR;
          t = gimli_type_new_integer(file->types, type_name, &enc);
          break;
        case DW_ATE_unsigned:
          t = gimli_type_new_integer(file->types, type_name, &enc);
          break;
        case DW_ATE_signed:
          enc.format = GIMLI_INT_SIGNED;
          t = gimli_type_new_integer(file->types, type_name, &enc);
          break;
        case DW_ATE_signed_char:
          enc.format = GIMLI_INT_SIGNED|GIMLI_INT_CHAR;
          t = gimli_type_new_integer(file->types, type_name, &enc);
          break;
        case DW_ATE_boolean:
          enc.format = GIMLI_INT_BOOL;
          t = gimli_type_new_integer(file->types, type_name, &enc);
          break;
        case DW_ATE_float:
          switch (size) {
            case sizeof(double):
              enc.format = GIMLI_FP_DOUBLE;
              break;
            case sizeof(float):
              enc.format = GIMLI_FP_SINGLE;
              break;
            case sizeof(long double):
              enc.format = GIMLI_FP_LONG_DOUBLE;
              break;
          }
          t = gimli_type_new_float(file->types, type_name, &enc);
          break;
        case DW_ATE_complex_float:
          enc.format = GIMLI_FP_COMPLEX;
          t = gimli_type_new_float(file->types, type_name, &enc);
          break;
        case DW_ATE_imaginary_float:
          enc.format = GIMLI_FP_IMAGINARY;
          t = gimli_type_new_float(file->types, type_name, &enc);
          break;
        default:
          printf("unhandled DW_AT_encoding for base_type: 0x%" PRIx64 "\n", ate);
      }
      break;

    case DW_TAG_pointer_type:
      type = gimli_dwarf_die_get_attr(die, DW_AT_type);
      if (type) {
        target = load_type(file, type);
      } else {
        target = load_void(file);
      }
      if (target) {
        t = gimli_type_new_pointer(file->types, target);
      }
      break;
    case DW_TAG_const_type:
      type = gimli_dwarf_die_get_attr(die, DW_AT_type);
      if (type) {
        target = load_type(file, type);
        if (target) {
          t = gimli_type_new_const(file->types, target);
        }
      }
      break;
    case DW_TAG_volatile_type:
      type = gimli_dwarf_die_get_attr(die, DW_AT_type);
      if (type) {
        target = load_type(file, type);
        if (target) {
          t = gimli_type_new_volatile(file->types, target);
        }
      }
      break;
    case DW_TAG_restrict_type:
      type = gimli_dwarf_die_get_attr(die, DW_AT_type);
      if (type) {
        target = load_type(file, type);
        if (target) {
          t = gimli_type_new_restrict(file->types, target);
        }
      }
      break;

    case DW_TAG_typedef:
      type = gimli_dwarf_die_get_attr(die, DW_AT_type);
      if (type) {
        target = load_type(file, type);
        if (target) {
          t = gimli_type_new_typedef(file->types, target, type_name);
        }
      }
      break;

    case DW_TAG_structure_type:
      t = gimli_type_new_struct(file->types, type_name);
      gimli_hash_insert_ptr(file->die_to_type, die, t);
      populate_struct_or_union(t, file, die);
      return t;

    case DW_TAG_union_type:
      t = gimli_type_new_union(file->types, type_name);
      gimli_hash_insert_ptr(file->die_to_type, die, t);
      populate_struct_or_union(t, file, die);
      return t;


    case DW_TAG_array_type:
      t = populate_array(file, die);
      break;

    case DW_TAG_enumeration_type:
      memset(&enc, 0, sizeof(enc));
      gimli_dwarf_die_get_uint64_t_attr(die, DW_AT_byte_size, &size);
      enc.bits = size * 8;

      t = gimli_type_new_enum(file->types, type_name, &enc);
      if (!populate_enum(t, file, die)) {
        return NULL;
      }
      break;

    case DW_TAG_subroutine_type:
      t = populate_func(file, type_name, die);
      break;

    default:
      printf("unhandled tag 0x%" PRIx64 " in load_type (%s)\n", die->tag, type_name);
      return NULL;
  }

  if (t) {
    gimli_hash_insert_ptr(file->die_to_type, die, t);
  }

  return t;
}

static gimli_type_t load_type(
    gimli_mapped_object_t file,
    struct gimli_dwarf_attr *type)
{
  struct gimli_dwarf_die *die;

  die = gimli_dwarf_get_die(file, type->code);
  if (!die) {
    return NULL;
  }

  return load_type_die(file, die);
}

static void load_types_in_die(gimli_mapped_object_t file,
    struct gimli_dwarf_die *die)
{
  struct gimli_dwarf_die *kid;

  switch (die->tag) {
    case DW_TAG_base_type:
    case DW_TAG_pointer_type:
    case DW_TAG_const_type:
    case DW_TAG_volatile_type:
    case DW_TAG_restrict_type:
    case DW_TAG_typedef:
    case DW_TAG_structure_type:
    case DW_TAG_union_type:
    case DW_TAG_array_type:
    case DW_TAG_enumeration_type:
    case DW_TAG_subroutine_type:
      load_type_die(file, die);
      break;
  }

  STAILQ_FOREACH(kid, &die->kids, siblings) {
    load_types_in_die(file, kid);
  }
}

/* pull in all the CU's and register all the types we find;
 * this is relatively expensive but we only do it based
 * on user request; a module has to ask for a type by name
 * that we haven't already loaded */
void gimli_dwarf_load_all_types(gimli_mapped_object_t file)
{
  struct gimli_dwarf_cu *cu;
  struct gimli_dwarf_die *die;
  const uint8_t *cuptr;
  uint64_t off;

  if (!init_debug_info(file)) {
    return;
  }

  cuptr = file->debug_info.start;
  while (cuptr < file->debug_info.end) {
    off = cuptr - file->debug_info.start;
    cu = find_cu(file, off);
    if (!cu) {
      cu = load_cu(file, off);
    }
    if (!cu) break;
    cuptr = file->debug_info.start + cu->end;

    /* now walk the DIEs and map the types */
    STAILQ_FOREACH(die, &cu->dies, siblings) {
      load_types_in_die(file, die);
    }
  }
}

/* Locate the DIE for a data address and load its type
 * information */
gimli_type_t gimli_dwarf_load_type_for_data(gimli_proc_t proc,
    gimli_addr_t addr)
{
  struct gimli_dwarf_die *die;
  struct gimli_object_mapping *m;
  gimli_mapped_object_t file;
  struct gimli_dwarf_attr *type;

  die = gimli_dwarf_get_die_for_data(proc, addr);
  if (!die) {
    return NULL;
  }

  m = gimli_mapping_for_addr(proc, addr);
  file = m->objfile;

  type = gimli_dwarf_die_get_attr(die, DW_AT_type);
  if (!type) {
    return NULL;
  }

  return load_type(file, type);
}

static void load_var(
    gimli_stack_frame_t frame,
    struct gimli_dwarf_die *die,
    uint64_t frame_base, uint64_t comp_unit_base,
    struct gimli_object_mapping *m)
{
  uint64_t res = 0;
  int is_stack = 1;
  struct gimli_dwarf_attr *location, *type, *name;
  gimli_var_t var;

  type = gimli_dwarf_die_get_attr(die, DW_AT_type);
  if (!type) {
    return;
  }

  location = gimli_dwarf_die_get_attr(die, DW_AT_location);
  name = gimli_dwarf_die_get_attr(die, DW_AT_name);

  if (location) {
    switch (location->form) {
      case DW_FORM_block:
        if (!dw_eval_expr(&frame->cur, (uint8_t*)location->ptr, location->code,
              frame_base, &res, NULL, &is_stack)) {
          res = 0;
        }
        break;
      case DW_FORM_data8:
        if (!dw_calc_location(&frame->cur, comp_unit_base, m,
              location->code, &res, NULL, &is_stack)) {
          res = 0;
        }
        break;
      default:
        printf("Unhandled location form 0x%" PRIx64 "\n", location->form);
    }
  } else if (name) {
    /* no location defined, so assume the compiler optimized it away */
    res = 0; /* we treat NULL address as "optimized out" */
  } else {
    return;
  }

  var = calloc(1, sizeof(*var));
  var->varname = name ? name->ptr : NULL;
  var->addr = res;
  var->proc = frame->cur.proc;
  var->type = load_type(m->objfile, type);
  var->is_param = (die->tag == DW_TAG_formal_parameter) ?
    GIMLI_WANT_PARAMS : GIMLI_WANT_LOCALS;

  STAILQ_INSERT_TAIL(&frame->vars, var, vars);
}

/* load DWARF DIEs to collect information about variables */
int gimli_dwarf_load_frame_var_info(gimli_stack_frame_t frame)
{
  struct gimli_dwarf_die *die, *kid;
  uint64_t frame_base = 0;
  uint64_t comp_unit_base = 0;
  struct gimli_dwarf_attr *frame_base_attr;
  struct gimli_object_mapping *m;
  gimli_proc_t proc = frame->cur.proc;
  gimli_addr_t pc = (gimli_addr_t)frame->cur.st.pc;

  if (frame->loaded_vars) return 1;
  frame->loaded_vars = 1;

  die = gimli_dwarf_get_die_for_pc(proc, pc);
  if (!die) {
//    printf("no DIE for pc=" PTRFMT "\n", pc);
    return 0;
  }
  m = gimli_mapping_for_addr(proc, pc);

  if (die->parent->tag == DW_TAG_compile_unit) {
    gimli_dwarf_die_get_uint64_t_attr(die->parent,
      DW_AT_low_pc, &comp_unit_base);
  }

  frame_base_attr = gimli_dwarf_die_get_attr(die, DW_AT_frame_base);
  if (frame_base_attr) {
    int is_stack = 0;

    switch (frame_base_attr->form) {
      case DW_FORM_block:
        dw_eval_expr(&frame->cur, (uint8_t*)frame_base_attr->ptr, frame_base_attr->code,
            0, &frame_base, NULL, &is_stack);
        break;
      case DW_FORM_data8:
        dw_calc_location(&frame->cur, comp_unit_base, m,
            frame_base_attr->code, &frame_base, NULL, &is_stack);
        break;
      default:
        printf("Unhandled frame base form 0x%" PRIx64 "\n",
            frame_base_attr->form);
        return 0;
    }
  }

  STAILQ_FOREACH(kid, &die->kids, siblings) {
    if (kid->tag == DW_TAG_formal_parameter || kid->tag == DW_TAG_variable) {
      load_var(frame, kid, frame_base, comp_unit_base, m);
    }
  }

  return 1;
}

/* vim:ts=2:sw=2:et:
 */