1 /* size.c -- report size of various sections of an executable file.
2 Copyright 1991, 1992 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 2 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., 675 Mass Ave, Cambridge, MA 02139, 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 --predantic option.
40 /* Various program options */
42 enum {decimal, octal, hex} radix = decimal;
43 int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output */
50 extern char *program_version;
51 extern char *program_name;
54 /* Forward declarations */
57 display_file PARAMS ((char *filename));
60 print_sizes PARAMS ((bfd *file));
62 /** main and like trivia */
67 fprintf (stderr, "size %s\n\
68 Usage: %s [-ABdoxV] [--format=berkeley|sysv] [--radix=8|10|16]\n\
69 [--target=bfdname] [--version] [--help] [file...]\n",
70 program_version, program_name);
72 fputs (" (default is --format=berkeley)\n", stderr);
74 fputs (" (default is --format=sysv)\n", stderr);
79 struct option long_options[] = {
80 {"format", required_argument, 0, 200},
81 {"radix", required_argument, 0, 201},
82 {"target", required_argument, 0, 202},
83 {"version", no_argument, &show_version, 1},
84 {"help", no_argument, &show_help, 1},
85 {0, no_argument, 0, 0}
94 int c; /* sez which option char */
95 extern int optind; /* steps thru options */
101 while ((c = getopt_long(argc, argv, "ABVdox", long_options,
104 case 200: /* --format */
106 case 'B': case 'b': berkeley_format = 1; break;
107 case 'S': case 's': berkeley_format = 0; break;
108 default: fprintf(stderr, "invalid argument to --format: %s\n", optarg);
113 case 202: /* --target */
117 case 201: /* --radix */
118 #ifdef ANSI_LIBRARIES
119 temp = strtol(optarg, NULL, 10);
124 case 10: radix = decimal; break;
125 case 8: radix = octal; break;
126 case 16: radix = hex; break;
127 default: printf("Unknown radix: %s\n", optarg);
132 case 'A': berkeley_format = 0; break;
133 case 'B': berkeley_format = 1; break;
134 case 'V': show_version = 1; break;
135 case 'd': radix = decimal; break;
136 case 'x': radix = hex; break;
137 case 'o': radix = octal; break;
141 if (show_version) printf("%s version %s\n", program_name, program_version);
142 if (show_help) usage();
145 display_file ("a.out");
147 for (; optind < argc;)
148 display_file (argv[optind++]);
153 /** Display a file's stats */
159 CONST char *core_cmd;
161 if (bfd_check_format(abfd, bfd_archive)) return;
163 if (bfd_check_format(abfd, bfd_object)) {
168 if (bfd_check_format(abfd, bfd_core)) {
170 fputs(" (core file", stdout);
172 core_cmd = bfd_core_file_failing_command(abfd);
173 if (core_cmd) printf(" invoked as %s", core_cmd);
179 printf("Unknown file format: %s.", bfd_get_filename(abfd));
190 display_file(filename)
193 bfd *file, *arfile = (bfd *) NULL;
195 file = bfd_openr (filename, target);
197 fprintf (stderr, "%s: ", program_name);
198 bfd_perror (filename);
203 if (bfd_check_format(file, bfd_archive) == true) {
206 bfd_error = no_error;
208 arfile = bfd_openr_next_archived_file (file, arfile);
209 if (arfile == NULL) {
210 if (bfd_error != no_more_archived_files) {
211 fprintf (stderr, "%s: ", program_name);
212 bfd_perror (bfd_get_filename (file));
218 display_bfd (arfile);
219 /* Don't close the archive elements; we need them for next_archive */
228 /* This is what lexical functions are for */
230 lprint_number (width, num)
234 printf ((radix == decimal ? "%-*lu\t" :
235 ((radix == octal) ? "%-*lo\t" : "%-*lx\t")),
236 width, (unsigned long)num);
240 rprint_number(width, num)
244 printf ((radix == decimal ? "%*lu\t" :
245 ((radix == octal) ? "%*lo\t" : "%*lx\t")),
246 width, (unsigned long)num);
249 static char *bss_section_name = ".bss";
250 static char *data_section_name = ".data";
251 static char *stack_section_name = ".stack";
252 static char *text_section_name = ".text";
254 void print_berkeley_format(abfd)
257 static int files_seen = 0;
258 sec_ptr bsssection = NULL;
259 sec_ptr datasection = NULL;
260 sec_ptr textsection = NULL;
261 bfd_size_type bsssize = 0;
262 bfd_size_type datasize = 0;
263 bfd_size_type textsize = 0;
264 bfd_size_type total = 0;
267 if ((textsection = bfd_get_section_by_name (abfd, text_section_name))
269 textsize = bfd_get_section_size_before_reloc (textsection);
272 if ((datasection = bfd_get_section_by_name (abfd, data_section_name))
274 datasize = bfd_get_section_size_before_reloc ( datasection);
277 if (bfd_get_format (abfd) == bfd_object) {
278 if ((bsssection = bfd_get_section_by_name (abfd, bss_section_name))
280 bsssize = bfd_section_size(abfd, bsssection);
283 if ((bsssection = bfd_get_section_by_name (abfd, stack_section_name))
285 bsssize = bfd_section_size(abfd, bsssection);
289 if (files_seen++ == 0)
290 #if 0 /* intel doesn't like bss/stk b/c they don't gave core files */
291 puts((radix == octal) ? "text\tdata\tbss/stk\toct\thex\tfilename" :
292 "text\tdata\tbss/stk\tdec\thex\tfilename");
294 puts((radix == octal) ? "text\tdata\tbss\toct\thex\tfilename" :
295 "text\tdata\tbss\tdec\thex\tfilename");
298 total = textsize + datasize + bsssize;
300 lprint_number (7, textsize);
301 lprint_number (7, datasize);
302 lprint_number (7, bsssize);
303 printf (((radix == octal) ? "%-7lo\t%-7lx\t" : "%-7lu\t%-7lx\t"),
304 (unsigned long)total, (unsigned long)total);
306 fputs(bfd_get_filename(abfd), stdout);
307 if (abfd->my_archive) printf (" (ex %s)", abfd->my_archive->filename);
310 /* I REALLY miss lexical functions! */
311 bfd_size_type svi_total = 0;
314 sysv_internal_printer(file, sec, ignore)
319 bfd_size_type size = bfd_section_size (file, sec);
320 if (sec!= &bfd_abs_section
321 && ! bfd_is_com_section (sec)
322 && sec!=&bfd_und_section)
327 printf ("%-12s", bfd_section_name(file, sec));
328 rprint_number (8, size);
330 rprint_number (8, bfd_section_vma(file, sec));
337 print_sysv_format(file)
342 printf ("%s ", bfd_get_filename (file));
343 if (file->my_archive) printf (" (ex %s)", file->my_archive->filename);
345 puts(":\nsection\t\tsize\t addr");
346 bfd_map_over_sections (file, sysv_internal_printer, (PTR)NULL);
349 rprint_number(8, svi_total);
350 printf("\n"); printf("\n");
358 print_berkeley_format(file);
359 else print_sysv_format(file);