summaryrefslogtreecommitdiffhomepage
path: root/modutils/insmod.c
blob: f1798ad0927f95052c4fce51c8ab110f6d8f1a6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
/* vi: set sw=4 ts=4: */
/*
 * Mini insmod implementation for busybox
 *
 * Copyright (C) 1999,2000,2001 by Lineo, inc.
 * Written by Erik Andersen <andersen@lineo.com>
 * and Ron Alder <alder@lineo.com>
 *
 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
 *
 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI.  Only
 * very minor changes required to also work with StrongArm and presumably
 * all ARM based systems.
 *
 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
 *   PowerPC specific code stolen from modutils-2.3.16, 
 *   written by Paul Mackerras, Copyright 1996, 1997 Linux International.
 *   I've only tested the code on mpc8xx platforms in big-endian mode.
 *   Did some cleanup and added BB_USE_xxx_ENTRIES...
 *
 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
 *   based on modutils-2.4.2
 *   MIPS specific support for Elf loading and relocation.
 *   Copyright 1996, 1997 Linux International.
 *   Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
 *
 * Based almost entirely on the Linux modutils-2.3.11 implementation.
 *   Copyright 1996, 1997 Linux International.
 *   New implementation contributed by Richard Henderson <rth@tamu.edu>
 *   Based on original work by Bjorn Ekwall <bj0rn@blox.se>
 *   Restructured (and partly rewritten) by:
 *   Björn Ekwall <bj0rn@blox.se> February 1999
 *
 * 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include <getopt.h>
#include <sys/utsname.h>
#include "busybox.h"

#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
# define new_sys_init_module	init_module
#else
# define old_sys_init_module	init_module
#endif

#if defined(__powerpc__)
#define BB_USE_PLT_ENTRIES
#define BB_PLT_ENTRY_SIZE 16
#endif

#if defined(__arm__)
#define BB_USE_PLT_ENTRIES
#define BB_PLT_ENTRY_SIZE 8
#define BB_USE_GOT_ENTRIES
#define BB_GOT_ENTRY_SIZE 8
#endif

#if defined(__sh__)
#define BB_USE_GOT_ENTRIES
#define BB_GOT_ENTRY_SIZE 4
#endif

#if defined(__i386__)
#define BB_USE_GOT_ENTRIES
#define BB_GOT_ENTRY_SIZE 4
#endif

#if defined(__mips__)
// neither used
#endif

//----------------------------------------------------------------------------
//--------modutils module.h, lines 45-242
//----------------------------------------------------------------------------

/* Definitions for the Linux module syscall interface.
   Copyright 1996, 1997 Linux International.

   Contributed by Richard Henderson <rth@tamu.edu>

   This file is part of the Linux modutils.

   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 2 of the License, or (at your
   option) any later version.

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

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */


#ifndef MODUTILS_MODULE_H
static const int MODUTILS_MODULE_H = 1;

#ident "$Id: insmod.c,v 1.57 2001/04/05 07:33:10 andersen Exp $"

/* This file contains the structures used by the 2.0 and 2.1 kernels.
   We do not use the kernel headers directly because we do not wish
   to be dependant on a particular kernel version to compile insmod.  */


/*======================================================================*/
/* The structures used by Linux 2.0.  */

/* The symbol format used by get_kernel_syms(2).  */
struct old_kernel_sym
{
  unsigned long value;
  char name[60];
};

struct old_module_ref
{
  unsigned long module;		/* kernel addresses */
  unsigned long next;
};

struct old_module_symbol
{
  unsigned long addr;
  unsigned long name;
};

struct old_symbol_table
{
  int size;			/* total, including string table!!! */
  int n_symbols;
  int n_refs;
  struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
  struct old_module_ref ref[0];	/* actual size defined by n_refs */
};

struct old_mod_routines
{
  unsigned long init;
  unsigned long cleanup;
};

struct old_module
{
  unsigned long next;
  unsigned long ref;		/* the list of modules that refer to me */
  unsigned long symtab;
  unsigned long name;
  int size;			/* size of module in pages */
  unsigned long addr;		/* address of module */
  int state;
  unsigned long cleanup;	/* cleanup routine */
};

/* Sent to init_module(2) or'ed into the code size parameter.  */
static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */

int get_kernel_syms(struct old_kernel_sym *);
int old_sys_init_module(const char *name, char *code, unsigned codesize,
			struct old_mod_routines *, struct old_symbol_table *);

/*======================================================================*/
/* For sizeof() which are related to the module platform and not to the
   environment isnmod is running in, use sizeof_xx instead of sizeof(xx).  */

#define tgt_sizeof_char		sizeof(char)
#define tgt_sizeof_short	sizeof(short)
#define tgt_sizeof_int		sizeof(int)
#define tgt_sizeof_long		sizeof(long)
#define tgt_sizeof_char_p	sizeof(char *)
#define tgt_sizeof_void_p	sizeof(void *)
#define tgt_long		long

#if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
#undef tgt_sizeof_long
#undef tgt_sizeof_char_p
#undef tgt_sizeof_void_p
#undef tgt_long
static const int tgt_sizeof_long = 8;
static const int tgt_sizeof_char_p = 8;
static const int tgt_sizeof_void_p = 8;
#define tgt_long		long long
#endif

/*======================================================================*/
/* The structures used in Linux 2.1.  */

/* Note: new_module_symbol does not use tgt_long intentionally */
struct new_module_symbol
{
  unsigned long value;
  unsigned long name;
};

struct new_module_persist;

struct new_module_ref
{
  unsigned tgt_long dep;		/* kernel addresses */
  unsigned tgt_long ref;
  unsigned tgt_long next_ref;
};

struct new_module
{
  unsigned tgt_long size_of_struct;	/* == sizeof(module) */
  unsigned tgt_long next;
  unsigned tgt_long name;
  unsigned tgt_long size;

  tgt_long usecount;
  unsigned tgt_long flags;		/* AUTOCLEAN et al */

  unsigned nsyms;
  unsigned ndeps;

  unsigned tgt_long syms;
  unsigned tgt_long deps;
  unsigned tgt_long refs;
  unsigned tgt_long init;
  unsigned tgt_long cleanup;
  unsigned tgt_long ex_table_start;
  unsigned tgt_long ex_table_end;
#ifdef __alpha__
  unsigned tgt_long gp;
#endif
  /* Everything after here is extension.  */
  unsigned tgt_long persist_start;
  unsigned tgt_long persist_end;
  unsigned tgt_long can_unload;
  unsigned tgt_long runsize;
};

struct new_module_info
{
  unsigned long addr;
  unsigned long size;
  unsigned long flags;
	   long usecount;
};

/* Bits of module.flags.  */
static const int NEW_MOD_RUNNING = 1;
static const int NEW_MOD_DELETED = 2;
static const int NEW_MOD_AUTOCLEAN = 4;
static const int NEW_MOD_VISITED = 8;
static const int NEW_MOD_USED_ONCE = 16;

int new_sys_init_module(const char *name, const struct new_module *);
int query_module(const char *name, int which, void *buf, size_t bufsize,
		 size_t *ret);

/* Values for query_module's which.  */

static const int QM_MODULES = 1;
static const int QM_DEPS = 2;
static const int QM_REFS = 3;
static const int QM_SYMBOLS = 4;
static const int QM_INFO = 5;

/*======================================================================*/
/* The system calls unchanged between 2.0 and 2.1.  */

unsigned long create_module(const char *, size_t);
int delete_module(const char *);


#endif /* module.h */

//----------------------------------------------------------------------------
//--------end of modutils module.h
//----------------------------------------------------------------------------



//----------------------------------------------------------------------------
//--------modutils obj.h, lines 253-462
//----------------------------------------------------------------------------

/* Elf object file loading and relocation routines.
   Copyright 1996, 1997 Linux International.

   Contributed by Richard Henderson <rth@tamu.edu>

   This file is part of the Linux modutils.

   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 2 of the License, or (at your
   option) any later version.

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

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */


#ifndef MODUTILS_OBJ_H
static const int MODUTILS_OBJ_H = 1;

#ident "$Id: insmod.c,v 1.57 2001/04/05 07:33:10 andersen Exp $"

/* The relocatable object is manipulated using elfin types.  */

#include <stdio.h>
#include <elf.h>


/* Machine-specific elf macros for i386 et al.  */

/* the SH changes have only been tested on the SH4 in =little endian= mode */
/* I'm not sure about big endian, so let's warn: */

#if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
#error insmod.c may require changes for use on big endian SH4/SH3
#endif

/* it may or may not work on the SH1/SH2... So let's error on those
   also */
#if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
#error insmod.c may require changes for non-SH3/SH4 use
#endif

#define ELFCLASSM	ELFCLASS32

#if defined(__sh__)

#define MATCH_MACHINE(x) (x == EM_SH)
#define SHT_RELM	SHT_RELA
#define Elf32_RelM	Elf32_Rela
#define ELFDATAM	ELFDATA2LSB

#elif defined(__arm__)

#define MATCH_MACHINE(x) (x == EM_ARM)
#define SHT_RELM	SHT_REL
#define Elf32_RelM	Elf32_Rel
#define ELFDATAM	ELFDATA2LSB

#elif defined(__powerpc__)

#define MATCH_MACHINE(x) (x == EM_PPC)
#define SHT_RELM	SHT_RELA
#define Elf32_RelM	Elf32_Rela
#define ELFDATAM        ELFDATA2MSB

#elif defined(__mips__)

/* Account for ELF spec changes.  */
#ifndef EM_MIPS_RS3_LE
#ifdef EM_MIPS_RS4_BE
#define EM_MIPS_RS3_LE	EM_MIPS_RS4_BE
#else
#define EM_MIPS_RS3_LE	10
#endif
#endif /* !EM_MIPS_RS3_LE */

#define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
#define SHT_RELM	SHT_REL
#define Elf32_RelM	Elf32_Rel
#ifdef __MIPSEB__
#define ELFDATAM        ELFDATA2MSB
#endif
#ifdef __MIPSEL__
#define ELFDATAM        ELFDATA2LSB
#endif

#elif defined(__i386__)

/* presumably we can use these for anything but the SH and ARM*/
/* this is the previous behavior, but it does result in
   insmod.c being broken on anything except i386 */
#ifndef EM_486
#define MATCH_MACHINE(x)  (x == EM_386)
#else
#define MATCH_MACHINE(x)  (x == EM_386 || x == EM_486)
#endif

#define SHT_RELM	SHT_REL
#define Elf32_RelM	Elf32_Rel
#define ELFDATAM	ELFDATA2LSB

