summaryrefslogtreecommitdiff
path: root/libdw/c++/dwarf_data
blob: d11efdb308963c71db04316678f46e6809facc26 (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
/* elfutils::dwarf_data -- internal DWARF data representations in -*- C++ -*-
   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_DATA
#define _ELFUTILS_DWARF_DATA	1

#include "dwarf"
#include <cassert>
#include <bitset>

/* This contains common classes/templates used by dwarf_output and dwarf_edit.

   These are implementations of the "boring" components of the dwarf
   object interface.
*/

namespace elfutils
{
  // This is a class only for scoping purposes.
  // It contains no members, only inner classes.
  class dwarf_data
  {
  public:

    // Main container anchoring all the output.
    template<class impl>
    class compile_units : public std::list<typename impl::compile_unit>
    {
      friend class subr::create_container;

    protected:
      typedef std::list<typename impl::compile_unit> _base;

      // Constructor copying CUs from input container.
      template<typename input, typename arg_type>
      inline compile_units (const input &other, arg_type &arg)
	: _base (subr::argify<input, compile_units, arg_type &>
		 (other.begin (), arg),
		 subr::argify<input, compile_units, arg_type &>
		 (other.end (), arg))
      {}

    public:
      // Default constructor: an empty container, no CUs.
      inline compile_units () {}

      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);
      }
    };

    template<class impl>
    class compile_unit : public impl::debug_info_entry
    {
      friend class subr::create_container;
      friend class impl::compile_units;

    protected:
      template<typename input>
      static inline const input &require_cu (const input &cu)
      {
	if (cu.tag () != ::DW_TAG_compile_unit)
	  throw std::runtime_error
	    ("top-level debug_info_entry must be DW_TAG_compile_unit");
	return cu;
      }

      template<typename die_type, typename arg_type>
      inline void set (const die_type &die, arg_type &arg)
      {
	impl::debug_info_entry::set (require_cu (die), arg);
      }

    public:
      explicit inline compile_unit ()
	: impl::debug_info_entry ()
      {
	this->_m_tag = ::DW_TAG_compile_unit;
      }

      inline compile_unit (const compile_unit &other)
	: impl::debug_info_entry (require_cu (other))
      {}

      template<typename input, typename arg_type>
      inline compile_unit (const input &cu, arg_type &arg)
	: impl::debug_info_entry (typename impl::debug_info_entry::pointer (),
				  require_cu (cu), arg)
      {}

      /* Assignment details are up to the base class.
	 We just ensure it's really a compile_unit.  */
      inline compile_unit &
      operator= (const typename impl::debug_info_entry &other)
      {
	impl::debug_info_entry::operator= (require_cu (other));
	return *this;
      }

      // Fetch the CU's DW_AT_stmt_list.
      inline const typename impl::line_info_table &line_info () const
      {
	return this->attributes ()[::DW_AT_stmt_list].line_info ();
      }

      inline typename impl::line_info_table &line_info ()
      {
	return this->attributes ()[::DW_AT_stmt_list].line_info ();
      }

      // Convenience methods for line_info_table sub-containers.
      inline const typename impl::directory_table &include_directories () const
      {
	return line_info ().include_directories ();
      }

      inline typename impl::directory_table &include_directories ()
      {
	return line_info ().include_directories ();
      }

      inline const typename impl::line_table &lines () const
      {
	return line_info ().lines ();
      }

      inline typename impl::line_table &lines ()
      {
	return line_info ().lines ();
      }
    };

    class source_file
    {
    private:
      std::string _m_name;
      ::Dwarf_Word _m_mtime;
      ::Dwarf_Word _m_size;

    public:

      struct hasher
	: public std::unary_function<source_file, size_t>
      {
	size_t operator () (const source_file &v) const
	{
	  size_t hash = 0;
	  subr::hash_combine (hash, v._m_name);
	  subr::hash_combine (hash, v._m_mtime);
	  subr::hash_combine (hash, v._m_size);
	  return hash;
	}
      };

      source_file () : _m_name (), _m_mtime (0), _m_size (0) {}
      source_file (const std::string &n, ::Dwarf_Word m = 0, ::Dwarf_Word s = 0)
	: _m_name (n), _m_mtime (m), _m_size (s) {}
      source_file (const char *n, ::Dwarf_Word m = 0, ::Dwarf_Word s = 0)
	: _m_name (n), _m_mtime (m), _m_size (s) {}

      template<typename file>
      source_file (const file &other)
	: _m_name (other.name ()),
	  _m_mtime (other.mtime ()), _m_size (other.size ()) {}

      template<typename file>
      inline source_file &operator= (const file &other)
      {
	_m_name = other.name ();
	_m_mtime = other.mtime ();
	_m_size = other.size ();
	return *this;
      }
      inline source_file &operator= (const std::string &n)
      {
	_m_name = n;
	_m_mtime = 0;
	_m_size = 0;
	return *this;
      }
      inline source_file &operator= (const char *n)
      {
	_m_name = n;
	_m_mtime = 0;
	_m_size = 0;
	return *this;
      }

      std::string to_string () const;

      inline std::string &name ()
      {
	return _m_name;
      }
      inline const std::string &name () const
      {
	return _m_name;
      }
      inline ::Dwarf_Word &mtime ()
      {
	return _m_mtime;
      }
      inline ::Dwarf_Word mtime () const
      {
	return _m_mtime;
      }
      inline ::Dwarf_Word &size ()
      {
	return _m_size;
      }
      inline ::Dwarf_Word size () const
      {
	return _m_size;
      }

      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 name () == other.name ();
      }
      template<typename other_file>
      inline bool operator!= (const other_file &other) const
      {
	return !(*this == other);
      }
    };

    // This describes a CU's directory table, a simple array of strings.
    class directory_table : public std::vector<std::string>
    {
    private:
      typedef std::vector<std::string> _base;

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

      directory_table () {}

      template<typename table>
      directory_table (const table &other)
	: _base (other.begin (), other.end ()) {}

      std::string to_string () const;

      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);
      }
    };

  private:

    /* This is the common base class for all line_entry<T> instantiations.
       For some reason beyond my ken, std::bitset<flag_count>::reference
       as a return type is rejected by the compiler when used in a template
       class, but not a non-template class.  Go figure.  */
    class line_entry_common
    {
    protected:
      unsigned int _m_line;
      unsigned int _m_column;

      enum flag_bit
	{
	  flag_statement,
	  flag_basic_block,
	  flag_end_sequence,
	  flag_prologue_end,
	  flag_epilogue_begin,
	  flag_count
	};
      std::bitset<flag_count> _m_flags;

    public:
      line_entry_common ()
	: _m_line (0), _m_column (0) {}

      inline unsigned int &line ()
      {
	return _m_line;
      }
      inline unsigned int line () const
      {
	return _m_line;
      }
      inline unsigned int &column ()
      {
	return _m_column;
      }
      inline unsigned int column () const
      {
	return _m_column;
      }

#define _DWARF_EDIT_LE_FLAG(what)			\
      bool what () const				\
      {							\
	return _m_flags[flag_##what];			\
      }							\
      std::bitset<flag_count>::reference what ()	\
      {							\
	return _m_flags[flag_##what];			\
      }
      _DWARF_EDIT_LE_FLAG (statement)
      _DWARF_EDIT_LE_FLAG (basic_block)
      _DWARF_EDIT_LE_FLAG (end_sequence)
      _DWARF_EDIT_LE_FLAG (prologue_end)
      _DWARF_EDIT_LE_FLAG (epilogue_begin)
#undef	_DWARF_EDIT_LE_FLAG
    };

  public:
    /* This holds a line table entry.
       It's parameterized by the source_file representation.  */
    template<typename source_file>
    class line_entry : public line_entry_common
    {
    private:
      ::Dwarf_Addr _m_addr;	// XXX dwfl, reloc
      source_file _m_file;

    public:

      struct hasher
	: public std::unary_function<line_entry, size_t>
      {
	size_t operator () (const line_entry &v) const
	{
	  size_t hash = 0;
	  subr::hash_combine (hash, v._m_addr);
	  subr::hash_combine (hash, v._m_file);
	  subr::hash_combine (hash, v._m_line);
	  subr::hash_combine (hash, v._m_column);
	  return hash;
	}
      };

      line_entry (::Dwarf_Addr addr)
	: line_entry_common (), _m_addr (addr), _m_file ()
      {}

      template<typename entry>
      line_entry (const entry &other)
	: line_entry_common (), _m_addr (0), _m_file ()
      {
	*this = other;
      }

      template<typename entry>
      line_entry &operator= (const entry &other)
      {
	_m_addr = other.address ();
	_m_file = other.file ();
	_m_line = other.line ();
	_m_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 ();
	return *this;
      }

      inline ::Dwarf_Addr &address ()
      {
	return _m_addr;
      }
      inline ::Dwarf_Addr address () const
      {
	return _m_addr;
      }
      inline source_file &file ()
      {
	return _m_file;
      }
      inline const source_file &file () const
      {
	return _m_file;
      }

      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);
      }
    };

    /* This holds a line table.
       It's parameterized by the line_entry representation.  */
    template<typename line_entry>
    class line_table : public std::vector<line_entry>
    {
    private:
      typedef std::vector<line_entry> _base;

    public:
      typedef typename _base::size_type size_type;
      typedef typename _base::difference_type difference_type;
      typedef typename _base::value_type value_type;
      typedef typename _base::iterator iterator;
      typedef typename _base::const_iterator const_iterator;

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

      line_table () {}

      template<typename table>
      line_table (const table &other) : _base (other.begin (), other.end ()) {}

      std::string to_string () const;

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

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

    /* This holds the entirety of line information.  It's parameterized
       by the directory_table and line_table representations.  */
    template<typename directory_table, typename line_table>
    class line_info_table
      : private std::pair<directory_table, line_table>
    {
    private:
      typedef std::pair<directory_table, line_table> _base;

    public:
      friend class subr::base_hasher<line_info_table, _base>;
      typedef subr::base_hasher<line_info_table, _base> hasher;

      inline line_info_table () : _base () {}

      template<typename table>
      inline line_info_table (const table &other)
	: _base (other.include_directories (), other.lines ())
      {}

      template<typename table>
      inline line_info_table &operator= (const table &other)
      {
	this->first = directory_table (other.include_directories ());
	this->second = line_table (other.lines ());
	return *this;
      }

      std::string to_string () const;

      inline directory_table &include_directories ()
      {
	return this->first;
      }
      inline const directory_table &include_directories () const
      {
	return this->first;
      }
      inline line_table &lines ()
      {
	return this->second;
      }
      inline const line_table &lines () const
      {
	return this->second;
      }

      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
      : private std::pair< ::Dwarf_Word, unsigned int>
    {
    private:
      typedef std::pair< ::Dwarf_Word, unsigned int> _base;

      inline dwarf_enum ()
	: _base (0, 0)
      {}

    public:
      friend class subr::base_hasher<dwarf_enum, _base>;
      typedef subr::base_hasher<dwarf_enum, _base> hasher;

      inline dwarf_enum (unsigned int attr, unsigned int value)
	: _base (value, attr)
      {}

      template<typename constant>
      inline dwarf_enum (const constant &other)
	: _base (static_cast<unsigned int> (other), other.which ())
      {}

      // Return the DW_AT_* indicating which enum this value belongs to.
      inline unsigned int which () const
      {
	return this->second;
      }

      inline operator unsigned int () const
      {
	return this->first;
      }

      inline dwarf_enum &operator= (::Dwarf_Word value)
      {
	this->first = value;
	return *this;
      }

      inline dwarf_enum &operator= (const dwarf_enum& other)
      {
	if (this->second == 0)
	  {
	    throw std::logic_error ("dwarf_enum default constructed");
	    this->second = other.second;
	  }
	else if (this->second != other.second)
	  throw std::runtime_error
	    ("cannot assign dwarf_constant () from "
	     + dwarf::attributes::name (other.second) + "to "
	     + dwarf::attributes::name (this->second));

	this->first = other.first;
	return *this;
      }

      template<typename constant>
      inline dwarf_enum &operator= (const constant& other)
      {
	return *this = dwarf_enum (other.which (), other);
      }

      std::string to_string () const;

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

      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);
      }
    };

    // Same as set<pair<Dwarf_Addr, Dwarf_Addr>>.
    typedef dwarf::arange_list range_list;

    class location_attr
      : public std::map<dwarf::location_attr::key_type, std::vector<uint8_t> >
    {
    private:
      typedef std::map<dwarf::location_attr::key_type,
		       std::vector<uint8_t> > _base;

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

    public:
      typedef _base::size_type size_type;
      typedef _base::difference_type difference_type;
      typedef _base::key_type key_type;
      typedef _base::mapped_type mapped_type;
      typedef _base::value_type value_type;
      typedef _base::iterator iterator;
      typedef _base::const_iterator const_iterator;

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

      inline location_attr () : _base () {}
      inline location_attr (const location_attr &other)
	: _base (static_cast<const _base &> (other)) {}
      template<typename loc>
      inline location_attr (const loc &other) : _base ()
      {
	*this = other;
      }

      template<typename loc>
      inline location_attr &operator= (const loc &other)
      {
	clear ();
	if (other.empty ())
	  ;
	else if (other.is_list ())
	  for (typename loc::const_iterator i = other.begin ();
	       i != other.end ();
	       ++i)
	    {
	      const typename loc::mapped_type &x = (*i).second;
	      (*this)[(*i).first] = mapped_type (x.begin (), x.end ());
	    }
	else
	  {
	    mapped_type v = other.location ();
	    (*this)[key_type (0, -1)] = v;
	  }
	return *this;
      }

      inline bool is_list () const
      {
	if (empty ())
	  return false;
	if (size () > 1)
	  return true;

	const key_type &elt = begin ()->first;
	return !(elt.first == 0 && elt.second == (Dwarf_Addr) -1);
      }

      inline mapped_type &location ()
      {
	if (empty ())
	  return (*this)[key_type (0, -1)];

	value_type &v = *begin ();
	if (v.first.first != 0 || v.first.second != (Dwarf_Addr) -1
	    || size () > 1)
	  throw std::runtime_error ("location is list, not single location");

	return v.second;
      }
      inline const mapped_type &location () const
      {
	if (size () == 1)
	  {
	    const value_type &v = *begin ();
	    if (v.first.first == 0 && v.first.second == (Dwarf_Addr) -1)
	      return v.second;
	  }
	throw std::runtime_error ("location is list, not single location");
      }

      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 ()
		  && subr::container_equal (location (), other.location ()));

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

      std::string to_string () const;
    };

    template<typename impl, bool alloc_values = true>
    struct value
    {
      struct value_dispatch
      {
	virtual ~value_dispatch () {}
      };

      typedef value_dispatch value_cell_type;

      static const bool delete_value = alloc_values;

      template<typename flavor>
      static inline flavor &
      variant (flavor *&, const value_dispatch *&)
      {
	assert (!alloc_values);
	throw std::logic_error ("can't happen!");
      }

      template<typename flavor>
      static inline flavor &
      variant (flavor *&result, value_dispatch *&value)
      {
	assert (alloc_values);
	if (value == NULL)
	 {
	   result = new flavor;
	   value = result;
	   return *result;
	 }
	result = dynamic_cast<flavor *> (value);
	if (result == NULL)
	  throw std::runtime_error ("wrong value type");
	return *result;
      }

      template<typename arg_type>
      struct maker
      {
	inline explicit maker (const arg_type &) {}

	template<typename flavor, typename input>
	static inline void
	make (value_dispatch *&v, flavor *&result,
	      int /*whatattr*/, const input &x, arg_type &arg)
	{
	  assert (alloc_values);
	  v = result = new flavor (x, arg);
	}
      };

      template<typename arg_type>
      static inline maker<arg_type> make (arg_type &arg)
      {
	return maker<arg_type> (arg);
      }

      struct value_string : public value_dispatch, public std::string
      {
	typedef subr::hash<std::string> hasher;

	inline value_string () {}

	template<typename string, typename arg_type>
	inline value_string (const string &s, arg_type &)
	  : std::string (s)
	{}

	template<typename string>
	inline value_string (const string &s)
	  : std::string (s)
	{}

	std::string to_string () const
	{
	  std::string result ("\"");
	  result += *this;
	  result += "\"";
	  return result;
	}
      };

      struct value_identifier : public value_string
      {
	inline value_identifier () {}

	template<typename id, typename arg_type>
	inline value_identifier (const id &s, arg_type &arg)
	  : value_string (s, arg)
	{}

	template<typename id>
	inline value_identifier (const id &s)
	  : value_string (s)
	{}
      };

      struct value_reference : public value_dispatch
      {
	typedef typename impl::debug_info_entry::pointer value_type;
	value_type ref;

	// Default constructor: reference to nowhere, invalid.
	inline value_reference ()
	  : ref ()
	{}

	/* This is only kosher for a pointer into the same dwarf_edit
	   object.  This is what plain assignment does.  This just uses
	   this pointer, rather than translating it from another file
	   into this one (which requires a tracker).  */
	inline value_reference (const value_type &i, subr::nothing &)
	  : ref (i)
	{}

	template<typename iter, typename tracker>
	inline value_reference (const iter &i, tracker &t)
	  : ref ()		// Invalid until t.finish ().
	{
	  t.refer (&ref, i);
	}
      };

      struct value_flag : public value_dispatch
      {
	bool flag;

	inline value_flag (bool t = true)
	  : flag (t)
	{}

	template<typename arg_type>
	inline value_flag (bool t, arg_type &)
	  : flag (t)
	{}
      };

      struct value_address : public value_dispatch
      {
	// XXX dwfl, reloc
	::Dwarf_Addr addr;

	inline value_address (::Dwarf_Addr x = 0)
	  : addr (x)
	{}

	template<typename arg_type>
	inline value_address (::Dwarf_Addr x, arg_type &)
	  : addr (x)
	{}

	struct hasher : public std::unary_function<value_address, size_t>
	{
	  inline size_t operator () (const value_address &c) const
	  {
	    return c.addr;
	  }
	};

	inline operator ::Dwarf_Addr () const
	{
	  return addr;
	}

	inline bool operator== (::Dwarf_Addr x) const
	{
	  return addr == x;
	}
      };

      struct value_rangelistptr : public value_dispatch, public range_list
      {
	inline value_rangelistptr () {}

	template<typename list, typename arg_type>
	inline value_rangelistptr (const list &other, arg_type &)
	  : range_list (other)
	{}

	template<typename list>
	inline value_rangelistptr (const list &other)
	  : range_list (other)
	{}
      };

      struct value_lineptr : public value_dispatch, public impl::line_info_table
      {
	inline value_lineptr () {}

	template<typename table, typename arg_type>
	inline value_lineptr (const table &other, arg_type &)
	  : impl::line_info_table (other)
	{}
      };

      struct value_constant : public value_dispatch
      {
	union
	{
	  ::Dwarf_Word word;
	  ::Dwarf_Sword sword;
	};

	inline value_constant (::Dwarf_Word value = 0)
	  : word (value)
	{}

	template<typename arg_type>
	inline value_constant (::Dwarf_Word x, arg_type &)
	  : word (x)
	{}

	struct hasher : public std::unary_function<value_constant, size_t>
	{
	  inline size_t operator () (const value_constant &c) const
	  {
	    return c.word;
	  }
	};

	inline operator ::Dwarf_Word () const
	{
	  return word;
	}

	inline bool operator== (::Dwarf_Word x) const
	{
	  return word == x;
	}
      };

      struct value_constant_block : public value_dispatch,
				    public std::vector<uint8_t>
      {
	typedef subr::hash<std::vector<uint8_t> > hasher;

	inline value_constant_block () {}

	template<typename block, typename arg_type>
	inline value_constant_block (const block &b, arg_type &)
	  : std::vector<uint8_t> (b.begin (), b.end ())
	{}

	template<typename block>
	inline value_constant_block (const block &b)
	  : std::vector<uint8_t> (b.begin (), b.end ())
	{}
      };

      struct value_dwarf_constant : public value_dispatch, public dwarf_enum
      {
	inline value_dwarf_constant () {}

	template<typename constant>
	inline value_dwarf_constant (const constant &other)
	  : dwarf_enum (other)
	{}

	template<typename constant, typename arg_type>
	inline value_dwarf_constant (const constant &other, arg_type &)
	  : dwarf_enum (other)
	{}
      };

      struct value_source_file : public value_dispatch, public source_file
      {
	inline value_source_file () {}

	template<typename file, typename arg_type>
	inline value_source_file (const file &other, arg_type &)
	  : source_file (other)
	{}

	template<typename file>
	inline value_source_file (const file &other)
	  : source_file (other)
	{}
      };

      struct value_source_line : public value_dispatch
      {
	unsigned int n;

	inline value_source_line (unsigned int x = 0)
	  : n (x)
	{}

	template<typename arg_type>
	inline value_source_line (unsigned int m, arg_type &)
	  : n (m)
	{}

	struct hasher : public std::unary_function<value_source_line, size_t>
	{
	  inline size_t operator () (const value_source_line &c) const
	  {
	    return c.n;
	  }
	};

	inline operator unsigned int () const
	{
	  return n;
	}

	inline bool operator== (unsigned int x) const
	{
	  return n == x;
	}
      };

      class value_source_column : public value_source_line
      {
      };

      struct value_macptr : public value_dispatch {};

      struct value_location : public value_dispatch, public location_attr
      {
	inline value_location () {}

	template<typename loc, typename arg_type>
	inline value_location (const loc &other, arg_type &)
	  : location_attr (other)
	{}

	template<typename loc>
	inline value_location (const loc &other)
	  : location_attr (other)
	{}
      };
    };

    // Forward decl.
    template<class impl, typename v = value<impl> > class attributes_type;

    template<class impl, typename vw = value<impl> >
    class attr_value
    {
      friend class attributes_type<impl, vw>;
      friend class value<impl>;

    protected:
      typename vw::value_cell_type *_m_value;
      typedef typename impl::debug_info_entry::pointer die_ptr;

      template<typename value, typename arg_type = subr::nothing>
      struct init
      {
	inline init (attr_value *av, int whatattr,
		     const value &other, arg_type &arg)
	{
	  do_init (av, whatattr, other, arg);
	}

	inline init (attr_value *av, int whatattr, const value &other)
	{
	  arg_type dummy;
	  do_init (av, whatattr, other, dummy);
	}

	static inline void do_init (attr_value *av, int whatattr,
				    const value &other, arg_type &arg)
	{
	  switch (other.what_space ())
	    {
#define _DWARF_DATA_AV_MAKE(flavor, fetch)				      \
	      case dwarf::VS_##flavor:					      \
		{							      \
		  typename vw::value_##flavor *p;			      \
		  vw::make (arg)	      				      \
		    .make (av->_m_value, p, whatattr, other.fetch (), arg);   \
		}							      \
	      break

	      _DWARF_DATA_AV_MAKE (identifier, identifier);
	      _DWARF_DATA_AV_MAKE (string, string);
	      _DWARF_DATA_AV_MAKE (flag, flag);
	      _DWARF_DATA_AV_MAKE (rangelistptr, ranges);
	      _DWARF_DATA_AV_MAKE (lineptr, line_info);
	      _DWARF_DATA_AV_MAKE (address, address);
	      _DWARF_DATA_AV_MAKE (source_line, source_line);
	      _DWARF_DATA_AV_MAKE (source_file, source_file);
	      _DWARF_DATA_AV_MAKE (dwarf_constant, dwarf_constant);
	      _DWARF_DATA_AV_MAKE (reference, reference);
	      _DWARF_DATA_AV_MAKE (location, location);
	      //_DWARF_DATA_AV_MAKE (macptr, macros); 	XXX

	    case dwarf::VS_constant:
	      if (other.constant_is_integer ())
		{
		  typename vw::value_constant *p;
		  vw::make (arg).make (av->_m_value, p, whatattr,
				       other.constant (), arg);
		}
	      else
		{
		  typename vw::value_constant_block *p;
		  vw::make (arg).make (av->_m_value, p, whatattr,
				       other.constant_block (), arg);
		}
	      break;

	    default:
	    case dwarf::VS_discr_list:
	      throw std::runtime_error ("XXX unimplemented");

#undef _DWARF_DATA_AV_MAKE
	    }
	}
      };

      // This is kosher only when freshly default-constructed.
      template<typename value, typename arg_type>
      inline void set (int whatattr, const value &other, arg_type &arg)
      {
	assert (_m_value == NULL);
	init<value, arg_type> me (this, whatattr, other, arg);
      }

      template<typename flavor>
      inline flavor &const_variant () const
      {
	flavor *p = dynamic_cast<flavor *> (_m_value);
	if (p == NULL)
	  throw std::runtime_error (_m_value != NULL ? "wrong value type"
				    : "uninitialized attr_value (const?)");
	return *p;
      }

      template<typename flavor>
      inline const flavor &variant () const
      {
	return const_variant<const flavor> ();
      }

      template<typename flavor>
      inline flavor &variant ()
      {
	flavor *p;
	return vw::variant (p, _m_value);
      }

      template<typename value>
      inline attr_value &copy (const value &other)
      {
	if (_m_value != NULL)
	  {
	    delete _m_value;
	    _m_value = NULL;
	  }
	init<value> me (this, 0, other);
	return *this;
      }

    public:
      attr_value (const attr_value &other)
	: _m_value (NULL)
      {
	if (other._m_value != NULL)
	  init<attr_value> me (this, 0, other);
      }

      inline attr_value ()
	: _m_value (NULL)
      {}

      ~attr_value ()
      {
	if (vw::delete_value && _m_value != NULL)
	  delete _m_value;
      }

      template<typename value>
      inline attr_value &operator= (const value &other)
      {
	return copy (other);
      }

      /* This is the same as the generic template one,
	 but we have to define it explicitly to override
	 the default assignment operator.  */
      inline attr_value &operator= (const attr_value &other)
      {
	return copy (other);
      }

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

      inline const bool &flag () const
      {
	return variant<typename vw::value_flag> ().flag;
      }

      inline bool &flag ()
      {
	return variant<typename vw::value_flag> ().flag;
      }

      // XXX dwfl, reloc
      inline const ::Dwarf_Addr &address () const
      {
	return variant<typename vw::value_address> ().addr;
      }

      // XXX dwfl, reloc
      inline ::Dwarf_Addr &address ()
      {
	return variant<typename vw::value_address> ().addr;
      }

      inline const die_ptr &reference () const
      {
	return variant<typename vw::value_reference> ().ref;
      }

      inline die_ptr &reference ()
      {
	return variant<typename vw::value_reference> ().ref;
      }

      inline const location_attr &location () const
      {
	return static_cast<const location_attr &>
	  (variant<typename vw::value_location> ());
      }

      inline location_attr &location ()
      {
	return static_cast<location_attr &>
	  (variant<typename vw::value_location> ());
      }

      inline const std::string &string () const
      {
	return static_cast<const std::string &>
	  (variant<typename vw::value_string> ());
      }

      inline std::string &string ()
      {
	return static_cast<std::string &>
	  (variant<typename vw::value_string> ());
      }

      inline const std::string &identifier () const
      {
	return static_cast<const std::string &>
	  (variant<typename vw::value_identifier> ());
      }

      inline std::string &identifier ()
      {
	return static_cast<std::string &>
	  (variant<typename vw::value_identifier> ());
      }

      inline const typename impl::source_file &source_file () const
      {
	return static_cast<const typename impl::source_file &>
	  (variant<typename vw::value_source_file> ());
      }

      inline typename impl::source_file &source_file ()
      {
	return static_cast<typename impl::source_file &>
	  (variant<typename vw::value_source_file> ());
      }

      inline const unsigned int &source_line () const
      {
	return variant<typename vw::value_source_line> ().n;
      }

      inline unsigned int &source_line ()
      {
	return variant<typename vw::value_source_line> ().n;
      }

      inline const unsigned int &source_column () const
      {
	return variant<typename vw::value_source_column> ().n;
      }

      inline unsigned int &source_column ()
      {
	return variant<typename vw::value_source_column> ().n;
      }

      inline const ::Dwarf_Word &constant () const
      {
	return variant<typename vw::value_constant> ().word;
      }

      inline ::Dwarf_Word &constant ()
      {
	return variant<typename vw::value_constant> ().word;
      }

      inline const ::Dwarf_Sword &signed_constant () const
      {
	return variant<typename vw::value_constant> ().sword;
      }

      inline ::Dwarf_Sword &signed_constant ()
      {
	return variant<typename vw::value_constant> ().sword;
      }

      inline const std::vector<uint8_t> &constant_block () const
      {
	return static_cast<const std::vector<uint8_t> &>
	  (variant<typename vw::value_constant_block> ());
      }

      inline std::vector<uint8_t> &constant_block ()
      {
	return static_cast<std::vector<uint8_t> &>
	  (variant<typename vw::value_constant_block> ());
      }

      inline const typename impl::dwarf_enum &dwarf_constant () const
      {
	return variant<typename vw::value_dwarf_constant> ();
      }

      inline typename impl::dwarf_enum &dwarf_constant ()
      {
	return variant<typename vw::value_dwarf_constant> ();
      }

      inline bool constant_is_integer () const
      {
	return (dynamic_cast<const typename vw::value_constant *> (_m_value)
		!= NULL);
      }

      inline const typename impl::range_list &ranges () const
      {
	return static_cast<const range_list &>
	  (variant<const typename vw::value_rangelistptr> ());
      }

      inline typename impl::range_list &ranges ()
      {
	return static_cast<range_list &>
	  (variant<typename vw::value_rangelistptr> ());
      }

      inline const typename impl::line_info_table &line_info () const
      {
	return static_cast<const typename impl::line_info_table &>
	  (variant<const typename vw::value_lineptr> ());
      }

      inline typename impl::line_info_table &line_info ()
      {
	return static_cast<typename impl::line_info_table &>
	  (variant<typename vw::value_lineptr> ());
      }

      // macptr

      template<typename value>
      inline bool operator== (const value &other) const
      {
	const dwarf::value_space what = what_space ();
	if (likely (other.what_space () == what))
	  switch (what)
	    {
	    case dwarf::VS_identifier:
	      return identifier () == other.identifier ();
	    case dwarf::VS_string:
	      return string () == other.string ();
	    case dwarf::VS_reference:
	      return reference () == other.reference ();
	    case dwarf::VS_flag:
	      return flag () == other.flag ();
	    case dwarf::VS_rangelistptr:
	      return ranges () == other.ranges ();
	    case dwarf::VS_lineptr:
	      return line_info () == other.line_info ();
	    case dwarf::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 dwarf::VS_source_file:
	      return source_file () == other.source_file ();
	    case dwarf::VS_source_line:
	      return source_line () == other.source_line ();
	    case dwarf::VS_source_column:
	      return source_column () == other.source_column ();
	    case dwarf::VS_address:
	      return address () == other.address ();
	    case dwarf::VS_location:
	      return location () == other.location ();
	    case dwarf::VS_dwarf_constant:
	      return dwarf_constant () == other.dwarf_constant ();
#if 0
	    case dwarf::VS_macptr:
	      return macptr () == other.macptr ();
#endif
	    default:
	    case dwarf::VS_discr_list:
	      throw std::runtime_error ("XXX unimplemented");
	    }
	return false;
      }
      template<typename value>
      inline bool operator!= (const value &other) const
      {
	return !(*this == other);
      }
    };

    template<class impl, typename v>
    class attributes_type
      : public std::map<int, typename impl::attr_value>
    {
      friend class impl::me;
    protected:
      typedef std::map<int, typename impl::attr_value> base_type;

      inline attributes_type () {}

      template<typename iter>
      inline attributes_type (const iter &from, const iter &to)
	: base_type (from, to)
      {}

      template<typename input, typename arg_type>
      inline void set (const input &other, arg_type &c)
      {
	for (typename input::const_iterator attr = other.begin ();
	     attr != other.end ();
	     ++attr)
	  (*this)[(*attr).first].set ((*attr).first, (*attr).second, c);
      }

    public: // XXX should be protected

      /* We don't use the base_type (begin, end) iterator constructor here
	 for good reason.  The ref-maker needs to collect back-pointers
	 into our mapped_value (attr_value) objects.  It would not fly to
	 have that done in a temporary attr_value object that gets copied
	 into the map cell by assignment.  We must make sure that when a
	 value_reference is constructed, it is really the one sitting in
	 our map that the ref-maker will want to update later.  */
      template<typename input, typename arg_type>
      inline attributes_type (const input &other, arg_type &c)
	: base_type ()
      {
	set (other, c);
      }

    public:
      typedef typename base_type::key_type key_type;
      typedef typename base_type::value_type value_type;
      typedef typename base_type::mapped_type mapped_type;

      static const bool ordered = true;

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

  };

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

  template<class impl, typename v>
  inline std::string dwarf_data::attr_value<impl, v>::to_string () const
  {
    return elfutils::to_string (*this); // Use that.
  }

};

#endif