xref: /netbsd-src/lib/libc/gen/nlist.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: nlist.c,v 1.5 1995/02/27 04:35:29 cgd Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  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 by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)nlist.c	8.1 (Berkeley) 6/4/93";
39 #else
40 static char rcsid[] = "$NetBSD: nlist.c,v 1.5 1995/02/27 04:35:29 cgd Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include <sys/param.h>
45 #include <sys/mman.h>
46 #include <sys/stat.h>
47 #include <sys/file.h>
48 
49 #include <errno.h>
50 #include <a.out.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 int
56 nlist(name, list)
57 	const char *name;
58 	struct nlist *list;
59 {
60 	int fd, n;
61 
62 	fd = open(name, O_RDONLY, 0);
63 	if (fd < 0)
64 		return (-1);
65 	n = __fdnlist(fd, list);
66 	(void)close(fd);
67 	return (n);
68 }
69 
70 #define	ISLAST(p)	(p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
71 
72 int
73 __fdnlist(fd, list)
74 	register int fd;
75 	register struct nlist *list;
76 {
77 	register struct nlist *p, *s;
78 	register caddr_t strtab;
79 	register off_t stroff, symoff;
80 	register u_long symsize;
81 	register int nent, cc;
82 	size_t strsize;
83 	struct nlist nbuf[1024];
84 	struct exec exec;
85 	struct stat st;
86 
87 	if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
88 	    read(fd, &exec, sizeof(exec)) != sizeof(exec) ||
89 	    N_BADMAG(exec) || fstat(fd, &st) < 0)
90 		return (-1);
91 
92 	symoff = N_SYMOFF(exec);
93 	symsize = exec.a_syms;
94 	stroff = symoff + symsize;
95 
96 	/* Check for files too large to mmap. */
97 	if (st.st_size - stroff > SIZE_T_MAX) {
98 		errno = EFBIG;
99 		return (-1);
100 	}
101 	/*
102 	 * Map string table into our address space.  This gives us
103 	 * an easy way to randomly access all the strings, without
104 	 * making the memory allocation permanent as with malloc/free
105 	 * (i.e., munmap will return it to the system).
106 	 */
107 	strsize = st.st_size - stroff;
108 	strtab = mmap(NULL, (size_t)strsize, PROT_READ, 0, fd, stroff);
109 	if (strtab == (char *)-1)
110 		return (-1);
111 	/*
112 	 * clean out any left-over information for all valid entries.
113 	 * Type and value defined to be 0 if not found; historical
114 	 * versions cleared other and desc as well.  Also figure out
115 	 * the largest string length so don't read any more of the
116 	 * string table than we have to.
117 	 *
118 	 * XXX clearing anything other than n_type and n_value violates
119 	 * the semantics given in the man page.
120 	 */
121 	nent = 0;
122 	for (p = list; !ISLAST(p); ++p) {
123 		p->n_type = 0;
124 		p->n_other = 0;
125 		p->n_desc = 0;
126 		p->n_value = 0;
127 		++nent;
128 	}
129 	if (lseek(fd, symoff, SEEK_SET) == -1)
130 		return (-1);
131 
132 	while (symsize > 0) {
133 		cc = MIN(symsize, sizeof(nbuf));
134 		if (read(fd, nbuf, cc) != cc)
135 			break;
136 		symsize -= cc;
137 		for (s = nbuf; cc > 0; ++s, cc -= sizeof(*s)) {
138 			register int soff = s->n_un.n_strx;
139 
140 			if (soff == 0 || (s->n_type & N_STAB) != 0)
141 				continue;
142 			for (p = list; !ISLAST(p); p++)
143 				if (!strcmp(&strtab[soff], p->n_un.n_name)) {
144 					p->n_value = s->n_value;
145 					p->n_type = s->n_type;
146 					p->n_desc = s->n_desc;
147 					p->n_other = s->n_other;
148 					if (--nent <= 0)
149 						break;
150 				}
151 		}
152 	}
153 	munmap(strtab, strsize);
154 	return (nent);
155 }
156