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