Loading...
   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
/*
 * Copyright (c) 2017 Apple Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_LICENSE_HEADER_END@
 */


#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <mach/mach_vm.h>
#include <mach-o/dyld.h>
#include <mach-o/dyld_priv.h>
#include <uuid/uuid.h>
#include <os/log.h>

#include <string>
#include <vector>
#include <array>

#include "ImageProxy.h"
#include "FileUtils.h"
#include "StringUtils.h"
#include "MachOParser.h"
#include "LaunchCacheFormat.h"
#include "LaunchCacheWriter.h"
#include "PathOverrides.h"
#include "libdyldEntryVector.h"

namespace dyld3 {

typedef launch_cache::TargetSymbolValue   TargetSymbolValue;



///////////////////////////  ImageProxy  ///////////////////////////

ImageProxy::ImageProxy(const mach_header* mh, const BinaryImageData* imageData, uint32_t indexInGroup, bool dyldCacheIsRaw)
 : _mh(mh), _sliceFileOffset(0), _modTime(0), _inode(0), _imageBinaryData(imageData), _runtimePath(launch_cache::Image(imageData).path()),
   _groupNum(0), _indexInGroup(indexInGroup), _isSetUID(false), _dyldCacheIsRaw(dyldCacheIsRaw), _platformBinary(false), _overrideOf(ImageRef::weakImportMissing()),
   _directDependentsSet(false), _deepDependentsSet(false), _initBeforesArraySet(false), _initBeforesComputed(false),
   _invalid(launch_cache::Image(imageData).isInvalid()), _staticallyReferenced(false), _cwdMustBeThisDir(false)
{
}

ImageProxy::ImageProxy(const DyldSharedCache::MappedMachO& mapping, uint32_t groupNum, uint32_t indexInGroup, bool dyldCacheIsRaw)
 : _mh(mapping.mh), _sliceFileOffset(mapping.sliceFileOffset), _modTime(mapping.modTime), _inode(mapping.inode), _imageBinaryData(nullptr), _runtimePath(mapping.runtimePath),
   _groupNum(groupNum), _indexInGroup(indexInGroup), _isSetUID(mapping.isSetUID), _dyldCacheIsRaw(dyldCacheIsRaw), _platformBinary(mapping.protectedBySIP),
   _overrideOf(ImageRef::weakImportMissing()), _directDependentsSet(false), _deepDependentsSet(false), _initBeforesArraySet(false), _initBeforesComputed(false),
   _invalid(false), _staticallyReferenced(false), _cwdMustBeThisDir(false)
{
}


void ImageProxy::processRPaths(ImageProxyGroup& owningGroup)
{
    // parse LC_RPATH
    __block std::unordered_set<std::string> rawRpaths;
    MachOParser parser(_mh, _dyldCacheIsRaw);
    parser.forEachRPath(^(const char* rpath, bool& stop) {
        if ( rawRpaths.count(rpath) ) {
            _diag.warning("duplicate LC_RPATH (%s) in %s", rpath, _runtimePath.c_str());
            return;
        }
        rawRpaths.insert(rpath);
        std::string thisRPath = rpath;
        if ( startsWith(thisRPath, "@executable_path/") ) {
            std::string mainPath = owningGroup.mainProgRuntimePath();
            if ( mainPath.empty() && parser.isDynamicExecutable() ) {
                mainPath = _runtimePath;
            }
            if ( !mainPath.empty() ) {
                std::string newPath = mainPath.substr(0, mainPath.rfind('/')+1) + thisRPath.substr(17);
                std::string normalizedPath = owningGroup.normalizedPath(newPath);
                if ( fileExists(normalizedPath) )
                    _rpaths.push_back(normalizedPath);
                else
                    _diag.warning("LC_RPATH to nowhere (%s) in %s", rpath, _runtimePath.c_str());
                char resolvedMainPath[PATH_MAX];
                if ( (realpath(mainPath.c_str(), resolvedMainPath) != nullptr) && (mainPath.c_str() != resolvedMainPath) ) {
                    std::string realMainPath = resolvedMainPath;
                    size_t lastSlashPos = realMainPath.rfind('/');
                    std::string newRealPath = realMainPath.substr(0, lastSlashPos+1) + thisRPath.substr(17);
                    if ( realMainPath != mainPath ) {
                        for (const std::string& pre : owningGroup._buildTimePrefixes) {
                            std::string aPath = owningGroup.normalizedPath(pre + newRealPath);
                            if ( fileExists(aPath) ) {
                                _rpaths.push_back(owningGroup.normalizedPath(newRealPath));
                            }
                        }
                    }
                }
            }
            else {
                _diag.warning("LC_RPATH uses @executable_path in %s", _runtimePath.c_str());
            }
        }
        else if ( thisRPath == "@executable_path" ) {
            std::string mainPath = owningGroup.mainProgRuntimePath();
            if ( mainPath.empty() && parser.isDynamicExecutable() ) {
                mainPath = _runtimePath;
            }
            if ( !mainPath.empty() ) {
                std::string newPath = mainPath.substr(0, mainPath.rfind('/')+1);
                std::string normalizedPath = owningGroup.normalizedPath(newPath);
                _rpaths.push_back(normalizedPath);
            }
            else {
                _diag.warning("LC_RPATH uses @executable_path in %s", _runtimePath.c_str());
            }
        }
        else if ( startsWith(thisRPath, "@loader_path/") ) {
            size_t lastSlashPos = _runtimePath.rfind('/');
            std::string newPath = _runtimePath.substr(0, lastSlashPos+1) + thisRPath.substr(13);
            bool found = false;
            for (const std::string& pre : owningGroup._buildTimePrefixes) {
                std::string aPath = owningGroup.normalizedPath(pre + newPath);
                if ( fileExists(aPath) ) {
                    _rpaths.push_back(owningGroup.normalizedPath(newPath));
                    found = true;
                    break;
                }
            }
            char resolvedPath[PATH_MAX];
            if ( (realpath(_runtimePath.c_str(), resolvedPath) != nullptr) && (_runtimePath.c_str() != resolvedPath) ) {
                std::string realRunPath = resolvedPath;
                lastSlashPos = realRunPath.rfind('/');
                std::string newRealPath = realRunPath.substr(0, lastSlashPos+1) + thisRPath.substr(13);
                if ( newRealPath != newPath ) {
                    for (const std::string& pre : owningGroup._buildTimePrefixes) {
                        std::string aPath = owningGroup.normalizedPath(pre + newRealPath);
                        if ( fileExists(aPath) ) {
                            _rpaths.push_back(owningGroup.normalizedPath(newRealPath));
                            found = true;
                            break;
                        }
                    }
                }
            }
            if ( !found ) {
                // even though this path does not exist, we need to add it to must-be-missing paths
                // in case it shows up at launch time
                _rpaths.push_back(owningGroup.normalizedPath(newPath));
                _diag.warning("LC_RPATH to nowhere (%s) in %s", rpath, _runtimePath.c_str());
            }
        }
        else if ( thisRPath == "@loader_path" ) {
            size_t lastSlashPos = _runtimePath.rfind('/');
            std::string newPath = _runtimePath.substr(0, lastSlashPos+1);
            std::string normalizedPath = owningGroup.normalizedPath(newPath);
            _rpaths.push_back(normalizedPath);
        }
        else if ( rpath[0] == '@' ) {
            _diag.warning("LC_RPATH with unknown @ variable (%s) in %s", rpath, _runtimePath.c_str());
        }
        else {
            if ( rpath[0] == '/' )
                _diag.warning("LC_RPATH is absolute path (%s) in %s", rpath, _runtimePath.c_str());
            _rpaths.push_back(rpath);
        }
    });
    //if ( !_rpaths.empty() ) {
    //    fprintf(stderr, "for %s\n", _runtimePath.c_str());
    //    for (const std::string& p : _rpaths)
    //        fprintf(stderr, "   %s\n", p.c_str());
    //}
}

void ImageProxy::addDependentsDeep(ImageProxyGroup& owningGroup, RPathChain* prev, bool staticallyReferenced)
{
    // mark binaries that are statically referenced and thus will never be unloaded
    if ( staticallyReferenced )
        _staticallyReferenced = true;

    if ( _deepDependentsSet )
        return;

    // find all immediate dependents
    addDependentsShallow(owningGroup, prev);
    if ( _diag.hasError() ) {
        _invalid = true;
        return;
    }

    // recurse though each dependent
    RPathChain rchain = { this, prev, _rpaths };
    for (ImageProxy* proxy : _dependents) {
        if ( proxy == nullptr )
            continue; // skip over weak missing dependents
        if ( !proxy->_directDependentsSet )
            proxy->addDependentsDeep(owningGroup, &rchain, staticallyReferenced);
        if ( proxy->invalid() )
            _invalid = true;
    }

    _deepDependentsSet = true;
}

void ImageProxy::addDependentsShallow(ImageProxyGroup& owningGroup, RPathChain* prev)
{
    if ( _directDependentsSet )
        return;

    MachOParser thisParser(mh(), _dyldCacheIsRaw);
    dyld3::Platform thisPlatform = thisParser.platform();

    processRPaths(owningGroup);
    __block RPathChain rchain = { this, prev, _rpaths };

    thisParser.forEachDependentDylib(^(const char* loadPath, bool isWeak, bool isReExport, bool isUpward, uint32_t compatVersion, uint32_t curVersion, bool &stop) {
        if ( (loadPath[0] != '/') && (loadPath[0] != '@') ) {
            _diag.warning("load path is file system relative (%s) in %s", loadPath, runtimePath().c_str());
        }
        Diagnostics depDiag;
        ImageProxy* dep = owningGroup.findImage(depDiag, loadPath, isWeak, &rchain);
        if ( (dep == nullptr) || dep->invalid() ) {
            if (isWeak) {
                // weak link against a broken dylib, pretend dylib is not there
                dep = nullptr;
            } else {
                if ( depDiag.warnings().empty() ) {
                    if ( thisParser.header()->filetype == MH_EXECUTE )
                        _diag.error("required dylib '%s' not found", loadPath);
                    else
                        _diag.error("required dylib '%s' not found, needed by '%s'", loadPath, runtimePath().c_str());
                }
                else {
                    std::string allTries;
                    for (const std::string& warn : depDiag.warnings()) {
                        if ( allTries.empty() )
                            allTries = warn;
                        else
                            allTries = allTries + ", " + warn;
                    }
                    _diag.error("required dylib '%s' not found, needed by '%s'.  Did try: %s", loadPath, runtimePath().c_str(), allTries.c_str());
                }
            }
        }
        else {
            MachOParser depParser(dep->mh(), _dyldCacheIsRaw);
            if ( _diag.noError() ) {
                // verify found image has compatible version and matching platform
                dyld3::Platform depPlatform = depParser.platform();
                if ( depPlatform != thisPlatform ) {
                    // simulator allows a few macOS libSystem dylibs
                    if ( !inLibSystem() || !dep->inLibSystem() ) {
                        _diag.error("found '%s' but it was built for different platform '%s' than required '%s'.  Needed by '%s'", dep->runtimePath().c_str(),
                                    MachOParser::platformName(depPlatform).c_str(), MachOParser::platformName(thisPlatform).c_str(), runtimePath().c_str());
                    }
                }
            }
            if ( _diag.noError() ) {
                // verify compat version
                const char* installName;
                uint32_t    foundCompatVers;
                uint32_t    foundCurrentVers;
                if ( depParser.header()->filetype != MH_DYLIB ) {
                    _diag.error("found '%s' which is not a dylib.  Needed by '%s'", dep->runtimePath().c_str(), runtimePath().c_str());
                }
                else {
                    depParser.getDylibInstallName(&installName, &foundCompatVers, &foundCurrentVers);
                    if ( foundCompatVers < compatVersion ) {
                        _diag.error("found '%s' which has compat version (%s) which is less than required (%s).  Needed by '%s'", dep->runtimePath().c_str(),
                                    MachOParser::versionString(foundCompatVers).c_str(), MachOParser::versionString(compatVersion).c_str(), runtimePath().c_str());
                    }
                }
            }
        }
        if ( _diag.hasError() ) {
            stop = true;
            _invalid = true;
        }
        _dependents.push_back(dep);
        if ( isWeak )
            _dependentsKind.push_back(launch_cache::Image::LinkKind::weak);
        else if ( isReExport )
            _dependentsKind.push_back(launch_cache::Image::LinkKind::reExport);
        else if ( isUpward )
            _dependentsKind.push_back(launch_cache::Image::LinkKind::upward);
        else
            _dependentsKind.push_back(launch_cache::Image::LinkKind::regular);
    });
    _directDependentsSet = true;
}

bool ImageProxy::inLibSystem() const
{
    return startsWith(runtimePath(), "/usr/lib/system/") || startsWith(runtimePath(), "/usr/lib/libSystem.");
}

void ImageProxy::forEachDependent(void (^handler)(ImageProxy* dep, LinkKind)) const
{
    for (int i=0; i < _dependents.size(); ++i) {
        handler(_dependents[i], _dependentsKind[i]);
    }
}


bool ImageProxy::findExportedSymbol(Diagnostics& diag, const char* symbolName, MachOParser::FoundSymbol& foundInfo) const
{
    MachOParser parser(_mh, _dyldCacheIsRaw);
    return parser.findExportedSymbol(diag, symbolName, (void*)this, foundInfo, ^(uint32_t depIndex, const char* depLoadPath, void* extra, const mach_header** foundMH, void** foundExtra) {
        ImageProxy* proxy    = (ImageProxy*)extra;
        if ( depIndex < proxy->_dependents.size() ) {
            ImageProxy* depProxy = proxy->_dependents[depIndex];
            *foundMH    = depProxy->_mh;
            *foundExtra = (void*)depProxy;
            return true;
        }
        return false;
    });
}

bool ImageProxy::InitOrderInfo::beforeHas(ImageRef ref)
{
    ImageRef clearRef = ref;
    clearRef.clearKind();
    return ( std::find(initBefore.begin(), initBefore.end(), clearRef) != initBefore.end() );
}

bool ImageProxy::InitOrderInfo::upwardHas(ImageProxy* proxy)
{
    return ( std::find(danglingUpward.begin(), danglingUpward.end(), proxy) != danglingUpward.end() );
}

void ImageProxy::InitOrderInfo::removeRedundantUpwards()
{
    danglingUpward.erase(std::remove_if(danglingUpward.begin(), danglingUpward.end(),
                                        [&](ImageProxy* proxy) {
                                            ImageRef ref(0, proxy->_groupNum, proxy->_indexInGroup);
                                            return beforeHas(ref);
                                        }), danglingUpward.end());
}


//
// Every image has a list of "init-before" which means if that image was dlopen()ed
// here is the exact list of images to initialize in the exact order.  This makes
// the runtime easy.  It just walks the init-before list in order and runs each
// initializer if it has not already been run.
//
// The init-before list for each image is calculated based on the init-before list
// of each of its dependents.  It simply starts with the list of its first dependent,
// then appends the list of the next, removing entries already in the list, etc.
// Lastly if the current image has an initializer, it is appended to its init-before list.
//
// To handle cycles, when recursing to get a dependent's init-before list, any image
// whose list is still being calculated (cycle), just returns its list so far.
//
// Explicit upward links are handled in two parts.  First, in the algorithm described above,
// all upward links are ignored, which works fine as long as anything upward linked is
// downward linked at some point.  If not, it is called a "dangling upward link". Since
// nothing depends on those, they are added to the end of the final init-before list.
//

void ImageProxy::recursiveBuildInitBeforeInfo(ImageProxyGroup& owningGroup)
{
    if ( _initBeforesComputed )
        return;
    _initBeforesComputed = true; // break cycles

    if ( _imageBinaryData != nullptr ) {
        assert(_groupNum == 0);
        // if this is proxy for something in dyld cache, get its list from cache
        // and parse list into befores and upwards
        launch_cache::Image image(_imageBinaryData);
        image.forEachInitBefore(^(launch_cache::binary_format::ImageRef ref) {
            if ( (LinkKind)ref.kind() == LinkKind::upward ) {
                ImageProxyGroup* groupP = &owningGroup;
                while (groupP->_groupNum != 0)
                    groupP = groupP->_nextSearchGroup;
                launch_cache::ImageGroup dyldCacheGroup(groupP->_basedOn);
                launch_cache::Image      dyldCacheImage = dyldCacheGroup.image(ref.indexInGroup());
                Diagnostics diag;
                ImageProxy* p = groupP->findAbsoluteImage(diag, dyldCacheImage.path(), false, false);
                if ( diag.noError() )
                    _initBeforesInfo.danglingUpward.push_back(p);
            }
            else {
                _initBeforesInfo.initBefore.push_back(ref);
            }
        });
    }
    else {
        // calculate init-before list for this image by merging init-before's of all its dependent dylibs
        unsigned depIndex = 0;
        for (ImageProxy* depProxy : _dependents) {
            if ( depProxy == nullptr ) {
                assert(_dependentsKind[depIndex] == LinkKind::weak);
            }
            else {
                if ( _dependentsKind[depIndex] == LinkKind::upward ) {
                    // if this upward link is already in the list, we ignore it.  Otherwise add to front of list
                    if ( _initBeforesInfo.upwardHas(depProxy) ) {
                        // already in upward list, do nothing
                    }
                    else {
                        ImageRef ref(0, depProxy->_groupNum, depProxy->_indexInGroup);
                        if ( _initBeforesInfo.beforeHas(ref) ) {
                            // already in before list, do nothing
                        }
                        else {
                            // add to upward list
                            _initBeforesInfo.danglingUpward.push_back(depProxy);
                        }
                    }
                }
                else {
                    // compute init-befores of downward dependents
                    depProxy->recursiveBuildInitBeforeInfo(owningGroup);
                    // merge befores from this downward link into current befores list
                    for (ImageRef depInit : depProxy->_initBeforesInfo.initBefore) {
                        if ( !_initBeforesInfo.beforeHas(depInit) )
                            _initBeforesInfo.initBefore.push_back(depInit);
                    }
                    // merge upwards from this downward link into current befores list
                    for (ImageProxy* upProxy : depProxy->_initBeforesInfo.danglingUpward) {
                        ImageRef ref(0, upProxy->_groupNum, upProxy->_indexInGroup);
                        if ( _initBeforesInfo.beforeHas(ref) ) {
                            // already in current initBefore list, so ignore this upward
                        }
                        else if ( _initBeforesInfo.upwardHas(upProxy) ) {
                            // already in current danglingUpward list, so ignore this upward
                        }
                        else {
                            // append to current danglingUpward list
                            _initBeforesInfo.danglingUpward.push_back(upProxy);
                        }
                    }
                }
            }
            ++depIndex;
        }
        // eliminate any upward links added to befores list by some other dependent
        _initBeforesInfo.removeRedundantUpwards();

        // if this images has initializer(s) (or +load), add it to list
        MachOParser parser(_mh, _dyldCacheIsRaw);
        Diagnostics diag;
        if ( parser.hasInitializer(diag) || parser.hasPlusLoadMethod(diag) ) {
            launch_cache::binary_format::ImageRef ref(0, _groupNum, _indexInGroup);
            _initBeforesInfo.initBefore.push_back(ref);
        }

        //fprintf(stderr, "info for (%d, %d) %s\n",  _group, _index, _runtimePath.c_str());
        //for (ImageRef ref : _initBeforesInfo.initBefore)
        //     fprintf(stderr, "   ref = {%d, %d, %d}\n", ref.kind(), ref.group(), ref.index());
        //for (ImageProxy* p : _initBeforesInfo.danglingUpward)
        //     fprintf(stderr, "   up = %s\n", p->runtimePath().c_str());
    }
}

void ImageProxy::convertInitBeforeInfoToArray(ImageProxyGroup& owningGroup)
{
    if ( _initBeforesInfo.danglingUpward.empty() ) {
        _initBeforesArray = _initBeforesInfo.initBefore;
    }
    else {
        for (ImageRef ref : _initBeforesInfo.initBefore)
            _initBeforesArray.push_back(ref);
        bool inLibSys = inLibSystem();
        for (ImageProxy* proxy : _initBeforesInfo.danglingUpward) {
            // ignore upward dependendencies between stuff within libSystem.dylib
            if ( inLibSys && proxy->inLibSystem() )
                continue;
            proxy->getInitBeforeList(owningGroup);
            for (ImageRef depInit : proxy->_initBeforesInfo.initBefore) {
                if ( std::find(_initBeforesArray.begin(), _initBeforesArray.end(), depInit) == _initBeforesArray.end() )
                    _initBeforesArray.push_back(depInit);
            }
            ImageRef ref(0, proxy->_groupNum, proxy->_indexInGroup);
            if ( std::find(_initBeforesArray.begin(), _initBeforesArray.end(), ref) == _initBeforesArray.end() )
                _initBeforesArray.push_back(ref);
        }
    }
    //fprintf(stderr, "final init-before info for %s\n", _runtimePath.c_str());
    //for (ImageRef ref : _initBeforesArray) {
    //    fprintf(stderr, "   ref = {%d, %d, %d}\n", ref.linkKind, ref.group, ref.index);
    //}
}

const std::vector<ImageRef>& ImageProxy::getInitBeforeList(ImageProxyGroup& owningGroup)
{
    if ( !_initBeforesArraySet ) {
        _initBeforesArraySet = true; // break cycles
        recursiveBuildInitBeforeInfo(owningGroup);
        convertInitBeforeInfoToArray(owningGroup);
    }
    return _initBeforesArray;
}

ImageProxy::FixupInfo ImageProxy::buildFixups(Diagnostics& diag, uint64_t cacheUnslideBaseAddress, launch_cache::ImageGroupWriter& groupWriter) const
{
    __block ImageProxy::FixupInfo info;
    MachOParser image(_mh, _dyldCacheIsRaw);

    // add fixup for each rebase
    __block bool rebaseError = false;
    image.forEachRebase(diag, ^(uint32_t segIndex, uint64_t segOffset, uint8_t type, bool& stop) {
        dyld3::launch_cache::ImageGroupWriter::FixupType fixupType = launch_cache::ImageGroupWriter::FixupType::rebase;
        switch ( type ) {
            case REBASE_TYPE_POINTER:
                fixupType = launch_cache::ImageGroupWriter::FixupType::rebase;
                break;
            case REBASE_TYPE_TEXT_ABSOLUTE32:
                fixupType = launch_cache::ImageGroupWriter::FixupType::rebaseText;
                info.hasTextRelocs = true;
                break;
            case REBASE_TYPE_TEXT_PCREL32:
                diag.error("pcrel text rebasing not supported");
                stop = true;
                rebaseError = true;
                break;
            default:
                diag.error("unknown rebase type");
                stop = true;
                rebaseError = true;
                break;
        }
        info.fixups.push_back({segIndex, segOffset, fixupType, TargetSymbolValue::makeInvalid()});
        //fprintf(stderr, "rebase: segIndex=%d, segOffset=0x%0llX, type=%d\n", segIndex, segOffset, type);
    });
    if ( diag.hasError() )
        return FixupInfo();

    // add fixup for each bind
    image.forEachBind(diag, ^(uint32_t segIndex, uint64_t segOffset, uint8_t type, int libOrdinal,
                              uint64_t addend, const char* symbolName, bool weakImport, bool lazy, bool& stop) {
        launch_cache::ImageGroupWriter::FixupType fixupType;
        switch ( type ) {
            case BIND_TYPE_POINTER:
                if ( lazy )
                    fixupType = launch_cache::ImageGroupWriter::FixupType::pointerLazyBind;
                else
                    fixupType = launch_cache::ImageGroupWriter::FixupType::pointerBind;
                break;
            case BIND_TYPE_TEXT_ABSOLUTE32:
                fixupType = launch_cache::ImageGroupWriter::FixupType::bindText;
                info.hasTextRelocs = true;
                break;
            case BIND_TYPE_TEXT_PCREL32:
                fixupType = launch_cache::ImageGroupWriter::FixupType::bindTextRel;
                info.hasTextRelocs = true;
                break;
            case BIND_TYPE_IMPORT_JMP_REL32:
                fixupType = launch_cache::ImageGroupWriter::FixupType::bindImportJmpRel;
                break;
           default:
                diag.error("malformed executable, unknown bind type (%d)", type);
                stop = true;
                return;
        }
        const ImageProxy*    depProxy    = nullptr;
        bool                isWeakDylib = false;
        if ( libOrdinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE ) {
            // -bundle_loader symbols cannot be bound ahead of time, we must look them up at load time
            uint32_t imagePathPoolOffset   =  groupWriter.addString("@main");
            uint32_t imageSymbolPoolOffset =  groupWriter.addString(symbolName);
            info.fixups.push_back({segIndex, segOffset, fixupType, TargetSymbolValue::makeDynamicGroupValue(imagePathPoolOffset, imageSymbolPoolOffset, weakImport)});
            return;
        }
        else if ( libOrdinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP ) {
            // -dynamic_lookup symbols cannot be bound ahead of time, we must look them up at load time
            uint32_t imagePathPoolOffset   =  groupWriter.addString("@flat");
            uint32_t imageSymbolPoolOffset =  groupWriter.addString(symbolName);
            info.fixups.push_back({segIndex, segOffset, fixupType, TargetSymbolValue::makeDynamicGroupValue(imagePathPoolOffset, imageSymbolPoolOffset, weakImport)});
            return;
        }
        else if ( libOrdinal == BIND_SPECIAL_DYLIB_SELF ) {
            depProxy = this;
        }
        else if ( (libOrdinal >= 1) && (libOrdinal <= _dependents.size()) ) {
            isWeakDylib = (_dependentsKind[libOrdinal-1] == LinkKind::weak);
            depProxy    = _dependents[libOrdinal-1];
        }
        else {
            diag.error("ordinal %d not supported", libOrdinal);
            stop = true;
            return;
        }
        if ( depProxy != nullptr ) {
            MachOParser::FoundSymbol foundInfo;
            if ( depProxy->findExportedSymbol(diag, symbolName, foundInfo) ) {
                MachOParser implDylib(foundInfo.foundInDylib, _dyldCacheIsRaw);
                switch ( foundInfo.kind ) {
                    case MachOParser::FoundSymbol::Kind::headerOffset:
                    case MachOParser::FoundSymbol::Kind::resolverOffset:
                        if ( implDylib.inDyldCache() ) {
                            uint32_t cacheOffset = (uint32_t)(implDylib.preferredLoadAddress() + foundInfo.value - cacheUnslideBaseAddress + addend);
                            info.fixups.push_back({segIndex, segOffset, fixupType, TargetSymbolValue::makeSharedCacheOffset(cacheOffset)});
                        }
                        else {
                            ImageProxy* foundProxy   = (ImageProxy*)(foundInfo.foundExtra);
                            bool isIndirectGroupNum  = foundProxy->_groupNum >= 128;
                            uint32_t groupNum        = isIndirectGroupNum ? groupWriter.addIndirectGroupNum(foundProxy->_groupNum) : foundProxy->_groupNum;
                            info.fixups.push_back({segIndex, segOffset, fixupType, TargetSymbolValue::makeGroupValue(groupNum, foundProxy->_indexInGroup, foundInfo.value+addend, isIndirectGroupNum)});
                        }
                        break;
                   case MachOParser::FoundSymbol::Kind::absolute:
                        if (((((intptr_t)(foundInfo.value+addend)) << 2) >> 2) != (foundInfo.value+addend)) {
                            diag.error("absolute value %lld not supported", foundInfo.value+addend);
                            stop = true;
                            return;
                        }
                        info.fixups.push_back({segIndex, segOffset, fixupType, TargetSymbolValue::makeAbsolute(foundInfo.value+addend)});
                        break;
                }
            }
            else {
                if ( !weakImport ) {
                    diag.error("symbol '%s' not found, expected in '%s'", symbolName, depProxy->runtimePath().c_str());
                    stop = true;
                }
                // mark fixup needs to set fixup location to zero
                info.fixups.push_back({segIndex, segOffset, fixupType, TargetSymbolValue::makeAbsolute(0)});
            }
        }
        else {
            if ( isWeakDylib ) {
                // dylib not found and is weak, set pointers into it to zero
                info.fixups.push_back({segIndex, segOffset, fixupType, TargetSymbolValue::makeAbsolute(0)});
            }
            else {
                diag.error("dylib ordinal %d not found and not weak", libOrdinal);
                stop = true;
            }
         }
    });
    if ( diag.hasError() )
        return FixupInfo();

    uint32_t weakDefPathPoolOffset = groupWriter.addString("@weak_def");
    image.forEachWeakDef(diag, ^(bool strongDef, uint32_t segIndex, uint64_t segOffset, uint64_t addend, const char* symbolName, bool& stop) {
        if ( strongDef )
            return;
        // find fixup for that location and change it to be a @weakdef dynamic target
        bool altered = false;
        for (FixUp& fixup : info.fixups) {
            if ( (fixup.segOffset == segOffset) && (fixup.segIndex == segIndex) ) {
                uint32_t symbolPoolOffset =  groupWriter.addString(symbolName);
                fixup.type   = launch_cache::ImageGroupWriter::FixupType::pointerBind;
                fixup.target = TargetSymbolValue::makeDynamicGroupValue(weakDefPathPoolOffset, symbolPoolOffset, false);
                altered = true;
            }
        }
        if ( !altered ) {
            if ( image.isSlideable() ) {
                fprintf(stderr, "weak def for %s can't find underlying rebase/bind in %s\n", symbolName, runtimePath().c_str());
            }
            else {
                // no-pie executable does not have rebase for weak-def fixup, so add fixup
                uint32_t symbolPoolOffset =  groupWriter.addString(symbolName);
                info.fixups.push_back({segIndex, segOffset, launch_cache::ImageGroupWriter::FixupType::pointerBind, TargetSymbolValue::makeDynamicGroupValue(weakDefPathPoolOffset, symbolPoolOffset, false)} );
            }
        }

    });

    return info;
}


void ImageProxy::setOverrideOf(uint32_t groupNum, uint32_t indexInGroup)
{
    _overrideOf = ImageRef(0, groupNum, indexInGroup);
}


static bool alreadyInList(const std::vector<ImageProxy*>& imageList, ImageProxy* image)
{
    for (ImageProxy* proxy : imageList) {
        if ( proxy == image )
            return true;
    }
    return false;
}

void ImageProxy::addToFlatLookup(std::vector<ImageProxy*>& imageList)
{
    // add all images shallow
    bool addedSomething = false;
    for (ImageProxy* dep : _dependents) {
        if ( dep == nullptr )
            continue;
        if ( !alreadyInList(imageList, dep) ) {
            imageList.push_back(dep);
            addedSomething = true;
        }
    }
    // recurse
    if ( addedSomething ) {
        for (ImageProxy* dep : _dependents) {
            if ( dep == nullptr )
                continue;
            dep->addToFlatLookup(imageList);
        }
    }
}


///////////////////////////  ImageProxyGroup  ///////////////////////////


class StringPool
{
public:
    uint32_t            add(const std::string& str);
    size_t              size() const   { return _buffer.size(); }
    const char*         buffer() const { return &_buffer[0]; }
    void                align();
private:
    std::vector<char>                           _buffer;
    std::unordered_map<std::string, uint32_t>   _existingEntries;
};

uint32_t StringPool::add(const std::string& str)
{
    auto pos = _existingEntries.find(str);
    if ( pos != _existingEntries.end() )
        return pos->second;
    size_t len = str.size() + 1;
    size_t offset = _buffer.size();
    _buffer.insert(_buffer.end(), &str[0], &str[len]);
    _existingEntries[str] = (uint32_t)offset;
    assert(offset < 0xFFFF);
    return (uint32_t)offset;
}

void StringPool::align()
{
    while ( (_buffer.size() % 4) != 0 )
        _buffer.push_back('\0');
}

ImageProxyGroup::ImageProxyGroup(uint32_t groupNum, const DyldCacheParser& dyldCache, const launch_cache::binary_format::ImageGroup* basedOn,
                                 ImageProxyGroup* next, const std::string& mainProgRuntimePath,
                                 const std::vector<const BinaryImageGroupData*>& knownGroups,
                                 const std::vector<std::string>& buildTimePrefixes,
                                 const std::vector<std::string>& envVars,
                                 bool stubsEliminated, bool dylibsExpectedOnDisk, bool inodesAreSameAsRuntime)
    : _pathOverrides(envVars), _patchTable(nullptr), _basedOn(basedOn), _dyldCache(dyldCache), _nextSearchGroup(next), _groupNum(groupNum),
      _stubEliminated(stubsEliminated), _dylibsExpectedOnDisk(dylibsExpectedOnDisk), _inodesAreSameAsRuntime(inodesAreSameAsRuntime),
      _knownGroups(knownGroups), _buildTimePrefixes(buildTimePrefixes), _mainProgRuntimePath(mainProgRuntimePath), _platform(Platform::unknown)
{
    _archName = dyldCache.cacheHeader()->archName();
    _platform = (Platform)(dyldCache.cacheHeader()->platform());
}


ImageProxyGroup::~ImageProxyGroup()
{
    for (DyldSharedCache::MappedMachO& mapping : _ownedMappings ) {
        vm_deallocate(mach_task_self(), (vm_address_t)mapping.mh, mapping.length);
    }
    for (ImageProxy* proxy : _images) {
        delete proxy;
    }
}


std::string ImageProxyGroup::normalizedPath(const std::string& path)
{
    for (const std::string& prefix : _buildTimePrefixes) {
        std::string fullPath = prefix + path;
        if ( fileExists(fullPath) ) {
            if ( (fullPath.find("/../") != std::string::npos) || (fullPath.find("//") != std::string::npos) || (fullPath.find("/./") != std::string::npos) ) {
                char resolvedPath[PATH_MAX];
                if ( realpath(fullPath.c_str(), resolvedPath) != nullptr ) {
                    std::string resolvedUnPrefixed = &resolvedPath[prefix.size()];
                    return resolvedUnPrefixed;
                }
            }
            break;
        }
    }
    return path;
}


ImageProxy* ImageProxyGroup::findImage(Diagnostics& diag, const std::string& runtimeLoadPath, bool canBeMissing, ImageProxy::RPathChain* rChain)
{
    __block ImageProxy* result = nullptr;
    _pathOverrides.forEachPathVariant(runtimeLoadPath.c_str(), _platform, ^(const char* possiblePath, bool& stop) {
        if ( startsWith(possiblePath, "@rpath/") ) {
            std::string trailing = &possiblePath[6];
            for (const ImageProxy::RPathChain* cur=rChain; cur != nullptr; cur = cur->prev) {
                for (const std::string& rpath : cur->rpaths) {
                    std::string aPath = rpath + trailing;
                    result = findAbsoluteImage(diag, aPath, true, false);
                    if ( result != nullptr ) {
                        _pathToProxy[runtimeLoadPath] = result;
                        stop = true;
                        return;
                    }
                }
            }
            // if cannot be found via current stack of rpaths, check if already found
            auto pos = _pathToProxy.find(possiblePath);
            if ( pos != _pathToProxy.end() ) {
                result = pos->second;
                stop = true;
                return;
            }
        }
        else if ( startsWith(possiblePath, "@loader_path/") ) {
            std::string loaderFile = rChain->inProxy->runtimePath();
            size_t lastSlash = loaderFile.rfind('/');
            if ( lastSlash != std::string::npos ) {
                std::string loaderDir = loaderFile.substr(0, lastSlash);
                std::string newPath = loaderDir + &possiblePath[12];
                result = findAbsoluteImage(diag, newPath, canBeMissing, false);
                if ( result != nullptr ) {
                    _pathToProxy[runtimeLoadPath] = result;
                    stop = true;
                    return;
                }
            }
        }
        else if ( startsWith(possiblePath, "@executable_path/") ) {
            for (const ImageProxy::RPathChain* cur=rChain; cur != nullptr; cur = cur->prev) {
                if ( cur->inProxy->mh()->filetype == MH_EXECUTE ) {
                    std::string mainProg = cur->inProxy->runtimePath();
                    size_t lastSlash = mainProg.rfind('/');
                    if ( lastSlash != std::string::npos ) {
                        std::string mainDir = mainProg.substr(0, lastSlash);
                        std::string newPath = mainDir + &possiblePath[16];
                        result = findAbsoluteImage(diag, newPath, canBeMissing, false);
                        if ( result != nullptr ) {
                            _pathToProxy[runtimeLoadPath] = result;
                            stop = true;
                            return;
                        }
                    }
                }
            }
        }
        else {
            // load command is full path to dylib
            result = findAbsoluteImage(diag, possiblePath, canBeMissing, false);
            if ( result != nullptr ) {
                stop = true;
                return;
            }
        }
    });

    // when building closure, check if an added dylib is an override for something in the cache
    if ( (result != nullptr) && (_groupNum > 1) && !result->isProxyForCachedDylib() ) {
        for (ImageProxyGroup* grp = this; grp != nullptr; grp = grp->_nextSearchGroup) {
            if ( grp->_basedOn == nullptr )
                continue;
            uint32_t indexInGroup;
            launch_cache::ImageGroup imageGroup(grp->_basedOn);
            if ( imageGroup.findImageByPath(runtimeLoadPath.c_str(), indexInGroup) ) {
                result->setOverrideOf(imageGroup.groupNum(), indexInGroup);
                break;
            }
        }
    }

    return result;
}


bool ImageProxyGroup::builtImageStillValid(const launch_cache::Image& image)
{
    // only do checks when running on system
    if ( _buildTimePrefixes.size() != 1 )
        return true;
    if ( _buildTimePrefixes.front().size() != 0 )
        return true;
    if ( _platform != MachOParser::currentPlatform() )
        return true;

    struct stat statBuf;
    bool expectedOnDisk   = image.group().dylibsExpectedOnDisk();
    bool overridableDylib = image.overridableDylib();
    bool cachedDylib      = !image.isDiskImage();
    bool fileFound        = ( ::stat(image.path(), &statBuf) == 0 );

    if ( cachedDylib ) {
        if ( expectedOnDisk ) {
            if ( fileFound ) {
                // macOS case: verify dylib file info matches what it was when cache was built
                return ( (image.fileModTime() == statBuf.st_mtime) && (image.fileINode() == statBuf.st_ino) );
            }
            else {
                // macOS case: dylib missing
                return false;
            }
        }
        else {
            if ( fileFound ) {
                if ( overridableDylib ) {
                    // iOS case: internal install with dylib root
                    return false;
                }
                else {
                    // iOS case: customer install, ignore dylib on disk
                    return true;
                }
            }
            else {
                // iOS case: cached dylib not on disk as expected
                return true;
            }
        }
    }
    else {
        if ( fileFound ) {
            if ( image.validateUsingModTimeAndInode() ) {
                // macOS case: verify dylib file info matches what it was when cache was built
                return ( (image.fileModTime() == statBuf.st_mtime) && (image.fileINode() == statBuf.st_ino) );
            }
            else {
                // FIXME: need to verify file cdhash
                return true;
            }
        }
        else {
            // dylib not on disk as expected
            return false;
        }
    }
}

ImageProxy* ImageProxyGroup::findAbsoluteImage(Diagnostics& diag, const std::string& runtimeLoadPath, bool canBeMissing, bool makeErrorMessage, bool pathIsAlreadyReal)
{
    auto pos = _pathToProxy.find(runtimeLoadPath);
    if ( pos != _pathToProxy.end() )
        return pos->second;

    // see if this ImageProxyGroup is a proxy for an ImageGroup from the dyld shared cache
    if ( _basedOn != nullptr ) {
        uint32_t foundIndex;
        launch_cache::ImageGroup imageGroup(_basedOn);
        if ( imageGroup.findImageByPath(runtimeLoadPath.c_str(), foundIndex) ) {
            launch_cache::Image image = imageGroup.image(foundIndex);
            if ( builtImageStillValid(image) ) {
                ImageProxy* proxy = nullptr;
                if ( _groupNum == 0 ) {
                    const mach_header* mh = (mach_header*)((uint8_t*)(_dyldCache.cacheHeader()) + image.cacheOffset());
                    proxy = new ImageProxy(mh, image.binaryData(), foundIndex, _dyldCache.cacheIsMappedRaw());
                }
                else {
                    DyldSharedCache::MappedMachO* mapping = addMappingIfValidMachO(diag, runtimeLoadPath);
                    if ( mapping != nullptr ) {
                        proxy = new ImageProxy(*mapping, _groupNum, foundIndex, false);
                    }
                }
                if ( proxy != nullptr ) {
                    _pathToProxy[runtimeLoadPath] = proxy;
                    _images.push_back(proxy);
                    if ( runtimeLoadPath != image.path() ) {
                        // lookup path is an alias, add real path too
                        _pathToProxy[image.path()] = proxy;
                    }
                    return proxy;
                }
            }
        }
    }

    if ( _nextSearchGroup != nullptr ) {
        ImageProxy* result = _nextSearchGroup->findAbsoluteImage(diag, runtimeLoadPath, true, false);
        if ( result != nullptr )
            return result;
    }

    // see if this is a symlink to a dylib
    if ( !pathIsAlreadyReal ) {
        for (const std::string& prefix : _buildTimePrefixes) {
            std::string fullPath = prefix + runtimeLoadPath;
            if ( endsWith(prefix, "/") )
                fullPath = prefix.substr(0, prefix.size()-1) + runtimeLoadPath;
            if ( fileExists(fullPath) ) {
                std::string resolvedPath = realFilePath(fullPath);
                if ( !resolvedPath.empty() && (resolvedPath!= fullPath) ) {
                    std::string resolvedRuntimePath = resolvedPath.substr(prefix.size());
                    ImageProxy* proxy = findAbsoluteImage(diag, resolvedRuntimePath, true, false, true);
                    if ( proxy != nullptr )
                        return proxy;
                }
            }
        }
    }

    if ( (_groupNum >= 2) && (_basedOn == nullptr) ) {
        if ( (runtimeLoadPath[0] != '/') && (runtimeLoadPath[0] != '@') ) {
            for (ImageProxy* aProxy : _images) {
                if ( endsWith(aProxy->runtimePath(), runtimeLoadPath) ) {
                    aProxy->setCwdMustBeThisDir();
                    return aProxy;
                }
            }
        }

        DyldSharedCache::MappedMachO* mapping = addMappingIfValidMachO(diag, runtimeLoadPath);
        if ( mapping != nullptr ) {
            ImageProxy* proxy = new ImageProxy(*mapping, _groupNum, (uint32_t)_images.size(), false);
            _pathToProxy[runtimeLoadPath] = proxy;
            _images.push_back(proxy);
            return proxy;
        }
    }

    if ( !canBeMissing && makeErrorMessage ) {
        if ( diag.warnings().empty() ) {
            if ( diag.hasError() ) {
                std::string orgMsg = diag.errorMessage();
                diag.error("'%s' %s", runtimeLoadPath.c_str(), orgMsg.c_str());
            }
            else {
                diag.error("could not find '%s'", runtimeLoadPath.c_str());
            }
        }
        else {
            std::string allTries;
            for (const std::string& warn : diag.warnings()) {
                if ( allTries.empty() )
                    allTries = warn;
                else
                    allTries = allTries + ", " + warn;
            }
            diag.clearWarnings();
            diag.error("could not use '%s'. Did try: %s", runtimeLoadPath.c_str(), allTries.c_str());
        }
    }

    // record locations not found so it can be verified they are still missing at runtime
    _mustBeMissingFiles.insert(runtimeLoadPath);

    return nullptr;
}


DyldSharedCache::MappedMachO* ImageProxyGroup::addMappingIfValidMachO(Diagnostics& diag, const std::string& runtimePath, bool ignoreMainExecutables)
{
    bool fileFound = false;
    for (const std::string& prefix : _buildTimePrefixes) {
        std::string fullPath = prefix + runtimePath;
        struct stat statBuf;
        if ( stat(fullPath.c_str(), &statBuf) != 0 )
            continue;
        fileFound = true;
        // map whole file and determine if it is mach-o or a fat file
        int fd = ::open(fullPath.c_str(), O_RDONLY);
        if ( fd < 0 ) {
            diag.warning("file not open()able '%s' errno=%d", fullPath.c_str(), errno);
            continue;
        }
        size_t len = (size_t)statBuf.st_size;
        size_t offset = 0;
        const void* p = ::mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
        if ( p != MAP_FAILED ) {
            size_t sliceLen;
            size_t sliceOffset;
            bool missingSlice;
            Diagnostics fatDiag;
            if ( FatUtil::isFatFileWithSlice(fatDiag, p, len, _archName, sliceOffset, sliceLen, missingSlice) ) {
                // unmap whole file
                ::munmap((void*)p, len);
                // remap just slice
                p = ::mmap(NULL, sliceLen, PROT_READ, MAP_PRIVATE, fd, sliceOffset);
                if ( p != MAP_FAILED ) {
                    offset = sliceOffset;
                    len    = sliceLen;
                }
            }
            else if ( fatDiag.hasError() ) {
                diag.warning("%s", fatDiag.errorMessage().c_str());
            }
            if ( (p != MAP_FAILED) && !missingSlice && MachOParser::isValidMachO(diag, _archName, _platform, p, len, fullPath, ignoreMainExecutables) ) {
                bool issetuid = (statBuf.st_mode & (S_ISUID|S_ISGID));
                bool sip = false; // FIXME
                _ownedMappings.emplace_back(runtimePath, (mach_header*)p, len, issetuid, sip, offset, statBuf.st_mtime, statBuf.st_ino);
                ::close(fd);
                return &_ownedMappings.back();
            }
            else if (p != MAP_FAILED) {
                ::munmap((void*)p, len);
            }
        }
        ::close(fd);
    }
    if ( !fileFound )
        diag.warning("file not found '%s'", runtimePath.c_str());

    return nullptr;
}

static bool dontExamineDir(const std::string& dirPath)
{
    return endsWith(dirPath, ".app") || endsWith(dirPath, ".xctoolchain") || endsWith(dirPath, ".sdk") || endsWith(dirPath, ".platform");
}

void ImageProxyGroup::addExtraMachOsInBundle(const std::string& appDir)
{
    iterateDirectoryTree("", appDir, ^(const std::string& dirPath) { return dontExamineDir(dirPath); }, ^(const std::string& path, const struct stat& statBuf) {
        // ignore files that don't have 'x' bit set (all runnable mach-o files do)
        const bool hasXBit = ((statBuf.st_mode & S_IXOTH) == S_IXOTH);
        if ( !hasXBit )
            return;

        // ignore files too small
        if ( statBuf.st_size < 0x1000 )
            return;

        // if the file is mach-o, add to list
        if ( _pathToProxy.find(path) == _pathToProxy.end() ) {
            Diagnostics  machoDiag;
            DyldSharedCache::MappedMachO* mapping = addMappingIfValidMachO(machoDiag, path, true);
            if ( mapping != nullptr ) {
                ImageProxy* proxy = new ImageProxy(*mapping, _groupNum, (uint32_t)_images.size(), false);
                if ( proxy != nullptr ) {
                    _pathToProxy[path] = proxy;
                    _images.push_back(proxy);
                }
            }
        }
    });
}

// used when building dyld shared cache
ImageProxyGroup* ImageProxyGroup::makeDyldCacheDylibsGroup(Diagnostics& diag, const DyldCacheParser& dyldCache,
                                                           const std::vector<DyldSharedCache::MappedMachO>& cachedDylibs,
                                                           const std::vector<std::string>& buildTimePrefixes,
                                                           const PatchTable& patchTable, bool stubEliminated, bool dylibsExpectedOnDisk)
{
    std::vector<std::string> emptyEnvVars; // Note: this method only used when constructing dyld cache where envs are not used
    std::vector<const BinaryImageGroupData*> noExistingGroups;
    ImageProxyGroup* groupProxy = new ImageProxyGroup(0, dyldCache, nullptr, nullptr, "", noExistingGroups, buildTimePrefixes, emptyEnvVars, stubEliminated, dylibsExpectedOnDisk);
    groupProxy->_patchTable = &patchTable;

    // add every dylib in shared cache to _images
    uint32_t indexInGroup = 0;
    for (const DyldSharedCache::MappedMachO& mapping : cachedDylibs) {
        ImageProxy* proxy = new ImageProxy(mapping, 0, indexInGroup++, true);
        groupProxy->_images.push_back(proxy);
        groupProxy->_pathToProxy[mapping.runtimePath] = proxy;
    }

    // verify libdyld is compatible
    ImageRef libdyldEntryImageRef = ImageRef::makeEmptyImageRef();
    uint32_t libdyldEntryOffset;
    groupProxy->findLibdyldEntry(diag, libdyldEntryImageRef, libdyldEntryOffset);
    if ( diag.hasError() ) {
        delete groupProxy;
        return nullptr;
    }

    // wire up dependents
    bool hadError = false;
    for (size_t i=0; i < groupProxy->_images.size(); ++i) {
        // note: addDependentsShallow() can append to _images, so can't use regular iterator
        ImageProxy* proxy = groupProxy->_images[i];
        proxy->addDependentsShallow(*groupProxy);
        if ( proxy->diagnostics().hasError() ) {
            hadError = true;
            diag.copy(proxy->diagnostics());
            break;
        }
    }

    if ( hadError ) {
        delete groupProxy;
        return nullptr;
    }

    return groupProxy;
}


// used when building dyld shared cache
ImageProxyGroup* ImageProxyGroup::makeOtherOsGroup(Diagnostics& diag, const DyldCacheParser& dyldCache, ImageProxyGroup* cachedDylibsGroup,
                                                   const std::vector<DyldSharedCache::MappedMachO>& otherDylibsAndBundles,
                                                   bool inodesAreSameAsRuntime, const std::vector<std::string>& buildTimePrefixes)
{
    std::vector<std::string> emptyEnvVars; // Note: this method only used when constructing dyld cache where envs are not used
    const BinaryImageGroupData* cachedDylibsGroupData = dyldCache.cachedDylibsGroup();
    std::vector<const BinaryImageGroupData*> existingGroups = { cachedDylibsGroupData };
    ImageProxyGroup          dyldCacheDylibProxyGroup(0, dyldCache, cachedDylibsGroupData, nullptr,           "", existingGroups, buildTimePrefixes, emptyEnvVars);
    ImageProxyGroup* groupProxy = new ImageProxyGroup(1, dyldCache, nullptr,               cachedDylibsGroup, "", existingGroups, buildTimePrefixes, emptyEnvVars,
                                                      false, true, inodesAreSameAsRuntime);

    // add every dylib/bundle in "other: list to _images
    uint32_t indexInGroup = 0;
    for (const DyldSharedCache::MappedMachO& mapping : otherDylibsAndBundles) {
        ImageProxy* proxy = new ImageProxy(mapping, 1, indexInGroup++, false);
        groupProxy->_images.push_back(proxy);
        groupProxy->_pathToProxy[mapping.runtimePath] = proxy;
    }

    // wire up dependents
    for (size_t i=0; i < groupProxy->_images.size(); ++i) {
        // note: addDependentsShallow() can append to _images, so can't use regular iterator
        ImageProxy* proxy = groupProxy->_images[i];
        // note: other-dylibs can only depend on dylibs in this group or group 0, so no need for deep dependents
        proxy->addDependentsShallow(*groupProxy);
        if ( proxy->diagnostics().hasError() ) {
            diag.warning("adding dependents to %s: %s", proxy->runtimePath().c_str(), proxy->diagnostics().errorMessage().c_str());
            proxy->markInvalid();
        }
    }
    // propagate invalidness
    __block bool somethingInvalid;
    do {
        somethingInvalid = false;
        for (ImageProxy* proxy : groupProxy->_images) {
            proxy->forEachDependent(^(ImageProxy* dep, LinkKind) {
                if ( (dep != nullptr) && dep->invalid() && !proxy->invalid()) {
                    proxy->markInvalid();
                    somethingInvalid = true;
                }
            });
        }
    } while (somethingInvalid);

    return groupProxy;
}

// used by closured for dlopen of unknown dylibs
const BinaryImageGroupData* ImageProxyGroup::makeDlopenGroup(Diagnostics& diag, const DyldCacheParser& dyldCache, uint32_t groupNum,
                                                             const std::vector<const BinaryImageGroupData*>& existingGroups,
                                                             const std::string& imagePath, const std::vector<std::string>& envVars)
{
    const std::vector<std::string>& noBuildTimePrefixes = {""};
    ImageProxyGroup dyldCacheDylibProxyGroup(0, dyldCache, existingGroups[0], nullptr,                   "",        existingGroups, noBuildTimePrefixes, envVars);
    ImageProxyGroup dyldCacheOtherProxyGroup(1, dyldCache, nullptr,           &dyldCacheDylibProxyGroup, "",        existingGroups, noBuildTimePrefixes, envVars);
    ImageProxyGroup dlopenGroupProxy(groupNum,  dyldCache, nullptr,           &dyldCacheOtherProxyGroup, imagePath, existingGroups, noBuildTimePrefixes, envVars, false, true, true);

    DyldSharedCache::MappedMachO* topMapping = dlopenGroupProxy.addMappingIfValidMachO(diag, imagePath, true);
    if ( topMapping == nullptr ) {
        if ( diag.noError() ) {
            const std::set<std::string>& warnings = diag.warnings();
            if ( warnings.empty() )
                diag.error("no loadable mach-o in %s", imagePath.c_str());
            else
                diag.error("%s", (*warnings.begin()).c_str());
        }
        return nullptr;
    }

    ImageProxy* topImageProxy = new ImageProxy(*topMapping, groupNum, 0, false);
    if ( topImageProxy == nullptr ) {
        diag.error("can't find slice matching dyld cache in %s", imagePath.c_str());
        return nullptr;
    }
    dlopenGroupProxy._images.push_back(topImageProxy);
    dlopenGroupProxy._pathToProxy[imagePath] = topImageProxy;

    // add all dylibs needed by dylib and are not in dyld cache
    topImageProxy->addDependentsDeep(dlopenGroupProxy, nullptr, false);
    if ( topImageProxy->diagnostics().hasError() ) {
        diag.copy(topImageProxy->diagnostics());
        return nullptr;
    }

    const BinaryImageGroupData* result = dlopenGroupProxy.makeImageGroupBinary(diag);

    return result;
}


// used when building dyld shared cache
BinaryClosureData* ImageProxyGroup::makeClosure(Diagnostics& diag, const DyldCacheParser& dyldCache, ImageProxyGroup* cachedDylibsGroup,
                                                ImageProxyGroup* otherOsDylibs, const DyldSharedCache::MappedMachO& mainProgMapping,
                                                bool inodesAreSameAsRuntime, const std::vector<std::string>& buildTimePrefixes)
{
    // _basedOn can not be set until ImageGroup is built
    if ( cachedDylibsGroup->_basedOn == nullptr ) {
        cachedDylibsGroup->_basedOn = dyldCache.cachedDylibsGroup();
    }
    const BinaryImageGroupData* cachedDylibsGroupData = dyldCache.cachedDylibsGroup();
    const BinaryImageGroupData* otherDylibsGroupData = dyldCache.otherDylibsGroup();
    std::vector<const BinaryImageGroupData*> existingGroups = { cachedDylibsGroupData, otherDylibsGroupData };
    std::vector<std::string> emptyEnvVars; // Note: this method only used when constructing dyld cache where envs are not used
    ImageProxyGroup mainClosureGroupProxy(2, dyldCache, nullptr, otherOsDylibs, mainProgMapping.runtimePath, existingGroups, buildTimePrefixes,
                                          emptyEnvVars, false, true, inodesAreSameAsRuntime);

    ImageProxy* mainProxy = new ImageProxy(mainProgMapping, 2, 0, false);
    if ( mainProxy == nullptr ) {
        diag.error("can't find slice matching dyld cache in %s", mainProgMapping.runtimePath.c_str());
        return nullptr;
    }
    mainClosureGroupProxy._images.push_back(mainProxy);
    mainClosureGroupProxy._pathToProxy[mainProgMapping.runtimePath] = mainProxy;

    return mainClosureGroupProxy.makeClosureBinary(diag, mainProxy, false);
}


bool ImageProxyGroup::addInsertedDylibs(Diagnostics& diag)
{
    __block bool success = true;
    _pathOverrides.forEachInsertedDylib(^(const char* dylibPath) {
        ImageProxy* insertProxy = findAbsoluteImage(diag, dylibPath, false, true);
        if ( insertProxy == nullptr )
            success = false;
    });
    return success;
}

static DyldCacheParser findDyldCache(Diagnostics& diag, const ClosureBuffer::CacheIdent& cacheIdent, task_t requestor, bool* dealloc)
{
    *dealloc = false;
#if !defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101300)
    size_t currentCacheSize;
    const DyldSharedCache* currentCache = (const DyldSharedCache*)_dyld_get_shared_cache_range(&currentCacheSize);
    if ( currentCache != nullptr ) {
        uuid_t currentCacheUUID;
        currentCache->getUUID(currentCacheUUID);
        if ( memcmp(currentCacheUUID, cacheIdent.cacheUUID, 16) == 0 ) 
            return DyldCacheParser((const DyldSharedCache*)currentCache, false);
    }
#endif
    if ( requestor == mach_task_self() ) {
        // handle dyld_closure_util case where -cache_file option maps raw cache file into this process
        const DyldSharedCache* altCache = (DyldSharedCache*)cacheIdent.cacheAddress;
        uuid_t altCacheUUID;
        altCache->getUUID(altCacheUUID);
        if ( memcmp(altCacheUUID, cacheIdent.cacheUUID, 16) == 0 )
            return DyldCacheParser(altCache, true); // only one cache can be mapped into process, so this must be raw
        else
            diag.error("dyld cache uuid has changed");
    }
#if BUILDING_CLOSURED
    else {
        // handle case where requestor to closured is running with a different dyld cache that closured
        uint8_t cacheBuffer[4096];
        mach_vm_size_t actualReadSize = sizeof(cacheBuffer);
        kern_return_t r;
        r = mach_vm_read_overwrite(requestor, cacheIdent.cacheAddress, sizeof(cacheBuffer), (vm_address_t)&cacheBuffer, &actualReadSize);
        if ( r != KERN_SUCCESS ) {
            diag.error("unable to read cache header from requesting process (addr=0x%llX), kern err=%d", cacheIdent.cacheAddress, r);
            return DyldCacheParser(nullptr, false);
        }
        const dyld_cache_header* header = (dyld_cache_header*)cacheBuffer;
        const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)(cacheBuffer + header->mappingOffset);
        vm_address_t bufferAddress = 0;
        r = vm_allocate(mach_task_self(), &bufferAddress, (long)cacheIdent.cacheMappedSize, VM_FLAGS_ANYWHERE);
        if ( r != KERN_SUCCESS ) {
            diag.error("unable to allocate space to copy custom dyld cache (size=0x%llX), kern err=%d", cacheIdent.cacheMappedSize, r);
            return DyldCacheParser(nullptr, false);
        }
        uint64_t slide =  cacheIdent.cacheAddress - mappings[0].address;
        for (int i=0; i < 3; ++i) {
            mach_vm_address_t mappedAddress = bufferAddress + (mappings[i].address - mappings[0].address);
            mach_vm_size_t    mappedSize    = mappings[i].size;
            vm_prot_t         curProt       = VM_PROT_READ;
            vm_prot_t         maxProt       = VM_PROT_READ;
            r = mach_vm_remap(mach_task_self(), &mappedAddress, mappedSize, 0, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,
                              requestor, mappings[i].address+slide, true, &curProt, &maxProt, VM_INHERIT_NONE);
            if ( r != KERN_SUCCESS ) {
                 diag.error("unable to mach_vm_remap region %d custom dyld cache (request addr=0x%llX, size=0x%llX), kern err=%d, localBuffer=0x%lX, localMapTarget=0x%llX",
                           i, mappings[i].address+slide, mappedSize, r, (long)bufferAddress, mappedAddress);
                 return DyldCacheParser(nullptr, false);
            }
            if ( curProt != VM_PROT_READ )
                vm_protect(mach_task_self(), (long)mappedAddress, (long)mappedSize, false, VM_PROT_READ);
       }
       *dealloc = true;
       return DyldCacheParser((DyldSharedCache*)bufferAddress, false);  // assumes cache in other process is mapped as three regions
    }
#endif
    return DyldCacheParser(nullptr, false);
}

