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

  Copyright (C) 2000-2006 Silicon Graphics, Inc.  All Rights Reserved.
  Portions Copyright (C) 2007-2011 David Anderson. 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.

  Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
  Mountain View, CA 94043, or:

  http://www.sgi.com

  For further information regarding this notice, see:

  http://oss.sgi.com/projects/GenInfo/NoticeExplan

*/
/* The address of the Free Software Foundation is
   Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 
   Boston, MA 02110-1301, USA.
   SGI has moved from the Crittenden Lane address.
*/





#include "config.h"
#include "dwarf_incl.h"
#include <stdio.h>
#include <stdlib.h>
#include "dwarf_frame.h"
#include "dwarf_arange.h" /* Using Arange as a way to build a list */

#define FDE_NULL_CHECKS_AND_SET_DBG(fde,dbg )          \
    do {                                               \
        if ((fde) == NULL) {                           \
            _dwarf_error(NULL, error, DW_DLE_FDE_NULL);\
        return (DW_DLV_ERROR);                         \
    }                                                  \
    (dbg)= (fde)->fd_dbg;                              \
    if ((dbg) == NULL) {                               \
        _dwarf_error(NULL, error, DW_DLE_FDE_DBG_NULL);\
        return (DW_DLV_ERROR);                         \
    } } while (0)


#define MIN(a,b)  (((a) < (b))? a:b)

static int dwarf_initialize_fde_table(Dwarf_Debug dbg,
    struct Dwarf_Frame_s *fde_table,
    unsigned table_real_data_size,
    Dwarf_Error * error);
static void dwarf_free_fde_table(struct Dwarf_Frame_s *fde_table);
static void dwarf_init_reg_rules_ru(struct Dwarf_Reg_Rule_s *base,
    unsigned first, unsigned last,int initial_value);
static void dwarf_init_reg_rules_dw(struct Dwarf_Regtable_Entry_s *base,
    unsigned first, unsigned last,int initial_value);
static void dwarf_init_reg_rules_dw3(struct Dwarf_Regtable_Entry3_s *base,
    unsigned first, unsigned last,int initial_value);


#if 0
/* Only used for debugging libdwarf. */
static void dump_frame_rule(char *msg,
    struct Dwarf_Reg_Rule_s *reg_rule);
#endif



/* 
    This function is the heart of the debug_frame stuff.  Don't even
    think of reading this without reading both the Libdwarf and 
    consumer API carefully first.  This function basically executes     
    frame instructions contained in a Cie or an Fde, but does in a      
    number of different ways depending on the information sought.       
    Start_instr_ptr points to the first byte of the frame instruction    
    stream, and final_instr_ptr to the to the first byte after the       
    last.                                                                
                                                                        
    The offsets returned in the frame instructions are factored.  That   
    is they need to be multiplied by either the code_alignment_factor    
    or the data_alignment_factor, as appropriate to obtain the actual      
    offset.  This makes it possible to expand an instruction stream      
    without the corresponding Cie.  However, when an Fde frame instr     
    sequence is being expanded there must be a valid Cie with a pointer  
    to an initial table row.                                             
                                                                         

    If successful, returns DW_DLV_OK
        And sets returned_count thru the pointer
        if make_instr is true.
        If make_instr is false returned_count 
        should NOT be used by the caller (returned_count
        is set to 0 thru the pointer by this routine...)
    If unsuccessful, returns DW_DLV_ERROR
        and sets returned_error to the error code

    It does not do a whole lot of input validation being a private 
    function.  Please make sure inputs are valid.
                                                                        
    (1) If make_instr is true, it makes a list of pointers to              
    Dwarf_Frame_Op structures containing the frame instructions          
    executed.  A pointer to this list is returned in ret_frame_instr.    
    Make_instr is true only when a list of frame instructions is to be   
    returned.  In this case since we are not interested in the contents  
    of the table, the input Cie can be NULL.  This is the only case
    where the inpute Cie can be NULL.

    (2) If search_pc is true, frame instructions are executed till       
    either a location is reached that is greater than the search_pc_val
    provided, or all instructions are executed.  At this point the       
    last row of the table generated is returned in a structure.          
    A pointer to this structure is supplied in table.                    
                                                                    
    (3) This function is also used to create the initial table row       
    defined by a Cie.  In this case, the Dwarf_Cie pointer cie, is       
    NULL.  For an FDE, however, cie points to the associated Cie.        

    make_instr - make list of frame instr? 0/1
    ret_frame_instr -  Ptr to list of ptrs to frame instrs
    search_pc  - Search for a pc value?  0/1
    search_pc_val -  Search for this pc value
    initial_loc - Initial code location value.
    start_instr_ptr -   Ptr to start of frame instrs.
    final_instr_ptr -   Ptr just past frame instrs. 
    table       -     Ptr to struct with last row. 
    cie     -   Ptr to Cie used by the Fde.

    Different cies may have distinct address-sizes, so the cie
    is used, not de_pointer_size.

*/

