xref: /freebsd/usr.bin/gprof/elf.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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/types.h>
37 #include <sys/mman.h>
38 #include <sys/stat.h>
39 #include <machine/elf.h>
40 
41 #include <err.h>
42 #include <fcntl.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "gprof.h"
47 
48 static bool wantsym(const Elf_Sym *, const char *);
49 
50 /* Things which get -E excluded by default. */
51 static char	*excludes[] = { ".mcount", "_mcleanup", NULL };
52 
53 int
54 elf_getnfile(const char *filename, char ***defaultEs)
55 {
56     int fd;
57     Elf_Ehdr h;
58     struct stat s;
59     void *mapbase;
60     const char *base;
61     const Elf_Shdr *shdrs;
62     const Elf_Shdr *sh_symtab;
63     const Elf_Shdr *sh_strtab;
64     const char *strtab;
65     const Elf_Sym *symtab;
66     int symtabct;
67     int i;
68 
69     if ((fd = open(filename, O_RDONLY)) == -1)
70 	err(1, "%s", filename);
71     if (read(fd, &h, sizeof h) != sizeof h || !IS_ELF(h)) {
72 	close(fd);
73 	return -1;
74     }
75     if (fstat(fd, &s) == -1)
76 	err(1, "cannot fstat %s", filename);
77     if ((mapbase = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
78       MAP_FAILED)
79 	err(1, "cannot mmap %s", filename);
80     close(fd);
81 
82     base = (const char *)mapbase;
83     shdrs = (const Elf_Shdr *)(base + h.e_shoff);
84 
85     /* Find the symbol table and associated string table section. */
86     for (i = 1;  i < h.e_shnum;  i++)
87 	if (shdrs[i].sh_type == SHT_SYMTAB)
88 	    break;
89     if (i == h.e_shnum)
90 	errx(1, "%s has no symbol table", filename);
91     sh_symtab = &shdrs[i];
92     sh_strtab = &shdrs[sh_symtab->sh_link];
93 
94     symtab = (const Elf_Sym *)(base + sh_symtab->sh_offset);
95     symtabct = sh_symtab->sh_size / sh_symtab->sh_entsize;
96     strtab = (const char *)(base + sh_strtab->sh_offset);
97 
98     /* Count the symbols that we're interested in. */
99     nname = 0;
100     for (i = 1;  i < symtabct;  i++)
101 	if (wantsym(&symtab[i], strtab))
102 	    nname++;
103 
104     /* Allocate memory for them, plus a terminating entry. */
105     if ((nl = (nltype *)calloc(nname + 1, sizeof(nltype))) == NULL)
106 	errx(1, "insufficient memory for symbol table");
107 
108     /* Read them in. */
109     npe = nl;
110     for (i = 1;  i < symtabct;  i++) {
111 	const Elf_Sym *sym = &symtab[i];
112 
113 	if (wantsym(sym, strtab)) {
114 	    npe->value = sym->st_value;
115 	    npe->name = strtab + sym->st_name;
116 	    npe++;
117 	}
118     }
119     npe->value = -1;
120 
121     *defaultEs = excludes;
122     return 0;
123 }
124 
125 static bool
126 wantsym(const Elf_Sym *sym, const char *strtab)
127 {
128     int type;
129     int bind;
130 
131     type = ELF_ST_TYPE(sym->st_info);
132     bind = ELF_ST_BIND(sym->st_info);
133 
134     if (type != STT_FUNC ||
135       (aflag && bind == STB_LOCAL) ||
136       (uflag && strchr(strtab + sym->st_name, '.') != NULL))
137 	return 0;
138 
139     return 1;
140 }
141