BinaryClosureData* ImageProxyGroup::makeClosure(Diagnostics& diag, const ClosureBuffer& buffer, task_t requestor, const std::vector<std::string>& buildTimePrefixes)
{
    // unpack buffer
    bool deallocCacheCopy;
    DyldCacheParser dyldCache = findDyldCache(diag, buffer.cacheIndent(), requestor, &deallocCacheCopy);
    if ( diag.hasError() )
        return nullptr;
    const char* mainProg = buffer.targetPath();
    std::vector<std::string> envVars;
    int envCount = buffer.envVarCount();
    const char* envVarCStrings[envCount];
    buffer.copyImageGroups(envVarCStrings);
    for (int i=0; i < envCount; ++i) {
        envVars.push_back(envVarCStrings[i]);
    }

    // make ImageProxyGroups: 0, 1, 2
    const BinaryImageGroupData* cachedDylibsGroupData = dyldCache.cachedDylibsGroup();
    const BinaryImageGroupData* otherDylibsGroupData  = dyldCache.otherDylibsGroup();
    std::vector<std::string> realBuildTimePrefixes;
    for (const std::string& prefix : buildTimePrefixes)  {
        char resolvedPath[PATH_MAX];
        if ( realpath(prefix.c_str(), resolvedPath) != nullptr )
            realBuildTimePrefixes.push_back(resolvedPath);
        else
            realBuildTimePrefixes.push_back(prefix);
    }
    std::vector<const BinaryImageGroupData*> existingGroups = { cachedDylibsGroupData, otherDylibsGroupData };
    ImageProxyGroup dyldCacheDylibProxyGroup(0, dyldCache, cachedDylibsGroupData, nullptr,                   "",       existingGroups, realBuildTimePrefixes, envVars);
    ImageProxyGroup dyldCacheOtherProxyGroup(1, dyldCache, otherDylibsGroupData,  &dyldCacheDylibProxyGroup, "",       existingGroups, realBuildTimePrefixes, envVars);
    ImageProxyGroup mainClosureGroupProxy(   2, dyldCache, nullptr,               &dyldCacheOtherProxyGroup, mainProg, existingGroups, realBuildTimePrefixes, envVars, false, true, true);

    // add any DYLD_INSERTED_LIBRARIES then main program into closure
    BinaryClosureData* result = nullptr;
    if ( mainClosureGroupProxy.addInsertedDylibs(diag) ) {
        ImageProxy* proxy = mainClosureGroupProxy.findAbsoluteImage(diag, mainProg, false, true);
        if ( proxy != nullptr ) {
            // build closure
            result = mainClosureGroupProxy.makeClosureBinary(diag, proxy, false);
        }
    }

    // if client has a different cache, unmap our copy
    if ( deallocCacheCopy )
        vm_deallocate(mach_task_self(), (vm_address_t)dyldCache.cacheHeader(), (long)buffer.cacheIndent().cacheMappedSize);

    return result;
}

