source: trunk/kLdr/kLdr.h@ 2847

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

Roughly done with kldrDyldMod now.

  • Property svn:keywords set to Id
File size: 29.5 KB
Line 
1/* $Id: kLdr.h 2847 2006-11-01 19:17:21Z bird $ */
2/** @file
3 *
4 * kLdr - The Dynamic Loader.
5 *
6 * Copyright (c) 2006 knut st. osmundsen <bird@anduin.net>
7 *
8 *
9 * This file is part of kLdr.
10 *
11 * kLdr is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kLdr is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kLdr; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#ifndef __kLdr_h__
28#define __kLdr_h__
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/* kLdr depend on size_t, [u]intNN_t, [u]intptr_t and some related constants. */
35#include <sys/types.h>
36#include <stddef.h>
37#ifdef _MSC_VER
38typedef signed char int8_t;
39typedef unsigned char uint8_t;
40typedef signed short int16_t;
41typedef unsigned short uint16_t;
42typedef signed int int32_t;
43typedef unsigned int uint32_t;
44typedef signed __int64 int64_t;
45typedef unsigned __int64 uint64_t;
46typedef int64_t intmax_t;
47typedef uint64_t uintmax_t;
48#else
49# include <stdint.h>
50#endif
51
52
53/** @defgroup grp_kLdrRdr kLdrRdr - The file provider
54 * @{ */
55
56/**
57 * Memory Mapping Protections.
58 *
59 * @remark Shared segments can be mapped using the non copy-on-write variant.
60 * (Normally the copy-on-write variant is used because changes must
61 * be private and not shared with other processes mapping the file.)
62 */
63typedef enum KLDRPROT
64{
65 /** The usual invalid 0. */
66 KLDRPROT_INVALID = 0,
67 /** No access (page not present). */
68 KLDRPROT_NOACCESS,
69 /** Read only. */
70 KLDRPROT_READONLY,
71 /** Read & write. */
72 KLDRPROT_READWRITE,
73 /** Read & copy on write. */
74 KLDRPROT_WRITECOPY,
75 /** Execute only. */
76 KLDRPROT_EXECUTE,
77 /** Execute & read. */
78 KLDRPROT_EXECUTE_READ,
79 /** Execute, read & write. */
80 KLDRPROT_EXECUTE_READWRITE,
81 /** Execute, read & copy on write. */
82 KLDRPROT_EXECUTE_WRITECOPY,
83 /** The usual end value. (exclusive) */
84 KLDRPROT_END,
85 /** Blow the type up to 32-bits. */
86 KLDRPROT_32BIT_HACK = 0x7fffffff
87} KLDRPROT;
88
89
90/** Pointer to a file provider instance core. */
91typedef struct KLDRRDR *PKLDRRDR;
92/** Pointer to a file provider instance core pointer. */
93typedef struct KLDRRDR **PPKLDRRDR;
94
95/**
96 * File provider instance operations.
97 */
98typedef struct KLDRRDROPS
99{
100 /** The name of this file provider. */
101 const char *pszName;
102 /** Pointer to the next file provider. */
103 const struct KLDRRDROPS *pNext;
104
105 /** Try create a new file provider instance.
106 *
107 * @returns 0 on success, OS specific error code on failure.
108 * @param ppRdr Where to store the file provider instance.
109 * @param pszFilename The filename to open.
110 */
111 int (* pfnCreate)( PPKLDRRDR ppRdr, const char *pszFilename);
112 /** Destroy the file provider instance.
113 *
114 * @returns 0 on success, OS specific error code on failure.
115 * On failure, the file provider instance will be in an indeterminate state - don't touch it!
116 * @param pRdr The file provider instance.
117 */
118 int (* pfnDestroy)( PKLDRRDR pRdr);
119 /** Read bits from the file.
120 *
121 * @returns 0 on success, OS specific error code on failure.
122 * @param pRdr The file provider instance.
123 * @param pvBuf Where to put the bits.
124 * @param cb The number of bytes to read.
125 * @param off Where to start reading.
126 */
127 int (* pfnRead)( PKLDRRDR pRdr, void *pvBuf, size_t cb, off_t off);
128 /** Map all the file bits into memory (read only).
129 *
130 * @returns 0 on success, OS specific error code on failure.
131 * @param pRdr The file provider instance.
132 * @param ppvBits Where to store the address of the mapping.
133 * The size can be obtained using pfnSize.
134 */
135 int (* pfnAllMap)( PKLDRRDR pRdr, const void **ppvBits);
136 /** Unmap a file bits mapping obtained by KLDRRDROPS::pfnAllMap.
137 *
138 * @returns 0 on success, OS specific error code on failure.
139 * @param pRdr The file provider instance.
140 * @param pvBits The mapping address.
141 */
142 int (* pfnAllUnmap)(PKLDRRDR pRdr, const void *pvBits);
143 /** Get the file size.
144 *
145 * @returns The file size. Returns -1 on failure.
146 * @param pRdr The file provider instance.
147 */
148 off_t (* pfnSize)( PKLDRRDR pRdr);
149 /** Get the file pointer offset.
150 *
151 * @returns The file pointer offset. Returns -1 on failure.
152 * @param pRdr The file provider instance.
153 */
154 off_t (* pfnTell)( PKLDRRDR pRdr);
155 /** Get the file name.
156 *
157 * @returns The file size. Returns -1 on failure.
158 * @param pRdr The file provider instance.
159 */
160 const char * (* pfnName)(PKLDRRDR pRdr);
161 /**
162 * Prepares a memory region to map file sections into.
163 *
164 * @returns 0 on success, OS specific error code on failure.
165 * @param pRdr The file provider instance.
166 * @param ppv If fFixed is set, *ppv contains the memory location which
167 * the region should be based at. If fFixed is clear the OS
168 * is free to choose the location.
169 * On successful return *ppv contains address of the prepared
170 * memory region.
171 * @param cb The size of the memory region to prepare.
172 * @param fFixed When set *ppv will contain the desired region address.
173 *
174 */
175 int (* pfnPrepare)(PKLDRRDR pRdr, void **ppv, size_t cb, unsigned fFixed);
176 /**
177 * Maps a section of the file into the memory region reserved by pfnPrepare.
178 *
179 * @returns 0 on success, OS specific error code on failure.
180 * @param pRdr The file provider instance.
181 * @param pv The address in the prepared region.
182 * @param cb The size of the memory mapping.
183 * @param enmProt The desired memory protection.
184 * @param offFile The start of the raw file bytes.
185 * @param cbFile The number of raw file bytes. This must be less or equal to cb.
186 */
187 int (* pfnMap)(PKLDRRDR pRdr, void *pv, size_t cb, KLDRPROT enmProt, off_t offFile, size_t cbFile);
188 /**
189 * Changes the page protection of a section mapped using pfnMap.
190 *
191 * This is typically used for applying fixups and similar.
192 *
193 * @returns 0 on success, OS specific error code on failure.
194 * @param pRdr The file provider instance.
195 * @param pv The address passed to pfnMap.
196 * @param cb The size passed to pfnMap.
197 * @param enmProt The desired memory protection.
198 */
199 int (* pfnProtect)(PKLDRRDR pRdr, void *pv, size_t cb, KLDRPROT enmProt);
200 /**
201 * Unmaps a section of the file previously mapped using pfnMap.
202 *
203 * @returns 0 on success, OS specific error code on failure.
204 * @param pRdr The file provider instance.
205 * @param pv The address passed to pfnMap.
206 * @param cb The size passed to pfnMap.
207 */
208 int (* pfnUnmap)(PKLDRRDR pRdr, void *pv, size_t cb);
209 /**
210 * Releases the memory region prepared by pfnPrepare().
211 *
212 * Before calling this function, all sections mapped by pfnMap must first be unmapped by calling pfnUnmap.
213 *
214 * @returns 0 on success, OS specific error code on failure.
215 * @param pRdr The file provider instance.
216 * @param pv The address of the prepared region.
217 * @param cb The size of the prepared region.
218 */
219 int (* pfnUnprepare)(PKLDRRDR pRdr, void *pv, size_t cb);
220 /**
221 * We're done reading from the file but would like to keep file mappings.
222 *
223 * If the OS support closing the file handle while the file is mapped,
224 * the reader should do so.
225 *
226 * @param pRdr The file provider instance.
227 */
228 void (* pfnDone)(PKLDRRDR pRdr);
229 /** The usual non-zero dummy that makes sure we've initialized all members. */
230 uint32_t u32Dummy;
231} KLDRRDROPS;
232/** Pointer to file provider operations. */
233typedef KLDRRDROPS *PKLDRRDROPS;
234/** Pointer to const file provider operations. */
235typedef const KLDRRDROPS *PCKLDRRDROPS;
236
237
238/**
239 * File provider instance core.
240 */
241typedef struct KLDRRDR
242{
243 /** Pointer to the file provider operations. */
244 PCKLDRRDROPS pOps;
245} KLDRRDR;
246
247void kLdrRdrAddProvider(PKLDRRDROPS pAdd);
248
249int kLdrRdrOpen( PPKLDRRDR ppRdr, const char *pszFilename);
250int kLdrRdrClose( PKLDRRDR pRdr);
251int kLdrRdrRead( PKLDRRDR pRdr, void *pvBuf, size_t cb, off_t off);
252int kLdrRdrAllMap( PKLDRRDR pRdr, const void **ppvBits);
253int kLdrRdrAllUnmap(PKLDRRDR pRdr, const void *pvBits);
254off_t kLdrRdrSize( PKLDRRDR pRdr);
255off_t kLdrRdrTell( PKLDRRDR pRdr);
256const char *kLdrRdrName(PKLDRRDR pRdr);
257
258/** @} */
259
260
261
262/** @defgroup grp_kLdrMod kLdrMod - The executable image intepreter
263 * @{ */
264
265/**
266 * Stack information.
267 */
268typedef struct KLDRSTACKINFO
269{
270 /** The base address of the stack (sub) segment, link address.
271 * Set this to ~(uintmax_t)0 if the module doesn't include any stack (sub)segment. */
272 uintmax_t uLinkAddress;
273 /** The base address of the stack (sub) segment, actual load address.
274 * Set this to ~(uintmax_t)0 if the module doesn't include any stack (sub)segment or if
275 * the module isn't mapped (loaded) yet. */
276 uintmax_t uLoadAddress;
277 /** The stack size of the main thread.
278 * If no stack (sub)segment in the module, this is the stack size of the main thread.
279 * If the module doesn't contain this kind of information this field will be set to 0. */
280 uintmax_t cbStack;
281 /** The stack size of non-main threads.
282 * If the module doesn't contain this kind of information this field will be set to 0. */
283 uintmax_t cbStackThread;
284} KLDRSTACKINFO, *PKLDRSTACKINFO;
285
286
287/**
288 * Loader segment.
289 */
290typedef struct KLDRSEG
291{
292 /** Variable free to use for the kLdr user. */
293 void *pvUser;
294 /** The segment name. */
295 const char *pszName;
296 /** The size of the segment. */
297 uintmax_t cb;
298 /** The link time load address. */
299 uintmax_t LinkAddress;
300 /** The actual load address (if loaded). */
301 uintmax_t LoadAddress;
302 /** The segment protection. */
303 KLDRPROT enmProt;
304} KLDRSEG, *PKLDRSEG;
305
306
307/**
308 * Loader module format.
309 */
310typedef enum KLDRFMT
311{
312 /** The usual invalid 0 format. */
313 KLDRFMT_INVALID = 0,
314 /** The native OS loader. */
315 KLDRFMT_NATIVE,
316 /** The AOUT loader. */
317 KLDRFMT_AOUT,
318 /** The ELF loader. */
319 KLDRFMT_ELF,
320 /** The LX loader. */
321 KLDRFMT_LX,
322 /** The mach-o loader. */
323 KLDRFMT_MACHO,
324 /** The LX loader. */
325 KLDRFMT_PE,
326 /** The end of the valid format values (exclusive). */
327 KLDRFMT_END,
328 /** Hack to blow the type up to 32-bit. */
329 KLDRFMT_32BIT_HACK = 0x7fffffff
330} KLDRFMT;
331
332
333/**
334 * Loader module type.
335 */
336typedef enum KLDRTYPE
337{
338 /** The usual invalid 0 type. */
339 KLDRTYPE_INVALID = 0,
340 /** Object file. */
341 KLDRTYPE_OBJECT,
342 /** Executable module, fixed load address. */
343 KLDRTYPE_EXECUTABLE_FIXED,
344 /** Executable module, relocatable, non-fixed load address. */
345 KLDRTYPE_EXECUTABLE_RELOCATABLE,
346 /** Executable module, position independent code, non-fixed load address. */
347 KLDRTYPE_EXECUTABLE_PIC,
348 /** Shared library, fixed load address.
349 * Typically a system library. */
350 KLDRTYPE_SHARED_LIBRARY_FIXED,
351 /** Shared library, relocatable, non-fixed load address. */
352 KLDRTYPE_SHARED_LIBRARY_RELOCATABLE,
353 /** Shared library, position independent code, non-fixed load address. */
354 KLDRTYPE_SHARED_LIBRARY_PIC,
355 /** DLL that contains no code or data only imports and exports. (Chiefly OS/2.) */
356 KLDRTYPE_FORWARDER_DLL,
357 /** Core or dump. */
358 KLDRTYPE_CORE,
359 /** The end of the valid types values (exclusive). */
360 KLDRTYPE_END,
361 /** Hack to blow the type up to 32-bit. */
362 KLDRTYPE_32BIT_HACK = 0x7fffffff
363} KLDRTYPE;
364
365
366/**
367 * CPU Architecture.
368 * @todo Double check the non intel architectures.
369 */
370typedef enum KLDRARCH
371{
372 /** The usual invalid one. */
373 KLDRARCH_INVALID = 0,
374 /** Clone or Intel 16-bit x86. */
375 KLDRARCH_X86_16,
376 /** Clone or Intel 32-bit x86. */
377 KLDRARCH_X86_32,
378 /** AMD64 (including clones). */
379 KLDRARCH_AMD64,
380 /** Itanic (64-bit). */
381 KLDRARCH_IA64,
382 /** ALPHA (64-bit). */
383 KLDRARCH_ALPHA,
384 /** ALPHA limited to 32-bit. */
385 KLDRARCH_ALPHA_32,
386 /** 32-bit ARM. */
387 KLDRARCH_ARM_32,
388 /** 64-bit ARM. */
389 KLDRARCH_ARM_64,
390 /** 32-bit MIPS. */
391 KLDRARCH_MIPS_32,
392 /** 64-bit MIPS. */
393 KLDRARCH_MIPS_64,
394 /** 32-bit PowerPC. */
395 KLDRARCH_POWERPC_32,
396 /** 64-bit PowerPC. */
397 KLDRARCH_POWERPC_64,
398 /** 32-bit SPARC. */
399 KLDRARCH_SPARC_32,
400 /** 64-bit SPARC. */
401 KLDRARCH_SPARC_64,
402 /** The end of the valid architecture values (exclusive). */
403 KLDRARCH_END,
404 /** Hack to blow the type up to 32-bit. */
405 KLDRARCH_32BIT_HACK = 0x7fffffff
406} KLDRARCH;
407
408/**
409 * CPU models.
410 */
411typedef enum KLDRCPU
412{
413 /** The usual invalid cpu. */
414 KLDRCPU_INVALID = 0,
415 /** @name KLDRARCH_X86_16
416 * @{ */
417 KLDRCPU_I8086,
418 KLDRCPU_I8088,
419 KLDRCPU_I80186,
420 KLDRCPU_I80286,
421 KLDRCPU_I386_16,
422 KLDRCPU_I486_16,
423 KLDRCPU_I486SX_16,
424 KLDRCPU_I586_16,
425 KLDRCPU_I686_16,
426 KLDRCPU_K6_16,
427 KLDRCPU_K7_16,
428 KLDRCPU_K8_16,
429 /** @} */
430
431 /** @name KLDRARCH_X86_32
432 * @{ */
433 KLDRCPU_I386,
434 KLDRCPU_I486,
435 KLDRCPU_I486SX,
436 KLDRCPU_I586,
437 KLDRCPU_I686,
438 KLDRCPU_P4,
439 KLDRCPU_CORE2_32,
440 KLDRCPU_K6,
441 KLDRCPU_K7,
442 KLDRCPU_K8_32,
443 /** @} */
444
445 /** @name KLDRARCH_AMD64
446 * @{ */
447 KLDRCPU_K8,
448 KLDRCPU_P4_64,
449 KLDRCPU_CORE2,
450 /** @} */
451
452 /** The end of the valid cpu values (exclusive). */
453 KLDRCPU_END,
454 /** Hack to blow the type up to 32-bit. */
455 KLDRCPU_32BIT_HACK = 0x7fffffff
456} KLDRCPU;
457
458
459/**
460 * Loader endian indicator.
461 */
462typedef enum KLDRENDIAN
463{
464 /** The usual invalid endian. */
465 KLDRENDIAN_INVALID,
466 /** Little endian. */
467 KLDRENDIAN_LITTLE,
468 /** Bit endian. */
469 KLDRENDIAN_BIG,
470 /** Endianness doesn't have a meaning in the context. */
471 KLDRENDIAN_NA,
472 /** The end of the valid endian values (exclusive). */
473 KLDRENDIAN_END,
474 /** Hack to blow the type up to 32-bit. */
475 KLDRENDIAN_32BIT_HACK = 0x7fffffff
476} KLDRENDIAN;
477
478
479/**
480 * Loader module.
481 */
482typedef struct KLDRMOD
483{
484 /** Magic number. */
485 uint32_t u32Magic;
486 /** The format of this module. */
487 KLDRFMT enmFmt;
488 /** The type of module. */
489 KLDRTYPE enmType;
490 /** The architecture this module was built for. */
491 KLDRARCH enmArch;
492 /** The minium cpu this module was built for.
493 * This might not be accurate, so use kLdrModCanExecuteOn() to check. */
494 KLDRARCH enmCpu;
495 /** The endian used by the module. */
496 KLDRENDIAN enmEndian;
497 /** The filename length (bytes). */
498 uint32_t cchFilename;
499 /** The filename. */
500 const char *pszFilename;
501 /** The module name. */
502 const char *pszName;
503 /** The module name length (bytes). */
504 uint32_t cchName;
505 /** The number of segments in the module. */
506 uint32_t cSegments;
507 /** The module data. */
508 void *pvData;
509 /** Segments. (variable size, can be zero) */
510 KLDRSEG aSegments[1];
511} KLDRMOD, *PKLDRMOD, **PPKLDRMOD;
512
513
514/** Special base address value alias for the link address. */
515#define KLDRMOD_BASEADDRESS_LINK (~(uintmax_t)1)
516/** Special base address value alias for the actual load address (must be mapped). */
517#define KLDRMOD_BASEADDRESS_MAP (~(uintmax_t)2)
518
519/** @name Load symbol kind flags.
520 * @{ */
521/** The bitness doesn't matter. */
522#define KLDRSYMKIND_NO_BIT 0x00000000
523/** 16-bit symbol. */
524#define KLDRSYMKIND_16BIT 0x00000001
525/** 32-bit symbol. */
526#define KLDRSYMKIND_32BIT 0x00000002
527/** 64-bit symbol. */
528#define KLDRSYMKIND_64BIT 0x00000003
529/** Mask out the bit.*/
530#define KLDRSYMKIND_BIT_MASK 0x00000003
531/** We don't know the type of symbol. */
532#define KLDRSYMKIND_NO_TYPE 0x00000000
533/** The symbol is a code object (method/function/procedure/whateveryouwannacallit). */
534#define KLDRSYMKIND_CODE 0x00000010
535/** The symbol is a data object. */
536#define KLDRSYMKIND_DATA 0x00000020
537/** Mask out the symbol type. */
538#define KLDRSYMKIND_TYPE_MASK 0x00000030
539/** Valid symbol kind mask. */
540#define KLDRSYMKIND_MASK 0x00000033
541/** @} */
542
543/** @name kLdrModEnumSymbols flags.
544 * @{ */
545/** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
546#define KLDRMOD_ENUM_SYMBOL_FLAGS_ALL 0x00000001
547/** @} */
548
549
550typedef int FNKLDRMODGETIMPORT(PKLDRMOD pMod, const char *pszModule, const char *pszSymbol, uint32_t uSymbol,
551 uintmax_t *pValue, uint32_t *pfKind, void *pvModuleUser, void *pvUser);
552typedef FNKLDRMODGETIMPORT *PFNKLDRMODGETIMPORT;
553typedef int FNKLDRMODENUMSYMS(PKLDRMOD pMod, const char *pszSymbol, unsigned uSymbol, uintmax_t Value, uint32_t fKind, void *pvUser);
554typedef FNKLDRMODENUMSYMS *PFNKLDRMODENUMSYMS;
555
556int kLdrModOpen(const char *pszFilename, PPKLDRMOD ppMod);
557int kLdrModOpenFromRdr(PKLDRRDR pRdr, PPKLDRMOD ppMod);
558int kLdrModOpenNative(const char *pszFilename, PPKLDRMOD ppMod);
559int kLdrModClose(PKLDRMOD pMod);
560int kLdrModQuerySymbol(PKLDRMOD pMod, const void *pvBits, uintmax_t BaseAddress, const char *pszSymbol, uintmax_t *pValue, uint32_t *pfKind);
561int kLdrModEnumSymbols(PKLDRMOD pMod, unsigned fFlags, const void *pvBits, uintmax_t BaseAddress, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser);
562int kLdrModGetImport(PKLDRMOD pMod, uint32_t iImport, const char *pszName, size_t cchName);
563int32_t kLdrModNumberOfImports(PKLDRMOD pMod);
564int kLdrModMap(PKLDRMOD pMod);
565int kLdrModUnmap(PKLDRMOD pMod);
566int kLdrModFixupMapping(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
567size_t kLdrModSize(PKLDRMOD pMod);
568int kLdrModGetBits(PKLDRMOD pMod, void *pvBits, uintmax_t BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
569int kLdrModRelocateBits(PKLDRMOD pMod, void *pvBits, uintmax_t NewBaseAddress, uintmax_t OldBaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
570int kLdrModCanExecuteOn(PKLDRMOD pMod, KLDRARCH enmArch, KLDRCPU enmCpu);
571int kLdrModGetStackInfo(PKLDRMOD pMod, PKLDRSTACKINFO pStackInfo);
572int kLdrModQueryMainEntrypoint(PKLDRMOD pMod, void *pvBits, uintmax_t uBaseAddress, uintmax_t *puValue);
573int kLdrModAllocTLS(PKLDRMOD pMod);
574void kLdrModFreeTLS(PKLDRMOD pMod);
575int kLdrModReload(PKLDRMOD pMod);
576int kLdrModCallInit(PKLDRMOD pMod);
577int kLdrModCallTerm(PKLDRMOD pMod);
578int kLdrModCallThread(PKLDRMOD pMod, unsigned fAttachingOrDetaching);
579
580/** @} */
581
582
583
584
585/** @defgroup grp_kLdrDyld kLdrDyld - The dynamic loader
586 * @{ */
587
588/** The handle to a dynamic loader module. */
589typedef struct KLDRDYLDMOD *HKLDRMOD;
590/** Pointer to the handle to a dynamic loader module. */
591typedef HKLDRMOD *PHKLDRMOD;
592/** NIL handle value. */
593#define NIL_HKLDRMOD ((HKLDRMOD)0)
594
595
596/**
597 * File search method.
598 *
599 * In addition to it's own way of finding files, kLdr emulates
600 * the methods employed by the most popular systems.
601 */
602typedef enum KLDRDYLDSEARCH
603{
604 /** The usual invalid file search method. */
605 KLDRDYLD_SEARCH_INVALID = 0,
606 /** Uses the kLdr file search method.
607 * @todo invent me. */
608 KLDRDYLD_SEARCH_KLDR,
609 /** Use the emulation closest to the host system. */
610 KLDRDYLD_SEARCH_HOST,
611 /** Emulate the OS/2 file search method.
612 * On non-OS/2 systems, BEGINLIBPATH, LIBPATH, ENDLIBPATH and LIBPATHSTRICT are
613 * taken form the environment. */
614 KLDRDYLD_SEARCH_OS2,
615 /** Emulate the standard window file search method. */
616 KLDRDYLD_SEARCH_WINDOWS,
617 /** Emulate the alternative window file search method. */
618 KLDRDYLD_SEARCH_WINDOWS_ALTERED,
619 /** Emulate the most common UNIX file search method. */
620 KLDRDYLD_SEARCH_UNIX_COMMON,
621 /** End of the valid file search method values. */
622 KLDRDYLD_SEARCH_END,
623 /** Hack to blow the type up to 32-bit. */
624 KLDRDYLD_SEARCH_32BIT_HACK = 0x7fffffff
625} KLDRDYLDSEARCH;
626
627/** @name kLdrDyldLoad and kLdrDyldFindByName flags.
628 * @{ */
629/** The symbols in the module should be loaded into the global unix namespace.
630 * If not specified, the symbols are local and can only be referenced directly. */
631#define KLDRYDLD_LOAD_FLAGS_GLOBAL_SYMBOLS 0x00000001
632/** The module shouldn't be found by a global module search.
633 * If not specified, the module can be found by unspecified module searches,
634 * typical used when loading import/dep modules. */
635#define KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE 0x00000002
636/** Do a recursive initialization calls instead of defering them to the outermost call. */
637#define KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT 0x00000004
638/** We're loading the executable module.
639 * Internal flag which will be rejected by kLdrDyldLoad. */
640#define KLDRDYLD_LOAD_FLAGS_EXECUTABLE 0x40000000
641/** @} */
642
643
644int kLdrDyldLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
645 unsigned fFlags, PHKLDRMOD phMod, char *pszErr, size_t cchErr);
646int kLdrDyldUnload(HKLDRMOD hMod);
647int kLdrDyldFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
648 unsigned fFlags, PHKLDRMOD phMod);
649int kLdrDyldFindByAddress(uintptr_t Address, PHKLDRMOD phMod, uint32_t *piSegment, uintptr_t *poffSegment);
650int kLdrDyldGetName(HKLDRMOD hMod, char *pszName, size_t cchName);
651int kLdrDyldGetFilename(HKLDRMOD hMod, char *pszFilename, size_t cchFilename);
652int kLdrDyldQuerySymbol(HKLDRMOD hMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, uintptr_t *pValue, uint32_t *pfKind);
653
654/** @name OS/2 like API
655 * @{ */
656int kLdrDosLoadModule(char *pszObject, size_t cbObject, const char *pszModule, PHKLDRMOD phMod);
657int kLdrDosFreeModule(HKLDRMOD hMod);
658int kLdrDosQueryModuleHandle(const char *pszModname, PHKLDRMOD phMod);
659int kLdrDosQueryModuleName(HKLDRMOD hMod, size_t cchName, char *pszName);
660int kLdrDosQueryProcAddr(HKLDRMOD hMod, uint32_t iOrdinal, const char *pszProcName, void **ppvProcAddr);
661int kLdrDosQueryProcType(HKLDRMOD hMod, uint32_t iOrdinal, const char *pszProcName, uint32_t *pfProcType);
662int kLdrDosQueryModFromEIP(PHKLDRMOD phMod, uint32_t *piObject, size_t cbName, char *pszName, uintptr_t *poffObject, uintptr_t ulEIP);
663int kLdrDosReplaceModule(const char *pszOldModule, const char *pszNewModule, const char *pszBackupModule);
664int kLdrDosGetResource(HKLDRMOD hMod, uint32_t idType, uint32_t idName, void **pvResAddr);
665int kLdrDosQueryResourceSize(HKLDRMOD hMod, uint32_t idTypeID, uint32_t idName, uint32_t *pcb);
666int kLdrDosFreeResource(void *pvResAddr);
667/** @} */
668
669/** @name POSIX like API
670 * @{ */
671HKLDRMOD kLdrDlOpen(const char *pszLibrary, int fFlags);
672const char *kLdrDlError(void);
673void * kLdrDlSym(HKLDRMOD hMod, const char *pszSymbol);
674int kLdrDlClose(HKLDRMOD hMod);
675/** @} */
676
677/** @name Win32 like API
678 * @{ */
679HKLDRMOD kLdrWLoadLibrary(const char *pszFilename);
680HKLDRMOD kLdrWLoadLibraryEx(const char *pszFilename, void *hFileReserved, uint32_t fFlags);
681uint32_t kLdrWGetModuleFileName(HKLDRMOD hMod, char *pszModName, size_t cchModName);
682HKLDRMOD kLdrWGetModuleHandle(const char *pszFilename);
683int kLdrWGetModuleHandleEx(uint32_t fFlags, const char *pszFilename, HKLDRMOD hMod);
684void * kLdrWGetProcAddress(HKLDRMOD hMod, const char *pszProcName);
685uint32_t kLdrWGetDllDirectory(size_t cchDir, char *pszDir);
686int kLdrWSetDllDirectory(const char *pszDir);
687int kLdrWFreeLibrary(HKLDRMOD hMod);
688int kLdrWDisableThreadLibraryCalls(HKLDRMOD hMod);
689
690/** @} */
691
692
693/** @name Process Bootstrapping
694 * @{ */
695
696/**
697 * Argument package from the stub.
698 */
699typedef struct KLDREXEARGS
700{
701 /** Flags. (Currently unused, MBZ.) */
702 uint32_t fFlags;
703 /** The search method to use when loading this executable. */
704 KLDRDYLDSEARCH enmSearch;
705 /** The executable file that the stub is supposed to load. */
706 char szExecutable[260];
707 /** The default prefix used when searching for DLLs. */
708 char szDefPrefix[16];
709 /** The default suffix used when searching for DLLs. */
710 char szDefSuffix[16];
711 /** The LD_LIBRARY_PATH prefix for the process.. */
712 char szLibPath[4096 - sizeof(uint32_t) - sizeof(KLDRDYLDSEARCH) - 16 - 16 - 260];
713} KLDREXEARGS, *PKLDREXEARGS;
714
715void kLdrLoadExe(PKLDREXEARGS pArgs, void *pvOS);
716
717/** @} */
718
719/** @} */
720
721
722/** @defgroup grp_kLdrErr kLdr Status Codes
723 * kLdr uses a mix of native status codes and it's own status codes.
724 * A status code of 0 means success, all other status codes means failure.
725 * @{
726 */
727#ifdef __OS2__
728# define KLDR_ERR_BASE 0x7face000
729#elif defined(__WIN__)
730# define KLDR_ERR_BASE 0x7face000
731#else
732# error "port me"
733#endif
734/** The image format is unknown. */
735#define KLDR_ERR_UNKNOWN_FORMAT (KLDR_ERR_BASE + 0)
736/** The MZ image format isn't supported by this kLdr build. */
737#define KLDR_ERR_MZ_NOT_SUPPORTED (KLDR_ERR_BASE + 1)
738/** The NE image format isn't supported by this kLdr build. */
739#define KLDR_ERR_NE_NOT_SUPPORTED (KLDR_ERR_BASE + 2)
740/** The LX image format isn't supported by this kLdr build. */
741#define KLDR_ERR_LX_NOT_SUPPORTED (KLDR_ERR_BASE + 3)
742/** The LE image format isn't supported by this kLdr build. */
743#define KLDR_ERR_LE_NOT_SUPPORTED (KLDR_ERR_BASE + 4)
744/** The PE image format isn't supported by this kLdr build. */
745#define KLDR_ERR_PE_NOT_SUPPORTED (KLDR_ERR_BASE + 5)
746/** The ELF image format isn't supported by this kLdr build. */
747#define KLDR_ERR_ELF_NOT_SUPPORTED (KLDR_ERR_BASE + 6)
748/** The mach-o image format isn't supported by this kLdr build. */
749#define KLDR_ERR_MACHO_NOT_SUPPORTED (KLDR_ERR_BASE + 7)
750/** The mach-o image format isn't supported by this kLdr build. */
751#define KLDR_ERR_AOUT_NOT_SUPPORTED (KLDR_ERR_BASE + 8)
752
753/** Invalid parameter to a kLdr API. */
754#define KLDR_ERR_INVALID_PARAMETER (KLDR_ERR_BASE + 32)
755/** Invalid handle parameter to a kLdr API. */
756#define KLDR_ERR_INVALID_HANDLE (KLDR_ERR_BASE + 33)
757/** The module wasn't loaded dynamically. */
758#define KLDR_ERR_NOT_LOADED_DYNAMICALLY (KLDR_ERR_BASE + 34)
759/** The module wasn't found. */
760#define KLDR_ERR_MODULE_NOT_FOUND (KLDR_ERR_BASE + 35)
761/** A prerequisit module wasn't found. */
762#define KLDR_ERR_PREREQUISITE_MODULE_NOT_FOUND (KLDR_ERR_BASE + 36)
763/** The module is being terminated and can therefore not be loaded. */
764#define KLDR_ERR_MODULE_TERMINATING (KLDR_ERR_BASE + 37)
765/** A prerequisit module is being terminated and can therefore not be loaded. */
766#define KLDR_ERR_PREREQUISITE_MODULE_TERMINATING (KLDR_ERR_BASE + 38)
767/** The module initialization failed. */
768#define KLDR_ERR_MODULE_INIT_FAILED (KLDR_ERR_BASE + 39)
769/** The initialization of a prerequisite module failed. */
770#define KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED (KLDR_ERR_BASE + 40)
771/** The module has already failed initialization and can't be attempted reloaded until
772 * after we've finished garbage collection. */
773#define KLDR_ERR_MODULE_INIT_FAILED_ALREADY (KLDR_ERR_BASE + 41)
774/** A prerequisite module has already failed initialization and can't be attempted
775 * reloaded until after we've finished garbage collection. */
776#define KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED_ALREADY (KLDR_ERR_BASE + 42)
777/** Prerequisite recursed too deeply. */
778#define KLDR_ERR_PREREQUISITE_RECURSED_TOO_DEEPLY (KLDR_ERR_BASE + 43)
779/** Failed to allocate the main stack. */
780#define KLDR_ERR_MAIN_STACK_ALLOC_FAILED (KLDR_ERR_BASE + 44)
781/** Buffer overflow. */
782#define KLDR_ERR_BUFFER_OVERFLOW (KLDR_ERR_BASE + 45)
783
784/** Encountered a bad fixup. */
785#define KLDR_ERR_BAD_FIXUP (KLDR_ERR_BASE + 48)
786
787/** A memory allocation failed. */
788#define KLDR_ERR_NO_MEMORY (KLDR_ERR_BASE + 64)
789
790/** @} */
791
792
793#ifdef __cplusplus
794}
795#endif
796
797#endif
798
Note: See TracBrowser for help on using the repository browser.