int
_dwarf_exec_frame_instr(Dwarf_Bool make_instr,
    Dwarf_Frame_Op ** ret_frame_instr,
    Dwarf_Bool search_pc,
    Dwarf_Addr search_pc_val,
    Dwarf_Addr initial_loc,
    Dwarf_Small * start_instr_ptr,
    Dwarf_Small * final_instr_ptr,
    Dwarf_Frame table,
    Dwarf_Cie cie,
    Dwarf_Debug dbg,
    Dwarf_Half reg_num_of_cfa,
    Dwarf_Sword * returned_count,
    int *returned_error)
{
#define ERROR_IF_REG_NUM_TOO_HIGH(macreg,machigh_reg)        \
    do {                                                     \
        if ((macreg) >= (machigh_reg) || (macreg) < 0) {     \
            SIMPLE_ERROR_RETURN(DW_DLE_DF_REG_NUM_TOO_HIGH); \
        }                                                    \
    } /*CONSTCOND */ while(0)
#define SIMPLE_ERROR_RETURN(code) \
        free(localregtab);        \
        *returned_error = code;   \
        return DW_DLV_ERROR

    /*  Sweeps the frame instructions. */
    Dwarf_Small *instr_ptr;

    /*  Register numbers not limited to just 255, thus not using
        Dwarf_Small. */
    typedef int reg_num_type;

    Dwarf_Unsigned factored_N_value;
    Dwarf_Signed signed_factored_N_value;
    Dwarf_Addr current_loc = initial_loc;       /* code location/
        pc-value corresponding to the frame instructions.
        Starts at zero when the caller has no value to pass in. */

    /*  Must be min de_pointer_size bytes and must be at least sizeof
        Dwarf_ufixed */
    Dwarf_Unsigned adv_loc = 0;

    int reg_count = dbg->de_frame_reg_rules_entry_count;
    struct Dwarf_Reg_Rule_s *localregtab = calloc(reg_count,
        sizeof(struct Dwarf_Reg_Rule_s));

    struct Dwarf_Reg_Rule_s cfa_reg;


    /*  This is used to end executing frame instructions.  */
    /*  Becomes true when search_pc is true and current_loc */
    /*  is greater than search_pc_val.  */
    Dwarf_Bool search_over = false;

    /*  Used by the DW_FRAME_advance_loc instr */
    /*  to hold the increment in pc value.  */
    Dwarf_Addr adv_pc;

    /*  Contains the length in bytes of */
    /*  an leb128 encoded number.  */
    Dwarf_Word leb128_length;

    Dwarf_Half address_size = (cie)? cie->ci_address_size: 
        dbg->de_pointer_size;

    /*  Counts the number of frame instructions executed.  */
    Dwarf_Word instr_count = 0;

    /*  These contain the current fields of the current frame
        instruction. */
    Dwarf_Small fp_base_op = 0;
    Dwarf_Small fp_extended_op;
    reg_num_type fp_register;

    /*  The value in fp_offset may be signed, though we call it
        unsigned. This works ok for 2-s complement arithmetic. */
    Dwarf_Unsigned fp_offset;
    Dwarf_Off fp_instr_offset;

    /*  Stack_table points to the row (Dwarf_Frame ie) being pushed or
        popped by a remember or restore instruction. Top_stack points to 
        the top of the stack of rows. */
    Dwarf_Frame stack_table = NULL;
    Dwarf_Frame top_stack = NULL;

    /*  These are used only when make_instr is true. Curr_instr is a
        pointer to the current frame instruction executed.
        Curr_instr_ptr, head_instr_list, and curr_instr_list are used to 
        form a chain of Dwarf_Frame_Op structs. Dealloc_instr_ptr is
        used to deallocate the structs used to form the chain.
        Head_instr_block points to a contiguous list of pointers to the
        Dwarf_Frame_Op structs executed. */
    Dwarf_Frame_Op *curr_instr;
    Dwarf_Chain curr_instr_item, dealloc_instr_item;
    Dwarf_Chain head_instr_chain = NULL;
    Dwarf_Chain tail_instr_chain = NULL;
    Dwarf_Frame_Op *head_instr_block;

    /*  These are the alignment_factors taken from the Cie provided.
        When no input Cie is provided they are set to 1, because only
        factored offsets are required. */
    Dwarf_Sword code_alignment_factor = 1;
    Dwarf_Sword data_alignment_factor = 1;

    /*  This flag indicates when an actual alignment factor is needed.
        So if a frame instruction that computes an offset using an
        alignment factor is encountered when this flag is set, an error
        is returned because the Cie did not have a valid augmentation. */
    Dwarf_Bool need_augmentation = false;

    Dwarf_Word i;

    /*  Initialize first row from associated Cie. Using temp regs
        explicity */

    if (localregtab == 0) {
        SIMPLE_ERROR_RETURN(DW_DLE_ALLOC_FAIL);
    }
    {
        struct Dwarf_Reg_Rule_s *t1reg = localregtab;
        if (cie != NULL && cie->ci_initial_table != NULL) {
            unsigned minregcount = 0;
            unsigned curreg = 0;
            struct Dwarf_Reg_Rule_s *t2reg = cie->ci_initial_table->fr_reg;

            if (reg_count != cie->ci_initial_table->fr_reg_count) {
                /*  Should never happen, it makes no sense to have the
                    table sizes change. There is no real allowance for
                    the set of registers to change dynamically in a
                    single Dwarf_Debug (except the size can be set near
                    initial Dwarf_Debug creation time). */
                SIMPLE_ERROR_RETURN
                    (DW_DLE_FRAME_REGISTER_COUNT_MISMATCH);
            }
            minregcount = MIN(reg_count,cie->ci_initial_table->fr_reg_count);
            for (; curreg < minregcount ;curreg++, t1reg++, t2reg++) {
                *t1reg = *t2reg;
            }
            cfa_reg = cie->ci_initial_table->fr_cfa_rule;
        } else {
            dwarf_init_reg_rules_ru(localregtab,0,reg_count,
                dbg->de_frame_rule_initial_value);
            dwarf_init_reg_rules_ru(&cfa_reg,0, 1,
                dbg->de_frame_rule_initial_value);
        }
    }

    /*  The idea here is that the code_alignment_factor and
        data_alignment_factor which are needed for certain instructions
        are valid only when the Cie has a proper augmentation string. So 
        if the augmentation is not right, only Frame instruction can be
        read. */
    if (cie != NULL && cie->ci_augmentation != NULL) {
        code_alignment_factor = cie->ci_code_alignment_factor;
        data_alignment_factor = cie->ci_data_alignment_factor;
    } else {
        need_augmentation = !make_instr;
    }

    instr_ptr = start_instr_ptr;
    while ((instr_ptr < final_instr_ptr) && (!search_over)) {
        Dwarf_Small instr = 0;
        Dwarf_Small opcode = 0;
        reg_num_type reg_no = 0;

        fp_instr_offset = instr_ptr - start_instr_ptr;
        instr = *(Dwarf_Small *) instr_ptr;
        instr_ptr += sizeof(Dwarf_Small);

        fp_base_op = (instr & 0xc0) >> 6;
        if ((instr & 0xc0) == 0x00) {
            opcode = instr;     /* is really extended op */
            fp_extended_op = (instr & (~(0xc0))) & 0xff;
        } else {
            opcode = instr & 0xc0;      /* is base op */
            fp_extended_op = 0;
        }

        fp_register = 0;
        fp_offset = 0;
        switch (opcode) {
        case DW_CFA_advance_loc:
            {
                /* base op */
                fp_offset = adv_pc = instr & DW_FRAME_INSTR_OFFSET_MASK;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                adv_pc = adv_pc * code_alignment_factor;

                search_over = search_pc &&
                    (current_loc + adv_pc > search_pc_val);
                /* If gone past pc needed, retain old pc.  */
                if (!search_over) {
                    current_loc = current_loc + adv_pc;
                }
                break;
            }

        case DW_CFA_offset:
            {                   /* base op */
                reg_no =
                    (reg_num_type) (instr & DW_FRAME_INSTR_OFFSET_MASK);
                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);

                factored_N_value =
                    _dwarf_decode_u_leb128(instr_ptr, &leb128_length);
                instr_ptr = instr_ptr + leb128_length;

                fp_register = reg_no;
                fp_offset = factored_N_value;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }

                localregtab[reg_no].ru_is_off = 1;
                localregtab[reg_no].ru_value_type = DW_EXPR_OFFSET;
                localregtab[reg_no].ru_register = reg_num_of_cfa;
                localregtab[reg_no].ru_offset_or_block_len =
                    factored_N_value * data_alignment_factor;

                break;
            }

        case DW_CFA_restore:
            {                   /* base op */
                reg_no = (instr & DW_FRAME_INSTR_OFFSET_MASK);
                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);

                fp_register = reg_no;

                if (cie != NULL && cie->ci_initial_table != NULL)
                    localregtab[reg_no] = 
                        cie->ci_initial_table->fr_reg[reg_no];
                else if (!make_instr) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_MAKE_INSTR_NO_INIT);
                }

                break;
            }
        case DW_CFA_set_loc:
            {
                Dwarf_Addr new_loc = 0;

                READ_UNALIGNED(dbg, new_loc, Dwarf_Addr,
                    instr_ptr, address_size);
                instr_ptr += address_size;
                if (new_loc != 0 && current_loc != 0) {
                    /*  Pre-relocation or before current_loc is set the
                        test comparing new_loc and current_loc makes no
                        sense. Testing for non-zero (above) is a way
                        (fallible) to check that current_loc, new_loc
                        are already relocated.  */
                    if (new_loc <= current_loc) {
                        /*  Within a frame, address must increase.
                            Seemingly it has not. Seems to be an error. */

                        SIMPLE_ERROR_RETURN
                            (DW_DLE_DF_NEW_LOC_LESS_OLD_LOC);
                    }
                }

                search_over = search_pc && (new_loc > search_pc_val);

                /* If gone past pc needed, retain old pc.  */
                if (!search_over) {
                    current_loc = new_loc;
                }
                fp_offset = new_loc;
                break;
            }

        case DW_CFA_advance_loc1:
            {
                fp_offset = adv_loc = *(Dwarf_Small *) instr_ptr;
                instr_ptr += sizeof(Dwarf_Small);

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                adv_loc *= code_alignment_factor;

                search_over = search_pc &&
                    (current_loc + adv_loc > search_pc_val);

                /* If gone past pc needed, retain old pc.  */
                if (!search_over) {
                    current_loc = current_loc + adv_loc;
                }
                break;
            }

        case DW_CFA_advance_loc2:
            {
                READ_UNALIGNED(dbg, adv_loc, Dwarf_Unsigned,
                    instr_ptr, sizeof(Dwarf_Half));
                instr_ptr += sizeof(Dwarf_Half);
                fp_offset = adv_loc;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                adv_loc *= code_alignment_factor;

                search_over = search_pc &&
                    (current_loc + adv_loc > search_pc_val);

                /* If gone past pc needed, retain old pc.  */
                if (!search_over) {
                    current_loc = current_loc + adv_loc;
                }
                break;
            }

        case DW_CFA_advance_loc4:
            {
                READ_UNALIGNED(dbg, adv_loc, Dwarf_Unsigned,
                    instr_ptr, sizeof(Dwarf_ufixed));
                instr_ptr += sizeof(Dwarf_ufixed);
                fp_offset = adv_loc;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                adv_loc *= code_alignment_factor;

                search_over = search_pc &&
                    (current_loc + adv_loc > search_pc_val);

                /* If gone past pc needed, retain old pc.  */
                if (!search_over) {
                    current_loc = current_loc + adv_loc;
                }
                break;
            }

        case DW_CFA_offset_extended:
            {
                Dwarf_Unsigned lreg;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;
                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);;
                factored_N_value =
                    _dwarf_decode_u_leb128(instr_ptr, &leb128_length);
                instr_ptr += leb128_length;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                localregtab[reg_no].ru_is_off = 1;
                localregtab[reg_no].ru_value_type = DW_EXPR_OFFSET;
                localregtab[reg_no].ru_register = reg_num_of_cfa;
                localregtab[reg_no].ru_offset_or_block_len = factored_N_value *
                    data_alignment_factor;

                fp_register = reg_no;
                fp_offset = factored_N_value;
                break;
            }

        case DW_CFA_restore_extended:
            {
                Dwarf_Unsigned lreg;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;

                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);

                if (cie != NULL && cie->ci_initial_table != NULL) {
                    localregtab[reg_no] = cie->ci_initial_table->fr_reg[reg_no];
                } else {
                    if (!make_instr) {
                        SIMPLE_ERROR_RETURN
                            (DW_DLE_DF_MAKE_INSTR_NO_INIT);
                    }
                }

                fp_register = reg_no;
                break;
            }

        case DW_CFA_undefined:
            {
                Dwarf_Unsigned lreg;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;
                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);

                localregtab[reg_no].ru_is_off = 0;
                localregtab[reg_no].ru_value_type = DW_EXPR_OFFSET;
                localregtab[reg_no].ru_register = 
                    dbg->de_frame_undefined_value_number;
                localregtab[reg_no].ru_offset_or_block_len = 0;

                fp_register = reg_no;
                break;
            }

        case DW_CFA_same_value:
            {
                Dwarf_Unsigned lreg;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;
                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);

                localregtab[reg_no].ru_is_off = 0;
                localregtab[reg_no].ru_value_type = DW_EXPR_OFFSET;
                localregtab[reg_no].ru_register = 
                    dbg->de_frame_same_value_number;
                localregtab[reg_no].ru_offset_or_block_len = 0;
                fp_register = reg_no;
                break;
            }

        case DW_CFA_register:
            {
                Dwarf_Unsigned lreg;
                reg_num_type reg_noA = 0;
                reg_num_type reg_noB = 0;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_noA = (reg_num_type) lreg;

                ERROR_IF_REG_NUM_TOO_HIGH(reg_noA, reg_count);

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_noB = (reg_num_type) lreg;

                if (reg_noB > reg_count) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_REG_NUM_TOO_HIGH);
                }


                localregtab[reg_noA].ru_is_off = 0;
                localregtab[reg_noA].ru_value_type = DW_EXPR_OFFSET;
                localregtab[reg_noA].ru_register = reg_noB;
                localregtab[reg_noA].ru_offset_or_block_len = 0;

                fp_register = reg_noA;
                fp_offset = reg_noB;
                break;
            }

        case DW_CFA_remember_state:
            {
                stack_table = (Dwarf_Frame)
                    _dwarf_get_alloc(dbg, DW_DLA_FRAME, 1);
                if (stack_table == NULL) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_ALLOC_FAIL);
                }

                for (i = 0; i < reg_count; i++)
                    stack_table->fr_reg[i] = localregtab[i];
                stack_table->fr_cfa_rule = cfa_reg;

                if (top_stack != NULL)
                    stack_table->fr_next = top_stack;
                top_stack = stack_table;

                break;
            }

        case DW_CFA_restore_state:
            {
                if (top_stack == NULL) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_POP_EMPTY_STACK);
                }
                stack_table = top_stack;
                top_stack = stack_table->fr_next;

                for (i = 0; i < reg_count; i++)
                    localregtab[i] = stack_table->fr_reg[i];
                cfa_reg = stack_table->fr_cfa_rule;

                dwarf_dealloc(dbg, stack_table, DW_DLA_FRAME);
                break;
            }

        case DW_CFA_def_cfa:
            {
                Dwarf_Unsigned lreg;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;

                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);

                factored_N_value =
                    _dwarf_decode_u_leb128(instr_ptr, &leb128_length);
                instr_ptr += leb128_length;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                cfa_reg.ru_is_off = 1;
                cfa_reg.ru_value_type = DW_EXPR_OFFSET;
                cfa_reg.ru_register = reg_no;
                cfa_reg.ru_offset_or_block_len = factored_N_value;

                fp_register = reg_no;
                fp_offset = factored_N_value;
                break;
            }

        case DW_CFA_def_cfa_register:
            {
                Dwarf_Unsigned lreg;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;
                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);

                cfa_reg.ru_register = reg_no;
                /*  Do NOT set ru_offset_or_block_len or ru_is_off here. 
                    See dwarf2/3 spec.  */
                fp_register = reg_no;
                break;
            }

        case DW_CFA_def_cfa_offset:
            {
                factored_N_value =
                    _dwarf_decode_u_leb128(instr_ptr, &leb128_length);
                instr_ptr += leb128_length;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                /*  Do set ru_is_off here, as here factored_N_value
                    counts.  */
                cfa_reg.ru_is_off = 1;
                cfa_reg.ru_value_type = DW_EXPR_OFFSET;
                cfa_reg.ru_offset_or_block_len = factored_N_value;

                fp_offset = factored_N_value;
                break;
            }
        case DW_CFA_nop:
            {
                break;
            }
            /* DWARF3 ops begin here. */
        case DW_CFA_def_cfa_expression:
            {
                /*  A single DW_FORM_block representing a dwarf
                    expression. The form block establishes the way to
                    compute the CFA. */
                Dwarf_Unsigned block_len = 0;

                DECODE_LEB128_UWORD(instr_ptr, block_len);
                cfa_reg.ru_is_off = 0;  /* arbitrary */
                cfa_reg.ru_value_type = DW_EXPR_EXPRESSION;
                cfa_reg.ru_offset_or_block_len = block_len;
                cfa_reg.ru_block = instr_ptr;
                fp_offset = (Dwarf_Unsigned) instr_ptr;
                instr_ptr += block_len;
            }
            break;
        case DW_CFA_expression:
            {
                /*  An unsigned leb128 value is the first operand (a
                    register number). The second operand is single
                    DW_FORM_block representing a dwarf expression. The
                    evaluator pushes the CFA on the evaluation stack
                    then evaluates the expression to compute the value
                    of the register contents. */
                Dwarf_Unsigned lreg = 0;
                Dwarf_Unsigned block_len = 0;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;
                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
                DECODE_LEB128_UWORD(instr_ptr, block_len);
                localregtab[lreg].ru_is_off = 0;        /* arbitrary */
                localregtab[lreg].ru_value_type = DW_EXPR_EXPRESSION;
                localregtab[lreg].ru_offset_or_block_len = block_len;
                localregtab[lreg].ru_block = instr_ptr;
                fp_offset = (Dwarf_Unsigned) instr_ptr;
                fp_register = reg_no;
                instr_ptr += block_len;
            }
            break;
        case DW_CFA_offset_extended_sf:
            {
                /*  The first operand is an unsigned leb128 register
                    number. The second is a signed factored offset.
                    Identical to DW_CFA_offset_extended except the
                    second operand is signed */
                Dwarf_Unsigned lreg;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;
                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
                signed_factored_N_value =
                    _dwarf_decode_s_leb128(instr_ptr, &leb128_length);
                instr_ptr += leb128_length;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                localregtab[reg_no].ru_is_off = 1;
                localregtab[reg_no].ru_value_type = DW_EXPR_OFFSET;
                localregtab[reg_no].ru_register = reg_num_of_cfa;
                localregtab[reg_no].ru_offset_or_block_len =
                    signed_factored_N_value * data_alignment_factor;

                fp_register = reg_no;
                fp_offset = signed_factored_N_value;
            }
            break;
        case DW_CFA_def_cfa_sf:
            {
                /*  The first operand is an unsigned leb128 register
                    number. The second is a signed leb128 factored
                    offset. Identical to DW_CFA_def_cfa except that the
                    second operand is signed and factored. */
                Dwarf_Unsigned lreg;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;
                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);

                signed_factored_N_value =
                    _dwarf_decode_s_leb128(instr_ptr, &leb128_length);
                instr_ptr += leb128_length;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                cfa_reg.ru_is_off = 1;
                cfa_reg.ru_value_type = DW_EXPR_OFFSET;
                cfa_reg.ru_register = reg_no;
                cfa_reg.ru_offset_or_block_len =
                    signed_factored_N_value * data_alignment_factor;

                fp_register = reg_no;
                fp_offset = signed_factored_N_value;
            }
            break;
        case DW_CFA_def_cfa_offset_sf:
            {
                /*  The operand is a signed leb128 operand representing
                    a factored offset.  Identical to
                    DW_CFA_def_cfa_offset excep the operand is signed
                    and factored. */

                signed_factored_N_value =
                    _dwarf_decode_s_leb128(instr_ptr, &leb128_length);
                instr_ptr += leb128_length;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                /*  Do set ru_is_off here, as here factored_N_value
                    counts.  */
                cfa_reg.ru_is_off = 1;
                cfa_reg.ru_value_type = DW_EXPR_OFFSET;
                cfa_reg.ru_offset_or_block_len =
                    signed_factored_N_value * data_alignment_factor;

                fp_offset = signed_factored_N_value;
            }
            break;
        case DW_CFA_val_offset:
            {
                /*  The first operand is an unsigned leb128 register
                    number. The second is a factored unsigned offset.
                    Makes the register be a val_offset(N) rule with N =
                    factored_offset*data_alignment_factor. */

                Dwarf_Unsigned lreg;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;

                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);

                factored_N_value =
                    _dwarf_decode_u_leb128(instr_ptr, &leb128_length);
                instr_ptr += leb128_length;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                /*  Do set ru_is_off here, as here factored_N_value
                    counts.  */
                localregtab[reg_no].ru_is_off = 1;
                localregtab[reg_no].ru_register = reg_num_of_cfa;
                localregtab[reg_no].ru_value_type = DW_EXPR_VAL_OFFSET;
                localregtab[reg_no].ru_offset_or_block_len =
                    factored_N_value * data_alignment_factor;

                fp_offset = factored_N_value;
                break;
            }
        case DW_CFA_val_offset_sf:
            {
                /*  The first operand is an unsigned leb128 register
                    number. The second is a factored signed offset.
                    Makes the register be a val_offset(N) rule with N =
                    factored_offset*data_alignment_factor. */
                Dwarf_Unsigned lreg;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;

                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
                signed_factored_N_value =
                    _dwarf_decode_s_leb128(instr_ptr, &leb128_length);
                instr_ptr += leb128_length;

                if (need_augmentation) {
                    SIMPLE_ERROR_RETURN(DW_DLE_DF_NO_CIE_AUGMENTATION);
                }
                /*  Do set ru_is_off here, as here factored_N_value
                    counts.  */
                localregtab[reg_no].ru_is_off = 1;
                localregtab[reg_no].ru_value_type = DW_EXPR_VAL_OFFSET;
                localregtab[reg_no].ru_offset_or_block_len =
                    signed_factored_N_value * data_alignment_factor;

                fp_offset = signed_factored_N_value;

            }
            break;
        case DW_CFA_val_expression:
            {
                /*  The first operand is an unsigned leb128 register
                    number. The second is a DW_FORM_block representing a 
                    DWARF expression. The rule for the register number
                    becomes a val_expression(E) rule. */
                Dwarf_Unsigned lreg = 0;
                Dwarf_Unsigned block_len = 0;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;
                ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
                DECODE_LEB128_UWORD(instr_ptr, block_len);
                localregtab[lreg].ru_is_off = 0;        /* arbitrary */
                localregtab[lreg].ru_value_type = DW_EXPR_VAL_EXPRESSION;
                localregtab[lreg].ru_offset_or_block_len = block_len;
                localregtab[lreg].ru_block = instr_ptr;
                fp_offset = (Dwarf_Unsigned) instr_ptr;

                instr_ptr += block_len;
                fp_register = reg_no;

            }
            break;

            /* END DWARF3 new ops. */


