source: trunk/kStuff/include/k/kLdr.h@ 3570

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

Moving kLdr.h.

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