dl.c 18.5 KB
Newer Older
okuji's avatar
okuji committed
1 2
/* dl.c - loadable module support */
/*
3
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2002,2003,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 22
/* Force native word size */
#define GRUB_TARGET_WORDSIZE (8 * GRUB_CPU_SIZEOF_VOID_P)

okuji's avatar
okuji committed
23
#include <config.h>
24 25 26 27 28 29 30 31 32
#include <grub/elf.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/types.h>
#include <grub/symbol.h>
#include <grub/file.h>
#include <grub/env.h>
33
#include <grub/cache.h>
34
#include <grub/i18n.h>
35 36 37 38 39

/* Platforms where modules are in a readonly area of memory.  */
#if defined(GRUB_MACHINE_QEMU)
#define GRUB_MODULES_MACHINE_READONLY
#endif
40

okuji's avatar
okuji committed
41 42


43 44
#pragma GCC diagnostic ignored "-Wcast-align"

45
grub_dl_t grub_dl_head = 0;
okuji's avatar
okuji committed
46

47 48 49
grub_err_t
grub_dl_add (grub_dl_t mod);

50
/* Keep global so that GDB scripts work.  */
51
grub_err_t
52
grub_dl_add (grub_dl_t mod)
okuji's avatar
okuji committed
53
{
54 55
  if (grub_dl_get (mod->name))
    return grub_error (GRUB_ERR_BAD_MODULE,
okuji's avatar
okuji committed
56
		       "`%s' is already loaded", mod->name);
57

58
  return GRUB_ERR_NONE;
okuji's avatar
okuji committed
59 60 61
}

static void
62
grub_dl_remove (grub_dl_t mod)
okuji's avatar
okuji committed
63
{
64
  grub_dl_t *p, q;
okuji's avatar
okuji committed
65

66
  for (p = &grub_dl_head, q = *p; q; p = &q->next, q = *p)
67
    if (q == mod)
okuji's avatar
okuji committed
68 69 70 71 72 73 74 75
      {
	*p = q->next;
	return;
      }
}



76
struct grub_symbol
okuji's avatar
okuji committed
77
{
78
  struct grub_symbol *next;
okuji's avatar
okuji committed
79 80
  const char *name;
  void *addr;
81
  int isfunc;
82
  grub_dl_t mod;	/* The module to which this symbol belongs.  */
okuji's avatar
okuji committed
83
};
84
typedef struct grub_symbol *grub_symbol_t;
okuji's avatar
okuji committed
85 86

/* The size of the symbol table.  */
87
#define GRUB_SYMTAB_SIZE	509
okuji's avatar
okuji committed
88 89

/* The symbol table (using an open-hash).  */
90
static struct grub_symbol *grub_symtab[GRUB_SYMTAB_SIZE];
okuji's avatar
okuji committed
91 92 93

/* Simple hash function.  */
static unsigned
94
grub_symbol_hash (const char *s)
okuji's avatar
okuji committed
95 96 97 98 99 100
{
  unsigned key = 0;

  while (*s)
    key = key * 65599 + *s++;

101
  return (key + (key >> 5)) % GRUB_SYMTAB_SIZE;
okuji's avatar
okuji committed
102 103 104 105
}

/* Resolve the symbol name NAME and return the address.
   Return NULL, if not found.  */
106
static grub_symbol_t
107
grub_dl_resolve_symbol (const char *name)
okuji's avatar
okuji committed
108
{
109
  grub_symbol_t sym;
okuji's avatar
okuji committed
110

111 112
  for (sym = grub_symtab[grub_symbol_hash (name)]; sym; sym = sym->next)
    if (grub_strcmp (sym->name, name) == 0)
113
      return sym;
okuji's avatar
okuji committed
114 115 116 117 118

  return 0;
}

/* Register a symbol with the name NAME and the address ADDR.  */
119
grub_err_t
120 121
grub_dl_register_symbol (const char *name, void *addr, int isfunc,
			 grub_dl_t mod)
