]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/gprof/aout.c
ping(8): Fix a mandoc related issue
[FreeBSD/FreeBSD.git] / usr.bin / gprof / aout.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #if 0
33 /* From: */
34 #ifndef lint
35 static char sccsid[] = "@(#)gprof.c     8.1 (Berkeley) 6/6/93";
36 #endif /* not lint */
37 #endif
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <netinet/in.h>
43
44 #include <a.out.h>
45 #include <err.h>
46 #include <string.h>
47
48 #include "gprof.h"
49
50 static void getstrtab(FILE *, const char *);
51 static void getsymtab(FILE *, const char *);
52 static void gettextspace(FILE *);
53 static bool funcsymbol(struct nlist *);
54
55 static char     *strtab;                /* string table in core */
56 static long     ssiz;                   /* size of the string table */
57 static struct   exec xbuf;              /* exec header of a.out */
58
59 /* Things which get -E excluded by default. */
60 static char     *excludes[] = { "mcount", "__mcleanup", NULL };
61
62     /*
63      * Set up string and symbol tables from a.out.
64      *  and optionally the text space.
65      * On return symbol table is sorted by value.
66      *
67      * Returns 0 on success, -1 on failure.
68      */
69 int
70 aout_getnfile(const char *filename, char ***defaultEs)
71 {
72     FILE        *nfile;
73
74     nfile = fopen( filename ,"r");
75     if (nfile == NULL)
76         err( 1 , "%s", filename );
77     fread(&xbuf, 1, sizeof(xbuf), nfile);
78     if (N_BADMAG(xbuf)) {
79         fclose(nfile);
80         return -1;
81     }
82     getstrtab(nfile, filename);
83     getsymtab(nfile, filename);
84     gettextspace( nfile );
85     fclose(nfile);
86 #   ifdef DEBUG
87         if ( debug & AOUTDEBUG ) {
88             register int j;
89
90             for (j = 0; j < nname; j++){
91                 printf("[getnfile] 0X%08lx\t%s\n", nl[j].value, nl[j].name);
92             }
93         }
94 #   endif /* DEBUG */
95     *defaultEs = excludes;
96     return 0;
97 }
98
99 static void
100 getstrtab(FILE *nfile, const char *filename)
101 {
102
103     fseek(nfile, (long)(N_SYMOFF(xbuf) + xbuf.a_syms), 0);
104     if (fread(&ssiz, sizeof (ssiz), 1, nfile) == 0)
105         errx( 1 , "%s: no string table (old format?)" , filename );
106     strtab = calloc(ssiz, 1);
107     if (strtab == NULL)
108         errx( 1 , "%s: no room for %ld bytes of string table", filename , ssiz);
109     if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1)
110         errx( 1 , "%s: error reading string table" , filename );
111 }
112
113     /*
114      * Read in symbol table
115      */
116 static void
117 getsymtab(FILE *nfile, const char *filename)
118 {
119     register long       i;
120     int                 askfor;
121     struct nlist        nbuf;
122
123     /* pass1 - count symbols */
124     fseek(nfile, (long)N_SYMOFF(xbuf), 0);
125     nname = 0;
126     for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
127         fread(&nbuf, sizeof(nbuf), 1, nfile);
128         if ( ! funcsymbol( &nbuf ) ) {
129             continue;
130         }
131         nname++;
132     }
133     if (nname == 0)
134         errx( 1 , "%s: no symbols" , filename );
135     askfor = nname + 1;
136     nl = (nltype *) calloc( askfor , sizeof(nltype) );
137     if (nl == NULL)
138         errx( 1 , "no room for %zu bytes of symbol table" ,
139                 askfor * sizeof(nltype) );
140
141     /* pass2 - read symbols */
142     fseek(nfile, (long)N_SYMOFF(xbuf), 0);
143     npe = nl;
144     nname = 0;
145     for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
146         fread(&nbuf, sizeof(nbuf), 1, nfile);
147         if ( ! funcsymbol( &nbuf ) ) {
148 #           ifdef DEBUG
149                 if ( debug & AOUTDEBUG ) {
150                     printf( "[getsymtab] rejecting: 0x%x %s\n" ,
151                             nbuf.n_type , strtab + nbuf.n_un.n_strx );
152                 }
153 #           endif /* DEBUG */
154             continue;
155         }
156         npe->value = nbuf.n_value;
157         npe->name = strtab+nbuf.n_un.n_strx;
158 #       ifdef DEBUG
159             if ( debug & AOUTDEBUG ) {
160                 printf( "[getsymtab] %d %s 0x%08lx\n" ,
161                         nname , npe -> name , npe -> value );
162             }
163 #       endif /* DEBUG */
164         npe++;
165         nname++;
166     }
167     npe->value = -1;
168 }
169
170     /*
171      *  read in the text space of an a.out file
172      */
173 static void
174 gettextspace(FILE *nfile)
175 {
176
177     textspace = (u_char *) malloc( xbuf.a_text );
178     if ( textspace == NULL ) {
179         warnx("no room for %u bytes of text space: can't do -c" ,
180                   xbuf.a_text );
181         return;
182     }
183     (void) fseek( nfile , N_TXTOFF( xbuf ) , 0 );
184     if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) {
185         warnx("couldn't read text space: can't do -c");
186         free( textspace );
187         textspace = 0;
188         return;
189     }
190 }
191
192 static bool
193 funcsymbol(struct nlist *nlistp)
194 {
195     char        *name, c;
196
197         /*
198          *      must be a text symbol,
199          *      and static text symbols don't qualify if aflag set.
200          */
201     if ( ! (  ( nlistp -> n_type == ( N_TEXT | N_EXT ) )
202            || ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) {
203         return FALSE;
204     }
205         /*
206          *      name must start with an underscore if uflag is set.
207          *      can't have any `funny' characters in name,
208          *      where `funny' means `.' (.o file names)
209          *      need to make an exception for sparc .mul & co.
210          *      perhaps we should just drop this code entirely...
211          */
212     name = strtab + nlistp -> n_un.n_strx;
213     if ( uflag && *name != '_' )
214         return FALSE;
215 #ifdef sparc
216     if ( *name == '.' ) {
217         char *p = name + 1;
218         if ( *p == 'u' )
219             p++;
220         if ( strcmp ( p, "mul" ) == 0 || strcmp ( p, "div" ) == 0 ||
221              strcmp ( p, "rem" ) == 0 )
222                 return TRUE;
223     }
224 #endif
225     while ( (c = *name++) ) {
226         if ( c == '.' ) {
227             return FALSE;
228         }
229     }
230     return TRUE;
231 }