2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
35 static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
45 #include <machine/elf.h>
54 static bool wantsym(const Elf_Sym *, const char *);
56 /* Things which get -E excluded by default. */
57 static char *excludes[] = { ".mcount", "_mcleanup", NULL };
60 elf_getnfile(const char *filename, char ***defaultEs)
67 const Elf_Shdr *shdrs;
68 const Elf_Shdr *sh_symtab;
69 const Elf_Shdr *sh_strtab;
71 const Elf_Sym *symtab;
75 if ((fd = open(filename, O_RDONLY)) == -1)
76 err(1, "%s", filename);
77 if (read(fd, &h, sizeof h) != sizeof h || !IS_ELF(h)) {
81 if (fstat(fd, &s) == -1)
82 err(1, "cannot fstat %s", filename);
83 if ((mapbase = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
85 err(1, "cannot mmap %s", filename);
88 base = (const char *)mapbase;
89 shdrs = (const Elf_Shdr *)(base + h.e_shoff);
91 /* Find the symbol table and associated string table section. */
92 for (i = 1; i < h.e_shnum; i++)
93 if (shdrs[i].sh_type == SHT_SYMTAB)
96 errx(1, "%s has no symbol table", filename);
97 sh_symtab = &shdrs[i];
98 sh_strtab = &shdrs[sh_symtab->sh_link];
100 symtab = (const Elf_Sym *)(base + sh_symtab->sh_offset);
101 symtabct = sh_symtab->sh_size / sh_symtab->sh_entsize;
102 strtab = (const char *)(base + sh_strtab->sh_offset);
104 /* Count the symbols that we're interested in. */
106 for (i = 1; i < symtabct; i++)
107 if (wantsym(&symtab[i], strtab))
110 /* Allocate memory for them, plus a terminating entry. */
111 if ((nl = (nltype *)calloc(nname + 1, sizeof(nltype))) == NULL)
112 errx(1, "insufficient memory for symbol table");
116 for (i = 1; i < symtabct; i++) {
117 const Elf_Sym *sym = &symtab[i];
119 if (wantsym(sym, strtab)) {
120 npe->value = sym->st_value;
121 npe->name = strtab + sym->st_name;
127 *defaultEs = excludes;
132 wantsym(const Elf_Sym *sym, const char *strtab)
137 type = ELF_ST_TYPE(sym->st_info);
138 bind = ELF_ST_BIND(sym->st_info);
140 if (type != STT_FUNC ||
141 (aflag && bind == STB_LOCAL) ||
142 (uflag && strchr(strtab + sym->st_name, '.') != NULL))