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