summaryrefslogtreecommitdiff
path: root/libdw/c++/dwarf
blob: 13257421db4721e802b492fbfa83645cd755df44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
/* -*- C++ -*- interfaces for libdw.
   Copyright (C) 2009 Red Hat, Inc.
   This file is part of Red Hat elfutils.

   Red Hat elfutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by the
   Free Software Foundation; version 2 of the License.

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

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

   In addition, as a special exception, Red Hat, Inc. gives You the
   additional right to link the code of Red Hat elfutils with code licensed
   under any Open Source Initiative certified open source license
   (http://www.opensource.org/licenses/index.php) which requires the
   distribution of source code with any binary distribution and to
   distribute linked combinations of the two.  Non-GPL Code permitted under
   this exception must only link to the code of Red Hat elfutils through
   those well defined interfaces identified in the file named EXCEPTION
   found in the source code files (the "Approved Interfaces").  The files
   of Non-GPL Code may instantiate templates or use macros or inline
   functions from the Approved Interfaces without causing the resulting
   work to be covered by the GNU General Public License.  Only Red Hat,
   Inc. may make changes or additions to the list of Approved Interfaces.
   Red Hat's grant of this exception is conditioned upon your not adding
   any new exceptions.  If you wish to add a new Approved Interface or
   exception, please contact Red Hat.  You must obey the GNU General Public
   License in all respects for all of the Red Hat elfutils code and other
   code used in conjunction with Red Hat elfutils except the Non-GPL Code
   covered by this exception.  If you modify this file, you may extend this
   exception to your version of the file, but you are not obligated to do
   so.  If you do not wish to provide this exception without modification,
   you must delete this exception statement from your version and license
   this file solely under the GPL without exception.

   Red Hat elfutils is an included package of the Open Invention Network.
   An included package of the Open Invention Network is a package for which
   Open Invention Network licensees cross-license their patents.  No patent
   license is granted, either expressly or impliedly, by designation as an
   included package.  Should you wish to participate in the Open Invention
   Network licensing program, please visit www.openinventionnetwork.com
   <http://www.openinventionnetwork.com>.  */

#ifndef _ELFUTILS_DWARF
#define _ELFUTILS_DWARF	1

#include "libdw.h"
#include "dwarf.h"
#include "subr.hh"
#include <stdexcept>

#include <list>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <algorithm>
#include <functional>
#include <tr1/unordered_map>

/* Abstractly, one DWARF object file consists of a few containers.
   (We omit .debug_frame for now.  It does not interact with the others.)

   1. list of compilation units		(.debug_info)
   2. map of PC ranges to CU		(.debug_aranges)
   3. map of global names to CU+DIE	(.debug_pubnames)
   4. map of type names to CU+DIE	(.debug_pubtypes)

   These maps refer to the CUs in .debug_info and optimize lookups
   compared to simple iteration.

   A compile_unit is a debug_info_entry.
   A debug_info_entry consists of a tag (int/enum), and two containers:
   children and attributes.  The attributes are an unordered map of name
   (int/enum) to attribute value (complex variant record).  Children are
   in an ordered list, each also a debug_info_entry.

   dwarf.compile_units ()		works like list<compile_unit>
	-> compile_unit : debug_info_entry
		.attributes ()		like unordered_map<int, attr_value>
		.children ()		works like list<debug_info_entry>
			-> debug_info_entry
				.attributes ()
				.children ()

  A compile_unit is not deeply special, it's just a debug_info_entry.
  It has its own class just for some convenience methods that only
  make sense for a compile_unit DIE.

  This is the "logical" view of the file, grafting and eliding parts of the
  raw information that are purely the structural elements of DWARF and not
  part of the abstract semantics.  In the file reader (elfutils::dwarf),
  these containers form a layer above the raw containers that expose the
  file data directly (as the libdw C interfaces do).

  dwarf.raw_compile_units ()		works like list<compile_unit>
	-> compile_unit : debug_info_entry
		.raw_attributes ()	like unordered_map<int, attr_value>
		.raw_children ()	works like list<debug_info_entry>
			-> debug_info_entry
				.raw_attributes ()
				.raw_children ()

  compile_units () elides DW_TAG_partial_unit members,
  raw_compile_units () includes them.

  attributes () elides DW_AT_sibling, raw_attributes () includes it.

  raw_children () reports DW_TAG_imported_unit as any other child.
  children () flattens imported units into the containing list.

  The == and != comparisons for dwarf and debug_info_entry objects compare
  their logical containers, not the raw containers.  The comparisons are
  defined via templates, so you can compare elfutils::dwarf with any other
  class that implements the same structure of containers with input iterators.

  The elfutils::dwarf class and its inner classes form a thin, read-only
  layer of virtual containers that ideally could inline away entirely to
  calls into the C libdw API and small amounts of stack storage.  The tree
  of objects described above never exists in memory in its entirety.  The
  objects are constructed on the fly in each call to a container method.

  See the dwarf_edit and dwarf_output headers for other classes that are
  template-compatible with the "logical view" interface above, but do not
  support any of the "raw" container variants.  These == and != comparisons
  are template-driven too, so all different classes can be compared.

  The output classes have template-driven copy constructors, so they can be
  copied from files or substructures of the elfutils::dwarf input classes.

  ------ XXX to be done: more file-level containers

  input side only:

  units_by_addr : map<pair<begin,end>, CU> and map<address, CU>
	use dwarf_getarange_addr

  pub{names,types} : map<string, debug_info_entry> (across all CUs)

  output too:

  pubnames_map : map<string, debug_info_entry>
  pub{names,types}_units : map<compile_unit, pubnames_map>
	too much lang knowledge to autogenerate for now,
	output will do it explicitly

 */

// DWARF reader interfaces: front end to <libdw.h> routines
namespace elfutils
{
  template<typename type>
  inline std::string to_string (const type &item)
  {
    return item.to_string ();
  }

  template<typename key1, typename value1, class pair2>
  inline bool operator== (const std::pair<key1, value1> &a, const pair2 &b)
  {
    return a.first == b.first && a.second == b.second;
  }

  // Used like std::vector<elt>, but is really just a simple fixed array.
  template<typename elt>
  class const_vector
  {
  private:
    size_t _m_size;
    const elt *_m_array;

  public:
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;
    typedef elt value_type;
    typedef const elt *const_iterator;

    const_vector ()
      : _m_size (0), _m_array (NULL) {}
    const_vector (const const_vector &v)
      : _m_size (v._m_size), _m_array (v._m_array) {}
    const_vector (const elt *start, const elt *stop)
      : _m_size (stop - start), _m_array (start) {}
    const_vector (const ::Dwarf_Block &b)
      : _m_size (b.length), _m_array (reinterpret_cast<const elt *> (b.data)) {}

    inline const_vector &operator= (const const_vector &v)
    {
      _m_size = v._m_size;
      _m_array = v._m_array;
      return *this;
    }

    inline size_t size () const
    {
      return _m_size;
    }
    inline bool empty () const
    {
      return _m_size == 0;
    }

    const_iterator begin () const
    {
      return _m_array;
    }
    const_iterator end () const
    {
      return &_m_array[_m_size];
    }

    template<typename other>
    inline operator other () const
    {
      return other (begin (), end ());
    }

    template<typename vec>
    inline bool operator== (const vec &other) const
    {
      return (other.size () == size ()
	      && std::equal (begin (), end (), other.begin ()));
    }
    template<typename vec>
    inline bool operator!= (const vec &other) const
    {
      return !(*this == other);
    }

  };

  // One DWARF object file.
  class dwarf
  {
  private:
    static const char *known_tag (int);
    static const char *known_attribute (int);

  public:
    typedef subr::known<__typeof ("DW_TAG_"), known_tag> tags;
    typedef subr::known<__typeof ("DW_AT_"), known_attribute> attributes;

    template<typename attribute>
    static inline std::string attribute_name (const attribute &attr)
    {
      int code = attr.first;
      return attributes::name (code);
    }

    template<int key>
    class known_enum
    {
    public:
      static size_t prefix_length ();
      static const char *identifier (int);
      inline static const char *name (int value)
      {
	const char *id = identifier (value);
	return id != NULL ? id + prefix_length () : NULL;
      }

