5 static void getstrtab(FILE *, const char *);
6 static void getsymtab(FILE *, const char *);
7 static void gettextspace(FILE *);
8 static bool funcsymbol(struct nlist *);
10 static char *strtab; /* string table in core */
11 static long ssiz; /* size of the string table */
12 static struct exec xbuf; /* exec header of a.out */
14 /* Things which get -E excluded by default. */
15 static char *excludes[] = { "mcount", "__mcleanup", NULL };
18 * Set up string and symbol tables from a.out.
19 * and optionally the text space.
20 * On return symbol table is sorted by value.
22 * Returns 0 on success, -1 on failure.
25 aout_getnfile(const char *filename, char ***defaultEs)
30 nfile = fopen( filename ,"r");
35 fread(&xbuf, 1, sizeof(xbuf), nfile);
40 getstrtab(nfile, filename);
41 getsymtab(nfile, filename);
42 gettextspace( nfile );
45 if ( debug & AOUTDEBUG ) {
48 for (j = 0; j < nname; j++){
49 printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name);
53 *defaultEs = excludes;
58 getstrtab(FILE *nfile, const char *filename)
61 fseek(nfile, (long)(N_SYMOFF(xbuf) + xbuf.a_syms), 0);
62 if (fread(&ssiz, sizeof (ssiz), 1, nfile) == 0) {
63 warnx("%s: no string table (old format?)" , filename );
66 strtab = calloc(ssiz, 1);
68 warnx("%s: no room for %d bytes of string table", filename , ssiz);
71 if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1) {
72 warnx("%s: error reading string table", filename );
78 * Read in symbol table
81 getsymtab(FILE *nfile, const char *filename)
87 /* pass1 - count symbols */
88 fseek(nfile, (long)N_SYMOFF(xbuf), 0);
90 for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
91 fread(&nbuf, sizeof(nbuf), 1, nfile);
92 if ( ! funcsymbol( &nbuf ) ) {
98 warnx("%s: no symbols", filename );
102 nl = (nltype *) calloc( askfor , sizeof(nltype) );
104 warnx("no room for %d bytes of symbol table", askfor * sizeof(nltype) );
108 /* pass2 - read symbols */
109 fseek(nfile, (long)N_SYMOFF(xbuf), 0);
112 for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
113 fread(&nbuf, sizeof(nbuf), 1, nfile);
114 if ( ! funcsymbol( &nbuf ) ) {
116 if ( debug & AOUTDEBUG ) {
117 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
118 nbuf.n_type , strtab + nbuf.n_un.n_strx );
123 npe->value = nbuf.n_value;
124 npe->name = strtab+nbuf.n_un.n_strx;
126 if ( debug & AOUTDEBUG ) {
127 printf( "[getsymtab] %d %s 0x%08x\n" ,
128 nname , npe -> name , npe -> value );
138 * read in the text space of an a.out file
141 gettextspace(FILE *nfile)
147 textspace = (u_char *) malloc( xbuf.a_text );
148 if ( textspace == 0 ) {
149 warnx("ran out room for %d bytes of text space: can't do -c" ,
153 (void) fseek( nfile , N_TXTOFF( xbuf ) , 0 );
154 if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) {
155 warnx("couldn't read text space: can't do -c");
163 funcsymbol(struct nlist *nlistp)
168 * must be a text symbol,
169 * and static text symbols don't qualify if aflag set.
171 if ( ! ( ( nlistp -> n_type == ( N_TEXT | N_EXT ) )
172 || ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) {
176 * name must start with an underscore if uflag is set.
177 * can't have any `funny' characters in name,
178 * where `funny' means `.' (.o file names)
179 * need to make an exception for sparc .mul & co.
180 * perhaps we should just drop this code entirely...
182 name = strtab + nlistp -> n_un.n_strx;
183 if ( uflag && *name != '_' )
186 if ( *name == '.' ) {
190 if ( strcmp ( p, "mul" ) == 0 || strcmp ( p, "div" ) == 0 ||
191 strcmp ( p, "rem" ) == 0 )
195 while ( c = *name++ ) {