File: dwarf_query.c

package info (click to toggle)
dwarfutils 20180809-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,228 kB
  • sloc: ansic: 63,382; sh: 5,222; cpp: 4,041; makefile: 548; python: 445; awk: 11
file content (1841 lines) | stat: -rw-r--r-- 54,499 bytes parent folder | download
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
/*

  Copyright (C) 2000,2002,2004 Silicon Graphics, Inc.  All Rights Reserved.
  Portions Copyright (C) 2007-2018 David Anderson. All Rights Reserved.
  Portions Copyright 2012 SN Systems Ltd. All rights reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2.1 of the GNU Lesser General Public License
  as published by the Free Software Foundation.

  This program is distributed in the hope that it would be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  Further, this software is distributed without any warranty that it is
  free of the rightful claim of any third person regarding infringement
  or the like.  Any license provided herein, whether implied or
  otherwise, applies only to this software file.  Patent licenses, if
  any, provided herein do not apply to combinations of this program with
  other software, or any other product whatsoever.

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

*/

#include "config.h"
#include <stdio.h>
#include "dwarf_incl.h"
#include "dwarf_alloc.h"
#include "dwarf_error.h"
#include "dwarf_util.h"
#include "dwarf_die_deliv.h"

#define TRUE 1
static int _dwarf_die_attr_unsigned_constant(Dwarf_Die die,
    Dwarf_Half attr,
    Dwarf_Unsigned * return_val,
    Dwarf_Error * error);

static int _dwarf_get_ranges_base_attr_value(Dwarf_Debug dbg,
    Dwarf_CU_Context context,
    Dwarf_Unsigned * rabase_out,
    Dwarf_Error    * error);

static int _dwarf_get_address_base_attr_value(Dwarf_Debug dbg,
    Dwarf_CU_Context context,
    Dwarf_Unsigned *abase_out,
    Dwarf_Error *error);

int dwarf_get_offset_size(Dwarf_Debug dbg,
    Dwarf_Half  *    offset_size,
    Dwarf_Error *    error)
{
    if (dbg == 0) {
        _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
        return (DW_DLV_ERROR);
    }
    *offset_size = dbg->de_length_size;
    return DW_DLV_OK;
}

#if 0
static void
dump_bytes(char * msg,Dwarf_Small * start, long len)
{
    Dwarf_Small *end = start + len;
    Dwarf_Small *cur = start;

    printf("%s ",msg);
    for (; cur < end; cur++) {
        printf("%02x ", *cur);
    }
    printf("\n");
}
#endif



/* This is normally reliable.
But not always.
If different compilation
units have different address sizes
this may not give the correct value in all contexts.
If the Elf offset size != address_size
(for example if address_size = 4 but recorded in elf64 object)
this may not give the correct value in all contexts.
*/
int
dwarf_get_address_size(Dwarf_Debug dbg,
    Dwarf_Half * ret_addr_size, Dwarf_Error * error)
{
    Dwarf_Half address_size = 0;

    if (dbg == 0) {
        _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
        return (DW_DLV_ERROR);
    }
    address_size = dbg->de_pointer_size;
    *ret_addr_size = address_size;
    return DW_DLV_OK;
}

/* This will be correct in all contexts where the
   CU context of a DIE is known.
*/
int
dwarf_get_die_address_size(Dwarf_Die die,
    Dwarf_Half * ret_addr_size, Dwarf_Error * error)
{
    Dwarf_Half address_size = 0;
    CHECK_DIE(die, DW_DLV_ERROR);
    address_size = die->di_cu_context->cc_address_size;
    *ret_addr_size = address_size;
    return DW_DLV_OK;
}

int
dwarf_dieoffset(Dwarf_Die die,
    Dwarf_Off * ret_offset, Dwarf_Error * error)
{
    Dwarf_Small *dataptr = 0;
    Dwarf_Debug dbg = 0;

    CHECK_DIE(die, DW_DLV_ERROR);
    dbg = die->di_cu_context->cc_dbg;
    dataptr = die->di_is_info? dbg->de_debug_info.dss_data:
        dbg->de_debug_types.dss_data;

    *ret_offset = (die->di_debug_ptr - dataptr);
    return DW_DLV_OK;
}


/*  This function returns the offset of
    the die relative to the start of its
    compilation-unit rather than .debug_info.
    Returns DW_DLV_ERROR on error.  */
int
dwarf_die_CU_offset(Dwarf_Die die,
    Dwarf_Off * cu_off, Dwarf_Error * error)
{
    Dwarf_CU_Context cu_context = 0;
    Dwarf_Small *dataptr = 0;
    Dwarf_Debug dbg = 0;

    CHECK_DIE(die, DW_DLV_ERROR);
    cu_context = die->di_cu_context;
    dbg = die->di_cu_context->cc_dbg;
    dataptr = die->di_is_info? dbg->de_debug_info.dss_data:
        dbg->de_debug_types.dss_data;

    *cu_off = (die->di_debug_ptr - dataptr - cu_context->cc_debug_offset);
    return DW_DLV_OK;
}

/*  A common function to get both offsets (local and global)
    It's unusual in that it sets both return offsets
    to zero on entry.  Normally we only set any
    output-args (through their pointers) in case
    of success.  */
int
dwarf_die_offsets(Dwarf_Die die,
    Dwarf_Off *off,
    Dwarf_Off *cu_off,
    Dwarf_Error *error)
{
    int res = 0;

    *off = 0;
    *cu_off = 0;
    res = dwarf_dieoffset(die,off,error);
    if (res == DW_DLV_OK) {
        res = dwarf_die_CU_offset(die,cu_off,error);
    }
    return res;
}

/*  This function returns the global offset
    (meaning the section offset) and length of
    the CU that this die is a part of.
    Used for correctness checking by dwarfdump.  */
int
dwarf_die_CU_offset_range(Dwarf_Die die,
    Dwarf_Off * cu_off,
    Dwarf_Off * cu_length,
    Dwarf_Error * error)
{
    Dwarf_CU_Context cu_context = 0;

    CHECK_DIE(die, DW_DLV_ERROR);
    cu_context = die->di_cu_context;

    *cu_off = cu_context->cc_debug_offset;
    *cu_length = cu_context->cc_length + cu_context->cc_length_size
        + cu_context->cc_extension_size;
    return DW_DLV_OK;
}



int
dwarf_tag(Dwarf_Die die, Dwarf_Half * tag, Dwarf_Error * error)
{
    CHECK_DIE(die, DW_DLV_ERROR);
    *tag = die->di_abbrev_list->abl_tag;
    return DW_DLV_OK;
}

