]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/readelflib.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / elf / readelflib.c
CommitLineData
2b778ceb 1/* Copyright (C) 1999-2021 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 16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
591e1ffb
UD
19
20/* This code is a heavily simplified version of the readelf program
21 that's part of the current binutils development version. For architectures
22 which need to handle both 32bit and 64bit ELF libraries, this file is
23 included twice for each arch size. */
24
dc95d158
AJ
25/* check_ptr checks that a pointer is in the mmaped file and doesn't
26 point outside it. */
a0f5c800 27#undef check_ptr
dc95d158
AJ
28#define check_ptr(ptr) \
29do \
30 { \
31 if ((void *)(ptr) < file_contents \
32 || (void *)(ptr) > (file_contents+file_length)) \
33 { \
34 error (0, 0, _("file %s is truncated\n"), file_name); \
35 return 1; \
36 } \
37 } \
38 while (0);
c98d82db 39
591e1ffb
UD
40/* Returns 0 if everything is ok, != 0 in case of error. */
41int
dc95d158 42process_elf_file (const char *file_name, const char *lib, int *flag,
a986484f
UD
43 unsigned int *osversion, char **soname, void *file_contents,
44 size_t file_length)
591e1ffb
UD
45{
46 int i;
47 unsigned int j;
591e1ffb
UD
48 unsigned int dynamic_addr;
49 size_t dynamic_size;
50 char *program_interpreter;
c98d82db 51
591e1ffb
UD
52 ElfW(Ehdr) *elf_header;
53 ElfW(Phdr) *elf_pheader, *segment;
54 ElfW(Dyn) *dynamic_segment, *dyn_entry;
c98d82db 55 char *dynamic_strings;
591e1ffb
UD
56
57 elf_header = (ElfW(Ehdr) *) file_contents;
a986484f 58 *osversion = 0;
591e1ffb
UD
59
60 if (elf_header->e_ident [EI_CLASS] != ElfW (CLASS))
61 {
62 if (opt_verbose)
63 {
63fb40b3 64 if (elf_header->e_ident [EI_CLASS] == ELFCLASS32)
591e1ffb 65 error (0, 0, _("%s is a 32 bit ELF file.\n"), file_name);
63fb40b3 66 else if (elf_header->e_ident [EI_CLASS] == ELFCLASS64)
591e1ffb
UD
67 error (0, 0, _("%s is a 64 bit ELF file.\n"), file_name);
68 else
69 error (0, 0, _("Unknown ELFCLASS in file %s.\n"), file_name);
70 }
71 return 1;
72 }
73
74 if (elf_header->e_type != ET_DYN)
75 {
76 error (0, 0, _("%s is not a shared object file (Type: %d).\n"), file_name,
77 elf_header->e_type);
78 return 1;
79 }
c98d82db 80
591e1ffb
UD
81 /* Get information from elf program header. */
82 elf_pheader = (ElfW(Phdr) *) (elf_header->e_phoff + file_contents);
dc95d158 83 check_ptr (elf_pheader);
591e1ffb
UD
84
85 /* The library is an elf library, now search for soname and
86 libc5/libc6. */
87 *flag = FLAG_ELF;
c98d82db 88
591e1ffb
UD
89 dynamic_addr = 0;
90 dynamic_size = 0;
91 program_interpreter = NULL;
92 for (i = 0, segment = elf_pheader;
93 i < elf_header->e_phnum; i++, segment++)
94 {
dc95d158
AJ
95 check_ptr (segment);
96
591e1ffb
UD
97 switch (segment->p_type)
98 {
591e1ffb
UD
99 case PT_DYNAMIC:
100 if (dynamic_addr)
101 error (0, 0, _("more than one dynamic segment\n"));
102
103 dynamic_addr = segment->p_offset;
104 dynamic_size = segment->p_filesz;
105 break;
a986484f 106
591e1ffb
UD
107 case PT_INTERP:
108 program_interpreter = (char *) (file_contents + segment->p_offset);
dc95d158 109 check_ptr (program_interpreter);
591e1ffb
UD
110
111 /* Check if this is enough to classify the binary. */
112 for (j = 0; j < sizeof (interpreters) / sizeof (interpreters [0]);
113 ++j)
114 if (strcmp (program_interpreter, interpreters[j].soname) == 0)
115 {
116 *flag = interpreters[j].flag;
117 break;
118 }
119 break;
a986484f
UD
120
121 case PT_NOTE:
0e508a6e 122 if (!*osversion && segment->p_filesz >= 32 && segment->p_align >= 4)
a986484f
UD
123 {
124 ElfW(Word) *abi_note = (ElfW(Word) *) (file_contents
125 + segment->p_offset);
0e508a6e 126 ElfW(Addr) size = segment->p_filesz;
8d81ce0c
L
127 /* NB: Some PT_NOTE segment may have alignment value of 0
128 or 1. gABI specifies that PT_NOTE segments should be
129 aligned to 4 bytes in 32-bit objects and to 8 bytes in
130 64-bit objects. As a Linux extension, we also support
131 4 byte alignment in 64-bit objects. If p_align is less
132 than 4, we treate alignment as 4 bytes since some note
133 segments have 0 or 1 byte alignment. */
134 ElfW(Addr) align = segment->p_align;
135 if (align < 4)
136 align = 4;
137 else if (align != 4 && align != 8)
138 continue;
0e508a6e
UD
139
140 while (abi_note [0] != 4 || abi_note [1] != 16
141 || abi_note [2] != 1
142 || memcmp (abi_note + 3, "GNU", 4) != 0)
143 {
8d81ce0c
L
144 ElfW(Addr) note_size
145 = ELF_NOTE_NEXT_OFFSET (abi_note[0], abi_note[1],
146 align);
0e508a6e
UD
147
148 if (size - 32 < note_size || note_size == 0)
149 {
150 size = 0;
151 break;
152 }
153 size -= note_size;
154 abi_note = (void *) abi_note + note_size;
155 }
156
157 if (size == 0)
158 break;
159
34a5a146
JM
160 *osversion = ((abi_note [4] << 24)
161 | ((abi_note [5] & 0xff) << 16)
162 | ((abi_note [6] & 0xff) << 8)
163 | (abi_note [7] & 0xff));
a986484f
UD
164 }
165 break;
166
591e1ffb
UD
167 default:
168 break;
169 }
c98d82db 170
591e1ffb 171 }
591e1ffb
UD
172
173 /* Now we can read the dynamic sections. */
174 if (dynamic_size == 0)
175 return 1;
c98d82db 176
591e1ffb 177 dynamic_segment = (ElfW(Dyn) *) (file_contents + dynamic_addr);
dc95d158 178 check_ptr (dynamic_segment);
591e1ffb
UD
179
180 /* Find the string table. */
181 dynamic_strings = NULL;
182 for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL;
183 ++dyn_entry)
184 {
dc95d158 185 check_ptr (dyn_entry);
591e1ffb
UD
186 if (dyn_entry->d_tag == DT_STRTAB)
187 {
58e8f5fd
AS
188 /* Find the file offset of the segment containing the dynamic
189 string table. */
190 ElfW(Off) loadoff = -1;
191 for (i = 0, segment = elf_pheader;
192 i < elf_header->e_phnum; i++, segment++)
193 {
194 if (segment->p_type == PT_LOAD
195 && dyn_entry->d_un.d_val >= segment->p_vaddr
196 && (dyn_entry->d_un.d_val - segment->p_vaddr
197 < segment->p_filesz))
198 {
199 loadoff = segment->p_vaddr - segment->p_offset;
200 break;
201 }
202 }
203 if (loadoff == (ElfW(Off)) -1)
204 {
205 /* Very strange. */
206 loadoff = 0;
207 }
208
209 dynamic_strings = (char *) (file_contents + dyn_entry->d_un.d_val
210 - loadoff);
dc95d158 211 check_ptr (dynamic_strings);
591e1ffb
UD
212 break;
213 }
214 }
215
216 if (dynamic_strings == NULL)
217 return 1;
218
219 /* Now read the DT_NEEDED and DT_SONAME entries. */
220 for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL;
221 ++dyn_entry)
222 {
223 if (dyn_entry->d_tag == DT_NEEDED || dyn_entry->d_tag == DT_SONAME)
224 {
225 char *name = dynamic_strings + dyn_entry->d_un.d_val;
dc95d158 226 check_ptr (name);
591e1ffb
UD
227
228 if (dyn_entry->d_tag == DT_NEEDED)
229 {
c98d82db 230
591e1ffb
UD
231 if (*flag == FLAG_ELF)
232 {
233 /* Check if this is enough to classify the binary. */
234 for (j = 0;
235 j < sizeof (known_libs) / sizeof (known_libs [0]);
236 ++j)
237 if (strcmp (name, known_libs [j].soname) == 0)
238 {
239 *flag = known_libs [j].flag;
240 break;
241 }
242 }
243 }
244
245 else if (dyn_entry->d_tag == DT_SONAME)
246 *soname = xstrdup (name);
c98d82db 247
591e1ffb
UD
248 /* Do we have everything we need? */
249 if (*soname && *flag != FLAG_ELF)
250 return 0;
251 }
252 }
253
591e1ffb
UD
254 return 0;
255}