#else
#error Sorry, but insmod.c does not yet support this architecture...
#endif

#ifndef ElfW
# if ELFCLASSM == ELFCLASS32
#  define ElfW(x)  Elf32_ ## x
#  define ELFW(x)  ELF32_ ## x
# else
#  define ElfW(x)  Elf64_ ## x
#  define ELFW(x)  ELF64_ ## x
# endif
#endif

/* For some reason this is missing from libc5.  */
#ifndef ELF32_ST_INFO
# define ELF32_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))
#endif

#ifndef ELF64_ST_INFO
# define ELF64_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))
#endif

struct obj_string_patch;
struct obj_symbol_patch;

struct obj_section
{
  ElfW(Shdr) header;
  const char *name;
  char *contents;
  struct obj_section *load_next;
  int idx;
};

struct obj_symbol
{
  struct obj_symbol *next;	/* hash table link */
  const char *name;
  unsigned long value;
  unsigned long size;
  int secidx;			/* the defining section index/module */
  int info;
  int ksymidx;			/* for export to the kernel symtab */
  int referenced;		/* actually used in the link */
};

/* Hardcode the hash table size.  We shouldn't be needing so many
   symbols that we begin to degrade performance, and we get a big win
   by giving the compiler a constant divisor.  */

#define HASH_BUCKETS  521

struct obj_file
{
  ElfW(Ehdr) header;
  ElfW(Addr) baseaddr;
  struct obj_section **sections;
  struct obj_section *load_order;
  struct obj_section **load_order_search_start;
  struct obj_string_patch *string_patches;
  struct obj_symbol_patch *symbol_patches;
  int (*symbol_cmp)(const char *, const char *);
  unsigned long (*symbol_hash)(const char *);
  unsigned long local_symtab_size;
  struct obj_symbol **local_symtab;
  struct obj_symbol *symtab[HASH_BUCKETS];
};

enum obj_reloc
{
  obj_reloc_ok,
  obj_reloc_overflow,
  obj_reloc_dangerous,
  obj_reloc_unhandled
};

struct obj_string_patch
{
  struct obj_string_patch *next;
  int reloc_secidx;
  ElfW(Addr) reloc_offset;
  ElfW(Addr) string_offset;
};

struct obj_symbol_patch
{
  struct obj_symbol_patch *next;
  int reloc_secidx;
  ElfW(Addr) reloc_offset;
  struct obj_symbol *sym;
};


/* Generic object manipulation routines.  */

unsigned long obj_elf_hash(const char *);

unsigned long obj_elf_hash_n(const char *, unsigned long len);

struct obj_symbol *obj_add_symbol (struct obj_file *f, const char *name,
				   unsigned long symidx, int info, int secidx,
				   ElfW(Addr) value, unsigned long size);

struct obj_symbol *obj_find_symbol (struct obj_file *f,
					 const char *name);

ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
				  struct obj_symbol *sym);

void obj_set_symbol_compare(struct obj_file *f,
			    int (*cmp)(const char *, const char *),
			    unsigned long (*hash)(const char *));

struct obj_section *obj_find_section (struct obj_file *f,
					   const char *name);

void obj_insert_section_load_order (struct obj_file *f,
				    struct obj_section *sec);

struct obj_section *obj_create_alloced_section (struct obj_file *f,
						const char *name,
						unsigned long align,
						unsigned long size);

struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
						      const char *name,
						      unsigned long align,
						      unsigned long size);

void *obj_extend_section (struct obj_section *sec, unsigned long more);

int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
		     const char *string);

int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
		     struct obj_symbol *sym);

int obj_check_undefineds(struct obj_file *f);

void obj_allocate_commons(struct obj_file *f);

unsigned long obj_load_size (struct obj_file *f);

int obj_relocate (struct obj_file *f, ElfW(Addr) base);

struct obj_file *obj_load(FILE *f);

int obj_create_image (struct obj_file *f, char *image);

/* Architecture specific manipulation routines.  */

struct obj_file *arch_new_file (void);

struct obj_section *arch_new_section (void);

struct obj_symbol *arch_new_symbol (void);

enum obj_reloc arch_apply_relocation (struct obj_file *f,
				      struct obj_section *targsec,
				      struct obj_section *symsec,
				      struct obj_symbol *sym,
				      ElfW(RelM) *rel, ElfW(Addr) value);

int arch_create_got (struct obj_file *f);

struct new_module;
int arch_init_module (struct obj_file *f, struct new_module *);

#endif /* obj.h */
//----------------------------------------------------------------------------
//--------end of modutils obj.h
//----------------------------------------------------------------------------





#define _PATH_MODULES	"/lib/modules"
static const int STRVERSIONLEN = 32;

/*======================================================================*/

int flag_force_load = 0;
int flag_autoclean = 0;
int flag_verbose = 0;
int flag_export = 1;


/*======================================================================*/

/* previously, these were named i386_* but since we could be
   compiling for the sh, I've renamed them to the more general
   arch_* These structures are the same between the x86 and SH, 
   and we can't support anything else right now anyway. In the
   future maybe they should be #if defined'd */

/* Done ;-) */



#if defined(BB_USE_PLT_ENTRIES)
struct arch_plt_entry
{
  int offset;
  int allocated:1;
  int inited:1;                /* has been set up */
};
#endif

#if defined(BB_USE_GOT_ENTRIES)
struct arch_got_entry {
	int offset;
	unsigned offset_done:1;
	unsigned reloc_done:1;
};
#endif

#if defined(__mips__)
struct mips_hi16
{
  struct mips_hi16 *next;
  Elf32_Addr *addr;
  Elf32_Addr value;
};
#endif

struct arch_file {
	struct obj_file root;
#if defined(BB_USE_PLT_ENTRIES)
	struct obj_section *plt;
#endif
#if defined(BB_USE_GOT_ENTRIES)
	struct obj_section *got;
#endif
#if defined(__mips__)
	struct mips_hi16 *mips_hi16_list;
#endif
};

struct arch_symbol {
	struct obj_symbol root;
#if defined(BB_USE_PLT_ENTRIES)
	struct arch_plt_entry pltent;
#endif
#if defined(BB_USE_GOT_ENTRIES)
	struct arch_got_entry gotent;
#endif
};


struct external_module {
	const char *name;
	ElfW(Addr) addr;
	int used;
	size_t nsyms;
	struct new_module_symbol *syms;
};

struct new_module_symbol *ksyms;
size_t nksyms;

struct external_module *ext_modules;
int n_ext_modules;
int n_ext_modules_used;


extern int delete_module(const char *);


/* This is kind of troublesome. See, we don't actually support
   the m68k or the arm the same way we support i386 and (now)
   sh. In doing my SH patch, I just assumed that whatever works
   for i386 also works for m68k and arm since currently insmod.c
   does nothing special for them. If this isn't true, the below
   line is rather misleading IMHO, and someone should either
   change it or add more proper architecture-dependent support
   for these boys.

   -- Bryan Rittmeyer <bryan@ixiacom.com>                    */

static char m_filename[BUFSIZ + 1];
static char m_fullName[BUFSIZ + 1];

/*======================================================================*/


static int findNamedModule(const char *fileName, struct stat *statbuf,
						   void *userDate)
{
	char *fullName = (char *) userDate;


	if (fullName[0] == '\0')
		return (FALSE);
	else {
		char *tmp = strrchr((char *) fileName, '/');

		if (tmp == NULL)
			tmp = (char *) fileName;
		else
			tmp++;
		if (check_wildcard_match(tmp, fullName) == TRUE) {
			/* Stop searching if we find a match */
			memcpy(m_filename, fileName, strlen(fileName)+1);
			return (FALSE);
		}
	}
	return (TRUE);
}


/*======================================================================*/

struct obj_file *arch_new_file(void)
{
	struct arch_file *f;
	f = xmalloc(sizeof(*f));

#if defined(BB_USE_PLT_ENTRIES)
	f->plt = NULL;
#endif
#if defined(BB_USE_GOT_ENTRIES)
	f->got = NULL;
#endif
#if defined(__mips__)
	f->mips_hi16_list = NULL;
#endif

	return &f->root;
}

struct obj_section *arch_new_section(void)
{
	return xmalloc(sizeof(struct obj_section));
}

struct obj_symbol *arch_new_symbol(void)
{
	struct arch_symbol *sym;
	sym = xmalloc(sizeof(*sym));

#if defined(BB_USE_PLT_ENTRIES)
	memset(&sym->pltent, 0, sizeof(sym->pltent));
#endif
#if defined(BB_USE_GOT_ENTRIES)
	memset(&sym->gotent, 0, sizeof(sym->gotent));
#endif

	return &sym->root;
}

