(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / elf / readlib.c
blob4fbc3e5e4c8adcba00ccd1656a464cae5d922817
1 /* Copyright (C) 1999, 2000, 2001, 2002, 2003 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 /* The code in this file and in readelflib is a heavily simplified
22 version of the readelf program that's part of the current binutils
23 development version. Besides the simplification, it has also been
24 modified to read some other file formats. */
26 #include <a.out.h>
27 #include <elf.h>
28 #include <error.h>
29 #include <libintl.h>
30 #include <link.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/mman.h>
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 #include <gnu/lib-names.h>
39 #include "ldconfig.h"
41 #define Elf32_CLASS ELFCLASS32
42 #define Elf64_CLASS ELFCLASS64
44 struct known_names
46 const char *soname;
47 int flag;
50 static struct known_names interpreters[] =
52 { "/lib/" LD_SO, FLAG_ELF_LIBC6 },
53 #ifdef SYSDEP_KNOWN_INTERPRETER_NAMES
54 SYSDEP_KNOWN_INTERPRETER_NAMES
55 #endif
58 static struct known_names known_libs[] =
60 { LIBC_SO, FLAG_ELF_LIBC6 },
61 { LIBM_SO, FLAG_ELF_LIBC6 },
62 #ifdef SYSDEP_KNOWN_LIBRARY_NAMES
63 SYSDEP_KNOWN_LIBRARY_NAMES
64 #endif
69 /* Returns 0 if everything is ok, != 0 in case of error. */
70 int
71 process_file (const char *real_file_name, const char *file_name,
72 const char *lib, int *flag, unsigned int *osversion,
73 char **soname, int is_link)
75 FILE *file;
76 struct stat64 statbuf;
77 void *file_contents;
78 int ret;
79 ElfW(Ehdr) *elf_header;
80 struct exec *aout_header;
82 ret = 0;
83 *flag = FLAG_ANY;
84 *soname = NULL;
86 file = fopen (real_file_name, "rb");
87 if (file == NULL)
89 /* No error for stale symlink. */
90 if (is_link && strstr (file_name, ".so") != NULL)
91 return 1;
92 error (0, 0, _("Input file %s not found.\n"), file_name);
93 return 1;
96 if (fstat64 (fileno (file), &statbuf) < 0)
98 error (0, 0, _("Cannot fstat file %s.\n"), file_name);
99 fclose (file);
100 return 1;
103 /* Check that the file is large enough so that we can access the
104 information. We're only checking the size of the headers here. */
105 if ((size_t) statbuf.st_size < sizeof (struct exec)
106 || (size_t) statbuf.st_size < sizeof (ElfW(Ehdr)))
108 if (statbuf.st_size == 0)
109 error (0, 0, _("File %s is empty, not checked."), file_name);
110 else
112 char buf[SELFMAG];
113 size_t n = MIN (statbuf.st_size, SELFMAG);
114 if (fread (buf, n, 1, file) == 1 && memcmp (buf, ELFMAG, n) == 0)
115 error (0, 0, _("File %s is too small, not checked."), file_name);
117 fclose (file);
118 return 1;
121 file_contents = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED,
122 fileno (file), 0);
123 if (file_contents == MAP_FAILED)
125 error (0, 0, _("Cannot mmap file %s.\n"), file_name);
126 fclose (file);
127 return 1;
130 /* First check if this is an aout file. */
131 aout_header = (struct exec *) file_contents;
132 if (N_MAGIC (*aout_header) == ZMAGIC
133 #ifdef QMAGIC /* Linuxism. */
134 || N_MAGIC (*aout_header) == QMAGIC
135 #endif
138 /* Aout files don't have a soname, just return the name
139 including the major number. */
140 char *copy, *major, *dot;
141 copy = xstrdup (lib);
142 major = strstr (copy, ".so.");
143 if (major)
145 dot = strstr (major + 4, ".");
146 if (dot)
147 *dot = '\0';
149 *soname = copy;
150 *flag = FLAG_LIBC4;
151 goto done;
154 elf_header = (ElfW(Ehdr) *) file_contents;
155 if (memcmp (elf_header->e_ident, ELFMAG, SELFMAG) != 0)
157 /* The file is neither ELF nor aout. Check if it's a linker
158 script, like libc.so - otherwise complain. Only search the
159 beginning of the file. */
160 size_t len = MIN (statbuf.st_size, 512);
161 if (memmem (file_contents, len, "GROUP", 5) == NULL
162 && memmem (file_contents, len, "GNU ld script", 13) == NULL)
163 error (0, 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"),
164 file_name);
165 ret = 1;
167 /* Libraries have to be shared object files. */
168 else if (elf_header->e_type != ET_DYN)
169 ret = 1;
170 else if (process_elf_file (file_name, lib, flag, osversion, soname,
171 file_contents, statbuf.st_size))
172 ret = 1;
174 done:
175 /* Clean up allocated memory and resources. */
176 munmap (file_contents, statbuf.st_size);
177 fclose (file);
179 return ret;
182 /* Get architecture specific version of process_elf_file. */
183 #include "readelflib.c"