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