aboutsummaryrefslogtreecommitdiff
path: root/gnu/usr.bin/ld/ld.c
blob: a22d2564a1b270a42ccca821ff013bee14e034ec (plain) (blame)
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
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
/*-
 * This code is derived from software copyrighted by the Free Software
 * Foundation.
 *
 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
 *
 * Modified 1993 by Paul Kranenburg, Erasmus University
 */

#ifndef lint
static char sccsid[] = "@(#)ld.c	6.10 (Berkeley) 5/22/91";
#endif /* not lint */

/* Linker `ld' for GNU
   Copyright (C) 1988 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 1, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Written by Richard Stallman with some help from Eric Albert.
   Set, indirect, and warning symbol features added by Randy Smith.

   NOTE: Set and indirect symbols are no longer supported by this
   version. (pk) */

/*
 *	$Id: ld.c,v 1.10 1993/11/01 16:26:13 pk Exp $
 */
   
/* Define how to initialize system-dependent header fields.  */

#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <ar.h>
#include <ranlib.h>
#include <a.out.h>
#include <stab.h>
#include <string.h>
#include <strings.h>

#include "ld.h"

int	building_shared_object;

/* 1 => write relocation into output file so can re-input it later.  */
int	relocatable_output;

/* Non zero means to create the output executable. */
/* Cleared by nonfatal errors.  */
int	make_executable;

/* Force the executable to be output, even if there are non-fatal errors */
int	force_executable;

/* 1 => assign space to common symbols even if `relocatable_output'.  */
int	force_common_definition;
 
/* 1 => assign jmp slots to text symbols in shared objects even if non-PIC */
int	force_alias_definition;

/*
 * Which symbols should be stripped (omitted from the output): none, all, or
 * debugger symbols.
 */
enum {
	STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER
} strip_symbols;

/*
 * Which local symbols should be omitted: none, all, or those starting with L.
 * This is irrelevant if STRIP_NONE.
 */
enum {
	DISCARD_NONE, DISCARD_ALL, DISCARD_L
} discard_locals;

/* Nonzero means print names of input files as processed.  */
int	trace_files;

/* Magic number to use for the output file, set by switch.  */
int	magic;

/*
 * `text-start' address is normally this much plus a page boundary.
 * This is not a user option; it is fixed for each system.
 */
int	text_start_alignment;

/*
 * Nonzero if -T was specified in the command line.
 * This prevents text_start from being set later to default values.
 */
int	T_flag_specified;
 
/*
 * Nonzero if -Tdata was specified in the command line.
 * This prevents data_start from being set later to default values.
 */
int	Tdata_flag_specified;

/*
 * Size to pad data section up to.
 * We simply increase the size of the data section, padding with zeros,
 * and reduce the size of the bss section to match.
 */
int	specified_data_size;

long	*set_vectors;
int	setv_fill_count;

int
main(argc, argv)
	char          **argv;
	int             argc;
{

	if ((progname = strrchr(argv[0], '/')) == NULL)
		progname = argv[0];
	else
		progname++;

	/* Added this to stop ld core-dumping on very large .o files.    */
#ifdef RLIMIT_STACK
	/* Get rid of any avoidable limit on stack size.  */
	{
		struct rlimit   rlim;

		/* Set the stack limit huge so that alloca does not fail. */
		getrlimit(RLIMIT_STACK, &rlim);
		rlim.rlim_cur = rlim.rlim_max;
		setrlimit(RLIMIT_STACK, &rlim);
	}
#endif	/* RLIMIT_STACK */

	page_size = PAGSIZ;

	/* Clear the cumulative info on the output file.  */

	text_size = 0;
	data_size = 0;
	bss_size = 0;
	text_reloc_size = 0;
	data_reloc_size = 0;

	data_pad = 0;
	text_pad = 0;

	/* Initialize the data about options.  */

	specified_data_size = 0;
	strip_symbols = STRIP_NONE;
	trace_files = 0;
	discard_locals = DISCARD_NONE;
	entry_symbol = 0;
	write_map = 0;
	relocatable_output = 0;
	force_common_definition = 0;
	T_flag_specified = 0;
	Tdata_flag_specified = 0;
	magic = DEFAULT_MAGIC;
	make_executable = 1;
	force_executable = 0;
	link_mode = DYNAMIC;
	soversion = LD_VERSION_BSD;

	/* Initialize the cumulative counts of symbols.  */

	local_sym_count = 0;
	non_L_local_sym_count = 0;
	debugger_sym_count = 0;
	undefined_global_sym_count = 0;
	warning_count = 0;
	multiple_def_count = 0;
	common_defined_global_count = 0;

	/* Keep a list of symbols referenced from the command line */
	cl_refs_allocated = 10;
	cmdline_references
		= (struct glosym **) xmalloc(cl_refs_allocated
					     * sizeof(struct glosym *));
	*cmdline_references = 0;

	/* Completely decode ARGV.  */
	decode_command(argc, argv);

	building_shared_object =
		(!relocatable_output && (link_mode & SHAREABLE));

	/* Create the symbols `etext', `edata' and `end'.  */
	symtab_init(relocatable_output);

	/* Prepare for the run-time linking support. */
	init_rrs();

	/*
	 * Determine whether to count the header as part of the text size,
	 * and initialize the text size accordingly. This depends on the kind
	 * of system and on the output format selected.
	 */

	md_init_header(&outheader, magic, 0);

	text_size = sizeof(struct exec);
	text_size -= N_TXTOFF(outheader);

	if (text_size < 0)
		text_size = 0;
	entry_offset = text_size;

	if (!T_flag_specified && !relocatable_output)
		text_start = TEXT_START(outheader);

	/* The text-start address is normally this far past a page boundary.  */
	text_start_alignment = text_start % page_size;

	/*
	 * Load symbols of all input files. Also search all libraries and
	 * decide which library members to load.
	 */
	load_symbols();

	/* Compute where each file's sections go, and relocate symbols.  */
	digest_symbols();

	/*
	 * Print error messages for any missing symbols, for any warning
	 * symbols, and possibly multiple definitions
	 */
	make_executable = do_warnings(stderr);

	/* Print a map, if requested.  */
	if (write_map)
		print_symbols(stdout);

	/* Write the output file.  */
	if (make_executable || force_executable)
		write_output();

	exit(!make_executable);
}

void            decode_option();

/*
 * Analyze a command line argument. Return 0 if the argument is a filename.
 * Return 1 if the argument is a option complete in itself. Return 2 if the
 * argument is a option which uses an argument.
 * 
 * Thus, the value is the number of consecutive arguments that are part of
 * options.
 */

int
classify_arg(arg)
	register char  *arg;
{
	if (*arg != '-')
		return 0;
	switch (arg[1]) {
	case 'a':
		if (!strcmp(&arg[2], "ssert"))
			return 2;
	case 'A':
	case 'D':
	case 'e':
	case 'L':
	case 'l':
	case 'o':
	case 'u':
	case 'V':
	case 'y':
		if (arg[2])
			return 1;
		return 2;

	case 'B':
		if (!strcmp(&arg[2], "static"))
			return 1;
		if (!strcmp(&arg[2], "dynamic"))
			return 1;

	case 'T':
		if (arg[2] == 0)
			return 2;
		if (!strcmp(&arg[2], "text"))
			return 2;
		if (!strcmp(&arg[2], "data"))
			return 2;
		return 1;
	}

	return 1;
}

/*
 * Process the command arguments, setting up file_table with an entry for
 * each input file, and setting variables according to the options.
 */

void
decode_command(argc, argv)
	char          **argv;
	int             argc;
{
	register int    i;
	register struct file_entry *p;
	char           *cp;

	number_of_files = 0;
	output_filename = "a.out";

	/*
	 * First compute number_of_files so we know how long to make
	 * file_table.
	 * Also process most options completely.
	 */

	for (i = 1; i < argc; i++) {
		register int    code = classify_arg(argv[i]);
		if (code) {
			if (i + code > argc)
				fatal("no argument following %s\n", argv[i]);

			decode_option(argv[i], argv[i + 1]);

			if (argv[i][1] == 'l' || argv[i][1] == 'A')
				number_of_files++;

			i += code - 1;
		} else
			number_of_files++;
	}

	if (!number_of_files)
		fatal("no input files");

	p = file_table = (struct file_entry *)
			xmalloc(number_of_files * sizeof(struct file_entry));
	bzero(p, number_of_files * sizeof(struct file_entry));

	/* Now scan again and fill in file_table.  */
	/* All options except -A and -l are ignored here.  */

	for (i = 1; i < argc; i++) {
		char           *string;
		register int    code = classify_arg(argv[i]);

		if (code == 0) {
			p->filename = argv[i];
			p->local_sym_name = argv[i];
			p++;
			continue;
		}
		if (code == 2)
			string = argv[i + 1];
		else
			string = &argv[i][2];

		if (argv[i][1] == 'B') {
			if (strcmp(string, "static") == 0)
				link_mode &= ~DYNAMIC;
			else if (strcmp(string, "dynamic") == 0)
				link_mode |= DYNAMIC;
			else if (strcmp(string, "symbolic") == 0)
				link_mode |= SYMBOLIC;
			else if (strcmp(string, "forcearchive") == 0)
				link_mode |= FORCEARCHIVE;
			else if (strcmp(string, "shareable") == 0)
				link_mode |= SHAREABLE;
		}
		if (argv[i][1] == 'A') {
			if (p != file_table)
				fatal("-A specified before an input file other than the first");
			p->filename = string;
			p->local_sym_name = string;
			p->just_syms_flag = 1;
			p++;
		}
		if (argv[i][1] == 'l') {
			p->filename = string;
			p->local_sym_name = concat("-l", string, "");
			p->search_dirs_flag = 1;
			if (link_mode & DYNAMIC && !relocatable_output)
				p->search_dynamic_flag = 1;
			p++;
		}
		i += code - 1;
	}

	/* Now check some option settings for consistency.  */

	if ((magic != OMAGIC)
	    && (text_start - text_start_alignment) & (page_size - 1))
		fatal("-T argument not multiple of page size, with sharable output");

	/* Append the standard search directories to the user-specified ones. */
	std_search_dirs(getenv("LD_LIBRARY_PATH"));
}

