source: trunk/kLdr/kLdr.h@ 2954

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

implemented kldrModMachOPreParseLoadCommands

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