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. */
28 static bfd_boolean elf_link_read_relocs_from_section
29 PARAMS ((bfd
*, Elf_Internal_Shdr
*, PTR
, Elf_Internal_Rela
*));
32 _bfd_elf_create_got_section (abfd
, info
)
34 struct bfd_link_info
*info
;
38 struct elf_link_hash_entry
*h
;
39 struct bfd_link_hash_entry
*bh
;
40 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
43 /* This function may be called more than once. */
44 s
= bfd_get_section_by_name (abfd
, ".got");
45 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
48 switch (bed
->s
->arch_size
)
59 bfd_set_error (bfd_error_bad_value
);
63 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
64 | SEC_LINKER_CREATED
);
66 s
= bfd_make_section (abfd
, ".got");
68 || !bfd_set_section_flags (abfd
, s
, flags
)
69 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
72 if (bed
->want_got_plt
)
74 s
= bfd_make_section (abfd
, ".got.plt");
76 || !bfd_set_section_flags (abfd
, s
, flags
)
77 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
81 if (bed
->want_got_sym
)
83 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
84 (or .got.plt) section. We don't do this in the linker script
85 because we don't want to define the symbol if we are not creating
86 a global offset table. */
88 if (!(_bfd_generic_link_add_one_symbol
89 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
90 bed
->got_symbol_offset
, (const char *) NULL
, FALSE
,
93 h
= (struct elf_link_hash_entry
*) bh
;
94 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
98 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
101 elf_hash_table (info
)->hgot
= h
;
104 /* The first bit of the global offset table is the header. */
105 s
->_raw_size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
110 /* Create some sections which will be filled in with dynamic linking
111 information. ABFD is an input file which requires dynamic sections
112 to be created. The dynamic sections take up virtual memory space
113 when the final executable is run, so we need to create them before
114 addresses are assigned to the output sections. We work out the
115 actual contents and size of these sections later. */
118 _bfd_elf_link_create_dynamic_sections (abfd
, info
)
120 struct bfd_link_info
*info
;
123 register asection
*s
;
124 struct elf_link_hash_entry
*h
;
125 struct bfd_link_hash_entry
*bh
;
126 struct elf_backend_data
*bed
;
128 if (! is_elf_hash_table (info
))
131 if (elf_hash_table (info
)->dynamic_sections_created
)
134 /* Make sure that all dynamic sections use the same input BFD. */
135 if (elf_hash_table (info
)->dynobj
== NULL
)
136 elf_hash_table (info
)->dynobj
= abfd
;
138 abfd
= elf_hash_table (info
)->dynobj
;
140 /* Note that we set the SEC_IN_MEMORY flag for all of these
142 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
143 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
145 /* A dynamically linked executable has a .interp section, but a
146 shared library does not. */
149 s
= bfd_make_section (abfd
, ".interp");
151 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
155 if (! info
->traditional_format
156 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
158 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
160 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
161 || ! bfd_set_section_alignment (abfd
, s
, 2))
163 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
166 bed
= get_elf_backend_data (abfd
);
168 /* Create sections to hold version informations. These are removed
169 if they are not needed. */
170 s
= bfd_make_section (abfd
, ".gnu.version_d");
172 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
173 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
176 s
= bfd_make_section (abfd
, ".gnu.version");
178 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
179 || ! bfd_set_section_alignment (abfd
, s
, 1))
182 s
= bfd_make_section (abfd
, ".gnu.version_r");
184 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
185 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
188 s
= bfd_make_section (abfd
, ".dynsym");
190 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
191 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
194 s
= bfd_make_section (abfd
, ".dynstr");
196 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
199 /* Create a strtab to hold the dynamic symbol names. */
200 if (elf_hash_table (info
)->dynstr
== NULL
)
202 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
203 if (elf_hash_table (info
)->dynstr
== NULL
)
207 s
= bfd_make_section (abfd
, ".dynamic");
209 || ! bfd_set_section_flags (abfd
, s
, flags
)
210 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
213 /* The special symbol _DYNAMIC is always set to the start of the
214 .dynamic section. This call occurs before we have processed the
215 symbols for any dynamic object, so we don't have to worry about
216 overriding a dynamic definition. We could set _DYNAMIC in a
217 linker script, but we only want to define it if we are, in fact,
218 creating a .dynamic section. We don't want to define it if there
219 is no .dynamic section, since on some ELF platforms the start up
220 code examines it to decide how to initialize the process. */
222 if (! (_bfd_generic_link_add_one_symbol
223 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
224 (const char *) 0, FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
226 h
= (struct elf_link_hash_entry
*) bh
;
227 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
228 h
->type
= STT_OBJECT
;
231 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
234 s
= bfd_make_section (abfd
, ".hash");
236 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
237 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
239 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
241 /* Let the backend create the rest of the sections. This lets the
242 backend set the right flags. The backend will normally create
243 the .got and .plt sections. */
244 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
247 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
252 /* Create dynamic sections when linking against a dynamic object. */
255 _bfd_elf_create_dynamic_sections (abfd
, info
)
257 struct bfd_link_info
*info
;
259 flagword flags
, pltflags
;
261 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
263 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
264 .rel[a].bss sections. */
266 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
267 | SEC_LINKER_CREATED
);
270 pltflags
|= SEC_CODE
;
271 if (bed
->plt_not_loaded
)
272 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
273 if (bed
->plt_readonly
)
274 pltflags
|= SEC_READONLY
;
276 s
= bfd_make_section (abfd
, ".plt");
278 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
279 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
282 if (bed
->want_plt_sym
)
284 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
286 struct elf_link_hash_entry
*h
;
287 struct bfd_link_hash_entry
*bh
= NULL
;
289 if (! (_bfd_generic_link_add_one_symbol
290 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
291 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
292 get_elf_backend_data (abfd
)->collect
, &bh
)))
294 h
= (struct elf_link_hash_entry
*) bh
;
295 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
296 h
->type
= STT_OBJECT
;
299 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
303 s
= bfd_make_section (abfd
,
304 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
306 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
307 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
310 if (! _bfd_elf_create_got_section (abfd
, info
))
313 if (bed
->want_dynbss
)
315 /* The .dynbss section is a place to put symbols which are defined
316 by dynamic objects, are referenced by regular objects, and are
317 not functions. We must allocate space for them in the process
318 image and use a R_*_COPY reloc to tell the dynamic linker to
319 initialize them at run time. The linker script puts the .dynbss
320 section into the .bss section of the final image. */
321 s
= bfd_make_section (abfd
, ".dynbss");
323 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
326 /* The .rel[a].bss section holds copy relocs. This section is not
327 normally needed. We need to create it here, though, so that the
328 linker will map it to an output section. We can't just create it
329 only if we need it, because we will not know whether we need it
330 until we have seen all the input files, and the first time the
331 main linker code calls BFD after examining all the input files
332 (size_dynamic_sections) the input sections have already been
333 mapped to the output sections. If the section turns out not to
334 be needed, we can discard it later. We will never need this
335 section when generating a shared object, since they do not use
339 s
= bfd_make_section (abfd
,
340 (bed
->default_use_rela_p
341 ? ".rela.bss" : ".rel.bss"));
343 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
344 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
352 /* Record a new dynamic symbol. We record the dynamic symbols as we
353 read the input files, since we need to have a list of all of them
354 before we can determine the final sizes of the output sections.
355 Note that we may actually call this function even though we are not
356 going to output any dynamic symbols; in some cases we know that a
357 symbol should be in the dynamic symbol table, but only if there is
361 _bfd_elf_link_record_dynamic_symbol (info
, h
)
362 struct bfd_link_info
*info
;
363 struct elf_link_hash_entry
*h
;
365 if (h
->dynindx
== -1)
367 struct elf_strtab_hash
*dynstr
;
373 /* XXX: The ABI draft says the linker must turn hidden and
374 internal symbols into STB_LOCAL symbols when producing the
375 DSO. However, if ld.so honors st_other in the dynamic table,
376 this would not be necessary. */
377 switch (ELF_ST_VISIBILITY (h
->other
))
381 if (h
->root
.type
!= bfd_link_hash_undefined
382 && h
->root
.type
!= bfd_link_hash_undefweak
)
384 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
392 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
393 ++elf_hash_table (info
)->dynsymcount
;
395 dynstr
= elf_hash_table (info
)->dynstr
;
398 /* Create a strtab to hold the dynamic symbol names. */
399 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
404 /* We don't put any version information in the dynamic string
406 name
= h
->root
.root
.string
;
407 p
= strchr (name
, ELF_VER_CHR
);
415 size_t len
= p
- name
+ 1;
417 alc
= bfd_malloc ((bfd_size_type
) len
);
420 memcpy (alc
, name
, len
- 1);
426 indx
= _bfd_elf_strtab_add (dynstr
, name
, copy
);
431 if (indx
== (bfd_size_type
) -1)
433 h
->dynstr_index
= indx
;
439 /* Record an assignment to a symbol made by a linker script. We need
440 this in case some dynamic object refers to this symbol. */
443 bfd_elf_record_link_assignment (output_bfd
, info
, name
, provide
)
444 bfd
*output_bfd ATTRIBUTE_UNUSED
;
445 struct bfd_link_info
*info
;
449 struct elf_link_hash_entry
*h
;
451 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
454 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
458 if (h
->root
.type
== bfd_link_hash_new
)
459 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
461 /* If this symbol is being provided by the linker script, and it is
462 currently defined by a dynamic object, but not by a regular
463 object, then mark it as undefined so that the generic linker will
464 force the correct value. */
466 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
467 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
468 h
->root
.type
= bfd_link_hash_undefined
;
470 /* If this symbol is not being provided by the linker script, and it is
471 currently defined by a dynamic object, but not by a regular object,
472 then clear out any version information because the symbol will not be
473 associated with the dynamic object any more. */
475 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
476 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
477 h
->verinfo
.verdef
= NULL
;
479 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
481 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
482 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
486 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
489 /* If this is a weak defined symbol, and we know a corresponding
490 real symbol from the same dynamic object, make sure the real
491 symbol is also made into a dynamic symbol. */
492 if (h
->weakdef
!= NULL
493 && h
->weakdef
->dynindx
== -1)
495 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
503 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
504 success, and 2 on a failure caused by attempting to record a symbol
505 in a discarded section, eg. a discarded link-once section symbol. */
508 elf_link_record_local_dynamic_symbol (info
, input_bfd
, input_indx
)
509 struct bfd_link_info
*info
;
514 struct elf_link_local_dynamic_entry
*entry
;
515 struct elf_link_hash_table
*eht
;
516 struct elf_strtab_hash
*dynstr
;
517 unsigned long dynstr_index
;
519 Elf_External_Sym_Shndx eshndx
;
520 char esym
[sizeof (Elf64_External_Sym
)];
522 if (! is_elf_hash_table (info
))
525 /* See if the entry exists already. */
526 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
527 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
530 amt
= sizeof (*entry
);
531 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
535 /* Go find the symbol, so that we can find it's name. */
536 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
537 (size_t) 1, (size_t) input_indx
,
538 &entry
->isym
, esym
, &eshndx
))
540 bfd_release (input_bfd
, entry
);
544 if (entry
->isym
.st_shndx
!= SHN_UNDEF
545 && (entry
->isym
.st_shndx
< SHN_LORESERVE
546 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
550 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
551 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
553 /* We can still bfd_release here as nothing has done another
554 bfd_alloc. We can't do this later in this function. */
555 bfd_release (input_bfd
, entry
);
560 name
= (bfd_elf_string_from_elf_section
561 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
562 entry
->isym
.st_name
));
564 dynstr
= elf_hash_table (info
)->dynstr
;
567 /* Create a strtab to hold the dynamic symbol names. */
568 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
573 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
574 if (dynstr_index
== (unsigned long) -1)
576 entry
->isym
.st_name
= dynstr_index
;
578 eht
= elf_hash_table (info
);
580 entry
->next
= eht
->dynlocal
;
581 eht
->dynlocal
= entry
;
582 entry
->input_bfd
= input_bfd
;
583 entry
->input_indx
= input_indx
;
586 /* Whatever binding the symbol had before, it's now local. */
588 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
590 /* The dynindx will be set at the end of size_dynamic_sections. */
595 /* Return the dynindex of a local dynamic symbol. */
598 _bfd_elf_link_lookup_local_dynindx (info
, input_bfd
, input_indx
)
599 struct bfd_link_info
*info
;
603 struct elf_link_local_dynamic_entry
*e
;
605 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
606 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
611 /* This function is used to renumber the dynamic symbols, if some of
612 them are removed because they are marked as local. This is called
613 via elf_link_hash_traverse. */
615 static bfd_boolean elf_link_renumber_hash_table_dynsyms
616 PARAMS ((struct elf_link_hash_entry
*, PTR
));
619 elf_link_renumber_hash_table_dynsyms (h
, data
)
620 struct elf_link_hash_entry
*h
;
623 size_t *count
= (size_t *) data
;
625 if (h
->root
.type
== bfd_link_hash_warning
)
626 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
628 if (h
->dynindx
!= -1)
629 h
->dynindx
= ++(*count
);
634 /* Assign dynsym indices. In a shared library we generate a section
635 symbol for each output section, which come first. Next come all of
636 the back-end allocated local dynamic syms, followed by the rest of
637 the global symbols. */
640 _bfd_elf_link_renumber_dynsyms (output_bfd
, info
)
642 struct bfd_link_info
*info
;
644 unsigned long dynsymcount
= 0;
649 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
650 if ((p
->flags
& SEC_EXCLUDE
) == 0)
651 elf_section_data (p
)->dynindx
= ++dynsymcount
;
654 if (elf_hash_table (info
)->dynlocal
)
656 struct elf_link_local_dynamic_entry
*p
;
657 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
658 p
->dynindx
= ++dynsymcount
;
661 elf_link_hash_traverse (elf_hash_table (info
),
662 elf_link_renumber_hash_table_dynsyms
,
665 /* There is an unused NULL entry at the head of the table which
666 we must account for in our count. Unless there weren't any
667 symbols, which means we'll have no table at all. */
668 if (dynsymcount
!= 0)
671 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
674 /* This function is called when we want to define a new symbol. It
675 handles the various cases which arise when we find a definition in
676 a dynamic object, or when there is already a definition in a
677 dynamic object. The new symbol is described by NAME, SYM, PSEC,
678 and PVALUE. We set SYM_HASH to the hash table entry. We set
679 OVERRIDE if the old symbol is overriding a new definition. We set
680 TYPE_CHANGE_OK if it is OK for the type to change. We set
681 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
682 change, we mean that we shouldn't warn if the type or size does
683 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
687 _bfd_elf_merge_symbol (abfd
, info
, name
, sym
, psec
, pvalue
, sym_hash
, skip
,
688 override
, type_change_ok
, size_change_ok
, dt_needed
)
690 struct bfd_link_info
*info
;
692 Elf_Internal_Sym
*sym
;
695 struct elf_link_hash_entry
**sym_hash
;
697 bfd_boolean
*override
;
698 bfd_boolean
*type_change_ok
;
699 bfd_boolean
*size_change_ok
;
700 bfd_boolean dt_needed
;
703 struct elf_link_hash_entry
*h
;
704 struct elf_link_hash_entry
*flip
;
707 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
708 bfd_boolean newweakdef
, oldweakdef
, newweakundef
, oldweakundef
;
714 bind
= ELF_ST_BIND (sym
->st_info
);
716 if (! bfd_is_und_section (sec
))
717 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
719 h
= ((struct elf_link_hash_entry
*)
720 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
725 /* This code is for coping with dynamic objects, and is only useful
726 if we are doing an ELF link. */
727 if (info
->hash
->creator
!= abfd
->xvec
)
730 /* For merging, we only care about real symbols. */
732 while (h
->root
.type
== bfd_link_hash_indirect
733 || h
->root
.type
== bfd_link_hash_warning
)
734 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
736 /* If we just created the symbol, mark it as being an ELF symbol.
737 Other than that, there is nothing to do--there is no merge issue
738 with a newly defined symbol--so we just return. */
740 if (h
->root
.type
== bfd_link_hash_new
)
742 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
746 /* OLDBFD is a BFD associated with the existing symbol. */
748 switch (h
->root
.type
)
754 case bfd_link_hash_undefined
:
755 case bfd_link_hash_undefweak
:
756 oldbfd
= h
->root
.u
.undef
.abfd
;
759 case bfd_link_hash_defined
:
760 case bfd_link_hash_defweak
:
761 oldbfd
= h
->root
.u
.def
.section
->owner
;
764 case bfd_link_hash_common
:
765 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
769 /* In cases involving weak versioned symbols, we may wind up trying
770 to merge a symbol with itself. Catch that here, to avoid the
771 confusion that results if we try to override a symbol with
772 itself. The additional tests catch cases like
773 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
774 dynamic object, which we do want to handle here. */
776 && ((abfd
->flags
& DYNAMIC
) == 0
777 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
780 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
781 respectively, is from a dynamic object. */
783 if ((abfd
->flags
& DYNAMIC
) != 0)
789 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
794 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
795 indices used by MIPS ELF. */
796 switch (h
->root
.type
)
802 case bfd_link_hash_defined
:
803 case bfd_link_hash_defweak
:
804 hsec
= h
->root
.u
.def
.section
;
807 case bfd_link_hash_common
:
808 hsec
= h
->root
.u
.c
.p
->section
;
815 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
818 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
819 respectively, appear to be a definition rather than reference. */
821 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
826 if (h
->root
.type
== bfd_link_hash_undefined
827 || h
->root
.type
== bfd_link_hash_undefweak
828 || h
->root
.type
== bfd_link_hash_common
)
833 /* We need to rememeber if a symbol has a definition in a dynamic
834 object or is weak in all dynamic objects. Internal and hidden
835 visibility will make it unavailable to dynamic objects. */
836 if (newdyn
&& (h
->elf_link_hash_flags
& ELF_LINK_DYNAMIC_DEF
) == 0)
838 if (!bfd_is_und_section (sec
))
839 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_DEF
;
842 /* Check if this symbol is weak in all dynamic objects. If it
843 is the first time we see it in a dynamic object, we mark
844 if it is weak. Otherwise, we clear it. */
845 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
847 if (bind
== STB_WEAK
)
848 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_WEAK
;
850 else if (bind
!= STB_WEAK
)
851 h
->elf_link_hash_flags
&= ~ELF_LINK_DYNAMIC_WEAK
;
855 /* If the old symbol has non-default visibility, we ignore the new
856 definition from a dynamic object. */
858 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
859 && !bfd_is_und_section (sec
))
862 /* Make sure this symbol is dynamic. */
863 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
864 /* A protected symbol has external availability. Make sure it is
867 FIXME: Should we check type and size for protected symbol? */
868 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
869 return _bfd_elf_link_record_dynamic_symbol (info
, h
);
874 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
875 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
877 /* If the new symbol with non-default visibility comes from a
878 relocatable file and the old definition comes from a dynamic
879 object, we remove the old definition. */
880 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
882 h
->root
.type
= bfd_link_hash_new
;
883 h
->root
.u
.undef
.abfd
= NULL
;
884 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
886 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
887 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
889 /* FIXME: Should we check type and size for protected symbol? */
895 /* We need to treat weak definiton right, depending on if there is a
896 definition from a dynamic object. */
897 if (bind
== STB_WEAK
)
902 newweakundef
= FALSE
;
911 newweakdef
= newweakundef
= FALSE
;
913 /* If the new weak definition comes from a relocatable file and the
914 old symbol comes from a dynamic object, we treat the new one as
916 if (newweakdef
&& !newdyn
&& olddyn
)
919 if (h
->root
.type
== bfd_link_hash_defweak
)
922 oldweakundef
= FALSE
;
924 else if (h
->root
.type
== bfd_link_hash_undefweak
)
930 oldweakdef
= oldweakundef
= FALSE
;
932 /* If the old weak definition comes from a relocatable file and the
933 new symbol comes from a dynamic object, we treat the old one as
935 if (oldweakdef
&& !olddyn
&& newdyn
)
938 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
939 symbol, respectively, appears to be a common symbol in a dynamic
940 object. If a symbol appears in an uninitialized section, and is
941 not weak, and is not a function, then it may be a common symbol
942 which was resolved when the dynamic object was created. We want
943 to treat such symbols specially, because they raise special
944 considerations when setting the symbol size: if the symbol
945 appears as a common symbol in a regular object, and the size in
946 the regular object is larger, we must make sure that we use the
947 larger size. This problematic case can always be avoided in C,
948 but it must be handled correctly when using Fortran shared
951 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
952 likewise for OLDDYNCOMMON and OLDDEF.
954 Note that this test is just a heuristic, and that it is quite
955 possible to have an uninitialized symbol in a shared object which
956 is really a definition, rather than a common symbol. This could
957 lead to some minor confusion when the symbol really is a common
958 symbol in some regular object. However, I think it will be
963 && (sec
->flags
& SEC_ALLOC
) != 0
964 && (sec
->flags
& SEC_LOAD
) == 0
968 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
971 newdyncommon
= FALSE
;
975 && h
->root
.type
== bfd_link_hash_defined
976 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
977 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
978 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
980 && h
->type
!= STT_FUNC
)
983 olddyncommon
= FALSE
;
985 /* It's OK to change the type if either the existing symbol or the
986 new symbol is weak unless it comes from a DT_NEEDED entry of
987 a shared object, in which case, the DT_NEEDED entry may not be
988 required at the run time. */
990 if ((! dt_needed
&& oldweakdef
)
994 *type_change_ok
= TRUE
;
996 /* It's OK to change the size if either the existing symbol or the
997 new symbol is weak, or if the old symbol is undefined. */
1000 || h
->root
.type
== bfd_link_hash_undefined
)
1001 *size_change_ok
= TRUE
;
1003 /* If both the old and the new symbols look like common symbols in a
1004 dynamic object, set the size of the symbol to the larger of the
1009 && sym
->st_size
!= h
->size
)
1011 /* Since we think we have two common symbols, issue a multiple
1012 common warning if desired. Note that we only warn if the
1013 size is different. If the size is the same, we simply let
1014 the old symbol override the new one as normally happens with
1015 symbols defined in dynamic objects. */
1017 if (! ((*info
->callbacks
->multiple_common
)
1018 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1019 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1022 if (sym
->st_size
> h
->size
)
1023 h
->size
= sym
->st_size
;
1025 *size_change_ok
= TRUE
;
1028 /* If we are looking at a dynamic object, and we have found a
1029 definition, we need to see if the symbol was already defined by
1030 some other object. If so, we want to use the existing
1031 definition, and we do not want to report a multiple symbol
1032 definition error; we do this by clobbering *PSEC to be
1033 bfd_und_section_ptr.
1035 We treat a common symbol as a definition if the symbol in the
1036 shared library is a function, since common symbols always
1037 represent variables; this can cause confusion in principle, but
1038 any such confusion would seem to indicate an erroneous program or
1039 shared library. We also permit a common symbol in a regular
1040 object to override a weak symbol in a shared object.
1042 We prefer a non-weak definition in a shared library to a weak
1043 definition in the executable unless it comes from a DT_NEEDED
1044 entry of a shared object, in which case, the DT_NEEDED entry
1045 may not be required at the run time. */
1050 || (h
->root
.type
== bfd_link_hash_common
1053 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)))
1061 newdyncommon
= FALSE
;
1063 *psec
= sec
= bfd_und_section_ptr
;
1064 *size_change_ok
= TRUE
;
1066 /* If we get here when the old symbol is a common symbol, then
1067 we are explicitly letting it override a weak symbol or
1068 function in a dynamic object, and we don't want to warn about
1069 a type change. If the old symbol is a defined symbol, a type
1070 change warning may still be appropriate. */
1072 if (h
->root
.type
== bfd_link_hash_common
)
1073 *type_change_ok
= TRUE
;
1076 /* Handle the special case of an old common symbol merging with a
1077 new symbol which looks like a common symbol in a shared object.
1078 We change *PSEC and *PVALUE to make the new symbol look like a
1079 common symbol, and let _bfd_generic_link_add_one_symbol will do
1083 && h
->root
.type
== bfd_link_hash_common
)
1087 newdyncommon
= FALSE
;
1088 *pvalue
= sym
->st_size
;
1089 *psec
= sec
= bfd_com_section_ptr
;
1090 *size_change_ok
= TRUE
;
1093 /* If the old symbol is from a dynamic object, and the new symbol is
1094 a definition which is not from a dynamic object, then the new
1095 symbol overrides the old symbol. Symbols from regular files
1096 always take precedence over symbols from dynamic objects, even if
1097 they are defined after the dynamic object in the link.
1099 As above, we again permit a common symbol in a regular object to
1100 override a definition in a shared object if the shared object
1101 symbol is a function or is weak.
1103 As above, we permit a non-weak definition in a shared object to
1104 override a weak definition in a regular object. */
1109 || (bfd_is_com_section (sec
)
1110 && (oldweakdef
|| h
->type
== STT_FUNC
)))
1113 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1114 && ((!newweakdef
&& !newweakundef
) || oldweakdef
))
1116 /* Change the hash table entry to undefined, and let
1117 _bfd_generic_link_add_one_symbol do the right thing with the
1120 h
->root
.type
= bfd_link_hash_undefined
;
1121 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1122 *size_change_ok
= TRUE
;
1125 olddyncommon
= FALSE
;
1127 /* We again permit a type change when a common symbol may be
1128 overriding a function. */
1130 if (bfd_is_com_section (sec
))
1131 *type_change_ok
= TRUE
;
1133 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1136 /* This union may have been set to be non-NULL when this symbol
1137 was seen in a dynamic object. We must force the union to be
1138 NULL, so that it is correct for a regular symbol. */
1139 h
->verinfo
.vertree
= NULL
;
1142 /* Handle the special case of a new common symbol merging with an
1143 old symbol that looks like it might be a common symbol defined in
1144 a shared object. Note that we have already handled the case in
1145 which a new common symbol should simply override the definition
1146 in the shared library. */
1149 && bfd_is_com_section (sec
)
1152 /* It would be best if we could set the hash table entry to a
1153 common symbol, but we don't know what to use for the section
1154 or the alignment. */
1155 if (! ((*info
->callbacks
->multiple_common
)
1156 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1157 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1160 /* If the predumed common symbol in the dynamic object is
1161 larger, pretend that the new symbol has its size. */
1163 if (h
->size
> *pvalue
)
1166 /* FIXME: We no longer know the alignment required by the symbol
1167 in the dynamic object, so we just wind up using the one from
1168 the regular object. */
1171 olddyncommon
= FALSE
;
1173 h
->root
.type
= bfd_link_hash_undefined
;
1174 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1176 *size_change_ok
= TRUE
;
1177 *type_change_ok
= TRUE
;
1179 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1182 h
->verinfo
.vertree
= NULL
;
1187 /* Handle the case where we had a versioned symbol in a dynamic
1188 library and now find a definition in a normal object. In this
1189 case, we make the versioned symbol point to the normal one. */
1190 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1191 flip
->root
.type
= h
->root
.type
;
1192 h
->root
.type
= bfd_link_hash_indirect
;
1193 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1194 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1195 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1196 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1198 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
1199 flip
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1203 /* Handle the special case of a weak definition in a regular object
1204 followed by a non-weak definition in a shared object. In this
1205 case, we prefer the definition in the shared object unless it
1206 comes from a DT_NEEDED entry of a shared object, in which case,
1207 the DT_NEEDED entry may not be required at the run time. */
1216 /* To make this work we have to frob the flags so that the rest
1217 of the code does not think we are using the regular
1219 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
1220 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
1221 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
1222 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1223 h
->elf_link_hash_flags
&= ~ (ELF_LINK_HASH_DEF_REGULAR
1224 | ELF_LINK_HASH_DEF_DYNAMIC
);
1226 /* If H is the target of an indirection, we want the caller to
1227 use H rather than the indirect symbol. Otherwise if we are
1228 defining a new indirect symbol we will wind up attaching it
1229 to the entry we are overriding. */
1233 /* Handle the special case of a non-weak definition in a shared
1234 object followed by a weak definition in a regular object. In
1235 this case we prefer the definition in the shared object. To make
1236 this work we have to tell the caller to not treat the new symbol
1243 && (newweakdef
|| newweakundef
))
1249 /* This function is called to create an indirect symbol from the
1250 default for the symbol with the default version if needed. The
1251 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1252 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
1253 indicates if it comes from a DT_NEEDED entry of a shared object. */
1256 _bfd_elf_add_default_symbol (abfd
, info
, h
, name
, sym
, psec
, value
,
1257 dynsym
, override
, dt_needed
)
1259 struct bfd_link_info
*info
;
1260 struct elf_link_hash_entry
*h
;
1262 Elf_Internal_Sym
*sym
;
1265 bfd_boolean
*dynsym
;
1266 bfd_boolean override
;
1267 bfd_boolean dt_needed
;
1269 bfd_boolean type_change_ok
;
1270 bfd_boolean size_change_ok
;
1273 struct elf_link_hash_entry
*hi
;
1274 struct bfd_link_hash_entry
*bh
;
1275 struct elf_backend_data
*bed
;
1276 bfd_boolean collect
;
1277 bfd_boolean dynamic
;
1279 size_t len
, shortlen
;
1282 /* If this symbol has a version, and it is the default version, we
1283 create an indirect symbol from the default name to the fully
1284 decorated name. This will cause external references which do not
1285 specify a version to be bound to this version of the symbol. */
1286 p
= strchr (name
, ELF_VER_CHR
);
1287 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1292 /* We are overridden by an old defition. We need to check if we
1293 need to create the indirect symbol from the default name. */
1294 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1296 BFD_ASSERT (hi
!= NULL
);
1299 while (hi
->root
.type
== bfd_link_hash_indirect
1300 || hi
->root
.type
== bfd_link_hash_warning
)
1302 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1308 bed
= get_elf_backend_data (abfd
);
1309 collect
= bed
->collect
;
1310 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1312 shortlen
= p
- name
;
1313 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1314 if (shortname
== NULL
)
1316 memcpy (shortname
, name
, shortlen
);
1317 shortname
[shortlen
] = '\0';
1319 /* We are going to create a new symbol. Merge it with any existing
1320 symbol with this name. For the purposes of the merge, act as
1321 though we were defining the symbol we just defined, although we
1322 actually going to define an indirect symbol. */
1323 type_change_ok
= FALSE
;
1324 size_change_ok
= FALSE
;
1326 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1327 &hi
, &skip
, &override
, &type_change_ok
,
1328 &size_change_ok
, dt_needed
))
1337 if (! (_bfd_generic_link_add_one_symbol
1338 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1339 (bfd_vma
) 0, name
, FALSE
, collect
, &bh
)))
1341 hi
= (struct elf_link_hash_entry
*) bh
;
1345 /* In this case the symbol named SHORTNAME is overriding the
1346 indirect symbol we want to add. We were planning on making
1347 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1348 is the name without a version. NAME is the fully versioned
1349 name, and it is the default version.
1351 Overriding means that we already saw a definition for the
1352 symbol SHORTNAME in a regular object, and it is overriding
1353 the symbol defined in the dynamic object.
1355 When this happens, we actually want to change NAME, the
1356 symbol we just added, to refer to SHORTNAME. This will cause
1357 references to NAME in the shared object to become references
1358 to SHORTNAME in the regular object. This is what we expect
1359 when we override a function in a shared object: that the
1360 references in the shared object will be mapped to the
1361 definition in the regular object. */
1363 while (hi
->root
.type
== bfd_link_hash_indirect
1364 || hi
->root
.type
== bfd_link_hash_warning
)
1365 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1367 h
->root
.type
= bfd_link_hash_indirect
;
1368 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1369 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1371 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1372 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1373 if (hi
->elf_link_hash_flags
1374 & (ELF_LINK_HASH_REF_REGULAR
1375 | ELF_LINK_HASH_DEF_REGULAR
))
1377 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1382 /* Now set HI to H, so that the following code will set the
1383 other fields correctly. */
1387 /* If there is a duplicate definition somewhere, then HI may not
1388 point to an indirect symbol. We will have reported an error to
1389 the user in that case. */
1391 if (hi
->root
.type
== bfd_link_hash_indirect
)
1393 struct elf_link_hash_entry
*ht
;
1395 /* If the symbol became indirect, then we assume that we have
1396 not seen a definition before. */
1397 BFD_ASSERT ((hi
->elf_link_hash_flags
1398 & (ELF_LINK_HASH_DEF_DYNAMIC
1399 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1401 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1402 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1404 /* See if the new flags lead us to realize that the symbol must
1411 || ((hi
->elf_link_hash_flags
1412 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1417 if ((hi
->elf_link_hash_flags
1418 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1424 /* We also need to define an indirection from the nondefault version
1428 len
= strlen (name
);
1429 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1430 if (shortname
== NULL
)
1432 memcpy (shortname
, name
, shortlen
);
1433 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1435 /* Once again, merge with any existing symbol. */
1436 type_change_ok
= FALSE
;
1437 size_change_ok
= FALSE
;
1439 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1440 &hi
, &skip
, &override
, &type_change_ok
,
1441 &size_change_ok
, dt_needed
))
1449 /* Here SHORTNAME is a versioned name, so we don't expect to see
1450 the type of override we do in the case above unless it is
1451 overridden by a versioned definiton. */
1452 if (hi
->root
.type
!= bfd_link_hash_defined
1453 && hi
->root
.type
!= bfd_link_hash_defweak
)
1454 (*_bfd_error_handler
)
1455 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1456 bfd_archive_filename (abfd
), shortname
);
1461 if (! (_bfd_generic_link_add_one_symbol
1462 (info
, abfd
, shortname
, BSF_INDIRECT
,
1463 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, FALSE
, collect
, &bh
)))
1465 hi
= (struct elf_link_hash_entry
*) bh
;
1467 /* If there is a duplicate definition somewhere, then HI may not
1468 point to an indirect symbol. We will have reported an error
1469 to the user in that case. */
1471 if (hi
->root
.type
== bfd_link_hash_indirect
)
1473 /* If the symbol became indirect, then we assume that we have
1474 not seen a definition before. */
1475 BFD_ASSERT ((hi
->elf_link_hash_flags
1476 & (ELF_LINK_HASH_DEF_DYNAMIC
1477 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1479 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1481 /* See if the new flags lead us to realize that the symbol
1488 || ((hi
->elf_link_hash_flags
1489 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1494 if ((hi
->elf_link_hash_flags
1495 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1505 /* This routine is used to export all defined symbols into the dynamic
1506 symbol table. It is called via elf_link_hash_traverse. */
1509 _bfd_elf_export_symbol (h
, data
)
1510 struct elf_link_hash_entry
*h
;
1513 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
1515 /* Ignore indirect symbols. These are added by the versioning code. */
1516 if (h
->root
.type
== bfd_link_hash_indirect
)
1519 if (h
->root
.type
== bfd_link_hash_warning
)
1520 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1522 if (h
->dynindx
== -1
1523 && (h
->elf_link_hash_flags
1524 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
1526 struct bfd_elf_version_tree
*t
;
1527 struct bfd_elf_version_expr
*d
;
1529 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1531 if (t
->globals
!= NULL
)
1533 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
1535 if ((*d
->match
) (d
, h
->root
.root
.string
))
1540 if (t
->locals
!= NULL
)
1542 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
1544 if ((*d
->match
) (d
, h
->root
.root
.string
))
1553 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1564 /* Look through the symbols which are defined in other shared
1565 libraries and referenced here. Update the list of version
1566 dependencies. This will be put into the .gnu.version_r section.
1567 This function is called via elf_link_hash_traverse. */
1570 _bfd_elf_link_find_version_dependencies (h
, data
)
1571 struct elf_link_hash_entry
*h
;
1574 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
1575 Elf_Internal_Verneed
*t
;
1576 Elf_Internal_Vernaux
*a
;
1579 if (h
->root
.type
== bfd_link_hash_warning
)
1580 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1582 /* We only care about symbols defined in shared objects with version
1584 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
1585 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
1587 || h
->verinfo
.verdef
== NULL
)
1590 /* See if we already know about this version. */
1591 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1593 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1596 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1597 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1603 /* This is a new version. Add it to tree we are building. */
1608 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
1611 rinfo
->failed
= TRUE
;
1615 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1616 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1617 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1621 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
1623 /* Note that we are copying a string pointer here, and testing it
1624 above. If bfd_elf_string_from_elf_section is ever changed to
1625 discard the string data when low in memory, this will have to be
1627 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1629 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1630 a
->vna_nextptr
= t
->vn_auxptr
;
1632 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1635 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1642 /* Figure out appropriate versions for all the symbols. We may not
1643 have the version number script until we have read all of the input
1644 files, so until that point we don't know which symbols should be
1645 local. This function is called via elf_link_hash_traverse. */
1648 _bfd_elf_link_assign_sym_version (h
, data
)
1649 struct elf_link_hash_entry
*h
;
1652 struct elf_assign_sym_version_info
*sinfo
;
1653 struct bfd_link_info
*info
;
1654 struct elf_backend_data
*bed
;
1655 struct elf_info_failed eif
;
1659 sinfo
= (struct elf_assign_sym_version_info
*) data
;
1662 if (h
->root
.type
== bfd_link_hash_warning
)
1663 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1665 /* Fix the symbol flags. */
1668 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1671 sinfo
->failed
= TRUE
;
1675 /* We only need version numbers for symbols defined in regular
1677 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1680 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1681 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1682 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1684 struct bfd_elf_version_tree
*t
;
1689 /* There are two consecutive ELF_VER_CHR characters if this is
1690 not a hidden symbol. */
1692 if (*p
== ELF_VER_CHR
)
1698 /* If there is no version string, we can just return out. */
1702 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1706 /* Look for the version. If we find it, it is no longer weak. */
1707 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1709 if (strcmp (t
->name
, p
) == 0)
1713 struct bfd_elf_version_expr
*d
;
1715 len
= p
- h
->root
.root
.string
;
1716 alc
= bfd_malloc ((bfd_size_type
) len
);
1719 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1720 alc
[len
- 1] = '\0';
1721 if (alc
[len
- 2] == ELF_VER_CHR
)
1722 alc
[len
- 2] = '\0';
1724 h
->verinfo
.vertree
= t
;
1728 if (t
->globals
!= NULL
)
1730 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
1731 if ((*d
->match
) (d
, alc
))
1735 /* See if there is anything to force this symbol to
1737 if (d
== NULL
&& t
->locals
!= NULL
)
1739 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
1741 if ((*d
->match
) (d
, alc
))
1743 if (h
->dynindx
!= -1
1745 && ! info
->export_dynamic
)
1747 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1760 /* If we are building an application, we need to create a
1761 version node for this version. */
1762 if (t
== NULL
&& ! info
->shared
)
1764 struct bfd_elf_version_tree
**pp
;
1767 /* If we aren't going to export this symbol, we don't need
1768 to worry about it. */
1769 if (h
->dynindx
== -1)
1773 t
= ((struct bfd_elf_version_tree
*)
1774 bfd_alloc (sinfo
->output_bfd
, amt
));
1777 sinfo
->failed
= TRUE
;
1786 t
->name_indx
= (unsigned int) -1;
1790 /* Don't count anonymous version tag. */
1791 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1793 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1795 t
->vernum
= version_index
;
1799 h
->verinfo
.vertree
= t
;
1803 /* We could not find the version for a symbol when
1804 generating a shared archive. Return an error. */
1805 (*_bfd_error_handler
)
1806 (_("%s: undefined versioned symbol name %s"),
1807 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
1808 bfd_set_error (bfd_error_bad_value
);
1809 sinfo
->failed
= TRUE
;
1814 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
1817 /* If we don't have a version for this symbol, see if we can find
1819 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1821 struct bfd_elf_version_tree
*t
;
1822 struct bfd_elf_version_tree
*local_ver
;
1823 struct bfd_elf_version_expr
*d
;
1825 /* See if can find what version this symbol is in. If the
1826 symbol is supposed to be local, then don't actually register
1829 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1831 if (t
->globals
!= NULL
)
1833 bfd_boolean matched
;
1836 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
1838 if ((*d
->match
) (d
, h
->root
.root
.string
))
1844 /* There is a version without definition. Make
1845 the symbol the default definition for this
1847 h
->verinfo
.vertree
= t
;
1858 /* There is no undefined version for this symbol. Hide the
1860 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1863 if (t
->locals
!= NULL
)
1865 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
1867 /* If the match is "*", keep looking for a more
1868 explicit, perhaps even global, match. */
1869 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
1871 else if ((*d
->match
) (d
, h
->root
.root
.string
))
1883 if (local_ver
!= NULL
)
1885 h
->verinfo
.vertree
= local_ver
;
1886 if (h
->dynindx
!= -1
1888 && ! info
->export_dynamic
)
1890 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1898 /* Create a special linker section, or return a pointer to a linker
1899 section already created */
1901 elf_linker_section_t
*
1902 _bfd_elf_create_linker_section (abfd
, info
, which
, defaults
)
1904 struct bfd_link_info
*info
;
1905 enum elf_linker_section_enum which
;
1906 elf_linker_section_t
*defaults
;
1908 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
1909 elf_linker_section_t
*lsect
;
1911 /* Record the first bfd section that needs the special section */
1913 dynobj
= elf_hash_table (info
)->dynobj
= abfd
;
1915 /* If this is the first time, create the section */
1916 lsect
= elf_linker_section (dynobj
, which
);
1920 bfd_size_type amt
= sizeof (elf_linker_section_t
);
1922 lsect
= (elf_linker_section_t
*) bfd_alloc (dynobj
, amt
);
1925 elf_linker_section (dynobj
, which
) = lsect
;
1926 lsect
->which
= which
;
1927 lsect
->hole_written_p
= FALSE
;
1929 /* See if the sections already exist */
1930 lsect
->section
= s
= bfd_get_section_by_name (dynobj
, lsect
->name
);
1931 if (!s
|| (s
->flags
& defaults
->flags
) != defaults
->flags
)
1933 lsect
->section
= s
= bfd_make_section_anyway (dynobj
, lsect
->name
);
1936 return (elf_linker_section_t
*)0;
1938 bfd_set_section_flags (dynobj
, s
, defaults
->flags
);
1939 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
1941 else if (bfd_get_section_alignment (dynobj
, s
) < lsect
->alignment
)
1942 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
1944 s
->_raw_size
= align_power (s
->_raw_size
, lsect
->alignment
);
1946 /* Is there a hole we have to provide? If so check whether the
1947 segment is too big already */
1948 if (lsect
->hole_size
)
1950 lsect
->hole_offset
= s
->_raw_size
;
1951 s
->_raw_size
+= lsect
->hole_size
;
1952 if (lsect
->hole_offset
> lsect
->max_hole_offset
)
1954 (*_bfd_error_handler
)
1955 (_("%s: Section %s is too large to add hole of %ld bytes"),
1956 bfd_get_filename (abfd
),
1958 (long) lsect
->hole_size
);
1960 bfd_set_error (bfd_error_bad_value
);
1961 return (elf_linker_section_t
*)0;
1966 fprintf (stderr
, "Creating section %s, current size = %ld\n",
1967 lsect
->name
, (long)s
->_raw_size
);
1970 if (lsect
->sym_name
)
1972 struct elf_link_hash_entry
*h
;
1973 struct bfd_link_hash_entry
*bh
;
1976 fprintf (stderr
, "Adding %s to section %s\n",
1980 bh
= bfd_link_hash_lookup (info
->hash
, lsect
->sym_name
,
1981 FALSE
, FALSE
, FALSE
);
1983 if ((bh
== NULL
|| bh
->type
== bfd_link_hash_undefined
)
1984 && !(_bfd_generic_link_add_one_symbol
1985 (info
, abfd
, lsect
->sym_name
, BSF_GLOBAL
, s
,
1987 ? s
->_raw_size
- lsect
->hole_size
+ lsect
->sym_offset
1988 : lsect
->sym_offset
),
1989 (const char *) NULL
, FALSE
,
1990 get_elf_backend_data (abfd
)->collect
, &bh
)))
1991 return (elf_linker_section_t
*) 0;
1992 h
= (struct elf_link_hash_entry
*) bh
;
1994 if ((defaults
->which
!= LINKER_SECTION_SDATA
)
1995 && (defaults
->which
!= LINKER_SECTION_SDATA2
))
1996 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_DYNAMIC
;
1998 h
->type
= STT_OBJECT
;
1999 lsect
->sym_hash
= h
;
2002 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2003 return (elf_linker_section_t
*) 0;
2008 /* This does not make sense. The sections which may exist in the
2009 object file have nothing to do with the sections we want to
2012 /* Find the related sections if they have been created */
2013 if (lsect
->bss_name
&& !lsect
->bss_section
)
2014 lsect
->bss_section
= bfd_get_section_by_name (dynobj
, lsect
->bss_name
);
2016 if (lsect
->rel_name
&& !lsect
->rel_section
)
2017 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
2023 /* Find a linker generated pointer with a given addend and type. */
2025 elf_linker_section_pointers_t
*
2026 _bfd_elf_find_pointer_linker_section (linker_pointers
, addend
, which
)
2027 elf_linker_section_pointers_t
*linker_pointers
;
2029 elf_linker_section_enum_t which
;
2031 for ( ; linker_pointers
!= NULL
; linker_pointers
= linker_pointers
->next
)
2033 if (which
== linker_pointers
->which
&& addend
== linker_pointers
->addend
)
2034 return linker_pointers
;
2037 return (elf_linker_section_pointers_t
*)0;
2040 /* Make the .rela section corresponding to the generated linker section. */
2043 _bfd_elf_make_linker_section_rela (dynobj
, lsect
, alignment
)
2045 elf_linker_section_t
*lsect
;
2048 if (lsect
->rel_section
)
2051 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
2052 if (lsect
->rel_section
== NULL
)
2054 lsect
->rel_section
= bfd_make_section (dynobj
, lsect
->rel_name
);
2055 if (lsect
->rel_section
== NULL
2056 || ! bfd_set_section_flags (dynobj
,
2062 | SEC_LINKER_CREATED
2064 || ! bfd_set_section_alignment (dynobj
, lsect
->rel_section
, alignment
))
2071 /* Read and swap the relocs from the section indicated by SHDR. This
2072 may be either a REL or a RELA section. The relocations are
2073 translated into RELA relocations and stored in INTERNAL_RELOCS,
2074 which should have already been allocated to contain enough space.
2075 The EXTERNAL_RELOCS are a buffer where the external form of the
2076 relocations should be stored.
2078 Returns FALSE if something goes wrong. */
2081 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2084 Elf_Internal_Shdr
*shdr
;
2085 PTR external_relocs
;
2086 Elf_Internal_Rela
*internal_relocs
;
2088 struct elf_backend_data
*bed
;
2089 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
2090 const bfd_byte
*erela
;
2091 const bfd_byte
*erelaend
;
2092 Elf_Internal_Rela
*irela
;
2094 /* If there aren't any relocations, that's OK. */
2098 /* Position ourselves at the start of the section. */
2099 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2102 /* Read the relocations. */
2103 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2106 bed
= get_elf_backend_data (abfd
);
2108 /* Convert the external relocations to the internal format. */
2109 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2110 swap_in
= bed
->s
->swap_reloc_in
;
2111 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2112 swap_in
= bed
->s
->swap_reloca_in
;
2115 bfd_set_error (bfd_error_wrong_format
);
2119 erela
= external_relocs
;
2120 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
) * shdr
->sh_entsize
;
2121 irela
= internal_relocs
;
2122 while (erela
< erelaend
)
2124 (*swap_in
) (abfd
, erela
, irela
);
2125 irela
+= bed
->s
->int_rels_per_ext_rel
;
2126 erela
+= shdr
->sh_entsize
;
2132 /* Read and swap the relocs for a section O. They may have been
2133 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2134 not NULL, they are used as buffers to read into. They are known to
2135 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2136 the return value is allocated using either malloc or bfd_alloc,
2137 according to the KEEP_MEMORY argument. If O has two relocation
2138 sections (both REL and RELA relocations), then the REL_HDR
2139 relocations will appear first in INTERNAL_RELOCS, followed by the
2140 REL_HDR2 relocations. */
2143 _bfd_elf_link_read_relocs (abfd
, o
, external_relocs
, internal_relocs
,
2147 PTR external_relocs
;
2148 Elf_Internal_Rela
*internal_relocs
;
2149 bfd_boolean keep_memory
;
2151 Elf_Internal_Shdr
*rel_hdr
;
2153 Elf_Internal_Rela
*alloc2
= NULL
;
2154 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2156 if (elf_section_data (o
)->relocs
!= NULL
)
2157 return elf_section_data (o
)->relocs
;
2159 if (o
->reloc_count
== 0)
2162 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2164 if (internal_relocs
== NULL
)
2168 size
= o
->reloc_count
;
2169 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2171 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2173 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2174 if (internal_relocs
== NULL
)
2178 if (external_relocs
== NULL
)
2180 bfd_size_type size
= rel_hdr
->sh_size
;
2182 if (elf_section_data (o
)->rel_hdr2
)
2183 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2184 alloc1
= (PTR
) bfd_malloc (size
);
2187 external_relocs
= alloc1
;
2190 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2194 if (!elf_link_read_relocs_from_section
2196 elf_section_data (o
)->rel_hdr2
,
2197 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2198 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2199 * bed
->s
->int_rels_per_ext_rel
)))
2202 /* Cache the results for next time, if we can. */
2204 elf_section_data (o
)->relocs
= internal_relocs
;
2209 /* Don't free alloc2, since if it was allocated we are passing it
2210 back (under the name of internal_relocs). */
2212 return internal_relocs
;
2222 /* Compute the size of, and allocate space for, REL_HDR which is the
2223 section header for a section containing relocations for O. */
2226 _bfd_elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
2228 Elf_Internal_Shdr
*rel_hdr
;
2231 bfd_size_type reloc_count
;
2232 bfd_size_type num_rel_hashes
;
2234 /* Figure out how many relocations there will be. */
2235 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2236 reloc_count
= elf_section_data (o
)->rel_count
;
2238 reloc_count
= elf_section_data (o
)->rel_count2
;
2240 num_rel_hashes
= o
->reloc_count
;
2241 if (num_rel_hashes
< reloc_count
)
2242 num_rel_hashes
= reloc_count
;
2244 /* That allows us to calculate the size of the section. */
2245 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2247 /* The contents field must last into write_object_contents, so we
2248 allocate it with bfd_alloc rather than malloc. Also since we
2249 cannot be sure that the contents will actually be filled in,
2250 we zero the allocated space. */
2251 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2252 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2255 /* We only allocate one set of hash entries, so we only do it the
2256 first time we are called. */
2257 if (elf_section_data (o
)->rel_hashes
== NULL
2260 struct elf_link_hash_entry
**p
;
2262 p
= ((struct elf_link_hash_entry
**)
2263 bfd_zmalloc (num_rel_hashes
2264 * sizeof (struct elf_link_hash_entry
*)));
2268 elf_section_data (o
)->rel_hashes
= p
;
2274 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2275 originated from the section given by INPUT_REL_HDR) to the
2279 _bfd_elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
2282 asection
*input_section
;
2283 Elf_Internal_Shdr
*input_rel_hdr
;
2284 Elf_Internal_Rela
*internal_relocs
;
2286 Elf_Internal_Rela
*irela
;
2287 Elf_Internal_Rela
*irelaend
;
2289 Elf_Internal_Shdr
*output_rel_hdr
;
2290 asection
*output_section
;
2291 unsigned int *rel_countp
= NULL
;
2292 struct elf_backend_data
*bed
;
2293 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
2295 output_section
= input_section
->output_section
;
2296 output_rel_hdr
= NULL
;
2298 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2299 == input_rel_hdr
->sh_entsize
)
2301 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2302 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2304 else if (elf_section_data (output_section
)->rel_hdr2
2305 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2306 == input_rel_hdr
->sh_entsize
))
2308 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2309 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2313 (*_bfd_error_handler
)
2314 (_("%s: relocation size mismatch in %s section %s"),
2315 bfd_get_filename (output_bfd
),
2316 bfd_archive_filename (input_section
->owner
),
2317 input_section
->name
);
2318 bfd_set_error (bfd_error_wrong_object_format
);
2322 bed
= get_elf_backend_data (output_bfd
);
2323 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2324 swap_out
= bed
->s
->swap_reloc_out
;
2325 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2326 swap_out
= bed
->s
->swap_reloca_out
;
2330 erel
= output_rel_hdr
->contents
;
2331 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2332 irela
= internal_relocs
;
2333 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2334 * bed
->s
->int_rels_per_ext_rel
);
2335 while (irela
< irelaend
)
2337 (*swap_out
) (output_bfd
, irela
, erel
);
2338 irela
+= bed
->s
->int_rels_per_ext_rel
;
2339 erel
+= input_rel_hdr
->sh_entsize
;
2342 /* Bump the counter, so that we know where to add the next set of
2344 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2349 /* Fix up the flags for a symbol. This handles various cases which
2350 can only be fixed after all the input files are seen. This is
2351 currently called by both adjust_dynamic_symbol and
2352 assign_sym_version, which is unnecessary but perhaps more robust in
2353 the face of future changes. */
2356 _bfd_elf_fix_symbol_flags (h
, eif
)
2357 struct elf_link_hash_entry
*h
;
2358 struct elf_info_failed
*eif
;
2360 /* If this symbol was mentioned in a non-ELF file, try to set
2361 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2362 permit a non-ELF file to correctly refer to a symbol defined in
2363 an ELF dynamic object. */
2364 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2366 while (h
->root
.type
== bfd_link_hash_indirect
)
2367 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2369 if (h
->root
.type
!= bfd_link_hash_defined
2370 && h
->root
.type
!= bfd_link_hash_defweak
)
2371 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2372 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2375 if (h
->root
.u
.def
.section
->owner
!= NULL
2376 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2377 == bfd_target_elf_flavour
))
2378 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
2379 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
2381 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2384 if (h
->dynindx
== -1
2385 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2386 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
2388 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2397 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2398 was first seen in a non-ELF file. Fortunately, if the symbol
2399 was first seen in an ELF file, we're probably OK unless the
2400 symbol was defined in a non-ELF file. Catch that case here.
2401 FIXME: We're still in trouble if the symbol was first seen in
2402 a dynamic object, and then later in a non-ELF regular object. */
2403 if ((h
->root
.type
== bfd_link_hash_defined
2404 || h
->root
.type
== bfd_link_hash_defweak
)
2405 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2406 && (h
->root
.u
.def
.section
->owner
!= NULL
2407 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2408 != bfd_target_elf_flavour
)
2409 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2410 && (h
->elf_link_hash_flags
2411 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
2412 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2415 /* If this is a final link, and the symbol was defined as a common
2416 symbol in a regular object file, and there was no definition in
2417 any dynamic object, then the linker will have allocated space for
2418 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2419 flag will not have been set. */
2420 if (h
->root
.type
== bfd_link_hash_defined
2421 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2422 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2423 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2424 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2425 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2427 /* If -Bsymbolic was used (which means to bind references to global
2428 symbols to the definition within the shared object), and this
2429 symbol was defined in a regular object, then it actually doesn't
2430 need a PLT entry. Likewise, if the symbol has non-default
2431 visibility. If the symbol has hidden or internal visibility, we
2432 will force it local. */
2433 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2434 && eif
->info
->shared
2435 && is_elf_hash_table (eif
->info
)
2436 && (eif
->info
->symbolic
2437 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2438 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2440 struct elf_backend_data
*bed
;
2441 bfd_boolean force_local
;
2443 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2445 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2446 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2447 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2450 /* If a weak undefined symbol has non-default visibility, we also
2451 hide it from the dynamic linker. */
2452 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2453 && h
->root
.type
== bfd_link_hash_undefweak
)
2455 struct elf_backend_data
*bed
;
2456 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2457 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2460 /* If this is a weak defined symbol in a dynamic object, and we know
2461 the real definition in the dynamic object, copy interesting flags
2462 over to the real definition. */
2463 if (h
->weakdef
!= NULL
)
2465 struct elf_link_hash_entry
*weakdef
;
2467 weakdef
= h
->weakdef
;
2468 if (h
->root
.type
== bfd_link_hash_indirect
)
2469 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2471 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2472 || h
->root
.type
== bfd_link_hash_defweak
);
2473 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2474 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2475 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2477 /* If the real definition is defined by a regular object file,
2478 don't do anything special. See the longer description in
2479 _bfd_elf_adjust_dynamic_symbol, below. */
2480 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2484 struct elf_backend_data
*bed
;
2486 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2487 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2494 /* Make the backend pick a good value for a dynamic symbol. This is
2495 called via elf_link_hash_traverse, and also calls itself
2499 _bfd_elf_adjust_dynamic_symbol (h
, data
)
2500 struct elf_link_hash_entry
*h
;
2503 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2505 struct elf_backend_data
*bed
;
2507 if (! is_elf_hash_table (eif
->info
))
2510 if (h
->root
.type
== bfd_link_hash_warning
)
2512 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2513 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2515 /* When warning symbols are created, they **replace** the "real"
2516 entry in the hash table, thus we never get to see the real
2517 symbol in a hash traversal. So look at it now. */
2518 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2521 /* Ignore indirect symbols. These are added by the versioning code. */
2522 if (h
->root
.type
== bfd_link_hash_indirect
)
2525 /* Fix the symbol flags. */
2526 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2529 /* If this symbol does not require a PLT entry, and it is not
2530 defined by a dynamic object, or is not referenced by a regular
2531 object, ignore it. We do have to handle a weak defined symbol,
2532 even if no regular object refers to it, if we decided to add it
2533 to the dynamic symbol table. FIXME: Do we normally need to worry
2534 about symbols which are defined by one dynamic object and
2535 referenced by another one? */
2536 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2537 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2538 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2539 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2540 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2542 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2546 /* If we've already adjusted this symbol, don't do it again. This
2547 can happen via a recursive call. */
2548 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2551 /* Don't look at this symbol again. Note that we must set this
2552 after checking the above conditions, because we may look at a
2553 symbol once, decide not to do anything, and then get called
2554 recursively later after REF_REGULAR is set below. */
2555 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2557 /* If this is a weak definition, and we know a real definition, and
2558 the real symbol is not itself defined by a regular object file,
2559 then get a good value for the real definition. We handle the
2560 real symbol first, for the convenience of the backend routine.
2562 Note that there is a confusing case here. If the real definition
2563 is defined by a regular object file, we don't get the real symbol
2564 from the dynamic object, but we do get the weak symbol. If the
2565 processor backend uses a COPY reloc, then if some routine in the
2566 dynamic object changes the real symbol, we will not see that
2567 change in the corresponding weak symbol. This is the way other
2568 ELF linkers work as well, and seems to be a result of the shared
2571 I will clarify this issue. Most SVR4 shared libraries define the
2572 variable _timezone and define timezone as a weak synonym. The
2573 tzset call changes _timezone. If you write
2574 extern int timezone;
2576 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2577 you might expect that, since timezone is a synonym for _timezone,
2578 the same number will print both times. However, if the processor
2579 backend uses a COPY reloc, then actually timezone will be copied
2580 into your process image, and, since you define _timezone
2581 yourself, _timezone will not. Thus timezone and _timezone will
2582 wind up at different memory locations. The tzset call will set
2583 _timezone, leaving timezone unchanged. */
2585 if (h
->weakdef
!= NULL
)
2587 /* If we get to this point, we know there is an implicit
2588 reference by a regular object file via the weak symbol H.
2589 FIXME: Is this really true? What if the traversal finds
2590 H->WEAKDEF before it finds H? */
2591 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2593 if (! _bfd_elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
2597 /* If a symbol has no type and no size and does not require a PLT
2598 entry, then we are probably about to do the wrong thing here: we
2599 are probably going to create a COPY reloc for an empty object.
2600 This case can arise when a shared object is built with assembly
2601 code, and the assembly code fails to set the symbol type. */
2603 && h
->type
== STT_NOTYPE
2604 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
2605 (*_bfd_error_handler
)
2606 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2607 h
->root
.root
.string
);
2609 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2610 bed
= get_elf_backend_data (dynobj
);
2611 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2620 /* Adjust all external symbols pointing into SEC_MERGE sections
2621 to reflect the object merging within the sections. */
2624 _bfd_elf_link_sec_merge_syms (h
, data
)
2625 struct elf_link_hash_entry
*h
;
2630 if (h
->root
.type
== bfd_link_hash_warning
)
2631 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2633 if ((h
->root
.type
== bfd_link_hash_defined
2634 || h
->root
.type
== bfd_link_hash_defweak
)
2635 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2636 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2638 bfd
*output_bfd
= (bfd
*) data
;
2640 h
->root
.u
.def
.value
=
2641 _bfd_merged_section_offset (output_bfd
,
2642 &h
->root
.u
.def
.section
,
2643 elf_section_data (sec
)->sec_info
,
2644 h
->root
.u
.def
.value
, (bfd_vma
) 0);