Automatic date update in version.in
[deliverable/binutils-gdb.git] / binutils / size.c
CommitLineData
252b5132 1/* size.c -- report size of various sections of an executable file.
250d07de 2 Copyright (C) 1991-2021 Free Software Foundation, Inc.
252b5132 3
15c82623 4 This file is part of GNU Binutils.
252b5132 5
15c82623
NC
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
32866df7 8 the Free Software Foundation; either version 3 of the License, or
15c82623 9 (at your option) any later version.
252b5132 10
15c82623
NC
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.
252b5132 15
15c82623
NC
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
32866df7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132
RH
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
15c82623 29 out of luck; there's no --compatibility or --pedantic option. */
252b5132 30
3db64b00 31#include "sysdep.h"
252b5132 32#include "bfd.h"
252b5132 33#include "libiberty.h"
d7a283d4 34#include "getopt.h"
3db64b00 35#include "bucomm.h"
252b5132
RH
36
37#ifndef BSD_DEFAULT
38#define BSD_DEFAULT 1
39#endif
40
41/* Program options. */
42
29422971 43static enum
252b5132
RH
44 {
45 decimal, octal, hex
15c82623
NC
46 }
47radix = decimal;
48
46cbf38d
AB
49/* Select the desired output format. */
50enum output_format
51 {
52 FORMAT_BERKLEY,
53 FORMAT_SYSV,
54 FORMAT_GNU
55 };
56static enum output_format selected_output_format =
57#if BSD_DEFAULT
58 FORMAT_BERKLEY
59#else
60 FORMAT_SYSV
61#endif
62 ;
85b1c36d 63
29422971
AM
64static int show_version = 0;
65static int show_help = 0;
66static int show_totals = 0;
67static int show_common = 0;
15c82623 68
29422971 69static bfd_size_type common_size;
15c82623
NC
70static bfd_size_type total_bsssize;
71static bfd_size_type total_datasize;
72static bfd_size_type total_textsize;
252b5132
RH
73
74/* Program exit status. */
29422971 75static int return_code = 0;
252b5132
RH
76
77static char *target = NULL;
78
29422971 79/* Forward declarations. */
252b5132 80
2da42df6 81static void display_file (char *);
2da42df6 82static void rprint_number (int, bfd_size_type);
2da42df6 83static void print_sizes (bfd * file);
252b5132
RH
84\f
85static void
2da42df6 86usage (FILE *stream, int status)
252b5132 87{
8b53311e
NC
88 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
89 fprintf (stream, _(" Displays the sizes of sections inside binary files\n"));
9f66665a 90 fprintf (stream, _(" If no input file(s) are specified, a.out is assumed\n"));
8b53311e 91 fprintf (stream, _(" The options are:\n\
46cbf38d 92 -A|-B|-G --format={sysv|berkeley|gnu} Select output style (default is %s)\n\
92acdfaf 93 -o|-d|-x --radix={8|10|16} Display numbers in octal, decimal or hex\n\
15c82623 94 -t --totals Display the total sizes (Berkeley only)\n\
29422971 95 --common Display total size for *COM* syms\n\
8b53311e 96 --target=<bfdname> Set the binary file format\n\
07012eee 97 @<file> Read options from <file>\n\
8b53311e
NC
98 -h --help Display this information\n\
99 -v --version Display the program's version\n\
100\n"),
252b5132 101#if BSD_DEFAULT
8b53311e 102 "berkeley"
252b5132 103#else
8b53311e 104 "sysv"
252b5132 105#endif
8b53311e 106);
252b5132 107 list_supported_targets (program_name, stream);
92f01d61 108 if (REPORT_BUGS_TO[0] && status == 0)
8ad3436c 109 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132
RH
110 exit (status);
111}
112
29422971
AM
113#define OPTION_FORMAT (200)
114#define OPTION_RADIX (OPTION_FORMAT + 1)
115#define OPTION_TARGET (OPTION_RADIX + 1)
116
85b1c36d 117static struct option long_options[] =
252b5132 118{
29422971
AM
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},
15c82623 123 {"totals", no_argument, &show_totals, 1},
252b5132
RH
124 {"version", no_argument, &show_version, 1},
125 {"help", no_argument, &show_help, 1},
126 {0, no_argument, 0, 0}
127};
128
2da42df6 129int main (int, char **);
65de42c0 130
252b5132 131int
2da42df6 132main (int argc, char **argv)
252b5132
RH
133{
134 int temp;
135 int c;
136
87b9f255 137#ifdef HAVE_LC_MESSAGES
252b5132 138 setlocale (LC_MESSAGES, "");
3882b010 139#endif
3882b010 140 setlocale (LC_CTYPE, "");
252b5132
RH
141 bindtextdomain (PACKAGE, LOCALEDIR);
142 textdomain (PACKAGE);
143
144 program_name = *argv;
145 xmalloc_set_program_name (program_name);
86eafac0 146 bfd_set_error_program_name (program_name);
252b5132 147
869b9d07
MM
148 expandargv (&argc, &argv);
149
bf2dd8d7
AM
150 if (bfd_init () != BFD_INIT_MAGIC)
151 fatal (_("fatal error: libbfd ABI mismatch"));
252b5132
RH
152 set_default_bfd_target ();
153
46cbf38d 154 while ((c = getopt_long (argc, argv, "ABGHhVvdfotx", long_options,
252b5132
RH
155 (int *) 0)) != EOF)
156 switch (c)
157 {
29422971 158 case OPTION_FORMAT:
252b5132
RH
159 switch (*optarg)
160 {
161 case 'B':
162 case 'b':
46cbf38d 163 selected_output_format = FORMAT_BERKLEY;
252b5132
RH
164 break;
165 case 'S':
166 case 's':
46cbf38d
AB
167 selected_output_format = FORMAT_SYSV;
168 break;
169 case 'G':
170 case 'g':
171 selected_output_format = FORMAT_GNU;
252b5132
RH
172 break;
173 default:
37cc8ec1 174 non_fatal (_("invalid argument to --format: %s"), optarg);
252b5132
RH
175 usage (stderr, 1);
176 }
177 break;
178
29422971 179 case OPTION_TARGET:
252b5132
RH
180 target = optarg;
181 break;
182
29422971 183 case OPTION_RADIX:
252b5132
RH
184#ifdef ANSI_LIBRARIES
185 temp = strtol (optarg, NULL, 10);
186#else
187 temp = atol (optarg);
188#endif
189 switch (temp)
190 {
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:
37cc8ec1 201 non_fatal (_("Invalid radix: %s\n"), optarg);
252b5132
RH
202 usage (stderr, 1);
203 }
204 break;
205
206 case 'A':
46cbf38d 207 selected_output_format = FORMAT_SYSV;
252b5132
RH
208 break;
209 case 'B':
46cbf38d
AB
210 selected_output_format = FORMAT_BERKLEY;
211 break;
212 case 'G':
213 selected_output_format = FORMAT_GNU;
252b5132 214 break;
8b53311e 215 case 'v':
252b5132
RH
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;
15c82623
NC
228 case 't':
229 show_totals = 1;
230 break;
e3a69612
AM
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;
252b5132
RH
241 case 0:
242 break;
8b53311e
NC
243 case 'h':
244 case 'H':
252b5132
RH
245 case '?':
246 usage (stderr, 1);
247 }
248
249 if (show_version)
250 print_version ("size");
251 if (show_help)
252 usage (stdout, 0);
253
254 if (optind == argc)
255 display_file ("a.out");
256 else
257 for (; optind < argc;)
258 display_file (argv[optind++]);
259
46cbf38d
AB
260 if (show_totals && (selected_output_format == FORMAT_BERKLEY
261 || selected_output_format == FORMAT_GNU))
15c82623
NC
262 {
263 bfd_size_type total = total_textsize + total_datasize + total_bsssize;
46cbf38d
AB
264 int col_width = (selected_output_format == FORMAT_BERKLEY) ? 7 : 10;
265 char sep_char = (selected_output_format == FORMAT_BERKLEY) ? '\t' : ' ';
266
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);
15c82623
NC
279 fputs ("(TOTALS)\n", stdout);
280 }
281
252b5132
RH
282 return return_code;
283}
284\f
29422971
AM
285/* Total size required for common symbols in ABFD. */
286
287static void
288calculate_common_size (bfd *abfd)
289{
290 asymbol **syms = NULL;
291 long storage, symcount;
292
293 common_size = 0;
294 if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC | HAS_SYMS)) != HAS_SYMS)
295 return;
296
297 storage = bfd_get_symtab_upper_bound (abfd);
298 if (storage < 0)
299 bfd_fatal (bfd_get_filename (abfd));
300 if (storage)
3f5e193b 301 syms = (asymbol **) xmalloc (storage);
29422971
AM
302
303 symcount = bfd_canonicalize_symtab (abfd, syms);
304 if (symcount < 0)
305 bfd_fatal (bfd_get_filename (abfd));
306
307 while (--symcount >= 0)
308 {
309 asymbol *sym = syms[symcount];
310
311 if (bfd_is_com_section (sym->section)
312 && (sym->flags & BSF_SECTION_SYM) == 0)
313 common_size += sym->value;
314 }
315 free (syms);
316}
317
252b5132
RH
318/* Display stats on file or archive member ABFD. */
319
320static void
2da42df6 321display_bfd (bfd *abfd)
252b5132
RH
322{
323 char **matching;
324
325 if (bfd_check_format (abfd, bfd_archive))
326 /* An archive within an archive. */
327 return;
328
329 if (bfd_check_format_matches (abfd, bfd_object, &matching))
330 {
331 print_sizes (abfd);
332 printf ("\n");
333 return;
334 }
335
336 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
337 {
338 bfd_nonfatal (bfd_get_filename (abfd));
339 list_matching_formats (matching);
340 free (matching);
341 return_code = 3;
342 return;
343 }
344
345 if (bfd_check_format_matches (abfd, bfd_core, &matching))
346 {
15c82623 347 const char *core_cmd;
252b5132
RH
348
349 print_sizes (abfd);
350 fputs (" (core file", stdout);
351
352 core_cmd = bfd_core_file_failing_command (abfd);
353 if (core_cmd)
354 printf (" invoked as %s", core_cmd);
355
356 puts (")\n");
357 return;
358 }
359
360 bfd_nonfatal (bfd_get_filename (abfd));
361
362 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
363 {
364 list_matching_formats (matching);
365 free (matching);
366 }
367
368 return_code = 3;
369}
370
371static void
2da42df6 372display_archive (bfd *file)
252b5132
RH
373{
374 bfd *arfile = (bfd *) NULL;
6b52b824 375 bfd *last_arfile = (bfd *) NULL;
252b5132
RH
376
377 for (;;)
378 {
379 bfd_set_error (bfd_error_no_error);
380
381 arfile = bfd_openr_next_archived_file (file, arfile);
382 if (arfile == NULL)
383 {
384 if (bfd_get_error () != bfd_error_no_more_archived_files)
385 {
386 bfd_nonfatal (bfd_get_filename (file));
387 return_code = 2;
388 }
389 break;
390 }
391
392 display_bfd (arfile);
6b52b824
AM
393
394 if (last_arfile != NULL)
896ca098
NC
395 {
396 bfd_close (last_arfile);
397
398 /* PR 17512: file: a244edbc. */
399 if (last_arfile == arfile)
400 return;
401 }
402
6b52b824 403 last_arfile = arfile;
252b5132 404 }
6b52b824
AM
405
406 if (last_arfile != NULL)
407 bfd_close (last_arfile);
252b5132
RH
408}
409
410static void
2da42df6 411display_file (char *filename)
252b5132 412{
f24ddbdd 413 bfd *file;
15c82623 414
f24ddbdd 415 if (get_file_size (filename) < 1)
d68c385b
NC
416 {
417 return_code = 1;
418 return;
419 }
f24ddbdd
NC
420
421 file = bfd_openr (filename, target);
252b5132
RH
422 if (file == NULL)
423 {
424 bfd_nonfatal (filename);
425 return_code = 1;
426 return;
427 }
428
b34976b6 429 if (bfd_check_format (file, bfd_archive))
252b5132
RH
430 display_archive (file);
431 else
432 display_bfd (file);
433
b34976b6 434 if (!bfd_close (file))
252b5132
RH
435 {
436 bfd_nonfatal (filename);
437 return_code = 1;
438 return;
439 }
440}
441\f
252b5132 442static int
2da42df6 443size_number (bfd_size_type num)
252b5132
RH
444{
445 char buffer[40];
15c82623 446
252b5132 447 sprintf (buffer,
14a91970
AM
448 (radix == decimal ? "%" BFD_VMA_FMT "u" :
449 ((radix == octal) ? "0%" BFD_VMA_FMT "o" : "0x%" BFD_VMA_FMT "x")),
450 num);
252b5132
RH
451
452 return strlen (buffer);
453}
454
252b5132 455static void
2da42df6 456rprint_number (int width, bfd_size_type num)
252b5132
RH
457{
458 char buffer[40];
15c82623 459
252b5132 460 sprintf (buffer,
14a91970
AM
461 (radix == decimal ? "%" BFD_VMA_FMT "u" :
462 ((radix == octal) ? "0%" BFD_VMA_FMT "o" : "0x%" BFD_VMA_FMT "x")),
463 num);
252b5132
RH
464
465 printf ("%*s", width, buffer);
466}
467
468static bfd_size_type bsssize;
469static bfd_size_type datasize;
470static bfd_size_type textsize;
471
472static void
46cbf38d
AB
473berkeley_or_gnu_sum (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec,
474 void *ignore ATTRIBUTE_UNUSED)
252b5132
RH
475{
476 flagword flags;
477 bfd_size_type size;
478
fd361982 479 flags = bfd_section_flags (sec);
252b5132
RH
480 if ((flags & SEC_ALLOC) == 0)
481 return;
482
fd361982 483 size = bfd_section_size (sec);
46cbf38d
AB
484 if ((flags & SEC_CODE) != 0
485 || (selected_output_format == FORMAT_BERKLEY
486 && (flags & SEC_READONLY) != 0))
252b5132
RH
487 textsize += size;
488 else if ((flags & SEC_HAS_CONTENTS) != 0)
489 datasize += size;
490 else
491 bsssize += size;
492}
493
9f66665a 494static void
46cbf38d 495print_berkeley_or_gnu_format (bfd *abfd)
252b5132
RH
496{
497 static int files_seen = 0;
498 bfd_size_type total;
46cbf38d
AB
499 int col_width = (selected_output_format == FORMAT_BERKLEY) ? 7 : 10;
500 char sep_char = (selected_output_format == FORMAT_BERKLEY) ? '\t' : ' ';
252b5132
RH
501
502 bsssize = 0;
503 datasize = 0;
504 textsize = 0;
505
46cbf38d 506 bfd_map_over_sections (abfd, berkeley_or_gnu_sum, NULL);
252b5132 507
29422971 508 bsssize += common_size;
252b5132 509 if (files_seen++ == 0)
46cbf38d
AB
510 {
511 if (selected_output_format == FORMAT_BERKLEY)
512 puts ((radix == octal) ? " text\t data\t bss\t oct\t hex\tfilename" :
513 " text\t data\t bss\t dec\t hex\tfilename");
514 else
515 puts (" text data bss total filename");
516 }
252b5132
RH
517
518 total = textsize + datasize + bsssize;
519
15c82623
NC
520 if (show_totals)
521 {
522 total_textsize += textsize;
523 total_datasize += datasize;
524 total_bsssize += bsssize;
525 }
526
46cbf38d
AB
527 rprint_number (col_width, textsize);
528 putchar (sep_char);
529 rprint_number (col_width, datasize);
530 putchar (sep_char);
531 rprint_number (col_width, bsssize);
532 putchar (sep_char);
252b5132 533
46cbf38d
AB
534 if (selected_output_format == FORMAT_BERKLEY)
535 printf (((radix == octal) ? "%7lo\t%7lx" : "%7lu\t%7lx"),
536 (unsigned long) total, (unsigned long) total);
537 else
538 rprint_number (col_width, total);
539
540 putchar (sep_char);
252b5132 541 fputs (bfd_get_filename (abfd), stdout);
15c82623 542
3860d2b4
AM
543 if (abfd->my_archive)
544 printf (" (ex %s)", bfd_get_filename (abfd->my_archive));
252b5132
RH
545}
546
547/* I REALLY miss lexical functions! */
548bfd_size_type svi_total = 0;
549bfd_vma svi_maxvma = 0;
550int svi_namelen = 0;
551int svi_vmalen = 0;
552int svi_sizelen = 0;
553
554static void
2da42df6
AJ
555sysv_internal_sizer (bfd *file ATTRIBUTE_UNUSED, sec_ptr sec,
556 void *ignore ATTRIBUTE_UNUSED)
252b5132 557{
e19511a6
AM
558 flagword flags = bfd_section_flags (sec);
559 /* Exclude sections with no flags set. This is to omit som spaces. */
560 if (flags == 0)
561 return;
15c82623
NC
562
563 if ( ! bfd_is_abs_section (sec)
564 && ! bfd_is_com_section (sec)
565 && ! bfd_is_und_section (sec))
252b5132 566 {
e19511a6 567 bfd_size_type size = bfd_section_size (sec);
fd361982 568 int namelen = strlen (bfd_section_name (sec));
15c82623 569
252b5132
RH
570 if (namelen > svi_namelen)
571 svi_namelen = namelen;
572
573 svi_total += size;
15c82623 574
fd361982
AM
575 if (bfd_section_vma (sec) > svi_maxvma)
576 svi_maxvma = bfd_section_vma (sec);
252b5132
RH
577 }
578}
579
29422971
AM
580static void
581sysv_one_line (const char *name, bfd_size_type size, bfd_vma vma)
582{
583 printf ("%-*s ", svi_namelen, name);
584 rprint_number (svi_sizelen, size);
585 printf (" ");
586 rprint_number (svi_vmalen, vma);
587 printf ("\n");
588}
589
252b5132 590static void
2da42df6
AJ
591sysv_internal_printer (bfd *file ATTRIBUTE_UNUSED, sec_ptr sec,
592 void *ignore ATTRIBUTE_UNUSED)
252b5132 593{
e19511a6
AM
594 flagword flags = bfd_section_flags (sec);
595 if (flags == 0)
596 return;
15c82623
NC
597
598 if ( ! bfd_is_abs_section (sec)
599 && ! bfd_is_com_section (sec)
600 && ! bfd_is_und_section (sec))
252b5132 601 {
e19511a6
AM
602 bfd_size_type size = bfd_section_size (sec);
603
252b5132
RH
604 svi_total += size;
605
fd361982 606 sysv_one_line (bfd_section_name (sec),
29422971 607 size,
fd361982 608 bfd_section_vma (sec));
252b5132
RH
609 }
610}
611
612static void
2da42df6 613print_sysv_format (bfd *file)
252b5132 614{
15c82623 615 /* Size all of the columns. */
252b5132
RH
616 svi_total = 0;
617 svi_maxvma = 0;
618 svi_namelen = 0;
2da42df6 619 bfd_map_over_sections (file, sysv_internal_sizer, NULL);
29422971
AM
620 if (show_common)
621 {
622 if (svi_namelen < (int) sizeof ("*COM*") - 1)
623 svi_namelen = sizeof ("*COM*") - 1;
624 svi_total += common_size;
625 }
626
252b5132 627 svi_vmalen = size_number ((bfd_size_type)svi_maxvma);
15c82623 628
252b5132
RH
629 if ((size_t) svi_vmalen < sizeof ("addr") - 1)
630 svi_vmalen = sizeof ("addr")-1;
631
632 svi_sizelen = size_number (svi_total);
633 if ((size_t) svi_sizelen < sizeof ("size") - 1)
634 svi_sizelen = sizeof ("size")-1;
635
636 svi_total = 0;
637 printf ("%s ", bfd_get_filename (file));
15c82623 638
3860d2b4
AM
639 if (file->my_archive)
640 printf (" (ex %s)", bfd_get_filename (file->my_archive));
252b5132
RH
641
642 printf (":\n%-*s %*s %*s\n", svi_namelen, "section",
643 svi_sizelen, "size", svi_vmalen, "addr");
15c82623 644
2da42df6 645 bfd_map_over_sections (file, sysv_internal_printer, NULL);
29422971
AM
646 if (show_common)
647 {
648 svi_total += common_size;
649 sysv_one_line ("*COM*", common_size, 0);
650 }
252b5132
RH
651
652 printf ("%-*s ", svi_namelen, "Total");
653 rprint_number (svi_sizelen, svi_total);
654 printf ("\n\n");
655}
656
657static void
2da42df6 658print_sizes (bfd *file)
252b5132 659{
29422971
AM
660 if (show_common)
661 calculate_common_size (file);
46cbf38d 662 if (selected_output_format == FORMAT_SYSV)
252b5132 663 print_sysv_format (file);
46cbf38d
AB
664 else
665 print_berkeley_or_gnu_format (file);
252b5132 666}
This page took 0.851169 seconds and 4 git commands to generate.