2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <netinet/in.h>
48 static void getstrtab(FILE *, const char *);
49 static void getsymtab(FILE *, const char *);
50 static void gettextspace(FILE *);
51 static bool funcsymbol(struct nlist *);
53 static char *strtab; /* string table in core */
54 static long ssiz; /* size of the string table */
55 static struct exec xbuf; /* exec header of a.out */
57 /* Things which get -E excluded by default. */
58 static char *excludes[] = { "mcount", "__mcleanup", NULL };
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.
65 * Returns 0 on success, -1 on failure.
68 aout_getnfile(const char *filename, char ***defaultEs)
72 nfile = fopen( filename ,"r");
74 err( 1 , "%s", filename );
75 fread(&xbuf, 1, sizeof(xbuf), nfile);
80 getstrtab(nfile, filename);
81 getsymtab(nfile, filename);
82 gettextspace( nfile );
85 if ( debug & AOUTDEBUG ) {
88 for (j = 0; j < nname; j++){
89 printf("[getnfile] 0X%08lx\t%s\n", nl[j].value, nl[j].name);
93 *defaultEs = excludes;
98 getstrtab(FILE *nfile, const char *filename)
101 fseek(nfile, (long)(N_SYMOFF(xbuf) + xbuf.a_syms), 0);
102 if (fread(&ssiz, sizeof (ssiz), 1, nfile) == 0)
103 errx( 1 , "%s: no string table (old format?)" , filename );
104 strtab = calloc(ssiz, 1);
106 errx( 1 , "%s: no room for %ld bytes of string table", filename , ssiz);
107 if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1)
108 errx( 1 , "%s: error reading string table" , filename );
112 * Read in symbol table
115 getsymtab(FILE *nfile, const char *filename)
121 /* pass1 - count symbols */
122 fseek(nfile, (long)N_SYMOFF(xbuf), 0);
124 for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
125 fread(&nbuf, sizeof(nbuf), 1, nfile);
126 if ( ! funcsymbol( &nbuf ) ) {
132 errx( 1 , "%s: no symbols" , filename );
134 nl = (nltype *) calloc( askfor , sizeof(nltype) );
136 errx( 1 , "no room for %zu bytes of symbol table" ,
137 askfor * sizeof(nltype) );
139 /* pass2 - read symbols */
140 fseek(nfile, (long)N_SYMOFF(xbuf), 0);
143 for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
144 fread(&nbuf, sizeof(nbuf), 1, nfile);
145 if ( ! funcsymbol( &nbuf ) ) {
147 if ( debug & AOUTDEBUG ) {
148 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
149 nbuf.n_type , strtab + nbuf.n_un.n_strx );
154 npe->value = nbuf.n_value;
155 npe->name = strtab+nbuf.n_un.n_strx;
157 if ( debug & AOUTDEBUG ) {
158 printf( "[getsymtab] %d %s 0x%08lx\n" ,
159 nname , npe -> name , npe -> value );
169 * read in the text space of an a.out file
172 gettextspace(FILE *nfile)
175 textspace = (u_char *) malloc( xbuf.a_text );
176 if ( textspace == NULL ) {
177 warnx("no room for %u bytes of text space: can't do -c" ,
181 (void) fseek( nfile , N_TXTOFF( xbuf ) , 0 );
182 if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) {
183 warnx("couldn't read text space: can't do -c");
191 funcsymbol(struct nlist *nlistp)
196 * must be a text symbol,
197 * and static text symbols don't qualify if aflag set.
199 if ( ! ( ( nlistp -> n_type == ( N_TEXT | N_EXT ) )
200 || ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) {
204 * name must start with an underscore if uflag is set.
205 * can't have any `funny' characters in name,
206 * where `funny' means `.' (.o file names)
207 * need to make an exception for sparc .mul & co.
208 * perhaps we should just drop this code entirely...
210 name = strtab + nlistp -> n_un.n_strx;
211 if ( uflag && *name != '_' )
214 if ( *name == '.' ) {
218 if ( strcmp ( p, "mul" ) == 0 || strcmp ( p, "div" ) == 0 ||
219 strcmp ( p, "rem" ) == 0 )
223 while ( (c = *name++) ) {