Automatic date update in version.in
[binutils-gdb.git] / binutils / size.c
blobff3db5b57c25ffce49bf661fb676ac80cb274c8d
1 /* size.c -- report size of various sections of an executable file.
2 Copyright (C) 1991-2024 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 -f Ignored.\n\
96 --common Display total size for *COM* syms\n\
97 --target=<bfdname> Set the binary file format\n\
98 @<file> Read options from <file>\n\
99 -h|-H|-? --help Display this information\n\
100 -v|-V --version Display the program's version\n\
101 \n"),
102 #if BSD_DEFAULT
103 "berkeley"
104 #else
105 "sysv"
106 #endif
108 list_supported_targets (program_name, stream);
109 if (REPORT_BUGS_TO[0] && status == 0)
110 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
111 exit (status);
114 #define OPTION_FORMAT (200)
115 #define OPTION_RADIX (OPTION_FORMAT + 1)
116 #define OPTION_TARGET (OPTION_RADIX + 1)
118 static struct option long_options[] =
120 {"common", no_argument, &show_common, 1},
121 {"format", required_argument, 0, OPTION_FORMAT},
122 {"radix", required_argument, 0, OPTION_RADIX},
123 {"target", required_argument, 0, OPTION_TARGET},
124 {"totals", no_argument, &show_totals, 1},
125 {"version", no_argument, &show_version, 1},
126 {"help", no_argument, &show_help, 1},
127 {0, no_argument, 0, 0}
130 int main (int, char **);
133 main (int argc, char **argv)
135 int temp;
136 int c;
138 #ifdef HAVE_LC_MESSAGES
139 setlocale (LC_MESSAGES, "");
140 #endif
141 setlocale (LC_CTYPE, "");
142 bindtextdomain (PACKAGE, LOCALEDIR);
143 textdomain (PACKAGE);
145 program_name = *argv;
146 xmalloc_set_program_name (program_name);
147 bfd_set_error_program_name (program_name);
149 expandargv (&argc, &argv);
151 if (bfd_init () != BFD_INIT_MAGIC)
152 fatal (_("fatal error: libbfd ABI mismatch"));
153 set_default_bfd_target ();
155 while ((c = getopt_long (argc, argv, "ABGHhVvdfotx", long_options,
156 (int *) 0)) != EOF)
157 switch (c)
159 case OPTION_FORMAT:
160 switch (*optarg)
162 case 'B':
163 case 'b':
164 selected_output_format = FORMAT_BERKLEY;
165 break;
166 case 'S':
167 case 's':
168 selected_output_format = FORMAT_SYSV;
169 break;
170 case 'G':
171 case 'g':
172 selected_output_format = FORMAT_GNU;
173 break;
174 default:
175 non_fatal (_("invalid argument to --format: %s"), optarg);
176 usage (stderr, 1);
178 break;
180 case OPTION_TARGET:
181 target = optarg;
182 break;
184 case OPTION_RADIX:
185 #ifdef ANSI_LIBRARIES
186 temp = strtol (optarg, NULL, 10);
187 #else
188 temp = atol (optarg);
189 #endif
190 switch (temp)
192 case 10:
193 radix = decimal;
194 break;
195 case 8:
196 radix = octal;
197 break;
198 case 16:
199 radix = hex;
200 break;
201 default:
202 non_fatal (_("Invalid radix: %s\n"), optarg);
203 usage (stderr, 1);
205 break;
207 case 'A':
208 selected_output_format = FORMAT_SYSV;
209 break;
210 case 'B':
211 selected_output_format = FORMAT_BERKLEY;
212 break;
213 case 'G':
214 selected_output_format = FORMAT_GNU;
215 break;
216 case 'v':
217 case 'V':
218 show_version = 1;
219 break;
220 case 'd':
221 radix = decimal;
222 break;
223 case 'x':
224 radix = hex;
225 break;
226 case 'o':
227 radix = octal;
228 break;
229 case 't':
230 show_totals = 1;
231 break;
232 case 'f': /* FIXME : For sysv68, `-f' means `full format', i.e.
233 `[fname:] M(.text) + N(.data) + O(.bss) + P(.comment) = Q'
234 where `fname: ' appears only if there are >= 2 input files,
235 and M, N, O, P, Q are expressed in decimal by default,
236 hexa or octal if requested by `-x' or `-o'.
237 Just to make things interesting, Solaris also accepts -f,
238 which prints out the size of each allocatable section, the
239 name of the section, and the total of the section sizes. */
240 /* For the moment, accept `-f' silently, and ignore it. */
241 break;
242 case 0:
243 break;
244 case 'h':
245 case 'H':
246 case '?':
247 usage (stderr, 1);
250 if (show_version)
251 print_version ("size");
252 if (show_help)
253 usage (stdout, 0);
255 if (optind == argc)
256 display_file ("a.out");
257 else
258 for (; optind < argc;)
259 display_file (argv[optind++]);
261 if (show_totals && (selected_output_format == FORMAT_BERKLEY
262 || selected_output_format == FORMAT_GNU))
264 bfd_size_type total = total_textsize + total_datasize + total_bsssize;
265 int col_width = (selected_output_format == FORMAT_BERKLEY) ? 7 : 10;
266 char sep_char = (selected_output_format == FORMAT_BERKLEY) ? '\t' : ' ';
268 rprint_number (col_width, total_textsize);
269 putchar(sep_char);
270 rprint_number (col_width, total_datasize);
271 putchar(sep_char);
272 rprint_number (col_width, total_bsssize);
273 putchar(sep_char);
274 if (selected_output_format == FORMAT_BERKLEY)
275 printf (((radix == octal) ? "%7lo\t%7lx" : "%7lu\t%7lx"),
276 (unsigned long) total, (unsigned long) total);
277 else
278 rprint_number (col_width, total);
279 putchar(sep_char);
280 fputs ("(TOTALS)\n", stdout);
283 return return_code;
286 /* Total size required for common symbols in ABFD. */
288 static void
289 calculate_common_size (bfd *abfd)
291 asymbol **syms = NULL;
292 long storage, symcount;
294 common_size = 0;
295 if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC | HAS_SYMS)) != HAS_SYMS)
296 return;
298 storage = bfd_get_symtab_upper_bound (abfd);
299 if (storage < 0)
300 bfd_fatal (bfd_get_filename (abfd));
301 if (storage)
302 syms = (asymbol **) xmalloc (storage);
304 symcount = bfd_canonicalize_symtab (abfd, syms);
305 if (symcount < 0)
306 bfd_fatal (bfd_get_filename (abfd));
308 while (--symcount >= 0)
310 asymbol *sym = syms[symcount];
312 if (bfd_is_com_section (sym->section)
313 && (sym->flags & BSF_SECTION_SYM) == 0)
314 common_size += sym->value;
316 free (syms);
319 /* Display stats on file or archive member ABFD. */
321 static void
322 display_bfd (bfd *abfd)
324 char **matching;
326 if (bfd_check_format (abfd, bfd_archive))
327 /* An archive within an archive. */
328 return;
330 if (bfd_check_format_matches (abfd, bfd_object, &matching))
332 print_sizes (abfd);
333 printf ("\n");
334 return;
337 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
339 bfd_nonfatal (bfd_get_filename (abfd));
340 list_matching_formats (matching);
341 return_code = 3;
342 return;
345 if (bfd_check_format_matches (abfd, bfd_core, &matching))
347 const char *core_cmd;
349 print_sizes (abfd);
350 fputs (" (core file", stdout);
352 core_cmd = bfd_core_file_failing_command (abfd);
353 if (core_cmd)
354 printf (" invoked as %s", core_cmd);
356 puts (")\n");
357 return;
360 bfd_nonfatal (bfd_get_filename (abfd));
362 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
363 list_matching_formats (matching);
365 return_code = 3;
368 static void
369 display_archive (bfd *file)
371 bfd *last_arfile = NULL;
372 for (;;)
374 bfd *arfile = bfd_openr_next_archived_file (file, last_arfile);
375 if (arfile == NULL
376 || arfile == last_arfile)
378 if (arfile != NULL)
379 bfd_set_error (bfd_error_malformed_archive);
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 if (last_arfile != NULL)
389 bfd_close (last_arfile);
391 display_bfd (arfile);
392 last_arfile = arfile;
395 if (last_arfile != NULL)
396 bfd_close (last_arfile);
399 static void
400 display_file (char *filename)
402 bfd *file;
404 if (get_file_size (filename) < 1)
406 return_code = 1;
407 return;
410 file = bfd_openr (filename, target);
411 if (file == NULL)
413 bfd_nonfatal (filename);
414 return_code = 1;
415 return;
418 if (bfd_check_format (file, bfd_archive))
419 display_archive (file);
420 else
421 display_bfd (file);
423 if (!bfd_close (file))
425 bfd_nonfatal (filename);
426 return_code = 1;
427 return;
431 static int
432 size_number (bfd_size_type num)
434 char buffer[40];
436 return sprintf (buffer, (radix == decimal ? "%" PRIu64
437 : radix == octal ? "0%" PRIo64 : "0x%" PRIx64),
438 (uint64_t) num);
441 static void
442 rprint_number (int width, bfd_size_type num)
444 char buffer[40];
446 sprintf (buffer, (radix == decimal ? "%" PRIu64
447 : radix == octal ? "0%" PRIo64 : "0x%" PRIx64),
448 (uint64_t) num);
450 printf ("%*s", width, buffer);
453 static bfd_size_type bsssize;
454 static bfd_size_type datasize;
455 static bfd_size_type textsize;
457 static void
458 berkeley_or_gnu_sum (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec,
459 void *ignore ATTRIBUTE_UNUSED)
461 flagword flags;
462 bfd_size_type size;
464 flags = bfd_section_flags (sec);
465 if ((flags & SEC_ALLOC) == 0)
466 return;
468 size = bfd_section_size (sec);
469 if ((flags & SEC_CODE) != 0
470 || (selected_output_format == FORMAT_BERKLEY
471 && (flags & SEC_READONLY) != 0))
472 textsize += size;
473 else if ((flags & SEC_HAS_CONTENTS) != 0)
474 datasize += size;
475 else
476 bsssize += size;
479 static void
480 print_berkeley_or_gnu_format (bfd *abfd)
482 static int files_seen = 0;
483 bfd_size_type total;
484 int col_width = (selected_output_format == FORMAT_BERKLEY) ? 7 : 10;
485 char sep_char = (selected_output_format == FORMAT_BERKLEY) ? '\t' : ' ';
487 bsssize = 0;
488 datasize = 0;
489 textsize = 0;
491 bfd_map_over_sections (abfd, berkeley_or_gnu_sum, NULL);
493 bsssize += common_size;
494 if (files_seen++ == 0)
496 if (selected_output_format == FORMAT_BERKLEY)
497 puts ((radix == octal) ? " text\t data\t bss\t oct\t hex\tfilename" :
498 " text\t data\t bss\t dec\t hex\tfilename");
499 else
500 puts (" text data bss total filename");
503 total = textsize + datasize + bsssize;
505 if (show_totals)
507 total_textsize += textsize;
508 total_datasize += datasize;
509 total_bsssize += bsssize;
512 rprint_number (col_width, textsize);
513 putchar (sep_char);
514 rprint_number (col_width, datasize);
515 putchar (sep_char);
516 rprint_number (col_width, bsssize);
517 putchar (sep_char);
519 if (selected_output_format == FORMAT_BERKLEY)
520 printf (((radix == octal) ? "%7lo\t%7lx" : "%7lu\t%7lx"),
521 (unsigned long) total, (unsigned long) total);
522 else
523 rprint_number (col_width, total);
525 putchar (sep_char);
526 fputs (bfd_get_filename (abfd), stdout);
528 if (abfd->my_archive)
529 printf (" (ex %s)", bfd_get_filename (abfd->my_archive));
532 /* I REALLY miss lexical functions! */
533 bfd_size_type svi_total = 0;
534 bfd_vma svi_maxvma = 0;
535 int svi_namelen = 0;
536 int svi_vmalen = 0;
537 int svi_sizelen = 0;
539 static void
540 sysv_internal_sizer (bfd *file ATTRIBUTE_UNUSED, sec_ptr sec,
541 void *ignore ATTRIBUTE_UNUSED)
543 flagword flags = bfd_section_flags (sec);
544 /* Exclude sections with no flags set. This is to omit som spaces. */
545 if (flags == 0)
546 return;
548 if ( ! bfd_is_abs_section (sec)
549 && ! bfd_is_com_section (sec)
550 && ! bfd_is_und_section (sec))
552 bfd_size_type size = bfd_section_size (sec);
553 int namelen = strlen (bfd_section_name (sec));
555 if (namelen > svi_namelen)
556 svi_namelen = namelen;
558 svi_total += size;
560 if (bfd_section_vma (sec) > svi_maxvma)
561 svi_maxvma = bfd_section_vma (sec);
565 static void
566 sysv_one_line (const char *name, bfd_size_type size, bfd_vma vma)
568 printf ("%-*s ", svi_namelen, name);
569 rprint_number (svi_sizelen, size);
570 printf (" ");
571 rprint_number (svi_vmalen, vma);
572 printf ("\n");
575 static void
576 sysv_internal_printer (bfd *file ATTRIBUTE_UNUSED, sec_ptr sec,
577 void *ignore ATTRIBUTE_UNUSED)
579 flagword flags = bfd_section_flags (sec);
580 if (flags == 0)
581 return;
583 if ( ! bfd_is_abs_section (sec)
584 && ! bfd_is_com_section (sec)
585 && ! bfd_is_und_section (sec))
587 bfd_size_type size = bfd_section_size (sec);
589 svi_total += size;
591 sysv_one_line (bfd_section_name (sec),
592 size,
593 bfd_section_vma (sec));
597 static void
598 print_sysv_format (bfd *file)
600 /* Size all of the columns. */
601 svi_total = 0;
602 svi_maxvma = 0;
603 svi_namelen = 0;
604 bfd_map_over_sections (file, sysv_internal_sizer, NULL);
605 if (show_common)
607 if (svi_namelen < (int) sizeof ("*COM*") - 1)
608 svi_namelen = sizeof ("*COM*") - 1;
609 svi_total += common_size;
612 svi_vmalen = size_number ((bfd_size_type)svi_maxvma);
614 if ((size_t) svi_vmalen < sizeof ("addr") - 1)
615 svi_vmalen = sizeof ("addr")-1;
617 svi_sizelen = size_number (svi_total);
618 if ((size_t) svi_sizelen < sizeof ("size") - 1)
619 svi_sizelen = sizeof ("size")-1;
621 svi_total = 0;
622 printf ("%s ", bfd_get_filename (file));
624 if (file->my_archive)
625 printf (" (ex %s)", bfd_get_filename (file->my_archive));
627 printf (":\n%-*s %*s %*s\n", svi_namelen, "section",
628 svi_sizelen, "size", svi_vmalen, "addr");
630 bfd_map_over_sections (file, sysv_internal_printer, NULL);
631 if (show_common)
633 svi_total += common_size;
634 sysv_one_line ("*COM*", common_size, 0);
637 printf ("%-*s ", svi_namelen, "Total");
638 rprint_number (svi_sizelen, svi_total);
639 printf ("\n\n");
642 static void
643 print_sizes (bfd *file)
645 if (show_common)
646 calculate_common_size (file);
647 if (selected_output_format == FORMAT_SYSV)
648 print_sysv_format (file);
649 else
650 print_berkeley_or_gnu_format (file);