enum obj_reloc
arch_apply_relocation(struct obj_file *f,
					  struct obj_section *targsec,
					  struct obj_section *symsec,
					  struct obj_symbol *sym,
				      ElfW(RelM) *rel, ElfW(Addr) v)
{
	struct arch_file *ifile = (struct arch_file *) f;
#if !(defined(__mips__))
	struct arch_symbol *isym = (struct arch_symbol *) sym;
#endif

	ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
	ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
#if defined(BB_USE_GOT_ENTRIES)
	ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
#endif
#if defined(BB_USE_PLT_ENTRIES)
	ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
	struct arch_plt_entry *pe;
	unsigned long *ip;
#endif
	enum obj_reloc ret = obj_reloc_ok;

	switch (ELF32_R_TYPE(rel->r_info)) {

/* even though these constants seem to be the same for
   the i386 and the sh, we "#if define" them for clarity
   and in case that ever changes */
#if defined(__sh__)
	case R_SH_NONE:
#elif defined(__arm__)
	case R_ARM_NONE:
#elif defined(__i386__)
	case R_386_NONE:
#elif defined(__powerpc__)
	case R_PPC_NONE:
#elif defined(__mips__)
	case R_MIPS_NONE:
#endif
		break;

#if defined(__sh__)
	case R_SH_DIR32:
#elif defined(__arm__)
	case R_ARM_ABS32:
#elif defined(__i386__)
	case R_386_32:	
#elif defined(__powerpc__)
	case R_PPC_ADDR32:
#elif defined(__mips__)
	case R_MIPS_32:
#endif
		*loc += v;
		break;

#if defined(__powerpc__)
	case R_PPC_ADDR16_HA:
		*(unsigned short *)loc = (v + 0x8000) >> 16;
		break;

	case R_PPC_ADDR16_HI:
		*(unsigned short *)loc = v >> 16;
		break;

	case R_PPC_ADDR16_LO:
		*(unsigned short *)loc = v;
		break;
#endif

#if defined(__mips__)
	case R_MIPS_26:
		if (v % 4)
			ret = obj_reloc_dangerous;
		if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
			ret = obj_reloc_overflow;
		*loc =
		    (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
					    0x03ffffff);
		break;

	case R_MIPS_HI16:
		{
			struct mips_hi16 *n;

			/* We cannot relocate this one now because we don't know the value
			   of the carry we need to add.  Save the information, and let LO16
			   do the actual relocation.  */
			n = (struct mips_hi16 *) xmalloc(sizeof *n);
			n->addr = loc;
			n->value = v;
			n->next = ifile->mips_hi16_list;
			ifile->mips_hi16_list = n;
	       		break;
		}

	case R_MIPS_LO16:
		{
			unsigned long insnlo = *loc;
			Elf32_Addr val, vallo;

			/* Sign extend the addend we extract from the lo insn.  */
			vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;

			if (ifile->mips_hi16_list != NULL) {
				struct mips_hi16 *l;

				l = ifile->mips_hi16_list;
				while (l != NULL) {
					struct mips_hi16 *next;
					unsigned long insn;

					/* The value for the HI16 had best be the same. */
					assert(v == l->value);

					/* Do the HI16 relocation.  Note that we actually don't
					   need to know anything about the LO16 itself, except where
					   to find the low 16 bits of the addend needed by the LO16.  */
					insn = *l->addr;
					val =
					    ((insn & 0xffff) << 16) +
					    vallo;
					val += v;

					/* Account for the sign extension that will happen in the
					   low bits.  */
					val =
					    ((val >> 16) +
					     ((val & 0x8000) !=
					      0)) & 0xffff;

					insn = (insn & ~0xffff) | val;
					*l->addr = insn;

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

				ifile->mips_hi16_list = NULL;
			}

			/* Ok, we're done with the HI16 relocs.  Now deal with the LO16.  */
			val = v + vallo;
			insnlo = (insnlo & ~0xffff) | (val & 0xffff);
			*loc = insnlo;
			break;
		}
#endif

#if defined(__arm__)
#elif defined(__sh__)
        case R_SH_REL32:
		*loc += v - dot;
		break;
#elif defined(__i386__)
	case R_386_PLT32:
	case R_386_PC32:
		*loc += v - dot;
		break;
#elif defined(__powerpc__)
	case R_PPC_REL32:
		*loc = v - dot;
		break;
#endif

#if defined(__sh__)
        case R_SH_PLT32:
                *loc = v - dot;
                break;
#elif defined(__i386__)
#endif

#if defined(BB_USE_PLT_ENTRIES)

#if defined(__arm__)
    case R_ARM_PC24:
    case R_ARM_PLT32:
#endif
#if defined(__powerpc__)
	case R_PPC_REL24:
#endif
      /* find the plt entry and initialize it if necessary */
      assert(isym != NULL);

      pe = (struct arch_plt_entry*) &isym->pltent;

      if (! pe->inited) {
	  	ip = (unsigned long *) (ifile->plt->contents + pe->offset);

		/* generate some machine code */

#if defined(__arm__)
	  	ip[0] = 0xe51ff004;			/* ldr pc,[pc,#-4] */
	  	ip[1] = v;				/* sym@ */
#endif
#if defined(__powerpc__)
	  ip[0] = 0x3d600000 + ((v + 0x8000) >> 16);  /* lis r11,sym@ha */
	  ip[1] = 0x396b0000 + (v & 0xffff);	      /* addi r11,r11,sym@l */
	  ip[2] = 0x7d6903a6;			      /* mtctr r11 */
	  ip[3] = 0x4e800420;			      /* bctr */
#endif
	  	pe->inited = 1;
	  }

      /* relative distance to target */
      v -= dot;
      /* if the target is too far away.... */
      if ((int)v < -0x02000000 || (int)v >= 0x02000000) {
	    /* go via the plt */
	    v = plt + pe->offset - dot;
	  }
      if (v & 3)
	    ret = obj_reloc_dangerous;

      /* merge the offset into the instruction. */
#if defined(__arm__)
      /* Convert to words. */
      v >>= 2;

      *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
#endif
#if defined(__powerpc__)
      *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
#endif
      break;
#endif /* BB_USE_PLT_ENTRIES */

#if defined(__arm__)
#elif defined(__sh__)
        case R_SH_GLOB_DAT:
        case R_SH_JMP_SLOT:
               	*loc = v;
                break;
#elif defined(__i386__)
	case R_386_GLOB_DAT:
	case R_386_JMP_SLOT:
		*loc = v;
		break;
#endif

#if defined(__arm__)
#elif defined(__sh__)
        case R_SH_RELATIVE:
	        *loc += f->baseaddr + rel->r_addend;
                break;
#elif defined(__i386__)
        case R_386_RELATIVE:
		*loc += f->baseaddr;
		break;
#endif

#if defined(BB_USE_GOT_ENTRIES)

#if defined(__sh__)
        case R_SH_GOTPC:
#elif defined(__arm__)
    case R_ARM_GOTPC:
#elif defined(__i386__)
	case R_386_GOTPC:
#endif
		assert(got != 0);
#if defined(__sh__)
		*loc += got - dot + rel->r_addend;;
#elif defined(__i386__) || defined(__arm__)
		*loc += got - dot;
#endif
		break;

#if defined(__sh__)
	case R_SH_GOT32:
#elif defined(__arm__)
	case R_ARM_GOT32:
#elif defined(__i386__)
	case R_386_GOT32:
#endif
		assert(isym != NULL);
        /* needs an entry in the .got: set it, once */
		if (!isym->gotent.reloc_done) {
			isym->gotent.reloc_done = 1;
			*(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
		}
        /* make the reloc with_respect_to_.got */
#if defined(__sh__)
		*loc += isym->gotent.offset + rel->r_addend;
#elif defined(__i386__) || defined(__arm__)
		*loc += isym->gotent.offset;
#endif
		break;

    /* address relative to the got */
#if defined(__sh__)
	case R_SH_GOTOFF:
#elif defined(__arm__)
	case R_ARM_GOTOFF:
#elif defined(__i386__)
	case R_386_GOTOFF:
#endif
		assert(got != 0);
		*loc += v - got;
		break;

#endif /* BB_USE_GOT_ENTRIES */

	default:
        printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
		ret = obj_reloc_unhandled;
		break;
	}

	return ret;
}

int arch_create_got(struct obj_file *f)
{
#if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
	struct arch_file *ifile = (struct arch_file *) f;
	int i;
#if defined(BB_USE_GOT_ENTRIES)
	int got_offset = 0, gotneeded = 0;
#endif
#if defined(BB_USE_PLT_ENTRIES)
	int plt_offset = 0, pltneeded = 0;
#endif
    struct obj_section *relsec, *symsec, *strsec;
	ElfW(RelM) *rel, *relend;
	ElfW(Sym) *symtab, *extsym;
	const char *strtab, *name;
	struct arch_symbol *intsym;

	for (i = 0; i < f->header.e_shnum; ++i) {
		relsec = f->sections[i];
		if (relsec->header.sh_type != SHT_RELM)
			continue;

		symsec = f->sections[relsec->header.sh_link];
		strsec = f->sections[symsec->header.sh_link];

		rel = (ElfW(RelM) *) relsec->contents;
		relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
		symtab = (ElfW(Sym) *) symsec->contents;
		strtab = (const char *) strsec->contents;

		for (; rel < relend; ++rel) {
			extsym = &symtab[ELF32_R_SYM(rel->r_info)];

			switch (ELF32_R_TYPE(rel->r_info)) {
#if defined(__arm__)
			case R_ARM_GOT32:
				break;
#elif defined(__sh__)
			case R_SH_GOT32:
				break;
#elif defined(__i386__)
			case R_386_GOT32:
				break;
#endif

#if defined(__powerpc__)
			case R_PPC_REL24:
				pltneeded = 1;
				break;
#endif

#if defined(__arm__)
			case R_ARM_PC24:
			case R_ARM_PLT32:
				pltneeded = 1;
				break;

			case R_ARM_GOTPC:
			case R_ARM_GOTOFF:
				gotneeded = 1;
				if (got_offset == 0)
					got_offset = 4;
#elif defined(__sh__)
			case R_SH_GOTPC:
			case R_SH_GOTOFF:
				gotneeded = 1;
#elif defined(__i386__)
			case R_386_GOTPC:
			case R_386_GOTOFF:
				gotneeded = 1;
#endif

			default:
				continue;
			}

			if (extsym->st_name != 0) {
				name = strtab + extsym->st_name;
			} else {
				name = f->sections[extsym->st_shndx]->name;
			}
			intsym = (struct arch_symbol *) obj_find_symbol(f, name);
#if defined(BB_USE_GOT_ENTRIES)
			if (!intsym->gotent.offset_done) {
				intsym->gotent.offset_done = 1;
				intsym->gotent.offset = got_offset;
				got_offset += BB_GOT_ENTRY_SIZE;
			}
#endif
#if defined(BB_USE_PLT_ENTRIES)
			if (pltneeded && intsym->pltent.allocated == 0) {
				intsym->pltent.allocated = 1;
				intsym->pltent.offset = plt_offset;
				plt_offset += BB_PLT_ENTRY_SIZE;
				intsym->pltent.inited = 0;
				pltneeded = 0;
			}
#endif
			}
		}

#if defined(BB_USE_GOT_ENTRIES)
	if (got_offset) {
		struct obj_section* myrelsec = obj_find_section(f, ".got");

		if (myrelsec) {
			obj_extend_section(myrelsec, got_offset);
		} else {
			myrelsec = obj_create_alloced_section(f, ".got", 
							    BB_GOT_ENTRY_SIZE,
							    got_offset);
			assert(myrelsec);
		}

		ifile->got = myrelsec;
	}
#endif

#if defined(BB_USE_PLT_ENTRIES)
	if (plt_offset)
		ifile->plt = obj_create_alloced_section(f, ".plt", 
							BB_PLT_ENTRY_SIZE, 
							plt_offset);
#endif
#endif
	return 1;
}

int arch_init_module(struct obj_file *f, struct new_module *mod)
{
	return 1;
}


/*======================================================================*/

/* Standard ELF hash function.  */
inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
{
	unsigned long h = 0;
	unsigned long g;
	unsigned char ch;

	while (n > 0) {
		ch = *name++;
		h = (h << 4) + ch;
		if ((g = (h & 0xf0000000)) != 0) {
			h ^= g >> 24;
			h &= ~g;
		}
		n--;
	}
	return h;
}

unsigned long obj_elf_hash(const char *name)
{
	return obj_elf_hash_n(name, strlen(name));
}

#ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
/* Get the kernel version in the canonical integer form.  */

static int get_kernel_version(char str[STRVERSIONLEN])
{
	struct utsname uts_info;
	char *p, *q;
	int a, b, c;

	if (uname(&uts_info) < 0)
		return -1;
	strncpy(str, uts_info.release, STRVERSIONLEN);
	p = uts_info.release;

	a = strtoul(p, &p, 10);
	if (*p != '.')
		return -1;
	b = strtoul(p + 1, &p, 10);
	if (*p != '.')
		return -1;
	c = strtoul(p + 1, &q, 10);
	if (p + 1 == q)
		return -1;

	return a << 16 | b << 8 | c;
}

/* String comparison for non-co-versioned kernel and module.  */

static int ncv_strcmp(const char *a, const char *b)
{
	size_t alen = strlen(a), blen = strlen(b);

	if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
		return strncmp(a, b, alen);
	else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
		return strncmp(a, b, blen);
	else
		return strcmp(a, b);
}

/* String hashing for non-co-versioned kernel and module.  Here
   we are simply forced to drop the crc from the hash.  */

static unsigned long ncv_symbol_hash(const char *str)
{
	size_t len = strlen(str);
	if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
		len -= 10;
	return obj_elf_hash_n(str, len);
}

void
obj_set_symbol_compare(struct obj_file *f,
					   int (*cmp) (const char *, const char *),
					   unsigned long (*hash) (const char *))
{
	if (cmp)
		f->symbol_cmp = cmp;
	if (hash) {
		struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
		int i;

		f->symbol_hash = hash;

		memcpy(tmptab, f->symtab, sizeof(tmptab));
		memset(f->symtab, 0, sizeof(f->symtab));

		for (i = 0; i < HASH_BUCKETS; ++i)
			for (sym = tmptab[i]; sym; sym = next) {
				unsigned long h = hash(sym->name) % HASH_BUCKETS;
				next = sym->next;
				sym->next = f->symtab[h];
				f->symtab[h] = sym;
			}
	}
}

#endif							/* BB_FEATURE_INSMOD_VERSION_CHECKING */


struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
								  unsigned long symidx, int info,
								  int secidx, ElfW(Addr) value,
								  unsigned long size)
{
	struct obj_symbol *sym;
	unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
	int n_type = ELFW(ST_TYPE) (info);
	int n_binding = ELFW(ST_BIND) (info);

	for (sym = f->symtab[hash]; sym; sym = sym->next)
		if (f->symbol_cmp(sym->name, name) == 0) {
			int o_secidx = sym->secidx;
			int o_info = sym->info;
			int o_type = ELFW(ST_TYPE) (o_info);
			int o_binding = ELFW(ST_BIND) (o_info);

			/* A redefinition!  Is it legal?  */

			if (secidx == SHN_UNDEF)
				return sym;
			else if (o_secidx == SHN_UNDEF)
				goto found;
			else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
				/* Cope with local and global symbols of the same name
				   in the same object file, as might have been created
				   by ld -r.  The only reason locals are now seen at this
				   level at all is so that we can do semi-sensible things
				   with parameters.  */

				struct obj_symbol *nsym, **p;

				nsym = arch_new_symbol();
				nsym->next = sym->next;
				nsym->ksymidx = -1;

				/* Excise the old (local) symbol from the hash chain.  */
				for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
					continue;
				*p = sym = nsym;
				goto found;
			} else if (n_binding == STB_LOCAL) {
				/* Another symbol of the same name has already been defined.
				   Just add this to the local table.  */
				sym = arch_new_symbol();
				sym->next = NULL;
				sym->ksymidx = -1;
				f->local_symtab[symidx] = sym;
				goto found;
			} else if (n_binding == STB_WEAK)
				return sym;
			else if (o_binding == STB_WEAK)
				goto found;
			/* Don't unify COMMON symbols with object types the programmer
			   doesn't expect.  */
			else if (secidx == SHN_COMMON
					 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
				return sym;
			else if (o_secidx == SHN_COMMON
					 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
				goto found;
			else {
				/* Don't report an error if the symbol is coming from
				   the kernel or some external module.  */
				if (secidx <= SHN_HIRESERVE)
					error_msg("%s multiply defined", name);
				return sym;
			}
		}

	/* Completely new symbol.  */
	sym = arch_new_symbol();
	sym->next = f->symtab[hash];
	f->symtab[hash] = sym;
	sym->ksymidx = -1;

	if (ELFW(ST_BIND) (info) == STB_LOCAL)
		f->local_symtab[symidx] = sym;

  found:
	sym->name = name;
	sym->value = value;
	sym->size = size;
	sym->secidx = secidx;
	sym->info = info;

	return sym;
}

struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
{
	struct obj_symbol *sym;
	unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;

	for (sym = f->symtab[hash]; sym; sym = sym->next)
		if (f->symbol_cmp(sym->name, name) == 0)
			return sym;

	return NULL;
}

ElfW(Addr)
	obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
{
	if (sym) {
		if (sym->secidx >= SHN_LORESERVE)
			return sym->value;

		return sym->value + f->sections[sym->secidx]->header.sh_addr;
	} else {
		/* As a special case, a NULL sym has value zero.  */
		return 0;
	}
}

struct obj_section *obj_find_section(struct obj_file *f, const char *name)
{
	int i, n = f->header.e_shnum;

	for (i = 0; i < n; ++i)
		if (strcmp(f->sections[i]->name, name) == 0)
			return f->sections[i];

	return NULL;
}

static int obj_load_order_prio(struct obj_section *a)
{
	unsigned long af, ac;

	af = a->header.sh_flags;

	ac = 0;
	if (a->name[0] != '.' || strlen(a->name) != 10 ||
		strcmp(a->name + 5, ".init"))
		ac |= 32;
	if (af & SHF_ALLOC)
		ac |= 16;
	if (!(af & SHF_WRITE))
		ac |= 8;
	if (af & SHF_EXECINSTR)
		ac |= 4;
	if (a->header.sh_type != SHT_NOBITS)
		ac |= 2;

	return ac;
}

void
obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
{
	struct obj_section **p;
	int prio = obj_load_order_prio(sec);
	for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
		if (obj_load_order_prio(*p) < prio)
			break;
	sec->load_next = *p;
	*p = sec;
}

struct obj_section *obj_create_alloced_section(struct obj_file *f,
											   const char *name,
											   unsigned long align,
											   unsigned long size)
{
	int newidx = f->header.e_shnum++;
	struct obj_section *sec;

	f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
	f->sections[newidx] = sec = arch_new_section();

	memset(sec, 0, sizeof(*sec));
	sec->header.sh_type = SHT_PROGBITS;
	sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
	sec->header.sh_size = size;
	sec->header.sh_addralign = align;
	sec->name = name;
	sec->idx = newidx;
	if (size)
		sec->contents = xmalloc(size);

	obj_insert_section_load_order(f, sec);

	return sec;
}

struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
													 const char *name,
													 unsigned long align,
													 unsigned long size)
{
	int newidx = f->header.e_shnum++;
	struct obj_section *sec;

	f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
	f->sections[newidx] = sec = arch_new_section();

	memset(sec, 0, sizeof(*sec));
	sec->header.sh_type = SHT_PROGBITS;
	sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
	sec->header.sh_size = size;
	sec->header.sh_addralign = align;
	sec->name = name;
	sec->idx = newidx;
	if (size)
		sec->contents = xmalloc(size);

	sec->load_next = f->load_order;
	f->load_order = sec;
	if (f->load_order_search_start == &f->load_order)
		f->load_order_search_start = &sec->load_next;

	return sec;
}

void *obj_extend_section(struct obj_section *sec, unsigned long more)
{
	unsigned long oldsize = sec->header.sh_size;
	sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
	return sec->contents + oldsize;
}



/* Conditionally add the symbols from the given symbol set to the
   new module.  */

static int
add_symbols_from(
				 struct obj_file *f,
				 int idx, struct new_module_symbol *syms, size_t nsyms)
{
	struct new_module_symbol *s;
	size_t i;
	int used = 0;

	for (i = 0, s = syms; i < nsyms; ++i, ++s) {

		/* Only add symbols that are already marked external.  If we
		   override locals we may cause problems for argument initialization.
		   We will also create a false dependency on the module.  */
		struct obj_symbol *sym;

		sym = obj_find_symbol(f, (char *) s->name);
		if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
			sym = obj_add_symbol(f, (char *) s->name, -1,
								 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
								 idx, s->value, 0);
			/* Did our symbol just get installed?  If so, mark the
			   module as "used".  */
			if (sym->secidx == idx)
				used = 1;
		}
	}

	return used;
}

