1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2020 Joyent, Inc.
*/
#include <sys/types.h>
#include <sys/inttypes.h>
#include <sys/systm.h>
#include <sys/elf.h>
#include <sys/elf_notes.h>
#include <util/memcpy.h>
#include "dboot_xboot.h"
#include "dboot_elfload.h"
#include "dboot_printf.h"
static caddr_t elf_file = 0;
#define PGETBYTES(offset) ((void *)(elf_file + (offset)))
static void *
getehdr(void)
{
uchar_t *ident;
void *hdr = NULL;
ident = PGETBYTES(0);
if (ident == NULL)
dboot_panic("Cannot read kernel ELF header");
if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3)
dboot_panic("not an ELF file!");
if (ident[EI_CLASS] == ELFCLASS32)
hdr = PGETBYTES(0);
else if (ident[EI_CLASS] == ELFCLASS64)
hdr = PGETBYTES(0);
else
dboot_panic("Unknown ELF class");
return (hdr);
}
/*
* parse the elf file for program information
*/
int
dboot_elfload64(uintptr_t file_image)
{
Elf64_Ehdr *eh;
Elf64_Phdr *phdr;
Elf64_Shdr *shdr;
caddr_t allphdrs, sechdrs;
int i;
paddr_t src;
paddr_t dst;
paddr_t next_addr;
next_addr = 0;
elf_file = (caddr_t)file_image;
allphdrs = NULL;
eh = getehdr();
if (eh == NULL)
dboot_panic("getehdr() failed");
if (eh->e_type != ET_EXEC)
dboot_panic("not ET_EXEC, e_type = 0x%x", eh->e_type);
if (eh->e_phnum == 0 || eh->e_phoff == 0)
dboot_panic("no program headers");
/*
* Get the program headers.
*/
allphdrs = PGETBYTES(eh->e_phoff);
if (allphdrs == NULL)
dboot_panic("Failed to get program headers e_phnum = %d",
eh->e_phnum);
/*
* Get the section headers.
*/
sechdrs = PGETBYTES(eh->e_shoff);
if (sechdrs == NULL)
dboot_panic("Failed to get section headers e_shnum = %d",
eh->e_shnum);
/*
* Next look for interesting program headers.
*/
for (i = 0; i < eh->e_phnum; i++) {
/*LINTED [ELF program header alignment]*/
phdr = (Elf64_Phdr *)(allphdrs + eh->e_phentsize * i);
/*
* Dynamically-linked executable.
* Complain.
*/
if (phdr->p_type == PT_INTERP) {
dboot_printf("warning: PT_INTERP section\n");
continue;
}
/*
* at this point we only care about PT_LOAD segments
*/
if (phdr->p_type != PT_LOAD)
continue;
if (phdr->p_flags == (PF_R | PF_W) && phdr->p_vaddr == 0) {
dboot_printf("warning: krtld reloc info?\n");
continue;
}
/*
* If memory size is zero just ignore this header.
*/
if (phdr->p_memsz == 0)
continue;
/*
* If load address 1:1 then ignore this header.
*/
if (phdr->p_paddr == phdr->p_vaddr) {
if (prom_debug)
dboot_printf("Skipping PT_LOAD segment for "
"paddr = 0x%lx\n", (ulong_t)phdr->p_paddr);
continue;
}
/*
* copy the data to kernel area
*/
if (phdr->p_paddr != FOUR_MEG && phdr->p_paddr != 2 * FOUR_MEG)
dboot_panic("Bad paddr for kernel nucleus segment");
src = (uintptr_t)PGETBYTES(phdr->p_offset);
dst = ktext_phys + phdr->p_paddr - FOUR_MEG;
if (prom_debug)
dboot_printf("copying %ld bytes from ELF offset 0x%lx "
"to physaddr 0x%lx (va=0x%lx)\n",
(ulong_t)phdr->p_filesz, (ulong_t)phdr->p_offset,
(ulong_t)dst, (ulong_t)phdr->p_vaddr);
(void) memcpy((void *)(uintptr_t)dst,
(void *)(uintptr_t)src, (size_t)phdr->p_filesz);
next_addr = dst + phdr->p_filesz;
}
/*
* Next look for bss
*/
for (i = 0; i < eh->e_shnum; i++) {
shdr = (Elf64_Shdr *)(sechdrs + eh->e_shentsize * i);
/* zero out bss */
if (shdr->sh_type == SHT_NOBITS) {
if (prom_debug)
dboot_printf("zeroing BSS %lu bytes from "
"physaddr 0x%" PRIx64
" (end=0x%" PRIx64 ")\n",
(ulong_t)shdr->sh_size,
next_addr,
next_addr + shdr->sh_size);
(void) memset((void *)(uintptr_t)next_addr, 0,
shdr->sh_size);
break;
}
}
/*
* Ignore the intepreter (or should we die if there is one??)
*/
return (0);
}
|