qld 0.1.1

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

use std::collections::BTreeSet;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::Command;

use qld::args::{InputAttrs, InputKind, ParseOutcome, parse_darwin};
use qld::diag::{Collect, DiagnosticSink};
use qld::macho::read::consts::{
    LC_CODE_SIGNATURE, LC_DYLD_CHAINED_FIXUPS, LC_DYLD_EXPORTS_TRIE, LC_LOAD_DYLIB, LC_MAIN,
    LC_SYMTAB, LC_UUID, MH_EXECUTE,
};
use qld::macho::read::{ChainedFixups, MachOFile, Source};
use qld::output::hash::Sha256;

#[path = "macho_link/arm64e.rs"]
mod arm64e;
#[path = "macho_link/library.rs"]
mod library;
#[path = "macho_link/namespace.rs"]
mod namespace;
#[path = "macho_link/objc.rs"]
mod objc;
#[path = "macho_link/search_paths.rs"]
mod search_paths;
#[path = "macho_link/suite.rs"]
mod suite;
#[path = "macho_link/undefined.rs"]
mod undefined;
#[path = "macho_link/weak_binding.rs"]
mod weak_binding;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn data_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/macho_link")
}

fn scratch(name: &str) -> PathBuf {
    let dir = Path::new(env!("CARGO_TARGET_TMPDIR"))
        .join("macho_link")
        .join(name);
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

fn require_tools() -> bool {
    std::env::var_os("QLD_REQUIRE_MACHO_TOOLS").is_some_and(|v| v == "1")
}

/// Skips (or fails, under `QLD_REQUIRE_MACHO_TOOLS=1`) with `why`.
fn skip(test: &str, why: &str) {
    assert!(!require_tools(), "{test}: {why}");
    eprintln!("skipping {test}: {why}");
}

/// `llvm-<name>`, or on macOS `<name>` when only that exists (Xcode ships
/// the LLVM tools without the prefix: `objdump`, `lipo`, `dwarfdump`).
/// Elsewhere an unprefixed tool is GNU binutils, which cannot read Mach-O
/// the same way, so it is never used.
fn tool(name: &str) -> String {
    let prefixed = format!("llvm-{name}");
    if Command::new(&prefixed).arg("--version").output().is_ok() {
        return prefixed;
    }
    if cfg!(target_os = "macos") && Command::new(name).arg("--version").output().is_ok() {
        return name.to_owned();
    }
    prefixed
}

/// Creates a static archive of `objects` with `llvm-ar`, or Xcode's
/// `libtool`.
fn make_archive(archive: &Path, objects: &[&Path]) -> bool {
    let _ = std::fs::remove_file(archive);
    let mut llvm_ar = Command::new("llvm-ar");
    llvm_ar
        .args(["--format=darwin", "rcs"])
        .arg(archive)
        .args(objects);
    if llvm_ar.output().is_ok_and(|o| o.status.success()) {
        return true;
    }
    Command::new("libtool")
        .args(["-static", "-o"])
        .arg(archive)
        .args(objects)
        .output()
        .is_ok_and(|o| o.status.success())
}

fn tool_works(tool: &str, args: &[&str]) -> bool {
    Command::new(tool)
        .args(args)
        .output()
        .is_ok_and(|o| o.status.success())
}

/// Whether clang can compile for `arch`-apple-macos.
fn clang_for(arch: &str) -> bool {
    let dir = scratch("probe");
    let source = dir.join("probe.c");
    std::fs::write(&source, "int probe(void) { return 1; }\n").unwrap();
    let object = dir.join(format!("probe-{arch}.o"));
    tool_works(
        "clang",
        &[
            &format!("--target={arch}-apple-macos13"),
            "-c",
            source.to_str().unwrap(),
            "-o",
            object.to_str().unwrap(),
        ],
    )
}

/// Compiles fixture `source` for `arch` into the test's scratch directory.
fn compile(test: &str, source: &str, arch: &str, extra: &[&str]) -> PathBuf {
    let dir = scratch(test);
    let input = data_dir().join(source);
    let stem = Path::new(source).file_stem().unwrap().to_str().unwrap();
    let output = dir.join(format!("{stem}-{arch}.o"));
    let compiler = if source.ends_with(".cpp") || source.ends_with(".mm") {
        "clang++"
    } else {
        "clang"
    };
    let status = Command::new(compiler)
        .arg(format!("--target={arch}-apple-macos13"))
        .args(["-O1", "-c"])
        .args(extra)
        .arg(&input)
        .arg("-o")
        .arg(&output)
        .status()
        .unwrap();
    assert!(status.success(), "compiling {source} for {arch}");
    output
}

/// The `-syslibroot` to link against: the real SDK on macOS, the stubs
/// elsewhere.
fn syslibroot() -> PathBuf {
    if cfg!(target_os = "macos")
        && let Ok(output) = Command::new("xcrun").arg("--show-sdk-path").output()
        && output.status.success()
    {
        return PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
    }
    data_dir().join("sdk")
}

fn os(args: &[&str]) -> Vec<OsString> {
    args.iter().map(OsString::from).collect()
}

/// Links with an ld64 command line (without `argv[0]`) and returns the
/// output bytes and the diagnostics.
fn link_bytes(args: &[OsString]) -> Result<(Vec<u8>, Vec<String>), String> {
    let mut argv = vec![OsString::from("ld64.qld")];
    argv.extend_from_slice(args);
    let options = match parse_darwin(&argv) {
        Ok(ParseOutcome::Link(options)) => options,
        Ok(other) => return Err(format!("not a link: {other:?}")),
        Err(error) => return Err(error.to_string()),
    };
    let diagnostics = Collect::new();
    let result = qld::macho::link_to_bytes(&options, &diagnostics);
    let messages: Vec<String> = diagnostics
        .take_sorted()
        .into_iter()
        .map(|d| d.message)
        .collect();
    match result {
        Ok(bytes) => Ok((bytes, messages)),
        Err(error) => Err(format!("{error}: {messages:?}")),
    }
}

/// Links `objects` into an executable for `arch` and writes it to `output`.
fn link_executable(arch: &str, objects: &[&Path], extra: &[&str], output: &Path) -> Vec<u8> {
    let root = syslibroot();
    let mut args = os(&[
        "-arch",
        arch,
        "-platform_version",
        "macos",
        "13.0",
        "13.0",
        "-syslibroot",
        root.to_str().unwrap(),
        "-o",
        output.to_str().unwrap(),
    ]);
    for object in objects {
        args.push(object.into());
    }
    args.extend(os(extra));
    args.extend(os(&["-lSystem"]));
    let (bytes, _) = link_bytes(&args).unwrap_or_else(|e| panic!("link failed: {e}"));
    std::fs::write(output, &bytes).unwrap();
    make_executable(output);
    bytes
}

#[cfg(unix)]
fn make_executable(path: &Path) {
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
}

#[cfg(not(unix))]
fn make_executable(_: &Path) {}

/// The load commands of `data`, as `(cmd, payload offset)`.
fn load_commands(data: &[u8]) -> Vec<(u32, usize, usize)> {
    let file = MachOFile::parse(data, Source::new(Path::new("out"))).unwrap();
    file.load_commands()
        .map(|c| {
            let c = c.unwrap();
            (c.cmd, c.offset as usize, c.data.len())
        })
        .collect()
}

fn u32_le(data: &[u8], at: usize) -> u32 {
    u32::from_le_bytes(data[at..at + 4].try_into().unwrap())
}

fn u32_be(data: &[u8], at: usize) -> u32 {
    u32::from_be_bytes(data[at..at + 4].try_into().unwrap())
}

/// Recomputes the page hashes of the ad-hoc signature.
fn check_signature(data: &[u8]) {
    let (_, offset, _) = load_commands(data)
        .into_iter()
        .find(|c| c.0 == LC_CODE_SIGNATURE)
        .expect("LC_CODE_SIGNATURE");
    let sig_offset = u32_le(data, offset + 8) as usize;
    let sig_size = u32_le(data, offset + 12) as usize;
    assert_eq!(sig_offset + sig_size, data.len(), "signature ends the file");
    let sig = &data[sig_offset..];
    assert_eq!(u32_be(sig, 0), 0xfade_0cc0, "SuperBlob magic");
    assert_eq!(u32_be(sig, 8), 1, "one blob");
    let cd = u32_be(sig, 16) as usize;
    assert_eq!(u32_be(sig, cd), 0xfade_0c02, "CodeDirectory magic");
    let flags = u32_be(sig, cd + 12);
    assert_eq!(flags, 0x0002_0002, "adhoc | linker-signed");
    let hash_offset = cd + u32_be(sig, cd + 16) as usize;
    let slots = u32_be(sig, cd + 28) as usize;
    let code_limit = u32_be(sig, cd + 32) as usize;
    assert_eq!(code_limit, sig_offset);
    assert_eq!(sig[cd + 36], 32, "hash size");
    assert_eq!(sig[cd + 37], 2, "SHA-256");
    assert_eq!(sig[cd + 39], 12, "4 KiB pages");
    assert_eq!(slots, code_limit.div_ceil(4096));
    for slot in 0..slots {
        let page = &data[slot * 4096..((slot + 1) * 4096).min(code_limit)];
        let expected = Sha256::digest(page);
        let at = hash_offset + slot * 32;
        assert_eq!(&sig[at..at + 32], &expected, "hash of page {slot}");
    }
}

/// A `nlist_64` record of `LC_SYMTAB`.
#[derive(Debug)]
struct Nlist {
    name: String,
    n_type: u8,
    n_value: u64,
}

impl Nlist {
    /// Defined in a section (and not a stab).
    fn is_defined(&self) -> bool {
        self.n_type & 0xe0 == 0 && self.n_type & 0x0e == 0x0e
    }

    fn is_external(&self) -> bool {
        self.n_type & 0xe0 == 0 && self.n_type & 0x01 != 0
    }
}

/// The symbol table of a 64-bit little-endian image, read with qld's reader
/// (not `nm`, whose output differs between LLVM, Xcode and GNU binutils).
fn symbols(data: &[u8]) -> Vec<Nlist> {
    let file = MachOFile::parse(data, Source::new(Path::new("out"))).unwrap();
    let Some(command) = file.find_command(LC_SYMTAB).unwrap() else {
        return Vec::new();
    };
    let symtab = command.symtab().unwrap();
    let strtab = &data[symtab.stroff as usize..][..symtab.strsize as usize];
    (0..symtab.nsyms as usize)
        .map(|i| {
            let at = symtab.symoff as usize + i * 16;
            let strx = u32_le(data, at) as usize;
            let name = strtab[strx..].split(|&b| b == 0).next().unwrap();
            Nlist {
                name: String::from_utf8_lossy(name).into_owned(),
                n_type: data[at + 4],
                n_value: u64::from_le_bytes(data[at + 8..at + 16].try_into().unwrap()),
            }
        })
        .collect()
}

/// The address of the defined symbol `name`.
fn symbol_address(data: &[u8], name: &str) -> Option<u64> {
    symbols(data)
        .into_iter()
        .find(|s| s.is_defined() && s.name == name)
        .map(|s| s.n_value)
}

/// An import of `LC_DYLD_CHAINED_FIXUPS`.
#[derive(Debug)]
struct Import {
    name: String,
    lib_ordinal: i32,
    weak: bool,
}

/// The chained-fixup imports, read with qld's reader (Xcode's `objdump`
/// prints neither the names nor the weak-lookup binds).
fn chained_imports(data: &[u8]) -> Vec<Import> {
    let source = Source::new(Path::new("out"));
    let file = MachOFile::parse(data, source).unwrap();
    let command = file
        .find_command(LC_DYLD_CHAINED_FIXUPS)
        .unwrap()
        .expect("LC_DYLD_CHAINED_FIXUPS");
    let blob = command.linkedit_data().unwrap();
    let bytes = &data[blob.dataoff as usize..][..blob.datasize as usize];
    let fixups =
        ChainedFixups::parse(bytes, u64::from(blob.dataoff), file.endian(), source).unwrap();
    fixups
        .imports()
        .map(|import| {
            let import = import.unwrap();
            Import {
                name: String::from_utf8_lossy(import.name).into_owned(),
                lib_ordinal: import.lib_ordinal,
                weak: import.weak_import,
            }
        })
        .collect()
}

/// The import named `name`, which must exist.
fn import<'a>(imports: &'a [Import], name: &str) -> &'a Import {
    imports
        .iter()
        .find(|i| i.name == name)
        .unwrap_or_else(|| panic!("no import {name}: {imports:?}"))
}