ClosureBuffer closured_CreateImageGroup(const ClosureBuffer& input)
{
    Diagnostics diag;
    const BinaryImageGroupData* newGroup = ImageProxyGroup::makeDlopenGroup(diag, input, mach_task_self(), {""});

    if ( diag.noError() ) {
        // on success return the ImageGroup binary in the ClosureBuffer
        dyld3::ClosureBuffer result(newGroup);
        free((void*)newGroup);
        return result;
    }
    else {
        // on failure return the error message in the ClosureBuffer
        dyld3::ClosureBuffer err(diag.errorMessage().c_str());
        return err;
    }
}

const BinaryImageGroupData* ImageProxyGroup::makeDlopenGroup(Diagnostics& diag, const ClosureBuffer& buffer, task_t requestor, const std::vector<std::string>& buildTimePrefixes)
{
    // unpack buffer
    bool deallocCacheCopy;
    DyldCacheParser dyldCache = findDyldCache(diag, buffer.cacheIndent(), requestor, &deallocCacheCopy);
    if ( diag.hasError() )
        return nullptr;

    const char* targetDylib = buffer.targetPath();
    std::vector<std::string> envVars;
    int envCount = buffer.envVarCount();
    const char* envVarCStrings[envCount];
    buffer.copyImageGroups(envVarCStrings);
    for (int i=0; i < envCount; ++i) {
        envVars.push_back(envVarCStrings[i]);
    }
    uint32_t groupCount = buffer.imageGroupCount() + 2;
    const launch_cache::BinaryImageGroupData* groupDataPtrs[groupCount];
    groupDataPtrs[0] = dyldCache.cachedDylibsGroup();
    groupDataPtrs[1] = dyldCache.otherDylibsGroup();
    buffer.copyImageGroups(&groupDataPtrs[2]);

    // build an ImageProxyGroup for each existing group, and one for new group being constructed
    std::vector<const launch_cache::BinaryImageGroupData*> existingGroups;
    std::vector<std::unique_ptr<ImageProxyGroup>> proxies;
    ImageProxyGroup* prevProxy = nullptr;
    for (uint32_t i=0; i < groupCount; ++i) {
        const launch_cache::BinaryImageGroupData* groupData = groupDataPtrs[i];
        existingGroups.push_back(groupData);
        launch_cache::ImageGroup group(groupData);
        uint32_t groupNum = group.groupNum();
        assert(groupNum == proxies.size());
        proxies.emplace_back(new ImageProxyGroup(groupNum, dyldCache, groupData, prevProxy, "", existingGroups, buildTimePrefixes, envVars));
        prevProxy = proxies.back().get();
    }
    ImageProxyGroup dlopenGroupProxy(groupCount, dyldCache, nullptr, prevProxy, targetDylib, existingGroups, buildTimePrefixes, envVars);

    // find and mmap() top level dylib
    DyldSharedCache::MappedMachO* topMapping = dlopenGroupProxy.addMappingIfValidMachO(diag, targetDylib, true);
    if ( topMapping == nullptr ) {
        std::string allWarnings;
        for (const std::string& warn : diag.warnings()) {
            if ( allWarnings.empty() )
                allWarnings = warn;
            else
                allWarnings = allWarnings + ", " + warn;
        }
        diag.clearWarnings();
        diag.error("%s", allWarnings.c_str());
        if ( deallocCacheCopy )
            vm_deallocate(mach_task_self(), (vm_address_t)dyldCache.cacheHeader(), (long)buffer.cacheIndent().cacheMappedSize);
        return nullptr;
    }

    // make ImageProxy for top level dylib
    ImageProxy* topImageProxy = new ImageProxy(*topMapping, groupCount, 0, false);
    if ( topImageProxy == nullptr ) {
        diag.error("can't find slice matching dyld cache in %s", targetDylib);
        if ( deallocCacheCopy )
            vm_deallocate(mach_task_self(), (vm_address_t)dyldCache.cacheHeader(), (long)buffer.cacheIndent().cacheMappedSize);
        return nullptr;
    }
    dlopenGroupProxy._images.push_back(topImageProxy);
    dlopenGroupProxy._pathToProxy[targetDylib] = topImageProxy;

    // add all dylibs needed by dylib and are not in dyld cache
    topImageProxy->addDependentsDeep(dlopenGroupProxy, nullptr, false);
    if ( topImageProxy->diagnostics().hasError() ) {
        diag.copy(topImageProxy->diagnostics());
        if ( deallocCacheCopy )
            vm_deallocate(mach_task_self(), (vm_address_t)dyldCache.cacheHeader(), (long)buffer.cacheIndent().cacheMappedSize);
        return nullptr;
    }

    // construct ImageGroup from ImageProxies
    const BinaryImageGroupData* result = dlopenGroupProxy.makeImageGroupBinary(diag);

    // clean up
    if ( deallocCacheCopy )
        vm_deallocate(mach_task_self(), (vm_address_t)dyldCache.cacheHeader(), (long)buffer.cacheIndent().cacheMappedSize);

    return result;
}