void
add_cmdline_ref(sp)
	struct glosym  *sp;
{
	struct glosym **ptr;

	for (ptr = cmdline_references;
	     ptr < cmdline_references + cl_refs_allocated && *ptr;
	     ptr++);

	if (ptr >= cmdline_references + cl_refs_allocated - 1) {
		int diff = ptr - cmdline_references;

		cl_refs_allocated *= 2;
		cmdline_references = (struct glosym **)
			xrealloc(cmdline_references,
			       cl_refs_allocated * sizeof(struct glosym *));
		ptr = cmdline_references + diff;
	}
	*ptr++ = sp;
	*ptr = (struct glosym *) 0;
}

int
set_element_prefixed_p(name)
	char           *name;
{
	struct string_list_element *p;
	int             i;

	for (p = set_element_prefixes; p; p = p->next) {

		for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++);
		if (p->str[i] == '\0')
			return 1;
	}
	return 0;
}

/*
 * Record an option and arrange to act on it later. ARG should be the
 * following command argument, which may or may not be used by this option.
 * 
 * The `l' and `A' options are ignored here since they actually specify input
 * files.
 */

void
decode_option(swt, arg)
	register char  *swt, *arg;
{
	/* We get Bstatic from gcc on suns.  */
	if (!strcmp(swt + 1, "Bstatic"))
		return;
	if (!strcmp(swt + 1, "Bdynamic"))
		return;
	if (!strcmp(swt + 1, "Bsymbolic"))
		return;
	if (!strcmp(swt + 1, "Bforcearchive"))
		return;
	if (!strcmp(swt + 1, "Bshareable"))
		return;
	if (!strcmp(swt + 1, "assert"))
		return;
	if (!strcmp(swt + 1, "Ttext")) {
		text_start = parse(arg, "%x", "invalid argument to -Ttext");
		T_flag_specified = 1;
		return;
	}
	if (!strcmp(swt + 1, "Tdata")) {
		rrs_data_start = parse(arg, "%x", "invalid argument to -Tdata");
		Tdata_flag_specified = 1;
		return;
	}
	if (!strcmp(swt + 1, "noinhibit-exec")) {
		force_executable = 1;
		return;
	}
	if (swt[2] != 0)
		arg = &swt[2];

	switch (swt[1]) {
	case 'A':
		return;

	case 'D':
		specified_data_size = parse(arg, "%x", "invalid argument to -D");
		return;

	case 'd':
		if (*arg == 'c')
			force_common_definition = 1;
		else if (*arg == 'p')
			force_alias_definition = 1;
		else
			fatal("-d option takes 'c' or 'p' argument");
		return;

	case 'e':
		entry_symbol = getsym(arg);
		if (!entry_symbol->defined && !entry_symbol->referenced)
			undefined_global_sym_count++;
		entry_symbol->referenced = 1;
		add_cmdline_ref(entry_symbol);
		return;

	case 'l':
		return;

	case 'L':
		add_search_dir(arg);
		return;

	case 'M':
		write_map = 1;
		return;

	case 'N':
		magic = OMAGIC;
		return;

#ifdef NMAGIC
	case 'n':
		magic = NMAGIC;
		return;
#endif

#ifdef QMAGIC
	case 'Q':
		magic = oldmagic = QMAGIC;
		return;
	case 'Z':
		magic = oldmagic = ZMAGIC;
		return;
#endif

	case 'o':
		output_filename = arg;
		return;

	case 'r':
		relocatable_output = 1;
		magic = OMAGIC;
		text_start = 0;
		return;

	case 'S':
		strip_symbols = STRIP_DEBUGGER;
		return;

	case 's':
		strip_symbols = STRIP_ALL;
		return;

	case 'T':
		text_start = parse(arg, "%x", "invalid argument to -T");
		T_flag_specified = 1;
		return;

	case 't':
		trace_files = 1;
		return;

	case 'u':
		{
			register symbol *sp = getsym(arg);

			if (!sp->defined && !sp->referenced)
				undefined_global_sym_count++;
			sp->referenced = 1;
			add_cmdline_ref(sp);
		}
		return;

#if 1
	case 'V':
		soversion = parse(arg, "%d", "invalid argument to -V");
		return;
#endif

	case 'X':
		discard_locals = DISCARD_L;
		return;

	case 'x':
		discard_locals = DISCARD_ALL;
		return;

	case 'y':
		{
			register symbol *sp = getsym(&swt[2]);
			sp->trace = 1;
		}
		return;

	case 'z':
		magic = ZMAGIC;
		return;

	default:
		fatal("invalid command option `%s'", swt);
	}
}

/* Convenient functions for operating on one or all files being loaded. */

/*
 * Call FUNCTION on each input file entry. Do not call for entries for
 * libraries; instead, call once for each library member that is being
 * loaded.
 * 
 * FUNCTION receives two arguments: the entry, and ARG.
 */

void
each_file(function, arg)
	register void   (*function) ();
	register int    arg;
{
	register int    i;

	for (i = 0; i < number_of_files; i++) {
		register struct file_entry *entry = &file_table[i];
		if (entry->library_flag) {
			register struct file_entry *subentry = entry->subfiles;
			for (; subentry; subentry = subentry->chain)
				(*function) (subentry, arg);
		} else
			(*function) (entry, arg);
	}
}

/*
 * Call FUNCTION on each input file entry until it returns a non-zero value.
 * Return this value. Do not call for entries for libraries; instead, call
 * once for each library member that is being loaded.
 * 
 * FUNCTION receives two arguments: the entry, and ARG.  It must be a function
 * returning unsigned long (though this can probably be fudged).
 */

unsigned long
check_each_file(function, arg)
	register unsigned long (*function) ();
	register int    arg;
{
	register int    i;
	register unsigned long return_val;

	for (i = 0; i < number_of_files; i++) {
		register struct file_entry *entry = &file_table[i];
		if (entry->library_flag) {
			register struct file_entry *subentry = entry->subfiles;
			for (; subentry; subentry = subentry->chain)
				if (return_val = (*function) (subentry, arg))
					return return_val;
		} else if (return_val = (*function) (entry, arg))
			return return_val;
	}
	return 0;
}

/* Like `each_file' but ignore files that were just for symbol definitions.  */

void
each_full_file(function, arg)
	register void   (*function) ();
	register int    arg;
{
	register int    i;

	for (i = 0; i < number_of_files; i++) {
		register struct file_entry *entry = &file_table[i];
		if (entry->just_syms_flag || entry->is_dynamic)
			continue;
		if (entry->library_flag) {
			register struct file_entry *subentry = entry->subfiles;
			for (; subentry; subentry = subentry->chain)
				(*function) (subentry, arg);
		} else
			(*function) (entry, arg);
	}
}

/* Close the input file that is now open.  */

void
file_close()
{
	close(input_desc);
	input_desc = 0;
	input_file = 0;
}

/*
 * Open the input file specified by 'entry', and return a descriptor. The
 * open file is remembered; if the same file is opened twice in a row, a new
 * open is not actually done.
 */
int
file_open (entry)
     register struct file_entry *entry;
{
	register int desc;

	if (entry->superfile)
		return file_open (entry->superfile);

	if (entry == input_file)
		return input_desc;

	if (input_file) file_close ();

	if (entry->search_dirs_flag) {
		desc = findlib(entry);
	} else
		desc = open (entry->filename, O_RDONLY, 0);

	if (desc > 0) {
		input_file = entry;
		input_desc = desc;
		return desc;
	}

	perror_file (entry);
	/* NOTREACHED */
}

int
text_offset (entry)
     struct file_entry *entry;
{
	return entry->starting_offset + N_TXTOFF (entry->header);
}

/* Medium-level input routines for rel files.  */

/*
 * Read a file's header into the proper place in the file_entry. DESC is the
 * descriptor on which the file is open. ENTRY is the file's entry.
 */
void
read_header (desc, entry)
     int desc;
     register struct file_entry *entry;
{
	register int len;
	struct exec *loc = (struct exec *) &entry->header;

	if (lseek (desc, entry->starting_offset, L_SET) !=
						entry->starting_offset)
		fatal_with_file("read_header: lseek failure ", entry);

	len = read (desc, &entry->header, sizeof (struct exec));
	if (len != sizeof (struct exec))
		fatal_with_file ("failure reading header of ", entry);

	md_swapin_exec_hdr(&entry->header);

	if (N_BADMAG (*loc))
		fatal_with_file ("bad magic number in ", entry);

	entry->header_read_flag = 1;
}

/*
 * Read the symbols of file ENTRY into core. Assume it is already open, on
 * descriptor DESC. Also read the length of the string table, which follows
 * the symbol table, but don't read the contents of the string table.
 */
void
read_entry_symbols (desc, entry)
     struct file_entry *entry;
     int desc;
{
	int		str_size;
	struct nlist	*np;
	int		i;

	if (!entry->header_read_flag)
		read_header (desc, entry);

	np = (struct nlist *) alloca (entry->header.a_syms);
	entry->nsymbols = entry->header.a_syms / sizeof(struct nlist);
	entry->symbols = (struct localsymbol *)
		xmalloc(entry->nsymbols * sizeof(struct localsymbol));

	if (lseek(desc, N_SYMOFF(entry->header) + entry->starting_offset, L_SET)
			!= N_SYMOFF(entry->header) + entry->starting_offset)
		fatal_with_file ("read_symbols(h): lseek failure ", entry);

	if (entry->header.a_syms != read (desc, np, entry->header.a_syms))
		fatal_with_file ("premature end of file in symbols of ", entry);

	md_swapin_symbols(np, entry->header.a_syms / sizeof(struct nlist));

	for (i = 0; i < entry->nsymbols; i++) {
		entry->symbols[i].nzlist.nlist = *np++;
		entry->symbols[i].nzlist.nz_size = 0;
		entry->symbols[i].symbol = NULL;
		entry->symbols[i].next = NULL;
		entry->symbols[i].gotslot_offset = -1;
		entry->symbols[i].gotslot_claimed = 0;
	}

	entry->strings_offset = N_STROFF(entry->header) +
					entry->starting_offset;
	if (lseek(desc, entry->strings_offset, 0) == (off_t)-1)
		fatal_with_file ("read_symbols(s): lseek failure ", entry);
	if (sizeof str_size != read (desc, &str_size, sizeof str_size))
		fatal_with_file ("bad string table size in ", entry);

	entry->string_size = md_swap_long(str_size);
}

