aboutsummaryrefslogtreecommitdiff
path: root/bfd/elfxx-riscv.c
blob: 1fe7e5d3df2de243d0eef707d772996335b5acce (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
/* RISC-V-specific support for ELF.
   Copyright (C) 2011-2024 Free Software Foundation, Inc.

   Contributed by Andrew Waterman (andrew@sifive.com).
   Based on TILE-Gx and MIPS targets.

   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; see the file COPYING3. If not,
   see <http://www.gnu.org/licenses/>.  */

#include "sysdep.h"
#include "bfd.h"
#include "libbfd.h"
#include "elf-bfd.h"
#include "elf/riscv.h"
#include "opcode/riscv.h"
#include "libiberty.h"
#include "elfxx-riscv.h"
#include "safe-ctype.h"

#define MINUS_ONE ((bfd_vma)0 - 1)

/* Special handler for ADD/SUB relocations that allows them to be filled out
   both in the pre-linked and post-linked file.  This is necessary to make
   pre-linked debug info work, as due to linker relaxations we need to emit
   relocations for the debug info.  */
static bfd_reloc_status_type riscv_elf_add_sub_reloc
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
static bfd_reloc_status_type riscv_elf_ignore_reloc
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);

/* The relocation table used for SHT_RELA sections.  */

static reloc_howto_type howto_table[] =
{
  /* No relocation.  */
  HOWTO (R_RISCV_NONE,			/* type */
	 0,				/* rightshift */
	 0,				/* size */
	 0,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_NONE",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0,				/* dst_mask */
	 false),			/* pcrel_offset */

  /* 32 bit relocation.  */
  HOWTO (R_RISCV_32,			/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_32",			/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffffffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  /* 64 bit relocation.  */
  HOWTO (R_RISCV_64,			/* type */
	 0,				/* rightshift */
	 8,				/* size */
	 64,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_64",			/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 MINUS_ONE,			/* dst_mask */
	 false),			/* pcrel_offset */

  /* Relocation against a local symbol in a shared object.  */
  HOWTO (R_RISCV_RELATIVE,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_RELATIVE",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffffffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  HOWTO (R_RISCV_COPY,			/* type */
	 0,				/* rightshift */
	 0,				/* this one is variable size */
	 0,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_COPY",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0,				/* dst_mask */
	 false),			/* pcrel_offset */

  HOWTO (R_RISCV_JUMP_SLOT,		/* type */
	 0,				/* rightshift */
	 8,				/* size */
	 64,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_bitfield,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_JUMP_SLOT",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0,				/* dst_mask */
	 false),			/* pcrel_offset */

  /* Dynamic TLS relocations.  */
  HOWTO (R_RISCV_TLS_DTPMOD32,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLS_DTPMOD32",	/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffffffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  HOWTO (R_RISCV_TLS_DTPMOD64,		/* type */
	 0,				/* rightshift */
	 8,				/* size */
	 64,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLS_DTPMOD64",	/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 MINUS_ONE,			/* dst_mask */
	 false),			/* pcrel_offset */

  HOWTO (R_RISCV_TLS_DTPREL32,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLS_DTPREL32",	/* name */
	 true,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffffffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  HOWTO (R_RISCV_TLS_DTPREL64,		/* type */
	 0,				/* rightshift */
	 8,				/* size */
	 64,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLS_DTPREL64",	/* name */
	 true,				/* partial_inplace */
	 0,				/* src_mask */
	 MINUS_ONE,			/* dst_mask */
	 false),			/* pcrel_offset */

  HOWTO (R_RISCV_TLS_TPREL32,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLS_TPREL32",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffffffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  HOWTO (R_RISCV_TLS_TPREL64,		/* type */
	 0,				/* rightshift */
	 8,				/* size */
	 64,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLS_TPREL64",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 MINUS_ONE,			/* dst_mask */
	 false),			/* pcrel_offset */

  HOWTO (R_RISCV_TLSDESC,		/* type */
	 0,				/* rightshift */
	 0,				/* size is handled by dynamic linker */
	 0,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLSDESC",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 MINUS_ONE,			/* dst_mask */
	 false),			/* pcrel_offset */

  EMPTY_HOWTO (13),
  EMPTY_HOWTO (14),
  EMPTY_HOWTO (15),

  /* 12-bit PC-relative branch offset.  */
  HOWTO (R_RISCV_BRANCH,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_BRANCH",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_BTYPE_IMM (-1U),	/* dst_mask */
	 true),				/* pcrel_offset */

  /* 20-bit PC-relative jump offset.  */
  HOWTO (R_RISCV_JAL,			/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_JAL",			/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_JTYPE_IMM (-1U),	/* dst_mask */
	 true),				/* pcrel_offset */

  /* 32-bit PC-relative function call (AUIPC/JALR).  */
  HOWTO (R_RISCV_CALL,			/* type */
	 0,				/* rightshift */
	 8,				/* size */
	 64,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_CALL",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_UTYPE_IMM (-1U) | ((bfd_vma) ENCODE_ITYPE_IMM (-1U) << 32),
					/* dst_mask */
	 true),				/* pcrel_offset */

  /* Like R_RISCV_CALL, but not locally binding.  */
  HOWTO (R_RISCV_CALL_PLT,		/* type */
	 0,				/* rightshift */
	 8,				/* size */
	 64,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_CALL_PLT",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_UTYPE_IMM (-1U) | ((bfd_vma) ENCODE_ITYPE_IMM (-1U) << 32),
					/* dst_mask */
	 true),				/* pcrel_offset */

  /* High 20 bits of 32-bit PC-relative GOT access.  */
  HOWTO (R_RISCV_GOT_HI20,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_GOT_HI20",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_UTYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* High 20 bits of 32-bit PC-relative TLS IE GOT access.  */
  HOWTO (R_RISCV_TLS_GOT_HI20,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLS_GOT_HI20",	/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_UTYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* High 20 bits of 32-bit PC-relative TLS GD GOT reference.  */
  HOWTO (R_RISCV_TLS_GD_HI20,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLS_GD_HI20",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_UTYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* High 20 bits of 32-bit PC-relative reference.  */
  HOWTO (R_RISCV_PCREL_HI20,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_PCREL_HI20",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_UTYPE_IMM (-1U),	/* dst_mask */
	 true),				/* pcrel_offset */

  /* Low 12 bits of a 32-bit PC-relative load or add.  */
  HOWTO (R_RISCV_PCREL_LO12_I,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_PCREL_LO12_I",	/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_ITYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* Low 12 bits of a 32-bit PC-relative store.  */
  HOWTO (R_RISCV_PCREL_LO12_S,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_PCREL_LO12_S",	/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_STYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* High 20 bits of 32-bit absolute address.  */
  HOWTO (R_RISCV_HI20,			/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_HI20",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_UTYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* High 12 bits of 32-bit load or add.  */
  HOWTO (R_RISCV_LO12_I,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_LO12_I",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_ITYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* High 12 bits of 32-bit store.  */
  HOWTO (R_RISCV_LO12_S,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_LO12_S",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_STYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* High 20 bits of TLS LE thread pointer offset.  */
  HOWTO (R_RISCV_TPREL_HI20,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TPREL_HI20",		/* name */
	 true,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_UTYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* Low 12 bits of TLS LE thread pointer offset for loads and adds.  */
  HOWTO (R_RISCV_TPREL_LO12_I,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TPREL_LO12_I",	/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_ITYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* Low 12 bits of TLS LE thread pointer offset for stores.  */
  HOWTO (R_RISCV_TPREL_LO12_S,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TPREL_LO12_S",	/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_STYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* TLS LE thread pointer usage.  May be relaxed.  */
  HOWTO (R_RISCV_TPREL_ADD,		/* type */
	 0,				/* rightshift */
	 0,				/* size */
	 0,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TPREL_ADD",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0,				/* dst_mask */
	 false),			/* pcrel_offset */

  /* 8-bit in-place addition, for local label subtraction.  */
  HOWTO (R_RISCV_ADD8,			/* type */
	 0,				/* rightshift */
	 1,				/* size */
	 8,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 riscv_elf_add_sub_reloc,	/* special_function */
	 "R_RISCV_ADD8",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xff,				/* dst_mask */
	 false),			/* pcrel_offset */

  /* 16-bit in-place addition, for local label subtraction.  */
  HOWTO (R_RISCV_ADD16,			/* type */
	 0,				/* rightshift */
	 2,				/* size */
	 16,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 riscv_elf_add_sub_reloc,	/* special_function */
	 "R_RISCV_ADD16",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  /* 32-bit in-place addition, for local label subtraction.  */
  HOWTO (R_RISCV_ADD32,			/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 riscv_elf_add_sub_reloc,	/* special_function */
	 "R_RISCV_ADD32",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffffffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  /* 64-bit in-place addition, for local label subtraction.  */
  HOWTO (R_RISCV_ADD64,			/* type */
	 0,				/* rightshift */
	 8,				/* size */
	 64,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 riscv_elf_add_sub_reloc,	/* special_function */
	 "R_RISCV_ADD64",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 MINUS_ONE,			/* dst_mask */
	 false),			/* pcrel_offset */

  /* 8-bit in-place addition, for local label subtraction.  */
  HOWTO (R_RISCV_SUB8,			/* type */
	 0,				/* rightshift */
	 1,				/* size */
	 8,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 riscv_elf_add_sub_reloc,	/* special_function */
	 "R_RISCV_SUB8",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xff,				/* dst_mask */
	 false),			/* pcrel_offset */

  /* 16-bit in-place addition, for local label subtraction.  */
  HOWTO (R_RISCV_SUB16,			/* type */
	 0,				/* rightshift */
	 2,				/* size */
	 16,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 riscv_elf_add_sub_reloc,	/* special_function */
	 "R_RISCV_SUB16",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  /* 32-bit in-place addition, for local label subtraction.  */
  HOWTO (R_RISCV_SUB32,			/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 riscv_elf_add_sub_reloc,	/* special_function */
	 "R_RISCV_SUB32",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffffffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  /* 64-bit in-place addition, for local label subtraction.  */
  HOWTO (R_RISCV_SUB64,			/* type */
	 0,				/* rightshift */
	 8,				/* size */
	 64,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 riscv_elf_add_sub_reloc,	/* special_function */
	 "R_RISCV_SUB64",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 MINUS_ONE,			/* dst_mask */
	 false),			/* pcrel_offset */

  EMPTY_HOWTO (41),
  EMPTY_HOWTO (42),

  /* Indicates an alignment statement.  The addend field encodes how many
     bytes of NOPs follow the statement.  The desired alignment is the
     addend rounded up to the next power of two.  */
  HOWTO (R_RISCV_ALIGN,			/* type */
	 0,				/* rightshift */
	 0,				/* size */
	 0,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_ALIGN",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0,				/* dst_mask */
	 false),			/* pcrel_offset */

  /* 8-bit PC-relative branch offset.  */
  HOWTO (R_RISCV_RVC_BRANCH,		/* type */
	 0,				/* rightshift */
	 2,				/* size */
	 16,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_RVC_BRANCH",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_CBTYPE_IMM (-1U),	/* dst_mask */
	 true),				/* pcrel_offset */

  /* 11-bit PC-relative jump offset.  */
  HOWTO (R_RISCV_RVC_JUMP,		/* type */
	 0,				/* rightshift */
	 2,				/* size */
	 16,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_RVC_JUMP",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_CJTYPE_IMM (-1U),	/* dst_mask */
	 true),				/* pcrel_offset */

  EMPTY_HOWTO (46),
  EMPTY_HOWTO (47),
  EMPTY_HOWTO (48),
  EMPTY_HOWTO (49),
  EMPTY_HOWTO (50),

  /* The paired relocation may be relaxed.  */
  HOWTO (R_RISCV_RELAX,			/* type */
	 0,				/* rightshift */
	 0,				/* size */
	 0,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_RELAX",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0,				/* dst_mask */
	 false),			/* pcrel_offset */

  /* 6-bit in-place addition, for local label subtraction.  */
  HOWTO (R_RISCV_SUB6,			/* type */
	 0,				/* rightshift */
	 1,				/* size */
	 8,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 riscv_elf_add_sub_reloc,	/* special_function */
	 "R_RISCV_SUB6",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0x3f,				/* dst_mask */
	 false),			/* pcrel_offset */

  /* 6-bit in-place setting, for local label subtraction.  */
  HOWTO (R_RISCV_SET6,			/* type */
	 0,				/* rightshift */
	 1,				/* size */
	 8,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_SET6",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0x3f,				/* dst_mask */
	 false),			/* pcrel_offset */

  /* 8-bit in-place setting, for local label subtraction.  */
  HOWTO (R_RISCV_SET8,			/* type */
	 0,				/* rightshift */
	 1,				/* size */
	 8,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_SET8",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xff,				/* dst_mask */
	 false),			/* pcrel_offset */

  /* 16-bit in-place setting, for local label subtraction.  */
  HOWTO (R_RISCV_SET16,			/* type */
	 0,				/* rightshift */
	 2,				/* size */
	 16,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_SET16",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  /* 32-bit in-place setting, for local label subtraction.  */
  HOWTO (R_RISCV_SET32,			/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_SET32",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffffffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  /* 32-bit PC relative.  */
  HOWTO (R_RISCV_32_PCREL,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_32_PCREL",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffffffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  /* Relocation against a local ifunc symbol in a shared object.  */
  HOWTO (R_RISCV_IRELATIVE,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_IRELATIVE",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0xffffffff,			/* dst_mask */
	 false),			/* pcrel_offset */

  /* Reserved for R_RISCV_PLT32.  */
  EMPTY_HOWTO (59),

  /* N-bit in-place setting, for unsigned-leb128 local label subtraction.  */
  HOWTO (R_RISCV_SET_ULEB128,		/* type */
	 0,				/* rightshift */
	 0,				/* size */
	 0,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 riscv_elf_ignore_reloc,	/* special_function */
	 "R_RISCV_SET_ULEB128",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0,				/* dst_mask */
	 false),			/* pcrel_offset */

  /* N-bit in-place addition, for unsigned-leb128 local label subtraction.  */
  HOWTO (R_RISCV_SUB_ULEB128,		/* type */
	 0,				/* rightshift */
	 0,				/* size */
	 0,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 riscv_elf_ignore_reloc,	/* special_function */
	 "R_RISCV_SUB_ULEB128",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 0,				/* dst_mask */
	 false),			/* pcrel_offset */

  HOWTO (R_RISCV_TLSDESC_HI20,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 true,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLSDESC_HI20",	/* name */
	 true,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_UTYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  HOWTO (R_RISCV_TLSDESC_LOAD_LO12,	/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLSDESC_LOAD_LO12",	/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_ITYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  HOWTO (R_RISCV_TLSDESC_ADD_LO12,	/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLSDESC_ADD_LO12",	/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_ITYPE_IMM (-1U),	/* dst_mask */
	 false), 			/* pcrel_offset */

  HOWTO (R_RISCV_TLSDESC_CALL,		/* type */
	 0,				/* rightshift */
	 0,				/* size */
	 0,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TLSDESC_CALL",	/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_ITYPE_IMM (-1U),	/* dst_mask */
	 false) 			/* pcrel_offset */
};

static reloc_howto_type howto_table_internal[] =
{
  /* R_RISCV_DELETE.  */
  EMPTY_HOWTO (0),

  /* High 6 bits of 18-bit absolute address.  */
  HOWTO (R_RISCV_RVC_LUI,		/* type */
	 0,				/* rightshift */
	 2,				/* size */
	 16,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_RVC_LUI",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_CITYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* GP-relative load.  */
  HOWTO (R_RISCV_GPREL_I,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_GPREL_I",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_ITYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* GP-relative store.  */
  HOWTO (R_RISCV_GPREL_S,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_dont,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_GPREL_S",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_STYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* TP-relative TLS LE load.  */
  HOWTO (R_RISCV_TPREL_I,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TPREL_I",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_ITYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */

  /* TP-relative TLS LE store.  */
  HOWTO (R_RISCV_TPREL_S,		/* type */
	 0,				/* rightshift */
	 4,				/* size */
	 32,				/* bitsize */
	 false,				/* pc_relative */
	 0,				/* bitpos */
	 complain_overflow_signed,	/* complain_on_overflow */
	 bfd_elf_generic_reloc,		/* special_function */
	 "R_RISCV_TPREL_S",		/* name */
	 false,				/* partial_inplace */
	 0,				/* src_mask */
	 ENCODE_STYPE_IMM (-1U),	/* dst_mask */
	 false),			/* pcrel_offset */
};

/* A mapping from BFD reloc types to RISC-V ELF reloc types.  */
struct elf_reloc_map
{
  bfd_reloc_code_real_type bfd_val;
  enum elf_riscv_reloc_type elf_val;
};

static const struct elf_reloc_map riscv_reloc_map[] =
{
  { BFD_RELOC_NONE, R_RISCV_NONE },
  { BFD_RELOC_32, R_RISCV_32 },
  { BFD_RELOC_64, R_RISCV_64 },
  { BFD_RELOC_RISCV_ADD8, R_RISCV_ADD8 },
  { BFD_RELOC_RISCV_ADD16, R_RISCV_ADD16 },
  { BFD_RELOC_RISCV_ADD32, R_RISCV_ADD32 },
  { BFD_RELOC_RISCV_ADD64, R_RISCV_ADD64 },
  { BFD_RELOC_RISCV_SUB8, R_RISCV_SUB8 },
  { BFD_RELOC_RISCV_SUB16, R_RISCV_SUB16 },
  { BFD_RELOC_RISCV_SUB32, R_RISCV_SUB32 },
  { BFD_RELOC_RISCV_SUB64, R_RISCV_SUB64 },
  { BFD_RELOC_CTOR, R_RISCV_64 },
  { BFD_RELOC_12_PCREL, R_RISCV_BRANCH },
  { BFD_RELOC_RISCV_HI20, R_RISCV_HI20 },
  { BFD_RELOC_RISCV_LO12_I, R_RISCV_LO12_I },
  { BFD_RELOC_RISCV_LO12_S, R_RISCV_LO12_S },
  { BFD_RELOC_RISCV_PCREL_LO12_I, R_RISCV_PCREL_LO12_I },
  { BFD_RELOC_RISCV_PCREL_LO12_S, R_RISCV_PCREL_LO12_S },
  { BFD_RELOC_RISCV_CALL, R_RISCV_CALL },
  { BFD_RELOC_RISCV_CALL_PLT, R_RISCV_CALL_PLT },
  { BFD_RELOC_RISCV_PCREL_HI20, R_RISCV_PCREL_HI20 },
  { BFD_RELOC_RISCV_JMP, R_RISCV_JAL },
  { BFD_RELOC_RISCV_GOT_HI20, R_RISCV_GOT_HI20 },
  { BFD_RELOC_RISCV_TLS_DTPMOD32, R_RISCV_TLS_DTPMOD32 },
  { BFD_RELOC_RISCV_TLS_DTPREL32, R_RISCV_TLS_DTPREL32 },
  { BFD_RELOC_RISCV_TLS_DTPMOD64, R_RISCV_TLS_DTPMOD64 },
  { BFD_RELOC_RISCV_TLS_DTPREL64, R_RISCV_TLS_DTPREL64 },
  { BFD_RELOC_RISCV_TLS_TPREL32, R_RISCV_TLS_TPREL32 },
  { BFD_RELOC_RISCV_TLS_TPREL64, R_RISCV_TLS_TPREL64 },
  { BFD_RELOC_RISCV_TPREL_HI20, R_RISCV_TPREL_HI20 },
  { BFD_RELOC_RISCV_TPREL_ADD, R_RISCV_TPREL_ADD },
  { BFD_RELOC_RISCV_TPREL_LO12_S, R_RISCV_TPREL_LO12_S },
  { BFD_RELOC_RISCV_TPREL_LO12_I, R_RISCV_TPREL_LO12_I },
  { BFD_RELOC_RISCV_TLS_GOT_HI20, R_RISCV_TLS_GOT_HI20 },
  { BFD_RELOC_RISCV_TLS_GD_HI20, R_RISCV_TLS_GD_HI20 },
  { BFD_RELOC_RISCV_TLSDESC_HI20, R_RISCV_TLSDESC_HI20 },
  { BFD_RELOC_RISCV_TLSDESC_LOAD_LO12, R_RISCV_TLSDESC_LOAD_LO12 },
  { BFD_RELOC_RISCV_TLSDESC_ADD_LO12, R_RISCV_TLSDESC_ADD_LO12 },
  { BFD_RELOC_RISCV_TLSDESC_CALL, R_RISCV_TLSDESC_CALL },
  { BFD_RELOC_RISCV_ALIGN, R_RISCV_ALIGN },
  { BFD_RELOC_RISCV_RVC_BRANCH, R_RISCV_RVC_BRANCH },
  { BFD_RELOC_RISCV_RVC_JUMP, R_RISCV_RVC_JUMP },
  { BFD_RELOC_RISCV_RELAX, R_RISCV_RELAX },
  { BFD_RELOC_RISCV_SUB6, R_RISCV_SUB6 },
  { BFD_RELOC_RISCV_SET6, R_RISCV_SET6 },
  { BFD_RELOC_RISCV_SET8, R_RISCV_SET8 },
  { BFD_RELOC_RISCV_SET16, R_RISCV_SET16 },
  { BFD_RELOC_RISCV_SET32, R_RISCV_SET32 },
  { BFD_RELOC_RISCV_32_PCREL, R_RISCV_32_PCREL },
  { BFD_RELOC_RISCV_SET_ULEB128, R_RISCV_SET_ULEB128 },
  { BFD_RELOC_RISCV_SUB_ULEB128, R_RISCV_SUB_ULEB128 },
};

/* Given a BFD reloc type, return a howto structure.  */

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

  for (i = 0; i < ARRAY_SIZE (riscv_reloc_map); i++)
    if (riscv_reloc_map[i].bfd_val == code)
      return &howto_table[(int) riscv_reloc_map[i].elf_val];

  bfd_set_error (bfd_error_bad_value);
  return NULL;
}

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

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

  return NULL;
}

reloc_howto_type *
riscv_elf_rtype_to_howto (bfd *abfd, unsigned int r_type)
{
  if (r_type < ARRAY_SIZE (howto_table))
    return &howto_table[r_type];
  else if (r_type < R_RISCV_max + ARRAY_SIZE (howto_table_internal))
    return &howto_table_internal[r_type - R_RISCV_max];
  else
    {
      (*_bfd_error_handler) (_("%pB: unsupported relocation type %#x"),
			     abfd, r_type);
      bfd_set_error (bfd_error_bad_value);
      return NULL;
    }
}

/* Special_function of RISCV_ADD and RISCV_SUB relocations.  */

static bfd_reloc_status_type
riscv_elf_add_sub_reloc (bfd *abfd,
			 arelent *reloc_entry,
			 asymbol *symbol,
			 void *data,
			 asection *input_section,
			 bfd *output_bfd,
			 char **error_message ATTRIBUTE_UNUSED)
{
  reloc_howto_type *howto = reloc_entry->howto;
  bfd_vma relocation;

  if (output_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)
    return bfd_reloc_continue;

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

  bfd_size_type octets = reloc_entry->address
    * bfd_octets_per_byte (abfd, input_section);
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
				  input_section, octets))
    return bfd_reloc_outofrange;

  bfd_vma old_value = bfd_get (howto->bitsize, abfd,
			       data + reloc_entry->address);

  switch (howto->type)
    {
    case R_RISCV_ADD8:
    case R_RISCV_ADD16:
    case R_RISCV_ADD32:
    case R_RISCV_ADD64:
      relocation = old_value + relocation;
      break;
    case R_RISCV_SUB6:
      relocation = (old_value & ~howto->dst_mask)
		   | (((old_value & howto->dst_mask) - relocation)
		      & howto->dst_mask);
      break;
    case R_RISCV_SUB8:
    case R_RISCV_SUB16:
    case R_RISCV_SUB32:
    case R_RISCV_SUB64:
      relocation = old_value - relocation;
      break;
    }
  bfd_put (howto->bitsize, abfd, relocation, data + reloc_entry->address);

  return bfd_reloc_ok;
}

/* Special handler for relocations which don't have to be relocated.
   This function just simply return bfd_reloc_ok.  */

static bfd_reloc_status_type
riscv_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;
}

/* Always add the IMPLICIT for the SUBSET.  */

static bool
check_implicit_always (const char *implicit ATTRIBUTE_UNUSED,
		       riscv_subset_t *subset ATTRIBUTE_UNUSED)
{
  return true;
}

/* Add the IMPLICIT only when the version of SUBSET less than 2.1.  */

static bool
check_implicit_for_i (const char *implicit ATTRIBUTE_UNUSED,
		      riscv_subset_t *subset)
{
  return (subset->major_version < 2
	  || (subset->major_version == 2
	      && subset->minor_version < 1));
}

/* Record all implicit information for the subsets.  */
struct riscv_implicit_subset
{
  const char *subset_name;
  const char *implicit_name;
  /* A function to determine if we need to add the implicit subset.  */
  bool (*check_func) (const char *, riscv_subset_t *);
};
static struct riscv_implicit_subset riscv_implicit_subsets[] =
{
  {"e", "i",		check_implicit_always},
  {"i", "zicsr",	check_implicit_for_i},
  {"i", "zifencei",	check_implicit_for_i},
  {"g", "i",		check_implicit_always},
  {"g", "m",		check_implicit_always},
  {"g", "a",		check_implicit_always},
  {"g", "f",		check_implicit_always},
  {"g", "d",		check_implicit_always},
  {"g", "zicsr",	check_implicit_always},
  {"g", "zifencei",	check_implicit_always},
  {"m", "zmmul",	check_implicit_always},
  {"h", "zicsr",	check_implicit_always},
  {"q", "d",		check_implicit_always},
  {"v", "d",		check_implicit_always},
  {"v", "zve64d",	check_implicit_always},
  {"v", "zvl128b",	check_implicit_always},
  {"zabha", "a",	check_implicit_always},
  {"zvfbfmin", "zve32f",	check_implicit_always},
  {"zvfbfwma", "zve32f",	check_implicit_always},
  {"zvfbfwma", "zfbfmin",	check_implicit_always},
  {"zvfh", "zvfhmin",	check_implicit_always},
  {"zvfh", "zfhmin",	check_implicit_always},
  {"zvfhmin", "zve32f",	check_implicit_always},
  {"zve64d", "d",	check_implicit_always},
  {"zve64d", "zve64f",	check_implicit_always},
  {"zve64f", "zve32f",	check_implicit_always},
  {"zve64f", "zve64x",	check_implicit_always},
  {"zve64f", "zvl64b",	check_implicit_always},
  {"zve32f", "f",	check_implicit_always},
  {"zve32f", "zvl32b",	check_implicit_always},
  {"zve32f", "zve32x",	check_implicit_always},
  {"zve64x", "zve32x",	check_implicit_always},
  {"zve64x", "zvl64b",	check_implicit_always},
  {"zve32x", "zvl32b",	check_implicit_always},
  {"zve32x", "zicsr",	check_implicit_always},
  {"zvl65536b", "zvl32768b",	check_implicit_always},
  {"zvl32768b", "zvl16384b",	check_implicit_always},
  {"zvl16384b", "zvl8192b",	check_implicit_always},
  {"zvl8192b", "zvl4096b",	check_implicit_always},
  {"zvl4096b", "zvl2048b",	check_implicit_always},
  {"zvl2048b", "zvl1024b",	check_implicit_always},
  {"zvl1024b", "zvl512b",	check_implicit_always},
  {"zvl512b", "zvl256b",	check_implicit_always},
  {"zvl256b", "zvl128b",	check_implicit_always},
  {"zvl128b", "zvl64b",		check_implicit_always},
  {"zvl64b", "zvl32b",		check_implicit_always},
  {"zicntr", "zicsr",		check_implicit_always},
  {"zihpm", "zicsr",		check_implicit_always},
  {"zcd", "d",		check_implicit_always},
  {"zcf", "f",		check_implicit_always},
  {"zfbfmin", "zfhmin",	check_implicit_always},
  {"zfa", "f",		check_implicit_always},
  {"d", "f",		check_implicit_always},
  {"zfh", "zfhmin",	check_implicit_always},
  {"zfhmin", "f",	check_implicit_always},
  {"f", "zicsr",	check_implicit_always},
  {"zqinx", "zdinx",	check_implicit_always},
  {"zdinx", "zfinx",	check_implicit_always},
  {"zhinx", "zhinxmin",	check_implicit_always},
  {"zhinxmin", "zfinx",	check_implicit_always},
  {"zfinx", "zicsr",	check_implicit_always},
  {"zk", "zkn",		check_implicit_always},
  {"zk", "zkr",		check_implicit_always},
  {"zk", "zkt",		check_implicit_always},
  {"zkn", "zbkb",	check_implicit_always},
  {"zkn", "zbkc",	check_implicit_always},
  {"zkn", "zbkx",	check_implicit_always},
  {"zkn", "zkne",	check_implicit_always},
  {"zkn", "zknd",	check_implicit_always},
  {"zkn", "zknh",	check_implicit_always},
  {"zks", "zbkb",	check_implicit_always},
  {"zks", "zbkc",	check_implicit_always},
  {"zks", "zbkx",	check_implicit_always},
  {"zks", "zksed",	check_implicit_always},
  {"zks", "zksh",	check_implicit_always},
  {"zvbb", "zvkb",	check_implicit_always},
  {"zvkn", "zvkned",	check_implicit_always},
  {"zvkn", "zvknha",	check_implicit_always},
  {"zvkn", "zvknhb",	check_implicit_always},
  {"zvkn", "zvkb",	check_implicit_always},
  {"zvkn", "zvkt",	check_implicit_always},
  {"zvkng", "zvkn",	check_implicit_always},
  {"zvkng", "zvkg",	check_implicit_always},
  {"zvknc", "zvkn",	check_implicit_always},
  {"zvknc", "zvbc",	check_implicit_always},
  {"zvks", "zvksed",	check_implicit_always},
  {"zvks", "zvksh",	check_implicit_always},
  {"zvks", "zvkb",	check_implicit_always},
  {"zvks", "zvkt",	check_implicit_always},
  {"zvksg", "zvks",	check_implicit_always},
  {"zvksg", "zvkg",	check_implicit_always},
  {"zvksc", "zvks",	check_implicit_always},
  {"zvksc", "zvbc",	check_implicit_always},
  {"zcf", "zca",	check_implicit_always},
  {"zcd", "zca",	check_implicit_always},
  {"zcb", "zca",	check_implicit_always},
  {"zcmp", "zca",	check_implicit_always},
  {"smaia", "ssaia",		check_implicit_always},
  {"smcntrpmf", "zicsr",	check_implicit_always},
  {"smstateen", "ssstateen",	check_implicit_always},
  {"smepmp", "zicsr",		check_implicit_always},
  {"ssaia", "zicsr",		check_implicit_always},
  {"sscofpmf", "zicsr",		check_implicit_always},
  {"ssstateen", "zicsr",	check_implicit_always},
  {"sstc", "zicsr",		check_implicit_always},
  {"svadu", "zicsr",		check_implicit_always},
  {"b", "zba",		check_implicit_always},
  {"b", "zbb",		check_implicit_always},
  {"b", "zbs",		check_implicit_always},
  {"a", "zaamo",	check_implicit_always},
  {"a", "zalrsc",	check_implicit_always},

  {"xsfvcp", "zve32x",  check_implicit_always},
  {NULL, NULL, NULL}
};

/* For default_enable field, decide if the extension should
   be enbaled by default.  */

#define EXT_DEFAULT   0x1

/* List all extensions that binutils should know about.  */

struct riscv_supported_ext
{
  const char *name;
  enum riscv_spec_class isa_spec_class;
  int major_version;
  int minor_version;
  unsigned long default_enable;
};

/* The standard extensions must be added in canonical order.  */

static struct riscv_supported_ext riscv_supported_std_ext[] =
{
  {"e",		ISA_SPEC_CLASS_20191213,	1, 9, 0 },
  {"e",		ISA_SPEC_CLASS_20190608,	1, 9, 0 },
  {"e",		ISA_SPEC_CLASS_2P2,		1, 9, 0 },
  {"i",		ISA_SPEC_CLASS_20191213,	2, 1, 0 },
  {"i",		ISA_SPEC_CLASS_20190608,	2, 1, 0 },
  {"i",		ISA_SPEC_CLASS_2P2,		2, 0, 0 },
  /* The g is a special case which we don't want to output it,
     but still need it when adding implicit extensions.  */
  {"g",		ISA_SPEC_CLASS_NONE, RISCV_UNKNOWN_VERSION, RISCV_UNKNOWN_VERSION, EXT_DEFAULT },
  {"m",		ISA_SPEC_CLASS_20191213,	2, 0, 0 },
  {"m",		ISA_SPEC_CLASS_20190608,	2, 0, 0 },
  {"m",		ISA_SPEC_CLASS_2P2,		2, 0, 0 },
  {"a",		ISA_SPEC_CLASS_20191213,	2, 1, 0 },
  {"a",		ISA_SPEC_CLASS_20190608,	2, 0, 0 },
  {"a",		ISA_SPEC_CLASS_2P2,		2, 0, 0 },
  {"f",		ISA_SPEC_CLASS_20191213,	2, 2, 0 },
  {"f",		ISA_SPEC_CLASS_20190608,	2, 2, 0 },
  {"f",		ISA_SPEC_CLASS_2P2,		2, 0, 0 },
  {"d",		ISA_SPEC_CLASS_20191213,	2, 2, 0 },
  {"d",		ISA_SPEC_CLASS_20190608,	2, 2, 0 },
  {"d",		ISA_SPEC_CLASS_2P2,		2, 0, 0 },
  {"q",		ISA_SPEC_CLASS_20191213,	2, 2, 0 },
  {"q",		ISA_SPEC_CLASS_20190608,	2, 2, 0 },
  {"q",		ISA_SPEC_CLASS_2P2,		2, 0, 0 },
  {"c",		ISA_SPEC_CLASS_20191213,	2, 0, 0 },
  {"c",		ISA_SPEC_CLASS_20190608,	2, 0, 0 },
  {"c",		ISA_SPEC_CLASS_2P2,		2, 0, 0 },
  {"b",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"v",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"h",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {NULL, 0, 0, 0, 0}
};

static struct riscv_supported_ext riscv_supported_std_z_ext[] =
{
  {"zicbom",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zicbop",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zicboz",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zicond",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zicntr",		ISA_SPEC_CLASS_DRAFT,		2, 0,  0 },
  {"zicsr",		ISA_SPEC_CLASS_20191213,	2, 0,  0 },
  {"zicsr",		ISA_SPEC_CLASS_20190608,	2, 0,  0 },
  {"zifencei",		ISA_SPEC_CLASS_20191213,	2, 0,  0 },
  {"zifencei",		ISA_SPEC_CLASS_20190608,	2, 0,  0 },
  {"zihintntl",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zihintpause",	ISA_SPEC_CLASS_DRAFT,		2, 0,  0 },
  {"zihpm",		ISA_SPEC_CLASS_DRAFT,		2, 0,  0 },
  {"zmmul",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zaamo",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zabha",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zalrsc",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zawrs",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zfbfmin",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zfa",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zfh",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zfhmin",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zfinx",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zdinx",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zqinx",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zhinx",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zhinxmin",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zbb",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zba",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zbc",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zbs",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zbkb",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zbkc",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zbkx",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zk",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zkn",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zknd",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zkne",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zknh",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zkr",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zks",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zksed",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zksh",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zkt",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zve32x",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zve32f",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zve64x",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zve64f",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zve64d",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvbb",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvbc",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvfbfmin",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvfbfwma",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvfh",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvfhmin",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvkb",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvkg",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvkn",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvkng",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvknc",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvkned",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvknha",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvknhb",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvksed",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvksh",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvks",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvksg",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvksc",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvkt",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl32b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl64b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl128b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl256b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl512b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl1024b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl2048b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl4096b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl8192b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl16384b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl32768b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zvl65536b",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"ztso",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zca",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zcb",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zcf",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zcd",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {"zcmp",		ISA_SPEC_CLASS_DRAFT,		1, 0,  0 },
  {NULL, 0, 0, 0, 0}
};

static struct riscv_supported_ext riscv_supported_std_s_ext[] =
{
  {"smaia",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"smcntrpmf",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"smepmp",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"smstateen",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"ssaia",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"sscofpmf",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"ssstateen",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"sstc",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"svadu",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"svinval",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"svnapot",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {"svpbmt",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
  {NULL, 0, 0, 0, 0}
};

static struct riscv_supported_ext riscv_supported_std_zxm_ext[] =
{
  {NULL, 0, 0, 0, 0}
};

static struct riscv_supported_ext riscv_supported_vendor_x_ext[] =
{
  {"xcvmac",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xcvalu",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xcvelw",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xcvbi",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xcvmem",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadba",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadbb",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadbs",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadcmo",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadcondmov",	ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadfmemidx",	ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadfmv",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadint",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadmac",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadmemidx",	ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadmempair",	ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadsync",	ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadvector",	ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xtheadzvamo",	ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xventanacondops",	ISA_SPEC_CLASS_DRAFT,	1, 0, 0 },
  {"xsfvcp",		ISA_SPEC_CLASS_DRAFT,	1, 0, 0},
  {NULL, 0, 0, 0, 0}
};

const struct riscv_supported_ext *riscv_all_supported_ext[] =
{
  riscv_supported_std_ext,
  riscv_supported_std_z_ext,
  riscv_supported_std_s_ext,
  riscv_supported_std_zxm_ext,
  riscv_supported_vendor_x_ext,
  NULL
};

/* ISA extension prefixed name class.  Must define them in parsing order.  */
enum riscv_prefix_ext_class
{
  RV_ISA_CLASS_Z = 1,
  RV_ISA_CLASS_S,
  RV_ISA_CLASS_ZXM,
  RV_ISA_CLASS_X,
  RV_ISA_CLASS_SINGLE
};

/* Record the strings of the prefixed extensions, and their corresponding
   classes.  The more letters of the prefix string, the more forward it must
   be defined.  Otherwise, the riscv_get_prefix_class will map it to the
   wrong classes.  */
struct riscv_parse_prefix_config
{
  /* Class of the extension. */
  enum riscv_prefix_ext_class class;

  /* Prefix string for error printing and internal parser usage.  */
  const char *prefix;
};
static const struct riscv_parse_prefix_config parse_config[] =
{
  {RV_ISA_CLASS_ZXM, "zxm"},
  {RV_ISA_CLASS_Z, "z"},
  {RV_ISA_CLASS_S, "s"},
  {RV_ISA_CLASS_X, "x"},
  {RV_ISA_CLASS_SINGLE, NULL}
};

/* Get the prefixed name class for the extensions, the class also
   means the order of the prefixed extensions.  */

static enum riscv_prefix_ext_class
riscv_get_prefix_class (const char *arch)
{
  int i = 0;
  while (parse_config[i].class != RV_ISA_CLASS_SINGLE)
    {
      if (strncmp (arch, parse_config[i].prefix,
		   strlen (parse_config[i].prefix)) == 0)
	return parse_config[i].class;
      i++;
    }
  return RV_ISA_CLASS_SINGLE;
}

/* Check KNOWN_EXTS to see if the EXT is supported.  */

static bool
riscv_known_prefixed_ext (const char *ext,
			  struct riscv_supported_ext *known_exts)
{
  size_t i;
  for (i = 0; known_exts[i].name != NULL; ++i)
    if (strcmp (ext, known_exts[i].name) == 0)
      return true;
  return false;
}

/* Check whether the prefixed extension is recognized or not.  Return
   true if recognized, otehrwise return false.  */

static bool
riscv_recognized_prefixed_ext (const char *ext)
{
  enum riscv_prefix_ext_class class = riscv_get_prefix_class (ext);
  switch (class)
  {
  case RV_ISA_CLASS_Z:
    return riscv_known_prefixed_ext (ext, riscv_supported_std_z_ext);
  case RV_ISA_CLASS_ZXM:
    return riscv_known_prefixed_ext (ext, riscv_supported_std_zxm_ext);
  case RV_ISA_CLASS_S:
    return riscv_known_prefixed_ext (ext, riscv_supported_std_s_ext);
  case RV_ISA_CLASS_X:
    /* Only the single x is unrecognized.  */
    if (strcmp (ext, "x") != 0)
      return true;
  default:
    break;
  }
  return false;
}

/* Canonical order for single letter extensions.  */
static const char riscv_ext_canonical_order[] = "eigmafdqlcbkjtpvnh";

/* Array is used to compare the orders of standard extensions quickly.  */
static int riscv_ext_order[26] = {0};

/* Init the riscv_ext_order array.  */

static void
riscv_init_ext_order (void)
{
  static bool inited = false;
  if (inited)
    return;

  /* The orders of all standard extensions are positive.  */
  int order = 1;

  for (const char *ext = &riscv_ext_canonical_order[0]; *ext; ++ext)
    riscv_ext_order[(*ext - 'a')] = order++;

  /* Some of the prefixed keyword are not single letter, so we set
     their prefixed orders in the riscv_compare_subsets directly,
     not through the riscv_ext_order.  */

  inited = true;
}

/* Similar to the strcmp.  It returns an integer less than, equal to,
   or greater than zero if `subset2` is found, respectively, to be less
   than, to match, or be greater than `subset1`.

   The order values,
   Zero: Preserved keywords.
   Positive number: Standard extensions.
   Negative number: Prefixed keywords.  */

int
riscv_compare_subsets (const char *subset1, const char *subset2)
{
  int order1 = riscv_ext_order[(*subset1 - 'a')];
  int order2 = riscv_ext_order[(*subset2 - 'a')];

  /* Compare the standard extension first.  */
  if (order1 > 0 && order2 > 0)
    return order1 - order2;

  /* Set the prefixed orders to negative numbers.  */
  enum riscv_prefix_ext_class class1 = riscv_get_prefix_class (subset1);
  enum riscv_prefix_ext_class class2 = riscv_get_prefix_class (subset2);

  if (class1 != RV_ISA_CLASS_SINGLE)
    order1 = - (int) class1;
  if (class2 != RV_ISA_CLASS_SINGLE)
    order2 = - (int) class2;

  if (order1 == order2)
    {
      /* Compare the standard addition z extensions.  */
      if (class1 == RV_ISA_CLASS_Z)
	{
	  order1 = riscv_ext_order[(*++subset1 - 'a')];
	  order2 = riscv_ext_order[(*++subset2 - 'a')];
	  if (order1 != order2)
	    return order1 - order2;
	}
      return strcasecmp (++subset1, ++subset2);
    }

  return order2 - order1;
}

/* Find subset in the list.  Return TRUE and set `current` to the subset
   if it is found.  Otherwise, return FALSE and set `current` to the place
   where we should insert the subset.  However, return FALSE with the NULL
   `current` means we should insert the subset at the head of subset list,
   if needed.  */

bool
riscv_lookup_subset (const riscv_subset_list_t *subset_list,
		     const char *subset,
		     riscv_subset_t **current)
{
  riscv_subset_t *s, *pre_s = NULL;

  /* If the subset is added in order, then just add it at the tail.  */
  if (subset_list->tail != NULL
      && riscv_compare_subsets (subset_list->tail->name, subset) < 0)
    {
      *current = subset_list->tail;
      return false;
    }

  for (s = subset_list->head;
       s != NULL;
       pre_s = s, s = s->next)
    {
      int cmp = riscv_compare_subsets (s->name, subset);
      if (cmp == 0)
	{
	  *current = s;
	  return true;
	}
      else if (cmp > 0)
	break;
    }
  *current = pre_s;

  return false;
}

/* Add the extension to the subset list.  Search the
   list first, and then find the right place to add.  */

void
riscv_add_subset (riscv_subset_list_t *subset_list,
		  const char *subset,
		  int major,
		  int minor)
{
  riscv_subset_t *current, *new;

  if (riscv_lookup_subset (subset_list, subset, &current))
    return;

  new = xmalloc (sizeof *new);
  new->name = xstrdup (subset);
  new->major_version = major;
  new->minor_version = minor;
  new->next = NULL;

  if (current != NULL)
    {
      new->next = current->next;
      current->next = new;
    }
  else
    {
      new->next = subset_list->head;
      subset_list->head = new;
    }

  if (new->next == NULL)
    subset_list->tail = new;
}

/* Get the default versions from the riscv_supported_*ext tables.  */

static void
riscv_get_default_ext_version (enum riscv_spec_class *default_isa_spec,
			       const char *name,
			       int *major_version,
			       int *minor_version)
{
  if (name == NULL
      || default_isa_spec == NULL
      || *default_isa_spec == ISA_SPEC_CLASS_NONE)
    return;

  struct riscv_supported_ext *table = NULL;
  enum riscv_prefix_ext_class class = riscv_get_prefix_class (name);
  switch (class)
    {
    case RV_ISA_CLASS_ZXM: table = riscv_supported_std_zxm_ext; break;
    case RV_ISA_CLASS_Z: table = riscv_supported_std_z_ext; break;
    case RV_ISA_CLASS_S: table = riscv_supported_std_s_ext; break;
    case RV_ISA_CLASS_X: table = riscv_supported_vendor_x_ext; break;
    default:
      table = riscv_supported_std_ext;
    }

  int i = 0;
  while (table != NULL && table[i].name != NULL)
    {
      if (strcmp (table[i].name, name) == 0
	  && (table[i].isa_spec_class == ISA_SPEC_CLASS_DRAFT
	      || table[i].isa_spec_class == *default_isa_spec))
	{
	  *major_version = table[i].major_version;
	  *minor_version = table[i].minor_version;
	  return;
	}
      i++;
    }
}

/* Find the default versions for the extension before adding them to
   the subset list, if their versions are RISCV_UNKNOWN_VERSION.
   Afterwards, report errors if we can not find their default versions.  */

static void
riscv_parse_add_subset (riscv_parse_subset_t *rps,
			const char *subset,
			int major,
			int minor,
			bool implicit)
{
  int major_version = major;
  int minor_version = minor;

  if (major_version == RISCV_UNKNOWN_VERSION
       || minor_version == RISCV_UNKNOWN_VERSION)
    riscv_get_default_ext_version (rps->isa_spec, subset,
				   &major_version, &minor_version);

  /* We don't care the versions of the implicit extensions.  */
  if (!implicit
      && (major_version == RISCV_UNKNOWN_VERSION
	  || minor_version == RISCV_UNKNOWN_VERSION))
    {
      if (subset[0] == 'x')
	rps->error_handler
	  (_("x ISA extension `%s' must be set with the versions"),
	   subset);
      /* Allow old ISA spec can recognize zicsr and zifencei.  */
      else if (strcmp (subset, "zicsr") != 0
	       && strcmp (subset, "zifencei") != 0)
	rps->error_handler
	  (_("cannot find default versions of the ISA extension `%s'"),
	   subset);
      return;
    }

  riscv_add_subset (rps->subset_list, subset,
		    major_version, minor_version);
}

/* Release subset list.  */

void
riscv_release_subset_list (riscv_subset_list_t *subset_list)
{
   while (subset_list->head != NULL)
    {
      riscv_subset_t *next = subset_list->head->next;
      free ((void *)subset_list->head->name);
      free (subset_list->head);
      subset_list->head = next;
    }

  subset_list->tail = NULL;

  if (subset_list->arch_str != NULL)
    {
      free ((void*) subset_list->arch_str);
      subset_list->arch_str = NULL;
    }
}

/* Parsing extension version.

   Return Value:
     Points to the end of version

   Arguments:
     `p`: Curent parsing position.
     `major_version`: Parsed major version.
     `minor_version`: Parsed minor version.  */

static const char *
riscv_parsing_subset_version (const char *p,
			      int *major_version,
			      int *minor_version)
{
  bool major_p = true;
  int version = 0;
  char np;

  *major_version = 0;
  *minor_version = 0;
  for (; *p; ++p)
    {
      if (*p == 'p')
	{
	  np = *(p + 1);

	  /* Might be beginning of `p` extension.  */
	  if (!ISDIGIT (np))
	    break;

	  *major_version = version;
	  major_p = false;
	  version = 0;
	}
      else if (ISDIGIT (*p))
	version = (version * 10) + (*p - '0');
      else
	break;
    }

  if (major_p)
    *major_version = version;
  else
    *minor_version = version;

  /* We can not find any version in string.  */
  if (*major_version == 0 && *minor_version == 0)
    {
      *major_version = RISCV_UNKNOWN_VERSION;
      *minor_version = RISCV_UNKNOWN_VERSION;
    }

  return p;
}

/* Parsing function for both standard and prefixed extensions.

   Return Value:
     Points to the end of extensions.

   Arguments:
     `rps`: Hooks and status for parsing extensions.
     `arch`: Full ISA string.
     `p`: Curent parsing position.  */

static const char *
riscv_parse_extensions (riscv_parse_subset_t *rps,
			const char *arch,
			const char *p)
{
  /* First letter must start with i, e or g.  */
  if (*p != 'e' && *p != 'i' && *p != 'g')
    {
      rps->error_handler
	(_("%s: first ISA extension must be `e', `i' or `g'"),
	 arch);
      return NULL;
    }

  while (*p != '\0')
    {
      if (*p == '_')
	{
	  p++;
	  continue;
	}

      char *subset = xstrdup (p);
      char *q = subset;	/* Start of version.  */
      const char *end_of_version;
      bool implicit = false;

      enum riscv_prefix_ext_class class = riscv_get_prefix_class (p);
      if (class == RV_ISA_CLASS_SINGLE)
	{
	  if (riscv_ext_order[(*subset - 'a')] == 0)
	    {
	      rps->error_handler
		(_("%s: unknown standard ISA extension or prefix class `%c'"),
		 arch, *subset);
	      free (subset);
	      return NULL;
	    }
	  q++;
	}
      else
	{
	  /* Extract the whole prefixed extension by '_'.  */
	  while (*++q != '\0' && *q != '_')
	    ;
	  /* Look forward to the first letter which is not <major>p<minor>.  */
	  bool find_any_version = false;
	  bool find_minor_version = false;
	  while (1)
	    {
	      q--;
	      if (ISDIGIT (*q))
		find_any_version = true;
	      else if (find_any_version
		       && !find_minor_version
		       && *q == 'p'
		       && ISDIGIT (*(q - 1)))
	      find_minor_version = true;
	      else
		break;
	    }
	  q++;

	  /* Check if the end of extension is 'p' or not.  If yes, then
	     the second letter from the end cannot be number.  */
	  if (*(q - 1) == 'p' && ISDIGIT (*(q - 2)))
	    {
	      *q = '\0';
	      rps->error_handler
		(_("%s: invalid prefixed ISA extension `%s' ends with <number>p"),
		 arch, subset);
	      free (subset);
	      return NULL;
	    }
	}

      int major_version = RISCV_UNKNOWN_VERSION;
      int minor_version = RISCV_UNKNOWN_VERSION;
      end_of_version =
	riscv_parsing_subset_version (q, &major_version, &minor_version);
      *q = '\0';
      if (end_of_version == NULL)
	{
	  free (subset);
	  return NULL;
	}

      /* Check if the prefixed extension name is well-formed.  */
      if (class != RV_ISA_CLASS_SINGLE
	  && rps->check_unknown_prefixed_ext
	  && !riscv_recognized_prefixed_ext (subset))
	{
	  rps->error_handler
	    (_("%s: unknown prefixed ISA extension `%s'"),
	     arch, subset);
	  free (subset);
	  return NULL;
	}

      /* Added g as an implicit extension.  */
      if (class == RV_ISA_CLASS_SINGLE
	  && strcmp (subset, "g") == 0)
	{
	  implicit = true;
	  major_version = RISCV_UNKNOWN_VERSION;
	  minor_version = RISCV_UNKNOWN_VERSION;
	}
      riscv_parse_add_subset (rps, subset,
			      major_version,
			      minor_version, implicit);
      p += end_of_version - subset;
      free (subset);

      if (class != RV_ISA_CLASS_SINGLE
	  && *p != '\0' && *p != '_')
	{
	  rps->error_handler
	    (_("%s: prefixed ISA extension must separate with _"),
	     arch);
	  return NULL;
	}
    }

  return p;
}

/* Add the implicit extensions.  */

static void
riscv_parse_add_implicit_subsets (riscv_parse_subset_t *rps)
{
  struct riscv_implicit_subset *t = riscv_implicit_subsets;
  bool finished = false;
  while (!finished)
    {
      finished = true;
      for (; t->subset_name; t++)
	{
	  riscv_subset_t *subset = NULL;
	  riscv_subset_t *implicit_subset = NULL;
	  if (riscv_lookup_subset (rps->subset_list, t->subset_name, &subset)
	      && !riscv_lookup_subset (rps->subset_list, t->implicit_name,
				       &implicit_subset)
	      && t->check_func (t->implicit_name, subset))
	    {
	      riscv_parse_add_subset (rps, t->implicit_name,
				      RISCV_UNKNOWN_VERSION,
				      RISCV_UNKNOWN_VERSION, true);

	      /* Restart the loop and pick up any new implications.  */
	      finished = false;
	      t = riscv_implicit_subsets;
	      break;
	    }
	}
    }
}

/* Check extensions conflicts.  */

static bool
riscv_parse_check_conflicts (riscv_parse_subset_t *rps)
{
  riscv_subset_t *subset = NULL;
  int xlen = *rps->xlen;
  bool no_conflict = true;

  if (riscv_subset_supports (rps, "e")
      && riscv_subset_supports (rps, "h"))
    {
      rps->error_handler
	(_("rv%de does not support the `h' extension"), xlen);
      no_conflict = false;
    }
  if (riscv_lookup_subset (rps->subset_list, "q", &subset)
      && (subset->major_version < 2 || (subset->major_version == 2
					&& subset->minor_version < 2))
      && xlen < 64)
    {
      rps->error_handler (_("rv%d does not support the `q' extension"), xlen);
      no_conflict = false;
    }
  if (riscv_subset_supports (rps, "zcmp")
      && riscv_subset_supports (rps, "zcd"))
    {
      rps->error_handler
	(_("zcmp' is incompatible with `d/zcd' extension"));
      no_conflict = false;
    }
  if (riscv_lookup_subset (rps->subset_list, "zcf", &subset)
      && xlen > 32)
    {
      rps->error_handler
	(_("rv%d does not support the `zcf' extension"), xlen);
      no_conflict = false;
    }
  if (riscv_lookup_subset (rps->subset_list, "zfinx", &subset)
      && riscv_lookup_subset (rps->subset_list, "f", &subset))
    {
      rps->error_handler
	(_("`zfinx' is conflict with the `f/d/q/zfh/zfhmin' extension"));
      no_conflict = false;
    }
  if (riscv_lookup_subset (rps->subset_list, "xtheadvector", &subset)
      && riscv_lookup_subset (rps->subset_list, "v", &subset))
    {
      rps->error_handler
	(_("`xtheadvector' is conflict with the `v' extension"));
      no_conflict = false;
    }

  bool support_zve = false;
  bool support_zvl = false;
  riscv_subset_t *s = rps->subset_list->head;
  for (; s != NULL; s = s->next)
    {
      if (!support_zve
	  && strncmp (s->name, "zve", 3) == 0)
	support_zve = true;
      if (!support_zvl
	  && strncmp (s->name, "zvl", 3) == 0)
	support_zvl = true;
      if (support_zve && support_zvl)
	break;
    }
  if (support_zvl && !support_zve)
    {
      rps->error_handler
	(_("zvl*b extensions need to enable either `v' or `zve' extension"));
      no_conflict = false;
    }

  return no_conflict;
}

/* Set the default subset list according to the default_enable field
   of riscv_supported_*ext tables.  */

static void
riscv_set_default_arch (riscv_parse_subset_t *rps)
{
  unsigned long enable = EXT_DEFAULT;
  int i, j;
  for (i = 0; riscv_all_supported_ext[i] != NULL; i++)
    {
      const struct riscv_supported_ext *table = riscv_all_supported_ext[i];
      for (j = 0; table[j].name != NULL; j++)
	{
	  bool implicit = false;
	  if (strcmp (table[j].name, "g") == 0)
	    implicit = true;
	  if (table[j].default_enable & enable)
	    riscv_parse_add_subset (rps, table[j].name,
				    RISCV_UNKNOWN_VERSION,
				    RISCV_UNKNOWN_VERSION, implicit);
	}
    }
}

/* Function for parsing ISA string.

   Return Value:
     Return TRUE on success.

   Arguments:
     `rps`: Hooks and status for parsing extensions.
     `arch`: Full ISA string.  */

bool
riscv_parse_subset (riscv_parse_subset_t *rps,
		    const char *arch)
{
  const char *p;

  /* Init the riscv_ext_order array to compare the order of extensions
     quickly.  */
  riscv_init_ext_order ();

  if (arch == NULL)
    {
      riscv_set_default_arch (rps);
      riscv_parse_add_implicit_subsets (rps);
      return riscv_parse_check_conflicts (rps);
    }

  for (p = arch; *p != '\0'; p++)
    {
      if (ISUPPER (*p))
	{
	  rps->error_handler
	    (_("%s: ISA string cannot contain uppercase letters"),
	     arch);
	  return false;
	}
    }

  p = arch;
  if (startswith (p, "rv32"))
    {
      *rps->xlen = 32;
      p += 4;
    }
  else if (startswith (p, "rv64"))
    {
      *rps->xlen = 64;
      p += 4;
    }
  else
    {
      /* ISA string shouldn't be NULL or empty here.  For linker,
	 it might be empty when we failed to merge the ISA string
	 in the riscv_merge_attributes.  For assembler, we might
	 give an empty string by .attribute arch, "" or -march=.
	 However, We have already issued the correct error message
	 in another side, so do not issue this error when the ISA
	 string is empty.  */
      if (strlen (arch))
	rps->error_handler (
	  _("%s: ISA string must begin with rv32 or rv64"),
	  arch);
      return false;
    }

  /* Parse single standard and prefixed extensions.  */
  if (riscv_parse_extensions (rps, arch, p) == NULL)
    return false;

  /* Finally add implicit extensions according to the current
     extensions.  */
  riscv_parse_add_implicit_subsets (rps);

  /* Check the conflicts.  */
  return riscv_parse_check_conflicts (rps);
}

/* Return the number of digits for the input.  */

size_t
riscv_estimate_digit (unsigned num)
{
  size_t digit = 0;
  if (num == 0)
    return 1;

  for (digit = 0; num ; num /= 10)
    digit++;

  return digit;
}

/* Auxiliary function to estimate string length of subset list.  */

static size_t
riscv_estimate_arch_strlen1 (const riscv_subset_t *subset)
{
  if (subset == NULL)
    return 6; /* For rv32/rv64/rv128 and string terminator.  */

  return riscv_estimate_arch_strlen1 (subset->next)
	 + strlen (subset->name)
	 + riscv_estimate_digit (subset->major_version)
	 + 1 /* For version seperator 'p'.  */
	 + riscv_estimate_digit (subset->minor_version)
	 + 1 /* For underscore.  */;
}

/* Estimate the string length of this subset list.  */

static size_t
riscv_estimate_arch_strlen (const riscv_subset_list_t *subset_list)
{
  return riscv_estimate_arch_strlen1 (subset_list->head);
}

/* Auxiliary function to convert subset info to string.  */

static void
riscv_arch_str1 (riscv_subset_t *subset,
		 char *attr_str, char *buf, size_t bufsz)
{
  const char *underline = "_";
  riscv_subset_t *subset_t = subset;

  if (subset_t == NULL)
    return;

  /* No underline between rvXX and i/e.  */
  if ((strcasecmp (subset_t->name, "i") == 0)
      || (strcasecmp (subset_t->name, "e") == 0))
    underline = "";

  snprintf (buf, bufsz, "%s%s%dp%d",
	    underline,
	    subset_t->name,
	    subset_t->major_version,
	    subset_t->minor_version);

  strncat (attr_str, buf, bufsz);

  /* Skip 'i' extension after 'e', or skip extensions which
     versions are unknown.  */
  while (subset_t->next
	 && ((strcmp (subset_t->name, "e") == 0
	      && strcmp (subset_t->next->name, "i") == 0)
	     || subset_t->next->major_version == RISCV_UNKNOWN_VERSION
	     || subset_t->next->minor_version == RISCV_UNKNOWN_VERSION))
    subset_t = subset_t->next;

  riscv_arch_str1 (subset_t->next, attr_str, buf, bufsz);
}

/* Convert subset information into string with explicit versions.  */

char *
riscv_arch_str (unsigned xlen, const riscv_subset_list_t *subset)
{
  size_t arch_str_len = riscv_estimate_arch_strlen (subset);
  char *attr_str = xmalloc (arch_str_len);
  char *buf = xmalloc (arch_str_len);

  snprintf (attr_str, arch_str_len, "rv%u", xlen);

  riscv_arch_str1 (subset->head, attr_str, buf, arch_str_len);
  free (buf);

  return attr_str;
}

/* Copy the subset in the subset list.  */

static struct riscv_subset_t *
riscv_copy_subset (riscv_subset_list_t *subset_list,
		   riscv_subset_t *subset)
{
  if (subset == NULL)
    return NULL;

  riscv_subset_t *new = xmalloc (sizeof *new);
  new->name = xstrdup (subset->name);
  new->major_version = subset->major_version;
  new->minor_version = subset->minor_version;
  new->next = riscv_copy_subset (subset_list, subset->next);

  if (subset->next == NULL)
    subset_list->tail = new;

  return new;
}

/* Copy the subset list.  */

riscv_subset_list_t *
riscv_copy_subset_list (riscv_subset_list_t *subset_list)
{
  riscv_subset_list_t *new = xmalloc (sizeof *new);
  new->head = riscv_copy_subset (new, subset_list->head);
  new->arch_str = strdup (subset_list->arch_str);
  return new;
}

/* Remove the SUBSET from the subset list.  */

static void
riscv_remove_subset (riscv_subset_list_t *subset_list,
		     const char *subset)
{
  riscv_subset_t *current = subset_list->head;
  riscv_subset_t *pre = NULL;
  for (; current != NULL; pre = current, current = current->next)
    {
      if (strcmp (current->name, subset) == 0)
	{
	  if (pre == NULL)
	    subset_list->head = current->next;
	  else
	    pre->next = current->next;
	  if (current->next == NULL)
	    subset_list->tail = pre;
	  free ((void *) current->name);
	  free (current);
	  break;
	}
    }
}

/* Add/Remove an extension to/from the subset list.  This is used for
   the .option rvc or norvc, and .option arch directives.  */

bool
riscv_update_subset (riscv_parse_subset_t *rps,
		     const char *str)
{
  const char *p = str;

  do
    {
      int major_version = RISCV_UNKNOWN_VERSION;
      int minor_version = RISCV_UNKNOWN_VERSION;

      bool removed = false;
      switch (*p)
	{
	case '+': removed = false; break;
	case '-': removed = true; break;
	default:
	  riscv_release_subset_list (rps->subset_list);
	  return riscv_parse_subset (rps, p);
	}
      ++p;

      char *subset = xstrdup (p);
      char *q = subset;
      const char *end_of_version;
      /* Extract the whole prefixed extension by ','.  */
      while (*q != '\0' && *q != ',')
        q++;

      /* Look forward to the first letter which is not <major>p<minor>.  */
      bool find_any_version = false;
      bool find_minor_version = false;
      size_t len = q - subset;
      size_t i;
      for (i = len; i > 0; i--)
        {
	  q--;
	  if (ISDIGIT (*q))
	    find_any_version = true;
	  else if (find_any_version
		   && !find_minor_version
		   && *q == 'p'
		   && ISDIGIT (*(q - 1)))
	    find_minor_version = true;
	  else
	    break;
	}
      if (len > 0)
	q++;

      /* Check if the end of extension is 'p' or not.  If yes, then
	 the second letter from the end cannot be number.  */
      if (len > 1 && *(q - 1) == 'p' && ISDIGIT (*(q - 2)))
	{
	  *q = '\0';
	  rps->error_handler
	    (_("invalid ISA extension ends with <number>p "
	       "in .option arch `%s'"), str);
	  free (subset);
	  return false;
	}

      end_of_version =
	riscv_parsing_subset_version (q, &major_version, &minor_version);
      *q = '\0';
      if (end_of_version == NULL)
	{
	  free (subset);
	  return false;
	}

      if (strlen (subset) == 0
	  || (strlen (subset) == 1
	      && riscv_ext_order[(*subset - 'a')] == 0)
	  || (strlen (subset) > 1
	      && rps->check_unknown_prefixed_ext
	      && !riscv_recognized_prefixed_ext (subset)))
	{
	  rps->error_handler
	    (_("unknown ISA extension `%s' in .option arch `%s'"),
	     subset, str);
	  free (subset);
	  return false;
	}

      if (strcmp (subset, "i") == 0
	  || strcmp (subset, "e") == 0
	  || strcmp (subset, "g") == 0)
	{
	  rps->error_handler
	    (_("cannot + or - base extension `%s' in .option "
	       "arch `%s'"), subset, str);
	  free (subset);
	  return false;
	}

      if (removed)
	riscv_remove_subset (rps->subset_list, subset);
      else
	riscv_parse_add_subset (rps, subset, major_version, minor_version, true);
      p += end_of_version - subset;
      free (subset);
    }
  while (*p++ == ',');

  riscv_parse_add_implicit_subsets (rps);
  return riscv_parse_check_conflicts (rps);
}

/* Check if the FEATURE subset is supported or not in the subset list.
   Return true if it is supported; Otherwise, return false.  */

bool
riscv_subset_supports (riscv_parse_subset_t *rps,
		       const char *feature)
{
  struct riscv_subset_t *subset;
  return riscv_lookup_subset (rps->subset_list, feature, &subset);
}

/* Each instuction is belonged to an instruction class INSN_CLASS_*.
   Call riscv_subset_supports to make sure if the instuction is valid.  */

bool
riscv_multi_subset_supports (riscv_parse_subset_t *rps,
			     enum riscv_insn_class insn_class)
{
  switch (insn_class)
    {
    case INSN_CLASS_I:
      return riscv_subset_supports (rps, "i");
    case INSN_CLASS_ZICBOM:
      return riscv_subset_supports (rps, "zicbom");
    case INSN_CLASS_ZICBOP:
      return riscv_subset_supports (rps, "zicbop");
    case INSN_CLASS_ZICBOZ:
      return riscv_subset_supports (rps, "zicboz");
    case INSN_CLASS_ZICOND:
      return riscv_subset_supports (rps, "zicond");
    case INSN_CLASS_ZICSR:
      return riscv_subset_supports (rps, "zicsr");
    case INSN_CLASS_ZIFENCEI:
      return riscv_subset_supports (rps, "zifencei");
    case INSN_CLASS_ZIHINTNTL:
      return riscv_subset_supports (rps, "zihintntl");
    case INSN_CLASS_ZIHINTNTL_AND_C:
      return (riscv_subset_supports (rps, "zihintntl")
	      && (riscv_subset_supports (rps, "c")
		  || riscv_subset_supports (rps, "zca")));
    case INSN_CLASS_ZIHINTPAUSE:
      return riscv_subset_supports (rps, "zihintpause");
    case INSN_CLASS_M:
      return riscv_subset_supports (rps, "m");
    case INSN_CLASS_ZMMUL:
      return riscv_subset_supports (rps, "zmmul");
    case INSN_CLASS_ZAAMO:
      return riscv_subset_supports (rps, "zaamo");
    case INSN_CLASS_ZABHA:
      return riscv_subset_supports (rps, "zabha");
    case INSN_CLASS_ZALRSC:
      return riscv_subset_supports (rps, "zalrsc");
    case INSN_CLASS_ZAWRS:
      return riscv_subset_supports (rps, "zawrs");
    case INSN_CLASS_F:
      return riscv_subset_supports (rps, "f");
    case INSN_CLASS_D:
      return riscv_subset_supports (rps, "d");
    case INSN_CLASS_Q:
      return riscv_subset_supports (rps, "q");
    case INSN_CLASS_C:
      return (riscv_subset_supports (rps, "c")
	      || riscv_subset_supports (rps, "zca"));
    case INSN_CLASS_F_AND_C:
      return (riscv_subset_supports (rps, "f")
	      && (riscv_subset_supports (rps, "c")
		  || riscv_subset_supports (rps, "zcf")));
    case INSN_CLASS_D_AND_C:
      return (riscv_subset_supports (rps, "d")
	      && (riscv_subset_supports (rps, "c")
		  || riscv_subset_supports (rps, "zcd")));
    case INSN_CLASS_F_INX:
      return (riscv_subset_supports (rps, "f")
	      || riscv_subset_supports (rps, "zfinx"));
    case INSN_CLASS_D_INX:
      return (riscv_subset_supports (rps, "d")
	      || riscv_subset_supports (rps, "zdinx"));
    case INSN_CLASS_Q_INX:
      return (riscv_subset_supports (rps, "q")
	      || riscv_subset_supports (rps, "zqinx"));
    case INSN_CLASS_ZFH_INX:
      return (riscv_subset_supports (rps, "zfh")
	      || riscv_subset_supports (rps, "zhinx"));
    case INSN_CLASS_ZFHMIN:
      return riscv_subset_supports (rps, "zfhmin");
    case INSN_CLASS_ZFHMIN_INX:
      return (riscv_subset_supports (rps, "zfhmin")
	      || riscv_subset_supports (rps, "zhinxmin"));
    case INSN_CLASS_ZFHMIN_AND_D_INX:
      return ((riscv_subset_supports (rps, "zfhmin")
	       && riscv_subset_supports (rps, "d"))
	      || (riscv_subset_supports (rps, "zhinxmin")
		  && riscv_subset_supports (rps, "zdinx")));
    case INSN_CLASS_ZFHMIN_AND_Q_INX:
      return ((riscv_subset_supports (rps, "zfhmin")
	       && riscv_subset_supports (rps, "q"))
	      || (riscv_subset_supports (rps, "zhinxmin")
		  && riscv_subset_supports (rps, "zqinx")));
    case INSN_CLASS_ZFBFMIN:
      return riscv_subset_supports (rps, "zfbfmin");
    case INSN_CLASS_ZFA:
      return riscv_subset_supports (rps, "zfa");
    case INSN_CLASS_D_AND_ZFA:
      return riscv_subset_supports (rps, "d")
	     && riscv_subset_supports (rps, "zfa");
    case INSN_CLASS_Q_AND_ZFA:
      return riscv_subset_supports (rps, "q")
	     && riscv_subset_supports (rps, "zfa");
    case INSN_CLASS_ZFH_AND_ZFA:
      return riscv_subset_supports (rps, "zfh")
	     && riscv_subset_supports (rps, "zfa");
    case INSN_CLASS_ZFH_OR_ZVFH_AND_ZFA:
      return (riscv_subset_supports (rps, "zfh")
	      || riscv_subset_supports (rps, "zvfh"))
	     && riscv_subset_supports (rps, "zfa");
    case INSN_CLASS_ZBA:
      return riscv_subset_supports (rps, "zba");
    case INSN_CLASS_ZBB:
      return riscv_subset_supports (rps, "zbb");
    case INSN_CLASS_ZBC:
      return riscv_subset_supports (rps, "zbc");
    case INSN_CLASS_ZBS:
      return riscv_subset_supports (rps, "zbs");
    case INSN_CLASS_ZBKB:
      return riscv_subset_supports (rps, "zbkb");
    case INSN_CLASS_ZBKC:
      return riscv_subset_supports (rps, "zbkc");
    case INSN_CLASS_ZBKX:
      return riscv_subset_supports (rps, "zbkx");
    case INSN_CLASS_ZBB_OR_ZBKB:
      return (riscv_subset_supports (rps, "zbb")
	      || riscv_subset_supports (rps, "zbkb"));
    case INSN_CLASS_ZBC_OR_ZBKC:
      return (riscv_subset_supports (rps, "zbc")
	      || riscv_subset_supports (rps, "zbkc"));
    case INSN_CLASS_ZKND:
      return riscv_subset_supports (rps, "zknd");
    case INSN_CLASS_ZKNE:
      return riscv_subset_supports (rps, "zkne");
    case INSN_CLASS_ZKNH:
      return riscv_subset_supports (rps, "zknh");
    case INSN_CLASS_ZKND_OR_ZKNE:
      return (riscv_subset_supports (rps, "zknd")
	      || riscv_subset_supports (rps, "zkne"));
    case INSN_CLASS_ZKSED:
      return riscv_subset_supports (rps, "zksed");
    case INSN_CLASS_ZKSH:
      return riscv_subset_supports (rps, "zksh");
    case INSN_CLASS_V:
      return (riscv_subset_supports (rps, "v")
	      || riscv_subset_supports (rps, "zve64x")
	      || riscv_subset_supports (rps, "zve32x"));
    case INSN_CLASS_ZVEF:
      return (riscv_subset_supports (rps, "v")
	      || riscv_subset_supports (rps, "zve64d")
	      || riscv_subset_supports (rps, "zve64f")
	      || riscv_subset_supports (rps, "zve32f"));
    case INSN_CLASS_ZVBB:
      return riscv_subset_supports (rps, "zvbb");
    case INSN_CLASS_ZVBC:
      return riscv_subset_supports (rps, "zvbc");
    case INSN_CLASS_ZVFBFMIN:
      return riscv_subset_supports (rps, "zvfbfmin");
    case INSN_CLASS_ZVFBFWMA:
      return riscv_subset_supports (rps, "zvfbfwma");
    case INSN_CLASS_ZVKB:
      return riscv_subset_supports (rps, "zvkb");
    case INSN_CLASS_ZVKG:
      return riscv_subset_supports (rps, "zvkg");
    case INSN_CLASS_ZVKNED:
      return riscv_subset_supports (rps, "zvkned");
    case INSN_CLASS_ZVKNHA_OR_ZVKNHB:
      return (riscv_subset_supports (rps, "zvknha")
	      || riscv_subset_supports (rps, "zvknhb"));
    case INSN_CLASS_ZVKSED:
      return riscv_subset_supports (rps, "zvksed");
    case INSN_CLASS_ZVKSH:
      return riscv_subset_supports (rps, "zvksh");
    case INSN_CLASS_ZCB:
      return riscv_subset_supports (rps, "zcb");
    case INSN_CLASS_ZCB_AND_ZBB:
      return (riscv_subset_supports (rps, "zcb")
	      && riscv_subset_supports (rps, "zbb"));
    case INSN_CLASS_ZCB_AND_ZBA:
      return (riscv_subset_supports (rps, "zcb")
	      && riscv_subset_supports (rps, "zba"));
    case INSN_CLASS_ZCB_AND_ZMMUL:
      return (riscv_subset_supports (rps, "zcb")
	      && riscv_subset_supports (rps, "zmmul"));
    case INSN_CLASS_ZCMP:
      return riscv_subset_supports (rps, "zcmp");
    case INSN_CLASS_SVINVAL:
      return riscv_subset_supports (rps, "svinval");
    case INSN_CLASS_H:
      return riscv_subset_supports (rps, "h");
    case INSN_CLASS_XCVMAC:
      return riscv_subset_supports (rps, "xcvmac");
    case INSN_CLASS_XCVALU:
      return riscv_subset_supports (rps, "xcvalu");
    case INSN_CLASS_XCVELW:
      return riscv_subset_supports (rps, "xcvelw");
    case INSN_CLASS_XCVBI:
      return riscv_subset_supports (rps, "xcvbi");
    case INSN_CLASS_XCVMEM:
      return riscv_subset_supports (rps, "xcvmem");
    case INSN_CLASS_XTHEADBA:
      return riscv_subset_supports (rps, "xtheadba");
    case INSN_CLASS_XTHEADBB:
      return riscv_subset_supports (rps, "xtheadbb");
    case INSN_CLASS_XTHEADBS:
      return riscv_subset_supports (rps, "xtheadbs");
    case INSN_CLASS_XTHEADCMO:
      return riscv_subset_supports (rps, "xtheadcmo");
    case INSN_CLASS_XTHEADCONDMOV:
      return riscv_subset_supports (rps, "xtheadcondmov");
    case INSN_CLASS_XTHEADFMEMIDX:
      return riscv_subset_supports (rps, "xtheadfmemidx");
    case INSN_CLASS_XTHEADFMV:
      return riscv_subset_supports (rps, "xtheadfmv");
    case INSN_CLASS_XTHEADINT:
      return riscv_subset_supports (rps, "xtheadint");
    case INSN_CLASS_XTHEADMAC:
      return riscv_subset_supports (rps, "xtheadmac");
    case INSN_CLASS_XTHEADMEMIDX:
      return riscv_subset_supports (rps, "xtheadmemidx");
    case INSN_CLASS_XTHEADMEMPAIR:
      return riscv_subset_supports (rps, "xtheadmempair");
    case INSN_CLASS_XTHEADSYNC:
      return riscv_subset_supports (rps, "xtheadsync");
    case INSN_CLASS_XTHEADVECTOR:
      return riscv_subset_supports (rps, "xtheadvector");
    case INSN_CLASS_XTHEADZVAMO:
      return riscv_subset_supports (rps, "xtheadzvamo");
    case INSN_CLASS_XVENTANACONDOPS:
      return riscv_subset_supports (rps, "xventanacondops");
    case INSN_CLASS_XSFVCP:
      return riscv_subset_supports (rps, "xsfvcp");
    default:
      rps->error_handler
        (_("internal: unreachable INSN_CLASS_*"));
      return false;
    }
}

/* Each instuction is belonged to an instruction class INSN_CLASS_*.
   Call riscv_subset_supports_ext to determine the missing extension.  */

const char *
riscv_multi_subset_supports_ext (riscv_parse_subset_t *rps,
				 enum riscv_insn_class insn_class)
{
  switch (insn_class)
    {
    case INSN_CLASS_I:
      return "i";
    case INSN_CLASS_ZICBOM:
      return "zicbom";
    case INSN_CLASS_ZICBOP:
      return "zicbop";
    case INSN_CLASS_ZICBOZ:
      return "zicboz";
    case INSN_CLASS_ZICOND:
      return "zicond";
    case INSN_CLASS_ZICSR:
      return "zicsr";
    case INSN_CLASS_ZIFENCEI:
      return "zifencei";
    case INSN_CLASS_ZIHINTNTL:
      return "zihintntl";
    case INSN_CLASS_ZIHINTNTL_AND_C:
      if (!riscv_subset_supports (rps, "zihintntl"))
	{
	  if (!riscv_subset_supports (rps, "c")
	      && !riscv_subset_supports (rps, "zca"))
	    return _("zihintntl' and `c', or `zihintntl' and `zca");
	  else
	    return "zihintntl";
	}
      else
	return _("c' or `zca");
    case INSN_CLASS_ZIHINTPAUSE:
      return "zihintpause";
    case INSN_CLASS_M:
      return "m";
    case INSN_CLASS_ZMMUL:
      return _ ("m' or `zmmul");
    case INSN_CLASS_ZAAMO:
      return "zaamo";
    case INSN_CLASS_ZABHA:
      return "zabha";
    case INSN_CLASS_ZALRSC:
      return "zalrsc";
    case INSN_CLASS_ZAWRS:
      return "zawrs";
    case INSN_CLASS_F:
      return "f";
    case INSN_CLASS_D:
      return "d";
    case INSN_CLASS_Q:
      return "q";
    case INSN_CLASS_C:
      return _("c' or `zca");
    case INSN_CLASS_F_AND_C:
      if (!riscv_subset_supports (rps, "f"))
	{
	  if (!riscv_subset_supports (rps, "c")
	      && !riscv_subset_supports (rps, "zcf"))
	    return _("f' and `c', or `f' and `zcf"); 
	  else
	    return "f";
	}
      else
	return _("c' or `zcf");
    case INSN_CLASS_D_AND_C:
      if (!riscv_subset_supports (rps, "d"))
	{
	  if (!riscv_subset_supports (rps, "c")
	      && !riscv_subset_supports (rps, "zcd"))
	    return _("d' and `c', or `d' and `zcd");
	  else
	    return "d";
	}
      else
	return _("c' or `zcd");
    case INSN_CLASS_F_INX:
      return _("f' or `zfinx");
    case INSN_CLASS_D_INX:
      return _("d' or `zdinx");
    case INSN_CLASS_Q_INX:
      return _("q' or `zqinx");
    case INSN_CLASS_ZFH_INX:
      return _("zfh' or `zhinx");
    case INSN_CLASS_ZFHMIN:
      return "zfhmin";
    case INSN_CLASS_ZFHMIN_INX:
      return _("zfhmin' or `zhinxmin");
    case INSN_CLASS_ZFHMIN_AND_D_INX:
      if (riscv_subset_supports (rps, "zfhmin"))
	return "d";
      else if (riscv_subset_supports (rps, "d"))
	return "zfhmin";
      else if (riscv_subset_supports (rps, "zhinxmin"))
	return "zdinx";
      else if (riscv_subset_supports (rps, "zdinx"))
	return "zhinxmin";
      else
	return _("zfhmin' and `d', or `zhinxmin' and `zdinx");
    case INSN_CLASS_ZFHMIN_AND_Q_INX:
      if (riscv_subset_supports (rps, "zfhmin"))
	return "q";
      else if (riscv_subset_supports (rps, "q"))
	return "zfhmin";
      else if (riscv_subset_supports (rps, "zhinxmin"))
	return "zqinx";
      else if (riscv_subset_supports (rps, "zqinx"))
	return "zhinxmin";
      else
	return _("zfhmin' and `q', or `zhinxmin' and `zqinx");
    case INSN_CLASS_ZFBFMIN:
      return "zfbfmin";
    case INSN_CLASS_ZFA:
      return "zfa";
    case INSN_CLASS_D_AND_ZFA:
      if (!riscv_subset_supports (rps, "d")
	  && !riscv_subset_supports (rps, "zfa"))
	return _("d' and `zfa");
      else if (!riscv_subset_supports (rps, "d"))
	return "d";
      else
	return "zfa";
    case INSN_CLASS_Q_AND_ZFA:
      if (!riscv_subset_supports (rps, "q")
	  && !riscv_subset_supports (rps, "zfa"))
	return _("q' and `zfa");
      else if (!riscv_subset_supports (rps, "q"))
	return "q";
      else
	return "zfa";
    case INSN_CLASS_ZFH_AND_ZFA:
      if (!riscv_subset_supports (rps, "zfh")
	  && !riscv_subset_supports (rps, "zfa"))
	return _("zfh' and `zfa");
      else if (!riscv_subset_supports (rps, "zfh"))
	return "zfh";
      else
	return "zfa";
    case INSN_CLASS_ZFH_OR_ZVFH_AND_ZFA:
      if (!riscv_subset_supports (rps, "zfa"))
	{
	  if (!riscv_subset_supports (rps, "zfh")
	      && !riscv_subset_supports (rps, "zvfh"))
	    return _("zfh' and `zfa', or `zvfh' and `zfa");
	  else
	    return "zfa";
	}
      else
	return _("zfh' or `zvfh");
    case INSN_CLASS_ZBA:
      return "zba";
    case INSN_CLASS_ZBB:
      return "zbb";
    case INSN_CLASS_ZBC:
      return "zbc";
    case INSN_CLASS_ZBS:
      return "zbs";
    case INSN_CLASS_ZBKB:
      return "zbkb";
    case INSN_CLASS_ZBKC:
      return "zbkc";
    case INSN_CLASS_ZBKX:
      return "zbkx";
    case INSN_CLASS_ZBB_OR_ZBKB:
      return _("zbb' or `zbkb");
    case INSN_CLASS_ZBC_OR_ZBKC:
      return _("zbc' or `zbkc");
    case INSN_CLASS_ZKND:
      return "zknd";
    case INSN_CLASS_ZKNE:
      return "zkne";
    case INSN_CLASS_ZKNH:
      return "zknh";
    case INSN_CLASS_ZKND_OR_ZKNE:
      return _("zknd' or `zkne");
    case INSN_CLASS_ZKSED:
      return "zksed";
    case INSN_CLASS_ZKSH:
      return "zksh";
    case INSN_CLASS_V:
      return _("v' or `zve64x' or `zve32x");
    case INSN_CLASS_ZVEF:
      return _("v' or `zve64d' or `zve64f' or `zve32f");
    case INSN_CLASS_ZVBB:
      return _("zvbb");
    case INSN_CLASS_ZVBC:
      return _("zvbc");
    case INSN_CLASS_ZVFBFMIN:
      return "zvfbfmin";
    case INSN_CLASS_ZVFBFWMA:
      return "zvfbfwma";
    case INSN_CLASS_ZVKB:
      return _("zvkb");
    case INSN_CLASS_ZVKG:
      return _("zvkg");
    case INSN_CLASS_ZVKNED:
      return _("zvkned");
    case INSN_CLASS_ZVKNHA_OR_ZVKNHB:
      return _("zvknha' or `zvknhb");
    case INSN_CLASS_ZVKSED:
      return _("zvksed");
    case INSN_CLASS_ZVKSH:
      return _("zvksh");
    case INSN_CLASS_ZCB:
      return "zcb";
    case INSN_CLASS_ZCB_AND_ZBA:
      return _("zcb' and `zba");
    case INSN_CLASS_ZCB_AND_ZBB:
      return _("zcb' and `zbb");
    case INSN_CLASS_ZCB_AND_ZMMUL:
      return _("zcb' and `zmmul', or `zcb' and `m");
    case INSN_CLASS_ZCMP:
      return "zcmp";
    case INSN_CLASS_SVINVAL:
      return "svinval";
    case INSN_CLASS_H:
      return _("h");
    case INSN_CLASS_XCVMAC:
      return "xcvmac";
    case INSN_CLASS_XCVALU:
      return "xcvalu";
    case INSN_CLASS_XCVELW:
      return "xcvelw";
    case INSN_CLASS_XCVBI:
      return "xcvbi";
    case INSN_CLASS_XCVMEM:
      return "xcvmem";
    case INSN_CLASS_XTHEADBA:
      return "xtheadba";
    case INSN_CLASS_XTHEADBB:
      return "xtheadbb";
    case INSN_CLASS_XTHEADBS:
      return "xtheadbs";
    case INSN_CLASS_XTHEADCMO:
      return "xtheadcmo";
    case INSN_CLASS_XTHEADCONDMOV:
      return "xtheadcondmov";
    case INSN_CLASS_XTHEADFMEMIDX:
      return "xtheadfmemidx";
    case INSN_CLASS_XTHEADFMV:
      return "xtheadfmv";
    case INSN_CLASS_XTHEADINT:
      return "xtheadint";
    case INSN_CLASS_XTHEADMAC:
      return "xtheadmac";
    case INSN_CLASS_XTHEADMEMIDX:
      return "xtheadmemidx";
    case INSN_CLASS_XTHEADMEMPAIR:
      return "xtheadmempair";
    case INSN_CLASS_XTHEADSYNC:
      return "xtheadsync";
    case INSN_CLASS_XTHEADVECTOR:
      return "xtheadvector";
    case INSN_CLASS_XTHEADZVAMO:
      return "xtheadzvamo";
    default:
      rps->error_handler
        (_("internal: unreachable INSN_CLASS_*"));
      return NULL;
    }
}

/* Print supported extensions with versions if -march=help.  */

void
riscv_print_extensions (void)
{
  /* Record the previous printed extension.
     Print the current one if they are not the same.  */
  const struct riscv_supported_ext *cur = NULL, *prev = NULL;
  int i, j;

  printf ("All available -march extensions for RISC-V:");

  for (i = 0; riscv_all_supported_ext[i] != NULL; i++)
    {
      const struct riscv_supported_ext *exts = riscv_all_supported_ext[i];
      prev = NULL;
      for (j = 0; exts[j].name != NULL; j++)
	{
	  cur = &exts[j];
	  /* Unclear version information, skip.  */
	  if (cur->isa_spec_class == ISA_SPEC_CLASS_NONE
	      || cur->major_version == RISCV_UNKNOWN_VERSION
	      || cur->minor_version == RISCV_UNKNOWN_VERSION)
	    continue;

	  /* Same extension.  */
	  if (prev && strcmp (prev->name, cur->name) == 0)
	    {
	      /* Same version, skip.  */
	      if (prev->major_version == cur->major_version
		  && prev->minor_version == cur->minor_version)
		continue;
	      /* Different version, print version with comma.  */
	      else
		printf (", %d.%d", cur->major_version, cur->minor_version);
	    }
	  /* Different extension, print extension and version with newline.  */
	  else
	    printf ("\n\t%-40s%d.%d", cur->name, cur->major_version,
		    cur->minor_version);
	  prev = &exts[j];
	}
    }
  printf ("\n");
}