PR 7093
[binutils.git] / bfd / elflink.c
blob92b40e9d9f700de401653ed204bd002bbe9fcfd0
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->rela_plts_and_copies_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->rela_plts_and_copies_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)
623 asection *s;
625 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
626 if (s == NULL || bfd_is_abs_section (s->output_section))
628 /* We can still bfd_release here as nothing has done another
629 bfd_alloc. We can't do this later in this function. */
630 bfd_release (input_bfd, entry);
631 return 2;
635 name = (bfd_elf_string_from_elf_section
636 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
637 entry->isym.st_name));
639 dynstr = elf_hash_table (info)->dynstr;
640 if (dynstr == NULL)
642 /* Create a strtab to hold the dynamic symbol names. */
643 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
644 if (dynstr == NULL)
645 return 0;
648 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
649 if (dynstr_index == (unsigned long) -1)
650 return 0;
651 entry->isym.st_name = dynstr_index;
653 eht = elf_hash_table (info);
655 entry->next = eht->dynlocal;
656 eht->dynlocal = entry;
657 entry->input_bfd = input_bfd;
658 entry->input_indx = input_indx;
659 eht->dynsymcount++;
661 /* Whatever binding the symbol had before, it's now local. */
662 entry->isym.st_info
663 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
665 /* The dynindx will be set at the end of size_dynamic_sections. */
667 return 1;
670 /* Return the dynindex of a local dynamic symbol. */
672 long
673 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
674 bfd *input_bfd,
675 long input_indx)
677 struct elf_link_local_dynamic_entry *e;
679 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
680 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
681 return e->dynindx;
682 return -1;
685 /* This function is used to renumber the dynamic symbols, if some of
686 them are removed because they are marked as local. This is called
687 via elf_link_hash_traverse. */
689 static bfd_boolean
690 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
691 void *data)
693 size_t *count = data;
695 if (h->root.type == bfd_link_hash_warning)
696 h = (struct elf_link_hash_entry *) h->root.u.i.link;
698 if (h->forced_local)
699 return TRUE;
701 if (h->dynindx != -1)
702 h->dynindx = ++(*count);
704 return TRUE;
708 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
709 STB_LOCAL binding. */
711 static bfd_boolean
712 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
713 void *data)
715 size_t *count = data;
717 if (h->root.type == bfd_link_hash_warning)
718 h = (struct elf_link_hash_entry *) h->root.u.i.link;
720 if (!h->forced_local)
721 return TRUE;
723 if (h->dynindx != -1)
724 h->dynindx = ++(*count);
726 return TRUE;
729 /* Return true if the dynamic symbol for a given section should be
730 omitted when creating a shared library. */
731 bfd_boolean
732 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
733 struct bfd_link_info *info,
734 asection *p)
736 struct elf_link_hash_table *htab;
738 switch (elf_section_data (p)->this_hdr.sh_type)
740 case SHT_PROGBITS:
741 case SHT_NOBITS:
742 /* If sh_type is yet undecided, assume it could be
743 SHT_PROGBITS/SHT_NOBITS. */
744 case SHT_NULL:
745 htab = elf_hash_table (info);
746 if (p == htab->tls_sec)
747 return FALSE;
749 if (htab->text_index_section != NULL)
750 return p != htab->text_index_section && p != htab->data_index_section;
752 if (strcmp (p->name, ".got") == 0
753 || strcmp (p->name, ".got.plt") == 0
754 || strcmp (p->name, ".plt") == 0)
756 asection *ip;
758 if (htab->dynobj != NULL
759 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
760 && (ip->flags & SEC_LINKER_CREATED)
761 && ip->output_section == p)
762 return TRUE;
764 return FALSE;
766 /* There shouldn't be section relative relocations
767 against any other section. */
768 default:
769 return TRUE;
773 /* Assign dynsym indices. In a shared library we generate a section
774 symbol for each output section, which come first. Next come symbols
775 which have been forced to local binding. Then all of the back-end
776 allocated local dynamic syms, followed by the rest of the global
777 symbols. */
779 static unsigned long
780 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
781 struct bfd_link_info *info,
782 unsigned long *section_sym_count)
784 unsigned long dynsymcount = 0;
786 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
788 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
789 asection *p;
790 for (p = output_bfd->sections; p ; p = p->next)
791 if ((p->flags & SEC_EXCLUDE) == 0
792 && (p->flags & SEC_ALLOC) != 0
793 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
794 elf_section_data (p)->dynindx = ++dynsymcount;
795 else
796 elf_section_data (p)->dynindx = 0;
798 *section_sym_count = dynsymcount;
800 elf_link_hash_traverse (elf_hash_table (info),
801 elf_link_renumber_local_hash_table_dynsyms,
802 &dynsymcount);
804 if (elf_hash_table (info)->dynlocal)
806 struct elf_link_local_dynamic_entry *p;
807 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
808 p->dynindx = ++dynsymcount;
811 elf_link_hash_traverse (elf_hash_table (info),
812 elf_link_renumber_hash_table_dynsyms,
813 &dynsymcount);
815 /* There is an unused NULL entry at the head of the table which
816 we must account for in our count. Unless there weren't any
817 symbols, which means we'll have no table at all. */
818 if (dynsymcount != 0)
819 ++dynsymcount;
821 elf_hash_table (info)->dynsymcount = dynsymcount;
822 return dynsymcount;
825 /* This function is called when we want to define a new symbol. It
826 handles the various cases which arise when we find a definition in
827 a dynamic object, or when there is already a definition in a
828 dynamic object. The new symbol is described by NAME, SYM, PSEC,
829 and PVALUE. We set SYM_HASH to the hash table entry. We set
830 OVERRIDE if the old symbol is overriding a new definition. We set
831 TYPE_CHANGE_OK if it is OK for the type to change. We set
832 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
833 change, we mean that we shouldn't warn if the type or size does
834 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
835 object is overridden by a regular object. */
837 bfd_boolean
838 _bfd_elf_merge_symbol (bfd *abfd,
839 struct bfd_link_info *info,
840 const char *name,
841 Elf_Internal_Sym *sym,
842 asection **psec,
843 bfd_vma *pvalue,
844 unsigned int *pold_alignment,
845 struct elf_link_hash_entry **sym_hash,
846 bfd_boolean *skip,
847 bfd_boolean *override,
848 bfd_boolean *type_change_ok,
849 bfd_boolean *size_change_ok)
851 asection *sec, *oldsec;
852 struct elf_link_hash_entry *h;
853 struct elf_link_hash_entry *flip;
854 int bind;
855 bfd *oldbfd;
856 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
857 bfd_boolean newweak, oldweak, newfunc, oldfunc;
858 const struct elf_backend_data *bed;
860 *skip = FALSE;
861 *override = FALSE;
863 sec = *psec;
864 bind = ELF_ST_BIND (sym->st_info);
866 /* Silently discard TLS symbols from --just-syms. There's no way to
867 combine a static TLS block with a new TLS block for this executable. */
868 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
869 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
871 *skip = TRUE;
872 return TRUE;
875 if (! bfd_is_und_section (sec))
876 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
877 else
878 h = ((struct elf_link_hash_entry *)
879 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
880 if (h == NULL)
881 return FALSE;
882 *sym_hash = h;
884 bed = get_elf_backend_data (abfd);
886 /* This code is for coping with dynamic objects, and is only useful
887 if we are doing an ELF link. */
888 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
889 return TRUE;
891 /* For merging, we only care about real symbols. */
893 while (h->root.type == bfd_link_hash_indirect
894 || h->root.type == bfd_link_hash_warning)
895 h = (struct elf_link_hash_entry *) h->root.u.i.link;
897 /* We have to check it for every instance since the first few may be
898 refereences and not all compilers emit symbol type for undefined
899 symbols. */
900 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
902 /* If we just created the symbol, mark it as being an ELF symbol.
903 Other than that, there is nothing to do--there is no merge issue
904 with a newly defined symbol--so we just return. */
906 if (h->root.type == bfd_link_hash_new)
908 h->non_elf = 0;
909 return TRUE;
912 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
913 existing symbol. */
915 switch (h->root.type)
917 default:
918 oldbfd = NULL;
919 oldsec = NULL;
920 break;
922 case bfd_link_hash_undefined:
923 case bfd_link_hash_undefweak:
924 oldbfd = h->root.u.undef.abfd;
925 oldsec = NULL;
926 break;
928 case bfd_link_hash_defined:
929 case bfd_link_hash_defweak:
930 oldbfd = h->root.u.def.section->owner;
931 oldsec = h->root.u.def.section;
932 break;
934 case bfd_link_hash_common:
935 oldbfd = h->root.u.c.p->section->owner;
936 oldsec = h->root.u.c.p->section;
937 break;
940 /* In cases involving weak versioned symbols, we may wind up trying
941 to merge a symbol with itself. Catch that here, to avoid the
942 confusion that results if we try to override a symbol with
943 itself. The additional tests catch cases like
944 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
945 dynamic object, which we do want to handle here. */
946 if (abfd == oldbfd
947 && ((abfd->flags & DYNAMIC) == 0
948 || !h->def_regular))
949 return TRUE;
951 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
952 respectively, is from a dynamic object. */
954 newdyn = (abfd->flags & DYNAMIC) != 0;
956 olddyn = FALSE;
957 if (oldbfd != NULL)
958 olddyn = (oldbfd->flags & DYNAMIC) != 0;
959 else if (oldsec != NULL)
961 /* This handles the special SHN_MIPS_{TEXT,DATA} section
962 indices used by MIPS ELF. */
963 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
966 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
967 respectively, appear to be a definition rather than reference. */
969 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
971 olddef = (h->root.type != bfd_link_hash_undefined
972 && h->root.type != bfd_link_hash_undefweak
973 && h->root.type != bfd_link_hash_common);
975 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
976 respectively, appear to be a function. */
978 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
979 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
981 oldfunc = (h->type != STT_NOTYPE
982 && bed->is_function_type (h->type));
984 /* When we try to create a default indirect symbol from the dynamic
985 definition with the default version, we skip it if its type and
986 the type of existing regular definition mismatch. We only do it
987 if the existing regular definition won't be dynamic. */
988 if (pold_alignment == NULL
989 && !info->shared
990 && !info->export_dynamic
991 && !h->ref_dynamic
992 && newdyn
993 && newdef
994 && !olddyn
995 && (olddef || h->root.type == bfd_link_hash_common)
996 && ELF_ST_TYPE (sym->st_info) != h->type
997 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
998 && h->type != STT_NOTYPE
999 && !(newfunc && oldfunc))
1001 *skip = TRUE;
1002 return TRUE;
1005 /* Check TLS symbol. We don't check undefined symbol introduced by
1006 "ld -u". */
1007 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
1008 && ELF_ST_TYPE (sym->st_info) != h->type
1009 && oldbfd != NULL)
1011 bfd *ntbfd, *tbfd;
1012 bfd_boolean ntdef, tdef;
1013 asection *ntsec, *tsec;
1015 if (h->type == STT_TLS)
1017 ntbfd = abfd;
1018 ntsec = sec;
1019 ntdef = newdef;
1020 tbfd = oldbfd;
1021 tsec = oldsec;
1022 tdef = olddef;
1024 else
1026 ntbfd = oldbfd;
1027 ntsec = oldsec;
1028 ntdef = olddef;
1029 tbfd = abfd;
1030 tsec = sec;
1031 tdef = newdef;
1034 if (tdef && ntdef)
1035 (*_bfd_error_handler)
1036 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1037 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1038 else if (!tdef && !ntdef)
1039 (*_bfd_error_handler)
1040 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1041 tbfd, ntbfd, h->root.root.string);
1042 else if (tdef)
1043 (*_bfd_error_handler)
1044 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1045 tbfd, tsec, ntbfd, h->root.root.string);
1046 else
1047 (*_bfd_error_handler)
1048 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1049 tbfd, ntbfd, ntsec, h->root.root.string);
1051 bfd_set_error (bfd_error_bad_value);
1052 return FALSE;
1055 /* We need to remember if a symbol has a definition in a dynamic
1056 object or is weak in all dynamic objects. Internal and hidden
1057 visibility will make it unavailable to dynamic objects. */
1058 if (newdyn && !h->dynamic_def)
1060 if (!bfd_is_und_section (sec))
1061 h->dynamic_def = 1;
1062 else
1064 /* Check if this symbol is weak in all dynamic objects. If it
1065 is the first time we see it in a dynamic object, we mark
1066 if it is weak. Otherwise, we clear it. */
1067 if (!h->ref_dynamic)
1069 if (bind == STB_WEAK)
1070 h->dynamic_weak = 1;
1072 else if (bind != STB_WEAK)
1073 h->dynamic_weak = 0;
1077 /* If the old symbol has non-default visibility, we ignore the new
1078 definition from a dynamic object. */
1079 if (newdyn
1080 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1081 && !bfd_is_und_section (sec))
1083 *skip = TRUE;
1084 /* Make sure this symbol is dynamic. */
1085 h->ref_dynamic = 1;
1086 /* A protected symbol has external availability. Make sure it is
1087 recorded as dynamic.
1089 FIXME: Should we check type and size for protected symbol? */
1090 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1091 return bfd_elf_link_record_dynamic_symbol (info, h);
1092 else
1093 return TRUE;
1095 else if (!newdyn
1096 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1097 && h->def_dynamic)
1099 /* If the new symbol with non-default visibility comes from a
1100 relocatable file and the old definition comes from a dynamic
1101 object, we remove the old definition. */
1102 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1104 /* Handle the case where the old dynamic definition is
1105 default versioned. We need to copy the symbol info from
1106 the symbol with default version to the normal one if it
1107 was referenced before. */
1108 if (h->ref_regular)
1110 const struct elf_backend_data *bed
1111 = get_elf_backend_data (abfd);
1112 struct elf_link_hash_entry *vh = *sym_hash;
1113 vh->root.type = h->root.type;
1114 h->root.type = bfd_link_hash_indirect;
1115 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1116 /* Protected symbols will override the dynamic definition
1117 with default version. */
1118 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1120 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1121 vh->dynamic_def = 1;
1122 vh->ref_dynamic = 1;
1124 else
1126 h->root.type = vh->root.type;
1127 vh->ref_dynamic = 0;
1128 /* We have to hide it here since it was made dynamic
1129 global with extra bits when the symbol info was
1130 copied from the old dynamic definition. */
1131 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1133 h = vh;
1135 else
1136 h = *sym_hash;
1139 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1140 && bfd_is_und_section (sec))
1142 /* If the new symbol is undefined and the old symbol was
1143 also undefined before, we need to make sure
1144 _bfd_generic_link_add_one_symbol doesn't mess
1145 up the linker hash table undefs list. Since the old
1146 definition came from a dynamic object, it is still on the
1147 undefs list. */
1148 h->root.type = bfd_link_hash_undefined;
1149 h->root.u.undef.abfd = abfd;
1151 else
1153 h->root.type = bfd_link_hash_new;
1154 h->root.u.undef.abfd = NULL;
1157 if (h->def_dynamic)
1159 h->def_dynamic = 0;
1160 h->ref_dynamic = 1;
1161 h->dynamic_def = 1;
1163 /* FIXME: Should we check type and size for protected symbol? */
1164 h->size = 0;
1165 h->type = 0;
1166 return TRUE;
1169 /* Differentiate strong and weak symbols. */
1170 newweak = bind == STB_WEAK;
1171 oldweak = (h->root.type == bfd_link_hash_defweak
1172 || h->root.type == bfd_link_hash_undefweak);
1174 /* If a new weak symbol definition comes from a regular file and the
1175 old symbol comes from a dynamic library, we treat the new one as
1176 strong. Similarly, an old weak symbol definition from a regular
1177 file is treated as strong when the new symbol comes from a dynamic
1178 library. Further, an old weak symbol from a dynamic library is
1179 treated as strong if the new symbol is from a dynamic library.
1180 This reflects the way glibc's ld.so works.
1182 Do this before setting *type_change_ok or *size_change_ok so that
1183 we warn properly when dynamic library symbols are overridden. */
1185 if (newdef && !newdyn && olddyn)
1186 newweak = FALSE;
1187 if (olddef && newdyn)
1188 oldweak = FALSE;
1190 /* Allow changes between different types of funciton symbol. */
1191 if (newfunc && oldfunc)
1192 *type_change_ok = TRUE;
1194 /* It's OK to change the type if either the existing symbol or the
1195 new symbol is weak. A type change is also OK if the old symbol
1196 is undefined and the new symbol is defined. */
1198 if (oldweak
1199 || newweak
1200 || (newdef
1201 && h->root.type == bfd_link_hash_undefined))
1202 *type_change_ok = TRUE;
1204 /* It's OK to change the size if either the existing symbol or the
1205 new symbol is weak, or if the old symbol is undefined. */
1207 if (*type_change_ok
1208 || h->root.type == bfd_link_hash_undefined)
1209 *size_change_ok = TRUE;
1211 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1212 symbol, respectively, appears to be a common symbol in a dynamic
1213 object. If a symbol appears in an uninitialized section, and is
1214 not weak, and is not a function, then it may be a common symbol
1215 which was resolved when the dynamic object was created. We want
1216 to treat such symbols specially, because they raise special
1217 considerations when setting the symbol size: if the symbol
1218 appears as a common symbol in a regular object, and the size in
1219 the regular object is larger, we must make sure that we use the
1220 larger size. This problematic case can always be avoided in C,
1221 but it must be handled correctly when using Fortran shared
1222 libraries.
1224 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1225 likewise for OLDDYNCOMMON and OLDDEF.
1227 Note that this test is just a heuristic, and that it is quite
1228 possible to have an uninitialized symbol in a shared object which
1229 is really a definition, rather than a common symbol. This could
1230 lead to some minor confusion when the symbol really is a common
1231 symbol in some regular object. However, I think it will be
1232 harmless. */
1234 if (newdyn
1235 && newdef
1236 && !newweak
1237 && (sec->flags & SEC_ALLOC) != 0
1238 && (sec->flags & SEC_LOAD) == 0
1239 && sym->st_size > 0
1240 && !newfunc)
1241 newdyncommon = TRUE;
1242 else
1243 newdyncommon = FALSE;
1245 if (olddyn
1246 && olddef
1247 && h->root.type == bfd_link_hash_defined
1248 && h->def_dynamic
1249 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1250 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1251 && h->size > 0
1252 && !oldfunc)
1253 olddyncommon = TRUE;
1254 else
1255 olddyncommon = FALSE;
1257 /* We now know everything about the old and new symbols. We ask the
1258 backend to check if we can merge them. */
1259 if (bed->merge_symbol
1260 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1261 pold_alignment, skip, override,
1262 type_change_ok, size_change_ok,
1263 &newdyn, &newdef, &newdyncommon, &newweak,
1264 abfd, &sec,
1265 &olddyn, &olddef, &olddyncommon, &oldweak,
1266 oldbfd, &oldsec))
1267 return FALSE;
1269 /* If both the old and the new symbols look like common symbols in a
1270 dynamic object, set the size of the symbol to the larger of the
1271 two. */
1273 if (olddyncommon
1274 && newdyncommon
1275 && sym->st_size != h->size)
1277 /* Since we think we have two common symbols, issue a multiple
1278 common warning if desired. Note that we only warn if the
1279 size is different. If the size is the same, we simply let
1280 the old symbol override the new one as normally happens with
1281 symbols defined in dynamic objects. */
1283 if (! ((*info->callbacks->multiple_common)
1284 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1285 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1286 return FALSE;
1288 if (sym->st_size > h->size)
1289 h->size = sym->st_size;
1291 *size_change_ok = TRUE;
1294 /* If we are looking at a dynamic object, and we have found a
1295 definition, we need to see if the symbol was already defined by
1296 some other object. If so, we want to use the existing
1297 definition, and we do not want to report a multiple symbol
1298 definition error; we do this by clobbering *PSEC to be
1299 bfd_und_section_ptr.
1301 We treat a common symbol as a definition if the symbol in the
1302 shared library is a function, since common symbols always
1303 represent variables; this can cause confusion in principle, but
1304 any such confusion would seem to indicate an erroneous program or
1305 shared library. We also permit a common symbol in a regular
1306 object to override a weak symbol in a shared object. */
1308 if (newdyn
1309 && newdef
1310 && (olddef
1311 || (h->root.type == bfd_link_hash_common
1312 && (newweak || newfunc))))
1314 *override = TRUE;
1315 newdef = FALSE;
1316 newdyncommon = FALSE;
1318 *psec = sec = bfd_und_section_ptr;
1319 *size_change_ok = TRUE;
1321 /* If we get here when the old symbol is a common symbol, then
1322 we are explicitly letting it override a weak symbol or
1323 function in a dynamic object, and we don't want to warn about
1324 a type change. If the old symbol is a defined symbol, a type
1325 change warning may still be appropriate. */
1327 if (h->root.type == bfd_link_hash_common)
1328 *type_change_ok = TRUE;
1331 /* Handle the special case of an old common symbol merging with a
1332 new symbol which looks like a common symbol in a shared object.
1333 We change *PSEC and *PVALUE to make the new symbol look like a
1334 common symbol, and let _bfd_generic_link_add_one_symbol do the
1335 right thing. */
1337 if (newdyncommon
1338 && h->root.type == bfd_link_hash_common)
1340 *override = TRUE;
1341 newdef = FALSE;
1342 newdyncommon = FALSE;
1343 *pvalue = sym->st_size;
1344 *psec = sec = bed->common_section (oldsec);
1345 *size_change_ok = TRUE;
1348 /* Skip weak definitions of symbols that are already defined. */
1349 if (newdef && olddef && newweak)
1350 *skip = TRUE;
1352 /* If the old symbol is from a dynamic object, and the new symbol is
1353 a definition which is not from a dynamic object, then the new
1354 symbol overrides the old symbol. Symbols from regular files
1355 always take precedence over symbols from dynamic objects, even if
1356 they are defined after the dynamic object in the link.
1358 As above, we again permit a common symbol in a regular object to
1359 override a definition in a shared object if the shared object
1360 symbol is a function or is weak. */
1362 flip = NULL;
1363 if (!newdyn
1364 && (newdef
1365 || (bfd_is_com_section (sec)
1366 && (oldweak || oldfunc)))
1367 && olddyn
1368 && olddef
1369 && h->def_dynamic)
1371 /* Change the hash table entry to undefined, and let
1372 _bfd_generic_link_add_one_symbol do the right thing with the
1373 new definition. */
1375 h->root.type = bfd_link_hash_undefined;
1376 h->root.u.undef.abfd = h->root.u.def.section->owner;
1377 *size_change_ok = TRUE;
1379 olddef = FALSE;
1380 olddyncommon = FALSE;
1382 /* We again permit a type change when a common symbol may be
1383 overriding a function. */
1385 if (bfd_is_com_section (sec))
1387 if (oldfunc)
1389 /* If a common symbol overrides a function, make sure
1390 that it isn't defined dynamically nor has type
1391 function. */
1392 h->def_dynamic = 0;
1393 h->type = STT_NOTYPE;
1395 *type_change_ok = TRUE;
1398 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1399 flip = *sym_hash;
1400 else
1401 /* This union may have been set to be non-NULL when this symbol
1402 was seen in a dynamic object. We must force the union to be
1403 NULL, so that it is correct for a regular symbol. */
1404 h->verinfo.vertree = NULL;
1407 /* Handle the special case of a new common symbol merging with an
1408 old symbol that looks like it might be a common symbol defined in
1409 a shared object. Note that we have already handled the case in
1410 which a new common symbol should simply override the definition
1411 in the shared library. */
1413 if (! newdyn
1414 && bfd_is_com_section (sec)
1415 && olddyncommon)
1417 /* It would be best if we could set the hash table entry to a
1418 common symbol, but we don't know what to use for the section
1419 or the alignment. */
1420 if (! ((*info->callbacks->multiple_common)
1421 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1422 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1423 return FALSE;
1425 /* If the presumed common symbol in the dynamic object is
1426 larger, pretend that the new symbol has its size. */
1428 if (h->size > *pvalue)
1429 *pvalue = h->size;
1431 /* We need to remember the alignment required by the symbol
1432 in the dynamic object. */
1433 BFD_ASSERT (pold_alignment);
1434 *pold_alignment = h->root.u.def.section->alignment_power;
1436 olddef = FALSE;
1437 olddyncommon = FALSE;
1439 h->root.type = bfd_link_hash_undefined;
1440 h->root.u.undef.abfd = h->root.u.def.section->owner;
1442 *size_change_ok = TRUE;
1443 *type_change_ok = TRUE;
1445 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1446 flip = *sym_hash;
1447 else
1448 h->verinfo.vertree = NULL;
1451 if (flip != NULL)
1453 /* Handle the case where we had a versioned symbol in a dynamic
1454 library and now find a definition in a normal object. In this
1455 case, we make the versioned symbol point to the normal one. */
1456 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1457 flip->root.type = h->root.type;
1458 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1459 h->root.type = bfd_link_hash_indirect;
1460 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1461 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1462 if (h->def_dynamic)
1464 h->def_dynamic = 0;
1465 flip->ref_dynamic = 1;
1469 return TRUE;
1472 /* This function is called to create an indirect symbol from the
1473 default for the symbol with the default version if needed. The
1474 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1475 set DYNSYM if the new indirect symbol is dynamic. */
1477 bfd_boolean
1478 _bfd_elf_add_default_symbol (bfd *abfd,
1479 struct bfd_link_info *info,
1480 struct elf_link_hash_entry *h,
1481 const char *name,
1482 Elf_Internal_Sym *sym,
1483 asection **psec,
1484 bfd_vma *value,
1485 bfd_boolean *dynsym,
1486 bfd_boolean override)
1488 bfd_boolean type_change_ok;
1489 bfd_boolean size_change_ok;
1490 bfd_boolean skip;
1491 char *shortname;
1492 struct elf_link_hash_entry *hi;
1493 struct bfd_link_hash_entry *bh;
1494 const struct elf_backend_data *bed;
1495 bfd_boolean collect;
1496 bfd_boolean dynamic;
1497 char *p;
1498 size_t len, shortlen;
1499 asection *sec;
1501 /* If this symbol has a version, and it is the default version, we
1502 create an indirect symbol from the default name to the fully
1503 decorated name. This will cause external references which do not
1504 specify a version to be bound to this version of the symbol. */
1505 p = strchr (name, ELF_VER_CHR);
1506 if (p == NULL || p[1] != ELF_VER_CHR)
1507 return TRUE;
1509 if (override)
1511 /* We are overridden by an old definition. We need to check if we
1512 need to create the indirect symbol from the default name. */
1513 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1514 FALSE, FALSE);
1515 BFD_ASSERT (hi != NULL);
1516 if (hi == h)
1517 return TRUE;
1518 while (hi->root.type == bfd_link_hash_indirect
1519 || hi->root.type == bfd_link_hash_warning)
1521 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1522 if (hi == h)
1523 return TRUE;
1527 bed = get_elf_backend_data (abfd);
1528 collect = bed->collect;
1529 dynamic = (abfd->flags & DYNAMIC) != 0;
1531 shortlen = p - name;
1532 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1533 if (shortname == NULL)
1534 return FALSE;
1535 memcpy (shortname, name, shortlen);
1536 shortname[shortlen] = '\0';
1538 /* We are going to create a new symbol. Merge it with any existing
1539 symbol with this name. For the purposes of the merge, act as
1540 though we were defining the symbol we just defined, although we
1541 actually going to define an indirect symbol. */
1542 type_change_ok = FALSE;
1543 size_change_ok = FALSE;
1544 sec = *psec;
1545 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1546 NULL, &hi, &skip, &override,
1547 &type_change_ok, &size_change_ok))
1548 return FALSE;
1550 if (skip)
1551 goto nondefault;
1553 if (! override)
1555 bh = &hi->root;
1556 if (! (_bfd_generic_link_add_one_symbol
1557 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1558 0, name, FALSE, collect, &bh)))
1559 return FALSE;
1560 hi = (struct elf_link_hash_entry *) bh;
1562 else
1564 /* In this case the symbol named SHORTNAME is overriding the
1565 indirect symbol we want to add. We were planning on making
1566 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1567 is the name without a version. NAME is the fully versioned
1568 name, and it is the default version.
1570 Overriding means that we already saw a definition for the
1571 symbol SHORTNAME in a regular object, and it is overriding
1572 the symbol defined in the dynamic object.
1574 When this happens, we actually want to change NAME, the
1575 symbol we just added, to refer to SHORTNAME. This will cause
1576 references to NAME in the shared object to become references
1577 to SHORTNAME in the regular object. This is what we expect
1578 when we override a function in a shared object: that the
1579 references in the shared object will be mapped to the
1580 definition in the regular object. */
1582 while (hi->root.type == bfd_link_hash_indirect
1583 || hi->root.type == bfd_link_hash_warning)
1584 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1586 h->root.type = bfd_link_hash_indirect;
1587 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1588 if (h->def_dynamic)
1590 h->def_dynamic = 0;
1591 hi->ref_dynamic = 1;
1592 if (hi->ref_regular
1593 || hi->def_regular)
1595 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1596 return FALSE;
1600 /* Now set HI to H, so that the following code will set the
1601 other fields correctly. */
1602 hi = h;
1605 /* Check if HI is a warning symbol. */
1606 if (hi->root.type == bfd_link_hash_warning)
1607 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1609 /* If there is a duplicate definition somewhere, then HI may not
1610 point to an indirect symbol. We will have reported an error to
1611 the user in that case. */
1613 if (hi->root.type == bfd_link_hash_indirect)
1615 struct elf_link_hash_entry *ht;
1617 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1618 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1620 /* See if the new flags lead us to realize that the symbol must
1621 be dynamic. */
1622 if (! *dynsym)
1624 if (! dynamic)
1626 if (info->shared
1627 || hi->ref_dynamic)
1628 *dynsym = TRUE;
1630 else
1632 if (hi->ref_regular)
1633 *dynsym = TRUE;
1638 /* We also need to define an indirection from the nondefault version
1639 of the symbol. */
1641 nondefault:
1642 len = strlen (name);
1643 shortname = bfd_hash_allocate (&info->hash->table, len);
1644 if (shortname == NULL)
1645 return FALSE;
1646 memcpy (shortname, name, shortlen);
1647 memcpy (shortname + shortlen, p + 1, len - shortlen);
1649 /* Once again, merge with any existing symbol. */
1650 type_change_ok = FALSE;
1651 size_change_ok = FALSE;
1652 sec = *psec;
1653 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1654 NULL, &hi, &skip, &override,
1655 &type_change_ok, &size_change_ok))
1656 return FALSE;
1658 if (skip)
1659 return TRUE;
1661 if (override)
1663 /* Here SHORTNAME is a versioned name, so we don't expect to see
1664 the type of override we do in the case above unless it is
1665 overridden by a versioned definition. */
1666 if (hi->root.type != bfd_link_hash_defined
1667 && hi->root.type != bfd_link_hash_defweak)
1668 (*_bfd_error_handler)
1669 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1670 abfd, shortname);
1672 else
1674 bh = &hi->root;
1675 if (! (_bfd_generic_link_add_one_symbol
1676 (info, abfd, shortname, BSF_INDIRECT,
1677 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1678 return FALSE;
1679 hi = (struct elf_link_hash_entry *) bh;
1681 /* If there is a duplicate definition somewhere, then HI may not
1682 point to an indirect symbol. We will have reported an error
1683 to the user in that case. */
1685 if (hi->root.type == bfd_link_hash_indirect)
1687 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1689 /* See if the new flags lead us to realize that the symbol
1690 must be dynamic. */
1691 if (! *dynsym)
1693 if (! dynamic)
1695 if (info->shared
1696 || hi->ref_dynamic)
1697 *dynsym = TRUE;
1699 else
1701 if (hi->ref_regular)
1702 *dynsym = TRUE;
1708 return TRUE;
1711 /* This routine is used to export all defined symbols into the dynamic
1712 symbol table. It is called via elf_link_hash_traverse. */
1714 bfd_boolean
1715 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1717 struct elf_info_failed *eif = data;
1719 /* Ignore this if we won't export it. */
1720 if (!eif->info->export_dynamic && !h->dynamic)
1721 return TRUE;
1723 /* Ignore indirect symbols. These are added by the versioning code. */
1724 if (h->root.type == bfd_link_hash_indirect)
1725 return TRUE;
1727 if (h->root.type == bfd_link_hash_warning)
1728 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1730 if (h->dynindx == -1
1731 && (h->def_regular
1732 || h->ref_regular))
1734 struct bfd_elf_version_tree *t;
1735 struct bfd_elf_version_expr *d;
1737 for (t = eif->verdefs; t != NULL; t = t->next)
1739 if (t->globals.list != NULL)
1741 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1742 if (d != NULL)
1743 goto doit;
1746 if (t->locals.list != NULL)
1748 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1749 if (d != NULL)
1750 return TRUE;
1754 if (!eif->verdefs)
1756 doit:
1757 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1759 eif->failed = TRUE;
1760 return FALSE;
1765 return TRUE;
1768 /* Look through the symbols which are defined in other shared
1769 libraries and referenced here. Update the list of version
1770 dependencies. This will be put into the .gnu.version_r section.
1771 This function is called via elf_link_hash_traverse. */
1773 bfd_boolean
1774 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1775 void *data)
1777 struct elf_find_verdep_info *rinfo = data;
1778 Elf_Internal_Verneed *t;
1779 Elf_Internal_Vernaux *a;
1780 bfd_size_type amt;
1782 if (h->root.type == bfd_link_hash_warning)
1783 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1785 /* We only care about symbols defined in shared objects with version
1786 information. */
1787 if (!h->def_dynamic
1788 || h->def_regular
1789 || h->dynindx == -1
1790 || h->verinfo.verdef == NULL)
1791 return TRUE;
1793 /* See if we already know about this version. */
1794 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1796 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1797 continue;
1799 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1800 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1801 return TRUE;
1803 break;
1806 /* This is a new version. Add it to tree we are building. */
1808 if (t == NULL)
1810 amt = sizeof *t;
1811 t = bfd_zalloc (rinfo->output_bfd, amt);
1812 if (t == NULL)
1814 rinfo->failed = TRUE;
1815 return FALSE;
1818 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1819 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1820 elf_tdata (rinfo->output_bfd)->verref = t;
1823 amt = sizeof *a;
1824 a = bfd_zalloc (rinfo->output_bfd, amt);
1825 if (a == NULL)
1827 rinfo->failed = TRUE;
1828 return FALSE;
1831 /* Note that we are copying a string pointer here, and testing it
1832 above. If bfd_elf_string_from_elf_section is ever changed to
1833 discard the string data when low in memory, this will have to be
1834 fixed. */
1835 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1837 a->vna_flags = h->verinfo.verdef->vd_flags;
1838 a->vna_nextptr = t->vn_auxptr;
1840 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1841 ++rinfo->vers;
1843 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1845 t->vn_auxptr = a;
1847 return TRUE;
1850 /* Figure out appropriate versions for all the symbols. We may not
1851 have the version number script until we have read all of the input
1852 files, so until that point we don't know which symbols should be
1853 local. This function is called via elf_link_hash_traverse. */
1855 bfd_boolean
1856 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1858 struct elf_assign_sym_version_info *sinfo;
1859 struct bfd_link_info *info;
1860 const struct elf_backend_data *bed;
1861 struct elf_info_failed eif;
1862 char *p;
1863 bfd_size_type amt;
1865 sinfo = data;
1866 info = sinfo->info;
1868 if (h->root.type == bfd_link_hash_warning)
1869 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1871 /* Fix the symbol flags. */
1872 eif.failed = FALSE;
1873 eif.info = info;
1874 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1876 if (eif.failed)
1877 sinfo->failed = TRUE;
1878 return FALSE;
1881 /* We only need version numbers for symbols defined in regular
1882 objects. */
1883 if (!h->def_regular)
1884 return TRUE;
1886 bed = get_elf_backend_data (sinfo->output_bfd);
1887 p = strchr (h->root.root.string, ELF_VER_CHR);
1888 if (p != NULL && h->verinfo.vertree == NULL)
1890 struct bfd_elf_version_tree *t;
1891 bfd_boolean hidden;
1893 hidden = TRUE;
1895 /* There are two consecutive ELF_VER_CHR characters if this is
1896 not a hidden symbol. */
1897 ++p;
1898 if (*p == ELF_VER_CHR)
1900 hidden = FALSE;
1901 ++p;
1904 /* If there is no version string, we can just return out. */
1905 if (*p == '\0')
1907 if (hidden)
1908 h->hidden = 1;
1909 return TRUE;
1912 /* Look for the version. If we find it, it is no longer weak. */
1913 for (t = sinfo->verdefs; t != NULL; t = t->next)
1915 if (strcmp (t->name, p) == 0)
1917 size_t len;
1918 char *alc;
1919 struct bfd_elf_version_expr *d;
1921 len = p - h->root.root.string;
1922 alc = bfd_malloc (len);
1923 if (alc == NULL)
1925 sinfo->failed = TRUE;
1926 return FALSE;
1928 memcpy (alc, h->root.root.string, len - 1);
1929 alc[len - 1] = '\0';
1930 if (alc[len - 2] == ELF_VER_CHR)
1931 alc[len - 2] = '\0';
1933 h->verinfo.vertree = t;
1934 t->used = TRUE;
1935 d = NULL;
1937 if (t->globals.list != NULL)
1938 d = (*t->match) (&t->globals, NULL, alc);
1940 /* See if there is anything to force this symbol to
1941 local scope. */
1942 if (d == NULL && t->locals.list != NULL)
1944 d = (*t->match) (&t->locals, NULL, alc);
1945 if (d != NULL
1946 && h->dynindx != -1
1947 && ! info->export_dynamic)
1948 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1951 free (alc);
1952 break;
1956 /* If we are building an application, we need to create a
1957 version node for this version. */
1958 if (t == NULL && info->executable)
1960 struct bfd_elf_version_tree **pp;
1961 int version_index;
1963 /* If we aren't going to export this symbol, we don't need
1964 to worry about it. */
1965 if (h->dynindx == -1)
1966 return TRUE;
1968 amt = sizeof *t;
1969 t = bfd_zalloc (sinfo->output_bfd, amt);
1970 if (t == NULL)
1972 sinfo->failed = TRUE;
1973 return FALSE;
1976 t->name = p;
1977 t->name_indx = (unsigned int) -1;
1978 t->used = TRUE;
1980 version_index = 1;
1981 /* Don't count anonymous version tag. */
1982 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1983 version_index = 0;
1984 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1985 ++version_index;
1986 t->vernum = version_index;
1988 *pp = t;
1990 h->verinfo.vertree = t;
1992 else if (t == NULL)
1994 /* We could not find the version for a symbol when
1995 generating a shared archive. Return an error. */
1996 (*_bfd_error_handler)
1997 (_("%B: version node not found for symbol %s"),
1998 sinfo->output_bfd, h->root.root.string);
1999 bfd_set_error (bfd_error_bad_value);
2000 sinfo->failed = TRUE;
2001 return FALSE;
2004 if (hidden)
2005 h->hidden = 1;
2008 /* If we don't have a version for this symbol, see if we can find
2009 something. */
2010 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
2012 struct bfd_elf_version_tree *t;
2013 struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
2014 struct bfd_elf_version_expr *d;
2016 /* See if can find what version this symbol is in. If the
2017 symbol is supposed to be local, then don't actually register
2018 it. */
2019 local_ver = NULL;
2020 global_ver = NULL;
2021 exist_ver = NULL;
2022 for (t = sinfo->verdefs; t != NULL; t = t->next)
2024 if (t->globals.list != NULL)
2026 d = NULL;
2027 while ((d = (*t->match) (&t->globals, d,
2028 h->root.root.string)) != NULL)
2030 global_ver = t;
2031 local_ver = NULL;
2032 if (d->symver)
2033 exist_ver = t;
2034 d->script = 1;
2035 /* If the match is a wildcard pattern, keep looking for
2036 a more explicit, perhaps even local, match. */
2037 if (d->literal)
2038 break;
2041 if (d != NULL)
2042 break;
2045 if (t->locals.list != NULL)
2047 d = NULL;
2048 while ((d = (*t->match) (&t->locals, d,
2049 h->root.root.string)) != NULL)
2051 local_ver = t;
2052 /* If the match is a wildcard pattern, keep looking for
2053 a more explicit, perhaps even global, match. */
2054 if (d->literal)
2056 /* An exact match overrides a global wildcard. */
2057 global_ver = NULL;
2058 break;
2062 if (d != NULL)
2063 break;
2067 if (global_ver != NULL)
2069 h->verinfo.vertree = global_ver;
2070 /* If we already have a versioned symbol that matches the
2071 node for this symbol, then we don't want to create a
2072 duplicate from the unversioned symbol. Instead hide the
2073 unversioned symbol. */
2074 if (exist_ver == global_ver)
2075 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2077 else if (local_ver != NULL)
2079 h->verinfo.vertree = local_ver;
2080 if (!info->export_dynamic
2081 || exist_ver == local_ver)
2082 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2086 return TRUE;
2089 /* Read and swap the relocs from the section indicated by SHDR. This
2090 may be either a REL or a RELA section. The relocations are
2091 translated into RELA relocations and stored in INTERNAL_RELOCS,
2092 which should have already been allocated to contain enough space.
2093 The EXTERNAL_RELOCS are a buffer where the external form of the
2094 relocations should be stored.
2096 Returns FALSE if something goes wrong. */
2098 static bfd_boolean
2099 elf_link_read_relocs_from_section (bfd *abfd,
2100 asection *sec,
2101 Elf_Internal_Shdr *shdr,
2102 void *external_relocs,
2103 Elf_Internal_Rela *internal_relocs)
2105 const struct elf_backend_data *bed;
2106 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2107 const bfd_byte *erela;
2108 const bfd_byte *erelaend;
2109 Elf_Internal_Rela *irela;
2110 Elf_Internal_Shdr *symtab_hdr;
2111 size_t nsyms;
2113 /* Position ourselves at the start of the section. */
2114 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2115 return FALSE;
2117 /* Read the relocations. */
2118 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2119 return FALSE;
2121 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2122 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2124 bed = get_elf_backend_data (abfd);
2126 /* Convert the external relocations to the internal format. */
2127 if (shdr->sh_entsize == bed->s->sizeof_rel)
2128 swap_in = bed->s->swap_reloc_in;
2129 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2130 swap_in = bed->s->swap_reloca_in;
2131 else
2133 bfd_set_error (bfd_error_wrong_format);
2134 return FALSE;
2137 erela = external_relocs;
2138 erelaend = erela + shdr->sh_size;
2139 irela = internal_relocs;
2140 while (erela < erelaend)
2142 bfd_vma r_symndx;
2144 (*swap_in) (abfd, erela, irela);
2145 r_symndx = ELF32_R_SYM (irela->r_info);
2146 if (bed->s->arch_size == 64)
2147 r_symndx >>= 24;
2148 if ((size_t) r_symndx >= nsyms)
2150 (*_bfd_error_handler)
2151 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2152 " for offset 0x%lx in section `%A'"),
2153 abfd, sec,
2154 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2155 bfd_set_error (bfd_error_bad_value);
2156 return FALSE;
2158 irela += bed->s->int_rels_per_ext_rel;
2159 erela += shdr->sh_entsize;
2162 return TRUE;
2165 /* Read and swap the relocs for a section O. They may have been
2166 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2167 not NULL, they are used as buffers to read into. They are known to
2168 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2169 the return value is allocated using either malloc or bfd_alloc,
2170 according to the KEEP_MEMORY argument. If O has two relocation
2171 sections (both REL and RELA relocations), then the REL_HDR
2172 relocations will appear first in INTERNAL_RELOCS, followed by the
2173 REL_HDR2 relocations. */
2175 Elf_Internal_Rela *
2176 _bfd_elf_link_read_relocs (bfd *abfd,
2177 asection *o,
2178 void *external_relocs,
2179 Elf_Internal_Rela *internal_relocs,
2180 bfd_boolean keep_memory)
2182 Elf_Internal_Shdr *rel_hdr;
2183 void *alloc1 = NULL;
2184 Elf_Internal_Rela *alloc2 = NULL;
2185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2187 if (elf_section_data (o)->relocs != NULL)
2188 return elf_section_data (o)->relocs;
2190 if (o->reloc_count == 0)
2191 return NULL;
2193 rel_hdr = &elf_section_data (o)->rel_hdr;
2195 if (internal_relocs == NULL)
2197 bfd_size_type size;
2199 size = o->reloc_count;
2200 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2201 if (keep_memory)
2202 internal_relocs = alloc2 = bfd_alloc (abfd, size);
2203 else
2204 internal_relocs = alloc2 = bfd_malloc (size);
2205 if (internal_relocs == NULL)
2206 goto error_return;
2209 if (external_relocs == NULL)
2211 bfd_size_type size = rel_hdr->sh_size;
2213 if (elf_section_data (o)->rel_hdr2)
2214 size += elf_section_data (o)->rel_hdr2->sh_size;
2215 alloc1 = bfd_malloc (size);
2216 if (alloc1 == NULL)
2217 goto error_return;
2218 external_relocs = alloc1;
2221 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2222 external_relocs,
2223 internal_relocs))
2224 goto error_return;
2225 if (elf_section_data (o)->rel_hdr2
2226 && (!elf_link_read_relocs_from_section
2227 (abfd, o,
2228 elf_section_data (o)->rel_hdr2,
2229 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2230 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2231 * bed->s->int_rels_per_ext_rel))))
2232 goto error_return;
2234 /* Cache the results for next time, if we can. */
2235 if (keep_memory)
2236 elf_section_data (o)->relocs = internal_relocs;
2238 if (alloc1 != NULL)
2239 free (alloc1);
2241 /* Don't free alloc2, since if it was allocated we are passing it
2242 back (under the name of internal_relocs). */
2244 return internal_relocs;
2246 error_return:
2247 if (alloc1 != NULL)
2248 free (alloc1);
2249 if (alloc2 != NULL)
2251 if (keep_memory)
2252 bfd_release (abfd, alloc2);
2253 else
2254 free (alloc2);
2256 return NULL;
2259 /* Compute the size of, and allocate space for, REL_HDR which is the
2260 section header for a section containing relocations for O. */
2262 bfd_boolean
2263 _bfd_elf_link_size_reloc_section (bfd *abfd,
2264 Elf_Internal_Shdr *rel_hdr,
2265 asection *o)
2267 bfd_size_type reloc_count;
2268 bfd_size_type num_rel_hashes;
2270 /* Figure out how many relocations there will be. */
2271 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2272 reloc_count = elf_section_data (o)->rel_count;
2273 else
2274 reloc_count = elf_section_data (o)->rel_count2;
2276 num_rel_hashes = o->reloc_count;
2277 if (num_rel_hashes < reloc_count)
2278 num_rel_hashes = reloc_count;
2280 /* That allows us to calculate the size of the section. */
2281 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2283 /* The contents field must last into write_object_contents, so we
2284 allocate it with bfd_alloc rather than malloc. Also since we
2285 cannot be sure that the contents will actually be filled in,
2286 we zero the allocated space. */
2287 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2288 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2289 return FALSE;
2291 /* We only allocate one set of hash entries, so we only do it the
2292 first time we are called. */
2293 if (elf_section_data (o)->rel_hashes == NULL
2294 && num_rel_hashes)
2296 struct elf_link_hash_entry **p;
2298 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2299 if (p == NULL)
2300 return FALSE;
2302 elf_section_data (o)->rel_hashes = p;
2305 return TRUE;
2308 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2309 originated from the section given by INPUT_REL_HDR) to the
2310 OUTPUT_BFD. */
2312 bfd_boolean
2313 _bfd_elf_link_output_relocs (bfd *output_bfd,
2314 asection *input_section,
2315 Elf_Internal_Shdr *input_rel_hdr,
2316 Elf_Internal_Rela *internal_relocs,
2317 struct elf_link_hash_entry **rel_hash
2318 ATTRIBUTE_UNUSED)
2320 Elf_Internal_Rela *irela;
2321 Elf_Internal_Rela *irelaend;
2322 bfd_byte *erel;
2323 Elf_Internal_Shdr *output_rel_hdr;
2324 asection *output_section;
2325 unsigned int *rel_countp = NULL;
2326 const struct elf_backend_data *bed;
2327 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2329 output_section = input_section->output_section;
2330 output_rel_hdr = NULL;
2332 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2333 == input_rel_hdr->sh_entsize)
2335 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2336 rel_countp = &elf_section_data (output_section)->rel_count;
2338 else if (elf_section_data (output_section)->rel_hdr2
2339 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2340 == input_rel_hdr->sh_entsize))
2342 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2343 rel_countp = &elf_section_data (output_section)->rel_count2;
2345 else
2347 (*_bfd_error_handler)
2348 (_("%B: relocation size mismatch in %B section %A"),
2349 output_bfd, input_section->owner, input_section);
2350 bfd_set_error (bfd_error_wrong_format);
2351 return FALSE;
2354 bed = get_elf_backend_data (output_bfd);
2355 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2356 swap_out = bed->s->swap_reloc_out;
2357 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2358 swap_out = bed->s->swap_reloca_out;
2359 else
2360 abort ();
2362 erel = output_rel_hdr->contents;
2363 erel += *rel_countp * input_rel_hdr->sh_entsize;
2364 irela = internal_relocs;
2365 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2366 * bed->s->int_rels_per_ext_rel);
2367 while (irela < irelaend)
2369 (*swap_out) (output_bfd, irela, erel);
2370 irela += bed->s->int_rels_per_ext_rel;
2371 erel += input_rel_hdr->sh_entsize;
2374 /* Bump the counter, so that we know where to add the next set of
2375 relocations. */
2376 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2378 return TRUE;
2381 /* Make weak undefined symbols in PIE dynamic. */
2383 bfd_boolean
2384 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2385 struct elf_link_hash_entry *h)
2387 if (info->pie
2388 && h->dynindx == -1
2389 && h->root.type == bfd_link_hash_undefweak)
2390 return bfd_elf_link_record_dynamic_symbol (info, h);
2392 return TRUE;
2395 /* Fix up the flags for a symbol. This handles various cases which
2396 can only be fixed after all the input files are seen. This is
2397 currently called by both adjust_dynamic_symbol and
2398 assign_sym_version, which is unnecessary but perhaps more robust in
2399 the face of future changes. */
2401 bfd_boolean
2402 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2403 struct elf_info_failed *eif)
2405 const struct elf_backend_data *bed;
2407 /* If this symbol was mentioned in a non-ELF file, try to set
2408 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2409 permit a non-ELF file to correctly refer to a symbol defined in
2410 an ELF dynamic object. */
2411 if (h->non_elf)
2413 while (h->root.type == bfd_link_hash_indirect)
2414 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2416 if (h->root.type != bfd_link_hash_defined
2417 && h->root.type != bfd_link_hash_defweak)
2419 h->ref_regular = 1;
2420 h->ref_regular_nonweak = 1;
2422 else
2424 if (h->root.u.def.section->owner != NULL
2425 && (bfd_get_flavour (h->root.u.def.section->owner)
2426 == bfd_target_elf_flavour))
2428 h->ref_regular = 1;
2429 h->ref_regular_nonweak = 1;
2431 else
2432 h->def_regular = 1;
2435 if (h->dynindx == -1
2436 && (h->def_dynamic
2437 || h->ref_dynamic))
2439 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2441 eif->failed = TRUE;
2442 return FALSE;
2446 else
2448 /* Unfortunately, NON_ELF is only correct if the symbol
2449 was first seen in a non-ELF file. Fortunately, if the symbol
2450 was first seen in an ELF file, we're probably OK unless the
2451 symbol was defined in a non-ELF file. Catch that case here.
2452 FIXME: We're still in trouble if the symbol was first seen in
2453 a dynamic object, and then later in a non-ELF regular object. */
2454 if ((h->root.type == bfd_link_hash_defined
2455 || h->root.type == bfd_link_hash_defweak)
2456 && !h->def_regular
2457 && (h->root.u.def.section->owner != NULL
2458 ? (bfd_get_flavour (h->root.u.def.section->owner)
2459 != bfd_target_elf_flavour)
2460 : (bfd_is_abs_section (h->root.u.def.section)
2461 && !h->def_dynamic)))
2462 h->def_regular = 1;
2465 /* Backend specific symbol fixup. */
2466 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2467 if (bed->elf_backend_fixup_symbol
2468 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2469 return FALSE;
2471 /* If this is a final link, and the symbol was defined as a common
2472 symbol in a regular object file, and there was no definition in
2473 any dynamic object, then the linker will have allocated space for
2474 the symbol in a common section but the DEF_REGULAR
2475 flag will not have been set. */
2476 if (h->root.type == bfd_link_hash_defined
2477 && !h->def_regular
2478 && h->ref_regular
2479 && !h->def_dynamic
2480 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2481 h->def_regular = 1;
2483 /* If -Bsymbolic was used (which means to bind references to global
2484 symbols to the definition within the shared object), and this
2485 symbol was defined in a regular object, then it actually doesn't
2486 need a PLT entry. Likewise, if the symbol has non-default
2487 visibility. If the symbol has hidden or internal visibility, we
2488 will force it local. */
2489 if (h->needs_plt
2490 && eif->info->shared
2491 && is_elf_hash_table (eif->info->hash)
2492 && (SYMBOLIC_BIND (eif->info, h)
2493 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2494 && h->def_regular)
2496 bfd_boolean force_local;
2498 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2499 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2500 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2503 /* If a weak undefined symbol has non-default visibility, we also
2504 hide it from the dynamic linker. */
2505 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2506 && h->root.type == bfd_link_hash_undefweak)
2507 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2509 /* If this is a weak defined symbol in a dynamic object, and we know
2510 the real definition in the dynamic object, copy interesting flags
2511 over to the real definition. */
2512 if (h->u.weakdef != NULL)
2514 struct elf_link_hash_entry *weakdef;
2516 weakdef = h->u.weakdef;
2517 if (h->root.type == bfd_link_hash_indirect)
2518 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2520 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2521 || h->root.type == bfd_link_hash_defweak);
2522 BFD_ASSERT (weakdef->def_dynamic);
2524 /* If the real definition is defined by a regular object file,
2525 don't do anything special. See the longer description in
2526 _bfd_elf_adjust_dynamic_symbol, below. */
2527 if (weakdef->def_regular)
2528 h->u.weakdef = NULL;
2529 else
2531 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2532 || weakdef->root.type == bfd_link_hash_defweak);
2533 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2537 return TRUE;
2540 /* Make the backend pick a good value for a dynamic symbol. This is
2541 called via elf_link_hash_traverse, and also calls itself
2542 recursively. */
2544 bfd_boolean
2545 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2547 struct elf_info_failed *eif = data;
2548 bfd *dynobj;
2549 const struct elf_backend_data *bed;
2551 if (! is_elf_hash_table (eif->info->hash))
2552 return FALSE;
2554 if (h->root.type == bfd_link_hash_warning)
2556 h->got = elf_hash_table (eif->info)->init_got_offset;
2557 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2559 /* When warning symbols are created, they **replace** the "real"
2560 entry in the hash table, thus we never get to see the real
2561 symbol in a hash traversal. So look at it now. */
2562 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2565 /* Ignore indirect symbols. These are added by the versioning code. */
2566 if (h->root.type == bfd_link_hash_indirect)
2567 return TRUE;
2569 /* Fix the symbol flags. */
2570 if (! _bfd_elf_fix_symbol_flags (h, eif))
2571 return FALSE;
2573 /* If this symbol does not require a PLT entry, and it is not
2574 defined by a dynamic object, or is not referenced by a regular
2575 object, ignore it. We do have to handle a weak defined symbol,
2576 even if no regular object refers to it, if we decided to add it
2577 to the dynamic symbol table. FIXME: Do we normally need to worry
2578 about symbols which are defined by one dynamic object and
2579 referenced by another one? */
2580 if (!h->needs_plt
2581 && (h->def_regular
2582 || !h->def_dynamic
2583 || (!h->ref_regular
2584 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2586 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2587 return TRUE;
2590 /* If we've already adjusted this symbol, don't do it again. This
2591 can happen via a recursive call. */
2592 if (h->dynamic_adjusted)
2593 return TRUE;
2595 /* Don't look at this symbol again. Note that we must set this
2596 after checking the above conditions, because we may look at a
2597 symbol once, decide not to do anything, and then get called
2598 recursively later after REF_REGULAR is set below. */
2599 h->dynamic_adjusted = 1;
2601 /* If this is a weak definition, and we know a real definition, and
2602 the real symbol is not itself defined by a regular object file,
2603 then get a good value for the real definition. We handle the
2604 real symbol first, for the convenience of the backend routine.
2606 Note that there is a confusing case here. If the real definition
2607 is defined by a regular object file, we don't get the real symbol
2608 from the dynamic object, but we do get the weak symbol. If the
2609 processor backend uses a COPY reloc, then if some routine in the
2610 dynamic object changes the real symbol, we will not see that
2611 change in the corresponding weak symbol. This is the way other
2612 ELF linkers work as well, and seems to be a result of the shared
2613 library model.
2615 I will clarify this issue. Most SVR4 shared libraries define the
2616 variable _timezone and define timezone as a weak synonym. The
2617 tzset call changes _timezone. If you write
2618 extern int timezone;
2619 int _timezone = 5;
2620 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2621 you might expect that, since timezone is a synonym for _timezone,
2622 the same number will print both times. However, if the processor
2623 backend uses a COPY reloc, then actually timezone will be copied
2624 into your process image, and, since you define _timezone
2625 yourself, _timezone will not. Thus timezone and _timezone will
2626 wind up at different memory locations. The tzset call will set
2627 _timezone, leaving timezone unchanged. */
2629 if (h->u.weakdef != NULL)
2631 /* If we get to this point, we know there is an implicit
2632 reference by a regular object file via the weak symbol H.
2633 FIXME: Is this really true? What if the traversal finds
2634 H->U.WEAKDEF before it finds H? */
2635 h->u.weakdef->ref_regular = 1;
2637 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2638 return FALSE;
2641 /* If a symbol has no type and no size and does not require a PLT
2642 entry, then we are probably about to do the wrong thing here: we
2643 are probably going to create a COPY reloc for an empty object.
2644 This case can arise when a shared object is built with assembly
2645 code, and the assembly code fails to set the symbol type. */
2646 if (h->size == 0
2647 && h->type == STT_NOTYPE
2648 && !h->needs_plt)
2649 (*_bfd_error_handler)
2650 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2651 h->root.root.string);
2653 dynobj = elf_hash_table (eif->info)->dynobj;
2654 bed = get_elf_backend_data (dynobj);
2656 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2658 eif->failed = TRUE;
2659 return FALSE;
2662 return TRUE;
2665 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2666 DYNBSS. */
2668 bfd_boolean
2669 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2670 asection *dynbss)
2672 unsigned int power_of_two;
2673 bfd_vma mask;
2674 asection *sec = h->root.u.def.section;
2676 /* The section aligment of definition is the maximum alignment
2677 requirement of symbols defined in the section. Since we don't
2678 know the symbol alignment requirement, we start with the
2679 maximum alignment and check low bits of the symbol address
2680 for the minimum alignment. */
2681 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2682 mask = ((bfd_vma) 1 << power_of_two) - 1;
2683 while ((h->root.u.def.value & mask) != 0)
2685 mask >>= 1;
2686 --power_of_two;
2689 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2690 dynbss))
2692 /* Adjust the section alignment if needed. */
2693 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2694 power_of_two))
2695 return FALSE;
2698 /* We make sure that the symbol will be aligned properly. */
2699 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2701 /* Define the symbol as being at this point in DYNBSS. */
2702 h->root.u.def.section = dynbss;
2703 h->root.u.def.value = dynbss->size;
2705 /* Increment the size of DYNBSS to make room for the symbol. */
2706 dynbss->size += h->size;
2708 return TRUE;
2711 /* Adjust all external symbols pointing into SEC_MERGE sections
2712 to reflect the object merging within the sections. */
2714 bfd_boolean
2715 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2717 asection *sec;
2719 if (h->root.type == bfd_link_hash_warning)
2720 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2722 if ((h->root.type == bfd_link_hash_defined
2723 || h->root.type == bfd_link_hash_defweak)
2724 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2725 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2727 bfd *output_bfd = data;
2729 h->root.u.def.value =
2730 _bfd_merged_section_offset (output_bfd,
2731 &h->root.u.def.section,
2732 elf_section_data (sec)->sec_info,
2733 h->root.u.def.value);
2736 return TRUE;
2739 /* Returns false if the symbol referred to by H should be considered
2740 to resolve local to the current module, and true if it should be
2741 considered to bind dynamically. */
2743 bfd_boolean
2744 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2745 struct bfd_link_info *info,
2746 bfd_boolean ignore_protected)
2748 bfd_boolean binding_stays_local_p;
2749 const struct elf_backend_data *bed;
2750 struct elf_link_hash_table *hash_table;
2752 if (h == NULL)
2753 return FALSE;
2755 while (h->root.type == bfd_link_hash_indirect
2756 || h->root.type == bfd_link_hash_warning)
2757 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2759 /* If it was forced local, then clearly it's not dynamic. */
2760 if (h->dynindx == -1)
2761 return FALSE;
2762 if (h->forced_local)
2763 return FALSE;
2765 /* Identify the cases where name binding rules say that a
2766 visible symbol resolves locally. */
2767 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2769 switch (ELF_ST_VISIBILITY (h->other))
2771 case STV_INTERNAL:
2772 case STV_HIDDEN:
2773 return FALSE;
2775 case STV_PROTECTED:
2776 hash_table = elf_hash_table (info);
2777 if (!is_elf_hash_table (hash_table))
2778 return FALSE;
2780 bed = get_elf_backend_data (hash_table->dynobj);
2782 /* Proper resolution for function pointer equality may require
2783 that these symbols perhaps be resolved dynamically, even though
2784 we should be resolving them to the current module. */
2785 if (!ignore_protected || !bed->is_function_type (h->type))
2786 binding_stays_local_p = TRUE;
2787 break;
2789 default:
2790 break;
2793 /* If it isn't defined locally, then clearly it's dynamic. */
2794 if (!h->def_regular)
2795 return TRUE;
2797 /* Otherwise, the symbol is dynamic if binding rules don't tell
2798 us that it remains local. */
2799 return !binding_stays_local_p;
2802 /* Return true if the symbol referred to by H should be considered
2803 to resolve local to the current module, and false otherwise. Differs
2804 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2805 undefined symbols and weak symbols. */
2807 bfd_boolean
2808 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2809 struct bfd_link_info *info,
2810 bfd_boolean local_protected)
2812 const struct elf_backend_data *bed;
2813 struct elf_link_hash_table *hash_table;
2815 /* If it's a local sym, of course we resolve locally. */
2816 if (h == NULL)
2817 return TRUE;
2819 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2820 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2821 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2822 return TRUE;
2824 /* Common symbols that become definitions don't get the DEF_REGULAR
2825 flag set, so test it first, and don't bail out. */
2826 if (ELF_COMMON_DEF_P (h))
2827 /* Do nothing. */;
2828 /* If we don't have a definition in a regular file, then we can't
2829 resolve locally. The sym is either undefined or dynamic. */
2830 else if (!h->def_regular)
2831 return FALSE;
2833 /* Forced local symbols resolve locally. */
2834 if (h->forced_local)
2835 return TRUE;
2837 /* As do non-dynamic symbols. */
2838 if (h->dynindx == -1)
2839 return TRUE;
2841 /* At this point, we know the symbol is defined and dynamic. In an
2842 executable it must resolve locally, likewise when building symbolic
2843 shared libraries. */
2844 if (info->executable || SYMBOLIC_BIND (info, h))
2845 return TRUE;
2847 /* Now deal with defined dynamic symbols in shared libraries. Ones
2848 with default visibility might not resolve locally. */
2849 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2850 return FALSE;
2852 hash_table = elf_hash_table (info);
2853 if (!is_elf_hash_table (hash_table))
2854 return TRUE;
2856 bed = get_elf_backend_data (hash_table->dynobj);
2858 /* STV_PROTECTED non-function symbols are local. */
2859 if (!bed->is_function_type (h->type))
2860 return TRUE;
2862 /* Function pointer equality tests may require that STV_PROTECTED
2863 symbols be treated as dynamic symbols, even when we know that the
2864 dynamic linker will resolve them locally. */
2865 return local_protected;
2868 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2869 aligned. Returns the first TLS output section. */
2871 struct bfd_section *
2872 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2874 struct bfd_section *sec, *tls;
2875 unsigned int align = 0;
2877 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2878 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2879 break;
2880 tls = sec;
2882 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2883 if (sec->alignment_power > align)
2884 align = sec->alignment_power;
2886 elf_hash_table (info)->tls_sec = tls;
2888 /* Ensure the alignment of the first section is the largest alignment,
2889 so that the tls segment starts aligned. */
2890 if (tls != NULL)
2891 tls->alignment_power = align;
2893 return tls;
2896 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2897 static bfd_boolean
2898 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2899 Elf_Internal_Sym *sym)
2901 const struct elf_backend_data *bed;
2903 /* Local symbols do not count, but target specific ones might. */
2904 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2905 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2906 return FALSE;
2908 bed = get_elf_backend_data (abfd);
2909 /* Function symbols do not count. */
2910 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2911 return FALSE;
2913 /* If the section is undefined, then so is the symbol. */
2914 if (sym->st_shndx == SHN_UNDEF)
2915 return FALSE;
2917 /* If the symbol is defined in the common section, then
2918 it is a common definition and so does not count. */
2919 if (bed->common_definition (sym))
2920 return FALSE;
2922 /* If the symbol is in a target specific section then we
2923 must rely upon the backend to tell us what it is. */
2924 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2925 /* FIXME - this function is not coded yet:
2927 return _bfd_is_global_symbol_definition (abfd, sym);
2929 Instead for now assume that the definition is not global,
2930 Even if this is wrong, at least the linker will behave
2931 in the same way that it used to do. */
2932 return FALSE;
2934 return TRUE;
2937 /* Search the symbol table of the archive element of the archive ABFD
2938 whose archive map contains a mention of SYMDEF, and determine if
2939 the symbol is defined in this element. */
2940 static bfd_boolean
2941 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2943 Elf_Internal_Shdr * hdr;
2944 bfd_size_type symcount;
2945 bfd_size_type extsymcount;
2946 bfd_size_type extsymoff;
2947 Elf_Internal_Sym *isymbuf;
2948 Elf_Internal_Sym *isym;
2949 Elf_Internal_Sym *isymend;
2950 bfd_boolean result;
2952 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2953 if (abfd == NULL)
2954 return FALSE;
2956 if (! bfd_check_format (abfd, bfd_object))
2957 return FALSE;
2959 /* If we have already included the element containing this symbol in the
2960 link then we do not need to include it again. Just claim that any symbol
2961 it contains is not a definition, so that our caller will not decide to
2962 (re)include this element. */
2963 if (abfd->archive_pass)
2964 return FALSE;
2966 /* Select the appropriate symbol table. */
2967 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2968 hdr = &elf_tdata (abfd)->symtab_hdr;
2969 else
2970 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2972 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2974 /* The sh_info field of the symtab header tells us where the
2975 external symbols start. We don't care about the local symbols. */
2976 if (elf_bad_symtab (abfd))
2978 extsymcount = symcount;
2979 extsymoff = 0;
2981 else
2983 extsymcount = symcount - hdr->sh_info;
2984 extsymoff = hdr->sh_info;
2987 if (extsymcount == 0)
2988 return FALSE;
2990 /* Read in the symbol table. */
2991 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2992 NULL, NULL, NULL);
2993 if (isymbuf == NULL)
2994 return FALSE;
2996 /* Scan the symbol table looking for SYMDEF. */
2997 result = FALSE;
2998 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3000 const char *name;
3002 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3003 isym->st_name);
3004 if (name == NULL)
3005 break;
3007 if (strcmp (name, symdef->name) == 0)
3009 result = is_global_data_symbol_definition (abfd, isym);
3010 break;
3014 free (isymbuf);
3016 return result;
3019 /* Add an entry to the .dynamic table. */
3021 bfd_boolean
3022 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3023 bfd_vma tag,
3024 bfd_vma val)
3026 struct elf_link_hash_table *hash_table;
3027 const struct elf_backend_data *bed;
3028 asection *s;
3029 bfd_size_type newsize;
3030 bfd_byte *newcontents;
3031 Elf_Internal_Dyn dyn;
3033 hash_table = elf_hash_table (info);
3034 if (! is_elf_hash_table (hash_table))
3035 return FALSE;
3037 bed = get_elf_backend_data (hash_table->dynobj);
3038 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3039 BFD_ASSERT (s != NULL);
3041 newsize = s->size + bed->s->sizeof_dyn;
3042 newcontents = bfd_realloc (s->contents, newsize);
3043 if (newcontents == NULL)
3044 return FALSE;
3046 dyn.d_tag = tag;
3047 dyn.d_un.d_val = val;
3048 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3050 s->size = newsize;
3051 s->contents = newcontents;
3053 return TRUE;
3056 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3057 otherwise just check whether one already exists. Returns -1 on error,
3058 1 if a DT_NEEDED tag already exists, and 0 on success. */
3060 static int
3061 elf_add_dt_needed_tag (bfd *abfd,
3062 struct bfd_link_info *info,
3063 const char *soname,
3064 bfd_boolean do_it)
3066 struct elf_link_hash_table *hash_table;
3067 bfd_size_type oldsize;
3068 bfd_size_type strindex;
3070 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3071 return -1;
3073 hash_table = elf_hash_table (info);
3074 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3075 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3076 if (strindex == (bfd_size_type) -1)
3077 return -1;
3079 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3081 asection *sdyn;
3082 const struct elf_backend_data *bed;
3083 bfd_byte *extdyn;
3085 bed = get_elf_backend_data (hash_table->dynobj);
3086 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3087 if (sdyn != NULL)
3088 for (extdyn = sdyn->contents;
3089 extdyn < sdyn->contents + sdyn->size;
3090 extdyn += bed->s->sizeof_dyn)
3092 Elf_Internal_Dyn dyn;
3094 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3095 if (dyn.d_tag == DT_NEEDED
3096 && dyn.d_un.d_val == strindex)
3098 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3099 return 1;
3104 if (do_it)
3106 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3107 return -1;
3109 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3110 return -1;
3112 else
3113 /* We were just checking for existence of the tag. */
3114 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3116 return 0;
3119 /* Sort symbol by value and section. */
3120 static int
3121 elf_sort_symbol (const void *arg1, const void *arg2)
3123 const struct elf_link_hash_entry *h1;
3124 const struct elf_link_hash_entry *h2;
3125 bfd_signed_vma vdiff;
3127 h1 = *(const struct elf_link_hash_entry **) arg1;
3128 h2 = *(const struct elf_link_hash_entry **) arg2;
3129 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3130 if (vdiff != 0)
3131 return vdiff > 0 ? 1 : -1;
3132 else
3134 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3135 if (sdiff != 0)
3136 return sdiff > 0 ? 1 : -1;
3138 return 0;
3141 /* This function is used to adjust offsets into .dynstr for
3142 dynamic symbols. This is called via elf_link_hash_traverse. */
3144 static bfd_boolean
3145 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3147 struct elf_strtab_hash *dynstr = data;
3149 if (h->root.type == bfd_link_hash_warning)
3150 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3152 if (h->dynindx != -1)
3153 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3154 return TRUE;
3157 /* Assign string offsets in .dynstr, update all structures referencing
3158 them. */
3160 static bfd_boolean
3161 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3163 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3164 struct elf_link_local_dynamic_entry *entry;
3165 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3166 bfd *dynobj = hash_table->dynobj;
3167 asection *sdyn;
3168 bfd_size_type size;
3169 const struct elf_backend_data *bed;
3170 bfd_byte *extdyn;
3172 _bfd_elf_strtab_finalize (dynstr);
3173 size = _bfd_elf_strtab_size (dynstr);
3175 bed = get_elf_backend_data (dynobj);
3176 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3177 BFD_ASSERT (sdyn != NULL);
3179 /* Update all .dynamic entries referencing .dynstr strings. */
3180 for (extdyn = sdyn->contents;
3181 extdyn < sdyn->contents + sdyn->size;
3182 extdyn += bed->s->sizeof_dyn)
3184 Elf_Internal_Dyn dyn;
3186 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3187 switch (dyn.d_tag)
3189 case DT_STRSZ:
3190 dyn.d_un.d_val = size;
3191 break;
3192 case DT_NEEDED:
3193 case DT_SONAME:
3194 case DT_RPATH:
3195 case DT_RUNPATH:
3196 case DT_FILTER:
3197 case DT_AUXILIARY:
3198 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3199 break;
3200 default:
3201 continue;
3203 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3206 /* Now update local dynamic symbols. */
3207 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3208 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3209 entry->isym.st_name);
3211 /* And the rest of dynamic symbols. */
3212 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3214 /* Adjust version definitions. */
3215 if (elf_tdata (output_bfd)->cverdefs)
3217 asection *s;
3218 bfd_byte *p;
3219 bfd_size_type i;
3220 Elf_Internal_Verdef def;
3221 Elf_Internal_Verdaux defaux;
3223 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3224 p = s->contents;
3227 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3228 &def);
3229 p += sizeof (Elf_External_Verdef);
3230 if (def.vd_aux != sizeof (Elf_External_Verdef))
3231 continue;
3232 for (i = 0; i < def.vd_cnt; ++i)
3234 _bfd_elf_swap_verdaux_in (output_bfd,
3235 (Elf_External_Verdaux *) p, &defaux);
3236 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3237 defaux.vda_name);
3238 _bfd_elf_swap_verdaux_out (output_bfd,
3239 &defaux, (Elf_External_Verdaux *) p);
3240 p += sizeof (Elf_External_Verdaux);
3243 while (def.vd_next);
3246 /* Adjust version references. */
3247 if (elf_tdata (output_bfd)->verref)
3249 asection *s;
3250 bfd_byte *p;
3251 bfd_size_type i;
3252 Elf_Internal_Verneed need;
3253 Elf_Internal_Vernaux needaux;
3255 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3256 p = s->contents;
3259 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3260 &need);
3261 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3262 _bfd_elf_swap_verneed_out (output_bfd, &need,
3263 (Elf_External_Verneed *) p);
3264 p += sizeof (Elf_External_Verneed);
3265 for (i = 0; i < need.vn_cnt; ++i)
3267 _bfd_elf_swap_vernaux_in (output_bfd,
3268 (Elf_External_Vernaux *) p, &needaux);
3269 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3270 needaux.vna_name);
3271 _bfd_elf_swap_vernaux_out (output_bfd,
3272 &needaux,
3273 (Elf_External_Vernaux *) p);
3274 p += sizeof (Elf_External_Vernaux);
3277 while (need.vn_next);
3280 return TRUE;
3283 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3284 The default is to only match when the INPUT and OUTPUT are exactly
3285 the same target. */
3287 bfd_boolean
3288 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3289 const bfd_target *output)
3291 return input == output;
3294 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3295 This version is used when different targets for the same architecture
3296 are virtually identical. */
3298 bfd_boolean
3299 _bfd_elf_relocs_compatible (const bfd_target *input,
3300 const bfd_target *output)
3302 const struct elf_backend_data *obed, *ibed;
3304 if (input == output)
3305 return TRUE;
3307 ibed = xvec_get_elf_backend_data (input);
3308 obed = xvec_get_elf_backend_data (output);
3310 if (ibed->arch != obed->arch)
3311 return FALSE;
3313 /* If both backends are using this function, deem them compatible. */
3314 return ibed->relocs_compatible == obed->relocs_compatible;
3317 /* Add symbols from an ELF object file to the linker hash table. */
3319 static bfd_boolean
3320 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3322 Elf_Internal_Shdr *hdr;
3323 bfd_size_type symcount;
3324 bfd_size_type extsymcount;
3325 bfd_size_type extsymoff;
3326 struct elf_link_hash_entry **sym_hash;
3327 bfd_boolean dynamic;
3328 Elf_External_Versym *extversym = NULL;
3329 Elf_External_Versym *ever;
3330 struct elf_link_hash_entry *weaks;
3331 struct elf_link_hash_entry **nondeflt_vers = NULL;
3332 bfd_size_type nondeflt_vers_cnt = 0;
3333 Elf_Internal_Sym *isymbuf = NULL;
3334 Elf_Internal_Sym *isym;
3335 Elf_Internal_Sym *isymend;
3336 const struct elf_backend_data *bed;
3337 bfd_boolean add_needed;
3338 struct elf_link_hash_table *htab;
3339 bfd_size_type amt;
3340 void *alloc_mark = NULL;
3341 struct bfd_hash_entry **old_table = NULL;
3342 unsigned int old_size = 0;
3343 unsigned int old_count = 0;
3344 void *old_tab = NULL;
3345 void *old_hash;
3346 void *old_ent;
3347 struct bfd_link_hash_entry *old_undefs = NULL;
3348 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3349 long old_dynsymcount = 0;
3350 size_t tabsize = 0;
3351 size_t hashsize = 0;
3353 htab = elf_hash_table (info);
3354 bed = get_elf_backend_data (abfd);
3356 if ((abfd->flags & DYNAMIC) == 0)
3357 dynamic = FALSE;
3358 else
3360 dynamic = TRUE;
3362 /* You can't use -r against a dynamic object. Also, there's no
3363 hope of using a dynamic object which does not exactly match
3364 the format of the output file. */
3365 if (info->relocatable
3366 || !is_elf_hash_table (htab)
3367 || info->output_bfd->xvec != abfd->xvec)
3369 if (info->relocatable)
3370 bfd_set_error (bfd_error_invalid_operation);
3371 else
3372 bfd_set_error (bfd_error_wrong_format);
3373 goto error_return;
3377 /* As a GNU extension, any input sections which are named
3378 .gnu.warning.SYMBOL are treated as warning symbols for the given
3379 symbol. This differs from .gnu.warning sections, which generate
3380 warnings when they are included in an output file. */
3381 if (info->executable)
3383 asection *s;
3385 for (s = abfd->sections; s != NULL; s = s->next)
3387 const char *name;
3389 name = bfd_get_section_name (abfd, s);
3390 if (CONST_STRNEQ (name, ".gnu.warning."))
3392 char *msg;
3393 bfd_size_type sz;
3395 name += sizeof ".gnu.warning." - 1;
3397 /* If this is a shared object, then look up the symbol
3398 in the hash table. If it is there, and it is already
3399 been defined, then we will not be using the entry
3400 from this shared object, so we don't need to warn.
3401 FIXME: If we see the definition in a regular object
3402 later on, we will warn, but we shouldn't. The only
3403 fix is to keep track of what warnings we are supposed
3404 to emit, and then handle them all at the end of the
3405 link. */
3406 if (dynamic)
3408 struct elf_link_hash_entry *h;
3410 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3412 /* FIXME: What about bfd_link_hash_common? */
3413 if (h != NULL
3414 && (h->root.type == bfd_link_hash_defined
3415 || h->root.type == bfd_link_hash_defweak))
3417 /* We don't want to issue this warning. Clobber
3418 the section size so that the warning does not
3419 get copied into the output file. */
3420 s->size = 0;
3421 continue;
3425 sz = s->size;
3426 msg = bfd_alloc (abfd, sz + 1);
3427 if (msg == NULL)
3428 goto error_return;
3430 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3431 goto error_return;
3433 msg[sz] = '\0';
3435 if (! (_bfd_generic_link_add_one_symbol
3436 (info, abfd, name, BSF_WARNING, s, 0, msg,
3437 FALSE, bed->collect, NULL)))
3438 goto error_return;
3440 if (! info->relocatable)
3442 /* Clobber the section size so that the warning does
3443 not get copied into the output file. */
3444 s->size = 0;
3446 /* Also set SEC_EXCLUDE, so that symbols defined in
3447 the warning section don't get copied to the output. */
3448 s->flags |= SEC_EXCLUDE;
3454 add_needed = TRUE;
3455 if (! dynamic)
3457 /* If we are creating a shared library, create all the dynamic
3458 sections immediately. We need to attach them to something,
3459 so we attach them to this BFD, provided it is the right
3460 format. FIXME: If there are no input BFD's of the same
3461 format as the output, we can't make a shared library. */
3462 if (info->shared
3463 && is_elf_hash_table (htab)
3464 && info->output_bfd->xvec == abfd->xvec
3465 && !htab->dynamic_sections_created)
3467 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3468 goto error_return;
3471 else if (!is_elf_hash_table (htab))
3472 goto error_return;
3473 else
3475 asection *s;
3476 const char *soname = NULL;
3477 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3478 int ret;
3480 /* ld --just-symbols and dynamic objects don't mix very well.
3481 ld shouldn't allow it. */
3482 if ((s = abfd->sections) != NULL
3483 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3484 abort ();
3486 /* If this dynamic lib was specified on the command line with
3487 --as-needed in effect, then we don't want to add a DT_NEEDED
3488 tag unless the lib is actually used. Similary for libs brought
3489 in by another lib's DT_NEEDED. When --no-add-needed is used
3490 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3491 any dynamic library in DT_NEEDED tags in the dynamic lib at
3492 all. */
3493 add_needed = (elf_dyn_lib_class (abfd)
3494 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3495 | DYN_NO_NEEDED)) == 0;
3497 s = bfd_get_section_by_name (abfd, ".dynamic");
3498 if (s != NULL)
3500 bfd_byte *dynbuf;
3501 bfd_byte *extdyn;
3502 unsigned int elfsec;
3503 unsigned long shlink;
3505 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3506 goto error_free_dyn;
3508 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3509 if (elfsec == SHN_BAD)
3510 goto error_free_dyn;
3511 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3513 for (extdyn = dynbuf;
3514 extdyn < dynbuf + s->size;
3515 extdyn += bed->s->sizeof_dyn)
3517 Elf_Internal_Dyn dyn;
3519 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3520 if (dyn.d_tag == DT_SONAME)
3522 unsigned int tagv = dyn.d_un.d_val;
3523 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3524 if (soname == NULL)
3525 goto error_free_dyn;
3527 if (dyn.d_tag == DT_NEEDED)
3529 struct bfd_link_needed_list *n, **pn;
3530 char *fnm, *anm;
3531 unsigned int tagv = dyn.d_un.d_val;
3533 amt = sizeof (struct bfd_link_needed_list);
3534 n = bfd_alloc (abfd, amt);
3535 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3536 if (n == NULL || fnm == NULL)
3537 goto error_free_dyn;
3538 amt = strlen (fnm) + 1;
3539 anm = bfd_alloc (abfd, amt);
3540 if (anm == NULL)
3541 goto error_free_dyn;
3542 memcpy (anm, fnm, amt);
3543 n->name = anm;
3544 n->by = abfd;
3545 n->next = NULL;
3546 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3548 *pn = n;
3550 if (dyn.d_tag == DT_RUNPATH)
3552 struct bfd_link_needed_list *n, **pn;
3553 char *fnm, *anm;
3554 unsigned int tagv = dyn.d_un.d_val;
3556 amt = sizeof (struct bfd_link_needed_list);
3557 n = bfd_alloc (abfd, amt);
3558 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3559 if (n == NULL || fnm == NULL)
3560 goto error_free_dyn;
3561 amt = strlen (fnm) + 1;
3562 anm = bfd_alloc (abfd, amt);
3563 if (anm == NULL)
3564 goto error_free_dyn;
3565 memcpy (anm, fnm, amt);
3566 n->name = anm;
3567 n->by = abfd;
3568 n->next = NULL;
3569 for (pn = & runpath;
3570 *pn != NULL;
3571 pn = &(*pn)->next)
3573 *pn = n;
3575 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3576 if (!runpath && dyn.d_tag == DT_RPATH)
3578 struct bfd_link_needed_list *n, **pn;
3579 char *fnm, *anm;
3580 unsigned int tagv = dyn.d_un.d_val;
3582 amt = sizeof (struct bfd_link_needed_list);
3583 n = bfd_alloc (abfd, amt);
3584 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3585 if (n == NULL || fnm == NULL)
3586 goto error_free_dyn;
3587 amt = strlen (fnm) + 1;
3588 anm = bfd_alloc (abfd, amt);
3589 if (anm == NULL)
3591 error_free_dyn:
3592 free (dynbuf);
3593 goto error_return;
3595 memcpy (anm, fnm, amt);
3596 n->name = anm;
3597 n->by = abfd;
3598 n->next = NULL;
3599 for (pn = & rpath;
3600 *pn != NULL;
3601 pn = &(*pn)->next)
3603 *pn = n;
3607 free (dynbuf);
3610 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3611 frees all more recently bfd_alloc'd blocks as well. */
3612 if (runpath)
3613 rpath = runpath;
3615 if (rpath)
3617 struct bfd_link_needed_list **pn;
3618 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3620 *pn = rpath;
3623 /* We do not want to include any of the sections in a dynamic
3624 object in the output file. We hack by simply clobbering the
3625 list of sections in the BFD. This could be handled more
3626 cleanly by, say, a new section flag; the existing
3627 SEC_NEVER_LOAD flag is not the one we want, because that one
3628 still implies that the section takes up space in the output
3629 file. */
3630 bfd_section_list_clear (abfd);
3632 /* Find the name to use in a DT_NEEDED entry that refers to this
3633 object. If the object has a DT_SONAME entry, we use it.
3634 Otherwise, if the generic linker stuck something in
3635 elf_dt_name, we use that. Otherwise, we just use the file
3636 name. */
3637 if (soname == NULL || *soname == '\0')
3639 soname = elf_dt_name (abfd);
3640 if (soname == NULL || *soname == '\0')
3641 soname = bfd_get_filename (abfd);
3644 /* Save the SONAME because sometimes the linker emulation code
3645 will need to know it. */
3646 elf_dt_name (abfd) = soname;
3648 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3649 if (ret < 0)
3650 goto error_return;
3652 /* If we have already included this dynamic object in the
3653 link, just ignore it. There is no reason to include a
3654 particular dynamic object more than once. */
3655 if (ret > 0)
3656 return TRUE;
3659 /* If this is a dynamic object, we always link against the .dynsym
3660 symbol table, not the .symtab symbol table. The dynamic linker
3661 will only see the .dynsym symbol table, so there is no reason to
3662 look at .symtab for a dynamic object. */
3664 if (! dynamic || elf_dynsymtab (abfd) == 0)
3665 hdr = &elf_tdata (abfd)->symtab_hdr;
3666 else
3667 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3669 symcount = hdr->sh_size / bed->s->sizeof_sym;
3671 /* The sh_info field of the symtab header tells us where the
3672 external symbols start. We don't care about the local symbols at
3673 this point. */
3674 if (elf_bad_symtab (abfd))
3676 extsymcount = symcount;
3677 extsymoff = 0;
3679 else
3681 extsymcount = symcount - hdr->sh_info;
3682 extsymoff = hdr->sh_info;
3685 sym_hash = NULL;
3686 if (extsymcount != 0)
3688 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3689 NULL, NULL, NULL);
3690 if (isymbuf == NULL)
3691 goto error_return;
3693 /* We store a pointer to the hash table entry for each external
3694 symbol. */
3695 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3696 sym_hash = bfd_alloc (abfd, amt);
3697 if (sym_hash == NULL)
3698 goto error_free_sym;
3699 elf_sym_hashes (abfd) = sym_hash;
3702 if (dynamic)
3704 /* Read in any version definitions. */
3705 if (!_bfd_elf_slurp_version_tables (abfd,
3706 info->default_imported_symver))
3707 goto error_free_sym;
3709 /* Read in the symbol versions, but don't bother to convert them
3710 to internal format. */
3711 if (elf_dynversym (abfd) != 0)
3713 Elf_Internal_Shdr *versymhdr;
3715 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3716 extversym = bfd_malloc (versymhdr->sh_size);
3717 if (extversym == NULL)
3718 goto error_free_sym;
3719 amt = versymhdr->sh_size;
3720 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3721 || bfd_bread (extversym, amt, abfd) != amt)
3722 goto error_free_vers;
3726 /* If we are loading an as-needed shared lib, save the symbol table
3727 state before we start adding symbols. If the lib turns out
3728 to be unneeded, restore the state. */
3729 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3731 unsigned int i;
3732 size_t entsize;
3734 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3736 struct bfd_hash_entry *p;
3737 struct elf_link_hash_entry *h;
3739 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3741 h = (struct elf_link_hash_entry *) p;
3742 entsize += htab->root.table.entsize;
3743 if (h->root.type == bfd_link_hash_warning)
3744 entsize += htab->root.table.entsize;
3748 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3749 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3750 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3751 if (old_tab == NULL)
3752 goto error_free_vers;
3754 /* Remember the current objalloc pointer, so that all mem for
3755 symbols added can later be reclaimed. */
3756 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3757 if (alloc_mark == NULL)
3758 goto error_free_vers;
3760 /* Make a special call to the linker "notice" function to
3761 tell it that we are about to handle an as-needed lib. */
3762 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3763 notice_as_needed))
3764 goto error_free_vers;
3766 /* Clone the symbol table and sym hashes. Remember some
3767 pointers into the symbol table, and dynamic symbol count. */
3768 old_hash = (char *) old_tab + tabsize;
3769 old_ent = (char *) old_hash + hashsize;
3770 memcpy (old_tab, htab->root.table.table, tabsize);
3771 memcpy (old_hash, sym_hash, hashsize);
3772 old_undefs = htab->root.undefs;
3773 old_undefs_tail = htab->root.undefs_tail;
3774 old_table = htab->root.table.table;
3775 old_size = htab->root.table.size;
3776 old_count = htab->root.table.count;
3777 old_dynsymcount = htab->dynsymcount;
3779 for (i = 0; i < htab->root.table.size; i++)
3781 struct bfd_hash_entry *p;
3782 struct elf_link_hash_entry *h;
3784 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3786 memcpy (old_ent, p, htab->root.table.entsize);
3787 old_ent = (char *) old_ent + htab->root.table.entsize;
3788 h = (struct elf_link_hash_entry *) p;
3789 if (h->root.type == bfd_link_hash_warning)
3791 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3792 old_ent = (char *) old_ent + htab->root.table.entsize;
3798 weaks = NULL;
3799 ever = extversym != NULL ? extversym + extsymoff : NULL;
3800 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3801 isym < isymend;
3802 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3804 int bind;
3805 bfd_vma value;
3806 asection *sec, *new_sec;
3807 flagword flags;
3808 const char *name;
3809 struct elf_link_hash_entry *h;
3810 bfd_boolean definition;
3811 bfd_boolean size_change_ok;
3812 bfd_boolean type_change_ok;
3813 bfd_boolean new_weakdef;
3814 bfd_boolean override;
3815 bfd_boolean common;
3816 unsigned int old_alignment;
3817 bfd *old_bfd;
3819 override = FALSE;
3821 flags = BSF_NO_FLAGS;
3822 sec = NULL;
3823 value = isym->st_value;
3824 *sym_hash = NULL;
3825 common = bed->common_definition (isym);
3827 bind = ELF_ST_BIND (isym->st_info);
3828 if (bind == STB_LOCAL)
3830 /* This should be impossible, since ELF requires that all
3831 global symbols follow all local symbols, and that sh_info
3832 point to the first global symbol. Unfortunately, Irix 5
3833 screws this up. */
3834 continue;
3836 else if (bind == STB_GLOBAL)
3838 if (isym->st_shndx != SHN_UNDEF && !common)
3839 flags = BSF_GLOBAL;
3841 else if (bind == STB_WEAK)
3842 flags = BSF_WEAK;
3843 else
3845 /* Leave it up to the processor backend. */
3848 if (isym->st_shndx == SHN_UNDEF)
3849 sec = bfd_und_section_ptr;
3850 else if (isym->st_shndx == SHN_ABS)
3851 sec = bfd_abs_section_ptr;
3852 else if (isym->st_shndx == SHN_COMMON)
3854 sec = bfd_com_section_ptr;
3855 /* What ELF calls the size we call the value. What ELF
3856 calls the value we call the alignment. */
3857 value = isym->st_size;
3859 else
3861 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3862 if (sec == NULL)
3863 sec = bfd_abs_section_ptr;
3864 else if (sec->kept_section)
3866 /* Symbols from discarded section are undefined. We keep
3867 its visibility. */
3868 sec = bfd_und_section_ptr;
3869 isym->st_shndx = SHN_UNDEF;
3871 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3872 value -= sec->vma;
3875 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3876 isym->st_name);
3877 if (name == NULL)
3878 goto error_free_vers;
3880 if (isym->st_shndx == SHN_COMMON
3881 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3882 && !info->relocatable)
3884 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3886 if (tcomm == NULL)
3888 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3889 (SEC_ALLOC
3890 | SEC_IS_COMMON
3891 | SEC_LINKER_CREATED
3892 | SEC_THREAD_LOCAL));
3893 if (tcomm == NULL)
3894 goto error_free_vers;
3896 sec = tcomm;
3898 else if (bed->elf_add_symbol_hook)
3900 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3901 &sec, &value))
3902 goto error_free_vers;
3904 /* The hook function sets the name to NULL if this symbol
3905 should be skipped for some reason. */
3906 if (name == NULL)
3907 continue;
3910 /* Sanity check that all possibilities were handled. */
3911 if (sec == NULL)
3913 bfd_set_error (bfd_error_bad_value);
3914 goto error_free_vers;
3917 if (bfd_is_und_section (sec)
3918 || bfd_is_com_section (sec))
3919 definition = FALSE;
3920 else
3921 definition = TRUE;
3923 size_change_ok = FALSE;
3924 type_change_ok = bed->type_change_ok;
3925 old_alignment = 0;
3926 old_bfd = NULL;
3927 new_sec = sec;
3929 if (is_elf_hash_table (htab))
3931 Elf_Internal_Versym iver;
3932 unsigned int vernum = 0;
3933 bfd_boolean skip;
3935 if (ever == NULL)
3937 if (info->default_imported_symver)
3938 /* Use the default symbol version created earlier. */
3939 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3940 else
3941 iver.vs_vers = 0;
3943 else
3944 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3946 vernum = iver.vs_vers & VERSYM_VERSION;
3948 /* If this is a hidden symbol, or if it is not version
3949 1, we append the version name to the symbol name.
3950 However, we do not modify a non-hidden absolute symbol
3951 if it is not a function, because it might be the version
3952 symbol itself. FIXME: What if it isn't? */
3953 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3954 || (vernum > 1
3955 && (!bfd_is_abs_section (sec)
3956 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
3958 const char *verstr;
3959 size_t namelen, verlen, newlen;
3960 char *newname, *p;
3962 if (isym->st_shndx != SHN_UNDEF)
3964 if (vernum > elf_tdata (abfd)->cverdefs)
3965 verstr = NULL;
3966 else if (vernum > 1)
3967 verstr =
3968 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3969 else
3970 verstr = "";
3972 if (verstr == NULL)
3974 (*_bfd_error_handler)
3975 (_("%B: %s: invalid version %u (max %d)"),
3976 abfd, name, vernum,
3977 elf_tdata (abfd)->cverdefs);
3978 bfd_set_error (bfd_error_bad_value);
3979 goto error_free_vers;
3982 else
3984 /* We cannot simply test for the number of
3985 entries in the VERNEED section since the
3986 numbers for the needed versions do not start
3987 at 0. */
3988 Elf_Internal_Verneed *t;
3990 verstr = NULL;
3991 for (t = elf_tdata (abfd)->verref;
3992 t != NULL;
3993 t = t->vn_nextref)
3995 Elf_Internal_Vernaux *a;
3997 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3999 if (a->vna_other == vernum)
4001 verstr = a->vna_nodename;
4002 break;
4005 if (a != NULL)
4006 break;
4008 if (verstr == NULL)
4010 (*_bfd_error_handler)
4011 (_("%B: %s: invalid needed version %d"),
4012 abfd, name, vernum);
4013 bfd_set_error (bfd_error_bad_value);
4014 goto error_free_vers;
4018 namelen = strlen (name);
4019 verlen = strlen (verstr);
4020 newlen = namelen + verlen + 2;
4021 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4022 && isym->st_shndx != SHN_UNDEF)
4023 ++newlen;
4025 newname = bfd_hash_allocate (&htab->root.table, newlen);
4026 if (newname == NULL)
4027 goto error_free_vers;
4028 memcpy (newname, name, namelen);
4029 p = newname + namelen;
4030 *p++ = ELF_VER_CHR;
4031 /* If this is a defined non-hidden version symbol,
4032 we add another @ to the name. This indicates the
4033 default version of the symbol. */
4034 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4035 && isym->st_shndx != SHN_UNDEF)
4036 *p++ = ELF_VER_CHR;
4037 memcpy (p, verstr, verlen + 1);
4039 name = newname;
4042 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4043 &value, &old_alignment,
4044 sym_hash, &skip, &override,
4045 &type_change_ok, &size_change_ok))
4046 goto error_free_vers;
4048 if (skip)
4049 continue;
4051 if (override)
4052 definition = FALSE;
4054 h = *sym_hash;
4055 while (h->root.type == bfd_link_hash_indirect
4056 || h->root.type == bfd_link_hash_warning)
4057 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4059 /* Remember the old alignment if this is a common symbol, so
4060 that we don't reduce the alignment later on. We can't
4061 check later, because _bfd_generic_link_add_one_symbol
4062 will set a default for the alignment which we want to
4063 override. We also remember the old bfd where the existing
4064 definition comes from. */
4065 switch (h->root.type)
4067 default:
4068 break;
4070 case bfd_link_hash_defined:
4071 case bfd_link_hash_defweak:
4072 old_bfd = h->root.u.def.section->owner;
4073 break;
4075 case bfd_link_hash_common:
4076 old_bfd = h->root.u.c.p->section->owner;
4077 old_alignment = h->root.u.c.p->alignment_power;
4078 break;
4081 if (elf_tdata (abfd)->verdef != NULL
4082 && ! override
4083 && vernum > 1
4084 && definition)
4085 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4088 if (! (_bfd_generic_link_add_one_symbol
4089 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4090 (struct bfd_link_hash_entry **) sym_hash)))
4091 goto error_free_vers;
4093 h = *sym_hash;
4094 while (h->root.type == bfd_link_hash_indirect
4095 || h->root.type == bfd_link_hash_warning)
4096 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4097 *sym_hash = h;
4099 new_weakdef = FALSE;
4100 if (dynamic
4101 && definition
4102 && (flags & BSF_WEAK) != 0
4103 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4104 && is_elf_hash_table (htab)
4105 && h->u.weakdef == NULL)
4107 /* Keep a list of all weak defined non function symbols from
4108 a dynamic object, using the weakdef field. Later in this
4109 function we will set the weakdef field to the correct
4110 value. We only put non-function symbols from dynamic
4111 objects on this list, because that happens to be the only
4112 time we need to know the normal symbol corresponding to a
4113 weak symbol, and the information is time consuming to
4114 figure out. If the weakdef field is not already NULL,
4115 then this symbol was already defined by some previous
4116 dynamic object, and we will be using that previous
4117 definition anyhow. */
4119 h->u.weakdef = weaks;
4120 weaks = h;
4121 new_weakdef = TRUE;
4124 /* Set the alignment of a common symbol. */
4125 if ((common || bfd_is_com_section (sec))
4126 && h->root.type == bfd_link_hash_common)
4128 unsigned int align;
4130 if (common)
4131 align = bfd_log2 (isym->st_value);
4132 else
4134 /* The new symbol is a common symbol in a shared object.
4135 We need to get the alignment from the section. */
4136 align = new_sec->alignment_power;
4138 if (align > old_alignment
4139 /* Permit an alignment power of zero if an alignment of one
4140 is specified and no other alignments have been specified. */
4141 || (isym->st_value == 1 && old_alignment == 0))
4142 h->root.u.c.p->alignment_power = align;
4143 else
4144 h->root.u.c.p->alignment_power = old_alignment;
4147 if (is_elf_hash_table (htab))
4149 bfd_boolean dynsym;
4151 /* Check the alignment when a common symbol is involved. This
4152 can change when a common symbol is overridden by a normal
4153 definition or a common symbol is ignored due to the old
4154 normal definition. We need to make sure the maximum
4155 alignment is maintained. */
4156 if ((old_alignment || common)
4157 && h->root.type != bfd_link_hash_common)
4159 unsigned int common_align;
4160 unsigned int normal_align;
4161 unsigned int symbol_align;
4162 bfd *normal_bfd;
4163 bfd *common_bfd;
4165 symbol_align = ffs (h->root.u.def.value) - 1;
4166 if (h->root.u.def.section->owner != NULL
4167 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4169 normal_align = h->root.u.def.section->alignment_power;
4170 if (normal_align > symbol_align)
4171 normal_align = symbol_align;
4173 else
4174 normal_align = symbol_align;
4176 if (old_alignment)
4178 common_align = old_alignment;
4179 common_bfd = old_bfd;
4180 normal_bfd = abfd;
4182 else
4184 common_align = bfd_log2 (isym->st_value);
4185 common_bfd = abfd;
4186 normal_bfd = old_bfd;
4189 if (normal_align < common_align)
4191 /* PR binutils/2735 */
4192 if (normal_bfd == NULL)
4193 (*_bfd_error_handler)
4194 (_("Warning: alignment %u of common symbol `%s' in %B"
4195 " is greater than the alignment (%u) of its section %A"),
4196 common_bfd, h->root.u.def.section,
4197 1 << common_align, name, 1 << normal_align);
4198 else
4199 (*_bfd_error_handler)
4200 (_("Warning: alignment %u of symbol `%s' in %B"
4201 " is smaller than %u in %B"),
4202 normal_bfd, common_bfd,
4203 1 << normal_align, name, 1 << common_align);
4207 /* Remember the symbol size if it isn't undefined. */
4208 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4209 && (definition || h->size == 0))
4211 if (h->size != 0
4212 && h->size != isym->st_size
4213 && ! size_change_ok)
4214 (*_bfd_error_handler)
4215 (_("Warning: size of symbol `%s' changed"
4216 " from %lu in %B to %lu in %B"),
4217 old_bfd, abfd,
4218 name, (unsigned long) h->size,
4219 (unsigned long) isym->st_size);
4221 h->size = isym->st_size;
4224 /* If this is a common symbol, then we always want H->SIZE
4225 to be the size of the common symbol. The code just above
4226 won't fix the size if a common symbol becomes larger. We
4227 don't warn about a size change here, because that is
4228 covered by --warn-common. Allow changed between different
4229 function types. */
4230 if (h->root.type == bfd_link_hash_common)
4231 h->size = h->root.u.c.size;
4233 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4234 && (definition || h->type == STT_NOTYPE))
4236 if (h->type != STT_NOTYPE
4237 && h->type != ELF_ST_TYPE (isym->st_info)
4238 && ! type_change_ok)
4239 (*_bfd_error_handler)
4240 (_("Warning: type of symbol `%s' changed"
4241 " from %d to %d in %B"),
4242 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4244 h->type = ELF_ST_TYPE (isym->st_info);
4247 /* If st_other has a processor-specific meaning, specific
4248 code might be needed here. We never merge the visibility
4249 attribute with the one from a dynamic object. */
4250 if (bed->elf_backend_merge_symbol_attribute)
4251 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4252 dynamic);
4254 /* If this symbol has default visibility and the user has requested
4255 we not re-export it, then mark it as hidden. */
4256 if (definition && !dynamic
4257 && (abfd->no_export
4258 || (abfd->my_archive && abfd->my_archive->no_export))
4259 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4260 isym->st_other = (STV_HIDDEN
4261 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4263 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4265 unsigned char hvis, symvis, other, nvis;
4267 /* Only merge the visibility. Leave the remainder of the
4268 st_other field to elf_backend_merge_symbol_attribute. */
4269 other = h->other & ~ELF_ST_VISIBILITY (-1);
4271 /* Combine visibilities, using the most constraining one. */
4272 hvis = ELF_ST_VISIBILITY (h->other);
4273 symvis = ELF_ST_VISIBILITY (isym->st_other);
4274 if (! hvis)
4275 nvis = symvis;
4276 else if (! symvis)
4277 nvis = hvis;
4278 else
4279 nvis = hvis < symvis ? hvis : symvis;
4281 h->other = other | nvis;
4284 /* Set a flag in the hash table entry indicating the type of
4285 reference or definition we just found. Keep a count of
4286 the number of dynamic symbols we find. A dynamic symbol
4287 is one which is referenced or defined by both a regular
4288 object and a shared object. */
4289 dynsym = FALSE;
4290 if (! dynamic)
4292 if (! definition)
4294 h->ref_regular = 1;
4295 if (bind != STB_WEAK)
4296 h->ref_regular_nonweak = 1;
4298 else
4299 h->def_regular = 1;
4300 if (! info->executable
4301 || h->def_dynamic
4302 || h->ref_dynamic)
4303 dynsym = TRUE;
4305 else
4307 if (! definition)
4308 h->ref_dynamic = 1;
4309 else
4310 h->def_dynamic = 1;
4311 if (h->def_regular
4312 || h->ref_regular
4313 || (h->u.weakdef != NULL
4314 && ! new_weakdef
4315 && h->u.weakdef->dynindx != -1))
4316 dynsym = TRUE;
4319 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4321 /* We don't want to make debug symbol dynamic. */
4322 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4323 dynsym = FALSE;
4326 /* Check to see if we need to add an indirect symbol for
4327 the default name. */
4328 if (definition || h->root.type == bfd_link_hash_common)
4329 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4330 &sec, &value, &dynsym,
4331 override))
4332 goto error_free_vers;
4334 if (definition && !dynamic)
4336 char *p = strchr (name, ELF_VER_CHR);
4337 if (p != NULL && p[1] != ELF_VER_CHR)
4339 /* Queue non-default versions so that .symver x, x@FOO
4340 aliases can be checked. */
4341 if (!nondeflt_vers)
4343 amt = ((isymend - isym + 1)
4344 * sizeof (struct elf_link_hash_entry *));
4345 nondeflt_vers = bfd_malloc (amt);
4346 if (!nondeflt_vers)
4347 goto error_free_vers;
4349 nondeflt_vers[nondeflt_vers_cnt++] = h;
4353 if (dynsym && h->dynindx == -1)
4355 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4356 goto error_free_vers;
4357 if (h->u.weakdef != NULL
4358 && ! new_weakdef
4359 && h->u.weakdef->dynindx == -1)
4361 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4362 goto error_free_vers;
4365 else if (dynsym && h->dynindx != -1)
4366 /* If the symbol already has a dynamic index, but
4367 visibility says it should not be visible, turn it into
4368 a local symbol. */
4369 switch (ELF_ST_VISIBILITY (h->other))
4371 case STV_INTERNAL:
4372 case STV_HIDDEN:
4373 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4374 dynsym = FALSE;
4375 break;
4378 if (!add_needed
4379 && definition
4380 && dynsym
4381 && h->ref_regular)
4383 int ret;
4384 const char *soname = elf_dt_name (abfd);
4386 /* A symbol from a library loaded via DT_NEEDED of some
4387 other library is referenced by a regular object.
4388 Add a DT_NEEDED entry for it. Issue an error if
4389 --no-add-needed is used. */
4390 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4392 (*_bfd_error_handler)
4393 (_("%s: invalid DSO for symbol `%s' definition"),
4394 abfd, name);
4395 bfd_set_error (bfd_error_bad_value);
4396 goto error_free_vers;
4399 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4401 add_needed = TRUE;
4402 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4403 if (ret < 0)
4404 goto error_free_vers;
4406 BFD_ASSERT (ret == 0);
4411 if (extversym != NULL)
4413 free (extversym);
4414 extversym = NULL;
4417 if (isymbuf != NULL)
4419 free (isymbuf);
4420 isymbuf = NULL;
4423 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4425 unsigned int i;
4427 /* Restore the symbol table. */
4428 if (bed->as_needed_cleanup)
4429 (*bed->as_needed_cleanup) (abfd, info);
4430 old_hash = (char *) old_tab + tabsize;
4431 old_ent = (char *) old_hash + hashsize;
4432 sym_hash = elf_sym_hashes (abfd);
4433 htab->root.table.table = old_table;
4434 htab->root.table.size = old_size;
4435 htab->root.table.count = old_count;
4436 memcpy (htab->root.table.table, old_tab, tabsize);
4437 memcpy (sym_hash, old_hash, hashsize);
4438 htab->root.undefs = old_undefs;
4439 htab->root.undefs_tail = old_undefs_tail;
4440 for (i = 0; i < htab->root.table.size; i++)
4442 struct bfd_hash_entry *p;
4443 struct elf_link_hash_entry *h;
4445 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4447 h = (struct elf_link_hash_entry *) p;
4448 if (h->root.type == bfd_link_hash_warning)
4449 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4450 if (h->dynindx >= old_dynsymcount)
4451 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4453 memcpy (p, old_ent, htab->root.table.entsize);
4454 old_ent = (char *) old_ent + htab->root.table.entsize;
4455 h = (struct elf_link_hash_entry *) p;
4456 if (h->root.type == bfd_link_hash_warning)
4458 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4459 old_ent = (char *) old_ent + htab->root.table.entsize;
4464 /* Make a special call to the linker "notice" function to
4465 tell it that symbols added for crefs may need to be removed. */
4466 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4467 notice_not_needed))
4468 goto error_free_vers;
4470 free (old_tab);
4471 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4472 alloc_mark);
4473 if (nondeflt_vers != NULL)
4474 free (nondeflt_vers);
4475 return TRUE;
4478 if (old_tab != NULL)
4480 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4481 notice_needed))
4482 goto error_free_vers;
4483 free (old_tab);
4484 old_tab = NULL;
4487 /* Now that all the symbols from this input file are created, handle
4488 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4489 if (nondeflt_vers != NULL)
4491 bfd_size_type cnt, symidx;
4493 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4495 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4496 char *shortname, *p;
4498 p = strchr (h->root.root.string, ELF_VER_CHR);
4499 if (p == NULL
4500 || (h->root.type != bfd_link_hash_defined
4501 && h->root.type != bfd_link_hash_defweak))
4502 continue;
4504 amt = p - h->root.root.string;
4505 shortname = bfd_malloc (amt + 1);
4506 if (!shortname)
4507 goto error_free_vers;
4508 memcpy (shortname, h->root.root.string, amt);
4509 shortname[amt] = '\0';
4511 hi = (struct elf_link_hash_entry *)
4512 bfd_link_hash_lookup (&htab->root, shortname,
4513 FALSE, FALSE, FALSE);
4514 if (hi != NULL
4515 && hi->root.type == h->root.type
4516 && hi->root.u.def.value == h->root.u.def.value
4517 && hi->root.u.def.section == h->root.u.def.section)
4519 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4520 hi->root.type = bfd_link_hash_indirect;
4521 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4522 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4523 sym_hash = elf_sym_hashes (abfd);
4524 if (sym_hash)
4525 for (symidx = 0; symidx < extsymcount; ++symidx)
4526 if (sym_hash[symidx] == hi)
4528 sym_hash[symidx] = h;
4529 break;
4532 free (shortname);
4534 free (nondeflt_vers);
4535 nondeflt_vers = NULL;
4538 /* Now set the weakdefs field correctly for all the weak defined
4539 symbols we found. The only way to do this is to search all the
4540 symbols. Since we only need the information for non functions in
4541 dynamic objects, that's the only time we actually put anything on
4542 the list WEAKS. We need this information so that if a regular
4543 object refers to a symbol defined weakly in a dynamic object, the
4544 real symbol in the dynamic object is also put in the dynamic
4545 symbols; we also must arrange for both symbols to point to the
4546 same memory location. We could handle the general case of symbol
4547 aliasing, but a general symbol alias can only be generated in
4548 assembler code, handling it correctly would be very time
4549 consuming, and other ELF linkers don't handle general aliasing
4550 either. */
4551 if (weaks != NULL)
4553 struct elf_link_hash_entry **hpp;
4554 struct elf_link_hash_entry **hppend;
4555 struct elf_link_hash_entry **sorted_sym_hash;
4556 struct elf_link_hash_entry *h;
4557 size_t sym_count;
4559 /* Since we have to search the whole symbol list for each weak
4560 defined symbol, search time for N weak defined symbols will be
4561 O(N^2). Binary search will cut it down to O(NlogN). */
4562 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4563 sorted_sym_hash = bfd_malloc (amt);
4564 if (sorted_sym_hash == NULL)
4565 goto error_return;
4566 sym_hash = sorted_sym_hash;
4567 hpp = elf_sym_hashes (abfd);
4568 hppend = hpp + extsymcount;
4569 sym_count = 0;
4570 for (; hpp < hppend; hpp++)
4572 h = *hpp;
4573 if (h != NULL
4574 && h->root.type == bfd_link_hash_defined
4575 && !bed->is_function_type (h->type))
4577 *sym_hash = h;
4578 sym_hash++;
4579 sym_count++;
4583 qsort (sorted_sym_hash, sym_count,
4584 sizeof (struct elf_link_hash_entry *),
4585 elf_sort_symbol);
4587 while (weaks != NULL)
4589 struct elf_link_hash_entry *hlook;
4590 asection *slook;
4591 bfd_vma vlook;
4592 long ilook;
4593 size_t i, j, idx;
4595 hlook = weaks;
4596 weaks = hlook->u.weakdef;
4597 hlook->u.weakdef = NULL;
4599 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4600 || hlook->root.type == bfd_link_hash_defweak
4601 || hlook->root.type == bfd_link_hash_common
4602 || hlook->root.type == bfd_link_hash_indirect);
4603 slook = hlook->root.u.def.section;
4604 vlook = hlook->root.u.def.value;
4606 ilook = -1;
4607 i = 0;
4608 j = sym_count;
4609 while (i < j)
4611 bfd_signed_vma vdiff;
4612 idx = (i + j) / 2;
4613 h = sorted_sym_hash [idx];
4614 vdiff = vlook - h->root.u.def.value;
4615 if (vdiff < 0)
4616 j = idx;
4617 else if (vdiff > 0)
4618 i = idx + 1;
4619 else
4621 long sdiff = slook->id - h->root.u.def.section->id;
4622 if (sdiff < 0)
4623 j = idx;
4624 else if (sdiff > 0)
4625 i = idx + 1;
4626 else
4628 ilook = idx;
4629 break;
4634 /* We didn't find a value/section match. */
4635 if (ilook == -1)
4636 continue;
4638 for (i = ilook; i < sym_count; i++)
4640 h = sorted_sym_hash [i];
4642 /* Stop if value or section doesn't match. */
4643 if (h->root.u.def.value != vlook
4644 || h->root.u.def.section != slook)
4645 break;
4646 else if (h != hlook)
4648 hlook->u.weakdef = h;
4650 /* If the weak definition is in the list of dynamic
4651 symbols, make sure the real definition is put
4652 there as well. */
4653 if (hlook->dynindx != -1 && h->dynindx == -1)
4655 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4657 err_free_sym_hash:
4658 free (sorted_sym_hash);
4659 goto error_return;
4663 /* If the real definition is in the list of dynamic
4664 symbols, make sure the weak definition is put
4665 there as well. If we don't do this, then the
4666 dynamic loader might not merge the entries for the
4667 real definition and the weak definition. */
4668 if (h->dynindx != -1 && hlook->dynindx == -1)
4670 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4671 goto err_free_sym_hash;
4673 break;
4678 free (sorted_sym_hash);
4681 if (bed->check_directives
4682 && !(*bed->check_directives) (abfd, info))
4683 return FALSE;
4685 /* If this object is the same format as the output object, and it is
4686 not a shared library, then let the backend look through the
4687 relocs.
4689 This is required to build global offset table entries and to
4690 arrange for dynamic relocs. It is not required for the
4691 particular common case of linking non PIC code, even when linking
4692 against shared libraries, but unfortunately there is no way of
4693 knowing whether an object file has been compiled PIC or not.
4694 Looking through the relocs is not particularly time consuming.
4695 The problem is that we must either (1) keep the relocs in memory,
4696 which causes the linker to require additional runtime memory or
4697 (2) read the relocs twice from the input file, which wastes time.
4698 This would be a good case for using mmap.
4700 I have no idea how to handle linking PIC code into a file of a
4701 different format. It probably can't be done. */
4702 if (! dynamic
4703 && is_elf_hash_table (htab)
4704 && bed->check_relocs != NULL
4705 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4707 asection *o;
4709 for (o = abfd->sections; o != NULL; o = o->next)
4711 Elf_Internal_Rela *internal_relocs;
4712 bfd_boolean ok;
4714 if ((o->flags & SEC_RELOC) == 0
4715 || o->reloc_count == 0
4716 || ((info->strip == strip_all || info->strip == strip_debugger)
4717 && (o->flags & SEC_DEBUGGING) != 0)
4718 || bfd_is_abs_section (o->output_section))
4719 continue;
4721 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4722 info->keep_memory);
4723 if (internal_relocs == NULL)
4724 goto error_return;
4726 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4728 if (elf_section_data (o)->relocs != internal_relocs)
4729 free (internal_relocs);
4731 if (! ok)
4732 goto error_return;
4736 /* If this is a non-traditional link, try to optimize the handling
4737 of the .stab/.stabstr sections. */
4738 if (! dynamic
4739 && ! info->traditional_format
4740 && is_elf_hash_table (htab)
4741 && (info->strip != strip_all && info->strip != strip_debugger))
4743 asection *stabstr;
4745 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4746 if (stabstr != NULL)
4748 bfd_size_type string_offset = 0;
4749 asection *stab;
4751 for (stab = abfd->sections; stab; stab = stab->next)
4752 if (CONST_STRNEQ (stab->name, ".stab")
4753 && (!stab->name[5] ||
4754 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4755 && (stab->flags & SEC_MERGE) == 0
4756 && !bfd_is_abs_section (stab->output_section))
4758 struct bfd_elf_section_data *secdata;
4760 secdata = elf_section_data (stab);
4761 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4762 stabstr, &secdata->sec_info,
4763 &string_offset))
4764 goto error_return;
4765 if (secdata->sec_info)
4766 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4771 if (is_elf_hash_table (htab) && add_needed)
4773 /* Add this bfd to the loaded list. */
4774 struct elf_link_loaded_list *n;
4776 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4777 if (n == NULL)
4778 goto error_return;
4779 n->abfd = abfd;
4780 n->next = htab->loaded;
4781 htab->loaded = n;
4784 return TRUE;
4786 error_free_vers:
4787 if (old_tab != NULL)
4788 free (old_tab);
4789 if (nondeflt_vers != NULL)
4790 free (nondeflt_vers);
4791 if (extversym != NULL)
4792 free (extversym);
4793 error_free_sym:
4794 if (isymbuf != NULL)
4795 free (isymbuf);
4796 error_return:
4797 return FALSE;
4800 /* Return the linker hash table entry of a symbol that might be
4801 satisfied by an archive symbol. Return -1 on error. */
4803 struct elf_link_hash_entry *
4804 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4805 struct bfd_link_info *info,
4806 const char *name)
4808 struct elf_link_hash_entry *h;
4809 char *p, *copy;
4810 size_t len, first;
4812 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4813 if (h != NULL)
4814 return h;
4816 /* If this is a default version (the name contains @@), look up the
4817 symbol again with only one `@' as well as without the version.
4818 The effect is that references to the symbol with and without the
4819 version will be matched by the default symbol in the archive. */
4821 p = strchr (name, ELF_VER_CHR);
4822 if (p == NULL || p[1] != ELF_VER_CHR)
4823 return h;
4825 /* First check with only one `@'. */
4826 len = strlen (name);
4827 copy = bfd_alloc (abfd, len);
4828 if (copy == NULL)
4829 return (struct elf_link_hash_entry *) 0 - 1;
4831 first = p - name + 1;
4832 memcpy (copy, name, first);
4833 memcpy (copy + first, name + first + 1, len - first);
4835 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4836 if (h == NULL)
4838 /* We also need to check references to the symbol without the
4839 version. */
4840 copy[first - 1] = '\0';
4841 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4842 FALSE, FALSE, FALSE);
4845 bfd_release (abfd, copy);
4846 return h;
4849 /* Add symbols from an ELF archive file to the linker hash table. We
4850 don't use _bfd_generic_link_add_archive_symbols because of a
4851 problem which arises on UnixWare. The UnixWare libc.so is an
4852 archive which includes an entry libc.so.1 which defines a bunch of
4853 symbols. The libc.so archive also includes a number of other
4854 object files, which also define symbols, some of which are the same
4855 as those defined in libc.so.1. Correct linking requires that we
4856 consider each object file in turn, and include it if it defines any
4857 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4858 this; it looks through the list of undefined symbols, and includes
4859 any object file which defines them. When this algorithm is used on
4860 UnixWare, it winds up pulling in libc.so.1 early and defining a
4861 bunch of symbols. This means that some of the other objects in the
4862 archive are not included in the link, which is incorrect since they
4863 precede libc.so.1 in the archive.
4865 Fortunately, ELF archive handling is simpler than that done by
4866 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4867 oddities. In ELF, if we find a symbol in the archive map, and the
4868 symbol is currently undefined, we know that we must pull in that
4869 object file.
4871 Unfortunately, we do have to make multiple passes over the symbol
4872 table until nothing further is resolved. */
4874 static bfd_boolean
4875 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4877 symindex c;
4878 bfd_boolean *defined = NULL;
4879 bfd_boolean *included = NULL;
4880 carsym *symdefs;
4881 bfd_boolean loop;
4882 bfd_size_type amt;
4883 const struct elf_backend_data *bed;
4884 struct elf_link_hash_entry * (*archive_symbol_lookup)
4885 (bfd *, struct bfd_link_info *, const char *);
4887 if (! bfd_has_map (abfd))
4889 /* An empty archive is a special case. */
4890 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4891 return TRUE;
4892 bfd_set_error (bfd_error_no_armap);
4893 return FALSE;
4896 /* Keep track of all symbols we know to be already defined, and all
4897 files we know to be already included. This is to speed up the
4898 second and subsequent passes. */
4899 c = bfd_ardata (abfd)->symdef_count;
4900 if (c == 0)
4901 return TRUE;
4902 amt = c;
4903 amt *= sizeof (bfd_boolean);
4904 defined = bfd_zmalloc (amt);
4905 included = bfd_zmalloc (amt);
4906 if (defined == NULL || included == NULL)
4907 goto error_return;
4909 symdefs = bfd_ardata (abfd)->symdefs;
4910 bed = get_elf_backend_data (abfd);
4911 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4915 file_ptr last;
4916 symindex i;
4917 carsym *symdef;
4918 carsym *symdefend;
4920 loop = FALSE;
4921 last = -1;
4923 symdef = symdefs;
4924 symdefend = symdef + c;
4925 for (i = 0; symdef < symdefend; symdef++, i++)
4927 struct elf_link_hash_entry *h;
4928 bfd *element;
4929 struct bfd_link_hash_entry *undefs_tail;
4930 symindex mark;
4932 if (defined[i] || included[i])
4933 continue;
4934 if (symdef->file_offset == last)
4936 included[i] = TRUE;
4937 continue;
4940 h = archive_symbol_lookup (abfd, info, symdef->name);
4941 if (h == (struct elf_link_hash_entry *) 0 - 1)
4942 goto error_return;
4944 if (h == NULL)
4945 continue;
4947 if (h->root.type == bfd_link_hash_common)
4949 /* We currently have a common symbol. The archive map contains
4950 a reference to this symbol, so we may want to include it. We
4951 only want to include it however, if this archive element
4952 contains a definition of the symbol, not just another common
4953 declaration of it.
4955 Unfortunately some archivers (including GNU ar) will put
4956 declarations of common symbols into their archive maps, as
4957 well as real definitions, so we cannot just go by the archive
4958 map alone. Instead we must read in the element's symbol
4959 table and check that to see what kind of symbol definition
4960 this is. */
4961 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4962 continue;
4964 else if (h->root.type != bfd_link_hash_undefined)
4966 if (h->root.type != bfd_link_hash_undefweak)
4967 defined[i] = TRUE;
4968 continue;
4971 /* We need to include this archive member. */
4972 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4973 if (element == NULL)
4974 goto error_return;
4976 if (! bfd_check_format (element, bfd_object))
4977 goto error_return;
4979 /* Doublecheck that we have not included this object
4980 already--it should be impossible, but there may be
4981 something wrong with the archive. */
4982 if (element->archive_pass != 0)
4984 bfd_set_error (bfd_error_bad_value);
4985 goto error_return;
4987 element->archive_pass = 1;
4989 undefs_tail = info->hash->undefs_tail;
4991 if (! (*info->callbacks->add_archive_element) (info, element,
4992 symdef->name))
4993 goto error_return;
4994 if (! bfd_link_add_symbols (element, info))
4995 goto error_return;
4997 /* If there are any new undefined symbols, we need to make
4998 another pass through the archive in order to see whether
4999 they can be defined. FIXME: This isn't perfect, because
5000 common symbols wind up on undefs_tail and because an
5001 undefined symbol which is defined later on in this pass
5002 does not require another pass. This isn't a bug, but it
5003 does make the code less efficient than it could be. */
5004 if (undefs_tail != info->hash->undefs_tail)
5005 loop = TRUE;
5007 /* Look backward to mark all symbols from this object file
5008 which we have already seen in this pass. */
5009 mark = i;
5012 included[mark] = TRUE;
5013 if (mark == 0)
5014 break;
5015 --mark;
5017 while (symdefs[mark].file_offset == symdef->file_offset);
5019 /* We mark subsequent symbols from this object file as we go
5020 on through the loop. */
5021 last = symdef->file_offset;
5024 while (loop);
5026 free (defined);
5027 free (included);
5029 return TRUE;
5031 error_return:
5032 if (defined != NULL)
5033 free (defined);
5034 if (included != NULL)
5035 free (included);
5036 return FALSE;
5039 /* Given an ELF BFD, add symbols to the global hash table as
5040 appropriate. */
5042 bfd_boolean
5043 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5045 switch (bfd_get_format (abfd))
5047 case bfd_object:
5048 return elf_link_add_object_symbols (abfd, info);
5049 case bfd_archive:
5050 return elf_link_add_archive_symbols (abfd, info);
5051 default:
5052 bfd_set_error (bfd_error_wrong_format);
5053 return FALSE;
5057 struct hash_codes_info
5059 unsigned long *hashcodes;
5060 bfd_boolean error;
5063 /* This function will be called though elf_link_hash_traverse to store
5064 all hash value of the exported symbols in an array. */
5066 static bfd_boolean
5067 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5069 struct hash_codes_info *inf = data;
5070 const char *name;
5071 char *p;
5072 unsigned long ha;
5073 char *alc = NULL;
5075 if (h->root.type == bfd_link_hash_warning)
5076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5078 /* Ignore indirect symbols. These are added by the versioning code. */
5079 if (h->dynindx == -1)
5080 return TRUE;
5082 name = h->root.root.string;
5083 p = strchr (name, ELF_VER_CHR);
5084 if (p != NULL)
5086 alc = bfd_malloc (p - name + 1);
5087 if (alc == NULL)
5089 inf->error = TRUE;
5090 return FALSE;
5092 memcpy (alc, name, p - name);
5093 alc[p - name] = '\0';
5094 name = alc;
5097 /* Compute the hash value. */
5098 ha = bfd_elf_hash (name);
5100 /* Store the found hash value in the array given as the argument. */
5101 *(inf->hashcodes)++ = ha;
5103 /* And store it in the struct so that we can put it in the hash table
5104 later. */
5105 h->u.elf_hash_value = ha;
5107 if (alc != NULL)
5108 free (alc);
5110 return TRUE;
5113 struct collect_gnu_hash_codes
5115 bfd *output_bfd;
5116 const struct elf_backend_data *bed;
5117 unsigned long int nsyms;
5118 unsigned long int maskbits;
5119 unsigned long int *hashcodes;
5120 unsigned long int *hashval;
5121 unsigned long int *indx;
5122 unsigned long int *counts;
5123 bfd_vma *bitmask;
5124 bfd_byte *contents;
5125 long int min_dynindx;
5126 unsigned long int bucketcount;
5127 unsigned long int symindx;
5128 long int local_indx;
5129 long int shift1, shift2;
5130 unsigned long int mask;
5131 bfd_boolean error;
5134 /* This function will be called though elf_link_hash_traverse to store
5135 all hash value of the exported symbols in an array. */
5137 static bfd_boolean
5138 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5140 struct collect_gnu_hash_codes *s = data;
5141 const char *name;
5142 char *p;
5143 unsigned long ha;
5144 char *alc = NULL;
5146 if (h->root.type == bfd_link_hash_warning)
5147 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5149 /* Ignore indirect symbols. These are added by the versioning code. */
5150 if (h->dynindx == -1)
5151 return TRUE;
5153 /* Ignore also local symbols and undefined symbols. */
5154 if (! (*s->bed->elf_hash_symbol) (h))
5155 return TRUE;
5157 name = h->root.root.string;
5158 p = strchr (name, ELF_VER_CHR);
5159 if (p != NULL)
5161 alc = bfd_malloc (p - name + 1);
5162 if (alc == NULL)
5164 s->error = TRUE;
5165 return FALSE;
5167 memcpy (alc, name, p - name);
5168 alc[p - name] = '\0';
5169 name = alc;
5172 /* Compute the hash value. */
5173 ha = bfd_elf_gnu_hash (name);
5175 /* Store the found hash value in the array for compute_bucket_count,
5176 and also for .dynsym reordering purposes. */
5177 s->hashcodes[s->nsyms] = ha;
5178 s->hashval[h->dynindx] = ha;
5179 ++s->nsyms;
5180 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5181 s->min_dynindx = h->dynindx;
5183 if (alc != NULL)
5184 free (alc);
5186 return TRUE;
5189 /* This function will be called though elf_link_hash_traverse to do
5190 final dynaminc symbol renumbering. */
5192 static bfd_boolean
5193 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5195 struct collect_gnu_hash_codes *s = data;
5196 unsigned long int bucket;
5197 unsigned long int val;
5199 if (h->root.type == bfd_link_hash_warning)
5200 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5202 /* Ignore indirect symbols. */
5203 if (h->dynindx == -1)
5204 return TRUE;
5206 /* Ignore also local symbols and undefined symbols. */
5207 if (! (*s->bed->elf_hash_symbol) (h))
5209 if (h->dynindx >= s->min_dynindx)
5210 h->dynindx = s->local_indx++;
5211 return TRUE;
5214 bucket = s->hashval[h->dynindx] % s->bucketcount;
5215 val = (s->hashval[h->dynindx] >> s->shift1)
5216 & ((s->maskbits >> s->shift1) - 1);
5217 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5218 s->bitmask[val]
5219 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5220 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5221 if (s->counts[bucket] == 1)
5222 /* Last element terminates the chain. */
5223 val |= 1;
5224 bfd_put_32 (s->output_bfd, val,
5225 s->contents + (s->indx[bucket] - s->symindx) * 4);
5226 --s->counts[bucket];
5227 h->dynindx = s->indx[bucket]++;
5228 return TRUE;
5231 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5233 bfd_boolean
5234 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5236 return !(h->forced_local
5237 || h->root.type == bfd_link_hash_undefined
5238 || h->root.type == bfd_link_hash_undefweak
5239 || ((h->root.type == bfd_link_hash_defined
5240 || h->root.type == bfd_link_hash_defweak)
5241 && h->root.u.def.section->output_section == NULL));
5244 /* Array used to determine the number of hash table buckets to use
5245 based on the number of symbols there are. If there are fewer than
5246 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5247 fewer than 37 we use 17 buckets, and so forth. We never use more
5248 than 32771 buckets. */
5250 static const size_t elf_buckets[] =
5252 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5253 16411, 32771, 0
5256 /* Compute bucket count for hashing table. We do not use a static set
5257 of possible tables sizes anymore. Instead we determine for all
5258 possible reasonable sizes of the table the outcome (i.e., the
5259 number of collisions etc) and choose the best solution. The
5260 weighting functions are not too simple to allow the table to grow
5261 without bounds. Instead one of the weighting factors is the size.
5262 Therefore the result is always a good payoff between few collisions
5263 (= short chain lengths) and table size. */
5264 static size_t
5265 compute_bucket_count (struct bfd_link_info *info,
5266 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5267 unsigned long int nsyms,
5268 int gnu_hash)
5270 size_t best_size = 0;
5271 unsigned long int i;
5273 /* We have a problem here. The following code to optimize the table
5274 size requires an integer type with more the 32 bits. If
5275 BFD_HOST_U_64_BIT is set we know about such a type. */
5276 #ifdef BFD_HOST_U_64_BIT
5277 if (info->optimize)
5279 size_t minsize;
5280 size_t maxsize;
5281 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5282 bfd *dynobj = elf_hash_table (info)->dynobj;
5283 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5284 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5285 unsigned long int *counts;
5286 bfd_size_type amt;
5288 /* Possible optimization parameters: if we have NSYMS symbols we say
5289 that the hashing table must at least have NSYMS/4 and at most
5290 2*NSYMS buckets. */
5291 minsize = nsyms / 4;
5292 if (minsize == 0)
5293 minsize = 1;
5294 best_size = maxsize = nsyms * 2;
5295 if (gnu_hash)
5297 if (minsize < 2)
5298 minsize = 2;
5299 if ((best_size & 31) == 0)
5300 ++best_size;
5303 /* Create array where we count the collisions in. We must use bfd_malloc
5304 since the size could be large. */
5305 amt = maxsize;
5306 amt *= sizeof (unsigned long int);
5307 counts = bfd_malloc (amt);
5308 if (counts == NULL)
5309 return 0;
5311 /* Compute the "optimal" size for the hash table. The criteria is a
5312 minimal chain length. The minor criteria is (of course) the size
5313 of the table. */
5314 for (i = minsize; i < maxsize; ++i)
5316 /* Walk through the array of hashcodes and count the collisions. */
5317 BFD_HOST_U_64_BIT max;
5318 unsigned long int j;
5319 unsigned long int fact;
5321 if (gnu_hash && (i & 31) == 0)
5322 continue;
5324 memset (counts, '\0', i * sizeof (unsigned long int));
5326 /* Determine how often each hash bucket is used. */
5327 for (j = 0; j < nsyms; ++j)
5328 ++counts[hashcodes[j] % i];
5330 /* For the weight function we need some information about the
5331 pagesize on the target. This is information need not be 100%
5332 accurate. Since this information is not available (so far) we
5333 define it here to a reasonable default value. If it is crucial
5334 to have a better value some day simply define this value. */
5335 # ifndef BFD_TARGET_PAGESIZE
5336 # define BFD_TARGET_PAGESIZE (4096)
5337 # endif
5339 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5340 and the chains. */
5341 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5343 # if 1
5344 /* Variant 1: optimize for short chains. We add the squares
5345 of all the chain lengths (which favors many small chain
5346 over a few long chains). */
5347 for (j = 0; j < i; ++j)
5348 max += counts[j] * counts[j];
5350 /* This adds penalties for the overall size of the table. */
5351 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5352 max *= fact * fact;
5353 # else
5354 /* Variant 2: Optimize a lot more for small table. Here we
5355 also add squares of the size but we also add penalties for
5356 empty slots (the +1 term). */
5357 for (j = 0; j < i; ++j)
5358 max += (1 + counts[j]) * (1 + counts[j]);
5360 /* The overall size of the table is considered, but not as
5361 strong as in variant 1, where it is squared. */
5362 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5363 max *= fact;
5364 # endif
5366 /* Compare with current best results. */
5367 if (max < best_chlen)
5369 best_chlen = max;
5370 best_size = i;
5374 free (counts);
5376 else
5377 #endif /* defined (BFD_HOST_U_64_BIT) */
5379 /* This is the fallback solution if no 64bit type is available or if we
5380 are not supposed to spend much time on optimizations. We select the
5381 bucket count using a fixed set of numbers. */
5382 for (i = 0; elf_buckets[i] != 0; i++)
5384 best_size = elf_buckets[i];
5385 if (nsyms < elf_buckets[i + 1])
5386 break;
5388 if (gnu_hash && best_size < 2)
5389 best_size = 2;
5392 return best_size;
5395 /* Set up the sizes and contents of the ELF dynamic sections. This is
5396 called by the ELF linker emulation before_allocation routine. We
5397 must set the sizes of the sections before the linker sets the
5398 addresses of the various sections. */
5400 bfd_boolean
5401 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5402 const char *soname,
5403 const char *rpath,
5404 const char *filter_shlib,
5405 const char * const *auxiliary_filters,
5406 struct bfd_link_info *info,
5407 asection **sinterpptr,
5408 struct bfd_elf_version_tree *verdefs)
5410 bfd_size_type soname_indx;
5411 bfd *dynobj;
5412 const struct elf_backend_data *bed;
5413 struct elf_assign_sym_version_info asvinfo;
5415 *sinterpptr = NULL;
5417 soname_indx = (bfd_size_type) -1;
5419 if (!is_elf_hash_table (info->hash))
5420 return TRUE;
5422 bed = get_elf_backend_data (output_bfd);
5423 if (info->execstack)
5424 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5425 else if (info->noexecstack)
5426 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5427 else
5429 bfd *inputobj;
5430 asection *notesec = NULL;
5431 int exec = 0;
5433 for (inputobj = info->input_bfds;
5434 inputobj;
5435 inputobj = inputobj->link_next)
5437 asection *s;
5439 if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5440 continue;
5441 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5442 if (s)
5444 if (s->flags & SEC_CODE)
5445 exec = PF_X;
5446 notesec = s;
5448 else if (bed->default_execstack)
5449 exec = PF_X;
5451 if (notesec)
5453 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5454 if (exec && info->relocatable
5455 && notesec->output_section != bfd_abs_section_ptr)
5456 notesec->output_section->flags |= SEC_CODE;
5460 /* Any syms created from now on start with -1 in
5461 got.refcount/offset and plt.refcount/offset. */
5462 elf_hash_table (info)->init_got_refcount
5463 = elf_hash_table (info)->init_got_offset;
5464 elf_hash_table (info)->init_plt_refcount
5465 = elf_hash_table (info)->init_plt_offset;
5467 /* The backend may have to create some sections regardless of whether
5468 we're dynamic or not. */
5469 if (bed->elf_backend_always_size_sections
5470 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5471 return FALSE;
5473 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5474 return FALSE;
5476 dynobj = elf_hash_table (info)->dynobj;
5478 /* If there were no dynamic objects in the link, there is nothing to
5479 do here. */
5480 if (dynobj == NULL)
5481 return TRUE;
5483 if (elf_hash_table (info)->dynamic_sections_created)
5485 struct elf_info_failed eif;
5486 struct elf_link_hash_entry *h;
5487 asection *dynstr;
5488 struct bfd_elf_version_tree *t;
5489 struct bfd_elf_version_expr *d;
5490 asection *s;
5491 bfd_boolean all_defined;
5493 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5494 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5496 if (soname != NULL)
5498 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5499 soname, TRUE);
5500 if (soname_indx == (bfd_size_type) -1
5501 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5502 return FALSE;
5505 if (info->symbolic)
5507 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5508 return FALSE;
5509 info->flags |= DF_SYMBOLIC;
5512 if (rpath != NULL)
5514 bfd_size_type indx;
5516 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5517 TRUE);
5518 if (indx == (bfd_size_type) -1
5519 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5520 return FALSE;
5522 if (info->new_dtags)
5524 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5525 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5526 return FALSE;
5530 if (filter_shlib != NULL)
5532 bfd_size_type indx;
5534 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5535 filter_shlib, TRUE);
5536 if (indx == (bfd_size_type) -1
5537 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5538 return FALSE;
5541 if (auxiliary_filters != NULL)
5543 const char * const *p;
5545 for (p = auxiliary_filters; *p != NULL; p++)
5547 bfd_size_type indx;
5549 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5550 *p, TRUE);
5551 if (indx == (bfd_size_type) -1
5552 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5553 return FALSE;
5557 eif.info = info;
5558 eif.verdefs = verdefs;
5559 eif.failed = FALSE;
5561 /* If we are supposed to export all symbols into the dynamic symbol
5562 table (this is not the normal case), then do so. */
5563 if (info->export_dynamic
5564 || (info->executable && info->dynamic))
5566 elf_link_hash_traverse (elf_hash_table (info),
5567 _bfd_elf_export_symbol,
5568 &eif);
5569 if (eif.failed)
5570 return FALSE;
5573 /* Make all global versions with definition. */
5574 for (t = verdefs; t != NULL; t = t->next)
5575 for (d = t->globals.list; d != NULL; d = d->next)
5576 if (!d->symver && d->literal)
5578 const char *verstr, *name;
5579 size_t namelen, verlen, newlen;
5580 char *newname, *p;
5581 struct elf_link_hash_entry *newh;
5583 name = d->pattern;
5584 namelen = strlen (name);
5585 verstr = t->name;
5586 verlen = strlen (verstr);
5587 newlen = namelen + verlen + 3;
5589 newname = bfd_malloc (newlen);
5590 if (newname == NULL)
5591 return FALSE;
5592 memcpy (newname, name, namelen);
5594 /* Check the hidden versioned definition. */
5595 p = newname + namelen;
5596 *p++ = ELF_VER_CHR;
5597 memcpy (p, verstr, verlen + 1);
5598 newh = elf_link_hash_lookup (elf_hash_table (info),
5599 newname, FALSE, FALSE,
5600 FALSE);
5601 if (newh == NULL
5602 || (newh->root.type != bfd_link_hash_defined
5603 && newh->root.type != bfd_link_hash_defweak))
5605 /* Check the default versioned definition. */
5606 *p++ = ELF_VER_CHR;
5607 memcpy (p, verstr, verlen + 1);
5608 newh = elf_link_hash_lookup (elf_hash_table (info),
5609 newname, FALSE, FALSE,
5610 FALSE);
5612 free (newname);
5614 /* Mark this version if there is a definition and it is
5615 not defined in a shared object. */
5616 if (newh != NULL
5617 && !newh->def_dynamic
5618 && (newh->root.type == bfd_link_hash_defined
5619 || newh->root.type == bfd_link_hash_defweak))
5620 d->symver = 1;
5623 /* Attach all the symbols to their version information. */
5624 asvinfo.output_bfd = output_bfd;
5625 asvinfo.info = info;
5626 asvinfo.verdefs = verdefs;
5627 asvinfo.failed = FALSE;
5629 elf_link_hash_traverse (elf_hash_table (info),
5630 _bfd_elf_link_assign_sym_version,
5631 &asvinfo);
5632 if (asvinfo.failed)
5633 return FALSE;
5635 if (!info->allow_undefined_version)
5637 /* Check if all global versions have a definition. */
5638 all_defined = TRUE;
5639 for (t = verdefs; t != NULL; t = t->next)
5640 for (d = t->globals.list; d != NULL; d = d->next)
5641 if (d->literal && !d->symver && !d->script)
5643 (*_bfd_error_handler)
5644 (_("%s: undefined version: %s"),
5645 d->pattern, t->name);
5646 all_defined = FALSE;
5649 if (!all_defined)
5651 bfd_set_error (bfd_error_bad_value);
5652 return FALSE;
5656 /* Find all symbols which were defined in a dynamic object and make
5657 the backend pick a reasonable value for them. */
5658 elf_link_hash_traverse (elf_hash_table (info),
5659 _bfd_elf_adjust_dynamic_symbol,
5660 &eif);
5661 if (eif.failed)
5662 return FALSE;
5664 /* Add some entries to the .dynamic section. We fill in some of the
5665 values later, in bfd_elf_final_link, but we must add the entries
5666 now so that we know the final size of the .dynamic section. */
5668 /* If there are initialization and/or finalization functions to
5669 call then add the corresponding DT_INIT/DT_FINI entries. */
5670 h = (info->init_function
5671 ? elf_link_hash_lookup (elf_hash_table (info),
5672 info->init_function, FALSE,
5673 FALSE, FALSE)
5674 : NULL);
5675 if (h != NULL
5676 && (h->ref_regular
5677 || h->def_regular))
5679 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5680 return FALSE;
5682 h = (info->fini_function
5683 ? elf_link_hash_lookup (elf_hash_table (info),
5684 info->fini_function, FALSE,
5685 FALSE, FALSE)
5686 : NULL);
5687 if (h != NULL
5688 && (h->ref_regular
5689 || h->def_regular))
5691 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5692 return FALSE;
5695 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5696 if (s != NULL && s->linker_has_input)
5698 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5699 if (! info->executable)
5701 bfd *sub;
5702 asection *o;
5704 for (sub = info->input_bfds; sub != NULL;
5705 sub = sub->link_next)
5706 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5707 for (o = sub->sections; o != NULL; o = o->next)
5708 if (elf_section_data (o)->this_hdr.sh_type
5709 == SHT_PREINIT_ARRAY)
5711 (*_bfd_error_handler)
5712 (_("%B: .preinit_array section is not allowed in DSO"),
5713 sub);
5714 break;
5717 bfd_set_error (bfd_error_nonrepresentable_section);
5718 return FALSE;
5721 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5722 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5723 return FALSE;
5725 s = bfd_get_section_by_name (output_bfd, ".init_array");
5726 if (s != NULL && s->linker_has_input)
5728 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5729 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5730 return FALSE;
5732 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5733 if (s != NULL && s->linker_has_input)
5735 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5736 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5737 return FALSE;
5740 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5741 /* If .dynstr is excluded from the link, we don't want any of
5742 these tags. Strictly, we should be checking each section
5743 individually; This quick check covers for the case where
5744 someone does a /DISCARD/ : { *(*) }. */
5745 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5747 bfd_size_type strsize;
5749 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5750 if ((info->emit_hash
5751 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5752 || (info->emit_gnu_hash
5753 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5754 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5755 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5756 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5757 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5758 bed->s->sizeof_sym))
5759 return FALSE;
5763 /* The backend must work out the sizes of all the other dynamic
5764 sections. */
5765 if (bed->elf_backend_size_dynamic_sections
5766 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5767 return FALSE;
5769 if (elf_hash_table (info)->dynamic_sections_created)
5771 unsigned long section_sym_count;
5772 asection *s;
5774 /* Set up the version definition section. */
5775 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5776 BFD_ASSERT (s != NULL);
5778 /* We may have created additional version definitions if we are
5779 just linking a regular application. */
5780 verdefs = asvinfo.verdefs;
5782 /* Skip anonymous version tag. */
5783 if (verdefs != NULL && verdefs->vernum == 0)
5784 verdefs = verdefs->next;
5786 if (verdefs == NULL && !info->create_default_symver)
5787 s->flags |= SEC_EXCLUDE;
5788 else
5790 unsigned int cdefs;
5791 bfd_size_type size;
5792 struct bfd_elf_version_tree *t;
5793 bfd_byte *p;
5794 Elf_Internal_Verdef def;
5795 Elf_Internal_Verdaux defaux;
5796 struct bfd_link_hash_entry *bh;
5797 struct elf_link_hash_entry *h;
5798 const char *name;
5800 cdefs = 0;
5801 size = 0;
5803 /* Make space for the base version. */
5804 size += sizeof (Elf_External_Verdef);
5805 size += sizeof (Elf_External_Verdaux);
5806 ++cdefs;
5808 /* Make space for the default version. */
5809 if (info->create_default_symver)
5811 size += sizeof (Elf_External_Verdef);
5812 ++cdefs;
5815 for (t = verdefs; t != NULL; t = t->next)
5817 struct bfd_elf_version_deps *n;
5819 size += sizeof (Elf_External_Verdef);
5820 size += sizeof (Elf_External_Verdaux);
5821 ++cdefs;
5823 for (n = t->deps; n != NULL; n = n->next)
5824 size += sizeof (Elf_External_Verdaux);
5827 s->size = size;
5828 s->contents = bfd_alloc (output_bfd, s->size);
5829 if (s->contents == NULL && s->size != 0)
5830 return FALSE;
5832 /* Fill in the version definition section. */
5834 p = s->contents;
5836 def.vd_version = VER_DEF_CURRENT;
5837 def.vd_flags = VER_FLG_BASE;
5838 def.vd_ndx = 1;
5839 def.vd_cnt = 1;
5840 if (info->create_default_symver)
5842 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5843 def.vd_next = sizeof (Elf_External_Verdef);
5845 else
5847 def.vd_aux = sizeof (Elf_External_Verdef);
5848 def.vd_next = (sizeof (Elf_External_Verdef)
5849 + sizeof (Elf_External_Verdaux));
5852 if (soname_indx != (bfd_size_type) -1)
5854 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5855 soname_indx);
5856 def.vd_hash = bfd_elf_hash (soname);
5857 defaux.vda_name = soname_indx;
5858 name = soname;
5860 else
5862 bfd_size_type indx;
5864 name = lbasename (output_bfd->filename);
5865 def.vd_hash = bfd_elf_hash (name);
5866 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5867 name, FALSE);
5868 if (indx == (bfd_size_type) -1)
5869 return FALSE;
5870 defaux.vda_name = indx;
5872 defaux.vda_next = 0;
5874 _bfd_elf_swap_verdef_out (output_bfd, &def,
5875 (Elf_External_Verdef *) p);
5876 p += sizeof (Elf_External_Verdef);
5877 if (info->create_default_symver)
5879 /* Add a symbol representing this version. */
5880 bh = NULL;
5881 if (! (_bfd_generic_link_add_one_symbol
5882 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5883 0, NULL, FALSE,
5884 get_elf_backend_data (dynobj)->collect, &bh)))
5885 return FALSE;
5886 h = (struct elf_link_hash_entry *) bh;
5887 h->non_elf = 0;
5888 h->def_regular = 1;
5889 h->type = STT_OBJECT;
5890 h->verinfo.vertree = NULL;
5892 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5893 return FALSE;
5895 /* Create a duplicate of the base version with the same
5896 aux block, but different flags. */
5897 def.vd_flags = 0;
5898 def.vd_ndx = 2;
5899 def.vd_aux = sizeof (Elf_External_Verdef);
5900 if (verdefs)
5901 def.vd_next = (sizeof (Elf_External_Verdef)
5902 + sizeof (Elf_External_Verdaux));
5903 else
5904 def.vd_next = 0;
5905 _bfd_elf_swap_verdef_out (output_bfd, &def,
5906 (Elf_External_Verdef *) p);
5907 p += sizeof (Elf_External_Verdef);
5909 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5910 (Elf_External_Verdaux *) p);
5911 p += sizeof (Elf_External_Verdaux);
5913 for (t = verdefs; t != NULL; t = t->next)
5915 unsigned int cdeps;
5916 struct bfd_elf_version_deps *n;
5918 cdeps = 0;
5919 for (n = t->deps; n != NULL; n = n->next)
5920 ++cdeps;
5922 /* Add a symbol representing this version. */
5923 bh = NULL;
5924 if (! (_bfd_generic_link_add_one_symbol
5925 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5926 0, NULL, FALSE,
5927 get_elf_backend_data (dynobj)->collect, &bh)))
5928 return FALSE;
5929 h = (struct elf_link_hash_entry *) bh;
5930 h->non_elf = 0;
5931 h->def_regular = 1;
5932 h->type = STT_OBJECT;
5933 h->verinfo.vertree = t;
5935 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5936 return FALSE;
5938 def.vd_version = VER_DEF_CURRENT;
5939 def.vd_flags = 0;
5940 if (t->globals.list == NULL
5941 && t->locals.list == NULL
5942 && ! t->used)
5943 def.vd_flags |= VER_FLG_WEAK;
5944 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5945 def.vd_cnt = cdeps + 1;
5946 def.vd_hash = bfd_elf_hash (t->name);
5947 def.vd_aux = sizeof (Elf_External_Verdef);
5948 def.vd_next = 0;
5949 if (t->next != NULL)
5950 def.vd_next = (sizeof (Elf_External_Verdef)
5951 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5953 _bfd_elf_swap_verdef_out (output_bfd, &def,
5954 (Elf_External_Verdef *) p);
5955 p += sizeof (Elf_External_Verdef);
5957 defaux.vda_name = h->dynstr_index;
5958 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5959 h->dynstr_index);
5960 defaux.vda_next = 0;
5961 if (t->deps != NULL)
5962 defaux.vda_next = sizeof (Elf_External_Verdaux);
5963 t->name_indx = defaux.vda_name;
5965 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5966 (Elf_External_Verdaux *) p);
5967 p += sizeof (Elf_External_Verdaux);
5969 for (n = t->deps; n != NULL; n = n->next)
5971 if (n->version_needed == NULL)
5973 /* This can happen if there was an error in the
5974 version script. */
5975 defaux.vda_name = 0;
5977 else
5979 defaux.vda_name = n->version_needed->name_indx;
5980 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5981 defaux.vda_name);
5983 if (n->next == NULL)
5984 defaux.vda_next = 0;
5985 else
5986 defaux.vda_next = sizeof (Elf_External_Verdaux);
5988 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5989 (Elf_External_Verdaux *) p);
5990 p += sizeof (Elf_External_Verdaux);
5994 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5995 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5996 return FALSE;
5998 elf_tdata (output_bfd)->cverdefs = cdefs;
6001 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6003 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6004 return FALSE;
6006 else if (info->flags & DF_BIND_NOW)
6008 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6009 return FALSE;
6012 if (info->flags_1)
6014 if (info->executable)
6015 info->flags_1 &= ~ (DF_1_INITFIRST
6016 | DF_1_NODELETE
6017 | DF_1_NOOPEN);
6018 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6019 return FALSE;
6022 /* Work out the size of the version reference section. */
6024 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6025 BFD_ASSERT (s != NULL);
6027 struct elf_find_verdep_info sinfo;
6029 sinfo.output_bfd = output_bfd;
6030 sinfo.info = info;
6031 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6032 if (sinfo.vers == 0)
6033 sinfo.vers = 1;
6034 sinfo.failed = FALSE;
6036 elf_link_hash_traverse (elf_hash_table (info),
6037 _bfd_elf_link_find_version_dependencies,
6038 &sinfo);
6039 if (sinfo.failed)
6040 return FALSE;
6042 if (elf_tdata (output_bfd)->verref == NULL)
6043 s->flags |= SEC_EXCLUDE;
6044 else
6046 Elf_Internal_Verneed *t;
6047 unsigned int size;
6048 unsigned int crefs;
6049 bfd_byte *p;
6051 /* Build the version definition section. */
6052 size = 0;
6053 crefs = 0;
6054 for (t = elf_tdata (output_bfd)->verref;
6055 t != NULL;
6056 t = t->vn_nextref)
6058 Elf_Internal_Vernaux *a;
6060 size += sizeof (Elf_External_Verneed);
6061 ++crefs;
6062 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6063 size += sizeof (Elf_External_Vernaux);
6066 s->size = size;
6067 s->contents = bfd_alloc (output_bfd, s->size);
6068 if (s->contents == NULL)
6069 return FALSE;
6071 p = s->contents;
6072 for (t = elf_tdata (output_bfd)->verref;
6073 t != NULL;
6074 t = t->vn_nextref)
6076 unsigned int caux;
6077 Elf_Internal_Vernaux *a;
6078 bfd_size_type indx;
6080 caux = 0;
6081 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6082 ++caux;
6084 t->vn_version = VER_NEED_CURRENT;
6085 t->vn_cnt = caux;
6086 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6087 elf_dt_name (t->vn_bfd) != NULL
6088 ? elf_dt_name (t->vn_bfd)
6089 : lbasename (t->vn_bfd->filename),
6090 FALSE);
6091 if (indx == (bfd_size_type) -1)
6092 return FALSE;
6093 t->vn_file = indx;
6094 t->vn_aux = sizeof (Elf_External_Verneed);
6095 if (t->vn_nextref == NULL)
6096 t->vn_next = 0;
6097 else
6098 t->vn_next = (sizeof (Elf_External_Verneed)
6099 + caux * sizeof (Elf_External_Vernaux));
6101 _bfd_elf_swap_verneed_out (output_bfd, t,
6102 (Elf_External_Verneed *) p);
6103 p += sizeof (Elf_External_Verneed);
6105 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6107 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6108 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6109 a->vna_nodename, FALSE);
6110 if (indx == (bfd_size_type) -1)
6111 return FALSE;
6112 a->vna_name = indx;
6113 if (a->vna_nextptr == NULL)
6114 a->vna_next = 0;
6115 else
6116 a->vna_next = sizeof (Elf_External_Vernaux);
6118 _bfd_elf_swap_vernaux_out (output_bfd, a,
6119 (Elf_External_Vernaux *) p);
6120 p += sizeof (Elf_External_Vernaux);
6124 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6125 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6126 return FALSE;
6128 elf_tdata (output_bfd)->cverrefs = crefs;
6132 if ((elf_tdata (output_bfd)->cverrefs == 0
6133 && elf_tdata (output_bfd)->cverdefs == 0)
6134 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6135 &section_sym_count) == 0)
6137 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6138 s->flags |= SEC_EXCLUDE;
6141 return TRUE;
6144 /* Find the first non-excluded output section. We'll use its
6145 section symbol for some emitted relocs. */
6146 void
6147 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6149 asection *s;
6151 for (s = output_bfd->sections; s != NULL; s = s->next)
6152 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6153 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6155 elf_hash_table (info)->text_index_section = s;
6156 break;
6160 /* Find two non-excluded output sections, one for code, one for data.
6161 We'll use their section symbols for some emitted relocs. */
6162 void
6163 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6165 asection *s;
6167 /* Data first, since setting text_index_section changes
6168 _bfd_elf_link_omit_section_dynsym. */
6169 for (s = output_bfd->sections; s != NULL; s = s->next)
6170 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6171 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6173 elf_hash_table (info)->data_index_section = s;
6174 break;
6177 for (s = output_bfd->sections; s != NULL; s = s->next)
6178 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6179 == (SEC_ALLOC | SEC_READONLY))
6180 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6182 elf_hash_table (info)->text_index_section = s;
6183 break;
6186 if (elf_hash_table (info)->text_index_section == NULL)
6187 elf_hash_table (info)->text_index_section
6188 = elf_hash_table (info)->data_index_section;
6191 bfd_boolean
6192 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6194 const struct elf_backend_data *bed;
6196 if (!is_elf_hash_table (info->hash))
6197 return TRUE;
6199 bed = get_elf_backend_data (output_bfd);
6200 (*bed->elf_backend_init_index_section) (output_bfd, info);
6202 if (elf_hash_table (info)->dynamic_sections_created)
6204 bfd *dynobj;
6205 asection *s;
6206 bfd_size_type dynsymcount;
6207 unsigned long section_sym_count;
6208 unsigned int dtagcount;
6210 dynobj = elf_hash_table (info)->dynobj;
6212 /* Assign dynsym indicies. In a shared library we generate a
6213 section symbol for each output section, which come first.
6214 Next come all of the back-end allocated local dynamic syms,
6215 followed by the rest of the global symbols. */
6217 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6218 &section_sym_count);
6220 /* Work out the size of the symbol version section. */
6221 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6222 BFD_ASSERT (s != NULL);
6223 if (dynsymcount != 0
6224 && (s->flags & SEC_EXCLUDE) == 0)
6226 s->size = dynsymcount * sizeof (Elf_External_Versym);
6227 s->contents = bfd_zalloc (output_bfd, s->size);
6228 if (s->contents == NULL)
6229 return FALSE;
6231 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6232 return FALSE;
6235 /* Set the size of the .dynsym and .hash sections. We counted
6236 the number of dynamic symbols in elf_link_add_object_symbols.
6237 We will build the contents of .dynsym and .hash when we build
6238 the final symbol table, because until then we do not know the
6239 correct value to give the symbols. We built the .dynstr
6240 section as we went along in elf_link_add_object_symbols. */
6241 s = bfd_get_section_by_name (dynobj, ".dynsym");
6242 BFD_ASSERT (s != NULL);
6243 s->size = dynsymcount * bed->s->sizeof_sym;
6245 if (dynsymcount != 0)
6247 s->contents = bfd_alloc (output_bfd, s->size);
6248 if (s->contents == NULL)
6249 return FALSE;
6251 /* The first entry in .dynsym is a dummy symbol.
6252 Clear all the section syms, in case we don't output them all. */
6253 ++section_sym_count;
6254 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6257 elf_hash_table (info)->bucketcount = 0;
6259 /* Compute the size of the hashing table. As a side effect this
6260 computes the hash values for all the names we export. */
6261 if (info->emit_hash)
6263 unsigned long int *hashcodes;
6264 struct hash_codes_info hashinf;
6265 bfd_size_type amt;
6266 unsigned long int nsyms;
6267 size_t bucketcount;
6268 size_t hash_entry_size;
6270 /* Compute the hash values for all exported symbols. At the same
6271 time store the values in an array so that we could use them for
6272 optimizations. */
6273 amt = dynsymcount * sizeof (unsigned long int);
6274 hashcodes = bfd_malloc (amt);
6275 if (hashcodes == NULL)
6276 return FALSE;
6277 hashinf.hashcodes = hashcodes;
6278 hashinf.error = FALSE;
6280 /* Put all hash values in HASHCODES. */
6281 elf_link_hash_traverse (elf_hash_table (info),
6282 elf_collect_hash_codes, &hashinf);
6283 if (hashinf.error)
6285 free (hashcodes);
6286 return FALSE;
6289 nsyms = hashinf.hashcodes - hashcodes;
6290 bucketcount
6291 = compute_bucket_count (info, hashcodes, nsyms, 0);
6292 free (hashcodes);
6294 if (bucketcount == 0)
6295 return FALSE;
6297 elf_hash_table (info)->bucketcount = bucketcount;
6299 s = bfd_get_section_by_name (dynobj, ".hash");
6300 BFD_ASSERT (s != NULL);
6301 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6302 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6303 s->contents = bfd_zalloc (output_bfd, s->size);
6304 if (s->contents == NULL)
6305 return FALSE;
6307 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6308 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6309 s->contents + hash_entry_size);
6312 if (info->emit_gnu_hash)
6314 size_t i, cnt;
6315 unsigned char *contents;
6316 struct collect_gnu_hash_codes cinfo;
6317 bfd_size_type amt;
6318 size_t bucketcount;
6320 memset (&cinfo, 0, sizeof (cinfo));
6322 /* Compute the hash values for all exported symbols. At the same
6323 time store the values in an array so that we could use them for
6324 optimizations. */
6325 amt = dynsymcount * 2 * sizeof (unsigned long int);
6326 cinfo.hashcodes = bfd_malloc (amt);
6327 if (cinfo.hashcodes == NULL)
6328 return FALSE;
6330 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6331 cinfo.min_dynindx = -1;
6332 cinfo.output_bfd = output_bfd;
6333 cinfo.bed = bed;
6335 /* Put all hash values in HASHCODES. */
6336 elf_link_hash_traverse (elf_hash_table (info),
6337 elf_collect_gnu_hash_codes, &cinfo);
6338 if (cinfo.error)
6340 free (cinfo.hashcodes);
6341 return FALSE;
6344 bucketcount
6345 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6347 if (bucketcount == 0)
6349 free (cinfo.hashcodes);
6350 return FALSE;
6353 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6354 BFD_ASSERT (s != NULL);
6356 if (cinfo.nsyms == 0)
6358 /* Empty .gnu.hash section is special. */
6359 BFD_ASSERT (cinfo.min_dynindx == -1);
6360 free (cinfo.hashcodes);
6361 s->size = 5 * 4 + bed->s->arch_size / 8;
6362 contents = bfd_zalloc (output_bfd, s->size);
6363 if (contents == NULL)
6364 return FALSE;
6365 s->contents = contents;
6366 /* 1 empty bucket. */
6367 bfd_put_32 (output_bfd, 1, contents);
6368 /* SYMIDX above the special symbol 0. */
6369 bfd_put_32 (output_bfd, 1, contents + 4);
6370 /* Just one word for bitmask. */
6371 bfd_put_32 (output_bfd, 1, contents + 8);
6372 /* Only hash fn bloom filter. */
6373 bfd_put_32 (output_bfd, 0, contents + 12);
6374 /* No hashes are valid - empty bitmask. */
6375 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6376 /* No hashes in the only bucket. */
6377 bfd_put_32 (output_bfd, 0,
6378 contents + 16 + bed->s->arch_size / 8);
6380 else
6382 unsigned long int maskwords, maskbitslog2;
6383 BFD_ASSERT (cinfo.min_dynindx != -1);
6385 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6386 if (maskbitslog2 < 3)
6387 maskbitslog2 = 5;
6388 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6389 maskbitslog2 = maskbitslog2 + 3;
6390 else
6391 maskbitslog2 = maskbitslog2 + 2;
6392 if (bed->s->arch_size == 64)
6394 if (maskbitslog2 == 5)
6395 maskbitslog2 = 6;
6396 cinfo.shift1 = 6;
6398 else
6399 cinfo.shift1 = 5;
6400 cinfo.mask = (1 << cinfo.shift1) - 1;
6401 cinfo.shift2 = maskbitslog2;
6402 cinfo.maskbits = 1 << maskbitslog2;
6403 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6404 amt = bucketcount * sizeof (unsigned long int) * 2;
6405 amt += maskwords * sizeof (bfd_vma);
6406 cinfo.bitmask = bfd_malloc (amt);
6407 if (cinfo.bitmask == NULL)
6409 free (cinfo.hashcodes);
6410 return FALSE;
6413 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6414 cinfo.indx = cinfo.counts + bucketcount;
6415 cinfo.symindx = dynsymcount - cinfo.nsyms;
6416 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6418 /* Determine how often each hash bucket is used. */
6419 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6420 for (i = 0; i < cinfo.nsyms; ++i)
6421 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6423 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6424 if (cinfo.counts[i] != 0)
6426 cinfo.indx[i] = cnt;
6427 cnt += cinfo.counts[i];
6429 BFD_ASSERT (cnt == dynsymcount);
6430 cinfo.bucketcount = bucketcount;
6431 cinfo.local_indx = cinfo.min_dynindx;
6433 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6434 s->size += cinfo.maskbits / 8;
6435 contents = bfd_zalloc (output_bfd, s->size);
6436 if (contents == NULL)
6438 free (cinfo.bitmask);
6439 free (cinfo.hashcodes);
6440 return FALSE;
6443 s->contents = contents;
6444 bfd_put_32 (output_bfd, bucketcount, contents);
6445 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6446 bfd_put_32 (output_bfd, maskwords, contents + 8);
6447 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6448 contents += 16 + cinfo.maskbits / 8;
6450 for (i = 0; i < bucketcount; ++i)
6452 if (cinfo.counts[i] == 0)
6453 bfd_put_32 (output_bfd, 0, contents);
6454 else
6455 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6456 contents += 4;
6459 cinfo.contents = contents;
6461 /* Renumber dynamic symbols, populate .gnu.hash section. */
6462 elf_link_hash_traverse (elf_hash_table (info),
6463 elf_renumber_gnu_hash_syms, &cinfo);
6465 contents = s->contents + 16;
6466 for (i = 0; i < maskwords; ++i)
6468 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6469 contents);
6470 contents += bed->s->arch_size / 8;
6473 free (cinfo.bitmask);
6474 free (cinfo.hashcodes);
6478 s = bfd_get_section_by_name (dynobj, ".dynstr");
6479 BFD_ASSERT (s != NULL);
6481 elf_finalize_dynstr (output_bfd, info);
6483 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6485 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6486 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6487 return FALSE;
6490 return TRUE;
6493 /* Indicate that we are only retrieving symbol values from this
6494 section. */
6496 void
6497 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6499 if (is_elf_hash_table (info->hash))
6500 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6501 _bfd_generic_link_just_syms (sec, info);
6504 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6506 static void
6507 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6508 asection *sec)
6510 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6511 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6514 /* Finish SHF_MERGE section merging. */
6516 bfd_boolean
6517 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6519 bfd *ibfd;
6520 asection *sec;
6522 if (!is_elf_hash_table (info->hash))
6523 return FALSE;
6525 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6526 if ((ibfd->flags & DYNAMIC) == 0)
6527 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6528 if ((sec->flags & SEC_MERGE) != 0
6529 && !bfd_is_abs_section (sec->output_section))
6531 struct bfd_elf_section_data *secdata;
6533 secdata = elf_section_data (sec);
6534 if (! _bfd_add_merge_section (abfd,
6535 &elf_hash_table (info)->merge_info,
6536 sec, &secdata->sec_info))
6537 return FALSE;
6538 else if (secdata->sec_info)
6539 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6542 if (elf_hash_table (info)->merge_info != NULL)
6543 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6544 merge_sections_remove_hook);
6545 return TRUE;
6548 /* Create an entry in an ELF linker hash table. */
6550 struct bfd_hash_entry *
6551 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6552 struct bfd_hash_table *table,
6553 const char *string)
6555 /* Allocate the structure if it has not already been allocated by a
6556 subclass. */
6557 if (entry == NULL)
6559 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6560 if (entry == NULL)
6561 return entry;
6564 /* Call the allocation method of the superclass. */
6565 entry = _bfd_link_hash_newfunc (entry, table, string);
6566 if (entry != NULL)
6568 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6569 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6571 /* Set local fields. */
6572 ret->indx = -1;
6573 ret->dynindx = -1;
6574 ret->got = htab->init_got_refcount;
6575 ret->plt = htab->init_plt_refcount;
6576 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6577 - offsetof (struct elf_link_hash_entry, size)));
6578 /* Assume that we have been called by a non-ELF symbol reader.
6579 This flag is then reset by the code which reads an ELF input
6580 file. This ensures that a symbol created by a non-ELF symbol
6581 reader will have the flag set correctly. */
6582 ret->non_elf = 1;
6585 return entry;
6588 /* Copy data from an indirect symbol to its direct symbol, hiding the
6589 old indirect symbol. Also used for copying flags to a weakdef. */
6591 void
6592 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6593 struct elf_link_hash_entry *dir,
6594 struct elf_link_hash_entry *ind)
6596 struct elf_link_hash_table *htab;
6598 /* Copy down any references that we may have already seen to the
6599 symbol which just became indirect. */
6601 dir->ref_dynamic |= ind->ref_dynamic;
6602 dir->ref_regular |= ind->ref_regular;
6603 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6604 dir->non_got_ref |= ind->non_got_ref;
6605 dir->needs_plt |= ind->needs_plt;
6606 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6608 if (ind->root.type != bfd_link_hash_indirect)
6609 return;
6611 /* Copy over the global and procedure linkage table refcount entries.
6612 These may have been already set up by a check_relocs routine. */
6613 htab = elf_hash_table (info);
6614 if (ind->got.refcount > htab->init_got_refcount.refcount)
6616 if (dir->got.refcount < 0)
6617 dir->got.refcount = 0;
6618 dir->got.refcount += ind->got.refcount;
6619 ind->got.refcount = htab->init_got_refcount.refcount;
6622 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6624 if (dir->plt.refcount < 0)
6625 dir->plt.refcount = 0;
6626 dir->plt.refcount += ind->plt.refcount;
6627 ind->plt.refcount = htab->init_plt_refcount.refcount;
6630 if (ind->dynindx != -1)
6632 if (dir->dynindx != -1)
6633 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6634 dir->dynindx = ind->dynindx;
6635 dir->dynstr_index = ind->dynstr_index;
6636 ind->dynindx = -1;
6637 ind->dynstr_index = 0;
6641 void
6642 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6643 struct elf_link_hash_entry *h,
6644 bfd_boolean force_local)
6646 h->plt = elf_hash_table (info)->init_plt_offset;
6647 h->needs_plt = 0;
6648 if (force_local)
6650 h->forced_local = 1;
6651 if (h->dynindx != -1)
6653 h->dynindx = -1;
6654 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6655 h->dynstr_index);
6660 /* Initialize an ELF linker hash table. */
6662 bfd_boolean
6663 _bfd_elf_link_hash_table_init
6664 (struct elf_link_hash_table *table,
6665 bfd *abfd,
6666 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6667 struct bfd_hash_table *,
6668 const char *),
6669 unsigned int entsize)
6671 bfd_boolean ret;
6672 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6674 memset (table, 0, sizeof * table);
6675 table->init_got_refcount.refcount = can_refcount - 1;
6676 table->init_plt_refcount.refcount = can_refcount - 1;
6677 table->init_got_offset.offset = -(bfd_vma) 1;
6678 table->init_plt_offset.offset = -(bfd_vma) 1;
6679 /* The first dynamic symbol is a dummy. */
6680 table->dynsymcount = 1;
6682 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6683 table->root.type = bfd_link_elf_hash_table;
6685 return ret;
6688 /* Create an ELF linker hash table. */
6690 struct bfd_link_hash_table *
6691 _bfd_elf_link_hash_table_create (bfd *abfd)
6693 struct elf_link_hash_table *ret;
6694 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6696 ret = bfd_malloc (amt);
6697 if (ret == NULL)
6698 return NULL;
6700 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6701 sizeof (struct elf_link_hash_entry)))
6703 free (ret);
6704 return NULL;
6707 return &ret->root;
6710 /* This is a hook for the ELF emulation code in the generic linker to
6711 tell the backend linker what file name to use for the DT_NEEDED
6712 entry for a dynamic object. */
6714 void
6715 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6717 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6718 && bfd_get_format (abfd) == bfd_object)
6719 elf_dt_name (abfd) = name;
6723 bfd_elf_get_dyn_lib_class (bfd *abfd)
6725 int lib_class;
6726 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6727 && bfd_get_format (abfd) == bfd_object)
6728 lib_class = elf_dyn_lib_class (abfd);
6729 else
6730 lib_class = 0;
6731 return lib_class;
6734 void
6735 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6737 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6738 && bfd_get_format (abfd) == bfd_object)
6739 elf_dyn_lib_class (abfd) = lib_class;
6742 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6743 the linker ELF emulation code. */
6745 struct bfd_link_needed_list *
6746 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6747 struct bfd_link_info *info)
6749 if (! is_elf_hash_table (info->hash))
6750 return NULL;
6751 return elf_hash_table (info)->needed;
6754 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6755 hook for the linker ELF emulation code. */
6757 struct bfd_link_needed_list *
6758 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6759 struct bfd_link_info *info)
6761 if (! is_elf_hash_table (info->hash))
6762 return NULL;
6763 return elf_hash_table (info)->runpath;
6766 /* Get the name actually used for a dynamic object for a link. This
6767 is the SONAME entry if there is one. Otherwise, it is the string
6768 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6770 const char *
6771 bfd_elf_get_dt_soname (bfd *abfd)
6773 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6774 && bfd_get_format (abfd) == bfd_object)
6775 return elf_dt_name (abfd);
6776 return NULL;
6779 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6780 the ELF linker emulation code. */
6782 bfd_boolean
6783 bfd_elf_get_bfd_needed_list (bfd *abfd,
6784 struct bfd_link_needed_list **pneeded)
6786 asection *s;
6787 bfd_byte *dynbuf = NULL;
6788 unsigned int elfsec;
6789 unsigned long shlink;
6790 bfd_byte *extdyn, *extdynend;
6791 size_t extdynsize;
6792 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6794 *pneeded = NULL;
6796 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6797 || bfd_get_format (abfd) != bfd_object)
6798 return TRUE;
6800 s = bfd_get_section_by_name (abfd, ".dynamic");
6801 if (s == NULL || s->size == 0)
6802 return TRUE;
6804 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6805 goto error_return;
6807 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6808 if (elfsec == SHN_BAD)
6809 goto error_return;
6811 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
6813 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6814 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6816 extdyn = dynbuf;
6817 extdynend = extdyn + s->size;
6818 for (; extdyn < extdynend; extdyn += extdynsize)
6820 Elf_Internal_Dyn dyn;
6822 (*swap_dyn_in) (abfd, extdyn, &dyn);
6824 if (dyn.d_tag == DT_NULL)
6825 break;
6827 if (dyn.d_tag == DT_NEEDED)
6829 const char *string;
6830 struct bfd_link_needed_list *l;
6831 unsigned int tagv = dyn.d_un.d_val;
6832 bfd_size_type amt;
6834 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6835 if (string == NULL)
6836 goto error_return;
6838 amt = sizeof *l;
6839 l = bfd_alloc (abfd, amt);
6840 if (l == NULL)
6841 goto error_return;
6843 l->by = abfd;
6844 l->name = string;
6845 l->next = *pneeded;
6846 *pneeded = l;
6850 free (dynbuf);
6852 return TRUE;
6854 error_return:
6855 if (dynbuf != NULL)
6856 free (dynbuf);
6857 return FALSE;
6860 struct elf_symbuf_symbol
6862 unsigned long st_name; /* Symbol name, index in string tbl */
6863 unsigned char st_info; /* Type and binding attributes */
6864 unsigned char st_other; /* Visibilty, and target specific */
6867 struct elf_symbuf_head
6869 struct elf_symbuf_symbol *ssym;
6870 bfd_size_type count;
6871 unsigned int st_shndx;
6874 struct elf_symbol
6876 union
6878 Elf_Internal_Sym *isym;
6879 struct elf_symbuf_symbol *ssym;
6880 } u;
6881 const char *name;
6884 /* Sort references to symbols by ascending section number. */
6886 static int
6887 elf_sort_elf_symbol (const void *arg1, const void *arg2)
6889 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
6890 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
6892 return s1->st_shndx - s2->st_shndx;
6895 static int
6896 elf_sym_name_compare (const void *arg1, const void *arg2)
6898 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
6899 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
6900 return strcmp (s1->name, s2->name);
6903 static struct elf_symbuf_head *
6904 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
6906 Elf_Internal_Sym **ind, **indbufend, **indbuf;
6907 struct elf_symbuf_symbol *ssym;
6908 struct elf_symbuf_head *ssymbuf, *ssymhead;
6909 bfd_size_type i, shndx_count, total_size;
6911 indbuf = bfd_malloc2 (symcount, sizeof (*indbuf));
6912 if (indbuf == NULL)
6913 return NULL;
6915 for (ind = indbuf, i = 0; i < symcount; i++)
6916 if (isymbuf[i].st_shndx != SHN_UNDEF)
6917 *ind++ = &isymbuf[i];
6918 indbufend = ind;
6920 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
6921 elf_sort_elf_symbol);
6923 shndx_count = 0;
6924 if (indbufend > indbuf)
6925 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
6926 if (ind[0]->st_shndx != ind[1]->st_shndx)
6927 shndx_count++;
6929 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
6930 + (indbufend - indbuf) * sizeof (*ssym));
6931 ssymbuf = bfd_malloc (total_size);
6932 if (ssymbuf == NULL)
6934 free (indbuf);
6935 return NULL;
6938 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
6939 ssymbuf->ssym = NULL;
6940 ssymbuf->count = shndx_count;
6941 ssymbuf->st_shndx = 0;
6942 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
6944 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
6946 ssymhead++;
6947 ssymhead->ssym = ssym;
6948 ssymhead->count = 0;
6949 ssymhead->st_shndx = (*ind)->st_shndx;
6951 ssym->st_name = (*ind)->st_name;
6952 ssym->st_info = (*ind)->st_info;
6953 ssym->st_other = (*ind)->st_other;
6954 ssymhead->count++;
6956 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
6957 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
6958 == total_size));
6960 free (indbuf);
6961 return ssymbuf;
6964 /* Check if 2 sections define the same set of local and global
6965 symbols. */
6967 static bfd_boolean
6968 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
6969 struct bfd_link_info *info)
6971 bfd *bfd1, *bfd2;
6972 const struct elf_backend_data *bed1, *bed2;
6973 Elf_Internal_Shdr *hdr1, *hdr2;
6974 bfd_size_type symcount1, symcount2;
6975 Elf_Internal_Sym *isymbuf1, *isymbuf2;
6976 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
6977 Elf_Internal_Sym *isym, *isymend;
6978 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
6979 bfd_size_type count1, count2, i;
6980 unsigned int shndx1, shndx2;
6981 bfd_boolean result;
6983 bfd1 = sec1->owner;
6984 bfd2 = sec2->owner;
6986 /* Both sections have to be in ELF. */
6987 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
6988 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
6989 return FALSE;
6991 if (elf_section_type (sec1) != elf_section_type (sec2))
6992 return FALSE;
6994 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
6995 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
6996 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
6997 return FALSE;
6999 bed1 = get_elf_backend_data (bfd1);
7000 bed2 = get_elf_backend_data (bfd2);
7001 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7002 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7003 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7004 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7006 if (symcount1 == 0 || symcount2 == 0)
7007 return FALSE;
7009 result = FALSE;
7010 isymbuf1 = NULL;
7011 isymbuf2 = NULL;
7012 ssymbuf1 = elf_tdata (bfd1)->symbuf;
7013 ssymbuf2 = elf_tdata (bfd2)->symbuf;
7015 if (ssymbuf1 == NULL)
7017 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7018 NULL, NULL, NULL);
7019 if (isymbuf1 == NULL)
7020 goto done;
7022 if (!info->reduce_memory_overheads)
7023 elf_tdata (bfd1)->symbuf = ssymbuf1
7024 = elf_create_symbuf (symcount1, isymbuf1);
7027 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7029 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7030 NULL, NULL, NULL);
7031 if (isymbuf2 == NULL)
7032 goto done;
7034 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7035 elf_tdata (bfd2)->symbuf = ssymbuf2
7036 = elf_create_symbuf (symcount2, isymbuf2);
7039 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7041 /* Optimized faster version. */
7042 bfd_size_type lo, hi, mid;
7043 struct elf_symbol *symp;
7044 struct elf_symbuf_symbol *ssym, *ssymend;
7046 lo = 0;
7047 hi = ssymbuf1->count;
7048 ssymbuf1++;
7049 count1 = 0;
7050 while (lo < hi)
7052 mid = (lo + hi) / 2;
7053 if (shndx1 < ssymbuf1[mid].st_shndx)
7054 hi = mid;
7055 else if (shndx1 > ssymbuf1[mid].st_shndx)
7056 lo = mid + 1;
7057 else
7059 count1 = ssymbuf1[mid].count;
7060 ssymbuf1 += mid;
7061 break;
7065 lo = 0;
7066 hi = ssymbuf2->count;
7067 ssymbuf2++;
7068 count2 = 0;
7069 while (lo < hi)
7071 mid = (lo + hi) / 2;
7072 if (shndx2 < ssymbuf2[mid].st_shndx)
7073 hi = mid;
7074 else if (shndx2 > ssymbuf2[mid].st_shndx)
7075 lo = mid + 1;
7076 else
7078 count2 = ssymbuf2[mid].count;
7079 ssymbuf2 += mid;
7080 break;
7084 if (count1 == 0 || count2 == 0 || count1 != count2)
7085 goto done;
7087 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
7088 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
7089 if (symtable1 == NULL || symtable2 == NULL)
7090 goto done;
7092 symp = symtable1;
7093 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7094 ssym < ssymend; ssym++, symp++)
7096 symp->u.ssym = ssym;
7097 symp->name = bfd_elf_string_from_elf_section (bfd1,
7098 hdr1->sh_link,
7099 ssym->st_name);
7102 symp = symtable2;
7103 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7104 ssym < ssymend; ssym++, symp++)
7106 symp->u.ssym = ssym;
7107 symp->name = bfd_elf_string_from_elf_section (bfd2,
7108 hdr2->sh_link,
7109 ssym->st_name);
7112 /* Sort symbol by name. */
7113 qsort (symtable1, count1, sizeof (struct elf_symbol),
7114 elf_sym_name_compare);
7115 qsort (symtable2, count1, sizeof (struct elf_symbol),
7116 elf_sym_name_compare);
7118 for (i = 0; i < count1; i++)
7119 /* Two symbols must have the same binding, type and name. */
7120 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7121 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7122 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7123 goto done;
7125 result = TRUE;
7126 goto done;
7129 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7130 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7131 if (symtable1 == NULL || symtable2 == NULL)
7132 goto done;
7134 /* Count definitions in the section. */
7135 count1 = 0;
7136 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7137 if (isym->st_shndx == shndx1)
7138 symtable1[count1++].u.isym = isym;
7140 count2 = 0;
7141 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7142 if (isym->st_shndx == shndx2)
7143 symtable2[count2++].u.isym = isym;
7145 if (count1 == 0 || count2 == 0 || count1 != count2)
7146 goto done;
7148 for (i = 0; i < count1; i++)
7149 symtable1[i].name
7150 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7151 symtable1[i].u.isym->st_name);
7153 for (i = 0; i < count2; i++)
7154 symtable2[i].name
7155 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7156 symtable2[i].u.isym->st_name);
7158 /* Sort symbol by name. */
7159 qsort (symtable1, count1, sizeof (struct elf_symbol),
7160 elf_sym_name_compare);
7161 qsort (symtable2, count1, sizeof (struct elf_symbol),
7162 elf_sym_name_compare);
7164 for (i = 0; i < count1; i++)
7165 /* Two symbols must have the same binding, type and name. */
7166 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7167 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7168 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7169 goto done;
7171 result = TRUE;
7173 done:
7174 if (symtable1)
7175 free (symtable1);
7176 if (symtable2)
7177 free (symtable2);
7178 if (isymbuf1)
7179 free (isymbuf1);
7180 if (isymbuf2)
7181 free (isymbuf2);
7183 return result;
7186 /* Return TRUE if 2 section types are compatible. */
7188 bfd_boolean
7189 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7190 bfd *bbfd, const asection *bsec)
7192 if (asec == NULL
7193 || bsec == NULL
7194 || abfd->xvec->flavour != bfd_target_elf_flavour
7195 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7196 return TRUE;
7198 return elf_section_type (asec) == elf_section_type (bsec);
7201 /* Final phase of ELF linker. */
7203 /* A structure we use to avoid passing large numbers of arguments. */
7205 struct elf_final_link_info
7207 /* General link information. */
7208 struct bfd_link_info *info;
7209 /* Output BFD. */
7210 bfd *output_bfd;
7211 /* Symbol string table. */
7212 struct bfd_strtab_hash *symstrtab;
7213 /* .dynsym section. */
7214 asection *dynsym_sec;
7215 /* .hash section. */
7216 asection *hash_sec;
7217 /* symbol version section (.gnu.version). */
7218 asection *symver_sec;
7219 /* Buffer large enough to hold contents of any section. */
7220 bfd_byte *contents;
7221 /* Buffer large enough to hold external relocs of any section. */
7222 void *external_relocs;
7223 /* Buffer large enough to hold internal relocs of any section. */
7224 Elf_Internal_Rela *internal_relocs;
7225 /* Buffer large enough to hold external local symbols of any input
7226 BFD. */
7227 bfd_byte *external_syms;
7228 /* And a buffer for symbol section indices. */
7229 Elf_External_Sym_Shndx *locsym_shndx;
7230 /* Buffer large enough to hold internal local symbols of any input
7231 BFD. */
7232 Elf_Internal_Sym *internal_syms;
7233 /* Array large enough to hold a symbol index for each local symbol
7234 of any input BFD. */
7235 long *indices;
7236 /* Array large enough to hold a section pointer for each local
7237 symbol of any input BFD. */
7238 asection **sections;
7239 /* Buffer to hold swapped out symbols. */
7240 bfd_byte *symbuf;
7241 /* And one for symbol section indices. */
7242 Elf_External_Sym_Shndx *symshndxbuf;
7243 /* Number of swapped out symbols in buffer. */
7244 size_t symbuf_count;
7245 /* Number of symbols which fit in symbuf. */
7246 size_t symbuf_size;
7247 /* And same for symshndxbuf. */
7248 size_t shndxbuf_size;
7251 /* This struct is used to pass information to elf_link_output_extsym. */
7253 struct elf_outext_info
7255 bfd_boolean failed;
7256 bfd_boolean localsyms;
7257 struct elf_final_link_info *finfo;
7261 /* Support for evaluating a complex relocation.
7263 Complex relocations are generalized, self-describing relocations. The
7264 implementation of them consists of two parts: complex symbols, and the
7265 relocations themselves.
7267 The relocations are use a reserved elf-wide relocation type code (R_RELC
7268 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7269 information (start bit, end bit, word width, etc) into the addend. This
7270 information is extracted from CGEN-generated operand tables within gas.
7272 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7273 internal) representing prefix-notation expressions, including but not
7274 limited to those sorts of expressions normally encoded as addends in the
7275 addend field. The symbol mangling format is:
7277 <node> := <literal>
7278 | <unary-operator> ':' <node>
7279 | <binary-operator> ':' <node> ':' <node>
7282 <literal> := 's' <digits=N> ':' <N character symbol name>
7283 | 'S' <digits=N> ':' <N character section name>
7284 | '#' <hexdigits>
7287 <binary-operator> := as in C
7288 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7290 static void
7291 set_symbol_value (bfd *bfd_with_globals,
7292 Elf_Internal_Sym *isymbuf,
7293 size_t locsymcount,
7294 size_t symidx,
7295 bfd_vma val)
7297 struct elf_link_hash_entry **sym_hashes;
7298 struct elf_link_hash_entry *h;
7299 size_t extsymoff = locsymcount;
7301 if (symidx < locsymcount)
7303 Elf_Internal_Sym *sym;
7305 sym = isymbuf + symidx;
7306 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7308 /* It is a local symbol: move it to the
7309 "absolute" section and give it a value. */
7310 sym->st_shndx = SHN_ABS;
7311 sym->st_value = val;
7312 return;
7314 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7315 extsymoff = 0;
7318 /* It is a global symbol: set its link type
7319 to "defined" and give it a value. */
7321 sym_hashes = elf_sym_hashes (bfd_with_globals);
7322 h = sym_hashes [symidx - extsymoff];
7323 while (h->root.type == bfd_link_hash_indirect
7324 || h->root.type == bfd_link_hash_warning)
7325 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7326 h->root.type = bfd_link_hash_defined;
7327 h->root.u.def.value = val;
7328 h->root.u.def.section = bfd_abs_section_ptr;
7331 static bfd_boolean
7332 resolve_symbol (const char *name,
7333 bfd *input_bfd,
7334 struct elf_final_link_info *finfo,
7335 bfd_vma *result,
7336 Elf_Internal_Sym *isymbuf,
7337 size_t locsymcount)
7339 Elf_Internal_Sym *sym;
7340 struct bfd_link_hash_entry *global_entry;
7341 const char *candidate = NULL;
7342 Elf_Internal_Shdr *symtab_hdr;
7343 size_t i;
7345 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7347 for (i = 0; i < locsymcount; ++ i)
7349 sym = isymbuf + i;
7351 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7352 continue;
7354 candidate = bfd_elf_string_from_elf_section (input_bfd,
7355 symtab_hdr->sh_link,
7356 sym->st_name);
7357 #ifdef DEBUG
7358 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7359 name, candidate, (unsigned long) sym->st_value);
7360 #endif
7361 if (candidate && strcmp (candidate, name) == 0)
7363 asection *sec = finfo->sections [i];
7365 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7366 *result += sec->output_offset + sec->output_section->vma;
7367 #ifdef DEBUG
7368 printf ("Found symbol with value %8.8lx\n",
7369 (unsigned long) *result);
7370 #endif
7371 return TRUE;
7375 /* Hmm, haven't found it yet. perhaps it is a global. */
7376 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7377 FALSE, FALSE, TRUE);
7378 if (!global_entry)
7379 return FALSE;
7381 if (global_entry->type == bfd_link_hash_defined
7382 || global_entry->type == bfd_link_hash_defweak)
7384 *result = (global_entry->u.def.value
7385 + global_entry->u.def.section->output_section->vma
7386 + global_entry->u.def.section->output_offset);
7387 #ifdef DEBUG
7388 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7389 global_entry->root.string, (unsigned long) *result);
7390 #endif
7391 return TRUE;
7394 return FALSE;
7397 static bfd_boolean
7398 resolve_section (const char *name,
7399 asection *sections,
7400 bfd_vma *result)
7402 asection *curr;
7403 unsigned int len;
7405 for (curr = sections; curr; curr = curr->next)
7406 if (strcmp (curr->name, name) == 0)
7408 *result = curr->vma;
7409 return TRUE;
7412 /* Hmm. still haven't found it. try pseudo-section names. */
7413 for (curr = sections; curr; curr = curr->next)
7415 len = strlen (curr->name);
7416 if (len > strlen (name))
7417 continue;
7419 if (strncmp (curr->name, name, len) == 0)
7421 if (strncmp (".end", name + len, 4) == 0)
7423 *result = curr->vma + curr->size;
7424 return TRUE;
7427 /* Insert more pseudo-section names here, if you like. */
7431 return FALSE;
7434 static void
7435 undefined_reference (const char *reftype, const char *name)
7437 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7438 reftype, name);
7441 static bfd_boolean
7442 eval_symbol (bfd_vma *result,
7443 const char **symp,
7444 bfd *input_bfd,
7445 struct elf_final_link_info *finfo,
7446 bfd_vma dot,
7447 Elf_Internal_Sym *isymbuf,
7448 size_t locsymcount,
7449 int signed_p)
7451 size_t len;
7452 size_t symlen;
7453 bfd_vma a;
7454 bfd_vma b;
7455 char symbuf[4096];
7456 const char *sym = *symp;
7457 const char *symend;
7458 bfd_boolean symbol_is_section = FALSE;
7460 len = strlen (sym);
7461 symend = sym + len;
7463 if (len < 1 || len > sizeof (symbuf))
7465 bfd_set_error (bfd_error_invalid_operation);
7466 return FALSE;
7469 switch (* sym)
7471 case '.':
7472 *result = dot;
7473 *symp = sym + 1;
7474 return TRUE;
7476 case '#':
7477 ++sym;
7478 *result = strtoul (sym, (char **) symp, 16);
7479 return TRUE;
7481 case 'S':
7482 symbol_is_section = TRUE;
7483 case 's':
7484 ++sym;
7485 symlen = strtol (sym, (char **) symp, 10);
7486 sym = *symp + 1; /* Skip the trailing ':'. */
7488 if (symend < sym || symlen + 1 > sizeof (symbuf))
7490 bfd_set_error (bfd_error_invalid_operation);
7491 return FALSE;
7494 memcpy (symbuf, sym, symlen);
7495 symbuf[symlen] = '\0';
7496 *symp = sym + symlen;
7498 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7499 the symbol as a section, or vice-versa. so we're pretty liberal in our
7500 interpretation here; section means "try section first", not "must be a
7501 section", and likewise with symbol. */
7503 if (symbol_is_section)
7505 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7506 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7507 isymbuf, locsymcount))
7509 undefined_reference ("section", symbuf);
7510 return FALSE;
7513 else
7515 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7516 isymbuf, locsymcount)
7517 && !resolve_section (symbuf, finfo->output_bfd->sections,
7518 result))
7520 undefined_reference ("symbol", symbuf);
7521 return FALSE;
7525 return TRUE;
7527 /* All that remains are operators. */
7529 #define UNARY_OP(op) \
7530 if (strncmp (sym, #op, strlen (#op)) == 0) \
7532 sym += strlen (#op); \
7533 if (*sym == ':') \
7534 ++sym; \
7535 *symp = sym; \
7536 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7537 isymbuf, locsymcount, signed_p)) \
7538 return FALSE; \
7539 if (signed_p) \
7540 *result = op ((bfd_signed_vma) a); \
7541 else \
7542 *result = op a; \
7543 return TRUE; \
7546 #define BINARY_OP(op) \
7547 if (strncmp (sym, #op, strlen (#op)) == 0) \
7549 sym += strlen (#op); \
7550 if (*sym == ':') \
7551 ++sym; \
7552 *symp = sym; \
7553 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7554 isymbuf, locsymcount, signed_p)) \
7555 return FALSE; \
7556 ++*symp; \
7557 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7558 isymbuf, locsymcount, signed_p)) \
7559 return FALSE; \
7560 if (signed_p) \
7561 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7562 else \
7563 *result = a op b; \
7564 return TRUE; \
7567 default:
7568 UNARY_OP (0-);
7569 BINARY_OP (<<);
7570 BINARY_OP (>>);
7571 BINARY_OP (==);
7572 BINARY_OP (!=);
7573 BINARY_OP (<=);
7574 BINARY_OP (>=);
7575 BINARY_OP (&&);
7576 BINARY_OP (||);
7577 UNARY_OP (~);
7578 UNARY_OP (!);
7579 BINARY_OP (*);
7580 BINARY_OP (/);
7581 BINARY_OP (%);
7582 BINARY_OP (^);
7583 BINARY_OP (|);
7584 BINARY_OP (&);
7585 BINARY_OP (+);
7586 BINARY_OP (-);
7587 BINARY_OP (<);
7588 BINARY_OP (>);
7589 #undef UNARY_OP
7590 #undef BINARY_OP
7591 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7592 bfd_set_error (bfd_error_invalid_operation);
7593 return FALSE;
7597 static void
7598 put_value (bfd_vma size,
7599 unsigned long chunksz,
7600 bfd *input_bfd,
7601 bfd_vma x,
7602 bfd_byte *location)
7604 location += (size - chunksz);
7606 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7608 switch (chunksz)
7610 default:
7611 case 0:
7612 abort ();
7613 case 1:
7614 bfd_put_8 (input_bfd, x, location);
7615 break;
7616 case 2:
7617 bfd_put_16 (input_bfd, x, location);
7618 break;
7619 case 4:
7620 bfd_put_32 (input_bfd, x, location);
7621 break;
7622 case 8:
7623 #ifdef BFD64
7624 bfd_put_64 (input_bfd, x, location);
7625 #else
7626 abort ();
7627 #endif
7628 break;
7633 static bfd_vma
7634 get_value (bfd_vma size,
7635 unsigned long chunksz,
7636 bfd *input_bfd,
7637 bfd_byte *location)
7639 bfd_vma x = 0;
7641 for (; size; size -= chunksz, location += chunksz)
7643 switch (chunksz)
7645 default:
7646 case 0:
7647 abort ();
7648 case 1:
7649 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7650 break;
7651 case 2:
7652 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7653 break;
7654 case 4:
7655 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7656 break;
7657 case 8:
7658 #ifdef BFD64
7659 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7660 #else
7661 abort ();
7662 #endif
7663 break;
7666 return x;
7669 static void
7670 decode_complex_addend (unsigned long *start, /* in bits */
7671 unsigned long *oplen, /* in bits */
7672 unsigned long *len, /* in bits */
7673 unsigned long *wordsz, /* in bytes */
7674 unsigned long *chunksz, /* in bytes */
7675 unsigned long *lsb0_p,
7676 unsigned long *signed_p,
7677 unsigned long *trunc_p,
7678 unsigned long encoded)
7680 * start = encoded & 0x3F;
7681 * len = (encoded >> 6) & 0x3F;
7682 * oplen = (encoded >> 12) & 0x3F;
7683 * wordsz = (encoded >> 18) & 0xF;
7684 * chunksz = (encoded >> 22) & 0xF;
7685 * lsb0_p = (encoded >> 27) & 1;
7686 * signed_p = (encoded >> 28) & 1;
7687 * trunc_p = (encoded >> 29) & 1;
7690 bfd_reloc_status_type
7691 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7692 asection *input_section ATTRIBUTE_UNUSED,
7693 bfd_byte *contents,
7694 Elf_Internal_Rela *rel,
7695 bfd_vma relocation)
7697 bfd_vma shift, x, mask;
7698 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7699 bfd_reloc_status_type r;
7701 /* Perform this reloc, since it is complex.
7702 (this is not to say that it necessarily refers to a complex
7703 symbol; merely that it is a self-describing CGEN based reloc.
7704 i.e. the addend has the complete reloc information (bit start, end,
7705 word size, etc) encoded within it.). */
7707 decode_complex_addend (&start, &oplen, &len, &wordsz,
7708 &chunksz, &lsb0_p, &signed_p,
7709 &trunc_p, rel->r_addend);
7711 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7713 if (lsb0_p)
7714 shift = (start + 1) - len;
7715 else
7716 shift = (8 * wordsz) - (start + len);
7718 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7720 #ifdef DEBUG
7721 printf ("Doing complex reloc: "
7722 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7723 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7724 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7725 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7726 oplen, x, mask, relocation);
7727 #endif
7729 r = bfd_reloc_ok;
7730 if (! trunc_p)
7731 /* Now do an overflow check. */
7732 r = bfd_check_overflow ((signed_p
7733 ? complain_overflow_signed
7734 : complain_overflow_unsigned),
7735 len, 0, (8 * wordsz),
7736 relocation);
7738 /* Do the deed. */
7739 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7741 #ifdef DEBUG
7742 printf (" relocation: %8.8lx\n"
7743 " shifted mask: %8.8lx\n"
7744 " shifted/masked reloc: %8.8lx\n"
7745 " result: %8.8lx\n",
7746 relocation, (mask << shift),
7747 ((relocation & mask) << shift), x);
7748 #endif
7749 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7750 return r;
7753 /* When performing a relocatable link, the input relocations are
7754 preserved. But, if they reference global symbols, the indices
7755 referenced must be updated. Update all the relocations in
7756 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7758 static void
7759 elf_link_adjust_relocs (bfd *abfd,
7760 Elf_Internal_Shdr *rel_hdr,
7761 unsigned int count,
7762 struct elf_link_hash_entry **rel_hash)
7764 unsigned int i;
7765 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7766 bfd_byte *erela;
7767 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7768 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7769 bfd_vma r_type_mask;
7770 int r_sym_shift;
7772 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7774 swap_in = bed->s->swap_reloc_in;
7775 swap_out = bed->s->swap_reloc_out;
7777 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7779 swap_in = bed->s->swap_reloca_in;
7780 swap_out = bed->s->swap_reloca_out;
7782 else
7783 abort ();
7785 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7786 abort ();
7788 if (bed->s->arch_size == 32)
7790 r_type_mask = 0xff;
7791 r_sym_shift = 8;
7793 else
7795 r_type_mask = 0xffffffff;
7796 r_sym_shift = 32;
7799 erela = rel_hdr->contents;
7800 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7802 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7803 unsigned int j;
7805 if (*rel_hash == NULL)
7806 continue;
7808 BFD_ASSERT ((*rel_hash)->indx >= 0);
7810 (*swap_in) (abfd, erela, irela);
7811 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7812 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7813 | (irela[j].r_info & r_type_mask));
7814 (*swap_out) (abfd, irela, erela);
7818 struct elf_link_sort_rela
7820 union {
7821 bfd_vma offset;
7822 bfd_vma sym_mask;
7823 } u;
7824 enum elf_reloc_type_class type;
7825 /* We use this as an array of size int_rels_per_ext_rel. */
7826 Elf_Internal_Rela rela[1];
7829 static int
7830 elf_link_sort_cmp1 (const void *A, const void *B)
7832 const struct elf_link_sort_rela *a = A;
7833 const struct elf_link_sort_rela *b = B;
7834 int relativea, relativeb;
7836 relativea = a->type == reloc_class_relative;
7837 relativeb = b->type == reloc_class_relative;
7839 if (relativea < relativeb)
7840 return 1;
7841 if (relativea > relativeb)
7842 return -1;
7843 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7844 return -1;
7845 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7846 return 1;
7847 if (a->rela->r_offset < b->rela->r_offset)
7848 return -1;
7849 if (a->rela->r_offset > b->rela->r_offset)
7850 return 1;
7851 return 0;
7854 static int
7855 elf_link_sort_cmp2 (const void *A, const void *B)
7857 const struct elf_link_sort_rela *a = A;
7858 const struct elf_link_sort_rela *b = B;
7859 int copya, copyb;
7861 if (a->u.offset < b->u.offset)
7862 return -1;
7863 if (a->u.offset > b->u.offset)
7864 return 1;
7865 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7866 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7867 if (copya < copyb)
7868 return -1;
7869 if (copya > copyb)
7870 return 1;
7871 if (a->rela->r_offset < b->rela->r_offset)
7872 return -1;
7873 if (a->rela->r_offset > b->rela->r_offset)
7874 return 1;
7875 return 0;
7878 static size_t
7879 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7881 asection *dynamic_relocs;
7882 asection *rela_dyn;
7883 asection *rel_dyn;
7884 bfd_size_type count, size;
7885 size_t i, ret, sort_elt, ext_size;
7886 bfd_byte *sort, *s_non_relative, *p;
7887 struct elf_link_sort_rela *sq;
7888 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7889 int i2e = bed->s->int_rels_per_ext_rel;
7890 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7891 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7892 struct bfd_link_order *lo;
7893 bfd_vma r_sym_mask;
7894 bfd_boolean use_rela;
7896 /* Find a dynamic reloc section. */
7897 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7898 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7899 if (rela_dyn != NULL && rela_dyn->size > 0
7900 && rel_dyn != NULL && rel_dyn->size > 0)
7902 bfd_boolean use_rela_initialised = FALSE;
7904 /* This is just here to stop gcc from complaining.
7905 It's initialization checking code is not perfect. */
7906 use_rela = TRUE;
7908 /* Both sections are present. Examine the sizes
7909 of the indirect sections to help us choose. */
7910 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7911 if (lo->type == bfd_indirect_link_order)
7913 asection *o = lo->u.indirect.section;
7915 if ((o->size % bed->s->sizeof_rela) == 0)
7917 if ((o->size % bed->s->sizeof_rel) == 0)
7918 /* Section size is divisible by both rel and rela sizes.
7919 It is of no help to us. */
7921 else
7923 /* Section size is only divisible by rela. */
7924 if (use_rela_initialised && (use_rela == FALSE))
7926 _bfd_error_handler
7927 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7928 bfd_set_error (bfd_error_invalid_operation);
7929 return 0;
7931 else
7933 use_rela = TRUE;
7934 use_rela_initialised = TRUE;
7938 else if ((o->size % bed->s->sizeof_rel) == 0)
7940 /* Section size is only divisible by rel. */
7941 if (use_rela_initialised && (use_rela == TRUE))
7943 _bfd_error_handler
7944 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7945 bfd_set_error (bfd_error_invalid_operation);
7946 return 0;
7948 else
7950 use_rela = FALSE;
7951 use_rela_initialised = TRUE;
7954 else
7956 /* The section size is not divisible by either - something is wrong. */
7957 _bfd_error_handler
7958 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7959 bfd_set_error (bfd_error_invalid_operation);
7960 return 0;
7964 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7965 if (lo->type == bfd_indirect_link_order)
7967 asection *o = lo->u.indirect.section;
7969 if ((o->size % bed->s->sizeof_rela) == 0)
7971 if ((o->size % bed->s->sizeof_rel) == 0)
7972 /* Section size is divisible by both rel and rela sizes.
7973 It is of no help to us. */
7975 else
7977 /* Section size is only divisible by rela. */
7978 if (use_rela_initialised && (use_rela == FALSE))
7980 _bfd_error_handler
7981 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7982 bfd_set_error (bfd_error_invalid_operation);
7983 return 0;
7985 else
7987 use_rela = TRUE;
7988 use_rela_initialised = TRUE;
7992 else if ((o->size % bed->s->sizeof_rel) == 0)
7994 /* Section size is only divisible by rel. */
7995 if (use_rela_initialised && (use_rela == TRUE))
7997 _bfd_error_handler
7998 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7999 bfd_set_error (bfd_error_invalid_operation);
8000 return 0;
8002 else
8004 use_rela = FALSE;
8005 use_rela_initialised = TRUE;
8008 else
8010 /* The section size is not divisible by either - something is wrong. */
8011 _bfd_error_handler
8012 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8013 bfd_set_error (bfd_error_invalid_operation);
8014 return 0;
8018 if (! use_rela_initialised)
8019 /* Make a guess. */
8020 use_rela = TRUE;
8022 else if (rela_dyn != NULL && rela_dyn->size > 0)
8023 use_rela = TRUE;
8024 else if (rel_dyn != NULL && rel_dyn->size > 0)
8025 use_rela = FALSE;
8026 else
8027 return 0;
8029 if (use_rela)
8031 dynamic_relocs = rela_dyn;
8032 ext_size = bed->s->sizeof_rela;
8033 swap_in = bed->s->swap_reloca_in;
8034 swap_out = bed->s->swap_reloca_out;
8036 else
8038 dynamic_relocs = rel_dyn;
8039 ext_size = bed->s->sizeof_rel;
8040 swap_in = bed->s->swap_reloc_in;
8041 swap_out = bed->s->swap_reloc_out;
8044 size = 0;
8045 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8046 if (lo->type == bfd_indirect_link_order)
8047 size += lo->u.indirect.section->size;
8049 if (size != dynamic_relocs->size)
8050 return 0;
8052 sort_elt = (sizeof (struct elf_link_sort_rela)
8053 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8055 count = dynamic_relocs->size / ext_size;
8056 sort = bfd_zmalloc (sort_elt * count);
8058 if (sort == NULL)
8060 (*info->callbacks->warning)
8061 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8062 return 0;
8065 if (bed->s->arch_size == 32)
8066 r_sym_mask = ~(bfd_vma) 0xff;
8067 else
8068 r_sym_mask = ~(bfd_vma) 0xffffffff;
8070 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8071 if (lo->type == bfd_indirect_link_order)
8073 bfd_byte *erel, *erelend;
8074 asection *o = lo->u.indirect.section;
8076 if (o->contents == NULL && o->size != 0)
8078 /* This is a reloc section that is being handled as a normal
8079 section. See bfd_section_from_shdr. We can't combine
8080 relocs in this case. */
8081 free (sort);
8082 return 0;
8084 erel = o->contents;
8085 erelend = o->contents + o->size;
8086 p = sort + o->output_offset / ext_size * sort_elt;
8088 while (erel < erelend)
8090 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8092 (*swap_in) (abfd, erel, s->rela);
8093 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8094 s->u.sym_mask = r_sym_mask;
8095 p += sort_elt;
8096 erel += ext_size;
8100 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8102 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8104 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8105 if (s->type != reloc_class_relative)
8106 break;
8108 ret = i;
8109 s_non_relative = p;
8111 sq = (struct elf_link_sort_rela *) s_non_relative;
8112 for (; i < count; i++, p += sort_elt)
8114 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8115 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8116 sq = sp;
8117 sp->u.offset = sq->rela->r_offset;
8120 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8122 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8123 if (lo->type == bfd_indirect_link_order)
8125 bfd_byte *erel, *erelend;
8126 asection *o = lo->u.indirect.section;
8128 erel = o->contents;
8129 erelend = o->contents + o->size;
8130 p = sort + o->output_offset / ext_size * sort_elt;
8131 while (erel < erelend)
8133 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8134 (*swap_out) (abfd, s->rela, erel);
8135 p += sort_elt;
8136 erel += ext_size;
8140 free (sort);
8141 *psec = dynamic_relocs;
8142 return ret;
8145 /* Flush the output symbols to the file. */
8147 static bfd_boolean
8148 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8149 const struct elf_backend_data *bed)
8151 if (finfo->symbuf_count > 0)
8153 Elf_Internal_Shdr *hdr;
8154 file_ptr pos;
8155 bfd_size_type amt;
8157 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8158 pos = hdr->sh_offset + hdr->sh_size;
8159 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8160 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8161 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8162 return FALSE;
8164 hdr->sh_size += amt;
8165 finfo->symbuf_count = 0;
8168 return TRUE;
8171 /* Add a symbol to the output symbol table. */
8173 static bfd_boolean
8174 elf_link_output_sym (struct elf_final_link_info *finfo,
8175 const char *name,
8176 Elf_Internal_Sym *elfsym,
8177 asection *input_sec,
8178 struct elf_link_hash_entry *h)
8180 bfd_byte *dest;
8181 Elf_External_Sym_Shndx *destshndx;
8182 bfd_boolean (*output_symbol_hook)
8183 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8184 struct elf_link_hash_entry *);
8185 const struct elf_backend_data *bed;
8187 bed = get_elf_backend_data (finfo->output_bfd);
8188 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8189 if (output_symbol_hook != NULL)
8191 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
8192 return FALSE;
8195 if (name == NULL || *name == '\0')
8196 elfsym->st_name = 0;
8197 else if (input_sec->flags & SEC_EXCLUDE)
8198 elfsym->st_name = 0;
8199 else
8201 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8202 name, TRUE, FALSE);
8203 if (elfsym->st_name == (unsigned long) -1)
8204 return FALSE;
8207 if (finfo->symbuf_count >= finfo->symbuf_size)
8209 if (! elf_link_flush_output_syms (finfo, bed))
8210 return FALSE;
8213 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8214 destshndx = finfo->symshndxbuf;
8215 if (destshndx != NULL)
8217 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8219 bfd_size_type amt;
8221 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8222 destshndx = bfd_realloc (destshndx, amt * 2);
8223 if (destshndx == NULL)
8224 return FALSE;
8225 finfo->symshndxbuf = destshndx;
8226 memset ((char *) destshndx + amt, 0, amt);
8227 finfo->shndxbuf_size *= 2;
8229 destshndx += bfd_get_symcount (finfo->output_bfd);
8232 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8233 finfo->symbuf_count += 1;
8234 bfd_get_symcount (finfo->output_bfd) += 1;
8236 return TRUE;
8239 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8241 static bfd_boolean
8242 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8244 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8245 && sym->st_shndx < SHN_LORESERVE)
8247 /* The gABI doesn't support dynamic symbols in output sections
8248 beyond 64k. */
8249 (*_bfd_error_handler)
8250 (_("%B: Too many sections: %d (>= %d)"),
8251 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8252 bfd_set_error (bfd_error_nonrepresentable_section);
8253 return FALSE;
8255 return TRUE;
8258 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8259 allowing an unsatisfied unversioned symbol in the DSO to match a
8260 versioned symbol that would normally require an explicit version.
8261 We also handle the case that a DSO references a hidden symbol
8262 which may be satisfied by a versioned symbol in another DSO. */
8264 static bfd_boolean
8265 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8266 const struct elf_backend_data *bed,
8267 struct elf_link_hash_entry *h)
8269 bfd *abfd;
8270 struct elf_link_loaded_list *loaded;
8272 if (!is_elf_hash_table (info->hash))
8273 return FALSE;
8275 switch (h->root.type)
8277 default:
8278 abfd = NULL;
8279 break;
8281 case bfd_link_hash_undefined:
8282 case bfd_link_hash_undefweak:
8283 abfd = h->root.u.undef.abfd;
8284 if ((abfd->flags & DYNAMIC) == 0
8285 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8286 return FALSE;
8287 break;
8289 case bfd_link_hash_defined:
8290 case bfd_link_hash_defweak:
8291 abfd = h->root.u.def.section->owner;
8292 break;
8294 case bfd_link_hash_common:
8295 abfd = h->root.u.c.p->section->owner;
8296 break;
8298 BFD_ASSERT (abfd != NULL);
8300 for (loaded = elf_hash_table (info)->loaded;
8301 loaded != NULL;
8302 loaded = loaded->next)
8304 bfd *input;
8305 Elf_Internal_Shdr *hdr;
8306 bfd_size_type symcount;
8307 bfd_size_type extsymcount;
8308 bfd_size_type extsymoff;
8309 Elf_Internal_Shdr *versymhdr;
8310 Elf_Internal_Sym *isym;
8311 Elf_Internal_Sym *isymend;
8312 Elf_Internal_Sym *isymbuf;
8313 Elf_External_Versym *ever;
8314 Elf_External_Versym *extversym;
8316 input = loaded->abfd;
8318 /* We check each DSO for a possible hidden versioned definition. */
8319 if (input == abfd
8320 || (input->flags & DYNAMIC) == 0
8321 || elf_dynversym (input) == 0)
8322 continue;
8324 hdr = &elf_tdata (input)->dynsymtab_hdr;
8326 symcount = hdr->sh_size / bed->s->sizeof_sym;
8327 if (elf_bad_symtab (input))
8329 extsymcount = symcount;
8330 extsymoff = 0;
8332 else
8334 extsymcount = symcount - hdr->sh_info;
8335 extsymoff = hdr->sh_info;
8338 if (extsymcount == 0)
8339 continue;
8341 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8342 NULL, NULL, NULL);
8343 if (isymbuf == NULL)
8344 return FALSE;
8346 /* Read in any version definitions. */
8347 versymhdr = &elf_tdata (input)->dynversym_hdr;
8348 extversym = bfd_malloc (versymhdr->sh_size);
8349 if (extversym == NULL)
8350 goto error_ret;
8352 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8353 || (bfd_bread (extversym, versymhdr->sh_size, input)
8354 != versymhdr->sh_size))
8356 free (extversym);
8357 error_ret:
8358 free (isymbuf);
8359 return FALSE;
8362 ever = extversym + extsymoff;
8363 isymend = isymbuf + extsymcount;
8364 for (isym = isymbuf; isym < isymend; isym++, ever++)
8366 const char *name;
8367 Elf_Internal_Versym iver;
8368 unsigned short version_index;
8370 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8371 || isym->st_shndx == SHN_UNDEF)
8372 continue;
8374 name = bfd_elf_string_from_elf_section (input,
8375 hdr->sh_link,
8376 isym->st_name);
8377 if (strcmp (name, h->root.root.string) != 0)
8378 continue;
8380 _bfd_elf_swap_versym_in (input, ever, &iver);
8382 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
8384 /* If we have a non-hidden versioned sym, then it should
8385 have provided a definition for the undefined sym. */
8386 abort ();
8389 version_index = iver.vs_vers & VERSYM_VERSION;
8390 if (version_index == 1 || version_index == 2)
8392 /* This is the base or first version. We can use it. */
8393 free (extversym);
8394 free (isymbuf);
8395 return TRUE;
8399 free (extversym);
8400 free (isymbuf);
8403 return FALSE;
8406 /* Add an external symbol to the symbol table. This is called from
8407 the hash table traversal routine. When generating a shared object,
8408 we go through the symbol table twice. The first time we output
8409 anything that might have been forced to local scope in a version
8410 script. The second time we output the symbols that are still
8411 global symbols. */
8413 static bfd_boolean
8414 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8416 struct elf_outext_info *eoinfo = data;
8417 struct elf_final_link_info *finfo = eoinfo->finfo;
8418 bfd_boolean strip;
8419 Elf_Internal_Sym sym;
8420 asection *input_sec;
8421 const struct elf_backend_data *bed;
8423 if (h->root.type == bfd_link_hash_warning)
8425 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8426 if (h->root.type == bfd_link_hash_new)
8427 return TRUE;
8430 /* Decide whether to output this symbol in this pass. */
8431 if (eoinfo->localsyms)
8433 if (!h->forced_local)
8434 return TRUE;
8436 else
8438 if (h->forced_local)
8439 return TRUE;
8442 bed = get_elf_backend_data (finfo->output_bfd);
8444 if (h->root.type == bfd_link_hash_undefined)
8446 /* If we have an undefined symbol reference here then it must have
8447 come from a shared library that is being linked in. (Undefined
8448 references in regular files have already been handled). */
8449 bfd_boolean ignore_undef = FALSE;
8451 /* Some symbols may be special in that the fact that they're
8452 undefined can be safely ignored - let backend determine that. */
8453 if (bed->elf_backend_ignore_undef_symbol)
8454 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8456 /* If we are reporting errors for this situation then do so now. */
8457 if (ignore_undef == FALSE
8458 && h->ref_dynamic
8459 && ! h->ref_regular
8460 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8461 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8463 if (! (finfo->info->callbacks->undefined_symbol
8464 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
8465 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8467 eoinfo->failed = TRUE;
8468 return FALSE;
8473 /* We should also warn if a forced local symbol is referenced from
8474 shared libraries. */
8475 if (! finfo->info->relocatable
8476 && (! finfo->info->shared)
8477 && h->forced_local
8478 && h->ref_dynamic
8479 && !h->dynamic_def
8480 && !h->dynamic_weak
8481 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8483 (*_bfd_error_handler)
8484 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8485 finfo->output_bfd,
8486 h->root.u.def.section == bfd_abs_section_ptr
8487 ? finfo->output_bfd : h->root.u.def.section->owner,
8488 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
8489 ? "internal"
8490 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
8491 ? "hidden" : "local",
8492 h->root.root.string);
8493 eoinfo->failed = TRUE;
8494 return FALSE;
8497 /* We don't want to output symbols that have never been mentioned by
8498 a regular file, or that we have been told to strip. However, if
8499 h->indx is set to -2, the symbol is used by a reloc and we must
8500 output it. */
8501 if (h->indx == -2)
8502 strip = FALSE;
8503 else if ((h->def_dynamic
8504 || h->ref_dynamic
8505 || h->root.type == bfd_link_hash_new)
8506 && !h->def_regular
8507 && !h->ref_regular)
8508 strip = TRUE;
8509 else if (finfo->info->strip == strip_all)
8510 strip = TRUE;
8511 else if (finfo->info->strip == strip_some
8512 && bfd_hash_lookup (finfo->info->keep_hash,
8513 h->root.root.string, FALSE, FALSE) == NULL)
8514 strip = TRUE;
8515 else if (finfo->info->strip_discarded
8516 && (h->root.type == bfd_link_hash_defined
8517 || h->root.type == bfd_link_hash_defweak)
8518 && elf_discarded_section (h->root.u.def.section))
8519 strip = TRUE;
8520 else
8521 strip = FALSE;
8523 /* If we're stripping it, and it's not a dynamic symbol, there's
8524 nothing else to do unless it is a forced local symbol. */
8525 if (strip
8526 && h->dynindx == -1
8527 && !h->forced_local)
8528 return TRUE;
8530 sym.st_value = 0;
8531 sym.st_size = h->size;
8532 sym.st_other = h->other;
8533 if (h->forced_local)
8534 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8535 else if (h->root.type == bfd_link_hash_undefweak
8536 || h->root.type == bfd_link_hash_defweak)
8537 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8538 else
8539 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8541 switch (h->root.type)
8543 default:
8544 case bfd_link_hash_new:
8545 case bfd_link_hash_warning:
8546 abort ();
8547 return FALSE;
8549 case bfd_link_hash_undefined:
8550 case bfd_link_hash_undefweak:
8551 input_sec = bfd_und_section_ptr;
8552 sym.st_shndx = SHN_UNDEF;
8553 break;
8555 case bfd_link_hash_defined:
8556 case bfd_link_hash_defweak:
8558 input_sec = h->root.u.def.section;
8559 if (input_sec->output_section != NULL)
8561 sym.st_shndx =
8562 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8563 input_sec->output_section);
8564 if (sym.st_shndx == SHN_BAD)
8566 (*_bfd_error_handler)
8567 (_("%B: could not find output section %A for input section %A"),
8568 finfo->output_bfd, input_sec->output_section, input_sec);
8569 eoinfo->failed = TRUE;
8570 return FALSE;
8573 /* ELF symbols in relocatable files are section relative,
8574 but in nonrelocatable files they are virtual
8575 addresses. */
8576 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8577 if (! finfo->info->relocatable)
8579 sym.st_value += input_sec->output_section->vma;
8580 if (h->type == STT_TLS)
8582 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8583 if (tls_sec != NULL)
8584 sym.st_value -= tls_sec->vma;
8585 else
8587 /* The TLS section may have been garbage collected. */
8588 BFD_ASSERT (finfo->info->gc_sections
8589 && !input_sec->gc_mark);
8594 else
8596 BFD_ASSERT (input_sec->owner == NULL
8597 || (input_sec->owner->flags & DYNAMIC) != 0);
8598 sym.st_shndx = SHN_UNDEF;
8599 input_sec = bfd_und_section_ptr;
8602 break;
8604 case bfd_link_hash_common:
8605 input_sec = h->root.u.c.p->section;
8606 sym.st_shndx = bed->common_section_index (input_sec);
8607 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8608 break;
8610 case bfd_link_hash_indirect:
8611 /* These symbols are created by symbol versioning. They point
8612 to the decorated version of the name. For example, if the
8613 symbol foo@@GNU_1.2 is the default, which should be used when
8614 foo is used with no version, then we add an indirect symbol
8615 foo which points to foo@@GNU_1.2. We ignore these symbols,
8616 since the indirected symbol is already in the hash table. */
8617 return TRUE;
8620 /* Give the processor backend a chance to tweak the symbol value,
8621 and also to finish up anything that needs to be done for this
8622 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8623 forced local syms when non-shared is due to a historical quirk. */
8624 if ((h->dynindx != -1
8625 || h->forced_local)
8626 && ((finfo->info->shared
8627 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8628 || h->root.type != bfd_link_hash_undefweak))
8629 || !h->forced_local)
8630 && elf_hash_table (finfo->info)->dynamic_sections_created)
8632 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8633 (finfo->output_bfd, finfo->info, h, &sym)))
8635 eoinfo->failed = TRUE;
8636 return FALSE;
8640 /* If we are marking the symbol as undefined, and there are no
8641 non-weak references to this symbol from a regular object, then
8642 mark the symbol as weak undefined; if there are non-weak
8643 references, mark the symbol as strong. We can't do this earlier,
8644 because it might not be marked as undefined until the
8645 finish_dynamic_symbol routine gets through with it. */
8646 if (sym.st_shndx == SHN_UNDEF
8647 && h->ref_regular
8648 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8649 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8651 int bindtype;
8653 if (h->ref_regular_nonweak)
8654 bindtype = STB_GLOBAL;
8655 else
8656 bindtype = STB_WEAK;
8657 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8660 /* If this is a symbol defined in a dynamic library, don't use the
8661 symbol size from the dynamic library. Relinking an executable
8662 against a new library may introduce gratuitous changes in the
8663 executable's symbols if we keep the size. */
8664 if (sym.st_shndx == SHN_UNDEF
8665 && !h->def_regular
8666 && h->def_dynamic)
8667 sym.st_size = 0;
8669 /* If a non-weak symbol with non-default visibility is not defined
8670 locally, it is a fatal error. */
8671 if (! finfo->info->relocatable
8672 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8673 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8674 && h->root.type == bfd_link_hash_undefined
8675 && !h->def_regular)
8677 (*_bfd_error_handler)
8678 (_("%B: %s symbol `%s' isn't defined"),
8679 finfo->output_bfd,
8680 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8681 ? "protected"
8682 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8683 ? "internal" : "hidden",
8684 h->root.root.string);
8685 eoinfo->failed = TRUE;
8686 return FALSE;
8689 /* If this symbol should be put in the .dynsym section, then put it
8690 there now. We already know the symbol index. We also fill in
8691 the entry in the .hash section. */
8692 if (h->dynindx != -1
8693 && elf_hash_table (finfo->info)->dynamic_sections_created)
8695 bfd_byte *esym;
8697 sym.st_name = h->dynstr_index;
8698 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8699 if (! check_dynsym (finfo->output_bfd, &sym))
8701 eoinfo->failed = TRUE;
8702 return FALSE;
8704 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8706 if (finfo->hash_sec != NULL)
8708 size_t hash_entry_size;
8709 bfd_byte *bucketpos;
8710 bfd_vma chain;
8711 size_t bucketcount;
8712 size_t bucket;
8714 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8715 bucket = h->u.elf_hash_value % bucketcount;
8717 hash_entry_size
8718 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8719 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8720 + (bucket + 2) * hash_entry_size);
8721 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8722 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8723 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8724 ((bfd_byte *) finfo->hash_sec->contents
8725 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8728 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8730 Elf_Internal_Versym iversym;
8731 Elf_External_Versym *eversym;
8733 if (!h->def_regular)
8735 if (h->verinfo.verdef == NULL)
8736 iversym.vs_vers = 0;
8737 else
8738 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8740 else
8742 if (h->verinfo.vertree == NULL)
8743 iversym.vs_vers = 1;
8744 else
8745 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8746 if (finfo->info->create_default_symver)
8747 iversym.vs_vers++;
8750 if (h->hidden)
8751 iversym.vs_vers |= VERSYM_HIDDEN;
8753 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8754 eversym += h->dynindx;
8755 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8759 /* If we're stripping it, then it was just a dynamic symbol, and
8760 there's nothing else to do. */
8761 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8762 return TRUE;
8764 h->indx = bfd_get_symcount (finfo->output_bfd);
8766 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8768 eoinfo->failed = TRUE;
8769 return FALSE;
8772 return TRUE;
8775 /* Return TRUE if special handling is done for relocs in SEC against
8776 symbols defined in discarded sections. */
8778 static bfd_boolean
8779 elf_section_ignore_discarded_relocs (asection *sec)
8781 const struct elf_backend_data *bed;
8783 switch (sec->sec_info_type)
8785 case ELF_INFO_TYPE_STABS:
8786 case ELF_INFO_TYPE_EH_FRAME:
8787 return TRUE;
8788 default:
8789 break;
8792 bed = get_elf_backend_data (sec->owner);
8793 if (bed->elf_backend_ignore_discarded_relocs != NULL
8794 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8795 return TRUE;
8797 return FALSE;
8800 /* Return a mask saying how ld should treat relocations in SEC against
8801 symbols defined in discarded sections. If this function returns
8802 COMPLAIN set, ld will issue a warning message. If this function
8803 returns PRETEND set, and the discarded section was link-once and the
8804 same size as the kept link-once section, ld will pretend that the
8805 symbol was actually defined in the kept section. Otherwise ld will
8806 zero the reloc (at least that is the intent, but some cooperation by
8807 the target dependent code is needed, particularly for REL targets). */
8809 unsigned int
8810 _bfd_elf_default_action_discarded (asection *sec)
8812 if (sec->flags & SEC_DEBUGGING)
8813 return PRETEND;
8815 if (strcmp (".eh_frame", sec->name) == 0)
8816 return 0;
8818 if (strcmp (".gcc_except_table", sec->name) == 0)
8819 return 0;
8821 return COMPLAIN | PRETEND;
8824 /* Find a match between a section and a member of a section group. */
8826 static asection *
8827 match_group_member (asection *sec, asection *group,
8828 struct bfd_link_info *info)
8830 asection *first = elf_next_in_group (group);
8831 asection *s = first;
8833 while (s != NULL)
8835 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8836 return s;
8838 s = elf_next_in_group (s);
8839 if (s == first)
8840 break;
8843 return NULL;
8846 /* Check if the kept section of a discarded section SEC can be used
8847 to replace it. Return the replacement if it is OK. Otherwise return
8848 NULL. */
8850 asection *
8851 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8853 asection *kept;
8855 kept = sec->kept_section;
8856 if (kept != NULL)
8858 if ((kept->flags & SEC_GROUP) != 0)
8859 kept = match_group_member (sec, kept, info);
8860 if (kept != NULL
8861 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
8862 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
8863 kept = NULL;
8864 sec->kept_section = kept;
8866 return kept;
8869 /* Link an input file into the linker output file. This function
8870 handles all the sections and relocations of the input file at once.
8871 This is so that we only have to read the local symbols once, and
8872 don't have to keep them in memory. */
8874 static bfd_boolean
8875 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8877 int (*relocate_section)
8878 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8879 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8880 bfd *output_bfd;
8881 Elf_Internal_Shdr *symtab_hdr;
8882 size_t locsymcount;
8883 size_t extsymoff;
8884 Elf_Internal_Sym *isymbuf;
8885 Elf_Internal_Sym *isym;
8886 Elf_Internal_Sym *isymend;
8887 long *pindex;
8888 asection **ppsection;
8889 asection *o;
8890 const struct elf_backend_data *bed;
8891 struct elf_link_hash_entry **sym_hashes;
8893 output_bfd = finfo->output_bfd;
8894 bed = get_elf_backend_data (output_bfd);
8895 relocate_section = bed->elf_backend_relocate_section;
8897 /* If this is a dynamic object, we don't want to do anything here:
8898 we don't want the local symbols, and we don't want the section
8899 contents. */
8900 if ((input_bfd->flags & DYNAMIC) != 0)
8901 return TRUE;
8903 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8904 if (elf_bad_symtab (input_bfd))
8906 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8907 extsymoff = 0;
8909 else
8911 locsymcount = symtab_hdr->sh_info;
8912 extsymoff = symtab_hdr->sh_info;
8915 /* Read the local symbols. */
8916 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8917 if (isymbuf == NULL && locsymcount != 0)
8919 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8920 finfo->internal_syms,
8921 finfo->external_syms,
8922 finfo->locsym_shndx);
8923 if (isymbuf == NULL)
8924 return FALSE;
8927 /* Find local symbol sections and adjust values of symbols in
8928 SEC_MERGE sections. Write out those local symbols we know are
8929 going into the output file. */
8930 isymend = isymbuf + locsymcount;
8931 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8932 isym < isymend;
8933 isym++, pindex++, ppsection++)
8935 asection *isec;
8936 const char *name;
8937 Elf_Internal_Sym osym;
8939 *pindex = -1;
8941 if (elf_bad_symtab (input_bfd))
8943 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8945 *ppsection = NULL;
8946 continue;
8950 if (isym->st_shndx == SHN_UNDEF)
8951 isec = bfd_und_section_ptr;
8952 else if (isym->st_shndx == SHN_ABS)
8953 isec = bfd_abs_section_ptr;
8954 else if (isym->st_shndx == SHN_COMMON)
8955 isec = bfd_com_section_ptr;
8956 else
8958 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8959 if (isec == NULL)
8961 /* Don't attempt to output symbols with st_shnx in the
8962 reserved range other than SHN_ABS and SHN_COMMON. */
8963 *ppsection = NULL;
8964 continue;
8966 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
8967 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8968 isym->st_value =
8969 _bfd_merged_section_offset (output_bfd, &isec,
8970 elf_section_data (isec)->sec_info,
8971 isym->st_value);
8974 *ppsection = isec;
8976 /* Don't output the first, undefined, symbol. */
8977 if (ppsection == finfo->sections)
8978 continue;
8980 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8982 /* We never output section symbols. Instead, we use the
8983 section symbol of the corresponding section in the output
8984 file. */
8985 continue;
8988 /* If we are stripping all symbols, we don't want to output this
8989 one. */
8990 if (finfo->info->strip == strip_all)
8991 continue;
8993 /* If we are discarding all local symbols, we don't want to
8994 output this one. If we are generating a relocatable output
8995 file, then some of the local symbols may be required by
8996 relocs; we output them below as we discover that they are
8997 needed. */
8998 if (finfo->info->discard == discard_all)
8999 continue;
9001 /* If this symbol is defined in a section which we are
9002 discarding, we don't need to keep it. */
9003 if (isym->st_shndx != SHN_UNDEF
9004 && isym->st_shndx < SHN_LORESERVE
9005 && bfd_section_removed_from_list (output_bfd,
9006 isec->output_section))
9007 continue;
9009 /* Get the name of the symbol. */
9010 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9011 isym->st_name);
9012 if (name == NULL)
9013 return FALSE;
9015 /* See if we are discarding symbols with this name. */
9016 if ((finfo->info->strip == strip_some
9017 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9018 == NULL))
9019 || (((finfo->info->discard == discard_sec_merge
9020 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9021 || finfo->info->discard == discard_l)
9022 && bfd_is_local_label_name (input_bfd, name)))
9023 continue;
9025 /* If we get here, we are going to output this symbol. */
9027 osym = *isym;
9029 /* Adjust the section index for the output file. */
9030 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9031 isec->output_section);
9032 if (osym.st_shndx == SHN_BAD)
9033 return FALSE;
9035 *pindex = bfd_get_symcount (output_bfd);
9037 /* ELF symbols in relocatable files are section relative, but
9038 in executable files they are virtual addresses. Note that
9039 this code assumes that all ELF sections have an associated
9040 BFD section with a reasonable value for output_offset; below
9041 we assume that they also have a reasonable value for
9042 output_section. Any special sections must be set up to meet
9043 these requirements. */
9044 osym.st_value += isec->output_offset;
9045 if (! finfo->info->relocatable)
9047 osym.st_value += isec->output_section->vma;
9048 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9050 /* STT_TLS symbols are relative to PT_TLS segment base. */
9051 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9052 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9056 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
9057 return FALSE;
9060 /* Relocate the contents of each section. */
9061 sym_hashes = elf_sym_hashes (input_bfd);
9062 for (o = input_bfd->sections; o != NULL; o = o->next)
9064 bfd_byte *contents;
9066 if (! o->linker_mark)
9068 /* This section was omitted from the link. */
9069 continue;
9072 if (finfo->info->relocatable
9073 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9075 /* Deal with the group signature symbol. */
9076 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9077 unsigned long symndx = sec_data->this_hdr.sh_info;
9078 asection *osec = o->output_section;
9080 if (symndx >= locsymcount
9081 || (elf_bad_symtab (input_bfd)
9082 && finfo->sections[symndx] == NULL))
9084 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9085 while (h->root.type == bfd_link_hash_indirect
9086 || h->root.type == bfd_link_hash_warning)
9087 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9088 /* Arrange for symbol to be output. */
9089 h->indx = -2;
9090 elf_section_data (osec)->this_hdr.sh_info = -2;
9092 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9094 /* We'll use the output section target_index. */
9095 asection *sec = finfo->sections[symndx]->output_section;
9096 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9098 else
9100 if (finfo->indices[symndx] == -1)
9102 /* Otherwise output the local symbol now. */
9103 Elf_Internal_Sym sym = isymbuf[symndx];
9104 asection *sec = finfo->sections[symndx]->output_section;
9105 const char *name;
9107 name = bfd_elf_string_from_elf_section (input_bfd,
9108 symtab_hdr->sh_link,
9109 sym.st_name);
9110 if (name == NULL)
9111 return FALSE;
9113 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9114 sec);
9115 if (sym.st_shndx == SHN_BAD)
9116 return FALSE;
9118 sym.st_value += o->output_offset;
9120 finfo->indices[symndx] = bfd_get_symcount (output_bfd);
9121 if (! elf_link_output_sym (finfo, name, &sym, o, NULL))
9122 return FALSE;
9124 elf_section_data (osec)->this_hdr.sh_info
9125 = finfo->indices[symndx];
9129 if ((o->flags & SEC_HAS_CONTENTS) == 0
9130 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9131 continue;
9133 if ((o->flags & SEC_LINKER_CREATED) != 0)
9135 /* Section was created by _bfd_elf_link_create_dynamic_sections
9136 or somesuch. */
9137 continue;
9140 /* Get the contents of the section. They have been cached by a
9141 relaxation routine. Note that o is a section in an input
9142 file, so the contents field will not have been set by any of
9143 the routines which work on output files. */
9144 if (elf_section_data (o)->this_hdr.contents != NULL)
9145 contents = elf_section_data (o)->this_hdr.contents;
9146 else
9148 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
9150 contents = finfo->contents;
9151 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
9152 return FALSE;
9155 if ((o->flags & SEC_RELOC) != 0)
9157 Elf_Internal_Rela *internal_relocs;
9158 Elf_Internal_Rela *rel, *relend;
9159 bfd_vma r_type_mask;
9160 int r_sym_shift;
9161 int action_discarded;
9162 int ret;
9164 /* Get the swapped relocs. */
9165 internal_relocs
9166 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9167 finfo->internal_relocs, FALSE);
9168 if (internal_relocs == NULL
9169 && o->reloc_count > 0)
9170 return FALSE;
9172 if (bed->s->arch_size == 32)
9174 r_type_mask = 0xff;
9175 r_sym_shift = 8;
9177 else
9179 r_type_mask = 0xffffffff;
9180 r_sym_shift = 32;
9183 action_discarded = -1;
9184 if (!elf_section_ignore_discarded_relocs (o))
9185 action_discarded = (*bed->action_discarded) (o);
9187 /* Run through the relocs evaluating complex reloc symbols and
9188 looking for relocs against symbols from discarded sections
9189 or section symbols from removed link-once sections.
9190 Complain about relocs against discarded sections. Zero
9191 relocs against removed link-once sections. */
9193 rel = internal_relocs;
9194 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9195 for ( ; rel < relend; rel++)
9197 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9198 unsigned int s_type;
9199 asection **ps, *sec;
9200 struct elf_link_hash_entry *h = NULL;
9201 const char *sym_name;
9203 if (r_symndx == STN_UNDEF)
9204 continue;
9206 if (r_symndx >= locsymcount
9207 || (elf_bad_symtab (input_bfd)
9208 && finfo->sections[r_symndx] == NULL))
9210 h = sym_hashes[r_symndx - extsymoff];
9212 /* Badly formatted input files can contain relocs that
9213 reference non-existant symbols. Check here so that
9214 we do not seg fault. */
9215 if (h == NULL)
9217 char buffer [32];
9219 sprintf_vma (buffer, rel->r_info);
9220 (*_bfd_error_handler)
9221 (_("error: %B contains a reloc (0x%s) for section %A "
9222 "that references a non-existent global symbol"),
9223 input_bfd, o, buffer);
9224 bfd_set_error (bfd_error_bad_value);
9225 return FALSE;
9228 while (h->root.type == bfd_link_hash_indirect
9229 || h->root.type == bfd_link_hash_warning)
9230 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9232 s_type = h->type;
9234 ps = NULL;
9235 if (h->root.type == bfd_link_hash_defined
9236 || h->root.type == bfd_link_hash_defweak)
9237 ps = &h->root.u.def.section;
9239 sym_name = h->root.root.string;
9241 else
9243 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9245 s_type = ELF_ST_TYPE (sym->st_info);
9246 ps = &finfo->sections[r_symndx];
9247 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9248 sym, *ps);
9251 if (s_type == STT_RELC || s_type == STT_SRELC)
9253 bfd_vma val;
9254 bfd_vma dot = (rel->r_offset
9255 + o->output_offset + o->output_section->vma);
9256 #ifdef DEBUG
9257 printf ("Encountered a complex symbol!");
9258 printf (" (input_bfd %s, section %s, reloc %ld\n",
9259 input_bfd->filename, o->name, rel - internal_relocs);
9260 printf (" symbol: idx %8.8lx, name %s\n",
9261 r_symndx, sym_name);
9262 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9263 (unsigned long) rel->r_info,
9264 (unsigned long) rel->r_offset);
9265 #endif
9266 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9267 isymbuf, locsymcount, s_type == STT_SRELC))
9268 return FALSE;
9270 /* Symbol evaluated OK. Update to absolute value. */
9271 set_symbol_value (input_bfd, isymbuf, locsymcount,
9272 r_symndx, val);
9273 continue;
9276 if (action_discarded != -1 && ps != NULL)
9278 /* Complain if the definition comes from a
9279 discarded section. */
9280 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9282 BFD_ASSERT (r_symndx != 0);
9283 if (action_discarded & COMPLAIN)
9284 (*finfo->info->callbacks->einfo)
9285 (_("%X`%s' referenced in section `%A' of %B: "
9286 "defined in discarded section `%A' of %B\n"),
9287 sym_name, o, input_bfd, sec, sec->owner);
9289 /* Try to do the best we can to support buggy old
9290 versions of gcc. Pretend that the symbol is
9291 really defined in the kept linkonce section.
9292 FIXME: This is quite broken. Modifying the
9293 symbol here means we will be changing all later
9294 uses of the symbol, not just in this section. */
9295 if (action_discarded & PRETEND)
9297 asection *kept;
9299 kept = _bfd_elf_check_kept_section (sec,
9300 finfo->info);
9301 if (kept != NULL)
9303 *ps = kept;
9304 continue;
9311 /* Relocate the section by invoking a back end routine.
9313 The back end routine is responsible for adjusting the
9314 section contents as necessary, and (if using Rela relocs
9315 and generating a relocatable output file) adjusting the
9316 reloc addend as necessary.
9318 The back end routine does not have to worry about setting
9319 the reloc address or the reloc symbol index.
9321 The back end routine is given a pointer to the swapped in
9322 internal symbols, and can access the hash table entries
9323 for the external symbols via elf_sym_hashes (input_bfd).
9325 When generating relocatable output, the back end routine
9326 must handle STB_LOCAL/STT_SECTION symbols specially. The
9327 output symbol is going to be a section symbol
9328 corresponding to the output section, which will require
9329 the addend to be adjusted. */
9331 ret = (*relocate_section) (output_bfd, finfo->info,
9332 input_bfd, o, contents,
9333 internal_relocs,
9334 isymbuf,
9335 finfo->sections);
9336 if (!ret)
9337 return FALSE;
9339 if (ret == 2
9340 || finfo->info->relocatable
9341 || finfo->info->emitrelocations)
9343 Elf_Internal_Rela *irela;
9344 Elf_Internal_Rela *irelaend;
9345 bfd_vma last_offset;
9346 struct elf_link_hash_entry **rel_hash;
9347 struct elf_link_hash_entry **rel_hash_list;
9348 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
9349 unsigned int next_erel;
9350 bfd_boolean rela_normal;
9352 input_rel_hdr = &elf_section_data (o)->rel_hdr;
9353 rela_normal = (bed->rela_normal
9354 && (input_rel_hdr->sh_entsize
9355 == bed->s->sizeof_rela));
9357 /* Adjust the reloc addresses and symbol indices. */
9359 irela = internal_relocs;
9360 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9361 rel_hash = (elf_section_data (o->output_section)->rel_hashes
9362 + elf_section_data (o->output_section)->rel_count
9363 + elf_section_data (o->output_section)->rel_count2);
9364 rel_hash_list = rel_hash;
9365 last_offset = o->output_offset;
9366 if (!finfo->info->relocatable)
9367 last_offset += o->output_section->vma;
9368 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9370 unsigned long r_symndx;
9371 asection *sec;
9372 Elf_Internal_Sym sym;
9374 if (next_erel == bed->s->int_rels_per_ext_rel)
9376 rel_hash++;
9377 next_erel = 0;
9380 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9381 finfo->info, o,
9382 irela->r_offset);
9383 if (irela->r_offset >= (bfd_vma) -2)
9385 /* This is a reloc for a deleted entry or somesuch.
9386 Turn it into an R_*_NONE reloc, at the same
9387 offset as the last reloc. elf_eh_frame.c and
9388 bfd_elf_discard_info rely on reloc offsets
9389 being ordered. */
9390 irela->r_offset = last_offset;
9391 irela->r_info = 0;
9392 irela->r_addend = 0;
9393 continue;
9396 irela->r_offset += o->output_offset;
9398 /* Relocs in an executable have to be virtual addresses. */
9399 if (!finfo->info->relocatable)
9400 irela->r_offset += o->output_section->vma;
9402 last_offset = irela->r_offset;
9404 r_symndx = irela->r_info >> r_sym_shift;
9405 if (r_symndx == STN_UNDEF)
9406 continue;
9408 if (r_symndx >= locsymcount
9409 || (elf_bad_symtab (input_bfd)
9410 && finfo->sections[r_symndx] == NULL))
9412 struct elf_link_hash_entry *rh;
9413 unsigned long indx;
9415 /* This is a reloc against a global symbol. We
9416 have not yet output all the local symbols, so
9417 we do not know the symbol index of any global
9418 symbol. We set the rel_hash entry for this
9419 reloc to point to the global hash table entry
9420 for this symbol. The symbol index is then
9421 set at the end of bfd_elf_final_link. */
9422 indx = r_symndx - extsymoff;
9423 rh = elf_sym_hashes (input_bfd)[indx];
9424 while (rh->root.type == bfd_link_hash_indirect
9425 || rh->root.type == bfd_link_hash_warning)
9426 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9428 /* Setting the index to -2 tells
9429 elf_link_output_extsym that this symbol is
9430 used by a reloc. */
9431 BFD_ASSERT (rh->indx < 0);
9432 rh->indx = -2;
9434 *rel_hash = rh;
9436 continue;
9439 /* This is a reloc against a local symbol. */
9441 *rel_hash = NULL;
9442 sym = isymbuf[r_symndx];
9443 sec = finfo->sections[r_symndx];
9444 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9446 /* I suppose the backend ought to fill in the
9447 section of any STT_SECTION symbol against a
9448 processor specific section. */
9449 r_symndx = 0;
9450 if (bfd_is_abs_section (sec))
9452 else if (sec == NULL || sec->owner == NULL)
9454 bfd_set_error (bfd_error_bad_value);
9455 return FALSE;
9457 else
9459 asection *osec = sec->output_section;
9461 /* If we have discarded a section, the output
9462 section will be the absolute section. In
9463 case of discarded SEC_MERGE sections, use
9464 the kept section. relocate_section should
9465 have already handled discarded linkonce
9466 sections. */
9467 if (bfd_is_abs_section (osec)
9468 && sec->kept_section != NULL
9469 && sec->kept_section->output_section != NULL)
9471 osec = sec->kept_section->output_section;
9472 irela->r_addend -= osec->vma;
9475 if (!bfd_is_abs_section (osec))
9477 r_symndx = osec->target_index;
9478 if (r_symndx == 0)
9480 struct elf_link_hash_table *htab;
9481 asection *oi;
9483 htab = elf_hash_table (finfo->info);
9484 oi = htab->text_index_section;
9485 if ((osec->flags & SEC_READONLY) == 0
9486 && htab->data_index_section != NULL)
9487 oi = htab->data_index_section;
9489 if (oi != NULL)
9491 irela->r_addend += osec->vma - oi->vma;
9492 r_symndx = oi->target_index;
9496 BFD_ASSERT (r_symndx != 0);
9500 /* Adjust the addend according to where the
9501 section winds up in the output section. */
9502 if (rela_normal)
9503 irela->r_addend += sec->output_offset;
9505 else
9507 if (finfo->indices[r_symndx] == -1)
9509 unsigned long shlink;
9510 const char *name;
9511 asection *osec;
9513 if (finfo->info->strip == strip_all)
9515 /* You can't do ld -r -s. */
9516 bfd_set_error (bfd_error_invalid_operation);
9517 return FALSE;
9520 /* This symbol was skipped earlier, but
9521 since it is needed by a reloc, we
9522 must output it now. */
9523 shlink = symtab_hdr->sh_link;
9524 name = (bfd_elf_string_from_elf_section
9525 (input_bfd, shlink, sym.st_name));
9526 if (name == NULL)
9527 return FALSE;
9529 osec = sec->output_section;
9530 sym.st_shndx =
9531 _bfd_elf_section_from_bfd_section (output_bfd,
9532 osec);
9533 if (sym.st_shndx == SHN_BAD)
9534 return FALSE;
9536 sym.st_value += sec->output_offset;
9537 if (! finfo->info->relocatable)
9539 sym.st_value += osec->vma;
9540 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9542 /* STT_TLS symbols are relative to PT_TLS
9543 segment base. */
9544 BFD_ASSERT (elf_hash_table (finfo->info)
9545 ->tls_sec != NULL);
9546 sym.st_value -= (elf_hash_table (finfo->info)
9547 ->tls_sec->vma);
9551 finfo->indices[r_symndx]
9552 = bfd_get_symcount (output_bfd);
9554 if (! elf_link_output_sym (finfo, name, &sym, sec,
9555 NULL))
9556 return FALSE;
9559 r_symndx = finfo->indices[r_symndx];
9562 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9563 | (irela->r_info & r_type_mask));
9566 /* Swap out the relocs. */
9567 if (input_rel_hdr->sh_size != 0
9568 && !bed->elf_backend_emit_relocs (output_bfd, o,
9569 input_rel_hdr,
9570 internal_relocs,
9571 rel_hash_list))
9572 return FALSE;
9574 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
9575 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
9577 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9578 * bed->s->int_rels_per_ext_rel);
9579 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9580 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9581 input_rel_hdr2,
9582 internal_relocs,
9583 rel_hash_list))
9584 return FALSE;
9589 /* Write out the modified section contents. */
9590 if (bed->elf_backend_write_section
9591 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9592 contents))
9594 /* Section written out. */
9596 else switch (o->sec_info_type)
9598 case ELF_INFO_TYPE_STABS:
9599 if (! (_bfd_write_section_stabs
9600 (output_bfd,
9601 &elf_hash_table (finfo->info)->stab_info,
9602 o, &elf_section_data (o)->sec_info, contents)))
9603 return FALSE;
9604 break;
9605 case ELF_INFO_TYPE_MERGE:
9606 if (! _bfd_write_merged_section (output_bfd, o,
9607 elf_section_data (o)->sec_info))
9608 return FALSE;
9609 break;
9610 case ELF_INFO_TYPE_EH_FRAME:
9612 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9613 o, contents))
9614 return FALSE;
9616 break;
9617 default:
9619 if (! (o->flags & SEC_EXCLUDE)
9620 && ! (o->output_section->flags & SEC_NEVER_LOAD)
9621 && ! bfd_set_section_contents (output_bfd, o->output_section,
9622 contents,
9623 (file_ptr) o->output_offset,
9624 o->size))
9625 return FALSE;
9627 break;
9631 return TRUE;
9634 /* Generate a reloc when linking an ELF file. This is a reloc
9635 requested by the linker, and does not come from any input file. This
9636 is used to build constructor and destructor tables when linking
9637 with -Ur. */
9639 static bfd_boolean
9640 elf_reloc_link_order (bfd *output_bfd,
9641 struct bfd_link_info *info,
9642 asection *output_section,
9643 struct bfd_link_order *link_order)
9645 reloc_howto_type *howto;
9646 long indx;
9647 bfd_vma offset;
9648 bfd_vma addend;
9649 struct elf_link_hash_entry **rel_hash_ptr;
9650 Elf_Internal_Shdr *rel_hdr;
9651 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9652 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9653 bfd_byte *erel;
9654 unsigned int i;
9656 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9657 if (howto == NULL)
9659 bfd_set_error (bfd_error_bad_value);
9660 return FALSE;
9663 addend = link_order->u.reloc.p->addend;
9665 /* Figure out the symbol index. */
9666 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
9667 + elf_section_data (output_section)->rel_count
9668 + elf_section_data (output_section)->rel_count2);
9669 if (link_order->type == bfd_section_reloc_link_order)
9671 indx = link_order->u.reloc.p->u.section->target_index;
9672 BFD_ASSERT (indx != 0);
9673 *rel_hash_ptr = NULL;
9675 else
9677 struct elf_link_hash_entry *h;
9679 /* Treat a reloc against a defined symbol as though it were
9680 actually against the section. */
9681 h = ((struct elf_link_hash_entry *)
9682 bfd_wrapped_link_hash_lookup (output_bfd, info,
9683 link_order->u.reloc.p->u.name,
9684 FALSE, FALSE, TRUE));
9685 if (h != NULL
9686 && (h->root.type == bfd_link_hash_defined
9687 || h->root.type == bfd_link_hash_defweak))
9689 asection *section;
9691 section = h->root.u.def.section;
9692 indx = section->output_section->target_index;
9693 *rel_hash_ptr = NULL;
9694 /* It seems that we ought to add the symbol value to the
9695 addend here, but in practice it has already been added
9696 because it was passed to constructor_callback. */
9697 addend += section->output_section->vma + section->output_offset;
9699 else if (h != NULL)
9701 /* Setting the index to -2 tells elf_link_output_extsym that
9702 this symbol is used by a reloc. */
9703 h->indx = -2;
9704 *rel_hash_ptr = h;
9705 indx = 0;
9707 else
9709 if (! ((*info->callbacks->unattached_reloc)
9710 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9711 return FALSE;
9712 indx = 0;
9716 /* If this is an inplace reloc, we must write the addend into the
9717 object file. */
9718 if (howto->partial_inplace && addend != 0)
9720 bfd_size_type size;
9721 bfd_reloc_status_type rstat;
9722 bfd_byte *buf;
9723 bfd_boolean ok;
9724 const char *sym_name;
9726 size = bfd_get_reloc_size (howto);
9727 buf = bfd_zmalloc (size);
9728 if (buf == NULL)
9729 return FALSE;
9730 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9731 switch (rstat)
9733 case bfd_reloc_ok:
9734 break;
9736 default:
9737 case bfd_reloc_outofrange:
9738 abort ();
9740 case bfd_reloc_overflow:
9741 if (link_order->type == bfd_section_reloc_link_order)
9742 sym_name = bfd_section_name (output_bfd,
9743 link_order->u.reloc.p->u.section);
9744 else
9745 sym_name = link_order->u.reloc.p->u.name;
9746 if (! ((*info->callbacks->reloc_overflow)
9747 (info, NULL, sym_name, howto->name, addend, NULL,
9748 NULL, (bfd_vma) 0)))
9750 free (buf);
9751 return FALSE;
9753 break;
9755 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9756 link_order->offset, size);
9757 free (buf);
9758 if (! ok)
9759 return FALSE;
9762 /* The address of a reloc is relative to the section in a
9763 relocatable file, and is a virtual address in an executable
9764 file. */
9765 offset = link_order->offset;
9766 if (! info->relocatable)
9767 offset += output_section->vma;
9769 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9771 irel[i].r_offset = offset;
9772 irel[i].r_info = 0;
9773 irel[i].r_addend = 0;
9775 if (bed->s->arch_size == 32)
9776 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9777 else
9778 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9780 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9781 erel = rel_hdr->contents;
9782 if (rel_hdr->sh_type == SHT_REL)
9784 erel += (elf_section_data (output_section)->rel_count
9785 * bed->s->sizeof_rel);
9786 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9788 else
9790 irel[0].r_addend = addend;
9791 erel += (elf_section_data (output_section)->rel_count
9792 * bed->s->sizeof_rela);
9793 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9796 ++elf_section_data (output_section)->rel_count;
9798 return TRUE;
9802 /* Get the output vma of the section pointed to by the sh_link field. */
9804 static bfd_vma
9805 elf_get_linked_section_vma (struct bfd_link_order *p)
9807 Elf_Internal_Shdr **elf_shdrp;
9808 asection *s;
9809 int elfsec;
9811 s = p->u.indirect.section;
9812 elf_shdrp = elf_elfsections (s->owner);
9813 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9814 elfsec = elf_shdrp[elfsec]->sh_link;
9815 /* PR 290:
9816 The Intel C compiler generates SHT_IA_64_UNWIND with
9817 SHF_LINK_ORDER. But it doesn't set the sh_link or
9818 sh_info fields. Hence we could get the situation
9819 where elfsec is 0. */
9820 if (elfsec == 0)
9822 const struct elf_backend_data *bed
9823 = get_elf_backend_data (s->owner);
9824 if (bed->link_order_error_handler)
9825 bed->link_order_error_handler
9826 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
9827 return 0;
9829 else
9831 s = elf_shdrp[elfsec]->bfd_section;
9832 return s->output_section->vma + s->output_offset;
9837 /* Compare two sections based on the locations of the sections they are
9838 linked to. Used by elf_fixup_link_order. */
9840 static int
9841 compare_link_order (const void * a, const void * b)
9843 bfd_vma apos;
9844 bfd_vma bpos;
9846 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9847 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9848 if (apos < bpos)
9849 return -1;
9850 return apos > bpos;
9854 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9855 order as their linked sections. Returns false if this could not be done
9856 because an output section includes both ordered and unordered
9857 sections. Ideally we'd do this in the linker proper. */
9859 static bfd_boolean
9860 elf_fixup_link_order (bfd *abfd, asection *o)
9862 int seen_linkorder;
9863 int seen_other;
9864 int n;
9865 struct bfd_link_order *p;
9866 bfd *sub;
9867 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9868 unsigned elfsec;
9869 struct bfd_link_order **sections;
9870 asection *s, *other_sec, *linkorder_sec;
9871 bfd_vma offset;
9873 other_sec = NULL;
9874 linkorder_sec = NULL;
9875 seen_other = 0;
9876 seen_linkorder = 0;
9877 for (p = o->map_head.link_order; p != NULL; p = p->next)
9879 if (p->type == bfd_indirect_link_order)
9881 s = p->u.indirect.section;
9882 sub = s->owner;
9883 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9884 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
9885 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9886 && elfsec < elf_numsections (sub)
9887 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
9888 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
9890 seen_linkorder++;
9891 linkorder_sec = s;
9893 else
9895 seen_other++;
9896 other_sec = s;
9899 else
9900 seen_other++;
9902 if (seen_other && seen_linkorder)
9904 if (other_sec && linkorder_sec)
9905 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9906 o, linkorder_sec,
9907 linkorder_sec->owner, other_sec,
9908 other_sec->owner);
9909 else
9910 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9912 bfd_set_error (bfd_error_bad_value);
9913 return FALSE;
9917 if (!seen_linkorder)
9918 return TRUE;
9920 sections = (struct bfd_link_order **)
9921 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
9922 if (sections == NULL)
9923 return FALSE;
9924 seen_linkorder = 0;
9926 for (p = o->map_head.link_order; p != NULL; p = p->next)
9928 sections[seen_linkorder++] = p;
9930 /* Sort the input sections in the order of their linked section. */
9931 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9932 compare_link_order);
9934 /* Change the offsets of the sections. */
9935 offset = 0;
9936 for (n = 0; n < seen_linkorder; n++)
9938 s = sections[n]->u.indirect.section;
9939 offset &= ~(bfd_vma) 0 << s->alignment_power;
9940 s->output_offset = offset;
9941 sections[n]->offset = offset;
9942 offset += sections[n]->size;
9945 free (sections);
9946 return TRUE;
9950 /* Do the final step of an ELF link. */
9952 bfd_boolean
9953 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9955 bfd_boolean dynamic;
9956 bfd_boolean emit_relocs;
9957 bfd *dynobj;
9958 struct elf_final_link_info finfo;
9959 register asection *o;
9960 register struct bfd_link_order *p;
9961 register bfd *sub;
9962 bfd_size_type max_contents_size;
9963 bfd_size_type max_external_reloc_size;
9964 bfd_size_type max_internal_reloc_count;
9965 bfd_size_type max_sym_count;
9966 bfd_size_type max_sym_shndx_count;
9967 file_ptr off;
9968 Elf_Internal_Sym elfsym;
9969 unsigned int i;
9970 Elf_Internal_Shdr *symtab_hdr;
9971 Elf_Internal_Shdr *symtab_shndx_hdr;
9972 Elf_Internal_Shdr *symstrtab_hdr;
9973 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9974 struct elf_outext_info eoinfo;
9975 bfd_boolean merged;
9976 size_t relativecount = 0;
9977 asection *reldyn = 0;
9978 bfd_size_type amt;
9979 asection *attr_section = NULL;
9980 bfd_vma attr_size = 0;
9981 const char *std_attrs_section;
9983 if (! is_elf_hash_table (info->hash))
9984 return FALSE;
9986 if (info->shared)
9987 abfd->flags |= DYNAMIC;
9989 dynamic = elf_hash_table (info)->dynamic_sections_created;
9990 dynobj = elf_hash_table (info)->dynobj;
9992 emit_relocs = (info->relocatable
9993 || info->emitrelocations);
9995 finfo.info = info;
9996 finfo.output_bfd = abfd;
9997 finfo.symstrtab = _bfd_elf_stringtab_init ();
9998 if (finfo.symstrtab == NULL)
9999 return FALSE;
10001 if (! dynamic)
10003 finfo.dynsym_sec = NULL;
10004 finfo.hash_sec = NULL;
10005 finfo.symver_sec = NULL;
10007 else
10009 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10010 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
10011 BFD_ASSERT (finfo.dynsym_sec != NULL);
10012 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10013 /* Note that it is OK if symver_sec is NULL. */
10016 finfo.contents = NULL;
10017 finfo.external_relocs = NULL;
10018 finfo.internal_relocs = NULL;
10019 finfo.external_syms = NULL;
10020 finfo.locsym_shndx = NULL;
10021 finfo.internal_syms = NULL;
10022 finfo.indices = NULL;
10023 finfo.sections = NULL;
10024 finfo.symbuf = NULL;
10025 finfo.symshndxbuf = NULL;
10026 finfo.symbuf_count = 0;
10027 finfo.shndxbuf_size = 0;
10029 /* The object attributes have been merged. Remove the input
10030 sections from the link, and set the contents of the output
10031 secton. */
10032 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10033 for (o = abfd->sections; o != NULL; o = o->next)
10035 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10036 || strcmp (o->name, ".gnu.attributes") == 0)
10038 for (p = o->map_head.link_order; p != NULL; p = p->next)
10040 asection *input_section;
10042 if (p->type != bfd_indirect_link_order)
10043 continue;
10044 input_section = p->u.indirect.section;
10045 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10046 elf_link_input_bfd ignores this section. */
10047 input_section->flags &= ~SEC_HAS_CONTENTS;
10050 attr_size = bfd_elf_obj_attr_size (abfd);
10051 if (attr_size)
10053 bfd_set_section_size (abfd, o, attr_size);
10054 attr_section = o;
10055 /* Skip this section later on. */
10056 o->map_head.link_order = NULL;
10058 else
10059 o->flags |= SEC_EXCLUDE;
10063 /* Count up the number of relocations we will output for each output
10064 section, so that we know the sizes of the reloc sections. We
10065 also figure out some maximum sizes. */
10066 max_contents_size = 0;
10067 max_external_reloc_size = 0;
10068 max_internal_reloc_count = 0;
10069 max_sym_count = 0;
10070 max_sym_shndx_count = 0;
10071 merged = FALSE;
10072 for (o = abfd->sections; o != NULL; o = o->next)
10074 struct bfd_elf_section_data *esdo = elf_section_data (o);
10075 o->reloc_count = 0;
10077 for (p = o->map_head.link_order; p != NULL; p = p->next)
10079 unsigned int reloc_count = 0;
10080 struct bfd_elf_section_data *esdi = NULL;
10081 unsigned int *rel_count1;
10083 if (p->type == bfd_section_reloc_link_order
10084 || p->type == bfd_symbol_reloc_link_order)
10085 reloc_count = 1;
10086 else if (p->type == bfd_indirect_link_order)
10088 asection *sec;
10090 sec = p->u.indirect.section;
10091 esdi = elf_section_data (sec);
10093 /* Mark all sections which are to be included in the
10094 link. This will normally be every section. We need
10095 to do this so that we can identify any sections which
10096 the linker has decided to not include. */
10097 sec->linker_mark = TRUE;
10099 if (sec->flags & SEC_MERGE)
10100 merged = TRUE;
10102 if (info->relocatable || info->emitrelocations)
10103 reloc_count = sec->reloc_count;
10104 else if (bed->elf_backend_count_relocs)
10105 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10107 if (sec->rawsize > max_contents_size)
10108 max_contents_size = sec->rawsize;
10109 if (sec->size > max_contents_size)
10110 max_contents_size = sec->size;
10112 /* We are interested in just local symbols, not all
10113 symbols. */
10114 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10115 && (sec->owner->flags & DYNAMIC) == 0)
10117 size_t sym_count;
10119 if (elf_bad_symtab (sec->owner))
10120 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10121 / bed->s->sizeof_sym);
10122 else
10123 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10125 if (sym_count > max_sym_count)
10126 max_sym_count = sym_count;
10128 if (sym_count > max_sym_shndx_count
10129 && elf_symtab_shndx (sec->owner) != 0)
10130 max_sym_shndx_count = sym_count;
10132 if ((sec->flags & SEC_RELOC) != 0)
10134 size_t ext_size;
10136 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
10137 if (ext_size > max_external_reloc_size)
10138 max_external_reloc_size = ext_size;
10139 if (sec->reloc_count > max_internal_reloc_count)
10140 max_internal_reloc_count = sec->reloc_count;
10145 if (reloc_count == 0)
10146 continue;
10148 o->reloc_count += reloc_count;
10150 /* MIPS may have a mix of REL and RELA relocs on sections.
10151 To support this curious ABI we keep reloc counts in
10152 elf_section_data too. We must be careful to add the
10153 relocations from the input section to the right output
10154 count. FIXME: Get rid of one count. We have
10155 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10156 rel_count1 = &esdo->rel_count;
10157 if (esdi != NULL)
10159 bfd_boolean same_size;
10160 bfd_size_type entsize1;
10162 entsize1 = esdi->rel_hdr.sh_entsize;
10163 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
10164 || entsize1 == bed->s->sizeof_rela);
10165 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
10167 if (!same_size)
10168 rel_count1 = &esdo->rel_count2;
10170 if (esdi->rel_hdr2 != NULL)
10172 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
10173 unsigned int alt_count;
10174 unsigned int *rel_count2;
10176 BFD_ASSERT (entsize2 != entsize1
10177 && (entsize2 == bed->s->sizeof_rel
10178 || entsize2 == bed->s->sizeof_rela));
10180 rel_count2 = &esdo->rel_count2;
10181 if (!same_size)
10182 rel_count2 = &esdo->rel_count;
10184 /* The following is probably too simplistic if the
10185 backend counts output relocs unusually. */
10186 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
10187 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
10188 *rel_count2 += alt_count;
10189 reloc_count -= alt_count;
10192 *rel_count1 += reloc_count;
10195 if (o->reloc_count > 0)
10196 o->flags |= SEC_RELOC;
10197 else
10199 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10200 set it (this is probably a bug) and if it is set
10201 assign_section_numbers will create a reloc section. */
10202 o->flags &=~ SEC_RELOC;
10205 /* If the SEC_ALLOC flag is not set, force the section VMA to
10206 zero. This is done in elf_fake_sections as well, but forcing
10207 the VMA to 0 here will ensure that relocs against these
10208 sections are handled correctly. */
10209 if ((o->flags & SEC_ALLOC) == 0
10210 && ! o->user_set_vma)
10211 o->vma = 0;
10214 if (! info->relocatable && merged)
10215 elf_link_hash_traverse (elf_hash_table (info),
10216 _bfd_elf_link_sec_merge_syms, abfd);
10218 /* Figure out the file positions for everything but the symbol table
10219 and the relocs. We set symcount to force assign_section_numbers
10220 to create a symbol table. */
10221 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10222 BFD_ASSERT (! abfd->output_has_begun);
10223 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10224 goto error_return;
10226 /* Set sizes, and assign file positions for reloc sections. */
10227 for (o = abfd->sections; o != NULL; o = o->next)
10229 if ((o->flags & SEC_RELOC) != 0)
10231 if (!(_bfd_elf_link_size_reloc_section
10232 (abfd, &elf_section_data (o)->rel_hdr, o)))
10233 goto error_return;
10235 if (elf_section_data (o)->rel_hdr2
10236 && !(_bfd_elf_link_size_reloc_section
10237 (abfd, elf_section_data (o)->rel_hdr2, o)))
10238 goto error_return;
10241 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10242 to count upwards while actually outputting the relocations. */
10243 elf_section_data (o)->rel_count = 0;
10244 elf_section_data (o)->rel_count2 = 0;
10247 _bfd_elf_assign_file_positions_for_relocs (abfd);
10249 /* We have now assigned file positions for all the sections except
10250 .symtab and .strtab. We start the .symtab section at the current
10251 file position, and write directly to it. We build the .strtab
10252 section in memory. */
10253 bfd_get_symcount (abfd) = 0;
10254 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10255 /* sh_name is set in prep_headers. */
10256 symtab_hdr->sh_type = SHT_SYMTAB;
10257 /* sh_flags, sh_addr and sh_size all start off zero. */
10258 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10259 /* sh_link is set in assign_section_numbers. */
10260 /* sh_info is set below. */
10261 /* sh_offset is set just below. */
10262 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
10264 off = elf_tdata (abfd)->next_file_pos;
10265 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10267 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10268 incorrect. We do not yet know the size of the .symtab section.
10269 We correct next_file_pos below, after we do know the size. */
10271 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10272 continuously seeking to the right position in the file. */
10273 if (! info->keep_memory || max_sym_count < 20)
10274 finfo.symbuf_size = 20;
10275 else
10276 finfo.symbuf_size = max_sym_count;
10277 amt = finfo.symbuf_size;
10278 amt *= bed->s->sizeof_sym;
10279 finfo.symbuf = bfd_malloc (amt);
10280 if (finfo.symbuf == NULL)
10281 goto error_return;
10282 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
10284 /* Wild guess at number of output symbols. realloc'd as needed. */
10285 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10286 finfo.shndxbuf_size = amt;
10287 amt *= sizeof (Elf_External_Sym_Shndx);
10288 finfo.symshndxbuf = bfd_zmalloc (amt);
10289 if (finfo.symshndxbuf == NULL)
10290 goto error_return;
10293 /* Start writing out the symbol table. The first symbol is always a
10294 dummy symbol. */
10295 if (info->strip != strip_all
10296 || emit_relocs)
10298 elfsym.st_value = 0;
10299 elfsym.st_size = 0;
10300 elfsym.st_info = 0;
10301 elfsym.st_other = 0;
10302 elfsym.st_shndx = SHN_UNDEF;
10303 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10304 NULL))
10305 goto error_return;
10308 /* Output a symbol for each section. We output these even if we are
10309 discarding local symbols, since they are used for relocs. These
10310 symbols have no names. We store the index of each one in the
10311 index field of the section, so that we can find it again when
10312 outputting relocs. */
10313 if (info->strip != strip_all
10314 || emit_relocs)
10316 elfsym.st_size = 0;
10317 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10318 elfsym.st_other = 0;
10319 elfsym.st_value = 0;
10320 for (i = 1; i < elf_numsections (abfd); i++)
10322 o = bfd_section_from_elf_index (abfd, i);
10323 if (o != NULL)
10325 o->target_index = bfd_get_symcount (abfd);
10326 elfsym.st_shndx = i;
10327 if (!info->relocatable)
10328 elfsym.st_value = o->vma;
10329 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
10330 goto error_return;
10335 /* Allocate some memory to hold information read in from the input
10336 files. */
10337 if (max_contents_size != 0)
10339 finfo.contents = bfd_malloc (max_contents_size);
10340 if (finfo.contents == NULL)
10341 goto error_return;
10344 if (max_external_reloc_size != 0)
10346 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10347 if (finfo.external_relocs == NULL)
10348 goto error_return;
10351 if (max_internal_reloc_count != 0)
10353 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10354 amt *= sizeof (Elf_Internal_Rela);
10355 finfo.internal_relocs = bfd_malloc (amt);
10356 if (finfo.internal_relocs == NULL)
10357 goto error_return;
10360 if (max_sym_count != 0)
10362 amt = max_sym_count * bed->s->sizeof_sym;
10363 finfo.external_syms = bfd_malloc (amt);
10364 if (finfo.external_syms == NULL)
10365 goto error_return;
10367 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10368 finfo.internal_syms = bfd_malloc (amt);
10369 if (finfo.internal_syms == NULL)
10370 goto error_return;
10372 amt = max_sym_count * sizeof (long);
10373 finfo.indices = bfd_malloc (amt);
10374 if (finfo.indices == NULL)
10375 goto error_return;
10377 amt = max_sym_count * sizeof (asection *);
10378 finfo.sections = bfd_malloc (amt);
10379 if (finfo.sections == NULL)
10380 goto error_return;
10383 if (max_sym_shndx_count != 0)
10385 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10386 finfo.locsym_shndx = bfd_malloc (amt);
10387 if (finfo.locsym_shndx == NULL)
10388 goto error_return;
10391 if (elf_hash_table (info)->tls_sec)
10393 bfd_vma base, end = 0;
10394 asection *sec;
10396 for (sec = elf_hash_table (info)->tls_sec;
10397 sec && (sec->flags & SEC_THREAD_LOCAL);
10398 sec = sec->next)
10400 bfd_size_type size = sec->size;
10402 if (size == 0
10403 && (sec->flags & SEC_HAS_CONTENTS) == 0)
10405 struct bfd_link_order *o = sec->map_tail.link_order;
10406 if (o != NULL)
10407 size = o->offset + o->size;
10409 end = sec->vma + size;
10411 base = elf_hash_table (info)->tls_sec->vma;
10412 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
10413 elf_hash_table (info)->tls_size = end - base;
10416 /* Reorder SHF_LINK_ORDER sections. */
10417 for (o = abfd->sections; o != NULL; o = o->next)
10419 if (!elf_fixup_link_order (abfd, o))
10420 return FALSE;
10423 /* Since ELF permits relocations to be against local symbols, we
10424 must have the local symbols available when we do the relocations.
10425 Since we would rather only read the local symbols once, and we
10426 would rather not keep them in memory, we handle all the
10427 relocations for a single input file at the same time.
10429 Unfortunately, there is no way to know the total number of local
10430 symbols until we have seen all of them, and the local symbol
10431 indices precede the global symbol indices. This means that when
10432 we are generating relocatable output, and we see a reloc against
10433 a global symbol, we can not know the symbol index until we have
10434 finished examining all the local symbols to see which ones we are
10435 going to output. To deal with this, we keep the relocations in
10436 memory, and don't output them until the end of the link. This is
10437 an unfortunate waste of memory, but I don't see a good way around
10438 it. Fortunately, it only happens when performing a relocatable
10439 link, which is not the common case. FIXME: If keep_memory is set
10440 we could write the relocs out and then read them again; I don't
10441 know how bad the memory loss will be. */
10443 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10444 sub->output_has_begun = FALSE;
10445 for (o = abfd->sections; o != NULL; o = o->next)
10447 for (p = o->map_head.link_order; p != NULL; p = p->next)
10449 if (p->type == bfd_indirect_link_order
10450 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10451 == bfd_target_elf_flavour)
10452 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10454 if (! sub->output_has_begun)
10456 if (! elf_link_input_bfd (&finfo, sub))
10457 goto error_return;
10458 sub->output_has_begun = TRUE;
10461 else if (p->type == bfd_section_reloc_link_order
10462 || p->type == bfd_symbol_reloc_link_order)
10464 if (! elf_reloc_link_order (abfd, info, o, p))
10465 goto error_return;
10467 else
10469 if (! _bfd_default_link_order (abfd, info, o, p))
10470 goto error_return;
10475 /* Free symbol buffer if needed. */
10476 if (!info->reduce_memory_overheads)
10478 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10479 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10480 && elf_tdata (sub)->symbuf)
10482 free (elf_tdata (sub)->symbuf);
10483 elf_tdata (sub)->symbuf = NULL;
10487 /* Output any global symbols that got converted to local in a
10488 version script or due to symbol visibility. We do this in a
10489 separate step since ELF requires all local symbols to appear
10490 prior to any global symbols. FIXME: We should only do this if
10491 some global symbols were, in fact, converted to become local.
10492 FIXME: Will this work correctly with the Irix 5 linker? */
10493 eoinfo.failed = FALSE;
10494 eoinfo.finfo = &finfo;
10495 eoinfo.localsyms = TRUE;
10496 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10497 &eoinfo);
10498 if (eoinfo.failed)
10499 return FALSE;
10501 /* If backend needs to output some local symbols not present in the hash
10502 table, do it now. */
10503 if (bed->elf_backend_output_arch_local_syms)
10505 typedef bfd_boolean (*out_sym_func)
10506 (void *, const char *, Elf_Internal_Sym *, asection *,
10507 struct elf_link_hash_entry *);
10509 if (! ((*bed->elf_backend_output_arch_local_syms)
10510 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10511 return FALSE;
10514 /* That wrote out all the local symbols. Finish up the symbol table
10515 with the global symbols. Even if we want to strip everything we
10516 can, we still need to deal with those global symbols that got
10517 converted to local in a version script. */
10519 /* The sh_info field records the index of the first non local symbol. */
10520 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10522 if (dynamic
10523 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10525 Elf_Internal_Sym sym;
10526 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10527 long last_local = 0;
10529 /* Write out the section symbols for the output sections. */
10530 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10532 asection *s;
10534 sym.st_size = 0;
10535 sym.st_name = 0;
10536 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10537 sym.st_other = 0;
10539 for (s = abfd->sections; s != NULL; s = s->next)
10541 int indx;
10542 bfd_byte *dest;
10543 long dynindx;
10545 dynindx = elf_section_data (s)->dynindx;
10546 if (dynindx <= 0)
10547 continue;
10548 indx = elf_section_data (s)->this_idx;
10549 BFD_ASSERT (indx > 0);
10550 sym.st_shndx = indx;
10551 if (! check_dynsym (abfd, &sym))
10552 return FALSE;
10553 sym.st_value = s->vma;
10554 dest = dynsym + dynindx * bed->s->sizeof_sym;
10555 if (last_local < dynindx)
10556 last_local = dynindx;
10557 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10561 /* Write out the local dynsyms. */
10562 if (elf_hash_table (info)->dynlocal)
10564 struct elf_link_local_dynamic_entry *e;
10565 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10567 asection *s;
10568 bfd_byte *dest;
10570 sym.st_size = e->isym.st_size;
10571 sym.st_other = e->isym.st_other;
10573 /* Copy the internal symbol as is.
10574 Note that we saved a word of storage and overwrote
10575 the original st_name with the dynstr_index. */
10576 sym = e->isym;
10578 s = bfd_section_from_elf_index (e->input_bfd,
10579 e->isym.st_shndx);
10580 if (s != NULL)
10582 sym.st_shndx =
10583 elf_section_data (s->output_section)->this_idx;
10584 if (! check_dynsym (abfd, &sym))
10585 return FALSE;
10586 sym.st_value = (s->output_section->vma
10587 + s->output_offset
10588 + e->isym.st_value);
10591 if (last_local < e->dynindx)
10592 last_local = e->dynindx;
10594 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10595 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10599 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10600 last_local + 1;
10603 /* We get the global symbols from the hash table. */
10604 eoinfo.failed = FALSE;
10605 eoinfo.localsyms = FALSE;
10606 eoinfo.finfo = &finfo;
10607 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10608 &eoinfo);
10609 if (eoinfo.failed)
10610 return FALSE;
10612 /* If backend needs to output some symbols not present in the hash
10613 table, do it now. */
10614 if (bed->elf_backend_output_arch_syms)
10616 typedef bfd_boolean (*out_sym_func)
10617 (void *, const char *, Elf_Internal_Sym *, asection *,
10618 struct elf_link_hash_entry *);
10620 if (! ((*bed->elf_backend_output_arch_syms)
10621 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10622 return FALSE;
10625 /* Flush all symbols to the file. */
10626 if (! elf_link_flush_output_syms (&finfo, bed))
10627 return FALSE;
10629 /* Now we know the size of the symtab section. */
10630 off += symtab_hdr->sh_size;
10632 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10633 if (symtab_shndx_hdr->sh_name != 0)
10635 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10636 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10637 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10638 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10639 symtab_shndx_hdr->sh_size = amt;
10641 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10642 off, TRUE);
10644 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10645 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10646 return FALSE;
10650 /* Finish up and write out the symbol string table (.strtab)
10651 section. */
10652 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10653 /* sh_name was set in prep_headers. */
10654 symstrtab_hdr->sh_type = SHT_STRTAB;
10655 symstrtab_hdr->sh_flags = 0;
10656 symstrtab_hdr->sh_addr = 0;
10657 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10658 symstrtab_hdr->sh_entsize = 0;
10659 symstrtab_hdr->sh_link = 0;
10660 symstrtab_hdr->sh_info = 0;
10661 /* sh_offset is set just below. */
10662 symstrtab_hdr->sh_addralign = 1;
10664 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10665 elf_tdata (abfd)->next_file_pos = off;
10667 if (bfd_get_symcount (abfd) > 0)
10669 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10670 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10671 return FALSE;
10674 /* Adjust the relocs to have the correct symbol indices. */
10675 for (o = abfd->sections; o != NULL; o = o->next)
10677 if ((o->flags & SEC_RELOC) == 0)
10678 continue;
10680 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10681 elf_section_data (o)->rel_count,
10682 elf_section_data (o)->rel_hashes);
10683 if (elf_section_data (o)->rel_hdr2 != NULL)
10684 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10685 elf_section_data (o)->rel_count2,
10686 (elf_section_data (o)->rel_hashes
10687 + elf_section_data (o)->rel_count));
10689 /* Set the reloc_count field to 0 to prevent write_relocs from
10690 trying to swap the relocs out itself. */
10691 o->reloc_count = 0;
10694 if (dynamic && info->combreloc && dynobj != NULL)
10695 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10697 /* If we are linking against a dynamic object, or generating a
10698 shared library, finish up the dynamic linking information. */
10699 if (dynamic)
10701 bfd_byte *dyncon, *dynconend;
10703 /* Fix up .dynamic entries. */
10704 o = bfd_get_section_by_name (dynobj, ".dynamic");
10705 BFD_ASSERT (o != NULL);
10707 dyncon = o->contents;
10708 dynconend = o->contents + o->size;
10709 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10711 Elf_Internal_Dyn dyn;
10712 const char *name;
10713 unsigned int type;
10715 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10717 switch (dyn.d_tag)
10719 default:
10720 continue;
10721 case DT_NULL:
10722 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10724 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10726 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10727 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10728 default: continue;
10730 dyn.d_un.d_val = relativecount;
10731 relativecount = 0;
10732 break;
10734 continue;
10736 case DT_INIT:
10737 name = info->init_function;
10738 goto get_sym;
10739 case DT_FINI:
10740 name = info->fini_function;
10741 get_sym:
10743 struct elf_link_hash_entry *h;
10745 h = elf_link_hash_lookup (elf_hash_table (info), name,
10746 FALSE, FALSE, TRUE);
10747 if (h != NULL
10748 && (h->root.type == bfd_link_hash_defined
10749 || h->root.type == bfd_link_hash_defweak))
10751 dyn.d_un.d_ptr = h->root.u.def.value;
10752 o = h->root.u.def.section;
10753 if (o->output_section != NULL)
10754 dyn.d_un.d_ptr += (o->output_section->vma
10755 + o->output_offset);
10756 else
10758 /* The symbol is imported from another shared
10759 library and does not apply to this one. */
10760 dyn.d_un.d_ptr = 0;
10762 break;
10765 continue;
10767 case DT_PREINIT_ARRAYSZ:
10768 name = ".preinit_array";
10769 goto get_size;
10770 case DT_INIT_ARRAYSZ:
10771 name = ".init_array";
10772 goto get_size;
10773 case DT_FINI_ARRAYSZ:
10774 name = ".fini_array";
10775 get_size:
10776 o = bfd_get_section_by_name (abfd, name);
10777 if (o == NULL)
10779 (*_bfd_error_handler)
10780 (_("%B: could not find output section %s"), abfd, name);
10781 goto error_return;
10783 if (o->size == 0)
10784 (*_bfd_error_handler)
10785 (_("warning: %s section has zero size"), name);
10786 dyn.d_un.d_val = o->size;
10787 break;
10789 case DT_PREINIT_ARRAY:
10790 name = ".preinit_array";
10791 goto get_vma;
10792 case DT_INIT_ARRAY:
10793 name = ".init_array";
10794 goto get_vma;
10795 case DT_FINI_ARRAY:
10796 name = ".fini_array";
10797 goto get_vma;
10799 case DT_HASH:
10800 name = ".hash";
10801 goto get_vma;
10802 case DT_GNU_HASH:
10803 name = ".gnu.hash";
10804 goto get_vma;
10805 case DT_STRTAB:
10806 name = ".dynstr";
10807 goto get_vma;
10808 case DT_SYMTAB:
10809 name = ".dynsym";
10810 goto get_vma;
10811 case DT_VERDEF:
10812 name = ".gnu.version_d";
10813 goto get_vma;
10814 case DT_VERNEED:
10815 name = ".gnu.version_r";
10816 goto get_vma;
10817 case DT_VERSYM:
10818 name = ".gnu.version";
10819 get_vma:
10820 o = bfd_get_section_by_name (abfd, name);
10821 if (o == NULL)
10823 (*_bfd_error_handler)
10824 (_("%B: could not find output section %s"), abfd, name);
10825 goto error_return;
10827 dyn.d_un.d_ptr = o->vma;
10828 break;
10830 case DT_REL:
10831 case DT_RELA:
10832 case DT_RELSZ:
10833 case DT_RELASZ:
10834 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10835 type = SHT_REL;
10836 else
10837 type = SHT_RELA;
10838 dyn.d_un.d_val = 0;
10839 dyn.d_un.d_ptr = 0;
10840 for (i = 1; i < elf_numsections (abfd); i++)
10842 Elf_Internal_Shdr *hdr;
10844 hdr = elf_elfsections (abfd)[i];
10845 if (hdr->sh_type == type
10846 && (hdr->sh_flags & SHF_ALLOC) != 0)
10848 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10849 dyn.d_un.d_val += hdr->sh_size;
10850 else
10852 if (dyn.d_un.d_ptr == 0
10853 || hdr->sh_addr < dyn.d_un.d_ptr)
10854 dyn.d_un.d_ptr = hdr->sh_addr;
10858 break;
10860 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10864 /* If we have created any dynamic sections, then output them. */
10865 if (dynobj != NULL)
10867 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10868 goto error_return;
10870 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10871 if (info->warn_shared_textrel && info->shared)
10873 bfd_byte *dyncon, *dynconend;
10875 /* Fix up .dynamic entries. */
10876 o = bfd_get_section_by_name (dynobj, ".dynamic");
10877 BFD_ASSERT (o != NULL);
10879 dyncon = o->contents;
10880 dynconend = o->contents + o->size;
10881 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10883 Elf_Internal_Dyn dyn;
10885 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10887 if (dyn.d_tag == DT_TEXTREL)
10889 info->callbacks->einfo
10890 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10891 break;
10896 for (o = dynobj->sections; o != NULL; o = o->next)
10898 if ((o->flags & SEC_HAS_CONTENTS) == 0
10899 || o->size == 0
10900 || o->output_section == bfd_abs_section_ptr)
10901 continue;
10902 if ((o->flags & SEC_LINKER_CREATED) == 0)
10904 /* At this point, we are only interested in sections
10905 created by _bfd_elf_link_create_dynamic_sections. */
10906 continue;
10908 if (elf_hash_table (info)->stab_info.stabstr == o)
10909 continue;
10910 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10911 continue;
10912 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10913 != SHT_STRTAB)
10914 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10916 if (! bfd_set_section_contents (abfd, o->output_section,
10917 o->contents,
10918 (file_ptr) o->output_offset,
10919 o->size))
10920 goto error_return;
10922 else
10924 /* The contents of the .dynstr section are actually in a
10925 stringtab. */
10926 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10927 if (bfd_seek (abfd, off, SEEK_SET) != 0
10928 || ! _bfd_elf_strtab_emit (abfd,
10929 elf_hash_table (info)->dynstr))
10930 goto error_return;
10935 if (info->relocatable)
10937 bfd_boolean failed = FALSE;
10939 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10940 if (failed)
10941 goto error_return;
10944 /* If we have optimized stabs strings, output them. */
10945 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10947 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10948 goto error_return;
10951 if (info->eh_frame_hdr)
10953 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10954 goto error_return;
10957 if (finfo.symstrtab != NULL)
10958 _bfd_stringtab_free (finfo.symstrtab);
10959 if (finfo.contents != NULL)
10960 free (finfo.contents);
10961 if (finfo.external_relocs != NULL)
10962 free (finfo.external_relocs);
10963 if (finfo.internal_relocs != NULL)
10964 free (finfo.internal_relocs);
10965 if (finfo.external_syms != NULL)
10966 free (finfo.external_syms);
10967 if (finfo.locsym_shndx != NULL)
10968 free (finfo.locsym_shndx);
10969 if (finfo.internal_syms != NULL)
10970 free (finfo.internal_syms);
10971 if (finfo.indices != NULL)
10972 free (finfo.indices);
10973 if (finfo.sections != NULL)
10974 free (finfo.sections);
10975 if (finfo.symbuf != NULL)
10976 free (finfo.symbuf);
10977 if (finfo.symshndxbuf != NULL)
10978 free (finfo.symshndxbuf);
10979 for (o = abfd->sections; o != NULL; o = o->next)
10981 if ((o->flags & SEC_RELOC) != 0
10982 && elf_section_data (o)->rel_hashes != NULL)
10983 free (elf_section_data (o)->rel_hashes);
10986 elf_tdata (abfd)->linker = TRUE;
10988 if (attr_section)
10990 bfd_byte *contents = bfd_malloc (attr_size);
10991 if (contents == NULL)
10992 return FALSE; /* Bail out and fail. */
10993 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
10994 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
10995 free (contents);
10998 return TRUE;
11000 error_return:
11001 if (finfo.symstrtab != NULL)
11002 _bfd_stringtab_free (finfo.symstrtab);
11003 if (finfo.contents != NULL)
11004 free (finfo.contents);
11005 if (finfo.external_relocs != NULL)
11006 free (finfo.external_relocs);
11007 if (finfo.internal_relocs != NULL)
11008 free (finfo.internal_relocs);
11009 if (finfo.external_syms != NULL)
11010 free (finfo.external_syms);
11011 if (finfo.locsym_shndx != NULL)
11012 free (finfo.locsym_shndx);
11013 if (finfo.internal_syms != NULL)
11014 free (finfo.internal_syms);
11015 if (finfo.indices != NULL)
11016 free (finfo.indices);
11017 if (finfo.sections != NULL)
11018 free (finfo.sections);
11019 if (finfo.symbuf != NULL)
11020 free (finfo.symbuf);
11021 if (finfo.symshndxbuf != NULL)
11022 free (finfo.symshndxbuf);
11023 for (o = abfd->sections; o != NULL; o = o->next)
11025 if ((o->flags & SEC_RELOC) != 0
11026 && elf_section_data (o)->rel_hashes != NULL)
11027 free (elf_section_data (o)->rel_hashes);
11030 return FALSE;
11033 /* Initialize COOKIE for input bfd ABFD. */
11035 static bfd_boolean
11036 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11037 struct bfd_link_info *info, bfd *abfd)
11039 Elf_Internal_Shdr *symtab_hdr;
11040 const struct elf_backend_data *bed;
11042 bed = get_elf_backend_data (abfd);
11043 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11045 cookie->abfd = abfd;
11046 cookie->sym_hashes = elf_sym_hashes (abfd);
11047 cookie->bad_symtab = elf_bad_symtab (abfd);
11048 if (cookie->bad_symtab)
11050 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11051 cookie->extsymoff = 0;
11053 else
11055 cookie->locsymcount = symtab_hdr->sh_info;
11056 cookie->extsymoff = symtab_hdr->sh_info;
11059 if (bed->s->arch_size == 32)
11060 cookie->r_sym_shift = 8;
11061 else
11062 cookie->r_sym_shift = 32;
11064 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11065 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11067 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11068 cookie->locsymcount, 0,
11069 NULL, NULL, NULL);
11070 if (cookie->locsyms == NULL)
11072 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11073 return FALSE;
11075 if (info->keep_memory)
11076 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11078 return TRUE;
11081 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11083 static void
11084 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11086 Elf_Internal_Shdr *symtab_hdr;
11088 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11089 if (cookie->locsyms != NULL
11090 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11091 free (cookie->locsyms);
11094 /* Initialize the relocation information in COOKIE for input section SEC
11095 of input bfd ABFD. */
11097 static bfd_boolean
11098 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11099 struct bfd_link_info *info, bfd *abfd,
11100 asection *sec)
11102 const struct elf_backend_data *bed;
11104 if (sec->reloc_count == 0)
11106 cookie->rels = NULL;
11107 cookie->relend = NULL;
11109 else
11111 bed = get_elf_backend_data (abfd);
11113 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11114 info->keep_memory);
11115 if (cookie->rels == NULL)
11116 return FALSE;
11117 cookie->rel = cookie->rels;
11118 cookie->relend = (cookie->rels
11119 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11121 cookie->rel = cookie->rels;
11122 return TRUE;
11125 /* Free the memory allocated by init_reloc_cookie_rels,
11126 if appropriate. */
11128 static void
11129 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11130 asection *sec)
11132 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11133 free (cookie->rels);
11136 /* Initialize the whole of COOKIE for input section SEC. */
11138 static bfd_boolean
11139 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11140 struct bfd_link_info *info,
11141 asection *sec)
11143 if (!init_reloc_cookie (cookie, info, sec->owner))
11144 goto error1;
11145 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11146 goto error2;
11147 return TRUE;
11149 error2:
11150 fini_reloc_cookie (cookie, sec->owner);
11151 error1:
11152 return FALSE;
11155 /* Free the memory allocated by init_reloc_cookie_for_section,
11156 if appropriate. */
11158 static void
11159 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11160 asection *sec)
11162 fini_reloc_cookie_rels (cookie, sec);
11163 fini_reloc_cookie (cookie, sec->owner);
11166 /* Garbage collect unused sections. */
11168 /* Default gc_mark_hook. */
11170 asection *
11171 _bfd_elf_gc_mark_hook (asection *sec,
11172 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11173 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11174 struct elf_link_hash_entry *h,
11175 Elf_Internal_Sym *sym)
11177 if (h != NULL)
11179 switch (h->root.type)
11181 case bfd_link_hash_defined:
11182 case bfd_link_hash_defweak:
11183 return h->root.u.def.section;
11185 case bfd_link_hash_common:
11186 return h->root.u.c.p->section;
11188 default:
11189 break;
11192 else
11193 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11195 return NULL;
11198 /* COOKIE->rel describes a relocation against section SEC, which is
11199 a section we've decided to keep. Return the section that contains
11200 the relocation symbol, or NULL if no section contains it. */
11202 asection *
11203 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11204 elf_gc_mark_hook_fn gc_mark_hook,
11205 struct elf_reloc_cookie *cookie)
11207 unsigned long r_symndx;
11208 struct elf_link_hash_entry *h;
11210 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11211 if (r_symndx == 0)
11212 return NULL;
11214 if (r_symndx >= cookie->locsymcount
11215 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11217 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11218 while (h->root.type == bfd_link_hash_indirect
11219 || h->root.type == bfd_link_hash_warning)
11220 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11221 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11224 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11225 &cookie->locsyms[r_symndx]);
11228 /* COOKIE->rel describes a relocation against section SEC, which is
11229 a section we've decided to keep. Mark the section that contains
11230 the relocation symbol. */
11232 bfd_boolean
11233 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11234 asection *sec,
11235 elf_gc_mark_hook_fn gc_mark_hook,
11236 struct elf_reloc_cookie *cookie)
11238 asection *rsec;
11240 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11241 if (rsec && !rsec->gc_mark)
11243 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11244 rsec->gc_mark = 1;
11245 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11246 return FALSE;
11248 return TRUE;
11251 /* The mark phase of garbage collection. For a given section, mark
11252 it and any sections in this section's group, and all the sections
11253 which define symbols to which it refers. */
11255 bfd_boolean
11256 _bfd_elf_gc_mark (struct bfd_link_info *info,
11257 asection *sec,
11258 elf_gc_mark_hook_fn gc_mark_hook)
11260 bfd_boolean ret;
11261 asection *group_sec, *eh_frame;
11263 sec->gc_mark = 1;
11265 /* Mark all the sections in the group. */
11266 group_sec = elf_section_data (sec)->next_in_group;
11267 if (group_sec && !group_sec->gc_mark)
11268 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11269 return FALSE;
11271 /* Look through the section relocs. */
11272 ret = TRUE;
11273 eh_frame = elf_eh_frame_section (sec->owner);
11274 if ((sec->flags & SEC_RELOC) != 0
11275 && sec->reloc_count > 0
11276 && sec != eh_frame)
11278 struct elf_reloc_cookie cookie;
11280 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11281 ret = FALSE;
11282 else
11284 for (; cookie.rel < cookie.relend; cookie.rel++)
11285 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11287 ret = FALSE;
11288 break;
11290 fini_reloc_cookie_for_section (&cookie, sec);
11294 if (ret && eh_frame && elf_fde_list (sec))
11296 struct elf_reloc_cookie cookie;
11298 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11299 ret = FALSE;
11300 else
11302 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11303 gc_mark_hook, &cookie))
11304 ret = FALSE;
11305 fini_reloc_cookie_for_section (&cookie, eh_frame);
11309 return ret;
11312 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11314 struct elf_gc_sweep_symbol_info
11316 struct bfd_link_info *info;
11317 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11318 bfd_boolean);
11321 static bfd_boolean
11322 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11324 if (h->root.type == bfd_link_hash_warning)
11325 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11327 if ((h->root.type == bfd_link_hash_defined
11328 || h->root.type == bfd_link_hash_defweak)
11329 && !h->root.u.def.section->gc_mark
11330 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11332 struct elf_gc_sweep_symbol_info *inf = data;
11333 (*inf->hide_symbol) (inf->info, h, TRUE);
11336 return TRUE;
11339 /* The sweep phase of garbage collection. Remove all garbage sections. */
11341 typedef bfd_boolean (*gc_sweep_hook_fn)
11342 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11344 static bfd_boolean
11345 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11347 bfd *sub;
11348 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11349 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11350 unsigned long section_sym_count;
11351 struct elf_gc_sweep_symbol_info sweep_info;
11353 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11355 asection *o;
11357 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11358 continue;
11360 for (o = sub->sections; o != NULL; o = o->next)
11362 /* Keep debug and special sections. */
11363 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
11364 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
11365 o->gc_mark = 1;
11367 if (o->gc_mark)
11368 continue;
11370 /* Skip sweeping sections already excluded. */
11371 if (o->flags & SEC_EXCLUDE)
11372 continue;
11374 /* Since this is early in the link process, it is simple
11375 to remove a section from the output. */
11376 o->flags |= SEC_EXCLUDE;
11378 if (info->print_gc_sections && o->size != 0)
11379 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11381 /* But we also have to update some of the relocation
11382 info we collected before. */
11383 if (gc_sweep_hook
11384 && (o->flags & SEC_RELOC) != 0
11385 && o->reloc_count > 0
11386 && !bfd_is_abs_section (o->output_section))
11388 Elf_Internal_Rela *internal_relocs;
11389 bfd_boolean r;
11391 internal_relocs
11392 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11393 info->keep_memory);
11394 if (internal_relocs == NULL)
11395 return FALSE;
11397 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11399 if (elf_section_data (o)->relocs != internal_relocs)
11400 free (internal_relocs);
11402 if (!r)
11403 return FALSE;
11408 /* Remove the symbols that were in the swept sections from the dynamic
11409 symbol table. GCFIXME: Anyone know how to get them out of the
11410 static symbol table as well? */
11411 sweep_info.info = info;
11412 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11413 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11414 &sweep_info);
11416 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
11417 return TRUE;
11420 /* Propagate collected vtable information. This is called through
11421 elf_link_hash_traverse. */
11423 static bfd_boolean
11424 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11426 if (h->root.type == bfd_link_hash_warning)
11427 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11429 /* Those that are not vtables. */
11430 if (h->vtable == NULL || h->vtable->parent == NULL)
11431 return TRUE;
11433 /* Those vtables that do not have parents, we cannot merge. */
11434 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11435 return TRUE;
11437 /* If we've already been done, exit. */
11438 if (h->vtable->used && h->vtable->used[-1])
11439 return TRUE;
11441 /* Make sure the parent's table is up to date. */
11442 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11444 if (h->vtable->used == NULL)
11446 /* None of this table's entries were referenced. Re-use the
11447 parent's table. */
11448 h->vtable->used = h->vtable->parent->vtable->used;
11449 h->vtable->size = h->vtable->parent->vtable->size;
11451 else
11453 size_t n;
11454 bfd_boolean *cu, *pu;
11456 /* Or the parent's entries into ours. */
11457 cu = h->vtable->used;
11458 cu[-1] = TRUE;
11459 pu = h->vtable->parent->vtable->used;
11460 if (pu != NULL)
11462 const struct elf_backend_data *bed;
11463 unsigned int log_file_align;
11465 bed = get_elf_backend_data (h->root.u.def.section->owner);
11466 log_file_align = bed->s->log_file_align;
11467 n = h->vtable->parent->vtable->size >> log_file_align;
11468 while (n--)
11470 if (*pu)
11471 *cu = TRUE;
11472 pu++;
11473 cu++;
11478 return TRUE;
11481 static bfd_boolean
11482 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11484 asection *sec;
11485 bfd_vma hstart, hend;
11486 Elf_Internal_Rela *relstart, *relend, *rel;
11487 const struct elf_backend_data *bed;
11488 unsigned int log_file_align;
11490 if (h->root.type == bfd_link_hash_warning)
11491 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11493 /* Take care of both those symbols that do not describe vtables as
11494 well as those that are not loaded. */
11495 if (h->vtable == NULL || h->vtable->parent == NULL)
11496 return TRUE;
11498 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11499 || h->root.type == bfd_link_hash_defweak);
11501 sec = h->root.u.def.section;
11502 hstart = h->root.u.def.value;
11503 hend = hstart + h->size;
11505 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11506 if (!relstart)
11507 return *(bfd_boolean *) okp = FALSE;
11508 bed = get_elf_backend_data (sec->owner);
11509 log_file_align = bed->s->log_file_align;
11511 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11513 for (rel = relstart; rel < relend; ++rel)
11514 if (rel->r_offset >= hstart && rel->r_offset < hend)
11516 /* If the entry is in use, do nothing. */
11517 if (h->vtable->used
11518 && (rel->r_offset - hstart) < h->vtable->size)
11520 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
11521 if (h->vtable->used[entry])
11522 continue;
11524 /* Otherwise, kill it. */
11525 rel->r_offset = rel->r_info = rel->r_addend = 0;
11528 return TRUE;
11531 /* Mark sections containing dynamically referenced symbols. When
11532 building shared libraries, we must assume that any visible symbol is
11533 referenced. */
11535 bfd_boolean
11536 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
11538 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11540 if (h->root.type == bfd_link_hash_warning)
11541 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11543 if ((h->root.type == bfd_link_hash_defined
11544 || h->root.type == bfd_link_hash_defweak)
11545 && (h->ref_dynamic
11546 || (!info->executable
11547 && h->def_regular
11548 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11549 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
11550 h->root.u.def.section->flags |= SEC_KEEP;
11552 return TRUE;
11555 /* Keep all sections containing symbols undefined on the command-line,
11556 and the section containing the entry symbol. */
11558 void
11559 _bfd_elf_gc_keep (struct bfd_link_info *info)
11561 struct bfd_sym_chain *sym;
11563 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11565 struct elf_link_hash_entry *h;
11567 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11568 FALSE, FALSE, FALSE);
11570 if (h != NULL
11571 && (h->root.type == bfd_link_hash_defined
11572 || h->root.type == bfd_link_hash_defweak)
11573 && !bfd_is_abs_section (h->root.u.def.section))
11574 h->root.u.def.section->flags |= SEC_KEEP;
11578 /* Do mark and sweep of unused sections. */
11580 bfd_boolean
11581 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11583 bfd_boolean ok = TRUE;
11584 bfd *sub;
11585 elf_gc_mark_hook_fn gc_mark_hook;
11586 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11588 if (!bed->can_gc_sections
11589 || !is_elf_hash_table (info->hash))
11591 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11592 return TRUE;
11595 bed->gc_keep (info);
11597 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11598 at the .eh_frame section if we can mark the FDEs individually. */
11599 _bfd_elf_begin_eh_frame_parsing (info);
11600 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11602 asection *sec;
11603 struct elf_reloc_cookie cookie;
11605 sec = bfd_get_section_by_name (sub, ".eh_frame");
11606 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11608 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11609 if (elf_section_data (sec)->sec_info)
11610 elf_eh_frame_section (sub) = sec;
11611 fini_reloc_cookie_for_section (&cookie, sec);
11614 _bfd_elf_end_eh_frame_parsing (info);
11616 /* Apply transitive closure to the vtable entry usage info. */
11617 elf_link_hash_traverse (elf_hash_table (info),
11618 elf_gc_propagate_vtable_entries_used,
11619 &ok);
11620 if (!ok)
11621 return FALSE;
11623 /* Kill the vtable relocations that were not used. */
11624 elf_link_hash_traverse (elf_hash_table (info),
11625 elf_gc_smash_unused_vtentry_relocs,
11626 &ok);
11627 if (!ok)
11628 return FALSE;
11630 /* Mark dynamically referenced symbols. */
11631 if (elf_hash_table (info)->dynamic_sections_created)
11632 elf_link_hash_traverse (elf_hash_table (info),
11633 bed->gc_mark_dynamic_ref,
11634 info);
11636 /* Grovel through relocs to find out who stays ... */
11637 gc_mark_hook = bed->gc_mark_hook;
11638 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11640 asection *o;
11642 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11643 continue;
11645 for (o = sub->sections; o != NULL; o = o->next)
11646 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
11647 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11648 return FALSE;
11651 /* Allow the backend to mark additional target specific sections. */
11652 if (bed->gc_mark_extra_sections)
11653 bed->gc_mark_extra_sections (info, gc_mark_hook);
11655 /* ... and mark SEC_EXCLUDE for those that go. */
11656 return elf_gc_sweep (abfd, info);
11659 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11661 bfd_boolean
11662 bfd_elf_gc_record_vtinherit (bfd *abfd,
11663 asection *sec,
11664 struct elf_link_hash_entry *h,
11665 bfd_vma offset)
11667 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
11668 struct elf_link_hash_entry **search, *child;
11669 bfd_size_type extsymcount;
11670 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11672 /* The sh_info field of the symtab header tells us where the
11673 external symbols start. We don't care about the local symbols at
11674 this point. */
11675 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
11676 if (!elf_bad_symtab (abfd))
11677 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
11679 sym_hashes = elf_sym_hashes (abfd);
11680 sym_hashes_end = sym_hashes + extsymcount;
11682 /* Hunt down the child symbol, which is in this section at the same
11683 offset as the relocation. */
11684 for (search = sym_hashes; search != sym_hashes_end; ++search)
11686 if ((child = *search) != NULL
11687 && (child->root.type == bfd_link_hash_defined
11688 || child->root.type == bfd_link_hash_defweak)
11689 && child->root.u.def.section == sec
11690 && child->root.u.def.value == offset)
11691 goto win;
11694 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
11695 abfd, sec, (unsigned long) offset);
11696 bfd_set_error (bfd_error_invalid_operation);
11697 return FALSE;
11699 win:
11700 if (!child->vtable)
11702 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
11703 if (!child->vtable)
11704 return FALSE;
11706 if (!h)
11708 /* This *should* only be the absolute section. It could potentially
11709 be that someone has defined a non-global vtable though, which
11710 would be bad. It isn't worth paging in the local symbols to be
11711 sure though; that case should simply be handled by the assembler. */
11713 child->vtable->parent = (struct elf_link_hash_entry *) -1;
11715 else
11716 child->vtable->parent = h;
11718 return TRUE;
11721 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11723 bfd_boolean
11724 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
11725 asection *sec ATTRIBUTE_UNUSED,
11726 struct elf_link_hash_entry *h,
11727 bfd_vma addend)
11729 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11730 unsigned int log_file_align = bed->s->log_file_align;
11732 if (!h->vtable)
11734 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
11735 if (!h->vtable)
11736 return FALSE;
11739 if (addend >= h->vtable->size)
11741 size_t size, bytes, file_align;
11742 bfd_boolean *ptr = h->vtable->used;
11744 /* While the symbol is undefined, we have to be prepared to handle
11745 a zero size. */
11746 file_align = 1 << log_file_align;
11747 if (h->root.type == bfd_link_hash_undefined)
11748 size = addend + file_align;
11749 else
11751 size = h->size;
11752 if (addend >= size)
11754 /* Oops! We've got a reference past the defined end of
11755 the table. This is probably a bug -- shall we warn? */
11756 size = addend + file_align;
11759 size = (size + file_align - 1) & -file_align;
11761 /* Allocate one extra entry for use as a "done" flag for the
11762 consolidation pass. */
11763 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11765 if (ptr)
11767 ptr = bfd_realloc (ptr - 1, bytes);
11769 if (ptr != NULL)
11771 size_t oldbytes;
11773 oldbytes = (((h->vtable->size >> log_file_align) + 1)
11774 * sizeof (bfd_boolean));
11775 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11778 else
11779 ptr = bfd_zmalloc (bytes);
11781 if (ptr == NULL)
11782 return FALSE;
11784 /* And arrange for that done flag to be at index -1. */
11785 h->vtable->used = ptr + 1;
11786 h->vtable->size = size;
11789 h->vtable->used[addend >> log_file_align] = TRUE;
11791 return TRUE;
11794 struct alloc_got_off_arg {
11795 bfd_vma gotoff;
11796 struct bfd_link_info *info;
11799 /* We need a special top-level link routine to convert got reference counts
11800 to real got offsets. */
11802 static bfd_boolean
11803 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11805 struct alloc_got_off_arg *gofarg = arg;
11806 bfd *obfd = gofarg->info->output_bfd;
11807 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
11809 if (h->root.type == bfd_link_hash_warning)
11810 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11812 if (h->got.refcount > 0)
11814 h->got.offset = gofarg->gotoff;
11815 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
11817 else
11818 h->got.offset = (bfd_vma) -1;
11820 return TRUE;
11823 /* And an accompanying bit to work out final got entry offsets once
11824 we're done. Should be called from final_link. */
11826 bfd_boolean
11827 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11828 struct bfd_link_info *info)
11830 bfd *i;
11831 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11832 bfd_vma gotoff;
11833 struct alloc_got_off_arg gofarg;
11835 BFD_ASSERT (abfd == info->output_bfd);
11837 if (! is_elf_hash_table (info->hash))
11838 return FALSE;
11840 /* The GOT offset is relative to the .got section, but the GOT header is
11841 put into the .got.plt section, if the backend uses it. */
11842 if (bed->want_got_plt)
11843 gotoff = 0;
11844 else
11845 gotoff = bed->got_header_size;
11847 /* Do the local .got entries first. */
11848 for (i = info->input_bfds; i; i = i->link_next)
11850 bfd_signed_vma *local_got;
11851 bfd_size_type j, locsymcount;
11852 Elf_Internal_Shdr *symtab_hdr;
11854 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11855 continue;
11857 local_got = elf_local_got_refcounts (i);
11858 if (!local_got)
11859 continue;
11861 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11862 if (elf_bad_symtab (i))
11863 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11864 else
11865 locsymcount = symtab_hdr->sh_info;
11867 for (j = 0; j < locsymcount; ++j)
11869 if (local_got[j] > 0)
11871 local_got[j] = gotoff;
11872 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
11874 else
11875 local_got[j] = (bfd_vma) -1;
11879 /* Then the global .got entries. .plt refcounts are handled by
11880 adjust_dynamic_symbol */
11881 gofarg.gotoff = gotoff;
11882 gofarg.info = info;
11883 elf_link_hash_traverse (elf_hash_table (info),
11884 elf_gc_allocate_got_offsets,
11885 &gofarg);
11886 return TRUE;
11889 /* Many folk need no more in the way of final link than this, once
11890 got entry reference counting is enabled. */
11892 bfd_boolean
11893 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11895 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11896 return FALSE;
11898 /* Invoke the regular ELF backend linker to do all the work. */
11899 return bfd_elf_final_link (abfd, info);
11902 bfd_boolean
11903 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11905 struct elf_reloc_cookie *rcookie = cookie;
11907 if (rcookie->bad_symtab)
11908 rcookie->rel = rcookie->rels;
11910 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11912 unsigned long r_symndx;
11914 if (! rcookie->bad_symtab)
11915 if (rcookie->rel->r_offset > offset)
11916 return FALSE;
11917 if (rcookie->rel->r_offset != offset)
11918 continue;
11920 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11921 if (r_symndx == SHN_UNDEF)
11922 return TRUE;
11924 if (r_symndx >= rcookie->locsymcount
11925 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11927 struct elf_link_hash_entry *h;
11929 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11931 while (h->root.type == bfd_link_hash_indirect
11932 || h->root.type == bfd_link_hash_warning)
11933 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11935 if ((h->root.type == bfd_link_hash_defined
11936 || h->root.type == bfd_link_hash_defweak)
11937 && elf_discarded_section (h->root.u.def.section))
11938 return TRUE;
11939 else
11940 return FALSE;
11942 else
11944 /* It's not a relocation against a global symbol,
11945 but it could be a relocation against a local
11946 symbol for a discarded section. */
11947 asection *isec;
11948 Elf_Internal_Sym *isym;
11950 /* Need to: get the symbol; get the section. */
11951 isym = &rcookie->locsyms[r_symndx];
11952 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11953 if (isec != NULL && elf_discarded_section (isec))
11954 return TRUE;
11956 return FALSE;
11958 return FALSE;
11961 /* Discard unneeded references to discarded sections.
11962 Returns TRUE if any section's size was changed. */
11963 /* This function assumes that the relocations are in sorted order,
11964 which is true for all known assemblers. */
11966 bfd_boolean
11967 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
11969 struct elf_reloc_cookie cookie;
11970 asection *stab, *eh;
11971 const struct elf_backend_data *bed;
11972 bfd *abfd;
11973 bfd_boolean ret = FALSE;
11975 if (info->traditional_format
11976 || !is_elf_hash_table (info->hash))
11977 return FALSE;
11979 _bfd_elf_begin_eh_frame_parsing (info);
11980 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
11982 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11983 continue;
11985 bed = get_elf_backend_data (abfd);
11987 if ((abfd->flags & DYNAMIC) != 0)
11988 continue;
11990 eh = NULL;
11991 if (!info->relocatable)
11993 eh = bfd_get_section_by_name (abfd, ".eh_frame");
11994 if (eh != NULL
11995 && (eh->size == 0
11996 || bfd_is_abs_section (eh->output_section)))
11997 eh = NULL;
12000 stab = bfd_get_section_by_name (abfd, ".stab");
12001 if (stab != NULL
12002 && (stab->size == 0
12003 || bfd_is_abs_section (stab->output_section)
12004 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12005 stab = NULL;
12007 if (stab == NULL
12008 && eh == NULL
12009 && bed->elf_backend_discard_info == NULL)
12010 continue;
12012 if (!init_reloc_cookie (&cookie, info, abfd))
12013 return FALSE;
12015 if (stab != NULL
12016 && stab->reloc_count > 0
12017 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
12019 if (_bfd_discard_section_stabs (abfd, stab,
12020 elf_section_data (stab)->sec_info,
12021 bfd_elf_reloc_symbol_deleted_p,
12022 &cookie))
12023 ret = TRUE;
12024 fini_reloc_cookie_rels (&cookie, stab);
12027 if (eh != NULL
12028 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
12030 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
12031 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12032 bfd_elf_reloc_symbol_deleted_p,
12033 &cookie))
12034 ret = TRUE;
12035 fini_reloc_cookie_rels (&cookie, eh);
12038 if (bed->elf_backend_discard_info != NULL
12039 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12040 ret = TRUE;
12042 fini_reloc_cookie (&cookie, abfd);
12044 _bfd_elf_end_eh_frame_parsing (info);
12046 if (info->eh_frame_hdr
12047 && !info->relocatable
12048 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12049 ret = TRUE;
12051 return ret;
12054 /* For a SHT_GROUP section, return the group signature. For other
12055 sections, return the normal section name. */
12057 static const char *
12058 section_signature (asection *sec)
12060 if ((sec->flags & SEC_GROUP) != 0
12061 && elf_next_in_group (sec) != NULL
12062 && elf_group_name (elf_next_in_group (sec)) != NULL)
12063 return elf_group_name (elf_next_in_group (sec));
12064 return sec->name;
12067 void
12068 _bfd_elf_section_already_linked (bfd *abfd, asection *sec,
12069 struct bfd_link_info *info)
12071 flagword flags;
12072 const char *name, *p;
12073 struct bfd_section_already_linked *l;
12074 struct bfd_section_already_linked_hash_entry *already_linked_list;
12076 if (sec->output_section == bfd_abs_section_ptr)
12077 return;
12079 flags = sec->flags;
12081 /* Return if it isn't a linkonce section. A comdat group section
12082 also has SEC_LINK_ONCE set. */
12083 if ((flags & SEC_LINK_ONCE) == 0)
12084 return;
12086 /* Don't put group member sections on our list of already linked
12087 sections. They are handled as a group via their group section. */
12088 if (elf_sec_group (sec) != NULL)
12089 return;
12091 /* FIXME: When doing a relocatable link, we may have trouble
12092 copying relocations in other sections that refer to local symbols
12093 in the section being discarded. Those relocations will have to
12094 be converted somehow; as of this writing I'm not sure that any of
12095 the backends handle that correctly.
12097 It is tempting to instead not discard link once sections when
12098 doing a relocatable link (technically, they should be discarded
12099 whenever we are building constructors). However, that fails,
12100 because the linker winds up combining all the link once sections
12101 into a single large link once section, which defeats the purpose
12102 of having link once sections in the first place.
12104 Also, not merging link once sections in a relocatable link
12105 causes trouble for MIPS ELF, which relies on link once semantics
12106 to handle the .reginfo section correctly. */
12108 name = section_signature (sec);
12110 if (CONST_STRNEQ (name, ".gnu.linkonce.")
12111 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12112 p++;
12113 else
12114 p = name;
12116 already_linked_list = bfd_section_already_linked_table_lookup (p);
12118 for (l = already_linked_list->entry; l != NULL; l = l->next)
12120 /* We may have 2 different types of sections on the list: group
12121 sections and linkonce sections. Match like sections. */
12122 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12123 && strcmp (name, section_signature (l->sec)) == 0
12124 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12126 /* The section has already been linked. See if we should
12127 issue a warning. */
12128 switch (flags & SEC_LINK_DUPLICATES)
12130 default:
12131 abort ();
12133 case SEC_LINK_DUPLICATES_DISCARD:
12134 break;
12136 case SEC_LINK_DUPLICATES_ONE_ONLY:
12137 (*_bfd_error_handler)
12138 (_("%B: ignoring duplicate section `%A'"),
12139 abfd, sec);
12140 break;
12142 case SEC_LINK_DUPLICATES_SAME_SIZE:
12143 if (sec->size != l->sec->size)
12144 (*_bfd_error_handler)
12145 (_("%B: duplicate section `%A' has different size"),
12146 abfd, sec);
12147 break;
12149 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12150 if (sec->size != l->sec->size)
12151 (*_bfd_error_handler)
12152 (_("%B: duplicate section `%A' has different size"),
12153 abfd, sec);
12154 else if (sec->size != 0)
12156 bfd_byte *sec_contents, *l_sec_contents;
12158 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12159 (*_bfd_error_handler)
12160 (_("%B: warning: could not read contents of section `%A'"),
12161 abfd, sec);
12162 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12163 &l_sec_contents))
12164 (*_bfd_error_handler)
12165 (_("%B: warning: could not read contents of section `%A'"),
12166 l->sec->owner, l->sec);
12167 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12168 (*_bfd_error_handler)
12169 (_("%B: warning: duplicate section `%A' has different contents"),
12170 abfd, sec);
12172 if (sec_contents)
12173 free (sec_contents);
12174 if (l_sec_contents)
12175 free (l_sec_contents);
12177 break;
12180 /* Set the output_section field so that lang_add_section
12181 does not create a lang_input_section structure for this
12182 section. Since there might be a symbol in the section
12183 being discarded, we must retain a pointer to the section
12184 which we are really going to use. */
12185 sec->output_section = bfd_abs_section_ptr;
12186 sec->kept_section = l->sec;
12188 if (flags & SEC_GROUP)
12190 asection *first = elf_next_in_group (sec);
12191 asection *s = first;
12193 while (s != NULL)
12195 s->output_section = bfd_abs_section_ptr;
12196 /* Record which group discards it. */
12197 s->kept_section = l->sec;
12198 s = elf_next_in_group (s);
12199 /* These lists are circular. */
12200 if (s == first)
12201 break;
12205 return;
12209 /* A single member comdat group section may be discarded by a
12210 linkonce section and vice versa. */
12212 if ((flags & SEC_GROUP) != 0)
12214 asection *first = elf_next_in_group (sec);
12216 if (first != NULL && elf_next_in_group (first) == first)
12217 /* Check this single member group against linkonce sections. */
12218 for (l = already_linked_list->entry; l != NULL; l = l->next)
12219 if ((l->sec->flags & SEC_GROUP) == 0
12220 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12221 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12223 first->output_section = bfd_abs_section_ptr;
12224 first->kept_section = l->sec;
12225 sec->output_section = bfd_abs_section_ptr;
12226 break;
12229 else
12230 /* Check this linkonce section against single member groups. */
12231 for (l = already_linked_list->entry; l != NULL; l = l->next)
12232 if (l->sec->flags & SEC_GROUP)
12234 asection *first = elf_next_in_group (l->sec);
12236 if (first != NULL
12237 && elf_next_in_group (first) == first
12238 && bfd_elf_match_symbols_in_sections (first, sec, info))
12240 sec->output_section = bfd_abs_section_ptr;
12241 sec->kept_section = first;
12242 break;
12246 /* This is the first section with this name. Record it. */
12247 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
12248 info->callbacks->einfo (_("%F%P: already_linked_table: %E"));
12251 bfd_boolean
12252 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
12254 return sym->st_shndx == SHN_COMMON;
12257 unsigned int
12258 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12260 return SHN_COMMON;
12263 asection *
12264 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12266 return bfd_com_section_ptr;
12269 bfd_vma
12270 _bfd_elf_default_got_elt_size (bfd *abfd,
12271 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12272 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12273 bfd *ibfd ATTRIBUTE_UNUSED,
12274 unsigned long symndx ATTRIBUTE_UNUSED)
12276 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12277 return bed->s->arch_size / 8;
12280 /* Routines to support the creation of dynamic relocs. */
12282 /* Return true if NAME is a name of a relocation
12283 section associated with section S. */
12285 static bfd_boolean
12286 is_reloc_section (bfd_boolean rela, const char * name, asection * s)
12288 if (rela)
12289 return CONST_STRNEQ (name, ".rela")
12290 && strcmp (bfd_get_section_name (NULL, s), name + 5) == 0;
12292 return CONST_STRNEQ (name, ".rel")
12293 && strcmp (bfd_get_section_name (NULL, s), name + 4) == 0;
12296 /* Returns the name of the dynamic reloc section associated with SEC. */
12298 static const char *
12299 get_dynamic_reloc_section_name (bfd * abfd,
12300 asection * sec,
12301 bfd_boolean is_rela)
12303 const char * name;
12304 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
12305 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
12307 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
12308 if (name == NULL)
12309 return NULL;
12311 if (! is_reloc_section (is_rela, name, sec))
12313 static bfd_boolean complained = FALSE;
12315 if (! complained)
12317 (*_bfd_error_handler)
12318 (_("%B: bad relocation section name `%s\'"), abfd, name);
12319 complained = TRUE;
12321 name = NULL;
12324 return name;
12327 /* Returns the dynamic reloc section associated with SEC.
12328 If necessary compute the name of the dynamic reloc section based
12329 on SEC's name (looked up in ABFD's string table) and the setting
12330 of IS_RELA. */
12332 asection *
12333 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12334 asection * sec,
12335 bfd_boolean is_rela)
12337 asection * reloc_sec = elf_section_data (sec)->sreloc;
12339 if (reloc_sec == NULL)
12341 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12343 if (name != NULL)
12345 reloc_sec = bfd_get_section_by_name (abfd, name);
12347 if (reloc_sec != NULL)
12348 elf_section_data (sec)->sreloc = reloc_sec;
12352 return reloc_sec;
12355 /* Returns the dynamic reloc section associated with SEC. If the
12356 section does not exist it is created and attached to the DYNOBJ
12357 bfd and stored in the SRELOC field of SEC's elf_section_data
12358 structure.
12360 ALIGNMENT is the alignment for the newly created section and
12361 IS_RELA defines whether the name should be .rela.<SEC's name>
12362 or .rel.<SEC's name>. The section name is looked up in the
12363 string table associated with ABFD. */
12365 asection *
12366 _bfd_elf_make_dynamic_reloc_section (asection * sec,
12367 bfd * dynobj,
12368 unsigned int alignment,
12369 bfd * abfd,
12370 bfd_boolean is_rela)
12372 asection * reloc_sec = elf_section_data (sec)->sreloc;
12374 if (reloc_sec == NULL)
12376 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12378 if (name == NULL)
12379 return NULL;
12381 reloc_sec = bfd_get_section_by_name (dynobj, name);
12383 if (reloc_sec == NULL)
12385 flagword flags;
12387 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12388 if ((sec->flags & SEC_ALLOC) != 0)
12389 flags |= SEC_ALLOC | SEC_LOAD;
12391 reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12392 if (reloc_sec != NULL)
12394 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12395 reloc_sec = NULL;
12399 elf_section_data (sec)->sreloc = reloc_sec;
12402 return reloc_sec;