* sparc64-tdep.c (sparc64_store_arguments)
[deliverable/binutils-gdb.git] / bfd / elf-nacl.c
CommitLineData
5a68afcf
RM
1/* Native Client support for ELF
2 Copyright 2012 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 "elf-bfd.h"
24#include "elf-nacl.h"
25#include "elf/common.h"
26#include "elf/internal.h"
27
28static bfd_boolean
29segment_executable (struct elf_segment_map *seg)
30{
31 if (seg->p_flags_valid)
32 return (seg->p_flags & PF_X) != 0;
33 else
34 {
35 /* The p_flags value has not been computed yet,
36 so we have to look through the sections. */
37 unsigned int i;
38 for (i = 0; i < seg->count; ++i)
39 if (seg->sections[i]->flags & SEC_CODE)
40 return TRUE;
41 }
42 return FALSE;
43}
44
45static bfd_boolean
46segment_nonexecutable_and_has_contents (struct elf_segment_map *seg)
47{
48 bfd_boolean any_contents = FALSE;
49 unsigned int i;
50 for (i = 0; i < seg->count; ++i)
51 {
52 if (seg->sections[i]->flags & SEC_CODE)
53 return FALSE;
54 if (seg->sections[i]->flags & SEC_HAS_CONTENTS)
55 any_contents = TRUE;
56 }
57 return any_contents;
58}
59
60
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. */
64bfd_boolean
65nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
66{
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;
71
72 while (*m != NULL)
73 {
74 struct elf_segment_map *seg = *m;
75
76 if (seg->p_type == PT_LOAD)
77 {
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. */
81 last_load = m;
82 if (first_load == NULL)
83 {
84 if (!segment_executable (*m))
85 return TRUE;
86 first_load = m;
87 }
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))
92 {
93 /* This is the one we were looking for!
94
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;
99 prevseg != seg;
100 prevseg = prevseg->next)
101 if (prevseg->p_type == PT_LOAD)
102 {
103 prevseg->includes_filehdr = 0;
104 prevseg->includes_phdrs = 0;
105 }
106
107 /* This segment will include those headers instead. */
108 seg->includes_filehdr = 1;
109 seg->includes_phdrs = 1;
110
111 moved_headers = TRUE;
112 }
113 }
114
115 m = &seg->next;
116 }
117
118 if (first_load != last_load && moved_headers)
119 {
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;
126 last->next = first;
127 }
128
129 return TRUE;
130}
131
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. */
136bfd_boolean
137nacl_modify_program_headers (bfd *abfd,
138 struct bfd_link_info *info ATTRIBUTE_UNUSED)
139{
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;
143
144 /* Find the PT_LOAD that contains the headers (should be the first). */
145 while (*m != NULL)
146 {
147 if ((*m)->p_type == PT_LOAD && (*m)->includes_filehdr)
148 break;
149
150 m = &(*m)->next;
151 ++p;
152 }
153
154 if (*m != NULL)
155 {
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;
160
161 /* Now move past that first one and find the PT_LOAD that should be
162 before it by address order. */
163
164 m = &(*m)->next;
165 ++p;
166
167 while ((*m) != NULL)
168 {
169 if (p->p_type == PT_LOAD && p->p_vaddr < first_load_phdr->p_vaddr)
170 {
171 next_load_seg = m;
172 next_load_phdr = p;
173 break;
174 }
175
176 m = &(*m)->next;
177 ++p;
178 }
179
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)
184 {
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;
190
191 first_seg->next = next_next;
192 *first_load_seg = next_seg;
193
194 next_seg->next = first_next;
195 *next_load_seg = first_seg;
196
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;
201 }
202 }
203
204 return TRUE;
205}
This page took 0.033147 seconds and 4 git commands to generate.