dl.h 7.33 KB
Newer Older
okuji's avatar
okuji committed
1 2
/* 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.
okuji's avatar
okuji committed
5
 *
6
 *  GRUB is free software: you can redistribute it and/or modify
okuji's avatar
okuji committed
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
okuji's avatar
okuji committed
9 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
okuji's avatar
okuji committed
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
okuji's avatar
okuji committed
18 19
 */

20 21
#ifndef GRUB_DL_H
#define GRUB_DL_H	1
okuji's avatar
okuji committed
22

23
#include <grub/symbol.h>
24
#ifndef ASM_FILE
25 26
#include <grub/err.h>
#include <grub/types.h>
27
#include <grub/elf.h>
28
#include <grub/list.h>
29
#include <grub/misc.h>
30
#endif
okuji's avatar
okuji committed
31

32 33 34 35 36
/*
 * Macros GRUB_MOD_INIT and GRUB_MOD_FINI are also used by build rules
 * to collect module names, so we define them only when they are not
 * defined already.
 */
37
#ifndef ASM_FILE
38

39
#ifndef GRUB_MOD_INIT
40 41 42

#if !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_KERNEL)

43 44 45 46 47 48 49 50 51
#define GRUB_MOD_INIT(name)	\
static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
static void \
grub_mod_init (grub_dl_t mod __attribute__ ((unused)))

#define GRUB_MOD_FINI(name)	\
static void grub_mod_fini (void) __attribute__ ((used)); \
static void \
grub_mod_fini (void)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

#elif defined (GRUB_KERNEL)

#define GRUB_MOD_INIT(name)	\
static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
void \
grub_##name##_init (void) { grub_mod_init (0); } \
static void \
grub_mod_init (grub_dl_t mod __attribute__ ((unused)))

#define GRUB_MOD_FINI(name)	\
static void grub_mod_fini (void) __attribute__ ((used)); \
void \
grub_##name##_fini (void) { grub_mod_fini (); } \
static void \
grub_mod_fini (void)
68 69 70

#else

71
#define GRUB_MOD_INIT(name)	\
72
static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
73 74 75
void grub_##name##_init (void); \
void \
grub_##name##_init (void) { grub_mod_init (0); } \
okuji's avatar
okuji committed
76
static void \
77
grub_mod_init (grub_dl_t mod __attribute__ ((unused)))
okuji's avatar
okuji committed
78

79
#define GRUB_MOD_FINI(name)	\
80
static void grub_mod_fini (void) __attribute__ ((used)); \
81 82 83
void grub_##name##_fini (void); \
void \
grub_##name##_fini (void) { grub_mod_fini (); } \
okuji's avatar
okuji committed
84
static void \
85
grub_mod_fini (void)
86

87
#endif
okuji's avatar
okuji committed
88

89 90
#endif

91 92
#endif

93
#ifndef ASM_FILE
94
#ifdef __APPLE__
95 96 97 98 99
#define GRUB_MOD_SECTION(x) "_" #x ", _" #x ""
#else
#define GRUB_MOD_SECTION(x) "." #x
#endif
#else
100
#ifdef __APPLE__
101
#define GRUB_MOD_SECTION(x) _ ## x , _ ##x 
102
#else
103 104
#define GRUB_MOD_SECTION(x) . ## x
#endif
105 106 107 108 109 110 111 112 113 114
#endif

/* Me, Vladimir Serbinenko, hereby I add this module check as per new
   GNU module policy. Note that this license check is informative only.
   Modules have to be licensed under GPLv3 or GPLv3+ (optionally
   multi-licensed under other licences as well) independently of the
   presence of this check and solely by linking (module loading in GRUB
   constitutes linking) and GRUB core being licensed under GPLv3+.
   Be sure to understand your license obligations.
*/
115
#ifndef ASM_FILE
116 117 118 119 120
#if GNUC_PREREQ (3,2)
#define ATTRIBUTE_USED __used__
#else
#define ATTRIBUTE_USED __unused__
#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 124
#define GRUB_MOD_DEP(name)	\
static const char grub_module_depend_##name[] \
125 126 127 128
 __attribute__((section(GRUB_MOD_SECTION(moddeps)), ATTRIBUTE_USED)) = #name
#define GRUB_MOD_NAME(name)	\
static const char grub_module_name_##name[] \
 __attribute__((section(GRUB_MOD_SECTION(modname)), __used__)) = #name
129
#else
130 131 132 133 134 135 136 137 138 139 140 141
#ifdef __APPLE__
.macro GRUB_MOD_LICENSE
  .section GRUB_MOD_SECTION(module_license)
  .ascii "LICENSE="
  .ascii $0
  .byte 0
.endm
#else
.macro GRUB_MOD_LICENSE license
  .section GRUB_MOD_SECTION(module_license), "a"
  .ascii "LICENSE="
  .ascii "\license"
142
  .byte 0
143 144
.endm
#endif
145
#endif
146 147 148 149 150 151 152

/* Under GPL license obligations you have to distribute your module
   under GPLv3(+). However, you can also distribute the same code under
   another license as long as GPLv3(+) version is provided.
*/
#define GRUB_MOD_DUAL_LICENSE(x)