#ifdef DW_CFA_GNU_window_save
        case DW_CFA_GNU_window_save:
            {
                /*  No information: this just tells unwinder to restore
                    the window registers from the previous frame's
                    window save area */
                break;
            }
#endif
#ifdef  DW_CFA_GNU_args_size
            /*  Single uleb128 is the current arg area size in bytes. No 
                register exists yet to save this in */
        case DW_CFA_GNU_args_size:
            {
                Dwarf_Unsigned lreg;

                DECODE_LEB128_UWORD(instr_ptr, lreg);
                reg_no = (reg_num_type) lreg;

                break;
            }
#endif
        default:
            /*  ERROR, we have an opcode we know nothing about. Memory
                leak here, but an error like this is not supposed to
                happen so we ignore the leak. These used to be ignored,
                now we notice and report. */
            SIMPLE_ERROR_RETURN(DW_DLE_DF_FRAME_DECODING_ERROR);

        }

        if (make_instr) {
            instr_count++;

            curr_instr = (Dwarf_Frame_Op *)
                _dwarf_get_alloc(dbg, DW_DLA_FRAME_OP, 1);
            if (curr_instr == NULL) {
                SIMPLE_ERROR_RETURN(DW_DLE_DF_ALLOC_FAIL);
            }

            curr_instr->fp_base_op = fp_base_op;
            curr_instr->fp_extended_op = fp_extended_op;
            curr_instr->fp_register = fp_register;
            curr_instr->fp_offset = fp_offset;
            curr_instr->fp_instr_offset = fp_instr_offset;

            curr_instr_item = (Dwarf_Chain)
                _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
            if (curr_instr_item == NULL) {
                SIMPLE_ERROR_RETURN(DW_DLE_DF_ALLOC_FAIL);
            }

            curr_instr_item->ch_item = curr_instr;
            if (head_instr_chain == NULL)
                head_instr_chain = tail_instr_chain = curr_instr_item;
            else {
                tail_instr_chain->ch_next = curr_instr_item;
                tail_instr_chain = curr_instr_item;
            }
        }
    }

    /*  If frame instruction decoding was right we would stop exactly at 
        final_instr_ptr. */
    if (instr_ptr > final_instr_ptr) {
        SIMPLE_ERROR_RETURN(DW_DLE_DF_FRAME_DECODING_ERROR);
    }

    /* Fill in the actual output table, the space the caller passed in. */
    if (table != NULL) {

        struct Dwarf_Reg_Rule_s *t2reg = table->fr_reg;
        struct Dwarf_Reg_Rule_s *t3reg = localregtab;
        unsigned minregcount =  MIN(table->fr_reg_count,reg_count);
        unsigned curreg = 0;

        table->fr_loc = current_loc;
        for (; curreg < minregcount ; curreg++, t3reg++, t2reg++) {
            *t2reg = *t3reg;
        }

        /*  CONSTCOND */
        /*  Do not update the main table with the cfa_reg.
            Just leave cfa_reg as cfa_reg. */
        table->fr_cfa_rule = cfa_reg;
    }

    /* Dealloc anything remaining on stack. */
    for (; top_stack != NULL;) {
        stack_table = top_stack;
        top_stack = top_stack->fr_next;
        dwarf_dealloc(dbg, stack_table, DW_DLA_FRAME);
    }

    if (make_instr) {
        /*  Allocate list of pointers to Dwarf_Frame_Op's.  */
        head_instr_block = (Dwarf_Frame_Op *)
            _dwarf_get_alloc(dbg, DW_DLA_FRAME_BLOCK, instr_count);
        if (head_instr_block == NULL) {
            SIMPLE_ERROR_RETURN(DW_DLE_DF_ALLOC_FAIL);
        }

        /*  Store pointers to Dwarf_Frame_Op's in this list and
            deallocate the structs that chain the Dwarf_Frame_Op's. */
        curr_instr_item = head_instr_chain;
        for (i = 0; i < instr_count; i++) {
            *(head_instr_block + i) =
                *(Dwarf_Frame_Op *) curr_instr_item->ch_item;
            dealloc_instr_item = curr_instr_item;
            curr_instr_item = curr_instr_item->ch_next;
            dwarf_dealloc(dbg, dealloc_instr_item->ch_item,
                DW_DLA_FRAME_OP);
            dwarf_dealloc(dbg, dealloc_instr_item, DW_DLA_CHAIN);
        }
        *ret_frame_instr = head_instr_block;
        *returned_count = (Dwarf_Sword) instr_count;
    } else {
        *returned_count = 0;
    }
    free(localregtab);
    return DW_DLV_OK;
