Updated to fedora-glibc-20080524T2218
[glibc.git] / elf / readlib.c
blobeb64a79f101600ccea977783d1332f1ddf7e76b2
1 /* Copyright (C) 1999-2003, 2005, 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 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* The code in this file and in readelflib is a heavily simplified
21 version of the readelf program that's part of the current binutils
22 development version. Besides the simplification, it has also been
23 modified to read some other file formats. */
25 #include <a.out.h>
26 #include <elf.h>
27 #include <error.h>
28 #include <libintl.h>
29 #include <link.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/mman.h>
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <gnu/lib-names.h>
38 #include <ldconfig.h>
40 #define Elf32_CLASS ELFCLASS32
41 #define Elf64_CLASS ELFCLASS64
43 struct known_names
45 const char *soname;
46 int flag;
49 static struct known_names interpreters[] =
51 { "/lib/" LD_SO, FLAG_ELF_LIBC6 },
52 #ifdef SYSDEP_KNOWN_INTERPRETER_NAMES
53 SYSDEP_KNOWN_INTERPRETER_NAMES
54 #endif
57 static struct known_names known_libs[] =
59 { LIBC_SO, FLAG_ELF_LIBC6 },
60 { LIBM_SO, FLAG_ELF_LIBC6 },
61 #ifdef SYSDEP_KNOWN_LIBRARY_NAMES
62 SYSDEP_KNOWN_LIBRARY_NAMES
63 #endif
68 /* Returns 0 if everything is ok, != 0 in case of error. */
69 int
70 process_file (const char *real_file_name, const char *file_name,
71 const char *lib, int *flag, unsigned int *osversion,
72 char **soname, int is_link, struct stat64 *stat_buf)
74 FILE *file;
75 struct stat64 statbuf;
76 void *file_contents;
77 int ret;
78 ElfW(Ehdr) *elf_header;
79 struct exec *aout_header;
81 ret = 0;
82 *flag = FLAG_ANY;
83 *soname = NULL;
85 file = fopen (real_file_name, "rb");
86 if (file == NULL)
88 /* No error for stale symlink. */
89 if (is_link && strstr (file_name, ".so") != NULL)
90 return 1;
91 error (0, 0, _("Input file %s not found.\n"), file_name);
92 return 1;
95 if (fstat64 (fileno (file), &statbuf) < 0)
97 error (0, 0, _("Cannot fstat file %s.\n"), file_name);
98 fclose (file);
99 return 1;
102 /* Check that the file is large enough so that we can access the
103 information. We're only checking the size of the headers here. */
104 if ((size_t) statbuf.st_size < sizeof (struct exec)
105 || (size_t) statbuf.st_size < sizeof (ElfW(Ehdr)))
107 if (statbuf.st_size == 0)
108 error (0, 0, _("File %s is empty, not checked."), file_name);
109 else
111 char buf[SELFMAG];
112 size_t n = MIN (statbuf.st_size, SELFMAG);
113 if (fread (buf, n, 1, file) == 1 && memcmp (buf, ELFMAG, n) == 0)
114 error (0, 0, _("File %s is too small, not checked."), file_name);
116 fclose (file);
117 return 1;
120 file_contents = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED,
121 fileno (file), 0);
122 if (file_contents == MAP_FAILED)
124 error (0, 0, _("Cannot mmap file %s.\n"), file_name);
125 fclose (file);
126 return 1;
129 /* First check if this is an aout file. */
130 aout_header = (struct exec *) file_contents;
131 if (N_MAGIC (*aout_header) == ZMAGIC
132 #ifdef QMAGIC /* Linuxism. */
133 || N_MAGIC (*aout_header) == QMAGIC
134 #endif
137 /* Aout files don't have a soname, just return the name
138 including the major number. */
139 char *copy, *major, *dot;
140 copy = xstrdup (lib);
141 major = strstr (copy, ".so.");
142 if (major)
144 dot = strstr (major + 4, ".");
145 if (dot)
146 *dot = '\0';
148 *soname = copy;
149 *flag = FLAG_LIBC4;
150 goto done;
153 elf_header = (ElfW(Ehdr) *) file_contents;
154 if (memcmp (elf_header->e_ident, ELFMAG, SELFMAG) != 0)
156 /* The file is neither ELF nor aout. Check if it's a linker
157 script, like libc.so - otherwise complain. Only search the
158 beginning of the file. */
159 size_t len = MIN (statbuf.st_size, 512);
160 if (memmem (file_contents, len, "GROUP", 5) == NULL
161 && memmem (file_contents, len, "GNU ld script", 13) == NULL)
162 error (0, 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"),
163 file_name);
164 ret = 1;
166 /* Libraries have to be shared object files. */
167 else if (elf_header->e_type != ET_DYN)
168 ret = 1;
169 else if (process_elf_file (file_name, lib, flag, osversion, soname,
170 file_contents, statbuf.st_size))
171 ret = 1;
173 done:
174 /* Clean up allocated memory and resources. */
175 munmap (file_contents, statbuf.st_size);
176 fclose (file);
178 *stat_buf = statbuf;
179 return ret;
182 /* Returns made up soname if lib doesn't have explicit DT_SONAME. */
184 char *
185 implicit_soname (const char *lib, int flag)
187 char *soname = xstrdup (lib);
189 if ((flag & FLAG_TYPE_MASK) != FLAG_LIBC4)
190 return soname;
192 /* Aout files don't have a soname, just return the name
193 including the major number. */
194 char *major = strstr (soname, ".so.");
195 if (major)
197 char *dot = strstr (major + 4, ".");
198 if (dot)
199 *dot = '\0';
201 return soname;
204 /* Get architecture specific version of process_elf_file. */
205 #include <readelflib.c>