source: trunk/kLdr/kLdr.h@ 2893

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

interface adjustment (in progress).

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