bfd/
[external/binutils.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, 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)
53 {
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)
59     {
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;
64     }
65   return any_contents;
66 }
67
68
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)
74 {
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;
81
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;
86
87   while (*m != NULL)
88     {
89       struct elf_segment_map *seg = *m;
90
91       if (seg->p_type == PT_LOAD)
92         {
93           bfd_boolean executable = segment_executable (seg);
94
95           if (executable
96               && seg->count > 0
97               && seg->sections[0]->vma % minpagesize == 0)
98             {
99               asection *lastsec = seg->sections[seg->count - 1];
100               bfd_vma end = lastsec->vma + lastsec->size;
101               if (end % minpagesize != 0)
102                 {
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.
110
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.  */
123
124                   struct elf_segment_map *newseg;
125                   asection *sec;
126                   struct bfd_elf_section_data *secdata;
127
128                   BFD_ASSERT (!seg->p_size_valid);
129
130                   secdata = bfd_zalloc (abfd, sizeof *secdata);
131                   if (secdata == NULL)
132                     return FALSE;
133
134                   sec = bfd_zalloc (abfd, sizeof *sec);
135                   if (sec == NULL)
136                     return FALSE;
137
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;
146
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;
151
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;
161                 }
162             }
163
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)
169             {
170               if (!executable)
171                 goto next;
172               first_load = m;
173             }
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))
179             {
180               /* This is the one we were looking for!
181
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)
189                   {
190                     prevseg->includes_filehdr = 0;
191                     prevseg->includes_phdrs = 0;
192                   }
193
194               /* This segment will include those headers instead.  */
195               seg->includes_filehdr = 1;
196               seg->includes_phdrs = 1;
197
198               moved_headers = TRUE;
199             }
200         }
201
202     next:
203       m = &seg->next;
204     }
205
206   if (first_load != last_load && moved_headers)
207     {
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;
215     }
216
217   return TRUE;
218 }
219
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)
226 {
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;
230
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;
235
236   /* Find the PT_LOAD that contains the headers (should be the first).  */
237   while (*m != NULL)
238     {
239       if ((*m)->p_type == PT_LOAD && (*m)->includes_filehdr)
240         break;
241
242       m = &(*m)->next;
243       ++p;
244     }
245
246   if (*m != NULL)
247     {
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;
252
253       /* Now move past that first one and find the PT_LOAD that should be
254          before it by address order.  */
255
256       m = &(*m)->next;
257       ++p;
258
259       while (*m != NULL)
260         {
261           if (p->p_type == PT_LOAD && p->p_vaddr < first_load_phdr->p_vaddr)
262             {
263               next_load_seg = m;
264               next_load_phdr = p;
265               break;
266             }
267
268           m = &(*m)->next;
269           ++p;
270         }
271
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)
276         {
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;
282
283           if (next_load_seg == &first_seg->next)
284             {
285               *first_load_seg = next_seg;
286               next_seg->next = first_seg;
287               first_seg->next = next_next;
288             }
289           else
290             {
291               *first_load_seg = first_next;
292               *next_load_seg = next_next;
293
294               first_seg->next = *next_load_seg;
295               *next_load_seg = first_seg;
296
297               next_seg->next = *first_load_seg;
298               *first_load_seg = next_seg;
299             }
300
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;
305         }
306     }
307
308   return TRUE;
309 }
310
311 void
312 nacl_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
313 {
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)
319       {
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.  */
323
324         asection *sec = seg->sections[seg->count - 1];
325         char *fill;
326
327         BFD_ASSERT (sec->flags & SEC_LINKER_CREATED);
328         BFD_ASSERT (sec->flags & SEC_CODE);
329         BFD_ASSERT (sec->size > 0);
330
331         fill = abfd->arch_info->fill (sec->size, bfd_big_endian (abfd), TRUE);
332
333         if (fill == NULL
334             || bfd_seek (abfd, sec->filepos, SEEK_SET) != 0
335             || bfd_bwrite (fill, sec->size, abfd) != sec->size)
336           {
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;
341           }
342
343         free (fill);
344       }
345 }