source: trunk/kLdr/kLdr.h@ 2848

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

stubbed all the interpreter entry points.

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