/*
 * Read the string table of file ENTRY into core. Assume it is already open,
 * on descriptor DESC.
 */
void
read_entry_strings (desc, entry)
     struct file_entry *entry;
     int desc;
{
	int buffer;

	if (!entry->header_read_flag || !entry->strings_offset)
		fatal("internal error: %s", "cannot read string table");

	if (lseek (desc, entry->strings_offset, L_SET) != entry->strings_offset)
		fatal_with_file ("read_strings: lseek failure ", entry);

	if (entry->string_size !=
			read (desc, entry->strings, entry->string_size))
		fatal_with_file ("premature end of file in strings of ", entry);

	return;
}

/* DEAD - Read in all of the relocation information */

void
read_relocation ()
{
	each_full_file (read_entry_relocation, 0);
}

/* Read in the relocation sections of ENTRY if necessary */

void
read_entry_relocation (desc, entry)
	int			desc;
	struct file_entry	*entry;
{
	register struct relocation_info *reloc;
	off_t	pos;

	if (!entry->textrel) {

		reloc = (struct relocation_info *)
				xmalloc(entry->header.a_trsize);

		pos = text_offset(entry) +
				entry->header.a_text + entry->header.a_data;

		if (lseek(desc, pos, L_SET) != pos)
			fatal_with_file("read_reloc(t): lseek failure ", entry);

		if (entry->header.a_trsize !=
				read(desc, reloc, entry->header.a_trsize)) {
			fatal_with_file (
				"premature eof in text relocation of ", entry);
		}
		md_swapin_reloc(reloc, entry->header.a_trsize / sizeof(*reloc));
		entry->textrel = reloc;
		entry->ntextrel = entry->header.a_trsize / sizeof(*reloc);

	}

	if (!entry->datarel) {

		reloc = (struct relocation_info *)
				xmalloc(entry->header.a_drsize);

		pos = text_offset(entry) + entry->header.a_text +
			entry->header.a_data + entry->header.a_trsize;

		if (lseek(desc, pos, L_SET) != pos)
			fatal_with_file("read_reloc(d): lseek failure ", entry);

		if (entry->header.a_drsize !=
				read (desc, reloc, entry->header.a_drsize)) {
			fatal_with_file (
				"premature eof in data relocation of ", entry);
		}
		md_swapin_reloc(reloc, entry->header.a_drsize / sizeof(*reloc));
		entry->datarel = reloc;
		entry->ndatarel = entry->header.a_drsize / sizeof(*reloc);

	}
}


/* Read in the symbols of all input files.  */

void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
void enter_file_symbols (), enter_global_ref ();

void
load_symbols ()
{
	register int i;

	if (trace_files) fprintf (stderr, "Loading symbols:\n\n");

	for (i = 0; i < number_of_files; i++) {
		register struct file_entry *entry = &file_table[i];
		read_file_symbols (entry);
	}

	if (trace_files) fprintf (stderr, "\n");
}

/*
 * If ENTRY is a rel file, read its symbol and string sections into core. If
 * it is a library, search it and load the appropriate members (which means
 * calling this function recursively on those members).
 */

void
read_file_symbols (entry)
     register struct file_entry *entry;
{
	register int desc;
	register int len;
	struct exec hdr;

	desc = file_open (entry);

	len = read (desc, &hdr, sizeof hdr);
	if (len != sizeof hdr)
		fatal_with_file ("failure reading header of ", entry);

	md_swapin_exec_hdr(&hdr);

	if (!N_BADMAG (hdr)) {
		if (N_IS_DYNAMIC(hdr)) {
			if (relocatable_output) {
				fatal_with_file(
			"-r and shared objects currently not supported ",
					entry);
				return;
			}
			entry->is_dynamic = 1;
			read_shared_object(desc, entry);
			rrs_add_shobj(entry);
		} else {
			read_entry_symbols (desc, entry);
			entry->strings = (char *) alloca (entry->string_size);
			read_entry_strings (desc, entry);
			read_entry_relocation(desc, entry);
			enter_file_symbols (entry);
			entry->strings = 0;
		}
	} else {
		char armag[SARMAG];

		lseek (desc, 0, 0);
		if (SARMAG != read (desc, armag, SARMAG) ||
					strncmp (armag, ARMAG, SARMAG))
			fatal_with_file(
			"malformed input file (not rel or archive) ", entry);
		entry->library_flag = 1;
		search_library (desc, entry);
	}

	file_close ();
}

/* Enter the external symbol defs and refs of ENTRY in the hash table.  */

void
enter_file_symbols (entry)
     struct file_entry *entry;
{
	struct localsymbol	*lsp, *lspend;

	if (trace_files) prline_file_name (entry, stderr);

	lspend = entry->symbols + entry->nsymbols;

	for (lsp = entry->symbols; lsp < lspend; lsp++) {
		register struct nlist *p = &lsp->nzlist.nlist;

		if (p->n_type == (N_SETV | N_EXT))
			continue;

		/*
		 * Turn magically prefixed symbols into set symbols of
		 * a corresponding type.
		 */
		if (set_element_prefixes &&
		    set_element_prefixed_p(entry->strings+lsp->nzlist.nz_strx))
			lsp->nzlist.nz_type += (N_SETA - N_ABS);

		if (SET_ELEMENT_P(p->n_type)) {
			set_symbol_count++;
			if (!relocatable_output)
				enter_global_ref(lsp,
					p->n_un.n_strx + entry->strings, entry);
		} else if (p->n_type == N_WARNING) {
			char *name = p->n_un.n_strx + entry->strings;

			/* Grab the next entry.  */
			p++;
			if (p->n_type != (N_UNDF | N_EXT)) {
				error("Warning symbol found in %s without external reference following.",
					get_file_name(entry));
				make_executable = 0;
				p--;		/* Process normally.  */
			} else {
				symbol *sp;
				char *sname = p->n_un.n_strx + entry->strings;
				/* Deal with the warning symbol.  */
				enter_global_ref(lsp,
					p->n_un.n_strx + entry->strings, entry);
				sp = getsym (sname);
				sp->warning = (char *)xmalloc(strlen(name)+1);
				strcpy (sp->warning, name);
				warning_count++;
			}
		} else if (p->n_type & N_EXT) {
			enter_global_ref(lsp,
				p->n_un.n_strx + entry->strings, entry);
		} else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT))
							&& !entry->is_dynamic) {
			if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
				non_L_local_sym_count++;
			local_sym_count++;
		} else if (!entry->is_dynamic)
			debugger_sym_count++;
	}

	/*
	 * Count one for the local symbol that we generate,
	 * whose name is the file's name (usually) and whose address
	 * is the start of the file's text.
	 */

	if (!entry->is_dynamic) {
		local_sym_count++;
		non_L_local_sym_count++;
	}
}

/*
 * Enter one global symbol in the hash table. LSP points to the `struct
 * localsymbol' from the file that describes the global symbol.  NAME is the
 * symbol's name. ENTRY is the file entry for the file the symbol comes from.
 * 
 * LSP is put on the chain of all such structs that refer to the same symbol.
 * This chain starts in the `refs' for symbols from relocatable objects. A
 * backpointer to the global symbol is kept in LSP.
 *
 * Symbols from shared objects are linked through `dynref'. For such symbols
 * that's all we do at this stage, with the exception of the case where the
 * symbol is a common. The `referenced' bit is only set for references from
 * relocatable objects.
 *
 */

void
enter_global_ref (lsp, name, entry)
     struct localsymbol *lsp;
     char *name;
     struct file_entry *entry;
{
	register struct nzlist *nzp = &lsp->nzlist;
	register symbol *sp = getsym (name);
	register int type = nzp->nz_type;
	int oldref = sp->referenced;
	int olddef = sp->defined;
	int com = sp->defined && sp->max_common_size;

	if (type == (N_INDR | N_EXT)) {
		sp->alias = getsym(entry->strings + (lsp + 1)->nzlist.nz_strx);
		if (sp == sp->alias) {
			error("%s: %s is alias for itself",
					get_file_name(entry), name);
			/* Rewrite symbol as global text symbol with value 0 */
			lsp->nzlist.nz_type = N_TEXT|N_EXT;
			lsp->nzlist.nz_value = 0;
			make_executable = 0;
		} else
			global_alias_count++;
	}

	if (entry->is_dynamic) {
		lsp->next = sp->sorefs;
		sp->sorefs = lsp;

		/*
		 * Handle commons from shared objects:
		 *   1) If symbol hitherto undefined, turn it into a common.
		 *   2) If symbol already common, update size if necessary.
		 */
/*XXX - look at case where commons are only in shared objects */
		if (type == (N_UNDF | N_EXT) && nzp->nz_value) {
			if (!olddef) {
				if (oldref)
					undefined_global_sym_count--;
				common_defined_global_count++;
				sp->max_common_size = nzp->nz_value;
				sp->defined = N_UNDF | N_EXT;
			} else if (com && sp->max_common_size < nzp->nz_value) {
				sp->max_common_size = nzp->nz_value;
			}
		}

		/*
		 * Handle size information in shared objects.
		 */
		if (nzp->nz_size > sp->size)
			sp->size = nzp->nz_size;

		lsp->symbol = sp;
		return;
	}

	lsp->next = sp->refs;
	sp->refs = lsp;
	lsp->symbol = sp;

	sp->referenced = 1;

