blob: 85d3c9869c7edf492f3761a89c10833c3398c721 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <lib/elf.h>
24#include <assert.h>
25#include <debug.h>
26#include <endian.h>
27#include <err.h>
28#include <trace.h>
29#include <stdlib.h>
30#include <string.h>
31#include <arch/ops.h>
32
33#define LOCAL_TRACE 0
34
35struct read_hook_memory_args {
36 const uint8_t *ptr;
37 size_t len;
38};
39
40static ssize_t elf_read_hook_memory(struct elf_handle *handle, void *buf, uint64_t offset, size_t len)
41{
42 LTRACEF("handle %p, buf %p, offset %lld, len %zu\n", handle, buf, offset, len);
43
44 struct read_hook_memory_args *args = handle->read_hook_arg;
45
46 DEBUG_ASSERT(args);
47 DEBUG_ASSERT(buf);
48 DEBUG_ASSERT(handle);
49 DEBUG_ASSERT(handle->open);
50
51 ssize_t toread = len;
52 if (offset >= args->len)
53 toread = 0;
54 if (offset + len >= args->len)
55 toread = args->len - offset;
56
57 memcpy(buf, args->ptr + offset, toread);
58
59 LTRACEF("returning %ld\n", toread);
60
61 return toread;
62}
63
64status_t elf_open_handle(elf_handle_t *handle, elf_read_hook_t read_hook, void *read_hook_arg, bool free_read_hook_arg)
65{
66 if (!handle)
67 return ERR_INVALID_ARGS;
68 if (!read_hook)
69 return ERR_INVALID_ARGS;
70
71 memset(handle, 0, sizeof(*handle));
72
73 handle->read_hook = read_hook;
74 handle->read_hook_arg = read_hook_arg;
75 handle->free_read_hook_arg = free_read_hook_arg;
76
77 handle->open = true;
78
79 return NO_ERROR;
80}
81
82status_t elf_open_handle_memory(elf_handle_t *handle, const void *ptr, size_t len)
83{
84 struct read_hook_memory_args *args = malloc(sizeof(struct read_hook_memory_args));
85
86 args->ptr = ptr;
87 args->len = len;
88
89 status_t err = elf_open_handle(handle, elf_read_hook_memory, (void *)args, true);
90 if (err < 0)
91 free(args);
92
93 return err;
94}
95
96void elf_close_handle(elf_handle_t *handle)
97{
98 if (!handle || !handle->open)
99 return;
100
101 handle->open = false;
102
103 if (handle->free_read_hook_arg)
104 free(handle->read_hook_arg);
105
106 free(handle->pheaders);
107}
108
109static int verify_eheader(const struct Elf32_Ehdr *eheader)
110{
111 if (memcmp(eheader->e_ident, ELF_MAGIC, 4) != 0)
112 return ERR_NOT_FOUND;
113
114 if (eheader->e_ident[EI_CLASS] != ELFCLASS32)
115 return ERR_NOT_FOUND;
116
117#if BYTE_ORDER == LITTLE_ENDIAN
118 if (eheader->e_ident[EI_DATA] != ELFDATA2LSB)
119 return ERR_NOT_FOUND;
120#elif BYTE_ORDER == BIG_ENDIAN
121 if (eheader->e_ident[EI_DATA] != ELFDATA2MSB)
122 return ERR_NOT_FOUND;
123#endif
124
125 if (eheader->e_ident[EI_VERSION] != EV_CURRENT)
126 return ERR_NOT_FOUND;
127
128 if (eheader->e_phoff == 0)
129 return ERR_NOT_FOUND;
130
131 if (eheader->e_phentsize < sizeof(struct Elf32_Phdr))
132 return ERR_NOT_FOUND;
133
134#if ARCH_ARM
135 if (eheader->e_machine != EM_ARM)
136 return ERR_NOT_FOUND;
137#elif ARCH_X86
138 if (eheader->e_machine != EM_386)
139 return ERR_NOT_FOUND;
140#elif ARCH_X86_64
141 if (eheader->e_machine != EM_X86_64)
142 return ERR_NOT_FOUND;
143#elif ARCH_ARM64
144 if (eheader->e_machine != EM_AARCH64)
145 return ERR_NOT_FOUND;
146#elif ARCH_MICROBLAZE
147 if (eheader->e_machine != EM_MICROBLAZE)
148 return ERR_NOT_FOUND;
149#else
150#error find proper EM_ define for your machine
151#endif
152
153 return NO_ERROR;
154}
155
156status_t elf_load(elf_handle_t *handle)
157{
158 if (!handle)
159 return ERR_INVALID_ARGS;
160 if (!handle->open)
161 return ERR_NOT_READY;
162
163 // validate that this is an ELF file
164 ssize_t readerr = handle->read_hook(handle, &handle->eheader, 0, sizeof(handle->eheader));
165 if (readerr < (ssize_t)sizeof(handle->eheader)) {
166 LTRACEF("couldn't read elf header\n");
167 return ERR_NOT_FOUND;
168 }
169
170 if (verify_eheader(&handle->eheader)) {
171 LTRACEF("header not valid\n");
172 return ERR_NOT_FOUND;
173 }
174
175 // sanity check number of program headers
176 LTRACEF("number of program headers %u, entry size %u\n", handle->eheader.e_phnum, handle->eheader.e_phentsize);
177 if (handle->eheader.e_phnum > 16 || handle->eheader.e_phentsize != sizeof(struct Elf32_Phdr)) {
178 LTRACEF("too many program headers or bad size\n");
179 return ERR_NO_MEMORY;
180 }
181
182 // allocate and read in the program headers
183 handle->pheaders = calloc(1, handle->eheader.e_phnum * handle->eheader.e_phentsize);
184 if (!handle->pheaders) {
185 LTRACEF("failed to allocate memory for program headers\n");
186 return ERR_NO_MEMORY;
187 }
188
189 readerr = handle->read_hook(handle, handle->pheaders, handle->eheader.e_phoff, handle->eheader.e_phnum * handle->eheader.e_phentsize);
190 if (readerr < (ssize_t)(handle->eheader.e_phnum * handle->eheader.e_phentsize)) {
191 LTRACEF("failed to read program headers\n");
192 return ERR_NO_MEMORY;
193 }
194
195 LTRACEF("program headers:\n");
196 uint load_count = 0;
197 for (uint i = 0; i < handle->eheader.e_phnum; i++) {
198 // parse the program headers
199 struct Elf32_Phdr *pheader = &handle->pheaders[i];
200
201 LTRACEF("%u: type %u offset 0x%x vaddr 0x%x paddr 0x%x memsiz %u filesize %u\n",
202 i, pheader->p_type, pheader->p_offset, pheader->p_vaddr, pheader->p_paddr, pheader->p_memsz, pheader->p_filesz);
203
204 // we only care about PT_LOAD segments at the moment
205 if (pheader->p_type == PT_LOAD) {
206 // if the memory allocation hook exists, call it
207 void *ptr = (void *)(uintptr_t)pheader->p_vaddr;
208
209 if (handle->mem_alloc_hook) {
210 status_t err = handle->mem_alloc_hook(handle, &ptr, pheader->p_memsz, load_count, 0);
211 if (err < 0) {
212 LTRACEF("mem hook failed, abort\n");
213 // XXX clean up what we got so far
214 return err;
215 }
216 }
217
218 // read the file portion of the segment into memory at vaddr
219 LTRACEF("reading segment at offset %u to address %p\n", pheader->p_offset, ptr);
220 readerr = handle->read_hook(handle, ptr, pheader->p_offset, pheader->p_filesz);
221 if (readerr < (ssize_t)pheader->p_filesz) {
222 LTRACEF("error %ld reading program header %u\n", readerr, i);
223 return (readerr < 0) ? readerr : ERR_IO;
224 }
225
226 // zero out he difference between memsz and filesz
227 size_t tozero = pheader->p_memsz - pheader->p_filesz;
228 if (tozero > 0) {
229 uint8_t *ptr2 = (uint8_t *)ptr + pheader->p_filesz;
230 LTRACEF("zeroing memory at %p, size %zu\n", ptr2, tozero);
231 memset(ptr2, 0, tozero);
232 }
233
234 // make sure the i&d cache are coherent, if they exist
235 arch_sync_cache_range((addr_t)ptr, pheader->p_memsz);
236
237 // track the number of load segments we have seen to pass the mem alloc hook
238 load_count++;
239 }
240 }
241
242 // save the entry point
243 handle->entry = handle->eheader.e_entry;
244
245 return NO_ERROR;
246}
247