summaryrefslogtreecommitdiff
path: root/other/burneye2/ia32/ia32-function.c
blob: f515c67e9d16cfd3b329f63f4bf83a0c1dd8cc27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
/* ia32-function.c - ia32 function abstraction layer
 *
 * by scut
 */

#include <asm/unistd.h>

#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include <common.h>
#include <ia32-debug.h>
#include <ia32-function.h>


/*** static prototypes
 */
static void
ia32_func_copy (ia32_function *target, ia32_function *source, char *name);

static ia32_function **
ia32_func_treeplain_fixreloc (ia32_function *func, elf_reloc_list *rel,
	ia32_function **flist, unsigned int flist_count,
	unsigned int *nfcount);

static ia32_bblock **
ia32_func_br_sp_list_2 (ia32_bblock **btl, unsigned int btl_len,
	ia32_function *func, unsigned int *brl_len,
	ia32_bblock *source, ia32_bblock *dest);

static ia32_bblock *
ia32_func_br_sp_list_3 (ia32_bblock **brl, unsigned int brl_len,
	unsigned int idx, int *sparr);

static void
ia32_func_br_sp_2 (ia32_bblock **brl, unsigned int brl_length,
	unsigned int brl_cur, int *patharray);

static int
ia32_func_breakup_2 (ia32_function *func, ia32_bblock *root,
	ia32_bblock *this, elf_reloc_list *rel_code, elf_reloc_list *rel_rodata,
	ia32_bblock **all, unsigned int all_count);

static void
ia32_graphviz_func_out_2 (FILE *fp, ia32_function **flist,
	unsigned int flist_count, int *flist_consider,
	elf_reloc_list *reloc_text);

static char *
ia32_graphviz_func_out_extern (FILE *fp, ia32_function *func,
	ia32_xref *xr, elf_reloc_list *reloc_text);

static void
ia32_graphviz_func_out_intern (FILE *fp, ia32_function *func, ia32_xref *xr,
	ia32_function **flist, unsigned int flist_count,
	int *flist_consider);

static int
ia32_func_switchtable_is (ia32_instruction *inst);

#ifdef	DUPE_DEBUG
void dupe_alloc (unsigned int max_addr);
void dupe_free (void);
void dupe_check (unsigned int addr);

/* global enable flag. can be used to selectivly drop certain sections
 */
int		dupe_check_enabled = 0;
unsigned int *	addr_arr = NULL;
unsigned int	addr_max = 0;
unsigned int	addr_ac = 1;	/* sequential access counter */
#endif

int	ia32_graphviz_align_undefined = 0;


extern char *	ia32_regs_wide[];

/*** implementation
 */

ia32_xref *
ia32_xref_new (void)
{
	return (xcalloc (1, sizeof (ia32_xref)));
}


ia32_function *
ia32_func_new (void)
{
	return (xcalloc (1, sizeof (ia32_function)));
}


ia32_xref *
ia32_func_xref_add (ia32_function *func, int totype,
	unsigned int at, unsigned int addend, unsigned int to,
	ia32_function *called, Elf32_Rel *rel, int inter_section)
{
	unsigned int	n;
	ia32_xref *	xref;


	ia32_debug (IA32_DEBUG, "ia32_func_xref_add: from 0x%08x to 0x%08x, "
		"type %d\n", at + addend, to, totype);
	/*if (to > 0x08000000)
		ia32_confirm ();*/

	for (n = 0 ; n < func->func_xref_count ; ++n) {
		if ((func->func_xref[n]->from + func->func_xref[n]->addend) ==
			(at + addend))
		{
			ia32_debug (IA32_WARNING,
				"ia32_func_xref_add: double xref at 0x%08x\n",
				to);
		}
	}

	xref = ia32_xref_new ();
	if (rel != NULL) {
		memcpy (&xref->orig, rel, sizeof (Elf32_Rel));
		xref->original_relocation = 1;
	}

	xref->from = at;
	xref->addend = addend;
	xref->to = to;
	xref->to_type = totype;
	xref->to_data = called;
	xref->inter_section = inter_section;

	func->func_xref_count += 1;
	func->func_xref = xrealloc (func->func_xref, func->func_xref_count *
		sizeof (ia32_xref *));
	func->func_xref[func->func_xref_count - 1] = xref;

	return (xref);
}


ia32_xref *
ia32_func_xref_findfrom (ia32_function *func, unsigned int vaddr_from)
{
	unsigned int	xrn;


	for (xrn = 0 ; xrn < func->func_xref_count ; ++xrn) {
		if (func->func_xref[xrn]->from == vaddr_from)
			return (func->func_xref[xrn]);
	}

	return (NULL);
}


unsigned int
ia32_func_xref_count (ia32_function **flist, unsigned int flist_count,
	int xref_type)
{
	unsigned int	fln,
			xrn,
			xref_count = 0;


	for (fln = 0 ; fln < flist_count ; ++fln) {
		for (xrn = 0 ; xrn < flist[fln]->func_xref_count ; ++xrn) {
			if (flist[fln]->func_xref[xrn]->to_type == xref_type)
				xref_count += 1;
		}
	}

	return (xref_count);
}


ia32_bblock *
ia32_func_find_bblock_byrelofs (ia32_function *func, unsigned int relofs)
{
	unsigned int	n;
	unsigned int	brall_count;
	ia32_bblock **	brall;
	ia32_bblock *	br;


	brall = ia32_br_get_all (func->br_root, &brall_count);

	for (n = 0 ; n < brall_count ; ++n) {
		if ((brall[n]->start - func->start) > relofs)
			continue;
		if ((brall[n]->end - func->start) <= relofs)
			continue;

		br = brall[n];
		free (brall);

		return (br);
	}

	free (brall);

	return (NULL);
}


void
ia32_func_list_walk (ia32_function **flist, unsigned int flist_count,
	void (* walk)(ia32_function *))
{
	unsigned int	n;


	for (n = 0 ; n < flist_count ; ++n)
		walk (flist[n]);

	return;
}


/* br_xref_fromfunc
 *
 * sort in all other relocations happening within function `func' into the
 * corresponding bblockes for easier/faster access on bblock level.
 *
 * return in any case
 */

void
ia32_func_oxref_fromfunc (ia32_function *func)
{
	unsigned int	n,
			dst;
	ia32_bblock *	brc;
	ia32_xref **	xrarr;
	ia32_xref *	xrcur;


	if (func->is_copy)
		return;

	/* sort in all other cross references into their corresponding bblockes
	 */
	for (n = 0 ; n < func->other_xref_count ; ++n) {
		assert (func->other_xref[n] != NULL);
		brc = ia32_func_find_bblock_byrelofs (func,
			func->other_xref[n]->from);

		if (brc == NULL) {
			ia32_debug (IA32_WARNING, "WARNING: "
				"(ia32_func_oxref_fromfunc) dangling "
				"relocation in %s:0x%05x, no bblock found\n",
				func->name, func->other_xref[n]->from);
			ia32_debug (IA32_WARNING, "  func: 0x%08x-0x%08x, "
				"no bblock found at 0x%08x\n",
				func->start, func->end,
				func->other_xref[n]->from + func->start);

			/*ia32_debug (IA32_DEBUG, "dumping bblock level\n");
			ia32_br_dump (func->name, func->br_root);
			ia32_debug (IA32_DEBUG, "end of dump\n");*/

			/* leave func->other_xref[n] to non-NULL, so it
			 * at least appears in compressed array below
			 */
			continue;
		}

		assert (brc != NULL);
		brc->other_xref_count += 1;
		brc->other_xref = xrealloc (brc->other_xref,
			brc->other_xref_count * sizeof (ia32_xref *));
		xrarr = brc->other_xref;

		xrcur = func->other_xref[n];
		xrcur->from = (func->start + xrcur->from) - brc->start;
		xrarr[brc->other_xref_count - 1] = xrcur;
		func->other_xref[n] = NULL;
	}

	/* compress now modified func->other_xref array
	 */
	for (dst = 0, n = 0 ; n < func->other_xref_count ; ++n) {
		if (func->other_xref[n] == NULL)
			continue;

		func->other_xref[dst] = func->other_xref[n];
		dst += 1;
	}

	func->other_xref_count = dst;

	/* FIXME: remove this pseudo-assertion once we've found this nasty bug.
	 */
	for (n = 0 ; n < func->other_xref_count ; ++n) {
		assert (func->other_xref[n] != NULL);
	}

	func->other_xref = xrealloc (func->other_xref, func->other_xref_count *
		sizeof (ia32_xref *));

	return;
}


unsigned char *
ia32_func_v2real (ia32_function *func, unsigned int vaddr)
{
	/* kludge: some libraries (e.g. dietlibc) have functions written in
	 *         assembly, that do not have a proper size description and
	 * have their end set to the start address (i.e. zero sized). to work
	 * around this problem, we explicitly check here, and skip all sanity
	 * checking.
	 */
	if (func->start == func->end)
		return (&func->mem[vaddr - func->start]);

	assert (vaddr >= func->start && vaddr < func->end);

	return (&func->mem[vaddr - func->start]);
}


unsigned int
ia32_func_r2virt (ia32_function *func, unsigned int real)
{
	unsigned char *	rptr = (unsigned char *) real;

	/* kludge: see ia32_func_v2real for description
	 */
	if (func->start == func->end)
		return (rptr - func->mem);

	assert (rptr >= func->mem &&
		rptr < (func->mem + (func->end - func->start)));

	return (rptr - func->mem);
}


ia32_function *
ia32_func_findcopy (ia32_function *func,
	ia32_function **flist, unsigned int flist_count)
{
	unsigned int	n;


	ia32_debug (IA32_DEBUG, "DEBUG: (ia32_func_findcopy) checking dupe "
		"between (0x%x-0x%x): %s\n",
		func->start, func->end, func->name);

	for (n = 0 ; n < flist_count ; ++n) {
		if (flist[n]->passed == 0 || flist[n] == func)
			continue;

		if (flist[n]->section_idx != func->section_idx)
			continue;

		if (flist[n]->start == func->start)
			return (flist[n]);

		/* there are situations when two functions are nested, so that
		 * they share the same trailing code. this happens for manually
		 * optimized functions, such as __syscall_error and
		 * __syscall_error_1 within the GNU C library. since they are
		 * exactly the same functions when they have the same starting
		 * address, in case they are nested, it must be a different
		 * address. as we sort the function list before any parsing
		 * operation, we can ensure we have already processed the outer
		 * function.
		 *
		 * the same odd case but reversed: overlapping but not enclosed
		 * functions.
		 *
		 * example: siglongjmp and longjmp within glibc, which both
		 *     have a damaged symbol table entry, something like this:
		 *
		 *    siglongjmp  [0x644b - 0x6491]
		 *    longjmp           [0x6467 - 0x64ad]
		 *
		 * what we do here is that we do not mark the function, but
		 * clear the array of accesses for the overlapping range.
		 * crude solution to an even more crude problem.
		 */
		if ((flist[n]->start > func->start && flist[n]->start < func->end) ||
			(flist[n]->end > func->start && flist[n]->end < func->end) ||
			(flist[n]->start < func->start && flist[n]->end >= func->end))
#if 0
		if ((flist[n]->start < func->start && flist[n]->end >= func->end) ||
			(flist[n]->start < func->start &&
			flist[n]->end <= func->end))
#endif
		{
			func->is_nested = 1;
			ia32_debug (IA32_WARNING, "WARNING: %s is nested\n",
				func->name);

			return (flist[n]);
		}
	}

	return (NULL);
}


/* FIXME: do a deep copy instead of shallowing things
 */
static void
ia32_func_copy (ia32_function *target, ia32_function *source, char *name)
{
	/* since we do not care about memory leaks anyway, just do a shallow
	 * copy and then replace the name
	 */
	memcpy (target, source, sizeof (ia32_function));
	target->name = name;
	target->is_copy = 1;

	return;
}


