Clean up usage messages and option parsers.
[external/binutils.git] / binutils / size.c
1 /* size.c -- report size of various sections of an executable file.
2    Copyright 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 /* Extensions/incompatibilities:
22    o - BSD output has filenames at the end.
23    o - BSD output can appear in different radicies.
24    o - SysV output has less redundant whitespace.  Filename comes at end.
25    o - SysV output doesn't show VMA which is always the same as the PMA.
26    o - We also handle core files.
27    o - We also handle archives.
28    If you write shell scripts which manipulate this info then you may be
29    out of luck; there's no --predantic option.
30 */
31
32 #include "bfd.h"
33 #include "sysdep.h"
34 #include "getopt.h"
35
36 #ifndef BSD_DEFAULT
37 #define BSD_DEFAULT 1
38 #endif
39
40 /* Various program options */
41
42 enum {decimal, octal, hex} radix = decimal;
43 int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output */
44 int show_version = 0;
45 int show_help = 0;
46
47 int return_code = 0;
48
49 /* IMPORTS */
50 extern char *program_version;
51 extern char *program_name;
52 extern char *target;
53
54 /* Forward declarations */
55
56 static void
57 display_file PARAMS ((char *filename));
58
59 static void
60 print_sizes PARAMS ((bfd *file));
61 \f
62 /** main and like trivia */
63
64 void
65 usage ()
66 {
67   fprintf (stderr, "size %s\n\
68 Usage: %s [-ABdoxV] [--format=berkeley|sysv] [--radix=8|10|16]\n\
69        [--target=bfdname] [--version] [--help] [file...]\n",
70            program_version, program_name);
71 #if BSD_DEFAULT
72   fputs ("       (default is --format=berkeley)\n", stderr);
73 #else
74   fputs ("       (default is --format=sysv)\n", stderr);
75 #endif
76   exit (1);
77 }
78
79 struct option long_options[] = {
80   {"format",  required_argument, 0, 200},
81   {"radix",   required_argument, 0, 201},
82   {"target",  required_argument, 0, 202},
83   {"version", no_argument, &show_version, 1},
84   {"help",    no_argument, &show_help, 1},
85   {0, no_argument, 0, 0}
86 };
87
88 int
89 main (argc, argv)
90      int argc;
91      char **argv;
92 {
93   int temp;
94   int c;                        /* sez which option char */
95   extern int optind;            /* steps thru options */
96
97   program_name = *argv;
98
99   bfd_init();
100
101   while ((c = getopt_long(argc, argv, "ABVdox", long_options,
102                           (int *) 0)) != EOF)
103     switch(c) {
104     case 200:                   /* --format */
105         switch(*optarg) {
106         case 'B': case 'b': berkeley_format = 1; break;
107         case 'S': case 's': berkeley_format = 0; break;
108         default: fprintf(stderr, "invalid argument to --format: %s\n", optarg);
109           usage();
110         }
111         break;
112
113       case 202:                 /* --target */
114         target = optarg;
115         break;
116
117       case 201:                 /* --radix */
118 #ifdef ANSI_LIBRARIES
119         temp = strtol(optarg, NULL, 10);
120 #else
121         temp = atol(optarg);
122 #endif
123         switch(temp) {
124         case 10: radix = decimal; break;
125         case 8:  radix = octal; break;
126         case 16: radix = hex; break;
127         default: printf("Unknown radix: %s\n", optarg);
128           usage();
129         }
130         break;
131
132     case 'A': berkeley_format = 0; break;
133     case 'B': berkeley_format = 1; break;
134     case 'V': show_version = 1; break;
135     case 'd': radix = decimal; break;
136     case 'x': radix = hex; break;
137     case 'o': radix = octal; break;
138     case '?': usage();
139     }
140
141   if (show_version) printf("%s version %s\n", program_name, program_version);
142   if (show_help) usage();
143         
144   if (optind == argc)
145     display_file ("a.out");
146   else
147     for (; optind < argc;)
148       display_file (argv[optind++]);
149
150   return return_code;
151 }
152 \f
153 /** Display a file's stats */
154
155 void
156 display_bfd (abfd)
157      bfd *abfd;
158 {
159   CONST  char *core_cmd;
160
161   if (bfd_check_format(abfd, bfd_archive)) return;
162
163   if (bfd_check_format(abfd, bfd_object)) {
164     print_sizes(abfd);
165     goto done;
166   }
167
168   if (bfd_check_format(abfd, bfd_core)) {
169     print_sizes(abfd);
170     fputs(" (core file", stdout);
171
172     core_cmd = bfd_core_file_failing_command(abfd);
173     if (core_cmd) printf(" invoked as %s", core_cmd);
174
175     puts(")");
176     goto done;
177   }
178   
179   printf("Unknown file format: %s.", bfd_get_filename(abfd));
180   return_code = 3;
181
182  done:
183
184
185   printf("\n");
186   return;
187 }
188
189 static void
190 display_file(filename)
191      char *filename;
192 {
193   bfd *file, *arfile = (bfd *) NULL;
194
195   file = bfd_openr (filename, target);
196   if (file == NULL) {
197     fprintf (stderr, "%s: ", program_name);
198     bfd_perror (filename);
199     return_code = 1;
200     return;
201   }
202
203   if (bfd_check_format(file, bfd_archive) == true) {
204     for(;;) {
205       
206       bfd_error = no_error;
207
208        arfile = bfd_openr_next_archived_file (file, arfile);
209       if (arfile == NULL) {
210         if (bfd_error != no_more_archived_files) {
211           fprintf (stderr, "%s: ", program_name);
212           bfd_perror (bfd_get_filename (file));
213           return_code = 2;
214         }
215         return;
216       }
217
218       display_bfd (arfile);
219       /* Don't close the archive elements; we need them for next_archive */
220     }
221   }
222   else
223     display_bfd (file);
224
225   bfd_close (file);
226 }
227 \f
228 /* This is what lexical functions are for */
229 void
230 lprint_number (width, num)
231      int width;
232      bfd_size_type num;
233 {
234   printf ((radix == decimal ? "%-*lu\t" :
235            ((radix == octal) ? "%-*lo\t" : "%-*lx\t")),
236           width, (unsigned long)num);
237 }
238
239 void
240 rprint_number(width, num)
241      int width;
242      bfd_size_type num;
243 {
244   printf ((radix == decimal ? "%*lu\t" :
245            ((radix == octal) ? "%*lo\t" : "%*lx\t")),
246           width, (unsigned long)num);
247 }
248
249 static char *bss_section_name = ".bss";
250 static char *data_section_name = ".data";
251 static char *stack_section_name = ".stack";
252 static char *text_section_name = ".text";
253
254 void print_berkeley_format(abfd)
255 bfd *abfd;
256 {
257   static int files_seen = 0;
258   sec_ptr bsssection = NULL;
259   sec_ptr datasection = NULL;
260   sec_ptr textsection = NULL;
261   bfd_size_type bsssize = 0;
262   bfd_size_type datasize = 0;
263   bfd_size_type textsize = 0;
264   bfd_size_type total = 0;
265
266   
267   if ((textsection = bfd_get_section_by_name (abfd, text_section_name))
268       != NULL) {
269     textsize = bfd_get_section_size_before_reloc (textsection);
270   }
271
272   if ((datasection = bfd_get_section_by_name (abfd, data_section_name))
273       != NULL) {
274     datasize = bfd_get_section_size_before_reloc ( datasection);
275   }
276         
277   if (bfd_get_format (abfd) == bfd_object) {
278     if ((bsssection = bfd_get_section_by_name (abfd, bss_section_name))
279         != NULL) {
280       bsssize = bfd_section_size(abfd, bsssection);
281     }
282   } else {
283     if ((bsssection = bfd_get_section_by_name (abfd, stack_section_name))
284         != NULL) {
285       bsssize = bfd_section_size(abfd, bsssection);
286     }
287   }
288
289   if (files_seen++ == 0)
290 #if 0   /* intel doesn't like bss/stk b/c they don't gave core files */
291     puts((radix == octal) ? "text\tdata\tbss/stk\toct\thex\tfilename" :
292          "text\tdata\tbss/stk\tdec\thex\tfilename");
293 #else
294     puts((radix == octal) ? "text\tdata\tbss\toct\thex\tfilename" :
295          "text\tdata\tbss\tdec\thex\tfilename");
296 #endif
297         
298   total = textsize + datasize + bsssize;
299         
300   lprint_number (7, textsize);
301   lprint_number (7, datasize);
302   lprint_number (7, bsssize);
303   printf (((radix == octal) ? "%-7lo\t%-7lx\t" : "%-7lu\t%-7lx\t"),
304           (unsigned long)total, (unsigned long)total);
305
306   fputs(bfd_get_filename(abfd), stdout);
307   if (abfd->my_archive) printf (" (ex %s)", abfd->my_archive->filename);
308 }
309
310 /* I REALLY miss lexical functions! */
311 bfd_size_type svi_total = 0;
312
313 void
314 sysv_internal_printer(file, sec, ignore)
315      bfd *file;
316      sec_ptr sec;
317      PTR ignore;
318 {
319   bfd_size_type size = bfd_section_size (file, sec);
320   if (sec!= &bfd_abs_section 
321       && ! bfd_is_com_section (sec)
322       && sec!=&bfd_und_section) 
323   {
324   
325     svi_total += size;
326         
327     printf ("%-12s", bfd_section_name(file, sec));
328     rprint_number (8, size);
329     printf(" ");
330     rprint_number (8, bfd_section_vma(file, sec));
331     printf ("\n");
332   }
333
334 }
335
336 void
337 print_sysv_format(file)
338      bfd *file;
339 {
340   svi_total = 0;
341
342   printf ("%s  ", bfd_get_filename (file));
343   if (file->my_archive) printf (" (ex %s)", file->my_archive->filename);
344
345   puts(":\nsection\t\tsize\t     addr");
346   bfd_map_over_sections (file, sysv_internal_printer, (PTR)NULL);
347
348   printf("Total       ");
349   rprint_number(8, svi_total);
350   printf("\n");  printf("\n");
351 }
352
353 static void
354 print_sizes(file)
355      bfd *file;
356 {
357   if (berkeley_format)
358     print_berkeley_format(file);
359   else print_sysv_format(file);
360 }