// Used by closured and dyld_closure_util
BinaryClosureData* ImageProxyGroup::makeClosure(Diagnostics& diag, const DyldCacheParser& dyldCache,
                                                const std::string& mainProg, bool includeDylibsInDir,
                                                const std::vector<std::string>& buildTimePrefixes,
                                                const std::vector<std::string>& envVars)
{
    const BinaryImageGroupData* cachedDylibsGroupData = dyldCache.cachedDylibsGroup();
    const BinaryImageGroupData* otherDylibsGroupData  = dyldCache.otherDylibsGroup();
    std::vector<std::string> realBuildTimePrefixes;
    for (const std::string& prefix : buildTimePrefixes)  {
        char resolvedPath[PATH_MAX];
        if ( realpath(prefix.c_str(), resolvedPath) != nullptr )
            realBuildTimePrefixes.push_back(resolvedPath);
        else
            realBuildTimePrefixes.push_back(prefix);
    }
    std::vector<const BinaryImageGroupData*> existingGroups = { cachedDylibsGroupData, otherDylibsGroupData };
    ImageProxyGroup dyldCacheDylibProxyGroup(0, dyldCache, cachedDylibsGroupData, nullptr,                   "",       existingGroups, realBuildTimePrefixes, envVars);
    ImageProxyGroup dyldCacheOtherProxyGroup(1, dyldCache, otherDylibsGroupData,  &dyldCacheDylibProxyGroup, "",       existingGroups, realBuildTimePrefixes, envVars);
    ImageProxyGroup mainClosureGroupProxy(   2, dyldCache, nullptr,               &dyldCacheOtherProxyGroup, mainProg, existingGroups, realBuildTimePrefixes, envVars, false, true, true);

    // add any DYLD_INSERTED_LIBRARIES into closure
    if ( !mainClosureGroupProxy.addInsertedDylibs(diag) )
        return nullptr;

    ImageProxy* proxy = mainClosureGroupProxy.findAbsoluteImage(diag, mainProg, false, true);
    if ( proxy == nullptr )
        return nullptr;

    return mainClosureGroupProxy.makeClosureBinary(diag, proxy, includeDylibsInDir);
}

