summaryrefslogtreecommitdiff
path: root/minix/lib/libmagicrt/magic.c
blob: 553b3d42771abb6dc781ee04e00f9d232ba2f28f (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
#include <magic.h>
#include <magic_mem.h>
#include <magic_asr.h>
#include <magic_eval.h>
#include <magic_analysis.h>
#include <magic_splay_tree.h>
#include <stdarg.h>
#if MAGIC_MEM_USAGE_OUTPUT_CTL
#include <common/util/time.h>
#endif

/* Workaround for extern-only structs. */
#ifndef __MINIX
#include <stdio.h>
EXTERN FILE *stdout;
PUBLIC FILE **UNUSED(_____magic_instr_FILE_unused) = &stdout;

#include <ncurses.h>
PUBLIC WINDOW *UNUSED(_____magic_instr_WINDOW_unused);
#endif

#include <netinet/in.h>
PUBLIC struct in6_addr *UNUSED(_____magic_instr_in6_addr_unused);

/* Magic printf. */
MAGIC_VAR printf_ptr_t _magic_printf = MAGIC_PRINTF_DEFAULT;

/* Magic lock primitives. */
PUBLIC magic_lock_t magic_dsentry_lock = NULL;
PUBLIC magic_unlock_t magic_dsentry_unlock = NULL;
PUBLIC void *magic_dsentry_lock_args = NULL;
PUBLIC void *magic_dsentry_unlock_args = NULL;

PUBLIC magic_lock_t magic_dfunction_lock = NULL;
PUBLIC magic_unlock_t magic_dfunction_unlock = NULL;
PUBLIC void *magic_dfunction_lock_args = NULL;
PUBLIC void *magic_dfunction_unlock_args = NULL;

PUBLIC magic_lock_t magic_dsodesc_lock = NULL;
PUBLIC magic_unlock_t magic_dsodesc_unlock = NULL;
PUBLIC void *magic_dsodesc_lock_args = NULL;
PUBLIC void *magic_dsodesc_unlock_args = NULL;

PUBLIC magic_lock_t magic_mpdesc_lock = NULL;
PUBLIC magic_unlock_t magic_mpdesc_unlock = NULL;
PUBLIC void *magic_mpdesc_lock_args = NULL;
PUBLIC void *magic_mpdesc_unlock_args = NULL;

/* Magic vars references. */
MAGIC_VAR struct _magic_vars_t _magic_vars_buff = {

    /* Address Space Randomization (ASRPass) */
    0, /* asr_seed */
    0, /* asr_heap_map_do_permutate */
    0, /* asr_heap_max_offset */
    0, /* asr_heap_max_padding */
    0, /* asr_map_max_offset_pages */
    0, /* asr_map_max_padding_pages */

    /* Runtime flags. */
    0,    /* no_mem_inst */

    /* Magic type array. */
    NULL, /* types */
    0,    /* types_num */
    0,    /* types_next_id */

    /* Magic function array. */
    NULL, /* functions */
    0,    /* functions_num */
    0,    /* functions_next_id */

    /* Magic state entry array. */
    NULL, /* sentries */
    0,    /* sentries_num */
    0,    /* sentries_str_num */
    0,    /* sentries_next_id */

    /* Magic dynamic state index array. */
    NULL, /* dsindexes */
    0,    /* dsindexes_num */

    /* Magic dynamic state entry list. */
    NULL, /* first_dsentry */
    0,    /* num_dead_sentries */
    0,    /* size_dead_dsentries */

    /* Magic memory pool dynamic state entry list. */
    NULL, /* first_mempool_dsentry */

    /* Magic dynamic function list. */
    NULL, /* first_dfunction */
    NULL, /* last_dfunction */
    0,    /* dfunctions_num */

    /* Magic SO library descriptor list. */
    NULL, /* first_sodesc */
    NULL, /* last_sodesc */
    0,    /* sodescs_num */

    /* Magic DSO library descriptor list. */
    NULL, /* first_dsodesc */
    NULL, /* last_dsodesc */
    0,    /* dsodescs_num */

    /* Magic stack-related variables. */
    NULL, /* first_stack_dsentry */
    NULL, /* last_stack_dsentry */

    /* Magic memory ranges */

    { (void*) ULONG_MAX, (void*) 0 }, /* *null_range[2] */
    {0,0}, /* *data_range[2] */
    {0,0}, /* *heap_range[2] */
    {0,0}, /* *map_range[2] */
    {0,0}, /* *shm_range[2] */
    {0,0}, /* *stack_range[2] */
    {0,0}, /* *text_range[2] */

    {0,0}, /* *sentry_range[2] */
    {0,0}, /* *function_range[2] */
    {0,0}, /* *dfunction_range[2] */

    NULL, /* *heap_start */
    NULL, /* *heap_end */
    1,     /* update_dsentry_ranges */
    1,     /* update_dfunction_ranges */

#ifdef __MINIX
    { { NULL, 0 } }, /* unmap_mem */
#endif

    NULL,  /* sentry_rl_buff */
    0,     /* sentry_rl_buff_offset */
    0,     /* sentry_rl_buff_size */
    NULL,  /* sentry_rl_index */

    NULL,  /* sentry_hash_buff */
    0,     /* sentry_hash_buff_offset */
    0,     /* sentry_hash_buff_size */
    NULL,  /* sentry_hash_head */

    NULL,  /* function_hash_buff */
    0,     /* function_hash_buff_offset */
    0,     /* function_hash_buff_size */
    NULL,  /* function_hash_head */

    0      /* fake_malloc */
};

PUBLIC struct _magic_vars_t *_magic_vars = &_magic_vars_buff;

/* Magic void ptr and array (force at the least 1 void* and 1 void array in the list of globals). */
PUBLIC void* MAGIC_VOID_PTR = NULL;
PUBLIC char MAGIC_VOID_ARRAY[1];

/* Magic special types. */
MAGIC_VAR struct _magic_type *MAGIC_VOID_PTR_TYPE = NULL;
MAGIC_VAR struct _magic_type *MAGIC_VOID_PTR_INT_CAST_TYPE = NULL;
MAGIC_VAR struct _magic_type *MAGIC_VOID_ARRAY_TYPE = NULL;
MAGIC_VAR struct _magic_type *MAGIC_PTRINT_TYPE = NULL;
MAGIC_VAR struct _magic_type *MAGIC_PTRINT_ARRAY_TYPE = NULL;

/* Magic annotations. */
MAGIC_VAR VOLATILE int MAGIC_CALL_ANNOTATION_VAR;

/* Magic status variables. */
PUBLIC int magic_init_done = 0;
PUBLIC int magic_libcommon_active = 0;
PUBLIC int magic_lookup_nested_dsentries = 1;
PUBLIC int magic_allow_dead_dsentries = MAGIC_ALLOW_DEAD_DSENTRIES_DEFAULT;
PUBLIC int magic_ignore_dead_dsentries = 0;
PUBLIC int magic_mmap_dsentry_header_prot = PROT_READ | PROT_WRITE;
MAGIC_VAR int _magic_enabled = 0;
MAGIC_VAR int _magic_checkpoint_enabled = 0;
MAGIC_VAR int _magic_lazy_checkpoint_enabled = 0;

/* Magic out-of-band dsentries. */
PUBLIC struct _magic_obdsentry _magic_obdsentries[MAGIC_MAX_OBDSENTRIES];

/* Pool management data. */
PUBLIC struct _magic_mpdesc _magic_mpdescs[MAGIC_MAX_MEMPOOLS];

/* Magic page size. */
PUBLIC unsigned long magic_sys_pagesize = 0;

/* Private variables. */
PUBLIC int magic_type_str_print_style = MAGIC_TYPE_STR_PRINT_STYLE_DEFAULT;
PRIVATE THREAD_LOCAL const struct _magic_type* magic_nested_types[MAGIC_MAX_RECURSIVE_TYPES] = {0};
PRIVATE THREAD_LOCAL const struct _magic_type* magic_nested_types2[MAGIC_MAX_RECURSIVE_TYPES] = {0};
PRIVATE THREAD_LOCAL unsigned magic_level = 0;
PRIVATE THREAD_LOCAL unsigned magic_counter;
PRIVATE THREAD_LOCAL struct _magic_dsentry magic_dsentry_buff;
PRIVATE THREAD_LOCAL struct _magic_dfunction magic_dfunction_buff;

/* Magic default stubs. */
PUBLIC struct _magic_type magic_default_type = {
    0, "", NULL, 0, "", 0, 0, NULL, NULL, NULL, NULL, NULL, MAGIC_TYPE_OPAQUE, 0, 0, NULL
};

PUBLIC struct _magic_dsentry magic_default_dsentry = {
    MAGIC_DSENTRY_MNUM, /* magic_number */
    "", /* parent_name */
    { 0 }, /* name_ext_buff */
    { 0, "", NULL, MAGIC_STATE_DYNAMIC, NULL, NULL }, /* sentry */
    { 0, "", NULL, 0, "", 0, 0, NULL, NULL, NULL, NULL, NULL, MAGIC_TYPE_ARRAY, MAGIC_TYPE_IS_ROOT|MAGIC_TYPE_DYNAMIC, 0, NULL }, /* type */
    { NULL }, /* type_array */
#if MAGIC_DSENTRY_ALLOW_PREV
    NULL, /* prev */
#endif
    NULL, /* next */
    NULL, /* next_mpool */
    NULL, /* next_mblock */
#ifndef __MINIX
    NULL, /* next_sobject */
    NULL, /* sobject_base_addr */
#endif
    NULL, /* ext */
    MAGIC_DSENTRY_MSTATE_ALIVE, /* magic_state */
    { { 0, 0 } }, /* alloc_flags */
    0 /* site_id */
};

PUBLIC struct _magic_dfunction magic_default_dfunction = {
    MAGIC_DFUNCTION_MNUM,
    "",
    { 0, "", NULL, MAGIC_STATE_DYNAMIC|MAGIC_STATE_TEXT|MAGIC_STATE_CONSTANT, NULL },
    NULL,
    NULL
};

PUBLIC struct _magic_type magic_default_ret_addr_type = {
    0, "", NULL, 0, "", sizeof(void*), 1, NULL, NULL, NULL, NULL, NULL, MAGIC_TYPE_POINTER, MAGIC_TYPE_IS_ROOT|MAGIC_TYPE_DYNAMIC|MAGIC_TYPE_INT_CAST|MAGIC_TYPE_STRICT_VALUE_SET, 0, NULL
};

/* Magic code reentrant flag. */
PRIVATE int magic_reentrant = 1;

/*===========================================================================*
 *      	            _magic_vars_addr                        *
 *===========================================================================*/
void *_magic_vars_addr()
{
    return _magic_vars;
}

/*===========================================================================*
 *      	            _magic_vars_size                        *
 *===========================================================================*/
size_t _magic_vars_size()
{
    return sizeof(struct _magic_vars_t);
}

/*===========================================================================*
 *                          magic_null_printf                                *
 *===========================================================================*/
PUBLIC int magic_null_printf(const char *format, ...)
{
  return 0;
}

#ifndef __MINIX
/*===========================================================================*
 *                           magic_err_printf                                *
 *===========================================================================*/
PUBLIC int magic_err_printf(const char *format, ...)
{
  va_list va;
  int ret;
  va_start(va, format);
  ret = vfprintf(stderr, format, va);
  va_end(va);

  return ret;
}
#endif

/*===========================================================================*
 *                           magic_set_printf                                *
 *===========================================================================*/
PUBLIC void magic_set_printf(printf_ptr_t func_ptr)
{
   assert(func_ptr);
   _magic_printf = func_ptr;
}

/*===========================================================================*
 *                           magic_get_printf                                *
 *===========================================================================*/
PUBLIC printf_ptr_t magic_get_printf(void)
{
   return _magic_printf;
}

/*===========================================================================*
 *                        magic_reentrant_enable                             *
 *===========================================================================*/
PUBLIC void magic_reentrant_enable(void)
{
    magic_reentrant = 1;
}
/*===========================================================================*
 *                        magic_reentrant_disable                            *
 *===========================================================================*/
PUBLIC void magic_reentrant_disable(void)
{
    magic_reentrant = 0;
}

/*===========================================================================*
 *                          magic_assert_failed                              *
 *===========================================================================*/
PUBLIC void __dead magic_assert_failed(const char *assertion, const char *file,
    const char *function, const int line)
{
    _magic_printf("Assertion '%s' failed in file %s, function %s(), line %d, pid %d\n",
        assertion, file, function, line, getpid());
    abort();
}

/*===========================================================================*
 *                         magic_get_sys_pagesize                            *
 *===========================================================================*/
PUBLIC unsigned long magic_get_sys_pagesize()
{
    if(!magic_sys_pagesize) {
        magic_sys_pagesize = SYS_PAGESIZE;
    }
    return magic_sys_pagesize;
}

/*===========================================================================*
 *                   magic_dsentry_set_lock_primitives                       *
 *===========================================================================*/
PUBLIC void magic_dsentry_set_lock_primitives(magic_lock_t lock,
    magic_unlock_t unlock, void *lock_args, void *unlock_args)
{
    assert(lock && unlock);
    magic_dsentry_lock = lock;
    magic_dsentry_unlock = unlock;
    magic_dsentry_lock_args = lock_args;
    magic_dsentry_unlock_args = unlock_args;
}

/*===========================================================================*
 *                   magic_dfunction_set_lock_primitives                     *
 *===========================================================================*/
PUBLIC void magic_dfunction_set_lock_primitives(magic_lock_t lock,
    magic_unlock_t unlock, void *lock_args, void *unlock_args)
{
    assert(lock && unlock);
    magic_dfunction_lock = lock;
    magic_dfunction_unlock = unlock;
    magic_dfunction_lock_args = lock_args;
    magic_dfunction_unlock_args = unlock_args;
}

/*===========================================================================*
 *                    magic_dsodesc_set_lock_primitives                      *
 *===========================================================================*/
PUBLIC void magic_dsodesc_set_lock_primitives(magic_lock_t lock,
    magic_unlock_t unlock, void *lock_args, void *unlock_args)
{
    assert(lock && unlock);
    magic_dsodesc_lock = lock;
    magic_dsodesc_unlock = unlock;
    magic_dsodesc_lock_args = lock_args;
    magic_dsodesc_unlock_args = unlock_args;
}

/*===========================================================================*
 *                 magic_mpdesc_set_lock_primitives                          *
 *===========================================================================*/
PUBLIC void magic_mpdesc_set_lock_primitives(magic_lock_t lock,
    magic_unlock_t unlock, void *lock_args, void *unlock_args)
{
    assert(lock && unlock);
    magic_mpdesc_lock = lock;
    magic_mpdesc_unlock = unlock;
    magic_mpdesc_lock_args = lock_args;
    magic_mpdesc_unlock_args = unlock_args;
}

/*===========================================================================*
 *                            magic_types_init                               *
 *===========================================================================*/
PRIVATE void magic_types_init()
{
    static struct _magic_type _magic_void_ptr_type_buff;
    static struct _magic_type _magic_void_array_type_buff;
    static struct _magic_type *_magic_void_array_type_contained_types[1];
    static struct _magic_type _magic_ptrint_type_buff;
    static const char* _magic_ptrint_type_name = "ptrint";
    static char _magic_ptrint_type_str_buff[8];
    static struct _magic_type _magic_ptrint_array_type_buff;
    static struct _magic_type *_magic_ptrint_array_type_contained_types[1];

    assert(MAGIC_VOID_PTR_TYPE);
    assert(MAGIC_VOID_PTR_TYPE->size == sizeof(void*));
    assert(MAGIC_VOID_TYPE->size == sizeof(char));

    MAGIC_VOID_PTR_INT_CAST_TYPE = &_magic_void_ptr_type_buff;
    *MAGIC_VOID_PTR_INT_CAST_TYPE = *MAGIC_VOID_PTR_TYPE;
    MAGIC_VOID_PTR_INT_CAST_TYPE->flags |= MAGIC_TYPE_INT_CAST;

    MAGIC_VOID_ARRAY_TYPE = &_magic_void_array_type_buff;
    *MAGIC_VOID_ARRAY_TYPE = magic_default_type;
    MAGIC_TYPE_ARRAY_CREATE_FROM_N(MAGIC_VOID_ARRAY_TYPE, MAGIC_VOID_TYPE,
        _magic_void_array_type_contained_types, 1);

    MAGIC_PTRINT_TYPE = &_magic_ptrint_type_buff;
    *MAGIC_PTRINT_TYPE = magic_default_type;
    MAGIC_TYPE_INT_CREATE(MAGIC_PTRINT_TYPE, MAGIC_VOID_PTR_TYPE->size,
        _magic_ptrint_type_name, _magic_ptrint_type_str_buff);

    MAGIC_PTRINT_ARRAY_TYPE = &_magic_ptrint_array_type_buff;
    *MAGIC_PTRINT_ARRAY_TYPE = magic_default_type;
    MAGIC_TYPE_ARRAY_CREATE_FROM_N(MAGIC_PTRINT_ARRAY_TYPE, MAGIC_PTRINT_TYPE,
        _magic_ptrint_array_type_contained_types, 1);
}

/*===========================================================================*
 *                           magic_data_init                                 *
 *===========================================================================*/
MAGIC_FUNC void magic_data_init(void)
{
    MAGIC_FUNC_BODY();
}

/*===========================================================================*
 *                              magic_init                                   *
 *===========================================================================*/
PUBLIC void magic_init(void)
{
  unsigned i;

  if(magic_init_done || !_magic_enabled) {
     return;
  }

  /* Initialize magic data structures first. */
  magic_data_init();

  /* Initialize asr support. */
  magic_asr_init();

  /* Initialize eval support. */
  magic_eval_init();

  /* Initialize magic obdsentries. */
  memset(_magic_obdsentries, 0, MAGIC_MAX_OBDSENTRIES * sizeof(struct _magic_obdsentry));

  /* Initialize memory pool descriptors. */
  for (i = 0; i < MAGIC_MAX_MEMPOOLS; i++) {
      snprintf(_magic_mpdescs[i].name, sizeof(_magic_mpdescs[i].name), "%s%d%s", MAGIC_MEMPOOL_NAME_PREFIX, i, MAGIC_ALLOC_NAME_SUFFIX);
  }

  /* Initialize special types. */
  magic_types_init();

  /* Initialize magic ranges. */
  magic_ranges_init();

  /* Perform stack-related initialization. */
  magic_stack_init();

#if MAGIC_MEM_USAGE_OUTPUT_CTL
   /* Initialize CPU frequency - used for timestamp logging. */
   magic_cycles_per_ns = util_time_get_cycles_per_ns(1);
#endif

  /* Checks. */
  assert(magic_check_sentries() && "Bad sentries!");
  assert(magic_check_dsentries_safe() && "Bad dsentries!");

  /* All done. */
  magic_init_done = 1;
}

/*===========================================================================*
 *                          magic_do_check_dfunction                         *
 *===========================================================================*/
PRIVATE INLINE int magic_do_check_dfunction(struct _magic_dfunction *dfunction, int flags)
{
    struct _magic_function *function;
    int is_mnum_ok, is_flags_ok, is_prev_ok, is_next_ok;
    assert(dfunction && "NULL dfunction found!");
    function = MAGIC_DFUNCTION_TO_FUNCTION(dfunction);
    assert(function && "NULL function found!");
    is_mnum_ok = MAGIC_DFUNCTION_MNUM_OK(dfunction);
    if(!is_mnum_ok) {
        return FALSE;
    }
    is_flags_ok = ((function->flags & flags) == flags) && (function->flags & MAGIC_STATE_DYNAMIC) && (MAGIC_STATE_REGION(function) & MAGIC_STATE_TEXT);
    is_prev_ok = (dfunction->prev ? dfunction->prev->next && dfunction->prev->next == dfunction : dfunction == _magic_first_dfunction);
    is_next_ok = (dfunction->next ? dfunction->next->prev && dfunction->next->prev == dfunction : dfunction == _magic_last_dfunction);
    if(!is_flags_ok || !is_prev_ok || !is_next_ok) {
        _magic_printf("magic_do_check_dfunction: bad dfunction, checks: %d %d %d\n", is_flags_ok, is_prev_ok, is_next_ok);
        MAGIC_DFUNCTION_PRINT(dfunction, MAGIC_EXPAND_TYPE_STR);
        _magic_printf("\n");
        return FALSE;
    }
    return TRUE;
}

/*===========================================================================*
 *                            magic_check_dfunction                          *
 *===========================================================================*/
PUBLIC int magic_check_dfunction(struct _magic_dfunction *dfunction, int flags)
{
    int check;
    check = magic_do_check_dfunction(dfunction, flags);
    if(!check) {
        return FALSE;
    }

#if MAGIC_CHECK_LEVEL == 2
    check = magic_check_dfunctions();
    if(!check) {
        _magic_printf("magic_check_dfunction: bad other dfunction\n");
        return FALSE;
    }
#endif

    return TRUE;
}

/*===========================================================================*
 *                          magic_check_dfunctions                           *
 *===========================================================================*/
PUBLIC int magic_check_dfunctions()
{
    int magic_dfunctions_found = 0;
    struct _magic_dfunction* dfunction = NULL;
    struct _magic_function* function = NULL;
    int ret, check = TRUE;

    MAGIC_DFUNCTION_FUNC_ITER(_magic_first_dfunction, dfunction, function,
        ret = magic_do_check_dfunction(dfunction, 0);
        if(ret == FALSE) {
            check = FALSE;
        }
        magic_dfunctions_found++;
    );
    if(magic_dfunctions_found != _magic_dfunctions_num) {
        _magic_printf("magic_check_dfunctions: magic_dfunctions_found=%d != _magic_dfunctions_num%d\n", magic_dfunctions_found, _magic_dfunctions_num);
        check = FALSE;
    }
    if(dfunction != _magic_last_dfunction) {
        _magic_printf("magic_check_dfunctions: dfunction=0x%08x != _magic_last_dfunction=0x%08x\n", dfunction, _magic_last_dfunction);
        check = FALSE;
    }
    return check;
}

/*===========================================================================*
 *                       magic_check_dfunctions_safe                         *
 *===========================================================================*/
PUBLIC int magic_check_dfunctions_safe()
{
    int ret;
    MAGIC_DFUNCTION_LOCK();
    ret = magic_check_dfunctions();
    MAGIC_DFUNCTION_UNLOCK();
    return ret;
}

/*===========================================================================*
 *                         magic_print_dfunction                             *
 *===========================================================================*/
PUBLIC void magic_print_dfunction(struct _magic_dfunction *dfunction)
{
    MAGIC_DFUNCTION_PRINT(dfunction, MAGIC_EXPAND_TYPE_STR);
}

/*===========================================================================*
 *                         magic_print_dfunctions                            *
 *===========================================================================*/
PUBLIC void magic_print_dfunctions()
{
    int magic_dfunctions_found = 0;
    struct _magic_dfunction* dfunction;
    struct _magic_function* function;

    _magic_printf("magic_print_dfunctions: Printing %d functions\n", _magic_dfunctions_num);
    MAGIC_DFUNCTION_FUNC_ITER(_magic_first_dfunction, dfunction, function,
        MAGIC_DFUNCTION_PRINT(dfunction, MAGIC_EXPAND_TYPE_STR);
        _magic_printf("\n");
        magic_dfunctions_found++;
    );
    if(magic_dfunctions_found != _magic_dfunctions_num) {
        _magic_printf("magic_print_dfunctions: magic_dfunctions_found=%d != _magic_dfunctions_num%d\n", magic_dfunctions_found, _magic_dfunctions_num);
    }
}

/*===========================================================================*
 *                      magic_print_dfunctions_safe                          *
 *===========================================================================*/
PUBLIC void magic_print_dfunctions_safe()
{
    MAGIC_DFUNCTION_LOCK();
    magic_print_dfunctions();
    MAGIC_DFUNCTION_UNLOCK();
}

/*===========================================================================*
 *                             magic_copy_dfunction                          *
 *===========================================================================*/
PUBLIC void magic_copy_dfunction(struct _magic_dfunction *dfunction,
    struct _magic_dfunction *dst_dfunction)
{
    /* Raw copy. */
    memcpy(dst_dfunction, dfunction, sizeof(struct _magic_dfunction));
}

/*===========================================================================*
 *                          magic_print_dsindex                              *
 *===========================================================================*/
PUBLIC void magic_print_dsindex(struct _magic_dsindex *dsindex)
{
    MAGIC_DSINDEX_PRINT(dsindex, MAGIC_EXPAND_TYPE_STR);
}

/*===========================================================================*
 *                         magic_print_dsindexes                             *
 *===========================================================================*/
PUBLIC void magic_print_dsindexes()
{
    int i;
    struct _magic_dsindex* dsindex;

    _magic_printf("magic_print_dsindexes: Printing %d indexes\n", _magic_dsindexes_num);
    for(i=0;i<_magic_dsindexes_num;i++) {
        dsindex = &_magic_dsindexes[i];
        MAGIC_DSINDEX_PRINT(dsindex, MAGIC_EXPAND_TYPE_STR);
        _magic_printf("\n");
    }
}

/*===========================================================================*
 *                           magic_do_check_dsentry                          *
 *===========================================================================*/
PRIVATE INLINE int magic_do_check_dsentry(struct _magic_dsentry *dsentry, int flags)
{
    struct _magic_sentry *sentry;
    int is_mnum_ok, is_mstate_ok, is_flags_ok;
    assert(dsentry && "NULL dsentry found!");
    sentry = MAGIC_DSENTRY_TO_SENTRY(dsentry);
    assert(sentry && "NULL sentry found!");
    is_mnum_ok = MAGIC_DSENTRY_MNUM_OK(dsentry);
    if(!is_mnum_ok) {
        _magic_printf("magic_do_check_dsentry: bad ~mnum %08x\n", ~(dsentry->magic_number));
        return FALSE;
    }
    is_mstate_ok = MAGIC_DSENTRY_MSTATE_OK(dsentry);
    is_flags_ok = ((sentry->flags & flags) == flags) && (sentry->flags & MAGIC_STATE_DYNAMIC) && MAGIC_STATE_REGION(sentry);
    if(!is_mstate_ok || !is_flags_ok) {
        _magic_printf("magic_do_check_dsentry: bad dsentry, checks: %d %d\n", is_mstate_ok, is_flags_ok);
        MAGIC_DSENTRY_PRINT(dsentry, MAGIC_EXPAND_TYPE_STR);
        _magic_printf("\n");
        return FALSE;
    }
    return TRUE;
}

/*===========================================================================*
 *                             magic_check_dsentry                           *
 *===========================================================================*/
PUBLIC int magic_check_dsentry(struct _magic_dsentry *dsentry, int flags)
{
#if MAGIC_CHECK_LEVEL >= 1
    int check;
    check = magic_do_check_dsentry(dsentry, flags);
    if(!check) {
        return FALSE;
    }
#endif

#if MAGIC_CHECK_LEVEL == 2
    check = magic_check_dsentries();
    if(!check) {
        _magic_printf("magic_check_dsentry: bad other dsentry\n");
        return FALSE;
    }
#endif

    return TRUE;
}

/*===========================================================================*
 *                          magic_check_dsentries                            *
 *===========================================================================*/
PUBLIC int magic_check_dsentries()
{
    struct _magic_dsentry *prev_dsentry, *dsentry;
    struct _magic_sentry* sentry;
    int ret, check = TRUE;

    MAGIC_DSENTRY_NESTED_ITER(_magic_first_dsentry, prev_dsentry, dsentry, sentry,
        ret = magic_do_check_dsentry(dsentry, 0);
        if(ret == FALSE) {
            check = FALSE;
        }
    );
    return check;
}

/*===========================================================================*
 *                       magic_check_dsentries_safe                          *
 *===========================================================================*/
PUBLIC int magic_check_dsentries_safe()
{
    int ret;
    MAGIC_DSENTRY_LOCK();
    ret = magic_check_dsentries();
    MAGIC_DSENTRY_UNLOCK();
    return ret;
}

/*===========================================================================*
 *                          magic_print_dsentry                              *
 *===========================================================================*/
PUBLIC void magic_print_dsentry(struct _magic_dsentry *dsentry)
{
    MAGIC_DSENTRY_PRINT(dsentry, MAGIC_EXPAND_TYPE_STR);
}

/*===========================================================================*
 *                         magic_print_dsentries                             *
 *===========================================================================*/
PUBLIC void magic_print_dsentries()
{
    struct _magic_dsentry *prev_dsentry, *dsentry;
    struct _magic_sentry* sentry;
    int magic_dsentries_num = 0;

    _magic_printf("magic_print_dsentries: Printing entries\n");
    MAGIC_DSENTRY_NESTED_ITER(_magic_first_dsentry, prev_dsentry, dsentry, sentry,
        MAGIC_DSENTRY_PRINT(dsentry, MAGIC_EXPAND_TYPE_STR);
        _magic_printf("\n");
        magic_dsentries_num++;
    );
    _magic_printf("magic_print_dsentries: %d entries found\n", magic_dsentries_num);
}

/*===========================================================================*
 *                       magic_print_dsentries_safe                          *
 *===========================================================================*/
PUBLIC void magic_print_dsentries_safe()
{
    MAGIC_DSENTRY_LOCK();
    magic_print_dsentries();
    MAGIC_DSENTRY_UNLOCK();
}

/*===========================================================================*
 *                             magic_copy_dsentry                            *
 *===========================================================================*/
PUBLIC void magic_copy_dsentry(struct _magic_dsentry *dsentry,
    struct _magic_dsentry *dst_dsentry)
{
    struct _magic_sentry *sentry;

    /* Raw copy. */
    memcpy(dst_dsentry, dsentry, sizeof(struct _magic_dsentry));

    /* Adjust pointers. */
    sentry = MAGIC_DSENTRY_TO_SENTRY(dsentry);
    if(sentry->type == &dsentry->type) {
        MAGIC_DSENTRY_TO_SENTRY(dst_dsentry)->type = &dst_dsentry->type;
        if(sentry->type->contained_types == dsentry->type_array) {
            MAGIC_DSENTRY_TO_SENTRY(dst_dsentry)->type->contained_types = dst_dsentry->type_array;
        }
    }
}

/*===========================================================================*
 *                         magic_print_sodesc                                *
 *===========================================================================*/
PUBLIC void magic_print_sodesc(struct _magic_sodesc *sodesc)
{
    MAGIC_SODESC_PRINT(sodesc);
}

/*===========================================================================*
 *                         magic_print_sodescs                               *
 *===========================================================================*/
PUBLIC void magic_print_sodescs()
{
    int magic_sodescs_found = 0;
    struct _magic_sodesc* sodesc;

    _magic_printf("magic_print_sodescs: Printing %d sodescs\n", _magic_sodescs_num);
    MAGIC_SODESC_ITER(_magic_first_sodesc, sodesc,
        MAGIC_SODESC_PRINT(sodesc);
        _magic_printf("\n");
        magic_sodescs_found++;
    );
    if(magic_sodescs_found != _magic_sodescs_num) {
        _magic_printf("magic_print_sodescs: magic_sodescs_found=%d != _magic_sodescs_num%d\n", magic_sodescs_found, _magic_sodescs_num);
    }
}

/*===========================================================================*
 *                         magic_print_dsodesc                               *
 *===========================================================================*/
PUBLIC void magic_print_dsodesc(struct _magic_dsodesc *dsodesc)
{
    MAGIC_DSODESC_PRINT(dsodesc);
}

/*===========================================================================*
 *                         magic_print_dsodescs                              *
 *===========================================================================*/
PUBLIC void magic_print_dsodescs()
{
    int magic_dsodescs_found = 0;
    struct _magic_dsodesc* dsodesc;

    _magic_printf("magic_print_dsodescs: Printing %d dsodescs\n", _magic_dsodescs_num);
    MAGIC_DSODESC_ITER(_magic_first_dsodesc, dsodesc,
        MAGIC_DSODESC_PRINT(dsodesc);
        _magic_printf("\n");
        magic_dsodescs_found++;
    );
    if(magic_dsodescs_found != _magic_dsodescs_num) {
        _magic_printf("magic_print_dsodescs: magic_dsodescs_found=%d != _magic_dsodescs_num%d\n", magic_dsodescs_found, _magic_dsodescs_num);
    }
}

/*===========================================================================*
 *                       magic_print_dsodescs_safe                           *
 *===========================================================================*/
PUBLIC void magic_print_dsodescs_safe()
{
    MAGIC_DSODESC_LOCK();
    magic_print_dsodescs();
    MAGIC_DSODESC_UNLOCK();
}

/*===========================================================================*
 *                           magic_print_sections                            *
 *===========================================================================*/
PUBLIC void magic_print_sections(void)
{
    _magic_printf("magic_print_sections: data=[0x%08x;0x%08x], ro=[0x%08x;0x%08x], text=[0x%08x;0x%08x], st_data=[0x%08x;0x%08x], st_ro=[0x%08x;0x%08x], st_text=[0x%08x;0x%08x]",
            (unsigned long) MAGIC_DATA_SECTION_START, (unsigned long) MAGIC_DATA_SECTION_END,
            (unsigned long) MAGIC_DATA_RO_SECTION_START, (unsigned long) MAGIC_DATA_RO_SECTION_END,
            (unsigned long) MAGIC_TEXT_SECTION_START, (unsigned long) MAGIC_TEXT_SECTION_END,
            (unsigned long) MAGIC_ST_DATA_SECTION_START, (unsigned long) MAGIC_ST_DATA_SECTION_END,
            (unsigned long) MAGIC_ST_DATA_RO_SECTION_START, (unsigned long) MAGIC_ST_DATA_RO_SECTION_END,
            (unsigned long) MAGIC_ST_TEXT_SECTION_START, (unsigned long) MAGIC_ST_TEXT_SECTION_END);
}

/*===========================================================================*
 *                   magic_mempool_sentry_lookup_by_addr                     *
 *===========================================================================*/
PUBLIC struct _magic_sentry* magic_mempool_sentry_lookup_by_range(void *addr, struct _magic_dsentry *dsentry_buff)
{
    struct _magic_dsentry *prev_dsentry, *dsentry;
    struct _magic_sentry* sentry;
    struct _magic_sentry* entry = NULL;
    void *start_address, *end_address;

    MAGIC_DSENTRY_LOCK();
    MAGIC_DSENTRY_MEMPOOL_ALIVE_ITER(_magic_first_mempool_dsentry, prev_dsentry, dsentry, sentry,
        start_address = sentry->address;
        end_address = (void*) (((char*)sentry->address) + sentry->type->size - 1);
        if(MAGIC_ADDR_IS_WITHIN(addr, start_address, end_address)) {
            if(dsentry_buff) {
                magic_copy_dsentry(dsentry, dsentry_buff);
                entry = MAGIC_DSENTRY_TO_SENTRY(dsentry_buff);
            }
            else {
                entry = sentry;
            }
            break;
        }
    );
    MAGIC_DSENTRY_UNLOCK();

    return entry;
}

/*===========================================================================*
 *                   magic_dsindex_lookup_by_name                            *
 *===========================================================================*/
PUBLIC struct _magic_dsindex*
magic_dsindex_lookup_by_name(const char *parent_name, const char *name)
{
    int i;
    struct _magic_dsindex* index = NULL;
    assert(parent_name && name);

    /* Scan all the indexes and return the one matching the provided names. */
    for(i=0;i<_magic_dsindexes_num;i++) {
        if(!strcmp(_magic_dsindexes[i].parent_name, parent_name)
            && !strcmp(_magic_dsindexes[i].name, name)) {
            index = &_magic_dsindexes[i];
            break;
        }
    }

    return index;
}

/*===========================================================================*
 *                          magic_dsentry_prev_lookup                        *
 *===========================================================================*/
PUBLIC struct _magic_dsentry* magic_dsentry_prev_lookup(struct _magic_dsentry* dsentry)
{
    struct _magic_dsentry *prev_dsentry, *it_dsentry;
    struct _magic_sentry *sentry;
    int found = 0;

#if MAGIC_DSENTRY_ALLOW_PREV
    return dsentry->prev;
#else
    MAGIC_DSENTRY_ITER(_magic_first_dsentry, prev_dsentry, it_dsentry, sentry,
        if(dsentry == it_dsentry) {
            found = 1;
            break;
        }
    );
    if(!found) {
        return (struct _magic_dsentry*) MAGIC_ENOPTR;
    }
    return prev_dsentry;
#endif
}

/*===========================================================================*
 *                      magic_mempool_dsentry_prev_lookup                    *
 *===========================================================================*/
PUBLIC struct _magic_dsentry* magic_mempool_dsentry_prev_lookup(struct _magic_dsentry* dsentry)
{
    struct _magic_dsentry *prev_dsentry, *it_dsentry;
    struct _magic_sentry *sentry;
    int found = 0;

    MAGIC_DSENTRY_MEMPOOL_ITER(_magic_first_mempool_dsentry, prev_dsentry, it_dsentry, sentry,
        if(dsentry == it_dsentry) {
            found = 1;
            break;
        }
    );
    if(!found) {
        return (struct _magic_dsentry*) MAGIC_ENOPTR;
    }
    return prev_dsentry;
}

/*===========================================================================*
 *                         magic_function_lookup_by_id                       *
 *===========================================================================*/
PUBLIC struct _magic_function* magic_function_lookup_by_id(_magic_id_t id,
    struct _magic_dfunction *dfunction_buff)
{
    struct _magic_function* entry = NULL;
    struct _magic_function* function;
    struct _magic_dfunction* dfunction;

    if(id <= 0) {
        return NULL;
    }

    /* O(1) ID lookup for functions. */
#if MAGIC_LOOKUP_FUNCTION
    if((int)id <= _magic_functions_num) {
        return &_magic_functions[id - 1];
    }
#endif

    /* O(N) ID lookup for dfunctions. */
#if MAGIC_LOOKUP_DFUNCTION
    MAGIC_DFUNCTION_LOCK();
    MAGIC_DFUNCTION_FUNC_ITER(_magic_first_dfunction, dfunction, function,
        if(function->id == id) {
            if(dfunction_buff) {
                magic_copy_dfunction(dfunction, dfunction_buff);
                entry = MAGIC_DFUNCTION_TO_FUNCTION(dfunction_buff);
            }
            else {
                entry = function;
            }
            break;
        }
    );
    MAGIC_DFUNCTION_UNLOCK();
#endif

    return entry;
}

/*===========================================================================*
 *                        magic_function_lookup_by_addr                      *
 *===========================================================================*/
PUBLIC struct _magic_function* magic_function_lookup_by_addr(void *addr,
    struct _magic_dfunction *dfunction_buff)
{
    int i;
    struct _magic_function *entry = NULL;
    struct _magic_function *function;
    struct _magic_dfunction *dfunction;

#if MAGIC_LOOKUP_FUNCTION_ALLOW_ADDR_HASH
    if (magic_function_hash_head) {
        return magic_function_lookup_by_addr_hash(addr, dfunction_buff);
    }
#endif

    /* Scan all the entries and return the one matching the provided address. */
#if MAGIC_LOOKUP_FUNCTION
    if (MAGIC_ADDR_IS_IN_RANGE(addr, magic_function_range)) {
        for (i = 0 ; i < _magic_functions_num ; i++) {
            if (_magic_functions[i].address == addr) {
                entry = &_magic_functions[i];
                break;
            }
        }
    }
#endif

#if MAGIC_LOOKUP_DFUNCTION
    MAGIC_DFUNCTION_LOCK();
    if(!MAGIC_ADDR_LOOKUP_USE_DFUNCTION_RANGES || magic_range_is_dfunction(addr)) {
        MAGIC_DFUNCTION_FUNC_ITER(_magic_first_dfunction, dfunction, function,
            if(function->address == addr) {
                if(dfunction_buff) {
                    magic_copy_dfunction(dfunction, dfunction_buff);
                    entry = MAGIC_DFUNCTION_TO_FUNCTION(dfunction_buff);
                }
                else {
                    entry = function;
                }
                break;
            }
        );
    }
    MAGIC_DFUNCTION_UNLOCK();
#endif

    return entry;
}

/*===========================================================================*
 *                       magic_function_lookup_by_name                       *
 *===========================================================================*/
PUBLIC struct _magic_function*
magic_function_lookup_by_name(const char *parent_name, const char *name)
{
    int i;
    struct _magic_function* entry = NULL;

    /* Scan all the entries and return the one matching the provided name(s). */
#if MAGIC_LOOKUP_FUNCTION
    for (i = 0 ; i < _magic_functions_num ; i++) {
        if (!strcmp(_magic_functions[i].name, name)) {
            if (!parent_name || !strcmp(MAGIC_FUNCTION_PARENT(&_magic_functions[i]), parent_name)) {
                entry = &_magic_functions[i];
                break;
            }
        }
    }
#endif

    return entry;
}

/*===========================================================================*
 *                         magic_function_hash_insert                        *
 *===========================================================================*/
PRIVATE void magic_function_hash_insert(struct _magic_function_hash **head,
    struct _magic_function_hash *elem)
{
    if (head != NULL) {
        struct _magic_function_hash *tmp;
        HASH_FIND_PTR(*head, &elem->key, tmp);
        /* Skip inserting this function if an identical one already exists. */
        if (tmp)
            return;
    }
/*
 * **** START UTHASH SPECIFIC DEFINITIONS ****
 */
#undef uthash_malloc
#undef uthash_free
#define uthash_malloc(size)             magic_function_hash_alloc(size)
#define uthash_free(addr, size)         magic_function_hash_dealloc(addr, size)
/*
 * Since we have a limited buffer, we need to stop bucket expansion when
 * reaching a certain limit.
 */
#undef uthash_expand_fyi
#define uthash_expand_fyi(tbl)                                                 \
    do {                                                                       \
        if (tbl->num_buckets == MAGIC_FUNCTION_ADDR_EST_MAX_BUCKETS) {         \
            _magic_printf("Warning! Function address hash maximum bucket "     \
                "number reached! Consider increasing "                         \
                "MAGIC_FUNCTION_ADDR_EST_MAX_BUCKETS, unless you are "         \
                "comfortable with the current performance.\n");                \
            tbl->noexpand = 1;                                                 \
        }                                                                      \
    } while(0);
/*
 * **** FINISH UTHASH SPECIFIC DEFINITIONS ****
 */
    HASH_ADD_PTR(*head, key, elem);
/*
 * **** START UTHASH DEFINITION REMOVAL ****
 */
#undef uthash_malloc
#undef uthash_free
#undef uthash_expand_fyi
/*
 * **** FINISH UTHASH DEFINITION REMOVAL ****
 */
}

/*===========================================================================*
 *                         magic_function_hash_build                         *
 *===========================================================================*/
PUBLIC void magic_function_hash_build(void *buff, size_t buff_size)
{
    /*
     * XXX:
     * Warning: this implementation is thread unsafe and also makes
     * magic_function_lookup_by_addr thread unsafe!
     */
    int i;
    struct _magic_dfunction *dfunction;
    struct _magic_function *function;
    struct _magic_function_hash *function_hash, *head;

    assert(buff && buff_size > 0);
    magic_function_hash_buff = buff;
    magic_function_hash_buff_offset = 0;
    magic_function_hash_buff_size = buff_size;

    head = NULL;

    /* Add all the functions to the hash. */
#if MAGIC_LOOKUP_FUNCTION
    for(i = 0 ; i < _magic_functions_num ; i++) {
        function_hash = (struct _magic_function_hash *)
            magic_function_hash_alloc(sizeof(struct _magic_function_hash));
        function = &_magic_functions[i];
        MAGIC_FUNCTION_TO_HASH_EL(function, function_hash);
        magic_function_hash_insert(&head, function_hash);
    }
#endif

    /* Add all the dfunctions to the hash. */
#if MAGIC_LOOKUP_DFUNCTION
    MAGIC_DFUNCTION_LOCK();
    MAGIC_DFUNCTION_FUNC_ITER(_magic_first_dfunction, dfunction, function,
        function_hash = (struct _magic_function_hash *)
            magic_function_hash_alloc(sizeof(struct _magic_function_hash));
        MAGIC_DFUNCTION_TO_HASH_EL(dfunction, function, function_hash);
        magic_function_hash_insert(&head, function_hash);
    );
    MAGIC_DFUNCTION_UNLOCK();
#endif
    magic_function_hash_head = (void *)head;
    assert(magic_function_hash_head);
}

/*===========================================================================*
 *                        magic_function_hash_destroy                        *
 *===========================================================================*/
PUBLIC void magic_function_hash_destroy(void)
{
    magic_function_hash_buff = NULL;
    magic_function_hash_buff_offset = 0;
    magic_function_hash_buff_size = 0;
    magic_function_hash_head = NULL;
}

/*===========================================================================*
 *                    magic_function_hash_estimate_buff_size                 *
 *===========================================================================*/
PUBLIC size_t magic_function_hash_estimate_buff_size(int functions_num)
{
    if (functions_num == 0) {
        functions_num = _magic_dfunctions_num;
        functions_num += _magic_functions_num;
    }

    return (functions_num * sizeof(struct _magic_function_hash)) +
        MAGIC_FUNCTION_ADDR_HASH_OVERHEAD;
}

/*===========================================================================*
 *                           magic_function_hash_alloc                       *
 *===========================================================================*/
PUBLIC void* magic_function_hash_alloc(size_t size)
{
    void *addr;

    assert(magic_function_hash_buff);
    assert(magic_function_hash_buff_offset + size <=
        magic_function_hash_buff_size);

    addr = (char*) magic_function_hash_buff + magic_function_hash_buff_offset;
    magic_function_hash_buff_offset += size;

    return addr;
}

/*===========================================================================*
 *                          magic_function_hash_dealloc                      *
 *===========================================================================*/
PUBLIC void magic_function_hash_dealloc(UNUSED(void *object), UNUSED(size_t sz))
{
    return;
}

/*===========================================================================*
 *                      magic_function_lookup_by_addr_hash                   *
 *===========================================================================*/
PUBLIC struct _magic_function *magic_function_lookup_by_addr_hash(
    void *addr, struct _magic_dfunction *dfunction_buff)
{
    /*
     * Warning: this implementation is thread unsafe!
     */
    struct _magic_function_hash *res, *head;
    head = (struct _magic_function_hash *) magic_function_hash_head;

    HASH_FIND_PTR(head, &addr, res);
    if (res == NULL)
        return NULL;

    if (MAGIC_STATE_FLAG(res->function, MAGIC_STATE_DYNAMIC) &&
        dfunction_buff != NULL) {
        magic_copy_dfunction(MAGIC_DFUNCTION_FROM_FUNCTION(res->function),
            dfunction_buff);
    }

    return res->function;
}

/*===========================================================================*
 *                         magic_type_lookup_by_name                         *
 *===========================================================================*/
PUBLIC struct _magic_type* magic_type_lookup_by_name(const char *name)
{
    int i;
    unsigned int j;
    struct _magic_type* entry = NULL;

    /* Scan all the entries and return the one matching the provided name. */
#if MAGIC_LOOKUP_TYPE
    for (i = 0 ; i < _magic_types_num ; i++) {
        if (!strcmp(_magic_types[i].name, name)) {
            entry = &_magic_types[i];
            break;
        }
        if (_magic_types[i].names) {
            for (j = 0 ; j < _magic_types[i].num_names ; j++) {
                if (!strcmp(_magic_types[i].names[j], name)) {
                    entry = &_magic_types[i];
                    break;
                }
            }
            if (entry) {
                break;
            }
        }
    }
#endif

    return entry;
}

/*===========================================================================*
 *                      magic_dsodesc_lookup_by_handle                       *
 *===========================================================================*/
PUBLIC struct _magic_dsodesc* magic_dsodesc_lookup_by_handle(void *handle)
{
    struct _magic_dsodesc* desc = NULL;
    struct _magic_dsodesc* dsodesc;

    /*
     * Scan all the descriptors and return the one matching the provided handle.
     * Note that there is no locking here. The caller has to explicitely call
     * MAGIC_DSODESC_LOCK/UNLOCK before/after invoking this function.
     */
    MAGIC_DSODESC_ITER(_magic_first_dsodesc, dsodesc,
        if(dsodesc->handle == handle) {
            desc = dsodesc;
            break;
        }
    );

    return desc;
}

/*===========================================================================*
 *                         magic_print_function                              *
 *===========================================================================*/
PUBLIC void magic_print_function(struct _magic_function *function)
{
    MAGIC_FUNCTION_PRINT(function, MAGIC_EXPAND_TYPE_STR);
}

/*===========================================================================*
 *                         magic_print_functions                             *
 *===========================================================================*/
PUBLIC void magic_print_functions()
{
    int i;
    struct _magic_function* function;

    _magic_printf("magic_print_functions: Printing %d entries\n", _magic_functions_num);
    for(i=0;i<_magic_functions_num;i++) {
        function = &_magic_functions[i];
        MAGIC_FUNCTION_PRINT(function, MAGIC_EXPAND_TYPE_STR);
        _magic_printf("\n");
    }
}

/*===========================================================================*
 *                           magic_print_type                                *
 *===========================================================================*/
PUBLIC void magic_print_type(const struct _magic_type* type)
{
    MAGIC_TYPE_PRINT(type, MAGIC_EXPAND_TYPE_STR);
}

/*===========================================================================*
 *                           magic_print_types                               *
 *===========================================================================*/
PUBLIC void magic_print_types()
{
    int i;
    struct _magic_type* type;

    _magic_printf("magic_print_types: Printing %d types\n", _magic_types_num);
    for(i=0;i<_magic_types_num;i++) {
        type = &_magic_types[i];
        MAGIC_TYPE_PRINT(type, MAGIC_EXPAND_TYPE_STR);
        _magic_printf("\n");
    }
}

/*===========================================================================*
 *                      magic_type_str_set_print_style                       *
 *===========================================================================*/
PUBLIC void magic_type_str_set_print_style(const int style)
{
    magic_type_str_print_style = style;
}

/*===========================================================================*
 *                      magic_type_str_get_print_style                       *
 *===========================================================================*/
PUBLIC int magic_type_str_get_print_style()
{
    return magic_type_str_print_style;
}

/*===========================================================================*
 *                       magic_type_get_nesting_level                        *
 *===========================================================================*/
PRIVATE INLINE int magic_type_get_nesting_level(const struct _magic_type* type,
    int level)
{
    int i;
    int nesting_level = -1;

    for(i=0;i<level;i++) {
        if(magic_nested_types[i] == type) {
            nesting_level = i;
            break;
        }
    }

    return nesting_level;
}

/*===========================================================================*
 *                            magic_type_str_print                           *
 *===========================================================================*/
PUBLIC void magic_type_str_print(const struct _magic_type* type)
{
    int num_contained_types;
    int is_empty_str = !type->type_str || !strcmp(type->type_str, "");
    int type_has_name = type->name && strcmp(type->name, "");
    int print_multi_names = (magic_type_str_print_style & MAGIC_TYPE_STR_PRINT_MULTI_NAMES) && MAGIC_TYPE_HAS_MULTI_NAMES(type);
    int print_ptr_name = (magic_type_str_print_style & MAGIC_TYPE_STR_PRINT_MULTI_NAMES) && !MAGIC_TYPE_HAS_MULTI_NAMES(type) && type_has_name;
    assert(magic_level < MAGIC_MAX_RECURSIVE_TYPES);

    if(magic_level == 0) {
        magic_counter = 0;
    }
    else if(magic_counter >= MAGIC_TYPE_STR_PRINT_MAX) {
        _magic_printf("%%");
        return;
    }
    else if(magic_level >= MAGIC_TYPE_STR_PRINT_MAX_LEVEL) {
        _magic_printf("%%");
        return;
    }

    if(MAGIC_TYPE_STR_PRINT_DEBUG) {
        _magic_printf("Entering level %d...\n", magic_level);
    }

    if(type->type_id == MAGIC_TYPE_OPAQUE) {
        _magic_printf("opaque");
        magic_counter += strlen("opaque");
        return;
    }

    num_contained_types = MAGIC_TYPE_NUM_CONTAINED_TYPES(type);
    if(num_contained_types == 0) {
        assert(!is_empty_str);
        if((magic_type_str_print_style & (MAGIC_TYPE_STR_PRINT_LLVM_TYPES|MAGIC_TYPE_STR_PRINT_SOURCE_TYPES)) == (MAGIC_TYPE_STR_PRINT_LLVM_TYPES|MAGIC_TYPE_STR_PRINT_SOURCE_TYPES)) {
            if(print_multi_names) {
                _magic_printf("%s/", type->type_str);
                magic_type_names_print(type);
                magic_counter += strlen(type->type_str)+1+strlen(type->name)*type->num_names;
            }
            else {
                _magic_printf("%s/%s", type->type_str, type->name);
                magic_counter += strlen(type->type_str)+1+strlen(type->name);
            }
        }
        else if(magic_type_str_print_style & MAGIC_TYPE_STR_PRINT_LLVM_TYPES) {
            _magic_printf(type->type_str);
            magic_counter += strlen(type->type_str);
        }
        else if(magic_type_str_print_style & MAGIC_TYPE_STR_PRINT_SOURCE_TYPES) {
            if(print_multi_names) {
                magic_type_names_print(type);
                magic_counter += strlen(type->name)*type->num_names;
            }
            else {
                _magic_printf(type->name);
                magic_counter += strlen(type->name);
            }
        }
        return;
    }

    if(type->type_id == MAGIC_TYPE_POINTER) {
        int nesting_level = magic_type_get_nesting_level(type, magic_level);
        if(nesting_level >= 0) {
            _magic_printf("\\%d", magic_level - nesting_level);
            magic_counter += 2;
            return;
        }
    }

    magic_nested_types[magic_level] = type;
    magic_level++;
    if(type->type_id == MAGIC_TYPE_POINTER) {
        magic_type_str_print(type->contained_types[0]);
        _magic_printf("*");
        magic_counter += 1;
        if(print_multi_names) {
            _magic_printf("|");
            magic_type_names_print(type);
            magic_counter += 1+strlen(type->name)*type->num_names;
        }
        else if(print_ptr_name) {
            _magic_printf("|");
            _magic_printf("%s", type->name);
            magic_counter += 1+strlen(type->name);
        }
    }
    else if(type->type_id == MAGIC_TYPE_ARRAY || type->type_id == MAGIC_TYPE_VECTOR) {
        int num_elements = type->num_child_types;
        char start_sep = type->type_id == MAGIC_TYPE_ARRAY ? '[' : '<';
        char end_sep = type->type_id == MAGIC_TYPE_ARRAY ? ']' : '>';
        _magic_printf("%c", start_sep);
        magic_counter += 1;
        if(MAGIC_TYPE_FLAG(type, MAGIC_TYPE_VARSIZE)) {
            _magic_printf(" (V) ");
            magic_counter += 5;
        }
        if(num_elements) {
            _magic_printf("%d x ", num_elements);
            magic_counter += 5;
        }
        magic_type_str_print(type->contained_types[0]);
        _magic_printf("%c", end_sep);
        magic_counter += 1;
    }
    else if(type->type_id == MAGIC_TYPE_STRUCT || type->type_id == MAGIC_TYPE_UNION) {
        int i;
        int skip_struct = type->type_id == MAGIC_TYPE_STRUCT && (magic_type_str_print_style & MAGIC_TYPE_STR_PRINT_SKIP_STRUCTS);
        int skip_union = type->type_id == MAGIC_TYPE_UNION && (magic_type_str_print_style & MAGIC_TYPE_STR_PRINT_SKIP_UNIONS);
        _magic_printf("{ ");
        magic_counter += 2;
        if(type->type_id == MAGIC_TYPE_UNION) {
            _magic_printf("(U) ");
            magic_counter += 4;
        }
        if(print_multi_names) {
            _magic_printf("$");
            magic_type_names_print(type);
            _magic_printf(" ");
            magic_counter += 2 + strlen(type->name)*type->num_names;
        }
        else {
            _magic_printf("$%s ", strcmp(type->name, "") ? type->name : "ANONYMOUS");
            magic_counter += 2 + strlen(strcmp(type->name, "") ? type->name : "ANONYMOUS");
        }
        assert(type->member_names);
        if(!skip_struct && !skip_union) {
            for(i=0;i<num_contained_types;i++) {
                if(i > 0) {
                    _magic_printf(", ");
                    magic_counter += 2;
                }
                if((magic_type_str_print_style & MAGIC_TYPE_STR_PRINT_MEMBER_NAMES)
                    && (!MAGIC_TYPE_STR_PRINT_MAX || magic_counter < MAGIC_TYPE_STR_PRINT_MAX)) {
                    assert(type->member_names[i] && strcmp(type->member_names[i], "") && "Invalid member name!");
                    _magic_printf("%s ", type->member_names[i]);
                    magic_counter += strlen(type->member_names[i])+1;
                }
                magic_type_str_print(type->contained_types[i]);
            }
        }
        _magic_printf(" }");
        magic_counter += 2;
   }
   else if(type->type_id == MAGIC_TYPE_FUNCTION) {
       int i;
       assert(num_contained_types > 0);
       magic_type_str_print(type->contained_types[0]);
       num_contained_types--;
       _magic_printf(" (");
       magic_counter += 2;
       for(i=0;i<num_contained_types;i++) {
           if(i > 0) {
               _magic_printf(", ");
               magic_counter += 2;
           }
           magic_type_str_print(type->contained_types[i+1]);
       }
       _magic_printf(")");
       magic_counter += 1;
   }
   else {
       _magic_printf("???[id=%d,child_types=%d,size=%zu]", type->type_id, type->num_child_types, type->size);
       magic_counter += 30;
   }
   magic_level--;
   if(MAGIC_TYPE_STR_PRINT_DEBUG) {
       _magic_printf("Exiting level %d...\n", magic_level);
   }
}

/*===========================================================================*
 *                         magic_type_values_print                           *
 *===========================================================================*/
PUBLIC void magic_type_values_print(const struct _magic_type* type)
{
    int i=0;

    if(!MAGIC_TYPE_HAS_VALUE_SET(type)) {
        return;
    }
    while(MAGIC_TYPE_HAS_VALUE(type, i)) {
        int value = MAGIC_TYPE_VALUE(type, i);
        _magic_printf("%s%d", (i==0 ? "" : ", "), value);
        i++;
    }
}

/*===========================================================================*
 *                         magic_type_names_print                            *
 *===========================================================================*/
PUBLIC void magic_type_names_print(const struct _magic_type* type)
{
    unsigned int i;

    for(i=0;i<type->num_names;i++) {
        _magic_printf("%s%s", (i==0 ? "" : "|"), type->names[i]);
    }
}

/*===========================================================================*
 *                       magic_type_comp_types_print                         *
 *===========================================================================*/
PUBLIC void magic_type_comp_types_print(const struct _magic_type* type,
    int flags)
{
    int num;
    int i=0;
    const struct _magic_type* comp_type;

    if(!MAGIC_TYPE_HAS_COMP_TYPES(type)) {
        return;
    }
    MAGIC_TYPE_NUM_COMP_TYPES(type, &num);
    _magic_printf("#%d", num);
    if(flags & MAGIC_SKIP_COMP_TYPES) {
        return;
    }
    flags &= ~MAGIC_EXPAND_TYPE_STR;

    MAGIC_TYPE_COMP_ITER(type, comp_type,
        _magic_printf("%s%d=", (i==0 ? ": " : ", "), i+1);
        MAGIC_TYPE_PRINT(comp_type, flags|MAGIC_SKIP_COMP_TYPES);
        i++;
    );
}

/*===========================================================================*
 *                     magic_type_str_print_from_target                      *
 *===========================================================================*/
PUBLIC int magic_type_str_print_from_target(void* target)
{
    int printed_types=0;
    int ret;
    ret = magic_type_target_walk(target, NULL, NULL,
        magic_type_str_print_cb, &printed_types);
    if(ret < 0) {
        return ret;
    }
    if(printed_types == 0) {
        _magic_printf("BAD OFFSET");
    }
    return printed_types;
}

/*===========================================================================*
 *                            magic_type_equals                              *
 *===========================================================================*/
PUBLIC int magic_type_equals(const struct _magic_type* type, const struct _magic_type* other_type)
{
    assert(magic_level < MAGIC_MAX_RECURSIVE_TYPES);

    if(type == other_type) {
        return TRUE;
    }
    if(type->type_id != other_type->type_id) {
        return FALSE;
    }
    if((type->flags & MAGIC_TYPE_EXTERNAL) || (other_type->flags & MAGIC_TYPE_EXTERNAL)) {
        int i, nesting_level;
        if(type->num_child_types == other_type->num_child_types) {
            int num_contained_types = MAGIC_TYPE_NUM_CONTAINED_TYPES(type);
            if(num_contained_types == 0) {
                return !strcmp(type->type_str, other_type->type_str);
            }
            nesting_level = magic_type_get_nesting_level(type, magic_level);
            if(nesting_level >= 0) {
                return (other_type == magic_nested_types2[nesting_level]);
            }
            magic_nested_types[magic_level] = type;
            magic_nested_types2[magic_level] = other_type;
            magic_level++;
            for(i=0;i<num_contained_types;i++) {
                if(magic_type_equals(type->contained_types[i], other_type->contained_types[i]) == FALSE) {
                    magic_level--;
                    return FALSE;
                }
            }
            magic_level--;
            return TRUE;
        }
    }
    return FALSE;
}

/*===========================================================================*
 *                           magic_type_compatible                           *
 *===========================================================================*/
PUBLIC int magic_type_compatible(const struct _magic_type* type, const struct _magic_type* other_type, int flags)
{
    int i, nesting_level, num_contained_types;
    assert(magic_level < MAGIC_MAX_RECURSIVE_TYPES);

    if(type == other_type) {
        return TRUE;
    }

    if(type->type_id != other_type->type_id) {
        return FALSE;
    }

    if(type->num_child_types != other_type->num_child_types) {
        return FALSE;
    }

    if(flags & MAGIC_TYPE_COMPARE_FLAGS) {
        if((type->flags & (~MAGIC_TYPE_IS_ROOT)) != (other_type->flags & (~MAGIC_TYPE_IS_ROOT))){
            return FALSE;
        }
    }

    if(flags & MAGIC_TYPE_COMPARE_VALUE_SET) {
        if(MAGIC_TYPE_HAS_VALUE_SET(type) != MAGIC_TYPE_HAS_VALUE_SET(other_type)){
            return FALSE;
        }
        if(MAGIC_TYPE_HAS_VALUE_SET(type)){
            i=0;
            while(MAGIC_TYPE_HAS_VALUE(type, i) && MAGIC_TYPE_HAS_VALUE(other_type, i)) {
                if(MAGIC_TYPE_VALUE(type, i) != MAGIC_TYPE_VALUE(other_type, i)){
                    /* a value is different */
                    return FALSE;
                }
                i++;
            }
            if(MAGIC_TYPE_HAS_VALUE(type, i) || MAGIC_TYPE_HAS_VALUE(other_type, i)) {
                return FALSE;
            }
        }
    }

    if(flags & MAGIC_TYPE_COMPARE_NAME) {
        if(strcmp(type->name, other_type->name)){
            return FALSE;
        }
    }

    if(flags & MAGIC_TYPE_COMPARE_NAMES) {
        if(type->num_names != other_type->num_names) {
            return FALSE;
        }
        if(type->num_names > 1) {
            for(i=0; (unsigned int)i<type->num_names; i++){
                if(strcmp(type->names[i], other_type->names[i])) {
                    return FALSE;
                }
            }
        }
    }

    num_contained_types = MAGIC_TYPE_NUM_CONTAINED_TYPES(type);
    if(num_contained_types == 0) {
        return type->size == other_type->size && !strcmp(type->type_str, other_type->type_str);
    }

    if(type->type_id == MAGIC_TYPE_STRUCT) {
        if(flags & MAGIC_TYPE_COMPARE_MEMBER_NAMES) {
            for(i=0; (unsigned int)i<type->num_child_types; i++){
                if(strcmp(type->member_names[i], other_type->member_names[i])) {
                    return FALSE;
                }
            }
        }
        if(flags & MAGIC_TYPE_COMPARE_MEMBER_OFFSETS) {
            for(i=0; (unsigned int)i<type->num_child_types; i++){
                if(type->member_offsets[i] != other_type->member_offsets[i]) {
                    return FALSE;
                }
            }
        }
    }

    nesting_level = magic_type_get_nesting_level(type, magic_level);
    if(nesting_level >= 0) {
        return (other_type == magic_nested_types2[nesting_level]);
    }
    magic_nested_types[magic_level] = type;
    magic_nested_types2[magic_level] = other_type;
    magic_level++;
    for(i=0;i<num_contained_types;i++) {
        if(!magic_type_compatible(type->contained_types[i], other_type->contained_types[i], flags)) {
            magic_level--;
            return FALSE;
        }
    }
    magic_level--;
    return TRUE;
}

/*===========================================================================*
 *                        magic_type_comp_compatible                         *
 *===========================================================================*/
PUBLIC int magic_type_comp_compatible(const struct _magic_type* type, const struct _magic_type* other_type)
{
    const struct _magic_type *comp_type;

    MAGIC_TYPE_COMP_ITER(type, comp_type,
        if(magic_type_compatible(comp_type, other_type, 0)) {
             return TRUE;
        }
    );

    return FALSE;
}

/*===========================================================================*
 *                          magic_type_ptr_is_text                           *
 *===========================================================================*/
PUBLIC int magic_type_ptr_is_text(const struct _magic_type* ptr_type)
{
    const struct _magic_type *comp_type;

    assert(ptr_type->type_id == MAGIC_TYPE_POINTER);
    if(ptr_type->contained_types[0]->type_id == MAGIC_TYPE_FUNCTION
        || MAGIC_TYPE_IS_VOID(ptr_type->contained_types[0])) {
        return TRUE;
    }

    MAGIC_TYPE_COMP_ITER(ptr_type, comp_type,
        if(comp_type->type_id == MAGIC_TYPE_FUNCTION
            || MAGIC_TYPE_IS_VOID(comp_type)) {
            return TRUE;
        }
    );

    return FALSE;
}

/*===========================================================================*
 *                          magic_type_ptr_is_data                           *
 *===========================================================================*/
PUBLIC int magic_type_ptr_is_data(const struct _magic_type* ptr_type)
{
    const struct _magic_type *comp_type;

    assert(ptr_type->type_id == MAGIC_TYPE_POINTER);
    if(ptr_type->contained_types[0]->type_id != MAGIC_TYPE_FUNCTION) {
        return TRUE;
    }

    MAGIC_TYPE_COMP_ITER(ptr_type, comp_type,
        if(comp_type->type_id != MAGIC_TYPE_FUNCTION) {
            return TRUE;
        }
    );

    return FALSE;
}

/*===========================================================================*
 *                    magic_type_alloc_needs_varsized_array                  *
 *===========================================================================*/
PUBLIC int magic_type_alloc_needs_varsized_array(const struct _magic_type* type,
    size_t alloc_size, int *num_elements)
{
    /* See if this type needs a var-sized array for the given allocation size */
    const struct _magic_type *array_type, *array_el_type;
    size_t array_offset, array_size;
    if(!MAGIC_TYPE_FLAG(type, MAGIC_TYPE_VARSIZE)) {
        return FALSE;
    }
    assert(type->type_id == MAGIC_TYPE_STRUCT);

    if(alloc_size <= type->size || type->num_child_types == 0) {
        return FALSE;
    }
    array_type = type->contained_types[type->num_child_types-1];
    if(array_type->type_id != MAGIC_TYPE_ARRAY) {
        return FALSE;
    }
    array_el_type = array_type->contained_types[0];
    array_offset = type->member_offsets[type->num_child_types-1]+array_type->num_child_types*array_el_type->size;
    array_size = alloc_size - array_offset;
    if(array_size == 0 || array_size % array_el_type->size != 0) {
        return FALSE;
    }
    if(num_elements) {
        *num_elements = 1+array_size/array_el_type->size;
    }

    return TRUE;
}

/*===========================================================================*
 *                     magic_type_alloc_get_varsized_array_size              *
 *===========================================================================*/
PUBLIC size_t magic_type_alloc_get_varsized_array_size(const struct _magic_type* type,
    int num_elements)
{
    /* Get the allocation size from the number of elements of the varsized array. */
    const struct _magic_type *array_type, *array_el_type;
    size_t array_offset;
    if(!MAGIC_TYPE_FLAG(type, MAGIC_TYPE_VARSIZE)) {
        return 0;
    }
    assert(type->type_id == MAGIC_TYPE_STRUCT);

    if(num_elements == 1) {
        return type->size;
    }
    array_type = type->contained_types[type->num_child_types-1];
    if(array_type->type_id != MAGIC_TYPE_ARRAY) {
        return 0;
    }
    array_el_type = array_type->contained_types[0];
    array_offset = type->member_offsets[type->num_child_types-1]+array_type->num_child_types*array_el_type->size;
    return array_offset + array_el_type->size*(num_elements-1);
}

/*===========================================================================*
 *                      magic_type_parse_varsized_array                      *
 *===========================================================================*/
PUBLIC void magic_type_parse_varsized_array(const struct _magic_type *type,
    const struct _magic_type **sub_struct_type, const struct _magic_type **sub_array_type,
    size_t *sub_array_offset, size_t *sub_array_size)
{
    /* Parse a var-sized array containing a variable-sized struct. */
    const struct _magic_type *_sub_struct_type, *_sub_array_type, *_sub_array_el_type;
    size_t _sub_array_offset, _sub_array_size;

    assert(type->type_id == MAGIC_TYPE_ARRAY && MAGIC_TYPE_FLAG(type, MAGIC_TYPE_DYNAMIC));
    _sub_struct_type = type->contained_types[0];
    assert(magic_type_alloc_needs_varsized_array(_sub_struct_type, type->size, NULL));

    _sub_array_type = _sub_struct_type->contained_types[_sub_struct_type->num_child_types-1];
    _sub_array_el_type = _sub_array_type->contained_types[0];
    _sub_array_offset = _sub_struct_type->member_offsets[_sub_struct_type->num_child_types-1]+_sub_array_type->num_child_types*_sub_array_el_type->size;
    _sub_array_size = type->size - _sub_array_offset;

    if(sub_struct_type) *sub_struct_type = _sub_struct_type;
    if(sub_array_type) *sub_array_type = _sub_array_type;
    if(sub_array_offset) *sub_array_offset = _sub_array_offset;
    if(sub_array_size) *sub_array_size = _sub_array_size;
}

/*===========================================================================*
 *                          magic_type_walk_flags                            *
 *===========================================================================*/
PUBLIC int magic_type_walk_flags(const struct _magic_type* parent_type,
    unsigned parent_offset, int child_num,
    const struct _magic_type* type, unsigned offset,
    const unsigned min_offset, const unsigned max_offset,
    const magic_type_walk_cb_t cb, void* cb_args, int flags) {
    static THREAD_LOCAL int magic_depth = 0;
    int ret, status, action;
    ret = MAGIC_TYPE_WALK_CONTINUE;

    if(offset >= min_offset && offset <= max_offset) {
         ret = cb(parent_type, parent_offset, child_num, type, offset, magic_depth, cb_args);
    }
    else if(offset > max_offset) {
        ret = MAGIC_TYPE_WALK_STOP;
    }
    else if(offset+type->size <= min_offset) {
        ret = MAGIC_TYPE_WALK_SKIP_PATH;
    }

    /* The status code returned to the caller is propagated directly from the
     * callback only in case of ret<0 and ret == MAGIC_TYPE_WALK_STOP.
     * In all the other cases, we return 0 the caller.
     */
    status = ret < 0 ? ret : 0;
    action = ret < 0 ? MAGIC_TYPE_WALK_STOP : ret;
    switch(action) {
        case MAGIC_TYPE_WALK_STOP:
            status = status ? status : MAGIC_TYPE_WALK_STOP;
        break;
        case MAGIC_TYPE_WALK_SKIP_PATH:
            status = 0;
        break;
        case MAGIC_TYPE_WALK_CONTINUE:
            if(!MAGIC_TYPE_IS_WALKABLE(type)) {
                status = 0;
            }
            else {
                int i, num_child_types, start;
                num_child_types = type->num_child_types;
                start = 0;
                if(type->type_id == MAGIC_TYPE_ARRAY || type->type_id == MAGIC_TYPE_VECTOR) {
                    if(!MAGIC_TYPE_FLAG(type, MAGIC_TYPE_VARSIZE) && offset < min_offset) {
                        /* Skip irrelevant array iterations. */
                        start = (min_offset-offset)/(type->contained_types[0]->size);
                    }
                }
                for(i=start;i<num_child_types;i++) {
                    const struct _magic_type *child_type;
                    unsigned child_offset;
                    magic_type_walk_step(type, i, &child_type, &child_offset, flags);
                    magic_depth++;
                    status = magic_type_walk_flags(type, offset, i, child_type, offset+child_offset, min_offset, max_offset, cb, cb_args, flags);
                    magic_depth--;
                    if(status < 0 || status == MAGIC_TYPE_WALK_STOP) {
                        break;
                    }
                }
            }
        break;
        default:
            _magic_printf("magic_type_walk: unrecognized callback return code: %d, stopping type walk...\n", action);
            status = MAGIC_TYPE_WALK_STOP;
        break;
    }
    return status;
}

/*===========================================================================*
 *                          magic_type_target_walk                           *
 *===========================================================================*/
PUBLIC int magic_type_target_walk(void *target,
    struct _magic_dsentry **trg_dsentry, struct _magic_dfunction **trg_dfunction,
    const magic_type_walk_cb_t cb, void *cb_args)
{
    int ret;
    struct _magic_sentry *sentry = NULL;
    struct _magic_function *function = NULL;
    sentry = magic_sentry_lookup_by_range(target, magic_reentrant ? &magic_dsentry_buff : NULL);
    if (sentry == NULL) {
        function = magic_function_lookup_by_addr(target, magic_reentrant ? &magic_dfunction_buff : NULL);
        if (function == NULL) {
            /* No known entry found. */
            return MAGIC_ENOENT;
        }
        if (MAGIC_STATE_FLAG(function, MAGIC_STATE_ADDR_NOT_TAKEN)) {
            /* A function has been found, but it was not supposed to be a target. */
            return MAGIC_EBADENT;
        }
    }
    else if (MAGIC_STATE_FLAG(sentry, MAGIC_STATE_ADDR_NOT_TAKEN)) {
        /* An entry has been found, but it was not supposed to be a target. */
        return MAGIC_EBADENT;
    }
    assert(sentry || function);
    if (magic_reentrant) {
        if (sentry) {
            if (trg_dsentry) {
                if (MAGIC_STATE_FLAG(sentry, MAGIC_STATE_DYNAMIC)) {
                    magic_copy_dsentry(MAGIC_DSENTRY_FROM_SENTRY(sentry), *trg_dsentry);
                }
                else {
                    memcpy(MAGIC_DSENTRY_TO_SENTRY(*trg_dsentry), sentry, sizeof(struct _magic_sentry));
                }
            }
            if (trg_dfunction) {
                *trg_dfunction = NULL;
            }
        }
        else {
            if (trg_dfunction) {
                if (MAGIC_STATE_FLAG(function, MAGIC_STATE_DYNAMIC)) {
                    magic_copy_dfunction(MAGIC_DFUNCTION_FROM_FUNCTION(function), *trg_dfunction);
                }
                else {
                    memcpy(MAGIC_DFUNCTION_TO_FUNCTION(*trg_dfunction), function, sizeof(struct _magic_function));
                }
            }
            if (trg_dsentry) {
                *trg_dsentry = NULL;
            }
        }
    } else {
        /*
         * Just return the pointer to the target object.
         * NB!: Because the target objects can be static (i.e. sentries,
         * functions), the user MUST first check the flag
         * of the returned target element to see if it is a sentry
         * or function. Otherwise, he might end up accessing invalid
         * memory.
         */
        if (sentry) {
            if (trg_dsentry) {
                *trg_dsentry = MAGIC_DSENTRY_FROM_SENTRY(sentry);
            }
            if (trg_dfunction) {
                *trg_dfunction = NULL;
            }
        }
        else {
            if (trg_dfunction) {
                *trg_dfunction = MAGIC_DFUNCTION_FROM_FUNCTION(function);
            }
            if (trg_dsentry) {
                *trg_dsentry = NULL;
            }
        }
    }

    if (sentry) {
        ret = magic_type_walk_root_at_offset(sentry->type, (char *) target - (char *) sentry->address, cb, cb_args);
    } else {
        ret = magic_type_walk_root_at_offset(function->type, (char*) target - (char*) function->address, cb, cb_args);
    }

    return ret;
}

/*===========================================================================*
 *                      magic_type_walk_as_void_array                        *
 *===========================================================================*/
PUBLIC int magic_type_walk_as_void_array(const struct _magic_type* parent_type,
    unsigned parent_offset, int child_num, const struct _magic_type* type,
    unsigned offset, const unsigned min_offset, const unsigned max_offset,
    const magic_type_walk_cb_t cb, void* cb_args)
{
    struct _magic_type void_array_type;
    MAGIC_TYPE_VOID_ARRAY_GET_FROM_SIZE(&void_array_type, type->size);
    return magic_type_walk(parent_type, parent_offset, child_num, &void_array_type,
        offset, min_offset, max_offset, cb, cb_args);
}

/*===========================================================================*
 *                     magic_type_walk_as_ptrint_array                       *
 *===========================================================================*/
PUBLIC int magic_type_walk_as_ptrint_array(const struct _magic_type* parent_type,
    unsigned parent_offset, int child_num, const struct _magic_type* type, void* offset_addr,
    unsigned offset, const unsigned min_offset, const unsigned max_offset,
    const magic_type_walk_cb_t cb, void* cb_args)
{
    struct _magic_type ptrint_array_type;
    unsigned type_size = type->size;
    unsigned addr_diff = ((unsigned)offset_addr) % sizeof(void*);
    if(addr_diff > 0) {
        unsigned addr_off_by = sizeof(void*) - addr_diff;
        if(type_size <= addr_off_by) {
            return MAGIC_EBADWALK;
        }
        type_size -= addr_off_by;
        offset_addr = (void*)((unsigned)offset_addr + addr_off_by);
        offset += addr_off_by;
    }
    addr_diff = (((unsigned)offset_addr)+type_size) % sizeof(void*);
    if(addr_diff > 0) {
        unsigned addr_off_by = addr_diff;
        if(type_size <= addr_off_by) {
            return MAGIC_EBADWALK;
        }
        type_size -= addr_off_by;
    }
    MAGIC_TYPE_PTRINT_ARRAY_GET_FROM_SIZE(&ptrint_array_type, type_size);
    return magic_type_walk(parent_type, parent_offset, child_num, &ptrint_array_type,
        offset, min_offset, max_offset, cb, cb_args);
}

/*===========================================================================*
 *                         magic_type_str_print_cb                           *
 *===========================================================================*/
PUBLIC int magic_type_str_print_cb(const struct _magic_type* parent_type,
    const unsigned parent_offset, int child_num,
    const struct _magic_type* type, const unsigned offset, int depth, void* cb_args)
{
    int *printed_types = (int*) cb_args;
    if(printed_types) (*printed_types)++;
    magic_type_str_print(type);
    _magic_printf("; ");
    return MAGIC_TYPE_WALK_CONTINUE;
}

/*===========================================================================*
 *                            magic_type_count_cb                            *
 *===========================================================================*/
PUBLIC int magic_type_count_cb(const struct _magic_type* parent_type,
    const unsigned parent_offset, int child_num,
    const struct _magic_type* type, const unsigned offset, int depth, void* cb_args)
{
    int *type_counter = (int*) cb_args;
    if(type_counter) (*type_counter)++;
    return MAGIC_TYPE_WALK_CONTINUE;
}

/*===========================================================================*
 *                         magic_type_child_offset_cb                        *
 *===========================================================================*/
PUBLIC int magic_type_child_offset_cb(const struct _magic_type* parent_type,
    const unsigned parent_offset, int child_num,
    const struct _magic_type* type, const unsigned offset, int depth, void* cb_args)
{
    void **args_array = (void**) cb_args;
    int *my_child_num = (int*) args_array[0];
    unsigned *child_offset = (unsigned*) args_array[1];

    if(!parent_type) {
        return MAGIC_TYPE_WALK_CONTINUE;
    }
    if(child_num == *my_child_num) {
        *child_offset = offset;
        return MAGIC_TYPE_WALK_STOP;
    }
    return MAGIC_TYPE_WALK_SKIP_PATH;
}

/*===========================================================================*
 *                            magic_type_walk_step                           *
 *===========================================================================*/
PUBLIC void magic_type_walk_step(const struct _magic_type *type,
    int child_num, const struct _magic_type **child_type, unsigned *child_offset,
    int walk_flags)
{
    int type_id;
    struct _magic_type type_buff;
    if(type->type_id == MAGIC_TYPE_UNION && (walk_flags & MAGIC_TYPE_WALK_UNIONS_AS_VOID)) {
        MAGIC_TYPE_VOID_ARRAY_GET_FROM_SIZE(&type_buff, type->size);
        type = &type_buff;
    }
    type_id = type->type_id;
    if(type_id == MAGIC_TYPE_STRUCT || type_id == MAGIC_TYPE_UNION) {
        *child_type = type->contained_types[child_num];
        *child_offset = type->member_offsets[child_num];
    }
    else {
        assert(type_id == MAGIC_TYPE_ARRAY || type_id == MAGIC_TYPE_VECTOR);
        if(MAGIC_TYPE_FLAG(type, MAGIC_TYPE_VARSIZE) && child_num > 0) {
            const struct _magic_type *sub_array_type, *sub_array_el_type;
            size_t sub_array_offset;
            magic_type_parse_varsized_array(type, NULL, &sub_array_type, &sub_array_offset, NULL);
            sub_array_el_type = sub_array_type->contained_types[0];
            *child_type = sub_array_el_type;
            *child_offset = sub_array_offset + (child_num-1)*sub_array_el_type->size;
        }
        else {
            *child_type = type->contained_types[0];
            *child_offset = child_num*((*child_type)->size);
        }
    }
}

/*===========================================================================*
 *                            magic_type_get_size                            *
 *===========================================================================*/
PUBLIC size_t magic_type_get_size(struct _magic_type *type, int flags)
{
    size_t size;
    int i, num_contained_types;

    size = sizeof(type->size) +
           sizeof(type->num_child_types) + sizeof(type->contained_types) +
           sizeof(type->member_offsets) + sizeof(type->type_id) + sizeof(type->flags);
    num_contained_types = MAGIC_TYPE_NUM_CONTAINED_TYPES(type);

    if(num_contained_types > 0) {
        size += sizeof(*(type->contained_types))*num_contained_types;
    }
    if(type->type_id == MAGIC_TYPE_STRUCT) {
        size += sizeof(*(type->member_offsets))*num_contained_types;
        if(flags & MAGIC_SIZE_MEMBER_NAMES) {
            size += sizeof(*(type->member_names))*num_contained_types;
            for(i=0;i<num_contained_types;i++) {
                size += strlen(type->member_names[i])+1;
            }
        }
    }

    if(flags & MAGIC_SIZE_VALUE_SET) {
        if(MAGIC_TYPE_HAS_VALUE_SET(type)) {
            int num;
            MAGIC_TYPE_NUM_VALUES(type, &num);
            size += sizeof(int)+(num+1);
        }
    }
    if(flags & MAGIC_SIZE_TYPE_NAMES) {
        size += sizeof(type->num_names) + sizeof(type->names) + sizeof(*(type->names))*(type->num_names);
        for(i=0;(unsigned int)i<type->num_names;i++) {
            size += strlen(type->names[i])+1;
        }
    }
    if(flags & MAGIC_SIZE_COMP_TYPES) {
        if(MAGIC_TYPE_HAS_COMP_TYPES(type)) {
            int num;
            MAGIC_TYPE_NUM_COMP_TYPES(type, &num);
            size += sizeof(*(type->compatible_types))*num;
        }
    }

    return size;
}

/*===========================================================================*
 *                            magic_types_get_size                           *
 *===========================================================================*/
PUBLIC size_t magic_types_get_size(int flags)
{
    size_t size;
    int i;

    size = 0;
    for(i=0;i<_magic_types_num;i++) {
        size += magic_type_get_size(&_magic_types[i], flags);
    }

    return size;
}

/*===========================================================================*
 *                          magic_function_get_size                          *
 *===========================================================================*/
PUBLIC size_t magic_function_get_size(struct _magic_function *function, int flags)
{
    size_t size;

    size = sizeof(function->type) + sizeof(function->flags) + sizeof(function->address);

    if(flags & MAGIC_SIZE_NAMES) {
        size += sizeof(function->name) + strlen(function->name)+1;
    }

    return size;
}

/*===========================================================================*
 *                          magic_functions_get_size                         *
 *===========================================================================*/
PUBLIC size_t magic_functions_get_size(int flags)
{
    size_t size;
    int i;

    size = 0;
    for(i=0;i<_magic_functions_num;i++) {
        size += magic_function_get_size(&_magic_functions[i], flags);
    }

    return size;
}

/*===========================================================================*
 *                         magic_dfunctions_get_size                         *
 *===========================================================================*/
PUBLIC size_t magic_dfunctions_get_size(int flags)
{
    size_t size;
    struct _magic_dfunction* dfunction;
    struct _magic_function* function;

    size = 0;
    MAGIC_DFUNCTION_FUNC_ITER(_magic_first_dfunction, dfunction, function,
        size += magic_function_get_size(function, flags);
    );

    return size;
}

/*===========================================================================*
 *                           magic_sentry_get_size                           *
 *===========================================================================*/
PUBLIC size_t magic_sentry_get_size(struct _magic_sentry *sentry, int flags)
{
    size_t size;

    size = sizeof(sentry->type) + sizeof(sentry->flags);

    if(MAGIC_SENTRY_IS_DSENTRY(sentry)) {
        struct _magic_dsentry *dsentry = MAGIC_DSENTRY_FROM_SENTRY(sentry);
        if(flags & MAGIC_SIZE_DSENTRY_NAMES) {
            size += sizeof(sentry->name) + strlen(sentry->name)+1;
            if(dsentry->parent_name) {
                size += sizeof(dsentry->parent_name) + strlen(dsentry->parent_name)+1;
            }
        }
        if(sentry->type == &dsentry->type) {
            size += sizeof(dsentry->type.num_child_types);
        }
        size += sizeof(dsentry->next);
    }
    else {
        size += sizeof(sentry->address);
        if(flags & MAGIC_SIZE_NAMES) {
            size += sizeof(sentry->name) + strlen(sentry->name)+1;
        }
    }

    return size;
}

/*===========================================================================*
 *                           magic_sentries_get_size                         *
 *===========================================================================*/
PUBLIC size_t magic_sentries_get_size(int flags)
{
    size_t size;
    int i;

    size = 0;
    for(i=0;i<_magic_sentries_num;i++) {
        size += magic_sentry_get_size(&_magic_sentries[i], flags);
    }

    return size;
}

/*===========================================================================*
 *                          magic_dsentries_get_size                         *
 *===========================================================================*/
PUBLIC size_t magic_dsentries_get_size(int flags)
{
    size_t size;
    struct _magic_dsentry *prev_dsentry, *dsentry;
    struct _magic_sentry* sentry;

    size = 0;
    MAGIC_DSENTRY_ALIVE_ITER(_magic_first_dsentry, prev_dsentry, dsentry, sentry,
        if(!MAGIC_STATE_FLAG(sentry, MAGIC_STATE_OUT_OF_BAND)) {
            size += magic_sentry_get_size(sentry, flags);
        }
    );

    return size;
}

/*===========================================================================*
 *                           magic_dsindex_get_size                          *
 *===========================================================================*/
PUBLIC size_t magic_dsindex_get_size(struct _magic_dsindex *dsindex, int flags)
{
    size_t size;

    size = sizeof(dsindex->type) + sizeof(dsindex->flags);

    if(flags & MAGIC_SIZE_DSINDEX_NAMES) {
        size += sizeof(dsindex->parent_name) + strlen(dsindex->parent_name)+1;
        size += sizeof(dsindex->name) + strlen(dsindex->name)+1;
    }

    return size;
}

/*===========================================================================*
 *                          magic_dsindexes_get_size                         *
 *===========================================================================*/
PUBLIC size_t magic_dsindexes_get_size(int flags)
{
    size_t size;
    int i;

    size = 0;
    for(i=0;i<_magic_dsindexes_num;i++) {
        size += magic_dsindex_get_size(&_magic_dsindexes[i], flags);
    }

    return size;
}

/*===========================================================================*
 *                           magic_sodesc_get_size                           *
 *===========================================================================*/
PUBLIC size_t magic_sodesc_get_size(struct _magic_sodesc *sodesc, int flags)
{
    return sizeof(struct _magic_sodesc);
}

/*===========================================================================*
 *                           magic_sodescs_get_size                          *
 *===========================================================================*/
PUBLIC size_t magic_sodescs_get_size(int flags)
{
    size_t size;
    struct _magic_sodesc* sodesc;

    size = 0;
    MAGIC_SODESC_ITER(_magic_first_sodesc, sodesc,
        size += magic_sodesc_get_size(sodesc, flags);
    );

    return size;
}

/*===========================================================================*
 *                           magic_dsodesc_get_size                          *
 *===========================================================================*/
PUBLIC size_t magic_dsodesc_get_size(struct _magic_dsodesc *dsodesc, int flags)
{
    return sizeof(struct _magic_dsodesc);
}

/*===========================================================================*
 *                          magic_dsodescs_get_size                          *
 *===========================================================================*/
PUBLIC size_t magic_dsodescs_get_size(int flags)
{
    size_t size;
    struct _magic_dsodesc* dsodesc;

    size = 0;
    MAGIC_DSODESC_ITER(_magic_first_dsodesc, dsodesc,
        size += magic_dsodesc_get_size(dsodesc, flags);
    );

    return size;
}

/*===========================================================================*
 *                          magic_metadata_get_size                          *
 *===========================================================================*/
PUBLIC size_t magic_metadata_get_size(int flags)
{
    size_t size = 0;

    size += magic_types_get_size(flags);
    size += magic_functions_get_size(flags);
    size += magic_dfunctions_get_size(flags);
    size += magic_sentries_get_size(flags);
    size += magic_dsentries_get_size(flags);
    size += magic_dsindexes_get_size(flags);
    size += magic_dsodescs_get_size(flags);

    return size;
}

/*===========================================================================*
 *                       magic_sentries_data_get_size                        *
 *===========================================================================*/
PUBLIC size_t magic_sentries_data_get_size(int flags)
{
    size_t size;
    int i;

    size = 0;
    for(i=0;i<_magic_sentries_num;i++) {
        size += _magic_sentries[i].type->size;
    }

    return size;
}

/*===========================================================================*
 *                       magic_dsentries_data_get_size                       *
 *===========================================================================*/
PUBLIC size_t magic_dsentries_data_get_size(int flags)
{
    size_t size;
    struct _magic_dsentry *prev_dsentry, *dsentry;
    struct _magic_sentry* sentry;

    size = 0;
    MAGIC_DSENTRY_ALIVE_ITER(_magic_first_dsentry, prev_dsentry, dsentry, sentry,
        if(!MAGIC_STATE_FLAG(sentry, MAGIC_STATE_OUT_OF_BAND)) {
            size += sentry->type->size;
            if(MAGIC_STATE_FLAG(sentry, MAGIC_STATE_HEAP)) {
                /* Assume a couple of words for malloc header. */
                size += 2*sizeof(void*);
            }
        }
    );

    return size;
}

/*===========================================================================*
 *                         magic_other_data_get_size                         *
 *===========================================================================*/
PUBLIC size_t magic_other_data_get_size(int flags)
{
    size_t size = 0;

    MAGIC_DSENTRY_LOCK();
    magic_range_is_stack(NULL);
    MAGIC_DSENTRY_UNLOCK();
    size += MAGIC_RANGE_SIZE(magic_stack_range);
    size += MAGIC_RANGE_SIZE(magic_text_range);

    return size;
}

/*===========================================================================*
 *                            magic_data_get_size                            *
 *===========================================================================*/
PUBLIC size_t magic_data_get_size(int flags)
{
    size_t size = 0;

    size += magic_sentries_data_get_size(flags);
    size += magic_dsentries_data_get_size(flags);
    size += magic_other_data_get_size(flags);

    return size;
}

/*===========================================================================*
 *                           magic_print_size_stats                          *
 *===========================================================================*/
PUBLIC void magic_print_size_stats(int flags)
{
    size_t sentries_data_size, sentries_metadata_size;
    size_t dsentries_data_size, dsentries_metadata_size;
    size_t data_size, metadata_size;
    int dsentries_num;
    sentries_data_size = magic_sentries_data_get_size(flags);
    sentries_metadata_size = magic_sentries_get_size(flags);
    dsentries_data_size = magic_dsentries_data_get_size(flags);
    dsentries_metadata_size = magic_dsentries_get_size(flags);
    data_size = magic_data_get_size(flags);
    metadata_size = magic_metadata_get_size(flags);
    MAGIC_DSENTRY_NUM(_magic_first_dsentry, &dsentries_num);
    _magic_printf("--------------------------------------------------------\n");
    _magic_printf("magic_print_size_stats: Printing size stats:\n");
    _magic_printf("    - sentries:  # %6d, data %8d, metadata %8d, total %8d, ratio %.3f\n", _magic_sentries_num, sentries_data_size, sentries_metadata_size, sentries_data_size+sentries_metadata_size, ((double)sentries_metadata_size)/sentries_data_size);
    _magic_printf("    - dsentries: # %6d, data %8d, metadata %8d, total %8d, ratio %.3f\n", dsentries_num, dsentries_data_size, dsentries_metadata_size, dsentries_data_size+dsentries_metadata_size, ((double)dsentries_metadata_size)/dsentries_data_size);
    _magic_printf("    - other:     # %6d, data %8d\n", 2, magic_other_data_get_size(flags));
    _magic_printf("    - state all: # %6d, data %8d, metadata %8d, total %8d, ratio %.3f\n", _magic_sentries_num+dsentries_num, sentries_data_size+dsentries_data_size, metadata_size, sentries_data_size+dsentries_data_size+metadata_size, ((double)metadata_size)/(sentries_data_size+dsentries_data_size));
    _magic_printf("    - all:       # %6d, data %8d, metadata %8d, total %8d, ratio %.3f\n", _magic_sentries_num+dsentries_num+2, data_size, metadata_size, data_size+metadata_size, ((double)metadata_size)/data_size);
    _magic_printf("--------------------------------------------------------\n");
    _magic_printf("magic_print_size_stats: Printing metadata size breakdown:\n");
    _magic_printf("    - types:     # %6d, metadata %8d\n", _magic_types_num, magic_types_get_size(flags));
    _magic_printf("    - functions: # %6d, metadata %8d\n", _magic_functions_num, magic_functions_get_size(flags));
    _magic_printf("    - dfunctions # %6d, metadata %8d\n", 0, magic_dfunctions_get_size(flags));
    _magic_printf("    - sentries:  # %6d, metadata %8d\n", _magic_sentries_num, sentries_metadata_size);
    _magic_printf("    - dsentries: # %6d, metadata %8d\n", dsentries_num, dsentries_metadata_size);
    _magic_printf("    - dsindexes: # %6d, metadata %8d\n", _magic_dsindexes_num, magic_dsindexes_get_size(flags));
    _magic_printf("    - dsodescs:  # %6d, metadata %8d\n", 0, magic_dsodescs_get_size(flags));
    _magic_printf("--------------------------------------------------------\n");
}