fn objdump(args: &[&str], file: &Path) -> Option<String> {
    let output = Command::new(tool("objdump"))
        .args(args)
        .arg(file)
        .output()
        .ok()?;
    output
        .status
        .success()
        .then(|| String::from_utf8_lossy(&output.stdout).into_owned())
}

/// `ld64.lld`, from `QLD_LD64_LLD` or the usual build location.
fn ld64_lld() -> Option<PathBuf> {
    let candidates = [
        std::env::var_os("QLD_LD64_LLD").map(PathBuf::from),
        std::env::var_os("HOME").map(|h| {
            PathBuf::from(h).join(".cache/qld-projects/build/llvm-23.1.1-static/bin/ld64.lld")
        }),
        Some(PathBuf::from("ld64.lld")),
    ];
    candidates
        .into_iter()
        .flatten()
        .find(|path| tool_works(path.to_str().unwrap_or(""), &["--version"]))
}

/// Names of the sections, exports and imports, for comparing outputs.
#[derive(Debug, PartialEq, Eq)]
struct Summary {
    sections: BTreeSet<String>,
    exports: BTreeSet<String>,
    imports: BTreeSet<String>,
    dylibs: Vec<String>,
    /// The `__unwind_info` encodings, in function order, without addresses.
    unwind: Vec<String>,
}

fn summarize(file: &Path) -> Option<Summary> {
    let headers = objdump(&["--macho", "--section-headers"], file)?;
    let sections = headers
        .lines()
        .filter_map(|l| {
            let mut parts = l.split_whitespace();
            let _index = parts.next()?.parse::<u32>().ok()?;
            let name = parts.next()?;
            // Linker-specific extras: lld's lazy binding for legacy
            // (`LC_DYLD_INFO_ONLY`) outputs, which qld binds at load time.
            if matches!(
                name,
                "__unwind_info" | "__eh_frame" | "__stub_helper" | "__la_symbol_ptr"
            ) {
                return None;
            }
            // lld merges the literal sections into one `__literals`; ld64
            // and qld keep `__literal4`, `__literal8` and `__literal16`.
            // lld also merges Objective-C class names and method types into
            // `__cstring`.
            Some(if name.starts_with("__literal") {
                "__literals".to_owned()
            } else if matches!(name, "__objc_classname" | "__objc_methtype") {
                "__cstring".to_owned()
            } else {
                name.to_owned()
            })
        })
        .collect();
    let trie = objdump(&["--macho", "--exports-trie"], file)?;
    let exports = trie
        .lines()
        .filter_map(|l| l.split_whitespace().nth(1).map(str::to_owned))
        .filter(|n| n.starts_with('_'))
        .collect();
    let fixups = objdump(&["--macho", "--dyld-info"], file)?;
    let imports = fixups
        .lines()
        .filter(|l| l.contains("bind"))
        .filter_map(|l| l.split_whitespace().last().map(str::to_owned))
        .collect();
    let dylibs = objdump(&["--macho", "--dylibs-used"], file)?
        .lines()
        .skip(1)
        .map(|l| l.split_whitespace().next().unwrap_or("").to_owned())
        .collect();
    // DWARF encodings hold an `__eh_frame` offset, which depends on which
    // CIEs a linker keeps.
    let data = std::fs::read(file).ok()?;
    let arm64 = MachOFile::parse(&data, Source::new(file))
        .ok()?
        .header()
        .cpu_type
        == qld::macho::read::consts::CPU_TYPE_ARM64;
    let dwarf_mode = if arm64 { 0x0300_0000 } else { 0x0400_0000 };
    let unwind = objdump(&["--macho", "--unwind-info"], file)?
        .lines()
        .filter_map(|l| {
            let (_, encoding) = l.split_once("encoding[")?;
            let text = encoding.split_once('=')?.1.trim();
            let value = u32::from_str_radix(text.trim_start_matches("0x"), 16).ok()?;
            Some(if value & 0x0f00_0000 == dwarf_mode {
                "dwarf".to_owned()
            } else {
                text.to_owned()
            })
        })
        .collect();
    Some(Summary {
        sections,
        exports,
        imports,
        dylibs,
        unwind,
    })
}

/// Runs `binary` on macOS and returns its stdout.
fn run(binary: &Path) -> Option<String> {
    if !cfg!(target_os = "macos") {
        return None;
    }
    let output = Command::new(binary).output().unwrap();
    assert!(
        output.status.success(),
        "{} failed: {:?}\n{}",
        binary.display(),
        output.status,
        String::from_utf8_lossy(&output.stderr)
    );
    Some(String::from_utf8_lossy(&output.stdout).into_owned())
}

fn host_can_run(arch: &str) -> bool {
    cfg!(target_os = "macos")
        && (arch == std::env::consts::ARCH
            || (arch == "arm64" && std::env::consts::ARCH == "aarch64")
            || (arch == "x86_64" && tool_works("arch", &["-x86_64", "/usr/bin/true"])))
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

/// A Mach-O link reads [`LinkOptions::inputs`] like every other format, so
/// a library caller can hand it an object as bytes and name a framework
/// without an ld64 command line. Linking the same program both ways must
/// give the same image.
#[test]
fn in_memory_inputs_link_like_files() {
    let arch = "x86_64";
    if !clang_for(arch) {
        skip(
            "in_memory_inputs_link_like_files",
            &format!("clang cannot target {arch}-apple-macos"),
        );
        return;
    }
    let object = compile("memory-inputs", "hello.c", arch, &[]);
    let args = os(&[
        "-arch",
        arch,
        "-platform_version",
        "macos",
        "13.0",
        "13.0",
        "-syslibroot",
        syslibroot().to_str().unwrap(),
        object.to_str().unwrap(),
        "-lSystem",
        "-o",
        "unused",
    ]);
    let (from_files, _) = link_bytes(&args).unwrap();

    // The same link, with the object passed as bytes and the inputs built
    // by hand instead of parsed.
    let mut argv = vec![OsString::from("ld64.qld")];
    argv.extend_from_slice(&args);
    let mut options = match parse_darwin(&argv) {
        Ok(ParseOutcome::Link(options)) => options,
        other => panic!("{other:?}"),
    };
    let data = std::fs::read(&object).unwrap();
    assert!(
        options
            .inputs
            .iter()
            .any(|spec| spec.kind == InputKind::File(object.clone())),
        "the ld64 front end fills LinkOptions::inputs: {:?}",
        options.inputs
    );
    let rest: Vec<_> = options
        .inputs
        .drain(..)
        .filter(|spec| spec.kind != InputKind::File(object.clone()))
        .collect();
    options.inputs.clear();
    options.push_input(
        InputKind::bytes(object.to_string_lossy().into_owned(), data),
        InputAttrs::default(),
    );
    for spec in rest {
        options.push_input(spec.kind, spec.attrs);
    }
    let from_memory = qld::macho::link_to_bytes(&options, &Collect::new()).unwrap();
    assert!(!from_memory.is_empty());
    assert_eq!(from_memory, from_files);
}

#[test]
fn hello_executables() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "hello_executables",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let object = compile("hello", "hello.c", arch, &[]);
        let output = scratch("hello").join(format!("hello-{arch}"));
        let bytes = link_executable(arch, &[&object], &[], &output);

        let file = MachOFile::parse(&bytes, Source::new(&output)).unwrap();
        assert_eq!(file.header().file_type, MH_EXECUTE);
        let commands: Vec<u32> = load_commands(&bytes).iter().map(|c| c.0).collect();
        for wanted in [
            LC_DYLD_CHAINED_FIXUPS,
            LC_DYLD_EXPORTS_TRIE,
            LC_MAIN,
            LC_UUID,
            LC_LOAD_DYLIB,
        ] {
            assert!(
                commands.contains(&wanted),
                "{arch}: missing command {wanted:#x}"
            );
        }
        if arch == "arm64" {
            check_signature(&bytes);
        } else {
            assert!(!commands.contains(&LC_CODE_SIGNATURE));
        }

        if let Some(disassembly) = objdump(&["--macho", "-d"], &output) {
            assert!(
                disassembly.contains("symbol stub for: _printf"),
                "{arch}: {disassembly}"
            );
        }
        let imports = chained_imports(&bytes);
        let printf = import(&imports, "_printf");
        assert_eq!(printf.lib_ordinal, 1, "{arch}: {imports:?}");
        assert!(!printf.weak, "{arch}: {imports:?}");

        // Same inputs through ld64.lld: the same sections, exports, imports
        // and dylibs.
        if let Some(lld) = ld64_lld() {
            let reference = scratch("hello").join(format!("hello-{arch}-lld"));
            let status = Command::new(lld)
                .args(["-arch", arch, "-platform_version", "macos", "13.0", "13.0"])
                .arg("-syslibroot")
                .arg(syslibroot())
                .arg(&object)
                .arg("-lSystem")
                .arg("-o")
                .arg(&reference)
                .status()
                .unwrap();
            assert!(status.success());
            if let (Some(ours), Some(theirs)) = (summarize(&output), summarize(&reference)) {
                assert_eq!(ours, theirs, "{arch}: qld vs ld64.lld");
            }
        }

        if host_can_run(arch) {
            let stdout = run(&output).unwrap();
            assert_eq!(stdout, "hello from qld 3 42\n");
        }
    }
}