void
ia32_func_treeplain (ia32_function ***flist, unsigned int *flist_count,
	elf_reloc_list *rel_code, elf_reloc_list *rel_rodata,
	unsigned int code_idx)
{
	unsigned int	n;
	ia32_function *	func;

	ia32_function **	fnl;
	ia32_function *		fcopy;	/* symbolic copy of function */
	unsigned int		fnl_count;


	elf_function_list_sort (*flist, *flist_count);

	/* collect all bblockes of the function
	 */
reparse:
	while ((func = ia32_func_get_unpassed (*flist, *flist_count,
		code_idx)) != NULL)
	{
		ia32_debug (IA32_INFO, "INFO: (ia32_func_treeplain) "
			"breaking up: %s (0x%x-0x%x)\n", func->name,
			func->start, func->end);

		/* try to locate a symbolic copy. for example, in glibc, the
		 * __srandom function is aliased as srand, too. so, only
		 * decode one, and make the other function an alias. note that
		 * we cannot just drop one, because a relocation might refer
		 * to it using its symbol table entry
		 */
		fcopy = ia32_func_findcopy (func, *flist, *flist_count);
		if (fcopy != NULL && func->is_nested == 0) {
			ia32_func_copy (func, fcopy, func->name);
			func->passed = 1;
			ia32_debug (IA32_INFO, "INFO: (ia32_func_treeplain) "
				"\"%s\" is an alias of \"%s\"\n",
				func->name, fcopy->name);

			continue;
		} else if (fcopy != NULL && func->is_nested == 1) {
			ia32_debug (IA32_WARNING, "WARNING: "
				"(ia32_func_treeplain) %s is nested within "
				"%s, address checks disabled\n",
				func->name, fcopy->name);
		}

		func->br_root = ia32_func_breakup (func, rel_code, rel_rodata);
		ia32_debug (IA32_DEBUG, "\t=> %-10u xrefs\n",
			func->func_xref_count);
		func->passed = 1;
	}

	/* fix cross references
	 */
	for (n = 0 ; n < *flist_count ; ++n) {
		/* FIXME:SEC */
		if ((*flist)[n]->section_idx != code_idx)
			continue;

		if ((*flist)[n]->is_copy)
			continue;

		fnl = ia32_func_treeplain_fixreloc ((*flist)[n], rel_code,
			*flist, *flist_count, &fnl_count);

		if (fnl_count == 0)
			continue;

		*flist = xrealloc (*flist, (*flist_count + fnl_count) *
			sizeof (ia32_function *));
		memcpy (&((*flist)[*flist_count]), fnl,
			fnl_count * sizeof (ia32_function *));
		*flist_count += fnl_count;
		free (fnl);

		elf_function_list_sort (*flist, *flist_count);
		ia32_debug (IA32_INFO, "WARNING: (ia32_func_treeplain) "
			"reparsing functions\n");

		goto reparse;
	}

	return;
}


static ia32_function **
ia32_func_treeplain_fixreloc (ia32_function *func, elf_reloc_list *rel,
	ia32_function **flist, unsigned int flist_count,
	unsigned int *nfcount)
{
	unsigned int	xc,
			dst,
			rn;
	ia32_xref *	xref;
	elf_reloc *	reloc;
	unsigned int	rfunc_offset;	/* relative to function offset */

	ia32_function *		fnew;
	ia32_function **	new_flist = NULL;
	unsigned int		new_flist_count = 0,
				fwn;
	int			need_compress = 0;


	assert (func->is_copy == 0);

	for (xc = 0 ; xc < func->func_xref_count ; ++xc) {
		ia32_function *	coverfunc;


		xref = func->func_xref[xc];

		/* in case its refering an external function we cannot provide
		 * the ia32_function pointer, so set it to NULL, humm...
		 */
		if (xref->to_type == IA32_XREF_FUNCEXTERN) {
			ia32_debug (IA32_INFO, "extern function reference from "
				"0x%08x (%s)\n", xref->from, func->name);

			xref->to_data = NULL;

			continue;
		} else if (xref->to_type == IA32_XREF_FUNCTION) {
			/* FIXME:SEC */
			xref->to_data = ia32_func_list_find_bystart (flist,
				flist_count, xref->to);
		} else {
			assert (0);
		}

		if (xref->to_data != NULL)
			continue;

		/* heuristics: when the reference is to within the function it
		 *             is contained in, then we have some complicated
		 * redirection construct, most likely a complicated
		 * switchtable. see _IO_vfprintf code within glibc for an
		 * example:
		 *     0x0001bbc8 b827cd0100     mov     eax, 0x1cd27
		 *     0x0001bbcd e9a71b0000     jmp     0x1d779
		 *     ...
		 *     0x0001d779 ffe0           jmp     eax
		 *
		 * at the time we see the mov, we cannot be sure it is a
		 * function pointer or a in-function address. this is because
		 * the function could be processed in tracing mode, without
		 * known function boundaries. so step back at that time, and
		 * process such references now, after the boundaries are known.
		 *
		 * so, first test whether the reference is to within the own
		 * function. if it is, there are two cross references, added
		 * from ia32_func_breakup_2, one function xref, and one other
		 * xref. we need to remove the function cross reference, but
		 * conserve the other cross reference, but marking it as code
		 * related crossreference.
		 */
		if (ia32_trace_range (func->start, func->end, xref->to)) {
			ia32_xref *	oxf;
			ia32_xref *	carry_tmp;	/* temp. holder */

			ia32_debug (IA32_INFO, "INFO: internal function "
				"reference from %s to %s (0x%x)\n",
				func->name, func->name, xref->to);

			/* remove function crossreference. do the free later,
			 * as 'xref' still references to it.
			 */
			carry_tmp = func->func_xref[xc];
			func->func_xref[xc] = NULL;
			need_compress = 1;

			/* find the related other crossreference, and mark it
			 * as code related. note that other crossreference do
			 * not have addends, function xrefs do. so sum both.
			 */
			oxf = ia32_func_oxref_findfrom (func,
				xref->from + xref->addend);
			assert (oxf != NULL);
			oxf->other_subtype = IA32_XREF_O_FUNCTIONINTERN;

			free (carry_tmp);

			continue;
		}

		/* when the function is within another section, skip over the
		 * hidden function detection.
		 */
		if (xref->inter_section)
			continue;

		/* heuristics: some static functions used only locally within
		 *             an ELF object (eg. getopterror within getopt.c
		 * of dietlibc) have no symbol table entry at all. our idea to
		 * catch those functions also is to trace the calltree for
		 * calls to areas not covered by any function in the list.
		 * this is exactly the case we now check for.
		 */
		coverfunc = ia32_func_list_is_covered (xref->to,
			flist, flist_count);
		if (coverfunc != NULL) {
			ia32_debug (IA32_FATAL, "FATAL: "
				"(ia32_func_treeplain_fixreloc) function "
				"reference from %s:0x%x (0x%08x-0x%08x) "
				"to 0x%08x is covered/misaligned by another "
				"function: %s (0x%08x-0x%08x)\n",
				func->name, xref->from,
				func->start, func->end, xref->to,
				coverfunc->name, coverfunc->start,
				coverfunc->end);

			/*continue;*/
			exit (EXIT_FAILURE);
			/* FIXME: this should not happen, exit (EXIT_FAILURE);
			 */
		}

		ia32_debug (IA32_WARNING, "WARNING: "
			"(ia32_func_treeplain_fixreloc) hidden function "
			"detected due to %s:0x%x, entry point: 0x%08x\n",
			func->name, xref->from, xref->to);

		/* avoid duplicate functions within the new ones
		 */
		for (fwn = 0 ; fwn < new_flist_count ; ++fwn)
			if (new_flist[fwn]->start == xref->to)
				break;

		if (fwn < new_flist_count)
			continue;

		/* lets setup a new function definition for the hidden function
		 * FIXME: the fnew->mem stuff is rather ugly ;), as we assume
		 *        the function memory is all continuous
		 */
		fnew = ia32_func_new ();
		xref->to_data = fnew;	/* patch cross reference properly */
		fnew->name = "";
		fnew->start = fnew->end = xref->to;
		fnew->mem = func->mem - func->start + fnew->start;
		/* TODO: remove this once bugs are squashed */
		assert (fnew->mem >= (unsigned char *) 0x08000000 &&
			fnew->mem <= (unsigned char *) 0x50000000);

		fnew->section_idx = func->section_idx;

		new_flist_count += 1;
		new_flist = xrealloc (new_flist,
			new_flist_count * sizeof (ia32_function *));
		new_flist[new_flist_count - 1] = fnew;

		ia32_debug (IA32_DEBUG, "  ==> scheduled for the tracer\n");
	}

	/* compress func_xref array by removing any NULL entries
	 */
	if (need_compress) {
		for (dst = 0, xc = 0 ; xc < func->func_xref_count ; ++xc) {
			if (func->func_xref[xc] == NULL)
				continue;

			func->func_xref[dst] = func->func_xref[xc];
			dst += 1;
		}

		func->func_xref_count = dst;
		func->func_xref = xrealloc (func->func_xref,
			func->func_xref_count * sizeof (func->func_xref[0]));

		assert (func->func_xref_count == 0 || func->func_xref != NULL);
	}

#if 0
	/* OLD, obsolete version. remove soon when other proved correct.
	 * the new version is order-preserving and simpler and easier to
	 * understand
	 */
	if (need_compress) {
		for (xc = 0 ; xc < func->func_xref_count ; ++xc) {
			if (func->func_xref[xc] != NULL)
				continue;

			/* as its unsorted, take it the easy way
			 */
			func->func_xref[xc] =
				func->func_xref[func->func_xref_count - 1];
			func->func_xref_count -= 1;
			--xc;
		}

		func->func_xref = realloc (func->func_xref,
			func->func_xref_count * sizeof (func->func_xref[0]));

		assert (func->func_xref_count == 0 || func->func_xref != NULL);
	}
#endif

	/* now add the non-directly-control flow function cross references.
	 * for example code like this:
	 *      push   function
	 *      call   otherfunc
	 * then a reloc will be emitted by the linker that will insert the
	 * address of function at the correct place. since we do overlays of
	 * basic blocks, we have to insert the address in runtime and hence
	 * need this info.
	 */
	for (xc = 0 ; xc < rel->reloc_count ; ++xc) {
		reloc = rel->reloc[xc];

		if (reloc->offset_rel < func->start ||
			reloc->offset_rel >= func->end)
			continue;

		/* the crossreferences always refer relative to the function
		 * start
		 */
		rfunc_offset = reloc->offset_rel - func->start;

		/* check whether we already have this xref in the function
		 * references
		 */
		for (rn = 0 ; rn < func->func_xref_count ; ++rn) {
			if ((func->func_xref[rn]->from +
				func->func_xref[rn]->addend) == rfunc_offset)
			{
				break;
			}
		}

		if (rn < func->func_xref_count)
			continue;

		/* now we've found a relocation that is happening within the
		 * function body. we add it, since it is currently missing (it
		 * was not mentioned by any control flow instruction from
		 * within the functions code)
		 */
		ia32_debug (IA32_DEBUG, "DEBUG: _oxref_add "
			"(\"%s\", 0x%08x, 0x%08x)\n", func->name,
			rfunc_offset, (unsigned int) reloc);
		ia32_func_oxref_add (func, rfunc_offset, reloc);
	}

	*nfcount = new_flist_count;

	return (new_flist);
}


ia32_function *
ia32_func_list_is_covered (unsigned int vaddr, ia32_function **flist,
	unsigned int flist_count)
{
	unsigned int	n;


	for (n = 0 ; n < flist_count ; ++n) {
		if (flist[n]->is_copy)
			continue;

		if (vaddr >= flist[n]->start && vaddr < flist[n]->end)
			return (flist[n]);
	}

	return (NULL);
}


/* duplicate code, doh!
 */
ia32_xref *
ia32_func_oxref_findfrom (ia32_function *func, unsigned int vaddr_from)
{
	unsigned int	xrn;


	for (xrn = 0 ; xrn < func->other_xref_count ; ++xrn) {
		if (func->other_xref[xrn]->from == vaddr_from)
			return (func->other_xref[xrn]);
	}

	return (NULL);
}


void
ia32_func_oxref_add (ia32_function *func, unsigned int relofs, elf_reloc *rel)
{
	ia32_xref *	xref = ia32_xref_new ();
	unsigned int	wn;


	/* check for duplicates. this is necessary because _oxref_add may be
	 * called multiple times on the same relocation entry at multiple runs
	 * of ia32_func_treeplain_fixreloc.
	 */
	for (wn = 0 ; wn < func->other_xref_count ; ++wn) {
		if (func->other_xref[wn]->from == relofs) {
			ia32_debug (IA32_DEBUG, "DEBUG: equal (%d): in %s: "
				"0x%08x\n", wn, func->name, relofs);

			return;
		}
	}

	memcpy (&xref->orig, &rel->orig, sizeof (Elf32_Rel));

	/* no addend for other cross references
	 */
	xref->from = relofs;
	xref->addend = 0;
	xref->to_type = IA32_XREF_OTHER;
	xref->to_data = (void *) rel;
	assert (rel != NULL);
	xref->rel_addend = rel->addend;

	/* we once had the addend directly read out of memory. nowadays, we
	 * copy it from a once-read rel structure, as the original code may be
	 * properly relocated already. old behaviour was:
	 *
	 * xref->rel_addend = *((unsigned int *) &func->mem[relofs]);
	 */

	func->other_xref_count += 1;
	func->other_xref = xrealloc (func->other_xref, func->other_xref_count *
		sizeof (ia32_xref *));
	func->other_xref[func->other_xref_count - 1] = xref;

	return;
}


