1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * $FreeBSD: src/lib/libc/gen/nlist.c,v 1.12.2.1 2001/07/11 23:59:09 obrien Exp $ 34 * 35 * @(#)nlist.c 8.1 (Berkeley) 6/4/93 36 */ 37 38 #include "namespace.h" 39 #include <sys/param.h> 40 #include <sys/mman.h> 41 #include <sys/stat.h> 42 #include <sys/file.h> 43 44 #include <errno.h> 45 #include <a.out.h> 46 #include <stdio.h> 47 #include <string.h> 48 #include <unistd.h> 49 #include "un-namespace.h" 50 51 #define _NLIST_DO_AOUT 52 #define _NLIST_DO_ELF 53 54 #ifdef _NLIST_DO_ELF 55 #include <machine/elf.h> 56 #include <elf-hints.h> 57 #endif 58 59 int __fdnlist(int, struct nlist *); 60 int __aout_fdnlist(int, struct nlist *); 61 int __elf_fdnlist(int, struct nlist *); 62 63 int 64 nlist(const char *name, struct nlist *list) 65 { 66 int fd, n; 67 68 fd = _open(name, O_RDONLY, 0); 69 if (fd < 0) 70 return (-1); 71 n = __fdnlist(fd, list); 72 _close(fd); 73 return (n); 74 } 75 76 static struct nlist_handlers { 77 int (*fn)(int fd, struct nlist *list); 78 } nlist_fn[] = { 79 #ifdef _NLIST_DO_AOUT 80 { __aout_fdnlist }, 81 #endif 82 #ifdef _NLIST_DO_ELF 83 { __elf_fdnlist }, 84 #endif 85 }; 86 87 int 88 __fdnlist(int fd, struct nlist *list) 89 { 90 int n = -1; 91 size_t i; 92 93 for (i = 0; i < NELEM(nlist_fn); i++) { 94 n = (nlist_fn[i].fn)(fd, list); 95 if (n != -1) 96 break; 97 } 98 return (n); 99 } 100 101 #define ISLAST(p) (p->n_un.n_name == 0 || p->n_un.n_name[0] == 0) 102 103 #ifdef _NLIST_DO_AOUT 104 int 105 __aout_fdnlist(int fd, struct nlist *list) 106 { 107 struct nlist *p, *symtab; 108 caddr_t strtab, a_out_mmap; 109 off_t stroff, symoff; 110 u_long symsize; 111 int nent; 112 struct exec * exec; 113 struct stat st; 114 115 /* check that file is at least as large as struct exec! */ 116 if ((_fstat(fd, &st) < 0) || (st.st_size < sizeof(struct exec))) 117 return (-1); 118 119 /* Check for files too large to mmap. */ 120 if (st.st_size > SIZE_T_MAX) { 121 errno = EFBIG; 122 return (-1); 123 } 124 125 /* 126 * Map the whole a.out file into our address space. 127 * We then find the string table withing this area. 128 * We do not just mmap the string table, as it probably 129 * does not start at a page boundary - we save ourselves a 130 * lot of nastiness by mmapping the whole file. 131 * 132 * This gives us an easy way to randomly access all the strings, 133 * without making the memory allocation permanent as with 134 * malloc/free (i.e., munmap will return it to the system). 135 */ 136 a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0); 137 if (a_out_mmap == MAP_FAILED) 138 return (-1); 139 140 exec = (struct exec *)a_out_mmap; 141 if (N_BADMAG(*exec)) { 142 munmap(a_out_mmap, (size_t)st.st_size); 143 return (-1); 144 } 145 146 symoff = N_SYMOFF(*exec); 147 symsize = exec->a_syms; 148 stroff = symoff + symsize; 149 150 /* find the string table in our mmapped area */ 151 strtab = a_out_mmap + stroff; 152 symtab = (struct nlist *)(a_out_mmap + symoff); 153 154 /* 155 * clean out any left-over information for all valid entries. 156 * Type and value defined to be 0 if not found; historical 157 * versions cleared other and desc as well. Also figure out 158 * the largest string length so don't read any more of the 159 * string table than we have to. 160 * 161 * XXX clearing anything other than n_type and n_value violates 162 * the semantics given in the man page. 163 */ 164 nent = 0; 165 for (p = list; !ISLAST(p); ++p) { 166 p->n_type = 0; 167 p->n_other = 0; 168 p->n_desc = 0; 169 p->n_value = 0; 170 ++nent; 171 } 172 173 while (symsize > 0) { 174 int soff; 175 176 symsize-= sizeof(struct nlist); 177 soff = symtab->n_un.n_strx; 178 179 180 if (soff != 0 && (symtab->n_type & N_STAB) == 0) 181 for (p = list; !ISLAST(p); p++) 182 if (!strcmp(&strtab[soff], p->n_un.n_name)) { 183 p->n_value = symtab->n_value; 184 p->n_type = symtab->n_type; 185 p->n_desc = symtab->n_desc; 186 p->n_other = symtab->n_other; 187 if (--nent <= 0) 188 break; 189 } 190 symtab++; 191 } 192 munmap(a_out_mmap, (size_t)st.st_size); 193 return (nent); 194 } 195 #endif 196 197 #ifdef _NLIST_DO_ELF 198 static void elf_sym_to_nlist(struct nlist *, Elf_Sym *, Elf_Shdr *, int); 199 200 /* 201 * __elf_is_okay__ - Determine if ehdr really 202 * is ELF and valid for the target platform. 203 * 204 * WARNING: This is NOT a ELF ABI function and 205 * as such it's use should be restricted. 206 */ 207 static int 208 __elf_is_okay__(Elf_Ehdr *ehdr) 209 { 210 int retval = 0; 211 /* 212 * We need to check magic, class size, endianess, 213 * and version before we look at the rest of the 214 * Elf_Ehdr structure. These few elements are 215 * represented in a machine independant fashion. 216 */ 217 if (IS_ELF(*ehdr) && 218 ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS && 219 ehdr->e_ident[EI_DATA] == ELF_TARG_DATA && 220 ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) { 221 222 /* Now check the machine dependant header */ 223 if (ehdr->e_machine == ELF_TARG_MACH && 224 ehdr->e_version == ELF_TARG_VER) 225 retval = 1; 226 } 227 return retval; 228 } 229 230 int 231 __elf_fdnlist(int fd, struct nlist *list) 232 { 233 struct nlist *p; 234 Elf_Off symoff = 0, symstroff = 0; 235 Elf_Word symsize = 0, symstrsize = 0; 236 Elf_Sword cc, i; 237 int nent = -1; 238 int errsave; 239 Elf_Sym sbuf[1024]; 240 Elf_Sym *s; 241 Elf_Ehdr ehdr; 242 char *strtab = NULL; 243 Elf_Shdr *shdr = NULL; 244 Elf_Word shdr_size; 245 void *base; 246 struct stat st; 247 248 /* Make sure obj is OK */ 249 if (lseek(fd, (off_t)0, SEEK_SET) == -1 || 250 _read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) || 251 !__elf_is_okay__(&ehdr) || 252 _fstat(fd, &st) < 0) 253 return (-1); 254 255 /* calculate section header table size */ 256 shdr_size = ehdr.e_shentsize * ehdr.e_shnum; 257 258 #ifndef __x86_64__ 259 /* Make sure it's not too big to mmap */ 260 if (shdr_size > SIZE_T_MAX) { 261 errno = EFBIG; 262 return (-1); 263 } 264 #endif 265 266 /* mmap section header table */ 267 base = mmap(NULL, (size_t)shdr_size, PROT_READ, 0, fd, 268 (off_t)ehdr.e_shoff); 269 if (base == MAP_FAILED) 270 return (-1); 271 shdr = (Elf_Shdr *)base; 272 273 /* 274 * Find the symbol table entry and it's corresponding 275 * string table entry. Version 1.1 of the ABI states 276 * that there is only one symbol table but that this 277 * could change in the future. 278 */ 279 for (i = 0; i < ehdr.e_shnum; i++) { 280 if (shdr[i].sh_type == SHT_SYMTAB) { 281 symoff = shdr[i].sh_offset; 282 symsize = shdr[i].sh_size; 283 symstroff = shdr[shdr[i].sh_link].sh_offset; 284 symstrsize = shdr[shdr[i].sh_link].sh_size; 285 break; 286 } 287 } 288 289 #ifndef __x86_64__ 290 /* Check for files too large to mmap. */ 291 if (symstrsize > SIZE_T_MAX) { 292 errno = EFBIG; 293 goto done; 294 } 295 #endif 296 297 /* 298 * Map string table into our address space. This gives us 299 * an easy way to randomly access all the strings, without 300 * making the memory allocation permanent as with malloc/free 301 * (i.e., munmap will return it to the system). 302 */ 303 base = mmap(NULL, (size_t)symstrsize, PROT_READ, 0, fd, 304 (off_t)symstroff); 305 if (base == MAP_FAILED) 306 goto done; 307 strtab = (char *)base; 308 309 /* 310 * clean out any left-over information for all valid entries. 311 * Type and value defined to be 0 if not found; historical 312 * versions cleared other and desc as well. Also figure out 313 * the largest string length so don't read any more of the 314 * string table than we have to. 315 * 316 * XXX clearing anything other than n_type and n_value violates 317 * the semantics given in the man page. 318 */ 319 nent = 0; 320 for (p = list; !ISLAST(p); ++p) { 321 p->n_type = 0; 322 p->n_other = 0; 323 p->n_desc = 0; 324 p->n_value = 0; 325 ++nent; 326 } 327 328 /* Don't process any further if object is stripped. */ 329 if (symoff == 0) 330 goto done; 331 332 if (lseek(fd, (off_t) symoff, SEEK_SET) == -1) { 333 nent = -1; 334 goto done; 335 } 336 337 while (symsize > 0 && nent > 0) { 338 cc = MIN(symsize, sizeof(sbuf)); 339 if (_read(fd, sbuf, cc) != cc) 340 break; 341 symsize -= cc; 342 for (s = sbuf; cc > 0 && nent > 0; ++s, cc -= sizeof(*s)) { 343 char *name; 344 struct nlist *p_local; 345 346 name = strtab + s->st_name; 347 if (name[0] == '\0') 348 continue; 349 for (p_local = list; !ISLAST(p_local); p_local++) { 350 if ((p_local->n_un.n_name[0] == '_' && 351 strcmp(name, p_local->n_un.n_name+1) == 0) 352 || strcmp(name, p_local->n_un.n_name) == 0) { 353 elf_sym_to_nlist(p_local, s, shdr, 354 ehdr.e_shnum); 355 if (--nent <= 0) 356 break; 357 } 358 } 359 } 360 } 361 done: 362 errsave = errno; 363 if (strtab != NULL) 364 munmap(strtab, symstrsize); 365 if (shdr != NULL) 366 munmap(shdr, shdr_size); 367 errno = errsave; 368 return (nent); 369 } 370 371 /* 372 * Convert an Elf_Sym into an nlist structure. This fills in only the 373 * n_value and n_type members. 374 */ 375 static void 376 elf_sym_to_nlist(struct nlist *nl, Elf_Sym *s, Elf_Shdr *shdr, int shnum) 377 { 378 nl->n_value = s->st_value; 379 380 switch (s->st_shndx) { 381 case SHN_UNDEF: 382 case SHN_COMMON: 383 nl->n_type = N_UNDF; 384 break; 385 case SHN_ABS: 386 nl->n_type = ELF_ST_TYPE(s->st_info) == STT_FILE ? 387 N_FN : N_ABS; 388 break; 389 default: 390 if (s->st_shndx >= shnum) 391 nl->n_type = N_UNDF; 392 else { 393 Elf_Shdr *sh = shdr + s->st_shndx; 394 395 nl->n_type = sh->sh_type == SHT_PROGBITS ? 396 (sh->sh_flags & SHF_WRITE ? N_DATA : N_TEXT) : 397 (sh->sh_type == SHT_NOBITS ? N_BSS : N_UNDF); 398 } 399 break; 400 } 401 402 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL || 403 ELF_ST_BIND(s->st_info) == STB_WEAK) 404 nl->n_type |= N_EXT; 405 } 406 #endif /* _NLIST_DO_ELF */ 407