      // XXX perhaps have iterator/lookup methods like a read-only map?
    };

    typedef known_enum< ::DW_AT_producer> forms;
    typedef known_enum< ::DW_AT_location> ops;

  private:
    static void throw_libdw (::Dwarf *dw); // XXX raises (...)
    static void throw_libdw (::Dwarf_CU *); // XXX raises (...)

    inline void xif (bool fail) const
    {
      if (unlikely (fail))
	throw_libdw (_m_dw);
    }
    static inline void xif (::Dwarf_CU *cu, bool fail)
    {
      if (unlikely (fail))
	throw_libdw (cu);
    }

    static inline void xif (const ::Dwarf_Attribute *attr, bool fail)
    {
      xif (attr->cu, fail);
    }
    static inline void xif (const ::Dwarf_Die *die, bool fail)
    {
      xif (die->cu, fail);
    }

    template<typename raw, typename raw_element, typename element,
	     bool skip (const raw_element &)>
    class skipping_wrapper
    {
    protected:
      typedef typename raw::const_iterator raw_iterator;

      raw _m_raw;

    protected:
      inline skipping_wrapper (const raw &r) : _m_raw (r) {}

    public:
      inline skipping_wrapper (const skipping_wrapper &w) : _m_raw (w._m_raw) {}

      /*
	iterator: wraps raw iterator, skips DW_AT_sibling
	size/empty: search for DW_AT_sibling, adjust raw size
      */

      class const_iterator
	: public std::iterator<std::input_iterator_tag, element>
      {
	friend class skipping_wrapper<raw, raw_element, element, skip>;
      private:
	raw_iterator _m_raw;
	const raw_iterator _m_end;

	inline void jiggle ()
	{
	  while (_m_raw != _m_end && unlikely (skip (*_m_raw)))
	    ++_m_raw;
	}

      public:
	inline const_iterator ()
	  : _m_raw (), _m_end (raw::end ())
	{
	}

	const_iterator (const const_iterator &i)
	  : _m_raw (i._m_raw), _m_end (i._m_end) {}

	// Start at the raw position and skip as necessary.
	const_iterator (const raw_iterator &begin, const raw_iterator &end)
	  : _m_raw (begin), _m_end (end)
	{
	  jiggle ();
	}

	inline const_iterator &operator= (const const_iterator &other)
	{
	  _m_raw = other._m_raw;
	  return *this;
	}

	inline bool operator== (const const_iterator &other) const
	{
	  return _m_raw == other._m_raw;
	}
	inline bool operator!= (const const_iterator &other) const
	{
	  return !(*this == other);
	}

	struct hasher : public std::unary_function<const_iterator, size_t>
	{
	  size_t operator () (const const_iterator &i) const
	  {
	    return subr::hash_this (i._m_raw);
	  }
	};

	inline const_iterator &operator++ () // prefix
	{
	  ++_m_raw;
	  jiggle ();
	  return *this;
	}
	inline const_iterator operator++ (int) // postfix
	{
	  const_iterator prev = *this;
	  ++*this;
	  return prev;
	}

	inline element operator* () const
	{
	  return static_cast<element> (*_m_raw);
	}
      };

      inline const_iterator begin () const
      {
	return const_iterator (_m_raw.begin (), _m_raw.end ());
      }
      inline const_iterator end () const
      {
	const raw_iterator raw_end = _m_raw.end ();
	return const_iterator (raw_end, raw_end);
      }
    };

  public:
    /*
      getstring
    */

    class attribute;
    class attr_value;
    class location_attr;
    class range_list;
    class ranges;
    class line_info_table;
    class directory_table;
    class file_table;
    class line_table;
    class line_entry;
    class dwarf_enum;

    class debug_info_entry
    {
    private:
      ::Dwarf_Die _m_die;
      inline ::Dwarf_Die *thisdie () const
      {
	return const_cast< ::Dwarf_Die *> (&_m_die);
      }

      friend class dwarf;
      friend class attr_value;
    protected:

      inline void xif (bool fail) const
      {
	dwarf::xif (_m_die.cu, fail);
      }

      inline debug_info_entry ()
      {
	memset (&_m_die, 0, sizeof _m_die);
      }

      inline debug_info_entry (const dwarf &dw, ::Dwarf_Off off)
      {
	dw.xif (::dwarf_offdie (dw._m_dw, off, &_m_die) == NULL);
      }

    public:
      debug_info_entry (const debug_info_entry &die) : _m_die (die._m_die) {}

      // Containers, see class definitions below.
      class raw_children_type;
      inline raw_children_type raw_children () const;
      class raw_attributes_type;
      raw_attributes_type raw_attributes () const;
      class children_type;
      inline children_type children () const;
      class attributes_type;
      attributes_type attributes () const;

      class const_pointer;

      inline std::string to_string () const;

      inline int tag () const
      {
	int t = ::dwarf_tag (thisdie ());
	xif (t <= 0);
	return t;
      }

      bool has_children () const
      {
	int has = ::dwarf_haschildren (thisdie ());
	xif (has < 0);
	return has != 0;
      }

      /*
	const char *tag_name () const
	const_string tag_name () const // "name" or "0x123"
      */

      template<typename die>
      bool operator== (const die &other) const
      {
	return (tag () == other.tag ()
		&& attributes () == other.attributes ()
		&& children () == other.children ());
      }
      template<typename die>
      bool operator!= (const die &other) const
      {
	return !(*this == other);
      }

      ::Dwarf_Off offset () const
      {
	return ::dwarf_dieoffset (thisdie ());
      }

      inline const dwarf::ranges ranges () const
      {
	return dwarf::ranges (*this);
      }

      /* This is an identity pointer that only matches the very same
	 DIE in the very same file (same opened Dwarf instance).  */
      typedef uintptr_t identity_type;
      inline identity_type identity () const
      {
	return (uintptr_t) _m_die.addr;
      }
    };

    // Container for raw list of child DIEs, intended to be a compatible with
    // a read-only, unidirectional subset of std::list<debug_info_entry>.
    class debug_info_entry::raw_children_type
    {
      friend class debug_info_entry;
    private:
      const debug_info_entry &_m_die;

    protected:
      inline raw_children_type (const debug_info_entry &die) : _m_die (die) {}

    public:
      typedef debug_info_entry value_type;

      inline raw_children_type (const raw_children_type &c)
	: _m_die (c._m_die)
      {}

      bool empty () const
      {
	return begin () == end ();
      }

      class const_iterator
	: public std::iterator<std::input_iterator_tag, debug_info_entry>
      {
	friend class debug_info_entry;
	friend class attr_value;
      private:
	debug_info_entry _m_die;

	inline const_iterator ()
	{}

	inline const_iterator (const debug_info_entry &parent)
	{
	  int result = ::dwarf_child (parent.thisdie (), &_m_die._m_die);
	  parent.xif (result < 0);
	}

	// Construct from a reference attribute.
	inline const_iterator (Dwarf_Attribute *attr)
	{
	  dwarf::xif (attr, ::dwarf_formref_die (attr, &_m_die._m_die) == NULL);
	}

      public:
	inline const_iterator (const const_iterator &i) : _m_die (i._m_die) {}

	inline const debug_info_entry &operator* () const
	{
	  if (unlikely (_m_die._m_die.addr == NULL))
	    throw std::runtime_error ("dereferencing end iterator");
	  return _m_die;
	}
	inline const debug_info_entry *operator-> () const
	{
	  return &(operator* ());
	}

	inline const_iterator &operator= (const const_iterator &other)
	{
	  _m_die = other._m_die;
	  return *this;
	}

	// Assign directly from a DIE, as if "taking its address".
	inline const_iterator &operator= (const debug_info_entry &die)
	{
	  _m_die = die;
	  return *this;
	}

	inline bool operator== (const const_iterator &other) const
	{
	  return _m_die._m_die.addr == other._m_die._m_die.addr;
	}
	inline bool operator!= (const const_iterator &other) const
	{
	  return !(*this == other);
	}

	struct hasher : public std::unary_function<const_iterator, size_t>
	{
	  size_t operator () (const const_iterator &i) const
	  {
	    return subr::hash_this ((uintptr_t) i._m_die._m_die.addr);
	  }
	};

	inline const_iterator &operator++ () // prefix
	{
	  int result = ::dwarf_siblingof (&_m_die._m_die, &_m_die._m_die);
	  _m_die.xif (result < 0);
	  if (result > 0)	// Hit the end.
	    *this = const_iterator ();
	  return *this;
	}
	inline const_iterator operator++ (int) // postfix
	{
	  const_iterator prev = *this;
	  ++*this;
	  return prev;
	}
      };
      const_iterator begin () const
      {
	return const_iterator (_m_die);
      }
      static inline const_iterator end ()
      {
	return const_iterator ();
      }