ia32_function *
ia32_func_get_unpassed (ia32_function **flist, unsigned int flist_count,
	unsigned int sidx)
{
	unsigned int	n;


	for (n = 0 ; n < flist_count ; ++n)
		if ((sidx == 0 || flist[n]->section_idx == sidx) &&
			flist[n]->passed == 0)
		{
			return (flist[n]);
		}

	return (NULL);
}


ia32_bblock *
ia32_func_br_end (ia32_function *func)
{
	int		n;
	ia32_bblock *	br_end;
	ia32_bblock **	brl;


	brl = ia32_br_get_all (func->br_root, (unsigned int *) &n);

	for (n = n - 1 ; n >= 0 ; --n) {
		if (brl[n]->endtype == BR_END_RET ||
			brl[n]->endtype == BR_END_CTRL_SYSCALL_END)
		{
			break;
		}
	}

	assert (n >= 0);
	br_end = brl[n];
	free (brl);

	return (br_end);
}


ia32_bblock **
ia32_func_br_mustexec (ia32_function *func, unsigned int *br_count)
{
	unsigned int	n,
			bn;
	ia32_bblock *	br_return;

	ia32_bblock **	br_all;
	unsigned int	br_all_count;

	ia32_bblock **	brl_sp;
	unsigned int	brl_sp_count;

	ia32_bblock **	brl_must = NULL;
	unsigned int	brl_must_count = 0;


	/* 1. bblockes that must be executed on every functions run have to lie
	 * on the shortest path
	 */
	br_return = ia32_func_br_end (func);
	brl_sp = ia32_func_br_sp_list (func, &brl_sp_count,
		func->br_root, br_return);
	assert (brl_sp_count > 0);

	/* the startnode must lie in the list for sure
	 */
	brl_must_count += 1;
	brl_must = xrealloc (brl_must, brl_must_count * sizeof (ia32_bblock));
	brl_must[brl_must_count - 1] = func->br_root;

	/* 2. for the nodes that lie on the shortest path - except the start
	 * and end node, there must be no path around them, else they do not
	 * belong to the must-exec nodes
	 */
	br_all = ia32_br_get_all (func->br_root, &br_all_count);

	for (n = 0 ; n < brl_sp_count ; ++n) {
		ia32_bblock *	br_temp;
		ia32_bblock **	sp_temp;


		/* ignore start and end, they must be executed anytime
		 */
		if (brl_sp[n] == func->br_root || brl_sp[n] == br_return)
			continue;

		/* temporarily remove bblock from the "all bblockes" list,
		 * then check if there is a path now, with the bblock removed
		 */
		for (bn = 0 ; bn < br_all_count ; ++bn)
			if (brl_sp[n] == br_all[bn])
				break;
		assert (bn < br_all_count);

		br_temp = br_all[bn];
/*		assert ((br_all_count - bn - 1) > 0);*/

		br_all_count -= 1;
		memmove (&br_all[bn], &br_all[bn + 1], (br_all_count - n) *
			sizeof (ia32_bblock *));

		/* check whether there is a path
		 */
		sp_temp = ia32_func_br_sp_list_2 (br_all, br_all_count, func,
			NULL, func->br_root, br_return);

		if (sp_temp != NULL)
			free (sp_temp);

		/* no path => the bblock must be travelled in any possible case
		 */
		if (sp_temp == NULL) {
			brl_must_count += 1;
			brl_must = xrealloc (brl_must,
				brl_must_count * sizeof (ia32_bblock));
			brl_must[brl_must_count - 1] = br_temp;
		}

		/* fixup the bblock array by inserting the removed bblock again
		 */
		memmove (&br_all[bn + 1], &br_all[bn], (br_all_count - n) *
			sizeof (ia32_bblock *));
		br_all[bn] = br_temp;
		br_all_count += 1;
	}

	free (brl_sp);
	free (br_all);

	/* the endnode also lies always in the list, in case it is not
	 * identical with the start node
	 */
	if (br_return != func->br_root) {
		brl_must_count += 1;
		brl_must = xrealloc (brl_must, brl_must_count *
			sizeof (ia32_bblock));
		brl_must[brl_must_count - 1] = br_return;
	}

	*br_count = brl_must_count;

	return (brl_must);
}


ia32_bblock **
ia32_func_br_sp_list (ia32_function *func, unsigned int *brl_len,
	ia32_bblock *source, ia32_bblock *dest)
{
	ia32_bblock **	btl;
	unsigned int	btl_len;
	ia32_bblock **	br_ret;

	btl = ia32_br_get_all (func->br_root, &btl_len);
	br_ret = ia32_func_br_sp_list_2 (btl, btl_len, func, brl_len,
		source, dest);

	free (btl);

	return (br_ret);
}


static ia32_bblock **
ia32_func_br_sp_list_2 (ia32_bblock **btl, unsigned int btl_len,
	ia32_function *func, unsigned int *brl_len,
	ia32_bblock *source, ia32_bblock *dest)
{
	unsigned int	source_idx,
			dest_idx;
	int *		sp_arr;

	ia32_bblock **	sp_bra = NULL;
	unsigned int	sp_idx;
	unsigned int	brl_len_dummy;


	if (btl_len == 0)
		return (NULL);

	if (brl_len == NULL)
		brl_len = &brl_len_dummy;

	source_idx = ia32_brl_find (btl, btl_len, source);
	dest_idx = ia32_brl_find (btl, btl_len, dest);
	assert (source_idx != -1 && dest_idx != -1);

	sp_arr = xcalloc (btl_len, sizeof (int));
	ia32_func_br_sp (btl, btl_len, source_idx, sp_arr);

	/* ensure there is a path from source to dest, else bail out
	 */
	if (sp_arr[dest_idx] == -1)
		goto bail;

	sp_bra = xcalloc (btl_len, sizeof (ia32_bblock *));
	sp_idx = btl_len - 1;
	sp_bra[sp_idx] = dest;

	while (sp_bra[sp_idx] != source) {
		sp_idx -= 1;

		sp_bra[sp_idx] = ia32_func_br_sp_list_3 (btl, btl_len,
			dest_idx, sp_arr);

		dest_idx = ia32_brl_find (btl, btl_len, sp_bra[sp_idx]);
		assert (dest_idx != -1);
	}

	*brl_len = btl_len - sp_idx;
	memmove (&sp_bra[0], &sp_bra[sp_idx], *brl_len *
		sizeof (ia32_bblock *));
	sp_bra = xrealloc (sp_bra, *brl_len * sizeof (ia32_bblock *));

bail:
	free (sp_arr);

	return (sp_bra);
}


static ia32_bblock *
ia32_func_br_sp_list_3 (ia32_bblock **brl, unsigned int brl_len,
	unsigned int idx, int *sparr)
{
	unsigned int	n,
			bn;


	for (n = 0 ; n < brl_len ; ++n) {
		if (n == idx)
			continue;

		if ((sparr[n] + 1) != sparr[idx])
	      		continue;

		/* now we have a possible node with a correct stepcount, check
		 * whether our node is a subnode of it
		 */
		for (bn = 0 ; bn < brl[n]->endbr_count ; ++bn)
			if (brl[n]->endbr[bn] == brl[idx])
				return (brl[n]);
	}

	assert (n < brl_len);

	return (NULL);
}


void
ia32_func_br_sp (ia32_bblock **brl, unsigned int brl_length,
	unsigned int brl_source, int *patharray)
{
	unsigned int	n;


	/* use a recursing bellman-ford algorithm with constant edge weight of
	 * one. recursive due to adjascent vertex restriction in original
	 * bellman-ford.
	 *
	 * 1. initialization, -1 denotes infinity
	 */
	for (n = 0 ; n < brl_length ; ++n)
		patharray[n] = -1;
	patharray[brl_source] = 0;

	ia32_func_br_sp_2 (brl, brl_length, brl_source, patharray);
}


static void
ia32_func_br_sp_2 (ia32_bblock **brl, unsigned int brl_length,
	unsigned int brl_cur, int *patharray)
{
	unsigned int	n,
			bidx;
	ia32_bblock *	br = brl[brl_cur];


	for (n = 0 ; n < br->endbr_count ; ++n) {
		bidx = ia32_brl_find (brl, brl_length, br->endbr[n]);
		if (bidx == -1 || bidx == brl_cur)
			continue;

		/* 2. relax for each subnode, then recurse
		 */
		if (patharray[bidx] == -1 ||
			patharray[bidx] > (patharray[brl_cur] + 1))
		{
			ia32_debug (IA32_DEBUG, "relax: 0x%08x from "
				"%2d to %2d\n", brl[bidx]->start,
				patharray[bidx], patharray[brl_cur] + 1);

			patharray[bidx] = patharray[brl_cur] + 1;

			ia32_func_br_sp_2 (brl, brl_length,
				bidx, patharray);
		}
	}
}


static int
ia32_func_switchtable_is (ia32_instruction *inst)
{
	if (OD_TEST (inst->opc.used, OP_CONTROL) == 0 ||
		OD_TEST (inst->opc.used, OP_JUMP) == 0)
	{
		return (0);
	}

	if (OD_TEST (inst->opc.used, OP_TARGET) == 0)
		return (0);

	/* it may be somewhat dangerous, but we treat function pointers as
	 * special cases of one-element switch tables :-)
	 */
	if (inst->opc.target_type == OP_TYPE_REG)
		return (1);

	if (inst->opc.target_type != OP_TYPE_MEM)
		return (0);

	/* we either need a table start through a register or through a
	 * displacement. if both are lost, we are lost.
	 */
	if (inst->opc.base == 0 && OD_TEST (inst->opc.used, OP_DISPLACE) == 0)
		return (0);

	if (OD_TEST (inst->opc.used, OP_SIB))
		return (1);

	return (0);
}


ia32_bblock *
ia32_func_breakup (ia32_function *func, elf_reloc_list *rel_code,
	elf_reloc_list *rel_rodata)
{
	ia32_bblock *		root;
	ia32_bblock *		cur = ia32_br_new ();

	/* since the calls to ia32_br_get_all_2 are very expensive (uncached
	 * they take up >95% of program execution time), we cache the lookups
	 * here
	 */
	ia32_bblock **		all = NULL;
	unsigned int		all_count;
	int			cache_isdirty = 1;

	/* functions with zero size length are almost always the result of
	 * manual mangling with function definitions (when writing functions
	 * in assembly). hence, when we stumble over such a function, enable
	 * the tracer mode, in which we try to identify the function
	 * boundaries much like IDA does (by tracing the control flow)
	 *
	 * XXX: note that complex compiler constructs, such as switchtables,
	 *      require the assistance of function-length information still.
	 * it is technically possible to decode such constructs, too, but its
	 * not worth the effort here. to add such decoding, uncomment the
	 * line below and debug what breaks:
	 *
	 * func->end = func->start;
	 */
	if (func->start == func->end) {
		ia32_debug (IA32_WARNING, "WARNING: (ia32_func_breakup) "
			"broken function bounds (zero-sized), using tracer\n");

		func->traced_bounds = 1;
	} else
		func->traced_bounds = 0;

	cur->start = func->start;
	root = cur;

	/* work until all bblockes are passed
	 */
	do {
		if (cache_isdirty) {
			if (all != NULL)
				free (all);

			all = ia32_br_get_all (root, &all_count);
		}

		cur = ia32_br_get_unpassed (all, all_count);
		if (cur == NULL)
			break;

		if (func->end != func->start && cur->start == func->end &&
			func->is_symdamaged == 0 && func->traced_bounds == 0)
		{
			ia32_debug (IA32_WARNING, "WARNING: "
				"(ia32_func_breakup) function \"%s\" ends "
				"abnormaly\n", func->name);

			func->is_abnormal_end = 1;
		}
#ifdef	DUPE_DEBUG
		else if (func->is_nested == 0) {
			dupe_check (cur->start);
		}
#endif
		cache_isdirty = ia32_func_breakup_2 (func, root, cur,
			rel_code, rel_rodata, all, all_count);

		if (func->traced_bounds == 0 && cur->end > func->end) {
			ia32_debug (IA32_WARNING, "WARNING: invalid function "
				"symbol table entry for \"%s\" (tb = %d), "
				"fixing (end: 0x%x to 0x%x)\n",
				func->name, func->traced_bounds,
				func->end, cur->end);
			/*ia32_confirm ();*/

			func->end = cur->end;
			func->is_symdamaged = func->traced_bounds = 1;
		}
	} while (1);

	if (all != NULL) {
		free (all);
		all = NULL;
	}


	if (func->traced_bounds) {
		ia32_bblock *		br;
		ia32_bblock **		br_all;
		unsigned int		br_all_count,
					fend;

		br_all = ia32_br_get_all (root, &br_all_count);
		for (fend = 0 ; br_all_count > 0 ; --br_all_count) {
			br = br_all[br_all_count - 1];
			if (br->end > fend)
				fend = br->end;
		}
		free (br_all);

		func->end = fend;
		ia32_debug (IA32_INFO, "  traced function boundaries: "
			"0x%x-0x%x\n", func->start, func->end);
	}

	return (root);
}


