| [2965] | 1 | /* $Id: tst-3-driver.c 3601 2007-10-29 00:21:13Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kLdr - Dynamic Loader testcase no. 3, Driver.
|
|---|
| [3601] | 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
|
|---|
| [2965] | 8 | *
|
|---|
| [3601] | 9 | * This file is part of kStuff.
|
|---|
| [2965] | 10 | *
|
|---|
| [3601] | 11 | * kStuff is free software; you can redistribute it and/or
|
|---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 13 | * License as published by the Free Software Foundation; either
|
|---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| [2965] | 15 | *
|
|---|
| [3601] | 16 | * kStuff is distributed in the hope that it will be useful,
|
|---|
| [2965] | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| [3601] | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 19 | * Lesser General Public License for more details.
|
|---|
| [2965] | 20 | *
|
|---|
| [3601] | 21 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 22 | * License along with kStuff; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| [2965] | 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | /*******************************************************************************
|
|---|
| 28 | * Header Files *
|
|---|
| 29 | *******************************************************************************/
|
|---|
| 30 | #include "tst.h"
|
|---|
| [3584] | 31 | #include <k/kErr.h>
|
|---|
| 32 |
|
|---|
| [2965] | 33 | #include <stdarg.h>
|
|---|
| 34 | #include <stdio.h>
|
|---|
| 35 | #include <stdlib.h>
|
|---|
| 36 | #include <string.h>
|
|---|
| 37 | #ifdef _MSC_VER
|
|---|
| 38 | # include <malloc.h>
|
|---|
| 39 | #endif
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | /*******************************************************************************
|
|---|
| 43 | * Defined Constants And Macros *
|
|---|
| 44 | *******************************************************************************/
|
|---|
| 45 | /** Select the appropriate KLDRSYMKIND bit define. */
|
|---|
| 46 | #define MY_KLDRSYMKIND_BITS ( sizeof(void *) == 4 ? KLDRSYMKIND_32BIT : KLDRSYMKIND_64BIT )
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | /*******************************************************************************
|
|---|
| 50 | * Global Variables *
|
|---|
| 51 | *******************************************************************************/
|
|---|
| 52 | /** The numbers of errors. */
|
|---|
| 53 | static int g_cErrors = 0;
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Report failure.
|
|---|
| 58 | */
|
|---|
| 59 | static int Failure(const char *pszFormat, ...)
|
|---|
| 60 | {
|
|---|
| 61 | va_list va;
|
|---|
| 62 |
|
|---|
| 63 | g_cErrors++;
|
|---|
| 64 |
|
|---|
| 65 | printf("tst-3-driver: ");
|
|---|
| 66 | va_start(va, pszFormat);
|
|---|
| 67 | vprintf(pszFormat, va);
|
|---|
| 68 | va_end(va);
|
|---|
| 69 | printf("\n");
|
|---|
| 70 | return 1;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| [2967] | 74 | /**
|
|---|
| 75 | * External symbol used by the testcase module.
|
|---|
| 76 | */
|
|---|
| 77 | static int Tst3Ext(int iFortyTwo)
|
|---|
| 78 | {
|
|---|
| 79 | if (iFortyTwo != 42)
|
|---|
| 80 | return 256;
|
|---|
| 81 | return 42;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Callback for resolving the Tst3Ext import.
|
|---|
| 87 | */
|
|---|
| [3584] | 88 | static int GetImport(PKLDRMOD pMod, KU32 iImport, KU32 iSymbol, const char *pchSymbol, KSIZE cchSymbol,
|
|---|
| 89 | const char *pszVersion, PKLDRADDR puValue, KU32 *pfKind, void *pvUser)
|
|---|
| [2965] | 90 | {
|
|---|
| [2967] | 91 | if (*pfKind != KLDRSYMKIND_REQ_FLAT)
|
|---|
| 92 | return -1;
|
|---|
| 93 |
|
|---|
| 94 | if ( !strncmp(pchSymbol, "Tst3Ext", strlen("Tst3Ext"))
|
|---|
| 95 | || !strncmp(pchSymbol, "_Tst3Ext", strlen("_Tst3Ext")))
|
|---|
| 96 | {
|
|---|
| [3584] | 97 | *puValue = (KUPTR)&Tst3Ext;
|
|---|
| [2967] | 98 | *pfKind = KLDRSYMKIND_CODE | (sizeof(pfKind) == 4 ? KLDRSYMKIND_32BIT : KLDRSYMKIND_64BIT);
|
|---|
| 99 | return 0;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | return -2;
|
|---|
| [2965] | 103 | }
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Performs the tests on one module.
|
|---|
| 108 | * @returns non sense.
|
|---|
| 109 | */
|
|---|
| 110 | int TestModule(const char *pszFile)
|
|---|
| 111 | {
|
|---|
| 112 | PKLDRMOD pMod;
|
|---|
| 113 | KLDRSIZE cbImage;
|
|---|
| 114 | void *pvBits;
|
|---|
| 115 | int rc;
|
|---|
| 116 |
|
|---|
| 117 | printf("tst-3-driver: testing '%s'...\n", pszFile);
|
|---|
| 118 |
|
|---|
| 119 | /* open it. */
|
|---|
| 120 | rc = kLdrModOpen(pszFile, &pMod);
|
|---|
| 121 | if (rc)
|
|---|
| [3579] | 122 | return Failure("kLdrModOpen(%s,) -> %#d (%s)\n", pszFile, rc, kErrName(rc));
|
|---|
| [2965] | 123 |
|
|---|
| 124 | /* get bits. */
|
|---|
| 125 | cbImage = kLdrModSize(pMod);
|
|---|
| [3584] | 126 | pvBits = malloc((KSIZE)cbImage + 0xfff);
|
|---|
| [2965] | 127 | if (pvBits)
|
|---|
| 128 | {
|
|---|
| [3584] | 129 | void *pvBits2 = (void *)( ((KUPTR)pvBits + 0xfff) & ~(KUPTR)0xfff );
|
|---|
| [2967] | 130 |
|
|---|
| [3584] | 131 | KLDRADDR BaseAddress = (KUPTR)pvBits2;
|
|---|
| [2967] | 132 | rc = kLdrModGetBits(pMod, pvBits2, BaseAddress, GetImport, NULL);
|
|---|
| [2965] | 133 | if (!rc)
|
|---|
| 134 | {
|
|---|
| 135 | KLDRADDR EntryPoint;
|
|---|
| 136 |
|
|---|
| 137 | /* call into it */
|
|---|
| [2967] | 138 | rc = kLdrModQuerySymbol(pMod, pvBits2, BaseAddress, NIL_KLDRMOD_SYM_ORDINAL, "_Tst3", strlen("_Tst3"), NULL, NULL, NULL,
|
|---|
| [2965] | 139 | &EntryPoint, NULL);
|
|---|
| [2967] | 140 | if (rc == KLDR_ERR_SYMBOL_NOT_FOUND)
|
|---|
| 141 | rc = kLdrModQuerySymbol(pMod, pvBits2, BaseAddress, NIL_KLDRMOD_SYM_ORDINAL, "Tst3", strlen("Tst3"), NULL, NULL, NULL,
|
|---|
| 142 | &EntryPoint, NULL);
|
|---|
| [2965] | 143 | if (!rc)
|
|---|
| 144 | {
|
|---|
| [3584] | 145 | int (*pfnEntryPoint)(int) = (int (*)(int)) ((KUPTR)EntryPoint);
|
|---|
| [2965] | 146 | rc = pfnEntryPoint(42);
|
|---|
| 147 | if (rc == 42)
|
|---|
| 148 | {
|
|---|
| 149 | /* relocate twice and try again. */
|
|---|
| [2967] | 150 | rc = kLdrModRelocateBits(pMod, pvBits2, BaseAddress + 0x22000, BaseAddress, GetImport, NULL);
|
|---|
| [2965] | 151 | if (!rc)
|
|---|
| 152 | {
|
|---|
| [2967] | 153 | rc = kLdrModRelocateBits(pMod, pvBits2, BaseAddress, BaseAddress + 0x22000, GetImport, NULL);
|
|---|
| [2965] | 154 | if (!rc)
|
|---|
| 155 | {
|
|---|
| 156 | rc = pfnEntryPoint(42);
|
|---|
| 157 | if (rc == 42)
|
|---|
| 158 | {
|
|---|
| 159 | printf("tst-3-driver: success.\n");
|
|---|
| 160 | }
|
|---|
| 161 | else
|
|---|
| 162 | Failure("pfnEntryPoint(42) -> %d (2nd)\n", rc);
|
|---|
| 163 | }
|
|---|
| 164 | else
|
|---|
| [3579] | 165 | Failure("kLdrModRelocateBits(,,, + 0x22000,,,) -> %#x (%s)\n", rc, kErrName(rc));
|
|---|
| [2965] | 166 | }
|
|---|
| 167 | else
|
|---|
| [3579] | 168 | Failure("kLdrModRelocateBits(,, + 0x22000,,,,) -> %#x (%s)\n", rc, kErrName(rc));
|
|---|
| [2965] | 169 | }
|
|---|
| 170 | else
|
|---|
| 171 | Failure("pfnEntryPoint(42) -> %d (1st)\n", rc);
|
|---|
| 172 | }
|
|---|
| 173 | else
|
|---|
| [3579] | 174 | Failure("kLdrModQuerySymbol -> %#x (%s)\n", rc, kErrName(rc));
|
|---|
| [2965] | 175 | }
|
|---|
| 176 | else
|
|---|
| [3579] | 177 | Failure("kLdrModGetBits -> %#x (%s)\n", rc, kErrName(rc));
|
|---|
| [2965] | 178 | free(pvBits);
|
|---|
| 179 | }
|
|---|
| 180 | else
|
|---|
| 181 | Failure("malloc(%lx) -> NULL\n", (long)cbImage);
|
|---|
| 182 |
|
|---|
| 183 | /* clean up */
|
|---|
| 184 | rc = kLdrModClose(pMod);
|
|---|
| 185 | if (rc)
|
|---|
| [3579] | 186 | Failure("kLdrModOpen(%s,) -> %#x (%s)\n", pszFile, rc, kErrName(rc));
|
|---|
| [2965] | 187 | return 0;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 | int main(int argc, char **argv)
|
|---|
| 192 | {
|
|---|
| 193 | int i;
|
|---|
| 194 |
|
|---|
| 195 | /*
|
|---|
| 196 | * Test all the given modules (requires arguments).
|
|---|
| 197 | */
|
|---|
| 198 | for (i = 1; i < argc; i++)
|
|---|
| 199 | {
|
|---|
| 200 | TestModule(argv[i]);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 | /*
|
|---|
| 205 | * Summary
|
|---|
| 206 | */
|
|---|
| 207 | if (!g_cErrors)
|
|---|
| [2967] | 208 | printf("tst-3-driver: SUCCESS\n");
|
|---|
| [2965] | 209 | else
|
|---|
| [2967] | 210 | printf("tst-3-driver: FAILURE - %d errors\n", g_cErrors);
|
|---|
| [2965] | 211 | return !!g_cErrors;
|
|---|
| 212 | }
|
|---|