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