1/* Copyright (C) 1999-2024 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
40struct known_names
41{
42 const char *soname;
43 int flag;
44};
45
46/* Check if string corresponds to a GDB Python file. */
47static bool
48is_gdb_python_file (const char *name)
49{
50 size_t len = strlen (s: name);
51 return len > 7 && strcmp (s1: name + len - 7, s2: "-gdb.py") == 0;
52}
53
54/* Returns 0 if everything is ok, != 0 in case of error. */
55int
56process_file (const char *real_file_name, const char *file_name,
57 const char *lib, int *flag, unsigned int *isa_level,
58 char **soname, int is_link, struct stat *stat_buf)
59{
60 FILE *file;
61 struct stat statbuf;
62 void *file_contents;
63 int ret;
64 ElfW(Ehdr) *elf_header;
65 struct exec *aout_header;
66
67 ret = 0;
68 /* Just set FLAG_ELF_LIBC6 as old formats are not supported anymore. */
69 *flag = FLAG_ELF_LIBC6;
70 *soname = NULL;
71
72 file = fopen (filename: real_file_name, modes: "rb");
73 if (file == NULL)
74 {
75 /* No error for stale symlink. */
76 if (is_link && strstr (haystack: file_name, needle: ".so") != NULL)
77 return 1;
78 error (status: 0, errnum: 0, _("Input file %s not found.\n"), file_name);
79 return 1;
80 }
81
82 if (fstat (fd: fileno (stream: file), buf: &statbuf) < 0)
83 {
84 error (status: 0, errnum: 0, _("Cannot fstat file %s.\n"), file_name);
85 fclose (stream: file);
86 return 1;
87 }
88
89 /* Check that the file is large enough so that we can access the
90 information. We're only checking the size of the headers here. */
91 if ((size_t) statbuf.st_size < sizeof (struct exec)
92 || (size_t) statbuf.st_size < sizeof (ElfW(Ehdr)))
93 {
94 if (statbuf.st_size == 0)
95 error (status: 0, errnum: 0, _("File %s is empty, not checked."), file_name);
96 else
97 {
98 char buf[SELFMAG];
99 size_t n = MIN (statbuf.st_size, SELFMAG);
100 if (fread (ptr: buf, size: n, n: 1, stream: file) == 1 && memcmp (s1: buf, ELFMAG, n: n) == 0)
101 error (status: 0, errnum: 0, _("File %s is too small, not checked."), file_name);
102 }
103 fclose (stream: file);
104 return 1;
105 }
106
107 file_contents = mmap (addr: 0, len: statbuf.st_size, PROT_READ, MAP_SHARED,
108 fd: fileno (stream: file), offset: 0);
109 if (file_contents == MAP_FAILED)
110 {
111 error (status: 0, errnum: 0, _("Cannot mmap file %s.\n"), file_name);
112 fclose (stream: file);
113 return 1;
114 }
115
116 /* First check if this is an aout file. */
117 aout_header = (struct exec *) file_contents;
118 if (N_MAGIC (*aout_header) == ZMAGIC
119#ifdef QMAGIC /* Linuxism. */
120 || N_MAGIC (*aout_header) == QMAGIC
121#endif
122 )
123 {
124 /* Aout files don't have a soname, just return the name
125 including the major number. */
126 char *copy, *major, *dot;
127 copy = xstrdup (lib);
128 major = strstr (haystack: copy, needle: ".so.");
129 if (major)
130 {
131 dot = strstr (haystack: major + 4, needle: ".");
132 if (dot)
133 *dot = '\0';
134 }
135 *soname = copy;
136 goto done;
137 }
138
139 elf_header = (ElfW(Ehdr) *) file_contents;
140 if (memcmp (s1: elf_header->e_ident, ELFMAG, SELFMAG) != 0)
141 {
142 /* The file is neither ELF nor aout. Check if it's a linker
143 script, like libc.so - otherwise complain. Only search the
144 beginning of the file. */
145 size_t len = MIN (statbuf.st_size, 512);
146 if (memmem (haystack: file_contents, haystacklen: len, needle: "GROUP", needlelen: 5) == NULL
147 && memmem (haystack: file_contents, haystacklen: len, needle: "GNU ld script", needlelen: 13) == NULL
148 && !is_gdb_python_file (name: file_name))
149 error (status: 0, errnum: 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"),
150 file_name);
151 ret = 1;
152 }
153 /* Libraries have to be shared object files. */
154 else if (elf_header->e_type != ET_DYN)
155 ret = 1;
156 else if (process_elf_file (file_name, lib, flag, isa_level, soname,
157 file_contents, file_length: statbuf.st_size))
158 ret = 1;
159
160 done:
161 /* Clean up allocated memory and resources. */
162 munmap (addr: file_contents, len: statbuf.st_size);
163 fclose (stream: file);
164
165 *stat_buf = statbuf;
166 return ret;
167}
168
169/* Get architecture specific version of process_elf_file. */
170#include <readelflib.c>
171

source code of glibc/elf/readlib.c