summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sgs/libld/common/update.c
blob: 8ef1be8343a0237c37f7f868278e73088f80a36e (plain)
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
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 *	Copyright (c) 1988 AT&T
 *	  All Rights Reserved
 *
 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * Update the new output file image, perform virtual address, offset and
 * displacement calculations on the program headers and sections headers,
 * and generate any new output section information.
 */

#define	ELF_TARGET_AMD64

#include	<stdio.h>
#include	<string.h>
#include	<unistd.h>
#include	<debug.h>
#include	"msg.h"
#include	"_libld.h"

/*
 * Comparison routine used by qsort() for sorting of the global symbol list
 * based off of the hashbuckets the symbol will eventually be deposited in.
 */
static int
sym_hash_compare(Sym_s_list * s1, Sym_s_list * s2)
{
	return (s1->sl_hval - s2->sl_hval);
}

/*
 * Comparison routine used by qsort() for sorting of dyn[sym|tls]sort section
 * indices based on the address of the symbols they reference. The
 * use of the global dynsort_compare_syms variable is needed because
 * we need to examine the symbols the indices reference. It is safe, because
 * the linker is single threaded.
 */
static Sym *dynsort_compare_syms;

static int
dynsort_compare(const void *idx1, const void *idx2)
{
	Sym *s1 = dynsort_compare_syms + *((const Word *) idx1);
	Sym *s2 = dynsort_compare_syms + *((const Word *) idx2);

	/*
	 * Note: the logical computation for this is
	 *	(st_value1 - st_value2)
	 * However, that is only correct if the address type is smaller
	 * than a pointer. Writing it this way makes it immune to the
	 * class (32 or 64-bit) of the linker.
	 */
	return ((s1->st_value < s2->st_value) ? -1 :
	    (s1->st_value > s2->st_value));
}

/*
 * Scan the sorted symbols, and issue warnings if there are any duplicate
 * values in the list. We only do this if -zverbose is set, or we are
 * running with LD_DEBUG defined
 *
 * entry:
 *	ofl - Output file descriptor
 *	ldynsym - Pointer to start of .SUNW_ldynsym section that the
 *		sort section indexes reference.
 *	symsort - Pointer to start of .SUNW_dynsymsort or .SUNW_dyntlssort
 *		section.
 *	n - # of indices in symsort array
 *	secname - Name of the symsort section.
 *
 * exit:
 *	If the symsort section contains indexes to more than one
 *	symbol with the same address value, a warning is issued.
 */
static void
dynsort_dupwarn(Ofl_desc *ofl, Sym *ldynsym, const char *str,
    Word *symsort, Word n, const char *secname)
{
	int zverbose = (ofl->ofl_flags & FLG_OF_VERBOSE) != 0;
	Word ndx, cmp_ndx;
	Addr addr, cmp_addr;

	/* Nothing to do if -zverbose or LD_DEBUG are not active */
	if (!(zverbose || DBG_ENABLED))
		return;

	cmp_ndx = 0;
	cmp_addr = ldynsym[symsort[cmp_ndx]].st_value;
	for (ndx = 1; ndx < n; ndx++) {
		addr = ldynsym[symsort[ndx]].st_value;
		if (cmp_addr == addr) {
			if (zverbose)
				ld_eprintf(ofl, ERR_WARNING,
				    MSG_INTL(MSG_SYM_DUPSORTADDR), secname,
				    str + ldynsym[symsort[cmp_ndx]].st_name,
				    str + ldynsym[symsort[ndx]].st_name,
				    EC_ADDR(addr));
			DBG_CALL(Dbg_syms_dup_sort_addr(ofl->ofl_lml, secname,
			    str + ldynsym[symsort[cmp_ndx]].st_name,
			    str + ldynsym[symsort[ndx]].st_name,
			    EC_ADDR(addr)));
		} else {	/* Not a dup. Move reference up */
			cmp_ndx = ndx;
			cmp_addr = addr;
		}
	}
}

/*
 * Build and update any output symbol tables.  Here we work on all the symbol
 * tables at once to reduce the duplication of symbol and string manipulation.
 * Symbols and their associated strings are copied from the read-only input
 * file images to the output image and their values and index's updated in the
 * output image.
 */
static Addr
update_osym(Ofl_desc *ofl)
{
	/*
	 * There are several places in this function where we wish
	 * to insert a symbol index to the combined .SUNW_ldynsym/.dynsym
	 * symbol table into one of the two sort sections (.SUNW_dynsymsort
	 * or .SUNW_dyntlssort), if that symbol has the right attributes.
	 * This macro is used to generate the necessary code from a single
	 * specification.
	 *
	 * entry:
	 *	_sdp, _sym, _type - As per DYNSORT_COUNT. See _libld.h
	 *	_sym_ndx - Index that _sym will have in the combined
	 *		.SUNW_ldynsym/.dynsym symbol table.
	 */
#define	ADD_TO_DYNSORT(_sdp, _sym, _type, _sym_ndx) \
	{ \
		Word *_dynsort_arr, *_dynsort_ndx; \
		\
		if (dynsymsort_symtype[_type]) { \
			_dynsort_arr = dynsymsort; \
			_dynsort_ndx = &dynsymsort_ndx; \
		} else if (_type == STT_TLS) { \
			_dynsort_arr = dyntlssort; \
			_dynsort_ndx = &dyntlssort_ndx; \
		} else { \
			_dynsort_arr = NULL; \
		} \
		if ((_dynsort_arr != NULL) && DYNSORT_TEST_ATTR(_sdp, _sym)) \
		    _dynsort_arr[(*_dynsort_ndx)++] = _sym_ndx; \
	}

	Sym_desc	*sdp;
	Sym_avlnode	*sav;
	Sg_desc		*sgp, *tsgp = NULL, *dsgp = NULL, *esgp = NULL;
	Os_desc		*osp, *iosp = NULL, *fosp = NULL;
	Is_desc		*isc;
	Ifl_desc	*ifl;
	Word		bssndx, etext_ndx, edata_ndx = 0, end_ndx, start_ndx;
	Word		end_abs = 0, etext_abs = 0, edata_abs;
	Word		tlsbssndx = 0, parexpnndx;
#if	defined(_ELF64)
	Word		lbssndx = 0;
	Addr		lbssaddr = 0;
#endif
	Addr		bssaddr, etext = 0, edata = 0, end = 0, start = 0;
	Addr		tlsbssaddr = 0;
	Addr		parexpnbase, parexpnaddr;
	int		start_set = 0;
	Sym		_sym = {0}, *sym, *symtab = NULL;
	Sym		*dynsym = NULL, *ldynsym = NULL;
	Word		symtab_ndx = 0;		/* index into .symtab */
	Word		symtab_gbl_bndx;	/* .symtab ndx 1st global */
	Word		ldynsym_ndx = 0;	/* index into .SUNW_ldynsym */
	Word		dynsym_ndx = 0;		/* index into .dynsym */
	Word		scopesym_ndx = 0;	/* index into scoped symbols */
	Word		scopesym_bndx = 0;	/* .symtab ndx 1st scoped sym */
	Word		ldynscopesym_ndx = 0;	/* index to ldynsym scoped */
						/*	symbols */
	Word		*dynsymsort = NULL;	/* SUNW_dynsymsort index */
						/*	vector */
	Word		*dyntlssort = NULL;	/* SUNW_dyntlssort index */
						/*	vector */
	Word		dynsymsort_ndx;		/* index dynsymsort array */
	Word		dyntlssort_ndx;		/* index dyntlssort array */
	Word		*symndx;		/* symbol index (for */
						/*	relocation use) */
	Word		*symshndx = NULL;	/* .symtab_shndx table */
	Word		*dynshndx = NULL;	/* .dynsym_shndx table */
	Word		*ldynshndx = NULL;	/* .SUNW_ldynsym_shndx table */
	Word		ldynsym_cnt = 0;	/* number of items in */
						/*	.SUNW_ldynsym */
	Str_tbl		*shstrtab;
	Str_tbl		*strtab;
	Str_tbl		*dynstr;
	Word		*hashtab;	/* hash table pointer */
	Word		*hashbkt;	/* hash table bucket pointer */
	Word		*hashchain;	/* hash table chain pointer */
	Wk_desc		*wkp;
	Alist		*weak = NULL;
	ofl_flag_t	flags = ofl->ofl_flags;
	Versym		*versym;
	Gottable	*gottable;	/* used for display got debugging */
					/*	information */
	Syminfo		*syminfo;
	Sym_s_list	*sorted_syms;	/* table to hold sorted symbols */
	Word		ssndx;		/* global index into sorted_syms */
	Word		scndx;		/* scoped index into sorted_syms */
	size_t		stoff;		/* string offset */
	Aliste		idx1;

	/*
	 * Initialize pointers to the symbol table entries and the symbol
	 * table strings.  Skip the first symbol entry and the first string
	 * table byte.  Note that if we are not generating any output symbol
	 * tables we must still generate and update internal copies so
	 * that the relocation phase has the correct information.
	 */
	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
		symtab = (Sym *)ofl->ofl_ossymtab->os_outdata->d_buf;
		symtab[symtab_ndx++] = _sym;
		if (ofl->ofl_ossymshndx)
			symshndx =
			    (Word *)ofl->ofl_ossymshndx->os_outdata->d_buf;
	}
	if (OFL_ALLOW_DYNSYM(ofl)) {
		dynsym = (Sym *)ofl->ofl_osdynsym->os_outdata->d_buf;
		dynsym[dynsym_ndx++] = _sym;
		/*
		 * If we are also constructing a .SUNW_ldynsym section
		 * to contain local function symbols, then set it up too.
		 */
		if (ofl->ofl_osldynsym) {
			ldynsym = (Sym *)ofl->ofl_osldynsym->os_outdata->d_buf;
			ldynsym[ldynsym_ndx++] = _sym;
			ldynsym_cnt = 1 + ofl->ofl_dynlocscnt +
			    ofl->ofl_dynscopecnt;

			/*
			 * If there is a SUNW_ldynsym, then there may also
			 * be a .SUNW_dynsymsort and/or .SUNW_dyntlssort
			 * sections, used to collect indices of function
			 * and data symbols sorted by address order.
			 */
			if (ofl->ofl_osdynsymsort) {	/* .SUNW_dynsymsort */
				dynsymsort = (Word *)
				    ofl->ofl_osdynsymsort->os_outdata->d_buf;
				dynsymsort_ndx = 0;
			}
			if (ofl->ofl_osdyntlssort) {	/* .SUNW_dyntlssort */
				dyntlssort = (Word *)
				    ofl->ofl_osdyntlssort->os_outdata->d_buf;
				dyntlssort_ndx = 0;
			}
		}

		/*
		 * Initialize the hash table.
		 */
		hashtab = (Word *)(ofl->ofl_oshash->os_outdata->d_buf);
		hashbkt = &hashtab[2];
		hashchain = &hashtab[2 + ofl->ofl_hashbkts];
		hashtab[0] = ofl->ofl_hashbkts;
		hashtab[1] = DYNSYM_ALL_CNT(ofl);
		if (ofl->ofl_osdynshndx)
			dynshndx =
			    (Word *)ofl->ofl_osdynshndx->os_outdata->d_buf;
		if (ofl->ofl_osldynshndx)
			ldynshndx =
			    (Word *)ofl->ofl_osldynshndx->os_outdata->d_buf;
	}

	/*
	 * symndx is the symbol index to be used for relocation processing.  It
	 * points to the relevant symtab's (.dynsym or .symtab) symbol ndx.
	 */
	if (dynsym)
		symndx = &dynsym_ndx;
	else
		symndx = &symtab_ndx;

	/*
	 * If we have version definitions initialize the version symbol index
	 * table.  There is one entry for each symbol which contains the symbols
	 * version index.
	 */
	if (!(flags & FLG_OF_NOVERSEC) &&
	    (flags & (FLG_OF_VERNEED | FLG_OF_VERDEF))) {
		versym = (Versym *)ofl->ofl_osversym->os_outdata->d_buf;
		versym[0] = 0;
	} else
		versym = NULL;

	/*
	 * If syminfo section exists be prepared to fill it in.
	 */
	if (ofl->ofl_ossyminfo) {
		syminfo = ofl->ofl_ossyminfo->os_outdata->d_buf;
		syminfo[0].si_flags = SYMINFO_CURRENT;
	} else
		syminfo = NULL;

	/*
	 * Setup our string tables.
	 */
	shstrtab = ofl->ofl_shdrsttab;
	strtab = ofl->ofl_strtab;
	dynstr = ofl->ofl_dynstrtab;

	DBG_CALL(Dbg_syms_sec_title(ofl->ofl_lml));

	/*
	 * Put output file name to the first .symtab and .SUNW_ldynsym symbol.
	 */
	if (symtab) {
		(void) st_setstring(strtab, ofl->ofl_name, &stoff);
		sym = &symtab[symtab_ndx++];
		/* LINTED */
		sym->st_name = stoff;
		sym->st_value = 0;
		sym->st_size = 0;
		sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
		sym->st_other = 0;
		sym->st_shndx = SHN_ABS;

		if (versym && !dynsym)
			versym[1] = 0;
	}
	if (ldynsym) {
		(void) st_setstring(dynstr, ofl->ofl_name, &stoff);
		sym = &ldynsym[ldynsym_ndx];
		/* LINTED */
		sym->st_name = stoff;
		sym->st_value = 0;
		sym->st_size = 0;
		sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
		sym->st_other = 0;
		sym->st_shndx = SHN_ABS;

		/* Scoped symbols get filled in global loop below */
		ldynscopesym_ndx = ldynsym_ndx + 1;
		ldynsym_ndx += ofl->ofl_dynscopecnt;
	}

	/*
	 * If we are to display GOT summary information, then allocate
	 * the buffer to 'cache' the GOT symbols into now.
	 */
	if (DBG_ENABLED) {
		if ((ofl->ofl_gottable = gottable =
		    libld_calloc(ofl->ofl_gotcnt, sizeof (Gottable))) == NULL)
			return ((Addr)S_ERROR);
	}

	/*
	 * Traverse the program headers.  Determine the last executable segment
	 * and the last data segment so that we can update etext and edata. If
	 * we have empty segments (reservations) record them for setting _end.
	 */
	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
		Phdr	*phd = &(sgp->sg_phdr);
		Os_desc	*osp;
		Aliste	idx2;

		if (phd->p_type == PT_LOAD) {
			if (sgp->sg_osdescs != NULL) {
				Word	_flags = phd->p_flags & (PF_W | PF_R);

				if (_flags == PF_R)
					tsgp = sgp;
				else if (_flags == (PF_W | PF_R))
					dsgp = sgp;
			} else if (sgp->sg_flags & FLG_SG_EMPTY)
				esgp = sgp;
		}

		/*
		 * Generate a section symbol for each output section.
		 */
		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
			Word	sectndx;

			sym = &_sym;
			sym->st_value = osp->os_shdr->sh_addr;
			sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION);
			/* LINTED */
			sectndx = elf_ndxscn(osp->os_scn);

			if (symtab) {
				if (sectndx >= SHN_LORESERVE) {
					symshndx[symtab_ndx] = sectndx;
					sym->st_shndx = SHN_XINDEX;
				} else {
					/* LINTED */
					sym->st_shndx = (Half)sectndx;
				}
				symtab[symtab_ndx++] = *sym;
			}

			if (dynsym && (osp->os_flags & FLG_OS_OUTREL))
				dynsym[dynsym_ndx++] = *sym;

			if ((dynsym == NULL) ||
			    (osp->os_flags & FLG_OS_OUTREL)) {
				if (versym)
					versym[*symndx - 1] = 0;
				osp->os_identndx = *symndx - 1;
				DBG_CALL(Dbg_syms_sec_entry(ofl->ofl_lml,
				    osp->os_identndx, sgp, osp));
			}

			/*
			 * Generate the .shstrtab for this section.
			 */
			(void) st_setstring(shstrtab, osp->os_name, &stoff);
			osp->os_shdr->sh_name = (Word)stoff;

			/*
			 * Find the section index for our special symbols.
			 */
			if (sgp == tsgp) {
				/* LINTED */
				etext_ndx = elf_ndxscn(osp->os_scn);
			} else if (dsgp == sgp) {
				if (osp->os_shdr->sh_type != SHT_NOBITS) {
					/* LINTED */
					edata_ndx = elf_ndxscn(osp->os_scn);
				}
			}

			if (start_set == 0) {
				start = sgp->sg_phdr.p_vaddr;
				/* LINTED */
				start_ndx = elf_ndxscn(osp->os_scn);
				start_set++;
			}

			/*
			 * While we're here, determine whether a .init or .fini
			 * section exist.
			 */
			if ((iosp == NULL) && (strcmp(osp->os_name,
			    MSG_ORIG(MSG_SCN_INIT)) == 0))
				iosp = osp;
			if ((fosp == NULL) && (strcmp(osp->os_name,
			    MSG_ORIG(MSG_SCN_FINI)) == 0))
				fosp = osp;
		}
	}

	/*
	 * Add local register symbols to the .dynsym.  These are required as
	 * DT_REGISTER .dynamic entries must have a symbol to reference.
	 */
	if (ofl->ofl_regsyms && dynsym) {
		int	ndx;

		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
			Sym_desc	*rsdp;

			if ((rsdp = ofl->ofl_regsyms[ndx]) == NULL)
				continue;

			if (!SYM_IS_HIDDEN(rsdp) &&
			    (ELF_ST_BIND(rsdp->sd_sym->st_info) != STB_LOCAL))
				continue;

			dynsym[dynsym_ndx] = *(rsdp->sd_sym);
			rsdp->sd_symndx = *symndx;

			if (dynsym[dynsym_ndx].st_name) {
				(void) st_setstring(dynstr, rsdp->sd_name,
				    &stoff);
				dynsym[dynsym_ndx].st_name = stoff;
			}
			dynsym_ndx++;
		}
	}

	/*
	 * Having traversed all the output segments, warn the user if the
	 * traditional text or data segments don't exist.  Otherwise from these
	 * segments establish the values for `etext', `edata', `end', `END',
	 * and `START'.
	 */
	if (!(flags & FLG_OF_RELOBJ)) {
		Sg_desc	*sgp;

		if (tsgp)
			etext = tsgp->sg_phdr.p_vaddr + tsgp->sg_phdr.p_filesz;
		else {
			etext = (Addr)0;
			etext_ndx = SHN_ABS;
			etext_abs = 1;
			if (flags & FLG_OF_VERBOSE)
				ld_eprintf(ofl, ERR_WARNING,
				    MSG_INTL(MSG_UPD_NOREADSEG));
		}
		if (dsgp) {
			edata = dsgp->sg_phdr.p_vaddr + dsgp->sg_phdr.p_filesz;
		} else {
			edata = (Addr)0;
			edata_ndx = SHN_ABS;
			edata_abs = 1;
			if (flags & FLG_OF_VERBOSE)
				ld_eprintf(ofl, ERR_WARNING,
				    MSG_INTL(MSG_UPD_NORDWRSEG));
		}

		if (dsgp == NULL) {
			if (tsgp)
				sgp = tsgp;
			else
				sgp = 0;
		} else if (tsgp == NULL)
			sgp = dsgp;
		else if (dsgp->sg_phdr.p_vaddr > tsgp->sg_phdr.p_vaddr)
			sgp = dsgp;
		else if (dsgp->sg_phdr.p_vaddr < tsgp->sg_phdr.p_vaddr)
			sgp = tsgp;
		else {
			/*
			 * One of the segments must be of zero size.
			 */
			if (tsgp->sg_phdr.p_memsz)
				sgp = tsgp;
			else
				sgp = dsgp;
		}

		if (esgp && (esgp->sg_phdr.p_vaddr > sgp->sg_phdr.p_vaddr))
			sgp = esgp;

		if (sgp) {
			end = sgp->sg_phdr.p_vaddr + sgp->sg_phdr.p_memsz;

			/*
			 * If the last loadable segment is a read-only segment,
			 * then the application which uses the symbol _end to
			 * find the beginning of writable heap area may cause
			 * segmentation violation. We adjust the value of the
			 * _end to skip to the next page boundary.
			 *
			 * 6401812 System interface which returs beginning
			 *	   heap would be nice.
			 * When the above RFE is implemented, the changes below
			 * could be changed in a better way.
			 */
			if ((sgp->sg_phdr.p_flags & PF_W) == 0)
				end = (Addr)S_ROUND(end, sysconf(_SC_PAGESIZE));

			/*
			 * If we're dealing with a memory reservation there are
			 * no sections to establish an index for _end, so assign
			 * it as an absolute.
			 */
			if (sgp->sg_osdescs != NULL) {
				/*
				 * Determine the last section for this segment.
				 */
				Os_desc	*osp = sgp->sg_osdescs->apl_data
				    [sgp->sg_osdescs->apl_nitems - 1];

				/* LINTED */
				end_ndx = elf_ndxscn(osp->os_scn);
			} else {
				end_ndx = SHN_ABS;
				end_abs = 1;
			}
		} else {
			end = (Addr) 0;
			end_ndx = SHN_ABS;
			end_abs = 1;
			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_UPD_NOSEG));
		}
	}

	/*
	 * Initialize the scoped symbol table entry point.  This is for all
	 * the global symbols that have been scoped to locals and will be
	 * filled in during global symbol processing so that we don't have
	 * to traverse the globals symbol hash array more than once.
	 */
	if (symtab) {
		scopesym_bndx = symtab_ndx;
		scopesym_ndx = scopesym_bndx;
		symtab_ndx += ofl->ofl_scopecnt;
	}

	/*
	 * If expanding partially expanded symbols under '-z nopartial',
	 * prepare to do that.
	 */
	if (ofl->ofl_isparexpn) {
		osp = ofl->ofl_isparexpn->is_osdesc;
		parexpnbase = parexpnaddr = (Addr)(osp->os_shdr->sh_addr +
		    ofl->ofl_isparexpn->is_indata->d_off);
		/* LINTED */
		parexpnndx = elf_ndxscn(osp->os_scn);
		ofl->ofl_parexpnndx = osp->os_identndx;
	}

	/*
	 * If we are generating a .symtab collect all the local symbols,
	 * assigning a new virtual address or displacement (value).
	 */
	for (APLIST_TRAVERSE(ofl->ofl_objs, idx1, ifl)) {
		Xword		lndx, local = ifl->ifl_locscnt;
		Cap_desc	*cdp = ifl->ifl_caps;

		for (lndx = 1; lndx < local; lndx++) {
			Gotndx		*gnp;
			uchar_t		type;
			Word		*_symshndx;
			int		enter_in_symtab, enter_in_ldynsym;
			int		update_done;

			sdp = ifl->ifl_oldndx[lndx];
			sym = sdp->sd_sym;

			/*
			 * Assign a got offset if necessary.
			 */
			if ((ld_targ.t_mr.mr_assign_got != NULL) &&
			    (*ld_targ.t_mr.mr_assign_got)(ofl, sdp) == S_ERROR)
				return ((Addr)S_ERROR);

			if (DBG_ENABLED) {
				Aliste	idx2;

				for (ALIST_TRAVERSE(sdp->sd_GOTndxs,
				    idx2, gnp)) {
					gottable->gt_sym = sdp;
					gottable->gt_gndx.gn_gotndx =
					    gnp->gn_gotndx;
					gottable->gt_gndx.gn_addend =
					    gnp->gn_addend;
					gottable++;
				}
			}

			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION)
				continue;

			/*
			 * Ignore any symbols that have been marked as invalid
			 * during input processing.  Providing these aren't used
			 * for relocation they'll just be dropped from the
			 * output image.
			 */
			if (sdp->sd_flags & FLG_SY_INVALID)
				continue;

			/*
			 * If the section that this symbol was associated
			 * with has been discarded - then we discard
			 * the local symbol along with it.
			 */
			if (sdp->sd_flags & FLG_SY_ISDISC)
				continue;

			/*
			 * If this symbol is from a different file
			 * than the input descriptor we are processing,
			 * treat it as if it has FLG_SY_ISDISC set.
			 * This happens when sloppy_comdat_reloc()
			 * replaces a symbol to a discarded comdat section
			 * with an equivalent symbol from a different
			 * file. We only want to enter such a symbol
			 * once --- as part of the file that actually
			 * supplies it.
			 */
			if (ifl != sdp->sd_file)
				continue;

			/*
			 * Generate an output symbol to represent this input
			 * symbol.  Even if the symbol table is to be stripped
			 * we still need to update any local symbols that are
			 * used during relocation.
			 */
			enter_in_symtab = symtab &&
			    (!(ofl->ofl_flags & FLG_OF_REDLSYM) ||
			    sdp->sd_move);
			enter_in_ldynsym = ldynsym &&
			    ((sym->st_name != 0) || (type == STT_FILE)) &&
			    ldynsym_symtype[type] &&
			    !(ofl->ofl_flags & FLG_OF_REDLSYM);

			_symshndx = NULL;

			if (enter_in_symtab) {
				if (!dynsym)
					sdp->sd_symndx = *symndx;
				symtab[symtab_ndx] = *sym;

				/*
				 * Provided this isn't an unnamed register
				 * symbol, update its name.
				 */
				if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
				    symtab[symtab_ndx].st_name) {
					(void) st_setstring(strtab,
					    sdp->sd_name, &stoff);
					symtab[symtab_ndx].st_name = stoff;
				}
				sdp->sd_flags &= ~FLG_SY_CLEAN;
				if (symshndx)
					_symshndx = &symshndx[symtab_ndx];
				sdp->sd_sym = sym = &symtab[symtab_ndx++];

				if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
				    (sym->st_shndx == SHN_ABS) &&
				    !enter_in_ldynsym)
					continue;
			} else if (enter_in_ldynsym) {
				/*
				 * Not using symtab, but we do have ldynsym
				 * available.
				 */
				ldynsym[ldynsym_ndx] = *sym;
				(void) st_setstring(dynstr, sdp->sd_name,
				    &stoff);
				ldynsym[ldynsym_ndx].st_name = stoff;

				sdp->sd_flags &= ~FLG_SY_CLEAN;
				if (ldynshndx)
					_symshndx = &ldynshndx[ldynsym_ndx];
				sdp->sd_sym = sym = &ldynsym[ldynsym_ndx];
				/* Add it to sort section if it qualifies */
				ADD_TO_DYNSORT(sdp, sym, type, ldynsym_ndx);
				ldynsym_ndx++;
			} else {	/* Not using symtab or ldynsym */
				/*
				 * If this symbol requires modifying to provide
				 * for a relocation or move table update, make
				 * a copy of it.
				 */
				if (!(sdp->sd_flags & FLG_SY_UPREQD) &&
				    !(sdp->sd_move))
					continue;
				if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
				    (sym->st_shndx == SHN_ABS))
					continue;

				if (ld_sym_copy(sdp) == S_ERROR)
					return ((Addr)S_ERROR);
				sym = sdp->sd_sym;
			}

			/*
			 * Update the symbols contents if necessary.
			 */
			update_done = 0;
			if (type == STT_FILE) {
				sdp->sd_shndx = sym->st_shndx = SHN_ABS;
				sdp->sd_flags |= FLG_SY_SPECSEC;
				update_done = 1;
			}

			/*
			 * If we are expanding the locally bound partially
			 * initialized symbols, then update the address here.
			 */
			if (ofl->ofl_isparexpn &&
			    (sdp->sd_flags & FLG_SY_PAREXPN) && !update_done) {
				sym->st_shndx = parexpnndx;
				sdp->sd_isc = ofl->ofl_isparexpn;
				sym->st_value = parexpnaddr;
				parexpnaddr += sym->st_size;
				if ((flags & FLG_OF_RELOBJ) == 0)
					sym->st_value -= parexpnbase;
			}

			/*
			 * If this isn't an UNDEF symbol (ie. an input section
			 * is associated), update the symbols value and index.
			 */
			if (((isc = sdp->sd_isc) != NULL) && !update_done) {
				Word	sectndx;

				osp = isc->is_osdesc;
				/* LINTED */
				sym->st_value +=
				    (Off)_elf_getxoff(isc->is_indata);
				if ((flags & FLG_OF_RELOBJ) == 0) {
					sym->st_value += osp->os_shdr->sh_addr;
					/*
					 * TLS symbols are relative to
					 * the TLS segment.
					 */
					if ((type == STT_TLS) &&
					    (ofl->ofl_tlsphdr)) {
						sym->st_value -=
						    ofl->ofl_tlsphdr->p_vaddr;
					}
				}
				/* LINTED */
				if ((sdp->sd_shndx = sectndx =
				    elf_ndxscn(osp->os_scn)) >= SHN_LORESERVE) {
					if (_symshndx) {
						*_symshndx = sectndx;
					}
					sym->st_shndx = SHN_XINDEX;
				} else {
					/* LINTED */
					sym->st_shndx = sectndx;
				}
			}

			/*
			 * If entering the symbol in both the symtab and the
			 * ldynsym, then the one in symtab needs to be
			 * copied to ldynsym. If it is only in the ldynsym,
			 * then the code above already set it up and we have
			 * nothing more to do here.
			 */
			if (enter_in_symtab && enter_in_ldynsym) {
				ldynsym[ldynsym_ndx] = *sym;
				(void) st_setstring(dynstr, sdp->sd_name,
				    &stoff);
				ldynsym[ldynsym_ndx].st_name = stoff;

				if (_symshndx && ldynshndx)
					ldynshndx[ldynsym_ndx] = *_symshndx;

				/* Add it to sort section if it qualifies */
				ADD_TO_DYNSORT(sdp, sym, type, ldynsym_ndx);

				ldynsym_ndx++;
			}
		}

		/*
		 * If this input file has undergone object to symbol
		 * capabilities conversion, supply any new capabilities symbols.
		 * These symbols are copies of the original global symbols, and
		 * follow the existing local symbols that are supplied from this
		 * input file (which are identified with a preceding STT_FILE).
		 */
		if (symtab && cdp && cdp->ca_syms) {
			Aliste		idx2;
			Cap_sym		*csp;

			for (APLIST_TRAVERSE(cdp->ca_syms, idx2, csp)) {
				Is_desc	*isp;

				sdp = csp->cs_sdp;
				sym = sdp->sd_sym;

				if ((isp = sdp->sd_isc) != NULL) {
					Os_desc	*osp = isp->is_osdesc;

					/*
					 * Update the symbols value.
					 */
					/* LINTED */
					sym->st_value +=
					    (Off)_elf_getxoff(isp->is_indata);
					if ((flags & FLG_OF_RELOBJ) == 0)
						sym->st_value +=
						    osp->os_shdr->sh_addr;

					/*
					 * Update the symbols section index.
					 */
					sdp->sd_shndx = sym->st_shndx =
					    elf_ndxscn(osp->os_scn);
				}

				symtab[symtab_ndx] = *sym;
				(void) st_setstring(strtab, sdp->sd_name,
				    &stoff);
				symtab[symtab_ndx].st_name = stoff;
				sdp->sd_symndx = symtab_ndx++;
			}
		}
	}

	symtab_gbl_bndx = symtab_ndx;	/* .symtab index of 1st global entry */

	/*
	 * Two special symbols are `_init' and `_fini'.  If these are supplied
	 * by crti.o then they are used to represent the total concatenation of
	 * the `.init' and `.fini' sections.
	 *
	 * Determine whether any .init or .fini sections exist.  If these
	 * sections exist and a dynamic object is being built, but no `_init'
	 * or `_fini' symbols are found, then the user is probably building
	 * this object directly from ld(1) rather than using a compiler driver
	 * that provides the symbols via crt's.
	 *
	 * If the .init or .fini section exist, and their associated symbols,
	 * determine the size of the sections and updated the symbols value
	 * accordingly.
	 */
	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U), SYM_NOHASH, 0,
	    ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) && sdp->sd_isc &&
	    (sdp->sd_isc->is_osdesc == iosp)) {
		if (ld_sym_copy(sdp) == S_ERROR)
			return ((Addr)S_ERROR);
		sdp->sd_sym->st_size = sdp->sd_isc->is_osdesc->os_shdr->sh_size;

	} else if (iosp && !(flags & FLG_OF_RELOBJ)) {
		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_NOCRT),
		    MSG_ORIG(MSG_SYM_INIT_U), MSG_ORIG(MSG_SCN_INIT));
	}

	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U), SYM_NOHASH, 0,
	    ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) && sdp->sd_isc &&
	    (sdp->sd_isc->is_osdesc == fosp)) {
		if (ld_sym_copy(sdp) == S_ERROR)
			return ((Addr)S_ERROR);
		sdp->sd_sym->st_size = sdp->sd_isc->is_osdesc->os_shdr->sh_size;

	} else if (fosp && !(flags & FLG_OF_RELOBJ)) {
		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_NOCRT),
		    MSG_ORIG(MSG_SYM_FINI_U), MSG_ORIG(MSG_SCN_FINI));
	}

	/*
	 * Assign .bss information for use with updating COMMON symbols.
	 */
	if (ofl->ofl_isbss) {
		isc = ofl->ofl_isbss;
		osp = isc->is_osdesc;

		bssaddr = osp->os_shdr->sh_addr +
		    (Off)_elf_getxoff(isc->is_indata);
		/* LINTED */
		bssndx = elf_ndxscn(osp->os_scn);
	}