/* this is the heart of the control flow analyzation engine
 */

static int
ia32_func_breakup_2 (ia32_function *func, ia32_bblock *root,
	ia32_bblock *this, elf_reloc_list *rel_code, elf_reloc_list *rel_rodata,
	ia32_bblock **all, unsigned int all_count)
{
	int			tb;	/* tracing bounds */
	ia32_instruction *	inst,	/* helper pointer for inst_s */
				inst_s;	/* instruction structure */
		/* temporary working bblockes */
	ia32_bblock *		work1;	/* temporary work bblock */
	ia32_bblock *		work2;	/* temporary work bblock */
	unsigned int		vaddr;	/* current virtual address */
	unsigned int		destaddr = 0x0,	/* control flow destination */
				rdaddr;	/* relocation destaddr */
	unsigned char *		cur;	/* memory position of vaddr */
	unsigned int		ilen;	/* instruction length */

	/* flags for a single control flow change */
	int			intra;	/* is within current function? */
	int			cond;	/* is condition-based? */
	int			resume;	/* will the call/jmp will resume? */
	unsigned int		swmask;	/* switchmask */
	int			duesplit;
	int			c_volatile,
				switch_fail;
	int			br_splitted,
				rval = 0;	/* return value, helpervalue */
	char			inst_str[64];	/* string representation */



	tb = func->traced_bounds;
	vaddr = this->end = this->start;
	this->passed = 1;
	ilen = 0;

	while (tb || vaddr < func->end) {
	/*	ia32_br_dump (func->name, root); */
		/* relocate and decode current instruction
		 */
		cur = &func->mem[vaddr - func->start];
		inst = ia32_decode_instruction (cur, &inst_s);

		if (inst == NULL) {
			ia32_debug (IA32_WARNING,
				"WARNING: (ia32_func_breakup_2) failed to "
				"decode instruction at 0x%08x, in function "
				"\"%s\"\n",
				vaddr, func->name == NULL ? "__unknown" :
				func->name);
			this->endtype = BR_END_INVALID;

			return (rval);
		}

		/* optionally dump instruction being analyzed (when verbosity
		 * is greater-equal to DEBUG).
		 */
		ia32_sprint (inst, inst_str, sizeof (inst_str));
		ia32_debug (IA32_DEBUG, "\t\t(0x%08x) 0x%08x: %s\n",
			(unsigned int) cur, vaddr, inst_str);

		if (vaddr >= 0x17fef && vaddr <= 0x180a5)
			printf ("XXXSCUT\n");

		ilen = inst->length;

		this->end = vaddr;

		/* we have hit a control instruction, lets get its target, if
		 * possible.
		 */
		if (OD_TEST (inst->opc.used, OP_CONTROL))
			destaddr = ia32_trace_control (inst, cur, vaddr,
				&resume, &c_volatile);

		/* heuristics: there are function pointers passed as data that
		 *   are invisible in the symbol table (local static
		 *   functions). to catch those we rely on at least a generic
		 *   "relative-to-.text" symbol table entry. a good example
		 *   for such code can be found in dietlibc/libstdio/vfprintf.c
		 *   where "__fwrite" is passed to __vfprintf within a struct.
		 */
		if (strcmp (inst->opc.opcode->name, "mov") == 0 &&
			OD_TEST (inst->opc.used, OP_SOURCE | OP_TARGET |
				OP_IMMEDIATE) &&
			inst->opc.source_type == OP_TYPE_IMM &&
			inst->opc.source_width == IA32_WIDTH_WORD &&
			inst->opc.imm_size == IA32_WIDTH_WORD &&
			inst->opc.target_width == IA32_WIDTH_WORD)
		{
			elf_reloc *	rel_el;

			rel_el = elf_reloc_list_lookup (rel_code,
				vaddr + ia32_has_immediate (inst, NULL));

			/* this is the most important criterion in the
			 * heuristic (since any function is relocateable there
			 * have to be relocation entries for their offsets).
			 */
			if (rel_el == NULL)
				goto no_shadowed_fptr;

			assert (rel_el->sym != NULL);

			/* how to handle SHN_COMMON externs ?
			 * easy: we do not allow any common symbol, so
			 * we just bail out
			 */
			if (rel_el->sym->sent.st_shndx == SHN_COMMON) {
				ia32_debug (IA32_FATAL, "  0x%08x: relocation "
				"to \"common\" symbol %s\n", vaddr,
				rel_el->sym->name);

				assert (0);

				/* if you don't want to bail, go here */
				goto no_shadowed_fptr;
			}

			assert (rel_el->sym->sec != NULL);
			assert (rel_el->sym->sec->name != NULL);

			/* FIXME:SEC */
			if (strcmp (rel_el->sym->sec->name, ".text") != 0)
				goto no_shadowed_fptr;

			if (ELF32_R_TYPE (rel_el->orig.r_info) != R_386_32)
				goto no_shadowed_fptr;

			/* add new function cross reference (which will result
			 * in a new function being traced). the addend is the
			 * destination address
			 */
			ia32_func_xref_add (func, IA32_XREF_FUNCTION,
				vaddr - func->start,
				ia32_has_immediate (inst, NULL),
				rel_el->addend, NULL, &rel_el->orig, 0);

			ia32_func_oxref_add (func, vaddr - func->start +
				ia32_has_immediate (inst, NULL), rel_el);
		}
no_shadowed_fptr:

		if (tb == 0 && (vaddr + ilen) > func->end) {
			ia32_debug (IA32_WARNING, "WARNING: instruction (%d) "
				"in \"%s\" at 0x%x overlaps function end "
				"0x%x, tracing bounds\n",
				ilen, func->name, vaddr, func->end);

			/*ia32_confirm ();*/
			tb = func->traced_bounds = 1;

		/* even more complicated case: the basic block runs against
		 * the function end without a control flow instruction. that
		 * is, it executes beyond the function end. mostly as a result
		 * of fscked up symbol table entries.
		 * XXX: howto handle this?
		 */
		} else if (tb == 0 && (vaddr + ilen) == func->end &&
			OD_TEST (inst->opc.used, OP_CONTROL) == 0)
		{
			ia32_debug (IA32_WARNING, "WARNING: last basic block "
				"in \"%s\" at 0x%x runs into the function end "
				"0x%x\n", func->name, this->start, func->end);
			ia32_debug (IA32_WARNING, "         tracing bounds, "
				"but take care for possible code duplication\n");

			tb = func->traced_bounds = 1;
			/*ia32_confirm ();*/
		}

		if (OD_TEST (inst->opc.used, OP_CONTROL) == 0) {
			vaddr += ilen;

			work1 = ia32_br_find (root, vaddr, all, all_count,
				&br_splitted);

			if (work1 != NULL) {
				this->end = vaddr;
				this->endtype = BR_END_PASS;
				this->endbr = xcalloc (1, sizeof (ia32_bblock *));
				this->endbr[0] = work1;
				this->endbr_count = 1;

				return (br_splitted ? 1 : 0);
			}

			continue;
		}

		/* from now on, everything is a control flow instruction.
		 * the last instruction length is stored to be able to morph
		 * the last bblock instruction (jcc, call, ret, ..) without
		 * having to re-disassemble it
		 */
		this->last_ilen = ilen;

		/* try to detect whether the control flow leaves our function.
		 * for trace-bounds mode, we have to make a guess.
		 */
		intra = 0;
		if (tb && ia32_verbose (IA32_DEBUG))
			ia32_print (inst);

		if (destaddr != 0xffffffff && tb) {
			/* in case the control is within the instruction, its
			 * either a PIC or relocated entry and we decode it
			 * through the "within function" switchcase below.
			 */
			if (destaddr >= vaddr && destaddr <= vaddr + ilen)
				intra = 1;

			/* when its a "return" instruction, make it
			 * inter-function else only intra function instructions
			 * (cond, uncond jumps) are left, and we make it intra
			 * function
			 */
			if (ia32_trace_return_is (inst, cur))
				intra = 0;
			else
				intra = 1;

			if (destaddr < func->start)
				intra = 0;
		} else if (destaddr != 0xffffffff) {
			intra = ia32_trace_range (func->start, func->end,
				destaddr);

			/* special case (e.g. in glibc, "pause" function with
			 * its invalid symbol table entry): the call is right
			 * at the end of a non-traced bound function. happens
			 * only with invalid symbol table entries.
			 */
			if (intra == 0 && destaddr >= func->start &&
				destaddr <= (vaddr + ilen))
			{
				intra = 1;

				ia32_debug (IA32_WARNING, "WARNING: 0x%x in "
					"\"%s\": overhanging instruction\n",
					vaddr, func->name);
			}
		}

		cond = (OD_TEST (inst->opc.used, OP_COND)) ? 1 : 0;
#define	SW_INTRA	1
#define	SW_INTER	0
#define	SW_COND		2
#define	SW_UNCOND	0
#define	SW_RESUME	4
#define	SW_PASS		0
		swmask = (intra ? SW_INTRA : 0) | (cond ? SW_COND : 0) |
			(resume ? SW_RESUME : 0);

		this->end = vaddr + ilen;

		/* control flow instructions that cannot be predicted can be
		 * of two kinds:
		 *
		 *  - switch tables, which do not resume control and are
		 *    decoded by us
		 *  - function pointer calls, which do resume the control flow
		 */
		switch_fail = 0;

		if (c_volatile && resume == 0) {
			/* volatile jumps (switchtables or manual assembly)
			 */
			ia32_switchtable *	stab;
			int			selem;
			/* unsigned int		addr_sw;	*//* walker */


			this->endtype = BR_END_UNPREDICT;
			if (ia32_func_switchtable_is (inst) == 0) {
				ia32_debug (IA32_WARNING, "WARNING: "
					"(ia32_func_breakup_2) weird control "
					"flow at 0x%08x\n", vaddr);

				return (rval);
			}


			/* now we have to decode the switch table. the current
			 * bblock has to be terminated already, which we
			 * ensured above.
			 */
			ia32_debug (IA32_INFO, "  0x%08x: possible "
				"switch table\n", vaddr);

			stab = ia32_func_switchtable_decode (func, root, this,
				vaddr, inst);

			/* TODO: add && ia32_func_list_find_bystart
			 *         (..., vaddr + inst->length)
			 * XXX: if we failed to break up the switch table, try
			 *      as function pointer
			 */
			if (stab == NULL && inst->opc.target_type == OP_TYPE_REG) {
				switch_fail = 1;

				goto try_func_ptr;
			}

			/* TODO: is it necessary to process relocations for
			 *       stab->vaddr_loc here?
			 * FIXME: instead of always using rel_rodata, check
			 * where the relocation stems from and use the proper
			 * relocation table instead
			 */
			assert (stab != NULL);
			stab->mem_start = /*FIXME:RELOC rel_rodata->reloc_modify->data +*/
				(unsigned int *) stab->vaddr_start;

			/* build the bblock end
			 */
			this->endtype = BR_END_SWITCH;
			this->endbr = xcalloc (stab->entries,
				sizeof (ia32_bblock *));

			stab->reloc = rel_rodata;
			if (rel_rodata != NULL) {
				assert ((unsigned char *) stab->mem_start >=
					rel_rodata->reloc_modify->data);
				assert ((unsigned char *) stab->mem_start <
					(rel_rodata->reloc_modify->data +
					rel_rodata->reloc_modify->data_len));

				stab->vaddr_start = (unsigned int) stab->mem_start;
				stab->vaddr_start -=
					(unsigned int) rel_rodata->reloc_modify->data;
			} else
				stab->vaddr_start = 0x0;

			/*addr_sw = stab->vaddr_start;*/
			for (selem = 0 ; selem < stab->entries ; ++selem) {
				unsigned int	daddr,
						bbn;
#if 0
				elf_reloc *	rel_el;

				/* find the corresponding relocation entry
				 * for the switch table entry. then check for
				 * correct reloca type (R_386_32), and add the
				 * case to our internal struct
				 */
				rel_el = elf_reloc_list_lookup (rel_rodata,
					addr_sw);
				addr_sw += 4;

				/* FIXME: ugly heuristics. if we fail to obtain
				 *        a relocation entry, then our size
				 * guess of the table was wrong. lets refine.
				 */
				if (rel_el == NULL) {
					stab->entries = selem - 1;
					ia32_debug (IA32_INFO, "  refined to "
						"%u cases\n", stab->entries);
					continue;
				}
				assert (rel_el != NULL);

				assert (ELF32_R_TYPE (rel_el->orig.r_info) ==
					R_386_32);
				daddr = rel_el->sym->sent.st_value +
					rel_el->addend;
#endif
				daddr = stab->mem_start[selem];

				/* convert from memory to object file address,
				 * as we need to get the basic block
				 * addresses.
				 */
				daddr = daddr - (((unsigned int) func->mem) -
					func->start);

				ia32_debug (IA32_INFO, "    case %d ==> "
					"target: 0x%08x\n", selem, daddr);

				/* note that we rely on basic compiler sanity
				 * so that the switch will not reach into our
				 * current bblock.
				 * when we create a new bblock, mark return
				 * value
				 *
				 * first, try to find a basic block with the
				 * same destaddr within the already processed
				 * switch cases, then fall back on the
				 * function-level basic block search (_br_find)
				 */
				work1 = NULL;
				for (bbn = 0 ; bbn < selem ; ++bbn) {
					if (this->endbr[bbn]->start != daddr)
						continue;

					work1 = this->endbr[bbn];
					break;
				}

				br_splitted = 0;
				if (work1 == NULL)
					work1 = ia32_br_find (root, daddr,
						all, all_count, &br_splitted);

				if (br_splitted || work1 == NULL)
					rval = 1;

				if (work1 == NULL) {
					work1 = ia32_br_new ();
					work1->start = daddr;
				}
				this->endbr[selem] = work1;
			}

			this->endbr_count = stab->entries;
			this->switchtab = stab;

			return (rval);
		}

try_func_ptr:
		if (switch_fail || (c_volatile && resume)) {
			ia32_linux_interrupt *	intdef;

			/* volatile calls (function pointer calls or syscalls)
			 */
			assert (OD_TEST (inst->opc.used, OP_TARGET));

			intdef = ia32_linux_interrupt_decode (func, root,
				this, vaddr, inst);

			/* its a system call that does not resume, so setup an
			 * extra endtype of _END_CTRL_SYSCALL_END. does not return
			 * just means that it may jump elsewhere (like
			 * sigreturn), not that the program is terminated.
			 */
			if (intdef != NULL && intdef->resume == 0) {
				ia32_debug (IA32_WARNING, "WARNING: "
					"(ia32_func_breakup_2) non-returning "
					"system call 0x%x\n", intdef->syscall);

				this->interrupt = intdef;
				this->endtype = BR_END_CTRL_SYSCALL_END;

				return (rval);
			}
#if 1
			/* FIXME: linux centric code
			 */
			if (strcmp (inst->opc.opcode->name, "int") == 0 &&
				inst->opc.target_type == OP_TYPE_IMM &&
				inst->opc.imm_value == IA32_LINUX_SYSCALLINT)
			{
				ia32_debug (IA32_DEBUG, "DEBUG: 0x%x: "
					"generic linux system call\n", vaddr);

				work1 = ia32_br_find (root, vaddr + ilen,
					all, all_count, &br_splitted);

				if (br_splitted)
					rval = 1;

				this->endtype = BR_END_CTRL_SYSCALL;

				this->endbr = xcalloc (1,
					sizeof (ia32_bblock *));
				this->endbr[0] = work1;
				if (this->endbr[0] == NULL) {
					rval = 1;
					this->endbr[0] = ia32_br_new ();
					this->endbr[0]->start = vaddr + ilen;
				}
				this->endbr_count = 1;

				return (rval);
			}
#endif
			/* immediate target is another interrupt call
			 * normally. just go ahead
			 */
			if (inst->opc.target_type == OP_TYPE_IMM) {
				vaddr += ilen;

				work1 = ia32_br_find (root, vaddr,
					all, all_count, &br_splitted);
				if (work1 != NULL) {
					this->end = vaddr;
					this->endtype = BR_END_PASS;
					this->endbr = xcalloc (1,
						sizeof (ia32_bblock *));
					this->endbr[0] = work1;
					this->endbr_count = 1;

					return (br_splitted ? 1 : 0);
				}

				continue;
			}

			/* memory pointer call. (function pointer but stored
			 * in memory). hell, ia32 addressing modes are a mess.
			 * not every function pointer call is spilled through
			 * a register load and register call, but some are
			 * direct-memory calls. this is one, so decode it.
			 * example of such code: second basic block of
			 * __uselocale within glibc.
			 */
			if (inst->opc.target_type == OP_TYPE_MEMABS) {
				/* FIXME: keep this as warning because i am
				 * not yet sure how well this works
				 */
				ia32_debug (IA32_DEBUG, "DEBUG: (%s) 0x%x: "
					"memory indirect call found\n",
					func->name, vaddr);

				ia32_debug (IA32_DEBUG, "   0x%08x: volatile "
					"memory indirect \"%s\" instruction\n",
					vaddr, inst->opc.opcode->name);

				/* we are only interested in memory displaced
				 * calls
				 */
				if (strcmp (inst->opc.opcode->name,
					"call") != 0 ||
					ia32_has_displacement (inst, NULL) == 0)
				{
					assert (0);
				}

				work1 = ia32_br_find (root, vaddr + ilen,
					all, all_count, &br_splitted);

				if (br_splitted)
					rval = 1;

				this->endtype = BR_END_CALL_MEM;
				this->memabs_pos =
					ia32_has_displacement (inst, NULL);

				this->endbr = xcalloc (1,
					sizeof (ia32_bblock *));
				this->endbr[0] = work1;
				if (this->endbr[0] == NULL) {
					rval = 1;
					this->endbr[0] = ia32_br_new ();
					this->endbr[0]->start = vaddr + ilen;
				}
				this->endbr_count = 1;

				return (rval);
			}

			if (inst->opc.target_type == OP_TYPE_MEMREG) {
				ia32_debug (IA32_DEBUG, "DEBUG: 0x%08x: "
					"_MEMREG volatile call\n", vaddr);

				/* _MEMREG should implicit target_reg.
				 * displ_value is optional though.
				 */
				this->memreg_displ = 0;

#if 0
				/* this displacement should never be covered
				 * by a relocation entry.
				 */
				if (ia32_has_displacement (inst, NULL)) {
					elf_reloc *	rel_el;

					rel_el = elf_reloc_list_lookup
						(rel_code, vaddr +
						ia32_has_displacement
						(inst, NULL));

					if (rel_el != NULL) {
						ia32_debug (IA32_FATAL,
							"FATAL: 0x%08x: "
							"_MEMREG call covered "
							"by relocation\n",
							vaddr);

						assert (0);
						exit (EXIT_FAILURE);
					}

					this->memreg_displ = ia32_extend_signed
						(inst->opc.displ_value,
						inst->opc.displ_size);
				}
#endif
				this->memreg_displ = ia32_has_displacement (inst, NULL);
				this->memreg_callreg = inst->opc.target_reg;

				ia32_debug (IA32_DEBUG, "    [reg %d + 0x%08x]\n",
					this->memreg_callreg, this->memreg_displ);

				this->end = vaddr + ilen;
				work1 = ia32_br_find (root, vaddr + ilen,
					all, all_count, &br_splitted);

				if (br_splitted)
					rval = 1;

				this->endtype = BR_END_CALL_MEMREG;

				this->endbr = xcalloc (1,
					sizeof (ia32_bblock *));
				this->endbr[0] = work1;
				if (this->endbr[0] == NULL) {
					rval = 1;
					this->endbr[0] = ia32_br_new ();
					this->endbr[0]->start = vaddr + ilen;
				}
				this->endbr_count = 1;

				return (rval);
			}

			/* SIB addressed calls. the most complicated, but we
			 * just move the complexity to the loaders ;)
			 */
			if (inst->opc.target_type == OP_TYPE_MEM) {
				ia32_debug (IA32_DEBUG, "DEBUG: 0x%08x: SIB "
					"addressing call/jump\n", vaddr);

				assert (strcmp (inst->opc.opcode->name, "call") == 0);

				this->end = vaddr + ilen;
				work1 = ia32_br_find (root, vaddr + ilen,
					all, all_count, &br_splitted);

				if (br_splitted)
					rval = 1;

				this->endtype = BR_END_CALL_MEMSIB;

				this->endbr = xcalloc (1,
					sizeof (ia32_bblock *));
				this->endbr[0] = work1;
				if (this->endbr[0] == NULL) {
					rval = 1;
					this->endbr[0] = ia32_br_new ();
					this->endbr[0]->start = vaddr + ilen;
				}
				this->endbr_count = 1;

				this->memsib_scale = inst->opc.scale;
				this->memsib_index = inst->opc.index;
				this->memsib_indexreg = inst->opc.index_reg;
				this->memsib_base = inst->opc.base;
				this->memsib_basereg = inst->opc.base_reg;
				this->memsib_displpos =
					ia32_has_displacement (inst, NULL);

				return (rval);
			}

			/* the only target type thats left should be a simple
			 * register based jump/call. if its not, bail.
			 */
			if (inst->opc.target_type != OP_TYPE_REG) {
				ia32_debug (IA32_FATAL, "FATAL: 0x%08x: "
					"volatile, unhandled (sib?) call "
					"found, target type %d\n", vaddr,
					inst->opc.target_type);

				assert (0);
				exit (EXIT_FAILURE);
			}

			ia32_debug (IA32_DEBUG, "    0x%08x: volatile %s, "
				"register %s\n", vaddr,
				switch_fail ? "jump" : "call",
				ia32_regs_wide[inst->opc.target_reg]);

			this->end = vaddr + ilen;
			this->call_reg = inst->opc.target_reg;
			work1 = ia32_br_find (root, vaddr + ilen,
				all, all_count, &br_splitted);

			if (br_splitted)
				rval = 1;

			if (switch_fail) {
				this->endtype = BR_END_FUNCPTR_JUMP;
				this->endbr_count = 0;

				return (rval);
			}

			this->endtype = BR_END_FUNCPTR_CALL;
			this->endbr = xcalloc (1, sizeof (ia32_bblock *));

			this->endbr[0] = work1;
			if (this->endbr[0] == NULL) {
				rval = 1;
				this->endbr[0] = ia32_br_new ();
				this->endbr[0]->start = vaddr + ilen;
			}
			this->endbr_count = 1;

			return (rval);
		}

		switch (swmask) {
		/* inter function references
		 */
		/* bblock terminate, "ret" return or a "hlt" instruction, but
		 * we treat them much the same way.
		 */
		case (SW_INTER | SW_UNCOND | SW_PASS):
			this->endtype = BR_END_RET;

			return (rval);

		/* bblock ignore, conditional jump inter-function
		 */
		case (SW_INTER | SW_COND | SW_PASS):
			/* the most common case for this to happen is to share
			 * the same trailing function code (including ret) for
			 * a number of functions (e.g. strcoll in glibc). the
			 * problem with this type of jump is that the code it
			 * jumps to may lead to undiscovered functions which
			 * we have to analyze later. therefore, add a function
			 * reference and the special _END_IF_INTER type.
			 */
			assert (ia32_has_displacement (inst, NULL));
			ia32_func_xref_add (func, IA32_XREF_FUNCTION,
				vaddr - func->start,
				ia32_has_displacement (inst, NULL),
				destaddr, NULL, NULL, 0);

			work1 = ia32_br_find (root, vaddr + ilen,
				all, all_count, &br_splitted);

			if (br_splitted)
				rval = 1;

			this->endtype = BR_END_IF_INTER;
			this->cond = inst->opc.cond;
			this->endbr_count = 1;
			this->endbr = xcalloc (1, sizeof (ia32_bblock *));
			this->endbr[0] = work1;

			if (this->endbr[0] == NULL) {
				rval = 1;
				this->endbr[0] = ia32_br_new ();
				this->endbr[0]->start = vaddr + ilen;
			}

			return (rval);
			break;

		/* bblock call, external function reference
		 * add the special passing bblock type "call".
		 */
		case (SW_INTER | SW_UNCOND | SW_RESUME):
		case (SW_INTER | SW_COND | SW_RESUME):
			/* FIXME: calc correct addend (not just 1)
			 */
			ia32_func_xref_add (func, IA32_XREF_FUNCTION,
				vaddr - func->start, 1,
				destaddr, NULL, NULL, 0);

			this->end = vaddr + ilen;
			work1 = ia32_br_find (root, vaddr + ilen,
				all, all_count, &br_splitted);

			if (br_splitted)
				rval = 1;

			this->endtype = BR_END_CALL;
			this->endbr = xcalloc (1, sizeof (ia32_bblock *));

			this->endbr[0] = work1;
			if (this->endbr[0] == NULL) {
				rval = 1;
				this->endbr[0] = ia32_br_new ();
				this->endbr[0]->start = vaddr + ilen;
			}
			this->endbr_count = 1;

			return (rval);

		/* intra function references
		 */
		/* bblock if, conditional jump
		 */
		case (SW_INTRA | SW_COND | SW_PASS):
			if (destaddr == 0xffffffff)
				break;

			/* when the jcc points to within its own instruction,
			 * we can be sure there will be a relocation offset
			 * inserted at that place. this is rarely used, the
			 * only place i have seen it yet was the handling of
			 * system calls in glibc, where an inter-function jump
			 * to __syscall_error is used.
			 */
			if (ia32_trace_range (vaddr, vaddr + ilen, destaddr)) {
				unsigned int	odaddr = destaddr;


				destaddr = elf_reloc_list_lookup_func (rel_code,
					destaddr);

				ia32_debug (IA32_INFO, "    0x%08x: calljump "
					"to 0x%08x\n", vaddr, destaddr);

				assert (destaddr != 0xffffffff);

				ia32_func_xref_add (func, IA32_XREF_FUNCTION,
					vaddr - func->start, odaddr - vaddr,
					destaddr, NULL, NULL, 0);

				work1 = ia32_br_find (root, vaddr + ilen,
					all, all_count, &br_splitted);

				if (br_splitted)
					rval = 1;

				this->endtype = BR_END_IF_INTER;
				this->cond = inst->opc.cond;
				this->endbr_count = 1;
				this->endbr = xcalloc (1, sizeof (ia32_bblock *));
				this->endbr[0] = work1;

				if (this->endbr[0] == NULL) {
					rval = 1;
					this->endbr[0] = ia32_br_new ();
					this->endbr[0]->start = vaddr + ilen;
				}

				return (rval);
			}

			/* XXX: finding on the vaddr address cannot clash,
			 * since we either find no bblock or an already passed
			 * bblock. finding on the destination address can split
			 * our current bblock (`this'). hence, the first thing
			 * we have to ensure is whether we got a bblock and if
			 * resulted through a split
			 */
			duesplit = ia32_trace_range (this->start, this->end,
				destaddr);
			work1 = ia32_br_find (root, vaddr + ilen,
				all, all_count, &br_splitted);
			if (br_splitted)
				rval = 1;
			work2 = ia32_br_find (root, destaddr, all, all_count,
				&br_splitted);
			if (br_splitted)
				rval = 1;

			if (duesplit) {
				assert (work2 != NULL);
				this = work2;
			}

			/* XXX: set this after ia32_br_find, else clash
			 */
			this->endtype = BR_END_IF;
			this->cond = inst->opc.cond;
			this->endbr_count = 2;
			this->endbr = xcalloc (2, sizeof (ia32_bblock *));
			this->endbr[0] = work1;
			this->endbr[1] = work2;

			if (this->endbr[0] == NULL) {
				rval = 1;
				this->endbr[0] = ia32_br_new ();
				this->endbr[0]->start = vaddr + ilen;
			}

			if (this->endbr[1] == NULL) {
				rval = 1;
				this->endbr[1] = ia32_br_new ();
				this->endbr[1]->start = destaddr;
			}

			return (rval);

		/* bblock transfer, unconditional jump
		 */
		case (SW_INTRA | SW_UNCOND | SW_PASS):
			if (destaddr == 0xffffffff)
				break;

			/* check whether there are relocation information for
			 * the location
			 */
			rdaddr = elf_reloc_list_lookup_func (rel_code,
				vaddr + ia32_has_displacement (inst, NULL));
			ia32_debug (IA32_DEBUG, "    0x%08x: unconditional "
				"jump, reloc lookup: 0x%08x\n", vaddr, rdaddr);

			if (rdaddr != 0xffffffff) {
				unsigned int	rel_displ;

				rel_displ = ia32_has_displacement (inst, NULL);

				ia32_debug (IA32_DEBUG, "      through reloc "
					"at 0x%08x to 0x%08x\n",
					vaddr + rel_displ, rdaddr);

				ia32_func_xref_add (func, IA32_XREF_FUNCTION,
					vaddr - func->start, rel_displ,
					rdaddr, NULL, NULL, 0);

				this->endtype = BR_END_TRANSFER_INTER;
				this->endbr_count = 0;

				return (rval);
			}

			duesplit = ia32_trace_range (this->start, this->end,
				destaddr);
			work1 = ia32_br_find (root, destaddr, all, all_count,
				&br_splitted);
			if (br_splitted)
				rval = 1;

			if (duesplit) {
				assert (work1 != NULL);
				this = work1;
			}

			this->endtype = BR_END_TRANSFER;
			this->endbr = xcalloc (1, sizeof (ia32_bblock *));

			this->endbr[0] = work1;
			if (this->endbr[0] == NULL) {
				rval = 1;
				this->endbr[0] = ia32_br_new ();
				this->endbr[0]->start = destaddr;
			}
			this->endbr_count = 1;

			return (rval);

		/* bblock ignore or failure, relocation or non-conform code
		 * mostly normal calls that get fixed by relocation entries
		 */
		case (SW_INTRA | SW_UNCOND | SW_RESUME):
		case (SW_INTRA | SW_COND | SW_RESUME):
			/* ignore unknown targets
			 */
			if (destaddr == 0xffffffff)
				break;

		        /* for directly-after-instruction calls the call is
			 * used to obtain the current position (in PIC code
			 * before any relocation could take place).  this
			 * might be problematic for some applications, so mark
			 * that odd case within the function object but ignore
			 * otherwise.
			 */
			if (destaddr == vaddr + ilen) {
				ia32_debug (IA32_WARNING, "WARNING: function "
					"\"%s\" is position curious at 0x%x. "
					"marking as such.\n", func->name,
					vaddr);

				func->is_pos_curious = 1;

				break;
			}

			ia32_debug (IA32_INFO, "  0x%08x: call\n", vaddr);

			/* to-be-filled-in relocation positions in ELF object
			 * files have target one after the call instruction
			 * so, look it up in the relocation table.
			 * FIXME: use a more generic "destaddr within
			 *        instruction" approach, ia32_trace_range (..)
			 */
			if (destaddr == vaddr + 1) {
				elf_reloc * rel_item;

				destaddr = elf_reloc_list_lookup_func (rel_code,
					vaddr + 1);

				rel_item = elf_reloc_list_lookup (rel_code,
					vaddr + 1);

				/* still invalid :-/
				 */
				if (destaddr == 0xffffffff) {
					ia32_debug (IA32_DEBUG, "DEBUG: "
						"(ia32_func_breakup_2) failed "
						"to obtain relocation for "
						"0x%08x\n", vaddr);

					elf_reloc_list_debug (rel_code, vaddr + 1);

					ia32_func_xref_add (func,
						IA32_XREF_FUNCEXTERN,
						vaddr - func->start, 1,
						destaddr, NULL, rel_item == NULL ?
							NULL : &rel_item->orig, 0);

					/* XXX ugly hack: step over the call,
					 * create a "ignore instruction"
					 * endtype.
					 * FIXME: find a better way
					 */
					ia32_debug (IA32_DEBUG, "DEBUG: "
						"adding skip branch end type, "
						"danger ahead...\n");

					work1 = ia32_br_find (root, vaddr + ilen,
						all, all_count, &br_splitted);
					if (br_splitted)
						rval = 1;

					this->endtype = BR_END_CALL_EXTERN;
					this->endbr = xcalloc (1,
						sizeof (ia32_bblock *));

					this->endbr[0] = work1;
					if (this->endbr[0] == NULL) {
						rval = 1;
						this->endbr[0] = ia32_br_new ();
						this->endbr[0]->start = vaddr + ilen;
					}
					this->endbr_count = 1;
					/*assert (0); */
					return (rval);
				}

				ia32_debug (IA32_INFO, "    through reloc at "
					"0x%08x to 0x%08x\n", vaddr, destaddr);
				ia32_func_xref_add (func, IA32_XREF_FUNCTION,
					vaddr - func->start, 1, destaddr, NULL,
					rel_item == NULL ?
						NULL : &rel_item->orig, 0);

				work1 = ia32_br_find (root, vaddr + ilen,
					all, all_count, &br_splitted);
				if (br_splitted)
					rval = 1;

				this->endtype = BR_END_CALL;
				this->endbr = xcalloc (1, sizeof (ia32_bblock *));

				this->endbr[0] = work1;
				if (this->endbr[0] == NULL) {
					rval = 1;
					this->endbr[0] = ia32_br_new ();
					this->endbr[0]->start = vaddr + ilen;
				}
				this->endbr_count = 1;

				ia32_debug (IA32_INFO, "    ==> call at 0x%08x "
					"to 0x%08x\n", vaddr, destaddr);

				return (rval);
			}

			/* one legit destaddr is the start of this function,
			 * in case its direct recursive. check this case
			 */
			if (func->start == destaddr) {
				/* do not specify an ia32_function structure
				 * for now, will fill that in later
				 */
				ia32_func_xref_add (func, IA32_XREF_FUNCTION,
					vaddr - func->start, 1, destaddr, NULL,
					NULL, 0);

				break;
			}

			/* in case of inter-section calls (example: _init
			 * calling into .text section), add just the function
			 * address as reference and let it fillin the correct
			 * function object later, when all sections have been
			 * processed.
			 */
			if (ia32_has_displacement (inst, NULL)) {
				/* adress the relocation is done */
				unsigned int	rel_addr;
				elf_reloc *	rel;

				rel_addr = vaddr +
					ia32_has_displacement (inst, NULL);

				rel = elf_reloc_list_lookup (rel_code, rel_addr);
				if (rel != NULL) {
					if (rel->type != ELF_RELOC_SECTION)
						goto complex_func_hierarchy;

					destaddr = rel->addend -
						ia32_has_displacement (inst, NULL) +
						inst->length;

					ia32_debug (IA32_WARNING, "WARNING: 0x%08x: "
						"inter section call from \"%s\" "
						"(%s:0x%x (+%d) -> %s:0x%x)\n",
						vaddr, func->name,
						rel_code->reloc_modify->name, vaddr,
						ia32_has_displacement (inst, NULL),
						rel->sym != NULL ? rel->sym->sec->name :
						"?", destaddr);

				} else {
					/* no relocation entry found, that
					 * means destaddr is already valid
					 */
					ia32_debug (IA32_INFO, "INFO: 0x%x: "
						"displaced non-relocation call\n",
						vaddr);
					ia32_confirm ();
				}

				/* FIXME:SEC: we just pack in the offset of
				 * the function, but do not specify to what
				 * section it refers, doh! :-/
				 */
				ia32_func_xref_add (func, IA32_XREF_FUNCTION,
					vaddr - func->start,
					ia32_has_displacement (inst, NULL),
					destaddr, NULL,
					rel == NULL ? NULL : &rel->orig, 1);

				work1 = ia32_br_find (root, vaddr + ilen,
					all, all_count, &br_splitted);

				if (br_splitted)
					rval = 1;

				this->endtype = BR_END_CALL;
				this->endbr = xcalloc (1, sizeof (ia32_bblock *));

				this->endbr[0] = work1;
				if (this->endbr[0] == NULL) {
					rval = 1;
					this->endbr[0] = ia32_br_new ();
					this->endbr[0]->start = vaddr + ilen;
				}
				this->endbr_count = 1;

				return (rval);
			}

complex_func_hierarchy:
			/* everything else is an error or complex function
			 * unrolling we cannot cope with
			 */
			ia32_debug (IA32_FATAL, "FATAL: (ia32_func_breakup_2) "
				"complex function hierarchy\n");
			ia32_debug (IA32_FATAL, "  func-entry: 0x%08x, "
				"vaddr: 0x%08x, destaddr: 0x%08x",
				func->start, vaddr, destaddr);

			assert (0);
			this->endtype = BR_END_INVALID;

			return (rval);

		default:
			break;
		}
#undef	SW_INTRA
#undef	SW_INTER
#undef	SW_COND
#undef	SW_UNCOND
#undef	SW_RESUME
#undef	SW_PASS
		vaddr += ilen;

		/* examine next instruction, is there already a bblock for it?
		 */
		work1 = ia32_br_find (root, vaddr, all, all_count,
			&br_splitted);
		if (br_splitted)
			rval = 1;

		if (work1 != NULL) {
			this->end = vaddr;
			this->endtype = BR_END_PASS;
			this->endbr = xcalloc (1, sizeof (ia32_bblock *));
			this->endbr[0] = work1;
			this->endbr_count = 1;

			return (rval);
		}
	}

	return (rval);
}


