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