1 /* Copyright (C) 1998, 1999, 2000, 2002 Red Hat, Inc.
2 This file is part of elfutils.
3 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 static const char *machines[] =
32 #define MACHINE(name) [name] = #name
41 MACHINE (EM_MIPS_RS3_LE),
44 MACHINE (EM_SPARC32PLUS),
53 MACHINE (EM_FAKE_ALPHA),
64 MACHINE (EM_COLDFIRE),
70 MACHINE (EM_STARCORE),
88 main (int argc, char *argv[])
97 /* Recognize optional verbosity flag. */
98 if (arg < argc && strcmp (argv[arg], "-v") == 0)
104 /* Any more arguments available. */
106 error (EXIT_FAILURE, 0, "No input file given");
108 /* Open the input file. */
109 fd = open (argv[arg], O_RDONLY);
112 perror ("cannot open input file");
116 /* Set the ELF version we are using here. */
117 if (elf_version (EV_CURRENT) == EV_NONE)
119 puts ("ELF library too old");
123 /* Start reading the file. */
125 elf = elf_begin (fd, cmd, NULL);
128 printf ("elf_begin: %s\n", elf_errmsg (-1));
132 /* If it is no archive punt. */
133 if (elf_kind (elf) != ELF_K_AR)
135 printf ("%s is not an archive\n", argv[1]);
141 /* The verbose variant. We print a lot of information. */
146 /* Get the elements of the archive one after the other. */
147 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
149 /* The the header for this element. */
150 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
154 printf ("cannot get arhdr: %s\n", elf_errmsg (-1));
158 switch (elf_kind (subelf))
161 fputs ("ELF file:\n", stdout);
165 fputs ("archive:\n", stdout);
169 fputs ("unknown file:\n", stdout);
173 /* Print general information. */
175 strftime (buf, sizeof buf, "%Y-%m-%dT%H:%M:%S%z", gmtime (&t));
176 printf (" name : \"%s\"\n"
182 " rawname : \"%s\"\n",
185 (long int) arhdr->ar_uid,
186 (long int) arhdr->ar_gid,
188 (long int) arhdr->ar_size,
191 /* For ELF files we can provide some more information. */
192 if (elf_kind (subelf) == ELF_K_ELF)
196 /* Get the ELF header. */
197 if (gelf_getehdr (subelf, &ehdr) == NULL)
198 printf (" *** cannot get ELF header: %s\n", elf_errmsg (-1));
201 printf (" binary class : %s\n",
202 ehdr.e_ident[EI_CLASS] == ELFCLASS32
203 ? "ELFCLASS32" : "ELFCLASS64");
204 printf (" data encoding: %s\n",
205 ehdr.e_ident[EI_DATA] == ELFDATA2LSB
206 ? "ELFDATA2LSB" : "ELFDATA2MSB");
207 printf (" binary type : %s\n",
208 ehdr.e_type == ET_REL
210 : (ehdr.e_type == ET_EXEC
212 : (ehdr.e_type == ET_DYN
215 printf (" machine : %s\n",
216 (ehdr.e_machine >= (sizeof (machines)
217 / sizeof (machines[0]))
218 || machines[ehdr.e_machine] == NULL)
220 : machines[ehdr.e_machine]);
224 /* Get next archive element. */
225 cmd = elf_next (subelf);
226 if (elf_end (subelf) != 0)
227 printf ("error while freeing sub-ELF descriptor: %s\n",
233 /* The simple version. Only print a bit of information. */
234 Elf_Arsym *arsym = elf_getarsym (elf, &n);
237 printf ("no symbol table in archive: %s\n", elf_errmsg (-1));
243 printf ("name = \"%s\", offset = %ld, hash = %lx\n",
244 arsym[n].as_name, (long int) arsym[n].as_off,
249 /* Free the ELF handle. */
250 if (elf_end (elf) != 0)
251 printf ("error while freeing ELF descriptor: %s\n", elf_errmsg (-1));
253 /* Close the underlying file. */