1 | /* $Id: tstLdr.cpp 62571 2016-07-26 15:58:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Testcase for parts of RTLdr*.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/ldr.h>
|
---|
32 | #include <iprt/alloc.h>
|
---|
33 | #include <iprt/stream.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/initterm.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Global Variables *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | /** If set, don't bitch when failing to resolve symbols. */
|
---|
44 | static bool g_fDontBitchOnResolveFailure = false;
|
---|
45 | /** Whether it's kernel model code or not.. */
|
---|
46 | static bool g_fKernel = true;
|
---|
47 | /** Module architecture bit count. */
|
---|
48 | static uint32_t g_cBits = HC_ARCH_BITS;
|
---|
49 |
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Resolve an external symbol during RTLdrGetBits().
|
---|
53 | *
|
---|
54 | * @returns iprt status code.
|
---|
55 | * @param hLdrMod The loader module handle.
|
---|
56 | * @param pszModule Module name.
|
---|
57 | * @param pszSymbol Symbol name, NULL if uSymbol should be used.
|
---|
58 | * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
|
---|
59 | * @param pValue Where to store the symbol value (address).
|
---|
60 | * @param pvUser User argument.
|
---|
61 | */
|
---|
62 | static DECLCALLBACK(int) testGetImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol,
|
---|
63 | RTUINTPTR *pValue, void *pvUser)
|
---|
64 | {
|
---|
65 | /* check the name format and only permit certain names... later, right? */
|
---|
66 | RT_NOREF_PV(hLdrMod); RT_NOREF_PV(pszModule); RT_NOREF_PV(pszSymbol); RT_NOREF_PV(uSymbol); RT_NOREF_PV(pvUser);
|
---|
67 |
|
---|
68 | if (g_cBits == 32)
|
---|
69 | *pValue = 0xabcdef0f;
|
---|
70 | else
|
---|
71 | {
|
---|
72 | RTUINTPTR BaseAddr = *(PCRTUINTPTR)pvUser;
|
---|
73 | if (g_fKernel)
|
---|
74 | *pValue = BaseAddr & RT_BIT(31) ? -(int32_t)0x76634935 : 0x7f304938;
|
---|
75 | else
|
---|
76 | *pValue = (int32_t)0x76634935 * ((BaseAddr >> 8) & 7);
|
---|
77 | }
|
---|
78 | return VINF_SUCCESS;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * One test iteration with one file.
|
---|
84 | *
|
---|
85 | * The test is very simple, we load the file three times
|
---|
86 | * into two different regions. The first two into each of the
|
---|
87 | * regions the for compare usage. The third is loaded into one
|
---|
88 | * and then relocated between the two and other locations a few times.
|
---|
89 | *
|
---|
90 | * @returns number of errors.
|
---|
91 | * @param pszFilename The file to load the mess with.
|
---|
92 | */
|
---|
93 | static int testLdrOne(const char *pszFilename)
|
---|
94 | {
|
---|
95 | int rcRet = 0;
|
---|
96 | size_t cbImage = 0;
|
---|
97 | struct Load
|
---|
98 | {
|
---|
99 | RTLDRMOD hLdrMod;
|
---|
100 | void *pvBits;
|
---|
101 | RTUINTPTR Addr;
|
---|
102 | const char *pszName;
|
---|
103 | } aLoads[6] =
|
---|
104 | {
|
---|
105 | { NULL, NULL, (RTUINTPTR)0xefefef00, "foo" },
|
---|
106 | { NULL, NULL, (RTUINTPTR)0x40404040, "bar" },
|
---|
107 | { NULL, NULL, (RTUINTPTR)0xefefef00, "foobar" },
|
---|
108 | { NULL, NULL, (RTUINTPTR)0xefefef00, "kLdr-foo" },
|
---|
109 | { NULL, NULL, (RTUINTPTR)0x40404040, "kLdr-bar" },
|
---|
110 | { NULL, NULL, (RTUINTPTR)0xefefef00, "kLdr-foobar" }
|
---|
111 | };
|
---|
112 | unsigned i;
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Load them.
|
---|
116 | */
|
---|
117 | for (i = 0; i < RT_ELEMENTS(aLoads); i++)
|
---|
118 | {
|
---|
119 | /* adjust load address and announce our intentions */
|
---|
120 | if (g_cBits == 32)
|
---|
121 | aLoads[i].Addr &= UINT32_C(0xffffffff);
|
---|
122 | RTPrintf("tstLdr: Loading image at %RTptr\n", aLoads[i].Addr);
|
---|
123 |
|
---|
124 | /* open it */
|
---|
125 | int rc;
|
---|
126 | if (!strncmp(aLoads[i].pszName, RT_STR_TUPLE("kLdr-")))
|
---|
127 | rc = RTLdrOpenkLdr(pszFilename, 0, RTLDRARCH_WHATEVER, &aLoads[i].hLdrMod);
|
---|
128 | else
|
---|
129 | rc = RTLdrOpen(pszFilename, 0, RTLDRARCH_WHATEVER, &aLoads[i].hLdrMod);
|
---|
130 | if (RT_FAILURE(rc))
|
---|
131 | {
|
---|
132 | RTPrintf("tstLdr: Failed to open '%s'/%d, rc=%Rrc. aborting test.\n", pszFilename, i, rc);
|
---|
133 | Assert(aLoads[i].hLdrMod == NIL_RTLDRMOD);
|
---|
134 | rcRet++;
|
---|
135 | break;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /* size it */
|
---|
139 | size_t cb = RTLdrSize(aLoads[i].hLdrMod);
|
---|
140 | if (cbImage && cb != cbImage)
|
---|
141 | {
|
---|
142 | RTPrintf("tstLdr: Size mismatch '%s'/%d. aborting test.\n", pszFilename, i);
|
---|
143 | rcRet++;
|
---|
144 | break;
|
---|
145 | }
|
---|
146 | cbImage = cb;
|
---|
147 |
|
---|
148 | /* Allocate bits. */
|
---|
149 | aLoads[i].pvBits = RTMemAlloc(cb);
|
---|
150 | if (!aLoads[i].pvBits)
|
---|
151 | {
|
---|
152 | RTPrintf("tstLdr: Out of memory '%s'/%d cbImage=%d. aborting test.\n", pszFilename, i, cbImage);
|
---|
153 | rcRet++;
|
---|
154 | break;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /* Get the bits. */
|
---|
158 | rc = RTLdrGetBits(aLoads[i].hLdrMod, aLoads[i].pvBits, aLoads[i].Addr, testGetImport, &aLoads[i].Addr);
|
---|
159 | if (RT_FAILURE(rc))
|
---|
160 | {
|
---|
161 | RTPrintf("tstLdr: Failed to get bits for '%s'/%d, rc=%Rrc. aborting test\n", pszFilename, i, rc);
|
---|
162 | rcRet++;
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Continue with the relocations and symbol resolving.
|
---|
169 | */
|
---|
170 | if (!rcRet)
|
---|
171 | {
|
---|
172 | static RTUINTPTR aRels[] =
|
---|
173 | {
|
---|
174 | (RTUINTPTR)0xefefef00, /* same. */
|
---|
175 | (RTUINTPTR)0x40404040, /* the other. */
|
---|
176 | (RTUINTPTR)0xefefef00, /* back. */
|
---|
177 | (RTUINTPTR)0x40404040, /* the other. */
|
---|
178 | (RTUINTPTR)0xefefef00, /* back again. */
|
---|
179 | (RTUINTPTR)0x77773420, /* somewhere entirely else. */
|
---|
180 | (RTUINTPTR)0xf0000000, /* somewhere entirely else. */
|
---|
181 | (RTUINTPTR)0x40404040, /* the other. */
|
---|
182 | (RTUINTPTR)0xefefef00 /* back again. */
|
---|
183 | };
|
---|
184 | struct Symbols
|
---|
185 | {
|
---|
186 | /** The symbol offset. -1 indicates the first time. */
|
---|
187 | unsigned off;
|
---|
188 | /** The symbol name. */
|
---|
189 | const char *pszName;
|
---|
190 | } aSyms[] =
|
---|
191 | {
|
---|
192 | { ~0U, "Entrypoint" },
|
---|
193 | { ~0U, "SomeExportFunction1" },
|
---|
194 | { ~0U, "SomeExportFunction2" },
|
---|
195 | { ~0U, "SomeExportFunction3" },
|
---|
196 | { ~0U, "SomeExportFunction4" },
|
---|
197 | { ~0U, "SomeExportFunction5" },
|
---|
198 | { ~0U, "SomeExportFunction5" },
|
---|
199 | { ~0U, "DISCoreOne" }
|
---|
200 | };
|
---|
201 |
|
---|
202 | unsigned iRel = 0;
|
---|
203 | for (;;)
|
---|
204 | {
|
---|
205 | /* Compare all which are at the same address. */
|
---|
206 | for (i = 0; i < RT_ELEMENTS(aLoads) - 1; i++)
|
---|
207 | {
|
---|
208 | for (unsigned j = i + 1; j < RT_ELEMENTS(aLoads); j++)
|
---|
209 | {
|
---|
210 | if (aLoads[j].Addr == aLoads[i].Addr)
|
---|
211 | {
|
---|
212 | if (memcmp(aLoads[j].pvBits, aLoads[i].pvBits, cbImage))
|
---|
213 | {
|
---|
214 | RTPrintf("tstLdr: Mismatch between load %d and %d. ('%s')\n", j, i, pszFilename);
|
---|
215 | #if 1
|
---|
216 | const uint8_t *pu8J = (const uint8_t *)aLoads[j].pvBits;
|
---|
217 | const uint8_t *pu8I = (const uint8_t *)aLoads[i].pvBits;
|
---|
218 | for (uint32_t off = 0; off < cbImage; off++, pu8J++, pu8I++)
|
---|
219 | if (*pu8J != *pu8I)
|
---|
220 | RTPrintf(" %08x %02x != %02x\n", off, *pu8J, *pu8I);
|
---|
221 | #else
|
---|
222 | const uint32_t *pu32J = (const uint32_t *)aLoads[j].pvBits;
|
---|
223 | const uint32_t *pu32I = (const uint32_t *)aLoads[i].pvBits;
|
---|
224 | for (uint32_t off = 0; off < cbImage; off += 4, pu32J++, pu32I++)
|
---|
225 | if (*pu32J != *pu32I)
|
---|
226 | RTPrintf(" %08x %08x != %08x\n", off, *pu32J, *pu32I);
|
---|
227 | #endif
|
---|
228 | rcRet++;
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | /* compare symbols. */
|
---|
236 | for (i = 0; i < RT_ELEMENTS(aLoads); i++)
|
---|
237 | {
|
---|
238 | for (unsigned iSym = 0; iSym < RT_ELEMENTS(aSyms); iSym++)
|
---|
239 | {
|
---|
240 | RTUINTPTR Value;
|
---|
241 | int rc = RTLdrGetSymbolEx(aLoads[i].hLdrMod, aLoads[i].pvBits, aLoads[i].Addr,
|
---|
242 | UINT32_MAX, aSyms[iSym].pszName, &Value);
|
---|
243 | if (RT_SUCCESS(rc))
|
---|
244 | {
|
---|
245 | unsigned off = Value - aLoads[i].Addr;
|
---|
246 | if (off < cbImage)
|
---|
247 | {
|
---|
248 | if (aSyms[iSym].off == ~0U)
|
---|
249 | aSyms[iSym].off = off;
|
---|
250 | else if (off != aSyms[iSym].off)
|
---|
251 | {
|
---|
252 | RTPrintf("tstLdr: Mismatching symbol '%s' in '%s'/%d. expected off=%d got %d\n",
|
---|
253 | aSyms[iSym].pszName, pszFilename, i, aSyms[iSym].off, off);
|
---|
254 | rcRet++;
|
---|
255 | }
|
---|
256 | }
|
---|
257 | else
|
---|
258 | {
|
---|
259 | RTPrintf("tstLdr: Invalid value for symbol '%s' in '%s'/%d. off=%#x Value=%#x\n",
|
---|
260 | aSyms[iSym].pszName, pszFilename, i, off, Value);
|
---|
261 | rcRet++;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | else if (!g_fDontBitchOnResolveFailure)
|
---|
265 | {
|
---|
266 | RTPrintf("tstLdr: Failed to resolve symbol '%s' in '%s'/%d.\n", aSyms[iSym].pszName, pszFilename, i);
|
---|
267 | rcRet++;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | if (iRel >= RT_ELEMENTS(aRels))
|
---|
273 | break;
|
---|
274 |
|
---|
275 | /* adjust load address and announce our intentions */
|
---|
276 | if (g_cBits == 32)
|
---|
277 | aRels[iRel] &= UINT32_C(0xffffffff);
|
---|
278 |
|
---|
279 | /* relocate it stuff. */
|
---|
280 | RTPrintf("tstLdr: Relocating image 2 from %RTptr to %RTptr\n", aLoads[2].Addr, aRels[iRel]);
|
---|
281 | int rc = RTLdrRelocate(aLoads[2].hLdrMod, aLoads[2].pvBits, aRels[iRel], aLoads[2].Addr, testGetImport, &aRels[iRel]);
|
---|
282 | if (RT_FAILURE(rc))
|
---|
283 | {
|
---|
284 | RTPrintf("tstLdr: Relocate of '%s' from %#x to %#x failed, rc=%Rrc. Aborting test.\n",
|
---|
285 | pszFilename, aRels[iRel], aLoads[2].Addr, rc);
|
---|
286 | rcRet++;
|
---|
287 | break;
|
---|
288 | }
|
---|
289 | aLoads[2].Addr = aRels[iRel];
|
---|
290 |
|
---|
291 | /* next */
|
---|
292 | iRel++;
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * Clean up.
|
---|
298 | */
|
---|
299 | for (i = 0; i < RT_ELEMENTS(aLoads); i++)
|
---|
300 | {
|
---|
301 | if (aLoads[i].pvBits)
|
---|
302 | RTMemFree(aLoads[i].pvBits);
|
---|
303 | if (aLoads[i].hLdrMod)
|
---|
304 | {
|
---|
305 | int rc = RTLdrClose(aLoads[i].hLdrMod);
|
---|
306 | if (RT_FAILURE(rc))
|
---|
307 | {
|
---|
308 | RTPrintf("tstLdr: Failed to close '%s' i=%d, rc=%Rrc.\n", pszFilename, i, rc);
|
---|
309 | rcRet++;
|
---|
310 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | return rcRet;
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 |
|
---|
319 | int main(int argc, char **argv)
|
---|
320 | {
|
---|
321 | RTR3InitExe(argc, &argv, 0);
|
---|
322 |
|
---|
323 | int rcRet = 0;
|
---|
324 | if (argc <= 1)
|
---|
325 | {
|
---|
326 | RTPrintf("usage: %s [-32|-64] [-kernel] <module> [more options/modules]\n", argv[0]);
|
---|
327 | return 1;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /*
|
---|
331 | * Iterate the files.
|
---|
332 | */
|
---|
333 | for (int argi = 1; argi < argc; argi++)
|
---|
334 | {
|
---|
335 | if (!strcmp(argv[argi], "-n"))
|
---|
336 | g_fDontBitchOnResolveFailure = true;
|
---|
337 | else if (!strcmp(argv[argi], "-32"))
|
---|
338 | g_cBits = 32;
|
---|
339 | else if (!strcmp(argv[argi], "-64"))
|
---|
340 | g_cBits = 64;
|
---|
341 | else if (!strcmp(argv[argi], "-kernel"))
|
---|
342 | g_fKernel = true;
|
---|
343 | else
|
---|
344 | {
|
---|
345 | RTPrintf("tstLdr: TESTING '%s'...\n", argv[argi]);
|
---|
346 | rcRet += testLdrOne(argv[argi]);
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | /*
|
---|
351 | * Test result summary.
|
---|
352 | */
|
---|
353 | if (!rcRet)
|
---|
354 | RTPrintf("tstLdr: SUCCESS\n");
|
---|
355 | else
|
---|
356 | RTPrintf("tstLdr: FAILURE - %d errors\n", rcRet);
|
---|
357 | return !!rcRet;
|
---|
358 | }
|
---|