/* Returns the children offsets for the given offset */
int
dwarf_offset_list(Dwarf_Debug dbg,
    Dwarf_Off offset, Dwarf_Bool is_info,
    Dwarf_Off **offbuf, Dwarf_Unsigned *offcnt, Dwarf_Error * error)
{
    Dwarf_Die die = 0;
    Dwarf_Die child = 0;
    Dwarf_Die sib_die = 0;
    Dwarf_Die cur_die = 0;
    Dwarf_Off cur_off = 0;
    Dwarf_Word off_count = 0;
    int res = 0;

    /* Temporary counter. */
    Dwarf_Unsigned i = 0;

    /* Points to contiguous block of Dwarf_Off's to be returned. */
    Dwarf_Off *ret_offsets = 0;

    Dwarf_Chain_2 curr_chain = 0;
    Dwarf_Chain_2 prev_chain = 0;
    Dwarf_Chain_2 head_chain = 0;

    *offbuf = NULL;
    *offcnt = 0;

    /* Get DIE for offset */
    res = dwarf_offdie_b(dbg,offset,is_info,&die,error);
    if (DW_DLV_OK != res) {
        return res;
    }

    /* Get first child for die */
    res = dwarf_child(die,&child,error);
    if (DW_DLV_ERROR == res || DW_DLV_NO_ENTRY == res) {
        return res;
    }

    cur_die = child;
    for (;;) {
        if (DW_DLV_OK == res) {
            /* Get Global offset for current die */
            dwarf_dieoffset(cur_die,&cur_off,error);

            /* Record offset in current entry chain */
            curr_chain = (Dwarf_Chain_2)_dwarf_get_alloc(dbg,DW_DLA_CHAIN_2,1);
            if (curr_chain == NULL) {
                _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
                return (DW_DLV_ERROR);
            }

            /* Put current offset on singly_linked list. */
            curr_chain->ch_item = cur_off;
            ++off_count;

            if (head_chain == NULL) {
                head_chain = prev_chain = curr_chain;
            }
            else {
                prev_chain->ch_next = curr_chain;
                prev_chain = curr_chain;
            }
        }

        /* Process any siblings entries if any */
        sib_die = 0;
        res = dwarf_siblingof_b(dbg,cur_die,is_info,&sib_die,error);
        if (DW_DLV_ERROR == res) {
            return res;
        }
        if (DW_DLV_NO_ENTRY == res) {
            /* Done at this level. */
            break;
        }
        /* res == DW_DLV_OK */
        if (cur_die != die) {
            dwarf_dealloc(dbg,cur_die,DW_DLA_DIE);
        }
        cur_die = sib_die;
    }

    /* Points to contiguous block of Dwarf_Off's. */
    ret_offsets = (Dwarf_Off *) _dwarf_get_alloc(dbg, DW_DLA_ADDR, off_count);
    if (ret_offsets == NULL) {
        _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
        return (DW_DLV_ERROR);
    }

    /*  Store offsets in contiguous block, and deallocate the chain. */
    curr_chain = head_chain;
    for (i = 0; i < off_count; i++) {
        *(ret_offsets + i) = curr_chain->ch_item;
        prev_chain = curr_chain;
        curr_chain = curr_chain->ch_next;
        dwarf_dealloc(dbg, prev_chain, DW_DLA_CHAIN_2);
    }

    *offbuf = ret_offsets;
    *offcnt = off_count;

    return DW_DLV_OK;
}

/*  If the input is improper (see DW_DLV_ERROR)
    this may leak memory. Such badly formed input
    should be very very rare.
*/
int
dwarf_attrlist(Dwarf_Die die,
    Dwarf_Attribute ** attrbuf,
    Dwarf_Signed * attrcnt, Dwarf_Error * error)
{
    Dwarf_Word attr_count = 0;
    Dwarf_Word i = 0;
    Dwarf_Half attr = 0;
    Dwarf_Half attr_form = 0;
    Dwarf_Byte_Ptr abbrev_ptr = 0;
    Dwarf_Byte_Ptr abbrev_end = 0;
    Dwarf_Abbrev_List abbrev_list = 0;
    Dwarf_Attribute new_attr = 0;
    Dwarf_Attribute head_attr = NULL;
    Dwarf_Attribute curr_attr = NULL;
    Dwarf_Attribute *attr_ptr = 0;
    Dwarf_Debug dbg = 0;
    Dwarf_Byte_Ptr info_ptr = 0;
    Dwarf_Byte_Ptr die_info_end = 0;
    int lres = 0;
    Dwarf_CU_Context context = 0;

    CHECK_DIE(die, DW_DLV_ERROR);
    context = die->di_cu_context;
    dbg = context->cc_dbg;
    die_info_end =
        _dwarf_calculate_info_section_end_ptr(context);

    lres = _dwarf_get_abbrev_for_code(context,
        die->di_abbrev_list->abl_code,
        &abbrev_list,error);
    if (lres == DW_DLV_ERROR) {
        return lres;
    }
    if (lres == DW_DLV_NO_ENTRY) {
        _dwarf_error(dbg, error, DW_DLE_ABBREV_MISSING);
        return DW_DLV_ERROR;
    }

    abbrev_ptr = abbrev_list->abl_abbrev_ptr;
    abbrev_end = _dwarf_calculate_abbrev_section_end_ptr(context);


    info_ptr = die->di_debug_ptr;
    SKIP_LEB128_WORD_CK(info_ptr,dbg,error,die_info_end);
    if (info_ptr >= die_info_end) {
        /* Stepped off the end SKIPping the leb  */
        _dwarf_error(dbg, error, DW_DLE_DIE_BAD);
        return DW_DLV_ERROR;
    }

    do {
        Dwarf_Unsigned utmp2;

        DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp2,dbg,error,abbrev_end);
        attr = (Dwarf_Half) utmp2;
        DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp2,dbg,error,abbrev_end);
        attr_form = (Dwarf_Half) utmp2;

        if (!_dwarf_valid_form_we_know(dbg,attr_form,attr)) {
            _dwarf_error(dbg, error, DW_DLE_UNKNOWN_FORM);
            return DW_DLV_ERROR;
        }

        if (attr != 0) {
            new_attr =
                (Dwarf_Attribute) _dwarf_get_alloc(dbg, DW_DLA_ATTR, 1);
            if (new_attr == NULL) {
                _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
                return DW_DLV_ERROR;
            }

            new_attr->ar_attribute = attr;
            new_attr->ar_attribute_form_direct = attr_form;
            new_attr->ar_attribute_form = attr_form;
            if (attr_form == DW_FORM_indirect) {
                Dwarf_Unsigned utmp6;
                if (_dwarf_reference_outside_section(die,
                    (Dwarf_Small*) info_ptr,
                    ((Dwarf_Small*) info_ptr )+1)) {
                    _dwarf_error(dbg, error,DW_DLE_ATTR_OUTSIDE_SECTION);
                    return DW_DLV_ERROR;
                }

                /* DECODE_LEB128_UWORD does info_ptr update */
                DECODE_LEB128_UWORD_CK(info_ptr, utmp6,dbg,error,die_info_end);
                attr_form = (Dwarf_Half) utmp6;
                new_attr->ar_attribute_form = attr_form;
            }
            /*  Here the final address must be *inside* the section, as we
                will read from there, and read at least one byte, we think.
                We do not want info_ptr to point past end so we add 1 to
                the end-pointer.  */
            if (_dwarf_reference_outside_section(die,
                (Dwarf_Small*) info_ptr,
                ((Dwarf_Small*) info_ptr )+1)) {
                _dwarf_error(dbg, error,DW_DLE_ATTR_OUTSIDE_SECTION);
                return DW_DLV_ERROR;
            }
            new_attr->ar_cu_context = die->di_cu_context;
            new_attr->ar_debug_ptr = info_ptr;
            new_attr->ar_die = die;
            {
                Dwarf_Unsigned sov = 0;
                int res = _dwarf_get_size_of_val(dbg,
                    attr_form,
                    die->di_cu_context->cc_version_stamp,
                    die->di_cu_context->cc_address_size,
                    info_ptr,
                    die->di_cu_context->cc_length_size,
                    &sov,
                    die_info_end,
                    error);
                if(res!= DW_DLV_OK) {
                    return res;
                }
                info_ptr += sov;
            }

            if (head_attr == NULL)
                head_attr = curr_attr = new_attr;
            else {
                curr_attr->ar_next = new_attr;
                curr_attr = new_attr;
            }
            attr_count++;
        }
    } while (attr != 0 || attr_form != 0);

    if (attr_count == 0) {
        *attrbuf = NULL;
        *attrcnt = 0;
        return (DW_DLV_NO_ENTRY);
    }

    attr_ptr = (Dwarf_Attribute *)
        _dwarf_get_alloc(dbg, DW_DLA_LIST, attr_count);
    if (attr_ptr == NULL) {
        _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
        return (DW_DLV_ERROR);
    }

    curr_attr = head_attr;
    for (i = 0; i < attr_count; i++) {
        *(attr_ptr + i) = curr_attr;
        curr_attr = curr_attr->ar_next;
    }

    *attrbuf = attr_ptr;
    *attrcnt = attr_count;
    return (DW_DLV_OK);
}


