1 /* Copyright (C) 1998, 1999, 2000, 2002 Red Hat, Inc.
2 This file is part of Red Hat elfutils.
3 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5 Red Hat elfutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by the
7 Free Software Foundation; version 2 of the License.
9 Red Hat elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with Red Hat elfutils; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18 Red Hat elfutils is an included package of the Open Invention Network.
19 An included package of the Open Invention Network is a package for which
20 Open Invention Network licensees cross-license their patents. No patent
21 license is granted, either expressly or impliedly, by designation as an
22 included package. Should you wish to participate in the Open Invention
23 Network licensing program, please visit www.openinventionnetwork.com
24 <http://www.openinventionnetwork.com>. */
38 static const char *machines[] =
40 #define MACHINE(name) [name] = #name
49 MACHINE (EM_MIPS_RS3_LE),
52 MACHINE (EM_SPARC32PLUS),
61 MACHINE (EM_FAKE_ALPHA),
72 MACHINE (EM_COLDFIRE),
78 MACHINE (EM_STARCORE),
96 main (int argc, char *argv[])
105 /* Recognize optional verbosity flag. */
106 if (arg < argc && strcmp (argv[arg], "-v") == 0)
112 /* Any more arguments available. */
114 error (EXIT_FAILURE, 0, "No input file given");
116 /* Open the input file. */
117 fd = open (argv[arg], O_RDONLY);
120 perror ("cannot open input file");
124 /* Set the ELF version we are using here. */
125 if (elf_version (EV_CURRENT) == EV_NONE)
127 puts ("ELF library too old");
131 /* Start reading the file. */
133 elf = elf_begin (fd, cmd, NULL);
136 printf ("elf_begin: %s\n", elf_errmsg (-1));
140 /* If it is no archive punt. */
141 if (elf_kind (elf) != ELF_K_AR)
143 printf ("%s is not an archive\n", argv[1]);
149 /* The verbose variant. We print a lot of information. */
154 /* Get the elements of the archive one after the other. */
155 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
157 /* The the header for this element. */
158 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
162 printf ("cannot get arhdr: %s\n", elf_errmsg (-1));
166 switch (elf_kind (subelf))
169 fputs ("ELF file:\n", stdout);
173 fputs ("archive:\n", stdout);
177 fputs ("unknown file:\n", stdout);
181 /* Print general information. */
183 strftime (buf, sizeof buf, "%Y-%m-%dT%H:%M:%S%z", gmtime (&t));
184 printf (" name : \"%s\"\n"
190 " rawname : \"%s\"\n",
193 (long int) arhdr->ar_uid,
194 (long int) arhdr->ar_gid,
196 (long int) arhdr->ar_size,
199 /* For ELF files we can provide some more information. */
200 if (elf_kind (subelf) == ELF_K_ELF)
204 /* Get the ELF header. */
205 if (gelf_getehdr (subelf, &ehdr) == NULL)
206 printf (" *** cannot get ELF header: %s\n", elf_errmsg (-1));
209 printf (" binary class : %s\n",
210 ehdr.e_ident[EI_CLASS] == ELFCLASS32
211 ? "ELFCLASS32" : "ELFCLASS64");
212 printf (" data encoding: %s\n",
213 ehdr.e_ident[EI_DATA] == ELFDATA2LSB
214 ? "ELFDATA2LSB" : "ELFDATA2MSB");
215 printf (" binary type : %s\n",
216 ehdr.e_type == ET_REL
218 : (ehdr.e_type == ET_EXEC
220 : (ehdr.e_type == ET_DYN
223 printf (" machine : %s\n",
224 (ehdr.e_machine >= (sizeof (machines)
225 / sizeof (machines[0]))
226 || machines[ehdr.e_machine] == NULL)
228 : machines[ehdr.e_machine]);
232 /* Get next archive element. */
233 cmd = elf_next (subelf);
234 if (elf_end (subelf) != 0)
235 printf ("error while freeing sub-ELF descriptor: %s\n",
241 /* The simple version. Only print a bit of information. */
242 Elf_Arsym *arsym = elf_getarsym (elf, &n);
245 printf ("no symbol table in archive: %s\n", elf_errmsg (-1));
251 printf ("name = \"%s\", offset = %ld, hash = %lx\n",
252 arsym[n].as_name, (long int) arsym[n].as_off,
257 /* Free the ELF handle. */
258 if (elf_end (elf) != 0)
259 printf ("error while freeing ELF descriptor: %s\n", elf_errmsg (-1));
261 /* Close the underlying file. */