* elfxx-mips.c (_bfd_mips_elf_merge_private_bfd_data): Improve
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27
28 bfd_boolean
29 _bfd_elf_create_got_section (abfd, info)
30 bfd *abfd;
31 struct bfd_link_info *info;
32 {
33 flagword flags;
34 asection *s;
35 struct elf_link_hash_entry *h;
36 struct bfd_link_hash_entry *bh;
37 struct elf_backend_data *bed = get_elf_backend_data (abfd);
38 int ptralign;
39
40 /* This function may be called more than once. */
41 s = bfd_get_section_by_name (abfd, ".got");
42 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
43 return TRUE;
44
45 switch (bed->s->arch_size)
46 {
47 case 32:
48 ptralign = 2;
49 break;
50
51 case 64:
52 ptralign = 3;
53 break;
54
55 default:
56 bfd_set_error (bfd_error_bad_value);
57 return FALSE;
58 }
59
60 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
61 | SEC_LINKER_CREATED);
62
63 s = bfd_make_section (abfd, ".got");
64 if (s == NULL
65 || !bfd_set_section_flags (abfd, s, flags)
66 || !bfd_set_section_alignment (abfd, s, ptralign))
67 return FALSE;
68
69 if (bed->want_got_plt)
70 {
71 s = bfd_make_section (abfd, ".got.plt");
72 if (s == NULL
73 || !bfd_set_section_flags (abfd, s, flags)
74 || !bfd_set_section_alignment (abfd, s, ptralign))
75 return FALSE;
76 }
77
78 if (bed->want_got_sym)
79 {
80 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
81 (or .got.plt) section. We don't do this in the linker script
82 because we don't want to define the symbol if we are not creating
83 a global offset table. */
84 bh = NULL;
85 if (!(_bfd_generic_link_add_one_symbol
86 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
87 bed->got_symbol_offset, (const char *) NULL, FALSE,
88 bed->collect, &bh)))
89 return FALSE;
90 h = (struct elf_link_hash_entry *) bh;
91 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
92 h->type = STT_OBJECT;
93
94 if (info->shared
95 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
96 return FALSE;
97
98 elf_hash_table (info)->hgot = h;
99 }
100
101 /* The first bit of the global offset table is the header. */
102 s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
103
104 return TRUE;
105 }
106 \f
107 /* Create dynamic sections when linking against a dynamic object. */
108
109 bfd_boolean
110 _bfd_elf_create_dynamic_sections (abfd, info)
111 bfd *abfd;
112 struct bfd_link_info *info;
113 {
114 flagword flags, pltflags;
115 asection *s;
116 struct elf_backend_data *bed = get_elf_backend_data (abfd);
117 int ptralign;
118
119 switch (bed->s->arch_size)
120 {
121 case 32:
122 ptralign = 2;
123 break;
124
125 case 64:
126 ptralign = 3;
127 break;
128
129 default:
130 bfd_set_error (bfd_error_bad_value);
131 return FALSE;
132 }
133
134 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
135 .rel[a].bss sections. */
136
137 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
138 | SEC_LINKER_CREATED);
139
140 pltflags = flags;
141 pltflags |= SEC_CODE;
142 if (bed->plt_not_loaded)
143 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
144 if (bed->plt_readonly)
145 pltflags |= SEC_READONLY;
146
147 s = bfd_make_section (abfd, ".plt");
148 if (s == NULL
149 || ! bfd_set_section_flags (abfd, s, pltflags)
150 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
151 return FALSE;
152
153 if (bed->want_plt_sym)
154 {
155 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
156 .plt section. */
157 struct elf_link_hash_entry *h;
158 struct bfd_link_hash_entry *bh = NULL;
159
160 if (! (_bfd_generic_link_add_one_symbol
161 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
162 (bfd_vma) 0, (const char *) NULL, FALSE,
163 get_elf_backend_data (abfd)->collect, &bh)))
164 return FALSE;
165 h = (struct elf_link_hash_entry *) bh;
166 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
167 h->type = STT_OBJECT;
168
169 if (info->shared
170 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
171 return FALSE;
172 }
173
174 s = bfd_make_section (abfd,
175 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
176 if (s == NULL
177 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
178 || ! bfd_set_section_alignment (abfd, s, ptralign))
179 return FALSE;
180
181 if (! _bfd_elf_create_got_section (abfd, info))
182 return FALSE;
183
184 if (bed->want_dynbss)
185 {
186 /* The .dynbss section is a place to put symbols which are defined
187 by dynamic objects, are referenced by regular objects, and are
188 not functions. We must allocate space for them in the process
189 image and use a R_*_COPY reloc to tell the dynamic linker to
190 initialize them at run time. The linker script puts the .dynbss
191 section into the .bss section of the final image. */
192 s = bfd_make_section (abfd, ".dynbss");
193 if (s == NULL
194 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
195 return FALSE;
196
197 /* The .rel[a].bss section holds copy relocs. This section is not
198 normally needed. We need to create it here, though, so that the
199 linker will map it to an output section. We can't just create it
200 only if we need it, because we will not know whether we need it
201 until we have seen all the input files, and the first time the
202 main linker code calls BFD after examining all the input files
203 (size_dynamic_sections) the input sections have already been
204 mapped to the output sections. If the section turns out not to
205 be needed, we can discard it later. We will never need this
206 section when generating a shared object, since they do not use
207 copy relocs. */
208 if (! info->shared)
209 {
210 s = bfd_make_section (abfd,
211 (bed->default_use_rela_p
212 ? ".rela.bss" : ".rel.bss"));
213 if (s == NULL
214 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
215 || ! bfd_set_section_alignment (abfd, s, ptralign))
216 return FALSE;
217 }
218 }
219
220 return TRUE;
221 }
222 \f
223 /* Record a new dynamic symbol. We record the dynamic symbols as we
224 read the input files, since we need to have a list of all of them
225 before we can determine the final sizes of the output sections.
226 Note that we may actually call this function even though we are not
227 going to output any dynamic symbols; in some cases we know that a
228 symbol should be in the dynamic symbol table, but only if there is
229 one. */
230
231 bfd_boolean
232 _bfd_elf_link_record_dynamic_symbol (info, h)
233 struct bfd_link_info *info;
234 struct elf_link_hash_entry *h;
235 {
236 if (h->dynindx == -1)
237 {
238 struct elf_strtab_hash *dynstr;
239 char *p, *alc;
240 const char *name;
241 bfd_boolean copy;
242 bfd_size_type indx;
243
244 /* XXX: The ABI draft says the linker must turn hidden and
245 internal symbols into STB_LOCAL symbols when producing the
246 DSO. However, if ld.so honors st_other in the dynamic table,
247 this would not be necessary. */
248 switch (ELF_ST_VISIBILITY (h->other))
249 {
250 case STV_INTERNAL:
251 case STV_HIDDEN:
252 if (h->root.type != bfd_link_hash_undefined
253 && h->root.type != bfd_link_hash_undefweak)
254 {
255 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
256 return TRUE;
257 }
258
259 default:
260 break;
261 }
262
263 h->dynindx = elf_hash_table (info)->dynsymcount;
264 ++elf_hash_table (info)->dynsymcount;
265
266 dynstr = elf_hash_table (info)->dynstr;
267 if (dynstr == NULL)
268 {
269 /* Create a strtab to hold the dynamic symbol names. */
270 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
271 if (dynstr == NULL)
272 return FALSE;
273 }
274
275 /* We don't put any version information in the dynamic string
276 table. */
277 name = h->root.root.string;
278 p = strchr (name, ELF_VER_CHR);
279 if (p == NULL)
280 {
281 alc = NULL;
282 copy = FALSE;
283 }
284 else
285 {
286 size_t len = p - name + 1;
287
288 alc = bfd_malloc ((bfd_size_type) len);
289 if (alc == NULL)
290 return FALSE;
291 memcpy (alc, name, len - 1);
292 alc[len - 1] = '\0';
293 name = alc;
294 copy = TRUE;
295 }
296
297 indx = _bfd_elf_strtab_add (dynstr, name, copy);
298
299 if (alc != NULL)
300 free (alc);
301
302 if (indx == (bfd_size_type) -1)
303 return FALSE;
304 h->dynstr_index = indx;
305 }
306
307 return TRUE;
308 }
309
310 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
311 success, and 2 on a failure caused by attempting to record a symbol
312 in a discarded section, eg. a discarded link-once section symbol. */
313
314 int
315 elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
316 struct bfd_link_info *info;
317 bfd *input_bfd;
318 long input_indx;
319 {
320 bfd_size_type amt;
321 struct elf_link_local_dynamic_entry *entry;
322 struct elf_link_hash_table *eht;
323 struct elf_strtab_hash *dynstr;
324 unsigned long dynstr_index;
325 char *name;
326 Elf_External_Sym_Shndx eshndx;
327 char esym[sizeof (Elf64_External_Sym)];
328
329 if (! is_elf_hash_table (info))
330 return 0;
331
332 /* See if the entry exists already. */
333 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
334 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
335 return 1;
336
337 amt = sizeof (*entry);
338 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
339 if (entry == NULL)
340 return 0;
341
342 /* Go find the symbol, so that we can find it's name. */
343 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
344 (size_t) 1, (size_t) input_indx,
345 &entry->isym, esym, &eshndx))
346 {
347 bfd_release (input_bfd, entry);
348 return 0;
349 }
350
351 if (entry->isym.st_shndx != SHN_UNDEF
352 && (entry->isym.st_shndx < SHN_LORESERVE
353 || entry->isym.st_shndx > SHN_HIRESERVE))
354 {
355 asection *s;
356
357 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
358 if (s == NULL || bfd_is_abs_section (s->output_section))
359 {
360 /* We can still bfd_release here as nothing has done another
361 bfd_alloc. We can't do this later in this function. */
362 bfd_release (input_bfd, entry);
363 return 2;
364 }
365 }
366
367 name = (bfd_elf_string_from_elf_section
368 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
369 entry->isym.st_name));
370
371 dynstr = elf_hash_table (info)->dynstr;
372 if (dynstr == NULL)
373 {
374 /* Create a strtab to hold the dynamic symbol names. */
375 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
376 if (dynstr == NULL)
377 return 0;
378 }
379
380 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
381 if (dynstr_index == (unsigned long) -1)
382 return 0;
383 entry->isym.st_name = dynstr_index;
384
385 eht = elf_hash_table (info);
386
387 entry->next = eht->dynlocal;
388 eht->dynlocal = entry;
389 entry->input_bfd = input_bfd;
390 entry->input_indx = input_indx;
391 eht->dynsymcount++;
392
393 /* Whatever binding the symbol had before, it's now local. */
394 entry->isym.st_info
395 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
396
397 /* The dynindx will be set at the end of size_dynamic_sections. */
398
399 return 1;
400 }
401
402 /* Return the dynindex of a local dynamic symbol. */
403
404 long
405 _bfd_elf_link_lookup_local_dynindx (info, input_bfd, input_indx)
406 struct bfd_link_info *info;
407 bfd *input_bfd;
408 long input_indx;
409 {
410 struct elf_link_local_dynamic_entry *e;
411
412 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
413 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
414 return e->dynindx;
415 return -1;
416 }
417
418 /* This function is used to renumber the dynamic symbols, if some of
419 them are removed because they are marked as local. This is called
420 via elf_link_hash_traverse. */
421
422 static bfd_boolean elf_link_renumber_hash_table_dynsyms
423 PARAMS ((struct elf_link_hash_entry *, PTR));
424
425 static bfd_boolean
426 elf_link_renumber_hash_table_dynsyms (h, data)
427 struct elf_link_hash_entry *h;
428 PTR data;
429 {
430 size_t *count = (size_t *) data;
431
432 if (h->root.type == bfd_link_hash_warning)
433 h = (struct elf_link_hash_entry *) h->root.u.i.link;
434
435 if (h->dynindx != -1)
436 h->dynindx = ++(*count);
437
438 return TRUE;
439 }
440
441 /* Assign dynsym indices. In a shared library we generate a section
442 symbol for each output section, which come first. Next come all of
443 the back-end allocated local dynamic syms, followed by the rest of
444 the global symbols. */
445
446 unsigned long
447 _bfd_elf_link_renumber_dynsyms (output_bfd, info)
448 bfd *output_bfd;
449 struct bfd_link_info *info;
450 {
451 unsigned long dynsymcount = 0;
452
453 if (info->shared)
454 {
455 asection *p;
456 for (p = output_bfd->sections; p ; p = p->next)
457 if ((p->flags & SEC_EXCLUDE) == 0)
458 elf_section_data (p)->dynindx = ++dynsymcount;
459 }
460
461 if (elf_hash_table (info)->dynlocal)
462 {
463 struct elf_link_local_dynamic_entry *p;
464 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
465 p->dynindx = ++dynsymcount;
466 }
467
468 elf_link_hash_traverse (elf_hash_table (info),
469 elf_link_renumber_hash_table_dynsyms,
470 &dynsymcount);
471
472 /* There is an unused NULL entry at the head of the table which
473 we must account for in our count. Unless there weren't any
474 symbols, which means we'll have no table at all. */
475 if (dynsymcount != 0)
476 ++dynsymcount;
477
478 return elf_hash_table (info)->dynsymcount = dynsymcount;
479 }
480 \f
481 /* Create a special linker section, or return a pointer to a linker
482 section already created */
483
484 elf_linker_section_t *
485 _bfd_elf_create_linker_section (abfd, info, which, defaults)
486 bfd *abfd;
487 struct bfd_link_info *info;
488 enum elf_linker_section_enum which;
489 elf_linker_section_t *defaults;
490 {
491 bfd *dynobj = elf_hash_table (info)->dynobj;
492 elf_linker_section_t *lsect;
493
494 /* Record the first bfd section that needs the special section */
495 if (!dynobj)
496 dynobj = elf_hash_table (info)->dynobj = abfd;
497
498 /* If this is the first time, create the section */
499 lsect = elf_linker_section (dynobj, which);
500 if (!lsect)
501 {
502 asection *s;
503 bfd_size_type amt = sizeof (elf_linker_section_t);
504
505 lsect = (elf_linker_section_t *) bfd_alloc (dynobj, amt);
506
507 *lsect = *defaults;
508 elf_linker_section (dynobj, which) = lsect;
509 lsect->which = which;
510 lsect->hole_written_p = FALSE;
511
512 /* See if the sections already exist */
513 lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name);
514 if (!s || (s->flags & defaults->flags) != defaults->flags)
515 {
516 lsect->section = s = bfd_make_section_anyway (dynobj, lsect->name);
517
518 if (s == NULL)
519 return (elf_linker_section_t *)0;
520
521 bfd_set_section_flags (dynobj, s, defaults->flags);
522 bfd_set_section_alignment (dynobj, s, lsect->alignment);
523 }
524 else if (bfd_get_section_alignment (dynobj, s) < lsect->alignment)
525 bfd_set_section_alignment (dynobj, s, lsect->alignment);
526
527 s->_raw_size = align_power (s->_raw_size, lsect->alignment);
528
529 /* Is there a hole we have to provide? If so check whether the
530 segment is too big already */
531 if (lsect->hole_size)
532 {
533 lsect->hole_offset = s->_raw_size;
534 s->_raw_size += lsect->hole_size;
535 if (lsect->hole_offset > lsect->max_hole_offset)
536 {
537 (*_bfd_error_handler)
538 (_("%s: Section %s is too large to add hole of %ld bytes"),
539 bfd_get_filename (abfd),
540 lsect->name,
541 (long) lsect->hole_size);
542
543 bfd_set_error (bfd_error_bad_value);
544 return (elf_linker_section_t *)0;
545 }
546 }
547
548 #ifdef DEBUG
549 fprintf (stderr, "Creating section %s, current size = %ld\n",
550 lsect->name, (long)s->_raw_size);
551 #endif
552
553 if (lsect->sym_name)
554 {
555 struct elf_link_hash_entry *h;
556 struct bfd_link_hash_entry *bh;
557
558 #ifdef DEBUG
559 fprintf (stderr, "Adding %s to section %s\n",
560 lsect->sym_name,
561 lsect->name);
562 #endif
563 bh = bfd_link_hash_lookup (info->hash, lsect->sym_name,
564 FALSE, FALSE, FALSE);
565
566 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
567 && !(_bfd_generic_link_add_one_symbol
568 (info, abfd, lsect->sym_name, BSF_GLOBAL, s,
569 (lsect->hole_size
570 ? s->_raw_size - lsect->hole_size + lsect->sym_offset
571 : lsect->sym_offset),
572 (const char *) NULL, FALSE,
573 get_elf_backend_data (abfd)->collect, &bh)))
574 return (elf_linker_section_t *) 0;
575 h = (struct elf_link_hash_entry *) bh;
576
577 if ((defaults->which != LINKER_SECTION_SDATA)
578 && (defaults->which != LINKER_SECTION_SDATA2))
579 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_DYNAMIC;
580
581 h->type = STT_OBJECT;
582 lsect->sym_hash = h;
583
584 if (info->shared
585 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
586 return (elf_linker_section_t *) 0;
587 }
588 }
589
590 #if 0
591 /* This does not make sense. The sections which may exist in the
592 object file have nothing to do with the sections we want to
593 create. */
594
595 /* Find the related sections if they have been created */
596 if (lsect->bss_name && !lsect->bss_section)
597 lsect->bss_section = bfd_get_section_by_name (dynobj, lsect->bss_name);
598
599 if (lsect->rel_name && !lsect->rel_section)
600 lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
601 #endif
602
603 return lsect;
604 }
605 \f
606 /* Find a linker generated pointer with a given addend and type. */
607
608 elf_linker_section_pointers_t *
609 _bfd_elf_find_pointer_linker_section (linker_pointers, addend, which)
610 elf_linker_section_pointers_t *linker_pointers;
611 bfd_vma addend;
612 elf_linker_section_enum_t which;
613 {
614 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
615 {
616 if (which == linker_pointers->which && addend == linker_pointers->addend)
617 return linker_pointers;
618 }
619
620 return (elf_linker_section_pointers_t *)0;
621 }
622 \f
623 /* Make the .rela section corresponding to the generated linker section. */
624
625 bfd_boolean
626 _bfd_elf_make_linker_section_rela (dynobj, lsect, alignment)
627 bfd *dynobj;
628 elf_linker_section_t *lsect;
629 int alignment;
630 {
631 if (lsect->rel_section)
632 return TRUE;
633
634 lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
635 if (lsect->rel_section == NULL)
636 {
637 lsect->rel_section = bfd_make_section (dynobj, lsect->rel_name);
638 if (lsect->rel_section == NULL
639 || ! bfd_set_section_flags (dynobj,
640 lsect->rel_section,
641 (SEC_ALLOC
642 | SEC_LOAD
643 | SEC_HAS_CONTENTS
644 | SEC_IN_MEMORY
645 | SEC_LINKER_CREATED
646 | SEC_READONLY))
647 || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment))
648 return FALSE;
649 }
650
651 return TRUE;
652 }
This page took 0.045149 seconds and 4 git commands to generate.