]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libbsd/lib/contrib/src/nlist.c
update
[l4.git] / l4 / pkg / libbsd / lib / contrib / src / 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
35 #include <sys/param.h>
36 #include <sys/mman.h>
37 #include <sys/stat.h>
38 #include <sys/file.h>
39 #include <arpa/inet.h>
40
41 #include <errno.h>
42 #include <a.out.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <unistd.h>
46
47 #if !defined(__NO_A_OUT_SUPPORT)
48 #define _NLIST_DO_AOUT
49 #endif
50 #define _NLIST_DO_ELF
51
52 #ifdef _NLIST_DO_ELF
53 #include "local-elf.h"
54 #endif
55
56 #define SIZE_T_MAX 0xffffffffU
57
58 #ifdef _NLIST_DO_AOUT
59 static int __aout_fdnlist(int, struct nlist *);
60 #ifndef N_SYMSIZE
61 #define N_SYMSIZE(a)    ((a).a_syms)
62 #endif
63 #endif
64 #ifdef _NLIST_DO_ELF
65 static int __elf_fdnlist(int, struct nlist *);
66 #endif
67
68 /* FIXME: This function is used by libkvm0, so we need to export it.
69    It is not declared in the include files though. */
70 int __fdnlist(int, struct nlist *);
71
72 int
73 nlist(name, list)
74         const char *name;
75         struct nlist *list;
76 {
77         int fd, n;
78
79         fd = open(name, O_RDONLY, 0);
80         if (fd < 0)
81                 return (-1);
82         n = __fdnlist(fd, list);
83         (void)close(fd);
84         return (n);
85 }
86
87 static struct nlist_handlers {
88         int     (*fn)(int fd, struct nlist *list);
89 } nlist_fn[] = {
90 #ifdef _NLIST_DO_AOUT
91         { __aout_fdnlist },
92 #endif
93 #ifdef _NLIST_DO_ELF
94         { __elf_fdnlist },
95 #endif
96 };
97
98 int
99 __fdnlist(fd, list)
100         int fd;
101         struct nlist *list;
102 {
103         size_t i;
104         int n = -1;
105
106         for (i = 0; i < sizeof(nlist_fn) / sizeof(nlist_fn[0]); i++) {
107                 n = (nlist_fn[i].fn)(fd, list);
108                 if (n != -1)
109                         break;
110         }
111         return (n);
112 }
113
114 #define ISLAST(p)       (p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
115
116 #ifdef _NLIST_DO_AOUT
117 static int
118 __aout_fdnlist(fd, list)
119         int fd;
120         struct nlist *list;
121 {
122         struct nlist *p, *symtab;
123         caddr_t strtab, a_out_mmap;
124         off_t stroff, symoff;
125         u_long symsize;
126         int nent;
127         struct exec * exec;
128         struct stat st;
129
130         /* check that file is at least as large as struct exec! */
131         if ((fstat(fd, &st) < 0) || (st.st_size < sizeof(struct exec)))
132                 return (-1);
133
134         /* Check for files too large to mmap. */
135         if (st.st_size > SIZE_T_MAX) {
136                 errno = EFBIG;
137                 return (-1);
138         }
139
140         /*
141          * Map the whole a.out file into our address space.
142          * We then find the string table within this area.
143          * We do not just mmap the string table, as it probably
144          * does not start at a page boundary - we save ourselves a
145          * lot of nastiness by mmapping the whole file.
146          *
147          * This gives us an easy way to randomly access all the strings,
148          * without making the memory allocation permanent as with
149          * malloc/free (i.e., munmap will return it to the system).
150          */
151         a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
152         if (a_out_mmap == MAP_FAILED)
153                 return (-1);
154
155         exec = (struct exec *)a_out_mmap;
156         if (N_BADMAG(*exec)) {
157                 munmap(a_out_mmap, (size_t)st.st_size);
158                 return (-1);
159         }
160
161         symoff = N_SYMOFF(*exec);
162         symsize = N_SYMSIZE(*exec);
163         stroff = symoff + symsize;
164
165         /* find the string table in our mmapped area */
166         strtab = a_out_mmap + stroff;
167         symtab = (struct nlist *)(a_out_mmap + symoff);
168
169         /*
170          * clean out any left-over information for all valid entries.
171          * Type and value defined to be 0 if not found; historical
172          * versions cleared other and desc as well.  Also figure out
173          * the largest string length so don't read any more of the
174          * string table than we have to.
175          *
176          * XXX clearing anything other than n_type and n_value violates
177          * the semantics given in the man page.
178          */
179         nent = 0;
180         for (p = list; !ISLAST(p); ++p) {
181                 p->n_type = 0;
182                 p->n_other = 0;
183                 p->n_desc = 0;
184                 p->n_value = 0;
185                 ++nent;
186         }
187
188         while (symsize > 0) {
189                 int soff;
190
191                 symsize-= sizeof(struct nlist);
192                 soff = symtab->n_un.n_strx;
193
194
195                 if (soff != 0 && (symtab->n_type & N_STAB) == 0)
196                         for (p = list; !ISLAST(p); p++)
197                                 if (!strcmp(&strtab[soff], p->n_un.n_name)) {
198                                         p->n_value = symtab->n_value;
199                                         p->n_type = symtab->n_type;
200                                         p->n_desc = symtab->n_desc;
201                                         p->n_other = symtab->n_other;
202                                         if (--nent <= 0)
203                                                 break;
204                                 }
205                 symtab++;
206         }
207         munmap(a_out_mmap, (size_t)st.st_size);
208         return (nent);
209 }
210 #endif
211
212 #ifdef _NLIST_DO_ELF
213 static void elf_sym_to_nlist(struct nlist *, Elf_Sym *, Elf_Shdr *, int);
214
215 /*
216  * __elf_is_okay__ - Determine if ehdr really
217  * is ELF and valid for the target platform.
218  *
219  * WARNING:  This is NOT an ELF ABI function and
220  * as such its use should be restricted.
221  */
222 static int
223 __elf_is_okay__(ehdr)
224         Elf_Ehdr *ehdr;
225 {
226         int retval = 0;
227         /*
228          * We need to check magic, class size, endianess,
229          * and version before we look at the rest of the
230          * Elf_Ehdr structure.  These few elements are
231          * represented in a machine independent fashion.
232          */
233         if (IS_ELF(*ehdr) &&
234             ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS &&
235             ehdr->e_ident[EI_DATA] == ELF_TARG_DATA &&
236             ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) {
237
238                 /* Now check the machine dependent header */
239                 if (ehdr->e_machine == ELF_TARG_MACH &&
240                     ehdr->e_version == ELF_TARG_VER)
241                         retval = 1;
242         }
243         return retval;
244 }
245
246 static int
247 __elf_fdnlist(fd, list)
248         int fd;
249         struct nlist *list;
250 {
251         struct nlist *p;
252         Elf_Off symoff = 0, symstroff = 0;
253         Elf_Word symsize = 0, symstrsize = 0;
254         Elf_Sword cc, i;
255         int nent = -1;
256         int errsave;
257         Elf_Sym sbuf[1024];
258         Elf_Sym *s;
259         Elf_Ehdr ehdr;
260         char *strtab = NULL;
261         Elf_Shdr *shdr = NULL;
262         Elf_Word shdr_size;
263         void *base;
264         struct stat st;
265
266         /* Make sure obj is OK */
267         if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
268             read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) ||
269             !__elf_is_okay__(&ehdr) ||
270             fstat(fd, &st) < 0)
271                 return (-1);
272
273         /* calculate section header table size */
274         shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
275
276         /* Make sure it's not too big to mmap */
277         if (shdr_size > SIZE_T_MAX) {
278                 errno = EFBIG;
279                 return (-1);
280         }
281
282         /* mmap section header table */
283         base = mmap(NULL, (size_t)shdr_size, PROT_READ, 0, fd,
284             (off_t)ehdr.e_shoff);
285         if (base == MAP_FAILED)
286                 return (-1);
287         shdr = (Elf_Shdr *)base;
288
289         /*
290          * Find the symbol table entry and it's corresponding
291          * string table entry.  Version 1.1 of the ABI states
292          * that there is only one symbol table but that this
293          * could change in the future.
294          */
295         for (i = 0; i < ehdr.e_shnum; i++) {
296                 if (shdr[i].sh_type == SHT_SYMTAB) {
297                         symoff = shdr[i].sh_offset;
298                         symsize = shdr[i].sh_size;
299                         symstroff = shdr[shdr[i].sh_link].sh_offset;
300                         symstrsize = shdr[shdr[i].sh_link].sh_size;
301                         break;
302                 }
303         }
304
305         /* Check for files too large to mmap. */
306         if (symstrsize > SIZE_T_MAX) {
307                 errno = EFBIG;
308                 goto done;
309         }
310         /*
311          * Map string table into our address space.  This gives us
312          * an easy way to randomly access all the strings, without
313          * making the memory allocation permanent as with malloc/free
314          * (i.e., munmap will return it to the system).
315          */
316         base = mmap(NULL, (size_t)symstrsize, PROT_READ, 0, fd,
317             (off_t)symstroff);
318         if (base == MAP_FAILED)
319                 goto done;
320         strtab = (char *)base;
321
322         /*
323          * clean out any left-over information for all valid entries.
324          * Type and value defined to be 0 if not found; historical
325          * versions cleared other and desc as well.  Also figure out
326          * the largest string length so don't read any more of the
327          * string table than we have to.
328          *
329          * XXX clearing anything other than n_type and n_value violates
330          * the semantics given in the man page.
331          */
332         nent = 0;
333         for (p = list; !ISLAST(p); ++p) {
334                 p->n_type = 0;
335                 p->n_other = 0;
336                 p->n_desc = 0;
337                 p->n_value = 0;
338                 ++nent;
339         }
340
341         /* Don't process any further if object is stripped. */
342         if (symoff == 0)
343                 goto done;
344                 
345         if (lseek(fd, (off_t) symoff, SEEK_SET) == -1) {
346                 nent = -1;
347                 goto done;
348         }
349
350         while (symsize > 0 && nent > 0) {
351                 cc = MIN(symsize, sizeof(sbuf));
352                 if (read(fd, sbuf, cc) != cc)
353                         break;
354                 symsize -= cc;
355                 for (s = sbuf; cc > 0 && nent > 0; ++s, cc -= sizeof(*s)) {
356                         char *name;
357                         struct nlist *p;
358
359                         name = strtab + s->st_name;
360                         if (name[0] == '\0')
361                                 continue;
362                         for (p = list; !ISLAST(p); p++) {
363                                 if ((p->n_un.n_name[0] == '_' &&
364                                     strcmp(name, p->n_un.n_name+1) == 0)
365                                     || strcmp(name, p->n_un.n_name) == 0) {
366                                         elf_sym_to_nlist(p, s, shdr,
367                                             ehdr.e_shnum);
368                                         if (--nent <= 0)
369                                                 break;
370                                 }
371                         }
372                 }
373         }
374   done:
375         errsave = errno;
376         if (strtab != NULL)
377                 munmap(strtab, symstrsize);
378         if (shdr != NULL)
379                 munmap(shdr, shdr_size);
380         errno = errsave;
381         return (nent);
382 }
383
384 /*
385  * Convert an Elf_Sym into an nlist structure.  This fills in only the
386  * n_value and n_type members.
387  */
388 static void
389 elf_sym_to_nlist(nl, s, shdr, shnum)
390         struct nlist *nl;
391         Elf_Sym *s;
392         Elf_Shdr *shdr;
393         int shnum;
394 {
395         nl->n_value = s->st_value;
396
397         switch (s->st_shndx) {
398         case SHN_UNDEF:
399         case SHN_COMMON:
400                 nl->n_type = N_UNDF;
401                 break;
402         case SHN_ABS:
403                 nl->n_type = ELF_ST_TYPE(s->st_info) == STT_FILE ?
404                     N_FN : N_ABS;
405                 break;
406         default:
407                 if (s->st_shndx >= shnum)
408                         nl->n_type = N_UNDF;
409                 else {
410                         Elf_Shdr *sh = shdr + s->st_shndx;
411
412                         nl->n_type = sh->sh_type == SHT_PROGBITS ?
413                             (sh->sh_flags & SHF_WRITE ? N_DATA : N_TEXT) :
414                             (sh->sh_type == SHT_NOBITS ? N_BSS : N_UNDF);
415                 }
416                 break;
417         }
418
419         if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
420             ELF_ST_BIND(s->st_info) == STB_WEAK)
421                 nl->n_type |= N_EXT;
422 }
423 #endif /* _NLIST_DO_ELF */