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
//! The format-neutral resolution driver: symbol insertion and archive
//! extraction, iterated to a fixpoint.
//!
//! Each round:
//!
//! 1. **Load** the files that became live (in parallel): initially the
//!    objects and shared libraries on the command line, later the archive
//!    members chosen in the previous round.
//! 2. **Hook**: [`RoundHook::after_load`] sees the newly loaded files, in
//!    input-position order, before any of their symbols is read. A backend
//!    uses it to claim COMDAT groups and to stop reporting definitions from
//!    group copies it discards.
//! 3. **Intern** their global symbol names, and in the first round the lazy
//!    names of every archive member, as one [`intern_batch`] (so IDs stay
//!    deterministic).
//! 4. **Insert and mark**, in parallel: offer each definition, lazy ones
//!    included, to the table, which keeps the best candidate per symbol under
//!    the [`Resolver`]; and set reference flags. A symbol whose
//!    [`REFERENCED`](SymbolFlags::REFERENCED) bit this sets for the first
//!    time is a candidate for extraction.
//! 5. **Choose members**: for each candidate whose best definition
//!    [`extracts`](Resolver::extracts) (by default: is lazy), its defining
//!    member becomes live. Because lazy candidates compete by input position,
//!    that member is the earliest one that defines the symbol.
//!
//! The loop ends when a round chooses no member.
//!
//! Every step's outcome is a function of the previous state and the set of
//! files, never of thread scheduling, so the result is deterministic. It is
//! also independent of command-line order in *whether* a referenced symbol
//! resolves: a reference from any live file extracts a member from any
//! archive, wherever the two sit on the command line (see
//! `docs/compatibility.md`, "Archive resolution order"). Input order only
//! decides *which* definition wins.
//!
//! # Cost
//!
//! A round's work is proportional to the files that became live in it (the
//! first round also covers every lazy file's index names): the driver keeps
//! the round's file indices and never walks the whole file list after the
//! first round. Small rounds, below a few thousand symbols, run on the
//! calling thread, because waking a large pool for them costs more than the
//! work. The final undefined and duplicate reports cover the live files.
//!
//! [`intern_batch`]: SymbolTable::intern_batch

use core::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, PoisonError};

use rayon::prelude::*;

use super::definition::{Definition, DefinitionKind, Resolver};
use super::flags::SymbolFlags;
use super::name::{InputPosition, SymbolName};
use super::report::{DuplicateSymbol, SymbolReference, UndefinedSymbol};
use super::table::{InternJob, LookupView, SymbolTable};
use super::util::select_mut;
use crate::error::{Error, Result};
use crate::ids::{FileId, SymbolId};

/// Files smaller than this many symbols are processed as one parallel task.
const MIN_PARALLEL_SYMBOLS: usize = 1024;
/// Passes over fewer symbols (or candidates) than this, in total, run on the
/// calling thread.
const MIN_PARALLEL_WORK: usize = 4096;
/// Sizing the symbol ID vectors (a memset) runs in parallel only past this
/// many entries.
const MIN_PARALLEL_SIZING: usize = 1 << 20;

/// How one entry of a live file's global symbol list takes part in
/// resolution.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SymbolUse {
    /// The file refers to the symbol without defining it.
    Reference {
        /// A weak reference neither extracts archive members nor makes an
        /// unresolved symbol an error.
        weak: bool,
    },
    /// The file defines the symbol.
    Definition {
        /// The kind of definition. [`DefinitionKind::Lazy`] and
        /// [`DefinitionKind::Undefined`] make little sense here; `Undefined`
        /// is ignored.
        kind: DefinitionKind,
        /// Format-specific precedence data, stored in
        /// [`Definition::aux`].
        aux: u64,
    },
    /// The entry needs an ID but plays no part in resolution.
    Ignore,
}

/// An input file as the resolution driver sees it.
///
/// The format backend implements this for its object, shared-library and
/// archive-member types. The driver identifies files by their index in the
/// slice passed to [`resolve_symbols`]: file `i` is `FileId::new(i)`.
///
/// Linker-created references (the entry point, `-u` options) fit in as a
/// small always-live file at the front of the command line.
pub trait ResolveFile<'a>: Send + Sync {
    /// The file's position on the command line, for tie-breaks.
    fn position(&self) -> InputPosition;

    /// `true` for files that take part from the start: objects, shared
    /// libraries, and `--whole-archive` members. `false` for archive members,
    /// which are lazy until extracted.
    fn is_live_at_start(&self) -> bool;

    /// The names a lazy file would define if extracted, typically from the
    /// archive symbol index. Called once, in the first round, and only for
    /// files that are not live at start.
    fn lazy_names(&self) -> &[SymbolName<'a>];

    /// Makes a newly live file ready for [`symbol_names`](Self::symbol_names)
    /// and [`symbol_use`](Self::symbol_use), usually by parsing it. Called
    /// once per file, in parallel with other files.
    ///
    /// # Errors
    ///
    /// Any error stops resolution. When several files fail in the same round,
    /// the error of the file with the lowest position is returned.
    fn load(&mut self) -> Result<()>;

    /// The file's global symbols, after [`load`](Self::load) and the round's
    /// [`RoundHook::after_load`].
    fn symbol_names(&self) -> &[SymbolName<'a>];

    /// What entry `index` of [`symbol_names`](Self::symbol_names) does.
    /// Called from many threads, only with `index < symbol_names().len()`,
    /// and only after the round's [`RoundHook::after_load`] (but see
    /// [`can_load_early`](Self::can_load_early)).
    fn symbol_use(&self, index: usize) -> SymbolUse;

    /// Whether this lazy file may be loaded before it becomes live, maybe
    /// never to become live: [`load`](Self::load) then has no effect beyond
    /// the file, and [`unload`](Self::unload) undoes it. The default is
    /// `false`.
    ///
    /// With a [`LoadHook`], the driver loads, right after the first round,
    /// every such member that the rounds may extract, in one parallel pass,
    /// and (if the hook [keeps its names](LoadHook::keeps_names)) reads its
    /// names and the uses of its references before the round that makes it
    /// live. The rounds then only settle which of them become live. A load
    /// error is reported only if the file becomes live.
    fn can_load_early(&self) -> bool {
        false
    }

    /// Returns a file loaded early that never became live to its unloaded
    /// state, typically by dropping what [`load`](Self::load) parsed.
    /// Called once, at the end of resolution. The default does nothing.
    fn unload(&mut self) {}
}

/// A file that became live in the current round, as handed to
/// [`RoundHook::after_load`].
#[derive(Debug)]
pub struct RoundFile<'r, F> {
    /// The file's ID: its index in the slice passed to [`resolve_symbols`].
    pub id: FileId,
    /// The loaded file.
    pub file: &'r mut F,
    /// The file's rank among all files ordered by input position, as given
    /// to [`LoadHook::on_load`]; `None` when the driver uses no load hook.
    pub rank: Option<u32>,
}

/// A backend hook into each round of [`resolve_symbols_with`].
///
/// The driver calls [`after_load`](Self::after_load) once per round, on the
/// calling thread, after the round's newly live files are loaded and before
/// their [`symbol_names`](ResolveFile::symbol_names) and
/// [`symbol_use`](ResolveFile::symbol_use) are read. Whatever the hook changes
/// in those files is what the round interns and inserts.
///
/// # COMDAT groups
///
/// The intended use is claiming COMDAT groups before insertion, so that
/// definitions from discarded group copies never enter the table:
///
/// 1. For each file in `files` (input-position order), claim each of its
///    groups unless another file already holds the signature. A group
///    claimed in an earlier round stays claimed, even by a file with a
///    higher position. [`GroupClaims`](super::GroupClaims) does this
///    deterministically, and in parallel.
/// 2. In each file that lost a claim, make `symbol_use` report the
///    definitions in the discarded group's sections as
///    [`SymbolUse::Ignore`]. Their names keep their IDs, so relocations
///    against them resolve to the kept copy's definitions.
///
/// Because the kept copy's file is live in the same or an earlier round, its
/// definitions reach the table no later than the discarded ones would have,
/// and a discarded definition is never reported as a duplicate. Copies of a
/// group normally define the same symbols. If a kept copy lacks one, and an
/// archive member was extracted for it, that member's lazy candidate stays
/// in the table and references to the symbol are reported undefined, as with
/// a stale archive index.
///
/// # Determinism
///
/// `files` is sorted by `(position, id)` and holds exactly the files that
/// became live in this round, so a hook that decides by that order (or by
/// lowest position, as [`GroupClaims`](super::GroupClaims) does) gives the
/// same result for every thread count.
///
/// The unit type `()` is the no-op hook [`resolve_symbols`] uses.
pub trait RoundHook<F> {
    /// Work to do on each newly live file right after it loads, in
    /// parallel with the loading of the round's other files; `None` (the
    /// default) for none. See [`LoadHook`].
    fn load_hook(&self) -> Option<&dyn LoadHook<F>> {
        None
    }

