Home | History | Annotate | Line # | Download | only in prekern
elf.c revision 1.19
      1 /*	$NetBSD: elf.c,v 1.19 2020/05/05 19:26:47 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(void)
     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(void)
     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 	case R_X86_64_PLT32:
    200 		addr = elf_sym_lookup(symidx);
    201 		where32 = (Elf32_Addr *)where;
    202 		val32 = (Elf32_Addr)(addr + addend - (Elf64_Addr)where);
    203 		*where32 = val32;
    204 		break;
    205 
    206 	case R_X86_64_32:	/* S + A */
    207 	case R_X86_64_32S:	/* S + A sign extend */
    208 		addr = elf_sym_lookup(symidx);
    209 		val32 = (Elf32_Addr)(addr + addend);
    210 		where32 = (Elf32_Addr *)where;
    211 		*where32 = val32;
    212 		break;
    213 
    214 	case R_X86_64_GLOB_DAT:	/* S */
    215 	case R_X86_64_JUMP_SLOT:/* XXX need addend + offset */
    216 		addr = elf_sym_lookup(symidx);
    217 		*where = addr;
    218 		break;
    219 
    220 	case R_X86_64_RELATIVE:	/* B + A */
    221 		addr = relocbase + addend;
    222 		val = addr;
    223 		*where = val;
    224 		break;
    225 
    226 	default:
    227 		fatal("elf_apply_reloc: unexpected relocation type");
    228 	}
    229 }
    230 
    231 /* -------------------------------------------------------------------------- */
    232 
    233 size_t
    234 elf_get_head_size(vaddr_t headva)
    235 {
    236 	Elf_Ehdr *ehdr;
    237 	Elf_Shdr *shdr;
    238 	size_t size;
    239 
    240 	ehdr = (Elf_Ehdr *)headva;
    241 	shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff);
    242 
    243 	size = (vaddr_t)shdr + (vaddr_t)(ehdr->e_shnum * sizeof(Elf_Shdr)) -
    244 	    (vaddr_t)ehdr;
    245 
    246 	return roundup(size, PAGE_SIZE);
    247 }
    248 
    249 void
    250 elf_build_head(vaddr_t headva)
    251 {
    252 	memset(&eif, 0, sizeof(struct elfinfo));
    253 
    254 	eif.ehdr = (Elf_Ehdr *)headva;
    255 	eif.shdr = (Elf_Shdr *)((uint8_t *)eif.ehdr + eif.ehdr->e_shoff);
    256 
    257 	if (elf_check_header() == -1) {
    258 		fatal("elf_build_head: wrong kernel ELF header");
    259 	}
    260 }
    261 
    262 static bool
    263 elf_section_mappable(Elf_Shdr *shdr)
    264 {
    265 	if (!(shdr->sh_flags & SHF_ALLOC)) {
    266 		return false;
    267 	}
    268 	if (shdr->sh_type != SHT_NOBITS &&
    269 	    shdr->sh_type != SHT_PROGBITS) {
    270 		return false;
    271 	}
    272 	return true;
    273 }
    274 
    275 void
    276 elf_map_sections(void)
    277 {
    278 	const paddr_t basepa = kernpa_start;
    279 	const vaddr_t headva = (vaddr_t)eif.ehdr;
    280 	Elf_Shdr *shdr;
    281 	int segtype;
    282 	vaddr_t secva;
    283 	paddr_t secpa;
    284 	size_t i, secsz, secalign;
    285 
    286 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
    287 		shdr = &eif.shdr[i];
    288 
    289 		if (!elf_section_mappable(shdr)) {
    290 			continue;
    291 		}
    292 
    293 		if (shdr->sh_flags & SHF_EXECINSTR) {
    294 			segtype = BTSEG_TEXT;
    295 		} else if (shdr->sh_flags & SHF_WRITE) {
    296 			segtype = BTSEG_DATA;
    297 		} else {
    298 			segtype = BTSEG_RODATA;
    299 		}
    300 		secpa = basepa + shdr->sh_offset;
    301 		secsz = shdr->sh_size;
    302 		secalign = shdr->sh_addralign;
    303 		ASSERT(shdr->sh_offset != 0);
    304 		ASSERT(secpa % PAGE_SIZE == 0);
    305 		ASSERT(secpa + secsz <= kernpa_end);
    306 
    307 		secva = mm_map_segment(segtype, secpa, secsz, secalign);
    308 
    309 		/* We want (headva + sh_offset) to be the VA of the section. */
    310 		ASSERT(secva > headva);
    311 		shdr->sh_offset = secva - headva;
    312 	}
    313 }
    314 
    315 void
    316 elf_build_boot(vaddr_t bootva, paddr_t bootpa)
    317 {
    318 	const paddr_t basepa = kernpa_start;
    319 	const vaddr_t headva = (vaddr_t)eif.ehdr;
    320 	size_t i, j, offboot;
    321 
    322 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
    323 		if (eif.shdr[i].sh_type != SHT_STRTAB &&
    324 		    eif.shdr[i].sh_type != SHT_REL &&
    325 		    eif.shdr[i].sh_type != SHT_RELA &&
    326 		    eif.shdr[i].sh_type != SHT_SYMTAB) {
    327 			continue;
    328 		}
    329 		if (eif.shdr[i].sh_offset == 0) {
    330 			/* hasn't been loaded */
    331 			continue;
    332 		}
    333 
    334 		/* Offset of the section within the boot region. */
    335 		offboot = basepa + eif.shdr[i].sh_offset - bootpa;
    336 
    337 		/* We want (headva + sh_offset) to be the VA of the region. */
    338 		eif.shdr[i].sh_offset = (bootva + offboot - headva);
    339 	}
    340 
    341 	/* Locate the section names */
    342 	j = eif.ehdr->e_shstrndx;
    343 	if (j == SHN_UNDEF) {
    344 		fatal("elf_build_boot: shstrtab not found");
    345 	}
    346 	if (j >= eif.ehdr->e_shnum) {
    347 		fatal("elf_build_boot: wrong shstrtab index");
    348 	}
    349 	eif.shstrtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
    350 	eif.shstrsz = eif.shdr[j].sh_size;
    351 
    352 	/* Locate the symbol table */
    353 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
    354 		if (eif.shdr[i].sh_type == SHT_SYMTAB)
    355 			break;
    356 	}
    357 	if (i == eif.ehdr->e_shnum) {
    358 		fatal("elf_build_boot: symtab not found");
    359 	}
    360 	if (eif.shdr[i].sh_offset == 0) {
    361 		fatal("elf_build_boot: symtab not loaded");
    362 	}
    363 	eif.symtab = (Elf_Sym *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
    364 	eif.symcnt = eif.shdr[i].sh_size / sizeof(Elf_Sym);
    365 
    366 	/* Also locate the string table */
    367 	j = eif.shdr[i].sh_link;
    368 	if (j == SHN_UNDEF || j >= eif.ehdr->e_shnum) {
    369 		fatal("elf_build_boot: wrong strtab index");
    370 	}
    371 	if (eif.shdr[j].sh_type != SHT_STRTAB) {
    372 		fatal("elf_build_boot: wrong strtab type");
    373 	}
    374 	if (eif.shdr[j].sh_offset == 0) {
    375 		fatal("elf_build_boot: strtab not loaded");
    376 	}
    377 	eif.strtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
    378 	eif.strsz = eif.shdr[j].sh_size;
    379 }
    380 
    381 vaddr_t
    382 elf_kernel_reloc(void)
    383 {
    384 	const vaddr_t baseva = (vaddr_t)eif.ehdr;
    385 	vaddr_t secva, ent;
    386 	Elf_Sym *sym;
    387 	size_t i, j;
    388 
    389 	print_state(true, "ELF info created");
    390 
    391 	/*
    392 	 * Update all symbol values with the appropriate offset.
    393 	 */
    394 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
    395 		if (!elf_section_mappable(&eif.shdr[i])) {
    396 			continue;
    397 		}
    398 
    399 		ASSERT(eif.shdr[i].sh_offset != 0);
    400 		secva = baseva + eif.shdr[i].sh_offset;
    401 		for (j = 0; j < eif.symcnt; j++) {
    402 			sym = &eif.symtab[j];
    403 			if (sym->st_shndx != i) {
    404 				continue;
    405 			}
    406 			sym->st_value += (Elf_Addr)secva;
    407 		}
    408 	}
    409 
    410 	print_state(true, "Symbol values updated");
    411 
    412 	/*
    413 	 * Perform relocations without addend if there are any.
    414 	 */
    415 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
    416 		Elf_Rel *reltab, *rel;
    417 		size_t secidx, nrel;
    418 		uintptr_t base;
    419 
    420 		if (eif.shdr[i].sh_type != SHT_REL) {
    421 			continue;
    422 		}
    423 		ASSERT(eif.shdr[i].sh_offset != 0);
    424 		reltab = (Elf_Rel *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
    425 		nrel = eif.shdr[i].sh_size / sizeof(Elf_Rel);
    426 
    427 		secidx = eif.shdr[i].sh_info;
    428 		if (secidx >= eif.ehdr->e_shnum) {
    429 			fatal("elf_kernel_reloc: REL sh_info is malformed");
    430 		}
    431 		if (!elf_section_mappable(&eif.shdr[secidx])) {
    432 			fatal("elf_kernel_reloc: REL sh_info not mappable");
    433 		}
    434 		base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;
    435 
    436 		for (j = 0; j < nrel; j++) {
    437 			rel = &reltab[j];
    438 			elf_apply_reloc(base, rel, false);
    439 		}
    440 	}
    441 
    442 	print_state(true, "REL relocations applied");
    443 
    444 	/*
    445 	 * Perform relocations with addend if there are any.
    446 	 */
    447 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
    448 		Elf_Rela *relatab, *rela;
    449 		size_t secidx, nrela;
    450 		uintptr_t base;
    451 
    452 		if (eif.shdr[i].sh_type != SHT_RELA) {
    453 			continue;
    454 		}
    455 		ASSERT(eif.shdr[i].sh_offset != 0);
    456 		relatab = (Elf_Rela *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
    457 		nrela = eif.shdr[i].sh_size / sizeof(Elf_Rela);
    458 
    459 		secidx = eif.shdr[i].sh_info;
    460 		if (secidx >= eif.ehdr->e_shnum) {
    461 			fatal("elf_kernel_reloc: RELA sh_info is malformed");
    462 		}
    463 		if (!elf_section_mappable(&eif.shdr[secidx])) {
    464 			fatal("elf_kernel_reloc: RELA sh_info not mappable");
    465 		}
    466 		base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;
    467 
    468 		for (j = 0; j < nrela; j++) {
    469 			rela = &relatab[j];
    470 			elf_apply_reloc(base, rela, true);
    471 		}
    472 	}
    473 
    474 	print_state(true, "RELA relocations applied");
    475 
    476 	/*
    477 	 * Get the entry point.
    478 	 */
    479 	ent = elf_get_entrypoint();
    480 	if (ent == 0) {
    481 		fatal("elf_kernel_reloc: entry point not found");
    482 	}
    483 
    484 	print_state(true, "Entry point found");
    485 
    486 	return ent;
    487 }
    488