static void add_kernel_symbols(struct obj_file *f)
{
	struct external_module *m;
	int i, nused = 0;

	/* Add module symbols first.  */

	for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
		if (m->nsyms
			&& add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
								m->nsyms)) m->used = 1, ++nused;

	n_ext_modules_used = nused;

	/* And finally the symbols from the kernel proper.  */

	if (nksyms)
		add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
}

static char *get_modinfo_value(struct obj_file *f, const char *key)
{
	struct obj_section *sec;
	char *p, *v, *n, *ep;
	size_t klen = strlen(key);

	sec = obj_find_section(f, ".modinfo");
	if (sec == NULL)
		return NULL;
	p = sec->contents;
	ep = p + sec->header.sh_size;
	while (p < ep) {
		v = strchr(p, '=');
		n = strchr(p, '\0');
		if (v) {
			if (p + klen == v && strncmp(p, key, klen) == 0)
				return v + 1;
		} else {
			if (p + klen == n && strcmp(p, key) == 0)
				return n;
		}
		p = n + 1;
	}

	return NULL;
}


/*======================================================================*/
/* Functions relating to module loading in pre 2.1 kernels.  */

static int
old_process_module_arguments(struct obj_file *f, int argc, char **argv)
{
	while (argc > 0) {
		char *p, *q;
		struct obj_symbol *sym;
		int *loc;

		p = *argv;
		if ((q = strchr(p, '=')) == NULL) {
			argc--;
			continue;
                }
		*q++ = '\0';

		sym = obj_find_symbol(f, p);

		/* Also check that the parameter was not resolved from the kernel.  */
		if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
			error_msg("symbol for parameter %s not found", p);
			return 0;
		}

		loc = (int *) (f->sections[sym->secidx]->contents + sym->value);

		/* Do C quoting if we begin with a ".  */
		if (*q == '"') {
			char *r, *str;

			str = alloca(strlen(q));
			for (r = str, q++; *q != '"'; ++q, ++r) {
				if (*q == '\0') {
					error_msg("improperly terminated string argument for %s", p);
					return 0;
				} else if (*q == '\\')
					switch (*++q) {
					case 'a':
						*r = '\a';
						break;
					case 'b':
						*r = '\b';
						break;
					case 'e':
						*r = '\033';
						break;
					case 'f':
						*r = '\f';
						break;
					case 'n':
						*r = '\n';
						break;
					case 'r':
						*r = '\r';
						break;
					case 't':
						*r = '\t';
						break;

					case '0':
					case '1':
					case '2':
					case '3':
					case '4':
					case '5':
					case '6':
					case '7':
						{
							int c = *q - '0';
							if (q[1] >= '0' && q[1] <= '7') {
								c = (c * 8) + *++q - '0';
								if (q[1] >= '0' && q[1] <= '7')
									c = (c * 8) + *++q - '0';
							}
							*r = c;
						}
						break;

					default:
						*r = *q;
						break;
				} else
					*r = *q;
			}
			*r = '\0';
			obj_string_patch(f, sym->secidx, sym->value, str);
		} else if (*q >= '0' && *q <= '9') {
			do
				*loc++ = strtoul(q, &q, 0);
			while (*q++ == ',');
		} else {
			char *contents = f->sections[sym->secidx]->contents;
			char *myloc = contents + sym->value;
			char *r;			/* To search for commas */

			/* Break the string with comas */
			while ((r = strchr(q, ',')) != (char *) NULL) {
				*r++ = '\0';
				obj_string_patch(f, sym->secidx, myloc - contents, q);
				myloc += sizeof(char *);
				q = r;
			}

			/* last part */
			obj_string_patch(f, sym->secidx, myloc - contents, q);
		}

		argc--, argv++;
	}

	return 1;
}

#ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
static int old_is_module_checksummed(struct obj_file *f)
{
	return obj_find_symbol(f, "Using_Versions") != NULL;
}
/* Get the module's kernel version in the canonical integer form.  */

static int
old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
{
	struct obj_symbol *sym;
	char *p, *q;
	int a, b, c;

	sym = obj_find_symbol(f, "kernel_version");
	if (sym == NULL)
		return -1;

	p = f->sections[sym->secidx]->contents + sym->value;
	strncpy(str, p, STRVERSIONLEN);

	a = strtoul(p, &p, 10);
	if (*p != '.')
		return -1;
	b = strtoul(p + 1, &p, 10);
	if (*p != '.')
		return -1;
	c = strtoul(p + 1, &q, 10);
	if (p + 1 == q)
		return -1;

	return a << 16 | b << 8 | c;
}

#endif   /* BB_FEATURE_INSMOD_VERSION_CHECKING */

#ifdef BB_FEATURE_OLD_MODULE_INTERFACE

/* Fetch all the symbols and divvy them up as appropriate for the modules.  */

static int old_get_kernel_symbols(const char *m_name)
{
	struct old_kernel_sym *ks, *k;
	struct new_module_symbol *s;
	struct external_module *mod;
	int nks, nms, nmod, i;

	nks = get_kernel_syms(NULL);
	if (nks < 0) {
		perror_msg("get_kernel_syms: %s", m_name);
		return 0;
	}

	ks = k = xmalloc(nks * sizeof(*ks));

	if (get_kernel_syms(ks) != nks) {
		perror("inconsistency with get_kernel_syms -- is someone else "
			   "playing with modules?");
		free(ks);
		return 0;
	}

	/* Collect the module information.  */

	mod = NULL;
	nmod = -1;

	while (k->name[0] == '#' && k->name[1]) {
		struct old_kernel_sym *k2;
		struct new_module_symbol *s;

		/* Find out how many symbols this module has.  */
		for (k2 = k + 1; k2->name[0] != '#'; ++k2)
			continue;
		nms = k2 - k - 1;

		mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
		mod[nmod].name = k->name + 1;
		mod[nmod].addr = k->value;
		mod[nmod].used = 0;
		mod[nmod].nsyms = nms;
		mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);

		for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
			s->name = (unsigned long) k->name;
			s->value = k->value;
		}

		k = k2;
	}

	ext_modules = mod;
	n_ext_modules = nmod + 1;

	/* Now collect the symbols for the kernel proper.  */

	if (k->name[0] == '#')
		++k;

	nksyms = nms = nks - (k - ks);
	ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);

	for (i = 0; i < nms; ++i, ++s, ++k) {
		s->name = (unsigned long) k->name;
		s->value = k->value;
	}

	return 1;
}

/* Return the kernel symbol checksum version, or zero if not used.  */

static int old_is_kernel_checksummed(void)
{
	/* Using_Versions is the first symbol.  */
	if (nksyms > 0
		&& strcmp((char *) ksyms[0].name,
				  "Using_Versions") == 0) return ksyms[0].value;
	else
		return 0;
}


static int old_create_mod_use_count(struct obj_file *f)
{
	struct obj_section *sec;

	sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
										   sizeof(long));

	obj_add_symbol(f, "mod_use_count_", -1,
				   ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
				   sizeof(long));

	return 1;
}

static int
old_init_module(const char *m_name, struct obj_file *f,
				unsigned long m_size)
{
	char *image;
	struct old_mod_routines routines;
	struct old_symbol_table *symtab;
	int ret;

	/* Create the symbol table */
	{
		int nsyms = 0, strsize = 0, total;

		/* Size things first... */
		if (flag_export) {
			int i;
			for (i = 0; i < HASH_BUCKETS; ++i) {
				struct obj_symbol *sym;
				for (sym = f->symtab[i]; sym; sym = sym->next)
					if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
						&& sym->secidx <= SHN_HIRESERVE) 
					{
						sym->ksymidx = nsyms++;
						strsize += strlen(sym->name) + 1;
					}
			}
		}

		total = (sizeof(struct old_symbol_table)
				 + nsyms * sizeof(struct old_module_symbol)
				 + n_ext_modules_used * sizeof(struct old_module_ref)
				 + strsize);
		symtab = xmalloc(total);
		symtab->size = total;
		symtab->n_symbols = nsyms;
		symtab->n_refs = n_ext_modules_used;

		if (flag_export && nsyms) {
			struct old_module_symbol *ksym;
			char *str;
			int i;

			ksym = symtab->symbol;
			str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
				   + n_ext_modules_used * sizeof(struct old_module_ref));

			for (i = 0; i < HASH_BUCKETS; ++i) {
				struct obj_symbol *sym;
				for (sym = f->symtab[i]; sym; sym = sym->next)
					if (sym->ksymidx >= 0) {
						ksym->addr = obj_symbol_final_value(f, sym);
						ksym->name =
							(unsigned long) str - (unsigned long) symtab;

						strcpy(str, sym->name);
						str += strlen(sym->name) + 1;
						ksym++;
					}
			}
		}

		if (n_ext_modules_used) {
			struct old_module_ref *ref;
			int i;

			ref = (struct old_module_ref *)
				((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));

			for (i = 0; i < n_ext_modules; ++i)
				if (ext_modules[i].used)
					ref++->module = ext_modules[i].addr;
		}
	}

	/* Fill in routines.  */

	routines.init =
		obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
	routines.cleanup =
		obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));

	/* Whew!  All of the initialization is complete.  Collect the final
	   module image and give it to the kernel.  */

	image = xmalloc(m_size);
	obj_create_image(f, image);

	/* image holds the complete relocated module, accounting correctly for
	   mod_use_count.  However the old module kernel support assume that
	   it is receiving something which does not contain mod_use_count.  */
	ret = old_sys_init_module(m_name, image + sizeof(long),
							  m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
										: 0), &routines, symtab);
	if (ret)
		perror_msg("init_module: %s", m_name);

	free(image);
	free(symtab);

	return ret == 0;
}

#else

#define old_create_mod_use_count(x) TRUE
#define old_init_module(x, y, z) TRUE

#endif							/* BB_FEATURE_OLD_MODULE_INTERFACE */



/*======================================================================*/
/* Functions relating to module loading after 2.1.18.  */

