blob: 4dc7baf3a94634518ed9e2146da2d317f6993d36 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Print size information from ELF file.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Red Hat, Inc.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
4
5 This program is Open Source software; you can redistribute it and/or
6 modify it under the terms of the Open Software License version 1.0 as
7 published by the Open Source Initiative.
8
9 You should have received a copy of the Open Software License along
10 with this program; if not, you may obtain a copy of the Open Software
11 License version 1.0 from http://www.opensource.org/licenses/osl.php or
12 by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
13 3001 King Ranch Road, Ukiah, CA 95482. */
14
15#ifdef HAVE_CONFIG_H
16# include <config.h>
17#endif
18
19#include <argp.h>
20#include <error.h>
21#include <fcntl.h>
22#include <gelf.h>
23#include <inttypes.h>
24#include <libelf.h>
25#include <libintl.h>
26#include <locale.h>
27#include <mcheck.h>
28#include <stdbool.h>
29#include <stdio.h>
30#include <stdio_ext.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34#include <sys/param.h>
35
36#include <system.h>
37
38
39/* Name and version of program. */
40static void print_version (FILE *stream, struct argp_state *state);
41void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
42
43/* Bug report address. */
44const char *argp_program_bug_address = PACKAGE_BUGREPORT;
45
46
47/* Values for the parameters which have no short form. */
48#define OPT_FORMAT 0x100
49#define OPT_RADIX 0x101
50
51/* Definitions of arguments for argp functions. */
52static const struct argp_option options[] =
53{
54 { NULL, 0, NULL, 0, N_("Output format:"), 0 },
55 { "format", OPT_FORMAT, "FORMAT", 0,
56 N_("Use the output format FORMAT. FORMAT can be `bsd' or `sysv'. "
57 "The default is `bsd'"), 0 },
58 { NULL, 'A', NULL, 0, N_("Same as `--format=sysv'"), 0 },
59 { NULL, 'B', NULL, 0, N_("Same as `--format=bsd'"), 0 },
60 { "radix", OPT_RADIX, "RADIX", 0, N_("Use RADIX for printing symbol values"),
61 0},
62 { NULL, 'd', NULL, 0, N_("Same as `--radix=10'"), 0 },
63 { NULL, 'o', NULL, 0, N_("Same as `--radix=8'"), 0 },
64 { NULL, 'x', NULL, 0, N_("Same as `--radix=16'"), 0 },
65 { NULL, 'f', NULL, 0,
66 N_("Similar to `--format=sysv' output but in one line"), 0 },
67
68 { NULL, 0, NULL, 0, N_("Output options:"), 0 },
69 { NULL, 'F', NULL, 0,
70 N_("Print size and permission flags for loadable segments"), 0 },
71 { "totals", 't', NULL, 0, N_("Display the total sizes (bsd only)"), 0 },
72 { NULL, 0, NULL, 0, NULL, 0 }
73};
74
75/* Short description of program. */
76static const char doc[] = N_("\
77List section sizes of FILEs (a.out by default).");
78
79/* Strings for arguments in help texts. */
80static const char args_doc[] = N_("[FILE...]");
81
82/* Prototype for option handler. */
83static error_t parse_opt (int key, char *arg, struct argp_state *state);
84
85/* Data structure to communicate with argp functions. */
86static struct argp argp =
87{
88 options, parse_opt, args_doc, doc, NULL, NULL, NULL
89};
90
91
92/* Print symbols in file named FNAME. */
93static int process_file (const char *fname);
94
95/* Handle content of archive. */
96static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname);
97
98/* Handle ELF file. */
99static void handle_elf (Elf *elf, const char *fullname, const char *fname);
100
101/* Show total size. */
102static void show_bsd_totals (void);
103
104#define INTERNAL_ERROR(fname) \
105 error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s-%s): %s"), \
106 fname, __LINE__, VERSION, __DATE__, elf_errmsg (-1))
107
108
109/* User-selectable options. */
110
111/* The selected output format. */
112static enum
113{
114 format_bsd = 0,
115 format_sysv,
116 format_sysv_one_line,
117 format_segments
118} format;
119
120/* Radix for printed numbers. */
121static enum
122{
123 radix_decimal = 0,
124 radix_hex,
125 radix_octal
126} radix;
127
128
129/* Mapping of radix and binary class to length. */
130static const int length_map[2][3] =
131{
132 [ELFCLASS32 - 1] =
133 {
134 [radix_hex] = 8,
135 [radix_decimal] = 10,
136 [radix_octal] = 11
137 },
138 [ELFCLASS64 - 1] =
139 {
140 [radix_hex] = 16,
141 [radix_decimal] = 20,
142 [radix_octal] = 22
143 }
144};
145
146/* True if total sizes should be printed. */
147static bool totals;
148/* To print the total sizes in a reasonable format remember the higest
149 "class" of ELF binaries processed. */
150static int totals_class;
151
152
153int
154main (int argc, char *argv[])
155{
156 int remaining;
157 int result = 0;
158
159 /* Make memory leak detection possible. */
160 mtrace ();
161
162 /* We use no threads here which can interfere with handling a stream. */
163 __fsetlocking (stdin, FSETLOCKING_BYCALLER);
164 __fsetlocking (stdout, FSETLOCKING_BYCALLER);
165 __fsetlocking (stderr, FSETLOCKING_BYCALLER);
166
167 /* Set locale. */
168 setlocale (LC_ALL, "");
169
170 /* Make sure the message catalog can be found. */
171 bindtextdomain (PACKAGE, LOCALEDIR);
172
173 /* Initialize the message catalog. */
174 textdomain (PACKAGE);
175
176 /* Parse and process arguments. */
177 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
178
179
180 /* Tell the library which version we are expecting. */
181 elf_version (EV_CURRENT);
182
183 if (remaining == argc)
184 /* The user didn't specify a name so we use a.out. */
185 result = process_file ("a.out");
186 else
187 /* Process all the remaining files. */
188 do
189 result |= process_file (argv[remaining]);
190 while (++remaining < argc);
191
192 /* Print the total sizes but only if the output format is BSD and at
193 least one file has been correctly read (i.e., we recognized the
194 class). */
195 if (totals && format == format_bsd && totals_class != 0)
196 show_bsd_totals ();
197
198 return result;
199}
200
201
202/* Print the version information. */
203static void
204print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
205{
206 fprintf (stream, "size (%s) %s\n", PACKAGE_NAME, VERSION);
207 fprintf (stream, gettext ("\
208Copyright (C) %s Red Hat, Inc.\n\
209This is free software; see the source for copying conditions. There is NO\n\
210warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
211"), "2005");
212 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
213}
214
215
216/* Handle program arguments. */
217static error_t
218parse_opt (int key, char *arg,
219 struct argp_state *state __attribute__ ((unused)))
220{
221 switch (key)
222 {
223 case 'd':
224 radix = radix_decimal;
225 break;
226
227 case 'f':
228 format = format_sysv_one_line;
229 break;
230
231 case 'o':
232 radix = radix_octal;
233 break;
234
235 case 'x':
236 radix = radix_hex;
237 break;
238
239 case 'A':
240 format = format_sysv;
241 break;
242
243 case 'B':
244 format = format_bsd;
245 break;
246
247 case 'F':
248 format = format_segments;
249 break;
250
251 case OPT_FORMAT:
252 if (strcmp (arg, "bsd") == 0 || strcmp (arg, "berkeley") == 0)
253 format = format_bsd;
254 else if (strcmp (arg, "sysv") == 0)
255 format = format_sysv;
256 else
257 error (EXIT_FAILURE, 0, gettext ("Invalid format: %s"), arg);
258 break;
259
260 case OPT_RADIX:
261 if (strcmp (arg, "x") == 0 || strcmp (arg, "16") == 0)
262 radix = radix_hex;
263 else if (strcmp (arg, "d") == 0 || strcmp (arg, "10") == 0)
264 radix = radix_decimal;
265 else if (strcmp (arg, "o") == 0 || strcmp (arg, "8") == 0)
266 radix = radix_octal;
267 else
268 error (EXIT_FAILURE, 0, gettext ("Invalid radix: %s"), arg);
269 break;
270
271 case 't':
272 totals = true;
273 break;
274
275 default:
276 return ARGP_ERR_UNKNOWN;
277 }
278 return 0;
279}
280
281
282static int
283process_file (const char *fname)
284{
285 /* Open the file and determine the type. */
286 int fd;
287 Elf *elf;
288
289 /* Open the file. */
290 fd = open (fname, O_RDONLY);
291 if (fd == -1)
292 {
293 error (0, errno, gettext ("cannot open '%s"), fname);
294 return 1;
295 }
296
297 /* Now get the ELF descriptor. */
298 elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
299 if (elf != NULL)
300 {
301 if (elf_kind (elf) == ELF_K_ELF)
302 {
303 handle_elf (elf, NULL, fname);
304
305 if (elf_end (elf) != 0)
306 INTERNAL_ERROR (fname);
307
308 if (close (fd) != 0)
309 error (EXIT_FAILURE, errno, gettext ("while close '%s'"), fname);
310
311 return 0;
312 }
313 else
314 return handle_ar (fd, elf, NULL, fname);
315
316 /* We cannot handle this type. Close the descriptor anyway. */
317 if (elf_end (elf) != 0)
318 INTERNAL_ERROR (fname);
319 }
320
321 error (0, 0, gettext ("%s: file format not recognized"), fname);
322
323 return 1;
324}
325
326
327/* Print the BSD-style header. This is done exactly once. */
328static void
329print_header (Elf *elf)
330{
331 static int done;
332
333 if (! done)
334 {
335 int ddigits = length_map[gelf_getclass (elf) - 1][radix_decimal];
336 int xdigits = length_map[gelf_getclass (elf) - 1][radix_hex];
337
338 printf ("%*s %*s %*s %*s %*s %s\n",
339 ddigits - 2, sgettext ("bsd|text"),
340 ddigits - 2, sgettext ("bsd|data"),
341 ddigits - 2, sgettext ("bsd|bss"),
342 ddigits - 2, sgettext ("bsd|dec"),
343 xdigits - 2, sgettext ("bsd|hex"),
344 sgettext ("bsd|filename"));
345
346 done = 1;
347 }
348}
349
350
351static int
352handle_ar (int fd, Elf *elf, const char *prefix, const char *fname)
353{
354 Elf *subelf;
355 Elf_Cmd cmd = ELF_C_READ_MMAP;
356 size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
357 size_t fname_len = strlen (fname) + 1;
358 char new_prefix[prefix_len + 1 + fname_len];
359 int result = 0;
360 char *cp = new_prefix;
361
362 /* Create the full name of the file. */
363 if (prefix != NULL)
364 {
365 cp = mempcpy (cp, prefix, prefix_len);
366 *cp++ = ':';
367 }
368 memcpy (cp, fname, fname_len);
369
370 /* Process all the files contained in the archive. */
371 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
372 {
373 /* The the header for this element. */
374 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
375
376 if (elf_kind (subelf) == ELF_K_ELF)
377 handle_elf (subelf, new_prefix, arhdr->ar_name);
378 else if (elf_kind (subelf) == ELF_K_AR)
379 result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name);
380 /* else signal error??? */
381
382 /* Get next archive element. */
383 cmd = elf_next (subelf);
384 if (elf_end (subelf) != 0)
385 INTERNAL_ERROR (fname);
386 }
387
388 if (elf_end (elf) != 0)
389 INTERNAL_ERROR (fname);
390
391 if (close (fd) != 0)
392 error (EXIT_FAILURE, errno, gettext ("while closing `%s'"), fname);
393
394 return result;
395}
396
397
398/* Show sizes in SysV format. */
399static void
400show_sysv (Elf *elf, const char *prefix, const char *fname,
401 const char *fullname)
402{
403 size_t shstrndx;
404 Elf_Scn *scn = NULL;
405 GElf_Shdr shdr_mem;
406 int maxlen = 10;
407 int digits = length_map[gelf_getclass (elf) - 1][radix];
408 const char *fmtstr;
409 GElf_Off total = 0;
410
411 /* Get the section header string table index. */
412 if (elf_getshstrndx (elf, &shstrndx) < 0)
413 error (EXIT_FAILURE, 0,
414 gettext ("cannot get section header string table index"));
415
416 /* First round over the sections: determine the longest section name. */
417 while ((scn = elf_nextscn (elf, scn)) != NULL)
418 {
419 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
420
421 if (shdr == NULL)
422 INTERNAL_ERROR (fullname);
423
424 /* Ignore all sections which are not used at runtime. */
425 if ((shdr->sh_flags & SHF_ALLOC) != 0)
426 maxlen = MAX (maxlen,
427 (int) strlen (elf_strptr (elf, shstrndx,
428 shdr->sh_name)));
429 }
430
431 fputs_unlocked (fname, stdout);
432 if (prefix != NULL)
433 printf (gettext (" (ex %s)"), prefix);
434 printf (":\n%-*s %*s %*s\n",
435 maxlen, sgettext ("sysv|section"),
436 digits - 2, sgettext ("sysv|size"),
437 digits, sgettext ("sysv|addr"));
438
439 if (radix == radix_hex)
440 fmtstr = "%-*s %*" PRIx64 " %*" PRIx64 "\n";
441 else if (radix == radix_decimal)
442 fmtstr = "%-*s %*" PRId64 " %*" PRId64 "\n";
443 else
444 fmtstr = "%-*s %*" PRIo64 " %*" PRIo64 "\n";
445
446 /* Iterate over all sections. */
447 while ((scn = elf_nextscn (elf, scn)) != NULL)
448 {
449 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
450
451 /* Ignore all sections which are not used at runtime. */
452 if ((shdr->sh_flags & SHF_ALLOC) != 0)
453 {
454 printf (fmtstr,
455 maxlen, elf_strptr (elf, shstrndx, shdr->sh_name),
456 digits - 2, shdr->sh_size,
457 digits, shdr->sh_addr);
458
459 total += shdr->sh_size;
460 }
461 }
462
463 if (radix == radix_hex)
464 printf ("%-*s %*" PRIx64 "\n\n\n", maxlen, sgettext ("sysv|Total"),
465 digits - 2, total);
466 else if (radix == radix_decimal)
467 printf ("%-*s %*" PRId64 "\n\n\n", maxlen, sgettext ("sysv|Total"),
468 digits - 2, total);
469 else
470 printf ("%-*s %*" PRIo64 "\n\n\n", maxlen, sgettext ("sysv|Total"),
471 digits - 2, total);
472}
473
474
475/* Show sizes in SysV format in one line. */
476static void
477show_sysv_one_line (Elf *elf)
478{
479 size_t shstrndx;
480 Elf_Scn *scn = NULL;
481 GElf_Shdr shdr_mem;
482 const char *fmtstr;
483 GElf_Off total = 0;
484 int first = 1;
485
486 /* Get the section header string table index. */
487 if (elf_getshstrndx (elf, &shstrndx) < 0)
488 error (EXIT_FAILURE, 0,
489 gettext ("cannot get section header string table index"));
490
491 if (radix == radix_hex)
492 fmtstr = "%" PRIx64 "(%s)";
493 else if (radix == radix_decimal)
494 fmtstr = "%" PRId64 "(%s)";
495 else
496 fmtstr = "%" PRIo64 "(%s)";
497
498 /* Iterate over all sections. */
499 while ((scn = elf_nextscn (elf, scn)) != NULL)
500 {
501 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
502
503 /* Ignore all sections which are not used at runtime. */
504 if ((shdr->sh_flags & SHF_ALLOC) == 0)
505 continue;
506
507 if (! first)
508 fputs_unlocked (" + ", stdout);
509 first = 0;
510
511 printf (fmtstr, shdr->sh_size,
512 elf_strptr (elf, shstrndx, shdr->sh_name));
513
514 total += shdr->sh_size;
515 }
516
517 if (radix == radix_hex)
518 printf (" = %#" PRIx64 "\n", total);
519 else if (radix == radix_decimal)
520 printf (" = %" PRId64 "\n", total);
521 else
522 printf (" = %" PRIo64 "\n", total);
523}
524
525
526/* Variables to add up the sizes of all files. */
527static uintmax_t total_textsize;
528static uintmax_t total_datasize;
529static uintmax_t total_bsssize;
530
531
532/* Show sizes in BSD format. */
533static void
534show_bsd (Elf *elf, const char *prefix, const char *fname,
535 const char *fullname)
536{
537 Elf_Scn *scn = NULL;
538 GElf_Shdr shdr_mem;
539 GElf_Off textsize = 0;
540 GElf_Off datasize = 0;
541 GElf_Off bsssize = 0;
542 int ddigits = length_map[gelf_getclass (elf) - 1][radix_decimal];
543 int xdigits = length_map[gelf_getclass (elf) - 1][radix_hex];
544
545 /* Iterate over all sections. */
546 while ((scn = elf_nextscn (elf, scn)) != NULL)
547 {
548 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
549
550 if (shdr == NULL)
551 INTERNAL_ERROR (fullname);
552
553 /* Ignore all sections which are not marked as loaded. */
554 if ((shdr->sh_flags & SHF_ALLOC) == 0)
555 continue;
556
557 if ((shdr->sh_flags & SHF_WRITE) == 0)
558 textsize += shdr->sh_size;
559 else if (shdr->sh_type == SHT_NOBITS)
560 bsssize += shdr->sh_size;
561 else
562 datasize += shdr->sh_size;
563 }
564
565 printf ("%*" PRId64 " %*" PRId64 " %*" PRId64 " %*" PRId64 " %*"
566 PRIx64 " %s",
567 ddigits - 2, textsize,
568 ddigits - 2, datasize,
569 ddigits - 2, bsssize,
570 ddigits - 2, textsize + datasize + bsssize,
571 xdigits - 2, textsize + datasize + bsssize,
572 fname);
573 if (prefix != NULL)
574 printf (gettext (" (ex %s)"), prefix);
575 fputs_unlocked ("\n", stdout);
576
577 total_textsize += textsize;
578 total_datasize += datasize;
579 total_bsssize += bsssize;
580
581 totals_class = MAX (totals_class, gelf_getclass (elf));
582}
583
584
585/* Show total size. */
586static void
587show_bsd_totals (void)
588{
589 int ddigits = length_map[totals_class - 1][radix_decimal];
590 int xdigits = length_map[totals_class - 1][radix_hex];
591
592 printf ("%*" PRIuMAX " %*" PRIuMAX " %*" PRIuMAX " %*" PRIuMAX " %*"
593 PRIxMAX " %s",
594 ddigits - 2, total_textsize,
595 ddigits - 2, total_datasize,
596 ddigits - 2, total_bsssize,
597 ddigits - 2, total_textsize + total_datasize + total_bsssize,
598 xdigits - 2, total_textsize + total_datasize + total_bsssize,
599 gettext ("(TOTALS)\n"));
600}
601
602
603/* Show size and permission of loadable segments. */
604static void
605show_segments (Elf *elf, const char *fullname)
606{
607 GElf_Ehdr ehdr_mem;
608 GElf_Ehdr *ehdr;
609 size_t cnt;
610 GElf_Off total = 0;
611 int first = 1;
612
613 ehdr = gelf_getehdr (elf, &ehdr_mem);
614 if (ehdr == NULL)
615 INTERNAL_ERROR (fullname);
616
617 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt)
618 {
619 GElf_Phdr phdr_mem;
620 GElf_Phdr *phdr;
621
622 phdr = gelf_getphdr (elf, cnt, &phdr_mem);
623 if (phdr == NULL)
624 INTERNAL_ERROR (fullname);
625
626 if (phdr->p_type != PT_LOAD)
627 /* Only load segments. */
628 continue;
629
630 if (! first)
631 fputs_unlocked (" + ", stdout);
632 first = 0;
633
634 printf (radix == radix_hex ? "%" PRIx64 "(%c%c%c)"
635 : (radix == radix_decimal ? "%" PRId64 "(%c%c%c)"
636 : "%" PRIo64 "(%c%c%c)"),
637 phdr->p_memsz,
638 (phdr->p_flags & PF_R) == 0 ? '-' : 'r',
639 (phdr->p_flags & PF_W) == 0 ? '-' : 'w',
640 (phdr->p_flags & PF_X) == 0 ? '-' : 'x');
641
642 total += phdr->p_memsz;
643 }
644
645 if (radix == radix_hex)
646 printf (" = %#" PRIx64 "\n", total);
647 else if (radix == radix_decimal)
648 printf (" = %" PRId64 "\n", total);
649 else
650 printf (" = %" PRIo64 "\n", total);
651}
652
653
654static void
655handle_elf (Elf *elf, const char *prefix, const char *fname)
656{
657 size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
658 size_t fname_len = strlen (fname) + 1;
659 char fullname[prefix_len + 1 + fname_len];
660 char *cp = fullname;
661
662 /* Create the full name of the file. */
663 if (prefix != NULL)
664 {
665 cp = mempcpy (cp, prefix, prefix_len);
666 *cp++ = ':';
667 }
668 memcpy (cp, fname, fname_len);
669
670 if (format == format_sysv)
671 show_sysv (elf, prefix, fname, fullname);
672 else if (format == format_sysv_one_line)
673 show_sysv_one_line (elf);
674 else if (format == format_segments)
675 show_segments (elf, fullname);
676 else
677 {
678 print_header (elf);
679
680 show_bsd (elf, prefix, fname, fullname);
681 }
682}