1 /* size.c -- report size of various sections of an executable file.
2 Copyright (C) 1991 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 switch.
40 PROTO(void, display_file, (char *filename));
41 PROTO(void, print_sizes, (bfd *file));
43 /* Various program options */
45 enum {decimal, octal, hex} radix = decimal;
46 int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output */
53 extern char *program_version;
54 extern char *program_name;
57 /** main and like trivia */
62 fprintf (stderr, "size %s\nUsage: %s -{dox}{AB}V files ...\n",
63 program_version, program_name);
64 fputs("\t+radix={8|10|16} -- select appropriate output radix.\n\
65 \t-d -- output in decimal\n\
66 \t-o -- output in octal\n\
67 \t-x -- output in hex", stderr);
68 fputs("\t+format={Berkeley|SysV} -- select display format.\n\
69 \t-A -- SysV(AT&T) format\n\
70 \t-B -- BSD format", stderr);
72 fputs("\t (Default is +format=Berkeley)", stderr);
74 fputs("\t (Default is +format=SysV)", stderr);
76 fputs("\t-V, +version -- display program version, etc.\n\
77 \t+help -- this message\n", stderr);
81 struct option long_options[] = {{"radix", no_argument, 0, 0},
82 {"format", required_argument, 0, 0},
83 {"version", no_argument, &show_version, 1},
84 {"target", optional_argument, NULL, NULL},
85 {"help", no_argument, &show_help, 1},
86 {0, no_argument, 0, 0}};
94 int c; /* sez which option char */
96 extern int optind; /* steps thru options */
101 while ((c = getopt_long(argc, argv, "ABVdox", long_options,
102 &option_index)) != EOF)
105 if (!strcmp("format",(long_options[option_index]).name)) {
107 case 'B': case 'b': berkeley_format = 1; break;
108 case 'S': case 's': berkeley_format = 0; break;
109 default: printf("Unknown option to +format: %s\n", optarg);
115 if (!strcmp("target",(long_options[option_index]).name)) {
120 if (!strcmp("radix",(long_options[option_index]).name)) {
121 #ifdef ANSI_LIBRARIES
122 temp = strtol(optarg, NULL, 10);
127 case 10: radix = decimal; break;
128 case 8: radix = octal; break;
129 case 16: radix = hex; break;
130 default: printf("Unknown radix: %s\n", optarg);
135 case 'A': berkeley_format = 0; break;
136 case 'B': berkeley_format = 1; break;
137 case 'V': show_version = 1; break;
138 case 'd': radix = decimal; break;
139 case 'x': radix = hex; break;
140 case 'o': radix = octal; break;
144 if (show_version) printf("%s version %s\n", program_name, program_version);
145 if (show_help) usage();
148 display_file ("a.out");
150 for (; optind < argc;)
151 display_file (argv[optind++]);
156 /** Display a file's stats */
162 CONST char *core_cmd;
164 if (bfd_check_format(abfd, bfd_archive)) return;
166 if (bfd_check_format(abfd, bfd_object)) {
171 if (bfd_check_format(abfd, bfd_core)) {
173 fputs(" (core file", stdout);
175 core_cmd = bfd_core_file_failing_command(abfd);
176 if (core_cmd) printf(" invoked as %s", core_cmd);
182 printf("Unknown file format: %s.", bfd_get_filename(abfd));
193 display_file(filename)
196 bfd *file, *arfile = (bfd *) NULL;
198 file = bfd_openr (filename, target);
200 bfd_perror (filename);
205 if (bfd_check_format(file, bfd_archive) == true) {
208 bfd_error = no_error;
210 arfile = bfd_openr_next_archived_file (file, arfile);
211 if (arfile == NULL) {
212 if (bfd_error != no_more_archived_files) {
213 bfd_perror (bfd_get_filename (file));
219 display_bfd (arfile);
220 /* Don't close the archive elements; we need them for next_archive */
229 /* This is what lexical functions are for */
231 lprint_number (width, num)
234 printf ((radix == decimal ? "%-*d\t" :
235 ((radix == octal) ? "%-*o\t" : "%-*x\t")), width, num);
239 rprint_number(width, num)
242 printf ((radix == decimal ? "%*d\t" :
243 ((radix == octal) ? "%*o\t" : "%*x\t")), width, num);
246 static char *bss_section_name = ".bss";
247 static char *data_section_name = ".data";
248 static char *stack_section_name = ".stack";
249 static char *text_section_name = ".text";
251 void print_berkeley_format(abfd)
254 static int files_seen = 0;
255 sec_ptr bsssection = NULL;
256 sec_ptr datasection = NULL;
257 sec_ptr textsection = NULL;
258 unsigned long bsssize = 0;
259 unsigned long datasize = 0;
260 unsigned long textsize = 0;
261 unsigned long total = 0;
264 if ((textsection = bfd_get_section_by_name (abfd, text_section_name))
266 textsize = bfd_get_section_size_before_reloc (textsection);
269 if ((datasection = bfd_get_section_by_name (abfd, data_section_name))
271 datasize = bfd_get_section_size_before_reloc ( datasection);
274 if (bfd_get_format (abfd) == bfd_object) {
275 if ((bsssection = bfd_get_section_by_name (abfd, bss_section_name))
277 bsssize = bfd_section_size(abfd, bsssection);
280 if ((bsssection = bfd_get_section_by_name (abfd, stack_section_name))
282 bsssize = bfd_section_size(abfd, bsssection);
286 if (files_seen++ == 0)
287 #if 0 /* intel doesn't like bss/stk b/c they don't gave core files */
288 puts((radix == octal) ? "text\tdata\tbss/stk\toct\thex\tfilename" :
289 "text\tdata\tbss/stk\tdec\thex\tfilename");
291 puts((radix == octal) ? "text\tdata\tbss\toct\thex\tfilename" :
292 "text\tdata\tbss\tdec\thex\tfilename");
295 total = textsize + datasize + bsssize;
297 lprint_number (7, textsize);
298 lprint_number (7, datasize);
299 lprint_number (7, bsssize);
300 printf (((radix == octal) ? "%-7o\t%-7x\t" : "%-7d\t%-7x\t"), total, total);
302 fputs(bfd_get_filename(abfd), stdout);
303 if (abfd->my_archive) printf (" (ex %s)", abfd->my_archive->filename);
306 /* I REALLY miss lexical functions! */
310 sysv_internal_printer(file, sec, ignore)
315 int size = bfd_section_size (file, sec);
316 if (sec!= &bfd_abs_section
317 && sec!= &bfd_com_section
318 && sec!=&bfd_und_section)
323 printf ("%-12s", bfd_section_name(file, sec));
324 rprint_number (8, size);
326 rprint_number (8, bfd_section_vma(file, sec));
333 print_sysv_format(file)
338 printf ("%s ", bfd_get_filename (file));
339 if (file->my_archive) printf (" (ex %s)", file->my_archive->filename);
341 puts(":\nsection\t\tsize\t addr");
342 bfd_map_over_sections (file, sysv_internal_printer, (PTR)NULL);
345 rprint_number(8, svi_total);
346 printf("\n"); printf("\n");
354 print_berkeley_format(file);
355 else print_sysv_format(file);