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