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
|
/* $Id: VBoxDef2LazyLoad.cpp $ */
/** @file
* VBoxDef2LazyLoad - Lazy Library Loader Generator.
*
* @note Only tested on win.amd64 & darwin.amd64.
*/
/*
* Copyright (C) 2013-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* 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, in version 3 of the
* License.
*
* 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, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iprt/types.h>
#include <iprt/ldr.h> /* For RTLDRARCH. */
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
typedef struct MYEXPORT
{
struct MYEXPORT *pNext;
/** Pointer to unmangled name for stdcall (after szName), NULL if not. */
char *pszUnstdcallName;
/** Pointer to the exported name. */
char const *pszExportedNm;
unsigned uOrdinal;
bool fNoName;
char szName[1];
} MYEXPORT;
typedef MYEXPORT *PMYEXPORT;
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** @name Options
* @{ */
static const char *g_pszOutput = NULL;
static const char *g_pszLibrary = NULL;
static const char *g_apszInputs[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
static unsigned g_cInputs = 0;
static bool g_fIgnoreData = true;
static bool g_fWithExplictLoadFunction = false;
static bool g_fSystemLibrary = false;
#if defined(RT_ARCH_AMD64)
static RTLDRARCH g_enmTarget = RTLDRARCH_AMD64;
#elif defined(RT_ARCH_X86)
static RTLDRARCH g_enmTarget = RTLDRARCH_X86_32;
#elif defined(RT_ARCH_ARM64)
static RTLDRARCH g_enmTarget = RTLDRARCH_ARM64;
#else
# error "Port me!"
#endif
/** @} */
/** Pointer to the export name list head. */
static PMYEXPORT g_pExpHead = NULL;
/** Pointer to the next pointer for insertion. */
static PMYEXPORT *g_ppExpNext = &g_pExpHead;
#if 0 /* unused */
static const char *leftStrip(const char *psz)
{
while (isspace(*psz))
psz++;
return psz;
}
#endif
static char *leftStrip(char *psz)
{
while (isspace(*psz))
psz++;
return psz;
}
static unsigned wordLength(const char *pszWord)
{
unsigned off = 0;
char ch;
while ( (ch = pszWord[off]) != '\0'
&& ch != '='
&& ch != ','
&& ch != ':'
&& !isspace(ch) )
off++;
return off;
}
/**
* Parses the module definition file, collecting export information.
*
* @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE, in the latter case full
* details has been displayed.
* @param pInput The input stream.
*/
static RTEXITCODE parseInputInner(FILE *pInput, const char *pszInput)
{
/*
* Process the file line-by-line.
*/
bool fInExports = false;
unsigned iLine = 0;
char szLine[16384];
while (fgets(szLine, sizeof(szLine), pInput))
{
iLine++;
/*
* Strip leading and trailing spaces from the line as well as
* trailing comments.
*/
char *psz = leftStrip(szLine);
if (*psz == ';')
continue; /* comment line. */
char *pszComment = strchr(psz, ';');
if (pszComment)
*pszComment = '\0';
unsigned cch = (unsigned)strlen(psz);
while (cch > 0 && (isspace(psz[cch - 1]) || psz[cch - 1] == '\r' || psz[cch - 1] == '\n'))
psz[--cch] = '\0';
if (!cch)
continue;
/*
* Check for known directives.
*/
size_t cchWord0 = wordLength(psz);
#define WORD_CMP(pszWord1, cchWord1, szWord2) \
( (cchWord1) == sizeof(szWord2) - 1 && memcmp(pszWord1, szWord2, sizeof(szWord2) - 1) == 0 )
if (WORD_CMP(psz, cchWord0, "EXPORTS"))
{
fInExports = true;
/* In case there is an export on the same line. (Really allowed?) */
psz = leftStrip(psz + sizeof("EXPORTS") - 1);
if (!*psz)
continue;
}
/* Directives that we don't care about, but need to catch in order to
terminate the EXPORTS section in a timely manner. */
else if ( WORD_CMP(psz, cchWord0, "NAME")
|| WORD_CMP(psz, cchWord0, "LIBRARY")
|| WORD_CMP(psz, cchWord0, "DESCRIPTION")
|| WORD_CMP(psz, cchWord0, "STACKSIZE")
|| WORD_CMP(psz, cchWord0, "SECTIONS")
|| WORD_CMP(psz, cchWord0, "SEGMENTS")
|| WORD_CMP(psz, cchWord0, "VERSION")
)
{
fInExports = false;
}
/*
* Process exports:
* entryname[=internalname] [@ordinal[ ][NONAME]] [DATA] [PRIVATE]
*/
if (fInExports)
{
const char *pchName = psz;
unsigned cchName = wordLength(psz);
psz = leftStrip(psz + cchName);
if (*psz == '=')
{
psz = leftStrip(psz + 1);
psz = leftStrip(psz + wordLength(psz));
}
bool fNoName = false;
unsigned uOrdinal = ~0U;
if (*psz == '@')
{
psz++;
if (!isdigit(*psz))
{
fprintf(stderr, "%s:%u: error: Invalid ordinal spec.\n", pszInput, iLine);
return RTEXITCODE_FAILURE;
}
uOrdinal = *psz++ - '0';
while (isdigit(*psz))
{
uOrdinal *= 10;
uOrdinal += *psz++ - '0';
}
psz = leftStrip(psz);
cch = wordLength(psz);
if (WORD_CMP(psz, cch, "NONAME"))
{
fNoName = true;
psz = leftStrip(psz + cch);
}
}
while (*psz)
{
cch = wordLength(psz);
if (WORD_CMP(psz, cch, "DATA"))
{
if (!g_fIgnoreData)
{
fprintf(stderr, "%s:%u: error: Cannot wrap up DATA export '%.*s'.\n",
pszInput, iLine, cchName, pchName);
return RTEXITCODE_SUCCESS;
}
}
else if (!WORD_CMP(psz, cch, "PRIVATE"))
{
fprintf(stderr, "%s:%u: error: Cannot wrap up DATA export '%.*s'.\n",
pszInput, iLine, cchName, pchName);
return RTEXITCODE_SUCCESS;
}
psz = leftStrip(psz + cch);
}
/*
* Check for stdcall mangling.
*/
size_t cbExp = sizeof(MYEXPORT) + cchName;
unsigned cchStdcall = 0;
if (cchName > 3 && *pchName == '_' && isdigit(pchName[cchName - 1]))
{
if (cchName > 3 && pchName[cchName - 2] == '@')
cchStdcall = 2;
else if (cchName > 4 && pchName[cchName - 3] == '@' && isdigit(pchName[cchName - 2]))
cchStdcall = 3;
if (cchStdcall)
cbExp += cchName - 1 - cchStdcall;
}
/*
* Add the export.
*/
PMYEXPORT pExp = (PMYEXPORT)malloc(cbExp);
if (!pExp)
{
fprintf(stderr, "%s:%u: error: Out of memory.\n", pszInput, iLine);
return RTEXITCODE_SUCCESS;
}
memcpy(pExp->szName, pchName, cchName);
pExp->szName[cchName] = '\0';
if (!cchStdcall)
{
pExp->pszUnstdcallName = NULL;
pExp->pszExportedNm = pExp->szName;
}
else
{
pExp->pszUnstdcallName = &pExp->szName[cchName + 1];
memcpy(pExp->pszUnstdcallName, pchName + 1, cchName - 1 - cchStdcall);
pExp->pszUnstdcallName[cchName - 1 - cchStdcall] = '\0';
pExp->pszExportedNm = pExp->pszUnstdcallName;
}
pExp->uOrdinal = uOrdinal;
pExp->fNoName = fNoName;
pExp->pNext = NULL;
*g_ppExpNext = pExp;
g_ppExpNext = &pExp->pNext;
}
}
/*
* Why did we quit the loop, EOF or error?
*/
if (feof(pInput))
return RTEXITCODE_SUCCESS;
fprintf(stderr, "error: Read while reading '%s' (iLine=%u).\n", pszInput, iLine);
return RTEXITCODE_FAILURE;
}
/**
* Parses a_apszInputs, populating the list pointed to by g_pExpHead.
*
* @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE, in the latter case full
* details has been displayed.
*/
static RTEXITCODE parseInputs(void)
{
RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
for (unsigned i = 0; i < g_cInputs; i++)
{
FILE *pInput = fopen(g_apszInputs[i], "r");
if (pInput)
{
RTEXITCODE rcExit2 = parseInputInner(pInput, g_apszInputs[i]);
fclose(pInput);
if (rcExit2 == RTEXITCODE_SUCCESS && !g_pExpHead)
{
fprintf(stderr, "error: Found no exports in '%s'.\n", g_apszInputs[i]);
rcExit2 = RTEXITCODE_FAILURE;
}
if (rcExit2 != RTEXITCODE_SUCCESS)
rcExit = rcExit2;
}
else
{
fprintf(stderr, "error: Failed to open '%s' for reading.\n", g_apszInputs[i]);
rcExit = RTEXITCODE_FAILURE;
}
}
return rcExit;
}
/**
* Generates the assembly source code for AMD64 and x86, writing it
* to @a pOutput.
*
* @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE, in the latter case full
* details has been displayed.
* @param pOutput The output stream (caller checks it for errors
* when closing).
*/
static RTEXITCODE generateOutputInnerX86AndAMD64(FILE *pOutput)
{
fprintf(pOutput, ";;\n");
for (unsigned i = 0; i < g_cInputs; i++)
fprintf(pOutput, ";; Autogenerated from '%s'.\n", g_apszInputs[i]);
fprintf(pOutput,
";; DO NOT EDIT!\n"
";;\n"
"\n"
"\n"
"%%include \"iprt/asmdefs.mac\"\n"
"\n"
"\n");
/*
* Put the thunks first for alignment and other reasons. It's the hot part of the code.
*/
fprintf(pOutput,
";\n"
"; Thunks.\n"
";\n"
"BEGINCODE\n");
for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
if (!pExp->pszUnstdcallName)
fprintf(pOutput,
"BEGINPROC %s\n"
" jmp RTCCPTR_PRE [g_pfn%s xWrtRIP]\n"
"ENDPROC %s\n",
pExp->szName, pExp->szName, pExp->szName);
else
fprintf(pOutput,
"%%ifdef RT_ARCH_X86\n"
"global %s\n"
"%s:\n"
" jmp RTCCPTR_PRE [g_pfn%s xWrtRIP]\n"
"%%else\n"
"BEGINPROC %s\n"
" jmp RTCCPTR_PRE [g_pfn%s xWrtRIP]\n"
"ENDPROC %s\n"
"%%endif\n",
pExp->szName, pExp->szName, pExp->pszUnstdcallName,
pExp->pszUnstdcallName, pExp->pszUnstdcallName, pExp->pszUnstdcallName);
fprintf(pOutput,
"\n"
"\n");
/*
* Import pointers
*/
fprintf(pOutput,
";\n"
"; Import pointers. Initialized to point a lazy loading stubs.\n"
";\n"
"BEGINDATA\n"
"g_apfnImports:\n");
for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
if (pExp->pszUnstdcallName)
fprintf(pOutput,
"%%ifdef ASM_FORMAT_PE\n"
" %%ifdef RT_ARCH_X86\n"
"global __imp_%s\n"
"__imp_%s:\n"
" %%else\n"
"global __imp_%s\n"
"__imp_%s:\n"
" %%endif\n"
"%%endif\n"
"g_pfn%s RTCCPTR_DEF ___LazyLoad___%s\n"
"\n",
pExp->szName,
pExp->szName,
pExp->pszUnstdcallName,
pExp->pszUnstdcallName,
pExp->pszExportedNm,
pExp->pszExportedNm);
else
fprintf(pOutput,
"%%ifdef ASM_FORMAT_PE\n"
"global __imp_%s\n"
"__imp_%s:\n"
"%%endif\n"
"g_pfn%s RTCCPTR_DEF ___LazyLoad___%s\n"
"\n",
pExp->szName,
pExp->szName,
pExp->pszExportedNm,
pExp->pszExportedNm);
fprintf(pOutput,
"RTCCPTR_DEF 0 ; Terminator entry for traversal.\n"
"\n"
"\n");
/*
* Now for the less important stuff, starting with the names.
*
* We keep the names separate so we can traverse them in parallel to
* g_apfnImports in the load-everything routine further down.
*/
fprintf(pOutput,
";\n"
"; Imported names.\n"
";\n"
"BEGINCODE\n"
"g_szLibrary: db '%s',0\n"
"\n"
"g_szzNames:\n",
g_pszLibrary);
for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
if (!pExp->fNoName)
fprintf(pOutput, " g_sz%s:\n db '%s',0\n", pExp->pszExportedNm, pExp->pszExportedNm);
else
fprintf(pOutput, " g_sz%s:\n db '#%u',0\n", pExp->pszExportedNm, pExp->uOrdinal);
fprintf(pOutput,
"g_EndOfNames: db 0\n"
"\n"
"g_szFailLoadFmt: db 'Lazy loader failed to load \"%%s\": %%Rrc', 10, 0\n"
"g_szFailResolveFmt: db 'Lazy loader failed to resolve symbol \"%%s\" in \"%%s\": %%Rrc', 10, 0\n"
"\n"
"\n");
/*
* The per import lazy load code.
*/
fprintf(pOutput,
";\n"
"; Lazy load+resolve stubs.\n"
";\n"
"BEGINCODE\n");
for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
{
if (!pExp->fNoName)
fprintf(pOutput,
"___LazyLoad___%s:\n"
/* "int3\n" */
"%%ifdef RT_ARCH_AMD64\n"
" lea rax, [g_sz%s wrt rip]\n"
" lea r10, [g_pfn%s wrt rip]\n"
" call LazyLoadResolver\n"
"%%elifdef RT_ARCH_X86\n"
" push g_sz%s\n"
" push g_pfn%s\n"
" call LazyLoadResolver\n"
" add esp, 8h\n"
"%%else\n"
" %%error \"Unsupported architecture\"\n"
"%%endif\n"
,
pExp->pszExportedNm,
pExp->pszExportedNm,
pExp->pszExportedNm,
pExp->pszExportedNm,
pExp->pszExportedNm);
else
fprintf(pOutput,
"___LazyLoad___%s:\n"
/* "int3\n" */
"%%ifdef RT_ARCH_AMD64\n"
" mov eax, %u\n"
" lea r10, [g_pfn%s wrt rip]\n"
" call LazyLoadResolver\n"
"%%elifdef RT_ARCH_X86\n"
" push %u\n"
" push g_pfn%s\n"
" call LazyLoadResolver\n"
" add esp, 8h\n"
"%%else\n"
" %%error \"Unsupported architecture\"\n"
"%%endif\n"
,
pExp->pszExportedNm,
pExp->uOrdinal,
pExp->pszExportedNm,
pExp->uOrdinal,
pExp->pszExportedNm);
if (!pExp->pszUnstdcallName)
fprintf(pOutput, " jmp NAME(%s)\n", pExp->szName);
else
fprintf(pOutput,
"%%ifdef RT_ARCH_X86\n"
" jmp %s\n"
"%%else\n"
" jmp NAME(%s)\n"
"%%endif\n"
,
pExp->szName, pExp->pszUnstdcallName);
fprintf(pOutput, "\n");
}
fprintf(pOutput,
"\n"
"\n"
"\n");
/*
* The code that does the loading and resolving.
*/
fprintf(pOutput,
";\n"
"; The module handle.\n"
";\n"
"BEGINDATA\n"
"g_hMod RTCCPTR_DEF 0\n"
"\n"
"\n"
"\n");
/*
* How we load the module needs to be selectable later on.
*
* The LazyLoading routine returns the module handle in RCX/ECX, caller
* saved all necessary registers.
*/
if (!g_fSystemLibrary)
fprintf(pOutput,
";\n"
";SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod,\n"
"; uint32_t fFlags, PRTERRINFO pErrInfo);\n"
";\n"
"EXTERN_IMP2 SUPR3HardenedLdrLoadAppPriv\n"
"%%ifdef IN_RT_R3\n"
"extern NAME(RTAssertMsg2Weak)\n"
"%%else\n"
"EXTERN_IMP2 RTAssertMsg2Weak\n"
"%%endif\n"
"BEGINCODE\n"
"\n"
"LazyLoading:\n"
" mov xCX, [g_hMod xWrtRIP]\n"
" or xCX, xCX\n"
" jnz .return\n"
"\n"
"%%ifdef ASM_CALL64_GCC\n"
" xor rcx, rcx ; pErrInfo\n"
" xor rdx, rdx ; fFlags (local load)\n"
" lea rsi, [g_hMod wrt rip] ; phLdrMod\n"
" lea rdi, [g_szLibrary wrt rip] ; pszFilename\n"
" sub rsp, 08h\n"
" call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
" add rsp, 08h\n"
"\n"
"%%elifdef ASM_CALL64_MSC\n"
" xor r9, r9 ; pErrInfo\n"
" xor r8, r8 ; fFlags (local load)\n"
" lea rdx, [g_hMod wrt rip] ; phLdrMod\n"
" lea rcx, [g_szLibrary wrt rip] ; pszFilename\n"
" sub rsp, 28h\n"
" call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
" add rsp, 28h\n"
"\n"
"%%elifdef RT_ARCH_X86\n"
" sub xSP, 0ch\n"
" push 0 ; pErrInfo\n"
" push 0 ; fFlags (local load)\n"
" push g_hMod ; phLdrMod\n"
" push g_szLibrary ; pszFilename\n"
" call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
" add esp, 1ch\n"
"%%else\n"
" %%error \"Unsupported architecture\"\n"
"%%endif\n");
else
fprintf(pOutput,
";\n"
"; RTDECL(int) RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod);\n"
";\n"
"%%ifdef IN_RT_R3\n"
"extern NAME(RTLdrLoadSystem)\n"
"extern NAME(RTAssertMsg2Weak)\n"
"%%else\n"
"EXTERN_IMP2 RTLdrLoadSystem\n"
"EXTERN_IMP2 RTAssertMsg2Weak\n"
"%%endif\n"
"BEGINCODE\n"
"\n"
"LazyLoading:\n"
" mov xCX, [g_hMod xWrtRIP]\n"
" or xCX, xCX\n"
" jnz .return\n"
"\n"
"%%ifdef ASM_CALL64_GCC\n"
" lea rdx, [g_hMod wrt rip] ; phLdrMod\n"
" mov esi, 1 ; fNoUnload=true\n"
" lea rdi, [g_szLibrary wrt rip] ; pszFilename\n"
" sub rsp, 08h\n"
" %%ifdef IN_RT_R3\n"
" call NAME(RTLdrLoadSystem)\n"
" %%else\n"
" call IMP2(RTLdrLoadSystem)\n"
" %%endif\n"
" add rsp, 08h\n"
"\n"
"%%elifdef ASM_CALL64_MSC\n"
" lea r8, [g_hMod wrt rip] ; phLdrMod\n"
" mov edx, 1 ; fNoUnload=true\n"
" lea rcx, [g_szLibrary wrt rip] ; pszFilename\n"
" sub rsp, 28h\n"
" %%ifdef IN_RT_R3\n"
" call NAME(RTLdrLoadSystem)\n"
" %%else\n"
" call IMP2(RTLdrLoadSystem)\n"
" %%endif\n"
" add rsp, 28h\n"
"\n"
"%%elifdef RT_ARCH_X86\n"
" push g_hMod ; phLdrMod\n"
" push 1 ; fNoUnload=true\n"
" push g_szLibrary ; pszFilename\n"
" %%ifdef IN_RT_R3\n"
" call NAME(RTLdrLoadSystem)\n"
" %%else\n"
" call IMP2(RTLdrLoadSystem)\n"
" %%endif\n"
" add esp, 0ch\n"
"%%else\n"
" %%error \"Unsupported architecture\"\n"
"%%endif\n");
fprintf(pOutput,
" or eax, eax\n"
" jnz .badload\n"
" mov xCX, [g_hMod xWrtRIP]\n"
".return:\n"
" ret\n"
"\n"
".badload:\n"
"%%ifdef ASM_CALL64_GCC\n"
" mov edx, eax\n"
" lea rsi, [g_szLibrary wrt rip]\n"
" lea rdi, [g_szFailLoadFmt wrt rip]\n"
" sub rsp, 08h\n"
"%%elifdef ASM_CALL64_MSC\n"
" mov r8d, eax\n"
" lea rdx, [g_szLibrary wrt rip]\n"
" lea rcx, [g_szFailLoadFmt wrt rip]\n"
" sub rsp, 28h\n"
"%%elifdef RT_ARCH_X86\n"
" push eax\n"
" push g_szLibrary\n"
" push g_szFailLoadFmt\n"
"%%endif\n"
"%%ifdef IN_RT_R3\n"
" call NAME(RTAssertMsg2Weak)\n"
"%%else\n"
" call IMP2(RTAssertMsg2Weak)\n"
"%%endif\n"
".badloadloop:\n"
" int3\n"
" jmp .badloadloop\n"
"LazyLoading_End:\n"
"\n"
"\n");
fprintf(pOutput,
";\n"
";RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue);\n"
";\n"
"%%ifdef IN_RT_R3\n"
"extern NAME(RTLdrGetSymbol)\n"
"%%else\n"
"EXTERN_IMP2 RTLdrGetSymbol\n"
"%%endif\n"
"BEGINCODE\n"
"LazyLoadResolver:\n"
"%%ifdef RT_ARCH_AMD64\n"
" push rbp\n"
" mov rbp, rsp\n"
" push r15\n"
" push r14\n"
" mov r15, rax ; name\n"
" mov r14, r10 ; ppfn\n"
" push r9\n"
" push r8\n"
" push rcx\n"
" push rdx\n"
" push r12\n"
" %%ifdef ASM_CALL64_GCC\n"
" push rsi\n"
" push rdi\n"
" mov r12, rsp\n"
" %%else\n"
" mov r12, rsp\n"
" sub rsp, 20h\n"
" %%endif\n"
" and rsp, 0fffffff0h ; Try make sure the stack is aligned\n"
"\n"
" call LazyLoading ; returns handle in rcx\n"
" %%ifdef ASM_CALL64_GCC\n"
" mov rdi, rcx ; hLdrMod\n"
" mov rsi, r15 ; pszSymbol\n"
" mov rdx, r14 ; ppvValue\n"
" %%else\n"
" mov rdx, r15 ; pszSymbol\n"
" mov r8, r14 ; ppvValue\n"
" %%endif\n"
" %%ifdef IN_RT_R3\n"
" call NAME(RTLdrGetSymbol)\n"
" %%else\n"
" call IMP2(RTLdrGetSymbol)\n"
" %%endif\n"
" or eax, eax\n"
" jnz .badsym\n"
"\n"
" mov rsp, r12\n"
" %%ifdef ASM_CALL64_GCC\n"
" pop rdi\n"
" pop rsi\n"
" %%endif\n"
" pop r12\n"
" pop rdx\n"
" pop rcx\n"
" pop r8\n"
" pop r9\n"
" pop r14\n"
" pop r15\n"
" leave\n"
"\n"
"%%elifdef RT_ARCH_X86\n"
" push ebp\n"
" mov ebp, esp\n"
" push eax\n"
" push ecx\n"
" push edx\n"
" and esp, 0fffffff0h\n"
"\n"
".loaded:\n"
" call LazyLoading ; returns handle in ecx\n"
" push dword [ebp + 8] ; value addr\n"
" push dword [ebp + 12] ; symbol name\n"
" push ecx\n"
" %%ifdef IN_RT_R3\n"
" call NAME(RTLdrGetSymbol)\n"
" %%else\n"
" call IMP2(RTLdrGetSymbol)\n"
" %%endif\n"
" or eax, eax\n"
" jnz .badsym\n"
" lea esp, [ebp - 0ch]\n"
" pop edx\n"
" pop ecx\n"
" pop eax\n"
" leave\n"
"%%else\n"
" %%error \"Unsupported architecture\"\n"
"%%endif\n"
" ret\n"
"\n"
".badsym:\n"
"%%ifdef ASM_CALL64_GCC\n"
" mov ecx, eax\n"
" lea rdx, [g_szLibrary wrt rip]\n"
" mov rsi, r15\n"
" lea rdi, [g_szFailResolveFmt wrt rip]\n"
" sub rsp, 08h\n"
"%%elifdef ASM_CALL64_MSC\n"
" mov r9d, eax\n"
" mov r8, r15\n"
" lea rdx, [g_szLibrary wrt rip]\n"
" lea rcx, [g_szFailResolveFmt wrt rip]\n"
" sub rsp, 28h\n"
"%%elifdef RT_ARCH_X86\n"
" push eax\n"
" push dword [ebp + 12]\n"
" push g_szLibrary\n"
" push g_szFailResolveFmt\n"
"%%endif\n"
"%%ifdef IN_RT_R3\n"
" call NAME(RTAssertMsg2Weak)\n"
"%%else\n"
" call IMP2(RTAssertMsg2Weak)\n"
"%%endif\n"
".badsymloop:\n"
" int3\n"
" jmp .badsymloop\n"
"\n"
"LazyLoadResolver_End:\n"
"\n"
"\n"
);
/*
* C callable method for explicitly loading the library and optionally
* resolving all the imports.
*/
if (g_fWithExplictLoadFunction)
{
if (g_fSystemLibrary) /* Lazy bird. */
{
fprintf(stderr, "error: cannot use --system with --explicit-load-function, sorry\n");
return RTEXITCODE_FAILURE;
}
int cchLibBaseName = (int)(strchr(g_pszLibrary, '.') ? strchr(g_pszLibrary, '.') - g_pszLibrary : strlen(g_pszLibrary));
fprintf(pOutput,
";;\n"
"; ExplicitlyLoad%.*s(bool fResolveAllImports, pErrInfo);\n"
";\n"
"EXTERN_IMP2 RTErrInfoSet\n"
"BEGINCODE\n"
"BEGINPROC ExplicitlyLoad%.*s\n"
" push xBP\n"
" mov xBP, xSP\n"
" push xBX\n"
"%%ifdef ASM_CALL64_GCC\n"
" %%define pszCurStr r14\n"
" push r14\n"
"%%else\n"
" %%define pszCurStr xDI\n"
" push xDI\n"
"%%endif\n"
" sub xSP, 40h\n"
"\n"
" ;\n"
" ; Save parameters on stack (64-bit only).\n"
" ;\n"
"%%ifdef ASM_CALL64_GCC\n"
" mov [xBP - xCB * 3], rdi ; fResolveAllImports\n"
" mov [xBP - xCB * 4], rsi ; pErrInfo\n"
"%%elifdef ASM_CALL64_MSC\n"
" mov [xBP - xCB * 3], rcx ; fResolveAllImports\n"
" mov [xBP - xCB * 4], rdx ; pErrInfo\n"
"%%endif\n"
"\n"
" ;\n"
" ; Is the module already loaded?\n"
" ;\n"
" cmp RTCCPTR_PRE [g_hMod xWrtRIP], 0\n"
" jnz .loaded\n"
"\n"
" ;\n"
" ; Load the module.\n"
" ;\n"
"%%ifdef ASM_CALL64_GCC\n"
" mov rcx, [xBP - xCB * 4] ; pErrInfo\n"
" xor rdx, rdx ; fFlags (local load)\n"
" lea rsi, [g_hMod wrt rip] ; phLdrMod\n"
" lea rdi, [g_szLibrary wrt rip] ; pszFilename\n"
" call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
"\n"
"%%elifdef ASM_CALL64_MSC\n"
" mov r9, [xBP - xCB * 4] ; pErrInfo\n"
" xor r8, r8 ; fFlags (local load)\n"
" lea rdx, [g_hMod wrt rip] ; phLdrMod\n"
" lea rcx, [g_szLibrary wrt rip] ; pszFilename\n"
" call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
"\n"
"%%elifdef RT_ARCH_X86\n"
" sub xSP, 0ch\n"
" push dword [xBP + 12] ; pErrInfo\n"
" push 0 ; fFlags (local load)\n"
" push g_hMod ; phLdrMod\n"
" push g_szLibrary ; pszFilename\n"
" call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
" add esp, 1ch\n"
"%%else\n"
" %%error \"Unsupported architecture\"\n"
"%%endif\n"
" or eax, eax\n"
" jnz .return\n"
"\n"
" ;\n"
" ; Resolve the imports too if requested to do so.\n"
" ;\n"
".loaded:\n"
"%%ifdef ASM_ARCH_X86\n"
" cmp byte [xBP + 8], 0\n"
"%%else\n"
" cmp byte [xBP - xCB * 3], 0\n"
"%%endif\n"
" je .return\n"
"\n"
" lea pszCurStr, [g_szzNames xWrtRIP]\n"
" lea xBX, [g_apfnImports xWrtRIP]\n"
".next_import:\n"
" cmp RTCCPTR_PRE [xBX], 0\n"
" je .return\n"
"%%ifdef ASM_CALL64_GCC\n"
" mov rdx, xBX ; ppvValue\n"
" mov rsi, pszCurStr ; pszSymbol\n"
" mov rdi, [g_hMod wrt rip] ; hLdrMod\n"
" call IMP2(RTLdrGetSymbol)\n"
"%%elifdef ASM_CALL64_MSC\n"
" mov r8, xBX ; ppvValue\n"
" mov rdx, pszCurStr ; pszSymbol\n"
" mov rcx, [g_hMod wrt rip] ; pszSymbol\n"
" call IMP2(RTLdrGetSymbol)\n"
"%%else\n"
" push xBX ; ppvValue\n"
" push pszCurStr ; pszSymbol\n"
" push RTCCPTR_PRE [g_hMod] ; hLdrMod\n"
" call IMP2(RTLdrGetSymbol)\n"
" add xSP, 0ch\n"
"%%endif\n"
" or eax, eax\n"
" jnz .symbol_error\n"
"\n"
" ; Advance.\n"
" add xBX, RTCCPTR_CB\n"
" xor eax, eax\n"
" mov xCX, 0ffffffffh\n"
"%%ifdef ASM_CALL64_GCC\n"
" mov xDI, pszCurStr\n"
" repne scasb\n"
" mov pszCurStr, xDI\n"
"%%else\n"
" repne scasb\n"
"%%endif\n"
" jmp .next_import\n"
"\n"
" ;\n"
" ; Error loading a symbol. Call RTErrInfoSet on pErrInfo (preserves eax).\n"
" ;\n"
".symbol_error:\n"
"%%ifdef ASM_CALL64_GCC\n"
" mov rdx, pszCurStr ; pszMsg\n"
" mov esi, eax ; rc\n"
" mov rdi, [xBP - xCB * 4] ; pErrInfo\n"
" call IMP2(RTErrInfoSet)\n"
"%%elifdef ASM_CALL64_MSC\n"
" mov r8, pszCurStr ; pszMsg\n"
" mov edx, eax ; rc\n"
" mov rcx, [xBP - xCB * 4] ; pErrInfo\n"
" call IMP2(RTErrInfoSet)\n"
"%%else\n"
" push pszCurStr ; pszMsg\n"
" push eax ; pszSymbol\n"
" push dword [xBP + 0ch] ; pErrInfo\n"
" call IMP2(RTErrInfoSet)\n"
" add xSP, 0ch\n"
"%%endif\n"
" "
"\n"
".return:\n"
" mov pszCurStr, [xBP - xCB * 2]\n"
" mov xBX, [xBP - xCB * 1]\n"
" leave\n"
" ret\n"
"ENDPROC ExplicitlyLoad%.*s\n"
"\n"
"\n"
,
cchLibBaseName, g_pszLibrary,
cchLibBaseName, g_pszLibrary,
cchLibBaseName, g_pszLibrary
);
}
return RTEXITCODE_SUCCESS;
}
/**
* Generates the assembly source code for ARM64, writing it
* to @a pOutput.
*
* @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE, in the latter case full
* details has been displayed.
* @param pOutput The output stream (caller checks it for errors
* when closing).
*/
static RTEXITCODE generateOutputInnerArm64(FILE *pOutput)
{
// bool fMachO = true;
// bool fDarwin = true;
const char *pszNmPfx = "_";
fprintf(pOutput, ";;\n");
for (unsigned i = 0; i < g_cInputs; i++)
fprintf(pOutput, ";; Autogenerated from '%s'.\n", g_apszInputs[i]);
fprintf(pOutput,
";; DO NOT EDIT!\n"
";;\n"
"\n"
"\n"
/*"%%include \"iprt/asmdefs.mac\"\n"*/
"\n"
"\n");
/*
* Put the thunks first for alignment and other reasons. It's the hot part of the code.
*/
fprintf(pOutput,
";\n"
"; Thunks.\n"
";\n"
".section __TEXT,__text,regular,pure_instructions\n");
for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
fprintf(pOutput,
".p2align 3\n"
".globl %s%s\n"
"%s%s:\n"
" adrp x9, %sg_pfn%s@PAGE\n"
" ldr x9, [x9, %sg_pfn%s@PAGEOFF]\n"
" br x9\n",
pszNmPfx, pExp->szName, pszNmPfx, pExp->szName, pszNmPfx, pExp->szName, pszNmPfx, pExp->szName);
fprintf(pOutput,
"\n"
"\n");
/*
* Import pointers
*/
fprintf(pOutput,
";\n"
"; Import pointers. Initialized to point a lazy loading stubs.\n"
";\n"
".section __DATA,__data\n"
".p2align 3\n"
"g_apfnImports:\n");
for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
fprintf(pOutput,
".globl __imp_%s\n"
"__imp_%s:\n"
".globl %sg_pfn%s\n"
"%sg_pfn%s:\n"
" .quad ___LazyLoad___%s\n"
"\n",
pExp->szName, pExp->szName,
pszNmPfx, pExp->szName, pszNmPfx, pExp->szName,
pExp->pszExportedNm);
fprintf(pOutput,
" .quad 0 ; Terminator entry for traversal.\n"
"\n"
"\n");
/*
* Now for the less important stuff, starting with the names.
*
* We keep the names separate so we can traverse them in parallel to
* g_apfnImports in the load-everything routine further down.
*/
fprintf(pOutput,
";\n"
"; Imported names.\n"
";\n"
".section __TEXT,__cstring,cstring_literals\n"
"g_szLibrary:\n"
" .asciz \"%s\"\n"
"\n"
"g_szzNames:\n",
g_pszLibrary);
for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
if (!pExp->fNoName)
fprintf(pOutput, " g_sz%s:\n .asciz \"%s\"\n", pExp->pszExportedNm, pExp->pszExportedNm);
else
fprintf(pOutput, " g_sz%s:\n .asciz \"#%u\"\n", pExp->pszExportedNm, pExp->uOrdinal);
fprintf(pOutput,
"g_EndOfNames: .byte 0\n"
"\n"
"g_szFailLoadFmt: .asciz \"Lazy loader failed to load \\\"%%s\\\": %%Rrc\\n\"\n"
"g_szFailResolveFmt: .asciz \"Lazy loader failed to resolve symbol \\\"%%s\\\" in \\\"%%s\\\": %%Rrc\\n\"\n"
"\n"
"\n");
/*
* The per import lazy load code.
*/
fprintf(pOutput,
";\n"
"; Lazy load+resolve stubs.\n"
";\n"
".section __TEXT,__text,regular,pure_instructions\n"
".p2align 3\n");
for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
{
if (!pExp->fNoName)
fprintf(pOutput,
"___LazyLoad___%s:\n"
" adrp x9, g_sz%s@PAGE\n"
" add x9, x9, g_sz%s@PAGEOFF\n"
" adrp x10, %sg_pfn%s@PAGE\n"
" add x10, x10, %sg_pfn%s@PAGEOFF\n"
" bl LazyLoadResolver\n"
, pExp->pszExportedNm,
pExp->pszExportedNm, pExp->pszExportedNm,
pszNmPfx, pExp->pszExportedNm, pszNmPfx, pExp->pszExportedNm);
else
fprintf(pOutput,
"___LazyLoad___%s:\n"
" movk w9, #%u\n"
" adrp x10, %sg_pfn%s@PAGE\n"
" add x10, x10, %sg_pfn%s@PAGEOFF\n"
, pExp->pszExportedNm,
pExp->uOrdinal,
pszNmPfx, pExp->pszExportedNm, pszNmPfx, pExp->pszExportedNm);
fprintf(pOutput, " b %s%s\n", pszNmPfx, pExp->szName);
fprintf(pOutput, "\n");
}
fprintf(pOutput,
"\n"
"\n"
"\n");
/*
* The code that does the loading and resolving.
*/
fprintf(pOutput,
";\n"
"; The module handle.\n"
";\n"
".section __DATA,__data\n"
"g_hMod:\n"
" .quad 0\n"
"\n"
"\n"
"\n");
/*
* Common lazy loader and resolved.
*/
fprintf(pOutput,
";\n"
"; The resolver code.\n"
";\n"
".section __TEXT,__text,regular,pure_instructions\n"
".p2align 3\n"
"LazyLoadResolver:\n"
" .cfi_startproc\n"
" ; Create frame.\n"
" sub sp, sp, #(16 + 192)\n"
" stp x29, x30, [sp, #192]\n"
" add x29, sp, #192\n"
" .cfi_def_cfa x29, 16\n"
" .cfi_offset x30, -8\n"
" .cfi_offset x29, -16\n"
" ; Save all argument registers and a handful of preserved ones.\n"
" stp x0, x1, [sp, #(192 - 16)]\n"
" .cfi_offset x0, -32\n"
" .cfi_offset x1, -24\n"
" stp x2, x3, [sp, #(192 - 32)]\n"
" .cfi_offset x3, -40\n"
" .cfi_offset x2, -48\n"
" stp x4, x5, [sp, #(192 - 48)]\n"
" .cfi_offset x6, -56\n"
" .cfi_offset x5, -64\n"
" stp x6, x7, [sp, #(192 - 64)]\n"
" .cfi_offset x7, -72\n"
" .cfi_offset x6, -80\n"
" stp x16, x17, [sp, #(192 - 80)]\n"
" .cfi_offset x17, -88\n"
" .cfi_offset x16, -96\n"
" stp x18, x19, [sp, #(192 - 96)]\n"
" .cfi_offset x19, -104\n"
" .cfi_offset x18, -112\n"
" stp x20, x21, [sp, #(192 - 112)]\n"
" .cfi_offset x21, -120\n"
" .cfi_offset x20, -128\n"
" stp x22, x23, [sp, #(192 - 128)]\n"
" .cfi_offset x23, -136\n"
" .cfi_offset x22, -144\n"
" str x8, [sp, #(192 - 144)]\n"
"\n"
" ; Shift the symbol name to x19 and g_pfnXXXX pointer to x20 as these are preserved registers\n"
" ; (in case we need to call LazyLoadModule/RTLdrLoad)\n"
" mov x19, x9\n"
" mov x20, x10\n"
"\n"
" ; Get the module handle and call RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)\n"
" adrp x0, g_hMod@PAGE\n"
" ldr x0, [x0, g_hMod@PAGEOFF]\n"
" cmp x0, #0\n"
" b.eq LazyLoading\n"
" mov x1, x19\n"
" mov x2, x20\n"
" bl %sRTLdrGetSymbol\n"
"\n"
" cmp w0, #0\n"
" b.eq Lreturn\n"
"\n"
"Lbadsym: ; Call sRTAssertMsg2Weak. Variadic (...) arguments are passed on the stack it seems.\n"
" mov x3, x0\n"
" adrp x2, g_szLibrary@PAGE\n"
" add x2, x2, g_szLibrary@PAGEOFF\n"
" mov x1, x19\n"
" adrp x0, g_szFailLoadFmt@PAGE\n"
" add x0, x0, g_szFailLoadFmt@PAGEOFF\n"
" stp x1, x2, [sp]\n"
" str x3, [sp, #16]\n"
" bl %sRTAssertMsg2Weak\n"
"Lbadsymloop:\n"
" brk #0x1\n"
" b Lbadsymloop\n"
"Lreturn:\n"
" ; Restore saved register\n"
" ldr x8, [sp, #(192 - 144)]\n"
" .cfi_restore x8\n"
" ldp x22, x23, [sp, #(192 - 128)]\n"
" .cfi_restore x23\n"
" .cfi_restore x22\n"
" ldp x20, x21, [sp, #(192 - 112)]\n"
" .cfi_restore x21\n"
" .cfi_restore x20\n"
" ldp x18, x19, [sp, #(192 - 96)]\n"
" .cfi_restore x19\n"
" .cfi_restore x18\n"
" ldp x16, x17, [sp, #(192 - 80)]\n"
" .cfi_restore x17\n"
" .cfi_restore x18\n"
" ldp x6, x7, [sp, #(192 - 64)]\n"
" .cfi_restore x7\n"
" .cfi_restore x6\n"
" ldp x4, x5, [sp, #(192 - 48)]\n"
" .cfi_restore x5\n"
" .cfi_restore x4\n"
" ldp x2, x3, [sp, #(192 - 32)]\n"
" .cfi_restore x3\n"
" .cfi_restore x2\n"
" ldp x0, x1, [sp, #(192 - 16)]\n"
" .cfi_restore x1\n"
" .cfi_restore x0\n"
"\n"
" ldp x29, x30, [sp, #192]\n"
" .cfi_restore x29\n"
" .cfi_restore x30\n"
" add sp, sp, #(16 + 192)\n"
" ret\n"
" .cfi_endproc\n"
"\n"
"\n"
, pszNmPfx, pszNmPfx);
fprintf(pOutput,
";\n"
"; Loads the module.\n"
"; ASSUMES called from LazyLoadResolver where all relevant registers are already saved.\n"
";\n"
"LazyLoading:\n"
" .cfi_startproc\n"
" ; Create frame.\n"
" sub sp, sp, #(16 + 48)\n"
" stp x29, x30, [sp, #48]\n"
" add x29, sp, #48\n"
" .cfi_def_cfa x29, 16\n"
" .cfi_offset x30, -8\n"
" .cfi_offset x29, -16\n"
"\n");
if (!g_fSystemLibrary)
fprintf(pOutput,
" ; Call SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);\n"
" mov x3, #0\n"
" mov x2, #0\n"
" adrp x1, g_hMod@PAGE\n"
" add x1, x1, g_hMod@PAGEOFF\n"
" adrp x0, g_szLibrary@PAGE\n"
" add x0, x0, g_szLibrary@PAGEOFF\n"
" bl %sSUPR3HardenedLdrLoadAppPriv\n"
, pszNmPfx);
else
fprintf(pOutput,
" ; Call RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod);\n"
" adrp x2, g_hMod@PAGE\n"
" add x2, x2, g_hMod@PAGEOFF\n"
" mov x1, #1\n"
" adrp x0, g_szLibrary@PAGE\n"
" add x0, x0, g_szLibrary@PAGEOFF\n"
" bl %sRTLdrLoadSystem\n"
, pszNmPfx);
fprintf(pOutput,
" cmp w0, #0\n"
" b.eq Lload_return\n"
"\n"
"Lbadload: ; Call sRTAssertMsg2Weak. Variadic (...) arguments are passed on the stack it seems.\n"
" mov x2, x0\n"
" adrp x1, g_szLibrary@PAGE\n"
" add x1, x1, g_szLibrary@PAGEOFF\n"
" adrp x0, g_szFailResolveFmt@PAGE\n"
" add x0, x0, g_szFailResolveFmt@PAGEOFF\n"
" stp x1, x2, [sp]\n"
" bl %sRTAssertMsg2Weak\n"
"Lbadloadloop:\n"
" brk #0x1\n"
" b Lbadloadloop\n"
"Lload_return:\n"
" adrp x0, g_hMod@PAGE\n"
" ldr x0, [x0, g_hMod@PAGEOFF]\n"
" ldp x29, x30, [sp, #48]\n"
" .cfi_restore x29\n"
" .cfi_restore x30\n"
" add sp, sp, #(16 + 48)\n"
" ret\n"
" .cfi_endproc\n"
"\n"
"\n"
, pszNmPfx);
/*
* C callable method for explicitly loading the library and optionally
* resolving all the imports.
*/
if (g_fWithExplictLoadFunction)
{
if (g_fSystemLibrary) /* Lazy bird. */
{
fprintf(stderr, "error: cannot use --system with --explicit-load-function, sorry\n");
return RTEXITCODE_FAILURE;
}
int cchLibBaseName = (int)(strchr(g_pszLibrary, '.') ? strchr(g_pszLibrary, '.') - g_pszLibrary : strlen(g_pszLibrary));
fprintf(pOutput,
";;\n"
"; ExplicitlyLoad%.*s(bool fResolveAllImports, pErrInfo);\n"
";\n"
".section __TEXT,__text,regular,pure_instructions\n"
".p2align 3\n"
".globl %sExplicitlyLoad%.*s\n"
"%sExplicitlyLoad%.*s:\n"
" .cfi_startproc\n"
" ; Create frame.\n"
" sub sp, sp, #(16 + 96)\n"
" stp x29, x30, [sp, #96]\n"
" add x29, sp, #96\n"
" .cfi_def_cfa x29, 16\n"
" .cfi_offset x30, -8\n"
" .cfi_offset x29, -16\n"
"\n"
" stp x20, x21, [sp, #(96 - 16)]\n"
" .cfi_offset x21, -24\n"
" .cfi_offset x20, -32\n"
" stp x22, x23, [sp, #(96 - 32)]\n"
" .cfi_offset x23, -40\n"
" .cfi_offset x22, -48\n"
" ; Save the input parameters.\n"
" mov x20, x0\n"
" mov x21, x1\n"
"\n"
" ;\n"
" ; Is the module already loaded?\n"
" ;\n"
" adrp x0, g_hMod@PAGE\n"
" ldr x0, [x0, g_hMod@PAGEOFF]\n"
" cmp x0, #0\n"
" b.ne Lexplicit_loaded_module\n"
"\n"
,
cchLibBaseName, g_pszLibrary,
pszNmPfx, cchLibBaseName, g_pszLibrary,
pszNmPfx, cchLibBaseName, g_pszLibrary);
fprintf(pOutput,
"Lexplicit_load_module:\n"
" ; Call SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);\n"
" mov x3, #0\n"
" mov x2, #0\n"
" adrp x1, g_hMod@PAGE\n"
" add x1, x1, g_hMod@PAGEOFF\n"
" adrp x0, g_szLibrary@PAGE\n"
" add x0, x0, g_szLibrary@PAGEOFF\n"
" bl %sSUPR3HardenedLdrLoadAppPriv\n"
" cmp x0, #0\n"
" b.ne Lexplicit_load_return\n"
"\n"
, pszNmPfx);
fprintf(pOutput,
" ;\n"
" ; Resolve the imports too if requested to do so.\n"
" ;\n"
"Lexplicit_loaded_module:\n"
" cmp w20, #0\n"
" b.eq Lexplicit_load_return\n"
"\n"
" adrp x22, g_szzNames@PAGE\n"
" add x22, x22, g_szzNames@PAGEOFF\n"
" adrp x23, g_apfnImports@PAGE\n"
" add x23, x23, g_apfnImports@PAGEOFF\n"
"Lexplicit_load_next_import:\n"
" ldr x0, [x23]\n"
" cmp x0, #0\n"
" b.eq Lexplicit_load_return\n"
"\n"
" ; Get the module handle and call RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)\n"
" adrp x0, g_hMod@PAGE\n"
" ldr x0, [x0, g_hMod@PAGEOFF]\n"
" mov x1, x22\n"
" mov x2, x23\n"
" bl %sRTLdrGetSymbol\n"
" cmp x0, #0\n"
" b.ne Lexplicit_load_symbol_error\n"
"\n"
" ; Advance.\n"
" add x23, x23, #8\n"
"Lexplict_load_advance_string:\n"
" ldrb w0, [x22]\n"
" add x22, x22, #1\n"
" cmp w0, #0\n"
" b.ne Lexplict_load_advance_string\n"
" b Lexplicit_load_next_import\n"
"\n"
" ;\n"
" ; Error loading a symbol. Call RTErrInfoSet(PRTERRINFO pErrInfo, int rc, const char *pszMsg) on pErrInfo (preserves x0).\n"
" ;\n"
"Lexplicit_load_symbol_error:\n"
" mov x2, x22\n"
" mov x1, x0\n"
" mov x0, x21\n"
" bl %sRTErrInfoSet\n"
" b Lexplicit_load_return"
" "
"\n"
"Lexplicit_load_return:\n"
" ldp x22, x23, [sp, #(96 - 32)]\n"
" .cfi_restore x23\n"
" .cfi_restore x22\n"
" ldp x20, x21, [sp, #(96 - 16)]\n"
" .cfi_restore x21\n"
" .cfi_restore x20\n"
"\n"
" ldp x29, x30, [sp, #96]\n"
" .cfi_restore x29\n"
" .cfi_restore x30\n"
" add sp, sp, #(16 + 96)\n"
" ret\n"
" .cfi_endproc\n"
"\n"
"\n"
, pszNmPfx, pszNmPfx);
}
return RTEXITCODE_SUCCESS;
}
/**
* Generates the assembly source code, writing it to g_pszOutput.
*
* @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE, in the latter case full
* details has been displayed.
*/
static RTEXITCODE generateOutput(void)
{
RTEXITCODE rcExit = RTEXITCODE_FAILURE;
FILE *pOutput = fopen(g_pszOutput, "w");
if (pOutput)
{
switch (g_enmTarget)
{
case RTLDRARCH_AMD64:
case RTLDRARCH_X86_32:
rcExit = generateOutputInnerX86AndAMD64(pOutput);
break;
case RTLDRARCH_ARM64:
rcExit = generateOutputInnerArm64(pOutput);
break;
default:
rcExit = RTEXITCODE_FAILURE;
break;
}
if (fclose(pOutput))
{
fprintf(stderr, "error: Error closing '%s'.\n", g_pszOutput);
rcExit = RTEXITCODE_FAILURE;
}
}
else
fprintf(stderr, "error: Failed to open '%s' for writing.\n", g_pszOutput);
return rcExit;
}
/**
* Displays usage information.
*
* @returns RTEXITCODE_SUCCESS.
* @param pszArgv0 The argv[0] string.
*/
static int usage(const char *pszArgv0)
{
printf("usage: %s [options] --libary <loadname> --output <lazyload.asm> <input.def>\n"
"\n"
"Options:\n"
" --explicit-load-function, --no-explicit-load-function\n"
" Whether to include the explicit load function, default is not to.\n"
"\n"
"Copyright (C) 2013-2016 Oracle Corporation\n"
, pszArgv0);
return RTEXITCODE_SUCCESS;
}
int main(int argc, char **argv)
{
/*
* Parse options.
*/
for (int i = 1; i < argc; i++)
{
const char *psz = argv[i];
if (*psz == '-')
{
if (!strcmp(psz, "--output") || !strcmp(psz, "-o"))
{
if (++i >= argc)
{
fprintf(stderr, "syntax error: File name expected after '%s'.\n", psz);
return RTEXITCODE_SYNTAX;
}
g_pszOutput = argv[i];
}
else if (!strcmp(psz, "--library") || !strcmp(psz, "-l"))
{
if (++i >= argc)
{
fprintf(stderr, "syntax error: Library name expected after '%s'.\n", psz);
return RTEXITCODE_SYNTAX;
}
g_pszLibrary = argv[i];
}
else if (!strcmp(psz, "--explicit-load-function"))
g_fWithExplictLoadFunction = true;
else if (!strcmp(psz, "--no-explicit-load-function"))
g_fWithExplictLoadFunction = false;
else if (!strcmp(psz, "--system"))
g_fSystemLibrary = true;
/** @todo Support different load methods so this can be used on system libs and
* such if we like. */
else if ( !strcmp(psz, "--help")
|| !strcmp(psz, "-help")
|| !strcmp(psz, "-h")
|| !strcmp(psz, "-?") )
return usage(argv[0]);
else if ( !strcmp(psz, "--version")
|| !strcmp(psz, "-V"))
{
printf("$Revision: 155244 $\n");
return RTEXITCODE_SUCCESS;
}
else
{
fprintf(stderr, "syntax error: Unknown option '%s'.\n", psz);
return RTEXITCODE_SYNTAX;
}
}
else
{
if (g_cInputs >= RT_ELEMENTS(g_apszInputs))
{
fprintf(stderr, "syntax error: Too many input files, max is %d.\n", (int)RT_ELEMENTS(g_apszInputs));
return RTEXITCODE_SYNTAX;
}
g_apszInputs[g_cInputs++] = argv[i];
}
}
if (g_cInputs == 0)
{
fprintf(stderr, "syntax error: No input file specified.\n");
return RTEXITCODE_SYNTAX;
}
if (!g_pszOutput)
{
fprintf(stderr, "syntax error: No output file specified.\n");
return RTEXITCODE_SYNTAX;
}
if (!g_pszLibrary)
{
fprintf(stderr, "syntax error: No library name specified.\n");
return RTEXITCODE_SYNTAX;
}
/*
* Do the job.
*/
RTEXITCODE rcExit = parseInputs();
if (rcExit == RTEXITCODE_SUCCESS)
rcExit = generateOutput();
return rcExit;
}
|