File: ExternalGenericMetadataBuilder.cpp

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

#include "ExternalGenericMetadataBuilder.h"
#include "swift/ABI/MetadataValues.h"
#include "swift/Demangling/Demangle.h"
#include "swift/RemoteInspection/Records.h"
#include "swift/Runtime/GenericMetadataBuilder.h"
#include "swift/Runtime/LibPrespecialized.h"
#include "swift/Runtime/PrebuiltStringMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/MachOUniversal.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/JSON.h"

#include <cstdint>
#include <initializer_list>
#include <stdarg.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>

#include "swift/Demangling/Demangler.h"

namespace swift {

// Logging macro. Currently we always enable logs, but we'll want to make this
// conditional eventually. LOG_ENABLED can be used to gate work that's needed
// for log statements but not for anything else.
enum class LogLevel {
  None = 0,
  Warning = 1,
  Info = 2,
  Detail = 3,
};

#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#define LOG(level, fmt, ...)                                                   \
  do {                                                                         \
    if (level <= getLogLevel())                                                \
      fprintf(stderr, "%s:%d:%s: " fmt "\n", METADATA_BUILDER_LOG_FILE_NAME,   \
              __LINE__, __func__, __VA_ARGS__);                                \
  } while (0)

static const char *nodeKindString(swift::Demangle::Node::Kind k) {
  switch (k) {
#define NODE(ID)                                                               \
  case Node::Kind::ID:                                                         \
    return #ID;
#include "swift/Demangling/DemangleNodes.def"
  }
  return "Demangle::Node::Kind::???";
}

// An external runtime target that preserves the static type of pointees.
template <typename Runtime>
struct TypedExternal {
  //  using StoredPointer = typename Runtime::StoredPointer;
  struct StoredPointer {
    typename Runtime::StoredPointer value;

    StoredPointer(MetadataKind kind)
        : value(static_cast<typename Runtime::StoredPointer>(kind)) {}

    static bool isNull(StoredPointer ptr) { return ptr.value == 0; }
  };
  using StoredSignedPointer = typename Runtime::StoredSignedPointer;
  using StoredSize = typename Runtime::StoredSize;
  using StoredPointerDifference = typename Runtime::StoredPointerDifference;

  static constexpr size_t PointerSize = Runtime::PointerSize;
  static constexpr bool ObjCInterop = Runtime::ObjCInterop;
  const StoredPointer PointerValue;

  template <typename T, typename Integer, bool Nullable = true,
            typename TagType = void>
  struct TypedInteger {
    Integer value;
  };

  template <typename T, typename Integer, bool Nullable = true>
  struct TypedSignedInteger {
    Integer SignedValue;
  };

  template <typename T>
  using Pointer = TypedInteger<T, StoredPointer>;

  template <typename T>
  using SignedPointer = TypedSignedInteger<T, StoredPointer>;

  template <typename T, bool Nullable = false>
  using FarRelativeDirectPointer = TypedInteger<T, StoredPointer, Nullable>;

  struct RelativeIndirectablePointerTag {};

  template <typename T, bool Nullable = false>
  using RelativeIndirectablePointer =
      TypedInteger<T, int32_t, Nullable, RelativeIndirectablePointerTag>;

  template <typename T, bool Nullable = true>
  using RelativeDirectPointer = TypedInteger<T, int32_t, Nullable>;

  template <typename T, bool Nullable = true, typename Offset = int32_t>
  using CompactFunctionPointer = TypedInteger<T, int32_t, Nullable>;

  template <unsigned discriminator>
  struct ValueWitnessFunctionPointer {
    StoredPointer pointer;
  };

  StoredPointer
  getStrippedSignedPointer(const StoredSignedPointer pointer) const {
    return swift_ptrauth_strip(pointer);
  }
};

using ExternalRuntime32 =
    swift::TypedExternal<swift::WithObjCInterop<swift::RuntimeTarget<4>>>;
using ExternalRuntime64 =
    swift::TypedExternal<swift::WithObjCInterop<swift::RuntimeTarget<8>>>;

// Declare a specialized version of the value witness types that use a wrapper
// on the functions that captures the ptrauth discriminator.
template <>
class TargetValueWitnessTypes<ExternalRuntime64> {
public:
  using StoredPointer = typename ExternalRuntime64::StoredPointer;

#define WANT_ALL_VALUE_WITNESSES
#define DATA_VALUE_WITNESS(lowerId, upperId, type)
#define FUNCTION_VALUE_WITNESS(lowerId, upperId, returnType, paramTypes)       \
  typedef ExternalRuntime64::ValueWitnessFunctionPointer<                      \
      SpecialPointerAuthDiscriminators::upperId>                               \
      lowerId;
#define MUTABLE_VALUE_TYPE TargetPointer<ExternalRuntime64, OpaqueValue>
#define IMMUTABLE_VALUE_TYPE ConstTargetPointer<ExternalRuntime64, OpaqueValue>
#define MUTABLE_BUFFER_TYPE TargetPointer<ExternalRuntime64, ValueBuffer>
#define IMMUTABLE_BUFFER_TYPE ConstTargetPointer<ExternalRuntime64, ValueBuffer>
#define TYPE_TYPE ConstTargetPointer<ExternalRuntime64, Metadata>
#define SIZE_TYPE StoredSize
#define INT_TYPE int
#define UINT_TYPE unsigned
#define VOID_TYPE void
#include "swift/ABI/ValueWitness.def"

  // Handle the data witnesses explicitly so we can use more specific
  // types for the flags enums.
  typedef size_t size;
  typedef size_t stride;
  typedef TargetValueWitnessFlags<typename ExternalRuntime64::StoredSize> flags;
  typedef uint32_t extraInhabitantCount;
};

enum class PtrauthKey : int8_t {
  None = -1,
  IA = 0,
  IB = 1,
  DA = 2,
  DB = 3,
};

template <typename T>
inline MetadataKind getEnumeratedMetadataKind(T kind,
                                              decltype(kind.value) dummy = 0) {
  return getEnumeratedMetadataKind(kind.value);
}

template <typename... Args>
NodePointer wrapNode(Demangler &demangler, NodePointer node, Args... kinds) {
  std::initializer_list<Node::Kind> kindsList{kinds...};

  for (auto iter = std::rbegin(kindsList); iter != std::rend(kindsList);
       iter++) {
    auto wrapper = demangler.createNode(*iter);
    wrapper->addChild(node, demangler);
    node = wrapper;
  }
  return node;
}

std::string getErrorString(llvm::Error error) {
  std::string errorString;
  handleAllErrors(std::move(error), [&](const llvm::ErrorInfoBase &E) {
    errorString = E.message();
  });
  return errorString;
}

struct FixupTarget {
  // This value in `library` means the target is the containing library.
  static constexpr int selfLibrary = 0;

  int library;

  // Virtual address or symbol name.
  std::variant<uint64_t, llvm::StringRef> target;
};

// An export from a library. Contains a name and address.
struct Export {
  std::string name;
  uint64_t address;
};

// A segment in a library.
struct Segment {
  std::string name;
  uint64_t address;
  uint64_t size;

  Segment(const char name[16], uint64_t address, uint64_t size)
      : address(address), size(size) {
    size_t length = strnlen(name, 16);
    llvm::StringRef nameRef{name, length};
    this->name = nameRef.str();
  }
};

struct MachOFile {
  std::optional<std::unique_ptr<llvm::MemoryBuffer>> memoryBuffer;

  std::unique_ptr<llvm::object::MachOObjectFile> objectFile;
  std::string path;

  uint64_t baseAddress;

  // With the leading '_'.
  llvm::StringMap<Export> symbols;

  std::vector<Segment> segments;
  std::vector<Export> exportsSortedByAddress;
  std::vector<const char *> libraryNames;

  std::unordered_map<uint64_t, FixupTarget> fixups;

  MachOFile(std::optional<std::unique_ptr<llvm::MemoryBuffer>> &&memoryBuffer,
            std::unique_ptr<llvm::object::MachOObjectFile> &&objectFile,
            const std::string &path)
      : memoryBuffer(std::move(memoryBuffer)),
        objectFile(std::move(objectFile)), path(path) {}
};

template <typename Runtime>
class ReaderWriter;

template <typename Runtime>
class ExternalGenericMetadataBuilderContext {
  // Structs that provide the ptrauth info for pointers to specific tyeps.
  template <typename Target>
  struct PtrauthInfo;

  template <>
  struct PtrauthInfo<const TargetValueTypeDescriptor<Runtime>> {
    static constexpr PtrauthKey key = PtrauthKey::DA;
    static constexpr bool addressDiversified = true;
    static constexpr unsigned discriminator =
        SpecialPointerAuthDiscriminators::TypeDescriptor;
  };

  template <>
  struct PtrauthInfo<const TargetValueWitnessTable<Runtime>> {
    static constexpr PtrauthKey key = PtrauthKey::DA;
    static constexpr bool addressDiversified = true;
    static constexpr unsigned discriminator =
        SpecialPointerAuthDiscriminators::ValueWitnessTable;
  };

  struct Atom;

  struct FileTarget {
    MachOFile *file;
    const Export *nearestExport; // Pointer to entry in exportsSortedByAddress.
    uint64_t addressInFile;
  };

  struct AtomTarget {
    Atom *atom;
    uint64_t offset;
  };

  struct PointerTarget {
    template <typename T>
    class Buffer;

    unsigned offset;
    unsigned size;

    PtrauthKey ptrauthKey;
    bool addressDiversified;
    unsigned discriminator;

    std::variant<FileTarget, AtomTarget> fileOrAtom;
  };

  struct Atom {
    std::string name;
    std::vector<char> buffer;
    std::vector<PointerTarget> pointerTargetsSorted;
  };

  std::vector<std::unique_ptr<Atom>> atoms;

  std::unordered_set<std::string> atomNames;

  Atom *allocateAtom(size_t size) {
    auto atom = atoms.emplace_back(new Atom).get();
    atom->buffer.resize(size);
    return atom;
  }

public:
  using ContextDescriptor = TargetContextDescriptor<Runtime>;
  using ExtensionContextDescriptor = TargetExtensionContextDescriptor<Runtime>;
  using ModuleContextDescriptor = TargetModuleContextDescriptor<Runtime>;
  using ProtocolDescriptor = TargetProtocolDescriptor<Runtime>;
  using TypeContextDescriptor = TargetTypeContextDescriptor<Runtime>;
  using Metadata = TargetMetadata<Runtime>;
  using StoredPointer = typename Runtime::StoredPointer;

  template <typename T>
  using Pointer = typename Runtime::template Pointer<T>;
  template <typename T>
  using SignedPointer = typename Runtime::template SignedPointer<T>;

  template <typename T>
  class Buffer {
    template <typename U>
    friend class Buffer;

    // Copy constructor, but done as a static function to make errors less
    // confusing when outside code accidentally tries to initialize a Buffer<T>
    // with a Buffer<U>.
    template <typename U>
    static Buffer<T> createFrom(const Buffer<U> &other) {
      Buffer<T> result{other.context};
      result.section = other.section;
      result.ptr = (T *)(void *)other.ptr;
      result.file = other.file;
      result.atom = other.atom;
      return result;
    }

  protected:
    template <typename U>
    BuilderErrorOr<Buffer<const U>>
    resolveVirtualAddressInFile(MachOFile *targetFile, uint64_t vmAddr) {
      auto section = context->findSectionInFile(targetFile, vmAddr);
      if (!section)
        return BuilderError("No section contained resolved address %#" PRIx64,
                            vmAddr);

      intptr_t targetOffsetInSection = vmAddr - section->getAddress();
      auto targetSectionContents = section->getContents();
      if (!targetSectionContents) {
        return BuilderError(
            "Failed to get target section contents: %s",
            getErrorString(targetSectionContents.takeError()).c_str());
      }

      intptr_t target =
          (intptr_t)targetSectionContents->data() + targetOffsetInSection;

      return Buffer<const U>{context, targetFile, *section, (U *)target};
    }

