1 /* $NetBSD: nlist_ecoff.c,v 1.3 1996/10/01 13:37:05 cgd Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Christopher G. Demetriou 17 * for the NetBSD Project. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #if defined(LIBC_SCCS) && !defined(lint) 34 static char rcsid[] = "$NetBSD: nlist_ecoff.c,v 1.3 1996/10/01 13:37:05 cgd Exp $"; 35 #endif /* LIBC_SCCS and not lint */ 36 37 #include <sys/param.h> 38 #include <sys/mman.h> 39 #include <sys/stat.h> 40 #include <sys/file.h> 41 42 #include <errno.h> 43 #include <stdio.h> 44 #include <string.h> 45 #include <unistd.h> 46 #include <a.out.h> /* for 'struct nlist' declaration */ 47 48 #include "nlist_private.h" 49 #ifdef NLIST_ECOFF 50 #include <sys/exec_ecoff.h> 51 #endif 52 53 #ifdef NLIST_ECOFF 54 #define check(off, size) ((off < 0) || (off + size > mappedsize)) 55 #define BAD do { rv = -1; goto out; } while (0) 56 #define BADUNMAP do { rv = -1; goto unmap; } while (0) 57 58 int 59 __fdnlist_ecoff(fd, list) 60 register int fd; 61 register struct nlist *list; 62 { 63 struct nlist *p; 64 struct ecoff_exechdr *exechdrp; 65 struct ecoff_symhdr *symhdrp; 66 struct ecoff_extsym *esyms; 67 struct stat st; 68 char *mappedfile; 69 size_t mappedsize; 70 u_long symhdroff, extstroff; 71 u_int symhdrsize; 72 int rv, nent; 73 long i, nesyms; 74 75 rv = -1; 76 77 /* 78 * If we can't fstat() the file, something bad is going on. 79 */ 80 if (fstat(fd, &st) < 0) 81 BAD; 82 83 /* 84 * Map the file in its entirety. 85 */ 86 if (st.st_size > SIZE_T_MAX) { 87 errno = EFBIG; 88 BAD; 89 } 90 mappedsize = st.st_size; 91 mappedfile = mmap(NULL, mappedsize, PROT_READ, 0, fd, 0); 92 if (mappedfile == (char *)-1) 93 BAD; 94 95 /* 96 * Make sure we can access the executable's header 97 * directly, and make sure the recognize the executable 98 * as an ECOFF binary. 99 */ 100 if (check(0, sizeof *exechdrp)) 101 BADUNMAP; 102 exechdrp = (struct ecoff_exechdr *)&mappedfile[0]; 103 104 if (ECOFF_BADMAG(exechdrp)) 105 BADUNMAP; 106 107 /* 108 * Find the symbol list. 109 */ 110 symhdroff = exechdrp->f.f_symptr; 111 symhdrsize = exechdrp->f.f_nsyms; 112 113 if (check(symhdroff, sizeof *symhdrp) || 114 sizeof *symhdrp != symhdrsize) 115 BADUNMAP; 116 symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff]; 117 118 nesyms = symhdrp->esymMax; 119 if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms)) 120 BADUNMAP; 121 esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset]; 122 extstroff = symhdrp->cbSsExtOffset; 123 124 /* 125 * Clean out any left-over information for all valid entries. 126 * Type and value are defined to be 0 if not found; historical 127 * versions cleared other and desc as well. 128 * 129 * XXX Clearing anything other than n_type and n_value violates 130 * the semantics given in the man page. 131 */ 132 nent = 0; 133 for (p = list; !ISLAST(p); ++p) { 134 p->n_type = 0; 135 p->n_other = 0; 136 p->n_desc = 0; 137 p->n_value = 0; 138 ++nent; 139 } 140 141 for (i = 0; i < nesyms; i++) { 142 for (p = list; !ISLAST(p); p++) { 143 char *nlistname; 144 char *symtabname; 145 146 /* This may be incorrect */ 147 nlistname = p->n_un.n_name; 148 if (*nlistname == '_') 149 nlistname++; 150 151 symtabname = 152 &mappedfile[extstroff + esyms[i].es_strindex]; 153 154 if (!strcmp(symtabname, nlistname)) { 155 /* 156 * Translate (roughly) from ECOFF to nlist 157 */ 158 p->n_value = esyms[i].es_value; 159 p->n_type = N_EXT; /* XXX */ 160 p->n_desc = 0; /* XXX */ 161 p->n_other = 0; /* XXX */ 162 163 if (--nent <= 0) 164 goto done; 165 break; /* into next run of outer loop */ 166 } 167 } 168 } 169 170 done: 171 rv = nent; 172 unmap: 173 munmap(mappedfile, mappedsize); 174 out: 175 return (rv); 176 } 177 178 #endif /* NLIST_ECOFF */ 179