1 /* dl.h - types and prototypes for loadable module support */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
23 #include <grub/symbol.h>
26 #include <grub/types.h>
28 #include <grub/list.h>
29 #include <grub/misc.h>
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
41 #if !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_KERNEL)
43 #define GRUB_MOD_INIT(name) \
44 static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
46 grub_mod_init (grub_dl_t mod __attribute__ ((unused)))
48 #define GRUB_MOD_FINI(name) \
49 static void grub_mod_fini (void) __attribute__ ((used)); \
53 #elif defined (GRUB_KERNEL)
55 #define GRUB_MOD_INIT(name) \
56 static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
58 grub_##name##_init (void) { grub_mod_init (0); } \
60 grub_mod_init (grub_dl_t mod __attribute__ ((unused)))
62 #define GRUB_MOD_FINI(name) \
63 static void grub_mod_fini (void) __attribute__ ((used)); \
65 grub_##name##_fini (void) { grub_mod_fini (); } \
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); \
75 grub_##name##_init (void) { grub_mod_init (0); } \
77 grub_mod_init (grub_dl_t mod __attribute__ ((unused)))
79 #define GRUB_MOD_FINI(name) \
80 static void grub_mod_fini (void) __attribute__ ((used)); \
81 void grub_##name##_fini (void); \
83 grub_##name##_fini (void) { grub_mod_fini (); } \
95 #define GRUB_MOD_SECTION(x) "_" #x ", _" #x ""
97 #define GRUB_MOD_SECTION(x) "." #x
101 #define GRUB_MOD_SECTION(x) _ ## x , _ ##x
103 #define GRUB_MOD_SECTION(x) . ## x
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.
116 #if GNUC_PREREQ (3,2)
117 #define ATTRIBUTE_USED __used__
119 #define ATTRIBUTE_USED __unused__
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
131 .macro GRUB_MOD_LICENSE
132 .section
GRUB_MOD_SECTION(module_license
)
138 .macro GRUB_MOD_LICENSE license
139 .section
GRUB_MOD_SECTION(module_license
), "a"
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.
151 #define GRUB_MOD_DUAL_LICENSE(x)
155 struct grub_dl_segment
157 struct grub_dl_segment
*next
;
162 typedef struct grub_dl_segment
*grub_dl_segment_t
;
168 struct grub_dl_dep
*next
;
171 typedef struct grub_dl_dep
*grub_dl_dep_t
;
180 grub_dl_segment_t segment
;
183 void (*init
) (struct grub_dl
*mod
);
185 #if !defined (__i386__) && !defined (__x86_64__)
191 #if defined(__mips__) && (_MIPS_SIM != _ABI64)
192 grub_uint32_t
*reginfo
;
196 struct grub_dl
*next
;
199 typedef struct grub_dl
*grub_dl_t
;
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
);
213 #define FOR_DL_MODULES(var) FOR_LIST_ELEMENTS ((var), (grub_dl_head))
215 #ifdef GRUB_MACHINE_EMU
217 grub_osdep_dl_memalign (grub_size_t align
, grub_size_t size
);
219 grub_dl_osdep_dl_free (void *ptr
);
223 grub_dl_init (grub_dl_t mod
)
228 mod
->next
= grub_dl_head
;
232 static inline grub_dl_t
233 grub_dl_get (const char *name
)
238 if (grub_strcmp (name
, l
->name
) == 0)
245 grub_dl_set_persistent (grub_dl_t mod
)
251 grub_dl_is_persistent (grub_dl_t mod
)
253 return mod
->persistent
;
258 grub_err_t
grub_dl_register_symbol (const char *name
, void *addr
,
259 int isfunc
, grub_dl_t mod
);
261 grub_err_t
grub_arch_dl_check_header (void *ehdr
);
264 grub_arch_dl_relocate_symbols (grub_dl_t mod
, void *ehdr
,
265 Elf_Shdr
*s
, grub_dl_segment_t seg
);
268 #if defined (__mips__) && (_MIPS_SIM != _ABI64)
269 #define GRUB_LINKER_HAVE_INIT 1
270 void grub_arch_dl_init_linker (void);
273 #define GRUB_IA64_DL_TRAMP_ALIGN 16
274 #define GRUB_IA64_DL_GOT_ALIGN 16
277 grub_ia64_dl_get_tramp_got_size (const void *ehdr
, grub_size_t
*tramp
,
280 grub_arm64_dl_get_tramp_got_size (const void *ehdr
, grub_size_t
*tramp
,
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
291 grub_arch_dl_get_tramp_got_size (const void *ehdr
, grub_size_t
*tramp
,
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
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
309 #endif /* ! GRUB_DL_H */