source: trunk/kStuff/include/k/kLdr.h@ 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: 56.2 KB
Line 
1/* $Id: kLdr.h 3573 2007-08-31 04:09:23Z bird $ */
2/** @file
3 *
4 * kLdr - The Dynamic Loader.
5 *
6 * Copyright (c) 2006 knut st. osmundsen <bird@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#ifndef ___k_kLdr_h___
28#define ___k_kLdr_h___
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/*
35 * Include the base typedefs and macros.
36 */
37#include <k/kDefs.h>
38#include <k/kTypes.h>
39
40
41/** @defgroup grp_kLdrBasic kLdr Basic Types
42 * @{ */
43
44/** The kLdr address type. */
45typedef KU64 KLDRADDR;
46/** Pointer to a kLdr address. */
47typedef KLDRADDR *PKLDRADDR;
48/** Pointer to a const kLdr address. */
49typedef const KLDRADDR *PCKLDRADDR;
50
51/** NIL address. */
52#define NIL_KLDRADDR (~(KU64)0)
53
54/** @def PRI_KLDRADDR
55 * printf format type. */
56#ifdef _MSC_VER
57# define PRI_KLDRADDR "I64x"
58#else
59# define PRI_KLDRADDR "llx"
60#endif
61
62/** Align a KSIZE value. */
63#define KLDR_ALIGN_ADDR(val, align) ( ((val) + ((align) - 1)) & ~(KLDRADDR)((align) - 1) )
64
65
66/** The kLdr size type. */
67typedef KU64 KLDRSIZE;
68/** Pointer to a kLdr size. */
69typedef KLDRSIZE *PKLDRSIZE;
70/** Pointer to a const kLdr size. */
71typedef const KLDRSIZE *PCKLDRSIZE;
72
73/** @def PRI_KLDRSIZE
74 * printf format type. */
75#ifdef _MSC_VER
76# define PRI_KLDRSIZE "I64x"
77#else
78# define PRI_KLDRSIZE "llx"
79#endif
80
81
82/** The kLdr file offset type. */
83typedef long KLDRFOFF;
84/** Pointer to a kLdr file offset type. */
85typedef KLDRFOFF *PKLDRFOFF;
86/** Pointer to a const kLdr file offset type. */
87typedef const KLDRFOFF *PCKLDRFOFF;
88
89/** @def PRI_KLDRFOFF
90 * printf format type. */
91#define PRI_KLDRFOFF "lx"
92
93
94/**
95 * Union of all the integer types.
96 */
97typedef union KLDRU
98{
99 KI8 i8; /**< KI8 view. */
100 KU8 u8; /**< KU8 view. */
101 KI16 i16; /**< KI16 view. */
102 KU16 u16; /**< KU16 view. */
103 KI32 i32; /**< KI32 view. */
104 KU32 u32; /**< KU32 view. */
105 KI64 i64; /**< KI64 view. */
106 KU64 u64; /**< KU64 view. */
107
108 KI8 ai8[8]; /**< KI8 array view . */
109 KU8 au8[8]; /**< KU8 array view. */
110 KI16 ai16[4];/**< KI16 array view . */
111 KU16 au16[4];/**< KU16 array view. */
112 KI32 ai32[2];/**< KI32 array view . */
113 KU32 au32[2];/**< KU32 array view. */
114
115 signed char ch; /**< signed char view. */
116 unsigned char uch; /**< unsigned char view. */
117 signed short s; /**< signed short view. */
118 unsigned short us; /**< unsigned short view. */
119 signed int i; /**< signed int view. */
120 unsigned int u; /**< unsigned int view. */
121 signed long l; /**< signed long view. */
122 unsigned long ul; /**< unsigned long view. */
123 void *pv; /**< void pointer view. */
124
125 KLDRADDR Addr; /**< kLdr address view. */
126 KLDRSIZE Size; /**< kLdr size view. */
127} KLDRU;
128/** Pointer to an integer union. */
129typedef KLDRU *PKLDRU;
130/** Pointer to a const integer union. */
131typedef const KLDRU *PCKLDRU;
132
133
134/**
135 * Union of pointers to all the integer types.
136 */
137typedef union KLDRPU
138{
139 KI8 *pi8; /**< KI8 view. */
140 KU8 *pu8; /**< KU8 view. */
141 KI16 *pi16; /**< KI16 view. */
142 KU16 *pu16; /**< KU16 view. */
143 KI32 *pi32; /**< KI32 view. */
144 KU32 *pu32; /**< KU32 view. */
145 KI64 *pi64; /**< KI64 view. */
146 KU64 *pu64; /**< KU64 view. */
147
148 signed char *pch; /**< signed char view. */
149 unsigned char *puch; /**< unsigned char view. */
150 signed short *ps; /**< signed short view. */
151 unsigned short *pus; /**< unsigned short view. */
152 signed int *pi; /**< signed int view. */
153 unsigned int *pu; /**< unsigned int view. */
154 signed long *pl; /**< signed long view. */
155 unsigned long *pul; /**< unsigned long view. */
156 void *pv; /**< void pointer view. */
157} KLDRPU;
158/** Pointer to an integer pointer union. */
159typedef KLDRPU *PKLDRPU;
160/** Pointer to a const integer pointer union. */
161typedef const KLDRPU *PCKLDRPU;
162
163
164/** Pointer to a loader segment. */
165typedef struct KLDRSEG *PKLDRSEG;
166/** Pointer to a loader segment. */
167typedef const struct KLDRSEG *PCKLDRSEG;
168
169/** @} */
170
171
172/** @defgroup grp_kLdrRdr kLdrRdr - The file provider
173 * @{ */
174
175/** Pointer to a file provider instance core. */
176typedef struct KLDRRDR *PKLDRRDR;
177/** Pointer to a file provider instance core pointer. */
178typedef struct KLDRRDR **PPKLDRRDR;
179
180/**
181 * File provider instance operations.
182 */
183typedef struct KLDRRDROPS
184{
185 /** The name of this file provider. */
186 const char *pszName;
187 /** Pointer to the next file provider. */
188 const struct KLDRRDROPS *pNext;
189
190 /** Try create a new file provider instance.
191 *
192 * @returns 0 on success, OS specific error code on failure.
193 * @param ppRdr Where to store the file provider instance.
194 * @param pszFilename The filename to open.
195 */
196 int (* pfnCreate)( PPKLDRRDR ppRdr, const char *pszFilename);
197 /** Destroy the file provider instance.
198 *
199 * @returns 0 on success, OS specific error code on failure.
200 * On failure, the file provider instance will be in an indeterminate state - don't touch it!
201 * @param pRdr The file provider instance.
202 */
203 int (* pfnDestroy)( PKLDRRDR pRdr);
204 /** @copydoc kLdrRdrRead */
205 int (* pfnRead)( PKLDRRDR pRdr, void *pvBuf, KSIZE cb, KLDRFOFF off);
206 /** @copydoc kLdrRdrAllMap */
207 int (* pfnAllMap)( PKLDRRDR pRdr, const void **ppvBits);
208 /** @copydoc kLdrRdrAllUnmap */
209 int (* pfnAllUnmap)(PKLDRRDR pRdr, const void *pvBits);
210 /** @copydoc kLdrRdrSize */
211 KLDRFOFF (* pfnSize)( PKLDRRDR pRdr);
212 /** @copydoc kLdrRdrTell */
213 KLDRFOFF (* pfnTell)( PKLDRRDR pRdr);
214 /** @copydoc kLdrRdrName */
215 const char * (* pfnName)(PKLDRRDR pRdr);
216 /** @copydoc kLdrRdrPageSize */
217 KSIZE (* pfnPageSize)(PKLDRRDR pRdr);
218 /** @copydoc kLdrRdrMap */
219 int (* pfnMap)( PKLDRRDR pRdr, void **ppvBase, KU32 cSegments, PCKLDRSEG paSegments, unsigned fFixed);
220 /** @copydoc kLdrRdrRefresh */
221 int (* pfnRefresh)( PKLDRRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments);
222 /** @copydoc kLdrRdrProtect */
223 int (* pfnProtect)( PKLDRRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments, unsigned fUnprotectOrProtect);
224 /** @copydoc kLdrRdrUnmap */
225 int (* pfnUnmap)( PKLDRRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments);
226 /** @copydoc kLdrRdrDone */
227 void (* pfnDone)( PKLDRRDR pRdr);
228 /** The usual non-zero dummy that makes sure we've initialized all members. */
229 KU32 u32Dummy;
230} KLDRRDROPS;
231/** Pointer to file provider operations. */
232typedef KLDRRDROPS *PKLDRRDROPS;
233/** Pointer to const file provider operations. */
234typedef const KLDRRDROPS *PCKLDRRDROPS;
235
236
237/**
238 * File provider instance core.
239 */
240typedef struct KLDRRDR
241{
242 /** Magic number (KLDRRDR_MAGIC). */
243 KU32 u32Magic;
244 /** Pointer to the file provider operations. */
245 PCKLDRRDROPS pOps;
246} KLDRRDR;
247
248/** The magic for KLDRRDR::u32Magic. (Katsu Aki (Katsuaki Nakamura)) */
249#define KLDRRDR_MAGIC 0x19610919
250
251void kLdrRdrAddProvider(PKLDRRDROPS pAdd);
252
253int kLdrRdrOpen( PPKLDRRDR ppRdr, const char *pszFilename);
254int kLdrRdrClose( PKLDRRDR pRdr);
255int kLdrRdrRead( PKLDRRDR pRdr, void *pvBuf, KSIZE cb, KLDRFOFF off);
256int kLdrRdrAllMap( PKLDRRDR pRdr, const void **ppvBits);
257int kLdrRdrAllUnmap(PKLDRRDR pRdr, const void *pvBits);
258KLDRFOFF kLdrRdrSize( PKLDRRDR pRdr);
259KLDRFOFF kLdrRdrTell( PKLDRRDR pRdr);
260const char *kLdrRdrName(PKLDRRDR pRdr);
261KSIZE kLdrRdrPageSize(PKLDRRDR pRdr);
262int kLdrRdrMap( PKLDRRDR pRdr, void **ppvBase, KU32 cSegments, PCKLDRSEG paSegments, unsigned fFixed);
263int kLdrRdrRefresh( PKLDRRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments);
264int kLdrRdrProtect( PKLDRRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments, unsigned fUnprotectOrProtect);
265int kLdrRdrUnmap( PKLDRRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments);
266void kLdrRdrDone( PKLDRRDR pRdr);
267
268/** @} */
269
270
271
272/** @defgroup grp_kLdrMod kLdrMod - The executable image intepreter
273 * @{ */
274
275/**
276 * CPU Architecture.
277 * @todo Double check the non intel architectures.
278 */
279typedef enum KLDRARCH
280{
281 /** The usual invalid one. */
282 KLDRARCH_INVALID = 0,
283 /** Clone or Intel 16-bit x86. */
284 KLDRARCH_X86_16,
285 /** Clone or Intel 32-bit x86. */
286 KLDRARCH_X86_32,
287 /** AMD64 (including clones). */
288 KLDRARCH_AMD64,
289 /** Itanic (64-bit). */
290 KLDRARCH_IA64,
291 /** ALPHA (64-bit). */
292 KLDRARCH_ALPHA,
293 /** ALPHA limited to 32-bit. */
294 KLDRARCH_ALPHA_32,
295 /** 32-bit ARM. */
296 KLDRARCH_ARM_32,
297 /** 64-bit ARM. */
298 KLDRARCH_ARM_64,
299 /** 32-bit MIPS. */
300 KLDRARCH_MIPS_32,
301 /** 64-bit MIPS. */
302 KLDRARCH_MIPS_64,
303 /** 32-bit PowerPC. */
304 KLDRARCH_POWERPC_32,
305 /** 64-bit PowerPC. */
306 KLDRARCH_POWERPC_64,
307 /** 32-bit SPARC. */
308 KLDRARCH_SPARC_32,
309 /** 64-bit SPARC. */
310 KLDRARCH_SPARC_64,
311 /** The end of the valid architecture values (exclusive). */
312 KLDRARCH_END,
313 /** Hack to blow the type up to 32-bit. */
314 KLDRARCH_32BIT_HACK = 0x7fffffff
315} KLDRARCH;
316/** Pointer to a CPU architecture type. */
317typedef KLDRARCH *PKLDRARCH;
318
319/**
320 * CPU models.
321 */
322typedef enum KLDRCPU
323{
324 /** The usual invalid cpu. */
325 KLDRCPU_INVALID = 0,
326 /** @name KLDRARCH_X86_16
327 * @{ */
328 KLDRCPU_I8086,
329 KLDRCPU_I8088,
330 KLDRCPU_I80186,
331 KLDRCPU_I80286,
332 KLDRCPU_I386_16,
333 KLDRCPU_I486_16,
334 KLDRCPU_I486SX_16,
335 KLDRCPU_I586_16,
336 KLDRCPU_I686_16,
337 KLDRCPU_P4_16,
338 KLDRCPU_CORE2_16,
339 KLDRCPU_K6_16,
340 KLDRCPU_K7_16,
341 KLDRCPU_K8_16,
342 KLDRCPU_FIRST_X86_16 = KLDRCPU_I8086,
343 KLDRCPU_LAST_X86_16 = KLDRCPU_K8_16,
344 /** @} */
345
346 /** @name KLDRARCH_X86_32
347 * @{ */
348 KLDRCPU_X86_32_BLEND,
349 KLDRCPU_I386,
350 KLDRCPU_I486,
351 KLDRCPU_I486SX,
352 KLDRCPU_I586,
353 KLDRCPU_I686,
354 KLDRCPU_P4,
355 KLDRCPU_CORE2_32,
356 KLDRCPU_K6,
357 KLDRCPU_K7,
358 KLDRCPU_K8_32,
359 KLDRCPU_FIRST_X86_32 = KLDRCPU_I386,
360 KLDRCPU_LAST_X86_32 = KLDRCPU_K8_32,
361 /** @} */
362
363 /** @name KLDRARCH_AMD64
364 * @{ */
365 KLDRCPU_AMD64_BLEND,
366 KLDRCPU_K8,
367 KLDRCPU_P4_64,
368 KLDRCPU_CORE2,
369 KLDRCPU_FIRST_AMD64 = KLDRCPU_K8,
370 KLDRCPU_LAST_AMD64 = KLDRCPU_CORE2,
371 /** @} */
372
373 /** The end of the valid cpu values (exclusive). */
374 KLDRCPU_END,
375 /** Hack to blow the type up to 32-bit. */
376 KLDRCPU_32BIT_HACK = 0x7fffffff
377} KLDRCPU;
378/** Pointer to a CPU type. */
379typedef KLDRCPU *PKLDRCPU;
380
381void kLdrGetArchCpu(PKLDRARCH penmArch, PKLDRCPU penmCpu);
382int kLdrCompareCpus(KLDRARCH enmCodeArch, KLDRCPU enmCodeCpu, KLDRARCH enmArch, KLDRCPU enmCpu);
383
384
385/**
386 * Debug info type (from the loader point of view).
387 */
388typedef enum KLDRDBGINFOTYPE
389{
390 /** The usual invalid enum value. */
391 KLDRDBGINFOTYPE_INVALID = 0,
392 /** Unknown debug info format. */
393 KLDRDBGINFOTYPE_UNKNOWN,
394 /** Stabs. */
395 KLDRDBGINFOTYPE_STABS,
396 /** Debug With Arbitrary Record Format (DWARF). */
397 KLDRDBGINFOTYPE_DWARF,
398 /** Microsoft Codeview debug info. */
399 KLDRDBGINFOTYPE_CODEVIEW,
400 /** Watcom debug info. */
401 KLDRDBGINFOTYPE_WATCOM,
402 /** IBM High Level Language debug info.. */
403 KLDRDBGINFOTYPE_HLL,
404 /** The end of the valid debug info values (exclusive). */
405 KLDRDBGINFOTYPE_END,
406 /** Blow the type up to 32-bit. */
407 KLDRDBGINFOTYPE_32BIT_HACK = 0x7fffffff
408} KLDRDBGINFOTYPE;
409/** Pointer to a kLdr debug info type. */
410typedef KLDRDBGINFOTYPE *PKLDRDBGINFOTYPE;
411
412
413/**
414 * Stack information.
415 */
416typedef struct KLDRSTACKINFO
417{
418 /** The base address of the stack (sub) segment.
419 * Set this to NIL_KLDRADDR if the module doesn't include any stack segment. */
420 KLDRADDR Address;
421 /** The base address of the stack (sub) segment, link address.
422 * Set this to NIL_KLDRADDR if the module doesn't include any stack (sub)segment. */
423 KLDRADDR LinkAddress;
424 /** The stack size of the main thread.
425 * If no stack (sub)segment in the module, this is the stack size of the main thread.
426 * If the module doesn't contain this kind of information this field will be set to 0. */
427 KLDRSIZE cbStack;
428 /** The stack size of non-main threads.
429 * If the module doesn't contain this kind of information this field will be set to 0. */
430 KLDRSIZE cbStackThread;
431} KLDRSTACKINFO;
432/** Pointer to stack information. */
433typedef KLDRSTACKINFO *PKLDRSTACKINFO;
434/** Pointer to const stack information. */
435typedef const KLDRSTACKINFO *PCKLDRSTACKINFO;
436
437
438/**
439 * Loader segment.
440 */
441typedef struct KLDRSEG
442{
443 /** Variable free to use for the kLdr user. */
444 void *pvUser;
445 /** The segment name. (Might not be zero terminated!) */
446 const char *pchName;
447 /** The length of the segment name. */
448 KU32 cchName;
449 /** The flat selector to use for the segment (i.e. data/code).
450 * Primarily a way for the user to specify selectors for the LX/LE and NE interpreters. */
451 KU16 SelFlat;
452 /** The 16-bit selector to use for the segment.
453 * Primarily a way for the user to specify selectors for the LX/LE and NE interpreters. */
454 KU16 Sel16bit;
455 /** Segment flags. */
456 KU32 fFlags;
457 /** The segment protection. */
458 KPROT enmProt;
459 /** The size of the segment. */
460 KLDRSIZE cb;
461 /** The required segment alignment.
462 * The to 0 if the segment isn't supposed to be mapped. */
463 KLDRADDR Alignment;
464 /** The link address.
465 * Set to NIL_KLDRADDR if the segment isn't supposed to be
466 * mapped or if the image doesn't have link addresses. */
467 KLDRADDR LinkAddress;
468 /** File offset of the segment.
469 * Set to -1 if no file backing (like BSS). */
470 KLDRFOFF offFile;
471 /** Size of the file bits of the segment.
472 * Set to -1 if no file backing (like BSS). */
473 KLDRFOFF cbFile;
474 /** The relative virtual address when mapped.
475 * Set to NIL_KLDRADDR if the segment isn't supposed to be mapped. */
476 KLDRADDR RVA;
477 /** The size of the segment including the alignment gap up to the next segment when mapped. */
478 KSIZE cbMapped;
479 /** The address the segment was mapped at by kLdrModMap().
480 * Set to 0 if not mapped. */
481 KUPTR MapAddress;
482} KLDRSEG;
483
484
485/** @name Segment flags
486 * @{ */
487/** The segment is 16-bit. When not set the default of the target architecture is assumed. */
488#define KLDRSEG_FLAG_16BIT 1
489/** The segment requires a 16-bit selector alias. (OS/2) */
490#define KLDRSEG_FLAG_OS2_ALIAS16 2
491/** Conforming segment (x86 weirdness). (OS/2) */
492#define KLDRSEG_FLAG_OS2_CONFORM 4
493/** IOPL (ring-2) segment. (OS/2) */
494#define KLDRSEG_FLAG_OS2_IOPL 8
495/** @} */
496
497
498/**
499 * Loader module format.
500 */
501typedef enum KLDRFMT
502{
503 /** The usual invalid 0 format. */
504 KLDRFMT_INVALID = 0,
505 /** The native OS loader. */
506 KLDRFMT_NATIVE,
507 /** The AOUT loader. */
508 KLDRFMT_AOUT,
509 /** The ELF loader. */
510 KLDRFMT_ELF,
511 /** The LX loader. */
512 KLDRFMT_LX,
513 /** The Mach-O loader. */
514 KLDRFMT_MACHO,
515 /** The PE loader. */
516 KLDRFMT_PE,
517 /** The end of the valid format values (exclusive). */
518 KLDRFMT_END,
519 /** Hack to blow the type up to 32-bit. */
520 KLDRFMT_32BIT_HACK = 0x7fffffff
521} KLDRFMT;
522
523
524/**
525 * Loader module type.
526 */
527typedef enum KLDRTYPE
528{
529 /** The usual invalid 0 type. */
530 KLDRTYPE_INVALID = 0,
531 /** Object file. */
532 KLDRTYPE_OBJECT,
533 /** Executable module, fixed load address. */
534 KLDRTYPE_EXECUTABLE_FIXED,
535 /** Executable module, relocatable, non-fixed load address. */
536 KLDRTYPE_EXECUTABLE_RELOCATABLE,
537 /** Executable module, position independent code, non-fixed load address. */
538 KLDRTYPE_EXECUTABLE_PIC,
539 /** Shared library, fixed load address.
540 * Typically a system library. */
541 KLDRTYPE_SHARED_LIBRARY_FIXED,
542 /** Shared library, relocatable, non-fixed load address. */
543 KLDRTYPE_SHARED_LIBRARY_RELOCATABLE,
544 /** Shared library, position independent code, non-fixed load address. */
545 KLDRTYPE_SHARED_LIBRARY_PIC,
546 /** DLL that contains no code or data only imports and exports. (Chiefly OS/2.) */
547 KLDRTYPE_FORWARDER_DLL,
548 /** Core or dump. */
549 KLDRTYPE_CORE,
550 /** The end of the valid types values (exclusive). */
551 KLDRTYPE_END,
552 /** Hack to blow the type up to 32-bit. */
553 KLDRTYPE_32BIT_HACK = 0x7fffffff
554} KLDRTYPE;
555
556
557/**
558 * Loader endian indicator.
559 */
560typedef enum KLDRENDIAN
561{
562 /** The usual invalid endian. */
563 KLDRENDIAN_INVALID,
564 /** Little endian. */
565 KLDRENDIAN_LITTLE,
566 /** Bit endian. */
567 KLDRENDIAN_BIG,
568 /** Endianness doesn't have a meaning in the context. */
569 KLDRENDIAN_NA,
570 /** The end of the valid endian values (exclusive). */
571 KLDRENDIAN_END,
572 /** Hack to blow the type up to 32-bit. */
573 KLDRENDIAN_32BIT_HACK = 0x7fffffff
574} KLDRENDIAN;
575
576
577/** Pointer to a module interpreter method table. */
578typedef struct KLDRMODOPS *PKLDRMODOPS;
579/** Pointer to const module interpreter methods table. */
580typedef const struct KLDRMODOPS *PCKLDRMODOPS;
581
582/**
583 * Module interpreter instance.
584 * All members are read only unless you're kLdrMod or the module interpreter.
585 */
586typedef struct KLDRMOD
587{
588 /** Magic number (KLDRMOD_MAGIC). */
589 KU32 u32Magic;
590 /** The format of this module. */
591 KLDRFMT enmFmt;
592 /** The type of module. */
593 KLDRTYPE enmType;
594 /** The architecture this module was built for. */
595 KLDRARCH enmArch;
596 /** The minium cpu this module was built for.
597 * This might not be accurate, so use kLdrModCanExecuteOn() to check. */
598 KLDRARCH enmCpu;
599 /** The endian used by the module. */
600 KLDRENDIAN enmEndian;
601 /** The filename length (bytes). */
602 KU32 cchFilename;
603 /** The filename. */
604 const char *pszFilename;
605 /** The module name. */
606 const char *pszName;
607 /** The module name length (bytes). */
608 KU32 cchName;
609 /** The number of segments in the module. */
610 KU32 cSegments;
611 /** Pointer to the loader methods.
612 * Not meant for calling directly thru! */
613 PCKLDRMODOPS pOps;
614 /** Pointer to the read instance. (Can be NULL after kLdrModDone().)*/
615 PKLDRRDR pRdr;
616 /** The module data. */
617 void *pvData;
618 /** Segments. (variable size, can be zero) */
619 KLDRSEG aSegments[1];
620} KLDRMOD, *PKLDRMOD, **PPKLDRMOD;
621
622/** The magic for KLDRMOD::u32Magic. (Kosuke Fujishima) */
623#define KLDRMOD_MAGIC 0x19640707
624
625
626/** Special base address value alias for the link address. */
627#define KLDRMOD_BASEADDRESS_LINK (~(KLDRADDR)1)
628/** Special base address value alias for the actual load address (must be mapped). */
629#define KLDRMOD_BASEADDRESS_MAP (~(KLDRADDR)2)
630
631/** Special import module ordinal value used to indicate that there is no
632 * specific module associated with the requested symbol. */
633#define NIL_KLDRMOD_IMPORT (~(KU32)0)
634
635/** Special symbol ordinal value used to indicate that the symbol
636 * only has a string name. */
637#define NIL_KLDRMOD_SYM_ORDINAL (~(KU32)0)
638
639
640/** @name Load symbol kind flags.
641 * @{ */
642/** The bitness doesn't matter. */
643#define KLDRSYMKIND_NO_BIT 0x00000000
644/** 16-bit symbol. */
645#define KLDRSYMKIND_16BIT 0x00000001
646/** 32-bit symbol. */
647#define KLDRSYMKIND_32BIT 0x00000002
648/** 64-bit symbol. */
649#define KLDRSYMKIND_64BIT 0x00000003
650/** Mask out the bit.*/
651#define KLDRSYMKIND_BIT_MASK 0x00000003
652/** We don't know the type of symbol. */
653#define KLDRSYMKIND_NO_TYPE 0x00000000
654/** The symbol is a code object (method/function/procedure/whateveryouwannacallit). */
655#define KLDRSYMKIND_CODE 0x00000010
656/** The symbol is a data object. */
657#define KLDRSYMKIND_DATA 0x00000020
658/** Mask out the symbol type. */
659#define KLDRSYMKIND_TYPE_MASK 0x00000030
660/** Valid symbol kind mask. */
661#define KLDRSYMKIND_MASK 0x00000033
662/** Weak symbol. */
663#define KLDRSYMKIND_WEAK 0x00000100
664/** Forwarder symbol. */
665#define KLDRSYMKIND_FORWARDER 0x00000200
666/** Request a flat symbol address. */
667#define KLDRSYMKIND_REQ_FLAT 0x00000000
668/** Request a segmented symbol address. */
669#define KLDRSYMKIND_REQ_SEGMENTED 0x40000000
670/** Request type mask. */
671#define KLDRSYMKIND_REQ_TYPE_MASK 0x40000000
672/** @} */
673
674/** @name kLdrModEnumSymbols flags.
675 * @{ */
676/** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
677#define KLDRMOD_ENUM_SYMS_FLAGS_ALL 0x00000001
678/** @} */
679
680
681/**
682 * Callback for resolving imported symbols when applying fixups.
683 *
684 * @returns 0 on success and *pValue and *pfKind filled.
685 * @returns Non-zero OS specific or kLdr status code on failure.
686 *
687 * @param pMod The module which fixups are begin applied.
688 * @param iImport The import module ordinal number or NIL_KLDRMOD_IMPORT.
689 * @param iSymbol The symbol ordinal number or NIL_KLDRMOD_SYM_ORDINAL.
690 * @param pchSymbol The symbol name. Can be NULL if iSymbol isn't nil. Doesn't have to be null-terminated.
691 * @param cchSymbol The length of the symbol.
692 * @param pszVersion The symbol version. NULL if not versioned.
693 * @param puValue Where to store the symbol value.
694 * @param pfKind Where to store the symbol kind flags.
695 * @param pvUser The user parameter specified to the relocation function.
696 */
697typedef int FNKLDRMODGETIMPORT(PKLDRMOD pMod, KU32 iImport, KU32 iSymbol, const char *pchSymbol, KSIZE cchSymbol,
698 const char *pszVersion, PKLDRADDR puValue, KU32 *pfKind, void *pvUser);
699/** Pointer to a import callback. */
700typedef FNKLDRMODGETIMPORT *PFNKLDRMODGETIMPORT;
701
702/**
703 * Symbol enumerator callback.
704 *
705 * @returns 0 if enumeration should continue.
706 * @returns non-zero if the enumeration should stop. This status code will then be returned by kLdrModEnumSymbols().
707 *
708 * @param pMod The module which symbols are being enumerated.s
709 * @param iSymbol The symbol ordinal number or NIL_KLDRMOD_SYM_ORDINAL.
710 * @param pchSymbol The symbol name. This can be NULL if there is a symbol ordinal.
711 * This can also be an empty string if the symbol doesn't have a name
712 * or it's name has been stripped.
713 * Important, this doesn't have to be a null-terminated string.
714 * @param cchSymbol The length of the symbol.
715 * @param pszVersion The symbol version. NULL if not versioned.
716 * @param uValue The symbol value.
717 * @param fKind The symbol kind flags.
718 * @param pvUser The user parameter specified to kLdrModEnumSymbols().
719 */
720typedef int FNKLDRMODENUMSYMS(PKLDRMOD pMod, KU32 iSymbol, const char *pchSymbol, KSIZE cchSymbol, const char *pszVersion,
721 KLDRADDR uValue, KU32 fKind, void *pvUser);
722/** Pointer to a symbol enumerator callback. */
723typedef FNKLDRMODENUMSYMS *PFNKLDRMODENUMSYMS;
724
725/**
726 * Debug info enumerator callback.
727 *
728 * @returns 0 to continue the enumeration.
729 * @returns non-zero if the enumeration should stop. This status code will then be returned by kLdrModEnumDbgInfo().
730 *
731 * @param pMod The module.
732 * @param iDbgInfo The debug info ordinal number / id.
733 * @param enmType The debug info type.
734 * @param iMajorVer The major version number of the debug info format. -1 if unknow - implies invalid iMinorVer.
735 * @param iMinorVer The minor version number of the debug info format. -1 when iMajorVer is -1.
736 * @param offFile The file offset *if* this type has one specific location in the executable image file.
737 * This is -1 if there isn't any specific file location.
738 * @param LinkAddress The link address of the debug info if it's loadable. NIL_KLDRADDR if not loadable.
739 * @param cb The size of the debug information. -1 is used if this isn't applicable.
740 * @param pszExtFile This points to the name of an external file containing the debug info.
741 * This is NULL if there isn't any external file.
742 * @param pvUser The user parameter specified to kLdrModEnumDbgInfo.
743 */
744typedef int FNKLDRENUMDBG(PKLDRMOD pMod, KU32 iDbgInfo, KLDRDBGINFOTYPE enmType, KI16 iMajorVer, KI16 iMinorVer,
745 KLDRFOFF offFile, KLDRADDR LinkAddress, KLDRSIZE cb, const char *pszExtFile, void *pvUser);
746/** Pointer to a debug info enumerator callback. */
747typedef FNKLDRENUMDBG *PFNKLDRENUMDBG;
748
749/**
750 * Resource enumerator callback.
751 *
752 * @returns 0 to continue the enumeration.
753 * @returns non-zero if the enumeration should stop. This status code will then be returned by kLdrModEnumResources().
754 *
755 * @param pMod The module.
756 * @param idType The resource type id. NIL_KLDRMOD_RSRC_TYPE_ID if no type id.
757 * @param pszType The resource type name. NULL if no type name.
758 * @param idName The resource id. NIL_KLDRMOD_RSRC_NAME_ID if no id.
759 * @param pszName The resource name. NULL if no name.
760 * @param idLang The language id.
761 * @param AddrRsrc The address value for the resource.
762 * @param cbRsrc The size of the resource.
763 * @param pvUser The user parameter specified to kLdrModEnumDbgInfo.
764 */
765typedef int FNKLDRENUMRSRC(PKLDRMOD pMod, KU32 idType, const char *pszType, KU32 idName, const char *pszName,
766 KU32 idLang, KLDRADDR AddrRsrc, KLDRSIZE cbRsrc, void *pvUser);
767/** Pointer to a resource enumerator callback. */
768typedef FNKLDRENUMRSRC *PFNKLDRENUMRSRC;
769
770/** NIL resource name ID. */
771#define NIL_KLDRMOD_RSRC_NAME_ID ( ~(KU32)0 )
772/** NIL resource type ID. */
773#define NIL_KLDRMOD_RSRC_TYPE_ID ( ~(KU32)0 )
774/** @name Language ID
775 *
776 * Except for the special IDs #defined here, the values are considered
777 * format specific for now since it's only used by the PE resources.
778 *
779 * @{ */
780/** NIL language ID. */
781#define NIL_KLDR_LANG_ID ( ~(KU32)0 )
782/** Special language id value for matching any language. */
783#define KLDR_LANG_ID_ANY ( ~(KU32)1 )
784/** Special language id value indicating language neutral. */
785#define KLDR_LANG_ID_NEUTRAL ( ~(KU32)2 )
786/** Special language id value indicating user default language. */
787#define KLDR_LANG_ID_USER_DEFAULT ( ~(KU32)3 )
788/** Special language id value indicating system default language. */
789#define KLDR_LANG_ID_SYS_DEFAULT ( ~(KU32)4 )
790/** Special language id value indicating default custom locale. */
791#define KLDR_LANG_ID_CUSTOM_DEFAULT ( ~(KU32)5 )
792/** Special language id value indicating unspecified custom locale. */
793#define KLDR_LANG_ID_CUSTOM_UNSPECIFIED ( ~(KU32)6 )
794/** Special language id value indicating default custom MUI locale. */
795#define KLDR_LANG_ID_UI_CUSTOM_DEFAULT ( ~(KU32)7 )
796/** @} */
797
798
799int kLdrModOpen(const char *pszFilename, PPKLDRMOD ppMod);
800int kLdrModOpenFromRdr(PKLDRRDR pRdr, PPKLDRMOD ppMod);
801int kLdrModOpenNative(const char *pszFilename, PPKLDRMOD ppMod);
802int kLdrModOpenNativeByHandle(KUPTR uHandle, PPKLDRMOD ppMod);
803int kLdrModClose(PKLDRMOD pMod);
804
805int kLdrModQuerySymbol(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 iSymbol,
806 const char *pchSymbol, KSIZE cchSymbol, const char *pszVersion,
807 PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind);
808int kLdrModEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress,
809 KU32 fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser);
810int kLdrModGetImport(PKLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZE cchName);
811KI32 kLdrModNumberOfImports(PKLDRMOD pMod, const void *pvBits);
812int kLdrModCanExecuteOn(PKLDRMOD pMod, const void *pvBits, KLDRARCH enmArch, KLDRCPU enmCpu);
813int kLdrModGetStackInfo(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo);
814int kLdrModQueryMainEntrypoint(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress);
815int kLdrModQueryResource(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
816 KU32 idName, const char *pszName, KU32 idLang, PKLDRADDR pAddrRsrc, KSIZE *pcbRsrc);
817int kLdrModEnumResources(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
818 KU32 idName, const char *pszName, KU32 idLang, PFNKLDRENUMRSRC pfnCallback, void *pvUser);
819int kLdrModEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser);
820int kLdrModHasDbgInfo(PKLDRMOD pMod, const void *pvBits);
821int kLdrModMostlyDone(PKLDRMOD pMod);
822
823
824/** @name Operations On The Internally Managed Mapping
825 * @{ */
826int kLdrModMap(PKLDRMOD pMod);
827int kLdrModUnmap(PKLDRMOD pMod);
828int kLdrModAllocTLS(PKLDRMOD pMod);
829void kLdrModFreeTLS(PKLDRMOD pMod);
830int kLdrModReload(PKLDRMOD pMod);
831int kLdrModFixupMapping(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
832int kLdrModCallInit(PKLDRMOD pMod, KUPTR uHandle);
833int kLdrModCallTerm(PKLDRMOD pMod, KUPTR uHandle);
834int kLdrModCallThread(PKLDRMOD pMod, KUPTR uHandle, unsigned fAttachingOrDetaching);
835/** @} */
836
837/** @name Operations On The Externally Managed Mappings
838 * @{ */
839KLDRADDR kLdrModSize(PKLDRMOD pMod);
840int kLdrModGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
841int kLdrModRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
842 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
843/** @} */
844
845
846/**
847 * The loader module operation.
848 */
849typedef struct KLDRMODOPS
850{
851 /** The name of this module interpreter. */
852 const char *pszName;
853 /** Pointer to the next module interpreter. */
854 PCKLDRMODOPS pNext;
855
856 /**
857 * Create a loader module instance interpreting the executable image found
858 * in the specified file provider instance.
859 *
860 * @returns 0 on success and *ppMod pointing to a module instance.
861 * On failure, a non-zero OS specific error code is returned.
862 * @param pOps Pointer to the registered method table.
863 * @param pRdr The file provider instance to use.
864 * @param offNewHdr The offset of the new header in MZ files. -1 if not found.
865 * @param ppMod Where to store the module instance pointer.
866 */
867 int (* pfnCreate)(PCKLDRMODOPS pOps, PKLDRRDR pRdr, KLDRFOFF offNewHdr, PPKLDRMOD ppMod);
868 /**
869 * Destroys an loader module instance.
870 *
871 * The caller is responsible for calling kLdrModUnmap() and kLdrFreeTLS() first.
872 *
873 * @returns 0 on success, non-zero on failure. The module instance state
874 * is unknown on failure, it's best not to touch it.
875 * @param pMod The module.
876 */
877 int (* pfnDestroy)(PKLDRMOD pMod);
878
879 /** @copydoc kLdrModQuerySymbol */
880 int (* pfnQuerySymbol)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 iSymbol,
881 const char *pchSymbol, KSIZE cchSymbol, const char *pszVersion,
882 PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind);
883 /** @copydoc kLdrModEnumSymbols */
884 int (* pfnEnumSymbols)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 fFlags,
885 PFNKLDRMODENUMSYMS pfnCallback, void *pvUser);
886 /** @copydoc kLdrModGetImport */
887 int (* pfnGetImport)(PKLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZE cchName);
888 /** @copydoc kLdrModNumberOfImports */
889 KI32 (* pfnNumberOfImports)(PKLDRMOD pMod, const void *pvBits);
890 /** @copydoc kLdrModCanExecuteOn */
891 int (* pfnCanExecuteOn)(PKLDRMOD pMod, const void *pvBits, KLDRARCH enmArch, KLDRCPU enmCpu);
892 /** @copydoc kLdrModGetStackInfo */
893 int (* pfnGetStackInfo)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo);
894 /** @copydoc kLdrModQueryMainEntrypoint */
895 int (* pfnQueryMainEntrypoint)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress);
896 /** @copydoc kLdrModQueryResource */
897 int (* pfnQueryResource)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
898 KU32 idName, const char *pszName, KU32 idLang, PKLDRADDR pAddrRsrc, KSIZE *pcbRsrc);
899 /** @copydoc kLdrModEnumResources */
900 int (* pfnEnumResources)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 idType, const char *pszType,
901 KU32 idName, const char *pszName, KU32 idLang, PFNKLDRENUMRSRC pfnCallback, void *pvUser);
902 /** @copydoc kLdrModEnumDbgInfo */
903 int (* pfnEnumDbgInfo)(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser);
904 /** @copydoc kLdrModHasDbgInfo */
905 int (* pfnHasDbgInfo)(PKLDRMOD pMod, const void *pvBits);
906 /** @copydoc kLdrModMap */
907 int (* pfnMap)(PKLDRMOD pMod);
908 /** @copydoc kLdrModUnmap */
909 int (* pfnUnmap)(PKLDRMOD pMod);
910 /** @copydoc kLdrModAllocTLS */
911 int (* pfnAllocTLS)(PKLDRMOD pMod);
912 /** @copydoc kLdrModFreeTLS */
913 void (* pfnFreeTLS)(PKLDRMOD pMod);
914 /** @copydoc kLdrModReload */
915 int (* pfnReload)(PKLDRMOD pMod);
916 /** @copydoc kLdrModFixupMapping */
917 int (* pfnFixupMapping)(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
918 /** @copydoc kLdrModCallInit */
919 int (* pfnCallInit)(PKLDRMOD pMod, KUPTR uHandle);
920 /** @copydoc kLdrModCallTerm */
921 int (* pfnCallTerm)(PKLDRMOD pMod, KUPTR uHandle);
922 /** @copydoc kLdrModCallThread */
923 int (* pfnCallThread)(PKLDRMOD pMod, KUPTR uHandle, unsigned fAttachingOrDetaching);
924 /** @copydoc kLdrModSize */
925 KLDRADDR (* pfnSize)(PKLDRMOD pMod);
926 /** @copydoc kLdrModGetBits */
927 int (* pfnGetBits)(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
928 /** @copydoc kLdrModRelocateBits */
929 int (* pfnRelocateBits)(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
930 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
931 /** @copydoc kLdrModMostlyDone */
932 int (* pfnMostlyDone)(PKLDRMOD pMod);
933 /** Dummy which should be assigned a non-zero value. */
934 KU32 uEndOfStructure;
935} KLDRMODOPS;
936
937
938/** @} */
939
940
941
942
943/** @defgroup grp_kLdrDyld kLdrDyld - The dynamic loader
944 * @{ */
945
946/** The handle to a dynamic loader module. */
947typedef struct KLDRDYLDMOD *HKLDRMOD;
948/** Pointer to the handle to a dynamic loader module. */
949typedef HKLDRMOD *PHKLDRMOD;
950/** NIL handle value. */
951#define NIL_HKLDRMOD ((HKLDRMOD)0)
952
953
954/**
955 * File search method.
956 *
957 * In addition to it's own way of finding files, kLdr emulates
958 * the methods employed by the most popular systems.
959 */
960typedef enum KLDRDYLDSEARCH
961{
962 /** The usual invalid file search method. */
963 KLDRDYLD_SEARCH_INVALID = 0,
964 /** Uses the kLdr file search method.
965 * @todo invent me. */
966 KLDRDYLD_SEARCH_KLDR,
967 /** Use the emulation closest to the host system. */
968 KLDRDYLD_SEARCH_HOST,
969 /** Emulate the OS/2 file search method.
970 * On non-OS/2 systems, BEGINLIBPATH, LIBPATH, ENDLIBPATH and LIBPATHSTRICT are
971 * taken form the environment. */
972 KLDRDYLD_SEARCH_OS2,
973 /** Emulate the standard window file search method. */
974 KLDRDYLD_SEARCH_WINDOWS,
975 /** Emulate the alternative window file search method. */
976 KLDRDYLD_SEARCH_WINDOWS_ALTERED,
977 /** Emulate the most common UNIX file search method. */
978 KLDRDYLD_SEARCH_UNIX_COMMON,
979 /** End of the valid file search method values. */
980 KLDRDYLD_SEARCH_END,
981 /** Hack to blow the type up to 32-bit. */
982 KLDRDYLD_SEARCH_32BIT_HACK = 0x7fffffff
983} KLDRDYLDSEARCH;
984
985/** @name kLdrDyldLoad and kLdrDyldFindByName flags.
986 * @{ */
987/** The symbols in the module should be loaded into the global unix namespace.
988 * If not specified, the symbols are local and can only be referenced directly. */
989#define KLDRYDLD_LOAD_FLAGS_GLOBAL_SYMBOLS 0x00000001
990/** The symbols in the module should be loaded into the global unix namespace and
991 * it's symbols should take precedence over all currently loaded modules.
992 * This implies KLDRYDLD_LOAD_FLAGS_GLOBAL_SYMBOLS. */
993#define KLDRYDLD_LOAD_FLAGS_DEEP_SYMBOLS 0x00000002
994/** The module shouldn't be found by a global module search.
995 * If not specified, the module can be found by unspecified module searches,
996 * typical used when loading import/dep modules. */
997#define KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE 0x00000004
998/** Do a recursive initialization calls instead of defering them to the outermost call. */
999#define KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT 0x00000008
1000/** We're loading the executable module.
1001 * @internal */
1002#define KLDRDYLD_LOAD_FLAGS_EXECUTABLE 0x40000000
1003/** @} */
1004
1005
1006int kLdrDyldLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
1007 unsigned fFlags, PHKLDRMOD phMod, char *pszErr, KSIZE cchErr);
1008int kLdrDyldUnload(HKLDRMOD hMod);
1009int kLdrDyldFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
1010 unsigned fFlags, PHKLDRMOD phMod);
1011int kLdrDyldFindByAddress(KUPTR Address, PHKLDRMOD phMod, KU32 *piSegment, KUPTR *poffSegment);
1012int kLdrDyldGetName(HKLDRMOD hMod, char *pszName, KSIZE cchName);
1013int kLdrDyldGetFilename(HKLDRMOD hMod, char *pszFilename, KSIZE cchFilename);
1014int kLdrDyldQuerySymbol(HKLDRMOD hMod, KU32 uSymbolOrdinal, const char *pszSymbolName,
1015 const char *pszSymbolVersion, KUPTR *pValue, KU32 *pfKind);
1016int kLdrDyldQueryResource(HKLDRMOD hMod, KU32 idType, const char *pszType, KU32 idName,
1017 const char *pszName, KU32 idLang, void **pvRsrc, KSIZE *pcbRsrc);
1018int kLdrDyldEnumResources(HKLDRMOD hMod, KU32 idType, const char *pszType, KU32 idName,
1019 const char *pszName, KU32 idLang, PFNKLDRENUMRSRC pfnCallback, void *pvUser);
1020
1021
1022/** @name OS/2 like API
1023 * @{ */
1024#if defined(__OS2__)
1025# define KLDROS2API _System
1026#else
1027# define KLDROS2API
1028#endif
1029int kLdrDosLoadModule(char *pszObject, KSIZE cbObject, const char *pszModule, PHKLDRMOD phMod);
1030int kLdrDosFreeModule(HKLDRMOD hMod);
1031int kLdrDosQueryModuleHandle(const char *pszModname, PHKLDRMOD phMod);
1032int kLdrDosQueryModuleName(HKLDRMOD hMod, KSIZE cchName, char *pszName);
1033int kLdrDosQueryProcAddr(HKLDRMOD hMod, KU32 iOrdinal, const char *pszProcName, void **ppvProcAddr);
1034int kLdrDosQueryProcType(HKLDRMOD hMod, KU32 iOrdinal, const char *pszProcName, KU32 *pfProcType);
1035int kLdrDosQueryModFromEIP(PHKLDRMOD phMod, KU32 *piObject, KSIZE cbName, char *pszName, KUPTR *poffObject, KUPTR ulEIP);
1036int kLdrDosReplaceModule(const char *pszOldModule, const char *pszNewModule, const char *pszBackupModule);
1037int kLdrDosGetResource(HKLDRMOD hMod, KU32 idType, KU32 idName, void **pvResAddr);
1038int kLdrDosQueryResourceSize(HKLDRMOD hMod, KU32 idType, KU32 idName, KU32 *pcb);
1039int kLdrDosFreeResource(void *pvResAddr);
1040/** @} */
1041
1042/** @name POSIX like API
1043 * @{ */
1044HKLDRMOD kLdrDlOpen(const char *pszLibrary, int fFlags);
1045const char *kLdrDlError(void);
1046void * kLdrDlSym(HKLDRMOD hMod, const char *pszSymbol);
1047int kLdrDlClose(HKLDRMOD hMod);
1048/** @todo GNU extensions */
1049/** @} */
1050
1051/** @name Win32 like API
1052 * @{ */
1053#if defined(_MSC_VER)
1054# define KLDRWINAPI __stdcall
1055#else
1056# define KLDRWINAPI
1057#endif
1058HKLDRMOD KLDRWINAPI kLdrWLoadLibrary(const char *pszFilename);
1059HKLDRMOD KLDRWINAPI kLdrWLoadLibraryEx(const char *pszFilename, void *hFileReserved, KU32 fFlags);
1060KU32 KLDRWINAPI kLdrWGetModuleFileName(HKLDRMOD hMod, char *pszModName, KSIZE cchModName);
1061HKLDRMOD KLDRWINAPI kLdrWGetModuleHandle(const char *pszFilename);
1062int KLDRWINAPI kLdrWGetModuleHandleEx(KU32 fFlags, const char *pszFilename, HKLDRMOD hMod);
1063void * KLDRWINAPI kLdrWGetProcAddress(HKLDRMOD hMod, const char *pszProcName);
1064KU32 KLDRWINAPI kLdrWGetDllDirectory(KSIZE cchDir, char *pszDir);
1065int KLDRWINAPI kLdrWSetDllDirectory(const char *pszDir);
1066int KLDRWINAPI kLdrWFreeLibrary(HKLDRMOD hMod);
1067int KLDRWINAPI kLdrWDisableThreadLibraryCalls(HKLDRMOD hMod);
1068
1069/** The handle to a resource that's been found. */
1070typedef struct KLDRWRSRCFOUND *HKLDRWRSRCFOUND;
1071/** The handle to a loaded resource. */
1072typedef struct KLDRWRSRCLOADED *HKLDRWRSRCLOADED;
1073HKLDRWRSRCFOUND KLDRWINAPI kLdrWFindResource(HKLDRMOD hMod, const char *pszType, const char *pszName);
1074HKLDRWRSRCFOUND KLDRWINAPI kLdrWFindResourceEx(HKLDRMOD hMod, const char *pszType, const char *pszName, KU16 idLang);
1075KU32 KLDRWINAPI kLdrWSizeofResource(HKLDRMOD hMod, HKLDRWRSRCFOUND hFoundRsrc);
1076HKLDRWRSRCLOADED KLDRWINAPI kLdrWLoadResource(HKLDRMOD hMod, HKLDRWRSRCFOUND hFoundRsrc);
1077void *KLDRWINAPI kLdrWLockResource(HKLDRMOD hMod, HKLDRWRSRCLOADED hLoadedRsrc);
1078int KLDRWINAPI kLdrWFreeResource(HKLDRMOD hMod, HKLDRWRSRCLOADED hLoadedRsrc);
1079
1080typedef int (KLDRWINAPI *PFNKLDRWENUMRESTYPE)(HKLDRMOD hMod, const char *pszType, KUPTR uUser);
1081int KLDRWINAPI kLdrWEnumResourceTypes(HKLDRMOD hMod, PFNKLDRWENUMRESTYPE pfnEnum, KUPTR uUser);
1082int KLDRWINAPI kLdrWEnumResourceTypesEx(HKLDRMOD hMod, PFNKLDRWENUMRESTYPE pfnEnum, KUPTR uUser, KU32 fFlags, KU16 idLang);
1083
1084typedef int (KLDRWINAPI *PFNKLDRWENUMRESNAME)(HKLDRMOD hMod, const char *pszType, char *pszName, KUPTR uUser);
1085int KLDRWINAPI kLdrWEnumResourceNames(HKLDRMOD hMod, const char *pszType, PFNKLDRWENUMRESNAME pfnEnum, KUPTR uUser);
1086int KLDRWINAPI kLdrWEnumResourceNamesEx(HKLDRMOD hMod, const char *pszType, PFNKLDRWENUMRESNAME pfnEnum, KUPTR uUser, KU32 fFlags, KU16 idLang);
1087
1088typedef int (KLDRWINAPI *PFNKLDRWENUMRESLANG)(HKLDRMOD hMod, const char *pszType, const char *pszName, KU16 idLang, KUPTR uUser);
1089int KLDRWINAPI kLdrWEnumResourceLanguages(HKLDRMOD hMod, const char *pszType, const char *pszName, PFNKLDRWENUMRESLANG pfnEnum, KUPTR uUser);
1090int KLDRWINAPI kLdrWEnumResourceLanguagesEx(HKLDRMOD hMod, const char *pszType, const char *pszName,
1091 PFNKLDRWENUMRESLANG pfnEnum, KUPTR uUser, KU32 fFlags, KU16 idLang);
1092/** @} */
1093
1094
1095/** @name Process Bootstrapping
1096 * @{ */
1097
1098/**
1099 * Argument package from the stub.
1100 */
1101typedef struct KLDREXEARGS
1102{
1103 /** Load & search flags, some which will become defaults. */
1104 KU32 fFlags;
1105 /** The default search method. */
1106 KLDRDYLDSEARCH enmSearch;
1107 /** The executable file that the stub is supposed to load. */
1108 char szExecutable[260];
1109 /** The default prefix used when searching for DLLs. */
1110 char szDefPrefix[16];
1111 /** The default suffix used when searching for DLLs. */
1112 char szDefSuffix[16];
1113 /** The LD_LIBRARY_PATH prefix for the process.. */
1114 char szLibPath[4096 - sizeof(KU32) - sizeof(KLDRDYLDSEARCH) - 16 - 16 - 260];
1115} KLDREXEARGS, *PKLDREXEARGS;
1116/** Pointer to a const argument package from the stub. */
1117typedef const KLDREXEARGS *PCKLDREXEARGS;
1118
1119void kLdrLoadExe(PCKLDREXEARGS pArgs, void *pvOS); /** @todo fix this mess... */
1120void kLdrDyldLoadExe(PCKLDREXEARGS pArgs, void *pvOS);
1121
1122/** @} */
1123
1124/** @} */
1125
1126
1127/** @defgroup grp_kLdrErr kLdr Status Codes
1128 * kLdr uses a mix of native status codes and it's own status codes.
1129 * A status code of 0 means success, all other status codes means failure.
1130 * @{
1131 */
1132
1133#define KLDR_ERR_BASE 420000
1134
1135/** The image format is unknown. */
1136#define KLDR_ERR_UNKNOWN_FORMAT (KLDR_ERR_BASE + 0)
1137/** The MZ image format isn't supported by this kLdr build. */
1138#define KLDR_ERR_MZ_NOT_SUPPORTED (KLDR_ERR_BASE + 1)
1139/** The NE image format isn't supported by this kLdr build. */
1140#define KLDR_ERR_NE_NOT_SUPPORTED (KLDR_ERR_BASE + 2)
1141/** The LX image format isn't supported by this kLdr build. */
1142#define KLDR_ERR_LX_NOT_SUPPORTED (KLDR_ERR_BASE + 3)
1143/** The LE image format isn't supported by this kLdr build. */
1144#define KLDR_ERR_LE_NOT_SUPPORTED (KLDR_ERR_BASE + 4)
1145/** The PE image format isn't supported by this kLdr build. */
1146#define KLDR_ERR_PE_NOT_SUPPORTED (KLDR_ERR_BASE + 5)
1147/** The ELF image format isn't supported by this kLdr build. */
1148#define KLDR_ERR_ELF_NOT_SUPPORTED (KLDR_ERR_BASE + 6)
1149/** The mach-o image format isn't supported by this kLdr build. */
1150#define KLDR_ERR_MACHO_NOT_SUPPORTED (KLDR_ERR_BASE + 7)
1151/** The FAT image format isn't supported by this kLdr build or
1152 * a direct open was attempt without going thru the FAT file provider.
1153 * FAT images are also known as Universal Binaries. */
1154#define KLDR_ERR_FAT_NOT_SUPPORTED (KLDR_ERR_BASE + 8)
1155/** The a.out image format isn't supported by this kLdr build. */
1156#define KLDR_ERR_AOUT_NOT_SUPPORTED (KLDR_ERR_BASE + 9)
1157
1158/** Invalid parameter to a kLdr API. */
1159#define KLDR_ERR_INVALID_PARAMETER (KLDR_ERR_BASE + 32)
1160/** Invalid handle parameter to a kLdr API. */
1161#define KLDR_ERR_INVALID_HANDLE (KLDR_ERR_BASE + 33)
1162/** The module wasn't loaded dynamically. */
1163#define KLDR_ERR_NOT_LOADED_DYNAMICALLY (KLDR_ERR_BASE + 34)
1164/** The module wasn't found. */
1165#define KLDR_ERR_MODULE_NOT_FOUND (KLDR_ERR_BASE + 35)
1166/** A prerequisit module wasn't found. */
1167#define KLDR_ERR_PREREQUISITE_MODULE_NOT_FOUND (KLDR_ERR_BASE + 36)
1168/** The module is being terminated and can therefore not be loaded. */
1169#define KLDR_ERR_MODULE_TERMINATING (KLDR_ERR_BASE + 37)
1170/** A prerequisit module is being terminated and can therefore not be loaded. */
1171#define KLDR_ERR_PREREQUISITE_MODULE_TERMINATING (KLDR_ERR_BASE + 38)
1172/** The module initialization failed. */
1173#define KLDR_ERR_MODULE_INIT_FAILED (KLDR_ERR_BASE + 39)
1174/** The initialization of a prerequisite module failed. */
1175#define KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED (KLDR_ERR_BASE + 40)
1176/** The module has already failed initialization and can't be attempted reloaded until
1177 * after we've finished garbage collection. */
1178#define KLDR_ERR_MODULE_INIT_FAILED_ALREADY (KLDR_ERR_BASE + 41)
1179/** A prerequisite module has already failed initialization and can't be attempted
1180 * reloaded until after we've finished garbage collection. */
1181#define KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED_ALREADY (KLDR_ERR_BASE + 42)
1182/** Prerequisite recursed too deeply. */
1183#define KLDR_ERR_PREREQUISITE_RECURSED_TOO_DEEPLY (KLDR_ERR_BASE + 43)
1184/** Failed to allocate the main stack. */
1185#define KLDR_ERR_MAIN_STACK_ALLOC_FAILED (KLDR_ERR_BASE + 44)
1186/** Buffer overflow. */
1187#define KLDR_ERR_BUFFER_OVERFLOW (KLDR_ERR_BASE + 45)
1188/** The specified ARCH+CPU isn't compatible with image. */
1189#define KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE (KLDR_ERR_BASE + 46)
1190/** Symbol not found. */
1191#define KLDR_ERR_SYMBOL_NOT_FOUND (KLDR_ERR_BASE + 47)
1192/** A forward symbol was encountered but the caller didn't provide any means to resolve it. */
1193#define KLDR_ERR_FORWARDER_SYMBOL (KLDR_ERR_BASE + 48)
1194/** Encountered a bad fixup. */
1195#define KLDR_ERR_BAD_FIXUP (KLDR_ERR_BASE + 49)
1196/** A memory allocation failed. */
1197#define KLDR_ERR_NO_MEMORY (KLDR_ERR_BASE + 50)
1198/** The import ordinal was out of bounds. */
1199#define KLDR_ERR_IMPORT_ORDINAL_OUT_OF_BOUNDS (KLDR_ERR_BASE + 51)
1200/** A forwarder chain was too long. */
1201#define KLDR_ERR_TOO_LONG_FORWARDER_CHAIN (KLDR_ERR_BASE + 52)
1202/** The module has no debug info. */
1203#define KLDR_ERR_NO_DEBUG_INFO (KLDR_ERR_BASE + 53)
1204/** The module is already mapped.
1205 * kLdrModMap() can only be called once (without kLdrModUnmap() in between). */
1206#define KLDR_ERR_ALREADY_MAPPED (KLDR_ERR_BASE + 54)
1207/** The module was not mapped.
1208 * kLdrModUnmap() should not called without being preceeded by a kLdrModMap(). */
1209#define KLDR_ERR_NOT_MAPPED (KLDR_ERR_BASE + 55)
1210/** Couldn't fit the address value into the field. Typically a relocation kind of error. */
1211#define KLDR_ERR_ADDRESS_OVERFLOW (KLDR_ERR_BASE + 56)
1212/** Couldn't fit a calculated size value into the native size type of the host. */
1213#define KLDR_ERR_SIZE_OVERFLOW (KLDR_ERR_BASE + 57)
1214/** Thread attach failed. */
1215#define KLDR_ERR_THREAD_ATTACH_FAILED (KLDR_ERR_BASE + 58)
1216/** The file reader can't take more concurrent mappings. */
1217#define KLDR_ERR_TOO_MANY_MAPPINGS (KLDR_ERR_BASE + 59)
1218/** The module wasn't a DLL or object file. */
1219#define KLDR_ERR_NOT_DLL (KLDR_ERR_BASE + 60)
1220/** The module wasn't an EXE. */
1221#define KLDR_ERR_NOT_EXE (KLDR_ERR_BASE + 61)
1222/** Not implemented yet. */
1223#define KLDR_ERR_TODO (KLDR_ERR_BASE + 62)
1224
1225
1226/** @name kLdrModPE status codes
1227 * @{ */
1228#define KLDR_ERR_PE_BASE (KLDR_ERR_BASE + 63)
1229/** The machine isn't supported by the interpreter. */
1230#define KLDR_ERR_PE_UNSUPPORTED_MACHINE (KLDR_ERR_PE_BASE + 0)
1231/** The file handler isn't valid. */
1232#define KLDR_ERR_PE_BAD_FILE_HEADER (KLDR_ERR_PE_BASE + 1)
1233/** The the optional headers isn't valid. */
1234#define KLDR_ERR_PE_BAD_OPTIONAL_HEADER (KLDR_ERR_PE_BASE + 2)
1235/** One of the section headers aren't valid. */
1236#define KLDR_ERR_PE_BAD_SECTION_HEADER (KLDR_ERR_PE_BASE + 3)
1237/** Bad forwarder entry. */
1238#define KLDR_ERR_PE_BAD_FORWARDER (KLDR_ERR_PE_BASE + 4)
1239/** Forwarder module not found in the import descriptor table. */
1240#define KLDR_ERR_PE_FORWARDER_IMPORT_NOT_FOUND (KLDR_ERR_PE_BASE + 5)
1241/** Bad PE fixups. */
1242#define KLDR_ERR_PE_BAD_FIXUP (KLDR_ERR_PE_BASE + 6)
1243/** Bad PE import (thunk). */
1244#define KLDR_ERR_PE_BAD_IMPORT (KLDR_ERR_PE_BASE + 7)
1245/** @} */
1246
1247/** @name kLdrModLX status codes
1248 * @{ */
1249#define KLDR_ERR_LX_BASE (KLDR_ERR_PE_BASE + 8)
1250/** validation of LX header failed. */
1251#define KLDR_ERR_LX_BAD_HEADER (KLDR_ERR_LX_BASE + 0)
1252/** validation of the loader section (in the LX header) failed. */
1253#define KLDR_ERR_LX_BAD_LOADER_SECTION (KLDR_ERR_LX_BASE + 1)
1254/** validation of the fixup section (in the LX header) failed. */
1255#define KLDR_ERR_LX_BAD_FIXUP_SECTION (KLDR_ERR_LX_BASE + 2)
1256/** validation of the LX object table failed. */
1257#define KLDR_ERR_LX_BAD_OBJECT_TABLE (KLDR_ERR_LX_BASE + 3)
1258/** A bad page map entry was encountered. */
1259#define KLDR_ERR_LX_BAD_PAGE_MAP (KLDR_ERR_LX_BASE + 4)
1260/** Bad iterdata (EXEPACK) data. */
1261#define KLDR_ERR_LX_BAD_ITERDATA (KLDR_ERR_LX_BASE + 5)
1262/** Bad iterdata2 (EXEPACK2) data. */
1263#define KLDR_ERR_LX_BAD_ITERDATA2 (KLDR_ERR_LX_BASE + 6)
1264/** Bad bundle data. */
1265#define KLDR_ERR_LX_BAD_BUNDLE (KLDR_ERR_LX_BASE + 7)
1266/** No soname. */
1267#define KLDR_ERR_LX_NO_SONAME (KLDR_ERR_LX_BASE + 8)
1268/** Bad soname. */
1269#define KLDR_ERR_LX_BAD_SONAME (KLDR_ERR_LX_BASE + 9)
1270/** Bad forwarder entry. */
1271#define KLDR_ERR_LX_BAD_FORWARDER (KLDR_ERR_LX_BASE + 10)
1272/** internal fixup chain isn't implemented yet. */
1273#define KLDR_ERR_LX_NRICHAIN_NOT_SUPPORTED (KLDR_ERR_LX_BASE + 11)
1274/** @} */
1275
1276/** @name
1277 * @{ */
1278#define KLDR_ERR_MACHO_BASE (KLDR_ERR_LX_BASE + 12)
1279/** Only native endian Mach-O files are supported. */
1280#define KLDR_ERR_MACHO_OTHER_ENDIAN_NOT_SUPPORTED (KLDR_ERR_MACHO_BASE + 0)
1281/** 64-bit Mach-O files aren't supported yet. */
1282#define KLDR_ERR_MACHO_64BIT_NOT_SUPPORTED (KLDR_ERR_MACHO_BASE + 1)
1283/** The Mach-O header is bad or contains new and unsupported features. */
1284#define KLDR_ERR_MACHO_BAD_HEADER (KLDR_ERR_MACHO_BASE + 2)
1285/** The file type isn't supported. */
1286#define KLDR_ERR_MACHO_UNSUPPORTED_FILE_TYPE (KLDR_ERR_MACHO_BASE + 3)
1287/** The machine (cputype / cpusubtype combination) isn't supported. */
1288#define KLDR_ERR_MACHO_UNSUPPORTED_MACHINE (KLDR_ERR_MACHO_BASE + 4)
1289/** Bad load command(s). */
1290#define KLDR_ERR_MACHO_BAD_LOAD_COMMAND (KLDR_ERR_MACHO_BASE + 5)
1291/** Encountered an unknown load command.*/
1292#define KLDR_ERR_MACHO_UNKNOWN_LOAD_COMMAND (KLDR_ERR_MACHO_BASE + 6)
1293/** Encountered a load command that's not implemented.*/
1294#define KLDR_ERR_MACHO_UNSUPPORTED_LOAD_COMMAND (KLDR_ERR_MACHO_BASE + 7)
1295/** Bad section. */
1296#define KLDR_ERR_MACHO_BAD_SECTION (KLDR_ERR_MACHO_BASE + 8)
1297/** Encountered a section type that's not implemented.*/
1298#define KLDR_ERR_MACHO_UNSUPPORTED_SECTION (KLDR_ERR_MACHO_BASE + 9)
1299/** Encountered a section type that's not known to the loader. (probably invalid) */
1300#define KLDR_ERR_MACHO_UNKNOWN_SECTION (KLDR_ERR_MACHO_BASE + 10)
1301/** The sections aren't ordered by segment as expected by the loader. */
1302#define KLDR_ERR_MACHO_BAD_SECTION_ORDER (KLDR_ERR_MACHO_BASE + 11)
1303/** The image is 32-bit and contains 64-bit load commands or vise versa. */
1304#define KLDR_ERR_MACHO_BIT_MIX (KLDR_ERR_MACHO_BASE + 12)
1305/** Bad MH_OBJECT file. */
1306#define KLDR_ERR_MACHO_BAD_OBJECT_FILE (KLDR_ERR_MACHO_BASE + 13)
1307/** Bad symbol table entry. */
1308#define KLDR_ERR_MACHO_BAD_SYMBOL (KLDR_ERR_MACHO_BASE + 14)
1309/** Unsupported fixup type. */
1310#define KLDR_ERR_MACHO_UNSUPPORTED_FIXUP_TYPE (KLDR_ERR_MACHO_BASE + 15)
1311/** @} */
1312
1313/** End of the valid kLdr status codes. */
1314#define KLDR_ERR_END (KLDR_ERR_MACHO_BASE + 16)
1315
1316const char *kLdrErrStr(int rc);
1317
1318/** @} */
1319
1320
1321#ifdef __cplusplus
1322}
1323#endif
1324
1325#endif
1326
Note: See TracBrowser for help on using the repository browser.