Clean up some register keyword usage.
[dragonfly.git] / usr.bin / gprof / aout.c
1 /* $DragonFly: src/usr.bin/gprof/aout.c,v 1.2 2005/04/10 20:55:38 drhodus Exp $                                                         */
2 #include <a.out.h>
3
4 #include "gprof.h"
5
6 static void getstrtab(FILE *, const char *);
7 static void getsymtab(FILE *, const char *);
8 static void gettextspace(FILE *);
9 static bool funcsymbol(struct nlist *);
10
11 static char     *strtab;                /* string table in core */
12 static long     ssiz;                   /* size of the string table */
13 static struct   exec xbuf;              /* exec header of a.out */
14
15 /* Things which get -E excluded by default. */
16 static char     *excludes[] = { "mcount", "__mcleanup", NULL };
17
18     /*
19      * Set up string and symbol tables from a.out.
20      *  and optionally the text space.
21      * On return symbol table is sorted by value.
22      *
23      * Returns 0 on success, -1 on failure.
24      */
25 int
26 aout_getnfile(const char *filename, char ***defaultEs)
27 {
28     FILE        *nfile;
29     int         valcmp();
30
31     nfile = fopen( filename ,"r");
32     if (nfile == NULL) {
33         perror( filename );
34         done();
35     }
36     fread(&xbuf, 1, sizeof(xbuf), nfile);
37     if (N_BADMAG(xbuf)) {
38         fclose(nfile);
39         return -1;
40     }
41     getstrtab(nfile, filename);
42     getsymtab(nfile, filename);
43     gettextspace( nfile );
44     fclose(nfile);
45 #   ifdef DEBUG
46         if ( debug & AOUTDEBUG ) {
47             int j;
48
49             for (j = 0; j < nname; j++){
50                 printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name);
51             }
52         }
53 #   endif DEBUG
54     *defaultEs = excludes;
55     return 0;
56 }
57
58 static void
59 getstrtab(FILE *nfile, const char *filename)
60 {
61
62     fseek(nfile, (long)(N_SYMOFF(xbuf) + xbuf.a_syms), 0);
63     if (fread(&ssiz, sizeof (ssiz), 1, nfile) == 0) {
64         warnx("%s: no string table (old format?)" , filename );
65         done();
66     }
67     strtab = calloc(ssiz, 1);
68     if (strtab == NULL) {
69         warnx("%s: no room for %d bytes of string table", filename , ssiz);
70         done();
71     }
72     if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1) {
73         warnx("%s: error reading string table", filename );
74         done();
75     }
76 }
77
78     /*
79      * Read in symbol table
80      */
81 static void
82 getsymtab(FILE *nfile, const char *filename)
83 {
84     long        i;
85     int                 askfor;
86     struct nlist        nbuf;
87
88     /* pass1 - count symbols */
89     fseek(nfile, (long)N_SYMOFF(xbuf), 0);
90     nname = 0;
91     for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
92         fread(&nbuf, sizeof(nbuf), 1, nfile);
93         if ( ! funcsymbol( &nbuf ) ) {
94             continue;
95         }
96         nname++;
97     }
98     if (nname == 0) {
99         warnx("%s: no symbols", filename );
100         done();
101     }
102     askfor = nname + 1;
103     nl = (nltype *) calloc( askfor , sizeof(nltype) );
104     if (nl == 0) {
105         warnx("no room for %d bytes of symbol table", askfor * sizeof(nltype) );
106         done();
107     }
108
109     /* pass2 - read symbols */
110     fseek(nfile, (long)N_SYMOFF(xbuf), 0);
111     npe = nl;
112     nname = 0;
113     for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
114         fread(&nbuf, sizeof(nbuf), 1, nfile);
115         if ( ! funcsymbol( &nbuf ) ) {
116 #           ifdef DEBUG
117                 if ( debug & AOUTDEBUG ) {
118                     printf( "[getsymtab] rejecting: 0x%x %s\n" ,
119                             nbuf.n_type , strtab + nbuf.n_un.n_strx );
120                 }
121 #           endif DEBUG
122             continue;
123         }
124         npe->value = nbuf.n_value;
125         npe->name = strtab+nbuf.n_un.n_strx;
126 #       ifdef DEBUG
127             if ( debug & AOUTDEBUG ) {
128                 printf( "[getsymtab] %d %s 0x%08x\n" ,
129                         nname , npe -> name , npe -> value );
130             }
131 #       endif DEBUG
132         npe++;
133         nname++;
134     }
135     npe->value = -1;
136 }
137
138     /*
139      *  read in the text space of an a.out file
140      */
141 static void
142 gettextspace(FILE *nfile)
143 {
144
145     if ( cflag == 0 ) {
146         return;
147     }
148     textspace = (u_char *) malloc( xbuf.a_text );
149     if ( textspace == 0 ) {
150         warnx("ran out room for %d bytes of text space: can't do -c" ,
151                   xbuf.a_text );
152         return;
153     }
154     (void) fseek( nfile , N_TXTOFF( xbuf ) , 0 );
155     if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) {
156         warnx("couldn't read text space: can't do -c");
157         free( textspace );
158         textspace = 0;
159         return;
160     }
161 }
162
163 static bool
164 funcsymbol(struct nlist *nlistp)
165 {
166     char        *name, c;
167
168         /*
169          *      must be a text symbol,
170          *      and static text symbols don't qualify if aflag set.
171          */
172     if ( ! (  ( nlistp -> n_type == ( N_TEXT | N_EXT ) )
173            || ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) {
174         return FALSE;
175     }
176         /*
177          *      name must start with an underscore if uflag is set.
178          *      can't have any `funny' characters in name,
179          *      where `funny' means `.' (.o file names)
180          *      need to make an exception for sparc .mul & co.
181          *      perhaps we should just drop this code entirely...
182          */
183     name = strtab + nlistp -> n_un.n_strx;
184     if ( uflag && *name != '_' )
185         return FALSE;
186 #ifdef sparc
187     if ( *name == '.' ) {
188         char *p = name + 1;
189         if ( *p == 'u' )
190             p++;
191         if ( strcmp ( p, "mul" ) == 0 || strcmp ( p, "div" ) == 0 ||
192              strcmp ( p, "rem" ) == 0 )
193                 return TRUE;
194     }
195 #endif
196     while ( c = *name++ ) {
197         if ( c == '.' ) {
198             return FALSE;
199         }
200     }
201     return TRUE;
202 }