VM: simplify slab allocator
[minix.git] / lib / libc / gen / nlist_coff.c
blob1ef34754264ee2e8796b4dd3640eb7e969b6f119
1 /* $NetBSD: nlist_coff.c,v 1.8 2009/08/21 08:42:02 he Exp $ */
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou
5 * 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 for the
18 * NetBSD Project. See http://www.NetBSD.org/ for
19 * information about NetBSD.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 __RCSID("$NetBSD: nlist_coff.c,v 1.8 2009/08/21 08:42:02 he Exp $");
40 #endif /* LIBC_SCCS and not lint */
42 #include "namespace.h"
43 #include <sys/param.h>
44 #include <sys/mman.h>
45 #include <sys/stat.h>
46 #include <sys/file.h>
48 #include <assert.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <nlist.h>
55 #include "nlist_private.h"
56 #ifdef NLIST_COFF
57 #include <sys/exec_coff.h>
58 #endif
60 #ifdef NLIST_COFF
61 #define BAD do { rv = -1; goto out; } while (/*CONSTCOND*/0)
62 #define BADUNMAP do { rv = -1; goto unmap; } while (/*CONSTCOND*/0)
64 #define ES_LEN 18
65 struct coff_extsym {
66 union {
67 char u_name[8];
68 struct {
69 int u_zero;
70 int u_offset;
71 } s;
72 } u;
73 int32_t es_value;
74 int16_t es_scnum;
75 int16_t es_type;
76 int8_t es_class;
77 int8_t es_numaux;
79 #define es_name u.u_name
80 #define es_zero u.s.u_zero
81 #define es_offset u.s.u_offset
83 int
84 __fdnlist_coff(fd, list)
85 int fd;
86 struct nlist *list;
88 struct nlist *p;
89 struct coff_filehdr *filehdrp;
90 struct stat st;
91 char *mappedfile;
92 size_t mappedsize;
93 u_long symoff, extstroff;
94 int rv, nent;
95 long i, nesyms;
97 _DIAGASSERT(fd != -1);
98 _DIAGASSERT(list != NULL);
100 rv = -1;
103 * If we can't fstat() the file, something bad is going on.
105 if (fstat(fd, &st) < 0)
106 BAD;
109 * Map the file in its entirety.
111 if ((uintmax_t)st.st_size > (uintmax_t)SIZE_T_MAX) {
112 errno = EFBIG;
113 BAD;
115 mappedsize = st.st_size;
116 mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
117 fd, 0);
118 if (mappedfile == (char *)-1)
119 BAD;
122 * Make sure we can access the executable's header
123 * directly, and make sure we recognize the executable
124 * as an COFF binary.
126 if (mappedsize < sizeof (struct coff_filehdr))
127 BADUNMAP;
128 filehdrp = (struct coff_filehdr *)&mappedfile[0];
130 if (COFF_BADMAG(filehdrp))
131 BADUNMAP;
134 * Find the symbol list.
136 symoff = filehdrp->f_symptr;
137 nesyms = filehdrp->f_nsyms;
139 if (symoff + ES_LEN * nesyms > mappedsize)
140 BADUNMAP;
141 extstroff = symoff + ES_LEN * nesyms;
143 nent = 0;
144 for (p = list; !ISLAST(p); ++p) {
145 p->n_type = 0;
146 p->n_other = 0;
147 p->n_desc = 0;
148 p->n_value = 0;
149 ++nent;
152 for (i = 0; i < nesyms; i++) {
153 char *symtabname;
154 const char *nlistname;
155 struct coff_extsym esym;
156 char name[10];
158 memcpy(&esym, &mappedfile[symoff + ES_LEN * i], ES_LEN);
159 if (esym.es_numaux != 0) {
160 i += esym.es_numaux; /* XXX Skip aux entry */
161 continue;
164 if (esym.es_zero != 0) {
165 memcpy(name, esym.es_name, 8);
166 name[8] = 0;
167 symtabname = name;
168 } else if (esym.es_offset != 0)
169 symtabname = &mappedfile[extstroff + esym.es_offset];
170 else
171 continue;
173 for (p = list; !ISLAST(p); p++) {
174 nlistname = N_NAME(p);
175 if (!strcmp(symtabname, nlistname)) {
177 * Translate (roughly) from COFF to nlist
179 p->n_value = esym.es_value;
180 p->n_type = N_EXT; /* XXX */
181 p->n_desc = 0; /* XXX */
182 p->n_other = 0; /* XXX */
184 if (--nent <= 0)
185 goto done;
186 break; /* into next run of outer loop */
191 done:
192 rv = nent;
193 unmap:
194 munmap(mappedfile, mappedsize);
195 out:
196 return (rv);
199 #endif /* NLIST_COFF */