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