| 1 | /* $Id: $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * kLdr - The Dynamic Loader.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2006 knut st. osmundsen <[email protected]>
|
|---|
| 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 | /*******************************************************************************
|
|---|
| 37 | * Global Variables *
|
|---|
| 38 | *******************************************************************************/
|
|---|
| 39 | /** Pointer to the head module (the executable).
|
|---|
| 40 | * (This is exported, so no prefix.) */
|
|---|
| 41 | PKLDRDY kLdrDyHead = NULL;
|
|---|
| 42 | /** Pointer to the tail module.
|
|---|
| 43 | * (This is exported, so no prefix.) */
|
|---|
| 44 | PKLDRDY kLdrDyTail = NULL;
|
|---|
| 45 | /** The Library search path. */
|
|---|
| 46 | char kLdrDyLibraryPath[4096];
|
|---|
| 47 | /** The executable flags. */
|
|---|
| 48 | uint32_t kLdrDyFlags;
|
|---|
| 49 | /** Set if we've initialized the loader. */
|
|---|
| 50 | static int fInitialized = 0;
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | /*******************************************************************************
|
|---|
| 54 | * Internal Functions *
|
|---|
| 55 | *******************************************************************************/
|
|---|
| 56 | static int kldrDyInit(void);
|
|---|
| 57 | static int kldrDyTerm(void);
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 | #if 0
|
|---|
| 61 | /**
|
|---|
| 62 | * Initialize the loader.
|
|---|
| 63 | */
|
|---|
| 64 | int kldrDyInit(void)
|
|---|
| 65 | {
|
|---|
| 66 | if (fInitialized)
|
|---|
| 67 | return 0;
|
|---|
| 68 | /** @todo */
|
|---|
| 69 | return 0;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | void kldrLoadExe(PKLDREXEARGS pArgs)
|
|---|
| 75 | {
|
|---|
| 76 | /*
|
|---|
| 77 | * Copy the arguments into the globals and do load init.
|
|---|
| 78 | */
|
|---|
| 79 | kLdrFlags = pArgs->fFlags;
|
|---|
| 80 | kLdrMemCopy(kLdrLibraryPath, pArgs->szLibPath, KLDR_MIN(sizeof(pArgs->szLibPath), sizeof(kLdrLibraryPath)));
|
|---|
| 81 | int rc = kldrInit();
|
|---|
| 82 | if (rc)
|
|---|
| 83 | kldrFailure(rc, "kLdr: Init failure, rc=%d\n", rc);
|
|---|
| 84 |
|
|---|
| 85 | /*
|
|---|
| 86 | * Open the executable module.
|
|---|
| 87 | */
|
|---|
| 88 | PKLDRMOD pExe;
|
|---|
| 89 | kldrOpenExe(pArgs->szExecutable, &pExe);
|
|---|
| 90 |
|
|---|
| 91 | /* Map the segments. */
|
|---|
| 92 | kldrModMapSegments(pExe);
|
|---|
| 93 |
|
|---|
| 94 | /*
|
|---|
| 95 | * This is the point where we switch to the executable
|
|---|
| 96 | * stack, allocating it if necessary.
|
|---|
| 97 | */
|
|---|
| 98 | void *pvBottom;
|
|---|
| 99 | kldrModSetupStack(pExe, &pvBottom);
|
|---|
| 100 | kldrLoadExecSwitchStack(pvBottom);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | void kldrLoadExeOnNewStack(void)
|
|---|
| 105 | {
|
|---|
| 106 | /*
|
|---|
| 107 | * Load all dependant modules.
|
|---|
| 108 | */
|
|---|
| 109 | PKLDRMOD pCur;
|
|---|
| 110 | do for (pCur = kLdrModuleHead; pCur; pCur = pCur->pNext)
|
|---|
| 111 | {
|
|---|
| 112 | if (pCur->enmState >= KLDRSTATE_DEPS)
|
|---|
| 113 | continue;
|
|---|
| 114 | kldrModLoadDeps(pCur);
|
|---|
| 115 | }
|
|---|
| 116 | while (pCur);
|
|---|
| 117 |
|
|---|
| 118 | /*
|
|---|
| 119 | * Do fixups (FIFO).
|
|---|
| 120 | */
|
|---|
| 121 | for (pCur = kLdrModuleHead; pCur; pCur = pCur->pNext)
|
|---|
| 122 | {
|
|---|
| 123 | if (pCur->enmState >= KLDRSTATE_FIXED)
|
|---|
| 124 | continue;
|
|---|
| 125 | kldrModFixup(pCur, 0);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /*
|
|---|
| 129 | * Do module initialization.
|
|---|
| 130 | */
|
|---|
| 131 | for (pCur = kLdrModuleTail; pCur != kLdrModuleTail; pCur = pCur->pPrev)
|
|---|
| 132 | {
|
|---|
| 133 | if (pCur->enmState >= KLDRSTATE_INITED)
|
|---|
| 134 | continue;
|
|---|
| 135 | kldrModCallInit(pCur);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /*
|
|---|
| 139 | * Get the executable start address and commit the work that's been done.
|
|---|
| 140 | */
|
|---|
| 141 | void *pvEntry;
|
|---|
| 142 | kldrModGetExeEntry(&pvEntry);
|
|---|
| 143 |
|
|---|
| 144 | for (pCur = kLdrModuleHead; pCur; pCur = pCur->pNext)
|
|---|
| 145 | if (pCur->enmState == KLDRSTATE_INITED)
|
|---|
| 146 | pCur->enmState = KLDRSTATE_LOADED;
|
|---|
| 147 |
|
|---|
| 148 | kldrSemRelease();
|
|---|
| 149 |
|
|---|
| 150 | /*
|
|---|
| 151 | * We're now ready for starting the executable code.
|
|---|
| 152 | */
|
|---|
| 153 | kldrOSStartExe(pLdrModuleHead, pvEntry);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 | int kLdrLoadDll(const char *pszFilename, unsigned fFlags, void *pvmod)
|
|---|
| 158 | {
|
|---|
| 159 |
|
|---|
| 160 | return -1;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * Panic / failure
|
|---|
| 168 | *
|
|---|
| 169 | * @returns rc if we're in a position where we can return.
|
|---|
| 170 | * @param rc Return code.
|
|---|
| 171 | * @param pszFormat Message string. Limited fprintf like formatted.
|
|---|
| 172 | * @param ... Message string arguments.
|
|---|
| 173 | */
|
|---|
| 174 | int kldrFailure(int rc, const char *pszFormat, ...)
|
|---|
| 175 | {
|
|---|
| 176 | kldrExit(1);
|
|---|
| 177 | return rc;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | #endif
|
|---|
| 181 |
|
|---|