Make ath(4) compilable into the kernel and add it to LINT.
[dragonfly.git] / usr.bin / gprof / aout.c
blob1cf4f99d423882c71edb63384232ea06afa885f4
1 /* $DragonFly: src/usr.bin/gprof/aout.c,v 1.3 2006/01/22 03:43:37 swildner Exp $ */
2 #include <a.out.h>
4 #include "gprof.h"
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 *);
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 */
15 /* Things which get -E excluded by default. */
16 static char *excludes[] = { "mcount", "__mcleanup", NULL };
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.
23 * Returns 0 on success, -1 on failure.
25 int
26 aout_getnfile(const char *filename, char ***defaultEs)
28 FILE *nfile;
29 int valcmp();
31 nfile = fopen( filename ,"r");
32 if (nfile == NULL) {
33 perror( filename );
34 done();
36 fread(&xbuf, 1, sizeof(xbuf), nfile);
37 if (N_BADMAG(xbuf)) {
38 fclose(nfile);
39 return -1;
41 getstrtab(nfile, filename);
42 getsymtab(nfile, filename);
43 gettextspace( nfile );
44 fclose(nfile);
45 # ifdef DEBUG
46 if ( debug & AOUTDEBUG ) {
47 int j;
49 for (j = 0; j < nname; j++){
50 printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name);
53 # endif /* DEBUG */
54 *defaultEs = excludes;
55 return 0;
58 static void
59 getstrtab(FILE *nfile, const char *filename)
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();
67 strtab = calloc(ssiz, 1);
68 if (strtab == NULL) {
69 warnx("%s: no room for %d bytes of string table", filename , ssiz);
70 done();
72 if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1) {
73 warnx("%s: error reading string table", filename );
74 done();
79 * Read in symbol table
81 static void
82 getsymtab(FILE *nfile, const char *filename)
84 long i;
85 int askfor;
86 struct nlist nbuf;
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;
96 nname++;
98 if (nname == 0) {
99 warnx("%s: no symbols", filename );
100 done();
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();
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 );
121 # endif /* DEBUG */
122 continue;
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 );
131 # endif /* DEBUG */
132 npe++;
133 nname++;
135 npe->value = -1;
139 * read in the text space of an a.out file
141 static void
142 gettextspace(FILE *nfile)
145 if ( cflag == 0 ) {
146 return;
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;
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;
163 static bool
164 funcsymbol(struct nlist *nlistp)
166 char *name, c;
169 * must be a text symbol,
170 * and static text symbols don't qualify if aflag set.
172 if ( ! ( ( nlistp -> n_type == ( N_TEXT | N_EXT ) )
173 || ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) {
174 return FALSE;
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...
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;
195 #endif
196 while ( c = *name++ ) {
197 if ( c == '.' ) {
198 return FALSE;
201 return TRUE;