/*
    This function takes a die, and an attr, and returns
    a pointer to the start of the value of that attr in
    the given die in the .debug_info section.  The form
    is returned in *attr_form.

    Returns NULL on error, or if attr is not found.
    However, *attr_form is 0 on error, and positive
    otherwise.
*/
static int
_dwarf_get_value_ptr(Dwarf_Die die,
    Dwarf_Half attr,
    Dwarf_Half * attr_form,
    Dwarf_Byte_Ptr * ptr_to_value,
    Dwarf_Error *error)
{
    Dwarf_Byte_Ptr abbrev_ptr = 0;
    Dwarf_Byte_Ptr abbrev_end = 0;
    Dwarf_Abbrev_List abbrev_list;
    Dwarf_Half curr_attr = 0;
    Dwarf_Half curr_attr_form = 0;
    Dwarf_Byte_Ptr info_ptr = 0;
    Dwarf_CU_Context context = die->di_cu_context;
    Dwarf_Byte_Ptr die_info_end = 0;
    Dwarf_Debug dbg = 0;
    int lres = 0;

    if (!context) {
        _dwarf_error(NULL,error,DW_DLE_DIE_NO_CU_CONTEXT);
        return DW_DLV_ERROR;
    }
    dbg = context->cc_dbg;
    die_info_end =
        _dwarf_calculate_info_section_end_ptr(context);

    lres = _dwarf_get_abbrev_for_code(context,
        die->di_abbrev_list->abl_code,
        &abbrev_list,error);
    if (lres == DW_DLV_ERROR) {
        return lres;
    }
    if (lres == DW_DLV_NO_ENTRY) {
        _dwarf_error(dbg,error,DW_DLE_CU_DIE_NO_ABBREV_LIST);
        return DW_DLV_ERROR;
    }

    abbrev_ptr = abbrev_list->abl_abbrev_ptr;
    abbrev_end = _dwarf_calculate_abbrev_section_end_ptr(context);

    info_ptr = die->di_debug_ptr;
    /* This ensures and checks die_info_end >= info_ptr */
    SKIP_LEB128_WORD_CK(info_ptr,dbg,error,die_info_end);

    do {
        Dwarf_Unsigned utmp3 = 0;
        Dwarf_Unsigned value_size=0;
        int res = 0;

        DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp3,dbg,error,abbrev_end);
        curr_attr = (Dwarf_Half) utmp3;
        DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp3,dbg,error,abbrev_end);
        curr_attr_form = (Dwarf_Half) utmp3;
        if (curr_attr_form == DW_FORM_indirect) {
            Dwarf_Unsigned utmp6;

            /* DECODE_LEB128_UWORD updates info_ptr */
            DECODE_LEB128_UWORD_CK(info_ptr, utmp6,dbg,error,die_info_end);
            curr_attr_form = (Dwarf_Half) utmp6;
        }

        if (curr_attr == attr) {
            *attr_form = curr_attr_form;
            *ptr_to_value = info_ptr;
            return DW_DLV_OK;
        }

        res = _dwarf_get_size_of_val(dbg,
            curr_attr_form,
            die->di_cu_context->cc_version_stamp,
            die->di_cu_context->cc_address_size,
            info_ptr,
            die->di_cu_context->cc_length_size,
            &value_size,
            die_info_end,
            error);
        if (res != DW_DLV_OK) {
            return res;
        }
        {
            /* ptrdiff_t is signed type, so use DW signed type */
            Dwarf_Signed len = die_info_end - info_ptr;
            if (len < 0 || (value_size > ((Dwarf_Unsigned)len))) {
                /*  Something badly wrong. We point past end
                    of debug_info or debug_types or a
                    section is unreasonably sized or we are
                    pointing to two different sections? */
                _dwarf_error(dbg,error,DW_DLE_DIE_ABBREV_BAD);
                return DW_DLV_ERROR;
            }
        }
        info_ptr+= value_size;
    } while (curr_attr != 0 || curr_attr_form != 0);
    return DW_DLV_NO_ENTRY;
}

int
dwarf_die_text(Dwarf_Die die,
    Dwarf_Half attrnum,
    char **ret_name,
    Dwarf_Error * error)
{
    Dwarf_Debug dbg = 0;
    int res = DW_DLV_ERROR;
    Dwarf_Attribute attr = 0;
    Dwarf_Error lerr = 0;

    CHECK_DIE(die, DW_DLV_ERROR);

    res = dwarf_attr(die,attrnum,&attr,&lerr);
    dbg = die->di_cu_context->cc_dbg;
    if (res == DW_DLV_ERROR) {
        return DW_DLV_NO_ENTRY;
    }
    if (res == DW_DLV_NO_ENTRY) {
        return res;
    }
    res = dwarf_formstring(attr,ret_name,error);
    dwarf_dealloc(dbg,attr, DW_DLA_ATTR);
    attr = 0;
    return res;
}

int
dwarf_diename(Dwarf_Die die,
    char **ret_name,
    Dwarf_Error * error)
{
    return dwarf_die_text(die,DW_AT_name,ret_name,error);
}

int
dwarf_hasattr(Dwarf_Die die,
    Dwarf_Half attr,
    Dwarf_Bool * return_bool, Dwarf_Error * error)
{
    Dwarf_Half attr_form = 0;
    Dwarf_Byte_Ptr info_ptr = 0;
    int res = 0;

    CHECK_DIE(die, DW_DLV_ERROR);

    res = _dwarf_get_value_ptr(die, attr, &attr_form,&info_ptr,error);
    if(res == DW_DLV_ERROR) {
        return res;
    }
    if(res == DW_DLV_NO_ENTRY) {
        *return_bool = false;
        return DW_DLV_OK;
    }
    *return_bool = (true);
    return DW_DLV_OK;
}

int
dwarf_attr(Dwarf_Die die,
    Dwarf_Half attr,
    Dwarf_Attribute * ret_attr, Dwarf_Error * error)
{
    Dwarf_Half attr_form = 0;
    Dwarf_Attribute attrib = 0;
    Dwarf_Byte_Ptr info_ptr = 0;
    Dwarf_Debug dbg = 0;
    int res = 0;

    CHECK_DIE(die, DW_DLV_ERROR);
    dbg = die->di_cu_context->cc_dbg;

    res = _dwarf_get_value_ptr(die, attr, &attr_form,&info_ptr,error);
    if(res == DW_DLV_ERROR) {
        return res;
    }
    if(res == DW_DLV_NO_ENTRY) {
        return res;
    }


    attrib = (Dwarf_Attribute) _dwarf_get_alloc(dbg, DW_DLA_ATTR, 1);
    if (attrib == NULL) {
        _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
        return (DW_DLV_ERROR);
    }

    attrib->ar_attribute = attr;
    attrib->ar_attribute_form = attr_form;
    attrib->ar_attribute_form_direct = attr_form;
    attrib->ar_cu_context = die->di_cu_context;
    attrib->ar_debug_ptr = info_ptr;
    attrib->ar_die = die;
    *ret_attr = (attrib);
    return DW_DLV_OK;
}

/*  A DWP (.dwp) package object never contains .debug_addr,
    only a normal .o or executable object.
    Error returned here is on dbg, not tieddbg. */
int
_dwarf_extract_address_from_debug_addr(Dwarf_Debug dbg,
    Dwarf_CU_Context context,
    Dwarf_Unsigned index_to_addr,
    Dwarf_Addr *addr_out,
    Dwarf_Error *error)
{
    Dwarf_Unsigned address_base = 0;
    Dwarf_Unsigned addrindex = index_to_addr;
    Dwarf_Unsigned addr_offset = 0;
    Dwarf_Unsigned ret_addr = 0;
    int res = 0;
    Dwarf_Byte_Ptr  sectionstart = 0;
    Dwarf_Byte_Ptr  sectionend = 0;
    Dwarf_Unsigned  sectionsize  = 0;

    res = _dwarf_get_address_base_attr_value(dbg,context,
        &address_base, error);
    if (res != DW_DLV_OK) {
        return res;
    }
    res = _dwarf_load_section(dbg, &dbg->de_debug_addr,error);
    if (res != DW_DLV_OK) {
        /*  Ignore the inner error, report something meaningful */
        if (res == DW_DLV_ERROR) {
            dwarf_dealloc(dbg,*error, DW_DLA_ERROR);
            *error = 0;
        }
        _dwarf_error(dbg,error,
            DW_DLE_MISSING_NEEDED_DEBUG_ADDR_SECTION);
        return DW_DLV_ERROR;
    }
    /*  DW_FORM_addrx has a base value from the CU die:
        DW_AT_addr_base.  DW_OP_addrx and DW_OP_constx
        rely on DW_AT_addr_base too. */
    /*  DW_FORM_GNU_addr_index  relies on DW_AT_GNU_addr_base
        which is in the CU die. */

    sectionstart = dbg->de_debug_addr.dss_data;
    addr_offset = address_base + (addrindex * context->cc_address_size);
    /*  The offsets table is a series of address-size entries
        but with a base. */
    sectionsize = dbg->de_debug_addr.dss_size;
    sectionend = sectionstart + sectionsize;
    if (addr_offset > (sectionsize - context->cc_address_size)) {
        _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_SIZE_BAD);
        return (DW_DLV_ERROR);
    }
    READ_UNALIGNED_CK(dbg,ret_addr,Dwarf_Addr,
        sectionstart + addr_offset,
        context->cc_address_size,
        error,sectionend);
    *addr_out = ret_addr;
    return DW_DLV_OK;
}