#if	defined(_ELF64)
	/*
	 * For amd64 target, assign .lbss information for use
	 * with updating LCOMMON symbols.
	 */
	if ((ld_targ.t_m.m_mach == EM_AMD64) && ofl->ofl_islbss) {
		osp = ofl->ofl_islbss->is_osdesc;

		lbssaddr = osp->os_shdr->sh_addr +
		    (Off)_elf_getxoff(ofl->ofl_islbss->is_indata);
		/* LINTED */
		lbssndx = elf_ndxscn(osp->os_scn);
	}
#endif
	/*
	 * Assign .tlsbss information for use with updating COMMON symbols.
	 */
	if (ofl->ofl_istlsbss) {
		osp = ofl->ofl_istlsbss->is_osdesc;
		tlsbssaddr = osp->os_shdr->sh_addr +
		    (Off)_elf_getxoff(ofl->ofl_istlsbss->is_indata);
		/* LINTED */
		tlsbssndx = elf_ndxscn(osp->os_scn);
	}

	if ((sorted_syms = libld_calloc(ofl->ofl_globcnt +
	    ofl->ofl_elimcnt + ofl->ofl_scopecnt,
	    sizeof (*sorted_syms))) == NULL)
		return ((Addr)S_ERROR);

	scndx = 0;
	ssndx = ofl->ofl_scopecnt + ofl->ofl_elimcnt;

	DBG_CALL(Dbg_syms_up_title(ofl->ofl_lml));

	/*
	 * Traverse the internal symbol table updating global symbol information
	 * and allocating common.
	 */
	for (sav = avl_first(&ofl->ofl_symavl); sav;
	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
		Sym	*symptr;
		int	local;
		int	restore;

		sdp = sav->sav_sdp;

		/*
		 * Ignore any symbols that have been marked as invalid during
		 * input processing.  Providing these aren't used for
		 * relocation, they will be dropped from the output image.
		 */
		if (sdp->sd_flags & FLG_SY_INVALID) {
			DBG_CALL(Dbg_syms_old(ofl, sdp));
			DBG_CALL(Dbg_syms_ignore(ofl, sdp));
			continue;
		}

		/*
		 * Only needed symbols are copied to the output symbol table.
		 */
		if (sdp->sd_ref == REF_DYN_SEEN)
			continue;

		if (ld_sym_reducable(ofl, sdp))
			local = 1;
		else
			local = 0;

		if (local || (ofl->ofl_hashbkts == 0)) {
			sorted_syms[scndx++].sl_sdp = sdp;
		} else {
			sorted_syms[ssndx].sl_hval = sdp->sd_aux->sa_hash %
			    ofl->ofl_hashbkts;
			sorted_syms[ssndx].sl_sdp = sdp;
			ssndx++;
		}

		/*
		 * Note - expand the COMMON symbols here because an address
		 * must be assigned to them in the same order that space was
		 * calculated in sym_validate().  If this ordering isn't
		 * followed differing alignment requirements can throw us all
		 * out of whack.
		 *
		 * The expanded .bss global symbol is handled here as well.
		 *
		 * The actual adding entries into the symbol table still occurs
		 * below in hashbucket order.
		 */
		symptr = sdp->sd_sym;
		restore = 0;
		if ((sdp->sd_flags & FLG_SY_PAREXPN) ||
		    ((sdp->sd_flags & FLG_SY_SPECSEC) &&
		    (sdp->sd_shndx = symptr->st_shndx) == SHN_COMMON)) {

			/*
			 * An expanded symbol goes to a special .data section
			 * prepared for that purpose (ofl->ofl_isparexpn).
			 * Assign COMMON allocations to .bss.
			 * Otherwise leave it as is.
			 */
			if (sdp->sd_flags & FLG_SY_PAREXPN) {
				restore = 1;
				sdp->sd_shndx = parexpnndx;
				sdp->sd_flags &= ~FLG_SY_SPECSEC;
				symptr->st_value = (Xword) S_ROUND(
				    parexpnaddr, symptr->st_value);
				parexpnaddr = symptr->st_value +
				    symptr->st_size;
				sdp->sd_isc = ofl->ofl_isparexpn;
				sdp->sd_flags |= FLG_SY_COMMEXP;

			} else if (ELF_ST_TYPE(symptr->st_info) != STT_TLS &&
			    (local || !(flags & FLG_OF_RELOBJ))) {
				restore = 1;
				sdp->sd_shndx = bssndx;
				sdp->sd_flags &= ~FLG_SY_SPECSEC;
				symptr->st_value = (Xword)S_ROUND(bssaddr,
				    symptr->st_value);
				bssaddr = symptr->st_value + symptr->st_size;
				sdp->sd_isc = ofl->ofl_isbss;
				sdp->sd_flags |= FLG_SY_COMMEXP;

			} else if (ELF_ST_TYPE(symptr->st_info) == STT_TLS &&
			    (local || !(flags & FLG_OF_RELOBJ))) {
				restore = 1;
				sdp->sd_shndx = tlsbssndx;
				sdp->sd_flags &= ~FLG_SY_SPECSEC;
				symptr->st_value = (Xword)S_ROUND(tlsbssaddr,
				    symptr->st_value);
				tlsbssaddr = symptr->st_value + symptr->st_size;
				sdp->sd_isc = ofl->ofl_istlsbss;
				sdp->sd_flags |= FLG_SY_COMMEXP;
				/*
				 * TLS symbols are relative to the TLS segment.
				 */
				symptr->st_value -= ofl->ofl_tlsphdr->p_vaddr;
			}
#if	defined(_ELF64)
		} else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
		    (sdp->sd_flags & FLG_SY_SPECSEC) &&
		    ((sdp->sd_shndx = symptr->st_shndx) ==
		    SHN_X86_64_LCOMMON) &&
		    ((local || !(flags & FLG_OF_RELOBJ)))) {
			restore = 1;
			sdp->sd_shndx = lbssndx;
			sdp->sd_flags &= ~FLG_SY_SPECSEC;
			symptr->st_value = (Xword)S_ROUND(lbssaddr,
			    symptr->st_value);
			lbssaddr = symptr->st_value + symptr->st_size;
			sdp->sd_isc = ofl->ofl_islbss;
			sdp->sd_flags |= FLG_SY_COMMEXP;
#endif
		}

		if (restore != 0) {
			uchar_t		type, bind;

			/*
			 * Make sure this COMMON symbol is returned to the same
			 * binding as was defined in the original relocatable
			 * object reference.
			 */
			type = ELF_ST_TYPE(symptr->st_info);
			if (sdp->sd_flags & FLG_SY_GLOBREF)
				bind = STB_GLOBAL;
			else
				bind = STB_WEAK;

			symptr->st_info = ELF_ST_INFO(bind, type);
		}
	}

	/*
	 * If this is a dynamic object then add any local capabilities symbols.
	 */
	if (dynsym && ofl->ofl_capfamilies) {
		Cap_avlnode	*cav;

		for (cav = avl_first(ofl->ofl_capfamilies); cav;
		    cav = AVL_NEXT(ofl->ofl_capfamilies, cav)) {
			Cap_sym		*csp;
			Aliste		idx;

			for (APLIST_TRAVERSE(cav->cn_members, idx, csp)) {
				sdp = csp->cs_sdp;

				DBG_CALL(Dbg_syms_created(ofl->ofl_lml,
				    sdp->sd_name));
				DBG_CALL(Dbg_syms_entered(ofl, sdp->sd_sym,
				    sdp));

				dynsym[dynsym_ndx] = *sdp->sd_sym;

				(void) st_setstring(dynstr, sdp->sd_name,
				    &stoff);
				dynsym[dynsym_ndx].st_name = stoff;

				sdp->sd_sym = &dynsym[dynsym_ndx];
				sdp->sd_symndx = dynsym_ndx;

				/*
				 * Indicate that this is a capabilities symbol.
				 * Note, that this identification only provides
				 * information regarding the symbol that is
				 * visible from elfdump(1) -y.  The association
				 * of a symbol to its capabilities is derived
				 * from a .SUNW_capinfo entry.
				 */
				if (syminfo) {
					syminfo[dynsym_ndx].si_flags |=
					    SYMINFO_FLG_CAP;
				}

				dynsym_ndx++;
			}
		}
	}

	if (ofl->ofl_hashbkts) {
		qsort(sorted_syms + ofl->ofl_scopecnt + ofl->ofl_elimcnt,
		    ofl->ofl_globcnt, sizeof (Sym_s_list),
		    (int (*)(const void *, const void *))sym_hash_compare);
	}

	for (ssndx = 0; ssndx < (ofl->ofl_elimcnt + ofl->ofl_scopecnt +
	    ofl->ofl_globcnt); ssndx++) {
		const char	*name;
		Sym		*sym;
		Sym_aux		*sap;
		Half		spec;
		int		local = 0, dynlocal = 0, enter_in_symtab;
		Gotndx		*gnp;
		Word		sectndx;

		sdp = sorted_syms[ssndx].sl_sdp;
		sectndx = 0;

		if (symtab)
			enter_in_symtab = 1;
		else
			enter_in_symtab = 0;

		/*
		 * Assign a got offset if necessary.
		 */
		if ((ld_targ.t_mr.mr_assign_got != NULL) &&
		    (*ld_targ.t_mr.mr_assign_got)(ofl, sdp) == S_ERROR)
			return ((Addr)S_ERROR);

		if (DBG_ENABLED) {
			Aliste	idx2;

			for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx2, gnp)) {
				gottable->gt_sym = sdp;
				gottable->gt_gndx.gn_gotndx = gnp->gn_gotndx;
				gottable->gt_gndx.gn_addend = gnp->gn_addend;
				gottable++;
			}

			if (sdp->sd_aux && sdp->sd_aux->sa_PLTGOTndx) {
				gottable->gt_sym = sdp;
				gottable->gt_gndx.gn_gotndx =
				    sdp->sd_aux->sa_PLTGOTndx;
				gottable++;
			}
		}

		/*
		 * If this symbol has been marked as being reduced to local
		 * scope then it will have to be placed in the scoped portion
		 * of the .symtab.  Retain the appropriate index for use in
		 * version symbol indexing and relocation.
		 */
		if (ld_sym_reducable(ofl, sdp)) {
			local = 1;
			if (!(sdp->sd_flags & FLG_SY_ELIM) && !dynsym)
				sdp->sd_symndx = scopesym_ndx;
			else
				sdp->sd_symndx = 0;

			if (sdp->sd_flags & FLG_SY_ELIM) {
				enter_in_symtab = 0;
			} else if (ldynsym && sdp->sd_sym->st_name &&
			    ldynsym_symtype[
			    ELF_ST_TYPE(sdp->sd_sym->st_info)]) {
				dynlocal = 1;
			}
		} else {
			sdp->sd_symndx = *symndx;
		}

		/*
		 * Copy basic symbol and string information.
		 */
		name = sdp->sd_name;
		sap = sdp->sd_aux;

		/*
		 * If we require to record version symbol indexes, update the
		 * associated version symbol information for all defined
		 * symbols.  If a version definition is required any zero value
		 * symbol indexes would have been flagged as undefined symbol
		 * errors, however if we're just scoping these need to fall into
		 * the base of global symbols.
		 */
		if (sdp->sd_symndx && versym) {
			Half	vndx = 0;

			if (sdp->sd_flags & FLG_SY_MVTOCOMM) {
				vndx = VER_NDX_GLOBAL;
			} else if (sdp->sd_ref == REF_REL_NEED) {
				vndx = sap->sa_overndx;

				if ((vndx == 0) &&
				    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
					if (SYM_IS_HIDDEN(sdp))
						vndx = VER_NDX_LOCAL;
					else
						vndx = VER_NDX_GLOBAL;
				}
			} else if ((sdp->sd_ref == REF_DYN_NEED) &&
			    (sap->sa_dverndx > 0) &&
			    (sap->sa_dverndx <= sdp->sd_file->ifl_vercnt) &&
			    (sdp->sd_file->ifl_verndx != NULL)) {
				/* Use index of verneed record */
				vndx = sdp->sd_file->ifl_verndx
				    [sap->sa_dverndx].vi_overndx;
			}
			versym[sdp->sd_symndx] = vndx;
		}

		/*
		 * If we are creating the .syminfo section then set per symbol
		 * flags here.
		 */
		if (sdp->sd_symndx && syminfo &&
		    !(sdp->sd_flags & FLG_SY_NOTAVAIL)) {
			int	ndx = sdp->sd_symndx;
			APlist	**alpp = &(ofl->ofl_symdtent);

			if (sdp->sd_flags & FLG_SY_MVTOCOMM)
				/*
				 * Identify a copy relocation symbol.
				 */
				syminfo[ndx].si_flags |= SYMINFO_FLG_COPY;

			if (sdp->sd_ref == REF_DYN_NEED) {
				/*
				 * A reference is bound to a needed dependency.
				 * Save the syminfo entry, so that when the
				 * .dynamic section has been updated, a
				 * DT_NEEDED entry can be associated
				 * (see update_osyminfo()).
				 */
				if (aplist_append(alpp, sdp,
				    AL_CNT_OFL_SYMINFOSYMS) == NULL)
					return (0);

				/*
				 * Flag that the symbol has a direct association
				 * with the external reference (this is an old
				 * tagging, that has no real effect by itself).
				 */
				syminfo[ndx].si_flags |= SYMINFO_FLG_DIRECT;

				/*
				 * Flag any lazy or deferred reference.
				 */
				if (sdp->sd_flags & FLG_SY_LAZYLD)
					syminfo[ndx].si_flags |=
					    SYMINFO_FLG_LAZYLOAD;
				if (sdp->sd_flags & FLG_SY_DEFERRED)
					syminfo[ndx].si_flags |=
					    SYMINFO_FLG_DEFERRED;

				/*
				 * Enable direct symbol bindings if:
				 *
				 *  -	Symbol was identified with the DIRECT
				 *	keyword in a mapfile.
				 *
				 *  -	Symbol reference has been bound to a
				 *	dependency which was specified as
				 *	requiring direct bindings with -zdirect.
				 *
				 *  -	All symbol references are required to
				 *	use direct bindings via -Bdirect.
				 */
				if (sdp->sd_flags & FLG_SY_DIR)
					syminfo[ndx].si_flags |=
					    SYMINFO_FLG_DIRECTBIND;

			} else if ((sdp->sd_flags & FLG_SY_EXTERN) &&
			    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
				/*
				 * If this symbol has been explicitly defined
				 * as external, and remains unresolved, mark
				 * it as external.
				 */
				syminfo[ndx].si_boundto = SYMINFO_BT_EXTERN;

			} else if ((sdp->sd_flags & FLG_SY_PARENT) &&
			    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
				/*
				 * If this symbol has been explicitly defined
				 * to be a reference to a parent object,
				 * indicate whether a direct binding should be
				 * established.
				 */
				syminfo[ndx].si_flags |= SYMINFO_FLG_DIRECT;
				syminfo[ndx].si_boundto = SYMINFO_BT_PARENT;
				if (sdp->sd_flags & FLG_SY_DIR)
					syminfo[ndx].si_flags |=
					    SYMINFO_FLG_DIRECTBIND;

			} else if (sdp->sd_flags & FLG_SY_STDFLTR) {
				/*
				 * A filter definition.  Although this symbol
				 * can only be a stub, it might be necessary to
				 * prevent external direct bindings.
				 */
				syminfo[ndx].si_flags |= SYMINFO_FLG_FILTER;
				if (sdp->sd_flags & FLG_SY_NDIR)
					syminfo[ndx].si_flags |=
					    SYMINFO_FLG_NOEXTDIRECT;

			} else if (sdp->sd_flags & FLG_SY_AUXFLTR) {
				/*
				 * An auxiliary filter definition.  By nature,
				 * this definition is direct, in that should the
				 * filtee lookup fail, we'll fall back to this
				 * object.  It may still be necessary to
				 * prevent external direct bindings.
				 */
				syminfo[ndx].si_flags |= SYMINFO_FLG_AUXILIARY;
				if (sdp->sd_flags & FLG_SY_NDIR)
					syminfo[ndx].si_flags |=
					    SYMINFO_FLG_NOEXTDIRECT;

			} else if ((sdp->sd_ref == REF_REL_NEED) &&
			    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
				/*
				 * This definition exists within the object
				 * being created.  Provide a default boundto
				 * definition, which may be overridden later.
				 */
				syminfo[ndx].si_boundto = SYMINFO_BT_NONE;

				/*
				 * Indicate whether it is necessary to prevent
				 * external direct bindings.
				 */
				if (sdp->sd_flags & FLG_SY_NDIR) {
					syminfo[ndx].si_flags |=
					    SYMINFO_FLG_NOEXTDIRECT;
				}

				/*
				 * Indicate that this symbol is acting as an
				 * individual interposer.
				 */
				if (sdp->sd_flags & FLG_SY_INTPOSE) {
					syminfo[ndx].si_flags |=
					    SYMINFO_FLG_INTERPOSE;
				}

				/*
				 * Indicate that this symbol is deferred, and
				 * hence should not be bound to during BIND_NOW
				 * relocations.
				 */
				if (sdp->sd_flags & FLG_SY_DEFERRED) {
					syminfo[ndx].si_flags |=
					    SYMINFO_FLG_DEFERRED;
				}

				/*
				 * If external bindings are allowed, indicate
				 * the binding, and a direct binding if
				 * necessary.
				 */
				if ((sdp->sd_flags & FLG_SY_NDIR) == 0) {
					syminfo[ndx].si_flags |=
					    SYMINFO_FLG_DIRECT;

					if (sdp->sd_flags & FLG_SY_DIR)
						syminfo[ndx].si_flags |=
						    SYMINFO_FLG_DIRECTBIND;

					/*
					 * Provide a default boundto definition,
					 * which may be overridden later.
					 */
					syminfo[ndx].si_boundto =
					    SYMINFO_BT_SELF;
				}

				/*
				 * Indicate that this is a capabilities symbol.
				 * Note, that this identification only provides
				 * information regarding the symbol that is
				 * visible from elfdump(1) -y.  The association
				 * of a symbol to its capabilities is derived
				 * from a .SUNW_capinfo entry.
				 */
				if ((sdp->sd_flags & FLG_SY_CAP) &&
				    ofl->ofl_oscapinfo) {
					syminfo[ndx].si_flags |=
					    SYMINFO_FLG_CAP;
				}
			}
		}

		/*
		 * Note that the `sym' value is reset to be one of the new
		 * symbol table entries.  This symbol will be updated further
		 * depending on the type of the symbol.  Process the .symtab
		 * first, followed by the .dynsym, thus the `sym' value will
		 * remain as the .dynsym value when the .dynsym is present.
		 * This ensures that any versioning symbols st_name value will
		 * be appropriate for the string table used by version
		 * entries.
		 */
		if (enter_in_symtab) {
			Word	_symndx;

			if (local)
				_symndx = scopesym_ndx;
			else
				_symndx = symtab_ndx;

			symtab[_symndx] = *sdp->sd_sym;
			sdp->sd_sym = sym = &symtab[_symndx];
			(void) st_setstring(strtab, name, &stoff);
			sym->st_name = stoff;
		}
		if (dynlocal) {
			ldynsym[ldynscopesym_ndx] = *sdp->sd_sym;
			sdp->sd_sym = sym = &ldynsym[ldynscopesym_ndx];
			(void) st_setstring(dynstr, name, &stoff);
			ldynsym[ldynscopesym_ndx].st_name = stoff;
			/* Add it to sort section if it qualifies */
			ADD_TO_DYNSORT(sdp, sym, ELF_ST_TYPE(sym->st_info),
			    ldynscopesym_ndx);
		}

		if (dynsym && !local) {
			dynsym[dynsym_ndx] = *sdp->sd_sym;

			/*
			 * Provided this isn't an unnamed register symbol,
			 * update the symbols name and hash value.
			 */
			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
			    dynsym[dynsym_ndx].st_name) {
				(void) st_setstring(dynstr, name, &stoff);
				dynsym[dynsym_ndx].st_name = stoff;

				if (stoff) {
					Word	hashval, _hashndx;

					hashval =
					    sap->sa_hash % ofl->ofl_hashbkts;

					/* LINTED */
					if (_hashndx = hashbkt[hashval]) {
						while (hashchain[_hashndx]) {
							_hashndx =
							    hashchain[_hashndx];
						}
						hashchain[_hashndx] =
						    sdp->sd_symndx;
					} else {
						hashbkt[hashval] =
						    sdp->sd_symndx;
					}
				}
			}
			sdp->sd_sym = sym = &dynsym[dynsym_ndx];

			/*
			 * Add it to sort section if it qualifies.
			 * The indexes in that section are relative to the
			 * the adjacent SUNW_ldynsym/dymsym pair, so we
			 * add the number of items in SUNW_ldynsym to the
			 * dynsym index.
			 */
			ADD_TO_DYNSORT(sdp, sym, ELF_ST_TYPE(sym->st_info),
			    ldynsym_cnt + dynsym_ndx);
		}

		if (!enter_in_symtab && (!dynsym || (local && !dynlocal))) {
			if (!(sdp->sd_flags & FLG_SY_UPREQD))
				continue;
			sym = sdp->sd_sym;
		} else
			sdp->sd_flags &= ~FLG_SY_CLEAN;

		/*
		 * If we have a weak data symbol for which we need the real
		 * symbol also, save this processing until later.
		 *
		 * The exception to this is if the weak/strong have PLT's
		 * assigned to them.  In that case we don't do the post-weak
		 * processing because the PLT's must be maintained so that we
		 * can do 'interpositioning' on both of the symbols.
		 */
		if ((sap->sa_linkndx) &&
		    (ELF_ST_BIND(sym->st_info) == STB_WEAK) &&
		    (!sap->sa_PLTndx)) {
			Sym_desc	*_sdp;

			_sdp = sdp->sd_file->ifl_oldndx[sap->sa_linkndx];

			if (_sdp->sd_ref != REF_DYN_SEEN) {
				Wk_desc	wk;

				if (enter_in_symtab) {
					if (local) {
						wk.wk_symtab =
						    &symtab[scopesym_ndx];
						scopesym_ndx++;
					} else {
						wk.wk_symtab =
						    &symtab[symtab_ndx];
						symtab_ndx++;
					}
				} else {
					wk.wk_symtab = NULL;
				}
				if (dynsym) {
					if (!local) {
						wk.wk_dynsym =
						    &dynsym[dynsym_ndx];
						dynsym_ndx++;
					} else if (dynlocal) {
						wk.wk_dynsym =
						    &ldynsym[ldynscopesym_ndx];
						ldynscopesym_ndx++;
					}
				} else {
					wk.wk_dynsym = NULL;
				}
				wk.wk_weak = sdp;
				wk.wk_alias = _sdp;

				if (alist_append(&weak, &wk,
				    sizeof (Wk_desc), AL_CNT_WEAK) == NULL)
					return ((Addr)S_ERROR);

				continue;
			}
		}

		DBG_CALL(Dbg_syms_old(ofl, sdp));

		spec = 0;
		/*
		 * assign new symbol value.
		 */
		sectndx = sdp->sd_shndx;
		if (sectndx == SHN_UNDEF) {
			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) &&
			    (sym->st_value != 0)) {
				ld_eprintf(ofl, ERR_WARNING,
				    MSG_INTL(MSG_SYM_NOTNULL),
				    demangle(name), sdp->sd_file->ifl_name);
			}

			/*
			 * Undefined weak global, if we are generating a static
			 * executable, output as an absolute zero.  Otherwise
			 * leave it as is, ld.so.1 will skip symbols of this
			 * type (this technique allows applications and
			 * libraries to test for the existence of a symbol as an
			 * indication of the presence or absence of certain
			 * functionality).
			 */
			if (OFL_IS_STATIC_EXEC(ofl) &&
			    (ELF_ST_BIND(sym->st_info) == STB_WEAK)) {
				sdp->sd_flags |= FLG_SY_SPECSEC;
				sdp->sd_shndx = sectndx = SHN_ABS;
			}
		} else if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
		    (sectndx == SHN_COMMON)) {
			/* COMMONs have already been processed */
			/* EMPTY */
			;
		} else {
			if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
			    (sectndx == SHN_ABS))
				spec = sdp->sd_aux->sa_symspec;

			/* LINTED */
			if (sdp->sd_flags & FLG_SY_COMMEXP) {
				/*
				 * This is (or was) a COMMON symbol which was
				 * processed above - no processing
				 * required here.
				 */
				;
			} else if (sdp->sd_ref == REF_DYN_NEED) {
				uchar_t	type, bind;

				sectndx = SHN_UNDEF;
				sym->st_value = 0;
				sym->st_size = 0;

				/*
				 * Make sure this undefined symbol is returned
				 * to the same binding as was defined in the
				 * original relocatable object reference.
				 */
				type = ELF_ST_TYPE(sym-> st_info);
				if (sdp->sd_flags & FLG_SY_GLOBREF)
					bind = STB_GLOBAL;
				else
					bind = STB_WEAK;

				sym->st_info = ELF_ST_INFO(bind, type);

			} else if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
			    (sdp->sd_ref == REF_REL_NEED)) {
				osp = sdp->sd_isc->is_osdesc;
				/* LINTED */
				sectndx = elf_ndxscn(osp->os_scn);

				/*
				 * In an executable, the new symbol value is the
				 * old value (offset into defining section) plus
				 * virtual address of defining section.  In a
				 * relocatable, the new value is the old value
				 * plus the displacement of the section within
				 * the file.
				 */
				/* LINTED */
				sym->st_value +=
				    (Off)_elf_getxoff(sdp->sd_isc->is_indata);

				if (!(flags & FLG_OF_RELOBJ)) {
					sym->st_value += osp->os_shdr->sh_addr;
					/*
					 * TLS symbols are relative to
					 * the TLS segment.
					 */
					if ((ELF_ST_TYPE(sym->st_info) ==
					    STT_TLS) && (ofl->ofl_tlsphdr))
						sym->st_value -=
						    ofl->ofl_tlsphdr->p_vaddr;
				}
			}
		}

		if (spec) {
			switch (spec) {
			case SDAUX_ID_ETEXT:
				sym->st_value = etext;
				sectndx = etext_ndx;
				if (etext_abs)
					sdp->sd_flags |= FLG_SY_SPECSEC;
				else
					sdp->sd_flags &= ~FLG_SY_SPECSEC;
				break;
			case SDAUX_ID_EDATA:
				sym->st_value = edata;
				sectndx = edata_ndx;
				if (edata_abs)
					sdp->sd_flags |= FLG_SY_SPECSEC;
				else
					sdp->sd_flags &= ~FLG_SY_SPECSEC;
				break;
			case SDAUX_ID_END:
				sym->st_value = end;
				sectndx = end_ndx;
				if (end_abs)
					sdp->sd_flags |= FLG_SY_SPECSEC;
				else
					sdp->sd_flags &= ~FLG_SY_SPECSEC;
				break;
			case SDAUX_ID_START:
				sym->st_value = start;
				sectndx = start_ndx;
				sdp->sd_flags &= ~FLG_SY_SPECSEC;
				break;
			case SDAUX_ID_DYN:
				if (flags & FLG_OF_DYNAMIC) {
					sym->st_value = ofl->
					    ofl_osdynamic->os_shdr->sh_addr;
					/* LINTED */
					sectndx = elf_ndxscn(
					    ofl->ofl_osdynamic->os_scn);
					sdp->sd_flags &= ~FLG_SY_SPECSEC;
				}
				break;
			case SDAUX_ID_PLT:
				if (ofl->ofl_osplt) {
					sym->st_value = ofl->
					    ofl_osplt->os_shdr->sh_addr;
					/* LINTED */
					sectndx = elf_ndxscn(
					    ofl->ofl_osplt->os_scn);
					sdp->sd_flags &= ~FLG_SY_SPECSEC;
				}
				break;
			case SDAUX_ID_GOT:
				/*
				 * Symbol bias for negative growing tables is
				 * stored in symbol's value during
				 * allocate_got().
				 */
				sym->st_value += ofl->
				    ofl_osgot->os_shdr->sh_addr;
				/* LINTED */
				sectndx = elf_ndxscn(ofl->
				    ofl_osgot->os_scn);
				sdp->sd_flags &= ~FLG_SY_SPECSEC;
				break;
			case SDAUX_ID_SECBOUND_START:
				sym->st_value = sap->sa_boundsec->
				    os_shdr->sh_addr;
				sectndx = elf_ndxscn(sap->sa_boundsec->os_scn);
				sdp->sd_flags &= ~FLG_SY_SPECSEC;
				break;
			case SDAUX_ID_SECBOUND_STOP:
				sym->st_value = sap->sa_boundsec->
				    os_shdr->sh_addr +
				    sap->sa_boundsec->os_shdr->sh_size;
				sectndx = elf_ndxscn(sap->sa_boundsec->os_scn);
				sdp->sd_flags &= ~FLG_SY_SPECSEC;
				break;
			default:
				/* NOTHING */
				;
			}
		}

		/*
		 * If a plt index has been assigned to an undefined function,
		 * update the symbols value to the appropriate .plt address.
		 */
		if ((flags & FLG_OF_DYNAMIC) && (flags & FLG_OF_EXEC) &&
		    (sdp->sd_file) &&
		    (sdp->sd_file->ifl_ehdr->e_type == ET_DYN) &&
		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC) &&
		    !(flags & FLG_OF_BFLAG)) {
			if (sap->sa_PLTndx)
				sym->st_value =
				    (*ld_targ.t_mr.mr_calc_plt_addr)(sdp, ofl);
		}

		/*
		 * Finish updating the symbols.
		 */

		/*
		 * Sym Update: if scoped local - set local binding
		 */
		if (local)
			sym->st_info = ELF_ST_INFO(STB_LOCAL,
			    ELF_ST_TYPE(sym->st_info));

		/*
		 * Sym Updated: If both the .symtab and .dynsym
		 * are present then we've actually updated the information in
		 * the .dynsym, therefore copy this same information to the
		 * .symtab entry.
		 */
		sdp->sd_shndx = sectndx;
		if (enter_in_symtab && dynsym && (!local || dynlocal)) {
			Word _symndx = dynlocal ? scopesym_ndx : symtab_ndx;

			symtab[_symndx].st_value = sym->st_value;
			symtab[_symndx].st_size = sym->st_size;
			symtab[_symndx].st_info = sym->st_info;
			symtab[_symndx].st_other = sym->st_other;
		}

		if (enter_in_symtab) {
			Word	_symndx;

			if (local)
				_symndx = scopesym_ndx++;
			else
				_symndx = symtab_ndx++;
			if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
			    (sectndx >= SHN_LORESERVE)) {
				assert(symshndx != NULL);
				symshndx[_symndx] = sectndx;
				symtab[_symndx].st_shndx = SHN_XINDEX;
			} else {
				/* LINTED */
				symtab[_symndx].st_shndx = (Half)sectndx;
			}
		}

		if (dynsym && (!local || dynlocal)) {
			/*
			 * dynsym and ldynsym are distinct tables, so
			 * we use indirection to access the right one
			 * and the related extended section index array.
			 */
			Word	_symndx;
			Sym	*_dynsym;
			Word	*_dynshndx;

			if (!local) {
				_symndx = dynsym_ndx++;
				_dynsym = dynsym;
				_dynshndx = dynshndx;
			} else {
				_symndx = ldynscopesym_ndx++;
				_dynsym = ldynsym;
				_dynshndx = ldynshndx;
			}
			if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
			    (sectndx >= SHN_LORESERVE)) {
				assert(_dynshndx != NULL);
				_dynshndx[_symndx] = sectndx;
				_dynsym[_symndx].st_shndx = SHN_XINDEX;
			} else {
				/* LINTED */
				_dynsym[_symndx].st_shndx = (Half)sectndx;
			}
		}

		DBG_CALL(Dbg_syms_new(ofl, sym, sdp));
	}

	/*
	 * Now that all the symbols have been processed update any weak symbols
	 * information (ie. copy all information except `st_name').  As both
	 * symbols will be represented in the output, return the weak symbol to
	 * its correct type.
	 */
	for (ALIST_TRAVERSE(weak, idx1, wkp)) {
		Sym_desc	*sdp, *_sdp;
		Sym		*sym, *_sym, *__sym;
		uchar_t		bind;

		sdp = wkp->wk_weak;
		_sdp = wkp->wk_alias;
		_sym = __sym = _sdp->sd_sym;

		sdp->sd_flags |= FLG_SY_WEAKDEF;

		/*
		 * If the symbol definition has been scoped then assign it to
		 * be local, otherwise if it's from a shared object then we need
		 * to maintain the binding of the original reference.
		 */
		if (SYM_IS_HIDDEN(sdp)) {
			if (ld_sym_reducable(ofl, sdp))
				bind = STB_LOCAL;
			else
				bind = STB_WEAK;
		} else if ((sdp->sd_ref == REF_DYN_NEED) &&
		    (sdp->sd_flags & FLG_SY_GLOBREF))
			bind = STB_GLOBAL;
		else
			bind = STB_WEAK;

		DBG_CALL(Dbg_syms_old(ofl, sdp));
		if ((sym = wkp->wk_symtab) != NULL) {
			sym->st_value = _sym->st_value;
			sym->st_size = _sym->st_size;
			sym->st_other = _sym->st_other;
			sym->st_shndx = _sym->st_shndx;
			sym->st_info = ELF_ST_INFO(bind,
			    ELF_ST_TYPE(sym->st_info));
			__sym = sym;
		}
		if ((sym = wkp->wk_dynsym) != NULL) {
			sym->st_value = _sym->st_value;
			sym->st_size = _sym->st_size;
			sym->st_other = _sym->st_other;
			sym->st_shndx = _sym->st_shndx;
			sym->st_info = ELF_ST_INFO(bind,
			    ELF_ST_TYPE(sym->st_info));
			__sym = sym;
		}
		DBG_CALL(Dbg_syms_new(ofl, __sym, sdp));
	}

	/*
	 * Now display GOT debugging information if required.
	 */
	DBG_CALL(Dbg_got_display(ofl, 0, 0,
	    ld_targ.t_m.m_got_xnumber, ld_targ.t_m.m_got_entsize));

	/*
	 * Update the section headers information. sh_info is
	 * supposed to contain the offset at which the first
	 * global symbol resides in the symbol table, while
	 * sh_link contains the section index of the associated
	 * string table.
	 */
	if (symtab) {
		Shdr	*shdr = ofl->ofl_ossymtab->os_shdr;

		shdr->sh_info = symtab_gbl_bndx;
		/* LINTED */
		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osstrtab->os_scn);
		if (symshndx)
			ofl->ofl_ossymshndx->os_shdr->sh_link =
			    (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);

		/*
		 * Ensure that the expected number of symbols
		 * were entered into the right spots:
		 *	- Scoped symbols in the right range
		 *	- Globals start at the right spot
		 *		(correct number of locals entered)
		 *	- The table is exactly filled
		 *		(correct number of globals entered)
		 */
		assert((scopesym_bndx + ofl->ofl_scopecnt) == scopesym_ndx);
		assert(shdr->sh_info == SYMTAB_LOC_CNT(ofl));
		assert((shdr->sh_info + ofl->ofl_globcnt) == symtab_ndx);
	}
	if (dynsym) {
		Shdr	*shdr = ofl->ofl_osdynsym->os_shdr;

		shdr->sh_info = DYNSYM_LOC_CNT(ofl);
		/* LINTED */
		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);

		ofl->ofl_oshash->os_shdr->sh_link =
		    /* LINTED */
		    (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
		if (dynshndx) {
			shdr = ofl->ofl_osdynshndx->os_shdr;
			shdr->sh_link =
			    (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
		}
	}
	if (ldynsym) {
		Shdr	*shdr = ofl->ofl_osldynsym->os_shdr;

		/* ldynsym has no globals, so give index one past the end */
		shdr->sh_info = ldynsym_ndx;

		/*
		 * The ldynsym and dynsym must be adjacent. The
		 * idea is that rtld should be able to start with
		 * the ldynsym and march straight through the end
		 * of dynsym, seeing them as a single symbol table,
		 * despite the fact that they are in distinct sections.
		 * Ensure that this happened correctly.
		 *
		 * Note that I use ldynsym_ndx here instead of the
		 * computation I used to set the section size
		 * (found in ldynsym_cnt). The two will agree, unless
		 * we somehow miscounted symbols or failed to insert them
		 * all. Using ldynsym_ndx here catches that error in
		 * addition to checking for adjacency.
		 */
		assert(dynsym == (ldynsym + ldynsym_ndx));


		/* LINTED */
		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);

		if (ldynshndx) {
			shdr = ofl->ofl_osldynshndx->os_shdr;
			shdr->sh_link =
			    (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
		}

		/*
		 * The presence of .SUNW_ldynsym means that there may be
		 * associated sort sections, one for regular symbols
		 * and the other for TLS. Each sort section needs the
		 * following done:
		 *	- Section header link references .SUNW_ldynsym
		 *	- Should have received the expected # of items
		 *	- Sorted by increasing address
		 */
		if (ofl->ofl_osdynsymsort) {	/* .SUNW_dynsymsort */
			ofl->ofl_osdynsymsort->os_shdr->sh_link =
			    (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
			assert(ofl->ofl_dynsymsortcnt == dynsymsort_ndx);

			if (dynsymsort_ndx > 1) {
				dynsort_compare_syms = ldynsym;
				qsort(dynsymsort, dynsymsort_ndx,
				    sizeof (*dynsymsort), dynsort_compare);
				dynsort_dupwarn(ofl, ldynsym,
				    st_getstrbuf(dynstr),
				    dynsymsort, dynsymsort_ndx,
				    MSG_ORIG(MSG_SCN_DYNSYMSORT));
			}
		}
		if (ofl->ofl_osdyntlssort) {	/* .SUNW_dyntlssort */
			ofl->ofl_osdyntlssort->os_shdr->sh_link =
			    (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
			assert(ofl->ofl_dyntlssortcnt == dyntlssort_ndx);

			if (dyntlssort_ndx > 1) {
				dynsort_compare_syms = ldynsym;
				qsort(dyntlssort, dyntlssort_ndx,
				    sizeof (*dyntlssort), dynsort_compare);
				dynsort_dupwarn(ofl, ldynsym,
				    st_getstrbuf(dynstr),
				    dyntlssort, dyntlssort_ndx,
				    MSG_ORIG(MSG_SCN_DYNTLSSORT));
			}
		}
	}

	/*
	 * Used by ld.so.1 only.
	 */
	return (etext);

#undef ADD_TO_DYNSORT
}

/*
 * Build the dynamic section.
 *
 * This routine must be maintained in parallel with make_dynamic()
 * in sections.c
 */
static int
update_odynamic(Ofl_desc *ofl)
{
	Aliste		idx;
	Ifl_desc	*ifl;
	Sym_desc	*sdp;
	Shdr		*shdr;
	Dyn		*_dyn = (Dyn *)ofl->ofl_osdynamic->os_outdata->d_buf;
	Dyn		*dyn;
	Os_desc		*symosp, *strosp;
	Str_tbl		*strtbl;
	size_t		stoff;
	ofl_flag_t	flags = ofl->ofl_flags;
	int		not_relobj = !(flags & FLG_OF_RELOBJ);
	Word		cnt;

	/*
	 * Relocatable objects can be built with -r and -dy to trigger the
	 * creation of a .dynamic section.  This model is used to create kernel
	 * device drivers.  The .dynamic section provides a subset of userland
	 * .dynamic entries, typically entries such as DT_NEEDED and DT_RUNPATH.
	 *
	 * Within a dynamic object, any .dynamic string references are to the
	 * .dynstr table.  Within a relocatable object, these strings can reside
	 * within the .strtab.
	 */
	if (OFL_IS_STATIC_OBJ(ofl)) {
		symosp = ofl->ofl_ossymtab;
		strosp = ofl->ofl_osstrtab;
		strtbl = ofl->ofl_strtab;
	} else {
		symosp = ofl->ofl_osdynsym;
		strosp = ofl->ofl_osdynstr;
		strtbl = ofl->ofl_dynstrtab;
	}

	/* LINTED */
	ofl->ofl_osdynamic->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);

	dyn = _dyn;

	for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) {
		if ((ifl->ifl_flags &
		    (FLG_IF_IGNORE | FLG_IF_DEPREQD)) == FLG_IF_IGNORE)
			continue;

		/*
		 * Create and set up the DT_POSFLAG_1 entry here if required.
		 */
		if ((ifl->ifl_flags & MSK_IF_POSFLAG1) &&
		    (ifl->ifl_flags & FLG_IF_NEEDED) && not_relobj) {
			dyn->d_tag = DT_POSFLAG_1;
			if (ifl->ifl_flags & FLG_IF_LAZYLD)
				dyn->d_un.d_val = DF_P1_LAZYLOAD;
			if (ifl->ifl_flags & FLG_IF_GRPPRM)
				dyn->d_un.d_val |= DF_P1_GROUPPERM;
			if (ifl->ifl_flags & FLG_IF_DEFERRED)
				dyn->d_un.d_val |= DF_P1_DEFERRED;
			dyn++;
		}

		if (ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR))
			dyn->d_tag = DT_NEEDED;
		else
			continue;

		(void) st_setstring(strtbl, ifl->ifl_soname, &stoff);
		dyn->d_un.d_val = stoff;
		/* LINTED */
		ifl->ifl_neededndx = (Half)(((uintptr_t)dyn - (uintptr_t)_dyn) /
		    sizeof (Dyn));
		dyn++;
	}

	if (not_relobj) {
		if (ofl->ofl_dtsfltrs != NULL) {
			Dfltr_desc	*dftp;

			for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp)) {
				if (dftp->dft_flag == FLG_SY_AUXFLTR)
					dyn->d_tag = DT_SUNW_AUXILIARY;
				else
					dyn->d_tag = DT_SUNW_FILTER;

				(void) st_setstring(strtbl, dftp->dft_str,
				    &stoff);
				dyn->d_un.d_val = stoff;
				dftp->dft_ndx = (Half)(((uintptr_t)dyn -
				    (uintptr_t)_dyn) / sizeof (Dyn));
				dyn++;
			}
		}
		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
		    SYM_NOHASH, 0, ofl)) != NULL) &&
		    (sdp->sd_ref == REF_REL_NEED) &&
		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
			dyn->d_tag = DT_INIT;
			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
			dyn++;
		}
		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
		    SYM_NOHASH, 0, ofl)) != NULL) &&
		    (sdp->sd_ref == REF_REL_NEED) &&
		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
			dyn->d_tag = DT_FINI;
			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
			dyn++;
		}
		if (ofl->ofl_soname) {
			dyn->d_tag = DT_SONAME;
			(void) st_setstring(strtbl, ofl->ofl_soname, &stoff);
			dyn->d_un.d_val = stoff;
			dyn++;
		}
		if (ofl->ofl_filtees) {
			if (flags & FLG_OF_AUX) {
				dyn->d_tag = DT_AUXILIARY;
			} else {
				dyn->d_tag = DT_FILTER;
			}
			(void) st_setstring(strtbl, ofl->ofl_filtees, &stoff);
			dyn->d_un.d_val = stoff;
			dyn++;
		}
	}

	if (ofl->ofl_rpath) {
		(void) st_setstring(strtbl, ofl->ofl_rpath, &stoff);
		dyn->d_tag = DT_RUNPATH;
		dyn->d_un.d_val = stoff;
		dyn++;
		dyn->d_tag = DT_RPATH;
		dyn->d_un.d_val = stoff;
		dyn++;
	}

	if (not_relobj) {
		Aliste	idx;
		Sg_desc	*sgp;

		if (ofl->ofl_config) {
			dyn->d_tag = DT_CONFIG;
			(void) st_setstring(strtbl, ofl->ofl_config, &stoff);
			dyn->d_un.d_val = stoff;
			dyn++;
		}
		if (ofl->ofl_depaudit) {
			dyn->d_tag = DT_DEPAUDIT;
			(void) st_setstring(strtbl, ofl->ofl_depaudit, &stoff);
			dyn->d_un.d_val = stoff;
			dyn++;
		}
		if (ofl->ofl_audit) {
			dyn->d_tag = DT_AUDIT;
			(void) st_setstring(strtbl, ofl->ofl_audit, &stoff);
			dyn->d_un.d_val = stoff;
			dyn++;
		}

		dyn->d_tag = DT_HASH;
		dyn->d_un.d_ptr = ofl->ofl_oshash->os_shdr->sh_addr;
		dyn++;

		shdr = strosp->os_shdr;
		dyn->d_tag = DT_STRTAB;
		dyn->d_un.d_ptr = shdr->sh_addr;
		dyn++;

		dyn->d_tag = DT_STRSZ;
		dyn->d_un.d_ptr = shdr->sh_size;
		dyn++;

		/*
		 * Note, the shdr is set and used in the ofl->ofl_osldynsym case
		 * that follows.
		 */
		shdr = symosp->os_shdr;
		dyn->d_tag = DT_SYMTAB;
		dyn->d_un.d_ptr = shdr->sh_addr;
		dyn++;

		dyn->d_tag = DT_SYMENT;
		dyn->d_un.d_ptr = shdr->sh_entsize;
		dyn++;

		if (ofl->ofl_osldynsym) {
			Shdr	*lshdr = ofl->ofl_osldynsym->os_shdr;

			/*
			 * We have arranged for the .SUNW_ldynsym data to be
			 * immediately in front of the .dynsym data.
			 * This means that you could start at the top
			 * of .SUNW_ldynsym and see the data for both tables
			 * without a break. This is the view we want to
			 * provide for DT_SUNW_SYMTAB, which is why we
			 * add the lengths together.
			 */
			dyn->d_tag = DT_SUNW_SYMTAB;
			dyn->d_un.d_ptr = lshdr->sh_addr;
			dyn++;

			dyn->d_tag = DT_SUNW_SYMSZ;
			dyn->d_un.d_val = lshdr->sh_size + shdr->sh_size;
			dyn++;
		}

		if (ofl->ofl_osdynsymsort || ofl->ofl_osdyntlssort) {
			dyn->d_tag = DT_SUNW_SORTENT;
			dyn->d_un.d_val = sizeof (Word);
			dyn++;
		}

		if (ofl->ofl_osdynsymsort) {
			shdr = ofl->ofl_osdynsymsort->os_shdr;

			dyn->d_tag = DT_SUNW_SYMSORT;
			dyn->d_un.d_ptr = shdr->sh_addr;
			dyn++;

			dyn->d_tag = DT_SUNW_SYMSORTSZ;
			dyn->d_un.d_val = shdr->sh_size;
			dyn++;
		}

		if (ofl->ofl_osdyntlssort) {
			shdr = ofl->ofl_osdyntlssort->os_shdr;

			dyn->d_tag = DT_SUNW_TLSSORT;
			dyn->d_un.d_ptr = shdr->sh_addr;
			dyn++;

			dyn->d_tag = DT_SUNW_TLSSORTSZ;
			dyn->d_un.d_val = shdr->sh_size;
			dyn++;
		}

		/*
		 * Reserve the DT_CHECKSUM entry.  Its value will be filled in
		 * after the complete image is built.
		 */
		dyn->d_tag = DT_CHECKSUM;
		ofl->ofl_checksum = &dyn->d_un.d_val;
		dyn++;

		/*
		 * Versioning sections: DT_VERDEF and DT_VERNEED.
		 *
		 * The Solaris ld does not produce DT_VERSYM, but the GNU ld
		 * does, in order to support their style of versioning, which
		 * differs from ours:
		 *
		 *	- The top bit of the 16-bit Versym index is
		 *		not part of the version, but is interpreted
		 *		as a "hidden bit".
		 *
		 *	- External (SHN_UNDEF) symbols can have non-zero
		 *		Versym values, which specify versions in
		 *		referenced objects, via the Verneed section.
		 *
		 *	- The vna_other field of the Vernaux structures
		 *		found in the Verneed section are not zero as
		 *		with Solaris, but instead contain the version
		 *		index to be used by Versym indices to reference
		 *		the given external version.
		 *
		 * The Solaris ld, rtld, and elfdump programs all interpret the
		 * presence of DT_VERSYM as meaning that GNU versioning rules
		 * apply to the given file. If DT_VERSYM is not present,
		 * then Solaris versioning rules apply. If we should ever need
		 * to change our ld so that it does issue DT_VERSYM, then
		 * this rule for detecting GNU versioning will no longer work.
		 * In that case, we will have to invent a way to explicitly
		 * specify the style of versioning in use, perhaps via a
		 * new dynamic entry named something like DT_SUNW_VERSIONSTYLE,
		 * where the d_un.d_val value specifies which style is to be
		 * used.
		 */
		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
		    FLG_OF_VERDEF) {
			shdr = ofl->ofl_osverdef->os_shdr;

			dyn->d_tag = DT_VERDEF;
			dyn->d_un.d_ptr = shdr->sh_addr;
			dyn++;
			dyn->d_tag = DT_VERDEFNUM;
			dyn->d_un.d_ptr = shdr->sh_info;
			dyn++;
		}
		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
		    FLG_OF_VERNEED) {
			shdr = ofl->ofl_osverneed->os_shdr;

			dyn->d_tag = DT_VERNEED;
			dyn->d_un.d_ptr = shdr->sh_addr;
			dyn++;
			dyn->d_tag = DT_VERNEEDNUM;
			dyn->d_un.d_ptr = shdr->sh_info;
			dyn++;
		}

		if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt) {
			dyn->d_tag = ld_targ.t_m.m_rel_dt_count;
			dyn->d_un.d_val = ofl->ofl_relocrelcnt;
			dyn++;
		}
		if (flags & FLG_OF_TEXTREL) {
			/*
			 * Only the presence of this entry is used in this
			 * implementation, not the value stored.
			 */
			dyn->d_tag = DT_TEXTREL;
			dyn->d_un.d_val = 0;
			dyn++;
		}

		if (ofl->ofl_osfiniarray) {
			shdr = ofl->ofl_osfiniarray->os_shdr;

			dyn->d_tag = DT_FINI_ARRAY;
			dyn->d_un.d_ptr = shdr->sh_addr;
			dyn++;

			dyn->d_tag = DT_FINI_ARRAYSZ;
			dyn->d_un.d_val = shdr->sh_size;
			dyn++;
		}

		if (ofl->ofl_osinitarray) {
			shdr = ofl->ofl_osinitarray->os_shdr;

			dyn->d_tag = DT_INIT_ARRAY;
			dyn->d_un.d_ptr = shdr->sh_addr;
			dyn++;

			dyn->d_tag = DT_INIT_ARRAYSZ;
			dyn->d_un.d_val = shdr->sh_size;
			dyn++;
		}

		if (ofl->ofl_ospreinitarray) {
			shdr = ofl->ofl_ospreinitarray->os_shdr;

			dyn->d_tag = DT_PREINIT_ARRAY;
			dyn->d_un.d_ptr = shdr->sh_addr;
			dyn++;

			dyn->d_tag = DT_PREINIT_ARRAYSZ;
			dyn->d_un.d_val = shdr->sh_size;
			dyn++;
		}

		if (ofl->ofl_pltcnt) {
			shdr = ofl->ofl_osplt->os_relosdesc->os_shdr;

			dyn->d_tag = DT_PLTRELSZ;
			dyn->d_un.d_ptr = shdr->sh_size;
			dyn++;
			dyn->d_tag = DT_PLTREL;
			dyn->d_un.d_ptr = ld_targ.t_m.m_rel_dt_type;
			dyn++;
			dyn->d_tag = DT_JMPREL;
			dyn->d_un.d_ptr = shdr->sh_addr;
			dyn++;
		}
		if (ofl->ofl_pltpad) {
			shdr = ofl->ofl_osplt->os_shdr;

			dyn->d_tag = DT_PLTPAD;
			if (ofl->ofl_pltcnt) {
				dyn->d_un.d_ptr = shdr->sh_addr +
				    ld_targ.t_m.m_plt_reservsz +
				    ofl->ofl_pltcnt * ld_targ.t_m.m_plt_entsize;
			} else
				dyn->d_un.d_ptr = shdr->sh_addr;
			dyn++;
			dyn->d_tag = DT_PLTPADSZ;
			dyn->d_un.d_val = ofl->ofl_pltpad *
			    ld_targ.t_m.m_plt_entsize;
			dyn++;
		}
		if (ofl->ofl_relocsz) {
			shdr = ofl->ofl_osrelhead->os_shdr;

			dyn->d_tag = ld_targ.t_m.m_rel_dt_type;
			dyn->d_un.d_ptr = shdr->sh_addr;
			dyn++;
			dyn->d_tag = ld_targ.t_m.m_rel_dt_size;
			dyn->d_un.d_ptr = ofl->ofl_relocsz;
			dyn++;
			dyn->d_tag = ld_targ.t_m.m_rel_dt_ent;
			if (shdr->sh_type == SHT_REL)
				dyn->d_un.d_ptr = sizeof (Rel);
			else
				dyn->d_un.d_ptr = sizeof (Rela);
			dyn++;
		}
		if (ofl->ofl_ossyminfo) {
			shdr = ofl->ofl_ossyminfo->os_shdr;

			dyn->d_tag = DT_SYMINFO;
			dyn->d_un.d_ptr = shdr->sh_addr;
			dyn++;
			dyn->d_tag = DT_SYMINSZ;
			dyn->d_un.d_val = shdr->sh_size;
			dyn++;
			dyn->d_tag = DT_SYMINENT;
			dyn->d_un.d_val = sizeof (Syminfo);
			dyn++;
		}
		if (ofl->ofl_osmove) {
			shdr = ofl->ofl_osmove->os_shdr;

			dyn->d_tag = DT_MOVETAB;
			dyn->d_un.d_val = shdr->sh_addr;
			dyn++;
			dyn->d_tag = DT_MOVESZ;
			dyn->d_un.d_val = shdr->sh_size;
			dyn++;
			dyn->d_tag = DT_MOVEENT;
			dyn->d_un.d_val = shdr->sh_entsize;
			dyn++;
		}
		if (ofl->ofl_regsymcnt) {
			int	ndx;

			for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
				if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
					continue;

				dyn->d_tag = ld_targ.t_m.m_dt_register;
				dyn->d_un.d_val = sdp->sd_symndx;
				dyn++;
			}
		}

		for (APLIST_TRAVERSE(ofl->ofl_rtldinfo, idx, sdp)) {
			dyn->d_tag = DT_SUNW_RTLDINF;
			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
			dyn++;
		}

		if (((sgp = ofl->ofl_osdynamic->os_sgdesc) != NULL) &&
		    (sgp->sg_phdr.p_flags & PF_W) && ofl->ofl_osinterp) {
			dyn->d_tag = DT_DEBUG;
			dyn->d_un.d_ptr = 0;
			dyn++;
		}

		if (ofl->ofl_oscap) {
			dyn->d_tag = DT_SUNW_CAP;
			dyn->d_un.d_val = ofl->ofl_oscap->os_shdr->sh_addr;
			dyn++;
		}
		if (ofl->ofl_oscapinfo) {
			dyn->d_tag = DT_SUNW_CAPINFO;
			dyn->d_un.d_val = ofl->ofl_oscapinfo->os_shdr->sh_addr;
			dyn++;
		}
		if (ofl->ofl_oscapchain) {
			shdr = ofl->ofl_oscapchain->os_shdr;

			dyn->d_tag = DT_SUNW_CAPCHAIN;
			dyn->d_un.d_val = shdr->sh_addr;
			dyn++;
			dyn->d_tag = DT_SUNW_CAPCHAINSZ;
			dyn->d_un.d_val = shdr->sh_size;
			dyn++;
			dyn->d_tag = DT_SUNW_CAPCHAINENT;
			dyn->d_un.d_val = shdr->sh_entsize;
			dyn++;
		}

		if (ofl->ofl_aslr != 0) {
			dyn->d_tag = DT_SUNW_ASLR;
			dyn->d_un.d_val = (ofl->ofl_aslr == 1);
			dyn++;
		}

		if (flags & FLG_OF_SYMBOLIC) {
			dyn->d_tag = DT_SYMBOLIC;
			dyn->d_un.d_val = 0;
			dyn++;
		}
	}

	dyn->d_tag = DT_FLAGS;
	dyn->d_un.d_val = ofl->ofl_dtflags;
	dyn++;

	/*
	 * If -Bdirect was specified, but some NODIRECT symbols were specified
	 * via a mapfile, or -znodirect was used on the command line, then
	 * clear the DF_1_DIRECT flag.  The resultant object will use per-symbol
	 * direct bindings rather than be enabled for global direct bindings.
	 *
	 * If any no-direct bindings exist within this object, set the
	 * DF_1_NODIRECT flag.  ld(1) recognizes this flag when processing
	 * dependencies, and performs extra work to ensure that no direct
	 * bindings are established to the no-direct symbols that exist
	 * within these dependencies.
	 */
	if (ofl->ofl_flags1 & FLG_OF1_NGLBDIR)
		ofl->ofl_dtflags_1 &= ~DF_1_DIRECT;
	if (ofl->ofl_flags1 & FLG_OF1_NDIRECT)
		ofl->ofl_dtflags_1 |= DF_1_NODIRECT;

	dyn->d_tag = DT_FLAGS_1;
	dyn->d_un.d_val = ofl->ofl_dtflags_1;
	dyn++;

	dyn->d_tag = DT_SUNW_STRPAD;
	dyn->d_un.d_val = DYNSTR_EXTRA_PAD;
	dyn++;

	dyn->d_tag = DT_SUNW_LDMACH;
	dyn->d_un.d_val = ld_sunw_ldmach();
	dyn++;

	if (ofl->ofl_flags & FLG_OF_KMOD) {
		dyn->d_tag = DT_SUNW_KMOD;
		dyn->d_un.d_val = 1;
		dyn++;
	}

	(*ld_targ.t_mr.mr_mach_update_odynamic)(ofl, &dyn);

	for (cnt = 1 + DYNAMIC_EXTRA_ELTS; cnt--; dyn++) {
		dyn->d_tag = DT_NULL;
		dyn->d_un.d_val = 0;
	}

	/*
	 * Ensure that we wrote the right number of entries. If not, we either
	 * miscounted in make_dynamic(), or we did something wrong in this
	 * function.
	 */
	assert((ofl->ofl_osdynamic->os_shdr->sh_size /
	    ofl->ofl_osdynamic->os_shdr->sh_entsize) ==
	    ((uintptr_t)dyn - (uintptr_t)_dyn) / sizeof (*dyn));

	return (1);
}

