Home | History | Annotate | Line # | Download | only in gen
nlist_elf32.c revision 1.3
      1  1.3  cgd /*	$NetBSD: nlist_elf32.c,v 1.3 1996/10/01 00:32:52 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.3  cgd  *      This product includes software developed by Christopher G. Demetriou
     17  1.3  cgd  *	for the NetBSD Project.
     18  1.3  cgd  * 4. The name of the author may not be used to endorse or promote products
     19  1.3  cgd  *    derived from this software without specific prior written permission
     20  1.1  cgd  *
     21  1.3  cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.3  cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.3  cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.3  cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.3  cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.3  cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.3  cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.3  cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.3  cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.3  cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  cgd  */
     32  1.1  cgd 
     33  1.1  cgd /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
     34  1.1  cgd #ifndef ELFSIZE
     35  1.1  cgd #define	ELFSIZE		32
     36  1.1  cgd #endif
     37  1.1  cgd 
     38  1.1  cgd #include <sys/param.h>
     39  1.1  cgd #include <sys/mman.h>
     40  1.1  cgd #include <sys/stat.h>
     41  1.1  cgd #include <sys/file.h>
     42  1.1  cgd 
     43  1.1  cgd #include <errno.h>
     44  1.1  cgd #include <stdio.h>
     45  1.1  cgd #include <string.h>
     46  1.1  cgd #include <unistd.h>
     47  1.1  cgd #include <a.out.h>			/* for 'struct nlist' declaration */
     48  1.1  cgd 
     49  1.1  cgd #include "nlist_private.h"
     50  1.1  cgd #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
     51  1.1  cgd #include <sys/exec_elf.h>
     52  1.1  cgd #endif
     53  1.1  cgd 
     54  1.1  cgd #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
     55  1.1  cgd     (defined(NLIST_ELF64) && (ELFSIZE == 64))
     56  1.1  cgd 
     57  1.1  cgd #define	CONCAT(x,y)	__CONCAT(x,y)
     58  1.1  cgd #define	ELFNAME(x)	CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
     59  1.1  cgd #define	ELFNAME2(x,y)	CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
     60  1.1  cgd #define	ELFNAMEEND(x)	CONCAT(x,CONCAT(_elf,ELFSIZE))
     61  1.1  cgd #define	ELFDEFNNAME(x)	CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
     62  1.1  cgd 
     63  1.3  cgd #define	check(off, size)	((off < 0) || (off + size > mappedsize))
     64  1.3  cgd #define	BAD			do { rv = -1; goto out; } while (0)
     65  1.3  cgd #define	BADUNMAP		do { rv = -1; goto unmap; } while (0)
     66  1.3  cgd 
     67  1.1  cgd int
     68  1.2  cgd ELFNAMEEND(__fdnlist)(fd, list)
     69  1.2  cgd 	register int fd;
     70  1.2  cgd 	register struct nlist *list;
     71  1.1  cgd {
     72  1.2  cgd 	struct stat st;
     73  1.3  cgd 	struct nlist *p;
     74  1.3  cgd 	char *mappedfile, *strtab;
     75  1.3  cgd 	size_t mappedsize;
     76  1.3  cgd 	Elf_Ehdr *ehdrp;
     77  1.3  cgd 	Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
     78  1.3  cgd 	Elf_Sym *symp;
     79  1.3  cgd 	Elf32_Off shdr_off;
     80  1.3  cgd 	Elf32_Word shdr_size;
     81  1.3  cgd #if (ELFSIZE == 32)
     82  1.3  cgd 	Elf32_Half nshdr;
     83  1.3  cgd #elif (ELFSIZE == 64)
     84  1.3  cgd 	Elf64_Half nshdr;
     85  1.3  cgd #endif
     86  1.3  cgd 	unsigned long i, nsyms;
     87  1.3  cgd 	int rv, nent;
     88  1.3  cgd 
     89  1.3  cgd 	rv = -1;
     90  1.3  cgd 
     91  1.3  cgd 	/*
     92  1.3  cgd 	 * Map the whole file.  If we can't open/stat it, something bad
     93  1.3  cgd 	 * is going on so we punt.
     94  1.3  cgd 	 */
     95  1.3  cgd 	if (fstat(fd, &st) < 0)
     96  1.3  cgd 		BAD;
     97  1.3  cgd 	if (st.st_size > SIZE_T_MAX) {
     98  1.3  cgd 		errno = EFBIG;
     99  1.3  cgd 		BAD;
    100  1.3  cgd 	}
    101  1.2  cgd 
    102  1.3  cgd 	/*
    103  1.3  cgd 	 * Map the file in its entirety.
    104  1.3  cgd 	 */
    105  1.3  cgd 	mappedsize = st.st_size;
    106  1.3  cgd 	mappedfile = mmap(NULL, mappedsize, PROT_READ, 0, fd, 0);
    107  1.3  cgd 	if (mappedfile == (char *)-1)
    108  1.3  cgd 		BAD;
    109  1.3  cgd 
    110  1.3  cgd 	/*
    111  1.3  cgd 	 * Make sure we can access the executable's header
    112  1.3  cgd 	 * directly, and make sure the recognize the executable
    113  1.3  cgd 	 * as an ELF binary.
    114  1.3  cgd 	 */
    115  1.3  cgd 	if (check(0, sizeof *ehdrp))
    116  1.3  cgd 		BADUNMAP;
    117  1.3  cgd 	ehdrp = (Elf_Ehdr *)&mappedfile[0];
    118  1.1  cgd 
    119  1.3  cgd 	if (bcmp(ehdrp->e_ident, Elf_e_ident, Elf_e_siz))
    120  1.3  cgd 		BADUNMAP;
    121  1.1  cgd 
    122  1.3  cgd 	switch (ehdrp->e_machine) {
    123  1.1  cgd 	ELFDEFNNAME(MACHDEP_ID_CASES)
    124  1.1  cgd 
    125  1.1  cgd 	default:
    126  1.3  cgd 		BADUNMAP;
    127  1.1  cgd 	}
    128  1.1  cgd 
    129  1.2  cgd 	/*
    130  1.3  cgd 	 * Find the symbol list and string table.
    131  1.2  cgd 	 */
    132  1.3  cgd 	nshdr = ehdrp->e_shnum;
    133  1.3  cgd 	shdr_off = ehdrp->e_shoff;
    134  1.3  cgd 	shdr_size = ehdrp->e_shentsize * nshdr;
    135  1.3  cgd 
    136  1.3  cgd 	if (check(shdr_off, shdr_size) ||
    137  1.3  cgd 	    (sizeof *shdrp != ehdrp->e_shentsize))
    138  1.3  cgd 		BADUNMAP;
    139  1.3  cgd 	shdrp = (Elf_Shdr *)&mappedfile[shdr_off];
    140  1.3  cgd 
    141  1.3  cgd 	for (i = 0; i < nshdr; i++) {
    142  1.3  cgd 		if (shdrp[i].sh_type == Elf_sht_symtab) {
    143  1.3  cgd 			symshdrp = &shdrp[i];
    144  1.3  cgd 			symstrshdrp = &shdrp[shdrp[i].sh_link];
    145  1.2  cgd 		}
    146  1.2  cgd 	}
    147  1.2  cgd 
    148  1.3  cgd 	/* Make sure we're not stripped. */
    149  1.3  cgd 	if (symshdrp->sh_offset == 0)
    150  1.3  cgd 		BADUNMAP;
    151  1.3  cgd 
    152  1.3  cgd 	/* Make sure the symbols and strings are safely mapped. */
    153  1.3  cgd 	if (check(symshdrp->sh_offset, symshdrp->sh_size))
    154  1.3  cgd 		BADUNMAP;
    155  1.3  cgd 	if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
    156  1.3  cgd 		BADUNMAP;
    157  1.3  cgd 
    158  1.3  cgd 	symp = (Elf_Sym *)&mappedfile[symshdrp->sh_offset];
    159  1.3  cgd 	nsyms = symshdrp->sh_size / sizeof(*symp);
    160  1.3  cgd 	strtab = &mappedfile[symstrshdrp->sh_offset];
    161  1.1  cgd 
    162  1.2  cgd 	/*
    163  1.2  cgd 	 * clean out any left-over information for all valid entries.
    164  1.2  cgd 	 * Type and value defined to be 0 if not found; historical
    165  1.3  cgd 	 * versions cleared other and desc as well.
    166  1.2  cgd 	 *
    167  1.2  cgd 	 * XXX clearing anything other than n_type and n_value violates
    168  1.2  cgd 	 * the semantics given in the man page.
    169  1.2  cgd 	 */
    170  1.2  cgd 	nent = 0;
    171  1.2  cgd 	for (p = list; !ISLAST(p); ++p) {
    172  1.2  cgd 		p->n_type = 0;
    173  1.2  cgd 		p->n_other = 0;
    174  1.2  cgd 		p->n_desc = 0;
    175  1.2  cgd 		p->n_value = 0;
    176  1.2  cgd 		++nent;
    177  1.2  cgd 	}
    178  1.2  cgd 
    179  1.3  cgd 	for (i = 0; i < nsyms; i++) {
    180  1.3  cgd 		for (p = list; !ISLAST(p); ++p) {
    181  1.3  cgd 			char *nlistname;
    182  1.3  cgd 			char *symtabname;
    183  1.3  cgd 
    184  1.3  cgd 			/* This may be incorrect */
    185  1.3  cgd 			nlistname = p->n_un.n_name;
    186  1.3  cgd 			if (*nlistname == '_')
    187  1.3  cgd 				nlistname++;
    188  1.3  cgd 
    189  1.3  cgd 			symtabname = &strtab[symp[i].st_name];
    190  1.3  cgd 
    191  1.3  cgd 			if (!strcmp(symtabname, nlistname)) {
    192  1.3  cgd 				/* Fill in the nlist data. */
    193  1.3  cgd 				p->n_value = symp[i].st_value;
    194  1.3  cgd 				switch(ELF_SYM_TYPE(symp[i].st_info)) {
    195  1.3  cgd 				case Elf_estt_notype:
    196  1.3  cgd 					p->n_type = N_UNDF;
    197  1.3  cgd 					break;
    198  1.3  cgd 				case Elf_estt_object:
    199  1.3  cgd 					p->n_type = N_DATA;
    200  1.3  cgd 					break;
    201  1.3  cgd 				case Elf_estt_func:
    202  1.3  cgd 					p->n_type = N_TEXT;
    203  1.3  cgd 					break;
    204  1.3  cgd 				case Elf_estt_file:
    205  1.3  cgd 					p->n_type = N_FN;
    206  1.3  cgd 					break;
    207  1.2  cgd 				}
    208  1.3  cgd 				if (ELF_SYM_BIND(symp[i].st_info) !=
    209  1.3  cgd 				    Elf_estb_local)
    210  1.3  cgd 					p->n_type |= N_EXT;
    211  1.3  cgd 				p->n_desc = 0;			/* XXX */
    212  1.3  cgd 				p->n_other = 0;			/* XXX */
    213  1.3  cgd 
    214  1.3  cgd 				if (--nent <= 0)
    215  1.3  cgd 					goto done;
    216  1.3  cgd 				break;	/* into next run of outer loop */
    217  1.2  cgd 			}
    218  1.2  cgd 		}
    219  1.2  cgd 	}
    220  1.3  cgd 
    221  1.2  cgd done:
    222  1.3  cgd 	rv = nent;
    223  1.3  cgd unmap:
    224  1.3  cgd 	munmap(mappedfile, mappedsize);
    225  1.3  cgd out:
    226  1.3  cgd 	return (rv);
    227  1.1  cgd }
    228  1.1  cgd 
    229  1.1  cgd #endif
    230