#undef ERROR_IF_REG_NUM_TOO_HIGH
#undef SIMPLE_ERROR_RETURN
}

/*  Depending on version, either read the return address register
    as a ubyte or as an leb number.
    The form of this value changed for DWARF3.
*/
Dwarf_Unsigned
_dwarf_get_return_address_reg(Dwarf_Small * frame_ptr,
    int version, unsigned long *size)
{
    Dwarf_Unsigned uvalue = 0;
    Dwarf_Word leb128_length = 0;

    if (version == 1) {
        *size = 1;
        uvalue = *(unsigned char *) frame_ptr;
        return uvalue;
    }
    uvalue = _dwarf_decode_u_leb128(frame_ptr, &leb128_length);
    *size = leb128_length;
    return uvalue;
}


/* Trivial consumer function. 
*/
int
dwarf_get_cie_of_fde(Dwarf_Fde fde,
    Dwarf_Cie * cie_returned, Dwarf_Error * error)
{
    if (fde == NULL) {
        _dwarf_error(NULL, error, DW_DLE_FDE_NULL);
        return (DW_DLV_ERROR);
    }

    *cie_returned = fde->fd_cie;
    return DW_DLV_OK;

}

int dwarf_get_cie_index(
    Dwarf_Cie cie,
    Dwarf_Signed* index, 
    Dwarf_Error* error )
{
    if( cie == NULL )
    {
        _dwarf_error(NULL, error, DW_DLE_CIE_NULL);
        return (DW_DLV_ERROR);
    }

    *index = cie->ci_index;
    return (DW_DLV_OK);
}

/*  For g++ .eh_frame fde and cie.
    the cie id is different as the
    definition of the cie_id in an fde
        is the distance back from the address of the
        value to the cie.
    Or 0 if this is a true cie.
    Non standard dwarf, designed this way to be
    convenient at run time for an allocated 
    (mapped into memory as part of the running image) section.
*/
int
dwarf_get_fde_list_eh(Dwarf_Debug dbg,
    Dwarf_Cie ** cie_data,
    Dwarf_Signed * cie_element_count,
    Dwarf_Fde ** fde_data,
    Dwarf_Signed * fde_element_count,
    Dwarf_Error * error)
{
    int res = _dwarf_load_section(dbg, &dbg->de_debug_frame_eh_gnu,error);
    if (res != DW_DLV_OK) {
        return res;
    }

    res = _dwarf_get_fde_list_internal(dbg,
        cie_data,
        cie_element_count,
        fde_data,
        fde_element_count,
        dbg->de_debug_frame_eh_gnu.dss_data,
        dbg->de_debug_frame_eh_gnu.dss_index,
        dbg->de_debug_frame_eh_gnu.dss_size,
        /* cie_id_value */ 0,
        /* use_gnu_cie_calc= */ 1,
        error);
    return res;
}



/*  For standard dwarf .debug_frame
    cie_id is -1  in a cie, and
    is the section offset in the .debug_frame section
    of the cie otherwise.  Standard dwarf
*/
int
dwarf_get_fde_list(Dwarf_Debug dbg,
    Dwarf_Cie ** cie_data,
    Dwarf_Signed * cie_element_count,
    Dwarf_Fde ** fde_data,
    Dwarf_Signed * fde_element_count,
    Dwarf_Error * error)
{
    int res = _dwarf_load_section(dbg, &dbg->de_debug_frame,error);
    if (res != DW_DLV_OK) {
        return res;
    }

    res = _dwarf_get_fde_list_internal(dbg, cie_data,
        cie_element_count,
        fde_data,
        fde_element_count,
        dbg->de_debug_frame.dss_data,
        dbg->de_debug_frame.dss_index,
        dbg->de_debug_frame.dss_size,
        DW_CIE_ID,
        /* use_gnu_cie_calc= */ 0,
        error);

    return res;
}


/*  Only works on dwarf sections, not eh_frame
    Given a Dwarf_Die, see if it has a
    DW_AT_MIPS_fde attribute and if so use that
    to get an fde offset.
    Then create a Dwarf_Fde to return thru the ret_fde pointer.
    Also creates a cie (pointed at from the Dwarf_Fde).  */
int
dwarf_get_fde_for_die(Dwarf_Debug dbg,
    Dwarf_Die die,
    Dwarf_Fde * ret_fde, Dwarf_Error * error)
{
    Dwarf_Attribute attr;
    Dwarf_Unsigned fde_offset = 0;
    Dwarf_Signed signdval = 0;
    Dwarf_Fde new_fde = 0;
    unsigned char *fde_ptr = 0;
    unsigned char *cie_ptr = 0;
    Dwarf_Unsigned cie_id = 0;

    /* Fields for the current Cie being read. */
    int res = 0;
    int resattr = 0;
    int sdatares = 0;

    struct cie_fde_prefix_s prefix;
    struct cie_fde_prefix_s prefix_c;

    if (die == NULL) {
        _dwarf_error(NULL, error, DW_DLE_DIE_NULL);
        return (DW_DLV_ERROR);
    }

    resattr = dwarf_attr(die, DW_AT_MIPS_fde, &attr, error);
    if (resattr != DW_DLV_OK) {
        return resattr;
    }

    /* why is this formsdata? FIX */
    sdatares = dwarf_formsdata(attr, &signdval, error);
    if (sdatares != DW_DLV_OK) {
        return sdatares;
    }

    res = _dwarf_load_section(dbg, &dbg->de_debug_frame,error);
    if (res != DW_DLV_OK) {
        return res;
    }

    fde_offset = signdval;
    fde_ptr = (dbg->de_debug_frame.dss_data + fde_offset);


    /*  First read in the 'common prefix' to figure out what * we are to 
        do with this entry. */
    memset(&prefix_c, 0, sizeof(prefix_c));
    memset(&prefix, 0, sizeof(prefix));
    res = dwarf_read_cie_fde_prefix(dbg, fde_ptr,
        dbg->de_debug_frame.dss_data,
        dbg->de_debug_frame.dss_index,
        dbg->de_debug_frame.dss_size, 
        &prefix,
        error);
    if (res == DW_DLV_ERROR) {
        return res;
    }
    if (res == DW_DLV_NO_ENTRY)
        return res;
    fde_ptr = prefix.cf_addr_after_prefix;
    cie_id = prefix.cf_cie_id;
    /*  Pass NULL, not section pointer, for 3rd argument. 
        de_debug_frame.dss_data has no eh_frame relevance. */
    res = dwarf_create_fde_from_after_start(dbg, &prefix,
        (Dwarf_Small *) NULL,
        fde_ptr,
        /* use_gnu_cie_calc= */ 0,
        /* Dwarf_Cie = */ 0,
        &new_fde, error);
    if (res == DW_DLV_ERROR) {
        return res;
    } else if (res == DW_DLV_NO_ENTRY) {
        return res;
    }
    /* DW_DLV_OK */

    /* now read the cie corresponding to the fde */
    cie_ptr = new_fde->fd_section_ptr + cie_id;
    res = dwarf_read_cie_fde_prefix(dbg, cie_ptr,
        dbg->de_debug_frame.dss_data,
        dbg->de_debug_frame.dss_index,
        dbg->de_debug_frame.dss_size,
        &prefix_c, error);
    if (res == DW_DLV_ERROR) {
        return res;
    }
    if (res == DW_DLV_NO_ENTRY)
        return res;

    cie_ptr = prefix_c.cf_addr_after_prefix;
    cie_id = prefix_c.cf_cie_id;

    if (cie_id == DW_CIE_ID) {
        int res2 = 0;
        Dwarf_Cie new_cie = 0;

        /*  Pass NULL, not section pointer, for 3rd argument.
            de_debug_frame.dss_data has no eh_frame relevance. */
        res2 = dwarf_create_cie_from_after_start(dbg,
            &prefix_c,
            (Dwarf_Small *) NULL,
            cie_ptr,
            /* cie_count= */ 0,
            /* use_gnu_cie_calc= */
            0, &new_cie, error);
        if (res2 == DW_DLV_ERROR) {
            dwarf_dealloc(dbg, new_fde, DW_DLA_FDE);
            return res;
        } else if (res2 == DW_DLV_NO_ENTRY) {
            dwarf_dealloc(dbg, new_fde, DW_DLA_FDE);
            return res;
        }
        new_fde->fd_cie = new_cie;
    } else {
        _dwarf_error(dbg, error, DW_DLE_NO_CIE_FOR_FDE);
        return (DW_DLV_ERROR);
    }

    *ret_fde = new_fde;
    return DW_DLV_OK;
}

