source: trunk/kLdr/kLdrInternal.h@ 2837

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

in progress...

  • Property svn:keywords set to Id
File size: 10.5 KB
Line 
1/* $Id: kLdrInternal.h 2837 2006-10-28 03:59:21Z 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 */
68typedef enum KLDRSTATE
69{
70 /** The usual invalid 0 enum. */
71 KLDRSTATE_INVALID = 0,
72 /** The module has just been opened and has no references.
73 * Prev state: -
74 * Next state: MAPPED, DESTROYED
75 */
76 KLDRSTATE_OPEN,
77 /** The module been mapped.
78 * Prev state: OPEN
79 * Next state: LOADED_PREREQUISITES, DESTROYED
80 */
81 KLDRSTATE_MAPPED,
82 /** The module has been reloaded and needs to be fixed up again.
83 * (The loader can still be in a re-entrant mode.)
84 * Prev state: PENDING_GC
85 * Next state: LOADED_PREREQUISITES, PENDING_GC
86 */
87 KLDRSTATE_RELOADED,
88 /** The immediate prerequisites have been loaded.
89 * Prev state: MAPPED, RELOADED
90 * Next state: FIXED_UP, DESTROYED
91 */
92 KLDRSTATE_LOADED_PREREQUISITES,
93 /** Fixups has been applied.
94 * Prev state: LOADED_PREREQUISITES
95 * Next state: PENDING_INIT, DESTROYED
96 */
97 KLDRSTATE_FIXED_UP,
98 /** Pending initialization.
99 * (The loader can now be in a re-entrant mode.)
100 * Prev state: FIXED_UP
101 * Next state: PENDING_INITIALIZATION, PENDING_GC
102 */
103 KLDRSTATE_PENDING_INITIALIZATION,
104 /** Initializing.
105 * (The loader is now in a re-entrant mode.)
106 * Prev state: PENDING_INITIALIZATION
107 * Next state: GOOD, PENDING_GC
108 */
109 KLDRSTATE_INITIALIZING,
110 /** The module has been successfully loaded and initialized.
111 * (The loader can still be in a re-entrant mode.)
112 * Prev state: INITIALIZING
113 * Next state: PENDING_TERMINATION
114 * Re-entrant.
115 */
116 KLDRSTATE_GOOD,
117 /** Pending termination, reference count is 0.
118 * (The loader can now be in a re-entrant mode.)
119 * Prev state: GOOD
120 * Next state: TERMINATING, GOOD
121 */
122 KLDRSTATE_PENDING_TERMINATION,
123 /** Terminating, reference count is still 0.
124 * (The loader is now in a re-entrant mode, but loading is a bit restricted.)
125 * Prev state: PENDING_TERMINATION
126 * Next state: PENDING_GC
127 */
128 KLDRSTATE_TERMINATING,
129 /** Pending garbage collection.
130 * (The loader can still be in a re-entrant mode.)
131 * Prev state: TERMINATING, INITIALIZING, PENDING_INITIALIZATION
132 * Next state: GC, RELOADED
133 */
134 KLDRSTATE_PENDING_GC,
135 /** Being garbage collected.
136 * Prev state: PENDING_GC
137 * Next state: PENDING_DESTROY, DESTROYED
138 */
139 KLDRSTATE_GC,
140 /** The module has be unlinked, but there are still stack references to it.
141 * Prev state: GC
142 * Next state: DESTROYED
143 */
144 KLDRSTATE_PENDING_DESTROY,
145 /** The module has been destroyed and is no longer valid.
146 * Prev state: GC, PENDING_DESTROY
147 */
148 KLDRSTATE_DESTROYED,
149 /** The end of valid states (exclusive) */
150 KLDRSTATE_END = KLDRSTATE_DESTROYED,
151 /** The usual 32-bit blowup. */
152 KLDRSTATE_32BIT_HACK = 0x7fffffff
153} KLDRSTATE;
154
155
156/**
157 * Dynamic loader module.
158 */
159typedef struct KLDRDYLDMOD
160{
161 /** Magic number. */
162 uint32_t u32MagicHead;
163 /** The module state. */
164 KLDRSTATE enmState;
165 /** The module. */
166 PKLDRMOD pMod;
167 /** The module handle. */
168 HKLDRMOD hMod;
169 /** The total number of references. */
170 uint32_t cRefs;
171 /** The number of dependency references. */
172 uint32_t cDepRefs;
173 /** The number of dynamic load references. */
174 uint32_t cDynRefs;
175 /** Set if this is the executable module.
176 * When clear, the module is a shared object or relocatable object. */
177 uint32_t fExecutable : 1;
178 /** Global DLL (set) or specific DLL (clear). */
179 uint32_t fGlobalOrSpecific : 1;
180 /** Whether the module contains bindable symbols in the global unix namespace. */
181 uint32_t fBindable : 1;
182 /** Reserved for future use. */
183 uint32_t fReserved : 28;
184 /** Already checked dependencies.
185 * This is flag used when resolving module dependencies during a load, it
186 * deals with modules in the KLDRSTATE_PENDING_INITIALIZATION state. */
187 uint32_t fAlreadySeen : 1;
188 /** The load list linkage. */
189 struct
190 {
191 /** The next module in the list. */
192 struct KLDRDYLDMOD *pNext;
193 /** The prev module in the list. */
194 struct KLDRDYLDMOD *pPrev;
195 } Load;
196 /** The termination list linkage. */
197 struct
198 {
199 /** The next module in the list. */
200 struct KLDRDYLDMOD *pNext;
201 /** The prev module in the list. */
202 struct KLDRDYLDMOD *pPrev;
203 } Term;
204 /** The bind order list linkage.
205 * The module is not in this list when fBindable is clear. */
206 struct
207 {
208 /** The next module in the list. */
209 struct KLDRDYLDMOD *pNext;
210 /** The prev module in the list. */
211 struct KLDRDYLDMOD *pPrev;
212 } Bind;
213 /** Magic number. */
214 uint32_t u32MagicTail;
215} KLDRDYLDMOD, *PKLDRDYLDMOD, **PPKLDRDYLDMOD;
216
217/** KLDRDYLDMOD magic value. (Fuyumi Soryo) */
218#define KLDRDYMOD_MAGIC 0x19590106
219
220/** Return / crash validation of a module handle argument. */
221#define KLDRDYLD_VALIDATE_HKLDRMOD(hMod) \
222 do { \
223 if ( (hMod) == NIL_HKLDRMOD \
224 || (hMod)->u32MagicHead != KLDRDYMOD_MAGIC \
225 || (hMod)->u32MagicTail != KLDRDYMOD_MAGIC) \
226 { \
227 return KLDR_ERR_INVALID_HANDLE; \
228 } \
229 } while (0)
230
231
232int kldrDyldFindNewModule(const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
233 KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod);
234int kldrDyldFindExistingModule(const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
235 KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod);
236
237
238int kldrDyldModCreate(PKLDRRDR pRdr, PPKLDRDYLDMOD ppMod);
239int kldrDyldModDestroy(PKLDRDYLDMOD pMod);
240int kldrDyldModAddRef(PKLDRDYLDMOD pMod);
241void kldrDyldModDeref(PKLDRDYLDMOD pMod);
242int kldrDyldModAddDep(PKLDRDYLDMOD pMod, PKLDRDYLDMOD pDep);
243void kldrDyldModRemoveDep(PKLDRDYLDMOD pMod, PKLDRDYLDMOD pDep);
244int kldrDyldModDynamicLoad(PKLDRDYLDMOD pMod);
245int kldrDyldModDynamicUnload(PKLDRDYLDMOD pMod);
246int kldrDyldModMarkGlobal(PKLDRDYLDMOD pMod);
247int kldrDyldModMarkSpecific(PKLDRDYLDMOD pMod);
248int kldrDyldModSetBindable(PKLDRDYLDMOD pMod);
249int kldrDyldModClearBindable(PKLDRDYLDMOD pMod);
250int kldrDyldModSetDeepBindable(PKLDRDYLDMOD pMod);
251int kldrDyldModClearDeepBindable(PKLDRDYLDMOD pMod);
252int kldrDyldModMap(PKLDRDYLDMOD pMod);
253int kldrDyldModUnmap(PKLDRDYLDMOD pMod);
254int kldrDyldModLoadPrerequisites(PKLDRDYLDMOD pMod, const char *pszName, const char *pszDefPrefix, const char *pszDefSuffix,
255 KLDRDYLDSEARCH enmSearch, unsigned fFlags);
256int kldrDyldModCheckPrerequisites(PKLDRDYLDMOD pMod);
257void kldrDyldModUnloadPrerequisites(PKLDRDYLDMOD pMod);
258int kldrDyldModFixup(PKLDRDYLDMOD pMod);
259int kldrDyldModCallInit(PKLDRDYLDMOD pMod);
260void kldrDyldModCallTerm(PKLDRDYLDMOD pMod);
261int kldrDyldModReload(PKLDRDYLDMOD pMod);
262int kldrDyldModAttachThread(PKLDRDYLDMOD pMod);
263int kldrDyldModDetachThread(PKLDRDYLDMOD pMod);
264int kldrDyldModGetStackInfo(PKLDRDYLDMOD pMod, void *pvStack, size_t *pcbStack, size_t);
265int kldrDyldModStartExe(PKLDRDYLDMOD pMod);
266
267int kldrDyldModGetName(PKLDRDYLDMOD pMod, char *pszName, size_t cchName);
268int kldrDyldModGetFilename(PKLDRDYLDMOD pMod, char *pszFilename, size_t cchFilename);
269int kldrDyldModQuerySymbol(PKLDRDYLDMOD pMod, uint32_t uSymbolOrdinal, const char *pszSymbolName, uintptr_t *pValue, uint32_t *pfKind);
270
271
272void kldrDyldFailure(const char *pszFilename, ...);
273
274
275/** Pointer to the head module of the load list (the executable). */
276extern PKLDRDYLDMOD kLdrDyldModuleHead;
277/** Pointer to the tail module of the load list. */
278extern PKLDRDYLDMOD kLdrDyldModuleTail;
279/** Pointer to the head module of the termination order list. */
280extern PKLDRDYLDMOD g_pkLdrDyldTermHead;
281/** Pointer to the tail module of the termination order list. */
282extern PKLDRDYLDMOD g_pkLdrDyldTermTail;
283/** Pointer to the head module of the bind order list.
284 * The modules in this list makes up the global namespace used when binding symbol unix fashion. */
285extern PKLDRDYLDMOD g_pkLdrDyldBindHead;
286/** Pointer to the tail module of the bind order list. */
287extern PKLDRDYLDMOD g_pkLdrDyldBindTail;
288
289/** The global error buffer. */
290extern char g_szkLdrDyldError[1024];
291
292/** The Library search path. */
293extern char kLdrDyldLibraryPath[4096];
294
295
296/** @} */
297#ifdef __cplusplus
298}
299#endif
300
301#endif
Note: See TracBrowser for help on using the repository browser.