grub/grub-core/kern/mips/dl.c
Vladimir 'phcoder' Serbinenko 400b93718f SGI ARCS port.
* Makefile.util.def (libgrubmods.a): Add dvh.c.
	* conf/Makefile.common (CCASFLAGS_PLATFORM): Add -mips3 on all mips
	platforms.
	* configure.ac: New target mips-arc.
	* gentpl.py: Likewise.
	* grub-core/Makefile.am: Likewise.
	* grub-core/Makefile.core.def: Likewise.
	(xz_decompress): Remove -D GRUB_MACHINE_LINK_ADDR.
	(none_decompress): Likewise.
	(lsdev): New module.
	(datetime): Use lib/arc/datetime.c on ARC.
	(part_dvh): New module.
	* grub-core/commands/arc/lsdev.c: New file.
	* grub-core/disk/arc/arcdisk.c: Likewise.
	* grub-core/kern/mips/arc/init.c: Likewise.
	* grub-core/kern/mips/cache_flush.S: Don't flush non 4-byte
	aligned addresses.
	* grub-core/kern/mips/dl.c (grub_arch_dl_check_header): Fix bigendian
	support.
	(grub_arch_dl_relocate_symbols): Likewise.
	* grub-core/kern/mips/loongson/init.c (grub_get_rtc): Moved from here...
	* grub-core/kern/mips/init.c (grub_get_rtc): ... here.
	* grub-core/kern/mips/startup.S (grub_arch_cpuclock): Have on all
	platforms.
	* grub-core/lib/arc/datetime.c: New file.
	* grub-core/loader/mips/linux.c: Always include time.h. Don't include
	pci.h on non-loongson.
	(envp_off) [!GRUB_MACHINE_MIPS_LOONGSON]: Remove.
	(grub_linux_boot): Set unused registers to 0.
	(grub_cmd_linux) [!GRUB_MACHINE_MIPS_LOONGSON]: Remove envp.
	* grub-core/mmap/mips/loongson/uppermem.c: Moved from here ...
	* grub-core/mmap/mips/uppermem.c: ...here.
	* grub-core/partmap/dvh.c: New file.
	* grub-core/term/arc/console.c: Likewise.
	* grub-core/term/terminfo.c (ANSI_C0_STR): New const.
	(grub_terminfo_set_current): Add terminal "arc".
	(grub_terminfo_readkey): Support ARC sequences.
	* include/grub/arc/arc.h: New file.
	* include/grub/arc/console.h: Likewise.
	* include/grub/disk.h (grub_disk_dev_id): Add
	GRUB_DISK_DEVICE_ARCDISK_ID.
	* include/grub/mips/arc/kernel.h: New file.
	* include/grub/mips/arc/memory.h: Likewise.
	* include/grub/mips/arc/time.h: Likewise.
	* include/grub/mips/loongson/kernel.h (grub_halt): Moved from here ...
	* include/grub/mips/kernel.h (grub_halt): ... here.
	* include/grub/mips/loongson.h (GRUB_CPU_REGISTER_WRAP): Moved from
	here...
	* include/grub/mips/mips.h (GRUB_CPU_REGISTER_WRAP): ... here.
	(GRUB_CPU_LOONGSON_COP0_TIMER_COUNT): Moved from here ...
	* include/grub/mips/mips.h (GRUB_CPU_LOONGSON_COP0_TIMER_COUNT): .. here
	* include/grub/mips/loongson/kernel.h (grub_reboot): Removed redundant
	proto.
	* include/grub/mips/loongson/memory.h (GRUB_ARCH_LOWMEMVSTART): Moved
	from here ...
	* include/grub/mips/memory.h (GRUB_ARCH_LOWMEMVSTART): ... here.
	(GRUB_ARCH_LOWMEMPSTART): Moved from here ...
	* include/grub/mips/memory.h (GRUB_ARCH_LOWMEMPSTART): ... here.
	(GRUB_ARCH_LOWMEMMAXSIZE): Moved from here ...
	* include/grub/mips/memory.h (GRUB_ARCH_LOWMEMMAXSIZE): ... here.
	(GRUB_ARCH_HIGHMEMPSTART): Moved from here ...
	* include/grub/mips/memory.h (GRUB_ARCH_HIGHMEMPSTART): ... here.
	(grub_phys_addr_t): Moved from here ...
	* include/grub/mips/memory.h (grub_phys_addr_t): ... here.
	(grub_vtop): Moved from here ...
	* include/grub/mips/memory.h (grub_vtop): ... here.
	(grub_map_memory): Moved from here ...
	* include/grub/mips/memory.h (grub_map_memory): ... here.
	(grub_unmap_memory): Moved from here ...
	* include/grub/mips/memory.h (grub_unmap_memory): ... here.
	(grub_machine_mmap_iterate): Moved from here ...
	* include/grub/mips/memory.h (grub_machine_mmap_iterate): ... here.
	(grub_mmap_get_lower): Moved from here ...
	* include/grub/mips/memory.h (grub_mmap_get_lower): ... here.
	(grub_mmap_get_upper): Moved from here ...
	* include/grub/mips/memory.h (grub_mmap_get_upper): ... here.
	* include/grub/mips/loongson/time.h (grub_arch_cpuclock): Moved from
	here ...
	* include/grub/mips/time.h (grub_arch_cpuclock): ... here.
	* include/grub/mips/loongson/time.h (grub_get_rtc): Moved from
	here ...
	* include/grub/mips/time.h (grub_get_rtc): ... here.
	* include/grub/mips/loongson/time.h (grub_arch_cpuclock): Moved from
	here ...
	* include/grub/mips/time.h (grub_arch_cpuclock): ... here.
	* include/grub/mips/loongson/time.h (grub_cpu_idle): Moved from
	here ...
	* include/grub/mips/time.h (grub_cpu_idle): ... here.
	* include/grub/offsets.h (GRUB_KERNEL_MIPS_ARC_LINK_ADDR): New
	definition.
	(GRUB_KERNEL_MIPS_ARC_LINK_ALIGN): Likewise.
	(GRUB_KERNEL_MIPS_ARC_COMPRESSED_SIZE): Likewise.
	(GRUB_KERNEL_MIPS_ARC_UNCOMPRESSED_SIZE): Likewise.
	(GRUB_KERNEL_MIPS_ARC_TOTAL_MODULE_SIZE): Likewise.
	(GRUB_KERNEL_MIPS_ARC_PREFIX): Likewise.
	(GRUB_KERNEL_MIPS_ARC_PREFIX_END): Likewise.
	(GRUB_KERNEL_MIPS_ARC_MOD_ALIGN): Likewise.
	(GRUB_MACHINE_LINK_ADDR): Likewise.
	* include/grub/terminfo.h (GRUB_TERMINFO_READKEY_MAX_LEN): Increased
	to 6.
	* util/grub-install.in: Run dvhtool on ARC.
	* util/grub-mkimage.c (image_targets): Add mips-arc.