/* A dwarf consumer operation, see the consumer library documentation.
*/
int
dwarf_get_fde_range(Dwarf_Fde fde,
    Dwarf_Addr * low_pc,
    Dwarf_Unsigned * func_length,
    Dwarf_Ptr * fde_bytes,
    Dwarf_Unsigned * fde_byte_length,
    Dwarf_Off * cie_offset,
    Dwarf_Signed * cie_index,
    Dwarf_Off * fde_offset, Dwarf_Error * error)
{
    Dwarf_Debug dbg;

    if (fde == NULL) {
        _dwarf_error(NULL, error, DW_DLE_FDE_NULL);
        return (DW_DLV_ERROR);
    }

    dbg = fde->fd_dbg;
    if (dbg == NULL) {
        _dwarf_error(NULL, error, DW_DLE_FDE_DBG_NULL);
        return (DW_DLV_ERROR);
    }


    /*  We have always already done the section load here, so no need to 
        load the section. We did the section load in order to create the 
        Dwarf_Fde pointer passed in here. */


    if (low_pc != NULL)
        *low_pc = fde->fd_initial_location;
    if (func_length != NULL)
        *func_length = fde->fd_address_range;
    if (fde_bytes != NULL)
        *fde_bytes = fde->fd_fde_start;
    if (fde_byte_length != NULL)
        *fde_byte_length = fde->fd_length;
    if (cie_offset != NULL)
        *cie_offset = fde->fd_cie_offset;
    if (cie_index != NULL)
        *cie_index = fde->fd_cie_index;
    if (fde_offset != NULL)
        *fde_offset = fde->fd_fde_start - fde->fd_section_ptr;

    return DW_DLV_OK;
}

/*  IRIX specific function.   The exception tables
    have C++ destructor information and are
    at present undocumented.  */
int
dwarf_get_fde_exception_info(Dwarf_Fde fde,
    Dwarf_Signed *
    offset_into_exception_tables,
    Dwarf_Error * error)
{
    Dwarf_Debug dbg;

    dbg = fde->fd_dbg;
    if (dbg == NULL) {
        _dwarf_error(NULL, error, DW_DLE_FDE_DBG_NULL);
        return (DW_DLV_ERROR);
    }
    *offset_into_exception_tables =
        fde->fd_offset_into_exception_tables;
    return DW_DLV_OK;
}


/*  A consumer code function.
    Given a CIE pointer, return the normal CIE data thru
    pointers.
    Special augmentation data is not returned here.
*/
int
dwarf_get_cie_info(Dwarf_Cie cie,
    Dwarf_Unsigned * bytes_in_cie,
    Dwarf_Small * ptr_to_version,
    char **augmenter,
    Dwarf_Unsigned * code_alignment_factor,
    Dwarf_Signed * data_alignment_factor,
    Dwarf_Half * return_address_register,
    Dwarf_Ptr * initial_instructions,
    Dwarf_Unsigned * initial_instructions_length,
    Dwarf_Error * error)
{
    Dwarf_Debug dbg;

    if (cie == NULL) {
        _dwarf_error(NULL, error, DW_DLE_CIE_NULL);
        return (DW_DLV_ERROR);
    }

    dbg = cie->ci_dbg;
    if (dbg == NULL) {
        _dwarf_error(NULL, error, DW_DLE_CIE_DBG_NULL);
        return (DW_DLV_ERROR);
    }

    if (ptr_to_version != NULL)
        *ptr_to_version = cie->ci_cie_version_number;
    if (augmenter != NULL)
        *augmenter = cie->ci_augmentation;
    if (code_alignment_factor != NULL)
        *code_alignment_factor = cie->ci_code_alignment_factor;
    if (data_alignment_factor != NULL)
        *data_alignment_factor = cie->ci_data_alignment_factor;
    if (return_address_register != NULL)
        *return_address_register = cie->ci_return_address_register;
    if (initial_instructions != NULL)
        *initial_instructions = cie->ci_cie_instr_start;
    if (initial_instructions_length != NULL) {
        *initial_instructions_length = cie->ci_length +
            cie->ci_length_size +
            cie->ci_extension_size -
            (cie->ci_cie_instr_start - cie->ci_cie_start);

    }
    *bytes_in_cie = (cie->ci_length);
    return (DW_DLV_OK);
}

/* Return the register rules for all registers at a given pc. 
*/
static int
_dwarf_get_fde_info_for_a_pc_row(Dwarf_Fde fde,
    Dwarf_Addr pc_requested,
    Dwarf_Frame table,
    Dwarf_Half cfa_reg_col_num,
    Dwarf_Error * error)
{
    Dwarf_Debug dbg = 0;
    Dwarf_Cie cie = 0;
    int dw_err = 0;
    Dwarf_Sword icount = 0;
    int res = 0;

    if (fde == NULL) {
        _dwarf_error(NULL, error, DW_DLE_FDE_NULL);
        return (DW_DLV_ERROR);
    }

    dbg = fde->fd_dbg;
    if (dbg == NULL) {
        _dwarf_error(NULL, error, DW_DLE_FDE_DBG_NULL);
        return (DW_DLV_ERROR);
    }

    if (pc_requested < fde->fd_initial_location ||
        pc_requested >=
        fde->fd_initial_location + fde->fd_address_range) {
        _dwarf_error(dbg, error, DW_DLE_PC_NOT_IN_FDE_RANGE);
        return (DW_DLV_ERROR);
    }

    cie = fde->fd_cie;
    if (cie->ci_initial_table == NULL) {
        cie->ci_initial_table = _dwarf_get_alloc(dbg, DW_DLA_FRAME, 1);

        if (cie->ci_initial_table == NULL) {
            _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
            return (DW_DLV_ERROR);
        }
        dwarf_init_reg_rules_ru(cie->ci_initial_table->fr_reg,
            0, cie->ci_initial_table->fr_reg_count,dbg->de_frame_rule_initial_value);
        dwarf_init_reg_rules_ru(&cie->ci_initial_table->fr_cfa_rule,
            0,1,dbg->de_frame_rule_initial_value);
        res = _dwarf_exec_frame_instr( /* make_instr= */ false,
            /* ret_frame_instr= */ NULL,
            /* search_pc */ false,
            /* search_pc_val */ 0,
            /* location */ 0,
            cie->ci_cie_instr_start,
            cie->ci_cie_instr_start + (cie->ci_length +
                cie->ci_length_size +
                cie->ci_extension_size -
                (cie->ci_cie_instr_start -
                cie->ci_cie_start)),
            cie->ci_initial_table, cie, dbg,
            cfa_reg_col_num, &icount,
            &dw_err);
        if (res == DW_DLV_ERROR) {
            _dwarf_error(dbg, error, dw_err);
            return (res);
        } else if (res == DW_DLV_NO_ENTRY) {
            return res;
        }
    }

    {
        Dwarf_Small *instr_end = fde->fd_fde_instr_start +
            fde->fd_length +
            fde->fd_length_size +
            fde->fd_extension_size - (fde->fd_fde_instr_start -
                fde->fd_fde_start);

        res = _dwarf_exec_frame_instr( /* make_instr= */ false,
            /* ret_frame_instr= */ NULL,
            /* search_pc */ true,
            /* search_pc_val */ pc_requested,
            fde->fd_initial_location,
            fde->fd_fde_instr_start,
            instr_end,
            table,
            cie, dbg,
            cfa_reg_col_num, &icount,
            &dw_err);
    }
    if (res == DW_DLV_ERROR) {
        _dwarf_error(dbg, error, dw_err);
        return (res);
    } else if (res == DW_DLV_NO_ENTRY) {
        return res;
    }

    return DW_DLV_OK;
}

/*  A consumer call for efficiently getting the register info
    for all registers in one call.

    The output table rules array is size DW_REG_TABLE_SIZE.
    The frame info  rules array in fde_table is of size
    DW_REG_TABLE_SIZE too.

    This interface  really only works well with MIPS/IRIX
    where DW_FRAME_CFA_COL is zero (in that case it's safe).

    It is also restricted to the case  where
    DW_REG_TABLE_SIZE == DW_FRAME_LAST_REG_NUM  ==
    dbg->de_frame_reg_rules_entry_count (true for MIPS/IRIX).
    If this condition is not met calling this routine can result in
    incorrect output or in memory corruption.

    It is much better to use dwarf_get_fde_info_for_all_regs3()
    instead of this interface.
*/
int
dwarf_get_fde_info_for_all_regs(Dwarf_Fde fde,
    Dwarf_Addr pc_requested,
    Dwarf_Regtable * reg_table,
    Dwarf_Addr * row_pc,
    Dwarf_Error * error)
{

    /* Table size: DW_REG_TABLE_SIZE */
    struct Dwarf_Frame_s fde_table;
    Dwarf_Sword i = 0;
    struct Dwarf_Reg_Rule_s *rule = NULL;
    struct Dwarf_Regtable_Entry_s *out_rule = NULL;
    int res = 0;
    Dwarf_Debug dbg = 0;

    /* For this interface the size is fixed at compile time. */
    int output_table_real_data_size = DW_REG_TABLE_SIZE;

    FDE_NULL_CHECKS_AND_SET_DBG(fde, dbg);

    res = dwarf_initialize_fde_table(dbg, &fde_table,
        output_table_real_data_size,
        error);
    if (res != DW_DLV_OK)
        return res;

    /* _dwarf_get_fde_info_for_a_pc_row will perform more sanity checks 
    */
    res = _dwarf_get_fde_info_for_a_pc_row(fde, pc_requested,
        &fde_table, dbg->de_frame_cfa_col_number, error);
    if (res != DW_DLV_OK) {
        dwarf_free_fde_table(&fde_table);
        return res;
    }

    out_rule = &reg_table->rules[0];
    rule = &fde_table.fr_reg[0];
    for (i = 0; i < output_table_real_data_size;
        i++, ++out_rule, ++rule) {
        out_rule->dw_offset_relevant = rule->ru_is_off;
        out_rule->dw_value_type = rule->ru_value_type;
        out_rule->dw_regnum = rule->ru_register;
        out_rule->dw_offset = rule->ru_offset_or_block_len;
    }
    dwarf_init_reg_rules_dw(&reg_table->rules[0],i,DW_REG_TABLE_SIZE,
        dbg->de_frame_undefined_value_number);

    /*  The test is just in case it's not inside the table. For non-MIPS 
        it could be outside the table and that is just fine, it was
        really a mistake to put it in the table in 1993.  */
    /* CONSTCOND */
    if (dbg->de_frame_cfa_col_number < DW_REG_TABLE_SIZE) {
        out_rule = &reg_table->rules[dbg->de_frame_cfa_col_number];
        out_rule->dw_offset_relevant = fde_table.fr_cfa_rule.ru_is_off;
        out_rule->dw_value_type = fde_table.fr_cfa_rule.ru_value_type;
        out_rule->dw_regnum = fde_table.fr_cfa_rule.ru_register;
        out_rule->dw_offset =
            fde_table.fr_cfa_rule.ru_offset_or_block_len;
    }

    if (row_pc != NULL)
        *row_pc = fde_table.fr_loc;
    dwarf_free_fde_table(&fde_table);
    return DW_DLV_OK;
}

