* locale/programs/ld-collate.c (collate_read): If ignore_content
[glibc.git] / elf / readelflib.c
blobb8f677a7b00dd3a93b7b4a6eb998630030ca06c6
1 /* Copyright (C) 1999, 2000, 2001, 2002, 2007 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 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 /* check_ptr checks that a pointer is in the mmaped file and doesn't
27 point outside it. */
28 #undef check_ptr
29 #define check_ptr(ptr) \
30 do \
31 { \
32 if ((void *)(ptr) < file_contents \
33 || (void *)(ptr) > (file_contents+file_length)) \
34 { \
35 error (0, 0, _("file %s is truncated\n"), file_name); \
36 return 1; \
37 } \
38 } \
39 while (0);
41 /* Returns 0 if everything is ok, != 0 in case of error. */
42 int
43 process_elf_file (const char *file_name, const char *lib, int *flag,
44 unsigned int *osversion, char **soname, void *file_contents,
45 size_t file_length)
47 int i;
48 unsigned int j;
49 ElfW(Addr) loadaddr;
50 unsigned int dynamic_addr;
51 size_t dynamic_size;
52 char *program_interpreter;
54 ElfW(Ehdr) *elf_header;
55 ElfW(Phdr) *elf_pheader, *segment;
56 ElfW(Dyn) *dynamic_segment, *dyn_entry;
57 char *dynamic_strings;
59 elf_header = (ElfW(Ehdr) *) file_contents;
60 *osversion = 0;
62 if (elf_header->e_ident [EI_CLASS] != ElfW (CLASS))
64 if (opt_verbose)
66 if (elf_header->e_ident [EI_CLASS] == ELFCLASS32)
67 error (0, 0, _("%s is a 32 bit ELF file.\n"), file_name);
68 else if (elf_header->e_ident [EI_CLASS] == ELFCLASS64)
69 error (0, 0, _("%s is a 64 bit ELF file.\n"), file_name);
70 else
71 error (0, 0, _("Unknown ELFCLASS in file %s.\n"), file_name);
73 return 1;
76 if (elf_header->e_type != ET_DYN)
78 error (0, 0, _("%s is not a shared object file (Type: %d).\n"), file_name,
79 elf_header->e_type);
80 return 1;
83 /* Get information from elf program header. */
84 elf_pheader = (ElfW(Phdr) *) (elf_header->e_phoff + file_contents);
85 check_ptr (elf_pheader);
87 /* The library is an elf library, now search for soname and
88 libc5/libc6. */
89 *flag = FLAG_ELF;
91 loadaddr = -1;
92 dynamic_addr = 0;
93 dynamic_size = 0;
94 program_interpreter = NULL;
95 for (i = 0, segment = elf_pheader;
96 i < elf_header->e_phnum; i++, segment++)
98 check_ptr (segment);
100 switch (segment->p_type)
102 case PT_LOAD:
103 if (loadaddr == (ElfW(Addr)) -1)
104 loadaddr = segment->p_vaddr - segment->p_offset;
105 break;
107 case PT_DYNAMIC:
108 if (dynamic_addr)
109 error (0, 0, _("more than one dynamic segment\n"));
111 dynamic_addr = segment->p_offset;
112 dynamic_size = segment->p_filesz;
113 break;
115 case PT_INTERP:
116 program_interpreter = (char *) (file_contents + segment->p_offset);
117 check_ptr (program_interpreter);
119 /* Check if this is enough to classify the binary. */
120 for (j = 0; j < sizeof (interpreters) / sizeof (interpreters [0]);
121 ++j)
122 if (strcmp (program_interpreter, interpreters[j].soname) == 0)
124 *flag = interpreters[j].flag;
125 break;
127 break;
129 case PT_NOTE:
130 if (!*osversion && segment->p_filesz >= 32 && segment->p_align >= 4)
132 ElfW(Word) *abi_note = (ElfW(Word) *) (file_contents
133 + segment->p_offset);
134 ElfW(Addr) size = segment->p_filesz;
136 while (abi_note [0] != 4 || abi_note [1] != 16
137 || abi_note [2] != 1
138 || memcmp (abi_note + 3, "GNU", 4) != 0)
140 #define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
141 ElfW(Addr) note_size = 3 * sizeof (ElfW(Word))
142 + ROUND (abi_note[0])
143 + ROUND (abi_note[1]);
145 if (size - 32 < note_size || note_size == 0)
147 size = 0;
148 break;
150 size -= note_size;
151 abi_note = (void *) abi_note + note_size;
154 if (size == 0)
155 break;
157 *osversion = (abi_note [4] << 24) |
158 ((abi_note [5] & 0xff) << 16) |
159 ((abi_note [6] & 0xff) << 8) |
160 (abi_note [7] & 0xff);
162 break;
164 default:
165 break;
169 if (loadaddr == (ElfW(Addr)) -1)
171 /* Very strange. */
172 loadaddr = 0;
175 /* Now we can read the dynamic sections. */
176 if (dynamic_size == 0)
177 return 1;
179 dynamic_segment = (ElfW(Dyn) *) (file_contents + dynamic_addr);
180 check_ptr (dynamic_segment);
182 /* Find the string table. */
183 dynamic_strings = NULL;
184 for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL;
185 ++dyn_entry)
187 check_ptr (dyn_entry);
188 if (dyn_entry->d_tag == DT_STRTAB)
190 dynamic_strings = (char *) (file_contents + dyn_entry->d_un.d_val - loadaddr);
191 check_ptr (dynamic_strings);
192 break;
196 if (dynamic_strings == NULL)
197 return 1;
199 /* Now read the DT_NEEDED and DT_SONAME entries. */
200 for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL;
201 ++dyn_entry)
203 if (dyn_entry->d_tag == DT_NEEDED || dyn_entry->d_tag == DT_SONAME)
205 char *name = dynamic_strings + dyn_entry->d_un.d_val;
206 check_ptr (name);
208 if (dyn_entry->d_tag == DT_NEEDED)
211 if (*flag == FLAG_ELF)
213 /* Check if this is enough to classify the binary. */
214 for (j = 0;
215 j < sizeof (known_libs) / sizeof (known_libs [0]);
216 ++j)
217 if (strcmp (name, known_libs [j].soname) == 0)
219 *flag = known_libs [j].flag;
220 break;
225 else if (dyn_entry->d_tag == DT_SONAME)
226 *soname = xstrdup (name);
228 /* Do we have everything we need? */
229 if (*soname && *flag != FLAG_ELF)
230 return 0;
234 return 0;