anm.c

Go to the documentation of this file.
00001 /*
00002  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
00003  * See the copyright notice in the ACK home directory, in the file "Copyright".
00004  */
00005 /* $Id: anm.c,v 1.2 2005/06/23 09:50:54 philip Exp $ */
00006 /*
00007 **      print symbol tables for
00008 **      ACK object files
00009 **
00010 **      anm [-gopruns] [name ...]
00011 */
00012 
00013 #include        "out.h"
00014 #include        "arch.h"
00015 #include        "ranlib.h"
00016 
00017 #include        <stdio.h>
00018 #include        <ctype.h>
00019 
00020 int     numsort_flg;
00021 int     sectsort_flg;
00022 int     undef_flg;
00023 int     revsort_flg = 1;
00024 int     globl_flg;
00025 int     nosort_flg;
00026 int     arch_flg;
00027 int     prep_flg;
00028 int     read_error;
00029 struct  outhead hbuf;
00030 struct  outsect sbuf;
00031 long    off;
00032 char    *malloc();
00033 char    *realloc();
00034 long    s_base[S_MAX];  /* for specially encoded bases */
00035 char    *filename;
00036 int     narg;
00037 
00038 main(argc, argv)
00039 char **argv;
00040 {
00041 
00042         if (--argc>0 && argv[1][0]=='-' && argv[1][1]!=0) {
00043                 argv++;
00044                 while (*++*argv) switch (**argv) {
00045                 case 'n':               /* sort numerically */
00046                         numsort_flg++;
00047                         continue;
00048 
00049                 case 's':               /* sort in section order */
00050                         sectsort_flg++;
00051                         continue;
00052 
00053                 case 'g':               /* globl symbols only */
00054                         globl_flg++;
00055                         continue;
00056 
00057                 case 'u':               /* undefined symbols only */
00058                         undef_flg++;
00059                         continue;
00060 
00061                 case 'r':               /* sort in reverse order */
00062                         revsort_flg = -1;
00063                         continue;
00064 
00065                 case 'p':               /* don't sort -- symbol table order */
00066                         nosort_flg++;
00067                         continue;
00068 
00069                 case 'o':               /* prepend a name to each line */
00070                         prep_flg++;
00071                         continue;
00072 
00073                 default:                /* oops */
00074                         fprintf(stderr, "anm: invalid argument -%c\n", *argv[0]);
00075                         exit(1);
00076                 }
00077                 argc--;
00078         }
00079         if (argc == 0) {
00080                 argc = 1;
00081                 argv[1] = "a.out";
00082         }
00083         narg = argc;
00084 
00085         while(argc--) {
00086                 int fd;
00087 
00088                 filename = *++argv;
00089                 if ((fd = open(filename, 0)) < 0) {
00090                         fprintf(stderr, "anm: cannot open %s\n", filename);
00091                         continue;
00092                 }
00093                 process(fd);
00094                 close(fd);
00095         }
00096         exit(0);
00097 }
00098 
00099 extern int rd_unsigned2();
00100 extern long lseek();
00101 extern char *strncpy();
00102 
00103 process(fd)
00104         int     fd;
00105 {
00106         unsigned int    magic;
00107         long            nextpos;
00108         struct ar_hdr   archive_header;
00109         static char     buf[sizeof(archive_header.ar_name)+1];
00110 
00111         if (narg > 1) printf("\n%s:\n", filename);
00112 
00113         magic = rd_unsigned2(fd);
00114         switch(magic) {
00115         case O_MAGIC:
00116                 lseek(fd, 0L, 0);
00117                 do_file(fd);
00118                 break;
00119         case ARMAG:
00120         case AALMAG:
00121                 while (rd_arhdr(fd, &archive_header)) {
00122                         nextpos = lseek(fd, 0L, 1) + archive_header.ar_size;
00123                         if (nextpos & 1) nextpos++;
00124                         strncpy(buf,archive_header.ar_name,sizeof(archive_header.ar_name));
00125                         filename = buf;
00126                         if ( strcmp(filename, SYMDEF)) {
00127                                 printf("\n%s:\n", filename);
00128                                 do_file(fd);
00129                         }
00130                         lseek(fd, nextpos, 0);
00131                 }
00132                 break;
00133         default:
00134                 fprintf(stderr, "anm: %s -- bad format\n", filename);
00135                 break;
00136         }
00137 }
00138 
00139 do_file(fd)
00140         int     fd;
00141 {
00142         struct  outname *nbufp = NULL;
00143         struct  outname nbuf;
00144         char            *cbufp;
00145         long            fi_to_co;
00146         long            n;
00147         unsigned        readcount;
00148         int             i,j;
00149         int             compare();
00150 
00151         read_error = 0;
00152         rd_fdopen(fd);
00153 
00154         rd_ohead(&hbuf);
00155         if (read_error) {
00156                 return;
00157         }
00158         if (BADMAGIC(hbuf)) {
00159                 return;
00160         }
00161 
00162         n = hbuf.oh_nname;
00163         if (n == 0) {
00164                 fprintf(stderr, "anm: %s -- no name list\n", filename);
00165                 return;
00166         }
00167 
00168         if (hbuf.oh_nchar == 0) {
00169                 fprintf(stderr, "anm: %s -- no names\n", filename);
00170                 return;
00171         }
00172         if ((readcount = hbuf.oh_nchar) != hbuf.oh_nchar) {
00173                 fprintf(stderr, "anm: string area too big in %s\n", filename);
00174                 exit(2);
00175         }
00176 
00177         /* store special section bases ??? */
00178         if (hbuf.oh_flags & HF_8086) {
00179                 rd_sect(&sbuf, hbuf.oh_nsect);
00180                 if (read_error) {
00181                         return;
00182                 }
00183                 for (i=0; i<hbuf.oh_nsect; i++) {
00184                         s_base[i+S_MIN] =
00185                                 (sbuf.os_base>>12) & 03777760;
00186                 }
00187         }
00188 
00189         if ((cbufp = (char *)malloc(readcount)) == NULL) {
00190                 fprintf(stderr, "anm: out of memory on %s\n", filename);
00191                 exit(2);
00192         }
00193         rd_string(cbufp, hbuf.oh_nchar);
00194         if (read_error) {
00195                 free(cbufp);
00196                 return;
00197         }
00198 
00199         fi_to_co = (long) (cbufp - OFF_CHAR(hbuf));
00200         i = 0;
00201         while (--n >= 0) {
00202                 rd_name(&nbuf, 1);
00203                 if (read_error) {
00204                         break;
00205                 }
00206 
00207                 if (globl_flg && (nbuf.on_type&S_EXT)==0)
00208                         continue;
00209 
00210                 if (undef_flg
00211                     &&
00212                     ((nbuf.on_type&S_TYP)!=S_UND || (nbuf.on_type&S_ETC)!=0))
00213                         continue;
00214 
00215                 if (nbuf.on_foff == 0) nbuf.on_mptr = 0;
00216                 else nbuf.on_mptr = (char *) (nbuf.on_foff + fi_to_co);
00217 
00218                 /* adjust value for specially encoded bases */
00219                 if (hbuf.oh_flags & HF_8086) {
00220                     if (((nbuf.on_type&S_ETC) == 0) ||
00221                         ((nbuf.on_type&S_ETC) == S_SCT)) {
00222                         j = nbuf.on_type&S_TYP;
00223                         if ((j>=S_MIN) && (j<=S_MAX))
00224                             nbuf.on_valu += s_base[j];
00225                     }
00226                 }
00227 
00228                 if (nbufp == NULL)
00229                         nbufp = (struct outname *)malloc(sizeof(struct outname));
00230                 else
00231                         nbufp = (struct outname *)realloc(nbufp, (i+1)*sizeof(struct outname));
00232                 if (nbufp == NULL) {
00233                         fprintf(stderr, "anm: out of memory on %s\n", filename);
00234                         exit(2);
00235                 }
00236                 nbufp[i++] = nbuf;
00237         }
00238 
00239         if (nbufp && nosort_flg==0)
00240                 qsort(nbufp, i, sizeof(struct outname), compare);
00241 
00242         for (n=0; n<i; n++) {
00243                 char    cs1[4];
00244                 char    cs2[4];
00245 
00246                 if (prep_flg)
00247                         printf("%s:", filename);
00248 
00249                 switch(nbufp[n].on_type&S_ETC) {
00250                 case S_SCT:
00251                         sprintf(cs1, "%2d", (nbufp[n].on_type&S_TYP) - S_MIN);
00252                         sprintf(cs2, " S");
00253                         break;
00254                 case S_FIL:
00255                         sprintf(cs1, " -");
00256                         sprintf(cs2, " F");
00257                         break;
00258                 case S_MOD:
00259                         sprintf(cs1, " -");
00260                         sprintf(cs2, " M");
00261                         break;
00262                 case S_COM:
00263                         sprintf(cs1, " C");
00264                         if (nbufp[n].on_type&S_EXT)
00265                                 sprintf(cs2, " E");
00266                         else
00267                                 sprintf(cs2, " -");
00268                         break;
00269                 case 0:
00270                         if (nbufp[n].on_type&S_EXT)
00271                                 sprintf(cs2, " E");
00272                         else
00273                                 sprintf(cs2, " -");
00274 
00275                         switch(nbufp[n].on_type&S_TYP) {
00276                         case S_UND:
00277                                 sprintf(cs1, " U");
00278                                 break;
00279                         case S_ABS:
00280                                 sprintf(cs1, " A");
00281                                 break;
00282                         default:
00283                                 sprintf(cs1, "%2d", (nbufp[n].on_type&S_TYP) - S_MIN);
00284                         }
00285                         break;
00286                 default:
00287                         sprintf(cs1, "??");
00288                         sprintf(cs2, " ?");
00289                 }
00290 
00291                 printf("%8lx %s %s %s\n",nbufp[n].on_valu,cs1,cs2,nbufp[n].on_mptr ? nbufp[n].on_mptr : "(NULL)");
00292         }
00293 
00294         if (nbufp)
00295                 free((char *)nbufp);
00296         if (cbufp)
00297                 free((char *)cbufp);
00298 }
00299 
00300 compare(p1, p2)
00301 struct outname  *p1, *p2;
00302 {
00303         int     i;
00304 
00305         if (sectsort_flg) {
00306                 if ((p1->on_type&S_TYP) > (p2->on_type&S_TYP))
00307                         return(revsort_flg);
00308                 if ((p1->on_type&S_TYP) < (p2->on_type&S_TYP))
00309                         return(-revsort_flg);
00310         }
00311 
00312         if (numsort_flg) {
00313                 if (p1->on_valu > p2->on_valu)
00314                         return(revsort_flg);
00315                 if (p1->on_valu < p2->on_valu)
00316                         return(-revsort_flg);
00317         }
00318 
00319         if (! p1->on_mptr) {
00320                 if (! p2->on_mptr) return 0;
00321                 return -revsort_flg;
00322         }
00323         if (! p2->on_mptr) return revsort_flg;
00324 
00325         i = strcmp(p1->on_mptr, p2->on_mptr);
00326 
00327         if (i > 0)
00328                 return(revsort_flg);
00329         if (i < 0)
00330                 return(-revsort_flg);
00331 
00332         return(0);
00333 }
00334 
00335 rd_fatal()
00336 {
00337         fprintf(stderr,"read error on %s\n", filename);
00338         read_error = 1;
00339 }

Generated on Fri Apr 14 22:56:41 2006 for minix by  doxygen 1.4.6