/*  A consumer call for efficiently getting the register info
    for all registers in one call.

    The output table rules array is size output_table_real_data_size.
    (normally  DW_REG_TABLE_SIZE).
    The frame info  rules array in fde_table is normally of size
    DW_FRAME_LAST_REG_NUM.  */
int
dwarf_get_fde_info_for_all_regs3(Dwarf_Fde fde,
    Dwarf_Addr pc_requested,
    Dwarf_Regtable3 * reg_table,
    Dwarf_Addr * row_pc,
    Dwarf_Error * error)
{

    struct Dwarf_Frame_s fde_table;
    Dwarf_Sword i = 0;
    int res = 0;
    struct Dwarf_Reg_Rule_s *rule = NULL;
    struct Dwarf_Regtable_Entry3_s *out_rule = NULL;
    Dwarf_Debug dbg = 0;
    int output_table_real_data_size = reg_table->rt3_reg_table_size;

    FDE_NULL_CHECKS_AND_SET_DBG(fde, dbg);

    output_table_real_data_size =
        MIN(output_table_real_data_size,
            dbg->de_frame_reg_rules_entry_count);

    res = dwarf_initialize_fde_table(dbg, &fde_table,
        output_table_real_data_size,
        error);

    /* _dwarf_get_fde_info_for_a_pc_row will perform more sanity checks 
    */
    res = _dwarf_get_fde_info_for_a_pc_row(fde, pc_requested,
        &fde_table,
        dbg->de_frame_cfa_col_number,
        error);
    if (res != DW_DLV_OK) {
        dwarf_free_fde_table(&fde_table);
        return res;
    }

    out_rule = &reg_table->rt3_rules[0];
    rule = &fde_table.fr_reg[0];
    for (i = 0; i < output_table_real_data_size;
        i++, ++out_rule, ++rule) {
        out_rule->dw_offset_relevant = rule->ru_is_off;
        out_rule->dw_value_type = rule->ru_value_type;
        out_rule->dw_regnum = rule->ru_register;
        out_rule->dw_offset_or_block_len = rule->ru_offset_or_block_len;
        out_rule->dw_block_ptr = rule->ru_block;
    }
    dwarf_init_reg_rules_dw3(&reg_table->rt3_rules[0],i,reg_table->rt3_reg_table_size,
        dbg->de_frame_undefined_value_number);

    reg_table->rt3_cfa_rule.dw_offset_relevant =
        fde_table.fr_cfa_rule.ru_is_off;
    reg_table->rt3_cfa_rule.dw_value_type =
        fde_table.fr_cfa_rule.ru_value_type;
    reg_table->rt3_cfa_rule.dw_regnum =
        fde_table.fr_cfa_rule.ru_register;
    reg_table->rt3_cfa_rule.dw_offset_or_block_len =
        fde_table.fr_cfa_rule.ru_offset_or_block_len;
    reg_table->rt3_cfa_rule.dw_block_ptr =
        fde_table.fr_cfa_rule.ru_block;

    if (row_pc != NULL)
        *row_pc = fde_table.fr_loc;

    dwarf_free_fde_table(&fde_table);
    return DW_DLV_OK;
}


/*  Gets the register info for a single register at a given PC value
    for the FDE specified.
   
    This is the old MIPS interface and should no longer be used.
    Use dwarf_get_fde_info_for_reg3() instead.  */
int
dwarf_get_fde_info_for_reg(Dwarf_Fde fde,
    Dwarf_Half table_column,
    Dwarf_Addr pc_requested,
    Dwarf_Signed * offset_relevant,
    Dwarf_Signed * register_num,
    Dwarf_Signed * offset,
    Dwarf_Addr * row_pc, Dwarf_Error * error)
{
    struct Dwarf_Frame_s fde_table;
    int res = DW_DLV_ERROR;
    Dwarf_Debug dbg = 0;
    int output_table_real_data_size = 0;

    FDE_NULL_CHECKS_AND_SET_DBG(fde, dbg);
    output_table_real_data_size = dbg->de_frame_reg_rules_entry_count;

    res = dwarf_initialize_fde_table(dbg, &fde_table,
        output_table_real_data_size,
        error);
    if (res != DW_DLV_OK)
        return res;

    if (table_column >= output_table_real_data_size) {
        dwarf_free_fde_table(&fde_table);
        _dwarf_error(dbg, error, DW_DLE_FRAME_TABLE_COL_BAD);
        return (DW_DLV_ERROR);
    }

    /* _dwarf_get_fde_info_for_a_pc_row will perform more sanity checks 
    */
    res =
        _dwarf_get_fde_info_for_a_pc_row(fde, pc_requested, &fde_table,
            dbg->de_frame_cfa_col_number, error);
    if (res != DW_DLV_OK) {
        dwarf_free_fde_table(&fde_table);
        return res;
    }

    if (fde_table.fr_reg[table_column].ru_value_type != DW_EXPR_OFFSET) {
        /*  The problem here is that this interface cannot deal with
            other sorts of (newer) dwarf frame values.  Code must
            use dwarf_get_fde_info_for_reg3() to get these
            values correctly.  We error rather than return
            misleading incomplete data. */
        dwarf_free_fde_table(&fde_table);
        _dwarf_error(NULL, error,
            DW_DLE_FRAME_REGISTER_UNREPRESENTABLE);
        return (DW_DLV_ERROR);
    }
    if(table_column == dbg->de_frame_cfa_col_number) {
        if (register_num != NULL)
            *register_num = fde_table.fr_cfa_rule.ru_register;
        if (offset != NULL)
            *offset = fde_table.fr_cfa_rule.ru_offset_or_block_len;
        if (row_pc != NULL)
            *row_pc = fde_table.fr_loc;
        *offset_relevant = fde_table.fr_cfa_rule.ru_is_off;
       
    } else {
        if (register_num != NULL)
            *register_num = fde_table.fr_reg[table_column].ru_register;
        if (offset != NULL)
            *offset = fde_table.fr_reg[table_column].ru_offset_or_block_len;
        if (row_pc != NULL)
            *row_pc = fde_table.fr_loc;

        *offset_relevant = fde_table.fr_reg[table_column].ru_is_off;
    }
    dwarf_free_fde_table(&fde_table);
    return DW_DLV_OK;
}

/*  In this interface, table_column of DW_FRAME_CFA_COL
    is not meaningful.
    Use  dwarf_get_fde_info_for_cfa_reg3() to get the CFA.
    Call dwarf_set_frame_cfa_value() to set the correct column
    after calling dwarf_init()
    (DW_FRAME_CFA_COL3 is a sensible column to use).
*/
int
dwarf_get_fde_info_for_reg3(Dwarf_Fde fde,
    Dwarf_Half table_column,
    Dwarf_Addr pc_requested,
    Dwarf_Small * value_type,
    Dwarf_Signed * offset_relevant,
    Dwarf_Signed * register_num,
    Dwarf_Signed * offset_or_block_len,
    Dwarf_Ptr * block_ptr,
    Dwarf_Addr * row_pc_out,
    Dwarf_Error * error)
{
    struct Dwarf_Frame_s fde_table;
    int res = DW_DLV_ERROR;

    Dwarf_Debug dbg = 0;
    int table_real_data_size = 0;

    FDE_NULL_CHECKS_AND_SET_DBG(fde, dbg);
    table_real_data_size = dbg->de_frame_reg_rules_entry_count;
    res = dwarf_initialize_fde_table(dbg, &fde_table,
        table_real_data_size, error);
    if (res != DW_DLV_OK)
        return res;
    if (table_column >= table_real_data_size) {
        dwarf_free_fde_table(&fde_table);
        _dwarf_error(dbg, error, DW_DLE_FRAME_TABLE_COL_BAD);
        return (DW_DLV_ERROR);
    }

    /*  _dwarf_get_fde_info_for_a_pc_row will perform more sanity checks 
    */
    res = _dwarf_get_fde_info_for_a_pc_row(fde, pc_requested, &fde_table,
        dbg->de_frame_cfa_col_number,
        error);
    if (res != DW_DLV_OK) {
        dwarf_free_fde_table(&fde_table);
        return res;
    }

    if (register_num != NULL)
        *register_num = fde_table.fr_reg[table_column].ru_register;
    if (offset_or_block_len != NULL)
        *offset_or_block_len =
            fde_table.fr_reg[table_column].ru_offset_or_block_len;
    if (row_pc_out != NULL)
        *row_pc_out = fde_table.fr_loc;
    if (block_ptr)
        *block_ptr = fde_table.fr_reg[table_column].ru_block;

    /*  Without value_type the data cannot be understood, so we insist
        on it being present, we don't test it. */
    *value_type = fde_table.fr_reg[table_column].ru_value_type;
    *offset_relevant = (fde_table.fr_reg[table_column].ru_is_off);
    dwarf_free_fde_table(&fde_table);
    return DW_DLV_OK;

}

/*  For latest DWARF, this is the preferred interface.
    It more portably deals with the  CFA by not
    making the CFA a column number, which means
    DW_FRAME_CFA_COL3 becomes, like DW_CFA_SAME_VALUE,
    a special value, not something one uses as an index.

    Call dwarf_set_frame_cfa_value() to set the correct column
    after calling dwarf_init()
    (DW_FRAME_CFA_COL3 is a sensible column to use, and
    is the default unless '--enable-oldframecol' 
    is used to configure libdwarf).  */
