exsym.c
Go to the documentation of this file.
00001 /* anal: analyze a.out file
00002         date:   Dec/24/1985
00003         author: T.Matsui
00004 */
00005 
00006 #include <stdio.h>
00007 #include <fcntl.h>
00008 #include <a.out.h>
00009 
00010 #define MAXSYMBOLS 1024
00011 #define hexch(x) (char)((x<10)?(x+'0'):(x+'0'+7))
00012 #define min(x,y) ((x<y)?x:y)
00013 
00014 extern char *optarg;
00015 extern int optind;
00016 
00017 int  infd,outfd;
00018 int  symtabsize;
00019 int  fmask,castabs=0,rmesect=0;
00020 
00021 fatal(mes)
00022 { puts(mes); exit(2);}
00023 
00024 readsym(n)
00025 int n;
00026 { register int ch,i,f;
00027   struct {
00028     char flag;
00029     char dummy;
00030     long val;
00031     char id[16];} sym;
00032   
00033   while (n>0) {
00034     fread(&sym,6,1,stdin);      /*read flags*/
00035     n -= 6;
00036     i=0;
00037     do {        /*read symbol id letters*/
00038       ch=getchar();
00039       sym.id[i++]=ch;
00040       n--;}
00041       while (ch);
00042     if (sym.flag & 0x20) {      /*external symbol?*/
00043       if (sym.flag==0x23) f=0x28;
00044       else if (sym.flag==0x20) f=0x30;
00045       else f=sym.flag;
00046       if (castabs) sym.flag=0x21;
00047       if (rmesect && 
00048                 (strcmp(sym.id,"_etext")==0 || strcmp(sym.id,"_edata")==0 ||
00049                  strcmp(sym.id,"_end")==0)) /*remove section name*/ f=0;
00050       if (f & fmask) {
00051         write(outfd,&sym,6+i);
00052         symtabsize += 6+i;}}}
00053   }
00054 
00055 main(argc,argv)
00056 int argc;
00057 char *argv[];
00058 { struct bhdr header,newheader;
00059   char *progname,c;
00060   progname=argv[0];
00061   outfd= -1; fmask=31;
00062   while ((c=getopt(argc,argv,"o:dtbuaAE")) != EOF)
00063     switch(c) {
00064     case 'o': outfd=creat(optarg,0644);
00065               if (outfd<0) {
00066                 fprintf(stderr,"%s: cannot open output file: %s\n",
00067                         progname,optarg);
00068                 exit(1);}
00069               break;
00070     case 'a': fmask&=0x1e; break;
00071     case 'd': fmask&=0x17; break;
00072     case 't': fmask&=0x1d; break;
00073     case 'b': fmask&=0x1b; break;
00074     case 'u': fmask&=0x0f; break;
00075     case 'A': castabs=1; break;
00076     case 'E': rmesect=1; break;
00077     case '?': fprintf(stderr,"invalid option %s\n",optarg);
00078               break;
00079     default:  break;}
00080 
00081   /*set up input files*/
00082   if (optind<argc) {
00083     infd=open(argv[optind],O_RDONLY);
00084     if (infd<0) {
00085       fprintf(stderr,"%s: cannot open %s\n",progname,argv[optind]);
00086       exit(1);}
00087     close(0);   /*switch stdin*/
00088     dup(infd);} 
00089   if (outfd<0) outfd=creat("a.out",0644);
00090   if (read(0,&header,sizeof(header))<1) fatal("cannot read");
00091 
00092   newheader=header;
00093   newheader.tsize=0; newheader.dsize=0; newheader.bsize=0;
00094   newheader.rtsize=newheader.rdsize=0;
00095   write(outfd,&newheader,sizeof(newheader));
00096   
00097   lseek(0,32+header.tsize+header.dsize,0);      /*seek to symbol table*/
00098   symtabsize=0;
00099   readsym(header.ssize);
00100   lseek(outfd,0,0);             /*move back to the file head*/
00101 /*  fprintf(stderr,"symtabsize=%d\n",symtabsize); */
00102   newheader.ssize=symtabsize;
00103   write(outfd,&newheader,sizeof(newheader));
00104   exit(0);}
00105 


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Sep 3 2015 10:36:19