Move BZ#11261 from 2.18 to 2.16 bug list.
[glibc.git] / elf / readlib.c
blob82c182e488a1529bde0593ca9ad6fd639bdecfc7
1 /* Copyright (C) 1999-2013 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, see <http://www.gnu.org/licenses/>. */
19 /* The code in this file and in readelflib is a heavily simplified
20 version of the readelf program that's part of the current binutils
21 development version. Besides the simplification, it has also been
22 modified to read some other file formats. */
24 #include <a.out.h>
25 #include <elf.h>
26 #include <error.h>
27 #include <libintl.h>
28 #include <link.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/mman.h>
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <gnu/lib-names.h>
37 #include <ldconfig.h>
39 #define Elf32_CLASS ELFCLASS32
40 #define Elf64_CLASS ELFCLASS64
42 struct known_names
44 const char *soname;
45 int flag;
48 static struct known_names interpreters[] =
50 { "/lib/" LD_SO, FLAG_ELF_LIBC6 },
51 #ifdef SYSDEP_KNOWN_INTERPRETER_NAMES
52 SYSDEP_KNOWN_INTERPRETER_NAMES
53 #endif
56 static struct known_names known_libs[] =
58 { LIBC_SO, FLAG_ELF_LIBC6 },
59 { LIBM_SO, FLAG_ELF_LIBC6 },
60 #ifdef SYSDEP_KNOWN_LIBRARY_NAMES
61 SYSDEP_KNOWN_LIBRARY_NAMES
62 #endif
67 /* Returns 0 if everything is ok, != 0 in case of error. */
68 int
69 process_file (const char *real_file_name, const char *file_name,
70 const char *lib, int *flag, unsigned int *osversion,
71 char **soname, int is_link, struct stat64 *stat_buf)
73 FILE *file;
74 struct stat64 statbuf;
75 void *file_contents;
76 int ret;
77 ElfW(Ehdr) *elf_header;
78 struct exec *aout_header;
80 ret = 0;
81 *flag = FLAG_ANY;
82 *soname = NULL;
84 file = fopen (real_file_name, "rb");
85 if (file == NULL)
87 /* No error for stale symlink. */
88 if (is_link && strstr (file_name, ".so") != NULL)
89 return 1;
90 error (0, 0, _("Input file %s not found.\n"), file_name);
91 return 1;
94 if (fstat64 (fileno (file), &statbuf) < 0)
96 error (0, 0, _("Cannot fstat file %s.\n"), file_name);
97 fclose (file);
98 return 1;
101 /* Check that the file is large enough so that we can access the
102 information. We're only checking the size of the headers here. */
103 if ((size_t) statbuf.st_size < sizeof (struct exec)
104 || (size_t) statbuf.st_size < sizeof (ElfW(Ehdr)))
106 if (statbuf.st_size == 0)
107 error (0, 0, _("File %s is empty, not checked."), file_name);
108 else
110 char buf[SELFMAG];
111 size_t n = MIN (statbuf.st_size, SELFMAG);
112 if (fread (buf, n, 1, file) == 1 && memcmp (buf, ELFMAG, n) == 0)
113 error (0, 0, _("File %s is too small, not checked."), file_name);
115 fclose (file);
116 return 1;
119 file_contents = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED,
120 fileno (file), 0);
121 if (file_contents == MAP_FAILED)
123 error (0, 0, _("Cannot mmap file %s.\n"), file_name);
124 fclose (file);
125 return 1;
128 /* First check if this is an aout file. */
129 aout_header = (struct exec *) file_contents;
130 if (N_MAGIC (*aout_header) == ZMAGIC
131 #ifdef QMAGIC /* Linuxism. */
132 || N_MAGIC (*aout_header) == QMAGIC
133 #endif
136 /* Aout files don't have a soname, just return the name
137 including the major number. */
138 char *copy, *major, *dot;
139 copy = xstrdup (lib);
140 major = strstr (copy, ".so.");
141 if (major)
143 dot = strstr (major + 4, ".");
144 if (dot)
145 *dot = '\0';
147 *soname = copy;
148 *flag = FLAG_LIBC4;
149 goto done;
152 elf_header = (ElfW(Ehdr) *) file_contents;
153 if (memcmp (elf_header->e_ident, ELFMAG, SELFMAG) != 0)
155 /* The file is neither ELF nor aout. Check if it's a linker
156 script, like libc.so - otherwise complain. Only search the
157 beginning of the file. */
158 size_t len = MIN (statbuf.st_size, 512);
159 if (memmem (file_contents, len, "GROUP", 5) == NULL
160 && memmem (file_contents, len, "GNU ld script", 13) == NULL)
161 error (0, 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"),
162 file_name);
163 ret = 1;
165 /* Libraries have to be shared object files. */
166 else if (elf_header->e_type != ET_DYN)
167 ret = 1;
168 else if (process_elf_file (file_name, lib, flag, osversion, soname,
169 file_contents, statbuf.st_size))
170 ret = 1;
172 done:
173 /* Clean up allocated memory and resources. */
174 munmap (file_contents, statbuf.st_size);
175 fclose (file);
177 *stat_buf = statbuf;
178 return ret;
181 /* Returns made up soname if lib doesn't have explicit DT_SONAME. */
183 char *
184 implicit_soname (const char *lib, int flag)
186 char *soname = xstrdup (lib);
188 if ((flag & FLAG_TYPE_MASK) != FLAG_LIBC4)
189 return soname;
191 /* Aout files don't have a soname, just return the name
192 including the major number. */
193 char *major = strstr (soname, ".so.");
194 if (major)
196 char *dot = strstr (major + 4, ".");
197 if (dot)
198 *dot = '\0';
200 return soname;
203 /* Get architecture specific version of process_elf_file. */
204 #include <readelflib.c>