]>
Commit | Line | Data |
---|---|---|
581c785b | 1 | /* Copyright (C) 1999-2022 Free Software Foundation, Inc. |
591e1ffb | 2 | This file is part of the GNU C Library. |
591e1ffb | 3 | |
43bc8ac6 | 4 | This program is free software; you can redistribute it and/or modify |
2e2efe65 RM |
5 | it under the terms of the GNU General Public License as published |
6 | by the Free Software Foundation; version 2 of the License, or | |
7 | (at your option) any later version. | |
591e1ffb | 8 | |
43bc8ac6 | 9 | This program is distributed in the hope that it will be useful, |
591e1ffb | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
43bc8ac6 UD |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. | |
591e1ffb | 13 | |
43bc8ac6 | 14 | You should have received a copy of the GNU General Public License |
5a82c748 | 15 | along with this program; if not, see <https://www.gnu.org/licenses/>. */ |
591e1ffb UD |
16 | |
17 | /* The code in this file and in readelflib is a heavily simplified | |
18 | version of the readelf program that's part of the current binutils | |
19 | development version. Besides the simplification, it has also been | |
20 | modified to read some other file formats. */ | |
21 | ||
f00b1941 | 22 | #include <a.out.h> |
591e1ffb UD |
23 | #include <elf.h> |
24 | #include <error.h> | |
591e1ffb | 25 | #include <libintl.h> |
f00b1941 | 26 | #include <link.h> |
591e1ffb UD |
27 | #include <stdio.h> |
28 | #include <string.h> | |
29 | #include <unistd.h> | |
591e1ffb | 30 | #include <sys/mman.h> |
f00b1941 | 31 | #include <sys/param.h> |
591e1ffb | 32 | #include <sys/stat.h> |
dc95d158 | 33 | #include <gnu/lib-names.h> |
591e1ffb | 34 | |
8f480b4b | 35 | #include <ldconfig.h> |
591e1ffb UD |
36 | |
37 | #define Elf32_CLASS ELFCLASS32 | |
38 | #define Elf64_CLASS ELFCLASS64 | |
39 | ||
40 | struct known_names | |
41 | { | |
42 | const char *soname; | |
43 | int flag; | |
44 | }; | |
45 | ||
a986484f | 46 | static struct known_names interpreters[] = |
591e1ffb | 47 | { |
a986484f | 48 | { "/lib/" LD_SO, FLAG_ELF_LIBC6 }, |
667b0577 UD |
49 | #ifdef SYSDEP_KNOWN_INTERPRETER_NAMES |
50 | SYSDEP_KNOWN_INTERPRETER_NAMES | |
51 | #endif | |
591e1ffb UD |
52 | }; |
53 | ||
a986484f | 54 | static struct known_names known_libs[] = |
591e1ffb | 55 | { |
a986484f UD |
56 | { LIBC_SO, FLAG_ELF_LIBC6 }, |
57 | { LIBM_SO, FLAG_ELF_LIBC6 }, | |
667b0577 UD |
58 | #ifdef SYSDEP_KNOWN_LIBRARY_NAMES |
59 | SYSDEP_KNOWN_LIBRARY_NAMES | |
60 | #endif | |
591e1ffb UD |
61 | }; |
62 | ||
63 | ||
eea3dc5b L |
64 | /* Check if string corresponds to a GDB Python file. */ |
65 | static bool | |
66 | is_gdb_python_file (const char *name) | |
67 | { | |
68 | size_t len = strlen (name); | |
69 | return len > 7 && strcmp (name + len - 7, "-gdb.py") == 0; | |
70 | } | |
591e1ffb UD |
71 | |
72 | /* Returns 0 if everything is ok, != 0 in case of error. */ | |
73 | int | |
b4a555d6 | 74 | process_file (const char *real_file_name, const char *file_name, |
a986484f | 75 | const char *lib, int *flag, unsigned int *osversion, |
efbbd9c3 | 76 | unsigned int *isa_level, char **soname, int is_link, |
9fe6f636 | 77 | struct stat *stat_buf) |
591e1ffb UD |
78 | { |
79 | FILE *file; | |
9fe6f636 | 80 | struct stat statbuf; |
591e1ffb UD |
81 | void *file_contents; |
82 | int ret; | |
591e1ffb UD |
83 | ElfW(Ehdr) *elf_header; |
84 | struct exec *aout_header; | |
85 | ||
86 | ret = 0; | |
87 | *flag = FLAG_ANY; | |
88 | *soname = NULL; | |
89 | ||
b4a555d6 | 90 | file = fopen (real_file_name, "rb"); |
591e1ffb UD |
91 | if (file == NULL) |
92 | { | |
93 | /* No error for stale symlink. */ | |
7ad9abc0 | 94 | if (is_link && strstr (file_name, ".so") != NULL) |
591e1ffb UD |
95 | return 1; |
96 | error (0, 0, _("Input file %s not found.\n"), file_name); | |
97 | return 1; | |
98 | } | |
99 | ||
9fe6f636 | 100 | if (fstat (fileno (file), &statbuf) < 0) |
591e1ffb UD |
101 | { |
102 | error (0, 0, _("Cannot fstat file %s.\n"), file_name); | |
e7c036b3 UD |
103 | fclose (file); |
104 | return 1; | |
105 | } | |
106 | ||
107 | /* Check that the file is large enough so that we can access the | |
108 | information. We're only checking the size of the headers here. */ | |
6dd67bd5 UD |
109 | if ((size_t) statbuf.st_size < sizeof (struct exec) |
110 | || (size_t) statbuf.st_size < sizeof (ElfW(Ehdr))) | |
e7c036b3 | 111 | { |
625ef999 UD |
112 | if (statbuf.st_size == 0) |
113 | error (0, 0, _("File %s is empty, not checked."), file_name); | |
114 | else | |
115 | { | |
116 | char buf[SELFMAG]; | |
117 | size_t n = MIN (statbuf.st_size, SELFMAG); | |
118 | if (fread (buf, n, 1, file) == 1 && memcmp (buf, ELFMAG, n) == 0) | |
119 | error (0, 0, _("File %s is too small, not checked."), file_name); | |
120 | } | |
e7c036b3 | 121 | fclose (file); |
591e1ffb UD |
122 | return 1; |
123 | } | |
124 | ||
e7c036b3 UD |
125 | file_contents = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, |
126 | fileno (file), 0); | |
591e1ffb UD |
127 | if (file_contents == MAP_FAILED) |
128 | { | |
129 | error (0, 0, _("Cannot mmap file %s.\n"), file_name); | |
130 | fclose (file); | |
131 | return 1; | |
132 | } | |
133 | ||
134 | /* First check if this is an aout file. */ | |
135 | aout_header = (struct exec *) file_contents; | |
136 | if (N_MAGIC (*aout_header) == ZMAGIC | |
be3c40b6 RM |
137 | #ifdef QMAGIC /* Linuxism. */ |
138 | || N_MAGIC (*aout_header) == QMAGIC | |
139 | #endif | |
140 | ) | |
591e1ffb | 141 | { |
e7c036b3 | 142 | /* Aout files don't have a soname, just return the name |
27d9ffda | 143 | including the major number. */ |
591e1ffb UD |
144 | char *copy, *major, *dot; |
145 | copy = xstrdup (lib); | |
146 | major = strstr (copy, ".so."); | |
147 | if (major) | |
148 | { | |
149 | dot = strstr (major + 4, "."); | |
150 | if (dot) | |
151 | *dot = '\0'; | |
152 | } | |
153 | *soname = copy; | |
154 | *flag = FLAG_LIBC4; | |
155 | goto done; | |
156 | } | |
af1680f1 | 157 | |
591e1ffb | 158 | elf_header = (ElfW(Ehdr) *) file_contents; |
a986484f | 159 | if (memcmp (elf_header->e_ident, ELFMAG, SELFMAG) != 0) |
591e1ffb | 160 | { |
f00b1941 UD |
161 | /* The file is neither ELF nor aout. Check if it's a linker |
162 | script, like libc.so - otherwise complain. Only search the | |
163 | beginning of the file. */ | |
164 | size_t len = MIN (statbuf.st_size, 512); | |
591e1ffb | 165 | if (memmem (file_contents, len, "GROUP", 5) == NULL |
eea3dc5b L |
166 | && memmem (file_contents, len, "GNU ld script", 13) == NULL |
167 | && !is_gdb_python_file (file_name)) | |
591e1ffb UD |
168 | error (0, 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"), |
169 | file_name); | |
170 | ret = 1; | |
591e1ffb | 171 | } |
7220da7c AJ |
172 | /* Libraries have to be shared object files. */ |
173 | else if (elf_header->e_type != ET_DYN) | |
174 | ret = 1; | |
efbbd9c3 L |
175 | else if (process_elf_file (file_name, lib, flag, osversion, isa_level, |
176 | soname, file_contents, statbuf.st_size)) | |
591e1ffb UD |
177 | ret = 1; |
178 | ||
179 | done: | |
180 | /* Clean up allocated memory and resources. */ | |
181 | munmap (file_contents, statbuf.st_size); | |
182 | fclose (file); | |
183 | ||
27d9ffda | 184 | *stat_buf = statbuf; |
591e1ffb UD |
185 | return ret; |
186 | } | |
187 | ||
27d9ffda UD |
188 | /* Returns made up soname if lib doesn't have explicit DT_SONAME. */ |
189 | ||
190 | char * | |
191 | implicit_soname (const char *lib, int flag) | |
192 | { | |
193 | char *soname = xstrdup (lib); | |
194 | ||
195 | if ((flag & FLAG_TYPE_MASK) != FLAG_LIBC4) | |
196 | return soname; | |
197 | ||
198 | /* Aout files don't have a soname, just return the name | |
199 | including the major number. */ | |
200 | char *major = strstr (soname, ".so."); | |
201 | if (major) | |
202 | { | |
203 | char *dot = strstr (major + 4, "."); | |
204 | if (dot) | |
205 | *dot = '\0'; | |
206 | } | |
207 | return soname; | |
208 | } | |
209 | ||
591e1ffb | 210 | /* Get architecture specific version of process_elf_file. */ |
2fdaad97 | 211 | #include <readelflib.c> |