1 /* Print information from ELF file in human-readable form.
2 Copyright (C) 2005, 2006, 2007, 2009, 2011, 2012 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2005.
6 This file 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 3 of the License, or
9 (at your option) any later version.
11 elfutils is distributed in the hope that it will be useful, but
12 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, see <http://www.gnu.org/licenses/>. */
32 #include <stdio_ext.h>
36 #include <sys/param.h>
39 #include "../libebl/libeblP.h"
42 /* Name and version of program. */
43 static void print_version (FILE *stream, struct argp_state *state);
44 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
46 /* Bug report address. */
47 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
50 /* Definitions of arguments for argp functions. */
51 static const struct argp_option options[] =
53 { NULL, 0, NULL, 0, N_("Mode selection:"), 0 },
54 { "reloc", 'r', NULL, 0, N_("Display relocation information."), 0 },
55 { "full-contents", 's', NULL, 0,
56 N_("Display the full contents of all sections requested"), 0 },
57 { "disassemble", 'd', NULL, 0,
58 N_("Display assembler code of executable sections"), 0 },
60 { NULL, 0, NULL, 0, N_("Output content selection:"), 0 },
61 { "section", 'j', "NAME", 0,
62 N_("Only display information for section NAME."), 0 },
64 { NULL, 0, NULL, 0, NULL, 0 }
67 /* Short description of program. */
68 static const char doc[] = N_("\
69 Show information from FILEs (a.out by default).");
71 /* Strings for arguments in help texts. */
72 static const char args_doc[] = N_("[FILE...]");
74 /* Prototype for option handler. */
75 static error_t parse_opt (int key, char *arg, struct argp_state *state);
77 /* Parser children. */
78 static struct argp_child argp_children[] =
80 { &color_argp, 0, N_("Output formatting"), 2 },
84 /* Data structure to communicate with argp functions. */
85 static const struct argp argp =
87 options, parse_opt, args_doc, doc, argp_children, NULL, NULL
91 /* Print symbols in file named FNAME. */
92 static int process_file (const char *fname, bool more_than_one);
94 /* Handle content of archive. */
95 static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
98 /* Handle ELF file. */
99 static int handle_elf (Elf *elf, const char *prefix, const char *fname,
103 #define INTERNAL_ERROR(fname) \
104 error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s-%s): %s"), \
105 fname, __LINE__, PACKAGE_VERSION, __DATE__, elf_errmsg (-1))
108 /* List of sections which should be used. */
109 static struct section_list
117 struct section_list *next;
121 /* If true print archive index. */
122 static bool print_relocs;
124 /* If true print full contents of requested sections. */
125 static bool print_full_content;
127 /* If true print disassembled output.. */
128 static bool print_disasm;
132 main (int argc, char *argv[])
134 /* Make memory leak detection possible. */
137 /* We use no threads here which can interfere with handling a stream. */
138 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
139 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
140 (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER);
143 (void) setlocale (LC_ALL, "");
145 /* Make sure the message catalog can be found. */
146 (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
148 /* Initialize the message catalog. */
149 (void) textdomain (PACKAGE_TARNAME);
151 /* Parse and process arguments. */
153 (void) argp_parse (&argp, argc, argv, 0, &remaining, NULL);
155 /* Tell the library which version we are expecting. */
156 (void) elf_version (EV_CURRENT);
159 if (remaining == argc)
160 /* The user didn't specify a name so we use a.out. */
161 result = process_file ("a.out", false);
164 /* Process all the remaining files. */
165 const bool more_than_one = remaining + 1 < argc;
168 result |= process_file (argv[remaining], more_than_one);
169 while (++remaining < argc);
176 /* Print the version information. */
178 print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
180 fprintf (stream, "objdump (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
181 fprintf (stream, gettext ("\
182 Copyright (C) %s Red Hat, Inc.\n\
183 This is free software; see the source for copying conditions. There is NO\n\
184 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
186 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
190 /* Handle program arguments. */
192 parse_opt (int key, char *arg,
193 struct argp_state *state __attribute__ ((unused)))
195 /* True if any of the control options is set. */
196 static bool any_control_option;
202 struct section_list *newp = xmalloc (sizeof (*newp));
204 newp->scnndx = strtoul (arg, &endp, 0);
206 newp->is_name = false;
210 newp->is_name = true;
212 newp->next = section_list;
215 any_control_option = true;
220 any_control_option = true;
225 any_control_option = true;
229 print_full_content = true;
230 any_control_option = true;
234 if (! any_control_option)
236 fputs (gettext ("No operation specified.\n"), stderr);
237 argp_help (&argp, stderr, ARGP_HELP_SEE,
238 program_invocation_short_name);
243 return ARGP_ERR_UNKNOWN;
249 /* Open the file and determine the type. */
251 process_file (const char *fname, bool more_than_one)
254 int fd = open (fname, O_RDONLY);
257 error (0, errno, gettext ("cannot open %s"), fname);
261 /* Now get the ELF descriptor. */
262 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
265 if (elf_kind (elf) == ELF_K_ELF)
267 int result = handle_elf (elf, more_than_one ? "" : NULL,
270 if (elf_end (elf) != 0)
271 INTERNAL_ERROR (fname);
274 error (EXIT_FAILURE, errno, gettext ("while close `%s'"), fname);
278 else if (elf_kind (elf) == ELF_K_AR)
280 int result = handle_ar (fd, elf, NULL, fname, NULL);
282 if (elf_end (elf) != 0)
283 INTERNAL_ERROR (fname);
286 error (EXIT_FAILURE, errno, gettext ("while close `%s'"), fname);
291 /* We cannot handle this type. Close the descriptor anyway. */
292 if (elf_end (elf) != 0)
293 INTERNAL_ERROR (fname);
296 error (0, 0, gettext ("%s: File format not recognized"), fname);
303 handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
306 size_t fname_len = strlen (fname) + 1;
307 size_t prefix_len = prefix != NULL ? strlen (prefix) : 0;
308 char new_prefix[prefix_len + fname_len + 2];
309 size_t suffix_len = suffix != NULL ? strlen (suffix) : 0;
310 char new_suffix[suffix_len + 2];
312 Elf_Cmd cmd = ELF_C_READ_MMAP;
315 char *cp = new_prefix;
317 cp = stpcpy (cp, prefix);
318 cp = stpcpy (cp, fname);
323 cp = stpcpy (cp, suffix);
326 /* Process all the files contained in the archive. */
327 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
329 /* The the header for this element. */
330 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
332 /* Skip over the index entries. */
333 if (strcmp (arhdr->ar_name, "/") != 0
334 && strcmp (arhdr->ar_name, "//") != 0)
336 if (elf_kind (subelf) == ELF_K_ELF)
337 result |= handle_elf (subelf, new_prefix, arhdr->ar_name,
339 else if (elf_kind (subelf) == ELF_K_AR)
340 result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name,
344 error (0, 0, gettext ("%s%s%s: file format not recognized"),
345 new_prefix, arhdr->ar_name, new_suffix);
350 /* Get next archive element. */
351 cmd = elf_next (subelf);
352 if (elf_end (subelf) != 0)
353 INTERNAL_ERROR (fname);
361 show_relocs_x (Ebl *ebl, GElf_Shdr *shdr, Elf_Data *symdata,
362 Elf_Data *xndxdata, size_t symstrndx, size_t shstrndx,
363 GElf_Addr r_offset, GElf_Xword r_info, GElf_Sxword r_addend)
365 int elfclass = gelf_getclass (ebl->elf);
368 printf ("%0*" PRIx64 " %-20s ",
369 elfclass == ELFCLASS32 ? 8 : 16, r_offset,
370 ebl_reloc_type_name (ebl, GELF_R_TYPE (r_info), buf, sizeof (buf)));
374 GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata, GELF_R_SYM (r_info),
379 gettext ("INVALID SYMBOL"), (long int) GELF_R_SYM (r_info));
380 else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
382 elf_strptr (ebl->elf, symstrndx, sym->st_name));
385 GElf_Shdr destshdr_mem;
387 destshdr = gelf_getshdr (elf_getscn (ebl->elf,
388 sym->st_shndx == SHN_XINDEX
389 ? xndx : sym->st_shndx),
394 gettext ("INVALID SECTION"),
395 (long int) (sym->st_shndx == SHN_XINDEX
396 ? xndx : sym->st_shndx));
399 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
408 r_addend = -r_addend;
410 printf ("%c%#" PRIx64, sign, r_addend);
417 show_relocs_rel (Ebl *ebl, GElf_Shdr *shdr, Elf_Data *data,
418 Elf_Data *symdata, Elf_Data *xndxdata, size_t symstrndx,
421 int nentries = shdr->sh_size / shdr->sh_entsize;
423 for (int cnt = 0; cnt < nentries; ++cnt)
428 rel = gelf_getrel (data, cnt, &relmem);
430 show_relocs_x (ebl, shdr, symdata, xndxdata, symstrndx, shstrndx,
431 rel->r_offset, rel->r_info, 0);
437 show_relocs_rela (Ebl *ebl, GElf_Shdr *shdr, Elf_Data *data,
438 Elf_Data *symdata, Elf_Data *xndxdata, size_t symstrndx,
441 int nentries = shdr->sh_size / shdr->sh_entsize;
443 for (int cnt = 0; cnt < nentries; ++cnt)
448 rel = gelf_getrela (data, cnt, &relmem);
450 show_relocs_x (ebl, shdr, symdata, xndxdata, symstrndx, shstrndx,
451 rel->r_offset, rel->r_info, rel->r_addend);
457 section_match (Elf *elf, uint32_t scnndx, GElf_Shdr *shdr, size_t shstrndx)
459 if (section_list == NULL)
462 struct section_list *runp = section_list;
468 if (strcmp (runp->name,
469 elf_strptr (elf, shstrndx, shdr->sh_name)) == 0)
474 if (runp->scnndx == scnndx)
480 while (runp != NULL);
487 show_relocs (Ebl *ebl, const char *fname, uint32_t shstrndx)
489 int elfclass = gelf_getclass (ebl->elf);
492 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
495 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
498 INTERNAL_ERROR (fname);
500 if (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
502 if (! section_match (ebl->elf, elf_ndxscn (scn), shdr, shstrndx))
505 GElf_Shdr destshdr_mem;
506 GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf,
510 printf (gettext ("\nRELOCATION RECORDS FOR [%s]:\n"
511 "%-*s TYPE VALUE\n"),
512 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
513 elfclass == ELFCLASS32 ? 8 : 16, gettext ("OFFSET"));
515 /* Get the data of the section. */
516 Elf_Data *data = elf_getdata (scn, NULL);
520 /* Get the symbol table information. */
521 Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
522 GElf_Shdr symshdr_mem;
523 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
524 Elf_Data *symdata = elf_getdata (symscn, NULL);
526 /* Search for the optional extended section index table. */
527 Elf_Data *xndxdata = NULL;
528 Elf_Scn *xndxscn = NULL;
529 while ((xndxscn = elf_nextscn (ebl->elf, xndxscn)) != NULL)
531 GElf_Shdr xndxshdr_mem;
534 xndxshdr = gelf_getshdr (xndxscn, &xndxshdr_mem);
535 if (xndxshdr != NULL && xndxshdr->sh_type == SHT_SYMTAB_SHNDX
536 && xndxshdr->sh_link == elf_ndxscn (symscn))
539 xndxdata = elf_getdata (xndxscn, NULL);
544 if (shdr->sh_type == SHT_REL)
545 show_relocs_rel (ebl, shdr, data, symdata, xndxdata,
546 symshdr->sh_link, shstrndx);
548 show_relocs_rela (ebl, shdr, data, symdata, xndxdata,
549 symshdr->sh_link, shstrndx);
560 show_full_content (Ebl *ebl, const char *fname, uint32_t shstrndx)
563 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
566 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
569 INTERNAL_ERROR (fname);
571 if (shdr->sh_type == SHT_PROGBITS && shdr->sh_size > 0)
573 if (! section_match (ebl->elf, elf_ndxscn (scn), shdr, shstrndx))
576 printf (gettext ("Contents of section %s:\n"),
577 elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
579 /* Get the data of the section. */
580 Elf_Data *data = elf_getdata (scn, NULL);
584 unsigned char *cp = data->d_buf;
586 for (cnt = 0; cnt + 16 < data->d_size; cp += 16, cnt += 16)
588 printf (" %04zx ", cnt);
590 for (size_t inner = 0; inner < 16; inner += 4)
591 printf ("%02hhx%02hhx%02hhx%02hhx ",
592 cp[inner], cp[inner + 1], cp[inner + 2],
594 fputc_unlocked (' ', stdout);
596 for (size_t inner = 0; inner < 16; ++inner)
597 fputc_unlocked (isascii (cp[inner]) && isprint (cp[inner])
598 ? cp[inner] : '.', stdout);
599 fputc_unlocked ('\n', stdout);
602 printf (" %04zx ", cnt);
604 size_t remaining = data->d_size - cnt;
606 for (inner = 0; inner + 4 <= remaining; inner += 4)
607 printf ("%02hhx%02hhx%02hhx%02hhx ",
608 cp[inner], cp[inner + 1], cp[inner + 2], cp[inner + 3]);
610 for (; inner < remaining; ++inner)
611 printf ("%02hhx", cp[inner]);
613 for (inner = 2 * (16 - inner) + (16 - inner + 3) / 4 + 1; inner > 0;
615 fputc_unlocked (' ', stdout);
617 for (inner = 0; inner < remaining; ++inner)
618 fputc_unlocked (isascii (cp[inner]) && isprint (cp[inner])
619 ? cp[inner] : '.', stdout);
620 fputc_unlocked ('\n', stdout);
622 fputc_unlocked ('\n', stdout);
634 const uint8_t *last_end;
635 const char *address_color;
636 const char *bytes_color;
640 // XXX This is not the preferred output for all architectures. Needs
641 // XXX customization, too.
643 disasm_output (char *buf, size_t buflen, void *arg)
645 struct disasm_info *info = (struct disasm_info *) arg;
647 if (info->address_color != NULL)
648 printf ("%s%8" PRIx64 "%s: ",
649 info->address_color, (uint64_t) info->addr, color_off);
651 printf ("%8" PRIx64 ": ", (uint64_t) info->addr);
653 if (info->bytes_color != NULL)
654 fputs_unlocked (info->bytes_color, stdout);
656 for (cnt = 0; cnt < (size_t) MIN (info->cur - info->last_end, 8); ++cnt)
657 printf (" %02" PRIx8, info->last_end[cnt]);
658 if (info->bytes_color != NULL)
659 fputs_unlocked (color_off, stdout);
661 printf ("%*s %.*s\n",
662 (int) (8 - cnt) * 3 + 1, "", (int) buflen, buf);
666 /* We limit the number of bytes printed before the mnemonic to 8.
667 Print the rest on a separate, following line. */
668 if (info->cur - info->last_end > 8)
670 if (info->address_color != NULL)
671 printf ("%s%8" PRIx64 "%s: ",
672 info->address_color, (uint64_t) info->addr, color_off);
674 printf ("%8" PRIx64 ": ", (uint64_t) info->addr);
676 if (info->bytes_color != NULL)
677 fputs_unlocked (info->bytes_color, stdout);
678 for (; cnt < (size_t) (info->cur - info->last_end); ++cnt)
679 printf (" %02" PRIx8, info->last_end[cnt]);
680 if (info->bytes_color != NULL)
681 fputs_unlocked (color_off, stdout);
682 putchar_unlocked ('\n');
683 info->addr += info->cur - info->last_end - 8;
686 info->last_end = info->cur;
693 show_disasm (Ebl *ebl, const char *fname, uint32_t shstrndx)
695 DisasmCtx_t *ctx = disasm_begin (ebl, ebl->elf, NULL /* XXX TODO */);
697 error (EXIT_FAILURE, 0, gettext ("cannot disassemble"));
700 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
703 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
706 INTERNAL_ERROR (fname);
708 if (shdr->sh_type == SHT_PROGBITS && shdr->sh_size > 0
709 && (shdr->sh_flags & SHF_EXECINSTR) != 0)
711 if (! section_match (ebl->elf, elf_ndxscn (scn), shdr, shstrndx))
714 Elf_Data *data = elf_getdata (scn, NULL);
718 printf ("Disassembly of section %s:\n\n",
719 elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
721 struct disasm_info info;
722 info.addr = shdr->sh_addr;
723 info.last_end = info.cur = data->d_buf;
727 info.address_color = color_address;
728 info.bytes_color = color_bytes;
730 if (asprintf (&fmt, "%s%%7m %s%%.1o,%s%%.2o,%s%%.3o%%34a %s%%l",
731 color_mnemonic ?: "",
732 color_operand1 ?: "",
733 color_operand2 ?: "",
734 color_operand3 ?: "",
735 color_label ?: "") < 0)
736 error (EXIT_FAILURE, errno, _("cannot allocate memory"));
740 info.address_color = info.bytes_color = NULL;
742 fmt = "%7m %.1o,%.2o,%.3o%34a %l";
745 disasm_cb (ctx, &info.cur, info.cur + data->d_size, info.addr,
746 fmt, disasm_output, &info, NULL /* XXX */);
753 (void) disasm_end (ctx);
760 handle_elf (Elf *elf, const char *prefix, const char *fname,
764 /* Get the backend for this object file type. */
765 Ebl *ebl = ebl_openbackend (elf);
767 printf ("%s: elf%d-%s\n\n",
768 fname, gelf_getclass (elf) == ELFCLASS32 ? 32 : 64,
769 ebl_backend_name (ebl));
771 /* Create the full name of the file. */
772 size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
773 size_t suffix_len = suffix == NULL ? 0 : strlen (suffix);
774 size_t fname_len = strlen (fname) + 1;
775 char fullname[prefix_len + 1 + fname_len + suffix_len];
778 cp = mempcpy (cp, prefix, prefix_len);
779 cp = mempcpy (cp, fname, fname_len);
781 memcpy (cp - 1, suffix, suffix_len + 1);
783 /* Get the section header string table index. */
785 if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
786 error (EXIT_FAILURE, 0,
787 gettext ("cannot get section header string table index"));
791 result = show_disasm (ebl, fullname, shstrndx);
792 if (print_relocs && !print_disasm)
793 result = show_relocs (ebl, fullname, shstrndx);
794 if (print_full_content)
795 result = show_full_content (ebl, fullname, shstrndx);
797 /* Close the ELF backend library descriptor. */
798 ebl_closebackend (ebl);
804 #include "debugpred.h"