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