[2826] | 1 | /* $Id: kLdr.h 2826 2006-10-22 15:58:55Z 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 |
|
---|
[2825] | 55 | /** Pointer to a file provider instance core. */
|
---|
| 56 | typedef struct KLDRRDR *PKLDRRDR;
|
---|
| 57 | /** Pointer to a file provider instance core pointer. */
|
---|
| 58 | typedef struct KLDRRDR **PPKLDRRDR;
|
---|
| 59 |
|
---|
[2821] | 60 | /**
|
---|
[2825] | 61 | * File provider instance operations.
|
---|
| 62 | */
|
---|
| 63 | typedef struct KLDRRDROPS
|
---|
| 64 | {
|
---|
| 65 | /** The name of this file provider. */
|
---|
| 66 | const char *pszName;
|
---|
| 67 | /** Pointer to the next file provider. */
|
---|
| 68 | const struct KLDRRDROPS *pNext;
|
---|
| 69 |
|
---|
| 70 | /** Try create a new file provider instance.
|
---|
| 71 | *
|
---|
| 72 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 73 | * @param ppRdr Where to store the file provider instance.
|
---|
| 74 | * @param pszFilename The filename to open.
|
---|
| 75 | */
|
---|
| 76 | int (* pfnCreate)( PPKLDRRDR ppRdr, const char *pszFilename);
|
---|
| 77 | /** Destroy the file provider instance.
|
---|
| 78 | *
|
---|
| 79 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 80 | * On failure, the file provider instance will be in an indeterminate state - don't touch it!
|
---|
| 81 | * @param pRdr The file provider instance.
|
---|
| 82 | */
|
---|
| 83 | int (* pfnDestroy)( PKLDRRDR pRdr);
|
---|
| 84 | /** Read bits from the file.
|
---|
| 85 | *
|
---|
| 86 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 87 | * @param pRdr The file provider instance.
|
---|
| 88 | * @param pvBuf Where to put the bits.
|
---|
| 89 | * @param cb The number of bytes to read.
|
---|
| 90 | * @param off Where to start reading.
|
---|
| 91 | */
|
---|
| 92 | int (* pfnRead)( PKLDRRDR pRdr, void *pvBuf, size_t cb, off_t off);
|
---|
| 93 | /** Map all the file bits into memory (read only).
|
---|
| 94 | *
|
---|
| 95 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 96 | * @param pRdr The file provider instance.
|
---|
| 97 | * @param ppvBits Where to store the address of the mapping.
|
---|
| 98 | * The size can be obtained using pfnSize.
|
---|
| 99 | */
|
---|
| 100 | int (* pfnAllMap)( PKLDRRDR pRdr, const void **ppvBits);
|
---|
| 101 | /** Unmap a file bits mapping obtained by KLDRRDROPS::pfnAllMap.
|
---|
| 102 | *
|
---|
| 103 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 104 | * @param pRdr The file provider instance.
|
---|
| 105 | * @param pvBits The mapping address.
|
---|
| 106 | */
|
---|
| 107 | int (* pfnAllUnmap)(PKLDRRDR pRdr, const void *pvBits);
|
---|
| 108 | /** @todo generic mmap/MapViewOfFile */
|
---|
| 109 | /** Get the file size.
|
---|
| 110 | *
|
---|
| 111 | * @returns The file size. Returns -1 on failure.
|
---|
| 112 | * @param pRdr The file provider instance.
|
---|
| 113 | */
|
---|
| 114 | off_t (* pfnSize)( PKLDRRDR pRdr);
|
---|
| 115 | /** Get the file pointer offset.
|
---|
| 116 | *
|
---|
| 117 | * @returns The file pointer offset. Returns -1 on failure.
|
---|
| 118 | * @param pRdr The file provider instance.
|
---|
| 119 | */
|
---|
| 120 | off_t (* pfnTell)( PKLDRRDR pRdr);
|
---|
| 121 | /** Get the file name.
|
---|
| 122 | *
|
---|
| 123 | * @returns The file size. Returns -1 on failure.
|
---|
| 124 | * @param pRdr The file provider instance.
|
---|
| 125 | */
|
---|
| 126 | const char * (* pfnName)(PKLDRRDR pRdr);
|
---|
| 127 | } KLDRRDROPS;
|
---|
| 128 | /** Pointer to file provider operations. */
|
---|
| 129 | typedef KLDRRDROPS *PKLDRRDROPS;
|
---|
| 130 | /** Pointer to const file provider operations. */
|
---|
| 131 | typedef const KLDRRDROPS *PCKLDRRDROPS;
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * File provider instance core.
|
---|
| 136 | */
|
---|
| 137 | typedef struct KLDRRDR
|
---|
| 138 | {
|
---|
| 139 | /** Pointer to the file provider operations. */
|
---|
| 140 | PCKLDRRDROPS pOps;
|
---|
| 141 | } KLDRRDR;
|
---|
| 142 |
|
---|
| 143 | void kLdrRdrAddProvider(PKLDRRDROPS pAdd);
|
---|
| 144 |
|
---|
| 145 | int kLdrRdrOpen( PPKLDRRDR ppRdr, const char *pszFilename);
|
---|
| 146 | int kLdrRdrClose( PKLDRRDR pRdr);
|
---|
| 147 | int kLdrRdrRead( PKLDRRDR pRdr, void *pvBuf, size_t cb, off_t off);
|
---|
| 148 | int kLdrRdrAllMap( PKLDRRDR pRdr, const void **ppvBits);
|
---|
| 149 | int kLdrRdrAllUnmap(PKLDRRDR pRdr, const void *pvBits);
|
---|
| 150 | off_t kLdrRdrSize( PKLDRRDR pRdr);
|
---|
| 151 | off_t kLdrRdrTell( PKLDRRDR pRdr);
|
---|
| 152 | const char *kLdrRdrName(PKLDRRDR pRdr);
|
---|
| 153 |
|
---|
| 154 | /** @} */
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | /** @defgroup grp_kLdrMod kLdrMod - The executable image intepreter
|
---|
| 159 | * @{ */
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
[2821] | 162 | * Loader segment.
|
---|
| 163 | */
|
---|
| 164 | typedef struct KLDRSEG
|
---|
| 165 | {
|
---|
| 166 | /** The segment load address. */
|
---|
| 167 | void *pv;
|
---|
| 168 | /** The size of the segment. */
|
---|
| 169 | size_t cb;
|
---|
| 170 | /** The segment is readable. */
|
---|
| 171 | uint32_t fRead : 1;
|
---|
| 172 | /** The segment is writable. */
|
---|
| 173 | uint32_t fWrite : 1;
|
---|
| 174 | /** The segment is executable. */
|
---|
| 175 | uint32_t fExecute : 1;
|
---|
| 176 | /** Reserved for future use. */
|
---|
| 177 | uint32_t fReserved : 29;
|
---|
| 178 | /** Reserved for future use. */
|
---|
| 179 | uint32_t u32Reserved;
|
---|
| 180 | } KLDRSEG, *PKLDRSEG;
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 | /**
|
---|
| 184 | * Loader module type.
|
---|
| 185 | */
|
---|
| 186 | typedef enum KLDRTYPE
|
---|
| 187 | {
|
---|
| 188 | /** The usual invalid 0 type. */
|
---|
| 189 | KLDRTYPE_INVALID = 0,
|
---|
| 190 | /** The native OS loader. */
|
---|
| 191 | KLDRTYPE_NATIVE,
|
---|
| 192 | /** The LX loader. */
|
---|
| 193 | KLDRTYPE_LX,
|
---|
| 194 | /** The end type (not included). */
|
---|
| 195 | KLDRTYPE_END,
|
---|
| 196 | /** Hack to blow the type up to 32-bit. */
|
---|
| 197 | KLDRTYPE_32BIT_HACK = 0x7fffffff
|
---|
| 198 | } KLDRTYPE;
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | /**
|
---|
| 202 | * Loader module.
|
---|
| 203 | */
|
---|
| 204 | typedef struct KLDRMOD
|
---|
| 205 | {
|
---|
[2825] | 206 | /** Magic number. */
|
---|
| 207 | uint32_t u32Magic;
|
---|
| 208 | /** The type of module this is. */
|
---|
| 209 | KLDRTYPE enmType;
|
---|
[2821] | 210 | /** The module data. */
|
---|
| 211 | void *pvData;
|
---|
| 212 | /** The filename length (bytes). */
|
---|
| 213 | uint32_t cchFilename;
|
---|
| 214 | /** The filename. */
|
---|
| 215 | const char *pszFilename;
|
---|
| 216 | /** The module name. */
|
---|
| 217 | const char *pszName;
|
---|
| 218 | /** The module name length (bytes). */
|
---|
| 219 | uint32_t cchName;
|
---|
| 220 | /** The number of segments in the module. */
|
---|
| 221 | uint32_t cSegments;
|
---|
| 222 | /** Segments. (variable size, can be zero) */
|
---|
| 223 | KLDRSEG aSegments[1];
|
---|
| 224 | } KLDRMOD, *PKLDRMOD, **PPKLDRMOD;
|
---|
| 225 |
|
---|
| 226 |
|
---|
[2825] | 227 | int kLdrModOpen(const char *pszFilename, PPKLDRMOD ppMod);
|
---|
| 228 | int kLdrModClose(PKLDRMOD pMod);
|
---|
| 229 | int kLdrModGetSymbol(PKLDRMOD pMod, const void *pvBits, uintmax_t BaseAddress, const char *pszSymbol, uintmax_t *pValue, unsigned *pfType);
|
---|
| 230 | int kLdrModGetSymbol(PKLDRMOD pMod, const void *pvBits, uintmax_t BaseAddress, const char *pszSymbol, uintmax_t *pValue, unsigned *pfType);
|
---|
| 231 |
|
---|
| 232 | size_t kLdrModSize(PKLDRMOD pMod);
|
---|
| 233 |
|
---|
| 234 | typedef int FNKLDRMODGETIMPORT(PKLDRMOD pMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, uintmax_t *pValue, void *pvUser);
|
---|
| 235 | typedef FNKLDRMODGETIMPORT *PFNKLDRMODGETIMPORT;
|
---|
| 236 | int kLdrModGetBits(PKLDRMOD pMod, void *pvBits, uintmax_t BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
| 237 | int kLdrModRelocate(PKLDRMOD pMod, void *pvBits, uintmax_t NewBaseAddress, uintmax_t OldBaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
| 238 |
|
---|
| 239 | typedef int FNKLDRMODENUMSYMS(PKLDRMOD pMod, const char *pszSymbol, unsigned uSymbol, uintmax_t Value, void *pvUser);
|
---|
| 240 | typedef FNKLDRMODENUMSYMS *PFNKLDRMODENUMSYMS;
|
---|
| 241 | int kLdrModEnumSymbols(PKLDRMOD pMod, unsigned fFlags, const void *pvBits, uintmax_t BaseAddress, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser);
|
---|
| 242 | /** @name kLdrModEnumSymbols flags.
|
---|
| 243 | * @{ */
|
---|
| 244 | /** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
|
---|
| 245 | #define KLDRMOD_ENUM_SYMBOL_FLAGS_ALL 0x00000001
|
---|
| 246 | /** @} */
|
---|
| 247 |
|
---|
| 248 | /** @} */
|
---|
| 249 |
|
---|
| 250 |
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | /** @defgroup grp_kLdrDy kLdrDy - The dynamic loader
|
---|
| 254 | * @{ */
|
---|
| 255 |
|
---|
| 256 | /** The h*/
|
---|
| 257 | typedef struct KLDRDY *PKLDRDY;
|
---|
| 258 |
|
---|
| 259 | /*
|
---|
| 260 | int kLdrLoadDll(const char *pszFilename, unsigned fFlags, unsigned long *pvmod);
|
---|
| 261 | */
|
---|
| 262 |
|
---|
[2821] | 263 | /** @name Process Bootstrapping
|
---|
| 264 | * @{ */
|
---|
| 265 |
|
---|
| 266 | /**
|
---|
| 267 | * Argument package from the stub.
|
---|
| 268 | */
|
---|
| 269 | typedef struct KLDREXEARGS
|
---|
| 270 | {
|
---|
| 271 | /** Flags. (Currently unused, MBZ.) */
|
---|
| 272 | uint32_t fFlags;
|
---|
| 273 | /** The executable file that the stub is supposed to load. */
|
---|
| 274 | char szExecutable[260];
|
---|
| 275 | /** The LD_LIBRARY_PATH prefix for the process.. */
|
---|
| 276 | char szLibPath[4096 - 260 - sizeof(uint32_t)];
|
---|
| 277 | } KLDREXEARGS, *PKLDREXEARGS;
|
---|
| 278 |
|
---|
| 279 | void kLdrLoadExe(PKLDREXEARGS pArgs);
|
---|
| 280 | /** @} */
|
---|
| 281 |
|
---|
| 282 | /** @} */
|
---|
| 283 |
|
---|
| 284 | #ifdef __cplusplus
|
---|
| 285 | }
|
---|
| 286 | #endif
|
---|
| 287 |
|
---|
| 288 | #endif
|
---|
| 289 |
|
---|