    template <typename U>
    BuilderErrorOr<Buffer<const U>> resolveVirtualAddress(uint64_t vmAddr) {
      return resolveVirtualAddressInFile<U>(file, vmAddr);
    }

    template <typename U, typename Integer, bool Nullable>
    BuilderErrorOr<Buffer<const U>> resolveRelativePointer(const void *ptr,
                                                           Integer value) {
      if (Nullable && value == 0)
        return {{Buffer<const U>::null(context)}};

      auto pointerVirtualAddress = getVirtualAddress(ptr);
      if (!pointerVirtualAddress)
        return *pointerVirtualAddress.getError();
      uintptr_t targetVirtualAddress = *pointerVirtualAddress + value;
      return resolveVirtualAddress<U>(targetVirtualAddress);
    }

    BuilderErrorOr<const char *> getLibraryName(int ordinal) {
      if (ordinal > 0 && (size_t)ordinal <= file->libraryNames.size())
        return file->libraryNames[ordinal - 1];
      return BuilderError("ordinal %d is out of bounds", ordinal);
    }

  public:
    ExternalGenericMetadataBuilderContext *context;
    MachOFile *file = nullptr;
    llvm::object::SectionRef section;

    T *ptr;
    Atom *atom = nullptr;

    static Buffer null(ExternalGenericMetadataBuilderContext *context) {
      return Buffer{context};
    }

    Buffer(ExternalGenericMetadataBuilderContext *context)
        : context(context), ptr(nullptr) {}

    explicit Buffer(ExternalGenericMetadataBuilderContext *context,
                    MachOFile *file, llvm::object::SectionRef section, T *ptr)
        : context(context), file(file), section(section), ptr(ptr) {}

    explicit Buffer(ExternalGenericMetadataBuilderContext *context,
                    MachOFile *file, llvm::object::SectionRef section,
                    llvm::StringRef str)
        : context(context), file(file), section(section),
          ptr(reinterpret_cast<T *>(str.data())) {}

    operator bool() { return ptr != nullptr; }

    bool isNull() { return ptr == nullptr; }

    Buffer<const T> toConst() { return Buffer<const T>::createFrom(*this); }

    template <typename U>
    Buffer<U> cast() {
      return Buffer<U>::createFrom(*this);
    }

    Buffer<T> offsetBy(ptrdiff_t offset) const {
      auto newBuffer = *this;
      newBuffer.ptr = (T *)((uintptr_t)ptr + offset);
      return newBuffer;
    }

    BuilderErrorOr<uint64_t> getVirtualAddress(const void *ptr) {
      auto sectionContents = section.getContents();
      if (!sectionContents) {
        return BuilderError(
            "Failed to get section contents: %s",
            getErrorString(sectionContents.takeError()).c_str());
      }
      intptr_t sectionStartInMemory = (intptr_t)sectionContents->data();
      intptr_t pointerAddress = (intptr_t)ptr;
      uint64_t pointerVirtualAddress =
          section.getAddress() + pointerAddress - sectionStartInMemory;
      return pointerVirtualAddress;
    }

    // Get the virtual address of ptr, or ~0 if there's an error. Meant for
    // logging purposes.
    uint64_t getAddress() {
      if (auto *address = getVirtualAddress(ptr).getValue())
        return *address;
      return ~0;
    }

    template <typename U, bool Nullable>
    BuilderErrorOr<Buffer<const U>> resolvePointer(
        const TargetRelativeDirectPointer<Runtime, U, Nullable> *ptr) {
      return resolveRelativePointer<U, decltype(ptr->value), Nullable>(
          ptr, ptr->value);
    }

    template <typename U, bool Nullable>
    BuilderErrorOr<Buffer<const U>> resolvePointer(
        const TargetRelativeIndirectablePointer<Runtime, U, Nullable> *ptr) {
      bool isDirect = !(ptr->value & 1);
      auto pointerValue = ptr->value & ~static_cast<decltype(ptr->value)>(1);

      auto directBuffer =
          resolveRelativePointer<U, decltype(ptr->value), Nullable>(
              ptr, pointerValue);
      if (directBuffer.getError() || isDirect)
        return directBuffer;

      // Indirect case: relative pointer to absolute pointer to value.
      auto indirectAbsolutePointer =
          directBuffer.getValue()->template cast<const StoredPointer>();
      return indirectAbsolutePointer.template resolvePointer<U>(
          indirectAbsolutePointer.ptr);
    }

    template <typename U, bool Nullable, typename IntTy, typename Offset>
    BuilderErrorOr<Buffer<const U>> resolvePointer(
        const RelativeDirectPointerIntPair<U, IntTy, Nullable, Offset> *ptr) {
      return resolveRelativePointer<U, Offset, Nullable>(ptr, ptr->getOffset());
    }

    template <typename U = char>
    BuilderErrorOr<Buffer<const U>> resolvePointer(const StoredPointer *ptr) {
      if (isNull())
        return BuilderError("Tried to resolve pointer %p in null buffer", ptr);

      if (!file) {
        return reinterpret_cast<WritableData<T> *>(this)
            ->template resolveWritableDataPointer<U>(ptr);
      }
      assert(section.getObject());

      auto pointerVirtualAddress = getVirtualAddress(ptr);
      if (!pointerVirtualAddress)
        return *pointerVirtualAddress.getError();

      auto found = file->fixups.find(*pointerVirtualAddress);
      if (found != file->fixups.end()) {
        auto target = std::get<1>(*found);
        if (auto *vmAddr = std::get_if<uint64_t>(&target.target)) {
          if (target.library > 0) {
            const char *libName = "ordinal too large";
            if (auto ptr = getLibraryName(target.library).getValue())
              libName = *ptr;
            BuilderError("Found fixup at %#" PRIx64
                         " with address target %#" PRIx64
                         " and library target %s",
                         *pointerVirtualAddress, *vmAddr, libName);
          }

          return resolveVirtualAddress<U>(*vmAddr);
        }

        auto symbolName = std::get<llvm::StringRef>(target.target);

        MachOFile *targetFile;
        if (target.library > 0) {
          auto targetLibraryName = getLibraryName(target.library);
          if (!targetLibraryName)
            return *targetLibraryName.getError();
          auto found = context->machOFilesByPath.find(*targetLibraryName);
          if (found == context->machOFilesByPath.end())
            return BuilderError("Symbol referenced unknown library '%s'",
                                *targetLibraryName);
          targetFile = found->getValue();
        } else if (target.library == FixupTarget::selfLibrary) {
          targetFile = file;
        } else {
          return BuilderError(
              "Can't resolve pointer with fixup target ordinal %d",
              target.library);
        }

        auto foundSymbol = targetFile->symbols.find(symbolName);
        if (foundSymbol == targetFile->symbols.end()) {
          const char *targetLibraryName;
          if (auto *ptr = getLibraryName(target.library).getValue())
            targetLibraryName = *ptr;
          else
            targetLibraryName = file->path.c_str();
          return BuilderError(
              "Symbol referenced unknown symbol '%s' in library '%s'",
              symbolName.str().c_str(), targetLibraryName);
        }
        return context->readerWriter->template getSymbolPointerInFile<U>(
            symbolName, foundSymbol->getValue(), targetFile);
      }

      // We didn't find any fixups for this spot. It might just be NULL.
      if (ptr->value == 0)
        return {Buffer<const U>::null(context)};

      return BuilderError("Attempted to resolve pointer at %#" PRIx64
                          " with contents %#" PRIx64
                          " but no fixup exists at this location",
                          *pointerVirtualAddress, (uint64_t)ptr->value);
    }

    template <typename U = char>
    BuilderErrorOr<Buffer<const U>>
    resolvePointer(const SignedPointer<U *> *ptr) {
      return resolvePointer<U>(reinterpret_cast<const StoredPointer *>(ptr));
    }

    template <typename U = char>
    BuilderErrorOr<Buffer<const U>> resolvePointer(const Pointer<U> *ptr) {
      return resolvePointer<U>(reinterpret_cast<const StoredPointer *>(ptr));
    }

    template <typename U>
    BuilderErrorOr<Buffer<const char>> resolveFunctionPointer(const U *ptr) {
      static_assert(sizeof(*ptr) == sizeof(StoredPointer));
      return resolvePointer(reinterpret_cast<const StoredPointer *>(ptr));
    }

    template <typename U, bool nullable>
    BuilderErrorOr<Buffer<const char>> resolveFunctionPointer(
        const TargetCompactFunctionPointer<Runtime, U, nullable> *ptr) {
      auto result = resolvePointer(ptr);
      if (auto *error = result.getError())
        return *error;
      return result.getValue()->template cast<const char>();
    }
  };

  template <typename T>
  class WritableData : public Buffer<T> {
    /// Check that the given pointer lies within memory of this data object.
    void checkPtr(void *toCheck) {
      assert((uintptr_t)toCheck - (uintptr_t)this->atom->buffer.data() <
             this->atom->buffer.size());
    }

    template <typename U>
    BuilderErrorOr<std::monostate> writePointerImpl(
        void *where, Buffer<U> value, PtrauthKey ptrauthKey = PtrauthKey::None,
        bool addressDiversified = true, unsigned discriminator = 0) {
      if (!value) {
        memset(where, 0, sizeof(StoredPointer));
        return {{}};
      }

      // Write some arbitrary nonzero data so that null checks work with != 0.
      // This value won't make it out into the serialized data.
      memset(where, 0x5a, sizeof(StoredPointer));

      PointerTarget target = {};

      target.offset = (uintptr_t)where - (uintptr_t)this->atom->buffer.data();
      assert(target.offset + sizeof(StoredPointer) <=
             this->atom->buffer.size());

      target.size = sizeof(StoredPointer);

      target.ptrauthKey = ptrauthKey;
      target.addressDiversified = addressDiversified;
      target.discriminator = discriminator;

      if (auto *file = value.file) {
        auto contents = value.section.getContents();
        if (!contents) {
          fprintf(stderr,
                  "section.getContents should never fail, but it did: %s",
                  getErrorString(contents.takeError()).c_str());
          abort();
        }

        auto addressInFile = (uintptr_t)value.ptr -
                             (uintptr_t)contents->data() +
                             value.section.getAddress();
        auto foundExport =
            this->context->readerWriter->getNearestExportedSymbol(
                file, addressInFile);
        if (!foundExport)
          return BuilderError(
              "Failed to write pointer to %s %#" PRIx64
              " as there are no exported symbols in that segment.",
              file->path.c_str(), addressInFile);

        target.fileOrAtom = FileTarget{value.file, foundExport, addressInFile};
      } else {
        auto offsetInValue =
            (uintptr_t)value.ptr - (uintptr_t)value.atom->buffer.data();
        target.fileOrAtom = AtomTarget{value.atom, offsetInValue};
      }

      auto &targets = this->atom->pointerTargetsSorted;
      auto compare = [](const PointerTarget &a, const PointerTarget &b) {
        return a.offset < b.offset;
      };
      auto insertionPoint =
          std::upper_bound(targets.begin(), targets.end(), target, compare);

      // Check to see if there's already a target at this offset, and overwrite
      // it if so.
      if (insertionPoint > targets.begin()) {
        auto prevPoint = insertionPoint - 1;
        if (prevPoint->offset == target.offset) {
          *prevPoint = target;
          return {{}};
        }
      }
      targets.insert(insertionPoint, target);

      return {{}};
    }

  public:
    WritableData(ExternalGenericMetadataBuilderContext *context, Atom *atom)
        : Buffer<T>(context, {}, {},
                    reinterpret_cast<T *>(atom->buffer.data())) {
      this->atom = atom;
    }