static int
new_process_module_arguments(struct obj_file *f, int argc, char **argv)
{
	while (argc > 0) {
		char *p, *q, *key;
		struct obj_symbol *sym;
		char *contents, *loc;
		int min, max, n;

		p = *argv;
		if ((q = strchr(p, '=')) == NULL) {
			argc--;
			continue;
                }

		key = alloca(q - p + 6);
		memcpy(key, "parm_", 5);
		memcpy(key + 5, p, q - p);
		key[q - p + 5] = 0;

		p = get_modinfo_value(f, key);
		key += 5;
		if (p == NULL) {
			error_msg("invalid parameter %s", key);
			return 0;
		}

		sym = obj_find_symbol(f, key);

		/* Also check that the parameter was not resolved from the kernel.  */
		if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
			error_msg("symbol for parameter %s not found", key);
			return 0;
		}

		if (isdigit(*p)) {
			min = strtoul(p, &p, 10);
			if (*p == '-')
				max = strtoul(p + 1, &p, 10);
			else
				max = min;
		} else
			min = max = 1;

		contents = f->sections[sym->secidx]->contents;
		loc = contents + sym->value;
		n = (*++q != '\0');

		while (1) {
			if ((*p == 's') || (*p == 'c')) {
				char *str;

				/* Do C quoting if we begin with a ", else slurp the lot.  */
				if (*q == '"') {
					char *r;

					str = alloca(strlen(q));
					for (r = str, q++; *q != '"'; ++q, ++r) {
						if (*q == '\0') {
							error_msg("improperly terminated string argument for %s",
									key);
							return 0;
						} else if (*q == '\\')
							switch (*++q) {
							case 'a':
								*r = '\a';
								break;
							case 'b':
								*r = '\b';
								break;
							case 'e':
								*r = '\033';
								break;
							case 'f':
								*r = '\f';
								break;
							case 'n':
								*r = '\n';
								break;
							case 'r':
								*r = '\r';
								break;
							case 't':
								*r = '\t';
								break;

							case '0':
							case '1':
							case '2':
							case '3':
							case '4':
							case '5':
							case '6':
							case '7':
								{
									int c = *q - '0';
									if (q[1] >= '0' && q[1] <= '7') {
										c = (c * 8) + *++q - '0';
										if (q[1] >= '0' && q[1] <= '7')
											c = (c * 8) + *++q - '0';
									}
									*r = c;
								}
								break;

							default:
								*r = *q;
								break;
						} else
							*r = *q;
					}
					*r = '\0';
					++q;
				} else {
					char *r;

					/* In this case, the string is not quoted. We will break
					   it using the coma (like for ints). If the user wants to
					   include comas in a string, he just has to quote it */

					/* Search the next coma */
					r = strchr(q, ',');

					/* Found ? */
					if (r != (char *) NULL) {
						/* Recopy the current field */
						str = alloca(r - q + 1);
						memcpy(str, q, r - q);

						/* I don't know if it is usefull, as the previous case
						   doesn't null terminate the string ??? */
						str[r - q] = '\0';

						/* Keep next fields */
						q = r;
					} else {
						/* last string */
						str = q;
						q = "";
					}
				}

				if (*p == 's') {
					/* Normal string */
					obj_string_patch(f, sym->secidx, loc - contents, str);
					loc += tgt_sizeof_char_p;
				} else {
					/* Array of chars (in fact, matrix !) */
					unsigned long charssize;	/* size of each member */

					/* Get the size of each member */
					/* Probably we should do that outside the loop ? */
					if (!isdigit(*(p + 1))) {
						error_msg("parameter type 'c' for %s must be followed by"
								" the maximum size", key);
						return 0;
					}
					charssize = strtoul(p + 1, (char **) NULL, 10);

					/* Check length */
					if (strlen(str) >= charssize) {
						error_msg("string too long for %s (max %ld)", key,
								charssize - 1);
						return 0;
					}

					/* Copy to location */
					strcpy((char *) loc, str);
					loc += charssize;
				}
			} else {
				long v = strtoul(q, &q, 0);
				switch (*p) {
				case 'b':
					*loc++ = v;
					break;
				case 'h':
					*(short *) loc = v;
					loc += tgt_sizeof_short;
					break;
				case 'i':
					*(int *) loc = v;
					loc += tgt_sizeof_int;
					break;
				case 'l':
					*(long *) loc = v;
					loc += tgt_sizeof_long;
					break;

				default:
					error_msg("unknown parameter type '%c' for %s", *p, key);
					return 0;
				}
			}

		  retry_end_of_value:
			switch (*q) {
			case '\0':
				goto end_of_arg;

			case ' ':
			case '\t':
			case '\n':
			case '\r':
				++q;
				goto retry_end_of_value;

			case ',':
				if (++n > max) {
					error_msg("too many values for %s (max %d)", key, max);
					return 0;
				}
				++q;
				break;

			default:
				error_msg("invalid argument syntax for %s", key);
				return 0;
			}
		}

	  end_of_arg:
		if (n < min) {
			error_msg("too few values for %s (min %d)", key, min);
			return 0;
		}

		argc--, argv++;
	}

	return 1;
}

#ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
static int new_is_module_checksummed(struct obj_file *f)
{
	const char *p = get_modinfo_value(f, "using_checksums");
	if (p)
		return atoi(p);
	else
		return 0;
}

/* Get the module's kernel version in the canonical integer form.  */

static int
new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
{
	char *p, *q;
	int a, b, c;

	p = get_modinfo_value(f, "kernel_version");
	if (p == NULL)
		return -1;
	strncpy(str, p, STRVERSIONLEN);

	a = strtoul(p, &p, 10);
	if (*p != '.')
		return -1;
	b = strtoul(p + 1, &p, 10);
	if (*p != '.')
		return -1;
	c = strtoul(p + 1, &q, 10);
	if (p + 1 == q)
		return -1;

	return a << 16 | b << 8 | c;
}

#endif   /* BB_FEATURE_INSMOD_VERSION_CHECKING */


#ifdef BB_FEATURE_NEW_MODULE_INTERFACE

/* Fetch the loaded modules, and all currently exported symbols.  */

static int new_get_kernel_symbols(void)
{
	char *module_names, *mn;
	struct external_module *modules, *m;
	struct new_module_symbol *syms, *s;
	size_t ret, bufsize, nmod, nsyms, i, j;

	/* Collect the loaded modules.  */

	module_names = xmalloc(bufsize = 256);
  retry_modules_load:
	if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
		if (errno == ENOSPC) {
			module_names = xrealloc(module_names, bufsize = ret);
			goto retry_modules_load;
		}
		perror_msg("QM_MODULES");
		return 0;
	}

	n_ext_modules = nmod = ret;

	/* Collect the modules' symbols.  */

	if (nmod){
		ext_modules = modules = xmalloc(nmod * sizeof(*modules));
		memset(modules, 0, nmod * sizeof(*modules));
		for (i = 0, mn = module_names, m = modules;
			 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
			struct new_module_info info;
	
			if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
				if (errno == ENOENT) {
					/* The module was removed out from underneath us.  */
					continue;
				}
				perror_msg("query_module: QM_INFO: %s", mn);
				return 0;
			}
	
			syms = xmalloc(bufsize = 1024);
		  retry_mod_sym_load:
			if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
				switch (errno) {
				case ENOSPC:
					syms = xrealloc(syms, bufsize = ret);
					goto retry_mod_sym_load;
				case ENOENT:
					/* The module was removed out from underneath us.  */
					continue;
				default:
					perror_msg("query_module: QM_SYMBOLS: %s", mn);
					return 0;
				}
			}
			nsyms = ret;
	
			m->name = mn;
			m->addr = info.addr;
			m->nsyms = nsyms;
			m->syms = syms;
	
			for (j = 0, s = syms; j < nsyms; ++j, ++s) {
				s->name += (unsigned long) syms;
			}
		}
	}

	/* Collect the kernel's symbols.  */

	syms = xmalloc(bufsize = 16 * 1024);
  retry_kern_sym_load:
	if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
		if (errno == ENOSPC) {
			syms = xrealloc(syms, bufsize = ret);
			goto retry_kern_sym_load;
		}
		perror_msg("kernel: QM_SYMBOLS");
		return 0;
	}
	nksyms = nsyms = ret;
	ksyms = syms;

	for (j = 0, s = syms; j < nsyms; ++j, ++s) {
		s->name += (unsigned long) syms;
	}
	return 1;
}


/* Return the kernel symbol checksum version, or zero if not used.  */

static int new_is_kernel_checksummed(void)
{
	struct new_module_symbol *s;
	size_t i;

	/* Using_Versions is not the first symbol, but it should be in there.  */

	for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
		if (strcmp((char *) s->name, "Using_Versions") == 0)
			return s->value;

	return 0;
}


static int new_create_this_module(struct obj_file *f, const char *m_name)
{
	struct obj_section *sec;

	sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
										   sizeof(struct new_module));
	memset(sec->contents, 0, sizeof(struct new_module));

	obj_add_symbol(f, "__this_module", -1,
				   ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
				   sizeof(struct new_module));

	obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
					 m_name);

	return 1;
}


static int new_create_module_ksymtab(struct obj_file *f)
{
	struct obj_section *sec;
	int i;

	/* We must always add the module references.  */

	if (n_ext_modules_used) {
		struct new_module_ref *dep;
		struct obj_symbol *tm;

		sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
										 (sizeof(struct new_module_ref)
										  * n_ext_modules_used));
		if (!sec)
			return 0;

		tm = obj_find_symbol(f, "__this_module");
		dep = (struct new_module_ref *) sec->contents;
		for (i = 0; i < n_ext_modules; ++i)
			if (ext_modules[i].used) {
				dep->dep = ext_modules[i].addr;
				obj_symbol_patch(f, sec->idx,
								 (char *) &dep->ref - sec->contents, tm);
				dep->next_ref = 0;
				++dep;
			}
	}

	if (flag_export && !obj_find_section(f, "__ksymtab")) {
		size_t nsyms;
		int *loaded;

		sec =
			obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
									   0);

		/* We don't want to export symbols residing in sections that
		   aren't loaded.  There are a number of these created so that
		   we make sure certain module options don't appear twice.  */

		loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
		while (--i >= 0)
			loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;

		for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
			struct obj_symbol *sym;
			for (sym = f->symtab[i]; sym; sym = sym->next)
				if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
					&& sym->secidx <= SHN_HIRESERVE
					&& (sym->secidx >= SHN_LORESERVE
						|| loaded[sym->secidx])) {
					ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;

					obj_symbol_patch(f, sec->idx, ofs, sym);
					obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
									 sym->name);

					nsyms++;
				}
		}

		obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
	}

	return 1;
}


static int
new_init_module(const char *m_name, struct obj_file *f,
				unsigned long m_size)
{
	struct new_module *module;
	struct obj_section *sec;
	void *image;
	int ret;
	tgt_long m_addr;

	sec = obj_find_section(f, ".this");
	module = (struct new_module *) sec->contents;
	m_addr = sec->header.sh_addr;

	module->size_of_struct = sizeof(*module);
	module->size = m_size;
	module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;

	sec = obj_find_section(f, "__ksymtab");
	if (sec && sec->header.sh_size) {
		module->syms = sec->header.sh_addr;
		module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
	}

	if (n_ext_modules_used) {
		sec = obj_find_section(f, ".kmodtab");
		module->deps = sec->header.sh_addr;
		module->ndeps = n_ext_modules_used;
	}

	module->init =
		obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
	module->cleanup =
		obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));

	sec = obj_find_section(f, "__ex_table");
	if (sec) {
		module->ex_table_start = sec->header.sh_addr;
		module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
	}

	sec = obj_find_section(f, ".text.init");
	if (sec) {
		module->runsize = sec->header.sh_addr - m_addr;
	}
	sec = obj_find_section(f, ".data.init");
	if (sec) {
		if (!module->runsize ||
			module->runsize > sec->header.sh_addr - m_addr)
				module->runsize = sec->header.sh_addr - m_addr;
	}

	if (!arch_init_module(f, module))
		return 0;

	/* Whew!  All of the initialization is complete.  Collect the final
	   module image and give it to the kernel.  */

	image = xmalloc(m_size);
	obj_create_image(f, image);

	ret = new_sys_init_module(m_name, (struct new_module *) image);
	if (ret)
		perror_msg("init_module: %s", m_name);

	free(image);

	return ret == 0;
}

#else

#define new_init_module(x, y, z) TRUE
#define new_create_this_module(x, y) 0
#define new_create_module_ksymtab(x)
#define query_module(v, w, x, y, z) -1

#endif							/* BB_FEATURE_NEW_MODULE_INTERFACE */


/*======================================================================*/

