Automatic date update in version.in
[binutils-gdb.git] / binutils / size.c
blob9bdafcb7ac54bfe2e8ea9c78e964cf8e38e7fe7d
1 /* size.c -- report size of various sections of an executable file.
2 Copyright (C) 1991-2022 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
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 3 of the License, or
9 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
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. */
31 #include "sysdep.h"
32 #include "bfd.h"
33 #include "libiberty.h"
34 #include "getopt.h"
35 #include "bucomm.h"
37 #ifndef BSD_DEFAULT
38 #define BSD_DEFAULT 1
39 #endif
41 /* Program options. */
43 static enum
45 decimal, octal, hex
47 radix = decimal;
49 /* Select the desired output format. */
50 enum output_format
52 FORMAT_BERKLEY,
53 FORMAT_SYSV,
54 FORMAT_GNU
56 static enum output_format selected_output_format =
57 #if BSD_DEFAULT
58 FORMAT_BERKLEY
59 #else
60 FORMAT_SYSV
61 #endif
64 static int show_version = 0;
65 static int show_help = 0;
66 static int show_totals = 0;
67 static int show_common = 0;
69 static bfd_size_type common_size;
70 static bfd_size_type total_bsssize;
71 static bfd_size_type total_datasize;
72 static bfd_size_type total_textsize;
74 /* Program exit status. */
75 static int return_code = 0;
77 static char *target = NULL;
79 /* Forward declarations. */
81 static void display_file (char *);
82 static void rprint_number (int, bfd_size_type);
83 static void print_sizes (bfd * file);
85 static void
86 usage (FILE *stream, int status)
88 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
89 fprintf (stream, _(" Displays the sizes of sections inside binary files\n"));
90 fprintf (stream, _(" If no input file(s) are specified, a.out is assumed\n"));
91 fprintf (stream, _(" The options are:\n\
92 -A|-B|-G --format={sysv|berkeley|gnu} Select output style (default is %s)\n\
93 -o|-d|-x --radix={8|10|16} Display numbers in octal, decimal or hex\n\
94 -t --totals Display the total sizes (Berkeley only)\n\
95 --common Display total size for *COM* syms\n\
96 --target=<bfdname> Set the binary file format\n\
97 @<file> Read options from <file>\n\
98 -h --help Display this information\n\
99 -v --version Display the program's version\n\
100 \n"),
101 #if BSD_DEFAULT
102 "berkeley"
103 #else
104 "sysv"
105 #endif
107 list_supported_targets (program_name, stream);
108 if (REPORT_BUGS_TO[0] && status == 0)
109 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
110 exit (status);
113 #define OPTION_FORMAT (200)
114 #define OPTION_RADIX (OPTION_FORMAT + 1)
115 #define OPTION_TARGET (OPTION_RADIX + 1)
117 static struct option long_options[] =
119 {"common", no_argument, &show_common, 1},
120 {"format", required_argument, 0, OPTION_FORMAT},
121 {"radix", required_argument, 0, OPTION_RADIX},
122 {"target", required_argument, 0, OPTION_TARGET},
123 {"totals", no_argument, &show_totals, 1},
124 {"version", no_argument, &show_version, 1},
125 {"help", no_argument, &show_help, 1},
126 {0, no_argument, 0, 0}
129 int main (int, char **);
132 main (int argc, char **argv)
134 int temp;
135 int c;
137 #ifdef HAVE_LC_MESSAGES
138 setlocale (LC_MESSAGES, "");
139 #endif
140 setlocale (LC_CTYPE, "");
141 bindtextdomain (PACKAGE, LOCALEDIR);
142 textdomain (PACKAGE);
144 program_name = *argv;
145 xmalloc_set_program_name (program_name);
146 bfd_set_error_program_name (program_name);
148 expandargv (&argc, &argv);
150 if (bfd_init () != BFD_INIT_MAGIC)
151 fatal (_("fatal error: libbfd ABI mismatch"));
152 set_default_bfd_target ();
154 while ((c = getopt_long (argc, argv, "ABGHhVvdfotx", long_options,
155 (int *) 0)) != EOF)
156 switch (c)
158 case OPTION_FORMAT:
159 switch (*optarg)
161 case 'B':
162 case 'b':
163 selected_output_format = FORMAT_BERKLEY;
164 break;
165 case 'S':
166 case 's':
167 selected_output_format = FORMAT_SYSV;
168 break;
169 case 'G':
170 case 'g':
171 selected_output_format = FORMAT_GNU;
172 break;
173 default:
174 non_fatal (_("invalid argument to --format: %s"), optarg);
175 usage (stderr, 1);
177 break;
179 case OPTION_TARGET:
180 target = optarg;
181 break;
183 case OPTION_RADIX:
184 #ifdef ANSI_LIBRARIES
185 temp = strtol (optarg, NULL, 10);
186 #else
187 temp = atol (optarg);
188 #endif
189 switch (temp)
191 case 10:
192 radix = decimal;
193 break;
194 case 8:
195 radix = octal;
196 break;
197 case 16:
198 radix = hex;
199 break;
200 default:
201 non_fatal (_("Invalid radix: %s\n"), optarg);
202 usage (stderr, 1);
204 break;
206 case 'A':
207 selected_output_format = FORMAT_SYSV;
208 break;
209 case 'B':
210 selected_output_format = FORMAT_BERKLEY;
211 break;
212 case 'G':
213 selected_output_format = FORMAT_GNU;
214 break;
215 case 'v':
216 case 'V':
217 show_version = 1;
218 break;
219 case 'd':
220 radix = decimal;
221 break;
222 case 'x':
223 radix = hex;
224 break;
225 case 'o':
226 radix = octal;
227 break;
228 case 't':
229 show_totals = 1;
230 break;
231 case 'f': /* FIXME : For sysv68, `-f' means `full format', i.e.
232 `[fname:] M(.text) + N(.data) + O(.bss) + P(.comment) = Q'
233 where `fname: ' appears only if there are >= 2 input files,
234 and M, N, O, P, Q are expressed in decimal by default,
235 hexa or octal if requested by `-x' or `-o'.
236 Just to make things interesting, Solaris also accepts -f,
237 which prints out the size of each allocatable section, the
238 name of the section, and the total of the section sizes. */
239 /* For the moment, accept `-f' silently, and ignore it. */
240 break;
241 case 0:
242 break;
243 case 'h':
244 case 'H':
245 case '?':
246 usage (stderr, 1);
249 if (show_version)
250 print_version ("size");
251 if (show_help)
252 usage (stdout, 0);
254 if (optind == argc)
255 display_file ("a.out");
256 else
257 for (; optind < argc;)
258 display_file (argv[optind++]);
260 if (show_totals && (selected_output_format == FORMAT_BERKLEY
261 || selected_output_format == FORMAT_GNU))
263 bfd_size_type total = total_textsize + total_datasize + total_bsssize;
264 int col_width = (selected_output_format == FORMAT_BERKLEY) ? 7 : 10;
265 char sep_char = (selected_output_format == FORMAT_BERKLEY) ? '\t' : ' ';
267 rprint_number (col_width, total_textsize);
268 putchar(sep_char);
269 rprint_number (col_width, total_datasize);
270 putchar(sep_char);
271 rprint_number (col_width, total_bsssize);
272 putchar(sep_char);
273 if (selected_output_format == FORMAT_BERKLEY)
274 printf (((radix == octal) ? "%7lo\t%7lx" : "%7lu\t%7lx"),
275 (unsigned long) total, (unsigned long) total);
276 else
277 rprint_number (col_width, total);
278 putchar(sep_char);
279 fputs ("(TOTALS)\n", stdout);
282 return return_code;
285 /* Total size required for common symbols in ABFD. */
287 static void
288 calculate_common_size (bfd *abfd)
290 asymbol **syms = NULL;
291 long storage, symcount;
293 common_size = 0;
294 if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC | HAS_SYMS)) != HAS_SYMS)
295 return;
297 storage = bfd_get_symtab_upper_bound (abfd);
298 if (storage < 0)
299 bfd_fatal (bfd_get_filename (abfd));
300 if (storage)
301 syms = (asymbol **) xmalloc (storage);
303 symcount = bfd_canonicalize_symtab (abfd, syms);
304 if (symcount < 0)
305 bfd_fatal (bfd_get_filename (abfd));
307 while (--symcount >= 0)
309 asymbol *sym = syms[symcount];
311 if (bfd_is_com_section (sym->section)
312 && (sym->flags & BSF_SECTION_SYM) == 0)
313 common_size += sym->value;
315 free (syms);
318 /* Display stats on file or archive member ABFD. */
320 static void
321 display_bfd (bfd *abfd)
323 char **matching;
325 if (bfd_check_format (abfd, bfd_archive))
326 /* An archive within an archive. */
327 return;
329 if (bfd_check_format_matches (abfd, bfd_object, &matching))
331 print_sizes (abfd);
332 printf ("\n");
333 return;
336 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
338 bfd_nonfatal (bfd_get_filename (abfd));
339 list_matching_formats (matching);
340 return_code = 3;
341 return;
344 if (bfd_check_format_matches (abfd, bfd_core, &matching))
346 const char *core_cmd;
348 print_sizes (abfd);
349 fputs (" (core file", stdout);
351 core_cmd = bfd_core_file_failing_command (abfd);
352 if (core_cmd)
353 printf (" invoked as %s", core_cmd);
355 puts (")\n");
356 return;
359 bfd_nonfatal (bfd_get_filename (abfd));
361 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
362 list_matching_formats (matching);
364 return_code = 3;
367 static void
368 display_archive (bfd *file)
370 bfd *arfile = (bfd *) NULL;
371 bfd *last_arfile = (bfd *) NULL;
373 for (;;)
375 bfd_set_error (bfd_error_no_error);
377 arfile = bfd_openr_next_archived_file (file, arfile);
378 if (arfile == NULL)
380 if (bfd_get_error () != bfd_error_no_more_archived_files)
382 bfd_nonfatal (bfd_get_filename (file));
383 return_code = 2;
385 break;
388 display_bfd (arfile);
390 if (last_arfile != NULL)
392 bfd_close (last_arfile);
394 /* PR 17512: file: a244edbc. */
395 if (last_arfile == arfile)
396 return;
399 last_arfile = arfile;
402 if (last_arfile != NULL)
403 bfd_close (last_arfile);
406 static void
407 display_file (char *filename)
409 bfd *file;
411 if (get_file_size (filename) < 1)
413 return_code = 1;
414 return;
417 file = bfd_openr (filename, target);
418 if (file == NULL)
420 bfd_nonfatal (filename);
421 return_code = 1;
422 return;
425 if (bfd_check_format (file, bfd_archive))
426 display_archive (file);
427 else
428 display_bfd (file);
430 if (!bfd_close (file))
432 bfd_nonfatal (filename);
433 return_code = 1;
434 return;
438 static int
439 size_number (bfd_size_type num)
441 char buffer[40];
443 sprintf (buffer, (radix == decimal ? "%" PRIu64
444 : radix == octal ? "0%" PRIo64 : "0x%" PRIx64),
445 (uint64_t) num);
447 return strlen (buffer);
450 static void
451 rprint_number (int width, bfd_size_type num)
453 char buffer[40];
455 sprintf (buffer, (radix == decimal ? "%" PRIu64
456 : radix == octal ? "0%" PRIo64 : "0x%" PRIx64),
457 (uint64_t) num);
459 printf ("%*s", width, buffer);
462 static bfd_size_type bsssize;
463 static bfd_size_type datasize;
464 static bfd_size_type textsize;
466 static void
467 berkeley_or_gnu_sum (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec,
468 void *ignore ATTRIBUTE_UNUSED)
470 flagword flags;
471 bfd_size_type size;
473 flags = bfd_section_flags (sec);
474 if ((flags & SEC_ALLOC) == 0)
475 return;
477 size = bfd_section_size (sec);
478 if ((flags & SEC_CODE) != 0
479 || (selected_output_format == FORMAT_BERKLEY
480 && (flags & SEC_READONLY) != 0))
481 textsize += size;
482 else if ((flags & SEC_HAS_CONTENTS) != 0)
483 datasize += size;
484 else
485 bsssize += size;
488 static void
489 print_berkeley_or_gnu_format (bfd *abfd)
491 static int files_seen = 0;
492 bfd_size_type total;
493 int col_width = (selected_output_format == FORMAT_BERKLEY) ? 7 : 10;
494 char sep_char = (selected_output_format == FORMAT_BERKLEY) ? '\t' : ' ';
496 bsssize = 0;
497 datasize = 0;
498 textsize = 0;
500 bfd_map_over_sections (abfd, berkeley_or_gnu_sum, NULL);
502 bsssize += common_size;
503 if (files_seen++ == 0)
505 if (selected_output_format == FORMAT_BERKLEY)
506 puts ((radix == octal) ? " text\t data\t bss\t oct\t hex\tfilename" :
507 " text\t data\t bss\t dec\t hex\tfilename");
508 else
509 puts (" text data bss total filename");
512 total = textsize + datasize + bsssize;
514 if (show_totals)
516 total_textsize += textsize;
517 total_datasize += datasize;
518 total_bsssize += bsssize;
521 rprint_number (col_width, textsize);
522 putchar (sep_char);
523 rprint_number (col_width, datasize);
524 putchar (sep_char);
525 rprint_number (col_width, bsssize);
526 putchar (sep_char);
528 if (selected_output_format == FORMAT_BERKLEY)
529 printf (((radix == octal) ? "%7lo\t%7lx" : "%7lu\t%7lx"),
530 (unsigned long) total, (unsigned long) total);
531 else
532 rprint_number (col_width, total);
534 putchar (sep_char);
535 fputs (bfd_get_filename (abfd), stdout);
537 if (abfd->my_archive)
538 printf (" (ex %s)", bfd_get_filename (abfd->my_archive));
541 /* I REALLY miss lexical functions! */
542 bfd_size_type svi_total = 0;
543 bfd_vma svi_maxvma = 0;
544 int svi_namelen = 0;
545 int svi_vmalen = 0;
546 int svi_sizelen = 0;
548 static void
549 sysv_internal_sizer (bfd *file ATTRIBUTE_UNUSED, sec_ptr sec,
550 void *ignore ATTRIBUTE_UNUSED)
552 flagword flags = bfd_section_flags (sec);
553 /* Exclude sections with no flags set. This is to omit som spaces. */
554 if (flags == 0)
555 return;
557 if ( ! bfd_is_abs_section (sec)
558 && ! bfd_is_com_section (sec)
559 && ! bfd_is_und_section (sec))
561 bfd_size_type size = bfd_section_size (sec);
562 int namelen = strlen (bfd_section_name (sec));
564 if (namelen > svi_namelen)
565 svi_namelen = namelen;
567 svi_total += size;
569 if (bfd_section_vma (sec) > svi_maxvma)
570 svi_maxvma = bfd_section_vma (sec);
574 static void
575 sysv_one_line (const char *name, bfd_size_type size, bfd_vma vma)
577 printf ("%-*s ", svi_namelen, name);
578 rprint_number (svi_sizelen, size);
579 printf (" ");
580 rprint_number (svi_vmalen, vma);
581 printf ("\n");
584 static void
585 sysv_internal_printer (bfd *file ATTRIBUTE_UNUSED, sec_ptr sec,
586 void *ignore ATTRIBUTE_UNUSED)
588 flagword flags = bfd_section_flags (sec);
589 if (flags == 0)
590 return;
592 if ( ! bfd_is_abs_section (sec)
593 && ! bfd_is_com_section (sec)
594 && ! bfd_is_und_section (sec))
596 bfd_size_type size = bfd_section_size (sec);
598 svi_total += size;
600 sysv_one_line (bfd_section_name (sec),
601 size,
602 bfd_section_vma (sec));
606 static void
607 print_sysv_format (bfd *file)
609 /* Size all of the columns. */
610 svi_total = 0;
611 svi_maxvma = 0;
612 svi_namelen = 0;
613 bfd_map_over_sections (file, sysv_internal_sizer, NULL);
614 if (show_common)
616 if (svi_namelen < (int) sizeof ("*COM*") - 1)
617 svi_namelen = sizeof ("*COM*") - 1;
618 svi_total += common_size;
621 svi_vmalen = size_number ((bfd_size_type)svi_maxvma);
623 if ((size_t) svi_vmalen < sizeof ("addr") - 1)
624 svi_vmalen = sizeof ("addr")-1;
626 svi_sizelen = size_number (svi_total);
627 if ((size_t) svi_sizelen < sizeof ("size") - 1)
628 svi_sizelen = sizeof ("size")-1;
630 svi_total = 0;
631 printf ("%s ", bfd_get_filename (file));
633 if (file->my_archive)
634 printf (" (ex %s)", bfd_get_filename (file->my_archive));
636 printf (":\n%-*s %*s %*s\n", svi_namelen, "section",
637 svi_sizelen, "size", svi_vmalen, "addr");
639 bfd_map_over_sections (file, sysv_internal_printer, NULL);
640 if (show_common)
642 svi_total += common_size;
643 sysv_one_line ("*COM*", common_size, 0);
646 printf ("%-*s ", svi_namelen, "Total");
647 rprint_number (svi_sizelen, svi_total);
648 printf ("\n\n");
651 static void
652 print_sizes (bfd *file)
654 if (show_common)
655 calculate_common_size (file);
656 if (selected_output_format == FORMAT_SYSV)
657 print_sysv_format (file);
658 else
659 print_berkeley_or_gnu_format (file);