	if (sp == dynamic_symbol || sp == got_symbol) {
		if (type != (N_UNDF | N_EXT) && !entry->just_syms_flag)
			fatal("Linker reserved symbol %s defined as type %x ",	
						name, type);
		return;
	}

#ifdef N_SIZE
	if (type == (N_SIZE | N_EXT)) {
		if (sp->size < nzp->nz_value)
			sp->size = nzp->nz_value;
	} else
#endif
	if (type != (N_UNDF | N_EXT) || nzp->nz_value) {

		/*
		 * Set `->defined' here, so commons and undefined globals
		 * can be counted correctly.
		 */
		if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
			sp->defined = type;

		if (oldref && !olddef)
			/*
			 * It used to be undefined and we're defining it.
			 */
			undefined_global_sym_count--;

		if (!olddef && type == (N_UNDF | N_EXT) && nzp->nz_value) {
			/*
			 * First definition and it's common.
			 */
			common_defined_global_count++;
			sp->max_common_size = nzp->nz_value;
		} else if (com && type != (N_UNDF | N_EXT)) {
			/*
			 * It used to be common and we're defining
			 * it as something else.
			 */
			common_defined_global_count--;
			sp->max_common_size = 0;
		} else if (com && type == (N_UNDF | N_EXT)
				  && sp->max_common_size < nzp->nz_value)
			/*
			 * It used to be common and this is a new common entry
			 * to which we need to pay attention.
			 */
			sp->max_common_size = nzp->nz_value;

		if (SET_ELEMENT_P(type) && (!olddef || com))
			set_vector_count++;

	} else if (!oldref)
		undefined_global_sym_count++;


	if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
		text_start = nzp->nz_value;

	if (sp->trace) {
		register char *reftype;
		switch (type & N_TYPE) {
		case N_UNDF:
			reftype = nzp->nz_value?
					"defined as common":"referenced";
			break;

		case N_ABS:
			reftype = "defined as absolute";
			break;

		case N_TEXT:
			reftype = "defined in text section";
			break;

		case N_DATA:
			reftype = "defined in data section";
			break;

		case N_BSS:
			reftype = "defined in BSS section";
			break;

		default:
			reftype = "I don't know this type";
			break;
		}

		fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
		print_file_name (entry, stderr);
		fprintf (stderr, "\n");
	}
}

/*
 * This return 0 if the given file entry's symbol table does *not* contain
 * the nlist point entry, and it returns the files entry pointer (cast to
 * unsigned long) if it does.
 */

unsigned long
contains_symbol (entry, np)
     struct file_entry *entry;
     register struct nlist *np;
{
	if (np >= &entry->symbols->nzlist.nlist &&
		np < &(entry->symbols + entry->nsymbols)->nzlist.nlist)
		return (unsigned long) entry;
	return 0;
}


void consider_file_section_lengths (), relocate_file_addresses ();
void consider_relocation();

/*
 * Having entered all the global symbols and found the sizes of sections of
 * all files to be linked, make all appropriate deductions from this data.
 * 
 * We propagate global symbol values from definitions to references. We compute
 * the layout of the output file and where each input file's contents fit
 * into it.
 * 
 * This is now done in several stages.
 *
 * 1) All global symbols are examined for definitions in relocatable (.o)
 *    files. The symbols' type is set according to the definition found,
 *    but its value can not yet be determined. In stead, we keep a pointer
 *    to the file entry's localsymbol that bequeathed the global symbol with
 *    its definition. Also, multiple (incompatible) definitions are checked
 *    for in this pass. If no definition comes forward, the set of local
 *    symbols originating from shared objects is searched for a definition.
 *
 * 2) Then the relocation information of each relocatable file is examined
 *    for for possible contributions to the RRS section.
 *
 * 3) When this is done, the sizes and start addresses are set of all segments
 *    that will appear in the output file (including the RRS segment).
 *
 * 4) Finally, all symbols are relocated according according to the start
 *    of the entry they are part of. Then global symbols are assigned their
 *    final values. Also, space for commons and imported data are allocated
 *    during this pass, if the link mode in effect so demands.
 *
 */

void digest_pass1(), digest_pass2();

void
digest_symbols ()
{

	if (trace_files)
		fprintf(stderr, "Digesting symbol information:\n\n");

	if (!relocatable_output) {
		/*
		 * The set sector size is the number of set elements + a word
		 * for each symbol for the length word at the beginning of
		 * the vector, plus a word for each symbol for a zero at the
		 * end of the vector (for incremental linking).
		 */
		set_sect_size = (2 * set_symbol_count + set_vector_count) *
							sizeof (unsigned long);
		set_vectors = (unsigned long *) xmalloc (set_sect_size);
		setv_fill_count = 0;
	}

	/* Pass 1: check and define symbols */
	defined_global_sym_count = 0;
	digest_pass1();

	if (!relocatable_output) {
		each_full_file(consider_relocation, 0);	/* Text */
		each_full_file(consider_relocation, 1); /* Data */
	}

	/*
	 * Compute total size of sections.
	 * RRS data is the first output data section, RRS text is the last
	 * text section. Thus, DATA_START is calculated from RRS_DATA_START
	 * and RRS_DATA_SIZE, while RRS_TEXT_START is derived from TEXT_START
	 * and TEXT_SIZE.
	 */
	consider_rrs_section_lengths();
	each_full_file(consider_file_section_lengths, 0);
	rrs_text_start = text_start + text_size;
	text_size += rrs_text_size;
	data_size += rrs_data_size;

	/*
	 * If necessary, pad text section to full page in the file. Include
	 * the padding in the text segment size.
	 */

	if (magic == ZMAGIC) {
		int  text_end = text_size + N_TXTOFF(outheader);
		text_pad = PALIGN(text_end, page_size) - text_end;
		text_size += text_pad;
	}
	outheader.a_text = text_size;

	/*
	 * Make the data segment address start in memory on a suitable
	 * boundary.
	 */

	if (!Tdata_flag_specified)
		rrs_data_start = text_start +
			DATA_START(outheader) - TEXT_START(outheader);

	data_start = rrs_data_start + rrs_data_size;
	if (!relocatable_output) {
		set_sect_start = rrs_data_start + data_size;
		data_size += set_sect_size;
	}
	bss_start = rrs_data_start + data_size;

#ifdef DEBUG
printf("textstart = %#x, textsize = %#x, rrs_text_start = %#x, rrs_text_size %#x\n",
	text_start, text_size, rrs_text_start, rrs_text_size);
printf("datastart = %#x, datasize = %#x, rrs_data_start %#x, rrs_data_size %#x\n",
	data_start, data_size, rrs_data_start, rrs_data_size);
printf("bssstart = %#x, bsssize = %#x\n",
	bss_start, bss_size);
#endif

	/* Compute start addresses of each file's sections and symbols.  */

	each_full_file(relocate_file_addresses, 0);
	relocate_rrs_addresses();

	/* Pass 2: assign values to symbols */
	digest_pass2();

	if (end_symbol) {	/* These are null if -r.  */
		etext_symbol->value = text_start + text_size - text_pad;
		edata_symbol->value = rrs_data_start + data_size;
		end_symbol->value = rrs_data_start + data_size + bss_size;
	}
	/*
	 * Figure the data_pad now, so that it overlaps with the bss
	 * addresses.
	 */

	if (specified_data_size && specified_data_size > data_size)
		data_pad = specified_data_size - data_size;

	if (magic == ZMAGIC)
		data_pad = PALIGN(data_pad + data_size, page_size) - data_size;

	bss_size -= data_pad;
	if (bss_size < 0)
		bss_size = 0;

	data_size += data_pad;
}

void
digest_pass1()
{

	/*
	 * Now, for each symbol, verify that it is defined globally at most
	 * once within relocatable files (except when building a shared lib).
	 * and set the `defined' field if there is a definition.
	 *
	 * Then check the shared object symbol chain for any remaining
	 * undefined symbols. Set the `so_defined' field for any
	 * definition find this way.
	 */
	FOR_EACH_SYMBOL(i, sp) {
		struct localsymbol *lsp;
		int             defs = 0;

		if (!sp->referenced) {
#if 0
			/* Check for undefined symbols in shared objects */
			int type;
			for (lsp = sp->sorefs; lsp; lsp = lsp->next) {
				type = lsp->nzlist.nlist.n_type;
				if ((type & N_EXT) && type != (N_UNDF | N_EXT))
					break;
			}
			if ((type & N_EXT) && type == (N_UNDF | N_EXT))
				undefined_shobj_sym_count++;
#endif

			/* Superfluous symbol from shared object */
			continue;
		}

		if (sp == got_symbol || sp == dynamic_symbol)
			continue;

		for (lsp = sp->refs; lsp; lsp = lsp->next) {
			register struct nlist *p = &lsp->nzlist.nlist;
			register int    type = p->n_type;

			if (SET_ELEMENT_P(type)) {
				if (relocatable_output)
					fatal(
				"internal error: global ref to set el %s with -r",
						sp->name);
				if (!defs++) {
					sp->defined = N_SETV | N_EXT;
					sp->value =
						setv_fill_count++ * sizeof(long);
				} else if ((sp->defined & N_TYPE) != N_SETV) {
					sp->multiply_defined = 1;
					multiple_def_count++;
				}
				/* Keep count and remember symbol */
				sp->setv_count++;
				set_vectors[setv_fill_count++] = (long)p;

			} else if ((type & N_EXT) && type != (N_UNDF | N_EXT)
						&& (type & N_TYPE) != N_FN
						&& (type & N_TYPE) != N_SIZE) {
				/* non-common definition */
				if (defs++ && sp->value != p->n_value
				    && entry_symbol) {
					sp->multiply_defined = 1;
					multiple_def_count++;
				}
				sp->def_nlist = p;
				sp->defined = type;
			}
		}

		if (sp->defined) {
			if ((sp->defined & N_TYPE) == N_SETV)
				/* Allocate zero entry in set vector */
				setv_fill_count++;
			defined_global_sym_count++;
			continue;
		}

		if (relocatable_output)
			/* We're done */
			continue;

		/*
		 * Still undefined, search the shared object symbols for a
		 * definition. This symbol must go into the RRS.
		 */
		if (building_shared_object) {
			/* Just punt for now */
			undefined_global_sym_count--;
			continue;
		}

		for (lsp = sp->sorefs; lsp; lsp = lsp->next) {
			register struct nlist *p = &lsp->nzlist.nlist;
			register int    type = p->n_type;

			if ((type & N_EXT) && type != (N_UNDF | N_EXT)
			    && (type & N_TYPE) != N_FN) {
				/* non-common definition */
				sp->def_nlist = p;
				sp->so_defined = type;
				undefined_global_sym_count--;
#ifdef DEBUG
printf("shr: %s gets defined to %x with value %x\n", sp->name, type, sp->value);
#endif
				break;
			}
		}
	} END_EACH_SYMBOL;
}