ia32_linux_interrupt *
ia32_linux_interrupt_decode (ia32_function *func, ia32_bblock *root,
	ia32_bblock *this, unsigned int vaddr, ia32_instruction *inst)
{
	ia32_linux_interrupt *	intdef;
	ia32_instruction *	cur;
	ia32_instruction *	ilist;
	int			ilist_count,
				in;	/* ilist walker */
	int			syscall_number = -1;


	if (strcmp (inst->opc.opcode->name, "int") != 0 ||
		inst->opc.target_type != OP_TYPE_IMM ||
		inst->opc.imm_value != IA32_LINUX_SYSCALLINT)
		return (NULL);

	/* decode the instructions leading to the system call. then try to
	 * figure out its system call number.
	 */
	ilist = ia32_func_inst_traceup (4, &ilist_count, func, root,
		this, vaddr);
	assert (ilist != NULL && ilist_count > 0);

	for (in = ilist_count - 1 ; in >= 0 ; --in) {
		cur = &ilist[in];
		if (ia32_verbose (IA32_DEBUG))
			ia32_print (cur);

		if (strcmp (cur->opc.opcode->name, "mov") != 0)
			continue;

		assert (OD_TEST (cur->opc.used, OP_SOURCE | OP_TARGET));

		if (cur->opc.target_type != OP_TYPE_REG ||
			cur->opc.source_type != OP_TYPE_IMM)
			continue;

		if (cur->opc.target_reg != IA32_REG_EAX)
			continue;

		syscall_number = cur->opc.imm_value;
		break;
	}

	if (syscall_number < 0 || syscall_number > IA32_LINUX_MAXSYSCALL) {
		ia32_debug (IA32_FATAL, "FATAL: (ia32_linux_interrupt_decode) "
			"failed to obtain system call number\n");

		return (NULL);
	}

	intdef = xcalloc (1, sizeof (ia32_linux_interrupt));
	intdef->syscall = syscall_number;

	switch (syscall_number) {
	case (__NR_exit):
	case (__NR_sigreturn):
	case (__NR_rt_sigreturn):
		intdef->resume = 0;
		break;
	default:
		intdef->resume = 1;
		break;
	}

	return (intdef);
}