static int
_dwarf_look_in_local_and_tied_by_index(
    Dwarf_Debug dbg,
    Dwarf_CU_Context context,
    Dwarf_Unsigned index,
    Dwarf_Addr *return_addr,
    Dwarf_Error *error)
{
    int res2 = 0;

    res2 = _dwarf_extract_address_from_debug_addr(dbg,
        context, index, return_addr, error);
    if (res2 != DW_DLV_OK) {
        if (res2 == DW_DLV_ERROR &&
            error &&
            dwarf_errno(*error) == DW_DLE_MISSING_NEEDED_DEBUG_ADDR_SECTION
            && dbg->de_tied_data.td_tied_object) {
            int res3 = 0;

            /*  We do not want to leak error structs... */
            dwarf_dealloc(dbg,*error,DW_DLA_ERROR);

            *error = 0;
            /* error is returned on dbg, not tieddbg. */
            res3 = _dwarf_get_addr_from_tied(dbg,
                context,index,return_addr,error);
            if ( res3 == DW_DLV_ERROR) {
                return res3;
            } else if ( res3 == DW_DLV_NO_ENTRY) {
                return res3;
            }
        } else {
            return res2;
        }
    }
    return (DW_DLV_OK);
}

/*  The DIE here can be any DIE in the relevant CU.
    index is an index into .debug_addr */
int
dwarf_debug_addr_index_to_addr(Dwarf_Die die,
    Dwarf_Unsigned index,
    Dwarf_Addr *return_addr,
    Dwarf_Error *error)
{
    Dwarf_Debug dbg = 0;
    Dwarf_CU_Context context = 0;
    int res = 0;


    CHECK_DIE(die, DW_DLV_ERROR);
    context = die->di_cu_context;
    dbg = context->cc_dbg;

    /* error is returned on dbg, not tieddbg. */
    res = _dwarf_look_in_local_and_tied_by_index(dbg,
        context,
        index,
        return_addr,
        error);
    return res;
}
/* ASSERT:
    attr_form == DW_FORM_GNU_addr_index ||
        attr_form == DW_FORM_addrx
*/
int
_dwarf_look_in_local_and_tied(Dwarf_Half attr_form,
    Dwarf_CU_Context context,
    Dwarf_Small *info_ptr,
    Dwarf_Addr *return_addr,
    Dwarf_Error *error)
{
    int res2 = 0;
    Dwarf_Unsigned index_to_addr = 0;
    Dwarf_Debug dbg = 0;

    /*  We get the index. It might apply here
        or in tied object. Checking that next. */
    dbg = context->cc_dbg;
    res2 = _dwarf_get_addr_index_itself(attr_form,
        info_ptr,dbg,context, &index_to_addr,error);
    if(res2 != DW_DLV_OK) {
        return res2;
    }
    /* error is returned on dbg, not tieddbg. */
    res2 = _dwarf_look_in_local_and_tied_by_index(
        dbg,context,index_to_addr,return_addr,error);
    return res2;

}

int
dwarf_lowpc(Dwarf_Die die,
    Dwarf_Addr * return_addr,
    Dwarf_Error * error)
{
    Dwarf_Addr ret_addr = 0;
    Dwarf_Byte_Ptr info_ptr = 0;
    Dwarf_Half attr_form = 0;
    Dwarf_Debug dbg = 0;
    Dwarf_Half address_size = 0;
    Dwarf_Half offset_size = 0;
    int version = 0;
    enum Dwarf_Form_Class class = DW_FORM_CLASS_UNKNOWN;
    int res = 0;
    Dwarf_CU_Context context = die->di_cu_context;
    Dwarf_Small *die_info_end = 0;

    CHECK_DIE(die, DW_DLV_ERROR);

    dbg = context->cc_dbg;
    address_size = context->cc_address_size;
    offset_size = context->cc_length_size;
    res = _dwarf_get_value_ptr(die, DW_AT_low_pc, &attr_form,&info_ptr,error);
    if(res == DW_DLV_ERROR) {
        return res;
    }
    if(res == DW_DLV_NO_ENTRY) {
        return res;
    }
    version = context->cc_version_stamp;
    class = dwarf_get_form_class(version,DW_AT_low_pc,
        offset_size,attr_form);
    if (class != DW_FORM_CLASS_ADDRESS) {
        /* Not the correct form for DW_AT_low_pc */
        _dwarf_error(dbg, error, DW_DLE_LOWPC_WRONG_CLASS);
        return (DW_DLV_ERROR);
    }

    if(attr_form == DW_FORM_GNU_addr_index ||
        attr_form == DW_FORM_addrx) {
        /* error is returned on dbg, not tieddbg. */
        res = _dwarf_look_in_local_and_tied(
            attr_form,
            context,
            info_ptr,
            return_addr,
            error);
        return res;
    }
    die_info_end = _dwarf_calculate_info_section_end_ptr(context);
    READ_UNALIGNED_CK(dbg, ret_addr, Dwarf_Addr,
        info_ptr, address_size,
        error,die_info_end);

    *return_addr = ret_addr;
    return (DW_DLV_OK);
}


/*  This works for DWARF2 and DWARF3 but fails for DWARF4
    DW_AT_high_pc attributes of class constant.
    It is best to cease using this interface.
    */
int
dwarf_highpc(Dwarf_Die die,
    Dwarf_Addr * return_addr, Dwarf_Error * error)
{
    int res = 0;
    enum Dwarf_Form_Class class = DW_FORM_CLASS_UNKNOWN;
    Dwarf_Half form = 0;

    CHECK_DIE(die, DW_DLV_ERROR);
    res = dwarf_highpc_b(die,return_addr,&form,&class,error);
    if (res != DW_DLV_OK) {
        return res;
    }
    if (form != DW_FORM_addr) {
        /* Not the correct form for DWARF2/3 DW_AT_high_pc */
        Dwarf_Debug dbg = die->di_cu_context->cc_dbg;
        _dwarf_error(dbg, error, DW_DLE_HIGHPC_WRONG_FORM);
        return (DW_DLV_ERROR);
    }
    return (DW_DLV_OK);
}

/*  If the giving 'die' contains the DW_AT_type attribute, it returns
    the offset referenced by the attribute.
    In case of DW_DLV_NO_ENTRY or DW_DLV_ERROR it sets offset zero. */
int
dwarf_dietype_offset(Dwarf_Die die,
    Dwarf_Off *return_off, Dwarf_Error *error)
{
    int res = 0;
    Dwarf_Off offset = 0;
    Dwarf_Attribute attr = 0;

    CHECK_DIE(die, DW_DLV_ERROR);
    res = dwarf_attr(die,DW_AT_type,&attr,error);
    if (res == DW_DLV_OK) {
        res = dwarf_global_formref(attr,&offset,error);
        dwarf_dealloc(die->di_cu_context->cc_dbg,attr,DW_DLA_ATTR);
    }
    *return_off = offset;
    return res;
}



