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
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
|
//! The main driver of the COFF linker.
//! Currently uses our own implementation for the incremental linker, and falls back to
//! LLD for traditional linking (linking relocatable object files).
//! LLD is also the default linker for LLVM.
/// If this is not null, an object file is created by LLVM and emitted to zcu_object_sub_path.
llvm_object: ?*LlvmObject = null,
base: link.File,
image_base: u64,
subsystem: ?std.Target.SubSystem,
tsaware: bool,
nxcompat: bool,
dynamicbase: bool,
/// TODO this and minor_subsystem_version should be combined into one property and left as
/// default or populated together. They should not be separate fields.
major_subsystem_version: u16,
minor_subsystem_version: u16,
lib_dirs: []const []const u8,
entry: link.File.OpenOptions.Entry,
entry_addr: ?u32,
module_definition_file: ?[]const u8,
pdb_out_path: ?[]const u8,
ptr_width: PtrWidth,
page_size: u32,
objects: std.ArrayListUnmanaged(Object) = .{},
sections: std.MultiArrayList(Section) = .{},
data_directories: [coff.IMAGE_NUMBEROF_DIRECTORY_ENTRIES]coff.ImageDataDirectory,
text_section_index: ?u16 = null,
got_section_index: ?u16 = null,
rdata_section_index: ?u16 = null,
data_section_index: ?u16 = null,
reloc_section_index: ?u16 = null,
idata_section_index: ?u16 = null,
locals: std.ArrayListUnmanaged(coff.Symbol) = .{},
globals: std.ArrayListUnmanaged(SymbolWithLoc) = .{},
resolver: std.StringHashMapUnmanaged(u32) = .{},
unresolved: std.AutoArrayHashMapUnmanaged(u32, bool) = .{},
need_got_table: std.AutoHashMapUnmanaged(u32, void) = .{},
locals_free_list: std.ArrayListUnmanaged(u32) = .{},
globals_free_list: std.ArrayListUnmanaged(u32) = .{},
strtab: StringTable = .{},
strtab_offset: ?u32 = null,
temp_strtab: StringTable = .{},
got_table: TableSection(SymbolWithLoc) = .{},
/// A table of ImportTables partitioned by the library name.
/// Key is an offset into the interning string table `temp_strtab`.
import_tables: std.AutoArrayHashMapUnmanaged(u32, ImportTable) = .{},
got_table_count_dirty: bool = true,
got_table_contents_dirty: bool = true,
imports_count_dirty: bool = true,
/// Table of tracked LazySymbols.
lazy_syms: LazySymbolTable = .{},
/// Table of tracked Decls.
decls: DeclTable = .{},
/// List of atoms that are either synthetic or map directly to the Zig source program.
atoms: std.ArrayListUnmanaged(Atom) = .{},
/// Table of atoms indexed by the symbol index.
atom_by_index_table: std.AutoHashMapUnmanaged(u32, Atom.Index) = .{},
/// Table of unnamed constants associated with a parent `Decl`.
/// We store them here so that we can free the constants whenever the `Decl`
/// needs updating or is freed.
///
/// For example,
///
/// ```zig
/// const Foo = struct{
/// a: u8,
/// };
///
/// pub fn main() void {
/// var foo = Foo{ .a = 1 };
/// _ = foo;
/// }
/// ```
///
/// value assigned to label `foo` is an unnamed constant belonging/associated
/// with `Decl` `main`, and lives as long as that `Decl`.
unnamed_const_atoms: UnnamedConstTable = .{},
anon_decls: AnonDeclTable = .{},
/// A table of relocations indexed by the owning them `Atom`.
/// Note that once we refactor `Atom`'s lifetime and ownership rules,
/// this will be a table indexed by index into the list of Atoms.
relocs: RelocTable = .{},
/// A table of base relocations indexed by the owning them `Atom`.
/// Note that once we refactor `Atom`'s lifetime and ownership rules,
/// this will be a table indexed by index into the list of Atoms.
base_relocs: BaseRelocationTable = .{},
/// Hot-code swapping state.
hot_state: if (is_hot_update_compatible) HotUpdateState else struct {} = .{},
const is_hot_update_compatible = switch (builtin.target.os.tag) {
.windows => true,
else => false,
};
const HotUpdateState = struct {
/// Base address at which the process (image) got loaded.
/// We need this info to correctly slide pointers when relocating.
loaded_base_address: ?std.os.windows.HMODULE = null,
};
const DeclTable = std.AutoArrayHashMapUnmanaged(InternPool.DeclIndex, DeclMetadata);
const AnonDeclTable = std.AutoHashMapUnmanaged(InternPool.Index, DeclMetadata);
const RelocTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Relocation));
const BaseRelocationTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(u32));
const UnnamedConstTable = std.AutoArrayHashMapUnmanaged(InternPool.DeclIndex, std.ArrayListUnmanaged(Atom.Index));
const default_file_alignment: u16 = 0x200;
const default_size_of_stack_reserve: u32 = 0x1000000;
const default_size_of_stack_commit: u32 = 0x1000;
const default_size_of_heap_reserve: u32 = 0x100000;
const default_size_of_heap_commit: u32 = 0x1000;
const Section = struct {
header: coff.SectionHeader,
last_atom_index: ?Atom.Index = null,
/// A list of atoms that have surplus capacity. This list can have false
/// positives, as functions grow and shrink over time, only sometimes being added
/// or removed from the freelist.
///
/// An atom has surplus capacity when its overcapacity value is greater than
/// padToIdeal(minimum_atom_size). That is, when it has so
/// much extra capacity, that we could fit a small new symbol in it, itself with
/// ideal_capacity or more.
///
/// Ideal capacity is defined by size + (size / ideal_factor).
///
/// Overcapacity is measured by actual_capacity - ideal_capacity. Note that
/// overcapacity can be negative. A simple way to have negative overcapacity is to
/// allocate a fresh atom, which will have ideal capacity, and then grow it
/// by 1 byte. It will then have -1 overcapacity.
free_list: std.ArrayListUnmanaged(Atom.Index) = .{},
};
const LazySymbolTable = std.AutoArrayHashMapUnmanaged(InternPool.OptionalDeclIndex, LazySymbolMetadata);
const LazySymbolMetadata = struct {
const State = enum { unused, pending_flush, flushed };
text_atom: Atom.Index = undefined,
rdata_atom: Atom.Index = undefined,
text_state: State = .unused,
rdata_state: State = .unused,
};
const DeclMetadata = struct {
atom: Atom.Index,
section: u16,
/// A list of all exports aliases of this Decl.
exports: std.ArrayListUnmanaged(u32) = .{},
fn deinit(m: *DeclMetadata, allocator: Allocator) void {
m.exports.deinit(allocator);
}
fn getExport(m: DeclMetadata, coff_file: *const Coff, name: []const u8) ?u32 {
for (m.exports.items) |exp| {
if (mem.eql(u8, name, coff_file.getSymbolName(.{
.sym_index = exp,
.file = null,
}))) return exp;
}
return null;
}
fn getExportPtr(m: *DeclMetadata, coff_file: *Coff, name: []const u8) ?*u32 {
for (m.exports.items) |*exp| {
if (mem.eql(u8, name, coff_file.getSymbolName(.{
.sym_index = exp.*,
.file = null,
}))) return exp;
}
return null;
}
};
pub const PtrWidth = enum {
p32,
p64,
/// Size in bytes.
pub fn size(pw: PtrWidth) u4 {
return switch (pw) {
.p32 => 4,
.p64 => 8,
};
}
};
pub const SymbolWithLoc = struct {
// Index into the respective symbol table.
sym_index: u32,
// null means it's a synthetic global or Zig source.
file: ?u32 = null,
pub fn eql(this: SymbolWithLoc, other: SymbolWithLoc) bool {
if (this.file == null and other.file == null) {
return this.sym_index == other.sym_index;
}
if (this.file != null and other.file != null) {
return this.sym_index == other.sym_index and this.file.? == other.file.?;
}
return false;
}
};
/// When allocating, the ideal_capacity is calculated by
/// actual_capacity + (actual_capacity / ideal_factor)
const ideal_factor = 3;
/// In order for a slice of bytes to be considered eligible to keep metadata pointing at
/// it as a possible place to put new symbols, it must have enough room for this many bytes
/// (plus extra for reserved capacity).
const minimum_text_block_size = 64;
pub const min_text_capacity = padToIdeal(minimum_text_block_size);
pub fn createEmpty(
arena: Allocator,
comp: *Compilation,
emit: Compilation.Emit,
options: link.File.OpenOptions,
) !*Coff {
const target = comp.root_mod.resolved_target.result;
assert(target.ofmt == .coff);
const optimize_mode = comp.root_mod.optimize_mode;
const output_mode = comp.config.output_mode;
const link_mode = comp.config.link_mode;
const use_llvm = comp.config.use_llvm;
const use_lld = build_options.have_llvm and comp.config.use_lld;
const ptr_width: PtrWidth = switch (target.ptrBitWidth()) {
0...32 => .p32,
33...64 => .p64,
else => return error.UnsupportedCOFFArchitecture,
};
const page_size: u32 = switch (target.cpu.arch) {
else => 0x1000,
};
// If using LLD to link, this code should produce an object file so that it
// can be passed to LLD.
// If using LLVM to generate the object file for the zig compilation unit,
// we need a place to put the object file so that it can be subsequently
// handled.
const zcu_object_sub_path = if (!use_lld and !use_llvm)
null
else
try std.fmt.allocPrint(arena, "{s}.obj", .{emit.sub_path});
const self = try arena.create(Coff);
self.* = .{
.base = .{
.tag = .coff,
.comp = comp,
.emit = emit,
.zcu_object_sub_path = zcu_object_sub_path,
.stack_size = options.stack_size orelse 16777216,
.gc_sections = options.gc_sections orelse (optimize_mode != .Debug),
.print_gc_sections = options.print_gc_sections,
.allow_shlib_undefined = options.allow_shlib_undefined orelse false,
.file = null,
.disable_lld_caching = options.disable_lld_caching,
.build_id = options.build_id,
.rpath_list = options.rpath_list,
},
.ptr_width = ptr_width,
.page_size = page_size,
.data_directories = [1]coff.ImageDataDirectory{.{
.virtual_address = 0,
.size = 0,
}} ** coff.IMAGE_NUMBEROF_DIRECTORY_ENTRIES,
.image_base = options.image_base orelse switch (output_mode) {
.Exe => switch (target.cpu.arch) {
.aarch64 => 0x140000000,
.x86_64, .x86 => 0x400000,
else => unreachable,
},
.Lib => 0x10000000,
.Obj => 0,
},
// Subsystem depends on the set of public symbol names from linked objects.
// See LinkerDriver::inferSubsystem from the LLD project for the flow chart.
.subsystem = options.subsystem,
.entry = options.entry,
.tsaware = options.tsaware,
.nxcompat = options.nxcompat,
.dynamicbase = options.dynamicbase,
.major_subsystem_version = options.major_subsystem_version orelse 6,
.minor_subsystem_version = options.minor_subsystem_version orelse 0,
.lib_dirs = options.lib_dirs,
.entry_addr = math.cast(u32, options.entry_addr orelse 0) orelse
return error.EntryAddressTooBig,
.module_definition_file = options.module_definition_file,
.pdb_out_path = options.pdb_out_path,
};
if (use_llvm and comp.config.have_zcu) {
self.llvm_object = try LlvmObject.create(arena, comp);
}
errdefer self.base.destroy();
if (use_lld and (use_llvm or !comp.config.have_zcu)) {
// LLVM emits the object file (if any); LLD links it into the final product.
return self;
}
// What path should this COFF linker code output to?
// If using LLD to link, this code should produce an object file so that it
// can be passed to LLD.
const sub_path = if (use_lld) zcu_object_sub_path.? else emit.sub_path;
self.base.file = try emit.directory.handle.createFile(sub_path, .{
.truncate = true,
.read = true,
.mode = link.File.determineMode(use_lld, output_mode, link_mode),
});
assert(self.llvm_object == null);
const gpa = comp.gpa;
try self.strtab.buffer.ensureUnusedCapacity(gpa, @sizeOf(u32));
self.strtab.buffer.appendNTimesAssumeCapacity(0, @sizeOf(u32));
try self.temp_strtab.buffer.append(gpa, 0);
// Index 0 is always a null symbol.
try self.locals.append(gpa, .{
.name = [_]u8{0} ** 8,
.value = 0,
.section_number = .UNDEFINED,
.type = .{ .base_type = .NULL, .complex_type = .NULL },
.storage_class = .NULL,
.number_of_aux_symbols = 0,
});
if (self.text_section_index == null) {
const file_size: u32 = @intCast(options.program_code_size_hint);
self.text_section_index = try self.allocateSection(".text", file_size, .{
.CNT_CODE = 1,
.MEM_EXECUTE = 1,
.MEM_READ = 1,
});
}
if (self.got_section_index == null) {
const file_size = @as(u32, @intCast(options.symbol_count_hint)) * self.ptr_width.size();
self.got_section_index = try self.allocateSection(".got", file_size, .{
.CNT_INITIALIZED_DATA = 1,
.MEM_READ = 1,
});
}
if (self.rdata_section_index == null) {
const file_size: u32 = self.page_size;
self.rdata_section_index = try self.allocateSection(".rdata", file_size, .{
.CNT_INITIALIZED_DATA = 1,
.MEM_READ = 1,
});
}
if (self.data_section_index == null) {
const file_size: u32 = self.page_size;
self.data_section_index = try self.allocateSection(".data", file_size, .{
.CNT_INITIALIZED_DATA = 1,
.MEM_READ = 1,
.MEM_WRITE = 1,
});
}
if (self.idata_section_index == null) {
const file_size = @as(u32, @intCast(options.symbol_count_hint)) * self.ptr_width.size();
self.idata_section_index = try self.allocateSection(".idata", file_size, .{
.CNT_INITIALIZED_DATA = 1,
.MEM_READ = 1,
});
}
if (self.reloc_section_index == null) {
const file_size = @as(u32, @intCast(options.symbol_count_hint)) * @sizeOf(coff.BaseRelocation);
self.reloc_section_index = try self.allocateSection(".reloc", file_size, .{
.CNT_INITIALIZED_DATA = 1,
.MEM_DISCARDABLE = 1,
.MEM_READ = 1,
});
}
if (self.strtab_offset == null) {
const file_size = @as(u32, @intCast(self.strtab.buffer.items.len));
self.strtab_offset = self.findFreeSpace(file_size, @alignOf(u32)); // 4bytes aligned seems like a good idea here
log.debug("found strtab free space 0x{x} to 0x{x}", .{ self.strtab_offset.?, self.strtab_offset.? + file_size });
}
{
// We need to find out what the max file offset is according to section headers.
// Otherwise, we may end up with an COFF binary with file size not matching the final section's
// offset + it's filesize.
// TODO I don't like this here one bit
var max_file_offset: u64 = 0;
for (self.sections.items(.header)) |header| {
if (header.pointer_to_raw_data + header.size_of_raw_data > max_file_offset) {
max_file_offset = header.pointer_to_raw_data + header.size_of_raw_data;
}
}
try self.base.file.?.pwriteAll(&[_]u8{0}, max_file_offset);
}
return self;
}
pub fn open(
arena: Allocator,
comp: *Compilation,
emit: Compilation.Emit,
options: link.File.OpenOptions,
) !*Coff {
// TODO: restore saved linker state, don't truncate the file, and
// participate in incremental compilation.
return createEmpty(arena, comp, emit, options);
}
pub fn deinit(self: *Coff) void {
const gpa = self.base.comp.gpa;
if (self.llvm_object) |llvm_object| llvm_object.deinit();
for (self.objects.items) |*object| {
object.deinit(gpa);
}
self.objects.deinit(gpa);
for (self.sections.items(.free_list)) |*free_list| {
free_list.deinit(gpa);
}
self.sections.deinit(gpa);
self.atoms.deinit(gpa);
self.locals.deinit(gpa);
self.globals.deinit(gpa);
{
var it = self.resolver.keyIterator();
while (it.next()) |key_ptr| {
gpa.free(key_ptr.*);
}
self.resolver.deinit(gpa);
}
self.unresolved.deinit(gpa);
self.locals_free_list.deinit(gpa);
self.globals_free_list.deinit(gpa);
self.strtab.deinit(gpa);
self.temp_strtab.deinit(gpa);
self.got_table.deinit(gpa);
for (self.import_tables.values()) |*itab| {
itab.deinit(gpa);
}
self.import_tables.deinit(gpa);
self.lazy_syms.deinit(gpa);
for (self.decls.values()) |*metadata| {
metadata.deinit(gpa);
}
self.decls.deinit(gpa);
self.atom_by_index_table.deinit(gpa);
for (self.unnamed_const_atoms.values()) |*atoms| {
atoms.deinit(gpa);
}
self.unnamed_const_atoms.deinit(gpa);
{
var it = self.anon_decls.iterator();
while (it.next()) |entry| {
entry.value_ptr.exports.deinit(gpa);
}
self.anon_decls.deinit(gpa);
}
for (self.relocs.values()) |*relocs| {
relocs.deinit(gpa);
}
self.relocs.deinit(gpa);
for (self.base_relocs.values()) |*relocs| {
relocs.deinit(gpa);
}
self.base_relocs.deinit(gpa);
}
fn allocateSection(self: *Coff, name: []const u8, size: u32, flags: coff.SectionHeaderFlags) !u16 {
const index = @as(u16, @intCast(self.sections.slice().len));
const off = self.findFreeSpace(size, default_file_alignment);
// Memory is always allocated in sequence
// TODO: investigate if we can allocate .text last; this way it would never need to grow in memory!
const vaddr = blk: {
if (index == 0) break :blk self.page_size;
const prev_header = self.sections.items(.header)[index - 1];
break :blk mem.alignForward(u32, prev_header.virtual_address + prev_header.virtual_size, self.page_size);
};
// We commit more memory than needed upfront so that we don't have to reallocate too soon.
const memsz = mem.alignForward(u32, size, self.page_size) * 100;
log.debug("found {s} free space 0x{x} to 0x{x} (0x{x} - 0x{x})", .{
name,
off,
off + size,
vaddr,
vaddr + size,
});
var header = coff.SectionHeader{
.name = undefined,
.virtual_size = memsz,
.virtual_address = vaddr,
.size_of_raw_data = size,
.pointer_to_raw_data = off,
.pointer_to_relocations = 0,
.pointer_to_linenumbers = 0,
.number_of_relocations = 0,
.number_of_linenumbers = 0,
.flags = flags,
};
const gpa = self.base.comp.gpa;
try self.setSectionName(&header, name);
try self.sections.append(gpa, .{ .header = header });
return index;
}
fn growSection(self: *Coff, sect_id: u32, needed_size: u32) !void {
const header = &self.sections.items(.header)[sect_id];
const maybe_last_atom_index = self.sections.items(.last_atom_index)[sect_id];
const sect_capacity = self.allocatedSize(header.pointer_to_raw_data);
if (needed_size > sect_capacity) {
const new_offset = self.findFreeSpace(needed_size, default_file_alignment);
const current_size = if (maybe_last_atom_index) |last_atom_index| blk: {
const last_atom = self.getAtom(last_atom_index);
const sym = last_atom.getSymbol(self);
break :blk (sym.value + last_atom.size) - header.virtual_address;
} else 0;
log.debug("moving {s} from 0x{x} to 0x{x}", .{
self.getSectionName(header),
header.pointer_to_raw_data,
new_offset,
});
const amt = try self.base.file.?.copyRangeAll(
header.pointer_to_raw_data,
self.base.file.?,
new_offset,
current_size,
);
if (amt != current_size) return error.InputOutput;
header.pointer_to_raw_data = new_offset;
}
const sect_vm_capacity = self.allocatedVirtualSize(header.virtual_address);
if (needed_size > sect_vm_capacity) {
self.markRelocsDirtyByAddress(header.virtual_address + header.virtual_size);
try self.growSectionVirtualMemory(sect_id, needed_size);
}
header.virtual_size = @max(header.virtual_size, needed_size);
header.size_of_raw_data = needed_size;
}
fn growSectionVirtualMemory(self: *Coff, sect_id: u32, needed_size: u32) !void {
const header = &self.sections.items(.header)[sect_id];
const increased_size = padToIdeal(needed_size);
const old_aligned_end = header.virtual_address + mem.alignForward(u32, header.virtual_size, self.page_size);
const new_aligned_end = header.virtual_address + mem.alignForward(u32, increased_size, self.page_size);
const diff = new_aligned_end - old_aligned_end;
log.debug("growing {s} in virtual memory by {x}", .{ self.getSectionName(header), diff });
// TODO: enforce order by increasing VM addresses in self.sections container.
// This is required by the loader anyhow as far as I can tell.
for (self.sections.items(.header)[sect_id + 1 ..], 0..) |*next_header, next_sect_id| {
const maybe_last_atom_index = self.sections.items(.last_atom_index)[sect_id + 1 + next_sect_id];
next_header.virtual_address += diff;
if (maybe_last_atom_index) |last_atom_index| {
var atom_index = last_atom_index;
while (true) {
const atom = self.getAtom(atom_index);
const sym = atom.getSymbolPtr(self);
sym.value += diff;
if (atom.prev_index) |prev_index| {
atom_index = prev_index;
} else break;
}
}
}
header.virtual_size = increased_size;
}
fn allocateAtom(self: *Coff, atom_index: Atom.Index, new_atom_size: u32, alignment: u32) !u32 {
const tracy = trace(@src());
defer tracy.end();
const atom = self.getAtom(atom_index);
const sect_id = @intFromEnum(atom.getSymbol(self).section_number) - 1;
const header = &self.sections.items(.header)[sect_id];
const free_list = &self.sections.items(.free_list)[sect_id];
const maybe_last_atom_index = &self.sections.items(.last_atom_index)[sect_id];
const new_atom_ideal_capacity = if (header.isCode()) padToIdeal(new_atom_size) else new_atom_size;
// We use these to indicate our intention to update metadata, placing the new atom,
// and possibly removing a free list node.
// It would be simpler to do it inside the for loop below, but that would cause a
// problem if an error was returned later in the function. So this action
// is actually carried out at the end of the function, when errors are no longer possible.
var atom_placement: ?Atom.Index = null;
var free_list_removal: ?usize = null;
// First we look for an appropriately sized free list node.
// The list is unordered. We'll just take the first thing that works.
const vaddr = blk: {
var i: usize = 0;
while (i < free_list.items.len) {
const big_atom_index = free_list.items[i];
const big_atom = self.getAtom(big_atom_index);
// We now have a pointer to a live atom that has too much capacity.
// Is it enough that we could fit this new atom?
const sym = big_atom.getSymbol(self);
const capacity = big_atom.capacity(self);
const ideal_capacity = if (header.isCode()) padToIdeal(capacity) else capacity;
const ideal_capacity_end_vaddr = math.add(u32, sym.value, ideal_capacity) catch ideal_capacity;
const capacity_end_vaddr = sym.value + capacity;
const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity;
const new_start_vaddr = mem.alignBackward(u32, new_start_vaddr_unaligned, alignment);
if (new_start_vaddr < ideal_capacity_end_vaddr) {
// Additional bookkeeping here to notice if this free list node
// should be deleted because the atom that it points to has grown to take up
// more of the extra capacity.
if (!big_atom.freeListEligible(self)) {
_ = free_list.swapRemove(i);
} else {
i += 1;
}
continue;
}
// At this point we know that we will place the new atom here. But the
// remaining question is whether there is still yet enough capacity left
// over for there to still be a free list node.
const remaining_capacity = new_start_vaddr - ideal_capacity_end_vaddr;
const keep_free_list_node = remaining_capacity >= min_text_capacity;
// Set up the metadata to be updated, after errors are no longer possible.
atom_placement = big_atom_index;
if (!keep_free_list_node) {
free_list_removal = i;
}
break :blk new_start_vaddr;
} else if (maybe_last_atom_index.*) |last_index| {
const last = self.getAtom(last_index);
const last_symbol = last.getSymbol(self);
const ideal_capacity = if (header.isCode()) padToIdeal(last.size) else last.size;
const ideal_capacity_end_vaddr = last_symbol.value + ideal_capacity;
const new_start_vaddr = mem.alignForward(u32, ideal_capacity_end_vaddr, alignment);
atom_placement = last_index;
break :blk new_start_vaddr;
} else {
break :blk mem.alignForward(u32, header.virtual_address, alignment);
}
};
const expand_section = if (atom_placement) |placement_index|
self.getAtom(placement_index).next_index == null
else
true;
if (expand_section) {
const needed_size: u32 = (vaddr + new_atom_size) - header.virtual_address;
try self.growSection(sect_id, needed_size);
maybe_last_atom_index.* = atom_index;
}
self.getAtomPtr(atom_index).size = new_atom_size;
if (atom.prev_index) |prev_index| {
const prev = self.getAtomPtr(prev_index);
prev.next_index = atom.next_index;
}
if (atom.next_index) |next_index| {
const next = self.getAtomPtr(next_index);
next.prev_index = atom.prev_index;
}
if (atom_placement) |big_atom_index| {
const big_atom = self.getAtomPtr(big_atom_index);
const atom_ptr = self.getAtomPtr(atom_index);
atom_ptr.prev_index = big_atom_index;
atom_ptr.next_index = big_atom.next_index;
big_atom.next_index = atom_index;
} else {
const atom_ptr = self.getAtomPtr(atom_index);
atom_ptr.prev_index = null;
atom_ptr.next_index = null;
}
if (free_list_removal) |i| {
_ = free_list.swapRemove(i);
}
return vaddr;
}
pub fn allocateSymbol(self: *Coff) !u32 {
const gpa = self.base.comp.gpa;
try self.locals.ensureUnusedCapacity(gpa, 1);
const index = blk: {
if (self.locals_free_list.popOrNull()) |index| {
log.debug(" (reusing symbol index {d})", .{index});
break :blk index;
} else {
log.debug(" (allocating symbol index {d})", .{self.locals.items.len});
const index = @as(u32, @intCast(self.locals.items.len));
_ = self.locals.addOneAssumeCapacity();
break :blk index;
}
};
self.locals.items[index] = .{
.name = [_]u8{0} ** 8,
.value = 0,
.section_number = .UNDEFINED,
.type = .{ .base_type = .NULL, .complex_type = .NULL },
.storage_class = .NULL,
.number_of_aux_symbols = 0,
};
return index;
}
fn allocateGlobal(self: *Coff) !u32 {
const gpa = self.base.comp.gpa;
try self.globals.ensureUnusedCapacity(gpa, 1);
const index = blk: {
if (self.globals_free_list.popOrNull()) |index| {
log.debug(" (reusing global index {d})", .{index});
break :blk index;
} else {
log.debug(" (allocating global index {d})", .{self.globals.items.len});
const index = @as(u32, @intCast(self.globals.items.len));
_ = self.globals.addOneAssumeCapacity();
break :blk index;
}
};
self.globals.items[index] = .{
.sym_index = 0,
.file = null,
};
return index;
}
fn addGotEntry(self: *Coff, target: SymbolWithLoc) !void {
const gpa = self.base.comp.gpa;
if (self.got_table.lookup.contains(target)) return;
const got_index = try self.got_table.allocateEntry(gpa, target);
try self.writeOffsetTableEntry(got_index);
self.got_table_count_dirty = true;
self.markRelocsDirtyByTarget(target);
}
pub fn createAtom(self: *Coff) !Atom.Index {
const gpa = self.base.comp.gpa;
const atom_index = @as(Atom.Index, @intCast(self.atoms.items.len));
const atom = try self.atoms.addOne(gpa);
const sym_index = try self.allocateSymbol();
try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom_index);
atom.* = .{
.sym_index = sym_index,
.file = null,
.size = 0,
.prev_index = null,
.next_index = null,
};
log.debug("creating ATOM(%{d}) at index {d}", .{ sym_index, atom_index });
return atom_index;
}
fn growAtom(self: *Coff, atom_index: Atom.Index, new_atom_size: u32, alignment: u32) !u32 {
const atom = self.getAtom(atom_index);
const sym = atom.getSymbol(self);
const align_ok = mem.alignBackward(u32, sym.value, alignment) == sym.value;
const need_realloc = !align_ok or new_atom_size > atom.capacity(self);
if (!need_realloc) return sym.value;
return self.allocateAtom(atom_index, new_atom_size, alignment);
}
fn shrinkAtom(self: *Coff, atom_index: Atom.Index, new_block_size: u32) void {
_ = self;
_ = atom_index;
_ = new_block_size;
// TODO check the new capacity, and if it crosses the size threshold into a big enough
// capacity, insert a free list node for it.
}
fn writeAtom(self: *Coff, atom_index: Atom.Index, code: []u8) !void {
const atom = self.getAtom(atom_index);
const sym = atom.getSymbol(self);
const section = self.sections.get(@intFromEnum(sym.section_number) - 1);
const file_offset = section.header.pointer_to_raw_data + sym.value - section.header.virtual_address;
log.debug("writing atom for symbol {s} at file offset 0x{x} to 0x{x}", .{
atom.getName(self),
file_offset,
file_offset + code.len,
});
const gpa = self.base.comp.gpa;
// Gather relocs which can be resolved.
// We need to do this as we will be applying different slide values depending
// if we are running in hot-code swapping mode or not.
// TODO: how crazy would it be to try and apply the actual image base of the loaded
// process for the in-file values rather than the Windows defaults?
var relocs = std.ArrayList(*Relocation).init(gpa);
defer relocs.deinit();
if (self.relocs.getPtr(atom_index)) |rels| {
try relocs.ensureTotalCapacityPrecise(rels.items.len);
for (rels.items) |*reloc| {
if (reloc.isResolvable(self) and reloc.dirty) {
relocs.appendAssumeCapacity(reloc);
}
}
}
if (is_hot_update_compatible) {
if (self.base.child_pid) |handle| {
const slide = @intFromPtr(self.hot_state.loaded_base_address.?);
const mem_code = try gpa.dupe(u8, code);
defer gpa.free(mem_code);
self.resolveRelocs(atom_index, relocs.items, mem_code, slide);
const vaddr = sym.value + slide;
const pvaddr = @as(*anyopaque, @ptrFromInt(vaddr));
log.debug("writing to memory at address {x}", .{vaddr});
if (build_options.enable_logging) {
try debugMem(gpa, handle, pvaddr, mem_code);
}
if (section.header.flags.MEM_WRITE == 0) {
writeMemProtected(handle, pvaddr, mem_code) catch |err| {
log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)});
};
} else {
writeMem(handle, pvaddr, mem_code) catch |err| {
log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)});
};
}
}
}
self.resolveRelocs(atom_index, relocs.items, code, self.image_base);
try self.base.file.?.pwriteAll(code, file_offset);
// Now we can mark the relocs as resolved.
while (relocs.popOrNull()) |reloc| {
reloc.dirty = false;
}
}
fn debugMem(allocator: Allocator, handle: std.ChildProcess.Id, pvaddr: std.os.windows.LPVOID, code: []const u8) !void {
const buffer = try allocator.alloc(u8, code.len);
defer allocator.free(buffer);
const memread = try std.os.windows.ReadProcessMemory(handle, pvaddr, buffer);
log.debug("to write: {x}", .{std.fmt.fmtSliceHexLower(code)});
log.debug("in memory: {x}", .{std.fmt.fmtSliceHexLower(memread)});
}
fn writeMemProtected(handle: std.ChildProcess.Id, pvaddr: std.os.windows.LPVOID, code: []const u8) !void {
const old_prot = try std.os.windows.VirtualProtectEx(handle, pvaddr, code.len, std.os.windows.PAGE_EXECUTE_WRITECOPY);
try writeMem(handle, pvaddr, code);
// TODO: We can probably just set the pages writeable and leave it at that without having to restore the attributes.
// For that though, we want to track which page has already been modified.
_ = try std.os.windows.VirtualProtectEx(handle, pvaddr, code.len, old_prot);
}
fn writeMem(handle: std.ChildProcess.Id, pvaddr: std.os.windows.LPVOID, code: []const u8) !void {
const amt = try std.os.windows.WriteProcessMemory(handle, pvaddr, code);
if (amt != code.len) return error.InputOutput;
}
fn writeOffsetTableEntry(self: *Coff, index: usize) !void {
const sect_id = self.got_section_index.?;
if (self.got_table_count_dirty) {
const needed_size = @as(u32, @intCast(self.got_table.entries.items.len * self.ptr_width.size()));
try self.growSection(sect_id, needed_size);
self.got_table_count_dirty = false;
}
const header = &self.sections.items(.header)[sect_id];
const entry = self.got_table.entries.items[index];
const entry_value = self.getSymbol(entry).value;
const entry_offset = index * self.ptr_width.size();
const file_offset = header.pointer_to_raw_data + entry_offset;
const vmaddr = header.virtual_address + entry_offset;
log.debug("writing GOT entry {d}: @{x} => {x}", .{ index, vmaddr, entry_value + self.image_base });
switch (self.ptr_width) {
.p32 => {
var buf: [4]u8 = undefined;
mem.writeInt(u32, &buf, @as(u32, @intCast(entry_value + self.image_base)), .little);
try self.base.file.?.pwriteAll(&buf, file_offset);
},
.p64 => {
var buf: [8]u8 = undefined;
mem.writeInt(u64, &buf, entry_value + self.image_base, .little);
try self.base.file.?.pwriteAll(&buf, file_offset);
},
}
if (is_hot_update_compatible) {
if (self.base.child_pid) |handle| {
const gpa = self.base.comp.gpa;
const slide = @intFromPtr(self.hot_state.loaded_base_address.?);
const actual_vmaddr = vmaddr + slide;
const pvaddr = @as(*anyopaque, @ptrFromInt(actual_vmaddr));
log.debug("writing GOT entry to memory at address {x}", .{actual_vmaddr});
if (build_options.enable_logging) {
switch (self.ptr_width) {
.p32 => {
var buf: [4]u8 = undefined;
try debugMem(gpa, handle, pvaddr, &buf);
},
.p64 => {
var buf: [8]u8 = undefined;
try debugMem(gpa, handle, pvaddr, &buf);
},
}
}
switch (self.ptr_width) {
.p32 => {
var buf: [4]u8 = undefined;
mem.writeInt(u32, &buf, @as(u32, @intCast(entry_value + slide)), .little);
writeMem(handle, pvaddr, &buf) catch |err| {
log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)});
};
},
.p64 => {
var buf: [8]u8 = undefined;
mem.writeInt(u64, &buf, entry_value + slide, .little);
writeMem(handle, pvaddr, &buf) catch |err| {
log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)});
};
},
}
}
}
}
fn markRelocsDirtyByTarget(self: *Coff, target: SymbolWithLoc) void {
// TODO: reverse-lookup might come in handy here
for (self.relocs.values()) |*relocs| {
for (relocs.items) |*reloc| {
if (!reloc.target.eql(target)) continue;
reloc.dirty = true;
}
}
}
fn markRelocsDirtyByAddress(self: *Coff, addr: u32) void {
const got_moved = blk: {
const sect_id = self.got_section_index orelse break :blk false;
break :blk self.sections.items(.header)[sect_id].virtual_address >= addr;
};
// TODO: dirty relocations targeting import table if that got moved in memory
for (self.relocs.values()) |*relocs| {
for (relocs.items) |*reloc| {
if (reloc.isGotIndirection()) {
reloc.dirty = reloc.dirty or got_moved;
} else {
const target_vaddr = reloc.getTargetAddress(self) orelse continue;
if (target_vaddr >= addr) reloc.dirty = true;
}
}
}
// TODO: dirty only really affected GOT cells
for (self.got_table.entries.items) |entry| {
const target_addr = self.getSymbol(entry).value;
if (target_addr >= addr) {
self.got_table_contents_dirty = true;
break;
}
}
}
fn resolveRelocs(self: *Coff, atom_index: Atom.Index, relocs: []*const Relocation, code: []u8, image_base: u64) void {
log.debug("relocating '{s}'", .{self.getAtom(atom_index).getName(self)});
for (relocs) |reloc| {
reloc.resolve(atom_index, code, image_base, self);
}
}
pub fn ptraceAttach(self: *Coff, handle: std.ChildProcess.Id) !void {
if (!is_hot_update_compatible) return;
log.debug("attaching to process with handle {*}", .{handle});
self.hot_state.loaded_base_address = std.os.windows.ProcessBaseAddress(handle) catch |err| {
log.warn("failed to get base address for the process with error: {s}", .{@errorName(err)});
return;
};
}
pub fn ptraceDetach(self: *Coff, handle: std.ChildProcess.Id) void {
if (!is_hot_update_compatible) return;
log.debug("detaching from process with handle {*}", .{handle});
self.hot_state.loaded_base_address = null;
}
fn freeAtom(self: *Coff, atom_index: Atom.Index) void {
log.debug("freeAtom {d}", .{atom_index});
const gpa = self.base.comp.gpa;
// Remove any relocs and base relocs associated with this Atom
Atom.freeRelocations(self, atom_index);
const atom = self.getAtom(atom_index);
const sym = atom.getSymbol(self);
const sect_id = @intFromEnum(sym.section_number) - 1;
const free_list = &self.sections.items(.free_list)[sect_id];
var already_have_free_list_node = false;
{
var i: usize = 0;
// TODO turn free_list into a hash map
while (i < free_list.items.len) {
if (free_list.items[i] == atom_index) {
_ = free_list.swapRemove(i);
continue;
}
if (free_list.items[i] == atom.prev_index) {
already_have_free_list_node = true;
}
i += 1;
}
}
const maybe_last_atom_index = &self.sections.items(.last_atom_index)[sect_id];
if (maybe_last_atom_index.*) |last_atom_index| {
if (last_atom_index == atom_index) {
if (atom.prev_index) |prev_index| {
// TODO shrink the section size here
maybe_last_atom_index.* = prev_index;
} else {
maybe_last_atom_index.* = null;
}
}
}
if (atom.prev_index) |prev_index| {
const prev = self.getAtomPtr(prev_index);
prev.next_index = atom.next_index;
if (!already_have_free_list_node and prev.*.freeListEligible(self)) {
// The free list is heuristics, it doesn't have to be perfect, so we can
// ignore the OOM here.
free_list.append(gpa, prev_index) catch {};
}
} else {
self.getAtomPtr(atom_index).prev_index = null;
}
if (atom.next_index) |next_index| {
self.getAtomPtr(next_index).prev_index = atom.prev_index;
} else {
self.getAtomPtr(atom_index).next_index = null;
}
// Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
const sym_index = atom.getSymbolIndex().?;
self.locals_free_list.append(gpa, sym_index) catch {};
// Try freeing GOT atom if this decl had one
self.got_table.freeEntry(gpa, .{ .sym_index = sym_index });
self.locals.items[sym_index].section_number = .UNDEFINED;
_ = self.atom_by_index_table.remove(sym_index);
log.debug(" adding local symbol index {d} to free list", .{sym_index});
self.getAtomPtr(atom_index).sym_index = 0;
}
pub fn updateFunc(self: *Coff, mod: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void {
if (build_options.skip_non_native and builtin.object_format != .coff) {
@panic("Attempted to compile for object format that was disabled by build configuration");
}
if (self.llvm_object) |llvm_object| {
return llvm_object.updateFunc(mod, func_index, air, liveness);
}
const tracy = trace(@src());
defer tracy.end();
const func = mod.funcInfo(func_index);
const decl_index = func.owner_decl;
const decl = mod.declPtr(decl_index);
const atom_index = try self.getOrCreateAtomForDecl(decl_index);
self.freeUnnamedConsts(decl_index);
Atom.freeRelocations(self, atom_index);
const gpa = self.base.comp.gpa;
var code_buffer = std.ArrayList(u8).init(gpa);
defer code_buffer.deinit();
const res = try codegen.generateFunction(
&self.base,
decl.srcLoc(mod),
func_index,
air,
liveness,
&code_buffer,
.none,
);
const code = switch (res) {
.ok => code_buffer.items,
.fail => |em| {
func.analysis(&mod.intern_pool).state = .codegen_failure;
try mod.failed_decls.put(mod.gpa, decl_index, em);
return;
},
};
try self.updateDeclCode(decl_index, code, .FUNCTION);
// Since we updated the vaddr and the size, each corresponding export
// symbol also needs to be updated.
return self.updateExports(mod, .{ .decl_index = decl_index }, mod.getDeclExports(decl_index));
}
pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: InternPool.DeclIndex) !u32 {
const gpa = self.base.comp.gpa;
const mod = self.base.comp.module.?;
const decl = mod.declPtr(decl_index);
const gop = try self.unnamed_const_atoms.getOrPut(gpa, decl_index);
if (!gop.found_existing) {
gop.value_ptr.* = .{};
}
const unnamed_consts = gop.value_ptr;
const decl_name = mod.intern_pool.stringToSlice(try decl.fullyQualifiedName(mod));
const index = unnamed_consts.items.len;
const sym_name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
defer gpa.free(sym_name);
const atom_index = switch (try self.lowerConst(sym_name, tv, tv.ty.abiAlignment(mod), self.rdata_section_index.?, decl.srcLoc(mod))) {
.ok => |atom_index| atom_index,
.fail => |em| {
decl.analysis = .codegen_failure;
try mod.failed_decls.put(mod.gpa, decl_index, em);
log.err("{s}", .{em.msg});
return error.CodegenFail;
},
};
try unnamed_consts.append(gpa, atom_index);
return self.getAtom(atom_index).getSymbolIndex().?;
}
const LowerConstResult = union(enum) {
ok: Atom.Index,
fail: *Module.ErrorMsg,
};
fn lowerConst(self: *Coff, name: []const u8, tv: TypedValue, required_alignment: InternPool.Alignment, sect_id: u16, src_loc: Module.SrcLoc) !LowerConstResult {
const gpa = self.base.comp.gpa;
var code_buffer = std.ArrayList(u8).init(gpa);
defer code_buffer.deinit();
const atom_index = try self.createAtom();
const sym = self.getAtom(atom_index).getSymbolPtr(self);
try self.setSymbolName(sym, name);
sym.section_number = @as(coff.SectionNumber, @enumFromInt(sect_id + 1));
const res = try codegen.generateSymbol(&self.base, src_loc, tv, &code_buffer, .none, .{
.parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
});
const code = switch (res) {
.ok => code_buffer.items,
.fail => |em| return .{ .fail = em },
};
const atom = self.getAtomPtr(atom_index);
atom.size = @as(u32, @intCast(code.len));
atom.getSymbolPtr(self).value = try self.allocateAtom(
atom_index,
atom.size,
@intCast(required_alignment.toByteUnitsOptional().?),
);
errdefer self.freeAtom(atom_index);
log.debug("allocated atom for {s} at 0x{x}", .{ name, atom.getSymbol(self).value });
log.debug(" (required alignment 0x{x})", .{required_alignment});
try self.writeAtom(atom_index, code);
return .{ .ok = atom_index };
}
pub fn updateDecl(
self: *Coff,
mod: *Module,
decl_index: InternPool.DeclIndex,
) link.File.UpdateDeclError!void {
if (build_options.skip_non_native and builtin.object_format != .coff) {
@panic("Attempted to compile for object format that was disabled by build configuration");
}
if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(mod, decl_index);
const tracy = trace(@src());
defer tracy.end();
const decl = mod.declPtr(decl_index);
if (decl.val.getExternFunc(mod)) |_| {
return;
}
const gpa = self.base.comp.gpa;
if (decl.isExtern(mod)) {
// TODO make this part of getGlobalSymbol
const variable = decl.getOwnedVariable(mod).?;
const name = mod.intern_pool.stringToSlice(decl.name);
const lib_name = mod.intern_pool.stringToSliceUnwrap(variable.lib_name);
const global_index = try self.getGlobalSymbol(name, lib_name);
try self.need_got_table.put(gpa, global_index, {});
return;
}
const atom_index = try self.getOrCreateAtomForDecl(decl_index);
Atom.freeRelocations(self, atom_index);
const atom = self.getAtom(atom_index);
var code_buffer = std.ArrayList(u8).init(gpa);
defer code_buffer.deinit();
const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
.ty = decl.ty,
.val = decl_val,
}, &code_buffer, .none, .{
.parent_atom_index = atom.getSymbolIndex().?,
});
const code = switch (res) {
.ok => code_buffer.items,
.fail => |em| {
decl.analysis = .codegen_failure;
try mod.failed_decls.put(mod.gpa, decl_index, em);
return;
},
};
try self.updateDeclCode(decl_index, code, .NULL);
// Since we updated the vaddr and the size, each corresponding export
// symbol also needs to be updated.
return self.updateExports(mod, .{ .decl_index = decl_index }, mod.getDeclExports(decl_index));
}
fn updateLazySymbolAtom(
self: *Coff,
sym: link.File.LazySymbol,
atom_index: Atom.Index,
section_index: u16,
) !void {
const gpa = self.base.comp.gpa;
const mod = self.base.comp.module.?;
var required_alignment: InternPool.Alignment = .none;
var code_buffer = std.ArrayList(u8).init(gpa);
defer code_buffer.deinit();
const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{
@tagName(sym.kind),
sym.ty.fmt(mod),
});
defer gpa.free(name);
const atom = self.getAtomPtr(atom_index);
const local_sym_index = atom.getSymbolIndex().?;
const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|
mod.declPtr(owner_decl).srcLoc(mod)
else
Module.SrcLoc{
.file_scope = undefined,
.parent_decl_node = undefined,
.lazy = .unneeded,
};
const res = try codegen.generateLazySymbol(
&self.base,
src,
sym,
&required_alignment,
&code_buffer,
.none,
.{ .parent_atom_index = local_sym_index },
);
const code = switch (res) {
.ok => code_buffer.items,
.fail => |em| {
log.err("{s}", .{em.msg});
return error.CodegenFail;
},
};
const code_len = @as(u32, @intCast(code.len));
const symbol = atom.getSymbolPtr(self);
try self.setSymbolName(symbol, name);
symbol.section_number = @as(coff.SectionNumber, @enumFromInt(section_index + 1));
symbol.type = .{ .complex_type = .NULL, .base_type = .NULL };
const vaddr = try self.allocateAtom(atom_index, code_len, @intCast(required_alignment.toByteUnits(0)));
errdefer self.freeAtom(atom_index);
log.debug("allocated atom for {s} at 0x{x}", .{ name, vaddr });
log.debug(" (required alignment 0x{x})", .{required_alignment});
atom.size = code_len;
symbol.value = vaddr;
try self.addGotEntry(.{ .sym_index = local_sym_index });
try self.writeAtom(atom_index, code);
}
pub fn getOrCreateAtomForLazySymbol(self: *Coff, sym: link.File.LazySymbol) !Atom.Index {
const gpa = self.base.comp.gpa;
const mod = self.base.comp.module.?;
const gop = try self.lazy_syms.getOrPut(gpa, sym.getDecl(mod));
errdefer _ = if (!gop.found_existing) self.lazy_syms.pop();
if (!gop.found_existing) gop.value_ptr.* = .{};
const metadata: struct { atom: *Atom.Index, state: *LazySymbolMetadata.State } = switch (sym.kind) {
.code => .{ .atom = &gop.value_ptr.text_atom, .state = &gop.value_ptr.text_state },
.const_data => .{ .atom = &gop.value_ptr.rdata_atom, .state = &gop.value_ptr.rdata_state },
};
switch (metadata.state.*) {
.unused => metadata.atom.* = try self.createAtom(),
.pending_flush => return metadata.atom.*,
.flushed => {},
}
metadata.state.* = .pending_flush;
const atom = metadata.atom.*;
// anyerror needs to be deferred until flushModule
if (sym.getDecl(mod) != .none) try self.updateLazySymbolAtom(sym, atom, switch (sym.kind) {
.code => self.text_section_index.?,
.const_data => self.rdata_section_index.?,
});
return atom;
}
pub fn getOrCreateAtomForDecl(self: *Coff, decl_index: InternPool.DeclIndex) !Atom.Index {
const gpa = self.base.comp.gpa;
const gop = try self.decls.getOrPut(gpa, decl_index);
if (!gop.found_existing) {
gop.value_ptr.* = .{
.atom = try self.createAtom(),
.section = self.getDeclOutputSection(decl_index),
.exports = .{},
};
}
return gop.value_ptr.atom;
}
fn getDeclOutputSection(self: *Coff, decl_index: InternPool.DeclIndex) u16 {
const decl = self.base.comp.module.?.declPtr(decl_index);
const ty = decl.ty;
const mod = self.base.comp.module.?;
const zig_ty = ty.zigTypeTag(mod);
const val = decl.val;
const index: u16 = blk: {
if (val.isUndefDeep(mod)) {
// TODO in release-fast and release-small, we should put undef in .bss
break :blk self.data_section_index.?;
}
switch (zig_ty) {
// TODO: what if this is a function pointer?
.Fn => break :blk self.text_section_index.?,
else => {
if (val.getVariable(mod)) |_| {
break :blk self.data_section_index.?;
}
break :blk self.rdata_section_index.?;
},
}
};
return index;
}
fn updateDeclCode(self: *Coff, decl_index: InternPool.DeclIndex, code: []u8, complex_type: coff.ComplexType) !void {
const mod = self.base.comp.module.?;
const decl = mod.declPtr(decl_index);
const decl_name = mod.intern_pool.stringToSlice(try decl.fullyQualifiedName(mod));
log.debug("updateDeclCode {s}{*}", .{ decl_name, decl });
const required_alignment: u32 = @intCast(decl.getAlignment(mod).toByteUnits(0));
const decl_metadata = self.decls.get(decl_index).?;
const atom_index = decl_metadata.atom;
const atom = self.getAtom(atom_index);
const sym_index = atom.getSymbolIndex().?;
const sect_index = decl_metadata.section;
const code_len = @as(u32, @intCast(code.len));
if (atom.size != 0) {
const sym = atom.getSymbolPtr(self);
try self.setSymbolName(sym, decl_name);
sym.section_number = @as(coff.SectionNumber, @enumFromInt(sect_index + 1));
sym.type = .{ .complex_type = complex_type, .base_type = .NULL };
const capacity = atom.capacity(self);
const need_realloc = code.len > capacity or !mem.isAlignedGeneric(u64, sym.value, required_alignment);
if (need_realloc) {
const vaddr = try self.growAtom(atom_index, code_len, required_alignment);
log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, sym.value, vaddr });
log.debug(" (required alignment 0x{x}", .{required_alignment});
if (vaddr != sym.value) {
sym.value = vaddr;
log.debug(" (updating GOT entry)", .{});
const got_entry_index = self.got_table.lookup.get(.{ .sym_index = sym_index }).?;
try self.writeOffsetTableEntry(got_entry_index);
self.markRelocsDirtyByTarget(.{ .sym_index = sym_index });
}
} else if (code_len < atom.size) {
self.shrinkAtom(atom_index, code_len);
}
self.getAtomPtr(atom_index).size = code_len;
} else {
const sym = atom.getSymbolPtr(self);
try self.setSymbolName(sym, decl_name);
sym.section_number = @as(coff.SectionNumber, @enumFromInt(sect_index + 1));
sym.type = .{ .complex_type = complex_type, .base_type = .NULL };
const vaddr = try self.allocateAtom(atom_index, code_len, required_alignment);
errdefer self.freeAtom(atom_index);
log.debug("allocated atom for {s} at 0x{x}", .{ decl_name, vaddr });
self.getAtomPtr(atom_index).size = code_len;
sym.value = vaddr;
try self.addGotEntry(.{ .sym_index = sym_index });
}
try self.writeAtom(atom_index, code);
}
fn freeUnnamedConsts(self: *Coff, decl_index: InternPool.DeclIndex) void {
const gpa = self.base.comp.gpa;
const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
for (unnamed_consts.items) |atom_index| {
self.freeAtom(atom_index);
}
unnamed_consts.clearAndFree(gpa);
}
pub fn freeDecl(self: *Coff, decl_index: InternPool.DeclIndex) void {
if (self.llvm_object) |llvm_object| return llvm_object.freeDecl(decl_index);
const gpa = self.base.comp.gpa;
const mod = self.base.comp.module.?;
const decl = mod.declPtr(decl_index);
log.debug("freeDecl {*}", .{decl});
if (self.decls.fetchOrderedRemove(decl_index)) |const_kv| {
var kv = const_kv;
self.freeAtom(kv.value.atom);
self.freeUnnamedConsts(decl_index);
kv.value.exports.deinit(gpa);
}
}
pub fn updateExports(
self: *Coff,
mod: *Module,
exported: Module.Exported,
exports: []const *Module.Export,
) link.File.UpdateExportsError!void {
if (build_options.skip_non_native and builtin.object_format != .coff) {
@panic("Attempted to compile for object format that was disabled by build configuration");
}
const ip = &mod.intern_pool;
const comp = self.base.comp;
const target = comp.root_mod.resolved_target.result;
if (comp.config.use_llvm) {
// Even in the case of LLVM, we need to notice certain exported symbols in order to
// detect the default subsystem.
for (exports) |exp| {
const exported_decl_index = switch (exp.exported) {
.decl_index => |i| i,
.value => continue,
};
const exported_decl = mod.declPtr(exported_decl_index);
if (exported_decl.getOwnedFunction(mod) == null) continue;
const winapi_cc = switch (target.cpu.arch) {
.x86 => std.builtin.CallingConvention.Stdcall,
else => std.builtin.CallingConvention.C,
};
const decl_cc = exported_decl.ty.fnCallingConvention(mod);
if (decl_cc == .C and ip.stringEqlSlice(exp.opts.name, "main") and
comp.config.link_libc)
{
mod.stage1_flags.have_c_main = true;
} else if (decl_cc == winapi_cc and target.os.tag == .windows) {
if (ip.stringEqlSlice(exp.opts.name, "WinMain")) {
mod.stage1_flags.have_winmain = true;
} else if (ip.stringEqlSlice(exp.opts.name, "wWinMain")) {
mod.stage1_flags.have_wwinmain = true;
} else if (ip.stringEqlSlice(exp.opts.name, "WinMainCRTStartup")) {
mod.stage1_flags.have_winmain_crt_startup = true;
} else if (ip.stringEqlSlice(exp.opts.name, "wWinMainCRTStartup")) {
mod.stage1_flags.have_wwinmain_crt_startup = true;
} else if (ip.stringEqlSlice(exp.opts.name, "DllMainCRTStartup")) {
mod.stage1_flags.have_dllmain_crt_startup = true;
}
}
}
}
if (self.llvm_object) |llvm_object| return llvm_object.updateExports(mod, exported, exports);
const gpa = comp.gpa;
const metadata = switch (exported) {
.decl_index => |decl_index| blk: {
_ = try self.getOrCreateAtomForDecl(decl_index);
break :blk self.decls.getPtr(decl_index).?;
},
.value => |value| self.anon_decls.getPtr(value) orelse blk: {
const first_exp = exports[0];
const res = try self.lowerAnonDecl(value, .none, first_exp.getSrcLoc(mod));
switch (res) {
.ok => {},
.fail => |em| {
// TODO maybe it's enough to return an error here and let Module.processExportsInner
// handle the error?
try mod.failed_exports.ensureUnusedCapacity(mod.gpa, 1);
mod.failed_exports.putAssumeCapacityNoClobber(first_exp, em);
return;
},
}
break :blk self.anon_decls.getPtr(value).?;
},
};
const atom_index = metadata.atom;
const atom = self.getAtom(atom_index);
for (exports) |exp| {
log.debug("adding new export '{}'", .{exp.opts.name.fmt(&mod.intern_pool)});
if (mod.intern_pool.stringToSliceUnwrap(exp.opts.section)) |section_name| {
if (!mem.eql(u8, section_name, ".text")) {
try mod.failed_exports.putNoClobber(gpa, exp, try Module.ErrorMsg.create(
gpa,
exp.getSrcLoc(mod),
"Unimplemented: ExportOptions.section",
.{},
));
continue;
}
}
if (exp.opts.linkage == .link_once) {
try mod.failed_exports.putNoClobber(gpa, exp, try Module.ErrorMsg.create(
gpa,
exp.getSrcLoc(mod),
"Unimplemented: GlobalLinkage.link_once",
.{},
));
continue;
}
const exp_name = mod.intern_pool.stringToSlice(exp.opts.name);
const sym_index = metadata.getExport(self, exp_name) orelse blk: {
const sym_index = if (self.getGlobalIndex(exp_name)) |global_index| ind: {
const global = self.globals.items[global_index];
// TODO this is just plain wrong as it all should happen in a single `resolveSymbols`
// pass. This will go away once we abstact away Zig's incremental compilation into
// its own module.
if (global.file == null and self.getSymbol(global).section_number == .UNDEFINED) {
_ = self.unresolved.swapRemove(global_index);
break :ind global.sym_index;
}
break :ind try self.allocateSymbol();
} else try self.allocateSymbol();
try metadata.exports.append(gpa, sym_index);
break :blk sym_index;
};
const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
const sym = self.getSymbolPtr(sym_loc);
try self.setSymbolName(sym, exp_name);
sym.value = atom.getSymbol(self).value;
sym.section_number = @as(coff.SectionNumber, @enumFromInt(metadata.section + 1));
sym.type = atom.getSymbol(self).type;
switch (exp.opts.linkage) {
.strong => {
sym.storage_class = .EXTERNAL;
},
.internal => @panic("TODO Internal"),
.weak => @panic("TODO WeakExternal"),
else => unreachable,
}
try self.resolveGlobalSymbol(sym_loc);
}
}
pub fn deleteDeclExport(
self: *Coff,
decl_index: InternPool.DeclIndex,
name_ip: InternPool.NullTerminatedString,
) void {
if (self.llvm_object) |_| return;
const metadata = self.decls.getPtr(decl_index) orelse return;
const mod = self.base.comp.module.?;
const name = mod.intern_pool.stringToSlice(name_ip);
const sym_index = metadata.getExportPtr(self, name) orelse return;
const gpa = self.base.comp.gpa;
const sym_loc = SymbolWithLoc{ .sym_index = sym_index.*, .file = null };
const sym = self.getSymbolPtr(sym_loc);
log.debug("deleting export '{s}'", .{name});
assert(sym.storage_class == .EXTERNAL and sym.section_number != .UNDEFINED);
sym.* = .{
.name = [_]u8{0} ** 8,
.value = 0,
.section_number = .UNDEFINED,
.type = .{ .base_type = .NULL, .complex_type = .NULL },
.storage_class = .NULL,
.number_of_aux_symbols = 0,
};
self.locals_free_list.append(gpa, sym_index.*) catch {};
if (self.resolver.fetchRemove(name)) |entry| {
defer gpa.free(entry.key);
self.globals_free_list.append(gpa, entry.value) catch {};
self.globals.items[entry.value] = .{
.sym_index = 0,
.file = null,
};
}
sym_index.* = 0;
}
fn resolveGlobalSymbol(self: *Coff, current: SymbolWithLoc) !void {
const gpa = self.base.comp.gpa;
const sym = self.getSymbol(current);
const sym_name = self.getSymbolName(current);
const gop = try self.getOrPutGlobalPtr(sym_name);
if (!gop.found_existing) {
gop.value_ptr.* = current;
if (sym.section_number == .UNDEFINED) {
try self.unresolved.putNoClobber(gpa, self.getGlobalIndex(sym_name).?, false);
}
return;
}
log.debug("TODO finish resolveGlobalSymbols implementation", .{});
if (sym.section_number == .UNDEFINED) return;
_ = self.unresolved.swapRemove(self.getGlobalIndex(sym_name).?);
gop.value_ptr.* = current;
}
pub fn flush(self: *Coff, arena: Allocator, prog_node: *std.Progress.Node) link.File.FlushError!void {
const comp = self.base.comp;
const use_lld = build_options.have_llvm and comp.config.use_lld;
if (use_lld) {
return lld.linkWithLLD(self, arena, prog_node);
}
switch (comp.config.output_mode) {
.Exe, .Obj => return self.flushModule(arena, prog_node),
.Lib => return error.TODOImplementWritingLibFiles,
}
}
pub fn flushModule(self: *Coff, arena: Allocator, prog_node: *std.Progress.Node) link.File.FlushError!void {
const tracy = trace(@src());
defer tracy.end();
const comp = self.base.comp;
const gpa = comp.gpa;
if (self.llvm_object) |llvm_object| {
try self.base.emitLlvmObject(arena, llvm_object, prog_node);
return;
}
var sub_prog_node = prog_node.start("COFF Flush", 0);
sub_prog_node.activate();
defer sub_prog_node.end();
const module = comp.module orelse return error.LinkingWithoutZigSourceUnimplemented;
if (self.lazy_syms.getPtr(.none)) |metadata| {
// Most lazy symbols can be updated on first use, but
// anyerror needs to wait for everything to be flushed.
if (metadata.text_state != .unused) self.updateLazySymbolAtom(
link.File.LazySymbol.initDecl(.code, null, module),
metadata.text_atom,
self.text_section_index.?,
) catch |err| return switch (err) {
error.CodegenFail => error.FlushFailure,
else => |e| e,
};
if (metadata.rdata_state != .unused) self.updateLazySymbolAtom(
link.File.LazySymbol.initDecl(.const_data, null, module),
metadata.rdata_atom,
self.rdata_section_index.?,
) catch |err| return switch (err) {
error.CodegenFail => error.FlushFailure,
else => |e| e,
};
}
for (self.lazy_syms.values()) |*metadata| {
if (metadata.text_state != .unused) metadata.text_state = .flushed;
if (metadata.rdata_state != .unused) metadata.rdata_state = .flushed;
}
{
var it = self.need_got_table.iterator();
while (it.next()) |entry| {
const global = self.globals.items[entry.key_ptr.*];
try self.addGotEntry(global);
}
}
while (self.unresolved.popOrNull()) |entry| {
assert(entry.value);
const global = self.globals.items[entry.key];
const sym = self.getSymbol(global);
const res = try self.import_tables.getOrPut(gpa, sym.value);
const itable = res.value_ptr;
if (!res.found_existing) {
itable.* = .{};
}
if (itable.lookup.contains(global)) continue;
// TODO: we could technically write the pointer placeholder for to-be-bound import here,
// but since this happens in flush, there is currently no point.
_ = try itable.addImport(gpa, global);
self.imports_count_dirty = true;
}
try self.writeImportTables();
for (self.relocs.keys(), self.relocs.values()) |atom_index, relocs| {
const needs_update = for (relocs.items) |reloc| {
if (reloc.dirty) break true;
} else false;
if (!needs_update) continue;
const atom = self.getAtom(atom_index);
const sym = atom.getSymbol(self);
const section = self.sections.get(@intFromEnum(sym.section_number) - 1).header;
const file_offset = section.pointer_to_raw_data + sym.value - section.virtual_address;
var code = std.ArrayList(u8).init(gpa);
defer code.deinit();
try code.resize(math.cast(usize, atom.size) orelse return error.Overflow);
assert(atom.size > 0);
const amt = try self.base.file.?.preadAll(code.items, file_offset);
if (amt != code.items.len) return error.InputOutput;
try self.writeAtom(atom_index, code.items);
}
// Update GOT if it got moved in memory.
if (self.got_table_contents_dirty) {
for (self.got_table.entries.items, 0..) |entry, i| {
if (!self.got_table.lookup.contains(entry)) continue;
// TODO: write all in one go rather than incrementally.
try self.writeOffsetTableEntry(i);
}
self.got_table_contents_dirty = false;
}
try self.writeBaseRelocations();
if (self.getEntryPoint()) |entry_sym_loc| {
self.entry_addr = self.getSymbol(entry_sym_loc).value;
}
if (build_options.enable_logging) {
self.logSymtab();
self.logImportTables();
}
try self.writeStrtab();
try self.writeDataDirectoriesHeaders();
try self.writeSectionHeaders();
if (self.entry_addr == null and comp.config.output_mode == .Exe) {
log.debug("flushing. no_entry_point_found = true\n", .{});
comp.link_error_flags.no_entry_point_found = true;
} else {
log.debug("flushing. no_entry_point_found = false\n", .{});
comp.link_error_flags.no_entry_point_found = false;
try self.writeHeader();
}
assert(!self.imports_count_dirty);
}
pub fn getDeclVAddr(self: *Coff, decl_index: InternPool.DeclIndex, reloc_info: link.File.RelocInfo) !u64 {
assert(self.llvm_object == null);
const this_atom_index = try self.getOrCreateAtomForDecl(decl_index);
const sym_index = self.getAtom(this_atom_index).getSymbolIndex().?;
const atom_index = self.getAtomIndexForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;
const target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
try Atom.addRelocation(self, atom_index, .{
.type = .direct,
.target = target,
.offset = @as(u32, @intCast(reloc_info.offset)),
.addend = reloc_info.addend,
.pcrel = false,
.length = 3,
});
try Atom.addBaseRelocation(self, atom_index, @as(u32, @intCast(reloc_info.offset)));
return 0;
}
pub fn lowerAnonDecl(
self: *Coff,
decl_val: InternPool.Index,
explicit_alignment: InternPool.Alignment,
src_loc: Module.SrcLoc,
) !codegen.Result {
const gpa = self.base.comp.gpa;
const mod = self.base.comp.module.?;
const ty = Type.fromInterned(mod.intern_pool.typeOf(decl_val));
const decl_alignment = switch (explicit_alignment) {
.none => ty.abiAlignment(mod),
else => explicit_alignment,
};
if (self.anon_decls.get(decl_val)) |metadata| {
const existing_addr = self.getAtom(metadata.atom).getSymbol(self).value;
if (decl_alignment.check(existing_addr))
return .ok;
}
const val = Value.fromInterned(decl_val);
const tv = TypedValue{ .ty = ty, .val = val };
var name_buf: [32]u8 = undefined;
const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{
@intFromEnum(decl_val),
}) catch unreachable;
const res = self.lowerConst(
name,
tv,
decl_alignment,
self.rdata_section_index.?,
src_loc,
) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
else => |e| return .{ .fail = try Module.ErrorMsg.create(
gpa,
src_loc,
"lowerAnonDecl failed with error: {s}",
.{@errorName(e)},
) },
};
const atom_index = switch (res) {
.ok => |atom_index| atom_index,
.fail => |em| return .{ .fail = em },
};
try self.anon_decls.put(gpa, decl_val, .{ .atom = atom_index, .section = self.rdata_section_index.? });
return .ok;
}
pub fn getAnonDeclVAddr(self: *Coff, decl_val: InternPool.Index, reloc_info: link.File.RelocInfo) !u64 {
assert(self.llvm_object == null);
const this_atom_index = self.anon_decls.get(decl_val).?.atom;
const sym_index = self.getAtom(this_atom_index).getSymbolIndex().?;
const atom_index = self.getAtomIndexForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;
const target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
try Atom.addRelocation(self, atom_index, .{
.type = .direct,
.target = target,
.offset = @as(u32, @intCast(reloc_info.offset)),
.addend = reloc_info.addend,
.pcrel = false,
.length = 3,
});
try Atom.addBaseRelocation(self, atom_index, @as(u32, @intCast(reloc_info.offset)));
return 0;
}
pub fn getGlobalSymbol(self: *Coff, name: []const u8, lib_name_name: ?[]const u8) !u32 {
const gop = try self.getOrPutGlobalPtr(name);
const global_index = self.getGlobalIndex(name).?;
if (gop.found_existing) {
return global_index;
}
const sym_index = try self.allocateSymbol();
const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
gop.value_ptr.* = sym_loc;
const gpa = self.base.comp.gpa;
const sym = self.getSymbolPtr(sym_loc);
try self.setSymbolName(sym, name);
sym.storage_class = .EXTERNAL;
if (lib_name_name) |lib_name| {
// We repurpose the 'value' of the Symbol struct to store an offset into
// temporary string table where we will store the library name hint.
sym.value = try self.temp_strtab.insert(gpa, lib_name);
}
try self.unresolved.putNoClobber(gpa, global_index, true);
return global_index;
}
pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl_index: InternPool.DeclIndex) !void {
_ = self;
_ = module;
_ = decl_index;
log.debug("TODO implement updateDeclLineNumber", .{});
}
/// TODO: note if we need to rewrite base relocations by dirtying any of the entries in the global table
/// TODO: note that .ABSOLUTE is used as padding within each block; we could use this fact to do
/// incremental updates and writes into the table instead of doing it all at once
fn writeBaseRelocations(self: *Coff) !void {
const gpa = self.base.comp.gpa;
var page_table = std.AutoHashMap(u32, std.ArrayList(coff.BaseRelocation)).init(gpa);
defer {
var it = page_table.valueIterator();
while (it.next()) |inner| {
inner.deinit();
}
page_table.deinit();
}
{
var it = self.base_relocs.iterator();
while (it.next()) |entry| {
const atom_index = entry.key_ptr.*;
const atom = self.getAtom(atom_index);
const sym = atom.getSymbol(self);
const offsets = entry.value_ptr.*;
for (offsets.items) |offset| {
const rva = sym.value + offset;
const page = mem.alignBackward(u32, rva, self.page_size);
const gop = try page_table.getOrPut(page);
if (!gop.found_existing) {
gop.value_ptr.* = std.ArrayList(coff.BaseRelocation).init(gpa);
}
try gop.value_ptr.append(.{
.offset = @as(u12, @intCast(rva - page)),
.type = .DIR64,
});
}
}
{
const header = &self.sections.items(.header)[self.got_section_index.?];
for (self.got_table.entries.items, 0..) |entry, index| {
if (!self.got_table.lookup.contains(entry)) continue;
const sym = self.getSymbol(entry);
if (sym.section_number == .UNDEFINED) continue;
const rva = @as(u32, @intCast(header.virtual_address + index * self.ptr_width.size()));
const page = mem.alignBackward(u32, rva, self.page_size);
const gop = try page_table.getOrPut(page);
if (!gop.found_existing) {
gop.value_ptr.* = std.ArrayList(coff.BaseRelocation).init(gpa);
}
try gop.value_ptr.append(.{
.offset = @as(u12, @intCast(rva - page)),
.type = .DIR64,
});
}
}
}
// Sort pages by address.
var pages = try std.ArrayList(u32).initCapacity(gpa, page_table.count());
defer pages.deinit();
{
var it = page_table.keyIterator();
while (it.next()) |page| {
pages.appendAssumeCapacity(page.*);
}
}
mem.sort(u32, pages.items, {}, std.sort.asc(u32));
var buffer = std.ArrayList(u8).init(gpa);
defer buffer.deinit();
for (pages.items) |page| {
const entries = page_table.getPtr(page).?;
// Pad to required 4byte alignment
if (!mem.isAlignedGeneric(
usize,
entries.items.len * @sizeOf(coff.BaseRelocation),
@sizeOf(u32),
)) {
try entries.append(.{
.offset = 0,
.type = .ABSOLUTE,
});
}
const block_size = @as(
u32,
@intCast(entries.items.len * @sizeOf(coff.BaseRelocation) + @sizeOf(coff.BaseRelocationDirectoryEntry)),
);
try buffer.ensureUnusedCapacity(block_size);
buffer.appendSliceAssumeCapacity(mem.asBytes(&coff.BaseRelocationDirectoryEntry{
.page_rva = page,
.block_size = block_size,
}));
buffer.appendSliceAssumeCapacity(mem.sliceAsBytes(entries.items));
}
const header = &self.sections.items(.header)[self.reloc_section_index.?];
const needed_size = @as(u32, @intCast(buffer.items.len));
try self.growSection(self.reloc_section_index.?, needed_size);
try self.base.file.?.pwriteAll(buffer.items, header.pointer_to_raw_data);
self.data_directories[@intFromEnum(coff.DirectoryEntry.BASERELOC)] = .{
.virtual_address = header.virtual_address,
.size = needed_size,
};
}
fn writeImportTables(self: *Coff) !void {
if (self.idata_section_index == null) return;
if (!self.imports_count_dirty) return;
const gpa = self.base.comp.gpa;
const ext = ".dll";
const header = &self.sections.items(.header)[self.idata_section_index.?];
// Calculate needed size
var iat_size: u32 = 0;
var dir_table_size: u32 = @sizeOf(coff.ImportDirectoryEntry); // sentinel
var lookup_table_size: u32 = 0;
var names_table_size: u32 = 0;
var dll_names_size: u32 = 0;
for (self.import_tables.keys(), 0..) |off, i| {
const lib_name = self.temp_strtab.getAssumeExists(off);
const itable = self.import_tables.values()[i];
iat_size += itable.size() + 8;
dir_table_size += @sizeOf(coff.ImportDirectoryEntry);
lookup_table_size += @as(u32, @intCast(itable.entries.items.len + 1)) * @sizeOf(coff.ImportLookupEntry64.ByName);
for (itable.entries.items) |entry| {
const sym_name = self.getSymbolName(entry);
names_table_size += 2 + mem.alignForward(u32, @as(u32, @intCast(sym_name.len + 1)), 2);
}
dll_names_size += @as(u32, @intCast(lib_name.len + ext.len + 1));
}
const needed_size = iat_size + dir_table_size + lookup_table_size + names_table_size + dll_names_size;
try self.growSection(self.idata_section_index.?, needed_size);
// Do the actual writes
var buffer = std.ArrayList(u8).init(gpa);
defer buffer.deinit();
try buffer.ensureTotalCapacityPrecise(needed_size);
buffer.resize(needed_size) catch unreachable;
const dir_header_size = @sizeOf(coff.ImportDirectoryEntry);
const lookup_entry_size = @sizeOf(coff.ImportLookupEntry64.ByName);
var iat_offset: u32 = 0;
var dir_table_offset = iat_size;
var lookup_table_offset = dir_table_offset + dir_table_size;
var names_table_offset = lookup_table_offset + lookup_table_size;
var dll_names_offset = names_table_offset + names_table_size;
for (self.import_tables.keys(), 0..) |off, i| {
const lib_name = self.temp_strtab.getAssumeExists(off);
const itable = self.import_tables.values()[i];
// Lookup table header
const lookup_header = coff.ImportDirectoryEntry{
.import_lookup_table_rva = header.virtual_address + lookup_table_offset,
.time_date_stamp = 0,
.forwarder_chain = 0,
.name_rva = header.virtual_address + dll_names_offset,
.import_address_table_rva = header.virtual_address + iat_offset,
};
@memcpy(buffer.items[dir_table_offset..][0..@sizeOf(coff.ImportDirectoryEntry)], mem.asBytes(&lookup_header));
dir_table_offset += dir_header_size;
for (itable.entries.items) |entry| {
const import_name = self.getSymbolName(entry);
// IAT and lookup table entry
const lookup = coff.ImportLookupEntry64.ByName{ .name_table_rva = @as(u31, @intCast(header.virtual_address + names_table_offset)) };
@memcpy(
buffer.items[iat_offset..][0..@sizeOf(coff.ImportLookupEntry64.ByName)],
mem.asBytes(&lookup),
);
iat_offset += lookup_entry_size;
@memcpy(
buffer.items[lookup_table_offset..][0..@sizeOf(coff.ImportLookupEntry64.ByName)],
mem.asBytes(&lookup),
);
lookup_table_offset += lookup_entry_size;
// Names table entry
mem.writeInt(u16, buffer.items[names_table_offset..][0..2], 0, .little); // Hint set to 0 until we learn how to parse DLLs
names_table_offset += 2;
@memcpy(buffer.items[names_table_offset..][0..import_name.len], import_name);
names_table_offset += @as(u32, @intCast(import_name.len));
buffer.items[names_table_offset] = 0;
names_table_offset += 1;
if (!mem.isAlignedGeneric(usize, names_table_offset, @sizeOf(u16))) {
buffer.items[names_table_offset] = 0;
names_table_offset += 1;
}
}
// IAT sentinel
mem.writeInt(u64, buffer.items[iat_offset..][0..lookup_entry_size], 0, .little);
iat_offset += 8;
// Lookup table sentinel
@memcpy(
buffer.items[lookup_table_offset..][0..@sizeOf(coff.ImportLookupEntry64.ByName)],
mem.asBytes(&coff.ImportLookupEntry64.ByName{ .name_table_rva = 0 }),
);
lookup_table_offset += lookup_entry_size;
// DLL name
@memcpy(buffer.items[dll_names_offset..][0..lib_name.len], lib_name);
dll_names_offset += @as(u32, @intCast(lib_name.len));
@memcpy(buffer.items[dll_names_offset..][0..ext.len], ext);
dll_names_offset += @as(u32, @intCast(ext.len));
buffer.items[dll_names_offset] = 0;
dll_names_offset += 1;
}
// Sentinel
const lookup_header = coff.ImportDirectoryEntry{
.import_lookup_table_rva = 0,
.time_date_stamp = 0,
.forwarder_chain = 0,
.name_rva = 0,
.import_address_table_rva = 0,
};
@memcpy(
buffer.items[dir_table_offset..][0..@sizeOf(coff.ImportDirectoryEntry)],
mem.asBytes(&lookup_header),
);
dir_table_offset += dir_header_size;
assert(dll_names_offset == needed_size);
try self.base.file.?.pwriteAll(buffer.items, header.pointer_to_raw_data);
self.data_directories[@intFromEnum(coff.DirectoryEntry.IMPORT)] = .{
.virtual_address = header.virtual_address + iat_size,
.size = dir_table_size,
};
self.data_directories[@intFromEnum(coff.DirectoryEntry.IAT)] = .{
.virtual_address = header.virtual_address,
.size = iat_size,
};
self.imports_count_dirty = false;
}
fn writeStrtab(self: *Coff) !void {
if (self.strtab_offset == null) return;
const allocated_size = self.allocatedSize(self.strtab_offset.?);
const needed_size = @as(u32, @intCast(self.strtab.buffer.items.len));
if (needed_size > allocated_size) {
self.strtab_offset = null;
self.strtab_offset = @as(u32, @intCast(self.findFreeSpace(needed_size, @alignOf(u32))));
}
log.debug("writing strtab from 0x{x} to 0x{x}", .{ self.strtab_offset.?, self.strtab_offset.? + needed_size });
const gpa = self.base.comp.gpa;
var buffer = std.ArrayList(u8).init(gpa);
defer buffer.deinit();
try buffer.ensureTotalCapacityPrecise(needed_size);
buffer.appendSliceAssumeCapacity(self.strtab.buffer.items);
// Here, we do a trick in that we do not commit the size of the strtab to strtab buffer, instead
// we write the length of the strtab to a temporary buffer that goes to file.
mem.writeInt(u32, buffer.items[0..4], @as(u32, @intCast(self.strtab.buffer.items.len)), .little);
try self.base.file.?.pwriteAll(buffer.items, self.strtab_offset.?);
}
fn writeSectionHeaders(self: *Coff) !void {
const offset = self.getSectionHeadersOffset();
try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.sections.items(.header)), offset);
}
fn writeDataDirectoriesHeaders(self: *Coff) !void {
const offset = self.getDataDirectoryHeadersOffset();
try self.base.file.?.pwriteAll(mem.sliceAsBytes(&self.data_directories), offset);
}
fn writeHeader(self: *Coff) !void {
const target = self.base.comp.root_mod.resolved_target.result;
const gpa = self.base.comp.gpa;
var buffer = std.ArrayList(u8).init(gpa);
defer buffer.deinit();
const writer = buffer.writer();
try buffer.ensureTotalCapacity(self.getSizeOfHeaders());
writer.writeAll(msdos_stub) catch unreachable;
mem.writeInt(u32, buffer.items[0x3c..][0..4], msdos_stub.len, .little);
writer.writeAll("PE\x00\x00") catch unreachable;
var flags = coff.CoffHeaderFlags{
.EXECUTABLE_IMAGE = 1,
.DEBUG_STRIPPED = 1, // TODO
};
switch (self.ptr_width) {
.p32 => flags.@"32BIT_MACHINE" = 1,
.p64 => flags.LARGE_ADDRESS_AWARE = 1,
}
if (self.base.comp.config.output_mode == .Lib and self.base.comp.config.link_mode == .dynamic) {
flags.DLL = 1;
}
const timestamp = std.time.timestamp();
const size_of_optional_header = @as(u16, @intCast(self.getOptionalHeaderSize() + self.getDataDirectoryHeadersSize()));
var coff_header = coff.CoffHeader{
.machine = coff.MachineType.fromTargetCpuArch(target.cpu.arch),
.number_of_sections = @as(u16, @intCast(self.sections.slice().len)), // TODO what if we prune a section
.time_date_stamp = @as(u32, @truncate(@as(u64, @bitCast(timestamp)))),
.pointer_to_symbol_table = self.strtab_offset orelse 0,
.number_of_symbols = 0,
.size_of_optional_header = size_of_optional_header,
.flags = flags,
};
writer.writeAll(mem.asBytes(&coff_header)) catch unreachable;
const dll_flags: coff.DllFlags = .{
.HIGH_ENTROPY_VA = 1, // TODO do we want to permit non-PIE builds at all?
.DYNAMIC_BASE = 1,
.TERMINAL_SERVER_AWARE = 1, // We are not a legacy app
.NX_COMPAT = 1, // We are compatible with Data Execution Prevention
};
const subsystem: coff.Subsystem = .WINDOWS_CUI;
const size_of_image: u32 = self.getSizeOfImage();
const size_of_headers: u32 = mem.alignForward(u32, self.getSizeOfHeaders(), default_file_alignment);
const base_of_code = self.sections.get(self.text_section_index.?).header.virtual_address;
const base_of_data = self.sections.get(self.data_section_index.?).header.virtual_address;
var size_of_code: u32 = 0;
var size_of_initialized_data: u32 = 0;
var size_of_uninitialized_data: u32 = 0;
for (self.sections.items(.header)) |header| {
if (header.flags.CNT_CODE == 1) {
size_of_code += header.size_of_raw_data;
}
if (header.flags.CNT_INITIALIZED_DATA == 1) {
size_of_initialized_data += header.size_of_raw_data;
}
if (header.flags.CNT_UNINITIALIZED_DATA == 1) {
size_of_uninitialized_data += header.size_of_raw_data;
}
}
switch (self.ptr_width) {
.p32 => {
var opt_header = coff.OptionalHeaderPE32{
.magic = coff.IMAGE_NT_OPTIONAL_HDR32_MAGIC,
.major_linker_version = 0,
.minor_linker_version = 0,
.size_of_code = size_of_code,
.size_of_initialized_data = size_of_initialized_data,
.size_of_uninitialized_data = size_of_uninitialized_data,
.address_of_entry_point = self.entry_addr orelse 0,
.base_of_code = base_of_code,
.base_of_data = base_of_data,
.image_base = @intCast(self.image_base),
.section_alignment = self.page_size,
.file_alignment = default_file_alignment,
.major_operating_system_version = 6,
.minor_operating_system_version = 0,
.major_image_version = 0,
.minor_image_version = 0,
.major_subsystem_version = @intCast(self.major_subsystem_version),
.minor_subsystem_version = @intCast(self.minor_subsystem_version),
.win32_version_value = 0,
.size_of_image = size_of_image,
.size_of_headers = size_of_headers,
.checksum = 0,
.subsystem = subsystem,
.dll_flags = dll_flags,
.size_of_stack_reserve = default_size_of_stack_reserve,
.size_of_stack_commit = default_size_of_stack_commit,
.size_of_heap_reserve = default_size_of_heap_reserve,
.size_of_heap_commit = default_size_of_heap_commit,
.loader_flags = 0,
.number_of_rva_and_sizes = @intCast(self.data_directories.len),
};
writer.writeAll(mem.asBytes(&opt_header)) catch unreachable;
},
.p64 => {
var opt_header = coff.OptionalHeaderPE64{
.magic = coff.IMAGE_NT_OPTIONAL_HDR64_MAGIC,
.major_linker_version = 0,
.minor_linker_version = 0,
.size_of_code = size_of_code,
.size_of_initialized_data = size_of_initialized_data,
.size_of_uninitialized_data = size_of_uninitialized_data,
.address_of_entry_point = self.entry_addr orelse 0,
.base_of_code = base_of_code,
.image_base = self.image_base,
.section_alignment = self.page_size,
.file_alignment = default_file_alignment,
.major_operating_system_version = 6,
.minor_operating_system_version = 0,
.major_image_version = 0,
.minor_image_version = 0,
.major_subsystem_version = self.major_subsystem_version,
.minor_subsystem_version = self.minor_subsystem_version,
.win32_version_value = 0,
.size_of_image = size_of_image,
.size_of_headers = size_of_headers,
.checksum = 0,
.subsystem = subsystem,
.dll_flags = dll_flags,
.size_of_stack_reserve = default_size_of_stack_reserve,
.size_of_stack_commit = default_size_of_stack_commit,
.size_of_heap_reserve = default_size_of_heap_reserve,
.size_of_heap_commit = default_size_of_heap_commit,
.loader_flags = 0,
.number_of_rva_and_sizes = @intCast(self.data_directories.len),
};
writer.writeAll(mem.asBytes(&opt_header)) catch unreachable;
},
}
try self.base.file.?.pwriteAll(buffer.items, 0);
}
pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
return actual_size +| (actual_size / ideal_factor);
}
fn detectAllocCollision(self: *Coff, start: u32, size: u32) ?u32 {
const headers_size = @max(self.getSizeOfHeaders(), self.page_size);
if (start < headers_size)
return headers_size;
const end = start + padToIdeal(size);
if (self.strtab_offset) |off| {
const tight_size = @as(u32, @intCast(self.strtab.buffer.items.len));
const increased_size = padToIdeal(tight_size);
const test_end = off + increased_size;
if (end > off and start < test_end) {
return test_end;
}
}
for (self.sections.items(.header)) |header| {
const tight_size = header.size_of_raw_data;
const increased_size = padToIdeal(tight_size);
const test_end = header.pointer_to_raw_data + increased_size;
if (end > header.pointer_to_raw_data and start < test_end) {
return test_end;
}
}
return null;
}
fn allocatedSize(self: *Coff, start: u32) u32 {
if (start == 0)
return 0;
var min_pos: u32 = std.math.maxInt(u32);
if (self.strtab_offset) |off| {
if (off > start and off < min_pos) min_pos = off;
}
for (self.sections.items(.header)) |header| {
if (header.pointer_to_raw_data <= start) continue;
if (header.pointer_to_raw_data < min_pos) min_pos = header.pointer_to_raw_data;
}
return min_pos - start;
}
fn findFreeSpace(self: *Coff, object_size: u32, min_alignment: u32) u32 {
var start: u32 = 0;
while (self.detectAllocCollision(start, object_size)) |item_end| {
start = mem.alignForward(u32, item_end, min_alignment);
}
return start;
}
fn allocatedVirtualSize(self: *Coff, start: u32) u32 {
if (start == 0)
return 0;
var min_pos: u32 = std.math.maxInt(u32);
for (self.sections.items(.header)) |header| {
if (header.virtual_address <= start) continue;
if (header.virtual_address < min_pos) min_pos = header.virtual_address;
}
return min_pos - start;
}
inline fn getSizeOfHeaders(self: Coff) u32 {
const msdos_hdr_size = msdos_stub.len + 4;
return @as(u32, @intCast(msdos_hdr_size + @sizeOf(coff.CoffHeader) + self.getOptionalHeaderSize() +
self.getDataDirectoryHeadersSize() + self.getSectionHeadersSize()));
}
inline fn getOptionalHeaderSize(self: Coff) u32 {
return switch (self.ptr_width) {
.p32 => @as(u32, @intCast(@sizeOf(coff.OptionalHeaderPE32))),
.p64 => @as(u32, @intCast(@sizeOf(coff.OptionalHeaderPE64))),
};
}
inline fn getDataDirectoryHeadersSize(self: Coff) u32 {
return @as(u32, @intCast(self.data_directories.len * @sizeOf(coff.ImageDataDirectory)));
}
inline fn getSectionHeadersSize(self: Coff) u32 {
return @as(u32, @intCast(self.sections.slice().len * @sizeOf(coff.SectionHeader)));
}
inline fn getDataDirectoryHeadersOffset(self: Coff) u32 {
const msdos_hdr_size = msdos_stub.len + 4;
return @as(u32, @intCast(msdos_hdr_size + @sizeOf(coff.CoffHeader) + self.getOptionalHeaderSize()));
}
inline fn getSectionHeadersOffset(self: Coff) u32 {
return self.getDataDirectoryHeadersOffset() + self.getDataDirectoryHeadersSize();
}
inline fn getSizeOfImage(self: Coff) u32 {
var image_size: u32 = mem.alignForward(u32, self.getSizeOfHeaders(), self.page_size);
for (self.sections.items(.header)) |header| {
image_size += mem.alignForward(u32, header.virtual_size, self.page_size);
}
return image_size;
}
/// Returns symbol location corresponding to the set entrypoint (if any).
pub fn getEntryPoint(self: Coff) ?SymbolWithLoc {
const comp = self.base.comp;
// TODO This is incomplete.
// The entry symbol name depends on the subsystem as well as the set of
// public symbol names from linked objects.
// See LinkerDriver::findDefaultEntry from the LLD project for the flow chart.
const entry_name = switch (self.entry) {
.disabled => return null,
.default => switch (comp.config.output_mode) {
.Exe => "wWinMainCRTStartup",
.Obj, .Lib => return null,
},
.enabled => "wWinMainCRTStartup",
.named => |name| name,
};
const global_index = self.resolver.get(entry_name) orelse return null;
return self.globals.items[global_index];
}
/// Returns pointer-to-symbol described by `sym_loc` descriptor.
pub fn getSymbolPtr(self: *Coff, sym_loc: SymbolWithLoc) *coff.Symbol {
assert(sym_loc.file == null); // TODO linking object files
return &self.locals.items[sym_loc.sym_index];
}
/// Returns symbol described by `sym_loc` descriptor.
pub fn getSymbol(self: *const Coff, sym_loc: SymbolWithLoc) *const coff.Symbol {
assert(sym_loc.file == null); // TODO linking object files
return &self.locals.items[sym_loc.sym_index];
}
/// Returns name of the symbol described by `sym_loc` descriptor.
pub fn getSymbolName(self: *const Coff, sym_loc: SymbolWithLoc) []const u8 {
assert(sym_loc.file == null); // TODO linking object files
const sym = self.getSymbol(sym_loc);
const offset = sym.getNameOffset() orelse return sym.getName().?;
return self.strtab.get(offset).?;
}
/// Returns pointer to the global entry for `name` if one exists.
pub fn getGlobalPtr(self: *Coff, name: []const u8) ?*SymbolWithLoc {
const global_index = self.resolver.get(name) orelse return null;
return &self.globals.items[global_index];
}
/// Returns the global entry for `name` if one exists.
pub fn getGlobal(self: *const Coff, name: []const u8) ?SymbolWithLoc {
const global_index = self.resolver.get(name) orelse return null;
return self.globals.items[global_index];
}
/// Returns the index of the global entry for `name` if one exists.
pub fn getGlobalIndex(self: *const Coff, name: []const u8) ?u32 {
return self.resolver.get(name);
}
/// Returns global entry at `index`.
pub fn getGlobalByIndex(self: *const Coff, index: u32) SymbolWithLoc {
assert(index < self.globals.items.len);
return self.globals.items[index];
}
const GetOrPutGlobalPtrResult = struct {
found_existing: bool,
value_ptr: *SymbolWithLoc,
};
/// Used only for disambiguating local from global at relocation level.
/// TODO this must go away.
pub const global_symbol_bit: u32 = 0x80000000;
pub const global_symbol_mask: u32 = 0x7fffffff;
/// Return pointer to the global entry for `name` if one exists.
/// Puts a new global entry for `name` if one doesn't exist, and
/// returns a pointer to it.
pub fn getOrPutGlobalPtr(self: *Coff, name: []const u8) !GetOrPutGlobalPtrResult {
if (self.getGlobalPtr(name)) |ptr| {
return GetOrPutGlobalPtrResult{ .found_existing = true, .value_ptr = ptr };
}
const gpa = self.base.comp.gpa;
const global_index = try self.allocateGlobal();
const global_name = try gpa.dupe(u8, name);
_ = try self.resolver.put(gpa, global_name, global_index);
const ptr = &self.globals.items[global_index];
return GetOrPutGlobalPtrResult{ .found_existing = false, .value_ptr = ptr };
}
pub fn getAtom(self: *const Coff, atom_index: Atom.Index) Atom {
assert(atom_index < self.atoms.items.len);
return self.atoms.items[atom_index];
}
pub fn getAtomPtr(self: *Coff, atom_index: Atom.Index) *Atom {
assert(atom_index < self.atoms.items.len);
return &self.atoms.items[atom_index];
}
/// Returns atom if there is an atom referenced by the symbol described by `sym_loc` descriptor.
/// Returns null on failure.
pub fn getAtomIndexForSymbol(self: *const Coff, sym_loc: SymbolWithLoc) ?Atom.Index {
assert(sym_loc.file == null); // TODO linking with object files
return self.atom_by_index_table.get(sym_loc.sym_index);
}
fn setSectionName(self: *Coff, header: *coff.SectionHeader, name: []const u8) !void {
if (name.len <= 8) {
@memcpy(header.name[0..name.len], name);
@memset(header.name[name.len..], 0);
return;
}
const gpa = self.base.comp.gpa;
const offset = try self.strtab.insert(gpa, name);
const name_offset = fmt.bufPrint(&header.name, "/{d}", .{offset}) catch unreachable;
@memset(header.name[name_offset.len..], 0);
}
fn getSectionName(self: *const Coff, header: *const coff.SectionHeader) []const u8 {
if (header.getName()) |name| {
return name;
}
const offset = header.getNameOffset().?;
return self.strtab.get(offset).?;
}
fn setSymbolName(self: *Coff, symbol: *coff.Symbol, name: []const u8) !void {
if (name.len <= 8) {
@memcpy(symbol.name[0..name.len], name);
@memset(symbol.name[name.len..], 0);
return;
}
const gpa = self.base.comp.gpa;
const offset = try self.strtab.insert(gpa, name);
@memset(symbol.name[0..4], 0);
mem.writeInt(u32, symbol.name[4..8], offset, .little);
}
fn logSymAttributes(sym: *const coff.Symbol, buf: *[4]u8) []const u8 {
@memset(buf[0..4], '_');
switch (sym.section_number) {
.UNDEFINED => {
buf[3] = 'u';
switch (sym.storage_class) {
.EXTERNAL => buf[1] = 'e',
.WEAK_EXTERNAL => buf[1] = 'w',
.NULL => {},
else => unreachable,
}
},
.ABSOLUTE => unreachable, // handle ABSOLUTE
.DEBUG => unreachable,
else => {
buf[0] = 's';
switch (sym.storage_class) {
.EXTERNAL => buf[1] = 'e',
.WEAK_EXTERNAL => buf[1] = 'w',
.NULL => {},
else => unreachable,
}
},
}
return buf[0..];
}
fn logSymtab(self: *Coff) void {
var buf: [4]u8 = undefined;
log.debug("symtab:", .{});
log.debug(" object(null)", .{});
for (self.locals.items, 0..) |*sym, sym_id| {
const where = if (sym.section_number == .UNDEFINED) "ord" else "sect";
const def_index: u16 = switch (sym.section_number) {
.UNDEFINED => 0, // TODO
.ABSOLUTE => unreachable, // TODO
.DEBUG => unreachable, // TODO
else => @intFromEnum(sym.section_number),
};
log.debug(" %{d}: {?s} @{x} in {s}({d}), {s}", .{
sym_id,
self.getSymbolName(.{ .sym_index = @as(u32, @intCast(sym_id)), .file = null }),
sym.value,
where,
def_index,
logSymAttributes(sym, &buf),
});
}
log.debug("globals table:", .{});
for (self.globals.items) |sym_loc| {
const sym_name = self.getSymbolName(sym_loc);
log.debug(" {s} => %{d} in object({?d})", .{ sym_name, sym_loc.sym_index, sym_loc.file });
}
log.debug("GOT entries:", .{});
log.debug("{}", .{self.got_table});
}
fn logSections(self: *Coff) void {
log.debug("sections:", .{});
for (self.sections.items(.header)) |*header| {
log.debug(" {s}: VM({x}, {x}) FILE({x}, {x})", .{
self.getSectionName(header),
header.virtual_address,
header.virtual_address + header.virtual_size,
header.pointer_to_raw_data,
header.pointer_to_raw_data + header.size_of_raw_data,
});
}
}
fn logImportTables(self: *const Coff) void {
log.debug("import tables:", .{});
for (self.import_tables.keys(), 0..) |off, i| {
const itable = self.import_tables.values()[i];
log.debug("{}", .{itable.fmtDebug(.{
.coff_file = self,
.index = i,
.name_off = off,
})});
}
}
const Coff = @This();
const std = @import("std");
const build_options = @import("build_options");
const builtin = @import("builtin");
const assert = std.debug.assert;
const coff = std.coff;
const fmt = std.fmt;
const log = std.log.scoped(.link);
const math = std.math;
const mem = std.mem;
const Allocator = std.mem.Allocator;
const codegen = @import("../codegen.zig");
const link = @import("../link.zig");
const lld = @import("Coff/lld.zig");
const trace = @import("../tracy.zig").trace;
const Air = @import("../Air.zig");
pub const Atom = @import("Coff/Atom.zig");
const Compilation = @import("../Compilation.zig");
const ImportTable = @import("Coff/ImportTable.zig");
const Liveness = @import("../Liveness.zig");
const LlvmObject = @import("../codegen/llvm.zig").Object;
const Module = @import("../Module.zig");
const InternPool = @import("../InternPool.zig");
const Object = @import("Coff/Object.zig");
const Relocation = @import("Coff/Relocation.zig");
const TableSection = @import("table_section.zig").TableSection;
const StringTable = @import("StringTable.zig");
const Type = @import("../type.zig").Type;
const Value = @import("../Value.zig");
const TypedValue = @import("../TypedValue.zig");
pub const base_tag: link.File.Tag = .coff;
const msdos_stub = @embedFile("msdos-stub.bin");
|