okuji's avatar
okuji committed
122
{
123
  grub_symbol_t sym;
okuji's avatar
okuji committed
124
  unsigned k;
125

126
  sym = (grub_symbol_t) grub_malloc (sizeof (*sym));
okuji's avatar
okuji committed
127
  if (! sym)
128
    return grub_errno;
okuji's avatar
okuji committed
129 130 131

  if (mod)
    {
132
      sym->name = grub_strdup (name);
okuji's avatar
okuji committed
133 134
      if (! sym->name)
	{
135 136
	  grub_free (sym);
	  return grub_errno;
okuji's avatar
okuji committed
137 138 139 140
	}
    }
  else
    sym->name = name;
141

okuji's avatar
okuji committed
142 143
  sym->addr = addr;
  sym->mod = mod;
144
  sym->isfunc = isfunc;
145

146 147 148
  k = grub_symbol_hash (name);
  sym->next = grub_symtab[k];
  grub_symtab[k] = sym;
okuji's avatar
okuji committed
149

150
  return GRUB_ERR_NONE;
okuji's avatar
okuji committed
151 152 153 154
}

/* Unregister all the symbols defined in the module MOD.  */
static void
155
grub_dl_unregister_symbols (grub_dl_t mod)
okuji's avatar
okuji committed
156 157 158 159
{
  unsigned i;

  if (! mod)
160
    grub_fatal ("core symbols cannot be unregistered");
161

162
  for (i = 0; i < GRUB_SYMTAB_SIZE; i++)
okuji's avatar
okuji committed
163
    {
164
      grub_symbol_t sym, *p, q;
okuji's avatar
okuji committed
165

166
      for (p = &grub_symtab[i], sym = *p; sym; sym = q)
okuji's avatar
okuji committed
167 168 169 170 171
	{
	  q = sym->next;
	  if (sym->mod == mod)
	    {
	      *p = q;
172 173
	      grub_free ((void *) sym->name);
	      grub_free (sym);
okuji's avatar
okuji committed
174 175 176 177 178 179 180 181 182
	    }
	  else
	    p = &sym->next;
	}
    }
}

/* Return the address of a section whose index is N.  */
static void *
183
grub_dl_get_section_addr (grub_dl_t mod, unsigned n)
okuji's avatar
okuji committed
184
{
185
  grub_dl_segment_t seg;
okuji's avatar
okuji committed
186 187 188 189 190 191 192 193

  for (seg = mod->segment; seg; seg = seg->next)
    if (seg->section == n)
      return seg->addr;

  return 0;
}

194
/* Check if EHDR is a valid ELF header.  */
195
static grub_err_t
196 197 198
grub_dl_check_header (void *ehdr, grub_size_t size)
{
  Elf_Ehdr *e = ehdr;
199
  grub_err_t err;
200 201 202 203 204 205

  /* Check the header size.  */
  if (size < sizeof (Elf_Ehdr))
    return grub_error (GRUB_ERR_BAD_OS, "ELF header smaller than expected");

  /* Check the magic numbers.  */
206
  if (e->e_ident[EI_MAG0] != ELFMAG0
207 208 209 210 211
      || e->e_ident[EI_MAG1] != ELFMAG1
      || e->e_ident[EI_MAG2] != ELFMAG2
      || e->e_ident[EI_MAG3] != ELFMAG3
      || e->e_ident[EI_VERSION] != EV_CURRENT
      || e->e_version != EV_CURRENT)
212
    return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
213 214 215 216

  err = grub_arch_dl_check_header (ehdr);
  if (err)
    return err;
217 218 219 220

  return GRUB_ERR_NONE;
}