/*
 * Build the version definition section
 */
static int
update_overdef(Ofl_desc *ofl)
{
	Aliste		idx1;
	Ver_desc	*vdp, *_vdp;
	Verdef		*vdf, *_vdf;
	int		num = 0;
	Os_desc		*strosp;
	Str_tbl		*strtbl;

	/*
	 * Determine which string table to use.
	 */
	if (OFL_IS_STATIC_OBJ(ofl)) {
		strtbl = ofl->ofl_strtab;
		strosp = ofl->ofl_osstrtab;
	} else {
		strtbl = ofl->ofl_dynstrtab;
		strosp = ofl->ofl_osdynstr;
	}

	/*
	 * Traverse the version descriptors and update the version structures
	 * to point to the dynstr name in preparation for building the version
	 * section structure.
	 */
	for (APLIST_TRAVERSE(ofl->ofl_verdesc, idx1, vdp)) {
		Sym_desc	*sdp;

		if (vdp->vd_flags & VER_FLG_BASE) {
			const char	*name = vdp->vd_name;
			size_t		stoff;

			/*
			 * Create a new string table entry to represent the base
			 * version name (there is no corresponding symbol for
			 * this).
			 */
			(void) st_setstring(strtbl, name, &stoff);
			/* LINTED */
			vdp->vd_name = (const char *)stoff;
		} else {
			sdp = ld_sym_find(vdp->vd_name, vdp->vd_hash, 0, ofl);
			/* LINTED */
			vdp->vd_name = (const char *)
			    (uintptr_t)sdp->sd_sym->st_name;
		}
	}

	_vdf = vdf = (Verdef *)ofl->ofl_osverdef->os_outdata->d_buf;

	/*
	 * Traverse the version descriptors and update the version section to
	 * reflect each version and its associated dependencies.
	 */
	for (APLIST_TRAVERSE(ofl->ofl_verdesc, idx1, vdp)) {
		Aliste		idx2;
		Half		cnt = 1;
		Verdaux		*vdap, *_vdap;

		_vdap = vdap = (Verdaux *)(vdf + 1);

		vdf->vd_version = VER_DEF_CURRENT;
		vdf->vd_flags	= vdp->vd_flags & MSK_VER_USER;
		vdf->vd_ndx	= vdp->vd_ndx;
		vdf->vd_hash	= vdp->vd_hash;

		/* LINTED */
		vdap->vda_name = (uintptr_t)vdp->vd_name;
		vdap++;
		/* LINTED */
		_vdap->vda_next = (Word)((uintptr_t)vdap - (uintptr_t)_vdap);

		/*
		 * Traverse this versions dependency list generating the
		 * appropriate version dependency entries.
		 */
		for (APLIST_TRAVERSE(vdp->vd_deps, idx2, _vdp)) {
			/* LINTED */
			vdap->vda_name = (uintptr_t)_vdp->vd_name;
			_vdap = vdap;
			vdap++, cnt++;
			/* LINTED */
			_vdap->vda_next = (Word)((uintptr_t)vdap -
			    (uintptr_t)_vdap);
		}
		_vdap->vda_next = 0;

		/*
		 * Record the versions auxiliary array offset and the associated
		 * dependency count.
		 */
		/* LINTED */
		vdf->vd_aux = (Word)((uintptr_t)(vdf + 1) - (uintptr_t)vdf);
		vdf->vd_cnt = cnt;

		/*
		 * Record the next versions offset and update the version
		 * pointer.  Remember the previous version offset as the very
		 * last structures next pointer should be null.
		 */
		_vdf = vdf;
		vdf = (Verdef *)vdap, num++;
		/* LINTED */
		_vdf->vd_next = (Word)((uintptr_t)vdf - (uintptr_t)_vdf);
	}
	_vdf->vd_next = 0;

	/*
	 * Record the string table association with the version definition
	 * section, and the symbol table associated with the version symbol
	 * table (the actual contents of the version symbol table are filled
	 * in during symbol update).
	 */
	/* LINTED */
	ofl->ofl_osverdef->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);

	/*
	 * The version definition sections `info' field is used to indicate the
	 * number of entries in this section.
	 */
	ofl->ofl_osverdef->os_shdr->sh_info = num;

	return (1);
}

