1/* Copyright (C) 1999-2021 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.
5
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.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <https://www.gnu.org/licenses/>. */
18
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. */
23
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>
36
37#include <ldconfig.h>
38
39#define Elf32_CLASS ELFCLASS32
40#define Elf64_CLASS ELFCLASS64
41
42struct known_names
43{
44 const char *soname;
45 int flag;
46};
47
48static struct known_names interpreters[] =
49{
50 { "/lib/" LD_SO, FLAG_ELF_LIBC6 },
51#ifdef SYSDEP_KNOWN_INTERPRETER_NAMES
52 SYSDEP_KNOWN_INTERPRETER_NAMES
53#endif
54};
55
56static struct known_names known_libs[] =
57{
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
63};
64
65
66/* Check if string corresponds to a GDB Python file. */
67static bool
68is_gdb_python_file (const char *name)
69{
70 size_t len = strlen (name);
71 return len > 7 && strcmp (name + len - 7, "-gdb.py") == 0;
72}
73
74/* Returns 0 if everything is ok, != 0 in case of error. */
75int
76process_file (const char *real_file_name, const char *file_name,
77 const char *lib, int *flag, unsigned int *osversion,
78 unsigned int *isa_level, char **soname, int is_link,
79 struct stat64 *stat_buf)
80{
81 FILE *file;
82 struct stat64 statbuf;
83 void *file_contents;
84 int ret;
85 ElfW(Ehdr) *elf_header;
86 struct exec *aout_header;
87
88 ret = 0;
89 *flag = FLAG_ANY;
90 *soname = NULL;
91
92 file = fopen (real_file_name, "rb");
93 if (file == NULL)
94 {
95 /* No error for stale symlink. */
96 if (is_link && strstr (file_name, ".so") != NULL)
97 return 1;
98 error (0, 0, _("Input file %s not found.\n"), file_name);
99 return 1;
100 }
101
102 if (fstat64 (fileno (file), &statbuf) < 0)
103 {
104 error (0, 0, _("Cannot fstat file %s.\n"), file_name);
105 fclose (file);
106 return 1;
107 }
108
109 /* Check that the file is large enough so that we can access the
110 information. We're only checking the size of the headers here. */
111 if ((size_t) statbuf.st_size < sizeof (struct exec)
112 || (size_t) statbuf.st_size < sizeof (ElfW(Ehdr)))
113 {
114 if (statbuf.st_size == 0)
115 error (0, 0, _("File %s is empty, not checked."), file_name);
116 else
117 {
118 char buf[SELFMAG];
119 size_t n = MIN (statbuf.st_size, SELFMAG);
120 if (fread (buf, n, 1, file) == 1 && memcmp (buf, ELFMAG, n) == 0)
121 error (0, 0, _("File %s is too small, not checked."), file_name);
122 }
123 fclose (file);
124 return 1;
125 }
126
127 file_contents = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED,
128 fileno (file), 0);
129 if (file_contents == MAP_FAILED)
130 {
131 error (0, 0, _("Cannot mmap file %s.\n"), file_name);
132 fclose (file);
133 return 1;
134 }
135
136 /* First check if this is an aout file. */
137 aout_header = (struct exec *) file_contents;
138 if (N_MAGIC (*aout_header) == ZMAGIC
139#ifdef QMAGIC /* Linuxism. */
140 || N_MAGIC (*aout_header) == QMAGIC
141#endif
142 )
143 {
144 /* Aout files don't have a soname, just return the name
145 including the major number. */
146 char *copy, *major, *dot;
147 copy = xstrdup (lib);
148 major = strstr (copy, ".so.");
149 if (major)
150 {
151 dot = strstr (major + 4, ".");
152 if (dot)
153 *dot = '\0';
154 }
155 *soname = copy;
156 *flag = FLAG_LIBC4;
157 goto done;
158 }
159
160 elf_header = (ElfW(Ehdr) *) file_contents;
161 if (memcmp (elf_header->e_ident, ELFMAG, SELFMAG) != 0)
162 {
163 /* The file is neither ELF nor aout. Check if it's a linker
164 script, like libc.so - otherwise complain. Only search the
165 beginning of the file. */
166 size_t len = MIN (statbuf.st_size, 512);
167 if (memmem (file_contents, len, "GROUP", 5) == NULL
168 && memmem (file_contents, len, "GNU ld script", 13) == NULL
169 && !is_gdb_python_file (file_name))
170 error (0, 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"),
171 file_name);
172 ret = 1;
173 }
174 /* Libraries have to be shared object files. */
175 else if (elf_header->e_type != ET_DYN)
176 ret = 1;
177 else if (process_elf_file (file_name, lib, flag, osversion, isa_level,
178 soname, file_contents, statbuf.st_size))
179 ret = 1;
180
181 done:
182 /* Clean up allocated memory and resources. */
183 munmap (file_contents, statbuf.st_size);
184 fclose (file);
185
186 *stat_buf = statbuf;
187 return ret;
188}
189
190/* Returns made up soname if lib doesn't have explicit DT_SONAME. */
191
192char *
193implicit_soname (const char *lib, int flag)
194{
195 char *soname = xstrdup (lib);
196
197 if ((flag & FLAG_TYPE_MASK) != FLAG_LIBC4)
198 return soname;
199
200 /* Aout files don't have a soname, just return the name
201 including the major number. */
202 char *major = strstr (soname, ".so.");
203 if (major)
204 {
205 char *dot = strstr (major + 4, ".");
206 if (dot)
207 *dot = '\0';
208 }
209 return soname;
210}
211
212/* Get architecture specific version of process_elf_file. */
213#include <readelflib.c>
214