| [2826] | 1 | /* $Id: kLdrDyld.c 2893 2006-12-22 18:01:38Z bird $ */ | 
|---|
| [2825] | 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 |  | 
|---|
|  | 28 | /******************************************************************************* | 
|---|
|  | 29 | *   Header Files                                                               * | 
|---|
|  | 30 | *******************************************************************************/ | 
|---|
|  | 31 | #include <kLdr.h> | 
|---|
|  | 32 | #include "kLdrHlp.h" | 
|---|
|  | 33 | #include "kLdrInternal.h" | 
|---|
|  | 34 |  | 
|---|
|  | 35 |  | 
|---|
|  | 36 | /******************************************************************************* | 
|---|
| [2836] | 37 | *   Defined Constants And Macros                                               * | 
|---|
|  | 38 | *******************************************************************************/ | 
|---|
|  | 39 | /** @def KLDRDYLD_STRICT | 
|---|
|  | 40 | * Define KLDRDYLD_STRICT to enabled strict checks in kLdrDyld. */ | 
|---|
|  | 41 | #define KLDRDYLD_STRICT 1 | 
|---|
|  | 42 |  | 
|---|
|  | 43 | /** @def KLDRDYLD_ASSERT | 
|---|
|  | 44 | * Assert that an expression is true when KLDRDYLD_STRICT is defined. | 
|---|
|  | 45 | */ | 
|---|
|  | 46 | #ifdef KLDRDYLD_STRICT | 
|---|
|  | 47 | # define KLDRDYLD_ASSERT(expr)  kldrHlpAssert(expr) | 
|---|
|  | 48 | #else | 
|---|
|  | 49 | # define KLDRDYLD_ASSERT(expr)  do {} while (0) | 
|---|
|  | 50 | #endif | 
|---|
|  | 51 |  | 
|---|
|  | 52 |  | 
|---|
|  | 53 | /******************************************************************************* | 
|---|
| [2842] | 54 | *   Structures and Typedefs                                                    * | 
|---|
|  | 55 | *******************************************************************************/ | 
|---|
|  | 56 |  | 
|---|
|  | 57 |  | 
|---|
|  | 58 | /******************************************************************************* | 
|---|
| [2825] | 59 | *   Global Variables                                                           * | 
|---|
|  | 60 | *******************************************************************************/ | 
|---|
| [2843] | 61 | /** Pointer to the executable module. | 
|---|
|  | 62 | * (This is exported, so no prefix.) */ | 
|---|
|  | 63 | PKLDRDYLDMOD    kLdrDyldExe = NULL; | 
|---|
| [2825] | 64 | /** Pointer to the head module (the executable). | 
|---|
|  | 65 | * (This is exported, so no prefix.) */ | 
|---|
| [2833] | 66 | PKLDRDYLDMOD    kLdrDyldHead = NULL; | 
|---|
| [2825] | 67 | /** Pointer to the tail module. | 
|---|
|  | 68 | * (This is exported, so no prefix.) */ | 
|---|
| [2833] | 69 | PKLDRDYLDMOD    kLdrDyldTail = NULL; | 
|---|
| [2842] | 70 | /** Pointer to the head module of the initialization list. | 
|---|
|  | 71 | * The outermost load call will pop elements from this list in LIFO order (i.e. | 
|---|
|  | 72 | * from the tail). The list is only used during non-recursive initialization | 
|---|
|  | 73 | * and may therefore share the pNext/pPrev members with the termination list | 
|---|
|  | 74 | * since we don't push a module onto the termination list untill it has been | 
|---|
|  | 75 | * successfully initialized. */ | 
|---|
|  | 76 | PKLDRDYLDMOD    g_pkLdrDyldInitHead; | 
|---|
| [2869] | 77 | /** Pointer to the tail module of the initalization list.*/ | 
|---|
| [2842] | 78 | PKLDRDYLDMOD    g_pkLdrDyldInitTail; | 
|---|
| [2869] | 79 | /** Pointer to the head module of the termination order list. | 
|---|
|  | 80 | * This is a LIFO just like the the init list. */ | 
|---|
| [2836] | 81 | PKLDRDYLDMOD    g_pkLdrDyldTermHead; | 
|---|
|  | 82 | /** Pointer to the tail module of the termination order list. */ | 
|---|
|  | 83 | PKLDRDYLDMOD    g_pkLdrDyldTermTail; | 
|---|
|  | 84 | /** Pointer to the head module of the bind order list. | 
|---|
|  | 85 | * The modules in this list makes up the global namespace used when binding symbol unix fashion. */ | 
|---|
|  | 86 | PKLDRDYLDMOD    g_pkLdrDyldBindHead; | 
|---|
|  | 87 | /** Pointer to the tail module of the bind order list. */ | 
|---|
|  | 88 | PKLDRDYLDMOD    g_pkLdrDyldBindTail; | 
|---|
|  | 89 |  | 
|---|
| [2843] | 90 | /** Flag indicating bootstrap time. | 
|---|
|  | 91 | * When set the error behaviour changes. Any kind of serious failure | 
|---|
|  | 92 | * is fatal and will terminate the process. */ | 
|---|
|  | 93 | int             g_fBootstrapping; | 
|---|
|  | 94 | /** The global error buffer. */ | 
|---|
|  | 95 | char            g_szkLdrDyldError[1024]; | 
|---|
|  | 96 |  | 
|---|
|  | 97 | /** The default flags. */ | 
|---|
|  | 98 | uint32_t        kLdrDyldFlags = 0; | 
|---|
|  | 99 | /** The default search method. */ | 
|---|
| [2875] | 100 | KLDRDYLDSEARCH  kLdrDyldSearch = KLDRDYLD_SEARCH_HOST; | 
|---|
| [2843] | 101 |  | 
|---|
|  | 102 |  | 
|---|
| [2846] | 103 | /** @name The main stack. | 
|---|
|  | 104 | * @{ */ | 
|---|
|  | 105 | /** Indicates that the other MainStack globals have been filled in. */ | 
|---|
|  | 106 | unsigned        g_fkLdrDyldDoneMainStack = 0; | 
|---|
|  | 107 | /** Whether the stack was allocated seperatly or was part of the executable. */ | 
|---|
|  | 108 | unsigned        g_fkLdrDyldMainStackAllocated = 0; | 
|---|
|  | 109 | /** Pointer to the main stack object. */ | 
|---|
|  | 110 | void           *g_pvkLdrDyldMainStack = NULL; | 
|---|
|  | 111 | /** The size of the main stack object. */ | 
|---|
|  | 112 | size_t          g_cbkLdrDyldMainStack = 0; | 
|---|
|  | 113 | /** @} */ | 
|---|
|  | 114 |  | 
|---|
|  | 115 |  | 
|---|
| [2843] | 116 | /** The load stack. | 
|---|
|  | 117 | * This contains frames with modules affected by active loads. | 
|---|
| [2836] | 118 | * | 
|---|
| [2843] | 119 | * Each kLdrDyldLoad and kLdrDyldLoadExe call will create a new stack frame containing | 
|---|
|  | 120 | * all the modules involved in the operation. The modules will be ordered in recursive | 
|---|
|  | 121 | * init order within the frame. | 
|---|
| [2836] | 122 | */ | 
|---|
| [2837] | 123 | static PPKLDRDYLDMOD    g_papStackMods; | 
|---|
|  | 124 | /** The number of used entries in the g_papStackMods array. */ | 
|---|
|  | 125 | static uint32_t         g_cStackMods; | 
|---|
|  | 126 | /** The number of entries allocated for the g_papStackMods array. */ | 
|---|
|  | 127 | static uint32_t         g_cStackModsAllocated; | 
|---|
|  | 128 | /** Number of active load calls. */ | 
|---|
|  | 129 | static uint32_t         g_cActiveLoadCalls; | 
|---|
|  | 130 | /** Number of active unload calls. */ | 
|---|
|  | 131 | static uint32_t         g_cActiveUnloadCalls; | 
|---|
| [2842] | 132 | /** Total number of load calls. */ | 
|---|
|  | 133 | static uint32_t         g_cTotalLoadCalls; | 
|---|
|  | 134 | /** Total mumber of unload calls. */ | 
|---|
|  | 135 | static uint32_t         g_cTotalUnloadCalls; | 
|---|
| [2837] | 136 | /** Boolean flag indicating that GC is active. */ | 
|---|
|  | 137 | static uint32_t         g_fActiveGC; | 
|---|
| [2836] | 138 |  | 
|---|
|  | 139 |  | 
|---|
| [2825] | 140 |  | 
|---|
|  | 141 | /******************************************************************************* | 
|---|
|  | 142 | *   Internal Functions                                                         * | 
|---|
|  | 143 | *******************************************************************************/ | 
|---|
| [2842] | 144 | /** @name API worker routines. | 
|---|
|  | 145 | * @internal | 
|---|
|  | 146 | * @{ */ | 
|---|
| [2843] | 147 | void       kldrDyldDoLoadExeStackSwitch(PKLDRDYLDMOD pExe, void *pvStack, size_t cbStack); | 
|---|
|  | 148 | static int kldrDyldDoLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch, | 
|---|
| [2836] | 149 | unsigned fFlags, PPKLDRDYLDMOD ppMod, char *pszErr, size_t cchErr); | 
|---|
| [2843] | 150 | static int kldrDyldDoLoad2(PKLDRDYLDMOD pLoadedMod, const char *pszPrefix, const char *pszSuffix, | 
|---|
|  | 151 | KLDRDYLDSEARCH enmSearch, unsigned fFlags); | 
|---|
|  | 152 | static int kldrDyldDoLoadPrerequisites(PKLDRDYLDMOD pMod, const char *pszPrefix, const char *pszSuffix, | 
|---|
| [2842] | 153 | KLDRDYLDSEARCH enmSearch, unsigned fFlags); | 
|---|
| [2836] | 154 | static int kldrDyldDoUnload(PKLDRDYLDMOD pMod); | 
|---|
| [2843] | 155 | static int kldrDyldDoFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch, | 
|---|
| [2836] | 156 | unsigned fFlags, PPKLDRDYLDMOD ppMod); | 
|---|
|  | 157 | static int kldrDyldDoFindByAddress(uintptr_t Address, PPKLDRDYLDMOD ppMod, uint32_t *piSegment, uintptr_t *poffSegment); | 
|---|
|  | 158 | static int kldrDyldDoGetName(PKLDRDYLDMOD pMod, char *pszName, size_t cchName); | 
|---|
|  | 159 | static int kldrDyldDoGetFilename(PKLDRDYLDMOD pMod, char *pszFilename, size_t cchFilename); | 
|---|
|  | 160 | static int kldrDyldDoQuerySymbol(PKLDRDYLDMOD pMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, uintptr_t *pValue, uint32_t *pfKind); | 
|---|
| [2842] | 161 | /** @} */ | 
|---|
| [2825] | 162 |  | 
|---|
| [2842] | 163 | /** @name Misc load/unload workers | 
|---|
|  | 164 | * @internal | 
|---|
|  | 165 | * @{ | 
|---|
|  | 166 | */ | 
|---|
|  | 167 | static void         kldrDyldDoModuleTerminationAndGarabageCollection(void); | 
|---|
|  | 168 | /** @} */ | 
|---|
| [2825] | 169 |  | 
|---|
| [2842] | 170 | /** @name The load stack. | 
|---|
|  | 171 | * @internal | 
|---|
|  | 172 | * @{ */ | 
|---|
|  | 173 | static uint32_t     kldrDyldStackNewFrame(PKLDRDYLDMOD pMod); | 
|---|
|  | 174 | static int          kldrDyldStackAddModule(PKLDRDYLDMOD pMod); | 
|---|
|  | 175 | static int          kldrDyldStackFrameCompleted(void); | 
|---|
| [2845] | 176 | static void         kldrDyldStackCleanupOne(PKLDRDYLDMOD pMod, int rc); | 
|---|
| [2842] | 177 | static void         kldrDyldStackDropFrame(uint32_t iLoad1st, uint32_t iLoadEnd, int rc); | 
|---|
|  | 178 | /** @} */ | 
|---|
| [2833] | 179 |  | 
|---|
| [2842] | 180 | static int          kldrDyldCopyError(int rc, char *pszErr, size_t cchErr); | 
|---|
| [2837] | 181 |  | 
|---|
|  | 182 |  | 
|---|
|  | 183 |  | 
|---|
| [2825] | 184 | /** | 
|---|
| [2833] | 185 | * Initialize the dynamic loader. | 
|---|
| [2825] | 186 | */ | 
|---|
| [2869] | 187 | int kldrDyldInit(void) | 
|---|
| [2825] | 188 | { | 
|---|
| [2836] | 189 | kLdrDyldHead = kLdrDyldTail = NULL; | 
|---|
|  | 190 | g_pkLdrDyldTermHead = g_pkLdrDyldTermTail = NULL; | 
|---|
|  | 191 | g_pkLdrDyldBindHead = g_pkLdrDyldBindTail = NULL; | 
|---|
| [2833] | 192 | kLdrDyldFlags = 0; | 
|---|
| [2836] | 193 | g_szkLdrDyldError[0] = '\0'; | 
|---|
| [2846] | 194 |  | 
|---|
|  | 195 | g_fkLdrDyldDoneMainStack = 0; | 
|---|
|  | 196 | g_fkLdrDyldMainStackAllocated = 0; | 
|---|
|  | 197 | g_pvkLdrDyldMainStack = NULL; | 
|---|
|  | 198 | g_cbkLdrDyldMainStack = 0; | 
|---|
|  | 199 |  | 
|---|
| [2869] | 200 | return kldrDyldFindInit(); | 
|---|
| [2825] | 201 | } | 
|---|
|  | 202 |  | 
|---|
|  | 203 |  | 
|---|
| [2833] | 204 | /** | 
|---|
|  | 205 | * Terminate the dynamic loader. | 
|---|
|  | 206 | */ | 
|---|
| [2869] | 207 | void kldrDyldTerm(void) | 
|---|
| [2833] | 208 | { | 
|---|
| [2825] | 209 |  | 
|---|
| [2833] | 210 | } | 
|---|
|  | 211 |  | 
|---|
|  | 212 |  | 
|---|
|  | 213 | /** | 
|---|
| [2843] | 214 | * Bootstrap an executable. | 
|---|
|  | 215 | * | 
|---|
|  | 216 | * This is called from the executable stub to replace the stub and run the | 
|---|
|  | 217 | * executable specified in the argument package. | 
|---|
|  | 218 | * | 
|---|
|  | 219 | * Since this is boostrap time there isn't anything to return to. So, instead | 
|---|
|  | 220 | * the process will be terminated upon failure. | 
|---|
|  | 221 | * | 
|---|
|  | 222 | * We also have to keep in mind that this function is called on a small, small, | 
|---|
|  | 223 | * stack and therefore any kind of large stack objects or deep recursions must | 
|---|
|  | 224 | * be avoided. Since loading the executable will involve more or less all | 
|---|
|  | 225 | * operations in the loader, this restriction really applies everywhere. | 
|---|
|  | 226 | * | 
|---|
|  | 227 | * @param   pArgs       Pointer to the argument package residing in the executable stub. | 
|---|
| [2874] | 228 | * @param   pvOS        OS specific argument. | 
|---|
| [2843] | 229 | */ | 
|---|
| [2875] | 230 | #ifndef __OS2__  /* kLdrDyldLoadExe is implemented in assembly on OS/2. */ | 
|---|
|  | 231 | void kLdrDyldLoadExe(PCKLDREXEARGS pArgs, void *pvOS) | 
|---|
| [2874] | 232 | #else | 
|---|
| [2875] | 233 | void kldrDyldLoadExe(PCKLDREXEARGS pArgs, void *pvOS) | 
|---|
| [2874] | 234 | #endif | 
|---|
| [2843] | 235 | { | 
|---|
| [2867] | 236 | void *pvStack; | 
|---|
|  | 237 | size_t cbStack; | 
|---|
|  | 238 | PKLDRDYLDMOD pExe; | 
|---|
| [2843] | 239 | int rc; | 
|---|
|  | 240 |  | 
|---|
|  | 241 | /* | 
|---|
| [2875] | 242 | * Indicate that we're boostrapping and ensure that initialization was successful. | 
|---|
| [2843] | 243 | */ | 
|---|
| [2875] | 244 | g_fBootstrapping = 1; | 
|---|
|  | 245 | rc = kldrInit(); | 
|---|
|  | 246 | if (rc) | 
|---|
|  | 247 | kldrDyldFailure(rc, "Init failure, rc=%d", rc); | 
|---|
|  | 248 |  | 
|---|
|  | 249 | /* | 
|---|
|  | 250 | * Validate the argument package. | 
|---|
|  | 251 | */ | 
|---|
|  | 252 | if (pArgs->fFlags & ~(  KLDRYDLD_LOAD_FLAGS_GLOBAL_SYMBOLS | 
|---|
|  | 253 | | KLDRYDLD_LOAD_FLAGS_DEEP_SYMBOLS | 
|---|
|  | 254 | | KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT | 
|---|
|  | 255 | | KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE)) | 
|---|
|  | 256 | kldrDyldFailure(KLDR_ERR_INVALID_PARAMETER, "Bad fFlags=%#x", pArgs->fFlags); | 
|---|
|  | 257 | if (    pArgs->enmSearch <= KLDRDYLD_SEARCH_INVALID | 
|---|
|  | 258 | ||  pArgs->enmSearch >= KLDRDYLD_SEARCH_END) | 
|---|
|  | 259 | kldrDyldFailure(KLDR_ERR_INVALID_PARAMETER, "Bad enmSearch=%d", pArgs->enmSearch); | 
|---|
|  | 260 |  | 
|---|
|  | 261 | /* | 
|---|
|  | 262 | * Set defaults. | 
|---|
|  | 263 | */ | 
|---|
|  | 264 | kLdrDyldFlags |= (pArgs->fFlags & KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT); | 
|---|
| [2843] | 265 | kLdrDyldSearch = pArgs->enmSearch; | 
|---|
| [2875] | 266 |  | 
|---|
|  | 267 | /** @todo make sense of this default prefix/suffix stuff. */ | 
|---|
| [2867] | 268 | if (pArgs->szDefPrefix[0] != '\0') | 
|---|
|  | 269 | kLdrHlpMemCopy(kLdrDyldDefPrefix, pArgs->szDefPrefix, KLDR_MIN(sizeof(pArgs->szDefPrefix), sizeof(kLdrDyldDefPrefix))); | 
|---|
|  | 270 | if (pArgs->szDefSuffix[0] != '\0') | 
|---|
|  | 271 | kLdrHlpMemCopy(kLdrDyldDefSuffix, pArgs->szDefSuffix, KLDR_MIN(sizeof(pArgs->szDefSuffix), sizeof(kLdrDyldDefSuffix))); | 
|---|
| [2843] | 272 |  | 
|---|
| [2875] | 273 | /** @todo append that path to the one for the specified search method. */ | 
|---|
|  | 274 | /** @todo create a function for doing this, an exposed api preferably. */ | 
|---|
|  | 275 | /* append path */ | 
|---|
|  | 276 | cbStack = sizeof(kLdrDyldLibraryPath) - kLdrHlpStrLen(kLdrDyldLibraryPath); /* borrow cbStack for a itty bit. */ | 
|---|
|  | 277 | kLdrHlpMemCopy(kLdrDyldLibraryPath, pArgs->szLibPath, KLDR_MIN(sizeof(pArgs->szLibPath), cbStack)); | 
|---|
|  | 278 | kLdrDyldLibraryPath[sizeof(kLdrDyldLibraryPath) - 1] = '\0'; | 
|---|
| [2843] | 279 |  | 
|---|
|  | 280 | /* | 
|---|
|  | 281 | * Make sure we own the loader semaphore (necessary for init). | 
|---|
|  | 282 | */ | 
|---|
|  | 283 | rc = kldrHlpSemRequest(); | 
|---|
|  | 284 | if (rc) | 
|---|
|  | 285 | kldrDyldFailure(rc, "Sem req. failure, rc=%d", rc); | 
|---|
|  | 286 |  | 
|---|
|  | 287 | /* | 
|---|
|  | 288 | * Open and map the executable module before we join paths with kLdrDyldLoad(). | 
|---|
|  | 289 | */ | 
|---|
| [2875] | 290 | rc = kldrDyldFindNewModule(pArgs->szExecutable, NULL, NULL, pArgs->enmSearch, | 
|---|
|  | 291 | pArgs->fFlags | KLDRDYLD_LOAD_FLAGS_EXECUTABLE, &pExe); | 
|---|
| [2843] | 292 | if (rc) | 
|---|
|  | 293 | kldrDyldFailure(rc, "Can't find/open the executable '%s', rc=%d", pArgs->szExecutable, rc); | 
|---|
|  | 294 | rc = kldrDyldModMap(pExe); | 
|---|
|  | 295 | if (rc) | 
|---|
|  | 296 | kldrDyldFailure(rc, "Failed to map the executable '%s', rc=%d", pExe->pMod->pszFilename, rc); | 
|---|
|  | 297 |  | 
|---|
|  | 298 | kLdrDyldExe = pExe; | 
|---|
|  | 299 |  | 
|---|
|  | 300 | /* | 
|---|
| [2846] | 301 | * Query the stack and go to OS specific code to | 
|---|
| [2843] | 302 | * setup and switch stack. The OS specific code will call us | 
|---|
|  | 303 | * back at kldrDyldDoLoadExe. | 
|---|
|  | 304 | */ | 
|---|
| [2846] | 305 | rc = kldrDyldModGetMainStack(pExe, &pvStack, &cbStack); | 
|---|
| [2843] | 306 | if (rc) | 
|---|
|  | 307 | kldrDyldFailure(rc, "Failed to map the executable '%s', rc=%d", pExe->pMod->pszFilename, rc); | 
|---|
|  | 308 | kldrDyldDoLoadExeStackSwitch(pExe, pvStack, cbStack); | 
|---|
|  | 309 | kldrDyldFailure(-1, "Failed to setup the stack for '%s'.", pExe->pMod->pszFilename); | 
|---|
|  | 310 | } | 
|---|
|  | 311 |  | 
|---|
|  | 312 |  | 
|---|
|  | 313 | /** | 
|---|
| [2833] | 314 | * Loads a module into the current process. | 
|---|
|  | 315 | * | 
|---|
|  | 316 | * @returns 0 on success, non-zero native OS status code or kLdr status code on failure. | 
|---|
|  | 317 | * @param   pszDll          The name of the dll to open. | 
|---|
| [2843] | 318 | * @param   pszPrefix       Prefix to use when searching. | 
|---|
|  | 319 | * @param   pszSuffix       Suffix to use when searching. | 
|---|
| [2833] | 320 | * @param   enmSearch       Method to use when locating the module and any modules it may depend on. | 
|---|
|  | 321 | * @param   fFlags          Flags, a combintation of the KLDRYDLD_LOAD_FLAGS_* \#defines. | 
|---|
|  | 322 | * @param   phMod           Where to store the handle to the loaded module. | 
|---|
|  | 323 | * @param   pszErr          Where to store extended error information. (optional) | 
|---|
|  | 324 | * @param   cchErr          The size of the buffer pointed to by pszErr. | 
|---|
|  | 325 | */ | 
|---|
| [2843] | 326 | int     kLdrDyldLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch, | 
|---|
| [2833] | 327 | unsigned fFlags, PHKLDRMOD phMod, char *pszErr, size_t cchErr) | 
|---|
|  | 328 | { | 
|---|
|  | 329 | int rc; | 
|---|
|  | 330 |  | 
|---|
|  | 331 | /* validate arguments and initialize return values. */ | 
|---|
|  | 332 | if (pszErr && cchErr) | 
|---|
|  | 333 | *pszErr = '\0'; | 
|---|
|  | 334 | *phMod = NIL_HKLDRMOD; | 
|---|
|  | 335 | KLDRHLP_VALIDATE_STRING(pszDll); | 
|---|
| [2843] | 336 | KLDRHLP_VALIDATE_OPTIONAL_STRING(pszPrefix); | 
|---|
|  | 337 | KLDRHLP_VALIDATE_OPTIONAL_STRING(pszSuffix); | 
|---|
| [2833] | 338 | KLDRHLP_VALIDATE_ENUM(enmSearch, KLDRDYLD_SEARCH); | 
|---|
|  | 339 | KLDRHLP_VALIDATE_OPTIONAL_BUFFER(pszErr, cchErr); | 
|---|
|  | 340 |  | 
|---|
|  | 341 | /* get the semaphore and do the job. */ | 
|---|
|  | 342 | rc = kldrHlpSemRequest(); | 
|---|
|  | 343 | if (!rc) | 
|---|
|  | 344 | { | 
|---|
|  | 345 | PKLDRDYLDMOD pMod = NULL; | 
|---|
| [2842] | 346 | g_cTotalLoadCalls++; | 
|---|
| [2837] | 347 | g_cActiveLoadCalls++; | 
|---|
| [2869] | 348 | rc = kldrDyldDoLoad(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, &pMod, pszErr, cchErr); | 
|---|
| [2837] | 349 | g_cActiveLoadCalls--; | 
|---|
|  | 350 | kldrDyldDoModuleTerminationAndGarabageCollection(); | 
|---|
| [2833] | 351 | kldrHlpSemRelease(); | 
|---|
| [2870] | 352 | *phMod = pMod ? pMod->hMod : NIL_HKLDRMOD; | 
|---|
| [2833] | 353 | } | 
|---|
|  | 354 | return rc; | 
|---|
|  | 355 | } | 
|---|
|  | 356 |  | 
|---|
|  | 357 |  | 
|---|
|  | 358 | /** | 
|---|
|  | 359 | * Unloads a module loaded by kLdrDyldLoad. | 
|---|
|  | 360 | * | 
|---|
|  | 361 | * @returns 0 on success, non-zero native OS status code or kLdr status code on failure. | 
|---|
|  | 362 | * @param   hMod            Module handle. | 
|---|
|  | 363 | */ | 
|---|
|  | 364 | int     kLdrDyldUnload(HKLDRMOD hMod) | 
|---|
|  | 365 | { | 
|---|
|  | 366 | int rc; | 
|---|
|  | 367 |  | 
|---|
|  | 368 | /* validate */ | 
|---|
|  | 369 | KLDRDYLD_VALIDATE_HKLDRMOD(hMod); | 
|---|
|  | 370 |  | 
|---|
|  | 371 | /* get sem & do work */ | 
|---|
|  | 372 | rc = kldrHlpSemRequest(); | 
|---|
|  | 373 | if (!rc) | 
|---|
|  | 374 | { | 
|---|
| [2842] | 375 | g_cTotalUnloadCalls++; | 
|---|
| [2837] | 376 | g_cActiveUnloadCalls++; | 
|---|
| [2836] | 377 | rc = kldrDyldDoUnload(hMod); | 
|---|
| [2837] | 378 | g_cActiveUnloadCalls--; | 
|---|
|  | 379 | kldrDyldDoModuleTerminationAndGarabageCollection(); | 
|---|
| [2833] | 380 | kldrHlpSemRelease(); | 
|---|
|  | 381 | } | 
|---|
|  | 382 | return rc; | 
|---|
|  | 383 | } | 
|---|
|  | 384 |  | 
|---|
|  | 385 |  | 
|---|
|  | 386 | /** | 
|---|
|  | 387 | * Finds a module by name or filename. | 
|---|
|  | 388 | * | 
|---|
|  | 389 | * This call does not increase any reference counters and must not be | 
|---|
|  | 390 | * paired with kLdrDyldUnload() like kLdrDyldLoad(). | 
|---|
|  | 391 | * | 
|---|
|  | 392 | * @returns 0 on success. | 
|---|
| [2837] | 393 | * @returns KLDR_ERR_MODULE_NOT_FOUND or some I/O error on failure. | 
|---|
| [2833] | 394 | * @param   pszDll          The name of the dll to look for. | 
|---|
| [2843] | 395 | * @param   pszPrefix       Prefix than can be used when searching. | 
|---|
|  | 396 | * @param   pszSuffix       Suffix than can be used when searching. | 
|---|
| [2833] | 397 | * @param   enmSearch       Method to use when locating the module. | 
|---|
| [2836] | 398 | * @param   fFlags          Flags, a combintation of the KLDRYDLD_LOAD_FLAGS_* \#defines. | 
|---|
| [2833] | 399 | * @param   phMod           Where to store the handle of the module on success. | 
|---|
|  | 400 | */ | 
|---|
| [2843] | 401 | int     kLdrDyldFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch, | 
|---|
| [2836] | 402 | unsigned fFlags, PHKLDRMOD phMod) | 
|---|
| [2833] | 403 | { | 
|---|
|  | 404 | int rc; | 
|---|
|  | 405 |  | 
|---|
|  | 406 | /* validate & initialize */ | 
|---|
|  | 407 | *phMod = NIL_HKLDRMOD; | 
|---|
|  | 408 | KLDRHLP_VALIDATE_STRING(pszDll); | 
|---|
|  | 409 |  | 
|---|
|  | 410 | /* get sem & do work */ | 
|---|
|  | 411 | rc = kldrHlpSemRequest(); | 
|---|
|  | 412 | if (!rc) | 
|---|
|  | 413 | { | 
|---|
|  | 414 | PKLDRDYLDMOD pMod = NULL; | 
|---|
| [2870] | 415 | rc = kldrDyldDoFindByName(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, &pMod); | 
|---|
| [2833] | 416 | kldrHlpSemRelease(); | 
|---|
| [2870] | 417 | *phMod = pMod ? pMod->hMod : NIL_HKLDRMOD; | 
|---|
| [2833] | 418 | } | 
|---|
|  | 419 | return rc; | 
|---|
|  | 420 | } | 
|---|
|  | 421 |  | 
|---|
|  | 422 |  | 
|---|
|  | 423 | /** | 
|---|
|  | 424 | * Finds a module by address. | 
|---|
|  | 425 | * | 
|---|
|  | 426 | * This call does not increase any reference counters and must not be | 
|---|
|  | 427 | * paired with kLdrDyldUnload() like kLdrDyldLoad(). | 
|---|
|  | 428 | * | 
|---|
|  | 429 | * @returns 0 on success. | 
|---|
|  | 430 | * @returns KLDR_ERR_MODULE_NOT_FOUND on failure. | 
|---|
|  | 431 | * @param   Address         The address believed to be within some module. | 
|---|
|  | 432 | * @param   phMod           Where to store the module handle on success. | 
|---|
|  | 433 | * @param   piSegment       Where to store the segment number. (optional) | 
|---|
|  | 434 | * @param   poffSegment     Where to store the offset into the segment. (optional) | 
|---|
|  | 435 | */ | 
|---|
|  | 436 | int     kLdrDyldFindByAddress(uintptr_t Address, PHKLDRMOD phMod, uint32_t *piSegment, uintptr_t *poffSegment) | 
|---|
|  | 437 | { | 
|---|
|  | 438 | int rc; | 
|---|
|  | 439 |  | 
|---|
|  | 440 | /* validate & initialize */ | 
|---|
|  | 441 | *phMod = NIL_HKLDRMOD; | 
|---|
|  | 442 | if (piSegment) | 
|---|
|  | 443 | *piSegment = ~(uint32_t)0; | 
|---|
|  | 444 | if (poffSegment) | 
|---|
|  | 445 | *poffSegment = ~(uintptr_t)0; | 
|---|
|  | 446 |  | 
|---|
|  | 447 | /* get sem & do work */ | 
|---|
|  | 448 | rc = kldrHlpSemRequest(); | 
|---|
|  | 449 | if (!rc) | 
|---|
|  | 450 | { | 
|---|
|  | 451 | PKLDRDYLDMOD pMod = NULL; | 
|---|
| [2836] | 452 | rc = kldrDyldDoFindByAddress(Address, &pMod, piSegment, poffSegment); | 
|---|
| [2833] | 453 | kldrHlpSemRelease(); | 
|---|
| [2870] | 454 | *phMod = pMod ? pMod->hMod : NIL_HKLDRMOD; | 
|---|
| [2833] | 455 | } | 
|---|
|  | 456 | return rc; | 
|---|
|  | 457 | } | 
|---|
|  | 458 |  | 
|---|
|  | 459 |  | 
|---|
|  | 460 | /** | 
|---|
|  | 461 | * Gets the module name. | 
|---|
|  | 462 | * | 
|---|
|  | 463 | * @returns 0 on success and pszName filled with the name. | 
|---|
|  | 464 | * @returns KLDR_ERR_INVALID_HANDLE or KLDR_ERR_BUFFER_OVERFLOW on failure. | 
|---|
|  | 465 | * @param   hMod        The module handle. | 
|---|
|  | 466 | * @param   pszName     Where to put the name. | 
|---|
|  | 467 | * @param   cchName     The size of the name buffer. | 
|---|
|  | 468 | * @see kLdrDyldGetFilename | 
|---|
|  | 469 | */ | 
|---|
|  | 470 | int     kLdrDyldGetName(HKLDRMOD hMod, char *pszName, size_t cchName) | 
|---|
|  | 471 | { | 
|---|
|  | 472 | int rc; | 
|---|
|  | 473 |  | 
|---|
|  | 474 | /* validate */ | 
|---|
|  | 475 | if (pszName && cchName) | 
|---|
|  | 476 | *pszName = '\0'; | 
|---|
|  | 477 | KLDRDYLD_VALIDATE_HKLDRMOD(hMod); | 
|---|
|  | 478 | KLDRHLP_VALIDATE_BUFFER(pszName, cchName); | 
|---|
|  | 479 |  | 
|---|
|  | 480 | /* get sem & do work */ | 
|---|
|  | 481 | rc = kldrHlpSemRequest(); | 
|---|
|  | 482 | if (!rc) | 
|---|
|  | 483 | { | 
|---|
| [2836] | 484 | rc = kldrDyldDoGetName(hMod, pszName, cchName); | 
|---|
| [2833] | 485 | kldrHlpSemRelease(); | 
|---|
|  | 486 | } | 
|---|
|  | 487 | return rc; | 
|---|
|  | 488 | } | 
|---|
|  | 489 |  | 
|---|
|  | 490 |  | 
|---|
|  | 491 | /** | 
|---|
|  | 492 | * Gets the module filename. | 
|---|
|  | 493 | * | 
|---|
|  | 494 | * @returns 0 on success and pszFilename filled with the name. | 
|---|
|  | 495 | * @returns KLDR_ERR_INVALID_HANDLE or KLDR_ERR_BUFFER_OVERFLOW on failure. | 
|---|
|  | 496 | * @param   hMod            The module handle. | 
|---|
|  | 497 | * @param   pszFilename     Where to put the filename. | 
|---|
|  | 498 | * @param   cchFilename     The size of the filename buffer. | 
|---|
|  | 499 | * @see kLdrDyldGetName | 
|---|
|  | 500 | */ | 
|---|
|  | 501 | int     kLdrDyldGetFilename(HKLDRMOD hMod, char *pszFilename, size_t cchFilename) | 
|---|
|  | 502 | { | 
|---|
|  | 503 | int rc; | 
|---|
|  | 504 |  | 
|---|
|  | 505 | /* validate & initialize */ | 
|---|
|  | 506 | if  (pszFilename && cchFilename); | 
|---|
|  | 507 | *pszFilename = '\0'; | 
|---|
|  | 508 | KLDRDYLD_VALIDATE_HKLDRMOD(hMod); | 
|---|
|  | 509 | KLDRHLP_VALIDATE_BUFFER(pszFilename, cchFilename); | 
|---|
|  | 510 |  | 
|---|
|  | 511 | /* get sem & do work */ | 
|---|
|  | 512 | rc = kldrHlpSemRequest(); | 
|---|
|  | 513 | if (!rc) | 
|---|
|  | 514 | { | 
|---|
| [2836] | 515 | rc = kldrDyldDoGetFilename(hMod, pszFilename, cchFilename); | 
|---|
| [2833] | 516 | kldrHlpSemRelease(); | 
|---|
|  | 517 | } | 
|---|
|  | 518 | return rc; | 
|---|
|  | 519 | } | 
|---|
|  | 520 |  | 
|---|
|  | 521 |  | 
|---|
|  | 522 | /** | 
|---|
|  | 523 | * Queries the value and type of a symbol. | 
|---|
|  | 524 | * | 
|---|
|  | 525 | * @returns 0 on success and pValue and pfKind set. | 
|---|
|  | 526 | * @returns KLDR_ERR_INVALID_HANDLE or KLDR_ERR_SYMBOL_NOT_FOUND on failure. | 
|---|
| [2893] | 527 | * @param   hMod                The module handle. | 
|---|
|  | 528 | * @param   uSymbolOrdinal      The symbol ordinal. This is ignored if pszSymbolName is non-zero. | 
|---|
|  | 529 | * @param   pszSymbolName       The symbol name. | 
|---|
|  | 530 | * @param   pszSymbolVersion    The symbol version. Optional. | 
|---|
|  | 531 | * @param   pValue              Where to put the symbol value. Optional if pfKind is non-zero. | 
|---|
|  | 532 | * @param   pfKind              Where to put the symbol kind flags. Optional if pValue is non-zero. | 
|---|
| [2833] | 533 | */ | 
|---|
| [2893] | 534 | int     kLdrDyldQuerySymbol(HKLDRMOD hMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, | 
|---|
|  | 535 | const char *pszSymbolVersion, uintptr_t *pValue, uint32_t *pfKind) | 
|---|
| [2833] | 536 | { | 
|---|
|  | 537 | int rc; | 
|---|
|  | 538 |  | 
|---|
|  | 539 | /* validate & initialize */ | 
|---|
|  | 540 | if (pfKind) | 
|---|
|  | 541 | *pfKind = 0; | 
|---|
|  | 542 | if (pValue) | 
|---|
|  | 543 | *pValue = 0; | 
|---|
|  | 544 | if (!pfKind && !pValue) | 
|---|
|  | 545 | return KLDR_ERR_INVALID_PARAMETER; | 
|---|
|  | 546 | KLDRDYLD_VALIDATE_HKLDRMOD(hMod); | 
|---|
|  | 547 | KLDRHLP_VALIDATE_OPTIONAL_STRING(pszSymbolName); | 
|---|
|  | 548 |  | 
|---|
|  | 549 | /* get sem & do work */ | 
|---|
|  | 550 | rc = kldrHlpSemRequest(); | 
|---|
|  | 551 | if (!rc) | 
|---|
|  | 552 | { | 
|---|
| [2836] | 553 | rc = kldrDyldDoQuerySymbol(hMod, uSymbolOrdinal, pszSymbolName, pValue, pfKind); | 
|---|
| [2833] | 554 | kldrHlpSemRelease(); | 
|---|
|  | 555 | } | 
|---|
|  | 556 | return rc; | 
|---|
|  | 557 | } | 
|---|
|  | 558 |  | 
|---|
|  | 559 |  | 
|---|
| [2843] | 560 | /** | 
|---|
|  | 561 | * Worker kLdrDoLoadExe(). | 
|---|
|  | 562 | * Used after we've switch to the final process stack. | 
|---|
|  | 563 | * | 
|---|
|  | 564 | * @param   pExe    The executable module. | 
|---|
|  | 565 | * @internal | 
|---|
|  | 566 | */ | 
|---|
|  | 567 | void kldrDyldDoLoadExe(PKLDRDYLDMOD pExe) | 
|---|
|  | 568 | { | 
|---|
|  | 569 | int rc; | 
|---|
| [2833] | 570 |  | 
|---|
| [2843] | 571 | /* | 
|---|
|  | 572 | * Load the executable module with its prerequisites and initialize them. | 
|---|
|  | 573 | */ | 
|---|
|  | 574 | g_cActiveLoadCalls++; | 
|---|
|  | 575 | rc = kldrDyldDoLoad2(pExe, NULL, NULL, kLdrDyldSearch, kLdrDyldFlags | KLDRDYLD_LOAD_FLAGS_EXECUTABLE); | 
|---|
|  | 576 | if (rc) | 
|---|
|  | 577 | kldrDyldFailure(rc, "load 2 failed for '%s', rc=%d", pExe->pMod->pszFilename); | 
|---|
|  | 578 | g_cActiveLoadCalls--; | 
|---|
|  | 579 | kldrDyldDoModuleTerminationAndGarabageCollection(); | 
|---|
|  | 580 |  | 
|---|
|  | 581 | /* | 
|---|
|  | 582 | * Invoke the executable entry point. | 
|---|
|  | 583 | */ | 
|---|
|  | 584 | kldrDyldModStartExe(pExe); | 
|---|
|  | 585 | kldrDyldFailure(-1, "failed to invoke main!"); | 
|---|
|  | 586 | } | 
|---|
|  | 587 |  | 
|---|
|  | 588 |  | 
|---|
| [2837] | 589 | /** | 
|---|
| [2842] | 590 | * Worker for kLdrDyldLoad() and helper for kLdrDyldLoadExe(). | 
|---|
|  | 591 | * @internal | 
|---|
| [2837] | 592 | */ | 
|---|
| [2843] | 593 | static int kldrDyldDoLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch, | 
|---|
| [2842] | 594 | unsigned fFlags, PPKLDRDYLDMOD ppMod, char *pszErr, size_t cchErr) | 
|---|
| [2837] | 595 | { | 
|---|
|  | 596 | int rc; | 
|---|
| [2833] | 597 |  | 
|---|
| [2837] | 598 | /* | 
|---|
|  | 599 | * Try find the module among the ones that's already loaded. | 
|---|
|  | 600 | */ | 
|---|
| [2843] | 601 | rc = kldrDyldFindExistingModule(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, ppMod); | 
|---|
| [2837] | 602 | if (!rc) | 
|---|
|  | 603 | { | 
|---|
|  | 604 | switch ((*ppMod)->enmState) | 
|---|
|  | 605 | { | 
|---|
|  | 606 | /* | 
|---|
| [2842] | 607 | * Prerequisites are ok, so nothing to do really. | 
|---|
| [2837] | 608 | */ | 
|---|
|  | 609 | case KLDRSTATE_GOOD: | 
|---|
|  | 610 | case KLDRSTATE_INITIALIZING: | 
|---|
| [2842] | 611 | return kldrDyldModDynamicLoad(*ppMod); | 
|---|
| [2837] | 612 |  | 
|---|
|  | 613 | /* | 
|---|
| [2842] | 614 | * The module can't be loaded because it failed to initialize. | 
|---|
| [2837] | 615 | */ | 
|---|
| [2842] | 616 | case KLDRSTATE_INITIALIZATION_FAILED: | 
|---|
|  | 617 | return KLDR_ERR_MODULE_INIT_FAILED_ALREADY; | 
|---|
| [2837] | 618 |  | 
|---|
|  | 619 | /* | 
|---|
| [2842] | 620 | * Prerequisites needs loading / reattaching and the module | 
|---|
|  | 621 | * (may depending on fFlags) needs to be initialized. | 
|---|
| [2837] | 622 | */ | 
|---|
| [2842] | 623 | case KLDRSTATE_PENDING_INITIALIZATION: | 
|---|
| [2837] | 624 | break; | 
|---|
|  | 625 |  | 
|---|
|  | 626 | /* | 
|---|
| [2842] | 627 | * Prerequisites needs to be loaded again | 
|---|
| [2837] | 628 | */ | 
|---|
|  | 629 | case KLDRSTATE_PENDING_TERMINATION: | 
|---|
|  | 630 | break; | 
|---|
|  | 631 |  | 
|---|
|  | 632 | /* | 
|---|
|  | 633 | * The module has been terminated so it need to be reloaded, have it's | 
|---|
|  | 634 | * prereqs loaded, fixed up and initialized before we can use it again. | 
|---|
|  | 635 | */ | 
|---|
|  | 636 | case KLDRSTATE_PENDING_GC: | 
|---|
|  | 637 | rc = kldrDyldModReload(*ppMod); | 
|---|
|  | 638 | if (rc) | 
|---|
|  | 639 | return kldrDyldCopyError(rc, pszErr, cchErr); | 
|---|
|  | 640 | break; | 
|---|
|  | 641 |  | 
|---|
|  | 642 | /* | 
|---|
|  | 643 | * Forget it, we don't know how to deal with re-initialization here. | 
|---|
|  | 644 | */ | 
|---|
|  | 645 | case KLDRSTATE_TERMINATING: | 
|---|
|  | 646 | KLDRDYLD_ASSERT(!"KLDR_ERR_MODULE_TERMINATING"); | 
|---|
|  | 647 | return KLDR_ERR_MODULE_TERMINATING; | 
|---|
|  | 648 |  | 
|---|
|  | 649 | /* | 
|---|
|  | 650 | * Invalid state. | 
|---|
|  | 651 | */ | 
|---|
|  | 652 | default: | 
|---|
|  | 653 | KLDRDYLD_ASSERT(!"invalid state"); | 
|---|
|  | 654 | break; | 
|---|
| [2836] | 655 | } | 
|---|
|  | 656 | } | 
|---|
| [2837] | 657 | else | 
|---|
|  | 658 | { | 
|---|
|  | 659 | /* | 
|---|
|  | 660 | * We'll have to load it from file. | 
|---|
|  | 661 | */ | 
|---|
| [2843] | 662 | rc = kldrDyldFindNewModule(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, ppMod); | 
|---|
| [2837] | 663 | if (rc) | 
|---|
|  | 664 | return kldrDyldCopyError(rc, pszErr, cchErr); | 
|---|
|  | 665 | rc = kldrDyldModMap(*ppMod); | 
|---|
|  | 666 | } | 
|---|
| [2833] | 667 |  | 
|---|
| [2843] | 668 | /* | 
|---|
|  | 669 | * Join cause with kLdrDyldLoadExe. | 
|---|
|  | 670 | */ | 
|---|
| [2836] | 671 | if (!rc) | 
|---|
| [2843] | 672 | rc = kldrDyldDoLoad2(*ppMod, pszPrefix, pszSuffix, enmSearch, fFlags); | 
|---|
|  | 673 | else | 
|---|
| [2845] | 674 | kldrDyldStackCleanupOne(*ppMod, rc); | 
|---|
| [2833] | 675 |  | 
|---|
| [2843] | 676 | /* | 
|---|
|  | 677 | * Copy any error or warning to the error buffer. | 
|---|
|  | 678 | */ | 
|---|
|  | 679 | return kldrDyldCopyError(rc, pszErr, cchErr); | 
|---|
|  | 680 | } | 
|---|
| [2837] | 681 |  | 
|---|
| [2843] | 682 |  | 
|---|
|  | 683 | /** | 
|---|
|  | 684 | * 2nd half of kLdrDyldLoad() and kLdrDyldLoadExe(). | 
|---|
|  | 685 | * | 
|---|
|  | 686 | * @internal | 
|---|
|  | 687 | */ | 
|---|
|  | 688 | static int kldrDyldDoLoad2(PKLDRDYLDMOD pLoadedMod, const char *pszPrefix, const char *pszSuffix, | 
|---|
|  | 689 | KLDRDYLDSEARCH enmSearch, unsigned fFlags) | 
|---|
|  | 690 | { | 
|---|
|  | 691 | /* | 
|---|
|  | 692 | * Load prerequisites. | 
|---|
|  | 693 | */ | 
|---|
|  | 694 | uint32_t i; | 
|---|
|  | 695 | uint32_t iLoad1st = kldrDyldStackNewFrame(pLoadedMod); | 
|---|
|  | 696 | int rc = kldrDyldDoLoadPrerequisites(pLoadedMod, pszPrefix, pszSuffix, enmSearch, fFlags); | 
|---|
|  | 697 | uint32_t iLoadEnd = kldrDyldStackFrameCompleted(); | 
|---|
| [2845] | 698 | if (rc) | 
|---|
| [2887] | 699 | { | 
|---|
|  | 700 | kldrDyldModAddRef(pLoadedMod); | 
|---|
| [2845] | 701 | kldrDyldStackCleanupOne(pLoadedMod, rc); /* in case it didn't get pushed onto the stack. */ | 
|---|
| [2887] | 702 | kldrDyldModDeref(pLoadedMod); | 
|---|
|  | 703 | } | 
|---|
| [2843] | 704 |  | 
|---|
|  | 705 | /* | 
|---|
|  | 706 | * Apply fixups. | 
|---|
|  | 707 | */ | 
|---|
|  | 708 | for (i = iLoad1st; !rc && i < iLoadEnd; i++) | 
|---|
|  | 709 | { | 
|---|
|  | 710 | PKLDRDYLDMOD pMod = g_papStackMods[i]; | 
|---|
|  | 711 | if (    pMod->enmState == KLDRSTATE_LOADED_PREREQUISITES | 
|---|
|  | 712 | ||  pMod->enmState == KLDRSTATE_RELOADED_LOADED_PREREQUISITES) | 
|---|
|  | 713 | rc = kldrDyldModFixup(pMod); | 
|---|
|  | 714 | } | 
|---|
|  | 715 |  | 
|---|
|  | 716 | /* | 
|---|
|  | 717 | * Advance fixed up module onto initialization. | 
|---|
|  | 718 | */ | 
|---|
|  | 719 | for (i = iLoad1st; !rc && i < iLoadEnd; i++) | 
|---|
|  | 720 | { | 
|---|
|  | 721 | PKLDRDYLDMOD pMod = g_papStackMods[i]; | 
|---|
|  | 722 | if (    pMod->enmState == KLDRSTATE_FIXED_UP | 
|---|
|  | 723 | ||  pMod->enmState == KLDRSTATE_RELOADED_FIXED_UP) | 
|---|
|  | 724 | pMod->enmState = KLDRSTATE_PENDING_INITIALIZATION; | 
|---|
|  | 725 | KLDRDYLD_ASSERT(    pMod->enmState == KLDRSTATE_PENDING_INITIALIZATION | 
|---|
|  | 726 | ||  pMod->enmState == KLDRSTATE_GOOD); | 
|---|
|  | 727 | } | 
|---|
|  | 728 |  | 
|---|
|  | 729 | /* | 
|---|
|  | 730 | * Call the initializers if we're loading in recursive mode or | 
|---|
|  | 731 | * if we're the outermost load call. | 
|---|
|  | 732 | */ | 
|---|
|  | 733 | if (fFlags & KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT) | 
|---|
|  | 734 | { | 
|---|
| [2842] | 735 | for (i = iLoad1st; !rc && i < iLoadEnd; i++) | 
|---|
|  | 736 | { | 
|---|
|  | 737 | PKLDRDYLDMOD pMod = g_papStackMods[i]; | 
|---|
| [2843] | 738 | if (pMod->enmState == KLDRSTATE_PENDING_INITIALIZATION) | 
|---|
|  | 739 | rc = kldrDyldModCallInit(pMod); | 
|---|
|  | 740 | else if (pMod->enmState == KLDRSTATE_INITIALIZATION_FAILED) | 
|---|
|  | 741 | rc = KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED_ALREADY; | 
|---|
|  | 742 | else | 
|---|
|  | 743 | KLDRDYLD_ASSERT(g_papStackMods[i]->enmState == KLDRSTATE_GOOD); | 
|---|
| [2837] | 744 | } | 
|---|
| [2842] | 745 | #ifdef KLDRDYLD_STRICT | 
|---|
| [2843] | 746 | for (i = iLoad1st; !rc && i < iLoadEnd; i++) | 
|---|
|  | 747 | KLDRDYLD_ASSERT(g_papStackMods[i]->enmState == KLDRSTATE_GOOD); | 
|---|
| [2842] | 748 | #endif | 
|---|
| [2843] | 749 | } | 
|---|
|  | 750 | else if (g_cActiveLoadCalls <= 1) | 
|---|
|  | 751 | { | 
|---|
|  | 752 | while (!rc && g_pkLdrDyldInitHead) | 
|---|
| [2842] | 753 | { | 
|---|
| [2843] | 754 | PKLDRDYLDMOD pMod = g_pkLdrDyldInitHead; | 
|---|
|  | 755 | g_pkLdrDyldInitHead = pMod->InitTerm.pNext; | 
|---|
|  | 756 | if (pMod->InitTerm.pNext) | 
|---|
|  | 757 | pMod->InitTerm.pNext->InitTerm.pPrev = NULL; | 
|---|
| [2871] | 758 | else | 
|---|
|  | 759 | g_pkLdrDyldInitTail = NULL; | 
|---|
|  | 760 | pMod->fInitList = 0; | 
|---|
| [2843] | 761 | rc = kldrDyldModCallInit(pMod); | 
|---|
| [2837] | 762 | } | 
|---|
| [2843] | 763 | } | 
|---|
| [2837] | 764 |  | 
|---|
| [2843] | 765 | /* | 
|---|
|  | 766 | * Complete the load by incrementing the dynamic load count of the | 
|---|
|  | 767 | * requested module (return handle is already set). | 
|---|
|  | 768 | */ | 
|---|
|  | 769 | if (!rc) | 
|---|
| [2837] | 770 | { | 
|---|
| [2843] | 771 | if (fFlags & KLDRDYLD_LOAD_FLAGS_EXECUTABLE) | 
|---|
| [2875] | 772 | { | 
|---|
|  | 773 | pLoadedMod->cDepRefs++; /* just make it stick. */ | 
|---|
|  | 774 | pLoadedMod->cRefs++; | 
|---|
|  | 775 | } | 
|---|
| [2843] | 776 | else | 
|---|
|  | 777 | rc = kldrDyldModDynamicLoad(pLoadedMod); | 
|---|
| [2837] | 778 | } | 
|---|
|  | 779 |  | 
|---|
| [2843] | 780 | kldrDyldStackDropFrame(iLoad1st, iLoadEnd, rc); | 
|---|
|  | 781 | return rc; | 
|---|
| [2833] | 782 | } | 
|---|
|  | 783 |  | 
|---|
|  | 784 |  | 
|---|
|  | 785 | /** | 
|---|
| [2842] | 786 | * kldrDyldDoLoad() helper which will load prerequisites and | 
|---|
|  | 787 | * build the initialization array / list. | 
|---|
|  | 788 | * | 
|---|
|  | 789 | * @returns 0 on success, non-zero error code on failure. | 
|---|
|  | 790 | * @param   pMod            The module to start at. | 
|---|
| [2847] | 791 | * @param   pszPrefix       Prefix to use when searching. | 
|---|
|  | 792 | * @param   pszSuffix       Suffix to use when searching. | 
|---|
| [2842] | 793 | * @param   enmSearch       Method to use when locating the module and any modules it may depend on. | 
|---|
|  | 794 | * @param   fFlags          Flags, a combintation of the KLDRYDLD_LOAD_FLAGS_* \#defines. | 
|---|
| [2833] | 795 | */ | 
|---|
| [2843] | 796 | static int kldrDyldDoLoadPrerequisites(PKLDRDYLDMOD pMod, const char *pszPrefix, const char *pszSuffix, | 
|---|
| [2842] | 797 | KLDRDYLDSEARCH enmSearch, unsigned fFlags) | 
|---|
| [2833] | 798 | { | 
|---|
| [2842] | 799 | static struct | 
|---|
|  | 800 | { | 
|---|
|  | 801 | /** The module. */ | 
|---|
|  | 802 | PKLDRDYLDMOD    pMod; | 
|---|
|  | 803 | /** The number of prerequisite modules left to process. | 
|---|
|  | 804 | * This starts at ~0U to inidicate that we need to load/check prerequisistes. */ | 
|---|
|  | 805 | unsigned        cLeft; | 
|---|
|  | 806 | }               s_aEntries[64]; | 
|---|
|  | 807 | unsigned        cEntries; | 
|---|
|  | 808 | int             rc = 0; | 
|---|
| [2833] | 809 |  | 
|---|
| [2868] | 810 | /* Prerequisites are always global and they just aren't executables. */ | 
|---|
|  | 811 | fFlags &= ~(KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE | KLDRDYLD_LOAD_FLAGS_EXECUTABLE); | 
|---|
|  | 812 |  | 
|---|
| [2842] | 813 | /* push the first entry. */ | 
|---|
|  | 814 | s_aEntries[0].pMod = pMod; | 
|---|
|  | 815 | s_aEntries[0].cLeft = ~0U; | 
|---|
|  | 816 | cEntries = 1; | 
|---|
| [2833] | 817 |  | 
|---|
| [2842] | 818 | /* | 
|---|
|  | 819 | * The recursion loop. | 
|---|
|  | 820 | */ | 
|---|
|  | 821 | while (!rc && cEntries > 0) | 
|---|
|  | 822 | { | 
|---|
|  | 823 | const unsigned i = cEntries - 1; | 
|---|
|  | 824 | pMod = s_aEntries[i].pMod; | 
|---|
|  | 825 | if (s_aEntries[i].cLeft == ~0U) | 
|---|
|  | 826 | { | 
|---|
|  | 827 | /* | 
|---|
|  | 828 | * Load prerequisite modules. | 
|---|
|  | 829 | */ | 
|---|
|  | 830 | switch (pMod->enmState) | 
|---|
|  | 831 | { | 
|---|
|  | 832 | /* | 
|---|
|  | 833 | * Load immediate prerequisite modules and push the ones needing | 
|---|
|  | 834 | * attention onto the stack. | 
|---|
|  | 835 | */ | 
|---|
|  | 836 | case KLDRSTATE_MAPPED: | 
|---|
|  | 837 | case KLDRSTATE_RELOADED: | 
|---|
|  | 838 | case KLDRSTATE_PENDING_TERMINATION: | 
|---|
| [2843] | 839 | rc = kldrDyldModLoadPrerequisites(pMod, pszPrefix, pszSuffix, enmSearch, fFlags); | 
|---|
| [2842] | 840 | KLDRDYLD_ASSERT(    pMod->enmState == KLDRSTATE_GOOD | 
|---|
|  | 841 | ||  pMod->enmState == KLDRSTATE_RELOADED_LOADED_PREREQUISITES | 
|---|
|  | 842 | ||  pMod->enmState == KLDRSTATE_LOADED_PREREQUISITES | 
|---|
|  | 843 | ||  rc); | 
|---|
|  | 844 | if (!rc) | 
|---|
|  | 845 | s_aEntries[i].cLeft = pMod->cPrereqs; | 
|---|
|  | 846 | break; | 
|---|
| [2833] | 847 |  | 
|---|
| [2842] | 848 | /* | 
|---|
|  | 849 | * Check its prerequisite modules the first time around. | 
|---|
|  | 850 | */ | 
|---|
|  | 851 | case KLDRSTATE_PENDING_INITIALIZATION: | 
|---|
|  | 852 | if (pMod->fAlreadySeen) | 
|---|
|  | 853 | break; | 
|---|
|  | 854 | pMod->fAlreadySeen = 1; | 
|---|
|  | 855 | s_aEntries[i].cLeft = pMod->cPrereqs; | 
|---|
|  | 856 | break; | 
|---|
| [2833] | 857 |  | 
|---|
| [2842] | 858 | /* | 
|---|
|  | 859 | * These are ok. | 
|---|
|  | 860 | */ | 
|---|
|  | 861 | case KLDRSTATE_LOADED_PREREQUISITES: | 
|---|
|  | 862 | case KLDRSTATE_RELOADED_LOADED_PREREQUISITES: | 
|---|
|  | 863 | case KLDRSTATE_INITIALIZING: | 
|---|
|  | 864 | case KLDRSTATE_GOOD: | 
|---|
|  | 865 | s_aEntries[i].cLeft = 0; | 
|---|
|  | 866 | break; | 
|---|
|  | 867 |  | 
|---|
|  | 868 | /* | 
|---|
|  | 869 | * All other stats are invalid. | 
|---|
|  | 870 | */ | 
|---|
|  | 871 | default: | 
|---|
|  | 872 | KLDRDYLD_ASSERT(!"invalid state"); | 
|---|
|  | 873 | break; | 
|---|
|  | 874 | } | 
|---|
|  | 875 | } | 
|---|
|  | 876 | else if (s_aEntries[i].cLeft > 0) | 
|---|
| [2836] | 877 | { | 
|---|
| [2842] | 878 | /* | 
|---|
|  | 879 | * Recurse down into the next prereq. | 
|---|
|  | 880 | */ | 
|---|
|  | 881 | KLDRDYLD_ASSERT(s_aEntries[i].cLeft <= pMod->cPrereqs); | 
|---|
|  | 882 | if (cEntries < sizeof(s_aEntries) / sizeof(s_aEntries[0])) | 
|---|
| [2836] | 883 | { | 
|---|
| [2869] | 884 | s_aEntries[cEntries].cLeft = ~(uint32_t)0; | 
|---|
|  | 885 | s_aEntries[cEntries].pMod = pMod->papPrereqs[pMod->cPrereqs - s_aEntries[i].cLeft]; | 
|---|
| [2842] | 886 | s_aEntries[i].cLeft--; | 
|---|
| [2869] | 887 | cEntries++; | 
|---|
| [2836] | 888 | } | 
|---|
| [2842] | 889 | else | 
|---|
|  | 890 | rc = KLDR_ERR_PREREQUISITE_RECURSED_TOO_DEEPLY; | 
|---|
| [2836] | 891 | } | 
|---|
| [2842] | 892 | else | 
|---|
|  | 893 | { | 
|---|
|  | 894 | /* | 
|---|
|  | 895 | * We're done with this module, record it for init/cleanup. | 
|---|
|  | 896 | */ | 
|---|
|  | 897 | cEntries--; | 
|---|
|  | 898 | if (pMod->enmState != KLDRSTATE_GOOD) | 
|---|
|  | 899 | { | 
|---|
|  | 900 | kldrDyldStackAddModule(pMod); | 
|---|
|  | 901 | if  (   !(fFlags & KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT) | 
|---|
|  | 902 | && !pMod->fInitList) | 
|---|
|  | 903 | { | 
|---|
|  | 904 | pMod->fInitList = 1; | 
|---|
|  | 905 | pMod->InitTerm.pNext = NULL; | 
|---|
|  | 906 | pMod->InitTerm.pPrev = g_pkLdrDyldInitTail; | 
|---|
|  | 907 | if (g_pkLdrDyldInitTail) | 
|---|
|  | 908 | g_pkLdrDyldInitTail->InitTerm.pNext = pMod; | 
|---|
|  | 909 | else | 
|---|
|  | 910 | g_pkLdrDyldInitHead = pMod; | 
|---|
|  | 911 | g_pkLdrDyldInitTail = pMod; | 
|---|
|  | 912 | } | 
|---|
|  | 913 | } | 
|---|
|  | 914 | } | 
|---|
| [2836] | 915 | } | 
|---|
|  | 916 |  | 
|---|
| [2842] | 917 | return rc; | 
|---|
| [2833] | 918 | } | 
|---|
|  | 919 |  | 
|---|
|  | 920 |  | 
|---|
|  | 921 | /** | 
|---|
| [2842] | 922 | * Gets prerequisite module. | 
|---|
|  | 923 | * | 
|---|
|  | 924 | * This will try load the requested module if necessary, returning it in the MAPPED state. | 
|---|
|  | 925 | * | 
|---|
|  | 926 | * @returns 0 on success. | 
|---|
|  | 927 | * @returns KLDR_ERR_MODULE_NOT_FOUND or I/O error on failure. | 
|---|
|  | 928 | * @param   pszDll          The name of the dll to look for. | 
|---|
| [2843] | 929 | * @param   pszPrefix    Prefix than can be used when searching. | 
|---|
|  | 930 | * @param   pszSuffix    Suffix than can be used when searching. | 
|---|
| [2842] | 931 | * @param   enmSearch       Method to use when locating the module. | 
|---|
|  | 932 | * @param   fFlags          Flags, a combintation of the KLDRYDLD_LOAD_FLAGS_* \#defines. | 
|---|
|  | 933 | * @param   pDep            The depentant module. | 
|---|
|  | 934 | * @param   ppMod           Where to put the module we get. | 
|---|
| [2833] | 935 | */ | 
|---|
| [2843] | 936 | int kldrDyldGetPrerequisite(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch, | 
|---|
| [2842] | 937 | unsigned fFlags, PKLDRDYLDMOD pDep, PPKLDRDYLDMOD ppMod) | 
|---|
| [2833] | 938 | { | 
|---|
| [2842] | 939 | int             rc; | 
|---|
|  | 940 | PKLDRDYLDMOD    pMod; | 
|---|
| [2833] | 941 |  | 
|---|
| [2842] | 942 | *ppMod = NULL; | 
|---|
| [2833] | 943 |  | 
|---|
| [2825] | 944 | /* | 
|---|
| [2842] | 945 | * Try find the module among the ones that's already loaded. | 
|---|
|  | 946 | * | 
|---|
|  | 947 | * This is very similar to the kldrDyldDoLoad code, except it has to deal with | 
|---|
|  | 948 | * a couple of additional states and occurs only during prerequisite loading | 
|---|
|  | 949 | * and the action taken is a little bit different. | 
|---|
| [2825] | 950 | */ | 
|---|
| [2843] | 951 | rc = kldrDyldFindExistingModule(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, &pMod); | 
|---|
| [2842] | 952 | if (!rc) | 
|---|
|  | 953 | { | 
|---|
|  | 954 | switch (pMod->enmState) | 
|---|
|  | 955 | { | 
|---|
|  | 956 | /* | 
|---|
|  | 957 | * These are good. | 
|---|
|  | 958 | */ | 
|---|
|  | 959 | case KLDRSTATE_MAPPED: | 
|---|
|  | 960 | case KLDRSTATE_RELOADED: | 
|---|
|  | 961 | case KLDRSTATE_LOADED_PREREQUISITES: | 
|---|
|  | 962 | case KLDRSTATE_RELOADED_LOADED_PREREQUISITES: | 
|---|
|  | 963 | case KLDRSTATE_PENDING_INITIALIZATION: | 
|---|
|  | 964 | case KLDRSTATE_INITIALIZING: | 
|---|
|  | 965 | case KLDRSTATE_GOOD: | 
|---|
|  | 966 | case KLDRSTATE_PENDING_TERMINATION: | 
|---|
|  | 967 | break; | 
|---|
| [2825] | 968 |  | 
|---|
| [2842] | 969 | /* | 
|---|
|  | 970 | * The module has been terminated so it need to be reloaded, have it's | 
|---|
|  | 971 | * prereqs loaded, fixed up and initialized before we can use it again. | 
|---|
|  | 972 | */ | 
|---|
|  | 973 | case KLDRSTATE_PENDING_GC: | 
|---|
|  | 974 | rc = kldrDyldModReload(pMod); | 
|---|
|  | 975 | break; | 
|---|
| [2825] | 976 |  | 
|---|
| [2842] | 977 | /* | 
|---|
|  | 978 | * The module can't be loaded because it failed to initialize already. | 
|---|
|  | 979 | */ | 
|---|
|  | 980 | case KLDRSTATE_INITIALIZATION_FAILED: | 
|---|
|  | 981 | rc = KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED; | 
|---|
|  | 982 | break; | 
|---|
| [2825] | 983 |  | 
|---|
| [2842] | 984 | /* | 
|---|
|  | 985 | * Forget it, no idea how to deal with re-initialization. | 
|---|
|  | 986 | */ | 
|---|
|  | 987 | case KLDRSTATE_TERMINATING: | 
|---|
|  | 988 | return KLDR_ERR_PREREQUISITE_MODULE_TERMINATING; | 
|---|
| [2825] | 989 |  | 
|---|
| [2842] | 990 | /* | 
|---|
|  | 991 | * Invalid state. | 
|---|
|  | 992 | */ | 
|---|
|  | 993 | default: | 
|---|
|  | 994 | KLDRDYLD_ASSERT(!"invalid state"); | 
|---|
|  | 995 | break; | 
|---|
| [2825] | 996 | } | 
|---|
| [2842] | 997 | } | 
|---|
|  | 998 | else | 
|---|
| [2825] | 999 | { | 
|---|
| [2842] | 1000 | /* | 
|---|
|  | 1001 | * We'll have to load it from file. | 
|---|
|  | 1002 | */ | 
|---|
| [2869] | 1003 | rc = kldrDyldFindNewModule(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, &pMod); | 
|---|
| [2842] | 1004 | if (!rc) | 
|---|
|  | 1005 | rc = kldrDyldModMap(pMod); | 
|---|
| [2825] | 1006 | } | 
|---|
|  | 1007 |  | 
|---|
|  | 1008 | /* | 
|---|
| [2842] | 1009 | * On success add dependency. | 
|---|
| [2825] | 1010 | */ | 
|---|
| [2842] | 1011 | if (!rc) | 
|---|
| [2825] | 1012 | { | 
|---|
| [2845] | 1013 | kldrDyldModAddDep(pMod, pDep); | 
|---|
|  | 1014 | *ppMod = pMod; | 
|---|
| [2825] | 1015 | } | 
|---|
| [2842] | 1016 | return rc; | 
|---|
| [2825] | 1017 | } | 
|---|
|  | 1018 |  | 
|---|
|  | 1019 |  | 
|---|
|  | 1020 | /** | 
|---|
| [2842] | 1021 | * Starts a new load stack frame. | 
|---|
| [2837] | 1022 | * | 
|---|
| [2842] | 1023 | * @returns Where the new stack frame starts. | 
|---|
|  | 1024 | * @param   pLoadMod        The module being loaded (only used for asserting). | 
|---|
| [2837] | 1025 | */ | 
|---|
| [2842] | 1026 | static uint32_t kldrDyldStackNewFrame(PKLDRDYLDMOD pLoadMod) | 
|---|
| [2837] | 1027 | { | 
|---|
|  | 1028 | /* | 
|---|
| [2842] | 1029 | * Clear the fAlreadySeen flags. | 
|---|
| [2837] | 1030 | */ | 
|---|
| [2842] | 1031 | PKLDRDYLDMOD pMod = kLdrDyldHead; | 
|---|
|  | 1032 | while (pMod) | 
|---|
| [2837] | 1033 | { | 
|---|
| [2842] | 1034 | pMod->fAlreadySeen = 0; | 
|---|
| [2837] | 1035 |  | 
|---|
| [2842] | 1036 | #ifdef KLDRDYLD_ASSERT | 
|---|
| [2837] | 1037 | switch (pMod->enmState) | 
|---|
|  | 1038 | { | 
|---|
| [2842] | 1039 | case KLDRSTATE_MAPPED: | 
|---|
|  | 1040 | case KLDRSTATE_RELOADED: | 
|---|
|  | 1041 | /* only the just loaded module can be in this state. */ | 
|---|
|  | 1042 | KLDRDYLD_ASSERT(pMod == pLoadMod); | 
|---|
| [2837] | 1043 | break; | 
|---|
|  | 1044 |  | 
|---|
|  | 1045 | case KLDRSTATE_PENDING_INITIALIZATION: | 
|---|
| [2842] | 1046 | case KLDRSTATE_INITIALIZING: | 
|---|
| [2837] | 1047 | case KLDRSTATE_PENDING_TERMINATION: | 
|---|
|  | 1048 | case KLDRSTATE_PENDING_GC: | 
|---|
| [2842] | 1049 | case KLDRSTATE_TERMINATING: | 
|---|
|  | 1050 | case KLDRSTATE_INITIALIZATION_FAILED: | 
|---|
|  | 1051 | case KLDRSTATE_PENDING_DESTROY: | 
|---|
|  | 1052 | /* requires recursion. */ | 
|---|
|  | 1053 | KLDRDYLD_ASSERT(g_cActiveLoadCalls + g_cActiveLoadCalls + g_fActiveGC > 1); | 
|---|
| [2837] | 1054 | break; | 
|---|
|  | 1055 |  | 
|---|
|  | 1056 | case KLDRSTATE_GOOD: | 
|---|
| [2842] | 1057 | /* requires nothing. */ | 
|---|
| [2837] | 1058 | break; | 
|---|
|  | 1059 |  | 
|---|
|  | 1060 | default: | 
|---|
| [2842] | 1061 | KLDRDYLD_ASSERT(!"Invalid state"); | 
|---|
| [2837] | 1062 | break; | 
|---|
|  | 1063 | } | 
|---|
| [2842] | 1064 | #endif | 
|---|
| [2837] | 1065 |  | 
|---|
|  | 1066 | /* next */ | 
|---|
| [2836] | 1067 | pMod = pMod->Load.pNext; | 
|---|
|  | 1068 | } | 
|---|
| [2837] | 1069 | return g_cStackMods; | 
|---|
| [2836] | 1070 | } | 
|---|
|  | 1071 |  | 
|---|
|  | 1072 |  | 
|---|
|  | 1073 | /** | 
|---|
| [2837] | 1074 | * Records the module. | 
|---|
| [2836] | 1075 | * | 
|---|
|  | 1076 | * @return 0 on success, KLDR_ERR_NO_MEMORY if we can't expand the table. | 
|---|
|  | 1077 | * @param   pMod        The module to record. | 
|---|
|  | 1078 | */ | 
|---|
| [2842] | 1079 | static int kldrDyldStackAddModule(PKLDRDYLDMOD pMod) | 
|---|
| [2836] | 1080 | { | 
|---|
| [2837] | 1081 | /* | 
|---|
|  | 1082 | * Grow the stack if necessary. | 
|---|
|  | 1083 | */ | 
|---|
|  | 1084 | if (g_cStackMods + 1 > g_cStackModsAllocated) | 
|---|
|  | 1085 | { | 
|---|
|  | 1086 | uint32_t cNew = g_cStackModsAllocated ? g_cStackModsAllocated * 2 : 128; | 
|---|
|  | 1087 | void *pvOld = g_papStackMods; | 
|---|
|  | 1088 | void *pvNew = kldrHlpAlloc(cNew * sizeof(g_papStackMods[0])); | 
|---|
|  | 1089 | if (!pvNew) | 
|---|
|  | 1090 | return KLDR_ERR_NO_MEMORY; | 
|---|
|  | 1091 | kLdrHlpMemCopy(pvNew, pvOld, g_cStackMods * sizeof(g_papStackMods[0])); | 
|---|
|  | 1092 | g_papStackMods = (PPKLDRDYLDMOD)pvNew; | 
|---|
|  | 1093 | kldrHlpFree(pvOld); | 
|---|
|  | 1094 | } | 
|---|
|  | 1095 |  | 
|---|
|  | 1096 | /* | 
|---|
|  | 1097 | * Add a reference and push the module onto the stack. | 
|---|
|  | 1098 | */ | 
|---|
| [2845] | 1099 | kldrDyldModAddRef(pMod); | 
|---|
|  | 1100 | g_papStackMods[g_cStackMods++] = pMod; | 
|---|
|  | 1101 | return 0; | 
|---|
| [2836] | 1102 | } | 
|---|
|  | 1103 |  | 
|---|
|  | 1104 |  | 
|---|
| [2837] | 1105 | /** | 
|---|
|  | 1106 | * The frame has been completed. | 
|---|
|  | 1107 | * | 
|---|
|  | 1108 | * @returns Where the frame ends. | 
|---|
|  | 1109 | */ | 
|---|
|  | 1110 | static int kldrDyldStackFrameCompleted(void) | 
|---|
|  | 1111 | { | 
|---|
|  | 1112 | return g_cStackMods; | 
|---|
|  | 1113 | } | 
|---|
| [2836] | 1114 |  | 
|---|
| [2837] | 1115 |  | 
|---|
| [2836] | 1116 | /** | 
|---|
| [2845] | 1117 | * Worker routine for kldrDyldStackDropFrame() and kldrDyldDoLoad(). | 
|---|
|  | 1118 | * | 
|---|
|  | 1119 | * @param   pMod            The module to perform cleanups on. | 
|---|
|  | 1120 | * @param   rc              Used for state verification. | 
|---|
|  | 1121 | */ | 
|---|
|  | 1122 | static void kldrDyldStackCleanupOne(PKLDRDYLDMOD pMod, int rc) | 
|---|
|  | 1123 | { | 
|---|
|  | 1124 | switch (pMod->enmState) | 
|---|
|  | 1125 | { | 
|---|
|  | 1126 | /* | 
|---|
|  | 1127 | * Just push it along to the PENDING_DESTROY state. | 
|---|
|  | 1128 | */ | 
|---|
|  | 1129 | case KLDRSTATE_MAPPED: | 
|---|
|  | 1130 | KLDRDYLD_ASSERT(rc); | 
|---|
|  | 1131 | kldrDyldModUnmap(pMod); | 
|---|
|  | 1132 | KLDRDYLD_ASSERT(pMod->enmState == KLDRSTATE_PENDING_DESTROY); | 
|---|
|  | 1133 | break; | 
|---|
|  | 1134 |  | 
|---|
|  | 1135 | /* | 
|---|
|  | 1136 | * Move back to PENDING_GC. | 
|---|
|  | 1137 | */ | 
|---|
|  | 1138 | case KLDRSTATE_RELOADED: | 
|---|
|  | 1139 | KLDRDYLD_ASSERT(rc); | 
|---|
|  | 1140 | pMod->enmState = KLDRSTATE_PENDING_GC; | 
|---|
|  | 1141 | break; | 
|---|
|  | 1142 |  | 
|---|
|  | 1143 | /* | 
|---|
|  | 1144 | * Unload prerequisites and unmap the modules. | 
|---|
|  | 1145 | */ | 
|---|
|  | 1146 | case KLDRSTATE_LOADED_PREREQUISITES: | 
|---|
|  | 1147 | case KLDRSTATE_FIXED_UP: | 
|---|
|  | 1148 | KLDRDYLD_ASSERT(rc); | 
|---|
|  | 1149 | kldrDyldModUnloadPrerequisites(pMod); | 
|---|
|  | 1150 | KLDRDYLD_ASSERT(pMod->enmState == KLDRSTATE_PENDING_DESTROY); | 
|---|
|  | 1151 | kldrDyldModUnmap(pMod); | 
|---|
|  | 1152 | KLDRDYLD_ASSERT(pMod->enmState == KLDRSTATE_PENDING_DESTROY); | 
|---|
|  | 1153 | break; | 
|---|
|  | 1154 |  | 
|---|
|  | 1155 | /* | 
|---|
|  | 1156 | * Unload prerequisites and push it back to PENDING_GC. | 
|---|
|  | 1157 | */ | 
|---|
|  | 1158 | case KLDRSTATE_RELOADED_LOADED_PREREQUISITES: | 
|---|
|  | 1159 | case KLDRSTATE_RELOADED_FIXED_UP: | 
|---|
|  | 1160 | kldrDyldModUnloadPrerequisites(pMod); | 
|---|
|  | 1161 | KLDRDYLD_ASSERT(pMod->enmState == KLDRSTATE_PENDING_GC); | 
|---|
|  | 1162 | break; | 
|---|
|  | 1163 |  | 
|---|
|  | 1164 | /* | 
|---|
|  | 1165 | * Nothing to do, just asserting sanity. | 
|---|
|  | 1166 | */ | 
|---|
|  | 1167 | case KLDRSTATE_INITIALIZING: | 
|---|
|  | 1168 | /* Implies there is another load going on. */ | 
|---|
|  | 1169 | KLDRDYLD_ASSERT(g_cActiveLoadCalls > 1); | 
|---|
|  | 1170 | break; | 
|---|
|  | 1171 | case KLDRSTATE_TERMINATING: | 
|---|
|  | 1172 | /* GC in progress. */ | 
|---|
|  | 1173 | KLDRDYLD_ASSERT(g_fActiveGC); | 
|---|
|  | 1174 | break; | 
|---|
|  | 1175 | case KLDRSTATE_PENDING_TERMINATION: | 
|---|
|  | 1176 | case KLDRSTATE_PENDING_INITIALIZATION: | 
|---|
|  | 1177 | case KLDRSTATE_PENDING_GC: | 
|---|
|  | 1178 | case KLDRSTATE_PENDING_DESTROY: | 
|---|
|  | 1179 | KLDRDYLD_ASSERT(rc); | 
|---|
|  | 1180 | break; | 
|---|
|  | 1181 | case KLDRSTATE_GOOD: | 
|---|
|  | 1182 | break; | 
|---|
|  | 1183 |  | 
|---|
|  | 1184 | /* | 
|---|
|  | 1185 | * Bad states. | 
|---|
|  | 1186 | */ | 
|---|
|  | 1187 | default: | 
|---|
|  | 1188 | KLDRDYLD_ASSERT(!"drop frame bad state (a)"); | 
|---|
|  | 1189 | break; | 
|---|
|  | 1190 | } | 
|---|
|  | 1191 | } | 
|---|
|  | 1192 |  | 
|---|
|  | 1193 |  | 
|---|
|  | 1194 | /** | 
|---|
| [2842] | 1195 | * Done with the stack frame, dereference all the modules in it. | 
|---|
| [2836] | 1196 | * | 
|---|
| [2842] | 1197 | * @param   iLoad1st        The start of the stack frame. | 
|---|
|  | 1198 | * @param   iLoadEnd        The end of the stack frame. | 
|---|
| [2837] | 1199 | * @param   rc              Used for state verification. | 
|---|
| [2836] | 1200 | */ | 
|---|
| [2842] | 1201 | static void kldrDyldStackDropFrame(uint32_t iLoad1st, uint32_t iLoadEnd, int rc) | 
|---|
| [2836] | 1202 | { | 
|---|
| [2869] | 1203 | uint32_t i; | 
|---|
|  | 1204 | KLDRDYLD_ASSERT(iLoad1st <= g_cStackMods); | 
|---|
|  | 1205 | KLDRDYLD_ASSERT(iLoadEnd == g_cStackMods); | 
|---|
| [2836] | 1206 |  | 
|---|
| [2837] | 1207 | /* | 
|---|
| [2842] | 1208 | * First pass: Do all the cleanups we can, but don't destroy anything just yet. | 
|---|
| [2837] | 1209 | */ | 
|---|
| [2869] | 1210 | i = iLoadEnd; | 
|---|
|  | 1211 | while (i-- > iLoad1st) | 
|---|
| [2837] | 1212 | { | 
|---|
| [2869] | 1213 | PKLDRDYLDMOD pMod = g_papStackMods[i]; | 
|---|
| [2845] | 1214 | kldrDyldStackCleanupOne(pMod, rc); | 
|---|
| [2837] | 1215 | } | 
|---|
|  | 1216 |  | 
|---|
|  | 1217 | /* | 
|---|
| [2842] | 1218 | * Second pass: Release the references so modules pending destruction | 
|---|
|  | 1219 | *              can be completely removed. | 
|---|
| [2837] | 1220 | */ | 
|---|
| [2869] | 1221 | for (i = iLoad1st; i < iLoadEnd ; i++) | 
|---|
| [2837] | 1222 | { | 
|---|
| [2869] | 1223 | PKLDRDYLDMOD pMod = g_papStackMods[i]; | 
|---|
| [2837] | 1224 |  | 
|---|
|  | 1225 | /* | 
|---|
| [2842] | 1226 | * Revalidate the module state. | 
|---|
| [2837] | 1227 | */ | 
|---|
|  | 1228 | switch (pMod->enmState) | 
|---|
|  | 1229 | { | 
|---|
|  | 1230 | case KLDRSTATE_INITIALIZING: | 
|---|
|  | 1231 | case KLDRSTATE_TERMINATING: | 
|---|
| [2842] | 1232 | case KLDRSTATE_PENDING_TERMINATION: | 
|---|
| [2837] | 1233 | case KLDRSTATE_PENDING_INITIALIZATION: | 
|---|
|  | 1234 | case KLDRSTATE_PENDING_GC: | 
|---|
|  | 1235 | case KLDRSTATE_PENDING_DESTROY: | 
|---|
|  | 1236 | case KLDRSTATE_GOOD: | 
|---|
|  | 1237 | break; | 
|---|
|  | 1238 | default: | 
|---|
|  | 1239 | KLDRDYLD_ASSERT(!"drop frame bad state (b)"); | 
|---|
|  | 1240 | break; | 
|---|
|  | 1241 | } | 
|---|
|  | 1242 |  | 
|---|
|  | 1243 | /* | 
|---|
|  | 1244 | * Release it. | 
|---|
|  | 1245 | */ | 
|---|
|  | 1246 | kldrDyldModDeref(pMod); | 
|---|
|  | 1247 | } | 
|---|
| [2869] | 1248 |  | 
|---|
|  | 1249 | /* | 
|---|
|  | 1250 | * Drop the stack frame. | 
|---|
|  | 1251 | */ | 
|---|
|  | 1252 | g_cStackMods = iLoad1st; | 
|---|
| [2836] | 1253 | } | 
|---|
|  | 1254 |  | 
|---|
|  | 1255 |  | 
|---|
|  | 1256 | /** | 
|---|
| [2842] | 1257 | * Do garbage collection. | 
|---|
|  | 1258 | * | 
|---|
|  | 1259 | * This isn't doing anything unless it's called from the last | 
|---|
|  | 1260 | * load or unload call. | 
|---|
|  | 1261 | */ | 
|---|
|  | 1262 | static void kldrDyldDoModuleTerminationAndGarabageCollection(void) | 
|---|
|  | 1263 | { | 
|---|
|  | 1264 | PKLDRDYLDMOD pMod; | 
|---|
|  | 1265 |  | 
|---|
|  | 1266 | /* | 
|---|
|  | 1267 | * We don't do anything until we're got rid of all recursive calls. | 
|---|
|  | 1268 | * This will ensure that we get the most optimal termination order and | 
|---|
|  | 1269 | * that we don't unload anything too early. | 
|---|
|  | 1270 | */ | 
|---|
|  | 1271 | if (g_cActiveLoadCalls || g_cActiveUnloadCalls || g_fActiveGC) | 
|---|
|  | 1272 | return; | 
|---|
|  | 1273 | g_fActiveGC = 1; | 
|---|
|  | 1274 |  | 
|---|
|  | 1275 | do | 
|---|
|  | 1276 | { | 
|---|
|  | 1277 | /* | 
|---|
|  | 1278 | * 1. Release prerequisites for any left over modules. | 
|---|
|  | 1279 | */ | 
|---|
| [2866] | 1280 | for (pMod = kLdrDyldHead; pMod; pMod = pMod->Load.pNext) | 
|---|
| [2842] | 1281 | { | 
|---|
|  | 1282 | kldrDyldModAddRef(pMod); | 
|---|
|  | 1283 |  | 
|---|
|  | 1284 | switch (pMod->enmState) | 
|---|
|  | 1285 | { | 
|---|
|  | 1286 | case KLDRSTATE_GOOD: | 
|---|
|  | 1287 | case KLDRSTATE_PENDING_GC: | 
|---|
| [2869] | 1288 | case KLDRSTATE_PENDING_TERMINATION: | 
|---|
| [2842] | 1289 | break; | 
|---|
|  | 1290 |  | 
|---|
|  | 1291 | case KLDRSTATE_INITIALIZATION_FAILED: /* just in case */ | 
|---|
|  | 1292 | case KLDRSTATE_PENDING_INITIALIZATION: | 
|---|
|  | 1293 | kldrDyldModUnloadPrerequisites(pMod); | 
|---|
|  | 1294 | break; | 
|---|
|  | 1295 |  | 
|---|
|  | 1296 | default: | 
|---|
|  | 1297 | KLDRDYLD_ASSERT(!"invalid GC state (a)"); | 
|---|
|  | 1298 | break; | 
|---|
|  | 1299 | } | 
|---|
|  | 1300 |  | 
|---|
|  | 1301 | kldrDyldModDeref(pMod); | 
|---|
|  | 1302 | } | 
|---|
|  | 1303 |  | 
|---|
|  | 1304 | /* | 
|---|
|  | 1305 | * 2. Do init calls until we encounter somebody calling load/unload. | 
|---|
|  | 1306 | */ | 
|---|
|  | 1307 | for (pMod = g_pkLdrDyldTermHead; pMod; pMod = pMod->InitTerm.pNext) | 
|---|
|  | 1308 | { | 
|---|
|  | 1309 | int fRestart = 0; | 
|---|
|  | 1310 | kldrDyldModAddRef(pMod); | 
|---|
|  | 1311 |  | 
|---|
|  | 1312 | switch (pMod->enmState) | 
|---|
|  | 1313 | { | 
|---|
|  | 1314 | case KLDRSTATE_GOOD: | 
|---|
|  | 1315 | case KLDRSTATE_PENDING_GC: | 
|---|
|  | 1316 | break; | 
|---|
|  | 1317 |  | 
|---|
|  | 1318 | case KLDRSTATE_PENDING_TERMINATION: | 
|---|
|  | 1319 | { | 
|---|
|  | 1320 | const uint32_t cTotalLoadCalls = g_cTotalLoadCalls; | 
|---|
|  | 1321 | const uint32_t cTotalUnloadCalls = g_cTotalUnloadCalls; | 
|---|
|  | 1322 | kldrDyldModCallTerm(pMod); | 
|---|
|  | 1323 | fRestart = cTotalLoadCalls != g_cTotalLoadCalls | 
|---|
|  | 1324 | || cTotalUnloadCalls != g_cTotalUnloadCalls; | 
|---|
|  | 1325 | break; | 
|---|
|  | 1326 | } | 
|---|
|  | 1327 |  | 
|---|
|  | 1328 | default: | 
|---|
|  | 1329 | KLDRDYLD_ASSERT(!"invalid GC state (b)"); | 
|---|
|  | 1330 | break; | 
|---|
|  | 1331 | } | 
|---|
|  | 1332 |  | 
|---|
|  | 1333 | kldrDyldModDeref(pMod); | 
|---|
|  | 1334 | if (fRestart) | 
|---|
|  | 1335 | break; | 
|---|
|  | 1336 | } | 
|---|
|  | 1337 | } while (pMod); | 
|---|
|  | 1338 |  | 
|---|
|  | 1339 | /* | 
|---|
|  | 1340 | * Unmap and destroy modules pending for GC. | 
|---|
|  | 1341 | */ | 
|---|
|  | 1342 | pMod = kLdrDyldHead; | 
|---|
|  | 1343 | while (pMod) | 
|---|
|  | 1344 | { | 
|---|
|  | 1345 | PKLDRDYLDMOD pNext = pMod->Load.pNext; | 
|---|
| [2845] | 1346 | kldrDyldModAddRef(pMod); | 
|---|
|  | 1347 |  | 
|---|
| [2842] | 1348 | switch (pMod->enmState) | 
|---|
|  | 1349 | { | 
|---|
|  | 1350 | case KLDRSTATE_INITIALIZATION_FAILED: | 
|---|
|  | 1351 | case KLDRSTATE_PENDING_GC: | 
|---|
| [2869] | 1352 | KLDRDYLD_ASSERT(!pMod->cDepRefs); | 
|---|
|  | 1353 | KLDRDYLD_ASSERT(!pMod->cDynRefs); | 
|---|
| [2842] | 1354 | pMod->enmState = KLDRSTATE_GC; | 
|---|
|  | 1355 | kldrDyldModUnmap(pMod); | 
|---|
| [2845] | 1356 | KLDRDYLD_ASSERT(pMod->enmState == KLDRSTATE_PENDING_DESTROY); | 
|---|
| [2842] | 1357 | kldrDyldModDestroy(pMod); | 
|---|
| [2845] | 1358 | KLDRDYLD_ASSERT(pMod->enmState == KLDRSTATE_DESTROYED); | 
|---|
| [2842] | 1359 | break; | 
|---|
|  | 1360 |  | 
|---|
|  | 1361 | case KLDRSTATE_GOOD: | 
|---|
|  | 1362 | break; | 
|---|
|  | 1363 | default: | 
|---|
|  | 1364 | KLDRDYLD_ASSERT(!"invalid GC state (c)"); | 
|---|
|  | 1365 | break; | 
|---|
|  | 1366 | } | 
|---|
|  | 1367 |  | 
|---|
| [2845] | 1368 | kldrDyldModDeref(pMod); | 
|---|
|  | 1369 |  | 
|---|
| [2842] | 1370 | /* next */ | 
|---|
|  | 1371 | pMod = pNext; | 
|---|
|  | 1372 | } | 
|---|
|  | 1373 |  | 
|---|
|  | 1374 | g_fActiveGC = 0; | 
|---|
|  | 1375 | } | 
|---|
|  | 1376 |  | 
|---|
|  | 1377 |  | 
|---|
|  | 1378 | /** | 
|---|
|  | 1379 | * Worker for kLdrDyldUnload(). | 
|---|
|  | 1380 | * @internal | 
|---|
|  | 1381 | */ | 
|---|
|  | 1382 | static int kldrDyldDoUnload(PKLDRDYLDMOD pMod) | 
|---|
|  | 1383 | { | 
|---|
|  | 1384 | return kldrDyldModDynamicUnload(pMod); | 
|---|
|  | 1385 | } | 
|---|
|  | 1386 |  | 
|---|
|  | 1387 |  | 
|---|
|  | 1388 | /** | 
|---|
|  | 1389 | * Worker for kLdrDyldFindByName(). | 
|---|
|  | 1390 | * @internal | 
|---|
|  | 1391 | */ | 
|---|
| [2843] | 1392 | static int kldrDyldDoFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch, | 
|---|
| [2842] | 1393 | unsigned fFlags, PPKLDRDYLDMOD ppMod) | 
|---|
|  | 1394 | { | 
|---|
| [2843] | 1395 | return kldrDyldFindExistingModule(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, ppMod); | 
|---|
| [2842] | 1396 | } | 
|---|
|  | 1397 |  | 
|---|
|  | 1398 |  | 
|---|
|  | 1399 | /** | 
|---|
|  | 1400 | * Worker for kLdrDyldFindByAddress(). | 
|---|
|  | 1401 | * @internal | 
|---|
|  | 1402 | */ | 
|---|
|  | 1403 | static int kldrDyldDoFindByAddress(uintptr_t Address, PPKLDRDYLDMOD ppMod, uint32_t *piSegment, uintptr_t *poffSegment) | 
|---|
|  | 1404 | { | 
|---|
|  | 1405 | /* Scan the segments of each module in the load list. */ | 
|---|
|  | 1406 | PKLDRDYLDMOD pMod = kLdrDyldHead; | 
|---|
|  | 1407 | while (pMod) | 
|---|
|  | 1408 | { | 
|---|
|  | 1409 | uint32_t iSeg; | 
|---|
|  | 1410 | for (iSeg = 0; iSeg < pMod->pMod->cSegments; iSeg++) | 
|---|
|  | 1411 | { | 
|---|
| [2848] | 1412 | KLDRADDR off = (KLDRADDR)Address - pMod->pMod->aSegments[iSeg].MapAddress; | 
|---|
| [2842] | 1413 | if (off < pMod->pMod->aSegments[iSeg].cb) | 
|---|
|  | 1414 | { | 
|---|
|  | 1415 | *ppMod = pMod->hMod; | 
|---|
|  | 1416 | if (piSegment) | 
|---|
|  | 1417 | *piSegment = iSeg; | 
|---|
|  | 1418 | if (poffSegment) | 
|---|
|  | 1419 | *poffSegment = (uintptr_t)off; | 
|---|
|  | 1420 | return 0; | 
|---|
|  | 1421 | } | 
|---|
|  | 1422 | } | 
|---|
|  | 1423 |  | 
|---|
|  | 1424 | /* next */ | 
|---|
|  | 1425 | pMod = pMod->Load.pNext; | 
|---|
|  | 1426 | } | 
|---|
|  | 1427 |  | 
|---|
|  | 1428 | return KLDR_ERR_MODULE_NOT_FOUND; | 
|---|
|  | 1429 | } | 
|---|
|  | 1430 |  | 
|---|
|  | 1431 |  | 
|---|
|  | 1432 | /** | 
|---|
|  | 1433 | * Worker for kLdrDyldGetName(). | 
|---|
|  | 1434 | * @internal | 
|---|
|  | 1435 | */ | 
|---|
|  | 1436 | static int kldrDyldDoGetName(PKLDRDYLDMOD pMod, char *pszName, size_t cchName) | 
|---|
|  | 1437 | { | 
|---|
|  | 1438 | return kldrDyldModGetName(pMod, pszName, cchName); | 
|---|
|  | 1439 | } | 
|---|
|  | 1440 |  | 
|---|
|  | 1441 |  | 
|---|
|  | 1442 | /** | 
|---|
|  | 1443 | * Worker for kLdrDyldGetFilename(). | 
|---|
|  | 1444 | * @internal | 
|---|
|  | 1445 | */ | 
|---|
|  | 1446 | static int kldrDyldDoGetFilename(PKLDRDYLDMOD pMod, char *pszFilename, size_t cchFilename) | 
|---|
|  | 1447 | { | 
|---|
|  | 1448 | return kldrDyldModGetFilename(pMod, pszFilename, cchFilename); | 
|---|
|  | 1449 | } | 
|---|
|  | 1450 |  | 
|---|
|  | 1451 |  | 
|---|
|  | 1452 | /** | 
|---|
|  | 1453 | * Worker for kLdrDyldQuerySymbol(). | 
|---|
|  | 1454 | * @internal | 
|---|
|  | 1455 | */ | 
|---|
|  | 1456 | static int kldrDyldDoQuerySymbol(PKLDRDYLDMOD pMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, uintptr_t *pValue, uint32_t *pfKind) | 
|---|
|  | 1457 | { | 
|---|
|  | 1458 | return kldrDyldModQuerySymbol(pMod, uSymbolOrdinal, pszSymbolName, pValue, pfKind); | 
|---|
|  | 1459 | } | 
|---|
|  | 1460 |  | 
|---|
|  | 1461 |  | 
|---|
|  | 1462 | /** | 
|---|
| [2825] | 1463 | * Panic / failure | 
|---|
|  | 1464 | * | 
|---|
|  | 1465 | * @returns rc if we're in a position where we can return. | 
|---|
|  | 1466 | * @param   rc              Return code. | 
|---|
|  | 1467 | * @param   pszFormat       Message string. Limited fprintf like formatted. | 
|---|
|  | 1468 | * @param   ...             Message string arguments. | 
|---|
|  | 1469 | */ | 
|---|
| [2843] | 1470 | int kldrDyldFailure(int rc, const char *pszFilename, ...) | 
|---|
| [2825] | 1471 | { | 
|---|
| [2869] | 1472 | /** @todo print it. */ | 
|---|
|  | 1473 | if (g_fBootstrapping); | 
|---|
|  | 1474 | kldrHlpExit(1); | 
|---|
| [2825] | 1475 | return rc; | 
|---|
|  | 1476 | } | 
|---|
|  | 1477 |  | 
|---|
|  | 1478 |  | 
|---|
| [2836] | 1479 | /** | 
|---|
|  | 1480 | * Copies the error string to the user buffer. | 
|---|
|  | 1481 | * | 
|---|
|  | 1482 | * @returns rc. | 
|---|
|  | 1483 | * @param   rc      The status code. | 
|---|
|  | 1484 | * @param   pszErr  Where to copy the error string to. | 
|---|
|  | 1485 | * @param   cchErr  The size of the destination buffer. | 
|---|
|  | 1486 | */ | 
|---|
|  | 1487 | static int kldrDyldCopyError(int rc, char *pszErr, size_t cchErr) | 
|---|
|  | 1488 | { | 
|---|
|  | 1489 | size_t cchToCopy; | 
|---|
|  | 1490 |  | 
|---|
|  | 1491 | /* if no error string, format the rc into a string. */ | 
|---|
|  | 1492 | if (!g_szkLdrDyldError[0] && rc) | 
|---|
|  | 1493 | kldrHlpInt2Ascii(g_szkLdrDyldError, sizeof(g_szkLdrDyldError), rc, 10); | 
|---|
|  | 1494 |  | 
|---|
|  | 1495 | /* copy it if we got something. */ | 
|---|
|  | 1496 | if (cchErr && pszErr && g_szkLdrDyldError[0]) | 
|---|
|  | 1497 | { | 
|---|
|  | 1498 | cchToCopy = kLdrHlpStrLen(g_szkLdrDyldError); | 
|---|
|  | 1499 | if (cchToCopy >= cchErr) | 
|---|
|  | 1500 | cchToCopy = cchErr - 1; | 
|---|
|  | 1501 | kLdrHlpMemCopy(pszErr, g_szkLdrDyldError, cchToCopy); | 
|---|
|  | 1502 | pszErr[cchToCopy] = '\0'; | 
|---|
|  | 1503 | } | 
|---|
|  | 1504 |  | 
|---|
|  | 1505 | return rc; | 
|---|
|  | 1506 | } | 
|---|
|  | 1507 |  | 
|---|