2 * Copyright (c) 2000, Boris Popov
3 * Copyright (c) 1998-2000 Doug Rabson
4 * Copyright (c) 2004 Peter Wemm
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Boris Popov.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/param.h>
38 #include <sys/linker.h>
45 #include <machine/elf.h>
56 int sec; /* Original section */
76 struct elf_file *ef_efile;
91 Elf_Sym *ddbsymtab; /* The symbol table we are using */
92 long ddbsymcnt; /* Number of symbols */
93 caddr_t ddbstrtab; /* String table */
94 long ddbstrcnt; /* number of bytes in string table */
96 caddr_t shstrtab; /* Section name string table */
97 long shstrcnt; /* number of bytes in string table */
102 static int ef_obj_get_type(elf_file_t ef);
103 static int ef_obj_close(elf_file_t ef);
104 static int ef_obj_read(elf_file_t ef, Elf_Off offset, size_t len, void* dest);
105 static int ef_obj_read_entry(elf_file_t ef, Elf_Off offset, size_t len,
107 static int ef_obj_seg_read(elf_file_t ef, Elf_Off offset, size_t len,
109 static int ef_obj_seg_read_rel(elf_file_t ef, Elf_Off offset, size_t len,
111 static int ef_obj_seg_read_string(elf_file_t ef, Elf_Off offset, size_t len,
113 static int ef_obj_seg_read_entry(elf_file_t ef, Elf_Off offset, size_t len,
115 static int ef_obj_seg_read_entry_rel(elf_file_t ef, Elf_Off offset, size_t len,
117 static Elf_Addr ef_obj_symaddr(elf_file_t ef, Elf_Size symidx);
118 static int ef_obj_lookup_set(elf_file_t ef, const char *name, long *startp,
119 long *stopp, long *countp);
120 static int ef_obj_lookup_symbol(elf_file_t ef, const char* name, Elf_Sym** sym);
122 static struct elf_file_ops ef_obj_file_ops = {
129 ef_obj_seg_read_string,
130 ef_obj_seg_read_entry,
131 ef_obj_seg_read_entry_rel,
138 ef_obj_get_type(elf_file_t __unused ef)
145 ef_obj_lookup_symbol(elf_file_t ef, const char* name, Elf_Sym** sym)
151 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
152 strp = ef->ddbstrtab + symp->st_name;
153 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
162 ef_obj_lookup_set(elf_file_t ef, const char *name, long *startp, long *stopp,
167 for (i = 0; i < ef->nprogtab; i++) {
168 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
169 strcmp(ef->progtab[i].name + 4, name) == 0) {
170 *startp = (char *)ef->progtab[i].addr - ef->address;
171 *stopp = (char *)ef->progtab[i].addr +
172 ef->progtab[i].size - ef->address;
173 *countp = (*stopp - *startp) / sizeof(void *);
181 ef_obj_symaddr(elf_file_t ef, Elf_Size symidx)
185 if (symidx >= (size_t) ef->ddbsymcnt)
187 sym = ef->ddbsymtab + symidx;
189 if (sym->st_shndx != SHN_UNDEF)
190 return (sym->st_value - (Elf_Addr)ef->address);
195 ef_obj_read(elf_file_t ef, Elf_Off offset, size_t len, void *dest)
199 if (offset != (Elf_Off)-1) {
200 if (lseek(ef->ef_fd, offset, SEEK_SET) == -1)
204 r = read(ef->ef_fd, dest, len);
205 if (r != -1 && (size_t)r == len)
212 ef_obj_read_entry(elf_file_t ef, Elf_Off offset, size_t len, void **ptr)
219 error = ef_obj_read(ef, offset, len, *ptr);
226 ef_obj_seg_read(elf_file_t ef, Elf_Off offset, size_t len, void *dest)
229 if (offset + len > ef->size) {
231 warnx("ef_obj_seg_read(%s): bad offset/len (%lx:%ld)",
232 ef->ef_name, (long)offset, (long)len);
235 bcopy(ef->address + offset, dest, len);
240 ef_obj_seg_read_rel(elf_file_t ef, Elf_Off offset, size_t len, void *dest)
245 Elf_Off secbase, dataoff;
248 if (offset + len > ef->size) {
250 warnx("ef_obj_seg_read_rel(%s): bad offset/len (%lx:%ld)",
251 ef->ef_name, (long)offset, (long)len);
254 bcopy(ef->address + offset, dest, len);
256 /* Find out which section contains the data. */
257 memaddr = ef->address + offset;
259 secbase = dataoff = 0;
260 for (i = 0; i < ef->nprogtab; i++) {
261 if (ef->progtab[i].addr == NULL)
263 if (memaddr < (char *)ef->progtab[i].addr || memaddr + len >
264 (char *)ef->progtab[i].addr + ef->progtab[i].size)
266 sec = ef->progtab[i].sec;
267 /* We relocate to address 0. */
268 secbase = (char *)ef->progtab[i].addr - ef->address;
269 dataoff = memaddr - ef->address;
276 /* Now do the relocations. */
277 for (i = 0; i < ef->nrel; i++) {
278 if (ef->reltab[i].sec != sec)
280 for (r = ef->reltab[i].rel;
281 r < &ef->reltab[i].rel[ef->reltab[i].nrel]; r++) {
282 error = ef_reloc(ef->ef_efile, r, EF_RELOC_REL, secbase,
288 for (i = 0; i < ef->nrela; i++) {
289 if (ef->relatab[i].sec != sec)
291 for (a = ef->relatab[i].rela;
292 a < &ef->relatab[i].rela[ef->relatab[i].nrela]; a++) {
293 error = ef_reloc(ef->ef_efile, a, EF_RELOC_RELA,
294 secbase, dataoff, len, dest);
303 ef_obj_seg_read_string(elf_file_t ef, Elf_Off offset, size_t len, char *dest)
306 if (offset >= ef->size) {
308 warnx("ef_obj_seg_read_string(%s): bad offset (%lx)",
309 ef->ef_name, (long)offset);
313 if (ef->size - offset < len)
314 len = ef->size - offset;
316 if (strnlen(ef->address + offset, len) == len)
319 memcpy(dest, ef->address + offset, len);
324 ef_obj_seg_read_entry(elf_file_t ef, Elf_Off offset, size_t len, void **ptr)
331 error = ef_obj_seg_read(ef, offset, len, *ptr);
338 ef_obj_seg_read_entry_rel(elf_file_t ef, Elf_Off offset, size_t len,
346 error = ef_obj_seg_read_rel(ef, offset, len, *ptr);
353 ef_obj_open(const char *filename, struct elf_file *efile, int verbose)
361 size_t mapsize, alignmask, max_addralign;
362 int error, fd, pb, ra, res, rl;
363 int i, j, nbytes, nsym, shstrindex, symstrindex, symtabindex;
365 if (filename == NULL)
367 if ((fd = open(filename, O_RDONLY)) == -1)
370 ef = calloc(1, sizeof(*ef));
377 efile->ef_ops = &ef_obj_file_ops;
379 ef->ef_verbose = verbose;
381 ef->ef_name = strdup(filename);
382 ef->ef_efile = efile;
383 hdr = (Elf_Ehdr *)&ef->ef_hdr;
385 res = read(fd, hdr, sizeof(*hdr));
387 if (res != sizeof(*hdr))
391 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
392 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
393 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
394 hdr->e_version != EV_CURRENT || hdr->e_machine != ELF_TARG_MACH ||
395 hdr->e_type != ET_REL)
398 nbytes = hdr->e_shnum * hdr->e_shentsize;
399 if (nbytes == 0 || hdr->e_shoff == 0 ||
400 hdr->e_shentsize != sizeof(Elf_Shdr))
403 if (ef_obj_read_entry(ef, hdr->e_shoff, nbytes, &vtmp) != 0) {
404 printf("ef_read_entry failed\n");
407 ef->e_shdr = shdr = vtmp;
409 /* Scan the section header for information and table sizing. */
413 for (i = 0; i < hdr->e_shnum; i++) {
414 switch (shdr[i].sh_type) {
422 symstrindex = shdr[i].sh_link;
435 if (ef->nprogtab == 0) {
436 warnx("%s: file has no contents", filename);
440 warnx("%s: file has no valid symbol table", filename);
443 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
444 shdr[symstrindex].sh_type != SHT_STRTAB) {
445 warnx("%s: file has invalid symbol strings", filename);
449 /* Allocate space for tracking the load chunks */
450 if (ef->nprogtab != 0)
451 ef->progtab = calloc(ef->nprogtab, sizeof(*ef->progtab));
453 ef->reltab = calloc(ef->nrel, sizeof(*ef->reltab));
455 ef->relatab = calloc(ef->nrela, sizeof(*ef->relatab));
456 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
457 (ef->nrel != 0 && ef->reltab == NULL) ||
458 (ef->nrela != 0 && ef->relatab == NULL)) {
459 printf("malloc failed\n");
464 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
465 if (ef_obj_read_entry(ef, shdr[symtabindex].sh_offset,
466 shdr[symtabindex].sh_size, (void**)&ef->ddbsymtab) != 0) {
467 printf("ef_read_entry failed\n");
471 ef->ddbstrcnt = shdr[symstrindex].sh_size;
472 if (ef_obj_read_entry(ef, shdr[symstrindex].sh_offset,
473 shdr[symstrindex].sh_size, (void**)&ef->ddbstrtab) != 0) {
474 printf("ef_read_entry failed\n");
478 /* Do we have a string table for the section names? */
480 if (hdr->e_shstrndx != 0 &&
481 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
482 shstrindex = hdr->e_shstrndx;
483 ef->shstrcnt = shdr[shstrindex].sh_size;
484 if (ef_obj_read_entry(ef, shdr[shstrindex].sh_offset,
485 shdr[shstrindex].sh_size, (void**)&ef->shstrtab) != 0) {
486 printf("ef_read_entry failed\n");
491 /* Size up code/data(progbits) and bss(nobits). */
495 for (i = 0; i < hdr->e_shnum; i++) {
496 switch (shdr[i].sh_type) {
499 alignmask = shdr[i].sh_addralign - 1;
500 if (shdr[i].sh_addralign > max_addralign)
501 max_addralign = shdr[i].sh_addralign;
502 mapsize += alignmask;
503 mapsize &= ~alignmask;
504 mapsize += shdr[i].sh_size;
509 /* We know how much space we need for the text/data/bss/etc. */
511 if (posix_memalign((void **)&ef->address, max_addralign, mapsize)) {
512 printf("posix_memalign failed\n");
515 mapbase = ef->address;
518 * Now load code/data(progbits), zero bss(nobits), allocate
519 * space for and load relocs
525 for (i = 0; i < hdr->e_shnum; i++) {
526 switch (shdr[i].sh_type) {
529 alignmask = shdr[i].sh_addralign - 1;
530 mapbase += alignmask;
531 mapbase = (char *)((uintptr_t)mapbase & ~alignmask);
532 ef->progtab[pb].addr = (void *)(uintptr_t)mapbase;
533 if (shdr[i].sh_type == SHT_PROGBITS) {
534 ef->progtab[pb].name = "<<PROGBITS>>";
535 if (ef_obj_read(ef, shdr[i].sh_offset,
537 ef->progtab[pb].addr) != 0) {
538 printf("failed to read progbits\n");
542 ef->progtab[pb].name = "<<NOBITS>>";
543 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
545 ef->progtab[pb].size = shdr[i].sh_size;
546 ef->progtab[pb].sec = i;
547 if (ef->shstrtab && shdr[i].sh_name != 0)
548 ef->progtab[pb].name =
549 ef->shstrtab + shdr[i].sh_name;
551 /* Update all symbol values with the offset. */
552 for (j = 0; j < ef->ddbsymcnt; j++) {
553 es = &ef->ddbsymtab[j];
554 if (es->st_shndx != i)
556 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
558 mapbase += shdr[i].sh_size;
562 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
563 ef->reltab[rl].sec = shdr[i].sh_info;
564 if (ef_obj_read_entry(ef, shdr[i].sh_offset,
565 shdr[i].sh_size, (void**)&ef->reltab[rl].rel) !=
567 printf("ef_read_entry failed\n");
573 ef->relatab[ra].nrela =
574 shdr[i].sh_size / sizeof(Elf_Rela);
575 ef->relatab[ra].sec = shdr[i].sh_info;
576 if (ef_obj_read_entry(ef, shdr[i].sh_offset,
577 shdr[i].sh_size, (void**)&ef->relatab[ra].rela) !=
579 printf("ef_read_entry failed\n");
594 ef_obj_close(elf_file_t ef)
601 if (ef->e_shdr != NULL)
605 if (ef->nprogtab != 0)
608 for (i = 0; i < ef->nrel; i++)
609 if (ef->reltab[i].rel != NULL)
610 free(ef->reltab[i].rel);
613 if (ef->nrela != 0) {
614 for (i = 0; i < ef->nrela; i++)
615 if (ef->relatab[i].rela != NULL)
616 free(ef->relatab[i].rela);
619 if (ef->ddbsymtab != NULL)
621 if (ef->ddbstrtab != NULL)
623 if (ef->shstrtab != NULL)
625 ef->ef_efile->ef_ops = NULL;
626 ef->ef_efile->ef_ef = NULL;