source: trunk/kLdr/testcase/tst-3-driver.c@ 2966

Last change on this file since 2966 was 2965, checked in by bird, 19 years ago

messing about...

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1/* $Id: tst-3-driver.c 2965 2007-02-13 21:04:40Z 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 <kLdr.h>
32#include "tst.h"
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. */
53static int g_cErrors = 0;
54
55
56/**
57 * Report failure.
58 */
59static 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
74static int GetImport(PKLDRMOD pMod, uint32_t iImport, uint32_t iSymbol, const char *pchSymbol, size_t cchSymbol,
75 const char *pszVersion, PKLDRADDR puValue, uint32_t *pfKind, void *pvUser)
76{
77 /** todo */
78 return -1;
79}
80
81
82/**
83 * Performs the tests on one module.
84 * @returns non sense.
85 */
86int TestModule(const char *pszFile)
87{
88 PKLDRMOD pMod;
89 KLDRSIZE cbImage;
90 void *pvBits;
91 int rc;
92
93 printf("tst-3-driver: testing '%s'...\n", pszFile);
94
95 /* open it. */
96 rc = kLdrModOpen(pszFile, &pMod);
97 if (rc)
98 return Failure("kLdrModOpen(%s,) -> %#d (%s)\n", pszFile, rc, kLdrErrStr(rc));
99
100 /* get bits. */
101 cbImage = kLdrModSize(pMod);
102 pvBits = malloc((size_t)cbImage);
103 if (pvBits)
104 {
105 KLDRADDR BaseAddress = (uintptr_t)pvBits;
106 rc = kLdrModGetBits(pMod, pvBits, BaseAddress, GetImport, NULL);
107 if (!rc)
108 {
109 const char *pszSym = MY_NAME("Tst3");
110 KLDRADDR EntryPoint;
111
112 /* call into it */
113 rc = kLdrModQuerySymbol(pMod, pvBits, BaseAddress, NIL_KLDRMOD_SYM_ORDINAL, pszSym, strlen(pszSym), NULL, NULL, NULL,
114 &EntryPoint, NULL);
115 if (!rc)
116 {
117 int (*pfnEntryPoint)(int) = (int (*)(int)) ((uintptr_t)EntryPoint);
118 rc = pfnEntryPoint(42);
119 if (rc == 42)
120 {
121 /* relocate twice and try again. */
122 rc = kLdrModRelocateBits(pMod, pvBits, BaseAddress + 0x22000, BaseAddress, GetImport, NULL);
123 if (!rc)
124 {
125 rc = kLdrModRelocateBits(pMod, pvBits, BaseAddress, BaseAddress + 0x22000, GetImport, NULL);
126 if (!rc)
127 {
128 rc = pfnEntryPoint(42);
129 if (rc == 42)
130 {
131 printf("tst-3-driver: success.\n");
132 }
133 else
134 Failure("pfnEntryPoint(42) -> %d (2nd)\n", rc);
135 }
136 else
137 Failure("kLdrModRelocateBits(,,, + 0x22000,,,) -> %#x (%s)\n", rc, kLdrErrStr(rc));
138 }
139 else
140 Failure("kLdrModRelocateBits(,, + 0x22000,,,,) -> %#x (%s)\n", rc, kLdrErrStr(rc));
141 }
142 else
143 Failure("pfnEntryPoint(42) -> %d (1st)\n", rc);
144 }
145 else
146 Failure("kLdrModQuerySymbol -> %#x (%s)\n", rc, kLdrErrStr(rc));
147 }
148 else
149 Failure("kLdrModGetBits -> %#x (%s)\n", rc, kLdrErrStr(rc));
150 free(pvBits);
151 }
152 else
153 Failure("malloc(%lx) -> NULL\n", (long)cbImage);
154
155 /* clean up */
156 rc = kLdrModClose(pMod);
157 if (rc)
158 Failure("kLdrModOpen(%s,) -> %#x (%s)\n", pszFile, rc, kLdrErrStr(rc));
159 return 0;
160}
161
162
163int main(int argc, char **argv)
164{
165 int i;
166
167 /*
168 * Test all the given modules (requires arguments).
169 */
170 for (i = 1; i < argc; i++)
171 {
172 TestModule(argv[i]);
173 }
174
175
176 /*
177 * Summary
178 */
179 if (!g_cErrors)
180 printf("tst-0-driver: SUCCESS\n");
181 else
182 printf("tst-0-driver: FAILURE - %d errors\n", g_cErrors);
183 return !!g_cErrors;
184}
Note: See TracBrowser for help on using the repository browser.