xref: /freebsd/usr.bin/gprof/elf.c (revision aa0a1e58)
1 /*-
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 /* From: */
32 #ifndef lint
33 static char sccsid[] = "@(#)gprof.c	8.1 (Berkeley) 6/6/93";
34 #endif /* not lint */
35 #endif
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/types.h>
41 #include <sys/mman.h>
42 #include <sys/stat.h>
43 #include <machine/elf.h>
44 
45 #include <err.h>
46 #include <fcntl.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include "gprof.h"
51 
52 static bool wantsym(const Elf_Sym *, const char *);
53 
54 /* Things which get -E excluded by default. */
55 static char	*excludes[] = { ".mcount", "_mcleanup", NULL };
56 
57 int
58 elf_getnfile(const char *filename, char ***defaultEs)
59 {
60     int fd;
61     Elf_Ehdr h;
62     struct stat s;
63     void *mapbase;
64     const char *base;
65     const Elf_Shdr *shdrs;
66     const Elf_Shdr *sh_symtab;
67     const Elf_Shdr *sh_strtab;
68     const char *strtab;
69     const Elf_Sym *symtab;
70     int symtabct;
71     int i;
72 
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)) {
76 	close(fd);
77 	return -1;
78     }
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)) ==
82       MAP_FAILED)
83 	err(1, "cannot mmap %s", filename);
84     close(fd);
85 
86     base = (const char *)mapbase;
87     shdrs = (const Elf_Shdr *)(base + h.e_shoff);
88 
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)
92 	    break;
93     if (i == h.e_shnum)
94 	errx(1, "%s has no symbol table", filename);
95     sh_symtab = &shdrs[i];
96     sh_strtab = &shdrs[sh_symtab->sh_link];
97 
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);
101 
102     /* Count the symbols that we're interested in. */
103     nname = 0;
104     for (i = 1;  i < symtabct;  i++)
105 	if (wantsym(&symtab[i], strtab))
106 	    nname++;
107 
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");
111 
112     /* Read them in. */
113     npe = nl;
114     for (i = 1;  i < symtabct;  i++) {
115 	const Elf_Sym *sym = &symtab[i];
116 
117 	if (wantsym(sym, strtab)) {
118 	    npe->value = sym->st_value;
119 	    npe->name = strtab + sym->st_name;
120 	    npe++;
121 	}
122     }
123     npe->value = -1;
124 
125     *defaultEs = excludes;
126     return 0;
127 }
128 
129 static bool
130 wantsym(const Elf_Sym *sym, const char *strtab)
131 {
132     int type;
133     int bind;
134 
135     type = ELF_ST_TYPE(sym->st_info);
136     bind = ELF_ST_BIND(sym->st_info);
137 
138     if (type != STT_FUNC ||
139       (aflag && bind == STB_LOCAL) ||
140       (uflag && strchr(strtab + sym->st_name, '.') != NULL))
141 	return 0;
142 
143     return 1;
144 }
145