#[test]
fn output_is_deterministic() {
    if !clang_for("arm64") {
        skip(
            "output_is_deterministic",
            "clang cannot target arm64-apple-macos",
        );
        return;
    }
    let object = compile("determinism", "hello.c", "arm64", &[]);
    let root = syslibroot();
    let args = os(&[
        "-arch",
        "arm64",
        "-platform_version",
        "macos",
        "13.0",
        "13.0",
        "-syslibroot",
        root.to_str().unwrap(),
        object.to_str().unwrap(),
        "-lSystem",
        "-o",
        "determinism-out",
    ]);
    let mut outputs = Vec::new();
    for threads in [1, 2, 8] {
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(threads)
            .build()
            .unwrap();
        outputs.push(pool.install(|| link_bytes(&args).unwrap().0));
    }
    assert!(outputs.windows(2).all(|w| w[0] == w[1]));
}

/// Links `args` (an ld64 command line without `-o`) with qld into
/// `output`, and with `ld64.lld` into `output-lld` when it is available,
/// checking that both have the same sections, exports, imports and dylibs.
fn link_and_compare(args: &[String], output: &Path) -> Vec<u8> {
    let mut ours: Vec<OsString> = args.iter().map(OsString::from).collect();
    ours.push("-o".into());
    ours.push(output.into());
    let (bytes, _) = link_bytes(&ours).unwrap_or_else(|e| panic!("link failed: {e}"));
    std::fs::write(output, &bytes).unwrap();
    make_executable(output);
    if let Some(lld) = ld64_lld() {
        let mut reference = output.as_os_str().to_owned();
        reference.push("-lld");
        let reference = PathBuf::from(reference);
        let status = Command::new(lld)
            .args(args)
            .arg("-o")
            .arg(&reference)
            .status()
            .unwrap();
        assert!(status.success(), "ld64.lld failed on {args:?}");
        if let (Some(ours), Some(theirs)) = (summarize(output), summarize(&reference)) {
            assert_eq!(ours, theirs, "{}: qld vs ld64.lld", output.display());
        }
    }
    bytes
}

fn base_args(arch: &str) -> Vec<String> {
    [
        "-arch",
        arch,
        "-platform_version",
        "macos",
        "13.0",
        "13.0",
        "-syslibroot",
        syslibroot().to_str().unwrap(),
    ]
    .iter()
    .map(|s| (*s).to_owned())
    .collect()
}

fn strings(args: &[&str]) -> Vec<String> {
    args.iter().map(|s| (*s).to_owned()).collect()
}

#[test]
fn dylib_and_client() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "dylib_and_client",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let dir = scratch("dylib");
        let greet = compile("dylib", "greet.c", arch, &[]);
        let client = compile("dylib", "use_greet.c", arch, &[]);
        let lib_dir = dir.join(arch);
        std::fs::create_dir_all(&lib_dir).unwrap();

        let mut args = base_args(arch);
        args.extend(strings(&[
            "-dylib",
            "-install_name",
            "@rpath/libgreet.dylib",
            "-current_version",
            "1.2.3",
            greet.to_str().unwrap(),
            "-lSystem",
        ]));
        let lib = lib_dir.join("libgreet.dylib");
        let bytes = link_and_compare(&args, &lib);
        let file = MachOFile::parse(&bytes, Source::new(&lib)).unwrap();
        assert_eq!(file.header().file_type, qld::macho::read::consts::MH_DYLIB);
        if let Some(trie) = objdump(&["--macho", "--exports-trie"], &lib) {
            for name in ["_greet", "_greet_count", "_greet_tls", "_greet_weak"] {
                assert!(trie.contains(name), "{arch}: {name} not exported:\n{trie}");
            }
            assert!(
                !trie.contains("_greet_hidden"),
                "{arch}: hidden symbol exported"
            );
        }
        // References to an exported weak definition bind through weak
        // lookup (BIND_SPECIAL_DYLIB_WEAK_LOOKUP), so a strong definition
        // elsewhere can override it.
        let imports = chained_imports(&bytes);
        assert_eq!(
            import(&imports, "_greet_weak").lib_ordinal,
            -3,
            "{arch}: weak definition not bound through weak lookup: {imports:?}"
        );

        let mut args = base_args(arch);
        args.extend(strings(&[
            client.to_str().unwrap(),
            &format!("-L{}", lib_dir.display()),
            "-lgreet",
            "-rpath",
            "@executable_path",
            "-U",
            "_missing_weak",
            "-lSystem",
        ]));
        let exe = lib_dir.join("use_greet");
        let bytes = link_and_compare(&args, &exe);
        let imports = chained_imports(&bytes);
        assert!(
            import(&imports, "_missing_weak").weak,
            "{arch}: {imports:?}"
        );
        // libgreet is the first dylib on the command line.
        assert_eq!(
            import(&imports, "_greet").lib_ordinal,
            1,
            "{arch}: {imports:?}"
        );
        let file = MachOFile::parse(&bytes, Source::new(&exe)).unwrap();
        let first = file
            .load_commands()
            .map(Result::unwrap)
            .find(|c| c.cmd == LC_LOAD_DYLIB)
            .unwrap();
        assert_eq!(first.dylib().unwrap().name, b"@rpath/libgreet.dylib");
        if host_can_run(arch) {
            let stdout = run(&exe).unwrap();
            assert!(stdout.contains("hello, dylib"), "{stdout}");
        }
    }
}

#[test]
fn thread_local_variables() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "thread_local_variables",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let main = compile("tlv", "tlv.c", arch, &[]);
        let other = compile("tlv", "tlv_other.c", arch, &[]);
        let mut args = base_args(arch);
        args.extend(strings(&[
            main.to_str().unwrap(),
            other.to_str().unwrap(),
            "-lSystem",
        ]));
        let exe = scratch("tlv").join(format!("tlv-{arch}"));
        let bytes = link_and_compare(&args, &exe);
        let flags = MachOFile::parse(&bytes, Source::new(&exe))
            .unwrap()
            .header()
            .flags;
        assert_ne!(flags & qld::macho::read::consts::MH_HAS_TLV_DESCRIPTORS, 0);
        if let Some(contents) = objdump(&["--macho", "-s", "-j", "__thread_vars"], &exe) {
            // The offset fields: 0, 4, 0x50 and 0x48 into the template, in
            // descriptor order.
            assert!(contents.contains("__thread_vars"), "{contents}");
        }
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "42 3 tls 6\n");
        }
    }
}

#[test]
fn cxx_exceptions() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "cxx_exceptions",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let object = compile("exceptions", "exceptions.cpp", arch, &[]);
        let mut args = base_args(arch);
        args.extend(strings(&[object.to_str().unwrap(), "-lc++", "-lSystem"]));
        let exe = scratch("exceptions").join(format!("exceptions-{arch}"));
        link_and_compare(&args, &exe);
        if let Some(unwind) = objdump(&["--macho", "--unwind-info"], &exe) {
            assert!(
                unwind.contains("Personality functions: (count = 1)"),
                "{unwind}"
            );
            assert!(unwind.contains("LSDA descriptors:"), "{unwind}");
        }
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "caught 84 after 10 cleanups\n");
        }
    }
}

/// The address of symbol `name` in `file`, from `llvm-nm`.
#[test]
fn dwarf_unwind_information() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "dwarf_unwind_information",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let main = compile("dwarf", "dwarf_unwind.cpp", arch, &[]);
        let asm = compile("dwarf", &format!("dwarf-{arch}.s"), arch, &[]);
        let mut args = base_args(arch);
        args.extend(strings(&[
            main.to_str().unwrap(),
            asm.to_str().unwrap(),
            "-lc++",
            "-lSystem",
        ]));
        let exe = scratch("dwarf").join(format!("dwarf-{arch}"));
        let bytes = link_and_compare(&args, &exe);
        let function = symbol_address(&bytes, "_call_through").unwrap();
        // The FDE in __eh_frame covers call_through.
        if let Ok(frames) = Command::new(tool("dwarfdump"))
            .arg("--eh-frame")
            .arg(&exe)
            .output()
            && frames.status.success()
        {
            let frames = String::from_utf8_lossy(&frames.stdout);
            let wanted = format!("pc={function:08x}...");
            assert!(
                frames.contains(&wanted),
                "{arch}: no FDE for {wanted}:\n{frames}"
            );
        }
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "dwarf 7\n");
        }
    }
}

#[test]
fn dead_stripping() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "dead_stripping",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let object = compile("dead_strip", "dead_strip.c", arch, &[]);
        let mut args = base_args(arch);
        args.extend(strings(&[
            object.to_str().unwrap(),
            "-dead_strip",
            "-lSystem",
        ]));
        let exe = scratch("dead_strip").join(format!("dead_strip-{arch}"));
        let bytes = link_and_compare(&args, &exe);
        for (name, kept) in [
            ("_main", true),
            ("_used_data", true),
            ("_kept_by_attribute", true),
            ("_unused_function", false),
            ("_unused_caller", false),
            ("_unused_data", false),
        ] {
            assert_eq!(
                symbol_address(&bytes, name).is_some(),
                kept,
                "{arch}: {name} should {}be kept",
                if kept { "" } else { "not " }
            );
        }
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "stripped 42\n");
        }
    }
}

/// The objects and symbols of `dsymutil --dump-debug-map`, without
/// timestamps.
fn debug_map(binary: &Path) -> Option<Vec<String>> {
    let output = Command::new("dsymutil")
        .arg("--dump-debug-map")
        .arg(binary)
        .output()
        .ok()?;
    output.status.success().then(|| {
        String::from_utf8_lossy(&output.stdout)
            .lines()
            .filter(|l| l.contains("filename:") || l.contains("sym:"))
            .map(|l| l.split(", binAddr").next().unwrap_or(l).trim().to_owned())
            .collect()
    })
}

#[test]
fn stabs_debug_map() {
    let arch = "arm64";
    if !clang_for(arch) {
        skip("stabs_debug_map", "clang cannot target arm64-apple-macos");
        return;
    }
    let dir = scratch("stabs");
    let main = compile("stabs", "tlv.c", arch, &["-g"]);
    let other = compile("stabs", "tlv_other.c", arch, &["-g"]);
    let archive = dir.join("libother.a");
    if !make_archive(&archive, &[&other]) {
        skip("stabs_debug_map", "neither llvm-ar nor libtool works");
        return;
    }
    let mut args = base_args(arch);
    args.extend(strings(&[
        main.to_str().unwrap(),
        archive.to_str().unwrap(),
        "-lSystem",
    ]));
    let exe = dir.join("stabs");
    link_and_compare(&args, &exe);
    let Some(map) = debug_map(&exe) else {
        skip("stabs_debug_map", "dsymutil missing");
        return;
    };
    let joined = map.join("\n");
    assert!(joined.contains("tlv-arm64.o'"), "{joined}");
    assert!(
        joined.contains("libother.a(tlv_other-arm64.o)'"),
        "{joined}"
    );
    for symbol in [
        "sym: _main, objAddr: 0x0",
        "sym: _tlv_other",
        "sym: _tlv_zero",
    ] {
        assert!(joined.contains(symbol), "{symbol} missing:\n{joined}");
    }
    let reference = PathBuf::from(format!("{}-lld", exe.display()));
    if let Some(theirs) = debug_map(&reference).filter(|_| reference.exists()) {
        let normalize = |lines: Vec<String>| {
            let mut lines: Vec<String> = lines
                .into_iter()
                .map(|l| l.replace(&reference.display().to_string(), ""))
                .collect();
            lines.sort();
            lines
        };
        assert_eq!(
            normalize(map),
            normalize(theirs),
            "debug map: qld vs ld64.lld"
        );
    }
}

