Home | History | Annotate | Line # | Download | only in kvm_mkdb
nlist_elf32.c revision 1.1
      1 /*	$NetBSD: nlist_elf32.c,v 1.1 1996/09/29 02:19:59 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Christopher G. Demetriou
     17  *	for the NetBSD Project.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #ifndef lint
     34 static char *rcsid = "$NetBSD: nlist_elf32.c,v 1.1 1996/09/29 02:19:59 cgd Exp $";
     35 #endif /* not lint */
     36 
     37 /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
     38 #ifndef ELFSIZE
     39 #define	ELFSIZE		32
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #include <sys/mman.h>
     44 #include <sys/stat.h>
     45 
     46 #include <a.out.h>
     47 #include <db.h>
     48 #include <err.h>
     49 #include <errno.h>
     50 #include <fcntl.h>
     51 #include <kvm.h>
     52 #include <limits.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 
     58 #include "extern.h"
     59 
     60 #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
     61 #include <sys/exec_elf.h>
     62 #endif
     63 
     64 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
     65     (defined(NLIST_ELF64) && (ELFSIZE == 64))
     66 
     67 #define CONCAT(x,y)     __CONCAT(x,y)
     68 #define ELFNAME(x)      CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
     69 #define ELFNAME2(x,y)   CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
     70 #define ELFNAMEEND(x)   CONCAT(x,CONCAT(_elf,ELFSIZE))
     71 #define ELFDEFNNAME(x)  CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
     72 
     73 typedef struct nlist NLIST;
     74 #define	_strx	n_un.n_strx
     75 #define	_name	n_un.n_name
     76 
     77 #define	badfmt(str)							\
     78 	do {								\
     79 		warnx("%s: %s: %s", kfile, str, strerror(EFTYPE));	\
     80 		punt();							\
     81 	} while (0)
     82 
     83 #define	check(off, size)	((off < 0) || (off + size > mappedsize))
     84 #define	BAD			do { rv = -1; goto out; } while (0)
     85 #define	BADUNMAP		do { rv = -1; goto unmap; } while (0)
     86 
     87 static const char *kfile;
     88 
     89 int
     90 ELFNAMEEND(create_knlist)(name, db)
     91 	const char *name;
     92 	DB *db;
     93 {
     94 	struct stat st;
     95 	struct nlist nbuf;
     96 	DBT key, data;
     97 	char *mappedfile, *symname, *fsymname, *tmpcp, *strtab;
     98 	size_t mappedsize, symnamesize, fsymnamesize;
     99 	Elf_Ehdr *ehdrp;
    100 	Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
    101 	Elf_Sym *symp;
    102 	Elf32_Off shdr_off;
    103 	Elf32_Word shdr_size;
    104 #if (ELFSIZE == 32)
    105 	Elf32_Half nshdr;
    106 #elif (ELFSIZE == 64)
    107 	Elf64_Half nshdr;
    108 #endif
    109 	unsigned long i, nsyms;
    110 	int fd, rv;
    111 
    112 	rv = -1;
    113 
    114 	/*
    115 	 * Open and map the whole file.  If we can't open/stat it,
    116 	 * something bad is going on so we punt.
    117 	 */
    118 	kfile = name;
    119 	if ((fd = open(name, O_RDONLY, 0)) < 0) {
    120 		warn("%s", kfile);
    121 		punt();
    122 	}
    123 	if (fstat(fd, &st) < 0) {
    124 		warn("%s", kfile);
    125 		punt();
    126 	}
    127 	if (st.st_size > SIZE_T_MAX)
    128 		BAD;
    129 
    130 	/*
    131 	 * Map the file in its entirety.
    132 	 */
    133 	mappedsize = st.st_size;
    134 	mappedfile = mmap(NULL, mappedsize, PROT_READ, 0, fd, 0);
    135 	if (mappedfile == (char *)-1)
    136 		BAD;
    137 
    138 	/*
    139 	 * Make sure we can access the executable's header
    140 	 * directly, and make sure the recognize the executable
    141 	 * as an ELF binary.
    142 	 */
    143 	if (check(0, sizeof *ehdrp))
    144 		BADUNMAP;
    145 	ehdrp = (Elf_Ehdr *)&mappedfile[0];
    146 
    147 	if (bcmp(ehdrp->e_ident, Elf_e_ident, Elf_e_siz))
    148 		BADUNMAP;
    149 
    150 	switch (ehdrp->e_machine) {
    151 	ELFDEFNNAME(MACHDEP_ID_CASES)
    152 
    153 	default:
    154 		BADUNMAP;
    155 	}
    156 
    157 	/*
    158 	 * We've recognized it as an ELF binary.  From here
    159 	 * on out, all errors are fatal.
    160 	 */
    161 
    162 	/*
    163 	 * Find the symbol list and string table.
    164 	 */
    165 	nshdr = ehdrp->e_shnum;
    166 	shdr_off = ehdrp->e_shoff;
    167 	shdr_size = ehdrp->e_shentsize * nshdr;
    168 
    169 	if (check(shdr_off, shdr_size) ||
    170 	    (sizeof *shdrp != ehdrp->e_shentsize))
    171 		badfmt("bogus section header table");
    172 	shdrp = (Elf_Shdr *)&mappedfile[shdr_off];
    173 
    174 	for (i = 0; i < nshdr; i++) {
    175 		if (shdrp[i].sh_type == Elf_sht_symtab) {
    176 			symshdrp = &shdrp[i];
    177 			symstrshdrp = &shdrp[shdrp[i].sh_link];
    178 		}
    179 	}
    180 
    181 	if (symshdrp->sh_offset == 0)
    182 		badfmt("stripped");
    183 	if (check(symshdrp->sh_offset, symshdrp->sh_size))
    184 		badfmt("bogus symbol section header");
    185 	if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
    186 		badfmt("bogus symbol string section header");
    187 
    188 	symp = (Elf_Sym *)&mappedfile[symshdrp->sh_offset];
    189 	nsyms = symshdrp->sh_size / sizeof(*symp);
    190 	strtab = &mappedfile[symstrshdrp->sh_offset];
    191 
    192 	/*
    193 	 * Set up the data item, pointing to a nlist structure.
    194 	 * which we fill in for each symbol.
    195 	 */
    196 	data.data = (u_char *)&nbuf;
    197 	data.size = sizeof(nbuf);
    198 
    199 	/*
    200 	 * Create a buffer (to be expanded later, if necessary)
    201 	 * to hold symbol names after we've added underscores
    202 	 * to them.
    203 	 */
    204 	symnamesize = 1024;
    205 	if ((symname = malloc(symnamesize)) == NULL) {
    206 		warn("malloc");
    207 		punt();
    208 	}
    209 
    210 	/*
    211 	 * Read each symbol and enter it into the database.
    212 	 */
    213 	for (i = 0; i < nsyms; i++) {
    214 
    215 		/*
    216 		 * No symbol name; ignore this symbol.
    217 		 */
    218 		if (symp[i].st_name == 0)
    219 			continue;
    220 
    221 		/*
    222 		 * Find symbol name, copy it (with added underscore) to
    223 		 * temporary buffer, and prepare the database key for
    224 		 * insertion.
    225 		 */
    226 		fsymname = &strtab[symp[i].st_name];
    227 		fsymnamesize = strlen(fsymname) + 1;
    228 		while (symnamesize < fsymnamesize + 1) {
    229 			symnamesize *= 2;
    230 			if ((symname = realloc(symname, symnamesize)) == NULL) {
    231 				warn("malloc");
    232 				punt();
    233 			}
    234 		}
    235 		strcpy(symname, "_");
    236 		strcat(symname, fsymname);
    237 
    238 		key.data = symname;
    239 		key.size = strlen((char *)key.data);
    240 
    241 		/*
    242 		 * Convert the symbol information into an nlist structure,
    243 		 * as best we can.
    244 		 */
    245 		nbuf.n_value = symp[i].st_value;
    246 		switch (ELF_SYM_TYPE(symp[i].st_info)) {
    247 		default:
    248 		case Elf_estt_notype:
    249 			nbuf.n_type = N_UNDF;
    250 			break;
    251 		case Elf_estt_object:
    252 			nbuf.n_type = N_DATA;
    253 			break;
    254 		case Elf_estt_func:
    255 			nbuf.n_type = N_TEXT;
    256 			break;
    257 		case Elf_estt_file:
    258 			nbuf.n_type = N_FN;
    259 			break;
    260 		}
    261 		if (ELF_SYM_BIND(symp[i].st_info) != Elf_estb_local)
    262 			nbuf.n_type |= N_EXT;
    263 		nbuf.n_desc = 0;				/* XXX */
    264 		nbuf.n_other = 0;				/* XXX */
    265 
    266 		/*
    267 		 * Enter the symbol into the database.
    268 		 */
    269 		if (db->put(db, &key, &data, 0)) {
    270 			warn("record enter");
    271 			punt();
    272 		}
    273 
    274 		/*
    275 		 * If it's the kernel version string, we've gotta keep
    276 		 * some extra data around.  Under a seperate key,
    277 		 * we enter the first line (i.e. up to the first newline,
    278 		 * with the newline replaced by a NUL to terminate the
    279 		 * entered string) of the version string.
    280 		 */
    281 		if (strcmp((char *)key.data, VRS_SYM) == 0) {
    282 			key.data = (u_char *)VRS_KEY;
    283 			key.size = sizeof(VRS_KEY) - 1;
    284 			/* Find the version string, relative to its section */
    285 			data.data = strdup(&mappedfile[nbuf.n_value -
    286 			    shdrp[symp[i].st_shndx].sh_addr +
    287 			    shdrp[symp[i].st_shndx].sh_offset]);
    288 			/* assumes newline terminates version. */
    289 			if ((tmpcp = strchr(data.data, '\n')) != NULL)
    290 				*tmpcp = '\0';
    291 			data.size = strlen((char *)data.data);
    292 
    293 			if (db->put(db, &key, &data, 0)) {
    294 				warn("record enter");
    295 				punt();
    296 			}
    297 
    298 			/* free pointer created by strdup(). */
    299 			free(data.data);
    300 
    301 			/* Restore to original values */
    302 			data.data = (u_char *)&nbuf;
    303 			data.size = sizeof(nbuf);
    304 		}
    305 	}
    306 
    307 	rv = 0;
    308 
    309 unmap:
    310 	munmap(mappedfile, mappedsize);
    311 out:
    312 	return (rv);
    313 }
    314 
    315 #endif
    316