okuji's avatar
okuji committed
221
/* Load all segments from memory specified by E.  */
222 223
static grub_err_t
grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
okuji's avatar
okuji committed
224 225
{
  unsigned i;
226
  const Elf_Shdr *s;
227
  grub_size_t tsize = 0, talign = 1;
228
#if !defined (__i386__) && !defined (__x86_64__)
229 230
  grub_size_t tramp;
  grub_size_t got;
231
  grub_err_t err;
232 233 234
#endif
  char *ptr;

235
  for (i = 0, s = (const Elf_Shdr *)((const char *) e + e->e_shoff);
236
       i < e->e_shnum;
237
       i++, s = (const Elf_Shdr *)((const char *) s + e->e_shentsize))
238
    {
239
      tsize = ALIGN_UP (tsize, s->sh_addralign) + s->sh_size;
240 241 242 243
      if (talign < s->sh_addralign)
	talign = s->sh_addralign;
    }

244
#if !defined (__i386__) && !defined (__x86_64__)
245 246 247
  err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
  if (err)
    return err;
248 249 250 251 252 253 254 255 256
  tsize += ALIGN_UP (tramp, GRUB_ARCH_DL_TRAMP_ALIGN);
  if (talign < GRUB_ARCH_DL_TRAMP_ALIGN)
    talign = GRUB_ARCH_DL_TRAMP_ALIGN;
  tsize += ALIGN_UP (got, GRUB_ARCH_DL_GOT_ALIGN);
  if (talign < GRUB_ARCH_DL_GOT_ALIGN)
    talign = GRUB_ARCH_DL_GOT_ALIGN;
#endif

#ifdef GRUB_MACHINE_EMU
257 258
  mod->base = grub_osdep_dl_memalign (talign, tsize);
#else
259
  mod->base = grub_memalign (talign, tsize);
260
#endif
261 262
  if (!mod->base)
    return grub_errno;
263
  mod->sz = tsize;
264 265
  ptr = mod->base;

okuji's avatar
okuji committed
266 267 268 269 270 271
  for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff);
       i < e->e_shnum;
       i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize))
    {
      if (s->sh_flags & SHF_ALLOC)
	{
272
	  grub_dl_segment_t seg;
okuji's avatar
okuji committed
273

274
	  seg = (grub_dl_segment_t) grub_malloc (sizeof (*seg));
okuji's avatar
okuji committed
275
	  if (! seg)
276
	    return grub_errno;
277

okuji's avatar
okuji committed
278 279 280 281
	  if (s->sh_size)
	    {
	      void *addr;

282 283 284
	      ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, s->sh_addralign);
	      addr = ptr;
	      ptr += s->sh_size;
okuji's avatar
okuji committed
285 286 287 288

	      switch (s->sh_type)
		{
		case SHT_PROGBITS:
289
		  grub_memcpy (addr, (char *) e + s->sh_offset, s->sh_size);
okuji's avatar
okuji committed
290 291
		  break;
		case SHT_NOBITS:
292
		  grub_memset (addr, 0, s->sh_size);
okuji's avatar
okuji committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306
		  break;
		}

	      seg->addr = addr;
	    }
	  else
	    seg->addr = 0;

	  seg->size = s->sh_size;
	  seg->section = i;
	  seg->next = mod->segment;
	  mod->segment = seg;
	}
    }
307
#if !defined (__i386__) && !defined (__x86_64__)
308 309
  ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
  mod->tramp = ptr;
310
  mod->trampptr = ptr;
311 312 313
  ptr += tramp;
  ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_GOT_ALIGN);
  mod->got = ptr;
314
  mod->gotptr = ptr;
315 316
  ptr += got;
#endif
okuji's avatar
okuji committed
317

318
  return GRUB_ERR_NONE;
okuji's avatar
okuji committed
319 320
}

