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