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