dwarf_frame.c 48.6 KB
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
/*
    dwarf_frame.c

    This file implements the API relating to frames
    contained in the debug_frame section.

    $Revision: 1.1.1.1 $ $Date: 2002/05/02 03:29:20 $
*/


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


/* 
    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.        
*/
static int
_dwarf_exec_frame_instr (
    Dwarf_Bool      make_instr,         /* Make list of frame instr?        */
    Dwarf_Frame_Op  **ret_frame_instr,  /* Ptr to list of ptrs to fr instrs */
    Dwarf_Bool      search_pc,          /* Search for a pc value?           */
    Dwarf_Addr      search_pc_val,      /* Search for this pc value         */
    Dwarf_Addr 	    loc,		/* initial location value 	    */
    Dwarf_Small     *start_instr_ptr,   /* Ptr to start of frame instrs.    */
    Dwarf_Small     *final_instr_ptr,   /* Ptr just past frame instrs.      */
    Dwarf_Frame     table,              /* Ptr to struct with last row.     */
    Dwarf_Cie       cie,                /* Ptr to Cie used by the Fde.      */
    Dwarf_Debug     dbg,                /* Associated Dwarf_Debug           */
    Dwarf_Sword     *returned_count,
    int     *returned_error
)
{
    /* Sweeps the frame instructions. */
    Dwarf_Small     *instr_ptr;

    /* Obvious from the documents. */
    Dwarf_Small     instr, opcode;
    Dwarf_Small     reg_no, reg_noA, reg_noB;
    Dwarf_Unsigned  factored_N_value;
    Dwarf_Addr      new_loc;
    Dwarf_Unsigned  adv_loc;

    struct Dwarf_Reg_Rule_s   	reg[DW_FRAME_LAST_REG_NUM];

    /* This is used to end executing frame instructions.    */
    /* Becomes true when search_pc is true and 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;

    /* 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;
    Dwarf_Half      fp_register;
    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;
    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.    */
    if (cie != NULL && cie->ci_initial_table != NULL) {
        for (i = 0; i < DW_FRAME_LAST_REG_NUM; i++)
            reg[i] = cie->ci_initial_table->fr_reg[i];
    }
    else {	/* initialize with same_value */
        for (i = 0; i < DW_FRAME_LAST_REG_NUM; i++) {
            reg[i].ru_is_off = 0; 
            reg[i].ru_register = DW_FRAME_SAME_VAL;
            reg[i].ru_offset = 0;
	}
    }

    /*
	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) {

        fp_instr_offset = instr_ptr - start_instr_ptr;
        instr = *(Dwarf_Small *)instr_ptr;
        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) {
			
			*returned_error = (DF_NO_CIE_AUGMENTATION);
			return DW_DLV_ERROR;
		}
                adv_pc = adv_pc * code_alignment_factor;

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

            case DW_CFA_offset : { /* base op */
                reg_no = (instr & DW_FRAME_INSTR_OFFSET_MASK);
                if (reg_no > DW_FRAME_LAST_REG_NUM){
			*returned_error = DF_REG_NUM_TOO_HIGH;
			return DW_DLV_ERROR;
		}

                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)  {
			*returned_error = (DF_NO_CIE_AUGMENTATION);
			return DW_DLV_ERROR;
		}

                reg[reg_no].ru_is_off = 1;
                reg[reg_no].ru_register = DW_FRAME_CFA_COL;
                reg[reg_no].ru_offset = factored_N_value * 
                    data_alignment_factor;

                break;
            }

            case DW_CFA_restore : { /* base op */
                reg_no = (instr & DW_FRAME_INSTR_OFFSET_MASK);
                if (reg_no > DW_FRAME_LAST_REG_NUM)  {
			*returned_error =  (DF_REG_NUM_TOO_HIGH);
			return DW_DLV_ERROR;
	        }

                fp_register = reg_no;

                if (cie != NULL && cie->ci_initial_table != NULL)
                    reg[reg_no] = cie->ci_initial_table->fr_reg[reg_no];
                else
                    if (!make_instr) {
			*returned_error = (DF_MAKE_INSTR_NO_INIT);
			return DW_DLV_ERROR;
		    }

                break;
            }
            case DW_CFA_set_loc : {
			READ_UNALIGNED(new_loc, instr_ptr, dbg->de_length_size);
                instr_ptr += dbg->de_length_size;
                if (new_loc <= loc) {
			*returned_error = (DF_NEW_LOC_LESS_OLD_LOC);
			return DW_DLV_ERROR;
		}

                search_over = search_pc && (new_loc > search_pc_val);

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

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

                if (need_augmentation) {
			*returned_error = (DF_NO_CIE_AUGMENTATION);
			return DW_DLV_ERROR;
		}
                adv_loc *= code_alignment_factor;

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

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

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

                if (need_augmentation) {
			*returned_error = (DF_NO_CIE_AUGMENTATION);
			return DW_DLV_ERROR;
                }
                adv_loc *= code_alignment_factor;

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

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

            case DW_CFA_advance_loc4 : {
			READ_UNALIGNED(adv_loc, instr_ptr, sizeof(Dwarf_Word));
                instr_ptr += sizeof(Dwarf_Word);
                fp_offset = adv_loc;

                if (need_augmentation) {
			*returned_error = (DF_NO_CIE_AUGMENTATION);
			return DW_DLV_ERROR;
		}
                adv_loc *= code_alignment_factor;

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

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

            case DW_CFA_offset_extended : {
		DECODE_LEB128_UWORD(instr_ptr, reg_no)
                if (reg_no > DW_FRAME_LAST_REG_NUM)  {
			*returned_error = (DF_REG_NUM_TOO_HIGH);
			return DW_DLV_ERROR;
	        }
                factored_N_value = 
                    _dwarf_decode_u_leb128(instr_ptr,&leb128_length);
                instr_ptr = instr_ptr + leb128_length;

                if (need_augmentation) {
			*returned_error = (DF_NO_CIE_AUGMENTATION);
			return DW_DLV_ERROR;
		}
                reg[reg_no].ru_is_off = 1;
                reg[reg_no].ru_register = DW_FRAME_CFA_COL;
                reg[reg_no].ru_offset = factored_N_value *
                    data_alignment_factor;

                fp_register = reg_no;
                fp_offset = factored_N_value;
                break;
            }

            case DW_CFA_restore_extended : {
		DECODE_LEB128_UWORD(instr_ptr, reg_no)
                if (reg_no > DW_FRAME_LAST_REG_NUM)  {
			*returned_error = (DF_REG_NUM_TOO_HIGH);
			return DW_DLV_ERROR;
	        }

                if (cie != NULL && cie->ci_initial_table != NULL) {
                    reg[reg_no] = 
                        cie->ci_initial_table->fr_reg[reg_no];
                } else {
                    if (!make_instr) {
			*returned_error = (DF_MAKE_INSTR_NO_INIT);
			return DW_DLV_ERROR;
		    }
		}

                fp_register = reg_no;
                break;
            }

            case DW_CFA_undefined : {
		DECODE_LEB128_UWORD(instr_ptr, reg_no)
                if (reg_no > DW_FRAME_LAST_REG_NUM)  {
			*returned_error = (DF_REG_NUM_TOO_HIGH);
			return DW_DLV_ERROR;
	        }

                reg[reg_no].ru_is_off = 0;
                reg[reg_no].ru_register = DW_FRAME_UNDEFINED_VAL;
                reg[reg_no].ru_offset = 0;

                fp_register = reg_no;
                break;
            }

            case DW_CFA_same_value : {
		DECODE_LEB128_UWORD(instr_ptr, reg_no)
                if (reg_no > DW_FRAME_LAST_REG_NUM)  {
			*returned_error = (DF_REG_NUM_TOO_HIGH);
			return DW_DLV_ERROR;
	        }

                reg[reg_no].ru_is_off = 0;
                reg[reg_no].ru_register = DW_FRAME_SAME_VAL;
                reg[reg_no].ru_offset = 0;
                fp_register = reg_no;
                break;
            }

            case DW_CFA_register : {
		DECODE_LEB128_UWORD(instr_ptr, reg_noA)
                if (reg_noA > DW_FRAME_LAST_REG_NUM)  {
			*returned_error = (DF_REG_NUM_TOO_HIGH);
			return DW_DLV_ERROR;
	        }
		DECODE_LEB128_UWORD(instr_ptr, reg_noB)
                if (reg_noB > DW_FRAME_LAST_REG_NUM)  {
			*returned_error = (DF_REG_NUM_TOO_HIGH);
			return DW_DLV_ERROR;
	        }

                reg[reg_noA].ru_is_off = 0;
                reg[reg_noA].ru_register = reg_noB;
                reg[reg_noA].ru_offset = 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) {
			*returned_error = (DF_ALLOC_FAIL);
			return DW_DLV_ERROR;
		}

                for (i = 0; i < DW_FRAME_LAST_REG_NUM; i++)
                    stack_table->fr_reg[i] = reg[i];

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

                break;
            }

            case DW_CFA_restore_state : {
                if (top_stack == NULL) {
			*returned_error = (DF_POP_EMPTY_STACK);
			return DW_DLV_ERROR;
		}
                stack_table = top_stack;
                top_stack = stack_table->fr_next;

                for (i = 0; i < DW_FRAME_LAST_REG_NUM; i++)
                    reg[i] = stack_table->fr_reg[i];

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

            case DW_CFA_def_cfa : {
		DECODE_LEB128_UWORD(instr_ptr, reg_no)
                if (reg_no > DW_FRAME_LAST_REG_NUM)  {
			return(DF_REG_NUM_TOO_HIGH);
	        }

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

                if (need_augmentation) {
			*returned_error =  (DF_NO_CIE_AUGMENTATION);
			return DW_DLV_ERROR;
		}
                reg[DW_FRAME_CFA_COL].ru_is_off = 1;
                reg[DW_FRAME_CFA_COL].ru_register = reg_no;
                reg[DW_FRAME_CFA_COL].ru_offset = factored_N_value;

                fp_register = reg_no;
                fp_offset = factored_N_value;
                break;
            }

            case DW_CFA_def_cfa_register : {
		DECODE_LEB128_UWORD(instr_ptr, reg_no)
                if (reg_no > DW_FRAME_LAST_REG_NUM)  {
			*returned_error = (DF_REG_NUM_TOO_HIGH);
			return DW_DLV_ERROR;
	        }

                reg[DW_FRAME_CFA_COL].ru_is_off = 0;
                reg[DW_FRAME_CFA_COL].ru_register = reg_no;
                reg[DW_FRAME_CFA_COL].ru_offset = 0;
                fp_register = reg_no;
                break;
            }

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

                if (need_augmentation) {
			*returned_error = (DF_NO_CIE_AUGMENTATION);
			return DW_DLV_ERROR;
		}
                reg[DW_FRAME_CFA_COL].ru_offset = factored_N_value;

                fp_offset = factored_N_value;
                break;
            }

            case DW_CFA_nop : {
                break;
                }
        }

        if (make_instr) {
            instr_count++;

            curr_instr = (Dwarf_Frame_Op *)
                _dwarf_get_alloc(dbg, DW_DLA_FRAME_OP, 1);
            if (curr_instr == NULL) {
			*returned_error = (DF_ALLOC_FAIL);
			return DW_DLV_ERROR;
	    }

            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) {
			*returned_error = (DF_ALLOC_FAIL);
			return DW_DLV_ERROR;
	    }

            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) {
		*returned_error = (DF_FRAME_DECODING_ERROR);
		return DW_DLV_ERROR;
    }

    /* Create the last row generated.   */
    if (table != NULL) {
        table->fr_loc = loc;
        for (i = 0; i < DW_FRAME_LAST_REG_NUM; i++)
            table->fr_reg[i] = reg[i];
    }

    /* 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) {
		*returned_error =  DF_ALLOC_FAIL;
		return DW_DLV_ERROR;
	}

        /* 
           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 = instr_count;
    } else {
        *returned_count = 0;
    }
    return DW_DLV_OK;
}


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
)
{
    /* Scans the debug_frame section. */
    Dwarf_Small     *frame_ptr;

    /* Points to the start of the current Fde or Cie. */
    Dwarf_Small     *start_frame_ptr;

    /* Fields for the current Cie being read. */
    Dwarf_Unsigned  length;		/* READ_UNALIGNED needs 8 byte dest */
    Dwarf_Unsigned  offset;
    Dwarf_Small     version;
    Dwarf_Small     *augmentation;
    Dwarf_Word      code_alignment_factor;
    Dwarf_Sword     data_alignment_factor;
    Dwarf_Small     return_address_register;

    /* 
       New_cie points to the Cie being read, and    
       head_cie_ptr and cur_cie_ptr are used  for   
       chaining them up in sequence.                
    */
    Dwarf_Cie       new_cie;
    Dwarf_Cie       head_cie_ptr = NULL;
    Dwarf_Cie       cur_cie_ptr;
    Dwarf_Word      cie_count = 0;

    /* 
       Points to a list of contiguous pointers  
       to Dwarf_Cie structures.                 
    */
    Dwarf_Cie       *cie_list_ptr;

    /* Fields for the current Fde being read.   */
    Dwarf_Addr      initial_location;
    Dwarf_Addr      address_range;

    /* 
       New_fde points to the current Fde being read,    
       and head_fde_ptr and cur_fde_ptr are used to     
       chain them up.                                   
    */
    Dwarf_Fde       new_fde;
    Dwarf_Fde       head_fde_ptr = NULL;
    Dwarf_Fde       cur_fde_ptr;
    Dwarf_Word	    fde_count = 0;

    /* 
       Points to a list of contiguous pointers to   
       Dwarf_Fde structures.                        
    */
    Dwarf_Fde       *fde_list_ptr;

    /* 
       Is used to check the offset field in the Fde 
       by checking for a Cie at this address.       
    */
    Dwarf_Small     *fde_cie_ptr;

    Dwarf_Word      leb128_length;
    Dwarf_Word      i, j;


    frame_ptr = dbg->de_debug_frame;

    if(frame_ptr == 0) {
	return DW_DLV_NO_ENTRY;
    }

    while (frame_ptr < dbg->de_debug_frame + dbg->de_debug_frame_size) {

        start_frame_ptr = frame_ptr;

	READ_UNALIGNED(length, frame_ptr, dbg->de_length_size);
        frame_ptr += dbg->de_length_size;
        if (length % dbg->de_length_size != 0) {
	    _dwarf_error(dbg,error,DW_DLE_DEBUG_FRAME_LENGTH_BAD); 
	    return(DW_DLV_ERROR);
	}

	READ_UNALIGNED(offset, frame_ptr, dbg->de_length_size);
	SIGN_EXTEND(offset, dbg->de_length_size);

        frame_ptr += dbg->de_length_size;
        if (offset == DW_CIE_ID) {

            version = *(Dwarf_Small *)frame_ptr;
            frame_ptr++;
            if (version != DW_CIE_VERSION) {
		_dwarf_error(dbg,error,DW_DLE_FRAME_VERSION_BAD);
		return(DW_DLV_ERROR);
	    }

            if (strcmp((char *)frame_ptr,DW_DEBUG_FRAME_AUGMENTER_STRING) != 0 &&
                strcmp((char *)frame_ptr,DW_EMPTY_STRING) != 0)
                frame_ptr = start_frame_ptr + length + 
		    dbg->de_length_size;
            else {
                augmentation = frame_ptr;
                frame_ptr = frame_ptr + strlen((char *)frame_ptr) + 1;

		DECODE_LEB128_UWORD(frame_ptr, code_alignment_factor)

                data_alignment_factor = 
                    _dwarf_decode_s_leb128(frame_ptr,&leb128_length);
                frame_ptr = frame_ptr + leb128_length;

                return_address_register = *(Dwarf_Small *)frame_ptr;
                if (return_address_register > DW_FRAME_LAST_REG_NUM) {
		    _dwarf_error(dbg,error,DW_DLE_CIE_RET_ADDR_REG_ERROR);
		    return(DW_DLV_ERROR);
		}
                frame_ptr++;
            }

            new_cie = (Dwarf_Cie)_dwarf_get_alloc(dbg, DW_DLA_CIE, 1);
            if (new_cie == NULL) {
		_dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
		return(DW_DLV_ERROR);
	    }

            new_cie->ci_length = length;
            new_cie->ci_augmentation = (char *)augmentation;

            new_cie->ci_data_alignment_factor = data_alignment_factor;
            new_cie->ci_code_alignment_factor = code_alignment_factor;
            new_cie->ci_return_address_register = return_address_register;
            new_cie->ci_cie_start = start_frame_ptr;
            new_cie->ci_cie_instr_start = frame_ptr;
            new_cie->ci_dbg = dbg;

            cie_count++;
            if (head_cie_ptr == NULL)
                head_cie_ptr = cur_cie_ptr = new_cie;
            else {
                cur_cie_ptr->ci_next = new_cie;
                cur_cie_ptr = new_cie;
            }
        } else {
	    Dwarf_Small *initloc = frame_ptr;

	    READ_UNALIGNED(initial_location, frame_ptr, dbg->de_length_size);
            frame_ptr += dbg->de_length_size;

	    READ_UNALIGNED(address_range, frame_ptr, dbg->de_length_size);
            frame_ptr += dbg->de_length_size;

            new_fde = (Dwarf_Fde)_dwarf_get_alloc(dbg, DW_DLA_FDE, 1);
            if (new_fde == NULL) {
		_dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
		return(DW_DLV_ERROR);
	    }

            new_fde->fd_length = length;
            new_fde->fd_cie_offset = offset;
            new_fde->fd_initial_location = initial_location;
            new_fde->fd_initial_loc_pos =  initloc;
            new_fde->fd_address_range = address_range;
            new_fde->fd_fde_start = start_frame_ptr;
            new_fde->fd_fde_instr_start = frame_ptr;
            new_fde->fd_dbg = dbg;

            fde_count++;
            if (head_fde_ptr == NULL)
                head_fde_ptr = cur_fde_ptr = new_fde;
            else {
                cur_fde_ptr->fd_next = new_fde;
                cur_fde_ptr = new_fde;
            }
        }

	    /* Skip over instructions to start of next frame. */
	frame_ptr = start_frame_ptr + length + dbg->de_length_size;
    }

    if (cie_count > 0) {
	cie_list_ptr = (Dwarf_Cie *)
	    _dwarf_get_alloc(dbg, DW_DLA_LIST, cie_count);
    }
    else {
	return (DW_DLV_NO_ENTRY);
    }
    if (cie_list_ptr == NULL) {
	_dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
	return(DW_DLV_ERROR);
    }
    /* Return arguments. */
    *cie_data = cie_list_ptr;
    *cie_element_count = cie_count;
    dbg->de_cie_data = cie_list_ptr;
    dbg->de_cie_count = cie_count;

    cur_cie_ptr = head_cie_ptr;
    for (i = 0; i < cie_count; i++) {
        *(cie_list_ptr + i) = cur_cie_ptr;
        cur_cie_ptr = cur_cie_ptr->ci_next;
    }

    if (fde_count > 0) {
	fde_list_ptr = (Dwarf_Fde *)
	    _dwarf_get_alloc(dbg, DW_DLA_LIST, fde_count);
    }
    else {
	return(DW_DLV_NO_ENTRY);
    }
    if (fde_list_ptr == NULL) {
	_dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
	return(DW_DLV_ERROR);
    }
    /* Return arguments. */
    *fde_data = fde_list_ptr;
    *fde_element_count = fde_count;
    dbg->de_fde_data = fde_list_ptr;
    dbg->de_fde_count = fde_count;

    cur_fde_ptr = head_fde_ptr;
    for (i = 0; i < fde_count; i++) {
        *(fde_list_ptr + i) = cur_fde_ptr;

        fde_cie_ptr = (Dwarf_Small *)(dbg->de_debug_frame + 
            cur_fde_ptr->fd_cie_offset);
        for (j = 0; j < cie_count; j++)
            if (((Dwarf_Cie)*(cie_list_ptr+j))->ci_cie_start == fde_cie_ptr) 
                break;
        if (j == cie_count) {
	    _dwarf_error(dbg,error,DW_DLE_NO_CIE_FOR_FDE);
	    return(DW_DLV_ERROR);
	}
        else {
            cur_fde_ptr->fd_cie_index = j;
            cur_fde_ptr->fd_cie = *(cie_list_ptr + j);
        }

        cur_fde_ptr = cur_fde_ptr->fd_next;
    }

    return(DW_DLV_OK);
}

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;
    Dwarf_Signed        signdval;
    Dwarf_Unsigned  	length;
    Dwarf_Signed 	offset;
    Dwarf_Addr 		initial_location;
    Dwarf_Addr 		address_range;
    Dwarf_Fde 		new_fde;
    unsigned char	*fde_ptr;
    unsigned char	*cie_ptr;
    unsigned char	*start_cie_ptr;
    Dwarf_Cie		new_cie;

    /* Fields for the current Cie being read. */
    Dwarf_Small     version;
    Dwarf_Small     *augmentation;
    Dwarf_Word      code_alignment_factor;
    Dwarf_Sword     data_alignment_factor;
    Dwarf_Small     return_address_register;
    int resattr;
    int sdatares;

    Dwarf_Word      leb128_length;

    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;
    }
    fde_offset = signdval;
    fde_ptr = (dbg->de_debug_frame+fde_offset);

    READ_UNALIGNED(length, fde_ptr, dbg->de_length_size);
    fde_ptr += dbg->de_length_size;

    if (length % dbg->de_length_size != 0) {
	_dwarf_error(dbg,error,DW_DLE_DEBUG_FRAME_LENGTH_BAD); 
	return( DW_DLV_ERROR);
    }

    READ_UNALIGNED(offset, fde_ptr, dbg->de_length_size);
    SIGN_EXTEND(offset, dbg->de_length_size);
    fde_ptr += dbg->de_length_size;

    READ_UNALIGNED(initial_location, fde_ptr, dbg->de_length_size);
    fde_ptr += dbg->de_length_size;

    READ_UNALIGNED(address_range, fde_ptr, dbg->de_length_size);
    fde_ptr += dbg->de_length_size;

    new_fde = (Dwarf_Fde)_dwarf_get_alloc(dbg, DW_DLA_FDE, 1);
    if (new_fde == NULL) {
	_dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
	return(DW_DLV_ERROR);
    }

    new_fde->fd_length = length;
    new_fde->fd_cie_offset = offset;
    new_fde->fd_initial_location = initial_location;
    new_fde->fd_address_range = address_range;
    new_fde->fd_fde_start = dbg->de_debug_frame+fde_offset;
    new_fde->fd_fde_instr_start = (Dwarf_Small *)fde_ptr;
    new_fde->fd_dbg = dbg;

    /* now read the cie corresponding to the fde */
    cie_ptr = (dbg->de_debug_frame+offset);
    start_cie_ptr = cie_ptr;

    READ_UNALIGNED(length, cie_ptr, dbg->de_length_size);
    cie_ptr += dbg->de_length_size;
    
    if (length % dbg->de_length_size != 0) {
	_dwarf_error(dbg,error,DW_DLE_DEBUG_FRAME_LENGTH_BAD); 
	return(DW_DLV_ERROR);
    }

    READ_UNALIGNED(offset, cie_ptr, dbg->de_length_size);
    SIGN_EXTEND(offset, dbg->de_length_size);
    cie_ptr += dbg->de_length_size;

    if (offset == DW_CIE_ID) {
        version = *(Dwarf_Small *)cie_ptr;
        cie_ptr++;
        if (version != DW_CIE_VERSION) {
	    _dwarf_error(dbg,error,DW_DLE_FRAME_VERSION_BAD);
	    return(DW_DLV_ERROR);
	}
        if (strcmp((char *)cie_ptr,DW_DEBUG_FRAME_AUGMENTER_STRING) != 0 &&
            strcmp((char *)cie_ptr,DW_EMPTY_STRING) != 0)
            cie_ptr = start_cie_ptr + length + 
		dbg->de_length_size;
        else {
            augmentation = (Dwarf_Small *)cie_ptr;
            cie_ptr = cie_ptr + strlen((char *)cie_ptr) + 1;

	    DECODE_LEB128_UWORD(cie_ptr, code_alignment_factor)

            data_alignment_factor = 
                _dwarf_decode_s_leb128(cie_ptr,&leb128_length);
            cie_ptr = cie_ptr + leb128_length;

            return_address_register = *(Dwarf_Small *)cie_ptr;
            if (return_address_register > DW_FRAME_LAST_REG_NUM) {
		_dwarf_error(dbg,error,DW_DLE_CIE_RET_ADDR_REG_ERROR);
	        return( DW_DLV_ERROR);
	    }
            cie_ptr++;
        }

        new_cie = (Dwarf_Cie)_dwarf_get_alloc(dbg, DW_DLA_CIE, 1);
        if (new_cie == NULL) {
	    _dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
	    return(DW_DLV_ERROR);
	}

	new_cie->ci_initial_table = NULL;
        new_cie->ci_length = length;
        new_cie->ci_augmentation = (char *)augmentation;
        new_cie->ci_data_alignment_factor = data_alignment_factor;
        new_cie->ci_code_alignment_factor = code_alignment_factor;
        new_cie->ci_return_address_register = return_address_register;
        new_cie->ci_cie_start = start_cie_ptr;
        new_cie->ci_cie_instr_start = cie_ptr;
        new_cie->ci_dbg = dbg;
    }
    else {
	_dwarf_error(dbg,error,DW_DLE_NO_CIE_FOR_FDE);
	return(DW_DLV_ERROR);
    }
    new_fde->fd_cie = new_cie;

    *ret_fde = new_fde;
    return DW_DLV_OK;
}


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

    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 - dbg->de_debug_frame;

    return DW_DLV_OK;
}