void
digest_pass2()
{
	/*
	 * Assign each symbol its final value.
	 * If not -r'ing, allocate common symbols in the BSS section.
	 */

	FOR_EACH_SYMBOL(i, sp) {
		int		size;
		int             align = sizeof(int);

		if (!sp->referenced)
			continue;

		if ((sp->defined & N_TYPE) == N_SETV) {
			/*
			 * Set length word at front of vector and zero byte
			 * at end. Reverse the vector itself to put it in
			 * file order.
			 */
			unsigned long	i, tmp;
			unsigned long	length_word_index =
						sp->value / sizeof(long);

			/* Relocate symbol value */
			sp->value += set_sect_start;

			set_vectors[length_word_index] = sp->setv_count;

			/*
			 * Relocate vector to final address.
			 */
			for (i = 0; i < sp->setv_count; i++) {
				struct nlist	*p = (struct nlist *)
					set_vectors[1+i+length_word_index];

				set_vectors[1+i+length_word_index] = p->n_value;
			}

			/*
			 * Reverse the vector.
			 */
			for (i = 1; i < (sp->setv_count - 1)/2 + 1; i++) {

				tmp = set_vectors[length_word_index + i];
				set_vectors[length_word_index + i] =
					set_vectors[length_word_index + sp->setv_count + 1 - i];
				set_vectors[length_word_index + sp->setv_count + 1 - i] = tmp;
			}

			/* Clear terminating entry */
			set_vectors[length_word_index + sp->setv_count + 1] = 0;
			continue;
		}


		if (sp->defined && sp->def_nlist &&
				((sp->defined & ~N_EXT) != N_SETV))
			sp->value = sp->def_nlist->n_value;

		if (building_shared_object && !(link_mode & SYMBOLIC))
			/* No common allocation in shared objects */
			continue;

		if ((size = sp->max_common_size) != 0) {
			/*
			 * It's a common.
			 */
			if (sp->defined != (N_UNDF + N_EXT))
				fatal("%s: common isn't", sp->name);

		} else if ((size = sp->size) != 0 && sp->defined == N_SIZE) {
			/*
			 * It's data from shared object with size info.
			 */
			if (sp->so_defined != (N_DATA + N_EXT))
				fatal("%s: Bogus N_SIZE item", sp->name);

		} else
			/*
			 * It's neither
			 */
			continue;


		if (relocatable_output && !force_common_definition) {
			sp->defined = 0;
			undefined_global_sym_count++;
			defined_global_sym_count--;
			continue;
		}

		/*
		 * Round up to nearest sizeof (int). I don't know whether
		 * this is necessary or not (given that alignment is taken
		 * care of later), but it's traditional, so I'll leave it in.
		 * Note that if this size alignment is ever removed, ALIGN
		 * above will have to be initialized to 1 instead of sizeof
		 * (int).
		 */

		size = PALIGN(size, sizeof(int));

		while (!(size & align))
			align <<= 1;

		align = align > MAX_ALIGNMENT ?
			MAX_ALIGNMENT : align;

		bss_size = PALIGN(bss_size + data_size + rrs_data_start, align)
				- (data_size + rrs_data_start);

		sp->value = rrs_data_start + data_size + bss_size;
		if (sp->defined == (N_UNDF | N_EXT))
			sp->defined = N_BSS | N_EXT;
		else {
			sp->so_defined = 0;
			defined_global_sym_count++;
		}
		bss_size += size;
		if (write_map)
			printf("Allocating %s %s: %x at %x\n",
				sp->defined==(N_BSS|N_EXT)?"common":"data",
				sp->name, size, sp->value);

	} END_EACH_SYMBOL;
}

/*
 * Scan relocation info in ENTRY for contributions to the dynamic section of
 * the output file.
 */
void
consider_relocation (entry, dataseg)
	struct file_entry	*entry;
	int			dataseg;
{
	struct relocation_info	*reloc, *end;
	struct localsymbol	*lsp;
	symbol			*sp;

	if (dataseg == 0) {
		/* Text relocations */
		reloc = entry->textrel;
		end = entry->textrel + entry->ntextrel;
	} else {
		/* Data relocations */
		reloc = entry->datarel;
		end = entry->datarel + entry->ndatarel;
	}

	for (; reloc < end; reloc++) {

		/*
		 * First, do the PIC specific relocs.
		 * r_relative and r_copy should not occur at this point
		 * (we do output them). The others break down to these
		 * combinations:
		 *
		 * jmptab:	extern:		needs jmp slot
		 *		!extern:	"intersegment" jump/call,
		 *				should get resolved in output
		 *
		 * baserel:	extern:		need GOT entry
		 *		!extern:	may need GOT entry,
		 *				machine dependent
		 *
		 * baserel's always refer to symbol through `r_symbolnum'
		 * whether extern or not. Internal baserels refer to statics
		 * that must be accessed either *through* the GOT table like
		 * global data, or by means of an offset from the GOT table.
		 * The macro RELOC_STATICS_THROUGH_GOT_P() determines which
		 * applies, since this is a machine (compiler?) dependent
		 * addressing mode.
		 */

		if (RELOC_JMPTAB_P(reloc)) {

			if (!RELOC_EXTERN_P(reloc))
				continue;

			lsp = &entry->symbols[reloc->r_symbolnum];
			sp = lsp->symbol;
			if (sp->alias)
				sp = sp->alias;
			alloc_rrs_jmpslot(sp);

		} else if (RELOC_BASEREL_P(reloc)) {

			lsp = &entry->symbols[reloc->r_symbolnum];
			alloc_rrs_gotslot(reloc, lsp);

		} else if (RELOC_EXTERN_P(reloc)) {

			/*
			 * Non-PIC relocations.
			 * If the definition comes from a shared object
			 * we need a relocation entry in RRS.
			 *
			 * If the .so definition is N_TEXT a jmpslot is
			 * allocated.
			 *
			 * If it is N_DATA we allocate an address in BSS (?)
			 * and arrange for the data to be copied at run-time.
			 * The symbol is temporarily marked with N_SIZE in
			 * the `defined' field, so we know what to do in
			 * pass2() and during actual relocation. We convert
			 * the type back to something real again when writing
			 * out the symbols.
			 * 
			 */
			lsp = &entry->symbols[reloc->r_symbolnum];
			sp = lsp->symbol;
			if (sp == NULL)
				fatal_with_file(
					"internal error, sp==NULL", entry);

			if (sp->alias)
				sp = sp->alias;

			/*
			 * Skip refs to _GLOBAL_OFFSET_TABLE_ and __DYNAMIC
			 */
			if (sp == got_symbol) {
				if (!CHECK_GOT_RELOC(reloc))
					fatal_with_file(
					"Unexpected relocation type ", entry);
				continue;
			}

			/*
			 * This symbol gives rise to a RRS entry
			 */

			if (building_shared_object) {
				alloc_rrs_reloc(sp);
				continue;
			}

			if (force_alias_definition &&
					sp->so_defined == N_TEXT + N_EXT) {

				/* Call to shared library procedure */
				alloc_rrs_jmpslot(sp);

			} else if (sp->size &&
					sp->so_defined == N_DATA + N_EXT) {

				/* Reference to shared library data */
				alloc_rrs_cpy_reloc(sp);
				sp->defined = N_SIZE;

			} else if (!sp->defined && sp->max_common_size == 0)
				alloc_rrs_reloc(sp);

		} else {
			/*
			 * Segment relocation.
			 * Prepare an RRS relocation as these are load
			 * address dependent.
			 */
			if (building_shared_object) {
				alloc_rrs_segment_reloc(reloc);
			}
		}
	}
}

/*
 * Accumulate the section sizes of input file ENTRY into the section sizes of
 * the output file.
 */
void
consider_file_section_lengths (entry)
     register struct file_entry *entry;
{
	if (entry->just_syms_flag)
		return;

	entry->text_start_address = text_size;
	/* If there were any vectors, we need to chop them off */
	text_size += entry->header.a_text;
	entry->data_start_address = data_size;
	data_size += entry->header.a_data;
	entry->bss_start_address = bss_size;
	bss_size += entry->header.a_bss;

	text_reloc_size += entry->header.a_trsize;
	data_reloc_size += entry->header.a_drsize;
}

/*
 * Determine where the sections of ENTRY go into the output file,
 * whose total section sizes are already known.
 * Also relocate the addresses of the file's local and debugger symbols.
 */
void
relocate_file_addresses (entry)
     register struct file_entry *entry;
{
	register struct localsymbol *lsp, *lspend;

	entry->text_start_address += text_start;
	/*
	 * Note that `data_start' and `data_size' have not yet been
	 * adjusted for `data_pad'.  If they had been, we would get the wrong
	 * results here.
	 */
	entry->data_start_address += data_start;
	entry->bss_start_address += bss_start;
#ifdef DEBUG
printf("%s: datastart: %#x, bss %#x\n", get_file_name(entry),
		entry->data_start_address, entry->bss_start_address);
#endif

	lspend = entry->symbols + entry->nsymbols;

	for (lsp = entry->symbols; lsp < lspend; lsp++) {
		register struct nlist *p = &lsp->nzlist.nlist;
		/*
		 * If this belongs to a section, update it
		 * by the section's start address
		 */
		register int type = p->n_type & N_TYPE;

		switch (type) {
		case N_TEXT:
		case N_SETT:
			p->n_value += entry->text_start_address;
			break;
		case N_DATA:
		case N_SETD:
		case N_SETV:
			/*
			 * A symbol whose value is in the data section is
			 * present in the input file as if the data section
			 * started at an address equal to the length of the
			 * file's text.
			 */
			p->n_value += entry->data_start_address -
						entry->header.a_text;
			break;
		case N_BSS:
		case N_SETB:
			/* likewise for symbols with value in BSS.  */
			p->n_value += entry->bss_start_address
				- entry->header.a_text - entry->header.a_data;
		break;
		}
	}
}

