- Timestamp:
- Jun 23, 2023 8:45:52 AM (18 months ago)
- Location:
- trunk
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bldprogs/VBoxDef2LazyLoad.cpp
r99552 r100253 78 78 #elif defined(RT_ARCH_ARM64) 79 79 static RTLDRARCH g_enmTarget = RTLDRARCH_ARM64; 80 #else 81 # error "Port me!" 82 #endif 83 #if defined(RT_OS_DARWIN) 84 static RTLDRFMT g_enmLdrFmt = RTLDRFMT_MACHO; 85 #elif defined(RT_OS_WINDOWS) 86 static RTLDRFMT g_enmLdrFmt = RTLDRFMT_PE; 87 #elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD) || defined(RT_OS_SOLARIS) \ 88 || defined(RT_OS_NETBSD) || defined(RT_OS_OPENBSD) 89 static RTLDRFMT g_enmLdrFmt = RTLDRFMT_ELF; 80 90 #else 81 91 # error "Port me!" … … 1102 1112 static RTEXITCODE generateOutputInnerArm64(FILE *pOutput) 1103 1113 { 1104 // bool fMachO = true; 1105 // bool fDarwin = true; 1106 const char *pszNmPfx = "_"; 1107 1108 fprintf(pOutput, ";;\n"); 1114 const char *pszNmPfx = g_enmLdrFmt == RTLDRFMT_ELF ? "" : "_"; 1115 1116 fprintf(pOutput, "/*\n"); 1109 1117 for (unsigned i = 0; i < g_cInputs; i++) 1110 fprintf(pOutput, " ;;Autogenerated from '%s'.\n", g_apszInputs[i]);1111 1112 fprintf(pOutput, 1113 " ;;DO NOT EDIT!\n"1114 " ;;\n"1115 "\n" 1116 "\n" 1117 /*"%%include \"iprt/asmdefs.mac\"\n"*/1118 fprintf(pOutput, " * Autogenerated from '%s'.\n", g_apszInputs[i]); 1119 1120 fprintf(pOutput, 1121 " * DO NOT EDIT!\n" 1122 " */\n" 1123 "\n" 1124 "\n" 1125 "#include \"iprt/asmdefs-arm.h\"\n" 1118 1126 "\n" 1119 1127 "\n"); … … 1123 1131 */ 1124 1132 fprintf(pOutput, 1125 " ;\n"1126 " ;Thunks.\n"1127 " ;\n"1128 " .section __TEXT,__text,regular,pure_instructions\n");1133 "/*\n" 1134 " * Thunks.\n" 1135 " */\n" 1136 "BEGINCODE\n"); 1129 1137 for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext) 1130 1138 if (!pExp->fData) … … 1133 1141 ".globl %s%s\n" 1134 1142 "%s%s:\n" 1135 " adrp x9, %sg_pfn%s@PAGE\n"1136 " ldr x9, [x9, %sg_pfn%s@PAGEOFF]\n"1143 " adrp x9, PAGE(%sg_pfn%s)\n" 1144 " ldr x9, [x9, PAGEOFF(%sg_pfn%s)]\n" 1137 1145 " br x9\n", 1138 1146 pszNmPfx, pExp->szName, pszNmPfx, pExp->szName, pszNmPfx, pExp->szName, pszNmPfx, pExp->szName); … … 1142 1150 ".globl %sLazyGetPtr_%s\n" 1143 1151 "%sLazyGetPtr_%s:\n" 1144 " adrp x9, %sg_LazyPtr_%s@PAGE\n"1145 " ldr x9, [x9, %sg_LazyPtr_%s@PAGEOFF]\n"1152 " adrp x9, PAGE(%sg_LazyPtr_%s)\n" 1153 " ldr x9, [x9, PAGEOFF(%sg_LazyPtr_%s)]\n" 1146 1154 " cmp x9, #0\n" 1147 " b. ne___LazyLoad___%s\n"1155 " b.eq ___LazyLoad___%s\n" 1148 1156 " mov x0, x9\n" 1149 1157 " ret\n", 1150 1158 pszNmPfx, pExp->szName, pszNmPfx, pExp->szName, pszNmPfx, pExp->szName, pszNmPfx, pExp->szName, pExp->pszExportedNm); 1151 1159 fprintf(pOutput, 1160 "ENDCODE\n" 1152 1161 "\n" 1153 1162 "\n"); … … 1157 1166 */ 1158 1167 fprintf(pOutput, 1159 " ;\n"1160 " ;Import pointers. Initialized to point to lazy loading stubs.\n"1161 " ;\n"1162 " .section __DATA,__data\n"1168 "/*\n" 1169 " * Import pointers. Initialized to point to lazy loading stubs.\n" 1170 " */\n" 1171 "BEGINDATA\n" 1163 1172 ".p2align 3\n" 1164 1173 "g_apfnImports:\n"); … … 1183 1192 pszNmPfx, pExp->szName, pszNmPfx, pExp->szName); 1184 1193 fprintf(pOutput, 1185 " .quad 0 ; Terminator entry for traversal.\n" 1194 " .quad 0 /* Terminator entry for traversal. */\n" 1195 "ENDDATA\n" 1186 1196 "\n" 1187 1197 "\n"); … … 1194 1204 */ 1195 1205 fprintf(pOutput, 1196 " ;\n"1197 " ;Imported names.\n"1198 " ;\n"1199 " .section __TEXT,__cstring,cstring_literals\n"1206 "/*\n" 1207 " * Imported names.\n" 1208 " */\n" 1209 "BEGINCONST\n" 1200 1210 "g_szLibrary:\n" 1201 1211 " .asciz \"%s\"\n" … … 1213 1223 "g_szFailLoadFmt: .asciz \"Lazy loader failed to load \\\"%%s\\\": %%Rrc\\n\"\n" 1214 1224 "g_szFailResolveFmt: .asciz \"Lazy loader failed to resolve symbol \\\"%%s\\\" in \\\"%%s\\\": %%Rrc\\n\"\n" 1225 "ENDCONST\n" 1215 1226 "\n" 1216 1227 "\n"); … … 1220 1231 */ 1221 1232 fprintf(pOutput, 1222 " ;\n"1223 " ;Lazy load+resolve stubs.\n"1224 " ;\n"1225 " .section __TEXT,__text,regular,pure_instructions\n"1233 "/*\n" 1234 " * Lazy load+resolve stubs.\n" 1235 " */\n" 1236 "BEGINCODE\n" 1226 1237 ".p2align 3\n"); 1227 1238 for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext) … … 1230 1241 fprintf(pOutput, 1231 1242 "___LazyLoad___%s:\n" 1232 " adrp x9, g_sz%s@PAGE\n" 1233 " add x9, x9, g_sz%s@PAGEOFF\n" 1234 " adrp x10, %s%s%s@PAGE\n" 1235 " add x10, x10, %s%s%s@PAGEOFF\n" 1243 " adrp x9, PAGE(g_sz%s)\n" 1244 " add x9, x9, PAGEOFF(g_sz%s)\n" 1245 " adrp x10, PAGE(%s%s%s)\n" 1246 " add x10, x10, PAGEOFF(%s%s%s)\n" 1247 " mov x16, x30\n" 1236 1248 " bl LazyLoadResolver\n" 1249 " mov x30, x16\n" 1237 1250 , pExp->pszExportedNm, 1238 1251 pExp->pszExportedNm, pExp->pszExportedNm, … … 1243 1256 "___LazyLoad___%s:\n" 1244 1257 " movk w9, #%u\n" 1245 " adrp x10, %s%s%s@PAGE\n"1246 " add x10, x10, %s%s%s@PAGEOFF\n"1258 " adrp x10, PAGE(%s%s%s)\n" 1259 " add x10, x10, PAGEOFF(%s%s%s)\n" 1247 1260 , pExp->pszExportedNm, 1248 1261 pExp->uOrdinal, … … 1256 1269 } 1257 1270 fprintf(pOutput, 1271 "ENDCODE\n" 1258 1272 "\n" 1259 1273 "\n" … … 1264 1278 */ 1265 1279 fprintf(pOutput, 1266 " ;\n"1267 " ;The module handle.\n"1268 " ;\n"1269 " .section __DATA,__data\n"1280 "/*\n" 1281 " * The module handle.\n" 1282 " */\n" 1283 "BEGINDATA\n" 1270 1284 "g_hMod:\n" 1271 1285 " .quad 0\n" 1286 "ENDDATA\n" 1272 1287 "\n" 1273 1288 "\n" … … 1278 1293 */ 1279 1294 fprintf(pOutput, 1280 " ;\n"1281 " ;The resolver code.\n"1282 " ;\n"1283 " .section __TEXT,__text,regular,pure_instructions\n"1295 "/*\n" 1296 " * The resolver code.\n" 1297 " */\n" 1298 "BEGINCODE\n" 1284 1299 ".p2align 3\n" 1285 1300 "LazyLoadResolver:\n" 1286 1301 " .cfi_startproc\n" 1287 " ; Create frame.\n"1302 " /* Create frame. */\n" 1288 1303 " sub sp, sp, #(16 + 192)\n" 1289 1304 " stp x29, x30, [sp, #192]\n" … … 1292 1307 " .cfi_offset x30, -8\n" 1293 1308 " .cfi_offset x29, -16\n" 1294 " ; Save all argument registers and a handful of preserved ones.\n"1309 " /* Save all argument registers and a handful of preserved ones. */\n" 1295 1310 " stp x0, x1, [sp, #(192 - 16)]\n" 1296 1311 " .cfi_offset x0, -32\n" … … 1319 1334 " str x8, [sp, #(192 - 144)]\n" 1320 1335 "\n" 1321 " ;Shift the symbol name to x19 and g_pfnXXXX pointer to x20 as these are preserved registers\n"1322 " ; (in case we need to call LazyLoadModule/RTLdrLoad)\n"1336 " /* Shift the symbol name to x19 and g_pfnXXXX pointer to x20 as these are preserved registers\n" 1337 " * (in case we need to call LazyLoadModule/RTLdrLoad) */\n" 1323 1338 " mov x19, x9\n" 1324 1339 " mov x20, x10\n" 1325 1340 "\n" 1326 " ; Get the module handle and call RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)\n"1327 " adrp x0, g_hMod@PAGE\n"1328 " ldr x0, [x0, g_hMod@PAGEOFF]\n"1341 " /* Get the module handle and call RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue) */\n" 1342 " adrp x0, PAGE(g_hMod)\n" 1343 " ldr x0, [x0, PAGEOFF(g_hMod)]\n" 1329 1344 " cmp x0, #0\n" 1330 " b.eq LazyLoading\n" 1345 " b.ne Lloaded\n" 1346 " bl LazyLoading\n" 1347 "Lloaded:\n" 1331 1348 " mov x1, x19\n" 1332 1349 " mov x2, x20\n" … … 1336 1353 " b.eq Lreturn\n" 1337 1354 "\n" 1338 "Lbadsym: ; Call sRTAssertMsg2Weak. Variadic (...) arguments are passed on the stack it seems.\n"1355 "Lbadsym: /* Call sRTAssertMsg2Weak. Variadic (...) arguments are passed on the stack it seems. */\n" 1339 1356 " mov x3, x0\n" 1340 " adrp x2, g_szLibrary@PAGE\n"1341 " add x2, x2, g_szLibrary@PAGEOFF\n"1357 " adrp x2, PAGE(g_szLibrary)\n" 1358 " add x2, x2, PAGEOFF(g_szLibrary)\n" 1342 1359 " mov x1, x19\n" 1343 " adrp x0, g_szFailLoadFmt@PAGE\n"1344 " add x0, x0, g_szFailLoadFmt@PAGEOFF\n"1360 " adrp x0, PAGE(g_szFailLoadFmt)\n" 1361 " add x0, x0, PAGEOFF(g_szFailLoadFmt)\n" 1345 1362 " stp x1, x2, [sp]\n" 1346 1363 " str x3, [sp, #16]\n" … … 1351 1368 1352 1369 "Lreturn:\n" 1353 " ; Restore saved register\n"1370 " /* Restore saved register */\n" 1354 1371 " ldr x8, [sp, #(192 - 144)]\n" 1355 1372 " .cfi_restore x8\n" … … 1390 1407 1391 1408 fprintf(pOutput, 1392 " ;\n"1393 " ;Loads the module.\n"1394 " ;ASSUMES called from LazyLoadResolver where all relevant registers are already saved.\n"1395 " ;\n"1409 "/*\n" 1410 " * Loads the module.\n" 1411 " * ASSUMES called from LazyLoadResolver where all relevant registers are already saved.\n" 1412 " */\n" 1396 1413 "LazyLoading:\n" 1397 1414 " .cfi_startproc\n" 1398 " ; Create frame.\n"1415 " /* Create frame. */\n" 1399 1416 " sub sp, sp, #(16 + 48)\n" 1400 1417 " stp x29, x30, [sp, #48]\n" … … 1407 1424 if (!g_fSystemLibrary) 1408 1425 fprintf(pOutput, 1409 " ; Call SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);\n"1426 " /* Call SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo); */\n" 1410 1427 " mov x3, #0\n" 1411 1428 " mov x2, #0\n" 1412 " adrp x1, g_hMod@PAGE\n"1413 " add x1, x1, g_hMod@PAGEOFF\n"1414 " adrp x0, g_szLibrary@PAGE\n"1415 " add x0, x0, g_szLibrary@PAGEOFF\n"1429 " adrp x1, PAGE(g_hMod)\n" 1430 " add x1, x1, PAGEOFF(g_hMod)\n" 1431 " adrp x0, PAGE(g_szLibrary)\n" 1432 " add x0, x0, PAGEOFF(g_szLibrary)\n" 1416 1433 " bl %sSUPR3HardenedLdrLoadAppPriv\n" 1417 1434 , pszNmPfx); 1418 1435 else 1419 1436 fprintf(pOutput, 1420 " ; Call RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod);\n"1421 " adrp x2, g_hMod@PAGE\n"1422 " add x2, x2, g_hMod@PAGEOFF\n"1437 " /* Call RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod); */\n" 1438 " adrp x2, PAGE(g_hMod)\n" 1439 " add x2, x2, PAGEOFF(g_hMod)\n" 1423 1440 " mov x1, #1\n" 1424 " adrp x0, g_szLibrary@PAGE\n"1425 " add x0, x0, g_szLibrary@PAGEOFF\n"1441 " adrp x0, PAGE(g_szLibrary)\n" 1442 " add x0, x0, PAGEOFF(g_szLibrary)\n" 1426 1443 " bl %sRTLdrLoadSystem\n" 1427 1444 , pszNmPfx); … … 1431 1448 " b.eq Lload_return\n" 1432 1449 "\n" 1433 "Lbadload: ; Call sRTAssertMsg2Weak. Variadic (...) arguments are passed on the stack it seems.\n"1450 "Lbadload: /* Call sRTAssertMsg2Weak. Variadic (...) arguments are passed on the stack it seems. */\n" 1434 1451 " mov x2, x0\n" 1435 " adrp x1, g_szLibrary@PAGE\n"1436 " add x1, x1, g_szLibrary@PAGEOFF\n"1437 " adrp x0, g_szFailResolveFmt@PAGE\n"1438 " add x0, x0, g_szFailResolveFmt@PAGEOFF\n"1452 " adrp x1, PAGE(g_szLibrary)\n" 1453 " add x1, x1, PAGEOFF(g_szLibrary)\n" 1454 " adrp x0, PAGE(g_szFailResolveFmt)\n" 1455 " add x0, x0, PAGEOFF(g_szFailResolveFmt)\n" 1439 1456 " stp x1, x2, [sp]\n" 1440 1457 " bl %sRTAssertMsg2Weak\n" … … 1443 1460 " b Lbadloadloop\n" 1444 1461 "Lload_return:\n" 1445 " adrp x0, g_hMod@PAGE\n"1446 " ldr x0, [x0, g_hMod@PAGEOFF]\n"1462 " adrp x0, PAGE(g_hMod)\n" 1463 " ldr x0, [x0, PAGEOFF(g_hMod)]\n" 1447 1464 " ldp x29, x30, [sp, #48]\n" 1448 1465 " .cfi_restore x29\n" … … 1451 1468 " ret\n" 1452 1469 " .cfi_endproc\n" 1470 "ENDCODE\n" 1453 1471 "\n" 1454 1472 "\n" … … 1463 1481 int cchLibBaseName = (int)(strchr(g_pszLibrary, '.') ? strchr(g_pszLibrary, '.') - g_pszLibrary : strlen(g_pszLibrary)); 1464 1482 fprintf(pOutput, 1465 " ;;\n"1466 " ;ExplicitlyLoad%.*s(bool fResolveAllImports, pErrInfo);\n"1467 " ;\n"1468 " .section __TEXT,__text,regular,pure_instructions\n"1483 "/**\n" 1484 " * ExplicitlyLoad%.*s(bool fResolveAllImports, pErrInfo);\n" 1485 " */\n" 1486 "BEGINCODE\n" 1469 1487 ".p2align 3\n" 1470 1488 ".globl %sExplicitlyLoad%.*s\n" 1471 1489 "%sExplicitlyLoad%.*s:\n" 1472 1490 " .cfi_startproc\n" 1473 " ; Create frame.\n"1491 " /* Create frame. */\n" 1474 1492 " sub sp, sp, #(16 + 96)\n" 1475 1493 " stp x29, x30, [sp, #96]\n" … … 1486 1504 " .cfi_offset x22, -48\n" 1487 1505 1488 " ; Save the input parameters.\n"1506 " /* Save the input parameters. */\n" 1489 1507 " mov x20, x0\n" 1490 1508 " mov x21, x1\n" 1491 1509 "\n" 1492 " ;\n"1493 " ;Is the module already loaded?\n"1494 " ;\n"1495 " adrp x0, g_hMod@PAGE\n"1496 " ldr x0, [x0, g_hMod@PAGEOFF]\n"1510 " /*\n" 1511 " * Is the module already loaded?\n" 1512 " */\n" 1513 " adrp x0, PAGE(g_hMod)\n" 1514 " ldr x0, [x0, PAGEOFF(g_hMod)]\n" 1497 1515 " cmp x0, #0\n" 1498 1516 " b.ne Lexplicit_loaded_module\n" … … 1506 1524 if (!g_fSystemLibrary) 1507 1525 fprintf(pOutput, 1508 " ; Call SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);\n"1526 " /* Call SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo); */\n" 1509 1527 " mov x3, x21\n" 1510 1528 " mov x2, #0\n" 1511 " adrp x1, g_hMod@PAGE\n"1512 " add x1, x1, g_hMod@PAGEOFF\n"1513 " adrp x0, g_szLibrary@PAGE\n"1514 " add x0, x0, g_szLibrary@PAGEOFF\n"1529 " adrp x1, PAGE(g_hMod)\n" 1530 " add x1, x1, PAGEOFF(g_hMod)\n" 1531 " adrp x0, PAGE(g_szLibrary)\n" 1532 " add x0, x0, PAGEOFF(g_szLibrary)\n" 1515 1533 " bl %sSUPR3HardenedLdrLoadAppPriv\n" 1516 1534 , pszNmPfx); 1517 1535 else 1518 1536 fprintf(pOutput, 1519 " ; Call RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod);\n"1520 " adrp x2, g_hMod@PAGE\n"1521 " add x2, x2, g_hMod@PAGEOFF\n"1537 " /* Call RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod); */\n" 1538 " adrp x2, PAGE(g_hMod)\n" 1539 " add x2, x2, PAGEOFF(g_hMod)\n" 1522 1540 " mov x1, #1\n" 1523 " adrp x0, g_szLibrary@PAGE\n"1524 " add x0, x0, g_szLibrary@PAGEOFF\n"1541 " adrp x0, PAGE(g_szLibrary)\n" 1542 " add x0, x0, PAGEOFF(g_szLibrary)\n" 1525 1543 " bl %sRTLdrLoadSystem\n" 1526 1544 , pszNmPfx); … … 1531 1549 1532 1550 fprintf(pOutput, 1533 " ;\n"1534 " ;Resolve the imports too if requested to do so.\n"1535 " ;\n"1551 " /*\n" 1552 " * Resolve the imports too if requested to do so.\n" 1553 " */\n" 1536 1554 "Lexplicit_loaded_module:\n" 1537 1555 " cmp w20, #0\n" 1538 1556 " b.eq Lexplicit_load_return\n" 1539 1557 "\n" 1540 " adrp x22, g_szzNames@PAGE\n"1541 " add x22, x22, g_szzNames@PAGEOFF\n"1542 " adrp x23, g_apfnImports@PAGE\n"1543 " add x23, x23, g_apfnImports@PAGEOFF\n"1558 " adrp x22, PAGE(g_szzNames)\n" 1559 " add x22, x22, PAGEOFF(g_szzNames)\n" 1560 " adrp x23, PAGE(g_apfnImports)\n" 1561 " add x23, x23, PAGEOFF(g_apfnImports)\n" 1544 1562 "Lexplicit_load_next_import:\n" 1545 1563 " ldr x0, [x23]\n" … … 1547 1565 " b.eq Lexplicit_load_return\n" 1548 1566 "\n" 1549 " ; Get the module handle and call RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)\n"1550 " adrp x0, g_hMod@PAGE\n"1551 " ldr x0, [x0, g_hMod@PAGEOFF]\n"1567 " /* Get the module handle and call RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue) */\n" 1568 " adrp x0, PAGE(g_hMod)\n" 1569 " ldr x0, [x0, PAGEOFF(g_hMod)]\n" 1552 1570 " mov x1, x22\n" 1553 1571 " mov x2, x23\n" … … 1556 1574 " b.ne Lexplicit_load_symbol_error\n" 1557 1575 "\n" 1558 " ; Advance.\n"1576 " /* Advance. */\n" 1559 1577 " add x23, x23, #8\n" 1560 1578 "Lexplict_load_advance_string:\n" … … 1565 1583 " b Lexplicit_load_next_import\n" 1566 1584 "\n" 1567 " ;\n"1568 " ;Error loading a symbol. Call RTErrInfoSet(PRTERRINFO pErrInfo, int rc, const char *pszMsg) on pErrInfo (preserves x0).\n"1569 " ;\n"1585 " /*\n" 1586 " * Error loading a symbol. Call RTErrInfoSet(PRTERRINFO pErrInfo, int rc, const char *pszMsg) on pErrInfo (preserves x0).\n" 1587 " */\n" 1570 1588 "Lexplicit_load_symbol_error:\n" 1571 1589 " mov x2, x22\n" … … 1590 1608 " ret\n" 1591 1609 " .cfi_endproc\n" 1610 "ENDCODE\n" 1592 1611 "\n" 1593 1612 "\n"
Note:
See TracChangeset
for help on using the changeset viewer.