/*
 * Finish the version symbol index section
 */
static void
update_oversym(Ofl_desc *ofl)
{
	Os_desc		*osp;

	/*
	 * Record the symbol table associated with the version symbol table.
	 * The contents of the version symbol table are filled in during
	 * symbol update.
	 */
	if (OFL_IS_STATIC_OBJ(ofl))
		osp = ofl->ofl_ossymtab;
	else
		osp = ofl->ofl_osdynsym;

	/* LINTED */
	ofl->ofl_osversym->os_shdr->sh_link = (Word)elf_ndxscn(osp->os_scn);
}

/*
 * Build the version needed section
 */
static int
update_overneed(Ofl_desc *ofl)
{
	Aliste		idx1;
	Ifl_desc	*ifl;
	Verneed		*vnd, *_vnd;
	Os_desc		*strosp;
	Str_tbl		*strtbl;
	Word		num = 0;

	_vnd = vnd = (Verneed *)ofl->ofl_osverneed->os_outdata->d_buf;

	/*
	 * Determine which string table is appropriate.
	 */
	if (OFL_IS_STATIC_OBJ(ofl)) {
		strosp = ofl->ofl_osstrtab;
		strtbl = ofl->ofl_strtab;
	} else {
		strosp = ofl->ofl_osdynstr;
		strtbl = ofl->ofl_dynstrtab;
	}

	/*
	 * Traverse the shared object list looking for dependencies that have
	 * versions defined within them.
	 */
	for (APLIST_TRAVERSE(ofl->ofl_sos, idx1, ifl)) {
		Half		_cnt;
		Word		cnt = 0;
		Vernaux		*_vnap, *vnap;
		size_t		stoff;

		if (!(ifl->ifl_flags & FLG_IF_VERNEED))
			continue;

		vnd->vn_version = VER_NEED_CURRENT;

		(void) st_setstring(strtbl, ifl->ifl_soname, &stoff);
		vnd->vn_file = stoff;

		_vnap = vnap = (Vernaux *)(vnd + 1);

		/*
		 * Traverse the version index list recording
		 * each version as a needed dependency.
		 */
		for (_cnt = 0; _cnt <= ifl->ifl_vercnt; _cnt++) {
			Ver_index	*vip = &ifl->ifl_verndx[_cnt];

			if (vip->vi_flags & FLG_VER_REFER) {
				(void) st_setstring(strtbl, vip->vi_name,
				    &stoff);
				vnap->vna_name = stoff;

				if (vip->vi_desc) {
					vnap->vna_hash = vip->vi_desc->vd_hash;
					vnap->vna_flags =
					    vip->vi_desc->vd_flags;
				} else {
					vnap->vna_hash = 0;
					vnap->vna_flags = 0;
				}
				vnap->vna_other = vip->vi_overndx;

				/*
				 * If version A inherits version B, then
				 * B is implicit in A. It suffices for ld.so.1
				 * to verify A at runtime and skip B. The
				 * version normalization process sets the INFO
				 * flag for the versions we want ld.so.1 to
				 * skip.
				 */
				if (vip->vi_flags & VER_FLG_INFO)
					vnap->vna_flags |= VER_FLG_INFO;

				_vnap = vnap;
				vnap++, cnt++;
				_vnap->vna_next =
				    /* LINTED */
				    (Word)((uintptr_t)vnap - (uintptr_t)_vnap);
			}
		}

		_vnap->vna_next = 0;

		/*
		 * Record the versions auxiliary array offset and
		 * the associated dependency count.
		 */
		/* LINTED */
		vnd->vn_aux = (Word)((uintptr_t)(vnd + 1) - (uintptr_t)vnd);
		/* LINTED */
		vnd->vn_cnt = (Half)cnt;

		/*
		 * Record the next versions offset and update the version
		 * pointer.  Remember the previous version offset as the very
		 * last structures next pointer should be null.
		 */
		_vnd = vnd;
		vnd = (Verneed *)vnap, num++;
		/* LINTED */
		_vnd->vn_next = (Word)((uintptr_t)vnd - (uintptr_t)_vnd);
	}
	_vnd->vn_next = 0;

	/*
	 * Use sh_link to record the associated string table section, and
	 * sh_info to indicate the number of entries contained in the section.
	 */
	/* LINTED */
	ofl->ofl_osverneed->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
	ofl->ofl_osverneed->os_shdr->sh_info = num;

	return (1);
}

