home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / utlsrc33.lzh / UTLSRC33 / FIXSTK.C < prev    next >
C/C++ Source or Header  |  1993-07-30  |  5KB  |  234 lines

  1. /*
  2.  * utility to adjust _stksize in gcc-cc1.ttp
  3.  *
  4.  *    Usage: fixstk size [<filename>]
  5.  *      size:    specified as # of bytes         nnn
  6.  *        specified as # of Kilo Bytes        nnnK
  7.  *        specified as # of Mega Bytes        nnnM
  8.  *      filename:
  9.  *        optional, defaults to \.gcc-cc1.ttp
  10.  *
  11.  *    ++jrb
  12.  *
  13.  *      modified to allow fixing applications for which size of a stack
  14.  *      is defined via _initial_stack -- mj
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <st-out.h>
  19.  
  20. #if __STDC__
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #else
  25. #include <string.h>
  26. extern char *malloc();
  27. extern long lseek();
  28. #define size_t unsigned long
  29. #endif
  30.  
  31. #ifndef FILENAME_MAX
  32. # define FILENAME_MAX 128
  33. #endif
  34.  
  35. #ifdef WORD_ALIGNED
  36. # define SIZEOF_AEXEC ((2*sizeof(short)) + (6*sizeof(long)))
  37. # define SIZEOF_ASYM  ((SYMLEN*sizeof(char)) + sizeof(short) + sizeof(long))
  38. # define SYM_OFFSET   (sizeof(short) + (3*sizeof(long)))
  39. #endif
  40.  
  41.  
  42. static char *sym_names[] = { "__stksize", "__initial_stack" };
  43.  
  44. long find_offset (fd, fn, what)
  45. int fd;
  46. char *fn;
  47. int *what;
  48. {
  49.     struct aexec head;
  50.     struct asym  sym;
  51.     int found;
  52.     int    all = 0;
  53.     int index = 1;
  54.     
  55. #ifndef WORD_ALIGNED
  56.     if(read(fd, &head, sizeof(head)) != sizeof(head))
  57. #else
  58.     if(read_head(fd, &head))
  59. #endif
  60.     {
  61.     perror(fn);
  62.     exit(2);
  63.     }
  64.     if(head.a_magic != CMAGIC)
  65.     {
  66.     fprintf(stderr,"Invalid magic number %x\n", head.a_magic);
  67.     exit(3);
  68.     }
  69.     if(head.a_syms == 0)
  70.     {
  71.     fprintf(stderr,"%s: no symbol table\n", fn);
  72.     exit(4);
  73.     }
  74.     if(lseek(fd, head.a_text+head.a_data, 1) != 
  75. #ifndef WORD_ALIGNED
  76.        (head.a_text+head.a_data+sizeof(head)))
  77. #else
  78.        (head.a_text+head.a_data+SIZEOF_AEXEC))
  79. #endif
  80.     {
  81.     perror(fn);
  82.     exit(5);
  83.     }
  84.     for(;;)
  85.     {
  86. #ifndef WORD_ALIGNED
  87.     if(index && (read(fd, &sym, sizeof(sym)) != sizeof(sym)))
  88. #else
  89.     if(index && read_sym(fd, &sym))
  90. #endif
  91.     {
  92.         fprintf(stderr, "symbol _stksize not found\n");
  93.         exit(6);
  94.     }
  95.     /* after symbol read check first for _stksize */
  96.     index ^= 1;
  97.     if (strncmp(sym_names[index], sym.a_name, 8) == 0)
  98.     {
  99.         if ((found = (sym.a_type & A_DATA)) || all++)
  100.         break;
  101.     }
  102.     }
  103.     
  104.     if(!found)
  105.     {
  106.     fprintf(stderr, "symbol _stksize is undefined\n");
  107.     exit(9);
  108.     }
  109.     *what = index;
  110. #ifndef WORD_ALIGNED
  111.     return sym.a_value + sizeof(head);
  112. #else
  113.     return sym.a_value + SIZEOF_AEXEC;
  114. #endif
  115. }
  116.  
  117. long calc_newsize(s)
  118. char *s;
  119. {
  120.     size_t len = strlen(s) - 1;
  121.     long mul = 1;
  122.     long atol();
  123.  
  124.     switch(s[len])
  125.     {
  126.       case 'k': case 'K':
  127.     mul = 1L << 10;
  128.     break;
  129.       case 'm': case 'M':
  130.     mul = 1L << 20;
  131.     break;
  132.       default:
  133.     len += 1;
  134.     }
  135.     
  136.     s[len] = '\0';
  137.     return mul * atol(s);
  138. }
  139.  
  140. int main(argc, argv)
  141. int argc;
  142. char **argv;
  143. {
  144.     int fd;
  145.     int what;
  146.     long newstksize, stksize, offset;
  147.     char sizestr[16], fn[FILENAME_MAX];
  148.     
  149.     if(argc < 2)
  150.     {
  151.     fprintf(stderr, "usage: fixstk size [<filename>]\n");
  152.     exit(1);
  153.     }
  154.  
  155.     strcpy(sizestr, *++argv);
  156.  
  157.     if(argc > 2)
  158.     (void) strcpy(fn, *++argv);
  159.     else
  160.     (void) strcpy(fn, "gcc-cc1.ttp");
  161.  
  162.     if((fd = open(fn, 2)) < 0)
  163.     {
  164.     perror(fn);
  165.     exit(1);
  166.     }
  167.     
  168.     newstksize = calc_newsize(sizestr);
  169.     offset = find_offset(fd, fn, &what);
  170.     if(lseek(fd, offset, 0) != offset)
  171.     {
  172.     perror(fn);
  173.     close(fd);
  174.     exit(7);
  175.     }
  176.     read(fd, &stksize, sizeof(long));
  177.     printf("%s: %s was %ld (%dK)\n",
  178.          fn, sym_names[what] + 1, stksize, (int)(stksize/1024));
  179.     
  180.     lseek(fd, -((long)sizeof(long)), 1);
  181.     
  182.     if(write(fd, &newstksize, sizeof(long)) != sizeof(long))
  183.     {
  184.     perror(fn);
  185.     close(fd);
  186.     exit(8);
  187.     }
  188.     
  189.     lseek(fd, -((long)sizeof(long)), 1);
  190.     
  191.     read(fd, &stksize, sizeof(long));
  192.     printf("%s: %s now is %ld (%dK)\n",
  193.          fn, sym_names[what] + 1, stksize, (int)(stksize/1024));
  194.     return close(fd);
  195. }
  196.  
  197. #ifdef WORD_ALIGNED
  198. #ifndef atarist
  199. # define lread  read
  200. # define lwrite write
  201. #endif
  202.  
  203. /*
  204.  * read header -- return !0 on err
  205.   */
  206. #define ck_read(fd, addr, siz) \
  207.   if((long)siz != lread(fd, addr, (long)siz)) return !0;
  208.   
  209. int read_head (fd, a)
  210. int fd;
  211. struct aexec *a;
  212. {
  213.     ck_read(fd, &a->a_magic,   sizeof(a->a_magic));
  214.     ck_read(fd, &a->a_text,    sizeof(a->a_text));
  215.     ck_read(fd, &a->a_data,    sizeof(a->a_data));
  216.     ck_read(fd, &a->a_bss,     sizeof(a->a_bss));
  217.     ck_read(fd, &a->a_syms,    sizeof(a->a_syms));
  218.     ck_read(fd, &a->a_AZero1,  sizeof(a->a_AZero1));
  219.     ck_read(fd, &a->a_AZero2,  sizeof(a->a_AZero2));
  220.     ck_read(fd, &a->a_isreloc, sizeof(a->a_isreloc));
  221.     return 0;
  222. }
  223.  
  224. int read_sym(fd, s)
  225. int fd;
  226. struct asym *s;
  227. {
  228.     ck_read(fd, s->a_name, 8);
  229.     ck_read(fd, &(s->a_type), sizeof(short));
  230.     ck_read(fd, &(s->a_value), sizeof(long));
  231.     return 0;
  232. }
  233. #endif
  234.