      template<typename other_children>
      bool operator== (const other_children &other) const
      {
	return subr::container_equal (*this, other);
      }
      template<typename other_children>
      bool operator!= (const other_children &other) const
      {
	return !(*this == other);
      }
    };

    // Container for list of raw attributes as (name, value) pairs,
    // intended to be compatible with a read-only, unidirectional
    // subset of std::list<std::pair<int, attr_value>>.
    class debug_info_entry::raw_attributes_type
    {
      friend class debug_info_entry;
    private:
      const debug_info_entry &_m_die;

      raw_attributes_type (const debug_info_entry &die)
	: _m_die (die)
      {}

    public:
      typedef attribute value_type;

      inline raw_attributes_type (const raw_attributes_type &a)
	: _m_die (a._m_die)
      {}

      size_t size () const;
      inline bool empty () const
      {
	return size () == 0;
      }

      class const_iterator
	: public std::iterator<std::input_iterator_tag, attribute>
      {
	friend class raw_attributes_type;
      private:
	debug_info_entry _m_die;
	ptrdiff_t _m_offset; // Offset for next iteration in dwarf_getattrs.
	::Dwarf_Attribute _m_attr;

	/* We get called up to twice per iteration.  The first time, we
	   store *ATTR in the instance variable and return DWARF_CB_OK so
	   that we might be called again.  The second time, we return
	   DWARF_CB_ABORT so that the iteration will stop at the next
	   attribute's offset.  */
	static int getattrs_callback (Dwarf_Attribute *attr, void *arg)
	{
	  const_iterator *i = static_cast<const_iterator *> (arg);
	  if (i->_m_attr.valp == NULL)
	    {
	      i->_m_attr = *attr;
	      return DWARF_CB_OK;
	    }
	  return DWARF_CB_ABORT;
	}

	inline const_iterator (const debug_info_entry &die, ptrdiff_t offset)
	  : _m_die (die), _m_offset (offset), _m_attr ()
	{}

	inline const_iterator (ptrdiff_t offset)
	  : _m_die (), _m_offset (offset), _m_attr ()
	{}

      public:
	// Default constructor: invalid for anything but operator=.
	inline const_iterator ()
	  : _m_die (), _m_offset (-1), _m_attr ()
	{}

	inline const_iterator (const const_iterator &i)
	  : _m_die (i._m_die), _m_offset (i._m_offset), _m_attr (i._m_attr)
	{}

	inline const_iterator &operator= (const const_iterator &other)
	{
	  _m_die = other._m_die;
	  _m_offset = other._m_offset;
	  _m_attr = other._m_attr;
	  return *this;
	}

	inline bool operator== (const const_iterator &other) const
	{
	  return (_m_die._m_die.addr == other._m_die._m_die.addr
		  && _m_attr.valp == other._m_attr.valp);
	}
	inline bool operator!= (const const_iterator &other) const
	{
	  return !(*this == other);
	}

	inline const_iterator &operator++ () // prefix
	{
	  _m_attr.valp = NULL;
	  int result = ::dwarf_getattrs (&_m_die._m_die, &getattrs_callback,
					 (void *) this, _m_offset);
	  _m_die.xif (result < 0);
	  _m_offset = result;
	  if (result == 1)
	    // End iterators have no live pointers.
	    _m_die._m_die.addr = NULL;
	  return *this;
	}
	inline const_iterator operator++ (int) // postfix
	{
	  const_iterator prev = *this;
	  ++*this;
	  return prev;
	}

	inline attribute operator* () const
	{
	  if (unlikely (_m_attr.valp == NULL))
	    throw std::runtime_error ("dereferencing end iterator");
	  return attribute (_m_die, _m_attr);
	}
      };
      inline const_iterator begin () const
      {
	const_iterator i = const_iterator (_m_die, 0);
	return ++i;
      }
      static inline const_iterator end ()
      {
	return const_iterator (1);
      }

      // XXX can do faster internal (?)
      inline const_iterator find (int name) const
      {
	const_iterator i = begin ();
	while (i != end () && (*i).first != name)
	  ++i;
	return i;
      }
    };

    // Container for list of child DIEs, intended to be a compatible with
    // a read-only, unidirectional subset of std::list<debug_info_entry>.
    // Same as raw_children, but flattens DW_TAG_imported_unit children.
    class debug_info_entry::children_type
      : public debug_info_entry::raw_children_type
    {
      friend class debug_info_entry;
    private:

      inline children_type (const debug_info_entry &die)
	: raw_children_type::raw_children_type (die) {}

    public:
      typedef debug_info_entry value_type;

      inline children_type (const children_type &c)
	: raw_children_type (c)
      {}

      class const_iterator
	: public std::iterator<std::input_iterator_tag, debug_info_entry>
      {
	friend class children_type;
      private:

	typedef raw_children_type::const_iterator raw_iterator;
	std::stack<raw_iterator> _m_stack;

	/* Push and pop until _m_stack.top () == raw_children_type::end ()
	   or it's looking at a DIE other than DW_TAG_imported_unit.  */
	inline void jiggle ()
	{
	  while (true)
	    {
	      raw_iterator &i = _m_stack.top ();

	      if (i == raw_children_type::end ())
		{
		  /* We're at the end of this raw DIE.
		     Pop out to the iterator on the importing unit.  */
		  _m_stack.pop ();

		  if (_m_stack.empty ())
		    // That was the outermost unit, this is the end.
		    break;

		  continue;
		}

	      if (i->tag () == ::DW_TAG_imported_unit)
		// We have an imported unit.  Look at its referent.
		_m_stack.push (i->attributes ().at (::DW_AT_import)
			       .reference ()->raw_children ().begin ());
	      else
		// This is some other DIE.  Iterate on it.
		break;
	    }
	}

      public:
	inline const_iterator ()
	  : _m_stack ()
	{}

	inline const_iterator (const raw_iterator &i)
	{
	  _m_stack.push (i);
	  jiggle ();
	}

	inline const_iterator (const const_iterator &i)
	  : _m_stack (i._m_stack)
	{}

	// Construct directly from a DIE, as if "taking its address".
	inline const_iterator (const debug_info_entry &die)
	{
	  raw_iterator it;
	  it = die;
	  _m_stack.push (it);
	  jiggle ();
	}

	inline const_iterator &operator= (const const_iterator &other)
	{
	  _m_stack = other._m_stack;
	  return *this;
	}

	inline bool operator== (const const_iterator &other) const
	{
	  return _m_stack == other._m_stack;
	}
	inline bool operator!= (const const_iterator &other) const
	{
	  return !(*this == other);
	}

	inline const debug_info_entry &operator* () const
	{
	  return *_m_stack.top ();
	}
	inline const debug_info_entry *operator-> () const
	{
	  return &(operator* ());
	}

	inline const_iterator &operator++ () // prefix
	{
	  ++_m_stack.top ();
	  jiggle ();
	  return *this;
	}
	inline const_iterator operator++ (int) // postfix
	{
	  const_iterator prev = *this;
	  ++*this;
	  return prev;
	}
      };

      // Actually always const.
      typedef const_iterator iterator;

      const_iterator begin () const
      {
	return const_iterator (raw_children_type::begin ());
      }
      const_iterator end () const
      {
	return const_iterator (raw_children_type::end ());
      }

      template<typename other_children>
      bool operator== (const other_children &other) const
      {
	return subr::container_equal (*this, other);
      }
      template<typename other_children>
      bool operator!= (const other_children &other) const
      {
	return !(*this == other);
      }
    };