#[test]
fn universal_binary() {
    let lipo = tool("lipo");
    if !clang_for("arm64") || !clang_for("x86_64") || Command::new(&lipo).output().is_err() {
        skip(
            "universal_binary",
            "clang for both architectures or lipo missing",
        );
        return;
    }
    let dir = scratch("universal");
    // A fat input object.
    let hello_arm = compile("universal", "hello.c", "arm64", &[]);
    let hello_x86 = compile("universal", "hello.c", "x86_64", &[]);
    let fat_object = dir.join("hello-fat.o");
    assert!(tool_works(
        &lipo,
        &[
            "-create",
            hello_arm.to_str().unwrap(),
            hello_x86.to_str().unwrap(),
            "-output",
            fat_object.to_str().unwrap()
        ]
    ));
    let root = syslibroot();
    let args = os(&[
        "-arch",
        "arm64",
        "-arch",
        "x86_64",
        "-platform_version",
        "macos",
        "13.0",
        "13.0",
        "-syslibroot",
        root.to_str().unwrap(),
        fat_object.to_str().unwrap(),
        "-lSystem",
    ]);
    let (bytes, _) = link_bytes(&args).unwrap();
    let exe = dir.join("hello-universal");
    std::fs::write(&exe, &bytes).unwrap();
    make_executable(&exe);

    let fat = qld::macho::read::FatFile::parse(&bytes, Source::new(&exe)).unwrap();
    assert_eq!(fat.slices().len(), 2);
    for slice in fat.slices() {
        let file = MachOFile::parse(slice.data, Source::new(&exe)).unwrap();
        assert_eq!(file.header().file_type, MH_EXECUTE);
        if slice.arch == qld::macho::read::Arch::ARM64 {
            assert_eq!(slice.offset % 0x4000, 0);
            assert_eq!(slice.align, 14);
            check_signature(slice.data);
        } else {
            assert_eq!(slice.offset % 0x1000, 0);
        }
    }
    let info = Command::new(&lipo).arg("-info").arg(&exe).output().unwrap();
    let info = String::from_utf8_lossy(&info.stdout);
    assert!(info.contains("x86_64") && info.contains("arm64"), "{info}");

    // Each slice matches a thin link of the same architecture.
    for (arch, slice_arch) in [
        ("arm64", qld::macho::read::Arch::ARM64),
        ("x86_64", qld::macho::read::Arch::X86_64),
    ] {
        let mut thin = os(&["-arch", arch]);
        thin.extend(args.iter().skip(4).cloned());
        let (thin_bytes, _) = link_bytes(&thin).unwrap();
        let slice = fat.select(slice_arch).unwrap();
        assert!(
            slice.data == thin_bytes.as_slice(),
            "{arch} slice differs from a thin link"
        );
    }
    if cfg!(target_os = "macos") && host_can_run("arm64") {
        assert_eq!(run(&exe).unwrap(), "hello from qld 3 42\n");
    }
}

#[test]
fn range_extension_thunks() {
    let arch = "arm64";
    if !clang_for(arch) {
        skip(
            "range_extension_thunks",
            "clang cannot target arm64-apple-macos",
        );
        return;
    }
    let dir = scratch("thunks");
    let main = compile("thunks", "far_main.c", arch, &[]);
    let far = compile("thunks", "far.c", arch, &[]);
    let filler_source = dir.join("filler.s");
    std::fs::write(
        &filler_source,
        ".text\n.globl _filler\n.p2align 2\n_filler:\n.space 0x8200000\nret\n.subsections_via_symbols\n",
    )
    .unwrap();
    let filler = dir.join("filler.o");
    assert!(tool_works(
        "clang",
        &[
            "--target=arm64-apple-macos13",
            "-c",
            filler_source.to_str().unwrap(),
            "-o",
            filler.to_str().unwrap()
        ]
    ));
    let mut args = base_args(arch);
    args.extend(strings(&[
        main.to_str().unwrap(),
        filler.to_str().unwrap(),
        far.to_str().unwrap(),
        "-u",
        "_filler",
        "-lSystem",
    ]));
    // Hashing 130 MiB in a debug build is slow; the signature has its own
    // tests, except on macOS where the binary runs.
    if !host_can_run(arch) {
        args.push("-no_adhoc_codesign".to_owned());
    }
    let exe = dir.join("far");
    let mut ours: Vec<OsString> = args.iter().map(OsString::from).collect();
    ours.push("-o".into());
    ours.push((&exe).into());
    let (bytes, _) = link_bytes(&ours).unwrap_or_else(|e| panic!("{e}"));
    std::fs::write(&exe, &bytes).unwrap();
    make_executable(&exe);

    // main's calls go through thunks (adrp, add, br x16) that reach their
    // targets.
    // (Decoded here: llvm-objdump is slow on 130 MiB of code.)
    {
        let main_address = symbol_address(&bytes, "_main").unwrap();
        let far_address = symbol_address(&bytes, "_far_function").unwrap();
        assert!(far_address - main_address > 128 << 20);
        // __TEXT starts at file offset 0.
        let text = 0x1_0000_0000u64;
        let word = |address: u64| u32_le(&bytes, (address - text) as usize);
        let mut branches = Vec::new();
        for offset in (0..0x30).step_by(4) {
            let insn = word(main_address + offset);
            if insn & 0xfc00_0000 == 0x9400_0000 {
                let delta = (((insn & 0x03ff_ffff) << 6) as i32 >> 4) as i64;
                branches.push((main_address + offset).wrapping_add(delta as u64));
            }
        }
        assert_eq!(branches.len(), 2, "branches from main: {branches:x?}");
        let mut destinations = Vec::new();
        for thunk in branches {
            assert!(thunk.abs_diff(main_address) < 128 << 20);
            let adrp = word(thunk);
            let add = word(thunk + 4);
            assert_eq!(adrp & 0x9f00_001f, 0x9000_0010, "adrp x16 at {thunk:#x}");
            assert_eq!(add & 0xffc0_03ff, 0x9100_0210, "add x16, x16 at {thunk:#x}");
            assert_eq!(word(thunk + 8), 0xd61f_0200, "br x16 at {thunk:#x}");
            let pages = ((((adrp >> 29) & 3) | (((adrp >> 5) & 0x7ffff) << 2)) << 11) as i32 >> 11;
            let target = ((thunk & !0xfff) as i64 + ((pages as i64) << 12)) as u64
                + u64::from((add >> 10) & 0xfff);
            destinations.push(target);
        }
        assert!(destinations.contains(&far_address), "{destinations:x?}");
    }
    if host_can_run(arch) {
        assert_eq!(run(&exe).unwrap(), "near\nfar\n");
    }
}

#[test]
fn legacy_dyld_info() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "legacy_dyld_info",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let object = compile("legacy", "hello.c", arch, &[]);
        let root = syslibroot();
        let mut args = os(&[
            "-arch",
            arch,
            "-platform_version",
            "macos",
            "11.0",
            "11.0",
            "-syslibroot",
            root.to_str().unwrap(),
            object.to_str().unwrap(),
            "-lSystem",
        ]);
        let (bytes, _) = link_bytes(&args).unwrap();
        let commands: Vec<u32> = load_commands(&bytes).iter().map(|c| c.0).collect();
        assert!(commands.contains(&qld::macho::read::consts::LC_DYLD_INFO_ONLY));
        assert!(!commands.contains(&LC_DYLD_CHAINED_FIXUPS));
        let exe = scratch("legacy").join(format!("hello-{arch}"));
        std::fs::write(&exe, &bytes).unwrap();
        make_executable(&exe);
        if let Some(tables) = objdump(&["--macho", "--bind", "--rebase"], &exe) {
            assert!(tables.contains("_printf"), "{arch}: {tables}");
            assert!(
                tables
                    .lines()
                    .any(|l| l.contains("__data") && l.contains("pointer")),
                "{arch}: {tables}"
            );
        }
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "hello from qld 3 42\n");
        }

        // -fixup_chains overrides the deployment target.
        args.push("-fixup_chains".into());
        let (bytes, _) = link_bytes(&args).unwrap();
        let commands: Vec<u32> = load_commands(&bytes).iter().map(|c| c.0).collect();
        assert!(commands.contains(&LC_DYLD_CHAINED_FIXUPS));
    }
}

#[test]
fn bundles_and_export_lists() {
    let arch = "arm64";
    if !clang_for(arch) {
        skip(
            "bundles_and_export_lists",
            "clang cannot target arm64-apple-macos",
        );
        return;
    }
    let dir = scratch("bundle");
    let greet = compile("bundle", "greet.c", arch, &[]);
    let list = dir.join("exports.txt");
    std::fs::write(&list, "# only greet\n_greet\n").unwrap();
    let plist = dir.join("Info.plist");
    std::fs::write(&plist, "<plist>qld</plist>\n").unwrap();
    let mut args = base_args(arch);
    args.extend(strings(&[
        "-bundle",
        greet.to_str().unwrap(),
        "-exported_symbols_list",
        list.to_str().unwrap(),
        "-sectcreate",
        "__TEXT",
        "__info_plist",
        plist.to_str().unwrap(),
        "-lSystem",
    ]));
    let bundle = dir.join("greet.bundle");
    let bytes = link_and_compare(&args, &bundle);
    let needle = b"<plist>qld</plist>\n";
    assert!(
        bytes.windows(needle.len()).any(|w| w == needle),
        "-sectcreate contents missing"
    );
    let file = MachOFile::parse(&bytes, Source::new(&bundle)).unwrap();
    assert_eq!(file.header().file_type, qld::macho::read::consts::MH_BUNDLE);
    if let Some(trie) = objdump(&["--macho", "--exports-trie"], &bundle) {
        assert!(trie.contains("_greet"), "{trie}");
        assert!(!trie.contains("_greet_count"), "{trie}");
        assert!(!trie.contains("_greet_weak"), "{trie}");
    }
    // Symbols left out of the export list stay in the symbol table, but not
    // as externals.
    let table = symbols(&bytes);
    let count = table
        .iter()
        .find(|s| s.is_defined() && s.name == "_greet_count")
        .unwrap_or_else(|| panic!("_greet_count missing: {table:?}"));
    assert!(!count.is_external(), "{count:?}");
    let greet = table
        .iter()
        .find(|s| s.is_defined() && s.name == "_greet")
        .unwrap_or_else(|| panic!("_greet missing: {table:?}"));
    assert!(greet.is_external(), "{greet:?}");
}