int
dwarf_get_cie_info (
    Dwarf_Cie       cie,
    Dwarf_Unsigned  *bytes_in_cie,
    Dwarf_Small     *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 (version != NULL)
        *version = DW_CIE_VERSION;
    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 + 
	    dbg->de_length_size - 
	    (cie->ci_cie_instr_start - cie->ci_cie_start);

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


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
)
{
    Dwarf_Debug             dbg;
    struct Dwarf_Frame_s    fde_table;
    Dwarf_Cie               cie;
    Dwarf_Sword		    i;
    int dw_err;
    Dwarf_Sword icount;
    int res;

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

    if (table_column > DW_FRAME_LAST_REG_NUM) {
	_dwarf_error(NULL,error,DW_DLE_FRAME_TABLE_COL_BAD);
	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);
	}
        for (i = 0; i < DW_FRAME_LAST_REG_NUM; i++) {
            cie->ci_initial_table->fr_reg[i].ru_is_off = 0; 
            cie->ci_initial_table->fr_reg[i].ru_register = DW_FRAME_SAME_VAL;
            cie->ci_initial_table->fr_reg[i].ru_offset = 0;
	}
        res = _dwarf_exec_frame_instr(/*make_instr=*/false, NULL, 
		false, NULL, 0,
            cie->ci_cie_instr_start, 
            cie->ci_cie_start + cie->ci_length + dbg->de_length_size,
            cie->ci_initial_table, cie, dbg,&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;
	}
    }

    res = _dwarf_exec_frame_instr (/*make_instr=*/false, NULL, true, 
	pc_requested, 
	fde->fd_initial_location, fde->fd_fde_instr_start, 
        fde->fd_fde_start + fde->fd_length + dbg->de_length_size,
        &fde_table, cie, dbg,&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;
    }
    
    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;
    if (row_pc != NULL) 
	*row_pc = fde_table.fr_loc;

    *offset_relevant =  (fde_table.fr_reg[table_column].ru_is_off);
    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;
    unsigned char * instrs;
    Dwarf_Debug             dbg;

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

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

    instrs = inFde->fd_fde_instr_start,

    len = (inFde->fd_fde_start + inFde->fd_length + dbg->de_length_size)
		- instrs;

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

