source: trunk/kLdr/kLdr.c@ 2821

Last change on this file since 2821 was 2821, checked in by bird, 19 years ago

kLdr backup commit.

File size: 4.7 KB
Line 
1/* $Id: $ */
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#include "kLdr.h"
28
29/** memcpy */
30#define kLdrMemCopy(a,b,c) __builtin_memcpy(a,b,c)
31/** Get the minimum of two values. */
32#define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b))
33
34
35/*******************************************************************************
36* Global Variables *
37*******************************************************************************/
38/** Pointer to the head module (the executable).
39 * (This is exported, so no prefix.) */
40PKLDRMOD kLdrModuleHead = NULL;
41/** Pointer to the tail module.
42 * (This is exported, so no prefix.) */
43PKLDRMOD kLdrModuleTail = NULL;
44/** The Library search path. */
45char kLdrLibraryPath[4096];
46/** The executable flags. */
47uint32_t kLdrFlags;
48/** Bootstrap stack and temporary space. */
49char abStack[8192];
50/** Set if we've initialized the loader. */
51int fInitialized = 0;
52
53
54/*******************************************************************************
55* Internal Functions *
56*******************************************************************************/
57static int kldrInit(void);
58static int kldrTerm(void);
59
60
61/**
62 * Initialize the loader.
63 */
64int kldrInit(void)
65{
66 if (fInitialized)
67 return 0;
68 /** @todo */
69 return 0;
70}
71
72
73
74void 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
104void 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
157int 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 */
174int kldrFailure(int rc, const char *pszFormat, ...)
175{
176 kldrExit(1);
177 return rc;
178}
179
Note: See TracBrowser for help on using the repository browser.