/* Write the output file */

void
write_output ()
{
	struct stat	statbuf;
	int		filemode;

	if (lstat(output_filename, &statbuf) != -1) {
		if (!S_ISDIR(statbuf.st_mode))
			(void)unlink(output_filename);
	}

	outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
	if (outdesc < 0)
		perror_name (output_filename);

	if (fstat (outdesc, &statbuf) < 0)
		perror_name (output_filename);

	filemode = statbuf.st_mode;

	chmod (output_filename, filemode & ~0111);

	/* Output the a.out header.  */
	write_header ();

	/* Output the text and data segments, relocating as we go.  */
	write_text ();
	write_data ();

	/* Output the merged relocation info, if requested with `-r'.  */
	if (relocatable_output)
		write_rel ();

	/* Output the symbol table (both globals and locals).  */
	write_syms ();

	/* Output the RSS section */
	write_rrs ();

	close (outdesc);

	if (chmod (output_filename, filemode | 0111) == -1)
		perror_name (output_filename);
}

void modify_location (), perform_relocation (), copy_text (), copy_data ();

/* Total number of symbols to be written in the output file. */
int	nsyms;

void
write_header ()
{
	int flags = (rrs_section_type == RRS_FULL) ? EX_DYNAMIC : 0;

	N_SET_FLAG (outheader, flags);
	outheader.a_text = text_size;
	outheader.a_data = data_size;
	outheader.a_bss = bss_size;
	outheader.a_entry = (entry_symbol ? entry_symbol->value
					: text_start + entry_offset);

	if (strip_symbols == STRIP_ALL)
		nsyms = 0;
	else {
		nsyms = (defined_global_sym_count + undefined_global_sym_count);
		if (discard_locals == DISCARD_L)
			nsyms += non_L_local_sym_count;
		else if (discard_locals == DISCARD_NONE)
			nsyms += local_sym_count;

		if (relocatable_output)
			/* For each alias we write out two struct nlists */
			nsyms += set_symbol_count + global_alias_count;

		if (dynamic_symbol->referenced)
			nsyms++, special_sym_count++;

		if (got_symbol->referenced)
			nsyms++, special_sym_count++;
	}

	if (strip_symbols == STRIP_NONE)
		nsyms += debugger_sym_count;

#ifdef DEBUG
printf("defined globals: %d, undefined globals %d, locals: %d (non_L: %d), \
debug symbols: %d, special: %d --> nsyms %d\n",
	defined_global_sym_count, undefined_global_sym_count,
	local_sym_count, non_L_local_sym_count, debugger_sym_count,
	special_sym_count, nsyms);
#endif

	outheader.a_syms = nsyms * sizeof (struct nlist);

	if (relocatable_output) {
		outheader.a_trsize = text_reloc_size;
		outheader.a_drsize = data_reloc_size;
	} else {
		outheader.a_trsize = 0;
		outheader.a_drsize = 0;
	}

	md_swapout_exec_hdr(&outheader);
	mywrite (&outheader, sizeof (struct exec), 1, outdesc);
	md_swapin_exec_hdr(&outheader);

	/*
	 * Output whatever padding is required in the executable file
	 * between the header and the start of the text.
	 */

#ifndef COFF_ENCAPSULATE
	padfile (N_TXTOFF(outheader) - sizeof outheader, outdesc);
#endif
}

/* Relocate the text segment of each input file
   and write to the output file.  */

void
write_text ()
{

	if (trace_files)
		fprintf (stderr, "Copying and relocating text:\n\n");

	each_full_file (copy_text, 0);
	file_close ();

	if (trace_files)
		fprintf (stderr, "\n");

	padfile (text_pad, outdesc);
}

/*
 * Read the text segment contents of ENTRY, relocate them, and write the
 * result to the output file.  If `-r', save the text relocation for later
 * reuse.
 */
void
copy_text (entry)
     struct file_entry *entry;
{
	register char *bytes;
	register int desc;

	if (trace_files)
		prline_file_name (entry, stderr);

	desc = file_open (entry);

	/* Allocate space for the file's text section */
	bytes = (char *) alloca (entry->header.a_text);

	/* Deal with relocation information however is appropriate */
	if (entry->textrel == NULL)
		fatal_with_file("no text relocation of ", entry);

	/* Read the text section into core.  */
	lseek (desc, text_offset (entry), 0);
	if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
		fatal_with_file ("premature eof in text section of ", entry);


	/* Relocate the text according to the text relocation.  */
	perform_relocation (bytes, entry->header.a_text,
			entry->textrel, entry->ntextrel, entry, 0);

	/* Write the relocated text to the output file.  */
	mywrite (bytes, 1, entry->header.a_text, outdesc);
}

/* Relocate the data segment of each input file
   and write to the output file.  */

void
write_data ()
{
	long	pos;

	if (trace_files)
		fprintf (stderr, "Copying and relocating data:\n\n");

	pos = N_DATOFF(outheader) + data_start - rrs_data_start;
	if (lseek(outdesc, pos, L_SET) != pos)
		fatal("write_data: lseek: cant position data offset");

	each_full_file (copy_data, 0);
	file_close ();

	/*
	 * Write out the set element vectors.  See digest symbols for
	 * description of length of the set vector section.
	 */

	if (set_vector_count) {
		swap_longs(set_vectors, 2 * set_symbol_count + set_vector_count);
		mywrite (set_vectors, 2 * set_symbol_count + set_vector_count,
				sizeof (unsigned long), outdesc);
	}

	if (trace_files)
		fprintf (stderr, "\n");

	padfile (data_pad, outdesc);
}

/*
 * Read the data segment contents of ENTRY, relocate them, and write the
 * result to the output file. If `-r', save the data relocation for later
 * reuse. See comments in `copy_text'.
 */
void
copy_data (entry)
     struct file_entry *entry;
{
	register char *bytes;
	register int desc;

	if (trace_files)
		prline_file_name (entry, stderr);

	desc = file_open (entry);

	bytes = (char *)alloca(entry->header.a_data);

	if (entry->datarel == NULL)
		fatal_with_file("no data relocation of ", entry);

	lseek (desc, text_offset (entry) + entry->header.a_text, 0);
	if (entry->header.a_data != read(desc, bytes, entry->header.a_data))
		fatal_with_file ("premature eof in data section of ", entry);

	perform_relocation (bytes, entry->header.a_data,
			entry->datarel, entry->ndatarel, entry, 1);

	mywrite (bytes, 1, entry->header.a_data, outdesc);
}

/*
 * Relocate ENTRY's text or data section contents. DATA is the address of the
 * contents, in core. DATA_SIZE is the length of the contents. PC_RELOCATION
 * is the difference between the address of the contents in the output file
 * and its address in the input file. RELOC is the address of the
 * relocation info, in core. NRELOC says how many there are.
 */
