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