]>
Commit | Line | Data |
---|---|---|
ede4eed4 | 1 | /* ELF linking support for BFD. |
8afe83be | 2 | Copyright 1995 Free Software Foundation, Inc. |
ede4eed4 KR |
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 2 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 | |
3b3753b8 | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
ede4eed4 KR |
19 | |
20 | #include "bfd.h" | |
21 | #include "sysdep.h" | |
22 | #include "bfdlink.h" | |
23 | #include "libbfd.h" | |
24 | #define ARCH_SIZE 0 | |
3b3753b8 | 25 | #include "elf-bfd.h" |
ede4eed4 KR |
26 | |
27 | boolean | |
28 | _bfd_elf_create_got_section (abfd, info) | |
29 | bfd *abfd; | |
30 | struct bfd_link_info *info; | |
31 | { | |
32 | flagword flags; | |
33 | register asection *s; | |
34 | struct elf_link_hash_entry *h; | |
35 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
36 | ||
37 | /* This function may be called more than once. */ | |
38 | if (bfd_get_section_by_name (abfd, ".got") != NULL) | |
39 | return true; | |
40 | ||
41 | flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
42 | ||
43 | s = bfd_make_section (abfd, ".got"); | |
44 | if (s == NULL | |
45 | || !bfd_set_section_flags (abfd, s, flags) | |
46 | || !bfd_set_section_alignment (abfd, s, 2)) | |
47 | return false; | |
48 | ||
49 | if (bed->want_got_plt) | |
50 | { | |
51 | s = bfd_make_section (abfd, ".got.plt"); | |
52 | if (s == NULL | |
53 | || !bfd_set_section_flags (abfd, s, flags) | |
54 | || !bfd_set_section_alignment (abfd, s, 2)) | |
55 | return false; | |
56 | } | |
57 | ||
58 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
59 | (or .got.plt) section. We don't do this in the linker script | |
60 | because we don't want to define the symbol if we are not creating | |
61 | a global offset table. */ | |
62 | h = NULL; | |
63 | if (!(_bfd_generic_link_add_one_symbol | |
64 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0, | |
65 | (const char *) NULL, false, get_elf_backend_data (abfd)->collect, | |
66 | (struct bfd_link_hash_entry **) &h))) | |
67 | return false; | |
68 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
69 | h->type = STT_OBJECT; | |
70 | ||
71 | if (info->shared | |
72 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
73 | return false; | |
74 | ||
75 | /* The first three global offset table entries are reserved. */ | |
76 | s->_raw_size += 3 * 4; | |
77 | ||
78 | return true; | |
79 | } | |
3b3753b8 | 80 | \f |
ede4eed4 KR |
81 | |
82 | /* Create dynamic sections when linking against a dynamic object. */ | |
83 | ||
84 | boolean | |
85 | _bfd_elf_create_dynamic_sections (abfd, info) | |
86 | bfd *abfd; | |
87 | struct bfd_link_info *info; | |
88 | { | |
89 | flagword flags; | |
90 | register asection *s; | |
91 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
92 | ||
93 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and | |
94 | .rel[a].bss sections. */ | |
95 | ||
96 | flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
97 | ||
98 | s = bfd_make_section (abfd, ".plt"); | |
99 | if (s == NULL | |
100 | || ! bfd_set_section_flags (abfd, s, | |
101 | (flags | SEC_CODE | |
102 | | (bed->plt_readonly ? SEC_READONLY : 0))) | |
103 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
104 | return false; | |
105 | ||
106 | if (bed->want_plt_sym) | |
107 | { | |
108 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
109 | .plt section. */ | |
110 | struct elf_link_hash_entry *h = NULL; | |
111 | if (! (_bfd_generic_link_add_one_symbol | |
112 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, | |
113 | (bfd_vma) 0, (const char *) NULL, false, | |
114 | get_elf_backend_data (abfd)->collect, | |
115 | (struct bfd_link_hash_entry **) &h))) | |
116 | return false; | |
117 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
118 | h->type = STT_OBJECT; | |
119 | ||
120 | if (info->shared | |
121 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
122 | return false; | |
123 | } | |
124 | ||
125 | s = bfd_make_section (abfd, bed->use_rela_p ? ".rela.plt" : ".rel.plt"); | |
126 | if (s == NULL | |
127 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
128 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
129 | return false; | |
130 | ||
131 | if (! _bfd_elf_create_got_section (abfd, info)) | |
132 | return false; | |
133 | ||
134 | /* The .dynbss section is a place to put symbols which are defined | |
135 | by dynamic objects, are referenced by regular objects, and are | |
136 | not functions. We must allocate space for them in the process | |
137 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
138 | initialize them at run time. The linker script puts the .dynbss | |
139 | section into the .bss section of the final image. */ | |
140 | s = bfd_make_section (abfd, ".dynbss"); | |
141 | if (s == NULL | |
142 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC)) | |
143 | return false; | |
144 | ||
145 | /* The .rel[a].bss section holds copy relocs. This section is not | |
146 | normally needed. We need to create it here, though, so that the | |
147 | linker will map it to an output section. We can't just create it | |
148 | only if we need it, because we will not know whether we need it | |
149 | until we have seen all the input files, and the first time the | |
150 | main linker code calls BFD after examining all the input files | |
151 | (size_dynamic_sections) the input sections have already been | |
152 | mapped to the output sections. If the section turns out not to | |
153 | be needed, we can discard it later. We will never need this | |
154 | section when generating a shared object, since they do not use | |
155 | copy relocs. */ | |
156 | if (! info->shared) | |
157 | { | |
158 | s = bfd_make_section (abfd, bed->use_rela_p ? ".rela.bss" : ".rel.bss"); | |
159 | if (s == NULL | |
160 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
161 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
162 | return false; | |
163 | } | |
164 | ||
165 | return true; | |
166 | } | |
3b3753b8 | 167 | \f |
ede4eed4 KR |
168 | |
169 | /* Record a new dynamic symbol. We record the dynamic symbols as we | |
170 | read the input files, since we need to have a list of all of them | |
171 | before we can determine the final sizes of the output sections. | |
172 | Note that we may actually call this function even though we are not | |
173 | going to output any dynamic symbols; in some cases we know that a | |
174 | symbol should be in the dynamic symbol table, but only if there is | |
175 | one. */ | |
176 | ||
177 | boolean | |
178 | _bfd_elf_link_record_dynamic_symbol (info, h) | |
179 | struct bfd_link_info *info; | |
180 | struct elf_link_hash_entry *h; | |
181 | { | |
182 | if (h->dynindx == -1) | |
183 | { | |
184 | struct bfd_strtab_hash *dynstr; | |
185 | ||
186 | h->dynindx = elf_hash_table (info)->dynsymcount; | |
187 | ++elf_hash_table (info)->dynsymcount; | |
188 | ||
189 | dynstr = elf_hash_table (info)->dynstr; | |
190 | if (dynstr == NULL) | |
191 | { | |
192 | /* Create a strtab to hold the dynamic symbol names. */ | |
193 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init (); | |
194 | if (dynstr == NULL) | |
195 | return false; | |
196 | } | |
197 | ||
198 | h->dynstr_index = ((unsigned long) | |
199 | _bfd_stringtab_add (dynstr, h->root.root.string, | |
200 | true, false)); | |
201 | if (h->dynstr_index == (unsigned long) -1) | |
202 | return false; | |
203 | } | |
204 | ||
205 | return true; | |
206 | } | |
3b3753b8 MM |
207 | \f |
208 | /* Create a special linker section, or return a pointer to a linker section already created */ | |
209 | ||
210 | elf_linker_section_t * | |
211 | _bfd_elf_create_linker_section (abfd, info, which, defaults) | |
212 | bfd *abfd; | |
213 | struct bfd_link_info *info; | |
214 | enum elf_linker_section_enum which; | |
215 | elf_linker_section_t *defaults; | |
216 | { | |
217 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
218 | elf_linker_section_t *lsect; | |
219 | ||
220 | /* Record the first bfd section that needs the special section */ | |
221 | if (!dynobj) | |
222 | dynobj = elf_hash_table (info)->dynobj = abfd; | |
223 | ||
224 | /* If this is the first time, create the section */ | |
225 | lsect = elf_linker_section (dynobj, which); | |
226 | if (!lsect) | |
227 | { | |
228 | asection *s; | |
229 | static elf_linker_section_t zero_section; | |
230 | ||
231 | lsect = (elf_linker_section_t *) | |
232 | bfd_alloc (dynobj, sizeof (elf_linker_section_t)); | |
233 | ||
234 | *lsect = *defaults; | |
235 | elf_linker_section (dynobj, which) = lsect; | |
236 | lsect->which = which; | |
237 | lsect->hole_written_p = false; | |
238 | ||
239 | /* See if the sections already exist */ | |
240 | lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name); | |
241 | if (!s) | |
242 | { | |
243 | lsect->section = s = bfd_make_section (dynobj, lsect->name); | |
244 | ||
245 | if (s == NULL) | |
246 | return (elf_linker_section_t *)0; | |
247 | ||
248 | bfd_set_section_flags (dynobj, s, defaults->flags); | |
249 | bfd_set_section_alignment (dynobj, s, lsect->alignment); | |
250 | } | |
251 | else if (bfd_get_section_alignment (dynobj, s) < lsect->alignment) | |
252 | bfd_set_section_alignment (dynobj, s, lsect->alignment); | |
253 | ||
254 | s->_raw_size = align_power (s->_raw_size, lsect->alignment); | |
255 | ||
256 | /* Is there a hole we have to provide? If so check whether the segment is | |
257 | too big already */ | |
258 | if (lsect->hole_size) | |
259 | { | |
260 | lsect->hole_offset = s->_raw_size; | |
261 | s->_raw_size += lsect->hole_size; | |
262 | if (lsect->hole_offset > lsect->max_hole_offset) | |
263 | { | |
264 | (*_bfd_error_handler) ("%s: Section %s is already to large to put hole of %ld bytes in", | |
265 | bfd_get_filename (abfd), | |
266 | lsect->name, | |
267 | (long)lsect->hole_size); | |
268 | ||
269 | bfd_set_error (bfd_error_bad_value); | |
270 | return (elf_linker_section_t *)0; | |
271 | } | |
272 | } | |
273 | ||
274 | #ifdef DEBUG | |
275 | fprintf (stderr, "Creating section %s, current size = %ld\n", | |
276 | lsect->name, (long)s->_raw_size); | |
277 | #endif | |
278 | ||
279 | if (lsect->sym_name) | |
280 | { | |
281 | struct elf_link_hash_entry *h = NULL; | |
282 | #ifdef DEBUG | |
283 | fprintf (stderr, "Adding %s to section %s\n", | |
284 | lsect->sym_name, | |
285 | lsect->name); | |
286 | #endif | |
287 | if (!(_bfd_generic_link_add_one_symbol (info, | |
288 | abfd, | |
289 | lsect->sym_name, | |
290 | BSF_GLOBAL, | |
291 | s, | |
292 | ((lsect->hole_size) | |
293 | ? s->_raw_size - lsect->hole_size + lsect->sym_offset | |
294 | : lsect->sym_offset), | |
295 | (const char *) NULL, | |
296 | false, | |
297 | get_elf_backend_data (abfd)->collect, | |
298 | (struct bfd_link_hash_entry **) &h))) | |
299 | return (elf_linker_section_t *)0; | |
300 | ||
301 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_DYNAMIC; | |
302 | h->type = STT_OBJECT; | |
303 | lsect->sym_hash = h; | |
304 | ||
305 | if (info->shared | |
306 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
307 | return (elf_linker_section_t *)0; | |
308 | } | |
309 | } | |
310 | ||
311 | /* Find the related sections if they have been created */ | |
312 | if (lsect->bss_name && !lsect->bss_section) | |
313 | lsect->bss_section = bfd_get_section_by_name (dynobj, lsect->bss_name); | |
314 | ||
315 | if (lsect->rel_name && !lsect->rel_section) | |
316 | lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name); | |
317 | ||
318 | return lsect; | |
319 | } | |
320 | ||
321 | \f | |
322 | /* Find a linker generated pointer with a given addend and type. */ | |
323 | ||
324 | elf_linker_section_pointers_t * | |
325 | _bfd_elf_find_pointer_linker_section (linker_pointers, addend, which) | |
326 | elf_linker_section_pointers_t *linker_pointers; | |
327 | bfd_signed_vma addend; | |
328 | elf_linker_section_enum_t which; | |
329 | { | |
330 | for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next) | |
331 | { | |
332 | if (which == linker_pointers->which && addend == linker_pointers->addend) | |
333 | return linker_pointers; | |
334 | } | |
335 | ||
336 | return (elf_linker_section_pointers_t *)0; | |
337 | } | |
338 | ||
339 | \f | |
340 | /* Make the .rela section corresponding to the generated linker section. */ | |
341 | ||
342 | boolean | |
343 | _bfd_elf_make_linker_section_rela (dynobj, lsect, alignment) | |
344 | bfd *dynobj; | |
345 | elf_linker_section_t *lsect; | |
346 | int alignment; | |
347 | { | |
348 | if (lsect->rel_section) | |
349 | return true; | |
350 | ||
351 | lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name); | |
352 | if (lsect->rel_section == NULL) | |
353 | { | |
354 | lsect->rel_section = bfd_make_section (dynobj, lsect->rel_name); | |
355 | if (lsect->rel_section == NULL | |
356 | || ! bfd_set_section_flags (dynobj, | |
357 | lsect->rel_section, | |
358 | (SEC_ALLOC | |
359 | | SEC_LOAD | |
360 | | SEC_HAS_CONTENTS | |
361 | | SEC_IN_MEMORY | |
362 | | SEC_READONLY)) | |
363 | || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment)) | |
364 | return false; | |
365 | } | |
366 | ||
367 | return true; | |
368 | } | |
369 |