    class debug_info_entry::const_pointer
      : public debug_info_entry::children_type::const_iterator
    {};

  private:
    static inline bool skip_sibling (const attribute &attr)
    {
      return attr.first == ::DW_AT_sibling;
    }

    // Circumvent C++ namespace lookup.
    typedef class debug_info_entry::raw_attributes_type die_raw_attrs;
    typedef skipping_wrapper<die_raw_attrs, attribute, attribute, skip_sibling>
    attributes_base;

  public:
    // Container for attributes, indexed by name, intended to be compatible
    // with a read-only subset of std::unordered_map<int, attr_value>.
    // This excludes DW_AT_sibling.
    class debug_info_entry::attributes_type : public attributes_base
    {
      friend class dwarf;
    private:
      inline attributes_type (const raw_attributes_type &raw)
	: attributes_base (raw) {}

    public:
      typedef int key_type;
      typedef attr_value mapped_type;
      typedef attribute value_type;

      static inline bool ordered ()
      {
	return false;
      }

      inline attributes_type (const attributes_type &a)
	: attributes_base (a)
      {}

      typedef attributes_base::const_iterator const_iterator;

      /*
	iterator: wraps raw_attributes iterator, skips DW_AT_sibling
	size/empty: search for DW_AT_sibling, adjust raw_attributes size
       */

      inline const_iterator find (int name) const
      {
	if (unlikely (name == ::DW_AT_sibling))
	  return end ();
	return const_iterator (_m_raw.find (name), _m_raw.end ());
      }

      inline const attr_value at (int name)
      {
	const_iterator i = find (name);
	if (unlikely (i == end ()))
	  throw std::out_of_range ("XXX");
	return (*i).second;
      }
      inline const attr_value operator[] (int name)
      {
	return at (name);
      }

      // We are rvalue-coercible into a std::map, which is sorted by name.
      inline operator std::map<int, attr_value> () const
      {
	return std::map<int, attr_value> (begin (), end ());
      }
      /*
      template<typename attrs>
      inline operator attrs () const
      {
	return attrs (begin (), end ());
      }
      */

      template<typename attrs>
      bool operator== (const attrs &other) const
      {
	/* Our container is unordered (i.e., in file order).  A set of
	   attributes is conceptually equal if all the pairs match,
	   regardless of the order.  But the container_equal algorithm will
	   compare corresponding elements in order.  So we need an ordered
	   map of our attributes for the comparison.  */
	const std::map<int, attr_value> mine = *this;
	const std::map<int, typename attrs::mapped_type> his = other;
	return mine.size () == his.size () && subr::container_equal (mine, his);
      }

      template<typename attrs>
      bool operator!= (const attrs &other) const
      {
	return !(*this == other);
      }
    };

    class compile_unit : public debug_info_entry
    {
    public:
      inline compile_unit (const debug_info_entry &die)
	: debug_info_entry (die) {}

      // Fetch the CU's DW_AT_stmt_list.
      const line_info_table line_info () const;

      // Convenience methods for line_info_table sub-containers.
      inline const directory_table include_directories () const
      {
	return line_info ().include_directories ();
      }
      inline const file_table files () const
      {
	return line_info ().files ();
      }
      inline const line_table lines () const
      {
	return line_info ().lines ();
      }

      /*
	containers/iterators:

	XXX macros

	abbrevs (punt)

      */
    };

    // These are the kinds of values that attributes can have.
    enum value_space
      {
	// These spaces refer purely to DWARF concepts.
	VS_flag,		// Boolean.
	VS_dwarf_constant,	// Known DW_X_* space of integer constants.
	VS_discr_list,		// Block as used for DW_AT_discr_list.
	VS_reference,		// Pointer to another DIE.
	VS_lineptr,		// Pointer into .debug_line section.
	VS_macptr,		// Pointer into .debug_macinfo section.
	VS_rangelistptr,	// Pointer into .debug_ranges section.

	// These spaces refer to textual details of the program source.
	VS_identifier,		// String, identifier in source language.
	VS_string,		// String, miscellaneous use.
	VS_source_file,		// Source file, string or index into file table.
	VS_source_line,		// Line number in source file.
	VS_source_column,	// Column number in source file.

	// These spaces refer to target-format values in the debuggee.
	VS_address,	   // Address constant.
	VS_constant,	   // Other constant, integer or in target formats.
	VS_location,	   // Location expression or location list.
      };

    /* A source file can be just a file name.  When represented in the
       .debug_line file table, it can also have a modtime and a file size.
       If the modtime or size stored is zero, it doesn't count.  */
    class source_file
    {
      friend class attr_value;
      friend class file_table;
      friend class line_entry;
    private:
      ::Dwarf_Attribute _m_attr;
      inline ::Dwarf_Attribute *thisattr () const
      {
	return const_cast< ::Dwarf_Attribute *> (&_m_attr);
      }

      source_file (const Dwarf_Attribute &attr) : _m_attr (attr) {}

    public:
      std::string to_string () const;

      const char *name () const;
      ::Dwarf_Word mtime () const;
      ::Dwarf_Word size () const;

      template<typename other_file>
      bool operator== (const other_file &other) const
      {
	if (mtime () != 0)
	  {
	    ::Dwarf_Word other_mtime = other.mtime ();
	    if (other_mtime != 0 && other_mtime != mtime ())
	      return false;
	  }
	if (size () != 0)
	  {
	    ::Dwarf_Word other_size = other.size ();
	    if (other_size != 0 && other_size != size ())
	      return false;
	  }
	return subr::name_equal<typeof (other.name ())> () (name (),
							    other.name ());
      }
      template<typename other_file>
      inline bool operator!= (const other_file &other) const
      {
	return !(*this == other);
      }
    };

    // This describes the value of an attribute.
    class attr_value
    {
      friend class attribute;
      friend class location_attr;
      friend class range_list;
      friend class dwarf_enum;
    private:
      const int _m_tag;
      ::Dwarf_Attribute _m_attr;
      inline ::Dwarf_Attribute *thisattr () const
      {
	return const_cast< ::Dwarf_Attribute *> (&_m_attr);
      }
      inline int whatattr () const
      {
	return ::dwarf_whatattr (thisattr ());
      }

      attr_value (int tag, const ::Dwarf_Attribute &attr)
	: _m_tag (tag), _m_attr (attr) {}

      inline bool same (const attr_value &other) const
      {
	return _m_attr.valp == other._m_attr.valp;
      }

    public:
      // not copyable, don't worry about ref lifetime(?)
      // attr_value (const attr_value &v) : _m_attr (v.attr) {}

      value_space what_space () const;
      inline std::string to_string () const;

      // Return an iterator on which * will yield the referent debug_info_entry.
      inline debug_info_entry::children_type::const_iterator
      reference () const
      {
	return (debug_info_entry::raw_children_type::const_iterator
		(thisattr ()));
      }

      // XXX reloc, dwfl
      ::Dwarf_Addr address () const;

      bool flag () const;

      const location_attr location () const;

      const char *string () const;
      inline const char *identifier () const
      {
	return string ();
      }

      const dwarf::source_file source_file () const;
      inline unsigned int source_line () const
      {
	return constant ();
      }
      inline unsigned int source_column () const
      {
	return constant ();
      }

      // XXX reloc
      ::Dwarf_Word constant () const;
      ::Dwarf_Sword signed_constant () const;
      const_vector<uint8_t> constant_block () const;
      bool constant_is_integer () const;

      inline const dwarf_enum dwarf_constant () const
      {
	return dwarf_enum (*this);
      }

      inline const range_list ranges () const
      {
	return range_list (*this);
      }

      const line_info_table line_info () const;

      // XXX macptr

