* elf32-spu.c (spu_elf_relocate_section): Test identical conditions
[binutils.git] / bfd / elflink.c
blob513979926b412408ed0a376674c1051843d7ecc5
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
32 /* Define a symbol in a dynamic linkage section. */
34 struct elf_link_hash_entry *
35 _bfd_elf_define_linkage_sym (bfd *abfd,
36 struct bfd_link_info *info,
37 asection *sec,
38 const char *name)
40 struct elf_link_hash_entry *h;
41 struct bfd_link_hash_entry *bh;
42 const struct elf_backend_data *bed;
44 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
45 if (h != NULL)
47 /* Zap symbol defined in an as-needed lib that wasn't linked.
48 This is a symptom of a larger problem: Absolute symbols
49 defined in shared libraries can't be overridden, because we
50 lose the link to the bfd which is via the symbol section. */
51 h->root.type = bfd_link_hash_new;
54 bh = &h->root;
55 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
56 sec, 0, NULL, FALSE,
57 get_elf_backend_data (abfd)->collect,
58 &bh))
59 return NULL;
60 h = (struct elf_link_hash_entry *) bh;
61 h->def_regular = 1;
62 h->type = STT_OBJECT;
63 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
65 bed = get_elf_backend_data (abfd);
66 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
67 return h;
70 bfd_boolean
71 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
73 flagword flags;
74 asection *s;
75 struct elf_link_hash_entry *h;
76 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
77 int ptralign;
79 /* This function may be called more than once. */
80 s = bfd_get_section_by_name (abfd, ".got");
81 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
82 return TRUE;
84 switch (bed->s->arch_size)
86 case 32:
87 ptralign = 2;
88 break;
90 case 64:
91 ptralign = 3;
92 break;
94 default:
95 bfd_set_error (bfd_error_bad_value);
96 return FALSE;
99 flags = bed->dynamic_sec_flags;
101 s = bfd_make_section_with_flags (abfd, ".got", flags);
102 if (s == NULL
103 || !bfd_set_section_alignment (abfd, s, ptralign))
104 return FALSE;
106 if (bed->want_got_plt)
108 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
109 if (s == NULL
110 || !bfd_set_section_alignment (abfd, s, ptralign))
111 return FALSE;
114 if (bed->want_got_sym)
116 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
117 (or .got.plt) section. We don't do this in the linker script
118 because we don't want to define the symbol if we are not creating
119 a global offset table. */
120 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
121 elf_hash_table (info)->hgot = h;
122 if (h == NULL)
123 return FALSE;
126 /* The first bit of the global offset table is the header. */
127 s->size += bed->got_header_size;
129 return TRUE;
132 /* Create a strtab to hold the dynamic symbol names. */
133 static bfd_boolean
134 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
136 struct elf_link_hash_table *hash_table;
138 hash_table = elf_hash_table (info);
139 if (hash_table->dynobj == NULL)
140 hash_table->dynobj = abfd;
142 if (hash_table->dynstr == NULL)
144 hash_table->dynstr = _bfd_elf_strtab_init ();
145 if (hash_table->dynstr == NULL)
146 return FALSE;
148 return TRUE;
151 /* Create some sections which will be filled in with dynamic linking
152 information. ABFD is an input file which requires dynamic sections
153 to be created. The dynamic sections take up virtual memory space
154 when the final executable is run, so we need to create them before
155 addresses are assigned to the output sections. We work out the
156 actual contents and size of these sections later. */
158 bfd_boolean
159 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
161 flagword flags;
162 register asection *s;
163 const struct elf_backend_data *bed;
165 if (! is_elf_hash_table (info->hash))
166 return FALSE;
168 if (elf_hash_table (info)->dynamic_sections_created)
169 return TRUE;
171 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
172 return FALSE;
174 abfd = elf_hash_table (info)->dynobj;
175 bed = get_elf_backend_data (abfd);
177 flags = bed->dynamic_sec_flags;
179 /* A dynamically linked executable has a .interp section, but a
180 shared library does not. */
181 if (info->executable)
183 s = bfd_make_section_with_flags (abfd, ".interp",
184 flags | SEC_READONLY);
185 if (s == NULL)
186 return FALSE;
189 /* Create sections to hold version informations. These are removed
190 if they are not needed. */
191 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
192 flags | SEC_READONLY);
193 if (s == NULL
194 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
195 return FALSE;
197 s = bfd_make_section_with_flags (abfd, ".gnu.version",
198 flags | SEC_READONLY);
199 if (s == NULL
200 || ! bfd_set_section_alignment (abfd, s, 1))
201 return FALSE;
203 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
204 flags | SEC_READONLY);
205 if (s == NULL
206 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
207 return FALSE;
209 s = bfd_make_section_with_flags (abfd, ".dynsym",
210 flags | SEC_READONLY);
211 if (s == NULL
212 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
213 return FALSE;
215 s = bfd_make_section_with_flags (abfd, ".dynstr",
216 flags | SEC_READONLY);
217 if (s == NULL)
218 return FALSE;
220 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
221 if (s == NULL
222 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
223 return FALSE;
225 /* The special symbol _DYNAMIC is always set to the start of the
226 .dynamic section. We could set _DYNAMIC in a linker script, but we
227 only want to define it if we are, in fact, creating a .dynamic
228 section. We don't want to define it if there is no .dynamic
229 section, since on some ELF platforms the start up code examines it
230 to decide how to initialize the process. */
231 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
232 return FALSE;
234 if (info->emit_hash)
236 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
237 if (s == NULL
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
239 return FALSE;
240 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
243 if (info->emit_gnu_hash)
245 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
246 flags | SEC_READONLY);
247 if (s == NULL
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
249 return FALSE;
250 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
251 4 32-bit words followed by variable count of 64-bit words, then
252 variable count of 32-bit words. */
253 if (bed->s->arch_size == 64)
254 elf_section_data (s)->this_hdr.sh_entsize = 0;
255 else
256 elf_section_data (s)->this_hdr.sh_entsize = 4;
259 /* Let the backend create the rest of the sections. This lets the
260 backend set the right flags. The backend will normally create
261 the .got and .plt sections. */
262 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
263 return FALSE;
265 elf_hash_table (info)->dynamic_sections_created = TRUE;
267 return TRUE;
270 /* Create dynamic sections when linking against a dynamic object. */
272 bfd_boolean
273 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
275 flagword flags, pltflags;
276 struct elf_link_hash_entry *h;
277 asection *s;
278 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
280 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
281 .rel[a].bss sections. */
282 flags = bed->dynamic_sec_flags;
284 pltflags = flags;
285 if (bed->plt_not_loaded)
286 /* We do not clear SEC_ALLOC here because we still want the OS to
287 allocate space for the section; it's just that there's nothing
288 to read in from the object file. */
289 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
290 else
291 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
292 if (bed->plt_readonly)
293 pltflags |= SEC_READONLY;
295 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
296 if (s == NULL
297 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
298 return FALSE;
300 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
301 .plt section. */
302 if (bed->want_plt_sym)
304 h = _bfd_elf_define_linkage_sym (abfd, info, s,
305 "_PROCEDURE_LINKAGE_TABLE_");
306 elf_hash_table (info)->hplt = h;
307 if (h == NULL)
308 return FALSE;
311 s = bfd_make_section_with_flags (abfd,
312 (bed->default_use_rela_p
313 ? ".rela.plt" : ".rel.plt"),
314 flags | SEC_READONLY);
315 if (s == NULL
316 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
317 return FALSE;
319 if (! _bfd_elf_create_got_section (abfd, info))
320 return FALSE;
322 if (bed->want_dynbss)
324 /* The .dynbss section is a place to put symbols which are defined
325 by dynamic objects, are referenced by regular objects, and are
326 not functions. We must allocate space for them in the process
327 image and use a R_*_COPY reloc to tell the dynamic linker to
328 initialize them at run time. The linker script puts the .dynbss
329 section into the .bss section of the final image. */
330 s = bfd_make_section_with_flags (abfd, ".dynbss",
331 (SEC_ALLOC
332 | SEC_LINKER_CREATED));
333 if (s == NULL)
334 return FALSE;
336 /* The .rel[a].bss section holds copy relocs. This section is not
337 normally needed. We need to create it here, though, so that the
338 linker will map it to an output section. We can't just create it
339 only if we need it, because we will not know whether we need it
340 until we have seen all the input files, and the first time the
341 main linker code calls BFD after examining all the input files
342 (size_dynamic_sections) the input sections have already been
343 mapped to the output sections. If the section turns out not to
344 be needed, we can discard it later. We will never need this
345 section when generating a shared object, since they do not use
346 copy relocs. */
347 if (! info->shared)
349 s = bfd_make_section_with_flags (abfd,
350 (bed->default_use_rela_p
351 ? ".rela.bss" : ".rel.bss"),
352 flags | SEC_READONLY);
353 if (s == NULL
354 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
355 return FALSE;
359 return TRUE;
362 /* Record a new dynamic symbol. We record the dynamic symbols as we
363 read the input files, since we need to have a list of all of them
364 before we can determine the final sizes of the output sections.
365 Note that we may actually call this function even though we are not
366 going to output any dynamic symbols; in some cases we know that a
367 symbol should be in the dynamic symbol table, but only if there is
368 one. */
370 bfd_boolean
371 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
372 struct elf_link_hash_entry *h)
374 if (h->dynindx == -1)
376 struct elf_strtab_hash *dynstr;
377 char *p;
378 const char *name;
379 bfd_size_type indx;
381 /* XXX: The ABI draft says the linker must turn hidden and
382 internal symbols into STB_LOCAL symbols when producing the
383 DSO. However, if ld.so honors st_other in the dynamic table,
384 this would not be necessary. */
385 switch (ELF_ST_VISIBILITY (h->other))
387 case STV_INTERNAL:
388 case STV_HIDDEN:
389 if (h->root.type != bfd_link_hash_undefined
390 && h->root.type != bfd_link_hash_undefweak)
392 h->forced_local = 1;
393 if (!elf_hash_table (info)->is_relocatable_executable)
394 return TRUE;
397 default:
398 break;
401 h->dynindx = elf_hash_table (info)->dynsymcount;
402 ++elf_hash_table (info)->dynsymcount;
404 dynstr = elf_hash_table (info)->dynstr;
405 if (dynstr == NULL)
407 /* Create a strtab to hold the dynamic symbol names. */
408 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
409 if (dynstr == NULL)
410 return FALSE;
413 /* We don't put any version information in the dynamic string
414 table. */
415 name = h->root.root.string;
416 p = strchr (name, ELF_VER_CHR);
417 if (p != NULL)
418 /* We know that the p points into writable memory. In fact,
419 there are only a few symbols that have read-only names, being
420 those like _GLOBAL_OFFSET_TABLE_ that are created specially
421 by the backends. Most symbols will have names pointing into
422 an ELF string table read from a file, or to objalloc memory. */
423 *p = 0;
425 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
427 if (p != NULL)
428 *p = ELF_VER_CHR;
430 if (indx == (bfd_size_type) -1)
431 return FALSE;
432 h->dynstr_index = indx;
435 return TRUE;
438 /* Mark a symbol dynamic. */
440 void
441 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
442 struct elf_link_hash_entry *h,
443 Elf_Internal_Sym *sym)
445 struct bfd_elf_dynamic_list *d = info->dynamic_list;
447 /* It may be called more than once on the same H. */
448 if(h->dynamic || info->relocatable)
449 return;
451 if ((info->dynamic_data
452 && (h->type == STT_OBJECT
453 || (sym != NULL
454 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
455 || (d != NULL
456 && h->root.type == bfd_link_hash_new
457 && (*d->match) (&d->head, NULL, h->root.root.string)))
458 h->dynamic = 1;
461 /* Record an assignment to a symbol made by a linker script. We need
462 this in case some dynamic object refers to this symbol. */
464 bfd_boolean
465 bfd_elf_record_link_assignment (bfd *output_bfd,
466 struct bfd_link_info *info,
467 const char *name,
468 bfd_boolean provide,
469 bfd_boolean hidden)
471 struct elf_link_hash_entry *h, *hv;
472 struct elf_link_hash_table *htab;
473 const struct elf_backend_data *bed;
475 if (!is_elf_hash_table (info->hash))
476 return TRUE;
478 htab = elf_hash_table (info);
479 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
480 if (h == NULL)
481 return provide;
483 switch (h->root.type)
485 case bfd_link_hash_defined:
486 case bfd_link_hash_defweak:
487 case bfd_link_hash_common:
488 break;
489 case bfd_link_hash_undefweak:
490 case bfd_link_hash_undefined:
491 /* Since we're defining the symbol, don't let it seem to have not
492 been defined. record_dynamic_symbol and size_dynamic_sections
493 may depend on this. */
494 h->root.type = bfd_link_hash_new;
495 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
496 bfd_link_repair_undef_list (&htab->root);
497 break;
498 case bfd_link_hash_new:
499 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
500 h->non_elf = 0;
501 break;
502 case bfd_link_hash_indirect:
503 /* We had a versioned symbol in a dynamic library. We make the
504 the versioned symbol point to this one. */
505 bed = get_elf_backend_data (output_bfd);
506 hv = h;
507 while (hv->root.type == bfd_link_hash_indirect
508 || hv->root.type == bfd_link_hash_warning)
509 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
510 /* We don't need to update h->root.u since linker will set them
511 later. */
512 h->root.type = bfd_link_hash_undefined;
513 hv->root.type = bfd_link_hash_indirect;
514 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
515 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
516 break;
517 case bfd_link_hash_warning:
518 abort ();
519 break;
522 /* If this symbol is being provided by the linker script, and it is
523 currently defined by a dynamic object, but not by a regular
524 object, then mark it as undefined so that the generic linker will
525 force the correct value. */
526 if (provide
527 && h->def_dynamic
528 && !h->def_regular)
529 h->root.type = bfd_link_hash_undefined;
531 /* If this symbol is not being provided by the linker script, and it is
532 currently defined by a dynamic object, but not by a regular object,
533 then clear out any version information because the symbol will not be
534 associated with the dynamic object any more. */
535 if (!provide
536 && h->def_dynamic
537 && !h->def_regular)
538 h->verinfo.verdef = NULL;
540 h->def_regular = 1;
542 if (provide && hidden)
544 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
546 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
547 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
550 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
551 and executables. */
552 if (!info->relocatable
553 && h->dynindx != -1
554 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
555 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
556 h->forced_local = 1;
558 if ((h->def_dynamic
559 || h->ref_dynamic
560 || info->shared
561 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
562 && h->dynindx == -1)
564 if (! bfd_elf_link_record_dynamic_symbol (info, h))
565 return FALSE;
567 /* If this is a weak defined symbol, and we know a corresponding
568 real symbol from the same dynamic object, make sure the real
569 symbol is also made into a dynamic symbol. */
570 if (h->u.weakdef != NULL
571 && h->u.weakdef->dynindx == -1)
573 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
574 return FALSE;
578 return TRUE;
581 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
582 success, and 2 on a failure caused by attempting to record a symbol
583 in a discarded section, eg. a discarded link-once section symbol. */
586 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
587 bfd *input_bfd,
588 long input_indx)
590 bfd_size_type amt;
591 struct elf_link_local_dynamic_entry *entry;
592 struct elf_link_hash_table *eht;
593 struct elf_strtab_hash *dynstr;
594 unsigned long dynstr_index;
595 char *name;
596 Elf_External_Sym_Shndx eshndx;
597 char esym[sizeof (Elf64_External_Sym)];
599 if (! is_elf_hash_table (info->hash))
600 return 0;
602 /* See if the entry exists already. */
603 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
604 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
605 return 1;
607 amt = sizeof (*entry);
608 entry = bfd_alloc (input_bfd, amt);
609 if (entry == NULL)
610 return 0;
612 /* Go find the symbol, so that we can find it's name. */
613 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
614 1, input_indx, &entry->isym, esym, &eshndx))
616 bfd_release (input_bfd, entry);
617 return 0;
620 if (entry->isym.st_shndx != SHN_UNDEF
621 && (entry->isym.st_shndx < SHN_LORESERVE
622 || entry->isym.st_shndx > SHN_HIRESERVE))
624 asection *s;
626 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
627 if (s == NULL || bfd_is_abs_section (s->output_section))
629 /* We can still bfd_release here as nothing has done another
630 bfd_alloc. We can't do this later in this function. */
631 bfd_release (input_bfd, entry);
632 return 2;
636 name = (bfd_elf_string_from_elf_section
637 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
638 entry->isym.st_name));
640 dynstr = elf_hash_table (info)->dynstr;
641 if (dynstr == NULL)
643 /* Create a strtab to hold the dynamic symbol names. */
644 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
645 if (dynstr == NULL)
646 return 0;
649 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
650 if (dynstr_index == (unsigned long) -1)
651 return 0;
652 entry->isym.st_name = dynstr_index;
654 eht = elf_hash_table (info);
656 entry->next = eht->dynlocal;
657 eht->dynlocal = entry;
658 entry->input_bfd = input_bfd;
659 entry->input_indx = input_indx;
660 eht->dynsymcount++;
662 /* Whatever binding the symbol had before, it's now local. */
663 entry->isym.st_info
664 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
666 /* The dynindx will be set at the end of size_dynamic_sections. */
668 return 1;
671 /* Return the dynindex of a local dynamic symbol. */
673 long
674 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
675 bfd *input_bfd,
676 long input_indx)
678 struct elf_link_local_dynamic_entry *e;
680 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
681 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
682 return e->dynindx;
683 return -1;
686 /* This function is used to renumber the dynamic symbols, if some of
687 them are removed because they are marked as local. This is called
688 via elf_link_hash_traverse. */
690 static bfd_boolean
691 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
692 void *data)
694 size_t *count = data;
696 if (h->root.type == bfd_link_hash_warning)
697 h = (struct elf_link_hash_entry *) h->root.u.i.link;
699 if (h->forced_local)
700 return TRUE;
702 if (h->dynindx != -1)
703 h->dynindx = ++(*count);
705 return TRUE;
709 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
710 STB_LOCAL binding. */
712 static bfd_boolean
713 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
714 void *data)
716 size_t *count = data;
718 if (h->root.type == bfd_link_hash_warning)
719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
721 if (!h->forced_local)
722 return TRUE;
724 if (h->dynindx != -1)
725 h->dynindx = ++(*count);
727 return TRUE;
730 /* Return true if the dynamic symbol for a given section should be
731 omitted when creating a shared library. */
732 bfd_boolean
733 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
734 struct bfd_link_info *info,
735 asection *p)
737 struct elf_link_hash_table *htab;
739 switch (elf_section_data (p)->this_hdr.sh_type)
741 case SHT_PROGBITS:
742 case SHT_NOBITS:
743 /* If sh_type is yet undecided, assume it could be
744 SHT_PROGBITS/SHT_NOBITS. */
745 case SHT_NULL:
746 htab = elf_hash_table (info);
747 if (p == htab->tls_sec)
748 return FALSE;
750 if (htab->text_index_section != NULL)
751 return p != htab->text_index_section && p != htab->data_index_section;
753 if (strcmp (p->name, ".got") == 0
754 || strcmp (p->name, ".got.plt") == 0
755 || strcmp (p->name, ".plt") == 0)
757 asection *ip;
759 if (htab->dynobj != NULL
760 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
761 && (ip->flags & SEC_LINKER_CREATED)
762 && ip->output_section == p)
763 return TRUE;
765 return FALSE;
767 /* There shouldn't be section relative relocations
768 against any other section. */
769 default:
770 return TRUE;
774 /* Assign dynsym indices. In a shared library we generate a section
775 symbol for each output section, which come first. Next come symbols
776 which have been forced to local binding. Then all of the back-end
777 allocated local dynamic syms, followed by the rest of the global
778 symbols. */
780 static unsigned long
781 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
782 struct bfd_link_info *info,
783 unsigned long *section_sym_count)
785 unsigned long dynsymcount = 0;
787 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
789 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
790 asection *p;
791 for (p = output_bfd->sections; p ; p = p->next)
792 if ((p->flags & SEC_EXCLUDE) == 0
793 && (p->flags & SEC_ALLOC) != 0
794 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
795 elf_section_data (p)->dynindx = ++dynsymcount;
796 else
797 elf_section_data (p)->dynindx = 0;
799 *section_sym_count = dynsymcount;
801 elf_link_hash_traverse (elf_hash_table (info),
802 elf_link_renumber_local_hash_table_dynsyms,
803 &dynsymcount);
805 if (elf_hash_table (info)->dynlocal)
807 struct elf_link_local_dynamic_entry *p;
808 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
809 p->dynindx = ++dynsymcount;
812 elf_link_hash_traverse (elf_hash_table (info),
813 elf_link_renumber_hash_table_dynsyms,
814 &dynsymcount);
816 /* There is an unused NULL entry at the head of the table which
817 we must account for in our count. Unless there weren't any
818 symbols, which means we'll have no table at all. */
819 if (dynsymcount != 0)
820 ++dynsymcount;
822 elf_hash_table (info)->dynsymcount = dynsymcount;
823 return dynsymcount;
826 /* This function is called when we want to define a new symbol. It
827 handles the various cases which arise when we find a definition in
828 a dynamic object, or when there is already a definition in a
829 dynamic object. The new symbol is described by NAME, SYM, PSEC,
830 and PVALUE. We set SYM_HASH to the hash table entry. We set
831 OVERRIDE if the old symbol is overriding a new definition. We set
832 TYPE_CHANGE_OK if it is OK for the type to change. We set
833 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
834 change, we mean that we shouldn't warn if the type or size does
835 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
836 object is overridden by a regular object. */
838 bfd_boolean
839 _bfd_elf_merge_symbol (bfd *abfd,
840 struct bfd_link_info *info,
841 const char *name,
842 Elf_Internal_Sym *sym,
843 asection **psec,
844 bfd_vma *pvalue,
845 unsigned int *pold_alignment,
846 struct elf_link_hash_entry **sym_hash,
847 bfd_boolean *skip,
848 bfd_boolean *override,
849 bfd_boolean *type_change_ok,
850 bfd_boolean *size_change_ok)
852 asection *sec, *oldsec;
853 struct elf_link_hash_entry *h;
854 struct elf_link_hash_entry *flip;
855 int bind;
856 bfd *oldbfd;
857 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
858 bfd_boolean newweak, oldweak;
859 const struct elf_backend_data *bed;
861 *skip = FALSE;
862 *override = FALSE;
864 sec = *psec;
865 bind = ELF_ST_BIND (sym->st_info);
867 /* Silently discard TLS symbols from --just-syms. There's no way to
868 combine a static TLS block with a new TLS block for this executable. */
869 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
870 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
872 *skip = TRUE;
873 return TRUE;
876 if (! bfd_is_und_section (sec))
877 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
878 else
879 h = ((struct elf_link_hash_entry *)
880 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
881 if (h == NULL)
882 return FALSE;
883 *sym_hash = h;
885 /* This code is for coping with dynamic objects, and is only useful
886 if we are doing an ELF link. */
887 if (info->output_bfd->xvec != abfd->xvec)
888 return TRUE;
890 /* For merging, we only care about real symbols. */
892 while (h->root.type == bfd_link_hash_indirect
893 || h->root.type == bfd_link_hash_warning)
894 h = (struct elf_link_hash_entry *) h->root.u.i.link;
896 /* We have to check it for every instance since the first few may be
897 refereences and not all compilers emit symbol type for undefined
898 symbols. */
899 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
901 /* If we just created the symbol, mark it as being an ELF symbol.
902 Other than that, there is nothing to do--there is no merge issue
903 with a newly defined symbol--so we just return. */
905 if (h->root.type == bfd_link_hash_new)
907 h->non_elf = 0;
908 return TRUE;
911 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
912 existing symbol. */
914 switch (h->root.type)
916 default:
917 oldbfd = NULL;
918 oldsec = NULL;
919 break;
921 case bfd_link_hash_undefined:
922 case bfd_link_hash_undefweak:
923 oldbfd = h->root.u.undef.abfd;
924 oldsec = NULL;
925 break;
927 case bfd_link_hash_defined:
928 case bfd_link_hash_defweak:
929 oldbfd = h->root.u.def.section->owner;
930 oldsec = h->root.u.def.section;
931 break;
933 case bfd_link_hash_common:
934 oldbfd = h->root.u.c.p->section->owner;
935 oldsec = h->root.u.c.p->section;
936 break;
939 /* In cases involving weak versioned symbols, we may wind up trying
940 to merge a symbol with itself. Catch that here, to avoid the
941 confusion that results if we try to override a symbol with
942 itself. The additional tests catch cases like
943 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
944 dynamic object, which we do want to handle here. */
945 if (abfd == oldbfd
946 && ((abfd->flags & DYNAMIC) == 0
947 || !h->def_regular))
948 return TRUE;
950 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
951 respectively, is from a dynamic object. */
953 newdyn = (abfd->flags & DYNAMIC) != 0;
955 olddyn = FALSE;
956 if (oldbfd != NULL)
957 olddyn = (oldbfd->flags & DYNAMIC) != 0;
958 else if (oldsec != NULL)
960 /* This handles the special SHN_MIPS_{TEXT,DATA} section
961 indices used by MIPS ELF. */
962 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
965 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
966 respectively, appear to be a definition rather than reference. */
968 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
970 olddef = (h->root.type != bfd_link_hash_undefined
971 && h->root.type != bfd_link_hash_undefweak
972 && h->root.type != bfd_link_hash_common);
974 bed = get_elf_backend_data (abfd);
975 /* When we try to create a default indirect symbol from the dynamic
976 definition with the default version, we skip it if its type and
977 the type of existing regular definition mismatch. We only do it
978 if the existing regular definition won't be dynamic. */
979 if (pold_alignment == NULL
980 && !info->shared
981 && !info->export_dynamic
982 && !h->ref_dynamic
983 && newdyn
984 && newdef
985 && !olddyn
986 && (olddef || h->root.type == bfd_link_hash_common)
987 && ELF_ST_TYPE (sym->st_info) != h->type
988 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
989 && h->type != STT_NOTYPE
990 && !(bed->is_function_type (ELF_ST_TYPE (sym->st_info))
991 && bed->is_function_type (h->type)))
993 *skip = TRUE;
994 return TRUE;
997 /* Check TLS symbol. We don't check undefined symbol introduced by
998 "ld -u". */
999 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
1000 && ELF_ST_TYPE (sym->st_info) != h->type
1001 && oldbfd != NULL)
1003 bfd *ntbfd, *tbfd;
1004 bfd_boolean ntdef, tdef;
1005 asection *ntsec, *tsec;
1007 if (h->type == STT_TLS)
1009 ntbfd = abfd;
1010 ntsec = sec;
1011 ntdef = newdef;
1012 tbfd = oldbfd;
1013 tsec = oldsec;
1014 tdef = olddef;
1016 else
1018 ntbfd = oldbfd;
1019 ntsec = oldsec;
1020 ntdef = olddef;
1021 tbfd = abfd;
1022 tsec = sec;
1023 tdef = newdef;
1026 if (tdef && ntdef)
1027 (*_bfd_error_handler)
1028 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1029 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1030 else if (!tdef && !ntdef)
1031 (*_bfd_error_handler)
1032 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1033 tbfd, ntbfd, h->root.root.string);
1034 else if (tdef)
1035 (*_bfd_error_handler)
1036 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1037 tbfd, tsec, ntbfd, h->root.root.string);
1038 else
1039 (*_bfd_error_handler)
1040 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1041 tbfd, ntbfd, ntsec, h->root.root.string);
1043 bfd_set_error (bfd_error_bad_value);
1044 return FALSE;
1047 /* We need to remember if a symbol has a definition in a dynamic
1048 object or is weak in all dynamic objects. Internal and hidden
1049 visibility will make it unavailable to dynamic objects. */
1050 if (newdyn && !h->dynamic_def)
1052 if (!bfd_is_und_section (sec))
1053 h->dynamic_def = 1;
1054 else
1056 /* Check if this symbol is weak in all dynamic objects. If it
1057 is the first time we see it in a dynamic object, we mark
1058 if it is weak. Otherwise, we clear it. */
1059 if (!h->ref_dynamic)
1061 if (bind == STB_WEAK)
1062 h->dynamic_weak = 1;
1064 else if (bind != STB_WEAK)
1065 h->dynamic_weak = 0;
1069 /* If the old symbol has non-default visibility, we ignore the new
1070 definition from a dynamic object. */
1071 if (newdyn
1072 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1073 && !bfd_is_und_section (sec))
1075 *skip = TRUE;
1076 /* Make sure this symbol is dynamic. */
1077 h->ref_dynamic = 1;
1078 /* A protected symbol has external availability. Make sure it is
1079 recorded as dynamic.
1081 FIXME: Should we check type and size for protected symbol? */
1082 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1083 return bfd_elf_link_record_dynamic_symbol (info, h);
1084 else
1085 return TRUE;
1087 else if (!newdyn
1088 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1089 && h->def_dynamic)
1091 /* If the new symbol with non-default visibility comes from a
1092 relocatable file and the old definition comes from a dynamic
1093 object, we remove the old definition. */
1094 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1096 /* Handle the case where the old dynamic definition is
1097 default versioned. We need to copy the symbol info from
1098 the symbol with default version to the normal one if it
1099 was referenced before. */
1100 if (h->ref_regular)
1102 const struct elf_backend_data *bed
1103 = get_elf_backend_data (abfd);
1104 struct elf_link_hash_entry *vh = *sym_hash;
1105 vh->root.type = h->root.type;
1106 h->root.type = bfd_link_hash_indirect;
1107 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1108 /* Protected symbols will override the dynamic definition
1109 with default version. */
1110 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1112 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1113 vh->dynamic_def = 1;
1114 vh->ref_dynamic = 1;
1116 else
1118 h->root.type = vh->root.type;
1119 vh->ref_dynamic = 0;
1120 /* We have to hide it here since it was made dynamic
1121 global with extra bits when the symbol info was
1122 copied from the old dynamic definition. */
1123 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1125 h = vh;
1127 else
1128 h = *sym_hash;
1131 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1132 && bfd_is_und_section (sec))
1134 /* If the new symbol is undefined and the old symbol was
1135 also undefined before, we need to make sure
1136 _bfd_generic_link_add_one_symbol doesn't mess
1137 up the linker hash table undefs list. Since the old
1138 definition came from a dynamic object, it is still on the
1139 undefs list. */
1140 h->root.type = bfd_link_hash_undefined;
1141 h->root.u.undef.abfd = abfd;
1143 else
1145 h->root.type = bfd_link_hash_new;
1146 h->root.u.undef.abfd = NULL;
1149 if (h->def_dynamic)
1151 h->def_dynamic = 0;
1152 h->ref_dynamic = 1;
1153 h->dynamic_def = 1;
1155 /* FIXME: Should we check type and size for protected symbol? */
1156 h->size = 0;
1157 h->type = 0;
1158 return TRUE;
1161 /* Differentiate strong and weak symbols. */
1162 newweak = bind == STB_WEAK;
1163 oldweak = (h->root.type == bfd_link_hash_defweak
1164 || h->root.type == bfd_link_hash_undefweak);
1166 /* If a new weak symbol definition comes from a regular file and the
1167 old symbol comes from a dynamic library, we treat the new one as
1168 strong. Similarly, an old weak symbol definition from a regular
1169 file is treated as strong when the new symbol comes from a dynamic
1170 library. Further, an old weak symbol from a dynamic library is
1171 treated as strong if the new symbol is from a dynamic library.
1172 This reflects the way glibc's ld.so works.
1174 Do this before setting *type_change_ok or *size_change_ok so that
1175 we warn properly when dynamic library symbols are overridden. */
1177 if (newdef && !newdyn && olddyn)
1178 newweak = FALSE;
1179 if (olddef && newdyn)
1180 oldweak = FALSE;
1182 /* Allow changes between different types of funciton symbol. */
1183 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))
1184 && bed->is_function_type (h->type))
1185 *type_change_ok = TRUE;
1187 /* It's OK to change the type if either the existing symbol or the
1188 new symbol is weak. A type change is also OK if the old symbol
1189 is undefined and the new symbol is defined. */
1191 if (oldweak
1192 || newweak
1193 || (newdef
1194 && h->root.type == bfd_link_hash_undefined))
1195 *type_change_ok = TRUE;
1197 /* It's OK to change the size if either the existing symbol or the
1198 new symbol is weak, or if the old symbol is undefined. */
1200 if (*type_change_ok
1201 || h->root.type == bfd_link_hash_undefined)
1202 *size_change_ok = TRUE;
1204 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1205 symbol, respectively, appears to be a common symbol in a dynamic
1206 object. If a symbol appears in an uninitialized section, and is
1207 not weak, and is not a function, then it may be a common symbol
1208 which was resolved when the dynamic object was created. We want
1209 to treat such symbols specially, because they raise special
1210 considerations when setting the symbol size: if the symbol
1211 appears as a common symbol in a regular object, and the size in
1212 the regular object is larger, we must make sure that we use the
1213 larger size. This problematic case can always be avoided in C,
1214 but it must be handled correctly when using Fortran shared
1215 libraries.
1217 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1218 likewise for OLDDYNCOMMON and OLDDEF.
1220 Note that this test is just a heuristic, and that it is quite
1221 possible to have an uninitialized symbol in a shared object which
1222 is really a definition, rather than a common symbol. This could
1223 lead to some minor confusion when the symbol really is a common
1224 symbol in some regular object. However, I think it will be
1225 harmless. */
1227 if (newdyn
1228 && newdef
1229 && !newweak
1230 && (sec->flags & SEC_ALLOC) != 0
1231 && (sec->flags & SEC_LOAD) == 0
1232 && sym->st_size > 0
1233 && !bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
1234 newdyncommon = TRUE;
1235 else
1236 newdyncommon = FALSE;
1238 if (olddyn
1239 && olddef
1240 && h->root.type == bfd_link_hash_defined
1241 && h->def_dynamic
1242 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1243 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1244 && h->size > 0
1245 && !bed->is_function_type (h->type))
1246 olddyncommon = TRUE;
1247 else
1248 olddyncommon = FALSE;
1250 /* We now know everything about the old and new symbols. We ask the
1251 backend to check if we can merge them. */
1252 if (bed->merge_symbol
1253 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1254 pold_alignment, skip, override,
1255 type_change_ok, size_change_ok,
1256 &newdyn, &newdef, &newdyncommon, &newweak,
1257 abfd, &sec,
1258 &olddyn, &olddef, &olddyncommon, &oldweak,
1259 oldbfd, &oldsec))
1260 return FALSE;
1262 /* If both the old and the new symbols look like common symbols in a
1263 dynamic object, set the size of the symbol to the larger of the
1264 two. */
1266 if (olddyncommon
1267 && newdyncommon
1268 && sym->st_size != h->size)
1270 /* Since we think we have two common symbols, issue a multiple
1271 common warning if desired. Note that we only warn if the
1272 size is different. If the size is the same, we simply let
1273 the old symbol override the new one as normally happens with
1274 symbols defined in dynamic objects. */
1276 if (! ((*info->callbacks->multiple_common)
1277 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1278 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1279 return FALSE;
1281 if (sym->st_size > h->size)
1282 h->size = sym->st_size;
1284 *size_change_ok = TRUE;
1287 /* If we are looking at a dynamic object, and we have found a
1288 definition, we need to see if the symbol was already defined by
1289 some other object. If so, we want to use the existing
1290 definition, and we do not want to report a multiple symbol
1291 definition error; we do this by clobbering *PSEC to be
1292 bfd_und_section_ptr.
1294 We treat a common symbol as a definition if the symbol in the
1295 shared library is a function, since common symbols always
1296 represent variables; this can cause confusion in principle, but
1297 any such confusion would seem to indicate an erroneous program or
1298 shared library. We also permit a common symbol in a regular
1299 object to override a weak symbol in a shared object. */
1301 if (newdyn
1302 && newdef
1303 && (olddef
1304 || (h->root.type == bfd_link_hash_common
1305 && (newweak
1306 || bed->is_function_type (ELF_ST_TYPE (sym->st_info))))))
1308 *override = TRUE;
1309 newdef = FALSE;
1310 newdyncommon = FALSE;
1312 *psec = sec = bfd_und_section_ptr;
1313 *size_change_ok = TRUE;
1315 /* If we get here when the old symbol is a common symbol, then
1316 we are explicitly letting it override a weak symbol or
1317 function in a dynamic object, and we don't want to warn about
1318 a type change. If the old symbol is a defined symbol, a type
1319 change warning may still be appropriate. */
1321 if (h->root.type == bfd_link_hash_common)
1322 *type_change_ok = TRUE;
1325 /* Handle the special case of an old common symbol merging with a
1326 new symbol which looks like a common symbol in a shared object.
1327 We change *PSEC and *PVALUE to make the new symbol look like a
1328 common symbol, and let _bfd_generic_link_add_one_symbol do the
1329 right thing. */
1331 if (newdyncommon
1332 && h->root.type == bfd_link_hash_common)
1334 *override = TRUE;
1335 newdef = FALSE;
1336 newdyncommon = FALSE;
1337 *pvalue = sym->st_size;
1338 *psec = sec = bed->common_section (oldsec);
1339 *size_change_ok = TRUE;
1342 /* Skip weak definitions of symbols that are already defined. */
1343 if (newdef && olddef && newweak)
1344 *skip = TRUE;
1346 /* If the old symbol is from a dynamic object, and the new symbol is
1347 a definition which is not from a dynamic object, then the new
1348 symbol overrides the old symbol. Symbols from regular files
1349 always take precedence over symbols from dynamic objects, even if
1350 they are defined after the dynamic object in the link.
1352 As above, we again permit a common symbol in a regular object to
1353 override a definition in a shared object if the shared object
1354 symbol is a function or is weak. */
1356 flip = NULL;
1357 if (!newdyn
1358 && (newdef
1359 || (bfd_is_com_section (sec)
1360 && (oldweak
1361 || bed->is_function_type (h->type))))
1362 && olddyn
1363 && olddef
1364 && h->def_dynamic)
1366 /* Change the hash table entry to undefined, and let
1367 _bfd_generic_link_add_one_symbol do the right thing with the
1368 new definition. */
1370 h->root.type = bfd_link_hash_undefined;
1371 h->root.u.undef.abfd = h->root.u.def.section->owner;
1372 *size_change_ok = TRUE;
1374 olddef = FALSE;
1375 olddyncommon = FALSE;
1377 /* We again permit a type change when a common symbol may be
1378 overriding a function. */
1380 if (bfd_is_com_section (sec))
1381 *type_change_ok = TRUE;
1383 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1384 flip = *sym_hash;
1385 else
1386 /* This union may have been set to be non-NULL when this symbol
1387 was seen in a dynamic object. We must force the union to be
1388 NULL, so that it is correct for a regular symbol. */
1389 h->verinfo.vertree = NULL;
1392 /* Handle the special case of a new common symbol merging with an
1393 old symbol that looks like it might be a common symbol defined in
1394 a shared object. Note that we have already handled the case in
1395 which a new common symbol should simply override the definition
1396 in the shared library. */
1398 if (! newdyn
1399 && bfd_is_com_section (sec)
1400 && olddyncommon)
1402 /* It would be best if we could set the hash table entry to a
1403 common symbol, but we don't know what to use for the section
1404 or the alignment. */
1405 if (! ((*info->callbacks->multiple_common)
1406 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1407 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1408 return FALSE;
1410 /* If the presumed common symbol in the dynamic object is
1411 larger, pretend that the new symbol has its size. */
1413 if (h->size > *pvalue)
1414 *pvalue = h->size;
1416 /* We need to remember the alignment required by the symbol
1417 in the dynamic object. */
1418 BFD_ASSERT (pold_alignment);
1419 *pold_alignment = h->root.u.def.section->alignment_power;
1421 olddef = FALSE;
1422 olddyncommon = FALSE;
1424 h->root.type = bfd_link_hash_undefined;
1425 h->root.u.undef.abfd = h->root.u.def.section->owner;
1427 *size_change_ok = TRUE;
1428 *type_change_ok = TRUE;
1430 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1431 flip = *sym_hash;
1432 else
1433 h->verinfo.vertree = NULL;
1436 if (flip != NULL)
1438 /* Handle the case where we had a versioned symbol in a dynamic
1439 library and now find a definition in a normal object. In this
1440 case, we make the versioned symbol point to the normal one. */
1441 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1442 flip->root.type = h->root.type;
1443 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1444 h->root.type = bfd_link_hash_indirect;
1445 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1446 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1447 if (h->def_dynamic)
1449 h->def_dynamic = 0;
1450 flip->ref_dynamic = 1;
1454 return TRUE;
1457 /* This function is called to create an indirect symbol from the
1458 default for the symbol with the default version if needed. The
1459 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1460 set DYNSYM if the new indirect symbol is dynamic. */
1462 bfd_boolean
1463 _bfd_elf_add_default_symbol (bfd *abfd,
1464 struct bfd_link_info *info,
1465 struct elf_link_hash_entry *h,
1466 const char *name,
1467 Elf_Internal_Sym *sym,
1468 asection **psec,
1469 bfd_vma *value,
1470 bfd_boolean *dynsym,
1471 bfd_boolean override)
1473 bfd_boolean type_change_ok;
1474 bfd_boolean size_change_ok;
1475 bfd_boolean skip;
1476 char *shortname;
1477 struct elf_link_hash_entry *hi;
1478 struct bfd_link_hash_entry *bh;
1479 const struct elf_backend_data *bed;
1480 bfd_boolean collect;
1481 bfd_boolean dynamic;
1482 char *p;
1483 size_t len, shortlen;
1484 asection *sec;
1486 /* If this symbol has a version, and it is the default version, we
1487 create an indirect symbol from the default name to the fully
1488 decorated name. This will cause external references which do not
1489 specify a version to be bound to this version of the symbol. */
1490 p = strchr (name, ELF_VER_CHR);
1491 if (p == NULL || p[1] != ELF_VER_CHR)
1492 return TRUE;
1494 if (override)
1496 /* We are overridden by an old definition. We need to check if we
1497 need to create the indirect symbol from the default name. */
1498 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1499 FALSE, FALSE);
1500 BFD_ASSERT (hi != NULL);
1501 if (hi == h)
1502 return TRUE;
1503 while (hi->root.type == bfd_link_hash_indirect
1504 || hi->root.type == bfd_link_hash_warning)
1506 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1507 if (hi == h)
1508 return TRUE;
1512 bed = get_elf_backend_data (abfd);
1513 collect = bed->collect;
1514 dynamic = (abfd->flags & DYNAMIC) != 0;
1516 shortlen = p - name;
1517 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1518 if (shortname == NULL)
1519 return FALSE;
1520 memcpy (shortname, name, shortlen);
1521 shortname[shortlen] = '\0';
1523 /* We are going to create a new symbol. Merge it with any existing
1524 symbol with this name. For the purposes of the merge, act as
1525 though we were defining the symbol we just defined, although we
1526 actually going to define an indirect symbol. */
1527 type_change_ok = FALSE;
1528 size_change_ok = FALSE;
1529 sec = *psec;
1530 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1531 NULL, &hi, &skip, &override,
1532 &type_change_ok, &size_change_ok))
1533 return FALSE;
1535 if (skip)
1536 goto nondefault;
1538 if (! override)
1540 bh = &hi->root;
1541 if (! (_bfd_generic_link_add_one_symbol
1542 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1543 0, name, FALSE, collect, &bh)))
1544 return FALSE;
1545 hi = (struct elf_link_hash_entry *) bh;
1547 else
1549 /* In this case the symbol named SHORTNAME is overriding the
1550 indirect symbol we want to add. We were planning on making
1551 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1552 is the name without a version. NAME is the fully versioned
1553 name, and it is the default version.
1555 Overriding means that we already saw a definition for the
1556 symbol SHORTNAME in a regular object, and it is overriding
1557 the symbol defined in the dynamic object.
1559 When this happens, we actually want to change NAME, the
1560 symbol we just added, to refer to SHORTNAME. This will cause
1561 references to NAME in the shared object to become references
1562 to SHORTNAME in the regular object. This is what we expect
1563 when we override a function in a shared object: that the
1564 references in the shared object will be mapped to the
1565 definition in the regular object. */
1567 while (hi->root.type == bfd_link_hash_indirect
1568 || hi->root.type == bfd_link_hash_warning)
1569 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1571 h->root.type = bfd_link_hash_indirect;
1572 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1573 if (h->def_dynamic)
1575 h->def_dynamic = 0;
1576 hi->ref_dynamic = 1;
1577 if (hi->ref_regular
1578 || hi->def_regular)
1580 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1581 return FALSE;
1585 /* Now set HI to H, so that the following code will set the
1586 other fields correctly. */
1587 hi = h;
1590 /* Check if HI is a warning symbol. */
1591 if (hi->root.type == bfd_link_hash_warning)
1592 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1594 /* If there is a duplicate definition somewhere, then HI may not
1595 point to an indirect symbol. We will have reported an error to
1596 the user in that case. */
1598 if (hi->root.type == bfd_link_hash_indirect)
1600 struct elf_link_hash_entry *ht;
1602 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1603 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1605 /* See if the new flags lead us to realize that the symbol must
1606 be dynamic. */
1607 if (! *dynsym)
1609 if (! dynamic)
1611 if (info->shared
1612 || hi->ref_dynamic)
1613 *dynsym = TRUE;
1615 else
1617 if (hi->ref_regular)
1618 *dynsym = TRUE;
1623 /* We also need to define an indirection from the nondefault version
1624 of the symbol. */
1626 nondefault:
1627 len = strlen (name);
1628 shortname = bfd_hash_allocate (&info->hash->table, len);
1629 if (shortname == NULL)
1630 return FALSE;
1631 memcpy (shortname, name, shortlen);
1632 memcpy (shortname + shortlen, p + 1, len - shortlen);
1634 /* Once again, merge with any existing symbol. */
1635 type_change_ok = FALSE;
1636 size_change_ok = FALSE;
1637 sec = *psec;
1638 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1639 NULL, &hi, &skip, &override,
1640 &type_change_ok, &size_change_ok))
1641 return FALSE;
1643 if (skip)
1644 return TRUE;
1646 if (override)
1648 /* Here SHORTNAME is a versioned name, so we don't expect to see
1649 the type of override we do in the case above unless it is
1650 overridden by a versioned definition. */
1651 if (hi->root.type != bfd_link_hash_defined
1652 && hi->root.type != bfd_link_hash_defweak)
1653 (*_bfd_error_handler)
1654 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1655 abfd, shortname);
1657 else
1659 bh = &hi->root;
1660 if (! (_bfd_generic_link_add_one_symbol
1661 (info, abfd, shortname, BSF_INDIRECT,
1662 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1663 return FALSE;
1664 hi = (struct elf_link_hash_entry *) bh;
1666 /* If there is a duplicate definition somewhere, then HI may not
1667 point to an indirect symbol. We will have reported an error
1668 to the user in that case. */
1670 if (hi->root.type == bfd_link_hash_indirect)
1672 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1674 /* See if the new flags lead us to realize that the symbol
1675 must be dynamic. */
1676 if (! *dynsym)
1678 if (! dynamic)
1680 if (info->shared
1681 || hi->ref_dynamic)
1682 *dynsym = TRUE;
1684 else
1686 if (hi->ref_regular)
1687 *dynsym = TRUE;
1693 return TRUE;
1696 /* This routine is used to export all defined symbols into the dynamic
1697 symbol table. It is called via elf_link_hash_traverse. */
1699 bfd_boolean
1700 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1702 struct elf_info_failed *eif = data;
1704 /* Ignore this if we won't export it. */
1705 if (!eif->info->export_dynamic && !h->dynamic)
1706 return TRUE;
1708 /* Ignore indirect symbols. These are added by the versioning code. */
1709 if (h->root.type == bfd_link_hash_indirect)
1710 return TRUE;
1712 if (h->root.type == bfd_link_hash_warning)
1713 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1715 if (h->dynindx == -1
1716 && (h->def_regular
1717 || h->ref_regular))
1719 struct bfd_elf_version_tree *t;
1720 struct bfd_elf_version_expr *d;
1722 for (t = eif->verdefs; t != NULL; t = t->next)
1724 if (t->globals.list != NULL)
1726 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1727 if (d != NULL)
1728 goto doit;
1731 if (t->locals.list != NULL)
1733 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1734 if (d != NULL)
1735 return TRUE;
1739 if (!eif->verdefs)
1741 doit:
1742 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1744 eif->failed = TRUE;
1745 return FALSE;
1750 return TRUE;
1753 /* Look through the symbols which are defined in other shared
1754 libraries and referenced here. Update the list of version
1755 dependencies. This will be put into the .gnu.version_r section.
1756 This function is called via elf_link_hash_traverse. */
1758 bfd_boolean
1759 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1760 void *data)
1762 struct elf_find_verdep_info *rinfo = data;
1763 Elf_Internal_Verneed *t;
1764 Elf_Internal_Vernaux *a;
1765 bfd_size_type amt;
1767 if (h->root.type == bfd_link_hash_warning)
1768 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1770 /* We only care about symbols defined in shared objects with version
1771 information. */
1772 if (!h->def_dynamic
1773 || h->def_regular
1774 || h->dynindx == -1
1775 || h->verinfo.verdef == NULL)
1776 return TRUE;
1778 /* See if we already know about this version. */
1779 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1781 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1782 continue;
1784 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1785 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1786 return TRUE;
1788 break;
1791 /* This is a new version. Add it to tree we are building. */
1793 if (t == NULL)
1795 amt = sizeof *t;
1796 t = bfd_zalloc (rinfo->output_bfd, amt);
1797 if (t == NULL)
1799 rinfo->failed = TRUE;
1800 return FALSE;
1803 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1804 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1805 elf_tdata (rinfo->output_bfd)->verref = t;
1808 amt = sizeof *a;
1809 a = bfd_zalloc (rinfo->output_bfd, amt);
1810 if (a == NULL)
1812 rinfo->failed = TRUE;
1813 return FALSE;
1816 /* Note that we are copying a string pointer here, and testing it
1817 above. If bfd_elf_string_from_elf_section is ever changed to
1818 discard the string data when low in memory, this will have to be
1819 fixed. */
1820 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1822 a->vna_flags = h->verinfo.verdef->vd_flags;
1823 a->vna_nextptr = t->vn_auxptr;
1825 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1826 ++rinfo->vers;
1828 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1830 t->vn_auxptr = a;
1832 return TRUE;
1835 /* Figure out appropriate versions for all the symbols. We may not
1836 have the version number script until we have read all of the input
1837 files, so until that point we don't know which symbols should be
1838 local. This function is called via elf_link_hash_traverse. */
1840 bfd_boolean
1841 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1843 struct elf_assign_sym_version_info *sinfo;
1844 struct bfd_link_info *info;
1845 const struct elf_backend_data *bed;
1846 struct elf_info_failed eif;
1847 char *p;
1848 bfd_size_type amt;
1850 sinfo = data;
1851 info = sinfo->info;
1853 if (h->root.type == bfd_link_hash_warning)
1854 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1856 /* Fix the symbol flags. */
1857 eif.failed = FALSE;
1858 eif.info = info;
1859 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1861 if (eif.failed)
1862 sinfo->failed = TRUE;
1863 return FALSE;
1866 /* We only need version numbers for symbols defined in regular
1867 objects. */
1868 if (!h->def_regular)
1869 return TRUE;
1871 bed = get_elf_backend_data (sinfo->output_bfd);
1872 p = strchr (h->root.root.string, ELF_VER_CHR);
1873 if (p != NULL && h->verinfo.vertree == NULL)
1875 struct bfd_elf_version_tree *t;
1876 bfd_boolean hidden;
1878 hidden = TRUE;
1880 /* There are two consecutive ELF_VER_CHR characters if this is
1881 not a hidden symbol. */
1882 ++p;
1883 if (*p == ELF_VER_CHR)
1885 hidden = FALSE;
1886 ++p;
1889 /* If there is no version string, we can just return out. */
1890 if (*p == '\0')
1892 if (hidden)
1893 h->hidden = 1;
1894 return TRUE;
1897 /* Look for the version. If we find it, it is no longer weak. */
1898 for (t = sinfo->verdefs; t != NULL; t = t->next)
1900 if (strcmp (t->name, p) == 0)
1902 size_t len;
1903 char *alc;
1904 struct bfd_elf_version_expr *d;
1906 len = p - h->root.root.string;
1907 alc = bfd_malloc (len);
1908 if (alc == NULL)
1910 sinfo->failed = TRUE;
1911 return FALSE;
1913 memcpy (alc, h->root.root.string, len - 1);
1914 alc[len - 1] = '\0';
1915 if (alc[len - 2] == ELF_VER_CHR)
1916 alc[len - 2] = '\0';
1918 h->verinfo.vertree = t;
1919 t->used = TRUE;
1920 d = NULL;
1922 if (t->globals.list != NULL)
1923 d = (*t->match) (&t->globals, NULL, alc);
1925 /* See if there is anything to force this symbol to
1926 local scope. */
1927 if (d == NULL && t->locals.list != NULL)
1929 d = (*t->match) (&t->locals, NULL, alc);
1930 if (d != NULL
1931 && h->dynindx != -1
1932 && ! info->export_dynamic)
1933 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1936 free (alc);
1937 break;
1941 /* If we are building an application, we need to create a
1942 version node for this version. */
1943 if (t == NULL && info->executable)
1945 struct bfd_elf_version_tree **pp;
1946 int version_index;
1948 /* If we aren't going to export this symbol, we don't need
1949 to worry about it. */
1950 if (h->dynindx == -1)
1951 return TRUE;
1953 amt = sizeof *t;
1954 t = bfd_zalloc (sinfo->output_bfd, amt);
1955 if (t == NULL)
1957 sinfo->failed = TRUE;
1958 return FALSE;
1961 t->name = p;
1962 t->name_indx = (unsigned int) -1;
1963 t->used = TRUE;
1965 version_index = 1;
1966 /* Don't count anonymous version tag. */
1967 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1968 version_index = 0;
1969 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1970 ++version_index;
1971 t->vernum = version_index;
1973 *pp = t;
1975 h->verinfo.vertree = t;
1977 else if (t == NULL)
1979 /* We could not find the version for a symbol when
1980 generating a shared archive. Return an error. */
1981 (*_bfd_error_handler)
1982 (_("%B: version node not found for symbol %s"),
1983 sinfo->output_bfd, h->root.root.string);
1984 bfd_set_error (bfd_error_bad_value);
1985 sinfo->failed = TRUE;
1986 return FALSE;
1989 if (hidden)
1990 h->hidden = 1;
1993 /* If we don't have a version for this symbol, see if we can find
1994 something. */
1995 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1997 struct bfd_elf_version_tree *t;
1998 struct bfd_elf_version_tree *local_ver;
1999 struct bfd_elf_version_expr *d;
2001 /* See if can find what version this symbol is in. If the
2002 symbol is supposed to be local, then don't actually register
2003 it. */
2004 local_ver = NULL;
2005 for (t = sinfo->verdefs; t != NULL; t = t->next)
2007 if (t->globals.list != NULL)
2009 bfd_boolean matched;
2011 matched = FALSE;
2012 d = NULL;
2013 while ((d = (*t->match) (&t->globals, d,
2014 h->root.root.string)) != NULL)
2015 if (d->symver)
2016 matched = TRUE;
2017 else
2019 /* There is a version without definition. Make
2020 the symbol the default definition for this
2021 version. */
2022 h->verinfo.vertree = t;
2023 local_ver = NULL;
2024 d->script = 1;
2025 break;
2027 if (d != NULL)
2028 break;
2029 else if (matched)
2030 /* There is no undefined version for this symbol. Hide the
2031 default one. */
2032 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2035 if (t->locals.list != NULL)
2037 d = NULL;
2038 while ((d = (*t->match) (&t->locals, d,
2039 h->root.root.string)) != NULL)
2041 local_ver = t;
2042 /* If the match is "*", keep looking for a more
2043 explicit, perhaps even global, match.
2044 XXX: Shouldn't this be !d->wildcard instead? */
2045 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
2046 break;
2049 if (d != NULL)
2050 break;
2054 if (local_ver != NULL)
2056 h->verinfo.vertree = local_ver;
2057 if (h->dynindx != -1
2058 && ! info->export_dynamic)
2060 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2065 return TRUE;
2068 /* Read and swap the relocs from the section indicated by SHDR. This
2069 may be either a REL or a RELA section. The relocations are
2070 translated into RELA relocations and stored in INTERNAL_RELOCS,
2071 which should have already been allocated to contain enough space.
2072 The EXTERNAL_RELOCS are a buffer where the external form of the
2073 relocations should be stored.
2075 Returns FALSE if something goes wrong. */
2077 static bfd_boolean
2078 elf_link_read_relocs_from_section (bfd *abfd,
2079 asection *sec,
2080 Elf_Internal_Shdr *shdr,
2081 void *external_relocs,
2082 Elf_Internal_Rela *internal_relocs)
2084 const struct elf_backend_data *bed;
2085 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2086 const bfd_byte *erela;
2087 const bfd_byte *erelaend;
2088 Elf_Internal_Rela *irela;
2089 Elf_Internal_Shdr *symtab_hdr;
2090 size_t nsyms;
2092 /* Position ourselves at the start of the section. */
2093 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2094 return FALSE;
2096 /* Read the relocations. */
2097 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2098 return FALSE;
2100 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2101 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2103 bed = get_elf_backend_data (abfd);
2105 /* Convert the external relocations to the internal format. */
2106 if (shdr->sh_entsize == bed->s->sizeof_rel)
2107 swap_in = bed->s->swap_reloc_in;
2108 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2109 swap_in = bed->s->swap_reloca_in;
2110 else
2112 bfd_set_error (bfd_error_wrong_format);
2113 return FALSE;
2116 erela = external_relocs;
2117 erelaend = erela + shdr->sh_size;
2118 irela = internal_relocs;
2119 while (erela < erelaend)
2121 bfd_vma r_symndx;
2123 (*swap_in) (abfd, erela, irela);
2124 r_symndx = ELF32_R_SYM (irela->r_info);
2125 if (bed->s->arch_size == 64)
2126 r_symndx >>= 24;
2127 if ((size_t) r_symndx >= nsyms)
2129 (*_bfd_error_handler)
2130 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2131 " for offset 0x%lx in section `%A'"),
2132 abfd, sec,
2133 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2134 bfd_set_error (bfd_error_bad_value);
2135 return FALSE;
2137 irela += bed->s->int_rels_per_ext_rel;
2138 erela += shdr->sh_entsize;
2141 return TRUE;
2144 /* Read and swap the relocs for a section O. They may have been
2145 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2146 not NULL, they are used as buffers to read into. They are known to
2147 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2148 the return value is allocated using either malloc or bfd_alloc,
2149 according to the KEEP_MEMORY argument. If O has two relocation
2150 sections (both REL and RELA relocations), then the REL_HDR
2151 relocations will appear first in INTERNAL_RELOCS, followed by the
2152 REL_HDR2 relocations. */
2154 Elf_Internal_Rela *
2155 _bfd_elf_link_read_relocs (bfd *abfd,
2156 asection *o,
2157 void *external_relocs,
2158 Elf_Internal_Rela *internal_relocs,
2159 bfd_boolean keep_memory)
2161 Elf_Internal_Shdr *rel_hdr;
2162 void *alloc1 = NULL;
2163 Elf_Internal_Rela *alloc2 = NULL;
2164 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2166 if (elf_section_data (o)->relocs != NULL)
2167 return elf_section_data (o)->relocs;
2169 if (o->reloc_count == 0)
2170 return NULL;
2172 rel_hdr = &elf_section_data (o)->rel_hdr;
2174 if (internal_relocs == NULL)
2176 bfd_size_type size;
2178 size = o->reloc_count;
2179 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2180 if (keep_memory)
2181 internal_relocs = bfd_alloc (abfd, size);
2182 else
2183 internal_relocs = alloc2 = bfd_malloc (size);
2184 if (internal_relocs == NULL)
2185 goto error_return;
2188 if (external_relocs == NULL)
2190 bfd_size_type size = rel_hdr->sh_size;
2192 if (elf_section_data (o)->rel_hdr2)
2193 size += elf_section_data (o)->rel_hdr2->sh_size;
2194 alloc1 = bfd_malloc (size);
2195 if (alloc1 == NULL)
2196 goto error_return;
2197 external_relocs = alloc1;
2200 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2201 external_relocs,
2202 internal_relocs))
2203 goto error_return;
2204 if (elf_section_data (o)->rel_hdr2
2205 && (!elf_link_read_relocs_from_section
2206 (abfd, o,
2207 elf_section_data (o)->rel_hdr2,
2208 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2209 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2210 * bed->s->int_rels_per_ext_rel))))
2211 goto error_return;
2213 /* Cache the results for next time, if we can. */
2214 if (keep_memory)
2215 elf_section_data (o)->relocs = internal_relocs;
2217 if (alloc1 != NULL)
2218 free (alloc1);
2220 /* Don't free alloc2, since if it was allocated we are passing it
2221 back (under the name of internal_relocs). */
2223 return internal_relocs;
2225 error_return:
2226 if (alloc1 != NULL)
2227 free (alloc1);
2228 if (alloc2 != NULL)
2229 free (alloc2);
2230 return NULL;
2233 /* Compute the size of, and allocate space for, REL_HDR which is the
2234 section header for a section containing relocations for O. */
2236 bfd_boolean
2237 _bfd_elf_link_size_reloc_section (bfd *abfd,
2238 Elf_Internal_Shdr *rel_hdr,
2239 asection *o)
2241 bfd_size_type reloc_count;
2242 bfd_size_type num_rel_hashes;
2244 /* Figure out how many relocations there will be. */
2245 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2246 reloc_count = elf_section_data (o)->rel_count;
2247 else
2248 reloc_count = elf_section_data (o)->rel_count2;
2250 num_rel_hashes = o->reloc_count;
2251 if (num_rel_hashes < reloc_count)
2252 num_rel_hashes = reloc_count;
2254 /* That allows us to calculate the size of the section. */
2255 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2257 /* The contents field must last into write_object_contents, so we
2258 allocate it with bfd_alloc rather than malloc. Also since we
2259 cannot be sure that the contents will actually be filled in,
2260 we zero the allocated space. */
2261 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2262 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2263 return FALSE;
2265 /* We only allocate one set of hash entries, so we only do it the
2266 first time we are called. */
2267 if (elf_section_data (o)->rel_hashes == NULL
2268 && num_rel_hashes)
2270 struct elf_link_hash_entry **p;
2272 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2273 if (p == NULL)
2274 return FALSE;
2276 elf_section_data (o)->rel_hashes = p;
2279 return TRUE;
2282 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2283 originated from the section given by INPUT_REL_HDR) to the
2284 OUTPUT_BFD. */
2286 bfd_boolean
2287 _bfd_elf_link_output_relocs (bfd *output_bfd,
2288 asection *input_section,
2289 Elf_Internal_Shdr *input_rel_hdr,
2290 Elf_Internal_Rela *internal_relocs,
2291 struct elf_link_hash_entry **rel_hash
2292 ATTRIBUTE_UNUSED)
2294 Elf_Internal_Rela *irela;
2295 Elf_Internal_Rela *irelaend;
2296 bfd_byte *erel;
2297 Elf_Internal_Shdr *output_rel_hdr;
2298 asection *output_section;
2299 unsigned int *rel_countp = NULL;
2300 const struct elf_backend_data *bed;
2301 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2303 output_section = input_section->output_section;
2304 output_rel_hdr = NULL;
2306 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2307 == input_rel_hdr->sh_entsize)
2309 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2310 rel_countp = &elf_section_data (output_section)->rel_count;
2312 else if (elf_section_data (output_section)->rel_hdr2
2313 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2314 == input_rel_hdr->sh_entsize))
2316 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2317 rel_countp = &elf_section_data (output_section)->rel_count2;
2319 else
2321 (*_bfd_error_handler)
2322 (_("%B: relocation size mismatch in %B section %A"),
2323 output_bfd, input_section->owner, input_section);
2324 bfd_set_error (bfd_error_wrong_format);
2325 return FALSE;
2328 bed = get_elf_backend_data (output_bfd);
2329 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2330 swap_out = bed->s->swap_reloc_out;
2331 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2332 swap_out = bed->s->swap_reloca_out;
2333 else
2334 abort ();
2336 erel = output_rel_hdr->contents;
2337 erel += *rel_countp * input_rel_hdr->sh_entsize;
2338 irela = internal_relocs;
2339 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2340 * bed->s->int_rels_per_ext_rel);
2341 while (irela < irelaend)
2343 (*swap_out) (output_bfd, irela, erel);
2344 irela += bed->s->int_rels_per_ext_rel;
2345 erel += input_rel_hdr->sh_entsize;
2348 /* Bump the counter, so that we know where to add the next set of
2349 relocations. */
2350 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2352 return TRUE;
2355 /* Make weak undefined symbols in PIE dynamic. */
2357 bfd_boolean
2358 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2359 struct elf_link_hash_entry *h)
2361 if (info->pie
2362 && h->dynindx == -1
2363 && h->root.type == bfd_link_hash_undefweak)
2364 return bfd_elf_link_record_dynamic_symbol (info, h);
2366 return TRUE;
2369 /* Fix up the flags for a symbol. This handles various cases which
2370 can only be fixed after all the input files are seen. This is
2371 currently called by both adjust_dynamic_symbol and
2372 assign_sym_version, which is unnecessary but perhaps more robust in
2373 the face of future changes. */
2375 bfd_boolean
2376 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2377 struct elf_info_failed *eif)
2379 const struct elf_backend_data *bed;
2381 /* If this symbol was mentioned in a non-ELF file, try to set
2382 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2383 permit a non-ELF file to correctly refer to a symbol defined in
2384 an ELF dynamic object. */
2385 if (h->non_elf)
2387 while (h->root.type == bfd_link_hash_indirect)
2388 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2390 if (h->root.type != bfd_link_hash_defined
2391 && h->root.type != bfd_link_hash_defweak)
2393 h->ref_regular = 1;
2394 h->ref_regular_nonweak = 1;
2396 else
2398 if (h->root.u.def.section->owner != NULL
2399 && (bfd_get_flavour (h->root.u.def.section->owner)
2400 == bfd_target_elf_flavour))
2402 h->ref_regular = 1;
2403 h->ref_regular_nonweak = 1;
2405 else
2406 h->def_regular = 1;
2409 if (h->dynindx == -1
2410 && (h->def_dynamic
2411 || h->ref_dynamic))
2413 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2415 eif->failed = TRUE;
2416 return FALSE;
2420 else
2422 /* Unfortunately, NON_ELF is only correct if the symbol
2423 was first seen in a non-ELF file. Fortunately, if the symbol
2424 was first seen in an ELF file, we're probably OK unless the
2425 symbol was defined in a non-ELF file. Catch that case here.
2426 FIXME: We're still in trouble if the symbol was first seen in
2427 a dynamic object, and then later in a non-ELF regular object. */
2428 if ((h->root.type == bfd_link_hash_defined
2429 || h->root.type == bfd_link_hash_defweak)
2430 && !h->def_regular
2431 && (h->root.u.def.section->owner != NULL
2432 ? (bfd_get_flavour (h->root.u.def.section->owner)
2433 != bfd_target_elf_flavour)
2434 : (bfd_is_abs_section (h->root.u.def.section)
2435 && !h->def_dynamic)))
2436 h->def_regular = 1;
2439 /* Backend specific symbol fixup. */
2440 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2441 if (bed->elf_backend_fixup_symbol
2442 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2443 return FALSE;
2445 /* If this is a final link, and the symbol was defined as a common
2446 symbol in a regular object file, and there was no definition in
2447 any dynamic object, then the linker will have allocated space for
2448 the symbol in a common section but the DEF_REGULAR
2449 flag will not have been set. */
2450 if (h->root.type == bfd_link_hash_defined
2451 && !h->def_regular
2452 && h->ref_regular
2453 && !h->def_dynamic
2454 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2455 h->def_regular = 1;
2457 /* If -Bsymbolic was used (which means to bind references to global
2458 symbols to the definition within the shared object), and this
2459 symbol was defined in a regular object, then it actually doesn't
2460 need a PLT entry. Likewise, if the symbol has non-default
2461 visibility. If the symbol has hidden or internal visibility, we
2462 will force it local. */
2463 if (h->needs_plt
2464 && eif->info->shared
2465 && is_elf_hash_table (eif->info->hash)
2466 && (SYMBOLIC_BIND (eif->info, h)
2467 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2468 && h->def_regular)
2470 bfd_boolean force_local;
2472 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2473 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2474 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2477 /* If a weak undefined symbol has non-default visibility, we also
2478 hide it from the dynamic linker. */
2479 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2480 && h->root.type == bfd_link_hash_undefweak)
2481 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2483 /* If this is a weak defined symbol in a dynamic object, and we know
2484 the real definition in the dynamic object, copy interesting flags
2485 over to the real definition. */
2486 if (h->u.weakdef != NULL)
2488 struct elf_link_hash_entry *weakdef;
2490 weakdef = h->u.weakdef;
2491 if (h->root.type == bfd_link_hash_indirect)
2492 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2494 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2495 || h->root.type == bfd_link_hash_defweak);
2496 BFD_ASSERT (weakdef->def_dynamic);
2498 /* If the real definition is defined by a regular object file,
2499 don't do anything special. See the longer description in
2500 _bfd_elf_adjust_dynamic_symbol, below. */
2501 if (weakdef->def_regular)
2502 h->u.weakdef = NULL;
2503 else
2505 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2506 || weakdef->root.type == bfd_link_hash_defweak);
2507 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2511 return TRUE;
2514 /* Make the backend pick a good value for a dynamic symbol. This is
2515 called via elf_link_hash_traverse, and also calls itself
2516 recursively. */
2518 bfd_boolean
2519 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2521 struct elf_info_failed *eif = data;
2522 bfd *dynobj;
2523 const struct elf_backend_data *bed;
2525 if (! is_elf_hash_table (eif->info->hash))
2526 return FALSE;
2528 if (h->root.type == bfd_link_hash_warning)
2530 h->got = elf_hash_table (eif->info)->init_got_offset;
2531 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2533 /* When warning symbols are created, they **replace** the "real"
2534 entry in the hash table, thus we never get to see the real
2535 symbol in a hash traversal. So look at it now. */
2536 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2539 /* Ignore indirect symbols. These are added by the versioning code. */
2540 if (h->root.type == bfd_link_hash_indirect)
2541 return TRUE;
2543 /* Fix the symbol flags. */
2544 if (! _bfd_elf_fix_symbol_flags (h, eif))
2545 return FALSE;
2547 /* If this symbol does not require a PLT entry, and it is not
2548 defined by a dynamic object, or is not referenced by a regular
2549 object, ignore it. We do have to handle a weak defined symbol,
2550 even if no regular object refers to it, if we decided to add it
2551 to the dynamic symbol table. FIXME: Do we normally need to worry
2552 about symbols which are defined by one dynamic object and
2553 referenced by another one? */
2554 if (!h->needs_plt
2555 && (h->def_regular
2556 || !h->def_dynamic
2557 || (!h->ref_regular
2558 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2560 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2561 return TRUE;
2564 /* If we've already adjusted this symbol, don't do it again. This
2565 can happen via a recursive call. */
2566 if (h->dynamic_adjusted)
2567 return TRUE;
2569 /* Don't look at this symbol again. Note that we must set this
2570 after checking the above conditions, because we may look at a
2571 symbol once, decide not to do anything, and then get called
2572 recursively later after REF_REGULAR is set below. */
2573 h->dynamic_adjusted = 1;
2575 /* If this is a weak definition, and we know a real definition, and
2576 the real symbol is not itself defined by a regular object file,
2577 then get a good value for the real definition. We handle the
2578 real symbol first, for the convenience of the backend routine.
2580 Note that there is a confusing case here. If the real definition
2581 is defined by a regular object file, we don't get the real symbol
2582 from the dynamic object, but we do get the weak symbol. If the
2583 processor backend uses a COPY reloc, then if some routine in the
2584 dynamic object changes the real symbol, we will not see that
2585 change in the corresponding weak symbol. This is the way other
2586 ELF linkers work as well, and seems to be a result of the shared
2587 library model.
2589 I will clarify this issue. Most SVR4 shared libraries define the
2590 variable _timezone and define timezone as a weak synonym. The
2591 tzset call changes _timezone. If you write
2592 extern int timezone;
2593 int _timezone = 5;
2594 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2595 you might expect that, since timezone is a synonym for _timezone,
2596 the same number will print both times. However, if the processor
2597 backend uses a COPY reloc, then actually timezone will be copied
2598 into your process image, and, since you define _timezone
2599 yourself, _timezone will not. Thus timezone and _timezone will
2600 wind up at different memory locations. The tzset call will set
2601 _timezone, leaving timezone unchanged. */
2603 if (h->u.weakdef != NULL)
2605 /* If we get to this point, we know there is an implicit
2606 reference by a regular object file via the weak symbol H.
2607 FIXME: Is this really true? What if the traversal finds
2608 H->U.WEAKDEF before it finds H? */
2609 h->u.weakdef->ref_regular = 1;
2611 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2612 return FALSE;
2615 /* If a symbol has no type and no size and does not require a PLT
2616 entry, then we are probably about to do the wrong thing here: we
2617 are probably going to create a COPY reloc for an empty object.
2618 This case can arise when a shared object is built with assembly
2619 code, and the assembly code fails to set the symbol type. */
2620 if (h->size == 0
2621 && h->type == STT_NOTYPE
2622 && !h->needs_plt)
2623 (*_bfd_error_handler)
2624 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2625 h->root.root.string);
2627 dynobj = elf_hash_table (eif->info)->dynobj;
2628 bed = get_elf_backend_data (dynobj);
2629 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2631 eif->failed = TRUE;
2632 return FALSE;
2635 return TRUE;
2638 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2639 DYNBSS. */
2641 bfd_boolean
2642 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2643 asection *dynbss)
2645 unsigned int power_of_two;
2646 bfd_vma mask;
2647 asection *sec = h->root.u.def.section;
2649 /* The section aligment of definition is the maximum alignment
2650 requirement of symbols defined in the section. Since we don't
2651 know the symbol alignment requirement, we start with the
2652 maximum alignment and check low bits of the symbol address
2653 for the minimum alignment. */
2654 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2655 mask = ((bfd_vma) 1 << power_of_two) - 1;
2656 while ((h->root.u.def.value & mask) != 0)
2658 mask >>= 1;
2659 --power_of_two;
2662 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2663 dynbss))
2665 /* Adjust the section alignment if needed. */
2666 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2667 power_of_two))
2668 return FALSE;
2671 /* We make sure that the symbol will be aligned properly. */
2672 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2674 /* Define the symbol as being at this point in DYNBSS. */
2675 h->root.u.def.section = dynbss;
2676 h->root.u.def.value = dynbss->size;
2678 /* Increment the size of DYNBSS to make room for the symbol. */
2679 dynbss->size += h->size;
2681 return TRUE;
2684 /* Adjust all external symbols pointing into SEC_MERGE sections
2685 to reflect the object merging within the sections. */
2687 bfd_boolean
2688 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2690 asection *sec;
2692 if (h->root.type == bfd_link_hash_warning)
2693 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2695 if ((h->root.type == bfd_link_hash_defined
2696 || h->root.type == bfd_link_hash_defweak)
2697 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2698 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2700 bfd *output_bfd = data;
2702 h->root.u.def.value =
2703 _bfd_merged_section_offset (output_bfd,
2704 &h->root.u.def.section,
2705 elf_section_data (sec)->sec_info,
2706 h->root.u.def.value);
2709 return TRUE;
2712 /* Returns false if the symbol referred to by H should be considered
2713 to resolve local to the current module, and true if it should be
2714 considered to bind dynamically. */
2716 bfd_boolean
2717 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2718 struct bfd_link_info *info,
2719 bfd_boolean ignore_protected)
2721 bfd_boolean binding_stays_local_p;
2722 const struct elf_backend_data *bed;
2723 struct elf_link_hash_table *hash_table;
2725 if (h == NULL)
2726 return FALSE;
2728 while (h->root.type == bfd_link_hash_indirect
2729 || h->root.type == bfd_link_hash_warning)
2730 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2732 /* If it was forced local, then clearly it's not dynamic. */
2733 if (h->dynindx == -1)
2734 return FALSE;
2735 if (h->forced_local)
2736 return FALSE;
2738 /* Identify the cases where name binding rules say that a
2739 visible symbol resolves locally. */
2740 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2742 switch (ELF_ST_VISIBILITY (h->other))
2744 case STV_INTERNAL:
2745 case STV_HIDDEN:
2746 return FALSE;
2748 case STV_PROTECTED:
2749 hash_table = elf_hash_table (info);
2750 if (!is_elf_hash_table (hash_table))
2751 return FALSE;
2753 bed = get_elf_backend_data (hash_table->dynobj);
2755 /* Proper resolution for function pointer equality may require
2756 that these symbols perhaps be resolved dynamically, even though
2757 we should be resolving them to the current module. */
2758 if (!ignore_protected || !bed->is_function_type (h->type))
2759 binding_stays_local_p = TRUE;
2760 break;
2762 default:
2763 break;
2766 /* If it isn't defined locally, then clearly it's dynamic. */
2767 if (!h->def_regular)
2768 return TRUE;
2770 /* Otherwise, the symbol is dynamic if binding rules don't tell
2771 us that it remains local. */
2772 return !binding_stays_local_p;
2775 /* Return true if the symbol referred to by H should be considered
2776 to resolve local to the current module, and false otherwise. Differs
2777 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2778 undefined symbols and weak symbols. */
2780 bfd_boolean
2781 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2782 struct bfd_link_info *info,
2783 bfd_boolean local_protected)
2785 const struct elf_backend_data *bed;
2786 struct elf_link_hash_table *hash_table;
2788 /* If it's a local sym, of course we resolve locally. */
2789 if (h == NULL)
2790 return TRUE;
2792 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2793 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2794 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2795 return TRUE;
2797 /* Common symbols that become definitions don't get the DEF_REGULAR
2798 flag set, so test it first, and don't bail out. */
2799 if (ELF_COMMON_DEF_P (h))
2800 /* Do nothing. */;
2801 /* If we don't have a definition in a regular file, then we can't
2802 resolve locally. The sym is either undefined or dynamic. */
2803 else if (!h->def_regular)
2804 return FALSE;
2806 /* Forced local symbols resolve locally. */
2807 if (h->forced_local)
2808 return TRUE;
2810 /* As do non-dynamic symbols. */
2811 if (h->dynindx == -1)
2812 return TRUE;
2814 /* At this point, we know the symbol is defined and dynamic. In an
2815 executable it must resolve locally, likewise when building symbolic
2816 shared libraries. */
2817 if (info->executable || SYMBOLIC_BIND (info, h))
2818 return TRUE;
2820 /* Now deal with defined dynamic symbols in shared libraries. Ones
2821 with default visibility might not resolve locally. */
2822 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2823 return FALSE;
2825 hash_table = elf_hash_table (info);
2826 if (!is_elf_hash_table (hash_table))
2827 return TRUE;
2829 bed = get_elf_backend_data (hash_table->dynobj);
2831 /* STV_PROTECTED non-function symbols are local. */
2832 if (!bed->is_function_type (h->type))
2833 return TRUE;
2835 /* Function pointer equality tests may require that STV_PROTECTED
2836 symbols be treated as dynamic symbols, even when we know that the
2837 dynamic linker will resolve them locally. */
2838 return local_protected;
2841 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2842 aligned. Returns the first TLS output section. */
2844 struct bfd_section *
2845 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2847 struct bfd_section *sec, *tls;
2848 unsigned int align = 0;
2850 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2851 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2852 break;
2853 tls = sec;
2855 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2856 if (sec->alignment_power > align)
2857 align = sec->alignment_power;
2859 elf_hash_table (info)->tls_sec = tls;
2861 /* Ensure the alignment of the first section is the largest alignment,
2862 so that the tls segment starts aligned. */
2863 if (tls != NULL)
2864 tls->alignment_power = align;
2866 return tls;
2869 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2870 static bfd_boolean
2871 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2872 Elf_Internal_Sym *sym)
2874 const struct elf_backend_data *bed;
2876 /* Local symbols do not count, but target specific ones might. */
2877 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2878 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2879 return FALSE;
2881 bed = get_elf_backend_data (abfd);
2882 /* Function symbols do not count. */
2883 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2884 return FALSE;
2886 /* If the section is undefined, then so is the symbol. */
2887 if (sym->st_shndx == SHN_UNDEF)
2888 return FALSE;
2890 /* If the symbol is defined in the common section, then
2891 it is a common definition and so does not count. */
2892 if (bed->common_definition (sym))
2893 return FALSE;
2895 /* If the symbol is in a target specific section then we
2896 must rely upon the backend to tell us what it is. */
2897 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2898 /* FIXME - this function is not coded yet:
2900 return _bfd_is_global_symbol_definition (abfd, sym);
2902 Instead for now assume that the definition is not global,
2903 Even if this is wrong, at least the linker will behave
2904 in the same way that it used to do. */
2905 return FALSE;
2907 return TRUE;
2910 /* Search the symbol table of the archive element of the archive ABFD
2911 whose archive map contains a mention of SYMDEF, and determine if
2912 the symbol is defined in this element. */
2913 static bfd_boolean
2914 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2916 Elf_Internal_Shdr * hdr;
2917 bfd_size_type symcount;
2918 bfd_size_type extsymcount;
2919 bfd_size_type extsymoff;
2920 Elf_Internal_Sym *isymbuf;
2921 Elf_Internal_Sym *isym;
2922 Elf_Internal_Sym *isymend;
2923 bfd_boolean result;
2925 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2926 if (abfd == NULL)
2927 return FALSE;
2929 if (! bfd_check_format (abfd, bfd_object))
2930 return FALSE;
2932 /* If we have already included the element containing this symbol in the
2933 link then we do not need to include it again. Just claim that any symbol
2934 it contains is not a definition, so that our caller will not decide to
2935 (re)include this element. */
2936 if (abfd->archive_pass)
2937 return FALSE;
2939 /* Select the appropriate symbol table. */
2940 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2941 hdr = &elf_tdata (abfd)->symtab_hdr;
2942 else
2943 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2945 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2947 /* The sh_info field of the symtab header tells us where the
2948 external symbols start. We don't care about the local symbols. */
2949 if (elf_bad_symtab (abfd))
2951 extsymcount = symcount;
2952 extsymoff = 0;
2954 else
2956 extsymcount = symcount - hdr->sh_info;
2957 extsymoff = hdr->sh_info;
2960 if (extsymcount == 0)
2961 return FALSE;
2963 /* Read in the symbol table. */
2964 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2965 NULL, NULL, NULL);
2966 if (isymbuf == NULL)
2967 return FALSE;
2969 /* Scan the symbol table looking for SYMDEF. */
2970 result = FALSE;
2971 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2973 const char *name;
2975 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2976 isym->st_name);
2977 if (name == NULL)
2978 break;
2980 if (strcmp (name, symdef->name) == 0)
2982 result = is_global_data_symbol_definition (abfd, isym);
2983 break;
2987 free (isymbuf);
2989 return result;
2992 /* Add an entry to the .dynamic table. */
2994 bfd_boolean
2995 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2996 bfd_vma tag,
2997 bfd_vma val)
2999 struct elf_link_hash_table *hash_table;
3000 const struct elf_backend_data *bed;
3001 asection *s;
3002 bfd_size_type newsize;
3003 bfd_byte *newcontents;
3004 Elf_Internal_Dyn dyn;
3006 hash_table = elf_hash_table (info);
3007 if (! is_elf_hash_table (hash_table))
3008 return FALSE;
3010 bed = get_elf_backend_data (hash_table->dynobj);
3011 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3012 BFD_ASSERT (s != NULL);
3014 newsize = s->size + bed->s->sizeof_dyn;
3015 newcontents = bfd_realloc (s->contents, newsize);
3016 if (newcontents == NULL)
3017 return FALSE;
3019 dyn.d_tag = tag;
3020 dyn.d_un.d_val = val;
3021 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3023 s->size = newsize;
3024 s->contents = newcontents;
3026 return TRUE;
3029 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3030 otherwise just check whether one already exists. Returns -1 on error,
3031 1 if a DT_NEEDED tag already exists, and 0 on success. */
3033 static int
3034 elf_add_dt_needed_tag (bfd *abfd,
3035 struct bfd_link_info *info,
3036 const char *soname,
3037 bfd_boolean do_it)
3039 struct elf_link_hash_table *hash_table;
3040 bfd_size_type oldsize;
3041 bfd_size_type strindex;
3043 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3044 return -1;
3046 hash_table = elf_hash_table (info);
3047 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3048 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3049 if (strindex == (bfd_size_type) -1)
3050 return -1;
3052 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3054 asection *sdyn;
3055 const struct elf_backend_data *bed;
3056 bfd_byte *extdyn;
3058 bed = get_elf_backend_data (hash_table->dynobj);
3059 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3060 if (sdyn != NULL)
3061 for (extdyn = sdyn->contents;
3062 extdyn < sdyn->contents + sdyn->size;
3063 extdyn += bed->s->sizeof_dyn)
3065 Elf_Internal_Dyn dyn;
3067 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3068 if (dyn.d_tag == DT_NEEDED
3069 && dyn.d_un.d_val == strindex)
3071 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3072 return 1;
3077 if (do_it)
3079 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3080 return -1;
3082 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3083 return -1;
3085 else
3086 /* We were just checking for existence of the tag. */
3087 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3089 return 0;
3092 /* Sort symbol by value and section. */
3093 static int
3094 elf_sort_symbol (const void *arg1, const void *arg2)
3096 const struct elf_link_hash_entry *h1;
3097 const struct elf_link_hash_entry *h2;
3098 bfd_signed_vma vdiff;
3100 h1 = *(const struct elf_link_hash_entry **) arg1;
3101 h2 = *(const struct elf_link_hash_entry **) arg2;
3102 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3103 if (vdiff != 0)
3104 return vdiff > 0 ? 1 : -1;
3105 else
3107 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3108 if (sdiff != 0)
3109 return sdiff > 0 ? 1 : -1;
3111 return 0;
3114 /* This function is used to adjust offsets into .dynstr for
3115 dynamic symbols. This is called via elf_link_hash_traverse. */
3117 static bfd_boolean
3118 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3120 struct elf_strtab_hash *dynstr = data;
3122 if (h->root.type == bfd_link_hash_warning)
3123 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3125 if (h->dynindx != -1)
3126 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3127 return TRUE;
3130 /* Assign string offsets in .dynstr, update all structures referencing
3131 them. */
3133 static bfd_boolean
3134 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3136 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3137 struct elf_link_local_dynamic_entry *entry;
3138 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3139 bfd *dynobj = hash_table->dynobj;
3140 asection *sdyn;
3141 bfd_size_type size;
3142 const struct elf_backend_data *bed;
3143 bfd_byte *extdyn;
3145 _bfd_elf_strtab_finalize (dynstr);
3146 size = _bfd_elf_strtab_size (dynstr);
3148 bed = get_elf_backend_data (dynobj);
3149 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3150 BFD_ASSERT (sdyn != NULL);
3152 /* Update all .dynamic entries referencing .dynstr strings. */
3153 for (extdyn = sdyn->contents;
3154 extdyn < sdyn->contents + sdyn->size;
3155 extdyn += bed->s->sizeof_dyn)
3157 Elf_Internal_Dyn dyn;
3159 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3160 switch (dyn.d_tag)
3162 case DT_STRSZ:
3163 dyn.d_un.d_val = size;
3164 break;
3165 case DT_NEEDED:
3166 case DT_SONAME:
3167 case DT_RPATH:
3168 case DT_RUNPATH:
3169 case DT_FILTER:
3170 case DT_AUXILIARY:
3171 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3172 break;
3173 default:
3174 continue;
3176 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3179 /* Now update local dynamic symbols. */
3180 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3181 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3182 entry->isym.st_name);
3184 /* And the rest of dynamic symbols. */
3185 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3187 /* Adjust version definitions. */
3188 if (elf_tdata (output_bfd)->cverdefs)
3190 asection *s;
3191 bfd_byte *p;
3192 bfd_size_type i;
3193 Elf_Internal_Verdef def;
3194 Elf_Internal_Verdaux defaux;
3196 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3197 p = s->contents;
3200 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3201 &def);
3202 p += sizeof (Elf_External_Verdef);
3203 if (def.vd_aux != sizeof (Elf_External_Verdef))
3204 continue;
3205 for (i = 0; i < def.vd_cnt; ++i)
3207 _bfd_elf_swap_verdaux_in (output_bfd,
3208 (Elf_External_Verdaux *) p, &defaux);
3209 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3210 defaux.vda_name);
3211 _bfd_elf_swap_verdaux_out (output_bfd,
3212 &defaux, (Elf_External_Verdaux *) p);
3213 p += sizeof (Elf_External_Verdaux);
3216 while (def.vd_next);
3219 /* Adjust version references. */
3220 if (elf_tdata (output_bfd)->verref)
3222 asection *s;
3223 bfd_byte *p;
3224 bfd_size_type i;
3225 Elf_Internal_Verneed need;
3226 Elf_Internal_Vernaux needaux;
3228 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3229 p = s->contents;
3232 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3233 &need);
3234 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3235 _bfd_elf_swap_verneed_out (output_bfd, &need,
3236 (Elf_External_Verneed *) p);
3237 p += sizeof (Elf_External_Verneed);
3238 for (i = 0; i < need.vn_cnt; ++i)
3240 _bfd_elf_swap_vernaux_in (output_bfd,
3241 (Elf_External_Vernaux *) p, &needaux);
3242 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3243 needaux.vna_name);
3244 _bfd_elf_swap_vernaux_out (output_bfd,
3245 &needaux,
3246 (Elf_External_Vernaux *) p);
3247 p += sizeof (Elf_External_Vernaux);
3250 while (need.vn_next);
3253 return TRUE;
3256 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3257 The default is to only match when the INPUT and OUTPUT are exactly
3258 the same target. */
3260 bfd_boolean
3261 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3262 const bfd_target *output)
3264 return input == output;
3267 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3268 This version is used when different targets for the same architecture
3269 are virtually identical. */
3271 bfd_boolean
3272 _bfd_elf_relocs_compatible (const bfd_target *input,
3273 const bfd_target *output)
3275 const struct elf_backend_data *obed, *ibed;
3277 if (input == output)
3278 return TRUE;
3280 ibed = xvec_get_elf_backend_data (input);
3281 obed = xvec_get_elf_backend_data (output);
3283 if (ibed->arch != obed->arch)
3284 return FALSE;
3286 /* If both backends are using this function, deem them compatible. */
3287 return ibed->relocs_compatible == obed->relocs_compatible;
3290 /* Add symbols from an ELF object file to the linker hash table. */
3292 static bfd_boolean
3293 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3295 Elf_Internal_Shdr *hdr;
3296 bfd_size_type symcount;
3297 bfd_size_type extsymcount;
3298 bfd_size_type extsymoff;
3299 struct elf_link_hash_entry **sym_hash;
3300 bfd_boolean dynamic;
3301 Elf_External_Versym *extversym = NULL;
3302 Elf_External_Versym *ever;
3303 struct elf_link_hash_entry *weaks;
3304 struct elf_link_hash_entry **nondeflt_vers = NULL;
3305 bfd_size_type nondeflt_vers_cnt = 0;
3306 Elf_Internal_Sym *isymbuf = NULL;
3307 Elf_Internal_Sym *isym;
3308 Elf_Internal_Sym *isymend;
3309 const struct elf_backend_data *bed;
3310 bfd_boolean add_needed;
3311 struct elf_link_hash_table *htab;
3312 bfd_size_type amt;
3313 void *alloc_mark = NULL;
3314 struct bfd_hash_entry **old_table = NULL;
3315 unsigned int old_size = 0;
3316 unsigned int old_count = 0;
3317 void *old_tab = NULL;
3318 void *old_hash;
3319 void *old_ent;
3320 struct bfd_link_hash_entry *old_undefs = NULL;
3321 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3322 long old_dynsymcount = 0;
3323 size_t tabsize = 0;
3324 size_t hashsize = 0;
3326 htab = elf_hash_table (info);
3327 bed = get_elf_backend_data (abfd);
3329 if ((abfd->flags & DYNAMIC) == 0)
3330 dynamic = FALSE;
3331 else
3333 dynamic = TRUE;
3335 /* You can't use -r against a dynamic object. Also, there's no
3336 hope of using a dynamic object which does not exactly match
3337 the format of the output file. */
3338 if (info->relocatable
3339 || !is_elf_hash_table (htab)
3340 || info->output_bfd->xvec != abfd->xvec)
3342 if (info->relocatable)
3343 bfd_set_error (bfd_error_invalid_operation);
3344 else
3345 bfd_set_error (bfd_error_wrong_format);
3346 goto error_return;
3350 /* As a GNU extension, any input sections which are named
3351 .gnu.warning.SYMBOL are treated as warning symbols for the given
3352 symbol. This differs from .gnu.warning sections, which generate
3353 warnings when they are included in an output file. */
3354 if (info->executable)
3356 asection *s;
3358 for (s = abfd->sections; s != NULL; s = s->next)
3360 const char *name;
3362 name = bfd_get_section_name (abfd, s);
3363 if (CONST_STRNEQ (name, ".gnu.warning."))
3365 char *msg;
3366 bfd_size_type sz;
3368 name += sizeof ".gnu.warning." - 1;
3370 /* If this is a shared object, then look up the symbol
3371 in the hash table. If it is there, and it is already
3372 been defined, then we will not be using the entry
3373 from this shared object, so we don't need to warn.
3374 FIXME: If we see the definition in a regular object
3375 later on, we will warn, but we shouldn't. The only
3376 fix is to keep track of what warnings we are supposed
3377 to emit, and then handle them all at the end of the
3378 link. */
3379 if (dynamic)
3381 struct elf_link_hash_entry *h;
3383 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3385 /* FIXME: What about bfd_link_hash_common? */
3386 if (h != NULL
3387 && (h->root.type == bfd_link_hash_defined
3388 || h->root.type == bfd_link_hash_defweak))
3390 /* We don't want to issue this warning. Clobber
3391 the section size so that the warning does not
3392 get copied into the output file. */
3393 s->size = 0;
3394 continue;
3398 sz = s->size;
3399 msg = bfd_alloc (abfd, sz + 1);
3400 if (msg == NULL)
3401 goto error_return;
3403 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3404 goto error_return;
3406 msg[sz] = '\0';
3408 if (! (_bfd_generic_link_add_one_symbol
3409 (info, abfd, name, BSF_WARNING, s, 0, msg,
3410 FALSE, bed->collect, NULL)))
3411 goto error_return;
3413 if (! info->relocatable)
3415 /* Clobber the section size so that the warning does
3416 not get copied into the output file. */
3417 s->size = 0;
3419 /* Also set SEC_EXCLUDE, so that symbols defined in
3420 the warning section don't get copied to the output. */
3421 s->flags |= SEC_EXCLUDE;
3427 add_needed = TRUE;
3428 if (! dynamic)
3430 /* If we are creating a shared library, create all the dynamic
3431 sections immediately. We need to attach them to something,
3432 so we attach them to this BFD, provided it is the right
3433 format. FIXME: If there are no input BFD's of the same
3434 format as the output, we can't make a shared library. */
3435 if (info->shared
3436 && is_elf_hash_table (htab)
3437 && info->output_bfd->xvec == abfd->xvec
3438 && !htab->dynamic_sections_created)
3440 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3441 goto error_return;
3444 else if (!is_elf_hash_table (htab))
3445 goto error_return;
3446 else
3448 asection *s;
3449 const char *soname = NULL;
3450 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3451 int ret;
3453 /* ld --just-symbols and dynamic objects don't mix very well.
3454 ld shouldn't allow it. */
3455 if ((s = abfd->sections) != NULL
3456 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3457 abort ();
3459 /* If this dynamic lib was specified on the command line with
3460 --as-needed in effect, then we don't want to add a DT_NEEDED
3461 tag unless the lib is actually used. Similary for libs brought
3462 in by another lib's DT_NEEDED. When --no-add-needed is used
3463 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3464 any dynamic library in DT_NEEDED tags in the dynamic lib at
3465 all. */
3466 add_needed = (elf_dyn_lib_class (abfd)
3467 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3468 | DYN_NO_NEEDED)) == 0;
3470 s = bfd_get_section_by_name (abfd, ".dynamic");
3471 if (s != NULL)
3473 bfd_byte *dynbuf;
3474 bfd_byte *extdyn;
3475 int elfsec;
3476 unsigned long shlink;
3478 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3479 goto error_free_dyn;
3481 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3482 if (elfsec == -1)
3483 goto error_free_dyn;
3484 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3486 for (extdyn = dynbuf;
3487 extdyn < dynbuf + s->size;
3488 extdyn += bed->s->sizeof_dyn)
3490 Elf_Internal_Dyn dyn;
3492 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3493 if (dyn.d_tag == DT_SONAME)
3495 unsigned int tagv = dyn.d_un.d_val;
3496 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3497 if (soname == NULL)
3498 goto error_free_dyn;
3500 if (dyn.d_tag == DT_NEEDED)
3502 struct bfd_link_needed_list *n, **pn;
3503 char *fnm, *anm;
3504 unsigned int tagv = dyn.d_un.d_val;
3506 amt = sizeof (struct bfd_link_needed_list);
3507 n = bfd_alloc (abfd, amt);
3508 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3509 if (n == NULL || fnm == NULL)
3510 goto error_free_dyn;
3511 amt = strlen (fnm) + 1;
3512 anm = bfd_alloc (abfd, amt);
3513 if (anm == NULL)
3514 goto error_free_dyn;
3515 memcpy (anm, fnm, amt);
3516 n->name = anm;
3517 n->by = abfd;
3518 n->next = NULL;
3519 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3521 *pn = n;
3523 if (dyn.d_tag == DT_RUNPATH)
3525 struct bfd_link_needed_list *n, **pn;
3526 char *fnm, *anm;
3527 unsigned int tagv = dyn.d_un.d_val;
3529 amt = sizeof (struct bfd_link_needed_list);
3530 n = bfd_alloc (abfd, amt);
3531 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3532 if (n == NULL || fnm == NULL)
3533 goto error_free_dyn;
3534 amt = strlen (fnm) + 1;
3535 anm = bfd_alloc (abfd, amt);
3536 if (anm == NULL)
3537 goto error_free_dyn;
3538 memcpy (anm, fnm, amt);
3539 n->name = anm;
3540 n->by = abfd;
3541 n->next = NULL;
3542 for (pn = & runpath;
3543 *pn != NULL;
3544 pn = &(*pn)->next)
3546 *pn = n;
3548 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3549 if (!runpath && dyn.d_tag == DT_RPATH)
3551 struct bfd_link_needed_list *n, **pn;
3552 char *fnm, *anm;
3553 unsigned int tagv = dyn.d_un.d_val;
3555 amt = sizeof (struct bfd_link_needed_list);
3556 n = bfd_alloc (abfd, amt);
3557 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3558 if (n == NULL || fnm == NULL)
3559 goto error_free_dyn;
3560 amt = strlen (fnm) + 1;
3561 anm = bfd_alloc (abfd, amt);
3562 if (anm == NULL)
3564 error_free_dyn:
3565 free (dynbuf);
3566 goto error_return;
3568 memcpy (anm, fnm, amt);
3569 n->name = anm;
3570 n->by = abfd;
3571 n->next = NULL;
3572 for (pn = & rpath;
3573 *pn != NULL;
3574 pn = &(*pn)->next)
3576 *pn = n;
3580 free (dynbuf);
3583 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3584 frees all more recently bfd_alloc'd blocks as well. */
3585 if (runpath)
3586 rpath = runpath;
3588 if (rpath)
3590 struct bfd_link_needed_list **pn;
3591 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3593 *pn = rpath;
3596 /* We do not want to include any of the sections in a dynamic
3597 object in the output file. We hack by simply clobbering the
3598 list of sections in the BFD. This could be handled more
3599 cleanly by, say, a new section flag; the existing
3600 SEC_NEVER_LOAD flag is not the one we want, because that one
3601 still implies that the section takes up space in the output
3602 file. */
3603 bfd_section_list_clear (abfd);
3605 /* Find the name to use in a DT_NEEDED entry that refers to this
3606 object. If the object has a DT_SONAME entry, we use it.
3607 Otherwise, if the generic linker stuck something in
3608 elf_dt_name, we use that. Otherwise, we just use the file
3609 name. */
3610 if (soname == NULL || *soname == '\0')
3612 soname = elf_dt_name (abfd);
3613 if (soname == NULL || *soname == '\0')
3614 soname = bfd_get_filename (abfd);
3617 /* Save the SONAME because sometimes the linker emulation code
3618 will need to know it. */
3619 elf_dt_name (abfd) = soname;
3621 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3622 if (ret < 0)
3623 goto error_return;
3625 /* If we have already included this dynamic object in the
3626 link, just ignore it. There is no reason to include a
3627 particular dynamic object more than once. */
3628 if (ret > 0)
3629 return TRUE;
3632 /* If this is a dynamic object, we always link against the .dynsym
3633 symbol table, not the .symtab symbol table. The dynamic linker
3634 will only see the .dynsym symbol table, so there is no reason to
3635 look at .symtab for a dynamic object. */
3637 if (! dynamic || elf_dynsymtab (abfd) == 0)
3638 hdr = &elf_tdata (abfd)->symtab_hdr;
3639 else
3640 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3642 symcount = hdr->sh_size / bed->s->sizeof_sym;
3644 /* The sh_info field of the symtab header tells us where the
3645 external symbols start. We don't care about the local symbols at
3646 this point. */
3647 if (elf_bad_symtab (abfd))
3649 extsymcount = symcount;
3650 extsymoff = 0;
3652 else
3654 extsymcount = symcount - hdr->sh_info;
3655 extsymoff = hdr->sh_info;
3658 sym_hash = NULL;
3659 if (extsymcount != 0)
3661 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3662 NULL, NULL, NULL);
3663 if (isymbuf == NULL)
3664 goto error_return;
3666 /* We store a pointer to the hash table entry for each external
3667 symbol. */
3668 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3669 sym_hash = bfd_alloc (abfd, amt);
3670 if (sym_hash == NULL)
3671 goto error_free_sym;
3672 elf_sym_hashes (abfd) = sym_hash;
3675 if (dynamic)
3677 /* Read in any version definitions. */
3678 if (!_bfd_elf_slurp_version_tables (abfd,
3679 info->default_imported_symver))
3680 goto error_free_sym;
3682 /* Read in the symbol versions, but don't bother to convert them
3683 to internal format. */
3684 if (elf_dynversym (abfd) != 0)
3686 Elf_Internal_Shdr *versymhdr;
3688 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3689 extversym = bfd_malloc (versymhdr->sh_size);
3690 if (extversym == NULL)
3691 goto error_free_sym;
3692 amt = versymhdr->sh_size;
3693 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3694 || bfd_bread (extversym, amt, abfd) != amt)
3695 goto error_free_vers;
3699 /* If we are loading an as-needed shared lib, save the symbol table
3700 state before we start adding symbols. If the lib turns out
3701 to be unneeded, restore the state. */
3702 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3704 unsigned int i;
3705 size_t entsize;
3707 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3709 struct bfd_hash_entry *p;
3710 struct elf_link_hash_entry *h;
3712 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3714 h = (struct elf_link_hash_entry *) p;
3715 entsize += htab->root.table.entsize;
3716 if (h->root.type == bfd_link_hash_warning)
3717 entsize += htab->root.table.entsize;
3721 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3722 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3723 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3724 if (old_tab == NULL)
3725 goto error_free_vers;
3727 /* Remember the current objalloc pointer, so that all mem for
3728 symbols added can later be reclaimed. */
3729 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3730 if (alloc_mark == NULL)
3731 goto error_free_vers;
3733 /* Make a special call to the linker "notice" function to
3734 tell it that we are about to handle an as-needed lib. */
3735 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3736 notice_as_needed))
3737 goto error_free_vers;
3739 /* Clone the symbol table and sym hashes. Remember some
3740 pointers into the symbol table, and dynamic symbol count. */
3741 old_hash = (char *) old_tab + tabsize;
3742 old_ent = (char *) old_hash + hashsize;
3743 memcpy (old_tab, htab->root.table.table, tabsize);
3744 memcpy (old_hash, sym_hash, hashsize);
3745 old_undefs = htab->root.undefs;
3746 old_undefs_tail = htab->root.undefs_tail;
3747 old_table = htab->root.table.table;
3748 old_size = htab->root.table.size;
3749 old_count = htab->root.table.count;
3750 old_dynsymcount = htab->dynsymcount;
3752 for (i = 0; i < htab->root.table.size; i++)
3754 struct bfd_hash_entry *p;
3755 struct elf_link_hash_entry *h;
3757 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3759 memcpy (old_ent, p, htab->root.table.entsize);
3760 old_ent = (char *) old_ent + htab->root.table.entsize;
3761 h = (struct elf_link_hash_entry *) p;
3762 if (h->root.type == bfd_link_hash_warning)
3764 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3765 old_ent = (char *) old_ent + htab->root.table.entsize;
3771 weaks = NULL;
3772 ever = extversym != NULL ? extversym + extsymoff : NULL;
3773 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3774 isym < isymend;
3775 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3777 int bind;
3778 bfd_vma value;
3779 asection *sec, *new_sec;
3780 flagword flags;
3781 const char *name;
3782 struct elf_link_hash_entry *h;
3783 bfd_boolean definition;
3784 bfd_boolean size_change_ok;
3785 bfd_boolean type_change_ok;
3786 bfd_boolean new_weakdef;
3787 bfd_boolean override;
3788 bfd_boolean common;
3789 unsigned int old_alignment;
3790 bfd *old_bfd;
3792 override = FALSE;
3794 flags = BSF_NO_FLAGS;
3795 sec = NULL;
3796 value = isym->st_value;
3797 *sym_hash = NULL;
3798 common = bed->common_definition (isym);
3800 bind = ELF_ST_BIND (isym->st_info);
3801 if (bind == STB_LOCAL)
3803 /* This should be impossible, since ELF requires that all
3804 global symbols follow all local symbols, and that sh_info
3805 point to the first global symbol. Unfortunately, Irix 5
3806 screws this up. */
3807 continue;
3809 else if (bind == STB_GLOBAL)
3811 if (isym->st_shndx != SHN_UNDEF && !common)
3812 flags = BSF_GLOBAL;
3814 else if (bind == STB_WEAK)
3815 flags = BSF_WEAK;
3816 else
3818 /* Leave it up to the processor backend. */
3821 if (isym->st_shndx == SHN_UNDEF)
3822 sec = bfd_und_section_ptr;
3823 else if (isym->st_shndx < SHN_LORESERVE
3824 || isym->st_shndx > SHN_HIRESERVE)
3826 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3827 if (sec == NULL)
3828 sec = bfd_abs_section_ptr;
3829 else if (sec->kept_section)
3831 /* Symbols from discarded section are undefined. We keep
3832 its visibility. */
3833 sec = bfd_und_section_ptr;
3834 isym->st_shndx = SHN_UNDEF;
3836 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3837 value -= sec->vma;
3839 else if (isym->st_shndx == SHN_ABS)
3840 sec = bfd_abs_section_ptr;
3841 else if (isym->st_shndx == SHN_COMMON)
3843 sec = bfd_com_section_ptr;
3844 /* What ELF calls the size we call the value. What ELF
3845 calls the value we call the alignment. */
3846 value = isym->st_size;
3848 else
3850 /* Leave it up to the processor backend. */
3853 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3854 isym->st_name);
3855 if (name == NULL)
3856 goto error_free_vers;
3858 if (isym->st_shndx == SHN_COMMON
3859 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3860 && !info->relocatable)
3862 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3864 if (tcomm == NULL)
3866 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3867 (SEC_ALLOC
3868 | SEC_IS_COMMON
3869 | SEC_LINKER_CREATED
3870 | SEC_THREAD_LOCAL));
3871 if (tcomm == NULL)
3872 goto error_free_vers;
3874 sec = tcomm;
3876 else if (bed->elf_add_symbol_hook)
3878 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3879 &sec, &value))
3880 goto error_free_vers;
3882 /* The hook function sets the name to NULL if this symbol
3883 should be skipped for some reason. */
3884 if (name == NULL)
3885 continue;
3888 /* Sanity check that all possibilities were handled. */
3889 if (sec == NULL)
3891 bfd_set_error (bfd_error_bad_value);
3892 goto error_free_vers;
3895 if (bfd_is_und_section (sec)
3896 || bfd_is_com_section (sec))
3897 definition = FALSE;
3898 else
3899 definition = TRUE;
3901 size_change_ok = FALSE;
3902 type_change_ok = bed->type_change_ok;
3903 old_alignment = 0;
3904 old_bfd = NULL;
3905 new_sec = sec;
3907 if (is_elf_hash_table (htab))
3909 Elf_Internal_Versym iver;
3910 unsigned int vernum = 0;
3911 bfd_boolean skip;
3913 if (ever == NULL)
3915 if (info->default_imported_symver)
3916 /* Use the default symbol version created earlier. */
3917 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3918 else
3919 iver.vs_vers = 0;
3921 else
3922 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3924 vernum = iver.vs_vers & VERSYM_VERSION;
3926 /* If this is a hidden symbol, or if it is not version
3927 1, we append the version name to the symbol name.
3928 However, we do not modify a non-hidden absolute symbol
3929 if it is not a function, because it might be the version
3930 symbol itself. FIXME: What if it isn't? */
3931 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3932 || (vernum > 1
3933 && (!bfd_is_abs_section (sec)
3934 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
3936 const char *verstr;
3937 size_t namelen, verlen, newlen;
3938 char *newname, *p;
3940 if (isym->st_shndx != SHN_UNDEF)
3942 if (vernum > elf_tdata (abfd)->cverdefs)
3943 verstr = NULL;
3944 else if (vernum > 1)
3945 verstr =
3946 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3947 else
3948 verstr = "";
3950 if (verstr == NULL)
3952 (*_bfd_error_handler)
3953 (_("%B: %s: invalid version %u (max %d)"),
3954 abfd, name, vernum,
3955 elf_tdata (abfd)->cverdefs);
3956 bfd_set_error (bfd_error_bad_value);
3957 goto error_free_vers;
3960 else
3962 /* We cannot simply test for the number of
3963 entries in the VERNEED section since the
3964 numbers for the needed versions do not start
3965 at 0. */
3966 Elf_Internal_Verneed *t;
3968 verstr = NULL;
3969 for (t = elf_tdata (abfd)->verref;
3970 t != NULL;
3971 t = t->vn_nextref)
3973 Elf_Internal_Vernaux *a;
3975 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3977 if (a->vna_other == vernum)
3979 verstr = a->vna_nodename;
3980 break;
3983 if (a != NULL)
3984 break;
3986 if (verstr == NULL)
3988 (*_bfd_error_handler)
3989 (_("%B: %s: invalid needed version %d"),
3990 abfd, name, vernum);
3991 bfd_set_error (bfd_error_bad_value);
3992 goto error_free_vers;
3996 namelen = strlen (name);
3997 verlen = strlen (verstr);
3998 newlen = namelen + verlen + 2;
3999 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4000 && isym->st_shndx != SHN_UNDEF)
4001 ++newlen;
4003 newname = bfd_hash_allocate (&htab->root.table, newlen);
4004 if (newname == NULL)
4005 goto error_free_vers;
4006 memcpy (newname, name, namelen);
4007 p = newname + namelen;
4008 *p++ = ELF_VER_CHR;
4009 /* If this is a defined non-hidden version symbol,
4010 we add another @ to the name. This indicates the
4011 default version of the symbol. */
4012 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4013 && isym->st_shndx != SHN_UNDEF)
4014 *p++ = ELF_VER_CHR;
4015 memcpy (p, verstr, verlen + 1);
4017 name = newname;
4020 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4021 &value, &old_alignment,
4022 sym_hash, &skip, &override,
4023 &type_change_ok, &size_change_ok))
4024 goto error_free_vers;
4026 if (skip)
4027 continue;
4029 if (override)
4030 definition = FALSE;
4032 h = *sym_hash;
4033 while (h->root.type == bfd_link_hash_indirect
4034 || h->root.type == bfd_link_hash_warning)
4035 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4037 /* Remember the old alignment if this is a common symbol, so
4038 that we don't reduce the alignment later on. We can't
4039 check later, because _bfd_generic_link_add_one_symbol
4040 will set a default for the alignment which we want to
4041 override. We also remember the old bfd where the existing
4042 definition comes from. */
4043 switch (h->root.type)
4045 default:
4046 break;
4048 case bfd_link_hash_defined:
4049 case bfd_link_hash_defweak:
4050 old_bfd = h->root.u.def.section->owner;
4051 break;
4053 case bfd_link_hash_common:
4054 old_bfd = h->root.u.c.p->section->owner;
4055 old_alignment = h->root.u.c.p->alignment_power;
4056 break;
4059 if (elf_tdata (abfd)->verdef != NULL
4060 && ! override
4061 && vernum > 1
4062 && definition)
4063 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4066 if (! (_bfd_generic_link_add_one_symbol
4067 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4068 (struct bfd_link_hash_entry **) sym_hash)))
4069 goto error_free_vers;
4071 h = *sym_hash;
4072 while (h->root.type == bfd_link_hash_indirect
4073 || h->root.type == bfd_link_hash_warning)
4074 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4075 *sym_hash = h;
4077 new_weakdef = FALSE;
4078 if (dynamic
4079 && definition
4080 && (flags & BSF_WEAK) != 0
4081 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4082 && is_elf_hash_table (htab)
4083 && h->u.weakdef == NULL)
4085 /* Keep a list of all weak defined non function symbols from
4086 a dynamic object, using the weakdef field. Later in this
4087 function we will set the weakdef field to the correct
4088 value. We only put non-function symbols from dynamic
4089 objects on this list, because that happens to be the only
4090 time we need to know the normal symbol corresponding to a
4091 weak symbol, and the information is time consuming to
4092 figure out. If the weakdef field is not already NULL,
4093 then this symbol was already defined by some previous
4094 dynamic object, and we will be using that previous
4095 definition anyhow. */
4097 h->u.weakdef = weaks;
4098 weaks = h;
4099 new_weakdef = TRUE;
4102 /* Set the alignment of a common symbol. */
4103 if ((common || bfd_is_com_section (sec))
4104 && h->root.type == bfd_link_hash_common)
4106 unsigned int align;
4108 if (common)
4109 align = bfd_log2 (isym->st_value);
4110 else
4112 /* The new symbol is a common symbol in a shared object.
4113 We need to get the alignment from the section. */
4114 align = new_sec->alignment_power;
4116 if (align > old_alignment
4117 /* Permit an alignment power of zero if an alignment of one
4118 is specified and no other alignments have been specified. */
4119 || (isym->st_value == 1 && old_alignment == 0))
4120 h->root.u.c.p->alignment_power = align;
4121 else
4122 h->root.u.c.p->alignment_power = old_alignment;
4125 if (is_elf_hash_table (htab))
4127 bfd_boolean dynsym;
4129 /* Check the alignment when a common symbol is involved. This
4130 can change when a common symbol is overridden by a normal
4131 definition or a common symbol is ignored due to the old
4132 normal definition. We need to make sure the maximum
4133 alignment is maintained. */
4134 if ((old_alignment || common)
4135 && h->root.type != bfd_link_hash_common)
4137 unsigned int common_align;
4138 unsigned int normal_align;
4139 unsigned int symbol_align;
4140 bfd *normal_bfd;
4141 bfd *common_bfd;
4143 symbol_align = ffs (h->root.u.def.value) - 1;
4144 if (h->root.u.def.section->owner != NULL
4145 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4147 normal_align = h->root.u.def.section->alignment_power;
4148 if (normal_align > symbol_align)
4149 normal_align = symbol_align;
4151 else
4152 normal_align = symbol_align;
4154 if (old_alignment)
4156 common_align = old_alignment;
4157 common_bfd = old_bfd;
4158 normal_bfd = abfd;
4160 else
4162 common_align = bfd_log2 (isym->st_value);
4163 common_bfd = abfd;
4164 normal_bfd = old_bfd;
4167 if (normal_align < common_align)
4169 /* PR binutils/2735 */
4170 if (normal_bfd == NULL)
4171 (*_bfd_error_handler)
4172 (_("Warning: alignment %u of common symbol `%s' in %B"
4173 " is greater than the alignment (%u) of its section %A"),
4174 common_bfd, h->root.u.def.section,
4175 1 << common_align, name, 1 << normal_align);
4176 else
4177 (*_bfd_error_handler)
4178 (_("Warning: alignment %u of symbol `%s' in %B"
4179 " is smaller than %u in %B"),
4180 normal_bfd, common_bfd,
4181 1 << normal_align, name, 1 << common_align);
4185 /* Remember the symbol size if it isn't undefined. */
4186 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4187 && (definition || h->size == 0))
4189 if (h->size != 0
4190 && h->size != isym->st_size
4191 && ! size_change_ok)
4192 (*_bfd_error_handler)
4193 (_("Warning: size of symbol `%s' changed"
4194 " from %lu in %B to %lu in %B"),
4195 old_bfd, abfd,
4196 name, (unsigned long) h->size,
4197 (unsigned long) isym->st_size);
4199 h->size = isym->st_size;
4202 /* If this is a common symbol, then we always want H->SIZE
4203 to be the size of the common symbol. The code just above
4204 won't fix the size if a common symbol becomes larger. We
4205 don't warn about a size change here, because that is
4206 covered by --warn-common. Allow changed between different
4207 function types. */
4208 if (h->root.type == bfd_link_hash_common)
4209 h->size = h->root.u.c.size;
4211 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4212 && (definition || h->type == STT_NOTYPE))
4214 if (h->type != STT_NOTYPE
4215 && h->type != ELF_ST_TYPE (isym->st_info)
4216 && ! type_change_ok)
4217 (*_bfd_error_handler)
4218 (_("Warning: type of symbol `%s' changed"
4219 " from %d to %d in %B"),
4220 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4222 h->type = ELF_ST_TYPE (isym->st_info);
4225 /* If st_other has a processor-specific meaning, specific
4226 code might be needed here. We never merge the visibility
4227 attribute with the one from a dynamic object. */
4228 if (bed->elf_backend_merge_symbol_attribute)
4229 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4230 dynamic);
4232 /* If this symbol has default visibility and the user has requested
4233 we not re-export it, then mark it as hidden. */
4234 if (definition && !dynamic
4235 && (abfd->no_export
4236 || (abfd->my_archive && abfd->my_archive->no_export))
4237 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4238 isym->st_other = (STV_HIDDEN
4239 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4241 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4243 unsigned char hvis, symvis, other, nvis;
4245 /* Only merge the visibility. Leave the remainder of the
4246 st_other field to elf_backend_merge_symbol_attribute. */
4247 other = h->other & ~ELF_ST_VISIBILITY (-1);
4249 /* Combine visibilities, using the most constraining one. */
4250 hvis = ELF_ST_VISIBILITY (h->other);
4251 symvis = ELF_ST_VISIBILITY (isym->st_other);
4252 if (! hvis)
4253 nvis = symvis;
4254 else if (! symvis)
4255 nvis = hvis;
4256 else
4257 nvis = hvis < symvis ? hvis : symvis;
4259 h->other = other | nvis;
4262 /* Set a flag in the hash table entry indicating the type of
4263 reference or definition we just found. Keep a count of
4264 the number of dynamic symbols we find. A dynamic symbol
4265 is one which is referenced or defined by both a regular
4266 object and a shared object. */
4267 dynsym = FALSE;
4268 if (! dynamic)
4270 if (! definition)
4272 h->ref_regular = 1;
4273 if (bind != STB_WEAK)
4274 h->ref_regular_nonweak = 1;
4276 else
4277 h->def_regular = 1;
4278 if (! info->executable
4279 || h->def_dynamic
4280 || h->ref_dynamic)
4281 dynsym = TRUE;
4283 else
4285 if (! definition)
4286 h->ref_dynamic = 1;
4287 else
4288 h->def_dynamic = 1;
4289 if (h->def_regular
4290 || h->ref_regular
4291 || (h->u.weakdef != NULL
4292 && ! new_weakdef
4293 && h->u.weakdef->dynindx != -1))
4294 dynsym = TRUE;
4297 if (definition && (sec->flags & SEC_DEBUGGING))
4299 /* We don't want to make debug symbol dynamic. */
4300 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4301 dynsym = FALSE;
4304 /* Check to see if we need to add an indirect symbol for
4305 the default name. */
4306 if (definition || h->root.type == bfd_link_hash_common)
4307 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4308 &sec, &value, &dynsym,
4309 override))
4310 goto error_free_vers;
4312 if (definition && !dynamic)
4314 char *p = strchr (name, ELF_VER_CHR);
4315 if (p != NULL && p[1] != ELF_VER_CHR)
4317 /* Queue non-default versions so that .symver x, x@FOO
4318 aliases can be checked. */
4319 if (!nondeflt_vers)
4321 amt = ((isymend - isym + 1)
4322 * sizeof (struct elf_link_hash_entry *));
4323 nondeflt_vers = bfd_malloc (amt);
4324 if (!nondeflt_vers)
4325 goto error_free_vers;
4327 nondeflt_vers[nondeflt_vers_cnt++] = h;
4331 if (dynsym && h->dynindx == -1)
4333 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4334 goto error_free_vers;
4335 if (h->u.weakdef != NULL
4336 && ! new_weakdef
4337 && h->u.weakdef->dynindx == -1)
4339 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4340 goto error_free_vers;
4343 else if (dynsym && h->dynindx != -1)
4344 /* If the symbol already has a dynamic index, but
4345 visibility says it should not be visible, turn it into
4346 a local symbol. */
4347 switch (ELF_ST_VISIBILITY (h->other))
4349 case STV_INTERNAL:
4350 case STV_HIDDEN:
4351 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4352 dynsym = FALSE;
4353 break;
4356 if (!add_needed
4357 && definition
4358 && dynsym
4359 && h->ref_regular)
4361 int ret;
4362 const char *soname = elf_dt_name (abfd);
4364 /* A symbol from a library loaded via DT_NEEDED of some
4365 other library is referenced by a regular object.
4366 Add a DT_NEEDED entry for it. Issue an error if
4367 --no-add-needed is used. */
4368 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4370 (*_bfd_error_handler)
4371 (_("%s: invalid DSO for symbol `%s' definition"),
4372 abfd, name);
4373 bfd_set_error (bfd_error_bad_value);
4374 goto error_free_vers;
4377 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4379 add_needed = TRUE;
4380 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4381 if (ret < 0)
4382 goto error_free_vers;
4384 BFD_ASSERT (ret == 0);
4389 if (extversym != NULL)
4391 free (extversym);
4392 extversym = NULL;
4395 if (isymbuf != NULL)
4397 free (isymbuf);
4398 isymbuf = NULL;
4401 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4403 unsigned int i;
4405 /* Restore the symbol table. */
4406 if (bed->as_needed_cleanup)
4407 (*bed->as_needed_cleanup) (abfd, info);
4408 old_hash = (char *) old_tab + tabsize;
4409 old_ent = (char *) old_hash + hashsize;
4410 sym_hash = elf_sym_hashes (abfd);
4411 htab->root.table.table = old_table;
4412 htab->root.table.size = old_size;
4413 htab->root.table.count = old_count;
4414 memcpy (htab->root.table.table, old_tab, tabsize);
4415 memcpy (sym_hash, old_hash, hashsize);
4416 htab->root.undefs = old_undefs;
4417 htab->root.undefs_tail = old_undefs_tail;
4418 for (i = 0; i < htab->root.table.size; i++)
4420 struct bfd_hash_entry *p;
4421 struct elf_link_hash_entry *h;
4423 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4425 h = (struct elf_link_hash_entry *) p;
4426 if (h->root.type == bfd_link_hash_warning)
4427 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4428 if (h->dynindx >= old_dynsymcount)
4429 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4431 memcpy (p, old_ent, htab->root.table.entsize);
4432 old_ent = (char *) old_ent + htab->root.table.entsize;
4433 h = (struct elf_link_hash_entry *) p;
4434 if (h->root.type == bfd_link_hash_warning)
4436 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4437 old_ent = (char *) old_ent + htab->root.table.entsize;
4442 /* Make a special call to the linker "notice" function to
4443 tell it that symbols added for crefs may need to be removed. */
4444 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4445 notice_not_needed))
4446 goto error_free_vers;
4448 free (old_tab);
4449 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4450 alloc_mark);
4451 if (nondeflt_vers != NULL)
4452 free (nondeflt_vers);
4453 return TRUE;
4456 if (old_tab != NULL)
4458 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4459 notice_needed))
4460 goto error_free_vers;
4461 free (old_tab);
4462 old_tab = NULL;
4465 /* Now that all the symbols from this input file are created, handle
4466 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4467 if (nondeflt_vers != NULL)
4469 bfd_size_type cnt, symidx;
4471 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4473 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4474 char *shortname, *p;
4476 p = strchr (h->root.root.string, ELF_VER_CHR);
4477 if (p == NULL
4478 || (h->root.type != bfd_link_hash_defined
4479 && h->root.type != bfd_link_hash_defweak))
4480 continue;
4482 amt = p - h->root.root.string;
4483 shortname = bfd_malloc (amt + 1);
4484 if (!shortname)
4485 goto error_free_vers;
4486 memcpy (shortname, h->root.root.string, amt);
4487 shortname[amt] = '\0';
4489 hi = (struct elf_link_hash_entry *)
4490 bfd_link_hash_lookup (&htab->root, shortname,
4491 FALSE, FALSE, FALSE);
4492 if (hi != NULL
4493 && hi->root.type == h->root.type
4494 && hi->root.u.def.value == h->root.u.def.value
4495 && hi->root.u.def.section == h->root.u.def.section)
4497 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4498 hi->root.type = bfd_link_hash_indirect;
4499 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4500 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4501 sym_hash = elf_sym_hashes (abfd);
4502 if (sym_hash)
4503 for (symidx = 0; symidx < extsymcount; ++symidx)
4504 if (sym_hash[symidx] == hi)
4506 sym_hash[symidx] = h;
4507 break;
4510 free (shortname);
4512 free (nondeflt_vers);
4513 nondeflt_vers = NULL;
4516 /* Now set the weakdefs field correctly for all the weak defined
4517 symbols we found. The only way to do this is to search all the
4518 symbols. Since we only need the information for non functions in
4519 dynamic objects, that's the only time we actually put anything on
4520 the list WEAKS. We need this information so that if a regular
4521 object refers to a symbol defined weakly in a dynamic object, the
4522 real symbol in the dynamic object is also put in the dynamic
4523 symbols; we also must arrange for both symbols to point to the
4524 same memory location. We could handle the general case of symbol
4525 aliasing, but a general symbol alias can only be generated in
4526 assembler code, handling it correctly would be very time
4527 consuming, and other ELF linkers don't handle general aliasing
4528 either. */
4529 if (weaks != NULL)
4531 struct elf_link_hash_entry **hpp;
4532 struct elf_link_hash_entry **hppend;
4533 struct elf_link_hash_entry **sorted_sym_hash;
4534 struct elf_link_hash_entry *h;
4535 size_t sym_count;
4537 /* Since we have to search the whole symbol list for each weak
4538 defined symbol, search time for N weak defined symbols will be
4539 O(N^2). Binary search will cut it down to O(NlogN). */
4540 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4541 sorted_sym_hash = bfd_malloc (amt);
4542 if (sorted_sym_hash == NULL)
4543 goto error_return;
4544 sym_hash = sorted_sym_hash;
4545 hpp = elf_sym_hashes (abfd);
4546 hppend = hpp + extsymcount;
4547 sym_count = 0;
4548 for (; hpp < hppend; hpp++)
4550 h = *hpp;
4551 if (h != NULL
4552 && h->root.type == bfd_link_hash_defined
4553 && !bed->is_function_type (h->type))
4555 *sym_hash = h;
4556 sym_hash++;
4557 sym_count++;
4561 qsort (sorted_sym_hash, sym_count,
4562 sizeof (struct elf_link_hash_entry *),
4563 elf_sort_symbol);
4565 while (weaks != NULL)
4567 struct elf_link_hash_entry *hlook;
4568 asection *slook;
4569 bfd_vma vlook;
4570 long ilook;
4571 size_t i, j, idx;
4573 hlook = weaks;
4574 weaks = hlook->u.weakdef;
4575 hlook->u.weakdef = NULL;
4577 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4578 || hlook->root.type == bfd_link_hash_defweak
4579 || hlook->root.type == bfd_link_hash_common
4580 || hlook->root.type == bfd_link_hash_indirect);
4581 slook = hlook->root.u.def.section;
4582 vlook = hlook->root.u.def.value;
4584 ilook = -1;
4585 i = 0;
4586 j = sym_count;
4587 while (i < j)
4589 bfd_signed_vma vdiff;
4590 idx = (i + j) / 2;
4591 h = sorted_sym_hash [idx];
4592 vdiff = vlook - h->root.u.def.value;
4593 if (vdiff < 0)
4594 j = idx;
4595 else if (vdiff > 0)
4596 i = idx + 1;
4597 else
4599 long sdiff = slook->id - h->root.u.def.section->id;
4600 if (sdiff < 0)
4601 j = idx;
4602 else if (sdiff > 0)
4603 i = idx + 1;
4604 else
4606 ilook = idx;
4607 break;
4612 /* We didn't find a value/section match. */
4613 if (ilook == -1)
4614 continue;
4616 for (i = ilook; i < sym_count; i++)
4618 h = sorted_sym_hash [i];
4620 /* Stop if value or section doesn't match. */
4621 if (h->root.u.def.value != vlook
4622 || h->root.u.def.section != slook)
4623 break;
4624 else if (h != hlook)
4626 hlook->u.weakdef = h;
4628 /* If the weak definition is in the list of dynamic
4629 symbols, make sure the real definition is put
4630 there as well. */
4631 if (hlook->dynindx != -1 && h->dynindx == -1)
4633 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4634 goto error_return;
4637 /* If the real definition is in the list of dynamic
4638 symbols, make sure the weak definition is put
4639 there as well. If we don't do this, then the
4640 dynamic loader might not merge the entries for the
4641 real definition and the weak definition. */
4642 if (h->dynindx != -1 && hlook->dynindx == -1)
4644 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4645 goto error_return;
4647 break;
4652 free (sorted_sym_hash);
4655 if (bed->check_directives)
4656 (*bed->check_directives) (abfd, info);
4658 /* If this object is the same format as the output object, and it is
4659 not a shared library, then let the backend look through the
4660 relocs.
4662 This is required to build global offset table entries and to
4663 arrange for dynamic relocs. It is not required for the
4664 particular common case of linking non PIC code, even when linking
4665 against shared libraries, but unfortunately there is no way of
4666 knowing whether an object file has been compiled PIC or not.
4667 Looking through the relocs is not particularly time consuming.
4668 The problem is that we must either (1) keep the relocs in memory,
4669 which causes the linker to require additional runtime memory or
4670 (2) read the relocs twice from the input file, which wastes time.
4671 This would be a good case for using mmap.
4673 I have no idea how to handle linking PIC code into a file of a
4674 different format. It probably can't be done. */
4675 if (! dynamic
4676 && is_elf_hash_table (htab)
4677 && bed->check_relocs != NULL
4678 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4680 asection *o;
4682 for (o = abfd->sections; o != NULL; o = o->next)
4684 Elf_Internal_Rela *internal_relocs;
4685 bfd_boolean ok;
4687 if ((o->flags & SEC_RELOC) == 0
4688 || o->reloc_count == 0
4689 || ((info->strip == strip_all || info->strip == strip_debugger)
4690 && (o->flags & SEC_DEBUGGING) != 0)
4691 || bfd_is_abs_section (o->output_section))
4692 continue;
4694 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4695 info->keep_memory);
4696 if (internal_relocs == NULL)
4697 goto error_return;
4699 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4701 if (elf_section_data (o)->relocs != internal_relocs)
4702 free (internal_relocs);
4704 if (! ok)
4705 goto error_return;
4709 /* If this is a non-traditional link, try to optimize the handling
4710 of the .stab/.stabstr sections. */
4711 if (! dynamic
4712 && ! info->traditional_format
4713 && is_elf_hash_table (htab)
4714 && (info->strip != strip_all && info->strip != strip_debugger))
4716 asection *stabstr;
4718 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4719 if (stabstr != NULL)
4721 bfd_size_type string_offset = 0;
4722 asection *stab;
4724 for (stab = abfd->sections; stab; stab = stab->next)
4725 if (CONST_STRNEQ (stab->name, ".stab")
4726 && (!stab->name[5] ||
4727 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4728 && (stab->flags & SEC_MERGE) == 0
4729 && !bfd_is_abs_section (stab->output_section))
4731 struct bfd_elf_section_data *secdata;
4733 secdata = elf_section_data (stab);
4734 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4735 stabstr, &secdata->sec_info,
4736 &string_offset))
4737 goto error_return;
4738 if (secdata->sec_info)
4739 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4744 if (is_elf_hash_table (htab) && add_needed)
4746 /* Add this bfd to the loaded list. */
4747 struct elf_link_loaded_list *n;
4749 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4750 if (n == NULL)
4751 goto error_return;
4752 n->abfd = abfd;
4753 n->next = htab->loaded;
4754 htab->loaded = n;
4757 return TRUE;
4759 error_free_vers:
4760 if (old_tab != NULL)
4761 free (old_tab);
4762 if (nondeflt_vers != NULL)
4763 free (nondeflt_vers);
4764 if (extversym != NULL)
4765 free (extversym);
4766 error_free_sym:
4767 if (isymbuf != NULL)
4768 free (isymbuf);
4769 error_return:
4770 return FALSE;
4773 /* Return the linker hash table entry of a symbol that might be
4774 satisfied by an archive symbol. Return -1 on error. */
4776 struct elf_link_hash_entry *
4777 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4778 struct bfd_link_info *info,
4779 const char *name)
4781 struct elf_link_hash_entry *h;
4782 char *p, *copy;
4783 size_t len, first;
4785 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4786 if (h != NULL)
4787 return h;
4789 /* If this is a default version (the name contains @@), look up the
4790 symbol again with only one `@' as well as without the version.
4791 The effect is that references to the symbol with and without the
4792 version will be matched by the default symbol in the archive. */
4794 p = strchr (name, ELF_VER_CHR);
4795 if (p == NULL || p[1] != ELF_VER_CHR)
4796 return h;
4798 /* First check with only one `@'. */
4799 len = strlen (name);
4800 copy = bfd_alloc (abfd, len);
4801 if (copy == NULL)
4802 return (struct elf_link_hash_entry *) 0 - 1;
4804 first = p - name + 1;
4805 memcpy (copy, name, first);
4806 memcpy (copy + first, name + first + 1, len - first);
4808 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4809 if (h == NULL)
4811 /* We also need to check references to the symbol without the
4812 version. */
4813 copy[first - 1] = '\0';
4814 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4815 FALSE, FALSE, FALSE);
4818 bfd_release (abfd, copy);
4819 return h;
4822 /* Add symbols from an ELF archive file to the linker hash table. We
4823 don't use _bfd_generic_link_add_archive_symbols because of a
4824 problem which arises on UnixWare. The UnixWare libc.so is an
4825 archive which includes an entry libc.so.1 which defines a bunch of
4826 symbols. The libc.so archive also includes a number of other
4827 object files, which also define symbols, some of which are the same
4828 as those defined in libc.so.1. Correct linking requires that we
4829 consider each object file in turn, and include it if it defines any
4830 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4831 this; it looks through the list of undefined symbols, and includes
4832 any object file which defines them. When this algorithm is used on
4833 UnixWare, it winds up pulling in libc.so.1 early and defining a
4834 bunch of symbols. This means that some of the other objects in the
4835 archive are not included in the link, which is incorrect since they
4836 precede libc.so.1 in the archive.
4838 Fortunately, ELF archive handling is simpler than that done by
4839 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4840 oddities. In ELF, if we find a symbol in the archive map, and the
4841 symbol is currently undefined, we know that we must pull in that
4842 object file.
4844 Unfortunately, we do have to make multiple passes over the symbol
4845 table until nothing further is resolved. */
4847 static bfd_boolean
4848 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4850 symindex c;
4851 bfd_boolean *defined = NULL;
4852 bfd_boolean *included = NULL;
4853 carsym *symdefs;
4854 bfd_boolean loop;
4855 bfd_size_type amt;
4856 const struct elf_backend_data *bed;
4857 struct elf_link_hash_entry * (*archive_symbol_lookup)
4858 (bfd *, struct bfd_link_info *, const char *);
4860 if (! bfd_has_map (abfd))
4862 /* An empty archive is a special case. */
4863 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4864 return TRUE;
4865 bfd_set_error (bfd_error_no_armap);
4866 return FALSE;
4869 /* Keep track of all symbols we know to be already defined, and all
4870 files we know to be already included. This is to speed up the
4871 second and subsequent passes. */
4872 c = bfd_ardata (abfd)->symdef_count;
4873 if (c == 0)
4874 return TRUE;
4875 amt = c;
4876 amt *= sizeof (bfd_boolean);
4877 defined = bfd_zmalloc (amt);
4878 included = bfd_zmalloc (amt);
4879 if (defined == NULL || included == NULL)
4880 goto error_return;
4882 symdefs = bfd_ardata (abfd)->symdefs;
4883 bed = get_elf_backend_data (abfd);
4884 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4888 file_ptr last;
4889 symindex i;
4890 carsym *symdef;
4891 carsym *symdefend;
4893 loop = FALSE;
4894 last = -1;
4896 symdef = symdefs;
4897 symdefend = symdef + c;
4898 for (i = 0; symdef < symdefend; symdef++, i++)
4900 struct elf_link_hash_entry *h;
4901 bfd *element;
4902 struct bfd_link_hash_entry *undefs_tail;
4903 symindex mark;
4905 if (defined[i] || included[i])
4906 continue;
4907 if (symdef->file_offset == last)
4909 included[i] = TRUE;
4910 continue;
4913 h = archive_symbol_lookup (abfd, info, symdef->name);
4914 if (h == (struct elf_link_hash_entry *) 0 - 1)
4915 goto error_return;
4917 if (h == NULL)
4918 continue;
4920 if (h->root.type == bfd_link_hash_common)
4922 /* We currently have a common symbol. The archive map contains
4923 a reference to this symbol, so we may want to include it. We
4924 only want to include it however, if this archive element
4925 contains a definition of the symbol, not just another common
4926 declaration of it.
4928 Unfortunately some archivers (including GNU ar) will put
4929 declarations of common symbols into their archive maps, as
4930 well as real definitions, so we cannot just go by the archive
4931 map alone. Instead we must read in the element's symbol
4932 table and check that to see what kind of symbol definition
4933 this is. */
4934 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4935 continue;
4937 else if (h->root.type != bfd_link_hash_undefined)
4939 if (h->root.type != bfd_link_hash_undefweak)
4940 defined[i] = TRUE;
4941 continue;
4944 /* We need to include this archive member. */
4945 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4946 if (element == NULL)
4947 goto error_return;
4949 if (! bfd_check_format (element, bfd_object))
4950 goto error_return;
4952 /* Doublecheck that we have not included this object
4953 already--it should be impossible, but there may be
4954 something wrong with the archive. */
4955 if (element->archive_pass != 0)
4957 bfd_set_error (bfd_error_bad_value);
4958 goto error_return;
4960 element->archive_pass = 1;
4962 undefs_tail = info->hash->undefs_tail;
4964 if (! (*info->callbacks->add_archive_element) (info, element,
4965 symdef->name))
4966 goto error_return;
4967 if (! bfd_link_add_symbols (element, info))
4968 goto error_return;
4970 /* If there are any new undefined symbols, we need to make
4971 another pass through the archive in order to see whether
4972 they can be defined. FIXME: This isn't perfect, because
4973 common symbols wind up on undefs_tail and because an
4974 undefined symbol which is defined later on in this pass
4975 does not require another pass. This isn't a bug, but it
4976 does make the code less efficient than it could be. */
4977 if (undefs_tail != info->hash->undefs_tail)
4978 loop = TRUE;
4980 /* Look backward to mark all symbols from this object file
4981 which we have already seen in this pass. */
4982 mark = i;
4985 included[mark] = TRUE;
4986 if (mark == 0)
4987 break;
4988 --mark;
4990 while (symdefs[mark].file_offset == symdef->file_offset);
4992 /* We mark subsequent symbols from this object file as we go
4993 on through the loop. */
4994 last = symdef->file_offset;
4997 while (loop);
4999 free (defined);
5000 free (included);
5002 return TRUE;
5004 error_return:
5005 if (defined != NULL)
5006 free (defined);
5007 if (included != NULL)
5008 free (included);
5009 return FALSE;
5012 /* Given an ELF BFD, add symbols to the global hash table as
5013 appropriate. */
5015 bfd_boolean
5016 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5018 switch (bfd_get_format (abfd))
5020 case bfd_object:
5021 return elf_link_add_object_symbols (abfd, info);
5022 case bfd_archive:
5023 return elf_link_add_archive_symbols (abfd, info);
5024 default:
5025 bfd_set_error (bfd_error_wrong_format);
5026 return FALSE;
5030 struct hash_codes_info
5032 unsigned long *hashcodes;
5033 bfd_boolean error;
5036 /* This function will be called though elf_link_hash_traverse to store
5037 all hash value of the exported symbols in an array. */
5039 static bfd_boolean
5040 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5042 struct hash_codes_info *inf = data;
5043 const char *name;
5044 char *p;
5045 unsigned long ha;
5046 char *alc = NULL;
5048 if (h->root.type == bfd_link_hash_warning)
5049 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5051 /* Ignore indirect symbols. These are added by the versioning code. */
5052 if (h->dynindx == -1)
5053 return TRUE;
5055 name = h->root.root.string;
5056 p = strchr (name, ELF_VER_CHR);
5057 if (p != NULL)
5059 alc = bfd_malloc (p - name + 1);
5060 if (alc == NULL)
5062 inf->error = TRUE;
5063 return FALSE;
5065 memcpy (alc, name, p - name);
5066 alc[p - name] = '\0';
5067 name = alc;
5070 /* Compute the hash value. */
5071 ha = bfd_elf_hash (name);
5073 /* Store the found hash value in the array given as the argument. */
5074 *(inf->hashcodes)++ = ha;
5076 /* And store it in the struct so that we can put it in the hash table
5077 later. */
5078 h->u.elf_hash_value = ha;
5080 if (alc != NULL)
5081 free (alc);
5083 return TRUE;
5086 struct collect_gnu_hash_codes
5088 bfd *output_bfd;
5089 const struct elf_backend_data *bed;
5090 unsigned long int nsyms;
5091 unsigned long int maskbits;
5092 unsigned long int *hashcodes;
5093 unsigned long int *hashval;
5094 unsigned long int *indx;
5095 unsigned long int *counts;
5096 bfd_vma *bitmask;
5097 bfd_byte *contents;
5098 long int min_dynindx;
5099 unsigned long int bucketcount;
5100 unsigned long int symindx;
5101 long int local_indx;
5102 long int shift1, shift2;
5103 unsigned long int mask;
5104 bfd_boolean error;
5107 /* This function will be called though elf_link_hash_traverse to store
5108 all hash value of the exported symbols in an array. */
5110 static bfd_boolean
5111 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5113 struct collect_gnu_hash_codes *s = data;
5114 const char *name;
5115 char *p;
5116 unsigned long ha;
5117 char *alc = NULL;
5119 if (h->root.type == bfd_link_hash_warning)
5120 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5122 /* Ignore indirect symbols. These are added by the versioning code. */
5123 if (h->dynindx == -1)
5124 return TRUE;
5126 /* Ignore also local symbols and undefined symbols. */
5127 if (! (*s->bed->elf_hash_symbol) (h))
5128 return TRUE;
5130 name = h->root.root.string;
5131 p = strchr (name, ELF_VER_CHR);
5132 if (p != NULL)
5134 alc = bfd_malloc (p - name + 1);
5135 if (alc == NULL)
5137 s->error = TRUE;
5138 return FALSE;
5140 memcpy (alc, name, p - name);
5141 alc[p - name] = '\0';
5142 name = alc;
5145 /* Compute the hash value. */
5146 ha = bfd_elf_gnu_hash (name);
5148 /* Store the found hash value in the array for compute_bucket_count,
5149 and also for .dynsym reordering purposes. */
5150 s->hashcodes[s->nsyms] = ha;
5151 s->hashval[h->dynindx] = ha;
5152 ++s->nsyms;
5153 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5154 s->min_dynindx = h->dynindx;
5156 if (alc != NULL)
5157 free (alc);
5159 return TRUE;
5162 /* This function will be called though elf_link_hash_traverse to do
5163 final dynaminc symbol renumbering. */
5165 static bfd_boolean
5166 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5168 struct collect_gnu_hash_codes *s = data;
5169 unsigned long int bucket;
5170 unsigned long int val;
5172 if (h->root.type == bfd_link_hash_warning)
5173 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5175 /* Ignore indirect symbols. */
5176 if (h->dynindx == -1)
5177 return TRUE;
5179 /* Ignore also local symbols and undefined symbols. */
5180 if (! (*s->bed->elf_hash_symbol) (h))
5182 if (h->dynindx >= s->min_dynindx)
5183 h->dynindx = s->local_indx++;
5184 return TRUE;
5187 bucket = s->hashval[h->dynindx] % s->bucketcount;
5188 val = (s->hashval[h->dynindx] >> s->shift1)
5189 & ((s->maskbits >> s->shift1) - 1);
5190 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5191 s->bitmask[val]
5192 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5193 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5194 if (s->counts[bucket] == 1)
5195 /* Last element terminates the chain. */
5196 val |= 1;
5197 bfd_put_32 (s->output_bfd, val,
5198 s->contents + (s->indx[bucket] - s->symindx) * 4);
5199 --s->counts[bucket];
5200 h->dynindx = s->indx[bucket]++;
5201 return TRUE;
5204 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5206 bfd_boolean
5207 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5209 return !(h->forced_local
5210 || h->root.type == bfd_link_hash_undefined
5211 || h->root.type == bfd_link_hash_undefweak
5212 || ((h->root.type == bfd_link_hash_defined
5213 || h->root.type == bfd_link_hash_defweak)
5214 && h->root.u.def.section->output_section == NULL));
5217 /* Array used to determine the number of hash table buckets to use
5218 based on the number of symbols there are. If there are fewer than
5219 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5220 fewer than 37 we use 17 buckets, and so forth. We never use more
5221 than 32771 buckets. */
5223 static const size_t elf_buckets[] =
5225 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5226 16411, 32771, 0
5229 /* Compute bucket count for hashing table. We do not use a static set
5230 of possible tables sizes anymore. Instead we determine for all
5231 possible reasonable sizes of the table the outcome (i.e., the
5232 number of collisions etc) and choose the best solution. The
5233 weighting functions are not too simple to allow the table to grow
5234 without bounds. Instead one of the weighting factors is the size.
5235 Therefore the result is always a good payoff between few collisions
5236 (= short chain lengths) and table size. */
5237 static size_t
5238 compute_bucket_count (struct bfd_link_info *info,
5239 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5240 unsigned long int nsyms,
5241 int gnu_hash)
5243 size_t best_size = 0;
5244 unsigned long int i;
5246 /* We have a problem here. The following code to optimize the table
5247 size requires an integer type with more the 32 bits. If
5248 BFD_HOST_U_64_BIT is set we know about such a type. */
5249 #ifdef BFD_HOST_U_64_BIT
5250 if (info->optimize)
5252 size_t minsize;
5253 size_t maxsize;
5254 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5255 bfd *dynobj = elf_hash_table (info)->dynobj;
5256 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5257 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5258 unsigned long int *counts;
5259 bfd_size_type amt;
5261 /* Possible optimization parameters: if we have NSYMS symbols we say
5262 that the hashing table must at least have NSYMS/4 and at most
5263 2*NSYMS buckets. */
5264 minsize = nsyms / 4;
5265 if (minsize == 0)
5266 minsize = 1;
5267 best_size = maxsize = nsyms * 2;
5268 if (gnu_hash)
5270 if (minsize < 2)
5271 minsize = 2;
5272 if ((best_size & 31) == 0)
5273 ++best_size;
5276 /* Create array where we count the collisions in. We must use bfd_malloc
5277 since the size could be large. */
5278 amt = maxsize;
5279 amt *= sizeof (unsigned long int);
5280 counts = bfd_malloc (amt);
5281 if (counts == NULL)
5282 return 0;
5284 /* Compute the "optimal" size for the hash table. The criteria is a
5285 minimal chain length. The minor criteria is (of course) the size
5286 of the table. */
5287 for (i = minsize; i < maxsize; ++i)
5289 /* Walk through the array of hashcodes and count the collisions. */
5290 BFD_HOST_U_64_BIT max;
5291 unsigned long int j;
5292 unsigned long int fact;
5294 if (gnu_hash && (i & 31) == 0)
5295 continue;
5297 memset (counts, '\0', i * sizeof (unsigned long int));
5299 /* Determine how often each hash bucket is used. */
5300 for (j = 0; j < nsyms; ++j)
5301 ++counts[hashcodes[j] % i];
5303 /* For the weight function we need some information about the
5304 pagesize on the target. This is information need not be 100%
5305 accurate. Since this information is not available (so far) we
5306 define it here to a reasonable default value. If it is crucial
5307 to have a better value some day simply define this value. */
5308 # ifndef BFD_TARGET_PAGESIZE
5309 # define BFD_TARGET_PAGESIZE (4096)
5310 # endif
5312 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5313 and the chains. */
5314 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5316 # if 1
5317 /* Variant 1: optimize for short chains. We add the squares
5318 of all the chain lengths (which favors many small chain
5319 over a few long chains). */
5320 for (j = 0; j < i; ++j)
5321 max += counts[j] * counts[j];
5323 /* This adds penalties for the overall size of the table. */
5324 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5325 max *= fact * fact;
5326 # else
5327 /* Variant 2: Optimize a lot more for small table. Here we
5328 also add squares of the size but we also add penalties for
5329 empty slots (the +1 term). */
5330 for (j = 0; j < i; ++j)
5331 max += (1 + counts[j]) * (1 + counts[j]);
5333 /* The overall size of the table is considered, but not as
5334 strong as in variant 1, where it is squared. */
5335 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5336 max *= fact;
5337 # endif
5339 /* Compare with current best results. */
5340 if (max < best_chlen)
5342 best_chlen = max;
5343 best_size = i;
5347 free (counts);
5349 else
5350 #endif /* defined (BFD_HOST_U_64_BIT) */
5352 /* This is the fallback solution if no 64bit type is available or if we
5353 are not supposed to spend much time on optimizations. We select the
5354 bucket count using a fixed set of numbers. */
5355 for (i = 0; elf_buckets[i] != 0; i++)
5357 best_size = elf_buckets[i];
5358 if (nsyms < elf_buckets[i + 1])
5359 break;
5361 if (gnu_hash && best_size < 2)
5362 best_size = 2;
5365 return best_size;
5368 /* Set up the sizes and contents of the ELF dynamic sections. This is
5369 called by the ELF linker emulation before_allocation routine. We
5370 must set the sizes of the sections before the linker sets the
5371 addresses of the various sections. */
5373 bfd_boolean
5374 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5375 const char *soname,
5376 const char *rpath,
5377 const char *filter_shlib,
5378 const char * const *auxiliary_filters,
5379 struct bfd_link_info *info,
5380 asection **sinterpptr,
5381 struct bfd_elf_version_tree *verdefs)
5383 bfd_size_type soname_indx;
5384 bfd *dynobj;
5385 const struct elf_backend_data *bed;
5386 struct elf_assign_sym_version_info asvinfo;
5388 *sinterpptr = NULL;
5390 soname_indx = (bfd_size_type) -1;
5392 if (!is_elf_hash_table (info->hash))
5393 return TRUE;
5395 bed = get_elf_backend_data (output_bfd);
5396 if (info->execstack)
5397 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5398 else if (info->noexecstack)
5399 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5400 else
5402 bfd *inputobj;
5403 asection *notesec = NULL;
5404 int exec = 0;
5406 for (inputobj = info->input_bfds;
5407 inputobj;
5408 inputobj = inputobj->link_next)
5410 asection *s;
5412 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
5413 continue;
5414 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5415 if (s)
5417 if (s->flags & SEC_CODE)
5418 exec = PF_X;
5419 notesec = s;
5421 else if (bed->default_execstack)
5422 exec = PF_X;
5424 if (notesec)
5426 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5427 if (exec && info->relocatable
5428 && notesec->output_section != bfd_abs_section_ptr)
5429 notesec->output_section->flags |= SEC_CODE;
5433 /* Any syms created from now on start with -1 in
5434 got.refcount/offset and plt.refcount/offset. */
5435 elf_hash_table (info)->init_got_refcount
5436 = elf_hash_table (info)->init_got_offset;
5437 elf_hash_table (info)->init_plt_refcount
5438 = elf_hash_table (info)->init_plt_offset;
5440 /* The backend may have to create some sections regardless of whether
5441 we're dynamic or not. */
5442 if (bed->elf_backend_always_size_sections
5443 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5444 return FALSE;
5446 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5447 return FALSE;
5449 dynobj = elf_hash_table (info)->dynobj;
5451 /* If there were no dynamic objects in the link, there is nothing to
5452 do here. */
5453 if (dynobj == NULL)
5454 return TRUE;
5456 if (elf_hash_table (info)->dynamic_sections_created)
5458 struct elf_info_failed eif;
5459 struct elf_link_hash_entry *h;
5460 asection *dynstr;
5461 struct bfd_elf_version_tree *t;
5462 struct bfd_elf_version_expr *d;
5463 asection *s;
5464 bfd_boolean all_defined;
5466 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5467 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5469 if (soname != NULL)
5471 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5472 soname, TRUE);
5473 if (soname_indx == (bfd_size_type) -1
5474 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5475 return FALSE;
5478 if (info->symbolic)
5480 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5481 return FALSE;
5482 info->flags |= DF_SYMBOLIC;
5485 if (rpath != NULL)
5487 bfd_size_type indx;
5489 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5490 TRUE);
5491 if (indx == (bfd_size_type) -1
5492 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5493 return FALSE;
5495 if (info->new_dtags)
5497 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5498 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5499 return FALSE;
5503 if (filter_shlib != NULL)
5505 bfd_size_type indx;
5507 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5508 filter_shlib, TRUE);
5509 if (indx == (bfd_size_type) -1
5510 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5511 return FALSE;
5514 if (auxiliary_filters != NULL)
5516 const char * const *p;
5518 for (p = auxiliary_filters; *p != NULL; p++)
5520 bfd_size_type indx;
5522 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5523 *p, TRUE);
5524 if (indx == (bfd_size_type) -1
5525 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5526 return FALSE;
5530 eif.info = info;
5531 eif.verdefs = verdefs;
5532 eif.failed = FALSE;
5534 /* If we are supposed to export all symbols into the dynamic symbol
5535 table (this is not the normal case), then do so. */
5536 if (info->export_dynamic
5537 || (info->executable && info->dynamic))
5539 elf_link_hash_traverse (elf_hash_table (info),
5540 _bfd_elf_export_symbol,
5541 &eif);
5542 if (eif.failed)
5543 return FALSE;
5546 /* Make all global versions with definition. */
5547 for (t = verdefs; t != NULL; t = t->next)
5548 for (d = t->globals.list; d != NULL; d = d->next)
5549 if (!d->symver && d->symbol)
5551 const char *verstr, *name;
5552 size_t namelen, verlen, newlen;
5553 char *newname, *p;
5554 struct elf_link_hash_entry *newh;
5556 name = d->symbol;
5557 namelen = strlen (name);
5558 verstr = t->name;
5559 verlen = strlen (verstr);
5560 newlen = namelen + verlen + 3;
5562 newname = bfd_malloc (newlen);
5563 if (newname == NULL)
5564 return FALSE;
5565 memcpy (newname, name, namelen);
5567 /* Check the hidden versioned definition. */
5568 p = newname + namelen;
5569 *p++ = ELF_VER_CHR;
5570 memcpy (p, verstr, verlen + 1);
5571 newh = elf_link_hash_lookup (elf_hash_table (info),
5572 newname, FALSE, FALSE,
5573 FALSE);
5574 if (newh == NULL
5575 || (newh->root.type != bfd_link_hash_defined
5576 && newh->root.type != bfd_link_hash_defweak))
5578 /* Check the default versioned definition. */
5579 *p++ = ELF_VER_CHR;
5580 memcpy (p, verstr, verlen + 1);
5581 newh = elf_link_hash_lookup (elf_hash_table (info),
5582 newname, FALSE, FALSE,
5583 FALSE);
5585 free (newname);
5587 /* Mark this version if there is a definition and it is
5588 not defined in a shared object. */
5589 if (newh != NULL
5590 && !newh->def_dynamic
5591 && (newh->root.type == bfd_link_hash_defined
5592 || newh->root.type == bfd_link_hash_defweak))
5593 d->symver = 1;
5596 /* Attach all the symbols to their version information. */
5597 asvinfo.output_bfd = output_bfd;
5598 asvinfo.info = info;
5599 asvinfo.verdefs = verdefs;
5600 asvinfo.failed = FALSE;
5602 elf_link_hash_traverse (elf_hash_table (info),
5603 _bfd_elf_link_assign_sym_version,
5604 &asvinfo);
5605 if (asvinfo.failed)
5606 return FALSE;
5608 if (!info->allow_undefined_version)
5610 /* Check if all global versions have a definition. */
5611 all_defined = TRUE;
5612 for (t = verdefs; t != NULL; t = t->next)
5613 for (d = t->globals.list; d != NULL; d = d->next)
5614 if (!d->symver && !d->script)
5616 (*_bfd_error_handler)
5617 (_("%s: undefined version: %s"),
5618 d->pattern, t->name);
5619 all_defined = FALSE;
5622 if (!all_defined)
5624 bfd_set_error (bfd_error_bad_value);
5625 return FALSE;
5629 /* Find all symbols which were defined in a dynamic object and make
5630 the backend pick a reasonable value for them. */
5631 elf_link_hash_traverse (elf_hash_table (info),
5632 _bfd_elf_adjust_dynamic_symbol,
5633 &eif);
5634 if (eif.failed)
5635 return FALSE;
5637 /* Add some entries to the .dynamic section. We fill in some of the
5638 values later, in bfd_elf_final_link, but we must add the entries
5639 now so that we know the final size of the .dynamic section. */
5641 /* If there are initialization and/or finalization functions to
5642 call then add the corresponding DT_INIT/DT_FINI entries. */
5643 h = (info->init_function
5644 ? elf_link_hash_lookup (elf_hash_table (info),
5645 info->init_function, FALSE,
5646 FALSE, FALSE)
5647 : NULL);
5648 if (h != NULL
5649 && (h->ref_regular
5650 || h->def_regular))
5652 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5653 return FALSE;
5655 h = (info->fini_function
5656 ? elf_link_hash_lookup (elf_hash_table (info),
5657 info->fini_function, FALSE,
5658 FALSE, FALSE)
5659 : NULL);
5660 if (h != NULL
5661 && (h->ref_regular
5662 || h->def_regular))
5664 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5665 return FALSE;
5668 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5669 if (s != NULL && s->linker_has_input)
5671 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5672 if (! info->executable)
5674 bfd *sub;
5675 asection *o;
5677 for (sub = info->input_bfds; sub != NULL;
5678 sub = sub->link_next)
5679 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5680 for (o = sub->sections; o != NULL; o = o->next)
5681 if (elf_section_data (o)->this_hdr.sh_type
5682 == SHT_PREINIT_ARRAY)
5684 (*_bfd_error_handler)
5685 (_("%B: .preinit_array section is not allowed in DSO"),
5686 sub);
5687 break;
5690 bfd_set_error (bfd_error_nonrepresentable_section);
5691 return FALSE;
5694 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5695 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5696 return FALSE;
5698 s = bfd_get_section_by_name (output_bfd, ".init_array");
5699 if (s != NULL && s->linker_has_input)
5701 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5702 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5703 return FALSE;
5705 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5706 if (s != NULL && s->linker_has_input)
5708 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5709 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5710 return FALSE;
5713 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5714 /* If .dynstr is excluded from the link, we don't want any of
5715 these tags. Strictly, we should be checking each section
5716 individually; This quick check covers for the case where
5717 someone does a /DISCARD/ : { *(*) }. */
5718 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5720 bfd_size_type strsize;
5722 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5723 if ((info->emit_hash
5724 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5725 || (info->emit_gnu_hash
5726 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5727 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5728 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5729 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5730 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5731 bed->s->sizeof_sym))
5732 return FALSE;
5736 /* The backend must work out the sizes of all the other dynamic
5737 sections. */
5738 if (bed->elf_backend_size_dynamic_sections
5739 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5740 return FALSE;
5742 if (elf_hash_table (info)->dynamic_sections_created)
5744 unsigned long section_sym_count;
5745 asection *s;
5747 /* Set up the version definition section. */
5748 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5749 BFD_ASSERT (s != NULL);
5751 /* We may have created additional version definitions if we are
5752 just linking a regular application. */
5753 verdefs = asvinfo.verdefs;
5755 /* Skip anonymous version tag. */
5756 if (verdefs != NULL && verdefs->vernum == 0)
5757 verdefs = verdefs->next;
5759 if (verdefs == NULL && !info->create_default_symver)
5760 s->flags |= SEC_EXCLUDE;
5761 else
5763 unsigned int cdefs;
5764 bfd_size_type size;
5765 struct bfd_elf_version_tree *t;
5766 bfd_byte *p;
5767 Elf_Internal_Verdef def;
5768 Elf_Internal_Verdaux defaux;
5769 struct bfd_link_hash_entry *bh;
5770 struct elf_link_hash_entry *h;
5771 const char *name;
5773 cdefs = 0;
5774 size = 0;
5776 /* Make space for the base version. */
5777 size += sizeof (Elf_External_Verdef);
5778 size += sizeof (Elf_External_Verdaux);
5779 ++cdefs;
5781 /* Make space for the default version. */
5782 if (info->create_default_symver)
5784 size += sizeof (Elf_External_Verdef);
5785 ++cdefs;
5788 for (t = verdefs; t != NULL; t = t->next)
5790 struct bfd_elf_version_deps *n;
5792 size += sizeof (Elf_External_Verdef);
5793 size += sizeof (Elf_External_Verdaux);
5794 ++cdefs;
5796 for (n = t->deps; n != NULL; n = n->next)
5797 size += sizeof (Elf_External_Verdaux);
5800 s->size = size;
5801 s->contents = bfd_alloc (output_bfd, s->size);
5802 if (s->contents == NULL && s->size != 0)
5803 return FALSE;
5805 /* Fill in the version definition section. */
5807 p = s->contents;
5809 def.vd_version = VER_DEF_CURRENT;
5810 def.vd_flags = VER_FLG_BASE;
5811 def.vd_ndx = 1;
5812 def.vd_cnt = 1;
5813 if (info->create_default_symver)
5815 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5816 def.vd_next = sizeof (Elf_External_Verdef);
5818 else
5820 def.vd_aux = sizeof (Elf_External_Verdef);
5821 def.vd_next = (sizeof (Elf_External_Verdef)
5822 + sizeof (Elf_External_Verdaux));
5825 if (soname_indx != (bfd_size_type) -1)
5827 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5828 soname_indx);
5829 def.vd_hash = bfd_elf_hash (soname);
5830 defaux.vda_name = soname_indx;
5831 name = soname;
5833 else
5835 bfd_size_type indx;
5837 name = lbasename (output_bfd->filename);
5838 def.vd_hash = bfd_elf_hash (name);
5839 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5840 name, FALSE);
5841 if (indx == (bfd_size_type) -1)
5842 return FALSE;
5843 defaux.vda_name = indx;
5845 defaux.vda_next = 0;
5847 _bfd_elf_swap_verdef_out (output_bfd, &def,
5848 (Elf_External_Verdef *) p);
5849 p += sizeof (Elf_External_Verdef);
5850 if (info->create_default_symver)
5852 /* Add a symbol representing this version. */
5853 bh = NULL;
5854 if (! (_bfd_generic_link_add_one_symbol
5855 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5856 0, NULL, FALSE,
5857 get_elf_backend_data (dynobj)->collect, &bh)))
5858 return FALSE;
5859 h = (struct elf_link_hash_entry *) bh;
5860 h->non_elf = 0;
5861 h->def_regular = 1;
5862 h->type = STT_OBJECT;
5863 h->verinfo.vertree = NULL;
5865 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5866 return FALSE;
5868 /* Create a duplicate of the base version with the same
5869 aux block, but different flags. */
5870 def.vd_flags = 0;
5871 def.vd_ndx = 2;
5872 def.vd_aux = sizeof (Elf_External_Verdef);
5873 if (verdefs)
5874 def.vd_next = (sizeof (Elf_External_Verdef)
5875 + sizeof (Elf_External_Verdaux));
5876 else
5877 def.vd_next = 0;
5878 _bfd_elf_swap_verdef_out (output_bfd, &def,
5879 (Elf_External_Verdef *) p);
5880 p += sizeof (Elf_External_Verdef);
5882 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5883 (Elf_External_Verdaux *) p);
5884 p += sizeof (Elf_External_Verdaux);
5886 for (t = verdefs; t != NULL; t = t->next)
5888 unsigned int cdeps;
5889 struct bfd_elf_version_deps *n;
5891 cdeps = 0;
5892 for (n = t->deps; n != NULL; n = n->next)
5893 ++cdeps;
5895 /* Add a symbol representing this version. */
5896 bh = NULL;
5897 if (! (_bfd_generic_link_add_one_symbol
5898 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5899 0, NULL, FALSE,
5900 get_elf_backend_data (dynobj)->collect, &bh)))
5901 return FALSE;
5902 h = (struct elf_link_hash_entry *) bh;
5903 h->non_elf = 0;
5904 h->def_regular = 1;
5905 h->type = STT_OBJECT;
5906 h->verinfo.vertree = t;
5908 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5909 return FALSE;
5911 def.vd_version = VER_DEF_CURRENT;
5912 def.vd_flags = 0;
5913 if (t->globals.list == NULL
5914 && t->locals.list == NULL
5915 && ! t->used)
5916 def.vd_flags |= VER_FLG_WEAK;
5917 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5918 def.vd_cnt = cdeps + 1;
5919 def.vd_hash = bfd_elf_hash (t->name);
5920 def.vd_aux = sizeof (Elf_External_Verdef);
5921 def.vd_next = 0;
5922 if (t->next != NULL)
5923 def.vd_next = (sizeof (Elf_External_Verdef)
5924 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5926 _bfd_elf_swap_verdef_out (output_bfd, &def,
5927 (Elf_External_Verdef *) p);
5928 p += sizeof (Elf_External_Verdef);
5930 defaux.vda_name = h->dynstr_index;
5931 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5932 h->dynstr_index);
5933 defaux.vda_next = 0;
5934 if (t->deps != NULL)
5935 defaux.vda_next = sizeof (Elf_External_Verdaux);
5936 t->name_indx = defaux.vda_name;
5938 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5939 (Elf_External_Verdaux *) p);
5940 p += sizeof (Elf_External_Verdaux);
5942 for (n = t->deps; n != NULL; n = n->next)
5944 if (n->version_needed == NULL)
5946 /* This can happen if there was an error in the
5947 version script. */
5948 defaux.vda_name = 0;
5950 else
5952 defaux.vda_name = n->version_needed->name_indx;
5953 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5954 defaux.vda_name);
5956 if (n->next == NULL)
5957 defaux.vda_next = 0;
5958 else
5959 defaux.vda_next = sizeof (Elf_External_Verdaux);
5961 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5962 (Elf_External_Verdaux *) p);
5963 p += sizeof (Elf_External_Verdaux);
5967 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5968 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5969 return FALSE;
5971 elf_tdata (output_bfd)->cverdefs = cdefs;
5974 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5976 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5977 return FALSE;
5979 else if (info->flags & DF_BIND_NOW)
5981 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5982 return FALSE;
5985 if (info->flags_1)
5987 if (info->executable)
5988 info->flags_1 &= ~ (DF_1_INITFIRST
5989 | DF_1_NODELETE
5990 | DF_1_NOOPEN);
5991 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5992 return FALSE;
5995 /* Work out the size of the version reference section. */
5997 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5998 BFD_ASSERT (s != NULL);
6000 struct elf_find_verdep_info sinfo;
6002 sinfo.output_bfd = output_bfd;
6003 sinfo.info = info;
6004 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6005 if (sinfo.vers == 0)
6006 sinfo.vers = 1;
6007 sinfo.failed = FALSE;
6009 elf_link_hash_traverse (elf_hash_table (info),
6010 _bfd_elf_link_find_version_dependencies,
6011 &sinfo);
6012 if (sinfo.failed)
6013 return FALSE;
6015 if (elf_tdata (output_bfd)->verref == NULL)
6016 s->flags |= SEC_EXCLUDE;
6017 else
6019 Elf_Internal_Verneed *t;
6020 unsigned int size;
6021 unsigned int crefs;
6022 bfd_byte *p;
6024 /* Build the version definition section. */
6025 size = 0;
6026 crefs = 0;
6027 for (t = elf_tdata (output_bfd)->verref;
6028 t != NULL;
6029 t = t->vn_nextref)
6031 Elf_Internal_Vernaux *a;
6033 size += sizeof (Elf_External_Verneed);
6034 ++crefs;
6035 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6036 size += sizeof (Elf_External_Vernaux);
6039 s->size = size;
6040 s->contents = bfd_alloc (output_bfd, s->size);
6041 if (s->contents == NULL)
6042 return FALSE;
6044 p = s->contents;
6045 for (t = elf_tdata (output_bfd)->verref;
6046 t != NULL;
6047 t = t->vn_nextref)
6049 unsigned int caux;
6050 Elf_Internal_Vernaux *a;
6051 bfd_size_type indx;
6053 caux = 0;
6054 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6055 ++caux;
6057 t->vn_version = VER_NEED_CURRENT;
6058 t->vn_cnt = caux;
6059 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6060 elf_dt_name (t->vn_bfd) != NULL
6061 ? elf_dt_name (t->vn_bfd)
6062 : lbasename (t->vn_bfd->filename),
6063 FALSE);
6064 if (indx == (bfd_size_type) -1)
6065 return FALSE;
6066 t->vn_file = indx;
6067 t->vn_aux = sizeof (Elf_External_Verneed);
6068 if (t->vn_nextref == NULL)
6069 t->vn_next = 0;
6070 else
6071 t->vn_next = (sizeof (Elf_External_Verneed)
6072 + caux * sizeof (Elf_External_Vernaux));
6074 _bfd_elf_swap_verneed_out (output_bfd, t,
6075 (Elf_External_Verneed *) p);
6076 p += sizeof (Elf_External_Verneed);
6078 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6080 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6081 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6082 a->vna_nodename, FALSE);
6083 if (indx == (bfd_size_type) -1)
6084 return FALSE;
6085 a->vna_name = indx;
6086 if (a->vna_nextptr == NULL)
6087 a->vna_next = 0;
6088 else
6089 a->vna_next = sizeof (Elf_External_Vernaux);
6091 _bfd_elf_swap_vernaux_out (output_bfd, a,
6092 (Elf_External_Vernaux *) p);
6093 p += sizeof (Elf_External_Vernaux);
6097 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6098 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6099 return FALSE;
6101 elf_tdata (output_bfd)->cverrefs = crefs;
6105 if ((elf_tdata (output_bfd)->cverrefs == 0
6106 && elf_tdata (output_bfd)->cverdefs == 0)
6107 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6108 &section_sym_count) == 0)
6110 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6111 s->flags |= SEC_EXCLUDE;
6114 return TRUE;
6117 /* Find the first non-excluded output section. We'll use its
6118 section symbol for some emitted relocs. */
6119 void
6120 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6122 asection *s;
6124 for (s = output_bfd->sections; s != NULL; s = s->next)
6125 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6126 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6128 elf_hash_table (info)->text_index_section = s;
6129 break;
6133 /* Find two non-excluded output sections, one for code, one for data.
6134 We'll use their section symbols for some emitted relocs. */
6135 void
6136 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6138 asection *s;
6140 for (s = output_bfd->sections; s != NULL; s = s->next)
6141 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6142 == (SEC_ALLOC | SEC_READONLY))
6143 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6145 elf_hash_table (info)->text_index_section = s;
6146 break;
6149 for (s = output_bfd->sections; s != NULL; s = s->next)
6150 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6151 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6153 elf_hash_table (info)->data_index_section = s;
6154 break;
6157 if (elf_hash_table (info)->text_index_section == NULL)
6158 elf_hash_table (info)->text_index_section
6159 = elf_hash_table (info)->data_index_section;
6162 bfd_boolean
6163 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6165 const struct elf_backend_data *bed;
6167 if (!is_elf_hash_table (info->hash))
6168 return TRUE;
6170 bed = get_elf_backend_data (output_bfd);
6171 (*bed->elf_backend_init_index_section) (output_bfd, info);
6173 if (elf_hash_table (info)->dynamic_sections_created)
6175 bfd *dynobj;
6176 asection *s;
6177 bfd_size_type dynsymcount;
6178 unsigned long section_sym_count;
6179 unsigned int dtagcount;
6181 dynobj = elf_hash_table (info)->dynobj;
6183 /* Assign dynsym indicies. In a shared library we generate a
6184 section symbol for each output section, which come first.
6185 Next come all of the back-end allocated local dynamic syms,
6186 followed by the rest of the global symbols. */
6188 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6189 &section_sym_count);
6191 /* Work out the size of the symbol version section. */
6192 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6193 BFD_ASSERT (s != NULL);
6194 if (dynsymcount != 0
6195 && (s->flags & SEC_EXCLUDE) == 0)
6197 s->size = dynsymcount * sizeof (Elf_External_Versym);
6198 s->contents = bfd_zalloc (output_bfd, s->size);
6199 if (s->contents == NULL)
6200 return FALSE;
6202 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6203 return FALSE;
6206 /* Set the size of the .dynsym and .hash sections. We counted
6207 the number of dynamic symbols in elf_link_add_object_symbols.
6208 We will build the contents of .dynsym and .hash when we build
6209 the final symbol table, because until then we do not know the
6210 correct value to give the symbols. We built the .dynstr
6211 section as we went along in elf_link_add_object_symbols. */
6212 s = bfd_get_section_by_name (dynobj, ".dynsym");
6213 BFD_ASSERT (s != NULL);
6214 s->size = dynsymcount * bed->s->sizeof_sym;
6216 if (dynsymcount != 0)
6218 s->contents = bfd_alloc (output_bfd, s->size);
6219 if (s->contents == NULL)
6220 return FALSE;
6222 /* The first entry in .dynsym is a dummy symbol.
6223 Clear all the section syms, in case we don't output them all. */
6224 ++section_sym_count;
6225 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6228 elf_hash_table (info)->bucketcount = 0;
6230 /* Compute the size of the hashing table. As a side effect this
6231 computes the hash values for all the names we export. */
6232 if (info->emit_hash)
6234 unsigned long int *hashcodes;
6235 struct hash_codes_info hashinf;
6236 bfd_size_type amt;
6237 unsigned long int nsyms;
6238 size_t bucketcount;
6239 size_t hash_entry_size;
6241 /* Compute the hash values for all exported symbols. At the same
6242 time store the values in an array so that we could use them for
6243 optimizations. */
6244 amt = dynsymcount * sizeof (unsigned long int);
6245 hashcodes = bfd_malloc (amt);
6246 if (hashcodes == NULL)
6247 return FALSE;
6248 hashinf.hashcodes = hashcodes;
6249 hashinf.error = FALSE;
6251 /* Put all hash values in HASHCODES. */
6252 elf_link_hash_traverse (elf_hash_table (info),
6253 elf_collect_hash_codes, &hashinf);
6254 if (hashinf.error)
6255 return FALSE;
6257 nsyms = hashinf.hashcodes - hashcodes;
6258 bucketcount
6259 = compute_bucket_count (info, hashcodes, nsyms, 0);
6260 free (hashcodes);
6262 if (bucketcount == 0)
6263 return FALSE;
6265 elf_hash_table (info)->bucketcount = bucketcount;
6267 s = bfd_get_section_by_name (dynobj, ".hash");
6268 BFD_ASSERT (s != NULL);
6269 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6270 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6271 s->contents = bfd_zalloc (output_bfd, s->size);
6272 if (s->contents == NULL)
6273 return FALSE;
6275 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6276 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6277 s->contents + hash_entry_size);
6280 if (info->emit_gnu_hash)
6282 size_t i, cnt;
6283 unsigned char *contents;
6284 struct collect_gnu_hash_codes cinfo;
6285 bfd_size_type amt;
6286 size_t bucketcount;
6288 memset (&cinfo, 0, sizeof (cinfo));
6290 /* Compute the hash values for all exported symbols. At the same
6291 time store the values in an array so that we could use them for
6292 optimizations. */
6293 amt = dynsymcount * 2 * sizeof (unsigned long int);
6294 cinfo.hashcodes = bfd_malloc (amt);
6295 if (cinfo.hashcodes == NULL)
6296 return FALSE;
6298 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6299 cinfo.min_dynindx = -1;
6300 cinfo.output_bfd = output_bfd;
6301 cinfo.bed = bed;
6303 /* Put all hash values in HASHCODES. */
6304 elf_link_hash_traverse (elf_hash_table (info),
6305 elf_collect_gnu_hash_codes, &cinfo);
6306 if (cinfo.error)
6307 return FALSE;
6309 bucketcount
6310 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6312 if (bucketcount == 0)
6314 free (cinfo.hashcodes);
6315 return FALSE;
6318 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6319 BFD_ASSERT (s != NULL);
6321 if (cinfo.nsyms == 0)
6323 /* Empty .gnu.hash section is special. */
6324 BFD_ASSERT (cinfo.min_dynindx == -1);
6325 free (cinfo.hashcodes);
6326 s->size = 5 * 4 + bed->s->arch_size / 8;
6327 contents = bfd_zalloc (output_bfd, s->size);
6328 if (contents == NULL)
6329 return FALSE;
6330 s->contents = contents;
6331 /* 1 empty bucket. */
6332 bfd_put_32 (output_bfd, 1, contents);
6333 /* SYMIDX above the special symbol 0. */
6334 bfd_put_32 (output_bfd, 1, contents + 4);
6335 /* Just one word for bitmask. */
6336 bfd_put_32 (output_bfd, 1, contents + 8);
6337 /* Only hash fn bloom filter. */
6338 bfd_put_32 (output_bfd, 0, contents + 12);
6339 /* No hashes are valid - empty bitmask. */
6340 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6341 /* No hashes in the only bucket. */
6342 bfd_put_32 (output_bfd, 0,
6343 contents + 16 + bed->s->arch_size / 8);
6345 else
6347 unsigned long int maskwords, maskbitslog2;
6348 BFD_ASSERT (cinfo.min_dynindx != -1);
6350 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6351 if (maskbitslog2 < 3)
6352 maskbitslog2 = 5;
6353 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6354 maskbitslog2 = maskbitslog2 + 3;
6355 else
6356 maskbitslog2 = maskbitslog2 + 2;
6357 if (bed->s->arch_size == 64)
6359 if (maskbitslog2 == 5)
6360 maskbitslog2 = 6;
6361 cinfo.shift1 = 6;
6363 else
6364 cinfo.shift1 = 5;
6365 cinfo.mask = (1 << cinfo.shift1) - 1;
6366 cinfo.shift2 = maskbitslog2;
6367 cinfo.maskbits = 1 << maskbitslog2;
6368 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6369 amt = bucketcount * sizeof (unsigned long int) * 2;
6370 amt += maskwords * sizeof (bfd_vma);
6371 cinfo.bitmask = bfd_malloc (amt);
6372 if (cinfo.bitmask == NULL)
6374 free (cinfo.hashcodes);
6375 return FALSE;
6378 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6379 cinfo.indx = cinfo.counts + bucketcount;
6380 cinfo.symindx = dynsymcount - cinfo.nsyms;
6381 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6383 /* Determine how often each hash bucket is used. */
6384 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6385 for (i = 0; i < cinfo.nsyms; ++i)
6386 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6388 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6389 if (cinfo.counts[i] != 0)
6391 cinfo.indx[i] = cnt;
6392 cnt += cinfo.counts[i];
6394 BFD_ASSERT (cnt == dynsymcount);
6395 cinfo.bucketcount = bucketcount;
6396 cinfo.local_indx = cinfo.min_dynindx;
6398 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6399 s->size += cinfo.maskbits / 8;
6400 contents = bfd_zalloc (output_bfd, s->size);
6401 if (contents == NULL)
6403 free (cinfo.bitmask);
6404 free (cinfo.hashcodes);
6405 return FALSE;
6408 s->contents = contents;
6409 bfd_put_32 (output_bfd, bucketcount, contents);
6410 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6411 bfd_put_32 (output_bfd, maskwords, contents + 8);
6412 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6413 contents += 16 + cinfo.maskbits / 8;
6415 for (i = 0; i < bucketcount; ++i)
6417 if (cinfo.counts[i] == 0)
6418 bfd_put_32 (output_bfd, 0, contents);
6419 else
6420 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6421 contents += 4;
6424 cinfo.contents = contents;
6426 /* Renumber dynamic symbols, populate .gnu.hash section. */
6427 elf_link_hash_traverse (elf_hash_table (info),
6428 elf_renumber_gnu_hash_syms, &cinfo);
6430 contents = s->contents + 16;
6431 for (i = 0; i < maskwords; ++i)
6433 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6434 contents);
6435 contents += bed->s->arch_size / 8;
6438 free (cinfo.bitmask);
6439 free (cinfo.hashcodes);
6443 s = bfd_get_section_by_name (dynobj, ".dynstr");
6444 BFD_ASSERT (s != NULL);
6446 elf_finalize_dynstr (output_bfd, info);
6448 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6450 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6451 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6452 return FALSE;
6455 return TRUE;
6458 /* Indicate that we are only retrieving symbol values from this
6459 section. */
6461 void
6462 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6464 if (is_elf_hash_table (info->hash))
6465 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6466 _bfd_generic_link_just_syms (sec, info);
6469 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6471 static void
6472 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6473 asection *sec)
6475 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6476 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6479 /* Finish SHF_MERGE section merging. */
6481 bfd_boolean
6482 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6484 bfd *ibfd;
6485 asection *sec;
6487 if (!is_elf_hash_table (info->hash))
6488 return FALSE;
6490 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6491 if ((ibfd->flags & DYNAMIC) == 0)
6492 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6493 if ((sec->flags & SEC_MERGE) != 0
6494 && !bfd_is_abs_section (sec->output_section))
6496 struct bfd_elf_section_data *secdata;
6498 secdata = elf_section_data (sec);
6499 if (! _bfd_add_merge_section (abfd,
6500 &elf_hash_table (info)->merge_info,
6501 sec, &secdata->sec_info))
6502 return FALSE;
6503 else if (secdata->sec_info)
6504 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6507 if (elf_hash_table (info)->merge_info != NULL)
6508 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6509 merge_sections_remove_hook);
6510 return TRUE;
6513 /* Create an entry in an ELF linker hash table. */
6515 struct bfd_hash_entry *
6516 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6517 struct bfd_hash_table *table,
6518 const char *string)
6520 /* Allocate the structure if it has not already been allocated by a
6521 subclass. */
6522 if (entry == NULL)
6524 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6525 if (entry == NULL)
6526 return entry;
6529 /* Call the allocation method of the superclass. */
6530 entry = _bfd_link_hash_newfunc (entry, table, string);
6531 if (entry != NULL)
6533 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6534 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6536 /* Set local fields. */
6537 ret->indx = -1;
6538 ret->dynindx = -1;
6539 ret->got = htab->init_got_refcount;
6540 ret->plt = htab->init_plt_refcount;
6541 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6542 - offsetof (struct elf_link_hash_entry, size)));
6543 /* Assume that we have been called by a non-ELF symbol reader.
6544 This flag is then reset by the code which reads an ELF input
6545 file. This ensures that a symbol created by a non-ELF symbol
6546 reader will have the flag set correctly. */
6547 ret->non_elf = 1;
6550 return entry;
6553 /* Copy data from an indirect symbol to its direct symbol, hiding the
6554 old indirect symbol. Also used for copying flags to a weakdef. */
6556 void
6557 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6558 struct elf_link_hash_entry *dir,
6559 struct elf_link_hash_entry *ind)
6561 struct elf_link_hash_table *htab;
6563 /* Copy down any references that we may have already seen to the
6564 symbol which just became indirect. */
6566 dir->ref_dynamic |= ind->ref_dynamic;
6567 dir->ref_regular |= ind->ref_regular;
6568 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6569 dir->non_got_ref |= ind->non_got_ref;
6570 dir->needs_plt |= ind->needs_plt;
6571 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6573 if (ind->root.type != bfd_link_hash_indirect)
6574 return;
6576 /* Copy over the global and procedure linkage table refcount entries.
6577 These may have been already set up by a check_relocs routine. */
6578 htab = elf_hash_table (info);
6579 if (ind->got.refcount > htab->init_got_refcount.refcount)
6581 if (dir->got.refcount < 0)
6582 dir->got.refcount = 0;
6583 dir->got.refcount += ind->got.refcount;
6584 ind->got.refcount = htab->init_got_refcount.refcount;
6587 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6589 if (dir->plt.refcount < 0)
6590 dir->plt.refcount = 0;
6591 dir->plt.refcount += ind->plt.refcount;
6592 ind->plt.refcount = htab->init_plt_refcount.refcount;
6595 if (ind->dynindx != -1)
6597 if (dir->dynindx != -1)
6598 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6599 dir->dynindx = ind->dynindx;
6600 dir->dynstr_index = ind->dynstr_index;
6601 ind->dynindx = -1;
6602 ind->dynstr_index = 0;
6606 void
6607 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6608 struct elf_link_hash_entry *h,
6609 bfd_boolean force_local)
6611 h->plt = elf_hash_table (info)->init_plt_offset;
6612 h->needs_plt = 0;
6613 if (force_local)
6615 h->forced_local = 1;
6616 if (h->dynindx != -1)
6618 h->dynindx = -1;
6619 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6620 h->dynstr_index);
6625 /* Initialize an ELF linker hash table. */
6627 bfd_boolean
6628 _bfd_elf_link_hash_table_init
6629 (struct elf_link_hash_table *table,
6630 bfd *abfd,
6631 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6632 struct bfd_hash_table *,
6633 const char *),
6634 unsigned int entsize)
6636 bfd_boolean ret;
6637 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6639 memset (table, 0, sizeof * table);
6640 table->init_got_refcount.refcount = can_refcount - 1;
6641 table->init_plt_refcount.refcount = can_refcount - 1;
6642 table->init_got_offset.offset = -(bfd_vma) 1;
6643 table->init_plt_offset.offset = -(bfd_vma) 1;
6644 /* The first dynamic symbol is a dummy. */
6645 table->dynsymcount = 1;
6647 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6648 table->root.type = bfd_link_elf_hash_table;
6650 return ret;
6653 /* Create an ELF linker hash table. */
6655 struct bfd_link_hash_table *
6656 _bfd_elf_link_hash_table_create (bfd *abfd)
6658 struct elf_link_hash_table *ret;
6659 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6661 ret = bfd_malloc (amt);
6662 if (ret == NULL)
6663 return NULL;
6665 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6666 sizeof (struct elf_link_hash_entry)))
6668 free (ret);
6669 return NULL;
6672 return &ret->root;
6675 /* This is a hook for the ELF emulation code in the generic linker to
6676 tell the backend linker what file name to use for the DT_NEEDED
6677 entry for a dynamic object. */
6679 void
6680 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6682 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6683 && bfd_get_format (abfd) == bfd_object)
6684 elf_dt_name (abfd) = name;
6688 bfd_elf_get_dyn_lib_class (bfd *abfd)
6690 int lib_class;
6691 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6692 && bfd_get_format (abfd) == bfd_object)
6693 lib_class = elf_dyn_lib_class (abfd);
6694 else
6695 lib_class = 0;
6696 return lib_class;
6699 void
6700 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6702 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6703 && bfd_get_format (abfd) == bfd_object)
6704 elf_dyn_lib_class (abfd) = lib_class;
6707 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6708 the linker ELF emulation code. */
6710 struct bfd_link_needed_list *
6711 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6712 struct bfd_link_info *info)
6714 if (! is_elf_hash_table (info->hash))
6715 return NULL;
6716 return elf_hash_table (info)->needed;
6719 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6720 hook for the linker ELF emulation code. */
6722 struct bfd_link_needed_list *
6723 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6724 struct bfd_link_info *info)
6726 if (! is_elf_hash_table (info->hash))
6727 return NULL;
6728 return elf_hash_table (info)->runpath;
6731 /* Get the name actually used for a dynamic object for a link. This
6732 is the SONAME entry if there is one. Otherwise, it is the string
6733 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6735 const char *
6736 bfd_elf_get_dt_soname (bfd *abfd)
6738 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6739 && bfd_get_format (abfd) == bfd_object)
6740 return elf_dt_name (abfd);
6741 return NULL;
6744 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6745 the ELF linker emulation code. */
6747 bfd_boolean
6748 bfd_elf_get_bfd_needed_list (bfd *abfd,
6749 struct bfd_link_needed_list **pneeded)
6751 asection *s;
6752 bfd_byte *dynbuf = NULL;
6753 int elfsec;
6754 unsigned long shlink;
6755 bfd_byte *extdyn, *extdynend;
6756 size_t extdynsize;
6757 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6759 *pneeded = NULL;
6761 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6762 || bfd_get_format (abfd) != bfd_object)
6763 return TRUE;
6765 s = bfd_get_section_by_name (abfd, ".dynamic");
6766 if (s == NULL || s->size == 0)
6767 return TRUE;
6769 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6770 goto error_return;
6772 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6773 if (elfsec == -1)
6774 goto error_return;
6776 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
6778 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6779 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6781 extdyn = dynbuf;
6782 extdynend = extdyn + s->size;
6783 for (; extdyn < extdynend; extdyn += extdynsize)
6785 Elf_Internal_Dyn dyn;
6787 (*swap_dyn_in) (abfd, extdyn, &dyn);
6789 if (dyn.d_tag == DT_NULL)
6790 break;
6792 if (dyn.d_tag == DT_NEEDED)
6794 const char *string;
6795 struct bfd_link_needed_list *l;
6796 unsigned int tagv = dyn.d_un.d_val;
6797 bfd_size_type amt;
6799 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6800 if (string == NULL)
6801 goto error_return;
6803 amt = sizeof *l;
6804 l = bfd_alloc (abfd, amt);
6805 if (l == NULL)
6806 goto error_return;
6808 l->by = abfd;
6809 l->name = string;
6810 l->next = *pneeded;
6811 *pneeded = l;
6815 free (dynbuf);
6817 return TRUE;
6819 error_return:
6820 if (dynbuf != NULL)
6821 free (dynbuf);
6822 return FALSE;
6825 struct elf_symbuf_symbol
6827 unsigned long st_name; /* Symbol name, index in string tbl */
6828 unsigned char st_info; /* Type and binding attributes */
6829 unsigned char st_other; /* Visibilty, and target specific */
6832 struct elf_symbuf_head
6834 struct elf_symbuf_symbol *ssym;
6835 bfd_size_type count;
6836 unsigned int st_shndx;
6839 struct elf_symbol
6841 union
6843 Elf_Internal_Sym *isym;
6844 struct elf_symbuf_symbol *ssym;
6845 } u;
6846 const char *name;
6849 /* Sort references to symbols by ascending section number. */
6851 static int
6852 elf_sort_elf_symbol (const void *arg1, const void *arg2)
6854 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
6855 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
6857 return s1->st_shndx - s2->st_shndx;
6860 static int
6861 elf_sym_name_compare (const void *arg1, const void *arg2)
6863 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
6864 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
6865 return strcmp (s1->name, s2->name);
6868 static struct elf_symbuf_head *
6869 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
6871 Elf_Internal_Sym **ind, **indbufend, **indbuf;
6872 struct elf_symbuf_symbol *ssym;
6873 struct elf_symbuf_head *ssymbuf, *ssymhead;
6874 bfd_size_type i, shndx_count, total_size;
6876 indbuf = bfd_malloc2 (symcount, sizeof (*indbuf));
6877 if (indbuf == NULL)
6878 return NULL;
6880 for (ind = indbuf, i = 0; i < symcount; i++)
6881 if (isymbuf[i].st_shndx != SHN_UNDEF)
6882 *ind++ = &isymbuf[i];
6883 indbufend = ind;
6885 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
6886 elf_sort_elf_symbol);
6888 shndx_count = 0;
6889 if (indbufend > indbuf)
6890 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
6891 if (ind[0]->st_shndx != ind[1]->st_shndx)
6892 shndx_count++;
6894 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
6895 + (indbufend - indbuf) * sizeof (*ssym));
6896 ssymbuf = bfd_malloc (total_size);
6897 if (ssymbuf == NULL)
6899 free (indbuf);
6900 return NULL;
6903 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
6904 ssymbuf->ssym = NULL;
6905 ssymbuf->count = shndx_count;
6906 ssymbuf->st_shndx = 0;
6907 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
6909 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
6911 ssymhead++;
6912 ssymhead->ssym = ssym;
6913 ssymhead->count = 0;
6914 ssymhead->st_shndx = (*ind)->st_shndx;
6916 ssym->st_name = (*ind)->st_name;
6917 ssym->st_info = (*ind)->st_info;
6918 ssym->st_other = (*ind)->st_other;
6919 ssymhead->count++;
6921 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
6922 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
6923 == total_size));
6925 free (indbuf);
6926 return ssymbuf;
6929 /* Check if 2 sections define the same set of local and global
6930 symbols. */
6932 static bfd_boolean
6933 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
6934 struct bfd_link_info *info)
6936 bfd *bfd1, *bfd2;
6937 const struct elf_backend_data *bed1, *bed2;
6938 Elf_Internal_Shdr *hdr1, *hdr2;
6939 bfd_size_type symcount1, symcount2;
6940 Elf_Internal_Sym *isymbuf1, *isymbuf2;
6941 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
6942 Elf_Internal_Sym *isym, *isymend;
6943 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
6944 bfd_size_type count1, count2, i;
6945 int shndx1, shndx2;
6946 bfd_boolean result;
6948 bfd1 = sec1->owner;
6949 bfd2 = sec2->owner;
6951 /* Both sections have to be in ELF. */
6952 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
6953 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
6954 return FALSE;
6956 if (elf_section_type (sec1) != elf_section_type (sec2))
6957 return FALSE;
6959 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
6960 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
6961 if (shndx1 == -1 || shndx2 == -1)
6962 return FALSE;
6964 bed1 = get_elf_backend_data (bfd1);
6965 bed2 = get_elf_backend_data (bfd2);
6966 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
6967 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
6968 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
6969 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
6971 if (symcount1 == 0 || symcount2 == 0)
6972 return FALSE;
6974 result = FALSE;
6975 isymbuf1 = NULL;
6976 isymbuf2 = NULL;
6977 ssymbuf1 = elf_tdata (bfd1)->symbuf;
6978 ssymbuf2 = elf_tdata (bfd2)->symbuf;
6980 if (ssymbuf1 == NULL)
6982 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
6983 NULL, NULL, NULL);
6984 if (isymbuf1 == NULL)
6985 goto done;
6987 if (!info->reduce_memory_overheads)
6988 elf_tdata (bfd1)->symbuf = ssymbuf1
6989 = elf_create_symbuf (symcount1, isymbuf1);
6992 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
6994 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
6995 NULL, NULL, NULL);
6996 if (isymbuf2 == NULL)
6997 goto done;
6999 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7000 elf_tdata (bfd2)->symbuf = ssymbuf2
7001 = elf_create_symbuf (symcount2, isymbuf2);
7004 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7006 /* Optimized faster version. */
7007 bfd_size_type lo, hi, mid;
7008 struct elf_symbol *symp;
7009 struct elf_symbuf_symbol *ssym, *ssymend;
7011 lo = 0;
7012 hi = ssymbuf1->count;
7013 ssymbuf1++;
7014 count1 = 0;
7015 while (lo < hi)
7017 mid = (lo + hi) / 2;
7018 if ((unsigned int) shndx1 < ssymbuf1[mid].st_shndx)
7019 hi = mid;
7020 else if ((unsigned int) shndx1 > ssymbuf1[mid].st_shndx)
7021 lo = mid + 1;
7022 else
7024 count1 = ssymbuf1[mid].count;
7025 ssymbuf1 += mid;
7026 break;
7030 lo = 0;
7031 hi = ssymbuf2->count;
7032 ssymbuf2++;
7033 count2 = 0;
7034 while (lo < hi)
7036 mid = (lo + hi) / 2;
7037 if ((unsigned int) shndx2 < ssymbuf2[mid].st_shndx)
7038 hi = mid;
7039 else if ((unsigned int) shndx2 > ssymbuf2[mid].st_shndx)
7040 lo = mid + 1;
7041 else
7043 count2 = ssymbuf2[mid].count;
7044 ssymbuf2 += mid;
7045 break;
7049 if (count1 == 0 || count2 == 0 || count1 != count2)
7050 goto done;
7052 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
7053 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
7054 if (symtable1 == NULL || symtable2 == NULL)
7055 goto done;
7057 symp = symtable1;
7058 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7059 ssym < ssymend; ssym++, symp++)
7061 symp->u.ssym = ssym;
7062 symp->name = bfd_elf_string_from_elf_section (bfd1,
7063 hdr1->sh_link,
7064 ssym->st_name);
7067 symp = symtable2;
7068 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7069 ssym < ssymend; ssym++, symp++)
7071 symp->u.ssym = ssym;
7072 symp->name = bfd_elf_string_from_elf_section (bfd2,
7073 hdr2->sh_link,
7074 ssym->st_name);
7077 /* Sort symbol by name. */
7078 qsort (symtable1, count1, sizeof (struct elf_symbol),
7079 elf_sym_name_compare);
7080 qsort (symtable2, count1, sizeof (struct elf_symbol),
7081 elf_sym_name_compare);
7083 for (i = 0; i < count1; i++)
7084 /* Two symbols must have the same binding, type and name. */
7085 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7086 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7087 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7088 goto done;
7090 result = TRUE;
7091 goto done;
7094 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7095 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7096 if (symtable1 == NULL || symtable2 == NULL)
7097 goto done;
7099 /* Count definitions in the section. */
7100 count1 = 0;
7101 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7102 if (isym->st_shndx == (unsigned int) shndx1)
7103 symtable1[count1++].u.isym = isym;
7105 count2 = 0;
7106 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7107 if (isym->st_shndx == (unsigned int) shndx2)
7108 symtable2[count2++].u.isym = isym;
7110 if (count1 == 0 || count2 == 0 || count1 != count2)
7111 goto done;
7113 for (i = 0; i < count1; i++)
7114 symtable1[i].name
7115 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7116 symtable1[i].u.isym->st_name);
7118 for (i = 0; i < count2; i++)
7119 symtable2[i].name
7120 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7121 symtable2[i].u.isym->st_name);
7123 /* Sort symbol by name. */
7124 qsort (symtable1, count1, sizeof (struct elf_symbol),
7125 elf_sym_name_compare);
7126 qsort (symtable2, count1, sizeof (struct elf_symbol),
7127 elf_sym_name_compare);
7129 for (i = 0; i < count1; i++)
7130 /* Two symbols must have the same binding, type and name. */
7131 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7132 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7133 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7134 goto done;
7136 result = TRUE;
7138 done:
7139 if (symtable1)
7140 free (symtable1);
7141 if (symtable2)
7142 free (symtable2);
7143 if (isymbuf1)
7144 free (isymbuf1);
7145 if (isymbuf2)
7146 free (isymbuf2);
7148 return result;
7151 /* Return TRUE if 2 section types are compatible. */
7153 bfd_boolean
7154 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7155 bfd *bbfd, const asection *bsec)
7157 if (asec == NULL
7158 || bsec == NULL
7159 || abfd->xvec->flavour != bfd_target_elf_flavour
7160 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7161 return TRUE;
7163 return elf_section_type (asec) == elf_section_type (bsec);
7166 /* Final phase of ELF linker. */
7168 /* A structure we use to avoid passing large numbers of arguments. */
7170 struct elf_final_link_info
7172 /* General link information. */
7173 struct bfd_link_info *info;
7174 /* Output BFD. */
7175 bfd *output_bfd;
7176 /* Symbol string table. */
7177 struct bfd_strtab_hash *symstrtab;
7178 /* .dynsym section. */
7179 asection *dynsym_sec;
7180 /* .hash section. */
7181 asection *hash_sec;
7182 /* symbol version section (.gnu.version). */
7183 asection *symver_sec;
7184 /* Buffer large enough to hold contents of any section. */
7185 bfd_byte *contents;
7186 /* Buffer large enough to hold external relocs of any section. */
7187 void *external_relocs;
7188 /* Buffer large enough to hold internal relocs of any section. */
7189 Elf_Internal_Rela *internal_relocs;
7190 /* Buffer large enough to hold external local symbols of any input
7191 BFD. */
7192 bfd_byte *external_syms;
7193 /* And a buffer for symbol section indices. */
7194 Elf_External_Sym_Shndx *locsym_shndx;
7195 /* Buffer large enough to hold internal local symbols of any input
7196 BFD. */
7197 Elf_Internal_Sym *internal_syms;
7198 /* Array large enough to hold a symbol index for each local symbol
7199 of any input BFD. */
7200 long *indices;
7201 /* Array large enough to hold a section pointer for each local
7202 symbol of any input BFD. */
7203 asection **sections;
7204 /* Buffer to hold swapped out symbols. */
7205 bfd_byte *symbuf;
7206 /* And one for symbol section indices. */
7207 Elf_External_Sym_Shndx *symshndxbuf;
7208 /* Number of swapped out symbols in buffer. */
7209 size_t symbuf_count;
7210 /* Number of symbols which fit in symbuf. */
7211 size_t symbuf_size;
7212 /* And same for symshndxbuf. */
7213 size_t shndxbuf_size;
7216 /* This struct is used to pass information to elf_link_output_extsym. */
7218 struct elf_outext_info
7220 bfd_boolean failed;
7221 bfd_boolean localsyms;
7222 struct elf_final_link_info *finfo;
7226 /* Support for evaluating a complex relocation.
7228 Complex relocations are generalized, self-describing relocations. The
7229 implementation of them consists of two parts: complex symbols, and the
7230 relocations themselves.
7232 The relocations are use a reserved elf-wide relocation type code (R_RELC
7233 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7234 information (start bit, end bit, word width, etc) into the addend. This
7235 information is extracted from CGEN-generated operand tables within gas.
7237 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7238 internal) representing prefix-notation expressions, including but not
7239 limited to those sorts of expressions normally encoded as addends in the
7240 addend field. The symbol mangling format is:
7242 <node> := <literal>
7243 | <unary-operator> ':' <node>
7244 | <binary-operator> ':' <node> ':' <node>
7247 <literal> := 's' <digits=N> ':' <N character symbol name>
7248 | 'S' <digits=N> ':' <N character section name>
7249 | '#' <hexdigits>
7252 <binary-operator> := as in C
7253 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7255 static void
7256 set_symbol_value (bfd *bfd_with_globals,
7257 Elf_Internal_Sym *isymbuf,
7258 size_t locsymcount,
7259 size_t symidx,
7260 bfd_vma val)
7262 struct elf_link_hash_entry **sym_hashes;
7263 struct elf_link_hash_entry *h;
7264 size_t extsymoff = locsymcount;
7266 if (symidx < locsymcount)
7268 Elf_Internal_Sym *sym;
7270 sym = isymbuf + symidx;
7271 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7273 /* It is a local symbol: move it to the
7274 "absolute" section and give it a value. */
7275 sym->st_shndx = SHN_ABS;
7276 sym->st_value = val;
7277 return;
7279 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7280 extsymoff = 0;
7283 /* It is a global symbol: set its link type
7284 to "defined" and give it a value. */
7286 sym_hashes = elf_sym_hashes (bfd_with_globals);
7287 h = sym_hashes [symidx - extsymoff];
7288 while (h->root.type == bfd_link_hash_indirect
7289 || h->root.type == bfd_link_hash_warning)
7290 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7291 h->root.type = bfd_link_hash_defined;
7292 h->root.u.def.value = val;
7293 h->root.u.def.section = bfd_abs_section_ptr;
7296 static bfd_boolean
7297 resolve_symbol (const char *name,
7298 bfd *input_bfd,
7299 struct elf_final_link_info *finfo,
7300 bfd_vma *result,
7301 Elf_Internal_Sym *isymbuf,
7302 size_t locsymcount)
7304 Elf_Internal_Sym *sym;
7305 struct bfd_link_hash_entry *global_entry;
7306 const char *candidate = NULL;
7307 Elf_Internal_Shdr *symtab_hdr;
7308 size_t i;
7310 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7312 for (i = 0; i < locsymcount; ++ i)
7314 sym = isymbuf + i;
7316 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7317 continue;
7319 candidate = bfd_elf_string_from_elf_section (input_bfd,
7320 symtab_hdr->sh_link,
7321 sym->st_name);
7322 #ifdef DEBUG
7323 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7324 name, candidate, (unsigned long) sym->st_value);
7325 #endif
7326 if (candidate && strcmp (candidate, name) == 0)
7328 asection *sec = finfo->sections [i];
7330 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7331 *result += sec->output_offset + sec->output_section->vma;
7332 #ifdef DEBUG
7333 printf ("Found symbol with value %8.8lx\n",
7334 (unsigned long) *result);
7335 #endif
7336 return TRUE;
7340 /* Hmm, haven't found it yet. perhaps it is a global. */
7341 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7342 FALSE, FALSE, TRUE);
7343 if (!global_entry)
7344 return FALSE;
7346 if (global_entry->type == bfd_link_hash_defined
7347 || global_entry->type == bfd_link_hash_defweak)
7349 *result = (global_entry->u.def.value
7350 + global_entry->u.def.section->output_section->vma
7351 + global_entry->u.def.section->output_offset);
7352 #ifdef DEBUG
7353 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7354 global_entry->root.string, (unsigned long) *result);
7355 #endif
7356 return TRUE;
7359 return FALSE;
7362 static bfd_boolean
7363 resolve_section (const char *name,
7364 asection *sections,
7365 bfd_vma *result)
7367 asection *curr;
7368 unsigned int len;
7370 for (curr = sections; curr; curr = curr->next)
7371 if (strcmp (curr->name, name) == 0)
7373 *result = curr->vma;
7374 return TRUE;
7377 /* Hmm. still haven't found it. try pseudo-section names. */
7378 for (curr = sections; curr; curr = curr->next)
7380 len = strlen (curr->name);
7381 if (len > strlen (name))
7382 continue;
7384 if (strncmp (curr->name, name, len) == 0)
7386 if (strncmp (".end", name + len, 4) == 0)
7388 *result = curr->vma + curr->size;
7389 return TRUE;
7392 /* Insert more pseudo-section names here, if you like. */
7396 return FALSE;
7399 static void
7400 undefined_reference (const char *reftype, const char *name)
7402 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7403 reftype, name);
7406 static bfd_boolean
7407 eval_symbol (bfd_vma *result,
7408 const char **symp,
7409 bfd *input_bfd,
7410 struct elf_final_link_info *finfo,
7411 bfd_vma dot,
7412 Elf_Internal_Sym *isymbuf,
7413 size_t locsymcount,
7414 int signed_p)
7416 size_t len;
7417 size_t symlen;
7418 bfd_vma a;
7419 bfd_vma b;
7420 char symbuf[4096];
7421 const char *sym = *symp;
7422 const char *symend;
7423 bfd_boolean symbol_is_section = FALSE;
7425 len = strlen (sym);
7426 symend = sym + len;
7428 if (len < 1 || len > sizeof (symbuf))
7430 bfd_set_error (bfd_error_invalid_operation);
7431 return FALSE;
7434 switch (* sym)
7436 case '.':
7437 *result = dot;
7438 *symp = sym + 1;
7439 return TRUE;
7441 case '#':
7442 ++sym;
7443 *result = strtoul (sym, (char **) symp, 16);
7444 return TRUE;
7446 case 'S':
7447 symbol_is_section = TRUE;
7448 case 's':
7449 ++sym;
7450 symlen = strtol (sym, (char **) symp, 10);
7451 sym = *symp + 1; /* Skip the trailing ':'. */
7453 if (symend < sym || symlen + 1 > sizeof (symbuf))
7455 bfd_set_error (bfd_error_invalid_operation);
7456 return FALSE;
7459 memcpy (symbuf, sym, symlen);
7460 symbuf[symlen] = '\0';
7461 *symp = sym + symlen;
7463 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7464 the symbol as a section, or vice-versa. so we're pretty liberal in our
7465 interpretation here; section means "try section first", not "must be a
7466 section", and likewise with symbol. */
7468 if (symbol_is_section)
7470 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7471 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7472 isymbuf, locsymcount))
7474 undefined_reference ("section", symbuf);
7475 return FALSE;
7478 else
7480 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7481 isymbuf, locsymcount)
7482 && !resolve_section (symbuf, finfo->output_bfd->sections,
7483 result))
7485 undefined_reference ("symbol", symbuf);
7486 return FALSE;
7490 return TRUE;
7492 /* All that remains are operators. */
7494 #define UNARY_OP(op) \
7495 if (strncmp (sym, #op, strlen (#op)) == 0) \
7497 sym += strlen (#op); \
7498 if (*sym == ':') \
7499 ++sym; \
7500 *symp = sym; \
7501 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7502 isymbuf, locsymcount, signed_p)) \
7503 return FALSE; \
7504 if (signed_p) \
7505 *result = op ((bfd_signed_vma) a); \
7506 else \
7507 *result = op a; \
7508 return TRUE; \
7511 #define BINARY_OP(op) \
7512 if (strncmp (sym, #op, strlen (#op)) == 0) \
7514 sym += strlen (#op); \
7515 if (*sym == ':') \
7516 ++sym; \
7517 *symp = sym; \
7518 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7519 isymbuf, locsymcount, signed_p)) \
7520 return FALSE; \
7521 ++*symp; \
7522 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7523 isymbuf, locsymcount, signed_p)) \
7524 return FALSE; \
7525 if (signed_p) \
7526 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7527 else \
7528 *result = a op b; \
7529 return TRUE; \
7532 default:
7533 UNARY_OP (0-);
7534 BINARY_OP (<<);
7535 BINARY_OP (>>);
7536 BINARY_OP (==);
7537 BINARY_OP (!=);
7538 BINARY_OP (<=);
7539 BINARY_OP (>=);
7540 BINARY_OP (&&);
7541 BINARY_OP (||);
7542 UNARY_OP (~);
7543 UNARY_OP (!);
7544 BINARY_OP (*);
7545 BINARY_OP (/);
7546 BINARY_OP (%);
7547 BINARY_OP (^);
7548 BINARY_OP (|);
7549 BINARY_OP (&);
7550 BINARY_OP (+);
7551 BINARY_OP (-);
7552 BINARY_OP (<);
7553 BINARY_OP (>);
7554 #undef UNARY_OP
7555 #undef BINARY_OP
7556 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7557 bfd_set_error (bfd_error_invalid_operation);
7558 return FALSE;
7562 static void
7563 put_value (bfd_vma size,
7564 unsigned long chunksz,
7565 bfd *input_bfd,
7566 bfd_vma x,
7567 bfd_byte *location)
7569 location += (size - chunksz);
7571 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7573 switch (chunksz)
7575 default:
7576 case 0:
7577 abort ();
7578 case 1:
7579 bfd_put_8 (input_bfd, x, location);
7580 break;
7581 case 2:
7582 bfd_put_16 (input_bfd, x, location);
7583 break;
7584 case 4:
7585 bfd_put_32 (input_bfd, x, location);
7586 break;
7587 case 8:
7588 #ifdef BFD64
7589 bfd_put_64 (input_bfd, x, location);
7590 #else
7591 abort ();
7592 #endif
7593 break;
7598 static bfd_vma
7599 get_value (bfd_vma size,
7600 unsigned long chunksz,
7601 bfd *input_bfd,
7602 bfd_byte *location)
7604 bfd_vma x = 0;
7606 for (; size; size -= chunksz, location += chunksz)
7608 switch (chunksz)
7610 default:
7611 case 0:
7612 abort ();
7613 case 1:
7614 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7615 break;
7616 case 2:
7617 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7618 break;
7619 case 4:
7620 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7621 break;
7622 case 8:
7623 #ifdef BFD64
7624 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7625 #else
7626 abort ();
7627 #endif
7628 break;
7631 return x;
7634 static void
7635 decode_complex_addend (unsigned long *start, /* in bits */
7636 unsigned long *oplen, /* in bits */
7637 unsigned long *len, /* in bits */
7638 unsigned long *wordsz, /* in bytes */
7639 unsigned long *chunksz, /* in bytes */
7640 unsigned long *lsb0_p,
7641 unsigned long *signed_p,
7642 unsigned long *trunc_p,
7643 unsigned long encoded)
7645 * start = encoded & 0x3F;
7646 * len = (encoded >> 6) & 0x3F;
7647 * oplen = (encoded >> 12) & 0x3F;
7648 * wordsz = (encoded >> 18) & 0xF;
7649 * chunksz = (encoded >> 22) & 0xF;
7650 * lsb0_p = (encoded >> 27) & 1;
7651 * signed_p = (encoded >> 28) & 1;
7652 * trunc_p = (encoded >> 29) & 1;
7655 bfd_reloc_status_type
7656 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7657 asection *input_section ATTRIBUTE_UNUSED,
7658 bfd_byte *contents,
7659 Elf_Internal_Rela *rel,
7660 bfd_vma relocation)
7662 bfd_vma shift, x, mask;
7663 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7664 bfd_reloc_status_type r;
7666 /* Perform this reloc, since it is complex.
7667 (this is not to say that it necessarily refers to a complex
7668 symbol; merely that it is a self-describing CGEN based reloc.
7669 i.e. the addend has the complete reloc information (bit start, end,
7670 word size, etc) encoded within it.). */
7672 decode_complex_addend (&start, &oplen, &len, &wordsz,
7673 &chunksz, &lsb0_p, &signed_p,
7674 &trunc_p, rel->r_addend);
7676 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7678 if (lsb0_p)
7679 shift = (start + 1) - len;
7680 else
7681 shift = (8 * wordsz) - (start + len);
7683 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7685 #ifdef DEBUG
7686 printf ("Doing complex reloc: "
7687 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7688 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7689 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7690 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7691 oplen, x, mask, relocation);
7692 #endif
7694 r = bfd_reloc_ok;
7695 if (! trunc_p)
7696 /* Now do an overflow check. */
7697 r = bfd_check_overflow ((signed_p
7698 ? complain_overflow_signed
7699 : complain_overflow_unsigned),
7700 len, 0, (8 * wordsz),
7701 relocation);
7703 /* Do the deed. */
7704 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7706 #ifdef DEBUG
7707 printf (" relocation: %8.8lx\n"
7708 " shifted mask: %8.8lx\n"
7709 " shifted/masked reloc: %8.8lx\n"
7710 " result: %8.8lx\n",
7711 relocation, (mask << shift),
7712 ((relocation & mask) << shift), x);
7713 #endif
7714 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7715 return r;
7718 /* When performing a relocatable link, the input relocations are
7719 preserved. But, if they reference global symbols, the indices
7720 referenced must be updated. Update all the relocations in
7721 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7723 static void
7724 elf_link_adjust_relocs (bfd *abfd,
7725 Elf_Internal_Shdr *rel_hdr,
7726 unsigned int count,
7727 struct elf_link_hash_entry **rel_hash)
7729 unsigned int i;
7730 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7731 bfd_byte *erela;
7732 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7733 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7734 bfd_vma r_type_mask;
7735 int r_sym_shift;
7737 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7739 swap_in = bed->s->swap_reloc_in;
7740 swap_out = bed->s->swap_reloc_out;
7742 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7744 swap_in = bed->s->swap_reloca_in;
7745 swap_out = bed->s->swap_reloca_out;
7747 else
7748 abort ();
7750 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7751 abort ();
7753 if (bed->s->arch_size == 32)
7755 r_type_mask = 0xff;
7756 r_sym_shift = 8;
7758 else
7760 r_type_mask = 0xffffffff;
7761 r_sym_shift = 32;
7764 erela = rel_hdr->contents;
7765 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7767 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7768 unsigned int j;
7770 if (*rel_hash == NULL)
7771 continue;
7773 BFD_ASSERT ((*rel_hash)->indx >= 0);
7775 (*swap_in) (abfd, erela, irela);
7776 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7777 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7778 | (irela[j].r_info & r_type_mask));
7779 (*swap_out) (abfd, irela, erela);
7783 struct elf_link_sort_rela
7785 union {
7786 bfd_vma offset;
7787 bfd_vma sym_mask;
7788 } u;
7789 enum elf_reloc_type_class type;
7790 /* We use this as an array of size int_rels_per_ext_rel. */
7791 Elf_Internal_Rela rela[1];
7794 static int
7795 elf_link_sort_cmp1 (const void *A, const void *B)
7797 const struct elf_link_sort_rela *a = A;
7798 const struct elf_link_sort_rela *b = B;
7799 int relativea, relativeb;
7801 relativea = a->type == reloc_class_relative;
7802 relativeb = b->type == reloc_class_relative;
7804 if (relativea < relativeb)
7805 return 1;
7806 if (relativea > relativeb)
7807 return -1;
7808 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7809 return -1;
7810 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7811 return 1;
7812 if (a->rela->r_offset < b->rela->r_offset)
7813 return -1;
7814 if (a->rela->r_offset > b->rela->r_offset)
7815 return 1;
7816 return 0;
7819 static int
7820 elf_link_sort_cmp2 (const void *A, const void *B)
7822 const struct elf_link_sort_rela *a = A;
7823 const struct elf_link_sort_rela *b = B;
7824 int copya, copyb;
7826 if (a->u.offset < b->u.offset)
7827 return -1;
7828 if (a->u.offset > b->u.offset)
7829 return 1;
7830 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7831 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7832 if (copya < copyb)
7833 return -1;
7834 if (copya > copyb)
7835 return 1;
7836 if (a->rela->r_offset < b->rela->r_offset)
7837 return -1;
7838 if (a->rela->r_offset > b->rela->r_offset)
7839 return 1;
7840 return 0;
7843 static size_t
7844 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7846 asection *dynamic_relocs;
7847 asection *rela_dyn;
7848 asection *rel_dyn;
7849 bfd_size_type count, size;
7850 size_t i, ret, sort_elt, ext_size;
7851 bfd_byte *sort, *s_non_relative, *p;
7852 struct elf_link_sort_rela *sq;
7853 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7854 int i2e = bed->s->int_rels_per_ext_rel;
7855 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7856 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7857 struct bfd_link_order *lo;
7858 bfd_vma r_sym_mask;
7859 bfd_boolean use_rela;
7861 /* Find a dynamic reloc section. */
7862 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7863 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7864 if (rela_dyn != NULL && rela_dyn->size > 0
7865 && rel_dyn != NULL && rel_dyn->size > 0)
7867 bfd_boolean use_rela_initialised = FALSE;
7869 /* This is just here to stop gcc from complaining.
7870 It's initialization checking code is not perfect. */
7871 use_rela = TRUE;
7873 /* Both sections are present. Examine the sizes
7874 of the indirect sections to help us choose. */
7875 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7876 if (lo->type == bfd_indirect_link_order)
7878 asection *o = lo->u.indirect.section;
7880 if ((o->size % bed->s->sizeof_rela) == 0)
7882 if ((o->size % bed->s->sizeof_rel) == 0)
7883 /* Section size is divisible by both rel and rela sizes.
7884 It is of no help to us. */
7886 else
7888 /* Section size is only divisible by rela. */
7889 if (use_rela_initialised && (use_rela == FALSE))
7891 _bfd_error_handler
7892 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7893 bfd_set_error (bfd_error_invalid_operation);
7894 return 0;
7896 else
7898 use_rela = TRUE;
7899 use_rela_initialised = TRUE;
7903 else if ((o->size % bed->s->sizeof_rel) == 0)
7905 /* Section size is only divisible by rel. */
7906 if (use_rela_initialised && (use_rela == TRUE))
7908 _bfd_error_handler
7909 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7910 bfd_set_error (bfd_error_invalid_operation);
7911 return 0;
7913 else
7915 use_rela = FALSE;
7916 use_rela_initialised = TRUE;
7919 else
7921 /* The section size is not divisible by either - something is wrong. */
7922 _bfd_error_handler
7923 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7924 bfd_set_error (bfd_error_invalid_operation);
7925 return 0;
7929 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7930 if (lo->type == bfd_indirect_link_order)
7932 asection *o = lo->u.indirect.section;
7934 if ((o->size % bed->s->sizeof_rela) == 0)
7936 if ((o->size % bed->s->sizeof_rel) == 0)
7937 /* Section size is divisible by both rel and rela sizes.
7938 It is of no help to us. */
7940 else
7942 /* Section size is only divisible by rela. */
7943 if (use_rela_initialised && (use_rela == FALSE))
7945 _bfd_error_handler
7946 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7947 bfd_set_error (bfd_error_invalid_operation);
7948 return 0;
7950 else
7952 use_rela = TRUE;
7953 use_rela_initialised = TRUE;
7957 else if ((o->size % bed->s->sizeof_rel) == 0)
7959 /* Section size is only divisible by rel. */
7960 if (use_rela_initialised && (use_rela == TRUE))
7962 _bfd_error_handler
7963 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7964 bfd_set_error (bfd_error_invalid_operation);
7965 return 0;
7967 else
7969 use_rela = FALSE;
7970 use_rela_initialised = TRUE;
7973 else
7975 /* The section size is not divisible by either - something is wrong. */
7976 _bfd_error_handler
7977 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7978 bfd_set_error (bfd_error_invalid_operation);
7979 return 0;
7983 if (! use_rela_initialised)
7984 /* Make a guess. */
7985 use_rela = TRUE;
7987 else if (rela_dyn != NULL && rela_dyn->size > 0)
7988 use_rela = TRUE;
7989 else if (rel_dyn != NULL && rel_dyn->size > 0)
7990 use_rela = FALSE;
7991 else
7992 return 0;
7994 if (use_rela)
7996 dynamic_relocs = rela_dyn;
7997 ext_size = bed->s->sizeof_rela;
7998 swap_in = bed->s->swap_reloca_in;
7999 swap_out = bed->s->swap_reloca_out;
8001 else
8003 dynamic_relocs = rel_dyn;
8004 ext_size = bed->s->sizeof_rel;
8005 swap_in = bed->s->swap_reloc_in;
8006 swap_out = bed->s->swap_reloc_out;
8009 size = 0;
8010 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8011 if (lo->type == bfd_indirect_link_order)
8012 size += lo->u.indirect.section->size;
8014 if (size != dynamic_relocs->size)
8015 return 0;
8017 sort_elt = (sizeof (struct elf_link_sort_rela)
8018 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8020 count = dynamic_relocs->size / ext_size;
8021 sort = bfd_zmalloc (sort_elt * count);
8023 if (sort == NULL)
8025 (*info->callbacks->warning)
8026 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8027 return 0;
8030 if (bed->s->arch_size == 32)
8031 r_sym_mask = ~(bfd_vma) 0xff;
8032 else
8033 r_sym_mask = ~(bfd_vma) 0xffffffff;
8035 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8036 if (lo->type == bfd_indirect_link_order)
8038 bfd_byte *erel, *erelend;
8039 asection *o = lo->u.indirect.section;
8041 if (o->contents == NULL && o->size != 0)
8043 /* This is a reloc section that is being handled as a normal
8044 section. See bfd_section_from_shdr. We can't combine
8045 relocs in this case. */
8046 free (sort);
8047 return 0;
8049 erel = o->contents;
8050 erelend = o->contents + o->size;
8051 p = sort + o->output_offset / ext_size * sort_elt;
8053 while (erel < erelend)
8055 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8057 (*swap_in) (abfd, erel, s->rela);
8058 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8059 s->u.sym_mask = r_sym_mask;
8060 p += sort_elt;
8061 erel += ext_size;
8065 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8067 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8069 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8070 if (s->type != reloc_class_relative)
8071 break;
8073 ret = i;
8074 s_non_relative = p;
8076 sq = (struct elf_link_sort_rela *) s_non_relative;
8077 for (; i < count; i++, p += sort_elt)
8079 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8080 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8081 sq = sp;
8082 sp->u.offset = sq->rela->r_offset;
8085 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8087 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8088 if (lo->type == bfd_indirect_link_order)
8090 bfd_byte *erel, *erelend;
8091 asection *o = lo->u.indirect.section;
8093 erel = o->contents;
8094 erelend = o->contents + o->size;
8095 p = sort + o->output_offset / ext_size * sort_elt;
8096 while (erel < erelend)
8098 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8099 (*swap_out) (abfd, s->rela, erel);
8100 p += sort_elt;
8101 erel += ext_size;
8105 free (sort);
8106 *psec = dynamic_relocs;
8107 return ret;
8110 /* Flush the output symbols to the file. */
8112 static bfd_boolean
8113 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8114 const struct elf_backend_data *bed)
8116 if (finfo->symbuf_count > 0)
8118 Elf_Internal_Shdr *hdr;
8119 file_ptr pos;
8120 bfd_size_type amt;
8122 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8123 pos = hdr->sh_offset + hdr->sh_size;
8124 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8125 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8126 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8127 return FALSE;
8129 hdr->sh_size += amt;
8130 finfo->symbuf_count = 0;
8133 return TRUE;
8136 /* Add a symbol to the output symbol table. */
8138 static bfd_boolean
8139 elf_link_output_sym (struct elf_final_link_info *finfo,
8140 const char *name,
8141 Elf_Internal_Sym *elfsym,
8142 asection *input_sec,
8143 struct elf_link_hash_entry *h)
8145 bfd_byte *dest;
8146 Elf_External_Sym_Shndx *destshndx;
8147 bfd_boolean (*output_symbol_hook)
8148 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8149 struct elf_link_hash_entry *);
8150 const struct elf_backend_data *bed;
8152 bed = get_elf_backend_data (finfo->output_bfd);
8153 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8154 if (output_symbol_hook != NULL)
8156 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
8157 return FALSE;
8160 if (name == NULL || *name == '\0')
8161 elfsym->st_name = 0;
8162 else if (input_sec->flags & SEC_EXCLUDE)
8163 elfsym->st_name = 0;
8164 else
8166 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8167 name, TRUE, FALSE);
8168 if (elfsym->st_name == (unsigned long) -1)
8169 return FALSE;
8172 if (finfo->symbuf_count >= finfo->symbuf_size)
8174 if (! elf_link_flush_output_syms (finfo, bed))
8175 return FALSE;
8178 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8179 destshndx = finfo->symshndxbuf;
8180 if (destshndx != NULL)
8182 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8184 bfd_size_type amt;
8186 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8187 destshndx = bfd_realloc (destshndx, amt * 2);
8188 if (destshndx == NULL)
8189 return FALSE;
8190 finfo->symshndxbuf = destshndx;
8191 memset ((char *) destshndx + amt, 0, amt);
8192 finfo->shndxbuf_size *= 2;
8194 destshndx += bfd_get_symcount (finfo->output_bfd);
8197 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8198 finfo->symbuf_count += 1;
8199 bfd_get_symcount (finfo->output_bfd) += 1;
8201 return TRUE;
8204 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8206 static bfd_boolean
8207 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8209 if (sym->st_shndx > SHN_HIRESERVE)
8211 /* The gABI doesn't support dynamic symbols in output sections
8212 beyond 64k. */
8213 (*_bfd_error_handler)
8214 (_("%B: Too many sections: %d (>= %d)"),
8215 abfd, bfd_count_sections (abfd), SHN_LORESERVE);
8216 bfd_set_error (bfd_error_nonrepresentable_section);
8217 return FALSE;
8219 return TRUE;
8222 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8223 allowing an unsatisfied unversioned symbol in the DSO to match a
8224 versioned symbol that would normally require an explicit version.
8225 We also handle the case that a DSO references a hidden symbol
8226 which may be satisfied by a versioned symbol in another DSO. */
8228 static bfd_boolean
8229 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8230 const struct elf_backend_data *bed,
8231 struct elf_link_hash_entry *h)
8233 bfd *abfd;
8234 struct elf_link_loaded_list *loaded;
8236 if (!is_elf_hash_table (info->hash))
8237 return FALSE;
8239 switch (h->root.type)
8241 default:
8242 abfd = NULL;
8243 break;
8245 case bfd_link_hash_undefined:
8246 case bfd_link_hash_undefweak:
8247 abfd = h->root.u.undef.abfd;
8248 if ((abfd->flags & DYNAMIC) == 0
8249 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8250 return FALSE;
8251 break;
8253 case bfd_link_hash_defined:
8254 case bfd_link_hash_defweak:
8255 abfd = h->root.u.def.section->owner;
8256 break;
8258 case bfd_link_hash_common:
8259 abfd = h->root.u.c.p->section->owner;
8260 break;
8262 BFD_ASSERT (abfd != NULL);
8264 for (loaded = elf_hash_table (info)->loaded;
8265 loaded != NULL;
8266 loaded = loaded->next)
8268 bfd *input;
8269 Elf_Internal_Shdr *hdr;
8270 bfd_size_type symcount;
8271 bfd_size_type extsymcount;
8272 bfd_size_type extsymoff;
8273 Elf_Internal_Shdr *versymhdr;
8274 Elf_Internal_Sym *isym;
8275 Elf_Internal_Sym *isymend;
8276 Elf_Internal_Sym *isymbuf;
8277 Elf_External_Versym *ever;
8278 Elf_External_Versym *extversym;
8280 input = loaded->abfd;
8282 /* We check each DSO for a possible hidden versioned definition. */
8283 if (input == abfd
8284 || (input->flags & DYNAMIC) == 0
8285 || elf_dynversym (input) == 0)
8286 continue;
8288 hdr = &elf_tdata (input)->dynsymtab_hdr;
8290 symcount = hdr->sh_size / bed->s->sizeof_sym;
8291 if (elf_bad_symtab (input))
8293 extsymcount = symcount;
8294 extsymoff = 0;
8296 else
8298 extsymcount = symcount - hdr->sh_info;
8299 extsymoff = hdr->sh_info;
8302 if (extsymcount == 0)
8303 continue;
8305 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8306 NULL, NULL, NULL);
8307 if (isymbuf == NULL)
8308 return FALSE;
8310 /* Read in any version definitions. */
8311 versymhdr = &elf_tdata (input)->dynversym_hdr;
8312 extversym = bfd_malloc (versymhdr->sh_size);
8313 if (extversym == NULL)
8314 goto error_ret;
8316 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8317 || (bfd_bread (extversym, versymhdr->sh_size, input)
8318 != versymhdr->sh_size))
8320 free (extversym);
8321 error_ret:
8322 free (isymbuf);
8323 return FALSE;
8326 ever = extversym + extsymoff;
8327 isymend = isymbuf + extsymcount;
8328 for (isym = isymbuf; isym < isymend; isym++, ever++)
8330 const char *name;
8331 Elf_Internal_Versym iver;
8332 unsigned short version_index;
8334 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8335 || isym->st_shndx == SHN_UNDEF)
8336 continue;
8338 name = bfd_elf_string_from_elf_section (input,
8339 hdr->sh_link,
8340 isym->st_name);
8341 if (strcmp (name, h->root.root.string) != 0)
8342 continue;
8344 _bfd_elf_swap_versym_in (input, ever, &iver);
8346 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
8348 /* If we have a non-hidden versioned sym, then it should
8349 have provided a definition for the undefined sym. */
8350 abort ();
8353 version_index = iver.vs_vers & VERSYM_VERSION;
8354 if (version_index == 1 || version_index == 2)
8356 /* This is the base or first version. We can use it. */
8357 free (extversym);
8358 free (isymbuf);
8359 return TRUE;
8363 free (extversym);
8364 free (isymbuf);
8367 return FALSE;
8370 /* Add an external symbol to the symbol table. This is called from
8371 the hash table traversal routine. When generating a shared object,
8372 we go through the symbol table twice. The first time we output
8373 anything that might have been forced to local scope in a version
8374 script. The second time we output the symbols that are still
8375 global symbols. */
8377 static bfd_boolean
8378 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8380 struct elf_outext_info *eoinfo = data;
8381 struct elf_final_link_info *finfo = eoinfo->finfo;
8382 bfd_boolean strip;
8383 Elf_Internal_Sym sym;
8384 asection *input_sec;
8385 const struct elf_backend_data *bed;
8387 if (h->root.type == bfd_link_hash_warning)
8389 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8390 if (h->root.type == bfd_link_hash_new)
8391 return TRUE;
8394 /* Decide whether to output this symbol in this pass. */
8395 if (eoinfo->localsyms)
8397 if (!h->forced_local)
8398 return TRUE;
8400 else
8402 if (h->forced_local)
8403 return TRUE;
8406 bed = get_elf_backend_data (finfo->output_bfd);
8408 if (h->root.type == bfd_link_hash_undefined)
8410 /* If we have an undefined symbol reference here then it must have
8411 come from a shared library that is being linked in. (Undefined
8412 references in regular files have already been handled). */
8413 bfd_boolean ignore_undef = FALSE;
8415 /* Some symbols may be special in that the fact that they're
8416 undefined can be safely ignored - let backend determine that. */
8417 if (bed->elf_backend_ignore_undef_symbol)
8418 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8420 /* If we are reporting errors for this situation then do so now. */
8421 if (ignore_undef == FALSE
8422 && h->ref_dynamic
8423 && ! h->ref_regular
8424 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8425 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8427 if (! (finfo->info->callbacks->undefined_symbol
8428 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
8429 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8431 eoinfo->failed = TRUE;
8432 return FALSE;
8437 /* We should also warn if a forced local symbol is referenced from
8438 shared libraries. */
8439 if (! finfo->info->relocatable
8440 && (! finfo->info->shared)
8441 && h->forced_local
8442 && h->ref_dynamic
8443 && !h->dynamic_def
8444 && !h->dynamic_weak
8445 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8447 (*_bfd_error_handler)
8448 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8449 finfo->output_bfd,
8450 h->root.u.def.section == bfd_abs_section_ptr
8451 ? finfo->output_bfd : h->root.u.def.section->owner,
8452 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
8453 ? "internal"
8454 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
8455 ? "hidden" : "local",
8456 h->root.root.string);
8457 eoinfo->failed = TRUE;
8458 return FALSE;
8461 /* We don't want to output symbols that have never been mentioned by
8462 a regular file, or that we have been told to strip. However, if
8463 h->indx is set to -2, the symbol is used by a reloc and we must
8464 output it. */
8465 if (h->indx == -2)
8466 strip = FALSE;
8467 else if ((h->def_dynamic
8468 || h->ref_dynamic
8469 || h->root.type == bfd_link_hash_new)
8470 && !h->def_regular
8471 && !h->ref_regular)
8472 strip = TRUE;
8473 else if (finfo->info->strip == strip_all)
8474 strip = TRUE;
8475 else if (finfo->info->strip == strip_some
8476 && bfd_hash_lookup (finfo->info->keep_hash,
8477 h->root.root.string, FALSE, FALSE) == NULL)
8478 strip = TRUE;
8479 else if (finfo->info->strip_discarded
8480 && (h->root.type == bfd_link_hash_defined
8481 || h->root.type == bfd_link_hash_defweak)
8482 && elf_discarded_section (h->root.u.def.section))
8483 strip = TRUE;
8484 else
8485 strip = FALSE;
8487 /* If we're stripping it, and it's not a dynamic symbol, there's
8488 nothing else to do unless it is a forced local symbol. */
8489 if (strip
8490 && h->dynindx == -1
8491 && !h->forced_local)
8492 return TRUE;
8494 sym.st_value = 0;
8495 sym.st_size = h->size;
8496 sym.st_other = h->other;
8497 if (h->forced_local)
8498 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8499 else if (h->root.type == bfd_link_hash_undefweak
8500 || h->root.type == bfd_link_hash_defweak)
8501 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8502 else
8503 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8505 switch (h->root.type)
8507 default:
8508 case bfd_link_hash_new:
8509 case bfd_link_hash_warning:
8510 abort ();
8511 return FALSE;
8513 case bfd_link_hash_undefined:
8514 case bfd_link_hash_undefweak:
8515 input_sec = bfd_und_section_ptr;
8516 sym.st_shndx = SHN_UNDEF;
8517 break;
8519 case bfd_link_hash_defined:
8520 case bfd_link_hash_defweak:
8522 input_sec = h->root.u.def.section;
8523 if (input_sec->output_section != NULL)
8525 sym.st_shndx =
8526 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8527 input_sec->output_section);
8528 if (sym.st_shndx == SHN_BAD)
8530 (*_bfd_error_handler)
8531 (_("%B: could not find output section %A for input section %A"),
8532 finfo->output_bfd, input_sec->output_section, input_sec);
8533 eoinfo->failed = TRUE;
8534 return FALSE;
8537 /* ELF symbols in relocatable files are section relative,
8538 but in nonrelocatable files they are virtual
8539 addresses. */
8540 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8541 if (! finfo->info->relocatable)
8543 sym.st_value += input_sec->output_section->vma;
8544 if (h->type == STT_TLS)
8546 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8547 if (tls_sec != NULL)
8548 sym.st_value -= tls_sec->vma;
8549 else
8551 /* The TLS section may have been garbage collected. */
8552 BFD_ASSERT (finfo->info->gc_sections
8553 && !input_sec->gc_mark);
8558 else
8560 BFD_ASSERT (input_sec->owner == NULL
8561 || (input_sec->owner->flags & DYNAMIC) != 0);
8562 sym.st_shndx = SHN_UNDEF;
8563 input_sec = bfd_und_section_ptr;
8566 break;
8568 case bfd_link_hash_common:
8569 input_sec = h->root.u.c.p->section;
8570 sym.st_shndx = bed->common_section_index (input_sec);
8571 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8572 break;
8574 case bfd_link_hash_indirect:
8575 /* These symbols are created by symbol versioning. They point
8576 to the decorated version of the name. For example, if the
8577 symbol foo@@GNU_1.2 is the default, which should be used when
8578 foo is used with no version, then we add an indirect symbol
8579 foo which points to foo@@GNU_1.2. We ignore these symbols,
8580 since the indirected symbol is already in the hash table. */
8581 return TRUE;
8584 /* Give the processor backend a chance to tweak the symbol value,
8585 and also to finish up anything that needs to be done for this
8586 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8587 forced local syms when non-shared is due to a historical quirk. */
8588 if ((h->dynindx != -1
8589 || h->forced_local)
8590 && ((finfo->info->shared
8591 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8592 || h->root.type != bfd_link_hash_undefweak))
8593 || !h->forced_local)
8594 && elf_hash_table (finfo->info)->dynamic_sections_created)
8596 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8597 (finfo->output_bfd, finfo->info, h, &sym)))
8599 eoinfo->failed = TRUE;
8600 return FALSE;
8604 /* If we are marking the symbol as undefined, and there are no
8605 non-weak references to this symbol from a regular object, then
8606 mark the symbol as weak undefined; if there are non-weak
8607 references, mark the symbol as strong. We can't do this earlier,
8608 because it might not be marked as undefined until the
8609 finish_dynamic_symbol routine gets through with it. */
8610 if (sym.st_shndx == SHN_UNDEF
8611 && h->ref_regular
8612 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8613 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8615 int bindtype;
8617 if (h->ref_regular_nonweak)
8618 bindtype = STB_GLOBAL;
8619 else
8620 bindtype = STB_WEAK;
8621 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8624 /* If a non-weak symbol with non-default visibility is not defined
8625 locally, it is a fatal error. */
8626 if (! finfo->info->relocatable
8627 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8628 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8629 && h->root.type == bfd_link_hash_undefined
8630 && !h->def_regular)
8632 (*_bfd_error_handler)
8633 (_("%B: %s symbol `%s' isn't defined"),
8634 finfo->output_bfd,
8635 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8636 ? "protected"
8637 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8638 ? "internal" : "hidden",
8639 h->root.root.string);
8640 eoinfo->failed = TRUE;
8641 return FALSE;
8644 /* If this symbol should be put in the .dynsym section, then put it
8645 there now. We already know the symbol index. We also fill in
8646 the entry in the .hash section. */
8647 if (h->dynindx != -1
8648 && elf_hash_table (finfo->info)->dynamic_sections_created)
8650 bfd_byte *esym;
8652 sym.st_name = h->dynstr_index;
8653 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8654 if (! check_dynsym (finfo->output_bfd, &sym))
8656 eoinfo->failed = TRUE;
8657 return FALSE;
8659 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8661 if (finfo->hash_sec != NULL)
8663 size_t hash_entry_size;
8664 bfd_byte *bucketpos;
8665 bfd_vma chain;
8666 size_t bucketcount;
8667 size_t bucket;
8669 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8670 bucket = h->u.elf_hash_value % bucketcount;
8672 hash_entry_size
8673 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8674 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8675 + (bucket + 2) * hash_entry_size);
8676 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8677 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8678 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8679 ((bfd_byte *) finfo->hash_sec->contents
8680 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8683 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8685 Elf_Internal_Versym iversym;
8686 Elf_External_Versym *eversym;
8688 if (!h->def_regular)
8690 if (h->verinfo.verdef == NULL)
8691 iversym.vs_vers = 0;
8692 else
8693 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8695 else
8697 if (h->verinfo.vertree == NULL)
8698 iversym.vs_vers = 1;
8699 else
8700 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8701 if (finfo->info->create_default_symver)
8702 iversym.vs_vers++;
8705 if (h->hidden)
8706 iversym.vs_vers |= VERSYM_HIDDEN;
8708 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8709 eversym += h->dynindx;
8710 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8714 /* If we're stripping it, then it was just a dynamic symbol, and
8715 there's nothing else to do. */
8716 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8717 return TRUE;
8719 h->indx = bfd_get_symcount (finfo->output_bfd);
8721 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8723 eoinfo->failed = TRUE;
8724 return FALSE;
8727 return TRUE;
8730 /* Return TRUE if special handling is done for relocs in SEC against
8731 symbols defined in discarded sections. */
8733 static bfd_boolean
8734 elf_section_ignore_discarded_relocs (asection *sec)
8736 const struct elf_backend_data *bed;
8738 switch (sec->sec_info_type)
8740 case ELF_INFO_TYPE_STABS:
8741 case ELF_INFO_TYPE_EH_FRAME:
8742 return TRUE;
8743 default:
8744 break;
8747 bed = get_elf_backend_data (sec->owner);
8748 if (bed->elf_backend_ignore_discarded_relocs != NULL
8749 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8750 return TRUE;
8752 return FALSE;
8755 /* Return a mask saying how ld should treat relocations in SEC against
8756 symbols defined in discarded sections. If this function returns
8757 COMPLAIN set, ld will issue a warning message. If this function
8758 returns PRETEND set, and the discarded section was link-once and the
8759 same size as the kept link-once section, ld will pretend that the
8760 symbol was actually defined in the kept section. Otherwise ld will
8761 zero the reloc (at least that is the intent, but some cooperation by
8762 the target dependent code is needed, particularly for REL targets). */
8764 unsigned int
8765 _bfd_elf_default_action_discarded (asection *sec)
8767 if (sec->flags & SEC_DEBUGGING)
8768 return PRETEND;
8770 if (strcmp (".eh_frame", sec->name) == 0)
8771 return 0;
8773 if (strcmp (".gcc_except_table", sec->name) == 0)
8774 return 0;
8776 return COMPLAIN | PRETEND;
8779 /* Find a match between a section and a member of a section group. */
8781 static asection *
8782 match_group_member (asection *sec, asection *group,
8783 struct bfd_link_info *info)
8785 asection *first = elf_next_in_group (group);
8786 asection *s = first;
8788 while (s != NULL)
8790 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8791 return s;
8793 s = elf_next_in_group (s);
8794 if (s == first)
8795 break;
8798 return NULL;
8801 /* Check if the kept section of a discarded section SEC can be used
8802 to replace it. Return the replacement if it is OK. Otherwise return
8803 NULL. */
8805 asection *
8806 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8808 asection *kept;
8810 kept = sec->kept_section;
8811 if (kept != NULL)
8813 if ((kept->flags & SEC_GROUP) != 0)
8814 kept = match_group_member (sec, kept, info);
8815 if (kept != NULL
8816 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
8817 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
8818 kept = NULL;
8819 sec->kept_section = kept;
8821 return kept;
8824 /* Link an input file into the linker output file. This function
8825 handles all the sections and relocations of the input file at once.
8826 This is so that we only have to read the local symbols once, and
8827 don't have to keep them in memory. */
8829 static bfd_boolean
8830 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8832 int (*relocate_section)
8833 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8834 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8835 bfd *output_bfd;
8836 Elf_Internal_Shdr *symtab_hdr;
8837 size_t locsymcount;
8838 size_t extsymoff;
8839 Elf_Internal_Sym *isymbuf;
8840 Elf_Internal_Sym *isym;
8841 Elf_Internal_Sym *isymend;
8842 long *pindex;
8843 asection **ppsection;
8844 asection *o;
8845 const struct elf_backend_data *bed;
8846 struct elf_link_hash_entry **sym_hashes;
8848 output_bfd = finfo->output_bfd;
8849 bed = get_elf_backend_data (output_bfd);
8850 relocate_section = bed->elf_backend_relocate_section;
8852 /* If this is a dynamic object, we don't want to do anything here:
8853 we don't want the local symbols, and we don't want the section
8854 contents. */
8855 if ((input_bfd->flags & DYNAMIC) != 0)
8856 return TRUE;
8858 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8859 if (elf_bad_symtab (input_bfd))
8861 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8862 extsymoff = 0;
8864 else
8866 locsymcount = symtab_hdr->sh_info;
8867 extsymoff = symtab_hdr->sh_info;
8870 /* Read the local symbols. */
8871 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8872 if (isymbuf == NULL && locsymcount != 0)
8874 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8875 finfo->internal_syms,
8876 finfo->external_syms,
8877 finfo->locsym_shndx);
8878 if (isymbuf == NULL)
8879 return FALSE;
8882 /* Find local symbol sections and adjust values of symbols in
8883 SEC_MERGE sections. Write out those local symbols we know are
8884 going into the output file. */
8885 isymend = isymbuf + locsymcount;
8886 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8887 isym < isymend;
8888 isym++, pindex++, ppsection++)
8890 asection *isec;
8891 const char *name;
8892 Elf_Internal_Sym osym;
8894 *pindex = -1;
8896 if (elf_bad_symtab (input_bfd))
8898 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8900 *ppsection = NULL;
8901 continue;
8905 if (isym->st_shndx == SHN_UNDEF)
8906 isec = bfd_und_section_ptr;
8907 else if (isym->st_shndx < SHN_LORESERVE
8908 || isym->st_shndx > SHN_HIRESERVE)
8910 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8911 if (isec
8912 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
8913 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8914 isym->st_value =
8915 _bfd_merged_section_offset (output_bfd, &isec,
8916 elf_section_data (isec)->sec_info,
8917 isym->st_value);
8919 else if (isym->st_shndx == SHN_ABS)
8920 isec = bfd_abs_section_ptr;
8921 else if (isym->st_shndx == SHN_COMMON)
8922 isec = bfd_com_section_ptr;
8923 else
8925 /* Don't attempt to output symbols with st_shnx in the
8926 reserved range other than SHN_ABS and SHN_COMMON. */
8927 *ppsection = NULL;
8928 continue;
8931 *ppsection = isec;
8933 /* Don't output the first, undefined, symbol. */
8934 if (ppsection == finfo->sections)
8935 continue;
8937 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8939 /* We never output section symbols. Instead, we use the
8940 section symbol of the corresponding section in the output
8941 file. */
8942 continue;
8945 /* If we are stripping all symbols, we don't want to output this
8946 one. */
8947 if (finfo->info->strip == strip_all)
8948 continue;
8950 /* If we are discarding all local symbols, we don't want to
8951 output this one. If we are generating a relocatable output
8952 file, then some of the local symbols may be required by
8953 relocs; we output them below as we discover that they are
8954 needed. */
8955 if (finfo->info->discard == discard_all)
8956 continue;
8958 /* If this symbol is defined in a section which we are
8959 discarding, we don't need to keep it. */
8960 if (isym->st_shndx != SHN_UNDEF
8961 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
8962 && (isec == NULL
8963 || bfd_section_removed_from_list (output_bfd,
8964 isec->output_section)))
8965 continue;
8967 /* Get the name of the symbol. */
8968 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
8969 isym->st_name);
8970 if (name == NULL)
8971 return FALSE;
8973 /* See if we are discarding symbols with this name. */
8974 if ((finfo->info->strip == strip_some
8975 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
8976 == NULL))
8977 || (((finfo->info->discard == discard_sec_merge
8978 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
8979 || finfo->info->discard == discard_l)
8980 && bfd_is_local_label_name (input_bfd, name)))
8981 continue;
8983 /* If we get here, we are going to output this symbol. */
8985 osym = *isym;
8987 /* Adjust the section index for the output file. */
8988 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
8989 isec->output_section);
8990 if (osym.st_shndx == SHN_BAD)
8991 return FALSE;
8993 *pindex = bfd_get_symcount (output_bfd);
8995 /* ELF symbols in relocatable files are section relative, but
8996 in executable files they are virtual addresses. Note that
8997 this code assumes that all ELF sections have an associated
8998 BFD section with a reasonable value for output_offset; below
8999 we assume that they also have a reasonable value for
9000 output_section. Any special sections must be set up to meet
9001 these requirements. */
9002 osym.st_value += isec->output_offset;
9003 if (! finfo->info->relocatable)
9005 osym.st_value += isec->output_section->vma;
9006 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9008 /* STT_TLS symbols are relative to PT_TLS segment base. */
9009 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9010 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9014 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
9015 return FALSE;
9018 /* Relocate the contents of each section. */
9019 sym_hashes = elf_sym_hashes (input_bfd);
9020 for (o = input_bfd->sections; o != NULL; o = o->next)
9022 bfd_byte *contents;
9024 if (! o->linker_mark)
9026 /* This section was omitted from the link. */
9027 continue;
9030 if ((o->flags & SEC_HAS_CONTENTS) == 0
9031 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9032 continue;
9034 if ((o->flags & SEC_LINKER_CREATED) != 0)
9036 /* Section was created by _bfd_elf_link_create_dynamic_sections
9037 or somesuch. */
9038 continue;
9041 /* Get the contents of the section. They have been cached by a
9042 relaxation routine. Note that o is a section in an input
9043 file, so the contents field will not have been set by any of
9044 the routines which work on output files. */
9045 if (elf_section_data (o)->this_hdr.contents != NULL)
9046 contents = elf_section_data (o)->this_hdr.contents;
9047 else
9049 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
9051 contents = finfo->contents;
9052 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
9053 return FALSE;
9056 if ((o->flags & SEC_RELOC) != 0)
9058 Elf_Internal_Rela *internal_relocs;
9059 Elf_Internal_Rela *rel, *relend;
9060 bfd_vma r_type_mask;
9061 int r_sym_shift;
9062 int action_discarded;
9063 int ret;
9065 /* Get the swapped relocs. */
9066 internal_relocs
9067 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9068 finfo->internal_relocs, FALSE);
9069 if (internal_relocs == NULL
9070 && o->reloc_count > 0)
9071 return FALSE;
9073 if (bed->s->arch_size == 32)
9075 r_type_mask = 0xff;
9076 r_sym_shift = 8;
9078 else
9080 r_type_mask = 0xffffffff;
9081 r_sym_shift = 32;
9084 action_discarded = -1;
9085 if (!elf_section_ignore_discarded_relocs (o))
9086 action_discarded = (*bed->action_discarded) (o);
9088 /* Run through the relocs evaluating complex reloc symbols and
9089 looking for relocs against symbols from discarded sections
9090 or section symbols from removed link-once sections.
9091 Complain about relocs against discarded sections. Zero
9092 relocs against removed link-once sections. */
9094 rel = internal_relocs;
9095 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9096 for ( ; rel < relend; rel++)
9098 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9099 unsigned int s_type;
9100 asection **ps, *sec;
9101 struct elf_link_hash_entry *h = NULL;
9102 const char *sym_name;
9104 if (r_symndx == STN_UNDEF)
9105 continue;
9107 if (r_symndx >= locsymcount
9108 || (elf_bad_symtab (input_bfd)
9109 && finfo->sections[r_symndx] == NULL))
9111 h = sym_hashes[r_symndx - extsymoff];
9113 /* Badly formatted input files can contain relocs that
9114 reference non-existant symbols. Check here so that
9115 we do not seg fault. */
9116 if (h == NULL)
9118 char buffer [32];
9120 sprintf_vma (buffer, rel->r_info);
9121 (*_bfd_error_handler)
9122 (_("error: %B contains a reloc (0x%s) for section %A "
9123 "that references a non-existent global symbol"),
9124 input_bfd, o, buffer);
9125 bfd_set_error (bfd_error_bad_value);
9126 return FALSE;
9129 while (h->root.type == bfd_link_hash_indirect
9130 || h->root.type == bfd_link_hash_warning)
9131 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9133 s_type = h->type;
9135 ps = NULL;
9136 if (h->root.type == bfd_link_hash_defined
9137 || h->root.type == bfd_link_hash_defweak)
9138 ps = &h->root.u.def.section;
9140 sym_name = h->root.root.string;
9142 else
9144 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9146 s_type = ELF_ST_TYPE (sym->st_info);
9147 ps = &finfo->sections[r_symndx];
9148 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9149 sym, *ps);
9152 if (s_type == STT_RELC || s_type == STT_SRELC)
9154 bfd_vma val;
9155 bfd_vma dot = (rel->r_offset
9156 + o->output_offset + o->output_section->vma);
9157 #ifdef DEBUG
9158 printf ("Encountered a complex symbol!");
9159 printf (" (input_bfd %s, section %s, reloc %ld\n",
9160 input_bfd->filename, o->name, rel - internal_relocs);
9161 printf (" symbol: idx %8.8lx, name %s\n",
9162 r_symndx, sym_name);
9163 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9164 (unsigned long) rel->r_info,
9165 (unsigned long) rel->r_offset);
9166 #endif
9167 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9168 isymbuf, locsymcount, s_type == STT_SRELC))
9169 return FALSE;
9171 /* Symbol evaluated OK. Update to absolute value. */
9172 set_symbol_value (input_bfd, isymbuf, locsymcount,
9173 r_symndx, val);
9174 continue;
9177 if (action_discarded != -1 && ps != NULL)
9179 /* Complain if the definition comes from a
9180 discarded section. */
9181 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9183 BFD_ASSERT (r_symndx != 0);
9184 if (action_discarded & COMPLAIN)
9185 (*finfo->info->callbacks->einfo)
9186 (_("%X`%s' referenced in section `%A' of %B: "
9187 "defined in discarded section `%A' of %B\n"),
9188 sym_name, o, input_bfd, sec, sec->owner);
9190 /* Try to do the best we can to support buggy old
9191 versions of gcc. Pretend that the symbol is
9192 really defined in the kept linkonce section.
9193 FIXME: This is quite broken. Modifying the
9194 symbol here means we will be changing all later
9195 uses of the symbol, not just in this section. */
9196 if (action_discarded & PRETEND)
9198 asection *kept;
9200 kept = _bfd_elf_check_kept_section (sec,
9201 finfo->info);
9202 if (kept != NULL)
9204 *ps = kept;
9205 continue;
9212 /* Relocate the section by invoking a back end routine.
9214 The back end routine is responsible for adjusting the
9215 section contents as necessary, and (if using Rela relocs
9216 and generating a relocatable output file) adjusting the
9217 reloc addend as necessary.
9219 The back end routine does not have to worry about setting
9220 the reloc address or the reloc symbol index.
9222 The back end routine is given a pointer to the swapped in
9223 internal symbols, and can access the hash table entries
9224 for the external symbols via elf_sym_hashes (input_bfd).
9226 When generating relocatable output, the back end routine
9227 must handle STB_LOCAL/STT_SECTION symbols specially. The
9228 output symbol is going to be a section symbol
9229 corresponding to the output section, which will require
9230 the addend to be adjusted. */
9232 ret = (*relocate_section) (output_bfd, finfo->info,
9233 input_bfd, o, contents,
9234 internal_relocs,
9235 isymbuf,
9236 finfo->sections);
9237 if (!ret)
9238 return FALSE;
9240 if (ret == 2
9241 || finfo->info->relocatable
9242 || finfo->info->emitrelocations)
9244 Elf_Internal_Rela *irela;
9245 Elf_Internal_Rela *irelaend;
9246 bfd_vma last_offset;
9247 struct elf_link_hash_entry **rel_hash;
9248 struct elf_link_hash_entry **rel_hash_list;
9249 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
9250 unsigned int next_erel;
9251 bfd_boolean rela_normal;
9253 input_rel_hdr = &elf_section_data (o)->rel_hdr;
9254 rela_normal = (bed->rela_normal
9255 && (input_rel_hdr->sh_entsize
9256 == bed->s->sizeof_rela));
9258 /* Adjust the reloc addresses and symbol indices. */
9260 irela = internal_relocs;
9261 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9262 rel_hash = (elf_section_data (o->output_section)->rel_hashes
9263 + elf_section_data (o->output_section)->rel_count
9264 + elf_section_data (o->output_section)->rel_count2);
9265 rel_hash_list = rel_hash;
9266 last_offset = o->output_offset;
9267 if (!finfo->info->relocatable)
9268 last_offset += o->output_section->vma;
9269 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9271 unsigned long r_symndx;
9272 asection *sec;
9273 Elf_Internal_Sym sym;
9275 if (next_erel == bed->s->int_rels_per_ext_rel)
9277 rel_hash++;
9278 next_erel = 0;
9281 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9282 finfo->info, o,
9283 irela->r_offset);
9284 if (irela->r_offset >= (bfd_vma) -2)
9286 /* This is a reloc for a deleted entry or somesuch.
9287 Turn it into an R_*_NONE reloc, at the same
9288 offset as the last reloc. elf_eh_frame.c and
9289 bfd_elf_discard_info rely on reloc offsets
9290 being ordered. */
9291 irela->r_offset = last_offset;
9292 irela->r_info = 0;
9293 irela->r_addend = 0;
9294 continue;
9297 irela->r_offset += o->output_offset;
9299 /* Relocs in an executable have to be virtual addresses. */
9300 if (!finfo->info->relocatable)
9301 irela->r_offset += o->output_section->vma;
9303 last_offset = irela->r_offset;
9305 r_symndx = irela->r_info >> r_sym_shift;
9306 if (r_symndx == STN_UNDEF)
9307 continue;
9309 if (r_symndx >= locsymcount
9310 || (elf_bad_symtab (input_bfd)
9311 && finfo->sections[r_symndx] == NULL))
9313 struct elf_link_hash_entry *rh;
9314 unsigned long indx;
9316 /* This is a reloc against a global symbol. We
9317 have not yet output all the local symbols, so
9318 we do not know the symbol index of any global
9319 symbol. We set the rel_hash entry for this
9320 reloc to point to the global hash table entry
9321 for this symbol. The symbol index is then
9322 set at the end of bfd_elf_final_link. */
9323 indx = r_symndx - extsymoff;
9324 rh = elf_sym_hashes (input_bfd)[indx];
9325 while (rh->root.type == bfd_link_hash_indirect
9326 || rh->root.type == bfd_link_hash_warning)
9327 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9329 /* Setting the index to -2 tells
9330 elf_link_output_extsym that this symbol is
9331 used by a reloc. */
9332 BFD_ASSERT (rh->indx < 0);
9333 rh->indx = -2;
9335 *rel_hash = rh;
9337 continue;
9340 /* This is a reloc against a local symbol. */
9342 *rel_hash = NULL;
9343 sym = isymbuf[r_symndx];
9344 sec = finfo->sections[r_symndx];
9345 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9347 /* I suppose the backend ought to fill in the
9348 section of any STT_SECTION symbol against a
9349 processor specific section. */
9350 r_symndx = 0;
9351 if (bfd_is_abs_section (sec))
9353 else if (sec == NULL || sec->owner == NULL)
9355 bfd_set_error (bfd_error_bad_value);
9356 return FALSE;
9358 else
9360 asection *osec = sec->output_section;
9362 /* If we have discarded a section, the output
9363 section will be the absolute section. In
9364 case of discarded SEC_MERGE sections, use
9365 the kept section. relocate_section should
9366 have already handled discarded linkonce
9367 sections. */
9368 if (bfd_is_abs_section (osec)
9369 && sec->kept_section != NULL
9370 && sec->kept_section->output_section != NULL)
9372 osec = sec->kept_section->output_section;
9373 irela->r_addend -= osec->vma;
9376 if (!bfd_is_abs_section (osec))
9378 r_symndx = osec->target_index;
9379 if (r_symndx == 0)
9381 struct elf_link_hash_table *htab;
9382 asection *oi;
9384 htab = elf_hash_table (finfo->info);
9385 oi = htab->text_index_section;
9386 if ((osec->flags & SEC_READONLY) == 0
9387 && htab->data_index_section != NULL)
9388 oi = htab->data_index_section;
9390 if (oi != NULL)
9392 irela->r_addend += osec->vma - oi->vma;
9393 r_symndx = oi->target_index;
9397 BFD_ASSERT (r_symndx != 0);
9401 /* Adjust the addend according to where the
9402 section winds up in the output section. */
9403 if (rela_normal)
9404 irela->r_addend += sec->output_offset;
9406 else
9408 if (finfo->indices[r_symndx] == -1)
9410 unsigned long shlink;
9411 const char *name;
9412 asection *osec;
9414 if (finfo->info->strip == strip_all)
9416 /* You can't do ld -r -s. */
9417 bfd_set_error (bfd_error_invalid_operation);
9418 return FALSE;
9421 /* This symbol was skipped earlier, but
9422 since it is needed by a reloc, we
9423 must output it now. */
9424 shlink = symtab_hdr->sh_link;
9425 name = (bfd_elf_string_from_elf_section
9426 (input_bfd, shlink, sym.st_name));
9427 if (name == NULL)
9428 return FALSE;
9430 osec = sec->output_section;
9431 sym.st_shndx =
9432 _bfd_elf_section_from_bfd_section (output_bfd,
9433 osec);
9434 if (sym.st_shndx == SHN_BAD)
9435 return FALSE;
9437 sym.st_value += sec->output_offset;
9438 if (! finfo->info->relocatable)
9440 sym.st_value += osec->vma;
9441 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9443 /* STT_TLS symbols are relative to PT_TLS
9444 segment base. */
9445 BFD_ASSERT (elf_hash_table (finfo->info)
9446 ->tls_sec != NULL);
9447 sym.st_value -= (elf_hash_table (finfo->info)
9448 ->tls_sec->vma);
9452 finfo->indices[r_symndx]
9453 = bfd_get_symcount (output_bfd);
9455 if (! elf_link_output_sym (finfo, name, &sym, sec,
9456 NULL))
9457 return FALSE;
9460 r_symndx = finfo->indices[r_symndx];
9463 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9464 | (irela->r_info & r_type_mask));
9467 /* Swap out the relocs. */
9468 if (input_rel_hdr->sh_size != 0
9469 && !bed->elf_backend_emit_relocs (output_bfd, o,
9470 input_rel_hdr,
9471 internal_relocs,
9472 rel_hash_list))
9473 return FALSE;
9475 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
9476 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
9478 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9479 * bed->s->int_rels_per_ext_rel);
9480 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9481 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9482 input_rel_hdr2,
9483 internal_relocs,
9484 rel_hash_list))
9485 return FALSE;
9490 /* Write out the modified section contents. */
9491 if (bed->elf_backend_write_section
9492 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9493 contents))
9495 /* Section written out. */
9497 else switch (o->sec_info_type)
9499 case ELF_INFO_TYPE_STABS:
9500 if (! (_bfd_write_section_stabs
9501 (output_bfd,
9502 &elf_hash_table (finfo->info)->stab_info,
9503 o, &elf_section_data (o)->sec_info, contents)))
9504 return FALSE;
9505 break;
9506 case ELF_INFO_TYPE_MERGE:
9507 if (! _bfd_write_merged_section (output_bfd, o,
9508 elf_section_data (o)->sec_info))
9509 return FALSE;
9510 break;
9511 case ELF_INFO_TYPE_EH_FRAME:
9513 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9514 o, contents))
9515 return FALSE;
9517 break;
9518 default:
9520 if (! (o->flags & SEC_EXCLUDE)
9521 && ! (o->output_section->flags & SEC_NEVER_LOAD)
9522 && ! bfd_set_section_contents (output_bfd, o->output_section,
9523 contents,
9524 (file_ptr) o->output_offset,
9525 o->size))
9526 return FALSE;
9528 break;
9532 return TRUE;
9535 /* Generate a reloc when linking an ELF file. This is a reloc
9536 requested by the linker, and does not come from any input file. This
9537 is used to build constructor and destructor tables when linking
9538 with -Ur. */
9540 static bfd_boolean
9541 elf_reloc_link_order (bfd *output_bfd,
9542 struct bfd_link_info *info,
9543 asection *output_section,
9544 struct bfd_link_order *link_order)
9546 reloc_howto_type *howto;
9547 long indx;
9548 bfd_vma offset;
9549 bfd_vma addend;
9550 struct elf_link_hash_entry **rel_hash_ptr;
9551 Elf_Internal_Shdr *rel_hdr;
9552 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9553 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9554 bfd_byte *erel;
9555 unsigned int i;
9557 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9558 if (howto == NULL)
9560 bfd_set_error (bfd_error_bad_value);
9561 return FALSE;
9564 addend = link_order->u.reloc.p->addend;
9566 /* Figure out the symbol index. */
9567 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
9568 + elf_section_data (output_section)->rel_count
9569 + elf_section_data (output_section)->rel_count2);
9570 if (link_order->type == bfd_section_reloc_link_order)
9572 indx = link_order->u.reloc.p->u.section->target_index;
9573 BFD_ASSERT (indx != 0);
9574 *rel_hash_ptr = NULL;
9576 else
9578 struct elf_link_hash_entry *h;
9580 /* Treat a reloc against a defined symbol as though it were
9581 actually against the section. */
9582 h = ((struct elf_link_hash_entry *)
9583 bfd_wrapped_link_hash_lookup (output_bfd, info,
9584 link_order->u.reloc.p->u.name,
9585 FALSE, FALSE, TRUE));
9586 if (h != NULL
9587 && (h->root.type == bfd_link_hash_defined
9588 || h->root.type == bfd_link_hash_defweak))
9590 asection *section;
9592 section = h->root.u.def.section;
9593 indx = section->output_section->target_index;
9594 *rel_hash_ptr = NULL;
9595 /* It seems that we ought to add the symbol value to the
9596 addend here, but in practice it has already been added
9597 because it was passed to constructor_callback. */
9598 addend += section->output_section->vma + section->output_offset;
9600 else if (h != NULL)
9602 /* Setting the index to -2 tells elf_link_output_extsym that
9603 this symbol is used by a reloc. */
9604 h->indx = -2;
9605 *rel_hash_ptr = h;
9606 indx = 0;
9608 else
9610 if (! ((*info->callbacks->unattached_reloc)
9611 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9612 return FALSE;
9613 indx = 0;
9617 /* If this is an inplace reloc, we must write the addend into the
9618 object file. */
9619 if (howto->partial_inplace && addend != 0)
9621 bfd_size_type size;
9622 bfd_reloc_status_type rstat;
9623 bfd_byte *buf;
9624 bfd_boolean ok;
9625 const char *sym_name;
9627 size = bfd_get_reloc_size (howto);
9628 buf = bfd_zmalloc (size);
9629 if (buf == NULL)
9630 return FALSE;
9631 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9632 switch (rstat)
9634 case bfd_reloc_ok:
9635 break;
9637 default:
9638 case bfd_reloc_outofrange:
9639 abort ();
9641 case bfd_reloc_overflow:
9642 if (link_order->type == bfd_section_reloc_link_order)
9643 sym_name = bfd_section_name (output_bfd,
9644 link_order->u.reloc.p->u.section);
9645 else
9646 sym_name = link_order->u.reloc.p->u.name;
9647 if (! ((*info->callbacks->reloc_overflow)
9648 (info, NULL, sym_name, howto->name, addend, NULL,
9649 NULL, (bfd_vma) 0)))
9651 free (buf);
9652 return FALSE;
9654 break;
9656 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9657 link_order->offset, size);
9658 free (buf);
9659 if (! ok)
9660 return FALSE;
9663 /* The address of a reloc is relative to the section in a
9664 relocatable file, and is a virtual address in an executable
9665 file. */
9666 offset = link_order->offset;
9667 if (! info->relocatable)
9668 offset += output_section->vma;
9670 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9672 irel[i].r_offset = offset;
9673 irel[i].r_info = 0;
9674 irel[i].r_addend = 0;
9676 if (bed->s->arch_size == 32)
9677 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9678 else
9679 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9681 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9682 erel = rel_hdr->contents;
9683 if (rel_hdr->sh_type == SHT_REL)
9685 erel += (elf_section_data (output_section)->rel_count
9686 * bed->s->sizeof_rel);
9687 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9689 else
9691 irel[0].r_addend = addend;
9692 erel += (elf_section_data (output_section)->rel_count
9693 * bed->s->sizeof_rela);
9694 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9697 ++elf_section_data (output_section)->rel_count;
9699 return TRUE;
9703 /* Get the output vma of the section pointed to by the sh_link field. */
9705 static bfd_vma
9706 elf_get_linked_section_vma (struct bfd_link_order *p)
9708 Elf_Internal_Shdr **elf_shdrp;
9709 asection *s;
9710 int elfsec;
9712 s = p->u.indirect.section;
9713 elf_shdrp = elf_elfsections (s->owner);
9714 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9715 elfsec = elf_shdrp[elfsec]->sh_link;
9716 /* PR 290:
9717 The Intel C compiler generates SHT_IA_64_UNWIND with
9718 SHF_LINK_ORDER. But it doesn't set the sh_link or
9719 sh_info fields. Hence we could get the situation
9720 where elfsec is 0. */
9721 if (elfsec == 0)
9723 const struct elf_backend_data *bed
9724 = get_elf_backend_data (s->owner);
9725 if (bed->link_order_error_handler)
9726 bed->link_order_error_handler
9727 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
9728 return 0;
9730 else
9732 s = elf_shdrp[elfsec]->bfd_section;
9733 return s->output_section->vma + s->output_offset;
9738 /* Compare two sections based on the locations of the sections they are
9739 linked to. Used by elf_fixup_link_order. */
9741 static int
9742 compare_link_order (const void * a, const void * b)
9744 bfd_vma apos;
9745 bfd_vma bpos;
9747 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9748 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9749 if (apos < bpos)
9750 return -1;
9751 return apos > bpos;
9755 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9756 order as their linked sections. Returns false if this could not be done
9757 because an output section includes both ordered and unordered
9758 sections. Ideally we'd do this in the linker proper. */
9760 static bfd_boolean
9761 elf_fixup_link_order (bfd *abfd, asection *o)
9763 int seen_linkorder;
9764 int seen_other;
9765 int n;
9766 struct bfd_link_order *p;
9767 bfd *sub;
9768 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9769 unsigned elfsec;
9770 struct bfd_link_order **sections;
9771 asection *s, *other_sec, *linkorder_sec;
9772 bfd_vma offset;
9774 other_sec = NULL;
9775 linkorder_sec = NULL;
9776 seen_other = 0;
9777 seen_linkorder = 0;
9778 for (p = o->map_head.link_order; p != NULL; p = p->next)
9780 if (p->type == bfd_indirect_link_order)
9782 s = p->u.indirect.section;
9783 sub = s->owner;
9784 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9785 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
9786 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9787 && elfsec < elf_numsections (sub)
9788 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
9790 seen_linkorder++;
9791 linkorder_sec = s;
9793 else
9795 seen_other++;
9796 other_sec = s;
9799 else
9800 seen_other++;
9802 if (seen_other && seen_linkorder)
9804 if (other_sec && linkorder_sec)
9805 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9806 o, linkorder_sec,
9807 linkorder_sec->owner, other_sec,
9808 other_sec->owner);
9809 else
9810 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9812 bfd_set_error (bfd_error_bad_value);
9813 return FALSE;
9817 if (!seen_linkorder)
9818 return TRUE;
9820 sections = (struct bfd_link_order **)
9821 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
9822 if (sections == NULL)
9823 return FALSE;
9824 seen_linkorder = 0;
9826 for (p = o->map_head.link_order; p != NULL; p = p->next)
9828 sections[seen_linkorder++] = p;
9830 /* Sort the input sections in the order of their linked section. */
9831 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9832 compare_link_order);
9834 /* Change the offsets of the sections. */
9835 offset = 0;
9836 for (n = 0; n < seen_linkorder; n++)
9838 s = sections[n]->u.indirect.section;
9839 offset &= ~(bfd_vma) 0 << s->alignment_power;
9840 s->output_offset = offset;
9841 sections[n]->offset = offset;
9842 offset += sections[n]->size;
9845 return TRUE;
9849 /* Do the final step of an ELF link. */
9851 bfd_boolean
9852 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9854 bfd_boolean dynamic;
9855 bfd_boolean emit_relocs;
9856 bfd *dynobj;
9857 struct elf_final_link_info finfo;
9858 register asection *o;
9859 register struct bfd_link_order *p;
9860 register bfd *sub;
9861 bfd_size_type max_contents_size;
9862 bfd_size_type max_external_reloc_size;
9863 bfd_size_type max_internal_reloc_count;
9864 bfd_size_type max_sym_count;
9865 bfd_size_type max_sym_shndx_count;
9866 file_ptr off;
9867 Elf_Internal_Sym elfsym;
9868 unsigned int i;
9869 Elf_Internal_Shdr *symtab_hdr;
9870 Elf_Internal_Shdr *symtab_shndx_hdr;
9871 Elf_Internal_Shdr *symstrtab_hdr;
9872 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9873 struct elf_outext_info eoinfo;
9874 bfd_boolean merged;
9875 size_t relativecount = 0;
9876 asection *reldyn = 0;
9877 bfd_size_type amt;
9878 asection *attr_section = NULL;
9879 bfd_vma attr_size = 0;
9880 const char *std_attrs_section;
9882 if (! is_elf_hash_table (info->hash))
9883 return FALSE;
9885 if (info->shared)
9886 abfd->flags |= DYNAMIC;
9888 dynamic = elf_hash_table (info)->dynamic_sections_created;
9889 dynobj = elf_hash_table (info)->dynobj;
9891 emit_relocs = (info->relocatable
9892 || info->emitrelocations);
9894 finfo.info = info;
9895 finfo.output_bfd = abfd;
9896 finfo.symstrtab = _bfd_elf_stringtab_init ();
9897 if (finfo.symstrtab == NULL)
9898 return FALSE;
9900 if (! dynamic)
9902 finfo.dynsym_sec = NULL;
9903 finfo.hash_sec = NULL;
9904 finfo.symver_sec = NULL;
9906 else
9908 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
9909 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
9910 BFD_ASSERT (finfo.dynsym_sec != NULL);
9911 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
9912 /* Note that it is OK if symver_sec is NULL. */
9915 finfo.contents = NULL;
9916 finfo.external_relocs = NULL;
9917 finfo.internal_relocs = NULL;
9918 finfo.external_syms = NULL;
9919 finfo.locsym_shndx = NULL;
9920 finfo.internal_syms = NULL;
9921 finfo.indices = NULL;
9922 finfo.sections = NULL;
9923 finfo.symbuf = NULL;
9924 finfo.symshndxbuf = NULL;
9925 finfo.symbuf_count = 0;
9926 finfo.shndxbuf_size = 0;
9928 /* The object attributes have been merged. Remove the input
9929 sections from the link, and set the contents of the output
9930 secton. */
9931 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
9932 for (o = abfd->sections; o != NULL; o = o->next)
9934 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
9935 || strcmp (o->name, ".gnu.attributes") == 0)
9937 for (p = o->map_head.link_order; p != NULL; p = p->next)
9939 asection *input_section;
9941 if (p->type != bfd_indirect_link_order)
9942 continue;
9943 input_section = p->u.indirect.section;
9944 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9945 elf_link_input_bfd ignores this section. */
9946 input_section->flags &= ~SEC_HAS_CONTENTS;
9949 attr_size = bfd_elf_obj_attr_size (abfd);
9950 if (attr_size)
9952 bfd_set_section_size (abfd, o, attr_size);
9953 attr_section = o;
9954 /* Skip this section later on. */
9955 o->map_head.link_order = NULL;
9957 else
9958 o->flags |= SEC_EXCLUDE;
9962 /* Count up the number of relocations we will output for each output
9963 section, so that we know the sizes of the reloc sections. We
9964 also figure out some maximum sizes. */
9965 max_contents_size = 0;
9966 max_external_reloc_size = 0;
9967 max_internal_reloc_count = 0;
9968 max_sym_count = 0;
9969 max_sym_shndx_count = 0;
9970 merged = FALSE;
9971 for (o = abfd->sections; o != NULL; o = o->next)
9973 struct bfd_elf_section_data *esdo = elf_section_data (o);
9974 o->reloc_count = 0;
9976 for (p = o->map_head.link_order; p != NULL; p = p->next)
9978 unsigned int reloc_count = 0;
9979 struct bfd_elf_section_data *esdi = NULL;
9980 unsigned int *rel_count1;
9982 if (p->type == bfd_section_reloc_link_order
9983 || p->type == bfd_symbol_reloc_link_order)
9984 reloc_count = 1;
9985 else if (p->type == bfd_indirect_link_order)
9987 asection *sec;
9989 sec = p->u.indirect.section;
9990 esdi = elf_section_data (sec);
9992 /* Mark all sections which are to be included in the
9993 link. This will normally be every section. We need
9994 to do this so that we can identify any sections which
9995 the linker has decided to not include. */
9996 sec->linker_mark = TRUE;
9998 if (sec->flags & SEC_MERGE)
9999 merged = TRUE;
10001 if (info->relocatable || info->emitrelocations)
10002 reloc_count = sec->reloc_count;
10003 else if (bed->elf_backend_count_relocs)
10005 Elf_Internal_Rela * relocs;
10007 relocs = _bfd_elf_link_read_relocs (sec->owner, sec,
10008 NULL, NULL,
10009 info->keep_memory);
10011 if (relocs != NULL)
10013 reloc_count
10014 = (*bed->elf_backend_count_relocs) (sec, relocs);
10016 if (elf_section_data (sec)->relocs != relocs)
10017 free (relocs);
10021 if (sec->rawsize > max_contents_size)
10022 max_contents_size = sec->rawsize;
10023 if (sec->size > max_contents_size)
10024 max_contents_size = sec->size;
10026 /* We are interested in just local symbols, not all
10027 symbols. */
10028 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10029 && (sec->owner->flags & DYNAMIC) == 0)
10031 size_t sym_count;
10033 if (elf_bad_symtab (sec->owner))
10034 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10035 / bed->s->sizeof_sym);
10036 else
10037 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10039 if (sym_count > max_sym_count)
10040 max_sym_count = sym_count;
10042 if (sym_count > max_sym_shndx_count
10043 && elf_symtab_shndx (sec->owner) != 0)
10044 max_sym_shndx_count = sym_count;
10046 if ((sec->flags & SEC_RELOC) != 0)
10048 size_t ext_size;
10050 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
10051 if (ext_size > max_external_reloc_size)
10052 max_external_reloc_size = ext_size;
10053 if (sec->reloc_count > max_internal_reloc_count)
10054 max_internal_reloc_count = sec->reloc_count;
10059 if (reloc_count == 0)
10060 continue;
10062 o->reloc_count += reloc_count;
10064 /* MIPS may have a mix of REL and RELA relocs on sections.
10065 To support this curious ABI we keep reloc counts in
10066 elf_section_data too. We must be careful to add the
10067 relocations from the input section to the right output
10068 count. FIXME: Get rid of one count. We have
10069 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10070 rel_count1 = &esdo->rel_count;
10071 if (esdi != NULL)
10073 bfd_boolean same_size;
10074 bfd_size_type entsize1;
10076 entsize1 = esdi->rel_hdr.sh_entsize;
10077 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
10078 || entsize1 == bed->s->sizeof_rela);
10079 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
10081 if (!same_size)
10082 rel_count1 = &esdo->rel_count2;
10084 if (esdi->rel_hdr2 != NULL)
10086 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
10087 unsigned int alt_count;
10088 unsigned int *rel_count2;
10090 BFD_ASSERT (entsize2 != entsize1
10091 && (entsize2 == bed->s->sizeof_rel
10092 || entsize2 == bed->s->sizeof_rela));
10094 rel_count2 = &esdo->rel_count2;
10095 if (!same_size)
10096 rel_count2 = &esdo->rel_count;
10098 /* The following is probably too simplistic if the
10099 backend counts output relocs unusually. */
10100 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
10101 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
10102 *rel_count2 += alt_count;
10103 reloc_count -= alt_count;
10106 *rel_count1 += reloc_count;
10109 if (o->reloc_count > 0)
10110 o->flags |= SEC_RELOC;
10111 else
10113 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10114 set it (this is probably a bug) and if it is set
10115 assign_section_numbers will create a reloc section. */
10116 o->flags &=~ SEC_RELOC;
10119 /* If the SEC_ALLOC flag is not set, force the section VMA to
10120 zero. This is done in elf_fake_sections as well, but forcing
10121 the VMA to 0 here will ensure that relocs against these
10122 sections are handled correctly. */
10123 if ((o->flags & SEC_ALLOC) == 0
10124 && ! o->user_set_vma)
10125 o->vma = 0;
10128 if (! info->relocatable && merged)
10129 elf_link_hash_traverse (elf_hash_table (info),
10130 _bfd_elf_link_sec_merge_syms, abfd);
10132 /* Figure out the file positions for everything but the symbol table
10133 and the relocs. We set symcount to force assign_section_numbers
10134 to create a symbol table. */
10135 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10136 BFD_ASSERT (! abfd->output_has_begun);
10137 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10138 goto error_return;
10140 /* Set sizes, and assign file positions for reloc sections. */
10141 for (o = abfd->sections; o != NULL; o = o->next)
10143 if ((o->flags & SEC_RELOC) != 0)
10145 if (!(_bfd_elf_link_size_reloc_section
10146 (abfd, &elf_section_data (o)->rel_hdr, o)))
10147 goto error_return;
10149 if (elf_section_data (o)->rel_hdr2
10150 && !(_bfd_elf_link_size_reloc_section
10151 (abfd, elf_section_data (o)->rel_hdr2, o)))
10152 goto error_return;
10155 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10156 to count upwards while actually outputting the relocations. */
10157 elf_section_data (o)->rel_count = 0;
10158 elf_section_data (o)->rel_count2 = 0;
10161 _bfd_elf_assign_file_positions_for_relocs (abfd);
10163 /* We have now assigned file positions for all the sections except
10164 .symtab and .strtab. We start the .symtab section at the current
10165 file position, and write directly to it. We build the .strtab
10166 section in memory. */
10167 bfd_get_symcount (abfd) = 0;
10168 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10169 /* sh_name is set in prep_headers. */
10170 symtab_hdr->sh_type = SHT_SYMTAB;
10171 /* sh_flags, sh_addr and sh_size all start off zero. */
10172 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10173 /* sh_link is set in assign_section_numbers. */
10174 /* sh_info is set below. */
10175 /* sh_offset is set just below. */
10176 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
10178 off = elf_tdata (abfd)->next_file_pos;
10179 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10181 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10182 incorrect. We do not yet know the size of the .symtab section.
10183 We correct next_file_pos below, after we do know the size. */
10185 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10186 continuously seeking to the right position in the file. */
10187 if (! info->keep_memory || max_sym_count < 20)
10188 finfo.symbuf_size = 20;
10189 else
10190 finfo.symbuf_size = max_sym_count;
10191 amt = finfo.symbuf_size;
10192 amt *= bed->s->sizeof_sym;
10193 finfo.symbuf = bfd_malloc (amt);
10194 if (finfo.symbuf == NULL)
10195 goto error_return;
10196 if (elf_numsections (abfd) > SHN_LORESERVE)
10198 /* Wild guess at number of output symbols. realloc'd as needed. */
10199 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10200 finfo.shndxbuf_size = amt;
10201 amt *= sizeof (Elf_External_Sym_Shndx);
10202 finfo.symshndxbuf = bfd_zmalloc (amt);
10203 if (finfo.symshndxbuf == NULL)
10204 goto error_return;
10207 /* Start writing out the symbol table. The first symbol is always a
10208 dummy symbol. */
10209 if (info->strip != strip_all
10210 || emit_relocs)
10212 elfsym.st_value = 0;
10213 elfsym.st_size = 0;
10214 elfsym.st_info = 0;
10215 elfsym.st_other = 0;
10216 elfsym.st_shndx = SHN_UNDEF;
10217 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10218 NULL))
10219 goto error_return;
10222 /* Output a symbol for each section. We output these even if we are
10223 discarding local symbols, since they are used for relocs. These
10224 symbols have no names. We store the index of each one in the
10225 index field of the section, so that we can find it again when
10226 outputting relocs. */
10227 if (info->strip != strip_all
10228 || emit_relocs)
10230 elfsym.st_size = 0;
10231 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10232 elfsym.st_other = 0;
10233 elfsym.st_value = 0;
10234 for (i = 1; i < elf_numsections (abfd); i++)
10236 o = bfd_section_from_elf_index (abfd, i);
10237 if (o != NULL)
10239 o->target_index = bfd_get_symcount (abfd);
10240 elfsym.st_shndx = i;
10241 if (!info->relocatable)
10242 elfsym.st_value = o->vma;
10243 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
10244 goto error_return;
10246 if (i == SHN_LORESERVE - 1)
10247 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
10251 /* Allocate some memory to hold information read in from the input
10252 files. */
10253 if (max_contents_size != 0)
10255 finfo.contents = bfd_malloc (max_contents_size);
10256 if (finfo.contents == NULL)
10257 goto error_return;
10260 if (max_external_reloc_size != 0)
10262 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10263 if (finfo.external_relocs == NULL)
10264 goto error_return;
10267 if (max_internal_reloc_count != 0)
10269 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10270 amt *= sizeof (Elf_Internal_Rela);
10271 finfo.internal_relocs = bfd_malloc (amt);
10272 if (finfo.internal_relocs == NULL)
10273 goto error_return;
10276 if (max_sym_count != 0)
10278 amt = max_sym_count * bed->s->sizeof_sym;
10279 finfo.external_syms = bfd_malloc (amt);
10280 if (finfo.external_syms == NULL)
10281 goto error_return;
10283 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10284 finfo.internal_syms = bfd_malloc (amt);
10285 if (finfo.internal_syms == NULL)
10286 goto error_return;
10288 amt = max_sym_count * sizeof (long);
10289 finfo.indices = bfd_malloc (amt);
10290 if (finfo.indices == NULL)
10291 goto error_return;
10293 amt = max_sym_count * sizeof (asection *);
10294 finfo.sections = bfd_malloc (amt);
10295 if (finfo.sections == NULL)
10296 goto error_return;
10299 if (max_sym_shndx_count != 0)
10301 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10302 finfo.locsym_shndx = bfd_malloc (amt);
10303 if (finfo.locsym_shndx == NULL)
10304 goto error_return;
10307 if (elf_hash_table (info)->tls_sec)
10309 bfd_vma base, end = 0;
10310 asection *sec;
10312 for (sec = elf_hash_table (info)->tls_sec;
10313 sec && (sec->flags & SEC_THREAD_LOCAL);
10314 sec = sec->next)
10316 bfd_size_type size = sec->size;
10318 if (size == 0
10319 && (sec->flags & SEC_HAS_CONTENTS) == 0)
10321 struct bfd_link_order *o = sec->map_tail.link_order;
10322 if (o != NULL)
10323 size = o->offset + o->size;
10325 end = sec->vma + size;
10327 base = elf_hash_table (info)->tls_sec->vma;
10328 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
10329 elf_hash_table (info)->tls_size = end - base;
10332 /* Reorder SHF_LINK_ORDER sections. */
10333 for (o = abfd->sections; o != NULL; o = o->next)
10335 if (!elf_fixup_link_order (abfd, o))
10336 return FALSE;
10339 /* Since ELF permits relocations to be against local symbols, we
10340 must have the local symbols available when we do the relocations.
10341 Since we would rather only read the local symbols once, and we
10342 would rather not keep them in memory, we handle all the
10343 relocations for a single input file at the same time.
10345 Unfortunately, there is no way to know the total number of local
10346 symbols until we have seen all of them, and the local symbol
10347 indices precede the global symbol indices. This means that when
10348 we are generating relocatable output, and we see a reloc against
10349 a global symbol, we can not know the symbol index until we have
10350 finished examining all the local symbols to see which ones we are
10351 going to output. To deal with this, we keep the relocations in
10352 memory, and don't output them until the end of the link. This is
10353 an unfortunate waste of memory, but I don't see a good way around
10354 it. Fortunately, it only happens when performing a relocatable
10355 link, which is not the common case. FIXME: If keep_memory is set
10356 we could write the relocs out and then read them again; I don't
10357 know how bad the memory loss will be. */
10359 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10360 sub->output_has_begun = FALSE;
10361 for (o = abfd->sections; o != NULL; o = o->next)
10363 for (p = o->map_head.link_order; p != NULL; p = p->next)
10365 if (p->type == bfd_indirect_link_order
10366 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10367 == bfd_target_elf_flavour)
10368 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10370 if (! sub->output_has_begun)
10372 if (! elf_link_input_bfd (&finfo, sub))
10373 goto error_return;
10374 sub->output_has_begun = TRUE;
10377 else if (p->type == bfd_section_reloc_link_order
10378 || p->type == bfd_symbol_reloc_link_order)
10380 if (! elf_reloc_link_order (abfd, info, o, p))
10381 goto error_return;
10383 else
10385 if (! _bfd_default_link_order (abfd, info, o, p))
10386 goto error_return;
10391 /* Free symbol buffer if needed. */
10392 if (!info->reduce_memory_overheads)
10394 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10395 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10396 && elf_tdata (sub)->symbuf)
10398 free (elf_tdata (sub)->symbuf);
10399 elf_tdata (sub)->symbuf = NULL;
10403 /* Output any global symbols that got converted to local in a
10404 version script or due to symbol visibility. We do this in a
10405 separate step since ELF requires all local symbols to appear
10406 prior to any global symbols. FIXME: We should only do this if
10407 some global symbols were, in fact, converted to become local.
10408 FIXME: Will this work correctly with the Irix 5 linker? */
10409 eoinfo.failed = FALSE;
10410 eoinfo.finfo = &finfo;
10411 eoinfo.localsyms = TRUE;
10412 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10413 &eoinfo);
10414 if (eoinfo.failed)
10415 return FALSE;
10417 /* If backend needs to output some local symbols not present in the hash
10418 table, do it now. */
10419 if (bed->elf_backend_output_arch_local_syms)
10421 typedef bfd_boolean (*out_sym_func)
10422 (void *, const char *, Elf_Internal_Sym *, asection *,
10423 struct elf_link_hash_entry *);
10425 if (! ((*bed->elf_backend_output_arch_local_syms)
10426 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10427 return FALSE;
10430 /* That wrote out all the local symbols. Finish up the symbol table
10431 with the global symbols. Even if we want to strip everything we
10432 can, we still need to deal with those global symbols that got
10433 converted to local in a version script. */
10435 /* The sh_info field records the index of the first non local symbol. */
10436 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10438 if (dynamic
10439 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10441 Elf_Internal_Sym sym;
10442 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10443 long last_local = 0;
10445 /* Write out the section symbols for the output sections. */
10446 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10448 asection *s;
10450 sym.st_size = 0;
10451 sym.st_name = 0;
10452 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10453 sym.st_other = 0;
10455 for (s = abfd->sections; s != NULL; s = s->next)
10457 int indx;
10458 bfd_byte *dest;
10459 long dynindx;
10461 dynindx = elf_section_data (s)->dynindx;
10462 if (dynindx <= 0)
10463 continue;
10464 indx = elf_section_data (s)->this_idx;
10465 BFD_ASSERT (indx > 0);
10466 sym.st_shndx = indx;
10467 if (! check_dynsym (abfd, &sym))
10468 return FALSE;
10469 sym.st_value = s->vma;
10470 dest = dynsym + dynindx * bed->s->sizeof_sym;
10471 if (last_local < dynindx)
10472 last_local = dynindx;
10473 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10477 /* Write out the local dynsyms. */
10478 if (elf_hash_table (info)->dynlocal)
10480 struct elf_link_local_dynamic_entry *e;
10481 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10483 asection *s;
10484 bfd_byte *dest;
10486 sym.st_size = e->isym.st_size;
10487 sym.st_other = e->isym.st_other;
10489 /* Copy the internal symbol as is.
10490 Note that we saved a word of storage and overwrote
10491 the original st_name with the dynstr_index. */
10492 sym = e->isym;
10494 if (e->isym.st_shndx != SHN_UNDEF
10495 && (e->isym.st_shndx < SHN_LORESERVE
10496 || e->isym.st_shndx > SHN_HIRESERVE))
10498 s = bfd_section_from_elf_index (e->input_bfd,
10499 e->isym.st_shndx);
10501 sym.st_shndx =
10502 elf_section_data (s->output_section)->this_idx;
10503 if (! check_dynsym (abfd, &sym))
10504 return FALSE;
10505 sym.st_value = (s->output_section->vma
10506 + s->output_offset
10507 + e->isym.st_value);
10510 if (last_local < e->dynindx)
10511 last_local = e->dynindx;
10513 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10514 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10518 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10519 last_local + 1;
10522 /* We get the global symbols from the hash table. */
10523 eoinfo.failed = FALSE;
10524 eoinfo.localsyms = FALSE;
10525 eoinfo.finfo = &finfo;
10526 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10527 &eoinfo);
10528 if (eoinfo.failed)
10529 return FALSE;
10531 /* If backend needs to output some symbols not present in the hash
10532 table, do it now. */
10533 if (bed->elf_backend_output_arch_syms)
10535 typedef bfd_boolean (*out_sym_func)
10536 (void *, const char *, Elf_Internal_Sym *, asection *,
10537 struct elf_link_hash_entry *);
10539 if (! ((*bed->elf_backend_output_arch_syms)
10540 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10541 return FALSE;
10544 /* Flush all symbols to the file. */
10545 if (! elf_link_flush_output_syms (&finfo, bed))
10546 return FALSE;
10548 /* Now we know the size of the symtab section. */
10549 off += symtab_hdr->sh_size;
10551 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10552 if (symtab_shndx_hdr->sh_name != 0)
10554 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10555 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10556 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10557 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10558 symtab_shndx_hdr->sh_size = amt;
10560 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10561 off, TRUE);
10563 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10564 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10565 return FALSE;
10569 /* Finish up and write out the symbol string table (.strtab)
10570 section. */
10571 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10572 /* sh_name was set in prep_headers. */
10573 symstrtab_hdr->sh_type = SHT_STRTAB;
10574 symstrtab_hdr->sh_flags = 0;
10575 symstrtab_hdr->sh_addr = 0;
10576 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10577 symstrtab_hdr->sh_entsize = 0;
10578 symstrtab_hdr->sh_link = 0;
10579 symstrtab_hdr->sh_info = 0;
10580 /* sh_offset is set just below. */
10581 symstrtab_hdr->sh_addralign = 1;
10583 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10584 elf_tdata (abfd)->next_file_pos = off;
10586 if (bfd_get_symcount (abfd) > 0)
10588 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10589 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10590 return FALSE;
10593 /* Adjust the relocs to have the correct symbol indices. */
10594 for (o = abfd->sections; o != NULL; o = o->next)
10596 if ((o->flags & SEC_RELOC) == 0)
10597 continue;
10599 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10600 elf_section_data (o)->rel_count,
10601 elf_section_data (o)->rel_hashes);
10602 if (elf_section_data (o)->rel_hdr2 != NULL)
10603 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10604 elf_section_data (o)->rel_count2,
10605 (elf_section_data (o)->rel_hashes
10606 + elf_section_data (o)->rel_count));
10608 /* Set the reloc_count field to 0 to prevent write_relocs from
10609 trying to swap the relocs out itself. */
10610 o->reloc_count = 0;
10613 if (dynamic && info->combreloc && dynobj != NULL)
10614 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10616 /* If we are linking against a dynamic object, or generating a
10617 shared library, finish up the dynamic linking information. */
10618 if (dynamic)
10620 bfd_byte *dyncon, *dynconend;
10622 /* Fix up .dynamic entries. */
10623 o = bfd_get_section_by_name (dynobj, ".dynamic");
10624 BFD_ASSERT (o != NULL);
10626 dyncon = o->contents;
10627 dynconend = o->contents + o->size;
10628 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10630 Elf_Internal_Dyn dyn;
10631 const char *name;
10632 unsigned int type;
10634 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10636 switch (dyn.d_tag)
10638 default:
10639 continue;
10640 case DT_NULL:
10641 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10643 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10645 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10646 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10647 default: continue;
10649 dyn.d_un.d_val = relativecount;
10650 relativecount = 0;
10651 break;
10653 continue;
10655 case DT_INIT:
10656 name = info->init_function;
10657 goto get_sym;
10658 case DT_FINI:
10659 name = info->fini_function;
10660 get_sym:
10662 struct elf_link_hash_entry *h;
10664 h = elf_link_hash_lookup (elf_hash_table (info), name,
10665 FALSE, FALSE, TRUE);
10666 if (h != NULL
10667 && (h->root.type == bfd_link_hash_defined
10668 || h->root.type == bfd_link_hash_defweak))
10670 dyn.d_un.d_val = h->root.u.def.value;
10671 o = h->root.u.def.section;
10672 if (o->output_section != NULL)
10673 dyn.d_un.d_val += (o->output_section->vma
10674 + o->output_offset);
10675 else
10677 /* The symbol is imported from another shared
10678 library and does not apply to this one. */
10679 dyn.d_un.d_val = 0;
10681 break;
10684 continue;
10686 case DT_PREINIT_ARRAYSZ:
10687 name = ".preinit_array";
10688 goto get_size;
10689 case DT_INIT_ARRAYSZ:
10690 name = ".init_array";
10691 goto get_size;
10692 case DT_FINI_ARRAYSZ:
10693 name = ".fini_array";
10694 get_size:
10695 o = bfd_get_section_by_name (abfd, name);
10696 if (o == NULL)
10698 (*_bfd_error_handler)
10699 (_("%B: could not find output section %s"), abfd, name);
10700 goto error_return;
10702 if (o->size == 0)
10703 (*_bfd_error_handler)
10704 (_("warning: %s section has zero size"), name);
10705 dyn.d_un.d_val = o->size;
10706 break;
10708 case DT_PREINIT_ARRAY:
10709 name = ".preinit_array";
10710 goto get_vma;
10711 case DT_INIT_ARRAY:
10712 name = ".init_array";
10713 goto get_vma;
10714 case DT_FINI_ARRAY:
10715 name = ".fini_array";
10716 goto get_vma;
10718 case DT_HASH:
10719 name = ".hash";
10720 goto get_vma;
10721 case DT_GNU_HASH:
10722 name = ".gnu.hash";
10723 goto get_vma;
10724 case DT_STRTAB:
10725 name = ".dynstr";
10726 goto get_vma;
10727 case DT_SYMTAB:
10728 name = ".dynsym";
10729 goto get_vma;
10730 case DT_VERDEF:
10731 name = ".gnu.version_d";
10732 goto get_vma;
10733 case DT_VERNEED:
10734 name = ".gnu.version_r";
10735 goto get_vma;
10736 case DT_VERSYM:
10737 name = ".gnu.version";
10738 get_vma:
10739 o = bfd_get_section_by_name (abfd, name);
10740 if (o == NULL)
10742 (*_bfd_error_handler)
10743 (_("%B: could not find output section %s"), abfd, name);
10744 goto error_return;
10746 dyn.d_un.d_ptr = o->vma;
10747 break;
10749 case DT_REL:
10750 case DT_RELA:
10751 case DT_RELSZ:
10752 case DT_RELASZ:
10753 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10754 type = SHT_REL;
10755 else
10756 type = SHT_RELA;
10757 dyn.d_un.d_val = 0;
10758 for (i = 1; i < elf_numsections (abfd); i++)
10760 Elf_Internal_Shdr *hdr;
10762 hdr = elf_elfsections (abfd)[i];
10763 if (hdr->sh_type == type
10764 && (hdr->sh_flags & SHF_ALLOC) != 0)
10766 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10767 dyn.d_un.d_val += hdr->sh_size;
10768 else
10770 if (dyn.d_un.d_val == 0
10771 || hdr->sh_addr < dyn.d_un.d_val)
10772 dyn.d_un.d_val = hdr->sh_addr;
10776 break;
10778 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10782 /* If we have created any dynamic sections, then output them. */
10783 if (dynobj != NULL)
10785 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10786 goto error_return;
10788 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10789 if (info->warn_shared_textrel && info->shared)
10791 bfd_byte *dyncon, *dynconend;
10793 /* Fix up .dynamic entries. */
10794 o = bfd_get_section_by_name (dynobj, ".dynamic");
10795 BFD_ASSERT (o != NULL);
10797 dyncon = o->contents;
10798 dynconend = o->contents + o->size;
10799 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10801 Elf_Internal_Dyn dyn;
10803 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10805 if (dyn.d_tag == DT_TEXTREL)
10807 info->callbacks->einfo
10808 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10809 break;
10814 for (o = dynobj->sections; o != NULL; o = o->next)
10816 if ((o->flags & SEC_HAS_CONTENTS) == 0
10817 || o->size == 0
10818 || o->output_section == bfd_abs_section_ptr)
10819 continue;
10820 if ((o->flags & SEC_LINKER_CREATED) == 0)
10822 /* At this point, we are only interested in sections
10823 created by _bfd_elf_link_create_dynamic_sections. */
10824 continue;
10826 if (elf_hash_table (info)->stab_info.stabstr == o)
10827 continue;
10828 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10829 continue;
10830 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10831 != SHT_STRTAB)
10832 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10834 if (! bfd_set_section_contents (abfd, o->output_section,
10835 o->contents,
10836 (file_ptr) o->output_offset,
10837 o->size))
10838 goto error_return;
10840 else
10842 /* The contents of the .dynstr section are actually in a
10843 stringtab. */
10844 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10845 if (bfd_seek (abfd, off, SEEK_SET) != 0
10846 || ! _bfd_elf_strtab_emit (abfd,
10847 elf_hash_table (info)->dynstr))
10848 goto error_return;
10853 if (info->relocatable)
10855 bfd_boolean failed = FALSE;
10857 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10858 if (failed)
10859 goto error_return;
10862 /* If we have optimized stabs strings, output them. */
10863 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10865 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10866 goto error_return;
10869 if (info->eh_frame_hdr)
10871 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10872 goto error_return;
10875 if (finfo.symstrtab != NULL)
10876 _bfd_stringtab_free (finfo.symstrtab);
10877 if (finfo.contents != NULL)
10878 free (finfo.contents);
10879 if (finfo.external_relocs != NULL)
10880 free (finfo.external_relocs);
10881 if (finfo.internal_relocs != NULL)
10882 free (finfo.internal_relocs);
10883 if (finfo.external_syms != NULL)
10884 free (finfo.external_syms);
10885 if (finfo.locsym_shndx != NULL)
10886 free (finfo.locsym_shndx);
10887 if (finfo.internal_syms != NULL)
10888 free (finfo.internal_syms);
10889 if (finfo.indices != NULL)
10890 free (finfo.indices);
10891 if (finfo.sections != NULL)
10892 free (finfo.sections);
10893 if (finfo.symbuf != NULL)
10894 free (finfo.symbuf);
10895 if (finfo.symshndxbuf != NULL)
10896 free (finfo.symshndxbuf);
10897 for (o = abfd->sections; o != NULL; o = o->next)
10899 if ((o->flags & SEC_RELOC) != 0
10900 && elf_section_data (o)->rel_hashes != NULL)
10901 free (elf_section_data (o)->rel_hashes);
10904 elf_tdata (abfd)->linker = TRUE;
10906 if (attr_section)
10908 bfd_byte *contents = bfd_malloc (attr_size);
10909 if (contents == NULL)
10910 return FALSE; /* Bail out and fail. */
10911 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
10912 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
10913 free (contents);
10916 return TRUE;
10918 error_return:
10919 if (finfo.symstrtab != NULL)
10920 _bfd_stringtab_free (finfo.symstrtab);
10921 if (finfo.contents != NULL)
10922 free (finfo.contents);
10923 if (finfo.external_relocs != NULL)
10924 free (finfo.external_relocs);
10925 if (finfo.internal_relocs != NULL)
10926 free (finfo.internal_relocs);
10927 if (finfo.external_syms != NULL)
10928 free (finfo.external_syms);
10929 if (finfo.locsym_shndx != NULL)
10930 free (finfo.locsym_shndx);
10931 if (finfo.internal_syms != NULL)
10932 free (finfo.internal_syms);
10933 if (finfo.indices != NULL)
10934 free (finfo.indices);
10935 if (finfo.sections != NULL)
10936 free (finfo.sections);
10937 if (finfo.symbuf != NULL)
10938 free (finfo.symbuf);
10939 if (finfo.symshndxbuf != NULL)
10940 free (finfo.symshndxbuf);
10941 for (o = abfd->sections; o != NULL; o = o->next)
10943 if ((o->flags & SEC_RELOC) != 0
10944 && elf_section_data (o)->rel_hashes != NULL)
10945 free (elf_section_data (o)->rel_hashes);
10948 return FALSE;
10951 /* Initialize COOKIE for input bfd ABFD. */
10953 static bfd_boolean
10954 init_reloc_cookie (struct elf_reloc_cookie *cookie,
10955 struct bfd_link_info *info, bfd *abfd)
10957 Elf_Internal_Shdr *symtab_hdr;
10958 const struct elf_backend_data *bed;
10960 bed = get_elf_backend_data (abfd);
10961 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10963 cookie->abfd = abfd;
10964 cookie->sym_hashes = elf_sym_hashes (abfd);
10965 cookie->bad_symtab = elf_bad_symtab (abfd);
10966 if (cookie->bad_symtab)
10968 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10969 cookie->extsymoff = 0;
10971 else
10973 cookie->locsymcount = symtab_hdr->sh_info;
10974 cookie->extsymoff = symtab_hdr->sh_info;
10977 if (bed->s->arch_size == 32)
10978 cookie->r_sym_shift = 8;
10979 else
10980 cookie->r_sym_shift = 32;
10982 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
10983 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
10985 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
10986 cookie->locsymcount, 0,
10987 NULL, NULL, NULL);
10988 if (cookie->locsyms == NULL)
10990 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
10991 return FALSE;
10993 if (info->keep_memory)
10994 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
10996 return TRUE;
10999 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11001 static void
11002 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11004 Elf_Internal_Shdr *symtab_hdr;
11006 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11007 if (cookie->locsyms != NULL
11008 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11009 free (cookie->locsyms);
11012 /* Initialize the relocation information in COOKIE for input section SEC
11013 of input bfd ABFD. */
11015 static bfd_boolean
11016 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11017 struct bfd_link_info *info, bfd *abfd,
11018 asection *sec)
11020 const struct elf_backend_data *bed;
11022 if (sec->reloc_count == 0)
11024 cookie->rels = NULL;
11025 cookie->relend = NULL;
11027 else
11029 bed = get_elf_backend_data (abfd);
11031 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11032 info->keep_memory);
11033 if (cookie->rels == NULL)
11034 return FALSE;
11035 cookie->rel = cookie->rels;
11036 cookie->relend = (cookie->rels
11037 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11039 cookie->rel = cookie->rels;
11040 return TRUE;
11043 /* Free the memory allocated by init_reloc_cookie_rels,
11044 if appropriate. */
11046 static void
11047 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11048 asection *sec)
11050 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11051 free (cookie->rels);
11054 /* Initialize the whole of COOKIE for input section SEC. */
11056 static bfd_boolean
11057 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11058 struct bfd_link_info *info,
11059 asection *sec)
11061 if (!init_reloc_cookie (cookie, info, sec->owner))
11062 goto error1;
11063 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11064 goto error2;
11065 return TRUE;
11067 error2:
11068 fini_reloc_cookie (cookie, sec->owner);
11069 error1:
11070 return FALSE;
11073 /* Free the memory allocated by init_reloc_cookie_for_section,
11074 if appropriate. */
11076 static void
11077 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11078 asection *sec)
11080 fini_reloc_cookie_rels (cookie, sec);
11081 fini_reloc_cookie (cookie, sec->owner);
11084 /* Garbage collect unused sections. */
11086 /* Default gc_mark_hook. */
11088 asection *
11089 _bfd_elf_gc_mark_hook (asection *sec,
11090 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11091 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11092 struct elf_link_hash_entry *h,
11093 Elf_Internal_Sym *sym)
11095 if (h != NULL)
11097 switch (h->root.type)
11099 case bfd_link_hash_defined:
11100 case bfd_link_hash_defweak:
11101 return h->root.u.def.section;
11103 case bfd_link_hash_common:
11104 return h->root.u.c.p->section;
11106 default:
11107 break;
11110 else
11111 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11113 return NULL;
11116 /* COOKIE->rel describes a relocation against section SEC, which is
11117 a section we've decided to keep. Return the section that contains
11118 the relocation symbol, or NULL if no section contains it. */
11120 asection *
11121 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11122 elf_gc_mark_hook_fn gc_mark_hook,
11123 struct elf_reloc_cookie *cookie)
11125 unsigned long r_symndx;
11126 struct elf_link_hash_entry *h;
11128 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11129 if (r_symndx == 0)
11130 return NULL;
11132 if (r_symndx >= cookie->locsymcount
11133 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11135 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11136 while (h->root.type == bfd_link_hash_indirect
11137 || h->root.type == bfd_link_hash_warning)
11138 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11139 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11142 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11143 &cookie->locsyms[r_symndx]);
11146 /* COOKIE->rel describes a relocation against section SEC, which is
11147 a section we've decided to keep. Mark the section that contains
11148 the relocation symbol. */
11150 bfd_boolean
11151 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11152 asection *sec,
11153 elf_gc_mark_hook_fn gc_mark_hook,
11154 struct elf_reloc_cookie *cookie)
11156 asection *rsec;
11158 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11159 if (rsec && !rsec->gc_mark)
11161 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11162 rsec->gc_mark = 1;
11163 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11164 return FALSE;
11166 return TRUE;
11169 /* The mark phase of garbage collection. For a given section, mark
11170 it and any sections in this section's group, and all the sections
11171 which define symbols to which it refers. */
11173 bfd_boolean
11174 _bfd_elf_gc_mark (struct bfd_link_info *info,
11175 asection *sec,
11176 elf_gc_mark_hook_fn gc_mark_hook)
11178 bfd_boolean ret;
11179 asection *group_sec, *eh_frame;
11181 sec->gc_mark = 1;
11183 /* Mark all the sections in the group. */
11184 group_sec = elf_section_data (sec)->next_in_group;
11185 if (group_sec && !group_sec->gc_mark)
11186 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11187 return FALSE;
11189 /* Look through the section relocs. */
11190 ret = TRUE;
11191 eh_frame = elf_eh_frame_section (sec->owner);
11192 if ((sec->flags & SEC_RELOC) != 0
11193 && sec->reloc_count > 0
11194 && sec != eh_frame)
11196 struct elf_reloc_cookie cookie;
11198 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11199 ret = FALSE;
11200 else
11202 for (; cookie.rel < cookie.relend; cookie.rel++)
11203 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11205 ret = FALSE;
11206 break;
11208 fini_reloc_cookie_for_section (&cookie, sec);
11212 if (ret && eh_frame && elf_fde_list (sec))
11214 struct elf_reloc_cookie cookie;
11216 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11217 ret = FALSE;
11218 else
11220 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11221 gc_mark_hook, &cookie))
11222 ret = FALSE;
11223 fini_reloc_cookie_for_section (&cookie, eh_frame);
11227 return ret;
11230 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11232 struct elf_gc_sweep_symbol_info
11234 struct bfd_link_info *info;
11235 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11236 bfd_boolean);
11239 static bfd_boolean
11240 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11242 if (h->root.type == bfd_link_hash_warning)
11243 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11245 if ((h->root.type == bfd_link_hash_defined
11246 || h->root.type == bfd_link_hash_defweak)
11247 && !h->root.u.def.section->gc_mark
11248 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11250 struct elf_gc_sweep_symbol_info *inf = data;
11251 (*inf->hide_symbol) (inf->info, h, TRUE);
11254 return TRUE;
11257 /* The sweep phase of garbage collection. Remove all garbage sections. */
11259 typedef bfd_boolean (*gc_sweep_hook_fn)
11260 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11262 static bfd_boolean
11263 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11265 bfd *sub;
11266 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11267 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11268 unsigned long section_sym_count;
11269 struct elf_gc_sweep_symbol_info sweep_info;
11271 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11273 asection *o;
11275 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11276 continue;
11278 for (o = sub->sections; o != NULL; o = o->next)
11280 /* Keep debug and special sections. */
11281 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
11282 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
11283 o->gc_mark = 1;
11285 if (o->gc_mark)
11286 continue;
11288 /* Skip sweeping sections already excluded. */
11289 if (o->flags & SEC_EXCLUDE)
11290 continue;
11292 /* Since this is early in the link process, it is simple
11293 to remove a section from the output. */
11294 o->flags |= SEC_EXCLUDE;
11296 if (info->print_gc_sections && o->size != 0)
11297 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11299 /* But we also have to update some of the relocation
11300 info we collected before. */
11301 if (gc_sweep_hook
11302 && (o->flags & SEC_RELOC) != 0
11303 && o->reloc_count > 0
11304 && !bfd_is_abs_section (o->output_section))
11306 Elf_Internal_Rela *internal_relocs;
11307 bfd_boolean r;
11309 internal_relocs
11310 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11311 info->keep_memory);
11312 if (internal_relocs == NULL)
11313 return FALSE;
11315 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11317 if (elf_section_data (o)->relocs != internal_relocs)
11318 free (internal_relocs);
11320 if (!r)
11321 return FALSE;
11326 /* Remove the symbols that were in the swept sections from the dynamic
11327 symbol table. GCFIXME: Anyone know how to get them out of the
11328 static symbol table as well? */
11329 sweep_info.info = info;
11330 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11331 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11332 &sweep_info);
11334 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
11335 return TRUE;
11338 /* Propagate collected vtable information. This is called through
11339 elf_link_hash_traverse. */
11341 static bfd_boolean
11342 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11344 if (h->root.type == bfd_link_hash_warning)
11345 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11347 /* Those that are not vtables. */
11348 if (h->vtable == NULL || h->vtable->parent == NULL)
11349 return TRUE;
11351 /* Those vtables that do not have parents, we cannot merge. */
11352 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11353 return TRUE;
11355 /* If we've already been done, exit. */
11356 if (h->vtable->used && h->vtable->used[-1])
11357 return TRUE;
11359 /* Make sure the parent's table is up to date. */
11360 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11362 if (h->vtable->used == NULL)
11364 /* None of this table's entries were referenced. Re-use the
11365 parent's table. */
11366 h->vtable->used = h->vtable->parent->vtable->used;
11367 h->vtable->size = h->vtable->parent->vtable->size;
11369 else
11371 size_t n;
11372 bfd_boolean *cu, *pu;
11374 /* Or the parent's entries into ours. */
11375 cu = h->vtable->used;
11376 cu[-1] = TRUE;
11377 pu = h->vtable->parent->vtable->used;
11378 if (pu != NULL)
11380 const struct elf_backend_data *bed;
11381 unsigned int log_file_align;
11383 bed = get_elf_backend_data (h->root.u.def.section->owner);
11384 log_file_align = bed->s->log_file_align;
11385 n = h->vtable->parent->vtable->size >> log_file_align;
11386 while (n--)
11388 if (*pu)
11389 *cu = TRUE;
11390 pu++;
11391 cu++;
11396 return TRUE;
11399 static bfd_boolean
11400 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11402 asection *sec;
11403 bfd_vma hstart, hend;
11404 Elf_Internal_Rela *relstart, *relend, *rel;
11405 const struct elf_backend_data *bed;
11406 unsigned int log_file_align;
11408 if (h->root.type == bfd_link_hash_warning)
11409 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11411 /* Take care of both those symbols that do not describe vtables as
11412 well as those that are not loaded. */
11413 if (h->vtable == NULL || h->vtable->parent == NULL)
11414 return TRUE;
11416 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11417 || h->root.type == bfd_link_hash_defweak);
11419 sec = h->root.u.def.section;
11420 hstart = h->root.u.def.value;
11421 hend = hstart + h->size;
11423 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11424 if (!relstart)
11425 return *(bfd_boolean *) okp = FALSE;
11426 bed = get_elf_backend_data (sec->owner);
11427 log_file_align = bed->s->log_file_align;
11429 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11431 for (rel = relstart; rel < relend; ++rel)
11432 if (rel->r_offset >= hstart && rel->r_offset < hend)
11434 /* If the entry is in use, do nothing. */
11435 if (h->vtable->used
11436 && (rel->r_offset - hstart) < h->vtable->size)
11438 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
11439 if (h->vtable->used[entry])
11440 continue;
11442 /* Otherwise, kill it. */
11443 rel->r_offset = rel->r_info = rel->r_addend = 0;
11446 return TRUE;
11449 /* Mark sections containing dynamically referenced symbols. When
11450 building shared libraries, we must assume that any visible symbol is
11451 referenced. */
11453 bfd_boolean
11454 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
11456 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11458 if (h->root.type == bfd_link_hash_warning)
11459 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11461 if ((h->root.type == bfd_link_hash_defined
11462 || h->root.type == bfd_link_hash_defweak)
11463 && (h->ref_dynamic
11464 || (!info->executable
11465 && h->def_regular
11466 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11467 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
11468 h->root.u.def.section->flags |= SEC_KEEP;
11470 return TRUE;
11473 /* Keep all sections containing symbols undefined on the command-line,
11474 and the section containing the entry symbol. */
11476 void
11477 _bfd_elf_gc_keep (struct bfd_link_info *info)
11479 struct bfd_sym_chain *sym;
11481 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11483 struct elf_link_hash_entry *h;
11485 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11486 FALSE, FALSE, FALSE);
11488 if (h != NULL
11489 && (h->root.type == bfd_link_hash_defined
11490 || h->root.type == bfd_link_hash_defweak)
11491 && !bfd_is_abs_section (h->root.u.def.section))
11492 h->root.u.def.section->flags |= SEC_KEEP;
11496 /* Do mark and sweep of unused sections. */
11498 bfd_boolean
11499 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11501 bfd_boolean ok = TRUE;
11502 bfd *sub;
11503 elf_gc_mark_hook_fn gc_mark_hook;
11504 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11506 if (!bed->can_gc_sections
11507 || !is_elf_hash_table (info->hash))
11509 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11510 return TRUE;
11513 bed->gc_keep (info);
11515 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11516 at the .eh_frame section if we can mark the FDEs individually. */
11517 _bfd_elf_begin_eh_frame_parsing (info);
11518 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11520 asection *sec;
11521 struct elf_reloc_cookie cookie;
11523 sec = bfd_get_section_by_name (sub, ".eh_frame");
11524 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11526 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11527 if (elf_section_data (sec)->sec_info)
11528 elf_eh_frame_section (sub) = sec;
11529 fini_reloc_cookie_for_section (&cookie, sec);
11532 _bfd_elf_end_eh_frame_parsing (info);
11534 /* Apply transitive closure to the vtable entry usage info. */
11535 elf_link_hash_traverse (elf_hash_table (info),
11536 elf_gc_propagate_vtable_entries_used,
11537 &ok);
11538 if (!ok)
11539 return FALSE;
11541 /* Kill the vtable relocations that were not used. */
11542 elf_link_hash_traverse (elf_hash_table (info),
11543 elf_gc_smash_unused_vtentry_relocs,
11544 &ok);
11545 if (!ok)
11546 return FALSE;
11548 /* Mark dynamically referenced symbols. */
11549 if (elf_hash_table (info)->dynamic_sections_created)
11550 elf_link_hash_traverse (elf_hash_table (info),
11551 bed->gc_mark_dynamic_ref,
11552 info);
11554 /* Grovel through relocs to find out who stays ... */
11555 gc_mark_hook = bed->gc_mark_hook;
11556 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11558 asection *o;
11560 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11561 continue;
11563 for (o = sub->sections; o != NULL; o = o->next)
11564 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
11565 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11566 return FALSE;
11569 /* Allow the backend to mark additional target specific sections. */
11570 if (bed->gc_mark_extra_sections)
11571 bed->gc_mark_extra_sections (info, gc_mark_hook);
11573 /* ... and mark SEC_EXCLUDE for those that go. */
11574 return elf_gc_sweep (abfd, info);
11577 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11579 bfd_boolean
11580 bfd_elf_gc_record_vtinherit (bfd *abfd,
11581 asection *sec,
11582 struct elf_link_hash_entry *h,
11583 bfd_vma offset)
11585 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
11586 struct elf_link_hash_entry **search, *child;
11587 bfd_size_type extsymcount;
11588 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11590 /* The sh_info field of the symtab header tells us where the
11591 external symbols start. We don't care about the local symbols at
11592 this point. */
11593 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
11594 if (!elf_bad_symtab (abfd))
11595 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
11597 sym_hashes = elf_sym_hashes (abfd);
11598 sym_hashes_end = sym_hashes + extsymcount;
11600 /* Hunt down the child symbol, which is in this section at the same
11601 offset as the relocation. */
11602 for (search = sym_hashes; search != sym_hashes_end; ++search)
11604 if ((child = *search) != NULL
11605 && (child->root.type == bfd_link_hash_defined
11606 || child->root.type == bfd_link_hash_defweak)
11607 && child->root.u.def.section == sec
11608 && child->root.u.def.value == offset)
11609 goto win;
11612 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
11613 abfd, sec, (unsigned long) offset);
11614 bfd_set_error (bfd_error_invalid_operation);
11615 return FALSE;
11617 win:
11618 if (!child->vtable)
11620 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
11621 if (!child->vtable)
11622 return FALSE;
11624 if (!h)
11626 /* This *should* only be the absolute section. It could potentially
11627 be that someone has defined a non-global vtable though, which
11628 would be bad. It isn't worth paging in the local symbols to be
11629 sure though; that case should simply be handled by the assembler. */
11631 child->vtable->parent = (struct elf_link_hash_entry *) -1;
11633 else
11634 child->vtable->parent = h;
11636 return TRUE;
11639 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11641 bfd_boolean
11642 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
11643 asection *sec ATTRIBUTE_UNUSED,
11644 struct elf_link_hash_entry *h,
11645 bfd_vma addend)
11647 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11648 unsigned int log_file_align = bed->s->log_file_align;
11650 if (!h->vtable)
11652 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
11653 if (!h->vtable)
11654 return FALSE;
11657 if (addend >= h->vtable->size)
11659 size_t size, bytes, file_align;
11660 bfd_boolean *ptr = h->vtable->used;
11662 /* While the symbol is undefined, we have to be prepared to handle
11663 a zero size. */
11664 file_align = 1 << log_file_align;
11665 if (h->root.type == bfd_link_hash_undefined)
11666 size = addend + file_align;
11667 else
11669 size = h->size;
11670 if (addend >= size)
11672 /* Oops! We've got a reference past the defined end of
11673 the table. This is probably a bug -- shall we warn? */
11674 size = addend + file_align;
11677 size = (size + file_align - 1) & -file_align;
11679 /* Allocate one extra entry for use as a "done" flag for the
11680 consolidation pass. */
11681 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11683 if (ptr)
11685 ptr = bfd_realloc (ptr - 1, bytes);
11687 if (ptr != NULL)
11689 size_t oldbytes;
11691 oldbytes = (((h->vtable->size >> log_file_align) + 1)
11692 * sizeof (bfd_boolean));
11693 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11696 else
11697 ptr = bfd_zmalloc (bytes);
11699 if (ptr == NULL)
11700 return FALSE;
11702 /* And arrange for that done flag to be at index -1. */
11703 h->vtable->used = ptr + 1;
11704 h->vtable->size = size;
11707 h->vtable->used[addend >> log_file_align] = TRUE;
11709 return TRUE;
11712 struct alloc_got_off_arg {
11713 bfd_vma gotoff;
11714 unsigned int got_elt_size;
11717 /* We need a special top-level link routine to convert got reference counts
11718 to real got offsets. */
11720 static bfd_boolean
11721 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11723 struct alloc_got_off_arg *gofarg = arg;
11725 if (h->root.type == bfd_link_hash_warning)
11726 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11728 if (h->got.refcount > 0)
11730 h->got.offset = gofarg->gotoff;
11731 gofarg->gotoff += gofarg->got_elt_size;
11733 else
11734 h->got.offset = (bfd_vma) -1;
11736 return TRUE;
11739 /* And an accompanying bit to work out final got entry offsets once
11740 we're done. Should be called from final_link. */
11742 bfd_boolean
11743 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11744 struct bfd_link_info *info)
11746 bfd *i;
11747 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11748 bfd_vma gotoff;
11749 unsigned int got_elt_size = bed->s->arch_size / 8;
11750 struct alloc_got_off_arg gofarg;
11752 if (! is_elf_hash_table (info->hash))
11753 return FALSE;
11755 /* The GOT offset is relative to the .got section, but the GOT header is
11756 put into the .got.plt section, if the backend uses it. */
11757 if (bed->want_got_plt)
11758 gotoff = 0;
11759 else
11760 gotoff = bed->got_header_size;
11762 /* Do the local .got entries first. */
11763 for (i = info->input_bfds; i; i = i->link_next)
11765 bfd_signed_vma *local_got;
11766 bfd_size_type j, locsymcount;
11767 Elf_Internal_Shdr *symtab_hdr;
11769 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11770 continue;
11772 local_got = elf_local_got_refcounts (i);
11773 if (!local_got)
11774 continue;
11776 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11777 if (elf_bad_symtab (i))
11778 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11779 else
11780 locsymcount = symtab_hdr->sh_info;
11782 for (j = 0; j < locsymcount; ++j)
11784 if (local_got[j] > 0)
11786 local_got[j] = gotoff;
11787 gotoff += got_elt_size;
11789 else
11790 local_got[j] = (bfd_vma) -1;
11794 /* Then the global .got entries. .plt refcounts are handled by
11795 adjust_dynamic_symbol */
11796 gofarg.gotoff = gotoff;
11797 gofarg.got_elt_size = got_elt_size;
11798 elf_link_hash_traverse (elf_hash_table (info),
11799 elf_gc_allocate_got_offsets,
11800 &gofarg);
11801 return TRUE;
11804 /* Many folk need no more in the way of final link than this, once
11805 got entry reference counting is enabled. */
11807 bfd_boolean
11808 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11810 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11811 return FALSE;
11813 /* Invoke the regular ELF backend linker to do all the work. */
11814 return bfd_elf_final_link (abfd, info);
11817 bfd_boolean
11818 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11820 struct elf_reloc_cookie *rcookie = cookie;
11822 if (rcookie->bad_symtab)
11823 rcookie->rel = rcookie->rels;
11825 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11827 unsigned long r_symndx;
11829 if (! rcookie->bad_symtab)
11830 if (rcookie->rel->r_offset > offset)
11831 return FALSE;
11832 if (rcookie->rel->r_offset != offset)
11833 continue;
11835 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11836 if (r_symndx == SHN_UNDEF)
11837 return TRUE;
11839 if (r_symndx >= rcookie->locsymcount
11840 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11842 struct elf_link_hash_entry *h;
11844 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11846 while (h->root.type == bfd_link_hash_indirect
11847 || h->root.type == bfd_link_hash_warning)
11848 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11850 if ((h->root.type == bfd_link_hash_defined
11851 || h->root.type == bfd_link_hash_defweak)
11852 && elf_discarded_section (h->root.u.def.section))
11853 return TRUE;
11854 else
11855 return FALSE;
11857 else
11859 /* It's not a relocation against a global symbol,
11860 but it could be a relocation against a local
11861 symbol for a discarded section. */
11862 asection *isec;
11863 Elf_Internal_Sym *isym;
11865 /* Need to: get the symbol; get the section. */
11866 isym = &rcookie->locsyms[r_symndx];
11867 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
11869 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11870 if (isec != NULL && elf_discarded_section (isec))
11871 return TRUE;
11874 return FALSE;
11876 return FALSE;
11879 /* Discard unneeded references to discarded sections.
11880 Returns TRUE if any section's size was changed. */
11881 /* This function assumes that the relocations are in sorted order,
11882 which is true for all known assemblers. */
11884 bfd_boolean
11885 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
11887 struct elf_reloc_cookie cookie;
11888 asection *stab, *eh;
11889 const struct elf_backend_data *bed;
11890 bfd *abfd;
11891 bfd_boolean ret = FALSE;
11893 if (info->traditional_format
11894 || !is_elf_hash_table (info->hash))
11895 return FALSE;
11897 _bfd_elf_begin_eh_frame_parsing (info);
11898 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
11900 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11901 continue;
11903 bed = get_elf_backend_data (abfd);
11905 if ((abfd->flags & DYNAMIC) != 0)
11906 continue;
11908 eh = NULL;
11909 if (!info->relocatable)
11911 eh = bfd_get_section_by_name (abfd, ".eh_frame");
11912 if (eh != NULL
11913 && (eh->size == 0
11914 || bfd_is_abs_section (eh->output_section)))
11915 eh = NULL;
11918 stab = bfd_get_section_by_name (abfd, ".stab");
11919 if (stab != NULL
11920 && (stab->size == 0
11921 || bfd_is_abs_section (stab->output_section)
11922 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
11923 stab = NULL;
11925 if (stab == NULL
11926 && eh == NULL
11927 && bed->elf_backend_discard_info == NULL)
11928 continue;
11930 if (!init_reloc_cookie (&cookie, info, abfd))
11931 return FALSE;
11933 if (stab != NULL
11934 && stab->reloc_count > 0
11935 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
11937 if (_bfd_discard_section_stabs (abfd, stab,
11938 elf_section_data (stab)->sec_info,
11939 bfd_elf_reloc_symbol_deleted_p,
11940 &cookie))
11941 ret = TRUE;
11942 fini_reloc_cookie_rels (&cookie, stab);
11945 if (eh != NULL
11946 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
11948 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
11949 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
11950 bfd_elf_reloc_symbol_deleted_p,
11951 &cookie))
11952 ret = TRUE;
11953 fini_reloc_cookie_rels (&cookie, eh);
11956 if (bed->elf_backend_discard_info != NULL
11957 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
11958 ret = TRUE;
11960 fini_reloc_cookie (&cookie, abfd);
11962 _bfd_elf_end_eh_frame_parsing (info);
11964 if (info->eh_frame_hdr
11965 && !info->relocatable
11966 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
11967 ret = TRUE;
11969 return ret;
11972 void
11973 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec,
11974 struct bfd_link_info *info)
11976 flagword flags;
11977 const char *name, *p;
11978 struct bfd_section_already_linked *l;
11979 struct bfd_section_already_linked_hash_entry *already_linked_list;
11981 if (sec->output_section == bfd_abs_section_ptr)
11982 return;
11984 flags = sec->flags;
11986 /* Return if it isn't a linkonce section. A comdat group section
11987 also has SEC_LINK_ONCE set. */
11988 if ((flags & SEC_LINK_ONCE) == 0)
11989 return;
11991 /* Don't put group member sections on our list of already linked
11992 sections. They are handled as a group via their group section. */
11993 if (elf_sec_group (sec) != NULL)
11994 return;
11996 /* FIXME: When doing a relocatable link, we may have trouble
11997 copying relocations in other sections that refer to local symbols
11998 in the section being discarded. Those relocations will have to
11999 be converted somehow; as of this writing I'm not sure that any of
12000 the backends handle that correctly.
12002 It is tempting to instead not discard link once sections when
12003 doing a relocatable link (technically, they should be discarded
12004 whenever we are building constructors). However, that fails,
12005 because the linker winds up combining all the link once sections
12006 into a single large link once section, which defeats the purpose
12007 of having link once sections in the first place.
12009 Also, not merging link once sections in a relocatable link
12010 causes trouble for MIPS ELF, which relies on link once semantics
12011 to handle the .reginfo section correctly. */
12013 name = bfd_get_section_name (abfd, sec);
12015 if (CONST_STRNEQ (name, ".gnu.linkonce.")
12016 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12017 p++;
12018 else
12019 p = name;
12021 already_linked_list = bfd_section_already_linked_table_lookup (p);
12023 for (l = already_linked_list->entry; l != NULL; l = l->next)
12025 /* We may have 2 different types of sections on the list: group
12026 sections and linkonce sections. Match like sections. */
12027 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12028 && strcmp (name, l->sec->name) == 0
12029 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12031 /* The section has already been linked. See if we should
12032 issue a warning. */
12033 switch (flags & SEC_LINK_DUPLICATES)
12035 default:
12036 abort ();
12038 case SEC_LINK_DUPLICATES_DISCARD:
12039 break;
12041 case SEC_LINK_DUPLICATES_ONE_ONLY:
12042 (*_bfd_error_handler)
12043 (_("%B: ignoring duplicate section `%A'"),
12044 abfd, sec);
12045 break;
12047 case SEC_LINK_DUPLICATES_SAME_SIZE:
12048 if (sec->size != l->sec->size)
12049 (*_bfd_error_handler)
12050 (_("%B: duplicate section `%A' has different size"),
12051 abfd, sec);
12052 break;
12054 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12055 if (sec->size != l->sec->size)
12056 (*_bfd_error_handler)
12057 (_("%B: duplicate section `%A' has different size"),
12058 abfd, sec);
12059 else if (sec->size != 0)
12061 bfd_byte *sec_contents, *l_sec_contents;
12063 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12064 (*_bfd_error_handler)
12065 (_("%B: warning: could not read contents of section `%A'"),
12066 abfd, sec);
12067 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12068 &l_sec_contents))
12069 (*_bfd_error_handler)
12070 (_("%B: warning: could not read contents of section `%A'"),
12071 l->sec->owner, l->sec);
12072 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12073 (*_bfd_error_handler)
12074 (_("%B: warning: duplicate section `%A' has different contents"),
12075 abfd, sec);
12077 if (sec_contents)
12078 free (sec_contents);
12079 if (l_sec_contents)
12080 free (l_sec_contents);
12082 break;
12085 /* Set the output_section field so that lang_add_section
12086 does not create a lang_input_section structure for this
12087 section. Since there might be a symbol in the section
12088 being discarded, we must retain a pointer to the section
12089 which we are really going to use. */
12090 sec->output_section = bfd_abs_section_ptr;
12091 sec->kept_section = l->sec;
12093 if (flags & SEC_GROUP)
12095 asection *first = elf_next_in_group (sec);
12096 asection *s = first;
12098 while (s != NULL)
12100 s->output_section = bfd_abs_section_ptr;
12101 /* Record which group discards it. */
12102 s->kept_section = l->sec;
12103 s = elf_next_in_group (s);
12104 /* These lists are circular. */
12105 if (s == first)
12106 break;
12110 return;
12114 /* A single member comdat group section may be discarded by a
12115 linkonce section and vice versa. */
12117 if ((flags & SEC_GROUP) != 0)
12119 asection *first = elf_next_in_group (sec);
12121 if (first != NULL && elf_next_in_group (first) == first)
12122 /* Check this single member group against linkonce sections. */
12123 for (l = already_linked_list->entry; l != NULL; l = l->next)
12124 if ((l->sec->flags & SEC_GROUP) == 0
12125 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12126 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12128 first->output_section = bfd_abs_section_ptr;
12129 first->kept_section = l->sec;
12130 sec->output_section = bfd_abs_section_ptr;
12131 break;
12134 else
12135 /* Check this linkonce section against single member groups. */
12136 for (l = already_linked_list->entry; l != NULL; l = l->next)
12137 if (l->sec->flags & SEC_GROUP)
12139 asection *first = elf_next_in_group (l->sec);
12141 if (first != NULL
12142 && elf_next_in_group (first) == first
12143 && bfd_elf_match_symbols_in_sections (first, sec, info))
12145 sec->output_section = bfd_abs_section_ptr;
12146 sec->kept_section = first;
12147 break;
12151 /* This is the first section with this name. Record it. */
12152 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
12153 info->callbacks->einfo (_("%F%P: already_linked_table: %E"));
12156 bfd_boolean
12157 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
12159 return sym->st_shndx == SHN_COMMON;
12162 unsigned int
12163 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12165 return SHN_COMMON;
12168 asection *
12169 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12171 return bfd_com_section_ptr;