2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. 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.
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
33 static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
43 #include <machine/elf.h>
52 static bool wantsym(const Elf_Sym *, const char *);
54 /* Things which get -E excluded by default. */
55 static char *excludes[] = { ".mcount", "_mcleanup", NULL };
58 elf_getnfile(const char *filename, char ***defaultEs)
65 const Elf_Shdr *shdrs;
66 const Elf_Shdr *sh_symtab;
67 const Elf_Shdr *sh_strtab;
69 const Elf_Sym *symtab;
73 if ((fd = open(filename, O_RDONLY)) == -1)
74 err(1, "%s", filename);
75 if (read(fd, &h, sizeof h) != sizeof h || !IS_ELF(h)) {
79 if (fstat(fd, &s) == -1)
80 err(1, "cannot fstat %s", filename);
81 if ((mapbase = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
83 err(1, "cannot mmap %s", filename);
86 base = (const char *)mapbase;
87 shdrs = (const Elf_Shdr *)(base + h.e_shoff);
89 /* Find the symbol table and associated string table section. */
90 for (i = 1; i < h.e_shnum; i++)
91 if (shdrs[i].sh_type == SHT_SYMTAB)
94 errx(1, "%s has no symbol table", filename);
95 sh_symtab = &shdrs[i];
96 sh_strtab = &shdrs[sh_symtab->sh_link];
98 symtab = (const Elf_Sym *)(base + sh_symtab->sh_offset);
99 symtabct = sh_symtab->sh_size / sh_symtab->sh_entsize;
100 strtab = (const char *)(base + sh_strtab->sh_offset);
102 /* Count the symbols that we're interested in. */
104 for (i = 1; i < symtabct; i++)
105 if (wantsym(&symtab[i], strtab))
108 /* Allocate memory for them, plus a terminating entry. */
109 if ((nl = (nltype *)calloc(nname + 1, sizeof(nltype))) == NULL)
110 errx(1, "insufficient memory for symbol table");
114 for (i = 1; i < symtabct; i++) {
115 const Elf_Sym *sym = &symtab[i];
117 if (wantsym(sym, strtab)) {
118 npe->value = sym->st_value;
119 npe->name = strtab + sym->st_name;
125 *defaultEs = excludes;
130 wantsym(const Elf_Sym *sym, const char *strtab)
135 type = ELF_ST_TYPE(sym->st_info);
136 bind = ELF_ST_BIND(sym->st_info);
138 if (type != STT_FUNC ||
139 (aflag && bind == STB_LOCAL) ||
140 (uflag && strchr(strtab + sym->st_name, '.') != NULL))