1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
29 _bfd_elf_create_got_section (abfd
, info
)
31 struct bfd_link_info
*info
;
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
);
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)
45 switch (bed
->s
->arch_size
)
56 bfd_set_error (bfd_error_bad_value
);
60 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
61 | SEC_LINKER_CREATED
);
63 s
= bfd_make_section (abfd
, ".got");
65 || !bfd_set_section_flags (abfd
, s
, flags
)
66 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
69 if (bed
->want_got_plt
)
71 s
= bfd_make_section (abfd
, ".got.plt");
73 || !bfd_set_section_flags (abfd
, s
, flags
)
74 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
78 if (bed
->want_got_sym
)
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. */
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
,
90 h
= (struct elf_link_hash_entry
*) bh
;
91 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
95 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
98 elf_hash_table (info
)->hgot
= h
;
101 /* The first bit of the global offset table is the header. */
102 s
->_raw_size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
107 /* Create dynamic sections when linking against a dynamic object. */
110 _bfd_elf_create_dynamic_sections (abfd
, info
)
112 struct bfd_link_info
*info
;
114 flagword flags
, pltflags
;
116 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
119 switch (bed
->s
->arch_size
)
130 bfd_set_error (bfd_error_bad_value
);
134 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
135 .rel[a].bss sections. */
137 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
138 | SEC_LINKER_CREATED
);
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
;
147 s
= bfd_make_section (abfd
, ".plt");
149 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
150 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
153 if (bed
->want_plt_sym
)
155 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
157 struct elf_link_hash_entry
*h
;
158 struct bfd_link_hash_entry
*bh
= NULL
;
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
)))
165 h
= (struct elf_link_hash_entry
*) bh
;
166 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
167 h
->type
= STT_OBJECT
;
170 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
174 s
= bfd_make_section (abfd
,
175 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
177 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
178 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
181 if (! _bfd_elf_create_got_section (abfd
, info
))
184 if (bed
->want_dynbss
)
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");
194 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
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
210 s
= bfd_make_section (abfd
,
211 (bed
->default_use_rela_p
212 ? ".rela.bss" : ".rel.bss"));
214 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
215 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
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
232 _bfd_elf_link_record_dynamic_symbol (info
, h
)
233 struct bfd_link_info
*info
;
234 struct elf_link_hash_entry
*h
;
236 if (h
->dynindx
== -1)
238 struct elf_strtab_hash
*dynstr
;
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
))
252 if (h
->root
.type
!= bfd_link_hash_undefined
253 && h
->root
.type
!= bfd_link_hash_undefweak
)
255 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
263 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
264 ++elf_hash_table (info
)->dynsymcount
;
266 dynstr
= elf_hash_table (info
)->dynstr
;
269 /* Create a strtab to hold the dynamic symbol names. */
270 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
275 /* We don't put any version information in the dynamic string
277 name
= h
->root
.root
.string
;
278 p
= strchr (name
, ELF_VER_CHR
);
286 size_t len
= p
- name
+ 1;
288 alc
= bfd_malloc ((bfd_size_type
) len
);
291 memcpy (alc
, name
, len
- 1);
297 indx
= _bfd_elf_strtab_add (dynstr
, name
, copy
);
302 if (indx
== (bfd_size_type
) -1)
304 h
->dynstr_index
= indx
;
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. */
315 elf_link_record_local_dynamic_symbol (info
, input_bfd
, input_indx
)
316 struct bfd_link_info
*info
;
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
;
326 Elf_External_Sym_Shndx eshndx
;
327 char esym
[sizeof (Elf64_External_Sym
)];
329 if (! is_elf_hash_table (info
))
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
)
337 amt
= sizeof (*entry
);
338 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
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
))
347 bfd_release (input_bfd
, entry
);
351 if (entry
->isym
.st_shndx
!= SHN_UNDEF
352 && (entry
->isym
.st_shndx
< SHN_LORESERVE
353 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
357 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
358 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
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
);
367 name
= (bfd_elf_string_from_elf_section
368 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
369 entry
->isym
.st_name
));
371 dynstr
= elf_hash_table (info
)->dynstr
;
374 /* Create a strtab to hold the dynamic symbol names. */
375 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
380 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
381 if (dynstr_index
== (unsigned long) -1)
383 entry
->isym
.st_name
= dynstr_index
;
385 eht
= elf_hash_table (info
);
387 entry
->next
= eht
->dynlocal
;
388 eht
->dynlocal
= entry
;
389 entry
->input_bfd
= input_bfd
;
390 entry
->input_indx
= input_indx
;
393 /* Whatever binding the symbol had before, it's now local. */
395 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
397 /* The dynindx will be set at the end of size_dynamic_sections. */
402 /* Return the dynindex of a local dynamic symbol. */
405 _bfd_elf_link_lookup_local_dynindx (info
, input_bfd
, input_indx
)
406 struct bfd_link_info
*info
;
410 struct elf_link_local_dynamic_entry
*e
;
412 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
413 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
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. */
422 static bfd_boolean elf_link_renumber_hash_table_dynsyms
423 PARAMS ((struct elf_link_hash_entry
*, PTR
));
426 elf_link_renumber_hash_table_dynsyms (h
, data
)
427 struct elf_link_hash_entry
*h
;
430 size_t *count
= (size_t *) data
;
432 if (h
->root
.type
== bfd_link_hash_warning
)
433 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
435 if (h
->dynindx
!= -1)
436 h
->dynindx
= ++(*count
);
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. */
447 _bfd_elf_link_renumber_dynsyms (output_bfd
, info
)
449 struct bfd_link_info
*info
;
451 unsigned long dynsymcount
= 0;
456 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
457 if ((p
->flags
& SEC_EXCLUDE
) == 0)
458 elf_section_data (p
)->dynindx
= ++dynsymcount
;
461 if (elf_hash_table (info
)->dynlocal
)
463 struct elf_link_local_dynamic_entry
*p
;
464 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
465 p
->dynindx
= ++dynsymcount
;
468 elf_link_hash_traverse (elf_hash_table (info
),
469 elf_link_renumber_hash_table_dynsyms
,
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)
478 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
481 /* Create a special linker section, or return a pointer to a linker
482 section already created */
484 elf_linker_section_t
*
485 _bfd_elf_create_linker_section (abfd
, info
, which
, defaults
)
487 struct bfd_link_info
*info
;
488 enum elf_linker_section_enum which
;
489 elf_linker_section_t
*defaults
;
491 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
492 elf_linker_section_t
*lsect
;
494 /* Record the first bfd section that needs the special section */
496 dynobj
= elf_hash_table (info
)->dynobj
= abfd
;
498 /* If this is the first time, create the section */
499 lsect
= elf_linker_section (dynobj
, which
);
503 bfd_size_type amt
= sizeof (elf_linker_section_t
);
505 lsect
= (elf_linker_section_t
*) bfd_alloc (dynobj
, amt
);
508 elf_linker_section (dynobj
, which
) = lsect
;
509 lsect
->which
= which
;
510 lsect
->hole_written_p
= FALSE
;
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
)
516 lsect
->section
= s
= bfd_make_section_anyway (dynobj
, lsect
->name
);
519 return (elf_linker_section_t
*)0;
521 bfd_set_section_flags (dynobj
, s
, defaults
->flags
);
522 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
524 else if (bfd_get_section_alignment (dynobj
, s
) < lsect
->alignment
)
525 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
527 s
->_raw_size
= align_power (s
->_raw_size
, lsect
->alignment
);
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
)
533 lsect
->hole_offset
= s
->_raw_size
;
534 s
->_raw_size
+= lsect
->hole_size
;
535 if (lsect
->hole_offset
> lsect
->max_hole_offset
)
537 (*_bfd_error_handler
)
538 (_("%s: Section %s is too large to add hole of %ld bytes"),
539 bfd_get_filename (abfd
),
541 (long) lsect
->hole_size
);
543 bfd_set_error (bfd_error_bad_value
);
544 return (elf_linker_section_t
*)0;
549 fprintf (stderr
, "Creating section %s, current size = %ld\n",
550 lsect
->name
, (long)s
->_raw_size
);
555 struct elf_link_hash_entry
*h
;
556 struct bfd_link_hash_entry
*bh
;
559 fprintf (stderr
, "Adding %s to section %s\n",
563 bh
= bfd_link_hash_lookup (info
->hash
, lsect
->sym_name
,
564 FALSE
, FALSE
, FALSE
);
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
,
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
;
577 if ((defaults
->which
!= LINKER_SECTION_SDATA
)
578 && (defaults
->which
!= LINKER_SECTION_SDATA2
))
579 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_DYNAMIC
;
581 h
->type
= STT_OBJECT
;
585 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
586 return (elf_linker_section_t
*) 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
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
);
599 if (lsect
->rel_name
&& !lsect
->rel_section
)
600 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
606 /* Find a linker generated pointer with a given addend and type. */
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
;
612 elf_linker_section_enum_t which
;
614 for ( ; linker_pointers
!= NULL
; linker_pointers
= linker_pointers
->next
)
616 if (which
== linker_pointers
->which
&& addend
== linker_pointers
->addend
)
617 return linker_pointers
;
620 return (elf_linker_section_pointers_t
*)0;
623 /* Make the .rela section corresponding to the generated linker section. */
626 _bfd_elf_make_linker_section_rela (dynobj
, lsect
, alignment
)
628 elf_linker_section_t
*lsect
;
631 if (lsect
->rel_section
)
634 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
635 if (lsect
->rel_section
== NULL
)
637 lsect
->rel_section
= bfd_make_section (dynobj
, lsect
->rel_name
);
638 if (lsect
->rel_section
== NULL
639 || ! bfd_set_section_flags (dynobj
,
647 || ! bfd_set_section_alignment (dynobj
, lsect
->rel_section
, alignment
))