153 154
#ifndef ASM_FILE

155
struct grub_dl_segment
okuji's avatar
okuji committed
156
{
157
  struct grub_dl_segment *next;
okuji's avatar
okuji committed
158
  void *addr;
159
  grub_size_t size;
okuji's avatar
okuji committed
160 161
  unsigned section;
};
162
typedef struct grub_dl_segment *grub_dl_segment_t;
okuji's avatar
okuji committed
163

164
struct grub_dl;
okuji's avatar
okuji committed
165

166
struct grub_dl_dep
okuji's avatar
okuji committed
167
{
168 169
  struct grub_dl_dep *next;
  struct grub_dl *mod;
okuji's avatar
okuji committed
170
};
171
typedef struct grub_dl_dep *grub_dl_dep_t;
okuji's avatar
okuji committed
172

173
#ifndef GRUB_UTIL
174
struct grub_dl
okuji's avatar
okuji committed
175 176 177
{
  char *name;
  int ref_count;
178 179
  grub_dl_dep_t dep;
  grub_dl_segment_t segment;
180
  Elf_Sym *symtab;
181
  grub_size_t symsize;
182
  void (*init) (struct grub_dl *mod);
okuji's avatar
okuji committed
183
  void (*fini) (void);
184
#if !defined (__i386__) && !defined (__x86_64__)
185
  void *got;
186
  void *gotptr;
187
  void *tramp;
188 189 190 191
  void *trampptr;
#endif
#ifdef __mips__
  grub_uint32_t *reginfo;
192
#endif
193
  void *base;
194
  grub_size_t sz;
195
  struct grub_dl *next;
okuji's avatar
okuji committed
196
};
197
#endif
198
typedef struct grub_dl *grub_dl_t;
okuji's avatar
okuji committed
199

200
grub_dl_t grub_dl_load_file (const char *filename);
201 202
grub_dl_t EXPORT_FUNC(grub_dl_load) (const char *name);
grub_dl_t grub_dl_load_core (void *addr, grub_size_t size);
203
grub_dl_t EXPORT_FUNC(grub_dl_load_core_noinit) (void *addr, grub_size_t size);
204 205 206 207
int EXPORT_FUNC(grub_dl_unload) (grub_dl_t mod);
void grub_dl_unload_unneeded (void);
int EXPORT_FUNC(grub_dl_ref) (grub_dl_t mod);
int EXPORT_FUNC(grub_dl_unref) (grub_dl_t mod);
208 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 216 217 218 219 220
#ifdef GRUB_MACHINE_EMU
void *
grub_osdep_dl_memalign (grub_size_t align, grub_size_t size);
void
grub_dl_osdep_dl_free (void *ptr);
#endif

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
static inline void
grub_dl_init (grub_dl_t mod)
{
  if (mod->init)
    (mod->init) (mod);

  mod->next = grub_dl_head;
  grub_dl_head = mod;
}

static inline grub_dl_t
grub_dl_get (const char *name)
{
  grub_dl_t l;

  FOR_DL_MODULES(l)
    if (grub_strcmp (name, l->name) == 0)
      return l;

  return 0;
}

#endif

BVK Chaitanya's avatar
BVK Chaitanya committed
245
grub_err_t grub_dl_register_symbol (const char *name, void *addr,
246
				    int isfunc, grub_dl_t mod);
okuji's avatar
okuji committed
247

248
grub_err_t grub_arch_dl_check_header (void *ehdr);
249 250 251 252 253
#ifndef GRUB_UTIL
grub_err_t
grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
			       Elf_Shdr *s, grub_dl_segment_t seg);
#endif
okuji's avatar
okuji committed
254

BVK Chaitanya's avatar
BVK Chaitanya committed
255
#if defined (_mips)
256 257 258 259
#define GRUB_LINKER_HAVE_INIT 1
void grub_arch_dl_init_linker (void);
#endif

260 261 262
#define GRUB_IA64_DL_TRAMP_ALIGN 16
#define GRUB_IA64_DL_GOT_ALIGN 16

263
grub_err_t
264 265
grub_ia64_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
				 grub_size_t *got);
266

267
#if defined (__ia64__)
268 269
#define GRUB_ARCH_DL_TRAMP_ALIGN GRUB_IA64_DL_TRAMP_ALIGN
#define GRUB_ARCH_DL_GOT_ALIGN GRUB_IA64_DL_GOT_ALIGN
270
#define grub_arch_dl_get_tramp_got_size grub_ia64_dl_get_tramp_got_size
271
#else
272
grub_err_t
273 274 275 276
grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
				 grub_size_t *got);
#endif

277
#if defined (__powerpc__) || defined (__mips__) || defined (__arm__)
278 279
#define GRUB_ARCH_DL_TRAMP_ALIGN 4
#define GRUB_ARCH_DL_GOT_ALIGN 4
280 281
#endif

282
#if defined (__aarch64__) || defined (__sparc__)
283 284 285 286
#define GRUB_ARCH_DL_TRAMP_ALIGN 8
#define GRUB_ARCH_DL_GOT_ALIGN 8
#endif

287 288
#endif

289
#endif /* ! GRUB_DL_H */