void
perform_relocation(data, data_size, reloc, nreloc, entry, dataseg)
	char			*data;
	int			data_size;
	struct relocation_info	*reloc;
	int			nreloc;
	struct file_entry	*entry;
	int			dataseg;
{
	register struct relocation_info *r = reloc;
	struct relocation_info *end = reloc + nreloc;

	text_relocation = entry->text_start_address;
	data_relocation = entry->data_start_address - entry->header.a_text;
	bss_relocation = entry->bss_start_address -
				entry->header.a_text - entry->header.a_data;
	pc_relocation = dataseg?
			entry->data_start_address - entry->header.a_text:
			entry->text_start_address;

	for (; r < end; r++) {
		int	addr = RELOC_ADDRESS(r);
		long	addend = md_get_addend(r, data+addr);
		long	relocation;

		/*
		 * Loop over the relocations again as we did in
		 * consider_relocation(), claiming the reserved RRS
		 * relocations.
		 */

		if (addr >= data_size)
			fatal_with_file(
				"relocation address out of range in ", entry);

		if (RELOC_JMPTAB_P(r)) {

			int		   symindex = RELOC_SYMBOL(r);
			struct localsymbol *lsp = &entry->symbols[symindex];
			symbol		   *sp;

			if (symindex >= entry->nsymbols)
				fatal_with_file(
				"relocation symbolnum out of range in ", entry);

			sp = lsp->symbol;
			if (sp->alias)
				sp = sp->alias;

			if (relocatable_output)
				relocation = addend;
			else if (!RELOC_EXTERN_P(r)) {
				relocation = addend +
					data_relocation - text_relocation;
			} else
				relocation = addend +
					claim_rrs_jmpslot(r, sp, addend);

		} else if (RELOC_BASEREL_P(r)) {

			int		   symindex = RELOC_SYMBOL(r);
			struct localsymbol *lsp = &entry->symbols[symindex];

			if (symindex >= entry->nsymbols)
				fatal_with_file(
				"relocation symbolnum out of range in ", entry);

			if (relocatable_output)
				relocation = addend;
			else if (!RELOC_EXTERN_P(r))
				relocation = claim_rrs_internal_gotslot(entry,
								r, lsp, addend);
			else
				relocation = claim_rrs_gotslot(r, lsp, addend);

		} else if (RELOC_EXTERN_P(r)) {

			int     symindex = RELOC_SYMBOL(r);
			symbol  *sp;

			if (symindex >= entry->nsymbols)
				fatal_with_file(
				"relocation symbolnum out of range in ", entry);

			sp = entry->symbols[symindex].symbol;
			if (sp->alias)
				sp = sp->alias;

			if (relocatable_output) {
				relocation = addend + sp->value;
			} else if (sp->defined) {
				if (sp == got_symbol) {
					/* Handle _GOT_ refs */
					relocation = addend + sp->value
							+ md_got_reloc(r);
				} else if (building_shared_object) {
					/*
					 * Normal (non-PIC) relocation needs
					 * to be converted into an RRS reloc
					 * when building a shared object.
					 */
					r->r_address += dataseg?
						entry->data_start_address:
						entry->text_start_address;
					relocation = addend;
					if (claim_rrs_reloc(r, sp, &relocation))
						continue;
				} else if (sp->defined == N_SIZE) {
					/*
					 * If size is known, arrange a
					 * run-time copy.
					 */
					if (!sp->size)
						fatal("Copy item isn't: %s",
							sp->name);

					relocation = addend + sp->value;
					r->r_address = sp->value;
					claim_rrs_cpy_reloc(r, sp);
				} else
					/* Plain old relocation */
					relocation = addend + sp->value;
			} else {
				/*
				 * If the symbol is undefined, we relocate it
				 * in a way similar to -r case. We use an
				 * RRS relocation to resolve the symbol at
				 * run-time. The r_address field is updated
				 * to reflect the changed position in the
				 * output file.
				 *
				 * In case the symbol is defined in a shared
				 * object as N_TEXT or N_DATA, an appropriate
				 * jmpslot or copy relocation is generated.
				 */
				switch (sp->so_defined) {

				case N_TEXT+N_EXT:
					/*
					 * Claim a jmpslot if one was
					 * allocated (dependent on
					 * `force_alias_flag').
					 */

					if (sp->jmpslot_offset == -1)
						goto undefined;

					relocation = addend +
						claim_rrs_jmpslot(r, sp, addend);
					break;

				case N_DATA+N_EXT:
					/*FALLTHROUGH*/
				case 0:
				undefined:
					r->r_address += dataseg?
						entry->data_start_address:
						entry->text_start_address;
					relocation = addend;
					if (claim_rrs_reloc(r, sp, &relocation))
						continue;
					break;

				case N_BSS+N_EXT:
printf("%s: BSS found in so_defined\n", sp->name);
					/*break;*/

				default:
					fatal("%s: shobj symbol with unknown type %#x", sp->name, sp->so_defined);
					break;
				}
			}

		} else {

			switch (RELOC_TYPE(r)) {
			case N_TEXT:
			case N_TEXT | N_EXT:
				relocation = addend + text_relocation;
				break;

			case N_DATA:
			case N_DATA | N_EXT:
				/*
				 * A word that points to beginning of the the
				 * data section initially contains not 0 but
				 * rather the "address" of that section in
				 * the input file, which is the length of the
				 * file's text.
				 */
				relocation = addend + data_relocation;
				break;

			case N_BSS:
			case N_BSS | N_EXT:
				/*
				 * Similarly, an input word pointing to the
				 * beginning of the bss initially contains
				 * the length of text plus data of the file.
				 */
				relocation = addend + bss_relocation;
				break;

			case N_ABS:
			case N_ABS | N_EXT:
				/*
				 * Don't know why this code would occur, but
				 * apparently it does.
				 */
				break;

			default:
				fatal_with_file(
				"nonexternal relocation code invalid in ", entry);
			}

			/*
			 * When building a shared object, these segment
			 * relocations need a "load address relative"
			 * RRS fixup.
			 */
			if (building_shared_object) {
				r->r_address += dataseg?
					entry->data_start_address:
					entry->text_start_address;
				claim_rrs_segment_reloc(r);
			}
		}

		if (RELOC_PCREL_P(r))
			relocation -= pc_relocation;

		md_relocate(r, relocation, data+addr, relocatable_output);

	}
}

/* For relocatable_output only: write out the relocation,
   relocating the addresses-to-be-relocated.  */

void coptxtrel (), copdatrel ();

void
write_rel ()
{
	register int count = 0;

	if (trace_files)
		fprintf (stderr, "Writing text relocation:\n\n");

	/*
	 * Assign each global symbol a sequence number, giving the order
	 * in which `write_syms' will write it.
	 * This is so we can store the proper symbolnum fields
	 * in relocation entries we write.
	 *

	/* BLECH - Assign number 0 to __DYNAMIC (!! Sun compatibility) */

	if (dynamic_symbol->referenced)
		dynamic_symbol->symbolnum = count++;
	FOR_EACH_SYMBOL(i, sp) {
		if (sp != dynamic_symbol && sp->referenced) {
			sp->symbolnum = count++;
		}
	} END_EACH_SYMBOL;

	/* Correct, because if (relocatable_output), we will also be writing
	whatever indirect blocks we have.  */
	if (count != defined_global_sym_count + undefined_global_sym_count
							+ special_sym_count)
		fatal ("internal error: write_rel: count = %d", count);

	/* Write out the relocations of all files, remembered from copy_text. */
	each_full_file (coptxtrel, 0);

	if (trace_files)
		fprintf (stderr, "\nWriting data relocation:\n\n");

	each_full_file (copdatrel, 0);

	if (trace_files)
		fprintf (stderr, "\n");
}

void
coptxtrel(entry)
	struct file_entry *entry;
{
	register struct relocation_info *r, *end;
	register int    reloc = entry->text_start_address;

	r = entry->textrel;
	end = r + entry->ntextrel;

	for (; r < end; r++) {
		register int  symindex;
		symbol       *sp;

		RELOC_ADDRESS(r) += reloc;

		if (!RELOC_EXTERN_P(r))
			continue;

		symindex = RELOC_SYMBOL(r);
		sp = entry->symbols[symindex].symbol;

		if (symindex >= entry->nsymbols)
			fatal_with_file(
			"relocation symbolnum out of range in ", entry);

#ifdef N_INDR
		/* Resolve indirection.  */
		if ((sp->defined & ~N_EXT) == N_INDR) {
			if (sp->alias == NULL)
				fatal("internal error: alias in hyperspace");
			sp = sp->alias;
		}
#endif

		/*
		 * If the symbol is now defined, change the external
		 * relocation to an internal one.
		 */

		if (sp->defined) {
			RELOC_EXTERN_P(r) = 0;
			RELOC_SYMBOL(r) = (sp->defined & N_TYPE);
#ifdef RELOC_ADD_EXTRA
			/*
			 * If we aren't going to be adding in the
			 * value in memory on the next pass of the
			 * loader, then we need to add it in from the
			 * relocation entry.  Otherwise the work we
			 * did in this pass is lost.
			 */
			if (!RELOC_MEMORY_ADD_P(r))
				RELOC_ADD_EXTRA(r) += sp->value;
#endif
		} else
			/*
			 * Global symbols come first.
			 */
			RELOC_SYMBOL(r) = sp->symbolnum;
	}
	md_swapout_reloc(entry->textrel, entry->ntextrel);
	mywrite(entry->textrel, entry->ntextrel,
				sizeof(struct relocation_info), outdesc);
}

void
copdatrel(entry)
	struct file_entry *entry;
{
	register struct relocation_info *r, *end;
	/*
	 * Relocate the address of the relocation. Old address is relative to
	 * start of the input file's data section. New address is relative to
	 * start of the output file's data section.
	 */
	register int    reloc = entry->data_start_address - text_size;

	r = entry->datarel;
	end = r + entry->ndatarel;

	for (; r < end; r++) {
		register int  symindex;
		symbol       *sp;
		int           symtype;

		RELOC_ADDRESS(r) += reloc;

		if (!RELOC_EXTERN_P(r))
			continue;

		symindex = RELOC_SYMBOL(r);
		sp = entry->symbols[symindex].symbol;

		if (symindex >= entry->header.a_syms)
			fatal_with_file(
			"relocation symbolnum out of range in ", entry);

#ifdef N_INDR
		/* Resolve indirection.  */
		if ((sp->defined & ~N_EXT) == N_INDR) {
			if (sp->alias == NULL)
				fatal("internal error: alias in hyperspace");
			sp = sp->alias;
		}
#endif

		symtype = sp->defined & N_TYPE;

		if (force_common_definition ||
					symtype == N_DATA ||
					symtype == N_TEXT ||
					symtype == N_ABS) {
			RELOC_EXTERN_P(r) = 0;
			RELOC_SYMBOL(r) = symtype;
		} else
			/*
			 * Global symbols come first.
			 */
			RELOC_SYMBOL(r) =
				entry->symbols[symindex].symbol->symbolnum;
	}
	md_swapout_reloc(entry->datarel, entry->ndatarel);
	mywrite(entry->datarel, entry->ndatarel,
				sizeof(struct relocation_info), outdesc);
}

void write_file_syms ();
void write_string_table ();

/* Offsets and current lengths of symbol and string tables in output file. */

int symbol_table_offset;
int symbol_table_len;

/* Address in output file where string table starts.  */
int string_table_offset;

/* Offset within string table
   where the strings in `strtab_vector' should be written.  */
int string_table_len;

/* Total size of string table strings allocated so far,
   including strings in `strtab_vector'.  */
int strtab_size;

/* Vector whose elements are strings to be added to the string table.  */
char **strtab_vector;

/* Vector whose elements are the lengths of those strings.  */
int *strtab_lens;

/* Index in `strtab_vector' at which the next string will be stored.  */
int strtab_index;

/*
 * Add the string NAME to the output file string table. Record it in
 * `strtab_vector' to be output later. Return the index within the string
 * table that this string will have.
 */

int
assign_string_table_index(name)
	char           *name;
{
	register int    index = strtab_size;
	register int    len = strlen(name) + 1;

	strtab_size += len;
	strtab_vector[strtab_index] = name;
	strtab_lens[strtab_index++] = len;

	return index;
}

FILE           *outstream = (FILE *) 0;

/*
 * Write the contents of `strtab_vector' into the string table. This is done
 * once for each file's local&debugger symbols and once for the global
 * symbols.
 */
void
write_string_table ()
{
	register int i;

	lseek (outdesc, string_table_offset + string_table_len, 0);

	if (!outstream)
		outstream = fdopen (outdesc, "w");

	for (i = 0; i < strtab_index; i++) {
		fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
		string_table_len += strtab_lens[i];
	}

	fflush (outstream);

	/* Report I/O error such as disk full.  */
	if (ferror (outstream))
		perror_name (output_filename);
}

/* Write the symbol table and string table of the output file.  */