int
_dwarf_get_string_base_attr_value(Dwarf_Debug dbg,
    Dwarf_CU_Context context,
    Dwarf_Unsigned *sbase_out,
    Dwarf_Error *error)
{
    int res = 0;
    Dwarf_Die cudie = 0;
    Dwarf_Unsigned cu_die_offset = 0;
    Dwarf_Attribute myattr = 0;

    if(context->cc_str_offsets_base_present) {
        *sbase_out = context->cc_str_offsets_base;
        return DW_DLV_OK;
    }
    cu_die_offset = context->cc_cu_die_global_sec_offset;
    context->cc_cu_die_offset_present  = TRUE;
    res = dwarf_offdie_b(dbg,cu_die_offset,
        context->cc_is_info,
        &cudie,
        error);
    if(res != DW_DLV_OK) {
        return res;
    }
    res = dwarf_attr(cudie,DW_AT_str_offsets_base,
        &myattr,error);
    if(res == DW_DLV_ERROR) {
        dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
        return res;
    }
    if (res == DW_DLV_OK) {
        Dwarf_Unsigned val = 0;
        /* Expect DW_FORM_sec_offset */
        if (myattr->ar_attribute_form != DW_FORM_sec_offset) {
            dwarf_dealloc(dbg,myattr,DW_DLA_ATTR);
            dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
            _dwarf_error(dbg, error,DW_DLE_STR_OFFSETS_BASE_WRONG_FORM);
            return (DW_DLV_ERROR);
        }
        res = dwarf_global_formref(myattr,&val,error);
        dwarf_dealloc(dbg,myattr,DW_DLA_ATTR);
        dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
        if(res != DW_DLV_OK) {
            return res;
        }
        *sbase_out  = val;
        context->cc_str_offsets_base = val;
        context->cc_str_offsets_base_present = TRUE;
        return DW_DLV_OK;
    }
    /*  NO ENTRY, No other attr.Not even GNU, this one is standard
        DWARF5 only.  */
    dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
    /*  We do not need a base for a .dwo. We might for .dwp
        and would or .o or executable.
        FIXME: assume we do not need this.
        Should we really return DW_DLV_NO_ENTRY?
    */
    *sbase_out = 0;
    return DW_DLV_OK;
}
/*  Goes to the CU die and finds the DW_AT_GNU_addr_base
    (or DW_AT_addr_base ) and gets the value from that CU die
    and returns it thou abase_out. If we cannot find the value
    it is a serious error in the DWARF.
    */
static int
_dwarf_get_address_base_attr_value(Dwarf_Debug dbg,
    Dwarf_CU_Context context,
    Dwarf_Unsigned *abase_out,
    Dwarf_Error *error)
{
    int res = 0;
    Dwarf_Die cudie = 0;
    Dwarf_Bool cu_die_offset_present = 0;
    Dwarf_Unsigned cu_die_offset = 0;
    Dwarf_Attribute myattr = 0;
    if(context->cc_addr_base_present) {
        *abase_out = context->cc_addr_base;
        return DW_DLV_OK;
    }

    cu_die_offset = context->cc_cu_die_global_sec_offset;
    cu_die_offset_present = context->cc_cu_die_offset_present;
    if(!cu_die_offset_present) {
        _dwarf_error(dbg, error,
            DW_DLE_DEBUG_CU_UNAVAILABLE_FOR_FORM);
        return (DW_DLV_ERROR);

    }
    res = dwarf_offdie_b(dbg,cu_die_offset,
        context->cc_is_info,
        &cudie,
        error);
    if(res != DW_DLV_OK) {
        return res;
    }
    res = dwarf_attr(cudie,DW_AT_addr_base,
        &myattr,error);
    if(res == DW_DLV_ERROR) {
        dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
        return res;
    }
    if (res == DW_DLV_OK) {
        Dwarf_Unsigned val = 0;
        res = dwarf_formudata(myattr,&val,error);
        dwarf_dealloc(dbg,myattr,DW_DLA_ATTR);
        dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
        if(res != DW_DLV_OK) {
            return res;
        }
        *abase_out  = val;
        return DW_DLV_OK;
    }
    /* NO ENTRY, try the other attr. */
    res = dwarf_attr(cudie,DW_AT_GNU_addr_base, &myattr,error);
    if(res == DW_DLV_NO_ENTRY) {
        res = dwarf_attr(cudie,DW_AT_addr_base, &myattr,error);
        if (res == DW_DLV_NO_ENTRY) {
            /*  A .o or .dwp needs a base, but a .dwo does not.
                FIXME: check this claim...
                Assume zero is ok and works. */
            *abase_out = 0;
            dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
            return DW_DLV_OK;
        }
        if (res == DW_DLV_ERROR) {
            dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
            return res;
        }
    } else if (res == DW_DLV_ERROR) {
        dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
        return res;
    }

    {
        Dwarf_Unsigned val = 0;
        res = dwarf_formudata(myattr,&val,error);
        dwarf_dealloc(dbg,myattr,DW_DLA_ATTR);
        dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
        if(res != DW_DLV_OK) {
            return res;
        }
        *abase_out  = val;
    }
    return DW_DLV_OK;
}


/* The dbg here will be the tieddbg, and context will be
   a tied context.  */
static int
_dwarf_get_ranges_base_attr_value(Dwarf_Debug dbg,
    Dwarf_CU_Context context,
    Dwarf_Unsigned * rangesbase_out,
    Dwarf_Error    * error)
{
    int res = 0;
    Dwarf_Die cudie = 0;
    Dwarf_Bool cu_die_offset_present = 0;
    Dwarf_Unsigned cu_die_offset = 0;
    Dwarf_Attribute myattr = 0;
    if(context->cc_ranges_base_present) {
        *rangesbase_out = context->cc_ranges_base;
        return DW_DLV_OK;
    }

    cu_die_offset = context->cc_cu_die_global_sec_offset;
    cu_die_offset_present = context->cc_cu_die_offset_present;
    if(!cu_die_offset_present) {
        _dwarf_error(dbg, error,
            DW_DLE_DEBUG_CU_UNAVAILABLE_FOR_FORM);
        return (DW_DLV_ERROR);

    }
    res = dwarf_offdie_b(dbg,cu_die_offset,
        context->cc_is_info,
        &cudie,
        error);
    if(res != DW_DLV_OK) {
        return res;
    }
    res = dwarf_attr(cudie,DW_AT_rnglists_base,
        &myattr,error);
    if(res == DW_DLV_ERROR) {
        dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
        return res;
    }
    if (res == DW_DLV_OK) {
        Dwarf_Unsigned val = 0;
        res = dwarf_formudata(myattr,&val,error);
        dwarf_dealloc(dbg,myattr,DW_DLA_ATTR);
        dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
        if(res != DW_DLV_OK) {
            return res;
        }
        *rangesbase_out  = val;
        return DW_DLV_OK;
    }
    /* NO ENTRY, try the other attr. */
    res = dwarf_attr(cudie,DW_AT_GNU_ranges_base, &myattr,error);
    if(res == DW_DLV_NO_ENTRY) {
        res = dwarf_attr(cudie,DW_AT_rnglists_base, &myattr,error);
        if (res == DW_DLV_NO_ENTRY) {
            /*  A .o or execeutable skeleton  needs
                a base , but a .dwo does not.
                Assume zero is ok and works. */
            *rangesbase_out = 0;
            dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
            return DW_DLV_OK;
        }
        if (res == DW_DLV_ERROR) {
            dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
            return res;
        }
    } else if (res == DW_DLV_ERROR) {
        dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
        return res;
    }

    {
        Dwarf_Unsigned val = 0;
        res = dwarf_formudata(myattr,&val,error);
        dwarf_dealloc(dbg,myattr,DW_DLA_ATTR);
        dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
        if(res != DW_DLV_OK) {
            return res;
        }
        *rangesbase_out  = val;
    }
    return DW_DLV_OK;
}
/*  This works for  all versions of DWARF.
    This is the preferred interface, cease using dwarf_highpc.
    The consumer has to check the return_form or
    return_class to decide if the value returned
    through return_value is an address or an address-offset.

    See  DWARF4 section 2.17.2,
    "Contiguous Address Range".
    */
