From 2bd7b9e5cc98da40bbd8e07aaa1000e3bc651753 Mon Sep 17 00:00:00 2001 From: Ed Maste Date: Wed, 30 Apr 2014 18:11:53 +0000 Subject: [PATCH] kldxref: Clean up error reporting Omit "too many sections" warnings if the ELF file is not dynamically linked (and is therefore skipped anyway), and otherwise output it only once. An errant core file would previously cause kldxref to output a number of warnings. Also introduce a MAXSEGS #define and replace literal 2 with it, to make comparisons clear. Reviewed by: kib Sponsored by: The FreeBSD Foundation --- usr.sbin/kldxref/ef.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/usr.sbin/kldxref/ef.c b/usr.sbin/kldxref/ef.c index b6838980134..88fbc34a43b 100644 --- a/usr.sbin/kldxref/ef.c +++ b/usr.sbin/kldxref/ef.c @@ -47,6 +47,7 @@ #include "ef.h" +#define MAXSEGS 2 struct ef_file { char* ef_name; struct elf_file *ef_efile; @@ -68,7 +69,7 @@ struct ef_file { Elf_Off ef_symoff; Elf_Sym* ef_symtab; int ef_nsegs; - Elf_Phdr * ef_segs[2]; + Elf_Phdr * ef_segs[MAXSEGS]; int ef_verbose; Elf_Rel * ef_rel; /* relocation table */ int ef_relsz; /* number of entries */ @@ -580,12 +581,9 @@ ef_open(const char *filename, struct elf_file *efile, int verbose) 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; + if (nsegs < MAXSEGS) + ef->ef_segs[nsegs] = phdr; + nsegs++; break; case PT_PHDR: break; @@ -597,12 +595,15 @@ ef_open(const char *filename, struct elf_file *efile, int verbose) } if (verbose > 1) printf("\n"); - ef->ef_nsegs = nsegs; if (phdyn == NULL) { warnx("Skipping %s: not dynamically-linked", filename); break; + } else if (nsegs > MAXSEGS) { + warnx("%s: too many sections", filename); + break; } + ef->ef_nsegs = nsegs; if (ef_read_entry(ef, phdyn->p_offset, phdyn->p_filesz, (void**)&ef->ef_dyn) != 0) { printf("ef_read_entry failed\n"); -- 2.45.2