/*
 * Update syminfo section.
 */
static uintptr_t
update_osyminfo(Ofl_desc *ofl)
{
	Os_desc		*symosp, *infosp = ofl->ofl_ossyminfo;
	Syminfo		*sip = infosp->os_outdata->d_buf;
	Shdr		*shdr = infosp->os_shdr;
	char		*strtab;
	Aliste		idx;
	Sym_desc	*sdp;
	Sfltr_desc	*sftp;

	if (ofl->ofl_flags & FLG_OF_RELOBJ) {
		symosp = ofl->ofl_ossymtab;
		strtab = ofl->ofl_osstrtab->os_outdata->d_buf;
	} else {
		symosp = ofl->ofl_osdynsym;
		strtab = ofl->ofl_osdynstr->os_outdata->d_buf;
	}

	/* LINTED */
	infosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
	if (ofl->ofl_osdynamic)
		infosp->os_shdr->sh_info =
		    /* LINTED */
		    (Word)elf_ndxscn(ofl->ofl_osdynamic->os_scn);

	/*
	 * Update any references with the index into the dynamic table.
	 */
	for (APLIST_TRAVERSE(ofl->ofl_symdtent, idx, sdp))
		sip[sdp->sd_symndx].si_boundto = sdp->sd_file->ifl_neededndx;

	/*
	 * Update any filtee references with the index into the dynamic table.
	 */
	for (ALIST_TRAVERSE(ofl->ofl_symfltrs, idx, sftp)) {
		Dfltr_desc	*dftp;

		dftp = alist_item(ofl->ofl_dtsfltrs, sftp->sft_idx);
		sip[sftp->sft_sdp->sd_symndx].si_boundto = dftp->dft_ndx;
	}

	/*
	 * Display debugging information about section.
	 */
	DBG_CALL(Dbg_syminfo_title(ofl->ofl_lml));
	if (DBG_ENABLED) {
		Word	_cnt, cnt = shdr->sh_size / shdr->sh_entsize;
		Sym	*symtab = symosp->os_outdata->d_buf;
		Dyn	*dyn;

		if (ofl->ofl_osdynamic)
			dyn = ofl->ofl_osdynamic->os_outdata->d_buf;
		else
			dyn = NULL;

		for (_cnt = 1; _cnt < cnt; _cnt++) {
			if (sip[_cnt].si_flags || sip[_cnt].si_boundto)
				/* LINTED */
				DBG_CALL(Dbg_syminfo_entry(ofl->ofl_lml, _cnt,
				    &sip[_cnt], &symtab[_cnt], strtab, dyn));
		}
	}
	return (1);
}