/* we need to decode possible switch tables to do a complete basic block
 * analysis. for the actual native run we do not need one though.
 */

ia32_switchtable *
ia32_func_switchtable_decode (ia32_function *func, ia32_bblock *root,
	ia32_bblock *this, unsigned int vaddr, ia32_instruction *inst)
{
	ia32_instruction *	cur;
	ia32_instruction *	ilist;
	ia32_instruction	inst_s;
	int			ilist_count;

	/* we trace back the use of registers to index and displace into the
	 * switch table. to do this, we keep register indexes.
	 *   stab_idx_reg = register used to relativly index into the table
	 *   disp_reg = table base register
	 *   comb_reg = combined register
	 *
	 * only stab_idx_reg is mandatory, the others are used to fill
	 *   table_start = absolute address of switch table
	 *
	 * our algorithm is to trace back until we have both the table_start
	 * and the stab_idx_reg. then we can emulate the switch table
	 * behaviour and fill in the bblockes.
	 *
	 *   stab_size is optional, and filled in when we see a check for
	 * an upper bound of stab_idx_reg.
	 */
	int			stab_idx_reg = -1,
				stab_idx_scale = -1,
				disp_reg = -1,
				comb_reg = -1;
	unsigned int		table_start = 0xffffffff,
				table_loc_vaddr = 0xffffffff;
	int			in;	/* instruction walker */
	int			stab_size = -1;
	ia32_switchtable *	stab;
	unsigned int		t_rel,
				t_val = 0;


	ilist = ia32_func_inst_traceup (16, &ilist_count, func, root,
		this, vaddr);
	assert (ilist != NULL && ilist_count > 0);

	cur = inst;
	if (ia32_verbose (IA32_DEBUG))
		ia32_print (cur);

	if (cur->opc.target_type == OP_TYPE_REG) {
		comb_reg = cur->opc.target_reg;
	} else if (cur->opc.target_type == OP_TYPE_MEM) {
		unsigned int	displ_rel;


		if (cur->opc.base)
			disp_reg = cur->opc.base_reg;

		/* FIXME: check whether the index reg is already 4*'ed */
		if (cur->opc.index) {
			stab_idx_reg = cur->opc.index_reg;
			stab_idx_scale = cur->opc.scale;
		}

		displ_rel = ia32_has_displacement (cur, NULL);
		if (displ_rel != 0) {
			table_start = inst->opc.displ_value;
			table_loc_vaddr = vaddr + displ_rel;
		}
	}

	for (in = ilist_count - 1 ; in >= 0 ; --in) {
		cur = &ilist[in];
		if (ia32_verbose (IA32_DEBUG))
			ia32_print (cur);

		if (strcmp (cur->opc.opcode->name, "mov") == 0) {
			int *		regp;

			assert (OD_TEST (cur->opc.used, OP_SOURCE | OP_TARGET));

			if (cur->opc.target_type != OP_TYPE_REG)
				continue;

			if (cur->opc.target_reg == disp_reg)
				regp = &disp_reg;
			else if (cur->opc.target_reg == comb_reg)
				regp = &comb_reg;
			else
				continue;


			if (cur->opc.source_type == OP_TYPE_REG) {
				*regp = cur->opc.source_reg;
				continue;
			}

			t_rel = ia32_has_immediate (cur, NULL);
			if (t_rel) {
				t_val = cur->opc.imm_value;
			} else {
				t_rel = ia32_has_displacement (cur, NULL);
				if (t_rel)
					t_val = cur->opc.displ_value;

				/* SIB addressing on combined register almost
				 * always means jumptable access
				 */
				if (cur->opc.source_type == OP_TYPE_MEM &&
					cur->opc.target_reg == comb_reg &&
					cur->opc.index)
				{
					comb_reg = -1;
					stab_idx_reg = cur->opc.index_reg;
					/* FIXME: what to do about
					 * scales != 2 ?
					 */
					stab_idx_scale = cur->opc.scale;
					table_start = cur->opc.displ_value;
				}
			}

			if (t_rel && (cur->opc.source_type == OP_TYPE_IMM ||
				cur->opc.source_type == OP_TYPE_MEMREG))
			{
#ifdef	MAYBE_REWRITE_THIS_HEURISTICS
				ia32_debug (IA32_INFO, "INFO: abort "
					"switchtable search with t_val = "
					"0x%08x\n", t_val);

				return (NULL);
#endif
#if 1
				table_start = t_val;
				table_loc_vaddr = vaddr + t_rel;

				if (cur->opc.source_type == OP_TYPE_MEMREG &&
					regp == &comb_reg)
				{
					comb_reg = -1;
					stab_idx_reg = cur->opc.source_reg;
				}

				goto ccond;
#endif
			}

		} else if (strcmp (cur->opc.opcode->name, "cmp") == 0) {
			/* heuristics to guess the size of the switch table
			 */
			t_rel = ia32_has_immediate (cur, NULL);
			if (t_rel && cur->opc.imm_value > 0 &&
				cur->opc.imm_value < 4096)
			{
				stab_size = cur->opc.imm_value;
			}
		}
#if 1
ccond:
#endif
		if (stab_size != -1 && stab_idx_reg != -1 &&
			table_start != 0xffffffff)
		{
			break;
		}
	}

	/* XXX: ugly heuristics. if we have everything but the table start,
	 *      because one register is kept for the entire function and
	 * there are multiple pathes to the switch block, we just trace the
	 * function from top to bottom and use the first register load to
	 * the disp_reg. ugly, but should work in 90% of the cases.
	 * using a real safe method would require code- and dataflow analysis.
	 * XXX: maybe a dominator tree of the basic blocks is enough, or even
	 *      just _one_ path, since it would suffice to let the register
	 * load
	 */
	if (in < 0 && stab_size != -1 && stab_idx_reg != -1 &&
		disp_reg != -1 && table_start == 0xffffffff)
	{
		for (vaddr = func->start ; table_start == 0xffffffff &&
			vaddr < func->end ; vaddr += cur->length)
		{
			cur = ia32_decode_instruction
				(&func->mem[vaddr - func->start], &inst_s);
			assert (cur != NULL);

			if (strcmp (cur->opc.opcode->name, "mov") != 0)
				continue;

			assert (OD_TEST (cur->opc.used, OP_SOURCE | OP_TARGET));
			if (cur->opc.target_type != OP_TYPE_REG)
				continue;

			if (cur->opc.target_reg != disp_reg)
				continue;

			if (cur->opc.source_type != OP_TYPE_IMM)
				continue;

			t_rel = ia32_has_immediate (cur, NULL);
			if (t_rel) {
				t_val = cur->opc.imm_value;
			} else {
				t_rel = ia32_has_displacement (cur, NULL);
				if (t_rel)
					t_val = cur->opc.displ_value;
			}

			if (t_rel) {
				table_start = t_val;
				table_loc_vaddr = vaddr + t_rel;
			}
		}
	} else if (in < 0) {
		ia32_debug (IA32_WARNING, "WARNING: "
			"(ia32_func_switchtable_decode) failed to extract "
			"switchtable information\n");

		return (NULL);
	}

	ia32_debug (IA32_INFO, "switch table, start: 0x%08x, elements 0-%d, "
		"index reg %d (scale %d)\n",
		table_start, stab_size, stab_idx_reg, stab_idx_scale);

	free (ilist);

	stab = xcalloc (1, sizeof (ia32_switchtable));
	stab->entries = stab_size + 1;
	stab->vaddr_start = table_start;
	stab->vaddr_loc = table_loc_vaddr;	/* used for relocation */
	stab->idx_reg = stab_idx_reg;
	stab->idx_scale = stab_idx_scale;

	return (stab);
}