#[test]
fn objective_c() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "objective_c",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let object = compile("objc", "objc.m", arch, &[]);
        for dead_strip in [false, true] {
            let mut args = base_args(arch);
            args.extend(strings(&[object.to_str().unwrap(), "-lobjc", "-lSystem"]));
            if dead_strip {
                args.push("-dead_strip".to_owned());
            }
            let exe = scratch("objc").join(format!("objc-{arch}-{dead_strip}"));
            link_and_compare(&args, &exe);
            if let Some(headers) = objdump(&["--macho", "--section-headers"], &exe) {
                for section in ["__objc_classlist", "__objc_catlist", "__objc_imageinfo"] {
                    assert!(headers.contains(section), "{arch}: {section}:\n{headers}");
                }
            }
            if host_can_run(arch) {
                assert_eq!(run(&exe).unwrap(), "objc 42\n");
            }
        }
    }
}

/// `_objc_msgSend$<selector>` calls (Apple clang's selector stubs, which
/// upstream clang does not emit, hence the assembly) get stubs, selector
/// references and method names synthesized by the linker.
#[test]
fn objective_c_selector_stubs() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "objective_c_selector_stubs",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let object = compile(
            "selector_stubs",
            &format!("selector_stubs-{arch}.s"),
            arch,
            &[],
        );
        for dead_strip in [false, true] {
            let mut args = base_args(arch);
            args.extend(strings(&[object.to_str().unwrap(), "-lobjc", "-lSystem"]));
            if dead_strip {
                args.push("-dead_strip".to_owned());
            }
            let exe = scratch("selector_stubs").join(format!("stubs-{arch}-{dead_strip}"));
            let bytes = link_and_compare(&args, &exe);
            let file = MachOFile::parse(&bytes, Source::new(&exe)).unwrap();
            let mut sections = BTreeSet::new();
            for command in file.load_commands() {
                if let Ok(segment) = command.unwrap().segment() {
                    for section in segment.sections.iter() {
                        sections.insert((
                            String::from_utf8_lossy(section.segname).into_owned(),
                            String::from_utf8_lossy(section.sectname).into_owned(),
                            section.size,
                        ));
                    }
                }
            }
            let stub_size = if arch == "arm64" { 32 } else { 13 };
            for wanted in [
                ("__TEXT", "__objc_stubs", 2 * stub_size),
                ("__DATA", "__objc_selrefs", 16),
                ("__TEXT", "__objc_methname", 27),
            ] {
                let wanted = (wanted.0.to_owned(), wanted.1.to_owned(), wanted.2);
                assert!(
                    sections.contains(&wanted),
                    "{arch}: {wanted:?} in {sections:?}"
                );
            }
            let table = symbols(&bytes);
            for stub in ["_objc_msgSend$description", "_objc_msgSend$initWithCount:"] {
                let symbol = table
                    .iter()
                    .find(|s| s.is_defined() && s.name == stub)
                    .unwrap_or_else(|| panic!("{arch}: {stub} missing: {table:?}"));
                assert!(!symbol.is_external(), "{arch}: {symbol:?}");
            }
            let imports = chained_imports(&bytes);
            assert!(
                imports.iter().any(|i| i.name == "_objc_msgSend"),
                "{arch}: {imports:?}"
            );
            if host_can_run(arch) {
                run(&exe).unwrap();
            }
        }
    }
}

/// On macOS: Apple clang links through the `qld` binary (as `ld64.qld`,
/// with `-fuse-ld`) against the real SDK, and the program runs.
#[test]
fn clang_driver_uses_qld() {
    if !cfg!(target_os = "macos") {
        eprintln!("skipping clang_driver_uses_qld: needs macOS");
        return;
    }
    let dir = scratch("driver");
    let linker = dir.join("ld64.qld");
    let _ = std::fs::remove_file(&linker);
    #[cfg(unix)]
    std::os::unix::fs::symlink(env!("CARGO_BIN_EXE_qld"), &linker).unwrap();
    for (source, expected) in [
        ("hello.c", "hello from qld 3 42\n"),
        ("exceptions.cpp", "caught 84 after 10 cleanups\n"),
    ] {
        let output = dir.join(source.replace('.', "-"));
        let compiler = if source.ends_with(".cpp") {
            "clang++"
        } else {
            "clang"
        };
        let result = Command::new(compiler)
            .arg(format!("-fuse-ld={}", linker.display()))
            .arg(data_dir().join(source))
            .arg("-o")
            .arg(&output)
            .output()
            .unwrap();
        let stderr = String::from_utf8_lossy(&result.stderr);
        // Until `crate::link` dispatches Mach-O targets to `macho::link`,
        // the binary reports Mach-O output as not implemented.
        if !result.status.success() && stderr.contains("not implemented yet: MachO output") {
            skip(
                "clang_driver_uses_qld",
                "the qld binary does not link Mach-O yet",
            );
            return;
        }
        assert!(
            result.status.success(),
            "{compiler} -fuse-ld=ld64.qld {source}: {stderr}"
        );
        assert_eq!(run(&output).unwrap(), expected);
    }
}

#[test]
fn linker_options_from_archive_members() {
    let arch = "arm64";
    if !clang_for(arch) {
        skip(
            "linker_options_from_archive_members",
            "clang cannot target arm64-apple-macos",
        );
        return;
    }
    let dir = scratch("autolink");
    let member = compile("autolink", "autolink-arm64.s", arch, &[]);
    let archive = dir.join("librelease.a");
    if !make_archive(&archive, &[&member]) {
        skip(
            "linker_options_from_archive_members",
            "neither llvm-ar nor libtool works",
        );
        return;
    }
    let main = dir.join("main.c");
    std::fs::write(
        &main,
        "void release(void *);\nint main(void) { release(0); return 0; }\n",
    )
    .unwrap();
    let main_object = dir.join("main.o");
    assert!(tool_works(
        "clang",
        &[
            "--target=arm64-apple-macos13",
            "-c",
            main.to_str().unwrap(),
            "-o",
            main_object.to_str().unwrap()
        ]
    ));
    let mut args = base_args(arch);
    args.extend(strings(&[
        main_object.to_str().unwrap(),
        archive.to_str().unwrap(),
        "-lSystem",
    ]));
    let exe = dir.join("autolink");
    link_and_compare(&args, &exe);
    if let Some(dylibs) = objdump(&["--macho", "--dylibs-used"], &exe) {
        assert!(dylibs.contains("libc++"), "{dylibs}");
    }
}

#[test]
fn order_file() {
    let arch = "arm64";
    if !clang_for(arch) {
        skip("order_file", "clang cannot target arm64-apple-macos");
        return;
    }
    let dir = scratch("order");
    let object = compile("order", "dead_strip.c", arch, &[]);
    let order = dir.join("order.txt");
    std::fs::write(
        &order,
        "# callers first\narm64:_unused_caller\n_main\nx86_64:_unused_function\n",
    )
    .unwrap();
    let mut args = base_args(arch);
    args.extend(strings(&[
        object.to_str().unwrap(),
        "-order_file",
        order.to_str().unwrap(),
        "-lSystem",
    ]));
    let exe = dir.join("ordered");
    let bytes = link_and_compare(&args, &exe);
    {
        let caller = symbol_address(&bytes, "_unused_caller").unwrap();
        let main = symbol_address(&bytes, "_main").unwrap();
        let function = symbol_address(&bytes, "_unused_function").unwrap();
        assert!(
            caller < main && main < function,
            "{caller:#x} {main:#x} {function:#x}"
        );
    }
}

#[test]
fn undefined_symbols_are_reported() {
    if !clang_for("arm64") {
        skip(
            "undefined_symbols_are_reported",
            "clang cannot target arm64-apple-macos",
        );
        return;
    }
    let dir = scratch("undefined");
    let source = dir.join("undefined.c");
    std::fs::write(
        &source,
        "void missing(void); int main(void) { missing(); return 0; }\n",
    )
    .unwrap();
    let object = dir.join("undefined.o");
    assert!(tool_works(
        "clang",
        &[
            "--target=arm64-apple-macos13",
            "-c",
            source.to_str().unwrap(),
            "-o",
            object.to_str().unwrap()
        ]
    ));
    let root = data_dir().join("sdk");
    let base = os(&[
        "-arch",
        "arm64",
        "-syslibroot",
        root.to_str().unwrap(),
        object.to_str().unwrap(),
        "-lSystem",
    ]);
    let error = link_bytes(&base).unwrap_err();
    assert!(error.contains("undefined symbol: _missing"), "{error}");

    let mut lookup = base.clone();
    lookup.extend(os(&["-undefined", "dynamic_lookup"]));
    let (bytes, _) = link_bytes(&lookup).unwrap();
    let path = dir.join("lookup");
    std::fs::write(&path, &bytes).unwrap();
    // BIND_SPECIAL_DYLIB_FLAT_LOOKUP.
    let imports = chained_imports(&bytes);
    assert_eq!(import(&imports, "_missing").lib_ordinal, -2, "{imports:?}");
}

/// The sections of a Mach-O image, in load command order, as (segment,
/// section, file offset, size).
fn section_list(data: &[u8]) -> Vec<(String, String, usize, usize)> {
    let file = MachOFile::parse(data, Source::new(Path::new("out"))).unwrap();
    let mut sections = Vec::new();
    for command in file.load_commands() {
        let command = command.unwrap();
        if command.cmd == qld::macho::read::consts::LC_SEGMENT_64 {
            for section in command.segment().unwrap().sections.iter() {
                sections.push((
                    String::from_utf8_lossy(section.segname).into_owned(),
                    String::from_utf8_lossy(section.sectname).into_owned(),
                    section.offset as usize,
                    section.size as usize,
                ));
            }
        }
    }
    sections
}

/// The names of the sections of a Mach-O image, in load command order.
fn section_names(data: &[u8]) -> Vec<String> {
    section_list(data).into_iter().map(|s| s.1).collect()
}

/// The contents of section `name` (the first with that name).
fn section_contents<'a>(data: &'a [u8], name: &str) -> Option<&'a [u8]> {
    section_list(data)
        .into_iter()
        .find(|s| s.1 == name)
        .map(|(_, _, offset, size)| &data[offset..offset + size])
}

/// How many times `needle` occurs in `haystack`.
fn occurrences(haystack: &[u8], needle: &[u8]) -> usize {
    haystack
        .windows(needle.len())
        .filter(|w| *w == needle)
        .count()
}

