xref: /freebsd/usr.bin/gprof/elf.c (revision 848ee2a3a8b47c9905fc51fefcf60eb371edbb98)
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 #ifndef lint
35 static char sccsid[] = "@(#)gprof.c	8.1 (Berkeley) 6/6/93";
36 #endif /* not lint */
37 #endif
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/types.h>
43 #include <sys/mman.h>
44 #include <sys/stat.h>
45 #include <machine/elf.h>
46 
47 #include <err.h>
48 #include <fcntl.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include "gprof.h"
53 
54 static bool wantsym(const Elf_Sym *, const char *);
55 
56 /* Things which get -E excluded by default. */
57 static char	*excludes[] = { ".mcount", "_mcleanup", NULL };
58 
59 int
60 elf_getnfile(const char *filename, char ***defaultEs)
61 {
62     int fd;
63     Elf_Ehdr h;
64     struct stat s;
65     void *mapbase;
66     const char *base;
67     const Elf_Shdr *shdrs;
68     const Elf_Shdr *sh_symtab;
69     const Elf_Shdr *sh_strtab;
70     const char *strtab;
71     const Elf_Sym *symtab;
72     int symtabct;
73     int i;
74 
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)) {
78 	close(fd);
79 	return -1;
80     }
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)) ==
84       MAP_FAILED)
85 	err(1, "cannot mmap %s", filename);
86     close(fd);
87 
88     base = (const char *)mapbase;
89     shdrs = (const Elf_Shdr *)(base + h.e_shoff);
90 
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)
94 	    break;
95     if (i == h.e_shnum)
96 	errx(1, "%s has no symbol table", filename);
97     sh_symtab = &shdrs[i];
98     sh_strtab = &shdrs[sh_symtab->sh_link];
99 
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);
103 
104     /* Count the symbols that we're interested in. */
105     nname = 0;
106     for (i = 1;  i < symtabct;  i++)
107 	if (wantsym(&symtab[i], strtab))
108 	    nname++;
109 
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");
113 
114     /* Read them in. */
115     npe = nl;
116     for (i = 1;  i < symtabct;  i++) {
117 	const Elf_Sym *sym = &symtab[i];
118 
119 	if (wantsym(sym, strtab)) {
120 	    npe->value = sym->st_value;
121 	    npe->name = strtab + sym->st_name;
122 	    npe++;
123 	}
124     }
125     npe->value = -1;
126 
127     *defaultEs = excludes;
128     return 0;
129 }
130 
131 static bool
132 wantsym(const Elf_Sym *sym, const char *strtab)
133 {
134     int type;
135     int bind;
136 
137     type = ELF_ST_TYPE(sym->st_info);
138     bind = ELF_ST_BIND(sym->st_info);
139 
140     if (type != STT_FUNC ||
141       (aflag && bind == STB_LOCAL) ||
142       (uflag && strchr(strtab + sym->st_name, '.') != NULL))
143 	return 0;
144 
145     return 1;
146 }
147