    void setName(const std::string &str) { this->atom->name = str; }

    template <typename U = char>
    BuilderErrorOr<Buffer<const U>>
    resolveWritableDataPointer(const StoredPointer *ptr) {
      unsigned offset = (uintptr_t)ptr - (uintptr_t)this->atom->buffer.data();
      assert(offset + sizeof(StoredPointer) <= this->atom->buffer.size());

      auto &targets = this->atom->pointerTargetsSorted;
      auto compare = [](const PointerTarget &a, const unsigned &b) {
        return a.offset < b;
      };
      auto found =
          std::lower_bound(targets.begin(), targets.end(), offset, compare);
      if (found == targets.end() || found->offset != offset) {
        if (ptr->value == 0)
          return Buffer<const U>::null(this->context);
        return BuilderError(
            "Asked to resolve non-null pointer %#" PRIx64
            " at offset %u in writable data with no target at that offset",
            (uint64_t)ptr->value, offset);
      }

      if (auto *fileTarget = std::get_if<FileTarget>(&found->fileOrAtom))
        return this->template resolveVirtualAddressInFile<U>(
            fileTarget->file, fileTarget->addressInFile);

      auto atomTarget = std::get<AtomTarget>(found->fileOrAtom);
      WritableData<const U> buffer{this->context, atomTarget.atom};
      return buffer.offsetBy(atomTarget.offset);
    }

    template <typename U>
    BuilderErrorOr<std::monostate> writePointer(Pointer<U> *where,
                                                Buffer<U> value) {
      checkPtr(where);
      where->value.value = ~(uintptr_t)value.ptr;

      return writePointerImpl(where, value);
    }

    // SignedPointer is templated on the pointer type, not the pointee type like
    // the other pointer templates.
    template <typename U>
    BuilderErrorOr<std::monostate> writePointer(SignedPointer<U *> *where,
                                                Buffer<U> value) {
      checkPtr(where);
      where->SignedValue.value = ~(uintptr_t)value.ptr;

      return writePointerImpl(where, value, PtrauthInfo<U>::key,
                              PtrauthInfo<U>::addressDiversified,
                              PtrauthInfo<U>::discriminator);
    }

    template <typename U>
    BuilderErrorOr<std::monostate> writePointer(StoredPointer *where,
                                                Buffer<U> value) {
      checkPtr(where);
      where->value = ~(uintptr_t)value.ptr;

      return writePointerImpl(where, value);
    }

    BuilderErrorOr<std::monostate>
    writeFunctionPointer(void *where, Buffer<const char> target) {
      return writePointer(reinterpret_cast<StoredPointer *>(where), target);
    }

    template <unsigned discriminator>
    BuilderErrorOr<std::monostate> writeFunctionPointer(
        ExternalRuntime64::ValueWitnessFunctionPointer<discriminator> *where,
        Buffer<const char> target) {
      checkPtr(where);

      return writePointerImpl(where, target, PtrauthKey::IA, true,
                              discriminator);
    }
  };

  ExternalGenericMetadataBuilderContext() {
    readerWriter.reset(new ReaderWriter<Runtime>{this});
  }

  ExternalGenericMetadataBuilderContext(
      const ExternalGenericMetadataBuilderContext &) = delete;
  ExternalGenericMetadataBuilderContext &
  operator=(const ExternalGenericMetadataBuilderContext &) = delete;

  LogLevel getLogLevel() {
    return logLevel;
  }

  void setLogLevel(int level) {
    logLevel = LogLevel(level);
  }

  template <typename T>
  WritableData<T> allocate(size_t size) {
    auto atom = allocateAtom(size);
    return WritableData<T>{this, atom};
  }

  template <typename T>
  WritableData<T> allocateArray(size_t count) {
    return allocate<T>(count * sizeof(T));
  }

  void setArch(const char *arch) {
    this->arch = arch;
    this->usePtrauth = this->arch == "arm64e";
  }

  void setNamesToBuild(const std::vector<std::string> &names) {
    this->mangledNamesToBuild = names;
  }

  BuilderErrorOr<NodePointer>
  buildDemanglingForContext(Buffer<const ContextDescriptor> descriptorBuffer,
                            Demangler &dem);

  llvm::Error
  addImage(std::unique_ptr<llvm::object::Binary> &&binary,
           std::optional<std::unique_ptr<llvm::MemoryBuffer>> &&memoryBuffer,
           const std::string &path);
  llvm::Error addImageAtPath(const std::string &path);
  llvm::Error addImageInMemory(const void *start, uint64_t length,
                               const std::string &path);
  BuilderErrorOr<std::monostate> addImagesInPath(std::string path);

  BuilderErrorOr<Buffer<const Metadata>>
  metadataForNode(swift::Demangle::NodePointer Node);

  std::optional<std::pair<MachOFile *, Export>>
  findSymbol(llvm::StringRef name, MachOFile *searchFile);
  std::optional<llvm::object::SectionRef> findSectionInFile(MachOFile *file,
                                                            uint64_t address);
  std::optional<Segment> findSegmentInFile(MachOFile *file, uint64_t address);
  std::optional<MachOFile *> findPointerInFiles(const void *ptr);

  void build();
  void writeOutput(llvm::json::OStream &J, unsigned platform,
                   const std::string &platformVersion);
  void logDescriptorMap();

private:
  using Builder = GenericMetadataBuilder<ReaderWriter<Runtime>>;
  using ConstructedMetadata = typename Builder::ConstructedMetadata;

  void cacheDescriptor(Buffer<const ContextDescriptor> descriptorBuffer);

  void addImage(MachOFile *file);
  void populateMachOSymbols(MachOFile *file);
  void populateMachOFixups(MachOFile *file);

  void readMachOSections(MachOFile *file);
  BuilderErrorOr<std::string> _mangledNominalTypeNameForBoundGenericNode(Demangle::NodePointer BoundGenericNode);
  BuilderErrorOr<std::monostate>
  ensureMetadataForMangledTypeName(llvm::StringRef typeName);
  BuilderErrorOr<std::optional<typename Builder::ConstructedMetadata>>
  constructMetadataForNode(swift::Demangle::NodePointer Node);

  Buffer<char> serializeMetadataMapTable(void);

  template <typename SymbolCallback>
  void writeAtomContentsJSON(llvm::json::OStream &J, const Atom &atom,
                             const SymbolCallback &symbolCallback);
  void writeDylibsJSON(
      llvm::json::OStream &J,
      std::unordered_map<MachOFile *, std::unordered_set<std::string>>
          &symbolReferences);
  void writeJSONSerialization(llvm::json::OStream &J, unsigned platform,
                              const std::string &platformVersion);

  // The current log level.
  LogLevel logLevel = LogLevel::None;

  // The architecture being targeted.
  std::string arch;

  // Does this target use pointer authentication?
  bool usePtrauth = false;

  // The readerWriter and builder helper objects.
  std::unique_ptr<ReaderWriter<Runtime>> readerWriter;
  std::unique_ptr<Builder> builder;

  // The mangled names we're building metadata for.
  std::vector<std::string> mangledNamesToBuild;

  // Map from standardized mangled names to the corresponding type descriptors.
  std::unordered_map<std::string, Buffer<const TypeContextDescriptor>>
      mangledNominalTypeDescriptorMap;

  // Map from mangled type names to the built metadata, or error that occurred
  // when building it.
  std::unordered_map<
      std::string, BuilderErrorOr<const typename Builder::ConstructedMetadata>>
      builtMetadataMap;

  // All of the MachOFile objects we're working with.
  std::vector<std::unique_ptr<MachOFile>> machOFiles;

  // A map from paths (install names) to MachOFile objects.
  llvm::StringMap<MachOFile *> machOFilesByPath;

  // A map from symbol names (including _ prefix) to file and export.
  llvm::StringMap<std::pair<MachOFile *, Export>> allSymbols;

  swift::Demangle::Context demangleCtx;
};

template <typename RuntimeT>
class ReaderWriter {
  ExternalGenericMetadataBuilderContext<RuntimeT> *context;

  bool segmentContainsAddress(Segment segment, uint64_t addr) {
    return segment.address <= addr && addr < segment.address + segment.size;
  }

  const Export *getNearestSymbol(MachOFile *file, uint64_t addr,
                                 const std::vector<Export> &in) {
    auto comparator = [&](uint64_t addr, const Export &ex) -> bool {
      return addr < ex.address;
    };
    auto foundSymbol = std::upper_bound(in.begin(), in.end(), addr, comparator);

    // We found the first symbol with address greater than our target. Back
    // up one to find the symbol with address less than or equal to our
    // target.
    if (foundSymbol != in.begin())
      foundSymbol--;

    if (foundSymbol == in.end())
      return {};

    // Make sure the symbol is in the same segment as the target.
    auto targetSegment = context->findSegmentInFile(file, addr);

    // If the target isn't even in a segment, give up. This should never happen.
    if (!targetSegment)
      return {};

    if (!segmentContainsAddress(*targetSegment, foundSymbol->address)) {
      // Try the following symbol and use it if it's in the same segment as the
      // target.
      foundSymbol++;
      if (foundSymbol == in.end())
        return {};
      if (!segmentContainsAddress(*targetSegment, foundSymbol->address))
        return {};
    }

    return &*foundSymbol;
  }

public:
  using Runtime = RuntimeT;

  using MetadataContext = ExternalGenericMetadataBuilderContext<Runtime>;

  using Metadata = typename MetadataContext::Metadata;
  using GenericArgument =
      typename MetadataContext::template Buffer<const Metadata>;

  template <typename T>
  using Buffer = typename MetadataContext::template Buffer<T>;

  template <typename T>
  using WritableData = typename MetadataContext::template WritableData<T>;

  struct SymbolInfo {
    std::string symbolName;
    std::string libraryName;
    uint64_t pointerOffset;
  };

  class ResolveToDemangling {
    ReaderWriter &readerWriter;
    Demangle::Demangler &dem;

    LogLevel getLogLevel() {
      return readerWriter.getLogLevel();
    }

  public:
    ResolveToDemangling(ReaderWriter &readerWriter, Demangle::Demangler &dem)
        : readerWriter(readerWriter), dem(dem) {}

    Demangle::NodePointer operator()(Demangle::SymbolicReferenceKind kind,
                                     Demangle::Directness directness,
                                     int32_t offset, const void *base) {
      auto maybeBuffer = readerWriter.getBufferForPointerInFile<
          TargetRelativeDirectPointer<Runtime, char>>(base);
      if (auto *error = maybeBuffer.getError()) {
        LOG(LogLevel::Warning,
            "Failed to find buffer for symbolic reference at %p offset "
            "%" PRId32,
            base, offset);
        return nullptr;
      }

      auto buffer = maybeBuffer.getValue();
      auto target = buffer->resolvePointer(buffer->ptr);
      if (auto *error = target.getError()) {
        LOG(LogLevel::Warning,
            "Failed to resolve symbolic reference at %p offset %" PRId32, base,
            offset);
        return nullptr;
      }

      if (directness == Directness::Indirect) {
        auto castBuffer =
            target.getValue()
                ->template cast<typename MetadataContext::StoredPointer>();
        target = castBuffer.resolvePointer(castBuffer.ptr);
        if (auto *error = target.getError()) {
          LOG(LogLevel::Warning,
              "Failed to resolve indirect symbolic reference at %p offset "
              "%" PRId32,
              base, offset);
          return nullptr;
        }
      }

      switch (kind) {
      case Demangle::SymbolicReferenceKind::Context: {
        auto contextBuffer =
            target.getValue()
                ->template cast<const TargetContextDescriptor<Runtime>>();
        auto demangleNode =
            readerWriter.context->buildDemanglingForContext(contextBuffer, dem);
        if (auto *error = demangleNode.getError()) {
          LOG(LogLevel::Warning,
              "Failed to build demangling for symbolic reference at %p offset "
              "%" PRId32,
              base, offset);
          return nullptr;
        }
        return *demangleNode.getValue();
      }
      default:
        LOG(LogLevel::Warning,
            "Don't know how to handle symbolic reference kind %u",
            (unsigned)kind);
        return nullptr;
      }
    }
  };

