]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libc/gen/nlist.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libc / gen / nlist.c
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)nlist.c     8.1 (Berkeley) 6/4/93";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/mman.h>
39 #include <sys/stat.h>
40 #include <sys/file.h>
41 #include <arpa/inet.h>
42
43 #include <errno.h>
44 #include <a.out.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include "un-namespace.h"
49
50 #define _NLIST_DO_AOUT
51 #define _NLIST_DO_ELF
52
53 #ifdef _NLIST_DO_ELF
54 #include <machine/elf.h>
55 #include <elf-hints.h>
56 #endif
57
58 int __fdnlist(int, struct nlist *);
59 int __aout_fdnlist(int, struct nlist *);
60 int __elf_fdnlist(int, struct nlist *);
61
62 int
63 nlist(name, list)
64         const char *name;
65         struct nlist *list;
66 {
67         int fd, n;
68
69         fd = _open(name, O_RDONLY, 0);
70         if (fd < 0)
71                 return (-1);
72         n = __fdnlist(fd, list);
73         (void)_close(fd);
74         return (n);
75 }
76
77 static struct nlist_handlers {
78         int     (*fn)(int fd, struct nlist *list);
79 } nlist_fn[] = {
80 #ifdef _NLIST_DO_AOUT
81         { __aout_fdnlist },
82 #endif
83 #ifdef _NLIST_DO_ELF
84         { __elf_fdnlist },
85 #endif
86 };
87
88 int
89 __fdnlist(fd, list)
90         int fd;
91         struct nlist *list;
92 {
93         int n = -1, i;
94
95         for (i = 0; i < sizeof(nlist_fn) / sizeof(nlist_fn[0]); i++) {
96                 n = (nlist_fn[i].fn)(fd, list);
97                 if (n != -1)
98                         break;
99         }
100         return (n);
101 }
102
103 #define ISLAST(p)       (p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
104
105 #ifdef _NLIST_DO_AOUT
106 int
107 __aout_fdnlist(fd, list)
108         int fd;
109         struct nlist *list;
110 {
111         struct nlist *p, *symtab;
112         caddr_t strtab, a_out_mmap;
113         off_t stroff, symoff;
114         u_long symsize;
115         int nent;
116         struct exec * exec;
117         struct stat st;
118
119         /* check that file is at least as large as struct exec! */
120         if ((_fstat(fd, &st) < 0) || (st.st_size < sizeof(struct exec)))
121                 return (-1);
122
123         /* Check for files too large to mmap. */
124         if (st.st_size > SIZE_T_MAX) {
125                 errno = EFBIG;
126                 return (-1);
127         }
128
129         /*
130          * Map the whole a.out file into our address space.
131          * We then find the string table withing this area.
132          * We do not just mmap the string table, as it probably
133          * does not start at a page boundary - we save ourselves a
134          * lot of nastiness by mmapping the whole file.
135          *
136          * This gives us an easy way to randomly access all the strings,
137          * without making the memory allocation permanent as with
138          * malloc/free (i.e., munmap will return it to the system).
139          */
140         a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
141         if (a_out_mmap == MAP_FAILED)
142                 return (-1);
143
144         exec = (struct exec *)a_out_mmap;
145         if (N_BADMAG(*exec)) {
146                 munmap(a_out_mmap, (size_t)st.st_size);
147                 return (-1);
148         }
149
150         symoff = N_SYMOFF(*exec);
151         symsize = exec->a_syms;
152         stroff = symoff + symsize;
153
154         /* find the string table in our mmapped area */
155         strtab = a_out_mmap + stroff;
156         symtab = (struct nlist *)(a_out_mmap + symoff);
157
158         /*
159          * clean out any left-over information for all valid entries.
160          * Type and value defined to be 0 if not found; historical
161          * versions cleared other and desc as well.  Also figure out
162          * the largest string length so don't read any more of the
163          * string table than we have to.
164          *
165          * XXX clearing anything other than n_type and n_value violates
166          * the semantics given in the man page.
167          */
168         nent = 0;
169         for (p = list; !ISLAST(p); ++p) {
170                 p->n_type = 0;
171                 p->n_other = 0;
172                 p->n_desc = 0;
173                 p->n_value = 0;
174                 ++nent;
175         }
176
177         while (symsize > 0) {
178                 int soff;
179
180                 symsize-= sizeof(struct nlist);
181                 soff = symtab->n_un.n_strx;
182
183
184                 if (soff != 0 && (symtab->n_type & N_STAB) == 0)
185                         for (p = list; !ISLAST(p); p++)
186                                 if (!strcmp(&strtab[soff], p->n_un.n_name)) {
187                                         p->n_value = symtab->n_value;
188                                         p->n_type = symtab->n_type;
189                                         p->n_desc = symtab->n_desc;
190                                         p->n_other = symtab->n_other;
191                                         if (--nent <= 0)
192                                                 break;
193                                 }
194                 symtab++;
195         }
196         munmap(a_out_mmap, (size_t)st.st_size);
197         return (nent);
198 }
199 #endif
200
201 #ifdef _NLIST_DO_ELF
202 static void elf_sym_to_nlist(struct nlist *, Elf_Sym *, Elf_Shdr *, int);
203
204 /*
205  * __elf_is_okay__ - Determine if ehdr really
206  * is ELF and valid for the target platform.
207  *
208  * WARNING:  This is NOT an ELF ABI function and
209  * as such its use should be restricted.
210  */
211 int
212 __elf_is_okay__(ehdr)
213         Elf_Ehdr *ehdr;
214 {
215         int retval = 0;
216         /*
217          * We need to check magic, class size, endianess,
218          * and version before we look at the rest of the
219          * Elf_Ehdr structure.  These few elements are
220          * represented in a machine independant fashion.
221          */
222         if (IS_ELF(*ehdr) &&
223             ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS &&
224             ehdr->e_ident[EI_DATA] == ELF_TARG_DATA &&
225             ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) {
226
227                 /* Now check the machine dependant header */
228                 if (ehdr->e_machine == ELF_TARG_MACH &&
229                     ehdr->e_version == ELF_TARG_VER)
230                         retval = 1;
231         }
232         return retval;
233 }
234
235 int
236 __elf_fdnlist(fd, list)
237         int fd;
238         struct nlist *list;
239 {
240         struct nlist *p;
241         Elf_Off symoff = 0, symstroff = 0;
242         Elf_Size symsize = 0, symstrsize = 0;
243         Elf_Ssize cc, i;
244         int nent = -1;
245         int errsave;
246         Elf_Sym sbuf[1024];
247         Elf_Sym *s;
248         Elf_Ehdr ehdr;
249         char *strtab = NULL;
250         Elf_Shdr *shdr = NULL;
251         Elf_Size shdr_size;
252         void *base;
253         struct stat st;
254
255         /* Make sure obj is OK */
256         if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
257             _read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) ||
258             !__elf_is_okay__(&ehdr) ||
259             _fstat(fd, &st) < 0)
260                 return (-1);
261
262         /* calculate section header table size */
263         shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
264
265         /* Make sure it's not too big to mmap */
266         if (shdr_size > SIZE_T_MAX) {
267                 errno = EFBIG;
268                 return (-1);
269         }
270
271         /* mmap section header table */
272         base = mmap(NULL, (size_t)shdr_size, PROT_READ, 0, fd,
273             (off_t)ehdr.e_shoff);
274         if (base == MAP_FAILED)
275                 return (-1);
276         shdr = (Elf_Shdr *)base;
277
278         /*
279          * Find the symbol table entry and it's corresponding
280          * string table entry.  Version 1.1 of the ABI states
281          * that there is only one symbol table but that this
282          * could change in the future.
283          */
284         for (i = 0; i < ehdr.e_shnum; i++) {
285                 if (shdr[i].sh_type == SHT_SYMTAB) {
286                         symoff = shdr[i].sh_offset;
287                         symsize = shdr[i].sh_size;
288                         symstroff = shdr[shdr[i].sh_link].sh_offset;
289                         symstrsize = shdr[shdr[i].sh_link].sh_size;
290                         break;
291                 }
292         }
293
294         /* Check for files too large to mmap. */
295         if (symstrsize > SIZE_T_MAX) {
296                 errno = EFBIG;
297                 goto done;
298         }
299         /*
300          * Map string table into our address space.  This gives us
301          * an easy way to randomly access all the strings, without
302          * making the memory allocation permanent as with malloc/free
303          * (i.e., munmap will return it to the system).
304          */
305         base = mmap(NULL, (size_t)symstrsize, PROT_READ, 0, fd,
306             (off_t)symstroff);
307         if (base == MAP_FAILED)
308                 goto done;
309         strtab = (char *)base;
310
311         /*
312          * clean out any left-over information for all valid entries.
313          * Type and value defined to be 0 if not found; historical
314          * versions cleared other and desc as well.  Also figure out
315          * the largest string length so don't read any more of the
316          * string table than we have to.
317          *
318          * XXX clearing anything other than n_type and n_value violates
319          * the semantics given in the man page.
320          */
321         nent = 0;
322         for (p = list; !ISLAST(p); ++p) {
323                 p->n_type = 0;
324                 p->n_other = 0;
325                 p->n_desc = 0;
326                 p->n_value = 0;
327                 ++nent;
328         }
329
330         /* Don't process any further if object is stripped. */
331         if (symoff == 0)
332                 goto done;
333                 
334         if (lseek(fd, (off_t) symoff, SEEK_SET) == -1) {
335                 nent = -1;
336                 goto done;
337         }
338
339         while (symsize > 0 && nent > 0) {
340                 cc = MIN(symsize, sizeof(sbuf));
341                 if (_read(fd, sbuf, cc) != cc)
342                         break;
343                 symsize -= cc;
344                 for (s = sbuf; cc > 0 && nent > 0; ++s, cc -= sizeof(*s)) {
345                         char *name;
346                         struct nlist *p;
347
348                         name = strtab + s->st_name;
349                         if (name[0] == '\0')
350                                 continue;
351                         for (p = list; !ISLAST(p); p++) {
352                                 if ((p->n_un.n_name[0] == '_' &&
353                                     strcmp(name, p->n_un.n_name+1) == 0)
354                                     || strcmp(name, p->n_un.n_name) == 0) {
355                                         elf_sym_to_nlist(p, s, shdr,
356                                             ehdr.e_shnum);
357                                         if (--nent <= 0)
358                                                 break;
359                                 }
360                         }
361                 }
362         }
363   done:
364         errsave = errno;
365         if (strtab != NULL)
366                 munmap(strtab, symstrsize);
367         if (shdr != NULL)
368                 munmap(shdr, shdr_size);
369         errno = errsave;
370         return (nent);
371 }
372
373 /*
374  * Convert an Elf_Sym into an nlist structure.  This fills in only the
375  * n_value and n_type members.
376  */
377 static void
378 elf_sym_to_nlist(nl, s, shdr, shnum)
379         struct nlist *nl;
380         Elf_Sym *s;
381         Elf_Shdr *shdr;
382         int shnum;
383 {
384         nl->n_value = s->st_value;
385
386         switch (s->st_shndx) {
387         case SHN_UNDEF:
388         case SHN_COMMON:
389                 nl->n_type = N_UNDF;
390                 break;
391         case SHN_ABS:
392                 nl->n_type = ELF_ST_TYPE(s->st_info) == STT_FILE ?
393                     N_FN : N_ABS;
394                 break;
395         default:
396                 if (s->st_shndx >= shnum)
397                         nl->n_type = N_UNDF;
398                 else {
399                         Elf_Shdr *sh = shdr + s->st_shndx;
400
401                         nl->n_type = sh->sh_type == SHT_PROGBITS ?
402                             (sh->sh_flags & SHF_WRITE ? N_DATA : N_TEXT) :
403                             (sh->sh_type == SHT_NOBITS ? N_BSS : N_UNDF);
404                 }
405                 break;
406         }
407
408         if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
409             ELF_ST_BIND(s->st_info) == STB_WEAK)
410                 nl->n_type |= N_EXT;
411 }
412 #endif /* _NLIST_DO_ELF */