[46474] | 1 | /* $Id: VBoxDef2LazyLoad.cpp 104213 2024-04-07 16:29:33Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VBoxDef2LazyLoad - Lazy Library Loader Generator.
|
---|
[46475] | 4 | *
|
---|
[53743] | 5 | * @note Only tested on win.amd64 & darwin.amd64.
|
---|
[46474] | 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
[98103] | 9 | * Copyright (C) 2013-2023 Oracle and/or its affiliates.
|
---|
[46474] | 10 | *
|
---|
[96407] | 11 | * This file is part of VirtualBox base platform packages, as
|
---|
| 12 | * available from https://www.virtualbox.org.
|
---|
| 13 | *
|
---|
| 14 | * This program is free software; you can redistribute it and/or
|
---|
| 15 | * modify it under the terms of the GNU General Public License
|
---|
| 16 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 17 | * License.
|
---|
| 18 | *
|
---|
| 19 | * This program is distributed in the hope that it will be useful, but
|
---|
| 20 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 22 | * General Public License for more details.
|
---|
| 23 | *
|
---|
| 24 | * You should have received a copy of the GNU General Public License
|
---|
| 25 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 26 | *
|
---|
| 27 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[46474] | 28 | */
|
---|
| 29 |
|
---|
[57353] | 30 |
|
---|
| 31 | /*********************************************************************************************************************************
|
---|
| 32 | * Header Files *
|
---|
| 33 | *********************************************************************************************************************************/
|
---|
[46474] | 34 | #include <ctype.h>
|
---|
| 35 | #include <stdio.h>
|
---|
| 36 | #include <string.h>
|
---|
| 37 | #include <stdlib.h>
|
---|
| 38 | #include <iprt/types.h>
|
---|
[87282] | 39 | #include <iprt/ldr.h> /* For RTLDRARCH. */
|
---|
[46474] | 40 |
|
---|
| 41 |
|
---|
[57353] | 42 | /*********************************************************************************************************************************
|
---|
| 43 | * Structures and Typedefs *
|
---|
| 44 | *********************************************************************************************************************************/
|
---|
[46474] | 45 | typedef struct MYEXPORT
|
---|
| 46 | {
|
---|
| 47 | struct MYEXPORT *pNext;
|
---|
[59404] | 48 | /** Pointer to unmangled name for stdcall (after szName), NULL if not. */
|
---|
| 49 | char *pszUnstdcallName;
|
---|
| 50 | /** Pointer to the exported name. */
|
---|
| 51 | char const *pszExportedNm;
|
---|
| 52 | unsigned uOrdinal;
|
---|
[99551] | 53 | /** NONAME. */
|
---|
[46474] | 54 | bool fNoName;
|
---|
[99551] | 55 | /** DATA symbol if true, otherwise function. */
|
---|
| 56 | bool fData;
|
---|
[46474] | 57 | char szName[1];
|
---|
| 58 | } MYEXPORT;
|
---|
| 59 | typedef MYEXPORT *PMYEXPORT;
|
---|
| 60 |
|
---|
| 61 |
|
---|
[57353] | 62 | /*********************************************************************************************************************************
|
---|
| 63 | * Global Variables *
|
---|
| 64 | *********************************************************************************************************************************/
|
---|
[46474] | 65 | /** @name Options
|
---|
| 66 | * @{ */
|
---|
| 67 | static const char *g_pszOutput = NULL;
|
---|
| 68 | static const char *g_pszLibrary = NULL;
|
---|
[53732] | 69 | static const char *g_apszInputs[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
|
---|
| 70 | static unsigned g_cInputs = 0;
|
---|
[46474] | 71 | static bool g_fIgnoreData = true;
|
---|
[53732] | 72 | static bool g_fWithExplictLoadFunction = false;
|
---|
[59404] | 73 | static bool g_fSystemLibrary = false;
|
---|
[87282] | 74 | #if defined(RT_ARCH_AMD64)
|
---|
| 75 | static RTLDRARCH g_enmTarget = RTLDRARCH_AMD64;
|
---|
| 76 | #elif defined(RT_ARCH_X86)
|
---|
[87288] | 77 | static RTLDRARCH g_enmTarget = RTLDRARCH_X86_32;
|
---|
[87282] | 78 | #elif defined(RT_ARCH_ARM64)
|
---|
| 79 | static RTLDRARCH g_enmTarget = RTLDRARCH_ARM64;
|
---|
| 80 | #else
|
---|
| 81 | # error "Port me!"
|
---|
| 82 | #endif
|
---|
[46474] | 83 | /** @} */
|
---|
| 84 |
|
---|
| 85 | /** Pointer to the export name list head. */
|
---|
| 86 | static PMYEXPORT g_pExpHead = NULL;
|
---|
| 87 | /** Pointer to the next pointer for insertion. */
|
---|
| 88 | static PMYEXPORT *g_ppExpNext = &g_pExpHead;
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 |
|
---|
[63193] | 92 | #if 0 /* unused */
|
---|
[46474] | 93 | static const char *leftStrip(const char *psz)
|
---|
| 94 | {
|
---|
| 95 | while (isspace(*psz))
|
---|
| 96 | psz++;
|
---|
| 97 | return psz;
|
---|
| 98 | }
|
---|
[63193] | 99 | #endif
|
---|
[46474] | 100 |
|
---|
| 101 |
|
---|
| 102 | static char *leftStrip(char *psz)
|
---|
| 103 | {
|
---|
| 104 | while (isspace(*psz))
|
---|
| 105 | psz++;
|
---|
| 106 | return psz;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | static unsigned wordLength(const char *pszWord)
|
---|
| 111 | {
|
---|
| 112 | unsigned off = 0;
|
---|
| 113 | char ch;
|
---|
| 114 | while ( (ch = pszWord[off]) != '\0'
|
---|
| 115 | && ch != '='
|
---|
| 116 | && ch != ','
|
---|
| 117 | && ch != ':'
|
---|
| 118 | && !isspace(ch) )
|
---|
| 119 | off++;
|
---|
| 120 | return off;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
| 125 | * Parses the module definition file, collecting export information.
|
---|
| 126 | *
|
---|
| 127 | * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE, in the latter case full
|
---|
| 128 | * details has been displayed.
|
---|
| 129 | * @param pInput The input stream.
|
---|
| 130 | */
|
---|
[53732] | 131 | static RTEXITCODE parseInputInner(FILE *pInput, const char *pszInput)
|
---|
[46474] | 132 | {
|
---|
| 133 | /*
|
---|
| 134 | * Process the file line-by-line.
|
---|
| 135 | */
|
---|
| 136 | bool fInExports = false;
|
---|
| 137 | unsigned iLine = 0;
|
---|
| 138 | char szLine[16384];
|
---|
| 139 | while (fgets(szLine, sizeof(szLine), pInput))
|
---|
| 140 | {
|
---|
| 141 | iLine++;
|
---|
| 142 |
|
---|
| 143 | /*
|
---|
| 144 | * Strip leading and trailing spaces from the line as well as
|
---|
| 145 | * trailing comments.
|
---|
| 146 | */
|
---|
| 147 | char *psz = leftStrip(szLine);
|
---|
| 148 | if (*psz == ';')
|
---|
| 149 | continue; /* comment line. */
|
---|
| 150 |
|
---|
| 151 | char *pszComment = strchr(psz, ';');
|
---|
| 152 | if (pszComment)
|
---|
| 153 | *pszComment = '\0';
|
---|
| 154 |
|
---|
| 155 | unsigned cch = (unsigned)strlen(psz);
|
---|
| 156 | while (cch > 0 && (isspace(psz[cch - 1]) || psz[cch - 1] == '\r' || psz[cch - 1] == '\n'))
|
---|
| 157 | psz[--cch] = '\0';
|
---|
| 158 |
|
---|
| 159 | if (!cch)
|
---|
| 160 | continue;
|
---|
| 161 |
|
---|
| 162 | /*
|
---|
| 163 | * Check for known directives.
|
---|
| 164 | */
|
---|
| 165 | size_t cchWord0 = wordLength(psz);
|
---|
| 166 | #define WORD_CMP(pszWord1, cchWord1, szWord2) \
|
---|
| 167 | ( (cchWord1) == sizeof(szWord2) - 1 && memcmp(pszWord1, szWord2, sizeof(szWord2) - 1) == 0 )
|
---|
| 168 | if (WORD_CMP(psz, cchWord0, "EXPORTS"))
|
---|
| 169 | {
|
---|
| 170 | fInExports = true;
|
---|
| 171 |
|
---|
| 172 | /* In case there is an export on the same line. (Really allowed?) */
|
---|
| 173 | psz = leftStrip(psz + sizeof("EXPORTS") - 1);
|
---|
| 174 | if (!*psz)
|
---|
| 175 | continue;
|
---|
| 176 | }
|
---|
| 177 | /* Directives that we don't care about, but need to catch in order to
|
---|
| 178 | terminate the EXPORTS section in a timely manner. */
|
---|
| 179 | else if ( WORD_CMP(psz, cchWord0, "NAME")
|
---|
| 180 | || WORD_CMP(psz, cchWord0, "LIBRARY")
|
---|
| 181 | || WORD_CMP(psz, cchWord0, "DESCRIPTION")
|
---|
| 182 | || WORD_CMP(psz, cchWord0, "STACKSIZE")
|
---|
| 183 | || WORD_CMP(psz, cchWord0, "SECTIONS")
|
---|
| 184 | || WORD_CMP(psz, cchWord0, "SEGMENTS")
|
---|
| 185 | || WORD_CMP(psz, cchWord0, "VERSION")
|
---|
| 186 | )
|
---|
| 187 | {
|
---|
| 188 | fInExports = false;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /*
|
---|
| 192 | * Process exports:
|
---|
| 193 | * entryname[=internalname] [@ordinal[ ][NONAME]] [DATA] [PRIVATE]
|
---|
| 194 | */
|
---|
| 195 | if (fInExports)
|
---|
| 196 | {
|
---|
| 197 | const char *pchName = psz;
|
---|
| 198 | unsigned cchName = wordLength(psz);
|
---|
| 199 |
|
---|
| 200 | psz = leftStrip(psz + cchName);
|
---|
| 201 | if (*psz == '=')
|
---|
| 202 | {
|
---|
| 203 | psz = leftStrip(psz + 1);
|
---|
| 204 | psz = leftStrip(psz + wordLength(psz));
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[59404] | 207 | bool fNoName = false;
|
---|
[46474] | 208 | unsigned uOrdinal = ~0U;
|
---|
| 209 | if (*psz == '@')
|
---|
| 210 | {
|
---|
| 211 | psz++;
|
---|
| 212 | if (!isdigit(*psz))
|
---|
| 213 | {
|
---|
[53732] | 214 | fprintf(stderr, "%s:%u: error: Invalid ordinal spec.\n", pszInput, iLine);
|
---|
[46474] | 215 | return RTEXITCODE_FAILURE;
|
---|
| 216 | }
|
---|
| 217 | uOrdinal = *psz++ - '0';
|
---|
| 218 | while (isdigit(*psz))
|
---|
| 219 | {
|
---|
| 220 | uOrdinal *= 10;
|
---|
| 221 | uOrdinal += *psz++ - '0';
|
---|
| 222 | }
|
---|
| 223 | psz = leftStrip(psz);
|
---|
| 224 | cch = wordLength(psz);
|
---|
| 225 | if (WORD_CMP(psz, cch, "NONAME"))
|
---|
| 226 | {
|
---|
| 227 | fNoName = true;
|
---|
| 228 | psz = leftStrip(psz + cch);
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[99551] | 232 | bool fData = false;
|
---|
[46474] | 233 | while (*psz)
|
---|
| 234 | {
|
---|
| 235 | cch = wordLength(psz);
|
---|
| 236 | if (WORD_CMP(psz, cch, "DATA"))
|
---|
| 237 | {
|
---|
[99551] | 238 | fData = true;
|
---|
[46474] | 239 | if (!g_fIgnoreData)
|
---|
| 240 | {
|
---|
[98660] | 241 | fprintf(stderr, "%s:%u: error: Cannot process DATA export '%.*s'.\n",
|
---|
[53732] | 242 | pszInput, iLine, cchName, pchName);
|
---|
[46474] | 243 | return RTEXITCODE_SUCCESS;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
[99551] | 246 | else if (WORD_CMP(psz, cch, "PRIVATE"))
|
---|
[46474] | 247 | {
|
---|
[99551] | 248 | fprintf(stderr, "%s:%u: error: Cannot process PRIVATE export '%.*s'.\n",
|
---|
[53732] | 249 | pszInput, iLine, cchName, pchName);
|
---|
[46474] | 250 | return RTEXITCODE_SUCCESS;
|
---|
| 251 | }
|
---|
[99551] | 252 | else
|
---|
| 253 | {
|
---|
| 254 | fprintf(stderr, "%s:%u: error: Unknown keyword: %.*s.\n", pszInput, iLine, cch, psz);
|
---|
| 255 | return RTEXITCODE_FAILURE;
|
---|
| 256 | }
|
---|
[46474] | 257 | psz = leftStrip(psz + cch);
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | /*
|
---|
[59404] | 261 | * Check for stdcall mangling.
|
---|
| 262 | */
|
---|
| 263 | size_t cbExp = sizeof(MYEXPORT) + cchName;
|
---|
| 264 | unsigned cchStdcall = 0;
|
---|
| 265 | if (cchName > 3 && *pchName == '_' && isdigit(pchName[cchName - 1]))
|
---|
| 266 | {
|
---|
| 267 | if (cchName > 3 && pchName[cchName - 2] == '@')
|
---|
| 268 | cchStdcall = 2;
|
---|
| 269 | else if (cchName > 4 && pchName[cchName - 3] == '@' && isdigit(pchName[cchName - 2]))
|
---|
| 270 | cchStdcall = 3;
|
---|
| 271 | if (cchStdcall)
|
---|
| 272 | cbExp += cchName - 1 - cchStdcall;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /*
|
---|
[46474] | 276 | * Add the export.
|
---|
| 277 | */
|
---|
[59404] | 278 | PMYEXPORT pExp = (PMYEXPORT)malloc(cbExp);
|
---|
[46474] | 279 | if (!pExp)
|
---|
| 280 | {
|
---|
[53732] | 281 | fprintf(stderr, "%s:%u: error: Out of memory.\n", pszInput, iLine);
|
---|
[99551] | 282 | return RTEXITCODE_FAILURE;
|
---|
[46474] | 283 | }
|
---|
| 284 | memcpy(pExp->szName, pchName, cchName);
|
---|
| 285 | pExp->szName[cchName] = '\0';
|
---|
[59404] | 286 | if (!cchStdcall)
|
---|
| 287 | {
|
---|
| 288 | pExp->pszUnstdcallName = NULL;
|
---|
| 289 | pExp->pszExportedNm = pExp->szName;
|
---|
| 290 | }
|
---|
| 291 | else
|
---|
| 292 | {
|
---|
| 293 | pExp->pszUnstdcallName = &pExp->szName[cchName + 1];
|
---|
| 294 | memcpy(pExp->pszUnstdcallName, pchName + 1, cchName - 1 - cchStdcall);
|
---|
| 295 | pExp->pszUnstdcallName[cchName - 1 - cchStdcall] = '\0';
|
---|
| 296 | pExp->pszExportedNm = pExp->pszUnstdcallName;
|
---|
| 297 | }
|
---|
| 298 | pExp->uOrdinal = uOrdinal;
|
---|
| 299 | pExp->fNoName = fNoName;
|
---|
[99551] | 300 | pExp->fData = fData;
|
---|
[59404] | 301 | pExp->pNext = NULL;
|
---|
| 302 | *g_ppExpNext = pExp;
|
---|
| 303 | g_ppExpNext = &pExp->pNext;
|
---|
[46474] | 304 | }
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | /*
|
---|
| 308 | * Why did we quit the loop, EOF or error?
|
---|
| 309 | */
|
---|
| 310 | if (feof(pInput))
|
---|
| 311 | return RTEXITCODE_SUCCESS;
|
---|
[98660] | 312 | fprintf(stderr, "error: Incompletely read '%s' (iLine=%u).\n", pszInput, iLine);
|
---|
[46474] | 313 | return RTEXITCODE_FAILURE;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 |
|
---|
| 317 | /**
|
---|
[53732] | 318 | * Parses a_apszInputs, populating the list pointed to by g_pExpHead.
|
---|
[46474] | 319 | *
|
---|
| 320 | * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE, in the latter case full
|
---|
| 321 | * details has been displayed.
|
---|
| 322 | */
|
---|
[53732] | 323 | static RTEXITCODE parseInputs(void)
|
---|
[46474] | 324 | {
|
---|
[53732] | 325 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
| 326 | for (unsigned i = 0; i < g_cInputs; i++)
|
---|
[46474] | 327 | {
|
---|
[53732] | 328 | FILE *pInput = fopen(g_apszInputs[i], "r");
|
---|
| 329 | if (pInput)
|
---|
[46474] | 330 | {
|
---|
[53732] | 331 | RTEXITCODE rcExit2 = parseInputInner(pInput, g_apszInputs[i]);
|
---|
| 332 | fclose(pInput);
|
---|
| 333 | if (rcExit2 == RTEXITCODE_SUCCESS && !g_pExpHead)
|
---|
| 334 | {
|
---|
| 335 | fprintf(stderr, "error: Found no exports in '%s'.\n", g_apszInputs[i]);
|
---|
| 336 | rcExit2 = RTEXITCODE_FAILURE;
|
---|
| 337 | }
|
---|
| 338 | if (rcExit2 != RTEXITCODE_SUCCESS)
|
---|
| 339 | rcExit = rcExit2;
|
---|
| 340 | }
|
---|
| 341 | else
|
---|
| 342 | {
|
---|
| 343 | fprintf(stderr, "error: Failed to open '%s' for reading.\n", g_apszInputs[i]);
|
---|
[46474] | 344 | rcExit = RTEXITCODE_FAILURE;
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 | return rcExit;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 |
|
---|
[46475] | 351 | /**
|
---|
[87282] | 352 | * Generates the assembly source code for AMD64 and x86, writing it
|
---|
| 353 | * to @a pOutput.
|
---|
[46475] | 354 | *
|
---|
| 355 | * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE, in the latter case full
|
---|
| 356 | * details has been displayed.
|
---|
| 357 | * @param pOutput The output stream (caller checks it for errors
|
---|
| 358 | * when closing).
|
---|
| 359 | */
|
---|
[87282] | 360 | static RTEXITCODE generateOutputInnerX86AndAMD64(FILE *pOutput)
|
---|
[46474] | 361 | {
|
---|
[53732] | 362 | fprintf(pOutput, ";;\n");
|
---|
| 363 | for (unsigned i = 0; i < g_cInputs; i++)
|
---|
| 364 | fprintf(pOutput, ";; Autogenerated from '%s'.\n", g_apszInputs[i]);
|
---|
| 365 |
|
---|
[46474] | 366 | fprintf(pOutput,
|
---|
[53732] | 367 | ";; DO NOT EDIT!\n"
|
---|
| 368 | ";;\n"
|
---|
[46474] | 369 | "\n"
|
---|
[53732] | 370 | "\n"
|
---|
[46474] | 371 | "%%include \"iprt/asmdefs.mac\"\n"
|
---|
| 372 | "\n"
|
---|
[53732] | 373 | "\n");
|
---|
[46474] | 374 |
|
---|
[53732] | 375 | /*
|
---|
| 376 | * Put the thunks first for alignment and other reasons. It's the hot part of the code.
|
---|
| 377 | */
|
---|
| 378 | fprintf(pOutput,
|
---|
| 379 | ";\n"
|
---|
| 380 | "; Thunks.\n"
|
---|
| 381 | ";\n"
|
---|
| 382 | "BEGINCODE\n");
|
---|
[46474] | 383 | for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
|
---|
[99551] | 384 | if (pExp->fData)
|
---|
[59404] | 385 | fprintf(pOutput,
|
---|
[99551] | 386 | "BEGINPROC LazyGetPtr_%s\n"
|
---|
[99552] | 387 | " mov xAX, [NAME(g_LazyPtr_%s) xWrtRIP]\n"
|
---|
[99551] | 388 | " test xAX, xAX\n"
|
---|
| 389 | " jz ___LazyLoad___%s\n"
|
---|
| 390 | " ret\n"
|
---|
| 391 | "ENDPROC LazyGetPtr_%s\n",
|
---|
| 392 | pExp->szName, pExp->szName, pExp->szName, pExp->szName);
|
---|
| 393 | else if (!pExp->pszUnstdcallName)
|
---|
| 394 | fprintf(pOutput,
|
---|
[59404] | 395 | "BEGINPROC %s\n"
|
---|
[99551] | 396 | " jmp RTCCPTR_PRE [NAME(g_pfn%s) xWrtRIP]\n"
|
---|
[59404] | 397 | "ENDPROC %s\n",
|
---|
| 398 | pExp->szName, pExp->szName, pExp->szName);
|
---|
| 399 | else
|
---|
| 400 | fprintf(pOutput,
|
---|
| 401 | "%%ifdef RT_ARCH_X86\n"
|
---|
| 402 | "global %s\n"
|
---|
| 403 | "%s:\n"
|
---|
[99551] | 404 | " jmp RTCCPTR_PRE [NAME(g_pfn%s) xWrtRIP]\n"
|
---|
[59404] | 405 | "%%else\n"
|
---|
| 406 | "BEGINPROC %s\n"
|
---|
[99551] | 407 | " jmp RTCCPTR_PRE [NAME(g_pfn%s) xWrtRIP]\n"
|
---|
[59404] | 408 | "ENDPROC %s\n"
|
---|
| 409 | "%%endif\n",
|
---|
| 410 | pExp->szName, pExp->szName, pExp->pszUnstdcallName,
|
---|
| 411 | pExp->pszUnstdcallName, pExp->pszUnstdcallName, pExp->pszUnstdcallName);
|
---|
| 412 |
|
---|
[53732] | 413 | fprintf(pOutput,
|
---|
| 414 | "\n"
|
---|
| 415 | "\n");
|
---|
| 416 |
|
---|
| 417 | /*
|
---|
| 418 | * Import pointers
|
---|
| 419 | */
|
---|
| 420 | fprintf(pOutput,
|
---|
| 421 | ";\n"
|
---|
[98660] | 422 | "; Import pointers. Initialized to point to lazy loading stubs.\n"
|
---|
[53732] | 423 | ";\n"
|
---|
| 424 | "BEGINDATA\n"
|
---|
| 425 | "g_apfnImports:\n");
|
---|
| 426 | for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
|
---|
[99551] | 427 | if (pExp->fData)
|
---|
[64934] | 428 | fprintf(pOutput,
|
---|
| 429 | "%%ifdef ASM_FORMAT_PE\n"
|
---|
[99551] | 430 | ";@todo\n"
|
---|
| 431 | "%%endif\n"
|
---|
[99552] | 432 | "global NAME(g_LazyPtr_%s)\n"
|
---|
| 433 | "NAME(g_LazyPtr_%s): RTCCPTR_DEF 0\n",
|
---|
[99551] | 434 | pExp->pszExportedNm, pExp->pszExportedNm);
|
---|
| 435 | else if (pExp->pszUnstdcallName)
|
---|
| 436 | fprintf(pOutput,
|
---|
| 437 | "%%ifdef ASM_FORMAT_PE\n"
|
---|
[64934] | 438 | " %%ifdef RT_ARCH_X86\n"
|
---|
| 439 | "global __imp_%s\n"
|
---|
| 440 | "__imp_%s:\n"
|
---|
| 441 | " %%else\n"
|
---|
| 442 | "global __imp_%s\n"
|
---|
| 443 | "__imp_%s:\n"
|
---|
| 444 | " %%endif\n"
|
---|
| 445 | "%%endif\n"
|
---|
[99551] | 446 | "NAME(g_pfn%s) RTCCPTR_DEF ___LazyLoad___%s\n"
|
---|
[64934] | 447 | "\n",
|
---|
| 448 | pExp->szName,
|
---|
| 449 | pExp->szName,
|
---|
| 450 | pExp->pszUnstdcallName,
|
---|
| 451 | pExp->pszUnstdcallName,
|
---|
| 452 | pExp->pszExportedNm,
|
---|
| 453 | pExp->pszExportedNm);
|
---|
| 454 | else
|
---|
| 455 | fprintf(pOutput,
|
---|
| 456 | "%%ifdef ASM_FORMAT_PE\n"
|
---|
| 457 | "global __imp_%s\n"
|
---|
| 458 | "__imp_%s:\n"
|
---|
| 459 | "%%endif\n"
|
---|
[99551] | 460 | "NAME(g_pfn%s) RTCCPTR_DEF ___LazyLoad___%s\n"
|
---|
[64934] | 461 | "\n",
|
---|
| 462 | pExp->szName,
|
---|
| 463 | pExp->szName,
|
---|
| 464 | pExp->pszExportedNm,
|
---|
| 465 | pExp->pszExportedNm);
|
---|
[53732] | 466 | fprintf(pOutput,
|
---|
| 467 | "RTCCPTR_DEF 0 ; Terminator entry for traversal.\n"
|
---|
| 468 | "\n"
|
---|
| 469 | "\n");
|
---|
| 470 |
|
---|
| 471 | /*
|
---|
| 472 | * Now for the less important stuff, starting with the names.
|
---|
| 473 | *
|
---|
| 474 | * We keep the names separate so we can traverse them in parallel to
|
---|
| 475 | * g_apfnImports in the load-everything routine further down.
|
---|
| 476 | */
|
---|
| 477 | fprintf(pOutput,
|
---|
| 478 | ";\n"
|
---|
| 479 | "; Imported names.\n"
|
---|
| 480 | ";\n"
|
---|
| 481 | "BEGINCODE\n"
|
---|
[59404] | 482 | "g_szLibrary: db '%s',0\n"
|
---|
| 483 | "\n"
|
---|
[53732] | 484 | "g_szzNames:\n",
|
---|
| 485 | g_pszLibrary);
|
---|
| 486 | for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
|
---|
[59404] | 487 | if (!pExp->fNoName)
|
---|
| 488 | fprintf(pOutput, " g_sz%s:\n db '%s',0\n", pExp->pszExportedNm, pExp->pszExportedNm);
|
---|
| 489 | else
|
---|
| 490 | fprintf(pOutput, " g_sz%s:\n db '#%u',0\n", pExp->pszExportedNm, pExp->uOrdinal);
|
---|
[53732] | 491 | fprintf(pOutput,
|
---|
| 492 | "g_EndOfNames: db 0\n"
|
---|
| 493 | "\n"
|
---|
[59404] | 494 | "g_szFailLoadFmt: db 'Lazy loader failed to load \"%%s\": %%Rrc', 10, 0\n"
|
---|
| 495 | "g_szFailResolveFmt: db 'Lazy loader failed to resolve symbol \"%%s\" in \"%%s\": %%Rrc', 10, 0\n"
|
---|
| 496 | "\n"
|
---|
[53732] | 497 | "\n");
|
---|
| 498 |
|
---|
| 499 | /*
|
---|
| 500 | * The per import lazy load code.
|
---|
| 501 | */
|
---|
| 502 | fprintf(pOutput,
|
---|
| 503 | ";\n"
|
---|
| 504 | "; Lazy load+resolve stubs.\n"
|
---|
| 505 | ";\n"
|
---|
| 506 | "BEGINCODE\n");
|
---|
| 507 | for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
|
---|
[59404] | 508 | {
|
---|
| 509 | if (!pExp->fNoName)
|
---|
| 510 | fprintf(pOutput,
|
---|
| 511 | "___LazyLoad___%s:\n"
|
---|
| 512 | /* "int3\n" */
|
---|
| 513 | "%%ifdef RT_ARCH_AMD64\n"
|
---|
| 514 | " lea rax, [g_sz%s wrt rip]\n"
|
---|
[99552] | 515 | " lea r10, [NAME(%s%s) wrt rip]\n"
|
---|
[59404] | 516 | " call LazyLoadResolver\n"
|
---|
| 517 | "%%elifdef RT_ARCH_X86\n"
|
---|
| 518 | " push g_sz%s\n"
|
---|
[99552] | 519 | " push NAME(%s%s)\n"
|
---|
[59404] | 520 | " call LazyLoadResolver\n"
|
---|
| 521 | " add esp, 8h\n"
|
---|
| 522 | "%%else\n"
|
---|
| 523 | " %%error \"Unsupported architecture\"\n"
|
---|
| 524 | "%%endif\n"
|
---|
| 525 | ,
|
---|
| 526 | pExp->pszExportedNm,
|
---|
| 527 | pExp->pszExportedNm,
|
---|
[99552] | 528 | !pExp->fData ? "g_pfn" : "g_LazyPtr_", pExp->pszExportedNm,
|
---|
[59404] | 529 | pExp->pszExportedNm,
|
---|
[99552] | 530 | !pExp->fData ? "g_pfn" : "g_LazyPtr_", pExp->pszExportedNm);
|
---|
[59404] | 531 | else
|
---|
| 532 | fprintf(pOutput,
|
---|
| 533 | "___LazyLoad___%s:\n"
|
---|
| 534 | /* "int3\n" */
|
---|
| 535 | "%%ifdef RT_ARCH_AMD64\n"
|
---|
| 536 | " mov eax, %u\n"
|
---|
[99552] | 537 | " lea r10, [NAME(%s%s) wrt rip]\n"
|
---|
[59404] | 538 | " call LazyLoadResolver\n"
|
---|
| 539 | "%%elifdef RT_ARCH_X86\n"
|
---|
| 540 | " push %u\n"
|
---|
[99552] | 541 | " push NAME(%s%s)\n"
|
---|
[59404] | 542 | " call LazyLoadResolver\n"
|
---|
| 543 | " add esp, 8h\n"
|
---|
| 544 | "%%else\n"
|
---|
| 545 | " %%error \"Unsupported architecture\"\n"
|
---|
| 546 | "%%endif\n"
|
---|
| 547 | ,
|
---|
| 548 | pExp->pszExportedNm,
|
---|
| 549 | pExp->uOrdinal,
|
---|
[99552] | 550 | !pExp->fData ? "g_pfn" : "g_LazyPtr_", pExp->pszExportedNm,
|
---|
[59404] | 551 | pExp->uOrdinal,
|
---|
[99552] | 552 | !pExp->fData ? "g_pfn" : "g_LazyPtr_", pExp->pszExportedNm);
|
---|
[99551] | 553 | if (pExp->fData)
|
---|
| 554 | fprintf(pOutput, " jmp NAME(LazyGetPtr_%s)\n", pExp->szName);
|
---|
| 555 | else if (!pExp->pszUnstdcallName)
|
---|
[59404] | 556 | fprintf(pOutput, " jmp NAME(%s)\n", pExp->szName);
|
---|
| 557 | else
|
---|
| 558 | fprintf(pOutput,
|
---|
| 559 | "%%ifdef RT_ARCH_X86\n"
|
---|
| 560 | " jmp %s\n"
|
---|
| 561 | "%%else\n"
|
---|
| 562 | " jmp NAME(%s)\n"
|
---|
| 563 | "%%endif\n"
|
---|
| 564 | ,
|
---|
[64933] | 565 | pExp->szName, pExp->pszUnstdcallName);
|
---|
[59404] | 566 | fprintf(pOutput, "\n");
|
---|
| 567 | }
|
---|
[53732] | 568 | fprintf(pOutput,
|
---|
| 569 | "\n"
|
---|
| 570 | "\n"
|
---|
| 571 | "\n");
|
---|
[46474] | 572 |
|
---|
| 573 | /*
|
---|
| 574 | * The code that does the loading and resolving.
|
---|
| 575 | */
|
---|
| 576 | fprintf(pOutput,
|
---|
[53732] | 577 | ";\n"
|
---|
| 578 | "; The module handle.\n"
|
---|
| 579 | ";\n"
|
---|
[46474] | 580 | "BEGINDATA\n"
|
---|
| 581 | "g_hMod RTCCPTR_DEF 0\n"
|
---|
| 582 | "\n"
|
---|
[53732] | 583 | "\n"
|
---|
| 584 | "\n");
|
---|
[46474] | 585 |
|
---|
| 586 | /*
|
---|
| 587 | * How we load the module needs to be selectable later on.
|
---|
| 588 | *
|
---|
| 589 | * The LazyLoading routine returns the module handle in RCX/ECX, caller
|
---|
| 590 | * saved all necessary registers.
|
---|
| 591 | */
|
---|
[59404] | 592 | if (!g_fSystemLibrary)
|
---|
| 593 | fprintf(pOutput,
|
---|
| 594 | ";\n"
|
---|
| 595 | ";SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod,\n"
|
---|
| 596 | "; uint32_t fFlags, PRTERRINFO pErrInfo);\n"
|
---|
| 597 | ";\n"
|
---|
| 598 | "EXTERN_IMP2 SUPR3HardenedLdrLoadAppPriv\n"
|
---|
| 599 | "%%ifdef IN_RT_R3\n"
|
---|
| 600 | "extern NAME(RTAssertMsg2Weak)\n"
|
---|
| 601 | "%%else\n"
|
---|
| 602 | "EXTERN_IMP2 RTAssertMsg2Weak\n"
|
---|
| 603 | "%%endif\n"
|
---|
| 604 | "BEGINCODE\n"
|
---|
| 605 | "\n"
|
---|
| 606 | "LazyLoading:\n"
|
---|
| 607 | " mov xCX, [g_hMod xWrtRIP]\n"
|
---|
| 608 | " or xCX, xCX\n"
|
---|
| 609 | " jnz .return\n"
|
---|
| 610 | "\n"
|
---|
| 611 | "%%ifdef ASM_CALL64_GCC\n"
|
---|
| 612 | " xor rcx, rcx ; pErrInfo\n"
|
---|
| 613 | " xor rdx, rdx ; fFlags (local load)\n"
|
---|
| 614 | " lea rsi, [g_hMod wrt rip] ; phLdrMod\n"
|
---|
| 615 | " lea rdi, [g_szLibrary wrt rip] ; pszFilename\n"
|
---|
| 616 | " sub rsp, 08h\n"
|
---|
| 617 | " call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
|
---|
| 618 | " add rsp, 08h\n"
|
---|
| 619 | "\n"
|
---|
| 620 | "%%elifdef ASM_CALL64_MSC\n"
|
---|
| 621 | " xor r9, r9 ; pErrInfo\n"
|
---|
| 622 | " xor r8, r8 ; fFlags (local load)\n"
|
---|
| 623 | " lea rdx, [g_hMod wrt rip] ; phLdrMod\n"
|
---|
| 624 | " lea rcx, [g_szLibrary wrt rip] ; pszFilename\n"
|
---|
| 625 | " sub rsp, 28h\n"
|
---|
| 626 | " call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
|
---|
| 627 | " add rsp, 28h\n"
|
---|
| 628 | "\n"
|
---|
| 629 | "%%elifdef RT_ARCH_X86\n"
|
---|
| 630 | " sub xSP, 0ch\n"
|
---|
| 631 | " push 0 ; pErrInfo\n"
|
---|
| 632 | " push 0 ; fFlags (local load)\n"
|
---|
| 633 | " push g_hMod ; phLdrMod\n"
|
---|
| 634 | " push g_szLibrary ; pszFilename\n"
|
---|
| 635 | " call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
|
---|
| 636 | " add esp, 1ch\n"
|
---|
| 637 | "%%else\n"
|
---|
| 638 | " %%error \"Unsupported architecture\"\n"
|
---|
| 639 | "%%endif\n");
|
---|
| 640 | else
|
---|
| 641 | fprintf(pOutput,
|
---|
| 642 | ";\n"
|
---|
| 643 | "; RTDECL(int) RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod);\n"
|
---|
| 644 | ";\n"
|
---|
| 645 | "%%ifdef IN_RT_R3\n"
|
---|
| 646 | "extern NAME(RTLdrLoadSystem)\n"
|
---|
| 647 | "extern NAME(RTAssertMsg2Weak)\n"
|
---|
| 648 | "%%else\n"
|
---|
| 649 | "EXTERN_IMP2 RTLdrLoadSystem\n"
|
---|
| 650 | "EXTERN_IMP2 RTAssertMsg2Weak\n"
|
---|
| 651 | "%%endif\n"
|
---|
| 652 | "BEGINCODE\n"
|
---|
| 653 | "\n"
|
---|
| 654 | "LazyLoading:\n"
|
---|
| 655 | " mov xCX, [g_hMod xWrtRIP]\n"
|
---|
| 656 | " or xCX, xCX\n"
|
---|
| 657 | " jnz .return\n"
|
---|
| 658 | "\n"
|
---|
| 659 | "%%ifdef ASM_CALL64_GCC\n"
|
---|
| 660 | " lea rdx, [g_hMod wrt rip] ; phLdrMod\n"
|
---|
| 661 | " mov esi, 1 ; fNoUnload=true\n"
|
---|
| 662 | " lea rdi, [g_szLibrary wrt rip] ; pszFilename\n"
|
---|
| 663 | " sub rsp, 08h\n"
|
---|
| 664 | " %%ifdef IN_RT_R3\n"
|
---|
| 665 | " call NAME(RTLdrLoadSystem)\n"
|
---|
| 666 | " %%else\n"
|
---|
| 667 | " call IMP2(RTLdrLoadSystem)\n"
|
---|
| 668 | " %%endif\n"
|
---|
| 669 | " add rsp, 08h\n"
|
---|
| 670 | "\n"
|
---|
| 671 | "%%elifdef ASM_CALL64_MSC\n"
|
---|
| 672 | " lea r8, [g_hMod wrt rip] ; phLdrMod\n"
|
---|
| 673 | " mov edx, 1 ; fNoUnload=true\n"
|
---|
| 674 | " lea rcx, [g_szLibrary wrt rip] ; pszFilename\n"
|
---|
| 675 | " sub rsp, 28h\n"
|
---|
| 676 | " %%ifdef IN_RT_R3\n"
|
---|
| 677 | " call NAME(RTLdrLoadSystem)\n"
|
---|
| 678 | " %%else\n"
|
---|
| 679 | " call IMP2(RTLdrLoadSystem)\n"
|
---|
| 680 | " %%endif\n"
|
---|
| 681 | " add rsp, 28h\n"
|
---|
| 682 | "\n"
|
---|
| 683 | "%%elifdef RT_ARCH_X86\n"
|
---|
| 684 | " push g_hMod ; phLdrMod\n"
|
---|
| 685 | " push 1 ; fNoUnload=true\n"
|
---|
| 686 | " push g_szLibrary ; pszFilename\n"
|
---|
| 687 | " %%ifdef IN_RT_R3\n"
|
---|
| 688 | " call NAME(RTLdrLoadSystem)\n"
|
---|
| 689 | " %%else\n"
|
---|
| 690 | " call IMP2(RTLdrLoadSystem)\n"
|
---|
| 691 | " %%endif\n"
|
---|
| 692 | " add esp, 0ch\n"
|
---|
| 693 | "%%else\n"
|
---|
| 694 | " %%error \"Unsupported architecture\"\n"
|
---|
| 695 | "%%endif\n");
|
---|
[46474] | 696 | fprintf(pOutput,
|
---|
[59404] | 697 | " or eax, eax\n"
|
---|
| 698 | " jnz .badload\n"
|
---|
[46474] | 699 | " mov xCX, [g_hMod xWrtRIP]\n"
|
---|
[59404] | 700 | ".return:\n"
|
---|
| 701 | " ret\n"
|
---|
[46474] | 702 | "\n"
|
---|
[59404] | 703 | ".badload:\n"
|
---|
[46474] | 704 | "%%ifdef ASM_CALL64_GCC\n"
|
---|
[59404] | 705 | " mov edx, eax\n"
|
---|
| 706 | " lea rsi, [g_szLibrary wrt rip]\n"
|
---|
| 707 | " lea rdi, [g_szFailLoadFmt wrt rip]\n"
|
---|
[47685] | 708 | " sub rsp, 08h\n"
|
---|
[46474] | 709 | "%%elifdef ASM_CALL64_MSC\n"
|
---|
[59404] | 710 | " mov r8d, eax\n"
|
---|
| 711 | " lea rdx, [g_szLibrary wrt rip]\n"
|
---|
| 712 | " lea rcx, [g_szFailLoadFmt wrt rip]\n"
|
---|
[47685] | 713 | " sub rsp, 28h\n"
|
---|
[46474] | 714 | "%%elifdef RT_ARCH_X86\n"
|
---|
[59404] | 715 | " push eax\n"
|
---|
| 716 | " push g_szLibrary\n"
|
---|
| 717 | " push g_szFailLoadFmt\n"
|
---|
| 718 | "%%endif\n"
|
---|
| 719 | "%%ifdef IN_RT_R3\n"
|
---|
| 720 | " call NAME(RTAssertMsg2Weak)\n"
|
---|
[46474] | 721 | "%%else\n"
|
---|
[59404] | 722 | " call IMP2(RTAssertMsg2Weak)\n"
|
---|
[46474] | 723 | "%%endif\n"
|
---|
[59404] | 724 | ".badloadloop:\n"
|
---|
[46474] | 725 | " int3\n"
|
---|
[59404] | 726 | " jmp .badloadloop\n"
|
---|
[53742] | 727 | "LazyLoading_End:\n"
|
---|
[53732] | 728 | "\n"
|
---|
| 729 | "\n");
|
---|
[46474] | 730 |
|
---|
| 731 |
|
---|
| 732 | fprintf(pOutput,
|
---|
| 733 | ";\n"
|
---|
| 734 | ";RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue);\n"
|
---|
| 735 | ";\n"
|
---|
[59404] | 736 | "%%ifdef IN_RT_R3\n"
|
---|
| 737 | "extern NAME(RTLdrGetSymbol)\n"
|
---|
| 738 | "%%else\n"
|
---|
[53732] | 739 | "EXTERN_IMP2 RTLdrGetSymbol\n"
|
---|
[59404] | 740 | "%%endif\n"
|
---|
[53732] | 741 | "BEGINCODE\n"
|
---|
[53742] | 742 | "LazyLoadResolver:\n"
|
---|
[46474] | 743 | "%%ifdef RT_ARCH_AMD64\n"
|
---|
| 744 | " push rbp\n"
|
---|
| 745 | " mov rbp, rsp\n"
|
---|
| 746 | " push r15\n"
|
---|
| 747 | " push r14\n"
|
---|
| 748 | " mov r15, rax ; name\n"
|
---|
| 749 | " mov r14, r10 ; ppfn\n"
|
---|
| 750 | " push r9\n"
|
---|
| 751 | " push r8\n"
|
---|
| 752 | " push rcx\n"
|
---|
| 753 | " push rdx\n"
|
---|
[47685] | 754 | " push r12\n"
|
---|
[46474] | 755 | " %%ifdef ASM_CALL64_GCC\n"
|
---|
[46559] | 756 | " push rsi\n"
|
---|
[46474] | 757 | " push rdi\n"
|
---|
[47685] | 758 | " mov r12, rsp\n"
|
---|
[46474] | 759 | " %%else\n"
|
---|
[47685] | 760 | " mov r12, rsp\n"
|
---|
[46474] | 761 | " sub rsp, 20h\n"
|
---|
| 762 | " %%endif\n"
|
---|
[47685] | 763 | " and rsp, 0fffffff0h ; Try make sure the stack is aligned\n"
|
---|
[46474] | 764 | "\n"
|
---|
[53742] | 765 | " call LazyLoading ; returns handle in rcx\n"
|
---|
[46474] | 766 | " %%ifdef ASM_CALL64_GCC\n"
|
---|
| 767 | " mov rdi, rcx ; hLdrMod\n"
|
---|
| 768 | " mov rsi, r15 ; pszSymbol\n"
|
---|
| 769 | " mov rdx, r14 ; ppvValue\n"
|
---|
| 770 | " %%else\n"
|
---|
| 771 | " mov rdx, r15 ; pszSymbol\n"
|
---|
| 772 | " mov r8, r14 ; ppvValue\n"
|
---|
| 773 | " %%endif\n"
|
---|
[59404] | 774 | " %%ifdef IN_RT_R3\n"
|
---|
| 775 | " call NAME(RTLdrGetSymbol)\n"
|
---|
| 776 | " %%else\n"
|
---|
[46474] | 777 | " call IMP2(RTLdrGetSymbol)\n"
|
---|
[59404] | 778 | " %%endif\n"
|
---|
[46474] | 779 | " or eax, eax\n"
|
---|
[59404] | 780 | " jnz .badsym\n"
|
---|
[46474] | 781 | "\n"
|
---|
[47685] | 782 | " mov rsp, r12\n"
|
---|
[46474] | 783 | " %%ifdef ASM_CALL64_GCC\n"
|
---|
| 784 | " pop rdi\n"
|
---|
| 785 | " pop rsi\n"
|
---|
| 786 | " %%endif\n"
|
---|
[47685] | 787 | " pop r12\n"
|
---|
[46474] | 788 | " pop rdx\n"
|
---|
| 789 | " pop rcx\n"
|
---|
| 790 | " pop r8\n"
|
---|
| 791 | " pop r9\n"
|
---|
| 792 | " pop r14\n"
|
---|
| 793 | " pop r15\n"
|
---|
| 794 | " leave\n"
|
---|
| 795 | "\n"
|
---|
| 796 | "%%elifdef RT_ARCH_X86\n"
|
---|
| 797 | " push ebp\n"
|
---|
| 798 | " mov ebp, esp\n"
|
---|
| 799 | " push eax\n"
|
---|
| 800 | " push ecx\n"
|
---|
| 801 | " push edx\n"
|
---|
[47685] | 802 | " and esp, 0fffffff0h\n"
|
---|
[46474] | 803 | "\n"
|
---|
| 804 | ".loaded:\n"
|
---|
[59404] | 805 | " call LazyLoading ; returns handle in ecx\n"
|
---|
| 806 | " push dword [ebp + 8] ; value addr\n"
|
---|
| 807 | " push dword [ebp + 12] ; symbol name\n"
|
---|
| 808 | " push ecx\n"
|
---|
| 809 | " %%ifdef IN_RT_R3\n"
|
---|
| 810 | " call NAME(RTLdrGetSymbol)\n"
|
---|
| 811 | " %%else\n"
|
---|
[46474] | 812 | " call IMP2(RTLdrGetSymbol)\n"
|
---|
[59404] | 813 | " %%endif\n"
|
---|
[46474] | 814 | " or eax, eax\n"
|
---|
[59404] | 815 | " jnz .badsym\n"
|
---|
[47685] | 816 | " lea esp, [ebp - 0ch]\n"
|
---|
[46474] | 817 | " pop edx\n"
|
---|
| 818 | " pop ecx\n"
|
---|
| 819 | " pop eax\n"
|
---|
| 820 | " leave\n"
|
---|
| 821 | "%%else\n"
|
---|
| 822 | " %%error \"Unsupported architecture\"\n"
|
---|
| 823 | "%%endif\n"
|
---|
| 824 | " ret\n"
|
---|
[59404] | 825 | "\n"
|
---|
| 826 | ".badsym:\n"
|
---|
| 827 | "%%ifdef ASM_CALL64_GCC\n"
|
---|
| 828 | " mov ecx, eax\n"
|
---|
| 829 | " lea rdx, [g_szLibrary wrt rip]\n"
|
---|
| 830 | " mov rsi, r15\n"
|
---|
| 831 | " lea rdi, [g_szFailResolveFmt wrt rip]\n"
|
---|
| 832 | " sub rsp, 08h\n"
|
---|
| 833 | "%%elifdef ASM_CALL64_MSC\n"
|
---|
| 834 | " mov r9d, eax\n"
|
---|
| 835 | " mov r8, r15\n"
|
---|
| 836 | " lea rdx, [g_szLibrary wrt rip]\n"
|
---|
| 837 | " lea rcx, [g_szFailResolveFmt wrt rip]\n"
|
---|
| 838 | " sub rsp, 28h\n"
|
---|
| 839 | "%%elifdef RT_ARCH_X86\n"
|
---|
| 840 | " push eax\n"
|
---|
| 841 | " push dword [ebp + 12]\n"
|
---|
| 842 | " push g_szLibrary\n"
|
---|
| 843 | " push g_szFailResolveFmt\n"
|
---|
| 844 | "%%endif\n"
|
---|
| 845 | "%%ifdef IN_RT_R3\n"
|
---|
| 846 | " call NAME(RTAssertMsg2Weak)\n"
|
---|
| 847 | "%%else\n"
|
---|
| 848 | " call IMP2(RTAssertMsg2Weak)\n"
|
---|
| 849 | "%%endif\n"
|
---|
| 850 | ".badsymloop:\n"
|
---|
| 851 | " int3\n"
|
---|
| 852 | " jmp .badsymloop\n"
|
---|
| 853 | "\n"
|
---|
[53742] | 854 | "LazyLoadResolver_End:\n"
|
---|
[53732] | 855 | "\n"
|
---|
| 856 | "\n"
|
---|
[46474] | 857 | );
|
---|
| 858 |
|
---|
[53732] | 859 |
|
---|
| 860 |
|
---|
| 861 | /*
|
---|
| 862 | * C callable method for explicitly loading the library and optionally
|
---|
| 863 | * resolving all the imports.
|
---|
| 864 | */
|
---|
| 865 | if (g_fWithExplictLoadFunction)
|
---|
| 866 | {
|
---|
| 867 | int cchLibBaseName = (int)(strchr(g_pszLibrary, '.') ? strchr(g_pszLibrary, '.') - g_pszLibrary : strlen(g_pszLibrary));
|
---|
| 868 | fprintf(pOutput,
|
---|
| 869 | ";;\n"
|
---|
| 870 | "; ExplicitlyLoad%.*s(bool fResolveAllImports, pErrInfo);\n"
|
---|
| 871 | ";\n"
|
---|
[99539] | 872 | "%%ifdef IN_RT_R3\n"
|
---|
| 873 | "extern NAME(RTErrInfoSet)\n"
|
---|
| 874 | "%%else\n"
|
---|
[53732] | 875 | "EXTERN_IMP2 RTErrInfoSet\n"
|
---|
[99539] | 876 | "%%endif\n"
|
---|
[53732] | 877 | "BEGINCODE\n"
|
---|
| 878 | "BEGINPROC ExplicitlyLoad%.*s\n"
|
---|
| 879 | " push xBP\n"
|
---|
| 880 | " mov xBP, xSP\n"
|
---|
| 881 | " push xBX\n"
|
---|
| 882 | "%%ifdef ASM_CALL64_GCC\n"
|
---|
| 883 | " %%define pszCurStr r14\n"
|
---|
| 884 | " push r14\n"
|
---|
| 885 | "%%else\n"
|
---|
| 886 | " %%define pszCurStr xDI\n"
|
---|
| 887 | " push xDI\n"
|
---|
| 888 | "%%endif\n"
|
---|
| 889 | " sub xSP, 40h\n"
|
---|
| 890 | "\n"
|
---|
| 891 | " ;\n"
|
---|
| 892 | " ; Save parameters on stack (64-bit only).\n"
|
---|
| 893 | " ;\n"
|
---|
| 894 | "%%ifdef ASM_CALL64_GCC\n"
|
---|
| 895 | " mov [xBP - xCB * 3], rdi ; fResolveAllImports\n"
|
---|
| 896 | " mov [xBP - xCB * 4], rsi ; pErrInfo\n"
|
---|
| 897 | "%%elifdef ASM_CALL64_MSC\n"
|
---|
| 898 | " mov [xBP - xCB * 3], rcx ; fResolveAllImports\n"
|
---|
| 899 | " mov [xBP - xCB * 4], rdx ; pErrInfo\n"
|
---|
| 900 | "%%endif\n"
|
---|
| 901 | "\n"
|
---|
| 902 | " ;\n"
|
---|
| 903 | " ; Is the module already loaded?\n"
|
---|
| 904 | " ;\n"
|
---|
[53738] | 905 | " cmp RTCCPTR_PRE [g_hMod xWrtRIP], 0\n"
|
---|
[53732] | 906 | " jnz .loaded\n"
|
---|
| 907 | "\n"
|
---|
| 908 | " ;\n"
|
---|
| 909 | " ; Load the module.\n"
|
---|
| 910 | " ;\n"
|
---|
[99539] | 911 | ,
|
---|
| 912 | cchLibBaseName, g_pszLibrary,
|
---|
| 913 | cchLibBaseName, g_pszLibrary);
|
---|
| 914 | if (!g_fSystemLibrary)
|
---|
| 915 | fprintf(pOutput,
|
---|
| 916 | "%%ifdef ASM_CALL64_GCC\n"
|
---|
| 917 | " mov rcx, [xBP - xCB * 4] ; pErrInfo\n"
|
---|
| 918 | " xor rdx, rdx ; fFlags (local load)\n"
|
---|
| 919 | " lea rsi, [g_hMod wrt rip] ; phLdrMod\n"
|
---|
| 920 | " lea rdi, [g_szLibrary wrt rip] ; pszFilename\n"
|
---|
| 921 | " call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
|
---|
| 922 | "\n"
|
---|
| 923 | "%%elifdef ASM_CALL64_MSC\n"
|
---|
| 924 | " mov r9, [xBP - xCB * 4] ; pErrInfo\n"
|
---|
| 925 | " xor r8, r8 ; fFlags (local load)\n"
|
---|
| 926 | " lea rdx, [g_hMod wrt rip] ; phLdrMod\n"
|
---|
| 927 | " lea rcx, [g_szLibrary wrt rip] ; pszFilename\n"
|
---|
| 928 | " call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
|
---|
| 929 | "\n"
|
---|
| 930 | "%%elifdef RT_ARCH_X86\n"
|
---|
| 931 | " sub xSP, 0ch\n"
|
---|
| 932 | " push dword [xBP + 12] ; pErrInfo\n"
|
---|
| 933 | " push 0 ; fFlags (local load)\n"
|
---|
| 934 | " push g_hMod ; phLdrMod\n"
|
---|
| 935 | " push g_szLibrary ; pszFilename\n"
|
---|
| 936 | " call IMP2(SUPR3HardenedLdrLoadAppPriv)\n"
|
---|
| 937 | " add esp, 1ch\n"
|
---|
| 938 | "%%else\n"
|
---|
| 939 | " %%error \"Unsupported architecture\"\n"
|
---|
| 940 | "%%endif\n");
|
---|
| 941 | else
|
---|
| 942 | fprintf(pOutput,
|
---|
| 943 | "%%ifdef ASM_CALL64_GCC\n"
|
---|
| 944 | " lea rdx, [g_hMod wrt rip] ; phLdrMod\n"
|
---|
| 945 | " mov esi, 1 ; fNoUnload=true\n"
|
---|
| 946 | " lea rdi, [g_szLibrary wrt rip] ; pszFilename\n"
|
---|
| 947 | " %%ifdef IN_RT_R3\n"
|
---|
| 948 | " call NAME(RTLdrLoadSystem)\n"
|
---|
| 949 | " %%else\n"
|
---|
| 950 | " call IMP2(RTLdrLoadSystem)\n"
|
---|
| 951 | " %%endif\n"
|
---|
| 952 | "\n"
|
---|
| 953 | "%%elifdef ASM_CALL64_MSC\n"
|
---|
| 954 | " lea r8, [g_hMod wrt rip] ; phLdrMod\n"
|
---|
| 955 | " mov edx, 1 ; fNoUnload=true\n"
|
---|
| 956 | " lea rcx, [g_szLibrary wrt rip] ; pszFilename\n"
|
---|
| 957 | " %%ifdef IN_RT_R3\n"
|
---|
| 958 | " call NAME(RTLdrLoadSystem)\n"
|
---|
| 959 | " %%else\n"
|
---|
| 960 | " call IMP2(RTLdrLoadSystem)\n"
|
---|
| 961 | " %%endif\n"
|
---|
| 962 | "\n"
|
---|
| 963 | "%%elifdef RT_ARCH_X86\n"
|
---|
| 964 | " push g_hMod ; phLdrMod\n"
|
---|
| 965 | " push 1 ; fNoUnload=true\n"
|
---|
| 966 | " push g_szLibrary ; pszFilename\n"
|
---|
| 967 | " %%ifdef IN_RT_R3\n"
|
---|
| 968 | " call NAME(RTLdrLoadSystem)\n"
|
---|
| 969 | " %%else\n"
|
---|
| 970 | " call IMP2(RTLdrLoadSystem)\n"
|
---|
| 971 | " %%endif\n"
|
---|
| 972 | " add esp, 0ch\n"
|
---|
| 973 | "%%else\n"
|
---|
| 974 | " %%error \"Unsupported architecture\"\n"
|
---|
| 975 | "%%endif\n");
|
---|
| 976 | fprintf(pOutput,
|
---|
[53732] | 977 | " or eax, eax\n"
|
---|
| 978 | " jnz .return\n"
|
---|
| 979 | "\n"
|
---|
| 980 | " ;\n"
|
---|
| 981 | " ; Resolve the imports too if requested to do so.\n"
|
---|
| 982 | " ;\n"
|
---|
| 983 | ".loaded:\n"
|
---|
| 984 | "%%ifdef ASM_ARCH_X86\n"
|
---|
| 985 | " cmp byte [xBP + 8], 0\n"
|
---|
| 986 | "%%else\n"
|
---|
| 987 | " cmp byte [xBP - xCB * 3], 0\n"
|
---|
| 988 | "%%endif\n"
|
---|
| 989 | " je .return\n"
|
---|
| 990 | "\n"
|
---|
| 991 | " lea pszCurStr, [g_szzNames xWrtRIP]\n"
|
---|
| 992 | " lea xBX, [g_apfnImports xWrtRIP]\n"
|
---|
| 993 | ".next_import:\n"
|
---|
| 994 | " cmp RTCCPTR_PRE [xBX], 0\n"
|
---|
| 995 | " je .return\n"
|
---|
| 996 | "%%ifdef ASM_CALL64_GCC\n"
|
---|
| 997 | " mov rdx, xBX ; ppvValue\n"
|
---|
| 998 | " mov rsi, pszCurStr ; pszSymbol\n"
|
---|
| 999 | " mov rdi, [g_hMod wrt rip] ; hLdrMod\n"
|
---|
[99539] | 1000 | " %%ifdef IN_RT_R3\n"
|
---|
| 1001 | " call NAME(RTLdrGetSymbol)\n"
|
---|
| 1002 | " %%else\n"
|
---|
[53732] | 1003 | " call IMP2(RTLdrGetSymbol)\n"
|
---|
[99539] | 1004 | " %%endif\n"
|
---|
[53732] | 1005 | "%%elifdef ASM_CALL64_MSC\n"
|
---|
| 1006 | " mov r8, xBX ; ppvValue\n"
|
---|
| 1007 | " mov rdx, pszCurStr ; pszSymbol\n"
|
---|
| 1008 | " mov rcx, [g_hMod wrt rip] ; pszSymbol\n"
|
---|
[99539] | 1009 | " %%ifdef IN_RT_R3\n"
|
---|
| 1010 | " call NAME(RTLdrGetSymbol)\n"
|
---|
| 1011 | " %%else\n"
|
---|
[53732] | 1012 | " call IMP2(RTLdrGetSymbol)\n"
|
---|
[99539] | 1013 | " %%endif\n"
|
---|
[53732] | 1014 | "%%else\n"
|
---|
| 1015 | " push xBX ; ppvValue\n"
|
---|
| 1016 | " push pszCurStr ; pszSymbol\n"
|
---|
| 1017 | " push RTCCPTR_PRE [g_hMod] ; hLdrMod\n"
|
---|
[99539] | 1018 | " %%ifdef IN_RT_R3\n"
|
---|
| 1019 | " call NAME(RTLdrGetSymbol)\n"
|
---|
| 1020 | " %%else\n"
|
---|
[53732] | 1021 | " call IMP2(RTLdrGetSymbol)\n"
|
---|
[99539] | 1022 | " %%endif\n"
|
---|
[53732] | 1023 | " add xSP, 0ch\n"
|
---|
| 1024 | "%%endif\n"
|
---|
| 1025 | " or eax, eax\n"
|
---|
| 1026 | " jnz .symbol_error\n"
|
---|
| 1027 | "\n"
|
---|
| 1028 | " ; Advance.\n"
|
---|
| 1029 | " add xBX, RTCCPTR_CB\n"
|
---|
| 1030 | " xor eax, eax\n"
|
---|
| 1031 | " mov xCX, 0ffffffffh\n"
|
---|
| 1032 | "%%ifdef ASM_CALL64_GCC\n"
|
---|
| 1033 | " mov xDI, pszCurStr\n"
|
---|
| 1034 | " repne scasb\n"
|
---|
| 1035 | " mov pszCurStr, xDI\n"
|
---|
| 1036 | "%%else\n"
|
---|
| 1037 | " repne scasb\n"
|
---|
| 1038 | "%%endif\n"
|
---|
| 1039 | " jmp .next_import\n"
|
---|
| 1040 | "\n"
|
---|
| 1041 | " ;\n"
|
---|
| 1042 | " ; Error loading a symbol. Call RTErrInfoSet on pErrInfo (preserves eax).\n"
|
---|
| 1043 | " ;\n"
|
---|
| 1044 | ".symbol_error:\n"
|
---|
| 1045 | "%%ifdef ASM_CALL64_GCC\n"
|
---|
| 1046 | " mov rdx, pszCurStr ; pszMsg\n"
|
---|
| 1047 | " mov esi, eax ; rc\n"
|
---|
| 1048 | " mov rdi, [xBP - xCB * 4] ; pErrInfo\n"
|
---|
[99539] | 1049 | " %%ifdef IN_RT_R3\n"
|
---|
| 1050 | " call NAME(RTErrInfoSet)\n"
|
---|
| 1051 | " %%else\n"
|
---|
[53732] | 1052 | " call IMP2(RTErrInfoSet)\n"
|
---|
[99539] | 1053 | " %%endif\n"
|
---|
[53732] | 1054 | "%%elifdef ASM_CALL64_MSC\n"
|
---|
| 1055 | " mov r8, pszCurStr ; pszMsg\n"
|
---|
| 1056 | " mov edx, eax ; rc\n"
|
---|
| 1057 | " mov rcx, [xBP - xCB * 4] ; pErrInfo\n"
|
---|
[99539] | 1058 | " %%ifdef IN_RT_R3\n"
|
---|
| 1059 | " call NAME(RTErrInfoSet)\n"
|
---|
| 1060 | " %%else\n"
|
---|
[53732] | 1061 | " call IMP2(RTErrInfoSet)\n"
|
---|
[99539] | 1062 | " %%endif\n"
|
---|
[53732] | 1063 | "%%else\n"
|
---|
| 1064 | " push pszCurStr ; pszMsg\n"
|
---|
| 1065 | " push eax ; pszSymbol\n"
|
---|
[53738] | 1066 | " push dword [xBP + 0ch] ; pErrInfo\n"
|
---|
[99539] | 1067 | " %%ifdef IN_RT_R3\n"
|
---|
| 1068 | " call NAME(RTErrInfoSet)\n"
|
---|
| 1069 | " %%else\n"
|
---|
[53732] | 1070 | " call IMP2(RTErrInfoSet)\n"
|
---|
[99539] | 1071 | " %%endif\n"
|
---|
[53732] | 1072 | " add xSP, 0ch\n"
|
---|
| 1073 | "%%endif\n"
|
---|
| 1074 | " "
|
---|
| 1075 | "\n"
|
---|
| 1076 | ".return:\n"
|
---|
[54209] | 1077 | " mov pszCurStr, [xBP - xCB * 2]\n"
|
---|
| 1078 | " mov xBX, [xBP - xCB * 1]\n"
|
---|
[53732] | 1079 | " leave\n"
|
---|
| 1080 | " ret\n"
|
---|
| 1081 | "ENDPROC ExplicitlyLoad%.*s\n"
|
---|
| 1082 | "\n"
|
---|
| 1083 | "\n"
|
---|
| 1084 | ,
|
---|
[99539] | 1085 | cchLibBaseName, g_pszLibrary);
|
---|
[53732] | 1086 | }
|
---|
| 1087 |
|
---|
| 1088 |
|
---|
[46474] | 1089 | return RTEXITCODE_SUCCESS;
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
| 1092 |
|
---|
[46475] | 1093 | /**
|
---|
[87282] | 1094 | * Generates the assembly source code for ARM64, writing it
|
---|
| 1095 | * to @a pOutput.
|
---|
| 1096 | *
|
---|
| 1097 | * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE, in the latter case full
|
---|
| 1098 | * details has been displayed.
|
---|
| 1099 | * @param pOutput The output stream (caller checks it for errors
|
---|
| 1100 | * when closing).
|
---|
| 1101 | */
|
---|
| 1102 | static RTEXITCODE generateOutputInnerArm64(FILE *pOutput)
|
---|
| 1103 | {
|
---|
[100253] | 1104 | fprintf(pOutput, "/*\n");
|
---|
[87282] | 1105 | for (unsigned i = 0; i < g_cInputs; i++)
|
---|
[100253] | 1106 | fprintf(pOutput, " * Autogenerated from '%s'.\n", g_apszInputs[i]);
|
---|
[87282] | 1107 |
|
---|
| 1108 | fprintf(pOutput,
|
---|
[100253] | 1109 | " * DO NOT EDIT!\n"
|
---|
| 1110 | " */\n"
|
---|
[87282] | 1111 | "\n"
|
---|
| 1112 | "\n"
|
---|
[100253] | 1113 | "#include \"iprt/asmdefs-arm.h\"\n"
|
---|
[87282] | 1114 | "\n"
|
---|
| 1115 | "\n");
|
---|
| 1116 |
|
---|
| 1117 | /*
|
---|
| 1118 | * Put the thunks first for alignment and other reasons. It's the hot part of the code.
|
---|
| 1119 | */
|
---|
| 1120 | fprintf(pOutput,
|
---|
[100253] | 1121 | "/*\n"
|
---|
| 1122 | " * Thunks.\n"
|
---|
| 1123 | " */\n"
|
---|
| 1124 | "BEGINCODE\n");
|
---|
[87282] | 1125 | for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
|
---|
[99551] | 1126 | if (!pExp->fData)
|
---|
| 1127 | fprintf(pOutput,
|
---|
| 1128 | ".p2align 3\n"
|
---|
[100275] | 1129 | ".globl NAME(%s)\n"
|
---|
| 1130 | "NAME(%s):\n"
|
---|
| 1131 | " adrp x9, PAGE(NAME(g_pfn%s))\n"
|
---|
| 1132 | " ldr x9, [x9, PAGEOFF(NAME(g_pfn%s))]\n"
|
---|
[99551] | 1133 | " br x9\n",
|
---|
[100275] | 1134 | pExp->szName, pExp->szName, pExp->szName, pExp->szName);
|
---|
[99551] | 1135 | else
|
---|
| 1136 | fprintf(pOutput,
|
---|
| 1137 | ".p2align 3\n"
|
---|
[100275] | 1138 | ".globl NAME(LazyGetPtr_%s)\n"
|
---|
| 1139 | "NAME(LazyGetPtr_%s):\n"
|
---|
| 1140 | " adrp x9, PAGE(NAME(g_LazyPtr_%s))\n"
|
---|
| 1141 | " ldr x9, [x9, PAGEOFF(NAME(g_LazyPtr_%s))]\n"
|
---|
[99551] | 1142 | " cmp x9, #0\n"
|
---|
[100253] | 1143 | " b.eq ___LazyLoad___%s\n"
|
---|
[99551] | 1144 | " mov x0, x9\n"
|
---|
| 1145 | " ret\n",
|
---|
[100275] | 1146 | pExp->szName, pExp->szName, pExp->szName, pExp->szName, pExp->pszExportedNm);
|
---|
[87282] | 1147 | fprintf(pOutput,
|
---|
[100253] | 1148 | "ENDCODE\n"
|
---|
[87282] | 1149 | "\n"
|
---|
| 1150 | "\n");
|
---|
| 1151 |
|
---|
| 1152 | /*
|
---|
| 1153 | * Import pointers
|
---|
| 1154 | */
|
---|
| 1155 | fprintf(pOutput,
|
---|
[100253] | 1156 | "/*\n"
|
---|
| 1157 | " * Import pointers. Initialized to point to lazy loading stubs.\n"
|
---|
| 1158 | " */\n"
|
---|
| 1159 | "BEGINDATA\n"
|
---|
[93484] | 1160 | ".p2align 3\n"
|
---|
[87282] | 1161 | "g_apfnImports:\n");
|
---|
| 1162 | for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
|
---|
[99551] | 1163 | if (!pExp->fData)
|
---|
| 1164 | fprintf(pOutput,
|
---|
| 1165 | ".globl __imp_%s\n"
|
---|
| 1166 | "__imp_%s:\n"
|
---|
[100275] | 1167 | ".globl NAME(g_pfn%s)\n"
|
---|
| 1168 | "NAME(g_pfn%s):\n"
|
---|
[99551] | 1169 | " .quad ___LazyLoad___%s\n"
|
---|
| 1170 | "\n",
|
---|
| 1171 | pExp->szName, pExp->szName,
|
---|
[100275] | 1172 | pExp->szName, pExp->szName,
|
---|
[99551] | 1173 | pExp->pszExportedNm);
|
---|
| 1174 | else
|
---|
| 1175 | fprintf(pOutput,
|
---|
[100275] | 1176 | ".globl NAME(g_LazyPtr_%s)\n"
|
---|
| 1177 | "NAME(g_LazyPtr_%s):\n"
|
---|
[99551] | 1178 | " .quad 0\n"
|
---|
| 1179 | "\n",
|
---|
[100275] | 1180 | pExp->szName, pExp->szName);
|
---|
[87282] | 1181 | fprintf(pOutput,
|
---|
[100253] | 1182 | " .quad 0 /* Terminator entry for traversal. */\n"
|
---|
| 1183 | "ENDDATA\n"
|
---|
[87282] | 1184 | "\n"
|
---|
| 1185 | "\n");
|
---|
| 1186 |
|
---|
| 1187 | /*
|
---|
| 1188 | * Now for the less important stuff, starting with the names.
|
---|
| 1189 | *
|
---|
| 1190 | * We keep the names separate so we can traverse them in parallel to
|
---|
| 1191 | * g_apfnImports in the load-everything routine further down.
|
---|
| 1192 | */
|
---|
| 1193 | fprintf(pOutput,
|
---|
[100253] | 1194 | "/*\n"
|
---|
| 1195 | " * Imported names.\n"
|
---|
| 1196 | " */\n"
|
---|
[100257] | 1197 | "BEGINCONSTSTRINGS\n"
|
---|
[87282] | 1198 | "g_szLibrary:\n"
|
---|
| 1199 | " .asciz \"%s\"\n"
|
---|
| 1200 | "\n"
|
---|
| 1201 | "g_szzNames:\n",
|
---|
| 1202 | g_pszLibrary);
|
---|
| 1203 | for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
|
---|
| 1204 | if (!pExp->fNoName)
|
---|
| 1205 | fprintf(pOutput, " g_sz%s:\n .asciz \"%s\"\n", pExp->pszExportedNm, pExp->pszExportedNm);
|
---|
| 1206 | else
|
---|
| 1207 | fprintf(pOutput, " g_sz%s:\n .asciz \"#%u\"\n", pExp->pszExportedNm, pExp->uOrdinal);
|
---|
| 1208 | fprintf(pOutput,
|
---|
| 1209 | "g_EndOfNames: .byte 0\n"
|
---|
| 1210 | "\n"
|
---|
| 1211 | "g_szFailLoadFmt: .asciz \"Lazy loader failed to load \\\"%%s\\\": %%Rrc\\n\"\n"
|
---|
| 1212 | "g_szFailResolveFmt: .asciz \"Lazy loader failed to resolve symbol \\\"%%s\\\" in \\\"%%s\\\": %%Rrc\\n\"\n"
|
---|
[100257] | 1213 | "ENDCONSTSTRINGS\n"
|
---|
[87282] | 1214 | "\n"
|
---|
| 1215 | "\n");
|
---|
| 1216 |
|
---|
| 1217 | /*
|
---|
| 1218 | * The per import lazy load code.
|
---|
| 1219 | */
|
---|
| 1220 | fprintf(pOutput,
|
---|
[100253] | 1221 | "/*\n"
|
---|
| 1222 | " * Lazy load+resolve stubs.\n"
|
---|
| 1223 | " */\n"
|
---|
| 1224 | "BEGINCODE\n"
|
---|
[93484] | 1225 | ".p2align 3\n");
|
---|
[87282] | 1226 | for (PMYEXPORT pExp = g_pExpHead; pExp; pExp = pExp->pNext)
|
---|
| 1227 | {
|
---|
| 1228 | if (!pExp->fNoName)
|
---|
| 1229 | fprintf(pOutput,
|
---|
| 1230 | "___LazyLoad___%s:\n"
|
---|
[100253] | 1231 | " adrp x9, PAGE(g_sz%s)\n"
|
---|
[104213] | 1232 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1233 | " add x9, x9, PAGEOFF(g_sz%s)\n"
|
---|
[104213] | 1234 | "#else\n"
|
---|
| 1235 | " ldr x9, [x9, PAGEOFF(g_sz%s)]\n"
|
---|
| 1236 | "#endif\n"
|
---|
[100275] | 1237 | " adrp x10, PAGE(NAME(%s%s))\n"
|
---|
[104213] | 1238 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100275] | 1239 | " add x10, x10, PAGEOFF(NAME(%s%s))\n"
|
---|
[104213] | 1240 | "#else\n"
|
---|
| 1241 | " ldr x10, [x10, PAGEOFF(NAME(%s%s))]\n"
|
---|
| 1242 | "#endif\n"
|
---|
[100253] | 1243 | " mov x16, x30\n"
|
---|
[87282] | 1244 | " bl LazyLoadResolver\n"
|
---|
[100253] | 1245 | " mov x30, x16\n"
|
---|
[93484] | 1246 | , pExp->pszExportedNm,
|
---|
[104213] | 1247 | pExp->pszExportedNm, pExp->pszExportedNm, pExp->pszExportedNm,
|
---|
[100275] | 1248 | !pExp->fData ? "g_pfn" : "g_LazyPtr_", pExp->pszExportedNm,
|
---|
[104213] | 1249 | !pExp->fData ? "g_pfn" : "g_LazyPtr_", pExp->pszExportedNm,
|
---|
[100275] | 1250 | !pExp->fData ? "g_pfn" : "g_LazyPtr_", pExp->pszExportedNm);
|
---|
[87282] | 1251 | else
|
---|
| 1252 | fprintf(pOutput,
|
---|
| 1253 | "___LazyLoad___%s:\n"
|
---|
| 1254 | " movk w9, #%u\n"
|
---|
[100275] | 1255 | " adrp x10, PAGE(NAME(%s%s))\n"
|
---|
[104213] | 1256 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100275] | 1257 | " add x10, x10, PAGEOFF(NAME(%s%s))\n"
|
---|
[104213] | 1258 | "#else\n"
|
---|
| 1259 | " ldr x10, [x10, PAGEOFF(NAME(%s%s))]\n"
|
---|
| 1260 | "#endif\n"
|
---|
[93484] | 1261 | , pExp->pszExportedNm,
|
---|
| 1262 | pExp->uOrdinal,
|
---|
[100275] | 1263 | !pExp->fData ? "g_pfn" : "g_LazyPtr_", pExp->pszExportedNm,
|
---|
[104213] | 1264 | !pExp->fData ? "g_pfn" : "g_LazyPtr_", pExp->pszExportedNm,
|
---|
[100275] | 1265 | !pExp->fData ? "g_pfn" : "g_LazyPtr_", pExp->pszExportedNm);
|
---|
[99551] | 1266 | if (!pExp->fData)
|
---|
[100275] | 1267 | fprintf(pOutput, " b NAME(%s)\n", pExp->szName);
|
---|
[99551] | 1268 | else
|
---|
[100275] | 1269 | fprintf(pOutput, " b NAME(LazyGetPtr_%s)\n", pExp->szName);
|
---|
[87282] | 1270 | fprintf(pOutput, "\n");
|
---|
| 1271 | }
|
---|
| 1272 | fprintf(pOutput,
|
---|
[100253] | 1273 | "ENDCODE\n"
|
---|
[87282] | 1274 | "\n"
|
---|
| 1275 | "\n"
|
---|
| 1276 | "\n");
|
---|
| 1277 |
|
---|
| 1278 | /*
|
---|
| 1279 | * The code that does the loading and resolving.
|
---|
| 1280 | */
|
---|
| 1281 | fprintf(pOutput,
|
---|
[100253] | 1282 | "/*\n"
|
---|
| 1283 | " * The module handle.\n"
|
---|
| 1284 | " */\n"
|
---|
| 1285 | "BEGINDATA\n"
|
---|
[87282] | 1286 | "g_hMod:\n"
|
---|
| 1287 | " .quad 0\n"
|
---|
[100253] | 1288 | "ENDDATA\n"
|
---|
[87282] | 1289 | "\n"
|
---|
| 1290 | "\n"
|
---|
| 1291 | "\n");
|
---|
| 1292 |
|
---|
| 1293 | /*
|
---|
| 1294 | * Common lazy loader and resolved.
|
---|
| 1295 | */
|
---|
| 1296 | fprintf(pOutput,
|
---|
[100253] | 1297 | "/*\n"
|
---|
| 1298 | " * The resolver code.\n"
|
---|
| 1299 | " */\n"
|
---|
| 1300 | "BEGINCODE\n"
|
---|
[93484] | 1301 | ".p2align 3\n"
|
---|
[87282] | 1302 | "LazyLoadResolver:\n"
|
---|
| 1303 | " .cfi_startproc\n"
|
---|
[100253] | 1304 | " /* Create frame. */\n"
|
---|
[87282] | 1305 | " sub sp, sp, #(16 + 192)\n"
|
---|
| 1306 | " stp x29, x30, [sp, #192]\n"
|
---|
| 1307 | " add x29, sp, #192\n"
|
---|
| 1308 | " .cfi_def_cfa x29, 16\n"
|
---|
| 1309 | " .cfi_offset x30, -8\n"
|
---|
| 1310 | " .cfi_offset x29, -16\n"
|
---|
[100253] | 1311 | " /* Save all argument registers and a handful of preserved ones. */\n"
|
---|
[87282] | 1312 | " stp x0, x1, [sp, #(192 - 16)]\n"
|
---|
| 1313 | " .cfi_offset x0, -32\n"
|
---|
| 1314 | " .cfi_offset x1, -24\n"
|
---|
| 1315 | " stp x2, x3, [sp, #(192 - 32)]\n"
|
---|
| 1316 | " .cfi_offset x3, -40\n"
|
---|
| 1317 | " .cfi_offset x2, -48\n"
|
---|
| 1318 | " stp x4, x5, [sp, #(192 - 48)]\n"
|
---|
| 1319 | " .cfi_offset x6, -56\n"
|
---|
| 1320 | " .cfi_offset x5, -64\n"
|
---|
| 1321 | " stp x6, x7, [sp, #(192 - 64)]\n"
|
---|
| 1322 | " .cfi_offset x7, -72\n"
|
---|
| 1323 | " .cfi_offset x6, -80\n"
|
---|
| 1324 | " stp x16, x17, [sp, #(192 - 80)]\n"
|
---|
| 1325 | " .cfi_offset x17, -88\n"
|
---|
| 1326 | " .cfi_offset x16, -96\n"
|
---|
| 1327 | " stp x18, x19, [sp, #(192 - 96)]\n"
|
---|
| 1328 | " .cfi_offset x19, -104\n"
|
---|
| 1329 | " .cfi_offset x18, -112\n"
|
---|
| 1330 | " stp x20, x21, [sp, #(192 - 112)]\n"
|
---|
| 1331 | " .cfi_offset x21, -120\n"
|
---|
| 1332 | " .cfi_offset x20, -128\n"
|
---|
| 1333 | " stp x22, x23, [sp, #(192 - 128)]\n"
|
---|
| 1334 | " .cfi_offset x23, -136\n"
|
---|
| 1335 | " .cfi_offset x22, -144\n"
|
---|
| 1336 | " str x8, [sp, #(192 - 144)]\n"
|
---|
| 1337 | "\n"
|
---|
[100253] | 1338 | " /* Shift the symbol name to x19 and g_pfnXXXX pointer to x20 as these are preserved registers\n"
|
---|
| 1339 | " * (in case we need to call LazyLoadModule/RTLdrLoad) */\n"
|
---|
[87282] | 1340 | " mov x19, x9\n"
|
---|
| 1341 | " mov x20, x10\n"
|
---|
| 1342 | "\n"
|
---|
[100253] | 1343 | " /* Get the module handle and call RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue) */\n"
|
---|
| 1344 | " adrp x0, PAGE(g_hMod)\n"
|
---|
| 1345 | " ldr x0, [x0, PAGEOFF(g_hMod)]\n"
|
---|
[87282] | 1346 | " cmp x0, #0\n"
|
---|
[100253] | 1347 | " b.ne Lloaded\n"
|
---|
| 1348 | " bl LazyLoading\n"
|
---|
| 1349 | "Lloaded:\n"
|
---|
[87282] | 1350 | " mov x1, x19\n"
|
---|
| 1351 | " mov x2, x20\n"
|
---|
[100275] | 1352 | " bl NAME(RTLdrGetSymbol)\n"
|
---|
[87282] | 1353 | "\n"
|
---|
| 1354 | " cmp w0, #0\n"
|
---|
| 1355 | " b.eq Lreturn\n"
|
---|
| 1356 | "\n"
|
---|
[100253] | 1357 | "Lbadsym: /* Call sRTAssertMsg2Weak. Variadic (...) arguments are passed on the stack it seems. */\n"
|
---|
[87282] | 1358 | " mov x3, x0\n"
|
---|
[100253] | 1359 | " adrp x2, PAGE(g_szLibrary)\n"
|
---|
[104213] | 1360 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1361 | " add x2, x2, PAGEOFF(g_szLibrary)\n"
|
---|
[104213] | 1362 | "#else\n"
|
---|
| 1363 | " ldr x2, [x2, PAGEOFF(g_szLibrary)]\n"
|
---|
| 1364 | "#endif\n"
|
---|
[87282] | 1365 | " mov x1, x19\n"
|
---|
[100253] | 1366 | " adrp x0, PAGE(g_szFailLoadFmt)\n"
|
---|
[104213] | 1367 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1368 | " add x0, x0, PAGEOFF(g_szFailLoadFmt)\n"
|
---|
[104213] | 1369 | "#else\n"
|
---|
| 1370 | " ldr x0, [x0, PAGEOFF(g_szFailLoadFmt)]\n"
|
---|
| 1371 | "#endif\n"
|
---|
[87282] | 1372 | " stp x1, x2, [sp]\n"
|
---|
| 1373 | " str x3, [sp, #16]\n"
|
---|
[100275] | 1374 | " bl NAME(RTAssertMsg2Weak)\n"
|
---|
[87282] | 1375 | "Lbadsymloop:\n"
|
---|
| 1376 | " brk #0x1\n"
|
---|
| 1377 | " b Lbadsymloop\n"
|
---|
| 1378 |
|
---|
| 1379 | "Lreturn:\n"
|
---|
[100253] | 1380 | " /* Restore saved register */\n"
|
---|
[87282] | 1381 | " ldr x8, [sp, #(192 - 144)]\n"
|
---|
| 1382 | " .cfi_restore x8\n"
|
---|
| 1383 | " ldp x22, x23, [sp, #(192 - 128)]\n"
|
---|
| 1384 | " .cfi_restore x23\n"
|
---|
| 1385 | " .cfi_restore x22\n"
|
---|
| 1386 | " ldp x20, x21, [sp, #(192 - 112)]\n"
|
---|
| 1387 | " .cfi_restore x21\n"
|
---|
| 1388 | " .cfi_restore x20\n"
|
---|
| 1389 | " ldp x18, x19, [sp, #(192 - 96)]\n"
|
---|
| 1390 | " .cfi_restore x19\n"
|
---|
| 1391 | " .cfi_restore x18\n"
|
---|
| 1392 | " ldp x16, x17, [sp, #(192 - 80)]\n"
|
---|
| 1393 | " .cfi_restore x17\n"
|
---|
| 1394 | " .cfi_restore x18\n"
|
---|
| 1395 | " ldp x6, x7, [sp, #(192 - 64)]\n"
|
---|
| 1396 | " .cfi_restore x7\n"
|
---|
| 1397 | " .cfi_restore x6\n"
|
---|
| 1398 | " ldp x4, x5, [sp, #(192 - 48)]\n"
|
---|
| 1399 | " .cfi_restore x5\n"
|
---|
| 1400 | " .cfi_restore x4\n"
|
---|
| 1401 | " ldp x2, x3, [sp, #(192 - 32)]\n"
|
---|
| 1402 | " .cfi_restore x3\n"
|
---|
| 1403 | " .cfi_restore x2\n"
|
---|
| 1404 | " ldp x0, x1, [sp, #(192 - 16)]\n"
|
---|
| 1405 | " .cfi_restore x1\n"
|
---|
| 1406 | " .cfi_restore x0\n"
|
---|
| 1407 | "\n"
|
---|
| 1408 | " ldp x29, x30, [sp, #192]\n"
|
---|
| 1409 | " .cfi_restore x29\n"
|
---|
| 1410 | " .cfi_restore x30\n"
|
---|
| 1411 | " add sp, sp, #(16 + 192)\n"
|
---|
| 1412 | " ret\n"
|
---|
| 1413 | " .cfi_endproc\n"
|
---|
| 1414 | "\n"
|
---|
[100275] | 1415 | "\n");
|
---|
[87282] | 1416 |
|
---|
| 1417 | fprintf(pOutput,
|
---|
[100253] | 1418 | "/*\n"
|
---|
| 1419 | " * Loads the module.\n"
|
---|
| 1420 | " * ASSUMES called from LazyLoadResolver where all relevant registers are already saved.\n"
|
---|
| 1421 | " */\n"
|
---|
[87282] | 1422 | "LazyLoading:\n"
|
---|
| 1423 | " .cfi_startproc\n"
|
---|
[100253] | 1424 | " /* Create frame. */\n"
|
---|
[87282] | 1425 | " sub sp, sp, #(16 + 48)\n"
|
---|
| 1426 | " stp x29, x30, [sp, #48]\n"
|
---|
| 1427 | " add x29, sp, #48\n"
|
---|
| 1428 | " .cfi_def_cfa x29, 16\n"
|
---|
| 1429 | " .cfi_offset x30, -8\n"
|
---|
| 1430 | " .cfi_offset x29, -16\n"
|
---|
| 1431 | "\n");
|
---|
| 1432 |
|
---|
| 1433 | if (!g_fSystemLibrary)
|
---|
| 1434 | fprintf(pOutput,
|
---|
[100253] | 1435 | " /* Call SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo); */\n"
|
---|
[87282] | 1436 | " mov x3, #0\n"
|
---|
| 1437 | " mov x2, #0\n"
|
---|
[100253] | 1438 | " adrp x1, PAGE(g_hMod)\n"
|
---|
[104213] | 1439 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1440 | " add x1, x1, PAGEOFF(g_hMod)\n"
|
---|
[104213] | 1441 | "#else\n"
|
---|
| 1442 | " ldr x1, [x1, PAGEOFF(g_hMod)]\n"
|
---|
| 1443 | "#endif\n"
|
---|
[100253] | 1444 | " adrp x0, PAGE(g_szLibrary)\n"
|
---|
[104213] | 1445 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1446 | " add x0, x0, PAGEOFF(g_szLibrary)\n"
|
---|
[104213] | 1447 | "#else\n"
|
---|
| 1448 | " ldr x0, [x0, PAGEOFF(g_szLibrary)]\n"
|
---|
| 1449 | "#endif\n"
|
---|
[100275] | 1450 | " bl NAME(SUPR3HardenedLdrLoadAppPriv)\n");
|
---|
[87282] | 1451 | else
|
---|
| 1452 | fprintf(pOutput,
|
---|
[100253] | 1453 | " /* Call RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod); */\n"
|
---|
| 1454 | " adrp x2, PAGE(g_hMod)\n"
|
---|
[104213] | 1455 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1456 | " add x2, x2, PAGEOFF(g_hMod)\n"
|
---|
[104213] | 1457 | "#else\n"
|
---|
| 1458 | " ldr x2, [x2, PAGEOFF(g_hMod)]\n"
|
---|
| 1459 | "#endif\n"
|
---|
[87282] | 1460 | " mov x1, #1\n"
|
---|
[100253] | 1461 | " adrp x0, PAGE(g_szLibrary)\n"
|
---|
[104213] | 1462 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1463 | " add x0, x0, PAGEOFF(g_szLibrary)\n"
|
---|
[104213] | 1464 | "#else\n"
|
---|
| 1465 | " ldr x0, [x0, PAGEOFF(g_szLibrary)]\n"
|
---|
| 1466 | "#endif\n"
|
---|
[100275] | 1467 | " bl NAME(RTLdrLoadSystem)\n");
|
---|
[87282] | 1468 |
|
---|
| 1469 | fprintf(pOutput,
|
---|
| 1470 | " cmp w0, #0\n"
|
---|
| 1471 | " b.eq Lload_return\n"
|
---|
| 1472 | "\n"
|
---|
[100253] | 1473 | "Lbadload: /* Call sRTAssertMsg2Weak. Variadic (...) arguments are passed on the stack it seems. */\n"
|
---|
[87282] | 1474 | " mov x2, x0\n"
|
---|
[100253] | 1475 | " adrp x1, PAGE(g_szLibrary)\n"
|
---|
[104213] | 1476 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1477 | " add x1, x1, PAGEOFF(g_szLibrary)\n"
|
---|
[104213] | 1478 | "#else\n"
|
---|
| 1479 | " ldr x1, [x1, PAGEOFF(g_szLibrary)]\n"
|
---|
| 1480 | "#endif\n"
|
---|
[100253] | 1481 | " adrp x0, PAGE(g_szFailResolveFmt)\n"
|
---|
[104213] | 1482 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1483 | " add x0, x0, PAGEOFF(g_szFailResolveFmt)\n"
|
---|
[104213] | 1484 | "#else\n"
|
---|
| 1485 | " ldr x0, [x0, PAGEOFF(g_szFailResolveFmt)]\n"
|
---|
| 1486 | "#endif\n"
|
---|
[87282] | 1487 | " stp x1, x2, [sp]\n"
|
---|
[100275] | 1488 | " bl NAME(RTAssertMsg2Weak)\n"
|
---|
[87282] | 1489 | "Lbadloadloop:\n"
|
---|
| 1490 | " brk #0x1\n"
|
---|
| 1491 | " b Lbadloadloop\n"
|
---|
| 1492 | "Lload_return:\n"
|
---|
[100253] | 1493 | " adrp x0, PAGE(g_hMod)\n"
|
---|
| 1494 | " ldr x0, [x0, PAGEOFF(g_hMod)]\n"
|
---|
[87282] | 1495 | " ldp x29, x30, [sp, #48]\n"
|
---|
| 1496 | " .cfi_restore x29\n"
|
---|
| 1497 | " .cfi_restore x30\n"
|
---|
| 1498 | " add sp, sp, #(16 + 48)\n"
|
---|
| 1499 | " ret\n"
|
---|
| 1500 | " .cfi_endproc\n"
|
---|
[100253] | 1501 | "ENDCODE\n"
|
---|
[87282] | 1502 | "\n"
|
---|
[100275] | 1503 | "\n");
|
---|
[87282] | 1504 |
|
---|
| 1505 | /*
|
---|
| 1506 | * C callable method for explicitly loading the library and optionally
|
---|
| 1507 | * resolving all the imports.
|
---|
| 1508 | */
|
---|
| 1509 | if (g_fWithExplictLoadFunction)
|
---|
| 1510 | {
|
---|
| 1511 | int cchLibBaseName = (int)(strchr(g_pszLibrary, '.') ? strchr(g_pszLibrary, '.') - g_pszLibrary : strlen(g_pszLibrary));
|
---|
| 1512 | fprintf(pOutput,
|
---|
[100253] | 1513 | "/**\n"
|
---|
| 1514 | " * ExplicitlyLoad%.*s(bool fResolveAllImports, pErrInfo);\n"
|
---|
| 1515 | " */\n"
|
---|
| 1516 | "BEGINCODE\n"
|
---|
[93484] | 1517 | ".p2align 3\n"
|
---|
[100275] | 1518 | ".globl NAME(ExplicitlyLoad%.*s)\n"
|
---|
| 1519 | "NAME(ExplicitlyLoad%.*s):\n"
|
---|
[87282] | 1520 | " .cfi_startproc\n"
|
---|
[100253] | 1521 | " /* Create frame. */\n"
|
---|
[87287] | 1522 | " sub sp, sp, #(16 + 96)\n"
|
---|
[87282] | 1523 | " stp x29, x30, [sp, #96]\n"
|
---|
| 1524 | " add x29, sp, #96\n"
|
---|
| 1525 | " .cfi_def_cfa x29, 16\n"
|
---|
| 1526 | " .cfi_offset x30, -8\n"
|
---|
| 1527 | " .cfi_offset x29, -16\n"
|
---|
| 1528 | "\n"
|
---|
| 1529 | " stp x20, x21, [sp, #(96 - 16)]\n"
|
---|
| 1530 | " .cfi_offset x21, -24\n"
|
---|
| 1531 | " .cfi_offset x20, -32\n"
|
---|
| 1532 | " stp x22, x23, [sp, #(96 - 32)]\n"
|
---|
| 1533 | " .cfi_offset x23, -40\n"
|
---|
| 1534 | " .cfi_offset x22, -48\n"
|
---|
| 1535 |
|
---|
[100253] | 1536 | " /* Save the input parameters. */\n"
|
---|
[87282] | 1537 | " mov x20, x0\n"
|
---|
| 1538 | " mov x21, x1\n"
|
---|
| 1539 | "\n"
|
---|
[100253] | 1540 | " /*\n"
|
---|
| 1541 | " * Is the module already loaded?\n"
|
---|
| 1542 | " */\n"
|
---|
| 1543 | " adrp x0, PAGE(g_hMod)\n"
|
---|
| 1544 | " ldr x0, [x0, PAGEOFF(g_hMod)]\n"
|
---|
[87282] | 1545 | " cmp x0, #0\n"
|
---|
| 1546 | " b.ne Lexplicit_loaded_module\n"
|
---|
| 1547 | "\n"
|
---|
| 1548 | ,
|
---|
| 1549 | cchLibBaseName, g_pszLibrary,
|
---|
[100275] | 1550 | cchLibBaseName, g_pszLibrary,
|
---|
| 1551 | cchLibBaseName, g_pszLibrary);
|
---|
[87282] | 1552 | fprintf(pOutput,
|
---|
[99540] | 1553 | "Lexplicit_load_module:\n");
|
---|
| 1554 | if (!g_fSystemLibrary)
|
---|
| 1555 | fprintf(pOutput,
|
---|
[100253] | 1556 | " /* Call SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo); */\n"
|
---|
[99540] | 1557 | " mov x3, x21\n"
|
---|
| 1558 | " mov x2, #0\n"
|
---|
[100253] | 1559 | " adrp x1, PAGE(g_hMod)\n"
|
---|
[104213] | 1560 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1561 | " add x1, x1, PAGEOFF(g_hMod)\n"
|
---|
[104213] | 1562 | "#else\n"
|
---|
| 1563 | " ldr x1, [x1, PAGEOFF(g_hMod)]\n"
|
---|
| 1564 | "#endif\n"
|
---|
[100253] | 1565 | " adrp x0, PAGE(g_szLibrary)\n"
|
---|
[104213] | 1566 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1567 | " add x0, x0, PAGEOFF(g_szLibrary)\n"
|
---|
[104213] | 1568 | "#else\n"
|
---|
| 1569 | " ldr x0, [x0, PAGEOFF(g_szLibrary)]\n"
|
---|
| 1570 | "#endif\n"
|
---|
[100275] | 1571 | " bl NAME(SUPR3HardenedLdrLoadAppPriv)\n");
|
---|
[99540] | 1572 | else
|
---|
| 1573 | fprintf(pOutput,
|
---|
[100253] | 1574 | " /* Call RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod); */\n"
|
---|
| 1575 | " adrp x2, PAGE(g_hMod)\n"
|
---|
[104213] | 1576 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1577 | " add x2, x2, PAGEOFF(g_hMod)\n"
|
---|
[104213] | 1578 | "#else\n"
|
---|
| 1579 | " ldr x2, [x2, PAGEOFF(g_hMod)]\n"
|
---|
| 1580 | "#endif\n"
|
---|
[99540] | 1581 | " mov x1, #1\n"
|
---|
[100253] | 1582 | " adrp x0, PAGE(g_szLibrary)\n"
|
---|
[104213] | 1583 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1584 | " add x0, x0, PAGEOFF(g_szLibrary)\n"
|
---|
[104213] | 1585 | "#else\n"
|
---|
| 1586 | " ldr x0, [x0, PAGEOFF(g_szLibrary)]\n"
|
---|
| 1587 | "#endif\n"
|
---|
[100275] | 1588 | " bl NAME(RTLdrLoadSystem)\n");
|
---|
[99540] | 1589 | fprintf(pOutput,
|
---|
[87282] | 1590 | " cmp x0, #0\n"
|
---|
| 1591 | " b.ne Lexplicit_load_return\n"
|
---|
[99540] | 1592 | "\n");
|
---|
[87282] | 1593 |
|
---|
| 1594 | fprintf(pOutput,
|
---|
[100253] | 1595 | " /*\n"
|
---|
| 1596 | " * Resolve the imports too if requested to do so.\n"
|
---|
| 1597 | " */\n"
|
---|
[87282] | 1598 | "Lexplicit_loaded_module:\n"
|
---|
| 1599 | " cmp w20, #0\n"
|
---|
| 1600 | " b.eq Lexplicit_load_return\n"
|
---|
| 1601 | "\n"
|
---|
[100253] | 1602 | " adrp x22, PAGE(g_szzNames)\n"
|
---|
[104213] | 1603 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1604 | " add x22, x22, PAGEOFF(g_szzNames)\n"
|
---|
[104213] | 1605 | "#else\n"
|
---|
| 1606 | " ldr x22, [x22, PAGEOFF(g_szzNames)]\n"
|
---|
| 1607 | "#endif\n"
|
---|
[100253] | 1608 | " adrp x23, PAGE(g_apfnImports)\n"
|
---|
[104213] | 1609 | "#ifndef ASM_FORMAT_ELF\n"
|
---|
[100253] | 1610 | " add x23, x23, PAGEOFF(g_apfnImports)\n"
|
---|
[104213] | 1611 | "#else\n"
|
---|
| 1612 | " ldr x23, [x23, PAGEOFF(g_apfnImports)]\n"
|
---|
| 1613 | "#endif\n"
|
---|
[87282] | 1614 | "Lexplicit_load_next_import:\n"
|
---|
| 1615 | " ldr x0, [x23]\n"
|
---|
| 1616 | " cmp x0, #0\n"
|
---|
| 1617 | " b.eq Lexplicit_load_return\n"
|
---|
| 1618 | "\n"
|
---|
[100253] | 1619 | " /* Get the module handle and call RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue) */\n"
|
---|
| 1620 | " adrp x0, PAGE(g_hMod)\n"
|
---|
| 1621 | " ldr x0, [x0, PAGEOFF(g_hMod)]\n"
|
---|
[87282] | 1622 | " mov x1, x22\n"
|
---|
| 1623 | " mov x2, x23\n"
|
---|
[100275] | 1624 | " bl NAME(RTLdrGetSymbol)\n"
|
---|
[87282] | 1625 | " cmp x0, #0\n"
|
---|
| 1626 | " b.ne Lexplicit_load_symbol_error\n"
|
---|
| 1627 | "\n"
|
---|
[100253] | 1628 | " /* Advance. */\n"
|
---|
[87282] | 1629 | " add x23, x23, #8\n"
|
---|
| 1630 | "Lexplict_load_advance_string:\n"
|
---|
| 1631 | " ldrb w0, [x22]\n"
|
---|
| 1632 | " add x22, x22, #1\n"
|
---|
| 1633 | " cmp w0, #0\n"
|
---|
| 1634 | " b.ne Lexplict_load_advance_string\n"
|
---|
| 1635 | " b Lexplicit_load_next_import\n"
|
---|
| 1636 | "\n"
|
---|
[100253] | 1637 | " /*\n"
|
---|
| 1638 | " * Error loading a symbol. Call RTErrInfoSet(PRTERRINFO pErrInfo, int rc, const char *pszMsg) on pErrInfo (preserves x0).\n"
|
---|
| 1639 | " */\n"
|
---|
[87282] | 1640 | "Lexplicit_load_symbol_error:\n"
|
---|
| 1641 | " mov x2, x22\n"
|
---|
| 1642 | " mov x1, x0\n"
|
---|
| 1643 | " mov x0, x21\n"
|
---|
[100275] | 1644 | " bl NAME(RTErrInfoSet)\n"
|
---|
[87282] | 1645 | " b Lexplicit_load_return"
|
---|
| 1646 | " "
|
---|
| 1647 | "\n"
|
---|
| 1648 | "Lexplicit_load_return:\n"
|
---|
| 1649 | " ldp x22, x23, [sp, #(96 - 32)]\n"
|
---|
| 1650 | " .cfi_restore x23\n"
|
---|
| 1651 | " .cfi_restore x22\n"
|
---|
| 1652 | " ldp x20, x21, [sp, #(96 - 16)]\n"
|
---|
| 1653 | " .cfi_restore x21\n"
|
---|
| 1654 | " .cfi_restore x20\n"
|
---|
| 1655 | "\n"
|
---|
| 1656 | " ldp x29, x30, [sp, #96]\n"
|
---|
| 1657 | " .cfi_restore x29\n"
|
---|
| 1658 | " .cfi_restore x30\n"
|
---|
| 1659 | " add sp, sp, #(16 + 96)\n"
|
---|
| 1660 | " ret\n"
|
---|
| 1661 | " .cfi_endproc\n"
|
---|
[100253] | 1662 | "ENDCODE\n"
|
---|
[87282] | 1663 | "\n"
|
---|
[100275] | 1664 | "\n");
|
---|
[87282] | 1665 | }
|
---|
| 1666 |
|
---|
| 1667 | return RTEXITCODE_SUCCESS;
|
---|
| 1668 | }
|
---|
| 1669 |
|
---|
| 1670 |
|
---|
| 1671 | /**
|
---|
[46475] | 1672 | * Generates the assembly source code, writing it to g_pszOutput.
|
---|
| 1673 | *
|
---|
| 1674 | * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE, in the latter case full
|
---|
| 1675 | * details has been displayed.
|
---|
| 1676 | */
|
---|
[46474] | 1677 | static RTEXITCODE generateOutput(void)
|
---|
| 1678 | {
|
---|
| 1679 | RTEXITCODE rcExit = RTEXITCODE_FAILURE;
|
---|
| 1680 | FILE *pOutput = fopen(g_pszOutput, "w");
|
---|
| 1681 | if (pOutput)
|
---|
| 1682 | {
|
---|
[87282] | 1683 | switch (g_enmTarget)
|
---|
| 1684 | {
|
---|
| 1685 | case RTLDRARCH_AMD64:
|
---|
| 1686 | case RTLDRARCH_X86_32:
|
---|
| 1687 | rcExit = generateOutputInnerX86AndAMD64(pOutput);
|
---|
| 1688 | break;
|
---|
| 1689 | case RTLDRARCH_ARM64:
|
---|
| 1690 | rcExit = generateOutputInnerArm64(pOutput);
|
---|
| 1691 | break;
|
---|
| 1692 | default:
|
---|
| 1693 | rcExit = RTEXITCODE_FAILURE;
|
---|
| 1694 | break;
|
---|
| 1695 | }
|
---|
[46474] | 1696 | if (fclose(pOutput))
|
---|
| 1697 | {
|
---|
| 1698 | fprintf(stderr, "error: Error closing '%s'.\n", g_pszOutput);
|
---|
| 1699 | rcExit = RTEXITCODE_FAILURE;
|
---|
| 1700 | }
|
---|
| 1701 | }
|
---|
| 1702 | else
|
---|
| 1703 | fprintf(stderr, "error: Failed to open '%s' for writing.\n", g_pszOutput);
|
---|
| 1704 | return rcExit;
|
---|
| 1705 | }
|
---|
| 1706 |
|
---|
| 1707 |
|
---|
[46475] | 1708 | /**
|
---|
| 1709 | * Displays usage information.
|
---|
| 1710 | *
|
---|
| 1711 | * @returns RTEXITCODE_SUCCESS.
|
---|
| 1712 | * @param pszArgv0 The argv[0] string.
|
---|
| 1713 | */
|
---|
[46474] | 1714 | static int usage(const char *pszArgv0)
|
---|
| 1715 | {
|
---|
[99552] | 1716 | const char *pszTmp = strrchr(pszArgv0, '/');
|
---|
| 1717 | if (pszTmp)
|
---|
| 1718 | pszArgv0 = pszTmp + 1;
|
---|
| 1719 | pszTmp = strrchr(pszArgv0, '\\');
|
---|
| 1720 | if (pszTmp)
|
---|
| 1721 | pszArgv0 = pszTmp + 1;
|
---|
| 1722 |
|
---|
| 1723 | /* 0 1 2 3 4 5 6 7 8
|
---|
| 1724 | 012345678901234567890123456789012345678901234567890123456789012345678901234567890 */
|
---|
| 1725 | printf("VBoxDef2LazyLoad - Lazy DLL/SO/DYLIB loader code generator.\n"
|
---|
| 1726 | "Copyright (C) 2013-2016 Oracle Corporation\n"
|
---|
[46474] | 1727 | "\n"
|
---|
[99552] | 1728 | "Description:\n"
|
---|
| 1729 | "------------\n"
|
---|
| 1730 | "\n"
|
---|
| 1731 | "Takes a Microsoft-style linker definition file for a library (DLL/SO/DYLIB) and\n"
|
---|
| 1732 | "generates assembly code which defines stub functions that lazily loads and\n"
|
---|
| 1733 | "resolves the real symbols before calling them. This is entirely transparent when\n"
|
---|
| 1734 | "used with functions.\n"
|
---|
| 1735 | "\n"
|
---|
| 1736 | "With data symbols it's more messy since the compiler will not invoke code when\n"
|
---|
| 1737 | "using them, but access them directly (ELF executables) or indirectly (ELF SOs,\n"
|
---|
| 1738 | "PE, ++). For data symbols use the DATA keyword after the symbol name in the\n"
|
---|
| 1739 | "def-file and modify the header definition from 'extern type symbol;' to:\n"
|
---|
| 1740 | "\n"
|
---|
| 1741 | " DECLASM(type *) LazyGetPtr_<symbol>(void);\n"
|
---|
| 1742 | " #define <symbol> (*LazyGetPtr_<symbol>())\n"
|
---|
| 1743 | "\n"
|
---|
| 1744 | "or, if using --explict-load-function this will work as well:\n"
|
---|
| 1745 | "\n"
|
---|
| 1746 | " extern type *g_LazyPtr_<symbol>;\n"
|
---|
| 1747 | " #define <symbol> (*g_LazyPtr_)\n"
|
---|
| 1748 | "\n"
|
---|
| 1749 | "Usage:\n"
|
---|
| 1750 | "------\n"
|
---|
| 1751 | "%s [options] --libary <loadname> --output <lazyload.asm> <input.def>\n"
|
---|
| 1752 | "\n"
|
---|
[53732] | 1753 | "Options:\n"
|
---|
[99552] | 1754 | "--------\n"
|
---|
| 1755 | " --library <loadname>, -l <loadname>\n"
|
---|
| 1756 | " The name of the library. This is what will be passed to RTLdrLoadSystem\n"
|
---|
| 1757 | " or SUPR3HardenedLdrLoadAppPriv.\n"
|
---|
| 1758 | " --output <filename>, -o <filename>\n"
|
---|
| 1759 | " The assembly output file.\n"
|
---|
[53732] | 1760 | " --explicit-load-function, --no-explicit-load-function\n"
|
---|
[99552] | 1761 | " Whether to include the explicit load function:\n"
|
---|
| 1762 | " DECLASM(int) ExplicitlyLoad<basename>(bool fResolveAllImports, pErrInfo);\n"
|
---|
| 1763 | " The default is not to include it.\n"
|
---|
| 1764 | " --system\n"
|
---|
| 1765 | " The library is a system DLL to be loaded using RTLdrLoadSystem.\n"
|
---|
| 1766 | " The default is to use SUPR3HardenedLdrLoadAppPriv to load it.\n"
|
---|
[53732] | 1767 | "\n"
|
---|
[46474] | 1768 | , pszArgv0);
|
---|
| 1769 |
|
---|
| 1770 | return RTEXITCODE_SUCCESS;
|
---|
| 1771 | }
|
---|
| 1772 |
|
---|
| 1773 |
|
---|
| 1774 | int main(int argc, char **argv)
|
---|
| 1775 | {
|
---|
| 1776 | /*
|
---|
| 1777 | * Parse options.
|
---|
| 1778 | */
|
---|
| 1779 | for (int i = 1; i < argc; i++)
|
---|
| 1780 | {
|
---|
| 1781 | const char *psz = argv[i];
|
---|
| 1782 | if (*psz == '-')
|
---|
| 1783 | {
|
---|
| 1784 | if (!strcmp(psz, "--output") || !strcmp(psz, "-o"))
|
---|
| 1785 | {
|
---|
| 1786 | if (++i >= argc)
|
---|
| 1787 | {
|
---|
| 1788 | fprintf(stderr, "syntax error: File name expected after '%s'.\n", psz);
|
---|
| 1789 | return RTEXITCODE_SYNTAX;
|
---|
| 1790 | }
|
---|
| 1791 | g_pszOutput = argv[i];
|
---|
| 1792 | }
|
---|
| 1793 | else if (!strcmp(psz, "--library") || !strcmp(psz, "-l"))
|
---|
| 1794 | {
|
---|
| 1795 | if (++i >= argc)
|
---|
| 1796 | {
|
---|
| 1797 | fprintf(stderr, "syntax error: Library name expected after '%s'.\n", psz);
|
---|
| 1798 | return RTEXITCODE_SYNTAX;
|
---|
| 1799 | }
|
---|
| 1800 | g_pszLibrary = argv[i];
|
---|
| 1801 | }
|
---|
[53732] | 1802 | else if (!strcmp(psz, "--explicit-load-function"))
|
---|
| 1803 | g_fWithExplictLoadFunction = true;
|
---|
| 1804 | else if (!strcmp(psz, "--no-explicit-load-function"))
|
---|
| 1805 | g_fWithExplictLoadFunction = false;
|
---|
[59404] | 1806 | else if (!strcmp(psz, "--system"))
|
---|
| 1807 | g_fSystemLibrary = true;
|
---|
[46475] | 1808 | /** @todo Support different load methods so this can be used on system libs and
|
---|
| 1809 | * such if we like. */
|
---|
[46474] | 1810 | else if ( !strcmp(psz, "--help")
|
---|
| 1811 | || !strcmp(psz, "-help")
|
---|
| 1812 | || !strcmp(psz, "-h")
|
---|
| 1813 | || !strcmp(psz, "-?") )
|
---|
| 1814 | return usage(argv[0]);
|
---|
| 1815 | else if ( !strcmp(psz, "--version")
|
---|
| 1816 | || !strcmp(psz, "-V"))
|
---|
| 1817 | {
|
---|
| 1818 | printf("$Revision: 104213 $\n");
|
---|
| 1819 | return RTEXITCODE_SUCCESS;
|
---|
| 1820 | }
|
---|
| 1821 | else
|
---|
| 1822 | {
|
---|
| 1823 | fprintf(stderr, "syntax error: Unknown option '%s'.\n", psz);
|
---|
| 1824 | return RTEXITCODE_SYNTAX;
|
---|
| 1825 | }
|
---|
| 1826 | }
|
---|
| 1827 | else
|
---|
| 1828 | {
|
---|
[53732] | 1829 | if (g_cInputs >= RT_ELEMENTS(g_apszInputs))
|
---|
[46474] | 1830 | {
|
---|
[53732] | 1831 | fprintf(stderr, "syntax error: Too many input files, max is %d.\n", (int)RT_ELEMENTS(g_apszInputs));
|
---|
[46474] | 1832 | return RTEXITCODE_SYNTAX;
|
---|
| 1833 | }
|
---|
[53732] | 1834 | g_apszInputs[g_cInputs++] = argv[i];
|
---|
[46474] | 1835 | }
|
---|
| 1836 | }
|
---|
[53732] | 1837 | if (g_cInputs == 0)
|
---|
[46474] | 1838 | {
|
---|
| 1839 | fprintf(stderr, "syntax error: No input file specified.\n");
|
---|
| 1840 | return RTEXITCODE_SYNTAX;
|
---|
| 1841 | }
|
---|
| 1842 | if (!g_pszOutput)
|
---|
| 1843 | {
|
---|
| 1844 | fprintf(stderr, "syntax error: No output file specified.\n");
|
---|
| 1845 | return RTEXITCODE_SYNTAX;
|
---|
| 1846 | }
|
---|
| 1847 | if (!g_pszLibrary)
|
---|
| 1848 | {
|
---|
| 1849 | fprintf(stderr, "syntax error: No library name specified.\n");
|
---|
| 1850 | return RTEXITCODE_SYNTAX;
|
---|
| 1851 | }
|
---|
| 1852 |
|
---|
| 1853 | /*
|
---|
| 1854 | * Do the job.
|
---|
| 1855 | */
|
---|
[53732] | 1856 | RTEXITCODE rcExit = parseInputs();
|
---|
[46474] | 1857 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
| 1858 | rcExit = generateOutput();
|
---|
| 1859 | return rcExit;
|
---|
| 1860 | }
|
---|
| 1861 |
|
---|