summaryrefslogtreecommitdiff
path: root/plugins/pdb-ng/src/symbol_parser.rs
blob: 125d7c88f7077d3f461c103f172889ae8b5c1c6f (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
// Copyright 2022-2026 Vector 35 Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use binaryninja::confidence::ConfMergeable;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::mem;
use std::sync::OnceLock;

use anyhow::{anyhow, Result};
use itertools::Itertools;
use pdb::register::Register::{AMD64, X86};
use pdb::register::{AMD64Register, X86Register};
use pdb::Error::UnimplementedSymbolKind;
use pdb::{
    AnnotationReferenceSymbol, BasePointerRelativeSymbol, BlockSymbol, BuildInfoSymbol,
    CallSiteInfoSymbol, CompileFlagsSymbol, ConstantSymbol, DataReferenceSymbol, DataSymbol,
    DefRangeFramePointerRelativeFullScopeSymbol, DefRangeFramePointerRelativeSymbol,
    DefRangeRegisterRelativeSymbol, DefRangeRegisterSymbol, DefRangeSubFieldRegisterSymbol,
    DefRangeSubFieldSymbol, DefRangeSymbol, ExportSymbol, FallibleIterator, FrameProcedureSymbol,
    InlineSiteSymbol, LabelSymbol, LocalSymbol, MultiRegisterVariableSymbol, ObjNameSymbol,
    ProcedureReferenceSymbol, ProcedureSymbol, PublicSymbol, RegisterRelativeSymbol,
    RegisterVariableSymbol, Rva, SeparatedCodeSymbol, Source, Symbol, SymbolData, SymbolIndex,
    SymbolIter, ThreadStorageSymbol, ThunkSymbol, TrampolineSymbol, TypeIndex,
    UserDefinedTypeSymbol, UsingNamespaceSymbol,
};

use crate::PDBParserInstance;
use binaryninja::architecture::{Architecture, ArchitectureExt, Register, RegisterId};
use binaryninja::binary_view::BinaryViewBase;
use binaryninja::confidence::{Conf, MAX_CONFIDENCE, MIN_CONFIDENCE};
use binaryninja::demangle::demangle_ms;
use binaryninja::rc::Ref;
use binaryninja::types::{FunctionParameter, QualifiedName, StructureBuilder, Type, TypeClass};
use binaryninja::variable::{Variable, VariableSourceType};

const DEMANGLE_CONFIDENCE: u8 = 32;

/// Parsed Data Symbol like globals, etc
#[derive(Debug, Clone)]
pub struct SymbolNames {
    pub raw_name: String,
    pub short_name: Option<String>,
    pub full_name: Option<String>,
}

/// Parsed Data Symbol like globals, etc
#[derive(Debug, Clone)]
pub struct ParsedDataSymbol {
    /// If the symbol comes from the public symbol list (lower quality)
    pub is_public: bool,
    /// Absolute address in bv
    pub address: u64,
    /// Symbol name
    pub name: SymbolNames,
    /// Type if known
    pub type_: Option<Conf<Ref<Type>>>,
}

/// Parsed functions and function-y symbols
#[derive(Debug, Clone)]
pub struct ParsedProcedure {
    /// If the symbol comes from the public symbol list (lower quality)
    pub is_public: bool,
    /// Absolute address in bv
    pub address: u64,
    /// Symbol name
    pub name: SymbolNames,
    /// Function type if known
    pub type_: Option<Conf<Ref<Type>>>,
    /// List of local variables (TODO: use these)
    pub locals: Vec<ParsedVariable>,
}

/// Structure with some information about a procedure
#[derive(Debug, Clone)]
pub struct ParsedProcedureInfo {
    /// Known parameters for the procedure
    pub params: Vec<ParsedVariable>,
    /// Known local variables for the procedure
    pub locals: Vec<ParsedVariable>,
}

/// One parsed variable / parameter
#[derive(Debug, Clone)]
pub struct ParsedVariable {
    /// Variable name
    pub name: String,
    /// Variable type if known
    pub type_: Option<Conf<Ref<Type>>>,
    /// Location(s) where the variable is stored. PDB lets you store a variable in multiple locations
    /// despite binja not really understanding that. Length is probably never zero
    pub storage: Vec<ParsedLocation>,
    /// Do we think this is a parameter
    pub is_param: bool,
}

#[derive(Debug, Copy, Clone)]
pub struct ParsedLocation {
    /// Location information
    pub location: Variable,
    /// Is the storage location relative to the base pointer?
    pub base_relative: bool,
    /// Is the storage location relative to the stack pointer?
    pub stack_relative: bool,
}

/// Big enum of all the types of symbols we know how to parse
#[derive(Debug, Clone)]
pub enum ParsedSymbol {
    /// Parsed Data Symbol like globals, etc
    Data(ParsedDataSymbol),
    /// Parsed functions and function-y symbols
    Procedure(ParsedProcedure),
    /// Structure with some information about a procedure
    ProcedureInfo(ParsedProcedureInfo),
    /// One parsed variable / parameter
    LocalVariable(ParsedVariable),
    /// Location of a local variable
    Location(ParsedLocation),
}

/// This is all done in the parser instance namespace because the lifetimes are impossible to
/// wrangle otherwise.
impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
    pub fn parse_symbols(
        &mut self,
        progress: Box<dyn Fn(usize, usize) -> Result<()> + '_>,
    ) -> Result<(Vec<ParsedSymbol>, Vec<ParsedSymbol>)> {
        let mut module_count = 0usize;
        let dbg = self.pdb.debug_information()?;
        let mut modules = dbg.modules()?;
        while let Some(_module) = modules.next()? {
            module_count += 1;
        }

        let global_symbols = self.pdb.global_symbols()?;
        let symbols = global_symbols.iter();
        let parsed = self.parse_mod_symbols(symbols)?;
        for sym in parsed {
            match &sym {
                ParsedSymbol::Data(ParsedDataSymbol {
                    name: SymbolNames { raw_name, .. },
                    ..
                })
                | ParsedSymbol::Procedure(ParsedProcedure {
                    name: SymbolNames { raw_name, .. },
                    ..
                }) => {
                    self.parsed_symbols_by_name
                        .insert(raw_name.clone(), self.parsed_symbols.len());
                }
                _ => {}
            }
            self.parsed_symbols.push(sym);
        }

        (progress)(1, module_count + 1)?;

        let dbg = self.pdb.debug_information()?;
        let mut modules = dbg.modules()?;
        let mut i = 0;
        while let Some(module) = modules.next()? {
            i += 1;
            (progress)(i + 1, module_count + 1)?;

            self.log(|| {
                format!(
                    "Module {} {}",
                    module.module_name(),
                    module.object_file_name()
                )
            });
            if let Some(module_info) = self.pdb.module_info(&module)? {
                let symbols = module_info.symbols()?;
                let parsed = self.parse_mod_symbols(symbols)?;
                for sym in parsed {
                    match &sym {
                        ParsedSymbol::Data(ParsedDataSymbol {
                            name: SymbolNames { raw_name, .. },
                            ..
                        })
                        | ParsedSymbol::Procedure(ParsedProcedure {
                            name: SymbolNames { raw_name, .. },
                            ..
                        }) => {
                            self.parsed_symbols_by_name
                                .insert(raw_name.clone(), self.parsed_symbols.len());
                        }
                        _ => {}
                    }
                    self.parsed_symbols.push(sym);
                }
            }
        }

        let use_public = self.settings.get_bool_with_opts(
            "pdb.features.loadGlobalSymbols",
            &mut self.settings_query_opts,
        );

        let mut best_symbols = HashMap::<String, &ParsedSymbol>::new();
        for sym in &self.parsed_symbols {
            match sym {
                ParsedSymbol::Data(ParsedDataSymbol {
                    is_public,
                    address,
                    name:
                        SymbolNames {
                            raw_name,
                            full_name,
                            ..
                        },
                    type_,
                    ..
                }) => {
                    if *is_public && !use_public {
                        continue;
                    }

                    let this_confidence = match type_ {
                        Some(Conf { confidence, .. }) => *confidence,
                        _ => MIN_CONFIDENCE,
                    };
                    let (new_better, old_exists) = match best_symbols.get(raw_name) {
                        Some(ParsedSymbol::Data(ParsedDataSymbol {
                            type_:
                                Some(Conf {
                                    confidence: old_conf,
                                    ..
                                }),
                            ..
                        })) => (this_confidence > *old_conf, true),
                        Some(ParsedSymbol::Data(ParsedDataSymbol { type_: None, .. })) => {
                            (true, true)
                        }
                        Some(..) => (false, true),
                        _ => (true, false),
                    };
                    if new_better {
                        self.log(|| {
                            format!(
                                "New best symbol (at 0x{:x}) for `{}` / `{}`: {:?}",
                                *address,
                                raw_name,
                                full_name.as_ref().unwrap_or(raw_name),
                                sym
                            )
                        });
                        if old_exists {
                            self.log(|| "Clobbering old definition");
                        }
                        best_symbols.insert(raw_name.clone(), sym);
                    }
                }
                _ => {}
            }
        }

        let mut best_functions = HashMap::<String, &ParsedSymbol>::new();
        for sym in &self.parsed_symbols {
            match sym {
                ParsedSymbol::Procedure(ParsedProcedure {
                    is_public,
                    address,
                    name:
                        SymbolNames {
                            raw_name,
                            full_name,
                            ..
                        },
                    type_,
                    ..
                }) => {
                    if *is_public && !use_public {
                        continue;
                    }

                    let this_confidence = match type_ {
                        Some(Conf { confidence, .. }) => *confidence,
                        _ => MIN_CONFIDENCE,
                    };
                    let (new_better, old_exists) = match best_functions.get(raw_name) {
                        Some(ParsedSymbol::Procedure(ParsedProcedure {
                            type_:
                                Some(Conf {
                                    confidence: old_conf,
                                    ..
                                }),
                            ..
                        })) => (this_confidence > *old_conf, true),
                        Some(ParsedSymbol::Procedure(ParsedProcedure { type_: None, .. })) => {
                            (true, true)
                        }
                        Some(..) => (false, true),
                        _ => (true, false),
                    };
                    if new_better {
                        self.log(|| {
                            format!(
                                "New best function (at 0x{:x}) for `{}` / `{}`: {:?}",
                                *address,
                                raw_name,
                                full_name.as_ref().unwrap_or(raw_name),
                                sym
                            )
                        });
                        if old_exists {
                            self.log(|| "Clobbering old definition");
                        }
                        best_functions.insert(raw_name.clone(), sym);
                    }
                }
                _ => {}
            }
        }

        Ok((
            best_symbols
                .into_iter()
                .map(|(_, sym)| sym.clone())
                .sorted_by_key(|sym| match sym {
                    ParsedSymbol::Data(ParsedDataSymbol {
                        type_, is_public, ..
                    }) => type_
                        .as_ref()
                        .map(|ty| {
                            if *is_public {
                                ty.confidence / 2
                            } else {
                                ty.confidence
                            }
                        })
                        .unwrap_or(0),
                    ParsedSymbol::Procedure(ParsedProcedure {
                        type_, is_public, ..
                    }) => type_
                        .as_ref()
                        .map(|ty| {
                            if *is_public {
                                ty.confidence / 2
                            } else {
                                ty.confidence
                            }
                        })
                        .unwrap_or(0),
                    _ => 0,
                })
                .collect::<Vec<_>>(),
            best_functions
                .into_iter()
                .map(|(_, func)| func.clone())
                .sorted_by_key(|sym| match sym {
                    ParsedSymbol::Data(ParsedDataSymbol {
                        type_, is_public, ..
                    }) => type_
                        .as_ref()
                        .map(|ty| {
                            if *is_public {
                                ty.confidence / 2
                            } else {
                                ty.confidence
                            }
                        })
                        .unwrap_or(0),
                    ParsedSymbol::Procedure(ParsedProcedure {
                        type_, is_public, ..
                    }) => type_
                        .as_ref()
                        .map(|ty| {
                            if *is_public {
                                ty.confidence / 2
                            } else {
                                ty.confidence
                            }
                        })
                        .unwrap_or(0),
                    _ => 0,
                })
                .collect::<Vec<_>>(),
        ))
    }

    /// Parse all the symbols in a module, via the given SymbolIter
    pub fn parse_mod_symbols(&mut self, mut symbols: SymbolIter) -> Result<Vec<ParsedSymbol>> {
        // Collect tree structure first
        let mut first = None;
        let mut last_local = None;
        let mut top_level_syms = vec![];
        let mut thunk_syms = vec![];
        let mut unparsed_syms = BTreeMap::new();
        while let Some(sym) = symbols.next()? {
            if first.is_none() {
                first = Some(sym.index());
            }
            unparsed_syms.insert(sym.index(), sym);

            let p = sym.parse();
            self.log(|| format!("Parsed: {:x?}", p));

            // It's some sort of weird tree structure where SOME symbols have "end" indices
            // and anything between them and that index is a child symbol
            // Sometimes there are "end scope" symbols at those end indices but like, sometimes
            // there aren't? Which makes that entire system seem pointless (or I'm just missing
            // something and it makes sense to _someone_)
            if let Some(&(start, _end)) = self.symbol_stack.last() {
                self.add_symbol_child(start, sym.index());
            } else {
                // Place thunk symbols in their own list at the end, so they can reference
                // other symbols parsed in the module
                match &p {
                    Ok(SymbolData::Thunk(_)) => {
                        thunk_syms.push(sym.index());
                    }
                    _ => {
                        top_level_syms.push(sym.index());
                    }
                }
            }
            let mut popped = false;
            while let Some(&(_start, end)) = self.symbol_stack.last() {
                if sym.index().0 >= end.0 {
                    let _ = self.symbol_stack.pop();
                    popped = true;
                } else {
                    break;
                }
            }

            // These aren't actually used for parsing (I don't trust them) but we can include a little
            // debug error check here and see if it's ever actually wrong
            match p {
                Ok(SymbolData::ScopeEnd) | Ok(SymbolData::InlineSiteEnd) if popped => {}
                Ok(SymbolData::ScopeEnd) | Ok(SymbolData::InlineSiteEnd) if !popped => {
                    self.log(|| "Did not pop at a scope end??? WTF??");
                }
                _ if popped => {
                    self.log(|| "Popped but not at a scope end??? WTF??");
                }
                _ => {}
            }

            // Push new scopes on the stack to build the tree
            match p {
                Ok(SymbolData::Procedure(data)) => {
                    self.symbol_stack.push((sym.index(), data.end));
                }
                Ok(SymbolData::InlineSite(data)) => {
                    self.symbol_stack.push((sym.index(), data.end));
                }
                Ok(SymbolData::Block(data)) => {
                    self.symbol_stack.push((sym.index(), data.end));
                }
                Ok(SymbolData::Thunk(data)) => {
                    self.symbol_stack.push((sym.index(), data.end));
                }
                Ok(SymbolData::SeparatedCode(data)) => {
                    self.symbol_stack.push((sym.index(), data.end));
                }
                Ok(SymbolData::FrameProcedure(..)) => {
                    if let Some(&(_, proc_end)) = self.symbol_stack.last() {
                        self.symbol_stack.push((sym.index(), proc_end));
                    }
                }
                Ok(SymbolData::Local(..)) => {
                    last_local = Some(sym.index());
                }
                Ok(SymbolData::DefRange(..))
                | Ok(SymbolData::DefRangeSubField(..))
                | Ok(SymbolData::DefRangeRegister(..))
                | Ok(SymbolData::DefRangeFramePointerRelative(..))
                | Ok(SymbolData::DefRangeFramePointerRelativeFullScope(..))
                | Ok(SymbolData::DefRangeSubFieldRegister(..))
                | Ok(SymbolData::DefRangeRegisterRelative(..)) => {
                    // I'd like to retract my previous statement that someone could possibly
                    // understand this:
                    // These symbol types impact the previous symbol, if it was a local
                    // BUT ALSO!! PART III REVENGE OF THE SYM-TH: You can have more than one of
                    // these and they all (?? it's undocumented) apply to the last local, PROBABLY
                    if let Some(last) = last_local {
                        self.add_symbol_child(last, sym.index());
                    } else {
                        self.log(|| format!("Found def range with no last local: {:?}", p));
                    }
                }
                _ => {}
            }
        }
        assert!(self.symbol_stack.is_empty());
        // Add thunks at the end as per above
        top_level_syms.extend(thunk_syms);

        // Restart and do the processing for real this time
        if let Some(first) = first {
            symbols.seek(first);
        }

        let mut final_symbols = HashSet::new();

        for root_idx in top_level_syms {
            for child_idx in self.walk_children(root_idx).into_iter() {
                let &sym = unparsed_syms
                    .get(&child_idx)
                    .expect("should have parsed this");

                self.log(|| format!("Symbol {:?} ", sym.index()));
                let (name, address) =
                    if let Some(parsed) = self.handle_symbol_index(sym.index(), &sym)? {
                        final_symbols.insert(sym.index());
                        match parsed {
                            ParsedSymbol::Data(ParsedDataSymbol { name, address, .. }) => {
                                (Some(name.clone()), Some(*address))
                            }
                            ParsedSymbol::Procedure(ParsedProcedure { name, address, .. }) => {
                                (Some(name.clone()), Some(*address))
                            }
                            _ => (None, None),
                        }
                    } else {
                        (None, None)
                    };

                if let Some(name) = name {
                    self.named_symbols.insert(name.raw_name, sym.index());
                }
                if let Some(address) = address {
                    if !self.addressed_symbols.contains_key(&address) {
                        self.addressed_symbols.insert(address, vec![]);
                    }
                    self.addressed_symbols
                        .get_mut(&address)
                        .expect("just created this")
                        .push(
                            self.indexed_symbols
                                .get(&sym.index())
                                .ok_or_else(|| anyhow!("Can't find sym {} ?", sym.index()))?
                                .clone(),
                        );
                }
            }
        }

        let filtered_symbols = mem::replace(&mut self.indexed_symbols, BTreeMap::new())
            .into_iter()
            .filter_map(|(idx, sym)| {
                if final_symbols.contains(&idx) {
                    Some(sym)
                } else {
                    None
                }
            })
            .collect::<Vec<_>>();

        // The symbols overlap between modules or something, so we can't keep this info around
        self.symbol_tree.clear();
        self.module_cpu_type = None;

        Ok(filtered_symbols)
    }

    /// Set a symbol to be the parent of another, building the symbol tree
    fn add_symbol_child(&mut self, parent: SymbolIndex, child: SymbolIndex) {
        if let Some(tree) = self.symbol_tree.get_mut(&parent) {
            tree.push(child);
        } else {
            self.symbol_tree.insert(parent, Vec::from([child]));
        }

        self.symbol_parents.insert(child, parent);
    }

    /// Postorder traversal of children of symbol index (only during this module parse)
    fn walk_children(&self, sym: SymbolIndex) -> Vec<SymbolIndex> {
        let mut children = vec![];

        if let Some(tree) = self.symbol_tree.get(&sym) {
            for &child in tree {
                children.extend(self.walk_children(child).into_iter());
            }
        }

        children.push(sym);
        children
    }

    /// Direct children of symbol index (only during this module parse)
    fn symbol_children(&self, sym: SymbolIndex) -> Vec<SymbolIndex> {
        if let Some(tree) = self.symbol_tree.get(&sym) {
            tree.clone()
        } else {
            vec![]
        }
    }

    /// Direct parent of symbol index (only during this module parse)
    #[allow(dead_code)]
    fn symbol_parent(&self, sym: SymbolIndex) -> Option<SymbolIndex> {
        self.symbol_parents.get(&sym).copied()
    }

    /// Find symbol by index (only during this module parse)
    fn lookup_symbol(&self, sym: &SymbolIndex) -> Option<&ParsedSymbol> {
        self.indexed_symbols.get(sym)
    }

    /// Parse a new symbol by its index
    fn handle_symbol_index(
        &mut self,
        idx: SymbolIndex,
        sym: &Symbol,
    ) -> Result<Option<&ParsedSymbol>> {
        if let None = self.indexed_symbols.get(&idx) {
            match sym.parse() {
                Ok(data) => match self.handle_symbol(idx, &data) {
                    Ok(Some(parsed)) => {
                        self.log(|| format!("Symbol {} parsed into: {:?}", idx, parsed));
                        self.indexed_symbols.insert(idx, parsed);
                    }
                    Ok(None) => {}
                    e => {
                        self.log(|| format!("Error parsing symbol {}: {:?}", idx, e));
                    }
                },
                Err(UnimplementedSymbolKind(k)) => {
                    self.log(|| format!("Not parsing unimplemented symbol {}: kind {:x?}", idx, k));
                }
                Err(e) => {
                    self.log(|| format!("Could not parse symbol: {}: {}", idx, e));
                }
            };
        }

        Ok(self.indexed_symbols.get(&idx))
    }

    /// Parse a new symbol's data
    fn handle_symbol(
        &mut self,
        index: SymbolIndex,
        data: &SymbolData,
    ) -> Result<Option<ParsedSymbol>> {
        match data {
            SymbolData::ScopeEnd => self.handle_scope_end_symbol(index),
            SymbolData::ObjName(data) => self.handle_obj_name_symbol(index, data),
            SymbolData::RegisterVariable(data) => self.handle_register_variable_symbol(index, data),
            SymbolData::Constant(data) => self.handle_constant_symbol(index, data),
            SymbolData::UserDefinedType(data) => self.handle_user_defined_type_symbol(index, data),
            SymbolData::MultiRegisterVariable(data) => {
                self.handle_multi_register_variable_symbol(index, data)
            }
            SymbolData::Data(data) => self.handle_data_symbol(index, data),
            SymbolData::Public(data) => self.handle_public_symbol(index, data),
            SymbolData::Procedure(data) => self.handle_procedure_symbol(index, data),
            SymbolData::ThreadStorage(data) => self.handle_thread_storage_symbol(index, data),
            SymbolData::CompileFlags(data) => self.handle_compile_flags_symbol(index, data),
            SymbolData::UsingNamespace(data) => self.handle_using_namespace_symbol(index, data),
            SymbolData::ProcedureReference(data) => {
                self.handle_procedure_reference_symbol(index, &data)
            }
            SymbolData::DataReference(data) => self.handle_data_reference_symbol(index, data),
            SymbolData::AnnotationReference(data) => {
                self.handle_annotation_reference_symbol(index, data)
            }
            SymbolData::Trampoline(data) => self.handle_trampoline_symbol(index, data),
            SymbolData::Export(data) => self.handle_export_symbol(index, data),
            SymbolData::Local(data) => self.handle_local_symbol(index, data),
            SymbolData::BuildInfo(data) => self.handle_build_info_symbol(index, data),
            SymbolData::InlineSite(data) => self.handle_inline_site_symbol(index, data),
            SymbolData::InlineSiteEnd => self.handle_inline_site_end_symbol(index),
            SymbolData::ProcedureEnd => self.handle_procedure_end_symbol(index),
            SymbolData::Label(data) => self.handle_label_symbol(index, data),
            SymbolData::Block(data) => self.handle_block_symbol(index, data),
            SymbolData::RegisterRelative(data) => self.handle_register_relative_symbol(index, data),
            SymbolData::Thunk(data) => self.handle_thunk_symbol(index, data),
            SymbolData::SeparatedCode(data) => self.handle_separated_code_symbol(index, data),
            SymbolData::DefRange(data) => self.handle_def_range(index, &data),
            SymbolData::DefRangeSubField(data) => self.handle_def_range_sub_field(index, data),
            SymbolData::DefRangeRegister(data) => self.handle_def_range_register(index, data),
            SymbolData::DefRangeFramePointerRelative(data) => {
                self.handle_def_range_frame_pointer_relative_symbol(index, data)
            }
            SymbolData::DefRangeFramePointerRelativeFullScope(data) => {
                self.handle_def_range_frame_pointer_relative_full_scope_symbol(index, data)
            }
            SymbolData::DefRangeSubFieldRegister(data) => {
                self.handle_def_range_sub_field_register_symbol(index, data)
            }
            SymbolData::DefRangeRegisterRelative(data) => {
                self.handle_def_range_register_relative_symbol(index, data)
            }
            SymbolData::BasePointerRelative(data) => {
                self.handle_base_pointer_relative_symbol(index, data)
            }
            SymbolData::FrameProcedure(data) => self.handle_frame_procedure_symbol(index, data),
            SymbolData::CallSiteInfo(data) => self.handle_call_site_info(index, data),
            e => Err(anyhow!("Unhandled symbol type {:?}", e)),
        }
    }

    fn handle_scope_end_symbol(&mut self, _index: SymbolIndex) -> Result<Option<ParsedSymbol>> {
        self.log(|| "Got ScopeEnd symbol");
        Ok(None)
    }

    fn handle_obj_name_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &ObjNameSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got ObjName symbol: {:?}", data));
        Ok(None)
    }

    fn handle_register_variable_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &RegisterVariableSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got RegisterVariable symbol: {:?}", data));

        let storage = if let Some(reg) = self.convert_register(data.register) {
            vec![ParsedLocation {
                location: Variable {
                    ty: VariableSourceType::RegisterVariableSourceType,
                    index: 0,
                    storage: reg.0 as i64,
                },
                base_relative: false,
                stack_relative: false,
            }]
        } else {
            // TODO: What do we do here?
            vec![]
        };

        Ok(Some(ParsedSymbol::LocalVariable(ParsedVariable {
            name: data.name.to_string().to_string(),
            type_: self.lookup_type_conf(&data.type_index, false)?,
            storage,
            is_param: data.slot.map_or(true, |slot| slot > 0),
        })))
    }

    fn handle_constant_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &ConstantSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got Constant symbol: {:?}", data));
        Ok(None)
    }

    fn handle_user_defined_type_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &UserDefinedTypeSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got UserDefinedType symbol: {:?}", data));
        Ok(None)
    }

    fn handle_multi_register_variable_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &MultiRegisterVariableSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got MultiRegisterVariable symbol: {:?}", data));
        Ok(None)
    }

    fn handle_data_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &DataSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got Data symbol: {:?}", data));

        let rva = data.offset.to_rva(&self.address_map).unwrap_or_default();
        let raw_name = data.name.to_string().to_string();
        let (t, name) = self.demangle_to_type(&raw_name, rva)?;
        let name = name.map(|n| n.to_string());

        // Sometimes the demangler REALLY knows what type this is supposed to be, and the
        // data symbol is actually wrong. So in those cases, let the demangler take precedence
        // Otherwise-- the demangler is usually wrong and clueless
        let data_type = t.merge(self.lookup_type_conf(&data.type_index, false)?);

        // Ignore symbols with no name and no type
        if !self.settings.get_bool_with_opts(
            "pdb.features.allowUnnamedVoidSymbols",
            &mut self.settings_query_opts,
        ) && name.is_none()
        {
            if let Some(ty) = &data_type {
                if ty.contents.type_class() == TypeClass::VoidTypeClass {
                    return Ok(None);
                }
            } else {
                return Ok(None);
            }
        }

        let name = SymbolNames {
            raw_name,
            short_name: name.clone(),
            full_name: name,
        };

        self.log(|| {
            format!(
                "DATA: 0x{:x}: {:?} {:?}",
                self.bv.start() + rva.0 as u64,
                &name,
                &data_type
            )
        });

        Ok(Some(ParsedSymbol::Data(ParsedDataSymbol {
            is_public: false,
            address: self.bv.start() + rva.0 as u64,
            name,
            type_: data_type,
        })))
    }

    fn handle_public_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &PublicSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got Public symbol: {:?}", data));
        let rva = data.offset.to_rva(&self.address_map).unwrap_or_default();
        let raw_name = data.name.to_string().to_string();
        let (t, name) = self.demangle_to_type(&raw_name, rva)?;
        let name = name.map(|n| n.to_string());

        let name = SymbolNames {
            raw_name,
            short_name: name.clone(),
            full_name: name,
        };

        // These are generally low confidence because we only have the demangler to inform us of type

        if data.function {
            self.log(|| {
                format!(
                    "PUBLIC FUNCTION: 0x{:x}: {:?} {:?}",
                    self.bv.start() + rva.0 as u64,
                    &name,
                    t
                )
            });

            Ok(Some(ParsedSymbol::Procedure(ParsedProcedure {
                is_public: true,
                address: self.bv.start() + rva.0 as u64,
                name,
                type_: t,
                locals: vec![],
            })))
        } else {
            self.log(|| {
                format!(
                    "PUBLIC DATA: 0x{:x}: {:?} {:?}",
                    self.bv.start() + rva.0 as u64,
                    &name,
                    t
                )
            });

            Ok(Some(ParsedSymbol::Data(ParsedDataSymbol {
                is_public: true,
                address: self.bv.start() + rva.0 as u64,
                name,
                type_: t,
            })))
        }
    }

    /// Given a proc symbol index and guessed type (from demangler or tpi), find all the local variables
    /// and parameters related to that symbol.
    /// Returns Ok(Some((resolved params, locals))))
    fn lookup_locals(
        &self,
        index: SymbolIndex,
        type_index: TypeIndex,
        demangled_type: Option<Conf<Ref<Type>>>,
    ) -> Result<(Option<Conf<Ref<Type>>>, Vec<ParsedVariable>)> {
        // So generally speaking, here's the information we have:
        // - The function type is usually accurate wrt the parameter locations
        // - The parameter symbols have the names we want for the params
        // - The parameter symbols are a big ugly mess
        // We basically want to take the function type from the type, and just fill in the
        // names of all the parameters. Non-param locals don't really matter since binja
        // can't handle them anyway.

        // Type parameters order needs to be like this:
        // 1. `this` pointer (if exists)
        // 2. Various stack params
        // 3. Various register params
        // We assume that if a parameter is found in a register, that is where it is passed.
        // Otherwise they are in the default order as per the CC

        // Get child objects and search for local variable names
        let mut locals = vec![];
        let mut params = vec![];
        let mut known_frame = false;
        for child in self.symbol_children(index) {
            match self.lookup_symbol(&child) {
                Some(ParsedSymbol::ProcedureInfo(info)) => {
                    params = info.params.clone();
                    locals = info.locals.clone();
                    known_frame = true;
                }
                _ => {}
            }
        }

        let raw_type = self.lookup_type_conf(&type_index, false)?;
        let fancy_type = self.lookup_type_conf(&type_index, true)?;

        // Best guess so far in case of error handling
        let fancier_type = fancy_type
            .clone()
            .merge(raw_type.clone())
            .merge(demangled_type.clone());

        if !known_frame {
            return Ok((fancier_type, vec![]));
        }

        // We need both of these to exist (not sure why they wouldn't)
        let (raw_type, fancy_type) = match (raw_type, fancy_type) {
            (Some(raw), Some(fancy)) => (raw, fancy),
            _ => return Ok((fancier_type, locals)),
        };

        let raw_params = raw_type.contents.parameters().ok_or(anyhow!("no params"))?;
        let mut fancy_params = fancy_type
            .contents
            .parameters()
            .ok_or(anyhow!("no params"))?;

        // Collect all the parameters we are expecting from the symbols
        let mut parsed_params = vec![];
        for p in &params {
            let param = FunctionParameter::new(
                p.type_.clone().merge(Conf::new(
                    Type::int(self.arch.address_size(), false),
                    MIN_CONFIDENCE,
                )),
                p.name.clone(),
                p.storage.first().map(|loc| loc.location.into()),
            );
            // Ignore thisptr because it's not technically part of the raw type signature
            if p.name != "this" {
                parsed_params.push(param);
            }
        }
        let mut parsed_locals = vec![];
        for p in &locals {
            let param = FunctionParameter::new(
                p.type_.clone().merge(Conf::new(
                    Type::int(self.arch.address_size(), false),
                    MIN_CONFIDENCE,
                )),
                p.name.clone(),
                p.storage.first().map(|loc| loc.location.into()),
            );
            // Ignore thisptr because it's not technically part of the raw type signature
            if p.name != "this" {
                parsed_locals.push(param);
            }
        }

        self.log(|| format!("Raw params:    {:#x?}", raw_params));
        self.log(|| format!("Fancy params:  {:#x?}", fancy_params));
        self.log(|| format!("Parsed params: {:#x?}", parsed_params));

        // We expect one parameter for each unnamed parameter in the marked up type
        let expected_param_count = fancy_params.iter().filter(|p| p.name.is_empty()).count();
        // Sanity
        if expected_param_count != raw_params.len() {
            return Err(anyhow!(
                "Mismatched number of formal parameters and interpreted parameters"
            ));
        }

        // If we don't have enough parameters to fill the slots, there's a problem here
        // So just fallback to the unnamed params
        if expected_param_count > parsed_params.len() {
            // As per reversing of msdia140.dll (and nowhere else): if a function doesn't have
            // enough parameter variables declared as parameters, the remaining parameters are
            // the first however many locals. If you don't have enough of those, idk??
            if expected_param_count > (parsed_params.len() + parsed_locals.len()) {
                return Ok((fancier_type, locals));
            }
            parsed_params.extend(parsed_locals);
        }
        let expected_parsed_params = parsed_params
            .drain(0..expected_param_count)
            .collect::<Vec<_>>();

        // For all formal parameters, apply names to them in fancy_params
        // These should be all types in fancy_params that are unnamed (named ones we inserted)

        let mut i = 0;
        for p in fancy_params.iter_mut() {
            if p.name.is_empty() {
                if p.ty.contents != expected_parsed_params[i].ty.contents {
                    self.log(|| {
                        format!(
                            "Suspicious parameter {}: {:?} vs {:?}",
                            i, p, expected_parsed_params[i]
                        )
                    });
                }
                if expected_parsed_params[i].name.as_str() == "__formal" {
                    p.name = format!("__formal{}", i);
                } else {
                    p.name = expected_parsed_params[i].name.clone();
                }
                i += 1;
            }
        }

        let cc = fancy_type
            .contents
            .calling_convention()
            .map_or_else(|| Conf::new(self.default_cc.clone(), 0), |cc| cc);

        self.log(|| {
            format!(
                "Type calling convention: {:?}",
                fancy_type.contents.calling_convention()
            )
        });
        self.log(|| format!("Default calling convention: {:?}", self.default_cc));
        self.log(|| format!("Result calling convention: {:?}", cc));
        self.log(|| format!("Final params: {:#x?}", fancy_params));

        // Use the new locals we've parsed to make the Real Definitely True function type
        let fancy_type = Conf::new(
            Type::function_with_opts(
                &fancy_type
                    .contents
                    .return_value()
                    .ok_or(anyhow!("no ret"))?,
                fancy_params.as_slice(),
                fancy_type.contents.has_variable_arguments().contents,
                cc,
                fancy_type.contents.stack_adjustment(),
            ),
            MAX_CONFIDENCE,
        );

        let fancier_type = fancy_type
            .clone()
            .merge(raw_type.clone())
            .merge(demangled_type.clone());

        self.log(|| format!("Raw type:       {:#x?}", raw_type));
        self.log(|| format!("Demangled type: {:#x?}", demangled_type));
        self.log(|| format!("Fancy type:     {:#x?}", fancy_type));
        self.log(|| format!("Result type:    {:#x?}", fancier_type));

        Ok((Some(fancier_type), locals))
    }

    fn handle_procedure_symbol(
        &mut self,
        index: SymbolIndex,
        data: &ProcedureSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got Procedure symbol: {:?}", data));

        let rva = data.offset.to_rva(&self.address_map).unwrap_or_default();
        let address = self.bv.start() + rva.0 as u64;

        let mut raw_name = data.name.to_string().to_string();

        // Generally proc symbols have real types, but use the demangler just in case the microsoft
        // public pdbs have the function type as `void`
        let (t, name) = self.demangle_to_type(&raw_name, rva)?;
        let mut name = name.map(|n| n.to_string());

        // Some proc symbols don't have a mangled name, so try and look up their name
        if name.is_none() || name.as_ref().expect("just failed none") == &raw_name {
            // Lookup public symbol with the same name
            if let Some(others) = self.addressed_symbols.get(&address) {
                for o in others {
                    match o {
                        ParsedSymbol::Procedure(ParsedProcedure {
                            name: proc_name, ..
                        }) => {
                            if proc_name.full_name.as_ref().unwrap_or(&proc_name.raw_name)
                                == &raw_name
                            {
                                name = Some(raw_name);
                                raw_name = proc_name.raw_name.clone();
                                break;
                            }
                        }
                        _ => {}
                    }
                }
            }
        }

        let (fn_type, locals) = self.lookup_locals(index, data.type_index, t)?;

        let name = SymbolNames {
            raw_name,
            short_name: name.clone(),
            full_name: name,
        };

        self.log(|| format!("PROC: 0x{:x}: {:?} {:?}", address, &name, &fn_type));

        Ok(Some(ParsedSymbol::Procedure(ParsedProcedure {
            is_public: false,
            address,
            name,
            type_: fn_type,
            locals,
        })))
    }

    fn handle_thread_storage_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &ThreadStorageSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got ThreadStorage symbol: {:?}", data));
        Ok(None)
    }

    fn handle_compile_flags_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &CompileFlagsSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got CompileFlags symbol: {:?}", data));
        self.module_cpu_type = Some(data.cpu_type);
        Ok(None)
    }

    fn handle_using_namespace_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &UsingNamespaceSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got UsingNamespace symbol: {:?}", data));
        Ok(None)
    }

    fn handle_procedure_reference_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &ProcedureReferenceSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got ProcedureReference symbol: {:?}", data));
        Ok(None)
    }

    fn handle_data_reference_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &DataReferenceSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got DataReference symbol: {:?}", data));
        Ok(None)
    }

    fn handle_annotation_reference_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &AnnotationReferenceSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got AnnotationReference symbol: {:?}", data));
        Ok(None)
    }

    fn handle_trampoline_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &TrampolineSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got Trampoline symbol: {:?}", data));
        let rva = data.thunk.to_rva(&self.address_map).unwrap_or_default();
        let target_rva = data.target.to_rva(&self.address_map).unwrap_or_default();

        let address = self.bv.start() + rva.0 as u64;
        let target_address = self.bv.start() + target_rva.0 as u64;

        let mut target_name = None;
        let mut thunk_name = None;

        let mut fn_type: Option<Conf<Ref<Type>>> = None;

        // These have the same name as their target, so look that up
        if let Some(syms) = self.addressed_symbols.get(&target_address) {
            // Take name from the public symbol
            for sym in syms {
                match sym {
                    ParsedSymbol::Procedure(proc) if proc.is_public => {
                        fn_type = proc.type_.clone().merge(fn_type);
                        target_name = Some(proc.name.clone());
                    }
                    _ => {}
                }
            }
            // Take type from the non-public symbol if we have one
            for sym in syms {
                match sym {
                    ParsedSymbol::Procedure(proc) if !proc.is_public => {
                        fn_type = proc.type_.clone().merge(fn_type);
                        if target_name.is_none() {
                            target_name = Some(proc.name.clone());
                        }
                    }
                    _ => {}
                }
            }
        }

        // And handle the fact that pdb public symbols for trampolines have the name of their target
        // ugh
        if let Some(syms) = self.addressed_symbols.get_mut(&address) {
            if let [ParsedSymbol::Procedure(proc)] = syms.as_mut_slice() {
                if let Some(tn) = &target_name {
                    if proc.name.raw_name == tn.raw_name
                        || proc.name.full_name.as_ref().unwrap_or(&proc.name.raw_name)
                            == tn.full_name.as_ref().unwrap_or(&tn.raw_name)
                    {
                        // Yeah it's one of these symbols
                        let old_name = proc.name.clone();
                        let new_name = SymbolNames {
                            raw_name: "j_".to_string() + &old_name.raw_name,
                            short_name: old_name.short_name.as_ref().map(|n| "j_".to_string() + n),
                            full_name: old_name.full_name.as_ref().map(|n| "j_".to_string() + n),
                        };

                        // I'm so sorry about this
                        // XXX: Update the parsed public symbol's name to use j_ syntax
                        if let Some(idx) = self.named_symbols.remove(&old_name.raw_name) {
                            self.named_symbols.insert(new_name.raw_name.clone(), idx);
                        }
                        if let Some(idx) = self.parsed_symbols_by_name.remove(&old_name.raw_name) {
                            self.parsed_symbols_by_name
                                .insert(new_name.raw_name.clone(), idx);
                            match &mut self.parsed_symbols[idx] {
                                ParsedSymbol::Data(ParsedDataSymbol {
                                    name: parsed_name, ..
                                })
                                | ParsedSymbol::Procedure(ParsedProcedure {
                                    name: parsed_name,
                                    ..
                                }) => {
                                    parsed_name.raw_name = new_name.raw_name.clone();
                                    parsed_name.short_name = new_name.short_name.clone();
                                    parsed_name.full_name = new_name.full_name.clone();
                                }
                                _ => {}
                            }
                        }
                        proc.name = new_name.clone();
                        thunk_name = Some(new_name);
                    }
                }
            }
        }

        if thunk_name.is_none() {
            if let Some(tn) = target_name {
                thunk_name = Some(SymbolNames {
                    raw_name: "j_".to_string() + &tn.raw_name,
                    short_name: tn.short_name.as_ref().map(|n| "j_".to_string() + n),
                    full_name: tn.full_name.as_ref().map(|n| "j_".to_string() + n),
                });
            }
        }

        let name = thunk_name.unwrap_or(SymbolNames {
            raw_name: format!("j_sub_{:x}", target_address),
            short_name: None,
            full_name: None,
        });

        self.log(|| format!("TRAMPOLINE: 0x{:x}: {:?} {:?}", address, &name, &fn_type));

        Ok(Some(ParsedSymbol::Procedure(ParsedProcedure {
            is_public: false,
            address,
            name,
            type_: fn_type,
            locals: vec![],
        })))
    }

    fn handle_export_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &ExportSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got Export symbol: {:?}", data));
        Ok(None)
    }

    fn handle_local_symbol(
        &mut self,
        index: SymbolIndex,
        data: &LocalSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got Local symbol: {:?}", data));
        // Look for definition ranges for this symbol
        let mut locations: Vec<ParsedLocation> = vec![];
        for child in self.symbol_children(index) {
            match self.lookup_symbol(&child) {
                Some(ParsedSymbol::Location(loc)) => {
                    locations.push(*loc);
                }
                _ => {}
            }
        }

        Ok(Some(ParsedSymbol::LocalVariable(ParsedVariable {
            name: data.name.to_string().to_string(),
            type_: self.lookup_type_conf(&data.type_index, false)?,
            storage: locations,
            is_param: data.flags.isparam,
        })))
    }

    fn handle_build_info_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &BuildInfoSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got BuildInfo symbol: {:?}", data));
        Ok(None)
    }

    fn handle_inline_site_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &InlineSiteSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got InlineSite symbol: {:?}", data));
        Ok(None)
    }

    fn handle_inline_site_end_symbol(
        &mut self,
        _index: SymbolIndex,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| "Got InlineSiteEnd symbol");
        Ok(None)
    }

    fn handle_procedure_end_symbol(&mut self, _index: SymbolIndex) -> Result<Option<ParsedSymbol>> {
        self.log(|| "Got ProcedureEnd symbol");
        Ok(None)
    }

    fn handle_label_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &LabelSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got Label symbol: {:?}", data));
        Ok(None)
    }

    fn handle_block_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &BlockSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got Block symbol: {:?}", data));
        Ok(None)
    }

    fn handle_register_relative_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &RegisterRelativeSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got RegisterRelative symbol: {:?}", data));
        match self.lookup_register(data.register) {
            Some(X86(X86Register::EBP)) | Some(AMD64(AMD64Register::RBP)) => {
                // Local is relative to base pointer
                // This is just another way of writing BasePointerRelativeSymbol
                Ok(Some(ParsedSymbol::LocalVariable(ParsedVariable {
                    name: data.name.to_string().to_string(),
                    type_: self.lookup_type_conf(&data.type_index, false)?,
                    storage: vec![ParsedLocation {
                        location: Variable {
                            ty: VariableSourceType::StackVariableSourceType,
                            index: 0,
                            storage: data.offset as i64,
                        },
                        base_relative: true,   // !!
                        stack_relative: false, // !!
                    }],
                    is_param: data.slot.map_or(false, |slot| slot > 0),
                })))
            }
            Some(X86(X86Register::ESP)) | Some(AMD64(AMD64Register::RSP)) => {
                // Local is relative to stack pointer
                // This is the same as base pointer case except not base relative (ofc)
                Ok(Some(ParsedSymbol::LocalVariable(ParsedVariable {
                    name: data.name.to_string().to_string(),
                    type_: self.lookup_type_conf(&data.type_index, false)?,
                    storage: vec![ParsedLocation {
                        location: Variable {
                            ty: VariableSourceType::StackVariableSourceType,
                            index: 0,
                            storage: data.offset as i64,
                        },
                        base_relative: false, // !!
                        stack_relative: true, // !!
                    }],
                    is_param: data.slot.map_or(false, |slot| slot > 0),
                })))
            }
            _ => {
                // Local is relative to some non-bp register.
                // This is, of course, totally possible and normal
                // Binja just can't handle it in the slightest.
                // Soooooooo ????
                // TODO
                Ok(None)
            }
        }
    }

    fn handle_thunk_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &ThunkSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got Thunk symbol: {:?}", data));
        let rva = data.offset.to_rva(&self.address_map).unwrap_or_default();
        let raw_name = data.name.to_string().to_string();
        let address = self.bv.start() + rva.0 as u64;

        let (t, name) = self.demangle_to_type(&raw_name, rva)?;
        let name = name.map(|n| n.to_string());
        let mut fn_type = t;

        // These have the same name as their target, so look that up
        if let Some(&idx) = self.named_symbols.get(&raw_name) {
            if let Some(ParsedSymbol::Procedure(proc)) = self.indexed_symbols.get(&idx) {
                fn_type = proc.type_.clone().merge(fn_type);
            }
        }

        let mut thunk_name = None;

        // And handle the fact that pdb public symbols for thunks have the name of their target
        // ugh
        if let Some(syms) = self.addressed_symbols.get_mut(&address) {
            if let [ParsedSymbol::Procedure(proc)] = syms.as_mut_slice() {
                // Yeah it's one of these symbols
                // Make sure we don't do this twice (does that even happen?)
                if !proc.name.raw_name.starts_with("j_") {
                    let old_name = proc.name.clone();
                    let new_name = SymbolNames {
                        raw_name: "j_".to_string() + &old_name.raw_name,
                        short_name: Some(
                            "j_".to_string() + old_name.short_name.as_ref().unwrap_or(&raw_name),
                        ),
                        full_name: Some(
                            "j_".to_string() + old_name.full_name.as_ref().unwrap_or(&raw_name),
                        ),
                    };

                    // I'm so sorry about this
                    // XXX: Update the parsed public symbol's name to use j_ syntax
                    if let Some(idx) = self.named_symbols.remove(&old_name.raw_name) {
                        self.named_symbols.insert(new_name.raw_name.clone(), idx);
                    }
                    if let Some(idx) = self.parsed_symbols_by_name.remove(&old_name.raw_name) {
                        self.parsed_symbols_by_name
                            .insert(new_name.raw_name.clone(), idx);
                        match &mut self.parsed_symbols[idx] {
                            ParsedSymbol::Data(ParsedDataSymbol {
                                name: parsed_name, ..
                            })
                            | ParsedSymbol::Procedure(ParsedProcedure {
                                name: parsed_name, ..
                            }) => {
                                parsed_name.raw_name = new_name.raw_name.clone();
                                parsed_name.short_name = new_name.short_name.clone();
                                parsed_name.full_name = new_name.full_name.clone();
                            }
                            _ => {}
                        }
                    }
                    proc.name = new_name.clone();
                    thunk_name = Some(new_name);
                }
            }
        }

        let locals = vec![];
        let name = thunk_name.unwrap_or(SymbolNames {
            raw_name,
            short_name: name.clone(),
            full_name: name,
        });

        self.log(|| format!("THUNK: 0x{:x}: {:?} {:?}", address, &name, &fn_type));

        Ok(Some(ParsedSymbol::Procedure(ParsedProcedure {
            is_public: false,
            address,
            name,
            type_: fn_type,
            locals,
        })))
    }

    fn handle_separated_code_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &SeparatedCodeSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got SeparatedCode symbol: {:?}", data));
        Ok(None)
    }

    fn handle_def_range(
        &mut self,
        _index: SymbolIndex,
        data: &DefRangeSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got DefRange symbol: {:?}", data));
        Ok(None)
    }

    fn handle_def_range_sub_field(
        &mut self,
        _index: SymbolIndex,
        data: &DefRangeSubFieldSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got DefRangeSubField symbol: {:?}", data));
        Ok(None)
    }

    fn handle_def_range_register(
        &mut self,
        _index: SymbolIndex,
        data: &DefRangeRegisterSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got DefRangeRegister symbol: {:?}", data));
        if let Some(reg) = self.convert_register(data.register) {
            Ok(Some(ParsedSymbol::Location(ParsedLocation {
                location: Variable {
                    ty: VariableSourceType::RegisterVariableSourceType,
                    index: 0,
                    storage: reg.0 as i64,
                },
                base_relative: false,
                stack_relative: false,
            })))
        } else {
            Ok(None)
        }
    }

    fn handle_def_range_frame_pointer_relative_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &DefRangeFramePointerRelativeSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got DefRangeFramePointerRelative symbol: {:?}", data));
        Ok(Some(ParsedSymbol::Location(ParsedLocation {
            location: Variable {
                ty: VariableSourceType::StackVariableSourceType,
                index: 0,
                storage: data.offset as i64,
            },
            base_relative: true,
            stack_relative: false,
        })))
    }

    fn handle_def_range_frame_pointer_relative_full_scope_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &DefRangeFramePointerRelativeFullScopeSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| {
            format!(
                "Got DefRangeFramePointerRelativeFullScope symbol: {:?}",
                data
            )
        });
        Ok(Some(ParsedSymbol::Location(ParsedLocation {
            location: Variable {
                ty: VariableSourceType::StackVariableSourceType,
                index: 0,
                storage: data.offset as i64,
            },
            base_relative: true,
            stack_relative: false,
        })))
    }

    fn handle_def_range_sub_field_register_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &DefRangeSubFieldRegisterSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got DefRangeSubFieldRegister symbol: {:?}", data));
        Ok(None)
    }

    fn handle_def_range_register_relative_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &DefRangeRegisterRelativeSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got DefRangeRegisterRelative symbol: {:?}", data));
        match self.lookup_register(data.base_register) {
            Some(X86(X86Register::EBP)) | Some(AMD64(AMD64Register::RBP)) => {
                Ok(Some(ParsedSymbol::Location(ParsedLocation {
                    location: Variable {
                        ty: VariableSourceType::StackVariableSourceType,
                        index: 0,
                        storage: data.offset_base_pointer as i64,
                    },
                    base_relative: true,
                    stack_relative: false,
                })))
            }
            Some(X86(X86Register::ESP)) | Some(AMD64(AMD64Register::RSP)) => {
                Ok(Some(ParsedSymbol::Location(ParsedLocation {
                    location: Variable {
                        ty: VariableSourceType::StackVariableSourceType,
                        index: 0,
                        storage: data.offset_base_pointer as i64,
                    },
                    base_relative: false,
                    stack_relative: true,
                })))
            }
            _ => Ok(None),
        }
    }

    fn handle_base_pointer_relative_symbol(
        &mut self,
        _index: SymbolIndex,
        data: &BasePointerRelativeSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got BasePointerRelative symbol: {:?}", data));

        // These are usually parameters if offset > 0

        Ok(Some(ParsedSymbol::LocalVariable(ParsedVariable {
            name: data.name.to_string().to_string(),
            type_: self.lookup_type_conf(&data.type_index, false)?,
            storage: vec![ParsedLocation {
                location: Variable {
                    ty: VariableSourceType::StackVariableSourceType,
                    index: 0,
                    storage: data.offset as i64,
                },
                base_relative: true,
                stack_relative: false,
            }],
            is_param: data.offset as i64 > 0 || data.slot.map_or(false, |slot| slot > 0),
        })))
    }

    fn handle_frame_procedure_symbol(
        &mut self,
        index: SymbolIndex,
        data: &FrameProcedureSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got FrameProcedure symbol: {:?}", data));

        // This symbol generally comes before a proc and all various parameters
        // It has a lot of information we don't care about, and some information we maybe do?
        // This function also tries to find all the locals and parameters of the procedure

        let mut params = vec![];
        let mut locals = vec![];
        let mut seen_offsets = HashSet::new();

        for child in self.walk_children(index) {
            if child == index {
                continue;
            }
            match self.lookup_symbol(&child) {
                Some(ParsedSymbol::LocalVariable(ParsedVariable {
                    name,
                    type_,
                    storage,
                    is_param,
                    ..
                })) => {
                    // Adjust RSP-relative locations to RSP_entry-relative before param detection.
                    // Only for x86_64, because REGREL32 RSP offsets are measured after the prolog >:(
                    // On x86, offsets are already relative to the entry stack pointer
                    let frame_adjustment = if self.arch.address_size() == 8 {
                        data.frame_byte_count as i64
                    } else {
                        0
                    };
                    let adjusted_storage: Vec<ParsedLocation> = storage
                        .iter()
                        .map(|loc| {
                            if loc.stack_relative {
                                ParsedLocation {
                                    location: Variable {
                                        storage: loc.location.storage - frame_adjustment,
                                        ..loc.location
                                    },
                                    stack_relative: false,
                                    ..*loc
                                }
                            } else {
                                *loc
                            }
                        })
                        .collect();

                    // See if the parameter really is a parameter. Sometimes they don't say they are
                    let mut really_is_param = *is_param;
                    for loc in adjusted_storage.iter() {
                        match loc.location {
                            Variable {
                                ty: VariableSourceType::RegisterVariableSourceType,
                                ..
                            } => {
                                // Assume register vars are always parameters
                                really_is_param = true;
                            }
                            Variable {
                                ty: VariableSourceType::StackVariableSourceType,
                                storage: offset,
                                ..
                            } if offset >= 0 => {
                                // Sometimes you can get two locals at the same offset, both rbp+(x > 0)
                                // I'm guessing from looking at dumps from dia2dump that only the first
                                // one is considered a parameter, although there are times that I see
                                // two params at the same offset and both are considered parameters...
                                // This doesn't seem possible (or correct) because they would overlap
                                // and only one would be useful anyway.
                                // Regardless of the mess, Binja can only handle one parameter per slot
                                // so we're just going to use the first one.
                                really_is_param = seen_offsets.insert(offset);
                            }
                            _ => {}
                        }
                    }

                    let var = ParsedVariable {
                        name: name.clone(),
                        type_: type_.clone(),
                        storage: adjusted_storage,
                        is_param: really_is_param,
                    };
                    if really_is_param {
                        params.push(var);
                    } else {
                        locals.push(var);
                    }
                }
                Some(ParsedSymbol::Data(_)) => {
                    // Apparently you can have static data symbols as parameters
                    // Because of course you can
                }
                None => {}
                e => self.log(|| format!("Unexpected symbol type in frame: {:?}", e)),
            }
        }

        Ok(Some(ParsedSymbol::ProcedureInfo(ParsedProcedureInfo {
            params,
            locals,
        })))
    }

    fn handle_call_site_info(
        &mut self,
        _index: SymbolIndex,
        data: &CallSiteInfoSymbol,
    ) -> Result<Option<ParsedSymbol>> {
        self.log(|| format!("Got CallSiteInfo symbol: {:?}", data));
        Ok(None)
    }

    /// Demangle a name and get a type out
    /// Also fixes void(void) and __s_RTTI_Nonsense
    fn demangle_to_type(
        &self,
        raw_name: &String,
        rva: Rva,
    ) -> Result<(Option<Conf<Ref<Type>>>, Option<QualifiedName>)> {
        let (mut t, mut name) = match demangle_ms(&self.arch, raw_name, true) {
            Some((name, Some(t))) => (Some(Conf::new(t, DEMANGLE_CONFIDENCE)), name),
            Some((name, _)) => (None, name),
            _ => (None, QualifiedName::new(vec![raw_name.clone()])),
        };

        if let Some(ty) = t.as_ref() {
            if ty.contents.type_class() == TypeClass::FunctionTypeClass {
                // demangler makes (void) into (void arg1) which is wrong
                let parameters = ty.contents.parameters().ok_or(anyhow!("no parameters"))?;
                if let [p] = parameters.as_slice() {
                    if p.ty.contents.type_class() == TypeClass::VoidTypeClass {
                        t = Some(Conf::new(
                            Type::function(
                                &ty.contents
                                    .return_value()
                                    .ok_or(anyhow!("no return value"))?,
                                vec![],
                                ty.contents.has_variable_arguments().contents,
                            ),
                            ty.confidence,
                        ))
                    }
                }
            }
        }

        // These have types but they aren't actually set anywhere. So it's the demangler's
        // job to take care of them, apparently?
        static MEM: OnceLock<Vec<(String, Vec<String>)>> = OnceLock::new();
        let name_to_type = MEM.get_or_init(|| {
            vec![
                (
                    "`RTTI Complete Object Locator'".to_string(),
                    vec![
                        "_s_RTTICompleteObjectLocator".to_string(),
                        "_s__RTTICompleteObjectLocator".to_string(),
                        "_s__RTTICompleteObjectLocator2".to_string(),
                    ],
                ),
                (
                    "`RTTI Class Hierarchy Descriptor'".to_string(),
                    vec![
                        "_s_RTTIClassHierarchyDescriptor".to_string(),
                        "_s__RTTIClassHierarchyDescriptor".to_string(),
                        "_s__RTTIClassHierarchyDescriptor2".to_string(),
                    ],
                ),
                (
                    // TODO: This type is dynamic
                    "`RTTI Base Class Array'".to_string(),
                    vec![
                        "_s_RTTIBaseClassArray".to_string(),
                        "_s__RTTIBaseClassArray".to_string(),
                        "_s__RTTIBaseClassArray2".to_string(),
                    ],
                ),
                (
                    "`RTTI Base Class Descriptor at (".to_string(),
                    vec![
                        "_s_RTTIBaseClassDescriptor".to_string(),
                        "_s__RTTIBaseClassDescriptor".to_string(),
                        "_s__RTTICBaseClassDescriptor2".to_string(),
                    ],
                ),
                (
                    "`RTTI Type Descriptor'".to_string(),
                    vec!["_TypeDescriptor".to_string()],
                ),
            ]
        });

        if let Some(last_name) = name.last() {
            for (search_name, search_types) in name_to_type.iter() {
                if last_name.contains(search_name) {
                    for search_type in search_types {
                        if let Some(ty) = self.named_types.get(search_type) {
                            // Fallback in case we don't find a specific one
                            t = Some(Conf::new(
                                Type::named_type_from_type(search_type, ty.as_ref()),
                                DEMANGLE_CONFIDENCE,
                            ));

                            if self.settings.get_bool_with_opts(
                                "pdb.features.expandRTTIStructures",
                                &mut self.settings_query_opts.clone(),
                            ) {
                                if let Some((lengthy_type, length)) =
                                    self.make_lengthy_type(ty, self.bv.start() + rva.0 as u64)?
                                {
                                    // See if we have a type with this length
                                    let lengthy_name =
                                        format!("${}$_extraBytes_{}", search_type, length);

                                    if let Some(ty) = self.named_types.get(&lengthy_name) {
                                        // Wow!
                                        t = Some(Conf::new(
                                            Type::named_type_from_type(lengthy_name, ty.as_ref()),
                                            DEMANGLE_CONFIDENCE,
                                        ));
                                    } else {
                                        t = Some(Conf::new(lengthy_type, DEMANGLE_CONFIDENCE));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // VTables have types on their data symbols
        // Format: "ClassName::`vftable'" or "ClassName::`vftable'{for `BaseClass'}"
        if let Some(vt_name) = parse_vtable_type_name(&name) {
            let ty =
                self.named_types.get(&vt_name).cloned().unwrap_or_else(|| {
                    Type::structure(StructureBuilder::new().finalize().as_ref())
                });
            t = Some(Conf::new(
                Type::named_type_from_type(vt_name, ty.as_ref()),
                DEMANGLE_CONFIDENCE,
            ));
        }

        if let Some(last_name) = name.last_mut() {
            if last_name.starts_with("__imp_") {
                last_name.drain(0..("__imp_".len()));
            }
        }

        let name = if name.len() == 1 && &name[0] == raw_name && raw_name.starts_with('?') {
            None
        } else if name.len() == 1 && name[0].is_empty() {
            None
        } else if name.len() > 0 && name[0].starts_with("\x7f") {
            // Not sure why these exist but they do Weird Stuff
            name[0].drain(0..1);
            Some(name)
        } else {
            Some(name)
        };

        Ok((t, name))
    }

    fn make_lengthy_type(
        &self,
        base_type: &Ref<Type>,
        base_address: u64,
    ) -> Result<Option<(Ref<Type>, usize)>> {
        if base_type.type_class() != TypeClass::StructureTypeClass {
            return Ok(None);
        }
        let structure = base_type
            .get_structure()
            .ok_or(anyhow!("Expected structure"))?;
        let mut members = structure.members();
        let last_member = members.last_mut().ok_or(anyhow!("Not enough members"))?;

        if last_member.ty.contents.type_class() != TypeClass::ArrayTypeClass {
            return Ok(None);
        }
        if last_member.ty.contents.count() != 0 {
            return Ok(None);
        }

        let member_element = last_member
            .ty
            .contents
            .element_type()
            .ok_or(anyhow!("Last member has no type"))?
            .contents;
        let member_width = member_element.width();

        // Read member_width bytes from bv starting at that member, until we read all zeroes
        let member_address = base_address + last_member.offset;

        let mut bytes = vec![0; member_width as usize];

        let mut element_count = 0;
        while self.bv.read(
            bytes.as_mut_slice(),
            member_address + member_width * element_count,
        ) == member_width as usize
        {
            if bytes.iter().all(|&b| b == 0) {
                break;
            }
            element_count += 1;
        }

        // Make a new copy of the type with the correct element count
        last_member.ty.contents = Type::array(&member_element, element_count);

        Ok(Some((
            Type::structure(StructureBuilder::from(members).finalize().as_ref()),
            element_count as usize,
        )))
    }

    /// Sorry about the type names
    /// Given a pdb::Register (u32), get a pdb::register::Register (big enum with names)
    fn lookup_register(&self, reg: pdb::Register) -> Option<pdb::register::Register> {
        if let Some(cpu) = self.module_cpu_type {
            pdb::register::Register::new(reg, cpu).ok()
        } else {
            None
        }
    }

    /// Convert a pdb::Register (u32) to a binja register index for the current arch
    fn convert_register(&self, reg: pdb::Register) -> Option<RegisterId> {
        match self.lookup_register(reg) {
            Some(X86(xreg)) => {
                self.log(|| format!("Register {:?} ==> {:?}", reg, xreg));
                self.arch
                    .register_by_name(&xreg.to_string().to_lowercase())
                    .map(|reg| reg.id())
            }
            Some(AMD64(areg)) => {
                self.log(|| format!("Register {:?} ==> {:?}", reg, areg));
                self.arch
                    .register_by_name(&areg.to_string().to_lowercase())
                    .map(|reg| reg.id())
            }
            // TODO: Other arches
            _ => None,
        }
    }
}

/// Parse a vtable symbol name and return the parts for the vtable type.
/// Takes the last element of (e.g., `"ClassName::`vftable'{for `BaseClass'}"`)
/// and converts it to a vtable type name (e.g., `["ClassName", "BaseClass", "VTable"]`).
fn parse_vtable_type_name(name: &QualifiedName) -> Option<String> {
    let (last, qualified_name_base) = name.items.split_last()?;
    let (class_name, rest) = last.split_once("::`vftable'")?;

    let clean = |s: &str| {
        s.replace("class ", "")
            .replace("struct ", "")
            .replace("enum ", "")
    };

    let mut parts = qualified_name_base.to_vec();
    parts.extend(class_name.split("::").map(|s| clean(s)));

    if let Some(base_class) = rest
        .split_once("{for `")
        .and_then(|(_, base_start)| base_start.split_once("'}"))
        .map(|(base, _)| base)
    {
        parts = vec![clean(base_class)];
    }

    // We want only the last class name referenced in "parts", this is because the type we construct
    // for the vtable will want to use the existing base vt defined in the binary view.
    Some(format!("{}::VTable", parts.join("::")))
}

#[cfg(test)]
mod tests {
    use super::*;

    /// (input_demangled_name, expected_output)
    #[rustfmt::skip]
    const VTABLE_TEST_CASES: &[(&str, &[&str])] = &[
        // Simple vtables
        ("Base::`vftable'", &["Base", "VTable"]),
        // Multiple inheritance with {for}
        ("MultiDerived::`vftable'{for `InterfaceA'}", &["InterfaceA", "VTable"]),
        // Simple templates
        ("Container<int32_t>::`vftable'", &["Container<int32_t>", "VTable"]),
        ("Pair<int32_t,char>::`vftable'", &["Pair<int32_t,char>", "VTable"]),
        ("FixedArray<int32_t,10>::`vftable'", &["FixedArray<int32_t,10>", "VTable"]),
        // Namespaced classes
        ("outer::inner::NamespacedTemplate<int32_t>::`vftable'", &["outer", "inner", "NamespacedTemplate<int32_t>", "VTable"]),
        // Nested class inside template
        ("complex::HasNested<int32_t>::Nested::`vftable'", &["complex", "HasNested<int32_t>", "Nested", "VTable"]),
        // Template with {for} clause
        ("TemplateMultiDerived<int32_t>::`vftable'{for `TemplateInterfaceA<int32_t>'}", &["TemplateInterfaceA<int32_t>", "VTable"]),
        ("TemplateDiamond<int32_t>::`vftable'{for `TemplateLeftVirtual<int32_t>'}", &["TemplateLeftVirtual<int32_t>", "VTable"]),
        // Nested templates - note: "class " prefix gets stripped
        ("Wrapper<class Container<int32_t> >::`vftable'", &["Wrapper<Container<int32_t> >", "VTable"]),
        // Class/struct/enum keyword removal
        ("Wrapper<class Foo>::`vftable'", &["Wrapper<Foo>", "VTable"]),
        ("Container<struct Bar>::`vftable'", &["Container<Bar>", "VTable"]),
        ("Holder<enum Baz>::`vftable'", &["Holder<Baz>", "VTable"]),
    ];

    #[test]
    fn test_vtable_type_name_parsing() {
        for (input, expected) in VTABLE_TEST_CASES {
            let name = QualifiedName::new(vec![input.to_string()]);
            let result = parse_vtable_type_name(&name);
            assert_eq!(
                result,
                Some(expected.join("::")),
                "Failed for input: {}",
                input
            );
        }
    }
}