1 /* Native Client support for ELF
2 Copyright 2012 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. */
25 #include "elf/common.h"
26 #include "elf/internal.h"
29 segment_executable (struct elf_segment_map *seg)
31 if (seg->p_flags_valid)
32 return (seg->p_flags & PF_X) != 0;
35 /* The p_flags value has not been computed yet,
36 so we have to look through the sections. */
38 for (i = 0; i < seg->count; ++i)
39 if (seg->sections[i]->flags & SEC_CODE)
46 segment_nonexecutable_and_has_contents (struct elf_segment_map *seg)
48 bfd_boolean any_contents = FALSE;
50 for (i = 0; i < seg->count; ++i)
52 if (seg->sections[i]->flags & SEC_CODE)
54 if (seg->sections[i]->flags & SEC_HAS_CONTENTS)
61 /* We permute the segment_map to get BFD to do the file layout we want:
62 The first non-executable PT_LOAD segment appears first in the file
63 and contains the ELF file header and phdrs. */
65 nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
67 struct elf_segment_map **m = &elf_tdata (abfd)->segment_map;
68 struct elf_segment_map **first_load = NULL;
69 struct elf_segment_map **last_load = NULL;
70 bfd_boolean moved_headers = FALSE;
74 struct elf_segment_map *seg = *m;
76 if (seg->p_type == PT_LOAD)
78 /* First, we're just finding the earliest PT_LOAD.
79 By the normal rules, this will be the lowest-addressed one.
80 We only have anything interesting to do if it's executable. */
82 if (first_load == NULL)
84 if (!segment_executable (*m))
88 /* Now that we've noted the first PT_LOAD, we're looking for
89 the first non-executable PT_LOAD with a nonempty p_filesz. */
90 else if (!moved_headers
91 && segment_nonexecutable_and_has_contents (seg))
93 /* This is the one we were looking for!
95 First, clear the flags on previous segments that
96 say they include the file header and phdrs. */
97 struct elf_segment_map *prevseg;
98 for (prevseg = *first_load;
100 prevseg = prevseg->next)
101 if (prevseg->p_type == PT_LOAD)
103 prevseg->includes_filehdr = 0;
104 prevseg->includes_phdrs = 0;
107 /* This segment will include those headers instead. */
108 seg->includes_filehdr = 1;
109 seg->includes_phdrs = 1;
111 moved_headers = TRUE;
118 if (first_load != last_load && moved_headers)
120 /* Now swap the first and last PT_LOAD segments'
121 positions in segment_map. */
122 struct elf_segment_map *first = *first_load;
123 struct elf_segment_map *last = *last_load;
124 *first_load = first->next;
125 first->next = last->next;
132 /* After nacl_modify_segment_map has done its work, the file layout has
133 been done as we wanted. But the PT_LOAD phdrs are no longer in the
134 proper order for the ELF rule that they must appear in ascending address
135 order. So find the two segments we swapped before, and swap them back. */
137 nacl_modify_program_headers (bfd *abfd,
138 struct bfd_link_info *info ATTRIBUTE_UNUSED)
140 struct elf_segment_map **m = &elf_tdata (abfd)->segment_map;
141 Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
142 Elf_Internal_Phdr *p = phdr;
144 /* Find the PT_LOAD that contains the headers (should be the first). */
147 if ((*m)->p_type == PT_LOAD && (*m)->includes_filehdr)
156 struct elf_segment_map **first_load_seg = m;
157 Elf_Internal_Phdr *first_load_phdr = p;
158 struct elf_segment_map **next_load_seg = NULL;
159 Elf_Internal_Phdr *next_load_phdr = NULL;
161 /* Now move past that first one and find the PT_LOAD that should be
162 before it by address order. */
169 if (p->p_type == PT_LOAD && p->p_vaddr < first_load_phdr->p_vaddr)
180 /* Swap their positions in the segment_map back to how they used to be.
181 The phdrs have already been set up by now, so we have to slide up
182 the earlier ones to insert the one that should be first. */
183 if (next_load_seg != NULL)
185 Elf_Internal_Phdr move_phdr;
186 struct elf_segment_map *first_seg = *first_load_seg;
187 struct elf_segment_map *next_seg = *next_load_seg;
188 struct elf_segment_map *first_next = first_seg->next;
189 struct elf_segment_map *next_next = next_seg->next;
191 first_seg->next = next_next;
192 *first_load_seg = next_seg;
194 next_seg->next = first_next;
195 *next_load_seg = first_seg;
197 move_phdr = *next_load_phdr;
198 memmove (first_load_phdr + 1, first_load_phdr,
199 (next_load_phdr - first_load_phdr) * sizeof move_phdr);
200 *first_load_phdr = move_phdr;