source: trunk/kLdr/kLdrInternal.h@ 2840

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

think I got the state stuff right now.

  • Property svn:keywords set to Id
File size: 12.1 KB
Line 
1/* $Id: kLdrInternal.h 2840 2006-10-29 01:32:25Z bird $ */
2/** @file
3 *
4 * kLdr - The Dynamic Loader, internal header.
5 *
6 * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
7 *
8 *
9 * This file is part of kLdr.
10 *
11 * kLdr is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kLdr is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kLdr; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27
28#ifndef __kLdrInternal_h__
29#define __kLdrInternal_h__
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35/* ignore definitions in winnt.h */
36#undef IMAGE_DOS_SIGNATURE
37#undef IMAGE_NT_SIGNATURE
38
39/** @name Signatures we know
40 * @{ */
41/** ELF signature ("\x7fELF"). */
42#define IMAGE_ELF_SIGNATURE KLDRHLP_LE2H_U32(0x7f | ('E' << 8) | ((uint32_t)'L' << 16) | ((uint32_t)'F' << 24))
43/** PE signature ("PE\0\0"). */
44#define IMAGE_NT_SIGNATURE KLDRHLP_LE2H_U32('P' | ('E' << 8))
45/** LX signature ("LX") */
46#define IMAGE_LX_SIGNATURE KLDRHLP_LE2H_U16('L' | ('X' << 8))
47/** LE signature ("LE") */
48#define IMAGE_LE_SIGNATURE KLDRHLP_LE2H_U16('L' | ('E' << 8))
49/** NE signature ("NE") */
50#define IMAGE_NE_SIGNATURE KLDRHLP_LE2H_U16('N' | ('E' << 8))
51/** MZ signature ("MZ"). */
52#define IMAGE_DOS_SIGNATURE KLDRHLP_LE2H_U16('M' | ('Z' << 8))
53/** @} */
54
55/** @defgroup grp_kLdrInternal Internals
56 * @internal
57 * @{
58 */
59
60
61/** Native file provider operations. */
62extern const KLDRRDROPS g_kLdrRdrFileOps;
63
64
65/**
66 * The state of a dynamic loader module.
67 * @image html KLDRSTATE.gif "The state diagram"
68 */
69typedef enum KLDRSTATE
70{
71 /** The usual invalid 0 enum. */
72 KLDRSTATE_INVALID = 0,
73
74 /** The module has just been opened and linked into the load list.
75 *
76 * Prev state: -
77 * Next state: MAPPED, PENDING_DESTROY
78 */
79 KLDRSTATE_OPEN,
80
81 /** The module segments has been mapped into the process memory.
82 *
83 * Prev state: OPEN
84 * Next state: LOADED_PREREQUISITES, PENDING_DESTROY
85 */
86 KLDRSTATE_MAPPED,
87 /** The module has been reloaded and needs to be fixed up again.
88 * This can occure when the loader is called recursivly.
89 *
90 * The reason RELOADED modules must go back to the PENDING_GC state is
91 * because we want to guard against uninit order issues, and therefore
92 * doesn't unmap modules untill all pending termintation callbacks has
93 * been executed.
94 *
95 * Prev state: PENDING_GC
96 * Next state: RELOADED_LOADED_PREREQUISITES, PENDING_GC
97 */
98 KLDRSTATE_RELOADED,
99
100 /** The immediate prerequisites have been loaded.
101 *
102 * Prev state: MAPPED
103 * Next state: FIXED_UP, PENDING_DESTROY
104 */
105 KLDRSTATE_LOADED_PREREQUISITES,
106 /** The immediate prerequisites have been loaded for a reloaded module.
107 *
108 * Prev state: RELOADED
109 * Next state: RELOADED_FIXED_UP, PENDING_GC
110 */
111 KLDRSTATE_RELOADED_LOADED_PREREQUISITES,
112
113 /** Fixups has been applied.
114 *
115 * Prev state: LOADED_PREREQUISITES
116 * Next state: PENDING_INITIALIZATION, PENDING_DESTROY
117 */
118 KLDRSTATE_FIXED_UP,
119 /** Fixups has been applied.
120 *
121 * Prev state: RELOADED_LOADED_PREREQUISITES
122 * Next state: PENDING_INITIALIZATION, PENDING_GC
123 */
124 KLDRSTATE_RELOADED_FIXED_UP,
125
126 /** Pending initialization.
127 * While the module is in this state the loader is in reentrant mode.
128 *
129 * Prev state: FIXED_UP, RELOADED_FIXED_UP
130 * Next state: INITIALIZATION, PENDING_GC
131 */
132 KLDRSTATE_PENDING_INITIALIZATION,
133
134 /** Initializing.
135 * While the module is in this state the loader is in reentrant mode.
136 *
137 * Prev state: PENDING_INITIALIZATION
138 * Next state: GOOD, PENDING_GC
139 */
140 KLDRSTATE_INITIALIZING,
141
142 /** Initialization failed.
143 *
144 * This is somewhat similar to PENDING_GC except that, a module
145 * in this state cannot be reloaded untill we've done GC. This ensures
146 * that a init failure during recursive loading is propagated up.
147 *
148 * While the module is in this state the loader is in reentrant mode.
149 *
150 * Prev state: INITIALIZING
151 * Next state: GC
152 */
153 KLDRSTATE_INITIALIZATION_FAILED,
154
155 /** The module has been successfully loaded and initialized.
156 * While the module is in this state the loader can be in reentrant
157 * or 'unused' mode.
158 *
159 * Prev state: INITIALIZING
160 * Next state: PENDING_TERMINATION
161 */
162 KLDRSTATE_GOOD,
163
164 /** Pending termination, reference count is 0.
165 * While the module is in this state the loader is in reentrant mode.
166 *
167 * Prev state: GOOD
168 * Next state: TERMINATING, GOOD
169 */
170 KLDRSTATE_PENDING_TERMINATION,
171
172 /** Terminating, reference count is still 0.
173 * While the module is in this state the loader is in reentrant mode.
174 *
175 * Prev state: PENDING_TERMINATION
176 * Next state: PENDING_GC
177 */
178 KLDRSTATE_TERMINATING,
179
180 /** Pending garbage collection.
181 *
182 * Prev state: TERMINATING, PENDING_INITIALIZATION, INITIALIZATION_FAILED
183 * Next state: GC, RELOADED
184 */
185 KLDRSTATE_PENDING_GC,
186
187 /** Being garbage collected.
188 *
189 * Prev state: PENDING_GC, INITIALIZATION_FAILED
190 * Next state: PENDING_DESTROY, DESTROYED
191 */
192 KLDRSTATE_GC,
193
194 /** The module has be unlinked, but there are still stack references to it.
195 *
196 * Prev state: GC, FIXED_UP, LOADED_PREREQUISITES, MAPPED, OPEN
197 * Next state: DESTROYED
198 */
199 KLDRSTATE_PENDING_DESTROY,
200
201 /** The module has been destroyed and is no longer valid.
202 *
203 * Prev state: GC, PENDING_DESTROY
204 */
205 KLDRSTATE_DESTROYED,
206
207 /** The end of valid states (exclusive) */
208 KLDRSTATE_END = KLDRSTATE_DESTROYED,
209 /** The usual 32-bit blowup. */
210 KLDRSTATE_32BIT_HACK = 0x7fffffff
211} KLDRSTATE;
212
213
214/**
215 * Dynamic loader module.
216 */
217typedef struct KLDRDYLDMOD
218{
219 /** Magic number. */
220 uint32_t u32MagicHead;
221 /** The module state. */
222 KLDRSTATE enmState;
223 /** The module. */
224 PKLDRMOD pMod;
225 /** The module handle. */
226 HKLDRMOD hMod;
227 /** The total number of references. */
228 uint32_t cRefs;
229 /** The number of dependency references. */
230 uint32_t cDepRefs;
231 /** The number of dynamic load references. */
232 uint32_t cDynRefs;
233 /** Set if this is the executable module.
234 * When clear, the module is a shared object or relocatable object. */
235 uint32_t fExecutable : 1;
236 /** Global DLL (set) or specific DLL (clear). */
237 uint32_t fGlobalOrSpecific : 1;
238 /** Whether the module contains bindable symbols in the global unix namespace. */
239 uint32_t fBindable : 1;
240 /** Reserved for future use. */
241 uint32_t fReserved : 28;
242 /** Already checked dependencies.
243 * This is flag used when resolving module dependencies during a load, it
244 * deals with modules in the KLDRSTATE_PENDING_INITIALIZATION state. */
245 uint32_t fAlreadySeen : 1;
246 /** The load list linkage. */
247 struct
248 {
249 /** The next module in the list. */
250 struct KLDRDYLDMOD *pNext;
251 /** The prev module in the list. */
252 struct KLDRDYLDMOD *pPrev;
253 } Load;
254 /** The termination list linkage.
255 * A module will be linked into the termination list upon a successful
256 * return from module initialization. */
257 struct
258 {
259 /** The next module in the list. */
260 struct KLDRDYLDMOD *pNext;
261 /** The prev module in the list. */
262 struct KLDRDYLDMOD *pPrev;
263 } Term;
264 /** The bind order list linkage.
265 * The module is not in this list when fBindable is clear. */
266 struct
267 {
268 /** The next module in the list. */
269 struct KLDRDYLDMOD *pNext;
270 /** The prev module in the list. */
271 struct KLDRDYLDMOD *pPrev;
272 } Bind;
273 /** Magic number. */
274 uint32_t u32MagicTail;
275} KLDRDYLDMOD, *PKLDRDYLDMOD, **PPKLDRDYLDMOD;
276
277/** KLDRDYLDMOD magic value. (Fuyumi Soryo) */
278#define KLDRDYMOD_MAGIC 0x19590106
279
280/** Return / crash validation of a module handle argument. */
281#define KLDRDYLD_VALIDATE_HKLDRMOD(hMod) \
282 do { \
283 if ( (hMod) == NIL_HKLDRMOD \
284 || (hMod)->u32MagicHead != KLDRDYMOD_MAGIC \
285 || (hMod)->u32MagicTail != KLDRDYMOD_MAGIC) \
286 { \
287 return KLDR_ERR_INVALID_HANDLE; \
288 } \
289 } while (0)
290
291
292int kldrDyldFindNewModule(const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
293 KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod);
294int kldrDyldFindExistingModule(const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
295 KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod);
296
297
298int kldrDyldModCreate(PKLDRRDR pRdr, PPKLDRDYLDMOD ppMod);
299int kldrDyldModDestroy(PKLDRDYLDMOD pMod);
300int kldrDyldModAddRef(PKLDRDYLDMOD pMod);
301void kldrDyldModDeref(PKLDRDYLDMOD pMod);
302int kldrDyldModAddDep(PKLDRDYLDMOD pMod, PKLDRDYLDMOD pDep);
303void kldrDyldModRemoveDep(PKLDRDYLDMOD pMod, PKLDRDYLDMOD pDep);
304int kldrDyldModDynamicLoad(PKLDRDYLDMOD pMod);
305int kldrDyldModDynamicUnload(PKLDRDYLDMOD pMod);
306int kldrDyldModMarkGlobal(PKLDRDYLDMOD pMod);
307int kldrDyldModMarkSpecific(PKLDRDYLDMOD pMod);
308int kldrDyldModSetBindable(PKLDRDYLDMOD pMod);
309int kldrDyldModClearBindable(PKLDRDYLDMOD pMod);
310int kldrDyldModSetDeepBindable(PKLDRDYLDMOD pMod);
311int kldrDyldModClearDeepBindable(PKLDRDYLDMOD pMod);
312int kldrDyldModMap(PKLDRDYLDMOD pMod);
313int kldrDyldModUnmap(PKLDRDYLDMOD pMod);
314int kldrDyldModLoadPrerequisites(PKLDRDYLDMOD pMod, const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
315 KLDRDYLDSEARCH enmSearch, unsigned fFlags);
316int kldrDyldModCheckPrerequisites(PKLDRDYLDMOD pMod);
317void kldrDyldModUnloadPrerequisites(PKLDRDYLDMOD pMod);
318int kldrDyldModFixup(PKLDRDYLDMOD pMod);
319int kldrDyldModCallInit(PKLDRDYLDMOD pMod);
320void kldrDyldModCallTerm(PKLDRDYLDMOD pMod);
321int kldrDyldModReload(PKLDRDYLDMOD pMod);
322int kldrDyldModAttachThread(PKLDRDYLDMOD pMod);
323int kldrDyldModDetachThread(PKLDRDYLDMOD pMod);
324int kldrDyldModGetStackInfo(PKLDRDYLDMOD pMod, void *pvStack, size_t *pcbStack, size_t);
325int kldrDyldModStartExe(PKLDRDYLDMOD pMod);
326
327int kldrDyldModGetName(PKLDRDYLDMOD pMod, char *pszName, size_t cchName);
328int kldrDyldModGetFilename(PKLDRDYLDMOD pMod, char *pszFilename, size_t cchFilename);
329int kldrDyldModQuerySymbol(PKLDRDYLDMOD pMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, uintptr_t *pValue, uint32_t *pfKind);
330
331
332void kldrDyldFailure(const char *pszFilename, ...);
333
334
335/** Pointer to the head module of the load list (the executable). */
336extern PKLDRDYLDMOD kLdrDyldModuleHead;
337/** Pointer to the tail module of the load list. */
338extern PKLDRDYLDMOD kLdrDyldModuleTail;
339/** Pointer to the head module of the termination order list. */
340extern PKLDRDYLDMOD g_pkLdrDyldTermHead;
341/** Pointer to the tail module of the termination order list. */
342extern PKLDRDYLDMOD g_pkLdrDyldTermTail;
343/** Pointer to the head module of the bind order list.
344 * The modules in this list makes up the global namespace used when binding symbol unix fashion. */
345extern PKLDRDYLDMOD g_pkLdrDyldBindHead;
346/** Pointer to the tail module of the bind order list. */
347extern PKLDRDYLDMOD g_pkLdrDyldBindTail;
348
349/** The global error buffer. */
350extern char g_szkLdrDyldError[1024];
351
352/** The Library search path. */
353extern char kLdrDyldLibraryPath[4096];
354
355
356/** @} */
357#ifdef __cplusplus
358}
359#endif
360
361#endif
Note: See TracBrowser for help on using the repository browser.