      template<typename value>
      inline bool operator== (const value &other) const
      {
	const value_space what = what_space ();
	if (likely (other.what_space () == what))
	  switch (what)
	    {
	    case VS_reference:
	      // Stateless reference equality is just identity.
	      return (reference ()->identity ()
		      == other.reference ()->identity ());

	    case VS_flag:
	      return flag () == other.flag ();

	    case VS_rangelistptr:
	      return ranges () == other.ranges ();

	    case VS_lineptr:
	      return line_info () == other.line_info ();

	    case VS_macptr:	// XXX punt for now, treat as constant
	      /*FALLTHRU*/
	    case VS_dwarf_constant:
	      return constant () == other.constant ();

	    case VS_constant:
	      if (constant_is_integer ())
		return (other.constant_is_integer ()
			&& constant () == other.constant ());
	      return (!other.constant_is_integer ()
		      && constant_block () == other.constant_block ());

	    case VS_source_line:
	      return source_line () == other.source_line ();
	    case VS_source_column:
	      return source_column () == other.source_column ();

	    case VS_identifier:
	      return subr::name_equal<typeof (other.identifier ())> ()
		(identifier (), other.identifier ());

	    case VS_string:
	      return subr::name_equal<typeof (other.string ())> ()
		(string (), other.string ());

	    case VS_address:
	      return address () == other.address ();

	    case VS_source_file:
	      return source_file () == other.source_file ();

	    case VS_location:
	      return location () == other.location ();

	    case VS_discr_list:
	      throw std::runtime_error ("XXX unimplemented");
	    }
	return false;
      }
      template<typename value>
      inline bool operator!= (const value &other) const
      {
	return !(*this == other);
      }
    };

    /* The DW_AT_ranges attribute yields a range list.
       XXX reloc
       This is equivalent to unordered_set<pair<Dwarf_Addr, Dwarf_Addr> >.  */
    class range_list
    {
      friend class attr_value;
    private:
      const attr_value _m_attr;

      range_list (const attr_value &attr) : _m_attr (attr) {}

    public:
      typedef std::pair< ::Dwarf_Addr, ::Dwarf_Addr> key_type; // XXX reloc
      typedef key_type value_type;

      static inline bool ordered ()
      {
	return false;
      }

      inline bool canonical () const
      {
	return false;
      }

      inline range_list (const range_list &other)
	: _m_attr (other._m_attr)
      {}

      std::string to_string () const;

      class const_iterator
	: public std::iterator<std::input_iterator_tag, value_type>
      {
	friend class range_list;
      protected:
	::Dwarf_Addr _m_base;	// XXX reloc
	::Dwarf_Addr _m_begin;	// XXX reloc
	::Dwarf_Addr _m_end;	// XXX reloc
	::Dwarf_CU *_m_cu;
	ptrdiff_t _m_offset;

	const_iterator (Dwarf_Attribute *, ptrdiff_t);

      public:
	// Default constructor: only valid for operator=.
	inline const_iterator ()
	  : _m_base (-1), _m_begin (0), _m_end (0), _m_cu (NULL), _m_offset (-1)
	{}

	inline const_iterator (const const_iterator &i)
	  : _m_base (i._m_base), _m_begin (i._m_begin), _m_end (i._m_end),
	    _m_cu (i._m_cu), _m_offset (i._m_offset)
	{}

	inline value_type operator* () const
	{
	  if (unlikely (_m_offset == 1))
	    throw std::runtime_error ("dereferencing end iterator");
	  return std::make_pair (_m_base + _m_begin, _m_base + _m_end);
	}

	inline const_iterator &operator= (const const_iterator &other)
	{
	  _m_base = other._m_base;
	  _m_begin = other._m_begin;
	  _m_end = other._m_end;
	  _m_cu = other._m_cu;
	  _m_offset = other._m_offset;
	  return *this;
	}

	inline bool operator== (const const_iterator &other) const
	{
	  return _m_offset == other._m_offset && _m_cu == other._m_cu;
	}
	inline bool operator!= (const const_iterator &other) const
	{
	  return !(*this == other);
	}

	const_iterator &operator++ (); // prefix
	inline const_iterator operator++ (int) // postfix
	{
	  const_iterator prev = *this;
	  ++*this;
	  return prev;
	}
      };

      const_iterator begin () const
      {
	const_iterator it (_m_attr.thisattr (), 0);
	return ++it;
      }
      const_iterator end () const
      {
	return const_iterator (_m_attr.thisattr (), 1);
      }

      const_iterator find (const key_type &match) const
      {
	return std::find (begin (), end (), match);
      }

    private:
      struct entry_contains
	: public std::binary_function<key_type, ::Dwarf_Addr, bool>
      {
	inline bool operator() (const key_type &range, const ::Dwarf_Addr addr)
	  const
	{
	  return addr >= range.first && addr < range.second;
	}
      };

    public:
      const_iterator find (const ::Dwarf_Addr addr) const
      {
	return std::find_if (begin (), end (),
			     std::bind2nd (entry_contains (), addr));
      }

      inline operator std::set<key_type> () const
      {
	return std::set<key_type> (begin (), end ());
      }

      template<typename ranges>
      inline bool operator== (const ranges &other) const
      {
	/* Our container is unordered (i.e., in file order).  A range list
	   is conceptually equal if all the pairs match, regardless of the
	   order.  But the std::equal algorithm will compare corresponding
	   elements in order.  So we need an ordered set for comparison.  */
	std::set<key_type> mine = *this;
	coalesce (mine);
	std::set<key_type> his = other;
	coalesce (his);
	return mine == his;
      }
      template<typename ranges>
      inline bool operator!= (const ranges &other) const
      {
	return !(*this == other);
      }

      // Not very wise to call.
      size_t size () const
      {
	return subr::length (begin (), end ());
      }
    };

    /* A location attribute yields a location expression.
       Either it's a single expression, or a map of PC to location.  */
    class location_attr
    {
      friend class attr_value;
    private:
      attr_value _m_attr;

      location_attr (const attr_value &attr) : _m_attr (attr) {}

      inline bool same (const location_attr &it) const
      {
	return _m_attr.same (it._m_attr);
      }

      template<typename pair>
      struct nonempty : public std::unary_function<pair, bool>
      {
	inline bool operator () (const pair &x)
	{
	  return !x.second.empty ();
	}
      };

      template<typename pair>
      struct any : public std::unary_function<pair, bool>
      {
	inline bool operator () (const pair &)
	{
	  return true;
	}
      };

    public:
      typedef size_t size_type;
      typedef ptrdiff_t difference_type;
      // XXX need proper type for exprs
      typedef const_vector<uint8_t> mapped_type;
      typedef std::pair< ::Dwarf_Addr, ::Dwarf_Addr> key_type; // XXX reloc
      typedef std::pair<const key_type, mapped_type> value_type;

      std::string to_string () const;

      bool is_list () const;

      inline mapped_type location () const
      {
	if (is_list ())
	  throw std::runtime_error ("location is list, not single location");
	return _m_attr.constant_block ();
      }

      class const_iterator
	: public range_list::const_iterator
      {
	friend class location_attr;
      private:
	::Dwarf_Block _m_block;

	void advance ();

	inline const_iterator (Dwarf_Attribute *attr, ptrdiff_t offset)
	  : range_list::const_iterator (attr, offset), _m_block ()
	{}

      public:
	typedef location_attr::value_type value_type;

	inline const_iterator ()
	  : _m_block ()
	{}

	inline const_iterator (const const_iterator &i)
	  : range_list::const_iterator (i), _m_block (i._m_block)
	{}

	inline const_iterator &operator= (const const_iterator &i)
	{
	  range_list::const_iterator::operator= (i);
	  _m_block = i._m_block;
	  return *this;
	}

	inline bool operator== (const const_iterator &it) const
	{
	  return _m_block.data == it._m_block.data;
	}
	inline bool operator!= (const const_iterator &it) const
	{
	  return !(*this == it);
	};

	const_iterator &operator++ (); // prefix
	inline const_iterator operator++ (int) // postfix
	{
	  const_iterator prev = *this;
	  ++*this;
	  return prev;
	}

	inline value_type operator* () const
	{
	  if (unlikely (_m_block.data == NULL))
	    throw std::runtime_error ("dereferencing end iterator");

	  return value_type (key_type (_m_base + _m_begin, _m_base + _m_end),
			     const_vector<uint8_t> (_m_block));
	}
      };

      const_iterator begin () const;
      inline const_iterator end () const
      {
	return const_iterator (_m_attr.thisattr (), 1);
      }

