source: trunk/kLdr/kLdr.h@ 2961

Last change on this file since 2961 was 2961, checked in by bird, 19 years ago

Some more work in the GetBits and Relocate area.

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