Home | History | Annotate | Line # | Download | only in gen
nlist_elf32.c revision 1.34
      1 /* $NetBSD: nlist_elf32.c,v 1.34 2012/03/20 16:36:05 matt 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 #include <sys/cdefs.h>
     38 #if defined(LIBC_SCCS) && !defined(lint)
     39 __RCSID("$NetBSD: nlist_elf32.c,v 1.34 2012/03/20 16:36:05 matt Exp $");
     40 #endif /* LIBC_SCCS and not lint */
     41 
     42 /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
     43 #ifndef ELFSIZE
     44 #define	ELFSIZE		32
     45 #endif
     46 
     47 #include "namespace.h"
     48 #include <sys/param.h>
     49 #include <sys/mman.h>
     50 #include <sys/stat.h>
     51 #include <sys/file.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/ksyms.h>
     54 
     55 #include <assert.h>
     56 #include <errno.h>
     57 #include <stdio.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 #include <nlist.h>
     61 
     62 #include "nlist_private.h"
     63 #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
     64 #include <sys/exec_elf.h>
     65 #endif
     66 
     67 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
     68     (defined(NLIST_ELF64) && (ELFSIZE == 64))
     69 
     70 /* No need to check for off < 0 because it is unsigned */
     71 #define	check(off, size)	(off + size > mappedsize)
     72 #define	BAD			goto out
     73 #define	BADUNMAP		goto unmap
     74 
     75 int
     76 ELFNAMEEND(__fdnlist)(int fd, struct nlist *list)
     77 {
     78 	struct stat st;
     79 	struct nlist *p;
     80 	char *mappedfile, *strtab;
     81 	size_t mappedsize;
     82 	Elf_Ehdr *ehdrp, ehdr;
     83 	Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
     84 	Elf_Sym *symp;
     85 	Elf_Off shdr_off;
     86 	Elf_Word shdr_size;
     87 #if (ELFSIZE == 32)
     88 	Elf32_Half nshdr;
     89 #elif (ELFSIZE == 64)
     90 	Elf64_Word nshdr;
     91 #endif
     92 	size_t i, nsyms;
     93 	int rv, nent;
     94 
     95 	_DIAGASSERT(fd != -1);
     96 	_DIAGASSERT(list != NULL);
     97 
     98 	rv = -1;
     99 
    100 	symshdrp = symstrshdrp = NULL;
    101 
    102 	/*
    103 	 * If we can't fstat() the file, something bad is going on.
    104 	 */
    105 	if (fstat(fd, &st) < 0)
    106 		BAD;
    107 
    108 	/*
    109 	 * Map the file in its entirety.
    110 	 */
    111 	if ((uintmax_t)st.st_size > (uintmax_t)SIZE_T_MAX) {
    112 		errno = EFBIG;
    113 		BAD;
    114 	}
    115 
    116 	/*
    117 	 * Read the elf header of the file.
    118 	 */
    119 	if ((ssize_t)(i = pread(fd, &ehdr, sizeof(Elf_Ehdr), (off_t)0)) == -1)
    120 		BAD;
    121 
    122 	/*
    123 	 * Check that the elf header is correct.
    124 	 */
    125 	if (i != sizeof(Elf_Ehdr))
    126 		BAD;
    127 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
    128 	    ehdr.e_ident[EI_CLASS] != ELFCLASS)
    129 		BAD;
    130 
    131 	switch (ehdr.e_machine) {
    132 	ELFDEFNNAME(MACHDEP_ID_CASES)
    133 
    134 	default:
    135 		BAD;
    136 	}
    137 
    138 	if (S_ISCHR(st.st_mode)) {
    139 		const char *nlistname;
    140 		struct ksyms_gsymbol kg;
    141 		Elf_Sym sym;
    142 
    143 		/*
    144 		 * Character device; assume /dev/ksyms.
    145 		 */
    146 		nent = 0;
    147 		for (p = list; !ISLAST(p); ++p) {
    148 
    149 			p->n_other = 0;
    150 			p->n_desc = 0;
    151 			nlistname = N_NAME(p);
    152 			if (*nlistname == '_')
    153 				nlistname++;
    154 
    155 			kg.kg_name = nlistname;
    156 			kg.kg_sym = &sym;
    157 			if (ioctl(fd, KIOCGSYMBOL, &kg) == 0) {
    158 				p->n_value = sym.st_value;
    159 				switch (ELF_ST_TYPE(sym.st_info)) {
    160 				case STT_NOTYPE:
    161 					p->n_type = N_UNDF;
    162 					break;
    163 				case STT_COMMON:
    164 				case STT_OBJECT:
    165 					p->n_type = N_DATA;
    166 					break;
    167 				case STT_FUNC:
    168 					p->n_type = N_TEXT;
    169 					break;
    170 				case STT_FILE:
    171 					p->n_type = N_FN;
    172 					break;
    173 				default:
    174 					p->n_type = 0;
    175 					/* catch other enumerations for gcc */
    176 					break;
    177 				}
    178 				if (ELF_ST_BIND(sym.st_info) != STB_LOCAL)
    179 					p->n_type |= N_EXT;
    180 			} else {
    181 				nent++;
    182 				p->n_value = 0;
    183 				p->n_type = 0;
    184 			}
    185 		}
    186 		return nent;
    187 	}
    188 
    189 	mappedsize = (size_t)st.st_size;
    190 	mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
    191 	    fd, (off_t)0);
    192 	if (mappedfile == (char *)-1)
    193 		BAD;
    194 
    195 	/*
    196 	 * Make sure we can access the executable's header
    197 	 * directly, and make sure the recognize the executable
    198 	 * as an ELF binary.
    199 	 */
    200 	if (check(0, sizeof *ehdrp))
    201 		BADUNMAP;
    202 	ehdrp = (Elf_Ehdr *)(void *)&mappedfile[0];
    203 
    204 	/*
    205 	 * Find the symbol list and string table.
    206 	 */
    207 	nshdr = ehdrp->e_shnum;
    208 	shdr_off = ehdrp->e_shoff;
    209 	shdr_size = ehdrp->e_shentsize * nshdr;
    210 
    211 	if (check(shdr_off, shdr_size) ||
    212 	    (sizeof *shdrp != ehdrp->e_shentsize))
    213 		BADUNMAP;
    214 	shdrp = (Elf_Shdr *)(void *)&mappedfile[shdr_off];
    215 
    216 	for (i = 0; i < nshdr; i++) {
    217 		if (shdrp[i].sh_type == SHT_SYMTAB) {
    218 			symshdrp = &shdrp[i];
    219 			symstrshdrp = &shdrp[shdrp[i].sh_link];
    220 		}
    221 	}
    222 
    223 	/* Make sure we're not stripped. */
    224 	if (symshdrp == NULL || symshdrp->sh_offset == 0)
    225 		BADUNMAP;
    226 
    227 	/* Make sure the symbols and strings are safely mapped. */
    228 	if (check(symshdrp->sh_offset, symshdrp->sh_size))
    229 		BADUNMAP;
    230 	if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
    231 		BADUNMAP;
    232 
    233 	symp = (Elf_Sym *)(void *)&mappedfile[symshdrp->sh_offset];
    234 	nsyms = symshdrp->sh_size / sizeof(*symp);
    235 	strtab = &mappedfile[symstrshdrp->sh_offset];
    236 
    237 	/*
    238 	 * Clean out any left-over information for all valid entries.
    239 	 * Type and value are defined to be 0 if not found; historical
    240 	 * versions cleared other and desc as well.
    241 	 *
    242 	 * XXX Clearing anything other than n_type and n_value violates
    243 	 * the semantics given in the man page.
    244 	 */
    245 	nent = 0;
    246 	for (p = list; !ISLAST(p); ++p) {
    247 		p->n_type = 0;
    248 		p->n_other = 0;
    249 		p->n_desc = 0;
    250 		p->n_value = 0;
    251 		++nent;
    252 	}
    253 
    254 	for (i = 0; i < nsyms; i++) {
    255 		for (p = list; !ISLAST(p); ++p) {
    256 			const char *nlistname;
    257 			char *symtabname;
    258 
    259 			/* This may be incorrect */
    260 			nlistname = N_NAME(p);
    261 			if (*nlistname == '_')
    262 				nlistname++;
    263 
    264 			symtabname = &strtab[symp[i].st_name];
    265 
    266 			if (!strcmp(symtabname, nlistname)) {
    267 				/*
    268 				 * Translate (roughly) from ELF to nlist
    269 				 */
    270 				p->n_value = symp[i].st_value;
    271 				switch (ELF_ST_TYPE(symp[i].st_info)) {
    272 				case STT_NOTYPE:
    273 					p->n_type = N_UNDF;
    274 					break;
    275 				case STT_OBJECT:
    276 				case STT_COMMON:
    277 					p->n_type = N_DATA;
    278 					break;
    279 				case STT_FUNC:
    280 					p->n_type = N_TEXT;
    281 					break;
    282 				case STT_FILE:
    283 					p->n_type = N_FN;
    284 					break;
    285 				default:
    286 					/* catch other enumerations for gcc */
    287 					break;
    288 				}
    289 				if (ELF_ST_BIND(symp[i].st_info) != STB_LOCAL)
    290 					p->n_type |= N_EXT;
    291 				p->n_desc = 0;			/* XXX */
    292 				p->n_other = 0;			/* XXX */
    293 
    294 				if (--nent <= 0)
    295 					goto done;
    296 				break;	/* into next run of outer loop */
    297 			}
    298 		}
    299 	}
    300 
    301 done:
    302 	rv = nent;
    303 unmap:
    304 	munmap(mappedfile, mappedsize);
    305 out:
    306 	return (rv);
    307 }
    308 
    309 #endif
    310