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