Home | History | Annotate | Line # | Download | only in gen
nlist_elf32.c revision 1.21
      1 /* $NetBSD: nlist_elf32.c,v 1.21 2000/06/14 17:25:03 cgd Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *          This product includes software developed for the
     18  *          NetBSD Project.  See http://www.netbsd.org/ for
     19  *          information about NetBSD.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
     35  */
     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 "namespace.h"
     43 #include <sys/param.h>
     44 #include <sys/mman.h>
     45 #include <sys/stat.h>
     46 #include <sys/file.h>
     47 
     48 #include <assert.h>
     49 #include <errno.h>
     50 #include <stdio.h>
     51 #include <string.h>
     52 #include <unistd.h>
     53 #include <a.out.h>			/* for 'struct nlist' declaration */
     54 
     55 #include "nlist_private.h"
     56 #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
     57 #include <sys/exec_elf.h>
     58 #endif
     59 
     60 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
     61     (defined(NLIST_ELF64) && (ELFSIZE == 64))
     62 
     63 /* No need to check for off < 0 because it is unsigned */
     64 #define	check(off, size)	(off + size > mappedsize)
     65 #define	BAD			goto out
     66 #define	BADUNMAP		goto unmap
     67 
     68 int
     69 ELFNAMEEND(__fdnlist)(fd, list)
     70 	int fd;
     71 	struct nlist *list;
     72 {
     73 	struct stat st;
     74 	struct nlist *p;
     75 	char *mappedfile, *strtab;
     76 	size_t mappedsize;
     77 	Elf_Ehdr *ehdrp;
     78 	Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
     79 	Elf_Sym *symp;
     80 	Elf_Off shdr_off;
     81 	Elf_Word shdr_size;
     82 #if (ELFSIZE == 32)
     83 	Elf32_Half nshdr;
     84 #elif (ELFSIZE == 64)
     85 	Elf64_Half nshdr;
     86 #endif
     87 	size_t i, nsyms;
     88 	int rv, nent;
     89 
     90 	_DIAGASSERT(fd != -1);
     91 	_DIAGASSERT(list != NULL);
     92 
     93 	rv = -1;
     94 
     95 	symshdrp = symstrshdrp = NULL;
     96 
     97 	/*
     98 	 * If we can't fstat() the file, something bad is going on.
     99 	 */
    100 	if (fstat(fd, &st) < 0)
    101 		BAD;
    102 
    103 	/*
    104 	 * Map the file in its entirety.
    105 	 */
    106 	if (st.st_size > SIZE_T_MAX) {
    107 		errno = EFBIG;
    108 		BAD;
    109 	}
    110 	mappedsize = (size_t)st.st_size;
    111 	mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
    112 	    fd, (off_t)0);
    113 	if (mappedfile == (char *)-1)
    114 		BAD;
    115 
    116 	/*
    117 	 * Make sure we can access the executable's header
    118 	 * directly, and make sure the recognize the executable
    119 	 * as an ELF binary.
    120 	 */
    121 	if (check(0, sizeof *ehdrp))
    122 		BADUNMAP;
    123 	ehdrp = (Elf_Ehdr *)(void *)&mappedfile[0];
    124 
    125 	if (memcmp(ehdrp->e_ident, ELFMAG, SELFMAG) != 0 ||
    126 	    ehdrp->e_ident[EI_CLASS] != ELFCLASS)
    127 		BADUNMAP;
    128 
    129 	switch (ehdrp->e_machine) {
    130 	ELFDEFNNAME(MACHDEP_ID_CASES)
    131 
    132 	default:
    133 		BADUNMAP;
    134 	}
    135 
    136 	/*
    137 	 * Find the symbol list and string table.
    138 	 */
    139 	nshdr = ehdrp->e_shnum;
    140 	shdr_off = ehdrp->e_shoff;
    141 	shdr_size = ehdrp->e_shentsize * nshdr;
    142 
    143 	if (check(shdr_off, shdr_size) ||
    144 	    (sizeof *shdrp != ehdrp->e_shentsize))
    145 		BADUNMAP;
    146 	shdrp = (Elf_Shdr *)(void *)&mappedfile[shdr_off];
    147 
    148 	for (i = 0; i < nshdr; i++) {
    149 		if (shdrp[i].sh_type == SHT_SYMTAB) {
    150 			symshdrp = &shdrp[i];
    151 			symstrshdrp = &shdrp[shdrp[i].sh_link];
    152 		}
    153 	}
    154 
    155 	/* Make sure we're not stripped. */
    156 	if (symshdrp == NULL || symshdrp->sh_offset == 0)
    157 		BADUNMAP;
    158 
    159 	/* Make sure the symbols and strings are safely mapped. */
    160 	if (check(symshdrp->sh_offset, symshdrp->sh_size))
    161 		BADUNMAP;
    162 	if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
    163 		BADUNMAP;
    164 
    165 	symp = (Elf_Sym *)(void *)&mappedfile[symshdrp->sh_offset];
    166 	nsyms = symshdrp->sh_size / sizeof(*symp);
    167 	strtab = &mappedfile[symstrshdrp->sh_offset];
    168 
    169 	/*
    170 	 * Clean out any left-over information for all valid entries.
    171 	 * Type and value are defined to be 0 if not found; historical
    172 	 * versions cleared other and desc as well.
    173 	 *
    174 	 * XXX Clearing anything other than n_type and n_value violates
    175 	 * the semantics given in the man page.
    176 	 */
    177 	nent = 0;
    178 	for (p = list; !ISLAST(p); ++p) {
    179 		p->n_type = 0;
    180 		p->n_other = 0;
    181 		p->n_desc = 0;
    182 		p->n_value = 0;
    183 		++nent;
    184 	}
    185 
    186 	for (i = 0; i < nsyms; i++) {
    187 		for (p = list; !ISLAST(p); ++p) {
    188 			const char *nlistname;
    189 			char *symtabname;
    190 
    191 			/* This may be incorrect */
    192 			nlistname = p->n_un.n_name;
    193 			if (*nlistname == '_')
    194 				nlistname++;
    195 
    196 			symtabname = &strtab[symp[i].st_name];
    197 
    198 			if (!strcmp(symtabname, nlistname)) {
    199 				/*
    200 				 * Translate (roughly) from ELF to nlist
    201 				 */
    202 				p->n_value = symp[i].st_value;
    203 				switch (ELFDEFNNAME(ST_TYPE)(symp[i].st_info)) {
    204 				case STT_NOTYPE:
    205 					p->n_type = N_UNDF;
    206 					break;
    207 				case STT_OBJECT:
    208 					p->n_type = N_DATA;
    209 					break;
    210 				case STT_FUNC:
    211 					p->n_type = N_TEXT;
    212 					break;
    213 				case STT_FILE:
    214 					p->n_type = N_FN;
    215 					break;
    216 				default:
    217 					/* catch other enumerations for gcc */
    218 					break;
    219 				}
    220 				if (ELFDEFNNAME(ST_BIND)(symp[i].st_info) !=
    221 				    STB_LOCAL)
    222 					p->n_type |= N_EXT;
    223 				p->n_desc = 0;			/* XXX */
    224 				p->n_other = 0;			/* XXX */
    225 
    226 				if (--nent <= 0)
    227 					goto done;
    228 				break;	/* into next run of outer loop */
    229 			}
    230 		}
    231 	}
    232 
    233 done:
    234 	rv = nent;
    235 unmap:
    236 	munmap(mappedfile, mappedsize);
    237 out:
    238 	return (rv);
    239 }
    240 
    241 #endif
    242