1 /* size.c -- report size of various sections of an executable file.
2 Copyright 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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.
35 #include "libiberty.h"
41 /* Program options. */
47 int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output. */
51 /* Program exit status. */
54 static char *target = NULL;
56 /* Static declarations */
58 static void usage PARAMS ((FILE *, int));
59 static void display_file PARAMS ((char *filename));
60 static void display_bfd PARAMS ((bfd *));
61 static void display_archive PARAMS ((bfd *));
62 static int size_number PARAMS ((bfd_size_type));
64 static void lprint_number PARAMS ((int, bfd_size_type));
66 static void rprint_number PARAMS ((int, bfd_size_type));
67 static void print_berkeley_format PARAMS ((bfd *));
68 static void sysv_internal_sizer PARAMS ((bfd *, asection *, PTR));
69 static void sysv_internal_printer PARAMS ((bfd *, asection *, PTR));
70 static void print_sysv_format PARAMS ((bfd *));
71 static void print_sizes PARAMS ((bfd * file));
72 static void berkeley_sum PARAMS ((bfd *, sec_ptr, PTR));
75 usage (stream, status)
80 Usage: %s [-ABdoxV] [--format=berkeley|sysv] [--radix=8|10|16]\n\
81 [--target=bfdname] [--version] [--help] [file...]\n"), program_name);
83 fputs (_("default is --format=berkeley\n"), stream);
85 fputs (_("default is --format=sysv\n"), stream);
87 list_supported_targets (program_name, stream);
89 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
93 struct option long_options[] =
95 {"format", required_argument, 0, 200},
96 {"radix", required_argument, 0, 201},
97 {"target", required_argument, 0, 202},
98 {"version", no_argument, &show_version, 1},
99 {"help", no_argument, &show_help, 1},
100 {0, no_argument, 0, 0}
111 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
112 setlocale (LC_MESSAGES, "");
114 bindtextdomain (PACKAGE, LOCALEDIR);
115 textdomain (PACKAGE);
117 program_name = *argv;
118 xmalloc_set_program_name (program_name);
121 set_default_bfd_target ();
123 while ((c = getopt_long (argc, argv, "ABVdox", long_options,
127 case 200: /* --format */
139 non_fatal (_("invalid argument to --format: %s"), optarg);
144 case 202: /* --target */
148 case 201: /* --radix */
149 #ifdef ANSI_LIBRARIES
150 temp = strtol (optarg, NULL, 10);
152 temp = atol (optarg);
166 non_fatal (_("Invalid radix: %s\n"), optarg);
196 print_version ("size");
201 display_file ("a.out");
203 for (; optind < argc;)
204 display_file (argv[optind++]);
209 /* Display stats on file or archive member ABFD. */
217 if (bfd_check_format (abfd, bfd_archive))
218 /* An archive within an archive. */
221 if (bfd_check_format_matches (abfd, bfd_object, &matching))
228 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
230 bfd_nonfatal (bfd_get_filename (abfd));
231 list_matching_formats (matching);
237 if (bfd_check_format_matches (abfd, bfd_core, &matching))
239 CONST char *core_cmd;
242 fputs (" (core file", stdout);
244 core_cmd = bfd_core_file_failing_command (abfd);
246 printf (" invoked as %s", core_cmd);
252 bfd_nonfatal (bfd_get_filename (abfd));
254 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
256 list_matching_formats (matching);
264 display_archive (file)
267 bfd *arfile = (bfd *) NULL;
271 bfd_set_error (bfd_error_no_error);
273 arfile = bfd_openr_next_archived_file (file, arfile);
276 if (bfd_get_error () != bfd_error_no_more_archived_files)
278 bfd_nonfatal (bfd_get_filename (file));
284 display_bfd (arfile);
285 /* Don't close the archive elements; we need them for next_archive */
290 display_file (filename)
293 bfd *file = bfd_openr (filename, target);
296 bfd_nonfatal (filename);
301 if (bfd_check_format (file, bfd_archive) == true)
302 display_archive (file);
306 if (bfd_close (file) == false)
308 bfd_nonfatal (filename);
314 /* This is what lexical functions are for. */
322 (radix == decimal ? "%lu" :
323 ((radix == octal) ? "0%lo" : "0x%lx")),
324 (unsigned long) num);
326 return strlen (buffer);
331 /* This is not used. */
334 lprint_number (width, num)
340 (radix == decimal ? "%lu" :
341 ((radix == octal) ? "0%lo" : "0x%lx")),
342 (unsigned long) num);
344 printf ("%-*s", width, buffer);
350 rprint_number (width, num)
356 (radix == decimal ? "%lu" :
357 ((radix == octal) ? "0%lo" : "0x%lx")),
358 (unsigned long) num);
360 printf ("%*s", width, buffer);
363 static bfd_size_type bsssize;
364 static bfd_size_type datasize;
365 static bfd_size_type textsize;
368 berkeley_sum (abfd, sec, ignore)
369 bfd *abfd ATTRIBUTE_UNUSED;
371 PTR ignore ATTRIBUTE_UNUSED;
376 flags = bfd_get_section_flags (abfd, sec);
377 if ((flags & SEC_ALLOC) == 0)
380 size = bfd_get_section_size_before_reloc (sec);
381 if ((flags & SEC_CODE) != 0 || (flags & SEC_READONLY) != 0)
383 else if ((flags & SEC_HAS_CONTENTS) != 0)
390 print_berkeley_format (abfd)
393 static int files_seen = 0;
400 bfd_map_over_sections (abfd, berkeley_sum, (PTR) NULL);
402 if (files_seen++ == 0)
404 /* Intel doesn't like bss/stk because they don't have core files. */
405 puts ((radix == octal) ? " text\t data\tbss/stk\t oct\t hex\tfilename" :
406 " text\t data\tbss/stk\t dec\t hex\tfilename");
408 puts ((radix == octal) ? " text\t data\t bss\t oct\t hex\tfilename" :
409 " text\t data\t bss\t dec\t hex\tfilename");
412 total = textsize + datasize + bsssize;
414 rprint_number (7, textsize);
416 rprint_number (7, datasize);
418 rprint_number (7, bsssize);
419 printf (((radix == octal) ? "\t%7lo\t%7lx\t" : "\t%7lu\t%7lx\t"),
420 (unsigned long) total, (unsigned long) total);
422 fputs (bfd_get_filename (abfd), stdout);
423 if (bfd_my_archive (abfd))
424 printf (" (ex %s)", bfd_get_filename (bfd_my_archive (abfd)));
427 /* I REALLY miss lexical functions! */
428 bfd_size_type svi_total = 0;
429 bfd_vma svi_maxvma = 0;
435 sysv_internal_sizer (file, sec, ignore)
436 bfd *file ATTRIBUTE_UNUSED;
438 PTR ignore ATTRIBUTE_UNUSED;
440 bfd_size_type size = bfd_section_size (file, sec);
441 if (!bfd_is_abs_section (sec)
442 && !bfd_is_com_section (sec)
443 && !bfd_is_und_section (sec))
445 int namelen = strlen (bfd_section_name (file, sec));
446 if (namelen > svi_namelen)
447 svi_namelen = namelen;
450 if (bfd_section_vma (file, sec) > svi_maxvma)
451 svi_maxvma = bfd_section_vma (file, sec);
456 sysv_internal_printer (file, sec, ignore)
457 bfd *file ATTRIBUTE_UNUSED;
459 PTR ignore ATTRIBUTE_UNUSED;
461 bfd_size_type size = bfd_section_size (file, sec);
462 if (!bfd_is_abs_section (sec)
463 && !bfd_is_com_section (sec)
464 && !bfd_is_und_section (sec))
468 printf ("%-*s ", svi_namelen, bfd_section_name (file, sec));
469 rprint_number (svi_sizelen, size);
471 rprint_number (svi_vmalen, bfd_section_vma (file, sec));
477 print_sysv_format (file)
480 /* size all of the columns */
484 bfd_map_over_sections (file, sysv_internal_sizer, (PTR) NULL);
485 svi_vmalen = size_number ((bfd_size_type)svi_maxvma);
486 if ((size_t) svi_vmalen < sizeof ("addr") - 1)
487 svi_vmalen = sizeof ("addr")-1;
489 svi_sizelen = size_number (svi_total);
490 if ((size_t) svi_sizelen < sizeof ("size") - 1)
491 svi_sizelen = sizeof ("size")-1;
494 printf ("%s ", bfd_get_filename (file));
495 if (bfd_my_archive (file))
496 printf (" (ex %s)", bfd_get_filename (bfd_my_archive (file)));
498 printf (":\n%-*s %*s %*s\n", svi_namelen, "section",
499 svi_sizelen, "size", svi_vmalen, "addr");
500 bfd_map_over_sections (file, sysv_internal_printer, (PTR) NULL);
502 printf ("%-*s ", svi_namelen, "Total");
503 rprint_number (svi_sizelen, svi_total);
512 print_berkeley_format (file);
514 print_sysv_format (file);