daily update
[binutils.git] / bfd / elflink.c
blobf58a67aade2f1818671afbe913cf383bcaf283f4
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. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
28 bfd_boolean
29 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
31 flagword flags;
32 asection *s;
33 struct elf_link_hash_entry *h;
34 struct bfd_link_hash_entry *bh;
35 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
36 int ptralign;
38 /* This function may be called more than once. */
39 s = bfd_get_section_by_name (abfd, ".got");
40 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
41 return TRUE;
43 switch (bed->s->arch_size)
45 case 32:
46 ptralign = 2;
47 break;
49 case 64:
50 ptralign = 3;
51 break;
53 default:
54 bfd_set_error (bfd_error_bad_value);
55 return FALSE;
58 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
59 | SEC_LINKER_CREATED);
61 s = bfd_make_section (abfd, ".got");
62 if (s == NULL
63 || !bfd_set_section_flags (abfd, s, flags)
64 || !bfd_set_section_alignment (abfd, s, ptralign))
65 return FALSE;
67 if (bed->want_got_plt)
69 s = bfd_make_section (abfd, ".got.plt");
70 if (s == NULL
71 || !bfd_set_section_flags (abfd, s, flags)
72 || !bfd_set_section_alignment (abfd, s, ptralign))
73 return FALSE;
76 if (bed->want_got_sym)
78 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
79 (or .got.plt) section. We don't do this in the linker script
80 because we don't want to define the symbol if we are not creating
81 a global offset table. */
82 bh = NULL;
83 if (!(_bfd_generic_link_add_one_symbol
84 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
85 bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh)))
86 return FALSE;
87 h = (struct elf_link_hash_entry *) bh;
88 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
89 h->type = STT_OBJECT;
91 if (! info->executable
92 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
93 return FALSE;
95 elf_hash_table (info)->hgot = h;
98 /* The first bit of the global offset table is the header. */
99 s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
101 return TRUE;
104 /* Create some sections which will be filled in with dynamic linking
105 information. ABFD is an input file which requires dynamic sections
106 to be created. The dynamic sections take up virtual memory space
107 when the final executable is run, so we need to create them before
108 addresses are assigned to the output sections. We work out the
109 actual contents and size of these sections later. */
111 bfd_boolean
112 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
114 flagword flags;
115 register asection *s;
116 struct elf_link_hash_entry *h;
117 struct bfd_link_hash_entry *bh;
118 const struct elf_backend_data *bed;
120 if (! is_elf_hash_table (info->hash))
121 return FALSE;
123 if (elf_hash_table (info)->dynamic_sections_created)
124 return TRUE;
126 /* Make sure that all dynamic sections use the same input BFD. */
127 if (elf_hash_table (info)->dynobj == NULL)
128 elf_hash_table (info)->dynobj = abfd;
129 else
130 abfd = elf_hash_table (info)->dynobj;
132 /* Note that we set the SEC_IN_MEMORY flag for all of these
133 sections. */
134 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
135 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
137 /* A dynamically linked executable has a .interp section, but a
138 shared library does not. */
139 if (info->executable)
141 s = bfd_make_section (abfd, ".interp");
142 if (s == NULL
143 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
144 return FALSE;
147 if (! info->traditional_format)
149 s = bfd_make_section (abfd, ".eh_frame_hdr");
150 if (s == NULL
151 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
152 || ! bfd_set_section_alignment (abfd, s, 2))
153 return FALSE;
154 elf_hash_table (info)->eh_info.hdr_sec = s;
157 bed = get_elf_backend_data (abfd);
159 /* Create sections to hold version informations. These are removed
160 if they are not needed. */
161 s = bfd_make_section (abfd, ".gnu.version_d");
162 if (s == NULL
163 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
164 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
165 return FALSE;
167 s = bfd_make_section (abfd, ".gnu.version");
168 if (s == NULL
169 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
170 || ! bfd_set_section_alignment (abfd, s, 1))
171 return FALSE;
173 s = bfd_make_section (abfd, ".gnu.version_r");
174 if (s == NULL
175 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
176 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
177 return FALSE;
179 s = bfd_make_section (abfd, ".dynsym");
180 if (s == NULL
181 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
182 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
183 return FALSE;
185 s = bfd_make_section (abfd, ".dynstr");
186 if (s == NULL
187 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
188 return FALSE;
190 /* Create a strtab to hold the dynamic symbol names. */
191 if (elf_hash_table (info)->dynstr == NULL)
193 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
194 if (elf_hash_table (info)->dynstr == NULL)
195 return FALSE;
198 s = bfd_make_section (abfd, ".dynamic");
199 if (s == NULL
200 || ! bfd_set_section_flags (abfd, s, flags)
201 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
202 return FALSE;
204 /* The special symbol _DYNAMIC is always set to the start of the
205 .dynamic section. This call occurs before we have processed the
206 symbols for any dynamic object, so we don't have to worry about
207 overriding a dynamic definition. We could set _DYNAMIC in a
208 linker script, but we only want to define it if we are, in fact,
209 creating a .dynamic section. We don't want to define it if there
210 is no .dynamic section, since on some ELF platforms the start up
211 code examines it to decide how to initialize the process. */
212 bh = NULL;
213 if (! (_bfd_generic_link_add_one_symbol
214 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
215 get_elf_backend_data (abfd)->collect, &bh)))
216 return FALSE;
217 h = (struct elf_link_hash_entry *) bh;
218 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
219 h->type = STT_OBJECT;
221 if (! info->executable
222 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
223 return FALSE;
225 s = bfd_make_section (abfd, ".hash");
226 if (s == NULL
227 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
228 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
229 return FALSE;
230 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
232 /* Let the backend create the rest of the sections. This lets the
233 backend set the right flags. The backend will normally create
234 the .got and .plt sections. */
235 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
236 return FALSE;
238 elf_hash_table (info)->dynamic_sections_created = TRUE;
240 return TRUE;
243 /* Create dynamic sections when linking against a dynamic object. */
245 bfd_boolean
246 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
248 flagword flags, pltflags;
249 asection *s;
250 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
253 .rel[a].bss sections. */
255 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
256 | SEC_LINKER_CREATED);
258 pltflags = flags;
259 pltflags |= SEC_CODE;
260 if (bed->plt_not_loaded)
261 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
262 if (bed->plt_readonly)
263 pltflags |= SEC_READONLY;
265 s = bfd_make_section (abfd, ".plt");
266 if (s == NULL
267 || ! bfd_set_section_flags (abfd, s, pltflags)
268 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
269 return FALSE;
271 if (bed->want_plt_sym)
273 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
274 .plt section. */
275 struct elf_link_hash_entry *h;
276 struct bfd_link_hash_entry *bh = NULL;
278 if (! (_bfd_generic_link_add_one_symbol
279 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
280 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
281 return FALSE;
282 h = (struct elf_link_hash_entry *) bh;
283 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
284 h->type = STT_OBJECT;
286 if (! info->executable
287 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
288 return FALSE;
291 s = bfd_make_section (abfd,
292 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
293 if (s == NULL
294 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
295 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
296 return FALSE;
298 if (! _bfd_elf_create_got_section (abfd, info))
299 return FALSE;
301 if (bed->want_dynbss)
303 /* The .dynbss section is a place to put symbols which are defined
304 by dynamic objects, are referenced by regular objects, and are
305 not functions. We must allocate space for them in the process
306 image and use a R_*_COPY reloc to tell the dynamic linker to
307 initialize them at run time. The linker script puts the .dynbss
308 section into the .bss section of the final image. */
309 s = bfd_make_section (abfd, ".dynbss");
310 if (s == NULL
311 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
312 return FALSE;
314 /* The .rel[a].bss section holds copy relocs. This section is not
315 normally needed. We need to create it here, though, so that the
316 linker will map it to an output section. We can't just create it
317 only if we need it, because we will not know whether we need it
318 until we have seen all the input files, and the first time the
319 main linker code calls BFD after examining all the input files
320 (size_dynamic_sections) the input sections have already been
321 mapped to the output sections. If the section turns out not to
322 be needed, we can discard it later. We will never need this
323 section when generating a shared object, since they do not use
324 copy relocs. */
325 if (! info->shared)
327 s = bfd_make_section (abfd,
328 (bed->default_use_rela_p
329 ? ".rela.bss" : ".rel.bss"));
330 if (s == NULL
331 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
332 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
333 return FALSE;
337 return TRUE;
340 /* Record a new dynamic symbol. We record the dynamic symbols as we
341 read the input files, since we need to have a list of all of them
342 before we can determine the final sizes of the output sections.
343 Note that we may actually call this function even though we are not
344 going to output any dynamic symbols; in some cases we know that a
345 symbol should be in the dynamic symbol table, but only if there is
346 one. */
348 bfd_boolean
349 _bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
350 struct elf_link_hash_entry *h)
352 if (h->dynindx == -1)
354 struct elf_strtab_hash *dynstr;
355 char *p;
356 const char *name;
357 bfd_size_type indx;
359 /* XXX: The ABI draft says the linker must turn hidden and
360 internal symbols into STB_LOCAL symbols when producing the
361 DSO. However, if ld.so honors st_other in the dynamic table,
362 this would not be necessary. */
363 switch (ELF_ST_VISIBILITY (h->other))
365 case STV_INTERNAL:
366 case STV_HIDDEN:
367 if (h->root.type != bfd_link_hash_undefined
368 && h->root.type != bfd_link_hash_undefweak)
370 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
371 return TRUE;
374 default:
375 break;
378 h->dynindx = elf_hash_table (info)->dynsymcount;
379 ++elf_hash_table (info)->dynsymcount;
381 dynstr = elf_hash_table (info)->dynstr;
382 if (dynstr == NULL)
384 /* Create a strtab to hold the dynamic symbol names. */
385 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
386 if (dynstr == NULL)
387 return FALSE;
390 /* We don't put any version information in the dynamic string
391 table. */
392 name = h->root.root.string;
393 p = strchr (name, ELF_VER_CHR);
394 if (p != NULL)
395 /* We know that the p points into writable memory. In fact,
396 there are only a few symbols that have read-only names, being
397 those like _GLOBAL_OFFSET_TABLE_ that are created specially
398 by the backends. Most symbols will have names pointing into
399 an ELF string table read from a file, or to objalloc memory. */
400 *p = 0;
402 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
404 if (p != NULL)
405 *p = ELF_VER_CHR;
407 if (indx == (bfd_size_type) -1)
408 return FALSE;
409 h->dynstr_index = indx;
412 return TRUE;
415 /* Record an assignment to a symbol made by a linker script. We need
416 this in case some dynamic object refers to this symbol. */
418 bfd_boolean
419 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
420 struct bfd_link_info *info,
421 const char *name,
422 bfd_boolean provide)
424 struct elf_link_hash_entry *h;
426 if (!is_elf_hash_table (info->hash))
427 return TRUE;
429 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
430 if (h == NULL)
431 return FALSE;
433 if (h->root.type == bfd_link_hash_new)
434 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
436 /* If this symbol is being provided by the linker script, and it is
437 currently defined by a dynamic object, but not by a regular
438 object, then mark it as undefined so that the generic linker will
439 force the correct value. */
440 if (provide
441 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
442 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
443 h->root.type = bfd_link_hash_undefined;
445 /* If this symbol is not being provided by the linker script, and it is
446 currently defined by a dynamic object, but not by a regular object,
447 then clear out any version information because the symbol will not be
448 associated with the dynamic object any more. */
449 if (!provide
450 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
451 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
452 h->verinfo.verdef = NULL;
454 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
456 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
457 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
458 || info->shared)
459 && h->dynindx == -1)
461 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
462 return FALSE;
464 /* If this is a weak defined symbol, and we know a corresponding
465 real symbol from the same dynamic object, make sure the real
466 symbol is also made into a dynamic symbol. */
467 if (h->weakdef != NULL
468 && h->weakdef->dynindx == -1)
470 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
471 return FALSE;
475 return TRUE;
478 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
479 success, and 2 on a failure caused by attempting to record a symbol
480 in a discarded section, eg. a discarded link-once section symbol. */
483 elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
484 bfd *input_bfd,
485 long input_indx)
487 bfd_size_type amt;
488 struct elf_link_local_dynamic_entry *entry;
489 struct elf_link_hash_table *eht;
490 struct elf_strtab_hash *dynstr;
491 unsigned long dynstr_index;
492 char *name;
493 Elf_External_Sym_Shndx eshndx;
494 char esym[sizeof (Elf64_External_Sym)];
496 if (! is_elf_hash_table (info->hash))
497 return 0;
499 /* See if the entry exists already. */
500 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
501 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
502 return 1;
504 amt = sizeof (*entry);
505 entry = bfd_alloc (input_bfd, amt);
506 if (entry == NULL)
507 return 0;
509 /* Go find the symbol, so that we can find it's name. */
510 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
511 1, input_indx, &entry->isym, esym, &eshndx))
513 bfd_release (input_bfd, entry);
514 return 0;
517 if (entry->isym.st_shndx != SHN_UNDEF
518 && (entry->isym.st_shndx < SHN_LORESERVE
519 || entry->isym.st_shndx > SHN_HIRESERVE))
521 asection *s;
523 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
524 if (s == NULL || bfd_is_abs_section (s->output_section))
526 /* We can still bfd_release here as nothing has done another
527 bfd_alloc. We can't do this later in this function. */
528 bfd_release (input_bfd, entry);
529 return 2;
533 name = (bfd_elf_string_from_elf_section
534 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
535 entry->isym.st_name));
537 dynstr = elf_hash_table (info)->dynstr;
538 if (dynstr == NULL)
540 /* Create a strtab to hold the dynamic symbol names. */
541 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
542 if (dynstr == NULL)
543 return 0;
546 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
547 if (dynstr_index == (unsigned long) -1)
548 return 0;
549 entry->isym.st_name = dynstr_index;
551 eht = elf_hash_table (info);
553 entry->next = eht->dynlocal;
554 eht->dynlocal = entry;
555 entry->input_bfd = input_bfd;
556 entry->input_indx = input_indx;
557 eht->dynsymcount++;
559 /* Whatever binding the symbol had before, it's now local. */
560 entry->isym.st_info
561 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
563 /* The dynindx will be set at the end of size_dynamic_sections. */
565 return 1;
568 /* Return the dynindex of a local dynamic symbol. */
570 long
571 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
572 bfd *input_bfd,
573 long input_indx)
575 struct elf_link_local_dynamic_entry *e;
577 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
578 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
579 return e->dynindx;
580 return -1;
583 /* This function is used to renumber the dynamic symbols, if some of
584 them are removed because they are marked as local. This is called
585 via elf_link_hash_traverse. */
587 static bfd_boolean
588 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
589 void *data)
591 size_t *count = data;
593 if (h->root.type == bfd_link_hash_warning)
594 h = (struct elf_link_hash_entry *) h->root.u.i.link;
596 if (h->dynindx != -1)
597 h->dynindx = ++(*count);
599 return TRUE;
602 /* Assign dynsym indices. In a shared library we generate a section
603 symbol for each output section, which come first. Next come all of
604 the back-end allocated local dynamic syms, followed by the rest of
605 the global symbols. */
607 unsigned long
608 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
610 unsigned long dynsymcount = 0;
612 if (info->shared)
614 asection *p;
615 for (p = output_bfd->sections; p ; p = p->next)
616 if ((p->flags & SEC_EXCLUDE) == 0)
617 elf_section_data (p)->dynindx = ++dynsymcount;
620 if (elf_hash_table (info)->dynlocal)
622 struct elf_link_local_dynamic_entry *p;
623 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
624 p->dynindx = ++dynsymcount;
627 elf_link_hash_traverse (elf_hash_table (info),
628 elf_link_renumber_hash_table_dynsyms,
629 &dynsymcount);
631 /* There is an unused NULL entry at the head of the table which
632 we must account for in our count. Unless there weren't any
633 symbols, which means we'll have no table at all. */
634 if (dynsymcount != 0)
635 ++dynsymcount;
637 return elf_hash_table (info)->dynsymcount = dynsymcount;
640 /* This function is called when we want to define a new symbol. It
641 handles the various cases which arise when we find a definition in
642 a dynamic object, or when there is already a definition in a
643 dynamic object. The new symbol is described by NAME, SYM, PSEC,
644 and PVALUE. We set SYM_HASH to the hash table entry. We set
645 OVERRIDE if the old symbol is overriding a new definition. We set
646 TYPE_CHANGE_OK if it is OK for the type to change. We set
647 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
648 change, we mean that we shouldn't warn if the type or size does
649 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
650 a shared object. */
652 bfd_boolean
653 _bfd_elf_merge_symbol (bfd *abfd,
654 struct bfd_link_info *info,
655 const char *name,
656 Elf_Internal_Sym *sym,
657 asection **psec,
658 bfd_vma *pvalue,
659 struct elf_link_hash_entry **sym_hash,
660 bfd_boolean *skip,
661 bfd_boolean *override,
662 bfd_boolean *type_change_ok,
663 bfd_boolean *size_change_ok,
664 bfd_boolean dt_needed)
666 asection *sec;
667 struct elf_link_hash_entry *h;
668 struct elf_link_hash_entry *flip;
669 int bind;
670 bfd *oldbfd;
671 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
672 bfd_boolean newweakdef, oldweakdef, newweakundef, oldweakundef;
674 *skip = FALSE;
675 *override = FALSE;
677 sec = *psec;
678 bind = ELF_ST_BIND (sym->st_info);
680 if (! bfd_is_und_section (sec))
681 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
682 else
683 h = ((struct elf_link_hash_entry *)
684 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
685 if (h == NULL)
686 return FALSE;
687 *sym_hash = h;
689 /* This code is for coping with dynamic objects, and is only useful
690 if we are doing an ELF link. */
691 if (info->hash->creator != abfd->xvec)
692 return TRUE;
694 /* For merging, we only care about real symbols. */
696 while (h->root.type == bfd_link_hash_indirect
697 || h->root.type == bfd_link_hash_warning)
698 h = (struct elf_link_hash_entry *) h->root.u.i.link;
700 /* If we just created the symbol, mark it as being an ELF symbol.
701 Other than that, there is nothing to do--there is no merge issue
702 with a newly defined symbol--so we just return. */
704 if (h->root.type == bfd_link_hash_new)
706 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
707 return TRUE;
710 /* OLDBFD is a BFD associated with the existing symbol. */
712 switch (h->root.type)
714 default:
715 oldbfd = NULL;
716 break;
718 case bfd_link_hash_undefined:
719 case bfd_link_hash_undefweak:
720 oldbfd = h->root.u.undef.abfd;
721 break;
723 case bfd_link_hash_defined:
724 case bfd_link_hash_defweak:
725 oldbfd = h->root.u.def.section->owner;
726 break;
728 case bfd_link_hash_common:
729 oldbfd = h->root.u.c.p->section->owner;
730 break;
733 /* In cases involving weak versioned symbols, we may wind up trying
734 to merge a symbol with itself. Catch that here, to avoid the
735 confusion that results if we try to override a symbol with
736 itself. The additional tests catch cases like
737 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
738 dynamic object, which we do want to handle here. */
739 if (abfd == oldbfd
740 && ((abfd->flags & DYNAMIC) == 0
741 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
742 return TRUE;
744 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
745 respectively, is from a dynamic object. */
747 if ((abfd->flags & DYNAMIC) != 0)
748 newdyn = TRUE;
749 else
750 newdyn = FALSE;
752 if (oldbfd != NULL)
753 olddyn = (oldbfd->flags & DYNAMIC) != 0;
754 else
756 asection *hsec;
758 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
759 indices used by MIPS ELF. */
760 switch (h->root.type)
762 default:
763 hsec = NULL;
764 break;
766 case bfd_link_hash_defined:
767 case bfd_link_hash_defweak:
768 hsec = h->root.u.def.section;
769 break;
771 case bfd_link_hash_common:
772 hsec = h->root.u.c.p->section;
773 break;
776 if (hsec == NULL)
777 olddyn = FALSE;
778 else
779 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
782 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
783 respectively, appear to be a definition rather than reference. */
785 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
786 newdef = FALSE;
787 else
788 newdef = TRUE;
790 if (h->root.type == bfd_link_hash_undefined
791 || h->root.type == bfd_link_hash_undefweak
792 || h->root.type == bfd_link_hash_common)
793 olddef = FALSE;
794 else
795 olddef = TRUE;
797 /* We need to remember if a symbol has a definition in a dynamic
798 object or is weak in all dynamic objects. Internal and hidden
799 visibility will make it unavailable to dynamic objects. */
800 if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
802 if (!bfd_is_und_section (sec))
803 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF;
804 else
806 /* Check if this symbol is weak in all dynamic objects. If it
807 is the first time we see it in a dynamic object, we mark
808 if it is weak. Otherwise, we clear it. */
809 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
811 if (bind == STB_WEAK)
812 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK;
814 else if (bind != STB_WEAK)
815 h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK;
819 /* If the old symbol has non-default visibility, we ignore the new
820 definition from a dynamic object. */
821 if (newdyn
822 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
823 && !bfd_is_und_section (sec))
825 *skip = TRUE;
826 /* Make sure this symbol is dynamic. */
827 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
828 /* A protected symbol has external availability. Make sure it is
829 recorded as dynamic.
831 FIXME: Should we check type and size for protected symbol? */
832 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
833 return _bfd_elf_link_record_dynamic_symbol (info, h);
834 else
835 return TRUE;
837 else if (!newdyn
838 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
839 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
841 /* If the new symbol with non-default visibility comes from a
842 relocatable file and the old definition comes from a dynamic
843 object, we remove the old definition. */
844 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
845 h = *sym_hash;
846 h->root.type = bfd_link_hash_new;
847 h->root.u.undef.abfd = NULL;
848 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
850 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
851 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC
852 | ELF_LINK_DYNAMIC_DEF);
854 /* FIXME: Should we check type and size for protected symbol? */
855 h->size = 0;
856 h->type = 0;
857 return TRUE;
860 /* We need to treat weak definition right, depending on if there is a
861 definition from a dynamic object. */
862 if (bind == STB_WEAK)
864 if (olddef)
866 newweakdef = TRUE;
867 newweakundef = FALSE;
869 else
871 newweakdef = FALSE;
872 newweakundef = TRUE;
875 else
876 newweakdef = newweakundef = FALSE;
878 /* If the new weak definition comes from a relocatable file and the
879 old symbol comes from a dynamic object, we treat the new one as
880 strong. */
881 if (newweakdef && !newdyn && olddyn)
882 newweakdef = FALSE;
884 if (h->root.type == bfd_link_hash_defweak)
886 oldweakdef = TRUE;
887 oldweakundef = FALSE;
889 else if (h->root.type == bfd_link_hash_undefweak)
891 oldweakdef = FALSE;
892 oldweakundef = TRUE;
894 else
895 oldweakdef = oldweakundef = FALSE;
897 /* If the old weak definition comes from a relocatable file and the
898 new symbol comes from a dynamic object, we treat the old one as
899 strong. */
900 if (oldweakdef && !olddyn && newdyn)
901 oldweakdef = FALSE;
903 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
904 symbol, respectively, appears to be a common symbol in a dynamic
905 object. If a symbol appears in an uninitialized section, and is
906 not weak, and is not a function, then it may be a common symbol
907 which was resolved when the dynamic object was created. We want
908 to treat such symbols specially, because they raise special
909 considerations when setting the symbol size: if the symbol
910 appears as a common symbol in a regular object, and the size in
911 the regular object is larger, we must make sure that we use the
912 larger size. This problematic case can always be avoided in C,
913 but it must be handled correctly when using Fortran shared
914 libraries.
916 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
917 likewise for OLDDYNCOMMON and OLDDEF.
919 Note that this test is just a heuristic, and that it is quite
920 possible to have an uninitialized symbol in a shared object which
921 is really a definition, rather than a common symbol. This could
922 lead to some minor confusion when the symbol really is a common
923 symbol in some regular object. However, I think it will be
924 harmless. */
926 if (newdyn
927 && newdef
928 && (sec->flags & SEC_ALLOC) != 0
929 && (sec->flags & SEC_LOAD) == 0
930 && sym->st_size > 0
931 && !newweakdef
932 && !newweakundef
933 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
934 newdyncommon = TRUE;
935 else
936 newdyncommon = FALSE;
938 if (olddyn
939 && olddef
940 && h->root.type == bfd_link_hash_defined
941 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
942 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
943 && (h->root.u.def.section->flags & SEC_LOAD) == 0
944 && h->size > 0
945 && h->type != STT_FUNC)
946 olddyncommon = TRUE;
947 else
948 olddyncommon = FALSE;
950 /* It's OK to change the type if either the existing symbol or the
951 new symbol is weak unless it comes from a DT_NEEDED entry of
952 a shared object, in which case, the DT_NEEDED entry may not be
953 required at the run time. The type change is also OK if the
954 old symbol is undefined and the new symbol is defined. */
956 if ((! dt_needed && oldweakdef)
957 || oldweakundef
958 || newweakdef
959 || newweakundef
960 || (newdef
961 && (h->root.type == bfd_link_hash_undefined
962 || h->root.type == bfd_link_hash_undefweak)))
963 *type_change_ok = TRUE;
965 /* It's OK to change the size if either the existing symbol or the
966 new symbol is weak, or if the old symbol is undefined. */
968 if (*type_change_ok
969 || h->root.type == bfd_link_hash_undefined)
970 *size_change_ok = TRUE;
972 /* If both the old and the new symbols look like common symbols in a
973 dynamic object, set the size of the symbol to the larger of the
974 two. */
976 if (olddyncommon
977 && newdyncommon
978 && sym->st_size != h->size)
980 /* Since we think we have two common symbols, issue a multiple
981 common warning if desired. Note that we only warn if the
982 size is different. If the size is the same, we simply let
983 the old symbol override the new one as normally happens with
984 symbols defined in dynamic objects. */
986 if (! ((*info->callbacks->multiple_common)
987 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
988 h->size, abfd, bfd_link_hash_common, sym->st_size)))
989 return FALSE;
991 if (sym->st_size > h->size)
992 h->size = sym->st_size;
994 *size_change_ok = TRUE;
997 /* If we are looking at a dynamic object, and we have found a
998 definition, we need to see if the symbol was already defined by
999 some other object. If so, we want to use the existing
1000 definition, and we do not want to report a multiple symbol
1001 definition error; we do this by clobbering *PSEC to be
1002 bfd_und_section_ptr.
1004 We treat a common symbol as a definition if the symbol in the
1005 shared library is a function, since common symbols always
1006 represent variables; this can cause confusion in principle, but
1007 any such confusion would seem to indicate an erroneous program or
1008 shared library. We also permit a common symbol in a regular
1009 object to override a weak symbol in a shared object.
1011 We prefer a non-weak definition in a shared library to a weak
1012 definition in the executable unless it comes from a DT_NEEDED
1013 entry of a shared object, in which case, the DT_NEEDED entry
1014 may not be required at the run time. */
1016 if (newdyn
1017 && newdef
1018 && (olddef
1019 || (h->root.type == bfd_link_hash_common
1020 && (newweakdef
1021 || newweakundef
1022 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
1023 && (!oldweakdef
1024 || dt_needed
1025 || newweakdef
1026 || newweakundef))
1028 *override = TRUE;
1029 newdef = FALSE;
1030 newdyncommon = FALSE;
1032 *psec = sec = bfd_und_section_ptr;
1033 *size_change_ok = TRUE;
1035 /* If we get here when the old symbol is a common symbol, then
1036 we are explicitly letting it override a weak symbol or
1037 function in a dynamic object, and we don't want to warn about
1038 a type change. If the old symbol is a defined symbol, a type
1039 change warning may still be appropriate. */
1041 if (h->root.type == bfd_link_hash_common)
1042 *type_change_ok = TRUE;
1045 /* Handle the special case of an old common symbol merging with a
1046 new symbol which looks like a common symbol in a shared object.
1047 We change *PSEC and *PVALUE to make the new symbol look like a
1048 common symbol, and let _bfd_generic_link_add_one_symbol will do
1049 the right thing. */
1051 if (newdyncommon
1052 && h->root.type == bfd_link_hash_common)
1054 *override = TRUE;
1055 newdef = FALSE;
1056 newdyncommon = FALSE;
1057 *pvalue = sym->st_size;
1058 *psec = sec = bfd_com_section_ptr;
1059 *size_change_ok = TRUE;
1062 /* If the old symbol is from a dynamic object, and the new symbol is
1063 a definition which is not from a dynamic object, then the new
1064 symbol overrides the old symbol. Symbols from regular files
1065 always take precedence over symbols from dynamic objects, even if
1066 they are defined after the dynamic object in the link.
1068 As above, we again permit a common symbol in a regular object to
1069 override a definition in a shared object if the shared object
1070 symbol is a function or is weak.
1072 As above, we permit a non-weak definition in a shared object to
1073 override a weak definition in a regular object. */
1075 flip = NULL;
1076 if (! newdyn
1077 && (newdef
1078 || (bfd_is_com_section (sec)
1079 && (oldweakdef || h->type == STT_FUNC)))
1080 && olddyn
1081 && olddef
1082 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1083 && ((!newweakdef && !newweakundef) || oldweakdef))
1085 /* Change the hash table entry to undefined, and let
1086 _bfd_generic_link_add_one_symbol do the right thing with the
1087 new definition. */
1089 h->root.type = bfd_link_hash_undefined;
1090 h->root.u.undef.abfd = h->root.u.def.section->owner;
1091 *size_change_ok = TRUE;
1093 olddef = FALSE;
1094 olddyncommon = FALSE;
1096 /* We again permit a type change when a common symbol may be
1097 overriding a function. */
1099 if (bfd_is_com_section (sec))
1100 *type_change_ok = TRUE;
1102 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1103 flip = *sym_hash;
1104 else
1105 /* This union may have been set to be non-NULL when this symbol
1106 was seen in a dynamic object. We must force the union to be
1107 NULL, so that it is correct for a regular symbol. */
1108 h->verinfo.vertree = NULL;
1111 /* Handle the special case of a new common symbol merging with an
1112 old symbol that looks like it might be a common symbol defined in
1113 a shared object. Note that we have already handled the case in
1114 which a new common symbol should simply override the definition
1115 in the shared library. */
1117 if (! newdyn
1118 && bfd_is_com_section (sec)
1119 && olddyncommon)
1121 /* It would be best if we could set the hash table entry to a
1122 common symbol, but we don't know what to use for the section
1123 or the alignment. */
1124 if (! ((*info->callbacks->multiple_common)
1125 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1126 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1127 return FALSE;
1129 /* If the presumed common symbol in the dynamic object is
1130 larger, pretend that the new symbol has its size. */
1132 if (h->size > *pvalue)
1133 *pvalue = h->size;
1135 /* FIXME: We no longer know the alignment required by the symbol
1136 in the dynamic object, so we just wind up using the one from
1137 the regular object. */
1139 olddef = FALSE;
1140 olddyncommon = FALSE;
1142 h->root.type = bfd_link_hash_undefined;
1143 h->root.u.undef.abfd = h->root.u.def.section->owner;
1145 *size_change_ok = TRUE;
1146 *type_change_ok = TRUE;
1148 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1149 flip = *sym_hash;
1150 else
1151 h->verinfo.vertree = NULL;
1154 if (flip != NULL)
1156 /* Handle the case where we had a versioned symbol in a dynamic
1157 library and now find a definition in a normal object. In this
1158 case, we make the versioned symbol point to the normal one. */
1159 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1160 flip->root.type = h->root.type;
1161 h->root.type = bfd_link_hash_indirect;
1162 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1163 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1164 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1165 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1167 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1168 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1172 /* Handle the special case of a weak definition in a regular object
1173 followed by a non-weak definition in a shared object. In this
1174 case, we prefer the definition in the shared object unless it
1175 comes from a DT_NEEDED entry of a shared object, in which case,
1176 the DT_NEEDED entry may not be required at the run time. */
1177 if (olddef
1178 && ! dt_needed
1179 && oldweakdef
1180 && newdef
1181 && newdyn
1182 && !newweakdef
1183 && !newweakundef)
1185 /* To make this work we have to frob the flags so that the rest
1186 of the code does not think we are using the regular
1187 definition. */
1188 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
1189 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
1190 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
1191 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1192 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
1193 | ELF_LINK_HASH_DEF_DYNAMIC);
1195 /* If H is the target of an indirection, we want the caller to
1196 use H rather than the indirect symbol. Otherwise if we are
1197 defining a new indirect symbol we will wind up attaching it
1198 to the entry we are overriding. */
1199 *sym_hash = h;
1202 /* Handle the special case of a non-weak definition in a shared
1203 object followed by a weak definition in a regular object. In
1204 this case we prefer the definition in the shared object. To make
1205 this work we have to tell the caller to not treat the new symbol
1206 as a definition. */
1207 if (olddef
1208 && olddyn
1209 && !oldweakdef
1210 && newdef
1211 && ! newdyn
1212 && (newweakdef || newweakundef))
1213 *override = TRUE;
1215 return TRUE;
1218 /* This function is called to create an indirect symbol from the
1219 default for the symbol with the default version if needed. The
1220 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1221 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
1222 indicates if it comes from a DT_NEEDED entry of a shared object. */
1224 bfd_boolean
1225 _bfd_elf_add_default_symbol (bfd *abfd,
1226 struct bfd_link_info *info,
1227 struct elf_link_hash_entry *h,
1228 const char *name,
1229 Elf_Internal_Sym *sym,
1230 asection **psec,
1231 bfd_vma *value,
1232 bfd_boolean *dynsym,
1233 bfd_boolean override,
1234 bfd_boolean dt_needed)
1236 bfd_boolean type_change_ok;
1237 bfd_boolean size_change_ok;
1238 bfd_boolean skip;
1239 char *shortname;
1240 struct elf_link_hash_entry *hi;
1241 struct bfd_link_hash_entry *bh;
1242 const struct elf_backend_data *bed;
1243 bfd_boolean collect;
1244 bfd_boolean dynamic;
1245 char *p;
1246 size_t len, shortlen;
1247 asection *sec;
1249 /* If this symbol has a version, and it is the default version, we
1250 create an indirect symbol from the default name to the fully
1251 decorated name. This will cause external references which do not
1252 specify a version to be bound to this version of the symbol. */
1253 p = strchr (name, ELF_VER_CHR);
1254 if (p == NULL || p[1] != ELF_VER_CHR)
1255 return TRUE;
1257 if (override)
1259 /* We are overridden by an old definition. We need to check if we
1260 need to create the indirect symbol from the default name. */
1261 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1262 FALSE, FALSE);
1263 BFD_ASSERT (hi != NULL);
1264 if (hi == h)
1265 return TRUE;
1266 while (hi->root.type == bfd_link_hash_indirect
1267 || hi->root.type == bfd_link_hash_warning)
1269 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1270 if (hi == h)
1271 return TRUE;
1275 bed = get_elf_backend_data (abfd);
1276 collect = bed->collect;
1277 dynamic = (abfd->flags & DYNAMIC) != 0;
1279 shortlen = p - name;
1280 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1281 if (shortname == NULL)
1282 return FALSE;
1283 memcpy (shortname, name, shortlen);
1284 shortname[shortlen] = '\0';
1286 /* We are going to create a new symbol. Merge it with any existing
1287 symbol with this name. For the purposes of the merge, act as
1288 though we were defining the symbol we just defined, although we
1289 actually going to define an indirect symbol. */
1290 type_change_ok = FALSE;
1291 size_change_ok = FALSE;
1292 sec = *psec;
1293 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1294 &hi, &skip, &override, &type_change_ok,
1295 &size_change_ok, dt_needed))
1296 return FALSE;
1298 if (skip)
1299 goto nondefault;
1301 if (! override)
1303 bh = &hi->root;
1304 if (! (_bfd_generic_link_add_one_symbol
1305 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1306 0, name, FALSE, collect, &bh)))
1307 return FALSE;
1308 hi = (struct elf_link_hash_entry *) bh;
1310 else
1312 /* In this case the symbol named SHORTNAME is overriding the
1313 indirect symbol we want to add. We were planning on making
1314 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1315 is the name without a version. NAME is the fully versioned
1316 name, and it is the default version.
1318 Overriding means that we already saw a definition for the
1319 symbol SHORTNAME in a regular object, and it is overriding
1320 the symbol defined in the dynamic object.
1322 When this happens, we actually want to change NAME, the
1323 symbol we just added, to refer to SHORTNAME. This will cause
1324 references to NAME in the shared object to become references
1325 to SHORTNAME in the regular object. This is what we expect
1326 when we override a function in a shared object: that the
1327 references in the shared object will be mapped to the
1328 definition in the regular object. */
1330 while (hi->root.type == bfd_link_hash_indirect
1331 || hi->root.type == bfd_link_hash_warning)
1332 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1334 h->root.type = bfd_link_hash_indirect;
1335 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1336 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1338 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1339 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1340 if (hi->elf_link_hash_flags
1341 & (ELF_LINK_HASH_REF_REGULAR
1342 | ELF_LINK_HASH_DEF_REGULAR))
1344 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1345 return FALSE;
1349 /* Now set HI to H, so that the following code will set the
1350 other fields correctly. */
1351 hi = h;
1354 /* If there is a duplicate definition somewhere, then HI may not
1355 point to an indirect symbol. We will have reported an error to
1356 the user in that case. */
1358 if (hi->root.type == bfd_link_hash_indirect)
1360 struct elf_link_hash_entry *ht;
1362 /* If the symbol became indirect, then we assume that we have
1363 not seen a definition before. */
1364 BFD_ASSERT ((hi->elf_link_hash_flags
1365 & (ELF_LINK_HASH_DEF_DYNAMIC
1366 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1368 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1369 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1371 /* See if the new flags lead us to realize that the symbol must
1372 be dynamic. */
1373 if (! *dynsym)
1375 if (! dynamic)
1377 if (info->shared
1378 || ((hi->elf_link_hash_flags
1379 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1380 *dynsym = TRUE;
1382 else
1384 if ((hi->elf_link_hash_flags
1385 & ELF_LINK_HASH_REF_REGULAR) != 0)
1386 *dynsym = TRUE;
1391 /* We also need to define an indirection from the nondefault version
1392 of the symbol. */
1394 nondefault:
1395 len = strlen (name);
1396 shortname = bfd_hash_allocate (&info->hash->table, len);
1397 if (shortname == NULL)
1398 return FALSE;
1399 memcpy (shortname, name, shortlen);
1400 memcpy (shortname + shortlen, p + 1, len - shortlen);
1402 /* Once again, merge with any existing symbol. */
1403 type_change_ok = FALSE;
1404 size_change_ok = FALSE;
1405 sec = *psec;
1406 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1407 &hi, &skip, &override, &type_change_ok,
1408 &size_change_ok, dt_needed))
1409 return FALSE;
1411 if (skip)
1412 return TRUE;
1414 if (override)
1416 /* Here SHORTNAME is a versioned name, so we don't expect to see
1417 the type of override we do in the case above unless it is
1418 overridden by a versioned definition. */
1419 if (hi->root.type != bfd_link_hash_defined
1420 && hi->root.type != bfd_link_hash_defweak)
1421 (*_bfd_error_handler)
1422 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1423 bfd_archive_filename (abfd), shortname);
1425 else
1427 bh = &hi->root;
1428 if (! (_bfd_generic_link_add_one_symbol
1429 (info, abfd, shortname, BSF_INDIRECT,
1430 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1431 return FALSE;
1432 hi = (struct elf_link_hash_entry *) bh;
1434 /* If there is a duplicate definition somewhere, then HI may not
1435 point to an indirect symbol. We will have reported an error
1436 to the user in that case. */
1438 if (hi->root.type == bfd_link_hash_indirect)
1440 /* If the symbol became indirect, then we assume that we have
1441 not seen a definition before. */
1442 BFD_ASSERT ((hi->elf_link_hash_flags
1443 & (ELF_LINK_HASH_DEF_DYNAMIC
1444 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1446 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1448 /* See if the new flags lead us to realize that the symbol
1449 must be dynamic. */
1450 if (! *dynsym)
1452 if (! dynamic)
1454 if (info->shared
1455 || ((hi->elf_link_hash_flags
1456 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1457 *dynsym = TRUE;
1459 else
1461 if ((hi->elf_link_hash_flags
1462 & ELF_LINK_HASH_REF_REGULAR) != 0)
1463 *dynsym = TRUE;
1469 return TRUE;
1472 /* This routine is used to export all defined symbols into the dynamic
1473 symbol table. It is called via elf_link_hash_traverse. */
1475 bfd_boolean
1476 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1478 struct elf_info_failed *eif = data;
1480 /* Ignore indirect symbols. These are added by the versioning code. */
1481 if (h->root.type == bfd_link_hash_indirect)
1482 return TRUE;
1484 if (h->root.type == bfd_link_hash_warning)
1485 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1487 if (h->dynindx == -1
1488 && (h->elf_link_hash_flags
1489 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1491 struct bfd_elf_version_tree *t;
1492 struct bfd_elf_version_expr *d;
1494 for (t = eif->verdefs; t != NULL; t = t->next)
1496 if (t->globals.list != NULL)
1498 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1499 if (d != NULL)
1500 goto doit;
1503 if (t->locals.list != NULL)
1505 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1506 if (d != NULL)
1507 return TRUE;
1511 if (!eif->verdefs)
1513 doit:
1514 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
1516 eif->failed = TRUE;
1517 return FALSE;
1522 return TRUE;
1525 /* Look through the symbols which are defined in other shared
1526 libraries and referenced here. Update the list of version
1527 dependencies. This will be put into the .gnu.version_r section.
1528 This function is called via elf_link_hash_traverse. */
1530 bfd_boolean
1531 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1532 void *data)
1534 struct elf_find_verdep_info *rinfo = data;
1535 Elf_Internal_Verneed *t;
1536 Elf_Internal_Vernaux *a;
1537 bfd_size_type amt;
1539 if (h->root.type == bfd_link_hash_warning)
1540 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1542 /* We only care about symbols defined in shared objects with version
1543 information. */
1544 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1545 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1546 || h->dynindx == -1
1547 || h->verinfo.verdef == NULL)
1548 return TRUE;
1550 /* See if we already know about this version. */
1551 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1553 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1554 continue;
1556 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1557 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1558 return TRUE;
1560 break;
1563 /* This is a new version. Add it to tree we are building. */
1565 if (t == NULL)
1567 amt = sizeof *t;
1568 t = bfd_zalloc (rinfo->output_bfd, amt);
1569 if (t == NULL)
1571 rinfo->failed = TRUE;
1572 return FALSE;
1575 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1576 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1577 elf_tdata (rinfo->output_bfd)->verref = t;
1580 amt = sizeof *a;
1581 a = bfd_zalloc (rinfo->output_bfd, amt);
1583 /* Note that we are copying a string pointer here, and testing it
1584 above. If bfd_elf_string_from_elf_section is ever changed to
1585 discard the string data when low in memory, this will have to be
1586 fixed. */
1587 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1589 a->vna_flags = h->verinfo.verdef->vd_flags;
1590 a->vna_nextptr = t->vn_auxptr;
1592 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1593 ++rinfo->vers;
1595 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1597 t->vn_auxptr = a;
1599 return TRUE;
1602 /* Figure out appropriate versions for all the symbols. We may not
1603 have the version number script until we have read all of the input
1604 files, so until that point we don't know which symbols should be
1605 local. This function is called via elf_link_hash_traverse. */
1607 bfd_boolean
1608 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1610 struct elf_assign_sym_version_info *sinfo;
1611 struct bfd_link_info *info;
1612 const struct elf_backend_data *bed;
1613 struct elf_info_failed eif;
1614 char *p;
1615 bfd_size_type amt;
1617 sinfo = data;
1618 info = sinfo->info;
1620 if (h->root.type == bfd_link_hash_warning)
1621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1623 /* Fix the symbol flags. */
1624 eif.failed = FALSE;
1625 eif.info = info;
1626 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1628 if (eif.failed)
1629 sinfo->failed = TRUE;
1630 return FALSE;
1633 /* We only need version numbers for symbols defined in regular
1634 objects. */
1635 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1636 return TRUE;
1638 bed = get_elf_backend_data (sinfo->output_bfd);
1639 p = strchr (h->root.root.string, ELF_VER_CHR);
1640 if (p != NULL && h->verinfo.vertree == NULL)
1642 struct bfd_elf_version_tree *t;
1643 bfd_boolean hidden;
1645 hidden = TRUE;
1647 /* There are two consecutive ELF_VER_CHR characters if this is
1648 not a hidden symbol. */
1649 ++p;
1650 if (*p == ELF_VER_CHR)
1652 hidden = FALSE;
1653 ++p;
1656 /* If there is no version string, we can just return out. */
1657 if (*p == '\0')
1659 if (hidden)
1660 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1661 return TRUE;
1664 /* Look for the version. If we find it, it is no longer weak. */
1665 for (t = sinfo->verdefs; t != NULL; t = t->next)
1667 if (strcmp (t->name, p) == 0)
1669 size_t len;
1670 char *alc;
1671 struct bfd_elf_version_expr *d;
1673 len = p - h->root.root.string;
1674 alc = bfd_malloc (len);
1675 if (alc == NULL)
1676 return FALSE;
1677 memcpy (alc, h->root.root.string, len - 1);
1678 alc[len - 1] = '\0';
1679 if (alc[len - 2] == ELF_VER_CHR)
1680 alc[len - 2] = '\0';
1682 h->verinfo.vertree = t;
1683 t->used = TRUE;
1684 d = NULL;
1686 if (t->globals.list != NULL)
1687 d = (*t->match) (&t->globals, NULL, alc);
1689 /* See if there is anything to force this symbol to
1690 local scope. */
1691 if (d == NULL && t->locals.list != NULL)
1693 d = (*t->match) (&t->locals, NULL, alc);
1694 if (d != NULL
1695 && h->dynindx != -1
1696 && info->shared
1697 && ! info->export_dynamic)
1698 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1701 free (alc);
1702 break;
1706 /* If we are building an application, we need to create a
1707 version node for this version. */
1708 if (t == NULL && info->executable)
1710 struct bfd_elf_version_tree **pp;
1711 int version_index;
1713 /* If we aren't going to export this symbol, we don't need
1714 to worry about it. */
1715 if (h->dynindx == -1)
1716 return TRUE;
1718 amt = sizeof *t;
1719 t = bfd_zalloc (sinfo->output_bfd, amt);
1720 if (t == NULL)
1722 sinfo->failed = TRUE;
1723 return FALSE;
1726 t->name = p;
1727 t->name_indx = (unsigned int) -1;
1728 t->used = TRUE;
1730 version_index = 1;
1731 /* Don't count anonymous version tag. */
1732 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1733 version_index = 0;
1734 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1735 ++version_index;
1736 t->vernum = version_index;
1738 *pp = t;
1740 h->verinfo.vertree = t;
1742 else if (t == NULL)
1744 /* We could not find the version for a symbol when
1745 generating a shared archive. Return an error. */
1746 (*_bfd_error_handler)
1747 (_("%s: undefined versioned symbol name %s"),
1748 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
1749 bfd_set_error (bfd_error_bad_value);
1750 sinfo->failed = TRUE;
1751 return FALSE;
1754 if (hidden)
1755 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1758 /* If we don't have a version for this symbol, see if we can find
1759 something. */
1760 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1762 struct bfd_elf_version_tree *t;
1763 struct bfd_elf_version_tree *local_ver;
1764 struct bfd_elf_version_expr *d;
1766 /* See if can find what version this symbol is in. If the
1767 symbol is supposed to be local, then don't actually register
1768 it. */
1769 local_ver = NULL;
1770 for (t = sinfo->verdefs; t != NULL; t = t->next)
1772 if (t->globals.list != NULL)
1774 bfd_boolean matched;
1776 matched = FALSE;
1777 d = NULL;
1778 while ((d = (*t->match) (&t->globals, d,
1779 h->root.root.string)) != NULL)
1780 if (d->symver)
1781 matched = TRUE;
1782 else
1784 /* There is a version without definition. Make
1785 the symbol the default definition for this
1786 version. */
1787 h->verinfo.vertree = t;
1788 local_ver = NULL;
1789 d->script = 1;
1790 break;
1792 if (d != NULL)
1793 break;
1794 else if (matched)
1795 /* There is no undefined version for this symbol. Hide the
1796 default one. */
1797 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1800 if (t->locals.list != NULL)
1802 d = NULL;
1803 while ((d = (*t->match) (&t->locals, d,
1804 h->root.root.string)) != NULL)
1806 local_ver = t;
1807 /* If the match is "*", keep looking for a more
1808 explicit, perhaps even global, match.
1809 XXX: Shouldn't this be !d->wildcard instead? */
1810 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1811 break;
1814 if (d != NULL)
1815 break;
1819 if (local_ver != NULL)
1821 h->verinfo.vertree = local_ver;
1822 if (h->dynindx != -1
1823 && info->shared
1824 && ! info->export_dynamic)
1826 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1831 return TRUE;
1834 /* Read and swap the relocs from the section indicated by SHDR. This
1835 may be either a REL or a RELA section. The relocations are
1836 translated into RELA relocations and stored in INTERNAL_RELOCS,
1837 which should have already been allocated to contain enough space.
1838 The EXTERNAL_RELOCS are a buffer where the external form of the
1839 relocations should be stored.
1841 Returns FALSE if something goes wrong. */
1843 static bfd_boolean
1844 elf_link_read_relocs_from_section (bfd *abfd,
1845 asection *sec,
1846 Elf_Internal_Shdr *shdr,
1847 void *external_relocs,
1848 Elf_Internal_Rela *internal_relocs)
1850 const struct elf_backend_data *bed;
1851 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1852 const bfd_byte *erela;
1853 const bfd_byte *erelaend;
1854 Elf_Internal_Rela *irela;
1855 Elf_Internal_Shdr *symtab_hdr;
1856 size_t nsyms;
1858 /* If there aren't any relocations, that's OK. */
1859 if (!shdr)
1860 return TRUE;
1862 /* Position ourselves at the start of the section. */
1863 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1864 return FALSE;
1866 /* Read the relocations. */
1867 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1868 return FALSE;
1870 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1871 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1873 bed = get_elf_backend_data (abfd);
1875 /* Convert the external relocations to the internal format. */
1876 if (shdr->sh_entsize == bed->s->sizeof_rel)
1877 swap_in = bed->s->swap_reloc_in;
1878 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1879 swap_in = bed->s->swap_reloca_in;
1880 else
1882 bfd_set_error (bfd_error_wrong_format);
1883 return FALSE;
1886 erela = external_relocs;
1887 erelaend = erela + NUM_SHDR_ENTRIES (shdr) * shdr->sh_entsize;
1888 irela = internal_relocs;
1889 while (erela < erelaend)
1891 bfd_vma r_symndx;
1893 (*swap_in) (abfd, erela, irela);
1894 r_symndx = ELF32_R_SYM (irela->r_info);
1895 if (bed->s->arch_size == 64)
1896 r_symndx >>= 24;
1897 if ((size_t) r_symndx >= nsyms)
1899 (*_bfd_error_handler)
1900 (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
1901 bfd_archive_filename (abfd), (unsigned long) r_symndx,
1902 (unsigned long) nsyms, irela->r_offset, sec->name);
1903 bfd_set_error (bfd_error_bad_value);
1904 return FALSE;
1906 irela += bed->s->int_rels_per_ext_rel;
1907 erela += shdr->sh_entsize;
1910 return TRUE;
1913 /* Read and swap the relocs for a section O. They may have been
1914 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1915 not NULL, they are used as buffers to read into. They are known to
1916 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1917 the return value is allocated using either malloc or bfd_alloc,
1918 according to the KEEP_MEMORY argument. If O has two relocation
1919 sections (both REL and RELA relocations), then the REL_HDR
1920 relocations will appear first in INTERNAL_RELOCS, followed by the
1921 REL_HDR2 relocations. */
1923 Elf_Internal_Rela *
1924 _bfd_elf_link_read_relocs (bfd *abfd,
1925 asection *o,
1926 void *external_relocs,
1927 Elf_Internal_Rela *internal_relocs,
1928 bfd_boolean keep_memory)
1930 Elf_Internal_Shdr *rel_hdr;
1931 void *alloc1 = NULL;
1932 Elf_Internal_Rela *alloc2 = NULL;
1933 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1935 if (elf_section_data (o)->relocs != NULL)
1936 return elf_section_data (o)->relocs;
1938 if (o->reloc_count == 0)
1939 return NULL;
1941 rel_hdr = &elf_section_data (o)->rel_hdr;
1943 if (internal_relocs == NULL)
1945 bfd_size_type size;
1947 size = o->reloc_count;
1948 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1949 if (keep_memory)
1950 internal_relocs = bfd_alloc (abfd, size);
1951 else
1952 internal_relocs = alloc2 = bfd_malloc (size);
1953 if (internal_relocs == NULL)
1954 goto error_return;
1957 if (external_relocs == NULL)
1959 bfd_size_type size = rel_hdr->sh_size;
1961 if (elf_section_data (o)->rel_hdr2)
1962 size += elf_section_data (o)->rel_hdr2->sh_size;
1963 alloc1 = bfd_malloc (size);
1964 if (alloc1 == NULL)
1965 goto error_return;
1966 external_relocs = alloc1;
1969 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1970 external_relocs,
1971 internal_relocs))
1972 goto error_return;
1973 if (!elf_link_read_relocs_from_section
1974 (abfd, o,
1975 elf_section_data (o)->rel_hdr2,
1976 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1977 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1978 * bed->s->int_rels_per_ext_rel)))
1979 goto error_return;
1981 /* Cache the results for next time, if we can. */
1982 if (keep_memory)
1983 elf_section_data (o)->relocs = internal_relocs;
1985 if (alloc1 != NULL)
1986 free (alloc1);
1988 /* Don't free alloc2, since if it was allocated we are passing it
1989 back (under the name of internal_relocs). */
1991 return internal_relocs;
1993 error_return:
1994 if (alloc1 != NULL)
1995 free (alloc1);
1996 if (alloc2 != NULL)
1997 free (alloc2);
1998 return NULL;
2001 /* Compute the size of, and allocate space for, REL_HDR which is the
2002 section header for a section containing relocations for O. */
2004 bfd_boolean
2005 _bfd_elf_link_size_reloc_section (bfd *abfd,
2006 Elf_Internal_Shdr *rel_hdr,
2007 asection *o)
2009 bfd_size_type reloc_count;
2010 bfd_size_type num_rel_hashes;
2012 /* Figure out how many relocations there will be. */
2013 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2014 reloc_count = elf_section_data (o)->rel_count;
2015 else
2016 reloc_count = elf_section_data (o)->rel_count2;
2018 num_rel_hashes = o->reloc_count;
2019 if (num_rel_hashes < reloc_count)
2020 num_rel_hashes = reloc_count;
2022 /* That allows us to calculate the size of the section. */
2023 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2025 /* The contents field must last into write_object_contents, so we
2026 allocate it with bfd_alloc rather than malloc. Also since we
2027 cannot be sure that the contents will actually be filled in,
2028 we zero the allocated space. */
2029 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2030 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2031 return FALSE;
2033 /* We only allocate one set of hash entries, so we only do it the
2034 first time we are called. */
2035 if (elf_section_data (o)->rel_hashes == NULL
2036 && num_rel_hashes)
2038 struct elf_link_hash_entry **p;
2040 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2041 if (p == NULL)
2042 return FALSE;
2044 elf_section_data (o)->rel_hashes = p;
2047 return TRUE;
2050 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2051 originated from the section given by INPUT_REL_HDR) to the
2052 OUTPUT_BFD. */
2054 bfd_boolean
2055 _bfd_elf_link_output_relocs (bfd *output_bfd,
2056 asection *input_section,
2057 Elf_Internal_Shdr *input_rel_hdr,
2058 Elf_Internal_Rela *internal_relocs)
2060 Elf_Internal_Rela *irela;
2061 Elf_Internal_Rela *irelaend;
2062 bfd_byte *erel;
2063 Elf_Internal_Shdr *output_rel_hdr;
2064 asection *output_section;
2065 unsigned int *rel_countp = NULL;
2066 const struct elf_backend_data *bed;
2067 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2069 output_section = input_section->output_section;
2070 output_rel_hdr = NULL;
2072 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2073 == input_rel_hdr->sh_entsize)
2075 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2076 rel_countp = &elf_section_data (output_section)->rel_count;
2078 else if (elf_section_data (output_section)->rel_hdr2
2079 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2080 == input_rel_hdr->sh_entsize))
2082 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2083 rel_countp = &elf_section_data (output_section)->rel_count2;
2085 else
2087 (*_bfd_error_handler)
2088 (_("%s: relocation size mismatch in %s section %s"),
2089 bfd_get_filename (output_bfd),
2090 bfd_archive_filename (input_section->owner),
2091 input_section->name);
2092 bfd_set_error (bfd_error_wrong_object_format);
2093 return FALSE;
2096 bed = get_elf_backend_data (output_bfd);
2097 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2098 swap_out = bed->s->swap_reloc_out;
2099 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2100 swap_out = bed->s->swap_reloca_out;
2101 else
2102 abort ();
2104 erel = output_rel_hdr->contents;
2105 erel += *rel_countp * input_rel_hdr->sh_entsize;
2106 irela = internal_relocs;
2107 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2108 * bed->s->int_rels_per_ext_rel);
2109 while (irela < irelaend)
2111 (*swap_out) (output_bfd, irela, erel);
2112 irela += bed->s->int_rels_per_ext_rel;
2113 erel += input_rel_hdr->sh_entsize;
2116 /* Bump the counter, so that we know where to add the next set of
2117 relocations. */
2118 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2120 return TRUE;
2123 /* Fix up the flags for a symbol. This handles various cases which
2124 can only be fixed after all the input files are seen. This is
2125 currently called by both adjust_dynamic_symbol and
2126 assign_sym_version, which is unnecessary but perhaps more robust in
2127 the face of future changes. */
2129 bfd_boolean
2130 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2131 struct elf_info_failed *eif)
2133 /* If this symbol was mentioned in a non-ELF file, try to set
2134 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2135 permit a non-ELF file to correctly refer to a symbol defined in
2136 an ELF dynamic object. */
2137 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2139 while (h->root.type == bfd_link_hash_indirect)
2140 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2142 if (h->root.type != bfd_link_hash_defined
2143 && h->root.type != bfd_link_hash_defweak)
2144 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2145 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2146 else
2148 if (h->root.u.def.section->owner != NULL
2149 && (bfd_get_flavour (h->root.u.def.section->owner)
2150 == bfd_target_elf_flavour))
2151 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2152 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2153 else
2154 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2157 if (h->dynindx == -1
2158 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2159 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2161 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2163 eif->failed = TRUE;
2164 return FALSE;
2168 else
2170 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2171 was first seen in a non-ELF file. Fortunately, if the symbol
2172 was first seen in an ELF file, we're probably OK unless the
2173 symbol was defined in a non-ELF file. Catch that case here.
2174 FIXME: We're still in trouble if the symbol was first seen in
2175 a dynamic object, and then later in a non-ELF regular object. */
2176 if ((h->root.type == bfd_link_hash_defined
2177 || h->root.type == bfd_link_hash_defweak)
2178 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2179 && (h->root.u.def.section->owner != NULL
2180 ? (bfd_get_flavour (h->root.u.def.section->owner)
2181 != bfd_target_elf_flavour)
2182 : (bfd_is_abs_section (h->root.u.def.section)
2183 && (h->elf_link_hash_flags
2184 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2185 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2188 /* If this is a final link, and the symbol was defined as a common
2189 symbol in a regular object file, and there was no definition in
2190 any dynamic object, then the linker will have allocated space for
2191 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2192 flag will not have been set. */
2193 if (h->root.type == bfd_link_hash_defined
2194 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2195 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2196 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2197 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2198 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2200 /* If -Bsymbolic was used (which means to bind references to global
2201 symbols to the definition within the shared object), and this
2202 symbol was defined in a regular object, then it actually doesn't
2203 need a PLT entry. Likewise, if the symbol has non-default
2204 visibility. If the symbol has hidden or internal visibility, we
2205 will force it local. */
2206 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2207 && eif->info->shared
2208 && is_elf_hash_table (eif->info->hash)
2209 && (eif->info->symbolic
2210 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2211 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2213 const struct elf_backend_data *bed;
2214 bfd_boolean force_local;
2216 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2218 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2219 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2220 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2223 /* If a weak undefined symbol has non-default visibility, we also
2224 hide it from the dynamic linker. */
2225 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2226 && h->root.type == bfd_link_hash_undefweak)
2228 const struct elf_backend_data *bed;
2229 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2230 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2233 /* If this is a weak defined symbol in a dynamic object, and we know
2234 the real definition in the dynamic object, copy interesting flags
2235 over to the real definition. */
2236 if (h->weakdef != NULL)
2238 struct elf_link_hash_entry *weakdef;
2240 weakdef = h->weakdef;
2241 if (h->root.type == bfd_link_hash_indirect)
2242 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2244 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2245 || h->root.type == bfd_link_hash_defweak);
2246 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2247 || weakdef->root.type == bfd_link_hash_defweak);
2248 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2250 /* If the real definition is defined by a regular object file,
2251 don't do anything special. See the longer description in
2252 _bfd_elf_adjust_dynamic_symbol, below. */
2253 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2254 h->weakdef = NULL;
2255 else
2257 const struct elf_backend_data *bed;
2259 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2260 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2264 return TRUE;
2267 /* Make the backend pick a good value for a dynamic symbol. This is
2268 called via elf_link_hash_traverse, and also calls itself
2269 recursively. */
2271 bfd_boolean
2272 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2274 struct elf_info_failed *eif = data;
2275 bfd *dynobj;
2276 const struct elf_backend_data *bed;
2278 if (! is_elf_hash_table (eif->info->hash))
2279 return FALSE;
2281 if (h->root.type == bfd_link_hash_warning)
2283 h->plt = elf_hash_table (eif->info)->init_offset;
2284 h->got = elf_hash_table (eif->info)->init_offset;
2286 /* When warning symbols are created, they **replace** the "real"
2287 entry in the hash table, thus we never get to see the real
2288 symbol in a hash traversal. So look at it now. */
2289 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2292 /* Ignore indirect symbols. These are added by the versioning code. */
2293 if (h->root.type == bfd_link_hash_indirect)
2294 return TRUE;
2296 /* Fix the symbol flags. */
2297 if (! _bfd_elf_fix_symbol_flags (h, eif))
2298 return FALSE;
2300 /* If this symbol does not require a PLT entry, and it is not
2301 defined by a dynamic object, or is not referenced by a regular
2302 object, ignore it. We do have to handle a weak defined symbol,
2303 even if no regular object refers to it, if we decided to add it
2304 to the dynamic symbol table. FIXME: Do we normally need to worry
2305 about symbols which are defined by one dynamic object and
2306 referenced by another one? */
2307 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2308 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2309 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2310 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2311 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2313 h->plt = elf_hash_table (eif->info)->init_offset;
2314 return TRUE;
2317 /* If we've already adjusted this symbol, don't do it again. This
2318 can happen via a recursive call. */
2319 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2320 return TRUE;
2322 /* Don't look at this symbol again. Note that we must set this
2323 after checking the above conditions, because we may look at a
2324 symbol once, decide not to do anything, and then get called
2325 recursively later after REF_REGULAR is set below. */
2326 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2328 /* If this is a weak definition, and we know a real definition, and
2329 the real symbol is not itself defined by a regular object file,
2330 then get a good value for the real definition. We handle the
2331 real symbol first, for the convenience of the backend routine.
2333 Note that there is a confusing case here. If the real definition
2334 is defined by a regular object file, we don't get the real symbol
2335 from the dynamic object, but we do get the weak symbol. If the
2336 processor backend uses a COPY reloc, then if some routine in the
2337 dynamic object changes the real symbol, we will not see that
2338 change in the corresponding weak symbol. This is the way other
2339 ELF linkers work as well, and seems to be a result of the shared
2340 library model.
2342 I will clarify this issue. Most SVR4 shared libraries define the
2343 variable _timezone and define timezone as a weak synonym. The
2344 tzset call changes _timezone. If you write
2345 extern int timezone;
2346 int _timezone = 5;
2347 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2348 you might expect that, since timezone is a synonym for _timezone,
2349 the same number will print both times. However, if the processor
2350 backend uses a COPY reloc, then actually timezone will be copied
2351 into your process image, and, since you define _timezone
2352 yourself, _timezone will not. Thus timezone and _timezone will
2353 wind up at different memory locations. The tzset call will set
2354 _timezone, leaving timezone unchanged. */
2356 if (h->weakdef != NULL)
2358 /* If we get to this point, we know there is an implicit
2359 reference by a regular object file via the weak symbol H.
2360 FIXME: Is this really true? What if the traversal finds
2361 H->WEAKDEF before it finds H? */
2362 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2364 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
2365 return FALSE;
2368 /* If a symbol has no type and no size and does not require a PLT
2369 entry, then we are probably about to do the wrong thing here: we
2370 are probably going to create a COPY reloc for an empty object.
2371 This case can arise when a shared object is built with assembly
2372 code, and the assembly code fails to set the symbol type. */
2373 if (h->size == 0
2374 && h->type == STT_NOTYPE
2375 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2376 (*_bfd_error_handler)
2377 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2378 h->root.root.string);
2380 dynobj = elf_hash_table (eif->info)->dynobj;
2381 bed = get_elf_backend_data (dynobj);
2382 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2384 eif->failed = TRUE;
2385 return FALSE;
2388 return TRUE;
2391 /* Adjust all external symbols pointing into SEC_MERGE sections
2392 to reflect the object merging within the sections. */
2394 bfd_boolean
2395 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2397 asection *sec;
2399 if (h->root.type == bfd_link_hash_warning)
2400 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2402 if ((h->root.type == bfd_link_hash_defined
2403 || h->root.type == bfd_link_hash_defweak)
2404 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2405 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2407 bfd *output_bfd = data;
2409 h->root.u.def.value =
2410 _bfd_merged_section_offset (output_bfd,
2411 &h->root.u.def.section,
2412 elf_section_data (sec)->sec_info,
2413 h->root.u.def.value, 0);
2416 return TRUE;
2419 /* Returns false if the symbol referred to by H should be considered
2420 to resolve local to the current module, and true if it should be
2421 considered to bind dynamically. */
2423 bfd_boolean
2424 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2425 struct bfd_link_info *info,
2426 bfd_boolean ignore_protected)
2428 bfd_boolean binding_stays_local_p;
2430 if (h == NULL)
2431 return FALSE;
2433 while (h->root.type == bfd_link_hash_indirect
2434 || h->root.type == bfd_link_hash_warning)
2435 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2437 /* If it was forced local, then clearly it's not dynamic. */
2438 if (h->dynindx == -1)
2439 return FALSE;
2440 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2441 return FALSE;
2443 /* Identify the cases where name binding rules say that a
2444 visible symbol resolves locally. */
2445 binding_stays_local_p = info->executable || info->symbolic;
2447 switch (ELF_ST_VISIBILITY (h->other))
2449 case STV_INTERNAL:
2450 case STV_HIDDEN:
2451 return FALSE;
2453 case STV_PROTECTED:
2454 /* Proper resolution for function pointer equality may require
2455 that these symbols perhaps be resolved dynamically, even though
2456 we should be resolving them to the current module. */
2457 if (!ignore_protected)
2458 binding_stays_local_p = TRUE;
2459 break;
2461 default:
2462 break;
2465 /* If it isn't defined locally, then clearly it's dynamic. */
2466 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2467 return TRUE;
2469 /* Otherwise, the symbol is dynamic if binding rules don't tell
2470 us that it remains local. */
2471 return !binding_stays_local_p;
2474 /* Return true if the symbol referred to by H should be considered
2475 to resolve local to the current module, and false otherwise. Differs
2476 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2477 undefined symbols and weak symbols. */
2479 bfd_boolean
2480 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2481 struct bfd_link_info *info,
2482 bfd_boolean local_protected)
2484 /* If it's a local sym, of course we resolve locally. */
2485 if (h == NULL)
2486 return TRUE;
2488 /* If we don't have a definition in a regular file, then we can't
2489 resolve locally. The sym is either undefined or dynamic. */
2490 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2491 return FALSE;
2493 /* Forced local symbols resolve locally. */
2494 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2495 return TRUE;
2497 /* As do non-dynamic symbols. */
2498 if (h->dynindx == -1)
2499 return TRUE;
2501 /* At this point, we know the symbol is defined and dynamic. In an
2502 executable it must resolve locally, likewise when building symbolic
2503 shared libraries. */
2504 if (info->executable || info->symbolic)
2505 return TRUE;
2507 /* Now deal with defined dynamic symbols in shared libraries. Ones
2508 with default visibility might not resolve locally. */
2509 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2510 return FALSE;
2512 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2513 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2514 return TRUE;
2516 /* Function pointer equality tests may require that STV_PROTECTED
2517 symbols be treated as dynamic symbols, even when we know that the
2518 dynamic linker will resolve them locally. */
2519 return local_protected;
2522 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2523 aligned. Returns the first TLS output section. */
2525 struct bfd_section *
2526 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2528 struct bfd_section *sec, *tls;
2529 unsigned int align = 0;
2531 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2532 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2533 break;
2534 tls = sec;
2536 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2537 if (sec->alignment_power > align)
2538 align = sec->alignment_power;
2540 elf_hash_table (info)->tls_sec = tls;
2542 /* Ensure the alignment of the first section is the largest alignment,
2543 so that the tls segment starts aligned. */
2544 if (tls != NULL)
2545 tls->alignment_power = align;
2547 return tls;