      inline bool empty () const
      {
	if (is_list ())
	  return std::find_if (begin (), end (),
			       nonempty<value_type> ()) == end ();
	return location ().empty ();
      }
      inline size_type size () const
      {
	if (is_list ())
	  return subr::length (begin (), end ());
	return location ().empty () ? 0 : 1;
      }

      template<typename other_attr>
      bool operator== (const other_attr &other) const
      {
	if (empty ())
	  return (other.empty ()
		  || std::find_if (other.begin (), other.end (),
				   nonempty<typename other_attr::value_type> ()
				   ) == other.end ());
	if (!is_list ())
	  return (!other.is_list () && !other.empty ()
		  && location () == other.location ());

	return other.is_list () && subr::container_equal (*this, other);
      }
      template<typename other_file>
      inline bool operator!= (const other_file &other) const
      {
	return !(*this == other);
      }

      /*
	XXX missing: find, at; by key_type or by PC
	XXX worse than that: multiple overlapping matches!
       */
    };

    // This describes a CU's directory table, a simple array of strings.
    class directory_table
    {
    private:
      ::Dwarf_Files *_m_files;

      template<typename table>
      inline bool table_equal (const table &other) const
      {
	/* We ignore the first element, the compilation directory.
	   This is not encoded in the .debug_line table, but in
	   the DW_AT_comp_dir attribute of the referring CU.
	   The directory table itself matches regardless.  */
	const_iterator i = begin ();
	typename table::const_iterator j = other.begin ();
	return subr::container_equal
	  (++i, end (), ++j, other.end (),
	   subr::deref<directory_table, table,
	   	       subr::name_equal<typename table::value_type> > ());
      }

    public:
      typedef size_t size_type;
      typedef ptrdiff_t difference_type;
      typedef const char *value_type;

      inline directory_table (::Dwarf_Files *const files)
	: _m_files (files) {}
      inline directory_table (const directory_table &t)
	: _m_files (t._m_files) {}

      std::string to_string () const;

      typedef const char *const *const_iterator;

      inline bool empty () const
      {
	return size () == 0;
      }

      size_t size () const;
      const_iterator begin () const;
      const_iterator end () const;

      template<typename table>
      inline bool operator== (const table &other) const
      {
	return table_equal (other);
      }
      template<typename table>
      inline bool operator!= (const table &other) const
      {
	return !(*this == other);
      }
      // Short-circuit for comparing to self.
      inline bool operator== (const directory_table &other) const
      {
	return _m_files == other._m_files || table_equal (other);
      }
    };

    /* This describes a CU's file table.  It works like a read-only
       std::vector<source_file>, and also supports lookup by name.  */
    class file_table
    {
    private:
      ::Dwarf_Files *_m_files;

    public:
      typedef size_t size_type;
      typedef ptrdiff_t difference_type;
      typedef source_file value_type;

      inline file_table (::Dwarf_Files *const files)
	: _m_files (files) {}
      inline file_table (const file_table &t)
	: _m_files (t._m_files) {}

      inline file_table &operator= (const file_table &t)
      {
	_m_files = t._m_files;
	return *this;
      }

      typedef subr::indexed_iterator<file_table> const_iterator;

      inline bool empty () const
      {
	return size () == 0;
      }

      size_t size () const;

      inline const_iterator begin () const
      {
	return const_iterator (*this, 0);
      }
      inline const_iterator end () const
      {
	return const_iterator (*this, size ());
      }

      const source_file at (size_t idx) const;
      const source_file operator[] (size_t idx) const
      {
	return at (idx);
      }

      // Look up by matching file name.
      const_iterator find (const source_file &) const;
      const_iterator find (const char *filename) const
      {
	const_iterator i = begin ();
	while (i != end () && strcmp ((*i).name (), filename) != 0)
	  ++i;
	return i;
      }
      template<typename string>
      const_iterator find (const string &filename) const
      {
	const_iterator i = begin ();
	while (i != end () && filename != (*i).name ())
	  ++i;
	return i;
      }
    };

    // This describes one entry in the line information table.
    class line_entry
    {
    private:
      ::Dwarf_Line *_m_line;

    public:
      line_entry (::Dwarf_Line *entry) : _m_line (entry) {}
      line_entry (const line_entry &entry) : _m_line (entry._m_line) {}

      // XXX reloc, dwfl
      ::Dwarf_Addr address () const;

      bool statement () const;
      bool basic_block () const;
      bool end_sequence () const;
      bool prologue_end () const;
      bool epilogue_begin () const;

      const source_file file () const;
      unsigned int line () const;
      unsigned int column () const;

      template<typename entry>
      bool operator< (const entry &other) const
      {
	return address () < other.address ();
      }
      template<typename entry>
      bool operator> (const entry &other) const
      {
	return address () > other.address ();
      }
      template<typename entry>
      bool operator<= (const entry &other) const
      {
	return address () <= other.address ();
      }
      template<typename entry>
      bool operator>= (const entry &other) const
      {
	return address () >= other.address ();
      }

      template<typename entry>
      inline bool operator== (const entry &other) const
      {
	return (address () == other.address ()
		&& line () == other.line ()
		&& column () == other.column ()
		&& statement () == other.statement ()
		&& basic_block () == other.basic_block ()
		&& end_sequence () == other.end_sequence ()
		&& prologue_end () == other.prologue_end ()
		&& epilogue_begin () == other.epilogue_begin ()
		&& file () == other.file ());
      }
      template<typename entry>
      inline bool operator!= (const entry &other) const
      {
	return !(*this == other);
      }
      // Short-circuit for our own type.
      bool operator== (const line_entry &other) const;
    };

    /* This describes a CU's line information table.
       It works like a read-only std::vector<line_entry>,
       and also supports lookup by address.
       XXX later, by file/line
    */
    class line_table
    {
    private:
      ::Dwarf_Lines *_m_lines;

    public:
      typedef size_t size_type;
      typedef ptrdiff_t difference_type;
      typedef line_entry value_type;

      inline line_table (::Dwarf_Lines *const lines)
	: _m_lines (lines) {}
      inline line_table (const line_table &t)
	: _m_lines (t._m_lines) {}

      inline line_table &operator= (const line_table &t)
      {
	_m_lines = t._m_lines;
	return *this;
      }

      std::string to_string () const;

      typedef subr::indexed_iterator<line_table> const_iterator;

      inline bool empty () const
      {
	return size () == 0;
      }

      size_t size () const;

      inline const_iterator begin () const
      {
	return const_iterator (*this, 0);
      }
      inline const_iterator end () const
      {
	return const_iterator (*this, size ());
      }

      const line_entry at (size_t idx) const;
      const line_entry operator[] (size_t idx) const
      {
	return at (idx);
      }

      template<typename table>
      inline bool operator== (const table &other) const
      {
	return size () == other.size () && subr::container_equal (*this, other);
      }
      template<typename table>
      inline bool operator!= (const table &other) const
      {
	return !(*this == other);
      }
      // Short-circuit for comparing to self.
      inline bool operator== (const line_table &other) const
      {
	return (_m_lines == other._m_lines
		|| subr::container_equal (*this, other));
      }

      // Look up by matching address.
      const_iterator find (::Dwarf_Addr) const;
    };

    // The DW_AT_stmt_list attribute yields a line info table.
    class line_info_table
    {
    private:
      ::Dwarf_Files *_m_files;

    public:
      inline line_info_table (::Dwarf_Files *const t)
	: _m_files (t) {}
      inline line_info_table (const line_info_table &t)
	: _m_files (t._m_files) {}

      inline line_info_table &operator= (const line_info_table &t)
      {
	_m_files = t._m_files;
	return *this;
      }

      std::string to_string () const;

      inline const directory_table include_directories () const
      {
	return directory_table (_m_files);
      }
      inline const file_table files () const
      {
	return file_table (_m_files);
      }
      const line_table lines () const;

      template<typename table>
      inline bool operator== (const table &other) const
      {
	return (include_directories () == other.include_directories ()
		&& lines () == other.lines ());
      }
      template<typename table>
      inline bool operator!= (const table &other) const
      {
	return !(*this == other);
      }
    };

    class dwarf_enum
    {
      friend class attr_value;
    private:
      const attr_value _m_attr;

      dwarf_enum (const attr_value &attr) : _m_attr (attr) {}