  ReaderWriter(ExternalGenericMetadataBuilderContext<Runtime> *context)
      : context(context) {}

  bool isLoggingEnabled() { return getLogLevel() >= LogLevel::Info; }

  LogLevel getLogLevel() {
    return context->getLogLevel();
  }

  SWIFT_FORMAT(5, 6)
  void log(const char *filename, unsigned line, const char *function,
           const char *fmt, ...) {
    if (!isLoggingEnabled())
      return;

    va_list args;
    va_start(args, fmt);

    fprintf(stderr, "%s:%u:%s: ", filename, line, function);
    vfprintf(stderr, fmt, args);
    fputs("\n", stderr);

    va_end(args);
  }

  template <typename T = char>
  BuilderErrorOr<Buffer<const T>> getSymbolPointer(llvm::StringRef name) {
    auto result = context->findSymbol(name, nullptr);
    if (!result)
      return BuilderError("Could not find symbol '%s'", name.str().c_str());

    auto [file, symbol] = *result;
    return getSymbolPointerInFile<T>(name, symbol, file);
  }

  template <typename T = char>
  BuilderErrorOr<Buffer<const T>>
  getSymbolPointerInFile(llvm::StringRef name, Export symbol, MachOFile *file) {
    auto address = symbol.address;
    auto section = context->findSectionInFile(file, address);
    if (!section)
      return BuilderError("Could not get section of symbol '%s' (%#" PRIx64
                          "), it is undefined or absolute",
                          name.str().c_str(), address);
    auto sectionAddress = section->getAddress();

    if (address < sectionAddress)
      return BuilderError("Symbol '%s' has address %#" PRIx64
                          " < section address %#" PRIx64,
                          name.str().c_str(), address, sectionAddress);

    auto expectedSectionContents = section->getContents();
    if (!expectedSectionContents)
      return BuilderError(
          "Could not get section contents for symbol '%s': %s",
          name.str().c_str(),
          getErrorString(expectedSectionContents.takeError()).c_str());
    auto sectionContents = expectedSectionContents.get();

    auto delta = address - sectionAddress;
    if (delta >= sectionContents.size())
      return BuilderError(
          "Symbol '%s' has address %#" PRIx64
          " past the end of its section, start address %#" PRIx64 " length %zu",
          name.str().c_str(), address, sectionAddress, sectionContents.size());

    auto symbolContents = sectionContents.drop_front(delta);

    Buffer<const T> buffer{context, file, *section, symbolContents};
    return {buffer};
  }

  template <typename T>
  BuilderErrorOr<Buffer<const T>> getBufferForPointerInFile(const void *ptr) {
    auto maybeFile = context->findPointerInFiles(ptr);
    if (!maybeFile)
      return BuilderError("Pointer %p is not in any loaded mach-o file", ptr);
    auto file = *maybeFile;

    auto target = (uintptr_t)ptr;
    bool hadErrors = false;
    for (auto &section : file->objectFile->sections()) {
      auto contents = section.getContents();
      if (!contents) {
        consumeError(contents.takeError());
        hadErrors = true;
        continue;
      }

      auto start = (uintptr_t)contents->begin();
      auto end = (uintptr_t)contents->end();
      if (start <= target && target < end)
        return Buffer<const T>{context, file, section, (const T *)ptr};
    }

    return BuilderError(
        "Pointer %p is in file %s but is not in any section%s", ptr,
        file->path.c_str(),
        hadErrors ? " (errors occurred when fetching section contents)" : "");
  }

  BuilderErrorOr<NodePointer> substituteGenericParameters(
      NodePointer node, NodePointer metadataMangleNode,
      WritableData<FullMetadata<Metadata>> containingMetadataBuffer) {
    if (node->getKind() == Demangle::Node::Kind::DependentGenericParamType) {
      auto depth = node->getChild(0)->getIndex();
      auto index = node->getChild(1)->getIndex();

      unsigned foundDepth = 0;
      NodePointer cursor = metadataMangleNode;
      while (cursor) {
        switch (cursor->getKind()) {
        case Node::Kind::BoundGenericClass:
        case Node::Kind::BoundGenericStructure:
        case Node::Kind::BoundGenericEnum:
        case Node::Kind::BoundGenericProtocol:
        case Node::Kind::BoundGenericTypeAlias:
        case Node::Kind::BoundGenericFunction:
        case Node::Kind::BoundGenericOtherNominalType:
          if (foundDepth == depth) {
            auto typeList = cursor->getChild(1);
            if (!typeList)
              return BuilderError(
                  "Requested generic parameter at depth=%" PRIu64
                  " index=%" PRIu64
                  ", BoundGeneric node does not have two children",
                  depth, index);
            if (typeList->getKind() != Node::Kind::TypeList)
              return BuilderError(
                  "Requested generic parameter at depth=%" PRIu64
                  " index=%" PRIu64
                  ", child 1 of BoundGeneric is %s, not TypeList",
                  depth, index, nodeKindString(typeList->getKind()));

            auto child = typeList->getChild(index);
            if (!child)
              return BuilderError(
                  "Requested generic parameter at depth=%" PRIu64
                  " index=%" PRIu64
                  ", but bound generic TypeList only has %zu children",
                  depth, index, typeList->getNumChildren());

            return child;
          }
          foundDepth++;
          cursor = cursor->getFirstChild();
          break;

        default:
          cursor = cursor->getFirstChild();
          break;
        }
      }

      return BuilderError("Requested generic parameter at depth=%" PRIu64
                          " index=%" PRIu64 ", but maximum depth was %u",
                          depth, index, foundDepth);
    }

    for (size_t i = 0; i < node->getNumChildren(); i++) {
      auto child = node->getChild(i);
      auto newChild = substituteGenericParameters(child, metadataMangleNode,
                                                  containingMetadataBuffer);
      if (!newChild)
        return *newChild.getError();
      if (*newChild != child)
        node->replaceChild(i, *newChild);
    }

    return node;
  }

  BuilderErrorOr<Buffer<const Metadata>> getTypeByMangledName(
      WritableData<FullMetadata<Metadata>> containingMetadataBuffer,
      NodePointer metadataMangleNode, llvm::StringRef mangledTypeName) {
    Demangle::Demangler dem;

    auto node =
        dem.demangleType(mangledTypeName, ResolveToDemangling(*this, dem));
    LOG(LogLevel::Detail, "%s", getNodeTreeAsString(node).c_str());

    auto substituted = substituteGenericParameters(node, metadataMangleNode,
                                                   containingMetadataBuffer);
    if (!substituted)
      return *substituted.getError();

    LOG(LogLevel::Detail, "%s", getNodeTreeAsString(*substituted).c_str());

    return context->metadataForNode(*substituted);
  }

  /// Get info about the symbol corresponding to the given buffer. If no
  /// information can be retrieved, the result is filled with "<unknown>"
  /// strings and a 0 offset.
  template <typename T>
  SymbolInfo getSymbolInfo(Buffer<T> buffer) {
    SymbolInfo result = {"<unknown>", "<unknown>", 0};
    if (buffer.file) {
      result.libraryName = buffer.file->path;
      if (auto *address = buffer.getVirtualAddress(buffer.ptr).getValue()) {
        auto symbol = getNearestExportedSymbol(buffer.file, *address);
        if (symbol) {
          result.symbolName = symbol->name;
          result.pointerOffset = *address - symbol->address;
        }
      }
    }
    return result;
  }

  // Find the nearest exported symbol before the target, if possible. Finds the
  // first symbol after the target if there are none before.
  const Export *getNearestExportedSymbol(MachOFile *file, uint64_t addr) {
    return getNearestSymbol(file, addr, file->exportsSortedByAddress);
  }