int
obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
				 const char *string)
{
	struct obj_string_patch *p;
	struct obj_section *strsec;
	size_t len = strlen(string) + 1;
	char *loc;

	p = xmalloc(sizeof(*p));
	p->next = f->string_patches;
	p->reloc_secidx = secidx;
	p->reloc_offset = offset;
	f->string_patches = p;

	strsec = obj_find_section(f, ".kstrtab");
	if (strsec == NULL) {
		strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
		p->string_offset = 0;
		loc = strsec->contents;
	} else {
		p->string_offset = strsec->header.sh_size;
		loc = obj_extend_section(strsec, len);
	}
	memcpy(loc, string, len);

	return 1;
}

int
obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
				 struct obj_symbol *sym)
{
	struct obj_symbol_patch *p;

	p = xmalloc(sizeof(*p));
	p->next = f->symbol_patches;
	p->reloc_secidx = secidx;
	p->reloc_offset = offset;
	p->sym = sym;
	f->symbol_patches = p;

	return 1;
}

int obj_check_undefineds(struct obj_file *f)
{
	unsigned long i;
	int ret = 1;

	for (i = 0; i < HASH_BUCKETS; ++i) {
		struct obj_symbol *sym;
		for (sym = f->symtab[i]; sym; sym = sym->next)
			if (sym->secidx == SHN_UNDEF) {
				if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
					sym->secidx = SHN_ABS;
					sym->value = 0;
				} else {
					error_msg("unresolved symbol %s", sym->name);
					ret = 0;
				}
			}
	}

	return ret;
}

void obj_allocate_commons(struct obj_file *f)
{
	struct common_entry {
		struct common_entry *next;
		struct obj_symbol *sym;
	} *common_head = NULL;

	unsigned long i;

	for (i = 0; i < HASH_BUCKETS; ++i) {
		struct obj_symbol *sym;
		for (sym = f->symtab[i]; sym; sym = sym->next)
			if (sym->secidx == SHN_COMMON) {
				/* Collect all COMMON symbols and sort them by size so as to
				   minimize space wasted by alignment requirements.  */
				{
					struct common_entry **p, *n;
					for (p = &common_head; *p; p = &(*p)->next)
						if (sym->size <= (*p)->sym->size)
							break;

					n = alloca(sizeof(*n));
					n->next = *p;
					n->sym = sym;
					*p = n;
				}
			}
	}

	for (i = 1; i < f->local_symtab_size; ++i) {
		struct obj_symbol *sym = f->local_symtab[i];
		if (sym && sym->secidx == SHN_COMMON) {
			struct common_entry **p, *n;
			for (p = &common_head; *p; p = &(*p)->next)
				if (sym == (*p)->sym)
					break;
				else if (sym->size < (*p)->sym->size) {
					n = alloca(sizeof(*n));
					n->next = *p;
					n->sym = sym;
					*p = n;
					break;
				}
		}
	}

	if (common_head) {
		/* Find the bss section.  */
		for (i = 0; i < f->header.e_shnum; ++i)
			if (f->sections[i]->header.sh_type == SHT_NOBITS)
				break;

		/* If for some reason there hadn't been one, create one.  */
		if (i == f->header.e_shnum) {
			struct obj_section *sec;

			f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
			f->sections[i] = sec = arch_new_section();
			f->header.e_shnum = i + 1;

			memset(sec, 0, sizeof(*sec));
			sec->header.sh_type = SHT_PROGBITS;
			sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
			sec->name = ".bss";
			sec->idx = i;
		}

		/* Allocate the COMMONS.  */
		{
			ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
			ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
			struct common_entry *c;

			for (c = common_head; c; c = c->next) {
				ElfW(Addr) align = c->sym->value;

				if (align > max_align)
					max_align = align;
				if (bss_size & (align - 1))
					bss_size = (bss_size | (align - 1)) + 1;

				c->sym->secidx = i;
				c->sym->value = bss_size;

				bss_size += c->sym->size;
			}

			f->sections[i]->header.sh_size = bss_size;
			f->sections[i]->header.sh_addralign = max_align;
		}
	}

	/* For the sake of patch relocation and parameter initialization,
	   allocate zeroed data for NOBITS sections now.  Note that after
	   this we cannot assume NOBITS are really empty.  */
	for (i = 0; i < f->header.e_shnum; ++i) {
		struct obj_section *s = f->sections[i];
		if (s->header.sh_type == SHT_NOBITS) {
			if (s->header.sh_size != 0)
			s->contents = memset(xmalloc(s->header.sh_size),
								 0, s->header.sh_size);
			else
				s->contents = NULL;

			s->header.sh_type = SHT_PROGBITS;
		}
	}
}

unsigned long obj_load_size(struct obj_file *f)
{
	unsigned long dot = 0;
	struct obj_section *sec;

	/* Finalize the positions of the sections relative to one another.  */

	for (sec = f->load_order; sec; sec = sec->load_next) {
		ElfW(Addr) align;

		align = sec->header.sh_addralign;
		if (align && (dot & (align - 1)))
			dot = (dot | (align - 1)) + 1;

		sec->header.sh_addr = dot;
		dot += sec->header.sh_size;
	}

	return dot;
}

int obj_relocate(struct obj_file *f, ElfW(Addr) base)
{
	int i, n = f->header.e_shnum;
	int ret = 1;

	/* Finalize the addresses of the sections.  */

	f->baseaddr = base;
	for (i = 0; i < n; ++i)
		f->sections[i]->header.sh_addr += base;

	/* And iterate over all of the relocations.  */

	for (i = 0; i < n; ++i) {
		struct obj_section *relsec, *symsec, *targsec, *strsec;
		ElfW(RelM) * rel, *relend;
		ElfW(Sym) * symtab;
		const char *strtab;

		relsec = f->sections[i];
		if (relsec->header.sh_type != SHT_RELM)
			continue;

		symsec = f->sections[relsec->header.sh_link];
		targsec = f->sections[relsec->header.sh_info];
		strsec = f->sections[symsec->header.sh_link];

		rel = (ElfW(RelM) *) relsec->contents;
		relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
		symtab = (ElfW(Sym) *) symsec->contents;
		strtab = (const char *) strsec->contents;

		for (; rel < relend; ++rel) {
			ElfW(Addr) value = 0;
			struct obj_symbol *intsym = NULL;
			unsigned long symndx;
			ElfW(Sym) * extsym = 0;
			const char *errmsg;

			/* Attempt to find a value to use for this relocation.  */

			symndx = ELFW(R_SYM) (rel->r_info);
			if (symndx) {
				/* Note we've already checked for undefined symbols.  */

				extsym = &symtab[symndx];
				if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
					/* Local symbols we look up in the local table to be sure
					   we get the one that is really intended.  */
					intsym = f->local_symtab[symndx];
				} else {
					/* Others we look up in the hash table.  */
					const char *name;
					if (extsym->st_name)
						name = strtab + extsym->st_name;
					else
						name = f->sections[extsym->st_shndx]->name;
					intsym = obj_find_symbol(f, name);
				}

				value = obj_symbol_final_value(f, intsym);
				intsym->referenced = 1;
			}
#if SHT_RELM == SHT_RELA
#if defined(__alpha__) && defined(AXP_BROKEN_GAS)
			/* Work around a nasty GAS bug, that is fixed as of 2.7.0.9.  */
			if (!extsym || !extsym->st_name ||
				ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
#endif
				value += rel->r_addend;
#endif

			/* Do it! */
			switch (arch_apply_relocation
					(f, targsec, symsec, intsym, rel, value)) {
			case obj_reloc_ok:
				break;

			case obj_reloc_overflow:
				errmsg = "Relocation overflow";
				goto bad_reloc;
			case obj_reloc_dangerous:
				errmsg = "Dangerous relocation";
				goto bad_reloc;
			case obj_reloc_unhandled:
				errmsg = "Unhandled relocation";
			  bad_reloc:
				if (extsym) {
					error_msg("%s of type %ld for %s", errmsg,
							(long) ELFW(R_TYPE) (rel->r_info),
							strtab + extsym->st_name);
				} else {
					error_msg("%s of type %ld", errmsg,
							(long) ELFW(R_TYPE) (rel->r_info));
				}
				ret = 0;
				break;
			}
		}
	}

	/* Finally, take care of the patches.  */

	if (f->string_patches) {
		struct obj_string_patch *p;
		struct obj_section *strsec;
		ElfW(Addr) strsec_base;
		strsec = obj_find_section(f, ".kstrtab");
		strsec_base = strsec->header.sh_addr;

		for (p = f->string_patches; p; p = p->next) {
			struct obj_section *targsec = f->sections[p->reloc_secidx];
			*(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
				= strsec_base + p->string_offset;
		}
	}

	if (f->symbol_patches) {
		struct obj_symbol_patch *p;

		for (p = f->symbol_patches; p; p = p->next) {
			struct obj_section *targsec = f->sections[p->reloc_secidx];
			*(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
				= obj_symbol_final_value(f, p->sym);
		}
	}

	return ret;
}

int obj_create_image(struct obj_file *f, char *image)
{
	struct obj_section *sec;
	ElfW(Addr) base = f->baseaddr;

	for (sec = f->load_order; sec; sec = sec->load_next) {
		char *secimg;

		if (sec->contents == 0 || sec->header.sh_size == 0)
			continue;

		secimg = image + (sec->header.sh_addr - base);

		/* Note that we allocated data for NOBITS sections earlier.  */
		memcpy(secimg, sec->contents, sec->header.sh_size);
	}

	return 1;
}

/*======================================================================*/

struct obj_file *obj_load(FILE * fp)
{
	struct obj_file *f;
	ElfW(Shdr) * section_headers;
	int shnum, i;
	char *shstrtab;

	/* Read the file header.  */

	f = arch_new_file();
	memset(f, 0, sizeof(*f));
	f->symbol_cmp = strcmp;
	f->symbol_hash = obj_elf_hash;
	f->load_order_search_start = &f->load_order;

	fseek(fp, 0, SEEK_SET);
	if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
		perror_msg("error reading ELF header");
		return NULL;
	}

	if (f->header.e_ident[EI_MAG0] != ELFMAG0
		|| f->header.e_ident[EI_MAG1] != ELFMAG1
		|| f->header.e_ident[EI_MAG2] != ELFMAG2
		|| f->header.e_ident[EI_MAG3] != ELFMAG3) {
		error_msg("not an ELF file");
		return NULL;
	}
	if (f->header.e_ident[EI_CLASS] != ELFCLASSM
		|| f->header.e_ident[EI_DATA] != ELFDATAM
		|| f->header.e_ident[EI_VERSION] != EV_CURRENT
		|| !MATCH_MACHINE(f->header.e_machine)) {
		error_msg("ELF file not for this architecture");
		return NULL;
	}
	if (f->header.e_type != ET_REL) {
		error_msg("ELF file not a relocatable object");
		return NULL;
	}

	/* Read the section headers.  */

	if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
		error_msg("section header size mismatch: %lu != %lu",
				(unsigned long) f->header.e_shentsize,
				(unsigned long) sizeof(ElfW(Shdr)));
		return NULL;
	}

	shnum = f->header.e_shnum;
	f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
	memset(f->sections, 0, sizeof(struct obj_section *) * shnum);

	section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
	fseek(fp, f->header.e_shoff, SEEK_SET);
	if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
		perror_msg("error reading ELF section headers");
		return NULL;
	}

	/* Read the section data.  */

	for (i = 0; i < shnum; ++i) {
		struct obj_section *sec;

		f->sections[i] = sec = arch_new_section();
		memset(sec, 0, sizeof(*sec));

		sec->header = section_headers[i];
		sec->idx = i;

		if(sec->header.sh_size) switch (sec->header.sh_type) {
		case SHT_NULL:
		case SHT_NOTE:
		case SHT_NOBITS:
			/* ignore */
			break;

		case SHT_PROGBITS:
		case SHT_SYMTAB:
		case SHT_STRTAB:
		case SHT_RELM:
			if (sec->header.sh_size > 0) {
				sec->contents = xmalloc(sec->header.sh_size);
				fseek(fp, sec->header.sh_offset, SEEK_SET);
				if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
					perror_msg("error reading ELF section data");
					return NULL;
				}
			} else {
				sec->contents = NULL;
			}
			break;

#if SHT_RELM == SHT_REL
		case SHT_RELA:
			error_msg("RELA relocations not supported on this architecture");
			return NULL;
#else
		case SHT_REL:
			error_msg("REL relocations not supported on this architecture");
			return NULL;
#endif

		default:
			if (sec->header.sh_type >= SHT_LOPROC) {
				/* Assume processor specific section types are debug
				   info and can safely be ignored.  If this is ever not
				   the case (Hello MIPS?), don't put ifdefs here but
				   create an arch_load_proc_section().  */
				break;
			}

			error_msg("can't handle sections of type %ld",
					(long) sec->header.sh_type);
			return NULL;
		}
	}

	/* Do what sort of interpretation as needed by each section.  */

	shstrtab = f->sections[f->header.e_shstrndx]->contents;

	for (i = 0; i < shnum; ++i) {
		struct obj_section *sec = f->sections[i];
		sec->name = shstrtab + sec->header.sh_name;
	}

	for (i = 0; i < shnum; ++i) {
		struct obj_section *sec = f->sections[i];

		if (sec->header.sh_flags & SHF_ALLOC)
			obj_insert_section_load_order(f, sec);

		switch (sec->header.sh_type) {
		case SHT_SYMTAB:
			{
				unsigned long nsym, j;
				char *strtab;
				ElfW(Sym) * sym;

				if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
					error_msg("symbol size mismatch: %lu != %lu",
							(unsigned long) sec->header.sh_entsize,
							(unsigned long) sizeof(ElfW(Sym)));
					return NULL;
				}

				nsym = sec->header.sh_size / sizeof(ElfW(Sym));
				strtab = f->sections[sec->header.sh_link]->contents;
				sym = (ElfW(Sym) *) sec->contents;

				/* Allocate space for a table of local symbols.  */
				j = f->local_symtab_size = sec->header.sh_info;
				f->local_symtab = xmalloc(j *=
										  sizeof(struct obj_symbol *));
				memset(f->local_symtab, 0, j);

				/* Insert all symbols into the hash table.  */
				for (j = 1, ++sym; j < nsym; ++j, ++sym) {
					const char *name;
					if (sym->st_name)
						name = strtab + sym->st_name;
		else
						name = f->sections[sym->st_shndx]->name;

					obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
								   sym->st_value, sym->st_size);
		}
	}
			break;

		case SHT_RELM:
			if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
				error_msg("relocation entry size mismatch: %lu != %lu",
						(unsigned long) sec->header.sh_entsize,
						(unsigned long) sizeof(ElfW(RelM)));
				return NULL;
			}
			break;
		}
	}

	return f;
}

