Made gprof use bfd instead. This is the first step in allowing cross-hosted

gprof use.
This commit is contained in:
Sean Eric Fagan 1991-07-22 19:33:46 +00:00
parent 39ada6fced
commit 811e3c6a86
2 changed files with 123 additions and 111 deletions

View File

@ -185,24 +185,26 @@ main(argc, argv)
*/
getnfile()
{
FILE *nfile;
bfd *abfd;
int valcmp();
nfile = fopen( a_outname ,"r");
if (nfile == NULL) {
perror( a_outname );
abfd = bfd_openr (a_outname, NULL);
if (abfd == NULL) {
perror (a_outname);
done();
}
fread(&xbuf, 1, sizeof(xbuf), nfile);
if (N_BADMAG(xbuf)) {
fprintf(stderr, "%s: %s: bad format\n", whoami , a_outname );
if (!bfd_check_format (abfd, bfd_object)) {
fprintf (stderr, "%s: %s: bad format\n", whoami, a_outname);
done();
}
getstrtab(nfile);
getsymtab(nfile);
gettextspace( nfile );
/* getstrtab(nfile); */
getsymtab(abfd);
gettextspace( abfd );
qsort(nl, nname, sizeof(nltype), valcmp);
fclose(nfile);
# ifdef DEBUG
if ( debug & AOUTDEBUG ) {
register int j;
@ -214,6 +216,7 @@ getnfile()
# endif DEBUG
}
#if 0
getstrtab(nfile)
FILE *nfile;
{
@ -236,27 +239,30 @@ getstrtab(nfile)
done();
}
}
#endif /* 0 */
/*
* Read in symbol table
*/
getsymtab(nfile)
FILE *nfile;
getsymtab(abfd)
bfd *abfd;
{
register long i;
int askfor;
struct nlist nbuf;
int nosyms;
asymbol **syms;
i = get_symtab_upper_bound (abfd); /* This will probably give us more
* than we need, but that's ok.
*/
syms = malloc (i);
nosyms = bfd_canonicalize_symtab (abfd, syms);
/* pass1 - count symbols */
fseek(nfile, (long)N_SYMOFF(xbuf), 0);
nname = 0;
for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
fread(&nbuf, sizeof(nbuf), 1, nfile);
if ( ! funcsymbol( &nbuf ) ) {
for (i = 0; i < nosyms; i++) {
if (!funcsymbol (syms[i]))
continue;
}
nname++;
}
if (nname == 0) {
fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname );
done();
@ -270,22 +276,20 @@ getsymtab(nfile)
}
/* pass2 - read symbols */
fseek(nfile, (long)N_SYMOFF(xbuf), 0);
npe = nl;
nname = 0;
for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
fread(&nbuf, sizeof(nbuf), 1, nfile);
if ( ! funcsymbol( &nbuf ) ) {
for (i = 0; i < nosyms; i++) {
if (!funcsymbol (syms[i])) {
# ifdef DEBUG
if ( debug & AOUTDEBUG ) {
printf( "[getsymtab] rejecting: 0x%x %s\n" ,
nbuf.n_type , strtab + nbuf.n_un.n_strx );
syms[i]->value, syms[i]->name);
}
# endif DEBUG
continue;
}
npe->value = nbuf.n_value;
npe->name = strtab+nbuf.n_un.n_strx;
npe->value = syms[i]->value + syms[i]->section->vma;
npe->name = syms[i]->name;
# ifdef DEBUG
if ( debug & AOUTDEBUG ) {
printf( "[getsymtab] %d %s 0x%08x\n" ,
@ -298,34 +302,35 @@ getsymtab(nfile)
npe->value = -1;
}
/*
/*
* read in the text space of an a.out file
*/
gettextspace( nfile )
FILE *nfile;
gettextspace( abfd )
bfd *abfd;
{
char *malloc();
asection *texsec;
if ( cflag == 0 ) {
return;
}
textspace = (u_char *) malloc( xbuf.a_text );
texsec = bfd_get_section_by_name (abfd, ".text");
if (texsec == NULL) {
return;
}
textspace = (u_char *) malloc( texsec->size );
if ( textspace == 0 ) {
fprintf( stderr , "%s: ran out room for %d bytes of text space: " ,
whoami , xbuf.a_text );
whoami , texsec->size);
fprintf( stderr , "can't do -c\n" );
return;
}
(void) fseek( nfile , N_TXTOFF( xbuf ) , 0 );
if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) {
fprintf( stderr , "%s: couldn't read text space: " , whoami );
fprintf( stderr , "can't do -c\n" );
free( textspace );
textspace = 0;
return;
}
bfd_get_section_contents (abfd, texsec, textspace, texsec->filepos,
texsec->size);
}
/*
/*
* information from a gmon.out file is in two parts:
* an array of sampling hits within pc ranges,
* and the arcs.
@ -656,8 +661,8 @@ alignentries()
}
bool
funcsymbol( nlistp )
struct nlist *nlistp;
funcsymbol( symp )
asymbol *symp;
{
extern char *strtab; /* string table from a.out */
extern int aflag; /* if static functions aren't desired */
@ -667,16 +672,23 @@ funcsymbol( nlistp )
* must be a text symbol,
* and static text symbols don't qualify if aflag set.
*/
if ( ! ( ( nlistp -> n_type == ( N_TEXT | N_EXT ) )
|| ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) {
if (!symp->section)
return FALSE;
if (!aflag && (symp->flags&BSF_LOCAL)) {
#ifdef DEBUG
fprintf (stderr, "%s(%d): %s: not a function\n", __FILE__, __LINE__, symp->name);
#endif
return FALSE;
}
/*
* can't have any `funny' characters in name,
* where `funny' includes `.', .o file names
* and `$', pascal labels.
*/
for ( name = strtab + nlistp -> n_un.n_strx ; *name ; name += 1 ) {
for (name = symp->name; *name; name++) {
if ( *name == '.' || *name == '$' ) {
return FALSE;
}

View File

@ -19,10 +19,11 @@
* @(#)gprof.h 5.9 (Berkeley) 6/1/90
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <a.out.h>
#include <stdio.h>
#include "bfd.h"
#include "gmon.h"
#ifdef MACHINE_H
@ -164,7 +165,6 @@ double scale; /* scale factor converting samples to pc
values: each sample covers scale bytes */
char *strtab; /* string table in core */
off_t ssiz; /* size of the string table */
struct exec xbuf; /* exec header of a.out */
unsigned char *textspace; /* text space of a.out in core */
/*