Update.
[glibc.git] / sysdeps / generic / readelflib.c
blob645c9124a9a6c26ca54fd7694fb0d9870f2b4499
1 /* Copyright (C) 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>, 1999 and
4 Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This code is a heavily simplified version of the readelf program
22 that's part of the current binutils development version. For architectures
23 which need to handle both 32bit and 64bit ELF libraries, this file is
24 included twice for each arch size. */
26 /* Returns 0 if everything is ok, != 0 in case of error. */
27 int
28 process_elf_file (const char *file_name, const char *lib, int *flag, char **soname,
29 void *file_contents)
31 int i;
32 unsigned int j;
33 int loadaddr;
34 unsigned int dynamic_addr;
35 size_t dynamic_size;
36 char *program_interpreter;
38 ElfW(Ehdr) *elf_header;
39 ElfW(Phdr) *elf_pheader, *segment;
40 ElfW(Dyn) *dynamic_segment, *dyn_entry;
41 char *dynamic_strings;
43 elf_header = (ElfW(Ehdr) *) file_contents;
45 if (elf_header->e_ident [EI_CLASS] != ElfW (CLASS))
47 if (opt_verbose)
49 if (ElfW (CLASS) == ELFCLASS32)
50 error (0, 0, _("%s is a 32 bit ELF file.\n"), file_name);
51 else if (ElfW (CLASS) == ELFCLASS64)
52 error (0, 0, _("%s is a 64 bit ELF file.\n"), file_name);
53 else
54 error (0, 0, _("Unknown ELFCLASS in file %s.\n"), file_name);
56 return 1;
59 if (elf_header->e_type != ET_DYN)
61 error (0, 0, _("%s is not a shared object file (Type: %d).\n"), file_name,
62 elf_header->e_type);
63 return 1;
66 /* Get information from elf program header. */
67 elf_pheader = (ElfW(Phdr) *) (elf_header->e_phoff + file_contents);
69 /* The library is an elf library, now search for soname and
70 libc5/libc6. */
71 *flag = FLAG_ELF;
73 loadaddr = -1;
74 dynamic_addr = 0;
75 dynamic_size = 0;
76 program_interpreter = NULL;
77 for (i = 0, segment = elf_pheader;
78 i < elf_header->e_phnum; i++, segment++)
80 switch (segment->p_type)
82 case PT_LOAD:
83 if (loadaddr == -1)
84 loadaddr = (segment->p_vaddr & 0xfffff000)
85 - (segment->p_offset & 0xfffff000);
86 break;
88 case PT_DYNAMIC:
89 if (dynamic_addr)
90 error (0, 0, _("more than one dynamic segment\n"));
92 dynamic_addr = segment->p_offset;
93 dynamic_size = segment->p_filesz;
94 break;
95 case PT_INTERP:
96 program_interpreter = (char *) (file_contents + segment->p_offset);
98 /* Check if this is enough to classify the binary. */
99 for (j = 0; j < sizeof (interpreters) / sizeof (interpreters [0]);
100 ++j)
101 if (strcmp (program_interpreter, interpreters[j].soname) == 0)
103 *flag = interpreters[j].flag;
104 break;
106 break;
107 default:
108 break;
112 if (loadaddr == -1)
114 /* Very strange. */
115 loadaddr = 0;
118 /* Now we can read the dynamic sections. */
119 if (dynamic_size == 0)
120 return 1;
122 dynamic_segment = (ElfW(Dyn) *) (file_contents + dynamic_addr);
124 /* Find the string table. */
125 dynamic_strings = NULL;
126 for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL;
127 ++dyn_entry)
129 if (dyn_entry->d_tag == DT_STRTAB)
131 dynamic_strings = (char *) (file_contents + dyn_entry->d_un.d_val - loadaddr);
132 break;
136 if (dynamic_strings == NULL)
137 return 1;
139 /* Now read the DT_NEEDED and DT_SONAME entries. */
140 for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL;
141 ++dyn_entry)
143 if (dyn_entry->d_tag == DT_NEEDED || dyn_entry->d_tag == DT_SONAME)
145 char *name = dynamic_strings + dyn_entry->d_un.d_val;
147 if (dyn_entry->d_tag == DT_NEEDED)
150 if (*flag == FLAG_ELF)
152 /* Check if this is enough to classify the binary. */
153 for (j = 0;
154 j < sizeof (known_libs) / sizeof (known_libs [0]);
155 ++j)
156 if (strcmp (name, known_libs [j].soname) == 0)
158 *flag = known_libs [j].flag;
159 break;
164 else if (dyn_entry->d_tag == DT_SONAME)
165 *soname = xstrdup (name);
167 /* Do we have everything we need? */
168 if (*soname && *flag != FLAG_ELF)
169 return 0;
173 /* We reach this point only if the file doesn't contain a DT_SONAME
174 or if we can't classify the library. If it doesn't have a
175 soname, return the name of the library. */
176 if (*soname == NULL)
177 *soname = xstrdup (lib);
179 return 0;