/// Stores of immediates to statics (x86_64 `SIGNED_1`/`SIGNED_4` against a
/// local symbol that starts its atom) reach the static itself, not the
/// bytes before it.
#[test]
fn pcrel_immediate_stores() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "pcrel_immediate_stores",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let object = compile("statics", "statics.c", arch, &[]);
        let mut args = base_args(arch);
        args.extend(strings(&[object.to_str().unwrap(), "-lSystem"]));
        let exe = scratch("statics").join(format!("statics-{arch}"));
        link_and_compare(&args, &exe);
        if let Some(disassembly) = objdump(&["--macho", "-d"], &exe) {
            // llvm-objdump symbolizes RIP-relative operands: an operand one
            // byte before the static shows as `_flag-1`.
            for name in ["_flag", "_counter", "_wide"] {
                assert!(
                    !disassembly.contains(&format!("{name}-")),
                    "{arch}: {name} is missed:\n{disassembly}"
                );
            }
        }
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "1 12345678 1122334455667788\n");
        }
    }
}

/// An exception of a type with internal linkage: the LSDA's type table
/// reaches the local type info through a `__got` slot (`POINTER_TO_GOT` or
/// `X86_64_RELOC_GOT` against a local symbol), which must exist although
/// no global symbol owns it. Also through `-r` twice, where the type info
/// name is a local symbol with the arm64 top-bit addend.
#[test]
fn local_type_info() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "local_type_info",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let dir = scratch("local_rtti");
        let object = compile("local_rtti", "local_rtti.cpp", arch, &[]);
        let exceptions = compile("local_rtti", "exceptions.cpp", arch, &[]);
        let mut args = base_args(arch);
        args.extend(strings(&[object.to_str().unwrap(), "-lc++", "-lSystem"]));
        let exe = dir.join(format!("local_rtti-{arch}"));
        link_and_compare(&args, &exe);
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "local 7\n");
        }
        // Private-extern type infos become local with -r; a second -r and
        // a link read them back.
        let once = dir.join(format!("once-{arch}.o"));
        link_relocatable(arch, &[&exceptions], &once);
        let twice = dir.join(format!("twice-{arch}.o"));
        link_relocatable(arch, &[&once], &twice);
        let mut args = base_args(arch);
        args.extend(strings(&[twice.to_str().unwrap(), "-lc++", "-lSystem"]));
        let exe = dir.join(format!("exceptions-twice-{arch}"));
        link_and_compare(&args, &exe);
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "caught 84 after 10 cleanups\n");
        }
    }
}

/// Identical C strings and floating-point literals from two objects are
/// merged, as ld64 and lld merge them: one copy in the output, and both
/// objects' references resolve to it.
#[test]
fn literal_deduplication() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "literal_deduplication",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let a = compile("literals", "literals_a.c", arch, &[]);
        let b = compile("literals", "literals_b.c", arch, &[]);
        let mut args = base_args(arch);
        args.extend(strings(&[
            a.to_str().unwrap(),
            b.to_str().unwrap(),
            "-lSystem",
        ]));
        let exe = scratch("literals").join(format!("literals-{arch}"));
        let bytes = link_and_compare(&args, &exe);
        let cstrings = section_contents(&bytes, "__cstring").expect("__cstring");
        assert_eq!(
            occurrences(cstrings, b"shared string\0"),
            1,
            "{arch}: {cstrings:?}"
        );
        assert_eq!(occurrences(cstrings, b"only in a\0"), 1, "{arch}");
        if arch == "x86_64" {
            // The multiplier is a `__literal8` in both objects.
            let literals = section_contents(&bytes, "__literal8").expect("__literal8");
            assert_eq!(
                // 3.14159265358979
                occurrences(literals, &0x4009_21fb_5444_2d11_u64.to_le_bytes()),
                1,
                "{arch}: {literals:?}"
            );
        }
        let reference = PathBuf::from(format!("{}-lld", exe.display()));
        if let Ok(theirs) = std::fs::read(&reference) {
            assert_eq!(
                section_contents(&theirs, "__cstring").map(<[u8]>::len),
                Some(cstrings.len()),
                "{arch}: __cstring size, qld vs ld64.lld"
            );
        }
        if host_can_run(arch) {
            assert_eq!(
                run(&exe).unwrap(),
                "shared string shared string 1 only in a 9.42478\n"
            );
        }
    }
}

/// Merges `objects` with `-r` into `output` and returns the bytes.
fn link_relocatable(arch: &str, objects: &[&Path], output: &Path) -> Vec<u8> {
    let mut args = os(&[
        "-arch",
        arch,
        "-platform_version",
        "macos",
        "13.0",
        "13.0",
        "-r",
    ]);
    for object in objects {
        args.push(object.into());
    }
    args.extend(os(&["-o", output.to_str().unwrap()]));
    let (bytes, _) = link_bytes(&args).unwrap_or_else(|e| panic!("-r failed: {e}"));
    std::fs::write(output, &bytes).unwrap();
    bytes
}

/// The external symbols of an image or object: (name, defined), sorted.
fn external_symbols(data: &[u8]) -> Vec<(String, bool)> {
    let mut out: Vec<(String, bool)> = symbols(data)
        .into_iter()
        .filter(Nlist::is_external)
        .map(|s| {
            let defined = s.n_type & 0x0e != 0;
            (s.name, defined)
        })
        .collect();
    out.sort();
    out
}

/// Checks a relocatable object with qld's reader: an `MH_OBJECT` whose
/// relocations all decode and pair, and whose sections atomize.
fn check_object(data: &[u8], subsections: bool) {
    use qld::macho::read::{Atomization, ObjectFile};
    let file = MachOFile::parse(data, Source::new(Path::new("merged.o"))).unwrap();
    assert_eq!(file.header().file_type, qld::macho::read::consts::MH_OBJECT);
    assert_eq!(
        file.header().flags & qld::macho::read::consts::MH_SUBSECTIONS_VIA_SYMBOLS != 0,
        subsections
    );
    let object = ObjectFile::parse(data, Source::new(Path::new("merged.o"))).unwrap();
    Atomization::new(&object).unwrap();
    for index in 0..object.sections().len() {
        for relocation in object.paired_relocations(index).unwrap() {
            relocation.unwrap();
        }
    }
}

/// `-r`: two C++ objects (weak definitions in both, exceptions, statics, a
/// thread-local, string literals) merged into one object, which is then
/// linked into an executable. The executable matches a link of the two
/// objects themselves; `ld64.lld` (which has no `-r`) links the merged
/// object too and is compared. On macOS the executable runs, the merged
/// object's external symbols match Apple's `ld -r`, and Apple's linker
/// links the merged object into a program that runs as well.
#[test]
fn relocatable_output() {
    const EXPECTED: &str = "hello from a total -50 counter 3 local 5 tls 7 flag 1\n";
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "relocatable_output",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let dir = scratch("relocatable");
        let a = compile("relocatable", "relocatable_a.cpp", arch, &[]);
        let b = compile("relocatable", "relocatable_b.cpp", arch, &[]);
        let merged = dir.join(format!("merged-{arch}.o"));
        let bytes = link_relocatable(arch, &[&a, &b], &merged);
        check_object(&bytes, true);
        assert_eq!(
            link_relocatable(arch, &[&a, &b], &merged),
            bytes,
            "{arch}: -r output is not deterministic"
        );

        // One copy of each weak definition; undefined references stay.
        let externals = external_symbols(&bytes);
        let count = |name: &str| externals.iter().filter(|s| s.0 == name).count();
        assert_eq!(count("__ZNK3BoxIiE5twiceEv"), 1, "{arch}: {externals:?}");
        assert_eq!(count("__ZZ14shared_countervE5count"), 1, "{arch}");
        for undefined in ["___cxa_throw", "___gxx_personality_v0", "_printf"] {
            assert!(
                externals.iter().any(|s| s.0 == undefined && !s.1),
                "{arch}: {undefined} not undefined: {externals:?}"
            );
        }
        let names = section_names(&bytes);
        for wanted in [
            "__text",
            "__compact_unwind",
            "__gcc_except_tab",
            "__cstring",
        ] {
            assert!(
                names.iter().any(|n| n == wanted),
                "{arch}: no {wanted} in {names:?}"
            );
        }
        let cstrings = section_contents(&bytes, "__cstring").unwrap();
        assert_eq!(occurrences(cstrings, b"hello from a\0"), 1, "{arch}");

        // Linked, the merged object gives the program the objects give.
        let mut args = base_args(arch);
        args.extend(strings(&[merged.to_str().unwrap(), "-lc++", "-lSystem"]));
        let exe = dir.join(format!("merged-{arch}"));
        link_and_compare(&args, &exe);
        let mut args = base_args(arch);
        args.extend(strings(&[
            a.to_str().unwrap(),
            b.to_str().unwrap(),
            "-lc++",
            "-lSystem",
        ]));
        let direct = dir.join(format!("direct-{arch}"));
        link_and_compare(&args, &direct);
        if let (Some(ours), Some(theirs)) = (summarize(&exe), summarize(&direct)) {
            assert_eq!(ours, theirs, "{arch}: linked from -r vs from the objects");
            // `Box<int>::twice` is `.weak_def_can_be_hidden` in both
            // objects: not exported, as with ld64.
            assert!(
                !ours.exports.contains("__ZNK3BoxIiE5twiceEv"),
                "{arch}: {:?}",
                ours.exports
            );
        }
        if let (Some(ours), Some(theirs)) = (
            objdump(&["--macho", "--section-headers"], &exe),
            objdump(&["--macho", "--section-headers"], &direct),
        ) {
            // The same sections at the same addresses (the stubs and
            // `__got` slots may come in another order).
            let body = |text: &str| text.lines().skip(1).collect::<Vec<_>>().join("\n");
            assert_eq!(body(&ours), body(&theirs), "{arch}: layouts differ");
        }
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), EXPECTED, "{arch}");
        }

        if cfg!(target_os = "macos") && host_can_run(arch) {
            apple_relocatable(arch, &[&a, &b], &bytes, &merged, EXPECTED);
        }
    }
}

/// On macOS: compares the external symbols of `ours` (qld's `-r` output,
/// at `merged`) with Apple's `ld -r` of the same `objects`, then links
/// `merged` with Apple's linker and runs it.
fn apple_relocatable(arch: &str, objects: &[&Path], ours: &[u8], merged: &Path, expected: &str) {
    let dir = merged.parent().unwrap();
    let apple = dir.join(format!("apple-{arch}.o"));
    let status = Command::new("ld")
        .args(["-r", "-arch", arch])
        .args(objects)
        .arg("-o")
        .arg(&apple)
        .status();
    match status {
        Ok(status) if status.success() => {
            let theirs = std::fs::read(&apple).unwrap();
            assert_eq!(
                external_symbols(ours),
                external_symbols(&theirs),
                "{arch}: external symbols, qld -r vs Apple ld -r"
            );
        }
        _ => skip("relocatable_output", "Apple ld -r failed or is missing"),
    }
    let exe = dir.join(format!("apple-linked-{arch}"));
    let status = Command::new("clang++")
        .args(["-arch", arch])
        .arg(merged)
        .arg("-o")
        .arg(&exe)
        .status()
        .unwrap();
    assert!(
        status.success(),
        "{arch}: Apple's linker rejects qld's -r output"
    );
    assert_eq!(run(&exe).unwrap(), expected, "{arch}: linked by Apple's ld");
}

