xref: /openbsd/usr.bin/gprof/elf.c (revision 90a99070)
1*90a99070Sart /*-
2*90a99070Sart  * Copyright (c) 1983, 1993
3*90a99070Sart  *	The Regents of the University of California.  All rights reserved.
4*90a99070Sart  *
5*90a99070Sart  * Redistribution and use in source and binary forms, with or without
6*90a99070Sart  * modification, are permitted provided that the following conditions
7*90a99070Sart  * are met:
8*90a99070Sart  * 1. Redistributions of source code must retain the above copyright
9*90a99070Sart  *    notice, this list of conditions and the following disclaimer.
10*90a99070Sart  * 2. Redistributions in binary form must reproduce the above copyright
11*90a99070Sart  *    notice, this list of conditions and the following disclaimer in the
12*90a99070Sart  *    documentation and/or other materials provided with the distribution.
13*90a99070Sart  * 3. All advertising materials mentioning features or use of this software
14*90a99070Sart  *    must display the following acknowledgement:
15*90a99070Sart  *	This product includes software developed by the University of
16*90a99070Sart  *	California, Berkeley and its contributors.
17*90a99070Sart  * 4. Neither the name of the University nor the names of its contributors
18*90a99070Sart  *    may be used to endorse or promote products derived from this software
19*90a99070Sart  *    without specific prior written permission.
20*90a99070Sart  *
21*90a99070Sart  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22*90a99070Sart  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*90a99070Sart  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*90a99070Sart  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25*90a99070Sart  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*90a99070Sart  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*90a99070Sart  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*90a99070Sart  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*90a99070Sart  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*90a99070Sart  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*90a99070Sart  * SUCH DAMAGE.
32*90a99070Sart  */
33*90a99070Sart 
34*90a99070Sart #include <sys/types.h>
35*90a99070Sart #include <sys/mman.h>
36*90a99070Sart #include <sys/stat.h>
37*90a99070Sart #include <sys/exec_elf.h>
38*90a99070Sart 
39*90a99070Sart #include <err.h>
40*90a99070Sart #include <fcntl.h>
41*90a99070Sart #include <string.h>
42*90a99070Sart #include <unistd.h>
43*90a99070Sart 
44*90a99070Sart #include "gprof.h"
45*90a99070Sart 
46*90a99070Sart static bool wantsym(const Elf_Sym *, const char *);
47*90a99070Sart 
48*90a99070Sart /* Things which get -E excluded by default. */
49*90a99070Sart static char	*excludes[] = { ".mcount", "_mcleanup", NULL };
50*90a99070Sart 
51*90a99070Sart int
52*90a99070Sart getnfile(const char *filename, char ***defaultEs)
53*90a99070Sart {
54*90a99070Sart     int fd;
55*90a99070Sart     Elf_Ehdr h;
56*90a99070Sart     struct stat s;
57*90a99070Sart     void *mapbase;
58*90a99070Sart     const char *base;
59*90a99070Sart     const Elf_Shdr *shdrs;
60*90a99070Sart     const Elf_Shdr *sh_symtab;
61*90a99070Sart     const Elf_Shdr *sh_strtab;
62*90a99070Sart     const char *strtab;
63*90a99070Sart     const Elf_Sym *symtab;
64*90a99070Sart     int symtabct;
65*90a99070Sart     int i;
66*90a99070Sart 
67*90a99070Sart     if ((fd = open(filename, O_RDONLY)) == -1)
68*90a99070Sart 	err(1, "%s", filename);
69*90a99070Sart     if (read(fd, &h, sizeof h) != sizeof h || !IS_ELF(h)) {
70*90a99070Sart 	close(fd);
71*90a99070Sart 	return -1;
72*90a99070Sart     }
73*90a99070Sart     if (fstat(fd, &s) == -1)
74*90a99070Sart 	err(1, "Cannot fstat %s", filename);
75*90a99070Sart     if ((mapbase = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
76*90a99070Sart       MAP_FAILED)
77*90a99070Sart 	err(1, "Cannot mmap %s", filename);
78*90a99070Sart     close(fd);
79*90a99070Sart 
80*90a99070Sart     base = (const char *)mapbase;
81*90a99070Sart     shdrs = (const Elf_Shdr *)(base + h.e_shoff);
82*90a99070Sart 
83*90a99070Sart     /* Find the symbol table and associated string table section. */
84*90a99070Sart     for (i = 1;  i < h.e_shnum;  i++)
85*90a99070Sart 	if (shdrs[i].sh_type == SHT_SYMTAB)
86*90a99070Sart 	    break;
87*90a99070Sart     if (i == h.e_shnum)
88*90a99070Sart 	errx(1, "%s has no symbol table", filename);
89*90a99070Sart     sh_symtab = &shdrs[i];
90*90a99070Sart     sh_strtab = &shdrs[sh_symtab->sh_link];
91*90a99070Sart 
92*90a99070Sart     symtab = (const Elf_Sym *)(base + sh_symtab->sh_offset);
93*90a99070Sart     symtabct = sh_symtab->sh_size / sh_symtab->sh_entsize;
94*90a99070Sart     strtab = (const char *)(base + sh_strtab->sh_offset);
95*90a99070Sart 
96*90a99070Sart     /* Count the symbols that we're interested in. */
97*90a99070Sart     nname = 0;
98*90a99070Sart     for (i = 1;  i < symtabct;  i++)
99*90a99070Sart 	if (wantsym(&symtab[i], strtab))
100*90a99070Sart 	    nname++;
101*90a99070Sart 
102*90a99070Sart     /* Allocate memory for them, plus a terminating entry. */
103*90a99070Sart     if ((nl = (nltype *)calloc(nname + 1, sizeof(nltype))) == NULL)
104*90a99070Sart 	errx(1, "Insufficient memory for symbol table");
105*90a99070Sart 
106*90a99070Sart     /* Read them in. */
107*90a99070Sart     npe = nl;
108*90a99070Sart     for (i = 1;  i < symtabct;  i++) {
109*90a99070Sart 	const Elf_Sym *sym = &symtab[i];
110*90a99070Sart 
111*90a99070Sart 	if (wantsym(sym, strtab)) {
112*90a99070Sart 	    npe->value = sym->st_value;
113*90a99070Sart 	    npe->name = strtab + sym->st_name;
114*90a99070Sart 	    npe++;
115*90a99070Sart 	}
116*90a99070Sart     }
117*90a99070Sart     npe->value = -1;
118*90a99070Sart 
119*90a99070Sart     *defaultEs = excludes;
120*90a99070Sart     return 0;
121*90a99070Sart }
122*90a99070Sart 
123*90a99070Sart static bool
124*90a99070Sart wantsym(const Elf_Sym *sym, const char *strtab)
125*90a99070Sart {
126*90a99070Sart     int type;
127*90a99070Sart     int bind;
128*90a99070Sart 
129*90a99070Sart     type = ELF_ST_TYPE(sym->st_info);
130*90a99070Sart     bind = ELF_ST_BIND(sym->st_info);
131*90a99070Sart 
132*90a99070Sart     if (type != STT_FUNC || (aflag && bind == STB_LOCAL))
133*90a99070Sart #if 0
134*90a99070Sart  ||
135*90a99070Sart       (uflag && strchr(strtab + sym->st_name, '.') != NULL))
136*90a99070Sart #endif
137*90a99070Sart 	return 0;
138*90a99070Sart 
139*90a99070Sart     return 1;
140*90a99070Sart }
141