[2826] | 1 | /* $Id: kLdrInternal.h 2836 2006-10-26 03:58:53Z bird $ */
|
---|
[2825] | 2 | /** @file
|
---|
| 3 | *
|
---|
| 4 | * kLdr - The Dynamic Loader, internal header.
|
---|
| 5 | *
|
---|
| 6 | * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@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 |
|
---|
| 28 | #ifndef __kLdrInternal_h__
|
---|
| 29 | #define __kLdrInternal_h__
|
---|
| 30 |
|
---|
| 31 | #ifdef __cplusplus
|
---|
| 32 | extern "C" {
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
[2827] | 35 | /* ignore definitions in winnt.h */
|
---|
| 36 | #undef IMAGE_DOS_SIGNATURE
|
---|
| 37 | #undef IMAGE_NT_SIGNATURE
|
---|
| 38 |
|
---|
| 39 | /** @name Signatures we know
|
---|
| 40 | * @{ */
|
---|
| 41 | /** ELF signature ("\x7fELF"). */
|
---|
| 42 | #define IMAGE_ELF_SIGNATURE KLDRHLP_LE2H_U32(0x7f | ('E' << 8) | ((uint32_t)'L' << 16) | ((uint32_t)'F' << 24))
|
---|
| 43 | /** PE signature ("PE\0\0"). */
|
---|
| 44 | #define IMAGE_NT_SIGNATURE KLDRHLP_LE2H_U32('P' | ('E' << 8))
|
---|
| 45 | /** LX signature ("LX") */
|
---|
| 46 | #define IMAGE_LX_SIGNATURE KLDRHLP_LE2H_U16('L' | ('X' << 8))
|
---|
| 47 | /** LE signature ("LE") */
|
---|
| 48 | #define IMAGE_LE_SIGNATURE KLDRHLP_LE2H_U16('L' | ('E' << 8))
|
---|
| 49 | /** NE signature ("NE") */
|
---|
| 50 | #define IMAGE_NE_SIGNATURE KLDRHLP_LE2H_U16('N' | ('E' << 8))
|
---|
| 51 | /** MZ signature ("MZ"). */
|
---|
| 52 | #define IMAGE_DOS_SIGNATURE KLDRHLP_LE2H_U16('M' | ('Z' << 8))
|
---|
| 53 | /** @} */
|
---|
| 54 |
|
---|
[2833] | 55 | /** @defgroup grp_kLdrInternal Internals
|
---|
| 56 | * @internal
|
---|
| 57 | * @{
|
---|
| 58 | */
|
---|
[2827] | 59 |
|
---|
[2833] | 60 |
|
---|
[2825] | 61 | /** Native file provider operations. */
|
---|
| 62 | extern const KLDRRDROPS g_kLdrRdrFileOps;
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
| 66 | * The state of a dynamic loader module.
|
---|
| 67 | */
|
---|
| 68 | typedef enum KLDRSTATE
|
---|
| 69 | {
|
---|
| 70 | /** The usual invalid 0 enum. */
|
---|
| 71 | KLDRSTATE_INVALID = 0,
|
---|
[2836] | 72 | /** The module has just been opened and has no references.
|
---|
| 73 | * Prev state: -
|
---|
| 74 | * Next state: MAPPED, DESTROYED
|
---|
| 75 | */
|
---|
[2825] | 76 | KLDRSTATE_OPEN,
|
---|
[2836] | 77 | /** The module been mapped.
|
---|
| 78 | * Prev state: OPEN
|
---|
| 79 | * Next state: LOADED_DEPS, DESTROYED
|
---|
| 80 | */
|
---|
| 81 | KLDRSTATE_MAPPED,
|
---|
| 82 | /** The immediate dependencies has been loaded.
|
---|
| 83 | * Prev state: MAPPED
|
---|
| 84 | * Next state: FIXED_UP, DESTROYED
|
---|
| 85 | */
|
---|
| 86 | KLDRSTATE_LOADED_DEPS,
|
---|
| 87 | /** Fixups has been applied.
|
---|
| 88 | * Prev state: LOADED_DEPS
|
---|
| 89 | * Next state: PENDING_INIT, DESTROYED
|
---|
| 90 | */
|
---|
| 91 | KLDRSTATE_FIXED_UP,
|
---|
| 92 | /** Pending initialization.
|
---|
| 93 | * (The loader can now be in a re-entrant mode.)
|
---|
| 94 | * Prev state: FIXED_UP
|
---|
| 95 | * Next state: PENDING_INITIALIZATION, PENDING_GC
|
---|
| 96 | */
|
---|
| 97 | KLDRSTATE_PENDING_INITIALIZATION,
|
---|
| 98 | /** Initializing.
|
---|
| 99 | * (The loader is now in a re-entrant mode.)
|
---|
| 100 | * Prev state: PENDING_INITIALIZATION
|
---|
| 101 | * Next state: GOOD, PENDING_GC
|
---|
| 102 | */
|
---|
| 103 | KLDRSTATE_INITIALIZING,
|
---|
| 104 | /** The module has been successfully loaded and initialized.
|
---|
| 105 | * (The loader can still be in a re-entrant mode.)
|
---|
| 106 | * Prev state: INITIALIZING
|
---|
| 107 | * Next state: PENDING_TERMINATION
|
---|
| 108 | * Re-entrant.
|
---|
| 109 | */
|
---|
| 110 | KLDRSTATE_GOOD,
|
---|
| 111 | /** Pending termination, reference count is 0.
|
---|
| 112 | * (The loader can now be in a re-entrant mode.)
|
---|
| 113 | * Prev state: GOOD
|
---|
| 114 | * Next state: TERMINATING, GOOD
|
---|
| 115 | */
|
---|
| 116 | KLDRSTATE_PENDING_TERMINATION,
|
---|
| 117 | /** Terminating, reference count is still 0.
|
---|
| 118 | * (The loader is now in a re-entrant mode, but loading is a bit restricted.)
|
---|
| 119 | * Prev state: PENDING_TERMINATION
|
---|
| 120 | * Next state: PENDING_GC
|
---|
| 121 | */
|
---|
| 122 | KLDRSTATE_TERMINATING,
|
---|
| 123 | /** Pending garbage collection.
|
---|
| 124 | * (The loader can still be in a re-entrant mode.)
|
---|
| 125 | * Prev state: TERMINATING, INITIALIZING, PENDING_INITIALIZATION
|
---|
| 126 | * Next state: GC, RELOADING
|
---|
| 127 | */
|
---|
| 128 | KLDRSTATE_PENDING_GC,
|
---|
| 129 | /** Being garbage collected.
|
---|
| 130 | * (The loader can still be in a re-entrant mode.)
|
---|
| 131 | * Prev state: PENDING_GC
|
---|
| 132 | * Next state: DESTROYED
|
---|
| 133 | */
|
---|
| 134 | KLDRSTATE_GC,
|
---|
| 135 | /** The module is being reloaded after having been scheduled for termination or/and GC.
|
---|
| 136 | * (The loader can still be in a re-entrant mode.)
|
---|
| 137 | * Prev state: PENDING_GC
|
---|
| 138 | * Next state: PENDING_INITIALIZATION
|
---|
| 139 | */
|
---|
| 140 | KLDRSTATE_RELOADING,
|
---|
| 141 | /** The module has been destroyed and is no longer valid.
|
---|
| 142 | * Prev state: GC
|
---|
| 143 | */
|
---|
| 144 | KLDRSTATE_DESTROYED,
|
---|
[2825] | 145 | /** The end of valid states (exclusive) */
|
---|
[2836] | 146 | KLDRSTATE_END = KLDRSTATE_DESTROYED,
|
---|
[2825] | 147 | /** The usual 32-bit blowup. */
|
---|
| 148 | KLDRSTATE_32BIT_HACK = 0x7fffffff
|
---|
| 149 | } KLDRSTATE;
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * Dynamic loader module.
|
---|
| 154 | */
|
---|
[2833] | 155 | typedef struct KLDRDYLDMOD
|
---|
[2825] | 156 | {
|
---|
[2833] | 157 | /** Magic number. */
|
---|
| 158 | uint32_t u32MagicHead;
|
---|
| 159 | /** The module state. */
|
---|
| 160 | KLDRSTATE enmState;
|
---|
[2825] | 161 | /** The module. */
|
---|
| 162 | PKLDRMOD pMod;
|
---|
[2836] | 163 | /** The module handle. */
|
---|
| 164 | HKLDRMOD hMod;
|
---|
[2825] | 165 | /** The number of references. */
|
---|
| 166 | uint32_t cRefs;
|
---|
| 167 | /** The number of dynamic references. */
|
---|
| 168 | uint32_t cDynRefs;
|
---|
[2836] | 169 | /** The number of dynamic load operations in progress.
|
---|
| 170 | * This is used to reject anyone trying to unload a module before the load has
|
---|
| 171 | * been completed. An alternative to this would be to not add the cDynRefs until
|
---|
| 172 | * the load operation has completed... */
|
---|
| 173 | uint32_t cDynRefsInProgress;
|
---|
| 174 | /** Set if this is the executable module.
|
---|
| 175 | * When clear, the module is a shared object or relocatable object. */
|
---|
[2825] | 176 | uint32_t fExecutable : 1;
|
---|
| 177 | /** Global DLL (set) or specific DLL (clear). */
|
---|
[2836] | 178 | uint32_t fGlobalOrSpecific : 1;
|
---|
| 179 | /** Whether the module contains bindable symbols in the global unix namespace. */
|
---|
| 180 | uint32_t fBindable : 1;
|
---|
[2825] | 181 | /** Reserved for future use. */
|
---|
| 182 | uint32_t fReserved : 29;
|
---|
[2836] | 183 | /** The load list linkage. */
|
---|
| 184 | struct
|
---|
| 185 | {
|
---|
| 186 | /** The next module in the list. */
|
---|
| 187 | struct KLDRDYLDMOD *pNext;
|
---|
| 188 | /** The prev module in the list. */
|
---|
| 189 | struct KLDRDYLDMOD *pPrev;
|
---|
| 190 | } Load;
|
---|
| 191 | /** The termination list linkage. */
|
---|
| 192 | struct
|
---|
| 193 | {
|
---|
| 194 | /** The next module in the list. */
|
---|
| 195 | struct KLDRDYLDMOD *pNext;
|
---|
| 196 | /** The prev module in the list. */
|
---|
| 197 | struct KLDRDYLDMOD *pPrev;
|
---|
| 198 | } Term;
|
---|
| 199 | /** The bind order list linkage.
|
---|
| 200 | * The module is not in this list when fBindable is clear. */
|
---|
| 201 | struct
|
---|
| 202 | {
|
---|
| 203 | /** The next module in the list. */
|
---|
| 204 | struct KLDRDYLDMOD *pNext;
|
---|
| 205 | /** The prev module in the list. */
|
---|
| 206 | struct KLDRDYLDMOD *pPrev;
|
---|
| 207 | } Bind;
|
---|
[2833] | 208 | /** Magic number. */
|
---|
| 209 | uint32_t u32MagicTail;
|
---|
| 210 | } KLDRDYLDMOD, *PKLDRDYLDMOD, **PPKLDRDYLDMOD;
|
---|
[2825] | 211 |
|
---|
[2836] | 212 | /** KLDRDYLDMOD magic value. (Fuyumi Soryo) */
|
---|
[2833] | 213 | #define KLDRDYMOD_MAGIC 0x19590106
|
---|
[2825] | 214 |
|
---|
[2833] | 215 | /** Return / crash validation of a module handle argument. */
|
---|
| 216 | #define KLDRDYLD_VALIDATE_HKLDRMOD(hMod) \
|
---|
| 217 | do { \
|
---|
| 218 | if ( (hMod) == NIL_HKLDRMOD \
|
---|
| 219 | || (hMod)->u32MagicHead != KLDRDYMOD_MAGIC \
|
---|
| 220 | || (hMod)->u32MagicTail != KLDRDYMOD_MAGIC) \
|
---|
| 221 | { \
|
---|
| 222 | return KLDR_ERR_INVALID_HANDLE; \
|
---|
| 223 | } \
|
---|
| 224 | } while (0)
|
---|
| 225 |
|
---|
| 226 |
|
---|
[2836] | 227 | int kldrDyldFindNewModule(const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
|
---|
[2835] | 228 | KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod);
|
---|
[2836] | 229 | int kldrDyldFindExistingModule(const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
|
---|
[2835] | 230 | KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod);
|
---|
| 231 |
|
---|
| 232 |
|
---|
| 233 | int kldrDyldModCreate(PKLDRRDR pRdr, PPKLDRDYLDMOD ppMod);
|
---|
| 234 | int kldrDyldModDestroy(PKLDRDYLDMOD pMod);
|
---|
| 235 | int kldrDyldModAddDep(PKLDRDYLDMOD pMod, PKLDRDYLDMOD pModDep);
|
---|
| 236 | int kldrDyldModRemoveDep(PKLDRDYLDMOD pMod, PKLDRDYLDMOD pModDep);
|
---|
| 237 | int kldrDyldModDynamicLoad(PKLDRDYLDMOD pMod);
|
---|
| 238 | int kldrDyldModDynamicUnload(PKLDRDYLDMOD pMod);
|
---|
| 239 | int kldrDyldModMarkGlobal(PKLDRDYLDMOD pMod);
|
---|
| 240 | int kldrDyldModMarkSpecific(PKLDRDYLDMOD pMod);
|
---|
| 241 | int kldrDyldModSetBindable(PKLDRDYLDMOD pMod);
|
---|
| 242 | int kldrDyldModClearBindable(PKLDRDYLDMOD pMod);
|
---|
| 243 | int kldrDyldModSetDeepBindable(PKLDRDYLDMOD pMod);
|
---|
| 244 | int kldrDyldModClearDeepBindable(PKLDRDYLDMOD pMod);
|
---|
| 245 | int kldrDyldModMap(PKLDRDYLDMOD pMod);
|
---|
| 246 | int kldrDyldModUnmap(PKLDRDYLDMOD pMod);
|
---|
| 247 | int kldrDyldModLoadDependencies(PKLDRDYLDMOD pMod);
|
---|
| 248 | int kldrDyldModFixup(PKLDRDYLDMOD pMod);
|
---|
| 249 | int kldrDyldModCallInit(PKLDRDYLDMOD pMod);
|
---|
| 250 | int kldrDyldModCallTerm(PKLDRDYLDMOD pMod);
|
---|
[2836] | 251 | int kldrDyldModAttachThread(PKLDRDYLDMOD pMod);
|
---|
| 252 | int kldrDyldModDetachThread(PKLDRDYLDMOD pMod);
|
---|
[2835] | 253 | int kldrDyldModGetStackInfo(PKLDRDYLDMOD pMod, void *pvStack, size_t *pcbStack, size_t);
|
---|
| 254 | int kldrDyldModStartExe(PKLDRDYLDMOD pMod);
|
---|
| 255 |
|
---|
| 256 | int kldrDyldModGetName(PKLDRDYLDMOD pMod, char *pszName, size_t cchName);
|
---|
| 257 | int kldrDyldModGetFilename(PKLDRDYLDMOD pMod, char *pszFilename, size_t cchFilename);
|
---|
| 258 | int kldrDyldModQuerySymbol(PKLDRDYLDMOD pMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, uintptr_t *pValue, uint32_t *pfKind);
|
---|
| 259 |
|
---|
| 260 |
|
---|
| 261 | void kldrDyldFailure(const char *pszFilename, ...);
|
---|
| 262 |
|
---|
| 263 |
|
---|
[2836] | 264 | /** Pointer to the head module of the load list (the executable). */
|
---|
[2833] | 265 | extern PKLDRDYLDMOD kLdrDyldModuleHead;
|
---|
[2836] | 266 | /** Pointer to the tail module of the load list. */
|
---|
[2833] | 267 | extern PKLDRDYLDMOD kLdrDyldModuleTail;
|
---|
[2836] | 268 | /** Pointer to the head module of the termination order list. */
|
---|
| 269 | extern PKLDRDYLDMOD g_pkLdrDyldTermHead;
|
---|
| 270 | /** Pointer to the tail module of the termination order list. */
|
---|
| 271 | extern PKLDRDYLDMOD g_pkLdrDyldTermTail;
|
---|
| 272 | /** Pointer to the head module of the bind order list.
|
---|
| 273 | * The modules in this list makes up the global namespace used when binding symbol unix fashion. */
|
---|
| 274 | extern PKLDRDYLDMOD g_pkLdrDyldBindHead;
|
---|
| 275 | /** Pointer to the tail module of the bind order list. */
|
---|
| 276 | extern PKLDRDYLDMOD g_pkLdrDyldBindTail;
|
---|
| 277 |
|
---|
[2835] | 278 | /** The global error buffer. */
|
---|
| 279 | extern char g_szkLdrDyldError[1024];
|
---|
[2825] | 280 |
|
---|
[2836] | 281 | /** The Library search path. */
|
---|
| 282 | extern char kLdrDyldLibraryPath[4096];
|
---|
[2833] | 283 |
|
---|
[2836] | 284 |
|
---|
[2825] | 285 | /** @} */
|
---|
| 286 | #ifdef __cplusplus
|
---|
| 287 | }
|
---|
| 288 | #endif
|
---|
| 289 |
|
---|
| 290 | #endif
|
---|