const char* sSkipPrograms_macOS[] = {
    "/Applications/iBooks.app/Contents/MacOS/iBooks",
};

const char* sSkipPrograms_embeddedOSes[] = {
    "/sbin/launchd",
    "/usr/local/sbin/launchd.debug",
    "/usr/local/sbin/launchd.development"
};

BinaryClosureData* ImageProxyGroup::makeClosureBinary(Diagnostics& diag, ImageProxy* mainProgProxy, bool includeDylibsInDir)
{
    assert(mainProgProxy != nullptr);
    assert(_images.size() >= 1);

    // check black list
    if ( _platform == Platform::macOS ) {
        for (const char* skipProg : sSkipPrograms_macOS) {
            if ( mainProgProxy->runtimePath() == skipProg ) {
                diag.error("black listed program");
                return nullptr;
            }
        }
    } else {
        for (const char* skipProg : sSkipPrograms_embeddedOSes) {
            if ( mainProgProxy->runtimePath() == skipProg ) {
                diag.error("black listed program");
                return nullptr;
            }
        }
    }

    _mainExecutableIndex = (uint32_t)_images.size() - 1;
    // add all dylibs needed by main excutable and are not in dyld cache
    mainProgProxy->addDependentsDeep(*this, nullptr, true);
    if ( mainProgProxy->diagnostics().hasError() ) {
        diag.copy(mainProgProxy->diagnostics());
        return nullptr;
    }

    // if main program is in .app bundle, look for other mach-o files to add to closure for use by dlopen
    bool isAppMainExecutable = false;
    std::string appDir;
    std::string leafName = basePath(mainProgProxy->runtimePath());
    size_t posAppX = mainProgProxy->runtimePath().rfind(std::string("/") + leafName + ".appex/");
    size_t posApp  = mainProgProxy->runtimePath().rfind(std::string("/") + leafName + ".app/");
    if (  posAppX != std::string::npos ) {
        appDir = mainProgProxy->runtimePath().substr(0, posAppX+leafName.size()+7);
        isAppMainExecutable = true;
    }
    else if ( posApp != std::string::npos ) {
        appDir = mainProgProxy->runtimePath().substr(0, posApp+leafName.size()+5);
        isAppMainExecutable = true;
    }
    if ( isAppMainExecutable ) {
        addExtraMachOsInBundle(appDir);
        for (size_t i=0; i < _images.size(); ++i) {
            // note: addDependentsDeep() can append to _images, so can't use regular iterator
            ImageProxy* aProxy = _images[i];
            ImageProxy::RPathChain base = { aProxy, nullptr, mainProgProxy->rpaths() };
            aProxy->addDependentsDeep(*this, &base, false);
            if ( aProxy->diagnostics().hasError() ) {
                aProxy->markInvalid();
                diag.warning("%s could not be added to closure because %s", aProxy->runtimePath().c_str(), aProxy->diagnostics().errorMessage().c_str());
            }
        }
    }
    else if ( includeDylibsInDir ) {
        size_t pos = mainProgProxy->runtimePath().rfind('/');
        if ( pos != std::string::npos ) {
            std::string mainDir = mainProgProxy->runtimePath().substr(0, pos);
            addExtraMachOsInBundle(mainDir);
            for (size_t i=0; i < _images.size(); ++i) {
                // note: addDependentsDeep() can append to _images, so can't use regular iterator
                ImageProxy* aProxy = _images[i];
                aProxy->addDependentsDeep(*this, nullptr, false);
            }
        }
    }

    // add addition dependents of any inserted libraries
    if ( _mainExecutableIndex != 0 ) {
        for (uint32_t i=0; i < _mainExecutableIndex; ++i) {
            _images[i]->addDependentsDeep(*this, nullptr, true);
            if ( _images[i]->diagnostics().hasError() )
                return nullptr;
        }
    }

    // gather warnings from all statically dependent images
    for (ImageProxy* proxy : _images) {
        if ( !proxy->staticallyReferenced() && proxy->diagnostics().hasError() )
            continue;
        diag.copy(proxy->diagnostics());
        if ( diag.hasError() ) {
            return nullptr;
        }
    }

    // get program entry
    MachOParser mainExecutableParser(mainProgProxy->mh(), _dyldCache.cacheIsMappedRaw());
    bool usesCRT;
    uint32_t entryOffset;
    mainExecutableParser.getEntry(entryOffset, usesCRT);

    // build ImageGroupWriter
    launch_cache::ImageGroupWriter groupWriter(_groupNum, mainExecutableParser.uses16KPages(), mainExecutableParser.is64(), _dylibsExpectedOnDisk, _inodesAreSameAsRuntime);
    populateGroupWriter(diag, groupWriter);
    if ( diag.hasError() )
        return nullptr;

    // pre-compute libSystem and libdyld into closure
    ImageRef libdyldEntryImageRef = ImageRef::makeEmptyImageRef();
    uint32_t libdyldEntryOffset;
    findLibdyldEntry(diag, libdyldEntryImageRef, libdyldEntryOffset);
    if ( diag.hasError() )
        return nullptr;
    ImageRef libSystemImageRef = ImageRef::makeEmptyImageRef();

    findLibSystem(diag, mainExecutableParser.isSimulatorBinary(), libSystemImageRef);
    if ( diag.hasError() )
        return nullptr;

    // build info about missing files and env vars
    __block StringPool            stringPool;
    __block std::vector<uint32_t> envVarOffsets;
    std::vector<uint16_t>         missingFileComponentOffsets;
    stringPool.add(" ");
    for (const std::string& path : _mustBeMissingFiles) {
        size_t start = 1;
        size_t slashPos = path.find('/', start);
        while (slashPos != std::string::npos) {
            std::string component = path.substr(start, slashPos - start);
            uint16_t offset = stringPool.add(component);
            missingFileComponentOffsets.push_back(offset);
            start = slashPos + 1;
            slashPos = path.find('/', start);
        }
        std::string lastComponent = path.substr(start);
        uint16_t offset = stringPool.add(lastComponent);
        missingFileComponentOffsets.push_back(offset);
        missingFileComponentOffsets.push_back(0);  // mark end of a path
    }
    missingFileComponentOffsets.push_back(0);  // mark end of all paths
    if ( missingFileComponentOffsets.size() & 1 )
        missingFileComponentOffsets.push_back(0);  // 4-byte align array
    __block uint32_t envVarCount = 0;
    _pathOverrides.forEachEnvVar(^(const char* envVar) {
       envVarOffsets.push_back(stringPool.add(envVar));
       ++envVarCount;
    });

    // 4-byte align string pool size
    stringPool.align();

    // malloc a buffer and fill in ImageGroup part
    uint32_t groupSize             = groupWriter.size();
    uint32_t missingFilesArraySize = (uint32_t)((missingFileComponentOffsets.size()*sizeof(uint16_t) + 3) & (-4));
    uint32_t envVarsSize           = (uint32_t)(envVarOffsets.size()*sizeof(uint32_t));
    uint32_t stringPoolSize        = (uint32_t)stringPool.size();
    size_t allocSize = sizeof(launch_cache::binary_format::Closure)
                     + groupSize
                     + missingFilesArraySize
                     + envVarsSize
                     + stringPoolSize;
    BinaryClosureData* clo = (BinaryClosureData*)malloc(allocSize);
    groupWriter.finalizeTo(diag, _knownGroups, &clo->group);
    launch_cache::ImageGroup cloGroup(&clo->group);
    launch_cache::Image      mainImage(cloGroup.imageBinary(_mainExecutableIndex));

    uint32_t maxImageLoadCount = groupWriter.maxLoadCount(diag, _knownGroups, &clo->group);

    if ( mainImage.isInvalid() ) {
        free((void*)clo);
        diag.error("depends on invalid dylib");
        return nullptr;
    }

    // fill in closure attributes
    clo->magic                          = launch_cache::binary_format::Closure::magicV1;
    clo->usesCRT                        = usesCRT;
    clo->isRestricted                   = mainProgProxy->isSetUID() || mainExecutableParser.isRestricted();
    clo->usesLibraryValidation          = mainExecutableParser.usesLibraryValidation();
    clo->padding                        = 0;
    clo->missingFileComponentsOffset    = offsetof(launch_cache::binary_format::Closure, group) + groupSize;
    clo->dyldEnvVarsOffset              = clo->missingFileComponentsOffset + missingFilesArraySize;
    clo->dyldEnvVarsCount               = envVarCount;
    clo->stringPoolOffset               = clo->dyldEnvVarsOffset + envVarsSize;
    clo->stringPoolSize                 = stringPoolSize;
    clo->libSystemRef                   = libSystemImageRef;
    clo->libDyldRef                     = libdyldEntryImageRef;
    clo->libdyldVectorOffset            = libdyldEntryOffset;
    clo->mainExecutableIndexInGroup     = _mainExecutableIndex;
    clo->mainExecutableEntryOffset      = entryOffset;
    clo->initialImageCount              = maxImageLoadCount;
    _dyldCache.cacheHeader()->getUUID(clo->dyldCacheUUID);

    if ( !mainExecutableParser.getCDHash(clo->mainExecutableCdHash) ) {
        // if no code signature, fill in 16-bytes with UUID then 4 bytes of zero
        bzero(clo->mainExecutableCdHash, 20);
        mainExecutableParser.getUuid(clo->mainExecutableCdHash);
    }
    if ( missingFilesArraySize != 0 )
        memcpy((uint8_t*)clo + clo->missingFileComponentsOffset, &missingFileComponentOffsets[0], missingFileComponentOffsets.size()*sizeof(uint16_t));
    if ( envVarsSize != 0 )
        memcpy((uint8_t*)clo + clo->dyldEnvVarsOffset, &envVarOffsets[0], envVarsSize);
    if ( stringPool.size() != 0 )
        memcpy((uint8_t*)clo + clo->stringPoolOffset, stringPool.buffer(), stringPool.size());

    return clo;
}