/// More `-r` inputs: Objective-C metadata (selector references into merged
/// method names), `LC_LINKER_OPTION` carried to the final link, x86_64
/// `SIGNED_n` stores, private externs (made local, or kept with
/// `-keep_private_externs`), and debug information (dropped, with a
/// warning).
#[test]
fn relocatable_variants() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "relocatable_variants",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let dir = scratch("relocatable_variants");

        // Objective-C, and statics stored to with immediates.
        let objc = compile("relocatable_variants", "objc.m", arch, &[]);
        let merged = dir.join(format!("objc-{arch}.o"));
        let bytes = link_relocatable(arch, &[&objc], &merged);
        check_object(&bytes, true);
        let mut args = base_args(arch);
        args.extend(strings(&[merged.to_str().unwrap(), "-lobjc", "-lSystem"]));
        let exe = dir.join(format!("objc-{arch}"));
        link_and_compare(&args, &exe);
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "objc 42\n");
        }
        let statics = compile("relocatable_variants", "statics.c", arch, &[]);
        let merged = dir.join(format!("statics-{arch}.o"));
        link_relocatable(arch, &[&statics], &merged);
        let mut args = base_args(arch);
        args.extend(strings(&[merged.to_str().unwrap(), "-lSystem"]));
        let exe = dir.join(format!("statics-{arch}"));
        link_and_compare(&args, &exe);
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "1 12345678 1122334455667788\n");
        }

        // DWARF unwind information: `__eh_frame` rebuilt, its pointers
        // PC-relative without relocations (arm64 inputs have them).
        let main = compile("relocatable_variants", "dwarf_unwind.cpp", arch, &[]);
        let asm = compile(
            "relocatable_variants",
            &format!("dwarf-{arch}.s"),
            arch,
            &[],
        );
        let merged = dir.join(format!("dwarf-{arch}.o"));
        let bytes = link_relocatable(arch, &[&main, &asm], &merged);
        check_object(&bytes, true);
        assert!(section_names(&bytes).iter().any(|n| n == "__eh_frame"));
        let mut args = base_args(arch);
        args.extend(strings(&[merged.to_str().unwrap(), "-lc++", "-lSystem"]));
        let exe = dir.join(format!("dwarf-{arch}"));
        let linked = link_and_compare(&args, &exe);
        let function = symbol_address(&linked, "_call_through").unwrap();
        if let Ok(frames) = Command::new(tool("dwarfdump"))
            .arg("--eh-frame")
            .arg(&exe)
            .output()
            && frames.status.success()
        {
            let frames = String::from_utf8_lossy(&frames.stdout);
            let wanted = format!("pc={function:08x}...");
            assert!(
                frames.contains(&wanted),
                "{arch}: no FDE for {wanted}:\n{frames}"
            );
        }
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "dwarf 7\n");
            // Apple's linker takes the rebuilt `__eh_frame` too.
            let apple = dir.join(format!("dwarf-apple-{arch}"));
            let status = Command::new("clang++")
                .args(["-arch", arch])
                .arg(&merged)
                .arg("-o")
                .arg(&apple)
                .status()
                .unwrap();
            assert!(
                status.success(),
                "{arch}: Apple's linker rejects the object"
            );
            assert_eq!(run(&apple).unwrap(), "dwarf 7\n");
        }

        // Private externs, and debug information.
        let source = dir.join("hidden.c");
        std::fs::write(
            &source,
            "__attribute__((visibility(\"hidden\"))) int hidden_answer(void) { return 42; }\n\
             int visible_answer(void) { return hidden_answer(); }\n",
        )
        .unwrap();
        let hidden = dir.join(format!("hidden-{arch}.o"));
        assert!(tool_works(
            "clang",
            &[
                &format!("--target={arch}-apple-macos13"),
                "-g",
                "-c",
                source.to_str().unwrap(),
                "-o",
                hidden.to_str().unwrap()
            ]
        ));
        for keep in [false, true] {
            let merged = dir.join(format!("hidden-{arch}-{keep}.o"));
            let mut args = os(&[
                "-arch",
                arch,
                "-platform_version",
                "macos",
                "13.0",
                "13.0",
                "-r",
            ]);
            args.push(hidden.clone().into());
            if keep {
                args.push("-keep_private_externs".into());
            }
            args.extend(os(&["-o", merged.to_str().unwrap()]));
            let (bytes, messages) = link_bytes(&args).unwrap();
            assert!(
                messages.iter().any(|m| m.contains("DWARF")),
                "{arch}: no warning about debug sections: {messages:?}"
            );
            assert!(
                !section_names(&bytes)
                    .iter()
                    .any(|n| n.starts_with("__debug")),
                "{arch}: debug sections copied"
            );
            let symbol = symbols(&bytes)
                .into_iter()
                .find(|s| s.name == "_hidden_answer")
                .expect("_hidden_answer");
            // N_PEXT | N_EXT when kept, a plain local otherwise.
            assert_eq!(
                symbol.n_type & 0x11,
                if keep { 0x11 } else { 0 },
                "{arch}: keep_private_externs {keep}: {:#x}",
                symbol.n_type
            );
        }

        // LC_LINKER_OPTION reaches the final link through -r.
        if arch == "arm64" {
            let autolink = compile("relocatable_variants", "autolink-arm64.s", arch, &[]);
            let main = dir.join("autolink_main.c");
            std::fs::write(
                &main,
                "void release(void *);\nint main(void) { release(0); return 0; }\n",
            )
            .unwrap();
            let main_object = dir.join("autolink_main.o");
            assert!(tool_works(
                "clang",
                &[
                    "--target=arm64-apple-macos13",
                    "-c",
                    main.to_str().unwrap(),
                    "-o",
                    main_object.to_str().unwrap()
                ]
            ));
            let merged = dir.join("autolink.o");
            let bytes = link_relocatable(arch, &[&main_object, &autolink], &merged);
            let options: Vec<u32> = load_commands(&bytes).iter().map(|c| c.0).collect();
            assert!(
                options.contains(&qld::macho::read::consts::LC_LINKER_OPTION),
                "{options:x?}"
            );
            let mut args = base_args(arch);
            args.extend(strings(&[merged.to_str().unwrap(), "-lSystem"]));
            let exe = dir.join("autolink");
            link_and_compare(&args, &exe);
            if let Some(dylibs) = objdump(&["--macho", "--dylibs-used"], &exe) {
                assert!(dylibs.contains("libc++"), "{dylibs}");
            }
        }
    }
}

/// Runs `binary` with `args` on macOS and returns its stdout.
fn run_with(binary: &Path, args: &[&Path]) -> Option<String> {
    if !cfg!(target_os = "macos") {
        return None;
    }
    let output = Command::new(binary).args(args).output().unwrap();
    assert!(
        output.status.success(),
        "{} failed: {:?}\n{}{}",
        binary.display(),
        output.status,
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    Some(String::from_utf8_lossy(&output.stdout).into_owned())
}

/// `-bundle -bundle_loader <executable>`: the bundle's references to the
/// executable bind with the main-executable ordinal, and the executable
/// gets no load command. On macOS the executable loads the bundle, which
/// calls back into it.
#[test]
fn bundle_loader() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "bundle_loader",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let dir = scratch("bundle_loader");
        let host = compile("bundle_loader", "bundle_host.c", arch, &[]);
        let plugin = compile("bundle_loader", "bundle_plugin.c", arch, &[]);
        let exe = dir.join(format!("host-{arch}"));
        let mut args = base_args(arch);
        args.extend(strings(&[host.to_str().unwrap(), "-lSystem"]));
        link_and_compare(&args, &exe);

        let bundle = dir.join(format!("plugin-{arch}.bundle"));
        let mut args = base_args(arch);
        args.extend(strings(&[
            "-bundle",
            "-bundle_loader",
            exe.to_str().unwrap(),
            plugin.to_str().unwrap(),
            "-lSystem",
        ]));
        let bytes = link_and_compare(&args, &bundle);
        let file = MachOFile::parse(&bytes, Source::new(&bundle)).unwrap();
        assert_eq!(file.header().file_type, qld::macho::read::consts::MH_BUNDLE);
        let imports = chained_imports(&bytes);
        // BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE.
        assert_eq!(
            import(&imports, "_host_value").lib_ordinal,
            -1,
            "{imports:?}"
        );
        let loads = file
            .load_commands()
            .map(Result::unwrap)
            .filter(|c| c.cmd == LC_LOAD_DYLIB)
            .count();
        assert_eq!(loads, 1, "{arch}: only libSystem is loaded");
        if host_can_run(arch) {
            assert_eq!(run_with(&exe, &[&bundle]).unwrap(), "bundle 42\n");
        }
    }
}

/// `-init` (an `LC_ROUTINES_64` initializer that dyld runs before the
/// dylib's other initializers) and `-alias` (a second name for a symbol).
/// `ld64.lld` ignores `-init`, so only the rest is compared with it.
#[test]
fn init_and_alias() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "init_and_alias",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let dir = scratch("init").join(arch);
        std::fs::create_dir_all(&dir).unwrap();
        let lib = compile("init", "init_lib.c", arch, &[]);
        let client = compile("init", "init_client.c", arch, &[]);
        let dylib = dir.join("libinit.dylib");
        let mut args = base_args(arch);
        args.extend(strings(&[
            "-dylib",
            "-install_name",
            "@rpath/libinit.dylib",
            "-init",
            "_lib_init",
            "-alias",
            "_lib_ready",
            "_lib_ready_alias",
            lib.to_str().unwrap(),
            "-lSystem",
        ]));
        let bytes = link_and_compare(&args, &dylib);
        let (_, at, _) = load_commands(&bytes)
            .into_iter()
            .find(|c| c.0 == qld::macho::read::consts::LC_ROUTINES_64)
            .expect("LC_ROUTINES_64");
        let init_address = u64::from_le_bytes(bytes[at + 8..at + 16].try_into().unwrap());
        assert_eq!(Some(init_address), symbol_address(&bytes, "_lib_init"));
        assert_eq!(
            symbol_address(&bytes, "_lib_ready_alias"),
            symbol_address(&bytes, "_lib_ready"),
            "{arch}: the alias is not at its target"
        );
        if let Some(trie) = objdump(&["--macho", "--exports-trie"], &dylib) {
            assert!(trie.contains("_lib_ready_alias"), "{trie}");
        }

        let exe = dir.join("init_client");
        let mut args = base_args(arch);
        args.extend(strings(&[
            client.to_str().unwrap(),
            &format!("-L{}", dir.display()),
            "-linit",
            "-rpath",
            "@executable_path",
            "-lSystem",
        ]));
        link_and_compare(&args, &exe);
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "init ran\nready 42 42\n");
        }
    }
    let error = link_bytes(&os(&["-arch", "arm64", "-init", "_main", "a.o"])).unwrap_err();
    assert!(
        error.contains("-init can only be used with -dylib"),
        "{error}"
    );
}

