1 /* $NetBSD: nlist_elf32.c,v 1.26 2004/12/28 00:08:08 thorpej 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 /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */ 38 #ifndef ELFSIZE 39 #define ELFSIZE 32 40 #endif 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 #include <sys/ioctl.h> 48 #include <sys/ksyms.h> 49 50 #include <assert.h> 51 #include <errno.h> 52 #include <stdio.h> 53 #include <string.h> 54 #include <unistd.h> 55 #include <a.out.h> /* for 'struct nlist' declaration */ 56 57 #include "nlist_private.h" 58 #if defined(NLIST_ELF32) || defined(NLIST_ELF64) 59 #include <sys/exec_elf.h> 60 #endif 61 62 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \ 63 (defined(NLIST_ELF64) && (ELFSIZE == 64)) 64 65 /* No need to check for off < 0 because it is unsigned */ 66 #define check(off, size) (off + size > mappedsize) 67 #define BAD goto out 68 #define BADUNMAP goto unmap 69 70 int 71 ELFNAMEEND(__fdnlist)(fd, list) 72 int fd; 73 struct nlist *list; 74 { 75 struct stat st; 76 struct nlist *p; 77 char *mappedfile, *strtab; 78 size_t mappedsize; 79 Elf_Ehdr *ehdrp, ehdr; 80 Elf_Shdr *shdrp, *symshdrp, *symstrshdrp; 81 Elf_Sym *symp; 82 Elf_Off shdr_off; 83 Elf_Word shdr_size; 84 #if (ELFSIZE == 32) 85 Elf32_Half nshdr; 86 #elif (ELFSIZE == 64) 87 Elf64_Half nshdr; 88 #endif 89 size_t i, nsyms; 90 int rv, nent; 91 92 _DIAGASSERT(fd != -1); 93 _DIAGASSERT(list != NULL); 94 95 rv = -1; 96 97 symshdrp = symstrshdrp = NULL; 98 99 /* 100 * If we can't fstat() the file, something bad is going on. 101 */ 102 if (fstat(fd, &st) < 0) 103 BAD; 104 105 /* 106 * Map the file in its entirety. 107 */ 108 if (st.st_size > SIZE_T_MAX) { 109 errno = EFBIG; 110 BAD; 111 } 112 113 /* 114 * Read the elf header of the file. 115 */ 116 if ((ssize_t)(i = pread(fd, &ehdr, sizeof(Elf_Ehdr), (off_t)0)) == -1) 117 BAD; 118 119 /* 120 * Check that the elf header is correct. 121 */ 122 if (i != sizeof(Elf_Ehdr)) 123 BAD; 124 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 || 125 ehdr.e_ident[EI_CLASS] != ELFCLASS) 126 BAD; 127 128 switch (ehdr.e_machine) { 129 ELFDEFNNAME(MACHDEP_ID_CASES) 130 131 default: 132 BADUNMAP; 133 } 134 135 if (S_ISCHR(st.st_mode)) { 136 const char *nlistname; 137 struct ksyms_gsymbol kg; 138 Elf_Sym sym; 139 140 /* 141 * Character device; assume /dev/ksyms. 142 */ 143 nent = 0; 144 for (p = list; !ISLAST(p); ++p) { 145 146 p->n_other = 0; 147 p->n_desc = 0; 148 nlistname = p->n_un.n_name; 149 if (*nlistname == '_') 150 nlistname++; 151 152 kg.kg_name = nlistname; 153 kg.kg_sym = &sym; 154 if (ioctl(fd, KIOCGSYMBOL, &kg) == 0) { 155 p->n_value = sym.st_value; 156 switch (ELF_ST_TYPE(sym.st_info)) { 157 case STT_NOTYPE: 158 p->n_type = N_UNDF; 159 break; 160 case STT_OBJECT: 161 p->n_type = N_DATA; 162 break; 163 case STT_FUNC: 164 p->n_type = N_TEXT; 165 break; 166 case STT_FILE: 167 p->n_type = N_FN; 168 break; 169 default: 170 p->n_type = 0; 171 /* catch other enumerations for gcc */ 172 break; 173 } 174 if (ELF_ST_BIND(sym.st_info) != STB_LOCAL) 175 p->n_type |= N_EXT; 176 } else { 177 nent++; 178 p->n_value = 0; 179 p->n_type = 0; 180 } 181 } 182 return nent; 183 } 184 185 mappedsize = (size_t)st.st_size; 186 mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE, 187 fd, (off_t)0); 188 if (mappedfile == (char *)-1) 189 BAD; 190 191 /* 192 * Make sure we can access the executable's header 193 * directly, and make sure the recognize the executable 194 * as an ELF binary. 195 */ 196 if (check(0, sizeof *ehdrp)) 197 BADUNMAP; 198 ehdrp = (Elf_Ehdr *)(void *)&mappedfile[0]; 199 200 /* 201 * Find the symbol list and string table. 202 */ 203 nshdr = ehdrp->e_shnum; 204 shdr_off = ehdrp->e_shoff; 205 shdr_size = ehdrp->e_shentsize * nshdr; 206 207 if (check(shdr_off, shdr_size) || 208 (sizeof *shdrp != ehdrp->e_shentsize)) 209 BADUNMAP; 210 shdrp = (Elf_Shdr *)(void *)&mappedfile[shdr_off]; 211 212 for (i = 0; i < nshdr; i++) { 213 if (shdrp[i].sh_type == SHT_SYMTAB) { 214 symshdrp = &shdrp[i]; 215 symstrshdrp = &shdrp[shdrp[i].sh_link]; 216 } 217 } 218 219 /* Make sure we're not stripped. */ 220 if (symshdrp == NULL || symshdrp->sh_offset == 0) 221 BADUNMAP; 222 223 /* Make sure the symbols and strings are safely mapped. */ 224 if (check(symshdrp->sh_offset, symshdrp->sh_size)) 225 BADUNMAP; 226 if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size)) 227 BADUNMAP; 228 229 symp = (Elf_Sym *)(void *)&mappedfile[symshdrp->sh_offset]; 230 nsyms = symshdrp->sh_size / sizeof(*symp); 231 strtab = &mappedfile[symstrshdrp->sh_offset]; 232 233 /* 234 * Clean out any left-over information for all valid entries. 235 * Type and value are defined to be 0 if not found; historical 236 * versions cleared other and desc as well. 237 * 238 * XXX Clearing anything other than n_type and n_value violates 239 * the semantics given in the man page. 240 */ 241 nent = 0; 242 for (p = list; !ISLAST(p); ++p) { 243 p->n_type = 0; 244 p->n_other = 0; 245 p->n_desc = 0; 246 p->n_value = 0; 247 ++nent; 248 } 249 250 for (i = 0; i < nsyms; i++) { 251 for (p = list; !ISLAST(p); ++p) { 252 const char *nlistname; 253 char *symtabname; 254 255 /* This may be incorrect */ 256 nlistname = p->n_un.n_name; 257 if (*nlistname == '_') 258 nlistname++; 259 260 symtabname = &strtab[symp[i].st_name]; 261 262 if (!strcmp(symtabname, nlistname)) { 263 /* 264 * Translate (roughly) from ELF to nlist 265 */ 266 p->n_value = symp[i].st_value; 267 switch (ELF_ST_TYPE(symp[i].st_info)) { 268 case STT_NOTYPE: 269 p->n_type = N_UNDF; 270 break; 271 case STT_OBJECT: 272 p->n_type = N_DATA; 273 break; 274 case STT_FUNC: 275 p->n_type = N_TEXT; 276 break; 277 case STT_FILE: 278 p->n_type = N_FN; 279 break; 280 default: 281 /* catch other enumerations for gcc */ 282 break; 283 } 284 if (ELF_ST_BIND(symp[i].st_info) != STB_LOCAL) 285 p->n_type |= N_EXT; 286 p->n_desc = 0; /* XXX */ 287 p->n_other = 0; /* XXX */ 288 289 if (--nent <= 0) 290 goto done; 291 break; /* into next run of outer loop */ 292 } 293 } 294 } 295 296 done: 297 rv = nent; 298 unmap: 299 munmap(mappedfile, mappedsize); 300 out: 301 return (rv); 302 } 303 304 #endif 305