1 /*** size.c -- report size of various sections of an executable file */
2 /* Extensions/incompatibilities:
3 o - BSD output has filenames at the end.
4 o - BSD output can appear in different radicies.
5 o - SysV output has less redundant whitespace. Filename comes at end.
6 o - SysV output doesn't show VMA which is always the same as the PMA.
7 o - We also handle core files.
8 o - We also handle archives.
9 If you write shell scripts which manipulate this info then you may be
10 out of luck; there's no +predantic switch.
21 PROTO(void, display_file, (char *filename));
22 PROTO(void, print_sizes, (bfd *file));
24 /* Various program options */
26 enum {decimal, octal, hex} radix = decimal;
27 int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output */
32 extern char *program_version;
33 extern char *program_name;
36 /** main and like trivia */
41 fprintf (stderr, "size %s\nUsage: %s -{dox}{AB}V files ...\n",
42 program_version, program_name);
43 fputs("\t+radix={8|10|16} -- select appropriate output radix.\n\
44 \t-d -- output in decimal\n\
45 \t-o -- output in octal\n\
46 \t-x -- output in hex", stderr);
47 fputs("\t+format={Berkeley|SysV} -- select display format.\n\
48 \t-A -- SysV(AT&T) format\n\
49 \t-B -- BSD format", stderr);
51 fputs("\t (Default is +format=Berkeley)", stderr);
53 fputs("\t (Default is +format=SysV)", stderr);
55 fputs("\t-V, +version -- display program version, etc.\n\
56 \t+help -- this message\n", stderr);
60 struct option long_options[] = {{"radix", 1, 0, 0},
62 {"version", 0, &show_version, 1},
63 {"target", 2, NULL, NULL},
64 {"help", 0, &show_help, 1},
73 int c; /* sez which option char */
75 extern int optind; /* steps thru options */
78 while ((c = getopt_long(argc, argv, "ABVdox", long_options,
79 &option_index)) != EOF)
82 if (!strcmp("format",(long_options[option_index]).name)) {
84 case 'B': case 'b': berkeley_format = 1; break;
85 case 'S': case 's': berkeley_format = 0; break;
86 default: printf("Unknown option to +format: %s\n", optarg);
92 if (!strcmp("target",(long_options[option_index]).name)) {
97 if (!strcmp("radix",(long_options[option_index]).name)) {
99 temp = strtol(optarg, NULL, 10);
104 case 10: radix = decimal; break;
105 case 8: radix = octal; break;
106 case 16: radix = hex; break;
107 default: printf("Unknown radix: %s\n", optarg);
112 case 'A': berkeley_format = 0; break;
113 case 'B': berkeley_format = 1; break;
114 case 'V': show_version = 1; break;
115 case 'd': radix = decimal; break;
116 case 'x': radix = hex; break;
117 case 'o': radix = octal; break;
121 if (show_version) printf("%s version %s\n", program_name, program_version);
122 if (show_help) usage();
125 #if 0 /* intel doesn't like bss/stk b/c they don't gave core files */
126 puts((radix == octal) ? "text\tdata\tbss/stk\toct\thex\tfilename" :
127 "text\tdata\tbss/stk\tdec\thex\tfilename");
129 puts((radix == octal) ? "text\tdata\tbss\toct\thex\tfilename" :
130 "text\tdata\tbss\tdec\thex\tfilename");
133 display_file ("a.out");
135 for (; optind < argc;)
136 display_file (argv[optind++]);
141 /** Display a file's stats */
149 if (bfd_check_format(abfd, bfd_archive)) return;
151 if (bfd_check_format(abfd, bfd_object)) {
156 if (bfd_check_format(abfd, bfd_core)) {
158 fputs(" (core file", stdout);
160 core_cmd = bfd_core_file_failing_command(abfd);
161 if (core_cmd) printf(" invoked as %s", core_cmd);
167 printf("Unknown file format: %s.", bfd_get_filename(abfd));
177 display_file(filename)
180 bfd *file, *arfile = (bfd *) NULL;
182 file = bfd_openr (filename, target);
184 bfd_perror (filename);
188 if (bfd_check_format(file, bfd_archive) == true) {
191 bfd_error = no_error;
193 arfile = bfd_openr_next_archived_file (file, arfile);
194 if (arfile == NULL) {
195 if (bfd_error != no_more_archived_files)
196 bfd_perror (bfd_get_filename (file));
200 display_bfd (arfile);
201 /* Don't close the archive elements; we need them for next_archive */
210 /* This is what lexical functions are for */
212 lprint_number (width, num)
215 printf ((radix == decimal ? "%-*d\t" :
216 ((radix == octal) ? "%-*o\t" : "%-*x\t")), width, num);
220 rprint_number(width, num)
223 printf ((radix == decimal ? "%*d\t" :
224 ((radix == octal) ? "%*o\t" : "%*x\t")), width, num);
227 static char *bss_section_name = ".bss";
228 static char *data_section_name = ".data";
229 static char *stack_section_name = ".stack";
230 static char *text_section_name = ".text";
232 void print_berkeley_format(abfd)
235 sec_ptr bsssection = NULL;
236 sec_ptr datasection = NULL;
237 sec_ptr textsection = NULL;
238 unsigned long bsssize = 0;
239 unsigned long datasize = 0;
240 unsigned long textsize = 0;
241 unsigned long total = 0;
244 if ((textsection = bfd_get_section_by_name (abfd, text_section_name))
246 textsize = bfd_section_size (abfd, textsection);
249 if ((datasection = bfd_get_section_by_name (abfd, data_section_name))
251 datasize = bfd_section_size(abfd, datasection);
254 if (bfd_get_format (abfd) == bfd_object) {
255 if ((bsssection = bfd_get_section_by_name (abfd, bss_section_name))
257 bsssize = bfd_section_size(abfd, bsssection);
260 if ((bsssection = bfd_get_section_by_name (abfd, stack_section_name))
262 bsssize = bfd_section_size(abfd, bsssection);
266 total = textsize + datasize + bsssize;
268 lprint_number (7, textsize);
269 lprint_number (7, datasize);
270 lprint_number (7, bsssize);
271 printf (((radix == octal) ? "%-7o\t%-7x\t" : "%-7d\t%-7x\t"), total, total);
273 fputs(bfd_get_filename(abfd), stdout);
274 if (abfd->my_archive) printf (" (ex %s)", abfd->my_archive->filename);
277 /* I REALLY miss lexical functions! */
281 sysv_internal_printer(file, sec)
285 int size = bfd_section_size (file, sec);
289 printf ("%-12s", bfd_section_name(file, sec));
290 rprint_number (8, size);
292 rprint_number (8, bfd_section_vma(file, sec));
297 print_sysv_format(file)
302 printf ("%s ", bfd_get_filename (file));
303 if (file->my_archive) printf (" (ex %s)", file->my_archive->filename);
305 puts(":\nsection\t\tsize\t addr");
306 bfd_map_over_sections (file, sysv_internal_printer, NULL);
309 rprint_number(8, svi_total);
310 printf("\n"); printf("\n");
318 print_berkeley_format(file);
319 else print_sysv_format(file);