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