Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
305c7206 | 2 | Copyright 1995, 96, 97, 98, 99, 2000 Free Software Foundation, Inc. |
252b5132 RH |
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 | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
19 | ||
20 | #include "bfd.h" | |
21 | #include "sysdep.h" | |
22 | #include "bfdlink.h" | |
23 | #include "libbfd.h" | |
24 | #define ARCH_SIZE 0 | |
25 | #include "elf-bfd.h" | |
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 | int ptralign; | |
37 | ||
38 | /* This function may be called more than once. */ | |
39 | if (bfd_get_section_by_name (abfd, ".got") != NULL) | |
40 | return true; | |
41 | ||
42 | switch (bed->s->arch_size) | |
43 | { | |
bb0deeff AO |
44 | case 32: |
45 | ptralign = 2; | |
46 | break; | |
47 | ||
48 | case 64: | |
49 | ptralign = 3; | |
50 | break; | |
51 | ||
52 | default: | |
53 | bfd_set_error (bfd_error_bad_value); | |
54 | return false; | |
252b5132 RH |
55 | } |
56 | ||
57 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
58 | | SEC_LINKER_CREATED); | |
59 | ||
60 | s = bfd_make_section (abfd, ".got"); | |
61 | if (s == NULL | |
62 | || !bfd_set_section_flags (abfd, s, flags) | |
63 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
64 | return false; | |
65 | ||
66 | if (bed->want_got_plt) | |
67 | { | |
68 | s = bfd_make_section (abfd, ".got.plt"); | |
69 | if (s == NULL | |
70 | || !bfd_set_section_flags (abfd, s, flags) | |
71 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
72 | return false; | |
73 | } | |
74 | ||
75 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
76 | (or .got.plt) section. We don't do this in the linker script | |
77 | because we don't want to define the symbol if we are not creating | |
78 | a global offset table. */ | |
79 | h = NULL; | |
80 | if (!(_bfd_generic_link_add_one_symbol | |
81 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, | |
82 | bed->got_symbol_offset, (const char *) NULL, false, | |
83 | bed->collect, (struct bfd_link_hash_entry **) &h))) | |
84 | return false; | |
85 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
86 | h->type = STT_OBJECT; | |
87 | ||
88 | if (info->shared | |
89 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
90 | return false; | |
91 | ||
92 | elf_hash_table (info)->hgot = h; | |
93 | ||
94 | /* The first bit of the global offset table is the header. */ | |
95 | s->_raw_size += bed->got_header_size + bed->got_symbol_offset; | |
96 | ||
97 | return true; | |
98 | } | |
99 | \f | |
252b5132 RH |
100 | /* Create dynamic sections when linking against a dynamic object. */ |
101 | ||
102 | boolean | |
103 | _bfd_elf_create_dynamic_sections (abfd, info) | |
104 | bfd *abfd; | |
105 | struct bfd_link_info *info; | |
106 | { | |
107 | flagword flags, pltflags; | |
108 | register asection *s; | |
109 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7442e600 | 110 | int ptralign = 0; |
252b5132 RH |
111 | |
112 | switch (bed->s->arch_size) | |
113 | { | |
bb0deeff AO |
114 | case 32: |
115 | ptralign = 2; | |
116 | break; | |
117 | ||
118 | case 64: | |
119 | ptralign = 3; | |
120 | break; | |
121 | ||
122 | default: | |
123 | bfd_set_error (bfd_error_bad_value); | |
124 | return false; | |
252b5132 RH |
125 | } |
126 | ||
127 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and | |
128 | .rel[a].bss sections. */ | |
129 | ||
130 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
131 | | SEC_LINKER_CREATED); | |
132 | ||
133 | pltflags = flags; | |
134 | pltflags |= SEC_CODE; | |
135 | if (bed->plt_not_loaded) | |
136 | pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS); | |
137 | if (bed->plt_readonly) | |
138 | pltflags |= SEC_READONLY; | |
139 | ||
140 | s = bfd_make_section (abfd, ".plt"); | |
141 | if (s == NULL | |
142 | || ! bfd_set_section_flags (abfd, s, pltflags) | |
143 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) | |
144 | return false; | |
145 | ||
146 | if (bed->want_plt_sym) | |
147 | { | |
148 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
149 | .plt section. */ | |
150 | struct elf_link_hash_entry *h = NULL; | |
151 | if (! (_bfd_generic_link_add_one_symbol | |
152 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, | |
153 | (bfd_vma) 0, (const char *) NULL, false, | |
154 | get_elf_backend_data (abfd)->collect, | |
155 | (struct bfd_link_hash_entry **) &h))) | |
156 | return false; | |
157 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
158 | h->type = STT_OBJECT; | |
159 | ||
160 | if (info->shared | |
161 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
162 | return false; | |
163 | } | |
164 | ||
3e932841 | 165 | s = bfd_make_section (abfd, |
bf572ba0 | 166 | bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"); |
252b5132 RH |
167 | if (s == NULL |
168 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
169 | || ! bfd_set_section_alignment (abfd, s, ptralign)) | |
170 | return false; | |
171 | ||
172 | if (! _bfd_elf_create_got_section (abfd, info)) | |
173 | return false; | |
174 | ||
3018b441 RH |
175 | if (bed->want_dynbss) |
176 | { | |
177 | /* The .dynbss section is a place to put symbols which are defined | |
178 | by dynamic objects, are referenced by regular objects, and are | |
179 | not functions. We must allocate space for them in the process | |
180 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
181 | initialize them at run time. The linker script puts the .dynbss | |
182 | section into the .bss section of the final image. */ | |
183 | s = bfd_make_section (abfd, ".dynbss"); | |
184 | if (s == NULL | |
185 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC)) | |
186 | return false; | |
252b5132 | 187 | |
3018b441 | 188 | /* The .rel[a].bss section holds copy relocs. This section is not |
252b5132 RH |
189 | normally needed. We need to create it here, though, so that the |
190 | linker will map it to an output section. We can't just create it | |
191 | only if we need it, because we will not know whether we need it | |
192 | until we have seen all the input files, and the first time the | |
193 | main linker code calls BFD after examining all the input files | |
194 | (size_dynamic_sections) the input sections have already been | |
195 | mapped to the output sections. If the section turns out not to | |
196 | be needed, we can discard it later. We will never need this | |
197 | section when generating a shared object, since they do not use | |
198 | copy relocs. */ | |
3018b441 RH |
199 | if (! info->shared) |
200 | { | |
3e932841 KH |
201 | s = bfd_make_section (abfd, |
202 | (bed->default_use_rela_p | |
203 | ? ".rela.bss" : ".rel.bss")); | |
3018b441 RH |
204 | if (s == NULL |
205 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
206 | || ! bfd_set_section_alignment (abfd, s, ptralign)) | |
207 | return false; | |
208 | } | |
252b5132 RH |
209 | } |
210 | ||
211 | return true; | |
212 | } | |
213 | \f | |
252b5132 RH |
214 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
215 | read the input files, since we need to have a list of all of them | |
216 | before we can determine the final sizes of the output sections. | |
217 | Note that we may actually call this function even though we are not | |
218 | going to output any dynamic symbols; in some cases we know that a | |
219 | symbol should be in the dynamic symbol table, but only if there is | |
220 | one. */ | |
221 | ||
222 | boolean | |
223 | _bfd_elf_link_record_dynamic_symbol (info, h) | |
224 | struct bfd_link_info *info; | |
225 | struct elf_link_hash_entry *h; | |
226 | { | |
227 | if (h->dynindx == -1) | |
228 | { | |
229 | struct bfd_strtab_hash *dynstr; | |
230 | char *p, *alc; | |
231 | const char *name; | |
232 | boolean copy; | |
233 | bfd_size_type indx; | |
234 | ||
7a13edea NC |
235 | /* XXX: The ABI draft says the linker must turn hidden and |
236 | internal symbols into STB_LOCAL symbols when producing the | |
237 | DSO. However, if ld.so honors st_other in the dynamic table, | |
238 | this would not be necessary. */ | |
239 | switch (ELF_ST_VISIBILITY (h->other)) | |
240 | { | |
241 | case STV_INTERNAL: | |
242 | case STV_HIDDEN: | |
9d6eee78 L |
243 | if (h->root.type != bfd_link_hash_undefined |
244 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 L |
245 | { |
246 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; | |
247 | return true; | |
7a13edea | 248 | } |
0444bdd4 | 249 | |
7a13edea NC |
250 | default: |
251 | break; | |
252 | } | |
253 | ||
252b5132 RH |
254 | h->dynindx = elf_hash_table (info)->dynsymcount; |
255 | ++elf_hash_table (info)->dynsymcount; | |
256 | ||
257 | dynstr = elf_hash_table (info)->dynstr; | |
258 | if (dynstr == NULL) | |
259 | { | |
260 | /* Create a strtab to hold the dynamic symbol names. */ | |
261 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init (); | |
262 | if (dynstr == NULL) | |
263 | return false; | |
264 | } | |
265 | ||
266 | /* We don't put any version information in the dynamic string | |
267 | table. */ | |
268 | name = h->root.root.string; | |
269 | p = strchr (name, ELF_VER_CHR); | |
270 | if (p == NULL) | |
271 | { | |
272 | alc = NULL; | |
273 | copy = false; | |
274 | } | |
275 | else | |
276 | { | |
277 | alc = bfd_malloc (p - name + 1); | |
278 | if (alc == NULL) | |
279 | return false; | |
280 | strncpy (alc, name, p - name); | |
281 | alc[p - name] = '\0'; | |
282 | name = alc; | |
283 | copy = true; | |
284 | } | |
285 | ||
286 | indx = _bfd_stringtab_add (dynstr, name, true, copy); | |
287 | ||
288 | if (alc != NULL) | |
289 | free (alc); | |
290 | ||
291 | if (indx == (bfd_size_type) -1) | |
292 | return false; | |
293 | h->dynstr_index = indx; | |
294 | } | |
295 | ||
296 | return true; | |
297 | } | |
42751cf3 | 298 | |
30b30c21 | 299 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 300 | |
30b30c21 RH |
301 | long |
302 | _bfd_elf_link_lookup_local_dynindx (info, input_bfd, input_indx) | |
303 | struct bfd_link_info *info; | |
304 | bfd *input_bfd; | |
305 | long input_indx; | |
306 | { | |
307 | struct elf_link_local_dynamic_entry *e; | |
308 | ||
309 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
310 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
311 | return e->dynindx; | |
312 | return -1; | |
313 | } | |
314 | ||
315 | /* This function is used to renumber the dynamic symbols, if some of | |
316 | them are removed because they are marked as local. This is called | |
317 | via elf_link_hash_traverse. */ | |
318 | ||
319 | static boolean elf_link_renumber_hash_table_dynsyms | |
320 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
321 | ||
322 | static boolean | |
323 | elf_link_renumber_hash_table_dynsyms (h, data) | |
42751cf3 | 324 | struct elf_link_hash_entry *h; |
30b30c21 | 325 | PTR data; |
42751cf3 | 326 | { |
30b30c21 RH |
327 | size_t *count = (size_t *) data; |
328 | ||
42751cf3 | 329 | if (h->dynindx != -1) |
30b30c21 RH |
330 | h->dynindx = ++(*count); |
331 | ||
42751cf3 MM |
332 | return true; |
333 | } | |
30b30c21 RH |
334 | |
335 | /* Assign dynsym indicies. In a shared library we generate a section | |
336 | symbol for each output section, which come first. Next come all of | |
337 | the back-end allocated local dynamic syms, followed by the rest of | |
338 | the global symbols. */ | |
339 | ||
340 | unsigned long | |
341 | _bfd_elf_link_renumber_dynsyms (output_bfd, info) | |
342 | bfd *output_bfd; | |
343 | struct bfd_link_info *info; | |
344 | { | |
345 | unsigned long dynsymcount = 0; | |
346 | ||
347 | if (info->shared) | |
348 | { | |
349 | asection *p; | |
350 | for (p = output_bfd->sections; p ; p = p->next) | |
351 | elf_section_data (p)->dynindx = ++dynsymcount; | |
352 | } | |
353 | ||
354 | if (elf_hash_table (info)->dynlocal) | |
355 | { | |
356 | struct elf_link_local_dynamic_entry *p; | |
357 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
358 | p->dynindx = ++dynsymcount; | |
359 | } | |
360 | ||
361 | elf_link_hash_traverse (elf_hash_table (info), | |
362 | elf_link_renumber_hash_table_dynsyms, | |
363 | &dynsymcount); | |
364 | ||
365 | /* There is an unused NULL entry at the head of the table which | |
366 | we must account for in our count. Unless there weren't any | |
367 | symbols, which means we'll have no table at all. */ | |
368 | if (dynsymcount != 0) | |
369 | ++dynsymcount; | |
370 | ||
371 | return elf_hash_table (info)->dynsymcount = dynsymcount; | |
372 | } | |
252b5132 | 373 | \f |
30b30c21 RH |
374 | /* Create a special linker section, or return a pointer to a linker |
375 | section already created */ | |
252b5132 RH |
376 | |
377 | elf_linker_section_t * | |
378 | _bfd_elf_create_linker_section (abfd, info, which, defaults) | |
379 | bfd *abfd; | |
380 | struct bfd_link_info *info; | |
381 | enum elf_linker_section_enum which; | |
382 | elf_linker_section_t *defaults; | |
383 | { | |
384 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
385 | elf_linker_section_t *lsect; | |
386 | ||
387 | /* Record the first bfd section that needs the special section */ | |
388 | if (!dynobj) | |
389 | dynobj = elf_hash_table (info)->dynobj = abfd; | |
390 | ||
391 | /* If this is the first time, create the section */ | |
392 | lsect = elf_linker_section (dynobj, which); | |
393 | if (!lsect) | |
394 | { | |
395 | asection *s; | |
396 | ||
397 | lsect = (elf_linker_section_t *) | |
398 | bfd_alloc (dynobj, sizeof (elf_linker_section_t)); | |
399 | ||
400 | *lsect = *defaults; | |
401 | elf_linker_section (dynobj, which) = lsect; | |
402 | lsect->which = which; | |
403 | lsect->hole_written_p = false; | |
404 | ||
405 | /* See if the sections already exist */ | |
406 | lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name); | |
407 | if (!s || (s->flags & defaults->flags) != defaults->flags) | |
408 | { | |
409 | lsect->section = s = bfd_make_section_anyway (dynobj, lsect->name); | |
410 | ||
411 | if (s == NULL) | |
412 | return (elf_linker_section_t *)0; | |
413 | ||
414 | bfd_set_section_flags (dynobj, s, defaults->flags); | |
415 | bfd_set_section_alignment (dynobj, s, lsect->alignment); | |
416 | } | |
417 | else if (bfd_get_section_alignment (dynobj, s) < lsect->alignment) | |
418 | bfd_set_section_alignment (dynobj, s, lsect->alignment); | |
419 | ||
420 | s->_raw_size = align_power (s->_raw_size, lsect->alignment); | |
421 | ||
422 | /* Is there a hole we have to provide? If so check whether the segment is | |
423 | too big already */ | |
424 | if (lsect->hole_size) | |
425 | { | |
426 | lsect->hole_offset = s->_raw_size; | |
427 | s->_raw_size += lsect->hole_size; | |
428 | if (lsect->hole_offset > lsect->max_hole_offset) | |
429 | { | |
430 | (*_bfd_error_handler) (_("%s: Section %s is already to large to put hole of %ld bytes in"), | |
431 | bfd_get_filename (abfd), | |
432 | lsect->name, | |
433 | (long)lsect->hole_size); | |
434 | ||
435 | bfd_set_error (bfd_error_bad_value); | |
436 | return (elf_linker_section_t *)0; | |
437 | } | |
438 | } | |
439 | ||
440 | #ifdef DEBUG | |
441 | fprintf (stderr, "Creating section %s, current size = %ld\n", | |
442 | lsect->name, (long)s->_raw_size); | |
443 | #endif | |
444 | ||
445 | if (lsect->sym_name) | |
446 | { | |
447 | struct elf_link_hash_entry *h = NULL; | |
448 | #ifdef DEBUG | |
449 | fprintf (stderr, "Adding %s to section %s\n", | |
450 | lsect->sym_name, | |
451 | lsect->name); | |
452 | #endif | |
453 | h = (struct elf_link_hash_entry *) | |
454 | bfd_link_hash_lookup (info->hash, lsect->sym_name, false, false, false); | |
455 | ||
456 | if ((h == NULL || h->root.type == bfd_link_hash_undefined) | |
457 | && !(_bfd_generic_link_add_one_symbol (info, | |
458 | abfd, | |
459 | lsect->sym_name, | |
460 | BSF_GLOBAL, | |
461 | s, | |
462 | ((lsect->hole_size) | |
463 | ? s->_raw_size - lsect->hole_size + lsect->sym_offset | |
464 | : lsect->sym_offset), | |
465 | (const char *) NULL, | |
466 | false, | |
467 | get_elf_backend_data (abfd)->collect, | |
468 | (struct bfd_link_hash_entry **) &h))) | |
469 | return (elf_linker_section_t *)0; | |
470 | ||
471 | if ((defaults->which != LINKER_SECTION_SDATA) | |
472 | && (defaults->which != LINKER_SECTION_SDATA2)) | |
473 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_DYNAMIC; | |
474 | ||
475 | h->type = STT_OBJECT; | |
476 | lsect->sym_hash = h; | |
477 | ||
478 | if (info->shared | |
479 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
480 | return (elf_linker_section_t *)0; | |
481 | } | |
482 | } | |
483 | ||
484 | #if 0 | |
485 | /* This does not make sense. The sections which may exist in the | |
486 | object file have nothing to do with the sections we want to | |
487 | create. */ | |
488 | ||
489 | /* Find the related sections if they have been created */ | |
490 | if (lsect->bss_name && !lsect->bss_section) | |
491 | lsect->bss_section = bfd_get_section_by_name (dynobj, lsect->bss_name); | |
492 | ||
493 | if (lsect->rel_name && !lsect->rel_section) | |
494 | lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name); | |
495 | #endif | |
496 | ||
497 | return lsect; | |
498 | } | |
252b5132 RH |
499 | \f |
500 | /* Find a linker generated pointer with a given addend and type. */ | |
501 | ||
502 | elf_linker_section_pointers_t * | |
503 | _bfd_elf_find_pointer_linker_section (linker_pointers, addend, which) | |
504 | elf_linker_section_pointers_t *linker_pointers; | |
505 | bfd_signed_vma addend; | |
506 | elf_linker_section_enum_t which; | |
507 | { | |
508 | for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next) | |
509 | { | |
510 | if (which == linker_pointers->which && addend == linker_pointers->addend) | |
511 | return linker_pointers; | |
512 | } | |
513 | ||
514 | return (elf_linker_section_pointers_t *)0; | |
515 | } | |
252b5132 RH |
516 | \f |
517 | /* Make the .rela section corresponding to the generated linker section. */ | |
518 | ||
519 | boolean | |
520 | _bfd_elf_make_linker_section_rela (dynobj, lsect, alignment) | |
521 | bfd *dynobj; | |
522 | elf_linker_section_t *lsect; | |
523 | int alignment; | |
524 | { | |
525 | if (lsect->rel_section) | |
526 | return true; | |
527 | ||
528 | lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name); | |
529 | if (lsect->rel_section == NULL) | |
530 | { | |
531 | lsect->rel_section = bfd_make_section (dynobj, lsect->rel_name); | |
532 | if (lsect->rel_section == NULL | |
533 | || ! bfd_set_section_flags (dynobj, | |
534 | lsect->rel_section, | |
535 | (SEC_ALLOC | |
536 | | SEC_LOAD | |
537 | | SEC_HAS_CONTENTS | |
538 | | SEC_IN_MEMORY | |
539 | | SEC_LINKER_CREATED | |
540 | | SEC_READONLY)) | |
541 | || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment)) | |
542 | return false; | |
543 | } | |
544 | ||
545 | return true; | |
546 | } |