source: trunk/kLdr/kLdr.h@ 2958

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

Relocations (generic only - x86 is generic).

  • Property svn:keywords set to Id
File size: 55.0 KB
Line 
1/* $Id: kLdr.h 2958 2007-02-09 05:12:22Z 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/** @def KLDR_LITTLE_ENDIAN
546 * The kLdr build is for a little endian target. */
547/** @def KLDR_BIG_ENDIAN
548 * The kLdr build is for a big endian target. */
549#if !defined(KLDR_LITTLE_ENDIAN) && !defined(KLDR_BIG_ENDIAN)
550# define KLDR_LITTLE_ENDIAN
551#endif
552#ifdef __DOXYGEN__
553# define KLDR_BIG_ENDIAN
554#endif
555
556
557/** Pointer to a module interpreter method table. */
558typedef struct KLDRMODOPS *PKLDRMODOPS;
559/** Pointer to const module interpreter methods table. */
560typedef const struct KLDRMODOPS *PCKLDRMODOPS;
561
562/**
563 * Module interpreter instance.
564 * All members are read only unless you're kLdrMod or the module interpreter.
565 */
566typedef struct KLDRMOD
567{
568 /** Magic number (KLDRMOD_MAGIC). */
569 uint32_t u32Magic;
570 /** The format of this module. */
571 KLDRFMT enmFmt;
572 /** The type of module. */
573 KLDRTYPE enmType;
574 /** The architecture this module was built for. */
575 KLDRARCH enmArch;
576 /** The minium cpu this module was built for.
577 * This might not be accurate, so use kLdrModCanExecuteOn() to check. */
578 KLDRARCH enmCpu;
579 /** The endian used by the module. */
580 KLDRENDIAN enmEndian;
581 /** The filename length (bytes). */
582 uint32_t cchFilename;
583 /** The filename. */
584 const char *pszFilename;
585 /** The module name. */
586 const char *pszName;
587 /** The module name length (bytes). */
588 uint32_t cchName;
589 /** The number of segments in the module. */
590 uint32_t cSegments;
591 /** Pointer to the loader methods.
592 * Not meant for calling directly thru! */
593 PCKLDRMODOPS pOps;
594 /** Pointer to the read instance. (Can be NULL after kLdrModDone().)*/
595 PKLDRRDR pRdr;
596 /** The module data. */
597 void *pvData;
598 /** Segments. (variable size, can be zero) */
599 KLDRSEG aSegments[1];
600} KLDRMOD, *PKLDRMOD, **PPKLDRMOD;
601
602/** The magic for KLDRMOD::u32Magic. (Kosuke Fujishima) */
603#define KLDRMOD_MAGIC 0x19640707
604
605
606/** Special base address value alias for the link address. */
607#define KLDRMOD_BASEADDRESS_LINK (~(KLDRADDR)1)
608/** Special base address value alias for the actual load address (must be mapped). */
609#define KLDRMOD_BASEADDRESS_MAP (~(KLDRADDR)2)
610
611/** Special import module ordinal value used to indicate that there is no
612 * specific module associated with the requested symbol. */
613#define NIL_KLDRMOD_IMPORT (~(uint32_t)0)
614
615/** Special symbol ordinal value used to indicate that the symbol
616 * only has a string name. */
617#define NIL_KLDRMOD_SYM_ORDINAL (~(uint32_t)0)
618
619
620/** @name Load symbol kind flags.
621 * @{ */
622/** The bitness doesn't matter. */
623#define KLDRSYMKIND_NO_BIT 0x00000000
624/** 16-bit symbol. */
625#define KLDRSYMKIND_16BIT 0x00000001
626/** 32-bit symbol. */
627#define KLDRSYMKIND_32BIT 0x00000002
628/** 64-bit symbol. */
629#define KLDRSYMKIND_64BIT 0x00000003
630/** Mask out the bit.*/
631#define KLDRSYMKIND_BIT_MASK 0x00000003
632/** We don't know the type of symbol. */
633#define KLDRSYMKIND_NO_TYPE 0x00000000
634/** The symbol is a code object (method/function/procedure/whateveryouwannacallit). */
635#define KLDRSYMKIND_CODE 0x00000010
636/** The symbol is a data object. */
637#define KLDRSYMKIND_DATA 0x00000020
638/** Mask out the symbol type. */
639#define KLDRSYMKIND_TYPE_MASK 0x00000030
640/** Valid symbol kind mask. */
641#define KLDRSYMKIND_MASK 0x00000033
642/** Weak symbol. */
643#define KLDRSYMKIND_WEAK 0x00000100
644/** Forwarder symbol. */
645#define KLDRSYMKIND_FORWARDER 0x00000200
646/** Request a flat symbol address. */
647#define KLDRSYMKIND_REQ_FLAT 0x00000000
648/** Request a segmented symbol address. */
649#define KLDRSYMKIND_REQ_SEGMENTED 0x40000000
650/** @} */
651
652/** @name kLdrModEnumSymbols flags.
653 * @{ */
654/** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
655#define KLDRMOD_ENUM_SYMS_FLAGS_ALL 0x00000001
656/** @} */
657
658
659/**
660 * Callback for resolving imported symbols when applying fixups.
661 *
662 * @returns 0 on success and *pValue and *pfKind filled.
663 * @returns Non-zero OS specific or kLdr status code on failure.
664 *
665 * @param pMod The module which fixups are begin applied.
666 * @param iImport The import module ordinal number or NIL_KLDRMOD_IMPORT.
667 * @param iSymbol The symbol ordinal number or NIL_KLDRMOD_SYM_ORDINAL.
668 * @param pchSymbol The symbol name. Can be NULL if iSymbol isn't nil. Doesn't have to be null-terminated.
669 * @param cchSymbol The length of the symbol.
670 * @param pszVersion The symbol version. NULL if not versioned.
671 * @param puValue Where to store the symbol value.
672 * @param pfKind Where to store the symbol kind flags.
673 * @param pvUser The user parameter specified to the relocation function.
674 */
675typedef int FNKLDRMODGETIMPORT(PKLDRMOD pMod, uint32_t iImport, uint32_t iSymbol, const char *pchSymbol, size_t cchSymbol,
676 const char *pszVersion, PKLDRADDR puValue, uint32_t *pfKind, void *pvUser);
677/** Pointer to a import callback. */
678typedef FNKLDRMODGETIMPORT *PFNKLDRMODGETIMPORT;
679
680/**
681 * Symbol enumerator callback.
682 *
683 * @returns 0 if enumeration should continue.
684 * @returns non-zero if the enumeration should stop. This status code will then be returned by kLdrModEnumSymbols().
685 *
686 * @param pMod The module which symbols are being enumerated.s
687 * @param iSymbol The symbol ordinal number or NIL_KLDRMOD_SYM_ORDINAL.
688 * @param pchSymbol The symbol name. This can be NULL if there is a symbol ordinal.
689 * This can also be an empty string if the symbol doesn't have a name
690 * or it's name has been stripped.
691 * Important, this doesn't have to be a null-terminated string.
692 * @param cchSymbol The length of the symbol.
693 * @param pszVersion The symbol version. NULL if not versioned.
694 * @param uValue The symbol value.
695 * @param fKind The symbol kind flags.
696 * @param pvUser The user parameter specified to kLdrModEnumSymbols().
697 */
698typedef int FNKLDRMODENUMSYMS(PKLDRMOD pMod, uint32_t iSymbol, const char *pchSymbol, size_t cchSymbol, const char *pszVersion,
699 KLDRADDR uValue, uint32_t fKind, void *pvUser);
700/** Pointer to a symbol enumerator callback. */
701typedef FNKLDRMODENUMSYMS *PFNKLDRMODENUMSYMS;
702
703/**
704 * Debug info enumerator callback.
705 *
706 * @returns 0 to continue the enumeration.
707 * @returns non-zero if the enumeration should stop. This status code will then be returned by kLdrModEnumDbgInfo().
708 *
709 * @param pMod The module.
710 * @param iDbgInfo The debug info ordinal number / id.
711 * @param enmType The debug info type.
712 * @param iMajorVer The major version number of the debug info format. -1 if unknow - implies invalid iMinorVer.
713 * @param iMinorVer The minor version number of the debug info format. -1 when iMajorVer is -1.
714 * @param offFile The file offset *if* this type has one specific location in the executable image file.
715 * This is -1 if there isn't any specific file location.
716 * @param LinkAddress The link address of the debug info if it's loadable. NIL_KLDRADDR if not loadable.
717 * @param cb The size of the debug information. -1 is used if this isn't applicable.
718 * @param pszExtFile This points to the name of an external file containing the debug info.
719 * This is NULL if there isn't any external file.
720 * @param pvUser The user parameter specified to kLdrModEnumDbgInfo.
721 */
722typedef int FNKLDRENUMDBG(PKLDRMOD pMod, uint32_t iDbgInfo, KLDRDBGINFOTYPE enmType, int16_t iMajorVer, int16_t iMinorVer,
723 off_t offFile, KLDRADDR LinkAddress, KLDRSIZE cb, const char *pszExtFile, void *pvUser);
724/** Pointer to a debug info enumerator callback. */
725typedef FNKLDRENUMDBG *PFNKLDRENUMDBG;
726
727/**
728 * Resource enumerator callback.
729 *
730 * @returns 0 to continue the enumeration.
731 * @returns non-zero if the enumeration should stop. This status code will then be returned by kLdrModEnumResources().
732 *
733 * @param pMod The module.
734 * @param idType The resource type id. NIL_KLDRMOD_RSRC_TYPE_ID if no type id.
735 * @param pszType The resource type name. NULL if no type name.
736 * @param idName The resource id. NIL_KLDRMOD_RSRC_NAME_ID if no id.
737 * @param pszName The resource name. NULL if no name.
738 * @param idLang The language id.
739 * @param AddrRsrc The address value for the resource.
740 * @param cbRsrc The size of the resource.
741 * @param pvUser The user parameter specified to kLdrModEnumDbgInfo.
742 */
743typedef int FNKLDRENUMRSRC(PKLDRMOD pMod, uint32_t idType, const char *pszType, uint32_t idName, const char *pszName,
744 uint32_t idLang, KLDRADDR AddrRsrc, KLDRSIZE cbRsrc, void *pvUser);
745/** Pointer to a resource enumerator callback. */
746typedef FNKLDRENUMRSRC *PFNKLDRENUMRSRC;
747
748/** NIL resource name ID. */
749#define NIL_KLDRMOD_RSRC_NAME_ID ( ~(uint32_t)0 )
750/** NIL resource type ID. */
751#define NIL_KLDRMOD_RSRC_TYPE_ID ( ~(uint32_t)0 )
752/** @name Language ID
753 *
754 * Except for the special IDs #defined here, the values are considered
755 * format specific for now since it's only used by the PE resources.
756 *
757 * @{ */
758/** NIL language ID. */
759#define NIL_KLDR_LANG_ID ( ~(uint32_t)0 )
760/** Special language id value for matching any language. */
761#define KLDR_LANG_ID_ANY ( ~(uint32_t)1 )
762/** Special language id value indicating language neutral. */
763#define KLDR_LANG_ID_NEUTRAL ( ~(uint32_t)2 )
764/** Special language id value indicating user default language. */
765#define KLDR_LANG_ID_USER_DEFAULT ( ~(uint32_t)3 )
766/** Special language id value indicating system default language. */
767#define KLDR_LANG_ID_SYS_DEFAULT ( ~(uint32_t)4 )
768/** Special language id value indicating default custom locale. */
769#define KLDR_LANG_ID_CUSTOM_DEFAULT ( ~(uint32_t)5 )
770/** Special language id value indicating unspecified custom locale. */
771#define KLDR_LANG_ID_CUSTOM_UNSPECIFIED ( ~(uint32_t)6 )
772/** Special language id value indicating default custom MUI locale. */
773#define KLDR_LANG_ID_UI_CUSTOM_DEFAULT ( ~(uint32_t)7 )
774/** @} */
775
776
777int kLdrModOpen(const char *pszFilename, PPKLDRMOD ppMod);
778int kLdrModOpenFromRdr(PKLDRRDR pRdr, PPKLDRMOD ppMod);
779int kLdrModOpenNative(const char *pszFilename, PPKLDRMOD ppMod);
780int kLdrModOpenNativeByHandle(uintptr_t uHandle, PPKLDRMOD ppMod);
781int kLdrModClose(PKLDRMOD pMod);
782
783int kLdrModQuerySymbol(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, uint32_t iSymbol,
784 const char *pchSymbol, size_t cchSymbol, const char *pszVersion,
785 PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, uint32_t *pfKind);
786int kLdrModEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress,
787 uint32_t fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser);
788int kLdrModGetImport(PKLDRMOD pMod, const void *pvBits, uint32_t iImport, char *pszName, size_t cchName);
789int32_t kLdrModNumberOfImports(PKLDRMOD pMod, const void *pvBits);
790int kLdrModCanExecuteOn(PKLDRMOD pMod, const void *pvBits, KLDRARCH enmArch, KLDRCPU enmCpu);
791int kLdrModGetStackInfo(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo);
792int kLdrModQueryMainEntrypoint(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress);
793int kLdrModQueryResource(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, uint32_t idType, const char *pszType,
794 uint32_t idName, const char *pszName, uint32_t idLang, PKLDRADDR pAddrRsrc, size_t *pcbRsrc);
795int kLdrModEnumResources(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, uint32_t idType, const char *pszType,
796 uint32_t idName, const char *pszName, uint32_t idLang, PFNKLDRENUMRSRC pfnCallback, void *pvUser);
797int kLdrModEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser);
798int kLdrModHasDbgInfo(PKLDRMOD pMod, const void *pvBits);
799int kLdrModMostlyDone(PKLDRMOD pMod);
800
801
802/** @name Operations On The Internally Managed Mapping
803 * @{ */
804int kLdrModMap(PKLDRMOD pMod);
805int kLdrModUnmap(PKLDRMOD pMod);
806int kLdrModAllocTLS(PKLDRMOD pMod);
807void kLdrModFreeTLS(PKLDRMOD pMod);
808int kLdrModReload(PKLDRMOD pMod);
809int kLdrModFixupMapping(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
810int kLdrModCallInit(PKLDRMOD pMod, uintptr_t uHandle);
811int kLdrModCallTerm(PKLDRMOD pMod, uintptr_t uHandle);
812int kLdrModCallThread(PKLDRMOD pMod, uintptr_t uHandle, unsigned fAttachingOrDetaching);
813/** @} */
814
815/** @name Operations On The Externally Managed Mappings
816 * @{ */
817KLDRADDR kLdrModSize(PKLDRMOD pMod);
818int kLdrModGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
819int kLdrModRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
820 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
821/** @} */
822
823
824/**
825 * The loader module operation.
826 */
827typedef struct KLDRMODOPS
828{
829 /** The name of this module interpreter. */
830 const char *pszName;
831 /** Pointer to the next module interpreter. */
832 PCKLDRMODOPS pNext;
833
834 /**
835 * Create a loader module instance interpreting the executable image found
836 * in the specified file provider instance.
837 *
838 * @returns 0 on success and *ppMod pointing to a module instance.
839 * On failure, a non-zero OS specific error code is returned.
840 * @param pOps Pointer to the registered method table.
841 * @param pRdr The file provider instance to use.
842 * @param offNewHdr The offset of the new header in MZ files. -1 if not found.
843 * @param ppMod Where to store the module instance pointer.
844 */
845 int (* pfnCreate)(PCKLDRMODOPS pOps, PKLDRRDR pRdr, off_t offNewHdr, PPKLDRMOD ppMod);
846 /**
847 * Destroys an loader module instance.
848 *
849 * The caller is responsible for calling kLdrModUnmap() and kLdrFreeTLS() first.
850 *
851 * @returns 0 on success, non-zero on failure. The module instance state
852 * is unknown on failure, it's best not to touch it.
853 * @param pMod The module.
854 */
855 int (* pfnDestroy)(PKLDRMOD pMod);
856
857 /** @copydoc kLdrModQuerySymbol */
858 int (* pfnQuerySymbol)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, uint32_t iSymbol,
859 const char *pchSymbol, size_t cchSymbol, const char *pszVersion,
860 PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, uint32_t *pfKind);
861 /** @copydoc kLdrModEnumSymbols */
862 int (* pfnEnumSymbols)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, uint32_t fFlags,
863 PFNKLDRMODENUMSYMS pfnCallback, void *pvUser);
864 /** @copydoc kLdrModGetImport */
865 int (* pfnGetImport)(PKLDRMOD pMod, const void *pvBits, uint32_t iImport, char *pszName, size_t cchName);
866 /** @copydoc kLdrModNumberOfImports */
867 int32_t (* pfnNumberOfImports)(PKLDRMOD pMod, const void *pvBits);
868 /** @copydoc kLdrModCanExecuteOn */
869 int (* pfnCanExecuteOn)(PKLDRMOD pMod, const void *pvBits, KLDRARCH enmArch, KLDRCPU enmCpu);
870 /** @copydoc kLdrModGetStackInfo */
871 int (* pfnGetStackInfo)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo);
872 /** @copydoc kLdrModQueryMainEntrypoint */
873 int (* pfnQueryMainEntrypoint)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress);
874 /** @copydoc kLdrModQueryResource */
875 int (* pfnQueryResource)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, uint32_t idType, const char *pszType,
876 uint32_t idName, const char *pszName, uint32_t idLang, PKLDRADDR pAddrRsrc, size_t *pcbRsrc);
877 /** @copydoc kLdrModEnumResources */
878 int (* pfnEnumResources)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, uint32_t idType, const char *pszType,
879 uint32_t idName, const char *pszName, uint32_t idLang, PFNKLDRENUMRSRC pfnCallback, void *pvUser);
880 /** @copydoc kLdrModEnumDbgInfo */
881 int (* pfnEnumDbgInfo)(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser);
882 /** @copydoc kLdrModHasDbgInfo */
883 int (* pfnHasDbgInfo)(PKLDRMOD pMod, const void *pvBits);
884 /** @copydoc kLdrModMap */
885 int (* pfnMap)(PKLDRMOD pMod);
886 /** @copydoc kLdrModUnmap */
887 int (* pfnUnmap)(PKLDRMOD pMod);
888 /** @copydoc kLdrModAllocTLS */
889 int (* pfnAllocTLS)(PKLDRMOD pMod);
890 /** @copydoc kLdrModFreeTLS */
891 void (* pfnFreeTLS)(PKLDRMOD pMod);
892 /** @copydoc kLdrModReload */
893 int (* pfnReload)(PKLDRMOD pMod);
894 /** @copydoc kLdrModFixupMapping */
895 int (* pfnFixupMapping)(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
896 /** @copydoc kLdrModCallInit */
897 int (* pfnCallInit)(PKLDRMOD pMod, uintptr_t uHandle);
898 /** @copydoc kLdrModCallTerm */
899 int (* pfnCallTerm)(PKLDRMOD pMod, uintptr_t uHandle);
900 /** @copydoc kLdrModCallThread */
901 int (* pfnCallThread)(PKLDRMOD pMod, uintptr_t uHandle, unsigned fAttachingOrDetaching);
902 /** @copydoc kLdrModSize */
903 KLDRADDR (* pfnSize)(PKLDRMOD pMod);
904 /** @copydoc kLdrModGetBits */
905 int (* pfnGetBits)(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
906 /** @copydoc kLdrModRelocateBits */
907 int (* pfnRelocateBits)(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
908 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
909 /** @copydoc kLdrModMostlyDone */
910 int (* pfnMostlyDone)(PKLDRMOD pMod);
911 /** Dummy which should be assigned a non-zero value. */
912 uint32_t uEndOfStructure;
913} KLDRMODOPS;
914
915
916/** @} */
917
918
919
920
921/** @defgroup grp_kLdrDyld kLdrDyld - The dynamic loader
922 * @{ */
923
924/** The handle to a dynamic loader module. */
925typedef struct KLDRDYLDMOD *HKLDRMOD;
926/** Pointer to the handle to a dynamic loader module. */
927typedef HKLDRMOD *PHKLDRMOD;
928/** NIL handle value. */
929#define NIL_HKLDRMOD ((HKLDRMOD)0)
930
931
932/**
933 * File search method.
934 *
935 * In addition to it's own way of finding files, kLdr emulates
936 * the methods employed by the most popular systems.
937 */
938typedef enum KLDRDYLDSEARCH
939{
940 /** The usual invalid file search method. */
941 KLDRDYLD_SEARCH_INVALID = 0,
942 /** Uses the kLdr file search method.
943 * @todo invent me. */
944 KLDRDYLD_SEARCH_KLDR,
945 /** Use the emulation closest to the host system. */
946 KLDRDYLD_SEARCH_HOST,
947 /** Emulate the OS/2 file search method.
948 * On non-OS/2 systems, BEGINLIBPATH, LIBPATH, ENDLIBPATH and LIBPATHSTRICT are
949 * taken form the environment. */
950 KLDRDYLD_SEARCH_OS2,
951 /** Emulate the standard window file search method. */
952 KLDRDYLD_SEARCH_WINDOWS,
953 /** Emulate the alternative window file search method. */
954 KLDRDYLD_SEARCH_WINDOWS_ALTERED,
955 /** Emulate the most common UNIX file search method. */
956 KLDRDYLD_SEARCH_UNIX_COMMON,
957 /** End of the valid file search method values. */
958 KLDRDYLD_SEARCH_END,
959 /** Hack to blow the type up to 32-bit. */
960 KLDRDYLD_SEARCH_32BIT_HACK = 0x7fffffff
961} KLDRDYLDSEARCH;
962
963/** @name kLdrDyldLoad and kLdrDyldFindByName flags.
964 * @{ */
965/** The symbols in the module should be loaded into the global unix namespace.
966 * If not specified, the symbols are local and can only be referenced directly. */
967#define KLDRYDLD_LOAD_FLAGS_GLOBAL_SYMBOLS 0x00000001
968/** The symbols in the module should be loaded into the global unix namespace and
969 * it's symbols should take precedence over all currently loaded modules.
970 * This implies KLDRYDLD_LOAD_FLAGS_GLOBAL_SYMBOLS. */
971#define KLDRYDLD_LOAD_FLAGS_DEEP_SYMBOLS 0x00000002
972/** The module shouldn't be found by a global module search.
973 * If not specified, the module can be found by unspecified module searches,
974 * typical used when loading import/dep modules. */
975#define KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE 0x00000004
976/** Do a recursive initialization calls instead of defering them to the outermost call. */
977#define KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT 0x00000008
978/** We're loading the executable module.
979 * @internal */
980#define KLDRDYLD_LOAD_FLAGS_EXECUTABLE 0x40000000
981/** @} */
982
983
984int kLdrDyldLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
985 unsigned fFlags, PHKLDRMOD phMod, char *pszErr, size_t cchErr);
986int kLdrDyldUnload(HKLDRMOD hMod);
987int kLdrDyldFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
988 unsigned fFlags, PHKLDRMOD phMod);
989int kLdrDyldFindByAddress(uintptr_t Address, PHKLDRMOD phMod, uint32_t *piSegment, uintptr_t *poffSegment);
990int kLdrDyldGetName(HKLDRMOD hMod, char *pszName, size_t cchName);
991int kLdrDyldGetFilename(HKLDRMOD hMod, char *pszFilename, size_t cchFilename);
992int kLdrDyldQuerySymbol(HKLDRMOD hMod, uint32_t uSymbolOrdinal, const char *pszSymbolName,
993 const char *pszSymbolVersion, uintptr_t *pValue, uint32_t *pfKind);
994int kLdrDyldQueryResource(HKLDRMOD hMod, uint32_t idType, const char *pszType, uint32_t idName,
995 const char *pszName, uint32_t idLang, void **pvRsrc, size_t *pcbRsrc);
996int kLdrDyldEnumResources(HKLDRMOD hMod, uint32_t idType, const char *pszType, uint32_t idName,
997 const char *pszName, uint32_t idLang, PFNKLDRENUMRSRC pfnCallback, void *pvUser);
998
999/** @name OS/2 like API
1000 * @{ */
1001#if defined(__OS2__)
1002# define KLDROS2API _System
1003#else
1004# define KLDROS2API
1005#endif
1006int kLdrDosLoadModule(char *pszObject, size_t cbObject, const char *pszModule, PHKLDRMOD phMod);
1007int kLdrDosFreeModule(HKLDRMOD hMod);
1008int kLdrDosQueryModuleHandle(const char *pszModname, PHKLDRMOD phMod);
1009int kLdrDosQueryModuleName(HKLDRMOD hMod, size_t cchName, char *pszName);
1010int kLdrDosQueryProcAddr(HKLDRMOD hMod, uint32_t iOrdinal, const char *pszProcName, void **ppvProcAddr);
1011int kLdrDosQueryProcType(HKLDRMOD hMod, uint32_t iOrdinal, const char *pszProcName, uint32_t *pfProcType);
1012int kLdrDosQueryModFromEIP(PHKLDRMOD phMod, uint32_t *piObject, size_t cbName, char *pszName, uintptr_t *poffObject, uintptr_t ulEIP);
1013int kLdrDosReplaceModule(const char *pszOldModule, const char *pszNewModule, const char *pszBackupModule);
1014int kLdrDosGetResource(HKLDRMOD hMod, uint32_t idType, uint32_t idName, void **pvResAddr);
1015int kLdrDosQueryResourceSize(HKLDRMOD hMod, uint32_t idType, uint32_t idName, uint32_t *pcb);
1016int kLdrDosFreeResource(void *pvResAddr);
1017/** @} */
1018
1019/** @name POSIX like API
1020 * @{ */
1021HKLDRMOD kLdrDlOpen(const char *pszLibrary, int fFlags);
1022const char *kLdrDlError(void);
1023void * kLdrDlSym(HKLDRMOD hMod, const char *pszSymbol);
1024int kLdrDlClose(HKLDRMOD hMod);
1025/** @todo GNU extensions */
1026/** @} */
1027
1028/** @name Win32 like API
1029 * @{ */
1030#if defined(_MSC_VER)
1031# define KLDRWINAPI __stdcall
1032#else
1033# define KLDRWINAPI
1034#endif
1035HKLDRMOD KLDRWINAPI kLdrWLoadLibrary(const char *pszFilename);
1036HKLDRMOD KLDRWINAPI kLdrWLoadLibraryEx(const char *pszFilename, void *hFileReserved, uint32_t fFlags);
1037uint32_t KLDRWINAPI kLdrWGetModuleFileName(HKLDRMOD hMod, char *pszModName, size_t cchModName);
1038HKLDRMOD KLDRWINAPI kLdrWGetModuleHandle(const char *pszFilename);
1039int KLDRWINAPI kLdrWGetModuleHandleEx(uint32_t fFlags, const char *pszFilename, HKLDRMOD hMod);
1040void * KLDRWINAPI kLdrWGetProcAddress(HKLDRMOD hMod, const char *pszProcName);
1041uint32_t KLDRWINAPI kLdrWGetDllDirectory(size_t cchDir, char *pszDir);
1042int KLDRWINAPI kLdrWSetDllDirectory(const char *pszDir);
1043int KLDRWINAPI kLdrWFreeLibrary(HKLDRMOD hMod);
1044int KLDRWINAPI kLdrWDisableThreadLibraryCalls(HKLDRMOD hMod);
1045
1046/** The handle to a resource that's been found. */
1047typedef struct KLDRWRSRCFOUND *HKLDRWRSRCFOUND;
1048/** The handle to a loaded resource. */
1049typedef struct KLDRWRSRCLOADED *HKLDRWRSRCLOADED;
1050HKLDRWRSRCFOUND KLDRWINAPI kLdrWFindResource(HKLDRMOD hMod, const char *pszType, const char *pszName);
1051HKLDRWRSRCFOUND KLDRWINAPI kLdrWFindResourceEx(HKLDRMOD hMod, const char *pszType, const char *pszName, uint16_t idLang);
1052uint32_t KLDRWINAPI kLdrWSizeofResource(HKLDRMOD hMod, HKLDRWRSRCFOUND hFoundRsrc);
1053HKLDRWRSRCLOADED KLDRWINAPI kLdrWLoadResource(HKLDRMOD hMod, HKLDRWRSRCFOUND hFoundRsrc);
1054void *KLDRWINAPI kLdrWLockResource(HKLDRMOD hMod, HKLDRWRSRCLOADED hLoadedRsrc);
1055int KLDRWINAPI kLdrWFreeResource(HKLDRMOD hMod, HKLDRWRSRCLOADED hLoadedRsrc);
1056
1057typedef int (KLDRWINAPI *PFNKLDRWENUMRESTYPE)(HKLDRMOD hMod, const char *pszType, uintptr_t uUser);
1058int KLDRWINAPI kLdrWEnumResourceTypes(HKLDRMOD hMod, PFNKLDRWENUMRESTYPE pfnEnum, uintptr_t uUser);
1059int KLDRWINAPI kLdrWEnumResourceTypesEx(HKLDRMOD hMod, PFNKLDRWENUMRESTYPE pfnEnum, uintptr_t uUser, uint32_t fFlags, uint16_t idLang);
1060
1061typedef int (KLDRWINAPI *PFNKLDRWENUMRESNAME)(HKLDRMOD hMod, const char *pszType, char *pszName, uintptr_t uUser);
1062int KLDRWINAPI kLdrWEnumResourceNames(HKLDRMOD hMod, const char *pszType, PFNKLDRWENUMRESNAME pfnEnum, uintptr_t uUser);
1063int KLDRWINAPI kLdrWEnumResourceNamesEx(HKLDRMOD hMod, const char *pszType, PFNKLDRWENUMRESNAME pfnEnum, uintptr_t uUser, uint32_t fFlags, uint16_t idLang);
1064
1065typedef int (KLDRWINAPI *PFNKLDRWENUMRESLANG)(HKLDRMOD hMod, const char *pszType, const char *pszName, uint16_t idLang, uintptr_t uUser);
1066int KLDRWINAPI kLdrWEnumResourceLanguages(HKLDRMOD hMod, const char *pszType, const char *pszName, PFNKLDRWENUMRESLANG pfnEnum, uintptr_t uUser);
1067int KLDRWINAPI kLdrWEnumResourceLanguagesEx(HKLDRMOD hMod, const char *pszType, const char *pszName,
1068 PFNKLDRWENUMRESLANG pfnEnum, uintptr_t uUser, uint32_t fFlags, uint16_t idLang);
1069/** @} */
1070
1071
1072/** @name Process Bootstrapping
1073 * @{ */
1074
1075/**
1076 * Argument package from the stub.
1077 */
1078typedef struct KLDREXEARGS
1079{
1080 /** Load & search flags, some which will become defaults. */
1081 uint32_t fFlags;
1082 /** The default search method. */
1083 KLDRDYLDSEARCH enmSearch;
1084 /** The executable file that the stub is supposed to load. */
1085 char szExecutable[260];
1086 /** The default prefix used when searching for DLLs. */
1087 char szDefPrefix[16];
1088 /** The default suffix used when searching for DLLs. */
1089 char szDefSuffix[16];
1090 /** The LD_LIBRARY_PATH prefix for the process.. */
1091 char szLibPath[4096 - sizeof(uint32_t) - sizeof(KLDRDYLDSEARCH) - 16 - 16 - 260];
1092} KLDREXEARGS, *PKLDREXEARGS;
1093/** Pointer to a const argument package from the stub. */
1094typedef const KLDREXEARGS *PCKLDREXEARGS;
1095
1096void kLdrLoadExe(PCKLDREXEARGS pArgs, void *pvOS);
1097
1098/** @} */
1099
1100/** @} */
1101
1102
1103/** @defgroup grp_kLdrErr kLdr Status Codes
1104 * kLdr uses a mix of native status codes and it's own status codes.
1105 * A status code of 0 means success, all other status codes means failure.
1106 * @{
1107 */
1108#ifdef __OS2__
1109# define KLDR_ERR_BASE 420000
1110#elif defined(__WIN__)
1111# define KLDR_ERR_BASE 420000
1112#else
1113# error "port me"
1114#endif
1115/** The image format is unknown. */
1116#define KLDR_ERR_UNKNOWN_FORMAT (KLDR_ERR_BASE + 0)
1117/** The MZ image format isn't supported by this kLdr build. */
1118#define KLDR_ERR_MZ_NOT_SUPPORTED (KLDR_ERR_BASE + 1)
1119/** The NE image format isn't supported by this kLdr build. */
1120#define KLDR_ERR_NE_NOT_SUPPORTED (KLDR_ERR_BASE + 2)
1121/** The LX image format isn't supported by this kLdr build. */
1122#define KLDR_ERR_LX_NOT_SUPPORTED (KLDR_ERR_BASE + 3)
1123/** The LE image format isn't supported by this kLdr build. */
1124#define KLDR_ERR_LE_NOT_SUPPORTED (KLDR_ERR_BASE + 4)
1125/** The PE image format isn't supported by this kLdr build. */
1126#define KLDR_ERR_PE_NOT_SUPPORTED (KLDR_ERR_BASE + 5)
1127/** The ELF image format isn't supported by this kLdr build. */
1128#define KLDR_ERR_ELF_NOT_SUPPORTED (KLDR_ERR_BASE + 6)
1129/** The mach-o image format isn't supported by this kLdr build. */
1130#define KLDR_ERR_MACHO_NOT_SUPPORTED (KLDR_ERR_BASE + 7)
1131/** The FAT image format isn't supported by this kLdr build or
1132 * a direct open was attempt without going thru the FAT file provider.
1133 * FAT images are also known as Universal Binaries. */
1134#define KLDR_ERR_FAT_NOT_SUPPORTED (KLDR_ERR_BASE + 8)
1135/** The a.out image format isn't supported by this kLdr build. */
1136#define KLDR_ERR_AOUT_NOT_SUPPORTED (KLDR_ERR_BASE + 9)
1137
1138/** Invalid parameter to a kLdr API. */
1139#define KLDR_ERR_INVALID_PARAMETER (KLDR_ERR_BASE + 32)
1140/** Invalid handle parameter to a kLdr API. */
1141#define KLDR_ERR_INVALID_HANDLE (KLDR_ERR_BASE + 33)
1142/** The module wasn't loaded dynamically. */
1143#define KLDR_ERR_NOT_LOADED_DYNAMICALLY (KLDR_ERR_BASE + 34)
1144/** The module wasn't found. */
1145#define KLDR_ERR_MODULE_NOT_FOUND (KLDR_ERR_BASE + 35)
1146/** A prerequisit module wasn't found. */
1147#define KLDR_ERR_PREREQUISITE_MODULE_NOT_FOUND (KLDR_ERR_BASE + 36)
1148/** The module is being terminated and can therefore not be loaded. */
1149#define KLDR_ERR_MODULE_TERMINATING (KLDR_ERR_BASE + 37)
1150/** A prerequisit module is being terminated and can therefore not be loaded. */
1151#define KLDR_ERR_PREREQUISITE_MODULE_TERMINATING (KLDR_ERR_BASE + 38)
1152/** The module initialization failed. */
1153#define KLDR_ERR_MODULE_INIT_FAILED (KLDR_ERR_BASE + 39)
1154/** The initialization of a prerequisite module failed. */
1155#define KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED (KLDR_ERR_BASE + 40)
1156/** The module has already failed initialization and can't be attempted reloaded until
1157 * after we've finished garbage collection. */
1158#define KLDR_ERR_MODULE_INIT_FAILED_ALREADY (KLDR_ERR_BASE + 41)
1159/** A prerequisite module has already failed initialization and can't be attempted
1160 * reloaded until after we've finished garbage collection. */
1161#define KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED_ALREADY (KLDR_ERR_BASE + 42)
1162/** Prerequisite recursed too deeply. */
1163#define KLDR_ERR_PREREQUISITE_RECURSED_TOO_DEEPLY (KLDR_ERR_BASE + 43)
1164/** Failed to allocate the main stack. */
1165#define KLDR_ERR_MAIN_STACK_ALLOC_FAILED (KLDR_ERR_BASE + 44)
1166/** Buffer overflow. */
1167#define KLDR_ERR_BUFFER_OVERFLOW (KLDR_ERR_BASE + 45)
1168/** The specified ARCH+CPU isn't compatible with image. */
1169#define KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE (KLDR_ERR_BASE + 46)
1170/** Symbol not found. */
1171#define KLDR_ERR_SYMBOL_NOT_FOUND (KLDR_ERR_BASE + 47)
1172/** A forward symbol was encountered but the caller didn't provide any means to resolve it. */
1173#define KLDR_ERR_FORWARDER_SYMBOL (KLDR_ERR_BASE + 48)
1174/** Encountered a bad fixup. */
1175#define KLDR_ERR_BAD_FIXUP (KLDR_ERR_BASE + 49)
1176/** A memory allocation failed. */
1177#define KLDR_ERR_NO_MEMORY (KLDR_ERR_BASE + 50)
1178/** The import ordinal was out of bounds. */
1179#define KLDR_ERR_IMPORT_ORDINAL_OUT_OF_BOUNDS (KLDR_ERR_BASE + 51)
1180/** A forwarder chain was too long. */
1181#define KLDR_ERR_TOO_LONG_FORWARDER_CHAIN (KLDR_ERR_BASE + 52)
1182/** The module has no debug info. */
1183#define KLDR_ERR_NO_DEBUG_INFO (KLDR_ERR_BASE + 53)
1184/** The module is already mapped.
1185 * kLdrModMap() can only be called once (without kLdrModUnmap() in between). */
1186#define KLDR_ERR_ALREADY_MAPPED (KLDR_ERR_BASE + 54)
1187/** The module was not mapped.
1188 * kLdrModUnmap() should not called without being preceeded by a kLdrModMap(). */
1189#define KLDR_ERR_NOT_MAPPED (KLDR_ERR_BASE + 55)
1190/** Couldn't fit the address value into the field. Typically a relocation kind of error. */
1191#define KLDR_ERR_ADDRESS_OVERFLOW (KLDR_ERR_BASE + 56)
1192/** Thread attach failed. */
1193#define KLDR_ERR_THREAD_ATTACH_FAILED (KLDR_ERR_BASE + 57)
1194/** The file reader can't take more concurrent mappings. */
1195#define KLDR_ERR_TOO_MANY_MAPPINGS (KLDR_ERR_BASE + 58)
1196/** The module wasn't a DLL or object file. */
1197#define KLDR_ERR_NOT_DLL (KLDR_ERR_BASE + 59)
1198/** The module wasn't an EXE. */
1199#define KLDR_ERR_NOT_EXE (KLDR_ERR_BASE + 60)
1200/** Not implemented yet. */
1201#define KLDR_ERR_TODO (KLDR_ERR_BASE + 61)
1202
1203
1204/** @name kLdrModPE status codes
1205 * @{ */
1206#define KLDR_ERR_PE_BASE (KLDR_ERR_BASE + 62)
1207/** The machine isn't supported by the interpreter. */
1208#define KLDR_ERR_PE_UNSUPPORTED_MACHINE (KLDR_ERR_PE_BASE + 0)
1209/** The file handler isn't valid. */
1210#define KLDR_ERR_PE_BAD_FILE_HEADER (KLDR_ERR_PE_BASE + 1)
1211/** The the optional headers isn't valid. */
1212#define KLDR_ERR_PE_BAD_OPTIONAL_HEADER (KLDR_ERR_PE_BASE + 2)
1213/** One of the section headers aren't valid. */
1214#define KLDR_ERR_PE_BAD_SECTION_HEADER (KLDR_ERR_PE_BASE + 3)
1215/** Bad forwarder entry. */
1216#define KLDR_ERR_PE_BAD_FORWARDER (KLDR_ERR_PE_BASE + 4)
1217/** Forwarder module not found in the import descriptor table. */
1218#define KLDR_ERR_PE_FORWARDER_IMPORT_NOT_FOUND (KLDR_ERR_PE_BASE + 5)
1219/** Bad PE fixups. */
1220#define KLDR_ERR_PE_BAD_FIXUP (KLDR_ERR_PE_BASE + 6)
1221/** Bad PE import (thunk). */
1222#define KLDR_ERR_PE_BAD_IMPORT (KLDR_ERR_PE_BASE + 7)
1223/** @} */
1224
1225/** @name kLdrModLX status codes
1226 * @{ */
1227#define KLDR_ERR_LX_BASE (KLDR_ERR_PE_BASE + 8)
1228/** validation of LX header failed. */
1229#define KLDR_ERR_LX_BAD_HEADER (KLDR_ERR_LX_BASE + 0)
1230/** validation of the loader section (in the LX header) failed. */
1231#define KLDR_ERR_LX_BAD_LOADER_SECTION (KLDR_ERR_LX_BASE + 1)
1232/** validation of the fixup section (in the LX header) failed. */
1233#define KLDR_ERR_LX_BAD_FIXUP_SECTION (KLDR_ERR_LX_BASE + 2)
1234/** validation of the LX object table failed. */
1235#define KLDR_ERR_LX_BAD_OBJECT_TABLE (KLDR_ERR_LX_BASE + 3)
1236/** A bad page map entry was encountered. */
1237#define KLDR_ERR_LX_BAD_PAGE_MAP (KLDR_ERR_LX_BASE + 4)
1238/** Bad iterdata (EXEPACK) data. */
1239#define KLDR_ERR_LX_BAD_ITERDATA (KLDR_ERR_LX_BASE + 5)
1240/** Bad iterdata2 (EXEPACK2) data. */
1241#define KLDR_ERR_LX_BAD_ITERDATA2 (KLDR_ERR_LX_BASE + 6)
1242/** Bad bundle data. */
1243#define KLDR_ERR_LX_BAD_BUNDLE (KLDR_ERR_LX_BASE + 7)
1244/** No soname. */
1245#define KLDR_ERR_LX_NO_SONAME (KLDR_ERR_LX_BASE + 8)
1246/** Bad soname. */
1247#define KLDR_ERR_LX_BAD_SONAME (KLDR_ERR_LX_BASE + 9)
1248/** Bad forwarder entry. */
1249#define KLDR_ERR_LX_BAD_FORWARDER (KLDR_ERR_LX_BASE + 10)
1250/** internal fixup chain isn't implemented yet. */
1251#define KLDR_ERR_LX_NRICHAIN_NOT_SUPPORTED (KLDR_ERR_LX_BASE + 11)
1252/** @} */
1253
1254/** @name
1255 * @{ */
1256#define KLDR_ERR_MACHO_BASE (KLDR_ERR_LX_BASE + 12)
1257/** Only native endian Mach-O files are supported. */
1258#define KLDR_ERR_MACHO_OTHER_ENDIAN_NOT_SUPPORTED (KLDR_ERR_MACHO_BASE + 0)
1259/** 64-bit Mach-O files aren't supported yet. */
1260#define KLDR_ERR_MACHO_64BIT_NOT_SUPPORTED (KLDR_ERR_MACHO_BASE + 1)
1261/** The Mach-O header is bad or contains new and unsupported features. */
1262#define KLDR_ERR_MACHO_BAD_HEADER (KLDR_ERR_MACHO_BASE + 2)
1263/** The file type isn't supported. */
1264#define KLDR_ERR_MACHO_UNSUPPORTED_FILE_TYPE (KLDR_ERR_MACHO_BASE + 3)
1265/** The machine (cputype / cpusubtype combination) isn't supported. */
1266#define KLDR_ERR_MACHO_UNSUPPORTED_MACHINE (KLDR_ERR_MACHO_BASE + 4)
1267/** Bad load command(s). */
1268#define KLDR_ERR_MACHO_BAD_LOAD_COMMAND (KLDR_ERR_MACHO_BASE + 5)
1269/** Encountered an unknown load command.*/
1270#define KLDR_ERR_MACHO_UNKNOWN_LOAD_COMMAND (KLDR_ERR_MACHO_BASE + 6)
1271/** Encountered a load command that's not implemented.*/
1272#define KLDR_ERR_MACHO_UNSUPPORTED_LOAD_COMMAND (KLDR_ERR_MACHO_BASE + 7)
1273/** Bad section. */
1274#define KLDR_ERR_MACHO_BAD_SECTION (KLDR_ERR_MACHO_BASE + 8)
1275/** Encountered a section type that's not implemented.*/
1276#define KLDR_ERR_MACHO_UNSUPPORTED_SECTION (KLDR_ERR_MACHO_BASE + 9)
1277/** Encountered a section type that's not known to the loader. (probably invalid) */
1278#define KLDR_ERR_MACHO_UNKNOWN_SECTION (KLDR_ERR_MACHO_BASE + 10)
1279/** The sections aren't ordered by segment as expected by the loader. */
1280#define KLDR_ERR_MACHO_BAD_SECTION_ORDER (KLDR_ERR_MACHO_BASE + 11)
1281/** The image is 32-bit and contains 64-bit load commands or vise versa. */
1282#define KLDR_ERR_MACHO_BIT_MIX (KLDR_ERR_MACHO_BASE + 12)
1283/** The bad MH_OBJECT file. */
1284#define KLDR_ERR_MACHO_BAD_OBJECT_FILE (KLDR_ERR_MACHO_BASE + 13)
1285/** @} */
1286
1287/** End of the valid kLdr status codes. */
1288#define KLDR_ERR_END (KLDR_ERR_MACHO_BASE + 14)
1289
1290const char *kLdrErrStr(int rc);
1291
1292/** @} */
1293
1294
1295#ifdef __cplusplus
1296}
1297#endif
1298
1299#endif
1300
Note: See TracBrowser for help on using the repository browser.