  template <typename T>
  WritableData<T> allocate(size_t size) {
    return context->template allocate<T>(size);
  }
};

// Produces the standard mangled name for a prespecialized metadata that we'll
// use as the key to the metadata map table.
static BuilderErrorOr<std::string>
_standardMangledNameForNode(Demangle::NodePointer typeNode,
                            Demangler &demangler) {
  // Wrap the type in a global node to match the mangling we get from the input.
  if (typeNode->getKind() != Node::Kind::Global) {
    typeNode = wrapNode(demangler, typeNode, Node::Kind::Global);
  }

  auto resolver = [](SymbolicReferenceKind kind,
                     const void *ref) -> NodePointer { abort(); };
  auto mangling = Demangle::mangleNode(typeNode, resolver, demangler);

  if (!mangling.isSuccess())
    return BuilderError("Failed to mangle node: %s",
                        getNodeTreeAsString(typeNode).c_str());

  return mangling.result().str();
}

static std::string getInstallName(llvm::object::MachOObjectFile *objectFile) {
  for (auto &command : objectFile->load_commands()) {
    if (command.C.cmd == llvm::MachO::LC_ID_DYLIB) {
      auto dylibCommand = objectFile->getDylibIDLoadCommand(command);
      return command.Ptr + dylibCommand.dylib.name;
    }
  }

  return "";
}

template <typename Runtime>
BuilderErrorOr<NodePointer>
ExternalGenericMetadataBuilderContext<Runtime>::buildDemanglingForContext(
    Buffer<const ContextDescriptor> descriptorBuffer, Demangler &dem) {
  NodePointer node = nullptr;

  // Walk up the context tree.
  llvm::SmallVector<Buffer<const ContextDescriptor>, 8> descriptorPath;
  {
    Buffer<const ContextDescriptor> parent = descriptorBuffer;
    while (parent) {
      descriptorPath.push_back(parent);

      // Temporary shenanigans to resolve the weird pointer type until we figure
      // out the right template whatnot to make it work properly.
      auto parentFieldPointer = &parent.ptr->Parent;
      auto parentFieldPointerCast =
          (const TargetRelativeIndirectablePointer<Runtime, ContextDescriptor>
               *)parentFieldPointer;
      auto newParent = parent.resolvePointer(parentFieldPointerCast);
      if (!newParent)
        return *newParent.getError();
      parent = *newParent;
    }
  }

  for (auto component : llvm::reverse(descriptorPath)) {
    switch (auto kind = component.ptr->getKind()) {
    case ContextDescriptorKind::Module: {
      if (node != nullptr)
        return BuilderError("Building demangling for context, found module not "
                            "at the top level");

      auto moduleDescriptor =
          component.template cast<ModuleContextDescriptor>();
      auto name = moduleDescriptor.resolvePointer(&moduleDescriptor.ptr->Name);
      if (!name)
        return *name.getError();
      node = dem.createNode(Node::Kind::Module, name->ptr);
      break;
    }

    case ContextDescriptorKind::Extension: {
      auto extension = component.template cast<ExtensionContextDescriptor>();
      auto mangledExtendedContextBuffer =
          extension.resolvePointer(&extension.ptr->ExtendedContext);
      if (!mangledExtendedContextBuffer)
        return *mangledExtendedContextBuffer.getError();
      auto mangledExtendedContext = Demangle::makeSymbolicMangledNameStringRef(
          mangledExtendedContextBuffer->ptr);
      typename ReaderWriter<Runtime>::ResolveToDemangling resolver{
          *readerWriter, dem};
      auto selfType = dem.demangleType(mangledExtendedContext, resolver);

      if (selfType->getKind() == Node::Kind::Type)
        selfType = selfType->getFirstChild();

      if (selfType->getKind() == Node::Kind::BoundGenericEnum ||
          selfType->getKind() == Node::Kind::BoundGenericStructure ||
          selfType->getKind() == Node::Kind::BoundGenericClass ||
          selfType->getKind() == Node::Kind::BoundGenericOtherNominalType) {
        // TODO: Use the unsubstituted type if we can't handle the
        // substitutions yet.
        selfType = selfType->getChild(0)->getChild(0);
      }

      auto extNode = dem.createNode(Node::Kind::Extension);
      extNode->addChild(node, dem);
      extNode->addChild(selfType, dem);

      // TODO: Turn the generic signature into a demangling as the third
      // generic argument.

      node = extNode;
      break;
    }

    case ContextDescriptorKind::Protocol: {
      auto protocol = component.template cast<ProtocolDescriptor>();
      auto name = protocol.resolvePointer(&protocol.ptr->Name);
      if (!name)
        return *name.getError();

      auto protocolNode = dem.createNode(Node::Kind::Protocol);
      protocolNode->addChild(node, dem);
      auto nameNode = dem.createNode(Node::Kind::Identifier, name->ptr);
      protocolNode->addChild(nameNode, dem);

      node = protocolNode;
      break;
    }

    default:
      // Form a type context demangling for type contexts.
      if (auto type = llvm::dyn_cast<TypeContextDescriptor>(component.ptr)) {
        if (type->getTypeContextDescriptorFlags().hasImportInfo())
          return BuilderError("Don't know how to build demangling for type "
                              "context descriptor with import info");

        Node::Kind nodeKind;
        switch (kind) {
        case ContextDescriptorKind::Class:
          nodeKind = Node::Kind::Class;
          break;
        case ContextDescriptorKind::Struct:
          nodeKind = Node::Kind::Structure;
          break;
        case ContextDescriptorKind::Enum:
          nodeKind = Node::Kind::Enum;
          break;
        default:
          // We don't know about this kind of type. Use an "other type" mangling
          // for it.
          nodeKind = Node::Kind::OtherNominalType;
          break;
        }

        auto name = component.resolvePointer(&type->Name);
        if (!name)
          return *name.getError();

        auto typeNode = dem.createNode(nodeKind);
        typeNode->addChild(node, dem);
        auto nameNode = dem.createNode(Node::Kind::Identifier, name->ptr);
        typeNode->addChild(nameNode, dem);

        node = typeNode;
        break;
      }

      return BuilderError(
          "Don't know how to build demangling for descriptor kind %hhu",
          static_cast<uint8_t>(kind));
    }
  }

  return wrapNode(dem, node, Node::Kind::Type);
}

template <typename Runtime>
llvm::Error ExternalGenericMetadataBuilderContext<Runtime>::addImage(
    std::unique_ptr<llvm::object::Binary> &&binary,
    std::optional<std::unique_ptr<llvm::MemoryBuffer>> &&memoryBuffer,
    const std::string &path) {
  if (isa<llvm::object::MachOObjectFile>(binary.get())) {
    auto objectFile = cast<llvm::object::MachOObjectFile>(std::move(binary));
    auto filetype = objectFile->getHeader().filetype;
    // Do we want to support bundles?
    if (filetype != llvm::MachO::MH_DYLIB &&
        filetype != llvm::MachO::MH_EXECUTE)
      return llvm::createStringError(llvm::inconvertibleErrorCode(),
                                     "Unsupported mach-o filetype %" PRIu32,
                                     filetype);
    LOG(LogLevel::Info, "Loaded %p from %s", objectFile.get(), path.c_str());
    MachOFile *file =
        new MachOFile{std::move(memoryBuffer), std::move(objectFile), path};
    addImage(file);
    return llvm::Error::success();
  }

  if (auto universal =
          dyn_cast<llvm::object::MachOUniversalBinary>(binary.get())) {
    auto objectForArch = universal->getMachOObjectForArch(arch);
    if (!objectForArch)
      return objectForArch.takeError();
    auto objectFile = objectForArch.get().get();
    LOG(LogLevel::Info, "Loaded %p from %s", objectFile, path.c_str());
    MachOFile *file = new MachOFile{std::move(memoryBuffer),
                                    std::move(objectForArch.get()), path};
    addImage(file);
    return llvm::Error::success();
  }

  return llvm::createStringError(llvm::inconvertibleErrorCode(),
                                 "Unknown binary type.");
}

template <typename Runtime>
llvm::Error ExternalGenericMetadataBuilderContext<Runtime>::addImageAtPath(
    const std::string &path) {
  LOG(LogLevel::Info, "Adding %s", path.c_str());

  auto binaryOwner = llvm::object::createBinary(path);
  if (!binaryOwner) {
    return binaryOwner.takeError();
  }
  auto [binary, buffer] = binaryOwner->takeBinary();
  return addImage(std::move(binary), std::optional(std::move(buffer)), path);
}

template <typename Runtime>
llvm::Error ExternalGenericMetadataBuilderContext<Runtime>::addImageInMemory(
    const void *start, uint64_t length, const std::string &path) {
  LOG(LogLevel::Info, "Adding %s in memory", path.c_str());
  llvm::StringRef stringRef{reinterpret_cast<const char *>(start), length};
  llvm::MemoryBufferRef bufferRef{stringRef, {}};
  auto binary = llvm::object::createBinary(bufferRef);
  if (!binary)
    return binary.takeError();
  return addImage(std::move(binary.get()), {}, path);
}

/// Add images in the given path. Returns an error if there was a filesystem
/// error when iterating over the directory. Errors reading individual images
/// are NOT returned, as it's assumed that there may be non-dylib content in the
/// directory as well.
template <typename Runtime>
BuilderErrorOr<std::monostate>
ExternalGenericMetadataBuilderContext<Runtime>::addImagesInPath(
    std::string path) {
  auto forEachFile = [&](std::string filepath) {
    auto error = addImageAtPath(filepath);
    handleAllErrors(std::move(error), [&](const llvm::ErrorInfoBase &E) {
      fprintf(stderr, "Could not read dylib at %s: %s\n", filepath.c_str(),
              E.message().c_str());
    });
  };
  auto result = enumerateFilesInPath(path, forEachFile);
  if (!result)
    return *result.getError();

  return {{}};
}

template <typename Runtime>
BuilderErrorOr<
    typename ExternalGenericMetadataBuilderContext<Runtime>::template Buffer<
        const typename ExternalGenericMetadataBuilderContext<
            Runtime>::Metadata>>
ExternalGenericMetadataBuilderContext<Runtime>::metadataForNode(
    swift::Demangle::NodePointer node) {
  Demangler demangler;

  auto symbolNode =
      wrapNode(demangler, node, Node::Kind::Global, Node::Kind::TypeMetadata);

  auto resolver = [](SymbolicReferenceKind kind,
                     const void *ref) -> NodePointer { abort(); };
  auto symbolMangling = Demangle::mangleNode(symbolNode, resolver, demangler);
  if (!symbolMangling.isSuccess()) {
    return BuilderError("symbol mangling failed with code %u",
                        symbolMangling.error().code);
  }
  LOG(LogLevel::Detail, "symbol mangling: %s",
      symbolMangling.result().str().c_str());

  auto symbolName = symbolMangling.result();
  auto maybeSymbol =
      readerWriter->template getSymbolPointer<Metadata>(symbolName);
  auto symbolResult = findSymbol(symbolName, nullptr);
  if (symbolResult) {
    auto [file, symbol] = *symbolResult;
    LOG(LogLevel::Detail, "Found symbol for metadata '%s' in %s",
        symbolName.str().c_str(), file->path.c_str());
    return readerWriter->template getSymbolPointerInFile<const Metadata>(
        symbolName, symbol, file);
  }

  auto metadataName = _standardMangledNameForNode(node, demangler);
  if (!metadataName)
    return *metadataName.getError();
  auto metadata = builtMetadataMap.find(*metadataName);
  if (metadata != builtMetadataMap.end()) {
    auto &[name, constructedMetadata] = *metadata;
    if (!constructedMetadata)
      return *constructedMetadata.getError();
    LOG(LogLevel::Detail, "Found metadata we already built for '%s'",
        symbolName.str().c_str());
    return constructedMetadata->data.offsetBy(constructedMetadata->offset)
        .template cast<const Metadata>();
  }

  // TODO: need a way to find non-exported metadata with no corresponding
  // symbol.

  LOG(LogLevel::Detail, "Did not find metadata '%s', trying to build it",
      symbolName.str().c_str());

  auto constructedMetadata = constructMetadataForNode(node);
  if (!constructedMetadata)
    return *constructedMetadata.getError();
  if (!*constructedMetadata)
    return BuilderError("Failed to find non-generic type '%s'",
                        metadataName->c_str());

  LOG(LogLevel::Info, "Successfully built metadata for '%s'",
      symbolName.str().c_str());
  auto metadataFromMap = builtMetadataMap.find(*metadataName);
  if (metadataFromMap == builtMetadataMap.end())
    return BuilderError("Successfully constructed metadata for "
                        "'%s', but it was not in the map",
                        metadataName->c_str());
  auto constructedMetadataFromMap = std::get<1>(*metadataFromMap);
  if (!constructedMetadataFromMap)
    return BuilderError("Metadata construction for '%s' indicated success, but "
                        "the map contains an error: %s",
                        metadataName->c_str(),
                        constructedMetadataFromMap.getError()->cStr());
  assert((*constructedMetadata)->data.ptr ==
         constructedMetadataFromMap->data.ptr);
  assert((*constructedMetadata)->offset == constructedMetadataFromMap->offset);

  return (*constructedMetadata)
      ->data.offsetBy((*constructedMetadata)->offset)
      .template cast<const Metadata>();
}

template <typename Runtime>
std::optional<std::pair<MachOFile *, Export>>
ExternalGenericMetadataBuilderContext<Runtime>::findSymbol(
    llvm::StringRef toFind, MachOFile *searchFile) {
  llvm::SmallString<128> prefixed;
  prefixed += "_";
  prefixed += toFind;

  if (searchFile == nullptr) {
    auto it = allSymbols.find(prefixed);
    if (it != allSymbols.end())
      return it->getValue();
    return {};
  }

  auto it = searchFile->symbols.find(prefixed);
  if (it != searchFile->symbols.end())
    return {{searchFile, it->getValue()}};

  return {};
}

template <typename Runtime>
std::optional<llvm::object::SectionRef>
ExternalGenericMetadataBuilderContext<Runtime>::findSectionInFile(
    MachOFile *file, uint64_t vmAddr) {
  // TODO: be smarter than scanning all sections?
  for (auto &section : file->objectFile->sections()) {
    if (section.getAddress() <= vmAddr &&
        vmAddr < section.getAddress() + section.getSize()) {
      return section;
    }
  }

  return {};
}

template <typename Runtime>
std::optional<Segment>
ExternalGenericMetadataBuilderContext<Runtime>::findSegmentInFile(
    MachOFile *file, uint64_t address) {
  // A brute force search isn't the most efficient, but we typically have only
  // a handful of segments.
  for (auto &segment : file->segments) {
    if (segment.address <= address && address < segment.address + segment.size)
      return {segment};
  }
  return {};
}

template <typename Runtime>
std::optional<MachOFile *>
ExternalGenericMetadataBuilderContext<Runtime>::findPointerInFiles(
    const void *ptr) {
  auto target = (uintptr_t)ptr;

  // TODO: we should probably do a binary search or anything better than just
  // a linear scan.
  for (auto &file : machOFiles) {
    auto buffer = file->objectFile->getMemoryBufferRef();
    auto start = (uintptr_t)buffer.getBufferStart();
    auto end = (uintptr_t)buffer.getBufferEnd();
    if (start <= target && target < end)
      return file.get();
  }

  return {};
}

template <typename Runtime>
void ExternalGenericMetadataBuilderContext<Runtime>::build() {
  // Set up the builder.
  builder.reset(
      new GenericMetadataBuilder<ReaderWriter<Runtime>>{*readerWriter.get()});

  // Process all input symbmols.
  for (auto mangledTypeName : mangledNamesToBuild) {
    LOG(LogLevel::Info, "Processing JSON requested metadata for %s",
        mangledTypeName.c_str());
    auto result = ensureMetadataForMangledTypeName(mangledTypeName);
    if (auto *error = result.getError())
      LOG(LogLevel::Warning, "Could not construct metadata for '%s': %s\n",
          mangledTypeName.c_str(), error->cStr());
  }

  auto metadataMap = serializeMetadataMapTable();

  using PrespecializedData = LibPrespecializedData<Runtime>;
  auto topLevelData = allocate<PrespecializedData>(sizeof(PrespecializedData));
  topLevelData.setName(LIB_PRESPECIALIZED_TOP_LEVEL_SYMBOL_NAME);
  topLevelData.ptr->majorVersion = PrespecializedData::currentMajorVersion;
  topLevelData.ptr->minorVersion = PrespecializedData::currentMinorVersion;

  auto result = topLevelData.writePointer(
      &topLevelData.ptr->metadataMap, metadataMap.template cast<const void>());
  if (!result) {
    fprintf(stderr,
            "Fatal error: could not write pointer to top level data: %s\n",
            result.getError()->cStr());
    abort();
  }
}

template <typename Runtime>
void ExternalGenericMetadataBuilderContext<Runtime>::writeOutput(
    llvm::json::OStream &J, unsigned platform,
    const std::string &platformVersion) {
  writeJSONSerialization(J, platform, platformVersion);
}

template <typename Runtime>
void ExternalGenericMetadataBuilderContext<Runtime>::logDescriptorMap() {
  if (logLevel <= LogLevel::Detail) {
    LOG(LogLevel::Detail, "Descriptor map (%zu entries):",
        mangledNominalTypeDescriptorMap.size());
    for (auto [name, descriptor] : mangledNominalTypeDescriptorMap) {
      fprintf(stderr, "    %s -> %p\n", name.c_str(), descriptor.ptr);
    }
  }
}

template <typename Runtime>
void ExternalGenericMetadataBuilderContext<Runtime>::cacheDescriptor(
    Buffer<const ContextDescriptor> descriptorBuffer) {
  auto desc = descriptorBuffer.ptr;
  auto typeDesc = dyn_cast<const TypeContextDescriptor>(desc);
  if (!typeDesc) {
    return;
  }

  // This produces a Type -> {Structure|Class|Enum}
  Demangle::Demangler Dem;
  auto demangleNode = buildDemanglingForContext(descriptorBuffer, Dem);
  if (auto *error = demangleNode.getError()) {
    auto symbolInfo = readerWriter->getSymbolInfo(descriptorBuffer);
    LOG(LogLevel::Warning,
        "Could not build demangling for context %s (%s + %" PRIu64 "): %s",
        symbolInfo.symbolName.c_str(), symbolInfo.libraryName.c_str(),
        symbolInfo.pointerOffset, error->cStr());
    return;
  }

  // ... which needs to be wrapped in Global -> NominalTypeDescriptor -> * to
  // match what we cache during dylib reading.
  auto global = wrapNode(Dem, *demangleNode.getValue(), Node::Kind::Global,
                         Node::Kind::NominalTypeDescriptor);

  auto mangling = Demangle::mangleNode(global);
  if (!mangling.isSuccess()) {
    auto symbolInfo = readerWriter->getSymbolInfo(descriptorBuffer);
    LOG(LogLevel::Warning,
        "Could not build demangling for context %s (%s + %" PRIu64 "): %s",
        symbolInfo.symbolName.c_str(), symbolInfo.libraryName.c_str(),
        symbolInfo.pointerOffset, getNodeTreeAsString(global).c_str());
    return;
  }

  LOG(LogLevel::Info, "Found descriptor %s", mangling.result().c_str());

  auto [iterator, didInsert] = mangledNominalTypeDescriptorMap.try_emplace(
      mangling.result(),
      descriptorBuffer.template cast<const TypeContextDescriptor>());

  if (!didInsert) {
    auto [name, oldTypeDesc] = *iterator;
    LOG(LogLevel::Warning,
        "Descriptor for %s already exists - old: %p - new: %p", name.c_str(),
        oldTypeDesc.ptr, typeDesc);
  }
}

// Perform prep tasks on file, add it to machOFiles, and take ownership of the
// object.
template <typename Runtime>
void ExternalGenericMetadataBuilderContext<Runtime>::addImage(MachOFile *file) {
  // Replace the path with the library's install name if possible.
  auto installName = getInstallName(file->objectFile.get());
  if (installName.length() > 0)
    file->path = installName;

  populateMachOSymbols(file);
  populateMachOFixups(file);
  readMachOSections(file);
  machOFiles.emplace_back(file);
  machOFilesByPath.insert({file->path, file});
}

template <typename Runtime>
void ExternalGenericMetadataBuilderContext<Runtime>::populateMachOSymbols(
    MachOFile *file) {
  // Fill out library names.
  for (auto &command : file->objectFile->load_commands()) {
    if (command.C.cmd == llvm::MachO::LC_LOAD_DYLIB ||
        command.C.cmd == llvm::MachO::LC_LOAD_WEAK_DYLIB ||
        command.C.cmd == llvm::MachO::LC_REEXPORT_DYLIB ||
        command.C.cmd == llvm::MachO::LC_LOAD_UPWARD_DYLIB) {
      auto *dylibCommand =
          reinterpret_cast<const llvm::MachO::dylib_command *>(command.Ptr);
      auto name = reinterpret_cast<const char *>(dylibCommand) +
                  dylibCommand->dylib.name;
      file->libraryNames.push_back(name);
    }
  }

  // Find all the segments.
  for (auto &command : file->objectFile->load_commands()) {
    auto addSegment = [&](const auto &segmentCommand) {
      Segment segment{segmentCommand.segname, segmentCommand.vmaddr,
                      segmentCommand.vmsize};
      if (segment.name == "__TEXT")
        file->baseAddress = segment.address;
      file->segments.push_back(segment);
    };
    if (command.C.cmd == llvm::MachO::LC_SEGMENT_64) {
      auto segmentCommand = file->objectFile->getSegment64LoadCommand(command);
      addSegment(segmentCommand);
    } else if (command.C.cmd == llvm::MachO::LC_SEGMENT) {
      auto segmentCommand = file->objectFile->getSegmentLoadCommand(command);
      addSegment(segmentCommand);
    }
  }

  // Find all the exported symbols.
  llvm::Error err = llvm::Error::success();
  for (auto &ex : file->objectFile->exports(err)) {
    // Just in case there's somehow a symbol with a name that's not valid UTF-8,
    // ignore it, since we can't represent it in our JSON output.
    if (!llvm::json::isUTF8(ex.name()))
      continue;

    Export convertedExport{ex.name().str(), ex.address() + file->baseAddress};
    file->exportsSortedByAddress.push_back(convertedExport);
    allSymbols.insert({ex.name(), {file, convertedExport}});

    auto [iterator, didInsert] =
        file->symbols.try_emplace(ex.name(), convertedExport);
    if (!didInsert) {
      LOG(LogLevel::Detail, "Duplicate symbol name %s in %s",
          ex.name().str().c_str(), file->path.c_str());
    }
  }
  if (err)
    consumeError(std::move(err));

  auto compareAddresses = [&](Export &a, Export &b) {
    return a.address < b.address;
  };
  std::sort(file->exportsSortedByAddress.begin(),
            file->exportsSortedByAddress.end(), compareAddresses);

  LOG(LogLevel::Info, "%s: populated %zu linked libraries and %u symbols",
      file->path.c_str(), file->libraryNames.size(), file->symbols.size());
}

template <typename Runtime>
void ExternalGenericMetadataBuilderContext<Runtime>::populateMachOFixups(
    MachOFile *file) {
  llvm::Error err = llvm::Error::success();

  auto objectFile = file->objectFile.get();

  std::optional<std::variant<llvm::object::MachOBindEntry,
                             llvm::object::MachOChainedFixupEntry>>
      found;

  for (const auto &bind : objectFile->bindTable(err)) {
    LOG(LogLevel::Detail, "bind: %s %s %s %s %#" PRIx64,
        bind.typeName().str().c_str(), bind.symbolName().str().c_str(),
        bind.segmentName().str().c_str(), bind.sectionName().str().c_str(),
        bind.address());

    FixupTarget target{bind.ordinal(), bind.symbolName()};
    file->fixups.insert({bind.address(), target});
  }
  if (err) {
    LOG(LogLevel::Warning, "Failed to get bind table: %s",
        getErrorString(std::move(err)).c_str());
  }

  // Ignoring lazyBindTable and weakBindTable, as the symbols we're interested
  // in shouldn't appear in them.

  for (const auto &fixup : objectFile->fixupTable(err)) {
    const char *libName;
    int ordinal = fixup.ordinal();
    if (ordinal > 0 && (size_t)ordinal <= file->libraryNames.size())
      libName = file->libraryNames[ordinal - 1];
    else if (ordinal == FixupTarget::selfLibrary)
      libName = "<self>";
    else
      libName = "<unknown ordinal>";

    LOG(LogLevel::Detail,
        "fixup: ordinal=%d type=%s symbol=%s segment=%s section=%s "
        "address=%#" PRIx64 " lib=%s",
        ordinal, fixup.typeName().str().c_str(),
        fixup.symbolName().str().c_str(), fixup.segmentName().str().c_str(),
        fixup.sectionName().str().c_str(), fixup.address(), libName);

    FixupTarget target;
    target.library = fixup.ordinal();
    if (auto ptr = fixup.pointerValue())
      target.target = ptr;
    else
      target.target = fixup.symbolName();
    file->fixups.insert({fixup.address(), target});
  }
  if (err) {
    LOG(LogLevel::Warning, "Failed to get bind table: %s",
        getErrorString(std::move(err)).c_str());
  }
}

template <typename Runtime>
void ExternalGenericMetadataBuilderContext<Runtime>::readMachOSections(
    MachOFile *file) {
  using namespace llvm;
  // Search for the section for types
  for (auto &Section : file->objectFile->sections()) {
    llvm::Expected<StringRef> SectionNameOrErr = Section.getName();
    if (!SectionNameOrErr) {
      llvm::consumeError(SectionNameOrErr.takeError());
      continue;
    }

    StringRef SectionName = *SectionNameOrErr;
    if (SectionName == "__swift5_types") {
      llvm::Expected<llvm::StringRef> SectionData = Section.getContents();
      if (!SectionData) {
        LOG(LogLevel::Warning,
            "Failed to get __swift5_types section data from %s: %s",
            file->path.c_str(),
            getErrorString(SectionData.takeError()).c_str());
        return;
      }
      auto Contents = SectionData.get();

      using SectionContentType =
          const RelativeDirectPointerIntPair<ContextDescriptor,
                                             TypeReferenceKind>;

      Buffer<SectionContentType> sectionBuffer{this, file, Section, Contents};
      size_t count = Contents.size() / sizeof(SectionContentType);
      for (size_t i = 0; i < count; i++) {
        auto &record = sectionBuffer.ptr[i];
        if (record.getInt() == TypeReferenceKind::DirectTypeDescriptor) {
          auto resolved = sectionBuffer.resolvePointer(&record);
          if (auto *error = resolved.getError()) {
            LOG(LogLevel::Warning,
                "Descriptor record %p (__swift5_types + %zu), failed to "
                "resolve pointer: %s",
                &record, (size_t)&record - (size_t)sectionBuffer.ptr,
                error->cStr());
            break;
          }

          auto descBuffer = *resolved.getValue();
          LOG(LogLevel::Info,
              "Caching descriptor record %p (__swift5_types + %zu) -> %p",
              &record, (size_t)&record - (size_t)sectionBuffer.ptr,
              descBuffer.ptr);
          cacheDescriptor(descBuffer);
        } else {
          LOG(LogLevel::Warning,
              "Descriptor record %p (__swift5_types + %zu) is indirect, "
              "not yet implemented",
              &record, (size_t)&record - (size_t)sectionBuffer.ptr);
        }
      }
      break;
    }
  }
}

template <typename Runtime>
BuilderErrorOr<std::string> ExternalGenericMetadataBuilderContext<Runtime>::_mangledNominalTypeNameForBoundGenericNode(
    Demangle::NodePointer BoundGenericNode) {
  LOG(LogLevel::Detail, "BoundGenericNode:\n%s",
      getNodeTreeAsString(BoundGenericNode).c_str());

  Demangle::Demangler Dem;
  auto unspecializedOrError = getUnspecialized(BoundGenericNode, Dem);
  if (!unspecializedOrError.isSuccess()) {
    return BuilderError("getUnspecialized failed with code %u",
                        unspecializedOrError.error().code);
  }
  auto unspecialized = unspecializedOrError.result();
  LOG(LogLevel::Detail, "unspecialized:\n%s",
      getNodeTreeAsString(unspecialized).c_str());

  auto globalNode =
      wrapNode(Dem, unspecialized, Node::Kind::Global,
               Node::Kind::NominalTypeDescriptor, Node::Kind::Type);

  LOG(LogLevel::Detail, "globalTypeDescriptor:\n%s",
      getNodeTreeAsString(globalNode).c_str());

  auto mangling = Demangle::mangleNode(globalNode);
  if (!mangling.isSuccess()) {
    return BuilderError("Failed to mangle unspecialized node.");
  }

  return mangling.result();
}

template <typename Runtime>
BuilderErrorOr<std::monostate> ExternalGenericMetadataBuilderContext<
    Runtime>::ensureMetadataForMangledTypeName(llvm::StringRef typeName) {
  auto node = demangleCtx.demangleTypeAsNode(typeName);
  if (!node) {
    return BuilderError("Failed to demangle '%s'.", typeName.str().c_str());
  }
  LOG(LogLevel::Detail, "Result: %s", nodeToString(node).c_str());
  auto result = metadataForNode(node);
  if (!result)
    return *result.getError();
  return {{}};
}

// Returns the constructed metadata, or no value if the node doesn't contain a
// bound generic.
template <typename Runtime>
BuilderErrorOr<std::optional<typename ExternalGenericMetadataBuilderContext<
    Runtime>::Builder::ConstructedMetadata>>
ExternalGenericMetadataBuilderContext<Runtime>::constructMetadataForNode(
    swift::Demangle::NodePointer node) {
  LOG(LogLevel::Detail, "\n%s", getNodeTreeAsString(node).c_str());

  // Pre-specialize BoundGenericClass|Structure|Enum
  switch (node->getKind()) {
  case Node::Kind::Global:
  case Node::Kind::Type:
  case Node::Kind::TypeMangling:
  case Node::Kind::TypeMetadata:
    if (!node->hasChildren())
      return BuilderError("%s node has no children",
                          nodeKindString(node->getKind()));
    return constructMetadataForNode(node->getFirstChild());

  case Node::Kind::BoundGenericClass:
  case Node::Kind::BoundGenericStructure:
  case Node::Kind::BoundGenericEnum:
    break;
  case Node::Kind::BoundGenericProtocol:
  case Node::Kind::BoundGenericTypeAlias:
  case Node::Kind::BoundGenericFunction:
  case Node::Kind::BoundGenericOtherNominalType:
    return BuilderError("Can't construct metadata for kind %#hx (%s)",
                        static_cast<uint16_t>(node->getKind()),
                        nodeKindString(node->getKind()));
  default:
    LOG(LogLevel::Info, "Default success with kind %s",
        nodeKindString(node->getKind()));
    return {{}};
  }

  // Get the type descriptor.
  // Type descriptors were cached from __swift5_types sections during
  // readMachOSections. If one is missing here, then there was a mismatch
  // between what bound generic type names we were given, and what dylibs we
  // have parsed.
  auto mangledNominalTypeName =
      _mangledNominalTypeNameForBoundGenericNode(node);
  if (!mangledNominalTypeName)
    return *mangledNominalTypeName.getError();
  auto foundTypeDescriptor =
      mangledNominalTypeDescriptorMap.find(*mangledNominalTypeName);
  if (foundTypeDescriptor == mangledNominalTypeDescriptorMap.end()) {
    // Either the mangling failed, or we didn't find this type during dylib
    // ingestion.
    LOG(LogLevel::Detail, "%s", getNodeTreeAsString(node).c_str());
    return BuilderError(
        "Failed to look up type descriptor for '%s' from string '%s'.",
        nodeToString(node).c_str(), mangledNominalTypeName->c_str());
  }
  auto typeDescriptor = std::get<1>(*foundTypeDescriptor);

  LOG(LogLevel::Detail, "Found type descriptor %p\n", typeDescriptor.ptr);

  // Get the generic arguments.
  auto genericTypeListNode = node->getChild(1);
  assert(genericTypeListNode->getKind() == Node::Kind::TypeList);
  assert(genericTypeListNode->hasChildren());

  std::vector<Buffer<const Metadata>> genericTypeMetadatas;
  for (auto genericTypeNode : *genericTypeListNode) {
    LOG(LogLevel::Detail, "genericTypeNode:\n%s",
        getNodeTreeAsString(genericTypeNode).c_str());
    auto maybeMetadata = metadataForNode(genericTypeNode);
    if (auto *error = maybeMetadata.getError()) {
      return BuilderError("Failed to get generic type reference '%s': %s",
                          nodeToString(genericTypeNode).c_str(), error->cStr());
    }
    genericTypeMetadatas.push_back(*maybeMetadata.getValue());
  }

  // Get the generic metadata pattern.
  auto &generics = typeDescriptor.ptr->getFullGenericContextHeader();
  auto maybePattern =
      typeDescriptor.resolvePointer(&generics.DefaultInstantiationPattern);
  if (auto *error = maybePattern.getError()) {
    return BuilderError("Failed to resolve instantiation pattern pointer: %s",
                        error->cStr());
  }
  auto pattern = *maybePattern.getValue();

  LOG(LogLevel::Detail, "pattern: %p", pattern.ptr);

  // Compute extra data size.
  auto maybeExtraDataSize =
      builder->extraDataSize(typeDescriptor, pattern.toConst());
  if (auto *error = maybeExtraDataSize.getError()) {
    return BuilderError("Failed to compute extra data size: %s", error->cStr());
  }
  auto extraDataSize = *maybeExtraDataSize.getValue();
  LOG(LogLevel::Detail, "extraDataSize: %zu", extraDataSize);

  // Get the canonical name for this metadata.
  Demangle::Demangler Dem;
  auto mangledName = _standardMangledNameForNode(node, Dem);
  if (!mangledName)
    return *mangledName.getError();

  // Build the metadata.
  auto maybeMetadata = builder->buildGenericMetadata(
      typeDescriptor, genericTypeMetadatas, pattern.toConst(), extraDataSize);
  if (auto *error = maybeMetadata.getError()) {
    builtMetadataMap.emplace(*mangledName, *error);
    return BuilderError("Failed to build metadata '%s': %s",
                        getNodeTreeAsString(node).c_str(), error->cStr());
  }
  auto metadata = *maybeMetadata.getValue();

  LOG(LogLevel::Detail, "metadata: %p", metadata.data.ptr);

  // Place the built metadata in the map.
  metadata.data.setName(*mangledName);
  BuilderErrorOr<const typename Builder::ConstructedMetadata> mapValue{
      metadata};
  builtMetadataMap.emplace(*mangledName, mapValue);

  // Initialize the built metadata.
  auto initializeResult =
      builder->initializeGenericMetadata(metadata.data, node);
  if (auto *error = initializeResult.getError()) {
    builtMetadataMap.erase(*mangledName);
    builtMetadataMap.emplace(*mangledName, *error);
    auto resultError = BuilderError("Failed to build metadata '%s': %s",
                                    nodeToString(node).c_str(), error->cStr());
    LOG(LogLevel::Warning, "%s", resultError.cStr());
    return resultError;
  }

  auto optionalMetadata = std::optional{metadata};
  return {optionalMetadata};
}

/// Make the metadata map table, and return a buffer pointing to it.
template <typename Runtime>
typename ExternalGenericMetadataBuilderContext<Runtime>::template Buffer<char>
ExternalGenericMetadataBuilderContext<Runtime>::serializeMetadataMapTable(
    void) {
  // Build a sorted vector of the constructed metadatas. Skip all entries that
  // contain an error.
  std::vector<const std::pair<const std::string,
                              BuilderErrorOr<const ConstructedMetadata>> *>
      sortedMapElements;
  sortedMapElements.reserve(builtMetadataMap.size());
  for (auto &entry : builtMetadataMap) {
    auto &[name, metadata] = entry;
    if (metadata)
      sortedMapElements.push_back(&entry);
  }

  std::sort(sortedMapElements.begin(), sortedMapElements.end(),
            [](const auto *a, const auto *b) {
              return std::get<0>(*a) < std::get<0>(*b);
            });

  auto numEntries = sortedMapElements.size();

  // Array size must be at least numEntries+1 per the requirements of
  // PrebuiltStringMap. Aim for a 75% load factor.
  auto arraySize = std::max(numEntries * 4 / 3, numEntries + 1);

  using Map =
      PrebuiltStringMap<StoredPointer, StoredPointer, StoredPointer::isNull>;
  auto byteSize = Map::byteSize(arraySize);

  auto mapData = allocate<char>(byteSize);
  auto serializedMap = new (mapData.ptr) Map(arraySize);

  for (auto *entry : sortedMapElements) {
    auto [key, value] = *entry;
    auto *elementPtr = serializedMap->insert(key.c_str());
    if (!elementPtr) {
      fprintf(stderr, "PrebuiltStringMap insert failed!\n");
      abort();
    }

    auto stringData = allocate<char>(key.size() + 1);
    memcpy(stringData.ptr, key.c_str(), key.size());
    stringData.setName("_cstring_" + key);
    auto result = mapData.writePointer(&elementPtr->key, stringData);
    if (!result) {
      fprintf(
          stderr,
          "Fatal error: could not write pointer to metadata table name: %s\n",
          result.getError()->cStr());
      abort();
    }

    auto metadataPointer = value->data.offsetBy(value->offset);
    result = mapData.writePointer(&elementPtr->value, metadataPointer);
    if (!result) {
      fprintf(
          stderr,
          "Fatal error: could not write pointer to metadata table entry: %s\n",
          result.getError()->cStr());
      abort();
    }
  }

  mapData.setName("_swift_prespecializedMetadataMap");

  return mapData;
}

/// Write the atom's contents as JSON. For each symbol reference emitted, the
/// callback is passed the target MachOFile * and StringRef of the symbol's
/// name.
template <typename Runtime>
template <typename SymbolCallback>
void ExternalGenericMetadataBuilderContext<Runtime>::writeAtomContentsJSON(
    llvm::json::OStream &J,
    const typename ExternalGenericMetadataBuilderContext<Runtime>::Atom &atom,
    const SymbolCallback &symbolCallback) {

  // Maintain cursors in the atom's buffer and targets. In a loop, we write out
  // any buffer contents between the current point and the next target, then we
  // write out the next target.
  size_t bufferCursor = 0;
  auto targetsCursor = atom.pointerTargetsSorted.begin();
  auto targetsEnd = atom.pointerTargetsSorted.end();

  auto writeSlice = [&](size_t from, size_t to) {
    if (from < to) {
      llvm::StringRef slice{&atom.buffer[from], to - from};
      J.value(llvm::toHex(slice));
    }
  };

  while (targetsCursor != targetsEnd) {
    assert(bufferCursor <= targetsCursor->offset);
    writeSlice(bufferCursor, targetsCursor->offset);

    J.object([&] {
      if (auto *fileTarget =
              std::get_if<FileTarget>(&targetsCursor->fileOrAtom)) {
        symbolCallback(fileTarget->file, fileTarget->nearestExport->name);

        auto addend = (int64_t)fileTarget->addressInFile -
                      (int64_t)fileTarget->nearestExport->address;

        J.attribute("target", fileTarget->nearestExport->name);
        J.attribute("addend", addend);
      } else {
        auto atomTarget = std::get<AtomTarget>(targetsCursor->fileOrAtom);
        J.attribute("self", true);
        J.attribute("target", "_" + atomTarget.atom->name);
        J.attribute("addend", atomTarget.offset);
      }

      const char *ptrTargetKind =
          sizeof(StoredPointer) == 8 ? "ptr64" : "ptr32";

      if (usePtrauth && targetsCursor->ptrauthKey != PtrauthKey::None) {
        J.attributeObject("authPtr", [&] {
          J.attribute("key", static_cast<uint8_t>(targetsCursor->ptrauthKey));
          J.attribute("addr", targetsCursor->addressDiversified);
          J.attribute("diversity", targetsCursor->discriminator);
        });
        ptrTargetKind = "arm64_auth_ptr";
      }

      J.attribute("kind", ptrTargetKind);
    });

    bufferCursor = targetsCursor->offset + targetsCursor->size;

    targetsCursor++;
  }

  // Write any remaining data after the last target.
  writeSlice(bufferCursor, atom.buffer.size());
}

template <typename Runtime>
void ExternalGenericMetadataBuilderContext<Runtime>::writeDylibsJSON(
    llvm::json::OStream &J,
    std::unordered_map<MachOFile *, std::unordered_set<std::string>>
        &symbolReferences) {
  for (auto &elt : symbolReferences) {
    auto file = std::get<0>(elt);
    auto &names = std::get<1>(elt);
    J.object([&] {
      J.attribute("installName", file->path);
      J.attributeArray("exports", [&] {
        for (auto name : names) {
          J.object([&] { J.attribute("name", name); });
        }
      });
    });
  }
}

template <typename Runtime>
void ExternalGenericMetadataBuilderContext<Runtime>::writeJSONSerialization(
    llvm::json::OStream &J, unsigned platform,
    const std::string &platformVersion) {
  // Name any unnamed atoms.
  unsigned i = 0;
  for (auto &atom : atoms) {
    if (atom->name.length() == 0) {
      atom->name = "__unnamed_atom_" + std::to_string(i++);
    }
  }

  J.object([&] {
    J.attribute("version", 1);

    J.attribute("platform", platform);
    J.attribute("platformVersion", platformVersion);

    J.attribute("arch", arch);
    J.attribute("installName", "/usr/lib/libswiftPrespecialized.dylib");

    // Track the referenced libraries and symbol names from all of the atoms.
    std::unordered_map<MachOFile *, std::unordered_set<std::string>>
        symbolReferences;
    J.attributeArray("atoms", [&] {
      for (auto &atom : atoms) {
        J.object([&] {
          J.attribute("name", "_" + atom->name);
          J.attribute("contentType", "constData");
          J.attributeArray("contents", [&] {
            writeAtomContentsJSON(
                J, *atom, [&](MachOFile *file, llvm::StringRef symbolName) {
                  symbolReferences[file].insert(symbolName.str());
                });
          });
        });
      }
    });

    J.attributeArray("dylibs", [&] { writeDylibsJSON(J, symbolReferences); });
  });
}

BuilderErrorOr<std::vector<std::string>> readNames(llvm::StringRef contents) {
  auto json = llvm::json::parse(contents);
  if (!json)
    return BuilderError("Failed to parse names JSON: %s",
                        getErrorString(json.takeError()).c_str());

  auto topLevel = json->getAsObject();
  if (!topLevel)
    return BuilderError(
        "Failed to parse names JSON: top level value is not an object");

  auto metadataNamesValue = topLevel->get("metadataNames");
  if (!metadataNamesValue)
    return BuilderError(
        "Failed to parse names JSON: no 'metadataNames' key in top level");

  auto metadataNames = metadataNamesValue->getAsArray();
  if (!metadataNames)
    return BuilderError(
        "Failed to parse names JSON: 'metadataNames' is not an array");

  std::vector<std::string> names;

  for (auto &entryValue : *metadataNames) {
    auto entry = entryValue.getAsObject();
    if (!entry)
      return BuilderError("Failed to parse names JSON: 'entries' contains "
                          "value that is not an object");

    auto nameValue = entry->get("name");
    if (!nameValue)
      return BuilderError(
          "Failed to parse names JSON: entry does not contain 'name' key");

    auto name = nameValue->getAsString();
    if (!name)
      return BuilderError(
          "Failed to parse names JSON: 'name' value is not a string");

    names.push_back(std::string(*name));
  }

  return {names};
}

BuilderErrorOr<std::vector<std::string>> readNamesFromFile(const char *path) {
  auto contents = llvm::MemoryBuffer::getFile(path);
  if (!contents)
    return BuilderError("Could not read names file at '%s': %s", path,
                        contents.getError().message().c_str());
  return readNames(contents.get()->getBuffer());
}

template <typename Fn>
static BuilderErrorOr<std::monostate> enumerateFilesInPath(std::string path,
                                                           const Fn &callback) {
  std::error_code err;
  llvm::sys::fs::recursive_directory_iterator iterator(path, err);
  llvm::sys::fs::recursive_directory_iterator end;

  while (iterator != end && !err) {
    if (iterator->type() != llvm::sys::fs::file_type::directory_file)
      callback(iterator->path());
    iterator.increment(err);
  }

  if (err)
    return BuilderError("Iteration of directory %s failed: %s", path.c_str(),
                        err.message().c_str());

  return {{}};
}

BuilderErrorOr<unsigned> getPointerWidth(std::string arch) {
  if (arch == "arm64" || arch == "arm64e")
    return 8;
  if (arch == "arm64_32")
    return 4;
  if (arch == "x86_64")
    return 8;

  return BuilderError("Unknown arch '%s'", arch.c_str());
}

} // namespace swift

