191017Sbde/*-
291017Sbde * Copyright (c) 1983, 1993
391017Sbde *	The Regents of the University of California.  All rights reserved.
491017Sbde *
591017Sbde * Redistribution and use in source and binary forms, with or without
691017Sbde * modification, are permitted provided that the following conditions
791017Sbde * are met:
891017Sbde * 1. Redistributions of source code must retain the above copyright
991017Sbde *    notice, this list of conditions and the following disclaimer.
1091017Sbde * 2. Redistributions in binary form must reproduce the above copyright
1191017Sbde *    notice, this list of conditions and the following disclaimer in the
1291017Sbde *    documentation and/or other materials provided with the distribution.
1391017Sbde * 4. Neither the name of the University nor the names of its contributors
1491017Sbde *    may be used to endorse or promote products derived from this software
1591017Sbde *    without specific prior written permission.
1691017Sbde *
1791017Sbde * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1891017Sbde * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1991017Sbde * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2091017Sbde * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2191017Sbde * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2291017Sbde * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2391017Sbde * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2491017Sbde * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2591017Sbde * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2691017Sbde * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2791017Sbde * SUCH DAMAGE.
2891017Sbde */
2991017Sbde
3091017Sbde#if 0
3191017Sbde/* From: */
3276224Sobrien#ifndef lint
3391017Sbdestatic char sccsid[] = "@(#)gprof.c	8.1 (Berkeley) 6/6/93";
3476224Sobrien#endif /* not lint */
3591017Sbde#endif
3676224Sobrien
3791017Sbde#include <sys/cdefs.h>
3891017Sbde__FBSDID("$FreeBSD: releng/10.3/usr.bin/gprof/elf.c 216370 2010-12-11 08:32:16Z joel $");
3991017Sbde
4038928Sjdp#include <sys/types.h>
4138928Sjdp#include <sys/mman.h>
4238928Sjdp#include <sys/stat.h>
4376224Sobrien#include <machine/elf.h>
4438928Sjdp
4538928Sjdp#include <err.h>
4638928Sjdp#include <fcntl.h>
4738928Sjdp#include <string.h>
4838928Sjdp#include <unistd.h>
4938928Sjdp
5038928Sjdp#include "gprof.h"
5138928Sjdp
5238928Sjdpstatic bool wantsym(const Elf_Sym *, const char *);
5338928Sjdp
5438928Sjdp/* Things which get -E excluded by default. */
5538928Sjdpstatic char	*excludes[] = { ".mcount", "_mcleanup", NULL };
5638928Sjdp
5738928Sjdpint
5838928Sjdpelf_getnfile(const char *filename, char ***defaultEs)
5938928Sjdp{
6038928Sjdp    int fd;
6138928Sjdp    Elf_Ehdr h;
6238928Sjdp    struct stat s;
6338928Sjdp    void *mapbase;
6438928Sjdp    const char *base;
6538928Sjdp    const Elf_Shdr *shdrs;
6638928Sjdp    const Elf_Shdr *sh_symtab;
6738928Sjdp    const Elf_Shdr *sh_strtab;
6838928Sjdp    const char *strtab;
6938928Sjdp    const Elf_Sym *symtab;
7038928Sjdp    int symtabct;
7138928Sjdp    int i;
7238928Sjdp
7338928Sjdp    if ((fd = open(filename, O_RDONLY)) == -1)
7438928Sjdp	err(1, "%s", filename);
7538928Sjdp    if (read(fd, &h, sizeof h) != sizeof h || !IS_ELF(h)) {
7638928Sjdp	close(fd);
7738928Sjdp	return -1;
7838928Sjdp    }
7938928Sjdp    if (fstat(fd, &s) == -1)
80105243Scharnier	err(1, "cannot fstat %s", filename);
8138928Sjdp    if ((mapbase = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
8238928Sjdp      MAP_FAILED)
83105243Scharnier	err(1, "cannot mmap %s", filename);
8438928Sjdp    close(fd);
8538928Sjdp
8638928Sjdp    base = (const char *)mapbase;
8738928Sjdp    shdrs = (const Elf_Shdr *)(base + h.e_shoff);
8838928Sjdp
8938928Sjdp    /* Find the symbol table and associated string table section. */
9038928Sjdp    for (i = 1;  i < h.e_shnum;  i++)
9138928Sjdp	if (shdrs[i].sh_type == SHT_SYMTAB)
9238928Sjdp	    break;
9338928Sjdp    if (i == h.e_shnum)
9438928Sjdp	errx(1, "%s has no symbol table", filename);
9538928Sjdp    sh_symtab = &shdrs[i];
9638928Sjdp    sh_strtab = &shdrs[sh_symtab->sh_link];
9738928Sjdp
9838928Sjdp    symtab = (const Elf_Sym *)(base + sh_symtab->sh_offset);
9938928Sjdp    symtabct = sh_symtab->sh_size / sh_symtab->sh_entsize;
10038928Sjdp    strtab = (const char *)(base + sh_strtab->sh_offset);
10138928Sjdp
10238928Sjdp    /* Count the symbols that we're interested in. */
10338928Sjdp    nname = 0;
10438928Sjdp    for (i = 1;  i < symtabct;  i++)
10538928Sjdp	if (wantsym(&symtab[i], strtab))
10638928Sjdp	    nname++;
10738928Sjdp
10838928Sjdp    /* Allocate memory for them, plus a terminating entry. */
10938928Sjdp    if ((nl = (nltype *)calloc(nname + 1, sizeof(nltype))) == NULL)
110105243Scharnier	errx(1, "insufficient memory for symbol table");
11138928Sjdp
11238928Sjdp    /* Read them in. */
11338928Sjdp    npe = nl;
11438928Sjdp    for (i = 1;  i < symtabct;  i++) {
11538928Sjdp	const Elf_Sym *sym = &symtab[i];
11638928Sjdp
11738928Sjdp	if (wantsym(sym, strtab)) {
11838928Sjdp	    npe->value = sym->st_value;
11938928Sjdp	    npe->name = strtab + sym->st_name;
12038928Sjdp	    npe++;
12138928Sjdp	}
12238928Sjdp    }
12338928Sjdp    npe->value = -1;
12438928Sjdp
12538928Sjdp    *defaultEs = excludes;
12638928Sjdp    return 0;
12738928Sjdp}
12838928Sjdp
12938928Sjdpstatic bool
13038928Sjdpwantsym(const Elf_Sym *sym, const char *strtab)
13138928Sjdp{
13238928Sjdp    int type;
13338928Sjdp    int bind;
13438928Sjdp
13538928Sjdp    type = ELF_ST_TYPE(sym->st_info);
13638928Sjdp    bind = ELF_ST_BIND(sym->st_info);
13738928Sjdp
13838928Sjdp    if (type != STT_FUNC ||
13938928Sjdp      (aflag && bind == STB_LOCAL) ||
14038928Sjdp      (uflag && strchr(strtab + sym->st_name, '.') != NULL))
14138928Sjdp	return 0;
14238928Sjdp
14338928Sjdp    return 1;
14438928Sjdp}
145