elf.c revision 105243
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 * 3. All advertising materials mentioning features or use of this software 1491017Sbde * must display the following acknowledgement: 1591017Sbde * This product includes software developed by the University of 1691017Sbde * California, Berkeley and its contributors. 1791017Sbde * 4. Neither the name of the University nor the names of its contributors 1891017Sbde * may be used to endorse or promote products derived from this software 1991017Sbde * without specific prior written permission. 2091017Sbde * 2191017Sbde * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2291017Sbde * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2391017Sbde * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2491017Sbde * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2591017Sbde * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2691017Sbde * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2791017Sbde * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2891017Sbde * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2991017Sbde * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3091017Sbde * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3191017Sbde * SUCH DAMAGE. 3291017Sbde */ 3391017Sbde 3491017Sbde#if 0 3591017Sbde/* From: */ 3676224Sobrien#ifndef lint 3791017Sbdestatic char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93"; 3876224Sobrien#endif /* not lint */ 3991017Sbde#endif 4076224Sobrien 4191017Sbde#include <sys/cdefs.h> 4291017Sbde__FBSDID("$FreeBSD: head/usr.bin/gprof/elf.c 105243 2002-10-16 13:50:09Z charnier $"); 4391017Sbde 4438928Sjdp#include <sys/types.h> 4538928Sjdp#include <sys/mman.h> 4638928Sjdp#include <sys/stat.h> 4776224Sobrien#include <machine/elf.h> 4838928Sjdp 4938928Sjdp#include <err.h> 5038928Sjdp#include <fcntl.h> 5138928Sjdp#include <string.h> 5238928Sjdp#include <unistd.h> 5338928Sjdp 5438928Sjdp#include "gprof.h" 5538928Sjdp 5638928Sjdpstatic bool wantsym(const Elf_Sym *, const char *); 5738928Sjdp 5838928Sjdp/* Things which get -E excluded by default. */ 5938928Sjdpstatic char *excludes[] = { ".mcount", "_mcleanup", NULL }; 6038928Sjdp 6138928Sjdpint 6238928Sjdpelf_getnfile(const char *filename, char ***defaultEs) 6338928Sjdp{ 6438928Sjdp int fd; 6538928Sjdp Elf_Ehdr h; 6638928Sjdp struct stat s; 6738928Sjdp void *mapbase; 6838928Sjdp const char *base; 6938928Sjdp const Elf_Shdr *shdrs; 7038928Sjdp const Elf_Shdr *sh_symtab; 7138928Sjdp const Elf_Shdr *sh_strtab; 7238928Sjdp const char *strtab; 7338928Sjdp const Elf_Sym *symtab; 7438928Sjdp int symtabct; 7538928Sjdp int i; 7638928Sjdp 7738928Sjdp if ((fd = open(filename, O_RDONLY)) == -1) 7838928Sjdp err(1, "%s", filename); 7938928Sjdp if (read(fd, &h, sizeof h) != sizeof h || !IS_ELF(h)) { 8038928Sjdp close(fd); 8138928Sjdp return -1; 8238928Sjdp } 8338928Sjdp if (fstat(fd, &s) == -1) 84105243Scharnier err(1, "cannot fstat %s", filename); 8538928Sjdp if ((mapbase = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) == 8638928Sjdp MAP_FAILED) 87105243Scharnier err(1, "cannot mmap %s", filename); 8838928Sjdp close(fd); 8938928Sjdp 9038928Sjdp base = (const char *)mapbase; 9138928Sjdp shdrs = (const Elf_Shdr *)(base + h.e_shoff); 9238928Sjdp 9338928Sjdp /* Find the symbol table and associated string table section. */ 9438928Sjdp for (i = 1; i < h.e_shnum; i++) 9538928Sjdp if (shdrs[i].sh_type == SHT_SYMTAB) 9638928Sjdp break; 9738928Sjdp if (i == h.e_shnum) 9838928Sjdp errx(1, "%s has no symbol table", filename); 9938928Sjdp sh_symtab = &shdrs[i]; 10038928Sjdp sh_strtab = &shdrs[sh_symtab->sh_link]; 10138928Sjdp 10238928Sjdp symtab = (const Elf_Sym *)(base + sh_symtab->sh_offset); 10338928Sjdp symtabct = sh_symtab->sh_size / sh_symtab->sh_entsize; 10438928Sjdp strtab = (const char *)(base + sh_strtab->sh_offset); 10538928Sjdp 10638928Sjdp /* Count the symbols that we're interested in. */ 10738928Sjdp nname = 0; 10838928Sjdp for (i = 1; i < symtabct; i++) 10938928Sjdp if (wantsym(&symtab[i], strtab)) 11038928Sjdp nname++; 11138928Sjdp 11238928Sjdp /* Allocate memory for them, plus a terminating entry. */ 11338928Sjdp if ((nl = (nltype *)calloc(nname + 1, sizeof(nltype))) == NULL) 114105243Scharnier errx(1, "insufficient memory for symbol table"); 11538928Sjdp 11638928Sjdp /* Read them in. */ 11738928Sjdp npe = nl; 11838928Sjdp for (i = 1; i < symtabct; i++) { 11938928Sjdp const Elf_Sym *sym = &symtab[i]; 12038928Sjdp 12138928Sjdp if (wantsym(sym, strtab)) { 12238928Sjdp npe->value = sym->st_value; 12338928Sjdp npe->name = strtab + sym->st_name; 12438928Sjdp npe++; 12538928Sjdp } 12638928Sjdp } 12738928Sjdp npe->value = -1; 12838928Sjdp 12938928Sjdp *defaultEs = excludes; 13038928Sjdp return 0; 13138928Sjdp} 13238928Sjdp 13338928Sjdpstatic bool 13438928Sjdpwantsym(const Elf_Sym *sym, const char *strtab) 13538928Sjdp{ 13638928Sjdp int type; 13738928Sjdp int bind; 13838928Sjdp 13938928Sjdp type = ELF_ST_TYPE(sym->st_info); 14038928Sjdp bind = ELF_ST_BIND(sym->st_info); 14138928Sjdp 14238928Sjdp if (type != STT_FUNC || 14338928Sjdp (aflag && bind == STB_LOCAL) || 14438928Sjdp (uflag && strchr(strtab + sym->st_name, '.') != NULL)) 14538928Sjdp return 0; 14638928Sjdp 14738928Sjdp return 1; 14838928Sjdp} 149