/// `-flat_namespace`: imports are looked up by name (ordinal
/// `BIND_SPECIAL_DYLIB_FLAT_LOOKUP`), and the header has neither
/// `MH_TWOLEVEL` nor `MH_NOUNDEFS`, as with ld64 and lld.
#[test]
fn flat_namespace() {
    for arch in ["arm64", "x86_64"] {
        if !clang_for(arch) {
            skip(
                "flat_namespace",
                &format!("clang cannot target {arch}-apple-macos"),
            );
            continue;
        }
        let object = compile("flat", "hello.c", arch, &[]);
        let exe = scratch("flat").join(format!("hello-{arch}"));
        let mut args = base_args(arch);
        args.extend(strings(&[
            "-flat_namespace",
            object.to_str().unwrap(),
            "-lSystem",
        ]));
        let bytes = link_and_compare(&args, &exe);
        let flags = MachOFile::parse(&bytes, Source::new(&exe))
            .unwrap()
            .header()
            .flags;
        assert_eq!(
            flags & qld::macho::read::consts::MH_TWOLEVEL,
            0,
            "{flags:#x}"
        );
        assert_eq!(
            flags & qld::macho::read::consts::MH_NOUNDEFS,
            0,
            "{flags:#x}"
        );
        let imports = chained_imports(&bytes);
        assert_eq!(import(&imports, "_printf").lib_ordinal, -2, "{imports:?}");
        if host_can_run(arch) {
            assert_eq!(run(&exe).unwrap(), "hello from qld 3 42\n");
        }
    }
}

/// Whether `rustc` has the standard library for `target`.
fn rust_std_for(target: &str) -> bool {
    let Ok(output) = Command::new("rustc")
        .args(["--print", "target-libdir", "--target", target])
        .output()
    else {
        return false;
    };
    let dir = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
    output.status.success()
        && std::fs::read_dir(dir).is_ok_and(|entries| {
            entries
                .flatten()
                .any(|e| e.file_name().to_string_lossy().starts_with("libstd-"))
        })
}

/// How `rustc` reaches the linker.
#[derive(Clone, Copy)]
enum RustLinker<'a> {
    /// `-C linker=clang -C link-arg=-fuse-ld=<path>`: Apple clang's driver
    /// runs the linker, as `cargo` does on macOS.
    Clang(&'a Path),
    /// `-C linker-flavor=ld64.lld -C linker=<path>`: rustc runs the linker
    /// itself with an ld64 command line, against `syslibroot()`.
    Direct(&'a Path),
}

/// Builds `rust_std.rs` for `target` into `output` with `linker`, and
/// with `MACOSX_DEPLOYMENT_TARGET=deployment` when given (rustc's default
/// for arm64 is 11.0, which gets `LC_DYLD_INFO_ONLY`).
fn rustc_link(
    target: &str,
    linker: RustLinker<'_>,
    deployment: Option<&str>,
    link_args: &[&str],
    output: &Path,
) -> Result<(), String> {
    let mut command = Command::new("rustc");
    command
        .args(["--edition", "2021", "-O", "--target", target])
        .arg(data_dir().join("rust_std.rs"))
        .arg("-o")
        .arg(output)
        .current_dir(output.parent().unwrap());
    for arg in link_args {
        command.arg("-C").arg(format!("link-arg={arg}"));
    }
    match linker {
        RustLinker::Clang(path) => {
            command
                .args(["-C", "linker=clang", "-C"])
                .arg(format!("link-arg=-fuse-ld={}", path.display()));
        }
        RustLinker::Direct(path) => {
            command
                .args(["-C", "linker-flavor=ld64.lld", "-C"])
                .arg(format!("linker={}", path.display()))
                .env("SDKROOT", syslibroot());
        }
    }
    match deployment {
        Some(version) => command.env("MACOSX_DEPLOYMENT_TARGET", version),
        None => command.env_remove("MACOSX_DEPLOYMENT_TARGET"),
    };
    let result = command.output().map_err(|e| e.to_string())?;
    if result.status.success() {
        Ok(())
    } else {
        Err(String::from_utf8_lossy(&result.stderr).into_owned())
    }
}

const RUST_STD_OUTPUT: &str = "\
threads: [(1, 1), (2, 4), (3, 9), (4, 16)] tls-len 12 main 0
panics: caught 3 sum 54
thread panic: true
words: [(\"the\", 3), (\"brown\", 1), (\"dog\", 1)] pi    3.142 hex 0xff float 1.2345e3
";

/// A Rust program using threads, `panic=unwind` with `catch_unwind`,
/// thread-locals, formatting and `HashMap`, linked by qld (M8's exit
/// criterion for `aarch64-apple-darwin`; `x86_64-apple-darwin` too when
/// its standard library is installed).
///
/// On macOS, rustc links through Apple clang with `-fuse-ld=ld64.qld`, and
/// the result runs. Elsewhere rustc runs `ld64.qld` directly against the
/// stub SDK, and the output is compared with `ld64.lld`'s. Both the default
/// deployment target (legacy dyld info) and 13.0 (chained fixups) are
/// linked.
#[test]
fn rust_binary() {
    if !cfg!(unix) {
        eprintln!("skipping rust_binary: needs a symlink to the qld binary");
        return;
    }
    let dir = scratch("rust");
    let linker = dir.join("ld64.qld");
    let _ = std::fs::remove_file(&linker);
    #[cfg(unix)]
    std::os::unix::fs::symlink(env!("CARGO_BIN_EXE_qld"), &linker).unwrap();
    for (target, arch) in [
        ("aarch64-apple-darwin", "arm64"),
        ("x86_64-apple-darwin", "x86_64"),
    ] {
        if !rust_std_for(target) {
            // Only arm64 is required on the macOS runner.
            if arch == "arm64" {
                skip("rust_binary", &format!("rustc has no std for {target}"));
            } else {
                eprintln!("skipping rust_binary for {target}: rustc has no std for it");
            }
            continue;
        }
        for (deployment, chained) in [(None, false), (Some("13.0"), true)] {
            let variant = format!("{arch}-{}", deployment.unwrap_or("default"));
            let out_dir = dir.join(&variant);
            std::fs::create_dir_all(&out_dir).unwrap();
            let exe = out_dir.join("rust_std");
            let how = if cfg!(target_os = "macos") {
                RustLinker::Clang(&linker)
            } else {
                RustLinker::Direct(&linker)
            };
            rustc_link(target, how, deployment, &[], &exe)
                .unwrap_or_else(|e| panic!("{variant}: rustc with qld failed:\n{e}"));
            let bytes = std::fs::read(&exe).unwrap();

            let file = MachOFile::parse(&bytes, Source::new(&exe)).unwrap();
            assert_eq!(file.header().file_type, MH_EXECUTE, "{variant}");
            let commands: Vec<u32> = load_commands(&bytes).iter().map(|c| c.0).collect();
            assert!(commands.contains(&LC_MAIN), "{variant}");
            let dyld_info = if chained {
                LC_DYLD_CHAINED_FIXUPS
            } else {
                qld::macho::read::consts::LC_DYLD_INFO_ONLY
            };
            assert!(commands.contains(&dyld_info), "{variant}: {commands:x?}");
            if arch == "arm64" {
                check_signature(&bytes);
            }
            let names = section_names(&bytes);
            for wanted in [
                "__thread_vars",
                "__gcc_except_tab",
                "__eh_frame",
                "__unwind_info",
            ] {
                assert!(
                    names.iter().any(|n| n == wanted),
                    "{variant}: no {wanted} in {names:?}"
                );
            }
            // The personality is reached only from `__eh_frame` CIEs, and
            // must survive `-dead_strip`.
            assert!(
                symbol_address(&bytes, "_rust_eh_personality").is_some(),
                "{variant}: _rust_eh_personality was dead-stripped"
            );

            if let Some(lld) = ld64_lld() {
                let reference = out_dir.join("rust_std-lld");
                rustc_link(
                    target,
                    RustLinker::Direct(&lld),
                    deployment,
                    &[],
                    &reference,
                )
                .unwrap_or_else(|e| panic!("{variant}: rustc with ld64.lld failed:\n{e}"));
                if let (Some(ours), Some(theirs)) = (summarize(&exe), summarize(&reference)) {
                    assert_eq!(ours, theirs, "{variant}: qld vs ld64.lld");
                }
            }
            if host_can_run(arch) {
                assert_eq!(run(&exe).unwrap(), RUST_STD_OUTPUT, "{variant}");
            }
            if chained {
                rust_prelinked(target, arch, &linker, &out_dir);
            }
        }
    }
}

/// `-r` on a whole Rust program: rustc hands qld the program's objects
/// and the standard library's rlibs with `-r`, and the merged object then
/// links into the program (with qld, and with `ld64.lld` for comparison;
/// on macOS also with Apple's linker), which runs.
fn rust_prelinked(target: &str, arch: &str, linker: &Path, dir: &Path) {
    let merged = dir.join("rust_std.o");
    rustc_link(
        target,
        RustLinker::Direct(linker),
        Some("13.0"),
        &["-r"],
        &merged,
    )
    .unwrap_or_else(|e| panic!("{arch}: rustc with qld -r failed:\n{e}"));
    let bytes = std::fs::read(&merged).unwrap();
    check_object(&bytes, true);
    let mut args = base_args(arch);
    args.extend(strings(&[
        merged.to_str().unwrap(),
        "-lSystem",
        "-lc",
        "-lm",
        "-dead_strip",
    ]));
    let exe = dir.join("rust_std-prelinked");
    link_and_compare(&args, &exe);
    if host_can_run(arch) {
        assert_eq!(run(&exe).unwrap(), RUST_STD_OUTPUT, "{arch}: prelinked");
        let apple = dir.join("rust_std-prelinked-apple");
        let status = Command::new("clang")
            .args(["-arch", arch])
            .arg(&merged)
            .arg("-o")
            .arg(&apple)
            .status()
            .unwrap();
        assert!(
            status.success(),
            "{arch}: Apple's linker rejects the object"
        );
        assert_eq!(
            run(&apple).unwrap(),
            RUST_STD_OUTPUT,
            "{arch}: by Apple's ld"
        );
    }
}

/// Runs the link in `W25_ARGS` (whitespace-separated), for development.
#[test]
#[ignore = "manual"]
fn manual() {
    let args: Vec<OsString> = std::env::var("W25_ARGS")
        .unwrap_or_default()
        .split_whitespace()
        .map(OsString::from)
        .collect();
    let mut argv = vec![OsString::from("ld64.qld")];
    argv.extend(args);
    let Ok(ParseOutcome::Link(options)) = parse_darwin(&argv) else {
        panic!("not a link");
    };
    if let Err(error) = qld::macho::link(&options, &qld::diag::Stderr::new("qld")) {
        panic!("{error}");
    }
    let _: &dyn DiagnosticSink = &Collect::new();
}