xref: /minix3/lib/libc/gen/nlist_aout.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /* $NetBSD: nlist_aout.c,v 1.23 2012/03/21 15:32:26 christos Exp $ */
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1989, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras /*
332fe8fb19SBen Gras  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
342fe8fb19SBen Gras  *
352fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
362fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
372fe8fb19SBen Gras  * are met:
382fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
392fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
402fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
412fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
422fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
432fe8fb19SBen Gras  * 3. All advertising materials mentioning features or use of this software
442fe8fb19SBen Gras  *    must display the following acknowledgement:
452fe8fb19SBen Gras  *	This product includes software developed by the University of
462fe8fb19SBen Gras  *	California, Berkeley and its contributors.
472fe8fb19SBen Gras  * 4. Neither the name of the University nor the names of its contributors
482fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
492fe8fb19SBen Gras  *    without specific prior written permission.
502fe8fb19SBen Gras  *
512fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
522fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
532fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
542fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
552fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
562fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
572fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
582fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
592fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
602fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
612fe8fb19SBen Gras  * SUCH DAMAGE.
622fe8fb19SBen Gras  */
632fe8fb19SBen Gras 
642fe8fb19SBen Gras #include <sys/cdefs.h>
652fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
662fe8fb19SBen Gras #if 0
672fe8fb19SBen Gras static char sccsid[] = "@(#)nlist.c	8.1 (Berkeley) 6/4/93";
682fe8fb19SBen Gras #else
69*f14fb602SLionel Sambuc __RCSID("$NetBSD: nlist_aout.c,v 1.23 2012/03/21 15:32:26 christos Exp $");
702fe8fb19SBen Gras #endif
712fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
722fe8fb19SBen Gras 
732fe8fb19SBen Gras #include "namespace.h"
742fe8fb19SBen Gras #include <sys/param.h>
752fe8fb19SBen Gras #include <sys/mman.h>
762fe8fb19SBen Gras #include <sys/stat.h>
772fe8fb19SBen Gras #include <sys/file.h>
782fe8fb19SBen Gras 
792fe8fb19SBen Gras #include <assert.h>
802fe8fb19SBen Gras #include <errno.h>
812fe8fb19SBen Gras #include <stdio.h>
822fe8fb19SBen Gras #include <string.h>
832fe8fb19SBen Gras #include <unistd.h>
842fe8fb19SBen Gras #include <stdlib.h>
852fe8fb19SBen Gras 
862fe8fb19SBen Gras struct nlist;
872fe8fb19SBen Gras #include "nlist_private.h"
882fe8fb19SBen Gras 
892fe8fb19SBen Gras #ifdef NLIST_AOUT
902fe8fb19SBen Gras #include <a.out.h>
912fe8fb19SBen Gras #include <sys/exec_aout.h>
922fe8fb19SBen Gras 
932fe8fb19SBen Gras int
__fdnlist_aout(int fd,struct nlist * list)94*f14fb602SLionel Sambuc __fdnlist_aout(int fd, struct nlist *list)
952fe8fb19SBen Gras {
962fe8fb19SBen Gras 	struct nlist *p, *s;
972fe8fb19SBen Gras 	char *strtab;
982fe8fb19SBen Gras 	off_t stroff, symoff;
992fe8fb19SBen Gras 	int nent;
1002fe8fb19SBen Gras 	size_t strsize, symsize, cc;
1012fe8fb19SBen Gras 	struct nlist nbuf[1024];
1022fe8fb19SBen Gras 	struct exec exec;
1032fe8fb19SBen Gras 	struct stat st;
1042fe8fb19SBen Gras 	char *scoreboard, *scored;
1052fe8fb19SBen Gras 
1062fe8fb19SBen Gras 	_DIAGASSERT(fd != -1);
1072fe8fb19SBen Gras 	_DIAGASSERT(list != NULL);
1082fe8fb19SBen Gras 
1092fe8fb19SBen Gras 	if (pread(fd, &exec, sizeof(exec), (off_t)0) != sizeof(exec) ||
1102fe8fb19SBen Gras 	    N_BADMAG(exec) || fstat(fd, &st) < 0)
1112fe8fb19SBen Gras 		return (-1);
1122fe8fb19SBen Gras 
1132fe8fb19SBen Gras 	symoff = N_SYMOFF(exec);
1142fe8fb19SBen Gras 	symsize = (size_t)exec.a_syms;
1152fe8fb19SBen Gras 	stroff = symoff + symsize;
1162fe8fb19SBen Gras 
1172fe8fb19SBen Gras 	/* Check for files too large to mmap. */
1182fe8fb19SBen Gras 	if ((uintmax_t)(st.st_size - stroff) > (uintmax_t)SIZE_T_MAX) {
1192fe8fb19SBen Gras 		errno = EFBIG;
1202fe8fb19SBen Gras 		return (-1);
1212fe8fb19SBen Gras 	}
1222fe8fb19SBen Gras 	/*
1232fe8fb19SBen Gras 	 * Map string table into our address space.  This gives us
1242fe8fb19SBen Gras 	 * an easy way to randomly access all the strings, without
1252fe8fb19SBen Gras 	 * making the memory allocation permanent as with malloc/free
1262fe8fb19SBen Gras 	 * (i.e., munmap will return it to the system).
1272fe8fb19SBen Gras 	 */
1282fe8fb19SBen Gras 	strsize = (size_t)(st.st_size - stroff);
1292fe8fb19SBen Gras 	strtab = mmap(NULL, strsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
1302fe8fb19SBen Gras 	    fd, stroff);
1312fe8fb19SBen Gras 	if (strtab == (char *)-1)
1322fe8fb19SBen Gras 		return (-1);
1332fe8fb19SBen Gras 	/*
1342fe8fb19SBen Gras 	 * clean out any left-over information for all valid entries.
1352fe8fb19SBen Gras 	 * Type and value defined to be 0 if not found; historical
1362fe8fb19SBen Gras 	 * versions cleared other and desc as well.  Also figure out
1372fe8fb19SBen Gras 	 * the largest string length so don't read any more of the
1382fe8fb19SBen Gras 	 * string table than we have to.
1392fe8fb19SBen Gras 	 *
1402fe8fb19SBen Gras 	 * XXX clearing anything other than n_type and n_value violates
1412fe8fb19SBen Gras 	 * the semantics given in the man page.
1422fe8fb19SBen Gras 	 */
1432fe8fb19SBen Gras 	nent = 0;
1442fe8fb19SBen Gras 	for (p = list; !ISLAST(p); ++p) {
1452fe8fb19SBen Gras 		p->n_type = 0;
1462fe8fb19SBen Gras 		p->n_other = 0;
1472fe8fb19SBen Gras 		p->n_desc = 0;
1482fe8fb19SBen Gras 		p->n_value = 0;
1492fe8fb19SBen Gras 		++nent;
1502fe8fb19SBen Gras 	}
1512fe8fb19SBen Gras 	if (lseek(fd, symoff, SEEK_SET) == -1)
1522fe8fb19SBen Gras 		return (-1);
1532fe8fb19SBen Gras #if defined(__SSP__) || defined(__SSP_ALL__)
1542fe8fb19SBen Gras 	scoreboard = malloc((size_t)nent);
1552fe8fb19SBen Gras #else
1562fe8fb19SBen Gras 	scoreboard = alloca((size_t)nent);
1572fe8fb19SBen Gras #endif
1582fe8fb19SBen Gras 	if (scoreboard == NULL)
1592fe8fb19SBen Gras 		return (-1);
1602fe8fb19SBen Gras 	(void)memset(scoreboard, 0, (size_t)nent);
1612fe8fb19SBen Gras 
1622fe8fb19SBen Gras 	while (symsize > 0) {
1632fe8fb19SBen Gras 		cc = MIN(symsize, sizeof(nbuf));
1642fe8fb19SBen Gras 		if (read(fd, nbuf, cc) != (ssize_t) cc)
1652fe8fb19SBen Gras 			break;
1662fe8fb19SBen Gras 		symsize -= cc;
1672fe8fb19SBen Gras 		for (s = nbuf; cc > 0; ++s, cc -= sizeof(*s)) {
1682fe8fb19SBen Gras 			long soff = s->n_un.n_strx;
1692fe8fb19SBen Gras 
1702fe8fb19SBen Gras 			if (soff == 0 || (s->n_type & N_STAB) != 0)
1712fe8fb19SBen Gras 				continue;
1722fe8fb19SBen Gras 			for (p = list, scored = scoreboard; !ISLAST(p);
1732fe8fb19SBen Gras 			    p++, scored++)
1742fe8fb19SBen Gras 				if (*scored == 0 &&
1752fe8fb19SBen Gras 				    !strcmp(&strtab[(size_t)soff],
1762fe8fb19SBen Gras 				    p->n_un.n_name)) {
1772fe8fb19SBen Gras 					p->n_value = s->n_value;
1782fe8fb19SBen Gras 					p->n_type = s->n_type;
1792fe8fb19SBen Gras 					p->n_desc = s->n_desc;
1802fe8fb19SBen Gras 					p->n_other = s->n_other;
1812fe8fb19SBen Gras 					*scored = 1;
1822fe8fb19SBen Gras 					if (--nent <= 0)
1832fe8fb19SBen Gras 						break;
1842fe8fb19SBen Gras 				}
1852fe8fb19SBen Gras 		}
1862fe8fb19SBen Gras 	}
1872fe8fb19SBen Gras 	munmap(strtab, strsize);
1882fe8fb19SBen Gras #if defined(__SSP__) || defined(__SSP_ALL__)
1892fe8fb19SBen Gras 	free(scoreboard);
1902fe8fb19SBen Gras #endif
1912fe8fb19SBen Gras 	return (nent);
1922fe8fb19SBen Gras }
1932fe8fb19SBen Gras #endif /* NLIST_AOUT */
194