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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/* $OpenBSD: elf.c,v 1.2 2017/08/11 15:00:00 jasper Exp $ */
/*
* Copyright (c) 2016 Martin Pieuchot <mpi@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/exec_elf.h>
#include <machine/reloc.h>
#include <assert.h>
#include <err.h>
#include <string.h>
static int elf_reloc_size(unsigned long);
static void elf_reloc_apply(const char *, const char *, size_t, ssize_t,
char *, size_t);
int
iself(const char *p, size_t filesize)
{
Elf_Ehdr *eh = (Elf_Ehdr *)p;
if (filesize < (off_t)sizeof(Elf_Ehdr)) {
warnx("file too small to be ELF");
return 0;
}
if (eh->e_ehsize < sizeof(Elf_Ehdr) || !IS_ELF(*eh))
return 0;
if (eh->e_ident[EI_CLASS] != ELFCLASS) {
warnx("unexpected word size %u", eh->e_ident[EI_CLASS]);
return 0;
}
if (eh->e_ident[EI_VERSION] != ELF_TARG_VER) {
warnx("unexpected version %u", eh->e_ident[EI_VERSION]);
return 0;
}
if (eh->e_ident[EI_DATA] >= ELFDATANUM) {
warnx("unexpected data format %u", eh->e_ident[EI_DATA]);
return 0;
}
if (eh->e_shoff > filesize) {
warnx("bogus section table offset 0x%llx", (off_t)eh->e_shoff);
return 0;
}
if (eh->e_shentsize < sizeof(Elf_Shdr)) {
warnx("bogus section header size %u", eh->e_shentsize);
return 0;
}
if (eh->e_shnum > (filesize - eh->e_shoff) / eh->e_shentsize) {
warnx("bogus section header count %u", eh->e_shnum);
return 0;
}
if (eh->e_shstrndx >= eh->e_shnum) {
warnx("bogus string table index %u", eh->e_shstrndx);
return 0;
}
return 1;
}
int
elf_getshstab(const char *p, size_t filesize, const char **shstab,
size_t *shstabsize)
{
Elf_Ehdr *eh = (Elf_Ehdr *)p;
Elf_Shdr *sh;
sh = (Elf_Shdr *)(p + eh->e_shoff + eh->e_shstrndx * eh->e_shentsize);
if (sh->sh_type != SHT_STRTAB) {
warnx("unexpected string table type");
return -1;
}
if (sh->sh_offset > filesize) {
warnx("bogus string table offset");
return -1;
}
if (sh->sh_size > filesize - sh->sh_offset) {
warnx("bogus string table size");
return -1;
}
if (shstab != NULL)
*shstab = p + sh->sh_offset;
if (shstabsize != NULL)
*shstabsize = sh->sh_size;
return 0;
}
ssize_t
elf_getsymtab(const char *p, const char *shstab, size_t shstabsz,
const Elf_Sym **symtab, size_t *nsymb)
{
Elf_Ehdr *eh = (Elf_Ehdr *)p;
Elf_Shdr *sh;
size_t snlen;
ssize_t i;
snlen = strlen(ELF_SYMTAB);
for (i = 0; i < eh->e_shnum; i++) {
sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
if (sh->sh_type != SHT_SYMTAB)
continue;
if ((sh->sh_link >= eh->e_shnum) || (sh->sh_name >= shstabsz))
continue;
if (strncmp(shstab + sh->sh_name, ELF_SYMTAB, snlen) == 0) {
if (symtab != NULL)
*symtab = (Elf_Sym *)(p + sh->sh_offset);
if (nsymb != NULL)
*nsymb = (sh->sh_size / sh->sh_entsize);
return i;
}
}
return -1;
}
ssize_t
elf_getsection(char *p, const char *sname, const char *shstab,
size_t shstabsz, const char **psdata, size_t *pssz)
{
Elf_Ehdr *eh = (Elf_Ehdr *)p;
Elf_Shdr *sh;
char *sdata = NULL;
size_t snlen, ssz = 0;
ssize_t sidx, i;
snlen = strlen(sname);
if (snlen == 0)
return -1;
/* Find the given section. */
for (i = 0; i < eh->e_shnum; i++) {
sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
if ((sh->sh_link >= eh->e_shnum) || (sh->sh_name >= shstabsz))
continue;
if (strncmp(shstab + sh->sh_name, sname, snlen) == 0) {
sidx = i;
sdata = p + sh->sh_offset;
ssz = sh->sh_size;
elf_reloc_apply(p, shstab, shstabsz, sidx, sdata, ssz);
break;
}
}
if (sdata == NULL)
return -1;
if (psdata != NULL)
*psdata = sdata;
if (pssz != NULL)
*pssz = ssz;
return sidx;
}
static int
elf_reloc_size(unsigned long type)
{
switch (type) {
#ifdef R_X86_64_64
case R_X86_64_64:
return sizeof(uint64_t);
#endif
#ifdef R_X86_64_32
case R_X86_64_32:
return sizeof(uint32_t);
#endif
#ifdef RELOC_32
case RELOC_32:
return sizeof(uint32_t);
#endif
default:
break;
}
return -1;
}
#define ELF_WRITE_RELOC(buf, val, rsize) \
do { \
if (rsize == 4) { \
uint32_t v32 = val; \
memcpy(buf, &v32, sizeof(v32)); \
} else { \
uint64_t v64 = val; \
memcpy(buf, &v64, sizeof(v64)); \
} \
} while (0)
static void
elf_reloc_apply(const char *p, const char *shstab, size_t shstabsz,
ssize_t sidx, char *sdata, size_t ssz)
{
Elf_Ehdr *eh = (Elf_Ehdr *)p;
Elf_Shdr *sh;
Elf_Rel *rel = NULL;
Elf_RelA *rela = NULL;
const Elf_Sym *symtab, *sym;
ssize_t symtabidx;
size_t nsymb, rsym, rtyp, roff;
size_t i, j;
uint64_t value;
int rsize;
/* Find symbol table location and number of symbols. */
symtabidx = elf_getsymtab(p, shstab, shstabsz, &symtab, &nsymb);
if (symtabidx == -1) {
warnx("symbol table not found");
return;
}
/* Apply possible relocation. */
for (i = 0; i < eh->e_shnum; i++) {
sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
if (sh->sh_size == 0)
continue;
if ((sh->sh_info != sidx) || (sh->sh_link != symtabidx))
continue;
switch (sh->sh_type) {
case SHT_RELA:
rela = (Elf_RelA *)(p + sh->sh_offset);
for (j = 0; j < (sh->sh_size / sizeof(Elf_RelA)); j++) {
rsym = ELF_R_SYM(rela[j].r_info);
rtyp = ELF_R_TYPE(rela[j].r_info);
roff = rela[j].r_offset;
if (rsym >= nsymb)
continue;
sym = &symtab[rsym];
value = sym->st_value + rela[j].r_addend;
rsize = elf_reloc_size(rtyp);
if (rsize == -1 || roff + rsize >= ssz)
continue;
ELF_WRITE_RELOC(sdata + roff, value, rsize);
}
break;
case SHT_REL:
rel = (Elf_Rel *)(p + sh->sh_offset);
for (j = 0; j < (sh->sh_size / sizeof(Elf_Rel)); j++) {
rsym = ELF_R_SYM(rel[j].r_info);
rtyp = ELF_R_TYPE(rel[j].r_info);
roff = rel[j].r_offset;
if (rsym >= nsymb)
continue;
sym = &symtab[rsym];
value = sym->st_value;
rsize = elf_reloc_size(rtyp);
if (rsize == -1 || roff + rsize >= ssz)
continue;
ELF_WRITE_RELOC(sdata + roff, value, rsize);
}
break;
default:
continue;
}
}
}
|