int
dwarf_get_fde_info_for_cfa_reg3(Dwarf_Fde fde,
    Dwarf_Addr pc_requested,
    Dwarf_Small * value_type,
    Dwarf_Signed * offset_relevant,
    Dwarf_Signed * register_num,
    Dwarf_Signed * offset_or_block_len,
    Dwarf_Ptr * block_ptr,
    Dwarf_Addr * row_pc_out,
    Dwarf_Error * error)
{
    struct Dwarf_Frame_s fde_table;
    int res = DW_DLV_ERROR;
    Dwarf_Debug dbg = 0;

    int table_real_data_size = 0;

    FDE_NULL_CHECKS_AND_SET_DBG(fde, dbg);

    table_real_data_size = dbg->de_frame_reg_rules_entry_count;
    res = dwarf_initialize_fde_table(dbg, &fde_table,
        table_real_data_size, error);
    if (res != DW_DLV_OK)
        return res;
    res = _dwarf_get_fde_info_for_a_pc_row(fde, pc_requested, &fde_table,
        dbg->de_frame_cfa_col_number,error);
    if (res != DW_DLV_OK) {
        dwarf_free_fde_table(&fde_table);
        return res;
    }

    if (register_num != NULL)
        *register_num = fde_table.fr_cfa_rule.ru_register;
    if (offset_or_block_len != NULL)
        *offset_or_block_len =
            fde_table.fr_cfa_rule.ru_offset_or_block_len;
    if (row_pc_out != NULL)
        *row_pc_out = fde_table.fr_loc;
    if (block_ptr)
        *block_ptr = fde_table.fr_cfa_rule.ru_block;

    /*  Without value_type the data cannot be understood, so we insist
        on it being present, we don't test it. */
    *value_type = fde_table.fr_cfa_rule.ru_value_type;
    *offset_relevant = fde_table.fr_cfa_rule.ru_is_off;
    dwarf_free_fde_table(&fde_table);
    return DW_DLV_OK;
}



/*  Return pointer to the instructions in the dwarf fde.  */
int
dwarf_get_fde_instr_bytes(Dwarf_Fde inFde, Dwarf_Ptr * outinstraddr,
    Dwarf_Unsigned * outaddrlen,
    Dwarf_Error * error)
{
    Dwarf_Unsigned len = 0;
    unsigned char *instrs = 0;
    Dwarf_Debug dbg = 0;

    if (inFde == NULL) {
        _dwarf_error(dbg, error, DW_DLE_FDE_NULL);
        return (DW_DLV_ERROR);
    }

    dbg = inFde->fd_dbg;
    if (dbg == NULL) {
        _dwarf_error(dbg, error, DW_DLE_FDE_DBG_NULL);
        return (DW_DLV_ERROR);
    }

    instrs = inFde->fd_fde_instr_start;

    len = (inFde->fd_fde_start + inFde->fd_length +
        inFde->fd_length_size + inFde->fd_extension_size) - instrs;

    *outinstraddr = instrs;
    *outaddrlen = len;
    return DW_DLV_OK;
}

/*  Allows getting an fde from its table via an index.  
    With more error checking than simply indexing oneself.  */
int
dwarf_get_fde_n(Dwarf_Fde * fde_data,
    Dwarf_Unsigned fde_index,
    Dwarf_Fde * returned_fde, Dwarf_Error * error)
{
    Dwarf_Debug dbg = 0;
    Dwarf_Signed fdecount = 0;

    if (fde_data == NULL) {
        _dwarf_error(dbg, error, DW_DLE_FDE_PTR_NULL);
        return (DW_DLV_ERROR);
    }

    FDE_NULL_CHECKS_AND_SET_DBG(*fde_data, dbg);
    /* Assumes fde_data table has at least one entry. */
    fdecount = fde_data[0]->fd_is_eh? 
        dbg->de_fde_count_eh:dbg->de_fde_count;
    if (fde_index >= fdecount) {
        return (DW_DLV_NO_ENTRY);
    }
    *returned_fde = (*(fde_data + fde_index));
    return DW_DLV_OK;
}


/*  Lopc and hipc are extensions to the interface to 
    return the range of addresses that are described
    by the returned fde.  */
int
dwarf_get_fde_at_pc(Dwarf_Fde * fde_data,
    Dwarf_Addr pc_of_interest,
    Dwarf_Fde * returned_fde,
    Dwarf_Addr * lopc,
    Dwarf_Addr * hipc, Dwarf_Error * error)
{
    Dwarf_Debug dbg = NULL;
    Dwarf_Fde fde = NULL;
    Dwarf_Fde entryfde = NULL;
    Dwarf_Signed fdecount = 0;

    if (fde_data == NULL) {
        _dwarf_error(NULL, error, DW_DLE_FDE_PTR_NULL);
        return (DW_DLV_ERROR);
    }

    /*  Assumes fde_data table has at least one entry. */
    entryfde = *fde_data;
    FDE_NULL_CHECKS_AND_SET_DBG(entryfde, dbg);

    if (dbg == NULL) {
        _dwarf_error(NULL, error, DW_DLE_FDE_DBG_NULL);
        return (DW_DLV_ERROR);
    }
    fdecount = entryfde->fd_is_eh? 
        dbg->de_fde_count_eh:dbg->de_fde_count;
    {
        /*  The fdes are sorted by their addresses. Binary search to
            find correct fde. */
        Dwarf_Signed low = 0;
        Dwarf_Signed high = fdecount - 1L;
        Dwarf_Signed middle = 0;
        Dwarf_Fde cur_fde;

        while (low <= high) {
            middle = (low + high) / 2;
            cur_fde = fde_data[middle];
            if (pc_of_interest < cur_fde->fd_initial_location) {
                high = middle - 1;
            } else if (pc_of_interest >=
                (cur_fde->fd_initial_location +
                cur_fde->fd_address_range)) {
                low = middle + 1;
            } else {
                fde = fde_data[middle];
                break;
            }
        }
    }

    if (fde) {
        if (lopc != NULL)
            *lopc = fde->fd_initial_location;
        if (hipc != NULL)
            *hipc =
                fde->fd_initial_location + fde->fd_address_range - 1;
        *returned_fde = fde;
        return (DW_DLV_OK);
    }

    return (DW_DLV_NO_ENTRY);
}


/*  Expands a single frame instruction block
    from a specific cie
    into a n array of Dwarf_Frame_Op-s.
    This depends on having the cfa column set sensibly.

    Call dwarf_set_frame_cfa_value() to set the correct column
    after calling dwarf_init() unless you are using
    the old MIPS frame interfaces (in which case the default
    will be ok). (DW_FRAME_CFA_COL3 is a sensible column to use ).
*/
int
dwarf_expand_frame_instructions(Dwarf_Cie cie,
    Dwarf_Ptr instruction,
    Dwarf_Unsigned i_length,
    Dwarf_Frame_Op ** returned_op_list,
    Dwarf_Signed * returned_op_count,
    Dwarf_Error * error)
{
    Dwarf_Sword instr_count;
    int res = DW_DLV_ERROR;
    int dw_err;
    Dwarf_Debug dbg = 0;

    if (cie == 0) {
        _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
        return (DW_DLV_ERROR);
    }
    dbg = cie->ci_dbg;

    if (returned_op_list == 0 || returned_op_count == 0) {
        _dwarf_error(dbg, error, DW_DLE_RET_OP_LIST_NULL);
        return (DW_DLV_ERROR);
    }

    /*  The cast to Dwarf_Ptr may get a compiler warning, but it is safe 
        as it is just an i_length offset from 'instruction' itself. A
        caller has made a big mistake if the result is not a valid
        pointer. */
    res = _dwarf_exec_frame_instr( /* make_instr= */ true,
        returned_op_list,
        /* search_pc */ false,
        /* search_pc_val */ 0,
        /* location */ 0,
        instruction,
        (Dwarf_Ptr) ((Dwarf_Unsigned) instruction + i_length),
        /* Dwarf_Frame */ NULL,
        cie,
        dbg,
        dbg->de_frame_cfa_col_number, &instr_count,
        &dw_err);
    if (res != DW_DLV_OK) {
        if (res == DW_DLV_ERROR) {
            _dwarf_error(dbg, error, dw_err);
        }
        return (res);
    }

    *returned_op_count = instr_count;
    return DW_DLV_OK;
}


/*  Used by dwarfdump -v to print offsets, for debugging
    dwarf info.
    The dwarf_ version is preferred over the obsolete _dwarf version.
    _dwarf version kept for compatibility.
*/
/* ARGSUSED 4 */
int
_dwarf_fde_section_offset(Dwarf_Debug dbg, Dwarf_Fde in_fde,
    Dwarf_Off * fde_off, Dwarf_Off * cie_off,
    Dwarf_Error * err) 
{
    return dwarf_fde_section_offset(dbg,in_fde,fde_off,
        cie_off,err);
}
/* ARGSUSED 4 */
int
dwarf_fde_section_offset(Dwarf_Debug dbg, Dwarf_Fde in_fde,
    Dwarf_Off * fde_off, Dwarf_Off * cie_off,
    Dwarf_Error * err)
{
    char *start = 0;
    char *loc = 0;



    start = (char *) in_fde->fd_section_ptr;
    loc = (char *) in_fde->fd_fde_start;

    *fde_off = (loc - start);
    *cie_off = in_fde->fd_cie_offset;
    return DW_DLV_OK;
}

/* Used by dwarfdump -v to print offsets, for debugging
   dwarf info.
   The dwarf_ version is preferred over the obsolete _dwarf version.
   _dwarf version kept for compatibility.
*/
/* ARGSUSED 4 */
int
_dwarf_cie_section_offset(Dwarf_Debug dbg, Dwarf_Cie in_cie,
    Dwarf_Off * cie_off, Dwarf_Error * err)
{
    return dwarf_cie_section_offset(dbg,in_cie,cie_off,err);
}
/* ARGSUSED 4 */
int
dwarf_cie_section_offset(Dwarf_Debug dbg, Dwarf_Cie in_cie,
    Dwarf_Off * cie_off, Dwarf_Error * err)
{
    char *start = 0;
    char *loc = 0;

    start = (char *) in_cie->ci_section_ptr;
    loc = (char *) in_cie->ci_cie_start;

    *cie_off = (loc - start);
    return DW_DLV_OK;
}

/*  Returns  a pointer to target-specific augmentation data thru augdata
    and returns the length of the data thru augdata_len.

    It's up to the consumer code to know how to interpret the bytes
    of target-specific data (endian issues apply too, these
    are just raw bytes pointed to).
    See  Linux Standard Base Core Specification version 3.0 for
    the details on .eh_frame info.

    Returns DW_DLV_ERROR if fde is NULL or some other serious
    error.
    Returns DW_DLV_NO_ENTRY if there is no target-specific
    augmentation data. 

    The bytes pointed to are in the Dwarf_Cie, and as long as that
    is valid the bytes are there. No 'dealloc' call is needed
    for the bytes.  */
int
dwarf_get_cie_augmentation_data(Dwarf_Cie cie,
    Dwarf_Small ** augdata,
    Dwarf_Unsigned * augdata_len,
    Dwarf_Error * error)
{
    if (cie == NULL) {
        _dwarf_error(NULL, error, DW_DLE_CIE_NULL);
        return (DW_DLV_ERROR);
    }
    if (cie->ci_gnu_eh_augmentation_len == 0) {
        return DW_DLV_NO_ENTRY;
    }
    *augdata = (Dwarf_Small *) (cie->ci_gnu_eh_augmentation_bytes);
    *augdata_len = cie->ci_gnu_eh_augmentation_len;
    return DW_DLV_OK;
}


