44069067312a890b4e3ebb5130861ed23572bc54
[grub.git] / grub-core / kern / x86_64 / dl.c
1 /* dl-x86_64.c - arch-dependent part of loadable module support */
2 /*
3  *  GRUB  --  GRand Unified Bootloader
4  *  Copyright (C) 2002,2005,2007,2009  Free Software Foundation, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <grub/dl.h>
21 #include <grub/elf.h>
22 #include <grub/misc.h>
23 #include <grub/err.h>
24 #include <grub/i18n.h>
25
26 /* Check if EHDR is a valid ELF header.  */
27 grub_err_t
28 grub_arch_dl_check_header (void *ehdr)
29 {
30   Elf64_Ehdr *e = ehdr;
31
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"));
37
38   return GRUB_ERR_NONE;
39 }
40
41 /* Relocate symbols.  */
42 grub_err_t
43 grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
44                                Elf_Shdr *s, grub_dl_segment_t seg)
45 {
46   Elf64_Rela *rel, *max;
47
48   for (rel = (Elf64_Rela *) ((char *) ehdr + s->sh_offset),
49          max = (Elf64_Rela *) ((char *) rel + s->sh_size);
50        rel < max;
51        rel = (Elf64_Rela *) ((char *) rel + s->sh_entsize))
52     {
53       Elf64_Word *addr32;
54       Elf64_Xword *addr64;
55       Elf64_Sym *sym;
56
57       if (seg->size < rel->r_offset)
58         return grub_error (GRUB_ERR_BAD_MODULE,
59                            "reloc offset is out of the segment");
60
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));
65
66       switch (ELF_R_TYPE (rel->r_info))
67         {
68         case R_X86_64_64:
69           *addr64 += rel->r_addend + sym->st_value;
70           break;
71
72         case R_X86_64_PC32:
73           {
74             grub_int64_t 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");
79             *addr32 = value;
80           }
81           break;
82
83         case R_X86_64_PC64:
84           {
85             *addr64 += rel->r_addend + sym->st_value -
86               (Elf64_Xword) (grub_addr_t) seg->addr - rel->r_offset;
87           }
88           break;
89
90         case R_X86_64_32:
91           {
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");
95             *addr32 = value;
96           }
97           break;
98         case R_X86_64_32S:
99           {
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");
103             *addr32 = value;
104           }
105           break;
106
107         default:
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));
111         }
112     }
113
114   return GRUB_ERR_NONE;
115 }