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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
/*
* Copyright (c) 2000, Boris Popov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Boris Popov.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $FreeBSD$
*/
#include <sys/param.h>
#include <sys/linker.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <machine/elf.h>
#define FREEBSD_ELF
#include <link.h>
#include <err.h>
#include "ef.h"
struct ef_file {
char* ef_name;
struct elf_file *ef_efile;
Elf_Phdr * ef_ph;
int ef_fd;
int ef_type;
Elf_Ehdr ef_hdr;
void* ef_fpage; /* First block of the file */
int ef_fplen; /* length of first block */
Elf_Dyn* ef_dyn; /* Symbol table etc. */
Elf_Hashelt ef_nbuckets;
Elf_Hashelt ef_nchains;
Elf_Hashelt* ef_buckets;
Elf_Hashelt* ef_chains;
Elf_Hashelt* ef_hashtab;
Elf_Off ef_stroff;
caddr_t ef_strtab;
int ef_strsz;
Elf_Off ef_symoff;
Elf_Sym* ef_symtab;
int ef_nsegs;
Elf_Phdr * ef_segs[2];
int ef_verbose;
Elf_Rel * ef_rel; /* relocation table */
int ef_relsz; /* number of entries */
Elf_Rela * ef_rela; /* relocation table */
int ef_relasz; /* number of entries */
};
static void ef_print_phdr(Elf_Phdr *);
static u_long ef_get_offset(elf_file_t, Elf_Off);
static int ef_parse_dynamic(elf_file_t);
static int ef_get_type(elf_file_t ef);
static int ef_close(elf_file_t ef);
static int ef_read(elf_file_t ef, Elf_Off offset, size_t len, void* dest);
static int ef_read_entry(elf_file_t ef, Elf_Off offset, size_t len, void **ptr);
static int ef_seg_read(elf_file_t ef, Elf_Off offset, size_t len, void *dest);
static int ef_seg_read_rel(elf_file_t ef, Elf_Off offset, size_t len,
void *dest);
static int ef_seg_read_entry(elf_file_t ef, Elf_Off offset, size_t len,
void **ptr);
static int ef_seg_read_entry_rel(elf_file_t ef, Elf_Off offset, size_t len,
void **ptr);
static Elf_Addr ef_symaddr(elf_file_t ef, Elf_Size symidx);
static int ef_lookup_set(elf_file_t ef, const char *name, long *startp,
long *stopp, long *countp);
static int ef_lookup_symbol(elf_file_t ef, const char* name, Elf_Sym** sym);
static struct elf_file_ops ef_file_ops = {
ef_get_type,
ef_close,
ef_read,
ef_read_entry,
ef_seg_read,
ef_seg_read_rel,
ef_seg_read_entry,
ef_seg_read_entry_rel,
ef_symaddr,
ef_lookup_set,
ef_lookup_symbol
};
static void
ef_print_phdr(Elf_Phdr *phdr)
{
if ((phdr->p_flags & PF_W) == 0) {
printf("text=0x%lx ", (long)phdr->p_filesz);
} else {
printf("data=0x%lx", (long)phdr->p_filesz);
if (phdr->p_filesz < phdr->p_memsz)
printf("+0x%lx", (long)(phdr->p_memsz - phdr->p_filesz));
printf(" ");
}
}
static u_long
ef_get_offset(elf_file_t ef, Elf_Off off)
{
Elf_Phdr *ph;
int i;
for (i = 0; i < ef->ef_nsegs; i++) {
ph = ef->ef_segs[i];
if (off >= ph->p_vaddr && off < ph->p_vaddr + ph->p_memsz) {
return ph->p_offset + (off - ph->p_vaddr);
}
}
return 0;
}
static int
ef_get_type(elf_file_t ef)
{
return (ef->ef_type);
}
/*
* next three functions copied from link_elf.c
*/
static unsigned long
elf_hash(const char *name)
{
const unsigned char *p = (const unsigned char *) name;
unsigned long h = 0;
unsigned long g;
while (*p != '\0') {
h = (h << 4) + *p++;
if ((g = h & 0xf0000000) != 0)
h ^= g >> 24;
h &= ~g;
}
return h;
}
static int
ef_lookup_symbol(elf_file_t ef, const char* name, Elf_Sym** sym)
{
unsigned long symnum;
Elf_Sym* symp;
char *strp;
unsigned long hash;
/* First, search hashed global symbols */
hash = elf_hash(name);
symnum = ef->ef_buckets[hash % ef->ef_nbuckets];
while (symnum != STN_UNDEF) {
if (symnum >= ef->ef_nchains) {
warnx("ef_lookup_symbol: file %s have corrupted symbol table\n",
ef->ef_name);
return ENOENT;
}
symp = ef->ef_symtab + symnum;
if (symp->st_name == 0) {
warnx("ef_lookup_symbol: file %s have corrupted symbol table\n",
ef->ef_name);
return ENOENT;
}
strp = ef->ef_strtab + symp->st_name;
if (strcmp(name, strp) == 0) {
if (symp->st_shndx != SHN_UNDEF ||
(symp->st_value != 0 &&
ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
*sym = symp;
return 0;
} else
return ENOENT;
}
symnum = ef->ef_chains[symnum];
}
return ENOENT;
}
static int
ef_lookup_set(elf_file_t ef, const char *name, long *startp, long *stopp,
long *countp)
{
Elf_Sym *sym;
char *setsym;
int error, len;
len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
setsym = malloc(len);
if (setsym == NULL)
return (ENOMEM);
/* get address of first entry */
snprintf(setsym, len, "%s%s", "__start_set_", name);
error = ef_lookup_symbol(ef, setsym, &sym);
if (error)
goto out;
*startp = sym->st_value;
/* get address of last entry */
snprintf(setsym, len, "%s%s", "__stop_set_", name);
error = ef_lookup_symbol(ef, setsym, &sym);
if (error)
goto out;
*stopp = sym->st_value;
/* and the number of entries */
*countp = (*stopp - *startp) / sizeof(void *);
out:
free(setsym);
return (error);
}
static Elf_Addr
ef_symaddr(elf_file_t ef, Elf_Size symidx)
{
const Elf_Sym *sym;
if (symidx >= ef->ef_nchains)
return (0);
sym = ef->ef_symtab + symidx;
if (ELF_ST_BIND(sym->st_info) == STB_LOCAL &&
sym->st_shndx != SHN_UNDEF && sym->st_value != 0)
return (sym->st_value);
return (0);
}
static int
ef_parse_dynamic(elf_file_t ef)
{
Elf_Dyn *dp;
Elf_Hashelt hashhdr[2];
/* int plttype = DT_REL;*/
int error;
Elf_Off rel_off;
Elf_Off rela_off;
int rel_sz;
int rela_sz;
int rel_entry;
int rela_entry;
rel_off = rela_off = 0;
rel_sz = rela_sz = 0;
rel_entry = rela_entry = 0;
for (dp = ef->ef_dyn; dp->d_tag != DT_NULL; dp++) {
switch (dp->d_tag) {
case DT_HASH:
error = ef_read(ef, ef_get_offset(ef, dp->d_un.d_ptr),
sizeof(hashhdr), hashhdr);
if (error) {
warnx("can't read hash header (%lx)",
ef_get_offset(ef, dp->d_un.d_ptr));
return error;
}
ef->ef_nbuckets = hashhdr[0];
ef->ef_nchains = hashhdr[1];
error = ef_read_entry(ef, -1,
(hashhdr[0] + hashhdr[1]) * sizeof(Elf_Hashelt),
(void**)&ef->ef_hashtab);
if (error) {
warnx("can't read hash table");
return error;
}
ef->ef_buckets = ef->ef_hashtab;
ef->ef_chains = ef->ef_buckets + ef->ef_nbuckets;
break;
case DT_STRTAB:
ef->ef_stroff = dp->d_un.d_ptr;
break;
case DT_STRSZ:
ef->ef_strsz = dp->d_un.d_val;
break;
case DT_SYMTAB:
ef->ef_symoff = dp->d_un.d_ptr;
break;
case DT_SYMENT:
if (dp->d_un.d_val != sizeof(Elf_Sym))
return EFTYPE;
break;
case DT_REL:
if (rel_off != 0)
warnx("second DT_REL entry ignored");
rel_off = dp->d_un.d_ptr;
break;
case DT_RELSZ:
if (rel_sz != 0)
warnx("second DT_RELSZ entry ignored");
rel_sz = dp->d_un.d_val;
break;
case DT_RELENT:
if (rel_entry != 0)
warnx("second DT_RELENT entry ignored");
rel_entry = dp->d_un.d_val;
break;
case DT_RELA:
if (rela_off != 0)
warnx("second DT_RELA entry ignored");
rela_off = dp->d_un.d_ptr;
break;
case DT_RELASZ:
if (rela_sz != 0)
warnx("second DT_RELASZ entry ignored");
rela_sz = dp->d_un.d_val;
break;
case DT_RELAENT:
if (rela_entry != 0)
warnx("second DT_RELAENT entry ignored");
rela_entry = dp->d_un.d_val;
break;
}
}
if (ef->ef_symoff == 0) {
warnx("%s: no .dynsym section found\n", ef->ef_name);
return EFTYPE;
}
if (ef->ef_stroff == 0) {
warnx("%s: no .dynstr section found\n", ef->ef_name);
return EFTYPE;
}
if (ef_read_entry(ef, ef_get_offset(ef, ef->ef_symoff),
ef->ef_nchains * sizeof(Elf_Sym),
(void**)&ef->ef_symtab) != 0) {
if (ef->ef_verbose)
warnx("%s: can't load .dynsym section (0x%lx)",
ef->ef_name, (long)ef->ef_symoff);
return EIO;
}
if (ef_read_entry(ef, ef_get_offset(ef, ef->ef_stroff), ef->ef_strsz,
(void**)&ef->ef_strtab) != 0) {
warnx("can't load .dynstr section");
return EIO;
}
if (rel_off != 0) {
if (rel_entry == 0) {
warnx("%s: no DT_RELENT for DT_REL", ef->ef_name);
return (EFTYPE);
}
if (rel_entry != sizeof(Elf_Rel)) {
warnx("%s: inconsistent DT_RELENT value",
ef->ef_name);
return (EFTYPE);
}
if (rel_sz % rel_entry != 0) {
warnx("%s: inconsistent values for DT_RELSZ and "
"DT_RELENT", ef->ef_name);
return (EFTYPE);
}
if (ef_read_entry(ef, ef_get_offset(ef, rel_off), rel_sz,
(void **)&ef->ef_rel) != 0) {
warnx("%s: cannot load DT_REL section", ef->ef_name);
return (EIO);
}
ef->ef_relsz = rel_sz / rel_entry;
if (ef->ef_verbose)
warnx("%s: %d REL entries", ef->ef_name,
ef->ef_relsz);
}
if (rela_off != 0) {
if (rela_entry == 0) {
warnx("%s: no DT_RELAENT for DT_RELA", ef->ef_name);
return (EFTYPE);
}
if (rela_entry != sizeof(Elf_Rela)) {
warnx("%s: inconsistent DT_RELAENT value",
ef->ef_name);
return (EFTYPE);
}
if (rela_sz % rela_entry != 0) {
warnx("%s: inconsistent values for DT_RELASZ and "
"DT_RELAENT", ef->ef_name);
return (EFTYPE);
}
if (ef_read_entry(ef, ef_get_offset(ef, rela_off), rela_sz,
(void **)&ef->ef_rela) != 0) {
warnx("%s: cannot load DT_RELA section", ef->ef_name);
return (EIO);
}
ef->ef_relasz = rela_sz / rela_entry;
if (ef->ef_verbose)
warnx("%s: %d RELA entries", ef->ef_name,
ef->ef_relasz);
}
return 0;
}
static int
ef_read(elf_file_t ef, Elf_Off offset, size_t len, void*dest)
{
ssize_t r;
if (offset != (Elf_Off)-1) {
if (lseek(ef->ef_fd, offset, SEEK_SET) == -1)
return EIO;
}
r = read(ef->ef_fd, dest, len);
if (r != -1 && (size_t)r == len)
return 0;
else
return EIO;
}
static int
ef_read_entry(elf_file_t ef, Elf_Off offset, size_t len, void**ptr)
{
int error;
*ptr = malloc(len);
if (*ptr == NULL)
return ENOMEM;
error = ef_read(ef, offset, len, *ptr);
if (error)
free(*ptr);
return error;
}
static int
ef_seg_read(elf_file_t ef, Elf_Off offset, size_t len, void*dest)
{
u_long ofs = ef_get_offset(ef, offset);
if (ofs == 0) {
if (ef->ef_verbose)
warnx("ef_seg_read(%s): zero offset (%lx:%ld)",
ef->ef_name, (long)offset, ofs);
return EFAULT;
}
return ef_read(ef, ofs, len, dest);
}
static int
ef_seg_read_rel(elf_file_t ef, Elf_Off offset, size_t len, void*dest)
{
u_long ofs = ef_get_offset(ef, offset);
const Elf_Rela *a;
const Elf_Rel *r;
int error;
if (ofs == 0) {
if (ef->ef_verbose)
warnx("ef_seg_read(%s): zero offset (%lx:%ld)",
ef->ef_name, (long)offset, ofs);
return EFAULT;
}
if ((error = ef_read(ef, ofs, len, dest)) != 0)
return (error);
for (r = ef->ef_rel; r < &ef->ef_rel[ef->ef_relsz]; r++) {
error = ef_reloc(ef->ef_efile, r, EF_RELOC_REL, 0, offset, len,
dest);
if (error != 0)
return (error);
}
for (a = ef->ef_rela; a < &ef->ef_rela[ef->ef_relasz]; a++) {
error = ef_reloc(ef->ef_efile, a, EF_RELOC_RELA, 0, offset, len,
dest);
if (error != 0)
return (error);
}
return (0);
}
static int
ef_seg_read_entry(elf_file_t ef, Elf_Off offset, size_t len, void**ptr)
{
int error;
*ptr = malloc(len);
if (*ptr == NULL)
return ENOMEM;
error = ef_seg_read(ef, offset, len, *ptr);
if (error)
free(*ptr);
return error;
}
static int
ef_seg_read_entry_rel(elf_file_t ef, Elf_Off offset, size_t len, void**ptr)
{
int error;
*ptr = malloc(len);
if (*ptr == NULL)
return ENOMEM;
error = ef_seg_read_rel(ef, offset, len, *ptr);
if (error)
free(*ptr);
return error;
}
int
ef_open(const char *filename, struct elf_file *efile, int verbose)
{
elf_file_t ef;
Elf_Ehdr *hdr;
int fd;
int error;
int phlen, res;
int nsegs;
Elf_Phdr *phdr, *phdyn, *phphdr, *phlimit;
if (filename == NULL)
return EFTYPE;
if ((fd = open(filename, O_RDONLY)) == -1)
return errno;
ef = malloc(sizeof(*ef));
if (ef == NULL) {
close(fd);
return (ENOMEM);
}
efile->ef_ef = ef;
efile->ef_ops = &ef_file_ops;
bzero(ef, sizeof(*ef));
ef->ef_verbose = verbose;
ef->ef_fd = fd;
ef->ef_name = strdup(filename);
ef->ef_efile = efile;
hdr = (Elf_Ehdr *)&ef->ef_hdr;
do {
res = read(fd, hdr, sizeof(*hdr));
error = EFTYPE;
if (res != sizeof(*hdr))
break;
if (!IS_ELF(*hdr))
break;
if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
hdr->e_ident[EI_VERSION] != EV_CURRENT ||
hdr->e_version != EV_CURRENT ||
hdr->e_machine != ELF_TARG_MACH ||
hdr->e_phentsize != sizeof(Elf_Phdr))
break;
phlen = hdr->e_phnum * sizeof(Elf_Phdr);
if (ef_read_entry(ef, hdr->e_phoff, phlen,
(void**)&ef->ef_ph) != 0)
break;
phdr = ef->ef_ph;
phlimit = phdr + hdr->e_phnum;
nsegs = 0;
phdyn = NULL;
phphdr = NULL;
while (phdr < phlimit) {
if (verbose > 1)
ef_print_phdr(phdr);
switch (phdr->p_type) {
case PT_LOAD:
if (nsegs == 2) {
warnx("%s: too many sections",
filename);
break;
}
ef->ef_segs[nsegs++] = phdr;
break;
case PT_PHDR:
phphdr = phdr;
break;
case PT_DYNAMIC:
phdyn = phdr;
break;
}
phdr++;
}
if (verbose > 1)
printf("\n");
ef->ef_nsegs = nsegs;
if (phdyn == NULL) {
warnx("file isn't dynamically-linked");
break;
}
if (ef_read_entry(ef, phdyn->p_offset,
phdyn->p_filesz, (void**)&ef->ef_dyn) != 0) {
printf("ef_read_entry failed\n");
break;
}
error = ef_parse_dynamic(ef);
if (error)
break;
if (hdr->e_type == ET_DYN) {
ef->ef_type = EFT_KLD;
/* pad = (u_int)dest & PAGE_MASK;
if (pad)
dest += PAGE_SIZE - pad;*/
error = 0;
} else if (hdr->e_type == ET_EXEC) {
/* dest = hdr->e_entry;
if (dest == 0)
break;*/
ef->ef_type = EFT_KERNEL;
error = 0;
} else
break;
} while(0);
if (error)
ef_close(ef);
return error;
}
static int
ef_close(elf_file_t ef)
{
close(ef->ef_fd);
/* if (ef->ef_fpage)
free(ef->ef_fpage);*/
if (ef->ef_name)
free(ef->ef_name);
ef->ef_efile->ef_ops = NULL;
ef->ef_efile->ef_ef = NULL;
free(ef);
return 0;
}
|