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
|
/* $Id: kLdrModLX.c 117 2020-03-15 15:23:36Z bird $ */
/** @file
* kLdr - The Module Interpreter for the Linear eXecutable (LX) Format.
*/
/*
* Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <k/kLdr.h>
#include "kLdrInternal.h"
#include <k/kLdrFmts/lx.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @def KLDRMODLX_STRICT
* Define KLDRMODLX_STRICT to enabled strict checks in KLDRMODLX. */
#define KLDRMODLX_STRICT 1
/** @def KLDRMODLX_ASSERT
* Assert that an expression is true when KLDR_STRICT is defined.
*/
#ifdef KLDRMODLX_STRICT
# define KLDRMODLX_ASSERT(expr) kHlpAssert(expr)
#else
# define KLDRMODLX_ASSERT(expr) do {} while (0)
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Instance data for the LX module interpreter.
*/
typedef struct KLDRMODLX
{
/** Pointer to the module. (Follows the section table.) */
PKLDRMOD pMod;
/** Pointer to the user mapping. */
const void *pvMapping;
/** The size of the mapped LX image. */
KSIZE cbMapped;
/** Reserved flags. */
KU32 f32Reserved;
/** The offset of the LX header. */
KLDRFOFF offHdr;
/** Copy of the LX header. */
struct e32_exe Hdr;
/** Pointer to the loader section.
* Allocated together with this strcture. */
const KU8 *pbLoaderSection;
/** Pointer to the last byte in the loader section. */
const KU8 *pbLoaderSectionLast;
/** Pointer to the object table in the loader section. */
const struct o32_obj *paObjs;
/** Pointer to the object page map table in the loader section. */
const struct o32_map *paPageMappings;
/** Pointer to the resource table in the loader section. */
const struct rsrc32 *paRsrcs;
/** Pointer to the resident name table in the loader section. */
const KU8 *pbResNameTab;
/** Pointer to the entry table in the loader section. */
const KU8 *pbEntryTab;
/** Pointer to the non-resident name table. */
KU8 *pbNonResNameTab;
/** Pointer to the last byte in the non-resident name table. */
const KU8 *pbNonResNameTabLast;
/** Pointer to the fixup section. */
KU8 *pbFixupSection;
/** Pointer to the last byte in the fixup section. */
const KU8 *pbFixupSectionLast;
/** Pointer to the fixup page table within pvFixupSection. */
const KU32 *paoffPageFixups;
/** Pointer to the fixup record table within pvFixupSection. */
const KU8 *pbFixupRecs;
/** Pointer to the import module name table within pvFixupSection. */
const KU8 *pbImportMods;
/** Pointer to the import module name table within pvFixupSection. */
const KU8 *pbImportProcs;
} KLDRMODLX, *PKLDRMODLX;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int kldrModLXHasDbgInfo(PKLDRMOD pMod, const void *pvBits);
static int kldrModLXRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
static int kldrModLXDoCreate(PKRDR pRdr, KLDRFOFF offNewHdr, PKLDRMODLX *ppModLX);
static const KU8 *kldrModLXDoNameTableLookupByOrdinal(const KU8 *pbNameTable, KSSIZE cbNameTable, KU32 iOrdinal);
static int kldrModLXDoNameLookup(PKLDRMODLX pModLX, const char *pchSymbol, KSIZE cchSymbol, KU32 *piSymbol);
static const KU8 *kldrModLXDoNameTableLookupByName(const KU8 *pbNameTable, KSSIZE cbNameTable,
const char *pchSymbol, KSIZE cchSymbol);
static int kldrModLXDoLoadBits(PKLDRMODLX pModLX, void *pvBits);
static int kldrModLXDoIterDataUnpacking(KU8 *pbDst, const KU8 *pbSrc, int cbSrc);
static int kldrModLXDoIterData2Unpacking(KU8 *pbDst, const KU8 *pbSrc, int cbSrc);
static void kLdrModLXMemCopyW(KU8 *pbDst, const KU8 *pbSrc, int cb);
static int kldrModLXDoProtect(PKLDRMODLX pModLX, void *pvBits, unsigned fUnprotectOrProtect);
static int kldrModLXDoCallDLL(PKLDRMODLX pModLX, void *pvMapping, unsigned uOp, KUPTR uHandle);
static int kldrModLXDoForwarderQuery(PKLDRMODLX pModLX, const struct e32_entry *pEntry,
PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind);
static int kldrModLXDoLoadFixupSection(PKLDRMODLX pModLX);
static int kldrModLXDoReloc(KU8 *pbPage, int off, KLDRADDR PageAddress, const struct r32_rlc *prlc,
int iSelector, KLDRADDR uValue, KU32 fKind);
/**
* Create a loader module instance interpreting the executable image found
* in the specified file provider instance.
*
* @returns 0 on success and *ppMod pointing to a module instance.
* On failure, a non-zero OS specific error code is returned.
* @param pOps Pointer to the registered method table.
* @param pRdr The file provider instance to use.
* @param fFlags Flags, MBZ.
* @param enmCpuArch The desired CPU architecture. KCPUARCH_UNKNOWN means
* anything goes, but with a preference for the current
* host architecture.
* @param offNewHdr The offset of the new header in MZ files. -1 if not found.
* @param ppMod Where to store the module instance pointer.
*/
static int kldrModLXCreate(PCKLDRMODOPS pOps, PKRDR pRdr, KU32 fFlags, KCPUARCH enmCpuArch, KLDRFOFF offNewHdr, PPKLDRMOD ppMod)
{
PKLDRMODLX pModLX;
int rc;
K_NOREF(fFlags);
/*
* Create the instance data and do a minimal header validation.
*/
rc = kldrModLXDoCreate(pRdr, offNewHdr, &pModLX);
if (!rc)
{
/*
* Match up against the requested CPU architecture.
*/
if ( enmCpuArch == KCPUARCH_UNKNOWN
|| pModLX->pMod->enmArch == enmCpuArch)
{
pModLX->pMod->pOps = pOps;
pModLX->pMod->u32Magic = KLDRMOD_MAGIC;
*ppMod = pModLX->pMod;
return 0;
}
rc = KLDR_ERR_CPU_ARCH_MISMATCH;
}
kHlpFree(pModLX);
return rc;
}
/**
* Separate function for reading creating the LX module instance to
* simplify cleanup on failure.
*/
static int kldrModLXDoCreate(PKRDR pRdr, KLDRFOFF offNewHdr, PKLDRMODLX *ppModLX)
{
struct e32_exe Hdr;
PKLDRMODLX pModLX;
PKLDRMOD pMod;
KSIZE cb;
KSIZE cchFilename;
KSIZE offLdrStuff;
KU32 off, offEnd;
KU32 i;
int rc;
int fCanOptimizeMapping;
KU32 NextRVA;
*ppModLX = NULL;
/*
* Read the signature and file header.
*/
rc = kRdrRead(pRdr, &Hdr, sizeof(Hdr), offNewHdr > 0 ? offNewHdr : 0);
if (rc)
return rc;
if ( Hdr.e32_magic[0] != E32MAGIC1
|| Hdr.e32_magic[1] != E32MAGIC2)
return KLDR_ERR_UNKNOWN_FORMAT;
/* We're not interested in anything but x86 images. */
if ( Hdr.e32_level != E32LEVEL
|| Hdr.e32_border != E32LEBO
|| Hdr.e32_worder != E32LEWO
|| Hdr.e32_cpu < E32CPU286
|| Hdr.e32_cpu > E32CPU486
|| Hdr.e32_pagesize != OBJPAGELEN
)
return KLDR_ERR_LX_BAD_HEADER;
/* Some rough sanity checks. */
offEnd = kRdrSize(pRdr) >= (KLDRFOFF)~(KU32)16 ? ~(KU32)16 : (KU32)kRdrSize(pRdr);
if ( Hdr.e32_itermap > offEnd
|| Hdr.e32_datapage > offEnd
|| Hdr.e32_nrestab > offEnd
|| Hdr.e32_nrestab + Hdr.e32_cbnrestab > offEnd
|| Hdr.e32_ldrsize > offEnd - offNewHdr - sizeof(Hdr)
|| Hdr.e32_fixupsize > offEnd - offNewHdr - sizeof(Hdr)
|| Hdr.e32_fixupsize + Hdr.e32_ldrsize > offEnd - offNewHdr - sizeof(Hdr))
return KLDR_ERR_LX_BAD_HEADER;
/* Verify the loader section. */
offEnd = Hdr.e32_objtab + Hdr.e32_ldrsize;
if (Hdr.e32_objtab < sizeof(Hdr))
return KLDR_ERR_LX_BAD_LOADER_SECTION;
off = Hdr.e32_objtab + sizeof(struct o32_obj) * Hdr.e32_objcnt;
if (off > offEnd)
return KLDR_ERR_LX_BAD_LOADER_SECTION;
if ( Hdr.e32_objmap
&& (Hdr.e32_objmap < off || Hdr.e32_objmap > offEnd))
return KLDR_ERR_LX_BAD_LOADER_SECTION;
if ( Hdr.e32_rsrccnt
&& ( Hdr.e32_rsrctab < off
|| Hdr.e32_rsrctab > offEnd
|| Hdr.e32_rsrctab + sizeof(struct rsrc32) * Hdr.e32_rsrccnt > offEnd))
return KLDR_ERR_LX_BAD_LOADER_SECTION;
if ( Hdr.e32_restab
&& (Hdr.e32_restab < off || Hdr.e32_restab > offEnd - 2))
return KLDR_ERR_LX_BAD_LOADER_SECTION;
if ( Hdr.e32_enttab
&& (Hdr.e32_enttab < off || Hdr.e32_enttab >= offEnd))
return KLDR_ERR_LX_BAD_LOADER_SECTION;
if ( Hdr.e32_dircnt
&& (Hdr.e32_dirtab < off || Hdr.e32_dirtab > offEnd - 2))
return KLDR_ERR_LX_BAD_LOADER_SECTION;
/* Verify the fixup section. */
off = offEnd;
offEnd = off + Hdr.e32_fixupsize;
if ( Hdr.e32_fpagetab
&& (Hdr.e32_fpagetab < off || Hdr.e32_fpagetab > offEnd))
{
/*
* wlink mixes the fixup section and the loader section.
*/
off = Hdr.e32_fpagetab;
offEnd = off + Hdr.e32_fixupsize;
Hdr.e32_ldrsize = off - Hdr.e32_objtab;
}
if ( Hdr.e32_frectab
&& (Hdr.e32_frectab < off || Hdr.e32_frectab > offEnd))
return KLDR_ERR_LX_BAD_FIXUP_SECTION;
if ( Hdr.e32_impmod
&& (Hdr.e32_impmod < off || Hdr.e32_impmod > offEnd || Hdr.e32_impmod + Hdr.e32_impmodcnt > offEnd))
return KLDR_ERR_LX_BAD_FIXUP_SECTION;
if ( Hdr.e32_impproc
&& (Hdr.e32_impproc < off || Hdr.e32_impproc > offEnd))
return KLDR_ERR_LX_BAD_FIXUP_SECTION;
/*
* Calc the instance size, allocate and initialize it.
*/
cchFilename = kHlpStrLen(kRdrName(pRdr));
cb = K_ALIGN_Z(sizeof(KLDRMODLX), 8)
+ K_ALIGN_Z(K_OFFSETOF(KLDRMOD, aSegments[Hdr.e32_objcnt + 1]), 8)
+ K_ALIGN_Z(cchFilename + 1, 8);
offLdrStuff = cb;
cb += Hdr.e32_ldrsize + 2; /* +2 for two extra zeros. */
pModLX = (PKLDRMODLX)kHlpAlloc(cb);
if (!pModLX)
return KERR_NO_MEMORY;
*ppModLX = pModLX;
/* KLDRMOD */
pMod = (PKLDRMOD)((KU8 *)pModLX + K_ALIGN_Z(sizeof(KLDRMODLX), 8));
pMod->pvData = pModLX;
pMod->pRdr = pRdr;
pMod->pOps = NULL; /* set upon success. */
pMod->cSegments = Hdr.e32_objcnt;
pMod->cchFilename = (KU32)cchFilename;
pMod->pszFilename = (char *)K_ALIGN_P(&pMod->aSegments[pMod->cSegments], 8);
kHlpMemCopy((char *)pMod->pszFilename, kRdrName(pRdr), cchFilename + 1);
pMod->pszName = NULL; /* finalized further down */
pMod->cchName = 0;
pMod->fFlags = 0;
switch (Hdr.e32_cpu)
{
case E32CPU286:
pMod->enmCpu = KCPU_I80286;
pMod->enmArch = KCPUARCH_X86_16;
break;
case E32CPU386:
pMod->enmCpu = KCPU_I386;
pMod->enmArch = KCPUARCH_X86_32;
break;
case E32CPU486:
pMod->enmCpu = KCPU_I486;
pMod->enmArch = KCPUARCH_X86_32;
break;
}
pMod->enmEndian = KLDRENDIAN_LITTLE;
pMod->enmFmt = KLDRFMT_LX;
switch (Hdr.e32_mflags & E32MODMASK)
{
case E32MODEXE:
pMod->enmType = !(Hdr.e32_mflags & E32NOINTFIX)
? KLDRTYPE_EXECUTABLE_RELOCATABLE
: KLDRTYPE_EXECUTABLE_FIXED;
break;
case E32MODDLL:
case E32PROTDLL:
case E32MODPROTDLL:
pMod->enmType = !(Hdr.e32_mflags & E32SYSDLL)
? KLDRTYPE_SHARED_LIBRARY_RELOCATABLE
: KLDRTYPE_SHARED_LIBRARY_FIXED;
break;
case E32MODPDEV:
case E32MODVDEV:
pMod->enmType = KLDRTYPE_SHARED_LIBRARY_RELOCATABLE;
break;
}
pMod->u32Magic = 0; /* set upon success. */
/* KLDRMODLX */
pModLX->pMod = pMod;
pModLX->pvMapping = 0;
pModLX->cbMapped = 0;
pModLX->f32Reserved = 0;
pModLX->offHdr = offNewHdr >= 0 ? offNewHdr : 0;
kHlpMemCopy(&pModLX->Hdr, &Hdr, sizeof(Hdr));
pModLX->pbLoaderSection = (KU8 *)pModLX + offLdrStuff;
pModLX->pbLoaderSectionLast = pModLX->pbLoaderSection + pModLX->Hdr.e32_ldrsize - 1;
pModLX->paObjs = NULL;
pModLX->paPageMappings = NULL;
pModLX->paRsrcs = NULL;
pModLX->pbResNameTab = NULL;
pModLX->pbEntryTab = NULL;
pModLX->pbNonResNameTab = NULL;
pModLX->pbNonResNameTabLast = NULL;
pModLX->pbFixupSection = NULL;
pModLX->pbFixupSectionLast = NULL;
pModLX->paoffPageFixups = NULL;
pModLX->pbFixupRecs = NULL;
pModLX->pbImportMods = NULL;
pModLX->pbImportProcs = NULL;
/*
* Read the loader data.
*/
rc = kRdrRead(pRdr, (void *)pModLX->pbLoaderSection, pModLX->Hdr.e32_ldrsize, pModLX->Hdr.e32_objtab + pModLX->offHdr);
if (rc)
return rc;
((KU8 *)pModLX->pbLoaderSectionLast)[1] = 0;
((KU8 *)pModLX->pbLoaderSectionLast)[2] = 0;
if (pModLX->Hdr.e32_objcnt)
pModLX->paObjs = (const struct o32_obj *)pModLX->pbLoaderSection;
if (pModLX->Hdr.e32_objmap)
pModLX->paPageMappings = (const struct o32_map *)(pModLX->pbLoaderSection + pModLX->Hdr.e32_objmap - pModLX->Hdr.e32_objtab);
if (pModLX->Hdr.e32_rsrccnt)
pModLX->paRsrcs = (const struct rsrc32 *)(pModLX->pbLoaderSection + pModLX->Hdr.e32_rsrctab - pModLX->Hdr.e32_objtab);
if (pModLX->Hdr.e32_restab)
pModLX->pbResNameTab = pModLX->pbLoaderSection + pModLX->Hdr.e32_restab - pModLX->Hdr.e32_objtab;
if (pModLX->Hdr.e32_enttab)
pModLX->pbEntryTab = pModLX->pbLoaderSection + pModLX->Hdr.e32_enttab - pModLX->Hdr.e32_objtab;
/*
* Get the soname from the resident name table.
* Very convenient that it's the 0 ordinal, because then we get a
* free string terminator.
* (The table entry consists of a pascal string followed by a 16-bit ordinal.)
*/
if (pModLX->pbResNameTab)
pMod->pszName = (const char *)kldrModLXDoNameTableLookupByOrdinal(pModLX->pbResNameTab,
pModLX->pbLoaderSectionLast - pModLX->pbResNameTab + 1,
0);
if (!pMod->pszName)
return KLDR_ERR_LX_NO_SONAME;
pMod->cchName = *(const KU8 *)pMod->pszName++;
if (pMod->cchName != kHlpStrLen(pMod->pszName))
return KLDR_ERR_LX_BAD_SONAME;
/*
* Quick validation of the object table.
*/
cb = 0;
for (i = 0; i < pMod->cSegments; i++)
{
if (pModLX->paObjs[i].o32_base & (OBJPAGELEN - 1))
return KLDR_ERR_LX_BAD_OBJECT_TABLE;
if (pModLX->paObjs[i].o32_base + pModLX->paObjs[i].o32_size <= pModLX->paObjs[i].o32_base)
return KLDR_ERR_LX_BAD_OBJECT_TABLE;
if (pModLX->paObjs[i].o32_mapsize > (pModLX->paObjs[i].o32_size + (OBJPAGELEN - 1)))
return KLDR_ERR_LX_BAD_OBJECT_TABLE;
if ( pModLX->paObjs[i].o32_mapsize
&& ( (KU8 *)&pModLX->paPageMappings[pModLX->paObjs[i].o32_pagemap] > pModLX->pbLoaderSectionLast
|| (KU8 *)&pModLX->paPageMappings[pModLX->paObjs[i].o32_pagemap + pModLX->paObjs[i].o32_mapsize]
> pModLX->pbLoaderSectionLast))
return KLDR_ERR_LX_BAD_OBJECT_TABLE;
if (i > 0 && !(pModLX->paObjs[i].o32_flags & OBJRSRC))
{
if (pModLX->paObjs[i].o32_base <= pModLX->paObjs[i - 1].o32_base)
return KLDR_ERR_LX_BAD_OBJECT_TABLE;
if (pModLX->paObjs[i].o32_base < pModLX->paObjs[i - 1].o32_base + pModLX->paObjs[i - 1].o32_mapsize)
return KLDR_ERR_LX_BAD_OBJECT_TABLE;
}
}
/*
* Check if we can optimize the mapping by using a different
* object alignment. The linker typically uses 64KB alignment,
* we can easily get away with page alignment in most cases.
*/
fCanOptimizeMapping = !(Hdr.e32_mflags & (E32NOINTFIX | E32SYSDLL));
NextRVA = 0;
/*
* Setup the KLDRMOD segment array.
*/
for (i = 0; i < pMod->cSegments; i++)
{
/* unused */
pMod->aSegments[i].pvUser = NULL;
pMod->aSegments[i].MapAddress = 0;
pMod->aSegments[i].pchName = NULL;
pMod->aSegments[i].cchName = 0;
pMod->aSegments[i].offFile = -1;
pMod->aSegments[i].cbFile = -1;
pMod->aSegments[i].SelFlat = 0;
pMod->aSegments[i].Sel16bit = 0;
/* flags */
pMod->aSegments[i].fFlags = 0;
if (pModLX->paObjs[i].o32_flags & OBJBIGDEF)
pMod->aSegments[i].fFlags = KLDRSEG_FLAG_16BIT;
if (pModLX->paObjs[i].o32_flags & OBJALIAS16)
pMod->aSegments[i].fFlags = KLDRSEG_FLAG_OS2_ALIAS16;
if (pModLX->paObjs[i].o32_flags & OBJCONFORM)
pMod->aSegments[i].fFlags = KLDRSEG_FLAG_OS2_CONFORM;
if (pModLX->paObjs[i].o32_flags & OBJIOPL)
pMod->aSegments[i].fFlags = KLDRSEG_FLAG_OS2_IOPL;
/* size and addresses */
pMod->aSegments[i].Alignment = OBJPAGELEN;
pMod->aSegments[i].cb = pModLX->paObjs[i].o32_size;
pMod->aSegments[i].LinkAddress = pModLX->paObjs[i].o32_base;
pMod->aSegments[i].RVA = NextRVA;
if ( fCanOptimizeMapping
|| i + 1 >= pMod->cSegments
|| (pModLX->paObjs[i].o32_flags & OBJRSRC)
|| (pModLX->paObjs[i + 1].o32_flags & OBJRSRC))
pMod->aSegments[i].cbMapped = K_ALIGN_Z(pModLX->paObjs[i].o32_size, OBJPAGELEN);
else
pMod->aSegments[i].cbMapped = pModLX->paObjs[i + 1].o32_base - pModLX->paObjs[i].o32_base;
NextRVA += (KU32)pMod->aSegments[i].cbMapped;
/* protection */
switch ( pModLX->paObjs[i].o32_flags
& (OBJSHARED | OBJREAD | OBJWRITE | OBJEXEC))
{
case 0:
case OBJSHARED:
pMod->aSegments[i].enmProt = KPROT_NOACCESS;
break;
case OBJREAD:
case OBJREAD | OBJSHARED:
pMod->aSegments[i].enmProt = KPROT_READONLY;
break;
case OBJWRITE:
case OBJWRITE | OBJREAD:
pMod->aSegments[i].enmProt = KPROT_WRITECOPY;
break;
case OBJWRITE | OBJSHARED:
case OBJWRITE | OBJSHARED | OBJREAD:
pMod->aSegments[i].enmProt = KPROT_READWRITE;
break;
case OBJEXEC:
case OBJEXEC | OBJSHARED:
pMod->aSegments[i].enmProt = KPROT_EXECUTE;
break;
case OBJEXEC | OBJREAD:
case OBJEXEC | OBJREAD | OBJSHARED:
pMod->aSegments[i].enmProt = KPROT_EXECUTE_READ;
break;
case OBJEXEC | OBJWRITE:
case OBJEXEC | OBJWRITE | OBJREAD:
pMod->aSegments[i].enmProt = KPROT_EXECUTE_WRITECOPY;
break;
case OBJEXEC | OBJWRITE | OBJSHARED:
case OBJEXEC | OBJWRITE | OBJSHARED | OBJREAD:
pMod->aSegments[i].enmProt = KPROT_EXECUTE_READWRITE;
break;
}
if ((pModLX->paObjs[i].o32_flags & (OBJREAD | OBJWRITE | OBJEXEC | OBJRSRC)) == OBJRSRC)
pMod->aSegments[i].enmProt = KPROT_READONLY;
/*pMod->aSegments[i].f16bit = !(pModLX->paObjs[i].o32_flags & OBJBIGDEF)
pMod->aSegments[i].fIOPL = !(pModLX->paObjs[i].o32_flags & OBJIOPL)
pMod->aSegments[i].fConforming = !(pModLX->paObjs[i].o32_flags & OBJCONFORM) */
}
/* set the mapping size */
pModLX->cbMapped = NextRVA;
/*
* We're done.
*/
*ppModLX = pModLX;
return 0;
}
/** @copydoc KLDRMODOPS::pfnDestroy */
static int kldrModLXDestroy(PKLDRMOD pMod)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
int rc = 0;
KLDRMODLX_ASSERT(!pModLX->pvMapping);
if (pMod->pRdr)
{
rc = kRdrClose(pMod->pRdr);
pMod->pRdr = NULL;
}
if (pModLX->pbNonResNameTab)
{
kHlpFree(pModLX->pbNonResNameTab);
pModLX->pbNonResNameTab = NULL;
}
if (pModLX->pbFixupSection)
{
kHlpFree(pModLX->pbFixupSection);
pModLX->pbFixupSection = NULL;
}
pMod->u32Magic = 0;
pMod->pOps = NULL;
kHlpFree(pModLX);
return rc;
}
/**
* Resolved base address aliases.
*
* @param pModLX The interpreter module instance
* @param pBaseAddress The base address, IN & OUT.
*/
static void kldrModLXResolveBaseAddress(PKLDRMODLX pModLX, PKLDRADDR pBaseAddress)
{
if (*pBaseAddress == KLDRMOD_BASEADDRESS_MAP)
*pBaseAddress = pModLX->pMod->aSegments[0].MapAddress;
else if (*pBaseAddress == KLDRMOD_BASEADDRESS_LINK)
*pBaseAddress = pModLX->pMod->aSegments[0].LinkAddress;
}
/** @copydoc kLdrModQuerySymbol */
static int kldrModLXQuerySymbol(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 iSymbol,
const char *pchSymbol, KSIZE cchSymbol, const char *pszVersion,
PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
KU32 iOrdinal;
int rc;
const struct b32_bundle *pBundle;
K_NOREF(pvBits);
K_NOREF(pszVersion);
/*
* Give up at once if there is no entry table.
*/
if (!pModLX->Hdr.e32_enttab)
return KLDR_ERR_SYMBOL_NOT_FOUND;
/*
* Translate the symbol name into an ordinal.
*/
if (pchSymbol)
{
rc = kldrModLXDoNameLookup(pModLX, pchSymbol, cchSymbol, &iSymbol);
if (rc)
return rc;
}
/*
* Iterate the entry table.
* (The entry table is made up of bundles of similar exports.)
*/
iOrdinal = 1;
pBundle = (const struct b32_bundle *)pModLX->pbEntryTab;
while (pBundle->b32_cnt && iOrdinal <= iSymbol)
{
static const KSIZE s_cbEntry[] = { 0, 3, 5, 5, 7 };
/*
* Check for a hit first.
*/
iOrdinal += pBundle->b32_cnt;
if (iSymbol < iOrdinal)
{
KU32 offObject;
const struct e32_entry *pEntry = (const struct e32_entry *)((KUPTR)(pBundle + 1)
+ (iSymbol - (iOrdinal - pBundle->b32_cnt))
* s_cbEntry[pBundle->b32_type]);
/*
* Calculate the return address.
*/
kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
switch (pBundle->b32_type)
{
/* empty bundles are place holders unused ordinal ranges. */
case EMPTY:
return KLDR_ERR_SYMBOL_NOT_FOUND;
/* e32_flags + a 16-bit offset. */
case ENTRY16:
offObject = pEntry->e32_variant.e32_offset.offset16;
if (pfKind)
*pfKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_NO_TYPE;
break;
/* e32_flags + a 16-bit offset + a 16-bit callgate selector. */
case GATE16:
offObject = pEntry->e32_variant.e32_callgate.offset;
if (pfKind)
*pfKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_CODE;
break;
/* e32_flags + a 32-bit offset. */
case ENTRY32:
offObject = pEntry->e32_variant.e32_offset.offset32;
if (pfKind)
*pfKind = KLDRSYMKIND_32BIT;
break;
/* e32_flags + 16-bit import module ordinal + a 32-bit procname or ordinal. */
case ENTRYFWD:
return kldrModLXDoForwarderQuery(pModLX, pEntry, pfnGetForwarder, pvUser, puValue, pfKind);
default:
/* anyone actually using TYPEINFO will end up here. */
KLDRMODLX_ASSERT(!"Bad bundle type");
return KLDR_ERR_LX_BAD_BUNDLE;
}
/*
* Validate the object number and calc the return address.
*/
if ( pBundle->b32_obj <= 0
|| pBundle->b32_obj > pMod->cSegments)
return KLDR_ERR_LX_BAD_BUNDLE;
if (puValue)
*puValue = BaseAddress
+ offObject
+ pMod->aSegments[pBundle->b32_obj - 1].RVA;
return 0;
}
/*
* Skip the bundle.
*/
if (pBundle->b32_type > ENTRYFWD)
{
KLDRMODLX_ASSERT(!"Bad type"); /** @todo figure out TYPEINFO. */
return KLDR_ERR_LX_BAD_BUNDLE;
}
if (pBundle->b32_type == 0)
pBundle = (const struct b32_bundle *)((const KU8 *)pBundle + 2);
else
pBundle = (const struct b32_bundle *)((const KU8 *)(pBundle + 1) + s_cbEntry[pBundle->b32_type] * pBundle->b32_cnt);
}
return KLDR_ERR_SYMBOL_NOT_FOUND;
}
/**
* Do name lookup.
*
* @returns See kLdrModQuerySymbol.
* @param pModLX The module to lookup the symbol in.
* @param pchSymbol The symbol to lookup.
* @param cchSymbol The symbol name length.
* @param piSymbol Where to store the symbol ordinal.
*/
static int kldrModLXDoNameLookup(PKLDRMODLX pModLX, const char *pchSymbol, KSIZE cchSymbol, KU32 *piSymbol)
{
/*
* First do a hash table lookup.
*/
/** @todo hash name table for speed. */
/*
* Search the name tables.
*/
const KU8 *pbName = kldrModLXDoNameTableLookupByName(pModLX->pbResNameTab,
pModLX->pbLoaderSectionLast - pModLX->pbResNameTab + 1,
pchSymbol, cchSymbol);
if (!pbName)
{
if (!pModLX->pbNonResNameTab)
{
/* lazy load it */
/** @todo non-resident name table. */
}
if (pModLX->pbNonResNameTab)
pbName = kldrModLXDoNameTableLookupByName(pModLX->pbResNameTab,
pModLX->pbNonResNameTabLast - pModLX->pbResNameTab + 1,
pchSymbol, cchSymbol);
}
if (!pbName)
return KLDR_ERR_SYMBOL_NOT_FOUND;
*piSymbol = *(const KU16 *)(pbName + 1 + *pbName);
return 0;
}
#if 0
/**
* Hash a symbol using the algorithm from sdbm.
*
* The following was is the documenation of the orignal sdbm functions:
*
* This algorithm was created for sdbm (a public-domain reimplementation of
* ndbm) database library. it was found to do well in scrambling bits,
* causing better distribution of the keys and fewer splits. it also happens
* to be a good general hashing function with good distribution. the actual
* function is hash(i) = hash(i - 1) * 65599 + str[i]; what is included below
* is the faster version used in gawk. [there is even a faster, duff-device
* version] the magic constant 65599 was picked out of thin air while
* experimenting with different constants, and turns out to be a prime.
* this is one of the algorithms used in berkeley db (see sleepycat) and
* elsewhere.
*/
static KU32 kldrModLXDoHash(const char *pchSymbol, KU8 cchSymbol)
{
KU32 hash = 0;
int ch;
while ( cchSymbol-- > 0
&& (ch = *(unsigned const char *)pchSymbol++))
hash = ch + (hash << 6) + (hash << 16) - hash;
return hash;
}
#endif
/**
* Lookup a name table entry by name.
*
* @returns Pointer to the name table entry if found.
* @returns NULL if not found.
* @param pbNameTable Pointer to the name table that should be searched.
* @param cbNameTable The size of the name table.
* @param pchSymbol The name of the symbol we're looking for.
* @param cchSymbol The length of the symbol name.
*/
static const KU8 *kldrModLXDoNameTableLookupByName(const KU8 *pbNameTable, KSSIZE cbNameTable,
const char *pchSymbol, KSIZE cchSymbol)
{
/*
* Determin the namelength up front so we can skip anything which doesn't matches the length.
*/
KU8 cbSymbol8Bit = (KU8)cchSymbol;
if (cbSymbol8Bit != cchSymbol)
return NULL; /* too long. */
/*
* Walk the name table.
*/
while (*pbNameTable != 0 && cbNameTable > 0)
{
const KU8 cbName = *pbNameTable;
cbNameTable -= cbName + 1 + 2;
if (cbNameTable < 0)
break;
if ( cbName == cbSymbol8Bit
&& !kHlpMemComp(pbNameTable + 1, pchSymbol, cbName))
return pbNameTable;
/* next entry */
pbNameTable += cbName + 1 + 2;
}
return NULL;
}
/**
* Deal with a forwarder entry.
*
* @returns See kLdrModQuerySymbol.
* @param pModLX The PE module interpreter instance.
* @param pEntry The forwarder entry.
* @param pfnGetForwarder The callback for resolving forwarder symbols. (optional)
* @param pvUser The user argument for the callback.
* @param puValue Where to put the value. (optional)
* @param pfKind Where to put the symbol kind. (optional)
*/
static int kldrModLXDoForwarderQuery(PKLDRMODLX pModLX, const struct e32_entry *pEntry,
PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind)
{
int rc;
KU32 iSymbol;
const char *pchSymbol;
KU8 cchSymbol;
if (!pfnGetForwarder)
return KLDR_ERR_FORWARDER_SYMBOL;
/*
* Validate the entry import module ordinal.
*/
if ( !pEntry->e32_variant.e32_fwd.modord
|| pEntry->e32_variant.e32_fwd.modord > pModLX->Hdr.e32_impmodcnt)
return KLDR_ERR_LX_BAD_FORWARDER;
/*
* Figure out the parameters.
*/
if (pEntry->e32_flags & FWD_ORDINAL)
{
iSymbol = pEntry->e32_variant.e32_fwd.value;
pchSymbol = NULL; /* no symbol name. */
cchSymbol = 0;
}
else
{
const KU8 *pbName;
/* load the fixup section if necessary. */
if (!pModLX->pbImportProcs)
{
rc = kldrModLXDoLoadFixupSection(pModLX);
if (rc)
return rc;
}
/* Make name pointer. */
pbName = pModLX->pbImportProcs + pEntry->e32_variant.e32_fwd.value;
if ( pbName >= pModLX->pbFixupSectionLast
|| pbName < pModLX->pbFixupSection
|| !*pbName)
return KLDR_ERR_LX_BAD_FORWARDER;
/* check for '#' name. */
if (pbName[1] == '#')
{
KU8 cbLeft = *pbName;
const KU8 *pb = pbName + 1;
unsigned uBase;
/* base detection */
uBase = 10;
if ( cbLeft > 1
&& pb[1] == '0'
&& (pb[2] == 'x' || pb[2] == 'X'))
{
uBase = 16;
pb += 2;
cbLeft -= 2;
}
/* ascii to integer */
iSymbol = 0;
while (cbLeft-- > 0)
{
/* convert char to digit. */
unsigned uDigit = *pb++;
if (uDigit >= '0' && uDigit <= '9')
uDigit -= '0';
else if (uDigit >= 'a' && uDigit <= 'z')
uDigit -= 'a' + 10;
else if (uDigit >= 'A' && uDigit <= 'Z')
uDigit -= 'A' + 10;
else if (!uDigit)
break;
else
return KLDR_ERR_LX_BAD_FORWARDER;
if (uDigit >= uBase)
return KLDR_ERR_LX_BAD_FORWARDER;
/* insert the digit */
iSymbol *= uBase;
iSymbol += uDigit;
}
if (!iSymbol)
return KLDR_ERR_LX_BAD_FORWARDER;
pchSymbol = NULL; /* no symbol name. */
cchSymbol = 0;
}
else
{
pchSymbol = (char *)pbName + 1;
cchSymbol = *pbName;
iSymbol = NIL_KLDRMOD_SYM_ORDINAL;
}
}
/*
* Resolve the forwarder.
*/
rc = pfnGetForwarder(pModLX->pMod, pEntry->e32_variant.e32_fwd.modord - 1, iSymbol, pchSymbol, cchSymbol, NULL, puValue, pfKind, pvUser);
if (!rc && pfKind)
*pfKind |= KLDRSYMKIND_FORWARDER;
return rc;
}
/**
* Loads the fixup section from the executable image.
*
* The fixup section isn't loaded until it's accessed. It's also freed by kLdrModDone().
*
* @returns 0 on success, non-zero kLdr or native status code on failure.
* @param pModLX The PE module interpreter instance.
*/
static int kldrModLXDoLoadFixupSection(PKLDRMODLX pModLX)
{
int rc;
KU32 off;
void *pv;
pv = kHlpAlloc(pModLX->Hdr.e32_fixupsize);
if (!pv)
return KERR_NO_MEMORY;
off = pModLX->Hdr.e32_objtab + pModLX->Hdr.e32_ldrsize;
rc = kRdrRead(pModLX->pMod->pRdr, pv, pModLX->Hdr.e32_fixupsize,
off + pModLX->offHdr);
if (!rc)
{
pModLX->pbFixupSection = pv;
pModLX->pbFixupSectionLast = pModLX->pbFixupSection + pModLX->Hdr.e32_fixupsize;
KLDRMODLX_ASSERT(!pModLX->paoffPageFixups);
if (pModLX->Hdr.e32_fpagetab)
pModLX->paoffPageFixups = (const KU32 *)(pModLX->pbFixupSection + pModLX->Hdr.e32_fpagetab - off);
KLDRMODLX_ASSERT(!pModLX->pbFixupRecs);
if (pModLX->Hdr.e32_frectab)
pModLX->pbFixupRecs = pModLX->pbFixupSection + pModLX->Hdr.e32_frectab - off;
KLDRMODLX_ASSERT(!pModLX->pbImportMods);
if (pModLX->Hdr.e32_impmod)
pModLX->pbImportMods = pModLX->pbFixupSection + pModLX->Hdr.e32_impmod - off;
KLDRMODLX_ASSERT(!pModLX->pbImportProcs);
if (pModLX->Hdr.e32_impproc)
pModLX->pbImportProcs = pModLX->pbFixupSection + pModLX->Hdr.e32_impproc - off;
}
else
kHlpFree(pv);
return rc;
}
/** @copydoc kLdrModEnumSymbols */
static int kldrModLXEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress,
KU32 fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
const struct b32_bundle *pBundle;
KU32 iOrdinal;
int rc = 0;
K_NOREF(pvBits);
K_NOREF(fFlags);
kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
/*
* Enumerate the entry table.
* (The entry table is made up of bundles of similar exports.)
*/
iOrdinal = 1;
pBundle = (const struct b32_bundle *)pModLX->pbEntryTab;
while (pBundle->b32_cnt && iOrdinal)
{
static const KSIZE s_cbEntry[] = { 0, 3, 5, 5, 7 };
/*
* Enum the entries in the bundle.
*/
if (pBundle->b32_type != EMPTY)
{
const struct e32_entry *pEntry;
KSIZE cbEntry;
KLDRADDR BundleRVA;
unsigned cLeft;
/* Validate the bundle. */
switch (pBundle->b32_type)
{
case ENTRY16:
case GATE16:
case ENTRY32:
if ( pBundle->b32_obj <= 0
|| pBundle->b32_obj > pMod->cSegments)
return KLDR_ERR_LX_BAD_BUNDLE;
BundleRVA = pMod->aSegments[pBundle->b32_obj - 1].RVA;
break;
case ENTRYFWD:
BundleRVA = 0;
break;
default:
/* anyone actually using TYPEINFO will end up here. */
KLDRMODLX_ASSERT(!"Bad bundle type");
return KLDR_ERR_LX_BAD_BUNDLE;
}
/* iterate the bundle entries. */
cbEntry = s_cbEntry[pBundle->b32_type];
pEntry = (const struct e32_entry *)(pBundle + 1);
cLeft = pBundle->b32_cnt;
while (cLeft-- > 0)
{
KLDRADDR uValue;
KU32 fKind;
int fFoundName;
const KU8 *pbName;
/*
* Calc the symbol value and kind.
*/
switch (pBundle->b32_type)
{
/* e32_flags + a 16-bit offset. */
case ENTRY16:
uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_offset.offset16;
fKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_NO_TYPE;
break;
/* e32_flags + a 16-bit offset + a 16-bit callgate selector. */
case GATE16:
uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_callgate.offset;
fKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_CODE;
break;
/* e32_flags + a 32-bit offset. */
case ENTRY32:
uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_offset.offset32;
fKind = KLDRSYMKIND_32BIT;
break;
/* e32_flags + 16-bit import module ordinal + a 32-bit procname or ordinal. */
case ENTRYFWD:
uValue = 0; /** @todo implement enumeration of forwarders properly. */
fKind = KLDRSYMKIND_FORWARDER;
break;
default: /* shut up gcc. */
uValue = 0;
fKind = KLDRSYMKIND_NO_BIT | KLDRSYMKIND_NO_TYPE;
break;
}
/*
* Any symbol names?
*/
fFoundName = 0;
/* resident name table. */
pbName = pModLX->pbResNameTab;
if (pbName)
{
do
{
pbName = kldrModLXDoNameTableLookupByOrdinal(pbName, pModLX->pbLoaderSectionLast - pbName + 1, iOrdinal);
if (!pbName)
break;
fFoundName = 1;
rc = pfnCallback(pMod, iOrdinal, (const char *)pbName + 1, *pbName, NULL, uValue, fKind, pvUser);
if (rc)
return rc;
/* skip to the next entry */
pbName += 1 + *pbName + 2;
} while (pbName < pModLX->pbLoaderSectionLast);
}
/* resident name table. */
pbName = pModLX->pbNonResNameTab;
/** @todo lazy load the non-resident name table. */
if (pbName)
{
do
{
pbName = kldrModLXDoNameTableLookupByOrdinal(pbName, pModLX->pbNonResNameTabLast - pbName + 1, iOrdinal);
if (!pbName)
break;
fFoundName = 1;
rc = pfnCallback(pMod, iOrdinal, (const char *)pbName + 1, *pbName, NULL, uValue, fKind, pvUser);
if (rc)
return rc;
/* skip to the next entry */
pbName += 1 + *pbName + 2;
} while (pbName < pModLX->pbLoaderSectionLast);
}
/*
* If no names, call once with the ordinal only.
*/
if (!fFoundName)
{
rc = pfnCallback(pMod, iOrdinal, NULL, 0, NULL, uValue, fKind, pvUser);
if (rc)
return rc;
}
/* next */
iOrdinal++;
pEntry = (const struct e32_entry *)((KUPTR)pEntry + cbEntry);
}
}
/*
* The next bundle.
*/
if (pBundle->b32_type > ENTRYFWD)
{
KLDRMODLX_ASSERT(!"Bad type"); /** @todo figure out TYPEINFO. */
return KLDR_ERR_LX_BAD_BUNDLE;
}
if (pBundle->b32_type == 0)
pBundle = (const struct b32_bundle *)((const KU8 *)pBundle + 2);
else
pBundle = (const struct b32_bundle *)((const KU8 *)(pBundle + 1) + s_cbEntry[pBundle->b32_type] * pBundle->b32_cnt);
}
return 0;
}
/**
* Lookup a name table entry by ordinal.
*
* @returns Pointer to the name table entry if found.
* @returns NULL if not found.
* @param pbNameTable Pointer to the name table that should be searched.
* @param cbNameTable The size of the name table.
* @param iOrdinal The ordinal to search for.
*/
static const KU8 *kldrModLXDoNameTableLookupByOrdinal(const KU8 *pbNameTable, KSSIZE cbNameTable, KU32 iOrdinal)
{
while (*pbNameTable != 0 && cbNameTable > 0)
{
const KU8 cbName = *pbNameTable;
KU32 iName;
cbNameTable -= cbName + 1 + 2;
if (cbNameTable < 0)
break;
iName = *(pbNameTable + cbName + 1)
| ((unsigned)*(pbNameTable + cbName + 2) << 8);
if (iName == iOrdinal)
return pbNameTable;
/* next entry */
pbNameTable += cbName + 1 + 2;
}
return NULL;
}
/** @copydoc kLdrModGetImport */
static int kldrModLXGetImport(PKLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZE cchName)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
const KU8 *pb;
int rc;
K_NOREF(pvBits);
/*
* Validate
*/
if (iImport >= pModLX->Hdr.e32_impmodcnt)
return KLDR_ERR_IMPORT_ORDINAL_OUT_OF_BOUNDS;
/*
* Lazy loading the fixup section.
*/
if (!pModLX->pbImportMods)
{
rc = kldrModLXDoLoadFixupSection(pModLX);
if (rc)
return rc;
}
/*
* Iterate the module import table until we reach the requested import ordinal.
*/
pb = pModLX->pbImportMods;
while (iImport-- > 0)
pb += *pb + 1;
/*
* Copy out the result.
*/
if (*pb < cchName)
{
kHlpMemCopy(pszName, pb + 1, *pb);
pszName[*pb] = '\0';
rc = 0;
}
else
{
kHlpMemCopy(pszName, pb + 1, cchName);
if (cchName)
pszName[cchName - 1] = '\0';
rc = KERR_BUFFER_OVERFLOW;
}
return rc;
}
/** @copydoc kLdrModNumberOfImports */
static KI32 kldrModLXNumberOfImports(PKLDRMOD pMod, const void *pvBits)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
K_NOREF(pvBits);
return pModLX->Hdr.e32_impmodcnt;
}
/** @copydoc kLdrModGetStackInfo */
static int kldrModLXGetStackInfo(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
const KU32 i = pModLX->Hdr.e32_stackobj;
K_NOREF(pvBits);
if ( i
&& i <= pMod->cSegments
&& pModLX->Hdr.e32_esp <= pMod->aSegments[i - 1].LinkAddress + pMod->aSegments[i - 1].cb
&& pModLX->Hdr.e32_stacksize
&& pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize >= pMod->aSegments[i - 1].LinkAddress)
{
kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
pStackInfo->LinkAddress = pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize;
pStackInfo->Address = BaseAddress
+ pMod->aSegments[i - 1].RVA
+ pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize - pMod->aSegments[i - 1].LinkAddress;
}
else
{
pStackInfo->Address = NIL_KLDRADDR;
pStackInfo->LinkAddress = NIL_KLDRADDR;
}
pStackInfo->cbStack = pModLX->Hdr.e32_stacksize;
pStackInfo->cbStackThread = 0;
return 0;
}
/** @copydoc kLdrModQueryMainEntrypoint */
static int kldrModLXQueryMainEntrypoint(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
K_NOREF(pvBits);
/*
* Convert the address from the header.
*/
kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
*pMainEPAddress = pModLX->Hdr.e32_startobj
&& pModLX->Hdr.e32_startobj <= pMod->cSegments
&& pModLX->Hdr.e32_eip < pMod->aSegments[pModLX->Hdr.e32_startobj - 1].cb
? BaseAddress + pMod->aSegments[pModLX->Hdr.e32_startobj - 1].RVA + pModLX->Hdr.e32_eip
: NIL_KLDRADDR;
return 0;
}
/** @copydoc kLdrModEnumDbgInfo */
static int kldrModLXEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser)
{
/*PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;*/
K_NOREF(pfnCallback);
K_NOREF(pvUser);
/*
* Quit immediately if no debug info.
*/
if (kldrModLXHasDbgInfo(pMod, pvBits))
return 0;
#if 0
/*
* Read the debug info and look for familiar magics and structures.
*/
/** @todo */
#endif
return 0;
}
/** @copydoc kLdrModHasDbgInfo */
static int kldrModLXHasDbgInfo(PKLDRMOD pMod, const void *pvBits)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
K_NOREF(pvBits);
/*
* Don't curretnly bother with linkers which doesn't advertise it in the header.
*/
if ( !pModLX->Hdr.e32_debuginfo
|| !pModLX->Hdr.e32_debuglen)
return KLDR_ERR_NO_DEBUG_INFO;
return 0;
}
/** @copydoc kLdrModMap */
static int kldrModLXMap(PKLDRMOD pMod)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
unsigned fFixed;
void *pvBase;
int rc;
/*
* Already mapped?
*/
if (pModLX->pvMapping)
return KLDR_ERR_ALREADY_MAPPED;
/*
* Allocate memory for it.
*/
/* fixed image? */
fFixed = pMod->enmType == KLDRTYPE_EXECUTABLE_FIXED
|| pMod->enmType == KLDRTYPE_SHARED_LIBRARY_FIXED;
if (!fFixed)
pvBase = NULL;
else
{
pvBase = (void *)(KUPTR)pMod->aSegments[0].LinkAddress;
if ((KUPTR)pvBase != pMod->aSegments[0].LinkAddress)
return KLDR_ERR_ADDRESS_OVERFLOW;
}
rc = kHlpPageAlloc(&pvBase, pModLX->cbMapped, KPROT_EXECUTE_READWRITE, fFixed);
if (rc)
return rc;
/*
* Load the bits, apply page protection, and update the segment table.
*/
rc = kldrModLXDoLoadBits(pModLX, pvBase);
if (!rc)
rc = kldrModLXDoProtect(pModLX, pvBase, 0 /* protect */);
if (!rc)
{
KU32 i;
for (i = 0; i < pMod->cSegments; i++)
{
if (pMod->aSegments[i].RVA != NIL_KLDRADDR)
pMod->aSegments[i].MapAddress = (KUPTR)pvBase + (KUPTR)pMod->aSegments[i].RVA;
}
pModLX->pvMapping = pvBase;
}
else
kHlpPageFree(pvBase, pModLX->cbMapped);
return rc;
}
/**
* Loads the LX pages into the specified memory mapping.
*
* @returns 0 on success.
* @returns non-zero kLdr or OS status code on failure.
*
* @param pModLX The LX module interpreter instance.
* @param pvBits Where to load the bits.
*/
static int kldrModLXDoLoadBits(PKLDRMODLX pModLX, void *pvBits)
{
const PKRDR pRdr = pModLX->pMod->pRdr;
KU8 *pbTmpPage = NULL;
int rc = 0;
KU32 i;
/*
* Iterate the segments.
*/
for (i = 0; i < pModLX->Hdr.e32_objcnt; i++)
{
const struct o32_obj * const pObj = &pModLX->paObjs[i];
const KU32 cPages = (KU32)(pModLX->pMod->aSegments[i].cbMapped / OBJPAGELEN);
KU32 iPage;
KU8 *pbPage = (KU8 *)pvBits + (KUPTR)pModLX->pMod->aSegments[i].RVA;
/*
* Iterate the page map pages.
*/
for (iPage = 0; !rc && iPage < pObj->o32_mapsize; iPage++, pbPage += OBJPAGELEN)
{
const struct o32_map *pMap = &pModLX->paPageMappings[iPage + pObj->o32_pagemap - 1];
switch (pMap->o32_pageflags)
{
case VALID:
if (pMap->o32_pagesize == OBJPAGELEN)
rc = kRdrRead(pRdr, pbPage, OBJPAGELEN,
pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));
else if (pMap->o32_pagesize < OBJPAGELEN)
{
rc = kRdrRead(pRdr, pbPage, pMap->o32_pagesize,
pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));
kHlpMemSet(pbPage + pMap->o32_pagesize, 0, OBJPAGELEN - pMap->o32_pagesize);
}
else
rc = KLDR_ERR_LX_BAD_PAGE_MAP;
break;
case ITERDATA:
case ITERDATA2:
/* make sure we've got a temp page .*/
if (!pbTmpPage)
{
pbTmpPage = kHlpAlloc(OBJPAGELEN + 256);
if (!pbTmpPage)
break;
}
/* validate the size. */
if (pMap->o32_pagesize > OBJPAGELEN + 252)
{
rc = KLDR_ERR_LX_BAD_PAGE_MAP;
break;
}
/* read it and ensure 4 extra zero bytes. */
rc = kRdrRead(pRdr, pbTmpPage, pMap->o32_pagesize,
pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));
if (rc)
break;
kHlpMemSet(pbTmpPage + pMap->o32_pagesize, 0, 4);
/* unpack it into the image page. */
if (pMap->o32_pageflags == ITERDATA2)
rc = kldrModLXDoIterData2Unpacking(pbPage, pbTmpPage, pMap->o32_pagesize);
else
rc = kldrModLXDoIterDataUnpacking(pbPage, pbTmpPage, pMap->o32_pagesize);
break;
case INVALID: /* we're probably not dealing correctly with INVALID pages... */
case ZEROED:
kHlpMemSet(pbPage, 0, OBJPAGELEN);
break;
case RANGE:
KLDRMODLX_ASSERT(!"RANGE");
/* Falls through. */
default:
rc = KLDR_ERR_LX_BAD_PAGE_MAP;
break;
}
}
if (rc)
break;
/*
* Zero the remaining pages.
*/
if (iPage < cPages)
kHlpMemSet(pbPage, 0, (cPages - iPage) * OBJPAGELEN);
}
if (pbTmpPage)
kHlpFree(pbTmpPage);
return rc;
}
/**
* Unpacks iterdata (aka EXEPACK).
*
* @returns 0 on success, non-zero kLdr status code on failure.
* @param pbDst Where to put the uncompressed data. (Assumes OBJPAGELEN size.)
* @param pbSrc The compressed source data.
* @param cbSrc The file size of the compressed data. The source buffer
* contains 4 additional zero bytes.
*/
static int kldrModLXDoIterDataUnpacking(KU8 *pbDst, const KU8 *pbSrc, int cbSrc)
{
const struct LX_Iter *pIter = (const struct LX_Iter *)pbSrc;
int cbDst = OBJPAGELEN;
/* Validate size of data. */
if (cbSrc >= (int)OBJPAGELEN - 2)
return KLDR_ERR_LX_BAD_ITERDATA;
/*
* Expand the page.
*/
while (cbSrc > 0 && pIter->LX_nIter)
{
if (pIter->LX_nBytes == 1)
{
/*
* Special case - one databyte.
*/
cbDst -= pIter->LX_nIter;
if (cbDst < 0)
return KLDR_ERR_LX_BAD_ITERDATA;
cbSrc -= 4 + 1;
if (cbSrc < -4)
return KLDR_ERR_LX_BAD_ITERDATA;
kHlpMemSet(pbDst, pIter->LX_Iterdata, pIter->LX_nIter);
pbDst += pIter->LX_nIter;
pIter++;
}
else
{
/*
* General.
*/
int i;
cbDst -= pIter->LX_nIter * pIter->LX_nBytes;
if (cbDst < 0)
return KLDR_ERR_LX_BAD_ITERDATA;
cbSrc -= 4 + pIter->LX_nBytes;
if (cbSrc < -4)
return KLDR_ERR_LX_BAD_ITERDATA;
for (i = pIter->LX_nIter; i > 0; i--, pbDst += pIter->LX_nBytes)
kHlpMemCopy(pbDst, &pIter->LX_Iterdata, pIter->LX_nBytes);
pIter = (struct LX_Iter *)((char*)pIter + 4 + pIter->LX_nBytes);
}
}
/*
* Zero remainder of the page.
*/
if (cbDst > 0)
kHlpMemSet(pbDst, 0, cbDst);
return 0;
}
/**
* Unpacks iterdata (aka EXEPACK).
*
* @returns 0 on success, non-zero kLdr status code on failure.
* @param pbDst Where to put the uncompressed data. (Assumes OBJPAGELEN size.)
* @param pbSrc The compressed source data.
* @param cbSrc The file size of the compressed data. The source buffer
* contains 4 additional zero bytes.
*/
static int kldrModLXDoIterData2Unpacking(KU8 *pbDst, const KU8 *pbSrc, int cbSrc)
{
int cbDst = OBJPAGELEN;
while (cbSrc > 0)
{
/*
* Bit 0 and 1 is the encoding type.
*/
switch (*pbSrc & 0x03)
{
/*
*
* 0 1 2 3 4 5 6 7
* type | |
* ----------------
* cb <cb bytes of data>
*
* Bits 2-7 is, if not zero, the length of an uncompressed run
* starting at the following byte.
*
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
* type | | | | | |
* ---------------- ---------------------- -----------------------
* zero cb char to multiply
*
* If the bits are zero, the following two bytes describes a 1 byte interation
* run. First byte is count, second is the byte to copy. A count of zero is
* means end of data, and we simply stops. In that case the rest of the data
* should be zero.
*/
case 0:
{
if (*pbSrc)
{
const int cb = *pbSrc >> 2;
cbDst -= cb;
if (cbDst < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
cbSrc -= cb + 1;
if (cbSrc < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
kHlpMemCopy(pbDst, ++pbSrc, cb);
pbDst += cb;
pbSrc += cb;
}
else if (cbSrc < 2)
return KLDR_ERR_LX_BAD_ITERDATA2;
else
{
const int cb = pbSrc[1];
if (!cb)
goto l_endloop;
cbDst -= cb;
if (cbDst < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
cbSrc -= 3;
if (cbSrc < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
kHlpMemSet(pbDst, pbSrc[2], cb);
pbDst += cb;
pbSrc += 3;
}
break;
}
/*
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
* type | | | | | |
* ---- ------- -------------------------
* cb1 cb2 - 3 offset <cb1 bytes of data>
*
* Two bytes layed out as described above, followed by cb1 bytes of data to be copied.
* The cb2(+3) and offset describes an amount of data to be copied from the expanded
* data relative to the current position. The data copied as you would expect it to be.
*/
case 1:
{
cbSrc -= 2;
if (cbSrc < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
else
{
const unsigned off = ((unsigned)pbSrc[1] << 1) | (*pbSrc >> 7);
const int cb1 = (*pbSrc >> 2) & 3;
const int cb2 = ((*pbSrc >> 4) & 7) + 3;
pbSrc += 2;
cbSrc -= cb1;
if (cbSrc < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
cbDst -= cb1;
if (cbDst < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
kHlpMemCopy(pbDst, pbSrc, cb1);
pbDst += cb1;
pbSrc += cb1;
if (off > OBJPAGELEN - (unsigned)cbDst)
return KLDR_ERR_LX_BAD_ITERDATA2;
cbDst -= cb2;
if (cbDst < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
kHlpMemMove(pbDst, pbDst - off, cb2);
pbDst += cb2;
}
break;
}
/*
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
* type | | | |
* ---- ----------------------------------
* cb-3 offset
*
* Two bytes layed out as described above.
* The cb(+3) and offset describes an amount of data to be copied from the expanded
* data relative to the current position.
*
* If offset == 1 the data is not copied as expected, but in the memcpyw manner.
*/
case 2:
{
cbSrc -= 2;
if (cbSrc < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
else
{
const unsigned off = ((unsigned)pbSrc[1] << 4) | (*pbSrc >> 4);
const int cb = ((*pbSrc >> 2) & 3) + 3;
pbSrc += 2;
if (off > OBJPAGELEN - (unsigned)cbDst)
return KLDR_ERR_LX_BAD_ITERDATA2;
cbDst -= cb;
if (cbDst < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
kLdrModLXMemCopyW(pbDst, pbDst - off, cb);
pbDst += cb;
}
break;
}
/*
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
* type | | | | | |
* ---------- ---------------- ----------------------------------
* cb1 cb2 offset <cb1 bytes of data>
*
* Three bytes layed out as described above, followed by cb1 bytes of data to be copied.
* The cb2 and offset describes an amount of data to be copied from the expanded
* data relative to the current position.
*
* If offset == 1 the data is not copied as expected, but in the memcpyw manner.
*/
case 3:
{
cbSrc -= 3;
if (cbSrc < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
else
{
const int cb1 = (*pbSrc >> 2) & 0xf;
const int cb2 = ((pbSrc[1] & 0xf) << 2) | (*pbSrc >> 6);
const unsigned off = ((unsigned)pbSrc[2] << 4) | (pbSrc[1] >> 4);
pbSrc += 3;
cbSrc -= cb1;
if (cbSrc < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
cbDst -= cb1;
if (cbDst < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
kHlpMemCopy(pbDst, pbSrc, cb1);
pbDst += cb1;
pbSrc += cb1;
if (off > OBJPAGELEN - (unsigned)cbDst)
return KLDR_ERR_LX_BAD_ITERDATA2;
cbDst -= cb2;
if (cbDst < 0)
return KLDR_ERR_LX_BAD_ITERDATA2;
kLdrModLXMemCopyW(pbDst, pbDst - off, cb2);
pbDst += cb2;
}
break;
}
} /* type switch. */
} /* unpack loop */
l_endloop:
/*
* Zero remainder of the page.
*/
if (cbDst > 0)
kHlpMemSet(pbDst, 0, cbDst);
return 0;
}
/**
* Special memcpy employed by the iterdata2 algorithm.
*
* Emulate a 16-bit memcpy (copying 16-bit at a time) and the effects this
* has if src is very close to the destination.
*
* @param pbDst Destination pointer.
* @param pbSrc Source pointer. Will always be <= pbDst.
* @param cb Amount of data to be copied.
* @remark This assumes that unaligned word and dword access is fine.
*/
static void kLdrModLXMemCopyW(KU8 *pbDst, const KU8 *pbSrc, int cb)
{
switch (pbDst - pbSrc)
{
case 0:
case 1:
case 2:
case 3:
/* 16-bit copy (unaligned) */
if (cb & 1)
*pbDst++ = *pbSrc++;
for (cb >>= 1; cb > 0; cb--, pbDst += 2, pbSrc += 2)
*(KU16 *)pbDst = *(const KU16 *)pbSrc;
break;
default:
/* 32-bit copy (unaligned) */
if (cb & 1)
*pbDst++ = *pbSrc++;
if (cb & 2)
{
*(KU16 *)pbDst = *(const KU16 *)pbSrc;
pbDst += 2;
pbSrc += 2;
}
for (cb >>= 2; cb > 0; cb--, pbDst += 4, pbSrc += 4)
*(KU32 *)pbDst = *(const KU32 *)pbSrc;
break;
}
}
/**
* Unprotects or protects the specified image mapping.
*
* @returns 0 on success.
* @returns non-zero kLdr or OS status code on failure.
*
* @param pModLX The LX module interpreter instance.
* @param pvBits The mapping to protect.
* @param UnprotectOrProtect If 1 unprotect (i.e. make all writable), otherwise
* protect according to the object table.
*/
static int kldrModLXDoProtect(PKLDRMODLX pModLX, void *pvBits, unsigned fUnprotectOrProtect)
{
KU32 i;
PKLDRMOD pMod = pModLX->pMod;
/*
* Change object protection.
*/
for (i = 0; i < pMod->cSegments; i++)
{
int rc;
void *pv;
KPROT enmProt;
/* calc new protection. */
enmProt = pMod->aSegments[i].enmProt;
if (fUnprotectOrProtect)
{
switch (enmProt)
{
case KPROT_NOACCESS:
case KPROT_READONLY:
case KPROT_READWRITE:
case KPROT_WRITECOPY:
enmProt = KPROT_READWRITE;
break;
case KPROT_EXECUTE:
case KPROT_EXECUTE_READ:
case KPROT_EXECUTE_READWRITE:
case KPROT_EXECUTE_WRITECOPY:
enmProt = KPROT_EXECUTE_READWRITE;
break;
default:
KLDRMODLX_ASSERT(!"bad enmProt");
return -1;
}
}
else
{
/* copy on write -> normal write. */
if (enmProt == KPROT_EXECUTE_WRITECOPY)
enmProt = KPROT_EXECUTE_READWRITE;
else if (enmProt == KPROT_WRITECOPY)
enmProt = KPROT_READWRITE;
}
/* calc the address and set page protection. */
pv = (KU8 *)pvBits + pMod->aSegments[i].RVA;
rc = kHlpPageProtect(pv, pMod->aSegments[i].cbMapped, enmProt);
if (rc)
break;
/** @todo the gap page should be marked NOACCESS! */
}
return 0;
}
/** @copydoc kLdrModUnmap */
static int kldrModLXUnmap(PKLDRMOD pMod)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
KU32 i;
int rc;
/*
* Mapped?
*/
if (!pModLX->pvMapping)
return KLDR_ERR_NOT_MAPPED;
/*
* Free the mapping and update the segments.
*/
rc = kHlpPageFree((void *)pModLX->pvMapping, pModLX->cbMapped);
KLDRMODLX_ASSERT(!rc);
pModLX->pvMapping = NULL;
for (i = 0; i < pMod->cSegments; i++)
pMod->aSegments[i].MapAddress = 0;
return rc;
}
/** @copydoc kLdrModAllocTLS */
static int kldrModLXAllocTLS(PKLDRMOD pMod, void *pvMapping)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
/* no tls, just do the error checking. */
if ( pvMapping == KLDRMOD_INT_MAP
&& pModLX->pvMapping)
return KLDR_ERR_NOT_MAPPED;
return 0;
}
/** @copydoc kLdrModFreeTLS */
static void kldrModLXFreeTLS(PKLDRMOD pMod, void *pvMapping)
{
/* no tls. */
K_NOREF(pMod);
K_NOREF(pvMapping);
}
/** @copydoc kLdrModReload */
static int kldrModLXReload(PKLDRMOD pMod)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
int rc, rc2;
/*
* Mapped?
*/
if (!pModLX->pvMapping)
return KLDR_ERR_NOT_MAPPED;
/*
* Before doing anything we'll have to make all pages writable.
*/
rc = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 1 /* unprotect */);
if (rc)
return rc;
/*
* Load the bits again.
*/
rc = kldrModLXDoLoadBits(pModLX, (void *)pModLX->pvMapping);
/*
* Restore protection.
*/
rc2 = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 0 /* protect */);
if (!rc && rc2)
rc = rc2;
return rc;
}
/** @copydoc kLdrModFixupMapping */
static int kldrModLXFixupMapping(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
int rc, rc2;
/*
* Mapped?
*/
if (!pModLX->pvMapping)
return KLDR_ERR_NOT_MAPPED;
/*
* Before doing anything we'll have to make all pages writable.
*/
rc = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 1 /* unprotect */);
if (rc)
return rc;
/*
* Apply fixups and resolve imports.
*/
rc = kldrModLXRelocateBits(pMod, (void *)pModLX->pvMapping, (KUPTR)pModLX->pvMapping,
pMod->aSegments[0].LinkAddress, pfnGetImport, pvUser);
/*
* Restore protection.
*/
rc2 = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 0 /* protect */);
if (!rc && rc2)
rc = rc2;
return rc;
}
/** @copydoc kLdrModCallInit */
static int kldrModLXCallInit(PKLDRMOD pMod, void *pvMapping, KUPTR uHandle)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
int rc;
/*
* Mapped?
*/
if (pvMapping == KLDRMOD_INT_MAP)
{
pvMapping = (void *)pModLX->pvMapping;
if (!pvMapping)
return KLDR_ERR_NOT_MAPPED;
}
/*
* Do TLS callbacks first and then call the init/term function if it's a DLL.
*/
if ((pModLX->Hdr.e32_mflags & E32MODMASK) == E32MODDLL)
rc = kldrModLXDoCallDLL(pModLX, pvMapping, 0 /* attach */, uHandle);
else
rc = 0;
return rc;
}
/**
* Call the DLL entrypoint.
*
* @returns 0 on success.
* @returns KLDR_ERR_MODULE_INIT_FAILED or KLDR_ERR_THREAD_ATTACH_FAILED on failure.
* @param pModLX The LX module interpreter instance.
* @param pvMapping The module mapping to use (resolved).
* @param uOp The operation (DLL_*).
* @param uHandle The module handle to present.
*/
static int kldrModLXDoCallDLL(PKLDRMODLX pModLX, void *pvMapping, unsigned uOp, KUPTR uHandle)
{
int rc;
/*
* If no entrypoint there isn't anything to be done.
*/
if ( !pModLX->Hdr.e32_startobj
|| pModLX->Hdr.e32_startobj > pModLX->Hdr.e32_objcnt)
return 0;
/*
* Invoke the entrypoint and convert the boolean result to a kLdr status code.
*/
rc = kldrModLXDoCall((KUPTR)pvMapping
+ (KUPTR)pModLX->pMod->aSegments[pModLX->Hdr.e32_startobj - 1].RVA
+ pModLX->Hdr.e32_eip,
uHandle, uOp, NULL);
if (rc)
rc = 0;
else if (uOp == 0 /* attach */)
rc = KLDR_ERR_MODULE_INIT_FAILED;
else /* detach: ignore failures */
rc = 0;
return rc;
}
/**
* Do a 3 parameter callback.
*
* @returns 32-bit callback return.
* @param uEntrypoint The address of the function to be called.
* @param uHandle The first argument, the module handle.
* @param uOp The second argumnet, the reason we're calling.
* @param pvReserved The third argument, reserved argument. (figure this one out)
*/
KI32 kldrModLXDoCall(KUPTR uEntrypoint, KUPTR uHandle, KU32 uOp, void *pvReserved)
{
#if defined(__X86__) || defined(__i386__) || defined(_M_IX86)
KI32 rc;
/** @todo try/except */
/*
* Paranoia.
*/
# ifdef __GNUC__
__asm__ __volatile__(
"pushl %2\n\t"
"pushl %1\n\t"
"pushl %0\n\t"
"lea 12(%%esp), %2\n\t"
"call *%3\n\t"
"movl %2, %%esp\n\t"
: "=a" (rc)
: "d" (uOp),
"S" (0),
"c" (uEntrypoint),
"0" (uHandle));
# elif defined(_MSC_VER)
__asm {
mov eax, [uHandle]
mov edx, [uOp]
mov ecx, 0
mov ebx, [uEntrypoint]
push edi
mov edi, esp
push ecx
push edx
push eax
call ebx
mov esp, edi
pop edi
mov [rc], eax
}
# else
# error "port me!"
# endif
K_NOREF(pvReserved);
return rc;
#else
K_NOREF(uEntrypoint);
K_NOREF(uHandle);
K_NOREF(uOp);
K_NOREF(pvReserved);
return KCPU_ERR_ARCH_CPU_NOT_COMPATIBLE;
#endif
}
/** @copydoc kLdrModCallTerm */
static int kldrModLXCallTerm(PKLDRMOD pMod, void *pvMapping, KUPTR uHandle)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
/*
* Mapped?
*/
if (pvMapping == KLDRMOD_INT_MAP)
{
pvMapping = (void *)pModLX->pvMapping;
if (!pvMapping)
return KLDR_ERR_NOT_MAPPED;
}
/*
* Do the call.
*/
if ((pModLX->Hdr.e32_mflags & E32MODMASK) == E32MODDLL)
kldrModLXDoCallDLL(pModLX, pvMapping, 1 /* detach */, uHandle);
return 0;
}
/** @copydoc kLdrModCallThread */
static int kldrModLXCallThread(PKLDRMOD pMod, void *pvMapping, KUPTR uHandle, unsigned fAttachingOrDetaching)
{
/* no thread attach/detach callout. */
K_NOREF(pMod);
K_NOREF(pvMapping);
K_NOREF(uHandle);
K_NOREF(fAttachingOrDetaching);
return 0;
}
/** @copydoc kLdrModSize */
static KLDRADDR kldrModLXSize(PKLDRMOD pMod)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
return pModLX->cbMapped;
}
/** @copydoc kLdrModGetBits */
static int kldrModLXGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
int rc;
/*
* Load the image bits.
*/
rc = kldrModLXDoLoadBits(pModLX, pvBits);
if (rc)
return rc;
/*
* Perform relocations.
*/
return kldrModLXRelocateBits(pMod, pvBits, BaseAddress, pMod->aSegments[0].LinkAddress, pfnGetImport, pvUser);
}
/** @copydoc kLdrModRelocateBits */
static int kldrModLXRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
{
PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
KU32 iSeg;
int rc;
/*
* Do we need to to *anything*?
*/
if ( NewBaseAddress == OldBaseAddress
&& NewBaseAddress == pModLX->paObjs[0].o32_base
&& !pModLX->Hdr.e32_impmodcnt)
return 0;
/*
* Load the fixup section.
*/
if (!pModLX->pbFixupSection)
{
rc = kldrModLXDoLoadFixupSection(pModLX);
if (rc)
return rc;
}
/*
* Iterate the segments.
*/
for (iSeg = 0; iSeg < pModLX->Hdr.e32_objcnt; iSeg++)
{
const struct o32_obj * const pObj = &pModLX->paObjs[iSeg];
KLDRADDR PageAddress = NewBaseAddress + pModLX->pMod->aSegments[iSeg].RVA;
KU32 iPage;
KU8 *pbPage = (KU8 *)pvBits + (KUPTR)pModLX->pMod->aSegments[iSeg].RVA;
/*
* Iterate the page map pages.
*/
for (iPage = 0, rc = 0; !rc && iPage < pObj->o32_mapsize; iPage++, pbPage += OBJPAGELEN, PageAddress += OBJPAGELEN)
{
const KU8 * const pbFixupRecEnd = pModLX->pbFixupRecs + pModLX->paoffPageFixups[iPage + pObj->o32_pagemap];
const KU8 *pb = pModLX->pbFixupRecs + pModLX->paoffPageFixups[iPage + pObj->o32_pagemap - 1];
KLDRADDR uValue = NIL_KLDRADDR;
KU32 fKind = 0;
int iSelector;
/* sanity */
if (pbFixupRecEnd < pb)
return KLDR_ERR_BAD_FIXUP;
if (pbFixupRecEnd - 1 > pModLX->pbFixupSectionLast)
return KLDR_ERR_BAD_FIXUP;
if (pb < pModLX->pbFixupSection)
return KLDR_ERR_BAD_FIXUP;
/*
* Iterate the fixup record.
*/
while (pb < pbFixupRecEnd)
{
union _rel
{
const KU8 * pb;
const struct r32_rlc *prlc;
} u;
u.pb = pb;
pb += 3 + (u.prlc->nr_stype & NRCHAIN ? 0 : 1); /* place pch at the 4th member. */
/*
* Figure out the target.
*/
switch (u.prlc->nr_flags & NRRTYP)
{
/*
* Internal fixup.
*/
case NRRINT:
{
KU16 iTrgObject;
KU32 offTrgObject;
/* the object */
if (u.prlc->nr_flags & NR16OBJMOD)
{
iTrgObject = *(const KU16 *)pb;
pb += 2;
}
else
iTrgObject = *pb++;
iTrgObject--;
if (iTrgObject >= pModLX->Hdr.e32_objcnt)
return KLDR_ERR_BAD_FIXUP;
/* the target */
if ((u.prlc->nr_stype & NRSRCMASK) != NRSSEG)
{
if (u.prlc->nr_flags & NR32BITOFF)
{
offTrgObject = *(const KU32 *)pb;
pb += 4;
}
else
{
offTrgObject = *(const KU16 *)pb;
pb += 2;
}
/* calculate the symbol info. */
uValue = offTrgObject + NewBaseAddress + pMod->aSegments[iTrgObject].RVA;
}
else
uValue = NewBaseAddress + pMod->aSegments[iTrgObject].RVA;
if ( (u.prlc->nr_stype & NRALIAS)
|| (pMod->aSegments[iTrgObject].fFlags & KLDRSEG_FLAG_16BIT))
iSelector = pMod->aSegments[iTrgObject].Sel16bit;
else
iSelector = pMod->aSegments[iTrgObject].SelFlat;
fKind = 0;
break;
}
/*
* Import by symbol ordinal.
*/
case NRRORD:
{
KU16 iModule;
KU32 iSymbol;
/* the module ordinal */
if (u.prlc->nr_flags & NR16OBJMOD)
{
iModule = *(const KU16 *)pb;
pb += 2;
}
else
iModule = *pb++;
iModule--;
if (iModule >= pModLX->Hdr.e32_impmodcnt)
return KLDR_ERR_BAD_FIXUP;
#if 1
if (u.prlc->nr_flags & NRICHAIN)
return KLDR_ERR_BAD_FIXUP;
#endif
/* . */
if (u.prlc->nr_flags & NR32BITOFF)
{
iSymbol = *(const KU32 *)pb;
pb += 4;
}
else if (!(u.prlc->nr_flags & NR8BITORD))
{
iSymbol = *(const KU16 *)pb;
pb += 2;
}
else
iSymbol = *pb++;
/* resolve it. */
rc = pfnGetImport(pMod, iModule, iSymbol, NULL, 0, NULL, &uValue, &fKind, pvUser);
if (rc)
return rc;
iSelector = -1;
break;
}
/*
* Import by symbol name.
*/
case NRRNAM:
{
KU32 iModule;
KU16 offSymbol;
const KU8 *pbSymbol;
/* the module ordinal */
if (u.prlc->nr_flags & NR16OBJMOD)
{
iModule = *(const KU16 *)pb;
pb += 2;
}
else
iModule = *pb++;
iModule--;
if (iModule >= pModLX->Hdr.e32_impmodcnt)
return KLDR_ERR_BAD_FIXUP;
#if 1
if (u.prlc->nr_flags & NRICHAIN)
return KLDR_ERR_BAD_FIXUP;
#endif
/* . */
if (u.prlc->nr_flags & NR32BITOFF)
{
offSymbol = *(const KU32 *)pb;
pb += 4;
}
else if (!(u.prlc->nr_flags & NR8BITORD))
{
offSymbol = *(const KU16 *)pb;
pb += 2;
}
else
offSymbol = *pb++;
pbSymbol = pModLX->pbImportProcs + offSymbol;
if ( pbSymbol < pModLX->pbImportProcs
|| pbSymbol > pModLX->pbFixupSectionLast)
return KLDR_ERR_BAD_FIXUP;
/* resolve it. */
rc = pfnGetImport(pMod, iModule, NIL_KLDRMOD_SYM_ORDINAL, (const char *)pbSymbol + 1, *pbSymbol, NULL,
&uValue, &fKind, pvUser);
if (rc)
return rc;
iSelector = -1;
break;
}
case NRRENT:
KLDRMODLX_ASSERT(!"NRRENT");
/* Falls through. */
default:
iSelector = -1;
break;
}
/* addend */
if (u.prlc->nr_flags & NRADD)
{
if (u.prlc->nr_flags & NR32BITADD)
{
uValue += *(const KU32 *)pb;
pb += 4;
}
else
{
uValue += *(const KU16 *)pb;
pb += 2;
}
}
/*
* Deal with the 'source' (i.e. the place that should be modified - very logical).
*/
if (!(u.prlc->nr_stype & NRCHAIN))
{
int off = u.prlc->r32_soff;
/* common / simple */
if ( (u.prlc->nr_stype & NRSRCMASK) == NROFF32
&& off >= 0
&& off <= (int)OBJPAGELEN - 4)
*(KU32 *)&pbPage[off] = (KU32)uValue;
else if ( (u.prlc->nr_stype & NRSRCMASK) == NRSOFF32
&& off >= 0
&& off <= (int)OBJPAGELEN - 4)
*(KU32 *)&pbPage[off] = (KU32)(uValue - (PageAddress + off + 4));
else
{
/* generic */
rc = kldrModLXDoReloc(pbPage, off, PageAddress, u.prlc, iSelector, uValue, fKind);
if (rc)
return rc;
}
}
else if (!(u.prlc->nr_flags & NRICHAIN))
{
const KI16 *poffSrc = (const KI16 *)pb;
KU8 c = u.pb[2];
/* common / simple */
if ((u.prlc->nr_stype & NRSRCMASK) == NROFF32)
{
while (c-- > 0)
{
int off = *poffSrc++;
if (off >= 0 && off <= (int)OBJPAGELEN - 4)
*(KU32 *)&pbPage[off] = (KU32)uValue;
else
{
rc = kldrModLXDoReloc(pbPage, off, PageAddress, u.prlc, iSelector, uValue, fKind);
if (rc)
return rc;
}
}
}
else if ((u.prlc->nr_stype & NRSRCMASK) == NRSOFF32)
{
while (c-- > 0)
{
int off = *poffSrc++;
if (off >= 0 && off <= (int)OBJPAGELEN - 4)
*(KU32 *)&pbPage[off] = (KU32)(uValue - (PageAddress + off + 4));
else
{
rc = kldrModLXDoReloc(pbPage, off, PageAddress, u.prlc, iSelector, uValue, fKind);
if (rc)
return rc;
}
}
}
else
{
while (c-- > 0)
{
rc = kldrModLXDoReloc(pbPage, *poffSrc++, PageAddress, u.prlc, iSelector, uValue, fKind);
if (rc)
return rc;
}
}
pb = (const KU8 *)poffSrc;
}
else
{
/* This is a pain because it will require virgin pages on a relocation. */
KLDRMODLX_ASSERT(!"NRICHAIN");
return KLDR_ERR_LX_NRICHAIN_NOT_SUPPORTED;
}
}
}
}
return 0;
}
/**
* Applies the relocation to one 'source' in a page.
*
* This takes care of the more esotic case while the common cases
* are dealt with seperately.
*
* @returns 0 on success, non-zero kLdr status code on failure.
* @param pbPage The page in which to apply the fixup.
* @param off Page relative offset of where to apply the offset.
* @param uValue The target value.
* @param fKind The target kind.
*/
static int kldrModLXDoReloc(KU8 *pbPage, int off, KLDRADDR PageAddress, const struct r32_rlc *prlc,
int iSelector, KLDRADDR uValue, KU32 fKind)
{
#pragma pack(1) /* just to be sure */
union
{
KU8 ab[6];
KU32 off32;
KU16 off16;
KU8 off8;
struct
{
KU16 off;
KU16 Sel;
} Far16;
struct
{
KU32 off;
KU16 Sel;
} Far32;
} uData;
#pragma pack()
const KU8 *pbSrc;
KU8 *pbDst;
KU8 cb;
K_NOREF(fKind);
/*
* Compose the fixup data.
*/
switch (prlc->nr_stype & NRSRCMASK)
{
case NRSBYT:
uData.off8 = (KU8)uValue;
cb = 1;
break;
case NRSSEG:
if (iSelector == -1)
{
/* fixme */
}
uData.off16 = iSelector;
cb = 2;
break;
case NRSPTR:
if (iSelector == -1)
{
/* fixme */
}
uData.Far16.off = (KU16)uValue;
uData.Far16.Sel = iSelector;
cb = 4;
break;
case NRSOFF:
uData.off16 = (KU16)uValue;
cb = 2;
break;
case NRPTR48:
if (iSelector == -1)
{
/* fixme */
}
uData.Far32.off = (KU32)uValue;
uData.Far32.Sel = iSelector;
cb = 6;
break;
case NROFF32:
uData.off32 = (KU32)uValue;
cb = 4;
break;
case NRSOFF32:
uData.off32 = (KU32)(uValue - (PageAddress + off + 4));
cb = 4;
break;
default:
return KLDR_ERR_LX_BAD_FIXUP_SECTION; /** @todo fix error, add more checks! */
}
/*
* Apply it. This is sloooow...
*/
pbSrc = &uData.ab[0];
pbDst = pbPage + off;
while (cb-- > 0)
{
if (off > (int)OBJPAGELEN)
break;
if (off >= 0)
*pbDst = *pbSrc;
pbSrc++;
pbDst++;
}
return 0;
}
/**
* The LX module interpreter method table.
*/
KLDRMODOPS g_kLdrModLXOps =
{
"LX",
NULL,
kldrModLXCreate,
kldrModLXDestroy,
kldrModLXQuerySymbol,
kldrModLXEnumSymbols,
kldrModLXGetImport,
kldrModLXNumberOfImports,
NULL /* can execute one is optional */,
kldrModLXGetStackInfo,
kldrModLXQueryMainEntrypoint,
NULL /* pfnQueryImageUuid */,
NULL /* fixme */,
NULL /* fixme */,
kldrModLXEnumDbgInfo,
kldrModLXHasDbgInfo,
kldrModLXMap,
kldrModLXUnmap,
kldrModLXAllocTLS,
kldrModLXFreeTLS,
kldrModLXReload,
kldrModLXFixupMapping,
kldrModLXCallInit,
kldrModLXCallTerm,
kldrModLXCallThread,
kldrModLXSize,
kldrModLXGetBits,
kldrModLXRelocateBits,
NULL /* fixme: pfnMostlyDone */,
42 /* the end */
};
|