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