[BACK]Return to elf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / gprof

Annotation of src/usr.bin/gprof/elf.c, Revision 1.1

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