    /// Called once per round after loading; see the [trait documentation](Self).
    /// `round` is 0 for the files live at start, and `r` for the members
    /// listed in [`Resolution::extracted`]`()[r - 1]`.
    ///
    /// # Errors
    ///
    /// An error stops resolution and is returned by
    /// [`resolve_symbols_with`].
    fn after_load(&mut self, round: usize, files: &mut [RoundFile<'_, F>]) -> Result<()> {
        let _ = (round, files);
        Ok(())
    }
}

impl<F> RoundHook<F> for () {}

/// Per-file work of a [`RoundHook`] that can run while files load, in
/// parallel. Neither method may change the file's names or references
/// (they may have been read already, see [`ResolveFile::can_load_early`]).
///
/// - [`prepare`](Self::prepare) is called once per file, right after it
///   loads: in the round that makes it live, or earlier if the driver
///   loads it early. It must not depend on the round.
/// - [`on_load`](Self::on_load) is called in the round that makes the file
///   live, before the round's [`RoundHook::after_load`].
///
/// The driver uses a load hook only when all files have distinct input
/// positions (as input files always do), so that `rank` below is a total
/// order.
///
/// A COMDAT hook uses them to look each group up while the file is in
/// cache and to offer it, round by round, with one atomic operation (see
/// [`GroupSlots`](super::GroupSlots)), leaving `after_load` to settle the
/// claims.
pub trait LoadHook<F>: Sync {
    /// Whether [`RoundHook::after_load`] leaves the
    /// [`symbol_names`](ResolveFile::symbol_names) of this loaded file as
    /// they are, and the uses of its references
    /// ([`symbol_use`](ResolveFile::symbol_use)); it may still change the
    /// uses of definitions. The driver then looks the names up while the
    /// file loads, in the same parallel task, instead of in a pass of its
    /// own after `after_load`, and may load the file early (see
    /// [`ResolveFile::can_load_early`]).
    fn keeps_names(&self, file: &F) -> bool;

    /// Called for file `id` right after it loads; see the [trait
    /// documentation](Self). The default does nothing.
    ///
    /// # Errors
    ///
    /// As for [`on_load`](Self::on_load), and only reported if the file
    /// becomes live.
    fn prepare(&self, id: FileId, file: &mut F) -> Result<()> {
        let _ = (id, file);
        Ok(())
    }

    /// Called for file `id`, newly live in round `round` (numbered as in
    /// [`RoundHook::after_load`]). `rank` is the file's index among all
    /// files ordered by input position.
    ///
    /// # Errors
    ///
    /// An error stops resolution; as with load errors, the one of the file
    /// with the lowest position is returned.
    fn on_load(&self, round: usize, id: FileId, rank: u32, file: &mut F) -> Result<()>;
}

/// The outcome of [`resolve_symbols`].
#[derive(Debug)]
pub struct Resolution<'a> {
    live: Vec<bool>,
    symbol_ids: Vec<Vec<SymbolId>>,
    extracted: Vec<Vec<FileId>>,
    undefined: Vec<UndefinedSymbol<'a>>,
    duplicates: Vec<DuplicateSymbol<'a>>,
}

impl<'a> Resolution<'a> {
    /// Returns `true` if the file was live at start or was extracted.
    ///
    /// # Panics
    ///
    /// Panics if `file` is out of range for the resolved file slice.
    #[must_use]
    pub fn is_live(&self, file: FileId) -> bool {
        self.live[file.index()]
    }

    /// Returns the live files, in `FileId` order.
    pub fn live_files(&self) -> impl Iterator<Item = FileId> + '_ {
        self.live
            .iter()
            .enumerate()
            .filter(|&(_, &live)| live)
            .map(|(index, _)| FileId::new(index))
    }

    /// Returns the symbol ID of each entry of a live file's
    /// [`symbol_names`](ResolveFile::symbol_names). Empty for files that
    /// never became live.
    ///
    /// # Panics
    ///
    /// Panics if `file` is out of range for the resolved file slice.
    #[must_use]
    pub fn symbol_ids(&self, file: FileId) -> &[SymbolId] {
        &self.symbol_ids[file.index()]
    }

    /// Returns the archive members extracted in each round, each round
    /// sorted by input position. Its length is the number of rounds that
    /// extracted something.
    #[must_use]
    pub fn extracted(&self) -> &[Vec<FileId>] {
        &self.extracted
    }

    /// Returns the symbols referenced (non-weakly) by live files that ended
    /// up without a definition, ordered by first reference.
    #[must_use]
    pub fn undefined(&self) -> &[UndefinedSymbol<'a>] {
        &self.undefined
    }

    /// Returns the duplicate definitions the resolver reported, ordered by
    /// the position of the winning definition.
    #[must_use]
    pub fn duplicates(&self) -> &[DuplicateSymbol<'a>] {
        &self.duplicates
    }
}

/// A step of a resolution round, for [`Steps`].
#[derive(Clone, Copy)]
enum Step {
    Load,
    Hook,
    Intern,
    Insert,
    Choose,
    Report,
    Prefetch,
}

const STEP_NAMES: [&str; 7] = [
    "load", "hook", "intern", "insert", "choose", "report", "prefetch",
];

/// With `QLD_TIMING` set, the time of each step, for the first round and
/// summed over the later ones, printed to stderr as `qld-lap:` lines
/// (`benches/run.py --laps` collects them).
struct Steps {
    last: Option<std::time::Instant>,
    first: [f64; 7],
    later: [f64; 7],
}

impl Steps {
    fn new() -> Self {
        Self {
            last: std::env::var_os("QLD_TIMING").map(|_| std::time::Instant::now()),
            first: [0.0; 7],
            later: [0.0; 7],
        }
    }

    fn lap(&mut self, round: usize, step: Step) {
        let Some(last) = &mut self.last else {
            return;
        };
        let now = std::time::Instant::now();
        let ms = now.duration_since(*last).as_secs_f64() * 1000.0;
        *last = now;
        let totals = if round == 0 {
            &mut self.first
        } else {
            &mut self.later
        };
        totals[step as usize] += ms;
    }

    fn print(&self, rounds: usize) {
        if self.last.is_none() {
            return;
        }
        for (name, ms) in STEP_NAMES.iter().zip(&self.first) {
            if *ms > 0.0 {
                eprintln!("qld-lap: resolve round 0 {name}: {ms:.2} ms");
            }
        }
        for (name, ms) in STEP_NAMES.iter().zip(&self.later) {
            if *ms > 0.0 {
                eprintln!("qld-lap: resolve rounds 1+ {name}: {ms:.2} ms");
            }
        }
        eprintln!("qld-lap: resolve rounds (count): {rounds} ms");
    }
}

/// One file's symbols in a pass: the file index, the IDs of its entries,
/// and whether the entries are its lazy names rather than its real symbols.
#[derive(Clone, Copy)]
struct Work<'s> {
    file: usize,
    ids: &'s [SymbolId],
    lazy: bool,
}

