Index: /trunk/kLdr/kLdr.h
===================================================================
--- /trunk/kLdr/kLdr.h	(revision 2842)
+++ /trunk/kLdr/kLdr.h	(revision 2843)
@@ -545,6 +545,4 @@
 /** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
 #define KLDRMOD_ENUM_SYMBOL_FLAGS_ALL       0x00000001
-/** Do a recursive initialization calls instead of defering them to the outermost call. */
-#define KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT  0x00000002
 /** @} */
 
@@ -628,12 +626,16 @@
  * typical used when loading import/dep modules. */
 #define KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE     0x00000002
-/** @todo more to come. */
-/** @} */
-
-
-int     kLdrDyldLoad(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
+/** Do a recursive initialization calls instead of defering them to the outermost call. */
+#define KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT      0x00000004
+/** We're loading the executable module.
+ * Internal flag which will be rejected by kLdrDyldLoad. */
+#define KLDRDYLD_LOAD_FLAGS_EXECUTABLE          0x40000000
+/** @} */
+
+
+int     kLdrDyldLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
                      unsigned fFlags, PHKLDRMOD phMod, char *pszErr, size_t cchErr);
 int     kLdrDyldUnload(HKLDRMOD hMod);
-int     kLdrDyldFindByName(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
+int     kLdrDyldFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
                            unsigned fFlags, PHKLDRMOD phMod);
 int     kLdrDyldFindByAddress(uintptr_t Address, PHKLDRMOD phMod, uint32_t *piSegment, uintptr_t *poffSegment);
@@ -691,8 +693,14 @@
     /** Flags. (Currently unused, MBZ.) */
     uint32_t        fFlags;
+    /** The search method to use when loading this executable. */
+    KLDRDYLDSEARCH  enmSearch;
     /** The executable file that the stub is supposed to load. */
     char            szExecutable[260];
+    /** The default prefix used when searching for DLLs. */
+    char            szDefPrefix[16];
+    /** The default suffix used when searching for DLLs. */
+    char            szDefSuffix[16];
     /** The LD_LIBRARY_PATH prefix for the process.. */
-    char            szLibPath[4096 - 260 - sizeof(uint32_t)];
+    char            szLibPath[4096 - sizeof(uint32_t) - sizeof(KLDRDYLDSEARCH) - 16 - 16 - 260];
 } KLDREXEARGS, *PKLDREXEARGS;
 
Index: /trunk/kLdr/kLdrDyld.c
===================================================================
--- /trunk/kLdr/kLdrDyld.c	(revision 2842)
+++ /trunk/kLdr/kLdrDyld.c	(revision 2843)
@@ -59,4 +59,7 @@
 *   Global Variables                                                           *
 *******************************************************************************/
+/** Pointer to the executable module.
+ * (This is exported, so no prefix.) */
+PKLDRDYLDMOD    kLdrDyldExe = NULL;
 /** Pointer to the head module (the executable).
  * (This is exported, so no prefix.) */
@@ -84,22 +87,30 @@
 PKLDRDYLDMOD    g_pkLdrDyldBindTail;
 
-/** Stack of modules involved in the active loads.
- *
- * The main purpose is to allow module init routines to loading and unloading
- * modules without upsetting the init order and to assist in dereferencing
- * modules on load failure.
- *
- * Each call to kLdrDyldLoad and kLdrDyldLoadExe will start a load frame
- * when doing a fresh load. All dependant modules will be pushed on each
- * reference. The frame is completed when all the dependant modules has
- * been resolved (or a failure occurs). After doing fixups, the frame is
- * used to do module initialization. Should an error occur during the load
- * the frame will be used to dereference all the modules involved in the
- * load operation (it will not however, be used for termination calls as
- * they are postponed till the last load operation completes).
- *
- * Should any of the init calls load a module, a new frame will be created
- * for that operation and processed before the init call returns to the
- * previous frame.
+/** Flag indicating bootstrap time.
+ * When set the error behaviour changes. Any kind of serious failure
+ * is fatal and will terminate the process. */
+int             g_fBootstrapping;
+/** The global error buffer. */
+char            g_szkLdrDyldError[1024];
+
+/** The Library search path. */
+char            kLdrDyldLibraryPath[4096];
+
+/** The default flags. */
+uint32_t        kLdrDyldFlags = 0;
+/** The default search method. */
+KLDRDYLDSEARCH  kLdrDyldSearch = KLDRDYLD_SEARCH_INVALID;
+/** The default DLL prefix. */
+char            kLdrDyldDefPrefix[16];
+/** The default DLL suffix. */
+char            kLdrDyldDefSuffix[16];
+
+
+/** The load stack.
+ * This contains frames with modules affected by active loads.
+ *
+ * Each kLdrDyldLoad and kLdrDyldLoadExe call will create a new stack frame containing
+ * all the modules involved in the operation. The modules will be ordered in recursive
+ * init order within the frame.
  */
 static PPKLDRDYLDMOD    g_papStackMods;