ia32_instruction *
ia32_func_inst_traceup (int backcount, int *inst_count, ia32_function *func,
	ia32_bblock *root, ia32_bblock *cur, unsigned int end_vaddr)
{
	ia32_instruction *	inst,
				inst_s;
	ia32_instruction *	ilist = NULL;
	unsigned char *		mem;
	unsigned int		icount,
				vaddr,
				skipcount = 0;
	ia32_bblock **		brefs;
	int			brefs_count;
	ia32_instruction *	sub_ilist;
	int			sub_ilist_count;


	*inst_count = 0;

	icount = ia32_func_inst_count (func, cur->start, end_vaddr);
	if (icount > backcount) {
		skipcount = icount - backcount;
		icount = backcount;
	}

	/* create the list in correct order
	 */
	for (vaddr = cur->start ; vaddr < end_vaddr ; vaddr += inst->length) {
		mem = &func->mem[vaddr - func->start];
		inst = ia32_decode_instruction (mem, &inst_s);
		assert (inst != NULL);

		/* skip instructions that are beyond the number we should
		 * collect.
		 */
		if (skipcount > 0) {
			skipcount -= 1;
			continue;
		}

		/* else add the decoded instruction to our list
		 */
		*inst_count += 1;
		ilist = xrealloc (ilist, *inst_count * sizeof (ilist[0]));
		memcpy (&ilist[*inst_count - 1], &inst_s, sizeof (inst_s));
	}

	/* in case we collected enough instructions, return the list, else
	 * trace the bblockes back up when possible. for more than one backref
	 * we cannot tell which lead to here, so return less than the requested
	 * number of instructions
	 */
	if (*inst_count == backcount)
		return (ilist);

	brefs = ia32_func_bblock_backrefs (&brefs_count, root, cur);
	if (brefs_count != 1) {
		if (brefs != NULL)
			free (brefs);

		return (ilist);
	}

	/* recurse to collect the instructions of the last bblock
	 */
	assert (backcount > *inst_count);
	sub_ilist = ia32_func_inst_traceup (backcount - *inst_count,
		&sub_ilist_count, func, root, brefs[0], brefs[0]->end);

	assert (sub_ilist != NULL && sub_ilist_count > 0);
	/* merge the sublist to the front of the main list
	 */
	ilist = xrealloc (ilist,
		(*inst_count + sub_ilist_count) * sizeof (ilist[0]));
	memmove (&ilist[sub_ilist_count], &ilist[0],
		*inst_count * sizeof (ilist[0]));
	memcpy (&ilist[0], sub_ilist, sub_ilist_count * sizeof (ilist[0]));
	free (sub_ilist);

	*inst_count += sub_ilist_count;

	return (ilist);
}