/// Runs `f(file, work, symbol index, symbol ID)` on every entry of `work`
/// and collects the `Some` results, in unspecified order. Parallel only when
/// the total is large; small files are grouped into tasks, and only large
/// files are split.
fn map_symbols<'s, F, T, W>(files: &[F], work: &[Work<'s>], f: W) -> Vec<T>
where
    F: Sync,
    T: Send,
    W: Fn(&F, Work<'s>, usize, SymbolId) -> Option<T> + Sync,
{
    let f = &f;
    let sequential = move |w: &Work<'s>| {
        let (file, w) = (&files[w.file], *w);
        w.ids
            .iter()
            .enumerate()
            .filter_map(move |(symbol, &id)| f(file, w, symbol, id))
    };
    let total: usize = work.iter().map(|w| w.ids.len()).sum();
    if total < MIN_PARALLEL_WORK {
        return work.iter().flat_map(sequential).collect();
    }
    let (large, small): (Vec<Work<'s>>, Vec<Work<'s>>) = work
        .iter()
        .partition(|w| w.ids.len() >= 2 * MIN_PARALLEL_SYMBOLS);
    let small_total: usize = small.iter().map(|w| w.ids.len()).sum();
    let files_per_task = (MIN_PARALLEL_SYMBOLS * small.len() / small_total.max(1)).max(1);
    small
        .par_iter()
        .with_min_len(files_per_task)
        .flat_map_iter(sequential)
        .chain(large.par_iter().flat_map(move |w| {
            let (file, w) = (&files[w.file], *w);
            w.ids
                .par_iter()
                .enumerate()
                .with_min_len(MIN_PARALLEL_SYMBOLS)
                .filter_map(move |(symbol, &id)| f(file, w, symbol, id))
        }))
        .collect()
}

fn sort_unstable_by_key<T, K, G>(items: &mut [T], key: G)
where
    T: Send,
    K: Ord,
    G: Fn(&T) -> K + Sync,
{
    if items.len() < MIN_PARALLEL_WORK {
        items.sort_unstable_by_key(key);
    } else {
        items.par_sort_unstable_by_key(key);
    }
}

/// Resolves the symbols of `files` into `table`, extracting archive members
/// until nothing changes. See the [module documentation](self).
///
/// Runs in the current rayon pool. The result, including every symbol ID, is
/// the same for any thread count.
///
/// This is [`resolve_symbols_with`] and the no-op hook `()`.
///
/// # Errors
///
/// Returns the first (by input position) error from [`ResolveFile::load`] in
/// the round where loading failed, or [`Error::Limit`] if there are more than
/// `u32::MAX` files or the table would exceed
/// [`MAX_SYMBOLS`](super::table::MAX_SYMBOLS) symbols. Undefined and
/// duplicate symbols are not errors here; they are reported in the
/// [`Resolution`].
pub fn resolve_symbols<'a, F, R>(
    table: &mut SymbolTable<'a>,
    resolver: &R,
    files: &mut [F],
) -> Result<Resolution<'a>>
where
    F: ResolveFile<'a>,
    R: Resolver + ?Sized,
{
    resolve_symbols_with(table, resolver, files, &mut ())
}

/// Resolves like [`resolve_symbols`], calling `hook` in every round between
/// loading the newly live files and reading their symbols; see
/// [`RoundHook`].
///
/// # Errors
///
/// As for [`resolve_symbols`], plus any error the hook returns.
pub fn resolve_symbols_with<'a, F, R, H>(
    table: &mut SymbolTable<'a>,
    resolver: &R,
    files: &mut [F],
    hook: &mut H,
) -> Result<Resolution<'a>>
where
    F: ResolveFile<'a>,
    R: Resolver + ?Sized,
    H: RoundHook<F> + ?Sized,
{
    if u32::try_from(files.len()).is_err() {
        return Err(Error::Limit(format!(
            "{} input files (at most {})",
            files.len(),
            u32::MAX
        )));
    }
    let count = files.len();
    let mut live = vec![false; count];
    let mut symbol_ids: Vec<Vec<SymbolId>> = (0..count).map(|_| Vec::new()).collect();
    let mut lazy_ids: Vec<Vec<SymbolId>> = (0..count).map(|_| Vec::new()).collect();
    // The files to load this round, and (first round only) the lazy files,
    // both in index order.
    let (mut load, mut lazy): (Vec<usize>, Vec<usize>) =
        (0..count).partition(|&index| files[index].is_live_at_start());
    let mut extracted = Vec::new();
    // Members loaded ahead of their round: the number of their names the
    // table did not hold then (`None` if their names were not looked up),
    // or their load error.
    let mut early: Vec<Option<Result<Option<usize>>>> = (0..count).map(|_| None).collect();
    let mut loaded_early = Vec::new();

    // Each file's rank by position, when positions are distinct (as input
    // files' always are). The load hook, and so the lookups while files
    // load (whose misses are interned by position) and loading members
    // early, need them.
    let ranks: Option<Vec<u32>> = {
        let mut order: Vec<usize> = (0..count).collect();
        order.sort_unstable_by_key(|&index| files[index].position());
        order
            .windows(2)
            .all(|pair| files[pair[0]].position() != files[pair[1]].position())
            .then(|| {
                let mut ranks = vec![0u32; count];
                for (rank, &index) in order.iter().enumerate() {
                    // count <= u32::MAX, checked above.
                    ranks[index] = rank as u32;
                }
                ranks
            })
    };
    let with_load_hook = hook.load_hook().is_some() && ranks.is_some();
    let mut steps = Steps::new();
    for round in 0.. {
        // Round 0 fills an empty table: nothing to look up.
        let looked_up = with_load_hook && round > 0;
        let looked = {
            let mut loaded = select_mut(files, &load);
            let view = looked_up.then(|| table.lookup_view());
            let outputs = looked_up.then(|| select_mut(&mut symbol_ids, &load));
            let looked = load_files(
                &mut loaded,
                &load,
                round,
                hook.load_hook().zip(ranks.as_deref()),
                view.as_ref().zip(outputs),
                select_mut(&mut early, &load),
            )?;
            steps.lap(round, Step::Load);
            let mut round_files: Vec<RoundFile<'_, F>> = load
                .iter()
                .zip(loaded)
                .map(|(&index, file)| RoundFile {
                    id: FileId::new(index),
                    file,
                    rank: ranks
                        .as_ref()
                        .filter(|_| with_load_hook)
                        .map(|ranks| ranks[index]),
                })
                .collect();
            round_files.sort_by_key(|entry| (entry.file.position(), entry.id));
            hook.after_load(round, &mut round_files)?;
            steps.lap(round, Step::Hook);
            looked
        };
        for &index in &load {
            live[index] = true;
        }

        let files_ref = &*files;
        if looked_up {
            // Names not found, and the names of files not looked up (the
            // hook may have changed them), are interned now.
            if looked
                .iter()
                .any(|looked| looked.is_none_or(|missing| missing > 0))
            {
                let mut jobs: Vec<InternJob<'a, '_>> = load
                    .iter()
                    .zip(select_mut(&mut symbol_ids, &load))
                    .zip(&looked)
                    .map(|((&index, ids), looked)| {
                        let names = files_ref[index].symbol_names();
                        if looked.is_none() {
                            ids.clear();
                            ids.resize(names.len(), LookupView::MISSING);
                        }
                        InternJob {
                            position: files_ref[index].position(),
                            names,
                            ids: ids.as_mut_slice(),
                        }
                    })
                    .collect();
                table.try_intern_missing(&mut jobs)?;
            }
        } else {
            intern_round(
                table,
                files_ref,
                &load,
                &lazy,
                &mut symbol_ids,
                &mut lazy_ids,
            )?;
        }
        steps.lap(round, Step::Intern);
        let work: Vec<Work<'_>> = load
            .iter()
            .map(|&file| Work {
                file,
                ids: &symbol_ids[file],
                lazy: false,
            })
            .chain(lazy.iter().map(|&file| Work {
                file,
                ids: &lazy_ids[file],
                lazy: true,
            }))
            .collect();
        let table_ref = &*table;
        let candidates = insert_and_mark(table_ref, resolver, files_ref, &work);
        steps.lap(round, Step::Insert);

        let live_ref = &live;
        let choose = |&id: &SymbolId| {
            let current = table_ref.definition(id);
            if !current.is_defined() || !resolver.extracts(&current) {
                return None;
            }
            let member = current.file.index();
            (member < count && !live_ref[member]).then_some(member)
        };
        let mut members: Vec<usize> = if candidates.len() < MIN_PARALLEL_WORK {
            candidates.iter().filter_map(choose).collect()
        } else {
            candidates.par_iter().filter_map(choose).collect()
        };
        sort_unstable_by_key(&mut members, |&member| member);
        members.dedup();
        steps.lap(round, Step::Choose);
        if members.is_empty() {
            break;
        }
        if round == 0
            && with_load_hook
            && let Some(load_hook) = hook.load_hook()
        {
            loaded_early = prefetch(
                files,
                table,
                load_hook,
                &live,
                &members,
                &mut symbol_ids,
                &mut early,
            );
            steps.lap(round, Step::Prefetch);
        }

        for &member in &members {
            // The member's lazy candidates are superseded by its real symbols.
            lazy_ids[member] = Vec::new();
        }
        let mut round_members: Vec<FileId> = members.iter().copied().map(FileId::new).collect();
        round_members.sort_by_key(|file| (files[file.index()].position(), *file));
        extracted.push(round_members);
        load = members;
        lazy = Vec::new();
    }
    drop(lazy_ids);
    for index in loaded_early {
        if !live[index] {
            files[index].unload();
            symbol_ids[index] = Vec::new();
        }
    }

    let live_work: Vec<Work<'_>> = live
        .iter()
        .enumerate()
        .filter(|&(_, &live)| live)
        .map(|(file, _)| Work {
            file,
            ids: &symbol_ids[file],
            lazy: false,
        })
        .collect();
    let undefined = collect_undefined(table, files, &live_work);
    let duplicates = collect_duplicates(table, resolver, files, &live_work);
    steps.lap(0, Step::Report);
    steps.print(extracted.len() + 1);
    Ok(Resolution {
        live,
        symbol_ids,
        extracted,
        undefined,
        duplicates,
    })
}