    public:
      inline operator unsigned int () const
      {
	return _m_attr.constant ();
      }

      std::string to_string () const;

      const char *identifier () const;
      const char *name () const;

      // Return the DW_AT_* indicating which enum this value belongs to.
      unsigned int which () const
      {
	return _m_attr.whatattr ();
      }

      template<typename constant>
      inline bool operator== (const constant &other) const
      {
	return (static_cast<unsigned int> (*this)
		== static_cast<unsigned int> (other));
      }
      template<typename constant>
      inline bool operator!= (const constant &other) const
      {
	return !(*this == other);
      }
    };

    // This describes one attribute, equivalent to pair<const int, attr_value>.
    class attribute
    {
      friend class debug_info_entry::raw_attributes_type::const_iterator;
      friend class attr_value;
    private:
      inline ::Dwarf_Attribute *thisattr () const
      {
	return second.thisattr ();
      }

      class lhs
      {
	friend class attribute;
      private:
	const attribute &_m_attr;

	lhs (attribute &attr) : _m_attr (attr) {}

      public:
	operator int () const
	{
	  return ::dwarf_whatattr (_m_attr.thisattr ());
	}
      };

      attribute (const debug_info_entry &die, const ::Dwarf_Attribute &attr)
	: first (*this), second (die.tag (), attr) {}

    public:
      lhs first;
      attr_value second;

      inline attribute (const attribute &a)
	: first (*this), second (a.second) {}

      // This lets pair<...> x = (attribute) y work.
      template<typename value>
      operator std::pair<const int, value> () const
      {
	return std::make_pair (static_cast<int> (first), value (second));
      }

      template<typename pair>
      inline bool operator== (const pair &other) const
      {
	return first == other.first && second == other.second;
      }
      template<typename pair>
      inline bool operator!= (const pair &other) const
      {
	return !(*this == other);
      }

      inline std::string to_string () const;
    };

    /* This works like range_list, but is based on a debug_info_entry using
       dwarf_ranges.  If the entry has DW_AT_low_pc and DW_AT_high_pc, this
       will present a singleton list; if it has a DW_AT_ranges, it will be
       the same as the range_list presentation.  If neither, an empty list.  */
    class ranges
    {
      friend class debug_info_entry;
    private:
      debug_info_entry _m_die;

      ranges (const debug_info_entry &die) : _m_die (die) {}

    public:
      typedef std::pair< ::Dwarf_Addr, ::Dwarf_Addr> key_type; // XXX reloc
      typedef key_type value_type;

      ranges (const ranges &other) : _m_die (other._m_die) {}

      std::string to_string () const;

      class const_iterator
	: public std::iterator<std::input_iterator_tag, value_type>
      {
	friend class ranges;
      private:
	debug_info_entry _m_die;
	::Dwarf_Addr _m_base;	// XXX reloc
	::Dwarf_Addr _m_begin;	// XXX reloc
	::Dwarf_Addr _m_end;	// XXX reloc
	ptrdiff_t _m_offset;

	inline const_iterator (const debug_info_entry &die)
	  : _m_die (die), _m_offset (0) {}

      public:
	inline const_iterator (const const_iterator &i)
	  : _m_die (i._m_die), _m_base (i._m_base),
	    _m_begin (i._m_begin), _m_end (i._m_end),
	    _m_offset (i._m_offset) {}

	inline value_type operator* () const
	{
	  if (unlikely (_m_offset == 0))
	    throw std::runtime_error ("dereferencing end iterator");
	  return std::make_pair (_m_begin, _m_end);
	}

	inline const_iterator &operator= (const const_iterator &other)
	{
	  _m_die = other._m_die;
	  _m_base = other._m_base;
	  _m_begin = other._m_begin;
	  _m_end = other._m_end;
	  _m_offset = other._m_offset;
	  return *this;
	}

	inline bool operator== (const const_iterator &other) const
	{
	  return (_m_die._m_die.addr == other._m_die._m_die.addr
		  && _m_offset == other._m_offset);
	}
	inline bool operator!= (const const_iterator &other) const
	{
	  return !(*this == other);
	}

	const_iterator &operator++ () // prefix
	{
	  do
	    _m_offset = dwarf_ranges (_m_die.thisdie (), _m_offset,
				      &_m_base, &_m_begin, &_m_end);
	  // Skip over empty ranges.
	  while (_m_offset != 0 && _m_begin == _m_end);
	  return *this;
	}
	inline const_iterator operator++ (int) // postfix
	{
	  const_iterator prev = *this;
	  ++*this;
	  return prev;
	}
      };

      const_iterator begin () const
      {
	const_iterator it (_m_die);
	return ++it;
      }
      const_iterator end () const
      {
	return const_iterator (_m_die);
      }

      inline bool empty () const
      {
	return begin () == end ();
      }

      const_iterator find (const key_type &match) const
      {
	return std::find (begin (), end (), match);
      }

    private:
      struct entry_contains
	: public std::binary_function<key_type, ::Dwarf_Addr, bool>
      {
	inline bool operator() (const key_type &range, const ::Dwarf_Addr addr)
	  const
	{
	  return addr >= range.first && addr < range.second;
	}
      };

    public:
      const_iterator find (const ::Dwarf_Addr addr) const
      {
	return std::find_if (begin (), end (),
			     std::bind2nd (entry_contains (), addr));
      }

      inline operator std::set<key_type> () const
      {
	return std::set<key_type> (begin (), end ());
      }

      template<typename ranges>
      inline bool operator== (const ranges &other) const
      {
	/* Our container is unordered (i.e., in file order).  A range list
	   is conceptually equal if all the pairs match, regardless of the
	   order.  But the std::equal algorithm will compare corresponding
	   elements in order.  So we need an ordered set for comparison.  */
	std::set<key_type> mine = *this;
	coalesce (mine);
	std::set<key_type> his = other;
	coalesce (his);
	return mine == his;
      }
      template<typename ranges>
      inline bool operator!= (const ranges &other) const
      {
	return !(*this == other);
      }
    };

    // Container for raw CUs in file order, intended to be compatible
    // with a read-only subset of std::list<compile_unit>.
    class raw_compile_units
    {
      friend class dwarf;
    private:
      const dwarf &_m_file;

      raw_compile_units (const dwarf &file) : _m_file (file) {}

    public:
      typedef compile_unit value_type;

      inline raw_compile_units (const raw_compile_units &u)
	: _m_file (u._m_file) {}

      class const_iterator
	: public std::iterator<std::input_iterator_tag, compile_unit>
      {
	friend class raw_compile_units;
      private:
	debug_info_entry _m_die;
	const dwarf *_m_file;	// XXX
	::Dwarf_Off _m_next;	// XXX

	inline const_iterator (const dwarf &file)
	  : _m_file (&file), _m_next (0) {}

      public:
	inline const_iterator ()
	  : _m_die (), _m_file (NULL), _m_next (-1)
	{}

	inline const_iterator (const const_iterator &i)
	  : _m_die (i._m_die), _m_file (i._m_file), _m_next (i._m_next) {}

	inline const debug_info_entry &operator* () const
	{
	  if (unlikely (_m_next == (::Dwarf_Off) -1))
	    throw std::runtime_error ("dereferencing end iterator");
	  return _m_die;
	}
	inline const debug_info_entry *operator-> () const
	{
	  return &(operator* ());
	}

	inline const_iterator &operator= (const const_iterator &other)
	{
	  _m_die = other._m_die;
	  _m_next = other._m_next;
	  _m_file = other._m_file; // XXX
	  return *this;
	}

	inline bool operator== (const const_iterator &other) const
	{
	  return _m_next == other._m_next && _m_file == other._m_file;
	}
	inline bool operator!= (const const_iterator &other) const
	{
	  return !(*this == other);
	}

	inline const_iterator &operator++ () // prefix
	{
	  // XXX should be rewritten to use libdw_findcu internals
	  // slow way for first crack to avoid DSO issues
	  _m_next = _m_file->nextcu (_m_next, _m_die.thisdie ());
	  if (_m_next == (::Dwarf_Off) -1)
	    // End iterators have no file pointer.
	    _m_file = NULL;
	  return *this;
	}
	inline const_iterator operator++ (int) // postfix
	{
	  const_iterator prev = *this;
	  ++*this;
	  return prev;
	}
      };

