home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / binutils-1.8.x.tar.gz / binutils-1.8.x.tar / binutils / size.c < prev    next >
C/C++ Source or Header  |  1990-06-11  |  9KB  |  388 lines

  1. /* Size of rel file utility (`size') for GNU.
  2.    Copyright (C) 1986 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <ar.h>
  20. #include <sys/types.h>
  21. #include <sys/file.h>
  22.  
  23. #if !defined(A_OUT) && !defined(MACH_O)
  24. #define A_OUT
  25. #endif
  26.  
  27. #ifdef A_OUT
  28. #ifdef COFF_ENCAPSULATE
  29. #include "a.out.encap.h"
  30. #else
  31. /* On native BSD systems, use the system's own a.out.h.  */
  32. #include <a.out.h>
  33. #endif
  34. #endif
  35.  
  36. #ifdef MACH_O
  37. #include <sys/loader.h>
  38. #endif
  39.  
  40. #ifdef USG
  41. #include <fcntl.h>
  42. #include <string.h>
  43. #else
  44. #include <strings.h>
  45. #endif
  46.  
  47. /* Number of input file names specified.  */
  48.  
  49. int number_of_files;
  50.  
  51. /* Current file's name */
  52.  
  53. char *input_name;
  54.  
  55. /* Current member's name, or 0 if processing a non-library file.  */
  56.  
  57. char *input_member;
  58.  
  59. /* Offset within archive of the current member,
  60.    if we are processing an archive.  */
  61.  
  62. int member_offset;
  63.  
  64. char *malloc ();
  65.  
  66. void do_one_file (), do_one_rel_file ();
  67. char *xmalloc ();
  68. char *concat ();
  69.  
  70. main (argc, argv)
  71.      char **argv;
  72.      int argc;
  73. {
  74.   int i;
  75.  
  76.   number_of_files = argc - 1;
  77.  
  78.   printf ("text\tdata\tbss\tdec\thex\n");
  79.  
  80.   /* Now scan and describe the files.  */
  81.  
  82.   if (number_of_files == 0)
  83.     do_one_file ("a.out");
  84.   else
  85.     for (i = 1; i < argc; i++)
  86.       do_one_file (argv[i]);
  87.   exit (0);
  88. }
  89.  
  90. /* Print the filename of the current file on 'outfile' (a stdio stream).  */
  91.  
  92. print_file_name (outfile)
  93.      FILE *outfile;
  94. {
  95.   fprintf (outfile, "%s", input_name);
  96.   if (input_member)
  97.     fprintf (outfile, "(%s)", input_member);
  98. }
  99.  
  100. /* process one input file */
  101. void scan_library ();
  102.  
  103. void
  104. do_one_file (name)
  105.      char *name;
  106. {
  107.   int len, desc;
  108.   char armag[SARMAG];
  109.  
  110.   desc = open (name, O_RDONLY, 0);
  111.  
  112.   if (desc < 0)
  113.     {
  114.       perror_name (name);
  115.       return;
  116.     }
  117.  
  118.   input_name = name;
  119.   input_member = 0;
  120.  
  121.   if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  122.     do_one_rel_file (desc, 0L);
  123.   else
  124.     scan_library (desc);
  125.  
  126.   close (desc);
  127. }
  128.  
  129. /* Read in the archive data about one member.
  130.    subfile_offset is the address within the archive of the start of that data.
  131.    The value returned is the length of the member's contents, which does
  132.    not include the archive data about the member.
  133.  
  134.    If there are no more valid members, zero is returned.  */
  135.  
  136. int
  137. decode_library_subfile (desc, subfile_offset)
  138.      int desc;
  139.      int subfile_offset;
  140. {
  141.   int bytes_read;
  142.   int namelen;
  143.   int member_length;
  144.   char *name;
  145.   struct ar_hdr hdr1;
  146.  
  147.   lseek (desc, subfile_offset, 0);
  148.  
  149.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  150.   if (!bytes_read)
  151.     ;        /* end of archive */
  152.  
  153.   else if (sizeof hdr1 != bytes_read)
  154.     error_with_file ("malformed library archive");
  155.  
  156.   else if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  157.     error_with_file ("malformatted header of archive member");
  158.  
  159.   else
  160.     {
  161.       for (namelen = 0;
  162.        hdr1.ar_name[namelen] != 0
  163.        && hdr1.ar_name[namelen] != ' '
  164.        && hdr1.ar_name[namelen] != '/';
  165.        namelen++)
  166.     ;
  167.       name = (char *) xmalloc (namelen+1);
  168.       strncpy (name, hdr1.ar_name, namelen);
  169.       *(name + namelen) = 0;
  170.  
  171.       input_member = name;
  172.  
  173.       return member_length;
  174.     }
  175.   return 0;   /* tell caller to exit loop */
  176. }
  177.  
  178. /* Scan a library and describe each member.  */
  179.  
  180. void
  181. scan_library (desc)
  182.      int desc;
  183. {
  184.   int this_subfile_offset = SARMAG;
  185.   int member_length;
  186.   
  187.   while (member_length = decode_library_subfile (desc, this_subfile_offset))
  188.     {
  189.       /* describe every member except the ranlib data if any */
  190.       if (strcmp (input_member, "__.SYMDEF"))
  191.     do_one_rel_file (desc, this_subfile_offset + sizeof (struct ar_hdr));
  192.  
  193.       this_subfile_offset += ((member_length + sizeof (struct ar_hdr)) + 1) & -2;
  194.     }
  195. }
  196.  
  197. /* Read a file's header and fill in various pieces of information.
  198.    Return 0 on failure.  */
  199.  
  200. int
  201. read_header_info (desc, offset, tsize, dsize, bsize)
  202.      int desc;
  203.      long int offset;
  204.      unsigned int *tsize;
  205.      unsigned int *dsize;
  206.      unsigned int *bsize;
  207. {
  208.   int len;
  209.  
  210. #ifdef A_OUT
  211.   {
  212.     struct exec hdr;
  213.  
  214.     lseek (desc, offset, 0);
  215. #ifdef HEADER_SEEK_FD
  216.     /* Skip the headers that encapsulate our data in some other format
  217.        such as COFF.  */
  218.     HEADER_SEEK_FD (desc);
  219. #endif
  220.     len = read (desc, (char *) &hdr, sizeof (struct exec));
  221.     if (len == sizeof (struct exec) && !N_BADMAG (hdr))
  222.       {
  223.     *tsize = hdr.a_text;
  224.     *dsize = hdr.a_data;
  225.     *bsize = hdr.a_bss;
  226.     return 1;
  227.       }
  228.   }
  229. #endif
  230.  
  231. #ifdef MACH_O
  232.   {
  233.     struct mach_header mach_header;
  234.     char *hdrbuf;
  235.     struct load_command *load_command;
  236.     struct segment_command *segment_command;
  237.     struct section *section;
  238.     int len, cmd, seg;
  239.  
  240.     lseek (desc, offset, 0);
  241.     len = read (desc, (char *) &mach_header, sizeof (struct mach_header));
  242.     if (len == sizeof (struct mach_header) && mach_header.magic == MH_MAGIC)
  243.       {
  244.     hdrbuf = xmalloc (mach_header.sizeofcmds);
  245.     len = read (desc, hdrbuf, mach_header.sizeofcmds);
  246.     if (len != mach_header.sizeofcmds)
  247.       {
  248.         error_with_file ("failure reading Mach-O load commands");
  249.         return 0;
  250.       }
  251.     load_command = (struct load_command *) hdrbuf;
  252.     for (cmd = 0; cmd < mach_header.ncmds; ++cmd)
  253.       {
  254.         if (load_command->cmd == LC_SEGMENT)
  255.           {
  256.         segment_command = (struct segment_command *) load_command;
  257.         section = (struct section *) ((char *) (segment_command + 1));
  258.         for (seg = 0; seg < segment_command->nsects; ++seg, ++section)
  259.           {
  260.             if (!strncmp(SECT_TEXT, section->sectname, sizeof section->sectname))
  261.               *tsize = section->size;
  262.             else if (!strncmp(SECT_DATA, section->sectname, sizeof section->sectname))
  263.               *dsize = section->size;
  264.             else if (!strncmp(SECT_BSS, section->sectname, sizeof section->sectname))
  265.               *bsize = section->size;
  266.           }
  267.           }
  268.         load_command = (struct load_command *)
  269.           ((char *) load_command + load_command->cmdsize);
  270.       }
  271.     free (hdrbuf);
  272.     return 1;
  273.       }
  274.   }
  275. #endif
  276.  
  277.   return 0;
  278. }
  279.  
  280. void
  281. do_one_rel_file (desc, offset)
  282.      int desc;
  283.      long int offset;
  284. {
  285.   unsigned int tsize, dsize, bsize, total;
  286.  
  287.   if (read_header_info (desc, offset, &tsize, &dsize, &bsize))
  288.     {
  289.       total =  tsize + dsize + bsize;
  290.       printf ("%u\t%u\t%u\t%u\t%x", tsize, dsize, bsize, total, total);
  291.     }
  292.   else
  293.     {
  294.       error_with_file ("malformed input file (not rel or archive)");
  295.       return;
  296.     }
  297.  
  298.   if (number_of_files > 1 || input_member)
  299.     {
  300.       printf ("\t");
  301.       print_file_name (stdout);
  302.     }
  303.   printf ("\n");
  304. }
  305.  
  306. /* Report a fatal error.
  307.    STRING is a printf format string and ARG is one arg for it.  */
  308.  
  309. fatal (string, arg)
  310.      char *string, *arg;
  311. {
  312.   fprintf (stderr, "size: ");
  313.   fprintf (stderr, string, arg);
  314.   fprintf (stderr, "\n");
  315.   exit (1);
  316. }
  317.  
  318. /* Report a nonfatal error.
  319.    STRING is a printf format string and ARG is one arg for it.  */
  320.  
  321. error (string, arg)
  322.      char *string, *arg;
  323. {
  324.   fprintf (stderr, "size: ");
  325.   fprintf (stderr, string, arg);
  326.   fprintf (stderr, "\n");
  327. }
  328.  
  329. /* Report a nonfatal error.
  330.    STRING is printed, followed by the current file name.  */
  331.  
  332. error_with_file (string)
  333.      char *string;
  334. {
  335.   fprintf (stderr, "size: ");
  336.   print_file_name (stderr);
  337.   fprintf (stderr, ": ");
  338.   fprintf (stderr, string);
  339.   fprintf (stderr, "\n");
  340. }
  341.  
  342. /* Report an error using the message for the last failed system call,
  343.    followed by the string NAME.  */
  344.  
  345. perror_name (name)
  346.      char *name;
  347. {
  348.   extern int errno, sys_nerr;
  349.   extern char *sys_errlist[];
  350.   char *s;
  351.  
  352.   if (errno < sys_nerr)
  353.     s = concat (name, ": ", sys_errlist[errno]);
  354.   else
  355.     s = concat (name, ": ", "unknown error");
  356.   error (s, name);
  357. }
  358.  
  359. /* Like malloc but get fatal error if memory is exhausted.  */
  360.  
  361. char *
  362. xmalloc (size)
  363.      int size;
  364. {
  365.   char *result = malloc (size);
  366.   if (!result)
  367.     fatal ("virtual memory exhausted", 0);
  368.   return result;
  369. }
  370.  
  371. /* Return a newly-allocated string
  372.    whose contents concatenate those of S1, S2, S3.  */
  373.  
  374. char *
  375. concat (s1, s2, s3)
  376.      char *s1, *s2, *s3;
  377. {
  378.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  379.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  380.  
  381.   strcpy (result, s1);
  382.   strcpy (result + len1, s2);
  383.   strcpy (result + len1 + len2, s3);
  384.   result[len1 + len2 + len3] = 0;
  385.  
  386.   return result;
  387. }
  388.