321 322
static grub_err_t
grub_dl_resolve_symbols (grub_dl_t mod, Elf_Ehdr *e)
okuji's avatar
okuji committed
323 324 325 326 327 328
{
  unsigned i;
  Elf_Shdr *s;
  Elf_Sym *sym;
  const char *str;
  Elf_Word size, entsize;
329

okuji's avatar
okuji committed
330 331 332 333 334 335
  for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
       i < e->e_shnum;
       i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
    if (s->sh_type == SHT_SYMTAB)
      break;

336 337 338
  /* Module without symbol table may still be used to pull in dependencies.
     We verify at build time that such modules do not contain any relocations
     that may reference symbol table. */
okuji's avatar
okuji committed
339
  if (i == e->e_shnum)
340
    return GRUB_ERR_NONE;
okuji's avatar
okuji committed
341

342 343
#ifdef GRUB_MODULES_MACHINE_READONLY
  mod->symtab = grub_malloc (s->sh_size);
344 345
  if (!mod->symtab)
    return grub_errno;
346
  grub_memcpy (mod->symtab, (char *) e + s->sh_offset, s->sh_size);
347 348 349
#else
  mod->symtab = (Elf_Sym *) ((char *) e + s->sh_offset);
#endif
350
  mod->symsize = s->sh_entsize;
351
  sym = mod->symtab;
okuji's avatar
okuji committed
352 353
  size = s->sh_size;
  entsize = s->sh_entsize;
354

okuji's avatar
okuji committed
355 356 357 358 359 360 361 362 363 364
  s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shentsize * s->sh_link);
  str = (char *) e + s->sh_offset;

  for (i = 0;
       i < size / entsize;
       i++, sym = (Elf_Sym *) ((char *) sym + entsize))
    {
      unsigned char type = ELF_ST_TYPE (sym->st_info);
      unsigned char bind = ELF_ST_BIND (sym->st_info);
      const char *name = str + sym->st_name;
365

okuji's avatar
okuji committed
366 367 368
      switch (type)
	{
	case STT_NOTYPE:
369
	case STT_OBJECT:
okuji's avatar
okuji committed
370 371 372
	  /* Resolve a global symbol.  */
	  if (sym->st_name != 0 && sym->st_shndx == 0)
	    {
373 374
	      grub_symbol_t nsym = grub_dl_resolve_symbol (name);
	      if (! nsym)
375
		return grub_error (GRUB_ERR_BAD_MODULE,
376
				   N_("symbol `%s' not found"), name);
377 378 379
	      sym->st_value = (Elf_Addr) nsym->addr;
	      if (nsym->isfunc)
		sym->st_info = ELF_ST_INFO (bind, STT_FUNC);
okuji's avatar
okuji committed
380 381
	    }
	  else
382 383 384 385
	    {
	      sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
								    sym->st_shndx);
	      if (bind != STB_LOCAL)
386
		if (grub_dl_register_symbol (name, (void *) sym->st_value, 0, mod))
387 388
		  return grub_errno;
	    }
okuji's avatar
okuji committed
389 390 391
	  break;

	case STT_FUNC:
392
	  sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
okuji's avatar
okuji committed
393
								sym->st_shndx);
394
#ifdef __ia64__
395
	  {
396 397 398 399 400 401
	      /* FIXME: free descriptor once it's not used anymore. */
	      char **desc;
	      desc = grub_malloc (2 * sizeof (char *));
	      if (!desc)
		return grub_errno;
	      desc[0] = (void *) sym->st_value;
402
	      desc[1] = mod->base;
403 404 405
	      sym->st_value = (grub_addr_t) desc;
	  }
#endif
okuji's avatar
okuji committed
406
	  if (bind != STB_LOCAL)
407
	    if (grub_dl_register_symbol (name, (void *) sym->st_value, 1, mod))
408 409 410 411
	      return grub_errno;
	  if (grub_strcmp (name, "grub_mod_init") == 0)
	    mod->init = (void (*) (grub_dl_t)) sym->st_value;
	  else if (grub_strcmp (name, "grub_mod_fini") == 0)
412
	    mod->fini = (void (*) (void)) sym->st_value;
okuji's avatar
okuji committed
413 414 415
	  break;

	case STT_SECTION:
416
	  sym->st_value = (Elf_Addr) grub_dl_get_section_addr (mod,
okuji's avatar
okuji committed
417 418 419 420 421 422 423 424
							       sym->st_shndx);
	  break;

	case STT_FILE:
	  sym->st_value = 0;
	  break;

	default:
425
	  return grub_error (GRUB_ERR_BAD_MODULE,
okuji's avatar
okuji committed
426 427 428 429
			     "unknown symbol type `%d'", (int) type);
	}
    }

430
  return GRUB_ERR_NONE;