/// Loads, right after the first round, the members that later rounds may
/// extract (see [`ResolveFile::can_load_early`]), in one parallel pass that
/// follows references as members load: from `start` (the members the first
/// round extracts), every member whose lazy definition is the first
/// round's best for a non-weak reference of a loaded file. That is a
/// superset of what the rounds extract, since definitions only get better
/// than lazy ones and lazy candidates enter the table in the first round
/// only. Each member's names are looked up as it loads (the table does not
/// change meanwhile), if the hook keeps them; `early` receives the number
/// of names not found (`None` if not looked up; the references of such a
/// member are not followed), or the load error. Returns the members it
/// loaded.
///
/// The set does not depend on scheduling, and it only decides which files
/// are loaded early, not the resolution.
fn prefetch<'a, F>(
    files: &mut [F],
    table: &mut SymbolTable<'a>,
    load_hook: &dyn LoadHook<F>,
    live: &[bool],
    start: &[usize],
    symbol_ids: &mut [Vec<SymbolId>],
    early: &mut [Option<Result<Option<usize>>>],
) -> Vec<usize>
where
    F: ResolveFile<'a>,
{
    let claimed: Vec<AtomicBool> = live.iter().map(|&live| AtomicBool::new(live)).collect();
    let prefetcher = Prefetcher {
        slots: files
            .iter_mut()
            .zip(symbol_ids.iter_mut())
            .zip(early.iter_mut())
            .map(|((file, ids), early)| Mutex::new(Some((file, ids, early))))
            .collect(),
        claimed,
        view: table.lookup_view(),
        load_hook,
    };
    rayon::scope(|scope| {
        for &member in start {
            if !prefetcher.claimed[member].swap(true, Ordering::Relaxed) {
                let prefetcher = &prefetcher;
                scope.spawn(move |scope| prefetcher.visit(member, scope));
            }
        }
    });
    prefetcher
        .slots
        .iter()
        .enumerate()
        .filter(|(_, slot)| {
            slot.lock()
                .unwrap_or_else(PoisonError::into_inner)
                .as_ref()
                .is_some_and(|(_, _, early)| early.is_some())
        })
        .map(|(index, _)| index)
        .collect()
}

/// A file as [`prefetch`] hands it to the task that loads it: the file, its
/// symbol IDs, and its early load result.
type PrefetchSlot<'p, F> = Option<(
    &'p mut F,
    &'p mut Vec<SymbolId>,
    &'p mut Option<Result<Option<usize>>>,
)>;

/// The state of [`prefetch`]: each file, whether a task claimed it, and the
/// table.
struct Prefetcher<'p, 'a, F> {
    slots: Vec<Mutex<PrefetchSlot<'p, F>>>,
    claimed: Vec<AtomicBool>,
    view: LookupView<'p, 'a>,
    load_hook: &'p dyn LoadHook<F>,
}

impl<'a, F> Prefetcher<'_, 'a, F>
where
    F: ResolveFile<'a>,
{
    fn visit<'s>(&'s self, index: usize, scope: &rayon::Scope<'s>) {
        let mut slot = self.slots[index]
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        let Some((file, ids, early)) = slot.as_mut() else {
            return;
        };
        if !file.can_load_early() {
            return;
        }
        let loaded = file
            .load()
            .and_then(|()| self.load_hook.prepare(FileId::new(index), file));
        if let Err(error) = loaded {
            **early = Some(Err(error));
            return;
        }
        if !self.load_hook.keeps_names(file) {
            **early = Some(Ok(None));
            return;
        }
        let names = file.symbol_names();
        ids.clear();
        ids.resize(names.len(), SymbolId::from_u32(0));
        **early = Some(Ok(Some(self.view.find_all(names, ids))));
        for (symbol, &id) in ids.iter().enumerate() {
            if !LookupView::is_found(id)
                || file.symbol_use(symbol) != (SymbolUse::Reference { weak: false })
            {
                continue;
            }
            // Lazy definitions are the ones that extract under the
            // default `Resolver::extracts`; with another rule, loading a
            // member early that is not extracted costs only time, and a
            // member extracted without being loaded early loads in its
            // round.
            let Some(member) = self.view.lazy_file(id) else {
                continue;
            };
            let member = member.index();
            if self
                .claimed
                .get(member)
                .is_some_and(|claimed| !claimed.swap(true, Ordering::Relaxed))
            {
                scope.spawn(move |scope| self.visit(member, scope));
            }
        }
    }
}

