home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / gcl-1.000 / gcl-1 / gcl-1.0 / unixport / bsd_rsym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-29  |  3.4 KB  |  151 lines

  1. /*  Use this to build an executable rsym,
  2. which will grab only the external symbols from an object file,
  3. and put them in a simple format: (cf ext_sym.h) 
  4.  
  5. This information will be used for relocation. 
  6.  
  7. to compile use cc -g rsym.c -o rsym  -I../h
  8. */
  9.  
  10.  
  11. #define BSD
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <a.out.h>
  16. #include "ext_sym.h"
  17.  
  18.  
  19. /* our defs */
  20.  
  21. #define TABLE_SIZE 3
  22.  
  23.  
  24. #ifdef DEBUG
  25. int debug =1;
  26. #define dprintf(s,ar) if(debug) { printf(" ( s )",ar) ; fflush(stdout);}
  27. #else
  28. int debug =0;
  29. #define dprintf(s,ar) 
  30. #endif
  31.  
  32. struct exec my_header; 
  33. struct syment *my_symbol_table;
  34. char *my_string_table;
  35. char *start_address;
  36.  
  37.  
  38.  
  39. /*  this program will get the external symbols from a file writing
  40. them out to a file together with their addresses */
  41.  
  42.  
  43. main(argc,argv)
  44. int argc ;
  45. char *argv[];
  46. {
  47. if (argc!=3) {perror("bad arg count");fflush(stdout);exit(1);}
  48. get_myself(argv[1]);
  49.  output_externals(argv[2]);
  50.   }
  51.  
  52. get_myself(filename)
  53. char *filename;
  54. {
  55.     int i;
  56.     LDFILE *ldptr;
  57.     extern char *malloc();
  58.         
  59.     ldptr = ldopen(filename, RDONLY);
  60.     
  61.     if (ldptr == NULL) {
  62.         fprintf(stderr, "Can't open %s\n", filename);
  63.         exit(1);
  64.     }
  65.     ftell(ldptr);
  66.     fread(&my_header,sizeof(struct exec),1,ldptr);
  67.     if(N_BADMAG(my_header)){fprintf(stderr,"Bad magic %s",filename);
  68.                 exit(1);};
  69.  
  70.     if(fseek(ldptr,(int)N_SYMOFF(my_header),0))
  71.       {fprintf(stderr,"seek error");
  72.                            exit(1);}
  73.        
  74.     my_symbol_table
  75.     = (struct syment *)malloc(sizeof(struct syment) * NSYMS(my_header));
  76.     /*
  77.     sizeof(struct syment) and SYMESZ are not always the same.
  78.     */
  79.     for (i = 0;  i < NSYMS(my_header);  i++)
  80.         FREAD(&my_symbol_table[i], SYMESZ, 1, ldptr);
  81.     /*
  82.     If the string table is not empty,
  83.     its length is stored after the symbol table,
  84.     This is not described in the manual, and may change in the future.
  85.     */
  86.     /* fseek(ldptr,N_STROFF(my_header),0);
  87.        strings follow symbol table! */
  88.  
  89.     if (FREAD(&i, 4, 1, ldptr) > 0)    {
  90.         my_string_table = malloc(i);
  91.                 if(debug)
  92.           {printf(" i is %d Fseek %d ",i,FSEEK(ldptr,i-1,1));
  93.            printf(" Fseek back %d ",FSEEK(ldptr,1-i,1));};
  94.         FSEEK(ldptr, -4, 1);
  95.         if(i!=(FREAD(my_string_table, 1, i, ldptr)))
  96.           {dprintf( i was %d ,i);
  97.            perror("rsym could not read bad string table") ;
  98.            exit(1);}
  99.  
  100.     }
  101.     else {fprintf(stderr,"Error: There is no string table \n");
  102.              exit(1);}
  103.  
  104.     ldclose(ldptr);
  105. }
  106.  
  107. output_externals(outfile)
  108. char *outfile;
  109. {FILE *symout;
  110.  char *name;
  111.  struct lsymbol_table tab;
  112.  char out[4];
  113.  char tem[SYMNMLEN+1];
  114.  struct syment *p, *end;
  115. tem[SYMNMLEN]=0;
  116. tab.n_symbols=0;tab.tot_leng=0;
  117.  symout=fopen(outfile,"w");
  118.  if (!symout)
  119.    {perror(outfile); exit(1);};
  120.  fseek(symout,sizeof(struct lsymbol_table),0);
  121.  end = my_symbol_table + NSYMS(my_header);
  122.  for (p = my_symbol_table; p < end; p++)    {
  123.    /*
  124.      Is the following check enough?
  125.      */
  126.    if EXT_and_TEXT_BSS_DAT(p)
  127.      { name= SYM_NAME(p);
  128.        {int i=0;
  129.       dprintf(tab.n_symbols %d , tab.n_symbols);
  130.       tab.n_symbols++;
  131.       fwrite(&(p->n_value),sizeof(int),1,symout);
  132.       dprintf( p->n_value %d , p->n_value);
  133.       dprintf( name %s , name);
  134.       while(tab.tot_leng++,*name)
  135.     putc(*name++,symout); 
  136.        putc(0,symout);
  137. /*      fprintf(symout,name);
  138.       fprintf(symout," %d  ", p->n_value); 
  139. */
  140.      };
  141.    dprintf(  NUM_AUX(p) %d ,  NUM_AUX(p));
  142.    dprintf( index , (int) (p - my_symbol_table)  / sizeof(struct syment));
  143.    p = p + NUM_AUX(p); }
  144.  }
  145.  fseek(symout,0,0);
  146.  fwrite(&tab,sizeof(tab),1,symout);
  147.  fclose(symout);
  148.  return 0;
  149.  }
  150.  
  151.