1 /* $NetBSD: nlist_coff.c,v 1.11 2012/03/22 14:18:34 christos Exp $ */
2 
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.
22  *
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.
33  *
34  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35  */
36 
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 __RCSID("$NetBSD: nlist_coff.c,v 1.11 2012/03/22 14:18:34 christos Exp $");
40 #endif /* LIBC_SCCS and not lint */
41 
42 #include "namespace.h"
43 #include <sys/param.h>
44 #include <sys/mman.h>
45 #include <sys/stat.h>
46 #include <sys/file.h>
47 
48 #include <assert.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <nlist.h>
54 
55 #include "nlist_private.h"
56 #ifdef NLIST_COFF
57 #include <sys/exec_coff.h>
58 #endif
59 
60 #ifdef NLIST_COFF
61 
62 #define ES_LEN 18
63 struct coff_extsym {
64           union {
65                     char u_name[8];
66                     struct {
67                               int u_zero;
68                               int u_offset;
69                     } s;
70           } u;
71           int32_t es_value;
72           int16_t es_scnum;
73           int16_t es_type;
74           int8_t es_class;
75           int8_t es_numaux;
76 };
77 #define es_name u.u_name
78 #define es_zero u.s.u_zero
79 #define es_offset u.s.u_offset
80 
81 int
__fdnlist_coff(int fd,struct nlist * list)82 __fdnlist_coff(int fd, struct nlist *list)
83 {
84           struct nlist *p;
85           struct coff_filehdr *filehdrp;
86           struct stat st;
87           char *mappedfile;
88           size_t mappedsize;
89           u_long symoff, extstroff;
90           int rv, nent;
91           long i, nesyms;
92 
93           _DIAGASSERT(fd != -1);
94           _DIAGASSERT(list != NULL);
95 
96           rv = -1;
97 
98           /*
99            * If we can't fstat() the file, something bad is going on.
100            */
101           if (fstat(fd, &st) < 0)
102                     goto out;
103 
104           /*
105            * Map the file in its entirety.
106            */
107           if ((uintmax_t)st.st_size > (uintmax_t)SIZE_T_MAX) {
108                     errno = EFBIG;
109                     goto out;
110           }
111           mappedsize = (size_t)st.st_size;
112           mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
113               fd, 0);
114           if (mappedfile == MAP_FAILED)
115                     goto out;
116 
117           /*
118            * Make sure we can access the executable's header
119            * directly, and make sure we recognize the executable
120            * as an COFF binary.
121            */
122           if (mappedsize < sizeof (struct coff_filehdr))
123                     goto unmap;
124           filehdrp = (void *)&mappedfile[0];
125 
126           if (COFF_BADMAG(filehdrp))
127                     goto unmap;
128 
129           /*
130            * Find the symbol list.
131            */
132           symoff = filehdrp->f_symptr;
133           nesyms = filehdrp->f_nsyms;
134 
135           if (symoff + ES_LEN * nesyms > mappedsize)
136                     goto unmap;
137           extstroff = symoff + ES_LEN * nesyms;
138 
139           nent = 0;
140           for (p = list; !ISLAST(p); ++p) {
141                     p->n_type = 0;
142                     p->n_other = 0;
143                     p->n_desc = 0;
144                     p->n_value = 0;
145                     ++nent;
146           }
147 
148           for (i = 0; i < nesyms; i++) {
149                     char *symtabname;
150                     const char *nlistname;
151                     struct coff_extsym esym;
152                     char name[10];
153 
154                     memcpy(&esym, &mappedfile[symoff + ES_LEN * i], ES_LEN);
155                     if (esym.es_numaux != 0) {
156                               i += esym.es_numaux;          /* XXX Skip aux entry */
157                               continue;
158                     }
159 
160                     if (esym.es_zero != 0) {
161                               memcpy(name, esym.es_name, 8);
162                               name[8] = 0;
163                               symtabname = name;
164                     } else if (esym.es_offset != 0)
165                               symtabname = &mappedfile[extstroff + esym.es_offset];
166                     else
167                               continue;
168 
169                     for (p = list; !ISLAST(p); p++) {
170                               nlistname = N_NAME(p);
171                               if (!strcmp(symtabname, nlistname)) {
172                                         /*
173                                          * Translate (roughly) from COFF to nlist
174                                          */
175                                         p->n_value = esym.es_value;
176                                         p->n_type = N_EXT;            /* XXX */
177                                         p->n_desc = 0;                          /* XXX */
178                                         p->n_other = 0;                         /* XXX */
179 
180                                         if (--nent <= 0)
181                                                   goto done;
182                                         break;    /* into next run of outer loop */
183                               }
184                     }
185           }
186 
187 done:
188           rv = nent;
189 unmap:
190           munmap(mappedfile, mappedsize);
191 out:
192           return rv;
193 }
194 
195 #endif /* NLIST_COFF */
196