forked from unikraft/app-elfloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelf_load.c
269 lines (246 loc) · 9.11 KB
/
elf_load.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Copyright (c) 2017, Stefan Lankes, RWTH Aachen University
* All rights reserved.
* Copyright (c) 2018, Pierre Olivier, Virginia Polytechnic Institute and
* State University. All rights reserved.
* Copyright (c) 2019, Simon Kuenzer <simon.kuenzer@neclab.eu>,
* NEC Laboratories Europe GmbH, NEC Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Some code in this file was derived and adopted from:
* HermiTux Kernel
* https://github.com/ssrg-vt/hermitux-kernel, commit 2a3264d
* File: tools/uhyve-elf.c
* The authors of the original implementation are attributed in the license
* header of this file. HermiTux kernel was released under BSD 3-clause
* (see file /LICENSE in the HermiTux Kernel repository).
*/
#include <libelf.h>
#include <gelf.h>
#include <errno.h>
#include <string.h>
#include <uk/assert.h>
#include <uk/print.h>
#include <uk/essentials.h>
#include <uk/arch/limits.h>
#include "libelf_helper.h"
#include "binfmt_elf.h"
struct elf_prog *load_elf(struct uk_alloc *a, void *img_base, size_t img_len,
const char *progname)
{
struct elf_prog *elf_prog = NULL;
Elf *elf;
GElf_Ehdr ehdr;
GElf_Phdr phdr;
size_t phnum, phi;
uintptr_t prog_lowerl;
uintptr_t prog_upperl;
elf = elf_memory(img_base, img_len);
if (!elf) {
elferr_err("%s: Failed to initialize ELF parser\n",
progname);
errno = EBUSY;
goto out;
}
if (elf_kind(elf) != ELF_K_ELF) {
uk_pr_err("%s: Image format not recognized or not supported\n",
progname);
errno = EINVAL;
goto out_free_elf;
}
/*
* Executable Header
*/
if (gelf_getehdr(elf, &ehdr) == NULL) {
elferr_err("%s: Failed to get executable header",
progname);
errno = EINVAL;
goto out_free_elf;
}
/* Check machine */
uk_pr_debug("%s: ELF machine type: %"PRIu16"\n",
progname, ehdr.e_machine);
if
#if CONFIG_ARCH_X86_64
(ehdr.e_machine != EM_X86_64)
#elif CONFIG_ARCH_ARM_64
(ehdr.e_machine != EM_AARCH64)
#else
#error "Unsupported machine type"
#endif
{
uk_pr_err("%s: ELF machine type mismatch!\n", progname);
errno = EINVAL;
goto out_free_elf;
}
/* Check ABI */
uk_pr_debug("%s: ELF OS ABI: %"PRIu8"\n",
progname, ehdr.e_ident[EI_OSABI]);
if (ehdr.e_ident[EI_OSABI] != ELFOSABI_LINUX
&& ehdr.e_ident[EI_OSABI] != ELFOSABI_NONE) {
uk_pr_err("%s: ELF OS ABI unsupported!\n", progname);
errno = EINVAL;
goto out_free_elf;
}
/* Check executable type */
/* We support only static postiion-independent binaries for now:
* https://www.openwall.com/lists/musl/2015/06/01/12
* These binaries are type ET_DYN without having a PT_INTERP section */
uk_pr_debug("%s: ELF object type: %"PRIu16"\n",
progname, ehdr.e_type);
if (ehdr.e_type != ET_DYN) {
uk_pr_err("%s: ELF executable is not position-independent!\n",
progname);
errno = EINVAL;
goto out_free_elf;
}
/*
* Scan program headers
*/
/* When we have ET_DYN we shoiuld check that there is no PT_INTERP
* section; otherwise we need to support dynamic loading.
* For this purpose we need to scan the phdrs */
if (elf_getphnum(elf, &phnum) == 0) {
elferr_err("%s: Failed to get number of program headers",
progname);
errno = EINVAL;
goto out_free_elf;
}
/* While checking for compatible headers, we are trying to figure out
* how much memory needs to be allocated for loading program. We start
* loading the according segments in a second step */
prog_lowerl = 0;
prog_upperl = 0;
for (phi = 0; phi < phnum; ++phi) {
if (gelf_getphdr(elf, phi, &phdr) != &phdr) {
elferr_warn("%s: Failed to get program header %"PRIu64"\n",
progname, (uint64_t) phi);
continue;
}
if (phdr.p_type == PT_INTERP) {
/* TODO: If our executable requests an interpreter
* (e.g., dynamic loader) we have to stop since
* we do not support it yet */
uk_pr_err("%s: ELF executable requested program interpreter: Currently unsupported!\n",
progname);
errno = ENOTSUP;
goto out_free_elf;
}
if (phdr.p_type != PT_LOAD) {
/* We do not need to look further into headers
* that are not marked as 'load' */
continue;
}
uk_pr_debug("%s: phdr[%"PRIu64"]: %c%c%c, offset: %p, vaddr: %p, paddr: %p, filesz: %"PRIu64" B, memsz %"PRIu64" B, align: %"PRIu64" B\n",
progname, phi,
phdr.p_flags & PF_R ? 'R' : '-',
phdr.p_flags & PF_W ? 'W' : '-',
phdr.p_flags & PF_X ? 'X' : '-',
(void *) phdr.p_offset,
(void *) phdr.p_vaddr,
(void *) phdr.p_paddr,
(uint64_t) phdr.p_filesz,
(uint64_t) phdr.p_memsz,
(uint64_t) phdr.p_align);
uk_pr_debug("%s: \\_ segment at pie + 0x%"PRIx64" (len: 0x%"PRIx64") from file @ 0x%"PRIx64" (len: 0x%"PRIx64")\n",
progname, phdr.p_paddr, phdr.p_memsz,
(uint64_t) phdr.p_offset, (uint64_t) phdr.p_filesz);
if (prog_lowerl == 0 && prog_upperl == 0) {
/* first run */
prog_lowerl = phdr.p_paddr;
prog_upperl = prog_lowerl + phdr.p_memsz;
} else {
/* Move lower and upper border */
if (phdr.p_paddr < prog_lowerl)
prog_lowerl = phdr.p_paddr;
if (phdr.p_paddr + phdr.p_memsz > prog_upperl)
prog_upperl = phdr.p_paddr + phdr.p_memsz;
}
UK_ASSERT(prog_lowerl <= prog_upperl);
uk_pr_debug("%s: \\_ base: pie + 0x%"PRIx64" (len: 0x%"PRIx64")\n",
progname, prog_lowerl, prog_upperl - prog_lowerl);
}
/*
* At this point we are done with checking the image.
* We will allocate memory for `struct elf_prog` and for the
* program's/library's address space. We use a single contiguous
* allocation for it, for now
*/
/* We do not support yet an img base other than 0 */
UK_ASSERT(prog_lowerl == 0);
elf_prog = uk_calloc(a, 1, sizeof(*elf_prog));
if (!elf_prog)
goto out_free_elf;
elf_prog->a = a;
elf_prog->img_len = (size_t) (prog_upperl - prog_lowerl);
elf_prog->img = uk_memalign(elf_prog->a, __PAGE_SIZE,
elf_prog->img_len);
if (!elf_prog->img) {
uk_pr_debug("%s: Not enough memory to load image (failed to allocate %"PRIu64" bytes)\n",
progname, (uint64_t) elf_prog->img_len);
uk_free(elf_prog->a, elf_prog);
goto out_free_elf;
}
uk_pr_debug("%s: Program/Library memory region: 0x%"PRIx64"-0x%"PRIx64"\n",
progname,
(uint64_t) elf_prog->img,
(uint64_t) elf_prog->img + elf_prog->img_len);
/* Fill-out elf_prog and load segments to allocated memory */
elf_prog->ehdr_phoff = ehdr.e_phoff;
elf_prog->ehdr_phnum = ehdr.e_phnum;
elf_prog->ehdr_phentsize = ehdr.e_phentsize;
elf_prog->entry = ehdr.e_entry + (uintptr_t) elf_prog->img;
for (phi = 0; phi < phnum; ++phi) {
if ((gelf_getphdr(elf, phi, &phdr) != &phdr)
|| (phdr.p_type != PT_LOAD)) {
continue;
}
if(!elf_prog->start || (phdr.p_paddr + (uintptr_t) elf_prog->img < elf_prog->start))
elf_prog->start = phdr.p_paddr + (uintptr_t) elf_prog->img;
uk_pr_debug("%s: Copying 0x%"PRIx64" - 0x%"PRIx64" -> 0x%"PRIx64" - 0x%"PRIx64"\n",
progname,
(uint64_t) img_base + phdr.p_offset,
(uint64_t) img_base + phdr.p_offset + phdr.p_filesz,
(uint64_t) elf_prog->img + phdr.p_paddr,
(uint64_t) elf_prog->img + phdr.p_paddr + phdr.p_filesz);
memcpy((void *) elf_prog->img + phdr.p_paddr,
(const void *)((uintptr_t) img_base + phdr.p_offset),
(size_t) phdr.p_filesz);
uk_pr_debug("%s: Zeroing 0x%"PRIx64" - 0x%"PRIx64"\n",
progname,
(uint64_t) (elf_prog->img + phdr.p_paddr + phdr.p_filesz),
(uint64_t) (elf_prog->img + phdr.p_paddr + phdr.p_filesz + (phdr.p_memsz - phdr.p_filesz)));
memset((void *)(elf_prog->img + phdr.p_paddr + phdr.p_filesz),
0, phdr.p_memsz - phdr.p_filesz);
/*
* TODO: Setup memory protection (e.g., ukplat_mprotect())
*/
}
out_free_elf:
elf_end(elf);
out:
return elf_prog;
}