merge from gcc
[gdb/gnu.git] / bfd / elf-nacl.c
blob5ec11616c42cee09a57b602b5042dffacf4b32be
1 /* Native Client support for ELF
2 Copyright 2012, 2013 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 MA 02111-1307, USA. */
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf-nacl.h"
26 #include "elf/common.h"
27 #include "elf/internal.h"
29 static bfd_boolean
30 segment_executable (struct elf_segment_map *seg)
32 if (seg->p_flags_valid)
33 return (seg->p_flags & PF_X) != 0;
34 else
36 /* The p_flags value has not been computed yet,
37 so we have to look through the sections. */
38 unsigned int i;
39 for (i = 0; i < seg->count; ++i)
40 if (seg->sections[i]->flags & SEC_CODE)
41 return TRUE;
43 return FALSE;
46 /* Determine if this segment is eligible to receive the file and program
47 headers. It must be read-only, non-executable, and have contents.
48 Its first section must start far enough past the page boundary to
49 allow space for the headers. */
50 static bfd_boolean
51 segment_eligible_for_headers (struct elf_segment_map *seg,
52 bfd_vma minpagesize, bfd_vma sizeof_headers)
54 bfd_boolean any_contents = FALSE;
55 unsigned int i;
56 if (seg->count == 0 || seg->sections[0]->lma % minpagesize < sizeof_headers)
57 return FALSE;
58 for (i = 0; i < seg->count; ++i)
60 if ((seg->sections[i]->flags & (SEC_CODE|SEC_READONLY)) != SEC_READONLY)
61 return FALSE;
62 if (seg->sections[i]->flags & SEC_HAS_CONTENTS)
63 any_contents = TRUE;
65 return any_contents;
69 /* We permute the segment_map to get BFD to do the file layout we want:
70 The first non-executable PT_LOAD segment appears first in the file
71 and contains the ELF file header and phdrs. */
72 bfd_boolean
73 nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
75 struct elf_segment_map **m = &elf_seg_map (abfd);
76 struct elf_segment_map **first_load = NULL;
77 struct elf_segment_map **last_load = NULL;
78 bfd_boolean moved_headers = FALSE;
79 int sizeof_headers = info == NULL ? 0 : bfd_sizeof_headers (abfd, info);
80 bfd_vma minpagesize = get_elf_backend_data (abfd)->minpagesize;
82 if (info != NULL && info->user_phdrs)
83 /* The linker script used PHDRS explicitly, so don't change what the
84 user asked for. */
85 return TRUE;
87 while (*m != NULL)
89 struct elf_segment_map *seg = *m;
91 if (seg->p_type == PT_LOAD)
93 bfd_boolean executable = segment_executable (seg);
95 if (executable
96 && seg->count > 0
97 && seg->sections[0]->vma % minpagesize == 0)
99 asection *lastsec = seg->sections[seg->count - 1];
100 bfd_vma end = lastsec->vma + lastsec->size;
101 if (end % minpagesize != 0)
103 /* This is an executable segment that starts on a page
104 boundary but does not end on a page boundary. Fill
105 it out to a whole page with code fill (the tail of
106 the segment will not be within any section). Thus
107 the entire code segment can be mapped from the file
108 as whole pages and that mapping will contain only
109 valid instructions.
111 To accomplish this, we must fake out the code in
112 assign_file_positions_for_load_sections (elf.c) so
113 that it advances past the rest of the final page,
114 rather than trying to put the next (unaligned, or
115 unallocated) section. We do this by appending a
116 dummy section record to this element in the segment
117 map. No such output section ever actually exists,
118 but this gets the layout logic to advance the file
119 positions past this partial page. Since we are
120 lying to BFD like this, nothing will ever know to
121 write the section contents. So we do that by hand
122 after the fact, in nacl_final_write_processing, below. */
124 struct elf_segment_map *newseg;
125 asection *sec;
126 struct bfd_elf_section_data *secdata;
128 BFD_ASSERT (!seg->p_size_valid);
130 secdata = bfd_zalloc (abfd, sizeof *secdata);
131 if (secdata == NULL)
132 return FALSE;
134 sec = bfd_zalloc (abfd, sizeof *sec);
135 if (sec == NULL)
136 return FALSE;
138 /* Fill in only the fields that actually affect the logic
139 in assign_file_positions_for_load_sections. */
140 sec->vma = end;
141 sec->lma = lastsec->lma + lastsec->size;
142 sec->size = minpagesize - (end % minpagesize);
143 sec->flags = (SEC_ALLOC | SEC_LOAD
144 | SEC_READONLY | SEC_CODE | SEC_LINKER_CREATED);
145 sec->used_by_bfd = secdata;
147 secdata->this_hdr.sh_type = SHT_PROGBITS;
148 secdata->this_hdr.sh_flags = SHF_ALLOC | SHF_EXECINSTR;
149 secdata->this_hdr.sh_addr = sec->vma;
150 secdata->this_hdr.sh_size = sec->size;
152 newseg = bfd_alloc (abfd,
153 sizeof *newseg + ((seg->count + 1)
154 * sizeof (asection *)));
155 if (newseg == NULL)
156 return FALSE;
157 memcpy (newseg, seg,
158 sizeof *newseg + (seg->count * sizeof (asection *)));
159 newseg->sections[newseg->count++] = sec;
160 *m = seg = newseg;
164 /* First, we're just finding the earliest PT_LOAD.
165 By the normal rules, this will be the lowest-addressed one.
166 We only have anything interesting to do if it's executable. */
167 last_load = m;
168 if (first_load == NULL)
170 if (!executable)
171 goto next;
172 first_load = m;
174 /* Now that we've noted the first PT_LOAD, we're looking for
175 the first non-executable PT_LOAD with a nonempty p_filesz. */
176 else if (!moved_headers
177 && segment_eligible_for_headers (seg, minpagesize,
178 sizeof_headers))
180 /* This is the one we were looking for!
182 First, clear the flags on previous segments that
183 say they include the file header and phdrs. */
184 struct elf_segment_map *prevseg;
185 for (prevseg = *first_load;
186 prevseg != seg;
187 prevseg = prevseg->next)
188 if (prevseg->p_type == PT_LOAD)
190 prevseg->includes_filehdr = 0;
191 prevseg->includes_phdrs = 0;
194 /* This segment will include those headers instead. */
195 seg->includes_filehdr = 1;
196 seg->includes_phdrs = 1;
198 moved_headers = TRUE;
202 next:
203 m = &seg->next;
206 if (first_load != last_load && moved_headers)
208 /* Now swap the first and last PT_LOAD segments'
209 positions in segment_map. */
210 struct elf_segment_map *first = *first_load;
211 struct elf_segment_map *last = *last_load;
212 *first_load = first->next;
213 first->next = last->next;
214 last->next = first;
217 return TRUE;
220 /* After nacl_modify_segment_map has done its work, the file layout has
221 been done as we wanted. But the PT_LOAD phdrs are no longer in the
222 proper order for the ELF rule that they must appear in ascending address
223 order. So find the two segments we swapped before, and swap them back. */
224 bfd_boolean
225 nacl_modify_program_headers (bfd *abfd, struct bfd_link_info *info)
227 struct elf_segment_map **m = &elf_seg_map (abfd);
228 Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
229 Elf_Internal_Phdr *p = phdr;
231 if (info != NULL && info->user_phdrs)
232 /* The linker script used PHDRS explicitly, so don't change what the
233 user asked for. */
234 return TRUE;
236 /* Find the PT_LOAD that contains the headers (should be the first). */
237 while (*m != NULL)
239 if ((*m)->p_type == PT_LOAD && (*m)->includes_filehdr)
240 break;
242 m = &(*m)->next;
243 ++p;
246 if (*m != NULL)
248 struct elf_segment_map **first_load_seg = m;
249 Elf_Internal_Phdr *first_load_phdr = p;
250 struct elf_segment_map **next_load_seg = NULL;
251 Elf_Internal_Phdr *next_load_phdr = NULL;
253 /* Now move past that first one and find the PT_LOAD that should be
254 before it by address order. */
256 m = &(*m)->next;
257 ++p;
259 while (*m != NULL)
261 if (p->p_type == PT_LOAD && p->p_vaddr < first_load_phdr->p_vaddr)
263 next_load_seg = m;
264 next_load_phdr = p;
265 break;
268 m = &(*m)->next;
269 ++p;
272 /* Swap their positions in the segment_map back to how they used to be.
273 The phdrs have already been set up by now, so we have to slide up
274 the earlier ones to insert the one that should be first. */
275 if (next_load_seg != NULL)
277 Elf_Internal_Phdr move_phdr;
278 struct elf_segment_map *first_seg = *first_load_seg;
279 struct elf_segment_map *next_seg = *next_load_seg;
280 struct elf_segment_map *first_next = first_seg->next;
281 struct elf_segment_map *next_next = next_seg->next;
283 if (next_load_seg == &first_seg->next)
285 *first_load_seg = next_seg;
286 next_seg->next = first_seg;
287 first_seg->next = next_next;
289 else
291 *first_load_seg = first_next;
292 *next_load_seg = next_next;
294 first_seg->next = *next_load_seg;
295 *next_load_seg = first_seg;
297 next_seg->next = *first_load_seg;
298 *first_load_seg = next_seg;
301 move_phdr = *next_load_phdr;
302 memmove (first_load_phdr + 1, first_load_phdr,
303 (next_load_phdr - first_load_phdr) * sizeof move_phdr);
304 *first_load_phdr = move_phdr;
308 return TRUE;
311 void
312 nacl_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
314 struct elf_segment_map *seg;
315 for (seg = elf_seg_map (abfd); seg != NULL; seg = seg->next)
316 if (seg->p_type == PT_LOAD
317 && seg->count > 1
318 && seg->sections[seg->count - 1]->owner == NULL)
320 /* This is a fake section added in nacl_modify_segment_map, above.
321 It's not a real BFD section, so nothing wrote its contents.
322 Now write out its contents. */
324 asection *sec = seg->sections[seg->count - 1];
325 char *fill;
327 BFD_ASSERT (sec->flags & SEC_LINKER_CREATED);
328 BFD_ASSERT (sec->flags & SEC_CODE);
329 BFD_ASSERT (sec->size > 0);
331 fill = abfd->arch_info->fill (sec->size, bfd_big_endian (abfd), TRUE);
333 if (fill == NULL
334 || bfd_seek (abfd, sec->filepos, SEEK_SET) != 0
335 || bfd_bwrite (fill, sec->size, abfd) != sec->size)
337 /* We don't have a proper way to report an error here. So
338 instead fudge things so that elf_write_shdrs_and_ehdr will
339 fail. */
340 elf_elfheader (abfd)->e_shoff = (file_ptr) -1;
343 free (fill);