void
write_syms()
{
	/* Number of symbols written so far.  */
	int		non_local_syms = defined_global_sym_count
					+ undefined_global_sym_count
					+ global_alias_count
					+ special_sym_count;
	int		syms_written = 0;
	struct nlist	nl;

	/*
	 * Buffer big enough for all the global symbols.  One extra struct
	 * for each indirect symbol to hold the extra reference following.
	 */
	struct nlist   *buf
		= (struct nlist *)alloca(non_local_syms * sizeof(struct nlist));
	/* Pointer for storing into BUF.  */
	register struct nlist *bufp = buf;

	/* Size of string table includes the bytes that store the size.  */
	strtab_size = sizeof strtab_size;

	symbol_table_offset = N_SYMOFF(outheader);
	symbol_table_len = 0;
	string_table_offset = N_STROFF(outheader);
	string_table_len = strtab_size;

	if (strip_symbols == STRIP_ALL)
		return;

	/* First, write out the global symbols.  */

	/*
	 * Allocate two vectors that record the data to generate the string
	 * table from the global symbols written so far.  This must include
	 * extra space for the references following indirect outputs.
	 */

	strtab_vector = (char **) alloca((non_local_syms) * sizeof(char *));
	strtab_lens = (int *) alloca((non_local_syms) * sizeof(int));
	strtab_index = 0;

	/*
	 * __DYNAMIC symbol *must* be first for Sun compatibility, as Sun's
	 * ld.so reads the shared object's first symbol. This means that
	 * (Sun's) shared libraries cannot be stripped! (We only assume
	 * that __DYNAMIC is the first item in the data segment)
	 *
	 * If defined (ie. not relocatable_output), make it look
	 * like an internal symbol.
	 */
	if (dynamic_symbol->referenced) {
		nl.n_other = 0;
		nl.n_desc = 0;
		nl.n_type = dynamic_symbol->defined;
		if (nl.n_type == N_UNDF)
			nl.n_type |= N_EXT;
		else
			nl.n_type &= ~N_EXT;
		nl.n_value = dynamic_symbol->value;
		nl.n_un.n_strx = assign_string_table_index(dynamic_symbol->name);
		*bufp++ = nl;
		syms_written++;
	}

	/* Scan the symbol hash table, bucket by bucket.  */

	FOR_EACH_SYMBOL(i, sp) {
		if (sp == dynamic_symbol)
			/* Already dealt with above */
			continue;

		if (!sp->referenced)
			/* Came from shared object but was not used */
			continue;

		if (sp->so_defined)
			/*
			 * Definition came from shared object,
			 * don't mention it here
			 */
			continue;

		if (!sp->defined && !relocatable_output) {
			/*
			 * We're building a shared object and there
			 * are still undefined symbols. Don't output
			 * these, symbol was discounted in digest_pass1()
			 * (they are in the RRS symbol table).
			 */
			if (!building_shared_object)
				error("symbol %s remains undefined", sp->name);
			continue;
		}

		/* Construct a `struct nlist' for the symbol.  */

		nl.n_other = 0;
		nl.n_desc = 0;

		/*
		 * common condition needs to be before undefined
		 * condition because unallocated commons are set
		 * undefined in digest_symbols
		 */
		if (sp->defined > 1) {
			/* defined with known type */

			if (!relocatable_output && sp->alias &&
						sp->alias->defined > 1) {
				/*
				 * If the target of an indirect symbol has
				 * been defined and we are outputting an
				 * executable, resolve the indirection; it's
				 * no longer needed
				 */
				nl.n_type = sp->alias->defined;
				nl.n_type = sp->alias->value;
			} else if (sp->defined == N_SIZE)
				nl.n_type = N_DATA | N_EXT;
			else
				nl.n_type = sp->defined;
			nl.n_value = sp->value;
		} else if (sp->max_common_size) {
			/*
			 * defined as common but not allocated,
			 * happens only with -r and not -d, write out
			 * a common definition
			 */
			nl.n_type = N_UNDF | N_EXT;
			nl.n_value = sp->max_common_size;
		} else if (!sp->defined) {
			/* undefined -- legit only if -r */
			nl.n_type = N_UNDF | N_EXT;
			nl.n_value = 0;
		} else
			fatal(
			      "internal error: %s defined in mysterious way",
			      sp->name);

		/*
		 * Allocate string table space for the symbol name.
		 */

		nl.n_un.n_strx = assign_string_table_index(sp->name);

		/* Output to the buffer and count it.  */

		if (syms_written >= non_local_syms)
			fatal(
			"internal error: number of symbols exceeds allocated %d",
				non_local_syms);
		*bufp++ = nl;
		syms_written++;

		if (nl.n_type == N_INDR + N_EXT) {
			if (sp->alias == NULL)
				fatal("internal error: alias in hyperspace");
			nl.n_type = N_UNDF + N_EXT;
			nl.n_un.n_strx =
				assign_string_table_index(sp->alias->name);
			nl.n_value = 0;
			*bufp++ = nl;
			syms_written++;
		}

#ifdef DEBUG
printf("writesym(#%d): %s, type %x\n", syms_written, sp->name, sp->defined);
#endif
	} END_EACH_SYMBOL;

	if (syms_written != strtab_index || strtab_index != non_local_syms)
		fatal("internal error:\
wrong number (%d) of global symbols written into output file, should be %d",
				syms_written, non_local_syms);

	/* Output the buffer full of `struct nlist's.  */

	lseek(outdesc, symbol_table_offset + symbol_table_len, 0);
	md_swapout_symbols(buf, bufp - buf);
	mywrite(buf, bufp - buf, sizeof(struct nlist), outdesc);
	symbol_table_len += sizeof(struct nlist) * (bufp - buf);

	/* Write the strings for the global symbols.  */
	write_string_table();

	/* Write the local symbols defined by the various files.  */
	each_file(write_file_syms, &syms_written);
	file_close();

	if (syms_written != nsyms)
		fatal("internal error:\
wrong number of symbols (%d) written into output file, should be %d",
				syms_written, nsyms);

	if (symbol_table_offset + symbol_table_len != string_table_offset)
		fatal(
		"internal error: inconsistent symbol table length: %d vs %s",
		symbol_table_offset + symbol_table_len, string_table_offset);

	lseek(outdesc, string_table_offset, 0);
	strtab_size = md_swap_long(strtab_size);
	mywrite(&strtab_size, sizeof(int), 1, outdesc);
}


/*
 * Write the local and debugger symbols of file ENTRY. Increment
 * *SYMS_WRITTEN_ADDR for each symbol that is written.
 */

/*
 * Note that we do not combine identical names of local symbols. dbx or gdb
 * would be confused if we did that.
 */
void
write_file_syms(entry, syms_written_addr)
	struct file_entry *entry;
	int            *syms_written_addr;
{
	struct localsymbol	*lsp, *lspend;

	/* Upper bound on number of syms to be written here.  */
	int	max_syms = entry->nsymbols + 1;

	/*
	 * Buffer to accumulate all the syms before writing them. It has one
	 * extra slot for the local symbol we generate here.
	 */
	struct nlist *buf = (struct nlist *)
			alloca(max_syms * sizeof(struct nlist));

	register struct nlist *bufp = buf;

	if (entry->is_dynamic)
		return;

	/*
	 * Make tables that record, for each symbol, its name and its name's
	 * length. The elements are filled in by `assign_string_table_index'.
	 */

	strtab_vector = (char **) alloca(max_syms * sizeof(char *));
	strtab_lens = (int *) alloca(max_syms * sizeof(int));
	strtab_index = 0;

	/* Generate a local symbol for the start of this file's text.  */

	if (discard_locals != DISCARD_ALL) {
		struct nlist    nl;

		nl.n_type = N_FN | N_EXT;
		nl.n_un.n_strx = assign_string_table_index(entry->local_sym_name);
		nl.n_value = entry->text_start_address;
		nl.n_desc = 0;
		nl.n_other = 0;
		*bufp++ = nl;
		(*syms_written_addr)++;
		entry->local_syms_offset = *syms_written_addr * sizeof(struct nlist);
	}
	/* Read the file's string table.  */

	entry->strings = (char *) alloca(entry->string_size);
	read_entry_strings(file_open(entry), entry);

	lspend = entry->symbols + entry->nsymbols;

	for (lsp = entry->symbols; lsp < lspend; lsp++) {
		register struct nlist *p = &lsp->nzlist.nlist;
		register int    type = p->n_type;
		register int    write = 0;

		/*
		 * WRITE gets 1 for a non-global symbol that should be
		 * written.
		 */

		if (SET_ELEMENT_P (type))
			/*
			 * This occurs even if global. These types of
			 * symbols are never written globally, though
			 * they are stored globally.
			 */
			write = relocatable_output;
		else if (!(type & (N_STAB | N_EXT)))
			/* ordinary local symbol */
			write = ((discard_locals != DISCARD_ALL)
				 && !(discard_locals == DISCARD_L &&
			    (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
				 && type != N_WARNING);
		else if (!(type & N_EXT))
			/* debugger symbol */
			write = (strip_symbols == STRIP_NONE) &&
				!(discard_locals == DISCARD_L &&
				(p->n_un.n_strx + entry->strings)[0] == LPREFIX);

		if (write) {
			/*
			 * If this symbol has a name, allocate space for it
			 * in the output string table.
			 */

			if (p->n_un.n_strx)
				p->n_un.n_strx = assign_string_table_index(
						p->n_un.n_strx + entry->strings);

			/* Output this symbol to the buffer and count it.  */

			*bufp++ = *p;
			(*syms_written_addr)++;
		}
	}

	/* All the symbols are now in BUF; write them.  */

	lseek(outdesc, symbol_table_offset + symbol_table_len, 0);
	md_swapout_symbols(buf, bufp - buf);
	mywrite(buf, bufp - buf, sizeof(struct nlist), outdesc);
	symbol_table_len += sizeof(struct nlist) * (bufp - buf);

	/*
	 * Write the string-table data for the symbols just written, using
	 * the data in vectors `strtab_vector' and `strtab_lens'.
	 */

	write_string_table();
	entry->strings = 0;	/* Since it will disappear anyway.  */
}