struct SwiftExternalMetadataBuilder {
  using Builder32 =
      swift::ExternalGenericMetadataBuilderContext<swift::ExternalRuntime32>;
  using Builder64 =
      swift::ExternalGenericMetadataBuilderContext<swift::ExternalRuntime64>;

  std::variant<Builder32, Builder64> context;

  int platform;

  // Storage for an error string returned to the caller.
  std::string lastErrorString;

  SwiftExternalMetadataBuilder(int pointerSize) {
    if (pointerSize == 4) {
      context.emplace<Builder32>();
    } else if (pointerSize == 8) {
      context.emplace<Builder64>();
    } else {
      fprintf(stderr, "Unsupported pointer size %d\n", pointerSize);
      abort();
    }
  }

  template <typename Fn>
  void withContext(const Fn &fn) {
    if (auto *context32 = std::get_if<Builder32>(&context))
      fn(context32);
    if (auto *context64 = std::get_if<Builder64>(&context))
      fn(context64);
  }
};

struct SwiftExternalMetadataBuilder *
swift_externalMetadataBuilder_create(int platform, const char *arch) {
  auto pointerWidth = swift::getPointerWidth(arch);
  if (auto *error = pointerWidth.getError()) {
    fprintf(stderr, "%s\n", error->getErrorString().c_str());
    return nullptr;
  }

  auto *builder = new SwiftExternalMetadataBuilder(*pointerWidth.getValue());
  builder->platform = platform;
  builder->withContext([&](auto *context) { context->setArch(arch); });
  return builder;
}