okuji's avatar
okuji committed
431 432
}

433 434
static Elf_Shdr *
grub_dl_find_section (Elf_Ehdr *e, const char *name)
435 436 437 438 439 440 441 442 443 444 445
{
  Elf_Shdr *s;
  const char *str;
  unsigned i;

  s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shstrndx * e->e_shentsize);
  str = (char *) e + s->sh_offset;

  for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
       i < e->e_shnum;
       i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
446 447 448 449
    if (grub_strcmp (str + s->sh_name, name) == 0)
      return s;
  return NULL;
}
450

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
/* 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.
*/
static grub_err_t
grub_dl_check_license (Elf_Ehdr *e)
{
  Elf_Shdr *s = grub_dl_find_section (e, ".module_license");
  if (s && (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
	    || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
	    || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0))
    return GRUB_ERR_NONE;
467 468 469
  return grub_error (GRUB_ERR_BAD_MODULE, "incompatible license");
}

470 471
static grub_err_t
grub_dl_resolve_name (grub_dl_t mod, Elf_Ehdr *e)
okuji's avatar
okuji committed
472 473 474
{
  Elf_Shdr *s;

475 476
  s = grub_dl_find_section (e, ".modname");
  if (!s)
477
    return grub_error (GRUB_ERR_BAD_MODULE, "no module name found");
478 479 480 481
  
  mod->name = grub_strdup ((char *) e + s->sh_offset);
  if (! mod->name)
    return grub_errno;
okuji's avatar
okuji committed
482

483
  return GRUB_ERR_NONE;
okuji's avatar
okuji committed
484 485
}

486 487
static grub_err_t
grub_dl_resolve_dependencies (grub_dl_t mod, Elf_Ehdr *e)
okuji's avatar
okuji committed
488 489 490
{
  Elf_Shdr *s;

491
  s = grub_dl_find_section (e, ".moddeps");
492

493 494
  if (!s)
    return GRUB_ERR_NONE;
okuji's avatar
okuji committed
495

496 497
  const char *name = (char *) e + s->sh_offset;
  const char *max = name + s->sh_size;
498

499 500 501 502
  while ((name < max) && (*name))
    {
      grub_dl_t m;
      grub_dl_dep_t dep;
503

504 505 506
      m = grub_dl_load (name);
      if (! m)
	return grub_errno;
507

508
      grub_dl_ref (m);
509

510 511 512
      dep = (grub_dl_dep_t) grub_malloc (sizeof (*dep));
      if (! dep)
	return grub_errno;
513

514 515 516 517 518 519
      dep->mod = m;
      dep->next = mod->dep;
      mod->dep = dep;

      name += grub_strlen (name) + 1;
    }
okuji's avatar
okuji committed
520

521
  return GRUB_ERR_NONE;
okuji's avatar
okuji committed
522 523
}

524
int
525
grub_dl_ref (grub_dl_t mod)
526
{
527
  grub_dl_dep_t dep;
528

529 530 531
  if (!mod)
    return 0;

532
  for (dep = mod->dep; dep; dep = dep->next)
533
    grub_dl_ref (dep->mod);
534

535 536 537 538
  return ++mod->ref_count;
}

int
539
grub_dl_unref (grub_dl_t mod)
540
{
541
  grub_dl_dep_t dep;
542

543 544 545
  if (!mod)
    return 0;

546
  for (dep = mod->dep; dep; dep = dep->next)
547
    grub_dl_unref (dep->mod);
548

549
  return --mod->ref_count;
550 551
}

