2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2000, Boris Popov
5 * Copyright (c) 1998-2000 Doug Rabson
6 * Copyright (c) 2004 Peter Wemm
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Boris Popov.
20 * 4. Neither the name of the author nor the names of any co-contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/param.h>
40 #include <sys/linker.h>
47 #include <machine/elf.h>
58 int sec; /* Original section */
78 struct elf_file *ef_efile;
93 Elf_Sym *ddbsymtab; /* The symbol table we are using */
94 long ddbsymcnt; /* Number of symbols */
95 caddr_t ddbstrtab; /* String table */
96 long ddbstrcnt; /* number of bytes in string table */
98 caddr_t shstrtab; /* Section name string table */
99 long shstrcnt; /* number of bytes in string table */
104 static int ef_obj_get_type(elf_file_t ef);
105 static int ef_obj_close(elf_file_t ef);
106 static int ef_obj_read(elf_file_t ef, Elf_Off offset, size_t len, void* dest);
107 static int ef_obj_read_entry(elf_file_t ef, Elf_Off offset, size_t len,
109 static int ef_obj_seg_read(elf_file_t ef, Elf_Off offset, size_t len,
111 static int ef_obj_seg_read_rel(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_entry,
130 ef_obj_seg_read_entry_rel,
137 ef_obj_get_type(elf_file_t __unused ef)
144 ef_obj_lookup_symbol(elf_file_t ef, const char* name, Elf_Sym** sym)
150 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
151 strp = ef->ddbstrtab + symp->st_name;
152 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
161 ef_obj_lookup_set(elf_file_t ef, const char *name, long *startp, long *stopp,
166 for (i = 0; i < ef->nprogtab; i++) {
167 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
168 strcmp(ef->progtab[i].name + 4, name) == 0) {
169 *startp = (char *)ef->progtab[i].addr - ef->address;
170 *stopp = (char *)ef->progtab[i].addr +
171 ef->progtab[i].size - ef->address;
172 *countp = (*stopp - *startp) / sizeof(void *);
180 ef_obj_symaddr(elf_file_t ef, Elf_Size symidx)
184 if (symidx >= (size_t) ef->ddbsymcnt)
186 sym = ef->ddbsymtab + symidx;
188 if (sym->st_shndx != SHN_UNDEF)
189 return (sym->st_value - (Elf_Addr)ef->address);
194 ef_obj_read(elf_file_t ef, Elf_Off offset, size_t len, void *dest)
198 if (offset != (Elf_Off)-1) {
199 if (lseek(ef->ef_fd, offset, SEEK_SET) == -1)
203 r = read(ef->ef_fd, dest, len);
204 if (r != -1 && (size_t)r == len)
211 ef_obj_read_entry(elf_file_t ef, Elf_Off offset, size_t len, void **ptr)
218 error = ef_obj_read(ef, offset, len, *ptr);
225 ef_obj_seg_read(elf_file_t ef, Elf_Off offset, size_t len, void *dest)
228 if (offset + len > ef->size) {
230 warnx("ef_obj_seg_read(%s): bad offset/len (%lx:%ld)",
231 ef->ef_name, (long)offset, (long)len);
234 bcopy(ef->address + offset, dest, len);
239 ef_obj_seg_read_rel(elf_file_t ef, Elf_Off offset, size_t len, void *dest)
244 Elf_Off secbase, dataoff;
247 if (offset + len > ef->size) {
249 warnx("ef_obj_seg_read_rel(%s): bad offset/len (%lx:%ld)",
250 ef->ef_name, (long)offset, (long)len);
253 bcopy(ef->address + offset, dest, len);
255 /* Find out which section contains the data. */
256 memaddr = ef->address + offset;
258 secbase = dataoff = 0;
259 for (i = 0; i < ef->nprogtab; i++) {
260 if (ef->progtab[i].addr == NULL)
262 if (memaddr < (char *)ef->progtab[i].addr || memaddr + len >
263 (char *)ef->progtab[i].addr + ef->progtab[i].size)
265 sec = ef->progtab[i].sec;
266 /* We relocate to address 0. */
267 secbase = (char *)ef->progtab[i].addr - ef->address;
268 dataoff = memaddr - ef->address;
275 /* Now do the relocations. */
276 for (i = 0; i < ef->nrel; i++) {
277 if (ef->reltab[i].sec != sec)
279 for (r = ef->reltab[i].rel;
280 r < &ef->reltab[i].rel[ef->reltab[i].nrel]; r++) {
281 error = ef_reloc(ef->ef_efile, r, EF_RELOC_REL, secbase,
287 for (i = 0; i < ef->nrela; i++) {
288 if (ef->relatab[i].sec != sec)
290 for (a = ef->relatab[i].rela;
291 a < &ef->relatab[i].rela[ef->relatab[i].nrela]; a++) {
292 error = ef_reloc(ef->ef_efile, a, EF_RELOC_RELA,
293 secbase, dataoff, len, dest);
302 ef_obj_seg_read_entry(elf_file_t ef, Elf_Off offset, size_t len, void **ptr)
309 error = ef_obj_seg_read(ef, offset, len, *ptr);
316 ef_obj_seg_read_entry_rel(elf_file_t ef, Elf_Off offset, size_t len,
324 error = ef_obj_seg_read_rel(ef, offset, len, *ptr);
331 ef_obj_open(const char *filename, struct elf_file *efile, int verbose)
339 size_t mapsize, alignmask, max_addralign;
340 int error, fd, pb, ra, res, rl;
341 int i, j, nbytes, nsym, shstrindex, symstrindex, symtabindex;
343 if (filename == NULL)
345 if ((fd = open(filename, O_RDONLY)) == -1)
348 ef = calloc(1, sizeof(*ef));
355 efile->ef_ops = &ef_obj_file_ops;
357 ef->ef_verbose = verbose;
359 ef->ef_name = strdup(filename);
360 ef->ef_efile = efile;
361 hdr = (Elf_Ehdr *)&ef->ef_hdr;
363 res = read(fd, hdr, sizeof(*hdr));
365 if (res != sizeof(*hdr))
369 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
370 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
371 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
372 hdr->e_version != EV_CURRENT || hdr->e_machine != ELF_TARG_MACH ||
373 hdr->e_type != ET_REL)
376 nbytes = hdr->e_shnum * hdr->e_shentsize;
377 if (nbytes == 0 || hdr->e_shoff == 0 ||
378 hdr->e_shentsize != sizeof(Elf_Shdr))
381 if (ef_obj_read_entry(ef, hdr->e_shoff, nbytes, &vtmp) != 0) {
382 printf("ef_read_entry failed\n");
385 ef->e_shdr = shdr = vtmp;
387 /* Scan the section header for information and table sizing. */
391 for (i = 0; i < hdr->e_shnum; i++) {
392 switch (shdr[i].sh_type) {
400 symstrindex = shdr[i].sh_link;
413 if (ef->nprogtab == 0) {
414 warnx("%s: file has no contents", filename);
418 warnx("%s: file has no valid symbol table", filename);
421 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
422 shdr[symstrindex].sh_type != SHT_STRTAB) {
423 warnx("%s: file has invalid symbol strings", filename);
427 /* Allocate space for tracking the load chunks */
428 if (ef->nprogtab != 0)
429 ef->progtab = calloc(ef->nprogtab, sizeof(*ef->progtab));
431 ef->reltab = calloc(ef->nrel, sizeof(*ef->reltab));
433 ef->relatab = calloc(ef->nrela, sizeof(*ef->relatab));
434 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
435 (ef->nrel != 0 && ef->reltab == NULL) ||
436 (ef->nrela != 0 && ef->relatab == NULL)) {
437 printf("malloc failed\n");
442 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
443 if (ef_obj_read_entry(ef, shdr[symtabindex].sh_offset,
444 shdr[symtabindex].sh_size, (void**)&ef->ddbsymtab) != 0) {
445 printf("ef_read_entry failed\n");
449 ef->ddbstrcnt = shdr[symstrindex].sh_size;
450 if (ef_obj_read_entry(ef, shdr[symstrindex].sh_offset,
451 shdr[symstrindex].sh_size, (void**)&ef->ddbstrtab) != 0) {
452 printf("ef_read_entry failed\n");
456 /* Do we have a string table for the section names? */
458 if (hdr->e_shstrndx != 0 &&
459 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
460 shstrindex = hdr->e_shstrndx;
461 ef->shstrcnt = shdr[shstrindex].sh_size;
462 if (ef_obj_read_entry(ef, shdr[shstrindex].sh_offset,
463 shdr[shstrindex].sh_size, (void**)&ef->shstrtab) != 0) {
464 printf("ef_read_entry failed\n");
469 /* Size up code/data(progbits) and bss(nobits). */
473 for (i = 0; i < hdr->e_shnum; i++) {
474 switch (shdr[i].sh_type) {
477 alignmask = shdr[i].sh_addralign - 1;
478 if (shdr[i].sh_addralign > max_addralign)
479 max_addralign = shdr[i].sh_addralign;
480 mapsize += alignmask;
481 mapsize &= ~alignmask;
482 mapsize += shdr[i].sh_size;
487 /* We know how much space we need for the text/data/bss/etc. */
489 if (posix_memalign((void **)&ef->address, max_addralign, mapsize)) {
490 printf("posix_memalign failed\n");
493 mapbase = ef->address;
496 * Now load code/data(progbits), zero bss(nobits), allocate
497 * space for and load relocs
503 for (i = 0; i < hdr->e_shnum; i++) {
504 switch (shdr[i].sh_type) {
507 alignmask = shdr[i].sh_addralign - 1;
508 mapbase += alignmask;
509 mapbase = (char *)((uintptr_t)mapbase & ~alignmask);
510 ef->progtab[pb].addr = (void *)(uintptr_t)mapbase;
511 if (shdr[i].sh_type == SHT_PROGBITS) {
512 ef->progtab[pb].name = "<<PROGBITS>>";
513 if (ef_obj_read(ef, shdr[i].sh_offset,
515 ef->progtab[pb].addr) != 0) {
516 printf("failed to read progbits\n");
520 ef->progtab[pb].name = "<<NOBITS>>";
521 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
523 ef->progtab[pb].size = shdr[i].sh_size;
524 ef->progtab[pb].sec = i;
525 if (ef->shstrtab && shdr[i].sh_name != 0)
526 ef->progtab[pb].name =
527 ef->shstrtab + shdr[i].sh_name;
529 /* Update all symbol values with the offset. */
530 for (j = 0; j < ef->ddbsymcnt; j++) {
531 es = &ef->ddbsymtab[j];
532 if (es->st_shndx != i)
534 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
536 mapbase += shdr[i].sh_size;
540 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
541 ef->reltab[rl].sec = shdr[i].sh_info;
542 if (ef_obj_read_entry(ef, shdr[i].sh_offset,
543 shdr[i].sh_size, (void**)&ef->reltab[rl].rel) !=
545 printf("ef_read_entry failed\n");
551 ef->relatab[ra].nrela =
552 shdr[i].sh_size / sizeof(Elf_Rela);
553 ef->relatab[ra].sec = shdr[i].sh_info;
554 if (ef_obj_read_entry(ef, shdr[i].sh_offset,
555 shdr[i].sh_size, (void**)&ef->relatab[ra].rela) !=
557 printf("ef_read_entry failed\n");
572 ef_obj_close(elf_file_t ef)
579 if (ef->e_shdr != NULL)
583 if (ef->nprogtab != 0)
586 for (i = 0; i < ef->nrel; i++)
587 if (ef->reltab[i].rel != NULL)
588 free(ef->reltab[i].rel);
591 if (ef->nrela != 0) {
592 for (i = 0; i < ef->nrela; i++)
593 if (ef->relatab[i].rela != NULL)
594 free(ef->relatab[i].rela);
597 if (ef->ddbsymtab != NULL)
599 if (ef->ddbstrtab != NULL)
601 if (ef->shstrtab != NULL)
603 ef->ef_efile->ef_ops = NULL;
604 ef->ef_efile->ef_ef = NULL;