diff options
-rw-r--r-- | usr.sbin/kldxref/Makefile | 6 | ||||
-rw-r--r-- | usr.sbin/kldxref/ef.c | 122 | ||||
-rw-r--r-- | usr.sbin/kldxref/ef.h | 7 | ||||
-rw-r--r-- | usr.sbin/kldxref/ef_nop.c | 39 | ||||
-rw-r--r-- | usr.sbin/kldxref/ef_sparc64.c | 63 | ||||
-rw-r--r-- | usr.sbin/kldxref/kldxref.c | 5 |
6 files changed, 239 insertions, 3 deletions
diff --git a/usr.sbin/kldxref/Makefile b/usr.sbin/kldxref/Makefile index a7ad8d2..e7fa5c8 100644 --- a/usr.sbin/kldxref/Makefile +++ b/usr.sbin/kldxref/Makefile @@ -5,4 +5,10 @@ SRCS= kldxref.c ef.c WARNS?= 2 MAN= kldxref.8 +.if exists(ef_${MACHINE_ARCH}.c) +SRCS+= ef_${MACHINE_ARCH}.c +.else +SRCS+= ef_nop.c +.endif + .include <bsd.prog.mk> diff --git a/usr.sbin/kldxref/ef.c b/usr.sbin/kldxref/ef.c index 9490471..142c9cb 100644 --- a/usr.sbin/kldxref/ef.c +++ b/usr.sbin/kldxref/ef.c @@ -151,7 +151,16 @@ ef_parse_dynamic(elf_file_t ef) 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: @@ -187,6 +196,36 @@ ef_parse_dynamic(elf_file_t ef) 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) { @@ -210,6 +249,56 @@ ef_parse_dynamic(elf_file_t ef) 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; } @@ -259,6 +348,23 @@ ef_seg_read(elf_file_t ef, Elf_Off offset, size_t len, void*dest) } 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); + 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); + return (ef_reloc(ef, offset, len, dest)); +} + +int ef_seg_read_entry(elf_file_t ef, Elf_Off offset, size_t len, void**ptr) { int error; @@ -273,6 +379,20 @@ ef_seg_read_entry(elf_file_t ef, Elf_Off offset, size_t len, void**ptr) } 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, elf_file_t ef, int verbose) { Elf_Ehdr *hdr; diff --git a/usr.sbin/kldxref/ef.h b/usr.sbin/kldxref/ef.h index 1609bb8..f9a7884 100644 --- a/usr.sbin/kldxref/ef.h +++ b/usr.sbin/kldxref/ef.h @@ -28,6 +28,10 @@ typedef struct elf_file { 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 */ } *elf_file_t; __BEGIN_DECLS @@ -35,8 +39,11 @@ int ef_open(const char *, elf_file_t, int); int ef_close(elf_file_t ef); int ef_read(elf_file_t ef, Elf_Off offset, size_t len, void* dest); int ef_read_entry(elf_file_t ef, Elf_Off offset, size_t len, void **ptr); +int ef_reloc(elf_file_t ef, Elf_Off offset, size_t len, void *dest); int ef_seg_read(elf_file_t ef, Elf_Off offset, size_t len, void *dest); +int ef_seg_read_rel(elf_file_t ef, Elf_Off offset, size_t len, void *dest); int ef_seg_read_entry(elf_file_t ef, Elf_Off offset, size_t len, void**ptr); +int ef_seg_read_entry_rel(elf_file_t ef, Elf_Off offset, size_t len, void**ptr); int ef_lookup_symbol(elf_file_t ef, const char* name, Elf_Sym** sym); __END_DECLS diff --git a/usr.sbin/kldxref/ef_nop.c b/usr.sbin/kldxref/ef_nop.c new file mode 100644 index 0000000..3fff5de --- /dev/null +++ b/usr.sbin/kldxref/ef_nop.c @@ -0,0 +1,39 @@ +/*- + * Copyright (c) 2003 Jake Burkholder. + * 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. + * + * 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/types.h> +#include <machine/elf.h> + +#include "ef.h" + +int +ef_reloc(elf_file_t ef, Elf_Off offset, size_t len, void *dest) +{ + + return (0); +} diff --git a/usr.sbin/kldxref/ef_sparc64.c b/usr.sbin/kldxref/ef_sparc64.c new file mode 100644 index 0000000..bc728a4 --- /dev/null +++ b/usr.sbin/kldxref/ef_sparc64.c @@ -0,0 +1,63 @@ +/*- + * Copyright (c) 2003 Jake Burkholder. + * 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. + * + * 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/types.h> +#include <machine/elf.h> + +#include <err.h> +#include <string.h> + +#include "ef.h" + +/* + * Apply relocations to the values we got from the file. + */ +int +ef_reloc(elf_file_t ef, Elf_Off offset, size_t len, void *dest) +{ + const Elf_Rela *a; + Elf_Word w; + + for (a = ef->ef_rela; a < &ef->ef_rela[ef->ef_relasz]; a++) { + if (a->r_offset >= offset && a->r_offset < offset + len) { + switch (ELF_R_TYPE(a->r_info)) { + case R_SPARC_RELATIVE: + /* load address is 0 */ + w = a->r_addend; + memcpy((u_char *)dest + (a->r_offset - offset), + &w, sizeof(w)); + break; + default: + warnx("unhandled relocation type %u", + ELF_R_TYPE(a->r_info)); + break; + } + } + } + return (0); +} diff --git a/usr.sbin/kldxref/kldxref.c b/usr.sbin/kldxref/kldxref.c index e631ecc..c3b5593 100644 --- a/usr.sbin/kldxref/kldxref.c +++ b/usr.sbin/kldxref/kldxref.c @@ -229,10 +229,11 @@ read_kld(char *filename, char *kldname) check(ef_lookup_symbol(&ef, "__stop_set_" MDT_SETNAME, &sym)); finish = sym->st_value; entries = (finish - start) / sizeof(void *); - check(ef_seg_read_entry(&ef, start, sizeof(*p) * entries, (void**)&p)); + check(ef_seg_read_entry_rel(&ef, start, sizeof(*p) * entries, + (void**)&p)); orgp = p; while(entries--) { - check(ef_seg_read(&ef, (Elf_Off)*p, sizeof(md), &md)); + check(ef_seg_read_rel(&ef, (Elf_Off)*p, sizeof(md), &md)); p++; check(ef_seg_read(&ef, (Elf_Off)md.md_cval, sizeof(cval), cval)); cval[MAXMODNAME] = '\0'; |