summaryrefslogtreecommitdiffstats
path: root/kexec/kexec-elf-exec.c
blob: b3376427db3daf8ff249a6e8e7e53e575ed20200 (plain)
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
#include <limits.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include "elf.h"
#include <boot/elf_boot.h>
#include "kexec.h"
#include "kexec-elf.h"

static const int probe_debug = 0;

static void load_elf_segments(struct mem_ehdr *ehdr, struct kexec_info *info, unsigned long base)
{
	size_t i;

	/* Read in the PT_LOAD segments */
	for(i = 0; i < ehdr->e_phnum; i++) {
		struct mem_phdr *phdr;
		size_t size;
		phdr = &ehdr->e_phdr[i];
		if (phdr->p_type != PT_LOAD) {
			continue;
		}
		size = phdr->p_filesz;
		if (size > phdr->p_memsz) {
			size = phdr->p_memsz;
		}
		add_segment(info, phdr->p_data, size,
					phdr->p_paddr + base, phdr->p_memsz);
	}
}

static int get_elf_exec_load_base(struct mem_ehdr *ehdr, struct kexec_info *info,
				  unsigned long min, unsigned long max,
				  unsigned long align, unsigned long *base)
{
	unsigned long first, last;
	size_t i;

	/* Note on arm64 and loongarch64:
	 * arm64's vmlinux has virtual address in physical address
	 * field of PT_LOAD segments. So the following validity check
	 * and relocation makes no sense on arm64.
	 * This is also applies to LoongArch.
	 */
	if (ehdr->e_machine == EM_AARCH64 || ehdr->e_machine == EM_LOONGARCH)
		return 0;

	first = ULONG_MAX;
	last  = 0;
	for(i = 0; i < ehdr->e_phnum; i++) {
		unsigned long start, stop;
		struct mem_phdr *phdr;
		phdr = &ehdr->e_phdr[i];
		if ((phdr->p_type != PT_LOAD) ||
			(phdr->p_memsz == 0))
		{
			continue;
		}
		start = phdr->p_paddr;
		stop  = start + phdr->p_memsz;
		if (first > start) {
			first = start;
		}
		if (last < stop) {
			last = stop;
		}
		if (align < phdr->p_align) {
			align = phdr->p_align;
		}
	}

	if ((max - min) < (last - first))
		return -1;

	if (!valid_memory_range(info, min > first ? min : first, max < last ? max : last)) {
		unsigned long hole;
		hole = locate_hole(info, last - first + 1, align, min, max, 1);
		if (hole == ULONG_MAX)
			return -1;

		/* Base is the value that when added
		 * to any virtual address in the file
		 * yields it's load virtual address.
		 */
		*base = hole - first;
	}
	return 0;
}

int build_elf_exec_info(const char *buf, off_t len, struct mem_ehdr *ehdr,
				uint32_t flags)
{
	struct mem_phdr *phdr, *end_phdr;
	int result;
	result = build_elf_info(buf, len, ehdr, flags);
	if (result < 0) {
		return result;
	}
	if ((ehdr->e_type != ET_EXEC) && (ehdr->e_type != ET_DYN) &&
	    (ehdr->e_type != ET_CORE)) {
		/* not an ELF executable */
		if (probe_debug) {
			fprintf(stderr, "Not ELF type ET_EXEC or ET_DYN\n");
		}
		return -1;
	}
	if (!ehdr->e_phdr) {
		/* No program header */
		fprintf(stderr, "No ELF program header\n");
		return -1; 
	}
	end_phdr = &ehdr->e_phdr[ehdr->e_phnum];
	for(phdr = ehdr->e_phdr; phdr != end_phdr; phdr++) {
		/* Kexec does not support loading interpreters.
		 * In addition this check keeps us from attempting
		 * to kexec ordinay executables.
		 */
		if (phdr->p_type == PT_INTERP) {
			fprintf(stderr, "Requires an ELF interpreter\n");
			return -1;
		}
	}

	return 0;
}


int elf_exec_load(struct mem_ehdr *ehdr, struct kexec_info *info)
{
	unsigned long base;
	int result;

	if (!ehdr->e_phdr) {
		fprintf(stderr, "No program header?\n");
		result = -1;
		goto out;
	}

	/* If I have a dynamic executable find it's size
	 * and then find a location for it in memory.
	 */
	base = 0;
	if (ehdr->e_type == ET_DYN) {
		result = get_elf_exec_load_base(ehdr, info, 0, elf_max_addr(ehdr), 0 /* align */, &base);
		if (result < 0)
			goto out;
	}

	load_elf_segments(ehdr, info, base);

	/* Update entry point to reflect new load address*/
	ehdr->e_entry += base;

	result = 0;
 out:
	return result;
}

int elf_exec_load_relocatable(struct mem_ehdr *ehdr, struct kexec_info *info,
			      unsigned long reloc_min, unsigned long reloc_max,
			      unsigned long align)
{
	unsigned long base;
	int result;

	if (reloc_min > reloc_max) {
		fprintf(stderr, "Bad relocation range, start=%lux > end=%lux.\n", reloc_min, reloc_max);
		result = -1;
		goto out;
	}
	if (!ehdr->e_phdr) {
		fprintf(stderr, "No program header?\n");
		result = -1;
		goto out;
	}

	base = 0;
	result = get_elf_exec_load_base(ehdr, info, reloc_min, reloc_max, align, &base);
	if (result < 0)
		goto out;

	load_elf_segments(ehdr, info, base);

	/* Update entry point to reflect new load address*/
	ehdr->e_entry += base;

	result = 0;
 out:
	return result;
}

void elf_exec_build_load(struct kexec_info *info, struct mem_ehdr *ehdr, 
	const char *buf, off_t len, uint32_t flags)
{
	int result;
	/* Parse the Elf file */
	result = build_elf_exec_info(buf, len, ehdr, flags);
	if (result < 0) {
		die("ELF exec parse failed\n");
	}

	/* Load the Elf data */
	result = elf_exec_load(ehdr, info);
	if (result < 0) {
		die("ELF exec load failed\n");
	}
}

void elf_exec_build_load_relocatable(struct kexec_info *info, struct mem_ehdr *ehdr,
				     const char *buf, off_t len, uint32_t flags,
				     unsigned long reloc_min, unsigned long reloc_max,
				     unsigned long align)
{
	int result;
	/* Parse the Elf file */
	result = build_elf_exec_info(buf, len, ehdr, flags);
	if (result < 0) {
		die("%s: ELF exec parse failed\n", __func__);
	}

	/* Load the Elf data */
	result = elf_exec_load_relocatable(ehdr, info, reloc_min, reloc_max, align);
	if (result < 0) {
		die("%s: ELF exec load failed\n", __func__);
	}
}