]>
Commit | Line | Data |
---|---|---|
6dd67bd5 | 1 | /* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
591e1ffb UD |
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. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
591e1ffb UD |
10 | |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
591e1ffb | 15 | |
41bdb6e2 AJ |
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. */ | |
591e1ffb UD |
20 | |
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. */ | |
25 | ||
26 | ||
591e1ffb UD |
27 | #include <elf.h> |
28 | #include <error.h> | |
29 | #include <link.h> | |
30 | #include <libintl.h> | |
31 | #include <stdio.h> | |
32 | #include <string.h> | |
33 | #include <unistd.h> | |
af1680f1 | 34 | #include <a.out.h> |
591e1ffb UD |
35 | |
36 | #include <sys/mman.h> | |
37 | #include <sys/stat.h> | |
dc95d158 | 38 | #include <gnu/lib-names.h> |
591e1ffb UD |
39 | |
40 | #include "ldconfig.h" | |
41 | ||
42 | #define Elf32_CLASS ELFCLASS32 | |
43 | #define Elf64_CLASS ELFCLASS64 | |
44 | ||
45 | struct known_names | |
46 | { | |
47 | const char *soname; | |
48 | int flag; | |
49 | }; | |
50 | ||
a986484f | 51 | static struct known_names interpreters[] = |
591e1ffb | 52 | { |
a986484f | 53 | { "/lib/" LD_SO, FLAG_ELF_LIBC6 }, |
667b0577 UD |
54 | #ifdef SYSDEP_KNOWN_INTERPRETER_NAMES |
55 | SYSDEP_KNOWN_INTERPRETER_NAMES | |
56 | #endif | |
591e1ffb UD |
57 | }; |
58 | ||
a986484f | 59 | static struct known_names known_libs[] = |
591e1ffb | 60 | { |
a986484f UD |
61 | { LIBC_SO, FLAG_ELF_LIBC6 }, |
62 | { LIBM_SO, FLAG_ELF_LIBC6 }, | |
667b0577 UD |
63 | #ifdef SYSDEP_KNOWN_LIBRARY_NAMES |
64 | SYSDEP_KNOWN_LIBRARY_NAMES | |
65 | #endif | |
591e1ffb UD |
66 | }; |
67 | ||
68 | ||
69 | ||
70 | /* Returns 0 if everything is ok, != 0 in case of error. */ | |
71 | int | |
b4a555d6 | 72 | process_file (const char *real_file_name, const char *file_name, |
a986484f UD |
73 | const char *lib, int *flag, unsigned int *osversion, |
74 | char **soname, int is_link) | |
591e1ffb UD |
75 | { |
76 | FILE *file; | |
b4a555d6 | 77 | struct stat64 statbuf; |
591e1ffb UD |
78 | void *file_contents; |
79 | int ret; | |
591e1ffb UD |
80 | ElfW(Ehdr) *elf_header; |
81 | struct exec *aout_header; | |
82 | ||
83 | ret = 0; | |
84 | *flag = FLAG_ANY; | |
85 | *soname = NULL; | |
86 | ||
b4a555d6 | 87 | file = fopen (real_file_name, "rb"); |
591e1ffb UD |
88 | if (file == NULL) |
89 | { | |
90 | /* No error for stale symlink. */ | |
7ad9abc0 | 91 | if (is_link && strstr (file_name, ".so") != NULL) |
591e1ffb UD |
92 | return 1; |
93 | error (0, 0, _("Input file %s not found.\n"), file_name); | |
94 | return 1; | |
95 | } | |
96 | ||
b4a555d6 | 97 | if (fstat64 (fileno (file), &statbuf) < 0) |
591e1ffb UD |
98 | { |
99 | error (0, 0, _("Cannot fstat file %s.\n"), file_name); | |
e7c036b3 UD |
100 | fclose (file); |
101 | return 1; | |
102 | } | |
103 | ||
104 | /* Check that the file is large enough so that we can access the | |
105 | information. We're only checking the size of the headers here. */ | |
6dd67bd5 UD |
106 | if ((size_t) statbuf.st_size < sizeof (struct exec) |
107 | || (size_t) statbuf.st_size < sizeof (ElfW(Ehdr))) | |
e7c036b3 UD |
108 | { |
109 | error (0, 0, _("File %s is too small, not checked."), file_name); | |
110 | fclose (file); | |
591e1ffb UD |
111 | return 1; |
112 | } | |
113 | ||
e7c036b3 UD |
114 | file_contents = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, |
115 | fileno (file), 0); | |
591e1ffb UD |
116 | if (file_contents == MAP_FAILED) |
117 | { | |
118 | error (0, 0, _("Cannot mmap file %s.\n"), file_name); | |
119 | fclose (file); | |
120 | return 1; | |
121 | } | |
122 | ||
123 | /* First check if this is an aout file. */ | |
124 | aout_header = (struct exec *) file_contents; | |
125 | if (N_MAGIC (*aout_header) == ZMAGIC | |
be3c40b6 RM |
126 | #ifdef QMAGIC /* Linuxism. */ |
127 | || N_MAGIC (*aout_header) == QMAGIC | |
128 | #endif | |
129 | ) | |
591e1ffb | 130 | { |
e7c036b3 | 131 | /* Aout files don't have a soname, just return the name |
591e1ffb UD |
132 | including the major number. */ |
133 | char *copy, *major, *dot; | |
134 | copy = xstrdup (lib); | |
135 | major = strstr (copy, ".so."); | |
136 | if (major) | |
137 | { | |
138 | dot = strstr (major + 4, "."); | |
139 | if (dot) | |
140 | *dot = '\0'; | |
141 | } | |
142 | *soname = copy; | |
143 | *flag = FLAG_LIBC4; | |
144 | goto done; | |
145 | } | |
af1680f1 | 146 | |
591e1ffb | 147 | elf_header = (ElfW(Ehdr) *) file_contents; |
a986484f | 148 | if (memcmp (elf_header->e_ident, ELFMAG, SELFMAG) != 0) |
591e1ffb UD |
149 | { |
150 | /* The file is neither ELF nor aout. Check if it's a linker script, | |
151 | like libc.so - otherwise complain. */ | |
152 | int len = statbuf.st_size; | |
153 | /* Only search the beginning of the file. */ | |
154 | if (len > 512) | |
155 | len = 512; | |
156 | if (memmem (file_contents, len, "GROUP", 5) == NULL | |
157 | && memmem (file_contents, len, "GNU ld script", 13) == NULL) | |
158 | error (0, 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"), | |
159 | file_name); | |
160 | ret = 1; | |
161 | goto done; | |
162 | } | |
163 | ||
a986484f UD |
164 | if (process_elf_file (file_name, lib, flag, osversion, soname, |
165 | file_contents, statbuf.st_size)) | |
591e1ffb UD |
166 | ret = 1; |
167 | ||
168 | done: | |
169 | /* Clean up allocated memory and resources. */ | |
170 | munmap (file_contents, statbuf.st_size); | |
171 | fclose (file); | |
172 | ||
173 | return ret; | |
174 | } | |
175 | ||
176 | /* Get architecture specific version of process_elf_file. */ | |
177 | #include "readelflib.c" |