* elf-bfd.h (RELOC_FOR_GLOBAL_SYMBOL): Add input_bfd, input_section
[binutils.git] / bfd / elflink.c
blob39bf46f769e384c541297df4db345ac4c8b99029
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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 /* Since we're defining the symbol, don't let it seem to have not
434 been defined. record_dynamic_symbol and size_dynamic_sections
435 may depend on this. */
436 if (h->root.type == bfd_link_hash_undefweak
437 || h->root.type == bfd_link_hash_undefined)
438 h->root.type = bfd_link_hash_new;
440 if (h->root.type == bfd_link_hash_new)
441 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
443 /* If this symbol is being provided by the linker script, and it is
444 currently defined by a dynamic object, but not by a regular
445 object, then mark it as undefined so that the generic linker will
446 force the correct value. */
447 if (provide
448 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
449 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
450 h->root.type = bfd_link_hash_undefined;
452 /* If this symbol is not being provided by the linker script, and it is
453 currently defined by a dynamic object, but not by a regular object,
454 then clear out any version information because the symbol will not be
455 associated with the dynamic object any more. */
456 if (!provide
457 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
458 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
459 h->verinfo.verdef = NULL;
461 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
463 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
464 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
465 || info->shared)
466 && h->dynindx == -1)
468 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
469 return FALSE;
471 /* If this is a weak defined symbol, and we know a corresponding
472 real symbol from the same dynamic object, make sure the real
473 symbol is also made into a dynamic symbol. */
474 if (h->weakdef != NULL
475 && h->weakdef->dynindx == -1)
477 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
478 return FALSE;
482 return TRUE;
485 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
486 success, and 2 on a failure caused by attempting to record a symbol
487 in a discarded section, eg. a discarded link-once section symbol. */
490 elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
491 bfd *input_bfd,
492 long input_indx)
494 bfd_size_type amt;
495 struct elf_link_local_dynamic_entry *entry;
496 struct elf_link_hash_table *eht;
497 struct elf_strtab_hash *dynstr;
498 unsigned long dynstr_index;
499 char *name;
500 Elf_External_Sym_Shndx eshndx;
501 char esym[sizeof (Elf64_External_Sym)];
503 if (! is_elf_hash_table (info->hash))
504 return 0;
506 /* See if the entry exists already. */
507 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
508 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
509 return 1;
511 amt = sizeof (*entry);
512 entry = bfd_alloc (input_bfd, amt);
513 if (entry == NULL)
514 return 0;
516 /* Go find the symbol, so that we can find it's name. */
517 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
518 1, input_indx, &entry->isym, esym, &eshndx))
520 bfd_release (input_bfd, entry);
521 return 0;
524 if (entry->isym.st_shndx != SHN_UNDEF
525 && (entry->isym.st_shndx < SHN_LORESERVE
526 || entry->isym.st_shndx > SHN_HIRESERVE))
528 asection *s;
530 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
531 if (s == NULL || bfd_is_abs_section (s->output_section))
533 /* We can still bfd_release here as nothing has done another
534 bfd_alloc. We can't do this later in this function. */
535 bfd_release (input_bfd, entry);
536 return 2;
540 name = (bfd_elf_string_from_elf_section
541 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
542 entry->isym.st_name));
544 dynstr = elf_hash_table (info)->dynstr;
545 if (dynstr == NULL)
547 /* Create a strtab to hold the dynamic symbol names. */
548 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
549 if (dynstr == NULL)
550 return 0;
553 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
554 if (dynstr_index == (unsigned long) -1)
555 return 0;
556 entry->isym.st_name = dynstr_index;
558 eht = elf_hash_table (info);
560 entry->next = eht->dynlocal;
561 eht->dynlocal = entry;
562 entry->input_bfd = input_bfd;
563 entry->input_indx = input_indx;
564 eht->dynsymcount++;
566 /* Whatever binding the symbol had before, it's now local. */
567 entry->isym.st_info
568 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
570 /* The dynindx will be set at the end of size_dynamic_sections. */
572 return 1;
575 /* Return the dynindex of a local dynamic symbol. */
577 long
578 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
579 bfd *input_bfd,
580 long input_indx)
582 struct elf_link_local_dynamic_entry *e;
584 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
585 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
586 return e->dynindx;
587 return -1;
590 /* This function is used to renumber the dynamic symbols, if some of
591 them are removed because they are marked as local. This is called
592 via elf_link_hash_traverse. */
594 static bfd_boolean
595 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
596 void *data)
598 size_t *count = data;
600 if (h->root.type == bfd_link_hash_warning)
601 h = (struct elf_link_hash_entry *) h->root.u.i.link;
603 if (h->dynindx != -1)
604 h->dynindx = ++(*count);
606 return TRUE;
609 /* Assign dynsym indices. In a shared library we generate a section
610 symbol for each output section, which come first. Next come all of
611 the back-end allocated local dynamic syms, followed by the rest of
612 the global symbols. */
614 unsigned long
615 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
617 unsigned long dynsymcount = 0;
619 if (info->shared)
621 asection *p;
622 for (p = output_bfd->sections; p ; p = p->next)
623 if ((p->flags & SEC_EXCLUDE) == 0)
624 elf_section_data (p)->dynindx = ++dynsymcount;
627 if (elf_hash_table (info)->dynlocal)
629 struct elf_link_local_dynamic_entry *p;
630 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
631 p->dynindx = ++dynsymcount;
634 elf_link_hash_traverse (elf_hash_table (info),
635 elf_link_renumber_hash_table_dynsyms,
636 &dynsymcount);
638 /* There is an unused NULL entry at the head of the table which
639 we must account for in our count. Unless there weren't any
640 symbols, which means we'll have no table at all. */
641 if (dynsymcount != 0)
642 ++dynsymcount;
644 return elf_hash_table (info)->dynsymcount = dynsymcount;
647 /* This function is called when we want to define a new symbol. It
648 handles the various cases which arise when we find a definition in
649 a dynamic object, or when there is already a definition in a
650 dynamic object. The new symbol is described by NAME, SYM, PSEC,
651 and PVALUE. We set SYM_HASH to the hash table entry. We set
652 OVERRIDE if the old symbol is overriding a new definition. We set
653 TYPE_CHANGE_OK if it is OK for the type to change. We set
654 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
655 change, we mean that we shouldn't warn if the type or size does
656 change. */
658 bfd_boolean
659 _bfd_elf_merge_symbol (bfd *abfd,
660 struct bfd_link_info *info,
661 const char *name,
662 Elf_Internal_Sym *sym,
663 asection **psec,
664 bfd_vma *pvalue,
665 struct elf_link_hash_entry **sym_hash,
666 bfd_boolean *skip,
667 bfd_boolean *override,
668 bfd_boolean *type_change_ok,
669 bfd_boolean *size_change_ok)
671 asection *sec;
672 struct elf_link_hash_entry *h;
673 struct elf_link_hash_entry *flip;
674 int bind;
675 bfd *oldbfd;
676 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
677 bfd_boolean newweak, oldweak;
679 *skip = FALSE;
680 *override = FALSE;
682 sec = *psec;
683 bind = ELF_ST_BIND (sym->st_info);
685 if (! bfd_is_und_section (sec))
686 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
687 else
688 h = ((struct elf_link_hash_entry *)
689 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
690 if (h == NULL)
691 return FALSE;
692 *sym_hash = h;
694 /* This code is for coping with dynamic objects, and is only useful
695 if we are doing an ELF link. */
696 if (info->hash->creator != abfd->xvec)
697 return TRUE;
699 /* For merging, we only care about real symbols. */
701 while (h->root.type == bfd_link_hash_indirect
702 || h->root.type == bfd_link_hash_warning)
703 h = (struct elf_link_hash_entry *) h->root.u.i.link;
705 /* If we just created the symbol, mark it as being an ELF symbol.
706 Other than that, there is nothing to do--there is no merge issue
707 with a newly defined symbol--so we just return. */
709 if (h->root.type == bfd_link_hash_new)
711 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
712 return TRUE;
715 /* OLDBFD is a BFD associated with the existing symbol. */
717 switch (h->root.type)
719 default:
720 oldbfd = NULL;
721 break;
723 case bfd_link_hash_undefined:
724 case bfd_link_hash_undefweak:
725 oldbfd = h->root.u.undef.abfd;
726 break;
728 case bfd_link_hash_defined:
729 case bfd_link_hash_defweak:
730 oldbfd = h->root.u.def.section->owner;
731 break;
733 case bfd_link_hash_common:
734 oldbfd = h->root.u.c.p->section->owner;
735 break;
738 /* In cases involving weak versioned symbols, we may wind up trying
739 to merge a symbol with itself. Catch that here, to avoid the
740 confusion that results if we try to override a symbol with
741 itself. The additional tests catch cases like
742 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
743 dynamic object, which we do want to handle here. */
744 if (abfd == oldbfd
745 && ((abfd->flags & DYNAMIC) == 0
746 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
747 return TRUE;
749 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
750 respectively, is from a dynamic object. */
752 if ((abfd->flags & DYNAMIC) != 0)
753 newdyn = TRUE;
754 else
755 newdyn = FALSE;
757 if (oldbfd != NULL)
758 olddyn = (oldbfd->flags & DYNAMIC) != 0;
759 else
761 asection *hsec;
763 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
764 indices used by MIPS ELF. */
765 switch (h->root.type)
767 default:
768 hsec = NULL;
769 break;
771 case bfd_link_hash_defined:
772 case bfd_link_hash_defweak:
773 hsec = h->root.u.def.section;
774 break;
776 case bfd_link_hash_common:
777 hsec = h->root.u.c.p->section;
778 break;
781 if (hsec == NULL)
782 olddyn = FALSE;
783 else
784 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
787 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
788 respectively, appear to be a definition rather than reference. */
790 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
791 newdef = FALSE;
792 else
793 newdef = TRUE;
795 if (h->root.type == bfd_link_hash_undefined
796 || h->root.type == bfd_link_hash_undefweak
797 || h->root.type == bfd_link_hash_common)
798 olddef = FALSE;
799 else
800 olddef = TRUE;
802 /* We need to remember if a symbol has a definition in a dynamic
803 object or is weak in all dynamic objects. Internal and hidden
804 visibility will make it unavailable to dynamic objects. */
805 if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
807 if (!bfd_is_und_section (sec))
808 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF;
809 else
811 /* Check if this symbol is weak in all dynamic objects. If it
812 is the first time we see it in a dynamic object, we mark
813 if it is weak. Otherwise, we clear it. */
814 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
816 if (bind == STB_WEAK)
817 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK;
819 else if (bind != STB_WEAK)
820 h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK;
824 /* If the old symbol has non-default visibility, we ignore the new
825 definition from a dynamic object. */
826 if (newdyn
827 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
828 && !bfd_is_und_section (sec))
830 *skip = TRUE;
831 /* Make sure this symbol is dynamic. */
832 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
833 /* A protected symbol has external availability. Make sure it is
834 recorded as dynamic.
836 FIXME: Should we check type and size for protected symbol? */
837 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
838 return _bfd_elf_link_record_dynamic_symbol (info, h);
839 else
840 return TRUE;
842 else if (!newdyn
843 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
844 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
846 /* If the new symbol with non-default visibility comes from a
847 relocatable file and the old definition comes from a dynamic
848 object, we remove the old definition. */
849 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
850 h = *sym_hash;
852 if ((h->root.und_next || info->hash->undefs_tail == &h->root)
853 && bfd_is_und_section (sec))
855 /* If the new symbol is undefined and the old symbol was
856 also undefined before, we need to make sure
857 _bfd_generic_link_add_one_symbol doesn't mess
858 up the linker hash table undefs list. Since the old
859 definition came from a dynamic object, it is still on the
860 undefs list. */
861 h->root.type = bfd_link_hash_undefined;
862 /* FIXME: What if the new symbol is weak undefined? */
863 h->root.u.undef.abfd = abfd;
865 else
867 h->root.type = bfd_link_hash_new;
868 h->root.u.undef.abfd = NULL;
871 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
873 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
874 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC
875 | ELF_LINK_DYNAMIC_DEF);
877 /* FIXME: Should we check type and size for protected symbol? */
878 h->size = 0;
879 h->type = 0;
880 return TRUE;
883 /* Differentiate strong and weak symbols. */
884 newweak = bind == STB_WEAK;
885 oldweak = (h->root.type == bfd_link_hash_defweak
886 || h->root.type == bfd_link_hash_undefweak);
888 /* If a new weak symbol comes from a regular file and the old symbol
889 comes from a dynamic library, we treat the new one as strong.
890 Similarly, an old weak symbol from a regular file is treated as
891 strong when the new symbol comes from a dynamic library. Further,
892 an old weak symbol from a dynamic library is treated as strong if
893 the new symbol is from a dynamic library. This reflects the way
894 glibc's ld.so works. */
895 if (!newdyn && olddyn)
896 newweak = FALSE;
897 if (newdyn)
898 oldweak = FALSE;
900 /* It's OK to change the type if either the existing symbol or the
901 new symbol is weak. A type change is also OK if the old symbol
902 is undefined and the new symbol is defined. */
904 if (oldweak
905 || newweak
906 || (newdef
907 && h->root.type == bfd_link_hash_undefined))
908 *type_change_ok = TRUE;
910 /* It's OK to change the size if either the existing symbol or the
911 new symbol is weak, or if the old symbol is undefined. */
913 if (*type_change_ok
914 || h->root.type == bfd_link_hash_undefined)
915 *size_change_ok = TRUE;
917 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
918 symbol, respectively, appears to be a common symbol in a dynamic
919 object. If a symbol appears in an uninitialized section, and is
920 not weak, and is not a function, then it may be a common symbol
921 which was resolved when the dynamic object was created. We want
922 to treat such symbols specially, because they raise special
923 considerations when setting the symbol size: if the symbol
924 appears as a common symbol in a regular object, and the size in
925 the regular object is larger, we must make sure that we use the
926 larger size. This problematic case can always be avoided in C,
927 but it must be handled correctly when using Fortran shared
928 libraries.
930 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
931 likewise for OLDDYNCOMMON and OLDDEF.
933 Note that this test is just a heuristic, and that it is quite
934 possible to have an uninitialized symbol in a shared object which
935 is really a definition, rather than a common symbol. This could
936 lead to some minor confusion when the symbol really is a common
937 symbol in some regular object. However, I think it will be
938 harmless. */
940 if (newdyn
941 && newdef
942 && !newweak
943 && (sec->flags & SEC_ALLOC) != 0
944 && (sec->flags & SEC_LOAD) == 0
945 && sym->st_size > 0
946 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
947 newdyncommon = TRUE;
948 else
949 newdyncommon = FALSE;
951 if (olddyn
952 && olddef
953 && h->root.type == bfd_link_hash_defined
954 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
955 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
956 && (h->root.u.def.section->flags & SEC_LOAD) == 0
957 && h->size > 0
958 && h->type != STT_FUNC)
959 olddyncommon = TRUE;
960 else
961 olddyncommon = FALSE;
963 /* If both the old and the new symbols look like common symbols in a
964 dynamic object, set the size of the symbol to the larger of the
965 two. */
967 if (olddyncommon
968 && newdyncommon
969 && sym->st_size != h->size)
971 /* Since we think we have two common symbols, issue a multiple
972 common warning if desired. Note that we only warn if the
973 size is different. If the size is the same, we simply let
974 the old symbol override the new one as normally happens with
975 symbols defined in dynamic objects. */
977 if (! ((*info->callbacks->multiple_common)
978 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
979 h->size, abfd, bfd_link_hash_common, sym->st_size)))
980 return FALSE;
982 if (sym->st_size > h->size)
983 h->size = sym->st_size;
985 *size_change_ok = TRUE;
988 /* If we are looking at a dynamic object, and we have found a
989 definition, we need to see if the symbol was already defined by
990 some other object. If so, we want to use the existing
991 definition, and we do not want to report a multiple symbol
992 definition error; we do this by clobbering *PSEC to be
993 bfd_und_section_ptr.
995 We treat a common symbol as a definition if the symbol in the
996 shared library is a function, since common symbols always
997 represent variables; this can cause confusion in principle, but
998 any such confusion would seem to indicate an erroneous program or
999 shared library. We also permit a common symbol in a regular
1000 object to override a weak symbol in a shared object. */
1002 if (newdyn
1003 && newdef
1004 && (olddef
1005 || (h->root.type == bfd_link_hash_common
1006 && (newweak
1007 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1009 *override = TRUE;
1010 newdef = FALSE;
1011 newdyncommon = FALSE;
1013 *psec = sec = bfd_und_section_ptr;
1014 *size_change_ok = TRUE;
1016 /* If we get here when the old symbol is a common symbol, then
1017 we are explicitly letting it override a weak symbol or
1018 function in a dynamic object, and we don't want to warn about
1019 a type change. If the old symbol is a defined symbol, a type
1020 change warning may still be appropriate. */
1022 if (h->root.type == bfd_link_hash_common)
1023 *type_change_ok = TRUE;
1026 /* Handle the special case of an old common symbol merging with a
1027 new symbol which looks like a common symbol in a shared object.
1028 We change *PSEC and *PVALUE to make the new symbol look like a
1029 common symbol, and let _bfd_generic_link_add_one_symbol will do
1030 the right thing. */
1032 if (newdyncommon
1033 && h->root.type == bfd_link_hash_common)
1035 *override = TRUE;
1036 newdef = FALSE;
1037 newdyncommon = FALSE;
1038 *pvalue = sym->st_size;
1039 *psec = sec = bfd_com_section_ptr;
1040 *size_change_ok = TRUE;
1043 /* If the old symbol is from a dynamic object, and the new symbol is
1044 a definition which is not from a dynamic object, then the new
1045 symbol overrides the old symbol. Symbols from regular files
1046 always take precedence over symbols from dynamic objects, even if
1047 they are defined after the dynamic object in the link.
1049 As above, we again permit a common symbol in a regular object to
1050 override a definition in a shared object if the shared object
1051 symbol is a function or is weak. */
1053 flip = NULL;
1054 if (! newdyn
1055 && (newdef
1056 || (bfd_is_com_section (sec)
1057 && (oldweak
1058 || h->type == STT_FUNC)))
1059 && olddyn
1060 && olddef
1061 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
1063 /* Change the hash table entry to undefined, and let
1064 _bfd_generic_link_add_one_symbol do the right thing with the
1065 new definition. */
1067 h->root.type = bfd_link_hash_undefined;
1068 h->root.u.undef.abfd = h->root.u.def.section->owner;
1069 *size_change_ok = TRUE;
1071 olddef = FALSE;
1072 olddyncommon = FALSE;
1074 /* We again permit a type change when a common symbol may be
1075 overriding a function. */
1077 if (bfd_is_com_section (sec))
1078 *type_change_ok = TRUE;
1080 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1081 flip = *sym_hash;
1082 else
1083 /* This union may have been set to be non-NULL when this symbol
1084 was seen in a dynamic object. We must force the union to be
1085 NULL, so that it is correct for a regular symbol. */
1086 h->verinfo.vertree = NULL;
1089 /* Handle the special case of a new common symbol merging with an
1090 old symbol that looks like it might be a common symbol defined in
1091 a shared object. Note that we have already handled the case in
1092 which a new common symbol should simply override the definition
1093 in the shared library. */
1095 if (! newdyn
1096 && bfd_is_com_section (sec)
1097 && olddyncommon)
1099 /* It would be best if we could set the hash table entry to a
1100 common symbol, but we don't know what to use for the section
1101 or the alignment. */
1102 if (! ((*info->callbacks->multiple_common)
1103 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1104 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1105 return FALSE;
1107 /* If the presumed common symbol in the dynamic object is
1108 larger, pretend that the new symbol has its size. */
1110 if (h->size > *pvalue)
1111 *pvalue = h->size;
1113 /* FIXME: We no longer know the alignment required by the symbol
1114 in the dynamic object, so we just wind up using the one from
1115 the regular object. */
1117 olddef = FALSE;
1118 olddyncommon = FALSE;
1120 h->root.type = bfd_link_hash_undefined;
1121 h->root.u.undef.abfd = h->root.u.def.section->owner;
1123 *size_change_ok = TRUE;
1124 *type_change_ok = TRUE;
1126 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1127 flip = *sym_hash;
1128 else
1129 h->verinfo.vertree = NULL;
1132 if (flip != NULL)
1134 /* Handle the case where we had a versioned symbol in a dynamic
1135 library and now find a definition in a normal object. In this
1136 case, we make the versioned symbol point to the normal one. */
1137 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1138 flip->root.type = h->root.type;
1139 h->root.type = bfd_link_hash_indirect;
1140 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1141 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1142 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1143 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1145 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1146 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1150 return TRUE;
1153 /* This function is called to create an indirect symbol from the
1154 default for the symbol with the default version if needed. The
1155 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1156 set DYNSYM if the new indirect symbol is dynamic. */
1158 bfd_boolean
1159 _bfd_elf_add_default_symbol (bfd *abfd,
1160 struct bfd_link_info *info,
1161 struct elf_link_hash_entry *h,
1162 const char *name,
1163 Elf_Internal_Sym *sym,
1164 asection **psec,
1165 bfd_vma *value,
1166 bfd_boolean *dynsym,
1167 bfd_boolean override)
1169 bfd_boolean type_change_ok;
1170 bfd_boolean size_change_ok;
1171 bfd_boolean skip;
1172 char *shortname;
1173 struct elf_link_hash_entry *hi;
1174 struct bfd_link_hash_entry *bh;
1175 const struct elf_backend_data *bed;
1176 bfd_boolean collect;
1177 bfd_boolean dynamic;
1178 char *p;
1179 size_t len, shortlen;
1180 asection *sec;
1182 /* If this symbol has a version, and it is the default version, we
1183 create an indirect symbol from the default name to the fully
1184 decorated name. This will cause external references which do not
1185 specify a version to be bound to this version of the symbol. */
1186 p = strchr (name, ELF_VER_CHR);
1187 if (p == NULL || p[1] != ELF_VER_CHR)
1188 return TRUE;
1190 if (override)
1192 /* We are overridden by an old definition. We need to check if we
1193 need to create the indirect symbol from the default name. */
1194 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1195 FALSE, FALSE);
1196 BFD_ASSERT (hi != NULL);
1197 if (hi == h)
1198 return TRUE;
1199 while (hi->root.type == bfd_link_hash_indirect
1200 || hi->root.type == bfd_link_hash_warning)
1202 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1203 if (hi == h)
1204 return TRUE;
1208 bed = get_elf_backend_data (abfd);
1209 collect = bed->collect;
1210 dynamic = (abfd->flags & DYNAMIC) != 0;
1212 shortlen = p - name;
1213 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1214 if (shortname == NULL)
1215 return FALSE;
1216 memcpy (shortname, name, shortlen);
1217 shortname[shortlen] = '\0';
1219 /* We are going to create a new symbol. Merge it with any existing
1220 symbol with this name. For the purposes of the merge, act as
1221 though we were defining the symbol we just defined, although we
1222 actually going to define an indirect symbol. */
1223 type_change_ok = FALSE;
1224 size_change_ok = FALSE;
1225 sec = *psec;
1226 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1227 &hi, &skip, &override, &type_change_ok,
1228 &size_change_ok))
1229 return FALSE;
1231 if (skip)
1232 goto nondefault;
1234 if (! override)
1236 bh = &hi->root;
1237 if (! (_bfd_generic_link_add_one_symbol
1238 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1239 0, name, FALSE, collect, &bh)))
1240 return FALSE;
1241 hi = (struct elf_link_hash_entry *) bh;
1243 else
1245 /* In this case the symbol named SHORTNAME is overriding the
1246 indirect symbol we want to add. We were planning on making
1247 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1248 is the name without a version. NAME is the fully versioned
1249 name, and it is the default version.
1251 Overriding means that we already saw a definition for the
1252 symbol SHORTNAME in a regular object, and it is overriding
1253 the symbol defined in the dynamic object.
1255 When this happens, we actually want to change NAME, the
1256 symbol we just added, to refer to SHORTNAME. This will cause
1257 references to NAME in the shared object to become references
1258 to SHORTNAME in the regular object. This is what we expect
1259 when we override a function in a shared object: that the
1260 references in the shared object will be mapped to the
1261 definition in the regular object. */
1263 while (hi->root.type == bfd_link_hash_indirect
1264 || hi->root.type == bfd_link_hash_warning)
1265 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1267 h->root.type = bfd_link_hash_indirect;
1268 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1269 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1271 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1272 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1273 if (hi->elf_link_hash_flags
1274 & (ELF_LINK_HASH_REF_REGULAR
1275 | ELF_LINK_HASH_DEF_REGULAR))
1277 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1278 return FALSE;
1282 /* Now set HI to H, so that the following code will set the
1283 other fields correctly. */
1284 hi = h;
1287 /* If there is a duplicate definition somewhere, then HI may not
1288 point to an indirect symbol. We will have reported an error to
1289 the user in that case. */
1291 if (hi->root.type == bfd_link_hash_indirect)
1293 struct elf_link_hash_entry *ht;
1295 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1296 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1298 /* See if the new flags lead us to realize that the symbol must
1299 be dynamic. */
1300 if (! *dynsym)
1302 if (! dynamic)
1304 if (info->shared
1305 || ((hi->elf_link_hash_flags
1306 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1307 *dynsym = TRUE;
1309 else
1311 if ((hi->elf_link_hash_flags
1312 & ELF_LINK_HASH_REF_REGULAR) != 0)
1313 *dynsym = TRUE;
1318 /* We also need to define an indirection from the nondefault version
1319 of the symbol. */
1321 nondefault:
1322 len = strlen (name);
1323 shortname = bfd_hash_allocate (&info->hash->table, len);
1324 if (shortname == NULL)
1325 return FALSE;
1326 memcpy (shortname, name, shortlen);
1327 memcpy (shortname + shortlen, p + 1, len - shortlen);
1329 /* Once again, merge with any existing symbol. */
1330 type_change_ok = FALSE;
1331 size_change_ok = FALSE;
1332 sec = *psec;
1333 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1334 &hi, &skip, &override, &type_change_ok,
1335 &size_change_ok))
1336 return FALSE;
1338 if (skip)
1339 return TRUE;
1341 if (override)
1343 /* Here SHORTNAME is a versioned name, so we don't expect to see
1344 the type of override we do in the case above unless it is
1345 overridden by a versioned definition. */
1346 if (hi->root.type != bfd_link_hash_defined
1347 && hi->root.type != bfd_link_hash_defweak)
1348 (*_bfd_error_handler)
1349 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1350 bfd_archive_filename (abfd), shortname);
1352 else
1354 bh = &hi->root;
1355 if (! (_bfd_generic_link_add_one_symbol
1356 (info, abfd, shortname, BSF_INDIRECT,
1357 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1358 return FALSE;
1359 hi = (struct elf_link_hash_entry *) bh;
1361 /* If there is a duplicate definition somewhere, then HI may not
1362 point to an indirect symbol. We will have reported an error
1363 to the user in that case. */
1365 if (hi->root.type == bfd_link_hash_indirect)
1367 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1369 /* See if the new flags lead us to realize that the symbol
1370 must be dynamic. */
1371 if (! *dynsym)
1373 if (! dynamic)
1375 if (info->shared
1376 || ((hi->elf_link_hash_flags
1377 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1378 *dynsym = TRUE;
1380 else
1382 if ((hi->elf_link_hash_flags
1383 & ELF_LINK_HASH_REF_REGULAR) != 0)
1384 *dynsym = TRUE;
1390 return TRUE;
1393 /* This routine is used to export all defined symbols into the dynamic
1394 symbol table. It is called via elf_link_hash_traverse. */
1396 bfd_boolean
1397 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1399 struct elf_info_failed *eif = data;
1401 /* Ignore indirect symbols. These are added by the versioning code. */
1402 if (h->root.type == bfd_link_hash_indirect)
1403 return TRUE;
1405 if (h->root.type == bfd_link_hash_warning)
1406 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1408 if (h->dynindx == -1
1409 && (h->elf_link_hash_flags
1410 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1412 struct bfd_elf_version_tree *t;
1413 struct bfd_elf_version_expr *d;
1415 for (t = eif->verdefs; t != NULL; t = t->next)
1417 if (t->globals.list != NULL)
1419 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1420 if (d != NULL)
1421 goto doit;
1424 if (t->locals.list != NULL)
1426 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1427 if (d != NULL)
1428 return TRUE;
1432 if (!eif->verdefs)
1434 doit:
1435 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
1437 eif->failed = TRUE;
1438 return FALSE;
1443 return TRUE;
1446 /* Look through the symbols which are defined in other shared
1447 libraries and referenced here. Update the list of version
1448 dependencies. This will be put into the .gnu.version_r section.
1449 This function is called via elf_link_hash_traverse. */
1451 bfd_boolean
1452 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1453 void *data)
1455 struct elf_find_verdep_info *rinfo = data;
1456 Elf_Internal_Verneed *t;
1457 Elf_Internal_Vernaux *a;
1458 bfd_size_type amt;
1460 if (h->root.type == bfd_link_hash_warning)
1461 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1463 /* We only care about symbols defined in shared objects with version
1464 information. */
1465 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1466 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1467 || h->dynindx == -1
1468 || h->verinfo.verdef == NULL)
1469 return TRUE;
1471 /* See if we already know about this version. */
1472 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1474 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1475 continue;
1477 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1478 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1479 return TRUE;
1481 break;
1484 /* This is a new version. Add it to tree we are building. */
1486 if (t == NULL)
1488 amt = sizeof *t;
1489 t = bfd_zalloc (rinfo->output_bfd, amt);
1490 if (t == NULL)
1492 rinfo->failed = TRUE;
1493 return FALSE;
1496 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1497 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1498 elf_tdata (rinfo->output_bfd)->verref = t;
1501 amt = sizeof *a;
1502 a = bfd_zalloc (rinfo->output_bfd, amt);
1504 /* Note that we are copying a string pointer here, and testing it
1505 above. If bfd_elf_string_from_elf_section is ever changed to
1506 discard the string data when low in memory, this will have to be
1507 fixed. */
1508 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1510 a->vna_flags = h->verinfo.verdef->vd_flags;
1511 a->vna_nextptr = t->vn_auxptr;
1513 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1514 ++rinfo->vers;
1516 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1518 t->vn_auxptr = a;
1520 return TRUE;
1523 /* Figure out appropriate versions for all the symbols. We may not
1524 have the version number script until we have read all of the input
1525 files, so until that point we don't know which symbols should be
1526 local. This function is called via elf_link_hash_traverse. */
1528 bfd_boolean
1529 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1531 struct elf_assign_sym_version_info *sinfo;
1532 struct bfd_link_info *info;
1533 const struct elf_backend_data *bed;
1534 struct elf_info_failed eif;
1535 char *p;
1536 bfd_size_type amt;
1538 sinfo = data;
1539 info = sinfo->info;
1541 if (h->root.type == bfd_link_hash_warning)
1542 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1544 /* Fix the symbol flags. */
1545 eif.failed = FALSE;
1546 eif.info = info;
1547 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1549 if (eif.failed)
1550 sinfo->failed = TRUE;
1551 return FALSE;
1554 /* We only need version numbers for symbols defined in regular
1555 objects. */
1556 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1557 return TRUE;
1559 bed = get_elf_backend_data (sinfo->output_bfd);
1560 p = strchr (h->root.root.string, ELF_VER_CHR);
1561 if (p != NULL && h->verinfo.vertree == NULL)
1563 struct bfd_elf_version_tree *t;
1564 bfd_boolean hidden;
1566 hidden = TRUE;
1568 /* There are two consecutive ELF_VER_CHR characters if this is
1569 not a hidden symbol. */
1570 ++p;
1571 if (*p == ELF_VER_CHR)
1573 hidden = FALSE;
1574 ++p;
1577 /* If there is no version string, we can just return out. */
1578 if (*p == '\0')
1580 if (hidden)
1581 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1582 return TRUE;
1585 /* Look for the version. If we find it, it is no longer weak. */
1586 for (t = sinfo->verdefs; t != NULL; t = t->next)
1588 if (strcmp (t->name, p) == 0)
1590 size_t len;
1591 char *alc;
1592 struct bfd_elf_version_expr *d;
1594 len = p - h->root.root.string;
1595 alc = bfd_malloc (len);
1596 if (alc == NULL)
1597 return FALSE;
1598 memcpy (alc, h->root.root.string, len - 1);
1599 alc[len - 1] = '\0';
1600 if (alc[len - 2] == ELF_VER_CHR)
1601 alc[len - 2] = '\0';
1603 h->verinfo.vertree = t;
1604 t->used = TRUE;
1605 d = NULL;
1607 if (t->globals.list != NULL)
1608 d = (*t->match) (&t->globals, NULL, alc);
1610 /* See if there is anything to force this symbol to
1611 local scope. */
1612 if (d == NULL && t->locals.list != NULL)
1614 d = (*t->match) (&t->locals, NULL, alc);
1615 if (d != NULL
1616 && h->dynindx != -1
1617 && info->shared
1618 && ! info->export_dynamic)
1619 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1622 free (alc);
1623 break;
1627 /* If we are building an application, we need to create a
1628 version node for this version. */
1629 if (t == NULL && info->executable)
1631 struct bfd_elf_version_tree **pp;
1632 int version_index;
1634 /* If we aren't going to export this symbol, we don't need
1635 to worry about it. */
1636 if (h->dynindx == -1)
1637 return TRUE;
1639 amt = sizeof *t;
1640 t = bfd_zalloc (sinfo->output_bfd, amt);
1641 if (t == NULL)
1643 sinfo->failed = TRUE;
1644 return FALSE;
1647 t->name = p;
1648 t->name_indx = (unsigned int) -1;
1649 t->used = TRUE;
1651 version_index = 1;
1652 /* Don't count anonymous version tag. */
1653 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1654 version_index = 0;
1655 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1656 ++version_index;
1657 t->vernum = version_index;
1659 *pp = t;
1661 h->verinfo.vertree = t;
1663 else if (t == NULL)
1665 /* We could not find the version for a symbol when
1666 generating a shared archive. Return an error. */
1667 (*_bfd_error_handler)
1668 (_("%s: undefined versioned symbol name %s"),
1669 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
1670 bfd_set_error (bfd_error_bad_value);
1671 sinfo->failed = TRUE;
1672 return FALSE;
1675 if (hidden)
1676 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1679 /* If we don't have a version for this symbol, see if we can find
1680 something. */
1681 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1683 struct bfd_elf_version_tree *t;
1684 struct bfd_elf_version_tree *local_ver;
1685 struct bfd_elf_version_expr *d;
1687 /* See if can find what version this symbol is in. If the
1688 symbol is supposed to be local, then don't actually register
1689 it. */
1690 local_ver = NULL;
1691 for (t = sinfo->verdefs; t != NULL; t = t->next)
1693 if (t->globals.list != NULL)
1695 bfd_boolean matched;
1697 matched = FALSE;
1698 d = NULL;
1699 while ((d = (*t->match) (&t->globals, d,
1700 h->root.root.string)) != NULL)
1701 if (d->symver)
1702 matched = TRUE;
1703 else
1705 /* There is a version without definition. Make
1706 the symbol the default definition for this
1707 version. */
1708 h->verinfo.vertree = t;
1709 local_ver = NULL;
1710 d->script = 1;
1711 break;
1713 if (d != NULL)
1714 break;
1715 else if (matched)
1716 /* There is no undefined version for this symbol. Hide the
1717 default one. */
1718 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1721 if (t->locals.list != NULL)
1723 d = NULL;
1724 while ((d = (*t->match) (&t->locals, d,
1725 h->root.root.string)) != NULL)
1727 local_ver = t;
1728 /* If the match is "*", keep looking for a more
1729 explicit, perhaps even global, match.
1730 XXX: Shouldn't this be !d->wildcard instead? */
1731 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1732 break;
1735 if (d != NULL)
1736 break;
1740 if (local_ver != NULL)
1742 h->verinfo.vertree = local_ver;
1743 if (h->dynindx != -1
1744 && info->shared
1745 && ! info->export_dynamic)
1747 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1752 return TRUE;
1755 /* Read and swap the relocs from the section indicated by SHDR. This
1756 may be either a REL or a RELA section. The relocations are
1757 translated into RELA relocations and stored in INTERNAL_RELOCS,
1758 which should have already been allocated to contain enough space.
1759 The EXTERNAL_RELOCS are a buffer where the external form of the
1760 relocations should be stored.
1762 Returns FALSE if something goes wrong. */
1764 static bfd_boolean
1765 elf_link_read_relocs_from_section (bfd *abfd,
1766 asection *sec,
1767 Elf_Internal_Shdr *shdr,
1768 void *external_relocs,
1769 Elf_Internal_Rela *internal_relocs)
1771 const struct elf_backend_data *bed;
1772 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1773 const bfd_byte *erela;
1774 const bfd_byte *erelaend;
1775 Elf_Internal_Rela *irela;
1776 Elf_Internal_Shdr *symtab_hdr;
1777 size_t nsyms;
1779 /* Position ourselves at the start of the section. */
1780 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1781 return FALSE;
1783 /* Read the relocations. */
1784 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1785 return FALSE;
1787 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1788 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1790 bed = get_elf_backend_data (abfd);
1792 /* Convert the external relocations to the internal format. */
1793 if (shdr->sh_entsize == bed->s->sizeof_rel)
1794 swap_in = bed->s->swap_reloc_in;
1795 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1796 swap_in = bed->s->swap_reloca_in;
1797 else
1799 bfd_set_error (bfd_error_wrong_format);
1800 return FALSE;
1803 erela = external_relocs;
1804 erelaend = erela + shdr->sh_size;
1805 irela = internal_relocs;
1806 while (erela < erelaend)
1808 bfd_vma r_symndx;
1810 (*swap_in) (abfd, erela, irela);
1811 r_symndx = ELF32_R_SYM (irela->r_info);
1812 if (bed->s->arch_size == 64)
1813 r_symndx >>= 24;
1814 if ((size_t) r_symndx >= nsyms)
1816 (*_bfd_error_handler)
1817 (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
1818 bfd_archive_filename (abfd), (unsigned long) r_symndx,
1819 (unsigned long) nsyms, irela->r_offset, sec->name);
1820 bfd_set_error (bfd_error_bad_value);
1821 return FALSE;
1823 irela += bed->s->int_rels_per_ext_rel;
1824 erela += shdr->sh_entsize;
1827 return TRUE;
1830 /* Read and swap the relocs for a section O. They may have been
1831 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1832 not NULL, they are used as buffers to read into. They are known to
1833 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1834 the return value is allocated using either malloc or bfd_alloc,
1835 according to the KEEP_MEMORY argument. If O has two relocation
1836 sections (both REL and RELA relocations), then the REL_HDR
1837 relocations will appear first in INTERNAL_RELOCS, followed by the
1838 REL_HDR2 relocations. */
1840 Elf_Internal_Rela *
1841 _bfd_elf_link_read_relocs (bfd *abfd,
1842 asection *o,
1843 void *external_relocs,
1844 Elf_Internal_Rela *internal_relocs,
1845 bfd_boolean keep_memory)
1847 Elf_Internal_Shdr *rel_hdr;
1848 void *alloc1 = NULL;
1849 Elf_Internal_Rela *alloc2 = NULL;
1850 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1852 if (elf_section_data (o)->relocs != NULL)
1853 return elf_section_data (o)->relocs;
1855 if (o->reloc_count == 0)
1856 return NULL;
1858 rel_hdr = &elf_section_data (o)->rel_hdr;
1860 if (internal_relocs == NULL)
1862 bfd_size_type size;
1864 size = o->reloc_count;
1865 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1866 if (keep_memory)
1867 internal_relocs = bfd_alloc (abfd, size);
1868 else
1869 internal_relocs = alloc2 = bfd_malloc (size);
1870 if (internal_relocs == NULL)
1871 goto error_return;
1874 if (external_relocs == NULL)
1876 bfd_size_type size = rel_hdr->sh_size;
1878 if (elf_section_data (o)->rel_hdr2)
1879 size += elf_section_data (o)->rel_hdr2->sh_size;
1880 alloc1 = bfd_malloc (size);
1881 if (alloc1 == NULL)
1882 goto error_return;
1883 external_relocs = alloc1;
1886 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1887 external_relocs,
1888 internal_relocs))
1889 goto error_return;
1890 if (elf_section_data (o)->rel_hdr2
1891 && (!elf_link_read_relocs_from_section
1892 (abfd, o,
1893 elf_section_data (o)->rel_hdr2,
1894 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1895 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1896 * bed->s->int_rels_per_ext_rel))))
1897 goto error_return;
1899 /* Cache the results for next time, if we can. */
1900 if (keep_memory)
1901 elf_section_data (o)->relocs = internal_relocs;
1903 if (alloc1 != NULL)
1904 free (alloc1);
1906 /* Don't free alloc2, since if it was allocated we are passing it
1907 back (under the name of internal_relocs). */
1909 return internal_relocs;
1911 error_return:
1912 if (alloc1 != NULL)
1913 free (alloc1);
1914 if (alloc2 != NULL)
1915 free (alloc2);
1916 return NULL;
1919 /* Compute the size of, and allocate space for, REL_HDR which is the
1920 section header for a section containing relocations for O. */
1922 bfd_boolean
1923 _bfd_elf_link_size_reloc_section (bfd *abfd,
1924 Elf_Internal_Shdr *rel_hdr,
1925 asection *o)
1927 bfd_size_type reloc_count;
1928 bfd_size_type num_rel_hashes;
1930 /* Figure out how many relocations there will be. */
1931 if (rel_hdr == &elf_section_data (o)->rel_hdr)
1932 reloc_count = elf_section_data (o)->rel_count;
1933 else
1934 reloc_count = elf_section_data (o)->rel_count2;
1936 num_rel_hashes = o->reloc_count;
1937 if (num_rel_hashes < reloc_count)
1938 num_rel_hashes = reloc_count;
1940 /* That allows us to calculate the size of the section. */
1941 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
1943 /* The contents field must last into write_object_contents, so we
1944 allocate it with bfd_alloc rather than malloc. Also since we
1945 cannot be sure that the contents will actually be filled in,
1946 we zero the allocated space. */
1947 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
1948 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
1949 return FALSE;
1951 /* We only allocate one set of hash entries, so we only do it the
1952 first time we are called. */
1953 if (elf_section_data (o)->rel_hashes == NULL
1954 && num_rel_hashes)
1956 struct elf_link_hash_entry **p;
1958 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
1959 if (p == NULL)
1960 return FALSE;
1962 elf_section_data (o)->rel_hashes = p;
1965 return TRUE;
1968 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
1969 originated from the section given by INPUT_REL_HDR) to the
1970 OUTPUT_BFD. */
1972 bfd_boolean
1973 _bfd_elf_link_output_relocs (bfd *output_bfd,
1974 asection *input_section,
1975 Elf_Internal_Shdr *input_rel_hdr,
1976 Elf_Internal_Rela *internal_relocs)
1978 Elf_Internal_Rela *irela;
1979 Elf_Internal_Rela *irelaend;
1980 bfd_byte *erel;
1981 Elf_Internal_Shdr *output_rel_hdr;
1982 asection *output_section;
1983 unsigned int *rel_countp = NULL;
1984 const struct elf_backend_data *bed;
1985 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
1987 output_section = input_section->output_section;
1988 output_rel_hdr = NULL;
1990 if (elf_section_data (output_section)->rel_hdr.sh_entsize
1991 == input_rel_hdr->sh_entsize)
1993 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
1994 rel_countp = &elf_section_data (output_section)->rel_count;
1996 else if (elf_section_data (output_section)->rel_hdr2
1997 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
1998 == input_rel_hdr->sh_entsize))
2000 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2001 rel_countp = &elf_section_data (output_section)->rel_count2;
2003 else
2005 (*_bfd_error_handler)
2006 (_("%s: relocation size mismatch in %s section %s"),
2007 bfd_get_filename (output_bfd),
2008 bfd_archive_filename (input_section->owner),
2009 input_section->name);
2010 bfd_set_error (bfd_error_wrong_object_format);
2011 return FALSE;
2014 bed = get_elf_backend_data (output_bfd);
2015 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2016 swap_out = bed->s->swap_reloc_out;
2017 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2018 swap_out = bed->s->swap_reloca_out;
2019 else
2020 abort ();
2022 erel = output_rel_hdr->contents;
2023 erel += *rel_countp * input_rel_hdr->sh_entsize;
2024 irela = internal_relocs;
2025 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2026 * bed->s->int_rels_per_ext_rel);
2027 while (irela < irelaend)
2029 (*swap_out) (output_bfd, irela, erel);
2030 irela += bed->s->int_rels_per_ext_rel;
2031 erel += input_rel_hdr->sh_entsize;
2034 /* Bump the counter, so that we know where to add the next set of
2035 relocations. */
2036 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2038 return TRUE;
2041 /* Fix up the flags for a symbol. This handles various cases which
2042 can only be fixed after all the input files are seen. This is
2043 currently called by both adjust_dynamic_symbol and
2044 assign_sym_version, which is unnecessary but perhaps more robust in
2045 the face of future changes. */
2047 bfd_boolean
2048 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2049 struct elf_info_failed *eif)
2051 /* If this symbol was mentioned in a non-ELF file, try to set
2052 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2053 permit a non-ELF file to correctly refer to a symbol defined in
2054 an ELF dynamic object. */
2055 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2057 while (h->root.type == bfd_link_hash_indirect)
2058 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2060 if (h->root.type != bfd_link_hash_defined
2061 && h->root.type != bfd_link_hash_defweak)
2062 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2063 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2064 else
2066 if (h->root.u.def.section->owner != NULL
2067 && (bfd_get_flavour (h->root.u.def.section->owner)
2068 == bfd_target_elf_flavour))
2069 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2070 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2071 else
2072 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2075 if (h->dynindx == -1
2076 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2077 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2079 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2081 eif->failed = TRUE;
2082 return FALSE;
2086 else
2088 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2089 was first seen in a non-ELF file. Fortunately, if the symbol
2090 was first seen in an ELF file, we're probably OK unless the
2091 symbol was defined in a non-ELF file. Catch that case here.
2092 FIXME: We're still in trouble if the symbol was first seen in
2093 a dynamic object, and then later in a non-ELF regular object. */
2094 if ((h->root.type == bfd_link_hash_defined
2095 || h->root.type == bfd_link_hash_defweak)
2096 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2097 && (h->root.u.def.section->owner != NULL
2098 ? (bfd_get_flavour (h->root.u.def.section->owner)
2099 != bfd_target_elf_flavour)
2100 : (bfd_is_abs_section (h->root.u.def.section)
2101 && (h->elf_link_hash_flags
2102 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2103 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2106 /* If this is a final link, and the symbol was defined as a common
2107 symbol in a regular object file, and there was no definition in
2108 any dynamic object, then the linker will have allocated space for
2109 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2110 flag will not have been set. */
2111 if (h->root.type == bfd_link_hash_defined
2112 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2113 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2114 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2115 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2116 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2118 /* If -Bsymbolic was used (which means to bind references to global
2119 symbols to the definition within the shared object), and this
2120 symbol was defined in a regular object, then it actually doesn't
2121 need a PLT entry. Likewise, if the symbol has non-default
2122 visibility. If the symbol has hidden or internal visibility, we
2123 will force it local. */
2124 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2125 && eif->info->shared
2126 && is_elf_hash_table (eif->info->hash)
2127 && (eif->info->symbolic
2128 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2129 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2131 const struct elf_backend_data *bed;
2132 bfd_boolean force_local;
2134 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2136 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2137 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2138 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2141 /* If a weak undefined symbol has non-default visibility, we also
2142 hide it from the dynamic linker. */
2143 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2144 && h->root.type == bfd_link_hash_undefweak)
2146 const struct elf_backend_data *bed;
2147 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2148 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2151 /* If this is a weak defined symbol in a dynamic object, and we know
2152 the real definition in the dynamic object, copy interesting flags
2153 over to the real definition. */
2154 if (h->weakdef != NULL)
2156 struct elf_link_hash_entry *weakdef;
2158 weakdef = h->weakdef;
2159 if (h->root.type == bfd_link_hash_indirect)
2160 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2162 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2163 || h->root.type == bfd_link_hash_defweak);
2164 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2165 || weakdef->root.type == bfd_link_hash_defweak);
2166 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2168 /* If the real definition is defined by a regular object file,
2169 don't do anything special. See the longer description in
2170 _bfd_elf_adjust_dynamic_symbol, below. */
2171 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2172 h->weakdef = NULL;
2173 else
2175 const struct elf_backend_data *bed;
2177 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2178 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2182 return TRUE;
2185 /* Make the backend pick a good value for a dynamic symbol. This is
2186 called via elf_link_hash_traverse, and also calls itself
2187 recursively. */
2189 bfd_boolean
2190 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2192 struct elf_info_failed *eif = data;
2193 bfd *dynobj;
2194 const struct elf_backend_data *bed;
2196 if (! is_elf_hash_table (eif->info->hash))
2197 return FALSE;
2199 if (h->root.type == bfd_link_hash_warning)
2201 h->plt = elf_hash_table (eif->info)->init_offset;
2202 h->got = elf_hash_table (eif->info)->init_offset;
2204 /* When warning symbols are created, they **replace** the "real"
2205 entry in the hash table, thus we never get to see the real
2206 symbol in a hash traversal. So look at it now. */
2207 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2210 /* Ignore indirect symbols. These are added by the versioning code. */
2211 if (h->root.type == bfd_link_hash_indirect)
2212 return TRUE;
2214 /* Fix the symbol flags. */
2215 if (! _bfd_elf_fix_symbol_flags (h, eif))
2216 return FALSE;
2218 /* If this symbol does not require a PLT entry, and it is not
2219 defined by a dynamic object, or is not referenced by a regular
2220 object, ignore it. We do have to handle a weak defined symbol,
2221 even if no regular object refers to it, if we decided to add it
2222 to the dynamic symbol table. FIXME: Do we normally need to worry
2223 about symbols which are defined by one dynamic object and
2224 referenced by another one? */
2225 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2226 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2227 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2228 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2229 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2231 h->plt = elf_hash_table (eif->info)->init_offset;
2232 return TRUE;
2235 /* If we've already adjusted this symbol, don't do it again. This
2236 can happen via a recursive call. */
2237 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2238 return TRUE;
2240 /* Don't look at this symbol again. Note that we must set this
2241 after checking the above conditions, because we may look at a
2242 symbol once, decide not to do anything, and then get called
2243 recursively later after REF_REGULAR is set below. */
2244 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2246 /* If this is a weak definition, and we know a real definition, and
2247 the real symbol is not itself defined by a regular object file,
2248 then get a good value for the real definition. We handle the
2249 real symbol first, for the convenience of the backend routine.
2251 Note that there is a confusing case here. If the real definition
2252 is defined by a regular object file, we don't get the real symbol
2253 from the dynamic object, but we do get the weak symbol. If the
2254 processor backend uses a COPY reloc, then if some routine in the
2255 dynamic object changes the real symbol, we will not see that
2256 change in the corresponding weak symbol. This is the way other
2257 ELF linkers work as well, and seems to be a result of the shared
2258 library model.
2260 I will clarify this issue. Most SVR4 shared libraries define the
2261 variable _timezone and define timezone as a weak synonym. The
2262 tzset call changes _timezone. If you write
2263 extern int timezone;
2264 int _timezone = 5;
2265 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2266 you might expect that, since timezone is a synonym for _timezone,
2267 the same number will print both times. However, if the processor
2268 backend uses a COPY reloc, then actually timezone will be copied
2269 into your process image, and, since you define _timezone
2270 yourself, _timezone will not. Thus timezone and _timezone will
2271 wind up at different memory locations. The tzset call will set
2272 _timezone, leaving timezone unchanged. */
2274 if (h->weakdef != NULL)
2276 /* If we get to this point, we know there is an implicit
2277 reference by a regular object file via the weak symbol H.
2278 FIXME: Is this really true? What if the traversal finds
2279 H->WEAKDEF before it finds H? */
2280 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2282 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
2283 return FALSE;
2286 /* If a symbol has no type and no size and does not require a PLT
2287 entry, then we are probably about to do the wrong thing here: we
2288 are probably going to create a COPY reloc for an empty object.
2289 This case can arise when a shared object is built with assembly
2290 code, and the assembly code fails to set the symbol type. */
2291 if (h->size == 0
2292 && h->type == STT_NOTYPE
2293 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2294 (*_bfd_error_handler)
2295 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2296 h->root.root.string);
2298 dynobj = elf_hash_table (eif->info)->dynobj;
2299 bed = get_elf_backend_data (dynobj);
2300 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2302 eif->failed = TRUE;
2303 return FALSE;
2306 return TRUE;
2309 /* Adjust all external symbols pointing into SEC_MERGE sections
2310 to reflect the object merging within the sections. */
2312 bfd_boolean
2313 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2315 asection *sec;
2317 if (h->root.type == bfd_link_hash_warning)
2318 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2320 if ((h->root.type == bfd_link_hash_defined
2321 || h->root.type == bfd_link_hash_defweak)
2322 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2323 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2325 bfd *output_bfd = data;
2327 h->root.u.def.value =
2328 _bfd_merged_section_offset (output_bfd,
2329 &h->root.u.def.section,
2330 elf_section_data (sec)->sec_info,
2331 h->root.u.def.value, 0);
2334 return TRUE;
2337 /* Returns false if the symbol referred to by H should be considered
2338 to resolve local to the current module, and true if it should be
2339 considered to bind dynamically. */
2341 bfd_boolean
2342 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2343 struct bfd_link_info *info,
2344 bfd_boolean ignore_protected)
2346 bfd_boolean binding_stays_local_p;
2348 if (h == NULL)
2349 return FALSE;
2351 while (h->root.type == bfd_link_hash_indirect
2352 || h->root.type == bfd_link_hash_warning)
2353 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2355 /* If it was forced local, then clearly it's not dynamic. */
2356 if (h->dynindx == -1)
2357 return FALSE;
2358 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2359 return FALSE;
2361 /* Identify the cases where name binding rules say that a
2362 visible symbol resolves locally. */
2363 binding_stays_local_p = info->executable || info->symbolic;
2365 switch (ELF_ST_VISIBILITY (h->other))
2367 case STV_INTERNAL:
2368 case STV_HIDDEN:
2369 return FALSE;
2371 case STV_PROTECTED:
2372 /* Proper resolution for function pointer equality may require
2373 that these symbols perhaps be resolved dynamically, even though
2374 we should be resolving them to the current module. */
2375 if (!ignore_protected)
2376 binding_stays_local_p = TRUE;
2377 break;
2379 default:
2380 break;
2383 /* If it isn't defined locally, then clearly it's dynamic. */
2384 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2385 return TRUE;
2387 /* Otherwise, the symbol is dynamic if binding rules don't tell
2388 us that it remains local. */
2389 return !binding_stays_local_p;
2392 /* Return true if the symbol referred to by H should be considered
2393 to resolve local to the current module, and false otherwise. Differs
2394 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2395 undefined symbols and weak symbols. */
2397 bfd_boolean
2398 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2399 struct bfd_link_info *info,
2400 bfd_boolean local_protected)
2402 /* If it's a local sym, of course we resolve locally. */
2403 if (h == NULL)
2404 return TRUE;
2406 /* If we don't have a definition in a regular file, then we can't
2407 resolve locally. The sym is either undefined or dynamic. */
2408 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2409 return FALSE;
2411 /* Forced local symbols resolve locally. */
2412 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2413 return TRUE;
2415 /* As do non-dynamic symbols. */
2416 if (h->dynindx == -1)
2417 return TRUE;
2419 /* At this point, we know the symbol is defined and dynamic. In an
2420 executable it must resolve locally, likewise when building symbolic
2421 shared libraries. */
2422 if (info->executable || info->symbolic)
2423 return TRUE;
2425 /* Now deal with defined dynamic symbols in shared libraries. Ones
2426 with default visibility might not resolve locally. */
2427 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2428 return FALSE;
2430 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2431 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2432 return TRUE;
2434 /* Function pointer equality tests may require that STV_PROTECTED
2435 symbols be treated as dynamic symbols, even when we know that the
2436 dynamic linker will resolve them locally. */
2437 return local_protected;
2440 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2441 aligned. Returns the first TLS output section. */
2443 struct bfd_section *
2444 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2446 struct bfd_section *sec, *tls;
2447 unsigned int align = 0;
2449 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2450 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2451 break;
2452 tls = sec;
2454 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2455 if (sec->alignment_power > align)
2456 align = sec->alignment_power;
2458 elf_hash_table (info)->tls_sec = tls;
2460 /* Ensure the alignment of the first section is the largest alignment,
2461 so that the tls segment starts aligned. */
2462 if (tls != NULL)
2463 tls->alignment_power = align;
2465 return tls;
2468 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2469 static bfd_boolean
2470 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2471 Elf_Internal_Sym *sym)
2473 /* Local symbols do not count, but target specific ones might. */
2474 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2475 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2476 return FALSE;
2478 /* Function symbols do not count. */
2479 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2480 return FALSE;
2482 /* If the section is undefined, then so is the symbol. */
2483 if (sym->st_shndx == SHN_UNDEF)
2484 return FALSE;
2486 /* If the symbol is defined in the common section, then
2487 it is a common definition and so does not count. */
2488 if (sym->st_shndx == SHN_COMMON)
2489 return FALSE;
2491 /* If the symbol is in a target specific section then we
2492 must rely upon the backend to tell us what it is. */
2493 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2494 /* FIXME - this function is not coded yet:
2496 return _bfd_is_global_symbol_definition (abfd, sym);
2498 Instead for now assume that the definition is not global,
2499 Even if this is wrong, at least the linker will behave
2500 in the same way that it used to do. */
2501 return FALSE;
2503 return TRUE;
2506 /* Search the symbol table of the archive element of the archive ABFD
2507 whose archive map contains a mention of SYMDEF, and determine if
2508 the symbol is defined in this element. */
2509 static bfd_boolean
2510 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2512 Elf_Internal_Shdr * hdr;
2513 bfd_size_type symcount;
2514 bfd_size_type extsymcount;
2515 bfd_size_type extsymoff;
2516 Elf_Internal_Sym *isymbuf;
2517 Elf_Internal_Sym *isym;
2518 Elf_Internal_Sym *isymend;
2519 bfd_boolean result;
2521 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2522 if (abfd == NULL)
2523 return FALSE;
2525 if (! bfd_check_format (abfd, bfd_object))
2526 return FALSE;
2528 /* If we have already included the element containing this symbol in the
2529 link then we do not need to include it again. Just claim that any symbol
2530 it contains is not a definition, so that our caller will not decide to
2531 (re)include this element. */
2532 if (abfd->archive_pass)
2533 return FALSE;
2535 /* Select the appropriate symbol table. */
2536 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2537 hdr = &elf_tdata (abfd)->symtab_hdr;
2538 else
2539 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2541 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2543 /* The sh_info field of the symtab header tells us where the
2544 external symbols start. We don't care about the local symbols. */
2545 if (elf_bad_symtab (abfd))
2547 extsymcount = symcount;
2548 extsymoff = 0;
2550 else
2552 extsymcount = symcount - hdr->sh_info;
2553 extsymoff = hdr->sh_info;
2556 if (extsymcount == 0)
2557 return FALSE;
2559 /* Read in the symbol table. */
2560 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2561 NULL, NULL, NULL);
2562 if (isymbuf == NULL)
2563 return FALSE;
2565 /* Scan the symbol table looking for SYMDEF. */
2566 result = FALSE;
2567 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2569 const char *name;
2571 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2572 isym->st_name);
2573 if (name == NULL)
2574 break;
2576 if (strcmp (name, symdef->name) == 0)
2578 result = is_global_data_symbol_definition (abfd, isym);
2579 break;
2583 free (isymbuf);
2585 return result;
2588 /* Add symbols from an ELF archive file to the linker hash table. We
2589 don't use _bfd_generic_link_add_archive_symbols because of a
2590 problem which arises on UnixWare. The UnixWare libc.so is an
2591 archive which includes an entry libc.so.1 which defines a bunch of
2592 symbols. The libc.so archive also includes a number of other
2593 object files, which also define symbols, some of which are the same
2594 as those defined in libc.so.1. Correct linking requires that we
2595 consider each object file in turn, and include it if it defines any
2596 symbols we need. _bfd_generic_link_add_archive_symbols does not do
2597 this; it looks through the list of undefined symbols, and includes
2598 any object file which defines them. When this algorithm is used on
2599 UnixWare, it winds up pulling in libc.so.1 early and defining a
2600 bunch of symbols. This means that some of the other objects in the
2601 archive are not included in the link, which is incorrect since they
2602 precede libc.so.1 in the archive.
2604 Fortunately, ELF archive handling is simpler than that done by
2605 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
2606 oddities. In ELF, if we find a symbol in the archive map, and the
2607 symbol is currently undefined, we know that we must pull in that
2608 object file.
2610 Unfortunately, we do have to make multiple passes over the symbol
2611 table until nothing further is resolved. */
2613 bfd_boolean
2614 _bfd_elf_link_add_archive_symbols (bfd *abfd,
2615 struct bfd_link_info *info)
2617 symindex c;
2618 bfd_boolean *defined = NULL;
2619 bfd_boolean *included = NULL;
2620 carsym *symdefs;
2621 bfd_boolean loop;
2622 bfd_size_type amt;
2624 if (! bfd_has_map (abfd))
2626 /* An empty archive is a special case. */
2627 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
2628 return TRUE;
2629 bfd_set_error (bfd_error_no_armap);
2630 return FALSE;
2633 /* Keep track of all symbols we know to be already defined, and all
2634 files we know to be already included. This is to speed up the
2635 second and subsequent passes. */
2636 c = bfd_ardata (abfd)->symdef_count;
2637 if (c == 0)
2638 return TRUE;
2639 amt = c;
2640 amt *= sizeof (bfd_boolean);
2641 defined = bfd_zmalloc (amt);
2642 included = bfd_zmalloc (amt);
2643 if (defined == NULL || included == NULL)
2644 goto error_return;
2646 symdefs = bfd_ardata (abfd)->symdefs;
2650 file_ptr last;
2651 symindex i;
2652 carsym *symdef;
2653 carsym *symdefend;
2655 loop = FALSE;
2656 last = -1;
2658 symdef = symdefs;
2659 symdefend = symdef + c;
2660 for (i = 0; symdef < symdefend; symdef++, i++)
2662 struct elf_link_hash_entry *h;
2663 bfd *element;
2664 struct bfd_link_hash_entry *undefs_tail;
2665 symindex mark;
2667 if (defined[i] || included[i])
2668 continue;
2669 if (symdef->file_offset == last)
2671 included[i] = TRUE;
2672 continue;
2675 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
2676 FALSE, FALSE, FALSE);
2678 if (h == NULL)
2680 char *p, *copy;
2681 size_t len, first;
2683 /* If this is a default version (the name contains @@),
2684 look up the symbol again with only one `@' as well
2685 as without the version. The effect is that references
2686 to the symbol with and without the version will be
2687 matched by the default symbol in the archive. */
2689 p = strchr (symdef->name, ELF_VER_CHR);
2690 if (p == NULL || p[1] != ELF_VER_CHR)
2691 continue;
2693 /* First check with only one `@'. */
2694 len = strlen (symdef->name);
2695 copy = bfd_alloc (abfd, len);
2696 if (copy == NULL)
2697 goto error_return;
2698 first = p - symdef->name + 1;
2699 memcpy (copy, symdef->name, first);
2700 memcpy (copy + first, symdef->name + first + 1, len - first);
2702 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2703 FALSE, FALSE, FALSE);
2705 if (h == NULL)
2707 /* We also need to check references to the symbol
2708 without the version. */
2710 copy[first - 1] = '\0';
2711 h = elf_link_hash_lookup (elf_hash_table (info),
2712 copy, FALSE, FALSE, FALSE);
2715 bfd_release (abfd, copy);
2718 if (h == NULL)
2719 continue;
2721 if (h->root.type == bfd_link_hash_common)
2723 /* We currently have a common symbol. The archive map contains
2724 a reference to this symbol, so we may want to include it. We
2725 only want to include it however, if this archive element
2726 contains a definition of the symbol, not just another common
2727 declaration of it.
2729 Unfortunately some archivers (including GNU ar) will put
2730 declarations of common symbols into their archive maps, as
2731 well as real definitions, so we cannot just go by the archive
2732 map alone. Instead we must read in the element's symbol
2733 table and check that to see what kind of symbol definition
2734 this is. */
2735 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
2736 continue;
2738 else if (h->root.type != bfd_link_hash_undefined)
2740 if (h->root.type != bfd_link_hash_undefweak)
2741 defined[i] = TRUE;
2742 continue;
2745 /* We need to include this archive member. */
2746 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2747 if (element == NULL)
2748 goto error_return;
2750 if (! bfd_check_format (element, bfd_object))
2751 goto error_return;
2753 /* Doublecheck that we have not included this object
2754 already--it should be impossible, but there may be
2755 something wrong with the archive. */
2756 if (element->archive_pass != 0)
2758 bfd_set_error (bfd_error_bad_value);
2759 goto error_return;
2761 element->archive_pass = 1;
2763 undefs_tail = info->hash->undefs_tail;
2765 if (! (*info->callbacks->add_archive_element) (info, element,
2766 symdef->name))
2767 goto error_return;
2768 if (! bfd_link_add_symbols (element, info))
2769 goto error_return;
2771 /* If there are any new undefined symbols, we need to make
2772 another pass through the archive in order to see whether
2773 they can be defined. FIXME: This isn't perfect, because
2774 common symbols wind up on undefs_tail and because an
2775 undefined symbol which is defined later on in this pass
2776 does not require another pass. This isn't a bug, but it
2777 does make the code less efficient than it could be. */
2778 if (undefs_tail != info->hash->undefs_tail)
2779 loop = TRUE;
2781 /* Look backward to mark all symbols from this object file
2782 which we have already seen in this pass. */
2783 mark = i;
2786 included[mark] = TRUE;
2787 if (mark == 0)
2788 break;
2789 --mark;
2791 while (symdefs[mark].file_offset == symdef->file_offset);
2793 /* We mark subsequent symbols from this object file as we go
2794 on through the loop. */
2795 last = symdef->file_offset;
2798 while (loop);
2800 free (defined);
2801 free (included);
2803 return TRUE;
2805 error_return:
2806 if (defined != NULL)
2807 free (defined);
2808 if (included != NULL)
2809 free (included);
2810 return FALSE;