]> git.proxmox.com Git - grub2.git/blob - grub-core/kern/riscv/dl.c
RISC-V: Add awareness for RISC-V reloations
[grub2.git] / grub-core / kern / riscv / dl.c
1 /* dl.c - arch-dependent part of loadable module support */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2018 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/mm.h>
25 #include <grub/i18n.h>
26
27 /*
28 * Instructions and instruction encoding are documented in the RISC-V
29 * specification. This file is based on version 2.2:
30 *
31 * https://github.com/riscv/riscv-isa-manual/blob/master/release/riscv-spec-v2.2.pdf
32 */
33 #define LDR 0x58000050
34 #define BR 0xd61f0200
35
36 /*
37 * Check if EHDR is a valid ELF header.
38 */
39 grub_err_t
40 grub_arch_dl_check_header (void *ehdr)
41 {
42 Elf_Ehdr *e = ehdr;
43
44 /* Check the magic numbers. */
45 if (e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_RISCV)
46 return grub_error (GRUB_ERR_BAD_OS,
47 N_("invalid arch-dependent ELF magic"));
48
49 return GRUB_ERR_NONE;
50 }
51
52 #pragma GCC diagnostic ignored "-Wcast-align"
53
54 /* Relocate symbols. */
55 grub_err_t
56 grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
57 Elf_Shdr *s, grub_dl_segment_t seg)
58 {
59 Elf_Rel *rel, *max;
60
61 for (rel = (Elf_Rel *) ((char *) ehdr + s->sh_offset),
62 max = (Elf_Rel *) ((char *) rel + s->sh_size);
63 rel < max;
64 rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
65 {
66 Elf_Sym *sym;
67 void *place;
68 grub_size_t sym_addr;
69
70 if (rel->r_offset >= seg->size)
71 return grub_error (GRUB_ERR_BAD_MODULE,
72 "reloc offset is out of the segment");
73
74 sym = (Elf_Sym *) ((char *) mod->symtab
75 + mod->symsize * ELF_R_SYM (rel->r_info));
76
77 sym_addr = sym->st_value;
78 if (s->sh_type == SHT_RELA)
79 sym_addr += ((Elf_Rela *) rel)->r_addend;
80
81 place = (void *) ((grub_addr_t) seg->addr + rel->r_offset);
82
83 switch (ELF_R_TYPE (rel->r_info))
84 {
85 case R_RISCV_32:
86 {
87 grub_uint32_t *abs_place = place;
88
89 grub_dprintf ("dl", " reloc_abs32 %p => 0x%016llx\n",
90 place, (unsigned long long) sym_addr);
91
92 *abs_place = (grub_uint32_t) sym_addr;
93 }
94 break;
95 case R_RISCV_64:
96 {
97 grub_size_t *abs_place = place;
98
99 grub_dprintf ("dl", " reloc_abs64 %p => 0x%016llx\n",
100 place, (unsigned long long) sym_addr);
101
102 *abs_place = (grub_size_t) sym_addr;
103 }
104 break;
105
106 case R_RISCV_ADD8:
107 {
108 grub_uint8_t *abs_place = place;
109
110 *abs_place += (grub_uint8_t) sym_addr;
111 }
112 break;
113 case R_RISCV_ADD16:
114 {
115 grub_uint16_t *abs_place = place;
116
117 *abs_place += (grub_uint16_t) sym_addr;
118 }
119 break;
120 case R_RISCV_ADD32:
121 {
122 grub_uint32_t *abs_place = place;
123
124 *abs_place += (grub_uint32_t) sym_addr;
125 }
126 break;
127 case R_RISCV_ADD64:
128 {
129 grub_size_t *abs_place = place;
130
131 *abs_place += (grub_size_t) sym_addr;
132 }
133 break;
134
135 case R_RISCV_SUB8:
136 {
137 grub_uint8_t *abs_place = place;
138
139 *abs_place -= (grub_uint8_t) sym_addr;
140 }
141 break;
142 case R_RISCV_SUB16:
143 {
144 grub_uint16_t *abs_place = place;
145
146 *abs_place -= (grub_uint16_t) sym_addr;
147 }
148 break;
149 case R_RISCV_SUB32:
150 {
151 grub_uint32_t *abs_place = place;
152
153 *abs_place -= (grub_uint32_t) sym_addr;
154 }
155 break;
156 case R_RISCV_SUB64:
157 {
158 grub_size_t *abs_place = place;
159
160 *abs_place -= (grub_size_t) sym_addr;
161 }
162 break;
163
164 case R_RISCV_BRANCH:
165 {
166 grub_uint32_t *abs_place = place;
167 grub_ssize_t off = sym_addr - (grub_addr_t) place;
168 grub_uint32_t imm12 = (off & 0x1000) << (31 - 12);
169 grub_uint32_t imm11 = (off & 0x800) >> (11 - 7);
170 grub_uint32_t imm10_5 = (off & 0x7e0) << (30 - 10);
171 grub_uint32_t imm4_1 = (off & 0x1e) << (11 - 4);
172 *abs_place = (*abs_place & 0x1fff07f)
173 | imm12 | imm11 | imm10_5 | imm4_1;
174 }
175 break;
176
177 case R_RISCV_JAL:
178 {
179 grub_uint32_t *abs_place = place;
180 grub_ssize_t off = sym_addr - (grub_addr_t) place;
181 grub_uint32_t imm20 = (off & 0x100000) << (31 - 20);
182 grub_uint32_t imm19_12 = (off & 0xff000);
183 grub_uint32_t imm11 = (off & 0x800) << (20 - 11);
184 grub_uint32_t imm10_1 = (off & 0x7fe) << (30 - 10);
185 *abs_place = (*abs_place & 0xfff)
186 | imm20 | imm19_12 | imm11 | imm10_1;
187 }
188 break;
189
190 case R_RISCV_CALL:
191 {
192 grub_uint32_t *abs_place = place;
193 grub_ssize_t off = sym_addr - (grub_addr_t) place;
194 grub_uint32_t hi20, lo12;
195
196 if (off != (grub_int32_t) off)
197 return grub_error (GRUB_ERR_BAD_MODULE, "relocation overflow");
198
199 hi20 = (off + 0x800) & 0xfffff000;
200 lo12 = (off - hi20) & 0xfff;
201 abs_place[0] = (abs_place[0] & 0xfff) | hi20;
202 abs_place[1] = (abs_place[1] & 0xfffff) | (lo12 << 20);
203 }
204 break;
205
206 case R_RISCV_RVC_BRANCH:
207 {
208 grub_uint16_t *abs_place = place;
209 grub_ssize_t off = sym_addr - (grub_addr_t) place;
210 grub_uint16_t imm8 = (off & 0x100) << (12 - 8);
211 grub_uint16_t imm7_6 = (off & 0xc0) >> (6 - 5);
212 grub_uint16_t imm5 = (off & 0x20) >> (5 - 2);
213 grub_uint16_t imm4_3 = (off & 0x18) << (12 - 5);
214 grub_uint16_t imm2_1 = (off & 0x6) << (12 - 10);
215 *abs_place = (*abs_place & 0xe383)
216 | imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
217 }
218 break;
219
220 case R_RISCV_RVC_JUMP:
221 {
222 grub_uint16_t *abs_place = place;
223 grub_ssize_t off = sym_addr - (grub_addr_t) place;
224 grub_uint16_t imm11 = (off & 0x800) << (12 - 11);
225 grub_uint16_t imm10 = (off & 0x400) >> (10 - 8);
226 grub_uint16_t imm9_8 = (off & 0x300) << (12 - 11);
227 grub_uint16_t imm7 = (off & 0x80) >> (7 - 6);
228 grub_uint16_t imm6 = (off & 0x40) << (12 - 11);
229 grub_uint16_t imm5 = (off & 0x20) >> (5 - 2);
230 grub_uint16_t imm4 = (off & 0x10) << (12 - 5);
231 grub_uint16_t imm3_1 = (off & 0xe) << (12 - 10);
232 *abs_place = ((*abs_place & 0xe003)
233 | imm11 | imm10 | imm9_8 | imm7 | imm6
234 | imm5 | imm4 | imm3_1);
235 }
236 break;
237
238 case R_RISCV_PCREL_HI20:
239 {
240 grub_uint32_t *abs_place = place;
241 grub_ssize_t off = sym_addr - (grub_addr_t) place;
242 grub_int32_t hi20;
243
244 if (off != (grub_int32_t)off)
245 return grub_error (GRUB_ERR_BAD_MODULE, "relocation overflow");
246
247 hi20 = (off + 0x800) & 0xfffff000;
248 *abs_place = (*abs_place & 0xfff) | hi20;
249 }
250 break;
251
252 case R_RISCV_PCREL_LO12_I:
253 case R_RISCV_PCREL_LO12_S:
254 {
255 grub_uint32_t *t32 = place;
256 Elf_Rela *rel2;
257 /* Search backwards for matching HI20 reloc. */
258 for (rel2 = (Elf_Rela *) ((char *) rel - s->sh_entsize);
259 (unsigned long)rel2 >= ((unsigned long)ehdr + s->sh_offset);
260 rel2 = (Elf_Rela *) ((char *) rel2 - s->sh_entsize))
261 {
262 Elf_Addr rel2_info;
263 Elf_Addr rel2_offset;
264 Elf_Addr rel2_sym_addr;
265 Elf_Addr rel2_loc;
266 grub_ssize_t rel2_off;
267 grub_ssize_t off;
268 Elf_Sym *sym2;
269
270 rel2_offset = rel2->r_offset;
271 rel2_info = rel2->r_info;
272 rel2_loc = (grub_addr_t) seg->addr + rel2_offset;
273
274 if (ELF_R_TYPE (rel2_info) == R_RISCV_PCREL_HI20
275 && rel2_loc == sym_addr)
276 {
277 sym2 = (Elf_Sym *) ((char *) mod->symtab
278 + mod->symsize * ELF_R_SYM (rel2->r_info));
279 rel2_sym_addr = sym2->st_value;
280 if (s->sh_type == SHT_RELA)
281 rel2_sym_addr += ((Elf_Rela *) rel2)->r_addend;
282
283 rel2_off = rel2_sym_addr - rel2_loc;
284 off = rel2_off - ((rel2_off + 0x800) & 0xfffff000);
285
286 if (ELF_R_TYPE (rel->r_info) == R_RISCV_PCREL_LO12_I)
287 *t32 = (*t32 & 0xfffff) | (off & 0xfff) << 20;
288 else
289 {
290 grub_uint32_t imm11_5 = (off & 0xfe0) << (31 - 11);
291 grub_uint32_t imm4_0 = (off & 0x1f) << (11 - 4);
292 *t32 = (*t32 & 0x1fff07f) | imm11_5 | imm4_0;
293 }
294 break;
295 }
296 }
297 if ((unsigned long)rel2 < ((unsigned long)ehdr + s->sh_offset))
298 return grub_error (GRUB_ERR_BAD_MODULE, "cannot find matching HI20 relocation");
299 }
300 break;
301
302 case R_RISCV_HI20:
303 {
304 grub_uint32_t *abs_place = place;
305 *abs_place = (*abs_place & 0xfff) |
306 (((grub_int32_t) sym_addr + 0x800) & 0xfffff000);
307 }
308 break;
309
310 case R_RISCV_LO12_I:
311 {
312 grub_uint32_t *abs_place = place;
313 grub_int32_t lo12 = (grub_int32_t) sym_addr -
314 (((grub_int32_t) sym_addr + 0x800) & 0xfffff000);
315 *abs_place = (*abs_place & 0xfffff) | ((lo12 & 0xfff) << 20);
316 }
317 break;
318
319 case R_RISCV_LO12_S:
320 {
321 grub_uint32_t *abs_place = place;
322 grub_int32_t lo12 = (grub_int32_t) sym_addr -
323 (((grub_int32_t) sym_addr + 0x800) & 0xfffff000);
324 grub_uint32_t imm11_5 = (lo12 & 0xfe0) << (31 - 11);
325 grub_uint32_t imm4_0 = (lo12 & 0x1f) << (11 - 4);
326 *abs_place = (*abs_place & 0x1fff07f) | imm11_5 | imm4_0;
327 }
328 break;
329
330 case R_RISCV_RELAX:
331 break;
332 default:
333 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
334 N_("relocation 0x%x is not implemented yet"),
335 ELF_R_TYPE (rel->r_info));
336 }
337 }
338
339 return GRUB_ERR_NONE;
340 }