      const_iterator begin () const
      {
	const_iterator it (_m_file);
	return ++it;
      }
      static inline const_iterator end ()
      {
	return const_iterator ();
      }
    };
    inline raw_compile_units raw_compile_units () const
    {
      return raw_compile_units::raw_compile_units (*this);
    }

  private:
    static inline bool skip_partial_unit (const compile_unit &unit)
    {
      switch (unit.tag ())
	{
	case ::DW_TAG_partial_unit:
	  return true;
	case ::DW_TAG_compile_unit:
	  return false;
	default:
	  throw std::exception(); // XXX invalid dwarf
	}
    }

    typedef skipping_wrapper<class raw_compile_units,
			     compile_unit, compile_unit,
			     skip_partial_unit> compile_units_base;

  public:

    // Container for logical CUs in file order, intended to be compatible
    // with a read-only subset of std::list<compile_unit>.
    class compile_units : public compile_units_base
    {
      friend class dwarf;
    private:
      compile_units (class raw_compile_units raw) : compile_units_base (raw) {}

    public:
      typedef compile_unit value_type;

      compile_units (const compile_units &u) : compile_units_base (u) {}

      template<typename units>
      bool operator== (const units &other) const
      {
	return subr::container_equal (*this, other);
      }
      template<typename units>
      bool operator!= (const units &other) const
      {
	return !(*this == other);
      }
    };
    inline class compile_units compile_units () const
    {
      return compile_units::compile_units (raw_compile_units ());
    }

  private:
    ::Dwarf *_m_dw;

  public:
    // XXX temp hack
    inline ::Dwarf_Off nextcu (::Dwarf_Off offset, ::Dwarf_Die *die) const
    {
      ::Dwarf_Off next;
      ::size_t header_size;
      int result = ::dwarf_nextcu (_m_dw, offset, &next, &header_size,
				   NULL, NULL, NULL);
      xif (result < 0);
      if (result == 0)
	xif (::dwarf_offdie (_m_dw, offset + header_size, die) == NULL);
      else
	memset (die, 0, sizeof *die);
      return next;
    }

    inline dwarf (::Dwarf *dw) : _m_dw (dw) {};

    inline dwarf (const dwarf &dw) : _m_dw (dw._m_dw) {};

    template<typename file>
    inline bool operator== (const file &other) const
    {
      return compile_units () == other.compile_units ();
    }
    template<typename file>
    inline bool operator!= (const file &other) const
    {
      return !(*this == other);
    }

    // XXX reloc
    class arange_list
      : public std::set<std::pair< ::Dwarf_Addr, ::Dwarf_Addr> >
    {
    private:
      typedef std::set<std::pair< ::Dwarf_Addr, ::Dwarf_Addr> > _base;

    public:
      typedef _base::key_type key_type;
      typedef _base::value_type value_type;
      typedef _base::iterator iterator;
      typedef _base::const_iterator const_iterator;

      static inline bool ordered ()
      {
	return true;
      }

      struct hasher : public subr::container_hasher<arange_list> {};

      inline arange_list () {}
      inline arange_list (const arange_list &other)
	: _base (static_cast<const _base &> (other))
      {}

      template<typename iterator>
      arange_list (iterator first, iterator last)
	: _base (first, last)
      {}

      template<typename input>
      inline arange_list (const input &other)
	: _base (other.begin (), other.end ())
      {}

      std::string to_string () const;

      inline std::string to_string ()
      {
	coalesce (*this);
	return ((const arange_list *) this)->to_string ();
      }

      inline bool canonical () const
      {
	// Can't be sure.
	return false;
      }

      inline bool canonical ()
      {
	// Make it so.
	coalesce (*this);
	return true;
      }

      inline bool operator== (arange_list &other)
      {
	// Since we are not const, coalesce both in place.
	coalesce (other);
	if (size () < other.size ())
	  // Coalescing can only make us smaller.
	  return false;
	coalesce (*this);
	return size () == other.size () && subr::container_equal (*this, other);
      }

      template<typename list>
      inline bool operator== (const list &other)
      {
	// Since we are not const, coalesce in place.
	coalesce (*this);

	if (list::ordered () && other.canonical ()
	    && size () != other.size ())
	  return false;

	// If he happens to be sorted and canonical, we'll match.
	if (subr::container_equal (*this, other))
	  return true;

	// If he was sorted and canonical and we didn't match, it's conclusive.
	if (list::ordered () && other.canonical ())
	  return false;

	// Make a sorted and canonicalized copy to compare to.
	_base his (other);
	if (size () > his.size ()
	    || (list::ordered () && size () == his.size ()))
	  // Coalescing can only make him smaller.
	  return false;
	coalesce (his);
	return subr::container_equal (*this, his);
      }

      template<typename list>
      inline bool operator== (const list &other) const
      {
	if (list::ordered () && other.canonical ()
	    && size () < other.size ())
	  // Coalescing can only make us smaller.
	  return false;

	// If we both happen to be sorted and canonical, we'll match.
	if (subr::container_equal (*this, other))
	  return true;

	// Make a non-const copy that will coalesce in its operator==.
	if (list::ordered () && other.canonical ())
	  return size () != other.size () && arange_list (*this) == other;

	return arange_list (other) == *this;
      }
    };

  private:
    struct arange_less
      : public std::binary_function<compile_unit, compile_unit, bool>
    {
      inline bool operator() (const compile_unit &a, const compile_unit &b)
	const
      {
	return a.offset () < b.offset ();
      }
    };

  public:
    typedef std::map<compile_unit, arange_list, arange_less> aranges_map;

    aranges_map aranges () const;

  private:
    static bool adjacency (const arange_list::key_type &a,
			   const arange_list::key_type &b)
    {
      return a.second == b.first;
    }

    // Coalesce adjacent ranges.
    static void coalesce (std::set<arange_list::key_type> &set)
    {
      for (std::set<arange_list::key_type>::iterator i = set.begin ();
	   (i = std::adjacent_find (i, set.end (), adjacency)) != set.end ();
	   ++i)
	{
	  std::set<arange_list::key_type>::iterator j = i;
	  std::set<arange_list::key_type>::iterator k = ++j;
	  while (++k != set.end () && adjacency (*j, *k))
	    ++j;
	  const arange_list::key_type joined (i->first, j->second);
	  set.erase (i, k);
	  i = set.insert (joined).first;
	}
    }
  };

  inline class dwarf::debug_info_entry::raw_children_type
  dwarf::debug_info_entry::raw_children () const
  {
    return raw_children_type::raw_children_type (*this);
  }

  inline class dwarf::debug_info_entry::children_type
  dwarf::debug_info_entry::children () const
  {
    return children_type::children_type (*this);
  }

  inline class dwarf::debug_info_entry::raw_attributes_type
  dwarf::debug_info_entry::raw_attributes () const
  {
    return raw_attributes_type::raw_attributes_type (*this);
  }

  inline class dwarf::debug_info_entry::attributes_type
  dwarf::debug_info_entry::attributes () const
  {
    return attributes_type::attributes_type (raw_attributes ());
  }

  // Explicit specializations.
  template<>
  std::string
  to_string<dwarf::debug_info_entry> (const dwarf::debug_info_entry &);
  inline std::string dwarf::debug_info_entry::to_string () const
  {
    return elfutils::to_string (*this); // Use that.
  }
  template<>
  std::string to_string<dwarf::attribute> (const dwarf::attribute &);
  inline std::string dwarf::attribute::to_string () const
  {
    return elfutils::to_string (*this); // Use that.
  }
  template<>
  std::string to_string<dwarf::attr_value> (const dwarf::attr_value &);
  inline std::string dwarf::attr_value::to_string () const
  {
    return elfutils::to_string (*this); // Use that.
  }
  template<>
  std::string to_string<dwarf::dwarf_enum> (const dwarf::dwarf_enum &);
  inline std::string dwarf::dwarf_enum::to_string () const
  {
    return elfutils::to_string (*this); // Use that.
  }

};

#endif	// <elfutils/dwarf>