1 /* dl-x86_64.c - arch-dependent part of loadable module support */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2005,2007,2009 Free Software Foundation, Inc.
6 * 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
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
22 #include <grub/misc.h>
24 #include <grub/i18n.h>
26 /* Check if EHDR is a valid ELF header. */
28 grub_arch_dl_check_header (void *ehdr)
32 /* Check the magic numbers. */
33 if (e->e_ident[EI_CLASS] != ELFCLASS64
34 || e->e_ident[EI_DATA] != ELFDATA2LSB
35 || e->e_machine != EM_X86_64)
36 return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
41 /* Relocate symbols. */
43 grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
44 Elf_Shdr *s, grub_dl_segment_t seg)
46 Elf64_Rela *rel, *max;
48 for (rel = (Elf64_Rela *) ((char *) ehdr + s->sh_offset),
49 max = (Elf64_Rela *) ((char *) rel + s->sh_size);
51 rel = (Elf64_Rela *) ((char *) rel + s->sh_entsize))
57 if (seg->size < rel->r_offset)
58 return grub_error (GRUB_ERR_BAD_MODULE,
59 "reloc offset is out of the segment");
61 addr32 = (Elf64_Word *) ((char *) seg->addr + rel->r_offset);
62 addr64 = (Elf64_Xword *) addr32;
63 sym = (Elf64_Sym *) ((char *) mod->symtab
64 + mod->symsize * ELF_R_SYM (rel->r_info));
66 switch (ELF_R_TYPE (rel->r_info))
69 *addr64 += rel->r_addend + sym->st_value;
75 value = ((grub_int32_t) *addr32) + rel->r_addend + sym->st_value -
76 (Elf64_Xword) (grub_addr_t) seg->addr - rel->r_offset;
77 if (value != (grub_int32_t) value)
78 return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
85 *addr64 += rel->r_addend + sym->st_value -
86 (Elf64_Xword) (grub_addr_t) seg->addr - rel->r_offset;
92 grub_uint64_t value = *addr32 + rel->r_addend + sym->st_value;
93 if (value != (grub_uint32_t) value)
94 return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
100 grub_int64_t value = ((grub_int32_t) *addr32) + rel->r_addend + sym->st_value;
101 if (value != (grub_int32_t) value)
102 return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
108 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
109 N_("relocation 0x%x is not implemented yet"),
110 ELF_R_TYPE (rel->r_info));
114 return GRUB_ERR_NONE;