552 553 554
static void
grub_dl_flush_cache (grub_dl_t mod)
{
555 556 557
  grub_dprintf ("modules", "flushing 0x%lx bytes at %p\n",
		(unsigned long) mod->sz, mod->base);
  grub_arch_sync_caches (mod->base, mod->sz);
558 559
}

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
static grub_err_t
grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
{
  Elf_Ehdr *e = ehdr;
  Elf_Shdr *s;
  unsigned i;

  for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
       i < e->e_shnum;
       i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
    if (s->sh_type == SHT_REL || s->sh_type == SHT_RELA)
      {
	grub_dl_segment_t seg;
	grub_err_t err;

	/* Find the target segment.  */
	for (seg = mod->segment; seg; seg = seg->next)
	  if (seg->section == s->sh_info)
	    break;

	if (seg)
	  {
582 583 584
	    if (!mod->symtab)
	      return grub_error (GRUB_ERR_BAD_MODULE, "relocation without symbol table");

585 586 587 588 589 590 591 592 593
	    err = grub_arch_dl_relocate_symbols (mod, ehdr, s, seg);
	    if (err)
	      return err;
	  }
      }

  return GRUB_ERR_NONE;
}

okuji's avatar
okuji committed
594
/* Load a module from core memory.  */
595
grub_dl_t
596
grub_dl_load_core_noinit (void *addr, grub_size_t size)
okuji's avatar
okuji committed
597 598
{
  Elf_Ehdr *e;
599
  grub_dl_t mod;
600

601 602
  grub_dprintf ("modules", "module at %p, size 0x%lx\n", addr,
		(unsigned long) size);
okuji's avatar
okuji committed
603
  e = addr;
604 605
  if (grub_dl_check_header (e, size))
    return 0;
606

607
  if (e->e_type != ET_REL)
okuji's avatar
okuji committed
608
    {
609
      grub_error (GRUB_ERR_BAD_MODULE, N_("this ELF file is not of the right type"));
okuji's avatar
okuji committed
610 611
      return 0;
    }
612 613

  /* Make sure that every section is within the core.  */
614
  if (size < e->e_shoff + (grub_uint32_t) e->e_shentsize * e->e_shnum)
615 616 617 618 619
    {
      grub_error (GRUB_ERR_BAD_OS, "ELF sections outside core");
      return 0;
    }

620
  mod = (grub_dl_t) grub_zalloc (sizeof (*mod));
okuji's avatar
okuji committed
621 622 623 624 625
  if (! mod)
    return 0;

  mod->ref_count = 1;

626
  grub_dprintf ("modules", "relocating to %p\n", mod);
627 628 629 630 631 632 633 634 635 636
  /* 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.
  */
  if (grub_dl_check_license (e)
      || grub_dl_resolve_name (mod, e)
637 638 639
      || grub_dl_resolve_dependencies (mod, e)
      || grub_dl_load_segments (mod, e)
      || grub_dl_resolve_symbols (mod, e)
640
      || grub_dl_relocate_symbols (mod, e))
okuji's avatar
okuji committed
641 642
    {
      mod->fini = 0;
643
      grub_dl_unload (mod);
okuji's avatar
okuji committed
644 645 646
      return 0;
    }

647
  grub_dl_flush_cache (mod);
648 649 650

  grub_dprintf ("modules", "module name: %s\n", mod->name);
  grub_dprintf ("modules", "init function: %p\n", mod->init);
651

652
  if (grub_dl_add (mod))
okuji's avatar
okuji committed
653
    {
654
      grub_dl_unload (mod);
okuji's avatar
okuji committed
655 656
      return 0;
    }
657

okuji's avatar
okuji committed
658 659 660
  return mod;
}

661 662 663 664 665
grub_dl_t
grub_dl_load_core (void *addr, grub_size_t size)
{
  grub_dl_t mod;

666 667
  grub_boot_time ("Parsing module");

668 669 670 671 672 673 674 675 676 677 678 679
  mod = grub_dl_load_core_noinit (addr, size);

  if (!mod)
    return NULL;

  grub_boot_time ("Initing module %s", mod->name);
  grub_dl_init (mod);
  grub_boot_time ("Module %s inited", mod->name);

  return mod;
}