/*
 * Build the output elf header.
 */
static uintptr_t
update_oehdr(Ofl_desc * ofl)
{
	Ehdr	*ehdr = ofl->ofl_nehdr;

	/*
	 * If an entry point symbol has already been established (refer
	 * sym_validate()) simply update the elf header entry point with the
	 * symbols value.  If no entry point is defined it will have been filled
	 * with the start address of the first section within the text segment
	 * (refer update_outfile()).
	 */
	if (ofl->ofl_entry)
		ehdr->e_entry =
		    ((Sym_desc *)(ofl->ofl_entry))->sd_sym->st_value;

	ehdr->e_ident[EI_DATA] = ld_targ.t_m.m_data;
	ehdr->e_version = ofl->ofl_dehdr->e_version;

	/*
	 * When generating a relocatable object under -z symbolcap, set the
	 * e_machine to be generic, and remove any e_flags.  Input relocatable
	 * objects may identify alternative e_machine (m.machplus) and e_flags
	 * values.  However, the functions within the created output object
	 * are selected at runtime using the capabilities mechanism, which
	 * supersedes the e-machine and e_flags information.  Therefore,
	 * e_machine and e_flag values are not propagated to the output object,
	 * as these values might prevent the kernel from loading the object
	 * before the runtime linker gets control.
	 */
	if (ofl->ofl_flags & FLG_OF_OTOSCAP) {
		ehdr->e_machine = ld_targ.t_m.m_mach;
		ehdr->e_flags = 0;
	} else {
		/*
		 * Note. it may be necessary to update the e_flags field in the
		 * machine dependent section.
		 */
		ehdr->e_machine = ofl->ofl_dehdr->e_machine;
		ehdr->e_flags = ofl->ofl_dehdr->e_flags;

		if (ehdr->e_machine != ld_targ.t_m.m_mach) {
			if (ehdr->e_machine != ld_targ.t_m.m_machplus)
				return (S_ERROR);
			if ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0)
				return (S_ERROR);
		}
	}

	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
		ehdr->e_type = ET_DYN;
	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
		ehdr->e_type = ET_REL;
	else
		ehdr->e_type = ET_EXEC;

	return (1);
}

/*
 * Perform move table expansion.
 */
static void
expand_move(Ofl_desc *ofl, Sym_desc *sdp, Move *mvp)
{
	Os_desc		*osp;
	uchar_t		*taddr, *taddr0;
	Sxword		offset;
	Half		cnt;
	uint_t		stride;

	osp = ofl->ofl_isparexpn->is_osdesc;
	offset = sdp->sd_sym->st_value - osp->os_shdr->sh_addr;

	taddr0 = taddr = osp->os_outdata->d_buf;
	taddr += offset;
	taddr = taddr + mvp->m_poffset;

	for (cnt = 0; cnt < mvp->m_repeat; cnt++) {
		/* LINTED */
		DBG_CALL(Dbg_move_expand(ofl->ofl_lml, mvp,
		    (Addr)(taddr - taddr0)));
		stride = (uint_t)mvp->m_stride + 1;

		/*
		 * Update the target address based upon the move entry size.
		 * This size was validated in ld_process_move().
		 */
		/* LINTED */
		switch (ELF_M_SIZE(mvp->m_info)) {
		case 1:
			/* LINTED */
			*taddr = (uchar_t)mvp->m_value;
			taddr += stride;
			break;
		case 2:
			/* LINTED */
			*((Half *)taddr) = (Half)mvp->m_value;
			taddr += 2 * stride;
			break;
		case 4:
			/* LINTED */
			*((Word *)taddr) = (Word)mvp->m_value;
			taddr += 4 * stride;
			break;
		case 8:
			/* LINTED */
			*((u_longlong_t *)taddr) = mvp->m_value;
			taddr += 8 * stride;
			break;
		}
	}
}

/*
 * Update Move sections.
 */
static void
update_move(Ofl_desc *ofl)
{
	Word		ndx = 0;
	ofl_flag_t	flags = ofl->ofl_flags;
	Move		*omvp;
	Aliste		idx1;
	Sym_desc	*sdp;

	/*
	 * Determine the index of the symbol table that will be referenced by
	 * the Move section.
	 */
	if (OFL_ALLOW_DYNSYM(ofl))
		/* LINTED */
		ndx = (Word) elf_ndxscn(ofl->ofl_osdynsym->os_scn);
	else if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ))
		/* LINTED */
		ndx = (Word) elf_ndxscn(ofl->ofl_ossymtab->os_scn);

	/*
	 * Update sh_link of the Move section, and point to the new Move data.
	 */
	if (ofl->ofl_osmove) {
		ofl->ofl_osmove->os_shdr->sh_link = ndx;
		omvp = (Move *)ofl->ofl_osmove->os_outdata->d_buf;
	}

	/*
	 * Update symbol entry index
	 */
	for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx1, sdp)) {
		Aliste		idx2;
		Mv_desc		*mdp;

		/*
		 * Expand move table
		 */
		if (sdp->sd_flags & FLG_SY_PAREXPN) {
			const char	*str;

			if (flags & FLG_OF_STATIC)
				str = MSG_INTL(MSG_PSYM_EXPREASON1);
			else if (ofl->ofl_flags1 & FLG_OF1_NOPARTI)
				str = MSG_INTL(MSG_PSYM_EXPREASON2);
			else
				str = MSG_INTL(MSG_PSYM_EXPREASON3);

			DBG_CALL(Dbg_move_parexpn(ofl->ofl_lml,
			    sdp->sd_name, str));

			for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp)) {
				DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0,
				    mdp->md_move, sdp));
				expand_move(ofl, sdp, mdp->md_move);
			}
			continue;
		}

		/*
		 * Process move table
		 */
		DBG_CALL(Dbg_move_outmove(ofl->ofl_lml, sdp->sd_name));

		for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp)) {
			Move	*imvp;
			int	idx = 1;
			Sym	*sym;

			imvp = mdp->md_move;
			sym = sdp->sd_sym;

			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 1, imvp, sdp));

			*omvp = *imvp;
			if ((flags & FLG_OF_RELOBJ) == 0) {
				if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
					Os_desc	*osp = sdp->sd_isc->is_osdesc;
					Word	ndx = osp->os_identndx;

					omvp->m_info =
					    /* LINTED */
					    ELF_M_INFO(ndx, imvp->m_info);

					if (ELF_ST_TYPE(sym->st_info) !=
					    STT_SECTION) {
						omvp->m_poffset =
						    sym->st_value -
						    osp->os_shdr->sh_addr +
						    imvp->m_poffset;
					}
				} else {
					omvp->m_info =
					    /* LINTED */
					    ELF_M_INFO(sdp->sd_symndx,
					    imvp->m_info);
				}
			} else {
				Boolean		isredloc = FALSE;

				if ((ELF_ST_BIND(sym->st_info) == STB_LOCAL) &&
				    (ofl->ofl_flags & FLG_OF_REDLSYM))
					isredloc = TRUE;

				if (isredloc && !(sdp->sd_move)) {
					Os_desc	*osp = sdp->sd_isc->is_osdesc;
					Word	ndx = osp->os_identndx;

					omvp->m_info =
					    /* LINTED */
					    ELF_M_INFO(ndx, imvp->m_info);

					omvp->m_poffset += sym->st_value;
				} else {
					if (isredloc)
						DBG_CALL(Dbg_syms_reduce(ofl,
						    DBG_SYM_REDUCE_RETAIN,
						    sdp, idx,
						    ofl->ofl_osmove->os_name));

					omvp->m_info =
					    /* LINTED */
					    ELF_M_INFO(sdp->sd_symndx,
					    imvp->m_info);
				}
			}

			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0, omvp, sdp));
			omvp++;
			idx++;
		}
	}
}

/*
 * Scan through the SHT_GROUP output sections.  Update their sh_link/sh_info
 * fields as well as the section contents.
 */
static uintptr_t
update_ogroup(Ofl_desc *ofl)
{
	Aliste		idx;
	Os_desc		*osp;
	uintptr_t	error = 0;

	for (APLIST_TRAVERSE(ofl->ofl_osgroups, idx, osp)) {
		Is_desc		*isp;
		Ifl_desc	*ifl;
		Shdr		*shdr = osp->os_shdr;
		Sym_desc	*sdp;
		Xword		i, grpcnt;
		Word		*gdata;

		/*
		 * Since input GROUP sections always create unique
		 * output GROUP sections - we know there is only one
		 * item on the list.
		 */
		isp = ld_os_first_isdesc(osp);

		ifl = isp->is_file;
		sdp = ifl->ifl_oldndx[isp->is_shdr->sh_info];
		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
		shdr->sh_info = sdp->sd_symndx;

		/*
		 * Scan through the group data section and update
		 * all of the links to new values.
		 */
		grpcnt = shdr->sh_size / shdr->sh_entsize;
		gdata = (Word *)osp->os_outdata->d_buf;

		for (i = 1; i < grpcnt; i++) {
			Os_desc	*_osp;
			Is_desc	*_isp = ifl->ifl_isdesc[gdata[i]];

			/*
			 * If the referenced section didn't make it to the
			 * output file - just zero out the entry.
			 */
			if ((_osp = _isp->is_osdesc) == NULL)
				gdata[i] = 0;
			else
				gdata[i] = (Word)elf_ndxscn(_osp->os_scn);
		}
	}
	return (error);
}

