2004-12-19 20:35:06 +00:00
|
|
|
/* dl.c - arch-dependent part of loadable module support */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2007-07-21 23:32:33 +00:00
|
|
|
* Copyright (C) 2002,2004,2005,2007 Free Software Foundation, Inc.
|
2004-12-19 20:35:06 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2004-12-19 20:35:06 +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
|
2004-12-19 20:35:06 +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,
|
2004-12-19 20:35:06 +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/>.
|
2004-12-19 20:35:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/elf.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
|
|
|
|
/* 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)
|
2004-12-19 20:35:06 +00:00
|
|
|
{
|
2009-07-01 14:49:22 +00:00
|
|
|
Elf_Ehdr *e = ehdr;
|
2004-12-19 20:35:06 +00:00
|
|
|
|
|
|
|
/* Check the magic numbers. */
|
2005-02-14 18:41:33 +00:00
|
|
|
if (e->e_ident[EI_CLASS] != ELFCLASS32
|
|
|
|
|| e->e_ident[EI_DATA] != ELFDATA2MSB
|
|
|
|
|| e->e_machine != EM_PPC)
|
|
|
|
return grub_error (GRUB_ERR_BAD_OS, "invalid arch specific ELF magic");
|
2004-12-19 20:35:06 +00:00
|
|
|
|
2005-02-14 18:41:33 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2004-12-19 20:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Relocate symbols. */
|
|
|
|
grub_err_t
|
|
|
|
grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
|
|
|
|
{
|
2009-07-01 14:49:22 +00:00
|
|
|
Elf_Ehdr *e = ehdr;
|
|
|
|
Elf_Shdr *s;
|
|
|
|
Elf_Word entsize;
|
2004-12-19 20:35:06 +00:00
|
|
|
unsigned i;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-12-19 20:35:06 +00:00
|
|
|
/* Find a symbol table. */
|
2009-07-01 14:49:22 +00:00
|
|
|
for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
|
2004-12-19 20:35:06 +00:00
|
|
|
i < e->e_shnum;
|
2009-07-01 14:49:22 +00:00
|
|
|
i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
|
2004-12-19 20:35:06 +00:00
|
|
|
if (s->sh_type == SHT_SYMTAB)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i == e->e_shnum)
|
|
|
|
return grub_error (GRUB_ERR_BAD_MODULE, "no symtab found");
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-12-19 20:35:06 +00:00
|
|
|
entsize = s->sh_entsize;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-07-01 14:49:22 +00:00
|
|
|
for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
|
2004-12-19 20:35:06 +00:00
|
|
|
i < e->e_shnum;
|
2009-07-01 14:49:22 +00:00
|
|
|
i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
|
2004-12-19 20:35:06 +00:00
|
|
|
if (s->sh_type == SHT_RELA)
|
|
|
|
{
|
|
|
|
grub_dl_segment_t seg;
|
|
|
|
|
|
|
|
/* 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_Rela *rel, *max;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-07-01 14:49:22 +00:00
|
|
|
for (rel = (Elf_Rela *) ((char *) e + s->sh_offset),
|
2004-12-19 20:35:06 +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;
|
2004-12-19 20:35:06 +00:00
|
|
|
grub_uint32_t value;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-12-19 20:35:06 +00:00
|
|
|
if (seg->size < rel->r_offset)
|
|
|
|
return grub_error (GRUB_ERR_BAD_MODULE,
|
|
|
|
"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
|
|
|
|
2004-12-19 20:35:06 +00:00
|
|
|
/* On the PPC the value does not have an explicit
|
|
|
|
addend, add it. */
|
|
|
|
value = sym->st_value + rel->r_addend;
|
2009-07-01 14:49:22 +00:00
|
|
|
switch (ELF_R_TYPE (rel->r_info))
|
2004-12-19 20:35:06 +00:00
|
|
|
{
|
|
|
|
case R_PPC_ADDR16_LO:
|
2009-07-01 14:49:22 +00:00
|
|
|
*(Elf_Half *) addr = value;
|
2004-12-19 20:35:06 +00:00
|
|
|
break;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-12-19 20:35:06 +00:00
|
|
|
case R_PPC_REL24:
|
|
|
|
{
|
2009-07-01 14:49:22 +00:00
|
|
|
Elf_Sword delta = value - (Elf_Word) addr;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-12-19 20:35:06 +00:00
|
|
|
if (delta << 6 >> 6 != delta)
|
2009-12-24 22:53:05 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_MODULE, "relocation overflow");
|
2004-12-19 20:35:06 +00:00
|
|
|
*addr = (*addr & 0xfc000003) | (delta & 0x3fffffc);
|
|
|
|
break;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-12-19 20:35:06 +00:00
|
|
|
case R_PPC_ADDR16_HA:
|
2009-07-01 14:49:22 +00:00
|
|
|
*(Elf_Half *) addr = (value + 0x8000) >> 16;
|
2004-12-19 20:35:06 +00:00
|
|
|
break;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-12-19 20:35:06 +00:00
|
|
|
case R_PPC_ADDR32:
|
|
|
|
*addr = value;
|
|
|
|
break;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-12-27 Marco Gerards <metgerards@student.han.nl>
* genmk.rb: Handle the `Program' class in the main loop. Written
by Johan Rydberg <jrydberg@gnu.org>.
(Program): New class.
(programs): New variable.
* boot/powerpc/ieee1275/cmain.c: Include <grub/machine/ieee1275.h>
instead of "grub/machine/ieee1275.h". Include <grub/kernel.h>
instead of "grub/kernel.h". Include <grub/machine/init.h>.
(help_arch): Function removed.
* conf/powerpc-ieee1275.rmk (grubof_HEADERS): Add
`powerpc/libgcc.h' and `loader.h'.
(pkgdata_PROGRAMS): New variable.
(sbin_UTILITIES): Variable removed.
(grub_emu_SOURCES): Added kern/powerpc/cache.S.
(grubof_SOURCES): Variable re-defined so it only includes the
core functionality.
(grubof_CFLAGS): Remove `-DGRUBOF'.
(pkgdata_MODULES, fshelp_mod_SOURCES, fshelp_mod_CFLAGS,
(fat_mod_SOURCES, fat_mod_CFLAGS, ext2_mod_SOURCES)
(ext2_mod_CFLAGS, ufs_mod_SOURCES, ufs_mod_CFLAGS)
(minix_mod_SOURCES, minix_mod_CFLAGS, hfs_mod_SOURCES)
(hfs_mod_CFLAGS, jfs_mod_SOURCES, jfs_mod_CFLAGS)
(iso9660_mod_SOURCES, iso9660_mod_CFLAGS, _linux_mod_SOURCES)
(_linux_mod_CFLAGS, linux_mod_SOURCES, linux_mod_CFLAGS)
(normal_mod_SOURCES, normal_mod_CFLAGS, normal_mod_ASFLAGS)
(hello_mod_SOURCES, hello_mod_CFLAGS, boot_mod_SOURCES)
(boot_mod_CFLAGS, terminal_mod_SOURCES, terminal_mod_CFLAGS)
(ls_mod_SOURCES, ls_mod_CFLAGS, cmp_mod_SOURCES, cmp_mod_CFLAGS)
(cat_mod_SOURCES, cat_mod_CFLAGS, font_mod_SOURCES)
(font_mod_CFLAGS, amiga_mod_SOURCES, amiga_mod_CFLAGS)
(apple_mod_SOURCES, apple_mod_CFLAGS, pc_mod_SOURCES)
(pc_mod_CFLAGS): New variables.
* disk/powerpc/ieee1275/ofdisk.c: Include <grub/machine/init.h>.
(grub_ofdisk_iterate): Add a prototype for `dev_iterate'.
* include/grub/dl.h (grub_arch_dl_sync_caches): New prototype.
* include/grub/loader.h (grub_os_area_addr, grub_os_area_size):
Moved from here...
* include/grub/i386/pc/init.h (grub_os_area_addr)
(rub_os_area_size): ... to here.
* include/grub/powerpc/ieee1275/ieee1275.h
(grub_ieee1275_entry_fn): Export symbol.
* include/grub/powerpc/ieee1275/init.h: New file.
* include/grub/powerpc/libgcc.h: Likewise.
* include/grub/cache.h: Likewise.
* kern/powerpc/cache.S: Likewise. Written by Hollis Blanchard
<hollis@penguinppc.org>.
* kern/dl.c: Include <grub/cache.h>.
(grub_dl_flush_cache): New function.
(grub_dl_load_core): Call `grub_dl_flush_cache' to flush the cache
for this module.
* kern/powerpc/ieee1275/init.c (grub_ofdisk_init)
(grub_console_init): Removed prototypes.
(grub_machine_init): Don't initialize the modules anymore.
* kern/powerpc/ieee1275/openfw.c (grub_map): Make the function
static.
* include/grub/powerpc/types.h (GRUB_HOST_WORDS_LITTLEENDIAN):
Macro undef removed.
(GRUB_HOST_WORDS_BIGENDIAN): New macro.
* kern/powerpc/dl.c (grub_arch_dl_relocate_symbols): Add
relocation `R_PPC_REL32'. Return an error when the relocation is
unknown.
* Makefile.in (DATA): Add `$(pkgdata_PROGRAMS)'.
* kern/i386/pc/init.c (grub_arch_sync_caches): New function.
* util/misc.c (grub_arch_sync_caches): Likewise.
2004-12-27 13:46:20 +00:00
|
|
|
case R_PPC_REL32:
|
2009-07-01 14:49:22 +00:00
|
|
|
*addr = value - (Elf_Word) addr;
|
2004-12-27 Marco Gerards <metgerards@student.han.nl>
* genmk.rb: Handle the `Program' class in the main loop. Written
by Johan Rydberg <jrydberg@gnu.org>.
(Program): New class.
(programs): New variable.
* boot/powerpc/ieee1275/cmain.c: Include <grub/machine/ieee1275.h>
instead of "grub/machine/ieee1275.h". Include <grub/kernel.h>
instead of "grub/kernel.h". Include <grub/machine/init.h>.
(help_arch): Function removed.
* conf/powerpc-ieee1275.rmk (grubof_HEADERS): Add
`powerpc/libgcc.h' and `loader.h'.
(pkgdata_PROGRAMS): New variable.
(sbin_UTILITIES): Variable removed.
(grub_emu_SOURCES): Added kern/powerpc/cache.S.
(grubof_SOURCES): Variable re-defined so it only includes the
core functionality.
(grubof_CFLAGS): Remove `-DGRUBOF'.
(pkgdata_MODULES, fshelp_mod_SOURCES, fshelp_mod_CFLAGS,
(fat_mod_SOURCES, fat_mod_CFLAGS, ext2_mod_SOURCES)
(ext2_mod_CFLAGS, ufs_mod_SOURCES, ufs_mod_CFLAGS)
(minix_mod_SOURCES, minix_mod_CFLAGS, hfs_mod_SOURCES)
(hfs_mod_CFLAGS, jfs_mod_SOURCES, jfs_mod_CFLAGS)
(iso9660_mod_SOURCES, iso9660_mod_CFLAGS, _linux_mod_SOURCES)
(_linux_mod_CFLAGS, linux_mod_SOURCES, linux_mod_CFLAGS)
(normal_mod_SOURCES, normal_mod_CFLAGS, normal_mod_ASFLAGS)
(hello_mod_SOURCES, hello_mod_CFLAGS, boot_mod_SOURCES)
(boot_mod_CFLAGS, terminal_mod_SOURCES, terminal_mod_CFLAGS)
(ls_mod_SOURCES, ls_mod_CFLAGS, cmp_mod_SOURCES, cmp_mod_CFLAGS)
(cat_mod_SOURCES, cat_mod_CFLAGS, font_mod_SOURCES)
(font_mod_CFLAGS, amiga_mod_SOURCES, amiga_mod_CFLAGS)
(apple_mod_SOURCES, apple_mod_CFLAGS, pc_mod_SOURCES)
(pc_mod_CFLAGS): New variables.
* disk/powerpc/ieee1275/ofdisk.c: Include <grub/machine/init.h>.
(grub_ofdisk_iterate): Add a prototype for `dev_iterate'.
* include/grub/dl.h (grub_arch_dl_sync_caches): New prototype.
* include/grub/loader.h (grub_os_area_addr, grub_os_area_size):
Moved from here...
* include/grub/i386/pc/init.h (grub_os_area_addr)
(rub_os_area_size): ... to here.
* include/grub/powerpc/ieee1275/ieee1275.h
(grub_ieee1275_entry_fn): Export symbol.
* include/grub/powerpc/ieee1275/init.h: New file.
* include/grub/powerpc/libgcc.h: Likewise.
* include/grub/cache.h: Likewise.
* kern/powerpc/cache.S: Likewise. Written by Hollis Blanchard
<hollis@penguinppc.org>.
* kern/dl.c: Include <grub/cache.h>.
(grub_dl_flush_cache): New function.
(grub_dl_load_core): Call `grub_dl_flush_cache' to flush the cache
for this module.
* kern/powerpc/ieee1275/init.c (grub_ofdisk_init)
(grub_console_init): Removed prototypes.
(grub_machine_init): Don't initialize the modules anymore.
* kern/powerpc/ieee1275/openfw.c (grub_map): Make the function
static.
* include/grub/powerpc/types.h (GRUB_HOST_WORDS_LITTLEENDIAN):
Macro undef removed.
(GRUB_HOST_WORDS_BIGENDIAN): New macro.
* kern/powerpc/dl.c (grub_arch_dl_relocate_symbols): Add
relocation `R_PPC_REL32'. Return an error when the relocation is
unknown.
* Makefile.in (DATA): Add `$(pkgdata_PROGRAMS)'.
* kern/i386/pc/init.c (grub_arch_sync_caches): New function.
* util/misc.c (grub_arch_sync_caches): Likewise.
2004-12-27 13:46:20 +00:00
|
|
|
break;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-12-27 Marco Gerards <metgerards@student.han.nl>
* genmk.rb: Handle the `Program' class in the main loop. Written
by Johan Rydberg <jrydberg@gnu.org>.
(Program): New class.
(programs): New variable.
* boot/powerpc/ieee1275/cmain.c: Include <grub/machine/ieee1275.h>
instead of "grub/machine/ieee1275.h". Include <grub/kernel.h>
instead of "grub/kernel.h". Include <grub/machine/init.h>.
(help_arch): Function removed.
* conf/powerpc-ieee1275.rmk (grubof_HEADERS): Add
`powerpc/libgcc.h' and `loader.h'.
(pkgdata_PROGRAMS): New variable.
(sbin_UTILITIES): Variable removed.
(grub_emu_SOURCES): Added kern/powerpc/cache.S.
(grubof_SOURCES): Variable re-defined so it only includes the
core functionality.
(grubof_CFLAGS): Remove `-DGRUBOF'.
(pkgdata_MODULES, fshelp_mod_SOURCES, fshelp_mod_CFLAGS,
(fat_mod_SOURCES, fat_mod_CFLAGS, ext2_mod_SOURCES)
(ext2_mod_CFLAGS, ufs_mod_SOURCES, ufs_mod_CFLAGS)
(minix_mod_SOURCES, minix_mod_CFLAGS, hfs_mod_SOURCES)
(hfs_mod_CFLAGS, jfs_mod_SOURCES, jfs_mod_CFLAGS)
(iso9660_mod_SOURCES, iso9660_mod_CFLAGS, _linux_mod_SOURCES)
(_linux_mod_CFLAGS, linux_mod_SOURCES, linux_mod_CFLAGS)
(normal_mod_SOURCES, normal_mod_CFLAGS, normal_mod_ASFLAGS)
(hello_mod_SOURCES, hello_mod_CFLAGS, boot_mod_SOURCES)
(boot_mod_CFLAGS, terminal_mod_SOURCES, terminal_mod_CFLAGS)
(ls_mod_SOURCES, ls_mod_CFLAGS, cmp_mod_SOURCES, cmp_mod_CFLAGS)
(cat_mod_SOURCES, cat_mod_CFLAGS, font_mod_SOURCES)
(font_mod_CFLAGS, amiga_mod_SOURCES, amiga_mod_CFLAGS)
(apple_mod_SOURCES, apple_mod_CFLAGS, pc_mod_SOURCES)
(pc_mod_CFLAGS): New variables.
* disk/powerpc/ieee1275/ofdisk.c: Include <grub/machine/init.h>.
(grub_ofdisk_iterate): Add a prototype for `dev_iterate'.
* include/grub/dl.h (grub_arch_dl_sync_caches): New prototype.
* include/grub/loader.h (grub_os_area_addr, grub_os_area_size):
Moved from here...
* include/grub/i386/pc/init.h (grub_os_area_addr)
(rub_os_area_size): ... to here.
* include/grub/powerpc/ieee1275/ieee1275.h
(grub_ieee1275_entry_fn): Export symbol.
* include/grub/powerpc/ieee1275/init.h: New file.
* include/grub/powerpc/libgcc.h: Likewise.
* include/grub/cache.h: Likewise.
* kern/powerpc/cache.S: Likewise. Written by Hollis Blanchard
<hollis@penguinppc.org>.
* kern/dl.c: Include <grub/cache.h>.
(grub_dl_flush_cache): New function.
(grub_dl_load_core): Call `grub_dl_flush_cache' to flush the cache
for this module.
* kern/powerpc/ieee1275/init.c (grub_ofdisk_init)
(grub_console_init): Removed prototypes.
(grub_machine_init): Don't initialize the modules anymore.
* kern/powerpc/ieee1275/openfw.c (grub_map): Make the function
static.
* include/grub/powerpc/types.h (GRUB_HOST_WORDS_LITTLEENDIAN):
Macro undef removed.
(GRUB_HOST_WORDS_BIGENDIAN): New macro.
* kern/powerpc/dl.c (grub_arch_dl_relocate_symbols): Add
relocation `R_PPC_REL32'. Return an error when the relocation is
unknown.
* Makefile.in (DATA): Add `$(pkgdata_PROGRAMS)'.
* kern/i386/pc/init.c (grub_arch_sync_caches): New function.
* util/misc.c (grub_arch_sync_caches): Likewise.
2004-12-27 13:46:20 +00:00
|
|
|
default:
|
|
|
|
return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
|
2009-12-24 22:53:05 +00:00
|
|
|
"this relocation (%d) is not implemented yet",
|
2009-07-01 14:49:22 +00:00
|
|
|
ELF_R_TYPE (rel->r_info));
|
2004-12-19 20:35:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-12-19 20:35:06 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|