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. */
26 #include "elf/common.h"
27 #include "elf/internal.h"
30 segment_executable (struct elf_segment_map *seg)
32 if (seg->p_flags_valid)
33 return (seg->p_flags & PF_X) != 0;
36 /* The p_flags value has not been computed yet,
37 so we have to look through the sections. */
39 for (i = 0; i < seg->count; ++i)
40 if (seg->sections[i]->flags & SEC_CODE)
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. */
51 segment_eligible_for_headers (struct elf_segment_map *seg,
52 bfd_vma minpagesize, bfd_vma sizeof_headers)
55 if (seg->count == 0 || seg->sections[0]->lma % minpagesize < sizeof_headers)
57 for (i = 0; i < seg->count; ++i)
59 if ((seg->sections[i]->flags & (SEC_CODE|SEC_READONLY)) != SEC_READONLY)
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. */
70 nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
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;
79 if (info != NULL && info->user_phdrs)
80 /* The linker script used PHDRS explicitly, so don't change what the
86 struct elf_segment_map *seg = *m;
88 if (seg->p_type == PT_LOAD)
90 bfd_boolean executable = segment_executable (seg);
94 && seg->sections[0]->vma % minpagesize == 0)
96 asection *lastsec = seg->sections[seg->count - 1];
97 bfd_vma end = lastsec->vma + lastsec->size;
98 if (end % minpagesize != 0)
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
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. */
121 struct elf_segment_map *newseg;
123 struct bfd_elf_section_data *secdata;
125 BFD_ASSERT (!seg->p_size_valid);
127 secdata = bfd_zalloc (abfd, sizeof *secdata);
131 sec = bfd_zalloc (abfd, sizeof *sec);
135 /* Fill in only the fields that actually affect the logic
136 in assign_file_positions_for_load_sections. */
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;
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;
149 newseg = bfd_alloc (abfd,
150 sizeof *newseg + ((seg->count + 1)
151 * sizeof (asection *)));
155 sizeof *newseg + (seg->count * sizeof (asection *)));
156 newseg->sections[newseg->count++] = sec;
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. */
165 if (first_load == NULL)
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,
177 /* This is the one we were looking for!
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;
184 prevseg = prevseg->next)
185 if (prevseg->p_type == PT_LOAD)
187 prevseg->includes_filehdr = 0;
188 prevseg->includes_phdrs = 0;
191 /* This segment will include those headers instead. */
192 seg->includes_filehdr = 1;
193 seg->includes_phdrs = 1;
195 moved_headers = TRUE;
203 if (first_load != last_load && moved_headers)
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;
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. */
222 nacl_modify_program_headers (bfd *abfd, struct bfd_link_info *info)
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;
228 if (info != NULL && info->user_phdrs)
229 /* The linker script used PHDRS explicitly, so don't change what the
233 /* Find the PT_LOAD that contains the headers (should be the first). */
236 if ((*m)->p_type == PT_LOAD && (*m)->includes_filehdr)
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;
250 /* Now move past that first one and find the PT_LOAD that should be
251 before it by address order. */
258 if (p->p_type == PT_LOAD && p->p_vaddr < first_load_phdr->p_vaddr)
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)
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;
280 if (next_load_seg == &first_seg->next)
282 *first_load_seg = next_seg;
283 next_seg->next = first_seg;
284 first_seg->next = next_next;
288 *first_load_seg = first_next;
289 *next_load_seg = next_next;
291 first_seg->next = *next_load_seg;
292 *next_load_seg = first_seg;
294 next_seg->next = *first_load_seg;
295 *first_load_seg = next_seg;
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;
309 nacl_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
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
315 && seg->sections[seg->count - 1]->owner == NULL)
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. */
321 asection *sec = seg->sections[seg->count - 1];
324 BFD_ASSERT (sec->flags & SEC_LINKER_CREATED);
325 BFD_ASSERT (sec->flags & SEC_CODE);
326 BFD_ASSERT (sec->size > 0);
328 fill = abfd->arch_info->fill (sec->size, bfd_big_endian (abfd), TRUE);
331 || bfd_seek (abfd, sec->filepos, SEEK_SET) != 0
332 || bfd_bwrite (fill, sec->size, abfd) != sec->size)
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
337 elf_elfheader (abfd)->e_shoff = (file_ptr) -1;