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