1 /*- 2 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/types.h> 31 #include <sys/mman.h> 32 #include <sys/stat.h> 33 #include <sys/exec_elf.h> 34 35 #include <err.h> 36 #include <fcntl.h> 37 #include <string.h> 38 #include <unistd.h> 39 40 #include "gprof.h" 41 42 static bool wantsym(const Elf_Sym *, const char *); 43 44 /* Things which get -E excluded by default. */ 45 static char *excludes[] = { ".mcount", "_mcleanup", NULL }; 46 47 int 48 getnfile(const char *filename, char ***defaultEs) 49 { 50 int fd; 51 Elf_Ehdr h; 52 struct stat s; 53 void *mapbase; 54 const char *base; 55 const Elf_Shdr *shdrs; 56 const Elf_Shdr *sh_symtab; 57 const Elf_Shdr *sh_strtab; 58 const char *strtab; 59 const Elf_Sym *symtab; 60 int symtabct; 61 int i; 62 63 if ((fd = open(filename, O_RDONLY)) == -1) 64 err(1, "%s", filename); 65 if (read(fd, &h, sizeof h) != sizeof h || !IS_ELF(h)) { 66 close(fd); 67 return -1; 68 } 69 if (fstat(fd, &s) == -1) 70 err(1, "Cannot fstat %s", filename); 71 if ((mapbase = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) == 72 MAP_FAILED) 73 err(1, "Cannot mmap %s", filename); 74 close(fd); 75 76 base = (const char *)mapbase; 77 shdrs = (const Elf_Shdr *)(base + h.e_shoff); 78 79 /* Find the symbol table and associated string table section. */ 80 for (i = 1; i < h.e_shnum; i++) 81 if (shdrs[i].sh_type == SHT_SYMTAB) 82 break; 83 if (i == h.e_shnum) 84 errx(1, "%s has no symbol table", filename); 85 sh_symtab = &shdrs[i]; 86 sh_strtab = &shdrs[sh_symtab->sh_link]; 87 88 symtab = (const Elf_Sym *)(base + sh_symtab->sh_offset); 89 symtabct = sh_symtab->sh_size / sh_symtab->sh_entsize; 90 strtab = (const char *)(base + sh_strtab->sh_offset); 91 92 /* Count the symbols that we're interested in. */ 93 nname = 0; 94 for (i = 1; i < symtabct; i++) 95 if (wantsym(&symtab[i], strtab)) 96 nname++; 97 98 /* Allocate memory for them, plus a terminating entry. */ 99 if ((nl = (nltype *)calloc(nname + 1, sizeof(nltype))) == NULL) 100 errx(1, "Insufficient memory for symbol table"); 101 102 /* Read them in. */ 103 npe = nl; 104 for (i = 1; i < symtabct; i++) { 105 const Elf_Sym *sym = &symtab[i]; 106 107 if (wantsym(sym, strtab)) { 108 npe->value = sym->st_value; 109 npe->name = strtab + sym->st_name; 110 npe++; 111 } 112 } 113 npe->value = -1; 114 115 *defaultEs = excludes; 116 return 0; 117 } 118 119 static bool 120 wantsym(const Elf_Sym *sym, const char *strtab) 121 { 122 int type; 123 int bind; 124 125 type = ELF_ST_TYPE(sym->st_info); 126 bind = ELF_ST_BIND(sym->st_info); 127 128 if (type != STT_FUNC || (aflag && bind == STB_LOCAL)) 129 #if 0 130 || 131 (uflag && strchr(strtab + sym->st_name, '.') != NULL)) 132 #endif 133 return 0; 134 135 return 1; 136 } 137