source: trunk/kLdr/kLdr.h@ 2951

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

working on LX.

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