[2826] | 1 | /* $Id: kLdrInternal.h 2837 2006-10-28 03:59:21Z 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
|
---|
[2837] | 79 | * Next state: LOADED_PREREQUISITES, DESTROYED
|
---|
[2836] | 80 | */
|
---|
| 81 | KLDRSTATE_MAPPED,
|
---|
[2837] | 82 | /** The module has been reloaded and needs to be fixed up again.
|
---|
| 83 | * (The loader can still be in a re-entrant mode.)
|
---|
| 84 | * Prev state: PENDING_GC
|
---|
| 85 | * Next state: LOADED_PREREQUISITES, PENDING_GC
|
---|
| 86 | */
|
---|
| 87 | KLDRSTATE_RELOADED,
|
---|
| 88 | /** The immediate prerequisites have been loaded.
|
---|
| 89 | * Prev state: MAPPED, RELOADED
|
---|
[2836] | 90 | * Next state: FIXED_UP, DESTROYED
|
---|
| 91 | */
|
---|
[2837] | 92 | KLDRSTATE_LOADED_PREREQUISITES,
|
---|
[2836] | 93 | /** Fixups has been applied.
|
---|
[2837] | 94 | * Prev state: LOADED_PREREQUISITES
|
---|
[2836] | 95 | * Next state: PENDING_INIT, DESTROYED
|
---|
| 96 | */
|
---|
| 97 | KLDRSTATE_FIXED_UP,
|
---|
| 98 | /** Pending initialization.
|
---|
| 99 | * (The loader can now be in a re-entrant mode.)
|
---|
| 100 | * Prev state: FIXED_UP
|
---|
| 101 | * Next state: PENDING_INITIALIZATION, PENDING_GC
|
---|
| 102 | */
|
---|
| 103 | KLDRSTATE_PENDING_INITIALIZATION,
|
---|
| 104 | /** Initializing.
|
---|
| 105 | * (The loader is now in a re-entrant mode.)
|
---|
| 106 | * Prev state: PENDING_INITIALIZATION
|
---|
| 107 | * Next state: GOOD, PENDING_GC
|
---|
| 108 | */
|
---|
| 109 | KLDRSTATE_INITIALIZING,
|
---|
| 110 | /** The module has been successfully loaded and initialized.
|
---|
| 111 | * (The loader can still be in a re-entrant mode.)
|
---|
| 112 | * Prev state: INITIALIZING
|
---|
| 113 | * Next state: PENDING_TERMINATION
|
---|
| 114 | * Re-entrant.
|
---|
| 115 | */
|
---|
| 116 | KLDRSTATE_GOOD,
|
---|
| 117 | /** Pending termination, reference count is 0.
|
---|
| 118 | * (The loader can now be in a re-entrant mode.)
|
---|
| 119 | * Prev state: GOOD
|
---|
| 120 | * Next state: TERMINATING, GOOD
|
---|
| 121 | */
|
---|
| 122 | KLDRSTATE_PENDING_TERMINATION,
|
---|
| 123 | /** Terminating, reference count is still 0.
|
---|
| 124 | * (The loader is now in a re-entrant mode, but loading is a bit restricted.)
|
---|
| 125 | * Prev state: PENDING_TERMINATION
|
---|
| 126 | * Next state: PENDING_GC
|
---|
| 127 | */
|
---|
| 128 | KLDRSTATE_TERMINATING,
|
---|
| 129 | /** Pending garbage collection.
|
---|
| 130 | * (The loader can still be in a re-entrant mode.)
|
---|
| 131 | * Prev state: TERMINATING, INITIALIZING, PENDING_INITIALIZATION
|
---|
[2837] | 132 | * Next state: GC, RELOADED
|
---|
[2836] | 133 | */
|
---|
| 134 | KLDRSTATE_PENDING_GC,
|
---|
| 135 | /** Being garbage collected.
|
---|
| 136 | * Prev state: PENDING_GC
|
---|
[2837] | 137 | * Next state: PENDING_DESTROY, DESTROYED
|
---|
[2836] | 138 | */
|
---|
| 139 | KLDRSTATE_GC,
|
---|
[2837] | 140 | /** The module has be unlinked, but there are still stack references to it.
|
---|
| 141 | * Prev state: GC
|
---|
| 142 | * Next state: DESTROYED
|
---|
[2836] | 143 | */
|
---|
[2837] | 144 | KLDRSTATE_PENDING_DESTROY,
|
---|
[2836] | 145 | /** The module has been destroyed and is no longer valid.
|
---|
[2837] | 146 | * Prev state: GC, PENDING_DESTROY
|
---|
[2836] | 147 | */
|
---|
| 148 | KLDRSTATE_DESTROYED,
|
---|
[2825] | 149 | /** The end of valid states (exclusive) */
|
---|
[2836] | 150 | KLDRSTATE_END = KLDRSTATE_DESTROYED,
|
---|
[2825] | 151 | /** The usual 32-bit blowup. */
|
---|
| 152 | KLDRSTATE_32BIT_HACK = 0x7fffffff
|
---|
| 153 | } KLDRSTATE;
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | /**
|
---|
| 157 | * Dynamic loader module.
|
---|
| 158 | */
|
---|
[2833] | 159 | typedef struct KLDRDYLDMOD
|
---|
[2825] | 160 | {
|
---|
[2833] | 161 | /** Magic number. */
|
---|
| 162 | uint32_t u32MagicHead;
|
---|
| 163 | /** The module state. */
|
---|
| 164 | KLDRSTATE enmState;
|
---|
[2825] | 165 | /** The module. */
|
---|
| 166 | PKLDRMOD pMod;
|
---|
[2836] | 167 | /** The module handle. */
|
---|
| 168 | HKLDRMOD hMod;
|
---|
[2837] | 169 | /** The total number of references. */
|
---|
[2825] | 170 | uint32_t cRefs;
|
---|
[2837] | 171 | /** The number of dependency references. */
|
---|
| 172 | uint32_t cDepRefs;
|
---|
| 173 | /** The number of dynamic load references. */
|
---|
[2825] | 174 | uint32_t cDynRefs;
|
---|
[2836] | 175 | /** Set if this is the executable module.
|
---|
| 176 | * When clear, the module is a shared object or relocatable object. */
|
---|
[2825] | 177 | uint32_t fExecutable : 1;
|
---|
| 178 | /** Global DLL (set) or specific DLL (clear). */
|
---|
[2836] | 179 | uint32_t fGlobalOrSpecific : 1;
|
---|
| 180 | /** Whether the module contains bindable symbols in the global unix namespace. */
|
---|
| 181 | uint32_t fBindable : 1;
|
---|
[2825] | 182 | /** Reserved for future use. */
|
---|
[2837] | 183 | uint32_t fReserved : 28;
|
---|
| 184 | /** Already checked dependencies.
|
---|
| 185 | * This is flag used when resolving module dependencies during a load, it
|
---|
| 186 | * deals with modules in the KLDRSTATE_PENDING_INITIALIZATION state. */
|
---|
| 187 | uint32_t fAlreadySeen : 1;
|
---|
[2836] | 188 | /** The load list linkage. */
|
---|
| 189 | struct
|
---|
| 190 | {
|
---|
| 191 | /** The next module in the list. */
|
---|
| 192 | struct KLDRDYLDMOD *pNext;
|
---|
| 193 | /** The prev module in the list. */
|
---|
| 194 | struct KLDRDYLDMOD *pPrev;
|
---|
| 195 | } Load;
|
---|
| 196 | /** The termination list linkage. */
|
---|
| 197 | struct
|
---|
| 198 | {
|
---|
| 199 | /** The next module in the list. */
|
---|
| 200 | struct KLDRDYLDMOD *pNext;
|
---|
| 201 | /** The prev module in the list. */
|
---|
| 202 | struct KLDRDYLDMOD *pPrev;
|
---|
| 203 | } Term;
|
---|
| 204 | /** The bind order list linkage.
|
---|
| 205 | * The module is not in this list when fBindable is clear. */
|
---|
| 206 | struct
|
---|
| 207 | {
|
---|
| 208 | /** The next module in the list. */
|
---|
| 209 | struct KLDRDYLDMOD *pNext;
|
---|
| 210 | /** The prev module in the list. */
|
---|
| 211 | struct KLDRDYLDMOD *pPrev;
|
---|
| 212 | } Bind;
|
---|
[2833] | 213 | /** Magic number. */
|
---|
| 214 | uint32_t u32MagicTail;
|
---|
| 215 | } KLDRDYLDMOD, *PKLDRDYLDMOD, **PPKLDRDYLDMOD;
|
---|
[2825] | 216 |
|
---|
[2836] | 217 | /** KLDRDYLDMOD magic value. (Fuyumi Soryo) */
|
---|
[2833] | 218 | #define KLDRDYMOD_MAGIC 0x19590106
|
---|
[2825] | 219 |
|
---|
[2833] | 220 | /** Return / crash validation of a module handle argument. */
|
---|
| 221 | #define KLDRDYLD_VALIDATE_HKLDRMOD(hMod) \
|
---|
| 222 | do { \
|
---|
| 223 | if ( (hMod) == NIL_HKLDRMOD \
|
---|
| 224 | || (hMod)->u32MagicHead != KLDRDYMOD_MAGIC \
|
---|
| 225 | || (hMod)->u32MagicTail != KLDRDYMOD_MAGIC) \
|
---|
| 226 | { \
|
---|
| 227 | return KLDR_ERR_INVALID_HANDLE; \
|
---|
| 228 | } \
|
---|
| 229 | } while (0)
|
---|
| 230 |
|
---|
| 231 |
|
---|
[2836] | 232 | int kldrDyldFindNewModule(const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
|
---|
[2835] | 233 | KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod);
|
---|
[2836] | 234 | int kldrDyldFindExistingModule(const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
|
---|
[2835] | 235 | KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod);
|
---|
| 236 |
|
---|
| 237 |
|
---|
| 238 | int kldrDyldModCreate(PKLDRRDR pRdr, PPKLDRDYLDMOD ppMod);
|
---|
| 239 | int kldrDyldModDestroy(PKLDRDYLDMOD pMod);
|
---|
[2837] | 240 | int kldrDyldModAddRef(PKLDRDYLDMOD pMod);
|
---|
| 241 | void kldrDyldModDeref(PKLDRDYLDMOD pMod);
|
---|
| 242 | int kldrDyldModAddDep(PKLDRDYLDMOD pMod, PKLDRDYLDMOD pDep);
|
---|
| 243 | void kldrDyldModRemoveDep(PKLDRDYLDMOD pMod, PKLDRDYLDMOD pDep);
|
---|
[2835] | 244 | int kldrDyldModDynamicLoad(PKLDRDYLDMOD pMod);
|
---|
| 245 | int kldrDyldModDynamicUnload(PKLDRDYLDMOD pMod);
|
---|
| 246 | int kldrDyldModMarkGlobal(PKLDRDYLDMOD pMod);
|
---|
| 247 | int kldrDyldModMarkSpecific(PKLDRDYLDMOD pMod);
|
---|
| 248 | int kldrDyldModSetBindable(PKLDRDYLDMOD pMod);
|
---|
| 249 | int kldrDyldModClearBindable(PKLDRDYLDMOD pMod);
|
---|
| 250 | int kldrDyldModSetDeepBindable(PKLDRDYLDMOD pMod);
|
---|
| 251 | int kldrDyldModClearDeepBindable(PKLDRDYLDMOD pMod);
|
---|
| 252 | int kldrDyldModMap(PKLDRDYLDMOD pMod);
|
---|
| 253 | int kldrDyldModUnmap(PKLDRDYLDMOD pMod);
|
---|
[2837] | 254 | int kldrDyldModLoadPrerequisites(PKLDRDYLDMOD pMod, const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
|
---|
| 255 | KLDRDYLDSEARCH enmSearch, unsigned fFlags);
|
---|
| 256 | int kldrDyldModCheckPrerequisites(PKLDRDYLDMOD pMod);
|
---|
| 257 | void kldrDyldModUnloadPrerequisites(PKLDRDYLDMOD pMod);
|
---|
[2835] | 258 | int kldrDyldModFixup(PKLDRDYLDMOD pMod);
|
---|
| 259 | int kldrDyldModCallInit(PKLDRDYLDMOD pMod);
|
---|
[2837] | 260 | void kldrDyldModCallTerm(PKLDRDYLDMOD pMod);
|
---|
| 261 | int kldrDyldModReload(PKLDRDYLDMOD pMod);
|
---|
[2836] | 262 | int kldrDyldModAttachThread(PKLDRDYLDMOD pMod);
|
---|
| 263 | int kldrDyldModDetachThread(PKLDRDYLDMOD pMod);
|
---|
[2835] | 264 | int kldrDyldModGetStackInfo(PKLDRDYLDMOD pMod, void *pvStack, size_t *pcbStack, size_t);
|
---|
| 265 | int kldrDyldModStartExe(PKLDRDYLDMOD pMod);
|
---|
| 266 |
|
---|
| 267 | int kldrDyldModGetName(PKLDRDYLDMOD pMod, char *pszName, size_t cchName);
|
---|
| 268 | int kldrDyldModGetFilename(PKLDRDYLDMOD pMod, char *pszFilename, size_t cchFilename);
|
---|
| 269 | int kldrDyldModQuerySymbol(PKLDRDYLDMOD pMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, uintptr_t *pValue, uint32_t *pfKind);
|
---|
| 270 |
|
---|
| 271 |
|
---|
| 272 | void kldrDyldFailure(const char *pszFilename, ...);
|
---|
| 273 |
|
---|
| 274 |
|
---|
[2836] | 275 | /** Pointer to the head module of the load list (the executable). */
|
---|
[2833] | 276 | extern PKLDRDYLDMOD kLdrDyldModuleHead;
|
---|
[2836] | 277 | /** Pointer to the tail module of the load list. */
|
---|
[2833] | 278 | extern PKLDRDYLDMOD kLdrDyldModuleTail;
|
---|
[2836] | 279 | /** Pointer to the head module of the termination order list. */
|
---|
| 280 | extern PKLDRDYLDMOD g_pkLdrDyldTermHead;
|
---|
| 281 | /** Pointer to the tail module of the termination order list. */
|
---|
| 282 | extern PKLDRDYLDMOD g_pkLdrDyldTermTail;
|
---|
| 283 | /** Pointer to the head module of the bind order list.
|
---|
| 284 | * The modules in this list makes up the global namespace used when binding symbol unix fashion. */
|
---|
| 285 | extern PKLDRDYLDMOD g_pkLdrDyldBindHead;
|
---|
| 286 | /** Pointer to the tail module of the bind order list. */
|
---|
| 287 | extern PKLDRDYLDMOD g_pkLdrDyldBindTail;
|
---|
| 288 |
|
---|
[2835] | 289 | /** The global error buffer. */
|
---|
| 290 | extern char g_szkLdrDyldError[1024];
|
---|
[2825] | 291 |
|
---|
[2836] | 292 | /** The Library search path. */
|
---|
| 293 | extern char kLdrDyldLibraryPath[4096];
|
---|
[2833] | 294 |
|
---|
[2836] | 295 |
|
---|
[2825] | 296 | /** @} */
|
---|
| 297 | #ifdef __cplusplus
|
---|
| 298 | }
|
---|
| 299 | #endif
|
---|
| 300 |
|
---|
| 301 | #endif
|
---|