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 * 4. 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#if 0 31/* From: */ 32#ifndef lint 33static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93"; 34#endif /* not lint */ 35#endif 36 37#include <sys/cdefs.h> 38__FBSDID("$FreeBSD$"); 39 40#include <netinet/in.h> 41 42#include <a.out.h> 43#include <err.h> 44#include <string.h> 45 46#include "gprof.h" 47 48static void getstrtab(FILE *, const char *); 49static void getsymtab(FILE *, const char *); 50static void gettextspace(FILE *); 51static bool funcsymbol(struct nlist *); 52 53static char *strtab; /* string table in core */ 54static long ssiz; /* size of the string table */ 55static struct exec xbuf; /* exec header of a.out */ 56 57/* Things which get -E excluded by default. */ 58static char *excludes[] = { "mcount", "__mcleanup", NULL }; 59 60 /* 61 * Set up string and symbol tables from a.out. 62 * and optionally the text space. 63 * On return symbol table is sorted by value. 64 * 65 * Returns 0 on success, -1 on failure. 66 */ 67int 68aout_getnfile(const char *filename, char ***defaultEs) 69{ 70 FILE *nfile; 71 int valcmp(); 72 73 nfile = fopen( filename ,"r"); 74 if (nfile == NULL) 75 err( 1 , "%s", filename ); 76 fread(&xbuf, 1, sizeof(xbuf), nfile); 77 if (N_BADMAG(xbuf)) { 78 fclose(nfile); 79 return -1; 80 } 81 getstrtab(nfile, filename); 82 getsymtab(nfile, filename); 83 gettextspace( nfile ); 84 fclose(nfile); 85# ifdef DEBUG 86 if ( debug & AOUTDEBUG ) { 87 register int j; 88 89 for (j = 0; j < nname; j++){ 90 printf("[getnfile] 0X%08lx\t%s\n", nl[j].value, nl[j].name); 91 } 92 } 93# endif /* DEBUG */ 94 *defaultEs = excludes; 95 return 0; 96} 97 98static void 99getstrtab(FILE *nfile, const char *filename) 100{ 101 102 fseek(nfile, (long)(N_SYMOFF(xbuf) + xbuf.a_syms), 0); 103 if (fread(&ssiz, sizeof (ssiz), 1, nfile) == 0) 104 errx( 1 , "%s: no string table (old format?)" , filename ); 105 strtab = calloc(ssiz, 1); 106 if (strtab == NULL) 107 errx( 1 , "%s: no room for %ld bytes of string table", filename , ssiz); 108 if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1) 109 errx( 1 , "%s: error reading string table" , filename ); 110} 111 112 /* 113 * Read in symbol table 114 */ 115static void 116getsymtab(FILE *nfile, const char *filename) 117{ 118 register long i; 119 int askfor; 120 struct nlist nbuf; 121 122 /* pass1 - count symbols */ 123 fseek(nfile, (long)N_SYMOFF(xbuf), 0); 124 nname = 0; 125 for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) { 126 fread(&nbuf, sizeof(nbuf), 1, nfile); 127 if ( ! funcsymbol( &nbuf ) ) { 128 continue; 129 } 130 nname++; 131 } 132 if (nname == 0) 133 errx( 1 , "%s: no symbols" , filename ); 134 askfor = nname + 1; 135 nl = (nltype *) calloc( askfor , sizeof(nltype) ); 136 if (nl == 0) 137 errx( 1 , "no room for %zu bytes of symbol table" , 138 askfor * sizeof(nltype) ); 139 140 /* pass2 - read symbols */ 141 fseek(nfile, (long)N_SYMOFF(xbuf), 0); 142 npe = nl; 143 nname = 0; 144 for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) { 145 fread(&nbuf, sizeof(nbuf), 1, nfile); 146 if ( ! funcsymbol( &nbuf ) ) { 147# ifdef DEBUG 148 if ( debug & AOUTDEBUG ) { 149 printf( "[getsymtab] rejecting: 0x%x %s\n" , 150 nbuf.n_type , strtab + nbuf.n_un.n_strx ); 151 } 152# endif /* DEBUG */ 153 continue; 154 } 155 npe->value = nbuf.n_value; 156 npe->name = strtab+nbuf.n_un.n_strx; 157# ifdef DEBUG 158 if ( debug & AOUTDEBUG ) { 159 printf( "[getsymtab] %d %s 0x%08lx\n" , 160 nname , npe -> name , npe -> value ); 161 } 162# endif /* DEBUG */ 163 npe++; 164 nname++; 165 } 166 npe->value = -1; 167} 168 169 /* 170 * read in the text space of an a.out file 171 */ 172static void 173gettextspace(FILE *nfile) 174{ 175 176 textspace = (u_char *) malloc( xbuf.a_text ); 177 if ( textspace == 0 ) { 178 warnx("no room for %u bytes of text space: can't do -c" , 179 xbuf.a_text ); 180 return; 181 } 182 (void) fseek( nfile , N_TXTOFF( xbuf ) , 0 ); 183 if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) { 184 warnx("couldn't read text space: can't do -c"); 185 free( textspace ); 186 textspace = 0; 187 return; 188 } 189} 190 191static bool 192funcsymbol(struct nlist *nlistp) 193{ 194 char *name, c; 195 196 /* 197 * must be a text symbol, 198 * and static text symbols don't qualify if aflag set. 199 */ 200 if ( ! ( ( nlistp -> n_type == ( N_TEXT | N_EXT ) ) 201 || ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) { 202 return FALSE; 203 } 204 /* 205 * name must start with an underscore if uflag is set. 206 * can't have any `funny' characters in name, 207 * where `funny' means `.' (.o file names) 208 * need to make an exception for sparc .mul & co. 209 * perhaps we should just drop this code entirely... 210 */ 211 name = strtab + nlistp -> n_un.n_strx; 212 if ( uflag && *name != '_' ) 213 return FALSE; 214#ifdef sparc 215 if ( *name == '.' ) { 216 char *p = name + 1; 217 if ( *p == 'u' ) 218 p++; 219 if ( strcmp ( p, "mul" ) == 0 || strcmp ( p, "div" ) == 0 || 220 strcmp ( p, "rem" ) == 0 ) 221 return TRUE; 222 } 223#endif 224 while ( (c = *name++) ) { 225 if ( c == '.' ) { 226 return FALSE; 227 } 228 } 229 return TRUE; 230} 231