summaryrefslogtreecommitdiff
path: root/bfd/elf32-nds32.c
blob: ffad0dbd8e4307bb603aa9c1484a8b7ca51843e0 (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
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
/* NDS32-specific support for 32-bit ELF.
   Copyright (C) 2012-2016 Free Software Foundation, Inc.
   Contributed by Andes Technology Corporation.

   This file is part of BFD, the Binary File Descriptor library.

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

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

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */


#include "sysdep.h"
#include "bfd.h"
#include "bfd_stdint.h"
#include "bfdlink.h"
#include "libbfd.h"
#include "elf-bfd.h"
#include "libiberty.h"
#include "bfd_stdint.h"
#include "elf/nds32.h"
#include "opcode/nds32.h"
#include "elf32-nds32.h"
#include "opcode/cgen.h"
#include "../opcodes/nds32-opc.h"

/* Relocation HOWTO functions.  */
static bfd_reloc_status_type nds32_elf_ignore_reloc
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
static bfd_reloc_status_type nds32_elf_hi20_reloc
  (bfd *, arelent *, asymbol *, void *,
   asection *, bfd *, char **);
static bfd_reloc_status_type nds32_elf_lo12_reloc
  (bfd *, arelent *, asymbol *, void *,
   asection *, bfd *, char **);
static bfd_reloc_status_type nds32_elf_generic_reloc
  (bfd *, arelent *, asymbol *, void *,
   asection *, bfd *, char **);
static bfd_reloc_status_type nds32_elf_sda15_reloc
  (bfd *, arelent *, asymbol *, void *,
   asection *, bfd *, char **);

/* Helper functions for HOWTO.  */
static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
  (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
   asection *, bfd_vma, bfd_vma);
static void nds32_elf_relocate_hi20
  (bfd *, int, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *, bfd_vma);
static reloc_howto_type *bfd_elf32_bfd_reloc_type_table_lookup
  (enum elf_nds32_reloc_type);
static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
  (bfd *, bfd_reloc_code_real_type);

/* Target hooks.  */
static void nds32_info_to_howto_rel
  (bfd *, arelent *, Elf_Internal_Rela *dst);
static void nds32_info_to_howto
  (bfd *, arelent *, Elf_Internal_Rela *dst);
static bfd_boolean nds32_elf_add_symbol_hook
  (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, const char **,
   flagword *, asection **, bfd_vma *);
static bfd_boolean nds32_elf_relocate_section
  (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
   Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
static bfd_boolean nds32_elf_object_p (bfd *);
static void nds32_elf_final_write_processing (bfd *, bfd_boolean);
static bfd_boolean nds32_elf_set_private_flags (bfd *, flagword);
static bfd_boolean nds32_elf_merge_private_bfd_data (bfd *, bfd *);
static bfd_boolean nds32_elf_print_private_bfd_data (bfd *, void *);
static bfd_boolean nds32_elf_gc_sweep_hook
  (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
static bfd_boolean nds32_elf_check_relocs
  (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
static asection *nds32_elf_gc_mark_hook
  (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
   struct elf_link_hash_entry *, Elf_Internal_Sym *);
static bfd_boolean nds32_elf_adjust_dynamic_symbol
  (struct bfd_link_info *, struct elf_link_hash_entry *);
static bfd_boolean nds32_elf_size_dynamic_sections
  (bfd *, struct bfd_link_info *);
static bfd_boolean nds32_elf_create_dynamic_sections
  (bfd *, struct bfd_link_info *);
static bfd_boolean nds32_elf_finish_dynamic_sections
  (bfd *, struct bfd_link_info *info);
static bfd_boolean nds32_elf_finish_dynamic_symbol
  (bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
   Elf_Internal_Sym *);
static bfd_boolean nds32_elf_mkobject (bfd *);

/* Nds32 helper functions.  */
static bfd_reloc_status_type nds32_elf_final_sda_base
  (bfd *, struct bfd_link_info *, bfd_vma *, bfd_boolean);
static bfd_boolean allocate_dynrelocs (struct elf_link_hash_entry *, void *);
static bfd_boolean readonly_dynrelocs (struct elf_link_hash_entry *, void *);
static Elf_Internal_Rela *find_relocs_at_address
  (Elf_Internal_Rela *, Elf_Internal_Rela *,
   Elf_Internal_Rela *, enum elf_nds32_reloc_type);
static bfd_vma calculate_memory_address
(bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
static int nds32_get_section_contents (bfd *, asection *,
				       bfd_byte **, bfd_boolean);
static bfd_boolean nds32_elf_ex9_build_hash_table
(bfd *, asection *, struct bfd_link_info *);
static bfd_boolean nds32_elf_ex9_itb_base (struct bfd_link_info *);
static void nds32_elf_ex9_import_table (struct bfd_link_info *);
static void nds32_elf_ex9_finish (struct bfd_link_info *);
static void nds32_elf_ex9_reloc_jmp (struct bfd_link_info *);
static void nds32_elf_get_insn_with_reg
  (Elf_Internal_Rela *, uint32_t, uint32_t *);
static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
				 Elf_Internal_Sym **);
static bfd_boolean nds32_elf_ex9_replace_instruction
  (struct bfd_link_info *, bfd *, asection *);
static bfd_boolean nds32_elf_ifc_calc (struct bfd_link_info *, bfd *,
				       asection *);
static bfd_boolean nds32_elf_ifc_finish (struct bfd_link_info *);
static bfd_boolean nds32_elf_ifc_replace (struct bfd_link_info *);
static bfd_boolean nds32_elf_ifc_reloc (void);
static bfd_boolean  nds32_relax_fp_as_gp
  (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
   Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
   Elf_Internal_Sym *isymbuf);
static bfd_boolean nds32_fag_remove_unused_fpbase
  (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
   Elf_Internal_Rela *irelend);
static bfd_byte *
nds32_elf_get_relocated_section_contents (bfd *abfd,
					  struct bfd_link_info *link_info,
					  struct bfd_link_order *link_order,
					  bfd_byte *data,
					  bfd_boolean relocatable,
					  asymbol **symbols);

enum
{
  MACH_V1 = bfd_mach_n1h,
  MACH_V2 = bfd_mach_n1h_v2,
  MACH_V3 = bfd_mach_n1h_v3,
  MACH_V3M = bfd_mach_n1h_v3m
};

#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

/* The name of the dynamic interpreter.  This is put in the .interp
   section.  */
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"

/* The nop opcode we use.  */
#define NDS32_NOP32 0x40000009
#define NDS32_NOP16 0x9200

/* The size in bytes of an entry in the procedure linkage table.  */
#define PLT_ENTRY_SIZE 24
#define PLT_HEADER_SIZE 24

/* The first entry in a procedure linkage table are reserved,
   and the initial contents are unimportant (we zero them out).
   Subsequent entries look like this.  */
#define PLT0_ENTRY_WORD0  0x46f00000		/* sethi   r15, HI20(.got+4)      */
#define PLT0_ENTRY_WORD1  0x58f78000		/* ori     r15, r25, LO12(.got+4) */
#define PLT0_ENTRY_WORD2  0x05178000		/* lwi     r17, [r15+0]           */
#define PLT0_ENTRY_WORD3  0x04f78001		/* lwi     r15, [r15+4]           */
#define PLT0_ENTRY_WORD4  0x4a003c00		/* jr      r15                    */

/* $ta is change to $r15 (from $r25).  */
#define PLT0_PIC_ENTRY_WORD0  0x46f00000	/* sethi   r15, HI20(got[1]@GOT)  */
#define PLT0_PIC_ENTRY_WORD1  0x58f78000	/* ori     r15, r15, LO12(got[1]@GOT) */
#define PLT0_PIC_ENTRY_WORD2  0x40f7f400	/* add     r15, gp, r15           */
#define PLT0_PIC_ENTRY_WORD3  0x05178000	/* lwi     r17, [r15+0]           */
#define PLT0_PIC_ENTRY_WORD4  0x04f78001	/* lwi     r15, [r15+4]           */
#define PLT0_PIC_ENTRY_WORD5  0x4a003c00	/* jr      r15                    */

#define PLT_ENTRY_WORD0  0x46f00000		/* sethi   r15, HI20(&got[n+3])      */
#define PLT_ENTRY_WORD1  0x04f78000		/* lwi     r15, r15, LO12(&got[n+3]) */
#define PLT_ENTRY_WORD2  0x4a003c00		/* jr      r15                       */
#define PLT_ENTRY_WORD3  0x45000000		/* movi    r16, sizeof(RELA) * n     */
#define PLT_ENTRY_WORD4  0x48000000		/* j      .plt0.                     */

#define PLT_PIC_ENTRY_WORD0  0x46f00000		/* sethi  r15, HI20(got[n+3]@GOT)    */
#define PLT_PIC_ENTRY_WORD1  0x58f78000		/* ori    r15, r15,    LO12(got[n+3]@GOT) */
#define PLT_PIC_ENTRY_WORD2  0x38febc02		/* lw     r15, [gp+r15]              */
#define PLT_PIC_ENTRY_WORD3  0x4a003c00		/* jr     r15                        */
#define PLT_PIC_ENTRY_WORD4  0x45000000		/* movi   r16, sizeof(RELA) * n      */
#define PLT_PIC_ENTRY_WORD5  0x48000000		/* j      .plt0                      */

/* These are macros used to get the relocation accurate value.  */
#define ACCURATE_8BIT_S1	(0x100)
#define ACCURATE_U9BIT_S1	(0x400)
#define ACCURATE_12BIT_S1	(0x2000)
#define ACCURATE_14BIT_S1	(0x4000)
#define ACCURATE_19BIT		(0x40000)

/* These are macros used to get the relocation conservative value.  */
#define CONSERVATIVE_8BIT_S1	(0x100 - 4)
#define CONSERVATIVE_14BIT_S1	(0x4000 - 4)
#define CONSERVATIVE_16BIT_S1	(0x10000 - 4)
#define CONSERVATIVE_24BIT_S1	(0x1000000 - 4)
/* These must be more conservative because the address may be in
   different segment.  */
#define CONSERVATIVE_15BIT	(0x4000 - 0x1000)
#define CONSERVATIVE_15BIT_S1	(0x8000 - 0x1000)
#define CONSERVATIVE_15BIT_S2	(0x10000 - 0x1000)
#define CONSERVATIVE_19BIT	(0x40000 - 0x1000)
#define CONSERVATIVE_20BIT	(0x80000 - 0x1000)

/* Size of small data/bss sections, used to calculate SDA_BASE.  */
static long got_size = 0;
static int is_SDA_BASE_set = 0;
static int is_ITB_BASE_set = 0;

/* Convert ELF-VER in eflags to string for debugging purpose.  */
static const char *const nds32_elfver_strtab[] =
{
  "ELF-1.2",
  "ELF-1.3",
  "ELF-1.4",
};

/* The nds32 linker needs to keep track of the number of relocs that it
   decides to copy in check_relocs for each symbol.  This is so that
   it can discard PC relative relocs if it doesn't need them when
   linking with -Bsymbolic.  We store the information in a field
   extending the regular ELF linker hash table.  */

/* This structure keeps track of the number of PC relative relocs we
   have copied for a given symbol.  */

struct elf_nds32_pcrel_relocs_copied
{
  /* Next section.  */
  struct elf_nds32_pcrel_relocs_copied *next;
  /* A section in dynobj.  */
  asection *section;
  /* Number of relocs copied in this section.  */
  bfd_size_type count;
};

/* The sh linker needs to keep track of the number of relocs that it
   decides to copy as dynamic relocs in check_relocs for each symbol.
   This is so that it can later discard them if they are found to be
   unnecessary.  We store the information in a field extending the
   regular ELF linker hash table.  */

struct elf_nds32_dyn_relocs
{
  struct elf_nds32_dyn_relocs *next;

  /* The input section of the reloc.  */
  asection *sec;

  /* Total number of relocs copied for the input section.  */
  bfd_size_type count;

  /* Number of pc-relative relocs copied for the input section.  */
  bfd_size_type pc_count;
};

/* Nds32 ELF linker hash entry.  */

struct elf_nds32_link_hash_entry
{
  struct elf_link_hash_entry root;

  /* Track dynamic relocs copied for this symbol.  */
  struct elf_nds32_dyn_relocs *dyn_relocs;

  /* For checking relocation type.  */
#define GOT_UNKNOWN     0
#define GOT_NORMAL      1
#define GOT_TLS_IE      2
  unsigned int tls_type;
};

/* Get the nds32 ELF linker hash table from a link_info structure.  */

#define FP_BASE_NAME "_FP_BASE_"
static int check_start_export_sym = 0;
static size_t ex9_relax_size = 0;		/* Save ex9 predicted reducing size.  */

/* The offset for executable tls relaxation.  */
#define TP_OFFSET 0x0

struct elf_nds32_obj_tdata
{
  struct elf_obj_tdata root;

  /* tls_type for each local got entry.  */
  char *local_got_tls_type;
};

#define elf_nds32_tdata(bfd) \
  ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)

#define elf32_nds32_local_got_tls_type(bfd) \
  (elf_nds32_tdata (bfd)->local_got_tls_type)

#define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))

static bfd_boolean
nds32_elf_mkobject (bfd *abfd)
{
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
				  NDS32_ELF_DATA);
}

/* Relocations used for relocation.  */
static reloc_howto_type nds32_elf_howto_table[] =
{
  /* This reloc does nothing.  */
  HOWTO (R_NDS32_NONE,		/* type */
	 0,			/* rightshift */
	 3,			/* size (0 = byte, 1 = short, 2 = long) */
	 0,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_NONE",	/* name */
	 FALSE,			/* partial_inplace */
	 0,			/* src_mask */
	 0,			/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* A 16 bit absolute relocation.  */
  HOWTO (R_NDS32_16,		/* type */
	 0,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 16,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 nds32_elf_generic_reloc,	/* special_function */
	 "R_NDS32_16",		/* name */
	 FALSE,			/* partial_inplace */
	 0xffff,		/* src_mask */
	 0xffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* A 32 bit absolute relocation.  */
  HOWTO (R_NDS32_32,		/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 nds32_elf_generic_reloc,	/* special_function */
	 "R_NDS32_32",		/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* A 20 bit address.  */
  HOWTO (R_NDS32_20,		/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_unsigned,	/* complain_on_overflow */
	 nds32_elf_generic_reloc,	/* special_function */
	 "R_NDS32_20",		/* name */
	 FALSE,			/* partial_inplace */
	 0xfffff,		/* src_mask */
	 0xfffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* An PC Relative 9-bit relocation, shifted by 2.
     This reloc is complicated because relocations are relative to pc & -4.
     i.e. branches in the right insn slot use the address of the left insn
     slot for pc.  */
  /* ??? It's not clear whether this should have partial_inplace set or not.
     Branch relaxing in the assembler can store the addend in the insn,
     and if bfd_install_relocation gets called the addend may get added
     again.  */
  HOWTO (R_NDS32_9_PCREL,	/* type */
	 1,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 8,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 nds32_elf_9_pcrel_reloc,	/* special_function */
	 "R_NDS32_9_PCREL",	/* name */
	 FALSE,			/* partial_inplace */
	 0xff,			/* src_mask */
	 0xff,			/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* A relative 15 bit relocation, right shifted by 1.  */
  HOWTO (R_NDS32_15_PCREL,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 14,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_15_PCREL",	/* name */
	 FALSE,			/* partial_inplace */
	 0x3fff,		/* src_mask */
	 0x3fff,		/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* A relative 17 bit relocation, right shifted by 1.  */
  HOWTO (R_NDS32_17_PCREL,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 16,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_17_PCREL",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffff,		/* src_mask */
	 0xffff,		/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* A relative 25 bit relocation, right shifted by 1.  */
  /* ??? It's not clear whether this should have partial_inplace set or not.
     Branch relaxing in the assembler can store the addend in the insn,
     and if bfd_install_relocation gets called the addend may get added
     again.  */
  HOWTO (R_NDS32_25_PCREL,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 24,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_25_PCREL",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffff,		/* src_mask */
	 0xffffff,		/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* High 20 bits of address when lower 12 is or'd in.  */
  HOWTO (R_NDS32_HI20,		/* type */
	 12,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_hi20_reloc,	/* special_function */
	 "R_NDS32_HI20",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000fffff,		/* src_mask */
	 0x000fffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Lower 12 bits of address.  */
  HOWTO (R_NDS32_LO12S3,	/* type */
	 3,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 9,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_lo12_reloc,	/* special_function */
	 "R_NDS32_LO12S3",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000001ff,		/* src_mask */
	 0x000001ff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Lower 12 bits of address.  */
  HOWTO (R_NDS32_LO12S2,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 10,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_lo12_reloc,	/* special_function */
	 "R_NDS32_LO12S2",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000003ff,		/* src_mask */
	 0x000003ff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Lower 12 bits of address.  */
  HOWTO (R_NDS32_LO12S1,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 11,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_lo12_reloc,	/* special_function */
	 "R_NDS32_LO12S1",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000007ff,		/* src_mask */
	 0x000007ff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Lower 12 bits of address.  */
  HOWTO (R_NDS32_LO12S0,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 12,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_lo12_reloc,	/* special_function */
	 "R_NDS32_LO12S0",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Small data area 15 bits offset.  */
  HOWTO (R_NDS32_SDA15S3,	/* type */
	 3,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 nds32_elf_sda15_reloc,	/* special_function */
	 "R_NDS32_SDA15S3",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Small data area 15 bits offset.  */
  HOWTO (R_NDS32_SDA15S2,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 nds32_elf_sda15_reloc,	/* special_function */
	 "R_NDS32_SDA15S2",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Small data area 15 bits offset.  */
  HOWTO (R_NDS32_SDA15S1,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 nds32_elf_sda15_reloc,	/* special_function */
	 "R_NDS32_SDA15S1",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Small data area 15 bits offset.  */
  HOWTO (R_NDS32_SDA15S0,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 nds32_elf_sda15_reloc,	/* special_function */
	 "R_NDS32_SDA15S0",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* GNU extension to record C++ vtable hierarchy */
  HOWTO (R_NDS32_GNU_VTINHERIT,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 0,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 NULL,			/* special_function */
	 "R_NDS32_GNU_VTINHERIT",	/* name */
	 FALSE,			/* partial_inplace */
	 0,			/* src_mask */
	 0,			/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* GNU extension to record C++ vtable member usage */
  HOWTO (R_NDS32_GNU_VTENTRY,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 0,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 _bfd_elf_rel_vtable_reloc_fn,	/* special_function */
	 "R_NDS32_GNU_VTENTRY",	/* name */
	 FALSE,			/* partial_inplace */
	 0,			/* src_mask */
	 0,			/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* A 16 bit absolute relocation.  */
  HOWTO (R_NDS32_16_RELA,	/* type */
	 0,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 16,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_16_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffff,		/* src_mask */
	 0xffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* A 32 bit absolute relocation.  */
  HOWTO (R_NDS32_32_RELA,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_32_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* A 20 bit address.  */
  HOWTO (R_NDS32_20_RELA,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_20_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xfffff,		/* src_mask */
	 0xfffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  HOWTO (R_NDS32_9_PCREL_RELA,	/* type */
	 1,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 8,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_9_PCREL_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0xff,			/* src_mask */
	 0xff,			/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* A relative 15 bit relocation, right shifted by 1.  */
  HOWTO (R_NDS32_15_PCREL_RELA,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 14,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_15_PCREL_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x3fff,		/* src_mask */
	 0x3fff,		/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* A relative 17 bit relocation, right shifted by 1.  */
  HOWTO (R_NDS32_17_PCREL_RELA,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 16,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_17_PCREL_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffff,		/* src_mask */
	 0xffff,		/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* A relative 25 bit relocation, right shifted by 2.  */
  HOWTO (R_NDS32_25_PCREL_RELA,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 24,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_25_PCREL_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffff,		/* src_mask */
	 0xffffff,		/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* High 20 bits of address when lower 16 is or'd in.  */
  HOWTO (R_NDS32_HI20_RELA,	/* type */
	 12,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_HI20_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000fffff,		/* src_mask */
	 0x000fffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Lower 12 bits of address.  */
  HOWTO (R_NDS32_LO12S3_RELA,	/* type */
	 3,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 9,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_LO12S3_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000001ff,		/* src_mask */
	 0x000001ff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Lower 12 bits of address.  */
  HOWTO (R_NDS32_LO12S2_RELA,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 10,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_LO12S2_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000003ff,		/* src_mask */
	 0x000003ff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Lower 12 bits of address.  */
  HOWTO (R_NDS32_LO12S1_RELA,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 11,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_LO12S1_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000007ff,		/* src_mask */
	 0x000007ff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Lower 12 bits of address.  */
  HOWTO (R_NDS32_LO12S0_RELA,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 12,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_LO12S0_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Small data area 15 bits offset.  */
  HOWTO (R_NDS32_SDA15S3_RELA,	/* type */
	 3,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_SDA15S3_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Small data area 15 bits offset.  */
  HOWTO (R_NDS32_SDA15S2_RELA,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_SDA15S2_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  HOWTO (R_NDS32_SDA15S1_RELA,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_SDA15S1_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  HOWTO (R_NDS32_SDA15S0_RELA,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_SDA15S0_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* GNU extension to record C++ vtable hierarchy */
  HOWTO (R_NDS32_RELA_GNU_VTINHERIT,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 0,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 NULL,			/* special_function */
	 "R_NDS32_RELA_GNU_VTINHERIT",	/* name */
	 FALSE,			/* partial_inplace */
	 0,			/* src_mask */
	 0,			/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* GNU extension to record C++ vtable member usage */
  HOWTO (R_NDS32_RELA_GNU_VTENTRY,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 0,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 _bfd_elf_rel_vtable_reloc_fn,	/* special_function */
	 "R_NDS32_RELA_GNU_VTENTRY",	/* name */
	 FALSE,			/* partial_inplace */
	 0,			/* src_mask */
	 0,			/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Like R_NDS32_20, but referring to the GOT table entry for
     the symbol.  */
  HOWTO (R_NDS32_GOT20,		/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOT20",	/* name */
	 FALSE,			/* partial_inplace */
	 0xfffff,		/* src_mask */
	 0xfffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
     entry for the symbol.  */
  HOWTO (R_NDS32_25_PLTREL,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 24,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_25_PLTREL",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffff,		/* src_mask */
	 0xffffff,		/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* This is used only by the dynamic linker.  The symbol should exist
     both in the object being run and in some shared library.  The
     dynamic linker copies the data addressed by the symbol from the
     shared library into the object, because the object being
     run has to have the data at some particular address.  */
  HOWTO (R_NDS32_COPY,		/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_COPY",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Like R_NDS32_20, but used when setting global offset table
     entries.  */
  HOWTO (R_NDS32_GLOB_DAT,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GLOB_DAT",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Marks a procedure linkage table entry for a symbol.  */
  HOWTO (R_NDS32_JMP_SLOT,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_JMP_SLOT",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Used only by the dynamic linker.  When the object is run, this
     longword is set to the load address of the object, plus the
     addend.  */
  HOWTO (R_NDS32_RELATIVE,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_RELATIVE",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  HOWTO (R_NDS32_GOTOFF,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOTOFF",	/* name */
	 FALSE,			/* partial_inplace */
	 0xfffff,		/* src_mask */
	 0xfffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* An PC Relative 20-bit relocation used when setting PIC offset
     table register.  */
  HOWTO (R_NDS32_GOTPC20,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOTPC20",	/* name */
	 FALSE,			/* partial_inplace */
	 0xfffff,		/* src_mask */
	 0xfffff,		/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* Like R_NDS32_HI20, but referring to the GOT table entry for
     the symbol.  */
  HOWTO (R_NDS32_GOT_HI20,	/* type */
	 12,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOT_HI20",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000fffff,		/* src_mask */
	 0x000fffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_GOT_LO12,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 12,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOT_LO12",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* An PC Relative relocation used when setting PIC offset table register.
     Like R_NDS32_HI20, but referring to the GOT table entry for
     the symbol.  */
  HOWTO (R_NDS32_GOTPC_HI20,	/* type */
	 12,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOTPC_HI20",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000fffff,		/* src_mask */
	 0x000fffff,		/* dst_mask */
	 TRUE),			/* pcrel_offset */
  HOWTO (R_NDS32_GOTPC_LO12,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 12,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOTPC_LO12",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 TRUE),			/* pcrel_offset */

  HOWTO (R_NDS32_GOTOFF_HI20,	/* type */
	 12,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOTOFF_HI20",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000fffff,		/* src_mask */
	 0x000fffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_GOTOFF_LO12,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 12,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOTOFF_LO12",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Alignment hint for relaxable instruction.  This is used with
     R_NDS32_LABEL as a pair.  Relax this instruction from 4 bytes to 2
     in order to make next label aligned on word boundary.  */
  HOWTO (R_NDS32_INSN16,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_INSN16",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Alignment hint for label.  */
  HOWTO (R_NDS32_LABEL,		/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_LABEL",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for unconditional call sequence  */
  HOWTO (R_NDS32_LONGCALL1,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_LONGCALL1",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for conditional call sequence.  */
  HOWTO (R_NDS32_LONGCALL2,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_LONGCALL2",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for conditional call sequence.  */
  HOWTO (R_NDS32_LONGCALL3,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_LONGCALL3",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for unconditional branch sequence.  */
  HOWTO (R_NDS32_LONGJUMP1,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_LONGJUMP1",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for conditional branch sequence.  */
  HOWTO (R_NDS32_LONGJUMP2,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_LONGJUMP2",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for conditional branch sequence.  */
  HOWTO (R_NDS32_LONGJUMP3,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_LONGJUMP3",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for load/store sequence.   */
  HOWTO (R_NDS32_LOADSTORE,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_LOADSTORE",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for load/store sequence.  */
  HOWTO (R_NDS32_9_FIXED_RELA,	/* type */
	 0,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 16,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_9_FIXED_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0x000000ff,		/* src_mask */
	 0x000000ff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for load/store sequence.  */
  HOWTO (R_NDS32_15_FIXED_RELA,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_15_FIXED_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00003fff,		/* src_mask */
	 0x00003fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for load/store sequence.  */
  HOWTO (R_NDS32_17_FIXED_RELA,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_17_FIXED_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x0000ffff,		/* src_mask */
	 0x0000ffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for load/store sequence.  */
  HOWTO (R_NDS32_25_FIXED_RELA,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_25_FIXED_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00ffffff,		/* src_mask */
	 0x00ffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* High 20 bits of PLT symbol offset relative to PC.  */
  HOWTO (R_NDS32_PLTREL_HI20,	/* type */
	 12,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_PLTREL_HI20",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000fffff,		/* src_mask */
	 0x000fffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Low 12 bits of PLT symbol offset relative to PC.  */
  HOWTO (R_NDS32_PLTREL_LO12,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 12,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_PLTREL_LO12",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* High 20 bits of PLT symbol offset relative to GOT (GP).  */
  HOWTO (R_NDS32_PLT_GOTREL_HI20,	/* type */
	 12,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_PLT_GOTREL_HI20",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000fffff,		/* src_mask */
	 0x000fffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Low 12 bits of PLT symbol offset relative to GOT (GP).  */
  HOWTO (R_NDS32_PLT_GOTREL_LO12,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 12,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_PLT_GOTREL_LO12",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Small data area 12 bits offset.  */
  HOWTO (R_NDS32_SDA12S2_DP_RELA,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 12,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_SDA12S2_DP_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Small data area 12 bits offset.  */
  HOWTO (R_NDS32_SDA12S2_SP_RELA,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 12,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_SDA12S2_SP_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  /* Lower 12 bits of address.  */

  HOWTO (R_NDS32_LO12S2_DP_RELA,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 10,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_LO12S2_DP_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000003ff,		/* src_mask */
	 0x000003ff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Lower 12 bits of address.  */
  HOWTO (R_NDS32_LO12S2_SP_RELA,/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 10,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_LO12S2_SP_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000003ff,		/* src_mask */
	 0x000003ff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  /* Lower 12 bits of address.  Special identity for or case.  */
  HOWTO (R_NDS32_LO12S0_ORI_RELA,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 12,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_LO12S0_ORI_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  /* Small data area 19 bits offset.  */
  HOWTO (R_NDS32_SDA16S3_RELA,	/* type */
	 3,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 16,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_SDA16S3_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0x0000ffff,		/* src_mask */
	 0x0000ffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Small data area 15 bits offset.  */
  HOWTO (R_NDS32_SDA17S2_RELA,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 17,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_SDA17S2_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0x0001ffff,		/* src_mask */
	 0x0001ffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  HOWTO (R_NDS32_SDA18S1_RELA,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 18,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_SDA18S1_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0x0003ffff,		/* src_mask */
	 0x0003ffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  HOWTO (R_NDS32_SDA19S0_RELA,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 19,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_SDA19S0_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0x0007ffff,		/* src_mask */
	 0x0007ffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_DWARF2_OP1_RELA,	/* type */
	 0,			/* rightshift */
	 0,			/* size (0 = byte, 1 = short, 2 = long) */
	 8,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_DWARF2_OP1_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xff,			/* src_mask */
	 0xff,			/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_DWARF2_OP2_RELA,	/* type */
	 0,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 16,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_DWARF2_OP2_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffff,		/* src_mask */
	 0xffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_DWARF2_LEB_RELA,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_DWARF2_LEB_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_UPDATE_TA_RELA,/* type */
	 0,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 16,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_UPDATE_TA_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffff,		/* src_mask */
	 0xffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
     entry for the symbol.  */
  HOWTO (R_NDS32_9_PLTREL,	/* type */
	 1,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 8,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_9_PLTREL",	/* name */
	 FALSE,			/* partial_inplace */
	 0xff,			/* src_mask */
	 0xff,			/* dst_mask */
	 TRUE),			/* pcrel_offset */
  /* Low 20 bits of PLT symbol offset relative to GOT (GP).  */
  HOWTO (R_NDS32_PLT_GOTREL_LO20,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_PLT_GOTREL_LO20",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000fffff,		/* src_mask */
	 0x000fffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  /* low 15 bits of PLT symbol offset relative to GOT (GP) */
  HOWTO (R_NDS32_PLT_GOTREL_LO15,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_PLT_GOTREL_LO15",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  /* Low 19 bits of PLT symbol offset relative to GOT (GP).  */
  HOWTO (R_NDS32_PLT_GOTREL_LO19,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 19,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_PLT_GOTREL_LO19",	/* name */
	 FALSE,			/* partial_inplace */
	 0x0007ffff,		/* src_mask */
	 0x0007ffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_GOT_LO15,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOT_LO15",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_GOT_LO19,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 19,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOT_LO19",	/* name */
	 FALSE,			/* partial_inplace */
	 0x0007ffff,		/* src_mask */
	 0x0007ffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_GOTOFF_LO15,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOTOFF_LO15",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_GOTOFF_LO19,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 19,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOTOFF_LO19",	/* name */
	 FALSE,			/* partial_inplace */
	 0x0007ffff,		/* src_mask */
	 0x0007ffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  /* GOT 15 bits offset.  */
  HOWTO (R_NDS32_GOT15S2_RELA,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOT15S2_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0x00007fff,		/* src_mask */
	 0x00007fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  /* GOT 17 bits offset.  */
  HOWTO (R_NDS32_GOT17S2_RELA,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 17,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_GOT17S2_RELA",/* name */
	 FALSE,			/* partial_inplace */
	 0x0001ffff,		/* src_mask */
	 0x0001ffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  /* A 5 bit address.  */
  HOWTO (R_NDS32_5_RELA,	/* type */
	 0,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 5,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_5_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x1f,			/* src_mask */
	 0x1f,			/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_10_UPCREL_RELA,/* type */
	 1,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 9,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_unsigned,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_10_UPCREL_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x1ff,			/* src_mask */
	 0x1ff,			/* dst_mask */
	 TRUE),			/* pcrel_offset */
  HOWTO (R_NDS32_SDA_FP7U2_RELA,/* type */
	 2,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 7,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_unsigned,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_SDA_FP7U2_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x0000007f,		/* src_mask */
	 0x0000007f,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_WORD_9_PCREL_RELA,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 8,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_WORD_9_PCREL_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xff,			/* src_mask */
	 0xff,			/* dst_mask */
	 TRUE),			/* pcrel_offset */
  HOWTO (R_NDS32_25_ABS_RELA,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 24,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_25_ABS_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffff,		/* src_mask */
	 0xffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* A relative 17 bit relocation for ifc, right shifted by 1.  */
  HOWTO (R_NDS32_17IFC_PCREL_RELA,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 16,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_17IFC_PCREL_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffff,		/* src_mask */
	 0xffff,		/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* A relative unsigned 10 bit relocation for ifc, right shifted by 1.  */
  HOWTO (R_NDS32_10IFCU_PCREL_RELA,	/* type */
	 1,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 9,			/* bitsize */
	 TRUE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_unsigned,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_10IFCU_PCREL_RELA",	/* name */
	 FALSE,			/* partial_inplace */
	 0x1ff,			/* src_mask */
	 0x1ff,			/* dst_mask */
	 TRUE),			/* pcrel_offset */

  /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol.  */
  HOWTO (R_NDS32_TLS_LE_HI20,	/* type */
	 12,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_TLS_LE_HI20",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000fffff,		/* src_mask */
	 0x000fffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_TLS_LE_LO12,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 12,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_TLS_LE_LO12",	/* name */
	 FALSE,			/* partial_inplace */
	 0x00000fff,		/* src_mask */
	 0x00000fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol.  */
  HOWTO (R_NDS32_TLS_IE_HI20,	/* type */
	 12,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_TLS_IE_HI20",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000fffff,		/* src_mask */
	 0x000fffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_TLS_IE_LO12S2,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 10,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_TLS_IE_LO12S2",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000003ff,		/* src_mask */
	 0x000003ff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  /* Mark a TLS IE entry in GOT.  */
  HOWTO (R_NDS32_TLS_TPOFF,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_TLS_TPOFF",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  /* A 20 bit address.  */
  HOWTO (R_NDS32_TLS_LE_20,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 20,			/* bitsize */
	 FALSE,		/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_TLS_LE_20",	/* name */
	 FALSE,		/* partial_inplace */
	 0xfffff,		/* src_mask */
	 0xfffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_TLS_LE_15S0,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,		/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_TLS_LE_15S0",	/* name */
	 FALSE,		/* partial_inplace */
	 0x7fff,		/* src_mask */
	 0x7fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_TLS_LE_15S1,	/* type */
	 1,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,		/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_TLS_LE_15S1",	/* name */
	 FALSE,		/* partial_inplace */
	 0x7fff,		/* src_mask */
	 0x7fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_TLS_LE_15S2,	/* type */
	 2,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 15,			/* bitsize */
	 FALSE,		/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,	/* special_function */
	 "R_NDS32_TLS_LE_15S2",	/* name */
	 FALSE,		/* partial_inplace */
	 0x7fff,		/* src_mask */
	 0x7fff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for unconditional call sequence  */
  HOWTO (R_NDS32_LONGCALL4,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 nds32_elf_ignore_reloc,	/* special_function */
	 "R_NDS32_LONGCALL4",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for conditional call sequence.  */
  HOWTO (R_NDS32_LONGCALL5,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 nds32_elf_ignore_reloc,	/* special_function */
	 "R_NDS32_LONGCALL5",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for conditional call sequence.  */
  HOWTO (R_NDS32_LONGCALL6,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 nds32_elf_ignore_reloc,	/* special_function */
	 "R_NDS32_LONGCALL6",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for unconditional branch sequence.  */
  HOWTO (R_NDS32_LONGJUMP4,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 nds32_elf_ignore_reloc,	/* special_function */
	 "R_NDS32_LONGJUMP4",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for conditional branch sequence.  */
  HOWTO (R_NDS32_LONGJUMP5,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 nds32_elf_ignore_reloc,	/* special_function */
	 "R_NDS32_LONGJUMP5",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for conditional branch sequence.  */
  HOWTO (R_NDS32_LONGJUMP6,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 nds32_elf_ignore_reloc,	/* special_function */
	 "R_NDS32_LONGJUMP6",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */

  /* Relax hint for conditional branch sequence.  */
  HOWTO (R_NDS32_LONGJUMP7,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 nds32_elf_ignore_reloc,	/* special_function */
	 "R_NDS32_LONGJUMP7",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
};

/* Relocations used for relaxation.  */
static reloc_howto_type nds32_elf_relax_howto_table[] =
{
  HOWTO (R_NDS32_RELAX_ENTRY,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_RELAX_ENTRY",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_GOT_SUFF,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_GOT_SUFF",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_GOTOFF_SUFF,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_GOTOFF_SUFF",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_PLT_GOT_SUFF,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_PLT_GOT_SUFF",/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_MULCALL_SUFF,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_MULCALL_SUFF",/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_PTR,		/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_PTR",		/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_PTR_COUNT,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_PTR_COUNT",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_PTR_RESOLVED,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_PTR_RESOLVED",/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_PLTBLOCK,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_PLTBLOCK",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_RELAX_REGION_BEGIN,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_RELAX_REGION_BEGIN",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_RELAX_REGION_END,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_RELAX_REGION_END",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_MINUEND,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_MINUEND",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_SUBTRAHEND,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_SUBTRAHEND",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_DIFF8,		/* type */
	 0,			/* rightshift */
	 0,			/* size (0 = byte, 1 = short, 2 = long) */
	 8,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_DIFF8",	/* name */
	 FALSE,			/* partial_inplace */
	 0x000000ff,		/* src_mask */
	 0x000000ff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_DIFF16,	/* type */
	 0,			/* rightshift */
	 1,			/* size (0 = byte, 1 = short, 2 = long) */
	 16,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_DIFF16",	/* name */
	 FALSE,			/* partial_inplace */
	 0x0000ffff,		/* src_mask */
	 0x0000ffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_DIFF32,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_DIFF32",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_DIFF_ULEB128,	/* type */
	 0,			/* rightshift */
	 0,			/* size (0 = byte, 1 = short, 2 = long) */
	 0,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_DIFF_ULEB128",/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_DATA,		/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_DATA",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_TRAN,		/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,/* complain_on_overflow */
	 nds32_elf_ignore_reloc,/* special_function */
	 "R_NDS32_TRAN",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_TLS_LE_ADD,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 nds32_elf_ignore_reloc,	/* special_function */
	 "R_NDS32_TLS_LE_ADD",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_TLS_LE_LS,	/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 nds32_elf_ignore_reloc,	/* special_function */
	 "R_NDS32_TLS_LE_LS",	/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
  HOWTO (R_NDS32_EMPTY,		/* type */
	 0,			/* rightshift */
	 2,			/* size (0 = byte, 1 = short, 2 = long) */
	 32,			/* bitsize */
	 FALSE,			/* pc_relative */
	 0,			/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 nds32_elf_ignore_reloc,	/* special_function */
	 "R_NDS32_EMPTY",		/* name */
	 FALSE,			/* partial_inplace */
	 0xffffffff,		/* src_mask */
	 0xffffffff,		/* dst_mask */
	 FALSE),		/* pcrel_offset */
};


/* nds32_insertion_sort sorts an array with nmemb elements of size size.
   This prototype is the same as qsort ().  */

void
nds32_insertion_sort (void *base, size_t nmemb, size_t size,
		      int (*compar) (const void *lhs, const void *rhs))
{
  char *ptr = (char *) base;
  int i, j;
  char *tmp = alloca (size);

  /* If i is less than j, i is inserted before j.

     |---- j ----- i --------------|
      \		 / \		  /
	 sorted		unsorted
   */

  for (i = 1; i < (int) nmemb; i++)
    {
      for (j = (i - 1); j >= 0; j--)
	if (compar (ptr + i * size, ptr + j * size) >= 0)
	  break;

      j++;

      if (i == j)
	continue; /* i is in order.  */

      memcpy (tmp, ptr + i * size, size);
      memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
      memcpy (ptr + j * size, tmp, size);
    }
}

/* Sort relocation by r_offset.

   We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
   algorithm.  Relocations at the same r_offset must keep their order.
   For example, RELAX_ENTRY must be the very first relocation entry.

   Currently, this function implements insertion-sort.

   FIXME: If we already sort them in assembler, why bother sort them
	  here again?  */

static int
compar_reloc (const void *lhs, const void *rhs)
{
  const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
  const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;

  if (l->r_offset > r->r_offset)
    return 1;
  else if (l->r_offset == r->r_offset)
    return 0;
  else
    return -1;
}

/* Functions listed below are only used for old relocs.
   * nds32_elf_9_pcrel_reloc
   * nds32_elf_do_9_pcrel_reloc
   * nds32_elf_hi20_reloc
   * nds32_elf_relocate_hi20
   * nds32_elf_lo12_reloc
   * nds32_elf_sda15_reloc
   * nds32_elf_generic_reloc
   */

/* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc.  */

static bfd_reloc_status_type
nds32_elf_9_pcrel_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
			 void *data, asection *input_section, bfd *output_bfd,
			 char **error_message ATTRIBUTE_UNUSED)
{
  /* This part is from bfd_elf_generic_reloc.  */
  if (output_bfd != (bfd *) NULL
      && (symbol->flags & BSF_SECTION_SYM) == 0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
    {
      reloc_entry->address += input_section->output_offset;
      return bfd_reloc_ok;
    }

  if (output_bfd != NULL)
    {
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
      return bfd_reloc_continue;
    }

  return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
				     input_section,
				     data, reloc_entry->address,
				     symbol->section,
				     (symbol->value
				      + symbol->section->output_section->vma
				      + symbol->section->output_offset),
				     reloc_entry->addend);
}

/* Utility to actually perform an R_NDS32_9_PCREL reloc.  */
#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)

static bfd_reloc_status_type
nds32_elf_do_9_pcrel_reloc (bfd *abfd, reloc_howto_type *howto,
			    asection *input_section, bfd_byte *data,
			    bfd_vma offset,
			    asection *symbol_section ATTRIBUTE_UNUSED,
			    bfd_vma symbol_value, bfd_vma addend)
{
  bfd_signed_vma relocation;
  unsigned short x;
  bfd_reloc_status_type status;

  /* Sanity check the address (offset in section).  */
  if (offset > bfd_get_section_limit (abfd, input_section))
    return bfd_reloc_outofrange;

  relocation = symbol_value + addend;
  /* Make it pc relative.  */
  relocation -= (input_section->output_section->vma
		 + input_section->output_offset);
  /* These jumps mask off the lower two bits of the current address
     before doing pcrel calculations.  */
  relocation -= (offset & -(bfd_vma) 2);

  if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
    status = bfd_reloc_overflow;
  else
    status = bfd_reloc_ok;

  x = bfd_getb16 (data + offset);

  relocation >>= howto->rightshift;
  relocation <<= howto->bitpos;
  x = (x & ~howto->dst_mask)
      | (((x & howto->src_mask) + relocation) & howto->dst_mask);

  bfd_putb16 ((bfd_vma) x, data + offset);

  return status;
}

/* Handle the R_NDS32_HI20_[SU]LO relocs.
   HI20_SLO is for the add3 and load/store with displacement instructions.
   HI20 is for the or3 instruction.
   For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
   the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
   we must add one to the high 16 bytes (which will get subtracted off when
   the low 16 bits are added).
   These relocs have to be done in combination with an R_NDS32_LO12 reloc
   because there is a carry from the LO12 to the HI20.  Here we just save
   the information we need; we do the actual relocation when we see the LO12.
   This code is copied from the elf32-mips.c.  We also support an arbitrary
   number of HI20 relocs to be associated with a single LO12 reloc.  The
   assembler sorts the relocs to ensure each HI20 immediately precedes its
   LO12.  However if there are multiple copies, the assembler may not find
   the real LO12 so it picks the first one it finds.  */

struct nds32_hi20
{
  struct nds32_hi20 *next;
  bfd_byte *addr;
  bfd_vma addend;
};

static struct nds32_hi20 *nds32_hi20_list;

static bfd_reloc_status_type
nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
		      asymbol *symbol, void *data, asection *input_section,
		      bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
{
  bfd_reloc_status_type ret;
  bfd_vma relocation;
  struct nds32_hi20 *n;

  /* This part is from bfd_elf_generic_reloc.
     If we're relocating, and this an external symbol, we don't want
     to change anything.  */
  if (output_bfd != (bfd *) NULL
      && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
    {
      reloc_entry->address += input_section->output_offset;
      return bfd_reloc_ok;
    }

  /* Sanity check the address (offset in section).  */
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
    return bfd_reloc_outofrange;

  ret = bfd_reloc_ok;
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
    ret = bfd_reloc_undefined;

  if (bfd_is_com_section (symbol->section))
    relocation = 0;
  else
    relocation = symbol->value;

  relocation += symbol->section->output_section->vma;
  relocation += symbol->section->output_offset;
  relocation += reloc_entry->addend;

  /* Save the information, and let LO12 do the actual relocation.  */
  n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
  if (n == NULL)
    return bfd_reloc_outofrange;

  n->addr = (bfd_byte *) data + reloc_entry->address;
  n->addend = relocation;
  n->next = nds32_hi20_list;
  nds32_hi20_list = n;

  if (output_bfd != (bfd *) NULL)
    reloc_entry->address += input_section->output_offset;

  return ret;
}

/* Handle an NDS32 ELF HI20 reloc.  */

static void
nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
			 int type ATTRIBUTE_UNUSED, Elf_Internal_Rela *relhi,
			 Elf_Internal_Rela *rello, bfd_byte *contents,
			 bfd_vma addend)
{
  unsigned long insn;
  bfd_vma addlo;

  insn = bfd_getb32 (contents + relhi->r_offset);

  addlo = bfd_getb32 (contents + rello->r_offset);
  addlo &= 0xfff;

  addend += ((insn & 0xfffff) << 20) + addlo;

  insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
  bfd_putb32 (insn, contents + relhi->r_offset);
}

/* Do an R_NDS32_LO12 relocation.  This is a straightforward 12 bit
   inplace relocation; this function exists in order to do the
   R_NDS32_HI20_[SU]LO relocation described above.  */

static bfd_reloc_status_type
nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
		      void *data, asection *input_section, bfd *output_bfd,
		      char **error_message)
{
  /* This part is from bfd_elf_generic_reloc.
     If we're relocating, and this an external symbol, we don't want
     to change anything.  */
  if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
      && reloc_entry->addend == 0)
    {
      reloc_entry->address += input_section->output_offset;
      return bfd_reloc_ok;
    }

  if (nds32_hi20_list != NULL)
    {
      struct nds32_hi20 *l;

      l = nds32_hi20_list;
      while (l != NULL)
	{
	  unsigned long insn;
	  unsigned long val;
	  unsigned long vallo;
	  struct nds32_hi20 *next;

	  /* Do the HI20 relocation.  Note that we actually don't need
	     to know anything about the LO12 itself, except where to
	     find the low 12 bits of the addend needed by the LO12.  */
	  insn = bfd_getb32 (l->addr);
	  vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
	  vallo &= 0xfff;
	  switch (reloc_entry->howto->type)
	    {
	    case R_NDS32_LO12S3:
	      vallo <<= 3;
	      break;

	    case R_NDS32_LO12S2:
	      vallo <<= 2;
	      break;

	    case R_NDS32_LO12S1:
	      vallo <<= 1;
	      break;

	    case R_NDS32_LO12S0:
	      vallo <<= 0;
	      break;
	    }

	  val = ((insn & 0xfffff) << 12) + vallo;
	  val += l->addend;

	  insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
	  bfd_putb32 ((bfd_vma) insn, l->addr);

	  next = l->next;
	  free (l);
	  l = next;
	}

      nds32_hi20_list = NULL;
    }

  /* Now do the LO12 reloc in the usual way.
     ??? It would be nice to call bfd_elf_generic_reloc here,
     but we have partial_inplace set.  bfd_elf_generic_reloc will
     pass the handling back to bfd_install_relocation which will install
     a section relative addend which is wrong.  */
  return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
				  input_section, output_bfd, error_message);
}

/* Do generic partial_inplace relocation.
   This is a local replacement for bfd_elf_generic_reloc.  */

static bfd_reloc_status_type
nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
			 asymbol *symbol, void *data, asection *input_section,
			 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
{
  bfd_reloc_status_type ret;
  bfd_vma relocation;
  bfd_byte *inplace_address;

  /* This part is from bfd_elf_generic_reloc.
     If we're relocating, and this an external symbol, we don't want
     to change anything.  */
  if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
      && reloc_entry->addend == 0)
    {
      reloc_entry->address += input_section->output_offset;
      return bfd_reloc_ok;
    }

  /* Now do the reloc in the usual way.
     ??? It would be nice to call bfd_elf_generic_reloc here,
     but we have partial_inplace set.  bfd_elf_generic_reloc will
     pass the handling back to bfd_install_relocation which will install
     a section relative addend which is wrong.  */

  /* Sanity check the address (offset in section).  */
  if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
    return bfd_reloc_outofrange;

  ret = bfd_reloc_ok;
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
    ret = bfd_reloc_undefined;

  if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
    relocation = 0;
  else
    relocation = symbol->value;

  /* Only do this for a final link.  */
  if (output_bfd == (bfd *) NULL)
    {
      relocation += symbol->section->output_section->vma;
      relocation += symbol->section->output_offset;
    }

  relocation += reloc_entry->addend;
  switch (reloc_entry->howto->type)
    {
    case R_NDS32_LO12S3:
      relocation >>= 3;
      break;

    case R_NDS32_LO12S2:
      relocation >>= 2;
      break;

    case R_NDS32_LO12S1:
      relocation >>= 1;
      break;

    case R_NDS32_LO12S0:
    default:
      relocation >>= 0;
      break;
    }

  inplace_address = (bfd_byte *) data + reloc_entry->address;

#define DOIT(x)						\
  x = ((x & ~reloc_entry->howto->dst_mask) |		\
  (((x & reloc_entry->howto->src_mask) +  relocation) &	\
  reloc_entry->howto->dst_mask))

  switch (reloc_entry->howto->size)
    {
    case 1:
      {
	short x = bfd_getb16 (inplace_address);

	DOIT (x);
	bfd_putb16 ((bfd_vma) x, inplace_address);
      }
      break;
    case 2:
      {
	unsigned long x = bfd_getb32 (inplace_address);

	DOIT (x);
	bfd_putb32 ((bfd_vma) x, inplace_address);
      }
      break;
    default:
      BFD_ASSERT (0);
    }

  if (output_bfd != (bfd *) NULL)
    reloc_entry->address += input_section->output_offset;

  return ret;
}

/* Handle the R_NDS32_SDA15 reloc.
   This reloc is used to compute the address of objects in the small data area
   and to perform loads and stores from that area.
   The lower 15 bits are sign extended and added to the register specified
   in the instruction, which is assumed to point to _SDA_BASE_.

   Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
   the access size, this must be taken care of.  */

static bfd_reloc_status_type
nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
		       asymbol *symbol, void *data ATTRIBUTE_UNUSED,
		       asection *input_section, bfd *output_bfd,
		       char **error_message ATTRIBUTE_UNUSED)
{
  /* This part is from bfd_elf_generic_reloc.  */
  if (output_bfd != (bfd *) NULL
      && (symbol->flags & BSF_SECTION_SYM) == 0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
    {
      reloc_entry->address += input_section->output_offset;
      return bfd_reloc_ok;
    }

  if (output_bfd != NULL)
    {
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
      return bfd_reloc_continue;
    }

  /* FIXME: not sure what to do here yet.  But then again, the linker
     may never call us.  */
  abort ();
}

/* nds32_elf_ignore_reloc is the special function for
   relocation types which don't need to be relocated
   like relaxation relocation types.
   This function simply return bfd_reloc_ok when it is
   invoked.  */

static bfd_reloc_status_type
nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
			asymbol *symbol ATTRIBUTE_UNUSED,
			void *data ATTRIBUTE_UNUSED, asection *input_section,
			bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
{
  if (output_bfd != NULL)
    reloc_entry->address += input_section->output_offset;

  return bfd_reloc_ok;
}


/* Map BFD reloc types to NDS32 ELF reloc types.  */

struct nds32_reloc_map_entry
{
  bfd_reloc_code_real_type bfd_reloc_val;
  unsigned char elf_reloc_val;
};

static const struct nds32_reloc_map_entry nds32_reloc_map[] =
{
  {BFD_RELOC_NONE, R_NDS32_NONE},
  {BFD_RELOC_16, R_NDS32_16_RELA},
  {BFD_RELOC_32, R_NDS32_32_RELA},
  {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
  {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
  {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
  {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
  {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
  {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
  {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
  {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
  {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
  {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
  {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
  {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
  {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
  {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
  {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
  {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
  {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
  {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
  {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
  {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},

  {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
  {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
  {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
  {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
  {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
  {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
  {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
  {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
  {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
  {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
  {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
  {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
  {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
  {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
  {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
  {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
  {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
  {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
  {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
  {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
  {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
  {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
  {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
  {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
  {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
  {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
  {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
  {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
  {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
  {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
  {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
  {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
  {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
  {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
  {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
  {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
  {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
  {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
  {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
  {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
  {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
  {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
  {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
  {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
  {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
  {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
  {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
  {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
  {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
  {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
  {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
  {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
  {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
  {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
  {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
  {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
  {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
  {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
  {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
  {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
  {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
  {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
  {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
  {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
  {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
  {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
  {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
  {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
  {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
  {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
  {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
  {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
  {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
  {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
  {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},

  {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
  {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
  {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
  {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
  {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
  {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
  {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
  {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
  {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
  {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
  {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
  {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
  {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
  {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
  {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
  {BFD_RELOC_NDS32_TLS_TPOFF, R_NDS32_TLS_TPOFF},
  {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
  {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
  {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
  {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
};

/* Patch tag.  */

static reloc_howto_type *
bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
				 const char *r_name)
{
  unsigned int i;

  for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
    if (nds32_elf_howto_table[i].name != NULL
	&& strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
      return &nds32_elf_howto_table[i];

  for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
    if (nds32_elf_relax_howto_table[i].name != NULL
	&& strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
      return &nds32_elf_relax_howto_table[i];

  return NULL;
}

static reloc_howto_type *
bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
{
  if (code < R_NDS32_RELAX_ENTRY)
    {
      BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
      return &nds32_elf_howto_table[code];
    }
  else
    {
      BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
		  < ARRAY_SIZE (nds32_elf_relax_howto_table));
      return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
    }
}

static reloc_howto_type *
bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
				 bfd_reloc_code_real_type code)
{
  unsigned int i;

  for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
    {
      if (nds32_reloc_map[i].bfd_reloc_val == code)
	return bfd_elf32_bfd_reloc_type_table_lookup
		 (nds32_reloc_map[i].elf_reloc_val);
    }

  return NULL;
}

/* Set the howto pointer for an NDS32 ELF reloc.  */

static void
nds32_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
			 Elf_Internal_Rela *dst)
{
  enum elf_nds32_reloc_type r_type;

  r_type = ELF32_R_TYPE (dst->r_info);
  if (r_type > R_NDS32_GNU_VTENTRY)
    {
      _bfd_error_handler (_("%B: invalid NDS32 reloc number: %d"), abfd, r_type);
      r_type = 0;
    }
  cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
}

static void
nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
		     Elf_Internal_Rela *dst)
{
  BFD_ASSERT ((ELF32_R_TYPE (dst->r_info) == R_NDS32_NONE)
	      || ((ELF32_R_TYPE (dst->r_info) > R_NDS32_GNU_VTENTRY)
		  && (ELF32_R_TYPE (dst->r_info) < R_NDS32_max)));
  cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (dst->r_info));
}

/* Support for core dump NOTE sections.
   Reference to include/linux/elfcore.h in Linux.  */

static bfd_boolean
nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
{
  int offset;
  size_t size;

  switch (note->descsz)
    {
    case 0x114:
      /* Linux/NDS32 32-bit, ABI1 */

      /* pr_cursig */
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);

      /* pr_pid */
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);

      /* pr_reg */
      offset = 72;
      size = 200;
      break;

    case 0xfc:
      /* Linux/NDS32 32-bit */

      /* pr_cursig */
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);

      /* pr_pid */
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);

      /* pr_reg */
      offset = 72;
      size = 176;
      break;

    default:
      return FALSE;
    }

  /* Make a ".reg" section.  */
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
					  size, note->descpos + offset);
}

static bfd_boolean
nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
{
  switch (note->descsz)
    {
    case 124:
      /* Linux/NDS32 */

      /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform.  */
      elf_tdata (abfd)->core->program =
	_bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
      elf_tdata (abfd)->core->command =
	_bfd_elfcore_strndup (abfd, note->descdata + 44, 80);

    default:
      return FALSE;
    }

  /* Note that for some reason, a spurious space is tacked
     onto the end of the args in some (at least one anyway)
     implementations, so strip it off if it exists.  */
  {
    char *command = elf_tdata (abfd)->core->command;
    int n = strlen (command);

    if (0 < n && command[n - 1] == ' ')
      command[n - 1] = '\0';
  }

  return TRUE;
}

/* Hook called by the linker routine which adds symbols from an object
   file.  We must handle the special NDS32 section numbers here.
   We also keep watching for whether we need to create the sdata special
   linker sections.  */

static bfd_boolean
nds32_elf_add_symbol_hook (bfd *abfd,
			   struct bfd_link_info *info ATTRIBUTE_UNUSED,
			   Elf_Internal_Sym *sym,
			   const char **namep ATTRIBUTE_UNUSED,
			   flagword *flagsp ATTRIBUTE_UNUSED,
			   asection **secp, bfd_vma *valp)
{
  switch (sym->st_shndx)
    {
    case SHN_COMMON:
      /* Common symbols less than the GP size are automatically
	 treated as SHN_MIPS_SCOMMON symbols.  */
      if (sym->st_size > elf_gp_size (abfd)
	  || ELF_ST_TYPE (sym->st_info) == STT_TLS)
	break;

      /* st_value is the alignemnt constraint.
	 That might be its actual size if it is an array or structure.  */
      switch (sym->st_value)
	{
	case 1:
	  *secp = bfd_make_section_old_way (abfd, ".scommon_b");
	  break;
	case 2:
	  *secp = bfd_make_section_old_way (abfd, ".scommon_h");
	  break;
	case 4:
	  *secp = bfd_make_section_old_way (abfd, ".scommon_w");
	  break;
	case 8:
	  *secp = bfd_make_section_old_way (abfd, ".scommon_d");
	  break;
	default:
	  return TRUE;
	}

      (*secp)->flags |= SEC_IS_COMMON;
      *valp = sym->st_size;
      break;
    }

  return TRUE;
}


/* This function can figure out the best location for a base register to access
   data relative to this base register
   INPUT:
   sda_d0: size of first DOUBLE WORD data section
   sda_w0: size of first WORD data section
   sda_h0: size of first HALF WORD data section
   sda_b : size of BYTE data section
   sda_hi: size of second HALF WORD data section
   sda_w1: size of second WORD data section
   sda_d1: size of second DOUBLE WORD data section
   OUTPUT:
   offset (always positive) from the beginning of sda_d0 if OK
   a negative error value if fail
   NOTE:
   these 7 sections have to be located back to back if exist
   a pass in 0 value for non-existing section   */

/* Due to the interpretation of simm15 field of load/store depending on
   data accessing size, the organization of base register relative data shall
   like the following figure
   -------------------------------------------
   |  DOUBLE WORD sized data (range +/- 128K)
   -------------------------------------------
   |  WORD sized data (range +/- 64K)
   -------------------------------------------
   |  HALF WORD sized data (range +/- 32K)
   -------------------------------------------
   |  BYTE sized data (range +/- 16K)
   -------------------------------------------
   |  HALF WORD sized data (range +/- 32K)
   -------------------------------------------
   |  WORD sized data (range +/- 64K)
   -------------------------------------------
   |  DOUBLE WORD sized data (range +/- 128K)
   -------------------------------------------
   Its base register shall be set to access these data freely.  */

/* We have to figure out the SDA_BASE value, so that we can adjust the
   symbol value correctly.  We look up the symbol _SDA_BASE_ in the output
   BFD.  If we can't find it, we're stuck.  We cache it in the ELF
   target data.  We don't need to adjust the symbol value for an
   external symbol if we are producing relocatable output.  */

static asection *sda_rela_sec = NULL;

#define SDA_SECTION_NUM 10

static bfd_reloc_status_type
nds32_elf_final_sda_base (bfd *output_bfd, struct bfd_link_info *info,
			  bfd_vma *psb, bfd_boolean add_symbol)
{
  int relax_fp_as_gp;
  struct elf_nds32_link_hash_table *table;
  struct bfd_link_hash_entry *h, *h2;
  long unsigned int total = 0;

  h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
  if (!h || (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak))
    {
      asection *first = NULL, *final = NULL, *temp;
      bfd_vma sda_base;
      /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
	 4 byte-aligned.  Therefore, it has to set the first section ".data"
	 4 byte-aligned.  */
      static const char sec_name[SDA_SECTION_NUM][10] =
	{
	  ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
	  ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
	};
      size_t i = 0;

      if (output_bfd->sections == NULL)
	{
	  *psb = elf_gp (output_bfd);
	  return bfd_reloc_ok;
	}

      /* Get the first and final section.  */
      while (i < sizeof (sec_name) / sizeof (sec_name [0]))
	{
	  temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
	  if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
	    first = temp;
	  if (temp && (temp->size != 0 || temp->rawsize != 0))
	    final = temp;

	  /* Summarize the sections in order to check if joining .bss.  */
	  if (temp && temp->size != 0)
	    total += temp->size;
	  else if (temp && temp->rawsize != 0)
	    total += temp->rawsize;

	  i++;
	}

      /* Check .bss size.  */
      temp = bfd_get_section_by_name (output_bfd, ".bss");
      if (temp)
	{
	  if (temp->size != 0)
	    total += temp->size;
	  else if (temp->rawsize != 0)
	    total += temp->rawsize;

	  if (total < 0x80000)
	    {
	      if (!first && (temp->size != 0 || temp->rawsize != 0))
		first = temp;
	      if ((temp->size != 0 || temp->rawsize != 0))
		final = temp;
	    }
	}

      if (first && final)
	{
	  /* The middle of data region.  */
	  sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;

	  /* Find the section sda_base located.  */
	  i = 0;
	  while (i < sizeof (sec_name) / sizeof (sec_name [0]))
	    {
	      final = bfd_get_section_by_name (output_bfd, sec_name[i]);
	      if (final && (final->size != 0 || final->rawsize != 0)
		  && sda_base >= final->vma)
		{
		  first = final;
		  i++;
		}
	      else
		break;
	    }
	}
      else
	{
	  /* There is not any data section in output bfd, and set _SDA_BASE_ in
	     first output section.  */
	  first = output_bfd->sections;
	  while (first && first->size == 0 && first->rawsize == 0)
	    first = first->next;
	  if (!first)
	    {
	      *psb = elf_gp (output_bfd);
	      return bfd_reloc_ok;
	    }
	  sda_base = first->vma + first->rawsize;
	}

      sda_base -= first->vma;
      sda_base = sda_base & (~7);

      if (!_bfd_generic_link_add_one_symbol
	     (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
	      (bfd_vma) sda_base, (const char *) NULL, FALSE,
	      get_elf_backend_data (output_bfd)->collect, &h))
	return FALSE;

      sda_rela_sec = first;

      table = nds32_elf_hash_table (info);
      relax_fp_as_gp = table->relax_fp_as_gp;
      if (relax_fp_as_gp)
	{
	  h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME,
				     FALSE, FALSE, FALSE);
	  /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
	     And set FP equal to SDA_BASE to do relaxation for
	     la $fp, _FP_BASE_.  */
	  if (!_bfd_generic_link_add_one_symbol
		 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
		  first, (bfd_vma) sda_base, (const char *) NULL,
		  FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
	    return FALSE;
	}
    }

  if (add_symbol == TRUE)
    {
      if (h)
	{
	  /* Now set gp.  */
	  elf_gp (output_bfd) = (h->u.def.value
				 + h->u.def.section->output_section->vma
				 + h->u.def.section->output_offset);
	}
      else
	{
	  (*_bfd_error_handler) (_("error: Can't find symbol: _SDA_BASE_."));
	  return bfd_reloc_dangerous;
	}
    }

  *psb = h->u.def.value + h->u.def.section->output_section->vma
	 + h->u.def.section->output_offset;
  return bfd_reloc_ok;
}


/* Return size of a PLT entry.  */
#define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE


/* Create an entry in an nds32 ELF linker hash table.  */

static struct bfd_hash_entry *
nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
			     struct bfd_hash_table *table,
			     const char *string)
{
  struct elf_nds32_link_hash_entry *ret;

  ret = (struct elf_nds32_link_hash_entry *) entry;

  /* Allocate the structure if it has not already been allocated by a
     subclass.  */
  if (ret == NULL)
    ret = (struct elf_nds32_link_hash_entry *)
       bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));

  if (ret == NULL)
    return (struct bfd_hash_entry *) ret;

  /* Call the allocation method of the superclass.  */
  ret = (struct elf_nds32_link_hash_entry *)
    _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);

  if (ret != NULL)
    {
      struct elf_nds32_link_hash_entry *eh;

      eh = (struct elf_nds32_link_hash_entry *) ret;
      eh->dyn_relocs = NULL;
      eh->tls_type = GOT_UNKNOWN;
    }

  return (struct bfd_hash_entry *) ret;
}

/* Create an nds32 ELF linker hash table.  */

static struct bfd_link_hash_table *
nds32_elf_link_hash_table_create (bfd *abfd)
{
  struct elf_nds32_link_hash_table *ret;

  bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);

  ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
  if (ret == NULL)
    return NULL;

  /* patch tag.  */
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
				      nds32_elf_link_hash_newfunc,
				      sizeof (struct elf_nds32_link_hash_entry),
				      NDS32_ELF_DATA))
    {
      free (ret);
      return NULL;
    }

  ret->sgot = NULL;
  ret->sgotplt = NULL;
  ret->srelgot = NULL;
  ret->splt = NULL;
  ret->srelplt = NULL;
  ret->sdynbss = NULL;
  ret->srelbss = NULL;
  ret->sym_ld_script = NULL;
  ret->ex9_export_file = NULL;
  ret->ex9_import_file = NULL;

  return &ret->root.root;
}

/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
   shortcuts to them in our hash table.  */

static bfd_boolean
create_got_section (bfd *dynobj, struct bfd_link_info *info)
{
  struct elf_nds32_link_hash_table *htab;

  if (!_bfd_elf_create_got_section (dynobj, info))
    return FALSE;

  htab = nds32_elf_hash_table (info);
  htab->sgot = bfd_get_section_by_name (dynobj, ".got");
  htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
  if (!htab->sgot || !htab->sgotplt)
    abort ();

  /* _bfd_elf_create_got_section will create it for us.  */
  htab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
  if (htab->srelgot == NULL
      || !bfd_set_section_flags (dynobj, htab->srelgot,
				 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
				  | SEC_IN_MEMORY | SEC_LINKER_CREATED
				  | SEC_READONLY))
      || !bfd_set_section_alignment (dynobj, htab->srelgot, 2))
    return FALSE;

  return TRUE;
}

/* Create dynamic sections when linking against a dynamic object.  */

static bfd_boolean
nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
{
  struct elf_nds32_link_hash_table *htab;
  flagword flags, pltflags;
  register asection *s;
  const struct elf_backend_data *bed;
  int ptralign = 2;		/* 32-bit  */

  bed = get_elf_backend_data (abfd);

  htab = nds32_elf_hash_table (info);

  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
     .rel[a].bss sections.  */

  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
	   | SEC_LINKER_CREATED);

  pltflags = flags;
  pltflags |= SEC_CODE;
  if (bed->plt_not_loaded)
    pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
  if (bed->plt_readonly)
    pltflags |= SEC_READONLY;

  s = bfd_make_section (abfd, ".plt");
  htab->splt = s;
  if (s == NULL
      || !bfd_set_section_flags (abfd, s, pltflags)
      || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
    return FALSE;

  if (bed->want_plt_sym)
    {
      /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
	 .plt section.  */
      struct bfd_link_hash_entry *bh = NULL;
      struct elf_link_hash_entry *h;

      if (!(_bfd_generic_link_add_one_symbol
	    (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
	     (bfd_vma) 0, (const char *) NULL, FALSE,
	     get_elf_backend_data (abfd)->collect, &bh)))
	return FALSE;

      h = (struct elf_link_hash_entry *) bh;
      h->def_regular = 1;
      h->type = STT_OBJECT;

      if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
	return FALSE;
    }

  s = bfd_make_section (abfd,
			bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
  htab->srelplt = s;
  if (s == NULL
      || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
      || !bfd_set_section_alignment (abfd, s, ptralign))
    return FALSE;

  if (htab->sgot == NULL && !create_got_section (abfd, info))
    return FALSE;

  {
    const char *secname;
    char *relname;
    flagword secflags;
    asection *sec;

    for (sec = abfd->sections; sec; sec = sec->next)
      {
	secflags = bfd_get_section_flags (abfd, sec);
	if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
	    || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
	  continue;
	secname = bfd_get_section_name (abfd, sec);
	relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
	strcpy (relname, ".rela");
	strcat (relname, secname);
	if (bfd_get_section_by_name (abfd, secname))
	  continue;
	s = bfd_make_section (abfd, relname);
	if (s == NULL
	    || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
	    || !bfd_set_section_alignment (abfd, s, ptralign))
	  return FALSE;
      }
  }

  if (bed->want_dynbss)
    {
      /* The .dynbss section is a place to put symbols which are defined
	 by dynamic objects, are referenced by regular objects, and are
	 not functions.  We must allocate space for them in the process
	 image and use a R_*_COPY reloc to tell the dynamic linker to
	 initialize them at run time.  The linker script puts the .dynbss
	 section into the .bss section of the final image.  */
      s = bfd_make_section (abfd, ".dynbss");
      htab->sdynbss = s;
      if (s == NULL
	  || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
	return FALSE;
      /* The .rel[a].bss section holds copy relocs.  This section is not
	 normally needed.  We need to create it here, though, so that the
	 linker will map it to an output section.  We can't just create it
	 only if we need it, because we will not know whether we need it
	 until we have seen all the input files, and the first time the
	 main linker code calls BFD after examining all the input files
	 (size_dynamic_sections) the input sections have already been
	 mapped to the output sections.  If the section turns out not to
	 be needed, we can discard it later.  We will never need this
	 section when generating a shared object, since they do not use
	 copy relocs.  */
      if (!bfd_link_pic (info))
	{
	  s = bfd_make_section (abfd, (bed->default_use_rela_p
				       ? ".rela.bss" : ".rel.bss"));
	  htab->srelbss = s;
	  if (s == NULL
	      || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
	      || !bfd_set_section_alignment (abfd, s, ptralign))
	    return FALSE;
	}
    }

  return TRUE;
}

/* Copy the extra info we tack onto an elf_link_hash_entry.  */
static void
nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
				struct elf_link_hash_entry *dir,
				struct elf_link_hash_entry *ind)
{
  struct elf_nds32_link_hash_entry *edir, *eind;

  edir = (struct elf_nds32_link_hash_entry *) dir;
  eind = (struct elf_nds32_link_hash_entry *) ind;

  if (eind->dyn_relocs != NULL)
    {
      if (edir->dyn_relocs != NULL)
	{
	  struct elf_nds32_dyn_relocs **pp;
	  struct elf_nds32_dyn_relocs *p;

	  if (ind->root.type == bfd_link_hash_indirect)
	    abort ();

	  /* Add reloc counts against the weak sym to the strong sym
	     list.  Merge any entries against the same section.  */
	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
	    {
	      struct elf_nds32_dyn_relocs *q;

	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
		if (q->sec == p->sec)
		  {
		    q->pc_count += p->pc_count;
		    q->count += p->count;
		    *pp = p->next;
		    break;
		  }
	      if (q == NULL)
		pp = &p->next;
	    }
	  *pp = edir->dyn_relocs;
	}

      edir->dyn_relocs = eind->dyn_relocs;
      eind->dyn_relocs = NULL;
    }

  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
}


/* Adjust a symbol defined by a dynamic object and referenced by a
   regular object.  The current definition is in some section of the
   dynamic object, but we're not including those sections.  We have to
   change the definition to something the rest of the link can
   understand.  */

static bfd_boolean
nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
				 struct elf_link_hash_entry *h)
{
  struct elf_nds32_link_hash_table *htab;
  struct elf_nds32_link_hash_entry *eh;
  struct elf_nds32_dyn_relocs *p;
  bfd *dynobj;
  asection *s;
  unsigned int power_of_two;

  dynobj = elf_hash_table (info)->dynobj;

  /* Make sure we know what is going on here.  */
  BFD_ASSERT (dynobj != NULL
	      && (h->needs_plt
		  || h->u.weakdef != NULL
		  || (h->def_dynamic && h->ref_regular && !h->def_regular)));


  /* If this is a function, put it in the procedure linkage table.  We
     will fill in the contents of the procedure linkage table later,
     when we know the address of the .got section.  */
  if (h->type == STT_FUNC || h->needs_plt)
    {
      if (!bfd_link_pic (info)
	  && !h->def_dynamic
	  && !h->ref_dynamic
	  && h->root.type != bfd_link_hash_undefweak
	  && h->root.type != bfd_link_hash_undefined)
	{
	  /* This case can occur if we saw a PLT reloc in an input
	     file, but the symbol was never referred to by a dynamic
	     object.  In such a case, we don't actually need to build
	     a procedure linkage table, and we can just do a PCREL
	     reloc instead.  */
	  h->plt.offset = (bfd_vma) - 1;
	  h->needs_plt = 0;
	}

      return TRUE;
    }
  else
    h->plt.offset = (bfd_vma) - 1;

  /* If this is a weak symbol, and there is a real definition, the
     processor independent code will have arranged for us to see the
     real definition first, and we can just use the same value.  */
  if (h->u.weakdef != NULL)
    {
      BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
      h->root.u.def.section = h->u.weakdef->root.u.def.section;
      h->root.u.def.value = h->u.weakdef->root.u.def.value;
      return TRUE;
    }

  /* This is a reference to a symbol defined by a dynamic object which
     is not a function.  */

  /* If we are creating a shared library, we must presume that the
     only references to the symbol are via the global offset table.
     For such cases we need not do anything here; the relocations will
     be handled correctly by relocate_section.  */
  if (bfd_link_pic (info))
    return TRUE;

  /* If there are no references to this symbol that do not use the
     GOT, we don't need to generate a copy reloc.  */
  if (!h->non_got_ref)
    return TRUE;

  /* If -z nocopyreloc was given, we won't generate them either.  */
  if (info->nocopyreloc)
    {
      h->non_got_ref = 0;
      return TRUE;
    }

  eh = (struct elf_nds32_link_hash_entry *) h;
  for (p = eh->dyn_relocs; p != NULL; p = p->next)
    {
      s = p->sec->output_section;
      if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
	break;
    }

  /* If we didn't find any dynamic relocs in sections which needs the
     copy reloc, then we'll be keeping the dynamic relocs and avoiding
     the copy reloc.  */
  if (p == NULL)
    {
      h->non_got_ref = 0;
      return TRUE;
    }

  /* We must allocate the symbol in our .dynbss section, which will
     become part of the .bss section of the executable.  There will be
     an entry for this symbol in the .dynsym section.  The dynamic
     object will contain position independent code, so all references
     from the dynamic object to this symbol will go through the global
     offset table.  The dynamic linker will use the .dynsym entry to
     determine the address it must put in the global offset table, so
     both the dynamic object and the regular object will refer to the
     same memory location for the variable.  */

  htab = nds32_elf_hash_table (info);
  s = htab->sdynbss;
  BFD_ASSERT (s != NULL);

  /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
     to copy the initial value out of the dynamic object and into the
     runtime process image.  We need to remember the offset into the
     .rela.bss section we are going to use.  */
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
    {
      asection *srel;

      srel = htab->srelbss;
      BFD_ASSERT (srel != NULL);
      srel->size += sizeof (Elf32_External_Rela);
      h->needs_copy = 1;
    }

  /* We need to figure out the alignment required for this symbol.  I
     have no idea how ELF linkers handle this.  */
  power_of_two = bfd_log2 (h->size);
  if (power_of_two > 3)
    power_of_two = 3;

  /* Apply the required alignment.  */
  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
  if (power_of_two > bfd_get_section_alignment (dynobj, s))
    {
      if (!bfd_set_section_alignment (dynobj, s, power_of_two))
	return FALSE;
    }

  /* Define the symbol as being at this point in the section.  */
  h->root.u.def.section = s;
  h->root.u.def.value = s->size;

  /* Increment the section size to make room for the symbol.  */
  s->size += h->size;

  return TRUE;
}

/* Allocate space in .plt, .got and associated reloc sections for
   dynamic relocs.  */

static bfd_boolean
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
{
  struct bfd_link_info *info;
  struct elf_nds32_link_hash_table *htab;
  struct elf_nds32_link_hash_entry *eh;
  struct elf_nds32_dyn_relocs *p;

  if (h->root.type == bfd_link_hash_indirect)
    return TRUE;

  if (h->root.type == bfd_link_hash_warning)
    /* When warning symbols are created, they **replace** the "real"
       entry in the hash table, thus we never get to see the real
       symbol in a hash traversal.  So look at it now.  */
    h = (struct elf_link_hash_entry *) h->root.u.i.link;

  info = (struct bfd_link_info *) inf;
  htab = nds32_elf_hash_table (info);

  eh = (struct elf_nds32_link_hash_entry *) h;

  if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
    {
      /* Make sure this symbol is output as a dynamic symbol.
	 Undefined weak syms won't yet be marked as dynamic.  */
      if (h->dynindx == -1 && !h->forced_local)
	{
	  if (!bfd_elf_link_record_dynamic_symbol (info, h))
	    return FALSE;
	}

      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
	{
	  asection *s = htab->splt;

	  /* If this is the first .plt entry, make room for the special
	     first entry.  */
	  if (s->size == 0)
	    s->size += PLT_ENTRY_SIZE;

	  h->plt.offset = s->size;

	  /* If this symbol is not defined in a regular file, and we are
	     not generating a shared library, then set the symbol to this
	     location in the .plt.  This is required to make function
	     pointers compare as equal between the normal executable and
	     the shared library.  */
	  if (!bfd_link_pic (info) && !h->def_regular)
	    {
	      h->root.u.def.section = s;
	      h->root.u.def.value = h->plt.offset;
	    }

	  /* Make room for this entry.  */
	  s->size += PLT_ENTRY_SIZE;

	  /* We also need to make an entry in the .got.plt section, which
	     will be placed in the .got section by the linker script.  */
	  htab->sgotplt->size += 4;

	  /* We also need to make an entry in the .rel.plt section.  */
	  htab->srelplt->size += sizeof (Elf32_External_Rela);
	}
      else
	{
	  h->plt.offset = (bfd_vma) - 1;
	  h->needs_plt = 0;
	}
    }
  else
    {
      h->plt.offset = (bfd_vma) - 1;
      h->needs_plt = 0;
    }

  if (h->got.refcount > 0)
    {
      asection *s;
      bfd_boolean dyn;
      int tls_type = elf32_nds32_hash_entry (h)->tls_type;

      /* Make sure this symbol is output as a dynamic symbol.
	 Undefined weak syms won't yet be marked as dynamic.  */
      if (h->dynindx == -1 && !h->forced_local)
	{
	  if (!bfd_elf_link_record_dynamic_symbol (info, h))
	    return FALSE;
	}

      s = htab->sgot;
      h->got.offset = s->size;

      if (tls_type == GOT_UNKNOWN)
	abort ();
      else if (tls_type == GOT_NORMAL
	       || tls_type == GOT_TLS_IE)
	/* Need a GOT slot.  */
	s->size += 4;

      dyn = htab->root.dynamic_sections_created;
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
	htab->srelgot->size += sizeof (Elf32_External_Rela);
    }
  else
    h->got.offset = (bfd_vma) - 1;

  if (eh->dyn_relocs == NULL)
    return TRUE;

  /* In the shared -Bsymbolic case, discard space allocated for
     dynamic pc-relative relocs against symbols which turn out to be
     defined in regular objects.  For the normal shared case, discard
     space for pc-relative relocs that have become local due to symbol
     visibility changes.  */

  if (bfd_link_pic (info))
    {
      if (h->def_regular && (h->forced_local || info->symbolic))
	{
	  struct elf_nds32_dyn_relocs **pp;

	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
	    {
	      p->count -= p->pc_count;
	      p->pc_count = 0;
	      if (p->count == 0)
		*pp = p->next;
	      else
		pp = &p->next;
	    }
	}
    }
  else
    {
      /* For the non-shared case, discard space for relocs against
	 symbols which turn out to need copy relocs or are not dynamic.  */

      if (!h->non_got_ref
	  && ((h->def_dynamic
	       && !h->def_regular)
	      || (htab->root.dynamic_sections_created
		  && (h->root.type == bfd_link_hash_undefweak
		      || h->root.type == bfd_link_hash_undefined))))
	{
	  /* Make sure this symbol is output as a dynamic symbol.
	     Undefined weak syms won't yet be marked as dynamic.  */
	  if (h->dynindx == -1 && !h->forced_local)
	    {
	      if (!bfd_elf_link_record_dynamic_symbol (info, h))
		return FALSE;
	    }

	  /* If that succeeded, we know we'll be keeping all the
	     relocs.  */
	  if (h->dynindx != -1)
	    goto keep;
	}

      eh->dyn_relocs = NULL;

    keep:;
    }

  /* Finally, allocate space.  */
  for (p = eh->dyn_relocs; p != NULL; p = p->next)
    {
      asection *sreloc = elf_section_data (p->sec)->sreloc;
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
    }

  return TRUE;
}

/* Find any dynamic relocs that apply to read-only sections.  */

static bfd_boolean
readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
{
  struct elf_nds32_link_hash_entry *eh;
  struct elf_nds32_dyn_relocs *p;

  if (h->root.type == bfd_link_hash_warning)
    h = (struct elf_link_hash_entry *) h->root.u.i.link;

  eh = (struct elf_nds32_link_hash_entry *) h;
  for (p = eh->dyn_relocs; p != NULL; p = p->next)
    {
      asection *s = p->sec->output_section;

      if (s != NULL && (s->flags & SEC_READONLY) != 0)
	{
	  struct bfd_link_info *info = (struct bfd_link_info *) inf;

	  info->flags |= DF_TEXTREL;

	  /* Not an error, just cut short the traversal.  */
	  return FALSE;
	}
    }
  return TRUE;
}

/* Set the sizes of the dynamic sections.  */

static bfd_boolean
nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
				 struct bfd_link_info *info)
{
  struct elf_nds32_link_hash_table *htab;
  bfd *dynobj;
  asection *s;
  bfd_boolean relocs;
  bfd *ibfd;

  htab = nds32_elf_hash_table (info);
  dynobj = htab->root.dynobj;
  BFD_ASSERT (dynobj != NULL);

  if (htab->root.dynamic_sections_created)
    {
      /* Set the contents of the .interp section to the interpreter.  */
      if (!bfd_link_pic (info) && !info->nointerp)
	{
	  s = bfd_get_section_by_name (dynobj, ".interp");
	  BFD_ASSERT (s != NULL);
	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
	}
    }

  /* Set up .got offsets for local syms, and space for local dynamic
     relocs.  */
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
    {
      bfd_signed_vma *local_got;
      bfd_signed_vma *end_local_got;
      bfd_size_type locsymcount;
      Elf_Internal_Shdr *symtab_hdr;
      asection *srel;

      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
	continue;

      for (s = ibfd->sections; s != NULL; s = s->next)
	{
	  struct elf_nds32_dyn_relocs *p;

	  for (p = ((struct elf_nds32_dyn_relocs *)
		    elf_section_data (s)->local_dynrel);
	       p != NULL; p = p->next)
	    {
	      if (!bfd_is_abs_section (p->sec)
		  && bfd_is_abs_section (p->sec->output_section))
		{
		  /* Input section has been discarded, either because
		     it is a copy of a linkonce section or due to
		     linker script /DISCARD/, so we'll be discarding
		     the relocs too.  */
		}
	      else if (p->count != 0)
		{
		  srel = elf_section_data (p->sec)->sreloc;
		  srel->size += p->count * sizeof (Elf32_External_Rela);
		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
		    info->flags |= DF_TEXTREL;
		}
	    }
	}

      local_got = elf_local_got_refcounts (ibfd);
      if (!local_got)
	continue;

      symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
      locsymcount = symtab_hdr->sh_info;
      end_local_got = local_got + locsymcount;
      s = htab->sgot;
      srel = htab->srelgot;
      for (; local_got < end_local_got; ++local_got)
	{
	  if (*local_got > 0)
	    {
	      *local_got = s->size;
	      s->size += 4;
	      if (bfd_link_pic (info))
		srel->size += sizeof (Elf32_External_Rela);
	    }
	  else
	    *local_got = (bfd_vma) - 1;
	}
    }

  /* Allocate global sym .plt and .got entries, and space for global
     sym dynamic relocs.  */
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);

  /* We now have determined the sizes of the various dynamic sections.
     Allocate memory for them.  */
  relocs = FALSE;
  for (s = dynobj->sections; s != NULL; s = s->next)
    {
      if ((s->flags & SEC_LINKER_CREATED) == 0)
	continue;

      if (s == htab->splt)
	{
	  /* Strip this section if we don't need it; see the
	     comment below.  */
	}
      else if (s == htab->sgot)
	{
	  got_size += s->size;
	}
      else if (s == htab->sgotplt)
	{
	  got_size += s->size;
	}
      else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
	{
	  if (s->size != 0 && s != htab->srelplt)
	    relocs = TRUE;

	  /* We use the reloc_count field as a counter if we need
	     to copy relocs into the output file.  */
	  s->reloc_count = 0;
	}
      else
	{
	  /* It's not one of our sections, so don't allocate space.  */
	  continue;
	}

      if (s->size == 0)
	{
	  /* If we don't need this section, strip it from the
	     output file.  This is mostly to handle .rela.bss and
	     .rela.plt.  We must create both sections in
	     create_dynamic_sections, because they must be created
	     before the linker maps input sections to output
	     sections.  The linker does that before
	     adjust_dynamic_symbol is called, and it is that
	     function which decides whether anything needs to go
	     into these sections.  */
	  s->flags |= SEC_EXCLUDE;
	  continue;
	}

      /* Allocate memory for the section contents.  We use bfd_zalloc
	 here in case unused entries are not reclaimed before the
	 section's contents are written out.  This should not happen,
	 but this way if it does, we get a R_NDS32_NONE reloc instead
	 of garbage.  */
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
      if (s->contents == NULL)
	return FALSE;
    }


  if (htab->root.dynamic_sections_created)
    {
      /* Add some entries to the .dynamic section.  We fill in the
	 values later, in nds32_elf_finish_dynamic_sections, but we
	 must add the entries now so that we get the correct size for
	 the .dynamic section.  The DT_DEBUG entry is filled in by the
	 dynamic linker and used by the debugger.  */
#define add_dynamic_entry(TAG, VAL) \
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)

      if (!bfd_link_pic (info))
	{
	  if (!add_dynamic_entry (DT_DEBUG, 0))
	    return FALSE;
	}

      if (htab->splt->size != 0)
	{
	  if (!add_dynamic_entry (DT_PLTGOT, 0)
	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
	      || !add_dynamic_entry (DT_JMPREL, 0))
	    return FALSE;
	}

      if (relocs)
	{
	  if (!add_dynamic_entry (DT_RELA, 0)
	      || !add_dynamic_entry (DT_RELASZ, 0)
	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
	    return FALSE;

	  /* If any dynamic relocs apply to a read-only section,
	     then we need a DT_TEXTREL entry.  */
	  if ((info->flags & DF_TEXTREL) == 0)
	    elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
				    (void *) info);

	  if ((info->flags & DF_TEXTREL) != 0)
	    {
	      if (!add_dynamic_entry (DT_TEXTREL, 0))
		return FALSE;
	    }
	}
    }
#undef add_dynamic_entry

  return TRUE;
}

static bfd_reloc_status_type
nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
			 bfd_vma relocation, bfd_byte *location)
{
  int size;
  bfd_vma x = 0;
  bfd_reloc_status_type flag;
  unsigned int rightshift = howto->rightshift;
  unsigned int bitpos = howto->bitpos;

  /* If the size is negative, negate RELOCATION.  This isn't very
     general.  */
  if (howto->size < 0)
    relocation = -relocation;

  /* Get the value we are going to relocate.  */
  size = bfd_get_reloc_size (howto);
  switch (size)
    {
    default:
      abort ();
      break;
    case 0:
      return bfd_reloc_ok;
    case 2:
      x = bfd_getb16 (location);
      break;
    case 4:
      x = bfd_getb32 (location);
      break;
    }

  /* Check for overflow.  FIXME: We may drop bits during the addition
     which we don't check for.  We must either check at every single
     operation, which would be tedious, or we must do the computations
     in a type larger than bfd_vma, which would be inefficient.  */
  flag = bfd_reloc_ok;
  if (howto->complain_on_overflow != complain_overflow_dont)
    {
      bfd_vma addrmask, fieldmask, signmask, ss;
      bfd_vma a, b, sum;

      /* Get the values to be added together.  For signed and unsigned
	 relocations, we assume that all values should be truncated to
	 the size of an address.  For bitfields, all the bits matter.
	 See also bfd_check_overflow.  */
      fieldmask = N_ONES (howto->bitsize);
      signmask = ~fieldmask;
      addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
      a = (relocation & addrmask) >> rightshift;
      b = (x & howto->src_mask & addrmask) >> bitpos;

      switch (howto->complain_on_overflow)
	{
	case complain_overflow_signed:
	  /* If any sign bits are set, all sign bits must be set.
	     That is, A must be a valid negative address after
	     shifting.  */
	  signmask = ~(fieldmask >> 1);
	  /* Fall through.  */

	case complain_overflow_bitfield:
	  /* Much like the signed check, but for a field one bit
	     wider.  We allow a bitfield to represent numbers in the
	     range -2**n to 2**n-1, where n is the number of bits in the
	     field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
	     can't overflow, which is exactly what we want.  */
	  ss = a & signmask;
	  if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
	    flag = bfd_reloc_overflow;

	  /* We only need this next bit of code if the sign bit of B
	     is below the sign bit of A.  This would only happen if
	     SRC_MASK had fewer bits than BITSIZE.  Note that if
	     SRC_MASK has more bits than BITSIZE, we can get into
	     trouble; we would need to verify that B is in range, as
	     we do for A above.  */
	  ss = ((~howto->src_mask) >> 1) & howto->src_mask;
	  ss >>= bitpos;

	  /* Set all the bits above the sign bit.  */
	  b = (b ^ ss) - ss;

	  /* Now we can do the addition.  */
	  sum = a + b;

	  /* See if the result has the correct sign.  Bits above the
	     sign bit are junk now; ignore them.  If the sum is
	     positive, make sure we did not have all negative inputs;
	     if the sum is negative, make sure we did not have all
	     positive inputs.  The test below looks only at the sign
	     bits, and it really just
	     SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)

	     We mask with addrmask here to explicitly allow an address
	     wrap-around.  The Linux kernel relies on it, and it is
	     the only way to write assembler code which can run when
	     loaded at a location 0x80000000 away from the location at
	     which it is linked.  */
	  if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
	    flag = bfd_reloc_overflow;

	  break;

	case complain_overflow_unsigned:
	  /* Checking for an unsigned overflow is relatively easy:
	     trim the addresses and add, and trim the result as well.
	     Overflow is normally indicated when the result does not
	     fit in the field.  However, we also need to consider the
	     case when, e.g., fieldmask is 0x7fffffff or smaller, an
	     input is 0x80000000, and bfd_vma is only 32 bits; then we
	     will get sum == 0, but there is an overflow, since the
	     inputs did not fit in the field.  Instead of doing a
	     separate test, we can check for this by or-ing in the
	     operands when testing for the sum overflowing its final
	     field.  */
	  sum = (a + b) & addrmask;
	  if ((a | b | sum) & signmask)
	    flag = bfd_reloc_overflow;
	  break;

	default:
	  abort ();
	}
    }

  /* Put RELOCATION in the right bits.  */
  relocation >>= (bfd_vma) rightshift;
  relocation <<= (bfd_vma) bitpos;

  /* Add RELOCATION to the right bits of X.  */
  /* FIXME : 090616
     Because the relaxation may generate duplicate relocation at one address,
     an addition to immediate in the instruction may cause the relocation added
     several times.
     This bug should be fixed in assembler, but a check is also needed here.  */
  if (howto->partial_inplace)
    x = ((x & ~howto->dst_mask)
	 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
  else
    x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));


  /* Put the relocated value back in the object file.  */
  switch (size)
    {
    default:
    case 0:
    case 1:
    case 8:
      abort ();
      break;
    case 2:
      bfd_putb16 (x, location);
      break;
    case 4:
      bfd_putb32 (x, location);
      break;
    }

  return flag;
}

static bfd_reloc_status_type
nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
			       asection *input_section, bfd_byte *contents,
			       bfd_vma address, bfd_vma value, bfd_vma addend)
{
  bfd_vma relocation;

  /* Sanity check the address.  */
  if (address > bfd_get_section_limit (input_bfd, input_section))
    return bfd_reloc_outofrange;

  /* This function assumes that we are dealing with a basic relocation
     against a symbol.  We want to compute the value of the symbol to
     relocate to.  This is just VALUE, the value of the symbol, plus
     ADDEND, any addend associated with the reloc.  */
  relocation = value + addend;

  /* If the relocation is PC relative, we want to set RELOCATION to
     the distance between the symbol (currently in RELOCATION) and the
     location we are relocating.  Some targets (e.g., i386-aout)
     arrange for the contents of the section to be the negative of the
     offset of the location within the section; for such targets
     pcrel_offset is FALSE.  Other targets (e.g., m88kbcs or ELF)
     simply leave the contents of the section as zero; for such
     targets pcrel_offset is TRUE.  If pcrel_offset is FALSE we do not
     need to subtract out the offset of the location within the
     section (which is just ADDRESS).  */
  if (howto->pc_relative)
    {
      relocation -= (input_section->output_section->vma
		     + input_section->output_offset);
      if (howto->pcrel_offset)
	relocation -= address;
    }

  return nds32_relocate_contents (howto, input_bfd, relocation,
				  contents + address);
}

static bfd_boolean
nds32_elf_output_symbol_hook (struct bfd_link_info *info,
			      const char *name,
			      Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
			      asection *input_sec,
			      struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
{
  const char *source;
  FILE *sym_ld_script = NULL;
  struct elf_nds32_link_hash_table *table;

  table = nds32_elf_hash_table (info);
  sym_ld_script = table->sym_ld_script;
  if (!sym_ld_script)
    return TRUE;

  if (!h || !name || *name == '\0')
    return TRUE;

  if (input_sec->flags & SEC_EXCLUDE)
    return TRUE;

  if (!check_start_export_sym)
    {
      fprintf (sym_ld_script, "SECTIONS\n{\n");
      check_start_export_sym = 1;
    }

  if (h->root.type == bfd_link_hash_defined
      || h->root.type == bfd_link_hash_defweak)
    {
      if (!h->root.u.def.section->output_section)
	return TRUE;

      if (bfd_is_const_section (input_sec))
	source = input_sec->name;
      else
	source = input_sec->owner->filename;

      fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
	       h->root.root.string,
	       (long) (h->root.u.def.value
		+ h->root.u.def.section->output_section->vma
		+ h->root.u.def.section->output_offset), source);
    }

  return TRUE;
}

/* Relocate an NDS32/D ELF section.
   There is some attempt to make this function usable for many architectures,
   both for RELA and REL type relocs, if only to serve as a learning tool.

   The RELOCATE_SECTION function is called by the new ELF backend linker
   to handle the relocations for a section.

   The relocs are always passed as Rela structures; if the section
   actually uses Rel structures, the r_addend field will always be
   zero.

   This function is responsible for adjust the section contents as
   necessary, and (if using Rela relocs and generating a
   relocatable output file) adjusting the reloc addend as
   necessary.

   This function does not have to worry about setting the reloc
   address or the reloc symbol index.

   LOCAL_SYMS is a pointer to the swapped in local symbols.

   LOCAL_SECTIONS is an array giving the section in the input file
   corresponding to the st_shndx field of each local symbol.

   The global hash table entry for the global symbols can be found
   via elf_sym_hashes (input_bfd).

   When generating relocatable output, this function must handle
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   going to be the section symbol corresponding to the output
   section, which means that the addend must be adjusted
   accordingly.  */

static bfd_vma
dtpoff_base (struct bfd_link_info *info)
{
  /* If tls_sec is NULL, we should have signalled an error already.  */
  if (elf_hash_table (info)->tls_sec == NULL)
    return 0;
  return elf_hash_table (info)->tls_sec->vma;
}

static bfd_boolean
nds32_elf_relocate_section (bfd *                  output_bfd ATTRIBUTE_UNUSED,
			    struct bfd_link_info * info,
			    bfd *                  input_bfd,
			    asection *             input_section,
			    bfd_byte *             contents,
			    Elf_Internal_Rela *    relocs,
			    Elf_Internal_Sym *     local_syms,
			    asection **            local_sections)
{
  Elf_Internal_Shdr *symtab_hdr;
  struct elf_link_hash_entry **sym_hashes;
  Elf_Internal_Rela *rel, *relend;
  bfd_boolean ret = TRUE;		/* Assume success.  */
  int align = 0;
  bfd_reloc_status_type r;
  const char *errmsg = NULL;
  bfd_vma gp;
  struct elf_nds32_link_hash_table *htab;
  bfd *dynobj;
  bfd_vma *local_got_offsets;
  asection *sgot, *splt, *sreloc;
  bfd_vma high_address;
  struct elf_nds32_link_hash_table *table;
  int eliminate_gc_relocs;
  bfd_vma fpbase_addr;

  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  sym_hashes = elf_sym_hashes (input_bfd);
  htab = nds32_elf_hash_table (info);
  high_address = bfd_get_section_limit (input_bfd, input_section);

  dynobj = htab->root.dynobj;
  local_got_offsets = elf_local_got_offsets (input_bfd);

  sgot = htab->sgot;
  splt = htab->splt;
  sreloc = NULL;

  rel = relocs;
  relend = relocs + input_section->reloc_count;

  table = nds32_elf_hash_table (info);
  eliminate_gc_relocs = table->eliminate_gc_relocs;
  /* By this time, we can adjust the value of _SDA_BASE_.  */
  if ((!bfd_link_relocatable (info)))
    {
      is_SDA_BASE_set = 1;
      r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
      if (r != bfd_reloc_ok)
	return FALSE;
    }

  if (is_ITB_BASE_set == 0)
    {
      /* Set the _ITB_BASE_.  */
      if (!nds32_elf_ex9_itb_base (info))
	{
	  (*_bfd_error_handler) (_("%B: error: Cannot set _ITB_BASE_"),
				 output_bfd);
	  bfd_set_error (bfd_error_bad_value);
	}
    }

  if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
    if (!nds32_elf_ifc_reloc ())
      (*_bfd_error_handler) (_("error: IFC relocation error."));

 /* Relocation for .ex9.itable.  */
  if (table->target_optimize & NDS32_RELAX_EX9_ON
      || (table->ex9_import_file && table->update_ex9_table))
    nds32_elf_ex9_reloc_jmp (info);

  /* Use gp as fp to prevent truncated fit.  Because in relaxation time
     the fp value is set as gp, and it has be reverted for instruction
     setting fp.  */
  fpbase_addr = elf_gp (output_bfd);

  for (rel = relocs; rel < relend; rel++)
    {
      enum elf_nds32_reloc_type r_type;
      reloc_howto_type *howto = NULL;
      unsigned long r_symndx;
      struct elf_link_hash_entry *h = NULL;
      Elf_Internal_Sym *sym = NULL;
      asection *sec;
      bfd_vma relocation;

      /* We can't modify r_addend here as elf_link_input_bfd has an assert to
	 ensure it's zero (we use REL relocs, not RELA).  Therefore this
	 should be assigning zero to `addend', but for clarity we use
	 `r_addend'.  */

      bfd_vma addend = rel->r_addend;
      bfd_vma offset = rel->r_offset;

      r_type = ELF32_R_TYPE (rel->r_info);
      if (r_type >= R_NDS32_max)
	{
	  (*_bfd_error_handler) (_("%B: error: unknown relocation type %d."),
				 input_bfd, r_type);
	  bfd_set_error (bfd_error_bad_value);
	  ret = FALSE;
	  continue;
	}

      if (r_type == R_NDS32_GNU_VTENTRY
	  || r_type == R_NDS32_GNU_VTINHERIT
	  || r_type == R_NDS32_NONE
	  || r_type == R_NDS32_RELA_GNU_VTENTRY
	  || r_type == R_NDS32_RELA_GNU_VTINHERIT
	  || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
	  || r_type == R_NDS32_DATA
	  || r_type == R_NDS32_TRAN
	  || (r_type >= R_NDS32_LONGCALL4 && r_type <= R_NDS32_LONGJUMP7))
	continue;

      /* If we enter the fp-as-gp region.  Resolve the address
	 of best fp-base.  */
      if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
	  && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
	{
	  int dist;

	  /* Distance to relocation of best fp-base is encoded in R_SYM.  */
	  dist =  rel->r_addend >> 16;
	  fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
						  local_syms, symtab_hdr);
	}
      else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
	       && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
	{
	  fpbase_addr = elf_gp (output_bfd);
	}

      if (((r_type >= R_NDS32_DWARF2_OP1_RELA
	    && r_type <= R_NDS32_DWARF2_LEB_RELA)
	   || r_type >= R_NDS32_RELAX_ENTRY) && !bfd_link_relocatable (info))
	continue;

      howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
      r_symndx = ELF32_R_SYM (rel->r_info);

      /* This is a final link.  */
      sym = NULL;
      sec = NULL;
      h = NULL;

      if (r_symndx < symtab_hdr->sh_info)
	{
	  /* Local symbol.  */
	  sym = local_syms + r_symndx;
	  sec = local_sections[r_symndx];

	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
	  addend = rel->r_addend;
	}
      else
	{
	  /* External symbol.  */
	  bfd_boolean warned, ignored, unresolved_reloc;
	  int symndx = r_symndx - symtab_hdr->sh_info;

	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
				   r_symndx, symtab_hdr, sym_hashes, h, sec,
				   relocation, unresolved_reloc, warned,
				   ignored);

	  /* la $fp, _FP_BASE_ is per-function (region).
	     Handle it specially.  */
	  switch ((int) r_type)
	    {
	    case R_NDS32_SDA19S0_RELA:
	    case R_NDS32_SDA15S0_RELA:
	    case R_NDS32_20_RELA:
	      if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
			  FP_BASE_NAME) == 0)
		{
		  relocation = fpbase_addr;
		  break;
		}
	    }

	}

      if (bfd_link_relocatable (info))
	{
	  /* This is a relocatable link.  We don't have to change
	     anything, unless the reloc is against a section symbol,
	     in which case we have to adjust according to where the
	     section symbol winds up in the output section.  */
	  if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
	    rel->r_addend += sec->output_offset + sym->st_value;

	  continue;
	}

      /* Sanity check the address.  */
      if (offset > high_address)
	{
	  r = bfd_reloc_outofrange;
	  goto check_reloc;
	}

      if ((r_type >= R_NDS32_DWARF2_OP1_RELA
	   && r_type <= R_NDS32_DWARF2_LEB_RELA)
	  || r_type >= R_NDS32_RELAX_ENTRY)
	continue;

      switch ((int) r_type)
	{
	case R_NDS32_GOTOFF:
	  /* Relocation is relative to the start of the global offset
	     table (for ld24 rx, #uimm24), e.g. access at label+addend

	     ld24 rx. #label@GOTOFF + addend
	     sub  rx, r12.  */
	case R_NDS32_GOTOFF_HI20:
	case R_NDS32_GOTOFF_LO12:
	case R_NDS32_GOTOFF_LO15:
	case R_NDS32_GOTOFF_LO19:
	  BFD_ASSERT (sgot != NULL);

	  relocation -= elf_gp (output_bfd);
	  break;

	case R_NDS32_9_PLTREL:
	case R_NDS32_25_PLTREL:
	  /* Relocation is to the entry for this symbol in the
	     procedure linkage table.  */

	  /* The native assembler will generate a 25_PLTREL reloc
	     for a local symbol if you assemble a call from one
	     section to another when using -K pic.  */
	  if (h == NULL)
	    break;

	  if (h->forced_local)
	    break;

	  /* We didn't make a PLT entry for this symbol.  This
	     happens when statically linking PIC code, or when
	     using -Bsymbolic.  */
	  if (h->plt.offset == (bfd_vma) - 1)
	    break;

	  relocation = (splt->output_section->vma
			+ splt->output_offset + h->plt.offset);
	  break;

	case R_NDS32_PLT_GOTREL_HI20:
	case R_NDS32_PLT_GOTREL_LO12:
	case R_NDS32_PLT_GOTREL_LO15:
	case R_NDS32_PLT_GOTREL_LO19:
	case R_NDS32_PLT_GOTREL_LO20:
	  if (h == NULL || h->forced_local || h->plt.offset == (bfd_vma) - 1)
	    {
	      /* We didn't make a PLT entry for this symbol.  This
		 happens when statically linking PIC code, or when
		 using -Bsymbolic.  */
	      relocation -= elf_gp (output_bfd);
	      break;
	    }

	  relocation = (splt->output_section->vma
			+ splt->output_offset + h->plt.offset);

	  relocation -= elf_gp (output_bfd);
	  break;

	case R_NDS32_PLTREL_HI20:
	case R_NDS32_PLTREL_LO12:

	  /* Relocation is to the entry for this symbol in the
	     procedure linkage table.  */

	  /* The native assembler will generate a 25_PLTREL reloc
	     for a local symbol if you assemble a call from one
	     section to another when using -K pic.  */
	  if (h == NULL)
	    break;

	  if (h->forced_local)
	    break;

	  if (h->plt.offset == (bfd_vma) - 1)
	    /* We didn't make a PLT entry for this symbol.  This
	       happens when statically linking PIC code, or when
	       using -Bsymbolic.  */
	    break;

	  if (splt == NULL)
	    break;

	  relocation = (splt->output_section->vma
			+ splt->output_offset
			+ h->plt.offset + 4)
		       - (input_section->output_section->vma
			  + input_section->output_offset
			  + rel->r_offset);

	  break;

	case R_NDS32_GOTPC20:
	  /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
	     ld24 rx,#_GLOBAL_OFFSET_TABLE_  */
	  relocation = elf_gp (output_bfd);
	  break;

	case R_NDS32_GOTPC_HI20:
	case R_NDS32_GOTPC_LO12:
	    {
	      /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
		 bl .+4
		 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
		 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
		 or
		 bl .+4
		 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
		 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
	       */
	      relocation = elf_gp (output_bfd);
	      relocation -= (input_section->output_section->vma
			     + input_section->output_offset + rel->r_offset);
	      break;
	    }

	case R_NDS32_GOT20:
	  /* Fall through.  */
	case R_NDS32_GOT_HI20:
	case R_NDS32_GOT_LO12:
	case R_NDS32_GOT_LO15:
	case R_NDS32_GOT_LO19:
	  /* Relocation is to the entry for this symbol in the global
	     offset table.  */
	  BFD_ASSERT (sgot != NULL);

	  if (h != NULL)
	    {
	      bfd_boolean dyn;
	      bfd_vma off;

	      off = h->got.offset;
	      BFD_ASSERT (off != (bfd_vma) - 1);
	      dyn = htab->root.dynamic_sections_created;
	      if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
						    bfd_link_pic (info),
						    h)
		  || (bfd_link_pic (info)
		      && (info->symbolic
			  || h->dynindx == -1
			  || h->forced_local) && h->def_regular))
		{
		  /* This is actually a static link, or it is a
		     -Bsymbolic link and the symbol is defined
		     locally, or the symbol was forced to be local
		     because of a version file.  We must initialize
		     this entry in the global offset table.  Since the
		     offset must always be a multiple of 4, we use the
		     least significant bit to record whether we have
		     initialized it already.

		     When doing a dynamic link, we create a .rela.got
		     relocation entry to initialize the value.  This
		     is done in the finish_dynamic_symbol routine.  */
		  if ((off & 1) != 0)
		    off &= ~1;
		  else
		    {
		      bfd_put_32 (output_bfd, relocation, sgot->contents + off);
		      h->got.offset |= 1;
		    }
		}
	      relocation = sgot->output_section->vma + sgot->output_offset + off
			   - elf_gp (output_bfd);
	    }
	  else
	    {
	      bfd_vma off;
	      bfd_byte *loc;

	      BFD_ASSERT (local_got_offsets != NULL
			  && local_got_offsets[r_symndx] != (bfd_vma) - 1);

	      off = local_got_offsets[r_symndx];

	      /* The offset must always be a multiple of 4.  We use
		 the least significant bit to record whether we have
		 already processed this entry.  */
	      if ((off & 1) != 0)
		off &= ~1;
	      else
		{
		  bfd_put_32 (output_bfd, relocation, sgot->contents + off);

		  if (bfd_link_pic (info))
		    {
		      asection *srelgot;
		      Elf_Internal_Rela outrel;

		      /* We need to generate a R_NDS32_RELATIVE reloc
			 for the dynamic linker.  */
		      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
		      BFD_ASSERT (srelgot != NULL);

		      outrel.r_offset = (elf_gp (output_bfd)
					 + sgot->output_offset + off);
		      outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
		      outrel.r_addend = relocation;
		      loc = srelgot->contents;
		      loc +=
			srelgot->reloc_count * sizeof (Elf32_External_Rela);
		      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
		      ++srelgot->reloc_count;
		    }
		  local_got_offsets[r_symndx] |= 1;
		}
	      relocation = sgot->output_section->vma + sgot->output_offset + off
			   - elf_gp (output_bfd);
	    }

	  break;

	case R_NDS32_16_RELA:
	case R_NDS32_20_RELA:
	case R_NDS32_5_RELA:
	case R_NDS32_32_RELA:
	case R_NDS32_9_PCREL_RELA:
	case R_NDS32_WORD_9_PCREL_RELA:
	case R_NDS32_10_UPCREL_RELA:
	case R_NDS32_15_PCREL_RELA:
	case R_NDS32_17_PCREL_RELA:
	case R_NDS32_25_PCREL_RELA:
	case R_NDS32_HI20_RELA:
	case R_NDS32_LO12S3_RELA:
	case R_NDS32_LO12S2_RELA:
	case R_NDS32_LO12S2_DP_RELA:
	case R_NDS32_LO12S2_SP_RELA:
	case R_NDS32_LO12S1_RELA:
	case R_NDS32_LO12S0_RELA:
	case R_NDS32_LO12S0_ORI_RELA:
	  if (bfd_link_pic (info) && r_symndx != 0
	      && (input_section->flags & SEC_ALLOC) != 0
	      && (eliminate_gc_relocs == 0
		  || (sec && (sec->flags & SEC_EXCLUDE) == 0))
	      && ((r_type != R_NDS32_9_PCREL_RELA
		   && r_type != R_NDS32_WORD_9_PCREL_RELA
		   && r_type != R_NDS32_10_UPCREL_RELA
		   && r_type != R_NDS32_15_PCREL_RELA
		   && r_type != R_NDS32_17_PCREL_RELA
		   && r_type != R_NDS32_25_PCREL_RELA
		   && !(r_type == R_NDS32_32_RELA
			&& strcmp (input_section->name, ".eh_frame") == 0))
		  || (h != NULL && h->dynindx != -1
		      && (!info->symbolic || !h->def_regular))))
	    {
	      Elf_Internal_Rela outrel;
	      bfd_boolean skip, relocate;
	      bfd_byte *loc;

	      /* When generating a shared object, these relocations
		 are copied into the output file to be resolved at run
		 time.  */

	      if (sreloc == NULL)
		{
		  const char *name;

		  name = bfd_elf_string_from_elf_section
		    (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
		     elf_section_data (input_section)->rela.hdr->sh_name);
		  if (name == NULL)
		    return FALSE;

		  BFD_ASSERT (strncmp (name, ".rela", 5) == 0
			      && strcmp (bfd_get_section_name (input_bfd,
							       input_section),
					 name + 5) == 0);

		  sreloc = bfd_get_section_by_name (dynobj, name);
		  BFD_ASSERT (sreloc != NULL);
		}

	      skip = FALSE;
	      relocate = FALSE;

	      outrel.r_offset = _bfd_elf_section_offset (output_bfd,
							 info,
							 input_section,
							 rel->r_offset);
	      if (outrel.r_offset == (bfd_vma) - 1)
		skip = TRUE;
	      else if (outrel.r_offset == (bfd_vma) - 2)
		skip = TRUE, relocate = TRUE;
	      outrel.r_offset += (input_section->output_section->vma
				  + input_section->output_offset);

	      if (skip)
		memset (&outrel, 0, sizeof outrel);
	      else if (r_type == R_NDS32_17_PCREL_RELA
		       || r_type == R_NDS32_15_PCREL_RELA
		       || r_type == R_NDS32_25_PCREL_RELA)
		{
		  BFD_ASSERT (h != NULL && h->dynindx != -1);
		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
		  outrel.r_addend = rel->r_addend;
		}
	      else
		{
		  /* h->dynindx may be -1 if this symbol was marked to
		     become local.  */
		  if (h == NULL
		      || ((info->symbolic || h->dynindx == -1)
			  && h->def_regular))
		    {
		      relocate = TRUE;
		      outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
		      outrel.r_addend = relocation + rel->r_addend;
		    }
		  else
		    {
		      BFD_ASSERT (h->dynindx != -1);
		      outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
		      outrel.r_addend = rel->r_addend;
		    }
		}

	      loc = sreloc->contents;
	      loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
	      ++sreloc->reloc_count;

	      /* If this reloc is against an external symbol, we do
		 not want to fiddle with the addend.  Otherwise, we
		 need to include the symbol value so that it becomes
		 an addend for the dynamic reloc.  */
	      if (!relocate)
		continue;
	    }
	  break;

	case R_NDS32_25_ABS_RELA:
	  if (bfd_link_pic (info))
	    {
	      (*_bfd_error_handler)
		(_("%s: warning: cannot deal R_NDS32_25_ABS_RELA in shared "
		   "mode."), bfd_get_filename (input_bfd));
	      return FALSE;
	    }
	  break;

	case R_NDS32_9_PCREL:
	  r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
					  contents, offset,
					  sec, relocation, addend);
	  goto check_reloc;

	case R_NDS32_HI20:
	    {
	      Elf_Internal_Rela *lorel;

	      /* We allow an arbitrary number of HI20 relocs before the
		 LO12 reloc.  This permits gcc to emit the HI and LO relocs
		 itself.  */
	      for (lorel = rel + 1;
		   (lorel < relend
		    && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
		continue;
	      if (lorel < relend
		  && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
		      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
		      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
		      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
		{
		  nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
					   contents, relocation + addend);
		  r = bfd_reloc_ok;
		}
	      else
		r = _bfd_final_link_relocate (howto, input_bfd, input_section,
					      contents, offset, relocation,
					      addend);
	    }

	  goto check_reloc;

	case R_NDS32_GOT17S2_RELA:
	case R_NDS32_GOT15S2_RELA:
	    {
	      bfd_vma off;

	      BFD_ASSERT (sgot != NULL);

	      if (h != NULL)
		{
		  bfd_boolean dyn;

		  off = h->got.offset;
		  BFD_ASSERT (off != (bfd_vma) - 1);

		  dyn = htab->root.dynamic_sections_created;
		  if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
		      (dyn, bfd_link_pic (info), h)
		      || (bfd_link_pic (info)
			  && (info->symbolic
			      || h->dynindx == -1
			      || h->forced_local)
			  && h->def_regular))
		    {
		      /* This is actually a static link, or it is a
			 -Bsymbolic link and the symbol is defined
			 locally, or the symbol was forced to be local
			 because of a version file.  We must initialize
			 this entry in the global offset table.  Since the
			 offset must always be a multiple of 4, we use the
			 least significant bit to record whether we have
			 initialized it already.

			 When doing a dynamic link, we create a .rela.got
			 relocation entry to initialize the value.  This
			 is done in the finish_dynamic_symbol routine.  */
		      if ((off & 1) != 0)
			off &= ~1;
		      else
			{
			  bfd_put_32 (output_bfd, relocation,
				      sgot->contents + off);
			  h->got.offset |= 1;
			}
		    }
		}
	      else
		{
		  bfd_byte *loc;

		  BFD_ASSERT (local_got_offsets != NULL
			      && local_got_offsets[r_symndx] != (bfd_vma) - 1);

		  off = local_got_offsets[r_symndx];

		  /* The offset must always be a multiple of 4.  We use
		     the least significant bit to record whether we have
		     already processed this entry.  */
		  if ((off & 1) != 0)
		    off &= ~1;
		  else
		    {
		      bfd_put_32 (output_bfd, relocation, sgot->contents + off);

		      if (bfd_link_pic (info))
			{
			  asection *srelgot;
			  Elf_Internal_Rela outrel;

			  /* We need to generate a R_NDS32_RELATIVE reloc
			     for the dynamic linker.  */
			  srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
			  BFD_ASSERT (srelgot != NULL);

			  outrel.r_offset = (elf_gp (output_bfd)
					     + sgot->output_offset + off);
			  outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
			  outrel.r_addend = relocation;
			  loc = srelgot->contents;
			  loc +=
			    srelgot->reloc_count * sizeof (Elf32_External_Rela);
			  bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
			  ++srelgot->reloc_count;
			}
		      local_got_offsets[r_symndx] |= 1;
		    }
		}
	      relocation = sgot->output_section->vma + sgot->output_offset + off
			   - elf_gp (output_bfd);
	    }
	  if (relocation & align)
	    {
	      /* Incorrect alignment.  */
	      (*_bfd_error_handler)
		(_("%B: warning: unaligned access to GOT entry."), input_bfd);
	      ret = FALSE;
	      r = bfd_reloc_dangerous;
	      goto check_reloc;
	    }
	  break;

	case R_NDS32_SDA16S3_RELA:
	case R_NDS32_SDA15S3_RELA:
	case R_NDS32_SDA15S3:
	  align = 0x7;
	  goto handle_sda;

	case R_NDS32_SDA17S2_RELA:
	case R_NDS32_SDA15S2_RELA:
	case R_NDS32_SDA12S2_SP_RELA:
	case R_NDS32_SDA12S2_DP_RELA:
	case R_NDS32_SDA15S2:
	case R_NDS32_SDA_FP7U2_RELA:
	  align = 0x3;
	  goto handle_sda;

	case R_NDS32_SDA18S1_RELA:
	case R_NDS32_SDA15S1_RELA:
	case R_NDS32_SDA15S1:
	  align = 0x1;
	  goto handle_sda;

	case R_NDS32_SDA19S0_RELA:
	case R_NDS32_SDA15S0_RELA:
	case R_NDS32_SDA15S0:
	    {
	      align = 0x0;
handle_sda:
	      BFD_ASSERT (sec != NULL);

	      /* If the symbol is in the abs section, the out_bfd will be null.
		 This happens when the relocation has a symbol@GOTOFF.  */
	      r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
	      if (r != bfd_reloc_ok)
		{
		  (*_bfd_error_handler)
		    (_("%B: warning: relocate SDA_BASE failed."), input_bfd);
		  ret = FALSE;
		  goto check_reloc;
		}

	      /* At this point `relocation' contains the object's
		 address.  */
	      if (r_type == R_NDS32_SDA_FP7U2_RELA)
		{
		  relocation -= fpbase_addr;
		}
	      else
		relocation -= gp;
	      /* Now it contains the offset from _SDA_BASE_.  */

	      /* Make sure alignment is correct.  */

	      if (relocation & align)
		{
		  /* Incorrect alignment.  */
		  (*_bfd_error_handler)
		    (_("%B(%A): warning: unaligned small data access of type %d."),
		     input_bfd, input_section, r_type);
		  ret = FALSE;
		  goto check_reloc;
		}
	    }

	  break;
	case R_NDS32_17IFC_PCREL_RELA:
	case R_NDS32_10IFCU_PCREL_RELA:
	  /* do nothing */
	  break;

	case R_NDS32_TLS_LE_HI20:
	case R_NDS32_TLS_LE_LO12:
	case R_NDS32_TLS_LE_20:
	case R_NDS32_TLS_LE_15S0:
	case R_NDS32_TLS_LE_15S1:
	case R_NDS32_TLS_LE_15S2:
	  if (elf_hash_table (info)->tls_sec != NULL)
	    relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
	  break;
	case R_NDS32_TLS_IE_HI20:
	case R_NDS32_TLS_IE_LO12S2:
	  {
	    /* Relocation is to the entry for this symbol in the global
	       offset table.  */
	    unsigned int tls_type;
	    asection *srelgot;
	    Elf_Internal_Rela outrel;
	    bfd_vma off;
	    bfd_byte *loc;
	    int indx = 0;

	    BFD_ASSERT (sgot != NULL);
	    if (h != NULL)
	      {
		bfd_boolean dyn;

		off = h->got.offset;
		BFD_ASSERT (off != (bfd_vma) - 1);
		dyn = htab->root.dynamic_sections_created;
		tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
		if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
		    && (!bfd_link_pic (info)
			|| !SYMBOL_REFERENCES_LOCAL (info, h)))
		  indx = h->dynindx;
	      }
	    else
	      {
		/* Never happen currently.  */
		BFD_ASSERT (local_got_offsets != NULL
			    && local_got_offsets[r_symndx] != (bfd_vma) - 1);

		off = local_got_offsets[r_symndx];

		tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
	      }
	    relocation = sgot->output_section->vma + sgot->output_offset + off;

	    if (r_type == R_NDS32_TLS_IE_LO12S2)
	      break;

	    /* The offset must always be a multiple of 4.  We use
	       the least significant bit to record whether we have
	       already processed this entry.  */
	    if ((off & 1) != 0)
	      off &= ~1;
	    else
	      {
		bfd_boolean need_relocs = FALSE;
		srelgot = htab->srelgot;
		if ((bfd_link_pic (info) || indx != 0)
		    && (h == NULL
			|| ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
			|| h->root.type != bfd_link_hash_undefweak))
		  {
		    need_relocs = TRUE;
		    BFD_ASSERT (srelgot != NULL);
		  }
		if (tls_type & GOT_TLS_IE)
		  {
		    if (need_relocs)
		      {
			if (h->dynindx == 0)
			  outrel.r_addend = relocation - dtpoff_base (info);
			else
			  outrel.r_addend = 0;
			outrel.r_offset = (sgot->output_section->vma
					   + sgot->output_offset
					   + off);
			outrel.r_info =
			  ELF32_R_INFO (h->dynindx, R_NDS32_TLS_TPOFF);

			loc = srelgot->contents;
			loc +=
			  srelgot->reloc_count * sizeof (Elf32_External_Rela);
			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
			++srelgot->reloc_count;
		      }
		    else
		      bfd_put_32 (output_bfd, h->root.u.def.value - TP_OFFSET,
				  sgot->contents + off);
		  }
	      }
	  }
	break;

	  /* DON'T   fall through.  */

	default:
	  /* OLD_NDS32_RELOC.  */

	  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
					contents, offset, relocation, addend);
	  goto check_reloc;
	}

      switch ((int) r_type)
	{
	case R_NDS32_20_RELA:
	case R_NDS32_5_RELA:
	case R_NDS32_9_PCREL_RELA:
	case R_NDS32_WORD_9_PCREL_RELA:
	case R_NDS32_10_UPCREL_RELA:
	case R_NDS32_15_PCREL_RELA:
	case R_NDS32_17_PCREL_RELA:
	case R_NDS32_25_PCREL_RELA:
	case R_NDS32_25_ABS_RELA:
	case R_NDS32_HI20_RELA:
	case R_NDS32_LO12S3_RELA:
	case R_NDS32_LO12S2_RELA:
	case R_NDS32_LO12S2_DP_RELA:
	case R_NDS32_LO12S2_SP_RELA:
	case R_NDS32_LO12S1_RELA:
	case R_NDS32_LO12S0_RELA:
	case R_NDS32_LO12S0_ORI_RELA:
	case R_NDS32_SDA16S3_RELA:
	case R_NDS32_SDA17S2_RELA:
	case R_NDS32_SDA18S1_RELA:
	case R_NDS32_SDA19S0_RELA:
	case R_NDS32_SDA15S3_RELA:
	case R_NDS32_SDA15S2_RELA:
	case R_NDS32_SDA12S2_DP_RELA:
	case R_NDS32_SDA12S2_SP_RELA:
	case R_NDS32_SDA15S1_RELA:
	case R_NDS32_SDA15S0_RELA:
	case R_NDS32_SDA_FP7U2_RELA:
	case R_NDS32_9_PLTREL:
	case R_NDS32_25_PLTREL:
	case R_NDS32_GOT20:
	case R_NDS32_GOT_HI20:
	case R_NDS32_GOT_LO12:
	case R_NDS32_GOT_LO15:
	case R_NDS32_GOT_LO19:
	case R_NDS32_GOT15S2_RELA:
	case R_NDS32_GOT17S2_RELA:
	case R_NDS32_GOTPC20:
	case R_NDS32_GOTPC_HI20:
	case R_NDS32_GOTPC_LO12:
	case R_NDS32_GOTOFF:
	case R_NDS32_GOTOFF_HI20:
	case R_NDS32_GOTOFF_LO12:
	case R_NDS32_GOTOFF_LO15:
	case R_NDS32_GOTOFF_LO19:
	case R_NDS32_PLTREL_HI20:
	case R_NDS32_PLTREL_LO12:
	case R_NDS32_PLT_GOTREL_HI20:
	case R_NDS32_PLT_GOTREL_LO12:
	case R_NDS32_PLT_GOTREL_LO15:
	case R_NDS32_PLT_GOTREL_LO19:
	case R_NDS32_PLT_GOTREL_LO20:
	case R_NDS32_17IFC_PCREL_RELA:
	case R_NDS32_10IFCU_PCREL_RELA:
	case R_NDS32_TLS_LE_HI20:
	case R_NDS32_TLS_LE_LO12:
	case R_NDS32_TLS_IE_HI20:
	case R_NDS32_TLS_IE_LO12S2:
	case R_NDS32_TLS_LE_20:
	case R_NDS32_TLS_LE_15S0:
	case R_NDS32_TLS_LE_15S1:
	case R_NDS32_TLS_LE_15S2:
	  /* Instruction related relocs must handle endian properly.  */
	  /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER.  */
	  r = nds32_elf_final_link_relocate (howto, input_bfd,
					     input_section, contents,
					     rel->r_offset, relocation,
					     rel->r_addend);
	  break;

	default:
	  /* All other relocs can use default handler.  */
	  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
					contents, rel->r_offset,
					relocation, rel->r_addend);
	  break;
	}

check_reloc:

      if (r != bfd_reloc_ok)
	{
	  /* FIXME: This should be generic enough to go in a utility.  */
	  const char *name;

	  if (h != NULL)
	    name = h->root.root.string;
	  else
	    {
	      name = bfd_elf_string_from_elf_section
		      (input_bfd, symtab_hdr->sh_link, sym->st_name);
	      if (name == NULL || *name == '\0')
		name = bfd_section_name (input_bfd, sec);
	    }

	  if (errmsg != NULL)
	    goto common_error;

	  switch (r)
	    {
	    case bfd_reloc_overflow:
	      if (!((*info->callbacks->reloc_overflow)
		    (info, (h ? &h->root : NULL), name, howto->name,
		     (bfd_vma) 0, input_bfd, input_section, offset)))
		return FALSE;
	      break;

	    case bfd_reloc_undefined:
	      if (!((*info->callbacks->undefined_symbol)
		    (info, name, input_bfd, input_section, offset, TRUE)))
		return FALSE;
	      break;

	    case bfd_reloc_outofrange:
	      errmsg = _("internal error: out of range error");
	      goto common_error;

	    case bfd_reloc_notsupported:
	      errmsg = _("internal error: unsupported relocation error");
	      goto common_error;

	    case bfd_reloc_dangerous:
	      errmsg = _("internal error: dangerous error");
	      goto common_error;

	    default:
	      errmsg = _("internal error: unknown error");
	      /* Fall through.  */

common_error:
	      if (!((*info->callbacks->warning)
		    (info, errmsg, name, input_bfd, input_section, offset)))
		return FALSE;
	      break;
	    }
	}
    }

  return ret;
}

/* Finish up dynamic symbol handling.  We set the contents of various
   dynamic sections here.  */

static bfd_boolean
nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
				 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
{
  struct elf_nds32_link_hash_table *htab;
  bfd_byte *loc;

  htab = nds32_elf_hash_table (info);

  if (h->plt.offset != (bfd_vma) - 1)
    {
      asection *splt;
      asection *sgot;
      asection *srela;

      bfd_vma plt_index;
      bfd_vma got_offset;
      bfd_vma local_plt_offset;
      Elf_Internal_Rela rela;

      /* This symbol has an entry in the procedure linkage table.  Set
	 it up.  */

      BFD_ASSERT (h->dynindx != -1);

      splt = htab->splt;
      sgot = htab->sgotplt;
      srela = htab->srelplt;
      BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);

      /* Get the index in the procedure linkage table which
	 corresponds to this symbol.  This is the index of this symbol
	 in all the symbols for which we are making plt entries.  The
	 first entry in the procedure linkage table is reserved.  */
      plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;

      /* Get the offset into the .got table of the entry that
	 corresponds to this function.  Each .got entry is 4 bytes.
	 The first three are reserved.  */
      got_offset = (plt_index + 3) * 4;

      /* Fill in the entry in the procedure linkage table.  */
      if (!bfd_link_pic (info))
	{
	  unsigned long insn;

	  insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
				      + sgot->output_offset + got_offset) >> 12)
				    & 0xfffff);
	  bfd_putb32 (insn, splt->contents + h->plt.offset);

	  insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
				      + sgot->output_offset + got_offset) & 0x0fff)
				    >> 2);
	  bfd_putb32 (insn, splt->contents + h->plt.offset + 4);

	  insn = PLT_ENTRY_WORD2;
	  bfd_putb32 (insn, splt->contents + h->plt.offset + 8);

	  insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
	  bfd_putb32 (insn, splt->contents + h->plt.offset + 12);

	  insn = PLT_ENTRY_WORD4
		 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
	  bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
	  local_plt_offset = 12;
	}
      else
	{
	  /* sda_base must be set at this time.  */
	  unsigned long insn;
	  long offset;

	  /* FIXME, sda_base is 65536, it will damage opcode.  */
	  /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
	  offset = sgot->output_section->vma + sgot->output_offset + got_offset
		   - elf_gp (output_bfd);
	  insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
	  bfd_putb32 (insn, splt->contents + h->plt.offset);

	  insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
	  bfd_putb32 (insn, splt->contents + h->plt.offset + 4);

	  insn = PLT_PIC_ENTRY_WORD2;
	  bfd_putb32 (insn, splt->contents + h->plt.offset + 8);

	  insn = PLT_PIC_ENTRY_WORD3;
	  bfd_putb32 (insn, splt->contents + h->plt.offset + 12);

	  insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
	  bfd_putb32 (insn, splt->contents + h->plt.offset + 16);

	  insn = PLT_PIC_ENTRY_WORD5
	    + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
	  bfd_putb32 (insn, splt->contents + h->plt.offset + 20);

	  local_plt_offset = 16;
	}

      /* Fill in the entry in the global offset table,
	 so it will fall through to the next instruction for the first time.  */
      bfd_put_32 (output_bfd,
		  (splt->output_section->vma + splt->output_offset
		   + h->plt.offset + local_plt_offset),
		  sgot->contents + got_offset);

      /* Fill in the entry in the .rela.plt section.  */
      rela.r_offset = (sgot->output_section->vma
		       + sgot->output_offset + got_offset);
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
      rela.r_addend = 0;
      loc = srela->contents;
      loc += plt_index * sizeof (Elf32_External_Rela);
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);

      if (!h->def_regular)
	{
	  /* Mark the symbol as undefined, rather than as defined in
	     the .plt section.  Leave the value alone.  */
	  sym->st_shndx = SHN_UNDEF;
	  if (!h->ref_regular_nonweak)
	    sym->st_value = 0;
	}
    }

  if (h->got.offset != (bfd_vma) - 1)
    {
      asection *sgot;
      asection *srela;
      Elf_Internal_Rela rela;

      /* This symbol has an entry in the global offset table.
	 Set it up.  */

      sgot = htab->sgot;
      srela = htab->srelgot;
      BFD_ASSERT (sgot != NULL && srela != NULL);

      rela.r_offset = (sgot->output_section->vma
		       + sgot->output_offset + (h->got.offset & ~1));

      /* If this is a -Bsymbolic link, and the symbol is defined
	 locally, we just want to emit a RELATIVE reloc.  Likewise if
	 the symbol was forced to be local because of a version file.
	 The entry in the global offset table will already have been
	 initialized in the relocate_section function.  */
      if (bfd_link_pic (info)
	  && (info->symbolic
	      || h->dynindx == -1 || h->forced_local) && h->def_regular)
	{
	  rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
	  rela.r_addend = (h->root.u.def.value
			   + h->root.u.def.section->output_section->vma
			   + h->root.u.def.section->output_offset);
	}
      else
	{
	  BFD_ASSERT ((h->got.offset & 1) == 0);
	  bfd_put_32 (output_bfd, (bfd_vma) 0,
		      sgot->contents + h->got.offset);
	  rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
	  rela.r_addend = 0;
	}

      loc = srela->contents;
      loc += srela->reloc_count * sizeof (Elf32_External_Rela);
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
      ++srela->reloc_count;
    }

  if (h->needs_copy)
    {
      asection *s;
      Elf_Internal_Rela rela;

      /* This symbols needs a copy reloc.  Set it up.  */

      BFD_ASSERT (h->dynindx != -1
		  && (h->root.type == bfd_link_hash_defined
		      || h->root.type == bfd_link_hash_defweak));

      s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
      BFD_ASSERT (s != NULL);

      rela.r_offset = (h->root.u.def.value
		       + h->root.u.def.section->output_section->vma
		       + h->root.u.def.section->output_offset);
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
      rela.r_addend = 0;
      loc = s->contents;
      loc += s->reloc_count * sizeof (Elf32_External_Rela);
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
      ++s->reloc_count;
    }

  /* Mark some specially defined symbols as absolute.  */
  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
    sym->st_shndx = SHN_ABS;

  return TRUE;
}


/* Finish up the dynamic sections.  */

static bfd_boolean
nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
{
  struct elf_nds32_link_hash_table *htab;
  bfd *dynobj;
  asection *sdyn;
  asection *sgot;

  htab = nds32_elf_hash_table (info);
  dynobj = htab->root.dynobj;

  sgot = htab->sgotplt;
  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");

  if (htab->root.dynamic_sections_created)
    {
      asection *splt;
      Elf32_External_Dyn *dyncon, *dynconend;

      BFD_ASSERT (sgot != NULL && sdyn != NULL);

      dyncon = (Elf32_External_Dyn *) sdyn->contents;
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);

      for (; dyncon < dynconend; dyncon++)
	{
	  Elf_Internal_Dyn dyn;
	  asection *s;

	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);

	  switch (dyn.d_tag)
	    {
	    default:
	      break;

	    case DT_PLTGOT:
	      /* name = ".got"; */
	      s = htab->sgot->output_section;
	      goto get_vma;
	    case DT_JMPREL:
	      s = htab->srelplt->output_section;
	    get_vma:
	      BFD_ASSERT (s != NULL);
	      dyn.d_un.d_ptr = s->vma;
	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
	      break;

	    case DT_PLTRELSZ:
	      s = htab->srelplt->output_section;
	      BFD_ASSERT (s != NULL);
	      dyn.d_un.d_val = s->size;
	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
	      break;

	    case DT_RELASZ:
	      /* My reading of the SVR4 ABI indicates that the
		 procedure linkage table relocs (DT_JMPREL) should be
		 included in the overall relocs (DT_RELA).  This is
		 what Solaris does.  However, UnixWare can not handle
		 that case.  Therefore, we override the DT_RELASZ entry
		 here to make it not include the JMPREL relocs.  Since
		 the linker script arranges for .rela.plt to follow all
		 other relocation sections, we don't have to worry
		 about changing the DT_RELA entry.  */
	      if (htab->srelplt != NULL)
		{
		  s = htab->srelplt->output_section;
		  dyn.d_un.d_val -= s->size;
		}
	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
	      break;
	    }
	}

      /* Fill in the first entry in the procedure linkage table.  */
      splt = htab->splt;
      if (splt && splt->size > 0)
	{
	  if (bfd_link_pic (info))
	    {
	      unsigned long insn;
	      long offset;

	      /* FIXME, sda_base is 65536, it will damage opcode.  */
	      /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
	      offset = sgot->output_section->vma + sgot->output_offset + 4
		       - elf_gp (output_bfd);
	      insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
	      bfd_putb32 (insn, splt->contents);

	      /* insn = PLT0_PIC_ENTRY_WORD0 | (((8 - sda_base) >> 2) & 0x7fff) ; */
	      /* here has a typo?  */
	      insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
	      bfd_putb32 (insn, splt->contents + 4);

	      insn = PLT0_PIC_ENTRY_WORD2;
	      bfd_putb32 (insn, splt->contents + 8);

	      insn = PLT0_PIC_ENTRY_WORD3;
	      bfd_putb32 (insn, splt->contents + 12);

	      insn = PLT0_PIC_ENTRY_WORD4;
	      bfd_putb32 (insn, splt->contents + 16);

	      insn = PLT0_PIC_ENTRY_WORD5;
	      bfd_putb32 (insn, splt->contents + 20);
	    }
	  else
	    {
	      unsigned long insn;
	      unsigned long addr;

	      /* addr = .got + 4 */
	      addr = sgot->output_section->vma + sgot->output_offset + 4;
	      insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
	      bfd_putb32 (insn, splt->contents);

	      insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
	      bfd_putb32 (insn, splt->contents + 4);

	      insn = PLT0_ENTRY_WORD2;
	      bfd_putb32 (insn, splt->contents + 8);

	      insn = PLT0_ENTRY_WORD3;
	      bfd_putb32 (insn, splt->contents + 12);

	      insn = PLT0_ENTRY_WORD4;
	      bfd_putb32 (insn, splt->contents + 16);
	    }

	  elf_section_data (splt->output_section)->this_hdr.sh_entsize =
	    PLT_ENTRY_SIZE;
	}
    }

  /* Fill in the first three entries in the global offset table.  */
  if (sgot && sgot->size > 0)
    {
      if (sdyn == NULL)
	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
      else
	bfd_put_32 (output_bfd,
		    sdyn->output_section->vma + sdyn->output_offset,
		    sgot->contents);
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);

      elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
    }

  return TRUE;
}


/* Set the right machine number.  */

static bfd_boolean
nds32_elf_object_p (bfd *abfd)
{
  static unsigned int cur_arch = 0;

  if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
    {
      /* E_N1_ARCH is a wild card, so it is set only when no others exist.  */
      cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
    }

  switch (cur_arch)
    {
    default:
    case E_N1_ARCH:
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
      break;
    case E_N1H_ARCH:
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
      break;
    case E_NDS_ARCH_STAR_V2_0:
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
      break;
    case E_NDS_ARCH_STAR_V3_0:
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
      break;
    case E_NDS_ARCH_STAR_V3_M:
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
      break;
    }

  return TRUE;
}

/* Store the machine number in the flags field.  */

static void
nds32_elf_final_write_processing (bfd *abfd,
				  bfd_boolean linker ATTRIBUTE_UNUSED)
{
  unsigned long val;
  static unsigned int cur_mach = 0;

  if (bfd_mach_n1 != bfd_get_mach (abfd))
    {
      cur_mach = bfd_get_mach (abfd);
    }

  switch (cur_mach)
    {
    case bfd_mach_n1:
      /* Only happen when object is empty, since the case is abandon.  */
      val = E_N1_ARCH;
      val |= E_NDS_ABI_AABI;
      val |= E_NDS32_ELF_VER_1_4;
      break;
    case bfd_mach_n1h:
      val = E_N1H_ARCH;
      break;
    case bfd_mach_n1h_v2:
      val = E_NDS_ARCH_STAR_V2_0;
      break;
    case bfd_mach_n1h_v3:
      val = E_NDS_ARCH_STAR_V3_0;
      break;
    case bfd_mach_n1h_v3m:
      val = E_NDS_ARCH_STAR_V3_M;
      break;
    default:
      val = 0;
      break;
    }

  elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
  elf_elfheader (abfd)->e_flags |= val;
}

/* Function to keep NDS32 specific file flags.  */

static bfd_boolean
nds32_elf_set_private_flags (bfd *abfd, flagword flags)
{
  BFD_ASSERT (!elf_flags_init (abfd)
	      || elf_elfheader (abfd)->e_flags == flags);

  elf_elfheader (abfd)->e_flags = flags;
  elf_flags_init (abfd) = TRUE;
  return TRUE;
}

static unsigned int
convert_e_flags (unsigned int e_flags, unsigned int arch)
{
  if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
    {
      /* From 0.9 to 1.0.  */
      e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;

      /* Invert E_NDS32_HAS_NO_MAC_INST.  */
      e_flags ^= E_NDS32_HAS_NO_MAC_INST;
      if (arch == E_NDS_ARCH_STAR_V1_0)
	{
	  /* Done.  */
	  return e_flags;
	}
    }

  /* From 1.0 to 2.0.  */
  e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;

  /* Clear E_NDS32_HAS_MFUSR_PC_INST.  */
  e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;

  /* Invert E_NDS32_HAS_NO_MAC_INST.  */
  e_flags ^= E_NDS32_HAS_NO_MAC_INST;
  return e_flags;
}

static bfd_boolean
nds32_check_vec_size (bfd *ibfd)
{
  static unsigned int nds32_vec_size = 0;

  asection *sec_t = NULL;
  bfd_byte *contents = NULL;

  sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");

  if (sec_t && sec_t->size >= 4)
    {
      /* Get vec_size in file.  */
      unsigned int flag_t;

      nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
      flag_t = bfd_get_32 (ibfd, contents);

      /* The value could only be 4 or 16.  */

      if (!nds32_vec_size)
	/* Set if not set yet.  */
	nds32_vec_size = (flag_t & 0x3);
      else if (nds32_vec_size != (flag_t & 0x3))
	{
	  (*_bfd_error_handler) (_("%B: ISR vector size mismatch"
				   " with previous modules, previous %u-byte, current %u-byte"),
				 ibfd,
				 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
				 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
	  return FALSE;
	}
      else
	/* Only keep the first vec_size section.  */
	sec_t->flags |= SEC_EXCLUDE;
    }

  return TRUE;
}

/* Merge backend specific data from an object file to the output
   object file when linking.  */

static bfd_boolean
nds32_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
{
  flagword out_flags;
  flagword in_flags;
  flagword out_16regs;
  flagword in_no_mac;
  flagword out_no_mac;
  flagword in_16regs;
  flagword out_version;
  flagword in_version;
  flagword out_fpu_config;
  flagword in_fpu_config;

  /* TODO: Revise to use object-attributes instead.  */
  if (!nds32_check_vec_size (ibfd))
    return FALSE;

  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
    return TRUE;

  if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
    {
      (*_bfd_error_handler)
	(_("%B: warning: Endian mismatch with previous modules."), ibfd);

      bfd_set_error (bfd_error_bad_value);
      return FALSE;
    }

  in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
  if (in_version == E_NDS32_ELF_VER_1_2)
    {
      (*_bfd_error_handler)
	(_("%B: warning: Older version of object file encountered, "
	   "Please recompile with current tool chain."), ibfd);
    }

  /* We may need to merge V1 and V2 arch object files to V2.  */
  if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
      != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
    {
      /* Need to convert version.  */
      if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
	  == E_NDS_ARCH_STAR_RESERVED)
	{
	  elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
	}
      else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9
	       || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
		  > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
	{
	  elf_elfheader (obfd)->e_flags =
	    convert_e_flags (elf_elfheader (obfd)->e_flags,
			     (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
	}
      else
	{
	  elf_elfheader (ibfd)->e_flags =
	    convert_e_flags (elf_elfheader (ibfd)->e_flags,
			     (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
	}
    }

  /* Extract some flags.  */
  in_flags = elf_elfheader (ibfd)->e_flags
	     & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
		  | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));

  /* The following flags need special treatment.  */
  in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
  in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
  in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;

  /* Extract some flags.  */
  out_flags = elf_elfheader (obfd)->e_flags
	      & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
		   | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));

  /* The following flags need special treatment.  */
  out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
  out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
  out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
  out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
  if (!elf_flags_init (obfd))
    {
      /* If the input is the default architecture then do not
	 bother setting the flags for the output architecture,
	 instead allow future merges to do this.  If no future
	 merges ever set these flags then they will retain their
	 unitialised values, which surprise surprise, correspond
	 to the default values.  */
      if (bfd_get_arch_info (ibfd)->the_default)
	return TRUE;

      elf_flags_init (obfd) = TRUE;
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;

      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
	  && bfd_get_arch_info (obfd)->the_default)
	{
	  return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
				    bfd_get_mach (ibfd));
	}

      return TRUE;
    }

  /* Check flag compatibility.  */
  if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
    {
      (*_bfd_error_handler)
	(_("%B: error: ABI mismatch with previous modules."), ibfd);

      bfd_set_error (bfd_error_bad_value);
      return FALSE;
    }

  if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
    {
      if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
	{
	  (*_bfd_error_handler)
	    (_("%B: error: Instruction set mismatch with previous modules."), ibfd);

	  bfd_set_error (bfd_error_bad_value);
	  return FALSE;
	}
    }

  /* When linking with V1.2 and V1.3 objects together the output is V1.2.
     and perf ext1 and DIV are mergerd to perf ext1.  */
  if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
    {
      elf_elfheader (obfd)->e_flags =
	(in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
	| (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
	| (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
	   ?  E_NDS32_HAS_EXT_INST : 0)
	| (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
	   ?  E_NDS32_HAS_EXT_INST : 0)
	| (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
	| ((in_version > out_version) ? out_version : in_version);
    }
  else
    {
      if (in_version != out_version)
	(*_bfd_error_handler) (_("%B: warning: Incompatible elf-versions %s and  %s."),
				 ibfd, nds32_elfver_strtab[out_version],
				 nds32_elfver_strtab[in_version]);

      elf_elfheader (obfd)->e_flags = in_flags | out_flags
	| (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
	| (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
	| (in_version > out_version ?  out_version : in_version);
    }

  return TRUE;
}

/* Display the flags field.  */

static bfd_boolean
nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
{
  FILE *file = (FILE *) ptr;

  BFD_ASSERT (abfd != NULL && ptr != NULL);

  _bfd_elf_print_private_bfd_data (abfd, ptr);

  fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);

  switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
    {
    default:
    case E_N1_ARCH:
      fprintf (file, _(": n1 instructions"));
      break;
    case E_N1H_ARCH:
      fprintf (file, _(": n1h instructions"));
      break;
    }

  fputc ('\n', file);

  return TRUE;
}

static unsigned int
nds32_elf_action_discarded (asection *sec)
{

  if (strncmp
      (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
    return 0;

  return _bfd_elf_default_action_discarded (sec);
}

static asection *
nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
			Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
			Elf_Internal_Sym *sym)
{
  if (h != NULL)
    switch (ELF32_R_TYPE (rel->r_info))
      {
      case R_NDS32_GNU_VTINHERIT:
      case R_NDS32_GNU_VTENTRY:
      case R_NDS32_RELA_GNU_VTINHERIT:
      case R_NDS32_RELA_GNU_VTENTRY:
	return NULL;
      }

  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
}

static bfd_boolean
nds32_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info, asection *sec,
			 const Elf_Internal_Rela *relocs)
{
  /* Update the got entry reference counts for the section being removed.  */
  Elf_Internal_Shdr *symtab_hdr;
  struct elf_link_hash_entry **sym_hashes;
  bfd_signed_vma *local_got_refcounts;
  const Elf_Internal_Rela *rel, *relend;

  elf_section_data (sec)->local_dynrel = NULL;

  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  sym_hashes = elf_sym_hashes (abfd);
  local_got_refcounts = elf_local_got_refcounts (abfd);

  relend = relocs + sec->reloc_count;
  for (rel = relocs; rel < relend; rel++)
    {
      unsigned long r_symndx;
      struct elf_link_hash_entry *h = NULL;

      r_symndx = ELF32_R_SYM (rel->r_info);
      if (r_symndx >= symtab_hdr->sh_info)
	{
	  /* External symbol.  */
	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
	  while (h->root.type == bfd_link_hash_indirect
		 || h->root.type == bfd_link_hash_warning)
	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
	}

      switch (ELF32_R_TYPE (rel->r_info))
	{
	case R_NDS32_GOT_HI20:
	case R_NDS32_GOT_LO12:
	case R_NDS32_GOT_LO15:
	case R_NDS32_GOT_LO19:
	case R_NDS32_GOT17S2_RELA:
	case R_NDS32_GOT15S2_RELA:
	case R_NDS32_GOTOFF:
	case R_NDS32_GOTOFF_HI20:
	case R_NDS32_GOTOFF_LO12:
	case R_NDS32_GOTOFF_LO15:
	case R_NDS32_GOTOFF_LO19:
	case R_NDS32_GOT20:
	case R_NDS32_GOTPC_HI20:
	case R_NDS32_GOTPC_LO12:
	case R_NDS32_GOTPC20:
	  if (h != NULL)
	    {
	      if (h->got.refcount > 0)
		h->got.refcount--;
	    }
	  else
	    {
	      if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
		local_got_refcounts[r_symndx]--;
	    }
	  break;

	case R_NDS32_16_RELA:
	case R_NDS32_20_RELA:
	case R_NDS32_5_RELA:
	case R_NDS32_32_RELA:
	case R_NDS32_HI20_RELA:
	case R_NDS32_LO12S3_RELA:
	case R_NDS32_LO12S2_RELA:
	case R_NDS32_LO12S2_DP_RELA:
	case R_NDS32_LO12S2_SP_RELA:
	case R_NDS32_LO12S1_RELA:
	case R_NDS32_LO12S0_RELA:
	case R_NDS32_LO12S0_ORI_RELA:
	case R_NDS32_SDA16S3_RELA:
	case R_NDS32_SDA17S2_RELA:
	case R_NDS32_SDA18S1_RELA:
	case R_NDS32_SDA19S0_RELA:
	case R_NDS32_SDA15S3_RELA:
	case R_NDS32_SDA15S2_RELA:
	case R_NDS32_SDA12S2_DP_RELA:
	case R_NDS32_SDA12S2_SP_RELA:
	case R_NDS32_SDA15S1_RELA:
	case R_NDS32_SDA15S0_RELA:
	case R_NDS32_SDA_FP7U2_RELA:
	case R_NDS32_15_PCREL_RELA:
	case R_NDS32_17_PCREL_RELA:
	case R_NDS32_25_PCREL_RELA:
	  if (h != NULL)
	    {
	      struct elf_nds32_link_hash_entry *eh;
	      struct elf_nds32_dyn_relocs **pp;
	      struct elf_nds32_dyn_relocs *p;

	      if (!bfd_link_pic (info) && h->plt.refcount > 0)
		h->plt.refcount -= 1;

	      eh = (struct elf_nds32_link_hash_entry *) h;

	      for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
		if (p->sec == sec)
		  {
		    if (ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
			|| ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
			|| ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA)
		      p->pc_count -= 1;
		    p->count -= 1;
		    if (p->count == 0)
		      *pp = p->next;
		    break;
		  }
	    }
	  break;

	case R_NDS32_9_PLTREL:
	case R_NDS32_25_PLTREL:
	  if (h != NULL)
	    {
	      if (h->plt.refcount > 0)
		h->plt.refcount--;
	    }
	  break;

	default:
	  break;
	}
    }

  return TRUE;
}

/* Look through the relocs for a section during the first phase.
   Since we don't do .gots or .plts, we just need to consider the
   virtual table relocs for gc.  */

static bfd_boolean
nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
			asection *sec, const Elf_Internal_Rela *relocs)
{
  Elf_Internal_Shdr *symtab_hdr;
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
  const Elf_Internal_Rela *rel;
  const Elf_Internal_Rela *rel_end;
  struct elf_nds32_link_hash_table *htab;
  bfd *dynobj;
  asection *sreloc = NULL;

  if (bfd_link_relocatable (info))
    return TRUE;

  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  sym_hashes = elf_sym_hashes (abfd);
  sym_hashes_end =
    sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
  if (!elf_bad_symtab (abfd))
    sym_hashes_end -= symtab_hdr->sh_info;

  htab = nds32_elf_hash_table (info);
  dynobj = htab->root.dynobj;

  rel_end = relocs + sec->reloc_count;
  for (rel = relocs; rel < rel_end; rel++)
    {
      enum elf_nds32_reloc_type r_type;
      struct elf_link_hash_entry *h;
      unsigned long r_symndx;
      int tls_type, old_tls_type;

      r_symndx = ELF32_R_SYM (rel->r_info);
      r_type = ELF32_R_TYPE (rel->r_info);
      if (r_symndx < symtab_hdr->sh_info)
	h = NULL;
      else
	{
	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
	  while (h->root.type == bfd_link_hash_indirect
		 || h->root.type == bfd_link_hash_warning)
	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
	}

      /* Some relocs require a global offset table.  We create
	 got section here, since these relocation need got section
	 and it is not created yet.  */
      if (htab->sgot == NULL)
	{
	  switch (r_type)
	    {
	    case R_NDS32_GOT_HI20:
	    case R_NDS32_GOT_LO12:
	    case R_NDS32_GOT_LO15:
	    case R_NDS32_GOT_LO19:
	    case R_NDS32_GOT17S2_RELA:
	    case R_NDS32_GOT15S2_RELA:
	    case R_NDS32_GOTOFF:
	    case R_NDS32_GOTOFF_HI20:
	    case R_NDS32_GOTOFF_LO12:
	    case R_NDS32_GOTOFF_LO15:
	    case R_NDS32_GOTOFF_LO19:
	    case R_NDS32_GOTPC20:
	    case R_NDS32_GOTPC_HI20:
	    case R_NDS32_GOTPC_LO12:
	    case R_NDS32_GOT20:
	    case R_NDS32_TLS_IE_HI20:
	    case R_NDS32_TLS_IE_LO12S2:
	      if (dynobj == NULL)
		htab->root.dynobj = dynobj = abfd;
	      if (!create_got_section (dynobj, info))
		return FALSE;
	      break;

	    default:
	      break;
	    }
	}

      switch ((int) r_type)
	{
	case R_NDS32_GOT_HI20:
	case R_NDS32_GOT_LO12:
	case R_NDS32_GOT_LO15:
	case R_NDS32_GOT_LO19:
	case R_NDS32_GOT20:
	case R_NDS32_TLS_IE_HI20:
	case R_NDS32_TLS_IE_LO12S2:
	  switch (r_type)
	    {
	    case R_NDS32_TLS_IE_HI20:
	    case R_NDS32_TLS_IE_LO12S2:
	      tls_type = GOT_TLS_IE;
	      break;
	    default:
	      tls_type = GOT_NORMAL;
	      break;
	    }
	  if (h != NULL)
	    {
	      old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
	      h->got.refcount += 1;
	    }
	  else
	    {
	      bfd_signed_vma *local_got_refcounts;

	      /* This is a global offset table entry for a local
		 symbol.  */
	      local_got_refcounts = elf_local_got_refcounts (abfd);
	      if (local_got_refcounts == NULL)
		{
		  bfd_size_type size;

		  size = symtab_hdr->sh_info;
		  size *= sizeof (bfd_signed_vma);
		  local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
		  if (local_got_refcounts == NULL)
		    return FALSE;
		  elf_local_got_refcounts (abfd) = local_got_refcounts;
		}
	      local_got_refcounts[r_symndx] += 1;
	      old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
	    }

	  /* We will already have issued an error message if there
	     is a TLS/non-TLS mismatch, based on the symbol
	     type.  So just combine any TLS types needed.  */
	  if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
	      && tls_type != GOT_NORMAL)
	    tls_type |= old_tls_type;

	  if (old_tls_type != tls_type)
	    {
	      if (h != NULL)
		elf32_nds32_hash_entry (h)->tls_type = tls_type;
	      else
		elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
	    }
	  break;
	case R_NDS32_9_PLTREL:
	case R_NDS32_25_PLTREL:
	case R_NDS32_PLTREL_HI20:
	case R_NDS32_PLTREL_LO12:
	case R_NDS32_PLT_GOTREL_HI20:
	case R_NDS32_PLT_GOTREL_LO12:
	case R_NDS32_PLT_GOTREL_LO15:
	case R_NDS32_PLT_GOTREL_LO19:
	case R_NDS32_PLT_GOTREL_LO20:

	  /* This symbol requires a procedure linkage table entry.  We
	     actually build the entry in adjust_dynamic_symbol,
	     because this might be a case of linking PIC code without
	     linking in any dynamic objects, in which case we don't
	     need to generate a procedure linkage table after all.  */

	  /* If this is a local symbol, we resolve it directly without
	     creating a procedure linkage table entry.  */
	  if (h == NULL)
	    continue;

	  if (h->forced_local)
	    break;

	  elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
	  h->needs_plt = 1;
	  h->plt.refcount += 1;
	  break;

	case R_NDS32_16_RELA:
	case R_NDS32_20_RELA:
	case R_NDS32_5_RELA:
	case R_NDS32_32_RELA:
	case R_NDS32_HI20_RELA:
	case R_NDS32_LO12S3_RELA:
	case R_NDS32_LO12S2_RELA:
	case R_NDS32_LO12S2_DP_RELA:
	case R_NDS32_LO12S2_SP_RELA:
	case R_NDS32_LO12S1_RELA:
	case R_NDS32_LO12S0_RELA:
	case R_NDS32_LO12S0_ORI_RELA:
	case R_NDS32_SDA16S3_RELA:
	case R_NDS32_SDA17S2_RELA:
	case R_NDS32_SDA18S1_RELA:
	case R_NDS32_SDA19S0_RELA:
	case R_NDS32_SDA15S3_RELA:
	case R_NDS32_SDA15S2_RELA:
	case R_NDS32_SDA12S2_DP_RELA:
	case R_NDS32_SDA12S2_SP_RELA:
	case R_NDS32_SDA15S1_RELA:
	case R_NDS32_SDA15S0_RELA:
	case R_NDS32_SDA_FP7U2_RELA:
	case R_NDS32_15_PCREL_RELA:
	case R_NDS32_17_PCREL_RELA:
	case R_NDS32_25_PCREL_RELA:

	  if (h != NULL && !bfd_link_pic (info))
	    {
	      h->non_got_ref = 1;
	      h->plt.refcount += 1;
	    }

	  /* If we are creating a shared library, and this is a reloc against
	     a global symbol, or a non PC relative reloc against a local
	     symbol, then we need to copy the reloc into the shared library.
	     However, if we are linking with -Bsymbolic, we do not need to
	     copy a reloc against a global symbol which is defined in an
	     object we are including in the link (i.e., DEF_REGULAR is set).
	     At this point we have not seen all the input files, so it is
	     possible that DEF_REGULAR is not set now but will be set later
	     (it is never cleared).  We account for that possibility below by
	     storing information in the dyn_relocs field of the hash table
	     entry.  A similar situation occurs when creating shared libraries
	     and symbol visibility changes render the symbol local.

	     If on the other hand, we are creating an executable, we may need
	     to keep relocations for symbols satisfied by a dynamic library
	     if we manage to avoid copy relocs for the symbol.  */
	  if ((bfd_link_pic (info)
	       && (sec->flags & SEC_ALLOC) != 0
	       && ((r_type != R_NDS32_25_PCREL_RELA
		    && r_type != R_NDS32_15_PCREL_RELA
		    && r_type != R_NDS32_17_PCREL_RELA
		    && !(r_type == R_NDS32_32_RELA
			 && strcmp (sec->name, ".eh_frame") == 0))
		   || (h != NULL
		       && (!info->symbolic
			   || h->root.type == bfd_link_hash_defweak
			   || !h->def_regular))))
	      || (!bfd_link_pic (info)
		  && (sec->flags & SEC_ALLOC) != 0
		  && h != NULL
		  && (h->root.type == bfd_link_hash_defweak
		      || !h->def_regular)))
	    {
	      struct elf_nds32_dyn_relocs *p;
	      struct elf_nds32_dyn_relocs **head;

	      if (dynobj == NULL)
		htab->root.dynobj = dynobj = abfd;

	      /* When creating a shared object, we must copy these
		 relocs into the output file.  We create a reloc
		 section in dynobj and make room for the reloc.  */
	      if (sreloc == NULL)
		{
		  const char *name;

		  name = bfd_elf_string_from_elf_section
		    (abfd, elf_elfheader (abfd)->e_shstrndx,
		     elf_section_data (sec)->rela.hdr->sh_name);
		  if (name == NULL)
		    return FALSE;

		  BFD_ASSERT (strncmp (name, ".rela", 5) == 0
			      && strcmp (bfd_get_section_name (abfd, sec),
					 name + 5) == 0);

		  sreloc = bfd_get_section_by_name (dynobj, name);
		  if (sreloc == NULL)
		    {
		      flagword flags;

		      sreloc = bfd_make_section (dynobj, name);
		      flags = (SEC_HAS_CONTENTS | SEC_READONLY
			       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
		      if ((sec->flags & SEC_ALLOC) != 0)
			flags |= SEC_ALLOC | SEC_LOAD;
		      if (sreloc == NULL
			  || !bfd_set_section_flags (dynobj, sreloc, flags)
			  || !bfd_set_section_alignment (dynobj, sreloc, 2))
			return FALSE;

		      elf_section_type (sreloc) = SHT_RELA;
		    }
		  elf_section_data (sec)->sreloc = sreloc;
		}

	      /* If this is a global symbol, we count the number of
		 relocations we need for this symbol.  */
	      if (h != NULL)
		head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
	      else
		{
		  asection *s;

		  Elf_Internal_Sym *isym;
		  isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
		  if (isym == NULL)
		    return FALSE;

		  /* Track dynamic relocs needed for local syms too.  */
		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
		  if (s == NULL)
		    return FALSE;

		  head = ((struct elf_nds32_dyn_relocs **)
			&elf_section_data (s)->local_dynrel);
		}

	      p = *head;
	      if (p == NULL || p->sec != sec)
		{
		  bfd_size_type amt = sizeof (*p);
		  p = (struct elf_nds32_dyn_relocs *) bfd_alloc (dynobj, amt);
		  if (p == NULL)
		    return FALSE;
		  p->next = *head;
		  *head = p;
		  p->sec = sec;
		  p->count = 0;
		  p->pc_count = 0;
		}

	      p->count += 1;
	      if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
		  || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
		  || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA)
		p->pc_count += 1;
	    }
	  break;

	  /* This relocation describes the C++ object vtable hierarchy.
	     Reconstruct it for later use during GC.  */
	case R_NDS32_RELA_GNU_VTINHERIT:
	case R_NDS32_GNU_VTINHERIT:
	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
	    return FALSE;
	  break;

	  /* This relocation describes which C++ vtable entries are actually
	     used.  Record for later use during GC.  */
	case R_NDS32_GNU_VTENTRY:
	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
	    return FALSE;
	  break;
	case R_NDS32_RELA_GNU_VTENTRY:
	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
	    return FALSE;
	  break;
	}
    }

  return TRUE;
}

/* Write VAL in uleb128 format to P, returning a pointer to the
   following byte.
   This code is copied from elf-attr.c.  */

static bfd_byte *
write_uleb128 (bfd_byte *p, unsigned int val)
{
  bfd_byte c;
  do
    {
      c = val & 0x7f;
      val >>= 7;
      if (val)
	c |= 0x80;
      *(p++) = c;
    }
  while (val);
  return p;
}

static bfd_signed_vma
calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
		  Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
		  int *pic_ext_target)
{
  bfd_signed_vma foff;
  bfd_vma symval, addend;
  asection *sym_sec;

  /* Get the value of the symbol referred to by the reloc.  */
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
    {
      Elf_Internal_Sym *isym;

      /* A local symbol.  */
      isym = isymbuf + ELF32_R_SYM (irel->r_info);

      if (isym->st_shndx == SHN_UNDEF)
	sym_sec = bfd_und_section_ptr;
      else if (isym->st_shndx == SHN_ABS)
	sym_sec = bfd_abs_section_ptr;
      else if (isym->st_shndx == SHN_COMMON)
	sym_sec = bfd_com_section_ptr;
      else
	sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
      symval = isym->st_value + sym_sec->output_section->vma
	       + sym_sec->output_offset;
    }
  else
    {
      unsigned long indx;
      struct elf_link_hash_entry *h;
      bfd *owner;

      /* An external symbol.  */
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
      h = elf_sym_hashes (abfd)[indx];
      BFD_ASSERT (h != NULL);

      if (h->root.type != bfd_link_hash_defined
	  && h->root.type != bfd_link_hash_defweak)
	/* This appears to be a reference to an undefined
	   symbol.  Just ignore it--it will be caught by the
	   regular reloc processing.  */
	return 0;
      owner = h->root.u.def.section->owner;
      if (owner && (elf_elfheader (owner)->e_flags & E_NDS32_HAS_PIC))
	*pic_ext_target = 1;

      if (h->root.u.def.section->flags & SEC_MERGE)
	{
	  sym_sec = h->root.u.def.section;
	  symval = _bfd_merged_section_offset (abfd, &sym_sec,
					       elf_section_data (sym_sec)->sec_info,
					       h->root.u.def.value);
	  symval = symval + sym_sec->output_section->vma
		   + sym_sec->output_offset;
	}
      else
	symval = (h->root.u.def.value
		  + h->root.u.def.section->output_section->vma
		  + h->root.u.def.section->output_offset);
    }

  addend = irel->r_addend;

  foff = (symval + addend
	  - (irel->r_offset + sec->output_section->vma + sec->output_offset));
  return foff;
}

static bfd_vma
calculate_plt_memory_address (bfd *abfd, struct bfd_link_info *link_info,
			      Elf_Internal_Sym *isymbuf,
			      Elf_Internal_Rela *irel,
			      Elf_Internal_Shdr *symtab_hdr)
{
  bfd_vma symval;

  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
    {
      Elf_Internal_Sym *isym;
      asection *sym_sec;
      /* A local symbol.  */
      isym = isymbuf + ELF32_R_SYM (irel->r_info);

      if (isym->st_shndx == SHN_UNDEF)
	sym_sec = bfd_und_section_ptr;
      else if (isym->st_shndx == SHN_ABS)
	sym_sec = bfd_abs_section_ptr;
      else if (isym->st_shndx == SHN_COMMON)
	sym_sec = bfd_com_section_ptr;
      else
	sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
      symval = isym->st_value + sym_sec->output_section->vma
	       + sym_sec->output_offset;
    }
  else
    {
      unsigned long indx;
      struct elf_link_hash_entry *h;
      struct elf_nds32_link_hash_table *htab;
      asection *splt;

      /* An external symbol.  */
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
      h = elf_sym_hashes (abfd)[indx];
      BFD_ASSERT (h != NULL);
      htab = nds32_elf_hash_table (link_info);
      splt = htab->splt;

      while (h->root.type == bfd_link_hash_indirect
	     || h->root.type == bfd_link_hash_warning)
	h = (struct elf_link_hash_entry *) h->root.u.i.link;

      if (h->plt.offset == (bfd_vma) - 1)
	{
	  if (h->root.type != bfd_link_hash_defined
	      && h->root.type != bfd_link_hash_defweak)
	    /* This appears to be a reference to an undefined
	     * symbol.  Just ignore it--it will be caught by the
	     * regular reloc processing.  */
	    return 0;
	  symval = (h->root.u.def.value
		    + h->root.u.def.section->output_section->vma
		    + h->root.u.def.section->output_offset);
	}
      else
	symval = splt->output_section->vma + h->plt.offset;
    }

  return symval;
}

static bfd_signed_vma
calculate_plt_offset (bfd *abfd, asection *sec, struct bfd_link_info *link_info,
		      Elf_Internal_Sym *isymbuf, Elf_Internal_Rela *irel,
		      Elf_Internal_Shdr *symtab_hdr)
{
  bfd_vma foff;
  if ((foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
					    symtab_hdr)) == 0)
    return 0;
  else
    return foff - (irel->r_offset
		   + sec->output_section->vma + sec->output_offset);
}

/* Convert a 32-bit instruction to 16-bit one.
   INSN is the input 32-bit instruction, INSN16 is the output 16-bit
   instruction.  If INSN_TYPE is not NULL, it the CGEN instruction
   type of INSN16.  Return 1 if successful.  */

static int
nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
			     int *pinsn_type)
{
  uint16_t insn16 = 0;
  int insn_type = 0;
  unsigned long mach = bfd_get_mach (abfd);

  if (N32_SH5 (insn) != 0)
    return 0;

  switch (N32_SUB5 (insn))
    {
    case N32_ALU1_ADD_SLLI:
    case N32_ALU1_ADD_SRLI:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
	{
	  insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
				N32_RB5 (insn));
	  insn_type = NDS32_INSN_ADD333;
	}
      else if (N32_IS_RT4 (insn))
	{
	  if (N32_RT5 (insn) == N32_RA5 (insn))
	    insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
	  else if (N32_RT5 (insn) == N32_RB5 (insn))
	    insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
	  insn_type = NDS32_INSN_ADD45;
	}
      break;

    case N32_ALU1_SUB_SLLI:
    case N32_ALU1_SUB_SRLI:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
	{
	  insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
				N32_RB5 (insn));
	  insn_type = NDS32_INSN_SUB333;
	}
      else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
	{
	  insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
	  insn_type = NDS32_INSN_SUB45;
	}
      break;

    case N32_ALU1_AND_SLLI:
    case N32_ALU1_AND_SRLI:
      /* and $rt, $rt, $rb -> and33 for v3, v3m.  */
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	  && N32_IS_RB3 (insn))
	{
	  if (N32_RT5 (insn) == N32_RA5 (insn))
	    insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
	  else if (N32_RT5 (insn) == N32_RB5 (insn))
	    insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
	  if (insn16)
	    insn_type = NDS32_INSN_AND33;
	}
      break;

    case N32_ALU1_XOR_SLLI:
    case N32_ALU1_XOR_SRLI:
      /* xor $rt, $rt, $rb -> xor33 for v3, v3m.  */
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	  && N32_IS_RB3 (insn))
	{
	  if (N32_RT5 (insn) == N32_RA5 (insn))
	    insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
	  else if (N32_RT5 (insn) == N32_RB5 (insn))
	    insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
	  if (insn16)
	    insn_type = NDS32_INSN_XOR33;
	}
      break;

    case N32_ALU1_OR_SLLI:
    case N32_ALU1_OR_SRLI:
      /* or $rt, $rt, $rb -> or33 for v3, v3m.  */
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	  && N32_IS_RB3 (insn))
	{
	  if (N32_RT5 (insn) == N32_RA5 (insn))
	    insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
	  else if (N32_RT5 (insn) == N32_RB5 (insn))
	    insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
	  if (insn16)
	    insn_type = NDS32_INSN_OR33;
	}
      break;
    case N32_ALU1_NOR:
      /* nor $rt, $ra, $ra -> not33 for v3, v3m.  */
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
	  && N32_RA5 (insn) == N32_RB5 (insn))
	{
	  insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
	  insn_type = NDS32_INSN_NOT33;
	}
      break;
    case N32_ALU1_SRAI:
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
	{
	  insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
	  insn_type = NDS32_INSN_SRAI45;
	}
      break;

    case N32_ALU1_SRLI:
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
	{
	  insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
	  insn_type = NDS32_INSN_SRLI45;
	}
      break;

    case N32_ALU1_SLLI:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
	{
	  insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
				N32_UB5 (insn));
	  insn_type = NDS32_INSN_SLLI333;
	}
      break;

    case N32_ALU1_ZEH:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
	{
	  insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
	  insn_type = NDS32_INSN_ZEH33;
	}
      break;

    case N32_ALU1_SEB:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
	{
	  insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
	  insn_type = NDS32_INSN_SEB33;
	}
      break;

    case N32_ALU1_SEH:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
	{
	  insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
	  insn_type = NDS32_INSN_SEH33;
	}
      break;

    case N32_ALU1_SLT:
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
	{
	  /* Implicit r15.  */
	  insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
	  insn_type = NDS32_INSN_SLT45;
	}
      break;

    case N32_ALU1_SLTS:
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
	{
	  /* Implicit r15.  */
	  insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
	  insn_type = NDS32_INSN_SLTS45;
	}
      break;
    }

  if ((insn16 & 0x8000) == 0)
    return 0;

  if (pinsn16)
    *pinsn16 = insn16;
  if (pinsn_type)
    *pinsn_type = insn_type;
  return 1;
}

static int
nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
			     int *pinsn_type)
{
  uint16_t insn16 = 0;
  int insn_type;
  unsigned long mach = bfd_get_mach (abfd);

  /* TODO: bset, bclr, btgl, btst.  */
  if (__GF (insn, 6, 4) != 0)
    return 0;

  switch (N32_IMMU (insn, 6))
    {
    case N32_ALU2_MUL:
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	  && N32_IS_RB3 (insn))
	{
	  if (N32_RT5 (insn) == N32_RA5 (insn))
	    insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
	  else if (N32_RT5 (insn) == N32_RB5 (insn))
	    insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
	  if (insn16)
	    insn_type = NDS32_INSN_MUL33;
	}
    }

  if ((insn16 & 0x8000) == 0)
    return 0;

  if (pinsn16)
    *pinsn16 = insn16;
  if (pinsn_type)
    *pinsn_type = insn_type;
  return 1;
}

int
nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
			int *pinsn_type)
{
  int op6;
  uint16_t insn16 = 0;
  int insn_type;
  unsigned long mach = bfd_get_mach (abfd);

  /* Decode 32-bit instruction.  */
  if (insn & 0x80000000)
    {
      /* Not 32-bit insn.  */
      return 0;
    }

  op6 = N32_OP6 (insn);

  /* Convert it to 16-bit instruction.  */
  switch (op6)
    {
    case N32_OP6_MOVI:
      if (IS_WITHIN_S (N32_IMM20S (insn), 5))
	{
	  insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
	  insn_type = NDS32_INSN_MOVI55;
	}
      else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
	       && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
	{
	  insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
			       N32_IMM20S (insn) - 16);
	  insn_type = NDS32_INSN_MOVPI45;
	}
      break;

    case N32_OP6_ADDI:
      if (N32_IMM15S (insn) == 0)
	{
	  /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
	     because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
	  if (mach <= MACH_V2
	      || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
	    {
	      insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
	      insn_type = NDS32_INSN_MOV55;
	    }
	}
      else if (N32_IMM15S (insn) > 0)
	{
	  if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
	    {
	      insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
				    N32_IMM15S (insn));
	      insn_type = NDS32_INSN_ADDI333;
	    }
	  else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
		   && N32_IMM15S (insn) < 32)
	    {
	      insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
	      insn_type = NDS32_INSN_ADDI45;
	    }
	  else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
		   && N32_RT5 (insn) == N32_RA5 (insn)
		   && N32_IMM15S (insn) < 512)
	    {
	      insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
	      insn_type = NDS32_INSN_ADDI10_SP;
	    }
	  else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
		   && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
		   && (N32_IMM15S (insn) % 4 == 0))
	    {
	      insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
				   N32_IMM15S (insn) >> 2);
	      insn_type = NDS32_INSN_ADDRI36_SP;
	    }
	}
      else
	{
	  /* Less than 0.  */
	  if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
	    {
	      insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
				    0 - N32_IMM15S (insn));
	      insn_type = NDS32_INSN_SUBI333;
	    }
	  else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
		   && N32_IMM15S (insn) > -32)
	    {
	      insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
				   0 - N32_IMM15S (insn));
	      insn_type = NDS32_INSN_SUBI45;
	    }
	  else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
		   && N32_RT5 (insn) == N32_RA5 (insn)
		   && N32_IMM15S (insn) >= -512)
	    {
	      insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
	      insn_type = NDS32_INSN_ADDI10_SP;
	    }
	}
      break;

    case N32_OP6_ORI:
      if (N32_IMM15S (insn) == 0)
	{
	  /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
	     because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
	  if (mach <= MACH_V2
	      || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
	    {
	      insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
	      insn_type = NDS32_INSN_MOV55;
	    }
	}
      break;

    case N32_OP6_SUBRI:
      if (mach >= MACH_V3 && N32_IS_RT3 (insn)
	  && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
	{
	  insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
	  insn_type = NDS32_INSN_NEG33;
	}
      break;

    case N32_OP6_ANDI:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
	{
	  if (N32_IMM15U (insn) == 1)
	    {
	      insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
	      insn_type = NDS32_INSN_XLSB33;
	    }
	  else if (N32_IMM15U (insn) == 0x7ff)
	    {
	      insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
	      insn_type = NDS32_INSN_X11B33;
	    }
	  else if (N32_IMM15U (insn) == 0xff)
	    {
	      insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
	      insn_type = NDS32_INSN_ZEB33;
	    }
	  else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
		   && N32_IMM15U (insn) < 256)
	    {
	      int imm15u = N32_IMM15U (insn);

	      if (__builtin_popcount (imm15u) == 1)
		{
		  /* BMSKI33 */
		  int imm3u = __builtin_ctz (imm15u);

		  insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
		  insn_type = NDS32_INSN_BMSKI33;
		}
	      else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
		{
		  /* FEXTI33 */
		  int imm3u = __builtin_ctz (imm15u + 1) - 1;

		  insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
		  insn_type = NDS32_INSN_FEXTI33;
		}
	    }
	}
      break;

    case N32_OP6_SLTI:
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
	  && IS_WITHIN_U (N32_IMM15S (insn), 5))
	{
	  insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
	  insn_type = NDS32_INSN_SLTI45;
	}
      break;

    case N32_OP6_SLTSI:
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
	  && IS_WITHIN_U (N32_IMM15S (insn), 5))
	{
	  insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
	  insn_type = NDS32_INSN_SLTSI45;
	}
      break;

    case N32_OP6_LWI:
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
	{
	  insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
	  insn_type = NDS32_INSN_LWI450;
	}
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	       && IS_WITHIN_U (N32_IMM15S (insn), 3))
	{
	  insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
				N32_IMM15S (insn));
	  insn_type = NDS32_INSN_LWI333;
	}
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
	       && IS_WITHIN_U (N32_IMM15S (insn), 7))
	{
	  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
	  insn_type = NDS32_INSN_LWI37;
	}
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
	       && IS_WITHIN_U (N32_IMM15S (insn), 7))
	{
	  insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
	  insn_type = NDS32_INSN_LWI37_SP;
	}
      else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
	       && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
	{
	  insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
			       N32_IMM15S (insn) + 32);
	  insn_type = NDS32_INSN_LWI45_FE;
	}
      break;

    case N32_OP6_SWI:
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
	{
	  insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
	  insn_type = NDS32_INSN_SWI450;
	}
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	       && IS_WITHIN_U (N32_IMM15S (insn), 3))
	{
	  insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
				N32_IMM15S (insn));
	  insn_type = NDS32_INSN_SWI333;
	}
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
	       && IS_WITHIN_U (N32_IMM15S (insn), 7))
	{
	  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
	  insn_type = NDS32_INSN_SWI37;
	}
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
	       && IS_WITHIN_U (N32_IMM15S (insn), 7))
	{
	  insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
	  insn_type = NDS32_INSN_SWI37_SP;
	}
      break;

    case N32_OP6_LWI_BI:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
	{
	  insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
				N32_IMM15S (insn));
	  insn_type = NDS32_INSN_LWI333_BI;
	}
      break;

    case N32_OP6_SWI_BI:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
	{
	  insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
				N32_IMM15S (insn));
	  insn_type = NDS32_INSN_SWI333_BI;
	}
      break;

    case N32_OP6_LHI:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
	{
	  insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
				N32_IMM15S (insn));
	  insn_type = NDS32_INSN_LHI333;
	}
      break;

    case N32_OP6_SHI:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
	{
	  insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
				N32_IMM15S (insn));
	  insn_type = NDS32_INSN_SHI333;
	}
      break;

    case N32_OP6_LBI:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
	{
	  insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
				N32_IMM15S (insn));
	  insn_type = NDS32_INSN_LBI333;
	}
      break;

    case N32_OP6_SBI:
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
	{
	  insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
				N32_IMM15S (insn));
	  insn_type = NDS32_INSN_SBI333;
	}
      break;

    case N32_OP6_ALU1:
      return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);

    case N32_OP6_ALU2:
      return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);

    case N32_OP6_BR1:
      if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
	goto done;

      if ((insn & __BIT (14)) == 0)
	{
	  /* N32_BR1_BEQ */
	  if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
	      && N32_RT5 (insn) != REG_R5)
	    insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
	  else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
		   && N32_RA5 (insn) != REG_R5)
	    insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
	  insn_type = NDS32_INSN_BEQS38;
	  break;
	}
      else
	{
	  /* N32_BR1_BNE */
	  if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
	      && N32_RT5 (insn) != REG_R5)
	    insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
	  else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
		   && N32_RA5 (insn) != REG_R5)
	    insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
	  insn_type = NDS32_INSN_BNES38;
	  break;
	}
      break;

    case N32_OP6_BR2:
      switch (N32_BR2_SUB (insn))
	{
	case N32_BR2_BEQZ:
	  if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
	    {
	      insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
	      insn_type = NDS32_INSN_BEQZ38;
	    }
	  else if (N32_RT5 (insn) == REG_R15
		   && IS_WITHIN_S (N32_IMM16S (insn), 8))
	    {
	      insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
	      insn_type = NDS32_INSN_BEQZS8;
	    }
	  break;

	case N32_BR2_BNEZ:
	  if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
	    {
	      insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
	      insn_type = NDS32_INSN_BNEZ38;
	    }
	  else if (N32_RT5 (insn) == REG_R15
		   && IS_WITHIN_S (N32_IMM16S (insn), 8))
	    {
	      insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
	      insn_type = NDS32_INSN_BNEZS8;
	    }
	  break;

	case N32_BR2_IFCALL:
	  if (IS_WITHIN_U (N32_IMM16S (insn), 9))
	    {
	      insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
	      insn_type = NDS32_INSN_IFCALL9;
	    }
	  break;
	}
      break;

    case N32_OP6_JI:
      if ((insn & __BIT (24)) == 0)
	{
	  /* N32_JI_J */
	  if (IS_WITHIN_S (N32_IMM24S (insn), 8))
	    {
	      insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
	      insn_type = NDS32_INSN_J8;
	    }
	}
      break;

    case N32_OP6_JREG:
      if (__GF (insn, 8, 2) != 0)
	goto done;

      switch (N32_IMMU (insn, 5))
	{
	case N32_JREG_JR:
	  if (N32_JREG_HINT (insn) == 0)
	    {
	      /* jr */
	      insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
	      insn_type = NDS32_INSN_JR5;
	    }
	  else if (N32_JREG_HINT (insn) == 1)
	    {
	      /* ret */
	      insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
	      insn_type = NDS32_INSN_RET5;
	    }
	  else if (N32_JREG_HINT (insn) == 3)
	    {
	      /* ifret = mov55 $sp, $sp */
	      insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
	      insn_type = NDS32_INSN_IFRET;
	    }
	  break;

	case N32_JREG_JRAL:
	  /* It's convertible when return rt5 is $lp and address
	     translation is kept.  */
	  if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
	    {
	      insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
	      insn_type = NDS32_INSN_JRAL5;
	    }
	  break;
	}
      break;

    case N32_OP6_MISC:
      if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
	{
	  /* For v3, swid above 31 are used for ex9.it.  */
	  insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
	  insn_type = NDS32_INSN_BREAK16;
	}
      break;

    default:
      /* This instruction has no 16-bit variant.  */
      goto done;
    }

done:
  /* Bit-15 of insn16 should be set for a valid instruction.  */
  if ((insn16 & 0x8000) == 0)
    return 0;

  if (pinsn16)
    *pinsn16 = insn16;
  if (pinsn_type)
    *pinsn_type = insn_type;
  return 1;
}

static int
special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
			  Elf_Internal_Rela *reloc)
{
  uint16_t insn16 = 0;

  if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
      || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
    return 0;

  if (!N32_IS_RT3 (insn))
    return 0;

  switch (N32_OP6 (insn))
    {
    case N32_OP6_LWI:
      if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
	insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
      break;
    case N32_OP6_SWI:
      if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
	insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
      break;
    case N32_OP6_HWGP:
      if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
	break;

      if (__GF (insn, 17, 3) == 6)
	insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
      else if (__GF (insn, 17, 3) == 7)
	insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
      break;
    }

  if ((insn16 & 0x8000) == 0)
    return 0;

  *pinsn16 = insn16;
  return 1;
}

/* Convert a 16-bit instruction to 32-bit one.
   INSN16 it the input and PINSN it the point to output.
   Return non-zero on successful.  Otherwise 0 is returned.  */

int
nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
{
  uint32_t insn = 0xffffffff;
  unsigned long mach = bfd_get_mach (abfd);

  /* NOTE: push25, pop25 and movd44 do not have 32-bit variants.  */

  switch (__GF (insn16, 9, 6))
    {
    case 0x4:			/* add45 */
      insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
		       N16_RA5 (insn16));
      goto done;
    case 0x5:			/* sub45 */
      insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
		       N16_RA5 (insn16));
      goto done;
    case 0x6:			/* addi45 */
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
			N16_IMM5U (insn16));
      goto done;
    case 0x7:			/* subi45 */
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
			-N16_IMM5U (insn16));
      goto done;
    case 0x8:			/* srai45 */
      insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
		       N16_IMM5U (insn16));
      goto done;
    case 0x9:			/* srli45 */
      insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
		       N16_IMM5U (insn16));
      goto done;
    case 0xa:			/* slli333 */
      insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
		       N16_IMM3U (insn16));
      goto done;
    case 0xc:			/* add333 */
      insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
		       N16_RB3 (insn16));
      goto done;
    case 0xd:			/* sub333 */
      insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
		       N16_RB3 (insn16));
      goto done;
    case 0xe:			/* addi333 */
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
			N16_IMM3U (insn16));
      goto done;
    case 0xf:			/* subi333 */
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
			-N16_IMM3U (insn16));
      goto done;
    case 0x10:			/* lwi333 */
      insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
			N16_IMM3U (insn16));
      goto done;
    case 0x12:			/* lhi333 */
      insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
			N16_IMM3U (insn16));
      goto done;
    case 0x13:			/* lbi333 */
      insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
			N16_IMM3U (insn16));
      goto done;
    case 0x11:			/* lwi333.bi */
      insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
			N16_IMM3U (insn16));
      goto done;
    case 0x14:			/* swi333 */
      insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
			N16_IMM3U (insn16));
      goto done;
    case 0x16:			/* shi333 */
      insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
			N16_IMM3U (insn16));
      goto done;
    case 0x17:			/* sbi333 */
      insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
			N16_IMM3U (insn16));
      goto done;
    case 0x15:			/* swi333.bi */
      insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
			N16_IMM3U (insn16));
      goto done;
    case 0x18:			/* addri36.sp */
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
			N16_IMM6U (insn16) << 2);
      goto done;
    case 0x19:			/* lwi45.fe */
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
			(N16_IMM5U (insn16) - 32));
      goto done;
    case 0x1a:			/* lwi450 */
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
      goto done;
    case 0x1b:			/* swi450 */
      insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
      goto done;

      /* These are r15 implied instructions.  */
    case 0x30:			/* slts45 */
      insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
      goto done;
    case 0x31:			/* slt45 */
      insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
      goto done;
    case 0x32:			/* sltsi45 */
      insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
      goto done;
    case 0x33:			/* slti45 */
      insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
      goto done;
    case 0x34:			/* beqzs8, bnezs8 */
      if (insn16 & __BIT (8))
	insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
      else
	insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
      goto done;

    case 0x35:			/* break16, ex9.it */
      /* Only consider range of v3 break16.  */
      insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
      goto done;

    case 0x3c:			/* ifcall9 */
      insn = N32_BR2 (IFCALL, 0, N16_IMM9U (insn16));
      goto done;
    case 0x3d:			/* movpi45 */
      insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
      goto done;

    case 0x3f:			/* MISC33 */
      switch (insn16 & 0x7)
	{
	case 2:			/* neg33 */
	  insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
	  break;
	case 3:			/* not33 */
	  insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
			   N16_RA3 (insn16));
	  break;
	case 4:			/* mul33 */
	  insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
			   N16_RA3 (insn16));
	  break;
	case 5:			/* xor33 */
	  insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
			   N16_RA3 (insn16));
	  break;
	case 6:			/* and33 */
	  insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
			   N16_RA3 (insn16));
	  break;
	case 7:			/* or33 */
	  insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
			   N16_RA3 (insn16));
	  break;
	}
      goto done;

    case 0xb:
      switch (insn16 & 0x7)
	{
	case 0:			/* zeb33 */
	  insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
	  break;
	case 1:			/* zeh33 */
	  insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
	  break;
	case 2:			/* seb33 */
	  insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
	  break;
	case 3:			/* seh33 */
	  insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
	  break;
	case 4:			/* xlsb33 */
	  insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
	  break;
	case 5:			/* x11b33 */
	  insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
	  break;
	case 6:			/* bmski33 */
	  insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
			    1 << __GF (insn16, 3, 3));
	  break;
	case 7:			/* fexti33 */
	  insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
			    (1 << (__GF (insn16, 3, 3) + 1)) - 1);
	  break;
	}
      goto done;
    }

  switch (__GF (insn16, 10, 5))
    {
    case 0x0:			/* mov55 or ifret16 */
      if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
	  && N16_RT5 (insn16) == N16_RA5 (insn16))
	insn = N32_JREG (JR, 0, 0, 0, 3);
      else
	insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
      goto done;
    case 0x1:			/* movi55 */
      insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
      goto done;
    case 0x1b:			/* addi10s (V2) */
      insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
      goto done;
    }

  switch (__GF (insn16, 11, 4))
    {
    case 0x7:			/* lwi37.fp/swi37.fp */
      if (insn16 & __BIT (7))	/* swi37.fp */
	insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
      else			/* lwi37.fp */
	insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
      goto done;
    case 0x8:			/* beqz38 */
      insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
      goto done;
    case 0x9:			/* bnez38 */
      insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
      goto done;
    case 0xa:			/* beqs38/j8, implied r5 */
      if (N16_RT38 (insn16) == 5)
	insn = N32_JI (J, N16_IMM8S (insn16));
      else
	insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
      goto done;
    case 0xb:			/* bnes38 and others */
      if (N16_RT38 (insn16) == 5)
	{
	  switch (__GF (insn16, 5, 3))
	    {
	    case 0:		/* jr5 */
	      insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
	      break;
	    case 4:		/* ret5 */
	      insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
	      break;
	    case 1:		/* jral5 */
	      insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
	      break;
	    case 2:		/* ex9.it imm5 */
	      /* ex9.it had no 32-bit variantl.  */
	      break;
	    case 5:		/* add5.pc */
	      /* add5.pc had no 32-bit variantl.  */
	      break;
	    }
	}
      else			/* bnes38 */
	insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
      goto done;
    case 0xe:			/* lwi37/swi37 */
      if (insn16 & (1 << 7))	/* swi37.sp */
	insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
      else			/* lwi37.sp */
	insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
      goto done;
    }

done:
  if (insn & 0x80000000)
    return 0;

  if (pinsn)
    *pinsn = insn;
  return 1;
}

static bfd_boolean
is_sda_access_insn (unsigned long insn)
{
  switch (N32_OP6 (insn))
    {
    case N32_OP6_LWI:
    case N32_OP6_LHI:
    case N32_OP6_LHSI:
    case N32_OP6_LBI:
    case N32_OP6_LBSI:
    case N32_OP6_SWI:
    case N32_OP6_SHI:
    case N32_OP6_SBI:
    case N32_OP6_LWC:
    case N32_OP6_LDC:
    case N32_OP6_SWC:
    case N32_OP6_SDC:
      return TRUE;
    default:
      ;
    }
  return FALSE;
}

static unsigned long
turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
{
  uint32_t oinsn = 0;

  switch (type)
    {
    case R_NDS32_GOT_LO12:
    case R_NDS32_GOTOFF_LO12:
    case R_NDS32_PLTREL_LO12:
    case R_NDS32_PLT_GOTREL_LO12:
    case R_NDS32_LO12S0_RELA:
      switch (N32_OP6 (insn))
	{
	case N32_OP6_LBI:
	  /* lbi.gp */
	  oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
	  break;
	case N32_OP6_LBSI:
	  /* lbsi.gp */
	  oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), __BIT (19));
	  break;
	case N32_OP6_SBI:
	  /* sbi.gp */
	  oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
	  break;
	case N32_OP6_ORI:
	  /* addi.gp */
	  oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
	  break;
	}
      break;

    case R_NDS32_LO12S1_RELA:
      switch (N32_OP6 (insn))
	{
	case N32_OP6_LHI:
	  /* lhi.gp */
	  oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
	  break;
	case N32_OP6_LHSI:
	  /* lhsi.gp */
	  oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (18));
	  break;
	case N32_OP6_SHI:
	  /* shi.gp */
	  oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (19));
	  break;
	}
      break;

    case R_NDS32_LO12S2_RELA:
      switch (N32_OP6 (insn))
	{
	case N32_OP6_LWI:
	  /* lwi.gp */
	  oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
	  break;
	case N32_OP6_SWI:
	  /* swi.gp */
	  oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
	  break;
	}
      break;

    case R_NDS32_LO12S2_DP_RELA:
    case R_NDS32_LO12S2_SP_RELA:
      oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
      break;
    }

  if (oinsn)
    *pinsn = oinsn;

  return oinsn != 0;
}

/* Linker hasn't found the correct merge section for non-section symbol
   in relax time, this work is left to the function elf_link_input_bfd().
   So for non-section symbol, _bfd_merged_section_offset is also needed
   to find the correct symbol address.  */

static bfd_vma
nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
			  asection **psec, Elf_Internal_Rela *rel)
{
  asection *sec = *psec;
  bfd_vma relocation;

  relocation = (sec->output_section->vma
		+ sec->output_offset + sym->st_value);
  if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
    {
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
	rel->r_addend =
	  _bfd_merged_section_offset (abfd, psec,
				      elf_section_data (sec)->sec_info,
				      sym->st_value + rel->r_addend);
      else
	rel->r_addend =
	  _bfd_merged_section_offset (abfd, psec,
				      elf_section_data (sec)->sec_info,
				      sym->st_value) + rel->r_addend;

      if (sec != *psec)
	{
	  /* If we have changed the section, and our original section is
	     marked with SEC_EXCLUDE, it means that the original
	     SEC_MERGE section has been completely subsumed in some
	     other SEC_MERGE section.  In this case, we need to leave
	     some info around for --emit-relocs.  */
	  if ((sec->flags & SEC_EXCLUDE) != 0)
	    sec->kept_section = *psec;
	  sec = *psec;
	}
      rel->r_addend -= relocation;
      rel->r_addend += sec->output_section->vma + sec->output_offset;
    }
  return relocation;
}

static bfd_vma
calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
			  Elf_Internal_Sym *isymbuf,
			  Elf_Internal_Shdr *symtab_hdr)
{
  bfd_signed_vma foff;
  bfd_vma symval, addend;
  Elf_Internal_Rela irel_fn;
  Elf_Internal_Sym *isym;
  asection *sym_sec;

  /* Get the value of the symbol referred to by the reloc.  */
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
    {
      /* A local symbol.  */
      isym = isymbuf + ELF32_R_SYM (irel->r_info);

      if (isym->st_shndx == SHN_UNDEF)
	sym_sec = bfd_und_section_ptr;
      else if (isym->st_shndx == SHN_ABS)
	sym_sec = bfd_abs_section_ptr;
      else if (isym->st_shndx == SHN_COMMON)
	sym_sec = bfd_com_section_ptr;
      else
	sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
      memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
      symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
      addend = irel_fn.r_addend;
    }
  else
    {
      unsigned long indx;
      struct elf_link_hash_entry *h;

      /* An external symbol.  */
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
      h = elf_sym_hashes (abfd)[indx];
      BFD_ASSERT (h != NULL);

      while (h->root.type == bfd_link_hash_indirect
	     || h->root.type == bfd_link_hash_warning)
	h = (struct elf_link_hash_entry *) h->root.u.i.link;

      if (h->root.type != bfd_link_hash_defined
	  && h->root.type != bfd_link_hash_defweak)
	/* This appears to be a reference to an undefined
	   symbol.  Just ignore it--it will be caught by the
	   regular reloc processing.  */
	return 0;

      if (h->root.u.def.section->flags & SEC_MERGE)
	{
	  sym_sec = h->root.u.def.section;
	  symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
					       (sym_sec)->sec_info, h->root.u.def.value);
	  symval = symval + sym_sec->output_section->vma
		   + sym_sec->output_offset;
	}
      else
	symval = (h->root.u.def.value
		  + h->root.u.def.section->output_section->vma
		  + h->root.u.def.section->output_offset);
      addend = irel->r_addend;
    }

  foff = symval + addend;

  return foff;
}

static bfd_vma
calculate_got_memory_address (bfd *abfd, struct bfd_link_info *link_info,
			      Elf_Internal_Rela *irel,
			      Elf_Internal_Shdr *symtab_hdr)
{
  int symndx;
  bfd_vma *local_got_offsets;
  /* Get the value of the symbol referred to by the reloc.  */
  struct elf_link_hash_entry *h;
  struct elf_nds32_link_hash_table *htab = nds32_elf_hash_table (link_info);

  /* An external symbol.  */
  symndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  h = elf_sym_hashes (abfd)[symndx];
  while (h->root.type == bfd_link_hash_indirect
	 || h->root.type == bfd_link_hash_warning)
    h = (struct elf_link_hash_entry *) h->root.u.i.link;

  if (symndx >= 0)
    {
      BFD_ASSERT (h != NULL);
      return htab->sgot->output_section->vma + htab->sgot->output_offset
	     + h->got.offset;
    }
  else
    {
      local_got_offsets = elf_local_got_offsets (abfd);
      BFD_ASSERT (local_got_offsets != NULL);
      return htab->sgot->output_section->vma + htab->sgot->output_offset
	     + local_got_offsets[ELF32_R_SYM (irel->r_info)];
    }

  /* The _GLOBAL_OFFSET_TABLE_ may be undefweak(or should be?).  */
  /* The check of h->root.type is passed.  */
}

static int
is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
	      asection *sec, Elf_Internal_Rela *rel)
{
  bfd_byte *contents;
  unsigned short insn16;

  if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
    return FALSE;
  contents = elf_section_data (sec)->this_hdr.contents;
  insn16 = bfd_getb16 (contents + rel->r_offset);
  if (insn16 == NDS32_NOP16)
    return TRUE;
  return FALSE;
}

/* It checks whether the instruction could be converted to
   16-bit form and returns the converted one.

   `internal_relocs' is supposed to be sorted.  */

static int
is_convert_32_to_16 (bfd *abfd, asection *sec,
		     Elf_Internal_Rela *reloc,
		     Elf_Internal_Rela *internal_relocs,
		     Elf_Internal_Rela *irelend,
		     uint16_t *insn16)
{
#define NORMAL_32_TO_16 (1 << 0)
#define SPECIAL_32_TO_16 (1 << 1)
  bfd_byte *contents = NULL;
  bfd_signed_vma off;
  bfd_vma mem_addr;
  uint32_t insn = 0;
  Elf_Internal_Rela *pc_rel;
  int pic_ext_target = 0;
  Elf_Internal_Shdr *symtab_hdr;
  Elf_Internal_Sym *isymbuf = NULL;
  int convert_type;
  bfd_vma offset;

  if (reloc->r_offset + 4 > sec->size)
    return FALSE;

  offset = reloc->r_offset;

  if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
    return FALSE;
  insn = bfd_getb32 (contents + offset);

  if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
    convert_type = NORMAL_32_TO_16;
  else if (special_convert_32_to_16 (insn, insn16, reloc))
    convert_type = SPECIAL_32_TO_16;
  else
    return FALSE;

  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  if (!nds32_get_local_syms (abfd, sec, &isymbuf))
    return FALSE;

  /* Find the first relocation of the same relocation-type,
     so we iteratie them forward.  */
  pc_rel = reloc;
  while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
    pc_rel--;

  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
    {
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
	  || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
	  || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
	  || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
	{
	  off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
				  &pic_ext_target);
	  if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
	      || off == 0)
	    return FALSE;
	  break;
	}
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
	{
	  /* movi => movi55  */
	  mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
					       symtab_hdr);
	  /* mem_addr is unsigned, but the value should
	     be between [-16, 15].  */
	  if ((mem_addr + 0x10) >> 5)
	    return FALSE;
	  break;
	}
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
	       || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
	{
	  /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
	     because it can be relaxed to addi for TLS_LE_ADD.  */
	  return FALSE;
	}
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
		|| ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
	       && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
	       && convert_type == SPECIAL_32_TO_16)
	{
	  /* fp-as-gp
	     We've selected a best fp-base for this access, so we can
	     always resolve it anyway.  Do nothing.  */
	  break;
	}
      else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
		&& (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
	       || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
		   && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
	       || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
		   && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
	{
	  /* Prevent unresolved addi instruction translate
	     to addi45 or addi333.  */
	  return FALSE;
	}
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
	{
	  off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
				  &pic_ext_target);
	  if (off >= ACCURATE_U9BIT_S1 || off <= 0)
	    return FALSE;
	  break;
	}
    }

  return TRUE;
}

static void
nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
		    Elf_Internal_Rela *reloc,
		    Elf_Internal_Rela *internal_relocs,
		    Elf_Internal_Rela *irelend,
		    unsigned short insn16)
{
  Elf_Internal_Rela *pc_rel;
  bfd_vma offset;

  offset = reloc->r_offset;
  bfd_putb16 (insn16, contents + offset);
  /* Find the first relocation of the same relocation-type,
     so we iteratie them forward.  */
  pc_rel = reloc;
  while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
    pc_rel--;

  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
    {
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
	  || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
	  || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
	{
	  pc_rel->r_info =
	    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
	}
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
	pc_rel->r_info =
	  ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
	pc_rel->r_info =
	  ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
	       || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
	pc_rel->r_info =
	  ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
	pc_rel->r_info =
	  ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
    }
}

/* Find a relocation of type specified by `reloc_type'
   of the same r_offset with reloc.
   If not found, return irelend.

   Assuming relocations are sorted by r_offset,
   we find the relocation from `reloc' backward untill relocs,
   or find it from `reloc' forward untill irelend.  */

static Elf_Internal_Rela *
find_relocs_at_address (Elf_Internal_Rela *reloc,
			Elf_Internal_Rela *relocs,
			Elf_Internal_Rela *irelend,
			enum elf_nds32_reloc_type reloc_type)
{
  Elf_Internal_Rela *rel_t;

  /* Find backward.  */
  for (rel_t = reloc;
       rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
       rel_t--)
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
      return rel_t;

  /* We didn't find it backward.  Try find it forward.  */
  for (rel_t = reloc;
       rel_t < irelend && rel_t->r_offset == reloc->r_offset;
       rel_t++)
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
      return rel_t;

  return irelend;
}

/* Find a relocation of specified type and offset.
   `reloc' is just a refence point to find a relocation at specified offset.
   If not found, return irelend.

   Assuming relocations are sorted by r_offset,
   we find the relocation from `reloc' backward untill relocs,
   or find it from `reloc' forward untill irelend.  */

static Elf_Internal_Rela *
find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
			     Elf_Internal_Rela *relocs,
			     Elf_Internal_Rela *irelend,
			     enum elf_nds32_reloc_type reloc_type,
			     bfd_vma offset_p)
{
  Elf_Internal_Rela *rel_t = NULL;

  /* First, we try to find a relocation of offset `offset_p',
     and then we use find_relocs_at_address to find specific type.  */

  if (reloc->r_offset > offset_p)
    {
      /* Find backward.  */
      for (rel_t = reloc;
	   rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
	/* Do nothing.  */;
    }
  else if (reloc->r_offset < offset_p)
    {
      /* Find forward.  */
      for (rel_t = reloc;
	   rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
	/* Do nothing.  */;
    }
  else
    rel_t = reloc;

  /* Not found?  */
  if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
    return irelend;

  return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
}

static bfd_boolean
nds32_elf_check_dup_relocs (Elf_Internal_Rela *reloc,
			    Elf_Internal_Rela *internal_relocs,
			    Elf_Internal_Rela *irelend,
			    unsigned char reloc_type)
{
  Elf_Internal_Rela *rel_t;

  for (rel_t = reloc;
       rel_t >= internal_relocs && rel_t->r_offset == reloc->r_offset;
       rel_t--)
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
      {
	if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
	    && rel_t->r_addend == reloc->r_addend)
	  continue;
	return TRUE;
      }

  for (rel_t = reloc; rel_t < irelend && rel_t->r_offset == reloc->r_offset;
       rel_t++)
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
      {
	if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
	    && rel_t->r_addend == reloc->r_addend)
	  continue;
	return TRUE;
      }

  return FALSE;
}

typedef struct nds32_elf_blank nds32_elf_blank_t;
struct nds32_elf_blank
{
  /* Where the blank begins.  */
  bfd_vma offset;
  /* The size of the blank.  */
  bfd_vma size;
  /* The accumulative size before this blank.  */
  bfd_vma total_size;
  nds32_elf_blank_t *next;
  nds32_elf_blank_t *prev;
};

static nds32_elf_blank_t *blank_free_list = NULL;

static nds32_elf_blank_t *
create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
{
  nds32_elf_blank_t *blank_t;

  if (blank_free_list)
    {
      blank_t = blank_free_list;
      blank_free_list = blank_free_list->next;
    }
  else
    blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));

  if (blank_t == NULL)
    return NULL;

  blank_t->offset = offset_p;
  blank_t->size = size_p;
  blank_t->total_size = 0;
  blank_t->next = NULL;
  blank_t->prev = NULL;

  return blank_t;
}

static void
remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
{
  if (blank_free_list)
    {
      blank_free_list->prev = blank_p;
      blank_p->next = blank_free_list;
    }
  else
    blank_p->next = NULL;

  blank_p->prev = NULL;
  blank_free_list = blank_p;
}

static void
clean_nds32_elf_blank (void)
{
  nds32_elf_blank_t *blank_t;

  while (blank_free_list)
    {
      blank_t = blank_free_list;
      blank_free_list = blank_free_list->next;
      free (blank_t);
    }
}

static nds32_elf_blank_t *
search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
{
  nds32_elf_blank_t *blank_t;

  if (!blank_p)
    return NULL;
  blank_t = blank_p;

  while (blank_t && addr < blank_t->offset)
    blank_t = blank_t->prev;
  while (blank_t && blank_t->next && addr >= blank_t->next->offset)
    blank_t = blank_t->next;

  return blank_t;
}

static bfd_vma
get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
			   int overwrite)
{
  nds32_elf_blank_t *blank_t;

  blank_t = search_nds32_elf_blank (*blank_p, addr);
  if (!blank_t)
    return 0;

  if (overwrite)
    *blank_p = blank_t;

  if (addr < blank_t->offset + blank_t->size)
    return blank_t->total_size + (addr - blank_t->offset);
  else
    return blank_t->total_size + blank_t->size;
}

static bfd_boolean
insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
{
  nds32_elf_blank_t *blank_t, *blank_t2;

  if (!*blank_p)
    {
      *blank_p = create_nds32_elf_blank (addr, len);
      return *blank_p ? TRUE : FALSE;
    }

  blank_t = search_nds32_elf_blank (*blank_p, addr);

  if (blank_t == NULL)
    {
      blank_t = create_nds32_elf_blank (addr, len);
      if (!blank_t)
	return FALSE;
      while ((*blank_p)->prev != NULL)
	*blank_p = (*blank_p)->prev;
      blank_t->next = *blank_p;
      (*blank_p)->prev = blank_t;
      (*blank_p) = blank_t;
      return TRUE;
    }

  if (addr < blank_t->offset + blank_t->size)
    {
      if (addr > blank_t->offset + blank_t->size)
	blank_t->size = addr - blank_t->offset;
    }
  else
    {
      blank_t2 = create_nds32_elf_blank (addr, len);
      if (!blank_t2)
	return FALSE;
      if (blank_t->next)
	{
	  blank_t->next->prev = blank_t2;
	  blank_t2->next = blank_t->next;
	}
      blank_t2->prev = blank_t;
      blank_t->next = blank_t2;
      *blank_p = blank_t2;
    }

  return TRUE;
}

static bfd_boolean
insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
				     bfd_vma len)
{
  nds32_elf_blank_t *blank_t;

  if (!insert_nds32_elf_blank (blank_p, addr, len))
    return FALSE;

  blank_t = *blank_p;

  if (!blank_t->prev)
    {
      blank_t->total_size = 0;
      blank_t = blank_t->next;
    }

  while (blank_t)
    {
      blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
      blank_t = blank_t->next;
    }

  return TRUE;
}

static void
calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
{
  nds32_elf_blank_t *blank_t;
  bfd_vma total_size = 0;

  if (!blank_p)
    return;

  blank_t = blank_p;
  while (blank_t->prev)
    blank_t = blank_t->prev;
  while (blank_t)
    {
      blank_t->total_size = total_size;
      total_size += blank_t->size;
      blank_t = blank_t->next;
    }
}

static bfd_boolean
nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
			       nds32_elf_blank_t *blank_p)
{
  Elf_Internal_Shdr *symtab_hdr;	/* Symbol table header of this bfd.  */
  Elf_Internal_Sym *isym = NULL;	/* Symbol table of this bfd.  */
  Elf_Internal_Sym *isymend;		/* Symbol entry iterator.  */
  unsigned int sec_shndx;		/* The section the be relaxed.  */
  bfd_byte *contents;			/* Contents data of iterating section.  */
  Elf_Internal_Rela *internal_relocs;
  Elf_Internal_Rela *irel;
  Elf_Internal_Rela *irelend;
  struct elf_link_hash_entry **sym_hashes;
  struct elf_link_hash_entry **end_hashes;
  unsigned int symcount;
  asection *sect;
  nds32_elf_blank_t *blank_t;
  nds32_elf_blank_t *blank_t2;
  nds32_elf_blank_t *blank_head;

  blank_head = blank_t = blank_p;
  while (blank_head->prev != NULL)
    blank_head = blank_head->prev;
  while (blank_t->next != NULL)
    blank_t = blank_t->next;

  if (blank_t->offset + blank_t->size <= sec->size)
    {
      blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
      blank_t->next->prev = blank_t;
    }
  if (blank_head->offset > 0)
    {
      blank_head->prev = create_nds32_elf_blank (0, 0);
      blank_head->prev->next = blank_head;
      blank_head = blank_head->prev;
    }

  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);

  /* The deletion must stop at the next ALIGN reloc for an alignment
     power larger than the number of bytes we are deleting.  */

  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  if (!nds32_get_local_syms (abfd, sec, &isym))
    return FALSE;

  if (isym == NULL)
    {
      isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
				   symtab_hdr->sh_info, 0, NULL, NULL, NULL);
      symtab_hdr->contents = (bfd_byte *) isym;
    }

  if (isym == NULL || symtab_hdr->sh_info == 0)
    return FALSE;

  blank_t = blank_head;
  calc_nds32_blank_total (blank_head);

  for (sect = abfd->sections; sect != NULL; sect = sect->next)
    {
      /* Adjust all the relocs.  */

      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
      internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
						   TRUE /* keep_memory */);
      irelend = internal_relocs + sect->reloc_count;

      blank_t = blank_head;
      blank_t2 = blank_head;

      if (!(sect->flags & SEC_RELOC))
	continue;

      nds32_get_section_contents (abfd, sect, &contents, TRUE);

      for (irel = internal_relocs; irel < irelend; irel++)
	{
	  bfd_vma raddr;

	  if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
	      && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
	      && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
	    {
	      unsigned long val = 0;
	      unsigned long mask;
	      long before, between;
	      long offset = 0;

	      switch (ELF32_R_TYPE (irel->r_info))
		{
		case R_NDS32_DIFF8:
		  offset = bfd_get_8 (abfd, contents + irel->r_offset);
		  break;
		case R_NDS32_DIFF16:
		  offset = bfd_get_16 (abfd, contents + irel->r_offset);
		  break;
		case R_NDS32_DIFF32:
		  val = bfd_get_32 (abfd, contents + irel->r_offset);
		  /* Get the signed bit and mask for the high part.  The
		     gcc will alarm when right shift 32-bit since the
		     type size of long may be 32-bit.  */
		  mask = 0 - (val >> 31);
		  if (mask)
		    offset = (val | (mask - 0xffffffff));
		  else
		    offset = val;
		  break;
		default:
		  BFD_ASSERT (0);
		}

	      /*		  DIFF value
		0	     |encoded in location|
		|------------|-------------------|---------
			    sym+off(addend)
		-- before ---| *****************
		--------------------- between ---|

		We only care how much data are relax between DIFF,
		marked as ***.  */

	      before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
	      between = get_nds32_elf_blank_total (&blank_t,
						   irel->r_addend + offset, 0);
	      if (between == before)
		goto done_adjust_diff;

	      switch (ELF32_R_TYPE (irel->r_info))
		{
		case R_NDS32_DIFF8:
		  bfd_put_8 (abfd, offset - (between - before),
			     contents + irel->r_offset);
		  break;
		case R_NDS32_DIFF16:
		  bfd_put_16 (abfd, offset - (between - before),
			      contents + irel->r_offset);
		  break;
		case R_NDS32_DIFF32:
		  bfd_put_32 (abfd, offset - (between - before),
			      contents + irel->r_offset);
		  break;
		}
	    }
	  else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
	      && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
	    {
	      bfd_vma val = 0;
	      unsigned int len = 0;
	      unsigned long before, between;
	      bfd_byte *endp, *p;

	      val = read_unsigned_leb128 (abfd, contents + irel->r_offset,
					  &len);

	      before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
	      between = get_nds32_elf_blank_total (&blank_t,
						   irel->r_addend + val, 0);
	      if (between == before)
		goto done_adjust_diff;

	      p = contents + irel->r_offset;
	      endp = p + len -1;
	      memset (p, 0x80, len);
	      *(endp) = 0;
	      p = write_uleb128 (p, val - (between - before)) - 1;
	      if (p < endp)
		*p |= 0x80;
	    }
done_adjust_diff:

	  if (sec == sect)
	    {
	      raddr = irel->r_offset;
	      irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
							   irel->r_offset, 1);

	      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
		continue;
	      if (blank_t2 && blank_t2->next
		  && (blank_t2->offset > raddr
		      || blank_t2->next->offset <= raddr))
		(*_bfd_error_handler)
		  (_("%B: %s\n"), abfd,
		   "Error: search_nds32_elf_blank reports wrong node");

	      /* Mark reloc in deleted portion as NONE.
		 For some relocs like R_NDS32_LABEL that doesn't modify the
		 content in the section.  R_NDS32_LABEL doesn't belong to the
		 instruction in the section, so we should preserve it.  */
	      if (raddr >= blank_t2->offset
		  && raddr < blank_t2->offset + blank_t2->size
		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
		{
		  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
					       R_NDS32_NONE);
		  continue;
		}
	    }

	  if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
	      || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
	      || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
	    continue;

	  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
	      && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
	      && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
	    {
	      if (irel->r_addend <= sec->size)
		irel->r_addend -=
		  get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
	    }
	}
    }

  /* Adjust the local symbols defined in this section.  */
  blank_t = blank_head;
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
    {
      if (isym->st_shndx == sec_shndx)
	{
	  if (isym->st_value <= sec->size)
	    {
	      bfd_vma ahead;
	      bfd_vma orig_addr = isym->st_value;

	      ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
	      isym->st_value -= ahead;

	      /* Adjust function size.  */
	      if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
		  && isym->st_size > 0)
		isym->st_size -=
		  get_nds32_elf_blank_total
		  (&blank_t, orig_addr + isym->st_size, 0) - ahead;
	    }
	}
    }

  /* Now adjust the global symbols defined in this section.  */
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
	      - symtab_hdr->sh_info);
  sym_hashes = elf_sym_hashes (abfd);
  end_hashes = sym_hashes + symcount;
  blank_t = blank_head;
  for (; sym_hashes < end_hashes; sym_hashes++)
    {
      struct elf_link_hash_entry *sym_hash = *sym_hashes;

      if ((sym_hash->root.type == bfd_link_hash_defined
	   || sym_hash->root.type == bfd_link_hash_defweak)
	  && sym_hash->root.u.def.section == sec)
	{
	  if (sym_hash->root.u.def.value <= sec->size)
	    {
	      bfd_vma ahead;
	      bfd_vma orig_addr = sym_hash->root.u.def.value;

	      ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
	      sym_hash->root.u.def.value -= ahead;

	      /* Adjust function size.  */
	      if (sym_hash->type == STT_FUNC)
		sym_hash->size -=
		  get_nds32_elf_blank_total
		  (&blank_t, orig_addr + sym_hash->size, 0) - ahead;

	    }
	}
    }

  contents = elf_section_data (sec)->this_hdr.contents;
  blank_t = blank_head;
  while (blank_t->next)
    {
      /* Actually delete the bytes.  */

      /* If current blank is the last blank overlap with current section,
	 go to finish process.  */
      if (sec->size <= (blank_t->next->offset))
	break;

      memmove (contents + blank_t->offset - blank_t->total_size,
	       contents + blank_t->offset + blank_t->size,
	       blank_t->next->offset - (blank_t->offset + blank_t->size));

      blank_t = blank_t->next;
    }

  if (sec->size > (blank_t->offset + blank_t->size))
    {
      /* There are remaining code between blank and section boundary.
	 Move the remaining code to appropriate location.  */
      memmove (contents + blank_t->offset - blank_t->total_size,
	       contents + blank_t->offset + blank_t->size,
	       sec->size - (blank_t->offset + blank_t->size));
      sec->size -= blank_t->total_size + blank_t->size;
    }
  else
    /* This blank is not entirely included in the section,
       reduce the section size by only part of the blank size.  */
    sec->size -= blank_t->total_size + (sec->size - blank_t->offset);

  while (blank_head)
    {
      blank_t = blank_head;
      blank_head = blank_head->next;
      remove_nds32_elf_blank (blank_t);
    }

  return TRUE;
}

/* Get the contents of a section.  */

static int
nds32_get_section_contents (bfd *abfd, asection *sec,
			    bfd_byte **contents_p, bfd_boolean cache)
{
  /* Get the section contents.  */
  if (elf_section_data (sec)->this_hdr.contents != NULL)
    *contents_p = elf_section_data (sec)->this_hdr.contents;
  else
    {
      if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
	return FALSE;
      if (cache)
	elf_section_data (sec)->this_hdr.contents = *contents_p;
    }

  return TRUE;
}

/* Get the contents of the internal symbol of abfd.  */

static int
nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
		      Elf_Internal_Sym **isymbuf_p)
{
  Elf_Internal_Shdr *symtab_hdr;
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;

  /* Read this BFD's local symbols if we haven't done so already.  */
  if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
    {
      *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
      if (*isymbuf_p == NULL)
	{
	  *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
					     symtab_hdr->sh_info, 0,
					     NULL, NULL, NULL);
	  if (*isymbuf_p == NULL)
	    return FALSE;
	}
    }
  symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);

  return TRUE;
}

/* Range of small data.  */
static bfd_vma sdata_range[2][2];
static bfd_vma const sdata_init_range[2] =
{ ACCURATE_12BIT_S1, ACCURATE_19BIT };

static int
nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
		     bfd_byte *contents, bfd_vma addr)
{
  unsigned long insn = bfd_getb32 (contents + addr);

  if (insn & 0x80000000)
    return 2;

  return 4;
}

/* Set the gp relax range.  We have to measure the safe range
   to do gp relaxation.  */

static void
relax_range_measurement (bfd *abfd)
{
  asection *sec_f, *sec_b;
  /* For upper bound.   */
  bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
  bfd_vma align;
  static int decide_relax_range = 0;
  int i;
  int range_number = sizeof (sdata_init_range) / sizeof (sdata_init_range[0]);

  if (decide_relax_range)
    return;
  decide_relax_range = 1;

  if (sda_rela_sec == NULL)
    {
      /* Since there is no data sections, we assume the range is page size.  */
      for (i = 0; i < range_number; i++)
	{
	  sdata_range[i][0] = sdata_init_range[i] - 0x1000;
	  sdata_range[i][1] = sdata_init_range[i] - 0x1000;
	}
      return;
    }

  /* Get the biggest alignment power after the gp located section.  */
  sec_f = sda_rela_sec->output_section;
  sec_b = sec_f->next;
  align = 0;
  while (sec_b != NULL)
    {
      if ((unsigned)(1 << sec_b->alignment_power) > align)
	align = (1 << sec_b->alignment_power);
      sec_b = sec_b->next;
    }

  /* I guess we can not determine the section before
     gp located section, so we assume the align is max page size.  */
  for (i = 0; i < range_number; i++)
    {
      sdata_range[i][1] = sdata_init_range[i] - align;
      BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
      sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
      BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
    }
}

/* These are macros used to check flags encoded in r_addend.
   They are only used by nds32_elf_relax_section ().  */
#define GET_SEQ_LEN(addend)     ((addend) & 0x000000ff)
#define IS_1ST_CONVERT(addend)  ((addend) & 0x80000000)
#define IS_OPTIMIZE(addend)     ((addend) & 0x40000000)
#define IS_16BIT_ON(addend)     ((addend) & 0x20000000)

/* Relax LONGCALL1 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* There are 3 variations for LONGCALL1
     case 4-4-2; 16-bit on, optimize off or optimize for space
     sethi ta, hi20(symbol)     ; LONGCALL1/HI20
     ori   ta, ta, lo12(symbol) ; LO12S0
     jral5 ta                   ;

     case 4-4-4; 16-bit off, optimize don't care
     sethi ta, hi20(symbol)     ; LONGCALL1/HI20
     ori   ta, ta, lo12(symbol) ; LO12S0
     jral  ta                   ;

     case 4-4-4; 16-bit on, optimize for speed
     sethi ta, hi20(symbol)     ; LONGCALL1/HI20
     ori   ta, ta, lo12(symbol) ; LO12S0
     jral  ta                   ;
     Check code for -mlong-calls output.  */

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */

  bfd_vma laddr;
  int seq_len;	/* Original length of instruction sequence.  */
  uint32_t insn;
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
  int pic_ext_target = 0;
  bfd_signed_vma foff;
  uint16_t insn16;

  irelend = internal_relocs + sec->reloc_count;
  seq_len = GET_SEQ_LEN (irel->r_addend);
  laddr = irel->r_offset;
  *insn_len = seq_len;

  hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					   R_NDS32_HI20_RELA, laddr);
  lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					   R_NDS32_LO12S0_ORI_RELA,
					   laddr + 4);

  if (hi_irelfn == irelend || lo_irelfn == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGCALL1 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
			   &pic_ext_target);

  /* This condition only happened when symbol is undefined.  */
  if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
      || foff >= CONSERVATIVE_24BIT_S1)
    return FALSE;

  /* Relax to: jal symbol; 25_PCREL */
  /* For simplicity of coding, we are going to modify the section
     contents, the section relocs, and the BFD symbol table.  We
     must tell the rest of the code not to free up this
     information.  It would be possible to instead create a table
     of changes which have to be made, as is done in coff-mips.c;
     that would be more work, but would require less memory when
     the linker is run.  */

  /* Replace the long call with a jal.  */
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
			       R_NDS32_25_PCREL_RELA);
  irel->r_addend = hi_irelfn->r_addend;

  /* We don't resolve this here but resolve it in relocate_section.  */
  insn = INSN_JAL;
  bfd_putb32 (insn, contents + irel->r_offset);

  hi_irelfn->r_info =
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
  lo_irelfn->r_info =
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
  *insn_len = 4;

  if (seq_len & 0x2)
    {
      insn16 = NDS32_NOP16;
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
      lo_irelfn->r_info =
	ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
      *insn_len += 2;
    }
  return TRUE;
}

#define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
/* Relax LONGCALL2 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* bltz  rt, .L1   ; LONGCALL2
     jal   symbol   ; 25_PCREL
     .L1: */

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */

  bfd_vma laddr;
  uint32_t insn;
  Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
  int pic_ext_target = 0;
  bfd_signed_vma foff;

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;
  i1_irelfn =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_25_PCREL_RELA, laddr + 4);

  if (i1_irelfn == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGCALL2 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  insn = bfd_getb32 (contents + laddr);

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr,
			   &pic_ext_target);

  if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
      || foff >= CONSERVATIVE_16BIT_S1)
    return FALSE;

  /* Relax to	bgezal   rt, label ; 17_PCREL
     or		bltzal   rt, label ; 17_PCREL */

  /* Convert to complimentary conditional call.  */
  insn = CONVERT_CONDITION_CALL (insn);

  /* For simplicity of coding, we are going to modify the section
     contents, the section relocs, and the BFD symbol table.  We
     must tell the rest of the code not to free up this
     information.  It would be possible to instead create a table
     of changes which have to be made, as is done in coff-mips.c;
     that would be more work, but would require less memory when
     the linker is run.  */

  /* Clean unnessary relocations.  */
  i1_irelfn->r_info =
    ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
  cond_irelfn =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_17_PCREL_RELA, laddr);
  if (cond_irelfn != irelend)
    cond_irelfn->r_info =
      ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);

  /* Replace the long call with a bgezal.  */
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
			       R_NDS32_17_PCREL_RELA);
  irel->r_addend = i1_irelfn->r_addend;

  bfd_putb32 (insn, contents + irel->r_offset);

  *insn_len = 4;
  return TRUE;
}

/* Relax LONGCALL3 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* There are 3 variations for LONGCALL3
     case 4-4-4-2; 16-bit on, optimize off or optimize for space
     bltz  rt,   $1                ; LONGCALL3
     sethi ta,   hi20(symbol)      ; HI20
     ori   ta, ta,  lo12(symbol)   ; LO12S0
     jral5 ta                      ;
     $1

     case 4-4-4-4; 16-bit off, optimize don't care
     bltz  rt,   $1                ; LONGCALL3
     sethi ta,   hi20(symbol)      ; HI20
     ori   ta, ta,  lo12(symbol)   ; LO12S0
     jral  ta                      ;
     $1

     case 4-4-4-4; 16-bit on, optimize for speed
     bltz  rt,   $1                ; LONGCALL3
     sethi ta,   hi20(symbol)      ; HI20
     ori   ta, ta,  lo12(symbol)   ; LO12S0
     jral  ta                      ;
     $1 */

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */

  bfd_vma laddr;
  int seq_len;	/* Original length of instruction sequence.  */
  uint32_t insn;
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
  int pic_ext_target = 0;
  bfd_signed_vma foff;
  uint16_t insn16;

  irelend = internal_relocs + sec->reloc_count;
  seq_len = GET_SEQ_LEN (irel->r_addend);
  laddr = irel->r_offset;
  *insn_len = seq_len;

  hi_irelfn =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_HI20_RELA, laddr + 4);
  lo_irelfn =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_LO12S0_ORI_RELA, laddr + 8);

  if (hi_irelfn == irelend || lo_irelfn == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGCALL3 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
			   &pic_ext_target);

  if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
      || foff >= CONSERVATIVE_24BIT_S1)
    return FALSE;

  insn = bfd_getb32 (contents + laddr);
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
    {
      /* Relax to  bgezal   rt, label ; 17_PCREL
	 or	   bltzal   rt, label ; 17_PCREL */

      /* Convert to complimentary conditional call.  */
      insn = CONVERT_CONDITION_CALL (insn);
      bfd_putb32 (insn, contents + irel->r_offset);

      *insn_len = 4;
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
      hi_irelfn->r_info =
	ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
      lo_irelfn->r_info =
	ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);

      cond_irelfn =
	find_relocs_at_address_addr (irel, internal_relocs, irelend,
				     R_NDS32_17_PCREL_RELA, laddr);
      if (cond_irelfn != irelend)
	{
	  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
					      R_NDS32_17_PCREL_RELA);
	  cond_irelfn->r_addend = hi_irelfn->r_addend;
	}

      if (seq_len & 0x2)
	{
	  insn16 = NDS32_NOP16;
	  bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
	  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
					    R_NDS32_INSN16);
	  hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
	  insn_len += 2;
	}
    }
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
    {
      /* Relax to the following instruction sequence
	 bltz  rt,   $1 ; LONGCALL2
	 jal   symbol   ; 25_PCREL
	 $1	*/
      *insn_len = 8;
      insn = INSN_JAL;
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);

      hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
					R_NDS32_25_PCREL_RELA);
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);

      lo_irelfn->r_info =
	ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);

      if (seq_len & 0x2)
	{
	  insn16 = NDS32_NOP16;
	  bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
	  lo_irelfn->r_info =
	    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
	  lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
	  insn_len += 2;
	}
    }
  return TRUE;
}

/* Relax LONGJUMP1 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* There are 3 variations for LONGJUMP1
     case 4-4-2; 16-bit bit on, optimize off or optimize for space
     sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
     ori   ta, ta, lo12(symbol)  ; LO12S0
     jr5   ta                    ;

     case 4-4-4; 16-bit off, optimize don't care
     sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
     ori   ta, ta, lo12(symbol)  ; LO12S0
     jr    ta                    ;

     case 4-4-4; 16-bit on, optimize for speed
     sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
     ori   ta, ta, lo12(symbol)  ; LO12S0
     jr    ta                    ;	*/

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */

  bfd_vma laddr;
  int seq_len;	/* Original length of instruction sequence.  */
  int insn16_on;	/* 16-bit on/off.  */
  uint32_t insn;
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
  int pic_ext_target = 0;
  bfd_signed_vma foff;
  uint16_t insn16;
  unsigned long reloc;

  irelend = internal_relocs + sec->reloc_count;
  seq_len = GET_SEQ_LEN (irel->r_addend);
  laddr = irel->r_offset;
  *insn_len = seq_len;
  insn16_on = IS_16BIT_ON (irel->r_addend);

  hi_irelfn =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_HI20_RELA, laddr);
  lo_irelfn =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
  if (hi_irelfn == irelend || lo_irelfn == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGJUMP1 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
			   &pic_ext_target);

  if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
      || foff < -CONSERVATIVE_24BIT_S1)
    return FALSE;

  if (insn16_on && foff >= -ACCURATE_8BIT_S1
      && foff < ACCURATE_8BIT_S1 && (seq_len & 0x2))
    {
      /* j8	label */
      /* 16-bit on, but not optimized for speed.  */
      reloc = R_NDS32_9_PCREL_RELA;
      insn16 = INSN_J8;
      bfd_putb16 (insn16, contents + irel->r_offset);
      *insn_len = 2;
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
    }
  else
    {
      /* j     label */
      reloc = R_NDS32_25_PCREL_RELA;
      insn = INSN_J;
      bfd_putb32 (insn, contents + irel->r_offset);
      *insn_len = 4;
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
      irel->r_addend = 0;
    }

  hi_irelfn->r_info =
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
  lo_irelfn->r_info =
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);

  if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
    {
      insn16 = NDS32_NOP16;
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
      lo_irelfn->r_info =
	ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
		      R_NDS32_INSN16);
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
      *insn_len += 2;
    }
  return TRUE;
}

/* Revert condition branch.  This function does not check if the input
   instruction is condition branch or not.  */

static void
nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
			   uint16_t *re_insn16, uint32_t *re_insn)
{
  uint32_t comp_insn = 0;
  uint16_t comp_insn16 = 0;

  if (insn)
    {
      if (N32_OP6 (insn) == N32_OP6_BR1)
	{
	  /* beqs label.  */
	  comp_insn = (insn ^ 0x4000) & 0xffffc000;
	  if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
	    {
	      /* Insn can be contracted to 16-bit implied r5.  */
	      comp_insn16 =
		(comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
	      comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
	    }
	}
      else if (N32_OP6 (insn) == N32_OP6_BR3)
	{
	  /* bnec $ta, imm11, label.  */
	  comp_insn = (insn ^ 0x80000) & 0xffffff00;
	}
      else
	{
	  comp_insn = (insn ^ 0x10000) & 0xffffc000;
	  if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
	      || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
	    {
	      if (N32_IS_RT3 (insn))
		{
		  /* Insn can be contracted to 16-bit.  */
		  comp_insn16 =
		    (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
		  comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
		}
	      else if (N32_RT5 (insn) == REG_R15)
		{
		  /* Insn can be contracted to 16-bit.  */
		  comp_insn16 =
		    (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
		}
	    }
	}
    }
  else
    {
      switch ((insn16 & 0xf000) >> 12)
	{
	case 0xc:
	  /* beqz38 or bnez38 */
	  comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
	  comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
	  comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
	  break;

	case 0xd:
	  /* beqs38 or bnes38 */
	  comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
	  comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
	  comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
	    | (REG_R5 << 15);
	  break;

	case 0xe:
	  /* beqzS8 or bnezS8 */
	  comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
	  comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
	  comp_insn |= REG_R15 << 20;
	  break;

	default:
	  break;
	}
    }
  if (comp_insn && re_insn)
    *re_insn = comp_insn;
  if (comp_insn16 && re_insn16)
    *re_insn16 = comp_insn16;
}

/* Relax LONGJUMP2 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* There are 3 variations for LONGJUMP2
     case 2-4;  1st insn convertible, 16-bit on,
     optimize off or optimize for space
     bnes38  rt, ra, $1 ; LONGJUMP2
     j       label      ; 25_PCREL
     $1:

     case 4-4; 1st insn not convertible
     bne  rt, ra, $1 ; LONGJUMP2
     j    label      ; 25_PCREL
     $1:

     case 4-4; 1st insn convertible, 16-bit on, optimize for speed
     bne  rt, ra, $1 ; LONGJUMP2
     j    label      ; 25_PCREL
     $1: */

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */

  bfd_vma laddr;
  int seq_len;	/* Original length of instruction sequence.  */
  Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
  int pic_ext_target = 0, first_size;
  unsigned int i;
  bfd_signed_vma foff;
  uint32_t insn, re_insn = 0;
  uint16_t insn16, re_insn16 = 0;
  unsigned long reloc, cond_reloc;

  enum elf_nds32_reloc_type checked_types[] =
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };

  irelend = internal_relocs + sec->reloc_count;
  seq_len = GET_SEQ_LEN (irel->r_addend);
  laddr = irel->r_offset;
  *insn_len = seq_len;
  first_size = (seq_len == 6) ? 2 : 4;

  i2_irelfn =
    find_relocs_at_address_addr (irel, internal_relocs,
				 irelend, R_NDS32_25_PCREL_RELA,
				 laddr + first_size);

  for (i = 0; i < sizeof (checked_types) / sizeof(checked_types[0]); i++)
    {
      cond_irelfn =
	find_relocs_at_address_addr (irel, internal_relocs, irelend,
				     checked_types[i], laddr);
      if (cond_irelfn != irelend)
	break;
    }

  if (i2_irelfn == irelend || cond_irelfn == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGJUMP2 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff =
    calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr,
		      &pic_ext_target);
  if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
      || foff >= CONSERVATIVE_16BIT_S1)
    return FALSE;

  /* Get the all corresponding instructions.  */
  if (first_size == 4)
    {
      insn = bfd_getb32 (contents + laddr);
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
    }
  else
    {
      insn16 = bfd_getb16 (contents + laddr);
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
    }

  if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
      && foff < ACCURATE_8BIT_S1 - first_size)
    {
      if (first_size == 4)
	{
	  /* Don't convert it to 16-bit now, keep this as relaxable for
	     ``label reloc; INSN16''.  */

	  /* Save comp_insn32 to buffer.  */
	  bfd_putb32 (re_insn, contents + irel->r_offset);
	  *insn_len = 4;
	  reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
	    R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
	  cond_reloc = R_NDS32_INSN16;
	}
      else
	{
	  bfd_putb16 (re_insn16, contents + irel->r_offset);
	  *insn_len = 2;
	  reloc = R_NDS32_9_PCREL_RELA;
	  cond_reloc = R_NDS32_NONE;
	}
    }
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
	   && (foff >= -(ACCURATE_14BIT_S1 - first_size)
	       && foff < ACCURATE_14BIT_S1 - first_size))
    {
      /* beqs     label    ; 15_PCREL */
      bfd_putb32 (re_insn, contents + irel->r_offset);
      *insn_len = 4;
      reloc = R_NDS32_15_PCREL_RELA;
      cond_reloc = R_NDS32_NONE;
    }
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
	   && foff >= -CONSERVATIVE_16BIT_S1
	   && foff < CONSERVATIVE_16BIT_S1)
    {
      /* beqz     label ; 17_PCREL */
      bfd_putb32 (re_insn, contents + irel->r_offset);
      *insn_len = 4;
      reloc = R_NDS32_17_PCREL_RELA;
      cond_reloc = R_NDS32_NONE;
    }
  else
    return FALSE;

  /* Set all relocations.  */
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
  irel->r_addend = i2_irelfn->r_addend;

  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
				      cond_reloc);
  cond_irelfn->r_addend = 0;

  if ((seq_len ^ *insn_len ) & 0x2)
    {
      insn16 = NDS32_NOP16;
      bfd_putb16 (insn16, contents + irel->r_offset + 4);
      i2_irelfn->r_offset = 4;
      i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
					R_NDS32_INSN16);
      i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
      *insn_len += 2;
    }
  else
    i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
				      R_NDS32_NONE);
  return TRUE;
}

/* Relax LONGJUMP3 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* There are 5 variations for LONGJUMP3
     case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
     optimize off or optimize for space
     bnes38   rt, ra, $1            ; LONGJUMP3
     sethi    ta, hi20(symbol)      ; HI20
     ori      ta, ta, lo12(symbol)  ; LO12S0
     jr5      ta                    ;
     $1:                            ;

     case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
     bnes38   rt, ra, $1           ; LONGJUMP3
     sethi    ta, hi20(symbol)     ; HI20
     ori      ta, ta, lo12(symbol) ; LO12S0
     jr5      ta                   ;
     $1:                           ; LABEL

     case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
     optimize off or optimize for space
     bne   rt, ra, $1           ; LONGJUMP3
     sethi ta, hi20(symbol)     ; HI20
     ori   ta, ta, lo12(symbol) ; LO12S0
     jr5   ta                   ;
     $1:                        ;

     case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
     16-bit off if no INSN16
     bne   rt, ra, $1           ; LONGJUMP3
     sethi ta, hi20(symbol)     ; HI20
     ori   ta, ta, lo12(symbol) ; LO12S0
     jr    ta                   ;
     $1:                        ;

     case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
     16-bit off if no INSN16
     bne   rt, ra, $1           ; LONGJUMP3
     sethi ta, hi20(symbol)     ; HI20
     ori   ta, ta, lo12(symbol) ; LO12S0
     jr    ta                   ;
     $1:                        ; LABEL */

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */
  enum elf_nds32_reloc_type checked_types[] =
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };

  int reloc_off = 0, cond_removed = 0, convertible;
  bfd_vma laddr;
  int seq_len;	/* Original length of instruction sequence.  */
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
  int pic_ext_target = 0, first_size;
  unsigned int i;
  bfd_signed_vma foff;
  uint32_t insn, re_insn = 0;
  uint16_t insn16, re_insn16 = 0;
  unsigned long reloc, cond_reloc;

  irelend = internal_relocs + sec->reloc_count;
  seq_len = GET_SEQ_LEN (irel->r_addend);
  laddr = irel->r_offset;
  *insn_len = seq_len;

  convertible = IS_1ST_CONVERT (irel->r_addend);

  if (convertible)
    first_size = 2;
  else
    first_size = 4;

  /* Get all needed relocations.  */
  hi_irelfn =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_HI20_RELA, laddr + first_size);
  lo_irelfn =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_LO12S0_ORI_RELA,
				 laddr + first_size + 4);

  for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
    {
      cond_irelfn =
	find_relocs_at_address_addr (irel, internal_relocs, irelend,
				     checked_types[i], laddr);
      if (cond_irelfn != irelend)
	break;
    }

  if (hi_irelfn == irelend || lo_irelfn == irelend || cond_irelfn == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGJUMP3 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
			   &pic_ext_target);

  if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
      || foff >= CONSERVATIVE_24BIT_S1)
    return FALSE;

  /* Get the all corresponding instructions.  */
  if (first_size == 4)
    {
      insn = bfd_getb32 (contents + laddr);
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
    }
  else
    {
      insn16 = bfd_getb16 (contents + laddr);
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
    }

  /* For simplicity of coding, we are going to modify the section
     contents, the section relocs, and the BFD symbol table.  We
     must tell the rest of the code not to free up this
     information.  It would be possible to instead create a table
     of changes which have to be made, as is done in coff-mips.c;
     that would be more work, but would require less memory when
     the linker is run.  */

  if (re_insn16 && foff >= -ACCURATE_8BIT_S1 - first_size
      && foff < ACCURATE_8BIT_S1 - first_size)
    {
      if (!(seq_len & 0x2))
	{
	  /* Don't convert it to 16-bit now, keep this as relaxable
	     for ``label reloc; INSN1a''6.  */
	  /* Save comp_insn32 to buffer.  */
	  bfd_putb32 (re_insn, contents + irel->r_offset);
	  *insn_len = 4;
	  reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
	    R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
	  cond_reloc = R_NDS32_INSN16;
	}
      else
	{
	  /* Not optimize for speed; convert sequence to 16-bit.  */
	  /* Save comp_insn16 to buffer.  */
	  bfd_putb16 (re_insn16, contents + irel->r_offset);
	  *insn_len = 2;
	  reloc = R_NDS32_9_PCREL_RELA;
	  cond_reloc = R_NDS32_NONE;
	}
      cond_removed = 1;
    }
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
	   && (foff >= -(ACCURATE_14BIT_S1 - first_size)
	       && foff < ACCURATE_14BIT_S1 - first_size))
    {
      /* beqs     label    ; 15_PCREL */
      bfd_putb32 (re_insn, contents + irel->r_offset);
      *insn_len = 4;
      reloc = R_NDS32_15_PCREL_RELA;
      cond_reloc = R_NDS32_NONE;
      cond_removed = 1;
    }
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
	   && foff >= -CONSERVATIVE_16BIT_S1
	   && foff < CONSERVATIVE_16BIT_S1)
    {
      /* beqz     label ; 17_PCREL */
      bfd_putb32 (re_insn, contents + irel->r_offset);
      *insn_len = 4;
      reloc = R_NDS32_17_PCREL_RELA;
      cond_reloc = R_NDS32_NONE;
      cond_removed = 1;
    }
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
	   && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
    {
      /* Relax to one of the following 3 variations

	 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
	 for space
	 bnes38  rt, $1 ; LONGJUMP2
	 j       label  ; 25_PCREL
	 $1

	 case 4-4; 1st insn not convertible, others don't care
	 bne   rt, ra, $1 ; LONGJUMP2
	 j     label      ; 25_PCREL
	 $1

	 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
	 bne   rt, ra, $1 ; LONGJUMP2
	 j     label      ; 25_PCREL
	 $1 */

      /* Offset for first instruction.  */

      /* Use j label as second instruction.  */
      *insn_len = 4 + first_size;
      insn = INSN_J;
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);
      reloc = R_NDS32_LONGJUMP2;
      cond_reloc = R_NDS32_25_PLTREL;
    }
    else
      return FALSE;

    if (cond_removed == 1)
      {
	/* Set all relocations.  */
	irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
	irel->r_addend = hi_irelfn->r_addend;

	cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
					    cond_reloc);
	cond_irelfn->r_addend = 0;
	hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
					  R_NDS32_NONE);
      }
    else
      {
	irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
	irel->r_addend = irel->r_addend;
	hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
					  cond_reloc);
      }

  if ((seq_len ^ *insn_len ) & 0x2)
    {
      insn16 = NDS32_NOP16;
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
      lo_irelfn->r_offset = *insn_len;
      lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
					R_NDS32_INSN16);
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
      *insn_len += 2;
    }
  else
    lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
				      R_NDS32_NONE);
  return TRUE;
}

/* Relax LONGCALL4 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* The pattern for LONGCALL4.  Support for function cse.
     sethi ta, hi20(symbol)	; LONGCALL4/HI20
     ori   ta, ta, lo12(symbol)	; LO12S0_ORI/PTR
     jral  ta			; PTR_RES/EMPTY/INSN16  */

  bfd_vma laddr;
  uint32_t insn;
  Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
  Elf_Internal_Rela *irelend;
  int pic_ext_target = 0;
  bfd_signed_vma foff;

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */
  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					 R_NDS32_HI20_RELA, laddr);

  if (hi_irel == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGCALL4 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
			   &pic_ext_target);

  /* This condition only happened when symbol is undefined.  */
  if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
      || foff >= CONSERVATIVE_24BIT_S1)
    return FALSE;

  /* Relax to: jal symbol; 25_PCREL */
  /* For simplicity of coding, we are going to modify the section
     contents, the section relocs, and the BFD symbol table.  We
     must tell the rest of the code not to free up this
     information.  It would be possible to instead create a table
     of changes which have to be made, as is done in coff-mips.c;
     that would be more work, but would require less memory when
     the linker is run.  */

  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					  R_NDS32_PTR_RESOLVED, irel->r_addend);
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					  R_NDS32_EMPTY, irel->r_addend);

  if (ptr_irel == irelend || em_irel == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGCALL4 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }
  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
  insn = bfd_getb32 (contents + irel->r_addend);
  if (insn & 0x80000000)
    return FALSE;

  /* Replace the long call with a jal.  */
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
				  R_NDS32_25_PCREL_RELA);
  ptr_irel->r_addend = 1;

  /* We don't resolve this here but resolve it in relocate_section.  */
  insn = INSN_JAL;
  bfd_putb32 (insn, contents + em_irel->r_offset);

  irel->r_info =
    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);

  /* If there is function cse, HI20 can not remove now.  */
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					   R_NDS32_LONGCALL4, laddr);
  if (call_irel == irelend)
    {
      *insn_len = 0;
      hi_irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
    }

  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					  R_NDS32_INSN16, irel->r_addend);
  if (insn_irel != irelend)
    insn_irel->r_info =
      ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);

  return TRUE;
}

/* Relax LONGCALL5 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* The pattern for LONGCALL5.
     bltz  rt, .L1	; LONGCALL5/17_PCREL
     jal   symbol	; 25_PCREL
     .L1:  */

  bfd_vma laddr;
  uint32_t insn;
  Elf_Internal_Rela *cond_irel, *irelend;
  int pic_ext_target = 0;
  bfd_signed_vma foff;

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;
  insn = bfd_getb32 (contents + laddr);

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */
  cond_irel =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_25_PCREL_RELA, irel->r_addend);
  if (cond_irel == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGCALL5 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
			   &pic_ext_target);

  if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
      || foff >= CONSERVATIVE_16BIT_S1)
    return FALSE;

  /* Relax to	bgezal   rt, label ; 17_PCREL
     or		bltzal   rt, label ; 17_PCREL */

  /* Convert to complimentary conditional call.  */
  insn = CONVERT_CONDITION_CALL (insn);

  /* For simplicity of coding, we are going to modify the section
     contents, the section relocs, and the BFD symbol table.  We
     must tell the rest of the code not to free up this
     information.  It would be possible to instead create a table
     of changes which have to be made, as is done in coff-mips.c;
     that would be more work, but would require less memory when
     the linker is run.  */

  /* Modify relocation and contents.  */
  cond_irel->r_info =
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);

  /* Replace the long call with a bgezal.  */
  bfd_putb32 (insn, contents + cond_irel->r_offset);
  *insn_len = 0;

  /* Clean unnessary relocations.  */
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);

  cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					   R_NDS32_17_PCREL_RELA, laddr);
  cond_irel->r_info =
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);

  return TRUE;
}

/* Relax LONGCALL6 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* The pattern for LONGCALL6.
     bltz  rt,   .L1			; LONGCALL6/17_PCREL
     sethi ta,   hi20(symbol)		; HI20/PTR
     ori   ta, ta,  lo12(symbol)	; LO12S0_ORI/PTR
     jral  ta				; PTR_RES/EMPTY/INSN16
     .L1  */

  bfd_vma laddr;
  uint32_t insn;
  Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
  int pic_ext_target = 0;
  bfd_signed_vma foff;

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					 R_NDS32_EMPTY, irel->r_addend);

  if (em_irel == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGCALL6 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
			   &pic_ext_target);

  if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
      || foff >= CONSERVATIVE_24BIT_S1)
    return FALSE;

  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
  insn = bfd_getb32 (contents + irel->r_addend);
  if (insn & 0x80000000)
    return FALSE;

  insn = bfd_getb32 (contents + laddr);
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
    {
      /* Relax to  bgezal   rt, label ; 17_PCREL
	 or	   bltzal   rt, label ; 17_PCREL */

      /* Convert to complimentary conditional call.  */
      *insn_len = 0;
      insn = CONVERT_CONDITION_CALL (insn);
      bfd_putb32 (insn, contents + em_irel->r_offset);

      em_irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);

      /* Set resolved relocation.  */
      cond_irel =
	find_relocs_at_address_addr (irel, internal_relocs, irelend,
				     R_NDS32_PTR_RESOLVED, irel->r_addend);
      if (cond_irel == irelend)
	{
	  (*_bfd_error_handler)
	    ("%B: warning: R_NDS32_LONGCALL6 points to unrecognized"
	     "reloc at 0x%lx.", abfd, (long) irel->r_offset);
	  return FALSE;
	}
      cond_irel->r_addend = 1;

      /* Clear relocations.  */

      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);

      cond_irel =
	find_relocs_at_address_addr (irel, internal_relocs, irelend,
				     R_NDS32_17_PCREL_RELA, laddr);
      if (cond_irel != irelend)
	cond_irel->r_info =
	  ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);

      cond_irel =
	find_relocs_at_address_addr (irel, internal_relocs, irelend,
				     R_NDS32_INSN16, irel->r_addend);
      if (cond_irel != irelend)
	cond_irel->r_info =
	  ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);

    }
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
    {
      /* Relax to the following instruction sequence
	 bltz  rt, .L1	; LONGCALL2/17_PCREL
	 jal   symbol	; 25_PCREL/PTR_RES
	 .L1  */
      *insn_len = 4;
      /* Convert instruction.  */
      insn = INSN_JAL;
      bfd_putb32 (insn, contents + em_irel->r_offset);

      /* Convert relocations.  */
      em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
				      R_NDS32_25_PCREL_RELA);
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);

      /* Set resolved relocation.  */
      cond_irel =
	find_relocs_at_address_addr (irel, internal_relocs, irelend,
				     R_NDS32_PTR_RESOLVED, irel->r_addend);
      if (cond_irel == irelend)
	{
	  (*_bfd_error_handler)
	    ("%B: warning: R_NDS32_LONGCALL6 points to unrecognized"
	     "reloc at 0x%lx.", abfd, (long) irel->r_offset);
	  return FALSE;
	}
      cond_irel->r_addend = 1;

      cond_irel =
	find_relocs_at_address_addr (irel, internal_relocs, irelend,
				     R_NDS32_INSN16, irel->r_addend);
      if (cond_irel != irelend)
	cond_irel->r_info =
	  ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
    }
  return TRUE;
}

/* Relax LONGJUMP4 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* The pattern for LONGJUMP4.
     sethi ta, hi20(symbol)	; LONGJUMP4/HI20
     ori   ta, ta, lo12(symbol)	; LO12S0_ORI/PTR
     jr    ta			; PTR_RES/INSN16/EMPTY  */

  bfd_vma laddr;
  int seq_len;	/* Original length of instruction sequence.  */
  uint32_t insn;
  Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
  int pic_ext_target = 0;
  bfd_signed_vma foff;

  irelend = internal_relocs + sec->reloc_count;
  seq_len = GET_SEQ_LEN (irel->r_addend);
  laddr = irel->r_offset;
  *insn_len = seq_len;

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */

  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					 R_NDS32_HI20_RELA, laddr);

  if (hi_irel == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGJUMP4 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
			   &pic_ext_target);

  if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
      || foff < -CONSERVATIVE_24BIT_S1)
    return FALSE;

  /* Convert it to "j label", it may be converted to j8 in the final
     pass of relaxation.  Therefore, we do not consider this currently.  */
  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					  R_NDS32_PTR_RESOLVED, irel->r_addend);
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					 R_NDS32_EMPTY, irel->r_addend);

  if (ptr_irel == irelend || em_irel == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGJUMP4 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  em_irel->r_info =
    ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
  ptr_irel->r_addend = 1;

  /* Write instruction.  */
  insn = INSN_J;
  bfd_putb32 (insn, contents + em_irel->r_offset);

  /* Clear relocations.  */
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);

  /* If there is function cse, HI20 can not remove now.  */
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					   R_NDS32_LONGJUMP4, laddr);
  if (call_irel == irelend)
    {
      *insn_len = 0;
      hi_irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
    }

  return TRUE;
}

/* Relax LONGJUMP5 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   int *seq_len, bfd_byte *contents,
			   Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* There are 2 variations for LONGJUMP5
     case 2-4;  1st insn convertible, 16-bit on.
     bnes38  rt, ra, .L1	; LONGJUMP5/9_PCREL/INSN16
     j       label		; 25_PCREL/INSN16
     $1:

     case 4-4; 1st insn not convertible
     bne  rt, ra, .L1	; LONGJUMP5/15_PCREL/INSN16
     j    label		; 25_PCREL/INSN16
     .L1:  */

  bfd_vma laddr;
  Elf_Internal_Rela *cond_irel,  *irelend;
  int pic_ext_target = 0;
  unsigned int i;
  bfd_signed_vma foff;
  uint32_t insn, re_insn = 0;
  uint16_t insn16, re_insn16 = 0;
  unsigned long reloc;

  enum elf_nds32_reloc_type checked_types[] =
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */

  cond_irel =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_25_PCREL_RELA, irel->r_addend);
  if (cond_irel == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGJUMP5 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
			   &pic_ext_target);

  if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
      || foff >= CONSERVATIVE_16BIT_S1)
    return FALSE;

  /* Get the all corresponding instructions.  */
  insn = bfd_getb32 (contents + laddr);
  /* Check instruction size.  */
  if (insn & 0x80000000)
    {
      *seq_len = 0;
      insn16 = insn >> 16;
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
    }
  else
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);

  if (N32_OP6 (re_insn) == N32_OP6_BR1
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
    {
      /* beqs label ; 15_PCREL.  */
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
      reloc = R_NDS32_15_PCREL_RELA;
    }
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
	   && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
    {
      /* beqz label ; 17_PCREL.  */
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
      reloc = R_NDS32_17_PCREL_RELA;
    }
  else if ( N32_OP6 (re_insn) == N32_OP6_BR3
	   && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
    {
      /* beqc label ; 9_PCREL.  */
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
      reloc = R_NDS32_WORD_9_PCREL_RELA;
    }
  else
    return FALSE;

  /* Set all relocations.  */
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);

  /* Clean relocations.  */
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
    {
      cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					       checked_types[i], laddr);
      if (cond_irel != irelend)
	{
	  if (*seq_len == 0
	      && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
	    {
	      /* If the branch instruction is 2 byte, it cannot remove
		 directly.  Only convert it to nop16 and remove it after
		 checking alignment issue.  */
	      insn16 = NDS32_NOP16;
	      bfd_putb16 (insn16, contents + laddr);
	      cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
	    }
	  else
	    cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
					      R_NDS32_NONE);
	}
    }
  *insn_len = 0;

  return TRUE;
}

/* Relax LONGJUMP6 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   int *seq_len, bfd_byte *contents,
			   Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* There are 5 variations for LONGJUMP6
     case : 2-4-4-4; 1st insn convertible, 16-bit on.
     bnes38   rt, ra, .L1		; LONGJUMP6/15_PCREL/INSN16
     sethi    ta, hi20(symbol)		; HI20/PTR
     ori      ta, ta, lo12(symbol)	; LO12S0_ORI/PTR
     jr       ta			; PTR_RES/INSN16/EMPTY
     .L1:

     case : 4-4-4-4; 1st insn not convertible, 16-bit on.
     bne   rt, ra, .L1		; LONGJUMP6/15_PCREL/INSN16
     sethi ta, hi20(symbol)	; HI20/PTR
     ori   ta, ta, lo12(symbol)	; LO12S0_ORI/PTR
     jr    ta			; PTR_RES/INSN16/EMPTY
     .L1:  */

  enum elf_nds32_reloc_type checked_types[] =
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };

  int reloc_off = 0, cond_removed = 0;
  bfd_vma laddr;
  Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
  int pic_ext_target = 0;
  unsigned int i;
  bfd_signed_vma foff;
  uint32_t insn, re_insn = 0;
  uint16_t insn16, re_insn16 = 0;
  unsigned long reloc;

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					 R_NDS32_EMPTY, irel->r_addend);

  if (em_irel == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGJUMP6 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
			   &pic_ext_target);

  if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
      || foff >= CONSERVATIVE_24BIT_S1)
    return FALSE;

  insn = bfd_getb32 (contents + laddr);
  /* Check instruction size.  */
  if (insn & 0x80000000)
    {
      *seq_len = 0;
      insn16 = insn >> 16;
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
    }
  else
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);

  /* For simplicity of coding, we are going to modify the section
     contents, the section relocs, and the BFD symbol table.  We
     must tell the rest of the code not to free up this
     information.  It would be possible to instead create a table
     of changes which have to be made, as is done in coff-mips.c;
     that would be more work, but would require less memory when
     the linker is run.  */

  if (N32_OP6 (re_insn) == N32_OP6_BR1
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
    {
      /* beqs     label    ; 15_PCREL */
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
      reloc = R_NDS32_15_PCREL_RELA;
      cond_removed = 1;
    }
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
	   && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
    {
      /* beqz     label ; 17_PCREL */
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
      reloc = R_NDS32_17_PCREL_RELA;
      cond_removed = 1;
    }
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
	   && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
    {
      /* Relax to one of the following 2 variations

	 case 2-4;  1st insn convertible, 16-bit on.
	 bnes38  rt, ra, .L1	; LONGJUMP5/9_PCREL/INSN16
	 j       label		; 25_PCREL/INSN16
	 $1:

	 case 4-4; 1st insn not convertible
	 bne  rt, ra, .L1	; LONGJUMP5/15_PCREL/INSN16
	 j    label		; 25_PCREL/INSN16
	 .L1:  */

      /* Use j label as second instruction.  */
      insn = INSN_J;
      reloc = R_NDS32_25_PCREL_RELA;
      bfd_putb32 (insn, contents + em_irel->r_offset);
    }
  else
    return FALSE;

  /* Set all relocations.  */
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);

  cond_irel =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
  cond_irel->r_addend = 1;

  /* Use INSN16 of first branch instruction to distinguish if keeping
     INSN16 of final instruction or not.  */
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					   R_NDS32_INSN16, irel->r_offset);
  if (insn_irel == irelend)
    {
      /* Clean the final INSN16.  */
      insn_irel =
	find_relocs_at_address_addr (irel, internal_relocs, irelend,
				     R_NDS32_INSN16, em_irel->r_offset);
      insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
					R_NDS32_NONE);
    }

  if (cond_removed == 1)
    {
      *insn_len = 0;

      /* Clear relocations.  */
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);

      for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
	{
	  cond_irel =
	    find_relocs_at_address_addr (irel, internal_relocs, irelend,
					 checked_types[i], laddr);
	  if (cond_irel != irelend)
	    {
	      if (*seq_len == 0
		  && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
		{
		  /* If the branch instruction is 2 byte, it cannot remove
		     directly.  Only convert it to nop16 and remove it after
		     checking alignment issue.  */
		  insn16 = NDS32_NOP16;
		  bfd_putb16 (insn16, contents + laddr);
		  cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
		}
	      else
		cond_irel->r_info =
		  ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
	    }
	}
    }
  else
    {
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
				   R_NDS32_LONGJUMP5);
    }

  return TRUE;
}

/* Relax LONGJUMP7 relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   int *seq_len, bfd_byte *contents,
			   Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  /* There are 2 variations for LONGJUMP5
     case 2-4;  1st insn convertible, 16-bit on.
     movi55  ta, imm11		; LONGJUMP7/INSN16
     beq     rt, ta, label	; 15_PCREL

     case 4-4; 1st insn not convertible
     movi55  ta, imm11		; LONGJUMP7/INSN16
     beq     rt, ta, label	; 15_PCREL  */

  bfd_vma laddr;
  Elf_Internal_Rela *cond_irel,  *irelend, *insn_irel;
  int pic_ext_target = 0;
  bfd_signed_vma foff;
  uint32_t insn, re_insn = 0;
  uint16_t insn16;
  uint32_t imm11;

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;

  /* Get the reloc for the address from which the register is
     being loaded.  This reloc will tell us which function is
     actually being called.  */

  cond_irel =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_15_PCREL_RELA, irel->r_addend);
  if (cond_irel == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LONGJUMP7 points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
      return FALSE;
    }

  /* Get the value of the symbol referred to by the reloc.  */
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
			   &pic_ext_target);

  if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_8BIT_S1
      || foff >= CONSERVATIVE_8BIT_S1)
    return FALSE;

  /* Get the first instruction for its size.  */
  insn = bfd_getb32 (contents + laddr);
  if (insn & 0x80000000)
    {
      *seq_len = 0;
      /* Get the immediate from movi55.  */
      imm11 = N16_IMM5S (insn >> 16);
    }
  else
    {
      /* Get the immediate from movi.  */
      imm11 = N32_IMM20S (insn);
    }

  /* Get the branch instruction.  */
  insn = bfd_getb32 (contents + irel->r_addend);
  /* Convert instruction to BR3.  */
  if ((insn >> 14) & 0x1)
    re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
  else
    re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);

  bfd_putb32 (re_insn, contents + cond_irel->r_offset);

  /* Set all relocations.  */
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
				    R_NDS32_WORD_9_PCREL_RELA);

  /* Clean relocations.  */
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					   R_NDS32_INSN16, irel->r_offset);
  if (insn_irel != irelend)
    {
      if (*seq_len == 0)
	{
	  /* If the first insntruction is 16bit, convert it to nop16.  */
	  insn16 = NDS32_NOP16;
	  bfd_putb16 (insn16, contents + laddr);
	  insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
	}
      else
	cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
					  R_NDS32_NONE);
    }
  *insn_len = 0;

  return TRUE;
}

#define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)

/* Relax LOADSTORE relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
			   asection *sec, Elf_Internal_Rela *irel,
			   Elf_Internal_Rela *internal_relocs, int *insn_len,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr, int load_store_relax)
{
  int eliminate_sethi = 0, range_type;
  unsigned int i;
  bfd_vma local_sda, laddr;
  int seq_len;	/* Original length of instruction sequence.  */
  uint32_t insn;
  Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
  bfd_vma access_addr = 0;
  bfd_vma range_l = 0, range_h = 0;	/* Upper/lower bound.  */
  enum elf_nds32_reloc_type checked_types[] =
    { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
      R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
      R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
      R_NDS32_TLS_LE_HI20
    };

  irelend = internal_relocs + sec->reloc_count;
  seq_len = GET_SEQ_LEN (irel->r_addend);
  laddr = irel->r_offset;
  *insn_len = seq_len;

  /* Get the high part relocation.  */
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
    {
      hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
					       checked_types[i], laddr);
      if (hi_irelfn != irelend)
	break;
    }

  if (hi_irelfn == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_LOADSTORE points to unrecognized"
	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
	return FALSE;
    }

  range_type = GET_LOADSTORE_RANGE (irel->r_addend);
  nds32_elf_final_sda_base (sec->output_section->owner,
			    link_info, &local_sda, FALSE);

  switch (ELF32_R_TYPE (hi_irelfn->r_info))
    {
    case R_NDS32_HI20_RELA:
      insn = bfd_getb32 (contents + laddr);
      access_addr =
	calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);

      if (range_type == NDS32_LOADSTORE_IMM)
	{
	  struct elf_link_hash_entry *h = NULL;
	  int indx;

	  if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
	    {
	      indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
	      h = elf_sym_hashes (abfd)[indx];
	    }

	  if ((access_addr < CONSERVATIVE_20BIT)
	      && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
	    {
	      eliminate_sethi = 1;
	      break;
	    }

	  /* This is avoid to relax symbol address which is fixed
	     relocations.  Ex: _stack.  */
	  if (h && bfd_is_abs_section (h->root.u.def.section))
	    return FALSE;
	}

      if (!load_store_relax)
	return FALSE;

      /* Case for set gp register.  */
      if (N32_RT5 (insn) == REG_GP)
	break;

      if (range_type == NDS32_LOADSTORE_FLOAT_S
	  || range_type == NDS32_LOADSTORE_FLOAT_S)
	{
	  range_l = sdata_range[0][0];
	  range_h = sdata_range[0][1];
	}
      else
	{
	  range_l = sdata_range[1][0];
	  range_h = sdata_range[1][1];
	}
      break;

    case R_NDS32_GOT_HI20:
      access_addr =
	calculate_got_memory_address (abfd, link_info, hi_irelfn, symtab_hdr);

      /* If this symbol is not in .got, the return value will be -1.
	 Since the gp value is set to SDA_BASE but not GLOBAL_OFFSET_TABLE,
	 a negative offset is allowed.  */
      if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
	  && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
	eliminate_sethi = 1;
      break;

    case R_NDS32_PLT_GOTREL_HI20:
      access_addr = calculate_plt_memory_address (abfd, link_info, isymbuf,
						  hi_irelfn, symtab_hdr);

      if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
	  && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
	eliminate_sethi = 1;
      break;

    case R_NDS32_GOTOFF_HI20:
      access_addr =
	calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);

      if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
	  && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
	eliminate_sethi = 1;
      break;

    case R_NDS32_GOTPC_HI20:
      /* The access_addr must consider r_addend of hi_irel.  */
      access_addr = sec->output_section->vma + sec->output_offset
	+ irel->r_offset + hi_irelfn->r_addend;

      if ((bfd_signed_vma) (local_sda - access_addr) < CONSERVATIVE_20BIT
	  && (bfd_signed_vma) (local_sda - access_addr) >= -CONSERVATIVE_20BIT)
	eliminate_sethi = 1;
      break;

    case R_NDS32_TLS_LE_HI20:
      access_addr =
	calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
      BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
      access_addr -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
      if ((range_type == NDS32_LOADSTORE_IMM)
	  && (bfd_signed_vma) (access_addr) < CONSERVATIVE_20BIT
	  && (bfd_signed_vma) (access_addr) >= -CONSERVATIVE_20BIT)
	eliminate_sethi = 1;
      break;

    default:
      return FALSE;
    }

  /* Delete sethi instruction.  */
  if (eliminate_sethi == 1
      || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
      || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
    {
      hi_irelfn->r_info =
	ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
      *insn_len = 0;
    }
  return TRUE;
}

/* Relax LO12 relocation for nds32_elf_relax_section.  */

static void
nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
		      asection *sec, Elf_Internal_Rela *irel,
		      Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
		      Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
{
  uint32_t insn;
  bfd_vma local_sda, laddr;
  unsigned long reloc;
  bfd_vma access_addr;
  bfd_vma range_l = 0, range_h = 0;	/* Upper/lower bound.  */
  Elf_Internal_Rela *irelfn = NULL, *irelend;
  struct elf_link_hash_entry *h = NULL;
  int indx;

  /* For SDA base relative relaxation.  */
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
			    &local_sda, FALSE);

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;
  insn = bfd_getb32 (contents + laddr);

  if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
    return;

  access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);

  if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
    {
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
      h = elf_sym_hashes (abfd)[indx];
    }

  if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
      && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
    {
      reloc = R_NDS32_20_RELA;
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
      insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
      bfd_putb32 (insn, contents + laddr);
    }
  /* This is avoid to relax symbol address which is fixed
     relocations.  Ex: _stack.  */
  else if (N32_OP6 (insn) == N32_OP6_ORI
	   && h && bfd_is_abs_section (h->root.u.def.section))
    return;
  else
    {
      range_l = sdata_range[1][0];
      range_h = sdata_range[1][1];
      switch (ELF32_R_TYPE (irel->r_info))
	{
	case R_NDS32_LO12S0_RELA:
	  reloc = R_NDS32_SDA19S0_RELA;
	  break;
	case R_NDS32_LO12S1_RELA:
	  reloc = R_NDS32_SDA18S1_RELA;
	  break;
	case R_NDS32_LO12S2_RELA:
	  reloc = R_NDS32_SDA17S2_RELA;
	  break;
	case R_NDS32_LO12S2_DP_RELA:
	  range_l = sdata_range[0][0];
	  range_h = sdata_range[0][1];
	  reloc = R_NDS32_SDA12S2_DP_RELA;
	  break;
	case R_NDS32_LO12S2_SP_RELA:
	  range_l = sdata_range[0][0];
	  range_h = sdata_range[0][1];
	  reloc = R_NDS32_SDA12S2_SP_RELA;
	  break;
	default:
	  return;
	}

      /* There are range_h and range_l because linker has to promise
	 all sections move cross one page together.  */
      if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
	  || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
	{
	  if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
	    {
	      /* Maybe we should add R_NDS32_INSN16 reloc type here
		 or manually do some optimization.  sethi can't be
		 eliminated when updating $gp so the relative ori
		 needs to be preserved.  */
	      return;
	    }
	  if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
					&insn))
	    return;
	  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
	  bfd_putb32 (insn, contents + laddr);

	  irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
					   R_NDS32_INSN16);
	  /* SDA17 must keep INSN16 for converting fp_as_gp.  */
	  if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
	    irelfn->r_info =
	      ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);

	}
    }
  return;
}

/* Relax low part of PIC instruction pattern.  */

static void
nds32_elf_relax_piclo12 (struct bfd_link_info *link_info, bfd *abfd,
			 asection *sec, Elf_Internal_Rela *irel,
			 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			 Elf_Internal_Shdr *symtab_hdr)
{
  uint32_t insn;
  bfd_vma local_sda, laddr;
  bfd_signed_vma foff;
  unsigned long reloc;

  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
			    &local_sda, FALSE);
  laddr = irel->r_offset;
  insn = bfd_getb32 (contents + laddr);

  if (N32_OP6 (insn) != N32_OP6_ORI)
    return;

  if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12)
    {
      foff = calculate_got_memory_address (abfd, link_info, irel,
					   symtab_hdr) - local_sda;
      reloc = R_NDS32_GOT20;
    }
  else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12)
    {
      foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
					   symtab_hdr) - local_sda;
      reloc = R_NDS32_PLT_GOTREL_LO20;
    }
  else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12)
    {
      foff = calculate_memory_address (abfd, irel, isymbuf,
				       symtab_hdr) - local_sda;
      reloc = R_NDS32_GOTOFF;
    }
  else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12)
    {
      foff = local_sda - sec->output_section->vma + sec->output_offset
	+ irel->r_offset + irel->r_addend;
      reloc = R_NDS32_GOTPC20;
    }
  else
    return;

  if ((foff < CONSERVATIVE_20BIT) && (foff >= -CONSERVATIVE_20BIT))
    {
      /* Turn into MOVI.  */
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
      insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
      bfd_putb32 (insn, contents + laddr);
    }
}

/* Relax low part of LE TLS instruction pattern.  */

static void
nds32_elf_relax_letlslo12 (struct bfd_link_info *link_info, bfd *abfd,
			   Elf_Internal_Rela *irel,
			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			   Elf_Internal_Shdr *symtab_hdr)
{
  uint32_t insn;
  bfd_vma laddr;
  bfd_signed_vma foff;
  unsigned long reloc;

  laddr = irel->r_offset;
  foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
  BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
  foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
  insn = bfd_getb32 (contents + laddr);

  if ( (bfd_signed_vma) (foff) < CONSERVATIVE_20BIT
      && (bfd_signed_vma) (foff) >= -CONSERVATIVE_20BIT)
    {
      /* Pattern sethi-ori transform to movi.  */
      reloc = R_NDS32_TLS_LE_20;
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
      insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
      bfd_putb32 (insn, contents + laddr);
    }
}

/* Relax LE TLS calculate address instruction pattern.  */

static void
nds32_elf_relax_letlsadd (struct bfd_link_info *link_info, bfd *abfd,
			  asection *sec, Elf_Internal_Rela *irel,
			  Elf_Internal_Rela *internal_relocs,
			  bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			  Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
{
  /* Local TLS non-pic
     sethi    ta, hi20(symbol@tpoff)      ; TLS_LE_HI20
     ori      ta, ta, lo12(symbol@tpoff)  ; TLS_LE_LO12
     add      ra, ta, tp                  ; TLS_LE_ADD */

  uint32_t insn;
  bfd_vma laddr;
  bfd_signed_vma foff;
  Elf_Internal_Rela *i1_irelfn, *irelend;

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;
  insn = bfd_getb32 (contents + laddr);
  i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
				      R_NDS32_PTR_RESOLVED);
  foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
  BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
  foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);

  /* The range is +/-16k.  */
  if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
      && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
    {
      /* Transform add to addi.  */
      insn = N32_TYPE2 (ADDI, N32_RT5 (insn), N32_RB5 (insn), 0);
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);

      bfd_putb32 (insn, contents + laddr);
      if (i1_irelfn != irelend)
	{
	  i1_irelfn->r_addend |= 1;
	  *again = TRUE;
	}
    }
}

/* Relax LE TLS load store instruction pattern.  */

static void
nds32_elf_relax_letlsls (struct bfd_link_info *link_info, bfd *abfd,
			 asection *sec, Elf_Internal_Rela *irel,
			 Elf_Internal_Rela *internal_relocs,
			 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
{

  uint32_t insn;
  bfd_vma laddr;
  bfd_signed_vma foff;
  Elf_Internal_Rela *i1_irelfn, *irelend;
  int success = 0;

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;
  insn = bfd_getb32 (contents + laddr);
  i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
				      R_NDS32_PTR_RESOLVED);
  foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
  BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
  foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);

  switch ((N32_OP6 (insn) << 8) | (insn & 0xff))
    {
    case (N32_OP6_MEM << 8) | N32_MEM_LB:
    case (N32_OP6_MEM << 8) | N32_MEM_SB:
    case (N32_OP6_MEM << 8) | N32_MEM_LBS:
      /* The range is +/-16k.  */
      if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
	  && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
	{
	  insn =
	    ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
	  irel->r_info =
	    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
	  success = 1;
	  break;
	}
    case (N32_OP6_MEM << 8) | N32_MEM_LH:
    case (N32_OP6_MEM << 8) | N32_MEM_SH:
    case (N32_OP6_MEM << 8) | N32_MEM_LHS:
      /* The range is +/-32k.  */
      if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S1
	  && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S1)
	{
	  insn =
	    ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
	  irel->r_info =
	    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S1);
	  success = 1;
	  break;
	}
    case (N32_OP6_MEM << 8) | N32_MEM_LW:
    case (N32_OP6_MEM << 8) | N32_MEM_SW:
      /* The range is +/-64k.  */
      if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S2
	  && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S2)
	{
	  insn =
	    ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
	  irel->r_info =
	    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S2);
	  success = 1;
	  break;
	}
    default:
      break;
    }

  if (success)
    {
      bfd_putb32 (insn, contents + laddr);
      if (i1_irelfn != irelend)
	{
	  i1_irelfn->r_addend |= 1;
	  *again = TRUE;
	}
    }
}

/* Relax PTR relocation for nds32_elf_relax_section.  */

static bfd_boolean
nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
		     Elf_Internal_Rela *internal_relocs, int *insn_len,
		     int *seq_len, bfd_byte *contents)
{
  Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;

  irelend = internal_relocs + sec->reloc_count;

  re_irel =
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
				 R_NDS32_PTR_RESOLVED, irel->r_addend);

  if (re_irel == irelend)
    {
      (*_bfd_error_handler)
	("%B: warning: R_NDS32_PTR points to unrecognized reloc at 0x%lx.",
	 abfd, (long) irel->r_offset);
      return FALSE;
    }

  if (re_irel->r_addend != 1)
    return FALSE;

  /* Pointed target is relaxed and no longer needs this void *,
     change the type to NONE.  */
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);

  /* Find PTR_COUNT to decide remove it or not.  If PTR_COUNT does
     not exist, it means only count 1 and remove it directly.  */
  /* TODO: I hope we can obsolate R_NDS32_COUNT in the future.  */
  count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
				       R_NDS32_PTR_COUNT);
  ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
				     R_NDS32_PTR);
  if (count_irel != irelend)
    {
      if (--count_irel->r_addend > 0)
	return FALSE;
    }

  if (ptr_irel != irelend)
    return FALSE;

  /* If the PTR_COUNT is already 0, remove current instruction.  */
  *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
  *insn_len = 0;
  return TRUE;
}

/* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section.  */

static void
nds32_elf_relax_pltgot_suff (struct bfd_link_info *link_info, bfd *abfd,
			     asection *sec, Elf_Internal_Rela *irel,
			     Elf_Internal_Rela *internal_relocs,
			     bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			     Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
{
  uint32_t insn;
  bfd_signed_vma foff;
  Elf_Internal_Rela *i1_irelfn, *irelend;
  bfd_vma local_sda, laddr;

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;
  insn = bfd_getb32 (contents + laddr);

  /* FIXME: It's a little trouble to turn JRAL5 to JAL since
     we need additional space.  It might be help if we could
     borrow some space from instructions to be eliminated
     such as sethi, ori, add.  */
  if (insn & 0x80000000)
    return;

  if (nds32_elf_check_dup_relocs
      (irel, internal_relocs, irelend, R_NDS32_PLT_GOT_SUFF))
    return;

  i1_irelfn =
    find_relocs_at_address (irel, internal_relocs, irelend,
			    R_NDS32_PTR_RESOLVED);

  /* FIXIT 090606
     The boundary should be reduced since the .plt section hasn't
     been created and the address of specific entry is still unknown
     Maybe the range between the function call and the begin of the
     .text section can be used to decide if the .plt is in the range
     of function call.  */

  if (N32_OP6 (insn) == N32_OP6_ALU1
      && N32_SUB5 (insn) == N32_ALU1_ADD)
    {
      /* Get the value of the symbol referred to by the reloc.  */
      nds32_elf_final_sda_base (sec->output_section->owner, link_info,
				&local_sda, FALSE);
      foff = (bfd_signed_vma) (calculate_plt_memory_address
			       (abfd, link_info, isymbuf, irel,
				symtab_hdr) - local_sda);
      /* This condition only happened when symbol is undefined.  */
      if (foff == 0)
	return;

      if (foff < -CONSERVATIVE_19BIT || foff >= CONSERVATIVE_19BIT)
	return;
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
				   R_NDS32_PLT_GOTREL_LO19);
      /* addi.gp */
      insn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
    }
  else if (N32_OP6 (insn) == N32_OP6_JREG
	   && N32_SUB5 (insn) == N32_JREG_JRAL)
    {
      /* Get the value of the symbol referred to by the reloc.  */
      foff =
	calculate_plt_offset (abfd, sec, link_info, isymbuf, irel, symtab_hdr);
      /* This condition only happened when symbol is undefined.  */
      if (foff == 0)
	return;
      if (foff < -CONSERVATIVE_24BIT_S1 || foff >= CONSERVATIVE_24BIT_S1)
	return;
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_25_PLTREL);
      insn = INSN_JAL;
    }
  else
    return;

  bfd_putb32 (insn, contents + laddr);
  if (i1_irelfn != irelend)
    {
      i1_irelfn->r_addend |= 1;
      *again = TRUE;
    }
}

/* Relax GOT_SUFF relocation for nds32_elf_relax_section.  */

static void
nds32_elf_relax_got_suff (struct bfd_link_info *link_info, bfd *abfd,
			  asection *sec, Elf_Internal_Rela *irel,
			  Elf_Internal_Rela *internal_relocs,
			  bfd_byte *contents, Elf_Internal_Shdr *symtab_hdr,
			  bfd_boolean *again)
{
  uint32_t insn;
  bfd_signed_vma foff;
  Elf_Internal_Rela *i1_irelfn, *irelend;
  bfd_vma local_sda, laddr;

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;
  insn = bfd_getb32 (contents + laddr);
  if (insn & 0x80000000)
    return;

  if (nds32_elf_check_dup_relocs
      (irel, internal_relocs, irelend, R_NDS32_GOT_SUFF))
    return;

  i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
				      R_NDS32_PTR_RESOLVED);

  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
			    &local_sda, FALSE);
  foff = calculate_got_memory_address (abfd, link_info, irel,
				       symtab_hdr) - local_sda;

  if (foff < CONSERVATIVE_19BIT && foff >= -CONSERVATIVE_19BIT)
    {
      /* Turn LW to LWI.GP.  Change relocation type to R_NDS32_GOT_REL.  */
      insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_GOT17S2_RELA);
      bfd_putb32 (insn, contents + laddr);
      if (i1_irelfn != irelend)
	{
	  i1_irelfn->r_addend |= 1;
	  *again = TRUE;
	}
    }
}

/* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section.  */

static void
nds32_elf_relax_gotoff_suff (struct bfd_link_info *link_info, bfd *abfd,
			     asection *sec, Elf_Internal_Rela *irel,
			     Elf_Internal_Rela *internal_relocs,
			     bfd_byte *contents, Elf_Internal_Sym *isymbuf,
			     Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
{
  int opc_insn_gotoff;
  uint32_t insn;
  bfd_signed_vma foff;
  Elf_Internal_Rela *i1_irelfn, *i2_irelfn, *irelend;
  bfd_vma local_sda, laddr;

  irelend = internal_relocs + sec->reloc_count;
  laddr = irel->r_offset;
  insn = bfd_getb32 (contents + laddr);

  if (insn & 0x80000000)
    return;

  if (nds32_elf_check_dup_relocs
      (irel, internal_relocs, irelend, R_NDS32_GOTOFF_SUFF))
    return;

  i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
				      R_NDS32_PTR_RESOLVED);
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
			    &local_sda, FALSE);
  foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
  foff = foff - local_sda;

  if (foff >= CONSERVATIVE_19BIT || foff < -CONSERVATIVE_19BIT)
    return;

  /* Concatenate opcode and sub-opcode for switch case.
     It may be MEM or ALU1.  */
  opc_insn_gotoff = (N32_OP6 (insn) << 8) | (insn & 0xff);
  switch (opc_insn_gotoff)
    {
    case (N32_OP6_MEM << 8) | N32_MEM_LW:
      /* 4-byte aligned.  */
      insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
      break;
    case (N32_OP6_MEM << 8) | N32_MEM_SW:
      insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
      break;
    case (N32_OP6_MEM << 8) | N32_MEM_LH:
      /* 2-byte aligned.  */
      insn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
      break;
    case (N32_OP6_MEM << 8) | N32_MEM_LHS:
      insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (18));
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
      break;
    case (N32_OP6_MEM << 8) | N32_MEM_SH:
      insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (19));
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
      break;
    case (N32_OP6_MEM << 8) | N32_MEM_LB:
      /* 1-byte aligned.  */
      insn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
      break;
    case (N32_OP6_MEM << 8) | N32_MEM_LBS:
      insn = N32_TYPE1 (LBGP, N32_RT5 (insn), __BIT (19));
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
      break;
    case (N32_OP6_MEM << 8) | N32_MEM_SB:
      insn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
      break;
    case (N32_OP6_ALU1 << 8) | N32_ALU1_ADD:
      insn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
      irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
      break;
    default:
      return;
    }

  bfd_putb32 (insn, contents + laddr);
  if (i1_irelfn != irelend)
    {
      i1_irelfn->r_addend |= 1;
      *again = TRUE;
    }
  if ((i2_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
					   R_NDS32_INSN16)) != irelend)
    i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);

}

static bfd_boolean
nds32_relax_adjust_label (bfd *abfd, asection *sec,
			  Elf_Internal_Rela *internal_relocs,
			  bfd_byte *contents,
			  nds32_elf_blank_t **relax_blank_list,
			  int optimize, int opt_size)
{
  /* This code block is used to adjust 4-byte alignment by relax a pair
     of instruction a time.

     It recognizes three types of relocations.
     1. R_NDS32_LABEL - a aligment.
     2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
     3. is_16bit_NOP () - remove a 16-bit instruction.  */

  /* TODO: It seems currently implementation only support 4-byte aligment.
     We should handle any-aligment.  */

  Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
  Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
  Elf_Internal_Rela rel_temp;
  Elf_Internal_Rela *irelend;
  bfd_vma address;
  uint16_t insn16;

  /* Checking for branch relaxation relies on the relocations to
     be sorted on 'r_offset'.  This is not guaranteed so we must sort.  */
  nds32_insertion_sort (internal_relocs, sec->reloc_count,
			sizeof (Elf_Internal_Rela), compar_reloc);

  irelend = internal_relocs + sec->reloc_count;

  /* Force R_NDS32_LABEL before R_NDS32_INSN16.  */
  /* FIXME: Can we generate the right order in assembler?
     So we don't have to swapping them here.  */

  for (label_rel = internal_relocs, insn_rel = internal_relocs;
       label_rel < irelend; label_rel++)
    {
      if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
	continue;

      /* Find the first reloc has the same offset with label_rel.  */
      while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
	insn_rel++;

      for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
	   insn_rel++)
	/* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
	   address.  */
	if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
	  break;

      if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
	  && insn_rel < label_rel)
	{
	  /* Swap the two reloc if the R_NDS32_INSN16 is
	     before R_NDS32_LABEL.  */
	  memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
	  memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
	  memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
	}
    }

  label_rel = NULL;
  insn_rel = NULL;
  /* If there were a sequence of R_NDS32_LABEL end up with .align 2
     or higher, remove other R_NDS32_LABEL with lower alignment.
     If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
     then the R_NDS32_LABEL sequence is broke.  */
  for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
    {
      if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
	{
	  if (label_rel == NULL)
	    {
	      if (tmp_rel->r_addend < 2)
		label_rel = tmp_rel;
	      continue;
	    }
	  else if (tmp_rel->r_addend > 1)
	    {
	      /* Remove all LABEL relocation from label_rel to tmp_rel
		 including relocations with same offset as tmp_rel.  */
	      for (tmp2_rel = label_rel; tmp2_rel < tmp_rel
		   || tmp2_rel->r_offset == tmp_rel->r_offset; tmp2_rel++)
		{
		  if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
		      && tmp2_rel->r_addend < 2)
		    tmp2_rel->r_info =
		      ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
				    R_NDS32_NONE);
		}
	      label_rel = NULL;
	    }
	}
      else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
	{
	  /* A new INSN16 which can be converted, so clear label_rel.  */
	  if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
				   irelend, &insn16)
	      || is_16bit_NOP (abfd, sec, tmp_rel))
	    label_rel = NULL;
	}
    }

  label_rel = NULL;
  insn_rel = NULL;
  /* Optimized for speed and nothing has not been relaxed.
     It's time to align labels.
     We may convert a 16-bit instruction right before a label to
     32-bit, in order to align the label if necessary
     all reloc entries has been sorted by r_offset.  */
  for (irel = internal_relocs; irel < irelend; irel++)
    {
      if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
	  && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
	continue;

      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
	{
	  /* A new INSN16 found, resize the old one.  */
	  if (is_convert_32_to_16
	      (abfd, sec, irel, internal_relocs, irelend, &insn16)
	      || is_16bit_NOP (abfd, sec, irel))
	    {
	      if (insn_rel)
		{
		  /* Previous INSN16 reloc exists, reduce its
		     size to 16-bit.  */
		  if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
					   irelend, &insn16))
		    {
		      nds32_elf_write_16 (abfd, contents, insn_rel,
					  internal_relocs, irelend, insn16);

		      if (!insert_nds32_elf_blank_recalc_total
			  (relax_blank_list, insn_rel->r_offset + 2, 2))
			return FALSE;
		    }
		  else if (is_16bit_NOP (abfd, sec, insn_rel))
		    {
		      if (!insert_nds32_elf_blank_recalc_total
			  (relax_blank_list, insn_rel->r_offset, 2))
			return FALSE;
		    }
		  insn_rel->r_info =
		    ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
		}
	      /* Save the new one for later use.  */
	      insn_rel = irel;
	    }
	  else
	    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
					 R_NDS32_NONE);
	}
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
	{
	  /* Search for label.  */
	  int force_relax = 0;

	  /* Label on 16-bit instruction or optimization
	     needless, just reset this reloc.  */
	  insn16 = bfd_getb16 (contents + irel->r_offset);
	  if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
	    {
	      irel->r_info =
		ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
	      continue;
	    }

	  address =
	    irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
							irel->r_offset, 1);

	  if (!insn_rel)
	    {
	      /* Check if there is case which can not be aligned.  */
	      if (irel->r_addend == 2 && address & 0x2)
		return FALSE;
	      continue;
	    }

	  /* Try to align this label.  */

	  if ((irel->r_addend & 0x1f) < 2)
	    {
	      /* Check if there is a INSN16 at the same address.
		 Label_rel always seats before insn_rel after
		 our sort.  */

	      /* Search for INSN16 at LABEL location.  If INSN16 is at
		 same location and this LABEL alignment is lower than 2,
		 the INSN16 can be converted to 2-byte.  */
	      for (tmp_rel = irel;
		   tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
		   tmp_rel++)
		{
		  if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
		      && (is_convert_32_to_16
			  (abfd, sec, tmp_rel, internal_relocs,
			   irelend, &insn16)
			  || is_16bit_NOP (abfd, sec, tmp_rel)))
		    {
		      force_relax = 1;
		      break;
		    }
		}
	    }

	  if (force_relax || irel->r_addend == 1 || address & 0x2)
	    {
	      /* Label not aligned.  */
	      /* Previous reloc exists, reduce its size to 16-bit.  */
	      if (is_convert_32_to_16 (abfd, sec, insn_rel,
				       internal_relocs, irelend, &insn16))
		{
		  nds32_elf_write_16 (abfd, contents, insn_rel,
				      internal_relocs, irelend, insn16);

		  if (!insert_nds32_elf_blank_recalc_total
		      (relax_blank_list, insn_rel->r_offset + 2, 2))
		    return FALSE;
		}
	      else if (is_16bit_NOP (abfd, sec, insn_rel))
		{
		  if (!insert_nds32_elf_blank_recalc_total
		      (relax_blank_list, insn_rel->r_offset, 2))
		    return FALSE;
		}

	    }
	  /* INSN16 reloc is used.  */
	  insn_rel = NULL;
	}
    }

  address =
    sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
  if (insn_rel && (address & 0x2 || opt_size))
    {
      if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
			       irelend, &insn16))
	{
	  nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
			      irelend, insn16);
	  if (!insert_nds32_elf_blank_recalc_total
	      (relax_blank_list, insn_rel->r_offset + 2, 2))
	    return FALSE;
	  insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
					   R_NDS32_NONE);
	}
      else if (is_16bit_NOP (abfd, sec, insn_rel))
	{
	  if (!insert_nds32_elf_blank_recalc_total
	      (relax_blank_list, insn_rel->r_offset, 2))
	    return FALSE;
	  insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
					   R_NDS32_NONE);
	}
    }
  insn_rel = NULL;
  return TRUE;
}

/* Pick relaxation round.  */

static int
nds32_elf_pick_relax (bfd_boolean init, asection *sec, bfd_boolean *again,
		      struct elf_nds32_link_hash_table *table,
		      struct bfd_link_info *link_info)
{
  static asection *final_sec, *first_sec = NULL;
  static bfd_boolean normal_again = FALSE;
  static bfd_boolean set = FALSE;
  static bfd_boolean first = TRUE;
  int round_table[] = {
      NDS32_RELAX_NORMAL_ROUND,
      NDS32_RELAX_JUMP_IFC_ROUND,
      NDS32_RELAX_EX9_BUILD_ROUND,
      NDS32_RELAX_EX9_REPLACE_ROUND,
  };
  static int pass = 0;
  static int relax_round;

  /* The new round.  */
  if (init && first_sec == sec)
    {
      set = TRUE;
      normal_again = FALSE;
    }

  if (first)
    {
      /* Run an empty run to get the final section.  */
      relax_round = NDS32_RELAX_EMPTY_ROUND;

      /* It has to enter relax again because we can
	 not make sure what the final turn is.  */
      *again = TRUE;

      first = FALSE;
      first_sec = sec;
    }

  if (!set)
    {
      /* Not reenter yet.  */
      final_sec = sec;
      return relax_round;
    }

  relax_round = round_table[pass];

  if (!init && relax_round == NDS32_RELAX_NORMAL_ROUND && *again)
    normal_again = TRUE;

  if (!init && final_sec == sec)
    {
      switch (relax_round)
	{
	case NDS32_RELAX_NORMAL_ROUND:
	  if (!normal_again)
	    {
	      /* Normal relaxation done.  */
	      if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
		{
		  pass++;
		  *again = TRUE;
		}
	      else if (table->target_optimize & NDS32_RELAX_EX9_ON)
		{
		  pass += 2;	/* NDS32_RELAX_EX9_BUILD_ROUND */
		  *again = TRUE;
		}
	      else if (table->ex9_import_file)
		{
		  /* Import ex9 table.  */
		  if (table->update_ex9_table)
		    pass += 2;	/* NDS32_RELAX_EX9_BUILD_ROUND */
		  else
		    pass += 3;	/* NDS32_RELAX_EX9_REPLACE_ROUND */
		  nds32_elf_ex9_import_table (link_info);
		  *again = TRUE;
		}
	    }
	  break;
	case NDS32_RELAX_JUMP_IFC_ROUND:
	  if (!nds32_elf_ifc_finish (link_info))
	    (*_bfd_error_handler) (_("error: Jump IFC Fail."));
	  if (table->target_optimize & NDS32_RELAX_EX9_ON)
	    {
	      pass++;
	      *again = TRUE;
	    }
	  break;
	case NDS32_RELAX_EX9_BUILD_ROUND:
	  nds32_elf_ex9_finish (link_info);
	  pass++;
	  *again = TRUE;
	  break;
	case NDS32_RELAX_EX9_REPLACE_ROUND:
	  if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
	    {
	      /* Do jump IFC optimization again.  */
	      if (!nds32_elf_ifc_finish (link_info))
		(*_bfd_error_handler) (_("error: Jump IFC Fail."));
	    }
	  break;
	default:
	  break;
	}
    }

  return relax_round;
}

static bfd_boolean
nds32_elf_relax_section (bfd *abfd, asection *sec,
			 struct bfd_link_info *link_info, bfd_boolean *again)
{
  nds32_elf_blank_t *relax_blank_list = NULL;
  Elf_Internal_Shdr *symtab_hdr;
  Elf_Internal_Rela *internal_relocs;
  Elf_Internal_Rela *irel;
  Elf_Internal_Rela *irelend;
  Elf_Internal_Sym *isymbuf = NULL;
  bfd_byte *contents = NULL;
  bfd_boolean result = TRUE;
  int optimize = 0;
  int opt_size = 0;
  uint32_t insn;
  uint16_t insn16;

  /* Target dependnet option.  */
  struct elf_nds32_link_hash_table *table;
  int load_store_relax;
  int relax_round;

  relax_blank_list = NULL;

  *again = FALSE;

  /* Nothing to do for
   * relocatable link or
   * non-relocatable section or
   * non-code section or
   * empty content or
   * no reloc entry.  */
  if (bfd_link_relocatable (link_info)
      || (sec->flags & SEC_RELOC) == 0
      || (sec->flags & SEC_EXCLUDE) == 1
      || (sec->flags & SEC_CODE) == 0
      || sec->size == 0)
    return TRUE;

  /* 09.12.11 Workaround.  */
  /*  We have to adjust align for R_NDS32_LABEL if needed.
      The adjust approach only can fix 2-byte align once.  */
  if (sec->alignment_power > 2)
    return TRUE;

  /* The optimization type to do.  */

  table = nds32_elf_hash_table (link_info);
  relax_round = nds32_elf_pick_relax (TRUE, sec, again, table, link_info);
  switch (relax_round)
    {
    case NDS32_RELAX_JUMP_IFC_ROUND:
      /* Here is the entrance of ifc jump relaxation.  */
      if (!nds32_elf_ifc_calc (link_info, abfd, sec))
	return FALSE;
      nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
      return TRUE;

    case NDS32_RELAX_EX9_BUILD_ROUND:
      /* Here is the entrance of ex9 relaxation.  There are two pass of
	 ex9 relaxation.  The one is to traverse all instructions and build
	 the hash table.  The other one is to compare instructions and replace
	 it by ex9.it.  */
      if (!nds32_elf_ex9_build_hash_table (abfd, sec, link_info))
	return FALSE;
      nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
      return TRUE;

    case NDS32_RELAX_EX9_REPLACE_ROUND:
      if (!nds32_elf_ex9_replace_instruction (link_info, abfd, sec))
	return FALSE;
      return TRUE;

    case NDS32_RELAX_EMPTY_ROUND:
      nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
      return TRUE;

    case NDS32_RELAX_NORMAL_ROUND:
    default:
      if (sec->reloc_count == 0)
	return TRUE;
      break;
    }

  /* The begining of general relaxation.  */

  if (is_SDA_BASE_set == 0)
    {
      bfd_vma gp;
      is_SDA_BASE_set = 1;
      nds32_elf_final_sda_base (sec->output_section->owner, link_info,
				&gp, FALSE);
      relax_range_measurement (abfd);
    }

  if (is_ITB_BASE_set == 0)
    {
      /* Set the _ITB_BASE_.  */
      if (!nds32_elf_ex9_itb_base (link_info))
	{
	  (*_bfd_error_handler) (_("%B: error: Cannot set _ITB_BASE_"), abfd);
	  bfd_set_error (bfd_error_bad_value);
	}
    }

  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  /* Relocations MUST be kept in memory, because relaxation adjust them.  */
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
					       TRUE /* keep_memory */);
  if (internal_relocs == NULL)
    goto error_return;

  irelend = internal_relocs + sec->reloc_count;
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
				 irelend, R_NDS32_RELAX_ENTRY);

  if (irel == irelend)
    return TRUE;

  if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
    {
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
	{
	  nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
	  return TRUE;
	}

      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
	optimize = 1;

      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
	opt_size = 1;
    }

  load_store_relax = table->load_store_relax;

  /* Get symbol table and section content.  */
  if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
    goto error_return;

  /* Do relax loop only when finalize is not done.
     Take care of relaxable relocs except INSN16.  */
  for (irel = internal_relocs; irel < irelend; irel++)
    {
      int seq_len;		/* Original length of instruction sequence.  */
      int insn_len = 0;		/* Final length of instruction sequence.  */
      bfd_boolean removed;

      insn = 0;
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
	  && (irel->r_addend & 0x1f) >= 2)
	optimize = 1;

      /* Relocation Types
	 R_NDS32_LONGCALL1	53
	 R_NDS32_LONGCALL2	54
	 R_NDS32_LONGCALL3	55
	 R_NDS32_LONGJUMP1	56
	 R_NDS32_LONGJUMP2	57
	 R_NDS32_LONGJUMP3	58
	 R_NDS32_LOADSTORE	59  */
      if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
	  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
	seq_len = GET_SEQ_LEN (irel->r_addend);

      /* Relocation Types
	 R_NDS32_LONGCALL4	107
	 R_NDS32_LONGCALL5	108
	 R_NDS32_LONGCALL6	109
	 R_NDS32_LONGJUMP4	110
	 R_NDS32_LONGJUMP5	111
	 R_NDS32_LONGJUMP6	112
	 R_NDS32_LONGJUMP7	113  */
      else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
	       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
	seq_len = 4;

	/* Relocation Types
	 R_NDS32_LO12S0_RELA		30
	 R_NDS32_LO12S1_RELA		29
	 R_NDS32_LO12S2_RELA		28
	 R_NDS32_LO12S2_SP_RELA		71
	 R_NDS32_LO12S2_DP_RELA		70
	 R_NDS32_GOT_LO12		46
	 R_NDS32_GOTOFF_LO12		50
	 R_NDS32_PLTREL_LO12		65
	 R_NDS32_PLT_GOTREL_LO12	67
	 R_NDS32_17IFC_PCREL_RELA	96
	 R_NDS32_GOT_SUFF		193
	 R_NDS32_GOTOFF_SUFF		194
	 R_NDS32_PLT_GOT_SUFF		195
	 R_NDS32_MULCALL_SUFF		196
	 R_NDS32_PTR			197  */
      else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
		&& ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
	       || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
		   && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS)
	seq_len = 0;
      else
	continue;

      insn_len = seq_len;
      removed = FALSE;

      switch (ELF32_R_TYPE (irel->r_info))
	{
	case R_NDS32_LONGCALL1:
	  removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
					       &insn_len, contents, isymbuf,
					       symtab_hdr);
	  break;
	case R_NDS32_LONGCALL2:
	  removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
					       &insn_len, contents, isymbuf,
					       symtab_hdr);
	  break;
	case R_NDS32_LONGCALL3:
	  removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
					       &insn_len, contents, isymbuf,
					       symtab_hdr);
	  break;
	case R_NDS32_LONGJUMP1:
	  removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
					       &insn_len, contents, isymbuf,
					       symtab_hdr);
	  break;
	case R_NDS32_LONGJUMP2:
	  removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
					       &insn_len, contents, isymbuf,
					       symtab_hdr);
	  break;
	case R_NDS32_LONGJUMP3:
	  removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
					       &insn_len, contents, isymbuf,
					       symtab_hdr);
	  break;
	case R_NDS32_LONGCALL4:
	  removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
					       &insn_len, contents, isymbuf,
					       symtab_hdr);
	  break;
	case R_NDS32_LONGCALL5:
	  removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
					       &insn_len, contents, isymbuf,
					       symtab_hdr);
	  break;
	case R_NDS32_LONGCALL6:
	  removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
					       &insn_len, contents, isymbuf,
					       symtab_hdr);
	  break;
	case R_NDS32_LONGJUMP4:
	  removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
					       &insn_len, contents, isymbuf,
					       symtab_hdr);
	  break;
	case R_NDS32_LONGJUMP5:
	  removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
					       &insn_len, &seq_len, contents,
					       isymbuf, symtab_hdr);
	  break;
	case R_NDS32_LONGJUMP6:
	  removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
					       &insn_len, &seq_len, contents,
					       isymbuf, symtab_hdr);
	  break;
	case R_NDS32_LONGJUMP7:
	  removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
					       &insn_len, &seq_len, contents,
					       isymbuf, symtab_hdr);
	  break;
	case R_NDS32_LOADSTORE:
	  removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
					       internal_relocs, &insn_len,
					       contents, isymbuf, symtab_hdr,
					       load_store_relax);
	  break;
	case R_NDS32_LO12S0_RELA:
	case R_NDS32_LO12S1_RELA:
	case R_NDS32_LO12S2_DP_RELA:
	case R_NDS32_LO12S2_SP_RELA:
	case R_NDS32_LO12S2_RELA:
	  /* Relax for low part.  */
	  nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
				contents, isymbuf, symtab_hdr);

	  /* It is impossible to delete blank, so just continue.  */
	  continue;
	case R_NDS32_GOT_LO12:
	case R_NDS32_GOTOFF_LO12:
	case R_NDS32_PLTREL_LO12:
	case R_NDS32_PLT_GOTREL_LO12:
	case R_NDS32_GOTPC_LO12:
	  /* Relax for PIC gp-relative low part.  */
	  nds32_elf_relax_piclo12 (link_info, abfd, sec, irel, contents,
				   isymbuf, symtab_hdr);

	  /* It is impossible to delete blank, so just continue.  */
	  continue;
	case R_NDS32_TLS_LE_LO12:
	  /* Relax for LE TLS low part.  */
	  nds32_elf_relax_letlslo12 (link_info, abfd, irel, contents,
				     isymbuf, symtab_hdr);

	  /* It is impossible to delete blank, so just continue.  */
	  continue;
	case R_NDS32_TLS_LE_ADD:
	  nds32_elf_relax_letlsadd (link_info, abfd, sec, irel, internal_relocs,
				    contents, isymbuf, symtab_hdr, again);
	  /* It is impossible to delete blank, so just continue.  */
	  continue;
	case R_NDS32_TLS_LE_LS:
	  nds32_elf_relax_letlsls (link_info, abfd, sec, irel, internal_relocs,
				   contents, isymbuf, symtab_hdr, again);
	  continue;
	case R_NDS32_PTR:
	  removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
					 &insn_len, &seq_len, contents);
	  break;
	case R_NDS32_PLT_GOT_SUFF:
	  nds32_elf_relax_pltgot_suff (link_info, abfd, sec, irel,
				       internal_relocs, contents,
				       isymbuf, symtab_hdr, again);
	  /* It is impossible to delete blank, so just continue.  */
	  continue;
	case R_NDS32_GOT_SUFF:
	  nds32_elf_relax_got_suff (link_info, abfd, sec, irel,
				    internal_relocs, contents,
				    symtab_hdr, again);
	  /* It is impossible to delete blank, so just continue.  */
	  continue;
	case R_NDS32_GOTOFF_SUFF:
	  nds32_elf_relax_gotoff_suff (link_info, abfd, sec, irel,
				       internal_relocs, contents,
				       isymbuf, symtab_hdr, again);
	  /* It is impossible to delete blank, so just continue.  */
	  continue;
	default:
	  continue;

	}
      if (removed && seq_len - insn_len > 0)
	{
	  if (!insert_nds32_elf_blank
	      (&relax_blank_list, irel->r_offset + insn_len,
	       seq_len - insn_len))
	    goto error_return;
	  *again = TRUE;
	}
    }

  calc_nds32_blank_total (relax_blank_list);

  if (table->relax_fp_as_gp)
    {
      if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
				 irelend, isymbuf))
	goto error_return;

      if (*again == FALSE)
	{
	  if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
					       irelend))
	    goto error_return;
	}
    }

  nds32_elf_pick_relax (FALSE, sec, again, table, link_info);

  if (*again == FALSE)
    {
      if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
				     &relax_blank_list, optimize, opt_size))
	goto error_return;
    }

  /* It doesn't matter optimize_for_space_no_align anymore.
       If object file is assembled with flag '-Os',
       the we don't adjust jump-destination on 4-byte boundary.  */

  if (relax_blank_list)
    {
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
      relax_blank_list = NULL;
    }

  if (*again == FALSE)
    {
      /* Closing the section, so we don't relax it anymore.  */
      bfd_vma sec_size_align;
      Elf_Internal_Rela *tmp_rel;

      /* Pad to alignment boundary.  Only handle current section alignment.  */
      sec_size_align = (sec->size + (~((-1) << sec->alignment_power)))
		       & ((-1) << sec->alignment_power);
      if ((sec_size_align - sec->size) & 0x2)
	{
	  insn16 = NDS32_NOP16;
	  bfd_putb16 (insn16, contents + sec->size);
	  sec->size += 2;
	}

      while (sec_size_align != sec->size)
	{
	  insn = NDS32_NOP32;
	  bfd_putb32 (insn, contents + sec->size);
	  sec->size += 4;
	}

      tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
					irelend, R_NDS32_RELAX_ENTRY);
      if (tmp_rel != irelend)
	tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;

      clean_nds32_elf_blank ();
    }

finish:
  if (internal_relocs != NULL
      && elf_section_data (sec)->relocs != internal_relocs)
    free (internal_relocs);

  if (contents != NULL
      && elf_section_data (sec)->this_hdr.contents != contents)
    free (contents);

  if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
    free (isymbuf);

  return result;

error_return:
  result = FALSE;
  goto finish;
}

static struct bfd_elf_special_section const nds32_elf_special_sections[] =
{
  {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
  {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
  {NULL, 0, 0, 0, 0}
};

static bfd_boolean
nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
			    struct bfd_link_info *info,
			    void *finfo ATTRIBUTE_UNUSED,
			    bfd_boolean (*func) (void *, const char *,
						 Elf_Internal_Sym *,
						 asection *,
						 struct elf_link_hash_entry *)
			    ATTRIBUTE_UNUSED)
{
  FILE *sym_ld_script = NULL;
  struct elf_nds32_link_hash_table *table;

  table = nds32_elf_hash_table (info);
  sym_ld_script = table->sym_ld_script;

  if (check_start_export_sym)
    fprintf (sym_ld_script, "}\n");

  return TRUE;
}

static enum elf_reloc_type_class
nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
			    const asection *rel_sec ATTRIBUTE_UNUSED,
			    const Elf_Internal_Rela *rela)
{
  switch ((int) ELF32_R_TYPE (rela->r_info))
    {
    case R_NDS32_RELATIVE:
      return reloc_class_relative;
    case R_NDS32_JMP_SLOT:
      return reloc_class_plt;
    case R_NDS32_COPY:
      return reloc_class_copy;
    default:
      return reloc_class_normal;
    }
}

/* Put target dependent option into info hash table.  */
void
bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
				   int relax_fp_as_gp,
				   int eliminate_gc_relocs,
				   FILE * sym_ld_script, int load_store_relax,
				   int target_optimize, int relax_status,
				   int relax_round, FILE * ex9_export_file,
				   FILE * ex9_import_file,
				   int update_ex9_table, int ex9_limit,
				   bfd_boolean ex9_loop_aware,
				   bfd_boolean ifc_loop_aware)
{
  struct elf_nds32_link_hash_table *table;

  table = nds32_elf_hash_table (link_info);
  if (table == NULL)
    return;

  table->relax_fp_as_gp = relax_fp_as_gp;
  table->eliminate_gc_relocs = eliminate_gc_relocs;
  table->sym_ld_script = sym_ld_script;
  table ->load_store_relax = load_store_relax;
  table->target_optimize = target_optimize;
  table->relax_status = relax_status;
  table->relax_round = relax_round;
  table->ex9_export_file = ex9_export_file;
  table->ex9_import_file = ex9_import_file;
  table->update_ex9_table = update_ex9_table;
  table->ex9_limit = ex9_limit;
  table->ex9_loop_aware = ex9_loop_aware;
  table->ifc_loop_aware = ifc_loop_aware;
}

/* These functions and data-structures are used for fp-as-gp
   optimization.  */

#define FAG_THRESHOLD	3	/* At least 3 gp-access.  */
/* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
   the read-only section and read-write section.  */
#define FAG_WINDOW	(508 - 32)

/* An nds32_fag represent a gp-relative access.
   We find best fp-base by using a sliding window
   to find a base address which can cover most gp-access.  */
struct nds32_fag
{
  struct nds32_fag *next;	/* NULL-teminated linked list.  */
  bfd_vma addr;			/* The address of this fag.  */
  Elf_Internal_Rela **relas;	/* The relocations associated with this fag.
				   It is used for applying FP7U2_FLAG.  */
  int count;			/* How many times this address is referred.
				   There should be exactly `count' relocations
				   in relas.  */
  int relas_capcity;		/* The buffer size of relas.
				   We use an array instead of linked-list,
				   and realloc is used to adjust buffer size.  */
};

static void
nds32_fag_init (struct nds32_fag *head)
{
  memset (head, 0, sizeof (struct nds32_fag));
}

static void
nds32_fag_verify (struct nds32_fag *head)
{
  struct nds32_fag *iter;
  struct nds32_fag *prev;

  prev = NULL;
  iter = head->next;
  while (iter)
    {
      if (prev && prev->addr >= iter->addr)
	puts ("Bug in fp-as-gp insertion.");
      prev = iter;
      iter = iter->next;
    }
}

/* Insert a fag in ascending order.
   If a fag of the same address already exists,
   they are chained by relas array.  */

static void
nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
		  Elf_Internal_Rela * rel)
{
  struct nds32_fag *iter;
  struct nds32_fag *new_fag;
  const int INIT_RELAS_CAP = 4;

  for (iter = head;
       iter->next && iter->next->addr <= addr;
       iter = iter->next)
    /* Find somewhere to insert.  */ ;

  /* `iter' will be equal to `head' if the list is empty.  */
  if (iter != head && iter->addr == addr)
    {
      /* The address exists in the list.
	 Insert `rel' into relocation list, relas.  */

      /* Check whether relas is big enough.  */
      if (iter->count >= iter->relas_capcity)
	{
	  iter->relas_capcity *= 2;
	  iter->relas = bfd_realloc
	    (iter->relas, iter->relas_capcity * sizeof (void *));
	}
      iter->relas[iter->count++] = rel;
      return;
    }

  /* This is a new address.  Create a fag node for it.  */
  new_fag = bfd_malloc (sizeof (struct nds32_fag));
  memset (new_fag, 0, sizeof (*new_fag));
  new_fag->addr = addr;
  new_fag->count = 1;
  new_fag->next = iter->next;
  new_fag->relas_capcity = INIT_RELAS_CAP;
  new_fag->relas = (Elf_Internal_Rela **)
    bfd_malloc (new_fag->relas_capcity * sizeof (void *));
  new_fag->relas[0] = rel;
  iter->next = new_fag;

  nds32_fag_verify (head);
}

static void
nds32_fag_free_list (struct nds32_fag *head)
{
  struct nds32_fag *iter;

  iter = head->next;
  while (iter)
    {
      struct nds32_fag *tmp = iter;
      iter = iter->next;
      free (tmp->relas);
      tmp->relas = NULL;
      free (tmp);
    }
}

/* Find the best fp-base address.
   The relocation associated with that address is returned,
   so we can track the symbol instead of a fixed address.

   When relaxation, the address of an datum may change,
   because a text section is shrinked, so the data section
   moves forward.  If the aligments of text and data section
   are different, their distance may change too.
   Therefore, tracking a fixed address is not appriate.  */

static int
nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
{
  struct nds32_fag *base;	/* First fag in the window.  */
  struct nds32_fag *last;	/* First fag outside the window.  */
  int accu = 0;			/* Usage accumulation.  */
  struct nds32_fag *best;	/* Best fag.  */
  int baccu = 0;		/* Best accumulation.  */

  /* Use first fag for initial, and find the last fag in the window.

     In each iteration, we could simply subtract previous fag
     and accumulate following fags which are inside the window,
     untill we each the end.  */

  if (head->next == NULL)
    {
      *bestpp = NULL;
      return 0;
    }

  /* Initialize base.  */
  base = head->next;
  best = base;
  for (last = base;
       last && last->addr < base->addr + FAG_WINDOW;
       last = last->next)
    accu += last->count;

  baccu = accu;

  /* Record the best base in each iteration.  */
  while (base->next)
    {
      accu -= base->count;
      base = base->next;
      /* Account fags in window.  */
      for (/* Nothing.  */;
	   last && last->addr < base->addr + FAG_WINDOW;
	   last = last->next)
	accu += last->count;

      /* A better fp-base?  */
      if (accu > baccu)
	{
	  best = base;
	  baccu = accu;
	}
    }

  if (bestpp)
    *bestpp = best;
  return baccu;
}

/* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
   so we can convert it fo fp-relative access later.
   `best_fag' is the best fp-base.  Only those inside the window
   of best_fag is applied the flag.  */

static bfd_boolean
nds32_fag_mark_relax (struct bfd_link_info *link_info,
		      bfd *abfd, struct nds32_fag *best_fag,
		      Elf_Internal_Rela *internal_relocs,
		      Elf_Internal_Rela *irelend)
{
  struct nds32_fag *ifag;
  bfd_vma best_fpbase, gp;
  bfd *output_bfd;

  output_bfd = abfd->sections->output_section->owner;
  nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
  best_fpbase = best_fag->addr;

  if (best_fpbase > gp + sdata_range[1][1]
      || best_fpbase < gp - sdata_range[1][0])
    return FALSE;

  /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
     so we know they can be converted to lwi37.fp.   */
  for (ifag = best_fag;
       ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
    {
      int i;

      for (i = 0; i < ifag->count; i++)
	{
	  Elf_Internal_Rela *insn16_rel;
	  Elf_Internal_Rela *fag_rel;

	  fag_rel = ifag->relas[i];

	  /* Only if this is within the WINDOWS, FP7U2_FLAG
	     is applied.  */

	  insn16_rel = find_relocs_at_address
	    (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);

	  if (insn16_rel != irelend)
	    insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
	}
    }
  return TRUE;
}

/* Reset INSN16 to clean fp as gp.  */

static void
nds32_fag_unmark_relax (struct nds32_fag *fag,
			Elf_Internal_Rela *internal_relocs,
			Elf_Internal_Rela *irelend)
{
  struct nds32_fag *ifag;
  int i;
  Elf_Internal_Rela *insn16_rel;
  Elf_Internal_Rela *fag_rel;

  for (ifag = fag; ifag; ifag = ifag->next)
    {
      for (i = 0; i < ifag->count; i++)
	{
	  fag_rel = ifag->relas[i];

	  /* Restore the INSN16 relocation.  */
	  insn16_rel = find_relocs_at_address
	    (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);

	  if (insn16_rel != irelend)
	    insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
	}
    }
}

/* This is the main function of fp-as-gp optimization.
   It should be called by relax_section.  */

static bfd_boolean
nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
		      bfd *abfd, asection *sec,
		      Elf_Internal_Rela *internal_relocs,
		      Elf_Internal_Rela *irelend,
		      Elf_Internal_Sym *isymbuf)
{
  Elf_Internal_Rela *begin_rel = NULL;
  Elf_Internal_Rela *irel;
  struct nds32_fag fag_head;
  Elf_Internal_Shdr *symtab_hdr;
  bfd_byte *contents;
  bfd_boolean ifc_inside = FALSE;

  /* FIXME: Can we bfd_elf_link_read_relocs for the relocs?  */

  /* Per-function fp-base selection.
     1. Create a list for all the gp-relative access.
     2. Base on those gp-relative address,
	find a fp-base which can cover most access.
     3. Use the fp-base for fp-as-gp relaxation.

     NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
     we should
     1. delete the `la $fp, _FP_BASE_' instruction and
     2. not convert lwi.gp to lwi37.fp.

     To delete the _FP_BASE_ instruction, we simply apply
     R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.

     To suppress the conversion, we simply NOT to apply
     R_NDS32_INSN16_FP7U2_FLAG flag.  */

  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;

  if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
    return FALSE;

  /* Check whether it is worth for fp-as-gp optimization,
     i.e., at least 3 gp-load.

     Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
     apply this optimization.  */

  for (irel = internal_relocs; irel < irelend; irel++)
    {
      /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
	 One we enter the begin of the region, we track all the LW/ST
	 instructions, so when we leave the region, we try to find
	 the best fp-base address for those LW/ST instructions.  */

      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
	  && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
	{
	  /* Begin of the region.  */
	  if (begin_rel)
	    (*_bfd_error_handler) (_("%B: Nested OMIT_FP in %A."), abfd, sec);

	  begin_rel = irel;
	  nds32_fag_init (&fag_head);
	  ifc_inside = FALSE;
	}
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
	       && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
	{
	  int accu;
	  struct nds32_fag *best_fag, *tmp_fag;
	  int dist;

	  /* End of the region.
	     Check whether it is worth to do fp-as-gp.  */

	  if (begin_rel == NULL)
	    {
	      (*_bfd_error_handler) (_("%B: Unmatched OMIT_FP in %A."), abfd, sec);
	      continue;
	    }

	  accu = nds32_fag_find_base (&fag_head, &best_fag);

	  /* Clean FP7U2_FLAG because they may set ever.  */
	  tmp_fag = fag_head.next;
	  nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);

	  /* Check if it is worth, and FP_BASE is near enough to SDA_BASE.  */
	  if (accu < FAG_THRESHOLD
	      || !nds32_fag_mark_relax (link_info, abfd, best_fag,
					internal_relocs, irelend))
	    {
	      /* Not worth to do fp-as-gp.  */
	      begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
	      begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
	      irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
	      irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
	      nds32_fag_free_list (&fag_head);
	      begin_rel = NULL;
	      continue;
	    }

	  /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
	     so we use it to record the distance to the reloction of best
	     fp-base.  */
	  dist = best_fag->relas[0] - begin_rel;
	  BFD_ASSERT (dist > 0 && dist < 0xffffff);
	  /* Use high 16 bits of addend to record the _FP_BASE_ matched
	     relocation.  And get the base value when relocating.  */
	  begin_rel->r_addend &= (0x1 << 16) - 1;
	  begin_rel->r_addend |= dist << 16;

	  nds32_fag_free_list (&fag_head);
	  begin_rel = NULL;
	}

      if (begin_rel == NULL || ifc_inside)
	/* Skip if we are not in the region of fp-as-gp.  */
	continue;

      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
	  || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
	{
	  bfd_vma addr;
	  uint32_t insn;

	  /* A gp-relative access is found.  Insert it to the fag-list.  */

	  /* Rt is necessary an RT3, so it can be converted to lwi37.fp.  */
	  insn = bfd_getb32 (contents + irel->r_offset);
	  if (!N32_IS_RT3 (insn))
	    continue;

	  addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
	  nds32_fag_insert (&fag_head, addr, irel);
	}
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
	{
	  begin_rel = NULL;
	}
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
	{
	  /* Suppress fp as gp when encounter ifc.  */
	  ifc_inside = TRUE;
	}
    }

  return TRUE;
}

/* Remove unused `la $fp, _FD_BASE_' instruction.  */

static bfd_boolean
nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
				Elf_Internal_Rela *internal_relocs,
				Elf_Internal_Rela *irelend)
{
  Elf_Internal_Rela *irel;
  Elf_Internal_Shdr *symtab_hdr;
  bfd_byte *contents = NULL;
  nds32_elf_blank_t *relax_blank_list = NULL;
  bfd_boolean result = TRUE;
  bfd_boolean unused_region = FALSE;

  /*
     NOTE: Disable fp-as-gp if we encounter ifcall relocations.
     * R_NDS32_17IFC_PCREL_RELA
     * R_NDS32_10IFCU_PCREL_RELA

     CASE??????????????
  */

  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  nds32_get_section_contents (abfd, sec, &contents, TRUE);

  for (irel = internal_relocs; irel < irelend; irel++)
    {
      /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
	 we marked to in previous pass.
	 DO NOT scan relocations again, since we've alreadly decided it
	 and set the flag.  */
      const char *syname;
      int syndx;
      uint32_t insn;

      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
	  && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
	unused_region = TRUE;
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
	       && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
	unused_region = FALSE;

      /* We're not in the region.  */
      if (!unused_region)
	continue;

      /* _FP_BASE_ must be a GLOBAL symbol.  */
      syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
	continue;

      /* The symbol name must be _FP_BASE_.  */
      syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
      if (strcmp (syname, FP_BASE_NAME) != 0)
	continue;

      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
	{
	  /* addi.gp  $fp, -256  */
	  insn = bfd_getb32 (contents + irel->r_offset);
	  if (insn != INSN_ADDIGP_TO_FP)
	    continue;
	}
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
	{
	  /* addi  $fp, $gp, -256  */
	  insn = bfd_getb32 (contents + irel->r_offset);
	  if (insn != INSN_ADDI_GP_TO_FP)
	    continue;
	}
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
	{
	  /* movi  $fp, FP_BASE  */
	  insn = bfd_getb32 (contents + irel->r_offset);
	  if (insn != INSN_MOVI_TO_FP)
	    continue;
	}
      else
	continue;

      /* We got here because a FP_BASE instruction is found.  */
      if (!insert_nds32_elf_blank_recalc_total
	  (&relax_blank_list, irel->r_offset, 4))
	goto error_return;
    }

finish:
  if (relax_blank_list)
    {
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
      relax_blank_list = NULL;
    }
  return result;

error_return:
  result = FALSE;
  goto finish;
}

/* This is a version of bfd_generic_get_relocated_section_contents.
   We need this variety because relaxation will modify the dwarf
   infomation.  When there is undefined symbol reference error mesage,
   linker need to dump line number where the symbol be used.  However
   the address is be relaxed, it can not get the original dwarf contents.
   The variety only modify function call for reading in the section.  */

static bfd_byte *
nds32_elf_get_relocated_section_contents (bfd *abfd,
					  struct bfd_link_info *link_info,
					  struct bfd_link_order *link_order,
					  bfd_byte *data,
					  bfd_boolean relocatable,
					  asymbol **symbols)
{
  bfd *input_bfd = link_order->u.indirect.section->owner;
  asection *input_section = link_order->u.indirect.section;
  long reloc_size;
  arelent **reloc_vector;
  long reloc_count;

  reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  if (reloc_size < 0)
    return NULL;

  /* Read in the section.  */
  if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
    return NULL;

  if (reloc_size == 0)
    return data;

  reloc_vector = (arelent **) bfd_malloc (reloc_size);
  if (reloc_vector == NULL)
    return NULL;

  reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
					reloc_vector, symbols);
  if (reloc_count < 0)
    goto error_return;

  if (reloc_count > 0)
    {
      arelent **parent;
      for (parent = reloc_vector; *parent != NULL; parent++)
	{
	  char *error_message = NULL;
	  asymbol *symbol;
	  bfd_reloc_status_type r;

	  symbol = *(*parent)->sym_ptr_ptr;
	  if (symbol->section && discarded_section (symbol->section))
	    {
	      bfd_byte *p;
	      static reloc_howto_type none_howto
		= HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
			 "unused", FALSE, 0, 0, FALSE);

	      p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
	      _bfd_clear_contents ((*parent)->howto, input_bfd, input_section,
				   p);
	      (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
	      (*parent)->addend = 0;
	      (*parent)->howto = &none_howto;
	      r = bfd_reloc_ok;
	    }
	  else
	    r = bfd_perform_relocation (input_bfd, *parent, data,
					input_section,
					relocatable ? abfd : NULL,
					&error_message);

	  if (relocatable)
	    {
	      asection *os = input_section->output_section;

	      /* A partial link, so keep the relocs.  */
	      os->orelocation[os->reloc_count] = *parent;
	      os->reloc_count++;
	    }

	  if (r != bfd_reloc_ok)
	    {
	      switch (r)
		{
		case bfd_reloc_undefined:
		  if (!((*link_info->callbacks->undefined_symbol)
			(link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
			 input_bfd, input_section, (*parent)->address, TRUE)))
		    goto error_return;
		  break;
		case bfd_reloc_dangerous:
		  BFD_ASSERT (error_message != NULL);
		  if (!((*link_info->callbacks->reloc_dangerous)
			(link_info, error_message, input_bfd, input_section,
			 (*parent)->address)))
		    goto error_return;
		  break;
		case bfd_reloc_overflow:
		  if (!((*link_info->callbacks->reloc_overflow)
			(link_info, NULL,
			 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
			 (*parent)->howto->name, (*parent)->addend,
			 input_bfd, input_section, (*parent)->address)))
		    goto error_return;
		  break;
		case bfd_reloc_outofrange:
		  /* PR ld/13730:
		     This error can result when processing some partially
		     complete binaries.  Do not abort, but issue an error
		     message instead.  */
		  link_info->callbacks->einfo
		    (_("%X%P: %B(%A): relocation \"%R\" goes out of range\n"),
		     abfd, input_section, * parent);
		  goto error_return;

		default:
		  abort ();
		  break;
		}
	    }
	}
    }

  free (reloc_vector);
  return data;

error_return:
  free (reloc_vector);
  return NULL;
}

/* Link-time IFC relaxation.
   In this optimization, we chains jump instructions
   of the same destination with ifcall.  */


/* List to save jal and j relocation.  */
struct elf_nds32_ifc_symbol_entry
{
  asection *sec;
  struct elf_link_hash_entry *h;
  struct elf_nds32_ifc_irel_list *irel_head;
  unsigned long insn;
  int times;
  int enable;		/* Apply ifc.  */
  int ex9_enable;	/* Apply ifc after ex9.  */
  struct elf_nds32_ifc_symbol_entry *next;
};

struct elf_nds32_ifc_irel_list
{
  Elf_Internal_Rela *irel;
  asection *sec;
  bfd_vma addr;
  /* If this is set, then it is the last instruction for
     ifc-chain, so it must be keep for the actual branching.  */
  int keep;
  struct elf_nds32_ifc_irel_list *next;
};

static struct elf_nds32_ifc_symbol_entry *ifc_symbol_head = NULL;

/* Insert symbol of jal and j for ifc.  */

static void
nds32_elf_ifc_insert_symbol (asection *sec,
			     struct elf_link_hash_entry *h,
			     Elf_Internal_Rela *irel,
			     unsigned long insn)
{
  struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;

  /* Check there is target of existing entry the same as the new one.  */
  while (ptr != NULL)
    {
      if (((h == NULL && ptr->sec == sec
	    && ELF32_R_SYM (ptr->irel_head->irel->r_info) == ELF32_R_SYM (irel->r_info)
	    && ptr->irel_head->irel->r_addend == irel->r_addend)
	   || h != NULL)
	  && ptr->h == h
	  && ptr->insn == insn)
	{
	  /* The same target exist, so insert into list.  */
	  struct elf_nds32_ifc_irel_list *irel_list = ptr->irel_head;

	  while (irel_list->next != NULL)
	    irel_list = irel_list->next;
	  irel_list->next = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
	  irel_list = irel_list->next;
	  irel_list->irel = irel;
	  irel_list->keep = 1;

	  if (h == NULL)
	    irel_list->sec = NULL;
	  else
	    irel_list->sec = sec;
	  irel_list->next = NULL;
	  return;
	}
      if (ptr->next == NULL)
	break;
      ptr = ptr->next;
    }

  /* There is no same target entry, so build a new one.  */
  if (ifc_symbol_head == NULL)
    {
      ifc_symbol_head = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
      ptr = ifc_symbol_head;
    }
  else
    {
      ptr->next = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
      ptr = ptr->next;
    }

  ptr->h = h;
  ptr->irel_head = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
  ptr->irel_head->irel = irel;
  ptr->insn = insn;
  ptr->irel_head->keep = 1;

  if (h == NULL)
    {
      /* Local symbols.  */
      ptr->sec = sec;
      ptr->irel_head->sec = NULL;
    }
  else
    {
      /* Global symbol.  */
      ptr->sec = NULL;
      ptr->irel_head->sec = sec;
    }

  ptr->irel_head->next = NULL;
  ptr->times = 0;
  ptr->enable = 0;
  ptr->ex9_enable = 0;
  ptr->next = NULL;
}

/* Gather all jal and j instructions.  */

static bfd_boolean
nds32_elf_ifc_calc (struct bfd_link_info *info,
		    bfd *abfd, asection *sec)
{
  Elf_Internal_Rela *internal_relocs;
  Elf_Internal_Rela *irelend;
  Elf_Internal_Rela *irel;
  Elf_Internal_Shdr *symtab_hdr;
  bfd_byte *contents = NULL;
  uint32_t insn, insn_with_reg;
  unsigned long r_symndx;
  struct elf_link_hash_entry *h;
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
  struct elf_nds32_link_hash_table *table;
  bfd_boolean ifc_loop_aware;

  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
					       TRUE /* keep_memory */);
  irelend = internal_relocs + sec->reloc_count;
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;

  /* Check if the object enable ifc.  */
  irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
				 R_NDS32_RELAX_ENTRY);

  if (irel == NULL
      || irel >= irelend
      || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
      || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
	  && !(irel->r_addend & R_NDS32_RELAX_ENTRY_IFC_FLAG)))
    return TRUE;

  if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
    return FALSE;

  table = nds32_elf_hash_table (info);
  ifc_loop_aware = table->ifc_loop_aware;
  while (irel != NULL && irel < irelend)
    {
      /* Traverse all relocation and gather all of them to build the list.  */

      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN)
	{
	  if (ifc_loop_aware == 1
	      && (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0)
	    {
	      /* Check the region if loop or not.  If it is true and
		 ifc-loop-aware is true, ignore the region till region end.  */
	      while (irel != NULL
		     && irel < irelend
		     && (ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
			 || (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0))
		irel++;
	    }
	}

      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
	{
	  insn = bfd_getb32 (contents + irel->r_offset);
	  nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
	  r_symndx = ELF32_R_SYM (irel->r_info);
	  if (r_symndx < symtab_hdr->sh_info)
	    {
	      /* Local symbol.  */
	      nds32_elf_ifc_insert_symbol (sec, NULL, irel, insn_with_reg);
	    }
	  else
	    {
	      /* External symbol.  */
	      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
	      nds32_elf_ifc_insert_symbol (sec, h, irel, insn_with_reg);
	    }
	}
      irel++;
    }
  return TRUE;
}

/* Determine whether j and jal should be substituted.  */

static void
nds32_elf_ifc_filter (struct bfd_link_info *info)
{
  struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
  struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
  struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
  struct elf_nds32_link_hash_table *table;
  int target_optimize;
  bfd_vma address;

  table = nds32_elf_hash_table (info);
  target_optimize = table->target_optimize;
  while (ptr)
    {
      irel_ptr = ptr->irel_head;
      if (ptr->h == NULL)
	{
	  /* Local symbol.  */
	  irel_keeper = irel_ptr;
	  while (irel_ptr && irel_ptr->next)
	    {
	      /* Check there is jump target can be used.  */
	      if ((irel_ptr->next->irel->r_offset
		   - irel_keeper->irel->r_offset) > 1022)
		irel_keeper = irel_ptr->next;
	      else
		{
		  ptr->enable = 1;
		  irel_ptr->keep = 0;
		}
	      irel_ptr = irel_ptr->next;
	    }
	}
      else
	{
	  /* Global symbol.  */
	  /* We have to get the absolute address and decide
	     whether to keep it or not.  */
	  while (irel_ptr)
	    {
	      address = (irel_ptr->irel->r_offset
			 + irel_ptr->sec->output_section->vma
			 + irel_ptr->sec->output_offset);
	      irel_ptr->addr = address;
	      irel_ptr = irel_ptr->next;
	    }

	  irel_ptr = ptr->irel_head;
	  while (irel_ptr)
	    {
	      /* Sort by address.  */
	      struct elf_nds32_ifc_irel_list *irel_dest = irel_ptr;
	      struct elf_nds32_ifc_irel_list *irel_temp = irel_ptr;
	      struct elf_nds32_ifc_irel_list *irel_ptr_prev = NULL;
	      struct elf_nds32_ifc_irel_list *irel_dest_prev = NULL;

	      /* Get the smallest one.  */
	      while (irel_temp->next)
		{
		  if (irel_temp->next->addr < irel_dest->addr)
		    {
		      irel_dest_prev = irel_temp;
		      irel_dest = irel_temp->next;
		    }
		  irel_temp = irel_temp->next;
		}

	      if (irel_dest != irel_ptr)
		{
		  if (irel_ptr_prev)
		    irel_ptr_prev->next = irel_dest;
		  if (irel_dest_prev)
		    irel_dest_prev->next = irel_ptr;
		  irel_temp = irel_ptr->next;
		  irel_ptr->next = irel_dest->next;
		  irel_dest->next = irel_temp;
		}
	      irel_ptr_prev = irel_ptr;
	      irel_ptr = irel_ptr->next;
	    }

	  irel_ptr = ptr->irel_head;
	  irel_keeper = irel_ptr;
	  while (irel_ptr && irel_ptr->next)
	    {
	      if ((irel_ptr->next->addr - irel_keeper->addr) > 1022)
		irel_keeper = irel_ptr->next;
	      else
		{
		  ptr->enable = 1;
		  irel_ptr->keep = 0;
		}
	      irel_ptr = irel_ptr->next;
	    }
	}

	/* Ex9 enable.  Reserve it for ex9.  */
      if ((target_optimize & NDS32_RELAX_EX9_ON)
	  && ptr->irel_head != irel_keeper)
	ptr->enable = 0;
      ptr = ptr->next;
    }
}

/* Determine whether j and jal should be substituted after ex9 done.  */

static void
nds32_elf_ifc_filter_after_ex9 (void)
{
  struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
  struct elf_nds32_ifc_irel_list *irel_ptr = NULL;

  while (ptr)
    {
      if (ptr->enable == 0)
	{
	  /* Check whether ifc is applied or not.  */
	  irel_ptr = ptr->irel_head;
	  ptr->ex9_enable = 1;
	  while (irel_ptr)
	    {
	      if (ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_TRAN)
		{
		  /* Ex9 already.  */
		  ptr->ex9_enable = 0;
		  break;
		}
	      irel_ptr = irel_ptr->next;
	    }
	}
      ptr = ptr->next;
    }
}

/* Wrapper to do ifc relaxation.  */

bfd_boolean
nds32_elf_ifc_finish (struct bfd_link_info *info)
{
  int relax_status;
  struct elf_nds32_link_hash_table *table;

  table = nds32_elf_hash_table (info);
  relax_status = table->relax_status;

  if (!(relax_status & NDS32_RELAX_JUMP_IFC_DONE))
    nds32_elf_ifc_filter (info);
  else
    nds32_elf_ifc_filter_after_ex9 ();

  if (!nds32_elf_ifc_replace (info))
    return FALSE;

  if (table)
    table->relax_status |= NDS32_RELAX_JUMP_IFC_DONE;
  return TRUE;
}

/* Traverse the result of ifc filter and replace it with ifcall9.  */

static bfd_boolean
nds32_elf_ifc_replace (struct bfd_link_info *info)
{
  struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
  struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
  nds32_elf_blank_t *relax_blank_list = NULL;
  bfd_byte *contents = NULL;
  Elf_Internal_Rela *internal_relocs;
  Elf_Internal_Rela *irel;
  Elf_Internal_Rela *irelend;
  unsigned short insn16 = INSN_IFCALL9;
  struct elf_nds32_link_hash_table *table;
  int relax_status;

  table = nds32_elf_hash_table (info);
  relax_status = table->relax_status;

  while (ptr)
    {
      /* Traverse the ifc gather list, and replace the
	 filter entries by ifcall9.  */
      if ((!(relax_status & NDS32_RELAX_JUMP_IFC_DONE) && ptr->enable == 1)
	  || ((relax_status & NDS32_RELAX_JUMP_IFC_DONE)
	      && ptr->ex9_enable == 1))
	{
	  irel_ptr = ptr->irel_head;
	  if (ptr->h == NULL)
	    {
	      /* Local symbol.  */
	      internal_relocs = _bfd_elf_link_read_relocs
		(ptr->sec->owner, ptr->sec, NULL, NULL, TRUE /* keep_memory */);
	      irelend = internal_relocs + ptr->sec->reloc_count;

	      if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
					       &contents, TRUE))
		return FALSE;

	      while (irel_ptr)
		{
		  if (irel_ptr->keep == 0 && irel_ptr->next)
		    {
		      /* The one can be replaced.  We have to check whether
			 there is any alignment point in the region.  */
		      irel = irel_ptr->irel;
		      while (((irel_ptr->next->keep == 0
			       && irel < irel_ptr->next->irel)
			      || (irel_ptr->next->keep == 1 && irel < irelend))
			     && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
				  && (irel->r_addend & 0x1f) == 2))
			irel++;
		      if (irel >= irelend
			  || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
			       && (irel->r_addend & 0x1f) == 2
			       && ((irel->r_offset - get_nds32_elf_blank_total
				    (&relax_blank_list, irel->r_offset, 1))
				   & 0x02) == 0))
			{
			  /* Replace by ifcall9.  */
			  bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
			  if (!insert_nds32_elf_blank_recalc_total
			      (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
			    return FALSE;
			  irel_ptr->irel->r_info =
			    ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
					  R_NDS32_10IFCU_PCREL_RELA);
			}
		    }
		  irel_ptr = irel_ptr->next;
		}

	      /* Delete the redundant code.  */
	      if (relax_blank_list)
		{
		  nds32_elf_relax_delete_blanks (ptr->sec->owner, ptr->sec,
						 relax_blank_list);
		  relax_blank_list = NULL;
		}
	    }
	  else
	    {
	      /* Global symbol.  */
	      while (irel_ptr)
		{
		  if (irel_ptr->keep == 0 && irel_ptr->next)
		    {
		      /* The one can be replaced, and we have to check
			 whether there is any alignment point in the region.  */
		      internal_relocs = _bfd_elf_link_read_relocs
			(irel_ptr->sec->owner, irel_ptr->sec, NULL, NULL,
			 TRUE /* keep_memory */);
		      irelend = internal_relocs + irel_ptr->sec->reloc_count;
		      if (!nds32_get_section_contents (irel_ptr->sec->owner,
						       irel_ptr->sec, &contents,
						       TRUE))
			return FALSE;

		      irel = irel_ptr->irel;
		      while (((irel_ptr->sec == irel_ptr->next->sec
			       && irel_ptr->next->keep == 0
			       && irel < irel_ptr->next->irel)
			      || ((irel_ptr->sec != irel_ptr->next->sec
				   || irel_ptr->next->keep == 1)
				  && irel < irelend))
			     && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
				  && (irel->r_addend & 0x1f) == 2))
			irel++;
		      if (irel >= irelend
			  || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
			       && (irel->r_addend & 0x1f) == 2
			       && ((irel->r_offset
				    - get_nds32_elf_blank_total (&relax_blank_list,
							    irel->r_offset, 1)) & 0x02) == 0))
			{
			  /* Replace by ifcall9.  */
			  bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
			  if (!insert_nds32_elf_blank_recalc_total
			      (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
			    return FALSE;

			  /* Delete the redundant code, and clear the relocation.  */
			  nds32_elf_relax_delete_blanks (irel_ptr->sec->owner,
							 irel_ptr->sec,
							 relax_blank_list);
			  irel_ptr->irel->r_info =
			    ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
					  R_NDS32_10IFCU_PCREL_RELA);
			  relax_blank_list = NULL;
			}
		    }

		  irel_ptr = irel_ptr->next;
		}
	    }
	}
      ptr = ptr->next;
    }

  return TRUE;
}

/* Relocate ifcall.  */

static bfd_boolean
nds32_elf_ifc_reloc (void)
{
  struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
  struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
  struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
  bfd_vma relocation, address;
  unsigned short insn16;
  bfd_byte *contents = NULL;
  static bfd_boolean done = FALSE;

  if (done)
    return TRUE;

  done = TRUE;

  while (ptr)
    {
      /* Check the entry is enable ifcall.  */
      if (ptr->enable == 1 || ptr->ex9_enable == 1)
	{
	  /* Get the reserve jump.  */
	  irel_ptr = ptr->irel_head;
	  while (irel_ptr)
	    {
	      if (irel_ptr->keep == 1)
		{
		  irel_keeper = irel_ptr;
		  break;
		}
	      irel_ptr = irel_ptr->next;
	    }

	  irel_ptr = ptr->irel_head;
	  if (ptr->h == NULL)
	    {
	      /* Local symbol.  */
	      if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
					       &contents, TRUE))
		return FALSE;

	      while (irel_ptr)
		{
		  if (irel_ptr->keep == 0
		      && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
		    {
		      relocation = irel_keeper->irel->r_offset;
		      relocation = relocation - irel_ptr->irel->r_offset;
		      while (irel_keeper && relocation > 1022)
			{
			  irel_keeper = irel_keeper->next;
			  if (irel_keeper && irel_keeper->keep == 1)
			    {
			      relocation = irel_keeper->irel->r_offset;
			      relocation = relocation - irel_ptr->irel->r_offset;
			    }
			}
		      if (relocation > 1022)
			{
			  /* Double check.  */
			  irel_keeper = ptr->irel_head;
			  while (irel_keeper)
			    {
			      if (irel_keeper->keep == 1)
				{
				  relocation = irel_keeper->irel->r_offset;
				  relocation = relocation - irel_ptr->irel->r_offset;
				}
			      if (relocation <= 1022)
				break;
			      irel_keeper = irel_keeper->next;
			    }
			  if (!irel_keeper)
			    return FALSE;
			}
		      irel_ptr->irel->r_info =
			ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
				      R_NDS32_NONE);
		      insn16 = INSN_IFCALL9 | (relocation >> 1);
		      bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
		    }
		  irel_ptr = irel_ptr->next;
		}
	    }
	  else
	    {
	      /* Global symbol.  */
	      while (irel_ptr)
		{
		  if (irel_ptr->keep == 0
		      && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
		    {
		      /* Get the distance between ifcall and jump.  */
		      relocation = (irel_keeper->irel->r_offset
				    + irel_keeper->sec->output_section->vma
				    + irel_keeper->sec->output_offset);
		      address = (irel_ptr->irel->r_offset
				 + irel_ptr->sec->output_section->vma
				 + irel_ptr->sec->output_offset);
		      relocation = relocation - address;

		      /* The distance is over ragne, find callee again.  */
		      while (irel_keeper && relocation > 1022)
			{
			  irel_keeper = irel_keeper->next;
			  if (irel_keeper && irel_keeper->keep ==1)
			    {
			      relocation = (irel_keeper->irel->r_offset
					    + irel_keeper->sec->output_section->vma
					    + irel_keeper->sec->output_offset);
			      relocation = relocation - address;
			    }
			}

		      if (relocation > 1022)
			{
			  /* Double check.  */
			  irel_keeper = ptr->irel_head;
			  while (irel_keeper)
			    {
			      if (irel_keeper->keep == 1)
				{

				  relocation = (irel_keeper->irel->r_offset
						+ irel_keeper->sec->output_section->vma
						+ irel_keeper->sec->output_offset);
				  relocation = relocation - address;
				}
			      if (relocation <= 1022)
				break;
			      irel_keeper = irel_keeper->next;
			    }
			  if (!irel_keeper)
			    return FALSE;
			}
		      if (!nds32_get_section_contents
			  (irel_ptr->sec->owner, irel_ptr->sec, &contents, TRUE))
			return FALSE;
		      insn16 = INSN_IFCALL9 | (relocation >> 1);
		      bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
		      irel_ptr->irel->r_info =
			ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
				      R_NDS32_NONE);
		    }
		  irel_ptr =irel_ptr->next;
		}
	    }
	}
      ptr = ptr->next;
    }

  return TRUE;
}

/* End of IFC relaxation.  */

/* EX9 Instruction Table Relaxation.  */

/* Global hash list.  */
struct elf_link_hash_entry_list
{
  struct elf_link_hash_entry *h;
  struct elf_link_hash_entry_list *next;
};

/* Save different destination but same insn.  */
struct elf_link_hash_entry_mul_list
{
  /* Global symbol times.  */
  int times;
  /* Save relocation for each global symbol but useful??  */
  Elf_Internal_Rela *irel;
  /* For sethi, two sethi may have the same high-part but different low-parts.  */
  Elf_Internal_Rela rel_backup;
  struct elf_link_hash_entry_list *h_list;
  struct elf_link_hash_entry_mul_list *next;
};

/* Instruction hash table.  */
struct elf_nds32_code_hash_entry
{
  struct bfd_hash_entry root;
  int times;
  /* For insn that can use relocation or constant ex: sethi.  */
  int const_insn;
  asection *sec;
  struct elf_link_hash_entry_mul_list *m_list;
  /* Using r_addend.  */
  Elf_Internal_Rela *irel;
  /* Using r_info.  */
  Elf_Internal_Rela rel_backup;
};

/* Instruction count list.  */
struct elf_nds32_insn_times_entry
{
  const char *string;
  int times;
  int order;
  asection *sec;
  struct elf_link_hash_entry_mul_list *m_list;
  Elf_Internal_Rela *irel;
  Elf_Internal_Rela rel_backup;
  struct elf_nds32_insn_times_entry *next;
};

/* J and JAL symbol list.  */
struct elf_nds32_symbol_entry
{
  char *string;
  unsigned long insn;
  struct elf_nds32_symbol_entry *next;
};

/* Relocation list.  */
struct elf_nds32_irel_entry
{
  Elf_Internal_Rela *irel;
  struct elf_nds32_irel_entry *next;
};

/* ex9.it insn need to be fixed.  */
struct elf_nds32_ex9_refix
{
  Elf_Internal_Rela *irel;
  asection *sec;
  struct elf_link_hash_entry *h;
  int order;
  struct elf_nds32_ex9_refix *next;
};

static struct bfd_hash_table ex9_code_table;
static struct elf_nds32_insn_times_entry *ex9_insn_head = NULL;
static struct elf_nds32_ex9_refix *ex9_refix_head = NULL;

/* EX9 hash function.  */

static struct bfd_hash_entry *
nds32_elf_code_hash_newfunc (struct bfd_hash_entry *entry,
			     struct bfd_hash_table *table,
			     const char *string)
{
  struct elf_nds32_code_hash_entry *ret;

  /* Allocate the structure if it has not already been allocated by a
     subclass.  */
  if (entry == NULL)
    {
      entry = (struct bfd_hash_entry *)
	bfd_hash_allocate (table, sizeof (*ret));
      if (entry == NULL)
	return entry;
    }

  /* Call the allocation method of the superclass.  */
  entry = bfd_hash_newfunc (entry, table, string);
  if (entry == NULL)
    return entry;

  ret = (struct elf_nds32_code_hash_entry*) entry;
  ret->times = 0;
  ret->const_insn = 0;
  ret->m_list = NULL;
  ret->sec = NULL;
  ret->irel = NULL;
  return &ret->root;
}

/* Insert ex9 entry
   this insert must be stable sorted by times.  */

static void
nds32_elf_ex9_insert_entry (struct elf_nds32_insn_times_entry *ptr)
{
  struct elf_nds32_insn_times_entry *temp;
  struct elf_nds32_insn_times_entry *temp2;

  if (ex9_insn_head == NULL)
    {
      ex9_insn_head = ptr;
      ptr->next = NULL;
    }
  else
    {
      temp = ex9_insn_head;
      temp2 = ex9_insn_head;
      while (temp->next &&
	     (temp->next->times >= ptr->times
	      || temp->times == -1))
	{
	  if (temp->times == -1)
	    temp2 = temp;
	  temp = temp->next;
	}
      if (ptr->times > temp->times && temp->times != -1)
	{
	  ptr->next = temp;
	  if (temp2->times == -1)
	    temp2->next = ptr;
	  else
	    ex9_insn_head = ptr;
	}
      else if (temp->next == NULL)
	{
	  temp->next = ptr;
	  ptr->next = NULL;
	}
      else
	{
	  ptr->next = temp->next;
	  temp->next = ptr;
	}
    }
}

/* Examine each insn times in hash table.
   Handle multi-link hash entry.

   TODO: This function doesn't assign so much info since it is fake.  */

static int
nds32_elf_examine_insn_times (struct elf_nds32_code_hash_entry *h)
{
  struct elf_nds32_insn_times_entry *ptr;
  int times;

  if (h->m_list == NULL)
    {
      /* Local symbol insn or insn without relocation.  */
      if (h->times < 3)
	return TRUE;

      ptr = (struct elf_nds32_insn_times_entry *)
	bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
      ptr->times = h->times;
      ptr->string = h->root.string;
      ptr->m_list = NULL;
      ptr->sec = h->sec;
      ptr->irel = h->irel;
      ptr->rel_backup = h->rel_backup;
      nds32_elf_ex9_insert_entry (ptr);
    }
  else
    {
      /* Global symbol insn.  */
      /* Only sethi insn has multiple m_list.  */
      struct elf_link_hash_entry_mul_list *m_list = h->m_list;

      times = 0;
      while (m_list)
	{
	  times += m_list->times;
	  m_list = m_list->next;
	}
      if (times >= 3)
	{
	  m_list = h->m_list;
	  ptr = (struct elf_nds32_insn_times_entry *)
	    bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
	  ptr->times = times; /* Use the total times.  */
	  ptr->string = h->root.string;
	  ptr->m_list = m_list;
	  ptr->sec = h->sec;
	  ptr->irel = m_list->irel;
	  ptr->rel_backup = m_list->rel_backup;
	  nds32_elf_ex9_insert_entry (ptr);
	}
      if (h->const_insn == 1)
	{
	  /* sethi with constant value.  */
	  if (h->times < 3)
	    return TRUE;

	  ptr = (struct elf_nds32_insn_times_entry *)
	    bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
	  ptr->times = h->times;
	  ptr->string = h->root.string;
	  ptr->m_list = NULL;
	  ptr->sec = NULL;
	  ptr->irel = NULL;
	  ptr->rel_backup = h->rel_backup;
	  nds32_elf_ex9_insert_entry (ptr);
	}
    }
  return TRUE;
}

/* Count each insn times in hash table.
   Handle multi-link hash entry.  */

static int
nds32_elf_count_insn_times (struct elf_nds32_code_hash_entry *h)
{
  int reservation, times;
  unsigned long relocation, min_relocation;
  struct elf_nds32_insn_times_entry *ptr;

  if (h->m_list == NULL)
    {
      /* Local symbol insn or insn without relocation.  */
      if (h->times < 3)
	return TRUE;
      ptr = (struct elf_nds32_insn_times_entry *)
	bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
      ptr->times = h->times;
      ptr->string = h->root.string;
      ptr->m_list = NULL;
      ptr->sec = h->sec;
      ptr->irel = h->irel;
      ptr->rel_backup = h->rel_backup;
      nds32_elf_ex9_insert_entry (ptr);
    }
  else
    {
      /* Global symbol insn.  */
      /* Only sethi insn has multiple m_list.  */
      struct elf_link_hash_entry_mul_list *m_list = h->m_list;

      if (ELF32_R_TYPE (m_list->rel_backup.r_info) == R_NDS32_HI20_RELA
	  && m_list->next != NULL)
	{
	  /* Sethi insn has different symbol or addend but has same hi20.  */
	  times = 0;
	  reservation = 1;
	  relocation = 0;
	  min_relocation = 0xffffffff;
	  while (m_list)
	    {
	      /* Get the minimum sethi address
		 and calculate how many entry the sethi-list have to use.  */
	      if ((m_list->h_list->h->root.type == bfd_link_hash_defined
		   || m_list->h_list->h->root.type == bfd_link_hash_defweak)
		  && (m_list->h_list->h->root.u.def.section != NULL
		      && m_list->h_list->h->root.u.def.section->output_section != NULL))
		{
		  relocation = (m_list->h_list->h->root.u.def.value +
				m_list->h_list->h->root.u.def.section->output_section->vma +
				m_list->h_list->h->root.u.def.section->output_offset);
		  relocation += m_list->irel->r_addend;
		}
	      else
		relocation = 0;
	      if (relocation < min_relocation)
		min_relocation = relocation;
	      times += m_list->times;
	      m_list = m_list->next;
	    }
	  if (min_relocation < ex9_relax_size)
	    reservation = (min_relocation >> 12) + 1;
	  else
	    reservation = (min_relocation >> 12)
			  - ((min_relocation - ex9_relax_size) >> 12) + 1;
	  if (reservation < (times / 3))
	    {
	      /* Efficient enough to use ex9.  */
	      int i;

	      for (i = reservation ; i > 0; i--)
		{
		  /* Allocate number of reservation ex9 entry.  */
		  ptr = (struct elf_nds32_insn_times_entry *)
		    bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
		  ptr->times = h->m_list->times / reservation;
		  ptr->string = h->root.string;
		  ptr->m_list = h->m_list;
		  ptr->sec = h->sec;
		  ptr->irel = h->m_list->irel;
		  ptr->rel_backup = h->m_list->rel_backup;
		  nds32_elf_ex9_insert_entry (ptr);
		}
	    }
	}
      else
	{
	  /* Normal global symbol that means no different address symbol
	     using same ex9 entry.  */
	  if (m_list->times >= 3)
	    {
	      ptr = (struct elf_nds32_insn_times_entry *)
		bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
	      ptr->times = m_list->times;
	      ptr->string = h->root.string;
	      ptr->m_list = h->m_list;
	      ptr->sec = h->sec;
	      ptr->irel = h->m_list->irel;
	      ptr->rel_backup = h->m_list->rel_backup;
	      nds32_elf_ex9_insert_entry (ptr);
	    }
	}

      if (h->const_insn == 1)
	{
	  /* sethi with constant value.  */
	  if (h->times < 3)
	    return TRUE;

	  ptr = (struct elf_nds32_insn_times_entry *)
	    bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
	  ptr->times = h->times;
	  ptr->string = h->root.string;
	  ptr->m_list = NULL;
	  ptr->sec = NULL;
	  ptr->irel = NULL;
	  ptr->rel_backup = h->rel_backup;
	  nds32_elf_ex9_insert_entry (ptr);
	}
    }

  return TRUE;
}

/* Hash table traverse function.  */

static void
nds32_elf_code_hash_traverse (int (*func) (struct elf_nds32_code_hash_entry*))
{
  unsigned int i;

  ex9_code_table.frozen = 1;
  for (i = 0; i < ex9_code_table.size; i++)
    {
      struct bfd_hash_entry *p;

      for (p = ex9_code_table.table[i]; p != NULL; p = p->next)
	if (!func ((struct elf_nds32_code_hash_entry *) p))
	  goto out;
    }
out:
  ex9_code_table.frozen = 0;
}


/* Give order number to insn list.  */

static void
nds32_elf_order_insn_times (struct bfd_link_info *info)
{
  struct elf_nds32_insn_times_entry *ex9_insn;
  struct elf_nds32_insn_times_entry *temp = NULL;
  struct elf_nds32_link_hash_table *table;
  int ex9_limit;
  int number = 0;

  if (ex9_insn_head == NULL)
    return;

/* The max number of entries is 512.  */
  ex9_insn = ex9_insn_head;
  table = nds32_elf_hash_table (info);
  ex9_limit = table->ex9_limit;

  ex9_insn = ex9_insn_head;

  while (ex9_insn != NULL && number < ex9_limit)
    {
      ex9_insn->order = number;
      number++;
      temp = ex9_insn;
      ex9_insn = ex9_insn->next;
    }

  if (ex9_insn && temp)
    temp->next = NULL;

  while (ex9_insn != NULL)
    {
      /* Free useless entry.  */
      temp = ex9_insn;
      ex9_insn = ex9_insn->next;
      free (temp);
    }
}

/* Build .ex9.itable section.  */

static void
nds32_elf_ex9_build_itable (struct bfd_link_info *link_info)
{
  asection *table_sec;
  struct elf_nds32_insn_times_entry *ptr;
  bfd *it_abfd;
  int number = 0;
  bfd_byte *contents = NULL;

  for (it_abfd = link_info->input_bfds; it_abfd != NULL;
       it_abfd = it_abfd->link.next)
    {
      /* Find the section .ex9.itable, and put all entries into it.  */
      table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
      if (table_sec != NULL)
	{
	  if (!nds32_get_section_contents (it_abfd, table_sec, &contents, TRUE))
	    return;

	  for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
	    number++;

	  table_sec->size = number * 4;

	  if (number == 0)
	    return;

	  elf_elfheader (link_info->output_bfd)->e_flags |= E_NDS32_HAS_EX9_INST;
	  number = 0;
	  for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
	    {
	      long val;

	      val = strtol (ptr->string, NULL, 16);
	      bfd_putb32 ((bfd_vma) val, (char *) contents + (number * 4));
	      number++;
	    }
	  break;
	}
    }
}

/* Get insn with regs according to relocation type.  */

static void
nds32_elf_get_insn_with_reg (Elf_Internal_Rela *irel,
			     uint32_t insn, uint32_t *insn_with_reg)
{
  reloc_howto_type *howto = NULL;

  if (irel == NULL
      || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
	  && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
	     >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
    {
      *insn_with_reg = insn;
      return;
    }

  howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
  *insn_with_reg = insn & (0xffffffff ^ howto->dst_mask);
}

/* Mask number of address bits according to relocation.  */

static unsigned long
nds32_elf_irel_mask (Elf_Internal_Rela *irel)
{
  reloc_howto_type *howto = NULL;

  if (irel == NULL
      || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
	  && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
	     >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
    return 0;

  howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
  return howto->dst_mask;
}

static void
nds32_elf_insert_irel_entry (struct elf_nds32_irel_entry **irel_list,
			     struct elf_nds32_irel_entry *irel_ptr)
{
  if (*irel_list == NULL)
    {
      *irel_list = irel_ptr;
      irel_ptr->next = NULL;
    }
  else
    {
      irel_ptr->next = *irel_list;
      *irel_list = irel_ptr;
    }
}

static void
nds32_elf_ex9_insert_fix (asection * sec, Elf_Internal_Rela * irel,
			  struct elf_link_hash_entry *h, int order)
{
  struct elf_nds32_ex9_refix *ptr;

  ptr = bfd_malloc (sizeof (struct elf_nds32_ex9_refix));
  ptr->sec = sec;
  ptr->irel = irel;
  ptr->h = h;
  ptr->order = order;
  ptr->next = NULL;

  if (ex9_refix_head == NULL)
    ex9_refix_head = ptr;
  else
    {
      struct elf_nds32_ex9_refix *temp = ex9_refix_head;

      while (temp->next != NULL)
	temp = temp->next;
      temp->next = ptr;
    }
}

enum
{
  DATA_EXIST = 1,
  CLEAN_PRE = 1 << 1,
  PUSH_PRE = 1 << 2
};

/* Check relocation type if supporting for ex9.  */

static int
nds32_elf_ex9_relocation_check (struct bfd_link_info *info,
				Elf_Internal_Rela **irel,
				Elf_Internal_Rela *irelend,
				nds32_elf_blank_t *relax_blank_list,
				asection *sec,bfd_vma *off,
				bfd_byte *contents)
{
  /* Suppress ex9 if `.no_relax ex9' or inner loop.  */
  bfd_boolean nested_ex9, nested_loop;
  bfd_boolean ex9_loop_aware;
  /* We use the highest 1 byte of result to record
     how many bytes location counter has to move.  */
  int result = 0;
  Elf_Internal_Rela *irel_save = NULL;
  struct elf_nds32_link_hash_table *table;

  table = nds32_elf_hash_table (info);
  ex9_loop_aware = table->ex9_loop_aware;

  while ((*irel) != NULL && (*irel) < irelend && *off == (*irel)->r_offset)
    {
      switch (ELF32_R_TYPE ((*irel)->r_info))
	{
	case R_NDS32_RELAX_REGION_BEGIN:
	  /* Ignore code block.  */
	  nested_ex9 = FALSE;
	  nested_loop = FALSE;
	  if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG)
	      || (ex9_loop_aware
		  && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG)))
	    {
	      /* Check the region if loop or not.  If it is true and
		 ex9-loop-aware is true, ignore the region till region end.  */
	      /* To save the status for in .no_relax ex9 region and
		 loop region to conform the block can do ex9 relaxation.  */
	      nested_ex9 = ((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG);
	      nested_loop = (ex9_loop_aware
			     && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG));
	      while ((*irel) && (*irel) < irelend && (nested_ex9 || nested_loop))
		{
		  (*irel)++;
		  if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_BEGIN)
		    {
		      /* There may be nested region.  */
		      if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
			nested_ex9 = TRUE;
		      else if (ex9_loop_aware
			       && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
			nested_loop = TRUE;
		    }
		  else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_END)
		    {
		      /* The end of region.  */
		      if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
			nested_ex9 = FALSE;
		      else if (ex9_loop_aware
			       && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
			nested_loop = FALSE;
		    }
		  else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_LABEL
			   && ((*irel)->r_addend & 0x1f) == 2)
		    {
		      /* Alignment exist in the region.  */
		      result |= CLEAN_PRE;
		      if (((*irel)->r_offset -
			   get_nds32_elf_blank_total (&relax_blank_list,
						      (*irel)->r_offset, 0)) & 0x02)
			result |= PUSH_PRE;
		    }
		}
	      if ((*irel) >= irelend)
		*off = sec->size;
	      else
		*off = (*irel)->r_offset;

	      /* The final instruction in the region, regard this one as data to ignore it.  */
	      result |= DATA_EXIST;
	      return result;
	    }
	  break;

	case R_NDS32_LABEL:
	  if (((*irel)->r_addend & 0x1f) == 2)
	    {
	      /* Check this point is align and decide to do ex9 or not.  */
	      result |= CLEAN_PRE;
	      if (((*irel)->r_offset -
		   get_nds32_elf_blank_total (&relax_blank_list,
					      (*irel)->r_offset, 0)) & 0x02)
		result |= PUSH_PRE;
	    }
	  break;
	case R_NDS32_32_RELA:
	  /* Data.  */
	  result |= (4 << 24);
	  result |= DATA_EXIST;
	  break;
	case R_NDS32_16_RELA:
	  /* Data.  */
	  result |= (2 << 24);
	  result |= DATA_EXIST;
	  break;
	case R_NDS32_DATA:
	  /* Data.  */
	  /* The least code alignment is 2.  If the data is only one byte,
	     we have to shift one more byte.  */
	  if ((*irel)->r_addend == 1)
	    result |= ((*irel)->r_addend << 25) ;
	  else
	    result |= ((*irel)->r_addend << 24) ;

	  result |= DATA_EXIST;
	  break;

	case R_NDS32_25_PCREL_RELA:
	case R_NDS32_SDA16S3_RELA:
	case R_NDS32_SDA15S3_RELA:
	case R_NDS32_SDA15S3:
	case R_NDS32_SDA17S2_RELA:
	case R_NDS32_SDA15S2_RELA:
	case R_NDS32_SDA12S2_SP_RELA:
	case R_NDS32_SDA12S2_DP_RELA:
	case R_NDS32_SDA15S2:
	case R_NDS32_SDA18S1_RELA:
	case R_NDS32_SDA15S1_RELA:
	case R_NDS32_SDA15S1:
	case R_NDS32_SDA19S0_RELA:
	case R_NDS32_SDA15S0_RELA:
	case R_NDS32_SDA15S0:
	case R_NDS32_HI20_RELA:
	case R_NDS32_LO12S0_ORI_RELA:
	case R_NDS32_LO12S0_RELA:
	case R_NDS32_LO12S1_RELA:
	case R_NDS32_LO12S2_RELA:
	  /* These relocation is supported ex9 relaxation currently.  */
	  /* We have to save the relocation for using later, since we have
	     to check there is any alignment in the same address.  */
	  irel_save = *irel;
	  break;
	default:
	  /* Not support relocations.  */
	  if (ELF32_R_TYPE ((*irel)->r_info) < ARRAY_SIZE (nds32_elf_howto_table)
	      && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_NONE
	      && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_INSN16)
	    {
	      /* Note: To optimize aggressively, it maybe can ignore R_NDS32_INSN16 here.
		 But we have to consider if there is any side-effect.  */
	      if (!(result & DATA_EXIST))
		{
		  /* We have to confirm there is no data relocation in the
		     same address.  In general case, this won't happen.  */
		  /* We have to do ex9 conservative, for those relocation not
		     considerd we ignore instruction.  */
		  result |= DATA_EXIST;
		  if (*(contents + *off) & 0x80)
		    result |= (2 << 24);
		  else
		    result |= (4 << 24);
		  break;
		}
	    }
	}
      if ((*irel) < irelend
	  && ((*irel) + 1) < irelend
	  && (*irel)->r_offset == ((*irel) + 1)->r_offset)
	/* There are relocations pointing to the same address, we have to
	   check all of them.  */
	(*irel)++;
      else
	{
	  if (irel_save)
	    *irel = irel_save;
	  return result;
	}
    }
  return result;
}

/* Replace with ex9 instruction.  */

static bfd_boolean
nds32_elf_ex9_push_insn (uint16_t insn16, bfd_byte *contents, bfd_vma pre_off,
			 nds32_elf_blank_t **relax_blank_list,
			 struct elf_nds32_irel_entry *pre_irel_ptr,
			 struct elf_nds32_irel_entry **irel_list)
{
  if (insn16 != 0)
    {
      /* Implement the ex9 relaxation.  */
      bfd_putb16 (insn16, contents + pre_off);
      if (!insert_nds32_elf_blank_recalc_total (relax_blank_list,
						pre_off + 2, 2))
	return FALSE;
      if (pre_irel_ptr != NULL)
	nds32_elf_insert_irel_entry (irel_list, pre_irel_ptr);
    }
  return TRUE;
}

/* Replace input file instruction which is in ex9 itable.  */

static bfd_boolean
nds32_elf_ex9_replace_instruction (struct bfd_link_info *info, bfd *abfd, asection *sec)
{
  struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
  bfd_byte *contents = NULL;
  bfd_vma off;
  uint16_t insn16, insn_ex9;
  /* `pre_*' are used to track previous instruction that can use ex9.it.  */
  bfd_vma pre_off = -1;
  uint16_t pre_insn16 = 0;
  struct elf_nds32_irel_entry *pre_irel_ptr = NULL;
  Elf_Internal_Rela *internal_relocs;
  Elf_Internal_Rela *irel;
  Elf_Internal_Rela *irelend;
  Elf_Internal_Shdr *symtab_hdr;
  Elf_Internal_Sym *isym = NULL;
  nds32_elf_blank_t *relax_blank_list = NULL;
  uint32_t insn = 0;
  uint32_t insn_with_reg = 0;
  uint32_t it_insn;
  uint32_t it_insn_with_reg;
  unsigned long r_symndx;
  asection *isec;
  struct elf_nds32_irel_entry *irel_list = NULL;
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
  int data_flag, do_replace, save_irel;
  struct elf_link_hash_entry_list *h_list;


  /* Load section instructions, relocations, and symbol table.  */
  if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
      || !nds32_get_local_syms (abfd, sec, &isym))
    return FALSE;
  internal_relocs =
    _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, TRUE /* keep_memory */);
  irelend = internal_relocs + sec->reloc_count;
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;

  off = 0;

  /* Check if the object enable ex9.  */
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
				 irelend, R_NDS32_RELAX_ENTRY);

  /* Check this section trigger ex9 relaxation.  */
  if (irel == NULL
      || irel >= irelend
      || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
      || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
	  && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
    return TRUE;

  irel = internal_relocs;

  /* Check alignment and fetch proper relocation.  */
  while (off < sec->size)
    {
      struct elf_link_hash_entry *h = NULL;
      struct elf_nds32_irel_entry *irel_ptr = NULL;

      /* Syn the instruction and the relocation.  */
      while (irel != NULL && irel < irelend && irel->r_offset < off)
	irel++;

      data_flag = nds32_elf_ex9_relocation_check (info, &irel, irelend,
						  relax_blank_list, sec,
						  &off, contents);
      if (data_flag & PUSH_PRE)
	if (!nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
				      &relax_blank_list, pre_irel_ptr,
				      &irel_list))
	  return FALSE;

      if (data_flag & CLEAN_PRE)
	{
	  pre_off = 0;
	  pre_insn16 = 0;
	  pre_irel_ptr = NULL;
	}
      if (data_flag & DATA_EXIST)
	{
	  /* We save the move offset in the highest byte.  */
	  off += (data_flag >> 24);
	  continue;
	}

      if (*(contents + off) & 0x80)
	{
	  /* 2-byte instruction.  */
	  off += 2;
	  continue;
	}

      /* Load the instruction and its opcode with register for comparing.  */
      ex9_insn = ex9_insn_head;
      insn = bfd_getb32 (contents + off);
      insn_with_reg = 0;
      while (ex9_insn)
	{
	  it_insn = strtol (ex9_insn->string, NULL, 16);
	  it_insn_with_reg = 0;
	  do_replace = 0;
	  save_irel = 0;

	  if (irel != NULL && irel < irelend && irel->r_offset == off)
	    {
	      /* Insn with relocation.  */
	      nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);

	      if (ex9_insn->irel != NULL)
		nds32_elf_get_insn_with_reg (ex9_insn->irel, it_insn,
					     &it_insn_with_reg);

	      if (ex9_insn->irel != NULL
		  && (ELF32_R_TYPE (irel->r_info) ==
		      ELF32_R_TYPE (ex9_insn->irel->r_info))
		  && (insn_with_reg == it_insn_with_reg))
		{
		  /* Insn relocation and format is the same as table entry.  */

		  if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
		      || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
		      || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
		      || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
		      || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
			  && ELF32_R_TYPE (irel->r_info) <=
			  R_NDS32_SDA12S2_SP_RELA)
		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
		    {
		      r_symndx = ELF32_R_SYM (irel->r_info);
		      if (r_symndx < symtab_hdr->sh_info)
			{
			  /* Local symbol.  */
			  int shndx = isym[r_symndx].st_shndx;

			  isec = elf_elfsections (abfd)[shndx]->bfd_section;
			  if (ex9_insn->sec == isec
			      && ex9_insn->irel->r_addend == irel->r_addend
			      && ex9_insn->irel->r_info == irel->r_info)
			    {
			      do_replace = 1;
			      save_irel = 1;
			    }
			}
		      else
			{
			  /* External symbol.  */
			  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
			  if (ex9_insn->m_list)
			    {
			      h_list = ex9_insn->m_list->h_list;
			      while (h_list)
				{
				  if (h == h_list->h
				      && (ex9_insn->m_list->irel->r_addend ==
					  irel->r_addend))
				    {
				      do_replace = 1;
				      save_irel = 1;
				      break;
				    }
				  h_list = h_list->next;
				}
			    }
			}
		    }
		  else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA)
		    {
		      r_symndx = ELF32_R_SYM (irel->r_info);
		      if (r_symndx < symtab_hdr->sh_info)
			{
			  /* Local symbols.  Compare its base symbol and offset.  */
			  int shndx = isym[r_symndx].st_shndx;

			  isec = elf_elfsections (abfd)[shndx]->bfd_section;
			  if (ex9_insn->sec == isec
			      && ex9_insn->irel->r_addend == irel->r_addend
			      && ex9_insn->irel->r_info == irel->r_info)
			    {
			      do_replace = 1;
			      save_irel = 1;
			    }
			}
		      else
			{
			  /* External symbol.  */
			  struct elf_link_hash_entry_mul_list *m_list;

			  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
			  m_list = ex9_insn->m_list;

			  while (m_list)
			    {
			      h_list = m_list->h_list;

			      while (h_list)
				{
				  if (h == h_list->h
				      && (m_list->irel->r_addend
					  == irel->r_addend))
				    {
				      do_replace = 1;
				      save_irel = 1;
				      if (ex9_insn->next
					  && ex9_insn->m_list
					  && ex9_insn->m_list == ex9_insn->next->m_list)
					{
					  /* sethi multiple entry must be fixed */
					  nds32_elf_ex9_insert_fix (sec, irel,
								    h, ex9_insn->order);
					}
				      break;
				    }
				  h_list = h_list->next;
				}
			      m_list = m_list->next;
			    }
			}
		    }
		}

	      /* Import table: Check the symbol hash table and the
		 jump target.  Only R_NDS32_25_PCREL_RELA now.  */
	      else if (ex9_insn->times == -1
		       && ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
		{
		  nds32_elf_get_insn_with_reg (irel, it_insn, &it_insn_with_reg);
		  if (insn_with_reg == it_insn_with_reg)
		    {
		      char code[10];
		      bfd_vma relocation;

		      r_symndx = ELF32_R_SYM (irel->r_info);
		      if (r_symndx >= symtab_hdr->sh_info)
			{
			  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
			  if ((h->root.type == bfd_link_hash_defined
			       || h->root.type == bfd_link_hash_defweak)
			      && h->root.u.def.section != NULL
			      && h->root.u.def.section->output_section != NULL
			      && h->root.u.def.section->gc_mark == 1
			      && bfd_is_abs_section (h->root.u.def.section)
			      && h->root.u.def.value > sec->size)
			    {
			      relocation = h->root.u.def.value +
				h->root.u.def.section->output_section->vma +
				h->root.u.def.section->output_offset;
			      relocation += irel->r_addend;
			      insn = insn_with_reg
				| ((relocation >> 1) & 0xffffff);
			      snprintf (code, sizeof (code), "%08x", insn);
			      if (strcmp (code, ex9_insn->string) == 0)
				{
				  do_replace = 1;
				  save_irel = 1;
				}
			    }
			}
		    }
		}
	      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
		       || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
		       || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
		{
		  /* These relocations do not have to relocate contens, so it can
		     be regard as instruction without relocation.  */
		  if (insn == it_insn && ex9_insn->irel == NULL)
		    do_replace = 1;
		}
	    }
	  else
	    {
	      /* Instruction without relocation, we only
		 have to compare their byte code.  */
	      if (insn == it_insn && ex9_insn->irel == NULL)
		do_replace = 1;
	    }

	  /* Insntruction match so replacing the code here.  */
	  if (do_replace == 1)
	    {
	      /* There are two formats of ex9 instruction.  */
	      if (ex9_insn->order < 32)
		insn_ex9 = INSN_EX9_IT_2;
	      else
		insn_ex9 = INSN_EX9_IT_1;
	      insn16 = insn_ex9 | ex9_insn->order;

	      /* Insert ex9 instruction.  */
	      nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
				       &relax_blank_list, pre_irel_ptr,
				       &irel_list);
	      pre_off = off;
	      pre_insn16 = insn16;

	      if (save_irel)
		{
		  /* For instuction with relocation do relax.  */
		  irel_ptr = (struct elf_nds32_irel_entry *)
		    bfd_malloc (sizeof (struct elf_nds32_irel_entry));
		  irel_ptr->irel = irel;
		  irel_ptr->next = NULL;
		  pre_irel_ptr = irel_ptr;
		}
	      else
		pre_irel_ptr = NULL;
	      break;
	    }
	  ex9_insn = ex9_insn->next;
	}
      off += 4;
    }

  /* Insert ex9 instruction.  */
  nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
			   &relax_blank_list, pre_irel_ptr,
			   &irel_list);

  /* Delete the redundant code.  */
  if (relax_blank_list)
    {
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
      relax_blank_list = NULL;
    }

  /* Clear the relocation that is replaced by ex9.  */
  while (irel_list)
    {
      struct elf_nds32_irel_entry *irel_ptr;

      irel_ptr = irel_list;
      irel_list = irel_ptr->next;
      irel_ptr->irel->r_info =
	ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info), R_NDS32_TRAN);
      free (irel_ptr);
    }
  return TRUE;
}

/* Initialize ex9 hash table.  */

int
nds32_elf_ex9_init (void)
{
  if (!bfd_hash_table_init_n (&ex9_code_table, nds32_elf_code_hash_newfunc,
			      sizeof (struct elf_nds32_code_hash_entry),
			      1023))
    {
      (*_bfd_error_handler) (_("Linker: cannot init ex9 hash table error \n"));
      return FALSE;
    }
  return TRUE;
}

/* Predict how many bytes will be relaxed with ex9 and ifc.  */

static void
nds32_elf_ex9_total_relax (struct bfd_link_info *info)
{
  struct elf_nds32_insn_times_entry *ex9_insn;
  struct elf_nds32_insn_times_entry *temp;
  int target_optimize;
  struct elf_nds32_link_hash_table *table;

  if (ex9_insn_head == NULL)
    return;

  table = nds32_elf_hash_table (info);
  target_optimize  = table->target_optimize;
  ex9_insn = ex9_insn_head;
  while (ex9_insn)
    {
      ex9_relax_size = ex9_insn->times * 2 + ex9_relax_size;
      temp = ex9_insn;
      ex9_insn = ex9_insn->next;
      free (temp);
    }
  ex9_insn_head = NULL;

  if ((target_optimize & NDS32_RELAX_JUMP_IFC_ON))
    {
      /* Examine ifc reduce size.  */
      struct elf_nds32_ifc_symbol_entry *ifc_ent = ifc_symbol_head;
      struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
      int size = 0;

      while (ifc_ent)
	{
	  if (ifc_ent->enable == 0)
	    {
	      /* Not ifc yet.  */
	      irel_ptr = ifc_ent->irel_head;
	      while (irel_ptr)
		{
		  size += 2;
		  irel_ptr = irel_ptr->next;
		}
	    }
	  size -= 2;
	  ifc_ent = ifc_ent->next;
	}
      ex9_relax_size += size;
    }
}

/* Finish ex9 table.  */

void
nds32_elf_ex9_finish (struct bfd_link_info *link_info)
{
  nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
  nds32_elf_order_insn_times (link_info);
  nds32_elf_ex9_total_relax (link_info);
  /* Traverse the hash table and count its times.  */
  nds32_elf_code_hash_traverse (nds32_elf_count_insn_times);
  nds32_elf_order_insn_times (link_info);
  nds32_elf_ex9_build_itable (link_info);
}

/* Relocate the entries in ex9 table.  */

static bfd_vma
nds32_elf_ex9_reloc_insn (struct elf_nds32_insn_times_entry *ptr,
			  struct bfd_link_info *link_info)
{
  Elf_Internal_Sym *isym = NULL;
  bfd_vma relocation = -1;
  struct elf_link_hash_entry *h;

  if (ptr->m_list != NULL)
    {
      /* Global symbol.  */
      h = ptr->m_list->h_list->h;
      if ((h->root.type == bfd_link_hash_defined
	   || h->root.type == bfd_link_hash_defweak)
	  && h->root.u.def.section != NULL
	  && h->root.u.def.section->output_section != NULL)
	{

	  relocation = h->root.u.def.value +
	    h->root.u.def.section->output_section->vma +
	    h->root.u.def.section->output_offset;
	  relocation += ptr->m_list->irel->r_addend;
	}
      else
	relocation = 0;
    }
  else if (ptr->sec !=NULL)
    {
      /* Local symbol.  */
      Elf_Internal_Sym sym;
      asection *sec = NULL;
      asection isec;
      asection *isec_ptr = &isec;
      Elf_Internal_Rela irel_backup = *(ptr->irel);
      asection *sec_backup = ptr->sec;
      bfd *abfd = ptr->sec->owner;

      if (!nds32_get_local_syms (abfd, sec, &isym))
	return FALSE;
      isym = isym + ELF32_R_SYM (ptr->irel->r_info);

      sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
      if (sec != NULL)
	*isec_ptr = *sec;
      sym = *isym;

      /* The purpose is same as elf_link_input_bfd.  */
      if (isec_ptr != NULL
	  && isec_ptr->sec_info_type == SEC_INFO_TYPE_MERGE
	  && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
	{
	  sym.st_value =
	    _bfd_merged_section_offset (ptr->sec->output_section->owner, &isec_ptr,
					elf_section_data (isec_ptr)->sec_info,
					isym->st_value);
	}
      relocation = _bfd_elf_rela_local_sym (link_info->output_bfd, &sym,
					    &ptr->sec, ptr->irel);
      if (ptr->irel != NULL)
	relocation += ptr->irel->r_addend;

      /* Restore origin value since there may be some insntructions that
	 could not be replaced with ex9.it.  */
      *(ptr->irel) = irel_backup;
      ptr->sec = sec_backup;
    }

  return relocation;
}

/* Import ex9 table and build list.  */

void
nds32_elf_ex9_import_table (struct bfd_link_info *info)
{
  int num = 0;
  bfd_byte *contents;
  unsigned long insn;
  FILE *ex9_import_file;
  int update_ex9_table;
  struct elf_nds32_link_hash_table *table;

  table = nds32_elf_hash_table (info);
  ex9_import_file = table->ex9_import_file;
  rewind (table->ex9_import_file);

  contents = bfd_malloc (sizeof (bfd_byte) * 4);

  /* Read instructions from the input file and build the list.  */
  while (!feof (ex9_import_file))
    {
      char *code;
      struct elf_nds32_insn_times_entry *ptr;
      size_t nread;

      nread = fread (contents, sizeof (bfd_byte) * 4, 1, ex9_import_file);
      /* Ignore the final byte 0x0a.  */
      if (nread < 1)
	break;
      insn = bfd_getb32 (contents);
      code = bfd_malloc (sizeof (char) * 9);
      snprintf (code, 9, "%08lx", insn);
      ptr = bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
      ptr->string = code;
      ptr->order = num;
      ptr->times = -1;
      ptr->sec = NULL;
      ptr->m_list = NULL;
      ptr->rel_backup.r_offset = 0;
      ptr->rel_backup.r_info = 0;
      ptr->rel_backup.r_addend = 0;
      ptr->irel = NULL;
      ptr->next = NULL;
      nds32_elf_ex9_insert_entry (ptr);
      num++;
    }

  update_ex9_table = table->update_ex9_table;
  if (update_ex9_table == 1)
    {
      /* It has to consider of sethi need to use multiple page
	 but it not be done yet.  */
      nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
      nds32_elf_order_insn_times (info);
    }
}

/* Export ex9 table.  */

static void
nds32_elf_ex9_export (struct bfd_link_info *info,
		      bfd_byte *contents, int size)
{
  FILE *ex9_export_file;
  struct elf_nds32_link_hash_table *table;

  table = nds32_elf_hash_table (info);
  ex9_export_file = table->ex9_export_file;
  fwrite (contents, sizeof (bfd_byte), size, ex9_export_file);
  fclose (ex9_export_file);
}

/* Adjust relocations of J and JAL in ex9.itable.
   Export ex9 table.  */

static void
nds32_elf_ex9_reloc_jmp (struct bfd_link_info *link_info)
{
  asection *table_sec = NULL;
  struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
  struct elf_nds32_insn_times_entry *temp_ptr, *temp_ptr2;
  bfd *it_abfd;
  uint32_t insn, insn_with_reg, source_insn;
  bfd_byte *contents = NULL, *source_contents = NULL;
  int size = 0;
  bfd_vma gp;
  int shift, update_ex9_table, offset = 0;
  reloc_howto_type *howto = NULL;
  Elf_Internal_Rela rel_backup;
  unsigned short insn_ex9;
  struct elf_nds32_link_hash_table *table;
  FILE *ex9_export_file;
  static bfd_boolean done = FALSE;

  if (done)
    return;

  done = TRUE;

  table = nds32_elf_hash_table (link_info);
  if (table)
    table->relax_status |= NDS32_RELAX_EX9_DONE;


  update_ex9_table = table->update_ex9_table;
  /* Generated ex9.itable exactly.  */
  if (update_ex9_table == 0)
    {
      for (it_abfd = link_info->input_bfds; it_abfd != NULL;
	   it_abfd = it_abfd->link.next)
	{
	  table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
	  if (table_sec != NULL)
	    break;
	}

      if (table_sec != NULL)
	{
	  bfd *output_bfd;

	  output_bfd = table_sec->output_section->owner;
	  nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
	  if (table_sec->size == 0)
	    return;

	  if (!nds32_get_section_contents (it_abfd, table_sec, &contents, TRUE))
	    return;
	}
    }
  else
    {
      /* Set gp.  */
      bfd *output_bfd;

      output_bfd = link_info->input_bfds->sections->output_section->owner;
      nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
      contents = bfd_malloc (sizeof (bfd_byte) * 2048);
    }

  /* Relocate instruction.  */
  while (ex9_insn)
    {
      bfd_vma relocation, min_relocation = 0xffffffff;

      insn = strtol (ex9_insn->string, NULL, 16);
      insn_with_reg = 0;
      if (ex9_insn->m_list != NULL || ex9_insn->sec != NULL)
	{
	  if (ex9_insn->m_list)
	    rel_backup = ex9_insn->m_list->rel_backup;
	  else
	    rel_backup = ex9_insn->rel_backup;

	  nds32_elf_get_insn_with_reg (&rel_backup, insn, &insn_with_reg);
	  howto =
	    bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE
						   (rel_backup.r_info));
	  shift = howto->rightshift;
	  if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_25_PCREL_RELA
	      || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_ORI_RELA
	      || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_RELA
	      || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S1_RELA
	      || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S2_RELA)
	    {
	      relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
	      insn =
		insn_with_reg | ((relocation >> shift) &
				 nds32_elf_irel_mask (&rel_backup));
	      bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
	    }
	  else if ((ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3
		    && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0)
		   || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3_RELA
		       && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0_RELA)
		   || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA12S2_DP_RELA
		       && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA12S2_SP_RELA)
		   || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA16S3_RELA
		       && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA19S0_RELA))
	    {
	      relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
	      insn =
		insn_with_reg | (((relocation - gp) >> shift) &
				 nds32_elf_irel_mask (&rel_backup));
	      bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
	    }
	  else if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_HI20_RELA)
	    {
	      /* Sethi may be multiple entry for one insn.  */
	      if (ex9_insn->next && ex9_insn->m_list
		  && ex9_insn->m_list == ex9_insn->next->m_list)
		{
		  struct elf_link_hash_entry_mul_list *m_list;
		  struct elf_nds32_ex9_refix *fix_ptr;
		  struct elf_link_hash_entry *h;

		  temp_ptr = ex9_insn;
		  temp_ptr2 = ex9_insn;
		  m_list = ex9_insn->m_list;
		  while (m_list)
		    {
		      h = m_list->h_list->h;
		      relocation = h->root.u.def.value +
			h->root.u.def.section->output_section->vma +
			h->root.u.def.section->output_offset;
		      relocation += m_list->irel->r_addend;

		      if (relocation < min_relocation)
			min_relocation = relocation;
		      m_list = m_list->next;
		    }
		  relocation = min_relocation;

		  /* Put insntruction into ex9 table.  */
		  insn = insn_with_reg
		    | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
		  bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
		  relocation = relocation + 0x1000;	/* hi20 */

		  while (ex9_insn->next && ex9_insn->m_list
			 && ex9_insn->m_list == ex9_insn->next->m_list)
		    {
		      /* Multiple sethi.  */
		      ex9_insn = ex9_insn->next;
		      size += 4;
		      insn =
			insn_with_reg | ((relocation >> shift) &
					 nds32_elf_irel_mask (&rel_backup));
		      bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
		      relocation = relocation + 0x1000;	/* hi20 */
		    }

		  fix_ptr = ex9_refix_head;
		  while (fix_ptr)
		    {
		      /* Fix ex9 insn.  */
		      /* temp_ptr2 points to the head of multiple sethi.  */
		      temp_ptr = temp_ptr2;
		      while (fix_ptr->order != temp_ptr->order && fix_ptr->next)
			{
			  fix_ptr = fix_ptr->next;
			}
		      if (fix_ptr->order != temp_ptr->order)
			break;

		      /* Set source insn.  */
		      relocation =
			fix_ptr->h->root.u.def.value +
			fix_ptr->h->root.u.def.section->output_section->vma +
			fix_ptr->h->root.u.def.section->output_offset;
		      relocation += fix_ptr->irel->r_addend;
		      /* sethi imm is imm20s.  */
		      source_insn = insn_with_reg | ((relocation >> shift) & 0xfffff);

		      while (temp_ptr)
			{
			  /* Match entry and source code.  */
			  insn = bfd_getb32 (contents + (temp_ptr->order) * 4 + offset);
			  if (insn == source_insn)
			    {
			      /* Fix the ex9 insn.  */
			      if (temp_ptr->order != fix_ptr->order)
				{
				  if (!nds32_get_section_contents
					 (fix_ptr->sec->owner, fix_ptr->sec,
					  &source_contents, TRUE))
				    (*_bfd_error_handler)
				      (_("Linker: error cannot fixed ex9 relocation \n"));
				  if (temp_ptr->order < 32)
				    insn_ex9 = INSN_EX9_IT_2;
				  else
				    insn_ex9 = INSN_EX9_IT_1;
				  insn_ex9 = insn_ex9 | temp_ptr->order;
				  bfd_putb16 (insn_ex9, source_contents + fix_ptr->irel->r_offset);
				}
				break;
			    }
			  else
			    {
			      if (!temp_ptr->next || temp_ptr->m_list != temp_ptr->next->m_list)
				(*_bfd_error_handler)
				  (_("Linker: error cannot fixed ex9 relocation \n"));
			      else
				temp_ptr = temp_ptr->next;
			    }
			}
		      fix_ptr = fix_ptr->next;
		    }
		}
	      else
		{
		  relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
		  insn = insn_with_reg
			 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
		  bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
		}
	    }
	}
      else
	{
	  /* Insn without relocation does not have to be fixed
	     if need to update export table.  */
	  if (update_ex9_table == 1)
	    bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
	}
      ex9_insn = ex9_insn->next;
      size += 4;
    }

  ex9_export_file = table->ex9_export_file;
  if (ex9_export_file != NULL)
    nds32_elf_ex9_export (link_info, contents, table_sec->size);
  else if (update_ex9_table == 1)
    {
      table->ex9_export_file = table->ex9_import_file;
      rewind (table->ex9_export_file);
      nds32_elf_ex9_export (link_info, contents, size);
    }
}

/* Generate ex9 hash table.  */

static bfd_boolean
nds32_elf_ex9_build_hash_table (bfd *abfd, asection *sec,
				struct bfd_link_info *link_info)
{
  Elf_Internal_Rela *internal_relocs;
  Elf_Internal_Rela *irelend;
  Elf_Internal_Rela *irel;
  Elf_Internal_Rela *jrel;
  Elf_Internal_Rela rel_backup;
  Elf_Internal_Shdr *symtab_hdr;
  Elf_Internal_Sym *isym = NULL;
  asection *isec;
  struct elf_link_hash_entry **sym_hashes;
  bfd_byte *contents = NULL;
  bfd_vma off = 0;
  unsigned long r_symndx;
  uint32_t insn, insn_with_reg;
  struct elf_link_hash_entry *h;
  int data_flag, shift, align;
  bfd_vma relocation;
  /* Suppress ex9 if `.no_relax ex9' or inner loop.  */
  reloc_howto_type *howto = NULL;

  sym_hashes = elf_sym_hashes (abfd);
  /* Load section instructions, relocations, and symbol table.  */
  if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
    return FALSE;

  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
					       TRUE /* keep_memory */);
  irelend = internal_relocs + sec->reloc_count;
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  if (!nds32_get_local_syms (abfd, sec, &isym))
    return FALSE;

  /* Check the object if enable ex9.  */
  irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
				 R_NDS32_RELAX_ENTRY);

  /* Check this section trigger ex9 relaxation.  */
  if (irel == NULL
      || irel >= irelend
      || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
      || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
	  && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
    return TRUE;

  irel = internal_relocs;

  /* Push each insn into hash table.  */
  while (off < sec->size)
    {
      char code[10];
      struct elf_nds32_code_hash_entry *entry;

      while (irel != NULL && irel < irelend && irel->r_offset < off)
	irel++;

      data_flag = nds32_elf_ex9_relocation_check (link_info, &irel, irelend,
						  NULL, sec, &off, contents);
      if (data_flag & DATA_EXIST)
	{
	  /* We save the move offset in the highest byte.  */
	  off += (data_flag >> 24);
	  continue;
	}

      if (*(contents + off) & 0x80)
	{
	  off += 2;
	}
      else
	{
	  h = NULL;
	  isec = NULL;
	  jrel = NULL;
	  rel_backup.r_info = 0;
	  rel_backup.r_offset = 0;
	  rel_backup.r_addend = 0;
	  /* Load the instruction and its opcode with register for comparing.  */
	  insn = bfd_getb32 (contents + off);
	  insn_with_reg = 0;
	  if (irel != NULL && irel < irelend && irel->r_offset == off)
	    {
	      nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
	      howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
	      shift = howto->rightshift;
	      align = (1 << shift) - 1;
	      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
		  || ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA
		  || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
		  || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
		  || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
		  || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
		  ||(ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
		     && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
		  || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
		      && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
		  || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
		      && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
		  || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
		      && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
		{
		  r_symndx = ELF32_R_SYM (irel->r_info);
		  jrel = irel;
		  rel_backup = *irel;
		  if (r_symndx < symtab_hdr->sh_info)
		    {
		      /* Local symbol.  */
		      int shndx = isym[r_symndx].st_shndx;

		      bfd_vma st_value = (isym + r_symndx)->st_value;
		      isec = elf_elfsections (abfd)[shndx]->bfd_section;
		      relocation = (isec->output_section->vma + isec->output_offset
				    + st_value + irel->r_addend);
		    }
		  else
		    {
		      /* External symbol.  */
		      bfd_boolean warned ATTRIBUTE_UNUSED;
		      bfd_boolean ignored ATTRIBUTE_UNUSED;
		      bfd_boolean unresolved_reloc ATTRIBUTE_UNUSED;
		      asection *sym_sec;

		      /* Maybe there is a better way to get h and relocation */
		      RELOC_FOR_GLOBAL_SYMBOL (link_info, abfd, sec, irel,
					       r_symndx, symtab_hdr, sym_hashes,
					       h, sym_sec, relocation,
					       unresolved_reloc, warned, ignored);
		      relocation += irel->r_addend;
		      if ((h->root.type != bfd_link_hash_defined
			   && h->root.type != bfd_link_hash_defweak)
			  || strcmp (h->root.root.string, "_FP_BASE_") == 0)
			{
			  off += 4;
			  continue;
			}
		    }

		  /* Check for gp relative instruction alignment.  */
		  if ((ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
		       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
		    {
		      bfd_vma gp;
		      bfd *output_bfd = sec->output_section->owner;
		      bfd_reloc_status_type r;

		      /* If the symbol is in the abs section, the out_bfd will be null.
			 This happens when the relocation has a symbol@GOTOFF.  */
		      r = nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
		      if (r != bfd_reloc_ok)
			{
			  off += 4;
			  continue;
			}

		      relocation -= gp;

		      /* Make sure alignment is correct.  */
		      if (relocation & align)
			{
			  /* Incorrect alignment.  */
			  (*_bfd_error_handler)
			    (_("%s: warning: unaligned small data access. "
			       "For entry: {%d, %d, %d}, addr = 0x%x, align = 0x%x."),
			     bfd_get_filename (abfd), irel->r_offset,
			     irel->r_info, irel->r_addend, relocation, align);
			  off += 4;
			  continue;
			}
		    }

		  insn = insn_with_reg
		    | ((relocation >> shift) & nds32_elf_irel_mask (irel));
		}
	      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
		       || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
		       || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
		{
		  /* These relocations do not have to relocate contens, so it can
		     be regard as instruction without relocation.  */
		}
	      else
		{
		  off += 4;
		  continue;
		}
	    }

	  snprintf (code, sizeof (code), "%08x", insn);
	  /* Copy "code".  */
	  entry = (struct elf_nds32_code_hash_entry*)
	    bfd_hash_lookup (&ex9_code_table, code, TRUE, TRUE);
	  if (entry == NULL)
	    {
	      (*_bfd_error_handler)
		(_("%P%F: failed creating ex9.it %s hash table: %E\n"), code);
	      return FALSE;
	    }
	  if (h)
	    {
	      if (h->root.type == bfd_link_hash_undefined)
		return TRUE;
	      /* Global symbol.  */
	      /* In order to do sethi with different symbol but same value.  */
	      if (entry->m_list == NULL)
		{
		  struct elf_link_hash_entry_mul_list *m_list_new;
		  struct elf_link_hash_entry_list *h_list_new;

		  m_list_new = (struct elf_link_hash_entry_mul_list *)
		    bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
		  h_list_new = (struct elf_link_hash_entry_list *)
		    bfd_malloc (sizeof (struct elf_link_hash_entry_list));
		  entry->m_list = m_list_new;
		  m_list_new->h_list = h_list_new;
		  m_list_new->rel_backup = rel_backup;
		  m_list_new->times = 1;
		  m_list_new->irel = jrel;
		  m_list_new->next = NULL;
		  h_list_new->h = h;
		  h_list_new->next = NULL;
		}
	      else
		{
		  struct elf_link_hash_entry_mul_list *m_list = entry->m_list;
		  struct elf_link_hash_entry_list *h_list;

		  while (m_list)
		    {
		      /* Build the different symbols that point to the same address.  */
		      h_list = m_list->h_list;
		      if (h_list->h->root.u.def.value == h->root.u.def.value
			  && h_list->h->root.u.def.section->output_section->vma
			     == h->root.u.def.section->output_section->vma
			  && h_list->h->root.u.def.section->output_offset
			     == h->root.u.def.section->output_offset
			  && m_list->rel_backup.r_addend == rel_backup.r_addend)
			{
			  m_list->times++;
			  m_list->irel = jrel;
			  while (h_list->h != h && h_list->next)
			    h_list = h_list->next;
			  if (h_list->h != h)
			    {
			      struct elf_link_hash_entry_list *h_list_new;

			      h_list_new = (struct elf_link_hash_entry_list *)
				bfd_malloc (sizeof (struct elf_link_hash_entry_list));
			      h_list->next = h_list_new;
			      h_list_new->h = h;
			      h_list_new->next = NULL;
			    }
			  break;
			}
		      /* The sethi case may have different address but the
			 hi20 is the same.  */
		      else if (ELF32_R_TYPE (jrel->r_info) == R_NDS32_HI20_RELA
			       && m_list->next == NULL)
			{
			  struct elf_link_hash_entry_mul_list *m_list_new;
			  struct elf_link_hash_entry_list *h_list_new;

			  m_list_new = (struct elf_link_hash_entry_mul_list *)
			    bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
			  h_list_new = (struct elf_link_hash_entry_list *)
			    bfd_malloc (sizeof (struct elf_link_hash_entry_list));
			  m_list->next = m_list_new;
			  m_list_new->h_list = h_list_new;
			  m_list_new->rel_backup = rel_backup;
			  m_list_new->times = 1;
			  m_list_new->irel = jrel;
			  m_list_new->next = NULL;
			  h_list_new->h = h;
			  h_list_new->next = NULL;
			  break;
			}
		      m_list = m_list->next;
		    }
		  if (!m_list)
		    {
		      off += 4;
		      continue;
		    }
		}
	    }
	  else
	    {
	      /* Local symbol and insn without relocation*/
	      entry->times++;
	      entry->rel_backup = rel_backup;
	    }

	  /* Use in sethi insn with constant and global symbol in same format.  */
	  if (!jrel)
	    entry->const_insn = 1;
	  else
	    entry->irel = jrel;
	  entry->sec = isec;
	  off += 4;
	}
    }
  return TRUE;
}

/* Set the _ITB_BASE, and point it to ex9 table.  */

bfd_boolean
nds32_elf_ex9_itb_base (struct bfd_link_info *link_info)
{
  bfd *abfd;
  asection *sec;
  bfd *output_bfd = NULL;
  struct bfd_link_hash_entry *bh = NULL;

  if (is_ITB_BASE_set == 1)
    return TRUE;

  is_ITB_BASE_set = 1;

  bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_", FALSE, FALSE, TRUE);

  if (bh && (bh->type == bfd_link_hash_defined
	     || bh->type == bfd_link_hash_defweak))
    return TRUE;

  for (abfd = link_info->input_bfds; abfd != NULL;
       abfd = abfd->link.next)
    {
      sec = bfd_get_section_by_name (abfd, ".ex9.itable");
      if (sec != NULL)
	{
	  output_bfd = sec->output_section->owner;
	  break;
	}
    }
  if (output_bfd == NULL)
    {
      output_bfd = link_info->output_bfd;
      if (output_bfd->sections == NULL)
	return TRUE;
      else
	sec = bfd_abs_section_ptr;
    }
  bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_",
			     FALSE, FALSE, TRUE);
  return (_bfd_generic_link_add_one_symbol
	  (link_info, output_bfd, "_ITB_BASE_",
	   BSF_GLOBAL | BSF_WEAK, sec, 0,
	   (const char *) NULL, FALSE, get_elf_backend_data
	   (output_bfd)->collect, &bh));
} /* End EX9.IT  */


#define ELF_ARCH				bfd_arch_nds32
#define ELF_MACHINE_CODE			EM_NDS32
#define ELF_MAXPAGESIZE				0x1000
#define ELF_TARGET_ID                           NDS32_ELF_DATA

#define TARGET_BIG_SYM				nds32_elf32_be_vec
#define TARGET_BIG_NAME				"elf32-nds32be"
#define TARGET_LITTLE_SYM			nds32_elf32_le_vec
#define TARGET_LITTLE_NAME			"elf32-nds32le"

#define elf_info_to_howto			nds32_info_to_howto
#define elf_info_to_howto_rel			nds32_info_to_howto_rel

#define bfd_elf32_bfd_link_hash_table_create	nds32_elf_link_hash_table_create
#define bfd_elf32_bfd_merge_private_bfd_data	nds32_elf_merge_private_bfd_data
#define bfd_elf32_bfd_print_private_bfd_data	nds32_elf_print_private_bfd_data
#define bfd_elf32_bfd_relax_section		nds32_elf_relax_section
#define bfd_elf32_bfd_set_private_flags		nds32_elf_set_private_flags

#define bfd_elf32_mkobject		        nds32_elf_mkobject
#define elf_backend_action_discarded		nds32_elf_action_discarded
#define elf_backend_add_symbol_hook		nds32_elf_add_symbol_hook
#define elf_backend_check_relocs		nds32_elf_check_relocs
#define elf_backend_adjust_dynamic_symbol	nds32_elf_adjust_dynamic_symbol
#define elf_backend_create_dynamic_sections	nds32_elf_create_dynamic_sections
#define elf_backend_finish_dynamic_sections	nds32_elf_finish_dynamic_sections
#define elf_backend_finish_dynamic_symbol	nds32_elf_finish_dynamic_symbol
#define elf_backend_size_dynamic_sections	nds32_elf_size_dynamic_sections
#define elf_backend_relocate_section		nds32_elf_relocate_section
#define elf_backend_gc_mark_hook		nds32_elf_gc_mark_hook
#define elf_backend_gc_sweep_hook		nds32_elf_gc_sweep_hook
#define elf_backend_grok_prstatus		nds32_elf_grok_prstatus
#define elf_backend_grok_psinfo			nds32_elf_grok_psinfo
#define elf_backend_reloc_type_class		nds32_elf_reloc_type_class
#define elf_backend_copy_indirect_symbol	nds32_elf_copy_indirect_symbol
#define elf_backend_link_output_symbol_hook	nds32_elf_output_symbol_hook
#define elf_backend_output_arch_syms		nds32_elf_output_arch_syms
#define elf_backend_object_p			nds32_elf_object_p
#define elf_backend_final_write_processing	nds32_elf_final_write_processing
#define elf_backend_special_sections		nds32_elf_special_sections
#define bfd_elf32_bfd_get_relocated_section_contents \
                                nds32_elf_get_relocated_section_contents

#define elf_backend_can_gc_sections		1
#define elf_backend_can_refcount		1
#define elf_backend_want_got_plt		1
#define elf_backend_plt_readonly		1
#define elf_backend_want_plt_sym		0
#define elf_backend_got_header_size		12
#define elf_backend_may_use_rel_p		1
#define elf_backend_default_use_rela_p		1
#define elf_backend_may_use_rela_p		1

#include "elf32-target.h"

#undef ELF_MAXPAGESIZE
#define ELF_MAXPAGESIZE				0x2000

#undef TARGET_BIG_SYM
#define TARGET_BIG_SYM				nds32_elf32_linux_be_vec
#undef TARGET_BIG_NAME
#define TARGET_BIG_NAME				"elf32-nds32be-linux"
#undef TARGET_LITTLE_SYM
#define TARGET_LITTLE_SYM			nds32_elf32_linux_le_vec
#undef TARGET_LITTLE_NAME
#define TARGET_LITTLE_NAME			"elf32-nds32le-linux"
#undef elf32_bed
#define elf32_bed				elf32_nds32_lin_bed

#include "elf32-target.h"