/// Loads `files`, whose indices are `indices`, in parallel, running the
/// load hook on each. With `lookup`, also looks up the names of each file
/// the hook keeps names of, and writes their IDs (or placeholders, see
/// [`LookupView::find_all`]) to the file's output. Returns, per file, the
/// number of names not found, or `None` if they were not looked up.
fn load_files<'a, F: ResolveFile<'a>>(
    files: &mut [&mut F],
    indices: &[usize],
    round: usize,
    load_hook: Option<(&dyn LoadHook<F>, &[u32])>,
    lookup: Option<(&LookupView<'_, 'a>, Vec<&mut Vec<SymbolId>>)>,
    early: Vec<&mut Option<Result<Option<usize>>>>,
) -> Result<Vec<Option<usize>>> {
    let (view, outputs): (_, Vec<Option<&mut Vec<SymbolId>>>) = match lookup {
        Some((view, outputs)) => (Some(view), outputs.into_iter().map(Some).collect()),
        None => (None, (0..files.len()).map(|_| None).collect()),
    };
    let one = |file: &mut F,
               index: usize,
               ids: Option<&mut Vec<SymbolId>>,
               early: &mut Option<Result<Option<usize>>>|
     -> Result<Option<usize>> {
        let id = FileId::new(index);
        let mut looked = match early.take() {
            // Loaded and prepared (and maybe looked up) by `prefetch`.
            Some(result) => result?,
            None => {
                file.load()?;
                if let Some((load_hook, _)) = load_hook {
                    load_hook.prepare(id, file)?;
                }
                None
            }
        };
        if looked.is_none()
            && let (Some(ids), Some(view), Some((load_hook, _))) = (ids, view, load_hook)
            && load_hook.keeps_names(file)
        {
            let names = file.symbol_names();
            ids.clear();
            ids.resize(names.len(), SymbolId::from_u32(0));
            looked = Some(view.find_all(names, ids));
        }
        if let Some((load_hook, ranks)) = load_hook {
            load_hook.on_load(round, id, ranks[index], file)?;
        }
        Ok(looked)
    };
    let results: Vec<(InputPosition, usize, Result<Option<usize>>)> = files
        .par_iter_mut()
        .zip(indices.par_iter())
        .zip(outputs)
        .zip(early)
        .map(|(((file, &index), ids), early)| {
            (file.position(), index, one(file, index, ids, early))
        })
        .collect();
    let mut looked = Vec::with_capacity(results.len());
    let mut first_error: Option<(InputPosition, usize, Error)> = None;
    for (position, index, result) in results {
        match result {
            Ok(count) => looked.push(count),
            Err(error) => {
                if first_error
                    .as_ref()
                    .is_none_or(|(p, i, _)| (position, index) < (*p, *i))
                {
                    first_error = Some((position, index, error));
                }
            }
        }
    }
    match first_error {
        Some((_, _, error)) => Err(error),
        None => Ok(looked),
    }
}

/// Interns the symbol names of the `load` files and the lazy names of the
/// `lazy` files (both in index order) as one batch.
fn intern_round<'a, F: ResolveFile<'a>>(
    table: &mut SymbolTable<'a>,
    files: &[F],
    load: &[usize],
    lazy: &[usize],
    symbol_ids: &mut [Vec<SymbolId>],
    lazy_ids: &mut [Vec<SymbolId>],
) -> Result<()> {
    let names = |index: usize, is_lazy: bool| {
        let file = &files[index];
        if is_lazy {
            file.lazy_names()
        } else {
            file.symbol_names()
        }
    };
    // A lazy file whose names are the very same as an earlier one's (the
    // members of an archive named twice on the command line share the
    // index's bytes) gets that file's IDs instead of interning them again:
    // at a later position, its names are never a first occurrence. clang
    // names 22 archives twice, 84,500 of its 298,000 lazy names.
    let repeats = repeated_lazy_files(files, lazy);
    let interned: Vec<usize> = lazy
        .iter()
        .zip(&repeats)
        .filter(|(_, repeat)| repeat.is_none())
        .map(|(&index, _)| index)
        .collect();
    let mut outputs: Vec<(usize, bool, &mut Vec<SymbolId>)> = load
        .iter()
        .zip(select_mut(symbol_ids, load))
        .map(|(&index, ids)| (index, false, ids))
        .chain(
            interned
                .iter()
                .zip(select_mut(lazy_ids, &interned))
                .map(|(&index, ids)| (index, true, ids)),
        )
        .collect();

    // Size the output vectors (large files make this non-trivial).
    let total: usize = outputs
        .iter()
        .map(|(index, is_lazy, _)| names(*index, *is_lazy).len())
        .sum();
    let size = |(index, is_lazy, ids): &mut (usize, bool, &mut Vec<SymbolId>)| {
        ids.clear();
        ids.resize(names(*index, *is_lazy).len(), SymbolId::from_u32(0));
    };
    if total < MIN_PARALLEL_SIZING {
        outputs.iter_mut().for_each(size);
    } else {
        outputs.par_iter_mut().for_each(size);
    }

    let mut jobs: Vec<InternJob<'a, '_>> = outputs
        .into_iter()
        .map(|(index, is_lazy, ids)| InternJob {
            position: files[index].position(),
            names: names(index, is_lazy),
            ids: ids.as_mut_slice(),
        })
        .collect();
    table.try_intern_batch(&mut jobs)?;
    drop(jobs);
    for (&index, repeat) in lazy.iter().zip(&repeats) {
        if let &Some(original) = repeat {
            lazy_ids[index] = lazy_ids[original].clone();
        }
    }
    Ok(())
}

/// For each of the `lazy` files (in index order), the earlier one whose
/// lazy names are the very same (the same bytes in memory, not merely
/// equal), if any.
fn repeated_lazy_files<'a, F: ResolveFile<'a>>(files: &[F], lazy: &[usize]) -> Vec<Option<usize>> {
    let same = |a: &SymbolName<'_>, b: &SymbolName<'_>| {
        std::ptr::eq(a.bytes(), b.bytes())
            && a.version().map(<[u8]>::as_ptr) == b.version().map(<[u8]>::as_ptr)
            && a.version().map(<[u8]>::len) == b.version().map(<[u8]>::len)
    };
    // Candidates by the first name's address and the count, then a full
    // check of each, in parallel (84,500 names for clang).
    let mut first: hashbrown::HashMap<(usize, usize), usize> = hashbrown::HashMap::new();
    let candidates: Vec<Option<usize>> = lazy
        .iter()
        .map(|&index| {
            let names = files[index].lazy_names();
            let head = names.first()?;
            let key = (names.len(), head.bytes().as_ptr() as usize);
            match first.get(&key) {
                Some(&original) if files[original].position() < files[index].position() => {
                    Some(original)
                }
                Some(_) => None,
                None => {
                    first.insert(key, index);
                    None
                }
            }
        })
        .collect();
    candidates
        .into_par_iter()
        .zip(lazy.par_iter())
        .map(|(candidate, &index)| {
            candidate.filter(|&original| {
                files[original]
                    .lazy_names()
                    .iter()
                    .zip(files[index].lazy_names())
                    .all(|(a, b)| same(a, b))
            })
        })
        .collect()
}

/// Inserts this round's lazy and live definitions and sets the reference
/// flags of its live files. Returns the extraction candidates: the symbols
/// whose `REFERENCED` bit this round set for the first time, and the
/// already referenced symbols whose new best definition extracts (only
/// possible with resolvers where a newly inserted candidate can extract).
/// May contain repeats.
///
/// Inserting and marking in one pass gives the same candidates, after
/// filtering by the final definition, as inserting everything first: a
/// symbol referenced for the first time this round is a candidate either
/// way, and a definition that ends up best was best when inserted.
fn insert_and_mark<'a, F, R>(
    table: &SymbolTable<'a>,
    resolver: &R,
    files: &[F],
    work: &[Work<'_>],
) -> Vec<SymbolId>
where
    F: ResolveFile<'a>,
    R: Resolver + ?Sized,
{
    map_symbols(files, work, |file, w, symbol, id| {
        let (kind, aux) = if w.lazy {
            (DefinitionKind::Lazy, 0)
        } else {
            match file.symbol_use(symbol) {
                SymbolUse::Definition { kind, aux } => (kind, aux),
                SymbolUse::Reference { weak: false } => {
                    let before = table.set_flags(id, SymbolFlags::REFERENCED);
                    return (!before.contains(SymbolFlags::REFERENCED)).then_some(id);
                }
                SymbolUse::Reference { weak: true } => {
                    table.set_flags(id, SymbolFlags::WEAK_REFERENCED);
                    return None;
                }
                SymbolUse::Ignore => return None,
            }
        };
        let candidate = Definition {
            kind,
            file: FileId::new(w.file),
            index: u32::try_from(symbol).unwrap_or(u32::MAX),
            position: file.position(),
            aux,
        };
        let won = table.insert_definition(resolver, id, &candidate);
        (won && resolver.extracts(&candidate) && table.flags(id).contains(SymbolFlags::REFERENCED))
            .then_some(id)
    })
}