int
dwarf_get_fde_n (
    Dwarf_Fde       *fde_data,
    Dwarf_Unsigned  fde_index,
    Dwarf_Fde      *returned_fde,
    Dwarf_Error     *error
)
{
    Dwarf_Debug     dbg;

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

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

    if (fde_index >= dbg->de_fde_count) {
        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;
    Dwarf_Sword     i;
    Dwarf_Fde       fde;

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

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

    for (i = 0; i < dbg->de_fde_count; i++) {
        fde = *(fde_data + i);
        if (pc_of_interest >= fde->fd_initial_location &&
            pc_of_interest < fde->fd_initial_location+fde->fd_address_range)
            break;
    }
    if (i < dbg->de_fde_count) {
	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);
}


int
dwarf_expand_frame_instructions (
    Dwarf_Debug         dbg,
    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;
    int dw_err;

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

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

    res = _dwarf_exec_frame_instr (/*make_instr=*/true, 
	returned_op_list,
        false, NULL, 0,
        instruction, (Dwarf_Ptr)((Dwarf_Unsigned)instruction + i_length),
        NULL, NULL, dbg,&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 rqs.  Returns DW_DLV_OK if returns the arrays.
	Returns DW_DLV_NO_ENTRY if no section. ?? (How do I tell?)
	Returns DW_DLV_ERROR if there is an error.

*/
int
_dwarf_frame_address_offsets(Dwarf_Debug dbg,Dwarf_Addr  **addrlist,
	Dwarf_Off  **offsetlist,Dwarf_Signed *returncount,Dwarf_Error *err)
{
  int retval = DW_DLV_OK;
  int res;
  Dwarf_Cie *cie_data;
  Dwarf_Signed cie_count;
  Dwarf_Fde *fde_data;
  Dwarf_Signed fde_count;
  Dwarf_Signed i;
  Dwarf_Frame_Op *frame_inst;
  Dwarf_Fde fdep;
  Dwarf_Cie ciep;
  Dwarf_Chain curr_chain = 0;
  Dwarf_Chain head_chain = 0;
  Dwarf_Chain prev_chain = 0;
  Dwarf_Arange arange;
  Dwarf_Unsigned arange_count = 0;
  Dwarf_Addr *arange_addrs = 0;
  Dwarf_Off  *arange_offsets = 0;

  res = dwarf_get_fde_list(dbg,&cie_data,&cie_count,
	&fde_data,&fde_count,err);
  if(res != DW_DLV_OK) {
	return res;
  }
  for(i = 0;  i < cie_count;  i++) {
    Dwarf_Off instoff =  0;
    Dwarf_Signed initial_instructions_length = 0;
    Dwarf_Small * instr_end = 0;
    Dwarf_Sword icount = 0;
    int j;
    int dw_err;
    ciep = cie_data[i];
    instoff = 
		ciep->ci_cie_instr_start - dbg->de_debug_frame;
    initial_instructions_length = ciep->ci_length +
            dbg->de_length_size -
            (ciep->ci_cie_instr_start - ciep->ci_cie_start);
    instr_end = ciep->ci_cie_instr_start +
		initial_instructions_length;
    res = _dwarf_exec_frame_instr(/*make_instr*/true,
		&frame_inst,
		/* search_pc=*/false,
		/*search_pc_val=*/0,
		/*location*/ 0,
		 ciep->ci_cie_instr_start,
		 instr_end,
		 /* Dwarf_frame=*/0,
		 /* cie=*/0,
		 dbg,&icount,&dw_err);
    if(res == DW_DLV_ERROR) {
          _dwarf_error(dbg,err,dw_err);
        return(res);
    } else if (res == DW_DLV_NO_ENTRY) {
	continue;
    }
		 
    for(j = 0; j < icount; ++j) {
	Dwarf_Frame_Op *finst = frame_inst + j;
	if(finst->fp_base_op == 0 &&
		finst->fp_extended_op == 1)  {
	  /* is DW_CFA_set_loc */
	  Dwarf_Addr add = (Dwarf_Addr)finst->fp_offset;
	  Dwarf_Off off = finst->fp_instr_offset + instoff;

                arange = (Dwarf_Arange)
                    _dwarf_get_alloc(dbg, DW_DLA_ARANGE, 1);
                if (arange == NULL) {
                    _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
                    return(DW_DLV_ERROR);
                }
                arange->ar_address = add;
                arange->ar_info_offset = off;
                arange_count++;
                curr_chain = (Dwarf_Chain)
                    _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
                if (curr_chain == NULL) {
                    _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
                    return(DW_DLV_ERROR);
                }
                curr_chain->ch_item = arange;
                if (head_chain == NULL)
                    head_chain = prev_chain = curr_chain;
                else {
                    prev_chain->ch_next = curr_chain;
                    prev_chain = curr_chain;
                }
	}
    }
    dwarf_dealloc(dbg,frame_inst,DW_DLA_FRAME_BLOCK);
    
  }
  for(i = 0;  i < fde_count; i++) {
     Dwarf_Small *instr_end = 0;
     Dwarf_Sword icount = 0;
     Dwarf_Signed instructions_length = 0;
     Dwarf_Off instoff = 0; 
     Dwarf_Off off = 0;
     Dwarf_Addr addr = 0;
     int j;
     int dw_err;

     fdep = fde_data[i];
     off = fdep->fd_initial_loc_pos - dbg->de_debug_frame;
     addr = fdep->fd_initial_location;
               arange = (Dwarf_Arange)
                    _dwarf_get_alloc(dbg, DW_DLA_ARANGE, 1);
                if (arange == NULL) {
                    _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
                    return(DW_DLV_ERROR);
                }
                arange->ar_address = addr;
                arange->ar_info_offset = off;
                arange_count++;
                curr_chain = (Dwarf_Chain)
                    _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
                if (curr_chain == NULL) {
                    _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
                    return(DW_DLV_ERROR);
                }
                curr_chain->ch_item = arange;
                if (head_chain == NULL)
                    head_chain = prev_chain = curr_chain;
                else {
                    prev_chain->ch_next = curr_chain;
                    prev_chain = curr_chain;
                }


     instoff = fdep->fd_fde_instr_start - dbg->de_debug_frame;
     instructions_length = fdep->fd_length +
            dbg->de_length_size -
            (fdep->fd_fde_instr_start - fdep->fd_fde_start);
     instr_end = fdep->fd_fde_instr_start +
		instructions_length;
     res = _dwarf_exec_frame_instr(/*make_instr*/true,
		&frame_inst,
		/*search_pc=*/false,
		/*search_pc_val=*/0,
		/*location*/ 0,
		 fdep->fd_fde_instr_start,
		 instr_end,
		 /* Dwarf_frame=*/0,
		 /* cie=*/0,
		 dbg,&icount,&dw_err);
    if(res  == DW_DLV_ERROR) {
        _dwarf_error(dbg,err,dw_err);
        return(res);
    } else if (res == DW_DLV_NO_ENTRY) {
	continue;
    }
		 
    for(j = 0; j < icount; ++j) {
	Dwarf_Frame_Op *finst2 = frame_inst +j;
	if(finst2->fp_base_op == 0 &&
		finst2->fp_extended_op == 1)  {
	  /* is DW_CFA_set_loc */
	  Dwarf_Addr add = (Dwarf_Addr)finst2->fp_offset;
	  Dwarf_Off off = finst2->fp_instr_offset + instoff;
               arange = (Dwarf_Arange)
                    _dwarf_get_alloc(dbg, DW_DLA_ARANGE, 1);
                if (arange == NULL) {
                    _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
                    return(DW_DLV_ERROR);
                }
                arange->ar_address = add;
                arange->ar_info_offset = off;
                arange_count++;
                curr_chain = (Dwarf_Chain)
                    _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
                if (curr_chain == NULL) {
                    _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
                    return(DW_DLV_ERROR);
                }
                curr_chain->ch_item = arange;
                if (head_chain == NULL)
                    head_chain = prev_chain = curr_chain;
                else {
                    prev_chain->ch_next = curr_chain;
                    prev_chain = curr_chain;
                }

	}
    }
    dwarf_dealloc(dbg,frame_inst,DW_DLA_FRAME_BLOCK);

  }
  dwarf_dealloc(dbg,fde_data,DW_DLA_LIST);
  dwarf_dealloc(dbg,cie_data,DW_DLA_LIST);
  arange_addrs = (Dwarf_Addr *)
        _dwarf_get_alloc(dbg, DW_DLA_ADDR, arange_count);
  if (arange_addrs == NULL) {
        _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
        return(DW_DLV_ERROR);
  }
  arange_offsets = (Dwarf_Off *)
        _dwarf_get_alloc(dbg, DW_DLA_ADDR, arange_count);
  if (arange_offsets == NULL) {
        _dwarf_error(dbg, err, DW_DLE_ALLOC_FAIL);
        return(DW_DLV_ERROR);
  }

  curr_chain = head_chain;
  for (i = 0; i < arange_count; i++) {
        Dwarf_Arange   ar = curr_chain->ch_item;
        arange_addrs[i] = ar->ar_address;
        arange_offsets[i] = ar->ar_info_offset;
        prev_chain = curr_chain;
        curr_chain = curr_chain->ch_next;
        dwarf_dealloc(dbg, ar, DW_DLA_ARANGE);
        dwarf_dealloc(dbg, prev_chain, DW_DLA_CHAIN);
  }
  *returncount = arange_count;
  *offsetlist = arange_offsets;
  *addrlist = arange_addrs;
  return retval;
}