source: trunk/kStuff/kLdr/kLdrMod.c@ 3573

Last change on this file since 3573 was 3573, checked in by bird, 18 years ago

kHlp work...

  • Property svn:keywords set to Id
File size: 27.0 KB
Line 
1/* $Id: kLdrMod.c 3573 2007-08-31 04:09:23Z bird $ */
2/** @file
3 *
4 * kLdr - The Module Interpreter.
5 *
6 * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-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 <k/kLdr.h>
32#include "kLdrInternal.h"
33#include <k/kLdrFmts/mz.h>
34#if 1 /* testing headers */
35# include <k/kLdrFmts/pe.h>
36# include <k/kLdrFmts/lx.h>
37# include <k/kLdrFmts/elf32.h>
38# include <k/kLdrFmts/elf64.h>
39# include <k/kLdrFmts/mach-o.h>
40#endif
41
42
43/*******************************************************************************
44* Defined Constants And Macros *
45*******************************************************************************/
46/** @def KLDRMOD_STRICT
47 * Define KLDRMOD_STRICT to enabled strict checks in KLDRMOD. */
48#define KLDRMOD_STRICT 1
49
50/** @def KLDRMOD_ASSERT
51 * Assert that an expression is true when KLDR_STRICT is defined.
52 */
53#ifdef KLDRMOD_STRICT
54# define KLDRMOD_ASSERT(expr) kHlpAssert(expr)
55#else
56# define KLDRMOD_ASSERT(expr) do {} while (0)
57#endif
58
59/** Return / crash validation of a module argument. */
60#define KLDRMOD_VALIDATE_EX(pMod, rc) \
61 do { \
62 if ( (pMod)->u32Magic != KLDRMOD_MAGIC \
63 || (pMod)->pOps == NULL \
64 )\
65 { \
66 return (rc); \
67 } \
68 } while (0)
69
70/** Return / crash validation of a module argument. */
71#define KLDRMOD_VALIDATE(pMod) \
72 KLDRMOD_VALIDATE_EX(pMod, KLDR_ERR_INVALID_PARAMETER)
73
74/** Return / crash validation of a module argument. */
75#define KLDRMOD_VALIDATE_VOID(pMod) \
76 do { \
77 if ( (pMod)->u32Magic != KLDRMOD_MAGIC \
78 || (pMod)->pOps == NULL \
79 )\
80 { \
81 return; \
82 } \
83 } while (0)
84
85
86/*******************************************************************************
87* Global Variables *
88*******************************************************************************/
89/** The list of module interpreters. */
90static PCKLDRMODOPS g_pModInterpreterHead = NULL;
91
92
93
94/*******************************************************************************
95* Internal Functions *
96*******************************************************************************/
97
98
99
100/**
101 * Open a executable image by file name.
102 *
103 * @returns 0 on success and *ppMod pointing to a module instance.
104 * On failure, a non-zero OS specific error code is returned.
105 * @param pszFilename The filename to open.
106 * @param ppMod Where to store the module handle.
107 */
108int kLdrModOpen(const char *pszFilename, PPKLDRMOD ppMod)
109{
110 /*
111 * Open the file using a bit provider.
112 */
113 PKLDRRDR pRdr;
114 int rc = kLdrRdrOpen(&pRdr, pszFilename);
115 if (!rc)
116 {
117 rc = kLdrModOpenFromRdr(pRdr, ppMod);
118 if (!rc)
119 return 0;
120 kLdrRdrClose(pRdr);
121 }
122 return rc;
123}
124
125
126/**
127 * Open a executable image from a file provider instance.
128 *
129 * @returns 0 on success and *ppMod pointing to a module instance.
130 * On failure, a non-zero OS specific error code is returned.
131 * @param pRdr The file provider instance to use.
132 * On success, the ownership of the instance is taken by the
133 * module and the caller must not ever touch it again.
134 * (The instance is not closed on failure, the call has to do that.)
135 * @param ppMod Where to store the module handle.
136 */
137int kLdrModOpenFromRdr(PKLDRRDR pRdr, PPKLDRMOD ppMod)
138{
139 union
140 {
141 KU32 u32;
142 KU16 u16;
143 KU16 au16[2];
144 KU8 au8[4];
145 } u;
146 KLDRFOFF offHdr = 0;
147 int rc;
148
149 /*
150 * Try figure out what kind of image this is.
151 * Always read the 'new header' if we encounter MZ.
152 */
153 rc = kLdrRdrRead(pRdr, &u, sizeof(u), 0);
154 if (rc)
155 return rc;
156 if ( u.u16 == IMAGE_DOS_SIGNATURE
157 && kLdrRdrSize(pRdr) > sizeof(IMAGE_DOS_HEADER))
158 {
159 rc = kLdrRdrRead(pRdr, &u, sizeof(u.u32), K_OFFSETOF(IMAGE_DOS_HEADER, e_lfanew));
160 if (rc)
161 return rc;
162 if ((KLDRFOFF)u.u32 < kLdrRdrSize(pRdr))
163 {
164 offHdr = u.u32;
165 rc = kLdrRdrRead(pRdr, &u, sizeof(u.u32), offHdr);
166 if (rc)
167 return rc;
168 }
169 else
170 u.u16 = IMAGE_DOS_SIGNATURE;
171 }
172
173 /*
174 * Use the magic to select the appropriate image interpreter head on.
175 */
176 if (u.u16 == IMAGE_DOS_SIGNATURE)
177 rc = KLDR_ERR_MZ_NOT_SUPPORTED;
178 else if (u.u16 == IMAGE_NE_SIGNATURE)
179 rc = KLDR_ERR_NE_NOT_SUPPORTED;
180 else if (u.u16 == IMAGE_LX_SIGNATURE)
181 rc = g_kLdrModLXOps.pfnCreate(&g_kLdrModLXOps, pRdr, offHdr, ppMod);
182 else if (u.u16 == IMAGE_LE_SIGNATURE)
183 rc = KLDR_ERR_LE_NOT_SUPPORTED;
184 else if (u.u32 == IMAGE_NT_SIGNATURE)
185 rc = g_kLdrModPEOps.pfnCreate(&g_kLdrModPEOps, pRdr, offHdr, ppMod);
186 else if ( u.u32 == IMAGE_MACHO32_SIGNATURE
187 || u.u32 == IMAGE_MACHO32_SIGNATURE_OE
188 || u.u32 == IMAGE_MACHO64_SIGNATURE
189 || u.u32 == IMAGE_MACHO64_SIGNATURE_OE)
190 rc = g_kLdrModMachOOps.pfnCreate(&g_kLdrModMachOOps, pRdr, offHdr, ppMod);
191 else if (u.u32 == IMAGE_ELF_SIGNATURE)
192 rc = KLDR_ERR_ELF_NOT_SUPPORTED;
193 else if ( u.u32 == IMAGE_FAT_SIGNATURE
194 || u.u32 == IMAGE_FAT_SIGNATURE_OE)
195 rc = KLDR_ERR_FAT_NOT_SUPPORTED;
196 else
197 rc = KLDR_ERR_UNKNOWN_FORMAT;
198
199 /*
200 * If no head on hit, let each interpreter have a go.
201 */
202 if (rc)
203 {
204 PCKLDRMODOPS pOps;
205 for (pOps = g_pModInterpreterHead; pOps; pOps = pOps->pNext)
206 {
207 int rc2 = pOps->pfnCreate(pOps, pRdr, offHdr, ppMod);
208 if (!rc2)
209 return rc;
210 }
211 *ppMod = NULL;
212 }
213 return rc;
214}
215
216
217/**
218 * Closes an open module.
219 *
220 * The caller is responsible for calling kLdrModUnmap() and kLdrFreeTLS()
221 * before closing the module.
222 *
223 * @returns 0 on success, non-zero on failure. The module instance state
224 * is unknown on failure, it's best not to touch it.
225 * @param pMod The module.
226 */
227int kLdrModClose(PKLDRMOD pMod)
228{
229 KLDRMOD_VALIDATE(pMod);
230 return pMod->pOps->pfnDestroy(pMod);
231}
232
233
234/**
235 * Queries a symbol by name or ordinal number.
236 *
237 * @returns 0 and *puValue and *pfKind on success.
238 * KLDR_ERR_SYMBOL_NOT_FOUND is returned if the symbol wasn't found.
239 * Other failures could stem from bad executable format failures,
240 * read failure in case pvBits isn't specified and no mapping should be used.
241 * @param pMod The module.
242 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
243 * This can be used by some module interpreters to reduce memory consumption.
244 * @param BaseAddress The module base address to use when calculating the symbol value.
245 * There are two special values that can be used:
246 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
247 * @param iSymbol The symbol ordinal. (optional)
248 * @param pchSymbol The symbol name. (optional)
249 * Important, this doesn't have to be a null-terminated string.
250 * @param cchSymbol The length of the symbol name.
251 * @param pszVersion The symbol version. NULL if not versioned.
252 * @param pfnGetForwarder The callback to use when resolving a forwarder symbol. This is optional
253 * and if not specified KLDR_ERR_FORWARDER is returned instead.
254 * @param pvUser The user argument for the pfnGetForwarder callback.
255 * @param puValue Where to store the symbol value. (optional)
256 * @param pfKind On input one of the KLDRSYMKIND_REQ_* #defines.
257 * On output the symbol kind. (optional)
258 */
259int kLdrModQuerySymbol(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 iSymbol,
260 const char *pchSymbol, KSIZE cchSymbol, const char *pszVersion,
261 PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind)
262{
263 KLDRMOD_VALIDATE(pMod);
264 if (!puValue && !pfKind)
265 return KLDR_ERR_INVALID_PARAMETER;
266 if (puValue)
267 *puValue = 0;
268 if (pfKind)
269 K_VALIDATE_FLAGS(*pfKind, KLDRSYMKIND_REQ_SEGMENTED);
270 return pMod->pOps->pfnQuerySymbol(pMod, pvBits, BaseAddress, iSymbol, pchSymbol, cchSymbol, pszVersion,
271 pfnGetForwarder, pvUser, puValue, pfKind);
272}
273
274
275/**
276 * Enumerate the symbols in the module.
277 *
278 * @returns 0 on success and non-zero a status code on failure.
279 * @param pMod The module which symbols should be enumerated.
280 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
281 * This can be used by some module interpreters to reduce memory consumption.
282 * @param BaseAddress The module base address to use when calculating the symbol values.
283 * There are two special values that could be can:
284 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
285 * @param fFlags The enumeration flags. A combination of the KLDRMOD_ENUM_SYMS_FLAGS_* \#defines.
286 * @param pfnCallback The enumeration callback function.
287 * @param pvUser The user argument to the callback function.
288 */
289int kLdrModEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 fFlags,
290 PFNKLDRMODENUMSYMS pfnCallback, void *pvUser)
291{
292 KLDRMOD_VALIDATE(pMod);
293 K_VALIDATE_FLAGS(fFlags, KLDRMOD_ENUM_SYMS_FLAGS_ALL);
294 return pMod->pOps->pfnEnumSymbols(pMod, pvBits, BaseAddress, fFlags, pfnCallback, pvUser);
295}
296
297
298/**
299 * Get the name of an import module by ordinal number.
300 *
301 * @returns 0 and name in pszName on success.
302 * On buffer overruns KLDR_ERR_BUFFER_OVERFLOW will be returned.
303 * On other failures and appropriate error code is returned.
304 * @param pMod The module.
305 * @param pvBits Optional pointer to bits returned by kLdrModGetBits().
306 * This can be used by some module interpreters to reduce memory consumption.
307 * @param iImport The import module ordinal number.
308 * @param pszName Where to store the name.
309 * @param cchName The size of the name buffer.
310 */
311int kLdrModGetImport(PKLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZE cchName)
312{
313 KLDRMOD_VALIDATE(pMod);
314 return pMod->pOps->pfnGetImport(pMod, pvBits, iImport, pszName, cchName);
315}
316
317
318/**
319 * Get the number of import modules.
320 *
321 * @returns The number of import modules. -1 if something really bad happens.
322 * @param pMod The module.
323 * @param pvBits Optional pointer to bits returned by kLdrModGetBits().
324 * This can be used by some module interpreters to reduce memory consumption.
325 */
326KI32 kLdrModNumberOfImports(PKLDRMOD pMod, const void *pvBits)
327{
328 KLDRMOD_VALIDATE(pMod);
329 return pMod->pOps->pfnNumberOfImports(pMod, pvBits);
330}
331
332
333/**
334 * Checks if this module can be executed by the specified arch+cpu.
335 *
336 * @returns 0 if it can, KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE if it can't.
337 * Other failures may occur and cause other return values.
338 * @param pMod The module.
339 * @param pvBits Optional pointer to bits returned by kLdrModGetBits().
340 * This can be used by some module interpreters to reduce memory consumption.
341 */
342int kLdrModCanExecuteOn(PKLDRMOD pMod, const void *pvBits, KLDRARCH enmArch, KLDRCPU enmCpu)
343{
344 KLDRMOD_VALIDATE(pMod);
345 if (pMod->pOps->pfnCanExecuteOn)
346 return pMod->pOps->pfnCanExecuteOn(pMod, pvBits, enmArch, enmCpu);
347 return kLdrCompareCpus(pMod->enmArch, pMod->enmCpu, enmArch, enmCpu);
348}
349
350
351/**
352 * Gets the image stack info.
353 *
354 * @returns 0 on success, non-zero on failure.
355 * @param pMod
356 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
357 * This can be used by some module interpreters to reduce memory consumption.
358 * @param BaseAddress The module base address to use when calculating the stack address.
359 * There are two special values that can be used:
360 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
361 * @param pStackInfo The stack information.
362 */
363int kLdrModGetStackInfo(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo)
364{
365 KLDRMOD_VALIDATE(pMod);
366 return pMod->pOps->pfnGetStackInfo(pMod, pvBits, BaseAddress, pStackInfo);
367}
368
369
370/**
371 * Queries the main entrypoint of the module.
372 *
373 * Only executable are supposed to have an main entrypoint, though some object and DLL
374 * formats will also allow this.
375 *
376 * @returns 0 and *pMainEPAddress on success. Non-zero status code on failure.
377 * @param pMod The module.
378 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
379 * This can be used by some module interpreters to reduce memory consumption.
380 * @param BaseAddress The module base address to use when calculating the entrypoint address.
381 * There are two special values that can be used:
382 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
383 * @param pMainEPAddress Where to store the entry point address.
384 */
385int kLdrModQueryMainEntrypoint(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress)
386{
387 KLDRMOD_VALIDATE(pMod);
388 *pMainEPAddress = 0;
389 return pMod->pOps->pfnQueryMainEntrypoint(pMod, pvBits, BaseAddress, pMainEPAddress);
390}
391
392
393/**
394 * Queries info about a resource.
395 *
396 * If there are multiple resources matching the criteria, the best or
397 * first match will be return.
398 *
399 *
400 * @returns 0 on success.
401 * @returns Whatever non-zero status returned by pfnCallback (enumeration was stopped).
402 * @returns non-zero kLdr or native status code on failure.
403 *
404 * @param pMod The module.
405 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
406 * This can be used by some module interpreters to reduce memory consumption.
407 * @param BaseAddress The module base address to use when calculating the resource addresses.
408 * There are two special values that can be used:
409 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
410 * @param idType The resource type id to match if not NIL_KLDRMOD_RSRC_TYPE_ID.
411 * @param pszType The resource type name to match if no NULL.
412 * @param idName The resource name id to match if not NIL_KLDRMOD_RSRC_NAME_ID.
413 * @param pszName The resource name to match if not NULL.
414 * @param idLang The language id to match.
415 * @param pfnCallback The callback function.
416 * @param pvUser The user argument for the callback.
417 */
418int kLdrModQueryResource(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
419 KU32 idName, const char *pszName, KU32 idLang, PKLDRADDR pAddrRsrc, KSIZE *pcbRsrc)
420{
421 KLDRMOD_VALIDATE(pMod);
422 if (!pAddrRsrc && !pcbRsrc)
423 return KLDR_ERR_INVALID_PARAMETER;
424 if (pAddrRsrc)
425 *pAddrRsrc = NIL_KLDRADDR;
426 if (pcbRsrc)
427 *pcbRsrc = 0;
428 return pMod->pOps->pfnQueryResource(pMod, pvBits, BaseAddress, idType, pszType, idName, pszName, idLang, pAddrRsrc, pcbRsrc);
429}
430
431
432/**
433 * Enumerates the resources matching the specfied criteria.
434 *
435 *
436 * @returns 0 on success.
437 * @returns Whatever non-zero status returned by pfnCallback (enumeration was stopped).
438 * @returns non-zero kLdr or native status code on failure.
439 *
440 * @param pMod The module.
441 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
442 * This can be used by some module interpreters to reduce memory consumption.
443 * @param BaseAddress The module base address to use when calculating the resource addresses.
444 * There are two special values that can be used:
445 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
446 * @param idType The resource type id to match if not NIL_KLDRMOD_RSRC_TYPE_ID.
447 * @param pszType The resource type name to match if no NULL.
448 * @param idName The resource name id to match if not NIL_KLDRMOD_RSRC_NAME_ID.
449 * @param pszName The resource name to match if not NULL.
450 * @param idLang The language id to match.
451 * @param pfnCallback The callback function.
452 * @param pvUser The user argument for the callback.
453 */
454int kLdrModEnumResources(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
455 KU32 idName, const char *pszName, KU32 idLang, PFNKLDRENUMRSRC pfnCallback, void *pvUser)
456{
457 KLDRMOD_VALIDATE(pMod);
458 return pMod->pOps->pfnEnumResources(pMod, pvBits, BaseAddress, idType, pszType, idName, pszName, idLang, pfnCallback, pvUser);
459}
460
461
462/**
463 * Enumerate the debug info formats contained in the executable image.
464 *
465 * @returns 0 on success, non-zero OS or kLdr status code on failure, or non-zero callback status.
466 * @param pMod The module.
467 * @param pvBits Optional pointer to bits returned by kLdrModGetBits().
468 * This can be used by some module interpreters to reduce memory consumption.
469 * @param pfnCallback The callback function.
470 * @param pvUser The user argument.
471 * @see pg_kDbg for the debug info reader.
472 */
473int kLdrModEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser)
474{
475 KLDRMOD_VALIDATE(pMod);
476 return pMod->pOps->pfnEnumDbgInfo(pMod, pvBits, pfnCallback, pvUser);
477}
478
479
480/**
481 * Checks if the module has debug info embedded or otherwise associated with it.
482 *
483 * @returns 0 if it has debug info, KLDR_ERR_NO_DEBUG_INFO if no debug info,
484 * and non-zero OS or kLdr status code on failure.
485 * @param pMod The module.
486 * @param pvBits Optional pointer to bits returned by kLdrModGetBits().
487 * This can be used by some module interpreters to reduce memory consumption.
488 */
489int kLdrModHasDbgInfo(PKLDRMOD pMod, const void *pvBits)
490{
491 KLDRMOD_VALIDATE(pMod);
492 return pMod->pOps->pfnHasDbgInfo(pMod, pvBits);
493}
494
495
496/**
497 * May free up some resources held by the module.
498 *
499 * @todo define exactly what it possible to do after this call.
500 *
501 * @returns 0 on success, KLDR_ERR_* on failure.
502 * @param pMod The module.
503 */
504int kLdrModMostlyDone(PKLDRMOD pMod)
505{
506 KLDRMOD_VALIDATE(pMod);
507 return pMod->pOps->pfnMostlyDone(pMod);
508}
509
510
511/**
512 * Maps the module into the memory of the caller.
513 *
514 * On success the actual addresses for the segments can be found in MapAddress
515 * member of each segment in the segment array.
516 *
517 * @returns 0 on success, non-zero OS or kLdr status code on failure.
518 * @param pMod The module to be mapped.
519 * @remark kLdr only supports one mapping at a time of a module.
520 */
521int kLdrModMap(PKLDRMOD pMod)
522{
523 KLDRMOD_VALIDATE(pMod);
524 return pMod->pOps->pfnMap(pMod);
525}
526
527
528/**
529 * Unmaps a module previously mapped by kLdrModMap().
530 *
531 * @returns 0 on success, non-zero OS or kLdr status code on failure.
532 * @param pMod The module to unmap.
533 */
534int kLdrModUnmap(PKLDRMOD pMod)
535{
536 KLDRMOD_VALIDATE(pMod);
537 return pMod->pOps->pfnUnmap(pMod);
538}
539
540
541/**
542 * Allocates Thread Local Storage for module mapped by kLdrModMap().
543 *
544 * Calling kLdrModAllocTLS() more than once without calling kLdrModFreeTLS()
545 * between each invocation is not supported.
546 *
547 * @returns 0 on success, non-zero OS or kLdr status code on failure.
548 * @param pMod The module.
549 */
550int kLdrModAllocTLS(PKLDRMOD pMod)
551{
552 KLDRMOD_VALIDATE(pMod);
553 return pMod->pOps->pfnAllocTLS(pMod);
554}
555
556
557/**
558 * Frees Thread Local Storage previously allocated by kLdrModAllocTLS().
559 *
560 * The caller is responsible for only calling kLdrModFreeTLS() once
561 * after calling kLdrModAllocTLS().
562 *
563 * @returns 0 on success, non-zero OS or kLdr status code on failure.
564 * @param pMod The module.
565 */
566void kLdrModFreeTLS(PKLDRMOD pMod)
567{
568 KLDRMOD_VALIDATE_VOID(pMod);
569 pMod->pOps->pfnFreeTLS(pMod);
570}
571
572
573/**
574 * Reloads all dirty pages in a module previously mapped by kLdrModMap().
575 *
576 * The module interpreter may omit code pages if it can safely apply code
577 * fixups again in a subsequent kLdrModFixupMapping() call.
578 *
579 * The caller is responsible for freeing TLS before calling this function.
580 *
581 * @returns 0 on success, non-zero OS or kLdr status code on failure.
582 * @param pMod The module.
583 */
584int kLdrModReload(PKLDRMOD pMod)
585{
586 KLDRMOD_VALIDATE(pMod);
587 return pMod->pOps->pfnReload(pMod);
588}
589
590
591/**
592 * Fixup the mapping made by kLdrModMap().
593 *
594 * The caller is only responsible for not calling this function more than
595 * once without doing kLDrModReload() inbetween.
596 *
597 * @returns 0 on success, non-zero OS or kLdr status code on failure.
598 * @param pMod The module.
599 * @param pfnGetImport The callback for resolving external (imported) symbols.
600 * @param pvUser The callback user argument.
601 */
602int kLdrModFixupMapping(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
603{
604 KLDRMOD_VALIDATE(pMod);
605 return pMod->pOps->pfnFixupMapping(pMod, pfnGetImport, pvUser);
606}
607
608
609/**
610 * Call the module initializiation function of a mapped module (if any).
611 *
612 * @returns 0 on success or no init function, non-zero on init function failure or invalid pMod.
613 * @param pMod The module.
614 * @param uHandle The module handle to use if any of the init functions requires the module handle.
615 */
616int kLdrModCallInit(PKLDRMOD pMod, KUPTR uHandle)
617{
618 KLDRMOD_VALIDATE(pMod);
619 return pMod->pOps->pfnCallInit(pMod, uHandle);
620}
621
622
623/**
624 * Call the module termination function of a mapped module (if any).
625 *
626 * @returns 0 on success or no term function, non-zero on invalid pMod.
627 * @param pMod The module.
628 * @param uHandle The module handle to use if any of the term functions requires the module handle.
629 *
630 * @remark Termination function failure will be ignored by the module interpreter.
631 */
632int kLdrModCallTerm(PKLDRMOD pMod, KUPTR uHandle)
633{
634 KLDRMOD_VALIDATE(pMod);
635 return pMod->pOps->pfnCallTerm(pMod, uHandle);
636}
637
638
639/**
640 * Call the thread attach or detach function of a mapped module (if any).
641 *
642 * Any per-thread TLS initialization/termination will have to be done at this time too.
643 *
644 * @returns 0 on success or no attach/detach function, non-zero on attach failure or invalid pMod.
645 * @param pMod The module.
646 * @param uHandle The module handle to use if any of the thread attach/detach functions
647 * requires the module handle.
648 *
649 * @remark Detach function failure will be ignored by the module interpreter.
650 */
651int kLdrModCallThread(PKLDRMOD pMod, KUPTR uHandle, unsigned fAttachingOrDetaching)
652{
653 KLDRMOD_VALIDATE(pMod);
654 K_VALIDATE_FLAGS(fAttachingOrDetaching, 1);
655 return pMod->pOps->pfnCallThread(pMod, uHandle, fAttachingOrDetaching);
656}
657
658
659/**
660 * Get the size of the mapped module.
661 *
662 * @returns The size of the mapped module (in bytes).
663 * @param pMod The module.
664 */
665KLDRADDR kLdrModSize(PKLDRMOD pMod)
666{
667 KLDRMOD_VALIDATE_EX(pMod, 0);
668 return pMod->pOps->pfnSize(pMod);
669}
670
671
672/**
673 * Gets the module bits.
674 *
675 * The module interpreter will fill a mapping allocated by the caller with the
676 * module bits reallocated to the specified address.
677 *
678 * @returns 0 on succes, non-zero OS or kLdr status code on failure.
679 * @param pMod The module.
680 * @param pvBits Where to put the bits.
681 * @param BaseAddress The base address that should correspond to the first byte in pvBits
682 * upon return.
683 * @param pfnGetImport The callback ufor resolving external (imported) symbols.
684 * @param pvUser The callback user argument.
685 */
686int kLdrModGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
687{
688 KLDRMOD_VALIDATE(pMod);
689 return pMod->pOps->pfnGetBits(pMod, pvBits, BaseAddress, pfnGetImport, pvUser);
690}
691
692
693/**
694 * Relocates the module bits previously obtained by kLdrModGetBits().
695 *
696 * @returns 0 on succes, non-zero OS or kLdr status code on failure.
697 * @param pMod The module.
698 * @param pvBits Where to put the bits.
699 * @param NewBaseAddress The new base address.
700 * @param OldBaseAddress The old base address (i.e. the one specified to kLdrModGetBits() or as
701 * NewBaseAddressto the previous kLdrModRelocateBits() call).
702 * @param pfnGetImport The callback ufor resolving external (imported) symbols.
703 * @param pvUser The callback user argument.
704 */
705int kLdrModRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
706 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
707{
708 KLDRMOD_VALIDATE(pMod);
709 return pMod->pOps->pfnRelocateBits(pMod, pvBits, NewBaseAddress, OldBaseAddress, pfnGetImport, pvUser);
710}
711
Note: See TracBrowser for help on using the repository browser.