* sysdeps/m68k/dl-machine.h (_dl_start_user): Pass correct
[glibc.git] / sysdeps / generic / readelflib.c
blob270c9b00a511bcfe1a9e18bcb4a2020a1a535e28
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 /* check_ptr checks that a pointer is in the mmaped file and doesn't
27 point outside it. */
28 #define check_ptr(ptr) \
29 do \
30 { \
31 if ((void *)(ptr) < file_contents \
32 || (void *)(ptr) > (file_contents+file_length)) \
33 { \
34 error (0, 0, _("file %s is truncated\n"), file_name); \
35 return 1; \
36 } \
37 } \
38 while (0);
40 /* Returns 0 if everything is ok, != 0 in case of error. */
41 int
42 process_elf_file (const char *file_name, const char *lib, int *flag,
43 char **soname, void *file_contents, size_t file_length)
45 int i;
46 unsigned int j;
47 int loadaddr;
48 unsigned int dynamic_addr;
49 size_t dynamic_size;
50 char *program_interpreter;
52 ElfW(Ehdr) *elf_header;
53 ElfW(Phdr) *elf_pheader, *segment;
54 ElfW(Dyn) *dynamic_segment, *dyn_entry;
55 char *dynamic_strings;
57 elf_header = (ElfW(Ehdr) *) file_contents;
59 if (elf_header->e_ident [EI_CLASS] != ElfW (CLASS))
61 if (opt_verbose)
63 if (ElfW (CLASS) == ELFCLASS32)
64 error (0, 0, _("%s is a 32 bit ELF file.\n"), file_name);
65 else if (ElfW (CLASS) == ELFCLASS64)
66 error (0, 0, _("%s is a 64 bit ELF file.\n"), file_name);
67 else
68 error (0, 0, _("Unknown ELFCLASS in file %s.\n"), file_name);
70 return 1;
73 if (elf_header->e_type != ET_DYN)
75 error (0, 0, _("%s is not a shared object file (Type: %d).\n"), file_name,
76 elf_header->e_type);
77 return 1;
80 /* Get information from elf program header. */
81 elf_pheader = (ElfW(Phdr) *) (elf_header->e_phoff + file_contents);
82 check_ptr (elf_pheader);
84 /* The library is an elf library, now search for soname and
85 libc5/libc6. */
86 *flag = FLAG_ELF;
88 loadaddr = -1;
89 dynamic_addr = 0;
90 dynamic_size = 0;
91 program_interpreter = NULL;
92 for (i = 0, segment = elf_pheader;
93 i < elf_header->e_phnum; i++, segment++)
95 check_ptr (segment);
97 switch (segment->p_type)
99 case PT_LOAD:
100 if (loadaddr == -1)
101 loadaddr = (segment->p_vaddr & 0xfffff000)
102 - (segment->p_offset & 0xfffff000);
103 break;
105 case PT_DYNAMIC:
106 if (dynamic_addr)
107 error (0, 0, _("more than one dynamic segment\n"));
109 dynamic_addr = segment->p_offset;
110 dynamic_size = segment->p_filesz;
111 break;
112 case PT_INTERP:
113 program_interpreter = (char *) (file_contents + segment->p_offset);
114 check_ptr (program_interpreter);
116 /* Check if this is enough to classify the binary. */
117 for (j = 0; j < sizeof (interpreters) / sizeof (interpreters [0]);
118 ++j)
119 if (strcmp (program_interpreter, interpreters[j].soname) == 0)
121 *flag = interpreters[j].flag;
122 break;
124 break;
125 default:
126 break;
130 if (loadaddr == -1)
132 /* Very strange. */
133 loadaddr = 0;
136 /* Now we can read the dynamic sections. */
137 if (dynamic_size == 0)
138 return 1;
140 dynamic_segment = (ElfW(Dyn) *) (file_contents + dynamic_addr);
141 check_ptr (dynamic_segment);
143 /* Find the string table. */
144 dynamic_strings = NULL;
145 for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL;
146 ++dyn_entry)
148 check_ptr (dyn_entry);
149 if (dyn_entry->d_tag == DT_STRTAB)
151 dynamic_strings = (char *) (file_contents + dyn_entry->d_un.d_val - loadaddr);
152 check_ptr (dynamic_strings);
153 break;
157 if (dynamic_strings == NULL)
158 return 1;
160 /* Now read the DT_NEEDED and DT_SONAME entries. */
161 for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL;
162 ++dyn_entry)
164 if (dyn_entry->d_tag == DT_NEEDED || dyn_entry->d_tag == DT_SONAME)
166 char *name = dynamic_strings + dyn_entry->d_un.d_val;
167 check_ptr (name);
169 if (dyn_entry->d_tag == DT_NEEDED)
172 if (*flag == FLAG_ELF)
174 /* Check if this is enough to classify the binary. */
175 for (j = 0;
176 j < sizeof (known_libs) / sizeof (known_libs [0]);
177 ++j)
178 if (strcmp (name, known_libs [j].soname) == 0)
180 *flag = known_libs [j].flag;
181 break;
186 else if (dyn_entry->d_tag == DT_SONAME)
187 *soname = xstrdup (name);
189 /* Do we have everything we need? */
190 if (*soname && *flag != FLAG_ELF)
191 return 0;
195 /* We reach this point only if the file doesn't contain a DT_SONAME
196 or if we can't classify the library. If it doesn't have a
197 soname, return the name of the library. */
198 if (*soname == NULL)
199 *soname = xstrdup (lib);
201 return 0;