From: "Anthony.Appleyard" Organization: Materials Science Centre To: DJGPP AT DELORIE DOT COM Date: Wed, 9 Apr 1997 09:36:14 GMT-1 Subject: a program to find unused global symbols in a multi-file program Reply-to: Anthony DOT Appleyard AT umist DOT ac DOT uk Message-ID: <7969206701@fs2.mt.umist.ac.uk> I wrote this Gnu C program (WEEDSYMS.C) to find unused global symbols in a multi-file djgpp program. To use it:- (1) Compile all the program's component files with `gcc ... -S' to produce *.S files. (In the case of the text editor AAEMACS that I wrote, that will produce files CC.S MAIN.S EM.S DISPLAY.S MACROS.S KEYF.S ZIP.S .) (2) EITHER call WEEDSYMS.EXE with all these *.S filenames as its args, e.g. WEEDSYMS CC.S MAIN.S EM.S DISPLAY.S MACROS.S KEYF.S ZIP.S OR type e.g. WEEDSYMS @ARGFILE where the file ARGFILE contains all the *.S filenames, one per line. (3) The list of unused global symbols will now be on the file _.O (in `mangled' form, if the *.S files were compiled by C++), one per line. If a symbol started with an underline, that underline is printed out as space, to produce the correct input form for the demangler CXXFILT. ------------------------------------------------------- It will not find unused inlined functions (unless you un-inline them by inserting or #include'ing `#define inline' at the top of each source file). It will not output line numbers correctly unless the *.S files were compiled with the -g option. ------------------------------------------------------- #include #include #include #include #define uns unsigned /*-----*//* the args should be names of *.S files created by `djgpp -S' */ int main(int nargs,char**arg){ FILE*F; int i,j,k,l,m,n=0; uns short*lnaddr=0; uns char*sw,**sym; char L[1024],*arg0,*unused,*s; if(nargs==2) if(arg[1][0]=='@') { /* if the args are on a file */ struct stat B; arg0=arg[0]; if(stat(sw=arg[1]+1,&B)) {printf("I can't find file `%s'\n",sw); return 1;} F=fopen(sw,"r"); /* open the file */ sw=(char*)malloc((j=B.st_size)+1); /* make array to hold whole file */ fread(sw,1,j,F); /* read file */ fclose(F); sw[j]=0; for(k=i=0;i1) strcpy(sym[n-1],L+7); else sym[n-1][0]=0; sw=sym[n-1]+j; sw[1]=m; /* it is in the mth file */ sw[2]=sw[3]=0; /* for the line number */ lnaddr=(uns short*)&sw[2];}} fclose(F);} for(k=i=n-1;k;i--) for(k=j=0;j0) { sw=sym[j+1]; sym[j+1]=sym[j]; sym[j]=sw; k=1;} /* bubble sort */ for(i=n-1;i>0;i--) if(!strcmp(sym[i-1],sym[i])) sym[i][0]=0; /* find duplicates */ unused=(char*)malloc(n); for(i=0;i'9') if(k<'0'?:k>'9') if(k<'A'?:k>'Z') if(k<'a'?:k>'z') L[i]=0; /* replace all non-symbol chars by ascii 0 */ for(i=0;i