source: trunk/kLdr/kLdrMod.c@ 26

Last change on this file since 26 was 26, checked in by bird, 17 years ago

FAT Mach-O bugfix.

  • Property svn:keywords set to Id Revision
File size: 33.3 KB
Line 
1/* $Id: kLdrMod.c 26 2009-02-19 01:06:09Z bird $ */
2/** @file
3 * kLdr - The Module Interpreter.
4 */
5
6/*
7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
8 *
9 * This file is part of kStuff.
10 *
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.
15 *
16 * In addition to the permissions in the GNU Lesser General Public
17 * License, you are granted unlimited permission to link the compiled
18 * version of this file into combinations with other programs, and to
19 * distribute those combinations without any restriction coming from
20 * the use of this file.
21 *
22 * kStuff is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with kStuff; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 * 02110-1301, USA
31 */
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#include <k/kLdr.h>
37#include "kLdrInternal.h"
38#include <k/kCpu.h>
39#include <k/kLdrFmts/mz.h>
40#if 1 /* testing headers */
41# include <k/kLdrFmts/pe.h>
42# include <k/kLdrFmts/lx.h>
43# include <k/kLdrFmts/elf32.h>
44# include <k/kLdrFmts/elf64.h>
45# include <k/kLdrFmts/mach-o.h>
46#endif
47
48
49/*******************************************************************************
50* Defined Constants And Macros *
51*******************************************************************************/
52/** @def KLDRMOD_STRICT
53 * Define KLDRMOD_STRICT to enabled strict checks in KLDRMOD. */
54#define KLDRMOD_STRICT 1
55
56/** @def KLDRMOD_ASSERT
57 * Assert that an expression is true when KLDR_STRICT is defined.
58 */
59#ifdef KLDRMOD_STRICT
60# define KLDRMOD_ASSERT(expr) kHlpAssert(expr)
61#else
62# define KLDRMOD_ASSERT(expr) do {} while (0)
63#endif
64
65/** Return / crash validation of a module argument. */
66#define KLDRMOD_VALIDATE_EX(pMod, rc) \
67 do { \
68 if ( (pMod)->u32Magic != KLDRMOD_MAGIC \
69 || (pMod)->pOps == NULL \
70 )\
71 { \
72 return (rc); \
73 } \
74 } while (0)
75
76/** Return / crash validation of a module argument. */
77#define KLDRMOD_VALIDATE(pMod) \
78 KLDRMOD_VALIDATE_EX(pMod, KERR_INVALID_PARAMETER)
79
80/** Return / crash validation of a module argument. */
81#define KLDRMOD_VALIDATE_VOID(pMod) \
82 do { \
83 if ( (pMod)->u32Magic != KLDRMOD_MAGIC \
84 || (pMod)->pOps == NULL \
85 )\
86 { \
87 return; \
88 } \
89 } while (0)
90
91
92/*******************************************************************************
93* Global Variables *
94*******************************************************************************/
95/** The list of module interpreters. */
96static PCKLDRMODOPS g_pModInterpreterHead = NULL;
97
98
99
100/*******************************************************************************
101* Internal Functions *
102*******************************************************************************/
103
104
105
106/**
107 * Open a executable image by file name.
108 *
109 * @returns 0 on success and *ppMod pointing to a module instance.
110 * On failure, a non-zero OS specific error code is returned.
111 * @param pszFilename The filename to open.
112 * @param fFlags Flags, MBZ.
113 * @param enmCpuArch The desired CPU architecture. KCPUARCH_UNKNOWN means
114 * anything goes, but with a preference for the current
115 * host architecture.
116 * @param ppMod Where to store the module handle.
117 */
118int kLdrModOpen(const char *pszFilename, KU32 fFlags, KCPUARCH enmCpuArch, PPKLDRMOD ppMod)
119{
120 /*
121 * Open the file using a bit provider.
122 */
123 PKRDR pRdr;
124 int rc = kRdrOpen(&pRdr, pszFilename);
125 if (!rc)
126 {
127 rc = kLdrModOpenFromRdr(pRdr, fFlags, enmCpuArch, ppMod);
128 if (!rc)
129 return 0;
130 kRdrClose(pRdr);
131 }
132 return rc;
133}
134
135
136/**
137 * Select image from the FAT according to the enmCpuArch and fFlag.
138 *
139 * @returns 0 on success and *poffHdr set to the image header.
140 * On failure, a non-zero error code is returned.
141 *
142 * @param pRdr The file provider instance to use.
143 * @param fFlags Flags, MBZ.
144 * @param enmCpuArch The desired CPU architecture. KCPUARCH_UNKNOWN means
145 * anything goes, but with a preference for the current
146 * host architecture.
147 * @param u32Magic The FAT magic.
148 * @param poffHdr Where to store the offset of the selected image.
149 */
150static int kldrModOpenFromRdrSelectImageFromFAT(PKRDR pRdr, KU32 fFlags, KCPUARCH enmCpuArch, KU32 u32Magic, KLDRFOFF *poffHdr)
151{
152 int rcRet = KLDR_ERR_CPU_ARCH_MISMATCH;
153 KLDRFOFF off = *poffHdr + sizeof(KU32);
154 KLDRFOFF offEndFAT;
155 KBOOL fCpuArchWhatever;
156 KU32 cArchs;
157 KU32 iArch;
158 int rc;
159
160 /* Read fat_header_t::nfat_arch. */
161 rc = kRdrRead(pRdr, &cArchs, sizeof(cArchs), off);
162 if (rc)
163 return rc;
164 off += sizeof(KU32);
165 if (u32Magic == IMAGE_FAT_SIGNATURE_OE)
166 cArchs = K_E2E_U32(cArchs);
167 if (cArchs == 0)
168 return KLDR_ERR_FAT_INVALID;
169
170 /* Deal with KCPUARCH_UNKNOWN. */
171 fCpuArchWhatever = enmCpuArch == KCPUARCH_UNKNOWN;
172 if (fCpuArchWhatever)
173 {
174 KCPU enmCpuIgnored;
175 kCpuGetArchAndCpu(&enmCpuArch, &enmCpuIgnored);
176 }
177
178 /*
179 * Iterate the architecture list.
180 */
181 offEndFAT = off + cArchs * sizeof(fat_arch_t);
182 for (iArch = 0; iArch < cArchs; iArch++)
183 {
184 KCPUARCH enmEntryArch;
185 fat_arch_t Arch;
186 rc = kRdrRead(pRdr, &Arch, sizeof(Arch), off);
187 if (rc)
188 return rc;
189 off += sizeof(Arch);
190
191 if (u32Magic == IMAGE_FAT_SIGNATURE_OE)
192 {
193 Arch.cputype = K_E2E_U32(Arch.cputype);
194 Arch.cpusubtype = K_E2E_U32(Arch.cpusubtype);
195 Arch.offset = K_E2E_U32(Arch.offset);
196 Arch.size = K_E2E_U32(Arch.size);
197 Arch.align = K_E2E_U32(Arch.align);
198 }
199
200 /* Simple validation. */
201 if ( (KLDRFOFF)Arch.offset < offEndFAT
202 || (KLDRFOFF)Arch.offset >= kRdrSize(pRdr)
203 || Arch.align >= 32
204 || Arch.offset & ((KU32_C(1) << Arch.align) - KU32_C(1)))
205 return KLDR_ERR_FAT_INVALID;
206
207 /* deal with the cputype and cpusubtype. (See similar code in kLdrModMachO.c.) */
208 switch (Arch.cputype)
209 {
210 case CPU_TYPE_X86:
211 enmEntryArch = KCPUARCH_X86_32;
212 switch (Arch.cpusubtype)
213 {
214 case CPU_SUBTYPE_I386_ALL:
215 /*case CPU_SUBTYPE_386: ^^ ;*/
216 case CPU_SUBTYPE_486:
217 case CPU_SUBTYPE_486SX:
218 /*case CPU_SUBTYPE_586: vv */
219 case CPU_SUBTYPE_PENT:
220 case CPU_SUBTYPE_PENTPRO:
221 case CPU_SUBTYPE_PENTII_M3:
222 case CPU_SUBTYPE_PENTII_M5:
223 case CPU_SUBTYPE_CELERON:
224 case CPU_SUBTYPE_CELERON_MOBILE:
225 case CPU_SUBTYPE_PENTIUM_3:
226 case CPU_SUBTYPE_PENTIUM_3_M:
227 case CPU_SUBTYPE_PENTIUM_3_XEON:
228 case CPU_SUBTYPE_PENTIUM_M:
229 case CPU_SUBTYPE_PENTIUM_4:
230 case CPU_SUBTYPE_PENTIUM_4_M:
231 case CPU_SUBTYPE_XEON:
232 case CPU_SUBTYPE_XEON_MP:
233 break;
234 default:
235 return KLDR_ERR_FAT_UNSUPPORTED_CPU_SUBTYPE;
236 }
237 break;
238
239 case CPU_TYPE_X86_64:
240 enmEntryArch = KCPUARCH_AMD64;
241 switch (Arch.cpusubtype & ~CPU_SUBTYPE_MASK)
242 {
243 case CPU_SUBTYPE_X86_64_ALL:
244 break;
245 default:
246 return KLDR_ERR_FAT_UNSUPPORTED_CPU_SUBTYPE;
247 }
248 break;
249
250 default:
251 enmEntryArch = KCPUARCH_UNKNOWN;
252 break;
253 }
254
255 /*
256 * Finally the actual image selecting.
257 *
258 * Return immediately on a perfect match. Otherwise continue looking,
259 * if we're none too picky, remember the first image in case we don't
260 * get lucky.
261 */
262 if (enmEntryArch == enmCpuArch)
263 {
264 *poffHdr = Arch.offset;
265 return 0;
266 }
267
268 if ( fCpuArchWhatever
269 && rcRet == KLDR_ERR_CPU_ARCH_MISMATCH)
270 {
271 *poffHdr = Arch.offset;
272 rcRet = 0;
273 }
274 }
275
276 return rcRet;
277}
278
279
280/**
281 * Open a executable image from a file provider instance.
282 *
283 * @returns 0 on success and *ppMod pointing to a module instance.
284 * On failure, a non-zero OS specific error code is returned.
285 * @param pRdr The file provider instance to use.
286 * On success, the ownership of the instance is taken by the
287 * module and the caller must not ever touch it again.
288 * (The instance is not closed on failure, the call has to do that.)
289 * @param fFlags Flags, MBZ.
290 * @param enmCpuArch The desired CPU architecture. KCPUARCH_UNKNOWN means
291 * anything goes, but with a preference for the current
292 * host architecture.
293 * @param ppMod Where to store the module handle.
294 */
295int kLdrModOpenFromRdr(PKRDR pRdr, KU32 fFlags, KCPUARCH enmCpuArch, PPKLDRMOD ppMod)
296{
297 union
298 {
299 KU32 u32;
300 KU16 u16;
301 KU16 au16[2];
302 KU8 au8[4];
303 } u;
304 KLDRFOFF offHdr = 0;
305 int rc;
306
307 for (;;)
308 {
309 /*
310 * Try figure out what kind of image this is.
311 * Always read the 'new header' if we encounter MZ.
312 */
313 rc = kRdrRead(pRdr, &u, sizeof(u), offHdr);
314 if (rc)
315 return rc;
316 if ( u.u16 == IMAGE_DOS_SIGNATURE
317 && kRdrSize(pRdr) > sizeof(IMAGE_DOS_HEADER))
318 {
319 rc = kRdrRead(pRdr, &u, sizeof(u.u32), K_OFFSETOF(IMAGE_DOS_HEADER, e_lfanew));
320 if (rc)
321 return rc;
322 if ((KLDRFOFF)u.u32 < kRdrSize(pRdr))
323 {
324 offHdr = u.u32;
325 rc = kRdrRead(pRdr, &u, sizeof(u.u32), offHdr);
326 if (rc)
327 return rc;
328 }
329 else
330 u.u16 = IMAGE_DOS_SIGNATURE;
331 }
332
333 /*
334 * Handle FAT images too here (one only).
335 */
336 if ( ( u.u32 == IMAGE_FAT_SIGNATURE
337 || u.u32 == IMAGE_FAT_SIGNATURE_OE)
338 && offHdr == 0)
339 {
340 rc = kldrModOpenFromRdrSelectImageFromFAT(pRdr, fFlags, enmCpuArch, u.u32, &offHdr);
341 if (rc)
342 return rc;
343 if (offHdr)
344 continue;
345 }
346 break;
347 }
348
349
350 /*
351 * Use the magic to select the appropriate image interpreter head on.
352 */
353 if (u.u16 == IMAGE_DOS_SIGNATURE)
354 rc = KLDR_ERR_MZ_NOT_SUPPORTED;
355 else if (u.u16 == IMAGE_NE_SIGNATURE)
356 rc = KLDR_ERR_NE_NOT_SUPPORTED;
357 else if (u.u16 == IMAGE_LX_SIGNATURE)
358 rc = g_kLdrModLXOps.pfnCreate(&g_kLdrModLXOps, pRdr, fFlags, enmCpuArch, offHdr, ppMod);
359 else if (u.u16 == IMAGE_LE_SIGNATURE)
360 rc = KLDR_ERR_LE_NOT_SUPPORTED;
361 else if (u.u32 == IMAGE_NT_SIGNATURE)
362 rc = g_kLdrModPEOps.pfnCreate(&g_kLdrModPEOps, pRdr, fFlags, enmCpuArch, offHdr, ppMod);
363 else if ( u.u32 == IMAGE_MACHO32_SIGNATURE
364 || u.u32 == IMAGE_MACHO32_SIGNATURE_OE
365 || u.u32 == IMAGE_MACHO64_SIGNATURE
366 || u.u32 == IMAGE_MACHO64_SIGNATURE_OE)
367 rc = g_kLdrModMachOOps.pfnCreate(&g_kLdrModMachOOps, pRdr, fFlags, enmCpuArch, offHdr, ppMod);
368 else if (u.u32 == IMAGE_ELF_SIGNATURE)
369 rc = KLDR_ERR_ELF_NOT_SUPPORTED;
370 else
371 rc = KLDR_ERR_UNKNOWN_FORMAT;
372
373 /*
374 * If no head on hit, let each interpreter have a go.
375 */
376 if (rc)
377 {
378 PCKLDRMODOPS pOps;
379 for (pOps = g_pModInterpreterHead; pOps; pOps = pOps->pNext)
380 {
381 int rc2 = pOps->pfnCreate(pOps, pRdr, fFlags, enmCpuArch, offHdr, ppMod);
382 if (!rc2)
383 return rc;
384 }
385 *ppMod = NULL;
386 }
387 return rc;
388}
389
390
391/**
392 * Closes an open module.
393 *
394 * The caller is responsible for calling kLdrModUnmap() and kLdrFreeTLS()
395 * before closing the module.
396 *
397 * @returns 0 on success, non-zero on failure. The module instance state
398 * is unknown on failure, it's best not to touch it.
399 * @param pMod The module.
400 */
401int kLdrModClose(PKLDRMOD pMod)
402{
403 KLDRMOD_VALIDATE(pMod);
404 return pMod->pOps->pfnDestroy(pMod);
405}
406
407
408/**
409 * Queries a symbol by name or ordinal number.
410 *
411 * @returns 0 and *puValue and *pfKind on success.
412 * KLDR_ERR_SYMBOL_NOT_FOUND is returned if the symbol wasn't found.
413 * Other failures could stem from bad executable format failures,
414 * read failure in case pvBits isn't specified and no mapping should be used.
415 * @param pMod The module.
416 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
417 * This can be used by some module interpreters to reduce memory consumption.
418 * @param BaseAddress The module base address to use when calculating the symbol value.
419 * There are two special values that can be used:
420 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
421 * @param iSymbol The symbol ordinal. (optional)
422 * @param pchSymbol The symbol name. (optional)
423 * Important, this doesn't have to be a null-terminated string.
424 * @param cchSymbol The length of the symbol name.
425 * @param pszVersion The symbol version. NULL if not versioned.
426 * @param pfnGetForwarder The callback to use when resolving a forwarder symbol. This is optional
427 * and if not specified KLDR_ERR_FORWARDER is returned instead.
428 * @param pvUser The user argument for the pfnGetForwarder callback.
429 * @param puValue Where to store the symbol value. (optional)
430 * @param pfKind On input one of the KLDRSYMKIND_REQ_* #defines.
431 * On output the symbol kind. (optional)
432 */
433int kLdrModQuerySymbol(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 iSymbol,
434 const char *pchSymbol, KSIZE cchSymbol, const char *pszVersion,
435 PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind)
436{
437 KLDRMOD_VALIDATE(pMod);
438 if (!puValue && !pfKind)
439 return KERR_INVALID_PARAMETER;
440 if (puValue)
441 *puValue = 0;
442 if (pfKind)
443 K_VALIDATE_FLAGS(*pfKind, KLDRSYMKIND_REQ_SEGMENTED);
444 return pMod->pOps->pfnQuerySymbol(pMod, pvBits, BaseAddress, iSymbol, pchSymbol, cchSymbol, pszVersion,
445 pfnGetForwarder, pvUser, puValue, pfKind);
446}
447
448
449/**
450 * Enumerate the symbols in the module.
451 *
452 * @returns 0 on success and non-zero a status code on failure.
453 * @param pMod The module which symbols should be enumerated.
454 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
455 * This can be used by some module interpreters to reduce memory consumption.
456 * @param BaseAddress The module base address to use when calculating the symbol values.
457 * There are two special values that could be can:
458 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
459 * @param fFlags The enumeration flags. A combination of the KLDRMOD_ENUM_SYMS_FLAGS_* \#defines.
460 * @param pfnCallback The enumeration callback function.
461 * @param pvUser The user argument to the callback function.
462 */
463int kLdrModEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 fFlags,
464 PFNKLDRMODENUMSYMS pfnCallback, void *pvUser)
465{
466 KLDRMOD_VALIDATE(pMod);
467 K_VALIDATE_FLAGS(fFlags, KLDRMOD_ENUM_SYMS_FLAGS_ALL);
468 return pMod->pOps->pfnEnumSymbols(pMod, pvBits, BaseAddress, fFlags, pfnCallback, pvUser);
469}
470
471
472/**
473 * Get the name of an import module by ordinal number.
474 *
475 * @returns 0 and name in pszName on success.
476 * On buffer overruns KERR_BUFFER_OVERFLOW will be returned.
477 * On other failures and appropriate error code is returned.
478 * @param pMod The module.
479 * @param pvBits Optional pointer to bits returned by kLdrModGetBits().
480 * This can be used by some module interpreters to reduce memory consumption.
481 * @param iImport The import module ordinal number.
482 * @param pszName Where to store the name.
483 * @param cchName The size of the name buffer.
484 */
485int kLdrModGetImport(PKLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZE cchName)
486{
487 KLDRMOD_VALIDATE(pMod);
488 return pMod->pOps->pfnGetImport(pMod, pvBits, iImport, pszName, cchName);
489}
490
491
492/**
493 * Get the number of import modules.
494 *
495 * @returns The number of import modules. -1 if something really bad happens.
496 * @param pMod The module.
497 * @param pvBits Optional pointer to bits returned by kLdrModGetBits().
498 * This can be used by some module interpreters to reduce memory consumption.
499 */
500KI32 kLdrModNumberOfImports(PKLDRMOD pMod, const void *pvBits)
501{
502 KLDRMOD_VALIDATE(pMod);
503 return pMod->pOps->pfnNumberOfImports(pMod, pvBits);
504}
505
506
507/**
508 * Checks if this module can be executed by the specified arch+cpu.
509 *
510 * @returns 0 if it can, KCPU_ERR_ARCH_CPU_NOT_COMPATIBLE if it can't.
511 * Other failures may occur and cause other return values.
512 * @param pMod The module.
513 * @param pvBits Optional pointer to bits returned by kLdrModGetBits().
514 * This can be used by some module interpreters to reduce memory consumption.
515 */
516int kLdrModCanExecuteOn(PKLDRMOD pMod, const void *pvBits, KCPUARCH enmArch, KCPU enmCpu)
517{
518 KLDRMOD_VALIDATE(pMod);
519 if (pMod->pOps->pfnCanExecuteOn)
520 return pMod->pOps->pfnCanExecuteOn(pMod, pvBits, enmArch, enmCpu);
521 return kCpuCompare(pMod->enmArch, pMod->enmCpu, enmArch, enmCpu);
522}
523
524
525/**
526 * Gets the image stack info.
527 *
528 * @returns 0 on success, non-zero on failure.
529 * @param pMod
530 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
531 * This can be used by some module interpreters to reduce memory consumption.
532 * @param BaseAddress The module base address to use when calculating the stack address.
533 * There are two special values that can be used:
534 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
535 * @param pStackInfo The stack information.
536 */
537int kLdrModGetStackInfo(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo)
538{
539 KLDRMOD_VALIDATE(pMod);
540 return pMod->pOps->pfnGetStackInfo(pMod, pvBits, BaseAddress, pStackInfo);
541}
542
543
544/**
545 * Queries the main entrypoint of the module.
546 *
547 * Only executable are supposed to have an main entrypoint, though some object and DLL
548 * formats will also allow this.
549 *
550 * @returns 0 and *pMainEPAddress on success. Non-zero status code on failure.
551 * @param pMod The module.
552 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
553 * This can be used by some module interpreters to reduce memory consumption.
554 * @param BaseAddress The module base address to use when calculating the entrypoint address.
555 * There are two special values that can be used:
556 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
557 * @param pMainEPAddress Where to store the entry point address.
558 */
559int kLdrModQueryMainEntrypoint(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress)
560{
561 KLDRMOD_VALIDATE(pMod);
562 *pMainEPAddress = 0;
563 return pMod->pOps->pfnQueryMainEntrypoint(pMod, pvBits, BaseAddress, pMainEPAddress);
564}
565
566
567/**
568 * Queries info about a resource.
569 *
570 * If there are multiple resources matching the criteria, the best or
571 * first match will be return.
572 *
573 *
574 * @returns 0 on success.
575 * @returns Whatever non-zero status returned by pfnCallback (enumeration was stopped).
576 * @returns non-zero kLdr or native status code on failure.
577 *
578 * @param pMod The module.
579 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
580 * This can be used by some module interpreters to reduce memory consumption.
581 * @param BaseAddress The module base address to use when calculating the resource addresses.
582 * There are two special values that can be used:
583 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
584 * @param idType The resource type id to match if not NIL_KLDRMOD_RSRC_TYPE_ID.
585 * @param pszType The resource type name to match if no NULL.
586 * @param idName The resource name id to match if not NIL_KLDRMOD_RSRC_NAME_ID.
587 * @param pszName The resource name to match if not NULL.
588 * @param idLang The language id to match.
589 * @param pfnCallback The callback function.
590 * @param pvUser The user argument for the callback.
591 */
592int kLdrModQueryResource(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
593 KU32 idName, const char *pszName, KU32 idLang, PKLDRADDR pAddrRsrc, KSIZE *pcbRsrc)
594{
595 KLDRMOD_VALIDATE(pMod);
596 if (!pAddrRsrc && !pcbRsrc)
597 return KERR_INVALID_PARAMETER;
598 if (pAddrRsrc)
599 *pAddrRsrc = NIL_KLDRADDR;
600 if (pcbRsrc)
601 *pcbRsrc = 0;
602 return pMod->pOps->pfnQueryResource(pMod, pvBits, BaseAddress, idType, pszType, idName, pszName, idLang, pAddrRsrc, pcbRsrc);
603}
604
605
606/**
607 * Enumerates the resources matching the specfied criteria.
608 *
609 *
610 * @returns 0 on success.
611 * @returns Whatever non-zero status returned by pfnCallback (enumeration was stopped).
612 * @returns non-zero kLdr or native status code on failure.
613 *
614 * @param pMod The module.
615 * @param pvBits Optional pointer to bits returned by kLdrModGetBits() currently located at BaseAddress.
616 * This can be used by some module interpreters to reduce memory consumption.
617 * @param BaseAddress The module base address to use when calculating the resource addresses.
618 * There are two special values that can be used:
619 * KLDRMOD_BASEADDRESS_LINK and KLDRMOD_BASEADDRESS_MAP.
620 * @param idType The resource type id to match if not NIL_KLDRMOD_RSRC_TYPE_ID.
621 * @param pszType The resource type name to match if no NULL.
622 * @param idName The resource name id to match if not NIL_KLDRMOD_RSRC_NAME_ID.
623 * @param pszName The resource name to match if not NULL.
624 * @param idLang The language id to match.
625 * @param pfnCallback The callback function.
626 * @param pvUser The user argument for the callback.
627 */
628int kLdrModEnumResources(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
629 KU32 idName, const char *pszName, KU32 idLang, PFNKLDRENUMRSRC pfnCallback, void *pvUser)
630{
631 KLDRMOD_VALIDATE(pMod);
632 return pMod->pOps->pfnEnumResources(pMod, pvBits, BaseAddress, idType, pszType, idName, pszName, idLang, pfnCallback, pvUser);
633}
634
635
636/**
637 * Enumerate the debug info formats contained in the executable image.
638 *
639 * @returns 0 on success, non-zero OS or kLdr status code on failure, or non-zero callback status.
640 * @param pMod The module.
641 * @param pvBits Optional pointer to bits returned by kLdrModGetBits().
642 * This can be used by some module interpreters to reduce memory consumption.
643 * @param pfnCallback The callback function.
644 * @param pvUser The user argument.
645 * @see pg_kDbg for the debug info reader.
646 */
647int kLdrModEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser)
648{
649 KLDRMOD_VALIDATE(pMod);
650 return pMod->pOps->pfnEnumDbgInfo(pMod, pvBits, pfnCallback, pvUser);
651}
652
653
654/**
655 * Checks if the module has debug info embedded or otherwise associated with it.
656 *
657 * @returns 0 if it has debug info, KLDR_ERR_NO_DEBUG_INFO if no debug info,
658 * and non-zero OS or kLdr status code on failure.
659 * @param pMod The module.
660 * @param pvBits Optional pointer to bits returned by kLdrModGetBits().
661 * This can be used by some module interpreters to reduce memory consumption.
662 */
663int kLdrModHasDbgInfo(PKLDRMOD pMod, const void *pvBits)
664{
665 KLDRMOD_VALIDATE(pMod);
666 return pMod->pOps->pfnHasDbgInfo(pMod, pvBits);
667}
668
669
670/**
671 * May free up some resources held by the module.
672 *
673 * @todo define exactly what it possible to do after this call.
674 *
675 * @returns 0 on success, KLDR_ERR_* on failure.
676 * @param pMod The module.
677 */
678int kLdrModMostlyDone(PKLDRMOD pMod)
679{
680 KLDRMOD_VALIDATE(pMod);
681 return pMod->pOps->pfnMostlyDone(pMod);
682}
683
684
685/**
686 * Maps the module into the memory of the caller.
687 *
688 * On success the actual addresses for the segments can be found in MapAddress
689 * member of each segment in the segment array.
690 *
691 * @returns 0 on success, non-zero OS or kLdr status code on failure.
692 * @param pMod The module to be mapped.
693 * @remark kLdr only supports one mapping at a time of a module.
694 */
695int kLdrModMap(PKLDRMOD pMod)
696{
697 KLDRMOD_VALIDATE(pMod);
698 return pMod->pOps->pfnMap(pMod);
699}
700
701
702/**
703 * Unmaps a module previously mapped by kLdrModMap().
704 *
705 * @returns 0 on success, non-zero OS or kLdr status code on failure.
706 * @param pMod The module to unmap.
707 */
708int kLdrModUnmap(PKLDRMOD pMod)
709{
710 KLDRMOD_VALIDATE(pMod);
711 return pMod->pOps->pfnUnmap(pMod);
712}
713
714
715/**
716 * Allocates Thread Local Storage for module mapped by kLdrModMap().
717 *
718 * Calling kLdrModAllocTLS() more than once without calling kLdrModFreeTLS()
719 * between each invocation is not supported.
720 *
721 * @returns 0 on success, non-zero OS or kLdr status code on failure.
722 * @param pMod The module.
723 */
724int kLdrModAllocTLS(PKLDRMOD pMod)
725{
726 KLDRMOD_VALIDATE(pMod);
727 return pMod->pOps->pfnAllocTLS(pMod);
728}
729
730
731/**
732 * Frees Thread Local Storage previously allocated by kLdrModAllocTLS().
733 *
734 * The caller is responsible for only calling kLdrModFreeTLS() once
735 * after calling kLdrModAllocTLS().
736 *
737 * @returns 0 on success, non-zero OS or kLdr status code on failure.
738 * @param pMod The module.
739 */
740void kLdrModFreeTLS(PKLDRMOD pMod)
741{
742 KLDRMOD_VALIDATE_VOID(pMod);
743 pMod->pOps->pfnFreeTLS(pMod);
744}
745
746
747/**
748 * Reloads all dirty pages in a module previously mapped by kLdrModMap().
749 *
750 * The module interpreter may omit code pages if it can safely apply code
751 * fixups again in a subsequent kLdrModFixupMapping() call.
752 *
753 * The caller is responsible for freeing TLS before calling this function.
754 *
755 * @returns 0 on success, non-zero OS or kLdr status code on failure.
756 * @param pMod The module.
757 */
758int kLdrModReload(PKLDRMOD pMod)
759{
760 KLDRMOD_VALIDATE(pMod);
761 return pMod->pOps->pfnReload(pMod);
762}
763
764
765/**
766 * Fixup the mapping made by kLdrModMap().
767 *
768 * The caller is only responsible for not calling this function more than
769 * once without doing kLDrModReload() inbetween.
770 *
771 * @returns 0 on success, non-zero OS or kLdr status code on failure.
772 * @param pMod The module.
773 * @param pfnGetImport The callback for resolving external (imported) symbols.
774 * @param pvUser The callback user argument.
775 */
776int kLdrModFixupMapping(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
777{
778 KLDRMOD_VALIDATE(pMod);
779 return pMod->pOps->pfnFixupMapping(pMod, pfnGetImport, pvUser);
780}
781
782
783/**
784 * Call the module initializiation function of a mapped module (if any).
785 *
786 * @returns 0 on success or no init function, non-zero on init function failure or invalid pMod.
787 * @param pMod The module.
788 * @param uHandle The module handle to use if any of the init functions requires the module handle.
789 */
790int kLdrModCallInit(PKLDRMOD pMod, KUPTR uHandle)
791{
792 KLDRMOD_VALIDATE(pMod);
793 return pMod->pOps->pfnCallInit(pMod, uHandle);
794}
795
796
797/**
798 * Call the module termination function of a mapped module (if any).
799 *
800 * @returns 0 on success or no term function, non-zero on invalid pMod.
801 * @param pMod The module.
802 * @param uHandle The module handle to use if any of the term functions requires the module handle.
803 *
804 * @remark Termination function failure will be ignored by the module interpreter.
805 */
806int kLdrModCallTerm(PKLDRMOD pMod, KUPTR uHandle)
807{
808 KLDRMOD_VALIDATE(pMod);
809 return pMod->pOps->pfnCallTerm(pMod, uHandle);
810}
811
812
813/**
814 * Call the thread attach or detach function of a mapped module (if any).
815 *
816 * Any per-thread TLS initialization/termination will have to be done at this time too.
817 *
818 * @returns 0 on success or no attach/detach function, non-zero on attach failure or invalid pMod.
819 * @param pMod The module.
820 * @param uHandle The module handle to use if any of the thread attach/detach functions
821 * requires the module handle.
822 *
823 * @remark Detach function failure will be ignored by the module interpreter.
824 */
825int kLdrModCallThread(PKLDRMOD pMod, KUPTR uHandle, unsigned fAttachingOrDetaching)
826{
827 KLDRMOD_VALIDATE(pMod);
828 K_VALIDATE_FLAGS(fAttachingOrDetaching, 1);
829 return pMod->pOps->pfnCallThread(pMod, uHandle, fAttachingOrDetaching);
830}
831
832
833/**
834 * Get the size of the mapped module.
835 *
836 * @returns The size of the mapped module (in bytes).
837 * @param pMod The module.
838 */
839KLDRADDR kLdrModSize(PKLDRMOD pMod)
840{
841 KLDRMOD_VALIDATE_EX(pMod, 0);
842 return pMod->pOps->pfnSize(pMod);
843}
844
845
846/**
847 * Gets the module bits.
848 *
849 * The module interpreter will fill a mapping allocated by the caller with the
850 * module bits reallocated to the specified address.
851 *
852 * @returns 0 on succes, non-zero OS or kLdr status code on failure.
853 * @param pMod The module.
854 * @param pvBits Where to put the bits.
855 * @param BaseAddress The base address that should correspond to the first byte in pvBits
856 * upon return.
857 * @param pfnGetImport The callback ufor resolving external (imported) symbols.
858 * @param pvUser The callback user argument.
859 */
860int kLdrModGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
861{
862 KLDRMOD_VALIDATE(pMod);
863 return pMod->pOps->pfnGetBits(pMod, pvBits, BaseAddress, pfnGetImport, pvUser);
864}
865
866
867/**
868 * Relocates the module bits previously obtained by kLdrModGetBits().
869 *
870 * @returns 0 on succes, non-zero OS or kLdr status code on failure.
871 * @param pMod The module.
872 * @param pvBits Where to put the bits.
873 * @param NewBaseAddress The new base address.
874 * @param OldBaseAddress The old base address (i.e. the one specified to kLdrModGetBits() or as
875 * NewBaseAddressto the previous kLdrModRelocateBits() call).
876 * @param pfnGetImport The callback ufor resolving external (imported) symbols.
877 * @param pvUser The callback user argument.
878 */
879int kLdrModRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
880 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
881{
882 KLDRMOD_VALIDATE(pMod);
883 return pMod->pOps->pfnRelocateBits(pMod, pvBits, NewBaseAddress, OldBaseAddress, pfnGetImport, pvUser);
884}
885
Note: See TracBrowser for help on using the repository browser.