2N/A/* dl.c - arch-dependent part of loadable module support */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2002,2004,2005,2007,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/dl.h>
2N/A#include <grub/elf.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/err.h>
2N/A#include <grub/mm.h>
2N/A
2N/A/* Check if EHDR is a valid ELF header. */
2N/Agrub_err_t
2N/Agrub_arch_dl_check_header (void *ehdr)
2N/A{
2N/A Elf_Ehdr *e = ehdr;
2N/A
2N/A /* Check the magic numbers. */
2N/A if (e->e_ident[EI_CLASS] != ELFCLASS64
2N/A || e->e_ident[EI_DATA] != ELFDATA2LSB
2N/A || e->e_machine != EM_IA_64)
2N/A return grub_error (GRUB_ERR_BAD_OS, "invalid arch specific ELF magic");
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A#define MASK20 ((1 << 20) - 1)
2N/A#define MASK19 ((1 << 19) - 1)
2N/A
2N/Astruct unaligned_uint32
2N/A{
2N/A grub_uint32_t val;
2N/A} __attribute__ ((packed));
2N/A
2N/Astatic void
2N/Aadd_value_to_slot_20b (grub_addr_t addr, grub_uint32_t value)
2N/A{
2N/A struct unaligned_uint32 *p;
2N/A switch (addr & 3)
2N/A {
2N/A case 0:
2N/A p = (struct unaligned_uint32 *) ((addr & ~3ULL) + 2);
2N/A p->val = ((((((p->val >> 2) & MASK20) + value) & MASK20) << 2)
2N/A | (p->val & ~(MASK20 << 2)));
2N/A break;
2N/A case 1:
2N/A p = (struct unaligned_uint32 *) ((grub_uint8_t *) (addr & ~3ULL) + 7);
2N/A p->val = ((((((p->val >> 3) & MASK20) + value) & MASK20) << 3)
2N/A | (p->val & ~(MASK20 << 3)));
2N/A break;
2N/A case 2:
2N/A p = (struct unaligned_uint32 *) ((grub_uint8_t *) (addr & ~3ULL) + 12);
2N/A p->val = ((((((p->val >> 4) & MASK20) + value) & MASK20) << 4)
2N/A | (p->val & ~(MASK20 << 4)));
2N/A break;
2N/A }
2N/A}
2N/A
2N/A#define MASKF21 ( ((1 << 23) - 1) & ~((1 << 7) | (1 << 8)) )
2N/A
2N/Astatic grub_uint32_t
2N/Aadd_value_to_slot_21_real (grub_uint32_t a, grub_uint32_t value)
2N/A{
2N/A grub_uint32_t high, mid, low, c;
2N/A low = (a & 0x00007f);
2N/A mid = (a & 0x7fc000) >> 7;
2N/A high = (a & 0x003e00) << 7;
2N/A c = (low | mid | high) + value;
2N/A return (c & 0x7f) | ((c << 7) & 0x7fc000) | ((c >> 7) & 0x0003e00); //0x003e00
2N/A}
2N/A
2N/Astatic void
2N/Aadd_value_to_slot_21 (grub_addr_t addr, grub_uint32_t value)
2N/A{
2N/A struct unaligned_uint32 *p;
2N/A switch (addr & 3)
2N/A {
2N/A case 0:
2N/A p = (struct unaligned_uint32 *) ((addr & ~3ULL) + 2);
2N/A p->val = ((add_value_to_slot_21_real (((p->val >> 2) & MASKF21), value) & MASKF21) << 2) | (p->val & ~(MASKF21 << 2));
2N/A break;
2N/A case 1:
2N/A p = (struct unaligned_uint32 *) ((grub_uint8_t *) (addr & ~3ULL) + 7);
2N/A p->val = ((add_value_to_slot_21_real (((p->val >> 3) & MASKF21), value) & MASKF21) << 3) | (p->val & ~(MASKF21 << 3));
2N/A break;
2N/A case 2:
2N/A p = (struct unaligned_uint32 *) ((grub_uint8_t *) (addr & ~3ULL) + 12);
2N/A p->val = ((add_value_to_slot_21_real (((p->val >> 4) & MASKF21), value) & MASKF21) << 4) | (p->val & ~(MASKF21 << 4));
2N/A break;
2N/A }
2N/A}
2N/A
2N/Astatic const grub_uint8_t nopm[5] =
2N/A {
2N/A /* [MLX] nop.m 0x0 */
2N/A 0x05, 0x00, 0x00, 0x00, 0x01
2N/A };
2N/A
2N/Astatic const grub_uint8_t jump[0x20] =
2N/A {
2N/A /* ld8 r16=[r15],8 */
2N/A 0x02, 0x80, 0x20, 0x1e, 0x18, 0x14,
2N/A /* mov r14=r1;; */
2N/A 0xe0, 0x00, 0x04, 0x00, 0x42, 0x00,
2N/A /* nop.i 0x0 */
2N/A 0x00, 0x00, 0x04, 0x00,
2N/A /* ld8 r1=[r15] */
2N/A 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10,
2N/A /* mov b6=r16 */
2N/A 0x60, 0x80, 0x04, 0x80, 0x03, 0x00,
2N/A /* br.few b6;; */
2N/A 0x60, 0x00, 0x80, 0x00
2N/A };
2N/A
2N/Astruct ia64_trampoline
2N/A{
2N/A /* nop.m */
2N/A grub_uint8_t nop[5];
2N/A /* movl r15 = addr*/
2N/A grub_uint8_t addr_hi[6];
2N/A grub_uint8_t e0;
2N/A grub_uint8_t addr_lo[4];
2N/A grub_uint8_t jump[0x20];
2N/A};
2N/A
2N/Astatic void
2N/Amake_trampoline (struct ia64_trampoline *tr, grub_uint64_t addr)
2N/A{
2N/A COMPILE_TIME_ASSERT (sizeof (struct ia64_trampoline)
2N/A == GRUB_IA64_DL_TRAMP_SIZE);
2N/A grub_memcpy (tr->nop, nopm, sizeof (tr->nop));
2N/A tr->addr_hi[0] = ((addr & 0xc00000) >> 16);
2N/A tr->addr_hi[1] = (addr >> 24) & 0xff;
2N/A tr->addr_hi[2] = (addr >> 32) & 0xff;
2N/A tr->addr_hi[3] = (addr >> 40) & 0xff;
2N/A tr->addr_hi[4] = (addr >> 48) & 0xff;
2N/A tr->addr_hi[5] = (addr >> 56) & 0xff;
2N/A tr->e0 = 0xe0;
2N/A tr->addr_lo[0] = ((addr & 0x000f) << 4) | 0x01;
2N/A tr->addr_lo[1] = (((addr & 0x0070) >> 4) | ((addr & 0x070000) >> 11)
2N/A | ((addr & 0x200000) >> 17));
2N/A tr->addr_lo[2] = ((addr & 0x1f80) >> 5) | ((addr & 0x180000) >> 19);
2N/A tr->addr_lo[3] = ((addr & 0xe000) >> 13) | 0x60;
2N/A grub_memcpy (tr->jump, jump, sizeof (tr->jump));
2N/A}
2N/A
2N/A/* Relocate symbols. */
2N/Agrub_err_t
2N/Agrub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
2N/A{
2N/A Elf_Ehdr *e = ehdr;
2N/A Elf_Shdr *s;
2N/A Elf_Word entsize;
2N/A unsigned i;
2N/A grub_uint64_t *gp, *gpptr;
2N/A struct ia64_trampoline *tr;
2N/A
2N/A gp = (grub_uint64_t *) mod->base;
2N/A gpptr = (grub_uint64_t *) mod->got;
2N/A tr = mod->tramp;
2N/A
2N/A /* Find a symbol table. */
2N/A for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
2N/A i < e->e_shnum;
2N/A i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
2N/A if (s->sh_type == SHT_SYMTAB)
2N/A break;
2N/A
2N/A if (i == e->e_shnum)
2N/A return grub_error (GRUB_ERR_BAD_MODULE, "no symtab found");
2N/A
2N/A entsize = s->sh_entsize;
2N/A
2N/A for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
2N/A i < e->e_shnum;
2N/A i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
2N/A if (s->sh_type == SHT_RELA)
2N/A {
2N/A grub_dl_segment_t seg;
2N/A
2N/A /* Find the target segment. */
2N/A for (seg = mod->segment; seg; seg = seg->next)
2N/A if (seg->section == s->sh_info)
2N/A break;
2N/A
2N/A if (seg)
2N/A {
2N/A Elf_Rela *rel, *max;
2N/A
2N/A for (rel = (Elf_Rela *) ((char *) e + s->sh_offset),
2N/A max = rel + s->sh_size / s->sh_entsize;
2N/A rel < max;
2N/A rel++)
2N/A {
2N/A grub_addr_t addr;
2N/A Elf_Sym *sym;
2N/A grub_uint64_t value;
2N/A
2N/A if (seg->size < (rel->r_offset & ~3))
2N/A return grub_error (GRUB_ERR_BAD_MODULE,
2N/A "reloc offset is out of the segment");
2N/A
2N/A addr = (grub_addr_t) seg->addr + rel->r_offset;
2N/A sym = (Elf_Sym *) ((char *) mod->symtab
2N/A + entsize * ELF_R_SYM (rel->r_info));
2N/A
2N/A /* On the PPC the value does not have an explicit
2N/A addend, add it. */
2N/A value = sym->st_value + rel->r_addend;
2N/A
2N/A switch (ELF_R_TYPE (rel->r_info))
2N/A {
2N/A case R_IA64_PCREL21B:
2N/A {
2N/A grub_uint64_t noff;
2N/A make_trampoline (tr, value);
2N/A noff = ((char *) tr - (char *) (addr & ~3)) >> 4;
2N/A tr++;
2N/A if (noff & ~MASK19)
2N/A return grub_error (GRUB_ERR_BAD_OS,
2N/A "trampoline offset too big (%lx)", noff);
2N/A add_value_to_slot_20b (addr, noff);
2N/A }
2N/A break;
2N/A case R_IA64_SEGREL64LSB:
2N/A *(grub_uint64_t *) addr += value - (grub_addr_t) seg->addr;
2N/A break;
2N/A case R_IA64_FPTR64LSB:
2N/A case R_IA64_DIR64LSB:
2N/A *(grub_uint64_t *) addr += value;
2N/A break;
2N/A case R_IA64_PCREL64LSB:
2N/A *(grub_uint64_t *) addr += value - addr;
2N/A break;
2N/A case R_IA64_GPREL22:
2N/A add_value_to_slot_21 (addr, value - (grub_addr_t) gp);
2N/A break;
2N/A
2N/A case R_IA64_LTOFF22X:
2N/A case R_IA64_LTOFF22:
2N/A if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2N/A value = *(grub_uint64_t *) sym->st_value + rel->r_addend;
2N/A case R_IA64_LTOFF_FPTR22:
2N/A *gpptr = value;
2N/A add_value_to_slot_21 (addr, (grub_addr_t) gpptr - (grub_addr_t) gp);
2N/A gpptr++;
2N/A break;
2N/A
2N/A /* We treat LTOFF22X as LTOFF22, so we can ignore LDXMOV. */
2N/A case R_IA64_LDXMOV:
2N/A break;
2N/A default:
2N/A return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
2N/A "this relocation (0x%x) is not implemented yet",
2N/A ELF_R_TYPE (rel->r_info));
2N/A }
2N/A }
2N/A }
2N/A }
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}