unsigned int
ia32_func_inst_count (ia32_function *func, unsigned int vstart,
	unsigned int vend)
{
	ia32_instruction *	inst,
				inst_s;
	unsigned char *		mem;
	unsigned int		icount,
				cur_vaddr;


	/* exclude traced functions from range checking
	 */
	if (func->start != func->end && func->traced_bounds == 0) {
		assert (vstart >= func->start && vend <= func->end);
	}

	cur_vaddr = vstart;

	for (icount = 0 ; cur_vaddr < vend ; ++icount) {
		mem = &func->mem[cur_vaddr - func->start];
		inst = ia32_decode_instruction (mem, &inst_s);

		assert (inst != NULL);
		cur_vaddr += inst->length;
	}

	return (icount);
}


ia32_bblock **
ia32_func_bblock_backrefs (int *br_count, ia32_bblock *root, ia32_bblock *cur)
{
	ia32_bblock **	btl;	/* list of all bblockes */
	unsigned int	btl_len,/* length of this list */
			n,	/* main walker */
			ebn,	/* end bblock walker */
			aln;	/* add list walker */
	ia32_bblock **	list = NULL;
	ia32_bblock *	add;
	int		dupe;


	btl = ia32_br_get_all (root, &btl_len);
	*br_count = 0;

	/* the basic idea is this: walk all bblockes already processed and
	 * collect all bblockes that have forward references to the current
	 * bblock. avoid dupes, but leave the resulting list unsorted.
	 */
	for (n = 0 ; n < btl_len ; ++n) {
		if (btl[n] == cur)
			continue;

		add = NULL;
		for (ebn = 0 ; ebn < btl[n]->endbr_count ; ++ebn) {
			if (btl[n]->endbr[ebn] == cur) {
				add = btl[n];
				break;
			}
		}

		if (add == NULL)
			continue;

		dupe = 0;
		for (aln = 0 ; aln < *br_count ; ++aln)
			if (list[aln] == add)
				dupe = 1;

		/* when its already in the backref list, skip
		 */
		if (dupe)
			continue;

		*br_count += 1;
		list = xrealloc (list, *br_count * sizeof (ia32_bblock *));
		list[*br_count - 1] = add;
	}

	free (btl);

	return (list);
}


ia32_function *
ia32_func_list_find_bymem (ia32_function **flist, unsigned int flist_count,
	unsigned char *memstart)
{
	unsigned int	n;


	for (n = 0 ; n < flist_count ; ++n) {
		if (flist[n]->mem == memstart && flist[n]->is_copy == 0)
			return (flist[n]);
	}

	return (NULL);
}


ia32_function *
ia32_func_list_find_bystart (ia32_function **flist, unsigned int flist_count,
	unsigned int start)
{
	unsigned int	n;


	for (n = 0 ; n < flist_count ; ++n) {
		if (flist[n]->start == start && flist[n]->is_copy == 0)
			return (flist[n]);
	}

	return (NULL);
}


ia32_function *
ia32_func_list_find_byname (ia32_function **flist, unsigned int flist_count,
	char *fname)
{
	unsigned int	n;


	for (n = 0 ; n < flist_count ; ++n) {
		if (flist[n]->name == NULL)
			continue;

		if (strcmp (fname, flist[n]->name) == 0)
			return (flist[n]);
	}

	return (NULL);
}


int
ia32_func_list_find_index (ia32_function **flist, unsigned int flist_count,
	ia32_function *func)
{
	unsigned int	n;


	for (n = 0 ; n < flist_count ; ++n)
		if (flist[n] == func)
			return (n);

	return (-1);
}


void
ia32_func_list_dump (ia32_function **flist, unsigned int flist_count)
{
	unsigned int	n;


	for (n = 0 ; n < flist_count ; ++n) {
		printf ("%-32s | 0x%08x - 0x%08x | @ 0x%08x\n",
			flist[n]->name, flist[n]->start, flist[n]->end,
			(unsigned int) flist[n]->mem);
	}
}


void
ia32_graphviz_func_out (FILE *fp, ia32_function **flist,
	unsigned int flist_count, elf_reloc_list *reloc_text)
{
	ia32_graphviz_func_out_2 (fp, flist, flist_count, NULL, reloc_text);
}


static void
ia32_graphviz_func_out_2 (FILE *fp, ia32_function **flist,
	unsigned int flist_count, int *flist_consider,
	elf_reloc_list *reloc_text)
{
	char **		ext_list = NULL;
	unsigned int	ext_list_count = 0;
	char *		ext_current;
	unsigned int	ext_wlk;

	unsigned int	n,
			i;


	fprintf (fp, "digraph functiondeps {\n\n");
	fprintf (fp, "\tnode [\n");
	fprintf (fp, "\t\tstyle = filled\n");
	fprintf (fp, "\t\tshape = \"record\"\n");
	fprintf (fp, "\t\tfillcolor = \"lightskyblue\"\n");
	fprintf (fp, "\t];\n\n");

	for (n = 0 ; n < flist_count ; ++n) {
		if (flist_consider != NULL && flist_consider[n] == 0)
			continue;

		fprintf (fp, "\t\"%s\" [\n", flist[n]->name);
		fprintf (fp, "\t\tlabel = \"{ <fi> | %s | <fo> }\"\n",
			flist[n]->name);
		fprintf (fp, "\t];\n");
	}

	for (n = 0 ; n < flist_count ; ++n) {
		if (flist_consider != NULL && flist_consider[n] == 0)
			continue;

		for (i = 0 ; i < flist[n]->func_xref_count ; ++i) {
			ext_current = NULL;

			switch (flist[n]->func_xref[i]->to_type) {
			case (IA32_XREF_FUNCEXTERN):
				ext_current = ia32_graphviz_func_out_extern
					(fp, flist[n],
					flist[n]->func_xref[i],
					reloc_text);
				break;
			case (IA32_XREF_FUNCTION):
				ia32_graphviz_func_out_intern (fp,
					flist[n], flist[n]->func_xref[i],
					flist, flist_count, flist_consider);
				break;
			default:
				break;
			}

			if (ext_current == NULL)
				continue;

			for (ext_wlk = 0 ; ext_wlk < ext_list_count ; ++ext_wlk) {
				if (strcmp (ext_list[ext_wlk], ext_current) == 0)
					break;
			}

			if (ext_wlk < ext_list_count)
				continue;

			ext_list_count += 1;
			ext_list = xrealloc (ext_list,
				ext_list_count * sizeof (char *));
			ext_list[ext_list_count - 1] = ext_current;
		}
	}

	/* output all external references found while dumping normal references
	 */
	if (ext_list_count > 0 && ia32_graphviz_align_undefined)
		fprintf (fp, "\tsubgraph cluster_undefined {\n"
			"\t\tlabel = \"undefined\";\n\n");

	for (ext_wlk = 0 ; ext_wlk < ext_list_count ; ++ext_wlk) {
		fprintf (fp, "\t\t\"%s\" [\n", ext_list[ext_wlk]);
		fprintf (fp, "\t\t\tlabel = \"{ <fi> | %s | <fo> }\"\n",
			ext_list[ext_wlk]);
		fprintf (fp, "\t\t\tfillcolor = \"red\"\n");
		fprintf (fp, "\t\t];\n");
	}

	if (ext_list_count > 0 && ia32_graphviz_align_undefined)
		fprintf (fp, "\t}\n");

	free (ext_list);

	fprintf (fp, "}\n");

	return;
}


static char *
ia32_graphviz_func_out_extern (FILE *fp, ia32_function *func,
	ia32_xref *xr, elf_reloc_list *reloc_text)
{
	unsigned int	from_addr;
	elf_reloc *	rel;
	char *		ext_name;


	if (reloc_text == NULL)
		return (NULL);

	/* look up relocation entry for reference source address, then try
	 * to pull the symbol table entry
	 */
	from_addr = xr->from + xr->addend + func->start;
	rel = elf_reloc_list_lookup (reloc_text, from_addr);
	if (rel == NULL)
		return (NULL);

	ext_name = "???";
	if (rel->sym->name != NULL)
		ext_name = rel->sym->name;

	ia32_debug (IA32_DEBUG, "0x%08x: %s -> %s\n", from_addr,
		func->name, ext_name);
	fprintf (fp, "\t\"%s\":fo -> \"%s\":fi;\n",
		func->name, ext_name);

	return (ext_name);
}


static void
ia32_graphviz_func_out_intern (FILE *fp, ia32_function *func, ia32_xref *xr,
	ia32_function **flist, unsigned int flist_count,
	int *flist_consider)
{
	char *		dname;	/* destination reference */
	ia32_function *	dest_func;


	if (func == NULL)
		return;

	if (flist_consider != NULL &&
		flist_consider[ia32_func_list_find_index (flist,
			flist_count, func)] == 0)
	{
		return;
	}

	dest_func = xr->to_data;
	assert (dest_func != NULL);
	dname = dest_func->name;

	if (dname != NULL) {
		ia32_debug (IA32_DEBUG, "          : %s -> %s\n",
			func->name, dname);
		fprintf (fp, "\t\"%s\":fo -> \"%s\":fi;\n",
			func->name, dname);
	}

	return;
}


void
ia32_graphviz_func_out_calltree (FILE *fp, ia32_function **flist,
	unsigned int flist_count, ia32_function *interest)
{
	int			int_idx;
	int *			int_arr;

	ia32_function *		func;
	unsigned int		n_m,
				n,
				i;


	assert (interest != NULL);
	int_idx = ia32_func_list_find_index (flist, flist_count, interest);
	assert (int_idx != -1);

	int_arr = xcalloc (flist_count, sizeof (int));
	int_arr[int_idx] = 1;

	/* do a little by brute force,
	 * FIXME: use a more elegant way ;)
	 */
#if 0
	for (n_m = 0 ; n_m < (flist_count * flist_count) ; ++n_m) {
#endif
	for (n_m = 0 ; n_m < (32 * flist_count) ; ++n_m) {
		n = n_m % flist_count;

		if (int_arr[n] != 0)
			continue;

		for (i = 0 ; i < flist[n]->func_xref_count ; ++i) {
			unsigned int	sub_idx;

			if (flist[n]->func_xref[i]->to_type !=
				IA32_XREF_FUNCTION)
				continue;

			func = (ia32_function *) flist[n]->func_xref[i]->to_data;
			if (func == NULL)
				continue;

			sub_idx = ia32_func_list_find_index (flist,
				flist_count, func);
			if (int_arr[sub_idx] == 1)
				int_arr[n] = 1;
		}
	}

	ia32_graphviz_func_out_2 (fp, flist, flist_count, int_arr, NULL);

	free (int_arr);
}


#ifdef	DUPE_DEBUG
void
dupe_alloc (unsigned int max_addr)
{
	addr_arr = xcalloc (max_addr, sizeof (unsigned int));
	addr_max = max_addr;
}

void
dupe_free (void)
{
	if (addr_arr != NULL)
		free (addr_arr);

	addr_max = 0;
}


void
dupe_check (unsigned int addr)
{
	/* not to use dupe check for runtime
	 */
	if (dupe_check_enabled == 0 || addr_max == 0)
		return;

	assert (addr < addr_max);
	if (addr_arr[addr] != 0) {
		ia32_debug (IA32_FATAL, "FATAL: (dupe_check) address access "
			"(%d) dupe at 0x%08x, dumping\n", addr_ac, addr);
		ia32_debug (IA32_FATAL, "\t%1d %1d %1d %1d %1d %1d %1d %1d\n",
			addr_arr[addr + 0], addr_arr[addr + 1],
			addr_arr[addr + 2], addr_arr[addr + 3],
			addr_arr[addr + 4], addr_arr[addr + 5],
			addr_arr[addr + 6], addr_arr[addr + 7]);

		assert (0);
	} else
		ia32_debug (IA32_DEBUG, "§ %d access: 0x%08x\n", addr_ac, addr);

	addr_arr[addr] = addr_ac;
	addr_ac += 1;
}
#endif