void swift_externalMetadataBuilder_destroy(
    struct SwiftExternalMetadataBuilder *builder) {
  delete builder;
}

void swift_externalMetadataBuilder_setLogLevel(
    struct SwiftExternalMetadataBuilder *builder, int level) {
  builder->withContext([&](auto *context) { context->setLogLevel(level); });
}

const char *swift_externalMetadataBuilder_readNamesJSON(
    struct SwiftExternalMetadataBuilder *builder, const char *names_json) {
  auto names = swift::readNames(names_json);
  if (auto *error = names.getError()) {
    builder->lastErrorString = error->cStr();
    return builder->lastErrorString.c_str();
  }

  builder->withContext(
      [&](auto *context) { context->setNamesToBuild(*names.getValue()); });
  return nullptr;
}

const char *swift_externalMetadataBuilder_addDylib(
    struct SwiftExternalMetadataBuilder *builder, const char *installName,
    const struct mach_header *mh, uint64_t size) {
  builder->lastErrorString = "";
  builder->withContext([&](auto *context) {
    auto error = context->addImageInMemory(mh, size, installName);
    if (error)
      builder->lastErrorString = swift::getErrorString(std::move(error));
  });

  if (builder->lastErrorString != "")
    return builder->lastErrorString.c_str();
  return nullptr;
}

