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