PR25196, abort in rewrite_elf_program_header
[deliverable/binutils-gdb.git] / bfd / elf-nacl.c
CommitLineData
5a68afcf 1/* Native Client support for ELF
82704155 2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
5a68afcf
RM
3
4 This file is part of BFD, the Binary File Descriptor library.
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 by
8 the Free Software Foundation; either version 3 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
08832196 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
5a68afcf
RM
18
19#include "sysdep.h"
20#include "bfd.h"
887badb3 21#include "libbfd.h"
5a68afcf
RM
22#include "elf-bfd.h"
23#include "elf-nacl.h"
24#include "elf/common.h"
25#include "elf/internal.h"
26
27static bfd_boolean
28segment_executable (struct elf_segment_map *seg)
29{
30 if (seg->p_flags_valid)
31 return (seg->p_flags & PF_X) != 0;
32 else
33 {
34 /* The p_flags value has not been computed yet,
887badb3 35 so we have to look through the sections. */
5a68afcf
RM
36 unsigned int i;
37 for (i = 0; i < seg->count; ++i)
887badb3
RM
38 if (seg->sections[i]->flags & SEC_CODE)
39 return TRUE;
5a68afcf
RM
40 }
41 return FALSE;
42}
43
a3f548ed 44/* Determine if this segment is eligible to receive the file and program
86e0565d 45 headers. It must be read-only and non-executable.
a0c82a2b
RM
46 Its first section must start far enough past the page boundary to
47 allow space for the headers. */
5a68afcf 48static bfd_boolean
a3f548ed 49segment_eligible_for_headers (struct elf_segment_map *seg,
887badb3 50 bfd_vma minpagesize, bfd_vma sizeof_headers)
5a68afcf 51{
5a68afcf 52 unsigned int i;
887badb3 53 if (seg->count == 0 || seg->sections[0]->lma % minpagesize < sizeof_headers)
a3f548ed 54 return FALSE;
5a68afcf
RM
55 for (i = 0; i < seg->count; ++i)
56 {
a0c82a2b 57 if ((seg->sections[i]->flags & (SEC_CODE|SEC_READONLY)) != SEC_READONLY)
887badb3 58 return FALSE;
5a68afcf 59 }
86e0565d 60 return TRUE;
5a68afcf
RM
61}
62
63
64/* We permute the segment_map to get BFD to do the file layout we want:
65 The first non-executable PT_LOAD segment appears first in the file
66 and contains the ELF file header and phdrs. */
67bfd_boolean
d324f6d6 68nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
5a68afcf 69{
33aa641a 70 const struct elf_backend_data *const bed = get_elf_backend_data (abfd);
12bd6957 71 struct elf_segment_map **m = &elf_seg_map (abfd);
5a68afcf 72 struct elf_segment_map **first_load = NULL;
64029e93 73 struct elf_segment_map **headers = NULL;
33aa641a 74 int sizeof_headers;
5a68afcf 75
d324f6d6
RM
76 if (info != NULL && info->user_phdrs)
77 /* The linker script used PHDRS explicitly, so don't change what the
78 user asked for. */
79 return TRUE;
80
33aa641a
RM
81 if (info != NULL)
82 /* We're doing linking, so evalute SIZEOF_HEADERS as in a linker script. */
83 sizeof_headers = bfd_sizeof_headers (abfd, info);
84 else
85 {
86 /* We're not doing linking, so this is objcopy or suchlike.
87 We just need to collect the size of the existing headers. */
88 struct elf_segment_map *seg;
89 sizeof_headers = bed->s->sizeof_ehdr;
90 for (seg = *m; seg != NULL; seg = seg->next)
91 sizeof_headers += bed->s->sizeof_phdr;
92 }
93
5a68afcf
RM
94 while (*m != NULL)
95 {
96 struct elf_segment_map *seg = *m;
97
98 if (seg->p_type == PT_LOAD)
887badb3
RM
99 {
100 bfd_boolean executable = segment_executable (seg);
101
102 if (executable
103 && seg->count > 0
33aa641a 104 && seg->sections[0]->vma % bed->minpagesize == 0)
887badb3
RM
105 {
106 asection *lastsec = seg->sections[seg->count - 1];
107 bfd_vma end = lastsec->vma + lastsec->size;
33aa641a 108 if (end % bed->minpagesize != 0)
887badb3
RM
109 {
110 /* This is an executable segment that starts on a page
111 boundary but does not end on a page boundary. Fill
112 it out to a whole page with code fill (the tail of
113 the segment will not be within any section). Thus
114 the entire code segment can be mapped from the file
115 as whole pages and that mapping will contain only
116 valid instructions.
117
118 To accomplish this, we must fake out the code in
119 assign_file_positions_for_load_sections (elf.c) so
120 that it advances past the rest of the final page,
121 rather than trying to put the next (unaligned, or
122 unallocated) section. We do this by appending a
123 dummy section record to this element in the segment
124 map. No such output section ever actually exists,
125 but this gets the layout logic to advance the file
126 positions past this partial page. Since we are
127 lying to BFD like this, nothing will ever know to
128 write the section contents. So we do that by hand
129 after the fact, in nacl_final_write_processing, below. */
130
131 struct elf_segment_map *newseg;
132 asection *sec;
133 struct bfd_elf_section_data *secdata;
134
135 BFD_ASSERT (!seg->p_size_valid);
136
137 secdata = bfd_zalloc (abfd, sizeof *secdata);
138 if (secdata == NULL)
139 return FALSE;
140
141 sec = bfd_zalloc (abfd, sizeof *sec);
142 if (sec == NULL)
143 return FALSE;
144
145 /* Fill in only the fields that actually affect the logic
146 in assign_file_positions_for_load_sections. */
147 sec->vma = end;
148 sec->lma = lastsec->lma + lastsec->size;
33aa641a 149 sec->size = bed->minpagesize - (end % bed->minpagesize);
887badb3
RM
150 sec->flags = (SEC_ALLOC | SEC_LOAD
151 | SEC_READONLY | SEC_CODE | SEC_LINKER_CREATED);
152 sec->used_by_bfd = secdata;
153
154 secdata->this_hdr.sh_type = SHT_PROGBITS;
155 secdata->this_hdr.sh_flags = SHF_ALLOC | SHF_EXECINSTR;
156 secdata->this_hdr.sh_addr = sec->vma;
157 secdata->this_hdr.sh_size = sec->size;
158
159 newseg = bfd_alloc (abfd,
160 sizeof *newseg + ((seg->count + 1)
161 * sizeof (asection *)));
162 if (newseg == NULL)
163 return FALSE;
164 memcpy (newseg, seg,
165 sizeof *newseg + (seg->count * sizeof (asection *)));
166 newseg->sections[newseg->count++] = sec;
167 *m = seg = newseg;
168 }
169 }
170
171 /* First, we're just finding the earliest PT_LOAD.
64029e93 172 By the normal rules, this will be the lowest-addressed one. */
887badb3 173 if (first_load == NULL)
64029e93
AM
174 first_load = m;
175
887badb3
RM
176 /* Now that we've noted the first PT_LOAD, we're looking for
177 the first non-executable PT_LOAD with a nonempty p_filesz. */
64029e93 178 else if (headers == NULL
33aa641a 179 && segment_eligible_for_headers (seg, bed->minpagesize,
887badb3 180 sizeof_headers))
64029e93 181 headers = m;
887badb3 182 }
5a68afcf
RM
183 m = &seg->next;
184 }
185
64029e93 186 if (headers != NULL)
5a68afcf 187 {
64029e93
AM
188 struct elf_segment_map **last_load = NULL;
189 struct elf_segment_map *seg;
190
191 m = first_load;
192 while ((seg = *m) != NULL)
193 {
194 if (seg->p_type == PT_LOAD)
195 {
196 /* Clear the flags on any previous segment that
197 included the file header and phdrs. */
198 seg->includes_filehdr = 0;
199 seg->includes_phdrs = 0;
30fe1832 200 seg->no_sort_lma = 1;
64029e93
AM
201 /* Also strip out empty segments. */
202 if (seg->count == 0)
203 {
204 if (headers == &seg->next)
205 headers = m;
206 *m = seg->next;
207 continue;
208 }
209 last_load = m;
210 }
211 m = &seg->next;
212 }
213
214 /* This segment will include those headers instead. */
215 seg = *headers;
216 seg->includes_filehdr = 1;
217 seg->includes_phdrs = 1;
218
219 if (last_load != NULL && first_load != last_load && first_load != headers)
220 {
221 /* Put the first PT_LOAD header last. */
222 struct elf_segment_map *first = *first_load;
223 struct elf_segment_map *last = *last_load;
224 *first_load = first->next;
225 first->next = last->next;
226 last->next = first;
227 }
5a68afcf
RM
228 }
229
230 return TRUE;
231}
232
233/* After nacl_modify_segment_map has done its work, the file layout has
234 been done as we wanted. But the PT_LOAD phdrs are no longer in the
235 proper order for the ELF rule that they must appear in ascending address
236 order. So find the two segments we swapped before, and swap them back. */
237bfd_boolean
aa6407c6 238nacl_modify_program_headers (bfd *abfd, struct bfd_link_info *info)
5a68afcf 239{
12bd6957 240 struct elf_segment_map **m = &elf_seg_map (abfd);
5a68afcf
RM
241 Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
242 Elf_Internal_Phdr *p = phdr;
243
d324f6d6
RM
244 if (info != NULL && info->user_phdrs)
245 /* The linker script used PHDRS explicitly, so don't change what the
246 user asked for. */
247 return TRUE;
248
5a68afcf
RM
249 /* Find the PT_LOAD that contains the headers (should be the first). */
250 while (*m != NULL)
251 {
252 if ((*m)->p_type == PT_LOAD && (*m)->includes_filehdr)
887badb3 253 break;
5a68afcf
RM
254
255 m = &(*m)->next;
256 ++p;
257 }
258
259 if (*m != NULL)
260 {
261 struct elf_segment_map **first_load_seg = m;
262 Elf_Internal_Phdr *first_load_phdr = p;
263 struct elf_segment_map **next_load_seg = NULL;
264 Elf_Internal_Phdr *next_load_phdr = NULL;
265
266 /* Now move past that first one and find the PT_LOAD that should be
887badb3 267 before it by address order. */
5a68afcf
RM
268
269 m = &(*m)->next;
270 ++p;
271
887badb3
RM
272 while (*m != NULL)
273 {
274 if (p->p_type == PT_LOAD && p->p_vaddr < first_load_phdr->p_vaddr)
275 {
276 next_load_seg = m;
277 next_load_phdr = p;
278 break;
279 }
5a68afcf 280
887badb3
RM
281 m = &(*m)->next;
282 ++p;
283 }
5a68afcf
RM
284
285 /* Swap their positions in the segment_map back to how they used to be.
887badb3
RM
286 The phdrs have already been set up by now, so we have to slide up
287 the earlier ones to insert the one that should be first. */
5a68afcf 288 if (next_load_seg != NULL)
887badb3
RM
289 {
290 Elf_Internal_Phdr move_phdr;
291 struct elf_segment_map *first_seg = *first_load_seg;
292 struct elf_segment_map *next_seg = *next_load_seg;
293 struct elf_segment_map *first_next = first_seg->next;
294 struct elf_segment_map *next_next = next_seg->next;
295
296 if (next_load_seg == &first_seg->next)
297 {
298 *first_load_seg = next_seg;
299 next_seg->next = first_seg;
300 first_seg->next = next_next;
301 }
302 else
303 {
304 *first_load_seg = first_next;
305 *next_load_seg = next_next;
306
307 first_seg->next = *next_load_seg;
308 *next_load_seg = first_seg;
309
310 next_seg->next = *first_load_seg;
311 *first_load_seg = next_seg;
312 }
313
314 move_phdr = *next_load_phdr;
315 memmove (first_load_phdr + 1, first_load_phdr,
316 (next_load_phdr - first_load_phdr) * sizeof move_phdr);
317 *first_load_phdr = move_phdr;
318 }
5a68afcf
RM
319 }
320
321 return TRUE;
322}
887badb3 323
cc364be6
AM
324bfd_boolean
325nacl_final_write_processing (bfd *abfd)
887badb3
RM
326{
327 struct elf_segment_map *seg;
328 for (seg = elf_seg_map (abfd); seg != NULL; seg = seg->next)
329 if (seg->p_type == PT_LOAD
330 && seg->count > 1
331 && seg->sections[seg->count - 1]->owner == NULL)
332 {
333 /* This is a fake section added in nacl_modify_segment_map, above.
334 It's not a real BFD section, so nothing wrote its contents.
335 Now write out its contents. */
336
337 asection *sec = seg->sections[seg->count - 1];
338 char *fill;
339
340 BFD_ASSERT (sec->flags & SEC_LINKER_CREATED);
341 BFD_ASSERT (sec->flags & SEC_CODE);
342 BFD_ASSERT (sec->size > 0);
343
344 fill = abfd->arch_info->fill (sec->size, bfd_big_endian (abfd), TRUE);
345
346 if (fill == NULL
347 || bfd_seek (abfd, sec->filepos, SEEK_SET) != 0
348 || bfd_bwrite (fill, sec->size, abfd) != sec->size)
349 {
350 /* We don't have a proper way to report an error here. So
351 instead fudge things so that elf_write_shdrs_and_ehdr will
352 fail. */
353 elf_elfheader (abfd)->e_shoff = (file_ptr) -1;
354 }
355
356 free (fill);
357 }
cc364be6 358 return _bfd_elf_final_write_processing (abfd);
887badb3 359}
This page took 0.425964 seconds and 4 git commands to generate.