/*  Returns  a pointer to target-specific augmentation data thru augdata
    and returns the length of the data thru augdata_len.

    It's up to the consumer code to know how to interpret the bytes
    of target-specific data (endian issues apply too, these
    are just raw bytes pointed to).
    See  Linux Standard Base Core Specification version 3.0 for
    the details on .eh_frame info.

    Returns DW_DLV_ERROR if fde is NULL or some other serious
    error.
    Returns DW_DLV_NO_ENTRY if there is no target-specific
    augmentation data. 

    The bytes pointed to are in the Dwarf_Fde, and as long as that
    is valid the bytes are there. No 'dealloc' call is needed
    for the bytes.  */
int
dwarf_get_fde_augmentation_data(Dwarf_Fde fde,
    Dwarf_Small * *augdata,
    Dwarf_Unsigned * augdata_len,
    Dwarf_Error * error)
{
    Dwarf_Cie cie = 0;

    if (fde == NULL) {
        _dwarf_error(NULL, error, DW_DLE_FDE_NULL);
        return (DW_DLV_ERROR);
    }
    cie = fde->fd_cie;
    if (cie == NULL) {
        _dwarf_error(NULL, error, DW_DLE_CIE_NULL);
        return (DW_DLV_ERROR);
    }
    if (cie->ci_gnu_eh_augmentation_len == 0) {
        return DW_DLV_NO_ENTRY;
    }
    *augdata = (Dwarf_Small *) fde->fd_gnu_eh_augmentation_bytes;
    *augdata_len = fde->fd_gnu_eh_augmentation_len;
    return DW_DLV_OK;
}


#if 0
/* Used solely for debugging libdwarf. */
static void
dump_frame_rule(char *msg, struct Dwarf_Reg_Rule_s *reg_rule)
{
    printf
        ("%s type %s (0x%" DW_PR_XZEROS DW_PR_DUx 
        "), is_off %" DW_PR_DUu 
        " reg %" DW_PR_DUu " offset 0x%" DW_PR_XZEROS DW_PR_DUx 
        " blockp 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
        msg,
        (reg_rule->ru_value_type == DW_EXPR_OFFSET) ? 
            "DW_EXPR_OFFSET" : 
        (reg_rule->ru_value_type == DW_EXPR_VAL_OFFSET) ?
            "DW_EXPR_VAL_OFFSET" : 
        (reg_rule->ru_value_type == DW_EXPR_VAL_EXPRESSION) ?
            "DW_EXPR_VAL_EXPRESSION" : 
        (reg_rule->ru_value_type == DW_EXPR_EXPRESSION) ?
            "DW_EXPR_EXPRESSION" : "Unknown",
        (Dwarf_Unsigned) reg_rule->ru_value_type, 
        (Dwarf_Unsigned) reg_rule->ru_is_off,
        (Dwarf_Unsigned) reg_rule->ru_register,
        (Dwarf_Unsigned) reg_rule->ru_offset_or_block_len,
        (Dwarf_Unsigned) reg_rule->ru_block);
    return;
}
#endif

/*  This allows consumers to set the 'initial value' so that
    an ISA/ABI specific default can be used, dynamically,
    at run time.  Useful for dwarfdump and non-MIPS architectures.. 
    The value  defaults to one of
        DW_FRAME_SAME_VALUE or DW_FRAME_UNKNOWN_VALUE
    but dwarfdump can dump multiple ISA/ABI objects so
    we may want to get this set to what the ABI says is correct.

    Returns the value that was present before we changed it here.  */
Dwarf_Half
dwarf_set_frame_rule_initial_value(Dwarf_Debug dbg, Dwarf_Half value)
{
    Dwarf_Half orig = dbg->de_frame_rule_initial_value;
    dbg->de_frame_rule_initial_value = value;
    return orig;
}

/* The following spelling for backwards compatibility. */
Dwarf_Half
dwarf_set_frame_rule_inital_value(Dwarf_Debug dbg, Dwarf_Half value)
{
    return dwarf_set_frame_rule_initial_value(dbg,value);
}

/*  This allows consumers to set the array size of the  reg rules
    table so that
    an ISA/ABI specific value can be used, dynamically,
    at run time.  Useful for non-MIPS archtectures.
    The value  defaults  to DW_FRAME_LAST_REG_NUM.
    but dwarfdump can dump multiple ISA/ABI objects so
    consumers want to get this set to what the ABI says is correct.

    Returns the value that was present before we changed it here.
*/

Dwarf_Half
dwarf_set_frame_rule_table_size(Dwarf_Debug dbg, Dwarf_Half value)
{
    Dwarf_Half orig = dbg->de_frame_reg_rules_entry_count;
    dbg->de_frame_reg_rules_entry_count = value;

    /*  Take the caller-specified value, but do not
        let the value be too small. Keep it at least to
        DW_FRAME_LAST_REG_NUM.  
        This helps prevent libdwarf (mistakenly) indexing outside
        of of a register array when the ABI reg count is really small.  */
    if (value < DW_FRAME_LAST_REG_NUM) {
        dbg->de_frame_reg_rules_entry_count = DW_FRAME_LAST_REG_NUM;
    }
    return orig;
}
/*  This allows consumers to set the CFA register value
    so that an ISA/ABI specific value can be used, dynamically,
    at run time.  Useful for non-MIPS archtectures.
    The value  defaults  to DW_FRAME_CFA_COL3 and should be
    higher than any real register in the ABI. 
    Dwarfdump can dump multiple ISA/ABI objects so
    consumers want to get this set to what the ABI says is correct.
 
    Returns the value that was present before we changed it here.  */

Dwarf_Half
dwarf_set_frame_cfa_value(Dwarf_Debug dbg, Dwarf_Half value)
{
    Dwarf_Half orig = dbg->de_frame_cfa_col_number;
    dbg->de_frame_cfa_col_number = value;
    return orig;
}
/* Similar to above, but for the other crucial fields for frames. */
Dwarf_Half
dwarf_set_frame_same_value(Dwarf_Debug dbg, Dwarf_Half value)
{
    Dwarf_Half orig = dbg->de_frame_same_value_number;
    dbg->de_frame_same_value_number = value;
    return orig;
}
Dwarf_Half
dwarf_set_frame_undefined_value(Dwarf_Debug dbg, Dwarf_Half value)
{
    Dwarf_Half orig = dbg->de_frame_same_value_number;
    dbg->de_frame_undefined_value_number = value;
    return orig;
}

/*  Does something only if value passed in is greater than 0 and
    a size than we can handle (in number of bytes).  */
Dwarf_Small dwarf_set_default_address_size(Dwarf_Debug dbg,
    Dwarf_Small value  )
{
    Dwarf_Small orig = dbg->de_pointer_size;
    if (value > 0 && value <= sizeof(Dwarf_Addr)) {
        dbg->de_pointer_size = value;
    }
    return orig;
}

static int 
init_reg_rules_alloc(Dwarf_Debug dbg,struct Dwarf_Frame_s *f,
   unsigned count, Dwarf_Error * error)
{
    f->fr_reg_count = count;
    f->fr_reg = (struct Dwarf_Reg_Rule_s *)
        calloc(sizeof(struct Dwarf_Reg_Rule_s), count);
    if (f->fr_reg == 0) {
        if(error) {
            _dwarf_error(dbg, error, DW_DLE_DF_ALLOC_FAIL);
        }
        return (DW_DLV_ERROR);
    }
    dwarf_init_reg_rules_ru(f->fr_reg,0, count,
        dbg->de_frame_rule_initial_value);
    return DW_DLV_OK;
}
static int
dwarf_initialize_fde_table(Dwarf_Debug dbg,
    struct Dwarf_Frame_s *fde_table,
    unsigned table_real_data_size,
    Dwarf_Error * error)
{
    unsigned entry_size = sizeof(struct Dwarf_Frame_s);
    memset(fde_table,0,entry_size);
    fde_table->fr_loc = 0;
    fde_table->fr_next = 0;

    return init_reg_rules_alloc(dbg,fde_table,table_real_data_size,error);
}
static void
dwarf_free_fde_table(struct Dwarf_Frame_s *fde_table)
{
    free(fde_table->fr_reg);
    fde_table->fr_reg_count = 0;
    fde_table->fr_reg = 0;
}


/*  Return DW_DLV_OK if we succeed. else return DW_DLV_ERROR.
*/
int
_dwarf_frame_constructor(Dwarf_Debug dbg, void *frame)
{
    struct Dwarf_Frame_s *fp = frame;

    if (!dbg) {
        return DW_DLV_ERROR;
    }
    return init_reg_rules_alloc(dbg,fp,dbg->de_frame_reg_rules_entry_count, 0);
}

void
_dwarf_frame_destructor(void *frame)
{
    struct Dwarf_Frame_s *fp = frame;
    dwarf_free_fde_table(fp);
}

static void
dwarf_init_reg_rules_ru(struct Dwarf_Reg_Rule_s *base,
    unsigned first, unsigned last,int initial_value)
{
    struct Dwarf_Reg_Rule_s *r = base+first;
    unsigned i = first;
    for( ; i < last; ++i,++r) {
        r->ru_is_off = 0;
        r->ru_value_type = DW_EXPR_OFFSET;
        r->ru_register = initial_value;
        r->ru_offset_or_block_len = 0;
        r->ru_block = 0;
    }
}
static void
dwarf_init_reg_rules_dw(struct Dwarf_Regtable_Entry_s *base,
    unsigned first, unsigned last,int initial_value)
{
    struct Dwarf_Regtable_Entry_s *r = base+first;
    unsigned i = first;
    for( ; i < last; ++i,++r) {
        r->dw_offset_relevant = 0;
        r->dw_value_type = DW_EXPR_OFFSET;
        r->dw_regnum = initial_value;
        r->dw_offset = 0;
    }
}
static void
dwarf_init_reg_rules_dw3(struct Dwarf_Regtable_Entry3_s *base,
    unsigned first, unsigned last,int initial_value)
{
    struct Dwarf_Regtable_Entry3_s *r = base+first;
    unsigned i = first;
    for( ; i < last; ++i,++r) {
        r->dw_offset_relevant = 0;
        r->dw_value_type = DW_EXPR_OFFSET;
        r->dw_regnum = initial_value;
        r->dw_offset_or_block_len = 0;
        r->dw_block_ptr = 0;
    }
}