const char *swift_externalMetadataBuilder_buildMetadata(
    struct SwiftExternalMetadataBuilder *builder) {
  builder->withContext([&](auto *context) { context->build(); });
  return nullptr;
}

const char *swift_externalMetadataBuilder_getMetadataJSON(
    struct SwiftExternalMetadataBuilder *builder) {
  bool prettyPrint = true;
  unsigned indent = prettyPrint ? 4 : 0;

  std::string output;
  llvm::raw_string_ostream ostream(output);
  llvm::json::OStream J(ostream, indent);

  builder->withContext([&](auto *context) {
    context->writeOutput(J, builder->platform, "1.0");
  });

  return strdup(output.c_str());
}

int swift_type_metadata_extract(const char *inputPath,       // mangled names
                                const char *dylibSearchPath, // images to add
                                const char *arch,
                                const char *outputPath // json output
) {
  auto names = swift::readNamesFromFile(inputPath);
  if (auto *error = names.getError()) {
    fprintf(stderr, "%s\n", error->getErrorString().c_str());
    exit(1);
  }

  auto pointerWidth = swift::getPointerWidth(arch);
  if (auto *error = pointerWidth.getError()) {
    fprintf(stderr, "%s\n", error->getErrorString().c_str());
    exit(1);
  }

  SwiftExternalMetadataBuilder builder(*pointerWidth.getValue());
  builder.withContext([&](auto *context) {
    context->setNamesToBuild(*names.getValue());
    context->setArch(arch);
    auto imageAddResult = context->addImagesInPath(dylibSearchPath);
    if (auto *error = imageAddResult.getError()) {
      fprintf(stderr, "Error iterating over dylib search path '%s': %s",
              dylibSearchPath, error->cStr());
      exit(1);
    }
    context->logDescriptorMap();

    bool prettyPrint = true;

    std::error_code errorCode;
    llvm::raw_fd_ostream fileStream(outputPath, errorCode);
    if (errorCode) {
      fprintf(stderr, "ERROR: Could not open %s for writing.\n", outputPath);
      exit(1);
    }

    context->build();

    unsigned indent = prettyPrint ? 4 : 0;
    llvm::json::OStream J(fileStream, indent);

    unsigned platform = 1; // PLATFORM_MACOS
    const char *platformVersion = "14.0";
    context->writeOutput(J, platform, platformVersion);
  });

  fprintf(stderr, "Completed!\n");

  return 0;
}