2011-05-17 09:17:38 +02:00

253 lines
6.4 KiB
C

/* dl-386.c - arch-dependent part of loadable module support */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2005,2007,2009 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* 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
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/dl.h>
#include <grub/elf.h>
#include <grub/misc.h>
#include <grub/err.h>
#include <grub/cpu/types.h>
#include <grub/mm.h>
/* Dummy __gnu_local_gp. Resolved by linker. */
static char __gnu_local_gp_dummy;
/* Check if EHDR is a valid ELF header. */
grub_err_t
grub_arch_dl_check_header (void *ehdr)
{
Elf_Ehdr *e = ehdr;
/* Check the magic numbers. */
#ifdef GRUB_CPU_WORDS_BIGENDIAN
if (e->e_ident[EI_CLASS] != ELFCLASS32
|| e->e_ident[EI_DATA] != ELFDATA2MSB
|| e->e_machine != EM_MIPS)
#else
if (e->e_ident[EI_CLASS] != ELFCLASS32
|| e->e_ident[EI_DATA] != ELFDATA2LSB
|| e->e_machine != EM_MIPS)
#endif
return grub_error (GRUB_ERR_BAD_OS, "invalid arch specific ELF magic");
return GRUB_ERR_NONE;
}
/* Relocate symbols. */
grub_err_t
grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
{
Elf_Ehdr *e = ehdr;
Elf_Shdr *s;
Elf_Word entsize;
unsigned i;
grub_size_t gp_size = 0;
/* FIXME: suboptimal. */
grub_uint32_t *gp, *gpptr;
grub_uint32_t gp0;
/* Find a symbol table. */
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;
if (i == e->e_shnum)
return grub_error (GRUB_ERR_BAD_MODULE, "no symtab found");
entsize = s->sh_entsize;
/* Find reginfo. */
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_MIPS_REGINFO)
break;
if (i == e->e_shnum)
return grub_error (GRUB_ERR_BAD_MODULE, "no reginfo found");
gp0 = ((grub_uint32_t *)((char *) e + s->sh_offset))[5];
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)
{
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)
{
Elf_Rel *rel, *max;
for (rel = (Elf_Rel *) ((char *) e + s->sh_offset),
max = rel + s->sh_size / s->sh_entsize;
rel < max;
rel++)
switch (ELF_R_TYPE (rel->r_info))
{
case R_MIPS_GOT16:
case R_MIPS_CALL16:
case R_MIPS_GPREL32:
gp_size += 4;
break;
}
}
}
if (gp_size > 0x08000)
return grub_error (GRUB_ERR_OUT_OF_RANGE, "__gnu_local_gp is too big\n");
gpptr = gp = grub_malloc (gp_size);
if (!gp)
return grub_errno;
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)
{
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)
{
Elf_Rel *rel, *max;
for (rel = (Elf_Rel *) ((char *) e + s->sh_offset),
max = rel + s->sh_size / s->sh_entsize;
rel < max;
rel++)
{
grub_uint8_t *addr;
Elf_Sym *sym;
if (seg->size < rel->r_offset)
return grub_error (GRUB_ERR_BAD_MODULE,
"reloc offset is out of the segment");
addr = (grub_uint8_t *) ((char *) seg->addr + rel->r_offset);
sym = (Elf_Sym *) ((char *) mod->symtab
+ entsize * ELF_R_SYM (rel->r_info));
if (sym->st_value == (grub_addr_t) &__gnu_local_gp_dummy)
sym->st_value = (grub_addr_t) gp;
switch (ELF_R_TYPE (rel->r_info))
{
case R_MIPS_HI16:
{
grub_uint32_t value;
Elf_Rel *rel2;
#ifdef GRUB_CPU_WORDS_BIGENDIAN
addr += 2;
#endif
/* Handle partner lo16 relocation. Lower part is
treated as signed. Hence add 0x8000 to compensate.
*/
value = (*(grub_uint16_t *) addr << 16)
+ sym->st_value + 0x8000;
for (rel2 = rel + 1; rel2 < max; rel2++)
if (ELF_R_SYM (rel2->r_info)
== ELF_R_SYM (rel->r_info)
&& ELF_R_TYPE (rel2->r_info) == R_MIPS_LO16)
{
value += *(grub_int16_t *)
((char *) seg->addr + rel2->r_offset
#ifdef GRUB_CPU_WORDS_BIGENDIAN
+ 2
#endif
);
break;
}
*(grub_uint16_t *) addr = (value >> 16) & 0xffff;
}
break;
case R_MIPS_LO16:
#ifdef GRUB_CPU_WORDS_BIGENDIAN
addr += 2;
#endif
*(grub_uint16_t *) addr += (sym->st_value) & 0xffff;
break;
case R_MIPS_32:
*(grub_uint32_t *) addr += sym->st_value;
break;
case R_MIPS_GPREL32:
*(grub_uint32_t *) addr = sym->st_value
+ *(grub_uint32_t *) addr + gp0 - (grub_uint32_t)gp;
break;
case R_MIPS_26:
{
grub_uint32_t value;
grub_uint32_t raw;
raw = (*(grub_uint32_t *) addr) & 0x3ffffff;
value = raw << 2;
value += sym->st_value;
raw = (value >> 2) & 0x3ffffff;
*(grub_uint32_t *) addr =
raw | ((*(grub_uint32_t *) addr) & 0xfc000000);
}
break;
case R_MIPS_GOT16:
case R_MIPS_CALL16:
/* FIXME: reuse*/
#ifdef GRUB_CPU_WORDS_BIGENDIAN
addr += 2;
#endif
*gpptr = sym->st_value + *(grub_uint16_t *) addr;
*(grub_uint16_t *) addr
= sizeof (grub_uint32_t) * (gpptr - gp);
gpptr++;
break;
case R_MIPS_JALR:
break;
default:
{
grub_free (gp);
return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
"Unknown relocation type %d\n",
ELF_R_TYPE (rel->r_info));
}
break;
}
}
}
}
return GRUB_ERR_NONE;
}
void
grub_arch_dl_init_linker (void)
{
grub_dl_register_symbol ("__gnu_local_gp", &__gnu_local_gp_dummy, 0, 0);
}