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