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