2002-12-27 08:53:07 +00:00
|
|
|
/* dl-386.c - arch-dependent part of loadable module support */
|
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-01-03 22:05:07 +00:00
|
|
|
* Copyright (C) 2002,2005,2007,2009 Free Software Foundation, Inc.
|
2002-12-27 08:53:07 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2002-12-27 08:53:07 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2002-12-27 08:53:07 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2002-12-27 08:53:07 +00:00
|
|
|
* 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
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2002-12-27 08:53:07 +00:00
|
|
|
*/
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/elf.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/err.h>
|
2012-02-08 18:26:01 +00:00
|
|
|
#include <grub/i18n.h>
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
/* Check if EHDR is a valid ELF header. */
|
2005-02-14 18:41:33 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_arch_dl_check_header (void *ehdr)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2009-07-01 14:49:22 +00:00
|
|
|
Elf_Ehdr *e = ehdr;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
/* Check the magic numbers. */
|
2005-02-14 18:41:33 +00:00
|
|
|
if (e->e_ident[EI_CLASS] != ELFCLASS32
|
2002-12-27 08:53:07 +00:00
|
|
|
|| e->e_ident[EI_DATA] != ELFDATA2LSB
|
2005-02-14 18:41:33 +00:00
|
|
|
|| e->e_machine != EM_386)
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch dependent ELF magic"));
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2005-02-14 18:41:33 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Relocate symbols. */
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2009-07-01 14:49:22 +00:00
|
|
|
Elf_Ehdr *e = ehdr;
|
|
|
|
Elf_Shdr *s;
|
|
|
|
Elf_Word entsize;
|
2002-12-27 08:53:07 +00:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
/* Find a symbol table. */
|
2009-07-01 14:49:22 +00:00
|
|
|
for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
|
2002-12-27 08:53:07 +00:00
|
|
|
i < e->e_shnum;
|
2009-07-01 14:49:22 +00:00
|
|
|
i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
|
2002-12-27 08:53:07 +00:00
|
|
|
if (s->sh_type == SHT_SYMTAB)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i == e->e_shnum)
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_MODULE, N_("no symbol table"));
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
entsize = s->sh_entsize;
|
|
|
|
|
2009-07-01 14:49:22 +00:00
|
|
|
for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
|
2002-12-27 08:53:07 +00:00
|
|
|
i < e->e_shnum;
|
2009-07-01 14:49:22 +00:00
|
|
|
i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
|
2002-12-27 08:53:07 +00:00
|
|
|
if (s->sh_type == SHT_REL)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_dl_segment_t seg;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
/* Find the target segment. */
|
|
|
|
for (seg = mod->segment; seg; seg = seg->next)
|
|
|
|
if (seg->section == s->sh_info)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (seg)
|
|
|
|
{
|
2009-07-01 14:49:22 +00:00
|
|
|
Elf_Rel *rel, *max;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-07-01 14:49:22 +00:00
|
|
|
for (rel = (Elf_Rel *) ((char *) e + s->sh_offset),
|
2002-12-27 08:53:07 +00:00
|
|
|
max = rel + s->sh_size / s->sh_entsize;
|
|
|
|
rel < max;
|
|
|
|
rel++)
|
|
|
|
{
|
2009-07-01 14:49:22 +00:00
|
|
|
Elf_Word *addr;
|
|
|
|
Elf_Sym *sym;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
if (seg->size < rel->r_offset)
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_MODULE,
|
2002-12-27 08:53:07 +00:00
|
|
|
"reloc offset is out of the segment");
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-07-01 14:49:22 +00:00
|
|
|
addr = (Elf_Word *) ((char *) seg->addr + rel->r_offset);
|
|
|
|
sym = (Elf_Sym *) ((char *) mod->symtab
|
|
|
|
+ entsize * ELF_R_SYM (rel->r_info));
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-07-01 14:49:22 +00:00
|
|
|
switch (ELF_R_TYPE (rel->r_info))
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
case R_386_32:
|
|
|
|
*addr += sym->st_value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R_386_PC32:
|
2009-07-01 14:49:22 +00:00
|
|
|
*addr += (sym->st_value - (Elf_Word) seg->addr
|
2002-12-27 08:53:07 +00:00
|
|
|
- rel->r_offset);
|
|
|
|
break;
|
2012-02-08 18:26:01 +00:00
|
|
|
default:
|
|
|
|
return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
|
|
|
|
N_("relocation 0x%x is not implemented yet"),
|
|
|
|
ELF_R_TYPE (rel->r_info));
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|