]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/include/grub/dl.h
1.1.07 release
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / include / grub / dl.h
1 /* dl.h - types and prototypes for loadable module support */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef GRUB_DL_H
21 #define GRUB_DL_H 1
22
23 #include <grub/symbol.h>
24 #ifndef ASM_FILE
25 #include <grub/err.h>
26 #include <grub/types.h>
27 #include <grub/elf.h>
28 #include <grub/list.h>
29 #include <grub/misc.h>
30 #endif
31
32 /*
33 * Macros GRUB_MOD_INIT and GRUB_MOD_FINI are also used by build rules
34 * to collect module names, so we define them only when they are not
35 * defined already.
36 */
37 #ifndef ASM_FILE
38
39 #ifndef GRUB_MOD_INIT
40
41 #if !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_KERNEL)
42
43 #define GRUB_MOD_INIT(name) \
44 static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
45 static void \
46 grub_mod_init (grub_dl_t mod __attribute__ ((unused)))
47
48 #define GRUB_MOD_FINI(name) \
49 static void grub_mod_fini (void) __attribute__ ((used)); \
50 static void \
51 grub_mod_fini (void)
52
53 #elif defined (GRUB_KERNEL)
54
55 #define GRUB_MOD_INIT(name) \
56 static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
57 void \
58 grub_##name##_init (void) { grub_mod_init (0); } \
59 static void \
60 grub_mod_init (grub_dl_t mod __attribute__ ((unused)))
61
62 #define GRUB_MOD_FINI(name) \
63 static void grub_mod_fini (void) __attribute__ ((used)); \
64 void \
65 grub_##name##_fini (void) { grub_mod_fini (); } \
66 static void \
67 grub_mod_fini (void)
68
69 #else
70
71 #define GRUB_MOD_INIT(name) \
72 static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
73 void grub_##name##_init (void); \
74 void \
75 grub_##name##_init (void) { grub_mod_init (0); } \
76 static void \
77 grub_mod_init (grub_dl_t mod __attribute__ ((unused)))
78
79 #define GRUB_MOD_FINI(name) \
80 static void grub_mod_fini (void) __attribute__ ((used)); \
81 void grub_##name##_fini (void); \
82 void \
83 grub_##name##_fini (void) { grub_mod_fini (); } \
84 static void \
85 grub_mod_fini (void)
86
87 #endif
88
89 #endif
90
91 #endif
92
93 #ifndef ASM_FILE
94 #ifdef __APPLE__
95 #define GRUB_MOD_SECTION(x) "_" #x ", _" #x ""
96 #else
97 #define GRUB_MOD_SECTION(x) "." #x
98 #endif
99 #else
100 #ifdef __APPLE__
101 #define GRUB_MOD_SECTION(x) _ ## x , _ ##x
102 #else
103 #define GRUB_MOD_SECTION(x) . ## x
104 #endif
105 #endif
106
107 /* Me, Vladimir Serbinenko, hereby I add this module check as per new
108 GNU module policy. Note that this license check is informative only.
109 Modules have to be licensed under GPLv3 or GPLv3+ (optionally
110 multi-licensed under other licences as well) independently of the
111 presence of this check and solely by linking (module loading in GRUB
112 constitutes linking) and GRUB core being licensed under GPLv3+.
113 Be sure to understand your license obligations.
114 */
115 #ifndef ASM_FILE
116 #if GNUC_PREREQ (3,2)
117 #define ATTRIBUTE_USED __used__
118 #else
119 #define ATTRIBUTE_USED __unused__
120 #endif
121 #define GRUB_MOD_LICENSE(license) \
122 static char grub_module_license[] __attribute__ ((section (GRUB_MOD_SECTION (module_license)), ATTRIBUTE_USED)) = "LICENSE=" license;
123 #define GRUB_MOD_DEP(name) \
124 static const char grub_module_depend_##name[] \
125 __attribute__((section(GRUB_MOD_SECTION(moddeps)), ATTRIBUTE_USED)) = #name
126 #define GRUB_MOD_NAME(name) \
127 static const char grub_module_name_##name[] \
128 __attribute__((section(GRUB_MOD_SECTION(modname)), __used__)) = #name
129 #else
130 #ifdef __APPLE__
131 .macro GRUB_MOD_LICENSE
132 .section GRUB_MOD_SECTION(module_license)
133 .ascii "LICENSE="
134 .ascii $0
135 .byte 0
136 .endm
137 #else
138 .macro GRUB_MOD_LICENSE license
139 .section GRUB_MOD_SECTION(module_license), "a"
140 .ascii "LICENSE="
141 .ascii "\license"
142 .byte 0
143 .endm
144 #endif
145 #endif
146
147 /* Under GPL license obligations you have to distribute your module
148 under GPLv3(+). However, you can also distribute the same code under
149 another license as long as GPLv3(+) version is provided.
150 */
151 #define GRUB_MOD_DUAL_LICENSE(x)
152
153 #ifndef ASM_FILE
154
155 struct grub_dl_segment
156 {
157 struct grub_dl_segment *next;
158 void *addr;
159 grub_size_t size;
160 unsigned section;
161 };
162 typedef struct grub_dl_segment *grub_dl_segment_t;
163
164 struct grub_dl;
165
166 struct grub_dl_dep
167 {
168 struct grub_dl_dep *next;
169 struct grub_dl *mod;
170 };
171 typedef struct grub_dl_dep *grub_dl_dep_t;
172
173 #ifndef GRUB_UTIL
174 struct grub_dl
175 {
176 char *name;
177 int ref_count;
178 int persistent;
179 grub_dl_dep_t dep;
180 grub_dl_segment_t segment;
181 Elf_Sym *symtab;
182 grub_size_t symsize;
183 void (*init) (struct grub_dl *mod);
184 void (*fini) (void);
185 #if !defined (__i386__) && !defined (__x86_64__)
186 void *got;
187 void *gotptr;
188 void *tramp;
189 void *trampptr;
190 #endif
191 #if defined(__mips__) && (_MIPS_SIM != _ABI64)
192 grub_uint32_t *reginfo;
193 #endif
194 void *base;
195 grub_size_t sz;
196 struct grub_dl *next;
197 };
198 #endif
199 typedef struct grub_dl *grub_dl_t;
200
201 grub_dl_t grub_dl_load_file (const char *filename);
202 grub_dl_t EXPORT_FUNC(grub_dl_load) (const char *name);
203 grub_dl_t grub_dl_load_core (void *addr, grub_size_t size);
204 grub_dl_t EXPORT_FUNC(grub_dl_load_core_noinit) (void *addr, grub_size_t size);
205 int EXPORT_FUNC(grub_dl_unload) (grub_dl_t mod);
206 void grub_dl_unload_unneeded (void);
207 int EXPORT_FUNC(grub_dl_ref) (grub_dl_t mod);
208 int EXPORT_FUNC(grub_dl_unref) (grub_dl_t mod);
209 extern grub_dl_t EXPORT_VAR(grub_dl_head);
210
211 #ifndef GRUB_UTIL
212
213 #define FOR_DL_MODULES(var) FOR_LIST_ELEMENTS ((var), (grub_dl_head))
214
215 #ifdef GRUB_MACHINE_EMU
216 void *
217 grub_osdep_dl_memalign (grub_size_t align, grub_size_t size);
218 void
219 grub_dl_osdep_dl_free (void *ptr);
220 #endif
221
222 static inline void
223 grub_dl_init (grub_dl_t mod)
224 {
225 if (mod->init)
226 (mod->init) (mod);
227
228 mod->next = grub_dl_head;
229 grub_dl_head = mod;
230 }
231
232 static inline grub_dl_t
233 grub_dl_get (const char *name)
234 {
235 grub_dl_t l;
236
237 FOR_DL_MODULES(l)
238 if (grub_strcmp (name, l->name) == 0)
239 return l;
240
241 return 0;
242 }
243
244 static inline void
245 grub_dl_set_persistent (grub_dl_t mod)
246 {
247 mod->persistent = 1;
248 }
249
250 static inline int
251 grub_dl_is_persistent (grub_dl_t mod)
252 {
253 return mod->persistent;
254 }
255
256 #endif
257
258 grub_err_t grub_dl_register_symbol (const char *name, void *addr,
259 int isfunc, grub_dl_t mod);
260
261 grub_err_t grub_arch_dl_check_header (void *ehdr);
262 #ifndef GRUB_UTIL
263 grub_err_t
264 grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
265 Elf_Shdr *s, grub_dl_segment_t seg);
266 #endif
267
268 #if defined (__mips__) && (_MIPS_SIM != _ABI64)
269 #define GRUB_LINKER_HAVE_INIT 1
270 void grub_arch_dl_init_linker (void);
271 #endif
272
273 #define GRUB_IA64_DL_TRAMP_ALIGN 16
274 #define GRUB_IA64_DL_GOT_ALIGN 16
275
276 grub_err_t
277 grub_ia64_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
278 grub_size_t *got);
279 grub_err_t
280 grub_arm64_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
281 grub_size_t *got);
282
283 #if defined (__ia64__)
284 #define GRUB_ARCH_DL_TRAMP_ALIGN GRUB_IA64_DL_TRAMP_ALIGN
285 #define GRUB_ARCH_DL_GOT_ALIGN GRUB_IA64_DL_GOT_ALIGN
286 #define grub_arch_dl_get_tramp_got_size grub_ia64_dl_get_tramp_got_size
287 #elif defined (__aarch64__)
288 #define grub_arch_dl_get_tramp_got_size grub_arm64_dl_get_tramp_got_size
289 #else
290 grub_err_t
291 grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
292 grub_size_t *got);
293 #endif
294
295 #if defined (__powerpc__) || (defined (__mips__) && (_MIPS_SIM != _ABI64)) || defined (__arm__) || \
296 (defined(__riscv) && (__riscv_xlen == 32))
297 #define GRUB_ARCH_DL_TRAMP_ALIGN 4
298 #define GRUB_ARCH_DL_GOT_ALIGN 4
299 #endif
300
301 #if defined (__aarch64__) || defined (__sparc__) || (defined (__mips__) && (_MIPS_SIM == _ABI64)) || \
302 (defined(__riscv) && (__riscv_xlen == 64))
303 #define GRUB_ARCH_DL_TRAMP_ALIGN 8
304 #define GRUB_ARCH_DL_GOT_ALIGN 8
305 #endif
306
307 #endif
308
309 #endif /* ! GRUB_DL_H */