1 /* size.c -- report size of various sections of an executable file.
2 Copyright (C) 1991-2019 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. */
33 #include "libiberty.h"
41 /* Program options. */
49 /* Select the desired output format. */
56 static enum output_format selected_output_format =
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);
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\
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);
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)
137 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
138 setlocale (LC_MESSAGES, "");
140 #if defined (HAVE_SETLOCALE)
141 setlocale (LC_CTYPE, "");
143 bindtextdomain (PACKAGE, LOCALEDIR);
144 textdomain (PACKAGE);
146 program_name = *argv;
147 xmalloc_set_program_name (program_name);
148 bfd_set_error_program_name (program_name);
150 expandargv (&argc, &argv);
152 if (bfd_init () != BFD_INIT_MAGIC)
153 fatal (_("fatal error: libbfd ABI mismatch"));
154 set_default_bfd_target ();
156 while ((c = getopt_long (argc, argv, "ABGHhVvdfotx", long_options,
165 selected_output_format = FORMAT_BERKLEY;
169 selected_output_format = FORMAT_SYSV;
173 selected_output_format = FORMAT_GNU;
176 non_fatal (_("invalid argument to --format: %s"), optarg);
186 #ifdef ANSI_LIBRARIES
187 temp = strtol (optarg, NULL, 10);
189 temp = atol (optarg);
203 non_fatal (_("Invalid radix: %s\n"), optarg);
209 selected_output_format = FORMAT_SYSV;
212 selected_output_format = FORMAT_BERKLEY;
215 selected_output_format = FORMAT_GNU;
233 case 'f': /* FIXME : For sysv68, `-f' means `full format', i.e.
234 `[fname:] M(.text) + N(.data) + O(.bss) + P(.comment) = Q'
235 where `fname: ' appears only if there are >= 2 input files,
236 and M, N, O, P, Q are expressed in decimal by default,
237 hexa or octal if requested by `-x' or `-o'.
238 Just to make things interesting, Solaris also accepts -f,
239 which prints out the size of each allocatable section, the
240 name of the section, and the total of the section sizes. */
241 /* For the moment, accept `-f' silently, and ignore it. */
252 print_version ("size");
257 display_file ("a.out");
259 for (; optind < argc;)
260 display_file (argv[optind++]);
262 if (show_totals && (selected_output_format == FORMAT_BERKLEY
263 || selected_output_format == FORMAT_GNU))
265 bfd_size_type total = total_textsize + total_datasize + total_bsssize;
266 int col_width = (selected_output_format == FORMAT_BERKLEY) ? 7 : 10;
267 char sep_char = (selected_output_format == FORMAT_BERKLEY) ? '\t' : ' ';
269 rprint_number (col_width, total_textsize);
271 rprint_number (col_width, total_datasize);
273 rprint_number (col_width, total_bsssize);
275 if (selected_output_format == FORMAT_BERKLEY)
276 printf (((radix == octal) ? "%7lo\t%7lx" : "%7lu\t%7lx"),
277 (unsigned long) total, (unsigned long) total);
279 rprint_number (col_width, total);
281 fputs ("(TOTALS)\n", stdout);
287 /* Total size required for common symbols in ABFD. */
290 calculate_common_size (bfd *abfd)
292 asymbol **syms = NULL;
293 long storage, symcount;
296 if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC | HAS_SYMS)) != HAS_SYMS)
299 storage = bfd_get_symtab_upper_bound (abfd);
301 bfd_fatal (bfd_get_filename (abfd));
303 syms = (asymbol **) xmalloc (storage);
305 symcount = bfd_canonicalize_symtab (abfd, syms);
307 bfd_fatal (bfd_get_filename (abfd));
309 while (--symcount >= 0)
311 asymbol *sym = syms[symcount];
313 if (bfd_is_com_section (sym->section)
314 && (sym->flags & BSF_SECTION_SYM) == 0)
315 common_size += sym->value;
320 /* Display stats on file or archive member ABFD. */
323 display_bfd (bfd *abfd)
327 if (bfd_check_format (abfd, bfd_archive))
328 /* An archive within an archive. */
331 if (bfd_check_format_matches (abfd, bfd_object, &matching))
338 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
340 bfd_nonfatal (bfd_get_filename (abfd));
341 list_matching_formats (matching);
347 if (bfd_check_format_matches (abfd, bfd_core, &matching))
349 const char *core_cmd;
352 fputs (" (core file", stdout);
354 core_cmd = bfd_core_file_failing_command (abfd);
356 printf (" invoked as %s", core_cmd);
362 bfd_nonfatal (bfd_get_filename (abfd));
364 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
366 list_matching_formats (matching);
374 display_archive (bfd *file)
376 bfd *arfile = (bfd *) NULL;
377 bfd *last_arfile = (bfd *) NULL;
381 bfd_set_error (bfd_error_no_error);
383 arfile = bfd_openr_next_archived_file (file, arfile);
386 if (bfd_get_error () != bfd_error_no_more_archived_files)
388 bfd_nonfatal (bfd_get_filename (file));
394 display_bfd (arfile);
396 if (last_arfile != NULL)
398 bfd_close (last_arfile);
400 /* PR 17512: file: a244edbc. */
401 if (last_arfile == arfile)
405 last_arfile = arfile;
408 if (last_arfile != NULL)
409 bfd_close (last_arfile);
413 display_file (char *filename)
417 if (get_file_size (filename) < 1)
423 file = bfd_openr (filename, target);
426 bfd_nonfatal (filename);
431 if (bfd_check_format (file, bfd_archive))
432 display_archive (file);
436 if (!bfd_close (file))
438 bfd_nonfatal (filename);
445 size_number (bfd_size_type num)
450 (radix == decimal ? "%" BFD_VMA_FMT "u" :
451 ((radix == octal) ? "0%" BFD_VMA_FMT "o" : "0x%" BFD_VMA_FMT "x")),
454 return strlen (buffer);
458 rprint_number (int width, bfd_size_type num)
463 (radix == decimal ? "%" BFD_VMA_FMT "u" :
464 ((radix == octal) ? "0%" BFD_VMA_FMT "o" : "0x%" BFD_VMA_FMT "x")),
467 printf ("%*s", width, buffer);
470 static bfd_size_type bsssize;
471 static bfd_size_type datasize;
472 static bfd_size_type textsize;
475 berkeley_or_gnu_sum (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec,
476 void *ignore ATTRIBUTE_UNUSED)
481 flags = bfd_get_section_flags (abfd, sec);
482 if ((flags & SEC_ALLOC) == 0)
485 size = bfd_get_section_size (sec);
486 if ((flags & SEC_CODE) != 0
487 || (selected_output_format == FORMAT_BERKLEY
488 && (flags & SEC_READONLY) != 0))
490 else if ((flags & SEC_HAS_CONTENTS) != 0)
497 print_berkeley_or_gnu_format (bfd *abfd)
499 static int files_seen = 0;
501 int col_width = (selected_output_format == FORMAT_BERKLEY) ? 7 : 10;
502 char sep_char = (selected_output_format == FORMAT_BERKLEY) ? '\t' : ' ';
508 bfd_map_over_sections (abfd, berkeley_or_gnu_sum, NULL);
510 bsssize += common_size;
511 if (files_seen++ == 0)
513 if (selected_output_format == FORMAT_BERKLEY)
514 puts ((radix == octal) ? " text\t data\t bss\t oct\t hex\tfilename" :
515 " text\t data\t bss\t dec\t hex\tfilename");
517 puts (" text data bss total filename");
520 total = textsize + datasize + bsssize;
524 total_textsize += textsize;
525 total_datasize += datasize;
526 total_bsssize += bsssize;
529 rprint_number (col_width, textsize);
531 rprint_number (col_width, datasize);
533 rprint_number (col_width, bsssize);
536 if (selected_output_format == FORMAT_BERKLEY)
537 printf (((radix == octal) ? "%7lo\t%7lx" : "%7lu\t%7lx"),
538 (unsigned long) total, (unsigned long) total);
540 rprint_number (col_width, total);
543 fputs (bfd_get_filename (abfd), stdout);
545 if (abfd->my_archive)
546 printf (" (ex %s)", bfd_get_filename (abfd->my_archive));
549 /* I REALLY miss lexical functions! */
550 bfd_size_type svi_total = 0;
551 bfd_vma svi_maxvma = 0;
557 sysv_internal_sizer (bfd *file ATTRIBUTE_UNUSED, sec_ptr sec,
558 void *ignore ATTRIBUTE_UNUSED)
560 bfd_size_type size = bfd_section_size (file, sec);
562 if ( ! bfd_is_abs_section (sec)
563 && ! bfd_is_com_section (sec)
564 && ! bfd_is_und_section (sec))
566 int namelen = strlen (bfd_section_name (file, sec));
568 if (namelen > svi_namelen)
569 svi_namelen = namelen;
573 if (bfd_section_vma (file, sec) > svi_maxvma)
574 svi_maxvma = bfd_section_vma (file, sec);
579 sysv_one_line (const char *name, bfd_size_type size, bfd_vma vma)
581 printf ("%-*s ", svi_namelen, name);
582 rprint_number (svi_sizelen, size);
584 rprint_number (svi_vmalen, vma);
589 sysv_internal_printer (bfd *file ATTRIBUTE_UNUSED, sec_ptr sec,
590 void *ignore ATTRIBUTE_UNUSED)
592 bfd_size_type size = bfd_section_size (file, sec);
594 if ( ! bfd_is_abs_section (sec)
595 && ! bfd_is_com_section (sec)
596 && ! bfd_is_und_section (sec))
600 sysv_one_line (bfd_section_name (file, sec),
602 bfd_section_vma (file, sec));
607 print_sysv_format (bfd *file)
609 /* Size all of the columns. */
613 bfd_map_over_sections (file, sysv_internal_sizer, NULL);
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;
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);
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);
652 print_sizes (bfd *file)
655 calculate_common_size (file);
656 if (selected_output_format == FORMAT_SYSV)
657 print_sysv_format (file);
659 print_berkeley_or_gnu_format (file);