@@ -119,12 +130,4 @@
 static uint32_t         g_fActiveGC;
 
-/** The global error buffer. */
-char            g_szkLdrDyldError[1024];
-
-/** The Library search path. */
-char            kLdrDyldLibraryPath[4096];
-/** The executable flags. */
-uint32_t        kLdrDyldFlags;
-
 
 
@@ -135,10 +138,14 @@
  * @internal
  * @{ */
-static int kldrDyldDoLoad(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
+void       kldrDyldDoLoadExeStackSwitch(PKLDRDYLDMOD pExe, void *pvStack, size_t cbStack);
+void       kldrDyldDoLoadExe(PKLDRDYLDMOD pExe);
+static int kldrDyldDoLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
                           unsigned fFlags, PPKLDRDYLDMOD ppMod, char *pszErr, size_t cchErr);
-static int kldrDyldDoLoadPrerequisites(PKLDRDYLDMOD pMod, const char *pszDefPrefix, const char *pszDefSuffix,
+static int kldrDyldDoLoad2(PKLDRDYLDMOD pLoadedMod, const char *pszPrefix, const char *pszSuffix,
+                           KLDRDYLDSEARCH enmSearch, unsigned fFlags);
+static int kldrDyldDoLoadPrerequisites(PKLDRDYLDMOD pMod, const char *pszPrefix, const char *pszSuffix,
                                        KLDRDYLDSEARCH enmSearch, unsigned fFlags);
 static int kldrDyldDoUnload(PKLDRDYLDMOD pMod);
-static int kldrDyldDoFindByName(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
+static int kldrDyldDoFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
                               unsigned fFlags, PPKLDRDYLDMOD ppMod);
 static int kldrDyldDoFindByAddress(uintptr_t Address, PPKLDRDYLDMOD ppMod, uint32_t *piSegment, uintptr_t *poffSegment);
@@ -192,10 +199,80 @@
 
 /**
+ * Bootstrap an executable.
+ *
+ * This is called from the executable stub to replace the stub and run the
+ * executable specified in the argument package.
+ *
+ * Since this is boostrap time there isn't anything to return to. So, instead
+ * the process will be terminated upon failure.
+ *
+ * We also have to keep in mind that this function is called on a small, small,
+ * stack and therefore any kind of large stack objects or deep recursions must
+ * be avoided. Since loading the executable will involve more or less all
+ * operations in the loader, this restriction really applies everywhere.
+ *
+ * @param   pArgs       Pointer to the argument package residing in the executable stub.
+ */
+void kldrDoDyldLoadExe(PKLDREXEARGS pArgs)
+{
+    void *pvStack = NULL;
+    size_t cbStack = 0;
+    PKLDRDYLDMOD pExe = NULL;
+    int rc;
+
+    /*
+     * Copy the arguments into the globals and do loader init.
+     */
+    kLdrDyldFlags = pArgs->fFlags;
+    kLdrDyldSearch = pArgs->enmSearch;
+    kLdrHlpMemCopy(kLdrDyldDefPrefix, pArgs->szDefPrefix, KLDR_MIN(sizeof(pArgs->szDefPrefix), sizeof(kLdrDyldDefPrefix)));
+    kLdrHlpMemCopy(kLdrDyldDefSuffix, pArgs->szDefSuffix, KLDR_MIN(sizeof(pArgs->szDefSuffix), sizeof(kLdrDyldDefSuffix)));
+    kLdrHlpMemCopy(kLdrDyldLibraryPath, pArgs->szLibPath, KLDR_MIN(sizeof(pArgs->szLibPath), sizeof(kLdrDyldLibraryPath)));
+
+    g_fBootstrapping = 1;
+    rc = kldrInit();
+    if (rc)
+        kldrDyldFailure(rc, "Init failure, rc=%d", rc);
+
+    /*
+     * Make sure we own the loader semaphore (necessary for init).
+     */
+    rc = kldrHlpSemRequest();
+    if (rc)
+        kldrDyldFailure(rc, "Sem req. failure, rc=%d", rc);
+
+    /*
+     * Open and map the executable module before we join paths with kLdrDyldLoad().
+     */
+    rc = kldrDyldFindNewModule(pArgs->szExecutable, NULL, NULL, kLdrDyldSearch,
+                               kLdrDyldFlags | KLDRDYLD_LOAD_FLAGS_EXECUTABLE, &pExe);
+    if (rc)
+        kldrDyldFailure(rc, "Can't find/open the executable '%s', rc=%d", pArgs->szExecutable, rc);
+    rc = kldrDyldModMap(pExe);
+    if (rc)
+        kldrDyldFailure(rc, "Failed to map the executable '%s', rc=%d", pExe->pMod->pszFilename, rc);
+
+    kLdrDyldExe = pExe;
+
+    /*
+     * Query the stack information and go to OS specific code to
+     * setup and switch stack. The OS specific code will call us
+     * back at kldrDyldDoLoadExe.
+     */
+    rc = kldrDyldModGetStackInfo(pExe, &pvStack, &cbStack);
+    if (rc)
+        kldrDyldFailure(rc, "Failed to map the executable '%s', rc=%d", pExe->pMod->pszFilename, rc);
+    kldrDyldDoLoadExeStackSwitch(pExe, pvStack, cbStack);
+    kldrDyldFailure(-1, "Failed to setup the stack for '%s'.", pExe->pMod->pszFilename);
+}
+
+
+/**
  * Loads a module into the current process.
  *
  * @returns 0 on success, non-zero native OS status code or kLdr status code on failure.
  * @param   pszDll          The name of the dll to open.
- * @param   pszDefPrefix    Prefix to use when searching.
- * @param   pszDefSuffix    Suffix to use when searching.
+ * @param   pszPrefix       Prefix to use when searching.
+ * @param   pszSuffix       Suffix to use when searching.
  * @param   enmSearch       Method to use when locating the module and any modules it may depend on.
  * @param   fFlags          Flags, a combintation of the KLDRYDLD_LOAD_FLAGS_* \#defines.
@@ -204,5 +281,5 @@
  * @param   cchErr          The size of the buffer pointed to by pszErr.
  */
-int     kLdrDyldLoad(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
+int     kLdrDyldLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
                      unsigned fFlags, PHKLDRMOD phMod, char *pszErr, size_t cchErr)
 {
@@ -214,6 +291,6 @@
     *phMod = NIL_HKLDRMOD;
     KLDRHLP_VALIDATE_STRING(pszDll);
-    KLDRHLP_VALIDATE_OPTIONAL_STRING(pszDefPrefix);
-    KLDRHLP_VALIDATE_OPTIONAL_STRING(pszDefSuffix);
+    KLDRHLP_VALIDATE_OPTIONAL_STRING(pszPrefix);
+    KLDRHLP_VALIDATE_OPTIONAL_STRING(pszSuffix);
     KLDRHLP_VALIDATE_ENUM(enmSearch, KLDRDYLD_SEARCH);
     KLDRHLP_VALIDATE_OPTIONAL_BUFFER(pszErr, cchErr);
@@ -226,5 +303,5 @@
         g_cTotalLoadCalls++;
         g_cActiveLoadCalls++;
-        rc = kldrDyldDoLoad(pszDll, pszDefPrefix, pszDefSuffix, enmSearch, fFlags, phMod, pszErr, cchErr);
+        rc = kldrDyldDoLoad(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, phMod, pszErr, cchErr);
         g_cActiveLoadCalls--;
         kldrDyldDoModuleTerminationAndGarabageCollection();
@@ -273,11 +350,11 @@
  * @returns KLDR_ERR_MODULE_NOT_FOUND or some I/O error on failure.
  * @param   pszDll          The name of the dll to look for.
- * @param   pszDefPrefix    Prefix than can be used when searching.
- * @param   pszDefSuffix    Suffix than can be used when searching.
+ * @param   pszPrefix       Prefix than can be used when searching.
+ * @param   pszSuffix       Suffix than can be used when searching.
  * @param   enmSearch       Method to use when locating the module.
  * @param   fFlags          Flags, a combintation of the KLDRYDLD_LOAD_FLAGS_* \#defines.
  * @param   phMod           Where to store the handle of the module on success.
  */
-int     kLdrDyldFindByName(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
+int     kLdrDyldFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
                            unsigned fFlags, PHKLDRMOD phMod)
 {
@@ -293,5 +370,5 @@
     {
         PKLDRDYLDMOD pMod = NULL;
-        rc = kldrDyldDoFindByName(pszDll, pszDefPrefix, pszDefSuffix, enmSearch, fFlags, phMod);
+        rc = kldrDyldDoFindByName(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, phMod);
         kldrHlpSemRelease();
         *phMod = pMod;
@@ -436,4 +513,32 @@
 
 
+/**
+ * Worker kLdrDoLoadExe().
+ * Used after we've switch to the final process stack.
+ *
+ * @param   pExe    The executable module.
+ * @internal
+ */
+void kldrDyldDoLoadExe(PKLDRDYLDMOD pExe)
+{
+    int rc;
+
+    /*
+     * Load the executable module with its prerequisites and initialize them.
+     */
+    g_cActiveLoadCalls++;
+    rc = kldrDyldDoLoad2(pExe, NULL, NULL, kLdrDyldSearch, kLdrDyldFlags | KLDRDYLD_LOAD_FLAGS_EXECUTABLE);
+    if (rc)
+        kldrDyldFailure(rc, "load 2 failed for '%s', rc=%d", pExe->pMod->pszFilename);
+    g_cActiveLoadCalls--;
+    kldrDyldDoModuleTerminationAndGarabageCollection();
+
+    /*
+     * Invoke the executable entry point.
+     */
+    kldrDyldModStartExe(pExe);
+    kldrDyldFailure(-1, "failed to invoke main!");
+}
+
 
 /**
@@ -441,5 +546,5 @@
  * @internal
  */
-static int kldrDyldDoLoad(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
+static int kldrDyldDoLoad(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
                           unsigned fFlags, PPKLDRDYLDMOD ppMod, char *pszErr, size_t cchErr)
 {
@@ -449,5 +554,5 @@
      * Try find the module among the ones that's already loaded.
      */
-    rc = kldrDyldFindExistingModule(pszDll, pszDefPrefix, pszDefSuffix, enmSearch, fFlags, ppMod);
+    rc = kldrDyldFindExistingModule(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, ppMod);
     if (!rc)
     {
@@ -510,5 +615,5 @@
          * We'll have to load it from file.
          */
-        rc = kldrDyldFindNewModule(pszDll, pszDefPrefix, pszDefSuffix, enmSearch, fFlags, ppMod);
+        rc = kldrDyldFindNewModule(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, ppMod);
         if (rc)
             return kldrDyldCopyError(rc, pszErr, cchErr);
@@ -516,99 +621,111 @@
     }
 
+    /*
+     * Join cause with kLdrDyldLoadExe.
+     */
     if (!rc)
-    {
-        /*
-         * Load prerequisites.
-         */
-        uint32_t i;
-        uint32_t iLoadEnd;
-        uint32_t iLoad1st = kldrDyldStackNewFrame(*ppMod);
-        rc = kldrDyldDoLoadPrerequisites(*ppMod, pszDefPrefix, pszDefSuffix, enmSearch, fFlags);
-        iLoadEnd = kldrDyldStackFrameCompleted();
-
-        /*
-         * Apply fixups.
-         */
+        rc = kldrDyldDoLoad2(*ppMod, pszPrefix, pszSuffix, enmSearch, fFlags);
+    else
+    {
+        /** @todo this is probably not quite right. */
+        /* If the reference count is 0 do a quick ref/deref to trigger destruction. */
+        kldrDyldModAddRef(*ppMod);
+        kldrDyldModDeref(*ppMod);
+    }
+
+    /*
+     * Copy any error or warning to the error buffer.
+     */
+    return kldrDyldCopyError(rc, pszErr, cchErr);
+}
+
+
+/**
+ * 2nd half of kLdrDyldLoad() and kLdrDyldLoadExe().
+ *
+ * @internal
+ */
+static int kldrDyldDoLoad2(PKLDRDYLDMOD pLoadedMod, const char *pszPrefix, const char *pszSuffix,
+                           KLDRDYLDSEARCH enmSearch, unsigned fFlags)
+{
+    /*
+     * Load prerequisites.
+     */
+    uint32_t i;
+    uint32_t iLoad1st = kldrDyldStackNewFrame(pLoadedMod);
+    int rc = kldrDyldDoLoadPrerequisites(pLoadedMod, pszPrefix, pszSuffix, enmSearch, fFlags);
+    uint32_t iLoadEnd = kldrDyldStackFrameCompleted();
+
+    /*
+     * Apply fixups.
+     */
+    for (i = iLoad1st; !rc && i < iLoadEnd; i++)
+    {
+        PKLDRDYLDMOD pMod = g_papStackMods[i];
+        if (    pMod->enmState == KLDRSTATE_LOADED_PREREQUISITES
+            ||  pMod->enmState == KLDRSTATE_RELOADED_LOADED_PREREQUISITES)
+            rc = kldrDyldModFixup(pMod);
+    }
+
+    /*
+     * Advance fixed up module onto initialization.
+     */
+    for (i = iLoad1st; !rc && i < iLoadEnd; i++)
+    {
+        PKLDRDYLDMOD pMod = g_papStackMods[i];
+        if (    pMod->enmState == KLDRSTATE_FIXED_UP
+            ||  pMod->enmState == KLDRSTATE_RELOADED_FIXED_UP)
+            pMod->enmState = KLDRSTATE_PENDING_INITIALIZATION;
+        KLDRDYLD_ASSERT(    pMod->enmState == KLDRSTATE_PENDING_INITIALIZATION
+                        ||  pMod->enmState == KLDRSTATE_GOOD);
+    }
+
+    /*
+     * Call the initializers if we're loading in recursive mode or
+     * if we're the outermost load call.
+     */
+    if (fFlags & KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT)
+    {
         for (i = iLoad1st; !rc && i < iLoadEnd; i++)
         {
             PKLDRDYLDMOD pMod = g_papStackMods[i];
-            if (    pMod->enmState == KLDRSTATE_LOADED_PREREQUISITES
-                ||  pMod->enmState == KLDRSTATE_RELOADED_LOADED_PREREQUISITES)
-                rc = kldrDyldModFixup(pMod);
+            if (pMod->enmState == KLDRSTATE_PENDING_INITIALIZATION)
+                rc = kldrDyldModCallInit(pMod);
+            else if (pMod->enmState == KLDRSTATE_INITIALIZATION_FAILED)
+                rc = KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED_ALREADY;
+            else
+                KLDRDYLD_ASSERT(g_papStackMods[i]->enmState == KLDRSTATE_GOOD);
         }
-
-        /*
-         * Advance fixed up module onto initialization.
-         */
+#ifdef KLDRDYLD_STRICT
         for (i = iLoad1st; !rc && i < iLoadEnd; i++)
+            KLDRDYLD_ASSERT(g_papStackMods[i]->enmState == KLDRSTATE_GOOD);
+#endif
+    }
+    else if (g_cActiveLoadCalls <= 1)
+    {
+        while (!rc && g_pkLdrDyldInitHead)
         {
-            PKLDRDYLDMOD pMod = g_papStackMods[i];
-            if (    pMod->enmState == KLDRSTATE_FIXED_UP
-                ||  pMod->enmState == KLDRSTATE_RELOADED_FIXED_UP)
-                pMod->enmState = KLDRSTATE_PENDING_INITIALIZATION;
-            KLDRDYLD_ASSERT(    pMod->enmState == KLDRSTATE_PENDING_INITIALIZATION
-                            ||  pMod->enmState == KLDRSTATE_GOOD);
+            PKLDRDYLDMOD pMod = g_pkLdrDyldInitHead;
+            g_pkLdrDyldInitHead = pMod->InitTerm.pNext;
+            if (pMod->InitTerm.pNext)
+                pMod->InitTerm.pNext->InitTerm.pPrev = NULL;
+            rc = kldrDyldModCallInit(pMod);
         }
-
-        /*
-         * Call the initializers if we're loading in recursive mode or
-         * if we're the outermost load call.
-         */
-        if (fFlags & KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT)
-        {
-            for (i = iLoad1st; !rc && i < iLoadEnd; i++)
-            {
-                PKLDRDYLDMOD pMod = g_papStackMods[i];
-                if (pMod->enmState == KLDRSTATE_PENDING_INITIALIZATION)
-                    rc = kldrDyldModCallInit(pMod);
-                else if (pMod->enmState == KLDRSTATE_INITIALIZATION_FAILED)
-                    rc = KLDR_ERR_PREREQUISITE_MODULE_INIT_FAILED_ALREADY;
-                else
-                    KLDRDYLD_ASSERT(g_papStackMods[i]->enmState == KLDRSTATE_GOOD);
-            }
-#ifdef KLDRDYLD_STRICT
-            for (i = iLoad1st; !rc && i < iLoadEnd; i++)
-                KLDRDYLD_ASSERT(g_papStackMods[i]->enmState == KLDRSTATE_GOOD);
-#endif
-        }
-        else if (g_cActiveLoadCalls <= 1)
-        {
-            while (!rc && g_pkLdrDyldInitHead)
-            {
-                PKLDRDYLDMOD pMod = g_pkLdrDyldInitHead;
-                g_pkLdrDyldInitHead = pMod->InitTerm.pNext;
-                if (pMod->InitTerm.pNext)
-                    pMod->InitTerm.pNext->InitTerm.pPrev = NULL;
-                rc = kldrDyldModCallInit(pMod);
-            }
-        }
-
-        /*
-         * Complete the load by incrementing the dynamic load count of the
-         * requested module (return handle is already set).
-         */
-        if (!rc)
-        {
-            rc = kldrDyldModDynamicLoad(*ppMod);
-            if (!rc)
-            {
-                kldrDyldStackDropFrame(iLoad1st, iLoadEnd, rc);
-                kldrDyldModDeref(*ppMod);
-                return rc;
-            }
-        }
-        kldrDyldStackDropFrame(iLoad1st, iLoadEnd, rc);
-    }
-    else
-    {
-        /* If the reference count is 0 do a quick ref/deref to trigger destruction. */
-        kldrDyldModAddRef(*ppMod);
-        kldrDyldModDeref(*ppMod);
-    }
-
-    /*
-     * We've failed, copy/create error string.
-     */
-    return kldrDyldCopyError(rc, pszErr, cchErr);
+    }
+
+    /*
+     * Complete the load by incrementing the dynamic load count of the
+     * requested module (return handle is already set).
+     */
+    if (!rc)
+    {
+        if (fFlags & KLDRDYLD_LOAD_FLAGS_EXECUTABLE)
+            pLoadedMod->cDepRefs = 0xffff;
+        else
+            rc = kldrDyldModDynamicLoad(pLoadedMod);
+    }
+
+    kldrDyldStackDropFrame(iLoad1st, iLoadEnd, rc);
+    return rc;
 }
 
@@ -620,10 +737,10 @@
  * @returns 0 on success, non-zero error code on failure.
  * @param   pMod            The module to start at.
- * @param   pszDefPrefix    Prefix to use when searching.
- * @param   pszDefSuffix    Suffix to use when searching.
+ * @param   pszPrefix    Prefix to use when searching.
+ * @param   pszSuffix    Suffix to use when searching.
  * @param   enmSearch       Method to use when locating the module and any modules it may depend on.
  * @param   fFlags          Flags, a combintation of the KLDRYDLD_LOAD_FLAGS_* \#defines.
  */
-static int kldrDyldDoLoadPrerequisites(PKLDRDYLDMOD pMod, const char *pszDefPrefix, const char *pszDefSuffix,
+static int kldrDyldDoLoadPrerequisites(PKLDRDYLDMOD pMod, const char *pszPrefix, const char *pszSuffix,
                                        KLDRDYLDSEARCH enmSearch, unsigned fFlags)
 {
@@ -665,5 +782,5 @@
                 case KLDRSTATE_RELOADED:
                 case KLDRSTATE_PENDING_TERMINATION:
-                    rc = kldrDyldModLoadPrerequisites(pMod, pszDefPrefix, pszDefSuffix, enmSearch, fFlags);
+                    rc = kldrDyldModLoadPrerequisites(pMod, pszPrefix, pszSuffix, enmSearch, fFlags);
                     KLDRDYLD_ASSERT(    pMod->enmState == KLDRSTATE_GOOD
                                     ||  pMod->enmState == KLDRSTATE_RELOADED_LOADED_PREREQUISITES
@@ -753,6 +870,6 @@
  * @returns KLDR_ERR_MODULE_NOT_FOUND or I/O error on failure.
  * @param   pszDll          The name of the dll to look for.
- * @param   pszDefPrefix    Prefix than can be used when searching.
- * @param   pszDefSuffix    Suffix than can be used when searching.
+ * @param   pszPrefix    Prefix than can be used when searching.
+ * @param   pszSuffix    Suffix than can be used when searching.
  * @param   enmSearch       Method to use when locating the module.
  * @param   fFlags          Flags, a combintation of the KLDRYDLD_LOAD_FLAGS_* \#defines.
@@ -760,5 +877,5 @@
  * @param   ppMod           Where to put the module we get.
  */
-int kldrDyldGetPrerequisite(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
+int kldrDyldGetPrerequisite(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
                             unsigned fFlags, PKLDRDYLDMOD pDep, PPKLDRDYLDMOD ppMod)
 {
@@ -775,5 +892,5 @@
      * and the action taken is a little bit different.
      */
-    rc = kldrDyldFindExistingModule(pszDll, pszDefPrefix, pszDefSuffix, enmSearch, fFlags, &pMod);
+    rc = kldrDyldFindExistingModule(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, &pMod);
     if (!rc)
     {
@@ -827,5 +944,5 @@
          * We'll have to load it from file.
          */
-        rc = kldrDyldFindNewModule(pszDll, pszDefPrefix, pszDefSuffix, enmSearch, fFlags, ppMod);
+        rc = kldrDyldFindNewModule(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, ppMod);
         if (!rc)
             rc = kldrDyldModMap(pMod);
@@ -1199,8 +1316,8 @@
  * @internal
  */
-static int kldrDyldDoFindByName(const char *pszDll, const char *pszDefPrefix, const char *pszDefSuffix, KLDRDYLDSEARCH enmSearch,
+static int kldrDyldDoFindByName(const char *pszDll, const char *pszPrefix, const char *pszSuffix, KLDRDYLDSEARCH enmSearch,
                                 unsigned fFlags, PPKLDRDYLDMOD ppMod)
 {
-    return kldrDyldFindExistingModule(pszDll, pszDefPrefix, pszDefSuffix, enmSearch, fFlags, ppMod);
+    return kldrDyldFindExistingModule(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, ppMod);
 }
 
@@ -1269,90 +1386,4 @@
 
 
-#if 0
-void kldrLoadExe(PKLDREXEARGS pArgs)
-{
-    /*
-     * Copy the arguments into the globals and do load init.
-     */
-    kLdrFlags = pArgs->fFlags;
-    kLdrHlpMemCopy(kLdrLibraryPath, pArgs->szLibPath, KLDR_MIN(sizeof(pArgs->szLibPath), sizeof(kLdrLibraryPath)));
-    int rc = kldrInit();
-    if (rc)
-        kldrFailure(rc, "kLdr: Init failure, rc=%d\n", rc);
-
-    /*
-     * Open the executable module.
-     */
-    PKLDRMOD pExe;
-    kldrOpenExe(pArgs->szExecutable, &pExe);
-
-    /* Map the segments. */
-    kldrModMapSegments(pExe);
-
-    /*
-     * This is the point where we switch to the executable
-     * stack, allocating it if necessary.
-     */
-    void *pvBottom;
-    kldrModSetupStack(pExe, &pvBottom);
-    kldrLoadExecSwitchStack(pvBottom);
-}
-
-
-void kldrLoadExeOnNewStack(void)
-{
-    /*
-     * Load all prerequisite modules.
-     */
-    PKLDRMOD pCur;
-    do  for (pCur = kLdrModuleHead; pCur; pCur = pCur->pNext)
-        {
-            if (pCur->enmState >= KLDRSTATE_DEPS)
-                continue;
-            kldrModLoadDeps(pCur);
-        }
-    while (pCur);
-
-    /*
-     * Do fixups (FIFO).
-     */
-    for (pCur = kLdrModuleHead; pCur; pCur = pCur->pNext)
-    {
-        if (pCur->enmState >= KLDRSTATE_FIXED)
-            continue;
-        kldrModFixup(pCur, 0);
-    }
-
-    /*
-     * Do module initialization.
-     */
-    for (pCur = kLdrModuleTail; pCur != kLdrModuleTail; pCur = pCur->pPrev)
-    {
-        if (pCur->enmState >= KLDRSTATE_INITED)
-            continue;
-        kldrModCallInit(pCur);
-    }
-
-    /*
-     * Get the executable start address and commit the work that's been done.
-     */
-    void *pvEntry;
-    kldrModGetExeEntry(&pvEntry);
-
-    for (pCur = kLdrModuleHead; pCur; pCur = pCur->pNext)
-        if (pCur->enmState == KLDRSTATE_INITED)
-            pCur->enmState = KLDRSTATE_LOADED;
-
-    kldrHlpSemRelease();
-
-    /*
-     * We're now ready for starting the executable code.
-     */
-    kldrOSStartExe(pLdrModuleHead, pvEntry);
-}
-
-#endif
-
-
 /**
  * Panic / failure
@@ -1363,5 +1394,5 @@
  * @param   ...             Message string arguments.
  */
-int kldrFailure(int rc, const char *pszFormat, ...)
+int kldrDyldFailure(int rc, const char *pszFilename, ...)
 {
     kldrHlpExit(1);
Index: /trunk/kLdr/kLdrDyldFind.c
===================================================================
--- /trunk/kLdr/kLdrDyldFind.c	(revision 2842)
+++ /trunk/kLdr/kLdrDyldFind.c	(revision 2843)
@@ -41,6 +41,6 @@
  *
  * @param   pszName         Partial or complete name, it's specific to the search method to determin which.
- * @param   pszDefPrefix    Prefix than can be used when searching.
- * @param   pszDefSuffix    Suffix than can be used when searching.
+ * @param   pszPrefix       Prefix than can be used when searching.
+ * @param   pszSuffix       Suffix than can be used when searching.
  * @param   enmSearch       The file search method to apply.
  * @param   fFlags          Search flags.
@@ -61,6 +61,6 @@
  *
  * @param   pszName         Partial or complete name, it's specific to the search method to determin which.
- * @param   pszDefPrefix    Prefix than can be used when searching.
- * @param   pszDefSuffix    Suffix than can be used when searching.
+ * @param   pszPrefix       Prefix than can be used when searching.
+ * @param   pszSuffix       Suffix than can be used when searching.
  * @param   enmSearch       The file search method to apply.
  * @param   fFlags          Search flags.
Index: /trunk/kLdr/kLdrInternal.h
===================================================================
--- /trunk/kLdr/kLdrInternal.h	(revision 2842)
+++ /trunk/kLdr/kLdrInternal.h	(revision 2843)
@@ -305,7 +305,7 @@
 
 
-int kldrDyldFindNewModule(const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
+int kldrDyldFindNewModule(const char *pszName, const char *pszPrefix, const char *pszSuffix,
                           KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod);
-int kldrDyldFindExistingModule(const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
+int kldrDyldFindExistingModule(const char *pszName, const char *pszPrefix, const char *pszSuffix,
                                KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod);
 
@@ -327,5 +327,5 @@
 int kldrDyldModMap(PKLDRDYLDMOD pMod);
 int kldrDyldModUnmap(PKLDRDYLDMOD pMod);
-int kldrDyldModLoadPrerequisites(PKLDRDYLDMOD pMod, const char *pszDefPrefix, const char *pszDefSuffix,
+int kldrDyldModLoadPrerequisites(PKLDRDYLDMOD pMod, const char *pszPrefix, const char *pszSuffix,
                                  KLDRDYLDSEARCH enmSearch, unsigned fFlags);
 int kldrDyldModCheckPrerequisites(PKLDRDYLDMOD pMod);
@@ -337,5 +337,5 @@
 int kldrDyldModAttachThread(PKLDRDYLDMOD pMod);
 int kldrDyldModDetachThread(PKLDRDYLDMOD pMod);
-int kldrDyldModGetStackInfo(PKLDRDYLDMOD pMod, void *pvStack, size_t *pcbStack, size_t);
+int kldrDyldModGetStackInfo(PKLDRDYLDMOD pMod, void **ppvStack, size_t *pcbStack);
 int kldrDyldModStartExe(PKLDRDYLDMOD pMod);
 
@@ -345,5 +345,7 @@
 
 
-void kldrDyldFailure(const char *pszFilename, ...);
+int kldrDyldFailure(int rc, const char *pszFilename, ...);
+int kldrInit(void);
+void kldrTerm(void);
 
 