fn collect_undefined<'a, F: ResolveFile<'a>>(
    table: &SymbolTable<'a>,
    files: &[F],
    live_work: &[Work<'_>],
) -> Vec<UndefinedSymbol<'a>> {
    let mut references: Vec<(SymbolId, SymbolReference)> =
        map_symbols(files, live_work, |file, w, symbol, id| {
            if file.symbol_use(symbol) != (SymbolUse::Reference { weak: false }) {
                return None;
            }
            let kind = table.definition_kind(id);
            if kind != DefinitionKind::Undefined && kind != DefinitionKind::Lazy {
                return None;
            }
            Some((
                id,
                SymbolReference {
                    position: file.position(),
                    file: FileId::new(w.file),
                    index: u32::try_from(symbol).unwrap_or(u32::MAX),
                },
            ))
        });
    sort_unstable_by_key(&mut references, |&entry| entry);

    let mut undefined: Vec<UndefinedSymbol<'a>> = references
        .chunk_by(|a, b| a.0 == b.0)
        .map(|group| UndefinedSymbol {
            symbol: group[0].0,
            name: table.name(group[0].0),
            references: group.iter().map(|&(_, reference)| reference).collect(),
        })
        .collect();
    undefined.sort_unstable_by_key(|entry| (entry.references[0], entry.symbol));
    undefined
}

