1 | /* $Id: kLdr.h 2855 2006-11-04 02:30:19Z 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
|
---|
31 | extern "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
|
---|
38 | typedef signed char int8_t;
|
---|
39 | typedef unsigned char uint8_t;
|
---|
40 | typedef signed short int16_t;
|
---|
41 | typedef unsigned short uint16_t;
|
---|
42 | typedef signed int int32_t;
|
---|
43 | typedef unsigned int uint32_t;
|
---|
44 | typedef signed __int64 int64_t;
|
---|
45 | typedef unsigned __int64 uint64_t;
|
---|
46 | typedef int64_t intmax_t;
|
---|
47 | typedef 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. */
|
---|
57 | typedef uint64_t KLDRADDR;
|
---|
58 | /** Pointer to a kLdr address. */
|
---|
59 | typedef KLDRADDR *PKLDRADDR;
|
---|
60 | /** Pointer to a const kLdr address. */
|
---|
61 | typedef const KLDRADDR *PCKLDRADDR;
|
---|
62 |
|
---|
63 | /** NIL address. */
|
---|
64 | #define NIL_KLDRADDR (~(uint64_t)0)
|
---|
65 |
|
---|
66 | /** The kLdr size type. */
|
---|
67 | typedef uint64_t KLDRSIZE;
|
---|
68 | /** Pointer to a kLdr size. */
|
---|
69 | typedef KLDRSIZE *PKLDRSIZE;
|
---|
70 | /** Pointer to a const kLdr size. */
|
---|
71 | typedef 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 | */
|
---|
82 | typedef 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. */
|
---|
110 | typedef struct KLDRRDR *PKLDRRDR;
|
---|
111 | /** Pointer to a file provider instance core pointer. */
|
---|
112 | typedef struct KLDRRDR **PPKLDRRDR;
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * File provider instance operations.
|
---|
116 | */
|
---|
117 | typedef 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. */
|
---|
252 | typedef KLDRRDROPS *PKLDRRDROPS;
|
---|
253 | /** Pointer to const file provider operations. */
|
---|
254 | typedef const KLDRRDROPS *PCKLDRRDROPS;
|
---|
255 |
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * File provider instance core.
|
---|
259 | */
|
---|
260 | typedef struct KLDRRDR
|
---|
261 | {
|
---|
262 | /** Pointer to the file provider operations. */
|
---|
263 | PCKLDRRDROPS pOps;
|
---|
264 | } KLDRRDR;
|
---|
265 |
|
---|
266 | void kLdrRdrAddProvider(PKLDRRDROPS pAdd);
|
---|
267 |
|
---|
268 | int kLdrRdrOpen( PPKLDRRDR ppRdr, const char *pszFilename);
|
---|
269 | int kLdrRdrClose( PKLDRRDR pRdr);
|
---|
270 | int kLdrRdrRead( PKLDRRDR pRdr, void *pvBuf, size_t cb, off_t off);
|
---|
271 | int kLdrRdrAllMap( PKLDRRDR pRdr, const void **ppvBits);
|
---|
272 | int kLdrRdrAllUnmap(PKLDRRDR pRdr, const void *pvBits);
|
---|
273 | off_t kLdrRdrSize( PKLDRRDR pRdr);
|
---|
274 | off_t kLdrRdrTell( PKLDRRDR pRdr);
|
---|
275 | const char *kLdrRdrName(PKLDRRDR pRdr);
|
---|
276 |
|
---|
277 | /** @} */
|
---|
278 |
|
---|
279 |
|
---|
280 |
|
---|
281 | /** @defgroup grp_kLdrMod kLdrMod - The executable image intepreter
|
---|
282 | * @{ */
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * CPU Architecture.
|
---|
286 | * @todo Double check the non intel architectures.
|
---|
287 | */
|
---|
288 | typedef enum KLDRARCH
|
---|
289 | {
|
---|
290 | /** The usual invalid one. */
|
---|
291 | KLDRARCH_INVALID = 0,
|
---|
292 | /** Clone or Intel 16-bit x86. */
|
---|
293 | KLDRARCH_X86_16,
|
---|
294 | /** Clone or Intel 32-bit x86. */
|
---|
295 | KLDRARCH_X86_32,
|
---|
296 | /** AMD64 (including clones). */
|
---|
297 | KLDRARCH_AMD64,
|
---|
298 | /** Itanic (64-bit). */
|
---|
299 | KLDRARCH_IA64,
|
---|
300 | /** ALPHA (64-bit). */
|
---|
301 | KLDRARCH_ALPHA,
|
---|
302 | /** ALPHA limited to 32-bit. */
|
---|
303 | KLDRARCH_ALPHA_32,
|
---|
304 | /** 32-bit ARM. */
|
---|
305 | KLDRARCH_ARM_32,
|
---|
306 | /** 64-bit ARM. */
|
---|
307 | KLDRARCH_ARM_64,
|
---|
308 | /** 32-bit MIPS. */
|
---|
309 | KLDRARCH_MIPS_32,
|
---|
310 | /** 64-bit MIPS. */
|
---|
311 | KLDRARCH_MIPS_64,
|
---|
312 | /** 32-bit PowerPC. */
|
---|
313 | KLDRARCH_POWERPC_32,
|
---|
314 | /** 64-bit PowerPC. */
|
---|
315 | KLDRARCH_POWERPC_64,
|
---|
316 | /** 32-bit SPARC. */
|
---|
317 | KLDRARCH_SPARC_32,
|
---|
318 | /** 64-bit SPARC. */
|
---|
319 | KLDRARCH_SPARC_64,
|
---|
320 | /** The end of the valid architecture values (exclusive). */
|
---|
321 | KLDRARCH_END,
|
---|
322 | /** Hack to blow the type up to 32-bit. */
|
---|
323 | KLDRARCH_32BIT_HACK = 0x7fffffff
|
---|
324 | } KLDRARCH;
|
---|
325 | /** Pointer to a CPU architecture type. */
|
---|
326 | typedef KLDRARCH *PKLDRARCH;
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * CPU models.
|
---|
330 | */
|
---|
331 | typedef enum KLDRCPU
|
---|
332 | {
|
---|
333 | /** The usual invalid cpu. */
|
---|
334 | KLDRCPU_INVALID = 0,
|
---|
335 | /** @name KLDRARCH_X86_16
|
---|
336 | * @{ */
|
---|
337 | KLDRCPU_I8086,
|
---|
338 | KLDRCPU_I8088,
|
---|
339 | KLDRCPU_I80186,
|
---|
340 | KLDRCPU_I80286,
|
---|
341 | KLDRCPU_I386_16,
|
---|
342 | KLDRCPU_I486_16,
|
---|
343 | KLDRCPU_I486SX_16,
|
---|
344 | KLDRCPU_I586_16,
|
---|
345 | KLDRCPU_I686_16,
|
---|
346 | KLDRCPU_P4_16,
|
---|
347 | KLDRCPU_CORE2_16,
|
---|
348 | KLDRCPU_K6_16,
|
---|
349 | KLDRCPU_K7_16,
|
---|
350 | KLDRCPU_K8_16,
|
---|
351 | KLDRCPU_FIRST_X86_16 = KLDRCPU_I8086,
|
---|
352 | KLDRCPU_LAST_X86_16 = KLDRCPU_K8_16,
|
---|
353 | /** @} */
|
---|
354 |
|
---|
355 | /** @name KLDRARCH_X86_32
|
---|
356 | * @{ */
|
---|
357 | KLDRCPU_X86_32_BLEND,
|
---|
358 | KLDRCPU_I386,
|
---|
359 | KLDRCPU_I486,
|
---|
360 | KLDRCPU_I486SX,
|
---|
361 | KLDRCPU_I586,
|
---|
362 | KLDRCPU_I686,
|
---|
363 | KLDRCPU_P4,
|
---|
364 | KLDRCPU_CORE2_32,
|
---|
365 | KLDRCPU_K6,
|
---|
366 | KLDRCPU_K7,
|
---|
367 | KLDRCPU_K8_32,
|
---|
368 | KLDRCPU_FIRST_X86_32 = KLDRCPU_I386,
|
---|
369 | KLDRCPU_LAST_X86_32 = KLDRCPU_K8_32,
|
---|
370 | /** @} */
|
---|
371 |
|
---|
372 | /** @name KLDRARCH_AMD64
|
---|
373 | * @{ */
|
---|
374 | KLDRCPU_AMD64_BLEND,
|
---|
375 | KLDRCPU_K8,
|
---|
376 | KLDRCPU_P4_64,
|
---|
377 | KLDRCPU_CORE2,
|
---|
378 | KLDRCPU_FIRST_AMD64 = KLDRCPU_K8,
|
---|
379 | KLDRCPU_LAST_AMD64 = KLDRCPU_CORE2,
|
---|
380 | /** @} */
|
---|
381 |
|
---|
382 | /** The end of the valid cpu values (exclusive). */
|
---|
383 | KLDRCPU_END,
|
---|
384 | /** Hack to blow the type up to 32-bit. */
|
---|
385 | KLDRCPU_32BIT_HACK = 0x7fffffff
|
---|
386 | } KLDRCPU;
|
---|
387 | /** Pointer to a CPU type. */
|
---|
388 | typedef KLDRCPU *PKLDRCPU;
|
---|
389 |
|
---|
390 | void kLdrGetArchCpu(PKLDRARCH penmArch, PKLDRCPU penmCpu);
|
---|
391 | int kLdrCompareCpus(KLDRARCH enmCodeArch, KLDRCPU enmCodeCpu, KLDRARCH enmArch, KLDRCPU enmCpu);
|
---|
392 |
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Debug info type (from the loader point of view).
|
---|
396 | */
|
---|
397 | typedef enum KLDRDBGINFOTYPE
|
---|
398 | {
|
---|
399 | /** The usual invalid enum value. */
|
---|
400 | KLDRDBGINFOTYPE_INVALID = 0,
|
---|
401 | /** Stabs. */
|
---|
402 | KLDRDBGINFOTYPE_STABS,
|
---|
403 | /** Debug With Arbitrary Record Format (DWARF). */
|
---|
404 | KLDRDBGINFOTYPE_DWARF,
|
---|
405 | /** Microsoft Codeview debug info. */
|
---|
406 | KLDRDBGINFOTYPE_CODEVIEW,
|
---|
407 | /** Watcom debug info. */
|
---|
408 | KLDRDBGINFOTYPE_WATCOM,
|
---|
409 | /** IBM High Level Language debug info.. */
|
---|
410 | KLDRDBGINFOTYPE_HLL,
|
---|
411 | /** The end of the valid debug info values (exclusive). */
|
---|
412 | KLDRDBGINFOTYPE_END,
|
---|
413 | /** Blow the type up to 32-bit. */
|
---|
414 | KLDRDBGINFOTYPE_32BIT_HACK = 0x7fffffff
|
---|
415 | } KLDRDBGINFOTYPE;
|
---|
416 | /** Pointer to a kLdr debug info type. */
|
---|
417 | typedef KLDRDBGINFOTYPE *PKLDRDBGINFOTYPE;
|
---|
418 |
|
---|
419 |
|
---|
420 | /**
|
---|
421 | * Stack information.
|
---|
422 | */
|
---|
423 | typedef struct KLDRSTACKINFO
|
---|
424 | {
|
---|
425 | /** The base address of the stack (sub) segment.
|
---|
426 | * Set this to NIL_KLDRADDR if the module doesn't include any stack segment. */
|
---|
427 | KLDRADDR Address;
|
---|
428 | /** The base address of the stack (sub) segment, link address.
|
---|
429 | * Set this to NIL_KLDRADDR if the module doesn't include any stack (sub)segment. */
|
---|
430 | KLDRADDR LinkAddress;
|
---|
431 | /** The stack size of the main thread.
|
---|
432 | * If no stack (sub)segment in the module, this is the stack size of the main thread.
|
---|
433 | * If the module doesn't contain this kind of information this field will be set to 0. */
|
---|
434 | KLDRSIZE cbStack;
|
---|
435 | /** The stack size of non-main threads.
|
---|
436 | * If the module doesn't contain this kind of information this field will be set to 0. */
|
---|
437 | KLDRSIZE cbStackThread;
|
---|
438 | } KLDRSTACKINFO;
|
---|
439 | /** Pointer to stack information. */
|
---|
440 | typedef KLDRSTACKINFO *PKLDRSTACKINFO;
|
---|
441 | /** Pointer to const stack information. */
|
---|
442 | typedef const KLDRSTACKINFO *PCKLDRSTACKINFO;
|
---|
443 |
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Loader segment.
|
---|
447 | */
|
---|
448 | typedef struct KLDRSEG
|
---|
449 | {
|
---|
450 | /** Variable free to use for the kLdr user. */
|
---|
451 | void *pvUser;
|
---|
452 | /** The segment name. (Might not be zero terminated!) */
|
---|
453 | const char *pchName;
|
---|
454 | /** The length of the segment name. */
|
---|
455 | uint32_t cchName;
|
---|
456 | /** The size of the segment. */
|
---|
457 | KLDRSIZE cb;
|
---|
458 | /** The required segment alignment. */
|
---|
459 | KLDRADDR Alignment;
|
---|
460 | /** The link address.
|
---|
461 | * Set to NIL_KLDRADDR if the segment isn't supposed to be mapped. */
|
---|
462 | KLDRADDR LinkAddress;
|
---|
463 | /** The address the segment was mapped at by kLdrModMap().
|
---|
464 | * Set to NIL_KLDRADDR if not mapped. */
|
---|
465 | KLDRADDR MapAddress;
|
---|
466 | /** The segment protection. */
|
---|
467 | KLDRPROT enmProt;
|
---|
468 | } KLDRSEG;
|
---|
469 | /** Pointer to a loader segment. */
|
---|
470 | typedef KLDRSEG *PKLDRSEG;
|
---|
471 | /** Pointer to a loader segment. */
|
---|
472 | typedef const KLDRSEG *PCKLDRSEG;
|
---|
473 |
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Loader module format.
|
---|
477 | */
|
---|
478 | typedef enum KLDRFMT
|
---|
479 | {
|
---|
480 | /** The usual invalid 0 format. */
|
---|
481 | KLDRFMT_INVALID = 0,
|
---|
482 | /** The native OS loader. */
|
---|
483 | KLDRFMT_NATIVE,
|
---|
484 | /** The AOUT loader. */
|
---|
485 | KLDRFMT_AOUT,
|
---|
486 | /** The ELF loader. */
|
---|
487 | KLDRFMT_ELF,
|
---|
488 | /** The LX loader. */
|
---|
489 | KLDRFMT_LX,
|
---|
490 | /** The mach-o loader. */
|
---|
491 | KLDRFMT_MACHO,
|
---|
492 | /** The LX loader. */
|
---|
493 | KLDRFMT_PE,
|
---|
494 | /** The end of the valid format values (exclusive). */
|
---|
495 | KLDRFMT_END,
|
---|
496 | /** Hack to blow the type up to 32-bit. */
|
---|
497 | KLDRFMT_32BIT_HACK = 0x7fffffff
|
---|
498 | } KLDRFMT;
|
---|
499 |
|
---|
500 |
|
---|
501 | /**
|
---|
502 | * Loader module type.
|
---|
503 | */
|
---|
504 | typedef enum KLDRTYPE
|
---|
505 | {
|
---|
506 | /** The usual invalid 0 type. */
|
---|
507 | KLDRTYPE_INVALID = 0,
|
---|
508 | /** Object file. */
|
---|
509 | KLDRTYPE_OBJECT,
|
---|
510 | /** Executable module, fixed load address. */
|
---|
511 | KLDRTYPE_EXECUTABLE_FIXED,
|
---|
512 | /** Executable module, relocatable, non-fixed load address. */
|
---|
513 | KLDRTYPE_EXECUTABLE_RELOCATABLE,
|
---|
514 | /** Executable module, position independent code, non-fixed load address. */
|
---|
515 | KLDRTYPE_EXECUTABLE_PIC,
|
---|
516 | /** Shared library, fixed load address.
|
---|
517 | * Typically a system library. */
|
---|
518 | KLDRTYPE_SHARED_LIBRARY_FIXED,
|
---|
519 | /** Shared library, relocatable, non-fixed load address. */
|
---|
520 | KLDRTYPE_SHARED_LIBRARY_RELOCATABLE,
|
---|
521 | /** Shared library, position independent code, non-fixed load address. */
|
---|
522 | KLDRTYPE_SHARED_LIBRARY_PIC,
|
---|
523 | /** DLL that contains no code or data only imports and exports. (Chiefly OS/2.) */
|
---|
524 | KLDRTYPE_FORWARDER_DLL,
|
---|
525 | /** Core or dump. */
|
---|
526 | KLDRTYPE_CORE,
|
---|
527 | /** The end of the valid types values (exclusive). */
|
---|
528 | KLDRTYPE_END,
|
---|
529 | /** Hack to blow the type up to 32-bit. */
|
---|
530 | KLDRTYPE_32BIT_HACK = 0x7fffffff
|
---|
531 | } KLDRTYPE;
|
---|
532 |
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * Loader endian indicator.
|
---|
536 | */
|
---|
537 | typedef enum KLDRENDIAN
|
---|
538 | {
|
---|
539 | /** The usual invalid endian. */
|
---|
540 | KLDRENDIAN_INVALID,
|
---|
541 | /** Little endian. */
|
---|
542 | KLDRENDIAN_LITTLE,
|
---|
543 | /** Bit endian. */
|
---|
544 | KLDRENDIAN_BIG,
|
---|
545 | /** Endianness doesn't have a meaning in the context. */
|
---|
546 | KLDRENDIAN_NA,
|
---|
547 | /** The end of the valid endian values (exclusive). */
|
---|
548 | KLDRENDIAN_END,
|
---|
549 | /** Hack to blow the type up to 32-bit. */
|
---|
550 | KLDRENDIAN_32BIT_HACK = 0x7fffffff
|
---|
551 | } KLDRENDIAN;
|
---|
552 |
|
---|
553 |
|
---|
554 | /** Pointer to a module interpreter method table. */
|
---|
555 | typedef struct KLDRMODOPS *PKLDRMODOPS;
|
---|
556 | /** Pointer to const module interpreter methods table. */
|
---|
557 | typedef const struct KLDRMODOPS *PCKLDRMODOPS;
|
---|
558 |
|
---|
559 | /**
|
---|
560 | * Module interpreter instance.
|
---|
561 | * All members are read only unless you're kLdrMod or the module interpreter.
|
---|
562 | */
|
---|
563 | typedef struct KLDRMOD
|
---|
564 | {
|
---|
565 | /** Magic number. */
|
---|
566 | uint32_t u32Magic;
|
---|
567 | /** The format of this module. */
|
---|
568 | KLDRFMT enmFmt;
|
---|
569 | /** The type of module. */
|
---|
570 | KLDRTYPE enmType;
|
---|
571 | /** The architecture this module was built for. */
|
---|
572 | KLDRARCH enmArch;
|
---|
573 | /** The minium cpu this module was built for.
|
---|
574 | * This might not be accurate, so use kLdrModCanExecuteOn() to check. */
|
---|
575 | KLDRARCH enmCpu;
|
---|
576 | /** The endian used by the module. */
|
---|
577 | KLDRENDIAN enmEndian;
|
---|
578 | /** The filename length (bytes). */
|
---|
579 | uint32_t cchFilename;
|
---|
580 | /** The filename. */
|
---|
581 | const char *pszFilename;
|
---|
582 | /** The module name. */
|
---|
583 | const char *pszName;
|
---|
584 | /** The module name length (bytes). */
|
---|
585 | uint32_t cchName;
|
---|
586 | /** The number of segments in the module. */
|
---|
587 | uint32_t cSegments;
|
---|
588 | /** Pointer to the loader methods.
|
---|
589 | * Not meant for calling directly thru! */
|
---|
590 | PCKLDRMODOPS pOps;
|
---|
591 | /** Pointer to the read instance. (Can be NULL after kLdrModDone().)*/
|
---|
592 | PKLDRRDR pRdr;
|
---|
593 | /** The module data. */
|
---|
594 | void *pvData;
|
---|
595 | /** Segments. (variable size, can be zero) */
|
---|
596 | KLDRSEG aSegments[1];
|
---|
597 | } KLDRMOD, *PKLDRMOD, **PPKLDRMOD;
|
---|
598 |
|
---|
599 | /** The magic for KLDRMOD::u32Magic. (Kosuke Fujishima) */
|
---|
600 | #define KLDRMOD_MAGIC 0x19640707
|
---|
601 |
|
---|
602 |
|
---|
603 | /** Special base address value alias for the link address. */
|
---|
604 | #define KLDRMOD_BASEADDRESS_LINK (~(KLDRADDR)1)
|
---|
605 | /** Special base address value alias for the actual load address (must be mapped). */
|
---|
606 | #define KLDRMOD_BASEADDRESS_MAP (~(KLDRADDR)2)
|
---|
607 |
|
---|
608 | /** Special import module ordinal value used to indicate that there is no
|
---|
609 | * specific module associated with the requested symbol. */
|
---|
610 | #define NIL_KLDRMOD_IMPORT (~(uint32_t)0)
|
---|
611 |
|
---|
612 | /** Special symbol ordinal value used to indicate that the symbol
|
---|
613 | * only has a string name. */
|
---|
614 | #define NIL_KLDRMOD_SYM_ORDINAL (~(uint32_t)0)
|
---|
615 |
|
---|
616 |
|
---|
617 | /** @name Load symbol kind flags.
|
---|
618 | * @{ */
|
---|
619 | /** The bitness doesn't matter. */
|
---|
620 | #define KLDRSYMKIND_NO_BIT 0x00000000
|
---|
621 | /** 16-bit symbol. */
|
---|
622 | #define KLDRSYMKIND_16BIT 0x00000001
|
---|
623 | /** 32-bit symbol. */
|
---|
624 | #define KLDRSYMKIND_32BIT 0x00000002
|
---|
625 | /** 64-bit symbol. */
|
---|
626 | #define KLDRSYMKIND_64BIT 0x00000003
|
---|
627 | /** Mask out the bit.*/
|
---|
628 | #define KLDRSYMKIND_BIT_MASK 0x00000003
|
---|
629 | /** We don't know the type of symbol. */
|
---|
630 | #define KLDRSYMKIND_NO_TYPE 0x00000000
|
---|
631 | /** The symbol is a code object (method/function/procedure/whateveryouwannacallit). */
|
---|
632 | #define KLDRSYMKIND_CODE 0x00000010
|
---|
633 | /** The symbol is a data object. */
|
---|
634 | #define KLDRSYMKIND_DATA 0x00000020
|
---|
635 | /** Mask out the symbol type. */
|
---|
636 | #define KLDRSYMKIND_TYPE_MASK 0x00000030
|
---|
637 | /** Valid symbol kind mask. */
|
---|
638 | #define KLDRSYMKIND_MASK 0x00000033
|
---|
639 | /** Weak symbol. */
|
---|
640 | #define KLDRSYMKIND_WEAK 0x00000100
|
---|
641 | /** Forwarder symbol. */
|
---|
642 | #define KLDRSYMKIND_FORWARDER 0x00000200
|
---|
643 | /** @} */
|
---|
644 |
|
---|
645 | /** @name kLdrModEnumSymbols flags.
|
---|
646 | * @{ */
|
---|
647 | /** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
|
---|
648 | #define KLDRMOD_ENUM_SYMS_FLAGS_ALL 0x00000001
|
---|
649 | /** @} */
|
---|
650 |
|
---|
651 |
|
---|
652 | /**
|
---|
653 | * Callback for resolving imported symbols when applying fixups.
|
---|
654 | *
|
---|
655 | * @returns 0 on success and *pValue and *pfKind filled.
|
---|
656 | * @returns Non-zero OS specific or kLdr status code on failure.
|
---|
657 | *
|
---|
658 | * @param pMod The module which fixups are begin applied.
|
---|
659 | * @param iImport The import module ordinal number or NIL_KLDRMOD_IMPORT.
|
---|
660 | * @param uSymbol The symbol ordinal number or NIL_KLDRMOD_SYM_ORDINAL.
|
---|
661 | * @param pszSymbol The symbol name. Can be NULL if uSymbol isn't nil.
|
---|
662 | * @param puValue Where to store the symbol value.
|
---|
663 | * @param pfKind Where to store the symbol kind flags.
|
---|
664 | * @param pvUser The user parameter specified to the relocation function.
|
---|
665 | */
|
---|
666 | typedef int FNKLDRMODGETIMPORT(PKLDRMOD pMod, uint32_t iImport, uint32_t uSymbol, const char *pszSymbol,
|
---|
667 | PKLDRADDR puValue, uint32_t *pfKind, void *pvUser);
|
---|
668 | /** Pointer to a import callback. */
|
---|
669 | typedef FNKLDRMODGETIMPORT *PFNKLDRMODGETIMPORT;
|
---|
670 |
|
---|
671 | /**
|
---|
672 | * Symbol enumerator callback.
|
---|
673 | *
|
---|
674 | * @returns 0 if enumeration should continue.
|
---|
675 | * @returns non-zero if the enumeration should stop. This status code will then be returned by kLdrModEnumSymbols().
|
---|
676 | *
|
---|
677 | * @param pMod The module which symbols are being enumerated.s
|
---|
678 | * @param uSymbol The symbol ordinal number or NIL_KLDRMOD_SYM_ORDINAL.
|
---|
679 | * @param pszSymbol The symbol name. This can be NULL if there is a symbol ordinal.
|
---|
680 | * This can also be an empty string if the symbol doesn't have a name
|
---|
681 | * or it's name has been stripped.
|
---|
682 | * @param uValue The symbol value.
|
---|
683 | * @param fKind The symbol kind flags.
|
---|
684 | * @param pvUser The user parameter specified to kLdrModEnumSymbols().
|
---|
685 | */
|
---|
686 | typedef int FNKLDRMODENUMSYMS(PKLDRMOD pMod, uint32_t uSymbol, const char *pszSymbol,
|
---|
687 | KLDRADDR uValue, uint32_t fKind, void *pvUser);
|
---|
688 | /** Pointer to a symbol enumerator callback. */
|
---|
689 | typedef FNKLDRMODENUMSYMS *PFNKLDRMODENUMSYMS;
|
---|
690 |
|
---|
691 | /**
|
---|
692 | * Debug info enumerator callback.
|
---|
693 | *
|
---|
694 | * @returns 0 to continue the enumeration.
|
---|
695 | * @returns non-zero if the enumeration should stop. This status code will then be returned by kLdrModEnumDbgInfo().
|
---|
696 | *
|
---|
697 | * @param pMod The module.
|
---|
698 | * @param enmType The debug info type.
|
---|
699 | * @param iDbgInfo The debug info ordinal number / id.
|
---|
700 | * @param offFile The file offset *if* this type has one specific location in the executable image file.
|
---|
701 | * This is -1 if there isn't any specific file location.
|
---|
702 | * @param cbFile The file size.
|
---|
703 | * This is 0 if there isn't any specific file location.
|
---|
704 | * @param pszExtFile This points to the name of an external file containing the debug info.
|
---|
705 | * This is NULL if there isn't any external file.
|
---|
706 | * @param pvUser The user parameter specified to kLdrModEnumDbgInfo.
|
---|
707 | */
|
---|
708 | typedef int FNKLDRENUMDBG(PKLDRMOD pMod, KLDRDBGINFOTYPE enmType, uint32_t iDbgInfo, off_t offFile, off_t cbFile,
|
---|
709 | const char *pszExtFile, void *pvUser);
|
---|
710 |
|
---|
711 | int kLdrModOpen(const char *pszFilename, PPKLDRMOD ppMod);
|
---|
712 | int kLdrModOpenFromRdr(PKLDRRDR pRdr, PPKLDRMOD ppMod);
|
---|
713 | int kLdrModOpenNative(const char *pszFilename, PPKLDRMOD ppMod);
|
---|
714 | int kLdrModClose(PKLDRMOD pMod);
|
---|
715 |
|
---|
716 | int kLdrModQuerySymbol(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, uint32_t uSymbol,
|
---|
717 | const char *pszSymbol, PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser,
|
---|
718 | PKLDRADDR puValue, uint32_t *pfKind);
|
---|
719 | int kLdrModEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress,
|
---|
720 | uint32_t fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser);
|
---|
721 | int kLdrModGetImport(PKLDRMOD pMod, const void *pvBits, uint32_t iImport, char *pszName, size_t cchName);
|
---|
722 | int32_t kLdrModNumberOfImports(PKLDRMOD pMod, const void *pvBits);
|
---|
723 | int kLdrModCanExecuteOn(PKLDRMOD pMod, const void *pvBits, KLDRARCH enmArch, KLDRCPU enmCpu);
|
---|
724 | int kLdrModGetStackInfo(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo);
|
---|
725 | int kLdrModQueryMainEntrypoint(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress);
|
---|
726 | /** Pointer to a debug info enumberator callback. */
|
---|
727 | typedef FNKLDRENUMDBG *PFNKLDRENUMDBG;
|
---|
728 | int kLdrModEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser);
|
---|
729 | int kLdrModHasDbgInfo(PKLDRMOD pMod, const void *pvBits);
|
---|
730 |
|
---|
731 | /** @name Operations On The Internally Managed Mapping
|
---|
732 | * @{ */
|
---|
733 | int kLdrModMap(PKLDRMOD pMod);
|
---|
734 | int kLdrModUnmap(PKLDRMOD pMod);
|
---|
735 | int kLdrModAllocTLS(PKLDRMOD pMod);
|
---|
736 | void kLdrModFreeTLS(PKLDRMOD pMod);
|
---|
737 | int kLdrModReload(PKLDRMOD pMod);
|
---|
738 | int kLdrModFixupMapping(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
739 | int kLdrModCallInit(PKLDRMOD pMod);
|
---|
740 | int kLdrModCallTerm(PKLDRMOD pMod);
|
---|
741 | int kLdrModCallThread(PKLDRMOD pMod, unsigned fAttachingOrDetaching);
|
---|
742 | /** @} */
|
---|
743 |
|
---|
744 | /** @name Operations On The Externally Managed Mappings
|
---|
745 | * @{ */
|
---|
746 | size_t kLdrModSize(PKLDRMOD pMod);
|
---|
747 | int kLdrModGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
748 | int kLdrModRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
|
---|
749 | PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
750 | /** @} */
|
---|
751 |
|
---|
752 |
|
---|
753 | /**
|
---|
754 | * The loader module operation.
|
---|
755 | */
|
---|
756 | typedef struct KLDRMODOPS
|
---|
757 | {
|
---|
758 | /** The name of this module interpreter. */
|
---|
759 | const char *pszName;
|
---|
760 | /** Pointer to the next module interpreter. */
|
---|
761 | PCKLDRMODOPS pNext;
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * Create a loader module instance interpreting the executable image found
|
---|
765 | * in the specified file provider instance.
|
---|
766 | *
|
---|
767 | * @returns 0 on success and *ppMod pointing to a module instance.
|
---|
768 | * On failure, a non-zero OS specific error code is returned.
|
---|
769 | * @param pOps Pointer to the registered method table.
|
---|
770 | * @param pRdr The file provider instance to use.
|
---|
771 | * @param offNewHdr The offset of the new header in MZ files. -1 if not found.
|
---|
772 | * @param ppMod Where to store the module instance pointer.
|
---|
773 | */
|
---|
774 | int (* pfnCreate)(PCKLDRMODOPS pOps, PKLDRRDR pRdr, off_t offNewHdr, PPKLDRMOD ppMod);
|
---|
775 | /**
|
---|
776 | * Destroys an loader module instance.
|
---|
777 | *
|
---|
778 | * The caller is responsible for calling kLdrModUnmap() and kLdrFreeTLS() first.
|
---|
779 | *
|
---|
780 | * @returns 0 on success, non-zero on failure. The module instance state
|
---|
781 | * is unknown on failure, it's best not to touch it.
|
---|
782 | * @param pMod The module.
|
---|
783 | */
|
---|
784 | int (* pfnDestroy)(PKLDRMOD pMod);
|
---|
785 |
|
---|
786 | /** @copydoc kLdrModQuerySymbol */
|
---|
787 | int (* pfnQuerySymbol)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, uint32_t uSymbol,
|
---|
788 | const char *pszSymbol, PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser,
|
---|
789 | PKLDRADDR puValue, uint32_t *pfKind);
|
---|
790 | /** @copydoc kLdrModEnumSymbols */
|
---|
791 | int (* pfnEnumSymbols)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, uint32_t fFlags,
|
---|
792 | PFNKLDRMODENUMSYMS pfnCallback, void *pvUser);
|
---|
793 | /** @copydoc kLdrModGetImport */
|
---|
794 | int (* pfnGetImport)(PKLDRMOD pMod, const void *pvBits, uint32_t iImport, char *pszName, size_t cchName);
|
---|
795 | /** @copydoc kLdrModNumberOfImports */
|
---|
796 | int32_t (* pfnNumberOfImports)(PKLDRMOD pMod, const void *pvBits);
|
---|
797 | /** @copydoc kLdrModCanExecuteOn */
|
---|
798 | int (* pfnCanExecuteOn)(PKLDRMOD pMod, const void *pvBits, KLDRARCH enmArch, KLDRCPU enmCpu);
|
---|
799 | /** @copydoc kLdrModGetStackInfo */
|
---|
800 | int (* pfnGetStackInfo)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo);
|
---|
801 | /** @copydoc kLdrModQueryMainEntrypoint */
|
---|
802 | int (* pfnQueryMainEntrypoint)(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress);
|
---|
803 | /** @copydoc kLdrModEnumDbgInfo */
|
---|
804 | int (* pfnEnumDbgInfo)(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser);
|
---|
805 | /** @copydoc kLdrModHasDbgInfo */
|
---|
806 | int (* pfnHasDbgInfo)(PKLDRMOD pMod, const void *pvBits);
|
---|
807 | /** @copydoc kLdrModMap */
|
---|
808 | int (* pfnMap)(PKLDRMOD pMod);
|
---|
809 | /** @copydoc kLdrModUnmap */
|
---|
810 | int (* pfnUnmap)(PKLDRMOD pMod);
|
---|
811 | /** @copydoc kLdrModAllocTLS */
|
---|
812 | int (* pfnAllocTLS)(PKLDRMOD pMod);
|
---|
813 | /** @copydoc kLdrModFreeTLS */
|
---|
814 | void (* pfnFreeTLS)(PKLDRMOD pMod);
|
---|
815 | /** @copydoc kLdrModReload */
|
---|
816 | int (* pfnReload)(PKLDRMOD pMod);
|
---|
817 | /** @copydoc kLdrModFixupMapping */
|
---|
818 | int (* pfnFixupMapping)(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
819 | /** @copydoc kLdrModCallInit */
|
---|
820 | int (* pfnCallInit)(PKLDRMOD pMod);
|
---|
821 | /** @copydoc kLdrModCallTerm */
|
---|
822 | int (* pfnCallTerm)(PKLDRMOD pMod);
|
---|
823 | /** @copydoc kLdrModCallThread */
|
---|
824 | int (* pfnCallThread)(PKLDRMOD pMod, unsigned fAttachingOrDetaching);
|
---|
825 | /** @copydoc kLdrModSize */
|
---|
826 | size_t (* pfnSize)(PKLDRMOD pMod);
|
---|
827 | /** @copydoc kLdrModGetBits */
|
---|
828 | int (* pfnGetBits)(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
829 | /** @copydoc kLdrModRelocateBits */
|
---|
830 | int (* pfnRelocateBits)(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
|
---|
831 | PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
832 | /** Dummy which should be assigned a non-zero value. */
|
---|
833 | uint32_t uEndOfStructure;
|
---|
834 | } KLDRMODOPS;
|
---|
835 |
|
---|
836 |
|
---|
837 | /** @} */
|
---|
838 |
|
---|
839 |
|
---|
840 |
|
---|
841 |
|
---|
842 | /** @defgroup grp_kLdrDyld kLdrDyld - The dynamic loader
|
---|
843 | * @{ */
|
---|
844 |
|
---|
845 | /** The handle to a dynamic loader module. */
|
---|
846 | typedef struct KLDRDYLDMOD *HKLDRMOD;
|
---|
847 | /** Pointer to the handle to a dynamic loader module. */
|
---|
848 | typedef HKLDRMOD *PHKLDRMOD;
|
---|
849 | /** NIL handle value. */
|
---|
850 | #define NIL_HKLDRMOD ((HKLDRMOD)0)
|
---|
851 |
|
---|
852 |
|
---|
853 | /**
|
---|
854 | * File search method.
|
---|
855 | *
|
---|
856 | * In addition to it's own way of finding files, kLdr emulates
|
---|
857 | * the methods employed by the most popular systems.
|
---|
858 | */
|
---|
859 | typedef enum KLDRDYLDSEARCH
|
---|
860 | {
|
---|
861 | /** The usual invalid file search method. */
|
---|
862 | KLDRDYLD_SEARCH_INVALID = 0,
|
---|
863 | /** Uses the kLdr file search method.
|
---|
864 | * @todo invent me. */
|
---|
865 | KLDRDYLD_SEARCH_KLDR,
|
---|
866 | /** Use the emulation closest to the host system. */
|
---|
867 | KLDRDYLD_SEARCH_HOST,
|
---|
868 | /** Emulate the OS/2 file search method.
|
---|
869 | * On non-OS/2 systems, BEGINLIBPATH, LIBPATH, ENDLIBPATH and LIBPATHSTRICT are
|
---|
870 | * taken form the environment. */
|
---|
871 | KLDRDYLD_SEARCH_OS2,
|
---|
872 | /** Emulate the standard window file search method. */
|
---|
873 | KLDRDYLD_SEARCH_WINDOWS,
|
---|
874 | /** Emulate the alternative window file search method. */
|
---|
875 | KLDRDYLD_SEARCH_WINDOWS_ALTERED,
|
---|
876 | /** Emulate the most common UNIX file search method. */
|
---|
877 | KLDRDYLD_SEARCH_UNIX_COMMON,
|
---|
878 | /** End of the valid file search method values. */
|
---|
879 | KLDRDYLD_SEARCH_END,
|
---|
880 | /** Hack to blow the type up to 32-bit. */
|
---|
881 | KLDRDYLD_SEARCH_32BIT_HACK = 0x7fffffff
|
---|
882 | } KLDRDYLDSEARCH;
|
---|
883 |
|
---|
884 | /** @name kLdrDyldLoad and kLdrDyldFindByName flags.
|
---|
885 | * @{ */
|
---|
886 | /** The symbols in the module should be loaded into the global unix namespace.
|
---|
887 | * If not specified, the symbols are local and can only be referenced directly. */
|
---|
888 | #define KLDRYDLD_LOAD_FLAGS_GLOBAL_SYMBOLS 0x00000001
|
---|
889 | /** The module shouldn't be found by a global module search.
|
---|
890 | * If not specified, the module can be found by unspecified module searches,
|
---|
891 | * typical used when loading import/dep modules. */
|
---|
892 | #define KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE 0x00000002
|
---|
893 | /** Do a recursive initialization calls instead of defering them to the outermost call. */
|
---|
894 | #define KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT 0x00000004
|
---|
895 | /** We're loading the executable module.
|
---|
896 | * Internal flag which will be rejected by kLdrDyldLoad. */
|
---|
897 | #define KLDRDYLD_LOAD_FLAGS_EXECUTABLE 0x40000000
|
---|
898 | /** @} */
|
---|
899 |
|
---|
900 |
|
---|
901 | int kLdrDyldLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
|
---|
902 | unsigned fFlags, PHKLDRMOD phMod, char *pszErr, size_t cchErr);
|
---|
903 | int kLdrDyldUnload(HKLDRMOD hMod);
|
---|
904 | int kLdrDyldFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
|
---|
905 | unsigned fFlags, PHKLDRMOD phMod);
|
---|
906 | int kLdrDyldFindByAddress(uintptr_t Address, PHKLDRMOD phMod, uint32_t *piSegment, uintptr_t *poffSegment);
|
---|
907 | int kLdrDyldGetName(HKLDRMOD hMod, char *pszName, size_t cchName);
|
---|
908 | int kLdrDyldGetFilename(HKLDRMOD hMod, char *pszFilename, size_t cchFilename);
|
---|
909 | int kLdrDyldQuerySymbol(HKLDRMOD hMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, uintptr_t *pValue, uint32_t *pfKind);
|
---|
910 |
|
---|
911 | /** @name OS/2 like API
|
---|
912 | * @{ */
|
---|
913 | int kLdrDosLoadModule(char *pszObject, size_t cbObject, const char *pszModule, PHKLDRMOD phMod);
|
---|
914 | int kLdrDosFreeModule(HKLDRMOD hMod);
|
---|
915 | int kLdrDosQueryModuleHandle(const char *pszModname, PHKLDRMOD phMod);
|
---|
916 | int kLdrDosQueryModuleName(HKLDRMOD hMod, size_t cchName, char *pszName);
|
---|
917 | int kLdrDosQueryProcAddr(HKLDRMOD hMod, uint32_t iOrdinal, const char *pszProcName, void **ppvProcAddr);
|
---|
918 | int kLdrDosQueryProcType(HKLDRMOD hMod, uint32_t iOrdinal, const char *pszProcName, uint32_t *pfProcType);
|
---|
919 | int kLdrDosQueryModFromEIP(PHKLDRMOD phMod, uint32_t *piObject, size_t cbName, char *pszName, uintptr_t *poffObject, uintptr_t ulEIP);
|
---|
920 | int kLdrDosReplaceModule(const char *pszOldModule, const char *pszNewModule, const char *pszBackupModule);
|
---|
921 | int kLdrDosGetResource(HKLDRMOD hMod, uint32_t idType, uint32_t idName, void **pvResAddr);
|
---|
922 | int kLdrDosQueryResourceSize(HKLDRMOD hMod, uint32_t idTypeID, uint32_t idName, uint32_t *pcb);
|
---|
923 | int kLdrDosFreeResource(void *pvResAddr);
|
---|
924 | /** @} */
|
---|
925 |
|
---|
926 | /** @name POSIX like API
|
---|
927 | * @{ */
|
---|
928 | HKLDRMOD kLdrDlOpen(const char *pszLibrary, int fFlags);
|
---|
929 | const char *kLdrDlError(void);
|
---|
930 | void * kLdrDlSym(HKLDRMOD hMod, const char *pszSymbol);
|
---|
931 | int kLdrDlClose(HKLDRMOD hMod);
|
---|
932 | /** @} */
|
---|
933 |
|
---|
934 | /** @name Win32 like API
|
---|
935 | * @{ */
|
---|
936 | HKLDRMOD kLdrWLoadLibrary(const char *pszFilename);
|
---|
937 | HKLDRMOD kLdrWLoadLibraryEx(const char *pszFilename, void *hFileReserved, uint32_t fFlags);
|
---|
938 | uint32_t kLdrWGetModuleFileName(HKLDRMOD hMod, char *pszModName, size_t cchModName);
|
---|
939 | HKLDRMOD kLdrWGetModuleHandle(const char *pszFilename);
|
---|
940 | int kLdrWGetModuleHandleEx(uint32_t fFlags, const char *pszFilename, HKLDRMOD hMod);
|
---|
941 | void * kLdrWGetProcAddress(HKLDRMOD hMod, const char *pszProcName);
|
---|
942 | uint32_t kLdrWGetDllDirectory(size_t cchDir, char *pszDir);
|
---|
943 | int kLdrWSetDllDirectory(const char *pszDir);
|
---|
944 | int kLdrWFreeLibrary(HKLDRMOD hMod);
|
---|
945 | int kLdrWDisableThreadLibraryCalls(HKLDRMOD hMod);
|
---|
946 |
|
---|
947 | /** @} */
|
---|
948 |
|
---|
949 |
|
---|
950 | /** @name Process Bootstrapping
|
---|
951 | * @{ */
|
---|
952 |
|
---|
953 | /**
|
---|
954 | * Argument package from the stub.
|
---|
955 | */
|
---|
956 | typedef struct KLDREXEARGS
|
---|
957 | {
|
---|
958 | /** Flags. (Currently unused, MBZ.) */
|
---|
959 | uint32_t fFlags;
|
---|
960 | /** The search method to use when loading this executable. */
|
---|
961 | KLDRDYLDSEARCH enmSearch;
|
---|
962 | /** The executable file that the stub is supposed to load. */
|
---|
963 | char szExecutable[260];
|
---|
964 | /** The default prefix used when searching for DLLs. */
|
---|
965 | char szDefPrefix[16];
|
---|
966 | /** The default suffix used when searching for DLLs. */
|
---|
967 | char szDefSuffix[16];
|
---|
968 | /** The LD_LIBRARY_PATH prefix for the process.. */
|
---|
969 | char szLibPath[4096 - sizeof(uint32_t) - sizeof(KLDRDYLDSEARCH) - 16 - 16 - 260];
|
---|
970 | } KLDREXEARGS, *PKLDREXEARGS;
|
---|
971 |
|
---|
972 | void kLdrLoadExe(PKLDREXEARGS pArgs, void *pvOS);
|
---|
973 |
|
---|
974 | /** @} */
|
---|
975 |
|
---|
976 | /** @} */
|
---|
977 |
|
---|
978 |
|
---|
979 | /** @defgroup grp_kLdrErr kLdr Status Codes
|
---|
980 | * kLdr uses a mix of native status codes and it's own status codes.
|
---|
981 | * A status code of 0 means success, all other status codes means failure.
|
---|
982 | * @{
|
---|
983 | */
|
---|
984 | #ifdef __OS2__
|
---|
985 | # define KLDR_ERR_BASE 0x7face000
|
---|
986 | #elif defined(__WIN__)
|
---|
987 | # define KLDR_ERR_BASE 0x7face000
|
---|
988 | #else
|
---|
989 | # error "port me"
|
---|
990 | #endif
|
---|
991 | /** The image format is unknown. */
|
---|
992 | #define KLDR_ERR_UNKNOWN_FORMAT (KLDR_ERR_BASE + 0)
|
---|
993 | /** The MZ image format isn't supported by this kLdr build. */
|
---|
994 | #define KLDR_ERR_MZ_NOT_SUPPORTED (KLDR_ERR_BASE + 1)
|
---|
995 | /** The NE image format isn't supported by this kLdr build. */
|
---|
996 | #define KLDR_ERR_NE_NOT_SUPPORTED (KLDR_ERR_BASE + 2)
|
---|
997 | /** The LX image format isn't supported by this kLdr build. */
|
---|
998 | #define KLDR_ERR_LX_NOT_SUPPORTED (KLDR_ERR_BASE + 3)
|
---|
999 | /** The LE image format isn't supported by this kLdr build. */
|
---|
1000 | #define KLDR_ERR_LE_NOT_SUPPORTED (KLDR_ERR_BASE + 4)
|
---|
1001 | /** The PE image format isn't supported by this kLdr build. */
|
---|
1002 | #define KLDR_ERR_PE_NOT_SUPPORTED (KLDR_ERR_BASE + 5)
|
---|
1003 | /** The ELF image format isn't supported by this kLdr build. */
|
---|
1004 | #define KLDR_ERR_ELF_NOT_SUPPORTED (KLDR_ERR_BASE + 6)
|
---|
1005 | /** The mach-o image format isn't supported by this kLdr build. */
|
---|
1006 | #define KLDR_ERR_MACHO_NOT_SUPPORTED (KLDR_ERR_BASE + 7)
|
---|
1007 | /** The mach-o image format isn't supported by this kLdr build. */
|
---|
1008 | #define KLDR_ERR_AOUT_NOT_SUPPORTED (KLDR_ERR_BASE + 8)
|
---|
1009 |
|
---|
1010 | /** Invalid parameter to a kLdr API. */
|
---|
1011 | #define KLDR_ERR_INVALID_PARAMETER (KLDR_ERR_BASE + 32)
|
---|
1012 | /** Invalid handle parameter to a kLdr API. */
|
---|
1013 | #define KLDR_ERR_INVALID_HANDLE (KLDR_ERR_BASE + 33)
|
---|
1014 | /** The module wasn't loaded dynamically. */
|
---|
1015 | #define KLDR_ERR_NOT_LOADED_DYNAMICALLY (KLDR_ERR_BASE + 34)
|
---|
1016 | /** The module wasn't found. */
|
---|
1017 | #define KLDR_ERR_MODULE_NOT_FOUND (KLDR_ERR_BASE + 35)
|
---|
1018 | /** A prerequisit module wasn't found. */
|
---|
1019 | #define KLDR_ERR_PREREQUISITE_MODULE_NOT_FOUND (KLDR_ERR_BASE + 36)
|
---|
1020 | /** The module is being terminated and can therefore not be loaded. */
|
---|
1021 | #define KLDR_ERR_MODULE_TERMINATING (KLDR_ERR_BASE + 37)
|
---|
1022 | /** A prerequisit module is being terminated and can therefore not be loaded. */
|
---|
1023 | #define KLDR_ERR_PREREQUISITE_MODULE_TERMINATING (KLDR_ERR_BASE + 38)
|
---|
1024 | /** The module initialization failed. */
|
---|
1025 | #define KLDR_ERR_MODULE_INIT_FAILED (KLDR_ERR_BASE + 39)
|
---|
1026 | /** The initialization of a prerequisite module failed. */
|
---|
1027 | #define KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED (KLDR_ERR_BASE + 40)
|
---|
1028 | /** The module has already failed initialization and can't be attempted reloaded until
|
---|
1029 | * after we've finished garbage collection. */
|
---|
1030 | #define KLDR_ERR_MODULE_INIT_FAILED_ALREADY (KLDR_ERR_BASE + 41)
|
---|
1031 | /** A prerequisite module has already failed initialization and can't be attempted
|
---|
1032 | * reloaded until after we've finished garbage collection. */
|
---|
1033 | #define KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED_ALREADY (KLDR_ERR_BASE + 42)
|
---|
1034 | /** Prerequisite recursed too deeply. */
|
---|
1035 | #define KLDR_ERR_PREREQUISITE_RECURSED_TOO_DEEPLY (KLDR_ERR_BASE + 43)
|
---|
1036 | /** Failed to allocate the main stack. */
|
---|
1037 | #define KLDR_ERR_MAIN_STACK_ALLOC_FAILED (KLDR_ERR_BASE + 44)
|
---|
1038 | /** Buffer overflow. */
|
---|
1039 | #define KLDR_ERR_BUFFER_OVERFLOW (KLDR_ERR_BASE + 45)
|
---|
1040 | /** The specified ARCH+CPU isn't compatible with image. */
|
---|
1041 | #define KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE (KLDR_ERR_BASE + 45)
|
---|
1042 | /** Symbol not found. */
|
---|
1043 | #define KLDR_ERR_SYMBOL_NOT_FOUND (KLDR_ERR_BASE + 46)
|
---|
1044 | /** A forward symbol was encountered but the caller didn't provide any means to resolve it. */
|
---|
1045 | #define KLDR_ERR_FORWARDER_SYMBOL (KLDR_ERR_BASE + 47)
|
---|
1046 | /** Encountered a bad fixup. */
|
---|
1047 | #define KLDR_ERR_BAD_FIXUP (KLDR_ERR_BASE + 48)
|
---|
1048 | /** A memory allocation failed. */
|
---|
1049 | #define KLDR_ERR_NO_MEMORY (KLDR_ERR_BASE + 49)
|
---|
1050 | /** The import ordinal was out of bounds. */
|
---|
1051 | #define KLDR_ERR_IMPORT_ORDINAL_OUT_OF_BOUNDS (KLDR_ERR_BASE + 50)
|
---|
1052 | /** A forwarder chain was too long. */
|
---|
1053 | #define KLDR_ERR_TOO_LONG_FORWARDER_CHAIN (KLDR_ERR_BASE + 51)
|
---|
1054 |
|
---|
1055 | /** @name kLdrModPE status codes
|
---|
1056 | * @{ */
|
---|
1057 | #define KLDR_ERR_BASE_PE (KLDR_ERR_BASE + 96)
|
---|
1058 | /** The machine isn't supported by the interpreter. */
|
---|
1059 | #define KLDR_ERR_PE_UNSUPPORTED_MACHINE (KLDR_ERR_BASE_PE + 0)
|
---|
1060 | /** The file handler isn't valid. */
|
---|
1061 | #define KLDR_ERR_PE_BAD_FILE_HEADER (KLDR_ERR_BASE_PE + 1)
|
---|
1062 | /** The the optional headers isn't valid. */
|
---|
1063 | #define KLDR_ERR_PE_BAD_OPTIONAL_HEADER (KLDR_ERR_BASE_PE + 2)
|
---|
1064 | /** One of the section headers aren't valid. */
|
---|
1065 | #define KLDR_ERR_PE_BAD_SECTION_HEADER (KLDR_ERR_BASE_PE + 3)
|
---|
1066 | /** Bad forwarder entry. */
|
---|
1067 | #define KLDR_ERR_PE_BAD_FORWARDER (KLDR_ERR_BASE_PE + 4)
|
---|
1068 | /** Forwarder module not found in the import descriptor table. */
|
---|
1069 | #define KLDR_ERR_PE_FORWARDER_IMPORT_NOT_FOUND (KLDR_ERR_BASE_PE + 5)
|
---|
1070 | /** @} */
|
---|
1071 |
|
---|
1072 |
|
---|
1073 | /** @} */
|
---|
1074 |
|
---|
1075 |
|
---|
1076 | #ifdef __cplusplus
|
---|
1077 | }
|
---|
1078 | #endif
|
---|
1079 |
|
---|
1080 | #endif
|
---|
1081 |
|
---|