]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/size.c
Tidy up formatting of --help output.
[thirdparty/binutils-gdb.git] / binutils / size.c
CommitLineData
252b5132 1/* size.c -- report size of various sections of an executable file.
8b53311e 2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
252b5132
RH
3 Free Software Foundation, Inc.
4
5This file is part of GNU Binutils.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20\f
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 --compatibility or --pedantic option.
30*/
31
32#include "bfd.h"
33#include "getopt.h"
34#include "bucomm.h"
35#include "libiberty.h"
36
37#ifndef BSD_DEFAULT
38#define BSD_DEFAULT 1
39#endif
40
41/* Program options. */
42
43enum
44 {
45 decimal, octal, hex
46 } radix = decimal;
47int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output. */
48int show_version = 0;
49int show_help = 0;
50
51/* Program exit status. */
52int return_code = 0;
53
54static char *target = NULL;
55
56/* Static declarations */
57
58static void usage PARAMS ((FILE *, int));
59static void display_file PARAMS ((char *filename));
60static void display_bfd PARAMS ((bfd *));
61static void display_archive PARAMS ((bfd *));
62static int size_number PARAMS ((bfd_size_type));
63#if 0
64static void lprint_number PARAMS ((int, bfd_size_type));
65#endif
66static void rprint_number PARAMS ((int, bfd_size_type));
67static void print_berkeley_format PARAMS ((bfd *));
68static void sysv_internal_sizer PARAMS ((bfd *, asection *, PTR));
69static void sysv_internal_printer PARAMS ((bfd *, asection *, PTR));
70static void print_sysv_format PARAMS ((bfd *));
71static void print_sizes PARAMS ((bfd * file));
72static void berkeley_sum PARAMS ((bfd *, sec_ptr, PTR));
73\f
74static void
75usage (stream, status)
76 FILE *stream;
77 int status;
78{
8b53311e
NC
79 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
80 fprintf (stream, _(" Displays the sizes of sections inside binary files\n"));
81 fprintf (stream, _(" If no input file(s) are specified, a.out is assumed\n"));
82 fprintf (stream, _(" The options are:\n\
83 -A|-B --format={sysv|berkeley} Select output style (default is %s)\n\
84 -o|-d|-h --radix={8|10|16} Display numbers in octal, decimal or hex\n\
85 --target=<bfdname> Set the binary file format\n\
86 -h --help Display this information\n\
87 -v --version Display the program's version\n\
88\n"),
252b5132 89#if BSD_DEFAULT
8b53311e 90 "berkeley"
252b5132 91#else
8b53311e 92 "sysv"
252b5132 93#endif
8b53311e 94);
252b5132
RH
95 list_supported_targets (program_name, stream);
96 if (status == 0)
8ad3436c 97 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132
RH
98 exit (status);
99}
100
101struct option long_options[] =
102{
103 {"format", required_argument, 0, 200},
104 {"radix", required_argument, 0, 201},
105 {"target", required_argument, 0, 202},
106 {"version", no_argument, &show_version, 1},
107 {"help", no_argument, &show_help, 1},
108 {0, no_argument, 0, 0}
109};
110
65de42c0
TS
111int main PARAMS ((int, char **));
112
252b5132
RH
113int
114main (argc, argv)
115 int argc;
116 char **argv;
117{
118 int temp;
119 int c;
120
121#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
122 setlocale (LC_MESSAGES, "");
3882b010
L
123#endif
124#if defined (HAVE_SETLOCALE)
125 setlocale (LC_CTYPE, "");
252b5132
RH
126#endif
127 bindtextdomain (PACKAGE, LOCALEDIR);
128 textdomain (PACKAGE);
129
130 program_name = *argv;
131 xmalloc_set_program_name (program_name);
132
133 bfd_init ();
134 set_default_bfd_target ();
135
8b53311e 136 while ((c = getopt_long (argc, argv, "ABHhVvdfox", long_options,
252b5132
RH
137 (int *) 0)) != EOF)
138 switch (c)
139 {
140 case 200: /* --format */
141 switch (*optarg)
142 {
143 case 'B':
144 case 'b':
145 berkeley_format = 1;
146 break;
147 case 'S':
148 case 's':
149 berkeley_format = 0;
150 break;
151 default:
37cc8ec1 152 non_fatal (_("invalid argument to --format: %s"), optarg);
252b5132
RH
153 usage (stderr, 1);
154 }
155 break;
156
157 case 202: /* --target */
158 target = optarg;
159 break;
160
161 case 201: /* --radix */
162#ifdef ANSI_LIBRARIES
163 temp = strtol (optarg, NULL, 10);
164#else
165 temp = atol (optarg);
166#endif
167 switch (temp)
168 {
169 case 10:
170 radix = decimal;
171 break;
172 case 8:
173 radix = octal;
174 break;
175 case 16:
176 radix = hex;
177 break;
178 default:
37cc8ec1 179 non_fatal (_("Invalid radix: %s\n"), optarg);
252b5132
RH
180 usage (stderr, 1);
181 }
182 break;
183
184 case 'A':
185 berkeley_format = 0;
186 break;
187 case 'B':
188 berkeley_format = 1;
189 break;
8b53311e 190 case 'v':
252b5132
RH
191 case 'V':
192 show_version = 1;
193 break;
194 case 'd':
195 radix = decimal;
196 break;
197 case 'x':
198 radix = hex;
199 break;
200 case 'o':
201 radix = octal;
202 break;
e3a69612
AM
203 case 'f': /* FIXME : For sysv68, `-f' means `full format', i.e.
204 `[fname:] M(.text) + N(.data) + O(.bss) + P(.comment) = Q'
205 where `fname: ' appears only if there are >= 2 input files,
206 and M, N, O, P, Q are expressed in decimal by default,
207 hexa or octal if requested by `-x' or `-o'.
208 Just to make things interesting, Solaris also accepts -f,
209 which prints out the size of each allocatable section, the
210 name of the section, and the total of the section sizes. */
211 /* For the moment, accept `-f' silently, and ignore it. */
212 break;
252b5132
RH
213 case 0:
214 break;
8b53311e
NC
215 case 'h':
216 case 'H':
252b5132
RH
217 case '?':
218 usage (stderr, 1);
219 }
220
221 if (show_version)
222 print_version ("size");
223 if (show_help)
224 usage (stdout, 0);
225
226 if (optind == argc)
227 display_file ("a.out");
228 else
229 for (; optind < argc;)
230 display_file (argv[optind++]);
231
232 return return_code;
233}
234\f
235/* Display stats on file or archive member ABFD. */
236
237static void
238display_bfd (abfd)
239 bfd *abfd;
240{
241 char **matching;
242
243 if (bfd_check_format (abfd, bfd_archive))
244 /* An archive within an archive. */
245 return;
246
247 if (bfd_check_format_matches (abfd, bfd_object, &matching))
248 {
249 print_sizes (abfd);
250 printf ("\n");
251 return;
252 }
253
254 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
255 {
256 bfd_nonfatal (bfd_get_filename (abfd));
257 list_matching_formats (matching);
258 free (matching);
259 return_code = 3;
260 return;
261 }
262
263 if (bfd_check_format_matches (abfd, bfd_core, &matching))
264 {
265 CONST char *core_cmd;
266
267 print_sizes (abfd);
268 fputs (" (core file", stdout);
269
270 core_cmd = bfd_core_file_failing_command (abfd);
271 if (core_cmd)
272 printf (" invoked as %s", core_cmd);
273
274 puts (")\n");
275 return;
276 }
277
278 bfd_nonfatal (bfd_get_filename (abfd));
279
280 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
281 {
282 list_matching_formats (matching);
283 free (matching);
284 }
285
286 return_code = 3;
287}
288
289static void
290display_archive (file)
291 bfd *file;
292{
293 bfd *arfile = (bfd *) NULL;
294
295 for (;;)
296 {
297 bfd_set_error (bfd_error_no_error);
298
299 arfile = bfd_openr_next_archived_file (file, arfile);
300 if (arfile == NULL)
301 {
302 if (bfd_get_error () != bfd_error_no_more_archived_files)
303 {
304 bfd_nonfatal (bfd_get_filename (file));
305 return_code = 2;
306 }
307 break;
308 }
309
310 display_bfd (arfile);
311 /* Don't close the archive elements; we need them for next_archive */
312 }
313}
314
315static void
316display_file (filename)
317 char *filename;
318{
319 bfd *file = bfd_openr (filename, target);
320 if (file == NULL)
321 {
322 bfd_nonfatal (filename);
323 return_code = 1;
324 return;
325 }
326
327 if (bfd_check_format (file, bfd_archive) == true)
328 display_archive (file);
329 else
330 display_bfd (file);
331
332 if (bfd_close (file) == false)
333 {
334 bfd_nonfatal (filename);
335 return_code = 1;
336 return;
337 }
338}
339\f
340/* This is what lexical functions are for. */
341
342static int
343size_number (num)
344 bfd_size_type num;
345{
346 char buffer[40];
347 sprintf (buffer,
348 (radix == decimal ? "%lu" :
349 ((radix == octal) ? "0%lo" : "0x%lx")),
350 (unsigned long) num);
351
352 return strlen (buffer);
353}
354
355#if 0
356
357/* This is not used. */
358
359static void
360lprint_number (width, num)
361 int width;
362 bfd_size_type num;
363{
364 char buffer[40];
365 sprintf (buffer,
366 (radix == decimal ? "%lu" :
367 ((radix == octal) ? "0%lo" : "0x%lx")),
368 (unsigned long) num);
369
370 printf ("%-*s", width, buffer);
371}
372
373#endif
374
375static void
376rprint_number (width, num)
377 int width;
378 bfd_size_type num;
379{
380 char buffer[40];
381 sprintf (buffer,
382 (radix == decimal ? "%lu" :
383 ((radix == octal) ? "0%lo" : "0x%lx")),
384 (unsigned long) num);
385
386 printf ("%*s", width, buffer);
387}
388
389static bfd_size_type bsssize;
390static bfd_size_type datasize;
391static bfd_size_type textsize;
392
393static void
394berkeley_sum (abfd, sec, ignore)
b4c96d0d 395 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 396 sec_ptr sec;
b4c96d0d 397 PTR ignore ATTRIBUTE_UNUSED;
252b5132
RH
398{
399 flagword flags;
400 bfd_size_type size;
401
402 flags = bfd_get_section_flags (abfd, sec);
403 if ((flags & SEC_ALLOC) == 0)
404 return;
405
406 size = bfd_get_section_size_before_reloc (sec);
407 if ((flags & SEC_CODE) != 0 || (flags & SEC_READONLY) != 0)
408 textsize += size;
409 else if ((flags & SEC_HAS_CONTENTS) != 0)
410 datasize += size;
411 else
412 bsssize += size;
413}
414
415static void
416print_berkeley_format (abfd)
417 bfd *abfd;
418{
419 static int files_seen = 0;
420 bfd_size_type total;
421
422 bsssize = 0;
423 datasize = 0;
424 textsize = 0;
425
426 bfd_map_over_sections (abfd, berkeley_sum, (PTR) NULL);
427
428 if (files_seen++ == 0)
429#if 0
430 /* Intel doesn't like bss/stk because they don't have core files. */
431 puts ((radix == octal) ? " text\t data\tbss/stk\t oct\t hex\tfilename" :
432 " text\t data\tbss/stk\t dec\t hex\tfilename");
433#else
434 puts ((radix == octal) ? " text\t data\t bss\t oct\t hex\tfilename" :
435 " text\t data\t bss\t dec\t hex\tfilename");
436#endif
437
438 total = textsize + datasize + bsssize;
439
440 rprint_number (7, textsize);
441 putchar ('\t');
442 rprint_number (7, datasize);
443 putchar ('\t');
444 rprint_number (7, bsssize);
445 printf (((radix == octal) ? "\t%7lo\t%7lx\t" : "\t%7lu\t%7lx\t"),
446 (unsigned long) total, (unsigned long) total);
447
448 fputs (bfd_get_filename (abfd), stdout);
449 if (bfd_my_archive (abfd))
450 printf (" (ex %s)", bfd_get_filename (bfd_my_archive (abfd)));
451}
452
453/* I REALLY miss lexical functions! */
454bfd_size_type svi_total = 0;
455bfd_vma svi_maxvma = 0;
456int svi_namelen = 0;
457int svi_vmalen = 0;
458int svi_sizelen = 0;
459
460static void
461sysv_internal_sizer (file, sec, ignore)
b4c96d0d 462 bfd *file ATTRIBUTE_UNUSED;
252b5132 463 sec_ptr sec;
b4c96d0d 464 PTR ignore ATTRIBUTE_UNUSED;
252b5132
RH
465{
466 bfd_size_type size = bfd_section_size (file, sec);
467 if (!bfd_is_abs_section (sec)
468 && !bfd_is_com_section (sec)
469 && !bfd_is_und_section (sec))
470 {
471 int namelen = strlen (bfd_section_name (file, sec));
472 if (namelen > svi_namelen)
473 svi_namelen = namelen;
474
475 svi_total += size;
476 if (bfd_section_vma (file, sec) > svi_maxvma)
477 svi_maxvma = bfd_section_vma (file, sec);
478 }
479}
480
481static void
482sysv_internal_printer (file, sec, ignore)
b4c96d0d 483 bfd *file ATTRIBUTE_UNUSED;
252b5132 484 sec_ptr sec;
b4c96d0d 485 PTR ignore ATTRIBUTE_UNUSED;
252b5132
RH
486{
487 bfd_size_type size = bfd_section_size (file, sec);
488 if (!bfd_is_abs_section (sec)
489 && !bfd_is_com_section (sec)
490 && !bfd_is_und_section (sec))
491 {
492 svi_total += size;
493
494 printf ("%-*s ", svi_namelen, bfd_section_name (file, sec));
495 rprint_number (svi_sizelen, size);
496 printf (" ");
497 rprint_number (svi_vmalen, bfd_section_vma (file, sec));
498 printf ("\n");
499 }
500}
501
502static void
503print_sysv_format (file)
504 bfd *file;
505{
506 /* size all of the columns */
507 svi_total = 0;
508 svi_maxvma = 0;
509 svi_namelen = 0;
510 bfd_map_over_sections (file, sysv_internal_sizer, (PTR) NULL);
511 svi_vmalen = size_number ((bfd_size_type)svi_maxvma);
512 if ((size_t) svi_vmalen < sizeof ("addr") - 1)
513 svi_vmalen = sizeof ("addr")-1;
514
515 svi_sizelen = size_number (svi_total);
516 if ((size_t) svi_sizelen < sizeof ("size") - 1)
517 svi_sizelen = sizeof ("size")-1;
518
519 svi_total = 0;
520 printf ("%s ", bfd_get_filename (file));
521 if (bfd_my_archive (file))
522 printf (" (ex %s)", bfd_get_filename (bfd_my_archive (file)));
523
524 printf (":\n%-*s %*s %*s\n", svi_namelen, "section",
525 svi_sizelen, "size", svi_vmalen, "addr");
526 bfd_map_over_sections (file, sysv_internal_printer, (PTR) NULL);
527
528 printf ("%-*s ", svi_namelen, "Total");
529 rprint_number (svi_sizelen, svi_total);
530 printf ("\n\n");
531}
532
533static void
534print_sizes (file)
535 bfd *file;
536{
537 if (berkeley_format)
538 print_berkeley_format (file);
539 else
540 print_sysv_format (file);
541}