Source file linker_script.ml
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
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
open Lem_basic_classes
open Lem_function
open Lem_string
open Lem_tuple
open Lem_bool
open Lem_list
open Lem_sorting
open Lem_num
open Lem_maybe
open Lem_assert_extra
open Lem_set
open Byte_pattern
open Byte_sequence
open Default_printing
open Error
open Missing_pervasives
open Show
open Elf_header
open Elf_file
open Elf_interpreted_section
open Abis
open Command_line
open Input_list
open Linkable_list
open Memory_image
open Elf_memory_image
open Elf_memory_image_of_elf64_file
open Elf_relocation
open Elf_symbol_table
open Elf_section_header_table
open Elf_types_native_uint
open Memory_image_orderings
type input_section_rec = {
idx : Nat_big_num.num
; fname : string
; img : elf_memory_image
; shndx : Nat_big_num.num
; secname: string
; isec : elf64_interpreted_section
}
type input_spec
= Common of (Nat_big_num.num * string * elf_memory_image * symbol_definition)
| InputSection of input_section_rec
type symbol_def_policy = AlwaysDefine
| ProvideIfUsed
type input_selector = input_spec list -> input_spec list
type address_expr = Memory_image.expr
type output_guard = AlwaysOutput
| OnlyIfRo
| OnlyIfRw
type symbol_spec = (Nat_big_num.num * Uint32_wrapper.uint32 * Uint32_wrapper.uint32)
type retain_policy
= DefaultKeep
| KeepEvenWhenGC
type address_expr_fn_ref = Nat_big_num.num
type 'a address_expr_fn_map = (address_expr_fn_ref, (Nat_big_num.num -> 'a -> Nat_big_num.num)) Pmap.map
type output_section_composition_element
= IncludeInputSection of (retain_policy * input_section_rec)
| IncludeCommonSymbol of (retain_policy * string * Nat_big_num.num * symbol_definition * elf_memory_image)
| Hole of address_expr_fn
| ProvideSymbol of (symbol_def_policy * string * symbol_spec)
and
sort_policy
= DefaultSort
| SeenOrder
| ByName
| ByNameThenAlignment
| ByAlignment
| ByAlignmentThenName
| ByInitPriority
and
output_section_spec =
OutputSectionSpec of (output_guard * Nat_big_num.num option * string * ( output_section_composition_element list))
and
allocated_sections_map =
AllocatedSectionsMap of (string, (output_section_spec * Nat_big_num.num)) Pmap.map
and
address_expr_fn
= AddressExprFn of address_expr_fn_ref
type script_element =
DefineSymbol of (symbol_def_policy * string * symbol_spec)
| AdvanceAddress of address_expr_fn
| MarkAndAlignDataSegment of (Nat_big_num.num * Nat_big_num.num)
| MarkDataSegmentEnd
| MarkDataSegmentRelroEnd
| OutputSection of (output_guard * ( address_expr_fn option) * string * script_element list)
| DiscardInput of input_selector
| InputQuery of (retain_policy * sort_policy * input_selector)
type linker_control_script = script_element list
type labelled_linker_control_script = (script_element * Nat_big_num.num) list
let rec all_suffixes chars:((char)list)list=
((match chars with
[] -> [[]]
| c :: morecs -> chars :: (all_suffixes morecs)
))
let rec glob_match pat str:bool=
((match (pat, str) with
([], []) -> true
| ('?':: morepat, _ :: morestr) -> glob_match morepat morestr
| ('*':: morepat, _) ->
let or_suffix_match = (fun matched -> (fun newlist ->
matched || glob_match morepat newlist))
in
List.fold_left (or_suffix_match) false (all_suffixes str)
| (patc :: morepat, c :: morestr) -> (patc = c) && glob_match morepat morestr
| ([], _) -> false
| (_, []) -> false
))
let default_symbol_spec:Nat_big_num.num*Uint32_wrapper.uint32*Uint32_wrapper.uint32= ( (Nat_big_num.of_int 0), Uint32_wrapper.of_bigint( (Nat_big_num.of_int 0)), Uint32_wrapper.of_bigint( (Nat_big_num.of_int 0)))
let hidden_symbol_spec:Nat_big_num.num*Uint32_wrapper.uint32*Uint32_wrapper.uint32= ( (Nat_big_num.of_int 0), Uint32_wrapper.of_bigint( (Nat_big_num.of_int 0)), Uint32_wrapper.of_bigint stv_hidden)
let only_sections inputs:(input_spec)list= (Lem_list.mapMaybe
(fun i -> (match i with
| InputSection(_) -> Some(i)
| _ -> None
)) inputs)
let filter_and_concat p inputs:(input_spec)list= (List.filter p inputs)
let name_matches pat input:bool=
((match input with
InputSection(inp) ->
glob_match (Xstring.explode pat) (Xstring.explode inp.secname)
| _ -> false
))
let file_matches pat input:bool=
((match input with
InputSection(inp) -> glob_match (Xstring.explode pat) (Xstring.explode inp.fname)
| _ -> false
))
let compareInputSpecByNameThenAlignment i1 i2:int=
(let toPair = (fun is -> ((match is with
Common(idx1, fname1, img2, def) -> ("COMMON" , Ml_bindings.nat_big_num_of_uint64 def.def_syment.elf64_st_value)
| InputSection(isrec) -> (isrec.isec.elf64_section_name_as_string, isrec.isec.elf64_section_align)
)))
in (pairCompare compare Nat_big_num.compare (toPair i1) (toPair i2)))
let compareInputSpecByAlignment i1 i2:int=
(let toNatural = (fun is -> ((match is with
Common(idx1, fname1, img2, def) -> Ml_bindings.nat_big_num_of_uint64 def.def_syment.elf64_st_value
| InputSection(isrec) -> isrec.isec.elf64_section_align
)))
in Nat_big_num.compare (toNatural i1) (toNatural i2))
let compareInputSpecByName i1 i2:int=
(let toString = (fun is -> ((match is with
Common(idx1, fname1, img2, def) -> "COMMON"
| InputSection(isrec) -> isrec.isec.elf64_section_name_as_string
)))
in compare (toString i1) (toString i2))
let compareInputSpecByAlignmentThenName i1 i2:int=
(let toPair = (fun is -> ((match is with
Common(idx1, fname1, img2, def) -> (Ml_bindings.nat_big_num_of_uint64 def.def_syment.elf64_st_value,
"COMMON" )
| InputSection(isrec) -> (isrec.isec.elf64_section_align, isrec.isec.elf64_section_name_as_string)
)))
in (pairCompare Nat_big_num.compare compare (toPair i1) (toPair i2)))
let compareInputSpecByInitPriority i1 i2:int= 0
let has_writability:'a ->input_spec ->bool= (fun writable -> (fun input_sec -> (
(match input_sec with
Common(_, _, _, _)
-> true
| InputSection(inp)
-> let (flags : Nat_big_num.num) = ((match elf_memory_image_section_by_index inp.shndx inp.img with
Some x -> x.elf64_section_flags
| None -> failwith ("impossible: no such section" )
))
in
flag_is_set shf_write flags
)
)))
let address_zero fresh alloc_map:Nat_big_num.num*((Nat_big_num.num),(Nat_big_num.num ->allocated_sections_map ->Nat_big_num.num))Pmap.map*address_expr_fn=
(let alloc_map' = (Pmap.add fresh (fun pos -> (fun secs -> (Nat_big_num.of_int 0))) alloc_map) in
let fresh' = (Nat_big_num.add( (Nat_big_num.of_int 1)) fresh) in
(fresh', alloc_map', AddressExprFn fresh))
let do_output_section_layout_starting_at_addr start_addr (AllocatedSectionsMap secs) comps:Nat_big_num.num*(Nat_big_num.num)list=
(
List.fold_left (fun (next_free_addr, addr_list) -> (fun comp_el -> (match comp_el with
IncludeInputSection(retain_pol, irec ) ->
let aligned_next_free = (align_up_to irec.isec.elf64_section_align next_free_addr)
in
( Nat_big_num.add aligned_next_free irec.isec.elf64_section_size, List.rev_append (List.rev addr_list) [aligned_next_free])
| IncludeCommonSymbol(retain_pol, fname1, linkable_idx, def, img2) ->
let aligned_next_free = (align_up_to (Ml_bindings.nat_big_num_of_uint64 def.def_syment.elf64_st_value) next_free_addr)
in
( Nat_big_num.add aligned_next_free (Ml_bindings.nat_big_num_of_uint64 def.def_syment.elf64_st_size), List.rev_append (List.rev addr_list) [aligned_next_free])
| ProvideSymbol(pol, name1, spec) -> (next_free_addr, List.rev_append (List.rev addr_list) [next_free_addr])
)
)) (start_addr, []) comps)
let output_sec_composition_size_given_start_addr start_addr secs comp:Nat_big_num.num=
(let (end_addr, comp_addrs) = (do_output_section_layout_starting_at_addr start_addr secs comp)
in Nat_big_num.sub_nat
end_addr start_addr)
let sizeof secname1 (AllocatedSectionsMap secs):Nat_big_num.num=
((match Pmap.lookup secname1 secs with
Some(OutputSectionSpec (_, maybe_addr, _, comp), _) -> (match maybe_addr with
Some addr -> output_sec_composition_size_given_start_addr addr (AllocatedSectionsMap secs) comp
| None -> failwith ("error: sizeof applied to section without defined start address")
)
| None -> failwith ("error: sizeof applied to non-existent section name " ^ secname1)
))
let alignof_output_section_composition_element comp:Nat_big_num.num=
((match comp with
IncludeInputSection(_, irec) -> irec.isec.elf64_section_align
| IncludeCommonSymbol(_, _, _, def, _) -> Ml_bindings.nat_big_num_of_uint64 def.def_syment.elf64_st_value
| _ -> (Nat_big_num.of_int 1)
))
let alignof_output_section comps:Nat_big_num.num=
(let aligns = (Lem_list.map alignof_output_section_composition_element comps)
in
List.fold_left (fun acc_lcm -> fun next -> lcm acc_lcm next)( (Nat_big_num.of_int 1)) aligns)
let default_linker_control_script fresh alloc_map a user_text_segment_start user_data_segment_start user_rodata_segment_start :Nat_big_num.num*((Nat_big_num.num),(Nat_big_num.num ->allocated_sections_map ->Nat_big_num.num))Pmap.map*(script_element)list=
(let segment_start name1 default= ((match name1 with
"ldata-segment" -> (match user_data_segment_start with
None -> default
| Some addr -> addr
)
| "text-segment" -> (match user_text_segment_start with
None -> default
| Some addr -> addr
)
))
in
let is_large_common = (fun inp -> false
)
in
let is_common = (fun isec1 -> (match isec1 with
Common(idx1, fname1, img2, def) ->
not (is_large_common isec1)
| _ -> false
))
in
let alloc_fn1 = (fun _ -> (fun _ -> Nat_big_num.add (segment_start "text-segment" ( Nat_big_num.mul( (Nat_big_num.of_int 4))( (Nat_big_num.of_int 1048576)))) elf_headers_size)) in
let alloc_fn1_ref = fresh in
let alloc_map = (Pmap.add alloc_fn1_ref alloc_fn1 alloc_map) in
let fresh = (Nat_big_num.add( (Nat_big_num.of_int 1)) fresh) in
let alloc_fn2 = (fun addr -> (fun _ -> Nat_big_num.sub_nat
(align_up_to a.maxpagesize addr) (Nat_big_num.bitwise_and ( Nat_big_num.add a.maxpagesize (compl64 addr)) ( Nat_big_num.sub_nat a.maxpagesize( (Nat_big_num.of_int 1)))))) in
let (fresh, alloc_map, (address_zero_fn : address_expr_fn)) = (address_zero fresh alloc_map) in
let alloc_fn2_ref = fresh in
let alloc_map = (Pmap.add alloc_fn2_ref alloc_fn2 alloc_map) in
let fresh = (Nat_big_num.add( (Nat_big_num.of_int 1)) fresh) in
let alloc_fn3 = (fun pos -> (fun secs -> align_up_to (if Nat_big_num.equal pos( (Nat_big_num.of_int 0)) then (Nat_big_num.div( (Nat_big_num.of_int 64))( (Nat_big_num.of_int 8))) else (Nat_big_num.of_int 1)) pos)) in
let alloc_fn3_ref = fresh in
let alloc_map = (Pmap.add alloc_fn3_ref alloc_fn3 alloc_map) in
let fresh = (Nat_big_num.add( (Nat_big_num.of_int 1)) fresh) in
let alloc_fn4 = (fun pos -> (fun secs -> align_up_to (Nat_big_num.div( (Nat_big_num.of_int 64))( (Nat_big_num.of_int 8))) pos)) in
let alloc_fn4_ref = fresh in
let alloc_map = (Pmap.add alloc_fn4_ref alloc_fn4 alloc_map) in
let fresh = (Nat_big_num.add( (Nat_big_num.of_int 1)) fresh) in
let alloc_fn5 = (fun pos -> (fun secs -> segment_start "ldata-segment" pos)) in
let alloc_fn5_ref = fresh in
let alloc_map = (Pmap.add alloc_fn5_ref alloc_fn5 alloc_map) in
let fresh = (Nat_big_num.add( (Nat_big_num.of_int 1)) fresh) in
let alloc_fn6 = (fun pos -> fun secs -> align_up_to ( Nat_big_num.add a.maxpagesize ( Nat_big_num.sub_nat(Nat_big_num.bitwise_and pos a.maxpagesize)( (Nat_big_num.of_int 1)))) pos) in
let alloc_fn6_ref = fresh in
let alloc_map = (Pmap.add alloc_fn6_ref alloc_fn6 alloc_map) in
let fresh = (Nat_big_num.add( (Nat_big_num.of_int 1)) fresh) in
let alloc_fn7 = (fun pos -> (fun secs -> (if not (Nat_big_num.equal pos( (Nat_big_num.of_int 0))) then Nat_big_num.div( (Nat_big_num.of_int 64))( (Nat_big_num.of_int 8)) else (Nat_big_num.of_int 1)))) in
let alloc_fn7_ref = fresh in
let alloc_map = (Pmap.add alloc_fn7_ref alloc_fn7 alloc_map) in
let fresh = (Nat_big_num.add( (Nat_big_num.of_int 1)) fresh) in
let alloc_fn8 = (fun pos -> (fun secs -> align_up_to (Nat_big_num.div( (Nat_big_num.of_int 64))( (Nat_big_num.of_int 8))) pos)) in
let alloc_fn8_ref = fresh in
let alloc_map = (Pmap.add alloc_fn8_ref alloc_fn8 alloc_map) in
let fresh = (Nat_big_num.add( (Nat_big_num.of_int 1)) fresh) in
(fresh, alloc_map, [
(DefineSymbol(ProvideIfUsed, "__executable_start", default_symbol_spec))
; AdvanceAddress(AddressExprFn alloc_fn1_ref)
; OutputSection(AlwaysOutput, None, ".interp", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".interp"))])
; OutputSection(AlwaysOutput, None, ".note.gnu.build-id", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".note.gnu.build-id"))])
; OutputSection(AlwaysOutput, None, ".hash", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".hash"))])
; OutputSection(AlwaysOutput, None, ".gnu.hash", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".gnu.hash"))])
; OutputSection(AlwaysOutput, None, ".dynsym", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".dynsym"))])
; OutputSection(AlwaysOutput, None, ".dynstr", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".dynstr"))])
; OutputSection(AlwaysOutput, None, ".gnu.version", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".gnu.version"))])
; OutputSection(AlwaysOutput, None, ".gnu.version_d", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".gnu.version_d"))])
; OutputSection(AlwaysOutput, None, ".gnu.version_r", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".gnu.version_r"))])
; OutputSection(AlwaysOutput, None, ".rela.dyn", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".rela.init"))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".rela.text" s || (name_matches ".rela.text.*" s || name_matches ".rela.gnu.linkonce.t.*" s)))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".rela.rodata" s || (name_matches ".rela.rodata.*" s || name_matches ".rela.gnu.linkonce.r.*" s)))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".rela.data" s || (name_matches ".rela.data.*" s || name_matches ".rela.gnu.linkonce.d.*" s)))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".rela.tdata" s || (name_matches ".rela.tdata.*" s || name_matches ".rela.gnu.linkonce.td.*" s)))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".rela.tbss" s || (name_matches ".rela.tbss.*" s || name_matches ".rela.gnu.linkonce.tb.*" s)))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".rela.ctors"))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".rela.got"))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".rela.bss" s || (name_matches ".rela.bss.*" s || name_matches ".rela.gnu.linkonce.b.*" s)))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".rela.ldata" s || (name_matches ".rela.ldata.*" s || name_matches ".rela.gnu.linkonce.l.*" s)))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".rela.lbss" s || (name_matches ".rela.lbss.*" s || name_matches ".rela.gnu.linkonce.lb.*" s)))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".rela.ifunc"))
])
; OutputSection(AlwaysOutput, None, ".rela.plt", [
InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".rela.plt"))
; DefineSymbol(ProvideIfUsed, "__rela_iplt_start", ( (Nat_big_num.of_int 0), make_symbol_info stb_local stt_notype , make_symbol_other stv_hidden))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".rela.iplt"))
; DefineSymbol(ProvideIfUsed, "__rela_iplt_end", ( (Nat_big_num.of_int 0), make_symbol_info stb_local stt_notype , make_symbol_other stv_hidden))
])
; OutputSection(AlwaysOutput, None, ".init", [
InputQuery(KeepEvenWhenGC, SeenOrder, filter_and_concat (name_matches ".init"))
])
; OutputSection(AlwaysOutput, None, ".plt", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".plt"))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".iplt"))
])
; OutputSection(AlwaysOutput, None, ".plt.bnd", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".plt.bnd"))])
; OutputSection(AlwaysOutput, None, ".text", [
InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".text.unlikely" s || (name_matches ".text.*_unlikely" s || name_matches ".text.unlikely.*" s)
))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".text.exit" s || name_matches ".text.exit.*" s))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".text.startup" s || name_matches ".text.startup.*" s))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".text.hot" s || name_matches ".text.hot.*" s))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".text" s || (name_matches ".stub" s || (name_matches ".text.*" s || name_matches ".gnu.linkonce.t.*" s))))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".gnu_warning" s))
])
; OutputSection(AlwaysOutput, None, ".fini", [
InputQuery(KeepEvenWhenGC, SeenOrder, filter_and_concat (name_matches ".fini"))
])
; DefineSymbol(ProvideIfUsed, "__etext", default_symbol_spec)
; DefineSymbol(ProvideIfUsed, "_etext", default_symbol_spec)
; DefineSymbol(ProvideIfUsed, "etext", default_symbol_spec)
; OutputSection(AlwaysOutput, None, ".rodata", [
InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".rodata" s || (name_matches ".rodata.*" s || name_matches ".gnu.linkonce.r.*" s)
))])
; OutputSection(AlwaysOutput, None, ".eh_frame_hdr", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".eh_frame_hdr")) ])
; OutputSection(OnlyIfRo, None, ".eh_frame", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".eh_frame"))])
; OutputSection(OnlyIfRo, None, ".gcc_except_table", [InputQuery(DefaultKeep, DefaultSort,
filter_and_concat (fun s -> name_matches ".gcc_except_table" s || name_matches ".gcc_except_table.*" s))])
; OutputSection(OnlyIfRo, None, ".exception_ranges", [InputQuery(DefaultKeep, DefaultSort,
filter_and_concat (fun s -> name_matches ".exception_ranges" s || name_matches ".exception_ranges*" s))])
; AdvanceAddress(AddressExprFn alloc_fn2_ref)
; MarkAndAlignDataSegment( Nat_big_num.mul (Nat_big_num.mul( (Nat_big_num.of_int 2))( (Nat_big_num.of_int 1024)))( (Nat_big_num.of_int 1024)) , a.commonpagesize)
; OutputSection(OnlyIfRw, None, ".eh_frame", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".eh_frame"))])
; OutputSection(OnlyIfRw, None, ".gcc_except_table", [InputQuery(DefaultKeep, DefaultSort,
filter_and_concat (fun s -> name_matches ".gcc_except_table" s || name_matches ".gcc_except_table.*" s))])
; OutputSection(OnlyIfRw, None, ".exception_ranges", [InputQuery(DefaultKeep, DefaultSort,
filter_and_concat (fun s -> name_matches ".exception_ranges" s || name_matches ".exception_ranges*" s))])
; OutputSection(AlwaysOutput, None, ".tdata", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat
(fun s -> name_matches ".tdata" s || (name_matches ".tdata.*" s || name_matches ".gnu.linkonce.td.*" s)))])
; OutputSection(AlwaysOutput, None, ".tbss", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat
(fun s -> name_matches ".tbss" s || (name_matches ".tbss.*" s || name_matches ".gnu.linkonce.tb.*" s)))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".tcommon"))])
; OutputSection(AlwaysOutput, None, ".preinit_array", [
DefineSymbol(ProvideIfUsed, "__preinit_array_start", default_symbol_spec)
; InputQuery(KeepEvenWhenGC, DefaultSort, filter_and_concat (fun s -> name_matches ".preinit_array" s))
; DefineSymbol(ProvideIfUsed, "__preinit_array_end", default_symbol_spec)
])
; OutputSection(AlwaysOutput, None, ".init_array", [
DefineSymbol(ProvideIfUsed, "__init_array_start", default_symbol_spec)
; InputQuery(KeepEvenWhenGC, ByInitPriority, filter_and_concat (fun s -> name_matches ".init_array.*" s))
; InputQuery(KeepEvenWhenGC, ByInitPriority, filter_and_concat (fun s -> name_matches ".ctors.*" s))
; InputQuery(KeepEvenWhenGC, ByInitPriority, filter_and_concat
(fun s -> name_matches ".init_array" s
|| (name_matches ".ctors" s && not (file_matches "*crtbegin.o" s || (file_matches "*crtbegin?.o" s
|| (file_matches "*crtend.o" s || file_matches "*crtend?.o " s)))))
)
; DefineSymbol(ProvideIfUsed, "__init_array_end", default_symbol_spec)
])
; OutputSection(AlwaysOutput, None, ".fini_array", [
DefineSymbol(ProvideIfUsed, "__fini_array_start", default_symbol_spec)
; InputQuery(KeepEvenWhenGC, ByInitPriority, filter_and_concat (fun s -> name_matches ".fini_array.*" s))
; InputQuery(KeepEvenWhenGC, ByInitPriority, filter_and_concat (fun s -> name_matches ".dtors.*" s))
; InputQuery(KeepEvenWhenGC, ByInitPriority, filter_and_concat
(fun s -> name_matches ".fini_array" s
|| (name_matches ".dtors" s && not (file_matches "*crtbegin.o" s || (file_matches "*crtbegin?.o" s
|| (file_matches "*crtend.o" s || file_matches "*crtend?.o " s)))))
)
; DefineSymbol(ProvideIfUsed, "__fini_array_end", default_symbol_spec)
])
; OutputSection(AlwaysOutput, None, ".ctors", [
InputQuery(KeepEvenWhenGC, DefaultSort, filter_and_concat (fun s -> file_matches "*crtbegin.o" s && name_matches ".ctors" s))
; InputQuery(KeepEvenWhenGC, DefaultSort, filter_and_concat (fun s -> file_matches "*crtbegin?.o" s && name_matches ".ctors" s))
; InputQuery(KeepEvenWhenGC, DefaultSort, filter_and_concat
(fun s -> not (file_matches "*crtend.o" s || file_matches "*crtend?.o" s) && name_matches ".ctors" s))
; InputQuery(KeepEvenWhenGC, ByName, filter_and_concat (fun s -> name_matches ".ctors.*" s))
; InputQuery(KeepEvenWhenGC, DefaultSort, filter_and_concat
(fun s -> (file_matches "*crtend.o" s || file_matches "*crtend?.o" s) && name_matches ".ctors" s))
])
; OutputSection(AlwaysOutput, None, ".dtors", [
InputQuery(KeepEvenWhenGC, DefaultSort, filter_and_concat (fun s -> file_matches "*crtbegin.o" s && name_matches ".dtors" s))
; InputQuery(KeepEvenWhenGC, DefaultSort, filter_and_concat (fun s -> file_matches "*crtbegin?.o" s && name_matches ".dtors" s))
; InputQuery(KeepEvenWhenGC, DefaultSort, filter_and_concat
(fun s -> not (file_matches "*crtend.o" s || file_matches "*crtend?.o" s) && name_matches ".dtors" s))
; InputQuery(KeepEvenWhenGC, ByName, filter_and_concat (fun s -> name_matches ".dtors.*" s))
; InputQuery(KeepEvenWhenGC, DefaultSort, filter_and_concat
(fun s -> (file_matches "*crtend.o" s || file_matches "*crtend?.o" s) && name_matches ".dtors" s))
])
; OutputSection(AlwaysOutput, None, ".jcr", [InputQuery(KeepEvenWhenGC, DefaultSort, filter_and_concat (name_matches ".jcr"))])
; OutputSection(AlwaysOutput, None, ".data.rel.ro", [
InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".data.rel.ro.local*" s || name_matches ".gnu.linkonce.d.rel.ro.local.*" s
));
InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".data.rel.ro" s || (name_matches ".data.rel.ro.*" s || name_matches ".gnu.linkonce.d.rel.ro.*" s)
))
])
; OutputSection(AlwaysOutput, None, ".dynamic", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".dynamic"))])
; OutputSection(AlwaysOutput, None, ".got", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".got"))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".igot"))
])
; MarkDataSegmentRelroEnd
; OutputSection(AlwaysOutput, None, ".got.plt", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".got.plt"))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".igot.plt"))
])
; OutputSection(AlwaysOutput, None, ".data", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".data" s || (name_matches ".data.*" s || name_matches ".gnu.linkonce.d.*" s)))
])
; OutputSection(AlwaysOutput, None, ".data1", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".data1"))])
; DefineSymbol(AlwaysDefine, "_edata", default_symbol_spec)
; DefineSymbol(ProvideIfUsed, "edata", default_symbol_spec)
;
DefineSymbol(AlwaysDefine, "__bss_start", default_symbol_spec)
; OutputSection(AlwaysOutput, None, ".bss", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".dynbss"))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".bss" s || (name_matches ".bss.*" s || name_matches ".gnu.linkonce.b.*" s)))
; InputQuery(DefaultKeep, DefaultSort, (fun inputlist ->
let result = (filter_and_concat is_common inputlist)
in
result)
)
])
; AdvanceAddress(AddressExprFn alloc_fn3_ref)
; OutputSection(AlwaysOutput, None, ".lbss", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".dynlbss"))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".dynlbss"))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".lbss" s || (name_matches ".lbss.*" s || name_matches ".gnu.linkonce.lb.*" s)
))
; InputQuery(DefaultKeep, DefaultSort, filter_and_concat (is_large_common))
])
; AdvanceAddress(AddressExprFn alloc_fn4_ref)
; AdvanceAddress(AddressExprFn alloc_fn5_ref)
; OutputSection(AlwaysOutput, Some (AddressExprFn alloc_fn6_ref),
".lrodata",
[InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".lrodata" s || (name_matches ".lrodata.*" s || name_matches ".gnu.linkonce.lr.*" s)
))
; AdvanceAddress(AddressExprFn alloc_fn7_ref)
])
; AdvanceAddress(AddressExprFn alloc_fn8_ref)
; DefineSymbol(AlwaysDefine, "_end", default_symbol_spec)
; DefineSymbol(ProvideIfUsed, "end", default_symbol_spec)
; MarkDataSegmentEnd
; OutputSection(AlwaysOutput, Some address_zero_fn, ".stab", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".stab"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".stabstr", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".stabstr"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".stab.excl", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".stab.excl"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".stab.exclstr", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".stab.exclstr"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".stab.index", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".stab.index"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".stab.indexstr", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".stab.indexstr"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".comment", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".comment"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".line", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".line"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_srcinfo", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_srcinfo"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_sfnames", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_sfname"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_aranges", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_aranges"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_pubnames", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_pubnames"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_info", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".debug_info" s || name_matches ".gnu.linkonce.wi.*" s))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_abbrev", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_abbrev"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_line", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (
fun s -> name_matches ".debug_line" s || (name_matches ".debug_line.*" s || name_matches ".debug_line_end" s)))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_frame", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_frame"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_str", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_str"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_loc", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_loc"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_macinfo", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_macinfo"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_weaknames", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_weaknames"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_funcnames", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_funcnames"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_typenames", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_typenames"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_varnames", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_varnames"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_pubtypes", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_pubtypes"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_ranges", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_ranges"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".debug_macro", [InputQuery(DefaultKeep, DefaultSort, filter_and_concat (name_matches ".debug_macro"))])
; OutputSection(AlwaysOutput, Some address_zero_fn, ".gnu.attributes", [InputQuery(KeepEvenWhenGC, DefaultSort, filter_and_concat (name_matches ".gnu.attributes"))])
; DiscardInput(filter_and_concat (fun s -> name_matches ".note.GNU-stack" s || (name_matches ".gnu_debuglink" s || name_matches ".gnu.lto_*" s)))
]))
let interpret_guard guard comp name1:bool=
((match guard with
always0 -> true
| OnlyIfRo ->
let v = (List.for_all (fun comp_el -> (match comp_el with
IncludeInputSection(retainpol, irec) -> Nat_big_num.equal(
(Nat_big_num.of_int 0)) (Nat_big_num.bitwise_and irec.isec.elf64_section_flags shf_write)
| _ -> true
)) comp)
in v
| OnlyIfRw ->
let v = (List.for_all (fun comp_el -> (match comp_el with
IncludeInputSection(retainpol, irec) -> not (Nat_big_num.equal(
(Nat_big_num.of_int 0)) (Nat_big_num.bitwise_and irec.isec.elf64_section_flags shf_write))
| _ -> true
)) comp)
in v
))
let label_script_aux start script1:(script_element*Nat_big_num.num)list=
(mapi (fun i -> fun el -> (el, ( Nat_big_num.add start (Nat_big_num.of_int i)))) script1)
let label_script script1:(script_element*Nat_big_num.num)list= (label_script_aux( (Nat_big_num.of_int 0)) script1)
type input_output_assignment = ( input_spec list * (output_section_spec * Nat_big_num.num) list)
let rec assign_inputs_to_output_sections acc used_sections used_commons inputs (cur_output_sec : (output_section_spec * Nat_big_num.num)option) last_input_sec seen_ordering script1:(input_spec)list*(output_section_spec*Nat_big_num.num)list=
(let (rev_discards, rev_outputs) = acc in
let flush_output_sec
= (fun maybe_output_sec_and_idx -> (match (maybe_output_sec_and_idx : (output_section_spec * Nat_big_num.num)option) with
Some (OutputSectionSpec (guard, addr, name1, comp), script_idx) ->
if interpret_guard guard comp name1
then (rev_discards, (((OutputSectionSpec (guard, addr, name1, comp)), script_idx) :: rev_outputs))
else acc
| None ->
acc
))
in
(match script1 with
[] -> flush_output_sec cur_output_sec
| (element1, idx1) :: more_elements_and_idx ->
let do_nothing = (acc, used_sections, used_commons, cur_output_sec, last_input_sec)
in
let (new_acc, new_used_sections, new_used_commons, (new_cur_output_sec : (output_section_spec * Nat_big_num.num)option), new_last_input_sec)
= ((match element1 with
DefineSymbol(symdefpol, name1, (symsize, syminfo, symother)) ->
(acc,
used_sections,
used_commons,
(match (cur_output_sec : (output_section_spec * Nat_big_num.num)option) with
None -> None
| Some ((OutputSectionSpec (guard, maybe_addr, secname1, comp)), output_script_idx) ->
Some ((OutputSectionSpec (guard, maybe_addr, secname1,
List.rev_append (List.rev comp) [ProvideSymbol(symdefpol, name1, (symsize, syminfo, symother))]))
, output_script_idx)
),
last_input_sec)
| AdvanceAddress(AddressExprFn advance_fn) ->
(match cur_output_sec with
None -> do_nothing
| Some (sec, idx1) -> do_nothing
)
| MarkAndAlignDataSegment(maxpagesize1, commonpagesize1) ->
do_nothing
| MarkDataSegmentEnd ->
do_nothing
| MarkDataSegmentRelroEnd ->
do_nothing
| OutputSection(outputguard, maybe_expr, name1, sub_elements) ->
let acc_with_output_sec = (flush_output_sec cur_output_sec)
in
let new_cur_output_sec = (Some((OutputSectionSpec(outputguard, None, name1, [])), idx1))
in
let final_acc
= (assign_inputs_to_output_sections acc used_sections used_commons inputs new_cur_output_sec last_input_sec seen_ordering (label_script sub_elements))
in
(final_acc, used_sections, used_commons, None, last_input_sec)
| DiscardInput(selector) ->
let selected = (selector inputs)
in
let (rev_discards, rev_outputs) = acc in
(( List.rev_append (List.rev (List.rev (let x2 =
([]) in List.fold_right (fun i x2 -> if true then i :: x2 else x2) selected x2))) rev_discards, rev_outputs), used_sections, used_commons, cur_output_sec, last_input_sec)
| InputQuery(retainpol, sortpol, selector) ->
(match cur_output_sec with
None -> failwith "linker script error: input query without output section"
| Some ((OutputSectionSpec (output_guard1, output_sec_addr, output_sec_name, output_composition)), output_script_idx) ->
let sortfun = ((match sortpol with
DefaultSort -> List.sort seen_ordering
| SeenOrder -> List.sort seen_ordering
| ByName -> List.sort compareInputSpecByName
| ByNameThenAlignment -> List.sort compareInputSpecByNameThenAlignment
| ByAlignment -> List.sort compareInputSpecByAlignment
| ByAlignmentThenName -> List.sort compareInputSpecByAlignmentThenName
| ByInitPriority -> List.sort compareInputSpecByInitPriority
))
in
let selected = (selector inputs)
in
let selected_deduplicated = (List.filter (fun inp -> (match inp with
InputSection(irec) -> not ( Pset.mem(irec.idx, irec.shndx) used_sections)
| Common(idx1, fname1, img2, def) -> not ( Pset.mem(idx1, def.def_sym_scn, def.def_sym_idx) used_commons)
)) selected)
in
let sorted_selected_inputs = (sortfun selected_deduplicated)
in
let (sectionMatchList : input_section_rec list) = (Lem_list.mapMaybe (fun inp ->
(match inp with
InputSection(x) ->
Some x
| _ -> None
)) sorted_selected_inputs)
in
let commonMatchList = (Lem_list.mapMaybe (fun inp ->
(match inp with
| Common(idx1, fname1, img2, def) -> Some(idx1, fname1, img2, def)
| _ -> None
)) sorted_selected_inputs)
in
(acc,
Pset.(union) used_sections (let x2 =(Pset.from_list (pairCompare Nat_big_num.compare Nat_big_num.compare)
[]) in List.fold_right
(fun irec x2 -> if true then Pset.add (irec.idx, irec.shndx) x2 else x2)
sectionMatchList x2),
Pset.(union) used_commons (let x2 =(Pset.from_list (tripleCompare Nat_big_num.compare Nat_big_num.compare Nat_big_num.compare)
[]) in List.fold_right
(fun(idx1, fname1, img2, def) x2 ->
if true then Pset.add (idx1, def.def_sym_scn, def.def_sym_idx) x2 else x2)
commonMatchList x2),
Some (
(OutputSectionSpec(output_guard1, output_sec_addr, output_sec_name,
List.rev_append (List.rev (List.rev_append (List.rev output_composition) (let x2 =
([]) in List.fold_right
(fun input_sec x2 ->
if true then
IncludeInputSection
(retainpol, input_sec)
:: x2 else x2) sectionMatchList x2))) (let x2 = ([]) in List.fold_right
(fun(idx1, fname1, img2, def) x2 ->
if true then
IncludeCommonSymbol (DefaultKeep, fname1, idx1, def, img2) :: x2 else
x2) commonMatchList x2)
)), output_script_idx),
last_input_sec
)
)
))
in
assign_inputs_to_output_sections new_acc new_used_sections new_used_commons
(inputs : input_spec list)
(new_cur_output_sec)
(new_last_input_sec : input_spec option)
seen_ordering
(more_elements_and_idx : labelled_linker_control_script)
))
let compute_def_use_and_gc outputs_by_name:allocated_sections_map= outputs_by_name
let output_section_type comp:Nat_big_num.num=
(
let all_nobits = (List.for_all (fun comp_el ->
(match comp_el with
IncludeInputSection(retain_pol, irec) -> Nat_big_num.equal
irec.isec.elf64_section_type sht_nobits
| IncludeCommonSymbol(retain_pol, fname1, linkable_idx, def, img2) -> true
| _ -> true
)) comp)
in
if all_nobits then sht_nobits else sht_progbits)
let output_section_flags comp:Nat_big_num.num=
(let writable = (List.exists (fun comp_el ->
(match comp_el with
IncludeInputSection(retain_pol, irec) ->
flag_is_set shf_write irec.isec.elf64_section_flags
| IncludeCommonSymbol(retain_pol, fname1, linkable_idx, def, img2) ->
true
| _ -> false
)) comp)
in
let executable = (List.exists (fun comp_el ->
(match comp_el with
IncludeInputSection(retain_pol, irec) ->
flag_is_set shf_execinstr irec.isec.elf64_section_flags
| IncludeCommonSymbol(retain_pol, fname1, linkable_idx, def, img2) ->
false
| _ -> false
)) comp)
in
let alloc = (List.exists (fun comp_el ->
(match comp_el with
IncludeInputSection(retain_pol, irec) ->
flag_is_set shf_alloc irec.isec.elf64_section_flags
| IncludeCommonSymbol(retain_pol, fname1, linkable_idx, def, img2) ->
true
| ProvideSymbol(pol, name1, spec) ->
true
| _ -> false
)) comp)
in
let is_thread_local_yesnomaybe = (fun comp_el ->
(match comp_el with
IncludeInputSection(retain_pol, irec) ->
Some(flag_is_set shf_tls irec.isec.elf64_section_flags)
| IncludeCommonSymbol(retain_pol, fname1, linkable_idx, def, img2) ->
Some(false)
| ProvideSymbol(pol, name1, spec) ->
Some false
| _ -> None
)
)
in
let thread_local = (
let v = (List.fold_left (fun acc_ynm -> fun comp_el ->
let new_ynm = (is_thread_local_yesnomaybe comp_el)
in
(match (acc_ynm, new_ynm) with
(None, None) -> None
| (None, Some x) -> Some x
| (Some x, None) -> Some x
| (Some true, Some false) -> Some true
| (Some true, Some true) -> Some true
| (Some false, Some false) -> Some false
| (Some true, Some false) -> Some true
)) None comp)
in
if (Lem.option_equal (=) v (Some(true))) && not ( (Lem.option_equal (=)(Some(true))
(List.fold_left (fun acc_ynm -> fun comp_el ->
let new_ynm = (is_thread_local_yesnomaybe comp_el)
in
(match (acc_ynm, new_ynm) with
(None, None) -> None
| (None, Some x) -> Some x
| (Some x, None) -> Some x
| (Some true, Some false) -> Some false
| (Some true, Some true) -> Some true
| (Some false, Some false) -> Some false
| (Some true, Some false) -> Some false
)) None comp))) then failwith "error: section mixes thread-local and non-thread-local inputs"
else (match v with
None -> false
| Some x -> x
)
)
in
Nat_big_num.bitwise_or
(if thread_local then shf_tls else (Nat_big_num.of_int 0))
(Nat_big_num.bitwise_or
(if executable then shf_execinstr else (Nat_big_num.of_int 0))
(Nat_big_num.bitwise_or
(if writable then shf_write else (Nat_big_num.of_int 0))
(if alloc then shf_alloc else (Nat_big_num.of_int 0))
)
))
let symbol_def_for_provide_symbol name1 size2 info other control_script_linkable_idx:symbol_definition=
({
def_symname = name1
; def_syment = ({
elf64_st_name = (Uint32_wrapper.of_bigint( (Nat_big_num.of_int 0)))
; elf64_st_info = info
; elf64_st_other = other
; elf64_st_shndx = (Uint32_wrapper.of_bigint( (Nat_big_num.of_int 0)))
; elf64_st_value = (Uint64_wrapper.of_bigint( (Nat_big_num.of_int 0)))
; elf64_st_size = (Uint64_wrapper.of_bigint size2)
})
; def_sym_scn =( (Nat_big_num.of_int 0))
; def_sym_idx =( (Nat_big_num.of_int 0))
; def_linkable_idx = control_script_linkable_idx
})
let assign_dot_to_itself fresh alloc_map:Nat_big_num.num*((Nat_big_num.num),(Nat_big_num.num ->allocated_sections_map ->Nat_big_num.num))Pmap.map*address_expr_fn=
(let fn = (fun dot -> fun _ -> dot) in
let alloc_map' = (Pmap.add fresh fn alloc_map) in
let fresh' = (Nat_big_num.add( (Nat_big_num.of_int 1)) fresh) in
(fresh', alloc_map', AddressExprFn fresh))
let rec build_image a alloc_map acc pos (AllocatedSectionsMap outputs_by_name) bindings_by_name script1 control_script_linkable_idx linker_defs_by_name:(any_abi_feature)annotated_memory_image*allocated_sections_map=
(let (add_output_section : (Nat_big_num.num * elf_memory_image) -> output_section_spec -> (Nat_big_num.num * elf_memory_image * Nat_big_num.num * output_section_spec))
= (fun (pos, acc_img) ->
(fun (OutputSectionSpec (guard, addr, secname1, comp)) ->
let unaligned_start_addr = ((match addr with
Some a -> failwith ("internal error: section " ^ (secname1 ^ ": did not expect address to be assigned yet"))
| None -> pos
))
in
let align = (alignof_output_section comp)
in
let output_section_start_addr = (align_up_to align unaligned_start_addr)
in
let (end_addr, comp_addrs) = (do_output_section_layout_starting_at_addr output_section_start_addr (AllocatedSectionsMap outputs_by_name) comp)
in
let size2 = (Nat_big_num.sub_nat end_addr output_section_start_addr)
in
let (concatenated_content, final_addr, new_range_tag_pairs) = (List.fold_left (fun (accum_pat, accum_current_addr, accum_meta) -> (fun (comp_el, comp_addr) ->
let make_line = (fun namestr -> (fun addrstr -> (fun szstr -> (fun rhs -> (
(space_padded_and_maybe_newline( (Nat_big_num.of_int 16)) (" " ^ namestr)) ^
(("0x" ^ (left_zero_padded_to( (Nat_big_num.of_int 16)) addrstr)) ^ (" " ^
((left_space_padded_to( (Nat_big_num.of_int 10)) ("0x" ^ szstr)) ^ (" " ^ rhs))))
)))))
in
let (sz, comp_el_pat, this_el_meta) = ((match comp_el with
| IncludeInputSection(retainpolicy, irec) ->
let maybe_secname = (elf_memory_image_element_coextensive_with_section irec.shndx irec.img)
in
(match maybe_secname with
None -> failwith ("impossible: no such section" )
| Some idstr ->
(match Pmap.lookup idstr irec.img.elements with
Some el ->
let section_el_name = (get_unique_name_for_section_from_index irec.shndx irec.isec irec.img)
in
let range_or_sym_is_in_this_sec = (fun maybe_range -> (fun tag ->
(match maybe_range with
Some(el_name, (start, len)) ->
section_el_name = el_name
| None ->
(match tag with
SymbolDef(def) ->
let sym_shndx = (Uint32_wrapper.to_bigint def.def_syment.elf64_st_shndx)
in
if not (Nat_big_num.equal sym_shndx shn_abs) || ( not (Nat_big_num.equal (get_elf64_symbol_type def.def_syment) stt_section)) then false
else (
let abs_address = (Ml_bindings.nat_big_num_of_uint64 def.def_syment.elf64_st_value)
in
let section_end_addr = (Nat_big_num.add accum_current_addr irec.isec.elf64_section_size)
in
( Nat_big_num.greater_equal abs_address accum_current_addr
&& Nat_big_num.less abs_address section_end_addr)
&& false
)
| _ -> false
)
)
))
in
let ranges_and_tags = (let x2 =
([]) in List.fold_right
(fun(maybe_range, tag) x2 ->
if range_or_sym_is_in_this_sec maybe_range tag then
(maybe_range, tag) :: x2 else x2) (Pset.elements irec.img.by_range)
x2)
in
let included_defs = (let x2 =
([]) in List.fold_right
(fun(maybe_range, def) x2 ->
if range_or_sym_is_in_this_sec maybe_range (SymbolDef (def)) then
def :: x2 else x2)
(elf_memory_image_defined_symbols_and_ranges irec.img) x2)
in
let included_global_defs = (let x2 =
([]) in List.fold_right
(fun def x2 ->
if not
(Nat_big_num.equal
(
get_elf64_symbol_binding def.def_syment)
stb_local) then def :: x2 else x2) included_defs x2)
in
let (new_ranges_and_tags : (( element_range option) * ( any_abi_feature range_tag)) Pset.set)
= (Lem_set.setMapMaybe
(instance_Basic_classes_SetType_tup2_dict
(instance_Basic_classes_SetType_Maybe_maybe_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_var_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_Num_natural_dict
instance_Basic_classes_SetType_Num_natural_dict)))
instance_Basic_classes_SetType_var_dict) (instance_Basic_classes_SetType_tup2_dict
(instance_Basic_classes_SetType_Maybe_maybe_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_var_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_Num_natural_dict
instance_Basic_classes_SetType_Num_natural_dict)))
instance_Basic_classes_SetType_var_dict) (fun (maybe_range, tag) ->
let new_range = ((match maybe_range with
None -> None
| Some(el_name, (start, len)) ->
Some(secname1,
( let new_start_off = (Nat_big_num.add start ( Nat_big_num.sub_nat comp_addr output_section_start_addr))
in
(new_start_off,
len)))
))
in
(match tag with
| FileFeature(ElfSection(idx1, isec1)) -> None
| SymbolDef(def) ->
Some(new_range, SymbolDef({
def_symname = (def.def_symname)
; def_syment = (def.def_syment)
; def_sym_scn = (def.def_sym_scn)
; def_sym_idx = (def.def_sym_idx)
; def_linkable_idx = (irec.idx)
}))
| AbiFeature(x) -> Some(new_range, AbiFeature(x))
| SymbolRef(r) ->
let get_binding_for_ref = (fun symref -> (fun linkable_idx -> (fun fname1 ->
let name_matches1 = ((match Pmap.lookup symref.ref_symname bindings_by_name with Some x -> x | None -> [] ))
in
(match List.filter (fun (bi, ((r_idx, r, r_item), m_d)) -> Nat_big_num.equal r_idx linkable_idx && (r = symref)) name_matches1 with
[(b_idx, b)] -> (b_idx, b)
| [] -> failwith "no binding found"
| _ -> failwith ("ambiguous binding found for symbol `" ^ (symref.ref_symname ^ ("' in file " ^ fname1)))
)
)))
in
let (bi, b) = (get_binding_for_ref r.ref irec.idx irec.fname)
in
let ((ref_idx, ref1, ref_linkable), maybe_def) = b
in
(match r.maybe_reloc with
None -> None
| Some(rs) ->
let (rel_type1, _) = (a.parse_reloc_info rs.ref_relent.elf64_ra_info) in
Some(new_range, SymbolRef(
{ ref = ({
ref_symname = (ref1.ref_symname)
; ref_syment =
({ elf64_st_name = (Uint32_wrapper.of_bigint( (Nat_big_num.of_int 0)))
; elf64_st_info = (ref1.ref_syment.elf64_st_info)
; elf64_st_other = (ref1.ref_syment.elf64_st_other)
; elf64_st_shndx = (Uint32_wrapper.of_bigint( (Nat_big_num.of_int 0)))
; elf64_st_value = (Uint64_wrapper.of_bigint( (Nat_big_num.of_int 0)))
; elf64_st_size = (Uint64_wrapper.of_bigint( (Nat_big_num.of_int 0)))
})
; ref_sym_scn =( (Nat_big_num.of_int 0))
; ref_sym_idx =( (Nat_big_num.of_int 0))
})
; maybe_reloc = (Some {
ref_relent = ({
elf64_ra_offset = (Uint64_wrapper.of_bigint( (Nat_big_num.of_int 0)))
; elf64_ra_info = (Uint64_wrapper.logor
(Uint64_wrapper.of_bigint rel_type1)
(Uint64_wrapper.shift_left
(Uint64_wrapper.of_bigint( (Nat_big_num.of_int 0)))
32
)
)
; elf64_ra_addend = (rs.ref_relent.elf64_ra_addend)
})
; ref_rel_scn =( (Nat_big_num.of_int 0))
; ref_rel_idx =( (Nat_big_num.of_int 0))
; ref_src_scn =( (Nat_big_num.of_int 0))
})
; maybe_def_bound_to =
(
let (possible_bindings : (Nat_big_num.num * binding) list)
= ((match Pmap.lookup ref1.ref_symname bindings_by_name with
Some l -> if ref1.ref_symname = "__fini_array_end" then
l
else l
| None -> []
))
in
(match r.maybe_def_bound_to with
None -> failwith ("at this stage, all references must have a decision: `" ^ (ref1.ref_symname ^ "'"))
| Some(decision, _) ->
let matching_possibles = (List.filter (fun (bi, ((ref_idx, ref1, ref_item), maybe_d)) ->
(match maybe_d with
None -> false
| Some (def_idx, def, def_item) -> Nat_big_num.equal
irec.idx ref_idx
&& (Nat_big_num.equal r.ref.ref_sym_scn ref1.ref_sym_scn
&& Nat_big_num.equal r.ref.ref_sym_idx ref1.ref_sym_idx)
)
) possible_bindings)
in
let new_bound_to = ((match matching_possibles with
[] -> Some(ApplyReloc, None)
| [(bi, ((rl, r, ri), maybe_d))] ->
Some(decision,
(match maybe_d with
Some (def_idx, def, def_item) -> Some {
def_symname = (def.def_symname)
; def_syment = (def.def_syment)
; def_sym_scn = (def.def_sym_scn)
; def_sym_idx = (def.def_sym_idx)
; def_linkable_idx = def_idx
}
| None -> None
))
| _ -> failwith ("After linker script, ambiguous bindings for `" ^ (ref1.ref_symname ^ "'"))
))
in
if not ((Lem.option_equal (Lem.pair_equal (=) (Lem.option_equal (=))) new_bound_to r.maybe_def_bound_to)) then
new_bound_to
else if (Lem.option_equal (Lem.pair_equal (=) (Lem.option_equal (=))) new_bound_to None) then failwith "really need a decision by now"
else new_bound_to
))
}
))
)
)
) ((Pset.from_list (pairCompare (maybeCompare (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare))) compare) ranges_and_tags)))
in
let isec_sz = (irec.isec.elf64_section_size) in
let maybe_el_sz = (el.length1) in
let contents_sz = (length el.contents) in
let (actual_sz, padded_contents) =
((match maybe_el_sz with
Some el_sz ->
let diff = (Nat_big_num.sub_nat el_sz contents_sz) in
if Nat_big_num.less diff( (Nat_big_num.of_int 0)) then
(el_sz, take0 el_sz el.contents)
else (el_sz, List.rev_append (List.rev el.contents) (replicate0 diff None))
| None ->
if not (Nat_big_num.equal (length el.contents) isec_sz)
then failwith "input section size not equal to its content pattern length"
else (isec_sz, el.contents)
))
in
(actual_sz, padded_contents, new_ranges_and_tags)
| _ -> failwith "impossible: no such element"
)
)
| IncludeCommonSymbol(retain_pol, fname1, linkable_idx, def, img2) ->
let sz = (Ml_bindings.nat_big_num_of_uint64 def.def_syment.elf64_st_size)
in
let content = (Missing_pervasives.replicate0 sz (Some(Char.chr (Nat_big_num.to_int ( (Nat_big_num.of_int 0))))))
in
(sz, content,(Pset.from_list (pairCompare (maybeCompare (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare))) compare) [(Some(secname1, ( Nat_big_num.sub_nat comp_addr output_section_start_addr, sz)), SymbolDef({
def_symname = (def.def_symname)
; def_syment = (def.def_syment)
; def_sym_scn = (def.def_sym_scn)
; def_sym_idx = (def.def_sym_idx)
; def_linkable_idx = linkable_idx
}))]))
| ProvideSymbol(pol, name1, (size2, info, other)) ->
let symaddr = accum_current_addr
in
( (Nat_big_num.of_int 0), [],(Pset.from_list (pairCompare (maybeCompare (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare))) compare) [(
Some(secname1, (( Nat_big_num.sub_nat symaddr output_section_start_addr), (Nat_big_num.of_int 0))),
SymbolDef(symbol_def_for_provide_symbol name1 size2 info other control_script_linkable_idx)
)])
)
))
in
let new_content = (append_to_byte_pattern_at_offset ( Nat_big_num.sub_nat comp_addr output_section_start_addr) accum_pat comp_el_pat)
in
let new_addr = (Nat_big_num.add comp_addr sz)
in
let new_meta = (Pset.(union) accum_meta this_el_meta)
in
(new_content, new_addr, new_meta)
)) ([], output_section_start_addr,(Pset.from_list (pairCompare (maybeCompare (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare))) compare) [])) (list_combine comp comp_addrs))
in
let concat_sec_el = ({
Memory_image.startpos = (Some(output_section_start_addr))
; Memory_image.length1 = (Some(size2))
; Memory_image.contents = concatenated_content
})
in
let new_by_range_list =
((Some(secname1, ( (Nat_big_num.of_int 0), size2)), FileFeature(ElfSection( (Nat_big_num.of_int 0),
{ elf64_section_name =( (Nat_big_num.of_int 0))
; elf64_section_type = (output_section_type comp)
; elf64_section_flags = (output_section_flags comp)
; elf64_section_addr =( (Nat_big_num.of_int 0))
; elf64_section_offset =( (Nat_big_num.of_int 0))
; elf64_section_size =( (Nat_big_num.of_int 0))
; elf64_section_link =( (Nat_big_num.of_int 0))
; elf64_section_info =( (Nat_big_num.of_int 0))
; elf64_section_align = (alignof_output_section comp)
; elf64_section_entsize =( (Nat_big_num.of_int 0))
; elf64_section_body = Byte_sequence.empty
; elf64_section_name_as_string = secname1
}
))) :: Pset.elements new_range_tag_pairs)
in
let new_by_range = (List.fold_left (fun m -> fun (maybe_range, tag) ->
let new_s = (Pset.add (maybe_range, tag) m)
in
new_s
) acc_img.by_range new_by_range_list)
in
let new_by_tag = (by_tag_from_by_range
(instance_Basic_classes_SetType_Maybe_maybe_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_var_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_Num_natural_dict
instance_Basic_classes_SetType_Num_natural_dict))) instance_Basic_classes_SetType_var_dict new_by_range)
in
let _ =
(let section_tags_bare = (List.filter (fun (maybe_range, tag) ->
(match tag with
| FileFeature(ElfSection(idx1, isec1)) -> true
| _ -> false
)) (Pset.elements new_by_range))
in
())
in
( Nat_big_num.add
output_section_start_addr size2,
{
elements = (Pmap.add secname1 concat_sec_el acc_img.elements)
; by_range = new_by_range
; by_tag = new_by_tag
},
size2,
(OutputSectionSpec (guard, Some(output_section_start_addr), secname1, comp))
)
))
in
(match script1 with
[] -> (acc, (AllocatedSectionsMap outputs_by_name))
| (element1, el_idx) :: more_elements_and_idx ->
let do_nothing = (acc, pos, (AllocatedSectionsMap outputs_by_name)) in
let (new_acc, new_pos, new_outputs_by_name) =
((match element1 with
DefineSymbol(symdefpol, name1, (symsize, syminfo, symother)) ->
do_nothing
| AdvanceAddress(AddressExprFn advance_fn_ref) ->
let advance_fn =
((match Pmap.lookup advance_fn_ref alloc_map with
| Some m -> m
| None -> failwith "alloc_map invariant failure"
))
in
let new_pos = (advance_fn pos (AllocatedSectionsMap outputs_by_name))
in
(acc, new_pos, (AllocatedSectionsMap outputs_by_name))
| MarkAndAlignDataSegment(maxpagesize1, commonpagesize1) ->
let option1 = (Nat_big_num.add (align_up_to maxpagesize1 pos) (Nat_big_num.bitwise_and pos ( Nat_big_num.sub_nat maxpagesize1( (Nat_big_num.of_int 1)))))
in
let option2 = (Nat_big_num.add (align_up_to maxpagesize1 pos) (Nat_big_num.bitwise_and ( Nat_big_num.sub_nat (Nat_big_num.add pos commonpagesize1)( (Nat_big_num.of_int 1))) ( Nat_big_num.sub_nat maxpagesize1 commonpagesize1)))
in
let data_segment_endpos = (fun startpos1 ->
let (endpos, _) = (List.fold_left (fun (curpos, seen_end) -> fun (new_script_item, new_script_item_idx) ->
if seen_end
then (curpos, true)
else let (newpos, new_seen) = ((match new_script_item with
| MarkDataSegmentEnd ->
(curpos, true)
| OutputSection(outputguard, maybe_expr, name1, sub_elements) ->
let maybe_found = (Pmap.lookup name1 outputs_by_name)
in
let (OutputSectionSpec (guard, addr, secname1, comp), seen_script_el_idx) = ((match maybe_found with
Some (f, seen_script_el_idx) -> (f, seen_script_el_idx)
| None -> failwith "internal error: output section not found"
))
in
let replay_output = ( Nat_big_num.equal seen_script_el_idx el_idx)
in
if replay_output
then (
let unaligned_start_addr = curpos
in
let start_addr = (align_up_to (alignof_output_section comp) unaligned_start_addr)
in
let (end_addr, comp_addrs) = (do_output_section_layout_starting_at_addr start_addr (AllocatedSectionsMap outputs_by_name) comp)
in
let size2 = (Nat_big_num.sub_nat end_addr start_addr)
in
(end_addr, false)
)
else (curpos, false)
| AdvanceAddress(AddressExprFn advance_fn_ref) ->
let advance_fn =
((match Pmap.lookup advance_fn_ref alloc_map with
| Some m -> m
| None -> failwith "alloc_map invariant failed"
))
in
let new_pos = (advance_fn curpos (AllocatedSectionsMap outputs_by_name))
in
(new_pos, false)
| _ -> (curpos, seen_end)
))
in
if Nat_big_num.less newpos curpos then failwith "went backwards" else (newpos, new_seen)
) (startpos1, false) more_elements_and_idx)
in endpos
)
in
let endpos_option1 = (data_segment_endpos option1)
in
let endpos_option2 = (data_segment_endpos option2)
in
let npages = (fun startpos1 -> (fun endpos -> Nat_big_num.div
( Nat_big_num.sub_nat(align_up_to commonpagesize1 endpos)
(round_down_to commonpagesize1 startpos1)) commonpagesize1
))
in
let npages_option1 = (npages option1 endpos_option1)
in
let npages_option2 = (npages option2 endpos_option1)
in
if Nat_big_num.less npages_option1 npages_option2
then (acc, option1, (AllocatedSectionsMap outputs_by_name))
else (acc, option2, (AllocatedSectionsMap outputs_by_name))
| MarkDataSegmentEnd -> do_nothing
| MarkDataSegmentRelroEnd -> do_nothing
| OutputSection(outputguard, maybe_expr, name1, sub_elements) ->
let maybe_found = (Pmap.lookup name1 outputs_by_name)
in
let (found, seen_script_el_idx) = ((match maybe_found with
Some (f, saved_idx) -> (f, saved_idx)
| None -> failwith "internal error: output section not found"
))
in
let (OutputSectionSpec (guard, addr, secname1, comp)) = found
in
let count_sections_in_image = (fun img2 -> (
let (section_tags, section_ranges) = (elf_memory_image_section_ranges img2)
in
let section_tags_bare = (Lem_list.map (fun tag ->
(match tag with
| FileFeature(ElfSection(idx1, isec1)) -> true
| _ -> false
)) section_tags)
in
length section_tags_bare
))
in
let comp_element_allocates_space = (fun comp_el -> (match comp_el with
IncludeInputSection(_, irec) -> Nat_big_num.greater
irec.isec.elf64_section_size( (Nat_big_num.of_int 0))
| IncludeCommonSymbol(retain_pol, fname1, idx1, def, img2) -> Nat_big_num.greater
(Ml_bindings.nat_big_num_of_uint64 def.def_syment.elf64_st_size)( (Nat_big_num.of_int 0))
| ProvideSymbol(pol, name1, spec) -> true
| Hole(AddressExprFn(address_fn_ref)) ->
let address_fn =
((match Pmap.lookup address_fn_ref alloc_map with
| Some m -> m
| None -> failwith "alloc_map invariant failed"
))
in
let assignment_is_excluded = (fun f ->
let always_gives_0 =
( Nat_big_num.equal(f( (Nat_big_num.of_int 0)) (AllocatedSectionsMap outputs_by_name))( (Nat_big_num.of_int 0))
&& Nat_big_num.equal (f( (Nat_big_num.of_int 42)) (AllocatedSectionsMap outputs_by_name))( (Nat_big_num.of_int 0)))
in
let always_gives_dot =
( Nat_big_num.equal(f( (Nat_big_num.of_int 0)) (AllocatedSectionsMap outputs_by_name))( (Nat_big_num.of_int 0))
&& Nat_big_num.equal (f( (Nat_big_num.of_int 42)) (AllocatedSectionsMap outputs_by_name))( (Nat_big_num.of_int 42)))
in
always_gives_0 || (always_gives_dot ))
in
not (assignment_is_excluded address_fn)
))
in
let section_contains_non_empty_inputs =
(List.exists comp_element_allocates_space comp)
in
let do_output = (( Nat_big_num.equal seen_script_el_idx el_idx) && section_contains_non_empty_inputs)
in
if not do_output then
(acc, pos, (AllocatedSectionsMap outputs_by_name))
else (
let (new_pos, new_acc, sec_sz, replacement_output_sec)
= (add_output_section ( pos, acc) found)
in
(new_acc, new_pos, (AllocatedSectionsMap (Pmap.add name1 (replacement_output_sec, el_idx) (Pmap.remove name1 outputs_by_name))))
)
| DiscardInput(selector) -> do_nothing
| InputQuery(retainpol, sortpol, selector) -> do_nothing
))
in
build_image a alloc_map new_acc new_pos new_outputs_by_name bindings_by_name more_elements_and_idx control_script_linkable_idx linker_defs_by_name
))
let default_place_orphans (discards, outputs) inputs:(input_spec)list*(output_section_spec*Nat_big_num.num)list=
(
let output_irecs = (List.fold_left (fun acc -> fun outp -> ((match outp with
(OutputSectionSpec(guard, maybe_addr, name1, comp), script_el_idx) ->
let all_irecs = (List.fold_left (fun inner_acc -> fun comp_el -> (match comp_el with
IncludeInputSection(_, irec) -> Pset.add irec inner_acc
| _ -> inner_acc
))(Pset.from_list compare []) comp)
in
Pset.(union) all_irecs acc
| _ -> acc
)))(Pset.from_list compare []) outputs)
in
let (orphans : input_spec list) = (List.filter (fun inp -> (match inp with
InputSection(irec) -> let v = (not ( Pset.mem irec output_irecs))
in v
| _ -> false
)) inputs)
in
let place_one_orphan = (fun acc -> fun input -> (
let irec = ((match input with
InputSection(irec) -> irec
| _ -> failwith "impossible: orphan section is not a section"
))
in
let (discards, outputs) = acc in
let find_output = (fun maybe_name -> fun maybe_type -> fun flags_must_have -> fun flags_must_not_have -> (
Missing_pervasives.find_index0 (fun (OutputSectionSpec (guard, maybe_addr, name1, comp), script_el_idx) ->
let flags = (output_section_flags comp) in
(match maybe_name with Some n -> n = name1 | None -> true )
&& ((match maybe_type with Some t -> Nat_big_num.equal (output_section_type comp) t | None -> true )
&& (Pset.for_all (fun x -> flag_is_set x flags) flags_must_have
&& Pset.for_all (fun x -> not (flag_is_set x flags)) flags_must_not_have))
) outputs
))
in
let place_after_nonalloc = (find_output None None(Pset.from_list Nat_big_num.compare [])(Pset.from_list Nat_big_num.compare [ shf_alloc ])) in
let place_after_interp = (find_output (Some(".interp")) (Some(sht_progbits))(Pset.from_list Nat_big_num.compare [ shf_alloc ])(Pset.from_list Nat_big_num.compare [])) in
let place_after_bss = (find_output (Some(".bss")) (Some(sht_nobits))(Pset.from_list Nat_big_num.compare [ shf_alloc; shf_write])(Pset.from_list Nat_big_num.compare [])) in
let place_after_rodata = (find_output (Some(".rodata")) (Some(sht_progbits))(Pset.from_list Nat_big_num.compare [ shf_alloc ])(Pset.from_list Nat_big_num.compare [ shf_write ])) in
let place_after_rel = (find_output (Some(".rela.dyn")) (Some(sht_rela))(Pset.from_list Nat_big_num.compare [])(Pset.from_list Nat_big_num.compare [])) in
let place_after_data = (find_output (Some(".data")) (Some(sht_progbits))(Pset.from_list Nat_big_num.compare [ shf_alloc; shf_write ])(Pset.from_list Nat_big_num.compare [])) in
let place_after_text = (find_output (Some(".text")) (Some(sht_progbits))(Pset.from_list Nat_big_num.compare [ shf_alloc; shf_execinstr ])(Pset.from_list Nat_big_num.compare [])) in
let (place_after : Nat_big_num.num option) = ((match input with
InputSection(irec) ->
if irec.isec.elf64_section_name_as_string = ".note.GNU-stack" then None
else
if not (flag_is_set shf_alloc irec.isec.elf64_section_flags)
&& true
then place_after_nonalloc
else
if Nat_big_num.equal irec.isec.elf64_section_type sht_note
|| (irec.isec.elf64_section_name_as_string = ".note")
then place_after_interp
else if Nat_big_num.equal irec.isec.elf64_section_type sht_nobits
then place_after_bss
else
if not (flag_is_set shf_write irec.isec.elf64_section_flags)
&& not (flag_is_set shf_execinstr irec.isec.elf64_section_flags)
then place_after_rodata
else if flag_is_set shf_write irec.isec.elf64_section_flags
&& not (flag_is_set shf_execinstr irec.isec.elf64_section_flags)
then place_after_data
else place_after_text
))
in
let (discards, outputs) = acc in
(match place_after with
Some idx1 ->
(discards, mapi (fun i -> fun output ->
let (OutputSectionSpec (guard, maybe_addr, name1, comp), script_el_idx) = output in
if Nat_big_num.equal (Nat_big_num.of_int i) idx1 then (OutputSectionSpec(guard, maybe_addr, name1, List.rev_append (List.rev comp) [IncludeInputSection(DefaultKeep, irec)]), script_el_idx) else output
) outputs
)
| None ->
( List.rev_append (List.rev discards) [input], outputs)
)
))
in
List.fold_left place_one_orphan (discards, outputs) orphans)
let interpret_linker_control_script alloc_map script1 linkables control_script_linkable_idx a inputs seen_ordering place_orphans initial_bindings_by_name:(any_abi_feature)annotated_memory_image*((string),((Nat_big_num.num*binding)list))Pmap.map=
(let labelled_script = (label_script script1)
in
let (discards_before_orphans, outputs_before_orphans)
= (assign_inputs_to_output_sections ([], [])(Pset.from_list (pairCompare Nat_big_num.compare Nat_big_num.compare) [])(Pset.from_list (tripleCompare Nat_big_num.compare Nat_big_num.compare Nat_big_num.compare) []) inputs None None seen_ordering labelled_script)
in
let (discards, outputs) = (place_orphans (discards_before_orphans, outputs_before_orphans) inputs)
in
let (linker_defs_by_name, (bindings_by_name : ( (string, ( (Nat_big_num.num * binding)list))Pmap.map))) = (
let (script_defs_by_name : (string, ( (symbol_definition * symbol_def_policy)list)) Pmap.map)
= (List.fold_left (fun acc -> (fun ((OutputSectionSpec (guard, maybe_addr, secname1, comp)), script_el_idx) ->
List.fold_left (fun inner_acc -> fun comp_el -> (
(match comp_el with
ProvideSymbol(pol, name1, (size2, info, other)) ->
let def = (symbol_def_for_provide_symbol name1 size2 info other control_script_linkable_idx)
in
let v = ((match Pmap.lookup name1 inner_acc with
None -> [(def, pol)]
| Some l -> (def, pol) :: l
))
in
Pmap.add name1 v inner_acc
| _ -> inner_acc
)
)) (acc : (string, ( (symbol_definition * symbol_def_policy)list)) Pmap.map) comp
)) (Pmap.empty compare) outputs)
in
let idx_to_img = (List.fold_left (fun acc_m -> fun item ->
(match item with
Common(idx1, _, img2, symdef) -> Pmap.add idx1 img2 (Pmap.remove idx1 acc_m)
| InputSection(irec) -> Pmap.add irec.idx irec.img (Pmap.remove irec.idx acc_m)
)
) (Pmap.empty Nat_big_num.compare) inputs)
in
let (lowest_idx : Nat_big_num.num) = ((match Pset.min_elt_opt (Pmap.domain idx_to_img)
with Some x -> x
| None -> failwith "internal error: no linkable items"
))
in
let first_linkable_item = ((match linkables with x :: more -> x | _ -> failwith "internal error: no linkables" ))
in
let (control_script_input_item : input_item) = (
"(built-in control script)",
ControlScript,
(BuiltinControlScript, [Builtin])
)
in
let (control_script_linkable_item : linkable_item) = (
ControlScriptDefs, control_script_input_item,
{ item_fmt = ""
; item_check_sections = false
; item_copy_dt_needed = false
; item_force_output = true
}
)
in
let updated_bindings_and_new_defs = (Pmap.map (fun b_list_initial ->
Lem_list.map (fun (b_idx, b_initial) ->
let ((iref_idx, iref, iref_item), maybe_idef) = b_initial
in
let possible_script_defs = ((match Pmap.lookup iref.ref_symname script_defs_by_name with
Some l -> l
| None -> []
))
in
let (possible_linker_generated_def : symbol_definition option) =
(if a.symbol_is_generated_by_linker iref.ref_symname
then
((match Pmap.lookup lowest_idx idx_to_img with
None -> failwith "no lowest idx found"
| Some img2 ->
(match List.filter (fun def -> def.def_symname = iref.ref_symname) (defined_symbols
instance_Basic_classes_Ord_Abis_any_abi_feature_dict instance_Abi_classes_AbiFeatureTagEquiv_Abis_any_abi_feature_dict img2) with
[] -> None
| [def] -> Some(def)
| _ -> failwith ("first linkable has multiple defs of name `" ^ (iref.ref_symname ^ "'"))
)
))
else None)
in
let new_b_and_maybe_new_def = ((match (maybe_idef, possible_script_defs, possible_linker_generated_def) with
| (_, [], None) ->
(((iref_idx, iref, iref_item), maybe_idef), None)
| (None, [], Some(def)) ->
(((iref_idx, iref, iref_item), Some(lowest_idx, def, first_linkable_item)), Some(def))
| (_, [(def, AlwaysDefine)], _) ->
(((iref_idx, iref, iref_item), Some (control_script_linkable_idx, def, control_script_linkable_item)), Some(def))
| (Some existing_def, ([(def, ProvideIfUsed)]), _) ->
(((iref_idx, iref, iref_item), Some existing_def), None)
| (None, [(def, ProvideIfUsed)], _) ->
(((iref_idx, iref, iref_item), Some (control_script_linkable_idx, def, control_script_linkable_item)), Some(def))
| (_, pair1 :: pair2 :: more, _) ->
failwith "ambiguous symbol binding in linker control script"
))
in
(b_idx, new_b_and_maybe_new_def)
) b_list_initial
) initial_bindings_by_name)
in
let (new_symbol_defs_map : (string, ( ( symbol_definition option)list)) Pmap.map)
= (Pmap.map (fun b_pair_list -> Lem_list.map (fun (b_idx, (new_b, maybe_new_def)) -> maybe_new_def) b_pair_list) updated_bindings_and_new_defs)
in
let (new_symbol_defs_by_name : (string, ( symbol_definition list)) Pmap.map) = (Pmap.map
(fun v -> Lem_list.mapMaybe id0 v) new_symbol_defs_map)
in
let updated_bindings = (Pmap.map (fun b_pair_list -> Lem_list.map (fun (b_idx, (new_b, maybe_new_def)) -> (b_idx, new_b)) b_pair_list) updated_bindings_and_new_defs)
in
(new_symbol_defs_by_name, updated_bindings)
)
in
let outputs_by_name =
(let insert_fun = (fun m -> (fun (OutputSectionSpec(guard, maybe_addr, name1, compos), script_idx) -> Pmap.add name1 ((OutputSectionSpec (guard, maybe_addr, name1, compos)), script_idx) m))
in
List.fold_left insert_fun (Pmap.empty compare) outputs)
in
let discard_line = (fun i -> ((match i with
InputSection(s) ->
let lpadded_secname = (" " ^ s.secname)
in
lpadded_secname ^ ((space_padding_and_maybe_newline( (Nat_big_num.of_int 16)) lpadded_secname) ^ ("0x0000000000000000"
^ (" 0x" ^ ((hex_string_of_natural s.isec.elf64_section_size) ^ (" "
^ (s.fname ^ "\n"))))))
| Common(idx1, fname1, img2, def) -> ""
)))
in
let outputs_by_name_after_gc = (compute_def_use_and_gc (AllocatedSectionsMap outputs_by_name))
in
let (img2, outputs_by_name_with_position)
= (build_image a alloc_map empty_elf_memory_image( (Nat_big_num.of_int 0)) outputs_by_name_after_gc bindings_by_name labelled_script control_script_linkable_idx linker_defs_by_name)
in
(img2, bindings_by_name))