home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / util_src / cnm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-29  |  4.8 KB  |  268 lines

  1. /* Copyright (c) 1988 by Sozobon, Limited.  Author: Johann Ruegg
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  *
  11.  *    Alternative ST symbol table lister.
  12.  */
  13.  
  14. /* highly munged from above, VERY quick and dirty nm, just enough
  15.    here to find _stksize for fixstk.c and printstk.c, may be
  16.    useful for other stuff.
  17.  
  18.    WARNING: -g option will not work with gcc-ld produced a.out
  19.  
  20.  
  21.      ++jrb
  22.  */
  23. /*
  24.  * Modified to handle expanded, GST linker derived, format for symbols.
  25.  * This format is produced with -G flag to a proper version of gcc-ld.
  26.  * -g flag also will work if ld coached properly.
  27.  *
  28.  *   --mj
  29.  */
  30.  
  31. #ifdef atarist
  32. long _stksize = -1L;  /* grab all memory you can get */
  33. #endif
  34.  
  35. #include <stdio.h>
  36.  
  37. #define  A_LNAM 0x48
  38.  
  39. struct hdr {
  40.     short magic;
  41.     long tsize, dsize, bsize;
  42.     long syms;
  43.     long f1,f2;
  44.     short f3;
  45. } h;
  46.  
  47. struct sym {
  48.     char name[8];
  49.     char flags;
  50.     char xflags;
  51.     long value;
  52. };
  53.  
  54. struct xsym {
  55.     char name[8];
  56.     char flags;
  57.     char xflags;
  58.     long value;
  59.     char tail[sizeof (struct sym)];
  60. };
  61.  
  62. int gflag;
  63.  
  64. int main(int argc, char **argv);
  65. int doopt(char *s);
  66. int cmp(struct xsym *s1, struct xsym *s2);
  67. void doname(char *s, int many);
  68. int dohdr(FILE *fd);
  69. void dosym(struct xsym *s);
  70. int not_glob(int x);
  71. void sflags(int x);
  72.  
  73. main(argc, argv)
  74. char **argv;
  75. {
  76.     int many;
  77.  
  78.     many = argc - 2;
  79.  
  80.     while (--argc) {
  81.         argv++;
  82.         if (*argv[0] == '-')
  83.             many -= doopt(argv[0]);
  84.         else
  85.             doname(argv[0], many);
  86.     }
  87.     exit(0);
  88. }
  89.  
  90. doopt(s)
  91. char *s;
  92. {
  93.     while (*++s)
  94.         switch (*s) {
  95.         case 'g':
  96.             gflag++;
  97.         }
  98.     return 1;    /* return number of processed options */
  99. }
  100.  
  101. int  cmp(s1, s2)
  102. struct xsym *s1, *s2;
  103. {
  104.     return( s1->value - s2->value);
  105. }
  106.  
  107. void
  108. doname(s, many)
  109. char *s;
  110. {
  111.     FILE *fd, *fopen();
  112.     int i, count;
  113.  
  114.     fd = fopen(s, "r");
  115.     if (fd == NULL) {
  116.         fprintf(stderr, "Can't open %s\n", s);
  117.         return;
  118.     }
  119.  
  120.     if (many)
  121.         printf("\n%s:\n", s);
  122.  
  123.     if (i = dohdr(fd))
  124.     {
  125.         register struct xsym *syms;
  126.         register struct xsym *currsym;
  127.         printf("%d slots mem %d\n", i, i*sizeof(struct sym));
  128.         
  129.         if((syms = (struct xsym *)malloc(i*sizeof(struct xsym))) == NULL)
  130.         {
  131.         perror("Outa mem");
  132.         exit(1);
  133.         }
  134.         count = i;
  135.         currsym = syms;
  136.         while (count)
  137.         {
  138.             if(fread(currsym, sizeof(struct sym), 1, fd) != 1)
  139.             {
  140.             perror("reading syms");
  141.             exit(2);
  142.             }
  143.         if (--count) {
  144.             if (A_LNAM == (currsym->xflags & A_LNAM))
  145.             {
  146.                     if(fread(&(currsym->tail),
  147.                 sizeof(struct sym), 1, fd) != 1)
  148.                     {
  149.                     perror("reading syms");
  150.                     exit(2);
  151.                     }
  152.                 --i;
  153.                 --count;
  154.             }
  155.         }
  156.         else /* object was partially stripped */
  157.             currsym->xflags &= ~A_LNAM;        
  158.         if (gflag && not_glob(currsym->flags))
  159.             --i;
  160.         else
  161.             currsym++;
  162.             }
  163.         fclose(fd);
  164.         printf("%d %ssymbols\n", i, (gflag ? "global ": ""));
  165.         qsort(syms, i, sizeof(struct xsym), cmp);
  166.         while (i--)
  167.         {
  168.         dosym(syms);
  169.         syms += 1;
  170.         }
  171.         free(syms);
  172.     }
  173. }
  174.  
  175. dohdr(fd)
  176. FILE *fd;
  177. {
  178.     int i;
  179.     long len;
  180.  
  181. #if 0
  182.     fread(&h, 2, 1, fd);
  183.     if (h.magic == ARMAG1)
  184.         return -1;
  185. #endif
  186.     i = fread((char *)&h , sizeof(h), 1, fd);
  187.     if (i != 1 || h.magic != 0x601a) {
  188.         printf("Bad header\n");
  189.         return 0;
  190.     }
  191.     len = h.tsize + h.dsize;
  192.     fseek(fd, len, 1);
  193.     return h.syms / sizeof(struct sym);
  194. }
  195.  
  196. void
  197. dosym(s)
  198. struct xsym *s;
  199. {
  200.     printf("%-8.8s", s->name);
  201.     printf("%-14.14s", (A_LNAM == (s->xflags & A_LNAM) ? s->tail : ""));
  202.     printf("\t%8lx ", s->value);
  203.     sflags(s->flags);
  204.     putchar('\n');
  205. }
  206.  
  207. #if 0
  208. fill8(s)
  209. char *s;
  210. {
  211.     int i;
  212.  
  213.     for (i=0; i<8; i++)
  214.         if (s[i] == 0)
  215.             putchar(' ');
  216. }
  217. #endif
  218.  
  219. char *fname[] = {
  220.     "?0?", " bss", " text", "?3?", " data",
  221.     "?5?", "?6?", "?7?"
  222. };
  223. char *Fname[] = {
  224.     "?0?", "Bss", "Text", "?3?", "Data",
  225.     "?5?", "?6?", "?7?"
  226. };
  227.  
  228. not_glob(x)
  229. {
  230.     x &= 0xff;
  231.     if (x & 0x20)
  232.         return 0;
  233.     x &= ~0x20;
  234.     if (x == 0x88)
  235.         return 0;
  236.     return 1;
  237. }
  238.  
  239. void
  240. sflags(x)
  241. {
  242.     char **category;
  243.     int lflag;
  244.     
  245.     if (0 != (lflag = not_glob(x)))
  246.         category = fname;
  247.     else
  248.         category = Fname;
  249.     x &= 0xff;
  250.     if (x & 0xd8) {
  251.         if (x & 0x08)
  252.             printf (" external");
  253.         if (x & 0x50) {
  254.             printf (" equ");
  255.             if (x & 0x10)
  256.                 printf (" reg");
  257.         }
  258.         if (x & 0x08)
  259.             printf (" abs");
  260.         if (!lflag)
  261.             printf(" G");
  262.     }
  263.     else {
  264.         x &= 7;
  265.         printf(category[x]);
  266.     }
  267. }
  268.