static void hide_special_symbols(struct obj_file *f)
{
	static const char *const specials[] = {
		"cleanup_module",
		"init_module",
		"kernel_version",
		NULL
	};

	struct obj_symbol *sym;
	const char *const *p;

	for (p = specials; *p; ++p)
		if ((sym = obj_find_symbol(f, *p)) != NULL)
			sym->info =
				ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
}



extern int insmod_main( int argc, char **argv)
{
	int opt;
	int k_crcs;
	int k_new_syscalls;
	int len;
	char *tmp;
	unsigned long m_size;
	ElfW(Addr) m_addr;
	FILE *fp;
	struct obj_file *f;
	struct stat st;
	char m_name[BUFSIZ + 1] = "\0";
	int exit_status = EXIT_FAILURE;
	int m_has_modinfo;
#ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
	int k_version;
	char k_strversion[STRVERSIONLEN];
	char m_strversion[STRVERSIONLEN];
	int m_version;
	int m_crcs;
#endif

	/* Parse any options */
	while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
		switch (opt) {
			case 'f':			/* force loading */
				flag_force_load = 1;
				break;
			case 'k':			/* module loaded by kerneld, auto-cleanable */
				flag_autoclean = 1;
				break;
			case 'v':			/* verbose output */
				flag_verbose = 1;
				break;
			case 'x':			/* do not export externs */
				flag_export = 0;
				break;
			case 'o':			/* name the output module */
				strncpy(m_name, optarg, BUFSIZ);
				break;
			case 'L':			/* Stub warning */
				/* This is needed for compatibility with modprobe.
				 * In theory, this does locking, but we don't do
				 * that.  So be careful and plan your life around not
				 * loading the same module 50 times concurrently. */
				break;
			default:
				show_usage();
		}
	}
	
	if (argv[optind] == NULL) {
		show_usage();
	}

	/* Grab the module name */
	if ((tmp = strrchr(argv[optind], '/')) != NULL) {
		tmp++;
	} else {
		tmp = argv[optind];
	}
	len = strlen(tmp);

	if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
		len -= 2;
	strncpy(m_fullName, tmp, len);
	if (*m_name == '\0') {
		strcpy(m_name, m_fullName);
	}
	strcat(m_fullName, ".o");

	/* Get a filedesc for the module */
	if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
			(fp = fopen(argv[optind], "r")) == NULL) {
		/* Hmpf.  Could not open it. Search through _PATH_MODULES to find a module named m_name */
		if (recursive_action(_PATH_MODULES, TRUE, FALSE, FALSE,
							findNamedModule, 0, m_fullName) == FALSE) 
		{
			if (m_filename[0] == '\0'
				|| ((fp = fopen(m_filename, "r")) == NULL)) 
			{
				error_msg("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
				return EXIT_FAILURE;
			}
		} else
			error_msg_and_die("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
	} else
		memcpy(m_filename, argv[optind], strlen(argv[optind]));


	if ((f = obj_load(fp)) == NULL)
		perror_msg_and_die("Could not load the module");

	if (get_modinfo_value(f, "kernel_version") == NULL)
		m_has_modinfo = 0;
	else
		m_has_modinfo = 1;

#ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
	/* Version correspondence?  */

	k_version = get_kernel_version(k_strversion);
	if (m_has_modinfo) {
		m_version = new_get_module_version(f, m_strversion);
	} else {
		m_version = old_get_module_version(f, m_strversion);
		if (m_version == -1) {
			error_msg("couldn't find the kernel version the module was "
					"compiled for");
			goto out;
		}
	}

	if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
		if (flag_force_load) {
			error_msg("Warning: kernel-module version mismatch\n"
					"\t%s was compiled for kernel version %s\n"
					"\twhile this kernel is version %s",
					m_filename, m_strversion, k_strversion);
		} else {
			error_msg("kernel-module version mismatch\n"
					"\t%s was compiled for kernel version %s\n"
					"\twhile this kernel is version %s.",
					m_filename, m_strversion, k_strversion);
			goto out;
		}
	}
	k_crcs = 0;
#endif							/* BB_FEATURE_INSMOD_VERSION_CHECKING */

	k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);

	if (k_new_syscalls) {
#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
		if (!new_get_kernel_symbols())
			goto out;
		k_crcs = new_is_kernel_checksummed();
#else
		error_msg("Not configured to support new kernels");
		goto out;
#endif
	} else {
#ifdef BB_FEATURE_OLD_MODULE_INTERFACE
		if (!old_get_kernel_symbols(m_name))
			goto out;
		k_crcs = old_is_kernel_checksummed();
#else
		error_msg("Not configured to support old kernels");
		goto out;
#endif
	}

#ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
	if (m_has_modinfo)
		m_crcs = new_is_module_checksummed(f);
	else
		m_crcs = old_is_module_checksummed(f);

	if (m_crcs != k_crcs)
		obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
#endif							/* BB_FEATURE_INSMOD_VERSION_CHECKING */

	/* Let the module know about the kernel symbols.  */
	add_kernel_symbols(f);

	/* Allocate common symbols, symbol tables, and string tables.  */

	if (k_new_syscalls 
		? !new_create_this_module(f, m_name)
		: !old_create_mod_use_count(f)) 
	{
		goto out;
	}

	if (!obj_check_undefineds(f)) {
		goto out;
	}
	obj_allocate_commons(f);

	/* done with the module name, on to the optional var=value arguments */
	++optind;

	if (optind < argc) {
		if (m_has_modinfo
			? !new_process_module_arguments(f, argc - optind, argv + optind) 
			: !old_process_module_arguments(f, argc - optind, argv + optind)) 
		{
			goto out;
		}
	}

	arch_create_got(f);
	hide_special_symbols(f);

	if (k_new_syscalls)
		new_create_module_ksymtab(f);

	/* Find current size of the module */
	m_size = obj_load_size(f);


	m_addr = create_module(m_name, m_size);
	if (m_addr==-1) switch (errno) {
	case EEXIST:
		error_msg("A module named %s already exists", m_name);
		goto out;
	case ENOMEM:
		error_msg("Can't allocate kernel memory for module; needed %lu bytes",
				m_size);
		goto out;
	default:
		perror_msg("create_module: %s", m_name);
		goto out;
	}

	if (!obj_relocate(f, m_addr)) {
		delete_module(m_name);
		goto out;
	}

	if (k_new_syscalls 
		? !new_init_module(m_name, f, m_size)
		: !old_init_module(m_name, f, m_size)) 
	{
		delete_module(m_name);
		goto out;
	}

	exit_status = EXIT_SUCCESS;

out:
	fclose(fp);
	return(exit_status);
}