static void
update_ostrtab(Os_desc *osp, Str_tbl *stp, uint_t extra)
{
	Elf_Data	*data;

	if (osp == NULL)
		return;

	data = osp->os_outdata;
	assert(data->d_size == (st_getstrtab_sz(stp) + extra));
	(void) st_setstrbuf(stp, data->d_buf, data->d_size - extra);
	/* If leaving an extra hole at the end, zero it */
	if (extra > 0)
		(void) memset((char *)data->d_buf + data->d_size - extra,
		    0x0, extra);
}

/*
 * Update capabilities information.
 *
 * If string table capabilities exist, then the associated string must be
 * translated into an offset into the string table.
 */
static void
update_oscap(Ofl_desc *ofl)
{
	Os_desc		*strosp, *cosp;
	Cap		*cap;
	Str_tbl		*strtbl;
	Capstr		*capstr;
	size_t		stoff;
	Aliste		idx1;

	/*
	 * Determine which symbol table or string table is appropriate.
	 */
	if (OFL_IS_STATIC_OBJ(ofl)) {
		strosp = ofl->ofl_osstrtab;
		strtbl = ofl->ofl_strtab;
	} else {
		strosp = ofl->ofl_osdynstr;
		strtbl = ofl->ofl_dynstrtab;
	}

	/*
	 * If symbol capabilities exist, set the sh_link field of the .SUNW_cap
	 * section to the .SUNW_capinfo section.
	 */
	if (ofl->ofl_oscapinfo) {
		cosp = ofl->ofl_oscap;
		cosp->os_shdr->sh_link =
		    (Word)elf_ndxscn(ofl->ofl_oscapinfo->os_scn);
	}

	/*
	 * If there are capability strings to process, set the sh_info
	 * field of the .SUNW_cap section to the associated string table, and
	 * proceed to process any CA_SUNW_PLAT entries.
	 */
	if ((ofl->ofl_flags & FLG_OF_CAPSTRS) == 0)
		return;

	cosp = ofl->ofl_oscap;
	cosp->os_shdr->sh_info = (Word)elf_ndxscn(strosp->os_scn);

	cap = ofl->ofl_oscap->os_outdata->d_buf;

	/*
	 * Determine whether an object capability identifier, or object
	 * machine/platform capabilities exists.
	 */
	capstr = &ofl->ofl_ocapset.oc_id;
	if (capstr->cs_str) {
		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
	}
	for (ALIST_TRAVERSE(ofl->ofl_ocapset.oc_plat.cl_val, idx1, capstr)) {
		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
	}
	for (ALIST_TRAVERSE(ofl->ofl_ocapset.oc_mach.cl_val, idx1, capstr)) {
		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
	}

	/*
	 * Determine any symbol capability identifiers, or machine/platform
	 * capabilities.
	 */
	if (ofl->ofl_capgroups) {
		Cap_group	*cgp;

		for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
			Objcapset	*ocapset = &cgp->cg_set;
			Aliste		idx2;

			capstr = &ocapset->oc_id;
			if (capstr->cs_str) {
				(void) st_setstring(strtbl, capstr->cs_str,
				    &stoff);
				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
			}
			for (ALIST_TRAVERSE(ocapset->oc_plat.cl_val, idx2,
			    capstr)) {
				(void) st_setstring(strtbl, capstr->cs_str,
				    &stoff);
				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
			}
			for (ALIST_TRAVERSE(ocapset->oc_mach.cl_val, idx2,
			    capstr)) {
				(void) st_setstring(strtbl, capstr->cs_str,
				    &stoff);
				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
			}
		}
	}
}

/*
 * Update the .SUNW_capinfo, and possibly the .SUNW_capchain sections.
 */
static void
update_oscapinfo(Ofl_desc *ofl)
{
	Os_desc		*symosp, *ciosp, *ccosp = NULL;
	Capinfo		*ocapinfo;
	Capchain	*ocapchain;
	Cap_avlnode	*cav;
	Word		chainndx = 0;

	/*
	 * Determine which symbol table is appropriate.
	 */
	if (OFL_IS_STATIC_OBJ(ofl))
		symosp = ofl->ofl_ossymtab;
	else
		symosp = ofl->ofl_osdynsym;

	/*
	 * Update the .SUNW_capinfo sh_link to point to the appropriate symbol
	 * table section.  If we're creating a dynamic object, the
	 * .SUNW_capinfo sh_info is updated to point to the .SUNW_capchain
	 * section.
	 */
	ciosp = ofl->ofl_oscapinfo;
	ciosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);

	if (OFL_IS_STATIC_OBJ(ofl) == 0) {
		ccosp = ofl->ofl_oscapchain;
		ciosp->os_shdr->sh_info = (Word)elf_ndxscn(ccosp->os_scn);
	}

	/*
	 * Establish the data for each section.  The first element of each
	 * section defines the section's version number.
	 */
	ocapinfo = ciosp->os_outdata->d_buf;
	ocapinfo[0] = CAPINFO_CURRENT;
	if (ccosp) {
		ocapchain = ccosp->os_outdata->d_buf;
		ocapchain[chainndx++] = CAPCHAIN_CURRENT;
	}

	/*
	 * Traverse all capabilities families.  Each member has a .SUNW_capinfo
	 * assignment.  The .SUNW_capinfo entry differs for relocatable objects
	 * and dynamic objects.
	 *
	 * Relocatable objects:
	 *			ELF_C_GROUP		ELF_C_SYM
	 *
	 * Family lead:		CAPINFO_SUNW_GLOB	lead symbol index
	 * Family lead alias:	CAPINFO_SUNW_GLOB	lead symbol index
	 * Family member:	.SUNW_cap index		lead symbol index
	 *
	 * Dynamic objects:
	 *			ELF_C_GROUP		ELF_C_SYM
	 *
	 * Family lead:		CAPINFO_SUNW_GLOB	.SUNW_capchain index
	 * Family lead alias:	CAPINFO_SUNW_GLOB	.SUNW_capchain index
	 * Family member:	.SUNW_cap index		lead symbol index
	 *
	 * The ELF_C_GROUP field identifies a capabilities symbol.  Lead
	 * capability symbols, and lead capability aliases are identified by
	 * a CAPINFO_SUNW_GLOB group identifier.  For family members, the
	 * ELF_C_GROUP provides an index to the associate capabilities group
	 * (i.e, an index into the SUNW_cap section that defines a group).
	 *
	 * For relocatable objects, the ELF_C_SYM field identifies the lead
	 * capability symbol.  For the lead symbol itself, the .SUNW_capinfo
	 * index is the same as the ELF_C_SYM value.  For lead alias symbols,
	 * the .SUNW_capinfo index differs from the ELF_C_SYM value.  This
	 * differentiation of CAPINFO_SUNW_GLOB symbols allows ld(1) to
	 * identify, and propagate lead alias symbols.  For example, the lead
	 * capability symbol memcpy() would have the ELF_C_SYM for memcpy(),
	 * and the lead alias _memcpy() would also have the ELF_C_SYM for
	 * memcpy().
	 *
	 * For dynamic objects, both a lead capability symbol, and alias symbol
	 * would have a ELF_C_SYM value that represents the same capability
	 * chain index.  The capability chain allows ld.so.1 to traverse a
	 * family chain for a given lead symbol, and select the most appropriate
	 * family member.  The .SUNW_capchain array contains a series of symbol
	 * indexes for each family member:
	 *
	 *    chaincap[n]  chaincap[n + 1]  chaincap[n + 2]  chaincap[n + x]
	 *	foo() ndx    foo%x() ndx	foo%y() ndx	0
	 *
	 * For family members, the ELF_C_SYM value associates the capability
	 * members with their family lead symbol.  This association, although
	 * unused within a dynamic object, allows ld(1) to identify, and
	 * propagate family members when processing relocatable objects.
	 */
	for (cav = avl_first(ofl->ofl_capfamilies); cav;
	    cav = AVL_NEXT(ofl->ofl_capfamilies, cav)) {
		Cap_sym		*csp;
		Aliste		idx;
		Sym_desc	*asdp, *lsdp = cav->cn_symavlnode.sav_sdp;

		if (ccosp) {
			/*
			 * For a dynamic object, identify this lead symbol, and
			 * point it to the head of a capability chain.  Set the
			 * head of the capability chain to the same lead symbol.
			 */
			ocapinfo[lsdp->sd_symndx] =
			    ELF_C_INFO(chainndx, CAPINFO_SUNW_GLOB);
			ocapchain[chainndx] = lsdp->sd_symndx;
		} else {
			/*
			 * For a relocatable object, identify this lead symbol,
			 * and set the lead symbol index to itself.
			 */
			ocapinfo[lsdp->sd_symndx] =
			    ELF_C_INFO(lsdp->sd_symndx, CAPINFO_SUNW_GLOB);
		}

		/*
		 * Gather any lead symbol aliases.
		 */
		for (APLIST_TRAVERSE(cav->cn_aliases, idx, asdp)) {
			if (ccosp) {
				/*
				 * For a dynamic object, identify this lead
				 * alias symbol, and point it to the same
				 * capability chain index as the lead symbol.
				 */
				ocapinfo[asdp->sd_symndx] =
				    ELF_C_INFO(chainndx, CAPINFO_SUNW_GLOB);
			} else {
				/*
				 * For a relocatable object, identify this lead
				 * alias symbol, and set the lead symbol index
				 * to the lead symbol.
				 */
				ocapinfo[asdp->sd_symndx] =
				    ELF_C_INFO(lsdp->sd_symndx,
				    CAPINFO_SUNW_GLOB);
			}
		}

		chainndx++;

		/*
		 * Gather the family members.
		 */
		for (APLIST_TRAVERSE(cav->cn_members, idx, csp)) {
			Sym_desc	*msdp = csp->cs_sdp;

			/*
			 * Identify the members capability group, and the lead
			 * symbol of the family this symbol is a member of.
			 */
			ocapinfo[msdp->sd_symndx] =
			    ELF_C_INFO(lsdp->sd_symndx, csp->cs_group->cg_ndx);
			if (ccosp) {
				/*
				 * For a dynamic object, set the next capability
				 * chain to point to this family member.
				 */
				ocapchain[chainndx++] = msdp->sd_symndx;
			}
		}

		/*
		 * Any chain of family members is terminated with a 0 element.
		 */
		if (ccosp)
			ocapchain[chainndx++] = 0;
	}
}

/*
 * Translate the shdr->sh_{link, info} from its input section value to that
 * of the corresponding shdr->sh_{link, info} output section value.
 */
static Word
translate_link(Ofl_desc *ofl, Os_desc *osp, Word link, const char *msg)
{
	Is_desc		*isp;
	Ifl_desc	*ifl;

	/*
	 * Don't translate the special section numbers.
	 */
	if (link >= SHN_LORESERVE)
		return (link);

	/*
	 * Does this output section translate back to an input file.  If not
	 * then there is no translation to do.  In this case we will assume that
	 * if sh_link has a value, it's the right value.
	 */
	isp = ld_os_first_isdesc(osp);
	if ((ifl = isp->is_file) == NULL)
		return (link);

	/*
	 * Sanity check to make sure that the sh_{link, info} value
	 * is within range for the input file.
	 */
	if (link >= ifl->ifl_shnum) {
		ld_eprintf(ofl, ERR_WARNING, msg, ifl->ifl_name,
		    EC_WORD(isp->is_scnndx), isp->is_name, EC_XWORD(link));
		return (link);
	}

	/*
	 * Follow the link to the input section.
	 */
	if ((isp = ifl->ifl_isdesc[link]) == NULL)
		return (0);
	if ((osp = isp->is_osdesc) == NULL)
		return (0);

	/* LINTED */
	return ((Word)elf_ndxscn(osp->os_scn));
}

/*
 * Having created all of the necessary sections, segments, and associated
 * headers, fill in the program headers and update any other data in the
 * output image.  Some general rules:
 *
 *  -	If an interpreter is required always generate a PT_PHDR entry as
 *	well.  It is this entry that triggers the kernel into passing the
 *	interpreter an aux vector instead of just a file descriptor.
 *
 *  -	When generating an image that will be interpreted (ie. a dynamic
 *	executable, a shared object, or a static executable that has been
 *	provided with an interpreter - weird, but possible), make the initial
 *	loadable segment include both the ehdr and phdr[].  Both of these
 *	tables are used by the interpreter therefore it seems more intuitive
 *	to explicitly defined them as part of the mapped image rather than
 *	relying on page rounding by the interpreter to allow their access.
 *
 *  -	When generating a static image that does not require an interpreter
 *	have the first loadable segment indicate the address of the first
 *	.section as the start address (things like /kernel/unix and ufsboot
 *	expect this behavior).
 */
