VirtualBox

source: vbox/trunk/src/bldprogs/VBoxDef2LazyLoad.cpp@ 106605

Last change on this file since 106605 was 106605, checked in by vboxsync, 5 weeks ago

bldprogs/VBoxDef2LazyLoad,Installer/win/Stub: Made the VBoxDef2LazyLoad more cross platform friendly. jiraref:VBP-1171

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette