1*2fab93ccSchristos /* $NetBSD: nlist_aout.c,v 1.23 2012/03/21 15:32:26 christos Exp $ */
2eb7c1594Sagc
3eb7c1594Sagc /*
4eb7c1594Sagc * Copyright (c) 1989, 1993
5eb7c1594Sagc * The Regents of the University of California. All rights reserved.
6eb7c1594Sagc *
7eb7c1594Sagc * Redistribution and use in source and binary forms, with or without
8eb7c1594Sagc * modification, are permitted provided that the following conditions
9eb7c1594Sagc * are met:
10eb7c1594Sagc * 1. Redistributions of source code must retain the above copyright
11eb7c1594Sagc * notice, this list of conditions and the following disclaimer.
12eb7c1594Sagc * 2. Redistributions in binary form must reproduce the above copyright
13eb7c1594Sagc * notice, this list of conditions and the following disclaimer in the
14eb7c1594Sagc * documentation and/or other materials provided with the distribution.
15eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
16eb7c1594Sagc * may be used to endorse or promote products derived from this software
17eb7c1594Sagc * without specific prior written permission.
18eb7c1594Sagc *
19eb7c1594Sagc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20eb7c1594Sagc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21eb7c1594Sagc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22eb7c1594Sagc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23eb7c1594Sagc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24eb7c1594Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25eb7c1594Sagc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26eb7c1594Sagc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27eb7c1594Sagc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28eb7c1594Sagc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29eb7c1594Sagc * SUCH DAMAGE.
30eb7c1594Sagc */
31c8365a76Scgd
32c8365a76Scgd /*
33c8365a76Scgd * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
34c8365a76Scgd *
35c8365a76Scgd * Redistribution and use in source and binary forms, with or without
36c8365a76Scgd * modification, are permitted provided that the following conditions
37c8365a76Scgd * are met:
38c8365a76Scgd * 1. Redistributions of source code must retain the above copyright
39c8365a76Scgd * notice, this list of conditions and the following disclaimer.
40c8365a76Scgd * 2. Redistributions in binary form must reproduce the above copyright
41c8365a76Scgd * notice, this list of conditions and the following disclaimer in the
42c8365a76Scgd * documentation and/or other materials provided with the distribution.
43c8365a76Scgd * 3. All advertising materials mentioning features or use of this software
44c8365a76Scgd * must display the following acknowledgement:
45c8365a76Scgd * This product includes software developed by the University of
46c8365a76Scgd * California, Berkeley and its contributors.
47c8365a76Scgd * 4. Neither the name of the University nor the names of its contributors
48c8365a76Scgd * may be used to endorse or promote products derived from this software
49c8365a76Scgd * without specific prior written permission.
50c8365a76Scgd *
51c8365a76Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52c8365a76Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53c8365a76Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54c8365a76Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55c8365a76Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56c8365a76Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57c8365a76Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58c8365a76Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59c8365a76Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60c8365a76Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61c8365a76Scgd * SUCH DAMAGE.
62c8365a76Scgd */
63c8365a76Scgd
6426cc2d4fSchristos #include <sys/cdefs.h>
65c8365a76Scgd #if defined(LIBC_SCCS) && !defined(lint)
66c8365a76Scgd #if 0
67c8365a76Scgd static char sccsid[] = "@(#)nlist.c 8.1 (Berkeley) 6/4/93";
68c8365a76Scgd #else
69*2fab93ccSchristos __RCSID("$NetBSD: nlist_aout.c,v 1.23 2012/03/21 15:32:26 christos Exp $");
70c8365a76Scgd #endif
71c8365a76Scgd #endif /* LIBC_SCCS and not lint */
72c8365a76Scgd
73abc6d45eSkleink #include "namespace.h"
74c8365a76Scgd #include <sys/param.h>
75c8365a76Scgd #include <sys/mman.h>
76c8365a76Scgd #include <sys/stat.h>
77c8365a76Scgd #include <sys/file.h>
78c8365a76Scgd
79b48252f3Slukem #include <assert.h>
80c8365a76Scgd #include <errno.h>
81c8365a76Scgd #include <stdio.h>
82c8365a76Scgd #include <string.h>
83c8365a76Scgd #include <unistd.h>
84e8c0c9dfSchristos #include <stdlib.h>
85c8365a76Scgd
86bc27f3e0Smartin struct nlist;
87c8365a76Scgd #include "nlist_private.h"
88c8365a76Scgd
89c8365a76Scgd #ifdef NLIST_AOUT
904cf9cdf3She #include <a.out.h>
913308ff5cSmatt #include <sys/exec_aout.h>
923308ff5cSmatt
93c8365a76Scgd int
__fdnlist_aout(int fd,struct nlist * list)94*2fab93ccSchristos __fdnlist_aout(int fd, struct nlist *list)
95c8365a76Scgd {
964146d586Sperry struct nlist *p, *s;
9740941d96Schristos char *strtab;
984146d586Sperry off_t stroff, symoff;
9940941d96Schristos int nent;
10040941d96Schristos size_t strsize, symsize, cc;
101c8365a76Scgd struct nlist nbuf[1024];
102c8365a76Scgd struct exec exec;
103c8365a76Scgd struct stat st;
104e8c0c9dfSchristos char *scoreboard, *scored;
105c8365a76Scgd
106b48252f3Slukem _DIAGASSERT(fd != -1);
107b48252f3Slukem _DIAGASSERT(list != NULL);
108b48252f3Slukem
109b9cfbb01Sthorpej if (pread(fd, &exec, sizeof(exec), (off_t)0) != sizeof(exec) ||
11080fde3aaScgd N_BADMAG(exec) || fstat(fd, &st) < 0)
111c8365a76Scgd return (-1);
112c8365a76Scgd
113c8365a76Scgd symoff = N_SYMOFF(exec);
11440941d96Schristos symsize = (size_t)exec.a_syms;
115c8365a76Scgd stroff = symoff + symsize;
116c8365a76Scgd
117c8365a76Scgd /* Check for files too large to mmap. */
118be48221fSlukem if ((uintmax_t)(st.st_size - stroff) > (uintmax_t)SIZE_T_MAX) {
119c8365a76Scgd errno = EFBIG;
120c8365a76Scgd return (-1);
121c8365a76Scgd }
122c8365a76Scgd /*
123c8365a76Scgd * Map string table into our address space. This gives us
124c8365a76Scgd * an easy way to randomly access all the strings, without
125c8365a76Scgd * making the memory allocation permanent as with malloc/free
126c8365a76Scgd * (i.e., munmap will return it to the system).
127c8365a76Scgd */
12840941d96Schristos strsize = (size_t)(st.st_size - stroff);
1293787c5c3Sthorpej strtab = mmap(NULL, strsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
130c1ae5284Smrg fd, stroff);
131c8365a76Scgd if (strtab == (char *)-1)
132c8365a76Scgd return (-1);
133c8365a76Scgd /*
134c8365a76Scgd * clean out any left-over information for all valid entries.
135c8365a76Scgd * Type and value defined to be 0 if not found; historical
136c8365a76Scgd * versions cleared other and desc as well. Also figure out
137c8365a76Scgd * the largest string length so don't read any more of the
138c8365a76Scgd * string table than we have to.
139c8365a76Scgd *
140c8365a76Scgd * XXX clearing anything other than n_type and n_value violates
141c8365a76Scgd * the semantics given in the man page.
142c8365a76Scgd */
143c8365a76Scgd nent = 0;
144c8365a76Scgd for (p = list; !ISLAST(p); ++p) {
145c8365a76Scgd p->n_type = 0;
146c8365a76Scgd p->n_other = 0;
147c8365a76Scgd p->n_desc = 0;
148c8365a76Scgd p->n_value = 0;
149c8365a76Scgd ++nent;
150c8365a76Scgd }
151c8365a76Scgd if (lseek(fd, symoff, SEEK_SET) == -1)
152c8365a76Scgd return (-1);
153249933b8Schristos #if defined(__SSP__) || defined(__SSP_ALL__)
154249933b8Schristos scoreboard = malloc((size_t)nent);
155249933b8Schristos #else
156249933b8Schristos scoreboard = alloca((size_t)nent);
157249933b8Schristos #endif
158249933b8Schristos if (scoreboard == NULL)
159e8c0c9dfSchristos return (-1);
160e8c0c9dfSchristos (void)memset(scoreboard, 0, (size_t)nent);
161c8365a76Scgd
162c8365a76Scgd while (symsize > 0) {
163c8365a76Scgd cc = MIN(symsize, sizeof(nbuf));
1644747ad6eSthorpej if (read(fd, nbuf, cc) != (ssize_t) cc)
165c8365a76Scgd break;
166c8365a76Scgd symsize -= cc;
167c8365a76Scgd for (s = nbuf; cc > 0; ++s, cc -= sizeof(*s)) {
16840941d96Schristos long soff = s->n_un.n_strx;
169c8365a76Scgd
170c8365a76Scgd if (soff == 0 || (s->n_type & N_STAB) != 0)
171c8365a76Scgd continue;
172e8c0c9dfSchristos for (p = list, scored = scoreboard; !ISLAST(p);
173e8c0c9dfSchristos p++, scored++)
174e8c0c9dfSchristos if (*scored == 0 &&
175e8c0c9dfSchristos !strcmp(&strtab[(size_t)soff],
17640941d96Schristos p->n_un.n_name)) {
177c8365a76Scgd p->n_value = s->n_value;
178c8365a76Scgd p->n_type = s->n_type;
179c8365a76Scgd p->n_desc = s->n_desc;
180c8365a76Scgd p->n_other = s->n_other;
181e8c0c9dfSchristos *scored = 1;
182c8365a76Scgd if (--nent <= 0)
183c8365a76Scgd break;
184c8365a76Scgd }
185c8365a76Scgd }
186c8365a76Scgd }
187c8365a76Scgd munmap(strtab, strsize);
188249933b8Schristos #if defined(__SSP__) || defined(__SSP_ALL__)
189249933b8Schristos free(scoreboard);
190249933b8Schristos #endif
191c8365a76Scgd return (nent);
192c8365a76Scgd }
193c8365a76Scgd #endif /* NLIST_AOUT */
194