fn collect_duplicates<'a, F, R>(
    table: &SymbolTable<'a>,
    resolver: &R,
    files: &[F],
    live_work: &[Work<'_>],
) -> Vec<DuplicateSymbol<'a>>
where
    F: ResolveFile<'a>,
    R: Resolver + ?Sized,
{
    let mut losers: Vec<(SymbolId, Definition)> =
        map_symbols(files, live_work, |file, w, symbol, id| {
            let SymbolUse::Definition { kind, aux } = file.symbol_use(symbol) else {
                return None;
            };
            if kind == DefinitionKind::Undefined {
                return None;
            }
            let definition = Definition {
                kind,
                file: FileId::new(w.file),
                index: u32::try_from(symbol).unwrap_or(u32::MAX),
                position: file.position(),
                aux,
            };
            let winner = table.definition(id);
            (winner != definition && resolver.is_duplicate(&winner, &definition))
                .then_some((id, definition))
        });
    sort_unstable_by_key(&mut losers, |(id, definition)| (*id, definition.tie_key()));

    let mut duplicates: Vec<DuplicateSymbol<'a>> = losers
        .chunk_by(|a, b| a.0 == b.0)
        .map(|group| DuplicateSymbol {
            symbol: group[0].0,
            name: table.name(group[0].0),
            winner: table.definition(group[0].0),
            others: group.iter().map(|&(_, definition)| definition).collect(),
        })
        .collect();
    duplicates.sort_unstable_by_key(|entry| (entry.winner.tie_key(), entry.symbol));
    duplicates
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::Error;
    use crate::symbols::elf_reference::ElfReferenceRules;

    /// A synthetic input. Symbols are written as `"<tag>:<name>"`, where the
    /// tag is `D` (strong definition), `W` (weak definition), `C<size>`
    /// (common), `S` (shared definition), `U` (reference) or `u` (weak
    /// reference).
    struct Mock {
        position: InputPosition,
        live_at_start: bool,
        lazy: Vec<SymbolName<'static>>,
        names: Vec<SymbolName<'static>>,
        uses: Vec<SymbolUse>,
        loads: usize,
        fail: bool,
        /// Set by [`Recorder`]; symbols must not be read before it runs.
        hooked: bool,
        /// Whether the file may be loaded early (and its symbols read
        /// before its round's hook).
        early: bool,
        unloads: usize,
    }

    fn parse(spec: &'static str) -> (SymbolName<'static>, SymbolUse) {
        let (tag, name) = spec.split_once(':').unwrap();
        let use_ = match tag {
            "D" => SymbolUse::Definition {
                kind: DefinitionKind::Regular,
                aux: 0,
            },
            "W" => SymbolUse::Definition {
                kind: DefinitionKind::Weak,
                aux: 0,
            },
            "S" => SymbolUse::Definition {
                kind: DefinitionKind::Shared,
                aux: 0,
            },
            "U" => SymbolUse::Reference { weak: false },
            "u" => SymbolUse::Reference { weak: true },
            common => SymbolUse::Definition {
                kind: DefinitionKind::Common,
                aux: common[1..].parse().unwrap(),
            },
        };
        (SymbolName::new(name.as_bytes()), use_)
    }

    fn file(position: InputPosition, live: bool, specs: &[&'static str]) -> Mock {
        let (names, uses): (Vec<_>, Vec<_>) = specs.iter().map(|spec| parse(spec)).unzip();
        let lazy = names
            .iter()
            .zip(&uses)
            .filter(|(_, use_)| matches!(use_, SymbolUse::Definition { .. }))
            .map(|(name, _)| *name)
            .collect();
        Mock {
            position,
            live_at_start: live,
            lazy,
            names,
            uses,
            loads: 0,
            fail: false,
            hooked: false,
            early: false,
            unloads: 0,
        }
    }

    fn object(input: u32, specs: &[&'static str]) -> Mock {
        file(InputPosition::new(input, 0), true, specs)
    }

    fn member(input: u32, member: u32, specs: &[&'static str]) -> Mock {
        file(InputPosition::new(input, member), false, specs)
    }

    impl<'a> ResolveFile<'a> for Mock {
        fn position(&self) -> InputPosition {
            self.position
        }

        fn is_live_at_start(&self) -> bool {
            self.live_at_start
        }

        fn lazy_names(&self) -> &[SymbolName<'a>] {
            &self.lazy
        }

        fn load(&mut self) -> Result<()> {
            self.loads += 1;
            assert_eq!(self.loads, 1, "file loaded twice");
            if self.fail {
                return Err(Error::malformed(
                    format!("input{}", self.position.input()),
                    0,
                    "test",
                ));
            }
            Ok(())
        }

        fn symbol_names(&self) -> &[SymbolName<'a>] {
            assert!(
                self.loads == 1 && (self.hooked || self.early),
                "symbols read too early"
            );
            &self.names
        }

        fn symbol_use(&self, index: usize) -> SymbolUse {
            assert!(
                self.loads == 1 && (self.hooked || self.early),
                "symbols read too early"
            );
            self.uses[index]
        }

        fn can_load_early(&self) -> bool {
            self.early
        }

        fn unload(&mut self) {
            assert_eq!(self.loads, 1, "unloaded but not loaded");
            self.unloads += 1;
        }
    }

    /// Records each round's files and marks them hooked.
    #[derive(Default)]
    struct Recorder {
        rounds: Vec<Vec<(InputPosition, usize)>>,
        fail_in_round: Option<usize>,
    }

    impl RoundHook<Mock> for Recorder {
        fn after_load(&mut self, round: usize, files: &mut [RoundFile<'_, Mock>]) -> Result<()> {
            assert_eq!(round, self.rounds.len());
            let mut entries = Vec::new();
            for entry in files.iter_mut() {
                assert_eq!(entry.file.loads, 1, "hook before load");
                assert!(!entry.file.hooked, "file handed to the hook twice");
                entry.file.hooked = true;
                entries.push((entry.file.position, entry.id.index()));
            }
            self.rounds.push(entries);
            if self.fail_in_round == Some(round) {
                return Err(Error::Internal(format!("hook failed in round {round}")));
            }
            Ok(())
        }
    }

    fn run(files: &mut [Mock]) -> (SymbolTable<'static>, Resolution<'static>) {
        let mut table = SymbolTable::new();
        let resolution = resolve_symbols_with(
            &mut table,
            &ElfReferenceRules,
            files,
            &mut Recorder::default(),
        )
        .unwrap();
        (table, resolution)
    }

    fn id(table: &SymbolTable<'_>, name: &str) -> SymbolId {
        table.lookup(&SymbolName::new(name.as_bytes())).unwrap()
    }

    fn live(resolution: &Resolution<'_>) -> Vec<usize> {
        resolution.live_files().map(FileId::index).collect()
    }

    #[test]
    fn repeated_archive_gets_the_ids_of_the_first_copy() {
        // An archive named twice: the second copy's members either share
        // the first's names (as members of one mapped archive do, which
        // skips interning them) or have copies of them. IDs and the
        // resolution must not differ.
        let specs: [&[&'static str]; 3] = [&["D:f", "U:h"], &["D:g", "D:h"], &["D:x", "U:y"]];
        let build = |shared: bool| {
            let mut files = vec![object(0, &["U:f", "U:g", "D:main"])];
            let first: Vec<Mock> = specs
                .iter()
                .enumerate()
                .map(|(i, s)| member(1, i as u32, s))
                .collect();
            let mut second: Vec<Mock> = specs
                .iter()
                .enumerate()
                .map(|(i, s)| member(3, i as u32, s))
                .collect();
            for (copy, original) in second.iter_mut().zip(&first) {
                if shared {
                    copy.lazy = original.lazy.clone();
                    copy.names = original.names.clone();
                } else {
                    let fresh = |name: &SymbolName<'static>| {
                        SymbolName::new(Box::leak(name.bytes().to_vec().into_boxed_slice()))
                    };
                    copy.lazy = original.lazy.iter().map(fresh).collect();
                    copy.names = original.names.iter().map(fresh).collect();
                }
            }
            files.extend(first);
            files.push(object(2, &["U:x", "D:z"]));
            files.extend(second);
            files
        };
        let mut shared = build(true);
        let mut copied = build(false);
        let repeats = repeated_lazy_files(&shared, &[1, 2, 3, 5, 6, 7]);
        assert_eq!(repeats, [None, None, None, Some(1), Some(2), Some(3)]);
        let (table_a, resolution_a) = run(&mut shared);
        let (table_b, resolution_b) = run(&mut copied);
        let names = |table: &SymbolTable<'_>| {
            table
                .names()
                .iter()
                .map(|name| name.bytes().to_vec())
                .collect::<Vec<_>>()
        };
        assert_eq!(names(&table_a), names(&table_b));
        assert_eq!(live(&resolution_a), live(&resolution_b));
        for file in 0..shared.len() {
            let file = FileId::new(file);
            assert_eq!(resolution_a.symbol_ids(file), resolution_b.symbol_ids(file));
        }
    }

    #[test]
    fn extracts_transitively_and_skips_unneeded_members() {
        let mut files = [
            object(0, &["U:foo", "D:main"]),
            member(1, 0, &["D:foo", "U:bar"]),
            member(1, 1, &["D:bar"]),
            member(1, 2, &["D:unused"]),
        ];
        let (table, resolution) = run(&mut files);
        assert_eq!(live(&resolution), [0, 1, 2]);
        assert_eq!(
            resolution.extracted(),
            [vec![FileId::new(1)], vec![FileId::new(2)]]
        );
        assert!(resolution.undefined().is_empty());
        assert!(resolution.duplicates().is_empty());
        assert_eq!(
            table.definition_file(id(&table, "bar")),
            Some(FileId::new(2))
        );
        // The unextracted member's symbol stays lazy.
        assert_eq!(
            table.definition_kind(id(&table, "unused")),
            DefinitionKind::Lazy
        );
        assert!(
            table
                .flags(id(&table, "foo"))
                .contains(SymbolFlags::REFERENCED)
        );
        assert_eq!(resolution.symbol_ids(FileId::new(3)), []);
        assert_eq!(
            resolution.symbol_ids(FileId::new(1)),
            [id(&table, "foo"), id(&table, "bar")]
        );
    }

    #[test]
    fn archive_before_its_user_is_still_searched() {
        let mut files = [member(0, 0, &["D:foo"]), object(1, &["U:foo"])];
        let (_, resolution) = run(&mut files);
        assert_eq!(live(&resolution), [0, 1]);
        assert!(resolution.undefined().is_empty());
    }

    #[test]
    fn earliest_member_wins() {
        let mut files = [
            member(5, 0, &["D:foo"]),
            object(0, &["U:foo"]),
            member(2, 3, &["D:foo"]),
            member(2, 4, &["D:foo"]),
        ];
        let (table, resolution) = run(&mut files);
        assert_eq!(live(&resolution), [1, 2]);
        assert_eq!(
            table.definition_file(id(&table, "foo")),
            Some(FileId::new(2))
        );
    }

    #[test]
    fn weak_references_and_existing_definitions_do_not_extract() {
        let mut files = [
            object(
                0,
                &["u:weakref", "U:has_weak", "U:has_shared", "W:has_weak"],
            ),
            object(1, &["S:has_shared"]),
            member(2, 0, &["D:weakref"]),
            member(2, 1, &["D:has_weak"]),
            member(2, 2, &["D:has_shared"]),
        ];
        let (table, resolution) = run(&mut files);
        assert_eq!(live(&resolution), [0, 1]);
        assert!(resolution.undefined().is_empty());
        let weakref = id(&table, "weakref");
        assert!(table.flags(weakref).contains(SymbolFlags::WEAK_REFERENCED));
        assert!(!table.flags(weakref).contains(SymbolFlags::REFERENCED));
        assert_eq!(table.definition_kind(weakref), DefinitionKind::Lazy);
    }

    #[test]
    fn reports_undefined_and_duplicates_in_order() {
        let mut files = [
            object(3, &["U:zeta", "D:dup", "U:alpha"]),
            object(1, &["U:alpha", "D:dup", "C4:common", "u:weak_only"]),
            object(2, &["D:dup", "C8:common", "U:alpha"]),
        ];
        let (table, resolution) = run(&mut files);

        let undefined: Vec<(String, Vec<usize>)> = resolution
            .undefined()
            .iter()
            .map(|u| {
                (
                    u.name.display().to_string(),
                    u.references.iter().map(|r| r.file.index()).collect(),
                )
            })
            .collect();
        assert_eq!(
            undefined,
            [
                ("alpha".to_string(), vec![1, 2, 0]),
                ("zeta".to_string(), vec![0])
            ]
        );

        let [duplicate] = resolution.duplicates() else {
            panic!("expected one duplicate: {:?}", resolution.duplicates());
        };
        assert_eq!(duplicate.symbol, id(&table, "dup"));
        assert_eq!(duplicate.winner.file, FileId::new(1));
        let others: Vec<usize> = duplicate.others.iter().map(|d| d.file.index()).collect();
        assert_eq!(others, [2, 0]);

        let common = table.definition(id(&table, "common"));
        assert_eq!((common.file.index(), common.aux), (2, 8));
    }

    #[test]
    fn stale_lazy_index_terminates_and_reports() {
        let mut lying = member(1, 0, &["D:real"]);
        lying.lazy.push(SymbolName::new(b"ghost"));
        let mut files = [object(0, &["U:ghost", "U:real"]), lying];
        let (_, resolution) = run(&mut files);
        assert_eq!(live(&resolution), [0, 1]);
        let names: Vec<String> = resolution
            .undefined()
            .iter()
            .map(|u| u.name.display().to_string())
            .collect();
        assert_eq!(names, ["ghost"]);
    }

    #[test]
    fn load_error_of_earliest_input_is_returned() {
        let mut files = [object(4, &["U:foo"]), object(2, &["U:bar"]), object(3, &[])];
        files[0].fail = true;
        files[2].fail = true;
        let mut table = SymbolTable::new();
        let error = resolve_symbols(&mut table, &ElfReferenceRules, &mut files).unwrap_err();
        assert!(error.to_string().starts_with("input3"), "{error}");
    }

    #[test]
    fn hook_sees_each_rounds_new_files_in_position_order() {
        let mut files = [
            member(3, 1, &["D:c"]),
            object(2, &["U:a", "U:b"]),
            member(3, 0, &["D:a", "U:c"]),
            object(0, &["D:main"]),
            member(1, 0, &["D:b"]),
            member(1, 1, &["D:unused"]),
        ];
        let mut recorder = Recorder::default();
        let mut table = SymbolTable::new();
        let resolution =
            resolve_symbols_with(&mut table, &ElfReferenceRules, &mut files, &mut recorder)
                .unwrap();
        let p = InputPosition::new;
        assert_eq!(
            recorder.rounds,
            [
                vec![(p(0, 0), 3), (p(2, 0), 1)],
                vec![(p(1, 0), 4), (p(3, 0), 2)],
                vec![(p(3, 1), 0)],
            ]
        );
        assert_eq!(resolution.extracted().len(), 2);
        assert!(!files[5].hooked);
    }

    /// Keeps names and marks files hooked as they load, so the driver
    /// looks their names up while loading.
    #[derive(Default)]
    struct EarlyHook {
        loaded: std::sync::atomic::AtomicUsize,
        rounds: usize,
    }

    impl LoadHook<Mock> for EarlyHook {
        fn keeps_names(&self, _: &Mock) -> bool {
            true
        }

        fn prepare(&self, _: FileId, file: &mut Mock) -> Result<()> {
            assert_eq!(file.loads, 1, "hook before load");
            assert!(!file.hooked, "file prepared twice");
            file.hooked = true;
            Ok(())
        }

        fn on_load(&self, round: usize, _: FileId, _: u32, file: &mut Mock) -> Result<()> {
            assert_eq!(round, self.rounds);
            assert!(file.hooked, "on_load before prepare");
            self.loaded
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            Ok(())
        }
    }

    impl RoundHook<Mock> for EarlyHook {
        fn load_hook(&self) -> Option<&dyn LoadHook<Mock>> {
            Some(self)
        }

        fn after_load(&mut self, round: usize, files: &mut [RoundFile<'_, Mock>]) -> Result<()> {
            assert_eq!(round, self.rounds);
            assert!(files.iter().all(|entry| entry.file.hooked));
            // Ranks follow positions.
            assert!(files.iter().all(|entry| entry.rank.is_some()));
            assert!(files.windows(2).all(|pair| pair[0].rank < pair[1].rank));
            self.rounds += 1;
            Ok(())
        }
    }

    #[test]
    fn names_looked_up_while_loading_get_the_same_ids() {
        // Later rounds bring both known names and new ones (`late*`, only
        // referenced or defined by extracted members), in several files.
        let build = || {
            vec![
                object(0, &["U:a", "D:main", "U:b"]),
                member(1, 0, &["D:a", "U:late1", "U:c", "D:late2"]),
                member(1, 1, &["D:b", "U:late2", "U:late3", "U:late1"]),
                member(1, 2, &["D:c", "U:d", "u:late4"]),
                member(2, 0, &["D:d", "U:late5", "D:late3"]),
                member(2, 1, &["D:unused", "U:never"]),
            ]
        };
        let mut plain = build();
        let (table_a, resolution_a) = run(&mut plain);
        for threads in [1, 3] {
            let mut early = build();
            let mut table_b = SymbolTable::new();
            let mut hook = EarlyHook::default();
            let resolution_b = rayon::ThreadPoolBuilder::new()
                .num_threads(threads)
                .build()
                .unwrap()
                .install(|| {
                    resolve_symbols_with(&mut table_b, &ElfReferenceRules, &mut early, &mut hook)
                })
                .unwrap();
            assert_eq!(table_a.names(), table_b.names());
            assert_eq!(live(&resolution_a), live(&resolution_b));
            assert_eq!(resolution_a.extracted(), resolution_b.extracted());
            for file in 0..plain.len() {
                let file = FileId::new(file);
                assert_eq!(resolution_a.symbol_ids(file), resolution_b.symbol_ids(file));
            }
            assert_eq!(hook.rounds, resolution_b.extracted().len() + 1);
            assert_eq!(
                hook.loaded.load(std::sync::atomic::Ordering::Relaxed),
                live(&resolution_b).len()
            );
        }
    }

    #[test]
    fn members_loaded_early_resolve_the_same() {
        // `x` would define `s` under the first round's table, so it is
        // loaded early once `a` references `s`; but `c`, extracted in the
        // same round as `a`, defines `s` (weakly), so `x` never becomes
        // live. `bad`, which fails to load, is needed only by `x`. `y1` and
        // `y2` take two more rounds.
        let build = |early: bool| {
            let mut files = vec![
                object(0, &["U:a1", "U:c1", "D:main"]),
                member(1, 0, &["D:s", "U:bad1"]),
                member(1, 1, &["D:a1", "U:s", "U:y1"]),
                member(1, 2, &["D:c1", "W:s"]),
                member(2, 0, &["D:y1", "U:y2", "U:late"]),
                member(2, 1, &["D:y2", "D:late"]),
                member(3, 0, &["D:bad1"]),
            ];
            files[6].fail = true;
            for file in &mut files {
                file.early = early && !file.live_at_start;
            }
            files
        };
        let mut plain = build(false);
        let (table_a, resolution_a) = run(&mut plain);
        assert_eq!(live(&resolution_a), [0, 2, 3, 4, 5]);
        for threads in [1, 3] {
            let mut early = build(true);
            let mut table_b = SymbolTable::new();
            let mut hook = EarlyHook::default();
            let resolution_b = rayon::ThreadPoolBuilder::new()
                .num_threads(threads)
                .build()
                .unwrap()
                .install(|| {
                    resolve_symbols_with(&mut table_b, &ElfReferenceRules, &mut early, &mut hook)
                })
                .unwrap();
            assert_eq!(table_a.names(), table_b.names());
            assert_eq!(live(&resolution_a), live(&resolution_b));
            assert_eq!(resolution_a.extracted(), resolution_b.extracted());
            for file in 0..plain.len() {
                let id = FileId::new(file);
                assert_eq!(resolution_a.symbol_ids(id), resolution_b.symbol_ids(id));
            }
            for id in table_a.ids() {
                assert_eq!(table_a.definition(id), table_b.definition(id));
            }
            // `x` and `bad` were loaded early (`bad` failing silently) and
            // unloaded; the others were loaded once and kept.
            for (index, file) in early.iter().enumerate() {
                let dropped = usize::from(index == 1 || index == 6);
                assert_eq!((file.loads, file.unloads), (1, dropped), "file {index}");
            }
        }

        // A member that fails to load is an error once it is extracted,
        // whether it was loaded early or not.
        for early in [false, true] {
            let mut files = build(early);
            files[0].names.push(SymbolName::new(b"bad1"));
            files[0].uses.push(SymbolUse::Reference { weak: false });
            let mut table = SymbolTable::new();
            let error = resolve_symbols_with(
                &mut table,
                &ElfReferenceRules,
                &mut files,
                &mut EarlyHook::default(),
            )
            .unwrap_err();
            assert!(error.to_string().starts_with("input3"), "{error}");
        }
    }

    #[test]
    fn hook_error_stops_resolution() {
        let mut files = [object(0, &["U:a"]), member(1, 0, &["D:a"])];
        let mut recorder = Recorder {
            fail_in_round: Some(1),
            ..Recorder::default()
        };
        let mut table = SymbolTable::new();
        let error = resolve_symbols_with(&mut table, &ElfReferenceRules, &mut files, &mut recorder)
            .unwrap_err();
        assert!(matches!(error, Error::Internal(_)), "{error}");
        assert_eq!(recorder.rounds.len(), 2);
    }

    #[test]
    fn symbol_table_overflow_is_a_limit_error() {
        // Round 0 interns main, a, b; extracting the member adds c and d.
        let mut files = [
            object(0, &["D:main", "U:a"]),
            member(1, 0, &["D:a", "D:b", "U:c", "U:d"]),
        ];
        for (limit, ok) in [(5, true), (4, false), (2, false)] {
            for file in &mut files {
                file.loads = 0;
                file.hooked = false;
            }
            let mut table = SymbolTable::new();
            table.set_limit(limit);
            let result = resolve_symbols_with(
                &mut table,
                &ElfReferenceRules,
                &mut files,
                &mut Recorder::default(),
            );
            match result {
                Ok(_) => assert!(ok, "limit {limit}"),
                Err(Error::Limit(message)) => {
                    assert!(!ok, "limit {limit}: {message}");
                    assert!(table.len() <= limit);
                }
                Err(other) => panic!("limit {limit}: {other}"),
            }
        }
    }
}