1*dadf0eefSkhorben /* $NetBSD: elf.c,v 1.22 2021/05/04 21:09:16 khorben Exp $ */
2c9759921Smaxv
3c9759921Smaxv /*
446c4386aSmaxv * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
5c9759921Smaxv *
6c9759921Smaxv * This code is derived from software contributed to The NetBSD Foundation
7c9759921Smaxv * by Maxime Villard.
8c9759921Smaxv *
9c9759921Smaxv * Redistribution and use in source and binary forms, with or without
10c9759921Smaxv * modification, are permitted provided that the following conditions
11c9759921Smaxv * are met:
12c9759921Smaxv * 1. Redistributions of source code must retain the above copyright
13c9759921Smaxv * notice, this list of conditions and the following disclaimer.
14c9759921Smaxv * 2. Redistributions in binary form must reproduce the above copyright
15c9759921Smaxv * notice, this list of conditions and the following disclaimer in the
16c9759921Smaxv * documentation and/or other materials provided with the distribution.
17c9759921Smaxv *
18c9759921Smaxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19c9759921Smaxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20c9759921Smaxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21c9759921Smaxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22c9759921Smaxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23c9759921Smaxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24c9759921Smaxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25c9759921Smaxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26c9759921Smaxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27c9759921Smaxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28c9759921Smaxv * POSSIBILITY OF SUCH DAMAGE.
29c9759921Smaxv */
30c9759921Smaxv
31c9759921Smaxv #define ELFSIZE 64
32c9759921Smaxv
33c9759921Smaxv #include "prekern.h"
34c9759921Smaxv #include <sys/exec_elf.h>
35c9759921Smaxv
36c9759921Smaxv struct elfinfo {
37c9759921Smaxv Elf_Ehdr *ehdr;
38c9759921Smaxv Elf_Shdr *shdr;
39c9759921Smaxv char *shstrtab;
40c9759921Smaxv size_t shstrsz;
41c9759921Smaxv Elf_Sym *symtab;
42c9759921Smaxv size_t symcnt;
43c9759921Smaxv char *strtab;
44c9759921Smaxv size_t strsz;
45c9759921Smaxv };
46c9759921Smaxv
47569f0c08Smaxv extern paddr_t kernpa_start, kernpa_end;
48569f0c08Smaxv
49c9759921Smaxv static struct elfinfo eif;
50c9759921Smaxv static const char entrypoint[] = "start_prekern";
51c9759921Smaxv
52c9759921Smaxv static int
elf_check_header(void)5392fe5cfbSmaxv elf_check_header(void)
54c9759921Smaxv {
55c9759921Smaxv if (memcmp((char *)eif.ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
56eb4d6839Smaxv eif.ehdr->e_ident[EI_CLASS] != ELFCLASS ||
57eb4d6839Smaxv eif.ehdr->e_type != ET_REL) {
58c9759921Smaxv return -1;
59c9759921Smaxv }
60c9759921Smaxv return 0;
61c9759921Smaxv }
62c9759921Smaxv
6346c4386aSmaxv static bool
elf_section_mappable(Elf_Shdr * shdr)6446c4386aSmaxv elf_section_mappable(Elf_Shdr *shdr)
6546c4386aSmaxv {
6646c4386aSmaxv if (!(shdr->sh_flags & SHF_ALLOC)) {
6746c4386aSmaxv return false;
6846c4386aSmaxv }
6946c4386aSmaxv if (shdr->sh_type != SHT_NOBITS &&
7046c4386aSmaxv shdr->sh_type != SHT_PROGBITS) {
7146c4386aSmaxv return false;
7246c4386aSmaxv }
7346c4386aSmaxv return true;
7446c4386aSmaxv }
7546c4386aSmaxv
7646c4386aSmaxv static bool
elf_can_drop_unmappable(Elf_Shdr * shdr)7746c4386aSmaxv elf_can_drop_unmappable(Elf_Shdr *shdr)
7846c4386aSmaxv {
7946c4386aSmaxv /*
8046c4386aSmaxv * We found relocations from the section 'shdr' towards the rest of
8146c4386aSmaxv * the binary, but 'shdr' is not mapped. Decide whether to skip the
8246c4386aSmaxv * relocations from this section.
8346c4386aSmaxv *
8446c4386aSmaxv * We skip only if it is a note. It means that we allow notes to
8546c4386aSmaxv * have relocations towards the rest of the binary, typically with
8646c4386aSmaxv * the ".note.Xen" section. Notes do not play any role at run time.
8746c4386aSmaxv *
8846c4386aSmaxv * Any section other than a note is the sign there is a design
8946c4386aSmaxv * mistake in the kernel (variables stored outside of rodata/data).
9046c4386aSmaxv */
9146c4386aSmaxv if (shdr->sh_type == SHT_NOTE) {
9246c4386aSmaxv return true;
9346c4386aSmaxv }
9446c4386aSmaxv return false;
9546c4386aSmaxv }
9646c4386aSmaxv
97c9759921Smaxv static vaddr_t
elf_get_entrypoint(void)9892fe5cfbSmaxv elf_get_entrypoint(void)
99c9759921Smaxv {
100c9759921Smaxv Elf_Sym *sym;
101c9759921Smaxv size_t i;
102c9759921Smaxv char *buf;
103c9759921Smaxv
104c9759921Smaxv for (i = 0; i < eif.symcnt; i++) {
105c9759921Smaxv sym = &eif.symtab[i];
106c9759921Smaxv
107c9759921Smaxv if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
108c9759921Smaxv continue;
109c9759921Smaxv if (sym->st_name == 0)
110c9759921Smaxv continue;
111c9759921Smaxv if (sym->st_shndx == SHN_UNDEF)
112c9759921Smaxv continue; /* Skip external references */
113c9759921Smaxv buf = eif.strtab + sym->st_name;
114c9759921Smaxv
115c9759921Smaxv if (!memcmp(buf, entrypoint, sizeof(entrypoint))) {
116c9759921Smaxv return (vaddr_t)sym->st_value;
117c9759921Smaxv }
118c9759921Smaxv }
119c9759921Smaxv
120c9759921Smaxv return 0;
121c9759921Smaxv }
122c9759921Smaxv
123c9759921Smaxv static Elf_Shdr *
elf_find_section(char * name)124c9759921Smaxv elf_find_section(char *name)
125c9759921Smaxv {
126c9759921Smaxv char *buf;
127c9759921Smaxv size_t i;
128c9759921Smaxv
129c9759921Smaxv for (i = 0; i < eif.ehdr->e_shnum; i++) {
130c9759921Smaxv if (eif.shdr[i].sh_name == 0) {
131c9759921Smaxv continue;
132c9759921Smaxv }
133c9759921Smaxv buf = eif.shstrtab + eif.shdr[i].sh_name;
134c9759921Smaxv if (!strcmp(name, buf)) {
135c9759921Smaxv return &eif.shdr[i];
136c9759921Smaxv }
137c9759921Smaxv }
138c9759921Smaxv
139c9759921Smaxv return NULL;
140c9759921Smaxv }
141c9759921Smaxv
142c9759921Smaxv static uintptr_t
elf_sym_lookup(size_t symidx)143c9759921Smaxv elf_sym_lookup(size_t symidx)
144c9759921Smaxv {
145c9759921Smaxv const Elf_Sym *sym;
146c9759921Smaxv char *buf, *secname;
147c9759921Smaxv Elf_Shdr *sec;
148c9759921Smaxv
14992f4c0ceSmaxv if (symidx == STN_UNDEF) {
15092f4c0ceSmaxv return 0;
15192f4c0ceSmaxv }
15292f4c0ceSmaxv
153c9759921Smaxv if (symidx >= eif.symcnt) {
154c9759921Smaxv fatal("elf_sym_lookup: symbol beyond table");
155c9759921Smaxv }
156c9759921Smaxv sym = &eif.symtab[symidx];
157c9759921Smaxv buf = eif.strtab + sym->st_name;
158c9759921Smaxv
159c9759921Smaxv if (sym->st_shndx == SHN_UNDEF) {
160c9759921Smaxv if (!memcmp(buf, "__start_link_set", 16)) {
161c9759921Smaxv secname = buf + 8;
162c9759921Smaxv sec = elf_find_section(secname);
163c9759921Smaxv if (sec == NULL) {
164c9759921Smaxv fatal("elf_sym_lookup: unknown start link set");
165c9759921Smaxv }
166c9759921Smaxv return (uintptr_t)((uint8_t *)eif.ehdr +
167c9759921Smaxv sec->sh_offset);
168c9759921Smaxv }
169c9759921Smaxv if (!memcmp(buf, "__stop_link_set", 15)) {
170c9759921Smaxv secname = buf + 7;
171c9759921Smaxv sec = elf_find_section(secname);
172c9759921Smaxv if (sec == NULL) {
173c9759921Smaxv fatal("elf_sym_lookup: unknown stop link set");
174c9759921Smaxv }
175c9759921Smaxv return (uintptr_t)((uint8_t *)eif.ehdr +
176c9759921Smaxv sec->sh_offset + sec->sh_size);
177c9759921Smaxv }
178c9759921Smaxv
179c9759921Smaxv fatal("elf_sym_lookup: external symbol");
180c9759921Smaxv }
18146c4386aSmaxv if (sym->st_shndx >= eif.ehdr->e_shnum) {
18246c4386aSmaxv fatal("elf_sym_lookup: st_shndx is malformed");
18346c4386aSmaxv }
18446c4386aSmaxv if (!elf_section_mappable(&eif.shdr[sym->st_shndx])) {
18546c4386aSmaxv fatal("elf_sym_lookup: st_shndx not mappable");
18646c4386aSmaxv }
187c9759921Smaxv if (sym->st_value == 0) {
188c9759921Smaxv fatal("elf_sym_lookup: zero value");
189c9759921Smaxv }
190c9759921Smaxv return (uintptr_t)sym->st_value;
191c9759921Smaxv }
192c9759921Smaxv
193c9759921Smaxv static void
elf_apply_reloc(uintptr_t relocbase,const void * data,bool isrela)194c9759921Smaxv elf_apply_reloc(uintptr_t relocbase, const void *data, bool isrela)
195c9759921Smaxv {
196c9759921Smaxv Elf64_Addr *where, val;
197c9759921Smaxv Elf32_Addr *where32, val32;
198c9759921Smaxv Elf64_Addr addr;
199c9759921Smaxv Elf64_Addr addend;
200c9759921Smaxv uintptr_t rtype, symidx;
201c9759921Smaxv const Elf_Rel *rel;
202c9759921Smaxv const Elf_Rela *rela;
203c9759921Smaxv
204c9759921Smaxv if (isrela) {
205c9759921Smaxv rela = (const Elf_Rela *)data;
206c9759921Smaxv where = (Elf64_Addr *)(relocbase + rela->r_offset);
207c9759921Smaxv addend = rela->r_addend;
208c9759921Smaxv rtype = ELF_R_TYPE(rela->r_info);
209c9759921Smaxv symidx = ELF_R_SYM(rela->r_info);
210c9759921Smaxv } else {
211c9759921Smaxv rel = (const Elf_Rel *)data;
212c9759921Smaxv where = (Elf64_Addr *)(relocbase + rel->r_offset);
213c9759921Smaxv rtype = ELF_R_TYPE(rel->r_info);
214c9759921Smaxv symidx = ELF_R_SYM(rel->r_info);
215c9759921Smaxv /* Addend is 32 bit on 32 bit relocs */
216c9759921Smaxv switch (rtype) {
217c9759921Smaxv case R_X86_64_PC32:
218c9759921Smaxv case R_X86_64_32:
219c9759921Smaxv case R_X86_64_32S:
220c9759921Smaxv addend = *(Elf32_Addr *)where;
221c9759921Smaxv break;
222c9759921Smaxv default:
223c9759921Smaxv addend = *where;
224c9759921Smaxv break;
225c9759921Smaxv }
226c9759921Smaxv }
227c9759921Smaxv
228c9759921Smaxv switch (rtype) {
229c9759921Smaxv case R_X86_64_NONE: /* none */
230c9759921Smaxv break;
231c9759921Smaxv
232c9759921Smaxv case R_X86_64_64: /* S + A */
233c9759921Smaxv addr = elf_sym_lookup(symidx);
234c9759921Smaxv val = addr + addend;
235c9759921Smaxv *where = val;
236c9759921Smaxv break;
237c9759921Smaxv
238c9759921Smaxv case R_X86_64_PC32: /* S + A - P */
239249407abSmaxv case R_X86_64_PLT32:
240c9759921Smaxv addr = elf_sym_lookup(symidx);
241c9759921Smaxv where32 = (Elf32_Addr *)where;
242c9759921Smaxv val32 = (Elf32_Addr)(addr + addend - (Elf64_Addr)where);
243c9759921Smaxv *where32 = val32;
244c9759921Smaxv break;
245c9759921Smaxv
246c9759921Smaxv case R_X86_64_32: /* S + A */
247c9759921Smaxv case R_X86_64_32S: /* S + A sign extend */
248c9759921Smaxv addr = elf_sym_lookup(symidx);
249c9759921Smaxv val32 = (Elf32_Addr)(addr + addend);
250c9759921Smaxv where32 = (Elf32_Addr *)where;
251c9759921Smaxv *where32 = val32;
252c9759921Smaxv break;
253c9759921Smaxv
254c9759921Smaxv case R_X86_64_GLOB_DAT: /* S */
255c9759921Smaxv case R_X86_64_JUMP_SLOT:/* XXX need addend + offset */
256c9759921Smaxv addr = elf_sym_lookup(symidx);
257c9759921Smaxv *where = addr;
258c9759921Smaxv break;
259c9759921Smaxv
260c9759921Smaxv case R_X86_64_RELATIVE: /* B + A */
261c9759921Smaxv addr = relocbase + addend;
262c9759921Smaxv val = addr;
263c9759921Smaxv *where = val;
264c9759921Smaxv break;
265c9759921Smaxv
266c9759921Smaxv default:
267c9759921Smaxv fatal("elf_apply_reloc: unexpected relocation type");
268c9759921Smaxv }
269c9759921Smaxv }
270c9759921Smaxv
271569f0c08Smaxv /* -------------------------------------------------------------------------- */
272569f0c08Smaxv
273569f0c08Smaxv size_t
elf_get_head_size(vaddr_t headva)274569f0c08Smaxv elf_get_head_size(vaddr_t headva)
275569f0c08Smaxv {
276569f0c08Smaxv Elf_Ehdr *ehdr;
277569f0c08Smaxv Elf_Shdr *shdr;
278569f0c08Smaxv size_t size;
279569f0c08Smaxv
280569f0c08Smaxv ehdr = (Elf_Ehdr *)headva;
281569f0c08Smaxv shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff);
282569f0c08Smaxv
283569f0c08Smaxv size = (vaddr_t)shdr + (vaddr_t)(ehdr->e_shnum * sizeof(Elf_Shdr)) -
284569f0c08Smaxv (vaddr_t)ehdr;
285569f0c08Smaxv
286569f0c08Smaxv return roundup(size, PAGE_SIZE);
287569f0c08Smaxv }
288569f0c08Smaxv
289569f0c08Smaxv void
elf_build_head(vaddr_t headva)290569f0c08Smaxv elf_build_head(vaddr_t headva)
291569f0c08Smaxv {
292569f0c08Smaxv memset(&eif, 0, sizeof(struct elfinfo));
293569f0c08Smaxv
294569f0c08Smaxv eif.ehdr = (Elf_Ehdr *)headva;
295569f0c08Smaxv eif.shdr = (Elf_Shdr *)((uint8_t *)eif.ehdr + eif.ehdr->e_shoff);
296569f0c08Smaxv
297569f0c08Smaxv if (elf_check_header() == -1) {
2982247cb6bSmaxv fatal("elf_build_head: wrong kernel ELF header");
299569f0c08Smaxv }
300569f0c08Smaxv }
301569f0c08Smaxv
302569f0c08Smaxv void
elf_fixup_boot(vaddr_t bootva,paddr_t bootpa)303d4a66c4eSmaxv elf_fixup_boot(vaddr_t bootva, paddr_t bootpa)
304d4a66c4eSmaxv {
305d4a66c4eSmaxv const paddr_t basepa = kernpa_start;
306d4a66c4eSmaxv const vaddr_t headva = (vaddr_t)eif.ehdr;
307d4a66c4eSmaxv size_t i, offboot;
308d4a66c4eSmaxv
309d4a66c4eSmaxv /*
310d4a66c4eSmaxv * Fix up the 'sh_offset' field of the REL/RELA/SYM/STR sections, which
311d4a66c4eSmaxv * are all in the "boot" region.
312d4a66c4eSmaxv */
313d4a66c4eSmaxv for (i = 0; i < eif.ehdr->e_shnum; i++) {
314d4a66c4eSmaxv if (eif.shdr[i].sh_type != SHT_STRTAB &&
315d4a66c4eSmaxv eif.shdr[i].sh_type != SHT_REL &&
316d4a66c4eSmaxv eif.shdr[i].sh_type != SHT_RELA &&
317d4a66c4eSmaxv eif.shdr[i].sh_type != SHT_SYMTAB) {
318d4a66c4eSmaxv continue;
319d4a66c4eSmaxv }
320d4a66c4eSmaxv if (eif.shdr[i].sh_offset == 0) {
321d4a66c4eSmaxv /* The bootloader dropped it. */
322d4a66c4eSmaxv continue;
323d4a66c4eSmaxv }
324d4a66c4eSmaxv
325d4a66c4eSmaxv /* Offset of the section within the boot region. */
326d4a66c4eSmaxv offboot = basepa + eif.shdr[i].sh_offset - bootpa;
327d4a66c4eSmaxv
328d4a66c4eSmaxv /* We want (headva + sh_offset) to be the VA of the region. */
329d4a66c4eSmaxv eif.shdr[i].sh_offset = (bootva + offboot - headva);
330d4a66c4eSmaxv }
331d4a66c4eSmaxv }
332d4a66c4eSmaxv
333d4a66c4eSmaxv void
elf_map_sections(void)33492fe5cfbSmaxv elf_map_sections(void)
335569f0c08Smaxv {
336569f0c08Smaxv const paddr_t basepa = kernpa_start;
337569f0c08Smaxv const vaddr_t headva = (vaddr_t)eif.ehdr;
3386ac8be24Smaxv Elf_Shdr *shdr;
3396ac8be24Smaxv int segtype;
3406ac8be24Smaxv vaddr_t secva;
3416ac8be24Smaxv paddr_t secpa;
34226e9e80dSmaxv size_t i, secsz, secalign;
343569f0c08Smaxv
344569f0c08Smaxv for (i = 0; i < eif.ehdr->e_shnum; i++) {
3456ac8be24Smaxv shdr = &eif.shdr[i];
346569f0c08Smaxv
347b3f22be2Smaxv if (!elf_section_mappable(shdr)) {
348569f0c08Smaxv continue;
349569f0c08Smaxv }
350569f0c08Smaxv
3516ac8be24Smaxv if (shdr->sh_flags & SHF_EXECINSTR) {
3526ac8be24Smaxv segtype = BTSEG_TEXT;
3536ac8be24Smaxv } else if (shdr->sh_flags & SHF_WRITE) {
3546ac8be24Smaxv segtype = BTSEG_DATA;
3556ac8be24Smaxv } else {
3566ac8be24Smaxv segtype = BTSEG_RODATA;
3576ac8be24Smaxv }
3586ac8be24Smaxv secpa = basepa + shdr->sh_offset;
3596ac8be24Smaxv secsz = shdr->sh_size;
36026e9e80dSmaxv secalign = shdr->sh_addralign;
3616ac8be24Smaxv ASSERT(shdr->sh_offset != 0);
3626ac8be24Smaxv ASSERT(secpa % PAGE_SIZE == 0);
363fe436c9bSmaxv ASSERT(secpa + secsz <= kernpa_end);
3646ac8be24Smaxv
36526e9e80dSmaxv secva = mm_map_segment(segtype, secpa, secsz, secalign);
366569f0c08Smaxv
367d4a66c4eSmaxv /*
368d4a66c4eSmaxv * Fix up the 'sh_offset' field of the NOBITS/PROGBITS sections.
369d4a66c4eSmaxv * We want (headva + sh_offset) to be the VA of the section.
370d4a66c4eSmaxv */
3714f428722Smaxv ASSERT(secva > headva);
3726ac8be24Smaxv shdr->sh_offset = secva - headva;
373569f0c08Smaxv }
374569f0c08Smaxv }
375569f0c08Smaxv
376569f0c08Smaxv void
elf_build_info(void)377d4a66c4eSmaxv elf_build_info(void)
378569f0c08Smaxv {
379d4a66c4eSmaxv size_t i, j;
380c9759921Smaxv
381c9759921Smaxv /* Locate the section names */
382c9759921Smaxv j = eif.ehdr->e_shstrndx;
383c9759921Smaxv if (j == SHN_UNDEF) {
384d4a66c4eSmaxv fatal("elf_build_info: shstrtab not found");
385c9759921Smaxv }
386c9759921Smaxv if (j >= eif.ehdr->e_shnum) {
387d4a66c4eSmaxv fatal("elf_build_info: wrong shstrtab index");
388c9759921Smaxv }
389c9759921Smaxv eif.shstrtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
390c9759921Smaxv eif.shstrsz = eif.shdr[j].sh_size;
391c9759921Smaxv
392c9759921Smaxv /* Locate the symbol table */
393c9759921Smaxv for (i = 0; i < eif.ehdr->e_shnum; i++) {
394c9759921Smaxv if (eif.shdr[i].sh_type == SHT_SYMTAB)
395c9759921Smaxv break;
396c9759921Smaxv }
397c9759921Smaxv if (i == eif.ehdr->e_shnum) {
398d4a66c4eSmaxv fatal("elf_build_info: symtab not found");
399c9759921Smaxv }
400a98ea778Smaxv if (eif.shdr[i].sh_offset == 0) {
401d4a66c4eSmaxv fatal("elf_build_info: symtab not loaded");
402a98ea778Smaxv }
403c9759921Smaxv eif.symtab = (Elf_Sym *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
404c9759921Smaxv eif.symcnt = eif.shdr[i].sh_size / sizeof(Elf_Sym);
405c9759921Smaxv
406c9759921Smaxv /* Also locate the string table */
407c9759921Smaxv j = eif.shdr[i].sh_link;
408c9759921Smaxv if (j == SHN_UNDEF || j >= eif.ehdr->e_shnum) {
409d4a66c4eSmaxv fatal("elf_build_info: wrong strtab index");
410c9759921Smaxv }
411c9759921Smaxv if (eif.shdr[j].sh_type != SHT_STRTAB) {
412d4a66c4eSmaxv fatal("elf_build_info: wrong strtab type");
413c9759921Smaxv }
414a98ea778Smaxv if (eif.shdr[j].sh_offset == 0) {
415d4a66c4eSmaxv fatal("elf_build_info: strtab not loaded");
416a98ea778Smaxv }
417c9759921Smaxv eif.strtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
418c9759921Smaxv eif.strsz = eif.shdr[j].sh_size;
419c9759921Smaxv }
420c9759921Smaxv
421c9759921Smaxv vaddr_t
elf_kernel_reloc(void)42292fe5cfbSmaxv elf_kernel_reloc(void)
423c9759921Smaxv {
424569f0c08Smaxv const vaddr_t baseva = (vaddr_t)eif.ehdr;
425c9759921Smaxv vaddr_t secva, ent;
426c9759921Smaxv Elf_Sym *sym;
427c9759921Smaxv size_t i, j;
428c9759921Smaxv
429*dadf0eefSkhorben print_state(STATE_NORMAL, "ELF info created");
430c9759921Smaxv
431c9759921Smaxv /*
432c9759921Smaxv * Update all symbol values with the appropriate offset.
433c9759921Smaxv */
434c9759921Smaxv for (i = 0; i < eif.ehdr->e_shnum; i++) {
435b3f22be2Smaxv if (!elf_section_mappable(&eif.shdr[i])) {
436c9759921Smaxv continue;
437c9759921Smaxv }
438b3f22be2Smaxv
439a98ea778Smaxv ASSERT(eif.shdr[i].sh_offset != 0);
440c9759921Smaxv secva = baseva + eif.shdr[i].sh_offset;
441c9759921Smaxv for (j = 0; j < eif.symcnt; j++) {
442c9759921Smaxv sym = &eif.symtab[j];
443c9759921Smaxv if (sym->st_shndx != i) {
444c9759921Smaxv continue;
445c9759921Smaxv }
446c9759921Smaxv sym->st_value += (Elf_Addr)secva;
447c9759921Smaxv }
448c9759921Smaxv }
449c9759921Smaxv
450*dadf0eefSkhorben print_state(STATE_NORMAL, "Symbol values updated");
451c9759921Smaxv
452c9759921Smaxv /*
453c9759921Smaxv * Perform relocations without addend if there are any.
454c9759921Smaxv */
455c9759921Smaxv for (i = 0; i < eif.ehdr->e_shnum; i++) {
456c9759921Smaxv Elf_Rel *reltab, *rel;
457c9759921Smaxv size_t secidx, nrel;
458c9759921Smaxv uintptr_t base;
459c9759921Smaxv
460a98ea778Smaxv if (eif.shdr[i].sh_type != SHT_REL) {
461c9759921Smaxv continue;
462a98ea778Smaxv }
463a98ea778Smaxv ASSERT(eif.shdr[i].sh_offset != 0);
464c9759921Smaxv reltab = (Elf_Rel *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
465c9759921Smaxv nrel = eif.shdr[i].sh_size / sizeof(Elf_Rel);
466c9759921Smaxv
467c9759921Smaxv secidx = eif.shdr[i].sh_info;
468c9759921Smaxv if (secidx >= eif.ehdr->e_shnum) {
469b3f22be2Smaxv fatal("elf_kernel_reloc: REL sh_info is malformed");
470b3f22be2Smaxv }
471b3f22be2Smaxv if (!elf_section_mappable(&eif.shdr[secidx])) {
47246c4386aSmaxv if (elf_can_drop_unmappable(&eif.shdr[secidx])) {
47346c4386aSmaxv continue;
47446c4386aSmaxv }
475b3f22be2Smaxv fatal("elf_kernel_reloc: REL sh_info not mappable");
476c9759921Smaxv }
477c9759921Smaxv base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;
478c9759921Smaxv
479c9759921Smaxv for (j = 0; j < nrel; j++) {
480c9759921Smaxv rel = &reltab[j];
481c9759921Smaxv elf_apply_reloc(base, rel, false);
482c9759921Smaxv }
483c9759921Smaxv }
484c9759921Smaxv
485*dadf0eefSkhorben print_state(STATE_NORMAL, "REL relocations applied");
486c9759921Smaxv
487c9759921Smaxv /*
488c9759921Smaxv * Perform relocations with addend if there are any.
489c9759921Smaxv */
490c9759921Smaxv for (i = 0; i < eif.ehdr->e_shnum; i++) {
491c9759921Smaxv Elf_Rela *relatab, *rela;
492c9759921Smaxv size_t secidx, nrela;
493c9759921Smaxv uintptr_t base;
494c9759921Smaxv
495a98ea778Smaxv if (eif.shdr[i].sh_type != SHT_RELA) {
496c9759921Smaxv continue;
497a98ea778Smaxv }
498a98ea778Smaxv ASSERT(eif.shdr[i].sh_offset != 0);
499c9759921Smaxv relatab = (Elf_Rela *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
500c9759921Smaxv nrela = eif.shdr[i].sh_size / sizeof(Elf_Rela);
501c9759921Smaxv
502c9759921Smaxv secidx = eif.shdr[i].sh_info;
503c9759921Smaxv if (secidx >= eif.ehdr->e_shnum) {
504b3f22be2Smaxv fatal("elf_kernel_reloc: RELA sh_info is malformed");
505b3f22be2Smaxv }
506b3f22be2Smaxv if (!elf_section_mappable(&eif.shdr[secidx])) {
50746c4386aSmaxv if (elf_can_drop_unmappable(&eif.shdr[secidx])) {
50846c4386aSmaxv continue;
50946c4386aSmaxv }
510b3f22be2Smaxv fatal("elf_kernel_reloc: RELA sh_info not mappable");
511c9759921Smaxv }
512c9759921Smaxv base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;
513c9759921Smaxv
514c9759921Smaxv for (j = 0; j < nrela; j++) {
515c9759921Smaxv rela = &relatab[j];
516c9759921Smaxv elf_apply_reloc(base, rela, true);
517c9759921Smaxv }
518c9759921Smaxv }
519c9759921Smaxv
520*dadf0eefSkhorben print_state(STATE_NORMAL, "RELA relocations applied");
521c9759921Smaxv
522c9759921Smaxv /*
523c9759921Smaxv * Get the entry point.
524c9759921Smaxv */
52592fe5cfbSmaxv ent = elf_get_entrypoint();
526c9759921Smaxv if (ent == 0) {
527c9759921Smaxv fatal("elf_kernel_reloc: entry point not found");
528c9759921Smaxv }
529c9759921Smaxv
530*dadf0eefSkhorben print_state(STATE_NORMAL, "Entry point found");
531c9759921Smaxv
532c9759921Smaxv return ent;
533c9759921Smaxv }
534