This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / binutils / size.c
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.
11 */
12 #include "sysdep.h"
13 #include "bfd.h"
14 #include "getopt.h"
15
16
17 #ifndef BSD_DEFAULT
18 #define BSD_DEFAULT 1
19 #endif
20
21 PROTO(void, display_file, (char *filename));
22 PROTO(void, print_sizes,  (bfd *file));
23
24 /* Various program options */
25
26 enum {decimal, octal, hex} radix = decimal;
27 int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output */
28 int show_version = 0;
29 int show_help = 0;
30
31 /* IMPORTS */
32 extern char *program_version;
33 extern char *program_name;
34 extern char *target;
35 \f
36 /** main and like trivia */
37
38 void
39 usage ()
40 {
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);
50 #if BSD_DEFAULT
51   fputs("\t  (Default is +format=Berkeley)", stderr);
52 #else
53   fputs("\t  (Default is +format=SysV)", stderr);
54 #endif
55   fputs("\t-V, +version -- display program version, etc.\n\
56 \t+help -- this message\n", stderr);
57   exit(1);
58 }
59
60 struct option long_options[] = {{"radix",   1, 0, 0},
61                                 {"format",  1, 0, 0},
62                                 {"version", 0, &show_version, 1},
63                                 {"target",  2, NULL, NULL},
64                                 {"help",    0, &show_help, 1},
65                                 {0, 0, 0, 0}};
66
67 int
68 main (argc, argv)
69      int argc;
70      char **argv;
71 {
72   int temp;
73   int c;                        /* sez which option char */
74   int option_index = 0;
75   extern int optind;            /* steps thru options */
76   program_name = *argv;
77
78   while ((c = getopt_long(argc, argv, "ABVdox", long_options,
79                           &option_index)) != EOF)
80     switch(c) {
81     case 0:
82       if (!strcmp("format",(long_options[option_index]).name)) {
83         switch(*optarg) {
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);
87           usage();
88         }
89         break;
90       }
91
92       if (!strcmp("target",(long_options[option_index]).name)) {
93         target = optarg;
94         break;
95       }
96
97       if (!strcmp("radix",(long_options[option_index]).name)) {
98 #ifdef ANSI_LIBRARIES
99         temp = strtol(optarg, NULL, 10);
100 #else
101         temp = atol(optarg);
102 #endif
103         switch(temp) {
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);
108           usage();
109         }
110       }
111       break;
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;
118     case '?': usage();
119     }
120
121   if (show_version) printf("%s version %s\n", program_name, program_version);
122   if (show_help) usage();
123         
124   if (berkeley_format)
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");
128 #else
129     puts((radix == octal) ? "text\tdata\tbss\toct\thex\tfilename" :
130          "text\tdata\tbss\tdec\thex\tfilename");
131 #endif
132   if (optind == argc)
133     display_file ("a.out");
134   else
135     for (; optind < argc;)
136       display_file (argv[optind++]);
137
138   return 0;
139 }
140 \f
141 /** Display a file's stats */
142
143 void
144 display_bfd (abfd)
145      bfd *abfd;
146 {
147   char *core_cmd;
148
149   if (bfd_check_format(abfd, bfd_archive)) return;
150
151   if (bfd_check_format(abfd, bfd_object)) {
152     print_sizes(abfd);
153     goto done;
154   }
155
156   if (bfd_check_format(abfd, bfd_core)) {
157     print_sizes(abfd);
158     fputs(" (core file", stdout);
159
160     core_cmd = bfd_core_file_failing_command(abfd);
161     if (core_cmd) printf(" invoked as %s", core_cmd);
162
163     puts(")");
164     goto done;
165   }
166   
167   printf("Unknown file format: %s.", bfd_get_filename(abfd));
168
169  done:
170
171
172   printf("\n");
173   return;
174 }
175
176 void
177 display_file(filename)
178      char *filename;
179 {
180   bfd *file, *arfile = (bfd *) NULL;
181
182   file = bfd_openr (filename, target);
183   if (file == NULL) {
184     bfd_perror (filename);
185     return;
186   }
187
188   if (bfd_check_format(file, bfd_archive) == true) {
189     for(;;) {
190       
191       bfd_error = no_error;
192
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));
197         return;
198       }
199
200       display_bfd (arfile);
201       /* Don't close the archive elements; we need them for next_archive */
202     }
203   }
204   else
205     display_bfd (file);
206
207   bfd_close (file);
208 }
209 \f
210 /* This is what lexical functions are for */
211 void
212 lprint_number (width, num)
213      int width, num;
214 {
215   printf ((radix == decimal ? "%-*d\t" :
216            ((radix == octal) ? "%-*o\t" : "%-*x\t")), width, num);
217 }
218
219 void
220 rprint_number(width, num)
221      int width, num;
222 {
223   printf ((radix == decimal ? "%*d\t" :
224            ((radix == octal) ? "%*o\t" : "%*x\t")), width, num);
225 }
226
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";
231
232 void print_berkeley_format(abfd)
233 bfd *abfd;
234 {
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;
242
243   
244   if ((textsection = bfd_get_section_by_name (abfd, text_section_name))
245       != NULL) {
246     textsize = bfd_section_size (abfd, textsection);
247   }
248
249   if ((datasection = bfd_get_section_by_name (abfd, data_section_name))
250       != NULL) {
251     datasize = bfd_section_size(abfd, datasection);
252   }
253         
254   if (bfd_get_format (abfd) == bfd_object) {
255     if ((bsssection = bfd_get_section_by_name (abfd, bss_section_name))
256         != NULL) {
257       bsssize = bfd_section_size(abfd, bsssection);
258     }
259   } else {
260     if ((bsssection = bfd_get_section_by_name (abfd, stack_section_name))
261         != NULL) {
262       bsssize = bfd_section_size(abfd, bsssection);
263     }
264   }
265         
266   total = textsize + datasize + bsssize;
267         
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);
272
273   fputs(bfd_get_filename(abfd), stdout);
274   if (abfd->my_archive) printf (" (ex %s)", abfd->my_archive->filename);
275 }
276
277 /* I REALLY miss lexical functions! */
278 int svi_total = 0;
279
280 void
281 sysv_internal_printer(file, sec)
282      bfd *file;
283      sec_ptr sec;
284 {
285   int size = bfd_section_size (file, sec);
286
287   svi_total += size;
288         
289   printf ("%-12s", bfd_section_name(file, sec));
290   rprint_number (8, size);
291   printf(" ");
292   rprint_number (8, bfd_section_vma(file, sec));
293   printf ("\n");
294 }
295
296 void
297 print_sysv_format(file)
298      bfd *file;
299 {
300   svi_total = 0;
301
302   printf ("%s  ", bfd_get_filename (file));
303   if (file->my_archive) printf (" (ex %s)", file->my_archive->filename);
304
305   puts(":\nsection\t\tsize\t     addr");
306   bfd_map_over_sections (file, sysv_internal_printer, NULL);
307
308   printf("Total       ");
309   rprint_number(8, svi_total);
310   printf("\n");  printf("\n");
311 }
312
313 void
314 print_sizes(file)
315      bfd *file;
316 {
317   if (berkeley_format)
318     print_berkeley_format(file);
319   else print_sysv_format(file);
320 }