int
dwarf_highpc_b(Dwarf_Die die,
    Dwarf_Addr * return_value,
    Dwarf_Half * return_form,
    enum Dwarf_Form_Class * return_class,
    Dwarf_Error * error)
{
    Dwarf_Byte_Ptr info_ptr = 0;
    Dwarf_Half attr_form = 0;
    Dwarf_Debug dbg = 0;
    Dwarf_Half address_size = 0;
    Dwarf_Half offset_size = 0;
    enum Dwarf_Form_Class class = DW_FORM_CLASS_UNKNOWN;
    Dwarf_Half version = 0;
    Dwarf_Byte_Ptr die_info_end = 0;
    int res = 0;

    CHECK_DIE(die, DW_DLV_ERROR);
    dbg = die->di_cu_context->cc_dbg;
    address_size = die->di_cu_context->cc_address_size;

    res = _dwarf_get_value_ptr(die, DW_AT_high_pc, &attr_form,&info_ptr,error);
    if(res == DW_DLV_ERROR) {
        return res;
    }
    if(res == DW_DLV_NO_ENTRY) {
        return res;
    }
    die_info_end = _dwarf_calculate_info_section_end_ptr(die->di_cu_context);

    version = die->di_cu_context->cc_version_stamp;
    offset_size = die->di_cu_context->cc_length_size;
    class = dwarf_get_form_class(version,DW_AT_high_pc,
        offset_size,attr_form);

    if (class == DW_FORM_CLASS_ADDRESS) {
        Dwarf_Addr addr = 0;
        if (attr_form == DW_FORM_GNU_addr_index ||
            attr_form == DW_FORM_addrx) {
            Dwarf_Unsigned addr_out = 0;
            Dwarf_Unsigned index_to_addr = 0;
            int res2 = 0;
            Dwarf_CU_Context context = die->di_cu_context;

            /*  index_to_addr we get here might apply
                to this dbg or to tieddbg. */
            /* error is returned on dbg, not tied */
            res2 = _dwarf_get_addr_index_itself(attr_form,
                info_ptr,dbg,context,&index_to_addr,error);
            if(res2 != DW_DLV_OK) {
                return res2;
            }

            res2 = _dwarf_extract_address_from_debug_addr(dbg,
                context,
                index_to_addr,
                &addr_out,
                error);
            if(res2 != DW_DLV_OK) {
                if (res2 == DW_DLV_ERROR &&
                    error &&
                    dwarf_errno(*error) ==
                    DW_DLE_MISSING_NEEDED_DEBUG_ADDR_SECTION
                    && dbg->de_tied_data.td_tied_object) {
                    /*  .debug_addr is in tied dbg. */
                    int res3 = 0;

                    /*  Do not leak the above error pointer,
                        we have something else to try here. */
                    dwarf_dealloc(dbg,*error, DW_DLA_ERROR);
                    *error = 0;

                    /*  .debug_addr is in tied dbg.
                        Get the index of the addr */
                    res3 = _dwarf_get_addr_from_tied(dbg,
                        context,index_to_addr,&addr_out,error);
                    if ( res3 != DW_DLV_OK) {
                        return res3;
                    }
                } else {
                    return res2;
                }
            }
            *return_value = addr_out;
            *return_form = attr_form;
            *return_class = class;
            return (DW_DLV_OK);
        }

        READ_UNALIGNED_CK(dbg, addr, Dwarf_Addr,
            info_ptr, address_size,
            error,die_info_end);
        *return_value = addr;
    } else {
        int res3 = 0;
        Dwarf_Unsigned v = 0;
        res3 = _dwarf_die_attr_unsigned_constant(die,DW_AT_high_pc,
            &v,error);
        if(res3 != DW_DLV_OK) {
            Dwarf_Byte_Ptr info_ptr2 = 0;
            res3 = _dwarf_get_value_ptr(die, DW_AT_high_pc,
                &attr_form,&info_ptr2,error);
            if(res3 == DW_DLV_ERROR) {
                return res3;
            }
            if(res3 == DW_DLV_NO_ENTRY) {
                return res3;
            }
            if (attr_form == DW_FORM_sdata) {
                Dwarf_Signed sval = 0;

                /*  DWARF4 defines the value as an unsigned offset
                    in section 2.17.2. */
                DECODE_LEB128_UWORD_CK(info_ptr2, sval,
                    dbg,error,die_info_end);
                *return_value = (Dwarf_Unsigned)sval;
            } else {
                _dwarf_error(dbg, error, DW_DLE_HIGHPC_WRONG_FORM);
                return DW_DLV_ERROR;
            }
        } else {
            *return_value = v;
        }
    }
    *return_form = attr_form;
    *return_class = class;
    return DW_DLV_OK;
}

/* The dbg and context here are a file with DW_FORM_addrx
    but missing .debug_addr. So go to the tied file
    and using the signature from the current context
    locate the target CU in the tied file Then
    get the address.

*/
int
_dwarf_get_addr_from_tied(Dwarf_Debug dbg,
    Dwarf_CU_Context context,
    Dwarf_Unsigned index,
    Dwarf_Addr *addr_out,
    Dwarf_Error*error)
{
    Dwarf_Debug tieddbg = 0;
    int res = 0;
    Dwarf_Addr local_addr = 0;
    Dwarf_CU_Context tiedcontext = 0;

    if (!context->cc_signature_present) {
        _dwarf_error(dbg, error, DW_DLE_NO_SIGNATURE_TO_LOOKUP);
        return  DW_DLV_ERROR;
    }
    tieddbg = dbg->de_tied_data.td_tied_object;
    if (!tieddbg) {
        _dwarf_error(dbg, error, DW_DLE_NO_TIED_ADDR_AVAILABLE);
        return  DW_DLV_ERROR;
    }
    if (!context->cc_signature_present) {
        _dwarf_error(dbg, error, DW_DLE_NO_TIED_SIG_AVAILABLE);
        return  DW_DLV_ERROR;
    }
    res = _dwarf_search_for_signature(tieddbg,
        context->cc_type_signature,
        &tiedcontext,
        error);
    if ( res == DW_DLV_ERROR) {
        /* Associate the error with dbg, not tieddbg */
        _dwarf_error_mv_s_to_t(tieddbg,error,dbg,error);
        return res;
    } else if ( res == DW_DLV_NO_ENTRY) {
        return res;
    }

    res = _dwarf_extract_address_from_debug_addr(tieddbg,
        tiedcontext,
        index,
        &local_addr,
        error);
    if ( res == DW_DLV_ERROR) {
        /* Associate the error with dbg, not tidedbg */
        _dwarf_error_mv_s_to_t(tieddbg,error,dbg,error);
        return res;
    } else if ( res == DW_DLV_NO_ENTRY) {
        return res;
    }
    *addr_out = local_addr;
    return DW_DLV_OK;
}

int
_dwarf_get_ranges_base_attr_from_tied(Dwarf_Debug dbg,
    Dwarf_CU_Context context,
    Dwarf_Unsigned *tiedbase_out,
    Dwarf_Error*error)
{
    Dwarf_Debug tieddbg = 0;
    int res = 0;
    Dwarf_Unsigned tiedbase= 0;
    Dwarf_CU_Context tiedcontext = 0;

    if (!context->cc_signature_present) {
        _dwarf_error(dbg, error, DW_DLE_NO_SIGNATURE_TO_LOOKUP);
        return  DW_DLV_ERROR;
    }
    tieddbg = dbg->de_tied_data.td_tied_object;
    if (!tieddbg) {
        _dwarf_error(dbg, error, DW_DLE_NO_TIED_ADDR_AVAILABLE);
        return  DW_DLV_ERROR;
    }
    if (!context->cc_signature_present) {
        _dwarf_error(dbg, error, DW_DLE_NO_TIED_SIG_AVAILABLE);
        return  DW_DLV_ERROR;
    }
    res = _dwarf_search_for_signature(tieddbg,
        context->cc_type_signature,
        &tiedcontext,
        error);
    if ( res == DW_DLV_ERROR) {
        /* Associate the error with dbg, not tidedbg */
        _dwarf_error_mv_s_to_t(tieddbg,error,dbg,error);
        return res;
    } else if ( res != DW_DLV_NO_ENTRY) {
        return res;
    }
    res = _dwarf_get_ranges_base_attr_value(tieddbg, tiedcontext,
        &tiedbase, error);
    if (res != DW_DLV_OK) {
        /* Associate the error with dbg, not tidedbg */
        _dwarf_error_mv_s_to_t(tieddbg,error,dbg,error);
        return res;
    }
    *tiedbase_out = tiedbase;
    return DW_DLV_OK;
}


