1 |
/* dl.c - loadable module support */ |
/* dl.c - loadable module support */ |
2 |
/* |
/* |
3 |
* GRUB -- GRand Unified Bootloader |
* GRUB -- GRand Unified Bootloader |
4 |
* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. |
* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
5 |
* |
* |
6 |
* GRUB is free software; you can redistribute it and/or modify |
* 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 |
* it under the terms of the GNU General Public License as published by |
238 |
return 0; |
return 0; |
239 |
} |
} |
240 |
|
|
241 |
|
/* Check if EHDR is a valid ELF header. */ |
242 |
|
grub_err_t |
243 |
|
grub_dl_check_header (void *ehdr, grub_size_t size) |
244 |
|
{ |
245 |
|
Elf_Ehdr *e = ehdr; |
246 |
|
|
247 |
|
/* Check the header size. */ |
248 |
|
if (size < sizeof (Elf_Ehdr)) |
249 |
|
return grub_error (GRUB_ERR_BAD_OS, "ELF header smaller than expected"); |
250 |
|
|
251 |
|
/* Check the magic numbers. */ |
252 |
|
if (grub_arch_dl_check_header (ehdr) |
253 |
|
|| e->e_ident[EI_MAG0] != ELFMAG0 |
254 |
|
|| e->e_ident[EI_MAG1] != ELFMAG1 |
255 |
|
|| e->e_ident[EI_MAG2] != ELFMAG2 |
256 |
|
|| e->e_ident[EI_MAG3] != ELFMAG3 |
257 |
|
|| e->e_ident[EI_VERSION] != EV_CURRENT |
258 |
|
|| e->e_version != EV_CURRENT) |
259 |
|
return grub_error (GRUB_ERR_BAD_OS, "invalid arch independent ELF magic"); |
260 |
|
|
261 |
|
return GRUB_ERR_NONE; |
262 |
|
} |
263 |
|
|
264 |
/* Load all segments from memory specified by E. */ |
/* Load all segments from memory specified by E. */ |
265 |
static grub_err_t |
static grub_err_t |
266 |
grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e) |
grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e) |
520 |
grub_dl_t mod; |
grub_dl_t mod; |
521 |
|
|
522 |
e = addr; |
e = addr; |
523 |
if (! grub_arch_dl_check_header (e, size)) |
if (grub_dl_check_header (e, size)) |
524 |
|
return 0; |
525 |
|
|
526 |
|
if (e->e_type != ET_REL) |
527 |
{ |
{ |
528 |
grub_error (GRUB_ERR_BAD_MODULE, "invalid ELF header"); |
grub_error (GRUB_ERR_BAD_MODULE, "invalid ELF file type"); |
529 |
return 0; |
return 0; |
530 |
} |
} |
531 |
|
|
532 |
|
/* Make sure that every section is within the core. */ |
533 |
|
if (size < e->e_shoff + e->e_shentsize * e->e_shnum) |
534 |
|
{ |
535 |
|
grub_error (GRUB_ERR_BAD_OS, "ELF sections outside core"); |
536 |
|
return 0; |
537 |
|
} |
538 |
|
|
539 |
mod = (grub_dl_t) grub_malloc (sizeof (*mod)); |
mod = (grub_dl_t) grub_malloc (sizeof (*mod)); |
540 |
if (! mod) |
if (! mod) |
541 |
return 0; |
return 0; |