elf.c revision 1.9 1 /* $NetBSD: elf.c,v 1.9 2017/11/09 15:56:56 maxv Exp $ */
2
3 /*
4 * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Maxime Villard.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #define ELFSIZE 64
32
33 #include "prekern.h"
34 #include <sys/exec_elf.h>
35
36 struct elfinfo {
37 Elf_Ehdr *ehdr;
38 Elf_Shdr *shdr;
39 char *shstrtab;
40 size_t shstrsz;
41 Elf_Sym *symtab;
42 size_t symcnt;
43 char *strtab;
44 size_t strsz;
45 };
46
47 extern paddr_t kernpa_start, kernpa_end;
48
49 static struct elfinfo eif;
50 static const char entrypoint[] = "start_prekern";
51
52 static int
53 elf_check_header()
54 {
55 if (memcmp((char *)eif.ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
56 eif.ehdr->e_ident[EI_CLASS] != ELFCLASS ||
57 eif.ehdr->e_type != ET_REL) {
58 return -1;
59 }
60 return 0;
61 }
62
63 static vaddr_t
64 elf_get_entrypoint()
65 {
66 Elf_Sym *sym;
67 size_t i;
68 char *buf;
69
70 for (i = 0; i < eif.symcnt; i++) {
71 sym = &eif.symtab[i];
72
73 if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
74 continue;
75 if (sym->st_name == 0)
76 continue;
77 if (sym->st_shndx == SHN_UNDEF)
78 continue; /* Skip external references */
79 buf = eif.strtab + sym->st_name;
80
81 if (!memcmp(buf, entrypoint, sizeof(entrypoint))) {
82 return (vaddr_t)sym->st_value;
83 }
84 }
85
86 return 0;
87 }
88
89 static Elf_Shdr *
90 elf_find_section(char *name)
91 {
92 char *buf;
93 size_t i;
94
95 for (i = 0; i < eif.ehdr->e_shnum; i++) {
96 if (eif.shdr[i].sh_name == 0) {
97 continue;
98 }
99 buf = eif.shstrtab + eif.shdr[i].sh_name;
100 if (!strcmp(name, buf)) {
101 return &eif.shdr[i];
102 }
103 }
104
105 return NULL;
106 }
107
108 static uintptr_t
109 elf_sym_lookup(size_t symidx)
110 {
111 const Elf_Sym *sym;
112 char *buf, *secname;
113 Elf_Shdr *sec;
114
115 if (symidx == STN_UNDEF) {
116 return 0;
117 }
118
119 if (symidx >= eif.symcnt) {
120 fatal("elf_sym_lookup: symbol beyond table");
121 }
122 sym = &eif.symtab[symidx];
123 buf = eif.strtab + sym->st_name;
124
125 if (sym->st_shndx == SHN_UNDEF) {
126 if (!memcmp(buf, "__start_link_set", 16)) {
127 secname = buf + 8;
128 sec = elf_find_section(secname);
129 if (sec == NULL) {
130 fatal("elf_sym_lookup: unknown start link set");
131 }
132 return (uintptr_t)((uint8_t *)eif.ehdr +
133 sec->sh_offset);
134 }
135 if (!memcmp(buf, "__stop_link_set", 15)) {
136 secname = buf + 7;
137 sec = elf_find_section(secname);
138 if (sec == NULL) {
139 fatal("elf_sym_lookup: unknown stop link set");
140 }
141 return (uintptr_t)((uint8_t *)eif.ehdr +
142 sec->sh_offset + sec->sh_size);
143 }
144
145 fatal("elf_sym_lookup: external symbol");
146 }
147 if (sym->st_value == 0) {
148 fatal("elf_sym_lookup: zero value");
149 }
150 return (uintptr_t)sym->st_value;
151 }
152
153 static void
154 elf_apply_reloc(uintptr_t relocbase, const void *data, bool isrela)
155 {
156 Elf64_Addr *where, val;
157 Elf32_Addr *where32, val32;
158 Elf64_Addr addr;
159 Elf64_Addr addend;
160 uintptr_t rtype, symidx;
161 const Elf_Rel *rel;
162 const Elf_Rela *rela;
163
164 if (isrela) {
165 rela = (const Elf_Rela *)data;
166 where = (Elf64_Addr *)(relocbase + rela->r_offset);
167 addend = rela->r_addend;
168 rtype = ELF_R_TYPE(rela->r_info);
169 symidx = ELF_R_SYM(rela->r_info);
170 } else {
171 rel = (const Elf_Rel *)data;
172 where = (Elf64_Addr *)(relocbase + rel->r_offset);
173 rtype = ELF_R_TYPE(rel->r_info);
174 symidx = ELF_R_SYM(rel->r_info);
175 /* Addend is 32 bit on 32 bit relocs */
176 switch (rtype) {
177 case R_X86_64_PC32:
178 case R_X86_64_32:
179 case R_X86_64_32S:
180 addend = *(Elf32_Addr *)where;
181 break;
182 default:
183 addend = *where;
184 break;
185 }
186 }
187
188 switch (rtype) {
189 case R_X86_64_NONE: /* none */
190 break;
191
192 case R_X86_64_64: /* S + A */
193 addr = elf_sym_lookup(symidx);
194 val = addr + addend;
195 *where = val;
196 break;
197
198 case R_X86_64_PC32: /* S + A - P */
199 addr = elf_sym_lookup(symidx);
200 where32 = (Elf32_Addr *)where;
201 val32 = (Elf32_Addr)(addr + addend - (Elf64_Addr)where);
202 *where32 = val32;
203 break;
204
205 case R_X86_64_32: /* S + A */
206 case R_X86_64_32S: /* S + A sign extend */
207 addr = elf_sym_lookup(symidx);
208 val32 = (Elf32_Addr)(addr + addend);
209 where32 = (Elf32_Addr *)where;
210 *where32 = val32;
211 break;
212
213 case R_X86_64_GLOB_DAT: /* S */
214 case R_X86_64_JUMP_SLOT:/* XXX need addend + offset */
215 addr = elf_sym_lookup(symidx);
216 *where = addr;
217 break;
218
219 case R_X86_64_RELATIVE: /* B + A */
220 addr = relocbase + addend;
221 val = addr;
222 *where = val;
223 break;
224
225 default:
226 fatal("elf_apply_reloc: unexpected relocation type");
227 }
228 }
229
230 /* -------------------------------------------------------------------------- */
231
232 size_t
233 elf_get_head_size(vaddr_t headva)
234 {
235 Elf_Ehdr *ehdr;
236 Elf_Shdr *shdr;
237 size_t size;
238
239 ehdr = (Elf_Ehdr *)headva;
240 shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff);
241
242 size = (vaddr_t)shdr + (vaddr_t)(ehdr->e_shnum * sizeof(Elf_Shdr)) -
243 (vaddr_t)ehdr;
244
245 return roundup(size, PAGE_SIZE);
246 }
247
248 void
249 elf_build_head(vaddr_t headva)
250 {
251 memset(&eif, 0, sizeof(struct elfinfo));
252
253 eif.ehdr = (Elf_Ehdr *)headva;
254 eif.shdr = (Elf_Shdr *)((uint8_t *)eif.ehdr + eif.ehdr->e_shoff);
255
256 if (elf_check_header() == -1) {
257 fatal("elf_build_head: wrong kernel ELF header");
258 }
259 }
260
261 static bool
262 elf_section_is_text(Elf_Shdr *shdr)
263 {
264 if (shdr->sh_type != SHT_NOBITS &&
265 shdr->sh_type != SHT_PROGBITS) {
266 return false;
267 }
268 if (!(shdr->sh_flags & SHF_EXECINSTR)) {
269 return false;
270 }
271 return true;
272 }
273
274 static bool
275 elf_section_is_rodata(Elf_Shdr *shdr)
276 {
277 if (shdr->sh_type != SHT_NOBITS &&
278 shdr->sh_type != SHT_PROGBITS) {
279 return false;
280 }
281 if (shdr->sh_flags & (SHF_EXECINSTR|SHF_WRITE)) {
282 return false;
283 }
284 return true;
285 }
286
287 static bool
288 elf_section_is_data(Elf_Shdr *shdr)
289 {
290 if (shdr->sh_type != SHT_NOBITS &&
291 shdr->sh_type != SHT_PROGBITS) {
292 return false;
293 }
294 if (!(shdr->sh_flags & SHF_WRITE) ||
295 (shdr->sh_flags & SHF_EXECINSTR)) {
296 return false;
297 }
298 return true;
299 }
300
301 void
302 elf_get_text(paddr_t *pa, size_t *sz)
303 {
304 const paddr_t basepa = kernpa_start;
305 paddr_t minpa, maxpa, secpa;
306 size_t i, secsz;
307
308 minpa = 0xFFFFFFFFFFFFFFFF, maxpa = 0;
309 for (i = 0; i < eif.ehdr->e_shnum; i++) {
310 if (!elf_section_is_text(&eif.shdr[i])) {
311 continue;
312 }
313 secpa = basepa + eif.shdr[i].sh_offset;
314 secsz = eif.shdr[i].sh_size;
315 if (secpa < minpa) {
316 minpa = secpa;
317 }
318 if (secpa + secsz > maxpa) {
319 maxpa = secpa + secsz;
320 }
321 }
322 ASSERT(minpa % PAGE_SIZE == 0);
323
324 *pa = minpa;
325 *sz = maxpa - minpa;
326 }
327
328 void
329 elf_build_text(vaddr_t textva, paddr_t textpa)
330 {
331 const paddr_t basepa = kernpa_start;
332 const vaddr_t headva = (vaddr_t)eif.ehdr;
333 size_t i, offtext;
334
335 for (i = 0; i < eif.ehdr->e_shnum; i++) {
336 if (!elf_section_is_text(&eif.shdr[i])) {
337 continue;
338 }
339
340 /* Offset of the section within the text segment. */
341 offtext = basepa + eif.shdr[i].sh_offset - textpa;
342
343 /* We want (headva + sh_offset) to be the VA of the section. */
344 eif.shdr[i].sh_offset = (textva + offtext - headva);
345 }
346 }
347
348 void
349 elf_get_rodata(paddr_t *pa, size_t *sz)
350 {
351 const paddr_t basepa = kernpa_start;
352 paddr_t minpa, maxpa, secpa;
353 size_t i, secsz;
354
355 minpa = 0xFFFFFFFFFFFFFFFF, maxpa = 0;
356 for (i = 0; i < eif.ehdr->e_shnum; i++) {
357 if (!elf_section_is_rodata(&eif.shdr[i])) {
358 continue;
359 }
360 secpa = basepa + eif.shdr[i].sh_offset;
361 secsz = eif.shdr[i].sh_size;
362 if (secpa < minpa) {
363 minpa = secpa;
364 }
365 if (secpa + secsz > maxpa) {
366 maxpa = secpa + secsz;
367 }
368 }
369 ASSERT(minpa % PAGE_SIZE == 0);
370
371 *pa = minpa;
372 *sz = maxpa - minpa;
373 }
374
375 void
376 elf_build_rodata(vaddr_t rodatava, paddr_t rodatapa)
377 {
378 const paddr_t basepa = kernpa_start;
379 const vaddr_t headva = (vaddr_t)eif.ehdr;
380 size_t i, offrodata;
381
382 for (i = 0; i < eif.ehdr->e_shnum; i++) {
383 if (!elf_section_is_rodata(&eif.shdr[i])) {
384 continue;
385 }
386
387 /* Offset of the section within the rodata segment. */
388 offrodata = basepa + eif.shdr[i].sh_offset - rodatapa;
389
390 /* We want (headva + sh_offset) to be the VA of the section. */
391 eif.shdr[i].sh_offset = (rodatava + offrodata - headva);
392 }
393 }
394
395 void
396 elf_get_data(paddr_t *pa, size_t *sz)
397 {
398 const paddr_t basepa = kernpa_start;
399 paddr_t minpa, maxpa, secpa;
400 size_t i, secsz;
401
402 minpa = 0xFFFFFFFFFFFFFFFF, maxpa = 0;
403 for (i = 0; i < eif.ehdr->e_shnum; i++) {
404 if (!elf_section_is_data(&eif.shdr[i])) {
405 continue;
406 }
407 secpa = basepa + eif.shdr[i].sh_offset;
408 secsz = eif.shdr[i].sh_size;
409 if (secpa < minpa) {
410 minpa = secpa;
411 }
412 if (secpa + secsz > maxpa) {
413 maxpa = secpa + secsz;
414 }
415 }
416 ASSERT(minpa % PAGE_SIZE == 0);
417
418 *pa = minpa;
419 *sz = maxpa - minpa;
420 }
421
422 void
423 elf_build_data(vaddr_t datava, paddr_t datapa)
424 {
425 const paddr_t basepa = kernpa_start;
426 const vaddr_t headva = (vaddr_t)eif.ehdr;
427 size_t i, offdata;
428
429 for (i = 0; i < eif.ehdr->e_shnum; i++) {
430 if (!elf_section_is_data(&eif.shdr[i])) {
431 continue;
432 }
433
434 /* Offset of the section within the data segment. */
435 offdata = basepa + eif.shdr[i].sh_offset - datapa;
436
437 /* We want (headva + sh_offset) to be the VA of the section. */
438 eif.shdr[i].sh_offset = (datava + offdata - headva);
439 }
440 }
441
442 void
443 elf_build_boot(vaddr_t bootva, paddr_t bootpa)
444 {
445 const paddr_t basepa = kernpa_start;
446 const vaddr_t headva = (vaddr_t)eif.ehdr;
447 size_t i, j, offboot;
448
449 for (i = 0; i < eif.ehdr->e_shnum; i++) {
450 if (eif.shdr[i].sh_type != SHT_STRTAB &&
451 eif.shdr[i].sh_type != SHT_REL &&
452 eif.shdr[i].sh_type != SHT_RELA &&
453 eif.shdr[i].sh_type != SHT_SYMTAB) {
454 continue;
455 }
456 if (eif.shdr[i].sh_offset == 0) {
457 /* hasn't been loaded */
458 continue;
459 }
460
461 /* Offset of the section within the boot region. */
462 offboot = basepa + eif.shdr[i].sh_offset - bootpa;
463
464 /* We want (headva + sh_offset) to be the VA of the region. */
465 eif.shdr[i].sh_offset = (bootva + offboot - headva);
466 }
467
468 /* Locate the section names */
469 j = eif.ehdr->e_shstrndx;
470 if (j == SHN_UNDEF) {
471 fatal("elf_build_boot: shstrtab not found");
472 }
473 if (j >= eif.ehdr->e_shnum) {
474 fatal("elf_build_boot: wrong shstrtab index");
475 }
476 eif.shstrtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
477 eif.shstrsz = eif.shdr[j].sh_size;
478
479 /* Locate the symbol table */
480 for (i = 0; i < eif.ehdr->e_shnum; i++) {
481 if (eif.shdr[i].sh_type == SHT_SYMTAB)
482 break;
483 }
484 if (i == eif.ehdr->e_shnum) {
485 fatal("elf_build_boot: symtab not found");
486 }
487 eif.symtab = (Elf_Sym *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
488 eif.symcnt = eif.shdr[i].sh_size / sizeof(Elf_Sym);
489
490 /* Also locate the string table */
491 j = eif.shdr[i].sh_link;
492 if (j == SHN_UNDEF || j >= eif.ehdr->e_shnum) {
493 fatal("elf_build_boot: wrong strtab index");
494 }
495 if (eif.shdr[j].sh_type != SHT_STRTAB) {
496 fatal("elf_build_boot: wrong strtab type");
497 }
498 eif.strtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
499 eif.strsz = eif.shdr[j].sh_size;
500 }
501
502 vaddr_t
503 elf_kernel_reloc()
504 {
505 const vaddr_t baseva = (vaddr_t)eif.ehdr;
506 vaddr_t secva, ent;
507 Elf_Sym *sym;
508 size_t i, j;
509
510 print_state(true, "ELF info created");
511
512 /*
513 * The loaded sections are: SHT_PROGBITS, SHT_NOBITS, SHT_STRTAB,
514 * SHT_SYMTAB.
515 */
516
517 /*
518 * Update all symbol values with the appropriate offset.
519 */
520 for (i = 0; i < eif.ehdr->e_shnum; i++) {
521 if (eif.shdr[i].sh_type != SHT_NOBITS &&
522 eif.shdr[i].sh_type != SHT_PROGBITS) {
523 continue;
524 }
525 secva = baseva + eif.shdr[i].sh_offset;
526 for (j = 0; j < eif.symcnt; j++) {
527 sym = &eif.symtab[j];
528 if (sym->st_shndx != i) {
529 continue;
530 }
531 sym->st_value += (Elf_Addr)secva;
532 }
533 }
534
535 print_state(true, "Symbol values updated");
536
537 /*
538 * Perform relocations without addend if there are any.
539 */
540 for (i = 0; i < eif.ehdr->e_shnum; i++) {
541 Elf_Rel *reltab, *rel;
542 size_t secidx, nrel;
543 uintptr_t base;
544
545 if (eif.shdr[i].sh_type != SHT_REL)
546 continue;
547
548 reltab = (Elf_Rel *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
549 nrel = eif.shdr[i].sh_size / sizeof(Elf_Rel);
550
551 secidx = eif.shdr[i].sh_info;
552 if (secidx >= eif.ehdr->e_shnum) {
553 fatal("elf_kernel_reloc: wrong REL relocation");
554 }
555 base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;
556
557 for (j = 0; j < nrel; j++) {
558 rel = &reltab[j];
559 elf_apply_reloc(base, rel, false);
560 }
561 }
562
563 print_state(true, "REL relocations applied");
564
565 /*
566 * Perform relocations with addend if there are any.
567 */
568 for (i = 0; i < eif.ehdr->e_shnum; i++) {
569 Elf_Rela *relatab, *rela;
570 size_t secidx, nrela;
571 uintptr_t base;
572
573 if (eif.shdr[i].sh_type != SHT_RELA)
574 continue;
575
576 relatab = (Elf_Rela *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
577 nrela = eif.shdr[i].sh_size / sizeof(Elf_Rela);
578
579 secidx = eif.shdr[i].sh_info;
580 if (secidx >= eif.ehdr->e_shnum) {
581 fatal("elf_kernel_reloc: wrong RELA relocation");
582 }
583 base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;
584
585 for (j = 0; j < nrela; j++) {
586 rela = &relatab[j];
587 elf_apply_reloc(base, rela, true);
588 }
589 }
590
591 print_state(true, "RELA relocations applied");
592
593 /*
594 * Get the entry point.
595 */
596 ent = elf_get_entrypoint(&eif);
597 if (ent == 0) {
598 fatal("elf_kernel_reloc: entry point not found");
599 }
600
601 print_state(true, "Entry point found");
602
603 return ent;
604 }
605
606