/*
    Takes a die, an attribute attr, and checks if attr
    occurs in die.  Attr is required to be an attribute
    whose form is in the "constant" class.  If attr occurs
    in die, the value is returned.

    Returns DW_DLV_OK, DW_DLV_ERROR, or DW_DLV_NO_ENTRY as
    appropriate. Sets the value thru the pointer return_val.

    This function is meant to do all the
    processing for dwarf_bytesize, dwarf_bitsize, dwarf_bitoffset,
    and dwarf_srclang. And it helps in dwarf_highpc_with_form().
*/
static int
_dwarf_die_attr_unsigned_constant(Dwarf_Die die,
    Dwarf_Half attr,
    Dwarf_Unsigned * return_val,
    Dwarf_Error * error)
{
    Dwarf_Byte_Ptr info_ptr = 0;
    Dwarf_Half attr_form = 0;
    Dwarf_Unsigned ret_value = 0;
    Dwarf_Debug dbg = 0;
    int res = 0;
    Dwarf_Byte_Ptr die_info_end = 0;

    CHECK_DIE(die, DW_DLV_ERROR);

    die_info_end = _dwarf_calculate_info_section_end_ptr(die->di_cu_context);
    dbg = die->di_cu_context->cc_dbg;
    res = _dwarf_get_value_ptr(die,attr,&attr_form,
        &info_ptr,error);
    if(res != DW_DLV_OK) {
        return res;
    }
    switch (attr_form) {
    case DW_FORM_data1:
        READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
            info_ptr, sizeof(Dwarf_Small),
            error,die_info_end);
        *return_val = ret_value;
        return (DW_DLV_OK);

    case DW_FORM_data2:
        READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
            info_ptr, sizeof(Dwarf_Shalf),
            error,die_info_end);
        *return_val = ret_value;
        return (DW_DLV_OK);

    case DW_FORM_data4:
        READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
            info_ptr, DWARF_32BIT_SIZE,
            error,die_info_end);
        *return_val = ret_value;
        return (DW_DLV_OK);

    case DW_FORM_data8:
        READ_UNALIGNED_CK(dbg, ret_value, Dwarf_Unsigned,
            info_ptr, DWARF_64BIT_SIZE,
            error,die_info_end);
        *return_val = ret_value;
        return (DW_DLV_OK);

    case DW_FORM_udata: {
        Dwarf_Unsigned v = 0;

        DECODE_LEB128_UWORD_CK(info_ptr, v,dbg,error,die_info_end);
        *return_val = v;
        return DW_DLV_OK;


    }

    default:
        _dwarf_error(dbg, error, DW_DLE_DIE_BAD);
        return (DW_DLV_ERROR);
    }
}


int
dwarf_bytesize(Dwarf_Die die,
    Dwarf_Unsigned * ret_size, Dwarf_Error * error)
{
    Dwarf_Unsigned luns = 0;
    int res = _dwarf_die_attr_unsigned_constant(die, DW_AT_byte_size,
        &luns, error);
    *ret_size = luns;
    return res;
}


int
dwarf_bitsize(Dwarf_Die die,
    Dwarf_Unsigned * ret_size, Dwarf_Error * error)
{
    Dwarf_Unsigned luns = 0;
    int res = _dwarf_die_attr_unsigned_constant(die, DW_AT_bit_size,
        &luns, error);
    *ret_size = luns;
    return res;
}


int
dwarf_bitoffset(Dwarf_Die die,
    Dwarf_Unsigned * ret_size, Dwarf_Error * error)
{
    Dwarf_Unsigned luns = 0;
    int res = _dwarf_die_attr_unsigned_constant(die,
        DW_AT_bit_offset, &luns, error);
    *ret_size = luns;
    return res;
}


/* Refer section 3.1, page 21 in Dwarf Definition. */
int
dwarf_srclang(Dwarf_Die die,
    Dwarf_Unsigned * ret_size, Dwarf_Error * error)
{
    Dwarf_Unsigned luns = 0;
    int res = _dwarf_die_attr_unsigned_constant(die, DW_AT_language,
        &luns, error);
    *ret_size = luns;
    return res;
}


/* Refer section 5.4, page 37 in Dwarf Definition. */
int
dwarf_arrayorder(Dwarf_Die die,
    Dwarf_Unsigned * ret_size, Dwarf_Error * error)
{
    Dwarf_Unsigned luns = 0;
    int res = _dwarf_die_attr_unsigned_constant(die, DW_AT_ordering,
        &luns, error);
    *ret_size = luns;
    return res;
}

/*  Return DW_DLV_OK if ok
    DW_DLV_ERROR if failure.

    If the die and the attr are not related the result is
    meaningless.  */
int
dwarf_attr_offset(Dwarf_Die die, Dwarf_Attribute attr,
    Dwarf_Off * offset /* return offset thru this ptr */,
    Dwarf_Error * error)
{
    Dwarf_Off attroff = 0;
    Dwarf_Small *dataptr = 0;
    Dwarf_Debug dbg = 0;

    CHECK_DIE(die, DW_DLV_ERROR);
    dbg = die->di_cu_context->cc_dbg;
    dataptr = die->di_is_info? dbg->de_debug_info.dss_data:
        dbg->de_debug_types.dss_data;

    attroff = (attr->ar_debug_ptr - dataptr);
    *offset = attroff;
    return DW_DLV_OK;
}

int
dwarf_die_abbrev_code(Dwarf_Die die)
{
    return die->di_abbrev_code;
}

/*  Returns a flag through ablhas_child. Non-zero if
    the DIE has children, zero if it does not.   */
int
dwarf_die_abbrev_children_flag(Dwarf_Die die,Dwarf_Half *ab_has_child)
{
    if (die->di_abbrev_list) {
        *ab_has_child = die->di_abbrev_list->abl_has_child;
        return DW_DLV_OK;
    }
    return DW_DLV_ERROR;
}

/* Helper function for finding form class. */
static enum Dwarf_Form_Class
dw_get_special_offset(Dwarf_Half attrnum,
    Dwarf_Half dwversion)
{
    switch (attrnum) {
    case DW_AT_stmt_list:
        return DW_FORM_CLASS_LINEPTR;
    case DW_AT_macro_info: /* DWARF2-DWARF4 */
        return DW_FORM_CLASS_MACPTR;
    case DW_AT_start_scope:
    case DW_AT_ranges: {
        if (dwversion <= 4) {
            return DW_FORM_CLASS_RANGELISTPTR;
        }
        return DW_FORM_CLASS_RNGLIST;
        }
    case DW_AT_rnglists_base: /* DWARF5 */
        return DW_FORM_CLASS_RNGLISTSPTR;
    case DW_AT_macros:        /* DWARF5 */
        return DW_FORM_CLASS_MACROPTR;
    case DW_AT_loclists_base: /* DWARF5 */
        return DW_FORM_CLASS_LOCLISTSPTR;
    case DW_AT_addr_base:     /* DWARF5 */
        return DW_FORM_CLASS_ADDRPTR;
    case DW_AT_str_offsets_base: /* DWARF5 */
        return DW_FORM_CLASS_STROFFSETSPTR;

    case DW_AT_location:
    case DW_AT_string_length:
    case DW_AT_return_addr:
    case DW_AT_data_member_location:
    case DW_AT_frame_base:
    case DW_AT_segment:
    case DW_AT_static_link:
    case DW_AT_use_location:
    case DW_AT_vtable_elem_location: {
        if (dwversion <= 4) {
            return DW_FORM_CLASS_LOCLISTPTR;
        }
        return DW_FORM_CLASS_LOCLIST;
        }
    case DW_AT_sibling:
    case DW_AT_byte_size :
    case DW_AT_bit_offset :
    case DW_AT_bit_size :
    case DW_AT_discr :
    case DW_AT_import :
    case DW_AT_common_reference:
    case DW_AT_containing_type:
    case DW_AT_default_value:
    case DW_AT_lower_bound:
    case DW_AT_bit_stride:
    case DW_AT_upper_bound:
    case DW_AT_abstract_origin:
    case DW_AT_base_types:
    case DW_AT_count:
    case DW_AT_friend:
    case DW_AT_namelist_item:
    case DW_AT_priority:
    case DW_AT_specification:
    case DW_AT_type:
    case DW_AT_allocated:
    case DW_AT_associated:
    case DW_AT_byte_stride:
    case DW_AT_extension:
    case DW_AT_trampoline:
    case DW_AT_small:
    case DW_AT_object_pointer:
    case DW_AT_signature:
        return DW_FORM_CLASS_REFERENCE;
    case DW_AT_MIPS_fde: /* SGI/IRIX extension */
        return DW_FORM_CLASS_FRAMEPTR;
    }
    return DW_FORM_CLASS_UNKNOWN;
}