uintptr_t
ld_update_outfile(Ofl_desc *ofl)
{
	Addr		size, etext, vaddr;
	Sg_desc		*sgp;
	Sg_desc		*dtracesgp = NULL, *capsgp = NULL, *intpsgp = NULL;
	Os_desc		*osp;
	int		phdrndx = 0, segndx = -1, secndx, intppndx, intpsndx;
	int		dtracepndx, dtracesndx, cappndx, capsndx;
	Ehdr		*ehdr = ofl->ofl_nehdr;
	Shdr		*hshdr;
	Phdr		*_phdr = NULL;
	Word		phdrsz = (ehdr->e_phnum * ehdr->e_phentsize), shscnndx;
	ofl_flag_t	flags = ofl->ofl_flags;
	Word		ehdrsz = ehdr->e_ehsize;
	Boolean		nobits;
	Off		offset;
	Aliste		idx1;

	/*
	 * Initialize the starting address for the first segment.  Executables
	 * have different starting addresses depending upon the target ABI,
	 * where as shared objects have a starting address of 0.  If this is
	 * a 64-bit executable that is being constructed to run in a restricted
	 * address space, use an alternative origin that will provide more free
	 * address space for the the eventual process.
	 */
	if (ofl->ofl_flags & FLG_OF_EXEC) {
#if	defined(_ELF64)
		if (ofl->ofl_ocapset.oc_sf_1.cm_val & SF1_SUNW_ADDR32)
			vaddr = ld_targ.t_m.m_segm_aorigin;
		else
#endif
			vaddr = ld_targ.t_m.m_segm_origin;
	} else
		vaddr = 0;

	/*
	 * Loop through the segment descriptors and pick out what we need.
	 */
	DBG_CALL(Dbg_seg_title(ofl->ofl_lml));
	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
		Phdr		*phdr = &(sgp->sg_phdr);
		Xword		p_align;
		Aliste		idx2;
		Sym_desc	*sdp;

		segndx++;

		/*
		 * If an interpreter is required generate a PT_INTERP and
		 * PT_PHDR program header entry.  The PT_PHDR entry describes
		 * the program header table itself.  This information will be
		 * passed via the aux vector to the interpreter (ld.so.1).
		 * The program header array is actually part of the first
		 * loadable segment (and the PT_PHDR entry is the first entry),
		 * therefore its virtual address isn't known until the first
		 * loadable segment is processed.
		 */
		if (phdr->p_type == PT_PHDR) {
			if (ofl->ofl_osinterp) {
				phdr->p_offset = ehdr->e_phoff;
				phdr->p_filesz = phdr->p_memsz = phdrsz;

				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
				ofl->ofl_phdr[phdrndx++] = *phdr;
			}
			continue;
		}
		if (phdr->p_type == PT_INTERP) {
			if (ofl->ofl_osinterp) {
				intpsgp = sgp;
				intpsndx = segndx;
				intppndx = phdrndx++;
			}
			continue;
		}

		/*
		 * If we are creating a PT_SUNWDTRACE segment, remember where
		 * the program header is.  The header values are assigned after
		 * update_osym() has completed and the symbol table addresses
		 * have been updated.
		 */
		if (phdr->p_type == PT_SUNWDTRACE) {
			if (ofl->ofl_dtracesym &&
			    ((flags & FLG_OF_RELOBJ) == 0)) {
				dtracesgp = sgp;
				dtracesndx = segndx;
				dtracepndx = phdrndx++;
			}
			continue;
		}

		/*
		 * If a hardware/software capabilities section is required,
		 * generate the PT_SUNWCAP header.  Note, as this comes before
		 * the first loadable segment, we don't yet know its real
		 * virtual address.  This is updated later.
		 */
		if (phdr->p_type == PT_SUNWCAP) {
			if (ofl->ofl_oscap && (ofl->ofl_flags & FLG_OF_PTCAP) &&
			    ((flags & FLG_OF_RELOBJ) == 0)) {
				capsgp = sgp;
				capsndx = segndx;
				cappndx = phdrndx++;
			}
			continue;
		}

		/*
		 * As the dynamic program header occurs after the loadable
		 * headers in the segment descriptor table, all the address
		 * information for the .dynamic output section will have been
		 * figured out by now.
		 */
		if (phdr->p_type == PT_DYNAMIC) {
			if (OFL_ALLOW_DYNSYM(ofl)) {
				Shdr	*shdr = ofl->ofl_osdynamic->os_shdr;

				phdr->p_vaddr = shdr->sh_addr;
				phdr->p_offset = shdr->sh_offset;
				phdr->p_filesz = shdr->sh_size;
				phdr->p_flags = ld_targ.t_m.m_dataseg_perm;

				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
				ofl->ofl_phdr[phdrndx++] = *phdr;
			}
			continue;
		}

		/*
		 * As the unwind (.eh_frame_hdr) program header occurs after
		 * the loadable headers in the segment descriptor table, all
		 * the address information for the .eh_frame output section
		 * will have been figured out by now.
		 */
		if (phdr->p_type == PT_SUNW_UNWIND) {
			Shdr	    *shdr;

			if (ofl->ofl_unwindhdr == NULL)
				continue;

			shdr = ofl->ofl_unwindhdr->os_shdr;

			phdr->p_flags = PF_R;
			phdr->p_vaddr = shdr->sh_addr;
			phdr->p_memsz = shdr->sh_size;
			phdr->p_filesz = shdr->sh_size;
			phdr->p_offset = shdr->sh_offset;
			phdr->p_align = shdr->sh_addralign;
			phdr->p_paddr = 0;
			ofl->ofl_phdr[phdrndx++] = *phdr;
			continue;
		}

		/*
		 * The sunwstack program is used to convey non-default
		 * flags for the process stack. Only emit it if it would
		 * change the default.
		 */
		if (phdr->p_type == PT_SUNWSTACK) {
			if (((flags & FLG_OF_RELOBJ) == 0) &&
			    ((sgp->sg_flags & FLG_SG_DISABLED) == 0))
				ofl->ofl_phdr[phdrndx++] = *phdr;
			continue;
		}

		/*
		 * As the TLS program header occurs after the loadable
		 * headers in the segment descriptor table, all the address
		 * information for the .tls output section will have been
		 * figured out by now.
		 */
		if (phdr->p_type == PT_TLS) {
			Os_desc		*tlsosp;
			Shdr		*lastfileshdr = NULL;
			Shdr		*firstshdr = NULL, *lastshdr;
			Aliste		idx;

			if (ofl->ofl_ostlsseg == NULL)
				continue;

			/*
			 * Scan the output sections that have contributed TLS.
			 * Remember the first and last so as to determine the
			 * TLS memory size requirement.  Remember the last
			 * progbits section to determine the TLS data
			 * contribution, which determines the TLS program
			 * header filesz.
			 */
			for (APLIST_TRAVERSE(ofl->ofl_ostlsseg, idx, tlsosp)) {
				Shdr	*tlsshdr = tlsosp->os_shdr;

				if (firstshdr == NULL)
					firstshdr = tlsshdr;
				if (tlsshdr->sh_type != SHT_NOBITS)
					lastfileshdr = tlsshdr;
				lastshdr = tlsshdr;
			}

			phdr->p_flags = PF_R | PF_W;
			phdr->p_vaddr = firstshdr->sh_addr;
			phdr->p_offset = firstshdr->sh_offset;
			phdr->p_align = firstshdr->sh_addralign;

			/*
			 * Determine the initialized TLS data size.  This
			 * address range is from the start of the TLS segment
			 * to the end of the last piece of initialized data.
			 */
			if (lastfileshdr)
				phdr->p_filesz = lastfileshdr->sh_offset +
				    lastfileshdr->sh_size - phdr->p_offset;
			else
				phdr->p_filesz = 0;

			/*
			 * Determine the total TLS memory size.  This includes
			 * all TLS data and TLS uninitialized data.  This
			 * address range is from the start of the TLS segment
			 * to the memory address of the last piece of
			 * uninitialized data.
			 */
			phdr->p_memsz = lastshdr->sh_addr +
			    lastshdr->sh_size - phdr->p_vaddr;

			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
			ofl->ofl_phdr[phdrndx] = *phdr;
			ofl->ofl_tlsphdr = &ofl->ofl_phdr[phdrndx++];
			continue;
		}

		/*
		 * If this is an empty segment declaration, it will occur after
		 * all other loadable segments.  As empty segments can be
		 * defined with fixed addresses, make sure that no loadable
		 * segments overlap.  This might occur as the object evolves
		 * and the loadable segments grow, thus encroaching upon an
		 * existing segment reservation.
		 *
		 * Segments are only created for dynamic objects, thus this
		 * checking can be skipped when building a relocatable object.
		 */
		if (!(flags & FLG_OF_RELOBJ) &&
		    (sgp->sg_flags & FLG_SG_EMPTY)) {
			int	i;
			Addr	v_e;

			vaddr = phdr->p_vaddr;
			phdr->p_memsz = sgp->sg_length;
			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
			ofl->ofl_phdr[phdrndx++] = *phdr;

			if (phdr->p_type != PT_LOAD)
				continue;

			v_e = vaddr + phdr->p_memsz;

			/*
			 * Check overlaps
			 */
			for (i = 0; i < phdrndx - 1; i++) {
				Addr	p_s = (ofl->ofl_phdr[i]).p_vaddr;
				Addr	p_e;

				if ((ofl->ofl_phdr[i]).p_type != PT_LOAD)
					continue;

				p_e = p_s + (ofl->ofl_phdr[i]).p_memsz;
				if (((p_s <= vaddr) && (p_e > vaddr)) ||
				    ((vaddr <= p_s) && (v_e > p_s)))
					ld_eprintf(ofl, ERR_WARNING,
					    MSG_INTL(MSG_UPD_SEGOVERLAP),
					    ofl->ofl_name, EC_ADDR(p_e),
					    sgp->sg_name, EC_ADDR(vaddr));
			}
			continue;
		}

		/*
		 * Having processed any of the special program headers any
		 * remaining headers will be built to express individual
		 * segments.  Segments are only built if they have output
		 * section descriptors associated with them (ie. some form of
		 * input section has been matched to this segment).
		 */
		if (sgp->sg_osdescs == NULL)
			continue;

		/*
		 * Determine the segments offset and size from the section
		 * information provided from elf_update().
		 * Allow for multiple NOBITS sections.
		 */
		osp = sgp->sg_osdescs->apl_data[0];
		hshdr = osp->os_shdr;

		phdr->p_filesz = 0;
		phdr->p_memsz = 0;
		phdr->p_offset = offset = hshdr->sh_offset;

		nobits = ((hshdr->sh_type == SHT_NOBITS) &&
		    ((sgp->sg_flags & FLG_SG_PHREQ) == 0));

		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
			Shdr	*shdr = osp->os_shdr;

			p_align = 0;
			if (shdr->sh_addralign > p_align)
				p_align = shdr->sh_addralign;

			offset = (Off)S_ROUND(offset, shdr->sh_addralign);
			offset += shdr->sh_size;

			if (shdr->sh_type != SHT_NOBITS) {
				if (nobits) {
					ld_eprintf(ofl, ERR_FATAL,
					    MSG_INTL(MSG_UPD_NOBITS));
					return (S_ERROR);
				}
				phdr->p_filesz = offset - phdr->p_offset;
			} else if ((sgp->sg_flags & FLG_SG_PHREQ) == 0)
				nobits = TRUE;
		}
		phdr->p_memsz = offset - hshdr->sh_offset;

		/*
		 * If this is the first loadable segment of a dynamic object,
		 * or an interpreter has been specified (a static object built
		 * with an interpreter will still be given a PT_HDR entry), then
		 * compensate for the elf header and program header array.  Both
		 * of these are actually part of the loadable segment as they
		 * may be inspected by the interpreter.  Adjust the segments
		 * size and offset accordingly.
		 */
		if ((_phdr == NULL) && (phdr->p_type == PT_LOAD) &&
		    ((ofl->ofl_osinterp) || (flags & FLG_OF_DYNAMIC)) &&
		    (!(ofl->ofl_dtflags_1 & DF_1_NOHDR))) {
			size = (Addr)S_ROUND((phdrsz + ehdrsz),
			    hshdr->sh_addralign);
			phdr->p_offset -= size;
			phdr->p_filesz += size;
			phdr->p_memsz += size;
		}

		/*
		 * If segment size symbols are required (specified via a
		 * mapfile) update their value.
		 */
		for (APLIST_TRAVERSE(sgp->sg_sizesym, idx2, sdp))
			sdp->sd_sym->st_value = phdr->p_memsz;

		/*
		 * If no file content has been assigned to this segment (it
		 * only contains no-bits sections), then reset the offset for
		 * consistency.
		 */
		if (phdr->p_filesz == 0)
			phdr->p_offset = 0;

		/*
		 * If a virtual address has been specified for this segment
		 * from a mapfile use it and make sure the previous segment
		 * does not run into this segment.
		 */
		if (phdr->p_type == PT_LOAD) {
			if ((sgp->sg_flags & FLG_SG_P_VADDR)) {
				if (_phdr && (vaddr > phdr->p_vaddr) &&
				    (phdr->p_type == PT_LOAD))
					ld_eprintf(ofl, ERR_WARNING,
					    MSG_INTL(MSG_UPD_SEGOVERLAP),
					    ofl->ofl_name, EC_ADDR(vaddr),
					    sgp->sg_name,
					    EC_ADDR(phdr->p_vaddr));
				vaddr = phdr->p_vaddr;
				phdr->p_align = 0;
			} else {
				vaddr = phdr->p_vaddr =
				    (Addr)S_ROUND(vaddr, phdr->p_align);
			}
		}

		/*
		 * Adjust the address offset and p_align if needed.
		 */
		if (((sgp->sg_flags & FLG_SG_P_VADDR) == 0) &&
		    ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0)) {
			if (phdr->p_align != 0)
				vaddr += phdr->p_offset % phdr->p_align;
			else
				vaddr += phdr->p_offset;
			phdr->p_vaddr = vaddr;
		}

		/*
		 * If an interpreter is required set the virtual address of the
		 * PT_PHDR program header now that we know the virtual address
		 * of the loadable segment that contains it.  Update the
		 * PT_SUNWCAP header similarly.
		 */
		if ((_phdr == NULL) && (phdr->p_type == PT_LOAD)) {
			_phdr = phdr;

			if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0) {
				if (ofl->ofl_osinterp)
					ofl->ofl_phdr[0].p_vaddr =
					    vaddr + ehdrsz;

				/*
				 * Finally, if we're creating a dynamic object
				 * (or a static object in which an interpreter
				 * is specified) update the vaddr to reflect
				 * the address of the first section within this
				 * segment.
				 */
				if ((ofl->ofl_osinterp) ||
				    (flags & FLG_OF_DYNAMIC))
					vaddr += size;
			} else {
				/*
				 * If the DF_1_NOHDR flag was set, and an
				 * interpreter is being generated, the PT_PHDR
				 * will not be part of any loadable segment.
				 */
				if (ofl->ofl_osinterp) {
					ofl->ofl_phdr[0].p_vaddr = 0;
					ofl->ofl_phdr[0].p_memsz = 0;
					ofl->ofl_phdr[0].p_flags = 0;
				}
			}
		}

		/*
		 * Ensure the ELF entry point defaults to zero.  Typically, this
		 * value is overridden in update_oehdr() to one of the standard
		 * entry points.  Historically, this default was set to the
		 * address of first executable section, but this has since been
		 * found to be more confusing than it is helpful.
		 */
		ehdr->e_entry = 0;

		DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));

		/*
		 * Traverse the output section descriptors for this segment so
		 * that we can update the section headers addresses.  We've
		 * calculated the virtual address of the initial section within
		 * this segment, so each successive section can be calculated
		 * based on their offsets from each other.
		 */
		secndx = 0;
		hshdr = 0;
		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
			Shdr	*shdr = osp->os_shdr;

			if (shdr->sh_link)
				shdr->sh_link = translate_link(ofl, osp,
				    shdr->sh_link, MSG_INTL(MSG_FIL_INVSHLINK));

			if (shdr->sh_info && (shdr->sh_flags & SHF_INFO_LINK))
				shdr->sh_info = translate_link(ofl, osp,
				    shdr->sh_info, MSG_INTL(MSG_FIL_INVSHINFO));

			if (!(flags & FLG_OF_RELOBJ) &&
			    (phdr->p_type == PT_LOAD)) {
				if (hshdr)
					vaddr += (shdr->sh_offset -
					    hshdr->sh_offset);

				shdr->sh_addr = vaddr;
				hshdr = shdr;
			}

			DBG_CALL(Dbg_seg_os(ofl, osp, secndx));
			secndx++;
		}

		/*
		 * Establish the virtual address of the end of the last section
		 * in this segment so that the next segments offset can be
		 * calculated from this.
		 */
		if (hshdr)
			vaddr += hshdr->sh_size;

		/*
		 * Output sections for this segment complete.  Adjust the
		 * virtual offset for the last sections size, and make sure we
		 * haven't exceeded any maximum segment length specification.
		 */
		if ((sgp->sg_length != 0) && (sgp->sg_length < phdr->p_memsz)) {
			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_UPD_LARGSIZE),
			    ofl->ofl_name, sgp->sg_name,
			    EC_XWORD(phdr->p_memsz), EC_XWORD(sgp->sg_length));
			return (S_ERROR);
		}

		if (phdr->p_type == PT_NOTE) {
			phdr->p_vaddr = 0;
			phdr->p_paddr = 0;
			phdr->p_align = 0;
			phdr->p_memsz = 0;
		}

		if ((phdr->p_type != PT_NULL) && !(flags & FLG_OF_RELOBJ))
			ofl->ofl_phdr[phdrndx++] = *phdr;
	}

	/*
	 * Update any new output sections.  When building the initial output
	 * image, a number of sections were created but left uninitialized (eg.
	 * .dynsym, .dynstr, .symtab, .symtab, etc.).  Here we update these
	 * sections with the appropriate data.  Other sections may still be
	 * modified via reloc_process().
	 *
	 * Copy the interpreter name into the .interp section.
	 */
	if (ofl->ofl_interp)
		(void) strcpy((char *)ofl->ofl_osinterp->os_outdata->d_buf,
		    ofl->ofl_interp);

	/*
	 * Update the .shstrtab, .strtab and .dynstr sections.
	 */
	update_ostrtab(ofl->ofl_osshstrtab, ofl->ofl_shdrsttab, 0);
	update_ostrtab(ofl->ofl_osstrtab, ofl->ofl_strtab, 0);
	update_ostrtab(ofl->ofl_osdynstr, ofl->ofl_dynstrtab, DYNSTR_EXTRA_PAD);

	/*
	 * Build any output symbol tables, the symbols information is copied
	 * and updated into the new output image.
	 */
	if ((etext = update_osym(ofl)) == (Addr)S_ERROR)
		return (S_ERROR);

	/*
	 * If we have an PT_INTERP phdr, update it now from the associated
	 * section information.
	 */
	if (intpsgp) {
		Phdr	*phdr = &(intpsgp->sg_phdr);
		Shdr	*shdr = ofl->ofl_osinterp->os_shdr;

		phdr->p_vaddr = shdr->sh_addr;
		phdr->p_offset = shdr->sh_offset;
		phdr->p_memsz = phdr->p_filesz = shdr->sh_size;
		phdr->p_flags = PF_R;

		DBG_CALL(Dbg_seg_entry(ofl, intpsndx, intpsgp));
		ofl->ofl_phdr[intppndx] = *phdr;
	}

	/*
	 * If we have a PT_SUNWDTRACE phdr, update it now with the address of
	 * the symbol.  It's only now been updated via update_sym().
	 */
	if (dtracesgp) {
		Phdr		*aphdr, *phdr = &(dtracesgp->sg_phdr);
		Sym_desc	*sdp = ofl->ofl_dtracesym;

		phdr->p_vaddr = sdp->sd_sym->st_value;
		phdr->p_memsz = sdp->sd_sym->st_size;

		/*
		 * Take permissions from the segment that the symbol is
		 * associated with.
		 */
		aphdr = &sdp->sd_isc->is_osdesc->os_sgdesc->sg_phdr;
		assert(aphdr);
		phdr->p_flags = aphdr->p_flags;

		DBG_CALL(Dbg_seg_entry(ofl, dtracesndx, dtracesgp));
		ofl->ofl_phdr[dtracepndx] = *phdr;
	}

	/*
	 * If we have a PT_SUNWCAP phdr, update it now from the associated
	 * section information.
	 */
	if (capsgp) {
		Phdr	*phdr = &(capsgp->sg_phdr);
		Shdr	*shdr = ofl->ofl_oscap->os_shdr;

		phdr->p_vaddr = shdr->sh_addr;
		phdr->p_offset = shdr->sh_offset;
		phdr->p_memsz = phdr->p_filesz = shdr->sh_size;
		phdr->p_flags = PF_R;

		DBG_CALL(Dbg_seg_entry(ofl, capsndx, capsgp));
		ofl->ofl_phdr[cappndx] = *phdr;
	}

	/*
	 * Update the GROUP sections.
	 */
	if (update_ogroup(ofl) == S_ERROR)
		return (S_ERROR);

	/*
	 * Update Move Table.
	 */
	if (ofl->ofl_osmove || ofl->ofl_isparexpn)
		update_move(ofl);

	/*
	 * Build any output headers, version information, dynamic structure and
	 * syminfo structure.
	 */
	if (update_oehdr(ofl) == S_ERROR)
		return (S_ERROR);
	if (!(flags & FLG_OF_NOVERSEC)) {
		if ((flags & FLG_OF_VERDEF) &&
		    (update_overdef(ofl) == S_ERROR))
			return (S_ERROR);
		if ((flags & FLG_OF_VERNEED) &&
		    (update_overneed(ofl) == S_ERROR))
			return (S_ERROR);
		if (flags & (FLG_OF_VERNEED | FLG_OF_VERDEF))
			update_oversym(ofl);
	}
	if (flags & FLG_OF_DYNAMIC) {
		if (update_odynamic(ofl) == S_ERROR)
			return (S_ERROR);
	}
	if (ofl->ofl_ossyminfo) {
		if (update_osyminfo(ofl) == S_ERROR)
			return (S_ERROR);
	}

	/*
	 * Update capabilities information if required.
	 */
	if (ofl->ofl_oscap)
		update_oscap(ofl);
	if (ofl->ofl_oscapinfo)
		update_oscapinfo(ofl);

	/*
	 * Sanity test: the first and last data byte of a string table
	 * must be NULL.
	 */
	assert((ofl->ofl_osshstrtab == NULL) ||
	    (*((char *)ofl->ofl_osshstrtab->os_outdata->d_buf) == '\0'));
	assert((ofl->ofl_osshstrtab == NULL) ||
	    (*(((char *)ofl->ofl_osshstrtab->os_outdata->d_buf) +
	    ofl->ofl_osshstrtab->os_outdata->d_size - 1) == '\0'));

	assert((ofl->ofl_osstrtab == NULL) ||
	    (*((char *)ofl->ofl_osstrtab->os_outdata->d_buf) == '\0'));
	assert((ofl->ofl_osstrtab == NULL) ||
	    (*(((char *)ofl->ofl_osstrtab->os_outdata->d_buf) +
	    ofl->ofl_osstrtab->os_outdata->d_size - 1) == '\0'));

	assert((ofl->ofl_osdynstr == NULL) ||
	    (*((char *)ofl->ofl_osdynstr->os_outdata->d_buf) == '\0'));
	assert((ofl->ofl_osdynstr == NULL) ||
	    (*(((char *)ofl->ofl_osdynstr->os_outdata->d_buf) +
	    ofl->ofl_osdynstr->os_outdata->d_size - DYNSTR_EXTRA_PAD - 1) ==
	    '\0'));

	/*
	 * Emit Strtab diagnostics.
	 */
	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osshstrtab,
	    ofl->ofl_shdrsttab));
	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osstrtab,
	    ofl->ofl_strtab));
	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osdynstr,
	    ofl->ofl_dynstrtab));

	/*
	 * Initialize the section headers string table index within the elf
	 * header.
	 */
	/* LINTED */
	if ((shscnndx = elf_ndxscn(ofl->ofl_osshstrtab->os_scn)) <
	    SHN_LORESERVE) {
		ofl->ofl_nehdr->e_shstrndx =
		    /* LINTED */
		    (Half)shscnndx;
	} else {
		/*
		 * If the STRTAB section index doesn't fit into
		 * e_shstrndx, then we store it in 'shdr[0].st_link'.
		 */
		Elf_Scn	*scn;
		Shdr	*shdr0;

		if ((scn = elf_getscn(ofl->ofl_elf, 0)) == NULL) {
			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
			    ofl->ofl_name);
			return (S_ERROR);
		}
		if ((shdr0 = elf_getshdr(scn)) == NULL) {
			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
			    ofl->ofl_name);
			return (S_ERROR);
		}
		ofl->ofl_nehdr->e_shstrndx = SHN_XINDEX;
		shdr0->sh_link = shscnndx;
	}

	return ((uintptr_t)etext);
}