* configure.in: Define and substitute BFD_HOST_LONG_LONG.
[binutils.git] / bfd / elflink.c
blobd53571d448a04ffef7eca426b02e6d597a0ce01c
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))
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
148 && info->hash->creator->flavour == bfd_target_elf_flavour)
150 s = bfd_make_section (abfd, ".eh_frame_hdr");
151 if (s == NULL
152 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
153 || ! bfd_set_section_alignment (abfd, s, 2))
154 return FALSE;
155 elf_hash_table (info)->eh_info.hdr_sec = s;
158 bed = get_elf_backend_data (abfd);
160 /* Create sections to hold version informations. These are removed
161 if they are not needed. */
162 s = bfd_make_section (abfd, ".gnu.version_d");
163 if (s == NULL
164 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
165 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
166 return FALSE;
168 s = bfd_make_section (abfd, ".gnu.version");
169 if (s == NULL
170 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
171 || ! bfd_set_section_alignment (abfd, s, 1))
172 return FALSE;
174 s = bfd_make_section (abfd, ".gnu.version_r");
175 if (s == NULL
176 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
177 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
178 return FALSE;
180 s = bfd_make_section (abfd, ".dynsym");
181 if (s == NULL
182 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
183 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
184 return FALSE;
186 s = bfd_make_section (abfd, ".dynstr");
187 if (s == NULL
188 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
189 return FALSE;
191 /* Create a strtab to hold the dynamic symbol names. */
192 if (elf_hash_table (info)->dynstr == NULL)
194 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
195 if (elf_hash_table (info)->dynstr == NULL)
196 return FALSE;
199 s = bfd_make_section (abfd, ".dynamic");
200 if (s == NULL
201 || ! bfd_set_section_flags (abfd, s, flags)
202 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
203 return FALSE;
205 /* The special symbol _DYNAMIC is always set to the start of the
206 .dynamic section. This call occurs before we have processed the
207 symbols for any dynamic object, so we don't have to worry about
208 overriding a dynamic definition. We could set _DYNAMIC in a
209 linker script, but we only want to define it if we are, in fact,
210 creating a .dynamic section. We don't want to define it if there
211 is no .dynamic section, since on some ELF platforms the start up
212 code examines it to decide how to initialize the process. */
213 bh = NULL;
214 if (! (_bfd_generic_link_add_one_symbol
215 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
216 get_elf_backend_data (abfd)->collect, &bh)))
217 return FALSE;
218 h = (struct elf_link_hash_entry *) bh;
219 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
220 h->type = STT_OBJECT;
222 if (! info->executable
223 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
224 return FALSE;
226 s = bfd_make_section (abfd, ".hash");
227 if (s == NULL
228 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
229 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
230 return FALSE;
231 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
233 /* Let the backend create the rest of the sections. This lets the
234 backend set the right flags. The backend will normally create
235 the .got and .plt sections. */
236 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
237 return FALSE;
239 elf_hash_table (info)->dynamic_sections_created = TRUE;
241 return TRUE;
244 /* Create dynamic sections when linking against a dynamic object. */
246 bfd_boolean
247 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
249 flagword flags, pltflags;
250 asection *s;
251 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
253 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
254 .rel[a].bss sections. */
256 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
257 | SEC_LINKER_CREATED);
259 pltflags = flags;
260 pltflags |= SEC_CODE;
261 if (bed->plt_not_loaded)
262 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
263 if (bed->plt_readonly)
264 pltflags |= SEC_READONLY;
266 s = bfd_make_section (abfd, ".plt");
267 if (s == NULL
268 || ! bfd_set_section_flags (abfd, s, pltflags)
269 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
270 return FALSE;
272 if (bed->want_plt_sym)
274 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
275 .plt section. */
276 struct elf_link_hash_entry *h;
277 struct bfd_link_hash_entry *bh = NULL;
279 if (! (_bfd_generic_link_add_one_symbol
280 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
281 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
282 return FALSE;
283 h = (struct elf_link_hash_entry *) bh;
284 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
285 h->type = STT_OBJECT;
287 if (! info->executable
288 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
289 return FALSE;
292 s = bfd_make_section (abfd,
293 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
294 if (s == NULL
295 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
296 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
297 return FALSE;
299 if (! _bfd_elf_create_got_section (abfd, info))
300 return FALSE;
302 if (bed->want_dynbss)
304 /* The .dynbss section is a place to put symbols which are defined
305 by dynamic objects, are referenced by regular objects, and are
306 not functions. We must allocate space for them in the process
307 image and use a R_*_COPY reloc to tell the dynamic linker to
308 initialize them at run time. The linker script puts the .dynbss
309 section into the .bss section of the final image. */
310 s = bfd_make_section (abfd, ".dynbss");
311 if (s == NULL
312 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
313 return FALSE;
315 /* The .rel[a].bss section holds copy relocs. This section is not
316 normally needed. We need to create it here, though, so that the
317 linker will map it to an output section. We can't just create it
318 only if we need it, because we will not know whether we need it
319 until we have seen all the input files, and the first time the
320 main linker code calls BFD after examining all the input files
321 (size_dynamic_sections) the input sections have already been
322 mapped to the output sections. If the section turns out not to
323 be needed, we can discard it later. We will never need this
324 section when generating a shared object, since they do not use
325 copy relocs. */
326 if (! info->shared)
328 s = bfd_make_section (abfd,
329 (bed->default_use_rela_p
330 ? ".rela.bss" : ".rel.bss"));
331 if (s == NULL
332 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
333 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
334 return FALSE;
338 return TRUE;
341 /* Record a new dynamic symbol. We record the dynamic symbols as we
342 read the input files, since we need to have a list of all of them
343 before we can determine the final sizes of the output sections.
344 Note that we may actually call this function even though we are not
345 going to output any dynamic symbols; in some cases we know that a
346 symbol should be in the dynamic symbol table, but only if there is
347 one. */
349 bfd_boolean
350 _bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
351 struct elf_link_hash_entry *h)
353 if (h->dynindx == -1)
355 struct elf_strtab_hash *dynstr;
356 char *p, *alc;
357 const char *name;
358 bfd_boolean copy;
359 bfd_size_type indx;
361 /* XXX: The ABI draft says the linker must turn hidden and
362 internal symbols into STB_LOCAL symbols when producing the
363 DSO. However, if ld.so honors st_other in the dynamic table,
364 this would not be necessary. */
365 switch (ELF_ST_VISIBILITY (h->other))
367 case STV_INTERNAL:
368 case STV_HIDDEN:
369 if (h->root.type != bfd_link_hash_undefined
370 && h->root.type != bfd_link_hash_undefweak)
372 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
373 return TRUE;
376 default:
377 break;
380 h->dynindx = elf_hash_table (info)->dynsymcount;
381 ++elf_hash_table (info)->dynsymcount;
383 dynstr = elf_hash_table (info)->dynstr;
384 if (dynstr == NULL)
386 /* Create a strtab to hold the dynamic symbol names. */
387 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
388 if (dynstr == NULL)
389 return FALSE;
392 /* We don't put any version information in the dynamic string
393 table. */
394 name = h->root.root.string;
395 p = strchr (name, ELF_VER_CHR);
396 if (p == NULL)
398 alc = NULL;
399 copy = FALSE;
401 else
403 size_t len = p - name + 1;
405 alc = bfd_malloc (len);
406 if (alc == NULL)
407 return FALSE;
408 memcpy (alc, name, len - 1);
409 alc[len - 1] = '\0';
410 name = alc;
411 copy = TRUE;
414 indx = _bfd_elf_strtab_add (dynstr, name, copy);
416 if (alc != NULL)
417 free (alc);
419 if (indx == (bfd_size_type) -1)
420 return FALSE;
421 h->dynstr_index = indx;
424 return TRUE;
427 /* Record an assignment to a symbol made by a linker script. We need
428 this in case some dynamic object refers to this symbol. */
430 bfd_boolean
431 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
432 struct bfd_link_info *info,
433 const char *name,
434 bfd_boolean provide)
436 struct elf_link_hash_entry *h;
438 if (info->hash->creator->flavour != bfd_target_elf_flavour)
439 return TRUE;
441 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
442 if (h == NULL)
443 return FALSE;
445 if (h->root.type == bfd_link_hash_new)
446 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
448 /* If this symbol is being provided by the linker script, and it is
449 currently defined by a dynamic object, but not by a regular
450 object, then mark it as undefined so that the generic linker will
451 force the correct value. */
452 if (provide
453 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
454 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
455 h->root.type = bfd_link_hash_undefined;
457 /* If this symbol is not being provided by the linker script, and it is
458 currently defined by a dynamic object, but not by a regular object,
459 then clear out any version information because the symbol will not be
460 associated with the dynamic object any more. */
461 if (!provide
462 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
463 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
464 h->verinfo.verdef = NULL;
466 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
468 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
469 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
470 || info->shared)
471 && h->dynindx == -1)
473 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
474 return FALSE;
476 /* If this is a weak defined symbol, and we know a corresponding
477 real symbol from the same dynamic object, make sure the real
478 symbol is also made into a dynamic symbol. */
479 if (h->weakdef != NULL
480 && h->weakdef->dynindx == -1)
482 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
483 return FALSE;
487 return TRUE;
490 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
491 success, and 2 on a failure caused by attempting to record a symbol
492 in a discarded section, eg. a discarded link-once section symbol. */
495 elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
496 bfd *input_bfd,
497 long input_indx)
499 bfd_size_type amt;
500 struct elf_link_local_dynamic_entry *entry;
501 struct elf_link_hash_table *eht;
502 struct elf_strtab_hash *dynstr;
503 unsigned long dynstr_index;
504 char *name;
505 Elf_External_Sym_Shndx eshndx;
506 char esym[sizeof (Elf64_External_Sym)];
508 if (! is_elf_hash_table (info))
509 return 0;
511 /* See if the entry exists already. */
512 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
513 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
514 return 1;
516 amt = sizeof (*entry);
517 entry = bfd_alloc (input_bfd, amt);
518 if (entry == NULL)
519 return 0;
521 /* Go find the symbol, so that we can find it's name. */
522 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
523 1, input_indx, &entry->isym, esym, &eshndx))
525 bfd_release (input_bfd, entry);
526 return 0;
529 if (entry->isym.st_shndx != SHN_UNDEF
530 && (entry->isym.st_shndx < SHN_LORESERVE
531 || entry->isym.st_shndx > SHN_HIRESERVE))
533 asection *s;
535 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
536 if (s == NULL || bfd_is_abs_section (s->output_section))
538 /* We can still bfd_release here as nothing has done another
539 bfd_alloc. We can't do this later in this function. */
540 bfd_release (input_bfd, entry);
541 return 2;
545 name = (bfd_elf_string_from_elf_section
546 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
547 entry->isym.st_name));
549 dynstr = elf_hash_table (info)->dynstr;
550 if (dynstr == NULL)
552 /* Create a strtab to hold the dynamic symbol names. */
553 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
554 if (dynstr == NULL)
555 return 0;
558 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
559 if (dynstr_index == (unsigned long) -1)
560 return 0;
561 entry->isym.st_name = dynstr_index;
563 eht = elf_hash_table (info);
565 entry->next = eht->dynlocal;
566 eht->dynlocal = entry;
567 entry->input_bfd = input_bfd;
568 entry->input_indx = input_indx;
569 eht->dynsymcount++;
571 /* Whatever binding the symbol had before, it's now local. */
572 entry->isym.st_info
573 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
575 /* The dynindx will be set at the end of size_dynamic_sections. */
577 return 1;
580 /* Return the dynindex of a local dynamic symbol. */
582 long
583 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
584 bfd *input_bfd,
585 long input_indx)
587 struct elf_link_local_dynamic_entry *e;
589 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
590 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
591 return e->dynindx;
592 return -1;
595 /* This function is used to renumber the dynamic symbols, if some of
596 them are removed because they are marked as local. This is called
597 via elf_link_hash_traverse. */
599 static bfd_boolean
600 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
601 void *data)
603 size_t *count = data;
605 if (h->root.type == bfd_link_hash_warning)
606 h = (struct elf_link_hash_entry *) h->root.u.i.link;
608 if (h->dynindx != -1)
609 h->dynindx = ++(*count);
611 return TRUE;
614 /* Assign dynsym indices. In a shared library we generate a section
615 symbol for each output section, which come first. Next come all of
616 the back-end allocated local dynamic syms, followed by the rest of
617 the global symbols. */
619 unsigned long
620 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
622 unsigned long dynsymcount = 0;
624 if (info->shared)
626 asection *p;
627 for (p = output_bfd->sections; p ; p = p->next)
628 if ((p->flags & SEC_EXCLUDE) == 0)
629 elf_section_data (p)->dynindx = ++dynsymcount;
632 if (elf_hash_table (info)->dynlocal)
634 struct elf_link_local_dynamic_entry *p;
635 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
636 p->dynindx = ++dynsymcount;
639 elf_link_hash_traverse (elf_hash_table (info),
640 elf_link_renumber_hash_table_dynsyms,
641 &dynsymcount);
643 /* There is an unused NULL entry at the head of the table which
644 we must account for in our count. Unless there weren't any
645 symbols, which means we'll have no table at all. */
646 if (dynsymcount != 0)
647 ++dynsymcount;
649 return elf_hash_table (info)->dynsymcount = dynsymcount;
652 /* This function is called when we want to define a new symbol. It
653 handles the various cases which arise when we find a definition in
654 a dynamic object, or when there is already a definition in a
655 dynamic object. The new symbol is described by NAME, SYM, PSEC,
656 and PVALUE. We set SYM_HASH to the hash table entry. We set
657 OVERRIDE if the old symbol is overriding a new definition. We set
658 TYPE_CHANGE_OK if it is OK for the type to change. We set
659 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
660 change, we mean that we shouldn't warn if the type or size does
661 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
662 a shared object. */
664 bfd_boolean
665 _bfd_elf_merge_symbol (bfd *abfd,
666 struct bfd_link_info *info,
667 const char *name,
668 Elf_Internal_Sym *sym,
669 asection **psec,
670 bfd_vma *pvalue,
671 struct elf_link_hash_entry **sym_hash,
672 bfd_boolean *skip,
673 bfd_boolean *override,
674 bfd_boolean *type_change_ok,
675 bfd_boolean *size_change_ok,
676 bfd_boolean dt_needed)
678 asection *sec;
679 struct elf_link_hash_entry *h;
680 struct elf_link_hash_entry *flip;
681 int bind;
682 bfd *oldbfd;
683 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
684 bfd_boolean newweakdef, oldweakdef, newweakundef, oldweakundef;
686 *skip = FALSE;
687 *override = FALSE;
689 sec = *psec;
690 bind = ELF_ST_BIND (sym->st_info);
692 if (! bfd_is_und_section (sec))
693 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
694 else
695 h = ((struct elf_link_hash_entry *)
696 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
697 if (h == NULL)
698 return FALSE;
699 *sym_hash = h;
701 /* This code is for coping with dynamic objects, and is only useful
702 if we are doing an ELF link. */
703 if (info->hash->creator != abfd->xvec)
704 return TRUE;
706 /* For merging, we only care about real symbols. */
708 while (h->root.type == bfd_link_hash_indirect
709 || h->root.type == bfd_link_hash_warning)
710 h = (struct elf_link_hash_entry *) h->root.u.i.link;
712 /* If we just created the symbol, mark it as being an ELF symbol.
713 Other than that, there is nothing to do--there is no merge issue
714 with a newly defined symbol--so we just return. */
716 if (h->root.type == bfd_link_hash_new)
718 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
719 return TRUE;
722 /* OLDBFD is a BFD associated with the existing symbol. */
724 switch (h->root.type)
726 default:
727 oldbfd = NULL;
728 break;
730 case bfd_link_hash_undefined:
731 case bfd_link_hash_undefweak:
732 oldbfd = h->root.u.undef.abfd;
733 break;
735 case bfd_link_hash_defined:
736 case bfd_link_hash_defweak:
737 oldbfd = h->root.u.def.section->owner;
738 break;
740 case bfd_link_hash_common:
741 oldbfd = h->root.u.c.p->section->owner;
742 break;
745 /* In cases involving weak versioned symbols, we may wind up trying
746 to merge a symbol with itself. Catch that here, to avoid the
747 confusion that results if we try to override a symbol with
748 itself. The additional tests catch cases like
749 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
750 dynamic object, which we do want to handle here. */
751 if (abfd == oldbfd
752 && ((abfd->flags & DYNAMIC) == 0
753 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
754 return TRUE;
756 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
757 respectively, is from a dynamic object. */
759 if ((abfd->flags & DYNAMIC) != 0)
760 newdyn = TRUE;
761 else
762 newdyn = FALSE;
764 if (oldbfd != NULL)
765 olddyn = (oldbfd->flags & DYNAMIC) != 0;
766 else
768 asection *hsec;
770 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
771 indices used by MIPS ELF. */
772 switch (h->root.type)
774 default:
775 hsec = NULL;
776 break;
778 case bfd_link_hash_defined:
779 case bfd_link_hash_defweak:
780 hsec = h->root.u.def.section;
781 break;
783 case bfd_link_hash_common:
784 hsec = h->root.u.c.p->section;
785 break;
788 if (hsec == NULL)
789 olddyn = FALSE;
790 else
791 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
794 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
795 respectively, appear to be a definition rather than reference. */
797 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
798 newdef = FALSE;
799 else
800 newdef = TRUE;
802 if (h->root.type == bfd_link_hash_undefined
803 || h->root.type == bfd_link_hash_undefweak
804 || h->root.type == bfd_link_hash_common)
805 olddef = FALSE;
806 else
807 olddef = TRUE;
809 /* We need to rememeber if a symbol has a definition in a dynamic
810 object or is weak in all dynamic objects. Internal and hidden
811 visibility will make it unavailable to dynamic objects. */
812 if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
814 if (!bfd_is_und_section (sec))
815 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF;
816 else
818 /* Check if this symbol is weak in all dynamic objects. If it
819 is the first time we see it in a dynamic object, we mark
820 if it is weak. Otherwise, we clear it. */
821 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
823 if (bind == STB_WEAK)
824 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK;
826 else if (bind != STB_WEAK)
827 h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK;
831 /* If the old symbol has non-default visibility, we ignore the new
832 definition from a dynamic object. */
833 if (newdyn
834 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
835 && !bfd_is_und_section (sec))
837 *skip = TRUE;
838 /* Make sure this symbol is dynamic. */
839 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
840 /* A protected symbol has external availability. Make sure it is
841 recorded as dynamic.
843 FIXME: Should we check type and size for protected symbol? */
844 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
845 return _bfd_elf_link_record_dynamic_symbol (info, h);
846 else
847 return TRUE;
849 else if (!newdyn
850 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
851 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
853 /* If the new symbol with non-default visibility comes from a
854 relocatable file and the old definition comes from a dynamic
855 object, we remove the old definition. */
856 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
857 h = *sym_hash;
858 h->root.type = bfd_link_hash_new;
859 h->root.u.undef.abfd = NULL;
860 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
862 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
863 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC
864 | ELF_LINK_DYNAMIC_DEF);
866 /* FIXME: Should we check type and size for protected symbol? */
867 h->size = 0;
868 h->type = 0;
869 return TRUE;
872 /* We need to treat weak definiton right, depending on if there is a
873 definition from a dynamic object. */
874 if (bind == STB_WEAK)
876 if (olddef)
878 newweakdef = TRUE;
879 newweakundef = FALSE;
881 else
883 newweakdef = FALSE;
884 newweakundef = TRUE;
887 else
888 newweakdef = newweakundef = FALSE;
890 /* If the new weak definition comes from a relocatable file and the
891 old symbol comes from a dynamic object, we treat the new one as
892 strong. */
893 if (newweakdef && !newdyn && olddyn)
894 newweakdef = FALSE;
896 if (h->root.type == bfd_link_hash_defweak)
898 oldweakdef = TRUE;
899 oldweakundef = FALSE;
901 else if (h->root.type == bfd_link_hash_undefweak)
903 oldweakdef = FALSE;
904 oldweakundef = TRUE;
906 else
907 oldweakdef = oldweakundef = FALSE;
909 /* If the old weak definition comes from a relocatable file and the
910 new symbol comes from a dynamic object, we treat the old one as
911 strong. */
912 if (oldweakdef && !olddyn && newdyn)
913 oldweakdef = FALSE;
915 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
916 symbol, respectively, appears to be a common symbol in a dynamic
917 object. If a symbol appears in an uninitialized section, and is
918 not weak, and is not a function, then it may be a common symbol
919 which was resolved when the dynamic object was created. We want
920 to treat such symbols specially, because they raise special
921 considerations when setting the symbol size: if the symbol
922 appears as a common symbol in a regular object, and the size in
923 the regular object is larger, we must make sure that we use the
924 larger size. This problematic case can always be avoided in C,
925 but it must be handled correctly when using Fortran shared
926 libraries.
928 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
929 likewise for OLDDYNCOMMON and OLDDEF.
931 Note that this test is just a heuristic, and that it is quite
932 possible to have an uninitialized symbol in a shared object which
933 is really a definition, rather than a common symbol. This could
934 lead to some minor confusion when the symbol really is a common
935 symbol in some regular object. However, I think it will be
936 harmless. */
938 if (newdyn
939 && newdef
940 && (sec->flags & SEC_ALLOC) != 0
941 && (sec->flags & SEC_LOAD) == 0
942 && sym->st_size > 0
943 && !newweakdef
944 && !newweakundef
945 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
946 newdyncommon = TRUE;
947 else
948 newdyncommon = FALSE;
950 if (olddyn
951 && olddef
952 && h->root.type == bfd_link_hash_defined
953 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
954 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
955 && (h->root.u.def.section->flags & SEC_LOAD) == 0
956 && h->size > 0
957 && h->type != STT_FUNC)
958 olddyncommon = TRUE;
959 else
960 olddyncommon = FALSE;
962 /* It's OK to change the type if either the existing symbol or the
963 new symbol is weak unless it comes from a DT_NEEDED entry of
964 a shared object, in which case, the DT_NEEDED entry may not be
965 required at the run time. */
967 if ((! dt_needed && oldweakdef)
968 || oldweakundef
969 || newweakdef
970 || newweakundef)
971 *type_change_ok = TRUE;
973 /* It's OK to change the size if either the existing symbol or the
974 new symbol is weak, or if the old symbol is undefined. */
976 if (*type_change_ok
977 || h->root.type == bfd_link_hash_undefined)
978 *size_change_ok = TRUE;
980 /* If both the old and the new symbols look like common symbols in a
981 dynamic object, set the size of the symbol to the larger of the
982 two. */
984 if (olddyncommon
985 && newdyncommon
986 && sym->st_size != h->size)
988 /* Since we think we have two common symbols, issue a multiple
989 common warning if desired. Note that we only warn if the
990 size is different. If the size is the same, we simply let
991 the old symbol override the new one as normally happens with
992 symbols defined in dynamic objects. */
994 if (! ((*info->callbacks->multiple_common)
995 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
996 h->size, abfd, bfd_link_hash_common, sym->st_size)))
997 return FALSE;
999 if (sym->st_size > h->size)
1000 h->size = sym->st_size;
1002 *size_change_ok = TRUE;
1005 /* If we are looking at a dynamic object, and we have found a
1006 definition, we need to see if the symbol was already defined by
1007 some other object. If so, we want to use the existing
1008 definition, and we do not want to report a multiple symbol
1009 definition error; we do this by clobbering *PSEC to be
1010 bfd_und_section_ptr.
1012 We treat a common symbol as a definition if the symbol in the
1013 shared library is a function, since common symbols always
1014 represent variables; this can cause confusion in principle, but
1015 any such confusion would seem to indicate an erroneous program or
1016 shared library. We also permit a common symbol in a regular
1017 object to override a weak symbol in a shared object.
1019 We prefer a non-weak definition in a shared library to a weak
1020 definition in the executable unless it comes from a DT_NEEDED
1021 entry of a shared object, in which case, the DT_NEEDED entry
1022 may not be required at the run time. */
1024 if (newdyn
1025 && newdef
1026 && (olddef
1027 || (h->root.type == bfd_link_hash_common
1028 && (newweakdef
1029 || newweakundef
1030 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
1031 && (!oldweakdef
1032 || dt_needed
1033 || newweakdef
1034 || newweakundef))
1036 *override = TRUE;
1037 newdef = FALSE;
1038 newdyncommon = FALSE;
1040 *psec = sec = bfd_und_section_ptr;
1041 *size_change_ok = TRUE;
1043 /* If we get here when the old symbol is a common symbol, then
1044 we are explicitly letting it override a weak symbol or
1045 function in a dynamic object, and we don't want to warn about
1046 a type change. If the old symbol is a defined symbol, a type
1047 change warning may still be appropriate. */
1049 if (h->root.type == bfd_link_hash_common)
1050 *type_change_ok = TRUE;
1053 /* Handle the special case of an old common symbol merging with a
1054 new symbol which looks like a common symbol in a shared object.
1055 We change *PSEC and *PVALUE to make the new symbol look like a
1056 common symbol, and let _bfd_generic_link_add_one_symbol will do
1057 the right thing. */
1059 if (newdyncommon
1060 && h->root.type == bfd_link_hash_common)
1062 *override = TRUE;
1063 newdef = FALSE;
1064 newdyncommon = FALSE;
1065 *pvalue = sym->st_size;
1066 *psec = sec = bfd_com_section_ptr;
1067 *size_change_ok = TRUE;
1070 /* If the old symbol is from a dynamic object, and the new symbol is
1071 a definition which is not from a dynamic object, then the new
1072 symbol overrides the old symbol. Symbols from regular files
1073 always take precedence over symbols from dynamic objects, even if
1074 they are defined after the dynamic object in the link.
1076 As above, we again permit a common symbol in a regular object to
1077 override a definition in a shared object if the shared object
1078 symbol is a function or is weak.
1080 As above, we permit a non-weak definition in a shared object to
1081 override a weak definition in a regular object. */
1083 flip = NULL;
1084 if (! newdyn
1085 && (newdef
1086 || (bfd_is_com_section (sec)
1087 && (oldweakdef || h->type == STT_FUNC)))
1088 && olddyn
1089 && olddef
1090 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1091 && ((!newweakdef && !newweakundef) || oldweakdef))
1093 /* Change the hash table entry to undefined, and let
1094 _bfd_generic_link_add_one_symbol do the right thing with the
1095 new definition. */
1097 h->root.type = bfd_link_hash_undefined;
1098 h->root.u.undef.abfd = h->root.u.def.section->owner;
1099 *size_change_ok = TRUE;
1101 olddef = FALSE;
1102 olddyncommon = FALSE;
1104 /* We again permit a type change when a common symbol may be
1105 overriding a function. */
1107 if (bfd_is_com_section (sec))
1108 *type_change_ok = TRUE;
1110 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1111 flip = *sym_hash;
1112 else
1113 /* This union may have been set to be non-NULL when this symbol
1114 was seen in a dynamic object. We must force the union to be
1115 NULL, so that it is correct for a regular symbol. */
1116 h->verinfo.vertree = NULL;
1119 /* Handle the special case of a new common symbol merging with an
1120 old symbol that looks like it might be a common symbol defined in
1121 a shared object. Note that we have already handled the case in
1122 which a new common symbol should simply override the definition
1123 in the shared library. */
1125 if (! newdyn
1126 && bfd_is_com_section (sec)
1127 && olddyncommon)
1129 /* It would be best if we could set the hash table entry to a
1130 common symbol, but we don't know what to use for the section
1131 or the alignment. */
1132 if (! ((*info->callbacks->multiple_common)
1133 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1134 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1135 return FALSE;
1137 /* If the predumed common symbol in the dynamic object is
1138 larger, pretend that the new symbol has its size. */
1140 if (h->size > *pvalue)
1141 *pvalue = h->size;
1143 /* FIXME: We no longer know the alignment required by the symbol
1144 in the dynamic object, so we just wind up using the one from
1145 the regular object. */
1147 olddef = FALSE;
1148 olddyncommon = FALSE;
1150 h->root.type = bfd_link_hash_undefined;
1151 h->root.u.undef.abfd = h->root.u.def.section->owner;
1153 *size_change_ok = TRUE;
1154 *type_change_ok = TRUE;
1156 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1157 flip = *sym_hash;
1158 else
1159 h->verinfo.vertree = NULL;
1162 if (flip != NULL)
1164 /* Handle the case where we had a versioned symbol in a dynamic
1165 library and now find a definition in a normal object. In this
1166 case, we make the versioned symbol point to the normal one. */
1167 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1168 flip->root.type = h->root.type;
1169 h->root.type = bfd_link_hash_indirect;
1170 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1171 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1172 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1173 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1175 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1176 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1180 /* Handle the special case of a weak definition in a regular object
1181 followed by a non-weak definition in a shared object. In this
1182 case, we prefer the definition in the shared object unless it
1183 comes from a DT_NEEDED entry of a shared object, in which case,
1184 the DT_NEEDED entry may not be required at the run time. */
1185 if (olddef
1186 && ! dt_needed
1187 && oldweakdef
1188 && newdef
1189 && newdyn
1190 && !newweakdef
1191 && !newweakundef)
1193 /* To make this work we have to frob the flags so that the rest
1194 of the code does not think we are using the regular
1195 definition. */
1196 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
1197 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
1198 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
1199 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1200 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
1201 | ELF_LINK_HASH_DEF_DYNAMIC);
1203 /* If H is the target of an indirection, we want the caller to
1204 use H rather than the indirect symbol. Otherwise if we are
1205 defining a new indirect symbol we will wind up attaching it
1206 to the entry we are overriding. */
1207 *sym_hash = h;
1210 /* Handle the special case of a non-weak definition in a shared
1211 object followed by a weak definition in a regular object. In
1212 this case we prefer the definition in the shared object. To make
1213 this work we have to tell the caller to not treat the new symbol
1214 as a definition. */
1215 if (olddef
1216 && olddyn
1217 && !oldweakdef
1218 && newdef
1219 && ! newdyn
1220 && (newweakdef || newweakundef))
1221 *override = TRUE;
1223 return TRUE;
1226 /* This function is called to create an indirect symbol from the
1227 default for the symbol with the default version if needed. The
1228 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1229 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
1230 indicates if it comes from a DT_NEEDED entry of a shared object. */
1232 bfd_boolean
1233 _bfd_elf_add_default_symbol (bfd *abfd,
1234 struct bfd_link_info *info,
1235 struct elf_link_hash_entry *h,
1236 const char *name,
1237 Elf_Internal_Sym *sym,
1238 asection **psec,
1239 bfd_vma *value,
1240 bfd_boolean *dynsym,
1241 bfd_boolean override,
1242 bfd_boolean dt_needed)
1244 bfd_boolean type_change_ok;
1245 bfd_boolean size_change_ok;
1246 bfd_boolean skip;
1247 char *shortname;
1248 struct elf_link_hash_entry *hi;
1249 struct bfd_link_hash_entry *bh;
1250 const struct elf_backend_data *bed;
1251 bfd_boolean collect;
1252 bfd_boolean dynamic;
1253 char *p;
1254 size_t len, shortlen;
1255 asection *sec;
1257 /* If this symbol has a version, and it is the default version, we
1258 create an indirect symbol from the default name to the fully
1259 decorated name. This will cause external references which do not
1260 specify a version to be bound to this version of the symbol. */
1261 p = strchr (name, ELF_VER_CHR);
1262 if (p == NULL || p[1] != ELF_VER_CHR)
1263 return TRUE;
1265 if (override)
1267 /* We are overridden by an old defition. We need to check if we
1268 need to create the indirect symbol from the default name. */
1269 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1270 FALSE, FALSE);
1271 BFD_ASSERT (hi != NULL);
1272 if (hi == h)
1273 return TRUE;
1274 while (hi->root.type == bfd_link_hash_indirect
1275 || hi->root.type == bfd_link_hash_warning)
1277 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1278 if (hi == h)
1279 return TRUE;
1283 bed = get_elf_backend_data (abfd);
1284 collect = bed->collect;
1285 dynamic = (abfd->flags & DYNAMIC) != 0;
1287 shortlen = p - name;
1288 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1289 if (shortname == NULL)
1290 return FALSE;
1291 memcpy (shortname, name, shortlen);
1292 shortname[shortlen] = '\0';
1294 /* We are going to create a new symbol. Merge it with any existing
1295 symbol with this name. For the purposes of the merge, act as
1296 though we were defining the symbol we just defined, although we
1297 actually going to define an indirect symbol. */
1298 type_change_ok = FALSE;
1299 size_change_ok = FALSE;
1300 sec = *psec;
1301 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1302 &hi, &skip, &override, &type_change_ok,
1303 &size_change_ok, dt_needed))
1304 return FALSE;
1306 if (skip)
1307 goto nondefault;
1309 if (! override)
1311 bh = &hi->root;
1312 if (! (_bfd_generic_link_add_one_symbol
1313 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1314 0, name, FALSE, collect, &bh)))
1315 return FALSE;
1316 hi = (struct elf_link_hash_entry *) bh;
1318 else
1320 /* In this case the symbol named SHORTNAME is overriding the
1321 indirect symbol we want to add. We were planning on making
1322 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1323 is the name without a version. NAME is the fully versioned
1324 name, and it is the default version.
1326 Overriding means that we already saw a definition for the
1327 symbol SHORTNAME in a regular object, and it is overriding
1328 the symbol defined in the dynamic object.
1330 When this happens, we actually want to change NAME, the
1331 symbol we just added, to refer to SHORTNAME. This will cause
1332 references to NAME in the shared object to become references
1333 to SHORTNAME in the regular object. This is what we expect
1334 when we override a function in a shared object: that the
1335 references in the shared object will be mapped to the
1336 definition in the regular object. */
1338 while (hi->root.type == bfd_link_hash_indirect
1339 || hi->root.type == bfd_link_hash_warning)
1340 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1342 h->root.type = bfd_link_hash_indirect;
1343 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1344 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1346 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1347 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1348 if (hi->elf_link_hash_flags
1349 & (ELF_LINK_HASH_REF_REGULAR
1350 | ELF_LINK_HASH_DEF_REGULAR))
1352 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1353 return FALSE;
1357 /* Now set HI to H, so that the following code will set the
1358 other fields correctly. */
1359 hi = h;
1362 /* If there is a duplicate definition somewhere, then HI may not
1363 point to an indirect symbol. We will have reported an error to
1364 the user in that case. */
1366 if (hi->root.type == bfd_link_hash_indirect)
1368 struct elf_link_hash_entry *ht;
1370 /* If the symbol became indirect, then we assume that we have
1371 not seen a definition before. */
1372 BFD_ASSERT ((hi->elf_link_hash_flags
1373 & (ELF_LINK_HASH_DEF_DYNAMIC
1374 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1376 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1377 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1379 /* See if the new flags lead us to realize that the symbol must
1380 be dynamic. */
1381 if (! *dynsym)
1383 if (! dynamic)
1385 if (info->shared
1386 || ((hi->elf_link_hash_flags
1387 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1388 *dynsym = TRUE;
1390 else
1392 if ((hi->elf_link_hash_flags
1393 & ELF_LINK_HASH_REF_REGULAR) != 0)
1394 *dynsym = TRUE;
1399 /* We also need to define an indirection from the nondefault version
1400 of the symbol. */
1402 nondefault:
1403 len = strlen (name);
1404 shortname = bfd_hash_allocate (&info->hash->table, len);
1405 if (shortname == NULL)
1406 return FALSE;
1407 memcpy (shortname, name, shortlen);
1408 memcpy (shortname + shortlen, p + 1, len - shortlen);
1410 /* Once again, merge with any existing symbol. */
1411 type_change_ok = FALSE;
1412 size_change_ok = FALSE;
1413 sec = *psec;
1414 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1415 &hi, &skip, &override, &type_change_ok,
1416 &size_change_ok, dt_needed))
1417 return FALSE;
1419 if (skip)
1420 return TRUE;
1422 if (override)
1424 /* Here SHORTNAME is a versioned name, so we don't expect to see
1425 the type of override we do in the case above unless it is
1426 overridden by a versioned definiton. */
1427 if (hi->root.type != bfd_link_hash_defined
1428 && hi->root.type != bfd_link_hash_defweak)
1429 (*_bfd_error_handler)
1430 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1431 bfd_archive_filename (abfd), shortname);
1433 else
1435 bh = &hi->root;
1436 if (! (_bfd_generic_link_add_one_symbol
1437 (info, abfd, shortname, BSF_INDIRECT,
1438 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1439 return FALSE;
1440 hi = (struct elf_link_hash_entry *) bh;
1442 /* If there is a duplicate definition somewhere, then HI may not
1443 point to an indirect symbol. We will have reported an error
1444 to the user in that case. */
1446 if (hi->root.type == bfd_link_hash_indirect)
1448 /* If the symbol became indirect, then we assume that we have
1449 not seen a definition before. */
1450 BFD_ASSERT ((hi->elf_link_hash_flags
1451 & (ELF_LINK_HASH_DEF_DYNAMIC
1452 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1454 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1456 /* See if the new flags lead us to realize that the symbol
1457 must be dynamic. */
1458 if (! *dynsym)
1460 if (! dynamic)
1462 if (info->shared
1463 || ((hi->elf_link_hash_flags
1464 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1465 *dynsym = TRUE;
1467 else
1469 if ((hi->elf_link_hash_flags
1470 & ELF_LINK_HASH_REF_REGULAR) != 0)
1471 *dynsym = TRUE;
1477 return TRUE;
1480 /* This routine is used to export all defined symbols into the dynamic
1481 symbol table. It is called via elf_link_hash_traverse. */
1483 bfd_boolean
1484 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1486 struct elf_info_failed *eif = data;
1488 /* Ignore indirect symbols. These are added by the versioning code. */
1489 if (h->root.type == bfd_link_hash_indirect)
1490 return TRUE;
1492 if (h->root.type == bfd_link_hash_warning)
1493 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1495 if (h->dynindx == -1
1496 && (h->elf_link_hash_flags
1497 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1499 struct bfd_elf_version_tree *t;
1500 struct bfd_elf_version_expr *d;
1502 for (t = eif->verdefs; t != NULL; t = t->next)
1504 if (t->globals != NULL)
1506 for (d = t->globals; d != NULL; d = d->next)
1508 if ((*d->match) (d, h->root.root.string))
1509 goto doit;
1513 if (t->locals != NULL)
1515 for (d = t->locals ; d != NULL; d = d->next)
1517 if ((*d->match) (d, h->root.root.string))
1518 return TRUE;
1523 if (!eif->verdefs)
1525 doit:
1526 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
1528 eif->failed = TRUE;
1529 return FALSE;
1534 return TRUE;
1537 /* Look through the symbols which are defined in other shared
1538 libraries and referenced here. Update the list of version
1539 dependencies. This will be put into the .gnu.version_r section.
1540 This function is called via elf_link_hash_traverse. */
1542 bfd_boolean
1543 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1544 void *data)
1546 struct elf_find_verdep_info *rinfo = data;
1547 Elf_Internal_Verneed *t;
1548 Elf_Internal_Vernaux *a;
1549 bfd_size_type amt;
1551 if (h->root.type == bfd_link_hash_warning)
1552 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1554 /* We only care about symbols defined in shared objects with version
1555 information. */
1556 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1557 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1558 || h->dynindx == -1
1559 || h->verinfo.verdef == NULL)
1560 return TRUE;
1562 /* See if we already know about this version. */
1563 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1565 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1566 continue;
1568 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1569 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1570 return TRUE;
1572 break;
1575 /* This is a new version. Add it to tree we are building. */
1577 if (t == NULL)
1579 amt = sizeof *t;
1580 t = bfd_zalloc (rinfo->output_bfd, amt);
1581 if (t == NULL)
1583 rinfo->failed = TRUE;
1584 return FALSE;
1587 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1588 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1589 elf_tdata (rinfo->output_bfd)->verref = t;
1592 amt = sizeof *a;
1593 a = bfd_zalloc (rinfo->output_bfd, amt);
1595 /* Note that we are copying a string pointer here, and testing it
1596 above. If bfd_elf_string_from_elf_section is ever changed to
1597 discard the string data when low in memory, this will have to be
1598 fixed. */
1599 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1601 a->vna_flags = h->verinfo.verdef->vd_flags;
1602 a->vna_nextptr = t->vn_auxptr;
1604 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1605 ++rinfo->vers;
1607 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1609 t->vn_auxptr = a;
1611 return TRUE;
1614 /* Figure out appropriate versions for all the symbols. We may not
1615 have the version number script until we have read all of the input
1616 files, so until that point we don't know which symbols should be
1617 local. This function is called via elf_link_hash_traverse. */
1619 bfd_boolean
1620 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1622 struct elf_assign_sym_version_info *sinfo;
1623 struct bfd_link_info *info;
1624 const struct elf_backend_data *bed;
1625 struct elf_info_failed eif;
1626 char *p;
1627 bfd_size_type amt;
1629 sinfo = data;
1630 info = sinfo->info;
1632 if (h->root.type == bfd_link_hash_warning)
1633 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1635 /* Fix the symbol flags. */
1636 eif.failed = FALSE;
1637 eif.info = info;
1638 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1640 if (eif.failed)
1641 sinfo->failed = TRUE;
1642 return FALSE;
1645 /* We only need version numbers for symbols defined in regular
1646 objects. */
1647 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1648 return TRUE;
1650 bed = get_elf_backend_data (sinfo->output_bfd);
1651 p = strchr (h->root.root.string, ELF_VER_CHR);
1652 if (p != NULL && h->verinfo.vertree == NULL)
1654 struct bfd_elf_version_tree *t;
1655 bfd_boolean hidden;
1657 hidden = TRUE;
1659 /* There are two consecutive ELF_VER_CHR characters if this is
1660 not a hidden symbol. */
1661 ++p;
1662 if (*p == ELF_VER_CHR)
1664 hidden = FALSE;
1665 ++p;
1668 /* If there is no version string, we can just return out. */
1669 if (*p == '\0')
1671 if (hidden)
1672 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1673 return TRUE;
1676 /* Look for the version. If we find it, it is no longer weak. */
1677 for (t = sinfo->verdefs; t != NULL; t = t->next)
1679 if (strcmp (t->name, p) == 0)
1681 size_t len;
1682 char *alc;
1683 struct bfd_elf_version_expr *d;
1685 len = p - h->root.root.string;
1686 alc = bfd_malloc (len);
1687 if (alc == NULL)
1688 return FALSE;
1689 memcpy (alc, h->root.root.string, len - 1);
1690 alc[len - 1] = '\0';
1691 if (alc[len - 2] == ELF_VER_CHR)
1692 alc[len - 2] = '\0';
1694 h->verinfo.vertree = t;
1695 t->used = TRUE;
1696 d = NULL;
1698 if (t->globals != NULL)
1700 for (d = t->globals; d != NULL; d = d->next)
1701 if ((*d->match) (d, alc))
1702 break;
1705 /* See if there is anything to force this symbol to
1706 local scope. */
1707 if (d == NULL && t->locals != NULL)
1709 for (d = t->locals; d != NULL; d = d->next)
1711 if ((*d->match) (d, alc))
1713 if (h->dynindx != -1
1714 && info->shared
1715 && ! info->export_dynamic)
1717 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1720 break;
1725 free (alc);
1726 break;
1730 /* If we are building an application, we need to create a
1731 version node for this version. */
1732 if (t == NULL && info->executable)
1734 struct bfd_elf_version_tree **pp;
1735 int version_index;
1737 /* If we aren't going to export this symbol, we don't need
1738 to worry about it. */
1739 if (h->dynindx == -1)
1740 return TRUE;
1742 amt = sizeof *t;
1743 t = bfd_alloc (sinfo->output_bfd, amt);
1744 if (t == NULL)
1746 sinfo->failed = TRUE;
1747 return FALSE;
1750 t->next = NULL;
1751 t->name = p;
1752 t->globals = NULL;
1753 t->locals = NULL;
1754 t->deps = NULL;
1755 t->name_indx = (unsigned int) -1;
1756 t->used = TRUE;
1758 version_index = 1;
1759 /* Don't count anonymous version tag. */
1760 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1761 version_index = 0;
1762 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1763 ++version_index;
1764 t->vernum = version_index;
1766 *pp = t;
1768 h->verinfo.vertree = t;
1770 else if (t == NULL)
1772 /* We could not find the version for a symbol when
1773 generating a shared archive. Return an error. */
1774 (*_bfd_error_handler)
1775 (_("%s: undefined versioned symbol name %s"),
1776 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
1777 bfd_set_error (bfd_error_bad_value);
1778 sinfo->failed = TRUE;
1779 return FALSE;
1782 if (hidden)
1783 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1786 /* If we don't have a version for this symbol, see if we can find
1787 something. */
1788 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1790 struct bfd_elf_version_tree *t;
1791 struct bfd_elf_version_tree *local_ver;
1792 struct bfd_elf_version_expr *d;
1794 /* See if can find what version this symbol is in. If the
1795 symbol is supposed to be local, then don't actually register
1796 it. */
1797 local_ver = NULL;
1798 for (t = sinfo->verdefs; t != NULL; t = t->next)
1800 if (t->globals != NULL)
1802 bfd_boolean matched;
1804 matched = FALSE;
1805 for (d = t->globals; d != NULL; d = d->next)
1807 if ((*d->match) (d, h->root.root.string))
1809 if (d->symver)
1810 matched = TRUE;
1811 else
1813 /* There is a version without definition. Make
1814 the symbol the default definition for this
1815 version. */
1816 h->verinfo.vertree = t;
1817 local_ver = NULL;
1818 d->script = 1;
1819 break;
1824 if (d != NULL)
1825 break;
1826 else if (matched)
1827 /* There is no undefined version for this symbol. Hide the
1828 default one. */
1829 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1832 if (t->locals != NULL)
1834 for (d = t->locals; d != NULL; d = d->next)
1836 /* If the match is "*", keep looking for a more
1837 explicit, perhaps even global, match. */
1838 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
1839 local_ver = t;
1840 else if ((*d->match) (d, h->root.root.string))
1842 local_ver = t;
1843 break;
1847 if (d != NULL)
1848 break;
1852 if (local_ver != NULL)
1854 h->verinfo.vertree = local_ver;
1855 if (h->dynindx != -1
1856 && info->shared
1857 && ! info->export_dynamic)
1859 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1864 return TRUE;
1867 /* Read and swap the relocs from the section indicated by SHDR. This
1868 may be either a REL or a RELA section. The relocations are
1869 translated into RELA relocations and stored in INTERNAL_RELOCS,
1870 which should have already been allocated to contain enough space.
1871 The EXTERNAL_RELOCS are a buffer where the external form of the
1872 relocations should be stored.
1874 Returns FALSE if something goes wrong. */
1876 static bfd_boolean
1877 elf_link_read_relocs_from_section (bfd *abfd,
1878 Elf_Internal_Shdr *shdr,
1879 void *external_relocs,
1880 Elf_Internal_Rela *internal_relocs)
1882 const struct elf_backend_data *bed;
1883 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1884 const bfd_byte *erela;
1885 const bfd_byte *erelaend;
1886 Elf_Internal_Rela *irela;
1888 /* If there aren't any relocations, that's OK. */
1889 if (!shdr)
1890 return TRUE;
1892 /* Position ourselves at the start of the section. */
1893 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1894 return FALSE;
1896 /* Read the relocations. */
1897 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1898 return FALSE;
1900 bed = get_elf_backend_data (abfd);
1902 /* Convert the external relocations to the internal format. */
1903 if (shdr->sh_entsize == bed->s->sizeof_rel)
1904 swap_in = bed->s->swap_reloc_in;
1905 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1906 swap_in = bed->s->swap_reloca_in;
1907 else
1909 bfd_set_error (bfd_error_wrong_format);
1910 return FALSE;
1913 erela = external_relocs;
1914 erelaend = erela + NUM_SHDR_ENTRIES (shdr) * shdr->sh_entsize;
1915 irela = internal_relocs;
1916 while (erela < erelaend)
1918 (*swap_in) (abfd, erela, irela);
1919 irela += bed->s->int_rels_per_ext_rel;
1920 erela += shdr->sh_entsize;
1923 return TRUE;
1926 /* Read and swap the relocs for a section O. They may have been
1927 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1928 not NULL, they are used as buffers to read into. They are known to
1929 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1930 the return value is allocated using either malloc or bfd_alloc,
1931 according to the KEEP_MEMORY argument. If O has two relocation
1932 sections (both REL and RELA relocations), then the REL_HDR
1933 relocations will appear first in INTERNAL_RELOCS, followed by the
1934 REL_HDR2 relocations. */
1936 Elf_Internal_Rela *
1937 _bfd_elf_link_read_relocs (bfd *abfd,
1938 asection *o,
1939 void *external_relocs,
1940 Elf_Internal_Rela *internal_relocs,
1941 bfd_boolean keep_memory)
1943 Elf_Internal_Shdr *rel_hdr;
1944 void *alloc1 = NULL;
1945 Elf_Internal_Rela *alloc2 = NULL;
1946 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1948 if (elf_section_data (o)->relocs != NULL)
1949 return elf_section_data (o)->relocs;
1951 if (o->reloc_count == 0)
1952 return NULL;
1954 rel_hdr = &elf_section_data (o)->rel_hdr;
1956 if (internal_relocs == NULL)
1958 bfd_size_type size;
1960 size = o->reloc_count;
1961 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1962 if (keep_memory)
1963 internal_relocs = bfd_alloc (abfd, size);
1964 else
1965 internal_relocs = alloc2 = bfd_malloc (size);
1966 if (internal_relocs == NULL)
1967 goto error_return;
1970 if (external_relocs == NULL)
1972 bfd_size_type size = rel_hdr->sh_size;
1974 if (elf_section_data (o)->rel_hdr2)
1975 size += elf_section_data (o)->rel_hdr2->sh_size;
1976 alloc1 = bfd_malloc (size);
1977 if (alloc1 == NULL)
1978 goto error_return;
1979 external_relocs = alloc1;
1982 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
1983 external_relocs,
1984 internal_relocs))
1985 goto error_return;
1986 if (!elf_link_read_relocs_from_section
1987 (abfd,
1988 elf_section_data (o)->rel_hdr2,
1989 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1990 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1991 * bed->s->int_rels_per_ext_rel)))
1992 goto error_return;
1994 /* Cache the results for next time, if we can. */
1995 if (keep_memory)
1996 elf_section_data (o)->relocs = internal_relocs;
1998 if (alloc1 != NULL)
1999 free (alloc1);
2001 /* Don't free alloc2, since if it was allocated we are passing it
2002 back (under the name of internal_relocs). */
2004 return internal_relocs;
2006 error_return:
2007 if (alloc1 != NULL)
2008 free (alloc1);
2009 if (alloc2 != NULL)
2010 free (alloc2);
2011 return NULL;
2014 /* Compute the size of, and allocate space for, REL_HDR which is the
2015 section header for a section containing relocations for O. */
2017 bfd_boolean
2018 _bfd_elf_link_size_reloc_section (bfd *abfd,
2019 Elf_Internal_Shdr *rel_hdr,
2020 asection *o)
2022 bfd_size_type reloc_count;
2023 bfd_size_type num_rel_hashes;
2025 /* Figure out how many relocations there will be. */
2026 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2027 reloc_count = elf_section_data (o)->rel_count;
2028 else
2029 reloc_count = elf_section_data (o)->rel_count2;
2031 num_rel_hashes = o->reloc_count;
2032 if (num_rel_hashes < reloc_count)
2033 num_rel_hashes = reloc_count;
2035 /* That allows us to calculate the size of the section. */
2036 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2038 /* The contents field must last into write_object_contents, so we
2039 allocate it with bfd_alloc rather than malloc. Also since we
2040 cannot be sure that the contents will actually be filled in,
2041 we zero the allocated space. */
2042 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2043 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2044 return FALSE;
2046 /* We only allocate one set of hash entries, so we only do it the
2047 first time we are called. */
2048 if (elf_section_data (o)->rel_hashes == NULL
2049 && num_rel_hashes)
2051 struct elf_link_hash_entry **p;
2053 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2054 if (p == NULL)
2055 return FALSE;
2057 elf_section_data (o)->rel_hashes = p;
2060 return TRUE;
2063 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2064 originated from the section given by INPUT_REL_HDR) to the
2065 OUTPUT_BFD. */
2067 bfd_boolean
2068 _bfd_elf_link_output_relocs (bfd *output_bfd,
2069 asection *input_section,
2070 Elf_Internal_Shdr *input_rel_hdr,
2071 Elf_Internal_Rela *internal_relocs)
2073 Elf_Internal_Rela *irela;
2074 Elf_Internal_Rela *irelaend;
2075 bfd_byte *erel;
2076 Elf_Internal_Shdr *output_rel_hdr;
2077 asection *output_section;
2078 unsigned int *rel_countp = NULL;
2079 const struct elf_backend_data *bed;
2080 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2082 output_section = input_section->output_section;
2083 output_rel_hdr = NULL;
2085 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2086 == input_rel_hdr->sh_entsize)
2088 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2089 rel_countp = &elf_section_data (output_section)->rel_count;
2091 else if (elf_section_data (output_section)->rel_hdr2
2092 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2093 == input_rel_hdr->sh_entsize))
2095 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2096 rel_countp = &elf_section_data (output_section)->rel_count2;
2098 else
2100 (*_bfd_error_handler)
2101 (_("%s: relocation size mismatch in %s section %s"),
2102 bfd_get_filename (output_bfd),
2103 bfd_archive_filename (input_section->owner),
2104 input_section->name);
2105 bfd_set_error (bfd_error_wrong_object_format);
2106 return FALSE;
2109 bed = get_elf_backend_data (output_bfd);
2110 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2111 swap_out = bed->s->swap_reloc_out;
2112 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2113 swap_out = bed->s->swap_reloca_out;
2114 else
2115 abort ();
2117 erel = output_rel_hdr->contents;
2118 erel += *rel_countp * input_rel_hdr->sh_entsize;
2119 irela = internal_relocs;
2120 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2121 * bed->s->int_rels_per_ext_rel);
2122 while (irela < irelaend)
2124 (*swap_out) (output_bfd, irela, erel);
2125 irela += bed->s->int_rels_per_ext_rel;
2126 erel += input_rel_hdr->sh_entsize;
2129 /* Bump the counter, so that we know where to add the next set of
2130 relocations. */
2131 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2133 return TRUE;
2136 /* Fix up the flags for a symbol. This handles various cases which
2137 can only be fixed after all the input files are seen. This is
2138 currently called by both adjust_dynamic_symbol and
2139 assign_sym_version, which is unnecessary but perhaps more robust in
2140 the face of future changes. */
2142 bfd_boolean
2143 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2144 struct elf_info_failed *eif)
2146 /* If this symbol was mentioned in a non-ELF file, try to set
2147 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2148 permit a non-ELF file to correctly refer to a symbol defined in
2149 an ELF dynamic object. */
2150 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2152 while (h->root.type == bfd_link_hash_indirect)
2153 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2155 if (h->root.type != bfd_link_hash_defined
2156 && h->root.type != bfd_link_hash_defweak)
2157 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2158 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2159 else
2161 if (h->root.u.def.section->owner != NULL
2162 && (bfd_get_flavour (h->root.u.def.section->owner)
2163 == bfd_target_elf_flavour))
2164 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2165 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2166 else
2167 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2170 if (h->dynindx == -1
2171 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2172 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2174 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2176 eif->failed = TRUE;
2177 return FALSE;
2181 else
2183 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2184 was first seen in a non-ELF file. Fortunately, if the symbol
2185 was first seen in an ELF file, we're probably OK unless the
2186 symbol was defined in a non-ELF file. Catch that case here.
2187 FIXME: We're still in trouble if the symbol was first seen in
2188 a dynamic object, and then later in a non-ELF regular object. */
2189 if ((h->root.type == bfd_link_hash_defined
2190 || h->root.type == bfd_link_hash_defweak)
2191 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2192 && (h->root.u.def.section->owner != NULL
2193 ? (bfd_get_flavour (h->root.u.def.section->owner)
2194 != bfd_target_elf_flavour)
2195 : (bfd_is_abs_section (h->root.u.def.section)
2196 && (h->elf_link_hash_flags
2197 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2198 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2201 /* If this is a final link, and the symbol was defined as a common
2202 symbol in a regular object file, and there was no definition in
2203 any dynamic object, then the linker will have allocated space for
2204 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2205 flag will not have been set. */
2206 if (h->root.type == bfd_link_hash_defined
2207 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2208 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2209 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2210 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2211 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2213 /* If -Bsymbolic was used (which means to bind references to global
2214 symbols to the definition within the shared object), and this
2215 symbol was defined in a regular object, then it actually doesn't
2216 need a PLT entry. Likewise, if the symbol has non-default
2217 visibility. If the symbol has hidden or internal visibility, we
2218 will force it local. */
2219 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2220 && eif->info->shared
2221 && is_elf_hash_table (eif->info)
2222 && (eif->info->symbolic
2223 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2224 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2226 const struct elf_backend_data *bed;
2227 bfd_boolean force_local;
2229 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2231 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2232 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2233 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2236 /* If a weak undefined symbol has non-default visibility, we also
2237 hide it from the dynamic linker. */
2238 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2239 && h->root.type == bfd_link_hash_undefweak)
2241 const struct elf_backend_data *bed;
2242 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2243 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2246 /* If this is a weak defined symbol in a dynamic object, and we know
2247 the real definition in the dynamic object, copy interesting flags
2248 over to the real definition. */
2249 if (h->weakdef != NULL)
2251 struct elf_link_hash_entry *weakdef;
2253 weakdef = h->weakdef;
2254 if (h->root.type == bfd_link_hash_indirect)
2255 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2257 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2258 || h->root.type == bfd_link_hash_defweak);
2259 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2260 || weakdef->root.type == bfd_link_hash_defweak);
2261 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2263 /* If the real definition is defined by a regular object file,
2264 don't do anything special. See the longer description in
2265 _bfd_elf_adjust_dynamic_symbol, below. */
2266 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2267 h->weakdef = NULL;
2268 else
2270 const struct elf_backend_data *bed;
2272 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2273 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2277 return TRUE;
2280 /* Make the backend pick a good value for a dynamic symbol. This is
2281 called via elf_link_hash_traverse, and also calls itself
2282 recursively. */
2284 bfd_boolean
2285 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2287 struct elf_info_failed *eif = data;
2288 bfd *dynobj;
2289 const struct elf_backend_data *bed;
2291 if (! is_elf_hash_table (eif->info))
2292 return FALSE;
2294 if (h->root.type == bfd_link_hash_warning)
2296 h->plt = elf_hash_table (eif->info)->init_offset;
2297 h->got = elf_hash_table (eif->info)->init_offset;
2299 /* When warning symbols are created, they **replace** the "real"
2300 entry in the hash table, thus we never get to see the real
2301 symbol in a hash traversal. So look at it now. */
2302 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2305 /* Ignore indirect symbols. These are added by the versioning code. */
2306 if (h->root.type == bfd_link_hash_indirect)
2307 return TRUE;
2309 /* Fix the symbol flags. */
2310 if (! _bfd_elf_fix_symbol_flags (h, eif))
2311 return FALSE;
2313 /* If this symbol does not require a PLT entry, and it is not
2314 defined by a dynamic object, or is not referenced by a regular
2315 object, ignore it. We do have to handle a weak defined symbol,
2316 even if no regular object refers to it, if we decided to add it
2317 to the dynamic symbol table. FIXME: Do we normally need to worry
2318 about symbols which are defined by one dynamic object and
2319 referenced by another one? */
2320 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2321 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2322 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2323 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2324 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2326 h->plt = elf_hash_table (eif->info)->init_offset;
2327 return TRUE;
2330 /* If we've already adjusted this symbol, don't do it again. This
2331 can happen via a recursive call. */
2332 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2333 return TRUE;
2335 /* Don't look at this symbol again. Note that we must set this
2336 after checking the above conditions, because we may look at a
2337 symbol once, decide not to do anything, and then get called
2338 recursively later after REF_REGULAR is set below. */
2339 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2341 /* If this is a weak definition, and we know a real definition, and
2342 the real symbol is not itself defined by a regular object file,
2343 then get a good value for the real definition. We handle the
2344 real symbol first, for the convenience of the backend routine.
2346 Note that there is a confusing case here. If the real definition
2347 is defined by a regular object file, we don't get the real symbol
2348 from the dynamic object, but we do get the weak symbol. If the
2349 processor backend uses a COPY reloc, then if some routine in the
2350 dynamic object changes the real symbol, we will not see that
2351 change in the corresponding weak symbol. This is the way other
2352 ELF linkers work as well, and seems to be a result of the shared
2353 library model.
2355 I will clarify this issue. Most SVR4 shared libraries define the
2356 variable _timezone and define timezone as a weak synonym. The
2357 tzset call changes _timezone. If you write
2358 extern int timezone;
2359 int _timezone = 5;
2360 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2361 you might expect that, since timezone is a synonym for _timezone,
2362 the same number will print both times. However, if the processor
2363 backend uses a COPY reloc, then actually timezone will be copied
2364 into your process image, and, since you define _timezone
2365 yourself, _timezone will not. Thus timezone and _timezone will
2366 wind up at different memory locations. The tzset call will set
2367 _timezone, leaving timezone unchanged. */
2369 if (h->weakdef != NULL)
2371 /* If we get to this point, we know there is an implicit
2372 reference by a regular object file via the weak symbol H.
2373 FIXME: Is this really true? What if the traversal finds
2374 H->WEAKDEF before it finds H? */
2375 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2377 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
2378 return FALSE;
2381 /* If a symbol has no type and no size and does not require a PLT
2382 entry, then we are probably about to do the wrong thing here: we
2383 are probably going to create a COPY reloc for an empty object.
2384 This case can arise when a shared object is built with assembly
2385 code, and the assembly code fails to set the symbol type. */
2386 if (h->size == 0
2387 && h->type == STT_NOTYPE
2388 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2389 (*_bfd_error_handler)
2390 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2391 h->root.root.string);
2393 dynobj = elf_hash_table (eif->info)->dynobj;
2394 bed = get_elf_backend_data (dynobj);
2395 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2397 eif->failed = TRUE;
2398 return FALSE;
2401 return TRUE;
2404 /* Adjust all external symbols pointing into SEC_MERGE sections
2405 to reflect the object merging within the sections. */
2407 bfd_boolean
2408 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2410 asection *sec;
2412 if (h->root.type == bfd_link_hash_warning)
2413 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2415 if ((h->root.type == bfd_link_hash_defined
2416 || h->root.type == bfd_link_hash_defweak)
2417 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2418 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2420 bfd *output_bfd = data;
2422 h->root.u.def.value =
2423 _bfd_merged_section_offset (output_bfd,
2424 &h->root.u.def.section,
2425 elf_section_data (sec)->sec_info,
2426 h->root.u.def.value, 0);
2429 return TRUE;
2432 /* Returns false if the symbol referred to by H should be considered
2433 to resolve local to the current module, and true if it should be
2434 considered to bind dynamically. */
2436 bfd_boolean
2437 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2438 struct bfd_link_info *info,
2439 bfd_boolean ignore_protected)
2441 bfd_boolean binding_stays_local_p;
2443 if (h == NULL)
2444 return FALSE;
2446 while (h->root.type == bfd_link_hash_indirect
2447 || h->root.type == bfd_link_hash_warning)
2448 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2450 /* If it was forced local, then clearly it's not dynamic. */
2451 if (h->dynindx == -1)
2452 return FALSE;
2453 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2454 return FALSE;
2456 /* Identify the cases where name binding rules say that a
2457 visible symbol resolves locally. */
2458 binding_stays_local_p = info->executable || info->symbolic;
2460 switch (ELF_ST_VISIBILITY (h->other))
2462 case STV_INTERNAL:
2463 case STV_HIDDEN:
2464 return FALSE;
2466 case STV_PROTECTED:
2467 /* Proper resolution for function pointer equality may require
2468 that these symbols perhaps be resolved dynamically, even though
2469 we should be resolving them to the current module. */
2470 if (!ignore_protected)
2471 binding_stays_local_p = TRUE;
2472 break;
2474 default:
2475 break;
2478 /* If it isn't defined locally, then clearly it's dynamic. */
2479 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2480 return TRUE;
2482 /* Otherwise, the symbol is dynamic if binding rules don't tell
2483 us that it remains local. */
2484 return !binding_stays_local_p;
2487 /* Return true if the symbol referred to by H should be considered
2488 to resolve local to the current module, and false otherwise. Differs
2489 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2490 undefined symbols and weak symbols. */
2492 bfd_boolean
2493 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2494 struct bfd_link_info *info,
2495 bfd_boolean local_protected)
2497 /* If it's a local sym, of course we resolve locally. */
2498 if (h == NULL)
2499 return TRUE;
2501 /* If we don't have a definition in a regular file, then we can't
2502 resolve locally. The sym is either undefined or dynamic. */
2503 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2504 return FALSE;
2506 /* Forced local symbols resolve locally. */
2507 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2508 return TRUE;
2510 /* As do non-dynamic symbols. */
2511 if (h->dynindx == -1)
2512 return TRUE;
2514 /* At this point, we know the symbol is defined and dynamic. In an
2515 executable it must resolve locally, likewise when building symbolic
2516 shared libraries. */
2517 if (info->executable || info->symbolic)
2518 return TRUE;
2520 /* Now deal with defined dynamic symbols in shared libraries. Ones
2521 with default visibility might not resolve locally. */
2522 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2523 return FALSE;
2525 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2526 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2527 return TRUE;
2529 /* Function pointer equality tests may require that STV_PROTECTED
2530 symbols be treated as dynamic symbols, even when we know that the
2531 dynamic linker will resolve them locally. */
2532 return local_protected;