/* It takes 4 pieces of data (including the FORM)
   to accurately determine the form 'class' as documented
   in the DWARF spec. This is per DWARF4, but will work
   for DWARF2 or 3 as well.  */
enum Dwarf_Form_Class
dwarf_get_form_class(
    Dwarf_Half dwversion,
    Dwarf_Half attrnum,
    Dwarf_Half offset_size,
    Dwarf_Half form)
{
    switch (form) {
    case  DW_FORM_addr:  return DW_FORM_CLASS_ADDRESS;
    case  DW_FORM_data2:  return DW_FORM_CLASS_CONSTANT;

    case  DW_FORM_data4:
        if (dwversion <= 3 && offset_size == 4) {
            enum Dwarf_Form_Class class = dw_get_special_offset(attrnum,
                dwversion);
            if (class != DW_FORM_CLASS_UNKNOWN) {
                return class;
            }
        }
        return DW_FORM_CLASS_CONSTANT;
    case  DW_FORM_data8:
        if (dwversion <= 3 && offset_size == 8) {
            enum Dwarf_Form_Class class = dw_get_special_offset(attrnum,
                dwversion);
            if (class != DW_FORM_CLASS_UNKNOWN) {
                return class;
            }
        }
        return DW_FORM_CLASS_CONSTANT;
    case  DW_FORM_sec_offset:
        {
            enum Dwarf_Form_Class class = dw_get_special_offset(attrnum,
                dwversion);
            if (class != DW_FORM_CLASS_UNKNOWN) {
                return class;
            }
        }
        /* We do not know what this is. */
        break;

    case  DW_FORM_string: return DW_FORM_CLASS_STRING;
    case  DW_FORM_strp:   return DW_FORM_CLASS_STRING;

    case  DW_FORM_block:  return DW_FORM_CLASS_BLOCK;
    case  DW_FORM_block1: return DW_FORM_CLASS_BLOCK;
    case  DW_FORM_block2: return DW_FORM_CLASS_BLOCK;
    case  DW_FORM_block4: return DW_FORM_CLASS_BLOCK;

    case  DW_FORM_data16:  return DW_FORM_CLASS_CONSTANT;
    case  DW_FORM_data1:  return DW_FORM_CLASS_CONSTANT;
    case  DW_FORM_sdata:  return DW_FORM_CLASS_CONSTANT;
    case  DW_FORM_udata:  return DW_FORM_CLASS_CONSTANT;

    case  DW_FORM_ref_addr:    return DW_FORM_CLASS_REFERENCE;
    case  DW_FORM_ref1:        return DW_FORM_CLASS_REFERENCE;
    case  DW_FORM_ref2:        return DW_FORM_CLASS_REFERENCE;
    case  DW_FORM_ref4:        return DW_FORM_CLASS_REFERENCE;
    case  DW_FORM_ref8:        return DW_FORM_CLASS_REFERENCE;
    case  DW_FORM_ref_udata:   return DW_FORM_CLASS_REFERENCE;
    case  DW_FORM_ref_sig8:    return DW_FORM_CLASS_REFERENCE;

    case  DW_FORM_exprloc:      return DW_FORM_CLASS_EXPRLOC;

    case  DW_FORM_flag:         return DW_FORM_CLASS_FLAG;
    case  DW_FORM_flag_present: return DW_FORM_CLASS_FLAG;

    case  DW_FORM_addrx:           return DW_FORM_CLASS_ADDRESS; /* DWARF5 */
    case  DW_FORM_GNU_addr_index:  return DW_FORM_CLASS_ADDRESS;
    case  DW_FORM_strx:            return DW_FORM_CLASS_STRING;  /* DWARF5 */
    case  DW_FORM_GNU_str_index:   return DW_FORM_CLASS_STRING;

    case  DW_FORM_rnglistx:     return DW_FORM_CLASS_RNGLIST;    /* DWARF5 */
    case  DW_FORM_loclistx:     return DW_FORM_CLASS_LOCLIST;    /* DWARF5 */

    case  DW_FORM_GNU_ref_alt:  return DW_FORM_CLASS_REFERENCE;
    case  DW_FORM_GNU_strp_alt: return DW_FORM_CLASS_STRING;
    case  DW_FORM_strp_sup:     return DW_FORM_CLASS_STRING;    /* DWARF5 */
    case  DW_FORM_implicit_const: return DW_FORM_CLASS_CONSTANT; /* DWARF5 */

    case  DW_FORM_indirect:
    default:
        break;
    };
    return DW_FORM_CLASS_UNKNOWN;
}

/*  Given a DIE, figure out what the CU's DWARF version is
    and the size of an offset
    and return it through the *version pointer and return
    DW_DLV_OK.

    If we cannot find a CU,
        return DW_DLV_ERROR on error.
        In case of error no Dwarf_Debug was available,
        so setting a Dwarf_Error is somewhat futile.
    Never returns DW_DLV_NO_ENTRY.
*/
int
dwarf_get_version_of_die(Dwarf_Die die,
    Dwarf_Half *version,
    Dwarf_Half *offset_size)
{
    Dwarf_CU_Context cucontext = 0;
    if (!die) {
        return DW_DLV_ERROR;
    }
    cucontext = die->di_cu_context;
    if (!cucontext) {
        return DW_DLV_ERROR;
    }
    *version = cucontext->cc_version_stamp;
    *offset_size = cucontext->cc_length_size;
    return DW_DLV_OK;
}

Dwarf_Byte_Ptr
_dwarf_calculate_info_section_start_ptr(Dwarf_CU_Context context,
    Dwarf_Unsigned *section_len)
{
    Dwarf_Debug dbg = 0;
    Dwarf_Small *dataptr = 0;
    struct Dwarf_Section_s *sec = 0;

    dbg = context->cc_dbg;
    sec = context->cc_is_info? &dbg->de_debug_info: &dbg->de_debug_types;
    dataptr = sec->dss_data;
    *section_len = sec->dss_size;
    return dataptr;
}

Dwarf_Byte_Ptr
_dwarf_calculate_info_section_end_ptr(Dwarf_CU_Context context)
{
    Dwarf_Debug dbg = 0;
    Dwarf_Byte_Ptr info_end = 0;
    Dwarf_Byte_Ptr info_start = 0;
    Dwarf_Off off2 = 0;
    Dwarf_Small *dataptr = 0;

    dbg = context->cc_dbg;
    dataptr = context->cc_is_info? dbg->de_debug_info.dss_data:
        dbg->de_debug_types.dss_data;
    off2 = context->cc_debug_offset;
    info_start = dataptr + off2;
    info_end = info_start + context->cc_length +
        context->cc_length_size +
        context->cc_extension_size;
    return info_end;
}
Dwarf_Byte_Ptr
_dwarf_calculate_abbrev_section_end_ptr(Dwarf_CU_Context context)
{
    Dwarf_Debug dbg = 0;
    Dwarf_Byte_Ptr abbrev_end = 0;
    Dwarf_Byte_Ptr abbrev_start = 0;
    struct Dwarf_Section_s *sec = 0;

    dbg = context->cc_dbg;
    sec = &dbg->de_debug_abbrev;
    abbrev_start = sec->dss_data;
    abbrev_end = abbrev_start + sec->dss_size;
    return abbrev_end;
}