GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/gprof/elf.c Lines: 0 43 0.0 %
Date: 2017-11-07 Branches: 0 36 0.0 %

Line Branch Exec Source
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
 * 3. 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
#include <sys/types.h>
31
#include <sys/mman.h>
32
#include <sys/stat.h>
33
#include <sys/exec_elf.h>
34
35
#include <err.h>
36
#include <fcntl.h>
37
#include <string.h>
38
#include <unistd.h>
39
40
#include "gprof.h"
41
42
static bool wantsym(const Elf_Sym *, const char *);
43
44
/* Things which get -E excluded by default. */
45
static char	*excludes[] = { ".mcount", "_mcleanup", NULL };
46
47
int
48
getnfile(const char *filename, char ***defaultEs)
49
{
50
    int fd;
51
    Elf_Ehdr h;
52
    struct stat s;
53
    void *mapbase;
54
    const char *base;
55
    const Elf_Shdr *shdrs;
56
    const Elf_Shdr *sh_symtab;
57
    const Elf_Shdr *sh_strtab;
58
    const char *strtab;
59
    const Elf_Sym *symtab;
60
    int symtabct;
61
    int i;
62
63
    if ((fd = open(filename, O_RDONLY)) == -1)
64
	err(1, "%s", filename);
65
    if (read(fd, &h, sizeof h) != sizeof h || !IS_ELF(h)) {
66
	close(fd);
67
	return -1;
68
    }
69
    if (fstat(fd, &s) == -1)
70
	err(1, "Cannot fstat %s", filename);
71
    if ((mapbase = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
72
      MAP_FAILED)
73
	err(1, "Cannot mmap %s", filename);
74
    close(fd);
75
76
    base = (const char *)mapbase;
77
    shdrs = (const Elf_Shdr *)(base + h.e_shoff);
78
79
    /* Find the symbol table and associated string table section. */
80
    for (i = 1;  i < h.e_shnum;  i++)
81
	if (shdrs[i].sh_type == SHT_SYMTAB)
82
	    break;
83
    if (i == h.e_shnum)
84
	errx(1, "%s has no symbol table", filename);
85
    sh_symtab = &shdrs[i];
86
    sh_strtab = &shdrs[sh_symtab->sh_link];
87
88
    symtab = (const Elf_Sym *)(base + sh_symtab->sh_offset);
89
    symtabct = sh_symtab->sh_size / sh_symtab->sh_entsize;
90
    strtab = (const char *)(base + sh_strtab->sh_offset);
91
92
    /* Count the symbols that we're interested in. */
93
    nname = 0;
94
    for (i = 1;  i < symtabct;  i++)
95
	if (wantsym(&symtab[i], strtab))
96
	    nname++;
97
98
#ifdef DEBUG
99
    if (debug & ELFDEBUG) {
100
	    printf("[getnfile] symtab at %p, strtab at %p\n", symtab, strtab);
101
	    printf("[getnfile] %d of %d symbols wanted\n", nname, symtabct);
102
    }
103
#endif
104
105
    /* Allocate memory for them, plus a terminating entry. */
106
    if ((nl = calloc(nname + 1, sizeof(nltype))) == NULL)
107
	errx(1, "Insufficient memory for symbol table");
108
109
    /* Read them in. */
110
    npe = nl;
111
    for (i = 1;  i < symtabct;  i++) {
112
	const Elf_Sym *sym = &symtab[i];
113
114
	if (wantsym(sym, strtab)) {
115
	    npe->value = sym->st_value;
116
	    npe->name = strtab + sym->st_name;
117
#ifdef DEBUG
118
	    if (debug & ELFDEBUG)
119
		    printf("[getnfile] symbol %d: %s -> %lx\n", i,
120
			   npe->name ? npe->name : "(none)", npe->value);
121
#endif
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 || (aflag && bind == STB_LOCAL))
141
#if 0
142
 ||
143
      (uflag && strchr(strtab + sym->st_name, '.') != NULL))
144
#endif
145
	return 0;
146
147
#ifdef __arm__
148
    /* ignore what gas calls "mapping symbols" */
149
    {
150
	const char *c = strtab + sym->st_name;
151
	if (c[0] == '$')
152
	    return 0;
153
    }
154
#endif
155
156
    return 1;
157
}