const BinaryImageGroupData* ImageProxyGroup::makeImageGroupBinary(Diagnostics& diag, const char* const neverEliminateStubs[])
{
    const bool continueIfErrors = (_groupNum == 1);
    bool uses16KPages = true;
    bool is64 = true;
    if ( !_images.empty() ) {
        MachOParser firstParser(_images.front()->mh(), _dyldCache.cacheIsMappedRaw());
        uses16KPages = firstParser.uses16KPages();
        is64         = firstParser.is64();
    }
    launch_cache::ImageGroupWriter groupWriter(_groupNum, uses16KPages, is64, _dylibsExpectedOnDisk, _inodesAreSameAsRuntime);
    populateGroupWriter(diag, groupWriter, neverEliminateStubs);
    if ( diag.hasError() )
        return nullptr;

    // malloc a buffer and fill in ImageGroup part
    BinaryImageGroupData* groupData = (BinaryImageGroupData*)malloc(groupWriter.size());
    groupWriter.finalizeTo(diag, _knownGroups, groupData);

    if ( !continueIfErrors && groupWriter.isInvalid(0) ) {
        free((void*)groupData);
        diag.error("depends on invalid dylib");
        return nullptr;
    }

    return groupData;
}


void ImageProxyGroup::findLibdyldEntry(Diagnostics& diag, ImageRef& ref, uint32_t& vmOffsetInLibDyld)
{
    Diagnostics libDyldDiag;
    ImageProxy* libDyldProxy = findImage(libDyldDiag, "/usr/lib/system/libdyld.dylib", false, nullptr);
    if ( libDyldProxy == nullptr ) {
        diag.error("can't find libdyld.dylib");
        return;
    }
    ref = ImageRef(0, libDyldProxy->groupNum(), libDyldProxy->indexInGroup());

    // find offset of "dyld3::entryVectorForDyld" in libdyld.dylib
    Diagnostics entryDiag;
    MachOParser::FoundSymbol dyldEntryInfo;
    MachOParser libDyldParser(libDyldProxy->mh(), _dyldCache.cacheIsMappedRaw());
    if ( !libDyldParser.findExportedSymbol(entryDiag, "__ZN5dyld318entryVectorForDyldE", nullptr, dyldEntryInfo, nullptr) ) {
        diag.error("can't find dyld entry point into libdyld.dylib");
        return;
    }
    vmOffsetInLibDyld = (uint32_t)dyldEntryInfo.value;
    const LibDyldEntryVector* entry = (LibDyldEntryVector*)(libDyldParser.content(vmOffsetInLibDyld));
    if ( entry == nullptr ) {
        diag.error("dyld entry point at offset 0x%0X not found in libdyld.dylib", vmOffsetInLibDyld);
        return;
    }
    if ( entry->vectorVersion != LibDyldEntryVector::kCurrentVectorVersion )
        diag.error("libdyld.dylib vector version is incompatible with this dyld cache builder");
    else if ( entry->binaryFormatVersion != launch_cache::binary_format::kFormatVersion )
        diag.error("libdyld.dylib closures binary format version is incompatible with this dyld cache builder");
}

