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 | #ifndef __kLdr_h__
|
---|
28 | #define __kLdr_h__
|
---|
29 |
|
---|
30 | #ifdef __cplusplus
|
---|
31 | extern "C" {
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | /* kLdr depend on size_t, [u]intNN_t, [u]intptr_t and some related constants. */
|
---|
35 | #include <sys/types.h>
|
---|
36 | #include <stdint.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /** A KLDRMOD handle. */
|
---|
40 | typedef struct KLDRMOD *HKLDRMOD;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Loader segment.
|
---|
44 | */
|
---|
45 | typedef struct KLDRSEG
|
---|
46 | {
|
---|
47 | /** The segment load address. */
|
---|
48 | void *pv;
|
---|
49 | /** The size of the segment. */
|
---|
50 | size_t cb;
|
---|
51 | /** The segment is readable. */
|
---|
52 | uint32_t fRead : 1;
|
---|
53 | /** The segment is writable. */
|
---|
54 | uint32_t fWrite : 1;
|
---|
55 | /** The segment is executable. */
|
---|
56 | uint32_t fExecute : 1;
|
---|
57 | /** Reserved for future use. */
|
---|
58 | uint32_t fReserved : 29;
|
---|
59 | /** Reserved for future use. */
|
---|
60 | uint32_t u32Reserved;
|
---|
61 | } KLDRSEG, *PKLDRSEG;
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Loader module type.
|
---|
66 | */
|
---|
67 | typedef enum KLDRTYPE
|
---|
68 | {
|
---|
69 | /** The usual invalid 0 type. */
|
---|
70 | KLDRTYPE_INVALID = 0,
|
---|
71 | /** The native OS loader. */
|
---|
72 | KLDRTYPE_NATIVE,
|
---|
73 | /** The LX loader. */
|
---|
74 | KLDRTYPE_LX,
|
---|
75 | /** The end type (not included). */
|
---|
76 | KLDRTYPE_END,
|
---|
77 | /** Hack to blow the type up to 32-bit. */
|
---|
78 | KLDRTYPE_32BIT_HACK = 0x7fffffff
|
---|
79 | } KLDRTYPE;
|
---|
80 |
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * The state of a module.
|
---|
84 | */
|
---|
85 | typedef enum KLDRSTATE
|
---|
86 | {
|
---|
87 | /** The usual invalid 0 enum. */
|
---|
88 | KLDRSTATE_INVALID = 0,
|
---|
89 | /** kldrOpen succeeded.
|
---|
90 | * Modules in this state will be freed at */
|
---|
91 | KLDRSTATE_OPEN,
|
---|
92 | /** Dependencies has been loaded. */
|
---|
93 | KLDRSTATE_DEPS,
|
---|
94 | /** Fixups has been applied. */
|
---|
95 | KLDRSTATE_FIXED,
|
---|
96 | /** The module has been initialized. */
|
---|
97 | KLDRSTATE_INITED,
|
---|
98 | /** The module is loaded successfully. */
|
---|
99 | KLDRSTATE_LOADED,
|
---|
100 | /** The end of valid states (exclusive) */
|
---|
101 | KLDRSTATE_END,
|
---|
102 | /** The usual 32-bit blowup. */
|
---|
103 | KLDRSTATE_32BIT_HACK = 0x7fffffff
|
---|
104 | } KLDRSTATE;
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Loader module.
|
---|
109 | */
|
---|
110 | typedef struct KLDRMOD
|
---|
111 | {
|
---|
112 | /** Pointer to the next module. */
|
---|
113 | struct KLDRMOD *pNext;
|
---|
114 | /** Pointer to the previous module. */
|
---|
115 | struct KLDRMOD *pPrev;
|
---|
116 | /** Our module handle. */
|
---|
117 | HKLDRMOD hmod;
|
---|
118 | /** The module data. */
|
---|
119 | void *pvData;
|
---|
120 | /** The type of module this is. */
|
---|
121 | KLDRTYPE enmType;
|
---|
122 | /** The module state. */
|
---|
123 | KLDRSTATE enmState;
|
---|
124 | /** The number of references. */
|
---|
125 | uint32_t cRefs;
|
---|
126 | /** The number of dynamic references. */
|
---|
127 | uint32_t cDynRefs;
|
---|
128 | /** Magic number. */
|
---|
129 | uint32_t u32Magic;
|
---|
130 | /** Set if this is the executable module. */
|
---|
131 | uint32_t fExecutable : 1;
|
---|
132 | /** Global DLL (set) or specific DLL (clear). */
|
---|
133 | uint32_t fGlobal : 1;
|
---|
134 | /** Load stage one. */
|
---|
135 | uint32_t fLoadStageOne : 1;
|
---|
136 | /** Reserved for future use. */
|
---|
137 | uint32_t fReserved : 29;
|
---|
138 | /** The filename length (bytes). */
|
---|
139 | uint32_t cchFilename;
|
---|
140 | /** The filename. */
|
---|
141 | const char *pszFilename;
|
---|
142 | /** The module name. */
|
---|
143 | const char *pszName;
|
---|
144 | /** The module name length (bytes). */
|
---|
145 | uint32_t cchName;
|
---|
146 | /** The number of segments in the module. */
|
---|
147 | uint32_t cSegments;
|
---|
148 | /** Segments. (variable size, can be zero) */
|
---|
149 | KLDRSEG aSegments[1];
|
---|
150 | } KLDRMOD, *PKLDRMOD, **PPKLDRMOD;
|
---|
151 |
|
---|
152 | /** Pointer to the head module (the executable). */
|
---|
153 | extern PKLDRMOD kLdrModuleHead;
|
---|
154 | /** Pointer to the tail module. */
|
---|
155 | extern PKLDRMOD kLdrModuleTail;
|
---|
156 | /** The Library search path. */
|
---|
157 | extern char kLdrLibraryPath[4096];
|
---|
158 |
|
---|
159 | /** @name Process Bootstrapping
|
---|
160 | * @{ */
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Argument package from the stub.
|
---|
164 | */
|
---|
165 | typedef struct KLDREXEARGS
|
---|
166 | {
|
---|
167 | /** Flags. (Currently unused, MBZ.) */
|
---|
168 | uint32_t fFlags;
|
---|
169 | /** The executable file that the stub is supposed to load. */
|
---|
170 | char szExecutable[260];
|
---|
171 | /** The LD_LIBRARY_PATH prefix for the process.. */
|
---|
172 | char szLibPath[4096 - 260 - sizeof(uint32_t)];
|
---|
173 | } KLDREXEARGS, *PKLDREXEARGS;
|
---|
174 |
|
---|
175 | void kLdrLoadExe(PKLDREXEARGS pArgs);
|
---|
176 | /** @} */
|
---|
177 |
|
---|
178 | /** @name The Internal APIs
|
---|
179 | * @internal
|
---|
180 | * @{ */
|
---|
181 | int kldrOpenExe(const char *pszFilename, PPKLDRMOD ppMod)
|
---|
182 | int kldrOpen(const char *pszFilename, unsigned fFlags, PPKLDRMOD ppMod);
|
---|
183 | int kldrClose(PKLDRMOD pMod);
|
---|
184 |
|
---|
185 | void kldrFailure(const char *pszFilename, ...);
|
---|
186 | /** @} */
|
---|
187 |
|
---|
188 | /*
|
---|
189 | int kLdrLoadDll(const char *pszFilename, unsigned fFlags, unsigned long *pvmod);
|
---|
190 | */
|
---|
191 |
|
---|
192 | #ifndef NULL
|
---|
193 | # define NULL 0
|
---|
194 | #endif
|
---|
195 |
|
---|
196 | #ifdef __cplusplus
|
---|
197 | }
|
---|
198 | #endif
|
---|
199 |
|
---|
200 | #endif
|
---|
201 |
|
---|