okuji's avatar
okuji committed
680
/* Load a module from the file FILENAME.  */
681 682
grub_dl_t
grub_dl_load_file (const char *filename)
okuji's avatar
okuji committed
683
{
684
  grub_file_t file = NULL;
685
  grub_ssize_t size;
okuji's avatar
okuji committed
686
  void *core = 0;
687
  grub_dl_t mod = 0;
688

689 690
  grub_boot_time ("Loading module %s", filename);

691
  file = grub_file_open (filename);
okuji's avatar
okuji committed
692 693 694
  if (! file)
    return 0;

695 696
  size = grub_file_size (file);
  core = grub_malloc (size);
okuji's avatar
okuji committed
697
  if (! core)
698 699 700 701
    {
      grub_file_close (file);
      return 0;
    }
okuji's avatar
okuji committed
702

703
  if (grub_file_read (file, core, size) != (int) size)
704 705 706 707 708 709 710 711 712 713
    {
      grub_file_close (file);
      grub_free (core);
      return 0;
    }

  /* We must close this before we try to process dependencies.
     Some disk backends do not handle gracefully multiple concurrent
     opens of the same device.  */
  grub_file_close (file);
okuji's avatar
okuji committed
714

715
  mod = grub_dl_load_core (core, size);
716
  grub_free (core);
717
  if (! mod)
718
    return 0;
719

720
  mod->ref_count--;
okuji's avatar
okuji committed
721 722 723 724
  return mod;
}

/* Load a module using a symbolic name.  */
725 726
grub_dl_t
grub_dl_load (const char *name)
okuji's avatar
okuji committed
727 728
{
  char *filename;
729
  grub_dl_t mod;
730
  const char *grub_dl_dir = grub_env_get ("prefix");
okuji's avatar
okuji committed
731

732
  mod = grub_dl_get (name);
okuji's avatar
okuji committed
733
  if (mod)
734
    return mod;
735

736 737 738
  if (grub_no_modules)
    return 0;

739
  if (! grub_dl_dir) {
740
    grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "prefix");
741 742
    return 0;
  }
okuji's avatar
okuji committed
743

744 745
  filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM "/%s.mod",
			     grub_dl_dir, name);
okuji's avatar
okuji committed
746 747
  if (! filename)
    return 0;
748

749 750
  mod = grub_dl_load_file (filename);
  grub_free (filename);
okuji's avatar
okuji committed
751 752 753

  if (! mod)
    return 0;
754

755 756
  if (grub_strcmp (mod->name, name) != 0)
    grub_error (GRUB_ERR_BAD_MODULE, "mismatched names");
757

okuji's avatar
okuji committed
758 759 760 761
  return mod;
}

/* Unload the module MOD.  */
762
int
763
grub_dl_unload (grub_dl_t mod)
okuji's avatar
okuji committed
764
{
765
  grub_dl_dep_t dep, depn;
okuji's avatar
okuji committed
766

767 768
  if (mod->ref_count > 0)
    return 0;
okuji's avatar
okuji committed
769 770 771

  if (mod->fini)
    (mod->fini) ();
772

773 774
  grub_dl_remove (mod);
  grub_dl_unregister_symbols (mod);
775

okuji's avatar
okuji committed
776 777 778
  for (dep = mod->dep; dep; dep = depn)
    {
      depn = dep->next;
779

780
      grub_dl_unload (dep->mod);
781

782
      grub_free (dep);
okuji's avatar
okuji committed
783 784
    }

785 786 787
#ifdef GRUB_MACHINE_EMU
  grub_dl_osdep_dl_free (mod->base);
#else
788
  grub_free (mod->base);
789
#endif
790
  grub_free (mod->name);
791 792 793
#ifdef GRUB_MODULES_MACHINE_READONLY
  grub_free (mod->symtab);
#endif
794
  grub_free (mod);
795
  return 1;
okuji's avatar
okuji committed
796 797
}

798
/* Unload unneeded modules.  */
okuji's avatar
okuji committed
799
void
800
grub_dl_unload_unneeded (void)
801
{
802
  /* Because grub_dl_remove modifies the list of modules, this
803
     implementation is tricky.  */
804
  grub_dl_t p = grub_dl_head;
805

806 807
  while (p)
    {
808
      if (grub_dl_unload (p))
809
	{
810
	  p = grub_dl_head;
811 812 813 814 815 816
	  continue;
	}

      p = p->next;
    }
}