void ImageProxyGroup::findLibSystem(Diagnostics& diag, bool forSimulator, ImageRef& ref)
{
    Diagnostics libSysDiag;
    ImageProxy* libSystemProxy = findImage(libSysDiag, forSimulator ? "/usr/lib/libSystem.dylib" : "/usr/lib/libSystem.B.dylib" , false, nullptr);
    if ( libSystemProxy == nullptr ) {
        diag.error("can't find libSystem.dylib");
        return;
    }
    ref = ImageRef(0, libSystemProxy->groupNum(), libSystemProxy->indexInGroup());
}


std::vector<ImageProxy*> ImageProxyGroup::flatLookupOrder()
{
    std::vector<ImageProxy*> results;
    // start with main executable and any inserted dylibs
    for (uint32_t i=0; i <= _mainExecutableIndex; ++i)
        results.push_back(_images[i]);

    // recursive add dependents of main executable
    _images[_mainExecutableIndex]->addToFlatLookup(results);

    // recursive add dependents of any inserted dylibs
    for (uint32_t i=0; i < _mainExecutableIndex; ++i)
        _images[i]->addToFlatLookup(results);

    return results;
}

void ImageProxyGroup::populateGroupWriter(Diagnostics& diag, launch_cache::ImageGroupWriter& groupWriter, const char* const neverEliminateStubs[])
{
    const bool buildingDylibsInCache = (_groupNum == 0);
    const bool continueIfErrors      = (_groupNum == 1);

    std::unordered_set<std::string>  neverStubEliminate;
    if ( neverEliminateStubs != nullptr ) {
        for (const char* const* nes=neverEliminateStubs; *nes != nullptr; ++nes)
            neverStubEliminate.insert(*nes);
    }
    
    // pass 1: add all images
    const uint64_t cacheUnslideBaseAddress = _dyldCache.cacheHeader()->unslidLoadAddress();
    const uint32_t imageCount = (uint32_t)_images.size();
    groupWriter.setImageCount(imageCount);
    for (uint32_t i=0; i < imageCount; ++i) {
        MachOParser imageParser(_images[i]->mh(), _dyldCache.cacheIsMappedRaw());
        assert((imageParser.inDyldCache() == buildingDylibsInCache) && "all images must be same type");
        // add info for each image
        groupWriter.setImagePath(i, _images[i]->runtimePath().c_str());
        groupWriter.setImageIsBundle(i, (imageParser.fileType() == MH_BUNDLE));
        bool hasObjC = imageParser.hasObjC();
        groupWriter.setImageHasObjC(i, hasObjC);
        bool isEncrypted = imageParser.isEncrypted();
        groupWriter.setImageIsEncrypted(i, isEncrypted);
        bool mayHavePlusLoad = false;
        if ( hasObjC ) {
            mayHavePlusLoad = isEncrypted || imageParser.hasPlusLoadMethod(diag);
            groupWriter.setImageMayHavePlusLoads(i, mayHavePlusLoad);
        }
        groupWriter.setImageHasWeakDefs(i, imageParser.hasWeakDefs());
        groupWriter.setImageMustBeThisDir(i, _images[i]->cwdMustBeThisDir());
        groupWriter.setImageIsPlatformBinary(i, _images[i]->isPlatformBinary());
        groupWriter.setImageOverridableDylib(i, !_stubEliminated || (neverStubEliminate.count(_images[i]->runtimePath()) != 0));
        uuid_t uuid;
        if ( imageParser.getUuid(uuid) )
            groupWriter.setImageUUID(i, uuid);
        if ( _inodesAreSameAsRuntime ) {
            groupWriter.setImageFileMtimeAndInode(i, _images[i]->fileModTime(), _images[i]->fileInode());
        }
        else {
            uint8_t cdHash[20];
            if ( !imageParser.getCDHash(cdHash) )
                bzero(cdHash, 20);
            // if image is not code signed, cdHash filled with all zeros
            groupWriter.setImageCdHash(i, cdHash);
        }
        if ( !buildingDylibsInCache ) {
            groupWriter.setImageSliceOffset(i, _images[i]->sliceFileOffset());
            uint32_t fairPlayTextOffset;
            uint32_t fairPlaySize;
            if ( imageParser.isFairPlayEncrypted(fairPlayTextOffset, fairPlaySize) )
                groupWriter.setImageFairPlayRange(i, fairPlayTextOffset, fairPlaySize);
            uint32_t codeSigOffset;
            uint32_t codeSigSize;
            if ( imageParser.hasCodeSignature(codeSigOffset, codeSigSize) )
                groupWriter.setImageCodeSignatureLocation(i, codeSigOffset, codeSigSize);
        }
        groupWriter.setImageDependentsCount(i, imageParser.dependentDylibCount());
        // add segments to image
        groupWriter.setImageSegments(i, imageParser, cacheUnslideBaseAddress);
        // add initializers to image
        __block std::vector<uint32_t> initOffsets;
        imageParser.forEachInitializer(diag, ^(uint32_t offset) {
            initOffsets.push_back(offset);
        });
        groupWriter.setImageInitializerOffsets(i, initOffsets);
        if ( diag.hasError() && !continueIfErrors ) {
            return;
        }
        // add DOFs to image
        __block std::vector<uint32_t> dofOffsets;
        imageParser.forEachDOFSection(diag, ^(uint32_t offset) {
            dofOffsets.push_back(offset);
        });
        groupWriter.setImageDOFOffsets(i, dofOffsets);
        if ( diag.hasError() && !continueIfErrors ) {
            return;
        }
        bool neverUnload = false;
        if ( buildingDylibsInCache )
            neverUnload = true;
        if ( _images[i]->staticallyReferenced() )
            neverUnload = true;
        if ( imageParser.hasObjC() && (imageParser.fileType() == MH_DYLIB) )
            neverUnload = true;
        if ( imageParser.hasThreadLocalVariables() )
            neverUnload = true;
        if ( !dofOffsets.empty() )
            neverUnload = true;
        groupWriter.setImageNeverUnload(i, neverUnload);
        if ( _images[i]->invalid() )
            groupWriter.setImageInvalid(i);
        // record if this is an override of an OS dylib
        ImageRef stdRef = _images[i]->overrideOf();
        if ( stdRef != ImageRef::weakImportMissing() ) {
            ImageRef thisImageRef(0, _groupNum, i);
            groupWriter.addImageIsOverride(stdRef, thisImageRef);
        }

        // add alias if runtimepath does not match installName
        if ( imageParser.fileType() == MH_DYLIB ) {
            const char* installName = imageParser.installName();
            if ( installName[0] == '/' ) {
                if ( _images[i]->runtimePath() != installName ) {
                    // add install name as an alias
                    groupWriter.addImageAliasPath(i, installName);
                }
            }
            // IOKit.framework on embedded uses not flat bundle, but clients dlopen() it as if it were flat
            if ( buildingDylibsInCache && (_platform != Platform::macOS) && (_images[i]->runtimePath() == "/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit") ) {
                groupWriter.addImageAliasPath(i, "/System/Library/Frameworks/IOKit.framework/IOKit");
            }
        }
    }

    // pass 2: add all dependencies (now that we have indexes defined)
    for (uint32_t i=0; (i < imageCount) && diag.noError(); ++i) {
        // add dependents to image
        __block uint32_t depIndex = 0;
        _images[i]->forEachDependent(^(ImageProxy* dep, LinkKind kind) {
            if ( dep == nullptr ) {
                if ( kind == LinkKind::weak )
                    groupWriter.setImageDependent(i, depIndex, launch_cache::binary_format::ImageRef::weakImportMissing());
                else
                    groupWriter.setImageInvalid(i);
            }
            else {
                launch_cache::binary_format::ImageRef ref((uint8_t)kind, dep->groupNum(), dep->indexInGroup());
                groupWriter.setImageDependent(i, depIndex, ref);
            }
            ++depIndex;
        });
    }

    // pass 3: invalidate any images dependent on invalid images)
    if ( continueIfErrors ) {
        const launch_cache::binary_format::ImageRef missingRef = launch_cache::binary_format::ImageRef::weakImportMissing();
        __block bool somethingInvalidated = false;
        do {
            somethingInvalidated = false;
            for (uint32_t i=0; i < imageCount; ++i) {
                if ( groupWriter.isInvalid(i) )
                    continue;
                uint32_t depCount = groupWriter.imageDependentsCount(i);
                for (uint32_t depIndex=0; depIndex < depCount; ++depIndex) {
                    launch_cache::binary_format::ImageRef ref = groupWriter.imageDependent(i, depIndex);
                    if ( ref == missingRef )
                        continue;
                    if ( ref.groupNum() == _groupNum ) {
                        if ( groupWriter.isInvalid(ref.indexInGroup()) ) {
                            // this image depends on something invalid, so mark it invalid
                            //fprintf(stderr, "warning: image %s depends on invalid %s\n", _images[i]->runtimePath().c_str(), _images[ref.index()]->runtimePath().c_str());
                            groupWriter.setImageInvalid(i);
                            somethingInvalidated = true;
                            break;
                        }
                    }
                }
            }
        } while (somethingInvalidated);
    }

    // pass 4: add fixups for each image, if needed
    bool someBadFixups = false;
    if ( !buildingDylibsInCache ) {
        // compute fix ups for all images
        __block std::vector<ImageProxy::FixupInfo> fixupInfos;
        fixupInfos.resize(imageCount);
        for (uint32_t imageIndex=0; imageIndex < imageCount; ++imageIndex) {
            if ( groupWriter.isInvalid(imageIndex) )
                continue;
            Diagnostics fixupDiag;
            fixupInfos[imageIndex] = _images[imageIndex]->buildFixups(fixupDiag, cacheUnslideBaseAddress, groupWriter);
            if ( fixupDiag.hasError() ) {
                // disable image in group
                someBadFixups = true;
                groupWriter.setImageInvalid(imageIndex);
                if ( continueIfErrors ) {
                    diag.warning("fixup problem in %s: %s", _images[imageIndex]->runtimePath().c_str(), fixupDiag.errorMessage().c_str());
                    continue;
                }
                else {
                    diag.error("fixup problem in %s: %s", _images[imageIndex]->runtimePath().c_str(), fixupDiag.errorMessage().c_str());
                    return;
                }
            }
        }
        // if building closure, build patches to shared cache
        if ( _groupNum == 2) {
            std::unordered_set<ImageProxy*> staticImagesWithWeakDefs;
            ImageProxyGroup* cacheGroup = _nextSearchGroup->_nextSearchGroup;
            assert(cacheGroup->_basedOn != nullptr);
            launch_cache::ImageGroup dyldCacheGroup(cacheGroup->_basedOn);
            for (uint32_t imageIndex=0; imageIndex < imageCount; ++imageIndex) {
                if ( groupWriter.isInvalid(imageIndex) )
                    continue;
                ImageProxy* thisProxy = _images[imageIndex];
                // Only process interposing info on dylibs statically linked into closure
                if ( !thisProxy->staticallyReferenced() )
                    continue;
                MachOParser imageParser(thisProxy->mh(), _dyldCache.cacheIsMappedRaw());
                // if any images in closure interpose on something in dyld cache, record the cache patches needed
                imageParser.forEachInterposingTuple(diag, ^(uint32_t segIndex, uint64_t replacementSegOffset, uint64_t replaceeSegOffset, uint64_t replacementContent, bool& tupleStop) {
                    if ( _groupNum != 2 ) {
                        groupWriter.setImageInvalid(imageIndex);
                        return;
                    }
                    TargetSymbolValue interposeReplacee    = TargetSymbolValue::makeInvalid();
                    TargetSymbolValue interposeReplacement = TargetSymbolValue::makeInvalid();
                    for (const FixUp& fixup : fixupInfos[imageIndex].fixups) {
                        if ( fixup.segIndex != segIndex )
                            continue;
                        if ( fixup.segOffset == replacementSegOffset ) {
                            if ( fixup.type == launch_cache::ImageGroupWriter::FixupType::rebase ) {
                                uint64_t offsetInImage = replacementContent - imageParser.preferredLoadAddress();
                                interposeReplacement = TargetSymbolValue::makeGroupValue(2, imageIndex, offsetInImage, false);
                            }
                            else {
                                diag.warning("bad interposing implementation in %s", _images[imageIndex]->runtimePath().c_str());
                                return;
                            }
                        }
                        else if ( fixup.segOffset ==  replaceeSegOffset ) {
                            if ( fixup.type == launch_cache::ImageGroupWriter::FixupType::pointerBind ) {
                                interposeReplacee = fixup.target;
                            }
                            else {
                                diag.warning("bad interposing target in %s", _images[imageIndex]->runtimePath().c_str());
                                return;
                            }
                        }
                    }
                    // scan through fixups of other images in closure looking to see what functions this entry references
                    for (uint32_t otherIndex=0; otherIndex < imageCount; ++otherIndex) {
                        if ( otherIndex == imageIndex )
                            continue;
                        for (FixUp& fixup : fixupInfos[otherIndex].fixups) {
                            switch ( fixup.type ) {
                                case launch_cache::ImageGroupWriter::FixupType::pointerBind:
                                case launch_cache::ImageGroupWriter::FixupType::pointerLazyBind:
                                    // alter fixup to use interposed function instead of requested
                                    if ( fixup.target == interposeReplacee )
                                        fixup.target = interposeReplacement;
                                    break;
                                case launch_cache::ImageGroupWriter::FixupType::rebase:
                                case launch_cache::ImageGroupWriter::FixupType::rebaseText:
                                case launch_cache::ImageGroupWriter::FixupType::ignore:
                                case launch_cache::ImageGroupWriter::FixupType::bindText:
                                case launch_cache::ImageGroupWriter::FixupType::bindTextRel:
                                case launch_cache::ImageGroupWriter::FixupType::bindImportJmpRel:
                                   break;
                            }
                        }
                    }
                    if ( interposeReplacee.isInvalid() || interposeReplacement.isInvalid() ) {
                        diag.error("malformed interposing section in %s", _images[imageIndex]->runtimePath().c_str());
                        tupleStop = true;
                        return;
                    }
                    // record any overrides in shared cache that will need to be applied at launch time
                    uint64_t offsetInCache;
                    if ( interposeReplacee.isSharedCacheTarget(offsetInCache) ) {
                        uint32_t patchTableIndex;
                        if ( dyldCacheGroup.hasPatchTableIndex((uint32_t)offsetInCache, patchTableIndex) ) {
                            uint32_t    replacementGroupNum;
                            uint32_t    replacementIndexInGroup;
                            uint64_t    replacementOffsetInImage;
                            assert(interposeReplacement.isGroupImageTarget(replacementGroupNum, replacementIndexInGroup, replacementOffsetInImage));
                            assert(replacementGroupNum == 2);
                            assert(replacementIndexInGroup < (1 << 8));
                            assert(replacementOffsetInImage < 0xFFFFFFFFULL);
                            DyldCacheOverride cacheOverride;
                            cacheOverride.patchTableIndex = patchTableIndex;
                            cacheOverride.imageIndex      = replacementIndexInGroup;
                            cacheOverride.imageOffset     = replacementOffsetInImage;
                            _cacheOverrides.push_back(cacheOverride);
                        }
                    }
                });
                if ( diag.hasError() && !continueIfErrors ) {
                    return;
                }
                // if any dylibs in the closure override a dyld cache dylib, then record the cache patches needed
                ImageRef overrideOf = thisProxy->overrideOf();
                if ( (overrideOf != ImageRef::makeEmptyImageRef()) && (overrideOf.groupNum() == 0) ) {
                     //fprintf(stderr, "need to patch %s into cache\n", thisProxy->runtimePath().c_str());
                    const launch_cache::Image imageInCache = dyldCacheGroup.image(overrideOf.indexInGroup());
                    const mach_header* imageInCacheMH = (mach_header*)((char*)(_dyldCache.cacheHeader()) + imageInCache.cacheOffset());
                    MachOParser inCacheParser(imageInCacheMH, _dyldCache.cacheIsMappedRaw());
                    // walk all exported symbols in dylib in cache
                    inCacheParser.forEachExportedSymbol(diag, ^(const char* symbolName, uint64_t imageOffset, bool isReExport, bool &stop) {
                        if ( isReExport )
                            return;
                        uint32_t cacheOffsetOfSymbol = (uint32_t)(imageInCache.cacheOffset() + imageOffset);
                        //fprintf(stderr, "  patch cache offset 0x%08X which is %s\n", cacheOffsetOfSymbol, symbolName);
                        // for each exported symbol, see if it is in patch table (used by something else in cache)
                        uint32_t patchTableIndex;
                        if ( dyldCacheGroup.hasPatchTableIndex(cacheOffsetOfSymbol, patchTableIndex) ) {
                            //fprintf(stderr, "  need patch cache offset 0x%08X\n", cacheOffsetOfSymbol);
                            // lookup address of symbol in override dylib and add patch info
                            MachOParser::FoundSymbol foundInfo;
                            if ( imageParser.findExportedSymbol(diag, symbolName, nullptr, foundInfo, nullptr) ) {
                                DyldCacheOverride cacheOverride;
                                assert(patchTableIndex < (1 << 24));
                                assert(thisProxy->indexInGroup() < (1 << 8));
                                assert(foundInfo.value < (1ULL << 32));
                                cacheOverride.patchTableIndex = patchTableIndex;
                                cacheOverride.imageIndex      = thisProxy->indexInGroup();
                                cacheOverride.imageOffset     = foundInfo.value;
                                _cacheOverrides.push_back(cacheOverride);
                            }
                        }
                    });
                }
                // save off all images in closure with weak defines
                if ( thisProxy->mh()->flags & (MH_WEAK_DEFINES|MH_BINDS_TO_WEAK) ) {
                    staticImagesWithWeakDefs.insert(thisProxy);
                }
            }
            // if any dylibs in the closure override a weak symbol in a cached dylib, then record the cache patches needed
            if ( !staticImagesWithWeakDefs.empty() ) {
                // build list of all weak def symbol names
                __block std::unordered_map<std::string, DyldCacheOverride> weakSymbols;
                for (ImageProxy* proxy : staticImagesWithWeakDefs ) {
                    MachOParser weakDefParser(proxy->mh(), _dyldCache.cacheIsMappedRaw());
                    weakDefParser.forEachWeakDef(diag, ^(bool strongDef, uint32_t segIndex, uint64_t segOffset, uint64_t addend, const char* symbolName, bool& stop) {
                        weakSymbols[symbolName] = { 0, 0, 0 };
                    });
                }
                // do a flat namespace walk of all images
                std::vector<ImageProxy*> flatSearchOrder = flatLookupOrder();
                for (ImageProxy* proxy : flatSearchOrder) {
                    // only look at images that participate in weak coalescing
                    if ( (proxy->mh()->flags & (MH_WEAK_DEFINES|MH_BINDS_TO_WEAK)) == 0 )
                        continue;
                    // look only at images in closure
                    if ( proxy->groupNum() == 2 ) {
                        MachOParser weakDefParser(proxy->mh(), _dyldCache.cacheIsMappedRaw());
                        // check if this closure image defines any of the not-yet found weak symbols
                        for (auto& entry : weakSymbols ) {
                            if ( entry.second.imageOffset != 0 )
                                continue;
                            Diagnostics weakDiag;
                            MachOParser::FoundSymbol foundInfo;
                            if ( weakDefParser.findExportedSymbol(weakDiag, entry.first.c_str(), nullptr, foundInfo, nullptr) ) {
                                assert(proxy->indexInGroup() < (1 << 8));
                                assert(foundInfo.value < (1ULL << 32));
                                entry.second.imageIndex  = proxy->indexInGroup();
                                entry.second.imageOffset = foundInfo.value;
                            }
                        }
                    }
                }
                for (ImageProxy* proxy : flatSearchOrder) {
                    // only look at images that participate in weak coalescing
                    if ( (proxy->mh()->flags & (MH_WEAK_DEFINES|MH_BINDS_TO_WEAK)) == 0 )
                        continue;
                    // look only at images in dyld cache
                    if ( proxy->groupNum() == 0 ) {
                        const launch_cache::Image imageInCache = dyldCacheGroup.image(proxy->indexInGroup());
                        MachOParser inCacheParser(proxy->mh(), _dyldCache.cacheIsMappedRaw());
                        Diagnostics cacheDiag;
                        for (auto& entry : weakSymbols) {
                            if ( entry.second.imageOffset == 0 )
                                continue;
                            Diagnostics weakDiag;
                            MachOParser::FoundSymbol foundInfo;
                            if ( inCacheParser.findExportedSymbol(weakDiag, entry.first.c_str(), nullptr, foundInfo, nullptr) ) {
                                uint32_t cacheOffsetOfSymbol = (uint32_t)(imageInCache.cacheOffset() + foundInfo.value);
                                // see if this symbol is in patch table (used by something else in cache)
                                uint32_t patchTableIndex;
                                if ( dyldCacheGroup.hasPatchTableIndex(cacheOffsetOfSymbol, patchTableIndex) ) {
                                    //fprintf(stderr, "  need patch cache offset 0x%08X\n", cacheOffsetOfSymbol);
                                    DyldCacheOverride cacheOverride;
                                    cacheOverride.patchTableIndex = patchTableIndex;
                                    cacheOverride.imageIndex      = entry.second.imageIndex;
                                    cacheOverride.imageOffset     = entry.second.imageOffset;
                                    _cacheOverrides.push_back(cacheOverride);
                                }
                            }
                        }
                    }
                }
            }
        }
        // record fixups for each image
        for (uint32_t imageIndex=0; imageIndex < imageCount; ++imageIndex) {
            groupWriter.setImageFixups(diag, imageIndex, fixupInfos[imageIndex].fixups, fixupInfos[imageIndex].hasTextRelocs);
        }
    }

    // pass 5: invalidate any images dependent on invalid images)
    if ( someBadFixups && continueIfErrors ) {
        __block bool somethingInvalidated = false;
        do {
            somethingInvalidated = false;
            for (uint32_t i=0; i < imageCount; ++i) {
                if ( groupWriter.isInvalid(i) )
                    continue;
                uint32_t depCount = groupWriter.imageDependentsCount(i);
                for (uint32_t depIndex=0; depIndex < depCount; ++depIndex) {
                    launch_cache::binary_format::ImageRef ref = groupWriter.imageDependent(i, depIndex);
                    if ( ref.groupNum() == _groupNum ) {
                        if ( groupWriter.isInvalid(ref.indexInGroup()) ) {
                            // this image depends on something invalid, so mark it invalid
                            //fprintf(stderr, "warning: image %s depends on invalid %s\n", _images[i]->runtimePath().c_str(), _images[ref.index()]->runtimePath().c_str());
                            groupWriter.setImageInvalid(i);
                            somethingInvalidated = true;
                            break;
                        }
                    }
                }
            }
        } while (somethingInvalidated);
    }

    // pass 6: compute initializer lists for each image
    const bool log = false;
    for (uint32_t imageIndex=0; imageIndex < imageCount; ++imageIndex) {
        if ( groupWriter.isInvalid(imageIndex) )
            continue;

        auto inits = _images[imageIndex]->getInitBeforeList(*this);
        if ( log && buildingDylibsInCache ) {
            fprintf(stderr, "%s\n   init list: ", _images[imageIndex]->runtimePath().c_str());
            for (launch_cache::binary_format::ImageRef ref : inits) {
                if ( ref.groupNum() == 0 ) {
                    std::string dep = _images[ref.indexInGroup()]->runtimePath();
                    size_t off = dep.rfind('/');
                    fprintf(stderr, "%s, ", dep.substr(off+1).c_str());
                }
            }
            fprintf(stderr, "\n");
        }
        groupWriter.setImageInitBefore(imageIndex, inits);
    }

    // pass 7: compute DOFs
    for (uint32_t imageIndex=0; imageIndex < imageCount; ++imageIndex) {
        if ( groupWriter.isInvalid(imageIndex) )
            continue;

        auto inits = _images[imageIndex]->getInitBeforeList(*this);
        if ( log && buildingDylibsInCache ) {
            fprintf(stderr, "%s\n   DOFs: ", _images[imageIndex]->runtimePath().c_str());
            for (launch_cache::binary_format::ImageRef ref : inits) {
                if ( ref.groupNum() == 0 ) {
                    std::string dep = _images[ref.indexInGroup()]->runtimePath();
                    size_t off = dep.rfind('/');
                    fprintf(stderr, "%s, ", dep.substr(off+1).c_str());
                }
            }
            fprintf(stderr, "\n");
        }
        groupWriter.setImageInitBefore(imageIndex, inits);
    }

    // pass 8: add patch table entries iff this is dyld cache ImageGroup
    assert(buildingDylibsInCache == (_patchTable != nullptr));
    if ( _patchTable != nullptr ) {
        for (uint32_t i=0; i < imageCount; ++i) {
            const auto pos = _patchTable->find(_images[i]->mh());
            if ( pos != _patchTable->end() ) {
                for (const auto& entry : pos->second ) {
                    uint32_t defFunctionOffset = entry.first;
                    groupWriter.setImagePatchLocations(i, defFunctionOffset, entry.second);
                }
            }
        }
    }

    // if this is a main closure group with an interposing dylib, add cache overrides
    if ( !_cacheOverrides.empty() ) {
        groupWriter.setGroupCacheOverrides(_cacheOverrides);
    }

    // align string pool
    groupWriter.alignStringPool();
}



} // namespace dyld3