1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 if (bfd_get_section_by_name (abfd
, ".got") != NULL
)
44 switch (bed
->s
->arch_size
)
55 bfd_set_error (bfd_error_bad_value
);
59 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
60 | SEC_LINKER_CREATED
);
62 s
= bfd_make_section (abfd
, ".got");
64 || !bfd_set_section_flags (abfd
, s
, flags
)
65 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
68 if (bed
->want_got_plt
)
70 s
= bfd_make_section (abfd
, ".got.plt");
72 || !bfd_set_section_flags (abfd
, s
, flags
)
73 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
77 if (bed
->want_got_sym
)
79 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
80 (or .got.plt) section. We don't do this in the linker script
81 because we don't want to define the symbol if we are not creating
82 a global offset table. */
84 if (!(_bfd_generic_link_add_one_symbol
85 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
86 bed
->got_symbol_offset
, (const char *) NULL
, false,
89 h
= (struct elf_link_hash_entry
*) bh
;
90 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
94 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
97 elf_hash_table (info
)->hgot
= h
;
100 /* The first bit of the global offset table is the header. */
101 s
->_raw_size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
106 /* Create dynamic sections when linking against a dynamic object. */
109 _bfd_elf_create_dynamic_sections (abfd
, info
)
111 struct bfd_link_info
*info
;
113 flagword flags
, pltflags
;
114 register asection
*s
;
115 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
118 switch (bed
->s
->arch_size
)
129 bfd_set_error (bfd_error_bad_value
);
133 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
134 .rel[a].bss sections. */
136 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
137 | SEC_LINKER_CREATED
);
140 pltflags
|= SEC_CODE
;
141 if (bed
->plt_not_loaded
)
142 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
143 if (bed
->plt_readonly
)
144 pltflags
|= SEC_READONLY
;
146 s
= bfd_make_section (abfd
, ".plt");
148 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
149 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
152 if (bed
->want_plt_sym
)
154 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
156 struct elf_link_hash_entry
*h
;
157 struct bfd_link_hash_entry
*bh
= NULL
;
159 if (! (_bfd_generic_link_add_one_symbol
160 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
161 (bfd_vma
) 0, (const char *) NULL
, false,
162 get_elf_backend_data (abfd
)->collect
, &bh
)))
164 h
= (struct elf_link_hash_entry
*) bh
;
165 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
166 h
->type
= STT_OBJECT
;
169 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
173 s
= bfd_make_section (abfd
,
174 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
176 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
177 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
180 if (! _bfd_elf_create_got_section (abfd
, info
))
183 if (bed
->want_dynbss
)
185 /* The .dynbss section is a place to put symbols which are defined
186 by dynamic objects, are referenced by regular objects, and are
187 not functions. We must allocate space for them in the process
188 image and use a R_*_COPY reloc to tell the dynamic linker to
189 initialize them at run time. The linker script puts the .dynbss
190 section into the .bss section of the final image. */
191 s
= bfd_make_section (abfd
, ".dynbss");
193 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
196 /* The .rel[a].bss section holds copy relocs. This section is not
197 normally needed. We need to create it here, though, so that the
198 linker will map it to an output section. We can't just create it
199 only if we need it, because we will not know whether we need it
200 until we have seen all the input files, and the first time the
201 main linker code calls BFD after examining all the input files
202 (size_dynamic_sections) the input sections have already been
203 mapped to the output sections. If the section turns out not to
204 be needed, we can discard it later. We will never need this
205 section when generating a shared object, since they do not use
209 s
= bfd_make_section (abfd
,
210 (bed
->default_use_rela_p
211 ? ".rela.bss" : ".rel.bss"));
213 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
214 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
222 /* Record a new dynamic symbol. We record the dynamic symbols as we
223 read the input files, since we need to have a list of all of them
224 before we can determine the final sizes of the output sections.
225 Note that we may actually call this function even though we are not
226 going to output any dynamic symbols; in some cases we know that a
227 symbol should be in the dynamic symbol table, but only if there is
231 _bfd_elf_link_record_dynamic_symbol (info
, h
)
232 struct bfd_link_info
*info
;
233 struct elf_link_hash_entry
*h
;
235 if (h
->dynindx
== -1)
237 struct elf_strtab_hash
*dynstr
;
243 /* XXX: The ABI draft says the linker must turn hidden and
244 internal symbols into STB_LOCAL symbols when producing the
245 DSO. However, if ld.so honors st_other in the dynamic table,
246 this would not be necessary. */
247 switch (ELF_ST_VISIBILITY (h
->other
))
251 if (h
->root
.type
!= bfd_link_hash_undefined
252 && h
->root
.type
!= bfd_link_hash_undefweak
)
254 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
262 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
263 ++elf_hash_table (info
)->dynsymcount
;
265 dynstr
= elf_hash_table (info
)->dynstr
;
268 /* Create a strtab to hold the dynamic symbol names. */
269 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
274 /* We don't put any version information in the dynamic string
276 name
= h
->root
.root
.string
;
277 p
= strchr (name
, ELF_VER_CHR
);
285 size_t len
= p
- name
+ 1;
287 alc
= bfd_malloc ((bfd_size_type
) len
);
290 memcpy (alc
, name
, len
- 1);
296 indx
= _bfd_elf_strtab_add (dynstr
, name
, copy
);
301 if (indx
== (bfd_size_type
) -1)
303 h
->dynstr_index
= indx
;
309 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
310 success, and 2 on a failure caused by attempting to record a symbol
311 in a discarded section, eg. a discarded link-once section symbol. */
314 elf_link_record_local_dynamic_symbol (info
, input_bfd
, input_indx
)
315 struct bfd_link_info
*info
;
320 struct elf_link_local_dynamic_entry
*entry
;
321 struct elf_link_hash_table
*eht
;
322 struct elf_strtab_hash
*dynstr
;
323 unsigned long dynstr_index
;
325 Elf_External_Sym_Shndx eshndx
;
326 char esym
[sizeof (Elf64_External_Sym
)];
328 if (! is_elf_hash_table (info
))
331 /* See if the entry exists already. */
332 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
333 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
336 amt
= sizeof (*entry
);
337 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
341 /* Go find the symbol, so that we can find it's name. */
342 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
343 (size_t) 1, (size_t) input_indx
,
344 &entry
->isym
, esym
, &eshndx
))
346 bfd_release (input_bfd
, entry
);
350 if (entry
->isym
.st_shndx
!= SHN_UNDEF
351 && (entry
->isym
.st_shndx
< SHN_LORESERVE
352 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
356 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
357 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
359 /* We can still bfd_release here as nothing has done another
360 bfd_alloc. We can't do this later in this function. */
361 bfd_release (input_bfd
, entry
);
366 name
= (bfd_elf_string_from_elf_section
367 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
368 entry
->isym
.st_name
));
370 dynstr
= elf_hash_table (info
)->dynstr
;
373 /* Create a strtab to hold the dynamic symbol names. */
374 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
379 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
380 if (dynstr_index
== (unsigned long) -1)
382 entry
->isym
.st_name
= dynstr_index
;
384 eht
= elf_hash_table (info
);
386 entry
->next
= eht
->dynlocal
;
387 eht
->dynlocal
= entry
;
388 entry
->input_bfd
= input_bfd
;
389 entry
->input_indx
= input_indx
;
392 /* Whatever binding the symbol had before, it's now local. */
394 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
396 /* The dynindx will be set at the end of size_dynamic_sections. */
401 /* Return the dynindex of a local dynamic symbol. */
404 _bfd_elf_link_lookup_local_dynindx (info
, input_bfd
, input_indx
)
405 struct bfd_link_info
*info
;
409 struct elf_link_local_dynamic_entry
*e
;
411 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
412 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
417 /* This function is used to renumber the dynamic symbols, if some of
418 them are removed because they are marked as local. This is called
419 via elf_link_hash_traverse. */
421 static boolean elf_link_renumber_hash_table_dynsyms
422 PARAMS ((struct elf_link_hash_entry
*, PTR
));
425 elf_link_renumber_hash_table_dynsyms (h
, data
)
426 struct elf_link_hash_entry
*h
;
429 size_t *count
= (size_t *) data
;
431 if (h
->root
.type
== bfd_link_hash_warning
)
432 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
434 if (h
->dynindx
!= -1)
435 h
->dynindx
= ++(*count
);
440 /* Assign dynsym indices. In a shared library we generate a section
441 symbol for each output section, which come first. Next come all of
442 the back-end allocated local dynamic syms, followed by the rest of
443 the global symbols. */
446 _bfd_elf_link_renumber_dynsyms (output_bfd
, info
)
448 struct bfd_link_info
*info
;
450 unsigned long dynsymcount
= 0;
455 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
456 if ((p
->flags
& SEC_EXCLUDE
) == 0)
457 elf_section_data (p
)->dynindx
= ++dynsymcount
;
460 if (elf_hash_table (info
)->dynlocal
)
462 struct elf_link_local_dynamic_entry
*p
;
463 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
464 p
->dynindx
= ++dynsymcount
;
467 elf_link_hash_traverse (elf_hash_table (info
),
468 elf_link_renumber_hash_table_dynsyms
,
471 /* There is an unused NULL entry at the head of the table which
472 we must account for in our count. Unless there weren't any
473 symbols, which means we'll have no table at all. */
474 if (dynsymcount
!= 0)
477 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
480 /* Create a special linker section, or return a pointer to a linker
481 section already created */
483 elf_linker_section_t
*
484 _bfd_elf_create_linker_section (abfd
, info
, which
, defaults
)
486 struct bfd_link_info
*info
;
487 enum elf_linker_section_enum which
;
488 elf_linker_section_t
*defaults
;
490 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
491 elf_linker_section_t
*lsect
;
493 /* Record the first bfd section that needs the special section */
495 dynobj
= elf_hash_table (info
)->dynobj
= abfd
;
497 /* If this is the first time, create the section */
498 lsect
= elf_linker_section (dynobj
, which
);
502 bfd_size_type amt
= sizeof (elf_linker_section_t
);
504 lsect
= (elf_linker_section_t
*) bfd_alloc (dynobj
, amt
);
507 elf_linker_section (dynobj
, which
) = lsect
;
508 lsect
->which
= which
;
509 lsect
->hole_written_p
= false;
511 /* See if the sections already exist */
512 lsect
->section
= s
= bfd_get_section_by_name (dynobj
, lsect
->name
);
513 if (!s
|| (s
->flags
& defaults
->flags
) != defaults
->flags
)
515 lsect
->section
= s
= bfd_make_section_anyway (dynobj
, lsect
->name
);
518 return (elf_linker_section_t
*)0;
520 bfd_set_section_flags (dynobj
, s
, defaults
->flags
);
521 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
523 else if (bfd_get_section_alignment (dynobj
, s
) < lsect
->alignment
)
524 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
526 s
->_raw_size
= align_power (s
->_raw_size
, lsect
->alignment
);
528 /* Is there a hole we have to provide? If so check whether the segment is
530 if (lsect
->hole_size
)
532 lsect
->hole_offset
= s
->_raw_size
;
533 s
->_raw_size
+= lsect
->hole_size
;
534 if (lsect
->hole_offset
> lsect
->max_hole_offset
)
536 (*_bfd_error_handler
) (_("%s: Section %s is too large to add hole of %ld bytes"),
537 bfd_get_filename (abfd
),
539 (long) lsect
->hole_size
);
541 bfd_set_error (bfd_error_bad_value
);
542 return (elf_linker_section_t
*)0;
547 fprintf (stderr
, "Creating section %s, current size = %ld\n",
548 lsect
->name
, (long)s
->_raw_size
);
553 struct elf_link_hash_entry
*h
;
554 struct bfd_link_hash_entry
*bh
;
557 fprintf (stderr
, "Adding %s to section %s\n",
561 bh
= bfd_link_hash_lookup (info
->hash
, lsect
->sym_name
,
562 false, false, false);
564 if ((bh
== NULL
|| bh
->type
== bfd_link_hash_undefined
)
565 && !(_bfd_generic_link_add_one_symbol
566 (info
, abfd
, lsect
->sym_name
, BSF_GLOBAL
, s
,
568 ? s
->_raw_size
- lsect
->hole_size
+ lsect
->sym_offset
569 : lsect
->sym_offset
),
570 (const char *) NULL
, false,
571 get_elf_backend_data (abfd
)->collect
, &bh
)))
572 return (elf_linker_section_t
*) 0;
573 h
= (struct elf_link_hash_entry
*) bh
;
575 if ((defaults
->which
!= LINKER_SECTION_SDATA
)
576 && (defaults
->which
!= LINKER_SECTION_SDATA2
))
577 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_DYNAMIC
;
579 h
->type
= STT_OBJECT
;
583 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
584 return (elf_linker_section_t
*) 0;
589 /* This does not make sense. The sections which may exist in the
590 object file have nothing to do with the sections we want to
593 /* Find the related sections if they have been created */
594 if (lsect
->bss_name
&& !lsect
->bss_section
)
595 lsect
->bss_section
= bfd_get_section_by_name (dynobj
, lsect
->bss_name
);
597 if (lsect
->rel_name
&& !lsect
->rel_section
)
598 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
604 /* Find a linker generated pointer with a given addend and type. */
606 elf_linker_section_pointers_t
*
607 _bfd_elf_find_pointer_linker_section (linker_pointers
, addend
, which
)
608 elf_linker_section_pointers_t
*linker_pointers
;
610 elf_linker_section_enum_t which
;
612 for ( ; linker_pointers
!= NULL
; linker_pointers
= linker_pointers
->next
)
614 if (which
== linker_pointers
->which
&& addend
== linker_pointers
->addend
)
615 return linker_pointers
;
618 return (elf_linker_section_pointers_t
*)0;
621 /* Make the .rela section corresponding to the generated linker section. */
624 _bfd_elf_make_linker_section_rela (dynobj
, lsect
, alignment
)
626 elf_linker_section_t
*lsect
;
629 if (lsect
->rel_section
)
632 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
633 if (lsect
->rel_section
== NULL
)
635 lsect
->rel_section
= bfd_make_section (dynobj
, lsect
->rel_name
);
636 if (lsect
->rel_section
== NULL
637 || ! bfd_set_section_flags (dynobj
,
645 || ! bfd_set_section_alignment (dynobj
, lsect
->rel_section
, alignment
))